task.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458
  1. /* StarPU --- Runtime system for heterogeneous multicore architectures.
  2. *
  3. * Copyright (C) 2009, 2010, 2011 Université de Bordeaux 1
  4. * Copyright (C) 2010, 2011 Centre National de la Recherche Scientifique
  5. * Copyright (C) 2011 Télécom-SudParis
  6. *
  7. * StarPU is free software; you can redistribute it and/or modify
  8. * it under the terms of the GNU Lesser General Public License as published by
  9. * the Free Software Foundation; either version 2.1 of the License, or (at
  10. * your option) any later version.
  11. *
  12. * StarPU is distributed in the hope that it will be useful, but
  13. * WITHOUT ANY WARRANTY; without even the implied warranty of
  14. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
  15. *
  16. * See the GNU Lesser General Public License in COPYING.LGPL for more details.
  17. */
  18. #include <starpu.h>
  19. #include <starpu_profiling.h>
  20. #include <starpu_task_bundle.h>
  21. #include <core/workers.h>
  22. #include <core/jobs.h>
  23. #include <core/task.h>
  24. #include <common/config.h>
  25. #include <common/utils.h>
  26. #include <profiling/profiling.h>
  27. #include <profiling/bound.h>
  28. /* XXX this should be reinitialized when StarPU is shutdown (or we should make
  29. * sure that no task remains !) */
  30. /* TODO we could make this hierarchical to avoid contention ? */
  31. static pthread_cond_t submitted_cond = PTHREAD_COND_INITIALIZER;
  32. static pthread_mutex_t submitted_mutex = PTHREAD_MUTEX_INITIALIZER;
  33. static long int nsubmitted = 0, nready = 0;
  34. static void _starpu_increment_nsubmitted_tasks(void);
  35. /* This key stores the task currently handled by the thread, note that we
  36. * cannot use the worker structure to store that information because it is
  37. * possible that we have a task with a NULL codelet, which means its callback
  38. * could be executed by a user thread as well. */
  39. static pthread_key_t current_task_key;
  40. void starpu_task_init(struct starpu_task *task)
  41. {
  42. STARPU_ASSERT(task);
  43. task->cl = NULL;
  44. task->cl_arg = NULL;
  45. task->cl_arg_size = 0;
  46. task->callback_func = NULL;
  47. task->callback_arg = NULL;
  48. task->priority = STARPU_DEFAULT_PRIO;
  49. task->use_tag = 0;
  50. task->synchronous = 0;
  51. task->execute_on_a_specific_worker = 0;
  52. task->bundle = NULL;
  53. task->detach = 1;
  54. /* by default, we do not let StarPU free the task structure since
  55. * starpu_task_init is likely to be used only for statically allocated
  56. * tasks */
  57. task->destroy = 0;
  58. task->regenerate = 0;
  59. task->status = STARPU_TASK_INVALID;
  60. task->profiling_info = NULL;
  61. task->predicted = -1.0;
  62. task->predicted_transfer = -1.0;
  63. task->starpu_private = NULL;
  64. }
  65. /* Free all the ressources allocated for a task, without deallocating the task
  66. * structure itself (this is required for statically allocated tasks). */
  67. void starpu_task_deinit(struct starpu_task *task)
  68. {
  69. STARPU_ASSERT(task);
  70. /* If a buffer was allocated to store the profiling info, we free it. */
  71. if (task->profiling_info)
  72. {
  73. free(task->profiling_info);
  74. task->profiling_info = NULL;
  75. }
  76. /* If case the task is (still) part of a bundle */
  77. struct starpu_task_bundle *bundle = task->bundle;
  78. if (bundle)
  79. {
  80. _STARPU_PTHREAD_MUTEX_LOCK(&bundle->mutex);
  81. int ret = starpu_task_bundle_remove(bundle, task);
  82. /* Perhaps the bundle was destroyed when removing the last
  83. * entry */
  84. if (ret != 1)
  85. _STARPU_PTHREAD_MUTEX_UNLOCK(&bundle->mutex);
  86. }
  87. starpu_job_t j = (struct starpu_job_s *)task->starpu_private;
  88. if (j)
  89. _starpu_job_destroy(j);
  90. }
  91. struct starpu_task * __attribute__((malloc)) starpu_task_create(void)
  92. {
  93. struct starpu_task *task;
  94. task = (struct starpu_task *) calloc(1, sizeof(struct starpu_task));
  95. STARPU_ASSERT(task);
  96. starpu_task_init(task);
  97. /* Dynamically allocated tasks are destroyed by default */
  98. task->destroy = 1;
  99. return task;
  100. }
  101. /* Free the ressource allocated during starpu_task_create. This function can be
  102. * called automatically after the execution of a task by setting the "destroy"
  103. * flag of the starpu_task structure (default behaviour). Calling this function
  104. * on a statically allocated task results in an undefined behaviour. */
  105. void starpu_task_destroy(struct starpu_task *task)
  106. {
  107. STARPU_ASSERT(task);
  108. /* If starpu_task_destroy is called in a callback, we just set the destroy
  109. flag. The task will be destroyed after the callback returns */
  110. if (task == starpu_get_current_task()
  111. && _starpu_get_local_worker_status() == STATUS_CALLBACK) {
  112. task->destroy = 1;
  113. } else {
  114. starpu_task_deinit(task);
  115. /* TODO handle the case of task with detach = 1 and destroy = 1 */
  116. /* TODO handle the case of non terminated tasks -> return -EINVAL */
  117. free(task);
  118. }
  119. }
  120. int starpu_task_wait(struct starpu_task *task)
  121. {
  122. _STARPU_LOG_IN();
  123. STARPU_ASSERT(task);
  124. if (task->detach || task->synchronous) {
  125. _STARPU_DEBUG("Task is detached or asynchronous. Waiting returns immediately\n");
  126. _STARPU_LOG_OUT_TAG("einval");
  127. return -EINVAL;
  128. }
  129. if (STARPU_UNLIKELY(!_starpu_worker_may_perform_blocking_calls())) {
  130. _STARPU_LOG_OUT_TAG("edeadlk");
  131. return -EDEADLK;
  132. }
  133. starpu_job_t j = (struct starpu_job_s *)task->starpu_private;
  134. _starpu_wait_job(j);
  135. /* as this is a synchronous task, the liberation of the job
  136. structure was deferred */
  137. if (task->destroy)
  138. free(task);
  139. _STARPU_LOG_OUT();
  140. return 0;
  141. }
  142. starpu_job_t _starpu_get_job_associated_to_task(struct starpu_task *task)
  143. {
  144. STARPU_ASSERT(task);
  145. if (!task->starpu_private)
  146. {
  147. starpu_job_t j = _starpu_job_create(task);
  148. task->starpu_private = j;
  149. }
  150. return (struct starpu_job_s *)task->starpu_private;
  151. }
  152. /* NB in case we have a regenerable task, it is possible that the job was
  153. * already counted. */
  154. int _starpu_submit_job(starpu_job_t j)
  155. {
  156. _STARPU_LOG_IN();
  157. /* notify bound computation of a new task */
  158. _starpu_bound_record(j);
  159. j->terminated = 0;
  160. _starpu_increment_nsubmitted_tasks();
  161. _STARPU_PTHREAD_MUTEX_LOCK(&j->sync_mutex);
  162. j->submitted = 1;
  163. int ret = _starpu_enforce_deps_and_schedule(j, 1);
  164. _STARPU_PTHREAD_MUTEX_UNLOCK(&j->sync_mutex);
  165. _STARPU_LOG_OUT();
  166. return ret;
  167. }
  168. /* application should submit new tasks to StarPU through this function */
  169. int starpu_task_submit(struct starpu_task *task)
  170. {
  171. STARPU_ASSERT(task);
  172. int ret;
  173. unsigned is_sync = task->synchronous;
  174. _STARPU_LOG_IN();
  175. if (is_sync)
  176. {
  177. /* Perhaps it is not possible to submit a synchronous
  178. * (blocking) task */
  179. if (STARPU_UNLIKELY(!_starpu_worker_may_perform_blocking_calls())) {
  180. _STARPU_LOG_OUT_TAG("EDEADLK");
  181. return -EDEADLK;
  182. }
  183. task->detach = 0;
  184. }
  185. if (task->cl)
  186. {
  187. uint32_t where = task->cl->where;
  188. unsigned i;
  189. if (!_starpu_worker_exists(where)) {
  190. _STARPU_LOG_OUT_TAG("ENODEV");
  191. return -ENODEV;
  192. }
  193. assert(task->cl->nbuffers <= STARPU_NMAXBUFS);
  194. for (i = 0; i < task->cl->nbuffers; i++) {
  195. /* Make sure handles are not partitioned */
  196. assert(task->buffers[i].handle->nchildren == 0);
  197. }
  198. /* In case we require that a task should be explicitely
  199. * executed on a specific worker, we make sure that the worker
  200. * is able to execute this task. */
  201. if (task->execute_on_a_specific_worker && !starpu_combined_worker_may_execute_task(task->workerid, task, 0)) {
  202. _STARPU_LOG_OUT_TAG("ENODEV");
  203. return -ENODEV;
  204. }
  205. _starpu_detect_implicit_data_deps(task);
  206. if (task->cl->model)
  207. _starpu_load_perfmodel(task->cl->model);
  208. if (task->cl->power_model)
  209. _starpu_load_perfmodel(task->cl->power_model);
  210. if (task->cl->conversion_model)
  211. _starpu_load_perfmodel(task->cl->conversion_model);
  212. _STARPU_DEBUG("Performance models (if available) are loaded\n");
  213. }
  214. /* If profiling is activated, we allocate a structure to store the
  215. * appropriate info. */
  216. struct starpu_task_profiling_info *info;
  217. int profiling = starpu_profiling_status_get();
  218. info = _starpu_allocate_profiling_info_if_needed(task);
  219. task->profiling_info = info;
  220. /* The task is considered as block until we are sure there remains not
  221. * dependency. */
  222. task->status = STARPU_TASK_BLOCKED;
  223. if (profiling)
  224. starpu_clock_gettime(&info->submit_time);
  225. /* internally, StarPU manipulates a starpu_job_t which is a wrapper around a
  226. * task structure, it is possible that this job structure was already
  227. * allocated, for instance to enforce task depenencies. */
  228. starpu_job_t j = _starpu_get_job_associated_to_task(task);
  229. ret = _starpu_submit_job(j);
  230. if (is_sync)
  231. _starpu_wait_job(j);
  232. _STARPU_LOG_OUT();
  233. return ret;
  234. }
  235. void starpu_display_codelet_stats(struct starpu_codelet_t *cl)
  236. {
  237. unsigned worker;
  238. unsigned nworkers = starpu_worker_get_count();
  239. if (cl->model && cl->model->symbol)
  240. fprintf(stderr, "Statistics for codelet %s\n", cl->model->symbol);
  241. unsigned long total = 0;
  242. for (worker = 0; worker < nworkers; worker++)
  243. total += cl->per_worker_stats[worker];
  244. for (worker = 0; worker < nworkers; worker++)
  245. {
  246. char name[32];
  247. starpu_worker_get_name(worker, name, 32);
  248. fprintf(stderr, "\t%s -> %lu / %lu (%2.2f %%)\n", name, cl->per_worker_stats[worker], total, (100.0f*cl->per_worker_stats[worker])/total);
  249. }
  250. }
  251. /*
  252. * We wait for all the tasks that have already been submitted. Note that a
  253. * regenerable is not considered finished until it was explicitely set as
  254. * non-regenerale anymore (eg. from a callback).
  255. */
  256. int starpu_task_wait_for_all(void)
  257. {
  258. if (STARPU_UNLIKELY(!_starpu_worker_may_perform_blocking_calls()))
  259. return -EDEADLK;
  260. _STARPU_PTHREAD_MUTEX_LOCK(&submitted_mutex);
  261. STARPU_TRACE_TASK_WAIT_FOR_ALL;
  262. while (nsubmitted > 0)
  263. _STARPU_PTHREAD_COND_WAIT(&submitted_cond, &submitted_mutex);
  264. _STARPU_PTHREAD_MUTEX_UNLOCK(&submitted_mutex);
  265. return 0;
  266. }
  267. /*
  268. * We wait until there is no ready task any more (i.e. StarPU will not be able
  269. * to progress any more).
  270. */
  271. int starpu_task_wait_for_no_ready(void)
  272. {
  273. if (STARPU_UNLIKELY(!_starpu_worker_may_perform_blocking_calls()))
  274. return -EDEADLK;
  275. _STARPU_PTHREAD_MUTEX_LOCK(&submitted_mutex);
  276. STARPU_TRACE_TASK_WAIT_FOR_ALL;
  277. while (nready > 0)
  278. _STARPU_PTHREAD_COND_WAIT(&submitted_cond, &submitted_mutex);
  279. _STARPU_PTHREAD_MUTEX_UNLOCK(&submitted_mutex);
  280. return 0;
  281. }
  282. void _starpu_decrement_nsubmitted_tasks(void)
  283. {
  284. _STARPU_PTHREAD_MUTEX_LOCK(&submitted_mutex);
  285. if (--nsubmitted == 0)
  286. _STARPU_PTHREAD_COND_BROADCAST(&submitted_cond);
  287. STARPU_TRACE_UPDATE_TASK_CNT(nsubmitted);
  288. _STARPU_PTHREAD_MUTEX_UNLOCK(&submitted_mutex);
  289. }
  290. static void _starpu_increment_nsubmitted_tasks(void)
  291. {
  292. _STARPU_PTHREAD_MUTEX_LOCK(&submitted_mutex);
  293. nsubmitted++;
  294. STARPU_TRACE_UPDATE_TASK_CNT(nsubmitted);
  295. _STARPU_PTHREAD_MUTEX_UNLOCK(&submitted_mutex);
  296. }
  297. void _starpu_increment_nready_tasks(void)
  298. {
  299. _STARPU_PTHREAD_MUTEX_LOCK(&submitted_mutex);
  300. nready++;
  301. _STARPU_PTHREAD_MUTEX_UNLOCK(&submitted_mutex);
  302. }
  303. void _starpu_decrement_nready_tasks(void)
  304. {
  305. _STARPU_PTHREAD_MUTEX_LOCK(&submitted_mutex);
  306. if (--nready == 0)
  307. _STARPU_PTHREAD_COND_BROADCAST(&submitted_cond);
  308. _STARPU_PTHREAD_MUTEX_UNLOCK(&submitted_mutex);
  309. }
  310. void _starpu_initialize_current_task_key(void)
  311. {
  312. pthread_key_create(&current_task_key, NULL);
  313. }
  314. /* Return the task currently executed by the worker, or NULL if this is called
  315. * either from a thread that is not a task or simply because there is no task
  316. * being executed at the moment. */
  317. struct starpu_task *starpu_get_current_task(void)
  318. {
  319. return (struct starpu_task *) pthread_getspecific(current_task_key);
  320. }
  321. void _starpu_set_current_task(struct starpu_task *task)
  322. {
  323. pthread_setspecific(current_task_key, task);
  324. }
  325. double _starpu_task_get_conversion_time(struct starpu_task *task)
  326. {
  327. int i;
  328. double conversion_time = 0.0;
  329. for (i = 0; i < task->cl->nbuffers; i++) {
  330. starpu_data_handle_t handle = task->buffers[i].handle;
  331. enum starpu_data_interface_id id = starpu_get_handle_interface_id(handle);
  332. if (id == STARPU_MULTIFORMAT_INTERFACE_ID) {
  333. struct starpu_multiformat_interface *tmp;
  334. uint32_t node = starpu_worker_get_memory_node(task->workerid);
  335. tmp = starpu_data_get_interface_on_node(handle, node);
  336. conversion_time += tmp->conversion_time;
  337. /* XXX : this may not be the right place to reset this field,
  338. * but we need to make sure the conversion time won't be counted
  339. * twice */
  340. tmp->conversion_time = 0;
  341. }
  342. }
  343. return conversion_time;
  344. }