increment_redux_v2.c 7.9 KB

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