task.c 8.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317
  1. /*
  2. * StarPU
  3. * Copyright (C) INRIA 2008-2009 (see AUTHORS file)
  4. *
  5. * This program is free software; you can redistribute it and/or modify
  6. * it under the terms of the GNU Lesser General Public License as published by
  7. * the Free Software Foundation; either version 2.1 of the License, or (at
  8. * your option) any later version.
  9. *
  10. * This program is distributed in the hope that it will be useful, but
  11. * WITHOUT ANY WARRANTY; without even the implied warranty of
  12. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
  13. *
  14. * See the GNU Lesser General Public License in COPYING.LGPL for more details.
  15. */
  16. #include <starpu.h>
  17. #include <starpu_profiling.h>
  18. #include <core/workers.h>
  19. #include <core/jobs.h>
  20. #include <core/task.h>
  21. #include <common/config.h>
  22. #include <common/utils.h>
  23. #include <profiling/profiling.h>
  24. /* XXX this should be reinitialized when StarPU is shutdown (or we should make
  25. * sure that no task remains !) */
  26. /* TODO we could make this hierarchical to avoid contention ? */
  27. static pthread_cond_t submitted_cond = PTHREAD_COND_INITIALIZER;
  28. static pthread_mutex_t submitted_mutex = PTHREAD_MUTEX_INITIALIZER;
  29. static long int nsubmitted = 0;
  30. static void _starpu_increment_nsubmitted_tasks(void);
  31. /* This key stores the task currently handled by the thread, note that we
  32. * cannot use the worker structure to store that information because it is
  33. * possible that we have a task with a NULL codelet, which means its callback
  34. * could be executed by a user thread as well. */
  35. static pthread_key_t current_task_key;
  36. void starpu_task_init(struct starpu_task *task)
  37. {
  38. STARPU_ASSERT(task);
  39. task->cl = NULL;
  40. task->cl_arg = NULL;
  41. task->cl_arg_size = 0;
  42. task->callback_func = NULL;
  43. task->callback_arg = NULL;
  44. task->priority = STARPU_DEFAULT_PRIO;
  45. task->use_tag = 0;
  46. task->synchronous = 0;
  47. task->execute_on_a_specific_worker = 0;
  48. task->detach = 1;
  49. /* by default, we do not let StarPU free the task structure since
  50. * starpu_task_init is likely to be used only for statically allocated
  51. * tasks */
  52. task->destroy = 0;
  53. task->regenerate = 0;
  54. task->profiling_info = NULL;
  55. task->starpu_private = NULL;
  56. }
  57. /* Free all the ressources allocated for a task, without deallocating the task
  58. * structure itself (this is required for statically allocated tasks). */
  59. void starpu_task_deinit(struct starpu_task *task)
  60. {
  61. STARPU_ASSERT(task);
  62. /* If a buffer was allocated to store the profiling info, we free it. */
  63. if (task->profiling_info)
  64. {
  65. free(task->profiling_info);
  66. task->profiling_info = NULL;
  67. }
  68. starpu_job_t j = (struct starpu_job_s *)task->starpu_private;
  69. if (j)
  70. _starpu_job_destroy(j);
  71. }
  72. struct starpu_task * __attribute__((malloc)) starpu_task_create(void)
  73. {
  74. struct starpu_task *task;
  75. task = calloc(1, sizeof(struct starpu_task));
  76. STARPU_ASSERT(task);
  77. starpu_task_init(task);
  78. /* Dynamically allocated tasks are destroyed by default */
  79. task->destroy = 1;
  80. return task;
  81. }
  82. /* Free the ressource allocated during starpu_task_create. This function can be
  83. * called automatically after the execution of a task by setting the "destroy"
  84. * flag of the starpu_task structure (default behaviour). Calling this function
  85. * on a statically allocated task results in an undefined behaviour. */
  86. void starpu_task_destroy(struct starpu_task *task)
  87. {
  88. STARPU_ASSERT(task);
  89. starpu_task_deinit(task);
  90. /* TODO handle the case of task with detach = 1 and destroy = 1 */
  91. /* TODO handle the case of non terminated tasks -> return -EINVAL */
  92. free(task);
  93. }
  94. int starpu_task_wait(struct starpu_task *task)
  95. {
  96. STARPU_ASSERT(task);
  97. if (task->detach || task->synchronous)
  98. return -EINVAL;
  99. if (STARPU_UNLIKELY(!_starpu_worker_may_perform_blocking_calls()))
  100. return -EDEADLK;
  101. starpu_job_t j = (struct starpu_job_s *)task->starpu_private;
  102. _starpu_wait_job(j);
  103. /* as this is a synchronous task, the liberation of the job
  104. structure was deferred */
  105. if (task->destroy)
  106. free(task);
  107. return 0;
  108. }
  109. starpu_job_t _starpu_get_job_associated_to_task(struct starpu_task *task)
  110. {
  111. STARPU_ASSERT(task);
  112. if (!task->starpu_private)
  113. {
  114. starpu_job_t j = _starpu_job_create(task);
  115. task->starpu_private = j;
  116. }
  117. return (struct starpu_job_s *)task->starpu_private;
  118. }
  119. /* NB in case we have a regenerable task, it is possible that the job was
  120. * already counted. */
  121. int _starpu_submit_job(starpu_job_t j, unsigned do_not_increment_nsubmitted)
  122. {
  123. j->terminated = 0;
  124. if (!do_not_increment_nsubmitted)
  125. _starpu_increment_nsubmitted_tasks();
  126. j->submitted = 1;
  127. return _starpu_enforce_deps_and_schedule(j, 0);
  128. }
  129. /* application should submit new tasks to StarPU through this function */
  130. int starpu_task_submit(struct starpu_task *task)
  131. {
  132. int ret;
  133. unsigned is_sync = task->synchronous;
  134. if (is_sync)
  135. {
  136. /* Perhaps it is not possible to submit a synchronous
  137. * (blocking) task */
  138. if (STARPU_UNLIKELY(!_starpu_worker_may_perform_blocking_calls()))
  139. return -EDEADLK;
  140. task->detach = 0;
  141. }
  142. STARPU_ASSERT(task);
  143. if (task->cl)
  144. {
  145. uint32_t where = task->cl->where;
  146. if (!_starpu_worker_exists(where))
  147. return -ENODEV;
  148. /* In case we require that a task should be explicitely
  149. * executed on a specific worker, we make sure that the worker
  150. * is able to execute this task. */
  151. if (task->execute_on_a_specific_worker
  152. && !_starpu_worker_may_execute_task(task->workerid, where))
  153. return -ENODEV;
  154. _starpu_detect_implicit_data_deps(task);
  155. }
  156. /* If profiling is activated, we allocate a structure to store the
  157. * appropriate info. */
  158. struct starpu_task_profiling_info *info;
  159. info = _starpu_allocate_profiling_info_if_needed();
  160. task->profiling_info = info;
  161. if (info)
  162. {
  163. info->submit_time = (int64_t)_starpu_timing_now();
  164. info->start_time = -ENOSYS;
  165. info->end_time = -ENOSYS;
  166. }
  167. /* internally, StarPU manipulates a starpu_job_t which is a wrapper around a
  168. * task structure, it is possible that this job structure was already
  169. * allocated, for instance to enforce task depenencies. */
  170. starpu_job_t j;
  171. if (!task->starpu_private)
  172. {
  173. j = _starpu_job_create(task);
  174. task->starpu_private = j;
  175. }
  176. else {
  177. j = (struct starpu_job_s *)task->starpu_private;
  178. }
  179. ret = _starpu_submit_job(j, 0);
  180. if (is_sync)
  181. _starpu_wait_job(j);
  182. return ret;
  183. }
  184. void starpu_display_codelet_stats(struct starpu_codelet_t *cl)
  185. {
  186. unsigned worker;
  187. unsigned nworkers = starpu_worker_get_count();
  188. if (cl->model && cl->model->symbol)
  189. fprintf(stderr, "Statistics for codelet %s\n", cl->model->symbol);
  190. unsigned long total = 0;
  191. for (worker = 0; worker < nworkers; worker++)
  192. total += cl->per_worker_stats[worker];
  193. for (worker = 0; worker < nworkers; worker++)
  194. {
  195. char name[32];
  196. starpu_worker_get_name(worker, name, 32);
  197. fprintf(stderr, "\t%s -> %ld / %ld (%2.2f \%%)\n", name, cl->per_worker_stats[worker], total, (100.0f*cl->per_worker_stats[worker])/total);
  198. }
  199. }
  200. /*
  201. * We wait for all the tasks that have already been submitted. Note that a
  202. * regenerable is not considered finished until it was explicitely set as
  203. * non-regenerale anymore (eg. from a callback).
  204. */
  205. int starpu_task_wait_for_all(void)
  206. {
  207. if (STARPU_UNLIKELY(!_starpu_worker_may_perform_blocking_calls()))
  208. return -EDEADLK;
  209. PTHREAD_MUTEX_LOCK(&submitted_mutex);
  210. while (nsubmitted > 0)
  211. PTHREAD_COND_WAIT(&submitted_cond, &submitted_mutex);
  212. PTHREAD_MUTEX_UNLOCK(&submitted_mutex);
  213. return 0;
  214. }
  215. void _starpu_decrement_nsubmitted_tasks(void)
  216. {
  217. PTHREAD_MUTEX_LOCK(&submitted_mutex);
  218. if (--nsubmitted == 0)
  219. PTHREAD_COND_BROADCAST(&submitted_cond);
  220. PTHREAD_MUTEX_UNLOCK(&submitted_mutex);
  221. }
  222. static void _starpu_increment_nsubmitted_tasks(void)
  223. {
  224. PTHREAD_MUTEX_LOCK(&submitted_mutex);
  225. nsubmitted++;
  226. PTHREAD_MUTEX_UNLOCK(&submitted_mutex);
  227. }
  228. void _starpu_initialize_current_task_key(void)
  229. {
  230. pthread_key_create(&current_task_key, NULL);
  231. }
  232. /* Return the task currently executed by the worker, or NULL if this is called
  233. * either from a thread that is not a task or simply because there is no task
  234. * being executed at the moment. */
  235. struct starpu_task *starpu_get_current_task(void)
  236. {
  237. return pthread_getspecific(current_task_key);
  238. }
  239. void _starpu_set_current_task(struct starpu_task *task)
  240. {
  241. pthread_setspecific(current_task_key, task);
  242. }