manual_reduction.c 7.5 KB

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