increment_redux.c 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284
  1. /* StarPU --- Runtime system for heterogeneous multicore architectures.
  2. *
  3. * Copyright (C) 2010, 2012 Université de Bordeaux 1
  4. * Copyright (C) 2010, 2011, 2012 Centre National de la Recherche Scientifique
  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 <config.h>
  18. #include <starpu.h>
  19. #include "../helper.h"
  20. static unsigned var = 0;
  21. static starpu_data_handle_t handle;
  22. /*
  23. * Reduction methods
  24. */
  25. #ifdef STARPU_USE_CUDA
  26. static void redux_cuda_kernel(void *descr[], void *arg)
  27. {
  28. STARPU_SKIP_IF_VALGRIND;
  29. unsigned *dst = (unsigned *)STARPU_VARIABLE_GET_PTR(descr[0]);
  30. unsigned *src = (unsigned *)STARPU_VARIABLE_GET_PTR(descr[1]);
  31. unsigned host_dst, host_src;
  32. /* This is a dummy technique of course */
  33. cudaMemcpyAsync(&host_src, src, sizeof(unsigned), cudaMemcpyDeviceToHost, starpu_cuda_get_local_stream());
  34. cudaMemcpyAsync(&host_dst, dst, sizeof(unsigned), cudaMemcpyDeviceToHost, starpu_cuda_get_local_stream());
  35. cudaStreamSynchronize(starpu_cuda_get_local_stream());
  36. host_dst += host_src;
  37. cudaMemcpyAsync(dst, &host_dst, sizeof(unsigned), cudaMemcpyHostToDevice, starpu_cuda_get_local_stream());
  38. cudaStreamSynchronize(starpu_cuda_get_local_stream());
  39. }
  40. static void neutral_cuda_kernel(void *descr[], void *arg)
  41. {
  42. STARPU_SKIP_IF_VALGRIND;
  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. cudaStreamSynchronize(starpu_cuda_get_local_stream());
  48. }
  49. #endif
  50. #ifdef STARPU_USE_OPENCL
  51. static void redux_opencl_kernel(void *descr[], void *arg)
  52. {
  53. STARPU_SKIP_IF_VALGRIND;
  54. unsigned h_dst, h_src;
  55. cl_mem d_dst = (cl_mem)STARPU_VARIABLE_GET_PTR(descr[0]);
  56. cl_mem d_src = (cl_mem)STARPU_VARIABLE_GET_PTR(descr[1]);
  57. cl_command_queue queue;
  58. starpu_opencl_get_current_queue(&queue);
  59. /* This is a dummy technique of course */
  60. clEnqueueReadBuffer(queue, d_dst, CL_TRUE, 0, sizeof(unsigned), (void *)&h_dst, 0, NULL, NULL);
  61. clEnqueueReadBuffer(queue, d_src, CL_TRUE, 0, sizeof(unsigned), (void *)&h_src, 0, NULL, NULL);
  62. h_dst += h_src;
  63. clEnqueueWriteBuffer(queue, d_dst, CL_TRUE, 0, sizeof(unsigned), (void *)&h_dst, 0, NULL, NULL);
  64. clFinish(queue);
  65. }
  66. static void neutral_opencl_kernel(void *descr[], void *arg)
  67. {
  68. STARPU_SKIP_IF_VALGRIND;
  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. clFinish(queue);
  75. }
  76. #endif
  77. static 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 void neutral_cpu_kernel(void *descr[], void *arg)
  85. {
  86. STARPU_SKIP_IF_VALGRIND;
  87. unsigned *dst = (unsigned *)STARPU_VARIABLE_GET_PTR(descr[0]);
  88. *dst = 0;
  89. }
  90. static struct starpu_codelet redux_cl =
  91. {
  92. .where = STARPU_CPU|STARPU_CUDA|STARPU_OPENCL,
  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. .nbuffers = 2
  101. };
  102. static struct starpu_codelet neutral_cl =
  103. {
  104. .where = STARPU_CPU|STARPU_CUDA,
  105. #ifdef STARPU_USE_CUDA
  106. .cuda_funcs = {neutral_cuda_kernel, NULL},
  107. #endif
  108. #ifdef STARPU_USE_OPENCL
  109. .opencl_funcs = {neutral_opencl_kernel, NULL},
  110. #endif
  111. .cpu_funcs = {neutral_cpu_kernel, NULL},
  112. .nbuffers = 1
  113. };
  114. /*
  115. * Increment codelet
  116. */
  117. #ifdef STARPU_USE_OPENCL
  118. /* dummy OpenCL implementation */
  119. static void increment_opencl_kernel(void *descr[], void *cl_arg __attribute__((unused)))
  120. {
  121. STARPU_SKIP_IF_VALGRIND;
  122. cl_mem d_token = (cl_mem)STARPU_VARIABLE_GET_PTR(descr[0]);
  123. unsigned h_token;
  124. cl_command_queue queue;
  125. starpu_opencl_get_current_queue(&queue);
  126. clEnqueueReadBuffer(queue, d_token, CL_TRUE, 0, sizeof(unsigned), (void *)&h_token, 0, NULL, NULL);
  127. h_token++;
  128. clEnqueueWriteBuffer(queue, d_token, CL_TRUE, 0, sizeof(unsigned), (void *)&h_token, 0, NULL, NULL);
  129. clFinish(queue);
  130. }
  131. #endif
  132. #ifdef STARPU_USE_CUDA
  133. static void increment_cuda_kernel(void *descr[], void *arg)
  134. {
  135. STARPU_SKIP_IF_VALGRIND;
  136. unsigned *tokenptr = (unsigned *)STARPU_VARIABLE_GET_PTR(descr[0]);
  137. unsigned host_token;
  138. /* This is a dummy technique of course */
  139. cudaMemcpyAsync(&host_token, tokenptr, sizeof(unsigned), cudaMemcpyDeviceToHost, starpu_cuda_get_local_stream());
  140. cudaStreamSynchronize(starpu_cuda_get_local_stream());
  141. host_token++;
  142. cudaMemcpyAsync(tokenptr, &host_token, sizeof(unsigned), cudaMemcpyHostToDevice, starpu_cuda_get_local_stream());
  143. cudaStreamSynchronize(starpu_cuda_get_local_stream());
  144. }
  145. #endif
  146. static void increment_cpu_kernel(void *descr[], void *arg)
  147. {
  148. STARPU_SKIP_IF_VALGRIND;
  149. unsigned *tokenptr = (unsigned *)STARPU_VARIABLE_GET_PTR(descr[0]);
  150. *tokenptr = *tokenptr + 1;
  151. }
  152. static struct starpu_codelet increment_cl =
  153. {
  154. .where = STARPU_CPU|STARPU_CUDA|STARPU_OPENCL,
  155. #ifdef STARPU_USE_CUDA
  156. .cuda_funcs = {increment_cuda_kernel, NULL},
  157. #endif
  158. #ifdef STARPU_USE_OPENCL
  159. .opencl_funcs = {increment_opencl_kernel, NULL},
  160. #endif
  161. .cpu_funcs = {increment_cpu_kernel, NULL},
  162. .nbuffers = 1,
  163. .modes = {STARPU_REDUX}
  164. };
  165. int main(int argc, char **argv)
  166. {
  167. int ret;
  168. ret = starpu_init(NULL);
  169. if (ret == -ENODEV) return STARPU_TEST_SKIPPED;
  170. STARPU_CHECK_RETURN_VALUE(ret, "starpu_init");
  171. starpu_variable_data_register(&handle, 0, (uintptr_t)&var, 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. if (var != ntasks * (loop+1))
  196. {
  197. FPRINTF(stderr, "[end of loop] Value %u != Expected value %u\n", var, ntasks * (loop+1));
  198. starpu_data_release(handle);
  199. starpu_data_unregister(handle);
  200. goto err;
  201. }
  202. starpu_data_release(handle);
  203. }
  204. starpu_data_unregister(handle);
  205. if (var != ntasks * nloops)
  206. {
  207. FPRINTF(stderr, "Value %u != Expected value %u\n", var, ntasks * (loop+1));
  208. goto err;
  209. }
  210. starpu_shutdown();
  211. return EXIT_SUCCESS;
  212. enodev:
  213. starpu_data_unregister(handle);
  214. fprintf(stderr, "WARNING: No one can execute this task\n");
  215. /* yes, we do not perform the computation but we did detect that no one
  216. * could perform the kernel, so this is not an error from StarPU */
  217. starpu_shutdown();
  218. return STARPU_TEST_SKIPPED;
  219. err:
  220. starpu_shutdown();
  221. STARPU_RETURN(EXIT_FAILURE);
  222. }