increment_redux.c 7.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307
  1. /* StarPU --- Runtime system for heterogeneous multicore architectures.
  2. *
  3. * Copyright (C) 2010-2016 Université de Bordeaux
  4. * Copyright (C) 2012,2013,2017 Inria
  5. * Copyright (C) 2010-2013,2015,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
  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_REDUX}
  178. };
  179. int main(int argc, char **argv)
  180. {
  181. int ret;
  182. /* Not supported yet */
  183. if (starpu_get_env_number_default("STARPU_GLOBAL_ARBITER", 0) > 0)
  184. return STARPU_TEST_SKIPPED;
  185. ret = starpu_initialize(NULL, &argc, &argv);
  186. if (ret == -ENODEV) return STARPU_TEST_SKIPPED;
  187. STARPU_CHECK_RETURN_VALUE(ret, "starpu_init");
  188. starpu_variable_data_register(&handle, STARPU_MAIN_RAM, (uintptr_t)&var, sizeof(unsigned));
  189. starpu_data_set_reduction_methods(handle, &redux_cl, &neutral_cl);
  190. #ifdef STARPU_QUICK_CHECK
  191. unsigned ntasks = 32;
  192. unsigned nloops = 4;
  193. #else
  194. unsigned ntasks = 1024;
  195. unsigned nloops = 16;
  196. #endif
  197. unsigned loop;
  198. unsigned t;
  199. for (loop = 0; loop < nloops; loop++)
  200. {
  201. for (t = 0; t < ntasks; t++)
  202. {
  203. struct starpu_task *task = starpu_task_create();
  204. task->cl = &increment_cl;
  205. task->handles[0] = handle;
  206. ret = starpu_task_submit(task);
  207. if (ret == -ENODEV) goto enodev;
  208. STARPU_CHECK_RETURN_VALUE(ret, "starpu_task_submit");
  209. }
  210. ret = starpu_data_acquire(handle, STARPU_R);
  211. STARPU_CHECK_RETURN_VALUE(ret, "starpu_data_acquire");
  212. if (var != ntasks * (loop+1))
  213. {
  214. FPRINTF(stderr, "[end of loop] Value %u != Expected value %u\n", var, ntasks * (loop+1));
  215. starpu_data_release(handle);
  216. starpu_data_unregister(handle);
  217. goto err;
  218. }
  219. starpu_data_release(handle);
  220. }
  221. starpu_data_unregister(handle);
  222. if (var != ntasks * nloops)
  223. {
  224. FPRINTF(stderr, "Value %u != Expected value %u\n", var, ntasks * (loop+1));
  225. goto err;
  226. }
  227. starpu_shutdown();
  228. return EXIT_SUCCESS;
  229. enodev:
  230. starpu_data_unregister(handle);
  231. fprintf(stderr, "WARNING: No one can execute this task\n");
  232. /* yes, we do not perform the computation but we did detect that no one
  233. * could perform the kernel, so this is not an error from StarPU */
  234. starpu_shutdown();
  235. return STARPU_TEST_SKIPPED;
  236. err:
  237. starpu_shutdown();
  238. STARPU_RETURN(EXIT_FAILURE);
  239. }