execute_on_all.c 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171
  1. /* StarPU --- Runtime system for heterogeneous multicore architectures.
  2. *
  3. * Copyright (C) 2009-2021 Université de Bordeaux, CNRS (LaBRI UMR 5800), Inria
  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 <common/config.h>
  18. #include <core/jobs.h>
  19. #include <core/task.h>
  20. #include <core/workers.h>
  21. struct wrapper_func_args
  22. {
  23. void (*func)(void *);
  24. void *arg;
  25. };
  26. static void wrapper_func(void *buffers[] STARPU_ATTRIBUTE_UNUSED, void *_args)
  27. {
  28. struct wrapper_func_args *args = (struct wrapper_func_args *) _args;
  29. args->func(args->arg);
  30. }
  31. /**
  32. * Execute func(arg) on the given workers.
  33. */
  34. void starpu_execute_on_specific_workers(void (*func)(void*), void * arg, unsigned num_workers, unsigned * workers, const char * name)
  35. {
  36. int ret;
  37. unsigned w;
  38. struct starpu_task *tasks[STARPU_NMAXWORKERS];
  39. /* create a wrapper codelet */
  40. struct starpu_codelet wrapper_cl =
  41. {
  42. .where = 0xFF,
  43. .cuda_funcs = {wrapper_func},
  44. .cpu_funcs = {wrapper_func},
  45. .opencl_funcs = {wrapper_func},
  46. .nbuffers = 0,
  47. .name = name
  48. };
  49. struct wrapper_func_args args =
  50. {
  51. .func = func,
  52. .arg = arg
  53. };
  54. for (w = 0; w < num_workers; w++)
  55. {
  56. unsigned worker = workers[w];
  57. tasks[w] = starpu_task_create();
  58. tasks[w]->name = name;
  59. tasks[w]->cl = &wrapper_cl;
  60. tasks[w]->cl_arg = &args;
  61. tasks[w]->execute_on_a_specific_worker = 1;
  62. tasks[w]->workerid = worker;
  63. tasks[w]->detach = 0;
  64. tasks[w]->destroy = 0;
  65. _starpu_exclude_task_from_dag(tasks[w]);
  66. ret = starpu_task_submit(tasks[w]);
  67. if (ret == -ENODEV)
  68. {
  69. /* if the worker is not able to execute this tasks, we
  70. * don't insist as this means the worker is not
  71. * designated by the "where" bitmap */
  72. starpu_task_destroy(tasks[w]);
  73. tasks[w] = NULL;
  74. }
  75. }
  76. for (w= 0; w < num_workers; w++)
  77. {
  78. if (tasks[w])
  79. {
  80. ret = starpu_task_wait(tasks[w]);
  81. STARPU_ASSERT(!ret);
  82. starpu_task_destroy(tasks[w]);
  83. }
  84. }
  85. }
  86. /* execute func(arg) on each worker that matches the "where" flag */
  87. void starpu_execute_on_each_worker_ex(void (*func)(void *), void *arg, uint32_t where, const char * name)
  88. {
  89. int ret;
  90. unsigned worker;
  91. unsigned nworkers = starpu_worker_get_count();
  92. struct starpu_task *tasks[STARPU_NMAXWORKERS];
  93. STARPU_ASSERT_MSG((where & ~STARPU_CPU & ~STARPU_CUDA & ~STARPU_OPENCL) == 0, "This function is implemented only on CPU, CUDA, OpenCL");
  94. /* create a wrapper codelet */
  95. struct starpu_codelet wrapper_cl =
  96. {
  97. .where = where,
  98. .cuda_funcs = {wrapper_func},
  99. .cpu_funcs = {wrapper_func},
  100. .opencl_funcs = {wrapper_func},
  101. .nbuffers = 0,
  102. .name = (name != NULL ? name : "execute_on_all_wrapper")
  103. };
  104. struct wrapper_func_args args =
  105. {
  106. .func = func,
  107. .arg = arg
  108. };
  109. for (worker = 0; worker < nworkers; worker++)
  110. {
  111. tasks[worker] = starpu_task_create();
  112. tasks[worker]->name = wrapper_cl.name;
  113. tasks[worker]->cl = &wrapper_cl;
  114. tasks[worker]->cl_arg = &args;
  115. tasks[worker]->execute_on_a_specific_worker = 1;
  116. tasks[worker]->workerid = worker;
  117. tasks[worker]->detach = 0;
  118. tasks[worker]->destroy = 0;
  119. _starpu_exclude_task_from_dag(tasks[worker]);
  120. ret = _starpu_task_submit_internally(tasks[worker]);
  121. if (ret == -ENODEV)
  122. {
  123. /* if the worker is not able to execute this task, we
  124. * don't insist as this means the worker is not
  125. * designated by the "where" bitmap */
  126. starpu_task_destroy(tasks[worker]);
  127. tasks[worker] = NULL;
  128. }
  129. }
  130. for (worker = 0; worker < nworkers; worker++)
  131. {
  132. if (tasks[worker])
  133. {
  134. ret = starpu_task_wait(tasks[worker]);
  135. STARPU_ASSERT(!ret);
  136. starpu_task_destroy(tasks[worker]);
  137. }
  138. }
  139. }
  140. void starpu_execute_on_each_worker(void (*func)(void *), void *arg, uint32_t where)
  141. {
  142. starpu_execute_on_each_worker_ex(func, arg, where, NULL);
  143. }