manual_reduction.c 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278
  1. /* StarPU --- Runtime system for heterogeneous multicore architectures.
  2. *
  3. * Copyright (C) 2010, 2012 Université de Bordeaux 1
  4. * Copyright (C) 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 <config.h>
  18. #include <starpu.h>
  19. #include "../helper.h"
  20. #ifdef STARPU_USE_CUDA
  21. #include <cuda.h>
  22. #include <starpu_cuda.h>
  23. #endif
  24. #ifdef STARPU_USE_OPENCL
  25. #include <starpu_opencl.h>
  26. #endif
  27. #define INIT_VALUE 42
  28. #define NTASKS 10000
  29. static unsigned variable;
  30. static starpu_data_handle_t variable_handle;
  31. static uintptr_t per_worker[STARPU_NMAXWORKERS];
  32. static starpu_data_handle_t per_worker_handle[STARPU_NMAXWORKERS];
  33. /* Create per-worker handles */
  34. static void initialize_per_worker_handle(void *arg __attribute__((unused)))
  35. {
  36. int workerid = starpu_worker_get_id();
  37. /* Allocate memory on the worker, and initialize it to 0 */
  38. switch (starpu_worker_get_type(workerid))
  39. {
  40. case STARPU_CPU_WORKER:
  41. per_worker[workerid] = (uintptr_t)calloc(1, sizeof(variable));
  42. break;
  43. #ifdef STARPU_USE_OPENCL
  44. case STARPU_OPENCL_WORKER:
  45. {
  46. cl_context context;
  47. cl_command_queue queue;
  48. starpu_opencl_get_current_context(&context);
  49. starpu_opencl_get_current_queue(&queue);
  50. cl_mem ptr = clCreateBuffer(context, CL_MEM_READ_WRITE, sizeof(variable), NULL, NULL);
  51. /* Poor's man memset */
  52. unsigned zero = 0;
  53. clEnqueueWriteBuffer(queue, ptr, CL_FALSE, 0, sizeof(variable), (void *)&zero, 0, NULL, NULL);
  54. clFinish(queue);
  55. per_worker[workerid] = (uintptr_t)ptr;
  56. }
  57. break;
  58. #endif
  59. #ifdef STARPU_USE_CUDA
  60. case STARPU_CUDA_WORKER:
  61. cudaMalloc((void **)&per_worker[workerid], sizeof(variable));
  62. cudaMemset((void *)per_worker[workerid], 0, sizeof(variable));
  63. break;
  64. #endif
  65. default:
  66. STARPU_ABORT();
  67. break;
  68. }
  69. STARPU_ASSERT(per_worker[workerid]);
  70. }
  71. /*
  72. * Implement reduction method
  73. */
  74. static void cpu_redux_func(void *descr[], void *cl_arg __attribute__((unused)))
  75. {
  76. STARPU_SKIP_IF_VALGRIND;
  77. unsigned *a = (unsigned *)STARPU_VARIABLE_GET_PTR(descr[0]);
  78. unsigned *b = (unsigned *)STARPU_VARIABLE_GET_PTR(descr[1]);
  79. FPRINTF(stderr, "%u = %u + %u\n", *a + *b, *a, *b);
  80. *a = *a + *b;
  81. }
  82. static struct starpu_codelet reduction_codelet =
  83. {
  84. .where = STARPU_CPU,
  85. .cpu_funcs = {cpu_redux_func, NULL},
  86. .cuda_funcs = {NULL},
  87. .nbuffers = 2,
  88. .modes = {STARPU_RW, STARPU_R},
  89. .model = NULL
  90. };
  91. /*
  92. * Use per-worker local copy
  93. */
  94. static void cpu_func_incr(void *descr[], void *cl_arg __attribute__((unused)))
  95. {
  96. STARPU_SKIP_IF_VALGRIND;
  97. unsigned *val = (unsigned *)STARPU_VARIABLE_GET_PTR(descr[0]);
  98. *val = *val + 1;
  99. }
  100. #ifdef STARPU_USE_CUDA
  101. /* dummy CUDA implementation */
  102. static void cuda_func_incr(void *descr[], void *cl_arg __attribute__((unused)))
  103. {
  104. STARPU_SKIP_IF_VALGRIND;
  105. unsigned *val = (unsigned *)STARPU_VARIABLE_GET_PTR(descr[0]);
  106. unsigned h_val;
  107. cudaMemcpyAsync(&h_val, val, sizeof(unsigned), cudaMemcpyDeviceToHost, starpu_cuda_get_local_stream());
  108. cudaStreamSynchronize(starpu_cuda_get_local_stream());
  109. h_val++;
  110. cudaMemcpyAsync(val, &h_val, sizeof(unsigned), cudaMemcpyHostToDevice, starpu_cuda_get_local_stream());
  111. cudaStreamSynchronize(starpu_cuda_get_local_stream());
  112. }
  113. #endif
  114. #ifdef STARPU_USE_OPENCL
  115. /* dummy OpenCL implementation */
  116. static void opencl_func_incr(void *descr[], void *cl_arg __attribute__((unused)))
  117. {
  118. STARPU_SKIP_IF_VALGRIND;
  119. cl_mem d_val = (cl_mem)STARPU_VARIABLE_GET_PTR(descr[0]);
  120. unsigned h_val;
  121. cl_command_queue queue;
  122. starpu_opencl_get_current_queue(&queue);
  123. clEnqueueReadBuffer(queue, d_val, CL_FALSE, 0, sizeof(unsigned), (void *)&h_val, 0, NULL, NULL);
  124. clFinish(queue);
  125. h_val++;
  126. clEnqueueWriteBuffer(queue, d_val, CL_FALSE, 0, sizeof(unsigned), (void *)&h_val, 0, NULL, NULL);
  127. clFinish(queue);
  128. }
  129. #endif
  130. static struct starpu_codelet use_data_on_worker_codelet =
  131. {
  132. .where = STARPU_CPU|STARPU_CUDA|STARPU_OPENCL,
  133. .cpu_funcs = {cpu_func_incr, NULL},
  134. #ifdef STARPU_USE_CUDA
  135. .cuda_funcs = {cuda_func_incr, NULL},
  136. #endif
  137. #ifdef STARPU_USE_OPENCL
  138. .opencl_funcs = {opencl_func_incr, NULL},
  139. #endif
  140. .nbuffers = 1,
  141. .modes = {STARPU_RW},
  142. .model = NULL
  143. };
  144. int main(int argc, char **argv)
  145. {
  146. unsigned worker;
  147. unsigned i;
  148. int ret;
  149. variable = INIT_VALUE;
  150. ret = starpu_init(NULL);
  151. if (ret == -ENODEV) return STARPU_TEST_SKIPPED;
  152. STARPU_CHECK_RETURN_VALUE(ret, "starpu_init");
  153. unsigned nworkers = starpu_worker_get_count();
  154. starpu_variable_data_register(&variable_handle, 0, (uintptr_t)&variable, sizeof(unsigned));
  155. /* Allocate a per-worker handle on each worker (and initialize it to 0) */
  156. starpu_execute_on_each_worker(initialize_per_worker_handle, NULL, STARPU_CPU|STARPU_CUDA|STARPU_OPENCL);
  157. /* Register all per-worker handles */
  158. for (worker = 0; worker < nworkers; worker++)
  159. {
  160. STARPU_ASSERT(per_worker[worker]);
  161. unsigned memory_node = starpu_worker_get_memory_node(worker);
  162. starpu_variable_data_register(&per_worker_handle[worker], memory_node,
  163. per_worker[worker], sizeof(variable));
  164. }
  165. /* Submit NTASKS tasks to the different worker to simulate the usage of a data in reduction */
  166. for (i = 0; i < NTASKS; i++)
  167. {
  168. struct starpu_task *task = starpu_task_create();
  169. task->cl = &use_data_on_worker_codelet;
  170. int workerid = (i % nworkers);
  171. task->handles[0] = per_worker_handle[workerid];
  172. task->execute_on_a_specific_worker = 1;
  173. task->workerid = (unsigned)workerid;
  174. int ret = starpu_task_submit(task);
  175. if (ret == -ENODEV) goto enodev;
  176. STARPU_CHECK_RETURN_VALUE(ret, "starpu_task_submit");
  177. }
  178. /* Perform the reduction of all per-worker handles into the variable_handle */
  179. for (worker = 0; worker < nworkers; worker++)
  180. {
  181. struct starpu_task *task = starpu_task_create();
  182. task->cl = &reduction_codelet;
  183. task->handles[0] = variable_handle;
  184. task->handles[1] = per_worker_handle[worker];
  185. int ret = starpu_task_submit(task);
  186. if (ret == -ENODEV) goto enodev;
  187. STARPU_CHECK_RETURN_VALUE(ret, "starpu_task_submit");
  188. }
  189. starpu_data_unregister(variable_handle);
  190. /* Destroy all per-worker handles */
  191. for (worker = 0; worker < nworkers; worker++)
  192. {
  193. starpu_data_unregister_no_coherency(per_worker_handle[worker]);
  194. switch(starpu_worker_get_type(worker))
  195. {
  196. case STARPU_CPU_WORKER:
  197. free((void*)per_worker[worker]);
  198. break;
  199. #ifdef STARPU_USE_CUDA
  200. case STARPU_CUDA_WORKER:
  201. cudaFree((void*)per_worker[worker]);
  202. break;
  203. #endif /* !STARPU_USE_CUDA */
  204. #ifdef STARPU_USE_OPENCL
  205. case STARPU_OPENCL_WORKER:
  206. clReleaseMemObject((void*)per_worker[worker]);
  207. break;
  208. #endif /* !STARPU_USE_OPENCL */
  209. #ifdef STARPU_USE_GORDON
  210. case STARPU_GORDON_WORKER: /* Not supported */
  211. #endif /* ! STARPU_USE_GORDON */
  212. default:
  213. STARPU_ABORT();
  214. }
  215. }
  216. starpu_shutdown();
  217. if (variable == INIT_VALUE + NTASKS)
  218. ret = EXIT_SUCCESS;
  219. else
  220. ret = EXIT_FAILURE;
  221. STARPU_RETURN(ret);
  222. enodev:
  223. fprintf(stderr, "WARNING: No one can execute this task\n");
  224. starpu_task_wait_for_all();
  225. /* yes, we do not perform the computation but we did detect that no one
  226. * could perform the kernel, so this is not an error from StarPU */
  227. starpu_shutdown();
  228. return STARPU_TEST_SKIPPED;
  229. }