axpy.c 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227
  1. /* StarPU --- Runtime system for heterogeneous multicore architectures.
  2. *
  3. * Copyright (C) 2009-2014 Université de Bordeaux 1
  4. * Copyright (C) 2010 Mehdi Juhoor <mjuhoor@gmail.com>
  5. * Copyright (C) 2010, 2011, 2012, 2013 Centre National de la Recherche Scientifique
  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. #include <starpu.h>
  19. #include <stdlib.h>
  20. #include <stdio.h>
  21. #include <assert.h>
  22. #include <sys/time.h>
  23. #include <math.h>
  24. #include <common/blas.h>
  25. #ifdef STARPU_USE_CUDA
  26. #include <cublas.h>
  27. #endif
  28. #include "axpy.h"
  29. #define AXPY STARPU_SAXPY
  30. #define CUBLASAXPY cublasSaxpy
  31. #define N (16*1024*1024)
  32. #define NBLOCKS 8
  33. #define FPRINTF(ofile, fmt, ...) do { if (!getenv("STARPU_SSILENT")) {fprintf(ofile, fmt, ## __VA_ARGS__); }} while(0)
  34. #define EPSILON 1e-6
  35. TYPE *_vec_x, *_vec_y;
  36. TYPE _alpha = 3.41;
  37. /* descriptors for StarPU */
  38. starpu_data_handle_t _handle_y, _handle_x;
  39. void axpy_cpu(void *descr[], STARPU_ATTRIBUTE_UNUSED void *arg)
  40. {
  41. TYPE alpha = *((TYPE *)arg);
  42. unsigned n = STARPU_VECTOR_GET_NX(descr[0]);
  43. TYPE *block_x = (TYPE *)STARPU_VECTOR_GET_PTR(descr[0]);
  44. TYPE *block_y = (TYPE *)STARPU_VECTOR_GET_PTR(descr[1]);
  45. AXPY((int)n, alpha, block_x, 1, block_y, 1);
  46. }
  47. #ifdef STARPU_USE_CUDA
  48. void axpy_gpu(void *descr[], STARPU_ATTRIBUTE_UNUSED void *arg)
  49. {
  50. TYPE alpha = *((TYPE *)arg);
  51. unsigned n = STARPU_VECTOR_GET_NX(descr[0]);
  52. TYPE *block_x = (TYPE *)STARPU_VECTOR_GET_PTR(descr[0]);
  53. TYPE *block_y = (TYPE *)STARPU_VECTOR_GET_PTR(descr[1]);
  54. CUBLASAXPY((int)n, alpha, block_x, 1, block_y, 1);
  55. }
  56. #endif
  57. #ifdef STARPU_USE_OPENCL
  58. extern void axpy_opencl(void *buffers[], void *args);
  59. #endif
  60. static struct starpu_codelet axpy_cl =
  61. {
  62. .cpu_funcs = {axpy_cpu, NULL},
  63. #ifdef STARPU_USE_CUDA
  64. .cuda_funcs = {axpy_gpu, NULL},
  65. .cuda_flags = {STARPU_CUDA_ASYNC},
  66. #endif
  67. #ifdef STARPU_USE_OPENCL
  68. .opencl_funcs = {axpy_opencl, NULL},
  69. .opencl_flags = {STARPU_OPENCL_ASYNC},
  70. #endif
  71. .nbuffers = 2,
  72. .modes = {STARPU_R, STARPU_RW},
  73. .name = "axpy"
  74. };
  75. static int
  76. check(void)
  77. {
  78. int i;
  79. for (i = 0; i < N; i++)
  80. {
  81. TYPE expected_value = _alpha * _vec_x[i] + 4.0;
  82. if (fabs(_vec_y[i] - expected_value) > expected_value * EPSILON) {
  83. FPRINTF(stderr,"at %d, %f*%f+%f=%f, expected %f\n", i, _alpha, _vec_x[i], 4.0, _vec_y[i], expected_value);
  84. return EXIT_FAILURE;
  85. }
  86. }
  87. return EXIT_SUCCESS;
  88. }
  89. #ifdef STARPU_USE_OPENCL
  90. struct starpu_opencl_program opencl_program;
  91. #endif
  92. int main(int argc, char **argv)
  93. {
  94. int ret, exit_value = 0;
  95. /* Initialize StarPU */
  96. ret = starpu_init(NULL);
  97. if (ret == -ENODEV)
  98. return 77;
  99. STARPU_CHECK_RETURN_VALUE(ret, "starpu_init");
  100. #ifdef STARPU_USE_OPENCL
  101. ret = starpu_opencl_load_opencl_from_file("examples/axpy/axpy_opencl_kernel.cl",
  102. &opencl_program, NULL);
  103. STARPU_CHECK_RETURN_VALUE(ret, "starpu_opencl_load_opencl_from_file");
  104. #endif
  105. starpu_cublas_init();
  106. /* This is equivalent to
  107. vec_a = malloc(N*sizeof(TYPE));
  108. vec_b = malloc(N*sizeof(TYPE));
  109. */
  110. starpu_malloc((void **)&_vec_x, N*sizeof(TYPE));
  111. assert(_vec_x);
  112. starpu_malloc((void **)&_vec_y, N*sizeof(TYPE));
  113. assert(_vec_y);
  114. unsigned i;
  115. for (i = 0; i < N; i++)
  116. {
  117. _vec_x[i] = 1.0f; /*(TYPE)starpu_drand48(); */
  118. _vec_y[i] = 4.0f; /*(TYPE)starpu_drand48(); */
  119. }
  120. FPRINTF(stderr, "BEFORE x[0] = %2.2f\n", _vec_x[0]);
  121. FPRINTF(stderr, "BEFORE y[0] = %2.2f\n", _vec_y[0]);
  122. /* Declare the data to StarPU */
  123. starpu_vector_data_register(&_handle_x, STARPU_MAIN_RAM, (uintptr_t)_vec_x, N, sizeof(TYPE));
  124. starpu_vector_data_register(&_handle_y, STARPU_MAIN_RAM, (uintptr_t)_vec_y, N, sizeof(TYPE));
  125. /* Divide the vector into blocks */
  126. struct starpu_data_filter block_filter =
  127. {
  128. .filter_func = starpu_vector_filter_block,
  129. .nchildren = NBLOCKS
  130. };
  131. starpu_data_partition(_handle_x, &block_filter);
  132. starpu_data_partition(_handle_y, &block_filter);
  133. double start;
  134. double end;
  135. start = starpu_timing_now();
  136. unsigned b;
  137. for (b = 0; b < NBLOCKS; b++)
  138. {
  139. struct starpu_task *task = starpu_task_create();
  140. task->cl = &axpy_cl;
  141. task->cl_arg = &_alpha;
  142. task->handles[0] = starpu_data_get_sub_data(_handle_x, 1, b);
  143. task->handles[1] = starpu_data_get_sub_data(_handle_y, 1, b);
  144. task->tag_id = b;
  145. ret = starpu_task_submit(task);
  146. if (ret == -ENODEV)
  147. {
  148. exit_value = 77;
  149. goto enodev;
  150. }
  151. STARPU_CHECK_RETURN_VALUE(ret, "starpu_task_submit");
  152. }
  153. starpu_task_wait_for_all();
  154. enodev:
  155. starpu_data_unpartition(_handle_x, STARPU_MAIN_RAM);
  156. starpu_data_unpartition(_handle_y, STARPU_MAIN_RAM);
  157. starpu_data_unregister(_handle_x);
  158. starpu_data_unregister(_handle_y);
  159. end = starpu_timing_now();
  160. double timing = end - start;
  161. FPRINTF(stderr, "timing -> %2.2f us %2.2f MB/s\n", timing, 3*N*sizeof(TYPE)/timing);
  162. FPRINTF(stderr, "AFTER y[0] = %2.2f (ALPHA = %2.2f)\n", _vec_y[0], _alpha);
  163. if (exit_value != 77)
  164. exit_value = check();
  165. starpu_free((void *)_vec_x);
  166. starpu_free((void *)_vec_y);
  167. #ifdef STARPU_USE_OPENCL
  168. ret = starpu_opencl_unload_opencl(&opencl_program);
  169. STARPU_CHECK_RETURN_VALUE(ret, "starpu_opencl_unload_opencl");
  170. #endif
  171. /* Stop StarPU */
  172. starpu_shutdown();
  173. return exit_value;
  174. }