sched_policy.c 30 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012
  1. /* StarPU --- Runtime system for heterogeneous multicore architectures.
  2. *
  3. * Copyright (C) 2010-2015 Université de Bordeaux
  4. * Copyright (C) 2010-2015 CNRS
  5. * Copyright (C) 2011 INRIA
  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 <common/config.h>
  20. #include <common/utils.h>
  21. #include <core/sched_policy.h>
  22. #include <profiling/profiling.h>
  23. #include <common/barrier.h>
  24. #include <core/debug.h>
  25. static int use_prefetch = 0;
  26. double idle[STARPU_NMAXWORKERS];
  27. double idle_start[STARPU_NMAXWORKERS];
  28. int starpu_get_prefetch_flag(void)
  29. {
  30. return use_prefetch;
  31. }
  32. static struct starpu_sched_policy *predefined_policies[] =
  33. {
  34. &_starpu_sched_modular_eager_policy,
  35. &_starpu_sched_modular_eager_prefetching_policy,
  36. &_starpu_sched_modular_prio_policy,
  37. &_starpu_sched_modular_prio_prefetching_policy,
  38. &_starpu_sched_modular_random_policy,
  39. &_starpu_sched_modular_random_prio_policy,
  40. &_starpu_sched_modular_random_prefetching_policy,
  41. &_starpu_sched_modular_random_prio_prefetching_policy,
  42. //&_starpu_sched_modular_ws_policy,
  43. &_starpu_sched_modular_heft_policy,
  44. &_starpu_sched_modular_heft2_policy,
  45. &_starpu_sched_eager_policy,
  46. &_starpu_sched_prio_policy,
  47. &_starpu_sched_random_policy,
  48. &_starpu_sched_lws_policy,
  49. &_starpu_sched_ws_policy,
  50. &_starpu_sched_dm_policy,
  51. &_starpu_sched_dmda_policy,
  52. &_starpu_sched_dmda_ready_policy,
  53. &_starpu_sched_dmda_sorted_policy,
  54. &_starpu_sched_dmda_sorted_decision_policy,
  55. &_starpu_sched_parallel_heft_policy,
  56. &_starpu_sched_peager_policy,
  57. &_starpu_sched_heteroprio_policy,
  58. NULL
  59. };
  60. struct starpu_sched_policy **starpu_sched_get_predefined_policies()
  61. {
  62. return predefined_policies;
  63. }
  64. struct starpu_sched_policy *_starpu_get_sched_policy(struct _starpu_sched_ctx *sched_ctx)
  65. {
  66. return sched_ctx->sched_policy;
  67. }
  68. /*
  69. * Methods to initialize the scheduling policy
  70. */
  71. static void load_sched_policy(struct starpu_sched_policy *sched_policy, struct _starpu_sched_ctx *sched_ctx)
  72. {
  73. STARPU_ASSERT(sched_policy);
  74. #ifdef STARPU_VERBOSE
  75. if (sched_policy->policy_name)
  76. {
  77. if (sched_policy->policy_description)
  78. _STARPU_DEBUG("Use %s scheduler (%s)\n", sched_policy->policy_name, sched_policy->policy_description);
  79. else
  80. _STARPU_DEBUG("Use %s scheduler \n", sched_policy->policy_name);
  81. }
  82. #endif
  83. struct starpu_sched_policy *policy = sched_ctx->sched_policy;
  84. memcpy(policy, sched_policy, sizeof(*policy));
  85. }
  86. static struct starpu_sched_policy *find_sched_policy_from_name(const char *policy_name)
  87. {
  88. if (!policy_name)
  89. return NULL;
  90. if (strncmp(policy_name, "heft", 5) == 0)
  91. {
  92. _STARPU_DISP("Warning: heft is now called \"dmda\".\n");
  93. return &_starpu_sched_dmda_policy;
  94. }
  95. struct starpu_sched_policy **policy;
  96. for(policy=predefined_policies ; *policy!=NULL ; policy++)
  97. {
  98. struct starpu_sched_policy *p = *policy;
  99. if (p->policy_name)
  100. {
  101. if (strcmp(policy_name, p->policy_name) == 0)
  102. {
  103. /* we found a policy with the requested name */
  104. return p;
  105. }
  106. }
  107. }
  108. if (strcmp(policy_name, "help") != 0)
  109. fprintf(stderr, "Warning: scheduling policy \"%s\" was not found, try \"help\" to get a list\n", policy_name);
  110. /* nothing was found */
  111. return NULL;
  112. }
  113. static void display_sched_help_message(void)
  114. {
  115. const char *sched_env = starpu_getenv("STARPU_SCHED");
  116. if (sched_env && (strcmp(sched_env, "help") == 0))
  117. {
  118. /* display the description of all predefined policies */
  119. struct starpu_sched_policy **policy;
  120. fprintf(stderr, "\nThe variable STARPU_SCHED can be set to one of the following strings:\n");
  121. for(policy=predefined_policies ; *policy!=NULL ; policy++)
  122. {
  123. struct starpu_sched_policy *p = *policy;
  124. fprintf(stderr, "%-30s\t-> %s\n", p->policy_name, p->policy_description);
  125. }
  126. fprintf(stderr, "\n");
  127. }
  128. }
  129. struct starpu_sched_policy *_starpu_select_sched_policy(struct _starpu_machine_config *config, const char *required_policy)
  130. {
  131. struct starpu_sched_policy *selected_policy = NULL;
  132. struct starpu_conf *user_conf = &config->conf;
  133. if(required_policy)
  134. selected_policy = find_sched_policy_from_name(required_policy);
  135. /* First, we check whether the application explicitely gave a scheduling policy or not */
  136. if (!selected_policy && user_conf && (user_conf->sched_policy))
  137. return user_conf->sched_policy;
  138. /* Otherwise, we look if the application specified the name of a policy to load */
  139. const char *sched_pol_name;
  140. sched_pol_name = starpu_getenv("STARPU_SCHED");
  141. if (sched_pol_name == NULL && user_conf && user_conf->sched_policy_name)
  142. sched_pol_name = user_conf->sched_policy_name;
  143. if (!selected_policy && sched_pol_name)
  144. selected_policy = find_sched_policy_from_name(sched_pol_name);
  145. /* Perhaps there was no policy that matched the name */
  146. if (selected_policy)
  147. return selected_policy;
  148. /* If no policy was specified, we use the greedy policy as a default */
  149. return &_starpu_sched_eager_policy;
  150. }
  151. void _starpu_init_sched_policy(struct _starpu_machine_config *config, struct _starpu_sched_ctx *sched_ctx, struct starpu_sched_policy *selected_policy)
  152. {
  153. /* Perhaps we have to display some help */
  154. display_sched_help_message();
  155. /* Prefetch is activated by default */
  156. use_prefetch = starpu_get_env_number("STARPU_PREFETCH");
  157. if (use_prefetch == -1)
  158. use_prefetch = 1;
  159. /* Set calibrate flag */
  160. _starpu_set_calibrate_flag(config->conf.calibrate);
  161. load_sched_policy(selected_policy, sched_ctx);
  162. _STARPU_TRACE_WORKER_SCHEDULING_PUSH;
  163. sched_ctx->sched_policy->init_sched(sched_ctx->id);
  164. _STARPU_TRACE_WORKER_SCHEDULING_POP;
  165. }
  166. void _starpu_deinit_sched_policy(struct _starpu_sched_ctx *sched_ctx)
  167. {
  168. struct starpu_sched_policy *policy = sched_ctx->sched_policy;
  169. if (policy->deinit_sched)
  170. {
  171. _STARPU_TRACE_WORKER_SCHEDULING_PUSH;
  172. policy->deinit_sched(sched_ctx->id);
  173. _STARPU_TRACE_WORKER_SCHEDULING_POP;
  174. }
  175. }
  176. static void _starpu_push_task_on_specific_worker_notify_sched(struct starpu_task *task, struct _starpu_worker *worker, int workerid, int perf_workerid)
  177. {
  178. /* if we push a task on a specific worker, notify all the sched_ctxs the worker belongs to */
  179. struct _starpu_sched_ctx *sched_ctx;
  180. struct _starpu_sched_ctx_elt *e = NULL;
  181. struct _starpu_sched_ctx_list_iterator list_it;
  182. _starpu_sched_ctx_list_iterator_init(worker->sched_ctx_list, &list_it);
  183. while (_starpu_sched_ctx_list_iterator_has_next(&list_it))
  184. {
  185. e = _starpu_sched_ctx_list_iterator_get_next(&list_it);
  186. sched_ctx = _starpu_get_sched_ctx_struct(e->sched_ctx);
  187. if (sched_ctx->sched_policy != NULL && sched_ctx->sched_policy->push_task_notify)
  188. {
  189. _STARPU_TRACE_WORKER_SCHEDULING_PUSH;
  190. sched_ctx->sched_policy->push_task_notify(task, workerid, perf_workerid, sched_ctx->id);
  191. _STARPU_TRACE_WORKER_SCHEDULING_POP;
  192. }
  193. }
  194. }
  195. /* Enqueue a task into the list of tasks explicitely attached to a worker. In
  196. * case workerid identifies a combined worker, a task will be enqueued into
  197. * each worker of the combination. */
  198. static int _starpu_push_task_on_specific_worker(struct starpu_task *task, int workerid)
  199. {
  200. int nbasic_workers = (int)starpu_worker_get_count();
  201. /* Is this a basic worker or a combined worker ? */
  202. int is_basic_worker = (workerid < nbasic_workers);
  203. unsigned memory_node;
  204. struct _starpu_worker *worker = NULL;
  205. struct _starpu_combined_worker *combined_worker = NULL;
  206. if (is_basic_worker)
  207. {
  208. worker = _starpu_get_worker_struct(workerid);
  209. memory_node = worker->memory_node;
  210. }
  211. else
  212. {
  213. combined_worker = _starpu_get_combined_worker_struct(workerid);
  214. memory_node = combined_worker->memory_node;
  215. }
  216. if (use_prefetch)
  217. starpu_prefetch_task_input_on_node(task, memory_node);
  218. if (is_basic_worker)
  219. _starpu_push_task_on_specific_worker_notify_sched(task, worker, workerid, workerid);
  220. else
  221. {
  222. /* Notify all workers of the combined worker */
  223. int worker_size = combined_worker->worker_size;
  224. int *combined_workerid = combined_worker->combined_workerid;
  225. int j;
  226. for (j = 0; j < worker_size; j++)
  227. {
  228. int subworkerid = combined_workerid[j];
  229. _starpu_push_task_on_specific_worker_notify_sched(task, _starpu_get_worker_struct(subworkerid), subworkerid, workerid);
  230. }
  231. }
  232. #ifdef STARPU_USE_SC_HYPERVISOR
  233. starpu_sched_ctx_call_pushed_task_cb(workerid, task->sched_ctx);
  234. #endif //STARPU_USE_SC_HYPERVISOR
  235. unsigned i;
  236. if (is_basic_worker)
  237. {
  238. unsigned node = starpu_worker_get_memory_node(workerid);
  239. if (_starpu_task_uses_multiformat_handles(task))
  240. {
  241. unsigned nbuffers = STARPU_TASK_GET_NBUFFERS(task);
  242. for (i = 0; i < nbuffers; i++)
  243. {
  244. struct starpu_task *conversion_task;
  245. starpu_data_handle_t handle;
  246. handle = STARPU_TASK_GET_HANDLE(task, i);
  247. if (!_starpu_handle_needs_conversion_task(handle, node))
  248. continue;
  249. conversion_task = _starpu_create_conversion_task(handle, node);
  250. conversion_task->mf_skip = 1;
  251. conversion_task->execute_on_a_specific_worker = 1;
  252. conversion_task->workerid = workerid;
  253. _starpu_task_submit_conversion_task(conversion_task, workerid);
  254. //_STARPU_DEBUG("Pushing a conversion task\n");
  255. }
  256. for (i = 0; i < nbuffers; i++)
  257. {
  258. starpu_data_handle_t handle = STARPU_TASK_GET_HANDLE(task, i);
  259. handle->mf_node = node;
  260. }
  261. }
  262. // if(task->sched_ctx != _starpu_get_initial_sched_ctx()->id)
  263. if(task->priority > 0)
  264. return _starpu_push_local_task(worker, task, 1);
  265. else
  266. return _starpu_push_local_task(worker, task, 0);
  267. }
  268. else
  269. {
  270. /* This is a combined worker so we create task aliases */
  271. int worker_size = combined_worker->worker_size;
  272. int *combined_workerid = combined_worker->combined_workerid;
  273. int ret = 0;
  274. struct _starpu_job *job = _starpu_get_job_associated_to_task(task);
  275. job->task_size = worker_size;
  276. job->combined_workerid = workerid;
  277. job->active_task_alias_count = 0;
  278. STARPU_PTHREAD_BARRIER_INIT(&job->before_work_barrier, NULL, worker_size);
  279. STARPU_PTHREAD_BARRIER_INIT(&job->after_work_barrier, NULL, worker_size);
  280. /* Note: we have to call that early, or else the task may have
  281. * disappeared already */
  282. starpu_push_task_end(task);
  283. int j;
  284. for (j = 0; j < worker_size; j++)
  285. {
  286. struct starpu_task *alias = starpu_task_dup(task);
  287. worker = _starpu_get_worker_struct(combined_workerid[j]);
  288. ret |= _starpu_push_local_task(worker, alias, 0);
  289. }
  290. return ret;
  291. }
  292. }
  293. /* the generic interface that call the proper underlying implementation */
  294. int _starpu_push_task(struct _starpu_job *j)
  295. {
  296. if(j->task->prologue_callback_func)
  297. j->task->prologue_callback_func(j->task->prologue_callback_arg);
  298. return _starpu_repush_task(j);
  299. }
  300. int _starpu_repush_task(struct _starpu_job *j)
  301. {
  302. struct starpu_task *task = j->task;
  303. struct _starpu_sched_ctx *sched_ctx = _starpu_get_sched_ctx_struct(task->sched_ctx);
  304. unsigned nworkers = 0;
  305. int ret;
  306. _STARPU_LOG_IN();
  307. unsigned can_push = _starpu_increment_nready_tasks_of_sched_ctx(task->sched_ctx, task->flops, task);
  308. task->status = STARPU_TASK_READY;
  309. #ifdef HAVE_AYUDAME_H
  310. if (AYU_event)
  311. {
  312. intptr_t id = -1;
  313. AYU_event(AYU_ADDTASKTOQUEUE, j->job_id, &id);
  314. }
  315. #endif
  316. /* if the context does not have any workers save the tasks in a temp list */
  317. if(!sched_ctx->is_initial_sched)
  318. {
  319. /*if there are workers in the ctx that are not able to execute tasks
  320. we consider the ctx empty */
  321. nworkers = _starpu_nworkers_able_to_execute_task(task, sched_ctx);
  322. if(nworkers == 0)
  323. {
  324. STARPU_PTHREAD_MUTEX_LOCK(&sched_ctx->empty_ctx_mutex);
  325. starpu_task_list_push_front(&sched_ctx->empty_ctx_tasks, task);
  326. STARPU_PTHREAD_MUTEX_UNLOCK(&sched_ctx->empty_ctx_mutex);
  327. #ifdef STARPU_USE_SC_HYPERVISOR
  328. if(sched_ctx != NULL && sched_ctx->id != 0 && sched_ctx->perf_counters != NULL
  329. && sched_ctx->perf_counters->notify_empty_ctx)
  330. {
  331. _STARPU_TRACE_HYPERVISOR_BEGIN();
  332. sched_ctx->perf_counters->notify_empty_ctx(sched_ctx->id, task);
  333. _STARPU_TRACE_HYPERVISOR_END();
  334. }
  335. #endif
  336. return 0;
  337. }
  338. }
  339. if(!can_push)
  340. return 0;
  341. /* in case there is no codelet associated to the task (that's a control
  342. * task), we directly execute its callback and enforce the
  343. * corresponding dependencies */
  344. if (task->cl == NULL)
  345. {
  346. if(task->prologue_callback_pop_func)
  347. task->prologue_callback_pop_func(task->prologue_callback_pop_arg);
  348. _starpu_handle_job_termination(j);
  349. _STARPU_LOG_OUT_TAG("handle_job_termination");
  350. return 0;
  351. }
  352. ret = _starpu_push_task_to_workers(task);
  353. if (ret == -EAGAIN)
  354. /* pushed to empty context, that's fine */
  355. ret = 0;
  356. return ret;
  357. }
  358. int _starpu_push_task_to_workers(struct starpu_task *task)
  359. {
  360. struct _starpu_sched_ctx *sched_ctx = _starpu_get_sched_ctx_struct(task->sched_ctx);
  361. unsigned nworkers = 0;
  362. _STARPU_TRACE_JOB_PUSH(task, task->priority > 0);
  363. /* if the contexts still does not have workers put the task back to its place in
  364. the empty ctx list */
  365. if(!sched_ctx->is_initial_sched)
  366. {
  367. /*if there are workers in the ctx that are not able to execute tasks
  368. we consider the ctx empty */
  369. nworkers = _starpu_nworkers_able_to_execute_task(task, sched_ctx);
  370. if (nworkers == 0)
  371. {
  372. STARPU_PTHREAD_MUTEX_LOCK(&sched_ctx->empty_ctx_mutex);
  373. starpu_task_list_push_back(&sched_ctx->empty_ctx_tasks, task);
  374. STARPU_PTHREAD_MUTEX_UNLOCK(&sched_ctx->empty_ctx_mutex);
  375. #ifdef STARPU_USE_SC_HYPERVISOR
  376. if(sched_ctx != NULL && sched_ctx->id != 0 && sched_ctx->perf_counters != NULL
  377. && sched_ctx->perf_counters->notify_empty_ctx)
  378. {
  379. _STARPU_TRACE_HYPERVISOR_BEGIN();
  380. sched_ctx->perf_counters->notify_empty_ctx(sched_ctx->id, task);
  381. _STARPU_TRACE_HYPERVISOR_END();
  382. }
  383. #endif
  384. return -EAGAIN;
  385. }
  386. }
  387. _starpu_profiling_set_task_push_start_time(task);
  388. int ret = 0;
  389. if (STARPU_UNLIKELY(task->execute_on_a_specific_worker))
  390. {
  391. unsigned node = starpu_worker_get_memory_node(task->workerid);
  392. if (starpu_get_prefetch_flag())
  393. starpu_prefetch_task_input_on_node(task, node);
  394. ret = _starpu_push_task_on_specific_worker(task, task->workerid);
  395. }
  396. else
  397. {
  398. struct _starpu_machine_config *config = _starpu_get_machine_config();
  399. /* When a task can only be executed on a given arch and we have
  400. * only one memory node for that arch, we can systematically
  401. * prefetch before the scheduling decision. */
  402. if (starpu_get_prefetch_flag())
  403. {
  404. if (task->cl->where == STARPU_CPU && config->cpus_nodeid >= 0)
  405. starpu_prefetch_task_input_on_node(task, config->cpus_nodeid);
  406. else if (task->cl->where == STARPU_CUDA && config->cuda_nodeid >= 0)
  407. starpu_prefetch_task_input_on_node(task, config->cuda_nodeid);
  408. else if (task->cl->where == STARPU_OPENCL && config->opencl_nodeid >= 0)
  409. starpu_prefetch_task_input_on_node(task, config->opencl_nodeid);
  410. else if (task->cl->where == STARPU_MIC && config->mic_nodeid >= 0)
  411. starpu_prefetch_task_input_on_node(task, config->mic_nodeid);
  412. else if (task->cl->where == STARPU_SCC && config->scc_nodeid >= 0)
  413. starpu_prefetch_task_input_on_node(task, config->scc_nodeid);
  414. }
  415. if(!sched_ctx->sched_policy)
  416. {
  417. if(!sched_ctx->awake_workers)
  418. ret = _starpu_push_task_on_specific_worker(task, sched_ctx->main_master);
  419. else
  420. {
  421. struct starpu_worker_collection *workers = sched_ctx->workers;
  422. struct _starpu_job *job = _starpu_get_job_associated_to_task(task);
  423. job->task_size = workers->nworkers;
  424. job->combined_workerid = -1; // workerid; its a ctx not combined worker
  425. job->active_task_alias_count = 0;
  426. STARPU_PTHREAD_BARRIER_INIT(&job->before_work_barrier, NULL, workers->nworkers);
  427. STARPU_PTHREAD_BARRIER_INIT(&job->after_work_barrier, NULL, workers->nworkers);
  428. /* Note: we have to call that early, or else the task may have
  429. * disappeared already */
  430. starpu_push_task_end(task);
  431. unsigned workerid;
  432. struct starpu_sched_ctx_iterator it;
  433. if(workers->init_iterator)
  434. workers->init_iterator(workers, &it);
  435. while(workers->has_next(workers, &it))
  436. {
  437. workerid = workers->get_next(workers, &it);
  438. struct starpu_task *alias = starpu_task_dup(task);
  439. ret |= _starpu_push_task_on_specific_worker(alias, workerid);
  440. }
  441. }
  442. }
  443. else
  444. {
  445. STARPU_ASSERT(sched_ctx->sched_policy->push_task);
  446. /* check out if there are any workers in the context */
  447. starpu_pthread_rwlock_t *changing_ctx_mutex = _starpu_sched_ctx_get_changing_ctx_mutex(sched_ctx->id);
  448. STARPU_PTHREAD_RWLOCK_RDLOCK(changing_ctx_mutex);
  449. nworkers = starpu_sched_ctx_get_nworkers(sched_ctx->id);
  450. if (nworkers == 0)
  451. ret = -1;
  452. else
  453. {
  454. _STARPU_TRACE_WORKER_SCHEDULING_PUSH;
  455. ret = sched_ctx->sched_policy->push_task(task);
  456. _STARPU_TRACE_WORKER_SCHEDULING_POP;
  457. }
  458. STARPU_PTHREAD_RWLOCK_UNLOCK(changing_ctx_mutex);
  459. }
  460. if(ret == -1)
  461. {
  462. fprintf(stderr, "repush task \n");
  463. _STARPU_TRACE_JOB_POP(task, task->priority > 0);
  464. ret = _starpu_push_task_to_workers(task);
  465. }
  466. }
  467. /* Note: from here, the task might have been destroyed already! */
  468. _STARPU_LOG_OUT();
  469. return ret;
  470. }
  471. /* This is called right after the scheduler has pushed a task to a queue
  472. * but just before releasing mutexes: we need the task to still be alive!
  473. */
  474. int starpu_push_task_end(struct starpu_task *task)
  475. {
  476. _STARPU_TRACE_JOB_POP(task, task->priority > 0);
  477. _starpu_profiling_set_task_push_end_time(task);
  478. task->scheduled = 1;
  479. return 0;
  480. }
  481. /*
  482. * Given a handle that needs to be converted in order to be used on the given
  483. * node, returns a task that takes care of the conversion.
  484. */
  485. struct starpu_task *_starpu_create_conversion_task(starpu_data_handle_t handle,
  486. unsigned int node)
  487. {
  488. return _starpu_create_conversion_task_for_arch(handle, starpu_node_get_kind(node));
  489. }
  490. struct starpu_task *_starpu_create_conversion_task_for_arch(starpu_data_handle_t handle,
  491. enum starpu_node_kind node_kind)
  492. {
  493. struct starpu_task *conversion_task;
  494. #if defined(STARPU_USE_OPENCL) || defined(STARPU_USE_CUDA) || defined(STARPU_USE_MIC) || defined(STARPU_USE_SCC) || defined(STARPU_SIMGRID)
  495. struct starpu_multiformat_interface *format_interface;
  496. #endif
  497. conversion_task = starpu_task_create();
  498. conversion_task->name = "conversion_task";
  499. conversion_task->synchronous = 0;
  500. STARPU_TASK_SET_HANDLE(conversion_task, handle, 0);
  501. #if defined(STARPU_USE_OPENCL) || defined(STARPU_USE_CUDA) || defined(STARPU_USE_MIC) || defined(STARPU_USE_SCC) || defined(STARPU_SIMGRID)
  502. /* The node does not really matter here */
  503. format_interface = (struct starpu_multiformat_interface *) starpu_data_get_interface_on_node(handle, STARPU_MAIN_RAM);
  504. #endif
  505. _starpu_spin_lock(&handle->header_lock);
  506. handle->refcnt++;
  507. handle->busy_count++;
  508. _starpu_spin_unlock(&handle->header_lock);
  509. switch(node_kind)
  510. {
  511. case STARPU_CPU_RAM:
  512. case STARPU_SCC_RAM:
  513. case STARPU_SCC_SHM:
  514. switch (starpu_node_get_kind(handle->mf_node))
  515. {
  516. case STARPU_CPU_RAM:
  517. case STARPU_SCC_RAM:
  518. case STARPU_SCC_SHM:
  519. STARPU_ABORT();
  520. #if defined(STARPU_USE_CUDA) || defined(STARPU_SIMGRID)
  521. case STARPU_CUDA_RAM:
  522. {
  523. struct starpu_multiformat_data_interface_ops *mf_ops;
  524. mf_ops = (struct starpu_multiformat_data_interface_ops *) handle->ops->get_mf_ops(format_interface);
  525. conversion_task->cl = mf_ops->cuda_to_cpu_cl;
  526. break;
  527. }
  528. #endif
  529. #if defined(STARPU_USE_OPENCL) || defined(STARPU_SIMGRID)
  530. case STARPU_OPENCL_RAM:
  531. {
  532. struct starpu_multiformat_data_interface_ops *mf_ops;
  533. mf_ops = (struct starpu_multiformat_data_interface_ops *) handle->ops->get_mf_ops(format_interface);
  534. conversion_task->cl = mf_ops->opencl_to_cpu_cl;
  535. break;
  536. }
  537. #endif
  538. #ifdef STARPU_USE_MIC
  539. case STARPU_MIC_RAM:
  540. {
  541. struct starpu_multiformat_data_interface_ops *mf_ops;
  542. mf_ops = (struct starpu_multiformat_data_interface_ops *) handle->ops->get_mf_ops(format_interface);
  543. conversion_task->cl = mf_ops->mic_to_cpu_cl;
  544. break;
  545. }
  546. #endif
  547. default:
  548. _STARPU_ERROR("Oops : %u\n", handle->mf_node);
  549. }
  550. break;
  551. #if defined(STARPU_USE_CUDA) || defined(STARPU_SIMGRID)
  552. case STARPU_CUDA_RAM:
  553. {
  554. struct starpu_multiformat_data_interface_ops *mf_ops;
  555. mf_ops = (struct starpu_multiformat_data_interface_ops *) handle->ops->get_mf_ops(format_interface);
  556. conversion_task->cl = mf_ops->cpu_to_cuda_cl;
  557. break;
  558. }
  559. #endif
  560. #if defined(STARPU_USE_OPENCL) || defined(STARPU_SIMGRID)
  561. case STARPU_OPENCL_RAM:
  562. {
  563. struct starpu_multiformat_data_interface_ops *mf_ops;
  564. mf_ops = (struct starpu_multiformat_data_interface_ops *) handle->ops->get_mf_ops(format_interface);
  565. conversion_task->cl = mf_ops->cpu_to_opencl_cl;
  566. break;
  567. }
  568. #endif
  569. #ifdef STARPU_USE_MIC
  570. case STARPU_MIC_RAM:
  571. {
  572. struct starpu_multiformat_data_interface_ops *mf_ops;
  573. mf_ops = (struct starpu_multiformat_data_interface_ops *) handle->ops->get_mf_ops(format_interface);
  574. conversion_task->cl = mf_ops->cpu_to_mic_cl;
  575. break;
  576. }
  577. #endif
  578. default:
  579. STARPU_ABORT();
  580. }
  581. STARPU_TASK_SET_MODE(conversion_task, STARPU_RW, 0);
  582. return conversion_task;
  583. }
  584. static
  585. struct _starpu_sched_ctx* _get_next_sched_ctx_to_pop_into(struct _starpu_worker *worker)
  586. {
  587. struct _starpu_sched_ctx_elt *e = NULL;
  588. struct _starpu_sched_ctx_list_iterator list_it;
  589. int found = 0;
  590. _starpu_sched_ctx_list_iterator_init(worker->sched_ctx_list, &list_it);
  591. while (_starpu_sched_ctx_list_iterator_has_next(&list_it))
  592. {
  593. e = _starpu_sched_ctx_list_iterator_get_next(&list_it);
  594. if (e->task_number > 0)
  595. return _starpu_get_sched_ctx_struct(e->sched_ctx);
  596. }
  597. _starpu_sched_ctx_list_iterator_init(worker->sched_ctx_list, &list_it);
  598. while (_starpu_sched_ctx_list_iterator_has_next(&list_it))
  599. {
  600. e = _starpu_sched_ctx_list_iterator_get_next(&list_it);
  601. if (e->last_poped)
  602. {
  603. e->last_poped = 0;
  604. if (_starpu_sched_ctx_list_iterator_has_next(&list_it))
  605. {
  606. e = _starpu_sched_ctx_list_iterator_get_next(&list_it);
  607. found = 1;
  608. }
  609. break;
  610. }
  611. }
  612. if (!found)
  613. e = worker->sched_ctx_list->head;
  614. e->last_poped = 1;
  615. return _starpu_get_sched_ctx_struct(e->sched_ctx);
  616. }
  617. struct starpu_task *_starpu_pop_task(struct _starpu_worker *worker)
  618. {
  619. struct starpu_task *task;
  620. int worker_id;
  621. unsigned node;
  622. /* We can't tell in advance which task will be picked up, so we measure
  623. * a timestamp, and will attribute it afterwards to the task. */
  624. int profiling = starpu_profiling_status_get();
  625. struct timespec pop_start_time;
  626. if (profiling)
  627. _starpu_clock_gettime(&pop_start_time);
  628. pick:
  629. /* perhaps there is some local task to be executed first */
  630. task = _starpu_pop_local_task(worker);
  631. /* get tasks from the stacks of the strategy */
  632. if(!task)
  633. {
  634. struct _starpu_sched_ctx *sched_ctx ;
  635. #ifndef STARPU_NON_BLOCKING_DRIVERS
  636. int been_here[STARPU_NMAX_SCHED_CTXS];
  637. int i;
  638. for(i = 0; i < STARPU_NMAX_SCHED_CTXS; i++)
  639. been_here[i] = 0;
  640. while(!task)
  641. #endif
  642. {
  643. if(worker->nsched_ctxs == 1)
  644. sched_ctx = _starpu_get_initial_sched_ctx();
  645. else
  646. {
  647. while(1)
  648. {
  649. /** Caution
  650. * If you use multiple contexts your scheduler *needs*
  651. * to update the variable task_number of the ctx list.
  652. * In order to get the best performances.
  653. * This is done using functions :
  654. * starpu_sched_ctx_list_task_counters_increment...(...)
  655. * starpu_sched_ctx_list_task_counters_decrement...(...)
  656. **/
  657. sched_ctx = _get_next_sched_ctx_to_pop_into(worker);
  658. if(worker->removed_from_ctx[sched_ctx->id] == 1 && worker->shares_tasks_lists[sched_ctx->id] == 1)
  659. {
  660. _starpu_worker_gets_out_of_ctx(sched_ctx->id, worker);
  661. worker->removed_from_ctx[sched_ctx->id] = 0;
  662. sched_ctx = NULL;
  663. }
  664. else
  665. break;
  666. }
  667. }
  668. if(sched_ctx && sched_ctx->id != STARPU_NMAX_SCHED_CTXS)
  669. {
  670. if (sched_ctx->sched_policy && sched_ctx->sched_policy->pop_task)
  671. {
  672. task = sched_ctx->sched_policy->pop_task(sched_ctx->id);
  673. }
  674. }
  675. if(!task)
  676. {
  677. /* it doesn't matter if it shares tasks list or not in the scheduler,
  678. if it does not have any task to pop just get it out of here */
  679. /* however if it shares a task list it will be removed as soon as he
  680. finishes this job (in handle_job_termination) */
  681. if(worker->removed_from_ctx[sched_ctx->id])
  682. {
  683. _starpu_worker_gets_out_of_ctx(sched_ctx->id, worker);
  684. worker->removed_from_ctx[sched_ctx->id] = 0;
  685. }
  686. #ifdef STARPU_USE_SC_HYPERVISOR
  687. if(worker->pop_ctx_priority)
  688. {
  689. struct starpu_sched_ctx_performance_counters *perf_counters = sched_ctx->perf_counters;
  690. if(sched_ctx->id != 0 && perf_counters != NULL && perf_counters->notify_idle_cycle && _starpu_sched_ctx_allow_hypervisor(sched_ctx->id))
  691. {
  692. // _STARPU_TRACE_HYPERVISOR_BEGIN();
  693. perf_counters->notify_idle_cycle(sched_ctx->id, worker->workerid, 1.0);
  694. // _STARPU_TRACE_HYPERVISOR_END();
  695. }
  696. }
  697. #endif //STARPU_USE_SC_HYPERVISOR
  698. #ifndef STARPU_NON_BLOCKING_DRIVERS
  699. if(been_here[sched_ctx->id] || worker->nsched_ctxs == 1)
  700. break;
  701. been_here[sched_ctx->id] = 1;
  702. #endif
  703. }
  704. }
  705. }
  706. if (!task)
  707. {
  708. idle_start[worker->workerid] = starpu_timing_now();
  709. return NULL;
  710. }
  711. if(idle_start[worker->workerid] != 0.0)
  712. {
  713. double idle_end = starpu_timing_now();
  714. idle[worker->workerid] += (idle_end - idle_start[worker->workerid]);
  715. idle_start[worker->workerid] = 0.0;
  716. }
  717. #ifdef STARPU_USE_SC_HYPERVISOR
  718. struct _starpu_sched_ctx *sched_ctx = _starpu_get_sched_ctx_struct(task->sched_ctx);
  719. struct starpu_sched_ctx_performance_counters *perf_counters = sched_ctx->perf_counters;
  720. if(sched_ctx->id != 0 && perf_counters != NULL && perf_counters->notify_poped_task && _starpu_sched_ctx_allow_hypervisor(sched_ctx->id))
  721. {
  722. // _STARPU_TRACE_HYPERVISOR_BEGIN();
  723. perf_counters->notify_poped_task(task->sched_ctx, worker->workerid);
  724. // _STARPU_TRACE_HYPERVISOR_END();
  725. }
  726. #endif //STARPU_USE_SC_HYPERVISOR
  727. /* Make sure we do not bother with all the multiformat-specific code if
  728. * it is not necessary. */
  729. if (!_starpu_task_uses_multiformat_handles(task))
  730. goto profiling;
  731. /* This is either a conversion task, or a regular task for which the
  732. * conversion tasks have already been created and submitted */
  733. if (task->mf_skip)
  734. goto profiling;
  735. /*
  736. * This worker may not be able to execute this task. In this case, we
  737. * should return the task anyway. It will be pushed back almost immediatly.
  738. * This way, we avoid computing and executing the conversions tasks.
  739. * Here, we do not care about what implementation is used.
  740. */
  741. worker_id = starpu_worker_get_id();
  742. if (!starpu_worker_can_execute_task_first_impl(worker_id, task, NULL))
  743. return task;
  744. node = starpu_worker_get_memory_node(worker_id);
  745. /*
  746. * We do have a task that uses multiformat handles. Let's create the
  747. * required conversion tasks.
  748. */
  749. STARPU_PTHREAD_MUTEX_UNLOCK(&worker->sched_mutex);
  750. unsigned i;
  751. unsigned nbuffers = STARPU_TASK_GET_NBUFFERS(task);
  752. for (i = 0; i < nbuffers; i++)
  753. {
  754. struct starpu_task *conversion_task;
  755. starpu_data_handle_t handle;
  756. handle = STARPU_TASK_GET_HANDLE(task, i);
  757. if (!_starpu_handle_needs_conversion_task(handle, node))
  758. continue;
  759. conversion_task = _starpu_create_conversion_task(handle, node);
  760. conversion_task->mf_skip = 1;
  761. conversion_task->execute_on_a_specific_worker = 1;
  762. conversion_task->workerid = worker_id;
  763. /*
  764. * Next tasks will need to know where these handles have gone.
  765. */
  766. handle->mf_node = node;
  767. _starpu_task_submit_conversion_task(conversion_task, worker_id);
  768. }
  769. task->mf_skip = 1;
  770. starpu_task_list_push_back(&worker->local_tasks, task);
  771. STARPU_PTHREAD_MUTEX_LOCK(&worker->sched_mutex);
  772. goto pick;
  773. profiling:
  774. if (profiling)
  775. {
  776. struct starpu_profiling_task_info *profiling_info;
  777. profiling_info = task->profiling_info;
  778. /* The task may have been created before profiling was enabled,
  779. * so we check if the profiling_info structure is available
  780. * even though we already tested if profiling is enabled. */
  781. if (profiling_info)
  782. {
  783. memcpy(&profiling_info->pop_start_time,
  784. &pop_start_time, sizeof(struct timespec));
  785. _starpu_clock_gettime(&profiling_info->pop_end_time);
  786. }
  787. }
  788. if(task->prologue_callback_pop_func)
  789. task->prologue_callback_pop_func(task->prologue_callback_pop_arg);
  790. return task;
  791. }
  792. struct starpu_task *_starpu_pop_every_task(struct _starpu_sched_ctx *sched_ctx)
  793. {
  794. struct starpu_task *task = NULL;
  795. if(sched_ctx->sched_policy)
  796. {
  797. STARPU_ASSERT(sched_ctx->sched_policy->pop_every_task);
  798. /* TODO set profiling info */
  799. if(sched_ctx->sched_policy->pop_every_task)
  800. {
  801. _STARPU_TRACE_WORKER_SCHEDULING_PUSH;
  802. task = sched_ctx->sched_policy->pop_every_task(sched_ctx->id);
  803. _STARPU_TRACE_WORKER_SCHEDULING_POP;
  804. }
  805. }
  806. return task;
  807. }
  808. void _starpu_sched_pre_exec_hook(struct starpu_task *task)
  809. {
  810. unsigned sched_ctx_id = starpu_sched_ctx_get_ctx_for_task(task);
  811. struct _starpu_sched_ctx *sched_ctx = _starpu_get_sched_ctx_struct(sched_ctx_id);
  812. if (sched_ctx->sched_policy && sched_ctx->sched_policy->pre_exec_hook)
  813. {
  814. _STARPU_TRACE_WORKER_SCHEDULING_PUSH;
  815. sched_ctx->sched_policy->pre_exec_hook(task);
  816. _STARPU_TRACE_WORKER_SCHEDULING_POP;
  817. }
  818. }
  819. void _starpu_sched_post_exec_hook(struct starpu_task *task)
  820. {
  821. unsigned sched_ctx_id = starpu_sched_ctx_get_ctx_for_task(task);
  822. struct _starpu_sched_ctx *sched_ctx = _starpu_get_sched_ctx_struct(sched_ctx_id);
  823. if (sched_ctx->sched_policy && sched_ctx->sched_policy->post_exec_hook)
  824. {
  825. _STARPU_TRACE_WORKER_SCHEDULING_PUSH;
  826. sched_ctx->sched_policy->post_exec_hook(task);
  827. _STARPU_TRACE_WORKER_SCHEDULING_POP;
  828. }
  829. }
  830. void _starpu_wait_on_sched_event(void)
  831. {
  832. struct _starpu_worker *worker = _starpu_get_local_worker_key();
  833. STARPU_PTHREAD_MUTEX_LOCK(&worker->sched_mutex);
  834. _starpu_handle_all_pending_node_data_requests(worker->memory_node);
  835. if (_starpu_machine_is_running())
  836. {
  837. #ifndef STARPU_NON_BLOCKING_DRIVERS
  838. STARPU_PTHREAD_COND_WAIT(&worker->sched_cond,
  839. &worker->sched_mutex);
  840. #endif
  841. }
  842. STARPU_PTHREAD_MUTEX_UNLOCK(&worker->sched_mutex);
  843. }
  844. /* The scheduling policy may put tasks directly into a worker's local queue so
  845. * that it is not always necessary to create its own queue when the local queue
  846. * is sufficient. If "back" not null, the task is put at the back of the queue
  847. * where the worker will pop tasks first. Setting "back" to 0 therefore ensures
  848. * a FIFO ordering. */
  849. int starpu_push_local_task(int workerid, struct starpu_task *task, int prio)
  850. {
  851. struct _starpu_worker *worker = _starpu_get_worker_struct(workerid);
  852. return _starpu_push_local_task(worker, task, prio);
  853. }
  854. void _starpu_print_idle_time()
  855. {
  856. double all_idle = 0.0;
  857. int i = 0;
  858. for(i = 0; i < STARPU_NMAXWORKERS; i++)
  859. all_idle += idle[i];
  860. FILE *f;
  861. const char *sched_env = starpu_getenv("STARPU_IDLE_FILE");
  862. if(!sched_env)
  863. sched_env = "starpu_idle_microsec.log";
  864. f = fopen(sched_env, "a");
  865. if (!f)
  866. {
  867. fprintf(stderr, "couldn't open %s: %s\n", sched_env, strerror(errno));
  868. }
  869. else
  870. {
  871. fprintf(f, "%lf \n", all_idle);
  872. fclose(f);
  873. }
  874. }