manual_reduction.c 8.5 KB

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