increment_redux_lazy.c 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277
  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. *
  6. * StarPU is free software; you can redistribute it and/or modify
  7. * it under the terms of the GNU Lesser General Public License as published by
  8. * the Free Software Foundation; either version 2.1 of the License, or (at
  9. * your option) any later version.
  10. *
  11. * StarPU is distributed in the hope that it will be useful, but
  12. * WITHOUT ANY WARRANTY; without even the implied warranty of
  13. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
  14. *
  15. * See the GNU Lesser General Public License in COPYING.LGPL for more details.
  16. */
  17. #include <starpu.h>
  18. #include "../helper.h"
  19. /*
  20. * Check that STARPU_REDUX works with a mere incrementation, but without
  21. * initializing the variable
  22. */
  23. static starpu_data_handle_t handle;
  24. /*
  25. * Reduction methods
  26. */
  27. #ifdef STARPU_USE_CUDA
  28. static void redux_cuda_kernel(void *descr[], void *arg)
  29. {
  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. 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. unsigned h_dst, h_src;
  52. cl_mem d_dst = (cl_mem)STARPU_VARIABLE_GET_PTR(descr[0]);
  53. cl_mem d_src = (cl_mem)STARPU_VARIABLE_GET_PTR(descr[1]);
  54. cl_command_queue queue;
  55. starpu_opencl_get_current_queue(&queue);
  56. /* This is a dummy technique of course */
  57. clEnqueueReadBuffer(queue, d_dst, CL_TRUE, 0, sizeof(unsigned), (void *)&h_dst, 0, NULL, NULL);
  58. clEnqueueReadBuffer(queue, d_src, CL_TRUE, 0, sizeof(unsigned), (void *)&h_src, 0, NULL, NULL);
  59. h_dst += h_src;
  60. clEnqueueWriteBuffer(queue, d_dst, CL_TRUE, 0, sizeof(unsigned), (void *)&h_dst, 0, NULL, NULL);
  61. }
  62. static void neutral_opencl_kernel(void *descr[], void *arg)
  63. {
  64. unsigned h_dst = 0;
  65. cl_mem d_dst = (cl_mem)STARPU_VARIABLE_GET_PTR(descr[0]);
  66. cl_command_queue queue;
  67. starpu_opencl_get_current_queue(&queue);
  68. clEnqueueWriteBuffer(queue, d_dst, CL_TRUE, 0, sizeof(unsigned), (void *)&h_dst, 0, NULL, NULL);
  69. }
  70. #endif
  71. void redux_cpu_kernel(void *descr[], void *arg)
  72. {
  73. unsigned *dst = (unsigned *)STARPU_VARIABLE_GET_PTR(descr[0]);
  74. unsigned *src = (unsigned *)STARPU_VARIABLE_GET_PTR(descr[1]);
  75. *dst = *dst + *src;
  76. }
  77. void neutral_cpu_kernel(void *descr[], void *arg)
  78. {
  79. unsigned *dst = (unsigned *)STARPU_VARIABLE_GET_PTR(descr[0]);
  80. *dst = 0;
  81. }
  82. static struct starpu_codelet redux_cl =
  83. {
  84. #ifdef STARPU_USE_CUDA
  85. .cuda_funcs = {redux_cuda_kernel},
  86. .cuda_flags = {STARPU_CUDA_ASYNC},
  87. #endif
  88. #ifdef STARPU_USE_OPENCL
  89. .opencl_funcs = {redux_opencl_kernel},
  90. .opencl_flags = {STARPU_OPENCL_ASYNC},
  91. #endif
  92. .cpu_funcs = {redux_cpu_kernel},
  93. .cpu_funcs_name = {"redux_cpu_kernel"},
  94. .modes = {STARPU_RW, STARPU_R},
  95. .nbuffers = 2
  96. };
  97. static struct starpu_codelet neutral_cl =
  98. {
  99. #ifdef STARPU_USE_CUDA
  100. .cuda_funcs = {neutral_cuda_kernel},
  101. .cuda_flags = {STARPU_CUDA_ASYNC},
  102. #endif
  103. #ifdef STARPU_USE_OPENCL
  104. .opencl_funcs = {neutral_opencl_kernel},
  105. .opencl_flags = {STARPU_OPENCL_ASYNC},
  106. #endif
  107. .cpu_funcs = {neutral_cpu_kernel},
  108. .cpu_funcs_name = {"neutral_cpu_kernel"},
  109. .modes = {STARPU_W},
  110. .nbuffers = 1
  111. };
  112. /*
  113. * Increment codelet
  114. */
  115. #ifdef STARPU_USE_OPENCL
  116. /* dummy OpenCL implementation */
  117. static void increment_opencl_kernel(void *descr[], void *cl_arg STARPU_ATTRIBUTE_UNUSED)
  118. {
  119. cl_mem d_token = (cl_mem)STARPU_VARIABLE_GET_PTR(descr[0]);
  120. unsigned h_token;
  121. cl_command_queue queue;
  122. starpu_opencl_get_current_queue(&queue);
  123. clEnqueueReadBuffer(queue, d_token, CL_TRUE, 0, sizeof(unsigned), (void *)&h_token, 0, NULL, NULL);
  124. h_token++;
  125. clEnqueueWriteBuffer(queue, d_token, CL_TRUE, 0, sizeof(unsigned), (void *)&h_token, 0, NULL, NULL);
  126. }
  127. #endif
  128. #ifdef STARPU_USE_CUDA
  129. static void increment_cuda_kernel(void *descr[], void *arg)
  130. {
  131. unsigned *tokenptr = (unsigned *)STARPU_VARIABLE_GET_PTR(descr[0]);
  132. unsigned host_token;
  133. /* This is a dummy technique of course */
  134. cudaMemcpyAsync(&host_token, tokenptr, sizeof(unsigned), cudaMemcpyDeviceToHost, starpu_cuda_get_local_stream());
  135. cudaStreamSynchronize(starpu_cuda_get_local_stream());
  136. host_token++;
  137. cudaMemcpyAsync(tokenptr, &host_token, sizeof(unsigned), cudaMemcpyHostToDevice, starpu_cuda_get_local_stream());
  138. }
  139. #endif
  140. void increment_cpu_kernel(void *descr[], void *arg)
  141. {
  142. unsigned *tokenptr = (unsigned *)STARPU_VARIABLE_GET_PTR(descr[0]);
  143. *tokenptr = *tokenptr + 1;
  144. }
  145. static struct starpu_codelet increment_cl =
  146. {
  147. #ifdef STARPU_USE_CUDA
  148. .cuda_funcs = {increment_cuda_kernel},
  149. .cuda_flags = {STARPU_CUDA_ASYNC},
  150. #endif
  151. #ifdef STARPU_USE_OPENCL
  152. .opencl_funcs = {increment_opencl_kernel},
  153. .opencl_flags = {STARPU_OPENCL_ASYNC},
  154. #endif
  155. .cpu_funcs = {increment_cpu_kernel},
  156. .cpu_funcs_name = {"increment_cpu_kernel"},
  157. .nbuffers = 1,
  158. .modes = {STARPU_REDUX}
  159. };
  160. int main(int argc, char **argv)
  161. {
  162. int ret;
  163. unsigned *var;
  164. /* Not supported yet */
  165. if (starpu_get_env_number_default("STARPU_GLOBAL_ARBITER", 0) > 0)
  166. return STARPU_TEST_SKIPPED;
  167. ret = starpu_initialize(NULL, &argc, &argv);
  168. if (ret == -ENODEV) return STARPU_TEST_SKIPPED;
  169. STARPU_CHECK_RETURN_VALUE(ret, "starpu_init");
  170. starpu_variable_data_register(&handle, -1, (uintptr_t)NULL, sizeof(unsigned));
  171. starpu_data_set_reduction_methods(handle, &redux_cl, &neutral_cl);
  172. #ifdef STARPU_QUICK_CHECK
  173. unsigned ntasks = 32;
  174. unsigned nloops = 4;
  175. #else
  176. unsigned ntasks = 1024;
  177. unsigned nloops = 16;
  178. #endif
  179. unsigned loop;
  180. unsigned t;
  181. for (loop = 0; loop < nloops; loop++)
  182. {
  183. for (t = 0; t < ntasks; t++)
  184. {
  185. struct starpu_task *task = starpu_task_create();
  186. task->cl = &increment_cl;
  187. task->handles[0] = handle;
  188. ret = starpu_task_submit(task);
  189. if (ret == -ENODEV) goto enodev;
  190. STARPU_CHECK_RETURN_VALUE(ret, "starpu_task_submit");
  191. }
  192. ret = starpu_data_acquire(handle, STARPU_R);
  193. STARPU_CHECK_RETURN_VALUE(ret, "starpu_data_acquire");
  194. var = (unsigned*) starpu_variable_get_local_ptr(handle);
  195. starpu_data_release(handle);
  196. if (*var != ntasks*(loop + 1))
  197. {
  198. ret = EXIT_FAILURE;
  199. FPRINTF(stderr, "[end of loop] Value %u != Expected value %u\n", *var, ntasks * (loop+1));
  200. goto err;
  201. }
  202. }
  203. ret = starpu_data_acquire(handle, STARPU_R);
  204. STARPU_CHECK_RETURN_VALUE(ret, "starpu_data_acquire");
  205. var = (unsigned*) starpu_variable_get_local_ptr(handle);
  206. if (*var != ntasks*nloops)
  207. {
  208. ret = EXIT_FAILURE;
  209. FPRINTF(stderr, "Value %u != Expected value %u\n", *var, ntasks * (loop+1));
  210. goto err;
  211. }
  212. starpu_data_release(handle);
  213. starpu_data_unregister(handle);
  214. err:
  215. starpu_shutdown();
  216. STARPU_RETURN(ret);
  217. enodev:
  218. starpu_data_unregister(handle);
  219. fprintf(stderr, "WARNING: No one can execute this task\n");
  220. /* yes, we do not perform the computation but we did detect that no one
  221. * could perform the kernel, so this is not an error from StarPU */
  222. starpu_shutdown();
  223. STARPU_RETURN(STARPU_TEST_SKIPPED);
  224. }