increment_redux.c 6.9 KB

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