increment_redux_lazy.c 7.7 KB

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