increment_redux_lazy.c 7.8 KB

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