manual_reduction.c 8.6 KB

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