increment_redux.c 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279
  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. }
  71. static void neutral_opencl_kernel(void *descr[], void *arg)
  72. {
  73. STARPU_SKIP_IF_VALGRIND;
  74. unsigned h_dst = 0;
  75. cl_mem d_dst = (cl_mem)STARPU_VARIABLE_GET_PTR(descr[0]);
  76. cl_command_queue queue;
  77. starpu_opencl_get_current_queue(&queue);
  78. clEnqueueWriteBuffer(queue, d_dst, CL_TRUE, 0, sizeof(unsigned), (void *)&h_dst, 0, NULL, NULL);
  79. }
  80. #endif
  81. static void redux_cpu_kernel(void *descr[], void *arg)
  82. {
  83. STARPU_SKIP_IF_VALGRIND;
  84. unsigned *dst = (unsigned *)STARPU_VARIABLE_GET_PTR(descr[0]);
  85. unsigned *src = (unsigned *)STARPU_VARIABLE_GET_PTR(descr[1]);
  86. *dst = *dst + *src;
  87. }
  88. static void neutral_cpu_kernel(void *descr[], void *arg)
  89. {
  90. STARPU_SKIP_IF_VALGRIND;
  91. unsigned *dst = (unsigned *)STARPU_VARIABLE_GET_PTR(descr[0]);
  92. *dst = 0;
  93. }
  94. static struct starpu_codelet redux_cl =
  95. {
  96. .where = STARPU_CPU|STARPU_CUDA|STARPU_OPENCL,
  97. #ifdef STARPU_USE_CUDA
  98. .cuda_funcs = {redux_cuda_kernel, NULL},
  99. #endif
  100. #ifdef STARPU_USE_OPENCL
  101. .opencl_funcs = {redux_opencl_kernel, NULL},
  102. #endif
  103. .cpu_funcs = {redux_cpu_kernel, NULL},
  104. .nbuffers = 2
  105. };
  106. static struct starpu_codelet neutral_cl =
  107. {
  108. .where = STARPU_CPU|STARPU_CUDA,
  109. #ifdef STARPU_USE_CUDA
  110. .cuda_funcs = {neutral_cuda_kernel, NULL},
  111. #endif
  112. #ifdef STARPU_USE_OPENCL
  113. .opencl_funcs = {neutral_opencl_kernel, NULL},
  114. #endif
  115. .cpu_funcs = {neutral_cpu_kernel, NULL},
  116. .nbuffers = 1
  117. };
  118. /*
  119. * Increment codelet
  120. */
  121. #ifdef STARPU_USE_OPENCL
  122. /* dummy OpenCL implementation */
  123. static void increment_opencl_kernel(void *descr[], void *cl_arg __attribute__((unused)))
  124. {
  125. STARPU_SKIP_IF_VALGRIND;
  126. cl_mem d_token = (cl_mem)STARPU_VARIABLE_GET_PTR(descr[0]);
  127. unsigned h_token;
  128. cl_command_queue queue;
  129. starpu_opencl_get_current_queue(&queue);
  130. clEnqueueReadBuffer(queue, d_token, CL_TRUE, 0, sizeof(unsigned), (void *)&h_token, 0, NULL, NULL);
  131. h_token++;
  132. clEnqueueWriteBuffer(queue, d_token, CL_TRUE, 0, sizeof(unsigned), (void *)&h_token, 0, NULL, NULL);
  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. cudaMemcpy(&host_token, tokenptr, sizeof(unsigned), cudaMemcpyDeviceToHost);
  143. cudaThreadSynchronize();
  144. host_token++;
  145. cudaMemcpy(tokenptr, &host_token, sizeof(unsigned), cudaMemcpyHostToDevice);
  146. cudaThreadSynchronize();
  147. }
  148. #endif
  149. static void increment_cpu_kernel(void *descr[], void *arg)
  150. {
  151. STARPU_SKIP_IF_VALGRIND;
  152. unsigned *tokenptr = (unsigned *)STARPU_VARIABLE_GET_PTR(descr[0]);
  153. *tokenptr = *tokenptr + 1;
  154. }
  155. static struct starpu_codelet increment_cl =
  156. {
  157. .where = STARPU_CPU|STARPU_CUDA|STARPU_OPENCL,
  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. .nbuffers = 1,
  166. .modes = {STARPU_REDUX}
  167. };
  168. int main(int argc, char **argv)
  169. {
  170. int ret;
  171. ret = starpu_init(NULL);
  172. if (ret == -ENODEV) return STARPU_TEST_SKIPPED;
  173. STARPU_CHECK_RETURN_VALUE(ret, "starpu_init");
  174. starpu_variable_data_register(&handle, 0, (uintptr_t)&var, sizeof(unsigned));
  175. starpu_data_set_reduction_methods(handle, &redux_cl, &neutral_cl);
  176. unsigned ntasks = 1024;
  177. unsigned nloops = 16;
  178. unsigned loop;
  179. unsigned t;
  180. for (loop = 0; loop < nloops; loop++)
  181. {
  182. for (t = 0; t < ntasks; t++)
  183. {
  184. struct starpu_task *task = starpu_task_create();
  185. task->cl = &increment_cl;
  186. task->handles[0] = handle;
  187. int ret = starpu_task_submit(task);
  188. if (ret == -ENODEV) goto enodev;
  189. STARPU_CHECK_RETURN_VALUE(ret, "starpu_task_submit");
  190. }
  191. ret = starpu_data_acquire(handle, STARPU_R);
  192. STARPU_CHECK_RETURN_VALUE(ret, "starpu_data_acquire");
  193. if (var != ntasks * (loop+1))
  194. {
  195. starpu_data_release(handle);
  196. starpu_data_unregister(handle);
  197. goto err;
  198. }
  199. starpu_data_release(handle);
  200. }
  201. starpu_data_unregister(handle);
  202. if (var != ntasks * nloops)
  203. goto err;
  204. starpu_shutdown();
  205. return EXIT_SUCCESS;
  206. enodev:
  207. starpu_data_unregister(handle);
  208. fprintf(stderr, "WARNING: No one can execute this task\n");
  209. /* yes, we do not perform the computation but we did detect that no one
  210. * could perform the kernel, so this is not an error from StarPU */
  211. starpu_shutdown();
  212. return STARPU_TEST_SKIPPED;
  213. err:
  214. starpu_shutdown();
  215. STARPU_RETURN(EXIT_FAILURE);
  216. }