increment_redux.c 7.1 KB

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