manual_reduction.c 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235
  1. /* StarPU --- Runtime system for heterogeneous multicore architectures.
  2. *
  3. * Copyright (C) 2010 Université de Bordeaux 1
  4. *
  5. * StarPU is free software; you can redistribute it and/or modify
  6. * it under the terms of the GNU Lesser General Public License as published by
  7. * the Free Software Foundation; either version 2.1 of the License, or (at
  8. * your option) any later version.
  9. *
  10. * StarPU is distributed in the hope that it will be useful, but
  11. * WITHOUT ANY WARRANTY; without even the implied warranty of
  12. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
  13. *
  14. * See the GNU Lesser General Public License in COPYING.LGPL for more details.
  15. */
  16. #include <starpu.h>
  17. #include "../common/helper.h"
  18. #ifdef STARPU_USE_CUDA
  19. #include <cuda.h>
  20. #endif
  21. #ifdef STARPU_USE_OPENCL
  22. #include <starpu_opencl.h>
  23. #endif
  24. #define INIT_VALUE 42
  25. #define NTASKS 10000
  26. #define FPRINTF(ofile, fmt, args ...) do { if (!getenv("STARPU_SSILENT")) {fprintf(ofile, fmt, ##args); }} while(0)
  27. static unsigned variable;
  28. static starpu_data_handle variable_handle;
  29. static uintptr_t per_worker[STARPU_NMAXWORKERS];
  30. static starpu_data_handle 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. 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_TRUE, 0, sizeof(variable), (void *)&zero, 0, NULL, NULL);
  51. per_worker[workerid] = (uintptr_t)ptr;
  52. }
  53. break;
  54. #endif
  55. #ifdef STARPU_USE_CUDA
  56. case STARPU_CUDA_WORKER:
  57. cudaMalloc((void **)&per_worker[workerid], sizeof(variable));
  58. cudaMemset((void *)per_worker[workerid], 0, sizeof(variable));
  59. break;
  60. #endif
  61. default:
  62. STARPU_ABORT();
  63. break;
  64. }
  65. STARPU_ASSERT(per_worker[workerid]);
  66. }
  67. /*
  68. * Implement reduction method
  69. */
  70. static void cpu_redux_func(void *descr[], void *cl_arg __attribute__((unused)))
  71. {
  72. unsigned *a = (unsigned *)STARPU_VARIABLE_GET_PTR(descr[0]);
  73. unsigned *b = (unsigned *)STARPU_VARIABLE_GET_PTR(descr[1]);
  74. FPRINTF(stderr, "%u = %u + %u\n", *a + *b, *a, *b);
  75. *a = *a + *b;
  76. }
  77. static struct starpu_codelet_t reduction_codelet = {
  78. .where = STARPU_CPU,
  79. .cpu_func = cpu_redux_func,
  80. .cuda_func = NULL,
  81. .nbuffers = 2,
  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. unsigned *val = (unsigned *)STARPU_VARIABLE_GET_PTR(descr[0]);
  90. *val = *val + 1;
  91. }
  92. #ifdef STARPU_USE_CUDA
  93. /* dummy CUDA implementation */
  94. static void cuda_func_incr(void *descr[], void *cl_arg __attribute__((unused)))
  95. {
  96. unsigned *val = (unsigned *)STARPU_VARIABLE_GET_PTR(descr[0]);
  97. unsigned h_val;
  98. cudaMemcpy(&h_val, val, sizeof(unsigned), cudaMemcpyDeviceToHost);
  99. h_val++;
  100. cudaMemcpy(val, &h_val, sizeof(unsigned), cudaMemcpyHostToDevice);
  101. }
  102. #endif
  103. #ifdef STARPU_USE_OPENCL
  104. /* dummy OpenCL implementation */
  105. static void opencl_func_incr(void *descr[], void *cl_arg __attribute__((unused)))
  106. {
  107. cl_mem d_val = (cl_mem)STARPU_VARIABLE_GET_PTR(descr[0]);
  108. unsigned h_val;
  109. cl_command_queue queue;
  110. starpu_opencl_get_current_queue(&queue);
  111. clEnqueueReadBuffer(queue, d_val, CL_TRUE, 0, sizeof(unsigned), (void *)&h_val, 0, NULL, NULL);
  112. h_val++;
  113. clEnqueueWriteBuffer(queue, d_val, CL_TRUE, 0, sizeof(unsigned), (void *)&h_val, 0, NULL, NULL);
  114. }
  115. #endif
  116. static struct starpu_codelet_t use_data_on_worker_codelet = {
  117. .where = STARPU_CPU|STARPU_CUDA|STARPU_OPENCL,
  118. .cpu_func = cpu_func_incr,
  119. #ifdef STARPU_USE_CUDA
  120. .cuda_func = cuda_func_incr,
  121. #endif
  122. #ifdef STARPU_USE_OPENCL
  123. .opencl_func = opencl_func_incr,
  124. #endif
  125. .nbuffers = 1,
  126. .model = NULL
  127. };
  128. int main(int argc, char **argv)
  129. {
  130. unsigned worker;
  131. unsigned i;
  132. int ret;
  133. variable = INIT_VALUE;
  134. ret = starpu_init(NULL);
  135. STARPU_CHECK_RETURN_VALUE(ret, "starpu_init");
  136. unsigned nworkers = starpu_worker_get_count();
  137. starpu_variable_data_register(&variable_handle, 0, (uintptr_t)&variable, sizeof(unsigned));
  138. /* Allocate a per-worker handle on each worker (and initialize it to 0) */
  139. starpu_execute_on_each_worker(initialize_per_worker_handle, NULL, STARPU_CPU|STARPU_CUDA|STARPU_OPENCL);
  140. /* Register all per-worker handles */
  141. for (worker = 0; worker < nworkers; worker++)
  142. {
  143. STARPU_ASSERT(per_worker[worker]);
  144. unsigned memory_node = starpu_worker_get_memory_node(worker);
  145. starpu_variable_data_register(&per_worker_handle[worker], memory_node,
  146. per_worker[worker], sizeof(variable));
  147. }
  148. /* Submit NTASKS tasks to the different worker to simulate the usage of a data in reduction */
  149. for (i = 0; i < NTASKS; i++)
  150. {
  151. struct starpu_task *task = starpu_task_create();
  152. task->cl = &use_data_on_worker_codelet;
  153. int workerid = (i % nworkers);
  154. task->buffers[0].handle = per_worker_handle[workerid];
  155. task->buffers[0].mode = STARPU_RW;
  156. task->execute_on_a_specific_worker = 1;
  157. task->workerid = (unsigned)workerid;
  158. int ret = starpu_task_submit(task);
  159. if (ret == -ENODEV) goto enodev;
  160. STARPU_CHECK_RETURN_VALUE(ret, "starpu_task_submit");
  161. }
  162. /* Perform the reduction of all per-worker handles into the variable_handle */
  163. for (worker = 0; worker < nworkers; worker++)
  164. {
  165. struct starpu_task *task = starpu_task_create();
  166. task->cl = &reduction_codelet;
  167. task->buffers[0].handle = variable_handle;
  168. task->buffers[0].mode = STARPU_RW;
  169. task->buffers[1].handle = per_worker_handle[worker];
  170. task->buffers[1].mode = STARPU_R;
  171. int ret = starpu_task_submit(task);
  172. if (ret == -ENODEV) goto enodev;
  173. STARPU_CHECK_RETURN_VALUE(ret, "starpu_task_submit");
  174. }
  175. starpu_data_unregister(variable_handle);
  176. /* Destroy all per-worker handles */
  177. for (worker = 0; worker < nworkers; worker++)
  178. starpu_data_unregister_no_coherency(per_worker_handle[worker]);
  179. STARPU_ASSERT(variable == (INIT_VALUE + NTASKS));
  180. starpu_shutdown();
  181. return 0;
  182. enodev:
  183. fprintf(stderr, "WARNING: No one can execute this task\n");
  184. /* yes, we do not perform the computation but we did detect that no one
  185. * could perform the kernel, so this is not an error from StarPU */
  186. starpu_shutdown();
  187. return 77;
  188. }