manual_reduction.c 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178
  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. #define INIT_VALUE 42
  21. #define NTASKS 10000
  22. static unsigned variable;
  23. static starpu_data_handle variable_handle;
  24. static uintptr_t per_worker[STARPU_NMAXWORKERS];
  25. static starpu_data_handle per_worker_handle[STARPU_NMAXWORKERS];
  26. /* Create per-worker handles */
  27. static void initialize_per_worker_handle(void *arg __attribute__((unused)))
  28. {
  29. int workerid = starpu_worker_get_id();
  30. /* Allocate memory on the worker, and initialize it to 0 */
  31. switch (starpu_worker_get_type(workerid)) {
  32. case STARPU_CPU_WORKER:
  33. per_worker[workerid] = (uintptr_t)calloc(1, sizeof(variable));
  34. break;
  35. case STARPU_OPENCL_WORKER:
  36. /* Not supported yet */
  37. STARPU_ABORT();
  38. break;
  39. #ifdef STARPU_USE_CUDA
  40. case STARPU_CUDA_WORKER:
  41. cudaMalloc((void **)&per_worker[workerid], sizeof(variable));
  42. cudaMemset((void *)per_worker[workerid], 0, sizeof(variable));
  43. break;
  44. #endif
  45. default:
  46. STARPU_ABORT();
  47. break;
  48. }
  49. STARPU_ASSERT(per_worker[workerid]);
  50. }
  51. /*
  52. * Implement reduction method
  53. */
  54. static void cpu_redux_func(void *descr[], void *cl_arg __attribute__((unused)))
  55. {
  56. unsigned *a = (unsigned *)STARPU_VARIABLE_GET_PTR(descr[0]);
  57. unsigned *b = (unsigned *)STARPU_VARIABLE_GET_PTR(descr[1]);
  58. fprintf(stderr, "%d = %d + %d\n", *a + *b, *a, *b);
  59. *a = *a + *b;
  60. }
  61. static struct starpu_codelet_t reduction_codelet = {
  62. .where = STARPU_CPU,
  63. .cpu_func = cpu_redux_func,
  64. .cuda_func = NULL,
  65. .nbuffers = 2,
  66. .model = NULL
  67. };
  68. /*
  69. * Use per-worker local copy
  70. */
  71. static void cpu_func_incr(void *descr[], void *cl_arg __attribute__((unused)))
  72. {
  73. unsigned *val = (unsigned *)STARPU_VARIABLE_GET_PTR(descr[0]);
  74. *val = *val + 1;
  75. }
  76. #ifdef STARPU_USE_CUDA
  77. extern void cuda_codelet_unsigned_inc(void *descr[], void *cl_arg);
  78. #endif
  79. static struct starpu_codelet_t use_data_on_worker_codelet = {
  80. .where = STARPU_CPU|STARPU_CUDA,
  81. .cpu_func = cpu_func_incr,
  82. #ifdef STARPU_USE_CUDA
  83. .cuda_func = cuda_codelet_unsigned_inc,
  84. #endif
  85. .nbuffers = 1,
  86. .model = NULL
  87. };
  88. int main(int argc, char **argv)
  89. {
  90. unsigned worker;
  91. unsigned i;
  92. variable = INIT_VALUE;
  93. starpu_init(NULL);
  94. unsigned nworkers = starpu_worker_get_count();
  95. starpu_variable_data_register(&variable_handle, 0, (uintptr_t)&variable, sizeof(unsigned));
  96. /* Allocate a per-worker handle on each worker (and initialize it to 0) */
  97. starpu_execute_on_each_worker(initialize_per_worker_handle, NULL, STARPU_CPU|STARPU_CUDA);
  98. /* Register all per-worker handles */
  99. for (worker = 0; worker < nworkers; worker++)
  100. {
  101. STARPU_ASSERT(per_worker[worker]);
  102. unsigned memory_node = starpu_worker_get_memory_node(worker);
  103. starpu_variable_data_register(&per_worker_handle[worker], memory_node,
  104. per_worker[worker], sizeof(variable));
  105. }
  106. /* Submit NTASKS tasks to the different worker to simulate the usage of a data in reduction */
  107. for (i = 0; i < NTASKS; i++)
  108. {
  109. struct starpu_task *task = starpu_task_create();
  110. task->cl = &use_data_on_worker_codelet;
  111. int workerid = (i % nworkers);
  112. task->buffers[0].handle = per_worker_handle[workerid];
  113. task->buffers[0].mode = STARPU_RW;
  114. task->execute_on_a_specific_worker = 1;
  115. task->workerid = (unsigned)workerid;
  116. int ret = starpu_task_submit(task);
  117. STARPU_ASSERT(!ret);
  118. }
  119. /* Perform the reduction of all per-worker handles into the variable_handle */
  120. for (worker = 0; worker < nworkers; worker++)
  121. {
  122. struct starpu_task *task = starpu_task_create();
  123. task->cl = &reduction_codelet;
  124. task->buffers[0].handle = variable_handle;
  125. task->buffers[0].mode = STARPU_RW;
  126. task->buffers[1].handle = per_worker_handle[worker];
  127. task->buffers[1].mode = STARPU_R;
  128. int ret = starpu_task_submit(task);
  129. STARPU_ASSERT(!ret);
  130. }
  131. starpu_data_unregister(variable_handle);
  132. /* Destroy all per-worker handles */
  133. for (worker = 0; worker < nworkers; worker++)
  134. starpu_data_unregister_no_coherency(per_worker_handle[worker]);
  135. STARPU_ASSERT(variable == (INIT_VALUE + NTASKS));
  136. starpu_shutdown();
  137. return 0;
  138. }