axpy.c 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246
  1. /* StarPU --- Runtime system for heterogeneous multicore architectures.
  2. *
  3. * Copyright (C) 2009-2020 Université de Bordeaux, CNRS (LaBRI UMR 5800), Inria
  4. * Copyright (C) 2010 Mehdi Juhoor
  5. *
  6. * StarPU is free software; you can redistribute it and/or modify
  7. * it under the terms of the GNU Lesser General Public License as published by
  8. * the Free Software Foundation; either version 2.1 of the License, or (at
  9. * your option) any later version.
  10. *
  11. * StarPU is distributed in the hope that it will be useful, but
  12. * WITHOUT ANY WARRANTY; without even the implied warranty of
  13. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
  14. *
  15. * See the GNU Lesser General Public License in COPYING.LGPL for more details.
  16. */
  17. /*
  18. * This creates two dumb vectors, splits them into chunks, and for each pair of
  19. * chunk, run axpy on them.
  20. */
  21. #include <starpu.h>
  22. #include <stdlib.h>
  23. #include <stdio.h>
  24. #include <assert.h>
  25. #include <math.h>
  26. #include <common/blas.h>
  27. #ifdef STARPU_USE_CUDA
  28. #include <starpu_cublas_v2.h>
  29. #endif
  30. #include "axpy.h"
  31. #define AXPY STARPU_SAXPY
  32. #define CUBLASAXPY cublasSaxpy
  33. #define N (16*1024*1024)
  34. #define NBLOCKS 8
  35. #define FPRINTF(ofile, fmt, ...) do { if (!getenv("STARPU_SSILENT")) {fprintf(ofile, fmt, ## __VA_ARGS__); }} while(0)
  36. #define EPSILON 1e-6
  37. TYPE *_vec_x, *_vec_y;
  38. TYPE _alpha = 3.41;
  39. /* descriptors for StarPU */
  40. starpu_data_handle_t _handle_y, _handle_x;
  41. void axpy_cpu(void *descr[], void *arg)
  42. {
  43. TYPE alpha = *((TYPE *)arg);
  44. unsigned n = STARPU_VECTOR_GET_NX(descr[0]);
  45. TYPE *block_x = (TYPE *)STARPU_VECTOR_GET_PTR(descr[0]);
  46. TYPE *block_y = (TYPE *)STARPU_VECTOR_GET_PTR(descr[1]);
  47. AXPY((int)n, alpha, block_x, 1, block_y, 1);
  48. }
  49. #ifdef STARPU_USE_CUDA
  50. void axpy_gpu(void *descr[], void *arg)
  51. {
  52. TYPE alpha = *((TYPE *)arg);
  53. unsigned n = STARPU_VECTOR_GET_NX(descr[0]);
  54. TYPE *block_x = (TYPE *)STARPU_VECTOR_GET_PTR(descr[0]);
  55. TYPE *block_y = (TYPE *)STARPU_VECTOR_GET_PTR(descr[1]);
  56. cublasStatus_t status = CUBLASAXPY(starpu_cublas_get_local_handle(), (int)n, &alpha, block_x, 1, block_y, 1);
  57. if (status != CUBLAS_STATUS_SUCCESS)
  58. STARPU_CUBLAS_REPORT_ERROR(status);
  59. }
  60. #endif
  61. #ifdef STARPU_USE_OPENCL
  62. extern void axpy_opencl(void *buffers[], void *args);
  63. #endif
  64. static struct starpu_perfmodel axpy_model =
  65. {
  66. .type = STARPU_HISTORY_BASED,
  67. .symbol = "axpy"
  68. };
  69. static struct starpu_codelet axpy_cl =
  70. {
  71. .cpu_funcs = {axpy_cpu},
  72. .cpu_funcs_name = {"axpy_cpu"},
  73. #ifdef STARPU_USE_CUDA
  74. .cuda_funcs = {axpy_gpu},
  75. #elif defined(STARPU_SIMGRID)
  76. .cuda_funcs = {(void*)1},
  77. #endif
  78. .cuda_flags = {STARPU_CUDA_ASYNC},
  79. #ifdef STARPU_USE_OPENCL
  80. .opencl_funcs = {axpy_opencl},
  81. #elif defined(STARPU_SIMGRID)
  82. .opencl_funcs = {(void*)1},
  83. #endif
  84. .opencl_flags = {STARPU_OPENCL_ASYNC},
  85. .nbuffers = 2,
  86. .modes = {STARPU_R, STARPU_RW},
  87. .name = "axpy",
  88. .model = &axpy_model
  89. };
  90. static int
  91. check(void)
  92. {
  93. int i;
  94. for (i = 0; i < N; i++)
  95. {
  96. TYPE expected_value = _alpha * _vec_x[i] + 4.0;
  97. if (fabs(_vec_y[i] - expected_value) > expected_value * EPSILON)
  98. {
  99. FPRINTF(stderr,"at %d, %f*%f+%f=%f, expected %f\n", i, _alpha, _vec_x[i], 4.0, _vec_y[i], expected_value);
  100. return EXIT_FAILURE;
  101. }
  102. }
  103. return EXIT_SUCCESS;
  104. }
  105. #ifdef STARPU_USE_OPENCL
  106. struct starpu_opencl_program opencl_program;
  107. #endif
  108. int main(void)
  109. {
  110. int ret, exit_value = 0;
  111. /* Initialize StarPU */
  112. ret = starpu_init(NULL);
  113. if (ret == -ENODEV)
  114. return 77;
  115. STARPU_CHECK_RETURN_VALUE(ret, "starpu_init");
  116. #ifdef STARPU_USE_OPENCL
  117. ret = starpu_opencl_load_opencl_from_file("examples/axpy/axpy_opencl_kernel.cl",
  118. &opencl_program, NULL);
  119. STARPU_CHECK_RETURN_VALUE(ret, "starpu_opencl_load_opencl_from_file");
  120. #endif
  121. starpu_cublas_init();
  122. /* This is equivalent to
  123. vec_a = malloc(N*sizeof(TYPE));
  124. vec_b = malloc(N*sizeof(TYPE));
  125. */
  126. starpu_malloc((void **)&_vec_x, N*sizeof(TYPE));
  127. assert(_vec_x);
  128. starpu_malloc((void **)&_vec_y, N*sizeof(TYPE));
  129. assert(_vec_y);
  130. unsigned i;
  131. for (i = 0; i < N; i++)
  132. {
  133. _vec_x[i] = 1.0f; /*(TYPE)starpu_drand48(); */
  134. _vec_y[i] = 4.0f; /*(TYPE)starpu_drand48(); */
  135. }
  136. FPRINTF(stderr, "BEFORE x[0] = %2.2f\n", _vec_x[0]);
  137. FPRINTF(stderr, "BEFORE y[0] = %2.2f\n", _vec_y[0]);
  138. /* Declare the data to StarPU */
  139. starpu_vector_data_register(&_handle_x, STARPU_MAIN_RAM, (uintptr_t)_vec_x, N, sizeof(TYPE));
  140. starpu_vector_data_register(&_handle_y, STARPU_MAIN_RAM, (uintptr_t)_vec_y, N, sizeof(TYPE));
  141. /* Divide the vector into blocks */
  142. struct starpu_data_filter block_filter =
  143. {
  144. .filter_func = starpu_vector_filter_block,
  145. .nchildren = NBLOCKS
  146. };
  147. starpu_data_partition(_handle_x, &block_filter);
  148. starpu_data_partition(_handle_y, &block_filter);
  149. double start;
  150. double end;
  151. start = starpu_timing_now();
  152. unsigned b;
  153. for (b = 0; b < NBLOCKS; b++)
  154. {
  155. struct starpu_task *task = starpu_task_create();
  156. task->cl = &axpy_cl;
  157. task->cl_arg = &_alpha;
  158. task->cl_arg_size = sizeof(_alpha);
  159. task->handles[0] = starpu_data_get_sub_data(_handle_x, 1, b);
  160. task->handles[1] = starpu_data_get_sub_data(_handle_y, 1, b);
  161. task->tag_id = b;
  162. ret = starpu_task_submit(task);
  163. if (ret == -ENODEV)
  164. {
  165. exit_value = 77;
  166. goto enodev;
  167. }
  168. STARPU_CHECK_RETURN_VALUE(ret, "starpu_task_submit");
  169. }
  170. starpu_task_wait_for_all();
  171. enodev:
  172. starpu_data_unpartition(_handle_x, STARPU_MAIN_RAM);
  173. starpu_data_unpartition(_handle_y, STARPU_MAIN_RAM);
  174. starpu_data_unregister(_handle_x);
  175. starpu_data_unregister(_handle_y);
  176. end = starpu_timing_now();
  177. double timing = end - start;
  178. FPRINTF(stderr, "timing -> %2.2f us %2.2f MB/s\n", timing, 3*N*sizeof(TYPE)/timing);
  179. FPRINTF(stderr, "AFTER y[0] = %2.2f (ALPHA = %2.2f)\n", _vec_y[0], _alpha);
  180. if (exit_value != 77)
  181. exit_value = check();
  182. starpu_free((void *)_vec_x);
  183. starpu_free((void *)_vec_y);
  184. #ifdef STARPU_USE_OPENCL
  185. ret = starpu_opencl_unload_opencl(&opencl_program);
  186. STARPU_CHECK_RETURN_VALUE(ret, "starpu_opencl_unload_opencl");
  187. #endif
  188. /* Stop StarPU */
  189. starpu_shutdown();
  190. return exit_value;
  191. }