task.c 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297
  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. /* Liberate all the ressources allocated for a task, without deallocating the
  55. * task 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. /* Liberate the ressource allocated during starpu_task_create. This function
  74. * can be called automatically after the execution of a task by setting the
  75. * "destroy" flag of the starpu_task structure (default behaviour). Calling
  76. * this function on a statically allocated task results in an undefined
  77. * behaviour. */
  78. void starpu_task_destroy(struct starpu_task *task)
  79. {
  80. STARPU_ASSERT(task);
  81. starpu_task_deinit(task);
  82. /* TODO handle the case of task with detach = 1 and destroy = 1 */
  83. /* TODO handle the case of non terminated tasks -> return -EINVAL */
  84. free(task);
  85. }
  86. int starpu_task_wait(struct starpu_task *task)
  87. {
  88. STARPU_ASSERT(task);
  89. if (task->detach || task->synchronous)
  90. return -EINVAL;
  91. if (STARPU_UNLIKELY(!_starpu_worker_may_perform_blocking_calls()))
  92. return -EDEADLK;
  93. starpu_job_t j = (struct starpu_job_s *)task->starpu_private;
  94. _starpu_wait_job(j);
  95. /* as this is a synchronous task, the liberation of the job
  96. structure was deferred */
  97. if (task->destroy)
  98. free(task);
  99. return 0;
  100. }
  101. starpu_job_t _starpu_get_job_associated_to_task(struct starpu_task *task)
  102. {
  103. STARPU_ASSERT(task);
  104. if (!task->starpu_private)
  105. {
  106. starpu_job_t j = _starpu_job_create(task);
  107. task->starpu_private = j;
  108. }
  109. return (struct starpu_job_s *)task->starpu_private;
  110. }
  111. /* NB in case we have a regenerable task, it is possible that the job was
  112. * already counted. */
  113. int _starpu_submit_job(starpu_job_t j, unsigned do_not_increment_nsubmitted)
  114. {
  115. j->terminated = 0;
  116. if (!do_not_increment_nsubmitted)
  117. _starpu_increment_nsubmitted_tasks();
  118. j->submitted = 1;
  119. return _starpu_enforce_deps_and_schedule(j, 0);
  120. }
  121. /* application should submit new tasks to StarPU through this function */
  122. int starpu_task_submit(struct starpu_task *task)
  123. {
  124. int ret;
  125. unsigned is_sync = task->synchronous;
  126. if (is_sync)
  127. {
  128. /* Perhaps it is not possible to submit a synchronous
  129. * (blocking) task */
  130. if (STARPU_UNLIKELY(!_starpu_worker_may_perform_blocking_calls()))
  131. return -EDEADLK;
  132. task->detach = 0;
  133. }
  134. STARPU_ASSERT(task);
  135. if (task->cl)
  136. {
  137. uint32_t where = task->cl->where;
  138. if (!_starpu_worker_exists(where))
  139. return -ENODEV;
  140. /* In case we require that a task should be explicitely
  141. * executed on a specific worker, we make sure that the worker
  142. * is able to execute this task. */
  143. if (task->execute_on_a_specific_worker
  144. && !_starpu_worker_may_execute_task(task->workerid, where))
  145. return -ENODEV;
  146. }
  147. _starpu_detect_implicit_data_deps(task);
  148. /* internally, StarPU manipulates a starpu_job_t which is a wrapper around a
  149. * task structure, it is possible that this job structure was already
  150. * allocated, for instance to enforce task depenencies. */
  151. starpu_job_t j;
  152. if (!task->starpu_private)
  153. {
  154. j = _starpu_job_create(task);
  155. task->starpu_private = j;
  156. }
  157. else {
  158. j = (struct starpu_job_s *)task->starpu_private;
  159. }
  160. ret = _starpu_submit_job(j, 0);
  161. if (is_sync)
  162. _starpu_wait_job(j);
  163. return ret;
  164. }
  165. void starpu_display_codelet_stats(struct starpu_codelet_t *cl)
  166. {
  167. unsigned worker;
  168. unsigned nworkers = starpu_get_worker_count();
  169. if (cl->model && cl->model->symbol)
  170. fprintf(stderr, "Statistics for codelet %s\n", cl->model->symbol);
  171. unsigned long total = 0;
  172. for (worker = 0; worker < nworkers; worker++)
  173. total += cl->per_worker_stats[worker];
  174. for (worker = 0; worker < nworkers; worker++)
  175. {
  176. char name[32];
  177. starpu_get_worker_name(worker, name, 32);
  178. fprintf(stderr, "\t%s -> %ld / %ld (%2.2f \%%)\n", name, cl->per_worker_stats[worker], total, (100.0f*cl->per_worker_stats[worker])/total);
  179. }
  180. }
  181. /*
  182. * We wait for all the tasks that have already been submitted. Note that a
  183. * regenerable is not considered finished until it was explicitely set as
  184. * non-regenerale anymore (eg. from a callback).
  185. */
  186. int starpu_wait_all_tasks(void)
  187. {
  188. if (STARPU_UNLIKELY(!_starpu_worker_may_perform_blocking_calls()))
  189. return -EDEADLK;
  190. PTHREAD_MUTEX_LOCK(&submitted_mutex);
  191. while (nsubmitted > 0)
  192. PTHREAD_COND_WAIT(&submitted_cond, &submitted_mutex);
  193. PTHREAD_MUTEX_UNLOCK(&submitted_mutex);
  194. return 0;
  195. }
  196. void _starpu_decrement_nsubmitted_tasks(void)
  197. {
  198. PTHREAD_MUTEX_LOCK(&submitted_mutex);
  199. if (--nsubmitted == 0)
  200. PTHREAD_COND_BROADCAST(&submitted_cond);
  201. PTHREAD_MUTEX_UNLOCK(&submitted_mutex);
  202. }
  203. static void _starpu_increment_nsubmitted_tasks(void)
  204. {
  205. PTHREAD_MUTEX_LOCK(&submitted_mutex);
  206. nsubmitted++;
  207. PTHREAD_MUTEX_UNLOCK(&submitted_mutex);
  208. }
  209. void _starpu_initialize_current_task_key(void)
  210. {
  211. pthread_key_create(&current_task_key, NULL);
  212. }
  213. /* Return the task currently executed by the worker, or NULL if this is called
  214. * either from a thread that is not a task or simply because there is no task
  215. * being executed at the moment. */
  216. struct starpu_task *starpu_get_current_task(void)
  217. {
  218. return pthread_getspecific(current_task_key);
  219. }
  220. void _starpu_set_current_task(struct starpu_task *task)
  221. {
  222. if (task)
  223. STARPU_ASSERT(pthread_getspecific(current_task_key) == NULL);
  224. pthread_setspecific(current_task_key, task);
  225. }