task.c 8.7 KB

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