manual_reduction.c 6.6 KB

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