manual_reduction.c 4.5 KB

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