increment_redux_lazy.c 7.2 KB

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