increment_redux_v2.c 8.1 KB

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