task.c 7.5 KB

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