increment_redux_lazy.c 7.3 KB

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