sched_policy.c 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740
  1. /* StarPU --- Runtime system for heterogeneous multicore architectures.
  2. *
  3. * Copyright (C) 2010-2012 Université de Bordeaux 1
  4. * Copyright (C) 2010-2012 Centre National de la Recherche Scientifique
  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 <pthread.h>
  19. #include <starpu.h>
  20. #include <common/config.h>
  21. #include <common/utils.h>
  22. #include <core/sched_policy.h>
  23. #include <profiling/profiling.h>
  24. #include <common/barrier.h>
  25. static int use_prefetch = 0;
  26. int starpu_get_prefetch_flag(void)
  27. {
  28. return use_prefetch;
  29. }
  30. /*
  31. * Predefined policies
  32. */
  33. extern struct starpu_sched_policy _starpu_sched_ws_policy;
  34. extern struct starpu_sched_policy _starpu_sched_prio_policy;
  35. extern struct starpu_sched_policy _starpu_sched_random_policy;
  36. extern struct starpu_sched_policy _starpu_sched_dm_policy;
  37. extern struct starpu_sched_policy _starpu_sched_dmda_policy;
  38. extern struct starpu_sched_policy _starpu_sched_dmda_ready_policy;
  39. extern struct starpu_sched_policy _starpu_sched_dmda_sorted_policy;
  40. extern struct starpu_sched_policy _starpu_sched_eager_policy;
  41. extern struct starpu_sched_policy _starpu_sched_parallel_heft_policy;
  42. extern struct starpu_sched_policy _starpu_sched_pgreedy_policy;
  43. extern struct starpu_sched_policy _starpu_sched_heft_policy;
  44. static struct starpu_sched_policy *predefined_policies[] =
  45. {
  46. &_starpu_sched_ws_policy,
  47. &_starpu_sched_prio_policy,
  48. &_starpu_sched_dm_policy,
  49. &_starpu_sched_dmda_policy,
  50. &_starpu_sched_heft_policy,
  51. &_starpu_sched_dmda_ready_policy,
  52. &_starpu_sched_dmda_sorted_policy,
  53. &_starpu_sched_random_policy,
  54. &_starpu_sched_eager_policy,
  55. &_starpu_sched_parallel_heft_policy,
  56. &_starpu_sched_pgreedy_policy
  57. };
  58. struct starpu_sched_policy *_starpu_get_sched_policy(struct _starpu_sched_ctx *sched_ctx)
  59. {
  60. return sched_ctx->sched_policy;
  61. }
  62. /*
  63. * Methods to initialize the scheduling policy
  64. */
  65. static void load_sched_policy(struct starpu_sched_policy *sched_policy, struct _starpu_sched_ctx *sched_ctx)
  66. {
  67. STARPU_ASSERT(sched_policy);
  68. #ifdef STARPU_VERBOSE
  69. if (sched_policy->policy_name)
  70. {
  71. if (sched_policy->policy_description)
  72. _STARPU_DEBUG("Use %s scheduler (%s)\n", sched_policy->policy_name, sched_policy->policy_description);
  73. else
  74. _STARPU_DEBUG("Use %s scheduler \n", sched_policy->policy_name);
  75. }
  76. #endif
  77. struct starpu_sched_policy *policy = sched_ctx->sched_policy;
  78. memcpy(&policy, sched_policy, sizeof(policy));
  79. }
  80. static struct starpu_sched_policy *find_sched_policy_from_name(const char *policy_name)
  81. {
  82. if (!policy_name)
  83. return NULL;
  84. unsigned i;
  85. for (i = 0; i < sizeof(predefined_policies)/sizeof(predefined_policies[0]); i++)
  86. {
  87. struct starpu_sched_policy *p;
  88. p = predefined_policies[i];
  89. if (p->policy_name)
  90. {
  91. if (strcmp(policy_name, p->policy_name) == 0)
  92. {
  93. /* we found a policy with the requested name */
  94. return p;
  95. }
  96. }
  97. }
  98. fprintf(stderr, "Warning: scheduling policy \"%s\" was not found, try \"help\" to get a list\n", policy_name);
  99. /* nothing was found */
  100. return NULL;
  101. }
  102. static void display_sched_help_message(void)
  103. {
  104. const char *sched_env = getenv("STARPU_SCHED");
  105. if (sched_env && (strcmp(sched_env, "help") == 0))
  106. {
  107. fprintf(stderr, "STARPU_SCHED can be either of\n");
  108. /* display the description of all predefined policies */
  109. unsigned i;
  110. for (i = 0; i < sizeof(predefined_policies)/sizeof(predefined_policies[0]); i++)
  111. {
  112. struct starpu_sched_policy *p;
  113. p = predefined_policies[i];
  114. fprintf(stderr, "%s\t-> %s\n", p->policy_name, p->policy_description);
  115. }
  116. }
  117. }
  118. static struct starpu_sched_policy *select_sched_policy(struct _starpu_machine_config *config, const char *required_policy)
  119. {
  120. struct starpu_sched_policy *selected_policy = NULL;
  121. struct starpu_conf *user_conf = config->conf;
  122. if(required_policy)
  123. selected_policy = find_sched_policy_from_name(required_policy);
  124. /* First, we check whether the application explicitely gave a scheduling policy or not */
  125. if (!selected_policy && user_conf && (user_conf->sched_policy))
  126. return user_conf->sched_policy;
  127. /* Otherwise, we look if the application specified the name of a policy to load */
  128. const char *sched_pol_name;
  129. sched_pol_name = getenv("STARPU_SCHED");
  130. if (sched_pol_name == NULL && user_conf && user_conf->sched_policy_name)
  131. sched_pol_name = user_conf->sched_policy_name;
  132. if (!selected_policy && sched_pol_name)
  133. selected_policy = find_sched_policy_from_name(sched_pol_name);
  134. /* Perhaps there was no policy that matched the name */
  135. if (selected_policy)
  136. return selected_policy;
  137. /* If no policy was specified, we use the greedy policy as a default */
  138. return &_starpu_sched_eager_policy;
  139. }
  140. void _starpu_init_sched_policy(struct _starpu_machine_config *config, struct _starpu_sched_ctx *sched_ctx, const char *required_policy)
  141. {
  142. /* Perhaps we have to display some help */
  143. display_sched_help_message();
  144. /* Prefetch is activated by default */
  145. use_prefetch = starpu_get_env_number("STARPU_PREFETCH");
  146. if (use_prefetch == -1)
  147. use_prefetch = 1;
  148. /* Set calibrate flag */
  149. _starpu_set_calibrate_flag(config->conf->calibrate);
  150. struct starpu_sched_policy *selected_policy;
  151. selected_policy = select_sched_policy(config, required_policy);
  152. load_sched_policy(selected_policy, sched_ctx);
  153. sched_ctx->sched_policy->init_sched(sched_ctx->id);
  154. }
  155. void _starpu_deinit_sched_policy(struct _starpu_sched_ctx *sched_ctx)
  156. {
  157. struct starpu_sched_policy *policy = sched_ctx->sched_policy;
  158. if (policy->deinit_sched)
  159. policy->deinit_sched(sched_ctx->id);
  160. }
  161. /* Enqueue a task into the list of tasks explicitely attached to a worker. In
  162. * case workerid identifies a combined worker, a task will be enqueued into
  163. * each worker of the combination. */
  164. static int _starpu_push_task_on_specific_worker(struct starpu_task *task, int workerid)
  165. {
  166. int nbasic_workers = (int)starpu_worker_get_count();
  167. /* Is this a basic worker or a combined worker ? */
  168. int is_basic_worker = (workerid < nbasic_workers);
  169. unsigned memory_node;
  170. struct _starpu_worker *worker = NULL;
  171. struct _starpu_combined_worker *combined_worker = NULL;
  172. if (is_basic_worker)
  173. {
  174. worker = _starpu_get_worker_struct(workerid);
  175. memory_node = worker->memory_node;
  176. }
  177. else
  178. {
  179. combined_worker = _starpu_get_combined_worker_struct(workerid);
  180. memory_node = combined_worker->memory_node;
  181. }
  182. if (use_prefetch)
  183. starpu_prefetch_task_input_on_node(task, memory_node);
  184. /* if we push a task on a specific worker, notify all the sched_ctxs the worker belongs to */
  185. unsigned i;
  186. struct _starpu_sched_ctx *sched_ctx;
  187. for(i = 0; i < STARPU_NMAX_SCHED_CTXS; i++)
  188. {
  189. sched_ctx = worker->sched_ctx[i];
  190. if (sched_ctx != NULL && sched_ctx->sched_policy != NULL && sched_ctx->sched_policy->push_task_notify)
  191. {
  192. sched_ctx->sched_policy->push_task_notify(task, workerid);
  193. }
  194. }
  195. #ifdef STARPU_USE_SCHED_CTX_HYPERVISOR
  196. starpu_call_pushed_task_cb(workerid, task->sched_ctx);
  197. #endif //STARPU_USE_SCHED_CTX_HYPERVISOR
  198. if (is_basic_worker)
  199. {
  200. unsigned node = starpu_worker_get_memory_node(workerid);
  201. if (_starpu_task_uses_multiformat_handles(task))
  202. {
  203. unsigned i;
  204. for (i = 0; i < task->cl->nbuffers; i++)
  205. {
  206. struct starpu_task *conversion_task;
  207. starpu_data_handle_t handle;
  208. handle = task->handles[i];
  209. if (!_starpu_handle_needs_conversion_task(handle, node))
  210. continue;
  211. conversion_task = _starpu_create_conversion_task(handle, node);
  212. conversion_task->mf_skip = 1;
  213. conversion_task->execute_on_a_specific_worker = 1;
  214. conversion_task->workerid = workerid;
  215. _starpu_task_submit_conversion_task(conversion_task, workerid);
  216. //_STARPU_DEBUG("Pushing a conversion task\n");
  217. }
  218. for (i = 0; i < task->cl->nbuffers; i++)
  219. task->handles[i]->mf_node = node;
  220. }
  221. if(task->priority > 0)
  222. return _starpu_push_local_task(worker, task, 1);
  223. else
  224. return _starpu_push_local_task(worker, task, 0);
  225. }
  226. else
  227. {
  228. /* This is a combined worker so we create task aliases */
  229. int worker_size = combined_worker->worker_size;
  230. int *combined_workerid = combined_worker->combined_workerid;
  231. int ret = 0;
  232. int i;
  233. struct _starpu_job *j = _starpu_get_job_associated_to_task(task);
  234. j->task_size = worker_size;
  235. j->combined_workerid = workerid;
  236. j->active_task_alias_count = 0;
  237. _STARPU_PTHREAD_BARRIER_INIT(&j->before_work_barrier, NULL, worker_size);
  238. _STARPU_PTHREAD_BARRIER_INIT(&j->after_work_barrier, NULL, worker_size);
  239. int k;
  240. for (k = 0; k < worker_size; k++)
  241. {
  242. struct starpu_task *alias = _starpu_create_task_alias(task);
  243. worker = _starpu_get_worker_struct(combined_workerid[k]);
  244. ret |= _starpu_push_local_task(worker, alias, 0);
  245. }
  246. return ret;
  247. }
  248. }
  249. static int _starpu_nworkers_able_to_execute_task(struct starpu_task *task, struct _starpu_sched_ctx *sched_ctx)
  250. {
  251. int worker = -1, nworkers = 0;
  252. struct worker_collection *workers = sched_ctx->workers;
  253. if(workers->init_cursor)
  254. workers->init_cursor(workers);
  255. while(workers->has_next(workers))
  256. {
  257. worker = workers->get_next(workers);
  258. if (starpu_worker_can_execute_task(worker, task, 0) && starpu_is_ctxs_turn(worker, sched_ctx->id))
  259. nworkers++;
  260. }
  261. if(workers->init_cursor)
  262. workers->deinit_cursor(workers);
  263. return nworkers;
  264. }
  265. /* the generic interface that call the proper underlying implementation */
  266. int _starpu_push_task(struct _starpu_job *j)
  267. {
  268. struct starpu_task *task = j->task;
  269. struct _starpu_sched_ctx *sched_ctx = _starpu_get_sched_ctx_struct(task->sched_ctx);
  270. int workerid = starpu_worker_get_id();
  271. unsigned nworkers = 0;
  272. if(!sched_ctx->is_initial_sched)
  273. {
  274. /*if there are workers in the ctx that are not able to execute tasks
  275. we consider the ctx empty */
  276. nworkers = _starpu_nworkers_able_to_execute_task(task, sched_ctx);
  277. if(nworkers == 0)
  278. {
  279. if(task->already_pushed)
  280. {
  281. _STARPU_PTHREAD_MUTEX_LOCK(&sched_ctx->empty_ctx_mutex);
  282. starpu_task_list_push_back(&sched_ctx->empty_ctx_tasks, task);
  283. _STARPU_PTHREAD_MUTEX_UNLOCK(&sched_ctx->empty_ctx_mutex);
  284. return -1;
  285. }
  286. else
  287. {
  288. _STARPU_PTHREAD_MUTEX_LOCK(&sched_ctx->empty_ctx_mutex);
  289. task->already_pushed = 1;
  290. starpu_task_list_push_front(&sched_ctx->empty_ctx_tasks, task);
  291. _STARPU_PTHREAD_MUTEX_UNLOCK(&sched_ctx->empty_ctx_mutex);
  292. return 0;
  293. }
  294. }
  295. }
  296. _STARPU_LOG_IN();
  297. _starpu_increment_nready_tasks();
  298. task->status = STARPU_TASK_READY;
  299. _starpu_profiling_set_task_push_start_time(task);
  300. /* in case there is no codelet associated to the task (that's a control
  301. * task), we directly execute its callback and enforce the
  302. * corresponding dependencies */
  303. if (task->cl == NULL)
  304. {
  305. _starpu_handle_job_termination(j, -1);
  306. _STARPU_LOG_OUT_TAG("handle_job_termination");
  307. return 0;
  308. }
  309. int ret;
  310. if (STARPU_UNLIKELY(task->execute_on_a_specific_worker))
  311. {
  312. ret = _starpu_push_task_on_specific_worker(task, task->workerid);
  313. }
  314. else
  315. {
  316. STARPU_ASSERT(sched_ctx->sched_policy->push_task);
  317. ret = sched_ctx->sched_policy->push_task(task);
  318. if(ret == -1)
  319. {
  320. fprintf(stderr, "repush task \n");
  321. _starpu_decrement_nready_tasks();
  322. ret = _starpu_push_task(j);
  323. }
  324. }
  325. _starpu_profiling_set_task_push_end_time(task);
  326. _STARPU_LOG_OUT();
  327. return ret;
  328. }
  329. /*
  330. * Given a handle that needs to be converted in order to be used on the given
  331. * node, returns a task that takes care of the conversion.
  332. */
  333. struct starpu_task *_starpu_create_conversion_task(starpu_data_handle_t handle,
  334. unsigned int node)
  335. {
  336. struct starpu_task *conversion_task;
  337. struct starpu_multiformat_interface *format_interface;
  338. enum starpu_node_kind node_kind;
  339. conversion_task = starpu_task_create();
  340. conversion_task->synchronous = 0;
  341. conversion_task->handles[0] = handle;
  342. /* The node does not really matter here */
  343. format_interface = (struct starpu_multiformat_interface *) starpu_data_get_interface_on_node(handle, 0);
  344. node_kind = starpu_node_get_kind(node);
  345. _starpu_spin_lock(&handle->header_lock);
  346. handle->refcnt++;
  347. handle->busy_count++;
  348. _starpu_spin_unlock(&handle->header_lock);
  349. switch(node_kind)
  350. {
  351. case STARPU_CPU_RAM:
  352. switch (starpu_node_get_kind(handle->mf_node))
  353. {
  354. case STARPU_CPU_RAM:
  355. STARPU_ASSERT(0);
  356. #ifdef STARPU_USE_CUDA
  357. case STARPU_CUDA_RAM:
  358. {
  359. struct starpu_multiformat_data_interface_ops *mf_ops;
  360. mf_ops = (struct starpu_multiformat_data_interface_ops *) handle->ops->get_mf_ops(format_interface);
  361. conversion_task->cl = mf_ops->cuda_to_cpu_cl;
  362. break;
  363. }
  364. #endif
  365. #ifdef STARPU_USE_OPENCL
  366. case STARPU_OPENCL_RAM:
  367. {
  368. struct starpu_multiformat_data_interface_ops *mf_ops;
  369. mf_ops = (struct starpu_multiformat_data_interface_ops *) handle->ops->get_mf_ops(format_interface);
  370. conversion_task->cl = mf_ops->opencl_to_cpu_cl;
  371. break;
  372. }
  373. #endif
  374. default:
  375. fprintf(stderr, "Oops : %u\n", handle->mf_node);
  376. STARPU_ASSERT(0);
  377. }
  378. break;
  379. #ifdef STARPU_USE_CUDA
  380. case STARPU_CUDA_RAM:
  381. {
  382. struct starpu_multiformat_data_interface_ops *mf_ops;
  383. mf_ops = (struct starpu_multiformat_data_interface_ops *) handle->ops->get_mf_ops(format_interface);
  384. conversion_task->cl = mf_ops->cpu_to_cuda_cl;
  385. break;
  386. }
  387. #endif
  388. #ifdef STARPU_USE_OPENCL
  389. case STARPU_OPENCL_RAM:
  390. {
  391. struct starpu_multiformat_data_interface_ops *mf_ops;
  392. mf_ops = (struct starpu_multiformat_data_interface_ops *) handle->ops->get_mf_ops(format_interface);
  393. conversion_task->cl = mf_ops->cpu_to_opencl_cl;
  394. break;
  395. }
  396. #endif
  397. case STARPU_SPU_LS: /* Not supported */
  398. default:
  399. STARPU_ASSERT(0);
  400. }
  401. conversion_task->cl->modes[0] = STARPU_RW;
  402. return conversion_task;
  403. }
  404. struct _starpu_sched_ctx* _get_next_sched_ctx_to_pop_into(struct _starpu_worker *worker)
  405. {
  406. double max_time_on_ctx = starpu_get_max_time_worker_on_ctx();
  407. /* if(max_time_on_ctx != -1.0 && starpu_are_overlapping_ctxs_on_worker(worker->workerid)) */
  408. /* { */
  409. /* unsigned current_active_ctx = worker->active_ctx; */
  410. /* // current_time[worker->workerid][current_active_ctx] += predicted; */
  411. /* if(current_time[worker->workerid][current_active_ctx] >= max_time_on_ctx) */
  412. /* { */
  413. /* current_time[worker->workerid][current_active_ctx] = 0.0; */
  414. /* starpu_set_turn_to_other_ctx(worker->workerid, current_active_ctx); */
  415. /* } */
  416. /* return worker->active_ctx; */
  417. /* } */
  418. /* else */
  419. {
  420. struct _starpu_sched_ctx *sched_ctx, *good_sched_ctx = NULL;
  421. int smallest_counter = worker->nsched_ctxs;
  422. unsigned i;
  423. for(i = 0; i < STARPU_NMAX_SCHED_CTXS; i++)
  424. {
  425. sched_ctx = worker->sched_ctx[i];
  426. if(sched_ctx != NULL && sched_ctx->id != STARPU_NMAX_SCHED_CTXS &&
  427. sched_ctx->pop_counter[worker->workerid] < worker->nsched_ctxs &&
  428. smallest_counter > sched_ctx->pop_counter[worker->workerid])
  429. {
  430. good_sched_ctx = sched_ctx;
  431. smallest_counter = sched_ctx->pop_counter[worker->workerid];
  432. }
  433. }
  434. if(good_sched_ctx == NULL)
  435. {
  436. for(i = 0; i < STARPU_NMAX_SCHED_CTXS; i++)
  437. {
  438. sched_ctx = worker->sched_ctx[i];
  439. if(sched_ctx != NULL && sched_ctx->id != STARPU_NMAX_SCHED_CTXS)
  440. sched_ctx->pop_counter[worker->workerid] = 0;
  441. }
  442. return _get_next_sched_ctx_to_pop_into(worker);
  443. }
  444. return good_sched_ctx;
  445. }
  446. }
  447. struct starpu_task *_starpu_pop_task(struct _starpu_worker *worker)
  448. {
  449. struct starpu_task *task;
  450. int worker_id;
  451. unsigned node;
  452. /* We can't tell in advance which task will be picked up, so we measure
  453. * a timestamp, and will attribute it afterwards to the task. */
  454. int profiling = starpu_profiling_status_get();
  455. struct timespec pop_start_time;
  456. if (profiling)
  457. _starpu_clock_gettime(&pop_start_time);
  458. pick:
  459. _STARPU_PTHREAD_MUTEX_LOCK(&worker->sched_mutex);
  460. /* perhaps there is some local task to be executed first */
  461. task = _starpu_pop_local_task(worker);
  462. _STARPU_PTHREAD_MUTEX_UNLOCK(&worker->sched_mutex);
  463. /* get tasks from the stacks of the strategy */
  464. if(!task)
  465. {
  466. struct _starpu_sched_ctx *sched_ctx;
  467. pthread_mutex_t *sched_ctx_mutex;
  468. int been_here[STARPU_NMAX_SCHED_CTXS];
  469. int i;
  470. for(i = 0; i < STARPU_NMAX_SCHED_CTXS; i++)
  471. been_here[i] = 0;
  472. while(!task)
  473. {
  474. if(worker->nsched_ctxs == 1)
  475. sched_ctx = _starpu_get_initial_sched_ctx();
  476. else
  477. sched_ctx = _get_next_sched_ctx_to_pop_into(worker);
  478. if(sched_ctx != NULL && sched_ctx->id != STARPU_NMAX_SCHED_CTXS)
  479. {
  480. sched_ctx_mutex = _starpu_get_sched_mutex(sched_ctx, worker->workerid);
  481. if(sched_ctx_mutex != NULL)
  482. {
  483. _STARPU_PTHREAD_MUTEX_LOCK(sched_ctx_mutex);
  484. if (sched_ctx->sched_policy && sched_ctx->sched_policy->pop_task)
  485. task = sched_ctx->sched_policy->pop_task(sched_ctx->id);
  486. _STARPU_PTHREAD_MUTEX_UNLOCK(sched_ctx_mutex);
  487. }
  488. }
  489. if((!task && sched_ctx->pop_counter[worker->workerid] == 0 && been_here[sched_ctx->id]) || worker->nsched_ctxs == 1)
  490. break;
  491. been_here[sched_ctx->id] = 1;
  492. sched_ctx->pop_counter[worker->workerid]++;
  493. }
  494. }
  495. #ifdef STARPU_USE_SCHED_CTX_HYPERVISOR
  496. struct _starpu_sched_ctx *sched_ctx = NULL;
  497. struct starpu_performance_counters *perf_counters = NULL;
  498. int j;
  499. for(j = 0; j < STARPU_NMAX_SCHED_CTXS; j++)
  500. {
  501. sched_ctx = worker->sched_ctx[j];
  502. if(sched_ctx != NULL && sched_ctx->id != 0)
  503. {
  504. perf_counters = sched_ctx->perf_counters;
  505. if(perf_counters != NULL && perf_counters->notify_idle_cycle && perf_counters->notify_idle_end)
  506. {
  507. if(!task)
  508. perf_counters->notify_idle_cycle(sched_ctx->id, worker->workerid, 1.0);
  509. else
  510. perf_counters->notify_idle_end(sched_ctx->id, worker->workerid);
  511. }
  512. }
  513. }
  514. #endif //STARPU_USE_SCHED_CTX_HYPERVISOR
  515. if (!task)
  516. goto profiling;
  517. /* Make sure we do not bother with all the multiformat-specific code if
  518. * it is not necessary. */
  519. if (!_starpu_task_uses_multiformat_handles(task))
  520. goto profiling;
  521. /* This is either a conversion task, or a regular task for which the
  522. * conversion tasks have already been created and submitted */
  523. if (task->mf_skip)
  524. goto profiling;
  525. worker_id = starpu_worker_get_id();
  526. if (!starpu_worker_can_execute_task(worker_id, task, 0))
  527. return task;
  528. node = starpu_worker_get_memory_node(worker_id);
  529. /*
  530. * We do have a task that uses multiformat handles. Let's create the
  531. * required conversion tasks.
  532. */
  533. unsigned i;
  534. for (i = 0; i < task->cl->nbuffers; i++)
  535. {
  536. struct starpu_task *conversion_task;
  537. starpu_data_handle_t handle;
  538. handle = task->handles[i];
  539. if (!_starpu_handle_needs_conversion_task(handle, node))
  540. continue;
  541. conversion_task = _starpu_create_conversion_task(handle, node);
  542. conversion_task->mf_skip = 1;
  543. conversion_task->execute_on_a_specific_worker = 1;
  544. conversion_task->workerid = worker_id;
  545. /*
  546. * Next tasks will need to know where these handles have gone.
  547. */
  548. handle->mf_node = node;
  549. _starpu_task_submit_conversion_task(conversion_task, worker_id);
  550. }
  551. task->mf_skip = 1;
  552. starpu_task_list_push_front(&worker->local_tasks, task);
  553. goto pick;
  554. profiling:
  555. if (profiling && task)
  556. {
  557. struct starpu_task_profiling_info *profiling_info;
  558. profiling_info = task->profiling_info;
  559. /* The task may have been created before profiling was enabled,
  560. * so we check if the profiling_info structure is available
  561. * even though we already tested if profiling is enabled. */
  562. if (profiling_info)
  563. {
  564. memcpy(&profiling_info->pop_start_time,
  565. &pop_start_time, sizeof(struct timespec));
  566. _starpu_clock_gettime(&profiling_info->pop_end_time);
  567. }
  568. }
  569. return task;
  570. }
  571. struct starpu_task *_starpu_pop_every_task(struct _starpu_sched_ctx *sched_ctx)
  572. {
  573. STARPU_ASSERT(sched_ctx->sched_policy->pop_every_task);
  574. /* TODO set profiling info */
  575. if(sched_ctx->sched_policy->pop_every_task)
  576. return sched_ctx->sched_policy->pop_every_task(sched_ctx->id);
  577. return NULL;
  578. }
  579. void _starpu_sched_pre_exec_hook(struct starpu_task *task)
  580. {
  581. struct _starpu_sched_ctx *sched_ctx = _starpu_get_sched_ctx_struct(task->sched_ctx);
  582. if (sched_ctx->sched_policy->pre_exec_hook)
  583. sched_ctx->sched_policy->pre_exec_hook(task);
  584. }
  585. void _starpu_sched_post_exec_hook(struct starpu_task *task)
  586. {
  587. struct _starpu_sched_ctx *sched_ctx = _starpu_get_sched_ctx_struct(task->sched_ctx);
  588. #ifdef STARPU_USE_SCHED_CTX_HYPERVISOR
  589. if(task->hypervisor_tag > 0 && sched_ctx != NULL &&
  590. sched_ctx->id != 0 && sched_ctx->perf_counters != NULL)
  591. sched_ctx->perf_counters->notify_post_exec_hook(sched_ctx->id, task->hypervisor_tag);
  592. #endif //STARPU_USE_SCHED_CTX_HYPERVISOR
  593. if (sched_ctx->sched_policy->post_exec_hook)
  594. sched_ctx->sched_policy->post_exec_hook(task);
  595. }
  596. void _starpu_wait_on_sched_event(void)
  597. {
  598. struct _starpu_worker *worker = _starpu_get_local_worker_key();
  599. _STARPU_PTHREAD_MUTEX_LOCK(&worker->sched_mutex);
  600. _starpu_handle_all_pending_node_data_requests(worker->memory_node);
  601. if (_starpu_machine_is_running())
  602. {
  603. #ifndef STARPU_NON_BLOCKING_DRIVERS
  604. _STARPU_PTHREAD_COND_WAIT(worker->sched_cond,
  605. worker->sched_mutex);
  606. #endif
  607. }
  608. _STARPU_PTHREAD_MUTEX_UNLOCK(&worker->sched_mutex);
  609. }
  610. /* The scheduling policy may put tasks directly into a worker's local queue so
  611. * that it is not always necessary to create its own queue when the local queue
  612. * is sufficient. If "back" not null, the task is put at the back of the queue
  613. * where the worker will pop tasks first. Setting "back" to 0 therefore ensures
  614. * a FIFO ordering. */
  615. int starpu_push_local_task(int workerid, struct starpu_task *task, int back)
  616. {
  617. struct _starpu_worker *worker = _starpu_get_worker_struct(workerid);
  618. return _starpu_push_local_task(worker, task, back);
  619. }