manual_reduction.c 7.4 KB

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