execute_on_all.c 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176
  1. /* StarPU --- Runtime system for heterogeneous multicore architectures.
  2. *
  3. * Copyright (C) 2009, 2010 Université de Bordeaux 1
  4. * Copyright (C) 2010, 2011, 2012 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[] __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. int ret;
  36. unsigned w;
  37. struct starpu_task *tasks[STARPU_NMAXWORKERS];
  38. /* create a wrapper codelet */
  39. struct starpu_codelet wrapper_cl =
  40. {
  41. .where = 0xFF,
  42. .cuda_funcs = {wrapper_func, NULL},
  43. .cpu_funcs = {wrapper_func, NULL},
  44. .opencl_funcs = {wrapper_func, NULL},
  45. /* XXX we do not handle Cell .. */
  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]->cl = &wrapper_cl;
  59. tasks[w]->cl_arg = &args;
  60. tasks[w]->execute_on_a_specific_worker = 1;
  61. tasks[w]->workerid = worker;
  62. tasks[w]->detach = 0;
  63. tasks[w]->destroy = 0;
  64. #ifdef STARPU_USE_FXT
  65. _starpu_get_job_associated_to_task(tasks[w])->model_name = name;
  66. #endif
  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. /* create a wrapper codelet */
  96. struct starpu_codelet wrapper_cl =
  97. {
  98. .where = where,
  99. .cuda_funcs = {wrapper_func, NULL},
  100. .cpu_funcs = {wrapper_func, NULL},
  101. .opencl_funcs = {wrapper_func, NULL},
  102. /* XXX we do not handle Cell .. */
  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]->cl = &wrapper_cl;
  115. tasks[worker]->cl_arg = &args;
  116. tasks[worker]->execute_on_a_specific_worker = 1;
  117. tasks[worker]->workerid = worker;
  118. tasks[worker]->detach = 0;
  119. tasks[worker]->destroy = 0;
  120. #ifdef STARPU_USE_FXT
  121. _starpu_get_job_associated_to_task(tasks[worker])->model_name = wrapper_cl.name;
  122. #endif
  123. _starpu_exclude_task_from_dag(tasks[worker]);
  124. ret = _starpu_task_submit_internally(tasks[worker]);
  125. if (ret == -ENODEV)
  126. {
  127. /* if the worker is not able to execute this tasks, we
  128. * don't insist as this means the worker is not
  129. * designated by the "where" bitmap */
  130. starpu_task_destroy(tasks[worker]);
  131. tasks[worker] = NULL;
  132. }
  133. }
  134. for (worker = 0; worker < nworkers; worker++)
  135. {
  136. if (tasks[worker])
  137. {
  138. ret = starpu_task_wait(tasks[worker]);
  139. STARPU_ASSERT(!ret);
  140. starpu_task_destroy(tasks[worker]);
  141. }
  142. }
  143. }
  144. void starpu_execute_on_each_worker(void (*func)(void *), void *arg, uint32_t where) {
  145. starpu_execute_on_each_worker_ex(func, arg, where, NULL);
  146. }