increment_redux_v2.c 8.3 KB

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