manual_reduction.c 7.1 KB

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