increment_redux_lazy.c 7.6 KB

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