sched_policy.c 37 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213
  1. /* StarPU --- Runtime system for heterogeneous multicore architectures.
  2. *
  3. * Copyright (C) 2011-2017 Inria
  4. * Copyright (C) 2013 Simon Archipoff
  5. * Copyright (C) 2008-2019 Université de Bordeaux
  6. * Copyright (C) 2010-2017, 2019 CNRS
  7. * Copyright (C) 2013 Thibaut Lambert
  8. * Copyright (C) 2016 Uppsala University
  9. *
  10. * StarPU is free software; you can redistribute it and/or modify
  11. * it under the terms of the GNU Lesser General Public License as published by
  12. * the Free Software Foundation; either version 2.1 of the License, or (at
  13. * your option) any later version.
  14. *
  15. * StarPU is distributed in the hope that it will be useful, but
  16. * WITHOUT ANY WARRANTY; without even the implied warranty of
  17. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
  18. *
  19. * See the GNU Lesser General Public License in COPYING.LGPL for more details.
  20. */
  21. #include <starpu.h>
  22. #include <common/config.h>
  23. #include <common/utils.h>
  24. #include <core/sched_policy.h>
  25. #include <profiling/profiling.h>
  26. #include <common/barrier.h>
  27. #include <core/debug.h>
  28. #include <core/task.h>
  29. static int use_prefetch = 0;
  30. static double idle[STARPU_NMAXWORKERS];
  31. static double idle_start[STARPU_NMAXWORKERS];
  32. long _starpu_task_break_on_push = -1;
  33. long _starpu_task_break_on_sched = -1;
  34. long _starpu_task_break_on_pop = -1;
  35. long _starpu_task_break_on_exec = -1;
  36. static const char *starpu_idle_file;
  37. void _starpu_sched_init(void)
  38. {
  39. _starpu_task_break_on_push = starpu_get_env_number_default("STARPU_TASK_BREAK_ON_PUSH", -1);
  40. _starpu_task_break_on_sched = starpu_get_env_number_default("STARPU_TASK_BREAK_ON_SCHED", -1);
  41. _starpu_task_break_on_pop = starpu_get_env_number_default("STARPU_TASK_BREAK_ON_POP", -1);
  42. _starpu_task_break_on_exec = starpu_get_env_number_default("STARPU_TASK_BREAK_ON_EXEC", -1);
  43. starpu_idle_file = starpu_getenv("STARPU_IDLE_FILE");
  44. }
  45. int starpu_get_prefetch_flag(void)
  46. {
  47. return use_prefetch;
  48. }
  49. static struct starpu_sched_policy *predefined_policies[] =
  50. {
  51. &_starpu_sched_modular_eager_policy,
  52. &_starpu_sched_modular_eager_prefetching_policy,
  53. &_starpu_sched_modular_eager_prio_policy,
  54. &_starpu_sched_modular_gemm_policy,
  55. &_starpu_sched_modular_prio_policy,
  56. &_starpu_sched_modular_prio_prefetching_policy,
  57. &_starpu_sched_modular_random_policy,
  58. &_starpu_sched_modular_random_prio_policy,
  59. &_starpu_sched_modular_random_prefetching_policy,
  60. &_starpu_sched_modular_random_prio_prefetching_policy,
  61. &_starpu_sched_modular_parallel_random_policy,
  62. &_starpu_sched_modular_parallel_random_prio_policy,
  63. &_starpu_sched_modular_ws_policy,
  64. &_starpu_sched_modular_heft_policy,
  65. &_starpu_sched_modular_heft_prio_policy,
  66. &_starpu_sched_modular_heft2_policy,
  67. &_starpu_sched_modular_heteroprio_policy,
  68. &_starpu_sched_modular_heteroprio_heft_policy,
  69. &_starpu_sched_modular_parallel_heft_policy,
  70. &_starpu_sched_eager_policy,
  71. &_starpu_sched_prio_policy,
  72. &_starpu_sched_random_policy,
  73. &_starpu_sched_lws_policy,
  74. &_starpu_sched_ws_policy,
  75. &_starpu_sched_dm_policy,
  76. &_starpu_sched_dmda_policy,
  77. &_starpu_sched_dmda_prio_policy,
  78. &_starpu_sched_dmda_ready_policy,
  79. &_starpu_sched_dmda_sorted_policy,
  80. &_starpu_sched_dmda_sorted_decision_policy,
  81. &_starpu_sched_parallel_heft_policy,
  82. &_starpu_sched_peager_policy,
  83. &_starpu_sched_heteroprio_policy,
  84. &_starpu_sched_graph_test_policy,
  85. #ifdef STARPU_HAVE_HWLOC
  86. //&_starpu_sched_tree_heft_hierarchical_policy,
  87. #endif
  88. NULL
  89. };
  90. struct starpu_sched_policy **starpu_sched_get_predefined_policies()
  91. {
  92. return predefined_policies;
  93. }
  94. struct starpu_sched_policy *_starpu_get_sched_policy(struct _starpu_sched_ctx *sched_ctx)
  95. {
  96. return sched_ctx->sched_policy;
  97. }
  98. /*
  99. * Methods to initialize the scheduling policy
  100. */
  101. static void load_sched_policy(struct starpu_sched_policy *sched_policy, struct _starpu_sched_ctx *sched_ctx)
  102. {
  103. STARPU_ASSERT(sched_policy);
  104. #ifdef STARPU_VERBOSE
  105. if (sched_policy->policy_name)
  106. {
  107. if (sched_policy->policy_description)
  108. _STARPU_DEBUG("Use %s scheduler (%s)\n", sched_policy->policy_name, sched_policy->policy_description);
  109. else
  110. _STARPU_DEBUG("Use %s scheduler \n", sched_policy->policy_name);
  111. }
  112. #endif
  113. struct starpu_sched_policy *policy = sched_ctx->sched_policy;
  114. memcpy(policy, sched_policy, sizeof(*policy));
  115. }
  116. static struct starpu_sched_policy *find_sched_policy_from_name(const char *policy_name)
  117. {
  118. if (!policy_name)
  119. return NULL;
  120. if (strcmp(policy_name, "") == 0)
  121. return NULL;
  122. if (strncmp(policy_name, "heft", 4) == 0)
  123. {
  124. _STARPU_MSG("Warning: heft is now called \"dmda\".\n");
  125. return &_starpu_sched_dmda_policy;
  126. }
  127. struct starpu_sched_policy **policy;
  128. for(policy=predefined_policies ; *policy!=NULL ; policy++)
  129. {
  130. struct starpu_sched_policy *p = *policy;
  131. if (p->policy_name)
  132. {
  133. if (strcmp(policy_name, p->policy_name) == 0)
  134. {
  135. /* we found a policy with the requested name */
  136. return p;
  137. }
  138. }
  139. }
  140. if (strcmp(policy_name, "help") != 0)
  141. _STARPU_MSG("Warning: scheduling policy '%s' was not found, try 'help' to get a list\n", policy_name);
  142. /* nothing was found */
  143. return NULL;
  144. }
  145. static void display_sched_help_message(FILE *stream)
  146. {
  147. const char *sched_env = starpu_getenv("STARPU_SCHED");
  148. if (sched_env && (strcmp(sched_env, "help") == 0))
  149. {
  150. /* display the description of all predefined policies */
  151. struct starpu_sched_policy **policy;
  152. fprintf(stream, "\nThe variable STARPU_SCHED can be set to one of the following strings:\n");
  153. for(policy=predefined_policies ; *policy!=NULL ; policy++)
  154. {
  155. struct starpu_sched_policy *p = *policy;
  156. fprintf(stream, "%-30s\t-> %s\n", p->policy_name, p->policy_description);
  157. }
  158. fprintf(stream, "\n");
  159. }
  160. }
  161. struct starpu_sched_policy *_starpu_select_sched_policy(struct _starpu_machine_config *config, const char *required_policy)
  162. {
  163. struct starpu_sched_policy *selected_policy = NULL;
  164. struct starpu_conf *user_conf = &config->conf;
  165. if(required_policy)
  166. selected_policy = find_sched_policy_from_name(required_policy);
  167. /* If there is a policy that matches the required name, return it */
  168. if (selected_policy)
  169. return selected_policy;
  170. /* First, we check whether the application explicitely gave a scheduling policy or not */
  171. if (user_conf && (user_conf->sched_policy))
  172. return user_conf->sched_policy;
  173. /* Otherwise, we look if the application specified the name of a policy to load */
  174. const char *sched_pol_name;
  175. sched_pol_name = starpu_getenv("STARPU_SCHED");
  176. if (sched_pol_name == NULL && user_conf && user_conf->sched_policy_name)
  177. sched_pol_name = user_conf->sched_policy_name;
  178. if (sched_pol_name)
  179. selected_policy = find_sched_policy_from_name(sched_pol_name);
  180. /* If there is a policy that matches the name, return it */
  181. if (selected_policy)
  182. return selected_policy;
  183. /* If no policy was specified, we use the eager policy by default */
  184. return &_starpu_sched_lws_policy;
  185. }
  186. void _starpu_init_sched_policy(struct _starpu_machine_config *config, struct _starpu_sched_ctx *sched_ctx, struct starpu_sched_policy *selected_policy)
  187. {
  188. /* Perhaps we have to display some help */
  189. display_sched_help_message(stderr);
  190. /* Prefetch is activated by default */
  191. use_prefetch = starpu_get_env_number("STARPU_PREFETCH");
  192. if (use_prefetch == -1)
  193. use_prefetch = 1;
  194. /* Set calibrate flag */
  195. _starpu_set_calibrate_flag(config->conf.calibrate);
  196. load_sched_policy(selected_policy, sched_ctx);
  197. if (starpu_get_env_number_default("STARPU_WORKER_TREE", 0))
  198. {
  199. #ifdef STARPU_HAVE_HWLOC
  200. sched_ctx->sched_policy->worker_type = STARPU_WORKER_TREE;
  201. #else
  202. _STARPU_DISP("STARPU_WORKER_TREE ignored, please rebuild StarPU with hwloc support to enable it.");
  203. #endif
  204. }
  205. starpu_sched_ctx_create_worker_collection(sched_ctx->id,
  206. sched_ctx->sched_policy->worker_type);
  207. _STARPU_SCHED_BEGIN;
  208. sched_ctx->sched_policy->init_sched(sched_ctx->id);
  209. _STARPU_SCHED_END;
  210. }
  211. void _starpu_deinit_sched_policy(struct _starpu_sched_ctx *sched_ctx)
  212. {
  213. struct starpu_sched_policy *policy = sched_ctx->sched_policy;
  214. if (policy->deinit_sched)
  215. {
  216. _STARPU_SCHED_BEGIN;
  217. policy->deinit_sched(sched_ctx->id);
  218. _STARPU_SCHED_END;
  219. }
  220. starpu_sched_ctx_delete_worker_collection(sched_ctx->id);
  221. }
  222. void _starpu_sched_task_submit(struct starpu_task *task)
  223. {
  224. struct _starpu_sched_ctx *sched_ctx = _starpu_get_sched_ctx_struct(task->sched_ctx);
  225. if (!sched_ctx->sched_policy)
  226. return;
  227. if (!sched_ctx->sched_policy->submit_hook)
  228. return;
  229. _STARPU_SCHED_BEGIN;
  230. sched_ctx->sched_policy->submit_hook(task);
  231. _STARPU_SCHED_END;
  232. }
  233. void _starpu_sched_do_schedule(unsigned sched_ctx_id)
  234. {
  235. struct _starpu_sched_ctx *sched_ctx = _starpu_get_sched_ctx_struct(sched_ctx_id);
  236. if (!sched_ctx->sched_policy)
  237. return;
  238. if (!sched_ctx->sched_policy->do_schedule)
  239. return;
  240. _STARPU_SCHED_BEGIN;
  241. sched_ctx->sched_policy->do_schedule(sched_ctx_id);
  242. _STARPU_SCHED_END;
  243. }
  244. static void _starpu_push_task_on_specific_worker_notify_sched(struct starpu_task *task, struct _starpu_worker *worker, int workerid, int perf_workerid)
  245. {
  246. /* if we push a task on a specific worker, notify all the sched_ctxs the worker belongs to */
  247. struct _starpu_sched_ctx_list_iterator list_it;
  248. _starpu_sched_ctx_list_iterator_init(worker->sched_ctx_list, &list_it);
  249. while (_starpu_sched_ctx_list_iterator_has_next(&list_it))
  250. {
  251. struct _starpu_sched_ctx_elt *e = _starpu_sched_ctx_list_iterator_get_next(&list_it);
  252. struct _starpu_sched_ctx *sched_ctx = _starpu_get_sched_ctx_struct(e->sched_ctx);
  253. if (sched_ctx->sched_policy != NULL && sched_ctx->sched_policy->push_task_notify)
  254. {
  255. _STARPU_SCHED_BEGIN;
  256. sched_ctx->sched_policy->push_task_notify(task, workerid, perf_workerid, sched_ctx->id);
  257. _STARPU_SCHED_END;
  258. }
  259. }
  260. }
  261. /* Enqueue a task into the list of tasks explicitely attached to a worker. In
  262. * case workerid identifies a combined worker, a task will be enqueued into
  263. * each worker of the combination. */
  264. static int _starpu_push_task_on_specific_worker(struct starpu_task *task, int workerid)
  265. {
  266. int nbasic_workers = (int)starpu_worker_get_count();
  267. /* Is this a basic worker or a combined worker ? */
  268. int is_basic_worker = (workerid < nbasic_workers);
  269. struct _starpu_worker *worker = NULL;
  270. struct _starpu_combined_worker *combined_worker = NULL;
  271. if (is_basic_worker)
  272. {
  273. worker = _starpu_get_worker_struct(workerid);
  274. }
  275. else
  276. {
  277. combined_worker = _starpu_get_combined_worker_struct(workerid);
  278. }
  279. if (use_prefetch)
  280. starpu_prefetch_task_input_for(task, workerid);
  281. if (is_basic_worker)
  282. _starpu_push_task_on_specific_worker_notify_sched(task, worker, workerid, workerid);
  283. else
  284. {
  285. /* Notify all workers of the combined worker */
  286. int worker_size = combined_worker->worker_size;
  287. int *combined_workerid = combined_worker->combined_workerid;
  288. int j;
  289. for (j = 0; j < worker_size; j++)
  290. {
  291. int subworkerid = combined_workerid[j];
  292. _starpu_push_task_on_specific_worker_notify_sched(task, _starpu_get_worker_struct(subworkerid), subworkerid, workerid);
  293. }
  294. }
  295. #ifdef STARPU_USE_SC_HYPERVISOR
  296. starpu_sched_ctx_call_pushed_task_cb(workerid, task->sched_ctx);
  297. #endif //STARPU_USE_SC_HYPERVISOR
  298. if (is_basic_worker)
  299. {
  300. unsigned node = starpu_worker_get_memory_node(workerid);
  301. if (_starpu_task_uses_multiformat_handles(task))
  302. {
  303. unsigned nbuffers = STARPU_TASK_GET_NBUFFERS(task);
  304. unsigned i;
  305. for (i = 0; i < nbuffers; i++)
  306. {
  307. struct starpu_task *conversion_task;
  308. starpu_data_handle_t handle;
  309. handle = STARPU_TASK_GET_HANDLE(task, i);
  310. if (!_starpu_handle_needs_conversion_task(handle, node))
  311. continue;
  312. conversion_task = _starpu_create_conversion_task(handle, node);
  313. conversion_task->mf_skip = 1;
  314. conversion_task->execute_on_a_specific_worker = 1;
  315. conversion_task->workerid = workerid;
  316. _starpu_task_submit_conversion_task(conversion_task, workerid);
  317. //_STARPU_DEBUG("Pushing a conversion task\n");
  318. }
  319. for (i = 0; i < nbuffers; i++)
  320. {
  321. starpu_data_handle_t handle = STARPU_TASK_GET_HANDLE(task, i);
  322. handle->mf_node = node;
  323. }
  324. }
  325. // if(task->sched_ctx != _starpu_get_initial_sched_ctx()->id)
  326. if(task->priority > 0)
  327. return _starpu_push_local_task(worker, task, 1);
  328. else
  329. return _starpu_push_local_task(worker, task, 0);
  330. }
  331. else
  332. {
  333. /* This is a combined worker so we create task aliases */
  334. int worker_size = combined_worker->worker_size;
  335. int *combined_workerid = combined_worker->combined_workerid;
  336. int ret = 0;
  337. struct _starpu_job *job = _starpu_get_job_associated_to_task(task);
  338. job->task_size = worker_size;
  339. job->combined_workerid = workerid;
  340. job->active_task_alias_count = 0;
  341. STARPU_PTHREAD_BARRIER_INIT(&job->before_work_barrier, NULL, worker_size);
  342. STARPU_PTHREAD_BARRIER_INIT(&job->after_work_barrier, NULL, worker_size);
  343. job->after_work_busy_barrier = worker_size;
  344. /* Note: we have to call that early, or else the task may have
  345. * disappeared already */
  346. starpu_push_task_end(task);
  347. int j;
  348. for (j = 0; j < worker_size; j++)
  349. {
  350. struct starpu_task *alias = starpu_task_dup(task);
  351. alias->destroy = 1;
  352. _STARPU_TRACE_JOB_PUSH(alias, alias->priority > 0);
  353. worker = _starpu_get_worker_struct(combined_workerid[j]);
  354. ret |= _starpu_push_local_task(worker, alias, 0);
  355. }
  356. return ret;
  357. }
  358. }
  359. /* the generic interface that call the proper underlying implementation */
  360. int _starpu_push_task(struct _starpu_job *j)
  361. {
  362. if(j->task->prologue_callback_func)
  363. {
  364. _starpu_set_current_task(j->task);
  365. j->task->prologue_callback_func(j->task->prologue_callback_arg);
  366. _starpu_set_current_task(NULL);
  367. }
  368. return _starpu_repush_task(j);
  369. }
  370. int _starpu_repush_task(struct _starpu_job *j)
  371. {
  372. struct starpu_task *task = j->task;
  373. struct _starpu_sched_ctx *sched_ctx = _starpu_get_sched_ctx_struct(task->sched_ctx);
  374. int ret;
  375. _STARPU_LOG_IN();
  376. unsigned can_push = _starpu_increment_nready_tasks_of_sched_ctx(task->sched_ctx, task->flops, task);
  377. STARPU_ASSERT(task->status == STARPU_TASK_BLOCKED || task->status == STARPU_TASK_BLOCKED_ON_TAG || task->status == STARPU_TASK_BLOCKED_ON_TASK || task->status == STARPU_TASK_BLOCKED_ON_DATA);
  378. task->status = STARPU_TASK_READY;
  379. const unsigned continuation =
  380. #ifdef STARPU_OPENMP
  381. j->continuation
  382. #else
  383. 0
  384. #endif
  385. ;
  386. if (!j->internal && !continuation)
  387. {
  388. (void) STARPU_ATOMIC_ADD64(& _starpu_task__g_current_submitted__value, -1);
  389. int64_t value = STARPU_ATOMIC_ADD64(& _starpu_task__g_current_ready__value, 1);
  390. _starpu_perf_counter_update_max_int64(&_starpu_task__g_peak_ready__value, value);
  391. if (task->cl && task->cl->perf_counter_values)
  392. {
  393. struct starpu_perf_counter_sample_cl_values * const pcv = task->cl->perf_counter_values;
  394. (void)STARPU_ATOMIC_ADD64(&pcv->task.current_submitted, -1);
  395. value = STARPU_ATOMIC_ADD64(&pcv->task.current_ready, 1);
  396. _starpu_perf_counter_update_max_int64(&pcv->task.peak_ready, value);
  397. }
  398. }
  399. STARPU_AYU_ADDTOTASKQUEUE(j->job_id, -1);
  400. /* if the context does not have any workers save the tasks in a temp list */
  401. if ((task->cl != NULL && task->where != STARPU_NOWHERE) && (!sched_ctx->is_initial_sched))
  402. {
  403. /*if there are workers in the ctx that are not able to execute tasks
  404. we consider the ctx empty */
  405. unsigned nworkers = _starpu_nworkers_able_to_execute_task(task, sched_ctx);
  406. if(nworkers == 0)
  407. {
  408. _starpu_sched_ctx_lock_write(sched_ctx->id);
  409. starpu_task_list_push_front(&sched_ctx->empty_ctx_tasks, task);
  410. _starpu_sched_ctx_unlock_write(sched_ctx->id);
  411. #ifdef STARPU_USE_SC_HYPERVISOR
  412. if(sched_ctx->id != 0 && sched_ctx->perf_counters != NULL
  413. && sched_ctx->perf_counters->notify_empty_ctx)
  414. {
  415. _STARPU_TRACE_HYPERVISOR_BEGIN();
  416. sched_ctx->perf_counters->notify_empty_ctx(sched_ctx->id, task);
  417. _STARPU_TRACE_HYPERVISOR_END();
  418. }
  419. #endif
  420. return 0;
  421. }
  422. }
  423. if(!can_push)
  424. return 0;
  425. /* in case there is no codelet associated to the task (that's a control
  426. * task), we directly execute its callback and enforce the
  427. * corresponding dependencies */
  428. if (task->cl == NULL || task->where == STARPU_NOWHERE)
  429. {
  430. if (!j->internal)
  431. {
  432. (void)STARPU_ATOMIC_ADD64(& _starpu_task__g_current_ready__value, -1);
  433. if (task->cl && task->cl->perf_counter_values)
  434. {
  435. struct starpu_perf_counter_sample_cl_values * const pcv = task->cl->perf_counter_values;
  436. (void)STARPU_ATOMIC_ADD64(&pcv->task.current_ready, -1);
  437. }
  438. }
  439. task->status = STARPU_TASK_RUNNING;
  440. if (task->prologue_callback_pop_func)
  441. {
  442. _starpu_set_current_task(task);
  443. task->prologue_callback_pop_func(task->prologue_callback_pop_arg);
  444. _starpu_set_current_task(NULL);
  445. }
  446. if (task->cl && task->cl->specific_nodes)
  447. {
  448. /* Nothing to do, but we are asked to fetch data on some memory nodes */
  449. _starpu_fetch_nowhere_task_input(j);
  450. }
  451. else
  452. {
  453. if (task->cl)
  454. __starpu_push_task_output(j);
  455. _starpu_handle_job_termination(j);
  456. _STARPU_LOG_OUT_TAG("handle_job_termination");
  457. }
  458. return 0;
  459. }
  460. ret = _starpu_push_task_to_workers(task);
  461. if (ret == -EAGAIN)
  462. /* pushed to empty context, that's fine */
  463. ret = 0;
  464. return ret;
  465. }
  466. int _starpu_push_task_to_workers(struct starpu_task *task)
  467. {
  468. struct _starpu_sched_ctx *sched_ctx = _starpu_get_sched_ctx_struct(task->sched_ctx);
  469. unsigned nworkers = 0;
  470. _STARPU_TRACE_JOB_PUSH(task, task->priority > 0);
  471. /* if the contexts still does not have workers put the task back to its place in
  472. the empty ctx list */
  473. if(!sched_ctx->is_initial_sched)
  474. {
  475. /*if there are workers in the ctx that are not able to execute tasks
  476. we consider the ctx empty */
  477. nworkers = _starpu_nworkers_able_to_execute_task(task, sched_ctx);
  478. if (nworkers == 0)
  479. {
  480. _starpu_sched_ctx_lock_write(sched_ctx->id);
  481. starpu_task_list_push_back(&sched_ctx->empty_ctx_tasks, task);
  482. _starpu_sched_ctx_unlock_write(sched_ctx->id);
  483. #ifdef STARPU_USE_SC_HYPERVISOR
  484. if(sched_ctx->id != 0 && sched_ctx->perf_counters != NULL
  485. && sched_ctx->perf_counters->notify_empty_ctx)
  486. {
  487. _STARPU_TRACE_HYPERVISOR_BEGIN();
  488. sched_ctx->perf_counters->notify_empty_ctx(sched_ctx->id, task);
  489. _STARPU_TRACE_HYPERVISOR_END();
  490. }
  491. #endif
  492. return -EAGAIN;
  493. }
  494. }
  495. _starpu_profiling_set_task_push_start_time(task);
  496. int ret = 0;
  497. if (STARPU_UNLIKELY(task->execute_on_a_specific_worker))
  498. {
  499. if (starpu_get_prefetch_flag())
  500. starpu_prefetch_task_input_for(task, task->workerid);
  501. ret = _starpu_push_task_on_specific_worker(task, task->workerid);
  502. }
  503. else
  504. {
  505. struct _starpu_machine_config *config = _starpu_get_machine_config();
  506. /* When a task can only be executed on a given arch and we have
  507. * only one memory node for that arch, we can systematically
  508. * prefetch before the scheduling decision. */
  509. if (starpu_get_prefetch_flag())
  510. {
  511. if (task->where == STARPU_CPU && config->cpus_nodeid >= 0)
  512. starpu_prefetch_task_input_on_node(task, config->cpus_nodeid);
  513. else if (task->where == STARPU_CUDA && config->cuda_nodeid >= 0)
  514. starpu_prefetch_task_input_on_node(task, config->cuda_nodeid);
  515. else if (task->where == STARPU_OPENCL && config->opencl_nodeid >= 0)
  516. starpu_prefetch_task_input_on_node(task, config->opencl_nodeid);
  517. else if (task->where == STARPU_MIC && config->mic_nodeid >= 0)
  518. starpu_prefetch_task_input_on_node(task, config->mic_nodeid);
  519. }
  520. if(!sched_ctx->sched_policy)
  521. {
  522. /* Note: we have to call that early, or else the task may have
  523. * disappeared already */
  524. starpu_push_task_end(task);
  525. if(!sched_ctx->awake_workers)
  526. ret = _starpu_push_task_on_specific_worker(task, sched_ctx->main_master);
  527. else
  528. {
  529. struct starpu_worker_collection *workers = sched_ctx->workers;
  530. struct _starpu_job *job = _starpu_get_job_associated_to_task(task);
  531. job->task_size = workers->nworkers;
  532. job->combined_workerid = -1; // workerid; its a ctx not combined worker
  533. job->active_task_alias_count = 0;
  534. STARPU_PTHREAD_BARRIER_INIT(&job->before_work_barrier, NULL, workers->nworkers);
  535. STARPU_PTHREAD_BARRIER_INIT(&job->after_work_barrier, NULL, workers->nworkers);
  536. job->after_work_busy_barrier = workers->nworkers;
  537. struct starpu_sched_ctx_iterator it;
  538. if(workers->init_iterator)
  539. workers->init_iterator(workers, &it);
  540. while(workers->has_next(workers, &it))
  541. {
  542. unsigned workerid = workers->get_next(workers, &it);
  543. struct starpu_task *alias;
  544. if (job->task_size > 1)
  545. {
  546. alias = starpu_task_dup(task);
  547. _STARPU_TRACE_JOB_PUSH(alias, alias->priority > 0);
  548. alias->destroy = 1;
  549. }
  550. else
  551. alias = task;
  552. ret |= _starpu_push_task_on_specific_worker(alias, workerid);
  553. }
  554. }
  555. }
  556. else
  557. {
  558. STARPU_ASSERT(sched_ctx->sched_policy->push_task);
  559. /* check out if there are any workers in the context */
  560. nworkers = starpu_sched_ctx_get_nworkers(sched_ctx->id);
  561. if (nworkers == 0)
  562. ret = -1;
  563. else
  564. {
  565. struct _starpu_worker *worker = _starpu_get_local_worker_key();
  566. if (worker)
  567. {
  568. STARPU_PTHREAD_MUTEX_LOCK_SCHED(&worker->sched_mutex);
  569. _starpu_worker_enter_sched_op(worker);
  570. STARPU_PTHREAD_MUTEX_UNLOCK_SCHED(&worker->sched_mutex);
  571. }
  572. _STARPU_TASK_BREAK_ON(task, push);
  573. _STARPU_SCHED_BEGIN;
  574. ret = sched_ctx->sched_policy->push_task(task);
  575. _STARPU_SCHED_END;
  576. if (worker)
  577. {
  578. STARPU_PTHREAD_MUTEX_LOCK_SCHED(&worker->sched_mutex);
  579. _starpu_worker_leave_sched_op(worker);
  580. STARPU_PTHREAD_MUTEX_UNLOCK_SCHED(&worker->sched_mutex);
  581. }
  582. }
  583. }
  584. if(ret == -1)
  585. {
  586. _STARPU_MSG("repush task \n");
  587. _STARPU_TRACE_JOB_POP(task, task->priority > 0);
  588. ret = _starpu_push_task_to_workers(task);
  589. }
  590. }
  591. /* Note: from here, the task might have been destroyed already! */
  592. _STARPU_LOG_OUT();
  593. return ret;
  594. }
  595. /* This is called right after the scheduler has pushed a task to a queue
  596. * but just before releasing mutexes: we need the task to still be alive!
  597. */
  598. int starpu_push_task_end(struct starpu_task *task)
  599. {
  600. _starpu_profiling_set_task_push_end_time(task);
  601. task->scheduled = 1;
  602. return 0;
  603. }
  604. /* This is called right after the scheduler has pushed a task to a queue
  605. * but just before releasing mutexes: we need the task to still be alive!
  606. */
  607. int _starpu_pop_task_end(struct starpu_task *task)
  608. {
  609. if (!task)
  610. return 0;
  611. _STARPU_TRACE_JOB_POP(task, task->priority > 0);
  612. return 0;
  613. }
  614. /*
  615. * Given a handle that needs to be converted in order to be used on the given
  616. * node, returns a task that takes care of the conversion.
  617. */
  618. struct starpu_task *_starpu_create_conversion_task(starpu_data_handle_t handle, unsigned int node)
  619. {
  620. return _starpu_create_conversion_task_for_arch(handle, starpu_node_get_kind(node));
  621. }
  622. struct starpu_task *_starpu_create_conversion_task_for_arch(starpu_data_handle_t handle, enum starpu_node_kind node_kind)
  623. {
  624. struct starpu_task *conversion_task;
  625. #if defined(STARPU_USE_OPENCL) || defined(STARPU_USE_CUDA) || defined(STARPU_USE_MIC) || defined(STARPU_SIMGRID)
  626. struct starpu_multiformat_interface *format_interface;
  627. #endif
  628. conversion_task = starpu_task_create();
  629. conversion_task->name = "conversion_task";
  630. conversion_task->synchronous = 0;
  631. STARPU_TASK_SET_HANDLE(conversion_task, handle, 0);
  632. #if defined(STARPU_USE_OPENCL) || defined(STARPU_USE_CUDA) || defined(STARPU_USE_MIC) || defined(STARPU_SIMGRID)
  633. /* The node does not really matter here */
  634. format_interface = (struct starpu_multiformat_interface *) starpu_data_get_interface_on_node(handle, STARPU_MAIN_RAM);
  635. #endif
  636. _starpu_spin_lock(&handle->header_lock);
  637. handle->refcnt++;
  638. handle->busy_count++;
  639. _starpu_spin_unlock(&handle->header_lock);
  640. switch(node_kind)
  641. {
  642. case STARPU_CPU_RAM:
  643. switch (starpu_node_get_kind(handle->mf_node))
  644. {
  645. case STARPU_CPU_RAM:
  646. STARPU_ABORT();
  647. #if defined(STARPU_USE_CUDA) || defined(STARPU_SIMGRID)
  648. case STARPU_CUDA_RAM:
  649. {
  650. struct starpu_multiformat_data_interface_ops *mf_ops;
  651. mf_ops = (struct starpu_multiformat_data_interface_ops *) handle->ops->get_mf_ops(format_interface);
  652. conversion_task->cl = mf_ops->cuda_to_cpu_cl;
  653. break;
  654. }
  655. #endif
  656. #if defined(STARPU_USE_OPENCL) || defined(STARPU_SIMGRID)
  657. case STARPU_OPENCL_RAM:
  658. {
  659. struct starpu_multiformat_data_interface_ops *mf_ops;
  660. mf_ops = (struct starpu_multiformat_data_interface_ops *) handle->ops->get_mf_ops(format_interface);
  661. conversion_task->cl = mf_ops->opencl_to_cpu_cl;
  662. break;
  663. }
  664. #endif
  665. #ifdef STARPU_USE_MIC
  666. case STARPU_MIC_RAM:
  667. {
  668. struct starpu_multiformat_data_interface_ops *mf_ops;
  669. mf_ops = (struct starpu_multiformat_data_interface_ops *) handle->ops->get_mf_ops(format_interface);
  670. conversion_task->cl = mf_ops->mic_to_cpu_cl;
  671. break;
  672. }
  673. #endif
  674. default:
  675. _STARPU_ERROR("Oops : %u\n", handle->mf_node);
  676. }
  677. break;
  678. #if defined(STARPU_USE_CUDA) || defined(STARPU_SIMGRID)
  679. case STARPU_CUDA_RAM:
  680. {
  681. struct starpu_multiformat_data_interface_ops *mf_ops;
  682. mf_ops = (struct starpu_multiformat_data_interface_ops *) handle->ops->get_mf_ops(format_interface);
  683. conversion_task->cl = mf_ops->cpu_to_cuda_cl;
  684. break;
  685. }
  686. #endif
  687. #if defined(STARPU_USE_OPENCL) || defined(STARPU_SIMGRID)
  688. case STARPU_OPENCL_RAM:
  689. {
  690. struct starpu_multiformat_data_interface_ops *mf_ops;
  691. mf_ops = (struct starpu_multiformat_data_interface_ops *) handle->ops->get_mf_ops(format_interface);
  692. conversion_task->cl = mf_ops->cpu_to_opencl_cl;
  693. break;
  694. }
  695. #endif
  696. #ifdef STARPU_USE_MIC
  697. case STARPU_MIC_RAM:
  698. {
  699. struct starpu_multiformat_data_interface_ops *mf_ops;
  700. mf_ops = (struct starpu_multiformat_data_interface_ops *) handle->ops->get_mf_ops(format_interface);
  701. conversion_task->cl = mf_ops->cpu_to_mic_cl;
  702. break;
  703. }
  704. #endif
  705. default:
  706. STARPU_ABORT();
  707. }
  708. _starpu_codelet_check_deprecated_fields(conversion_task->cl);
  709. STARPU_TASK_SET_MODE(conversion_task, STARPU_RW, 0);
  710. return conversion_task;
  711. }
  712. static
  713. struct _starpu_sched_ctx* _get_next_sched_ctx_to_pop_into(struct _starpu_worker *worker)
  714. {
  715. struct _starpu_sched_ctx_elt *e = NULL;
  716. struct _starpu_sched_ctx_list_iterator list_it;
  717. int found = 0;
  718. _starpu_sched_ctx_list_iterator_init(worker->sched_ctx_list, &list_it);
  719. while (_starpu_sched_ctx_list_iterator_has_next(&list_it))
  720. {
  721. e = _starpu_sched_ctx_list_iterator_get_next(&list_it);
  722. if (e->task_number > 0)
  723. return _starpu_get_sched_ctx_struct(e->sched_ctx);
  724. }
  725. _starpu_sched_ctx_list_iterator_init(worker->sched_ctx_list, &list_it);
  726. while (_starpu_sched_ctx_list_iterator_has_next(&list_it))
  727. {
  728. e = _starpu_sched_ctx_list_iterator_get_next(&list_it);
  729. if (e->last_poped)
  730. {
  731. e->last_poped = 0;
  732. if (_starpu_sched_ctx_list_iterator_has_next(&list_it))
  733. {
  734. e = _starpu_sched_ctx_list_iterator_get_next(&list_it);
  735. found = 1;
  736. }
  737. break;
  738. }
  739. }
  740. if (!found)
  741. e = worker->sched_ctx_list->head;
  742. e->last_poped = 1;
  743. return _starpu_get_sched_ctx_struct(e->sched_ctx);
  744. }
  745. struct starpu_task *_starpu_pop_task(struct _starpu_worker *worker)
  746. {
  747. struct starpu_task *task;
  748. int worker_id;
  749. unsigned node;
  750. /* We can't tell in advance which task will be picked up, so we measure
  751. * a timestamp, and will attribute it afterwards to the task. */
  752. int profiling = starpu_profiling_status_get();
  753. struct timespec pop_start_time;
  754. if (profiling)
  755. _starpu_clock_gettime(&pop_start_time);
  756. pick:
  757. /* perhaps there is some local task to be executed first */
  758. task = _starpu_pop_local_task(worker);
  759. if (task)
  760. _STARPU_TASK_BREAK_ON(task, pop);
  761. /* get tasks from the stacks of the strategy */
  762. if(!task)
  763. {
  764. struct _starpu_sched_ctx *sched_ctx ;
  765. #ifndef STARPU_NON_BLOCKING_DRIVERS
  766. int been_here[STARPU_NMAX_SCHED_CTXS];
  767. int i;
  768. for(i = 0; i < STARPU_NMAX_SCHED_CTXS; i++)
  769. been_here[i] = 0;
  770. while(!task)
  771. #endif
  772. {
  773. if(worker->nsched_ctxs == 1)
  774. sched_ctx = _starpu_get_initial_sched_ctx();
  775. else
  776. {
  777. while(1)
  778. {
  779. /** Caution
  780. * If you use multiple contexts your scheduler *needs*
  781. * to update the variable task_number of the ctx list.
  782. * In order to get the best performances.
  783. * This is done using functions :
  784. * starpu_sched_ctx_list_task_counters_increment...(...)
  785. * starpu_sched_ctx_list_task_counters_decrement...(...)
  786. **/
  787. sched_ctx = _get_next_sched_ctx_to_pop_into(worker);
  788. if(worker->removed_from_ctx[sched_ctx->id] == 1 && worker->shares_tasks_lists[sched_ctx->id] == 1)
  789. {
  790. _starpu_worker_gets_out_of_ctx(sched_ctx->id, worker);
  791. worker->removed_from_ctx[sched_ctx->id] = 0;
  792. sched_ctx = NULL;
  793. }
  794. else
  795. break;
  796. }
  797. }
  798. if(sched_ctx && sched_ctx->id != STARPU_NMAX_SCHED_CTXS)
  799. {
  800. if (sched_ctx->sched_policy && sched_ctx->sched_policy->pop_task)
  801. {
  802. /* Note: we do not push the scheduling state here, because
  803. * otherwise when a worker is idle, we'd keep
  804. * pushing/popping a scheduling state here, while what we
  805. * want to see in the trace is a permanent idle state. */
  806. task = sched_ctx->sched_policy->pop_task(sched_ctx->id);
  807. if (task)
  808. _STARPU_TASK_BREAK_ON(task, pop);
  809. _starpu_pop_task_end(task);
  810. }
  811. }
  812. if(!task)
  813. {
  814. /* it doesn't matter if it shares tasks list or not in the scheduler,
  815. if it does not have any task to pop just get it out of here */
  816. /* however if it shares a task list it will be removed as soon as he
  817. finishes this job (in handle_job_termination) */
  818. if(worker->removed_from_ctx[sched_ctx->id])
  819. {
  820. _starpu_worker_gets_out_of_ctx(sched_ctx->id, worker);
  821. worker->removed_from_ctx[sched_ctx->id] = 0;
  822. }
  823. #ifdef STARPU_USE_SC_HYPERVISOR
  824. if(worker->pop_ctx_priority)
  825. {
  826. struct starpu_sched_ctx_performance_counters *perf_counters = sched_ctx->perf_counters;
  827. if(sched_ctx->id != 0 && perf_counters != NULL && perf_counters->notify_idle_cycle && _starpu_sched_ctx_allow_hypervisor(sched_ctx->id))
  828. {
  829. // _STARPU_TRACE_HYPERVISOR_BEGIN();
  830. perf_counters->notify_idle_cycle(sched_ctx->id, worker->workerid, 1.0);
  831. // _STARPU_TRACE_HYPERVISOR_END();
  832. }
  833. }
  834. #endif //STARPU_USE_SC_HYPERVISOR
  835. #ifndef STARPU_NON_BLOCKING_DRIVERS
  836. if(been_here[sched_ctx->id] || worker->nsched_ctxs == 1)
  837. break;
  838. been_here[sched_ctx->id] = 1;
  839. #endif
  840. }
  841. }
  842. }
  843. if (!task)
  844. {
  845. if (starpu_idle_file)
  846. idle_start[worker->workerid] = starpu_timing_now();
  847. return NULL;
  848. }
  849. if(starpu_idle_file && idle_start[worker->workerid] != 0.0)
  850. {
  851. double idle_end = starpu_timing_now();
  852. idle[worker->workerid] += (idle_end - idle_start[worker->workerid]);
  853. idle_start[worker->workerid] = 0.0;
  854. }
  855. #ifdef STARPU_USE_SC_HYPERVISOR
  856. struct _starpu_sched_ctx *sched_ctx = _starpu_get_sched_ctx_struct(task->sched_ctx);
  857. struct starpu_sched_ctx_performance_counters *perf_counters = sched_ctx->perf_counters;
  858. if(sched_ctx->id != 0 && perf_counters != NULL && perf_counters->notify_poped_task && _starpu_sched_ctx_allow_hypervisor(sched_ctx->id))
  859. {
  860. // _STARPU_TRACE_HYPERVISOR_BEGIN();
  861. perf_counters->notify_poped_task(task->sched_ctx, worker->workerid);
  862. // _STARPU_TRACE_HYPERVISOR_END();
  863. }
  864. #endif //STARPU_USE_SC_HYPERVISOR
  865. /* Make sure we do not bother with all the multiformat-specific code if
  866. * it is not necessary. */
  867. if (!_starpu_task_uses_multiformat_handles(task))
  868. goto profiling;
  869. /* This is either a conversion task, or a regular task for which the
  870. * conversion tasks have already been created and submitted */
  871. if (task->mf_skip)
  872. goto profiling;
  873. /*
  874. * This worker may not be able to execute this task. In this case, we
  875. * should return the task anyway. It will be pushed back almost immediatly.
  876. * This way, we avoid computing and executing the conversions tasks.
  877. * Here, we do not care about what implementation is used.
  878. */
  879. worker_id = starpu_worker_get_id_check();
  880. if (!starpu_worker_can_execute_task_first_impl(worker_id, task, NULL))
  881. return task;
  882. node = starpu_worker_get_memory_node(worker_id);
  883. /*
  884. * We do have a task that uses multiformat handles. Let's create the
  885. * required conversion tasks.
  886. */
  887. unsigned i;
  888. unsigned nbuffers = STARPU_TASK_GET_NBUFFERS(task);
  889. for (i = 0; i < nbuffers; i++)
  890. {
  891. struct starpu_task *conversion_task;
  892. starpu_data_handle_t handle;
  893. handle = STARPU_TASK_GET_HANDLE(task, i);
  894. if (!_starpu_handle_needs_conversion_task(handle, node))
  895. continue;
  896. conversion_task = _starpu_create_conversion_task(handle, node);
  897. conversion_task->mf_skip = 1;
  898. conversion_task->execute_on_a_specific_worker = 1;
  899. conversion_task->workerid = worker_id;
  900. /*
  901. * Next tasks will need to know where these handles have gone.
  902. */
  903. handle->mf_node = node;
  904. _starpu_task_submit_conversion_task(conversion_task, worker_id);
  905. }
  906. task->mf_skip = 1;
  907. starpu_task_list_push_back(&worker->local_tasks, task);
  908. goto pick;
  909. profiling:
  910. if (profiling)
  911. {
  912. struct starpu_profiling_task_info *profiling_info;
  913. profiling_info = task->profiling_info;
  914. /* The task may have been created before profiling was enabled,
  915. * so we check if the profiling_info structure is available
  916. * even though we already tested if profiling is enabled. */
  917. if (profiling_info)
  918. {
  919. memcpy(&profiling_info->pop_start_time,
  920. &pop_start_time, sizeof(struct timespec));
  921. _starpu_clock_gettime(&profiling_info->pop_end_time);
  922. }
  923. }
  924. if(task->prologue_callback_pop_func)
  925. {
  926. _starpu_set_current_task(task);
  927. task->prologue_callback_pop_func(task->prologue_callback_pop_arg);
  928. _starpu_set_current_task(NULL);
  929. }
  930. return task;
  931. }
  932. struct starpu_task *_starpu_pop_every_task(struct _starpu_sched_ctx *sched_ctx)
  933. {
  934. struct starpu_task *task = NULL;
  935. if(sched_ctx->sched_policy)
  936. {
  937. STARPU_ASSERT(sched_ctx->sched_policy->pop_every_task);
  938. /* TODO set profiling info */
  939. if(sched_ctx->sched_policy->pop_every_task)
  940. {
  941. _STARPU_SCHED_BEGIN;
  942. task = sched_ctx->sched_policy->pop_every_task(sched_ctx->id);
  943. _STARPU_SCHED_END;
  944. }
  945. }
  946. return task;
  947. }
  948. void _starpu_sched_pre_exec_hook(struct starpu_task *task)
  949. {
  950. unsigned sched_ctx_id = starpu_sched_ctx_get_ctx_for_task(task);
  951. struct _starpu_sched_ctx *sched_ctx = _starpu_get_sched_ctx_struct(sched_ctx_id);
  952. if (sched_ctx->sched_policy && sched_ctx->sched_policy->pre_exec_hook)
  953. {
  954. _STARPU_SCHED_BEGIN;
  955. sched_ctx->sched_policy->pre_exec_hook(task, sched_ctx_id);
  956. _STARPU_SCHED_END;
  957. }
  958. if(!sched_ctx->sched_policy)
  959. {
  960. int workerid = starpu_worker_get_id();
  961. struct _starpu_worker *worker = _starpu_get_worker_struct(workerid);
  962. struct _starpu_sched_ctx_list_iterator list_it;
  963. _starpu_sched_ctx_list_iterator_init(worker->sched_ctx_list, &list_it);
  964. while (_starpu_sched_ctx_list_iterator_has_next(&list_it))
  965. {
  966. struct _starpu_sched_ctx *other_sched_ctx;
  967. struct _starpu_sched_ctx_elt *e;
  968. e = _starpu_sched_ctx_list_iterator_get_next(&list_it);
  969. other_sched_ctx = _starpu_get_sched_ctx_struct(e->sched_ctx);
  970. if (other_sched_ctx != sched_ctx &&
  971. other_sched_ctx->sched_policy != NULL &&
  972. other_sched_ctx->sched_policy->pre_exec_hook)
  973. {
  974. _STARPU_SCHED_BEGIN;
  975. other_sched_ctx->sched_policy->pre_exec_hook(task, other_sched_ctx->id);
  976. _STARPU_SCHED_END;
  977. }
  978. }
  979. }
  980. }
  981. void _starpu_sched_post_exec_hook(struct starpu_task *task)
  982. {
  983. STARPU_ASSERT(task->cl != NULL && task->cl->where != STARPU_NOWHERE);
  984. unsigned sched_ctx_id = starpu_sched_ctx_get_ctx_for_task(task);
  985. struct _starpu_sched_ctx *sched_ctx = _starpu_get_sched_ctx_struct(sched_ctx_id);
  986. if (sched_ctx->sched_policy && sched_ctx->sched_policy->post_exec_hook)
  987. {
  988. _STARPU_SCHED_BEGIN;
  989. sched_ctx->sched_policy->post_exec_hook(task, sched_ctx_id);
  990. _STARPU_SCHED_END;
  991. }
  992. if(!sched_ctx->sched_policy)
  993. {
  994. int workerid = starpu_worker_get_id();
  995. struct _starpu_worker *worker = _starpu_get_worker_struct(workerid);
  996. struct _starpu_sched_ctx_list_iterator list_it;
  997. _starpu_sched_ctx_list_iterator_init(worker->sched_ctx_list, &list_it);
  998. while (_starpu_sched_ctx_list_iterator_has_next(&list_it))
  999. {
  1000. struct _starpu_sched_ctx *other_sched_ctx;
  1001. struct _starpu_sched_ctx_elt *e;
  1002. e = _starpu_sched_ctx_list_iterator_get_next(&list_it);
  1003. other_sched_ctx = _starpu_get_sched_ctx_struct(e->sched_ctx);
  1004. if (other_sched_ctx != sched_ctx &&
  1005. other_sched_ctx->sched_policy != NULL &&
  1006. other_sched_ctx->sched_policy->post_exec_hook)
  1007. {
  1008. _STARPU_SCHED_BEGIN;
  1009. other_sched_ctx->sched_policy->post_exec_hook(task, other_sched_ctx->id);
  1010. _STARPU_SCHED_END;
  1011. }
  1012. }
  1013. }
  1014. }
  1015. void _starpu_wait_on_sched_event(void)
  1016. {
  1017. struct _starpu_worker *worker = _starpu_get_local_worker_key();
  1018. STARPU_PTHREAD_MUTEX_LOCK_SCHED(&worker->sched_mutex);
  1019. _starpu_handle_all_pending_node_data_requests(worker->memory_node);
  1020. if (_starpu_machine_is_running())
  1021. {
  1022. #ifndef STARPU_NON_BLOCKING_DRIVERS
  1023. STARPU_PTHREAD_COND_WAIT(&worker->sched_cond,
  1024. &worker->sched_mutex);
  1025. #endif
  1026. }
  1027. STARPU_PTHREAD_MUTEX_UNLOCK_SCHED(&worker->sched_mutex);
  1028. }
  1029. /* The scheduling policy may put tasks directly into a worker's local queue so
  1030. * that it is not always necessary to create its own queue when the local queue
  1031. * is sufficient. If "back" not null, the task is put at the back of the queue
  1032. * where the worker will pop tasks first. Setting "back" to 0 therefore ensures
  1033. * a FIFO ordering. */
  1034. int starpu_push_local_task(int workerid, struct starpu_task *task, int prio)
  1035. {
  1036. struct _starpu_worker *worker = _starpu_get_worker_struct(workerid);
  1037. return _starpu_push_local_task(worker, task, prio);
  1038. }
  1039. void _starpu_print_idle_time()
  1040. {
  1041. if(!starpu_idle_file)
  1042. return;
  1043. double all_idle = 0.0;
  1044. int i = 0;
  1045. for(i = 0; i < STARPU_NMAXWORKERS; i++)
  1046. all_idle += idle[i];
  1047. FILE *f;
  1048. f = fopen(starpu_idle_file, "a");
  1049. if (!f)
  1050. {
  1051. _STARPU_MSG("couldn't open %s: %s\n", starpu_idle_file, strerror(errno));
  1052. }
  1053. else
  1054. {
  1055. fprintf(f, "%lf \n", all_idle);
  1056. fclose(f);
  1057. }
  1058. }
  1059. void starpu_sched_task_break(struct starpu_task *task)
  1060. {
  1061. _STARPU_TASK_BREAK_ON(task, sched);
  1062. }