increment_redux_v2.c 7.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306
  1. /* StarPU --- Runtime system for heterogeneous multicore architectures.
  2. *
  3. * Copyright (C) 2011-2013 Université de Bordeaux 1
  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 <config.h>
  17. #include <starpu.h>
  18. #include "../helper.h"
  19. static unsigned var = 0;
  20. static starpu_data_handle_t handle;
  21. /*
  22. * Reduction methods
  23. */
  24. #ifdef STARPU_USE_CUDA
  25. static void redux_cuda_kernel(void *descr[], void *arg)
  26. {
  27. STARPU_SKIP_IF_VALGRIND;
  28. unsigned *dst = (unsigned *)STARPU_VARIABLE_GET_PTR(descr[0]);
  29. unsigned *src = (unsigned *)STARPU_VARIABLE_GET_PTR(descr[1]);
  30. unsigned host_dst, host_src;
  31. /* This is a dummy technique of course */
  32. cudaMemcpyAsync(&host_src, src, sizeof(unsigned), cudaMemcpyDeviceToHost, starpu_cuda_get_local_stream());
  33. cudaMemcpyAsync(&host_dst, dst, sizeof(unsigned), cudaMemcpyDeviceToHost, starpu_cuda_get_local_stream());
  34. cudaStreamSynchronize(starpu_cuda_get_local_stream());
  35. host_dst += host_src;
  36. cudaMemcpyAsync(dst, &host_dst, sizeof(unsigned), cudaMemcpyHostToDevice, starpu_cuda_get_local_stream());
  37. cudaStreamSynchronize(starpu_cuda_get_local_stream());
  38. }
  39. static void neutral_cuda_kernel(void *descr[], void *arg)
  40. {
  41. STARPU_SKIP_IF_VALGRIND;
  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. cudaStreamSynchronize(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. STARPU_SKIP_IF_VALGRIND;
  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. clFinish(queue);
  64. }
  65. static void neutral_opencl_kernel(void *descr[], void *arg)
  66. {
  67. STARPU_SKIP_IF_VALGRIND;
  68. unsigned h_dst = 0;
  69. cl_mem d_dst = (cl_mem)STARPU_VARIABLE_GET_PTR(descr[0]);
  70. cl_command_queue queue;
  71. starpu_opencl_get_current_queue(&queue);
  72. clEnqueueWriteBuffer(queue, d_dst, CL_TRUE, 0, sizeof(unsigned), (void *)&h_dst, 0, NULL, NULL);
  73. clFinish(queue);
  74. }
  75. #endif
  76. static
  77. void redux_cpu_kernel(void *descr[], void *arg)
  78. {
  79. STARPU_SKIP_IF_VALGRIND;
  80. unsigned *dst = (unsigned *)STARPU_VARIABLE_GET_PTR(descr[0]);
  81. unsigned *src = (unsigned *)STARPU_VARIABLE_GET_PTR(descr[1]);
  82. *dst = *dst + *src;
  83. }
  84. static
  85. void neutral_cpu_kernel(void *descr[], void *arg)
  86. {
  87. STARPU_SKIP_IF_VALGRIND;
  88. unsigned *dst = (unsigned *)STARPU_VARIABLE_GET_PTR(descr[0]);
  89. *dst = 0;
  90. }
  91. static struct starpu_codelet redux_cl =
  92. {
  93. #ifdef STARPU_USE_CUDA
  94. .cuda_funcs = {redux_cuda_kernel, NULL},
  95. #endif
  96. #ifdef STARPU_USE_OPENCL
  97. .opencl_funcs = {redux_opencl_kernel, NULL},
  98. #endif
  99. .cpu_funcs = {redux_cpu_kernel, NULL},
  100. .cpu_funcs_name = {"redux_cpu_kernel", NULL},
  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, NULL},
  108. #endif
  109. #ifdef STARPU_USE_OPENCL
  110. .opencl_funcs = {neutral_opencl_kernel, NULL},
  111. #endif
  112. .cpu_funcs = {neutral_cpu_kernel, NULL},
  113. .cpu_funcs_name = {"neutral_cpu_kernel", NULL},
  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 STARPU_ATTRIBUTE_UNUSED)
  123. {
  124. STARPU_SKIP_IF_VALGRIND;
  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. clFinish(queue);
  133. }
  134. #endif
  135. #ifdef STARPU_USE_CUDA
  136. static void increment_cuda_kernel(void *descr[], void *arg)
  137. {
  138. STARPU_SKIP_IF_VALGRIND;
  139. unsigned *tokenptr = (unsigned *)STARPU_VARIABLE_GET_PTR(descr[0]);
  140. unsigned host_token;
  141. /* This is a dummy technique of course */
  142. cudaMemcpyAsync(&host_token, tokenptr, sizeof(unsigned), cudaMemcpyDeviceToHost, starpu_cuda_get_local_stream());
  143. cudaStreamSynchronize(starpu_cuda_get_local_stream());
  144. host_token++;
  145. cudaMemcpyAsync(tokenptr, &host_token, sizeof(unsigned), cudaMemcpyHostToDevice, starpu_cuda_get_local_stream());
  146. cudaStreamSynchronize(starpu_cuda_get_local_stream());
  147. }
  148. #endif
  149. static
  150. void increment_cpu_kernel(void *descr[], void *arg)
  151. {
  152. STARPU_SKIP_IF_VALGRIND;
  153. unsigned *tokenptr = (unsigned *)STARPU_VARIABLE_GET_PTR(descr[0]);
  154. *tokenptr = *tokenptr + 1;
  155. }
  156. static struct starpu_codelet increment_cl =
  157. {
  158. #ifdef STARPU_USE_CUDA
  159. .cuda_funcs = {increment_cuda_kernel, NULL},
  160. #endif
  161. #ifdef STARPU_USE_OPENCL
  162. .opencl_funcs = {increment_opencl_kernel, NULL},
  163. #endif
  164. .cpu_funcs = {increment_cpu_kernel, NULL},
  165. .cpu_funcs_name = {"increment_cpu_kernel", NULL},
  166. .nbuffers = 1,
  167. .modes = {STARPU_RW}
  168. };
  169. struct starpu_codelet increment_cl_redux =
  170. {
  171. #ifdef STARPU_USE_CUDA
  172. .cuda_funcs = {increment_cuda_kernel, NULL},
  173. #endif
  174. #ifdef STARPU_USE_OPENCL
  175. .opencl_funcs = {increment_opencl_kernel, NULL},
  176. #endif
  177. .cpu_funcs = {increment_cpu_kernel, NULL},
  178. .cpu_funcs_name = {"increment_cpu_kernel", NULL},
  179. .nbuffers = 1,
  180. .modes = {STARPU_REDUX}
  181. };
  182. int main(int argc, char **argv)
  183. {
  184. int ret;
  185. ret = starpu_initialize(NULL, &argc, &argv);
  186. if (ret == -ENODEV) return STARPU_TEST_SKIPPED;
  187. STARPU_CHECK_RETURN_VALUE(ret, "starpu_init");
  188. starpu_variable_data_register(&handle, STARPU_MAIN_RAM, (uintptr_t)&var, sizeof(unsigned));
  189. starpu_data_set_reduction_methods(handle, &redux_cl, &neutral_cl);
  190. #ifdef STARPU_QUICK_CHECK
  191. unsigned ntasks = 32;
  192. unsigned nloops = 4;
  193. #else
  194. unsigned ntasks = 1024;
  195. unsigned nloops = 16;
  196. #endif
  197. unsigned loop;
  198. unsigned t;
  199. for (loop = 0; loop < nloops; loop++)
  200. {
  201. for (t = 0; t < ntasks; t++)
  202. {
  203. struct starpu_task *task = starpu_task_create();
  204. if (t % 10 == 0)
  205. {
  206. task->cl = &increment_cl;
  207. }
  208. else
  209. {
  210. task->cl = &increment_cl_redux;
  211. }
  212. task->handles[0] = handle;
  213. ret = starpu_task_submit(task);
  214. if (ret == -ENODEV) goto enodev;
  215. STARPU_CHECK_RETURN_VALUE(ret, "starpu_task_submit");
  216. }
  217. ret = starpu_data_acquire(handle, STARPU_R);
  218. STARPU_CHECK_RETURN_VALUE(ret, "starpu_data_acquire");
  219. if (var != ntasks *(loop+1))
  220. {
  221. _STARPU_DEBUG("%u != %u\n", var, ntasks*(loop+1));
  222. starpu_data_release(handle);
  223. starpu_data_unregister(handle);
  224. goto err;
  225. }
  226. starpu_data_release(handle);
  227. }
  228. starpu_data_unregister(handle);
  229. if (var != ntasks *nloops)
  230. {
  231. _STARPU_DEBUG("%u != %u\n", var, ntasks*nloops);
  232. goto err;
  233. }
  234. starpu_shutdown();
  235. return EXIT_SUCCESS;
  236. enodev:
  237. starpu_data_unregister(handle);
  238. fprintf(stderr, "WARNING: No one can execute this task\n");
  239. /* yes, we do not perform the computation but we did detect that no one
  240. * could perform the kernel, so this is not an error from StarPU */
  241. starpu_shutdown();
  242. return STARPU_TEST_SKIPPED;
  243. err:
  244. starpu_shutdown();
  245. STARPU_RETURN(EXIT_FAILURE);
  246. }