sched_policy.c 22 KB

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