jobs.c 25 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825
  1. /* StarPU --- Runtime system for heterogeneous multicore architectures.
  2. *
  3. * Copyright (C) 2011-2017 Inria
  4. * Copyright (C) 2008-2019 Université de Bordeaux
  5. * Copyright (C) 2010-2019 CNRS
  6. * Copyright (C) 2013 Thibaut Lambert
  7. * Copyright (C) 2011 Télécom-SudParis
  8. *
  9. * StarPU is free software; you can redistribute it and/or modify
  10. * it under the terms of the GNU Lesser General Public License as published by
  11. * the Free Software Foundation; either version 2.1 of the License, or (at
  12. * your option) any later version.
  13. *
  14. * StarPU is distributed in the hope that it will be useful, but
  15. * WITHOUT ANY WARRANTY; without even the implied warranty of
  16. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
  17. *
  18. * See the GNU Lesser General Public License in COPYING.LGPL for more details.
  19. */
  20. #include <starpu.h>
  21. #include <core/jobs.h>
  22. #include <core/task.h>
  23. #include <core/workers.h>
  24. #include <core/dependencies/data_concurrency.h>
  25. #include <common/config.h>
  26. #include <common/utils.h>
  27. #include <common/graph.h>
  28. #include <profiling/profiling.h>
  29. #include <profiling/bound.h>
  30. #include <core/debug.h>
  31. #include <limits.h>
  32. static int max_memory_use;
  33. static unsigned long njobs, maxnjobs;
  34. #ifdef STARPU_DEBUG
  35. /* List of all jobs, for debugging */
  36. static struct _starpu_job_multilist_all_submitted all_jobs_list;
  37. static starpu_pthread_mutex_t all_jobs_list_mutex = STARPU_PTHREAD_MUTEX_INITIALIZER;
  38. #endif
  39. void _starpu_job_init(void)
  40. {
  41. max_memory_use = starpu_get_env_number_default("STARPU_MAX_MEMORY_USE", 0);
  42. #ifdef STARPU_DEBUG
  43. _starpu_job_multilist_head_init_all_submitted(&all_jobs_list);
  44. #endif
  45. }
  46. void _starpu_job_fini(void)
  47. {
  48. if (max_memory_use)
  49. {
  50. _STARPU_DISP("Memory used for %lu tasks: %lu MiB\n", maxnjobs, (unsigned long) (maxnjobs * (sizeof(struct starpu_task) + sizeof(struct _starpu_job))) >> 20);
  51. STARPU_ASSERT_MSG(njobs == 0, "Some tasks have not been cleaned, did you forget to call starpu_task_destroy or starpu_task_clean?");
  52. }
  53. }
  54. void _starpu_exclude_task_from_dag(struct starpu_task *task)
  55. {
  56. struct _starpu_job *j = _starpu_get_job_associated_to_task(task);
  57. j->exclude_from_dag = 1;
  58. }
  59. /* create an internal struct _starpu_job structure to encapsulate the task */
  60. struct _starpu_job* STARPU_ATTRIBUTE_MALLOC _starpu_job_create(struct starpu_task *task)
  61. {
  62. struct _starpu_job *job;
  63. _STARPU_LOG_IN();
  64. _STARPU_MALLOC(job, sizeof(*job));
  65. /* As most of the fields must be initialized at NULL, let's put 0
  66. * everywhere */
  67. memset(job, 0, sizeof(*job));
  68. if (task->dyn_handles)
  69. {
  70. _STARPU_MALLOC(job->dyn_ordered_buffers, STARPU_TASK_GET_NBUFFERS(task) * sizeof(job->dyn_ordered_buffers[0]));
  71. _STARPU_CALLOC(job->dyn_dep_slots, STARPU_TASK_GET_NBUFFERS(task), sizeof(job->dyn_dep_slots[0]));
  72. }
  73. job->task = task;
  74. #ifndef STARPU_USE_FXT
  75. if (_starpu_bound_recording || _starpu_task_break_on_push != -1 || _starpu_task_break_on_sched != -1 || _starpu_task_break_on_pop != -1 || _starpu_task_break_on_exec != -1 || STARPU_AYU_EVENT)
  76. #endif
  77. {
  78. job->job_id = _starpu_fxt_get_job_id();
  79. STARPU_AYU_ADDTASK(job->job_id, task);
  80. STARPU_ASSERT(job->job_id != ULONG_MAX);
  81. }
  82. if (max_memory_use)
  83. {
  84. unsigned long jobs = STARPU_ATOMIC_ADDL(&njobs, 1);
  85. if (jobs > maxnjobs)
  86. maxnjobs = jobs;
  87. }
  88. _starpu_cg_list_init(&job->job_successors);
  89. STARPU_PTHREAD_MUTEX_INIT(&job->sync_mutex, NULL);
  90. STARPU_PTHREAD_COND_INIT(&job->sync_cond, NULL);
  91. /* By default we have sequential tasks */
  92. job->task_size = 1;
  93. if (task->use_tag)
  94. _starpu_tag_declare(task->tag_id, job);
  95. if (_starpu_graph_record)
  96. _starpu_graph_add_job(job);
  97. _STARPU_LOG_OUT();
  98. return job;
  99. }
  100. void _starpu_job_destroy(struct _starpu_job *j)
  101. {
  102. /* Wait for any code that was still working on the job (and was
  103. * probably our waker) */
  104. STARPU_PTHREAD_MUTEX_LOCK(&j->sync_mutex);
  105. STARPU_PTHREAD_MUTEX_UNLOCK(&j->sync_mutex);
  106. STARPU_PTHREAD_COND_DESTROY(&j->sync_cond);
  107. STARPU_PTHREAD_MUTEX_DESTROY(&j->sync_mutex);
  108. if (j->task_size > 1)
  109. {
  110. STARPU_PTHREAD_BARRIER_DESTROY(&j->before_work_barrier);
  111. STARPU_PTHREAD_BARRIER_DESTROY(&j->after_work_barrier);
  112. STARPU_ASSERT(j->after_work_busy_barrier == 0);
  113. }
  114. _starpu_cg_list_deinit(&j->job_successors);
  115. if (j->dyn_ordered_buffers)
  116. {
  117. free(j->dyn_ordered_buffers);
  118. j->dyn_ordered_buffers = NULL;
  119. }
  120. if (j->dyn_dep_slots)
  121. {
  122. free(j->dyn_dep_slots);
  123. j->dyn_dep_slots = NULL;
  124. }
  125. if (_starpu_graph_record && j->graph_node)
  126. _starpu_graph_drop_job(j);
  127. if (max_memory_use)
  128. (void) STARPU_ATOMIC_ADDL(&njobs, -1);
  129. free(j);
  130. }
  131. int _starpu_job_finished(struct _starpu_job *j)
  132. {
  133. int ret;
  134. STARPU_PTHREAD_MUTEX_LOCK(&j->sync_mutex);
  135. ret = j->terminated == 2;
  136. STARPU_PTHREAD_MUTEX_UNLOCK(&j->sync_mutex);
  137. return ret;
  138. }
  139. void _starpu_wait_job(struct _starpu_job *j)
  140. {
  141. STARPU_ASSERT(j->task);
  142. STARPU_ASSERT(!j->task->detach);
  143. _STARPU_LOG_IN();
  144. STARPU_PTHREAD_MUTEX_LOCK(&j->sync_mutex);
  145. /* We wait for the flag to have a value of 2 which means that both the
  146. * codelet's implementation and its callback have been executed. That
  147. * way, _starpu_wait_job won't return until the entire task was really
  148. * executed (so that we cannot destroy the task while it is still being
  149. * manipulated by the driver). */
  150. while (j->terminated != 2)
  151. {
  152. STARPU_PTHREAD_COND_WAIT(&j->sync_cond, &j->sync_mutex);
  153. }
  154. STARPU_PTHREAD_MUTEX_UNLOCK(&j->sync_mutex);
  155. _STARPU_LOG_OUT();
  156. }
  157. #ifdef STARPU_OPENMP
  158. int _starpu_test_job_termination(struct _starpu_job *j)
  159. {
  160. STARPU_ASSERT(j->task);
  161. STARPU_ASSERT(!j->task->detach);
  162. /* Disable Helgrind race complaint, since we really just want to poll j->terminated */
  163. if (STARPU_RUNNING_ON_VALGRIND)
  164. {
  165. int v = STARPU_PTHREAD_MUTEX_TRYLOCK(&j->sync_mutex);
  166. if (v != EBUSY)
  167. {
  168. STARPU_ASSERT(v == 0);
  169. int ret = (j->terminated == 2);
  170. STARPU_PTHREAD_MUTEX_UNLOCK(&j->sync_mutex);
  171. return ret;
  172. }
  173. else
  174. {
  175. return 0;
  176. }
  177. }
  178. else
  179. {
  180. STARPU_SYNCHRONIZE();
  181. return j->terminated == 2;
  182. }
  183. }
  184. void _starpu_job_prepare_for_continuation_ext(struct _starpu_job *j, unsigned continuation_resubmit,
  185. void (*continuation_callback_on_sleep)(void *arg), void *continuation_callback_on_sleep_arg)
  186. {
  187. STARPU_ASSERT(!j->continuation);
  188. /* continuation are not supported for parallel tasks for now */
  189. STARPU_ASSERT(j->task_size == 1);
  190. j->continuation = 1;
  191. j->continuation_resubmit = continuation_resubmit;
  192. j->continuation_callback_on_sleep = continuation_callback_on_sleep;
  193. j->continuation_callback_on_sleep_arg = continuation_callback_on_sleep_arg;
  194. j->job_successors.ndeps = 0;
  195. }
  196. /* Prepare a currently running job for accepting a new set of
  197. * dependencies in anticipation of becoming a continuation. */
  198. void _starpu_job_prepare_for_continuation(struct _starpu_job *j)
  199. {
  200. _starpu_job_prepare_for_continuation_ext(j, 1, NULL, NULL);
  201. }
  202. void _starpu_job_set_omp_cleanup_callback(struct _starpu_job *j,
  203. void (*omp_cleanup_callback)(void *arg), void *omp_cleanup_callback_arg)
  204. {
  205. j->omp_cleanup_callback = omp_cleanup_callback;
  206. j->omp_cleanup_callback_arg = omp_cleanup_callback_arg;
  207. }
  208. #endif
  209. void _starpu_handle_job_submission(struct _starpu_job *j)
  210. {
  211. /* Need to atomically set submitted to 1 and check dependencies, since
  212. * this is concucrent with _starpu_notify_cg */
  213. j->terminated = 0;
  214. if (!j->submitted)
  215. j->submitted = 1;
  216. else
  217. j->submitted = 2;
  218. #ifdef STARPU_DEBUG
  219. STARPU_PTHREAD_MUTEX_LOCK(&all_jobs_list_mutex);
  220. _starpu_job_multilist_push_back_all_submitted(&all_jobs_list, j);
  221. STARPU_PTHREAD_MUTEX_UNLOCK(&all_jobs_list_mutex);
  222. #endif
  223. }
  224. void starpu_task_end_dep_release(struct starpu_task *t)
  225. {
  226. struct _starpu_job *j = _starpu_get_job_associated_to_task(t);
  227. _starpu_handle_job_termination(j);
  228. }
  229. void starpu_task_end_dep_add(struct starpu_task *t, int nb_deps)
  230. {
  231. struct _starpu_job *j = _starpu_get_job_associated_to_task(t);
  232. STARPU_PTHREAD_MUTEX_LOCK(&j->sync_mutex);
  233. t->nb_termination_call_required += nb_deps;
  234. STARPU_PTHREAD_MUTEX_UNLOCK(&j->sync_mutex);
  235. }
  236. void _starpu_handle_job_termination(struct _starpu_job *j)
  237. {
  238. if (j->task->nb_termination_call_required != 0)
  239. {
  240. STARPU_PTHREAD_MUTEX_LOCK(&j->sync_mutex);
  241. int nb = j->task->nb_termination_call_required;
  242. j->task->nb_termination_call_required -= 1;
  243. STARPU_PTHREAD_MUTEX_UNLOCK(&j->sync_mutex);
  244. if (nb != 0) return;
  245. }
  246. struct starpu_task *task = j->task;
  247. struct starpu_task *end_rdep = NULL;
  248. unsigned sched_ctx = task->sched_ctx;
  249. double flops = task->flops;
  250. const unsigned continuation =
  251. #ifdef STARPU_OPENMP
  252. j->continuation
  253. #else
  254. 0
  255. #endif
  256. ;
  257. #ifdef STARPU_DEBUG
  258. STARPU_PTHREAD_MUTEX_LOCK(&all_jobs_list_mutex);
  259. _starpu_job_multilist_erase_all_submitted(&all_jobs_list, j);
  260. STARPU_PTHREAD_MUTEX_UNLOCK(&all_jobs_list_mutex);
  261. #endif
  262. STARPU_PTHREAD_MUTEX_LOCK(&j->sync_mutex);
  263. STARPU_ASSERT(task->status == STARPU_TASK_RUNNING);
  264. #ifdef STARPU_OPENMP
  265. if (continuation)
  266. {
  267. task->status = STARPU_TASK_STOPPED;
  268. }
  269. else
  270. #endif
  271. {
  272. task->status = STARPU_TASK_FINISHED;
  273. /* We must have set the j->terminated flag early, so that it is
  274. * possible to express task dependencies within the callback
  275. * function. A value of 1 means that the codelet was executed but that
  276. * the callback is not done yet. */
  277. j->terminated = 1;
  278. end_rdep = j->end_rdep;
  279. }
  280. STARPU_PTHREAD_MUTEX_UNLOCK(&j->sync_mutex);
  281. #ifdef STARPU_USE_SC_HYPERVISOR
  282. size_t data_size = 0;
  283. #endif //STARPU_USE_SC_HYPERVISOR
  284. /* We release handle reference count */
  285. if (task->cl && !continuation)
  286. {
  287. unsigned i;
  288. unsigned nbuffers = STARPU_TASK_GET_NBUFFERS(task);
  289. #ifdef STARPU_USE_SC_HYPERVISOR
  290. for(i = 0; i < nbuffers; i++)
  291. {
  292. starpu_data_handle_t handle = STARPU_TASK_GET_HANDLE(task, i);
  293. if (handle != NULL)
  294. data_size += _starpu_data_get_size(handle);
  295. }
  296. #endif //STARPU_USE_SC_HYPERVISOR
  297. for (i = 0; i < nbuffers; i++)
  298. {
  299. starpu_data_handle_t handle = STARPU_TASK_GET_HANDLE(task, i);
  300. _starpu_spin_lock(&handle->header_lock);
  301. handle->busy_count--;
  302. if (!_starpu_data_check_not_busy(handle))
  303. _starpu_spin_unlock(&handle->header_lock);
  304. }
  305. }
  306. /* If this is a continuation, we do not release task dependencies now.
  307. * Task dependencies will be released only when the continued task
  308. * fully completes */
  309. if (!continuation)
  310. {
  311. /* Tell other tasks that we don't exist any more, thus no need for
  312. * implicit dependencies any more. */
  313. _starpu_release_task_enforce_sequential_consistency(j);
  314. }
  315. /* If the job was executed on a combined worker there is no need for the
  316. * scheduler to process it : the task structure doesn't contain any valuable
  317. * data as it's not linked to an actual worker */
  318. /* control task should not execute post_exec_hook */
  319. unsigned nowhere = !task->cl || task->cl->where == STARPU_NOWHERE || task->where == STARPU_NOWHERE;
  320. if(j->task_size == 1 && !nowhere && !j->internal
  321. #ifdef STARPU_OPENMP
  322. /* If this is a continuation, we do not execute the post_exec_hook. The
  323. * post_exec_hook will be run only when the continued task fully
  324. * completes.
  325. *
  326. * Note: If needed, a specific hook could be added to handle stopped
  327. * tasks */
  328. && !continuation
  329. #endif
  330. )
  331. {
  332. _starpu_sched_post_exec_hook(task);
  333. #ifdef STARPU_USE_SC_HYPERVISOR
  334. int workerid = starpu_worker_get_id();
  335. _starpu_sched_ctx_post_exec_task_cb(workerid, task, data_size, j->footprint);
  336. #endif //STARPU_USE_SC_HYPERVISOR
  337. }
  338. /* Remove ourself from the graph before notifying dependencies */
  339. if (_starpu_graph_record)
  340. _starpu_graph_drop_job(j);
  341. /* Task does not have a cl, but has explicit data dependencies, we need
  342. * to tell them that we will not exist any more before notifying the
  343. * tasks waiting for us
  344. *
  345. * For continuations, implicit dependency handles are only released
  346. * when the task fully completes */
  347. if (j->implicit_dep_handle && !continuation)
  348. {
  349. starpu_data_handle_t handle = j->implicit_dep_handle;
  350. _starpu_release_data_enforce_sequential_consistency(j->task, &j->implicit_dep_slot, handle);
  351. /* Release reference taken while setting implicit_dep_handle */
  352. _starpu_spin_lock(&handle->header_lock);
  353. handle->busy_count--;
  354. if (!_starpu_data_check_not_busy(handle))
  355. _starpu_spin_unlock(&handle->header_lock);
  356. }
  357. _STARPU_TRACE_TASK_NAME(j);
  358. /* If this is a continuation, we do not notify task/tag dependencies
  359. * now. Task/tag dependencies will be notified only when the continued
  360. * task fully completes */
  361. if (!continuation)
  362. {
  363. /* in case there are dependencies, wake up the proper tasks */
  364. if (end_rdep)
  365. starpu_task_end_dep_release(end_rdep);
  366. _starpu_notify_dependencies(j);
  367. }
  368. /* If this is a continuation, we do not execute the callback
  369. * now. The callback will be executed only when the continued
  370. * task fully completes */
  371. if (!continuation)
  372. {
  373. /* the callback is executed after the dependencies so that we may remove the tag
  374. * of the task itself */
  375. if (task->callback_func)
  376. {
  377. int profiling = starpu_profiling_status_get();
  378. if (profiling && task->profiling_info)
  379. _starpu_clock_gettime(&task->profiling_info->callback_start_time);
  380. /* so that we can check whether we are doing blocking calls
  381. * within the callback */
  382. _starpu_set_local_worker_status(STATUS_CALLBACK);
  383. /* Perhaps we have nested callbacks (eg. with chains of empty
  384. * tasks). So we store the current task and we will restore it
  385. * later. */
  386. struct starpu_task *current_task = starpu_task_get_current();
  387. _starpu_set_current_task(task);
  388. _STARPU_TRACE_START_CALLBACK(j);
  389. task->callback_func(task->callback_arg);
  390. _STARPU_TRACE_END_CALLBACK(j);
  391. _starpu_set_current_task(current_task);
  392. _starpu_set_local_worker_status(STATUS_UNKNOWN);
  393. if (profiling && task->profiling_info)
  394. _starpu_clock_gettime(&task->profiling_info->callback_end_time);
  395. }
  396. }
  397. /* Note: For now, we keep the TASK_DONE trace event for continuation,
  398. * however we could add a specific event for stopped tasks if needed.
  399. */
  400. _STARPU_TRACE_TASK_DONE(j);
  401. /* NB: we do not save those values before the callback, in case the
  402. * application changes some parameters eventually (eg. a task may not
  403. * be generated if the application is terminated). */
  404. unsigned destroy = task->destroy;
  405. unsigned detach = task->detach;
  406. unsigned regenerate = task->regenerate;
  407. unsigned synchronous = task->synchronous;
  408. /* we do not desallocate the job structure if some is going to
  409. * wait after the task */
  410. STARPU_PTHREAD_MUTEX_LOCK(&j->sync_mutex);
  411. if (!continuation)
  412. {
  413. #ifdef STARPU_OPENMP
  414. if (j->omp_cleanup_callback)
  415. {
  416. j->omp_cleanup_callback(j->omp_cleanup_callback_arg);
  417. j->omp_cleanup_callback = NULL;
  418. j->omp_cleanup_callback_arg = NULL;
  419. }
  420. #endif
  421. /* A value of 2 is put to specify that not only the codelet but
  422. * also the callback were executed. */
  423. j->terminated = 2;
  424. }
  425. STARPU_PTHREAD_COND_BROADCAST(&j->sync_cond);
  426. STARPU_AYU_REMOVETASK(j->job_id);
  427. STARPU_PTHREAD_MUTEX_UNLOCK(&j->sync_mutex);
  428. if (detach && !continuation)
  429. {
  430. /* no one is going to synchronize with that task so we release
  431. * the data structures now. In case the job was already locked
  432. * by the caller, it is its responsability to destroy the task.
  433. * */
  434. if (destroy)
  435. _starpu_task_destroy(task);
  436. }
  437. /* A continuation is not much different from a regenerated task. */
  438. if (regenerate || continuation)
  439. {
  440. STARPU_ASSERT_MSG((detach && !destroy && !synchronous)
  441. || continuation
  442. , "Regenerated task must be detached (was %u), and not have detroy=1 (was %u) or synchronous=1 (was %u)", detach, destroy, synchronous);
  443. STARPU_AYU_ADDTASK(j->job_id, j->exclude_from_dag?NULL:task);
  444. {
  445. #ifdef STARPU_OPENMP
  446. unsigned continuation_resubmit = j->continuation_resubmit;
  447. void (*continuation_callback_on_sleep)(void *arg) = j->continuation_callback_on_sleep;
  448. void *continuation_callback_on_sleep_arg = j->continuation_callback_on_sleep_arg;
  449. j->continuation_resubmit = 1;
  450. j->continuation_callback_on_sleep = NULL;
  451. j->continuation_callback_on_sleep_arg = NULL;
  452. if (!continuation || continuation_resubmit)
  453. #endif
  454. {
  455. /* We reuse the same job structure */
  456. task->status = STARPU_TASK_BLOCKED;
  457. int ret = _starpu_submit_job(j, 0);
  458. STARPU_ASSERT(!ret);
  459. }
  460. #ifdef STARPU_OPENMP
  461. if (continuation && continuation_callback_on_sleep != NULL)
  462. {
  463. continuation_callback_on_sleep(continuation_callback_on_sleep_arg);
  464. }
  465. #endif
  466. }
  467. }
  468. _starpu_decrement_nready_tasks_of_sched_ctx(sched_ctx, flops);
  469. _starpu_decrement_nsubmitted_tasks_of_sched_ctx(sched_ctx);
  470. struct _starpu_worker *worker;
  471. worker = _starpu_get_local_worker_key();
  472. if (worker)
  473. {
  474. STARPU_PTHREAD_MUTEX_LOCK_SCHED(&worker->sched_mutex);
  475. if(worker->removed_from_ctx[sched_ctx] == 1 && worker->shares_tasks_lists[sched_ctx] == 1)
  476. {
  477. _starpu_worker_gets_out_of_ctx(sched_ctx, worker);
  478. worker->removed_from_ctx[sched_ctx] = 0;
  479. }
  480. STARPU_PTHREAD_MUTEX_UNLOCK_SCHED(&worker->sched_mutex);
  481. }
  482. }
  483. /* This function is called when a new task is submitted to StarPU
  484. * it returns 1 if the tag deps are not fulfilled, 0 otherwise */
  485. static unsigned _starpu_not_all_tag_deps_are_fulfilled(struct _starpu_job *j)
  486. {
  487. unsigned ret;
  488. if (!j->task->use_tag)
  489. {
  490. /* this task does not use tags, so we can go on */
  491. return 0;
  492. }
  493. struct _starpu_tag *tag = j->tag;
  494. struct _starpu_cg_list *tag_successors = &tag->tag_successors;
  495. _starpu_spin_lock(&tag->lock);
  496. STARPU_ASSERT_MSG(tag->is_assigned == 1 || !tag_successors->ndeps, "a tag can be assigned only one task to wake (%llu had %u assigned tasks, and %u successors)", (unsigned long long) tag->id, tag->is_assigned, tag_successors->ndeps);
  497. if (tag_successors->ndeps != tag_successors->ndeps_completed)
  498. {
  499. tag->state = STARPU_BLOCKED;
  500. j->task->status = STARPU_TASK_BLOCKED_ON_TAG;
  501. ret = 1;
  502. }
  503. else
  504. {
  505. /* existing deps (if any) are fulfilled */
  506. /* If the same tag is being signaled by several tasks, do not
  507. * clear a DONE state. If it's the same job submitted several
  508. * times with the same tag, we have to do it */
  509. if (j->submitted == 2 || tag->state != STARPU_DONE)
  510. tag->state = STARPU_READY;
  511. /* already prepare for next run */
  512. tag_successors->ndeps_completed = 0;
  513. ret = 0;
  514. }
  515. _starpu_spin_unlock(&tag->lock);
  516. return ret;
  517. }
  518. static unsigned _starpu_not_all_task_deps_are_fulfilled(struct _starpu_job *j)
  519. {
  520. unsigned ret;
  521. struct _starpu_cg_list *job_successors = &j->job_successors;
  522. if (!j->submitted || (job_successors->ndeps != job_successors->ndeps_completed))
  523. {
  524. STARPU_ASSERT(j->task->status == STARPU_TASK_BLOCKED || j->task->status == STARPU_TASK_BLOCKED_ON_TAG);
  525. j->task->status = STARPU_TASK_BLOCKED_ON_TASK;
  526. ret = 1;
  527. }
  528. else
  529. {
  530. /* existing deps (if any) are fulfilled */
  531. /* already prepare for next run */
  532. job_successors->ndeps_completed = 0;
  533. ret = 0;
  534. }
  535. return ret;
  536. }
  537. /*
  538. * In order, we enforce tag, task and data dependencies. The task is
  539. * passed to the scheduler only once all these constraints are fulfilled.
  540. *
  541. * The job mutex has to be taken for atomicity with task submission, and
  542. * is released here.
  543. */
  544. unsigned _starpu_enforce_deps_and_schedule(struct _starpu_job *j)
  545. {
  546. unsigned ret;
  547. _STARPU_LOG_IN();
  548. /* enfore tag dependencies */
  549. if (_starpu_not_all_tag_deps_are_fulfilled(j))
  550. {
  551. STARPU_PTHREAD_MUTEX_UNLOCK(&j->sync_mutex);
  552. _STARPU_LOG_OUT_TAG("not_all_tag_deps_are_fulfilled");
  553. return 0;
  554. }
  555. /* enfore task dependencies */
  556. if (_starpu_not_all_task_deps_are_fulfilled(j))
  557. {
  558. STARPU_PTHREAD_MUTEX_UNLOCK(&j->sync_mutex);
  559. _STARPU_LOG_OUT_TAG("not_all_task_deps_are_fulfilled");
  560. return 0;
  561. }
  562. STARPU_PTHREAD_MUTEX_UNLOCK(&j->sync_mutex);
  563. /* enforce data dependencies */
  564. if (_starpu_submit_job_enforce_data_deps(j))
  565. {
  566. _STARPU_LOG_OUT_TAG("enforce_data_deps");
  567. return 0;
  568. }
  569. ret = _starpu_push_task(j);
  570. _STARPU_LOG_OUT();
  571. return ret;
  572. }
  573. /* Tag deps are already fulfilled */
  574. unsigned _starpu_enforce_deps_starting_from_task(struct _starpu_job *j)
  575. {
  576. unsigned ret;
  577. /* enfore task dependencies */
  578. if (_starpu_not_all_task_deps_are_fulfilled(j))
  579. {
  580. STARPU_PTHREAD_MUTEX_UNLOCK(&j->sync_mutex);
  581. return 0;
  582. }
  583. STARPU_PTHREAD_MUTEX_UNLOCK(&j->sync_mutex);
  584. /* enforce data dependencies */
  585. if (_starpu_submit_job_enforce_data_deps(j))
  586. return 0;
  587. ret = _starpu_push_task(j);
  588. return ret;
  589. }
  590. #ifdef STARPU_OPENMP
  591. /* When waking up a continuation, we only enforce new task dependencies */
  592. unsigned _starpu_reenforce_task_deps_and_schedule(struct _starpu_job *j)
  593. {
  594. unsigned ret;
  595. _STARPU_LOG_IN();
  596. STARPU_ASSERT(j->discontinuous);
  597. /* enfore task dependencies */
  598. if (_starpu_not_all_task_deps_are_fulfilled(j))
  599. {
  600. STARPU_PTHREAD_MUTEX_UNLOCK(&j->sync_mutex);
  601. _STARPU_LOG_OUT_TAG("not_all_task_deps_are_fulfilled");
  602. return 0;
  603. }
  604. STARPU_PTHREAD_MUTEX_UNLOCK(&j->sync_mutex);
  605. ret = _starpu_push_task(j);
  606. _STARPU_LOG_OUT();
  607. return ret;
  608. }
  609. #endif
  610. unsigned _starpu_take_deps_and_schedule(struct _starpu_job *j)
  611. {
  612. unsigned ret;
  613. STARPU_PTHREAD_MUTEX_UNLOCK(&j->sync_mutex);
  614. /* Take references */
  615. _starpu_submit_job_take_data_deps(j);
  616. /* And immediately push task */
  617. ret = _starpu_push_task(j);
  618. return ret;
  619. }
  620. /* This is called when a tag or task dependency is to be released. */
  621. void _starpu_enforce_deps_notify_job_ready_soon(struct _starpu_job *j, _starpu_notify_job_start_data *data, int tag)
  622. {
  623. if (!j->submitted)
  624. /* It's not even submitted actually */
  625. return;
  626. struct _starpu_cg_list *job_successors = &j->job_successors;
  627. /* tag is 1 when we got woken up by a tag dependency about to be
  628. * released, and thus we have to check the exact numbner of
  629. * dependencies. Otherwise it's a task dependency which is about to be
  630. * released. */
  631. if (job_successors->ndeps != job_successors->ndeps_completed + 1 - tag)
  632. /* There are still other dependencies */
  633. return;
  634. _starpu_enforce_data_deps_notify_job_ready_soon(j, data);
  635. }
  636. /* Ordered tasks are simply recorded as they arrive in the local_ordered_tasks
  637. * ring buffer, indexed by order, and pulled from its head. */
  638. /* TODO: replace with perhaps a heap */
  639. /* This function must be called with worker->sched_mutex taken */
  640. struct starpu_task *_starpu_pop_local_task(struct _starpu_worker *worker)
  641. {
  642. struct starpu_task *task = NULL;
  643. if (worker->local_ordered_tasks_size)
  644. {
  645. task = worker->local_ordered_tasks[worker->current_ordered_task];
  646. if (task)
  647. {
  648. worker->local_ordered_tasks[worker->current_ordered_task] = NULL;
  649. STARPU_ASSERT(task->workerorder == worker->current_ordered_task_order);
  650. /* Next ordered task is there, return it */
  651. worker->current_ordered_task = (worker->current_ordered_task + 1) % worker->local_ordered_tasks_size;
  652. worker->current_ordered_task_order++;
  653. _starpu_pop_task_end(task);
  654. return task;
  655. }
  656. }
  657. if (!starpu_task_list_empty(&worker->local_tasks))
  658. task = starpu_task_list_pop_front(&worker->local_tasks);
  659. _starpu_pop_task_end(task);
  660. return task;
  661. }
  662. int _starpu_push_local_task(struct _starpu_worker *worker, struct starpu_task *task, int prio)
  663. {
  664. /* Check that the worker is able to execute the task ! */
  665. STARPU_ASSERT(task && task->cl);
  666. if (STARPU_UNLIKELY(!(worker->worker_mask & task->where)))
  667. return -ENODEV;
  668. starpu_worker_lock(worker->workerid);
  669. if (task->execute_on_a_specific_worker && task->workerorder)
  670. {
  671. STARPU_ASSERT_MSG(task->workerorder >= worker->current_ordered_task_order, "worker order values must not have duplicates (%u pushed to worker %d, but %u already passed)", task->workerorder, worker->workerid, worker->current_ordered_task_order);
  672. /* Put it in the ordered task ring */
  673. unsigned needed = task->workerorder - worker->current_ordered_task_order + 1;
  674. if (worker->local_ordered_tasks_size < needed)
  675. {
  676. /* Increase the size */
  677. unsigned alloc = worker->local_ordered_tasks_size;
  678. struct starpu_task **new;
  679. if (!alloc)
  680. alloc = 1;
  681. while (alloc < needed)
  682. alloc *= 2;
  683. _STARPU_MALLOC(new, alloc * sizeof(*new));
  684. if (worker->local_ordered_tasks_size)
  685. {
  686. /* Put existing tasks at the beginning of the new ring */
  687. unsigned copied = worker->local_ordered_tasks_size - worker->current_ordered_task;
  688. memcpy(new, &worker->local_ordered_tasks[worker->current_ordered_task], copied * sizeof(*new));
  689. memcpy(new + copied, worker->local_ordered_tasks, (worker->local_ordered_tasks_size - copied) * sizeof(*new));
  690. }
  691. memset(new + worker->local_ordered_tasks_size, 0, (alloc - worker->local_ordered_tasks_size) * sizeof(*new));
  692. free(worker->local_ordered_tasks);
  693. worker->local_ordered_tasks = new;
  694. worker->local_ordered_tasks_size = alloc;
  695. worker->current_ordered_task = 0;
  696. }
  697. worker->local_ordered_tasks[(worker->current_ordered_task + task->workerorder - worker->current_ordered_task_order) % worker->local_ordered_tasks_size] = task;
  698. }
  699. else
  700. {
  701. #ifdef STARPU_DEVEL
  702. #warning FIXME use a prio_list
  703. #endif
  704. if (prio)
  705. starpu_task_list_push_front(&worker->local_tasks, task);
  706. else
  707. starpu_task_list_push_back(&worker->local_tasks, task);
  708. }
  709. starpu_wake_worker_locked(worker->workerid);
  710. starpu_push_task_end(task);
  711. starpu_worker_unlock(worker->workerid);
  712. return 0;
  713. }