increment_redux_v2.c 8.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316
  1. /* StarPU --- Runtime system for heterogeneous multicore architectures.
  2. *
  3. * Copyright (C) 2011-2016 Université de Bordeaux
  4. * Copyright (C) 2017 Inria
  5. *
  6. * StarPU is free software; you can redistribute it and/or modify
  7. * it under the terms of the GNU Lesser General Public License as published by
  8. * the Free Software Foundation; either version 2.1 of the License, or (at
  9. * your option) any later version.
  10. *
  11. * StarPU is distributed in the hope that it will be useful, but
  12. * WITHOUT ANY WARRANTY; without even the implied warranty of
  13. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
  14. *
  15. * See the GNU Lesser General Public License in COPYING.LGPL for more details.
  16. */
  17. #include <config.h>
  18. #include <starpu.h>
  19. #include "../helper.h"
  20. /*
  21. * Check that STARPU_REDUX works with a mere incrementation, but
  22. * intermixing with non-REDUX accesses
  23. */
  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. STARPU_SKIP_IF_VALGRIND;
  33. unsigned *dst = (unsigned *)STARPU_VARIABLE_GET_PTR(descr[0]);
  34. unsigned *src = (unsigned *)STARPU_VARIABLE_GET_PTR(descr[1]);
  35. unsigned host_dst, host_src;
  36. /* This is a dummy technique of course */
  37. cudaMemcpyAsync(&host_src, src, sizeof(unsigned), cudaMemcpyDeviceToHost, starpu_cuda_get_local_stream());
  38. cudaMemcpyAsync(&host_dst, dst, sizeof(unsigned), cudaMemcpyDeviceToHost, starpu_cuda_get_local_stream());
  39. cudaStreamSynchronize(starpu_cuda_get_local_stream());
  40. host_dst += host_src;
  41. cudaMemcpyAsync(dst, &host_dst, sizeof(unsigned), cudaMemcpyHostToDevice, starpu_cuda_get_local_stream());
  42. }
  43. static void neutral_cuda_kernel(void *descr[], void *arg)
  44. {
  45. STARPU_SKIP_IF_VALGRIND;
  46. unsigned *dst = (unsigned *)STARPU_VARIABLE_GET_PTR(descr[0]);
  47. /* This is a dummy technique of course */
  48. unsigned host_dst = 0;
  49. cudaMemcpyAsync(dst, &host_dst, sizeof(unsigned), cudaMemcpyHostToDevice, starpu_cuda_get_local_stream());
  50. }
  51. #endif
  52. #ifdef STARPU_USE_OPENCL
  53. static void redux_opencl_kernel(void *descr[], void *arg)
  54. {
  55. STARPU_SKIP_IF_VALGRIND;
  56. unsigned h_dst, h_src;
  57. cl_mem d_dst = (cl_mem)STARPU_VARIABLE_GET_PTR(descr[0]);
  58. cl_mem d_src = (cl_mem)STARPU_VARIABLE_GET_PTR(descr[1]);
  59. cl_command_queue queue;
  60. starpu_opencl_get_current_queue(&queue);
  61. /* This is a dummy technique of course */
  62. clEnqueueReadBuffer(queue, d_dst, CL_TRUE, 0, sizeof(unsigned), (void *)&h_dst, 0, NULL, NULL);
  63. clEnqueueReadBuffer(queue, d_src, CL_TRUE, 0, sizeof(unsigned), (void *)&h_src, 0, NULL, NULL);
  64. h_dst += h_src;
  65. clEnqueueWriteBuffer(queue, d_dst, CL_TRUE, 0, sizeof(unsigned), (void *)&h_dst, 0, NULL, NULL);
  66. }
  67. static void neutral_opencl_kernel(void *descr[], void *arg)
  68. {
  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. STARPU_SKIP_IF_VALGRIND;
  80. unsigned *dst = (unsigned *)STARPU_VARIABLE_GET_PTR(descr[0]);
  81. unsigned *src = (unsigned *)STARPU_VARIABLE_GET_PTR(descr[1]);
  82. *dst = *dst + *src;
  83. }
  84. void neutral_cpu_kernel(void *descr[], void *arg)
  85. {
  86. STARPU_SKIP_IF_VALGRIND;
  87. unsigned *dst = (unsigned *)STARPU_VARIABLE_GET_PTR(descr[0]);
  88. *dst = 0;
  89. }
  90. static struct starpu_codelet redux_cl =
  91. {
  92. #ifdef STARPU_USE_CUDA
  93. .cuda_funcs = {redux_cuda_kernel},
  94. .cuda_flags = {STARPU_CUDA_ASYNC},
  95. #endif
  96. #ifdef STARPU_USE_OPENCL
  97. .opencl_funcs = {redux_opencl_kernel},
  98. .opencl_flags = {STARPU_OPENCL_ASYNC},
  99. #endif
  100. .cpu_funcs = {redux_cpu_kernel},
  101. .cpu_funcs_name = {"redux_cpu_kernel"},
  102. .modes = {STARPU_RW, STARPU_R},
  103. .nbuffers = 2
  104. };
  105. static struct starpu_codelet neutral_cl =
  106. {
  107. #ifdef STARPU_USE_CUDA
  108. .cuda_funcs = {neutral_cuda_kernel},
  109. .cuda_flags = {STARPU_CUDA_ASYNC},
  110. #endif
  111. #ifdef STARPU_USE_OPENCL
  112. .opencl_funcs = {neutral_opencl_kernel},
  113. .opencl_flags = {STARPU_OPENCL_ASYNC},
  114. #endif
  115. .cpu_funcs = {neutral_cpu_kernel},
  116. .cpu_funcs_name = {"neutral_cpu_kernel"},
  117. .modes = {STARPU_W},
  118. .nbuffers = 1
  119. };
  120. /*
  121. * Increment codelet
  122. */
  123. #ifdef STARPU_USE_OPENCL
  124. /* dummy OpenCL implementation */
  125. static void increment_opencl_kernel(void *descr[], void *cl_arg STARPU_ATTRIBUTE_UNUSED)
  126. {
  127. STARPU_SKIP_IF_VALGRIND;
  128. cl_mem d_token = (cl_mem)STARPU_VARIABLE_GET_PTR(descr[0]);
  129. unsigned h_token;
  130. cl_command_queue queue;
  131. starpu_opencl_get_current_queue(&queue);
  132. clEnqueueReadBuffer(queue, d_token, CL_TRUE, 0, sizeof(unsigned), (void *)&h_token, 0, NULL, NULL);
  133. h_token++;
  134. clEnqueueWriteBuffer(queue, d_token, CL_TRUE, 0, sizeof(unsigned), (void *)&h_token, 0, NULL, NULL);
  135. }
  136. #endif
  137. #ifdef STARPU_USE_CUDA
  138. static void increment_cuda_kernel(void *descr[], void *arg)
  139. {
  140. STARPU_SKIP_IF_VALGRIND;
  141. unsigned *tokenptr = (unsigned *)STARPU_VARIABLE_GET_PTR(descr[0]);
  142. unsigned host_token;
  143. /* This is a dummy technique of course */
  144. cudaMemcpyAsync(&host_token, tokenptr, sizeof(unsigned), cudaMemcpyDeviceToHost, starpu_cuda_get_local_stream());
  145. cudaStreamSynchronize(starpu_cuda_get_local_stream());
  146. host_token++;
  147. cudaMemcpyAsync(tokenptr, &host_token, sizeof(unsigned), cudaMemcpyHostToDevice, starpu_cuda_get_local_stream());
  148. }
  149. #endif
  150. void increment_cpu_kernel(void *descr[], void *arg)
  151. {
  152. STARPU_SKIP_IF_VALGRIND;
  153. unsigned *tokenptr = (unsigned *)STARPU_VARIABLE_GET_PTR(descr[0]);
  154. *tokenptr = *tokenptr + 1;
  155. }
  156. static struct starpu_codelet increment_cl =
  157. {
  158. #ifdef STARPU_USE_CUDA
  159. .cuda_funcs = {increment_cuda_kernel},
  160. .cuda_flags = {STARPU_CUDA_ASYNC},
  161. #endif
  162. #ifdef STARPU_USE_OPENCL
  163. .opencl_funcs = {increment_opencl_kernel},
  164. .opencl_flags = {STARPU_OPENCL_ASYNC},
  165. #endif
  166. .cpu_funcs = {increment_cpu_kernel},
  167. .cpu_funcs_name = {"increment_cpu_kernel"},
  168. .nbuffers = 1,
  169. .modes = {STARPU_RW}
  170. };
  171. struct starpu_codelet increment_cl_redux =
  172. {
  173. #ifdef STARPU_USE_CUDA
  174. .cuda_funcs = {increment_cuda_kernel},
  175. .cuda_flags = {STARPU_CUDA_ASYNC},
  176. #endif
  177. #ifdef STARPU_USE_OPENCL
  178. .opencl_funcs = {increment_opencl_kernel},
  179. .opencl_flags = {STARPU_OPENCL_ASYNC},
  180. #endif
  181. .cpu_funcs = {increment_cpu_kernel},
  182. .cpu_funcs_name = {"increment_cpu_kernel"},
  183. .nbuffers = 1,
  184. .modes = {STARPU_REDUX}
  185. };
  186. int main(int argc, char **argv)
  187. {
  188. int ret;
  189. /* Not supported yet */
  190. if (starpu_get_env_number_default("STARPU_GLOBAL_ARBITER", 0) > 0)
  191. return STARPU_TEST_SKIPPED;
  192. ret = starpu_initialize(NULL, &argc, &argv);
  193. if (ret == -ENODEV) return STARPU_TEST_SKIPPED;
  194. STARPU_CHECK_RETURN_VALUE(ret, "starpu_init");
  195. starpu_variable_data_register(&handle, STARPU_MAIN_RAM, (uintptr_t)&var, sizeof(unsigned));
  196. starpu_data_set_reduction_methods(handle, &redux_cl, &neutral_cl);
  197. #ifdef STARPU_QUICK_CHECK
  198. unsigned ntasks = 32;
  199. unsigned nloops = 4;
  200. #else
  201. unsigned ntasks = 1024;
  202. unsigned nloops = 16;
  203. #endif
  204. unsigned loop;
  205. unsigned t;
  206. for (loop = 0; loop < nloops; loop++)
  207. {
  208. for (t = 0; t < ntasks; t++)
  209. {
  210. struct starpu_task *task = starpu_task_create();
  211. if (t % 10 == 0)
  212. {
  213. task->cl = &increment_cl;
  214. }
  215. else
  216. {
  217. task->cl = &increment_cl_redux;
  218. }
  219. task->handles[0] = handle;
  220. ret = starpu_task_submit(task);
  221. if (ret == -ENODEV) goto enodev;
  222. STARPU_CHECK_RETURN_VALUE(ret, "starpu_task_submit");
  223. }
  224. ret = starpu_data_acquire(handle, STARPU_R);
  225. STARPU_CHECK_RETURN_VALUE(ret, "starpu_data_acquire");
  226. if (var != ntasks * (loop+1))
  227. {
  228. FPRINTF(stderr, "[end of loop] Value %u != Expected value %u\n", var, ntasks * (loop+1));
  229. starpu_data_release(handle);
  230. starpu_data_unregister(handle);
  231. goto err;
  232. }
  233. starpu_data_release(handle);
  234. }
  235. starpu_data_unregister(handle);
  236. if (var != ntasks * nloops)
  237. {
  238. FPRINTF(stderr, "Value %u != Expected value %u\n", var, ntasks * (loop+1));
  239. goto err;
  240. }
  241. starpu_shutdown();
  242. return EXIT_SUCCESS;
  243. enodev:
  244. starpu_data_unregister(handle);
  245. fprintf(stderr, "WARNING: No one can execute this task\n");
  246. /* yes, we do not perform the computation but we did detect that no one
  247. * could perform the kernel, so this is not an error from StarPU */
  248. starpu_shutdown();
  249. return STARPU_TEST_SKIPPED;
  250. err:
  251. starpu_shutdown();
  252. STARPU_RETURN(EXIT_FAILURE);
  253. }