jobs.c 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760
  1. /* StarPU --- Runtime system for heterogeneous multicore architectures.
  2. *
  3. * Copyright (C) 2009-2017 Université de Bordeaux
  4. * Copyright (C) 2010, 2011, 2012, 2013, 2014, 2015, 2016, 2017 CNRS
  5. * Copyright (C) 2011 Télécom-SudParis
  6. * Copyright (C) 2011, 2014, 2016-2017 INRIA
  7. *
  8. * StarPU is free software; you can redistribute it and/or modify
  9. * it under the terms of the GNU Lesser General Public License as published by
  10. * the Free Software Foundation; either version 2.1 of the License, or (at
  11. * your option) any later version.
  12. *
  13. * StarPU is distributed in the hope that it will be useful, but
  14. * WITHOUT ANY WARRANTY; without even the implied warranty of
  15. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
  16. *
  17. * See the GNU Lesser General Public License in COPYING.LGPL for more details.
  18. */
  19. #include <starpu.h>
  20. #include <core/jobs.h>
  21. #include <core/task.h>
  22. #include <core/workers.h>
  23. #include <core/dependencies/data_concurrency.h>
  24. #include <common/config.h>
  25. #include <common/utils.h>
  26. #include <common/graph.h>
  27. #include <profiling/profiling.h>
  28. #include <profiling/bound.h>
  29. #include <starpu_top.h>
  30. #include <top/starpu_top_core.h>
  31. #include <core/debug.h>
  32. /* we need to identify each task to generate the DAG. */
  33. static unsigned long job_cnt = 0;
  34. static int max_memory_use;
  35. static int 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. #ifdef STARPU_DEBUG
  45. _starpu_job_multilist_init_all_submitted(&all_jobs_list);
  46. #endif
  47. }
  48. void _starpu_job_fini(void)
  49. {
  50. if (max_memory_use)
  51. {
  52. _STARPU_DISP("Memory used for %d tasks: %lu MiB\n", maxnjobs, (unsigned long) (maxnjobs * (sizeof(struct starpu_task) + sizeof(struct _starpu_job))) >> 20);
  53. STARPU_ASSERT_MSG(njobs == 0, "Some tasks have not been cleaned, did you forget to call starpu_task_destroy or starpu_task_clean?");
  54. }
  55. }
  56. void _starpu_exclude_task_from_dag(struct starpu_task *task)
  57. {
  58. struct _starpu_job *j = _starpu_get_job_associated_to_task(task);
  59. j->exclude_from_dag = 1;
  60. }
  61. /* create an internal struct _starpu_job structure to encapsulate the task */
  62. struct _starpu_job* STARPU_ATTRIBUTE_MALLOC _starpu_job_create(struct starpu_task *task)
  63. {
  64. struct _starpu_job *job;
  65. _STARPU_LOG_IN();
  66. _STARPU_MALLOC(job, sizeof(*job));
  67. /* As most of the fields must be initialized at NULL, let's put 0
  68. * everywhere */
  69. memset(job, 0, sizeof(*job));
  70. if (task->dyn_handles)
  71. {
  72. _STARPU_MALLOC(job->dyn_ordered_buffers, STARPU_TASK_GET_NBUFFERS(task) * sizeof(job->dyn_ordered_buffers[0]));
  73. _STARPU_CALLOC(job->dyn_dep_slots, STARPU_TASK_GET_NBUFFERS(task), sizeof(job->dyn_dep_slots[0]));
  74. }
  75. job->task = task;
  76. #ifndef STARPU_USE_FXT
  77. if (_starpu_bound_recording || _starpu_top_status_get() ||
  78. _starpu_task_break_on_push != -1 || _starpu_task_break_on_sched != -1 || _starpu_task_break_on_pop != -1 || _starpu_task_break_on_exec != -1
  79. || STARPU_AYU_EVENT)
  80. #endif
  81. {
  82. job->job_id = STARPU_ATOMIC_ADDL(&job_cnt, 1);
  83. STARPU_AYU_ADDTASK(job->job_id, task);
  84. }
  85. if (max_memory_use)
  86. {
  87. int jobs = STARPU_ATOMIC_ADDL(&njobs, 1);
  88. if (jobs > maxnjobs)
  89. maxnjobs = jobs;
  90. }
  91. _starpu_cg_list_init(&job->job_successors);
  92. STARPU_PTHREAD_MUTEX_INIT(&job->sync_mutex, NULL);
  93. STARPU_PTHREAD_COND_INIT(&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_handle_job_termination(struct _starpu_job *j)
  228. {
  229. struct starpu_task *task = j->task;
  230. unsigned sched_ctx = task->sched_ctx;
  231. double flops = task->flops;
  232. const unsigned continuation =
  233. #ifdef STARPU_OPENMP
  234. j->continuation
  235. #else
  236. 0
  237. #endif
  238. ;
  239. #ifdef STARPU_DEBUG
  240. STARPU_PTHREAD_MUTEX_LOCK(&all_jobs_list_mutex);
  241. _starpu_job_multilist_erase_all_submitted(&all_jobs_list, j);
  242. STARPU_PTHREAD_MUTEX_UNLOCK(&all_jobs_list_mutex);
  243. #endif
  244. STARPU_PTHREAD_MUTEX_LOCK(&j->sync_mutex);
  245. #ifdef STARPU_OPENMP
  246. if (continuation)
  247. {
  248. task->status = STARPU_TASK_STOPPED;
  249. }
  250. else
  251. #endif
  252. {
  253. task->status = STARPU_TASK_FINISHED;
  254. /* We must have set the j->terminated flag early, so that it is
  255. * possible to express task dependencies within the callback
  256. * function. A value of 1 means that the codelet was executed but that
  257. * the callback is not done yet. */
  258. j->terminated = 1;
  259. }
  260. STARPU_PTHREAD_MUTEX_UNLOCK(&j->sync_mutex);
  261. #ifdef STARPU_USE_SC_HYPERVISOR
  262. size_t data_size = 0;
  263. #endif //STARPU_USE_SC_HYPERVISOR
  264. /* We release handle reference count */
  265. if (task->cl && !continuation)
  266. {
  267. unsigned i;
  268. unsigned nbuffers = STARPU_TASK_GET_NBUFFERS(task);
  269. #ifdef STARPU_USE_SC_HYPERVISOR
  270. for(i = 0; i < nbuffers; i++)
  271. {
  272. starpu_data_handle_t handle = STARPU_TASK_GET_HANDLE(task, i);
  273. if (handle != NULL)
  274. data_size += _starpu_data_get_size(handle);
  275. }
  276. #endif //STARPU_USE_SC_HYPERVISOR
  277. for (i = 0; i < nbuffers; i++)
  278. {
  279. starpu_data_handle_t handle = STARPU_TASK_GET_HANDLE(task, i);
  280. _starpu_spin_lock(&handle->header_lock);
  281. handle->busy_count--;
  282. if (!_starpu_data_check_not_busy(handle))
  283. _starpu_spin_unlock(&handle->header_lock);
  284. }
  285. }
  286. /* If this is a continuation, we do not release task dependencies now.
  287. * Task dependencies will be released only when the continued task
  288. * fully completes */
  289. if (!continuation)
  290. {
  291. /* Tell other tasks that we don't exist any more, thus no need for
  292. * implicit dependencies any more. */
  293. _starpu_release_task_enforce_sequential_consistency(j);
  294. }
  295. /* If the job was executed on a combined worker there is no need for the
  296. * scheduler to process it : the task structure doesn't contain any valuable
  297. * data as it's not linked to an actual worker */
  298. /* control task should not execute post_exec_hook */
  299. unsigned nowhere = !task->cl || task->cl->where == STARPU_NOWHERE || task->where == STARPU_NOWHERE;
  300. if(j->task_size == 1 && !nowhere && !j->internal
  301. #ifdef STARPU_OPENMP
  302. /* If this is a continuation, we do not execute the post_exec_hook. The
  303. * post_exec_hook will be run only when the continued task fully
  304. * completes.
  305. *
  306. * Note: If needed, a specific hook could be added to handle stopped
  307. * tasks */
  308. && !continuation
  309. #endif
  310. )
  311. {
  312. _starpu_sched_post_exec_hook(task);
  313. #ifdef STARPU_USE_SC_HYPERVISOR
  314. int workerid = starpu_worker_get_id();
  315. _starpu_sched_ctx_post_exec_task_cb(workerid, task, data_size, j->footprint);
  316. #endif //STARPU_USE_SC_HYPERVISOR
  317. }
  318. /* Remove ourself from the graph before notifying dependencies */
  319. if (_starpu_graph_record)
  320. _starpu_graph_drop_job(j);
  321. /* Task does not have a cl, but has explicit data dependencies, we need
  322. * to tell them that we will not exist any more before notifying the
  323. * tasks waiting for us
  324. *
  325. * For continuations, implicit dependency handles are only released
  326. * when the task fully completes */
  327. if (j->implicit_dep_handle && !continuation)
  328. {
  329. starpu_data_handle_t handle = j->implicit_dep_handle;
  330. _starpu_release_data_enforce_sequential_consistency(j->task, &j->implicit_dep_slot, handle);
  331. /* Release reference taken while setting implicit_dep_handle */
  332. _starpu_spin_lock(&handle->header_lock);
  333. handle->busy_count--;
  334. if (!_starpu_data_check_not_busy(handle))
  335. _starpu_spin_unlock(&handle->header_lock);
  336. }
  337. /* If this is a continuation, we do not notify task/tag dependencies
  338. * now. Task/tag dependencies will be notified only when the continued
  339. * task fully completes */
  340. if (!continuation)
  341. {
  342. /* in case there are dependencies, wake up the proper tasks */
  343. _starpu_notify_dependencies(j);
  344. }
  345. /* If this is a continuation, we do not execute the callback
  346. * now. The callback will be executed only when the continued
  347. * task fully completes */
  348. if (!continuation)
  349. {
  350. /* the callback is executed after the dependencies so that we may remove the tag
  351. * of the task itself */
  352. if (task->callback_func)
  353. {
  354. int profiling = starpu_profiling_status_get();
  355. if (profiling && task->profiling_info)
  356. _starpu_clock_gettime(&task->profiling_info->callback_start_time);
  357. /* so that we can check whether we are doing blocking calls
  358. * within the callback */
  359. _starpu_set_local_worker_status(STATUS_CALLBACK);
  360. /* Perhaps we have nested callbacks (eg. with chains of empty
  361. * tasks). So we store the current task and we will restore it
  362. * later. */
  363. struct starpu_task *current_task = starpu_task_get_current();
  364. _starpu_set_current_task(task);
  365. _STARPU_TRACE_START_CALLBACK(j);
  366. task->callback_func(task->callback_arg);
  367. _STARPU_TRACE_END_CALLBACK(j);
  368. _starpu_set_current_task(current_task);
  369. _starpu_set_local_worker_status(STATUS_UNKNOWN);
  370. if (profiling && task->profiling_info)
  371. _starpu_clock_gettime(&task->profiling_info->callback_end_time);
  372. }
  373. }
  374. /* Note: For now, we keep the TASK_DONE trace event for continuation,
  375. * however we could add a specific event for stopped tasks if needed.
  376. */
  377. _STARPU_TRACE_TASK_DONE(j);
  378. /* NB: we do not save those values before the callback, in case the
  379. * application changes some parameters eventually (eg. a task may not
  380. * be generated if the application is terminated). */
  381. unsigned destroy = task->destroy;
  382. unsigned detach = task->detach;
  383. unsigned regenerate = task->regenerate;
  384. /* we do not desallocate the job structure if some is going to
  385. * wait after the task */
  386. STARPU_PTHREAD_MUTEX_LOCK(&j->sync_mutex);
  387. if (!continuation)
  388. {
  389. #ifdef STARPU_OPENMP
  390. if (j->omp_cleanup_callback)
  391. {
  392. j->omp_cleanup_callback(j->omp_cleanup_callback_arg);
  393. j->omp_cleanup_callback = NULL;
  394. j->omp_cleanup_callback_arg = NULL;
  395. }
  396. #endif
  397. /* A value of 2 is put to specify that not only the codelet but
  398. * also the callback were executed. */
  399. j->terminated = 2;
  400. }
  401. STARPU_PTHREAD_COND_BROADCAST(&j->sync_cond);
  402. STARPU_AYU_REMOVETASK(j->job_id);
  403. STARPU_PTHREAD_MUTEX_UNLOCK(&j->sync_mutex);
  404. if (detach && !continuation)
  405. {
  406. /* no one is going to synchronize with that task so we release
  407. * the data structures now. In case the job was already locked
  408. * by the caller, it is its responsability to destroy the task.
  409. * */
  410. if (destroy)
  411. _starpu_task_destroy(task);
  412. }
  413. /* A continuation is not much different from a regenerated task. */
  414. if (regenerate || continuation)
  415. {
  416. STARPU_ASSERT_MSG((detach && !destroy && !task->synchronous)
  417. || continuation
  418. , "Regenerated task must be detached (was %u), and not have detroy=1 (was %u) or synchronous=1 (was %u)", detach, destroy, task->synchronous);
  419. STARPU_AYU_ADDTASK(j->job_id, j->exclude_from_dag?NULL:task);
  420. {
  421. #ifdef STARPU_OPENMP
  422. unsigned continuation_resubmit = j->continuation_resubmit;
  423. void (*continuation_callback_on_sleep)(void *arg) = j->continuation_callback_on_sleep;
  424. void *continuation_callback_on_sleep_arg = j->continuation_callback_on_sleep_arg;
  425. j->continuation_resubmit = 1;
  426. j->continuation_callback_on_sleep = NULL;
  427. j->continuation_callback_on_sleep_arg = NULL;
  428. if (!continuation || continuation_resubmit)
  429. #endif
  430. {
  431. /* We reuse the same job structure */
  432. int ret = _starpu_submit_job(j);
  433. STARPU_ASSERT(!ret);
  434. }
  435. #ifdef STARPU_OPENMP
  436. if (continuation && continuation_callback_on_sleep != NULL)
  437. {
  438. continuation_callback_on_sleep(continuation_callback_on_sleep_arg);
  439. }
  440. #endif
  441. }
  442. }
  443. _starpu_decrement_nready_tasks_of_sched_ctx(sched_ctx, flops);
  444. _starpu_decrement_nsubmitted_tasks_of_sched_ctx(sched_ctx);
  445. struct _starpu_worker *worker;
  446. worker = _starpu_get_local_worker_key();
  447. if (worker)
  448. {
  449. STARPU_PTHREAD_MUTEX_LOCK_SCHED(&worker->sched_mutex);
  450. if(worker->removed_from_ctx[sched_ctx] == 1 && worker->shares_tasks_lists[sched_ctx] == 1)
  451. {
  452. _starpu_worker_gets_out_of_ctx(sched_ctx, worker);
  453. worker->removed_from_ctx[sched_ctx] = 0;
  454. }
  455. STARPU_PTHREAD_MUTEX_UNLOCK_SCHED(&worker->sched_mutex);
  456. }
  457. }
  458. /* This function is called when a new task is submitted to StarPU
  459. * it returns 1 if the tag deps are not fulfilled, 0 otherwise */
  460. static unsigned _starpu_not_all_tag_deps_are_fulfilled(struct _starpu_job *j)
  461. {
  462. unsigned ret;
  463. if (!j->task->use_tag)
  464. {
  465. /* this task does not use tags, so we can go on */
  466. return 0;
  467. }
  468. struct _starpu_tag *tag = j->tag;
  469. struct _starpu_cg_list *tag_successors = &tag->tag_successors;
  470. _starpu_spin_lock(&tag->lock);
  471. 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);
  472. if (tag_successors->ndeps != tag_successors->ndeps_completed)
  473. {
  474. tag->state = STARPU_BLOCKED;
  475. j->task->status = STARPU_TASK_BLOCKED_ON_TAG;
  476. ret = 1;
  477. }
  478. else
  479. {
  480. /* existing deps (if any) are fulfilled */
  481. /* If the same tag is being signaled by several tasks, do not
  482. * clear a DONE state. If it's the same job submitted several
  483. * times with the same tag, we have to do it */
  484. if (j->submitted == 2 || tag->state != STARPU_DONE)
  485. tag->state = STARPU_READY;
  486. /* already prepare for next run */
  487. tag_successors->ndeps_completed = 0;
  488. ret = 0;
  489. }
  490. _starpu_spin_unlock(&tag->lock);
  491. return ret;
  492. }
  493. static unsigned _starpu_not_all_task_deps_are_fulfilled(struct _starpu_job *j)
  494. {
  495. unsigned ret;
  496. struct _starpu_cg_list *job_successors = &j->job_successors;
  497. if (!j->submitted || (job_successors->ndeps != job_successors->ndeps_completed))
  498. {
  499. j->task->status = STARPU_TASK_BLOCKED_ON_TASK;
  500. ret = 1;
  501. }
  502. else
  503. {
  504. /* existing deps (if any) are fulfilled */
  505. /* already prepare for next run */
  506. job_successors->ndeps_completed = 0;
  507. ret = 0;
  508. }
  509. return ret;
  510. }
  511. /*
  512. * In order, we enforce tag, task and data dependencies. The task is
  513. * passed to the scheduler only once all these constraints are fulfilled.
  514. *
  515. * The job mutex has to be taken for atomicity with task submission, and
  516. * is released here.
  517. */
  518. unsigned _starpu_enforce_deps_and_schedule(struct _starpu_job *j)
  519. {
  520. unsigned ret;
  521. _STARPU_LOG_IN();
  522. /* enfore tag dependencies */
  523. if (_starpu_not_all_tag_deps_are_fulfilled(j))
  524. {
  525. STARPU_PTHREAD_MUTEX_UNLOCK(&j->sync_mutex);
  526. _STARPU_LOG_OUT_TAG("not_all_tag_deps_are_fulfilled");
  527. return 0;
  528. }
  529. /* enfore task dependencies */
  530. if (_starpu_not_all_task_deps_are_fulfilled(j))
  531. {
  532. STARPU_PTHREAD_MUTEX_UNLOCK(&j->sync_mutex);
  533. _STARPU_LOG_OUT_TAG("not_all_task_deps_are_fulfilled");
  534. return 0;
  535. }
  536. STARPU_PTHREAD_MUTEX_UNLOCK(&j->sync_mutex);
  537. /* enforce data dependencies */
  538. if (_starpu_submit_job_enforce_data_deps(j))
  539. {
  540. _STARPU_LOG_OUT_TAG("enforce_data_deps");
  541. return 0;
  542. }
  543. ret = _starpu_push_task(j);
  544. _STARPU_LOG_OUT();
  545. return ret;
  546. }
  547. /* Tag deps are already fulfilled */
  548. unsigned _starpu_enforce_deps_starting_from_task(struct _starpu_job *j)
  549. {
  550. unsigned ret;
  551. /* enfore task dependencies */
  552. if (_starpu_not_all_task_deps_are_fulfilled(j))
  553. {
  554. STARPU_PTHREAD_MUTEX_UNLOCK(&j->sync_mutex);
  555. return 0;
  556. }
  557. STARPU_PTHREAD_MUTEX_UNLOCK(&j->sync_mutex);
  558. /* enforce data dependencies */
  559. if (_starpu_submit_job_enforce_data_deps(j))
  560. return 0;
  561. ret = _starpu_push_task(j);
  562. return ret;
  563. }
  564. #ifdef STARPU_OPENMP
  565. /* When waking up a continuation, we only enforce new task dependencies */
  566. unsigned _starpu_reenforce_task_deps_and_schedule(struct _starpu_job *j)
  567. {
  568. unsigned ret;
  569. _STARPU_LOG_IN();
  570. STARPU_ASSERT(j->discontinuous);
  571. /* enfore task dependencies */
  572. if (_starpu_not_all_task_deps_are_fulfilled(j))
  573. {
  574. STARPU_PTHREAD_MUTEX_UNLOCK(&j->sync_mutex);
  575. _STARPU_LOG_OUT_TAG("not_all_task_deps_are_fulfilled");
  576. return 0;
  577. }
  578. STARPU_PTHREAD_MUTEX_UNLOCK(&j->sync_mutex);
  579. ret = _starpu_push_task(j);
  580. _STARPU_LOG_OUT();
  581. return ret;
  582. }
  583. #endif
  584. /* Ordered tasks are simply recorded as they arrive in the local_ordered_tasks
  585. * ring buffer, indexed by order, and pulled from its head. */
  586. /* TODO: replace with perhaps a heap */
  587. /* This function must be called with worker->sched_mutex taken */
  588. struct starpu_task *_starpu_pop_local_task(struct _starpu_worker *worker)
  589. {
  590. struct starpu_task *task = NULL;
  591. if (worker->local_ordered_tasks_size)
  592. {
  593. task = worker->local_ordered_tasks[worker->current_ordered_task];
  594. if (task)
  595. {
  596. worker->local_ordered_tasks[worker->current_ordered_task] = NULL;
  597. STARPU_ASSERT(task->workerorder == worker->current_ordered_task_order);
  598. /* Next ordered task is there, return it */
  599. worker->current_ordered_task = (worker->current_ordered_task + 1) % worker->local_ordered_tasks_size;
  600. worker->current_ordered_task_order++;
  601. _starpu_pop_task_end(task);
  602. return task;
  603. }
  604. }
  605. if (!starpu_task_list_empty(&worker->local_tasks))
  606. task = starpu_task_list_pop_front(&worker->local_tasks);
  607. _starpu_pop_task_end(task);
  608. return task;
  609. }
  610. int _starpu_push_local_task(struct _starpu_worker *worker, struct starpu_task *task, int prio)
  611. {
  612. /* Check that the worker is able to execute the task ! */
  613. STARPU_ASSERT(task && task->cl);
  614. if (STARPU_UNLIKELY(!(worker->worker_mask & task->where)))
  615. return -ENODEV;
  616. _starpu_worker_lock(worker->workerid);
  617. if (task->execute_on_a_specific_worker && task->workerorder)
  618. {
  619. STARPU_ASSERT_MSG(task->workerorder >= worker->current_ordered_task_order, "worker order values must not have duplicates (%u pushed to worker %d, but %d already passed)", task->workerorder, worker->workerid, worker->current_ordered_task_order);
  620. /* Put it in the ordered task ring */
  621. unsigned needed = task->workerorder - worker->current_ordered_task_order + 1;
  622. if (worker->local_ordered_tasks_size < needed)
  623. {
  624. /* Increase the size */
  625. unsigned alloc = worker->local_ordered_tasks_size;
  626. struct starpu_task **new;
  627. if (!alloc)
  628. alloc = 1;
  629. while (alloc < needed)
  630. alloc *= 2;
  631. _STARPU_MALLOC(new, alloc * sizeof(*new));
  632. if (worker->local_ordered_tasks_size)
  633. {
  634. /* Put existing tasks at the beginning of the new ring */
  635. unsigned copied = worker->local_ordered_tasks_size - worker->current_ordered_task;
  636. memcpy(new, &worker->local_ordered_tasks[worker->current_ordered_task], copied * sizeof(*new));
  637. memcpy(new + copied, worker->local_ordered_tasks, (worker->local_ordered_tasks_size - copied) * sizeof(*new));
  638. }
  639. memset(new + worker->local_ordered_tasks_size, 0, (alloc - worker->local_ordered_tasks_size) * sizeof(*new));
  640. free(worker->local_ordered_tasks);
  641. worker->local_ordered_tasks = new;
  642. worker->local_ordered_tasks_size = alloc;
  643. worker->current_ordered_task = 0;
  644. }
  645. worker->local_ordered_tasks[(worker->current_ordered_task + task->workerorder - worker->current_ordered_task_order) % worker->local_ordered_tasks_size] = task;
  646. }
  647. else
  648. {
  649. if (prio)
  650. starpu_task_list_push_front(&worker->local_tasks, task);
  651. else
  652. starpu_task_list_push_back(&worker->local_tasks, task);
  653. }
  654. starpu_wake_worker_locked(worker->workerid);
  655. starpu_push_task_end(task);
  656. _starpu_worker_unlock(worker->workerid);
  657. return 0;
  658. }