sched_policy.c 28 KB

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