increment_redux_v2.c 7.3 KB

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