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