manual_reduction.c 7.3 KB

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