increment_redux_lazy.c 7.4 KB

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