increment_redux_v2.c 8.1 KB

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