increment_redux_lazy.c 7.5 KB

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