manual_reduction.c 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220
  1. /*
  2. * StarPU
  3. * Copyright (C) Université Bordeaux 1, CNRS 2008-2010 (see AUTHORS file)
  4. *
  5. * This program 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. * This program 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. #ifdef STARPU_USE_CUDA
  18. #include <cuda.h>
  19. #endif
  20. #ifdef STARPU_USE_OPENCL
  21. #include <CL/cl.h>
  22. #endif
  23. #define INIT_VALUE 42
  24. #define NTASKS 10000
  25. static unsigned variable;
  26. static starpu_data_handle variable_handle;
  27. static uintptr_t per_worker[STARPU_NMAXWORKERS];
  28. static starpu_data_handle per_worker_handle[STARPU_NMAXWORKERS];
  29. /* Create per-worker handles */
  30. static void initialize_per_worker_handle(void *arg __attribute__((unused)))
  31. {
  32. int workerid = starpu_worker_get_id();
  33. /* Allocate memory on the worker, and initialize it to 0 */
  34. switch (starpu_worker_get_type(workerid)) {
  35. case STARPU_CPU_WORKER:
  36. per_worker[workerid] = (uintptr_t)calloc(1, sizeof(variable));
  37. break;
  38. #ifdef STARPU_USE_OPENCL
  39. case STARPU_OPENCL_WORKER:
  40. {
  41. cl_context context;
  42. cl_command_queue queue;
  43. starpu_opencl_get_current_context(&context);
  44. starpu_opencl_get_current_queue(&queue);
  45. cl_mem ptr = clCreateBuffer(context, CL_MEM_READ_WRITE, sizeof(variable), NULL, NULL);
  46. /* Poor's man memset */
  47. unsigned zero = 0;
  48. clEnqueueWriteBuffer(queue, ptr, CL_TRUE, 0, sizeof(variable), (void *)&zero, 0, NULL, NULL);
  49. per_worker[workerid] = (uintptr_t)ptr;
  50. }
  51. break;
  52. #endif
  53. #ifdef STARPU_USE_CUDA
  54. case STARPU_CUDA_WORKER:
  55. cudaMalloc((void **)&per_worker[workerid], sizeof(variable));
  56. cudaMemset((void *)per_worker[workerid], 0, sizeof(variable));
  57. break;
  58. #endif
  59. default:
  60. STARPU_ABORT();
  61. break;
  62. }
  63. STARPU_ASSERT(per_worker[workerid]);
  64. }
  65. /*
  66. * Implement reduction method
  67. */
  68. static void cpu_redux_func(void *descr[], void *cl_arg __attribute__((unused)))
  69. {
  70. unsigned *a = (unsigned *)STARPU_VARIABLE_GET_PTR(descr[0]);
  71. unsigned *b = (unsigned *)STARPU_VARIABLE_GET_PTR(descr[1]);
  72. fprintf(stderr, "%d = %d + %d\n", *a + *b, *a, *b);
  73. *a = *a + *b;
  74. }
  75. static struct starpu_codelet_t reduction_codelet = {
  76. .where = STARPU_CPU,
  77. .cpu_func = cpu_redux_func,
  78. .cuda_func = NULL,
  79. .nbuffers = 2,
  80. .model = NULL
  81. };
  82. /*
  83. * Use per-worker local copy
  84. */
  85. static void cpu_func_incr(void *descr[], void *cl_arg __attribute__((unused)))
  86. {
  87. unsigned *val = (unsigned *)STARPU_VARIABLE_GET_PTR(descr[0]);
  88. *val = *val + 1;
  89. }
  90. #ifdef STARPU_USE_CUDA
  91. /* dummy CUDA implementation */
  92. static void cuda_func_incr(void *descr[], void *cl_arg __attribute__((unused)))
  93. {
  94. unsigned *val = (unsigned *)STARPU_VARIABLE_GET_PTR(descr[0]);
  95. unsigned h_val;
  96. cudaMemcpy(&h_val, val, sizeof(unsigned), cudaMemcpyDeviceToHost);
  97. h_val++;
  98. cudaMemcpy(val, &h_val, sizeof(unsigned), cudaMemcpyHostToDevice);
  99. }
  100. #endif
  101. #ifdef STARPU_USE_OPENCL
  102. /* dummy OpenCL implementation */
  103. static void opencl_func_incr(void *descr[], void *cl_arg __attribute__((unused)))
  104. {
  105. cl_mem d_val = (cl_mem)STARPU_VARIABLE_GET_PTR(descr[0]);
  106. unsigned h_val;
  107. cl_command_queue queue;
  108. starpu_opencl_get_current_queue(&queue);
  109. clEnqueueReadBuffer(queue, d_val, CL_TRUE, 0, sizeof(unsigned), (void *)&h_val, 0, NULL, NULL);
  110. h_val++;
  111. clEnqueueWriteBuffer(queue, d_val, CL_TRUE, 0, sizeof(unsigned), (void *)&h_val, 0, NULL, NULL);
  112. }
  113. #endif
  114. static struct starpu_codelet_t use_data_on_worker_codelet = {
  115. .where = STARPU_CPU|STARPU_CUDA|STARPU_OPENCL,
  116. .cpu_func = cpu_func_incr,
  117. #ifdef STARPU_USE_CUDA
  118. .cuda_func = cuda_func_incr,
  119. #endif
  120. #ifdef STARPU_USE_OPENCL
  121. .opencl_func = opencl_func_incr,
  122. #endif
  123. .nbuffers = 1,
  124. .model = NULL
  125. };
  126. int main(int argc, char **argv)
  127. {
  128. unsigned worker;
  129. unsigned i;
  130. variable = INIT_VALUE;
  131. starpu_init(NULL);
  132. unsigned nworkers = starpu_worker_get_count();
  133. starpu_variable_data_register(&variable_handle, 0, (uintptr_t)&variable, sizeof(unsigned));
  134. /* Allocate a per-worker handle on each worker (and initialize it to 0) */
  135. starpu_execute_on_each_worker(initialize_per_worker_handle, NULL, STARPU_CPU|STARPU_CUDA|STARPU_OPENCL);
  136. /* Register all per-worker handles */
  137. for (worker = 0; worker < nworkers; worker++)
  138. {
  139. STARPU_ASSERT(per_worker[worker]);
  140. unsigned memory_node = starpu_worker_get_memory_node(worker);
  141. starpu_variable_data_register(&per_worker_handle[worker], memory_node,
  142. per_worker[worker], sizeof(variable));
  143. }
  144. /* Submit NTASKS tasks to the different worker to simulate the usage of a data in reduction */
  145. for (i = 0; i < NTASKS; i++)
  146. {
  147. struct starpu_task *task = starpu_task_create();
  148. task->cl = &use_data_on_worker_codelet;
  149. int workerid = (i % nworkers);
  150. task->buffers[0].handle = per_worker_handle[workerid];
  151. task->buffers[0].mode = STARPU_RW;
  152. task->execute_on_a_specific_worker = 1;
  153. task->workerid = (unsigned)workerid;
  154. int ret = starpu_task_submit(task);
  155. STARPU_ASSERT(!ret);
  156. }
  157. /* Perform the reduction of all per-worker handles into the variable_handle */
  158. for (worker = 0; worker < nworkers; worker++)
  159. {
  160. struct starpu_task *task = starpu_task_create();
  161. task->cl = &reduction_codelet;
  162. task->buffers[0].handle = variable_handle;
  163. task->buffers[0].mode = STARPU_RW;
  164. task->buffers[1].handle = per_worker_handle[worker];
  165. task->buffers[1].mode = STARPU_R;
  166. int ret = starpu_task_submit(task);
  167. STARPU_ASSERT(!ret);
  168. }
  169. starpu_data_unregister(variable_handle);
  170. /* Destroy all per-worker handles */
  171. for (worker = 0; worker < nworkers; worker++)
  172. starpu_data_unregister_no_coherency(per_worker_handle[worker]);
  173. STARPU_ASSERT(variable == (INIT_VALUE + NTASKS));
  174. starpu_shutdown();
  175. return 0;
  176. }