dot_product.c 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224
  1. /*
  2. * StarPU
  3. * Copyright (C) Université Bordeaux 1, CNRS 2008-2010 (see AUTHORS file)
  4. *
  5. * This program 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. * This program 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. static float *x;
  23. static float *y;
  24. static starpu_data_handle *x_handles;
  25. static starpu_data_handle *y_handles;
  26. static unsigned nblocks = 4096;
  27. static unsigned entries_per_bock = 1024;
  28. #define DOT_TYPE double
  29. static DOT_TYPE dot = 0.0f;
  30. static starpu_data_handle dot_handle;
  31. /*
  32. * Codelet to create a neutral element
  33. */
  34. void init_cpu_func(void *descr[], void *cl_arg)
  35. {
  36. DOT_TYPE *dot = (DOT_TYPE *)STARPU_VARIABLE_GET_PTR(descr[0]);
  37. *dot = 0.0f;
  38. }
  39. #ifdef STARPU_USE_CUDA
  40. void init_cuda_func(void *descr[], void *cl_arg)
  41. {
  42. DOT_TYPE *dot = (DOT_TYPE *)STARPU_VARIABLE_GET_PTR(descr[0]);
  43. cudaMemset(dot, 0, sizeof(DOT_TYPE));
  44. cudaThreadSynchronize();
  45. }
  46. #endif
  47. static struct starpu_codelet_t init_codelet = {
  48. .where = STARPU_CPU|STARPU_CUDA,
  49. .cpu_func = init_cpu_func,
  50. #ifdef STARPU_USE_CUDA
  51. .cuda_func = init_cuda_func,
  52. #endif
  53. .nbuffers = 1
  54. };
  55. /*
  56. * Codelet to perform the reduction of two elements
  57. */
  58. void redux_cpu_func(void *descr[], void *cl_arg)
  59. {
  60. DOT_TYPE *dota = (DOT_TYPE *)STARPU_VARIABLE_GET_PTR(descr[0]);
  61. DOT_TYPE *dotb = (DOT_TYPE *)STARPU_VARIABLE_GET_PTR(descr[1]);
  62. *dota = *dota + *dotb;
  63. }
  64. static struct starpu_codelet_t redux_codelet = {
  65. .where = STARPU_CPU,
  66. .cpu_func = redux_cpu_func,
  67. .nbuffers = 2
  68. };
  69. /*
  70. * Dot product codelet
  71. */
  72. void dot_cpu_func(void *descr[], void *cl_arg)
  73. {
  74. float *local_x = (float *)STARPU_VECTOR_GET_PTR(descr[0]);
  75. float *local_y = (float *)STARPU_VECTOR_GET_PTR(descr[1]);
  76. DOT_TYPE *dot = (DOT_TYPE *)STARPU_VARIABLE_GET_PTR(descr[2]);
  77. unsigned n = STARPU_VECTOR_GET_NX(descr[0]);
  78. DOT_TYPE local_dot = 0.0;
  79. unsigned i;
  80. for (i = 0; i < n; i++)
  81. {
  82. local_dot += (DOT_TYPE)local_x[i]*(DOT_TYPE)local_y[i];
  83. }
  84. *dot = *dot + local_dot;
  85. }
  86. #ifdef STARPU_USE_CUDA
  87. void dot_cuda_func(void *descr[], void *cl_arg)
  88. {
  89. DOT_TYPE current_dot;
  90. DOT_TYPE local_dot;
  91. float *local_x = (float *)STARPU_VECTOR_GET_PTR(descr[0]);
  92. float *local_y = (float *)STARPU_VECTOR_GET_PTR(descr[1]);
  93. DOT_TYPE *dot = (DOT_TYPE *)STARPU_VARIABLE_GET_PTR(descr[2]);
  94. unsigned n = STARPU_VECTOR_GET_NX(descr[0]);
  95. cudaMemcpy(&current_dot, dot, sizeof(DOT_TYPE), cudaMemcpyDeviceToHost);
  96. int ret = cudaThreadSynchronize();
  97. local_dot = (DOT_TYPE)cublasSdot(n, local_x, 1, local_y, 1);
  98. //fprintf(stderr, "current_dot %f local dot %f -> %f\n", current_dot, local_dot, current_dot + local_dot);
  99. current_dot += local_dot;
  100. cudaThreadSynchronize();
  101. cudaMemcpy(dot, &current_dot, sizeof(DOT_TYPE), cudaMemcpyHostToDevice);
  102. cudaThreadSynchronize();
  103. }
  104. #endif
  105. static struct starpu_codelet_t dot_codelet = {
  106. .where = STARPU_CPU|STARPU_CUDA,
  107. .cpu_func = dot_cpu_func,
  108. #ifdef STARPU_USE_CUDA
  109. .cuda_func = dot_cuda_func,
  110. #endif
  111. .nbuffers = 3
  112. };
  113. /*
  114. * Tasks initialization
  115. */
  116. extern void starpu_data_end_reduction_mode(starpu_data_handle handle);
  117. int main(int argc, char **argv)
  118. {
  119. starpu_init(NULL);
  120. starpu_helper_cublas_init();
  121. unsigned long nelems = nblocks*entries_per_bock;
  122. size_t size = nelems*sizeof(float);
  123. x = malloc(size);
  124. y = malloc(size);
  125. x_handles = calloc(nblocks, sizeof(starpu_data_handle));
  126. y_handles = calloc(nblocks, sizeof(starpu_data_handle));
  127. assert(x && y);
  128. starpu_srand48(0);
  129. DOT_TYPE reference_dot = 0.0;
  130. unsigned long i;
  131. for (i = 0; i < nelems; i++)
  132. {
  133. x[i] = (float)starpu_drand48();
  134. y[i] = (float)starpu_drand48();
  135. reference_dot += (DOT_TYPE)x[i]*(DOT_TYPE)y[i];
  136. }
  137. unsigned block;
  138. for (block = 0; block < nblocks; block++)
  139. {
  140. starpu_vector_data_register(&x_handles[block], 0,
  141. (uintptr_t)&x[entries_per_bock*block], entries_per_bock, sizeof(float));
  142. starpu_vector_data_register(&y_handles[block], 0,
  143. (uintptr_t)&y[entries_per_bock*block], entries_per_bock, sizeof(float));
  144. }
  145. starpu_variable_data_register(&dot_handle, 0, (uintptr_t)&dot, sizeof(DOT_TYPE));
  146. /*
  147. * Compute dot product with StarPU
  148. */
  149. starpu_data_set_reduction_methods(dot_handle, &redux_codelet, &init_codelet);
  150. for (block = 0; block < nblocks; block++)
  151. {
  152. struct starpu_task *task = starpu_task_create();
  153. task->cl = &dot_codelet;
  154. task->buffers[0].handle = x_handles[block];
  155. task->buffers[0].mode = STARPU_R;
  156. task->buffers[1].handle = y_handles[block];
  157. task->buffers[1].mode = STARPU_R;
  158. task->buffers[2].handle = dot_handle;
  159. task->buffers[2].mode = STARPU_REDUX;
  160. int ret = starpu_task_submit(task);
  161. STARPU_ASSERT(!ret);
  162. }
  163. starpu_data_unregister(dot_handle);
  164. fprintf(stderr, "Reference : %e vs. %e (Delta %e)\n", reference_dot, dot, reference_dot - dot);
  165. starpu_helper_cublas_shutdown();
  166. starpu_shutdown();
  167. return 0;
  168. }