increment_redux.c 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247
  1. /* StarPU --- Runtime system for heterogeneous multicore architectures.
  2. *
  3. * Copyright (C) 2010 Université de Bordeaux 1
  4. * Copyright (C) 2010, 2011 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 "../common/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 unsigned var = 0;
  26. static starpu_data_handle handle;
  27. /*
  28. * Reduction methods
  29. */
  30. #ifdef STARPU_USE_CUDA
  31. static void redux_cuda_kernel(void *descr[], void *arg)
  32. {
  33. unsigned *dst = (unsigned *)STARPU_VARIABLE_GET_PTR(descr[0]);
  34. unsigned *src = (unsigned *)STARPU_VARIABLE_GET_PTR(descr[1]);
  35. unsigned host_dst, host_src;
  36. /* This is a dummy technique of course */
  37. cudaMemcpy(&host_src, src, sizeof(unsigned), cudaMemcpyDeviceToHost);
  38. cudaMemcpy(&host_dst, dst, sizeof(unsigned), cudaMemcpyDeviceToHost);
  39. cudaThreadSynchronize();
  40. host_dst += host_src;
  41. cudaMemcpy(dst, &host_dst, sizeof(unsigned), cudaMemcpyHostToDevice);
  42. cudaThreadSynchronize();
  43. }
  44. static void neutral_cuda_kernel(void *descr[], void *arg)
  45. {
  46. unsigned *dst = (unsigned *)STARPU_VARIABLE_GET_PTR(descr[0]);
  47. /* This is a dummy technique of course */
  48. unsigned host_dst = 0;
  49. cudaMemcpy(dst, &host_dst, sizeof(unsigned), cudaMemcpyHostToDevice);
  50. cudaThreadSynchronize();
  51. }
  52. #endif
  53. #ifdef STARPU_USE_OPENCL
  54. static void redux_opencl_kernel(void *descr[], void *arg)
  55. {
  56. unsigned h_dst, h_src;
  57. cl_mem d_dst = (cl_mem)STARPU_VARIABLE_GET_PTR(descr[0]);
  58. cl_mem d_src = (cl_mem)STARPU_VARIABLE_GET_PTR(descr[1]);
  59. cl_command_queue queue;
  60. starpu_opencl_get_current_queue(&queue);
  61. /* This is a dummy technique of course */
  62. clEnqueueReadBuffer(queue, d_dst, CL_TRUE, 0, sizeof(unsigned), (void *)&h_dst, 0, NULL, NULL);
  63. clEnqueueReadBuffer(queue, d_src, CL_TRUE, 0, sizeof(unsigned), (void *)&h_src, 0, NULL, NULL);
  64. h_dst += h_src;
  65. clEnqueueWriteBuffer(queue, d_dst, CL_TRUE, 0, sizeof(unsigned), (void *)&h_dst, 0, NULL, NULL);
  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. }
  75. #endif
  76. static void redux_cpu_kernel(void *descr[], void *arg)
  77. {
  78. unsigned *dst = (unsigned *)STARPU_VARIABLE_GET_PTR(descr[0]);
  79. unsigned *src = (unsigned *)STARPU_VARIABLE_GET_PTR(descr[1]);
  80. *dst = *dst + *src;
  81. }
  82. static void neutral_cpu_kernel(void *descr[], void *arg)
  83. {
  84. unsigned *dst = (unsigned *)STARPU_VARIABLE_GET_PTR(descr[0]);
  85. *dst = 0;
  86. }
  87. static starpu_codelet redux_cl = {
  88. .where = STARPU_CPU|STARPU_CUDA|STARPU_OPENCL,
  89. #ifdef STARPU_USE_CUDA
  90. .cuda_func = redux_cuda_kernel,
  91. #endif
  92. #ifdef STARPU_USE_OPENCL
  93. .opencl_func = redux_opencl_kernel,
  94. #endif
  95. .cpu_func = redux_cpu_kernel,
  96. .nbuffers = 2
  97. };
  98. static starpu_codelet neutral_cl = {
  99. .where = STARPU_CPU|STARPU_CUDA,
  100. #ifdef STARPU_USE_CUDA
  101. .cuda_func = neutral_cuda_kernel,
  102. #endif
  103. #ifdef STARPU_USE_OPENCL
  104. .opencl_func = neutral_opencl_kernel,
  105. #endif
  106. .cpu_func = neutral_cpu_kernel,
  107. .nbuffers = 1
  108. };
  109. /*
  110. * Increment codelet
  111. */
  112. #ifdef STARPU_USE_OPENCL
  113. /* dummy OpenCL implementation */
  114. static void increment_opencl_kernel(void *descr[], void *cl_arg __attribute__((unused)))
  115. {
  116. cl_mem d_token = (cl_mem)STARPU_VARIABLE_GET_PTR(descr[0]);
  117. unsigned h_token;
  118. cl_command_queue queue;
  119. starpu_opencl_get_current_queue(&queue);
  120. clEnqueueReadBuffer(queue, d_token, CL_TRUE, 0, sizeof(unsigned), (void *)&h_token, 0, NULL, NULL);
  121. h_token++;
  122. clEnqueueWriteBuffer(queue, d_token, CL_TRUE, 0, sizeof(unsigned), (void *)&h_token, 0, NULL, NULL);
  123. }
  124. #endif
  125. #ifdef STARPU_USE_CUDA
  126. static void increment_cuda_kernel(void *descr[], void *arg)
  127. {
  128. unsigned *tokenptr = (unsigned *)STARPU_VARIABLE_GET_PTR(descr[0]);
  129. unsigned host_token;
  130. /* This is a dummy technique of course */
  131. cudaMemcpy(&host_token, tokenptr, sizeof(unsigned), cudaMemcpyDeviceToHost);
  132. cudaThreadSynchronize();
  133. host_token++;
  134. cudaMemcpy(tokenptr, &host_token, sizeof(unsigned), cudaMemcpyHostToDevice);
  135. cudaThreadSynchronize();
  136. }
  137. #endif
  138. static void increment_cpu_kernel(void *descr[], void *arg)
  139. {
  140. unsigned *tokenptr = (unsigned *)STARPU_VARIABLE_GET_PTR(descr[0]);
  141. *tokenptr = *tokenptr + 1;
  142. }
  143. static starpu_codelet increment_cl = {
  144. .where = STARPU_CPU|STARPU_CUDA|STARPU_OPENCL,
  145. #ifdef STARPU_USE_CUDA
  146. .cuda_func = increment_cuda_kernel,
  147. #endif
  148. #ifdef STARPU_USE_OPENCL
  149. .opencl_func = increment_opencl_kernel,
  150. #endif
  151. .cpu_func = increment_cpu_kernel,
  152. .nbuffers = 1
  153. };
  154. int main(int argc, char **argv)
  155. {
  156. int ret;
  157. ret = starpu_init(NULL);
  158. STARPU_CHECK_RETURN_VALUE(ret, "starpu_init");
  159. starpu_variable_data_register(&handle, 0, (uintptr_t)&var, sizeof(unsigned));
  160. starpu_data_set_reduction_methods(handle, &redux_cl, &neutral_cl);
  161. unsigned ntasks = 1024;
  162. unsigned nloops = 16;
  163. unsigned loop;
  164. unsigned t;
  165. for (loop = 0; loop < nloops; loop++)
  166. {
  167. for (t = 0; t < ntasks; t++)
  168. {
  169. struct starpu_task *task = starpu_task_create();
  170. task->cl = &increment_cl;
  171. task->buffers[0].mode = STARPU_REDUX;
  172. task->buffers[0].handle = handle;
  173. int ret = starpu_task_submit(task);
  174. if (ret == -ENODEV) goto enodev;
  175. STARPU_CHECK_RETURN_VALUE(ret, "starpu_task_submit");
  176. }
  177. ret = starpu_data_acquire(handle, STARPU_R);
  178. STARPU_CHECK_RETURN_VALUE(ret, "starpu_data_acquire");
  179. STARPU_ASSERT(var == ntasks*(loop + 1));
  180. starpu_data_release(handle);
  181. }
  182. starpu_data_unregister(handle);
  183. STARPU_ASSERT(var == ntasks*nloops);
  184. starpu_shutdown();
  185. return 0;
  186. enodev:
  187. starpu_data_unregister(handle);
  188. fprintf(stderr, "WARNING: No one can execute this task\n");
  189. /* yes, we do not perform the computation but we did detect that no one
  190. * could perform the kernel, so this is not an error from StarPU */
  191. starpu_shutdown();
  192. return 77;
  193. }