manual_reduction.c 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238
  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 "../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. static unsigned variable;
  27. static starpu_data_handle_t variable_handle;
  28. static uintptr_t per_worker[STARPU_NMAXWORKERS];
  29. static starpu_data_handle_t per_worker_handle[STARPU_NMAXWORKERS];
  30. /* Create per-worker handles */
  31. static void initialize_per_worker_handle(void *arg __attribute__((unused)))
  32. {
  33. int workerid = starpu_worker_get_id();
  34. /* Allocate memory on the worker, and initialize it to 0 */
  35. switch (starpu_worker_get_type(workerid))
  36. {
  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 reduction_codelet =
  78. {
  79. .where = STARPU_CPU,
  80. .cpu_funcs = {cpu_redux_func, NULL},
  81. .cuda_funcs = {NULL},
  82. .nbuffers = 2,
  83. .model = NULL
  84. };
  85. /*
  86. * Use per-worker local copy
  87. */
  88. static void cpu_func_incr(void *descr[], void *cl_arg __attribute__((unused)))
  89. {
  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. unsigned *val = (unsigned *)STARPU_VARIABLE_GET_PTR(descr[0]);
  98. unsigned h_val;
  99. cudaMemcpy(&h_val, val, sizeof(unsigned), cudaMemcpyDeviceToHost);
  100. h_val++;
  101. cudaMemcpy(val, &h_val, sizeof(unsigned), cudaMemcpyHostToDevice);
  102. }
  103. #endif
  104. #ifdef STARPU_USE_OPENCL
  105. /* dummy OpenCL implementation */
  106. static void opencl_func_incr(void *descr[], void *cl_arg __attribute__((unused)))
  107. {
  108. cl_mem d_val = (cl_mem)STARPU_VARIABLE_GET_PTR(descr[0]);
  109. unsigned h_val;
  110. cl_command_queue queue;
  111. starpu_opencl_get_current_queue(&queue);
  112. clEnqueueReadBuffer(queue, d_val, CL_TRUE, 0, sizeof(unsigned), (void *)&h_val, 0, NULL, NULL);
  113. h_val++;
  114. clEnqueueWriteBuffer(queue, d_val, CL_TRUE, 0, sizeof(unsigned), (void *)&h_val, 0, NULL, NULL);
  115. }
  116. #endif
  117. static struct starpu_codelet use_data_on_worker_codelet =
  118. {
  119. .where = STARPU_CPU|STARPU_CUDA|STARPU_OPENCL,
  120. .cpu_funcs = {cpu_func_incr, NULL},
  121. #ifdef STARPU_USE_CUDA
  122. .cuda_funcs = {cuda_func_incr, NULL},
  123. #endif
  124. #ifdef STARPU_USE_OPENCL
  125. .opencl_funcs = {opencl_func_incr, NULL},
  126. #endif
  127. .nbuffers = 1,
  128. .model = NULL
  129. };
  130. int main(int argc, char **argv)
  131. {
  132. unsigned worker;
  133. unsigned i;
  134. int ret;
  135. variable = INIT_VALUE;
  136. ret = starpu_init(NULL);
  137. STARPU_CHECK_RETURN_VALUE(ret, "starpu_init");
  138. unsigned nworkers = starpu_worker_get_count();
  139. starpu_variable_data_register(&variable_handle, 0, (uintptr_t)&variable, sizeof(unsigned));
  140. /* Allocate a per-worker handle on each worker (and initialize it to 0) */
  141. starpu_execute_on_each_worker(initialize_per_worker_handle, NULL, STARPU_CPU|STARPU_CUDA|STARPU_OPENCL);
  142. /* Register all per-worker handles */
  143. for (worker = 0; worker < nworkers; worker++)
  144. {
  145. STARPU_ASSERT(per_worker[worker]);
  146. unsigned memory_node = starpu_worker_get_memory_node(worker);
  147. starpu_variable_data_register(&per_worker_handle[worker], memory_node,
  148. per_worker[worker], sizeof(variable));
  149. }
  150. /* Submit NTASKS tasks to the different worker to simulate the usage of a data in reduction */
  151. for (i = 0; i < NTASKS; i++)
  152. {
  153. struct starpu_task *task = starpu_task_create();
  154. task->cl = &use_data_on_worker_codelet;
  155. int workerid = (i % nworkers);
  156. task->buffers[0].handle = per_worker_handle[workerid];
  157. task->buffers[0].mode = STARPU_RW;
  158. task->execute_on_a_specific_worker = 1;
  159. task->workerid = (unsigned)workerid;
  160. int ret = starpu_task_submit(task);
  161. if (ret == -ENODEV) goto enodev;
  162. STARPU_CHECK_RETURN_VALUE(ret, "starpu_task_submit");
  163. }
  164. /* Perform the reduction of all per-worker handles into the variable_handle */
  165. for (worker = 0; worker < nworkers; worker++)
  166. {
  167. struct starpu_task *task = starpu_task_create();
  168. task->cl = &reduction_codelet;
  169. task->buffers[0].handle = variable_handle;
  170. task->buffers[0].mode = STARPU_RW;
  171. task->buffers[1].handle = per_worker_handle[worker];
  172. task->buffers[1].mode = STARPU_R;
  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. }