dot_product.c 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249
  1. /* StarPU --- Runtime system for heterogeneous multicore architectures.
  2. *
  3. * Copyright (C) 2010-2011 Université de Bordeaux 1
  4. *
  5. * StarPU is free software; you can redistribute it and/or modify
  6. * it under the terms of the GNU Lesser General Public License as published by
  7. * the Free Software Foundation; either version 2.1 of the License, or (at
  8. * your option) any later version.
  9. *
  10. * StarPU is distributed in the hope that it will be useful, but
  11. * WITHOUT ANY WARRANTY; without even the implied warranty of
  12. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
  13. *
  14. * See the GNU Lesser General Public License in COPYING.LGPL for more details.
  15. */
  16. #include <starpu.h>
  17. #include <assert.h>
  18. #ifdef STARPU_USE_CUDA
  19. #include <cuda.h>
  20. #include <cublas.h>
  21. #endif
  22. #define FPRINTF(ofile, fmt, args ...) do { if (!getenv("STARPU_SSILENT")) {fprintf(ofile, fmt, ##args); }} while(0)
  23. static float *x;
  24. static float *y;
  25. static starpu_data_handle *x_handles;
  26. static starpu_data_handle *y_handles;
  27. static unsigned nblocks = 4096;
  28. static unsigned entries_per_block = 1024;
  29. #define DOT_TYPE double
  30. static DOT_TYPE dot = 0.0f;
  31. static starpu_data_handle dot_handle;
  32. /*
  33. * Codelet to create a neutral element
  34. */
  35. void init_cpu_func(void *descr[], void *cl_arg)
  36. {
  37. DOT_TYPE *dot = (DOT_TYPE *)STARPU_VARIABLE_GET_PTR(descr[0]);
  38. *dot = 0.0f;
  39. }
  40. #ifdef STARPU_USE_CUDA
  41. void init_cuda_func(void *descr[], void *cl_arg)
  42. {
  43. DOT_TYPE *dot = (DOT_TYPE *)STARPU_VARIABLE_GET_PTR(descr[0]);
  44. cudaMemset(dot, 0, sizeof(DOT_TYPE));
  45. cudaThreadSynchronize();
  46. }
  47. #endif
  48. static struct starpu_codelet_t init_codelet = {
  49. .where = STARPU_CPU|STARPU_CUDA,
  50. .cpu_func = init_cpu_func,
  51. #ifdef STARPU_USE_CUDA
  52. .cuda_func = init_cuda_func,
  53. #endif
  54. .nbuffers = 1
  55. };
  56. /*
  57. * Codelet to perform the reduction of two elements
  58. */
  59. void redux_cpu_func(void *descr[], void *cl_arg)
  60. {
  61. DOT_TYPE *dota = (DOT_TYPE *)STARPU_VARIABLE_GET_PTR(descr[0]);
  62. DOT_TYPE *dotb = (DOT_TYPE *)STARPU_VARIABLE_GET_PTR(descr[1]);
  63. *dota = *dota + *dotb;
  64. }
  65. #ifdef STARPU_USE_CUDA
  66. extern void redux_cuda_func(void *descr[], void *_args);
  67. #endif
  68. static struct starpu_codelet_t redux_codelet = {
  69. .where = STARPU_CPU|STARPU_CUDA,
  70. .cpu_func = redux_cpu_func,
  71. #ifdef STARPU_USE_CUDA
  72. .cuda_func = redux_cuda_func,
  73. #endif
  74. .nbuffers = 2
  75. };
  76. /*
  77. * Dot product codelet
  78. */
  79. void dot_cpu_func(void *descr[], void *cl_arg)
  80. {
  81. float *local_x = (float *)STARPU_VECTOR_GET_PTR(descr[0]);
  82. float *local_y = (float *)STARPU_VECTOR_GET_PTR(descr[1]);
  83. DOT_TYPE *dot = (DOT_TYPE *)STARPU_VARIABLE_GET_PTR(descr[2]);
  84. unsigned n = STARPU_VECTOR_GET_NX(descr[0]);
  85. DOT_TYPE local_dot = 0.0;
  86. unsigned i;
  87. for (i = 0; i < n; i++)
  88. {
  89. local_dot += (DOT_TYPE)local_x[i]*(DOT_TYPE)local_y[i];
  90. }
  91. *dot = *dot + local_dot;
  92. }
  93. #ifdef STARPU_USE_CUDA
  94. void dot_cuda_func(void *descr[], void *cl_arg)
  95. {
  96. DOT_TYPE current_dot;
  97. DOT_TYPE local_dot;
  98. float *local_x = (float *)STARPU_VECTOR_GET_PTR(descr[0]);
  99. float *local_y = (float *)STARPU_VECTOR_GET_PTR(descr[1]);
  100. DOT_TYPE *dot = (DOT_TYPE *)STARPU_VARIABLE_GET_PTR(descr[2]);
  101. unsigned n = STARPU_VECTOR_GET_NX(descr[0]);
  102. cudaMemcpy(&current_dot, dot, sizeof(DOT_TYPE), cudaMemcpyDeviceToHost);
  103. cudaThreadSynchronize();
  104. local_dot = (DOT_TYPE)cublasSdot(n, local_x, 1, local_y, 1);
  105. /* FPRINTF(stderr, "current_dot %f local dot %f -> %f\n", current_dot, local_dot, current_dot + local_dot); */
  106. current_dot += local_dot;
  107. cudaThreadSynchronize();
  108. cudaMemcpy(dot, &current_dot, sizeof(DOT_TYPE), cudaMemcpyHostToDevice);
  109. cudaThreadSynchronize();
  110. }
  111. #endif
  112. static struct starpu_codelet_t dot_codelet = {
  113. .where = STARPU_CPU|STARPU_CUDA,
  114. .cpu_func = dot_cpu_func,
  115. #ifdef STARPU_USE_CUDA
  116. .cuda_func = dot_cuda_func,
  117. #endif
  118. .nbuffers = 3
  119. };
  120. /*
  121. * Tasks initialization
  122. */
  123. int main(int argc, char **argv)
  124. {
  125. starpu_init(NULL);
  126. starpu_helper_cublas_init();
  127. unsigned long nelems = nblocks*entries_per_block;
  128. size_t size = nelems*sizeof(float);
  129. x = malloc(size);
  130. y = malloc(size);
  131. x_handles = calloc(nblocks, sizeof(starpu_data_handle));
  132. y_handles = calloc(nblocks, sizeof(starpu_data_handle));
  133. assert(x && y);
  134. starpu_srand48(0);
  135. DOT_TYPE reference_dot = 0.0;
  136. unsigned long i;
  137. for (i = 0; i < nelems; i++)
  138. {
  139. x[i] = (float)starpu_drand48();
  140. y[i] = (float)starpu_drand48();
  141. reference_dot += (DOT_TYPE)x[i]*(DOT_TYPE)y[i];
  142. }
  143. unsigned block;
  144. for (block = 0; block < nblocks; block++)
  145. {
  146. starpu_vector_data_register(&x_handles[block], 0,
  147. (uintptr_t)&x[entries_per_block*block], entries_per_block, sizeof(float));
  148. starpu_vector_data_register(&y_handles[block], 0,
  149. (uintptr_t)&y[entries_per_block*block], entries_per_block, sizeof(float));
  150. }
  151. starpu_variable_data_register(&dot_handle, 0, (uintptr_t)&dot, sizeof(DOT_TYPE));
  152. /*
  153. * Compute dot product with StarPU
  154. */
  155. starpu_data_set_reduction_methods(dot_handle, &redux_codelet, &init_codelet);
  156. for (block = 0; block < nblocks; block++)
  157. {
  158. struct starpu_task *task = starpu_task_create();
  159. task->cl = &dot_codelet;
  160. task->destroy = 1;
  161. task->buffers[0].handle = x_handles[block];
  162. task->buffers[0].mode = STARPU_R;
  163. task->buffers[1].handle = y_handles[block];
  164. task->buffers[1].mode = STARPU_R;
  165. task->buffers[2].handle = dot_handle;
  166. task->buffers[2].mode = STARPU_REDUX;
  167. int ret = starpu_task_submit(task);
  168. if (ret == -ENODEV) goto enodev;
  169. STARPU_ASSERT(!ret);
  170. }
  171. for (block = 0; block < nblocks; block++)
  172. {
  173. starpu_data_unregister(x_handles[block]);
  174. starpu_data_unregister(y_handles[block]);
  175. }
  176. starpu_data_unregister(dot_handle);
  177. FPRINTF(stderr, "Reference : %e vs. %e (Delta %e)\n", reference_dot, dot, reference_dot - dot);
  178. starpu_helper_cublas_shutdown();
  179. starpu_shutdown();
  180. free(x);
  181. free(y);
  182. free(x_handles);
  183. free(y_handles);
  184. return 0;
  185. enodev:
  186. fprintf(stderr, "WARNING: No one can execute this task\n");
  187. /* yes, we do not perform the computation but we did detect that no one
  188. * could perform the kernel, so this is not an error from StarPU */
  189. return 77;
  190. }