increment_redux_v2.c 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246
  1. /* StarPU --- Runtime system for heterogeneous multicore architectures.
  2. *
  3. * Copyright (C) 2011 Université de Bordeaux 1
  4. *
  5. * StarPU is free software; you can redistribute it and/or modify
  6. * it under the terms of the GNU Lesser General Public License as published by
  7. * the Free Software Foundation; either version 2.1 of the License, or (at
  8. * your option) any later version.
  9. *
  10. * StarPU is distributed in the hope that it will be useful, but
  11. * WITHOUT ANY WARRANTY; without even the implied warranty of
  12. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
  13. *
  14. * See the GNU Lesser General Public License in COPYING.LGPL for more details.
  15. */
  16. #include <starpu.h>
  17. #include "../common/helper.h"
  18. #ifdef STARPU_USE_CUDA
  19. #include <starpu_cuda.h>
  20. #endif
  21. #ifdef STARPU_USE_OPENCL
  22. #include <starpu_opencl.h>
  23. #endif
  24. static unsigned var = 0;
  25. static starpu_data_handle 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. }
  66. static void neutral_opencl_kernel(void *descr[], void *arg)
  67. {
  68. unsigned h_dst = 0;
  69. cl_mem d_dst = (cl_mem)STARPU_VARIABLE_GET_PTR(descr[0]);
  70. cl_command_queue queue;
  71. starpu_opencl_get_current_queue(&queue);
  72. clEnqueueWriteBuffer(queue, d_dst, CL_TRUE, 0, sizeof(unsigned), (void *)&h_dst, 0, NULL, NULL);
  73. }
  74. #endif
  75. static void redux_cpu_kernel(void *descr[], void *arg)
  76. {
  77. unsigned *dst = (unsigned *)STARPU_VARIABLE_GET_PTR(descr[0]);
  78. unsigned *src = (unsigned *)STARPU_VARIABLE_GET_PTR(descr[1]);
  79. *dst = *dst + *src;
  80. }
  81. static void neutral_cpu_kernel(void *descr[], void *arg)
  82. {
  83. unsigned *dst = (unsigned *)STARPU_VARIABLE_GET_PTR(descr[0]);
  84. *dst = 0;
  85. }
  86. static starpu_codelet redux_cl = {
  87. .where = STARPU_CPU|STARPU_CUDA|STARPU_OPENCL,
  88. #ifdef STARPU_USE_CUDA
  89. .cuda_func = redux_cuda_kernel,
  90. #endif
  91. #ifdef STARPU_USE_OPENCL
  92. .opencl_func = redux_opencl_kernel,
  93. #endif
  94. .cpu_func = redux_cpu_kernel,
  95. .nbuffers = 2
  96. };
  97. static starpu_codelet neutral_cl = {
  98. .where = STARPU_CPU|STARPU_CUDA,
  99. #ifdef STARPU_USE_CUDA
  100. .cuda_func = neutral_cuda_kernel,
  101. #endif
  102. #ifdef STARPU_USE_OPENCL
  103. .opencl_func = neutral_opencl_kernel,
  104. #endif
  105. .cpu_func = neutral_cpu_kernel,
  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 __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. cudaMemcpy(&host_token, tokenptr, sizeof(unsigned), cudaMemcpyDeviceToHost);
  131. cudaThreadSynchronize();
  132. host_token++;
  133. cudaMemcpy(tokenptr, &host_token, sizeof(unsigned), cudaMemcpyHostToDevice);
  134. cudaThreadSynchronize();
  135. }
  136. #endif
  137. static void increment_cpu_kernel(void *descr[], void *arg)
  138. {
  139. unsigned *tokenptr = (unsigned *)STARPU_VARIABLE_GET_PTR(descr[0]);
  140. *tokenptr = *tokenptr + 1;
  141. }
  142. static starpu_codelet increment_cl = {
  143. .where = STARPU_CPU|STARPU_CUDA|STARPU_OPENCL,
  144. #ifdef STARPU_USE_CUDA
  145. .cuda_func = increment_cuda_kernel,
  146. #endif
  147. #ifdef STARPU_USE_OPENCL
  148. .opencl_func = increment_opencl_kernel,
  149. #endif
  150. .cpu_func = increment_cpu_kernel,
  151. .nbuffers = 1
  152. };
  153. int main(int argc, char **argv)
  154. {
  155. int ret;
  156. ret = starpu_init(NULL);
  157. STARPU_CHECK_RETURN_VALUE(ret, "starpu_init");
  158. starpu_variable_data_register(&handle, 0, (uintptr_t)&var, sizeof(unsigned));
  159. starpu_data_set_reduction_methods(handle, &redux_cl, &neutral_cl);
  160. unsigned ntasks = 1024;
  161. unsigned nloops = 16;
  162. unsigned loop;
  163. unsigned t;
  164. for (loop = 0; loop < nloops; loop++)
  165. {
  166. for (t = 0; t < ntasks; t++)
  167. {
  168. struct starpu_task *task = starpu_task_create();
  169. task->cl = &increment_cl;
  170. task->buffers[0].mode = (t % 10 == 0)?STARPU_RW:STARPU_REDUX;
  171. task->buffers[0].handle = handle;
  172. int ret = starpu_task_submit(task);
  173. if (ret == -ENODEV) goto enodev;
  174. STARPU_CHECK_RETURN_VALUE(ret, "starpu_task_submit");
  175. }
  176. ret = starpu_data_acquire(handle, STARPU_R);
  177. STARPU_CHECK_RETURN_VALUE(ret, "starpu_data_acquire");
  178. STARPU_ASSERT(var == ntasks*(loop + 1));
  179. starpu_data_release(handle);
  180. }
  181. starpu_data_unregister(handle);
  182. STARPU_ASSERT(var == ntasks*nloops);
  183. starpu_shutdown();
  184. return 0;
  185. enodev:
  186. starpu_data_unregister(handle);
  187. fprintf(stderr, "WARNING: No one can execute this task\n");
  188. /* yes, we do not perform the computation but we did detect that no one
  189. * could perform the kernel, so this is not an error from StarPU */
  190. starpu_shutdown();
  191. return 77;
  192. }