axpy.c 6.0 KB

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