increment_redux_v2.c 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269
  1. /* StarPU --- Runtime system for heterogeneous multicore architectures.
  2. *
  3. * Copyright (C) 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 "../helper.h"
  18. #ifdef STARPU_USE_CUDA
  19. #include <starpu_cuda.h>
  20. #endif
  21. #ifdef STARPU_USE_OPENCL
  22. #include <starpu_opencl.h>
  23. #endif
  24. static unsigned var = 0;
  25. static starpu_data_handle_t handle;
  26. /*
  27. * Reduction methods
  28. */
  29. #ifdef STARPU_USE_CUDA
  30. static void redux_cuda_kernel(void *descr[], void *arg)
  31. {
  32. unsigned *dst = (unsigned *)STARPU_VARIABLE_GET_PTR(descr[0]);
  33. unsigned *src = (unsigned *)STARPU_VARIABLE_GET_PTR(descr[1]);
  34. unsigned host_dst, host_src;
  35. /* This is a dummy technique of course */
  36. cudaMemcpy(&host_src, src, sizeof(unsigned), cudaMemcpyDeviceToHost);
  37. cudaMemcpy(&host_dst, dst, sizeof(unsigned), cudaMemcpyDeviceToHost);
  38. cudaThreadSynchronize();
  39. host_dst += host_src;
  40. cudaMemcpy(dst, &host_dst, sizeof(unsigned), cudaMemcpyHostToDevice);
  41. cudaThreadSynchronize();
  42. }
  43. static void neutral_cuda_kernel(void *descr[], void *arg)
  44. {
  45. unsigned *dst = (unsigned *)STARPU_VARIABLE_GET_PTR(descr[0]);
  46. /* This is a dummy technique of course */
  47. unsigned host_dst = 0;
  48. cudaMemcpy(dst, &host_dst, sizeof(unsigned), cudaMemcpyHostToDevice);
  49. cudaThreadSynchronize();
  50. }
  51. #endif
  52. #ifdef STARPU_USE_OPENCL
  53. static void redux_opencl_kernel(void *descr[], void *arg)
  54. {
  55. unsigned h_dst, h_src;
  56. cl_mem d_dst = (cl_mem)STARPU_VARIABLE_GET_PTR(descr[0]);
  57. cl_mem d_src = (cl_mem)STARPU_VARIABLE_GET_PTR(descr[1]);
  58. cl_command_queue queue;
  59. starpu_opencl_get_current_queue(&queue);
  60. /* This is a dummy technique of course */
  61. clEnqueueReadBuffer(queue, d_dst, CL_TRUE, 0, sizeof(unsigned), (void *)&h_dst, 0, NULL, NULL);
  62. clEnqueueReadBuffer(queue, d_src, CL_TRUE, 0, sizeof(unsigned), (void *)&h_src, 0, NULL, NULL);
  63. h_dst += h_src;
  64. clEnqueueWriteBuffer(queue, d_dst, CL_TRUE, 0, sizeof(unsigned), (void *)&h_dst, 0, NULL, NULL);
  65. }
  66. static void neutral_opencl_kernel(void *descr[], void *arg)
  67. {
  68. unsigned h_dst = 0;
  69. cl_mem d_dst = (cl_mem)STARPU_VARIABLE_GET_PTR(descr[0]);
  70. cl_command_queue queue;
  71. starpu_opencl_get_current_queue(&queue);
  72. clEnqueueWriteBuffer(queue, d_dst, CL_TRUE, 0, sizeof(unsigned), (void *)&h_dst, 0, NULL, NULL);
  73. }
  74. #endif
  75. static void redux_cpu_kernel(void *descr[], void *arg)
  76. {
  77. unsigned *dst = (unsigned *)STARPU_VARIABLE_GET_PTR(descr[0]);
  78. unsigned *src = (unsigned *)STARPU_VARIABLE_GET_PTR(descr[1]);
  79. *dst = *dst + *src;
  80. }
  81. static void neutral_cpu_kernel(void *descr[], void *arg)
  82. {
  83. unsigned *dst = (unsigned *)STARPU_VARIABLE_GET_PTR(descr[0]);
  84. *dst = 0;
  85. }
  86. static struct starpu_codelet redux_cl =
  87. {
  88. .where = STARPU_CPU|STARPU_CUDA|STARPU_OPENCL,
  89. #ifdef STARPU_USE_CUDA
  90. .cuda_funcs = {redux_cuda_kernel, NULL},
  91. #endif
  92. #ifdef STARPU_USE_OPENCL
  93. .opencl_funcs = {redux_opencl_kernel, NULL},
  94. #endif
  95. .cpu_funcs = {redux_cpu_kernel, NULL},
  96. .nbuffers = 2
  97. };
  98. static struct starpu_codelet neutral_cl =
  99. {
  100. .where = STARPU_CPU|STARPU_CUDA,
  101. #ifdef STARPU_USE_CUDA
  102. .cuda_funcs = {neutral_cuda_kernel, NULL},
  103. #endif
  104. #ifdef STARPU_USE_OPENCL
  105. .opencl_funcs = {neutral_opencl_kernel, NULL},
  106. #endif
  107. .cpu_funcs = {neutral_cpu_kernel, NULL},
  108. .nbuffers = 1
  109. };
  110. /*
  111. * Increment codelet
  112. */
  113. #ifdef STARPU_USE_OPENCL
  114. /* dummy OpenCL implementation */
  115. static void increment_opencl_kernel(void *descr[], void *cl_arg __attribute__((unused)))
  116. {
  117. cl_mem d_token = (cl_mem)STARPU_VARIABLE_GET_PTR(descr[0]);
  118. unsigned h_token;
  119. cl_command_queue queue;
  120. starpu_opencl_get_current_queue(&queue);
  121. clEnqueueReadBuffer(queue, d_token, CL_TRUE, 0, sizeof(unsigned), (void *)&h_token, 0, NULL, NULL);
  122. h_token++;
  123. clEnqueueWriteBuffer(queue, d_token, CL_TRUE, 0, sizeof(unsigned), (void *)&h_token, 0, NULL, NULL);
  124. }
  125. #endif
  126. #ifdef STARPU_USE_CUDA
  127. static void increment_cuda_kernel(void *descr[], void *arg)
  128. {
  129. unsigned *tokenptr = (unsigned *)STARPU_VARIABLE_GET_PTR(descr[0]);
  130. unsigned host_token;
  131. /* This is a dummy technique of course */
  132. cudaMemcpy(&host_token, tokenptr, sizeof(unsigned), cudaMemcpyDeviceToHost);
  133. cudaThreadSynchronize();
  134. host_token++;
  135. cudaMemcpy(tokenptr, &host_token, sizeof(unsigned), cudaMemcpyHostToDevice);
  136. cudaThreadSynchronize();
  137. }
  138. #endif
  139. static void increment_cpu_kernel(void *descr[], void *arg)
  140. {
  141. unsigned *tokenptr = (unsigned *)STARPU_VARIABLE_GET_PTR(descr[0]);
  142. *tokenptr = *tokenptr + 1;
  143. }
  144. static struct starpu_codelet increment_cl =
  145. {
  146. .where = STARPU_CPU|STARPU_CUDA|STARPU_OPENCL,
  147. #ifdef STARPU_USE_CUDA
  148. .cuda_funcs = {increment_cuda_kernel, NULL},
  149. #endif
  150. #ifdef STARPU_USE_OPENCL
  151. .opencl_funcs = {increment_opencl_kernel, NULL},
  152. #endif
  153. .cpu_funcs = {increment_cpu_kernel, NULL},
  154. .nbuffers = 1
  155. };
  156. static struct starpu_codelet increment_cl_redux =
  157. {
  158. .where = STARPU_CPU|STARPU_CUDA|STARPU_OPENCL,
  159. #ifdef STARPU_USE_CUDA
  160. .cuda_funcs = {increment_cuda_kernel, NULL},
  161. #endif
  162. #ifdef STARPU_USE_OPENCL
  163. .opencl_funcs = {increment_opencl_kernel, NULL},
  164. #endif
  165. .cpu_funcs = {increment_cpu_kernel, NULL},
  166. .nbuffers = 1
  167. };
  168. int main(int argc, char **argv)
  169. {
  170. int ret;
  171. ret = starpu_init(NULL);
  172. STARPU_CHECK_RETURN_VALUE(ret, "starpu_init");
  173. starpu_variable_data_register(&handle, 0, (uintptr_t)&var, sizeof(unsigned));
  174. starpu_data_set_reduction_methods(handle, &redux_cl, &neutral_cl);
  175. unsigned ntasks = 1024;
  176. unsigned nloops = 16;
  177. unsigned loop;
  178. unsigned t;
  179. for (loop = 0; loop < nloops; loop++)
  180. {
  181. for (t = 0; t < ntasks; t++)
  182. {
  183. struct starpu_task *task = starpu_task_create();
  184. if (t % 10 == 0)
  185. {
  186. task->cl = &increment_cl;
  187. task->buffers[0].mode = STARPU_RW;
  188. }
  189. else
  190. {
  191. task->cl = &increment_cl_redux;
  192. task->buffers[0].mode = STARPU_REDUX;
  193. }
  194. task->buffers[0].handle = handle;
  195. int ret = starpu_task_submit(task);
  196. if (ret == -ENODEV) goto enodev;
  197. STARPU_CHECK_RETURN_VALUE(ret, "starpu_task_submit");
  198. }
  199. ret = starpu_data_acquire(handle, STARPU_R);
  200. STARPU_CHECK_RETURN_VALUE(ret, "starpu_data_acquire");
  201. STARPU_ASSERT(var == ntasks*(loop + 1));
  202. starpu_data_release(handle);
  203. }
  204. starpu_data_unregister(handle);
  205. STARPU_ASSERT(var == ntasks*nloops);
  206. starpu_shutdown();
  207. return EXIT_SUCCESS;
  208. enodev:
  209. starpu_data_unregister(handle);
  210. fprintf(stderr, "WARNING: No one can execute this task\n");
  211. /* yes, we do not perform the computation but we did detect that no one
  212. * could perform the kernel, so this is not an error from StarPU */
  213. starpu_shutdown();
  214. return STARPU_TEST_SKIPPED;
  215. }