task.c 10 KB

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