execute_on_all.c 2.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798
  1. /* StarPU --- Runtime system for heterogeneous multicore architectures.
  2. *
  3. * Copyright (C) 2009, 2010 Université de Bordeaux 1
  4. * Copyright (C) 2010 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. void (*func)(void *);
  23. void *arg;
  24. };
  25. static void wrapper_func(void *buffers[] __attribute__ ((unused)), void *_args)
  26. {
  27. struct wrapper_func_args *args = _args;
  28. args->func(args->arg);
  29. }
  30. /* execute func(arg) on each worker that matches the "where" flag */
  31. void starpu_execute_on_each_worker(void (*func)(void *), void *arg, uint32_t where)
  32. {
  33. int ret;
  34. unsigned worker;
  35. unsigned nworkers = starpu_worker_get_count();
  36. struct starpu_task *tasks[STARPU_NMAXWORKERS];
  37. /* create a wrapper codelet */
  38. struct starpu_codelet_t wrapper_cl = {
  39. .where = where,
  40. .cuda_func = wrapper_func,
  41. .cpu_func = wrapper_func,
  42. .opencl_func = wrapper_func,
  43. /* XXX we do not handle Cell .. */
  44. .nbuffers = 0
  45. };
  46. struct wrapper_func_args args = {
  47. .func = func,
  48. .arg = arg
  49. };
  50. for (worker = 0; worker < nworkers; worker++)
  51. {
  52. tasks[worker] = starpu_task_create();
  53. tasks[worker]->cl = &wrapper_cl;
  54. tasks[worker]->cl_arg = &args;
  55. tasks[worker]->execute_on_a_specific_worker = 1;
  56. tasks[worker]->workerid = worker;
  57. tasks[worker]->detach = 0;
  58. tasks[worker]->destroy = 0;
  59. #ifdef STARPU_USE_FXT
  60. starpu_job_t job = _starpu_get_job_associated_to_task(tasks[worker]);
  61. job->model_name = "execute_on_all_wrapper";
  62. #endif
  63. _starpu_exclude_task_from_dag(tasks[worker]);
  64. ret = starpu_task_submit(tasks[worker]);
  65. if (ret == -ENODEV)
  66. {
  67. /* if the worker is not able to execute this tasks, we
  68. * don't insist as this means the worker is not
  69. * designated by the "where" bitmap */
  70. starpu_task_destroy(tasks[worker]);
  71. tasks[worker] = NULL;
  72. }
  73. }
  74. for (worker = 0; worker < nworkers; worker++)
  75. {
  76. if (tasks[worker])
  77. {
  78. ret = starpu_task_wait(tasks[worker]);
  79. STARPU_ASSERT(!ret);
  80. starpu_task_destroy(tasks[worker]);
  81. }
  82. }
  83. }