jobs.c 26 KB

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