axpy.c 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227
  1. /* StarPU --- Runtime system for heterogeneous multicore architectures.
  2. *
  3. * Copyright (C) 2009-2012 Université de Bordeaux 1
  4. * Copyright (C) 2010 Mehdi Juhoor <mjuhoor@gmail.com>
  5. * Copyright (C) 2010, 2011, 2012 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. #ifdef STARPU_USE_OPENCL
  29. #include <starpu_opencl.h>
  30. #endif
  31. #include "axpy.h"
  32. #define AXPY SAXPY
  33. #define CUBLASAXPY cublasSaxpy
  34. #define N (16*1024*1024)
  35. #define NBLOCKS 8
  36. #define FPRINTF(ofile, fmt, args ...) do { if (!getenv("STARPU_SSILENT")) {fprintf(ofile, fmt, ##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[], __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[], __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. cudaThreadSynchronize();
  59. }
  60. #endif
  61. #ifdef STARPU_USE_OPENCL
  62. extern void axpy_opencl(void *buffers[], void *args);
  63. #endif
  64. static struct starpu_codelet axpy_cl =
  65. {
  66. .cpu_funcs = {axpy_cpu, NULL},
  67. #ifdef STARPU_USE_CUDA
  68. .cuda_funcs = {axpy_gpu, NULL},
  69. #endif
  70. #ifdef STARPU_USE_OPENCL
  71. .opencl_funcs = {axpy_opencl, NULL},
  72. #endif
  73. .nbuffers = 2,
  74. .modes = {STARPU_R, STARPU_RW}
  75. };
  76. static int
  77. check(void)
  78. {
  79. int i;
  80. for (i = 0; i < N; i++)
  81. {
  82. TYPE expected_value = alpha * vec_x[i] + 4.0;
  83. if (fabs(vec_y[i] - expected_value) > expected_value * EPSILON) {
  84. FPRINTF(stderr,"at %d, %f*%f+%f=%f, expected %f\n", i, alpha, vec_x[i], 4.0, vec_y[i], expected_value);
  85. return EXIT_FAILURE;
  86. }
  87. }
  88. return EXIT_SUCCESS;
  89. }
  90. #ifdef STARPU_USE_OPENCL
  91. struct starpu_opencl_program opencl_program;
  92. #endif
  93. int main(int argc, char **argv)
  94. {
  95. int ret, exit_value;
  96. /* Initialize StarPU */
  97. ret = starpu_init(NULL);
  98. if (ret == -ENODEV)
  99. return 77;
  100. STARPU_CHECK_RETURN_VALUE(ret, "starpu_init");
  101. #ifdef STARPU_USE_OPENCL
  102. ret = starpu_opencl_load_opencl_from_file("examples/axpy/axpy_opencl_kernel.cl",
  103. &opencl_program, NULL);
  104. STARPU_CHECK_RETURN_VALUE(ret, "starpu_opencl_load_opencl_from_file");
  105. #endif
  106. starpu_helper_cublas_init();
  107. /* This is equivalent to
  108. vec_a = malloc(N*sizeof(TYPE));
  109. vec_b = malloc(N*sizeof(TYPE));
  110. */
  111. starpu_malloc((void **)&vec_x, N*sizeof(TYPE));
  112. assert(vec_x);
  113. starpu_malloc((void **)&vec_y, N*sizeof(TYPE));
  114. assert(vec_y);
  115. unsigned i;
  116. for (i = 0; i < N; i++)
  117. {
  118. vec_x[i] = 1.0f; /*(TYPE)starpu_drand48(); */
  119. vec_y[i] = 4.0f; /*(TYPE)starpu_drand48(); */
  120. }
  121. FPRINTF(stderr, "BEFORE x[0] = %2.2f\n", vec_x[0]);
  122. FPRINTF(stderr, "BEFORE y[0] = %2.2f\n", vec_y[0]);
  123. /* Declare the data to StarPU */
  124. starpu_vector_data_register(&handle_x, 0, (uintptr_t)vec_x, N, sizeof(TYPE));
  125. starpu_vector_data_register(&handle_y, 0, (uintptr_t)vec_y, N, sizeof(TYPE));
  126. /* Divide the vector into blocks */
  127. struct starpu_data_filter block_filter =
  128. {
  129. .filter_func = starpu_block_filter_func_vector,
  130. .nchildren = NBLOCKS
  131. };
  132. starpu_data_partition(handle_x, &block_filter);
  133. starpu_data_partition(handle_y, &block_filter);
  134. struct timeval start;
  135. struct timeval end;
  136. gettimeofday(&start, NULL);
  137. unsigned b;
  138. for (b = 0; b < NBLOCKS; b++)
  139. {
  140. struct starpu_task *task = starpu_task_create();
  141. task->cl = &axpy_cl;
  142. task->cl_arg = &alpha;
  143. task->handles[0] = starpu_data_get_sub_data(handle_x, 1, b);
  144. task->handles[1] = starpu_data_get_sub_data(handle_y, 1, 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, 0);
  156. starpu_data_unpartition(handle_y, 0);
  157. starpu_data_unregister(handle_x);
  158. starpu_data_unregister(handle_y);
  159. gettimeofday(&end, NULL);
  160. double timing = (double)((end.tv_sec - start.tv_sec)*1000000 +
  161. (end.tv_usec - start.tv_usec));
  162. FPRINTF(stderr, "timing -> %2.2f us %2.2f MB/s\n", timing, 3*N*sizeof(TYPE)/timing);
  163. FPRINTF(stderr, "AFTER y[0] = %2.2f (ALPHA = %2.2f)\n", vec_y[0], alpha);
  164. if (exit_value != 77)
  165. exit_value = check();
  166. starpu_free((void *)vec_x);
  167. starpu_free((void *)vec_y);
  168. #ifdef STARPU_USE_OPENCL
  169. ret = starpu_opencl_unload_opencl(&opencl_program);
  170. STARPU_CHECK_RETURN_VALUE(ret, "starpu_opencl_unload_opencl");
  171. #endif
  172. /* Stop StarPU */
  173. starpu_shutdown();
  174. return exit_value;
  175. }