execute_on_all.c 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101
  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. /* execute func(arg) on each worker that matches the "where" flag */
  32. void starpu_execute_on_each_worker(void (*func)(void *), void *arg, uint32_t where)
  33. {
  34. int ret;
  35. unsigned worker;
  36. unsigned nworkers = starpu_worker_get_count();
  37. struct starpu_task *tasks[STARPU_NMAXWORKERS];
  38. /* create a wrapper codelet */
  39. struct starpu_codelet wrapper_cl =
  40. {
  41. .where = where,
  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. };
  48. struct wrapper_func_args args =
  49. {
  50. .func = func,
  51. .arg = arg
  52. };
  53. for (worker = 0; worker < nworkers; worker++)
  54. {
  55. tasks[worker] = starpu_task_create();
  56. tasks[worker]->cl = &wrapper_cl;
  57. tasks[worker]->cl_arg = &args;
  58. tasks[worker]->execute_on_a_specific_worker = 1;
  59. tasks[worker]->workerid = worker;
  60. tasks[worker]->detach = 0;
  61. tasks[worker]->destroy = 0;
  62. #ifdef STARPU_USE_FXT
  63. struct _starpu_job *job = _starpu_get_job_associated_to_task(tasks[worker]);
  64. job->model_name = "execute_on_all_wrapper";
  65. #endif
  66. _starpu_exclude_task_from_dag(tasks[worker]);
  67. ret = starpu_task_submit(tasks[worker]);
  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[worker]);
  74. tasks[worker] = NULL;
  75. }
  76. }
  77. for (worker = 0; worker < nworkers; worker++)
  78. {
  79. if (tasks[worker])
  80. {
  81. ret = starpu_task_wait(tasks[worker]);
  82. STARPU_ASSERT(!ret);
  83. starpu_task_destroy(tasks[worker]);
  84. }
  85. }
  86. }