increment_redux_lazy.c 7.0 KB

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