manual_reduction.c 6.4 KB

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