execute_on_all.c 4.2 KB

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