axpy.c 5.9 KB

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