manual_reduction.c 7.3 KB

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