axpy.c 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168
  1. /*
  2. * StarPU
  3. * Copyright (C) Université Bordeaux 1, CNRS 2008-2010 (see AUTHORS file)
  4. *
  5. * This program is free software; you can redistribute it and/or modify
  6. * it under the terms of the GNU Lesser General Public License as published by
  7. * the Free Software Foundation; either version 2.1 of the License, or (at
  8. * your option) any later version.
  9. *
  10. * This program is distributed in the hope that it will be useful, but
  11. * WITHOUT ANY WARRANTY; without even the implied warranty of
  12. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
  13. *
  14. * See the GNU Lesser General Public License in COPYING.LGPL for more details.
  15. */
  16. #include <starpu.h>
  17. #include <stdlib.h>
  18. #include <stdio.h>
  19. #include <assert.h>
  20. #include <sys/time.h>
  21. #include <common/blas.h>
  22. #ifdef STARPU_USE_CUDA
  23. #include <cublas.h>
  24. #endif
  25. #define TYPE float
  26. #define AXPY SAXPY
  27. #define CUBLASAXPY cublasSaxpy
  28. #define N (16*1024*1024)
  29. #define NBLOCKS 8
  30. TYPE *vec_x, *vec_y;
  31. /* descriptors for StarPU */
  32. starpu_data_handle handle_y, handle_x;
  33. void axpy_cpu(void *descr[], __attribute__((unused)) void *arg)
  34. {
  35. TYPE alpha = *((TYPE *)arg);
  36. unsigned n = STARPU_VECTOR_GET_NX(descr[0]);
  37. TYPE *block_x = (TYPE *)STARPU_VECTOR_GET_PTR(descr[0]);
  38. TYPE *block_y = (TYPE *)STARPU_VECTOR_GET_PTR(descr[1]);
  39. AXPY((int)n, alpha, block_x, 1, block_y, 1);
  40. }
  41. #ifdef STARPU_USE_CUDA
  42. void axpy_gpu(void *descr[], __attribute__((unused)) void *arg)
  43. {
  44. TYPE alpha = *((TYPE *)arg);
  45. unsigned n = STARPU_VECTOR_GET_NX(descr[0]);
  46. TYPE *block_x = (TYPE *)STARPU_VECTOR_GET_PTR(descr[0]);
  47. TYPE *block_y = (TYPE *)STARPU_VECTOR_GET_PTR(descr[1]);
  48. CUBLASAXPY((int)n, alpha, block_x, 1, block_y, 1);
  49. cudaThreadSynchronize();
  50. }
  51. #endif
  52. static starpu_codelet axpy_cl = {
  53. .where =
  54. #ifdef STARPU_USE_CUDA
  55. STARPU_CUDA|
  56. #endif
  57. STARPU_CPU,
  58. .cpu_func = axpy_cpu,
  59. #ifdef STARPU_USE_CUDA
  60. .cuda_func = axpy_gpu,
  61. #endif
  62. .nbuffers = 2
  63. };
  64. int main(int argc, char **argv)
  65. {
  66. /* Initialize StarPU */
  67. starpu_init(NULL);
  68. starpu_helper_cublas_init();
  69. /* This is equivalent to
  70. vec_a = malloc(N*sizeof(TYPE));
  71. vec_b = malloc(N*sizeof(TYPE));
  72. */
  73. starpu_data_malloc_pinned_if_possible((void **)&vec_x, N*sizeof(TYPE));
  74. assert(vec_x);
  75. starpu_data_malloc_pinned_if_possible((void **)&vec_y, N*sizeof(TYPE));
  76. assert(vec_y);
  77. unsigned i;
  78. for (i = 0; i < N; i++)
  79. {
  80. vec_x[i] = 1.0f;//(TYPE)starpu_drand48();
  81. vec_y[i] = 4.0f;//(TYPE)starpu_drand48();
  82. }
  83. fprintf(stderr, "BEFORE x[0] = %2.2f\n", vec_x[0]);
  84. fprintf(stderr, "BEFORE y[0] = %2.2f\n", vec_y[0]);
  85. /* Declare the data to StarPU */
  86. starpu_vector_data_register(&handle_x, 0, (uintptr_t)vec_x, N, sizeof(TYPE));
  87. starpu_vector_data_register(&handle_y, 0, (uintptr_t)vec_y, N, sizeof(TYPE));
  88. /* Divide the vector into blocks */
  89. struct starpu_data_filter block_filter = {
  90. .filter_func = starpu_block_filter_func_vector,
  91. .nchildren = NBLOCKS,
  92. .get_nchildren = NULL,
  93. .get_child_ops = NULL
  94. };
  95. starpu_data_partition(handle_x, &block_filter);
  96. starpu_data_partition(handle_y, &block_filter);
  97. TYPE alpha = 3.41;
  98. struct timeval start;
  99. struct timeval end;
  100. gettimeofday(&start, NULL);
  101. unsigned b;
  102. for (b = 0; b < NBLOCKS; b++)
  103. {
  104. struct starpu_task *task = starpu_task_create();
  105. task->cl = &axpy_cl;
  106. task->cl_arg = &alpha;
  107. task->buffers[0].handle = starpu_data_get_sub_data(handle_x, 1, b);
  108. task->buffers[0].mode = STARPU_R;
  109. task->buffers[1].handle = starpu_data_get_sub_data(handle_y, 1, b);
  110. task->buffers[1].mode = STARPU_RW;
  111. starpu_task_submit(task);
  112. }
  113. starpu_task_wait_for_all();
  114. starpu_data_unpartition(handle_y, 0);
  115. starpu_data_unregister(handle_y);
  116. gettimeofday(&end, NULL);
  117. double timing = (double)((end.tv_sec - start.tv_sec)*1000000 +
  118. (end.tv_usec - start.tv_usec));
  119. fprintf(stderr, "timing -> %2.2f us %2.2f MB/s\n", timing, 3*N*sizeof(TYPE)/timing);
  120. fprintf(stderr, "AFTER y[0] = %2.2f (ALPHA = %2.2f)\n", vec_y[0], alpha);
  121. /* Stop StarPU */
  122. starpu_shutdown();
  123. return 0;
  124. }