axpy.c 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245
  1. /* StarPU --- Runtime system for heterogeneous multicore architectures.
  2. *
  3. * Copyright (C) 2009-2015 Université de Bordeaux
  4. * Copyright (C) 2010 Mehdi Juhoor <mjuhoor@gmail.com>
  5. * Copyright (C) 2010, 2011, 2012, 2013, 2015 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 <cublas.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[], STARPU_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. AXPY((int)n, alpha, block_x, 1, block_y, 1);
  49. }
  50. #ifdef STARPU_USE_CUDA
  51. void axpy_gpu(void *descr[], STARPU_ATTRIBUTE_UNUSED 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. CUBLASAXPY((int)n, alpha, block_x, 1, block_y, 1);
  58. }
  59. #endif
  60. #ifdef STARPU_USE_OPENCL
  61. extern void axpy_opencl(void *buffers[], void *args);
  62. #endif
  63. static struct starpu_perfmodel axpy_model =
  64. {
  65. .type = STARPU_HISTORY_BASED,
  66. .symbol = "axpy"
  67. };
  68. static struct starpu_codelet axpy_cl =
  69. {
  70. .cpu_funcs = {axpy_cpu},
  71. .cpu_funcs_name = {"axpy_cpu"},
  72. #ifdef STARPU_USE_CUDA
  73. .cuda_funcs = {axpy_gpu},
  74. #elif defined(STARPU_SIMGRID)
  75. .cuda_funcs = {(void*)1},
  76. #endif
  77. .cuda_flags = {STARPU_CUDA_ASYNC},
  78. #ifdef STARPU_USE_OPENCL
  79. .opencl_funcs = {axpy_opencl},
  80. #elif defined(STARPU_SIMGRID)
  81. .opencl_funcs = {(void*)1},
  82. #endif
  83. .opencl_flags = {STARPU_OPENCL_ASYNC},
  84. .nbuffers = 2,
  85. .modes = {STARPU_R, STARPU_RW},
  86. .name = "axpy",
  87. .model = &axpy_model
  88. };
  89. static int
  90. check(void)
  91. {
  92. int i;
  93. for (i = 0; i < N; i++)
  94. {
  95. TYPE expected_value = _alpha * _vec_x[i] + 4.0;
  96. if (fabs(_vec_y[i] - expected_value) > expected_value * EPSILON)
  97. {
  98. FPRINTF(stderr,"at %d, %f*%f+%f=%f, expected %f\n", i, _alpha, _vec_x[i], 4.0, _vec_y[i], expected_value);
  99. return EXIT_FAILURE;
  100. }
  101. }
  102. return EXIT_SUCCESS;
  103. }
  104. #ifdef STARPU_USE_OPENCL
  105. struct starpu_opencl_program opencl_program;
  106. #endif
  107. int main(int argc, char **argv)
  108. {
  109. int ret, exit_value = 0;
  110. /* Initialize StarPU */
  111. ret = starpu_init(NULL);
  112. if (ret == -ENODEV)
  113. return 77;
  114. STARPU_CHECK_RETURN_VALUE(ret, "starpu_init");
  115. #ifdef STARPU_USE_OPENCL
  116. ret = starpu_opencl_load_opencl_from_file("examples/axpy/axpy_opencl_kernel.cl",
  117. &opencl_program, NULL);
  118. STARPU_CHECK_RETURN_VALUE(ret, "starpu_opencl_load_opencl_from_file");
  119. #endif
  120. starpu_cublas_init();
  121. /* This is equivalent to
  122. vec_a = malloc(N*sizeof(TYPE));
  123. vec_b = malloc(N*sizeof(TYPE));
  124. */
  125. starpu_malloc((void **)&_vec_x, N*sizeof(TYPE));
  126. assert(_vec_x);
  127. starpu_malloc((void **)&_vec_y, N*sizeof(TYPE));
  128. assert(_vec_y);
  129. unsigned i;
  130. for (i = 0; i < N; i++)
  131. {
  132. _vec_x[i] = 1.0f; /*(TYPE)starpu_drand48(); */
  133. _vec_y[i] = 4.0f; /*(TYPE)starpu_drand48(); */
  134. }
  135. FPRINTF(stderr, "BEFORE x[0] = %2.2f\n", _vec_x[0]);
  136. FPRINTF(stderr, "BEFORE y[0] = %2.2f\n", _vec_y[0]);
  137. /* Declare the data to StarPU */
  138. starpu_vector_data_register(&_handle_x, STARPU_MAIN_RAM, (uintptr_t)_vec_x, N, sizeof(TYPE));
  139. starpu_vector_data_register(&_handle_y, STARPU_MAIN_RAM, (uintptr_t)_vec_y, N, sizeof(TYPE));
  140. /* Divide the vector into blocks */
  141. struct starpu_data_filter block_filter =
  142. {
  143. .filter_func = starpu_vector_filter_block,
  144. .nchildren = NBLOCKS
  145. };
  146. starpu_data_partition(_handle_x, &block_filter);
  147. starpu_data_partition(_handle_y, &block_filter);
  148. double start;
  149. double end;
  150. start = starpu_timing_now();
  151. unsigned b;
  152. for (b = 0; b < NBLOCKS; b++)
  153. {
  154. struct starpu_task *task = starpu_task_create();
  155. task->cl = &axpy_cl;
  156. task->cl_arg = &_alpha;
  157. task->cl_arg_size = sizeof(_alpha);
  158. task->handles[0] = starpu_data_get_sub_data(_handle_x, 1, b);
  159. task->handles[1] = starpu_data_get_sub_data(_handle_y, 1, b);
  160. task->tag_id = b;
  161. ret = starpu_task_submit(task);
  162. if (ret == -ENODEV)
  163. {
  164. exit_value = 77;
  165. goto enodev;
  166. }
  167. STARPU_CHECK_RETURN_VALUE(ret, "starpu_task_submit");
  168. }
  169. starpu_task_wait_for_all();
  170. enodev:
  171. starpu_data_unpartition(_handle_x, STARPU_MAIN_RAM);
  172. starpu_data_unpartition(_handle_y, STARPU_MAIN_RAM);
  173. starpu_data_unregister(_handle_x);
  174. starpu_data_unregister(_handle_y);
  175. end = starpu_timing_now();
  176. double timing = end - start;
  177. FPRINTF(stderr, "timing -> %2.2f us %2.2f MB/s\n", timing, 3*N*sizeof(TYPE)/timing);
  178. FPRINTF(stderr, "AFTER y[0] = %2.2f (ALPHA = %2.2f)\n", _vec_y[0], _alpha);
  179. if (exit_value != 77)
  180. exit_value = check();
  181. starpu_free((void *)_vec_x);
  182. starpu_free((void *)_vec_y);
  183. #ifdef STARPU_USE_OPENCL
  184. ret = starpu_opencl_unload_opencl(&opencl_program);
  185. STARPU_CHECK_RETURN_VALUE(ret, "starpu_opencl_unload_opencl");
  186. #endif
  187. /* Stop StarPU */
  188. starpu_shutdown();
  189. return exit_value;
  190. }