execute_on_all.c 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174
  1. /* StarPU --- Runtime system for heterogeneous multicore architectures.
  2. *
  3. * Copyright (C) 2011,2012 Inria
  4. * Copyright (C) 2009-2014 Université de Bordeaux
  5. * Copyright (C) 2010-2013,2015,2017 CNRS
  6. *
  7. * StarPU is free software; you can redistribute it and/or modify
  8. * it under the terms of the GNU Lesser General Public License as published by
  9. * the Free Software Foundation; either version 2.1 of the License, or (at
  10. * your option) any later version.
  11. *
  12. * StarPU is distributed in the hope that it will be useful, but
  13. * WITHOUT ANY WARRANTY; without even the implied warranty of
  14. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
  15. *
  16. * See the GNU Lesser General Public License in COPYING.LGPL for more details.
  17. */
  18. #include <starpu.h>
  19. #include <common/config.h>
  20. #include <core/jobs.h>
  21. #include <core/task.h>
  22. struct wrapper_func_args
  23. {
  24. void (*func)(void *);
  25. void *arg;
  26. };
  27. static void wrapper_func(void *buffers[] STARPU_ATTRIBUTE_UNUSED, void *_args)
  28. {
  29. struct wrapper_func_args *args = (struct wrapper_func_args *) _args;
  30. args->func(args->arg);
  31. }
  32. /**
  33. * Execute func(arg) on the given workers.
  34. */
  35. void starpu_execute_on_specific_workers(void (*func)(void*), void * arg, unsigned num_workers, unsigned * workers, const char * name)
  36. {
  37. int ret;
  38. unsigned w;
  39. struct starpu_task *tasks[STARPU_NMAXWORKERS];
  40. /* create a wrapper codelet */
  41. struct starpu_codelet wrapper_cl =
  42. {
  43. .where = 0xFF,
  44. .cuda_funcs = {wrapper_func},
  45. .cpu_funcs = {wrapper_func},
  46. .opencl_funcs = {wrapper_func},
  47. /* XXX we do not handle Cell .. */
  48. .nbuffers = 0,
  49. .name = name
  50. };
  51. struct wrapper_func_args args =
  52. {
  53. .func = func,
  54. .arg = arg
  55. };
  56. for (w = 0; w < num_workers; w++)
  57. {
  58. unsigned worker = workers[w];
  59. tasks[w] = starpu_task_create();
  60. tasks[w]->name = name;
  61. tasks[w]->cl = &wrapper_cl;
  62. tasks[w]->cl_arg = &args;
  63. tasks[w]->execute_on_a_specific_worker = 1;
  64. tasks[w]->workerid = worker;
  65. tasks[w]->detach = 0;
  66. tasks[w]->destroy = 0;
  67. _starpu_exclude_task_from_dag(tasks[w]);
  68. ret = starpu_task_submit(tasks[w]);
  69. if (ret == -ENODEV)
  70. {
  71. /* if the worker is not able to execute this tasks, we
  72. * don't insist as this means the worker is not
  73. * designated by the "where" bitmap */
  74. starpu_task_destroy(tasks[w]);
  75. tasks[w] = NULL;
  76. }
  77. }
  78. for (w= 0; w < num_workers; w++)
  79. {
  80. if (tasks[w])
  81. {
  82. ret = starpu_task_wait(tasks[w]);
  83. STARPU_ASSERT(!ret);
  84. starpu_task_destroy(tasks[w]);
  85. }
  86. }
  87. }
  88. /* execute func(arg) on each worker that matches the "where" flag */
  89. void starpu_execute_on_each_worker_ex(void (*func)(void *), void *arg, uint32_t where, const char * name)
  90. {
  91. int ret;
  92. unsigned worker;
  93. unsigned nworkers = starpu_worker_get_count();
  94. struct starpu_task *tasks[STARPU_NMAXWORKERS];
  95. /* This method only work on CPU, CUDA, OPENCL */
  96. STARPU_ASSERT((where & ~STARPU_CPU & ~STARPU_CUDA & ~STARPU_OPENCL) == 0);
  97. /* create a wrapper codelet */
  98. struct starpu_codelet wrapper_cl =
  99. {
  100. .where = where,
  101. .cuda_funcs = {wrapper_func},
  102. .cpu_funcs = {wrapper_func},
  103. .opencl_funcs = {wrapper_func},
  104. .nbuffers = 0,
  105. .name = (name != NULL ? name : "execute_on_all_wrapper")
  106. };
  107. struct wrapper_func_args args =
  108. {
  109. .func = func,
  110. .arg = arg
  111. };
  112. for (worker = 0; worker < nworkers; worker++)
  113. {
  114. tasks[worker] = starpu_task_create();
  115. tasks[worker]->name = wrapper_cl.name;
  116. tasks[worker]->cl = &wrapper_cl;
  117. tasks[worker]->cl_arg = &args;
  118. tasks[worker]->execute_on_a_specific_worker = 1;
  119. tasks[worker]->workerid = worker;
  120. tasks[worker]->detach = 0;
  121. tasks[worker]->destroy = 0;
  122. _starpu_exclude_task_from_dag(tasks[worker]);
  123. ret = _starpu_task_submit_internally(tasks[worker]);
  124. if (ret == -ENODEV)
  125. {
  126. /* if the worker is not able to execute this task, we
  127. * don't insist as this means the worker is not
  128. * designated by the "where" bitmap */
  129. starpu_task_destroy(tasks[worker]);
  130. tasks[worker] = NULL;
  131. }
  132. }
  133. for (worker = 0; worker < nworkers; worker++)
  134. {
  135. if (tasks[worker])
  136. {
  137. ret = starpu_task_wait(tasks[worker]);
  138. STARPU_ASSERT(!ret);
  139. starpu_task_destroy(tasks[worker]);
  140. }
  141. }
  142. }
  143. void starpu_execute_on_each_worker(void (*func)(void *), void *arg, uint32_t where)
  144. {
  145. starpu_execute_on_each_worker_ex(func, arg, where, NULL);
  146. }