sched_policy.c 20 KB

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