deque_modeling_policy_data_aware.c 40 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273
  1. /* StarPU --- Runtime system for heterogeneous multicore architectures.
  2. *
  3. * Copyright (C) 2010-2015 Université de Bordeaux
  4. * Copyright (C) 2010, 2011, 2012, 2013, 2015 CNRS
  5. * Copyright (C) 2011 Télécom-SudParis
  6. * Copyright (C) 2011-2012 INRIA
  7. *
  8. * StarPU is free software; you can redistribute it and/or modify
  9. * it under the terms of the GNU Lesser General Public License as published by
  10. * the Free Software Foundation; either version 2.1 of the License, or (at
  11. * your option) any later version.
  12. *
  13. * StarPU is distributed in the hope that it will be useful, but
  14. * WITHOUT ANY WARRANTY; without even the implied warranty of
  15. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
  16. *
  17. * See the GNU Lesser General Public License in COPYING.LGPL for more details.
  18. */
  19. /* Distributed queues using performance modeling to assign tasks */
  20. #include <starpu_config.h>
  21. #include <starpu_scheduler.h>
  22. #include <common/fxt.h>
  23. #include <core/task.h>
  24. #include <sched_policies/fifo_queues.h>
  25. #include <limits.h>
  26. #ifdef HAVE_AYUDAME_H
  27. #include <Ayudame.h>
  28. #endif
  29. #ifndef DBL_MIN
  30. #define DBL_MIN __DBL_MIN__
  31. #endif
  32. #ifndef DBL_MAX
  33. #define DBL_MAX __DBL_MAX__
  34. #endif
  35. struct _starpu_dmda_data
  36. {
  37. double alpha;
  38. double beta;
  39. double _gamma;
  40. double idle_power;
  41. struct _starpu_fifo_taskq **queue_array;
  42. long int total_task_cnt;
  43. long int ready_task_cnt;
  44. int num_priorities;
  45. };
  46. /* The dmda scheduling policy uses
  47. *
  48. * alpha * T_computation + beta * T_communication + gamma * Consumption
  49. *
  50. * Here are the default values of alpha, beta, gamma
  51. */
  52. #define _STARPU_SCHED_ALPHA_DEFAULT 1.0
  53. #define _STARPU_SCHED_BETA_DEFAULT 1.0
  54. #define _STARPU_SCHED_GAMMA_DEFAULT 1000.0
  55. #ifdef STARPU_USE_TOP
  56. static double alpha = _STARPU_SCHED_ALPHA_DEFAULT;
  57. static double beta = _STARPU_SCHED_BETA_DEFAULT;
  58. static double _gamma = _STARPU_SCHED_GAMMA_DEFAULT;
  59. static double idle_power = 0.0;
  60. static const float alpha_minimum=0;
  61. static const float alpha_maximum=10.0;
  62. static const float beta_minimum=0;
  63. static const float beta_maximum=10.0;
  64. static const float gamma_minimum=0;
  65. static const float gamma_maximum=10000.0;
  66. static const float idle_power_minimum=0;
  67. static const float idle_power_maximum=10000.0;
  68. #endif /* !STARPU_USE_TOP */
  69. static int count_non_ready_buffers(struct starpu_task *task, unsigned node)
  70. {
  71. int cnt = 0;
  72. unsigned nbuffers = STARPU_TASK_GET_NBUFFERS(task);
  73. unsigned index;
  74. for (index = 0; index < nbuffers; index++)
  75. {
  76. starpu_data_handle_t handle;
  77. handle = STARPU_TASK_GET_HANDLE(task, index);
  78. int is_valid;
  79. starpu_data_query_status(handle, node, NULL, &is_valid, NULL);
  80. if (!is_valid)
  81. cnt++;
  82. }
  83. return cnt;
  84. }
  85. #ifdef STARPU_USE_TOP
  86. static void param_modified(struct starpu_top_param* d)
  87. {
  88. #ifdef STARPU_DEVEL
  89. #warning FIXME: get sched ctx to get alpha/beta/gamma/idle values
  90. #endif
  91. /* Just to show parameter modification. */
  92. fprintf(stderr,
  93. "%s has been modified : "
  94. "alpha=%f|beta=%f|gamma=%f|idle_power=%f !\n",
  95. d->name, alpha,beta,_gamma, idle_power);
  96. }
  97. #endif /* !STARPU_USE_TOP */
  98. static int _normalize_prio(int priority, int num_priorities, unsigned sched_ctx_id)
  99. {
  100. int min = starpu_sched_ctx_get_min_priority(sched_ctx_id);
  101. int max = starpu_sched_ctx_get_max_priority(sched_ctx_id);
  102. return ((num_priorities-1)/(max-min)) * (priority - min);
  103. }
  104. static struct starpu_task *_starpu_fifo_pop_first_ready_task(struct _starpu_fifo_taskq *fifo_queue, unsigned node, int num_priorities)
  105. {
  106. struct starpu_task *task = NULL, *current;
  107. if (fifo_queue->ntasks == 0)
  108. return NULL;
  109. if (fifo_queue->ntasks > 0)
  110. {
  111. fifo_queue->ntasks--;
  112. task = starpu_task_list_front(&fifo_queue->taskq);
  113. if (STARPU_UNLIKELY(!task))
  114. return NULL;
  115. int first_task_priority = task->priority;
  116. current = task;
  117. int non_ready_best = INT_MAX;
  118. while (current)
  119. {
  120. int priority = current->priority;
  121. if (priority >= first_task_priority)
  122. {
  123. int non_ready = count_non_ready_buffers(current, node);
  124. if (non_ready < non_ready_best)
  125. {
  126. non_ready_best = non_ready;
  127. task = current;
  128. if (non_ready == 0)
  129. break;
  130. }
  131. }
  132. current = current->next;
  133. }
  134. if(num_priorities != -1)
  135. {
  136. int i;
  137. int task_prio = _normalize_prio(task->priority, num_priorities, task->sched_ctx);
  138. for(i = 0; i <= task_prio; i++)
  139. fifo_queue->ntasks_per_priority[i]--;
  140. }
  141. starpu_task_list_erase(&fifo_queue->taskq, task);
  142. }
  143. return task;
  144. }
  145. static struct starpu_task *dmda_pop_ready_task(unsigned sched_ctx_id)
  146. {
  147. struct _starpu_dmda_data *dt = (struct _starpu_dmda_data*)starpu_sched_ctx_get_policy_data(sched_ctx_id);
  148. struct starpu_task *task;
  149. int workerid = starpu_worker_get_id();
  150. struct _starpu_fifo_taskq *fifo = dt->queue_array[workerid];
  151. unsigned node = starpu_worker_get_memory_node(workerid);
  152. task = _starpu_fifo_pop_first_ready_task(fifo, node, dt->num_priorities);
  153. if (task)
  154. {
  155. /* We now start the transfer, get rid of it in the completion
  156. * prediction */
  157. double transfer_model = task->predicted_transfer;
  158. if(!isnan(transfer_model))
  159. {
  160. fifo->exp_len -= transfer_model;
  161. fifo->exp_start = starpu_timing_now() + transfer_model;
  162. fifo->exp_end = fifo->exp_start + fifo->exp_len;
  163. if(dt->num_priorities != -1)
  164. {
  165. int i;
  166. int task_prio = _normalize_prio(task->priority, dt->num_priorities, task->sched_ctx);
  167. for(i = 0; i <= task_prio; i++)
  168. fifo->exp_len_per_priority[i] -= transfer_model;
  169. }
  170. }
  171. starpu_sched_ctx_list_task_counters_decrement(sched_ctx_id, workerid);
  172. #ifdef STARPU_VERBOSE
  173. if (task->cl)
  174. {
  175. int non_ready = count_non_ready_buffers(task, node);
  176. if (non_ready == 0)
  177. dt->ready_task_cnt++;
  178. }
  179. dt->total_task_cnt++;
  180. #endif
  181. }
  182. return task;
  183. }
  184. static struct starpu_task *dmda_pop_task(unsigned sched_ctx_id)
  185. {
  186. struct _starpu_dmda_data *dt = (struct _starpu_dmda_data*)starpu_sched_ctx_get_policy_data(sched_ctx_id);
  187. struct starpu_task *task;
  188. int workerid = starpu_worker_get_id();
  189. struct _starpu_fifo_taskq *fifo = dt->queue_array[workerid];
  190. STARPU_ASSERT_MSG(fifo, "worker %d does not belong to ctx %d anymore.\n", workerid, sched_ctx_id);
  191. task = _starpu_fifo_pop_local_task(fifo);
  192. if (task)
  193. {
  194. double transfer_model = task->predicted_transfer;
  195. /* We now start the transfer, get rid of it in the completion
  196. * prediction */
  197. if(!isnan(transfer_model))
  198. {
  199. double model = task->predicted;
  200. fifo->exp_len -= transfer_model;
  201. fifo->exp_start = starpu_timing_now() + transfer_model+model;
  202. fifo->exp_end = fifo->exp_start + fifo->exp_len;
  203. if(dt->num_priorities != -1)
  204. {
  205. int i;
  206. int task_prio = _normalize_prio(task->priority, dt->num_priorities, task->sched_ctx);
  207. for(i = 0; i <= task_prio; i++)
  208. fifo->exp_len_per_priority[i] -= transfer_model;
  209. }
  210. }
  211. starpu_sched_ctx_list_task_counters_decrement(sched_ctx_id, workerid);
  212. #ifdef STARPU_VERBOSE
  213. if (task->cl)
  214. {
  215. int non_ready = count_non_ready_buffers(task, starpu_worker_get_memory_node(workerid));
  216. if (non_ready == 0)
  217. dt->ready_task_cnt++;
  218. }
  219. dt->total_task_cnt++;
  220. #endif
  221. }
  222. return task;
  223. }
  224. static struct starpu_task *dmda_pop_every_task(unsigned sched_ctx_id)
  225. {
  226. struct _starpu_dmda_data *dt = (struct _starpu_dmda_data*)starpu_sched_ctx_get_policy_data(sched_ctx_id);
  227. struct starpu_task *new_list;
  228. int workerid = starpu_worker_get_id();
  229. struct _starpu_fifo_taskq *fifo = dt->queue_array[workerid];
  230. starpu_pthread_mutex_t *sched_mutex;
  231. starpu_pthread_cond_t *sched_cond;
  232. starpu_worker_get_sched_condition(workerid, &sched_mutex, &sched_cond);
  233. STARPU_PTHREAD_MUTEX_LOCK(sched_mutex);
  234. new_list = _starpu_fifo_pop_every_task(fifo, workerid);
  235. STARPU_PTHREAD_MUTEX_UNLOCK(sched_mutex);
  236. starpu_sched_ctx_list_task_counters_reset(sched_ctx_id, workerid);
  237. while (new_list)
  238. {
  239. double transfer_model = new_list->predicted_transfer;
  240. /* We now start the transfer, get rid of it in the completion
  241. * prediction */
  242. if(!isnan(transfer_model))
  243. {
  244. fifo->exp_len -= transfer_model;
  245. fifo->exp_start = starpu_timing_now() + transfer_model;
  246. fifo->exp_end = fifo->exp_start + fifo->exp_len;
  247. if(dt->num_priorities != -1)
  248. {
  249. int i;
  250. for(i = 0; i < new_list->priority; i++)
  251. fifo->exp_len_per_priority[i] -= transfer_model;
  252. }
  253. }
  254. new_list = new_list->next;
  255. }
  256. return new_list;
  257. }
  258. static int push_task_on_best_worker(struct starpu_task *task, int best_workerid,
  259. double predicted, double predicted_transfer,
  260. int prio, unsigned sched_ctx_id)
  261. {
  262. struct _starpu_dmda_data *dt = (struct _starpu_dmda_data*)starpu_sched_ctx_get_policy_data(sched_ctx_id);
  263. /* make sure someone could execute that task ! */
  264. STARPU_ASSERT(best_workerid != -1);
  265. unsigned child_sched_ctx = starpu_sched_ctx_worker_is_master_for_child_ctx(best_workerid, sched_ctx_id);
  266. if(child_sched_ctx != STARPU_NMAX_SCHED_CTXS)
  267. {
  268. starpu_sched_ctx_move_task_to_ctx(task, child_sched_ctx, 0);
  269. starpu_sched_ctx_revert_task_counters(sched_ctx_id, task->flops);
  270. return 0;
  271. }
  272. struct _starpu_fifo_taskq *fifo = dt->queue_array[best_workerid];
  273. starpu_pthread_mutex_t *sched_mutex;
  274. starpu_pthread_cond_t *sched_cond;
  275. starpu_worker_get_sched_condition(best_workerid, &sched_mutex, &sched_cond);
  276. #ifdef STARPU_USE_SC_HYPERVISOR
  277. starpu_sched_ctx_call_pushed_task_cb(best_workerid, sched_ctx_id);
  278. #endif //STARPU_USE_SC_HYPERVISOR
  279. STARPU_PTHREAD_MUTEX_LOCK(sched_mutex);
  280. /* Sometimes workers didn't take the tasks as early as we expected */
  281. fifo->exp_start = isnan(fifo->exp_start) ? starpu_timing_now() : STARPU_MAX(fifo->exp_start, starpu_timing_now());
  282. fifo->exp_end = fifo->exp_start + fifo->exp_len;
  283. if ((starpu_timing_now() + predicted_transfer) < fifo->exp_end)
  284. {
  285. /* We may hope that the transfer will be finished by
  286. * the start of the task. */
  287. predicted_transfer = 0.0;
  288. }
  289. else
  290. {
  291. /* The transfer will not be finished by then, take the
  292. * remainder into account */
  293. predicted_transfer = (starpu_timing_now() + predicted_transfer) - fifo->exp_end;
  294. }
  295. if(!isnan(predicted_transfer))
  296. {
  297. fifo->exp_end += predicted_transfer;
  298. fifo->exp_len += predicted_transfer;
  299. if(dt->num_priorities != -1)
  300. {
  301. int i;
  302. int task_prio = _normalize_prio(task->priority, dt->num_priorities, task->sched_ctx);
  303. for(i = 0; i <= task_prio; i++)
  304. fifo->exp_len_per_priority[i] += predicted_transfer;
  305. }
  306. }
  307. if(!isnan(predicted))
  308. {
  309. fifo->exp_end += predicted;
  310. fifo->exp_len += predicted;
  311. if(dt->num_priorities != -1)
  312. {
  313. int i;
  314. int task_prio = _normalize_prio(task->priority, dt->num_priorities, task->sched_ctx);
  315. for(i = 0; i <= task_prio; i++)
  316. fifo->exp_len_per_priority[i] += predicted;
  317. }
  318. }
  319. STARPU_PTHREAD_MUTEX_UNLOCK(sched_mutex);
  320. task->predicted = predicted;
  321. task->predicted_transfer = predicted_transfer;
  322. #ifdef STARPU_USE_TOP
  323. starpu_top_task_prevision(task, best_workerid,
  324. (unsigned long long)(fifo->exp_end-predicted)/1000,
  325. (unsigned long long)fifo->exp_end/1000);
  326. #endif /* !STARPU_USE_TOP */
  327. if (starpu_get_prefetch_flag())
  328. {
  329. unsigned memory_node = starpu_worker_get_memory_node(best_workerid);
  330. starpu_prefetch_task_input_on_node(task, memory_node);
  331. }
  332. #ifdef HAVE_AYUDAME_H
  333. if (AYU_event)
  334. {
  335. intptr_t id = best_workerid;
  336. AYU_event(AYU_ADDTASKTOQUEUE, _starpu_get_job_associated_to_task(task)->job_id, &id);
  337. }
  338. #endif
  339. int ret = 0;
  340. if (prio)
  341. {
  342. STARPU_PTHREAD_MUTEX_LOCK(sched_mutex);
  343. ret =_starpu_fifo_push_sorted_task(dt->queue_array[best_workerid], task);
  344. if(dt->num_priorities != -1)
  345. {
  346. int i;
  347. int task_prio = _normalize_prio(task->priority, dt->num_priorities, task->sched_ctx);
  348. for(i = 0; i <= task_prio; i++)
  349. dt->queue_array[best_workerid]->ntasks_per_priority[i]++;
  350. }
  351. #if !defined(STARPU_NON_BLOCKING_DRIVERS) || defined(STARPU_SIMGRID)
  352. starpu_wakeup_worker_locked(best_workerid, sched_cond, sched_mutex);
  353. #endif
  354. starpu_push_task_end(task);
  355. STARPU_PTHREAD_MUTEX_UNLOCK(sched_mutex);
  356. }
  357. else
  358. {
  359. STARPU_PTHREAD_MUTEX_LOCK(sched_mutex);
  360. starpu_task_list_push_back (&dt->queue_array[best_workerid]->taskq, task);
  361. dt->queue_array[best_workerid]->ntasks++;
  362. dt->queue_array[best_workerid]->nprocessed++;
  363. #if !defined(STARPU_NON_BLOCKING_DRIVERS) || defined(STARPU_SIMGRID)
  364. starpu_wakeup_worker_locked(best_workerid, sched_cond, sched_mutex);
  365. #endif
  366. starpu_push_task_end(task);
  367. STARPU_PTHREAD_MUTEX_UNLOCK(sched_mutex);
  368. }
  369. starpu_sched_ctx_list_task_counters_increment(sched_ctx_id, best_workerid);
  370. return ret;
  371. }
  372. /* TODO: factorize with dmda!! */
  373. static int _dm_push_task(struct starpu_task *task, unsigned prio, unsigned sched_ctx_id)
  374. {
  375. struct _starpu_dmda_data *dt = (struct _starpu_dmda_data*)starpu_sched_ctx_get_policy_data(sched_ctx_id);
  376. unsigned worker, worker_ctx = 0;
  377. int best = -1;
  378. double best_exp_end = 0.0;
  379. double model_best = 0.0;
  380. double transfer_model_best = 0.0;
  381. int ntasks_best = -1;
  382. double ntasks_best_end = 0.0;
  383. int calibrating = 0;
  384. /* A priori, we know all estimations */
  385. int unknown = 0;
  386. unsigned best_impl = 0;
  387. unsigned nimpl;
  388. unsigned impl_mask;
  389. struct starpu_worker_collection *workers = starpu_sched_ctx_get_worker_collection(sched_ctx_id);
  390. struct starpu_sched_ctx_iterator it;
  391. workers->init_iterator_for_parallel_tasks(workers, &it, task);
  392. while(workers->has_next(workers, &it))
  393. {
  394. worker = workers->get_next(workers, &it);
  395. struct _starpu_fifo_taskq *fifo = dt->queue_array[worker];
  396. unsigned memory_node = starpu_worker_get_memory_node(worker);
  397. struct starpu_perfmodel_arch* perf_arch = starpu_worker_get_perf_archtype(worker, sched_ctx_id);
  398. /* Sometimes workers didn't take the tasks as early as we expected */
  399. double exp_start = isnan(fifo->exp_start) ? starpu_timing_now() : STARPU_MAX(fifo->exp_start, starpu_timing_now());
  400. if (!starpu_worker_can_execute_task_impl(worker, task, &impl_mask))
  401. continue;
  402. for (nimpl = 0; nimpl < STARPU_MAXIMPLEMENTATIONS; nimpl++)
  403. {
  404. if (!(impl_mask & (1U << nimpl)))
  405. {
  406. /* no one on that queue may execute this task */
  407. // worker_ctx++;
  408. continue;
  409. }
  410. double exp_end;
  411. double local_length = starpu_task_expected_length(task, perf_arch, nimpl);
  412. double local_penalty = starpu_task_expected_data_transfer_time(memory_node, task);
  413. double ntasks_end = fifo->ntasks / starpu_worker_get_relative_speedup(perf_arch);
  414. //_STARPU_DEBUG("Scheduler dm: task length (%lf) worker (%u) kernel (%u) \n", local_length,worker,nimpl);
  415. /*
  416. * This implements a default greedy scheduler for the
  417. * case of tasks which have no performance model, or
  418. * whose performance model is not calibrated yet.
  419. *
  420. * It simply uses the number of tasks already pushed to
  421. * the workers, divided by the relative performance of
  422. * a CPU and of a GPU.
  423. *
  424. * This is always computed, but the ntasks_best
  425. * selection is only really used if the task indeed has
  426. * no performance model, or is not calibrated yet.
  427. */
  428. if (ntasks_best == -1
  429. /* Always compute the greedy decision, at least for
  430. * the tasks with no performance model. */
  431. || (!calibrating && ntasks_end < ntasks_best_end)
  432. /* The performance model of this task is not
  433. * calibrated on this worker, try to run it there
  434. * to calibrate it there. */
  435. || (!calibrating && isnan(local_length))
  436. /* the performance model of this task is not
  437. * calibrated on this worker either, rather run it
  438. * there if this one is low on scheduled tasks. */
  439. || (calibrating && isnan(local_length) && ntasks_end < ntasks_best_end)
  440. )
  441. {
  442. ntasks_best_end = ntasks_end;
  443. ntasks_best = worker;
  444. best_impl = nimpl;
  445. }
  446. if (isnan(local_length))
  447. /* we are calibrating, we want to speed-up calibration time
  448. * so we privilege non-calibrated tasks (but still
  449. * greedily distribute them to avoid dumb schedules) */
  450. calibrating = 1;
  451. if (isnan(local_length) || _STARPU_IS_ZERO(local_length))
  452. /* there is no prediction available for that task
  453. * with that arch yet, so switch to a greedy strategy */
  454. unknown = 1;
  455. if (unknown)
  456. continue;
  457. exp_end = exp_start + fifo->exp_len + local_length;
  458. if (best == -1 || exp_end < best_exp_end)
  459. {
  460. /* a better solution was found */
  461. best_exp_end = exp_end;
  462. best = worker;
  463. model_best = local_length;
  464. transfer_model_best = local_penalty;
  465. best_impl = nimpl;
  466. }
  467. }
  468. worker_ctx++;
  469. }
  470. if (unknown)
  471. {
  472. best = ntasks_best;
  473. model_best = 0.0;
  474. transfer_model_best = 0.0;
  475. }
  476. //_STARPU_DEBUG("Scheduler dm: kernel (%u)\n", best_impl);
  477. starpu_task_set_implementation(task, best_impl);
  478. /* we should now have the best worker in variable "best" */
  479. return push_task_on_best_worker(task, best,
  480. model_best, transfer_model_best, prio, sched_ctx_id);
  481. }
  482. /* TODO: factorise CPU computations, expensive with a lot of cores */
  483. static void compute_all_performance_predictions(struct starpu_task *task,
  484. unsigned nworkers,
  485. double local_task_length[nworkers][STARPU_MAXIMPLEMENTATIONS],
  486. double exp_end[nworkers][STARPU_MAXIMPLEMENTATIONS],
  487. double *max_exp_endp,
  488. double *best_exp_endp,
  489. double local_data_penalty[nworkers][STARPU_MAXIMPLEMENTATIONS],
  490. double local_power[nworkers][STARPU_MAXIMPLEMENTATIONS],
  491. int *forced_worker, int *forced_impl, unsigned sched_ctx_id, unsigned sorted_decision)
  492. {
  493. int calibrating = 0;
  494. double max_exp_end = DBL_MIN;
  495. double best_exp_end = DBL_MAX;
  496. int ntasks_best = -1;
  497. int nimpl_best = 0;
  498. double ntasks_best_end = 0.0;
  499. /* A priori, we know all estimations */
  500. int unknown = 0;
  501. unsigned worker, worker_ctx = 0;
  502. unsigned nimpl;
  503. unsigned impl_mask;
  504. int task_prio = 0;
  505. starpu_task_bundle_t bundle = task->bundle;
  506. struct _starpu_dmda_data *dt = (struct _starpu_dmda_data*)starpu_sched_ctx_get_policy_data(sched_ctx_id);
  507. if(sorted_decision && dt->num_priorities != -1)
  508. task_prio = _normalize_prio(task->priority, dt->num_priorities, sched_ctx_id);
  509. struct starpu_worker_collection *workers = starpu_sched_ctx_get_worker_collection(sched_ctx_id);
  510. struct starpu_sched_ctx_iterator it;
  511. workers->init_iterator_for_parallel_tasks(workers, &it, task);
  512. while(workers->has_next(workers, &it))
  513. {
  514. worker = workers->get_next(workers, &it);
  515. struct _starpu_fifo_taskq *fifo = dt->queue_array[worker];
  516. struct starpu_perfmodel_arch* perf_arch = starpu_worker_get_perf_archtype(worker, sched_ctx_id);
  517. unsigned memory_node = starpu_worker_get_memory_node(worker);
  518. /* Sometimes workers didn't take the tasks as early as we expected */
  519. double exp_start = isnan(fifo->exp_start) ? starpu_timing_now() : STARPU_MAX(fifo->exp_start, starpu_timing_now());
  520. if (!starpu_worker_can_execute_task_impl(worker, task, &impl_mask))
  521. continue;
  522. for (nimpl = 0; nimpl < STARPU_MAXIMPLEMENTATIONS; nimpl++)
  523. {
  524. if (!(impl_mask & (1U << nimpl)))
  525. {
  526. /* no one on that queue may execute this task */
  527. continue;
  528. }
  529. STARPU_ASSERT_MSG(fifo != NULL, "worker %d ctx %d\n", worker, sched_ctx_id);
  530. int fifo_ntasks = fifo->ntasks;
  531. double prev_exp_len = fifo->exp_len;
  532. /* consider the priority of the task when deciding on which worker to schedule,
  533. compute the expected_end of the task if it is inserted before other tasks already scheduled */
  534. if(sorted_decision)
  535. {
  536. if(dt->num_priorities != -1)
  537. {
  538. prev_exp_len = fifo->exp_len_per_priority[task_prio];
  539. fifo_ntasks = fifo->ntasks_per_priority[task_prio];
  540. }
  541. else
  542. {
  543. starpu_pthread_mutex_t *sched_mutex;
  544. starpu_pthread_cond_t *sched_cond;
  545. starpu_worker_get_sched_condition(worker, &sched_mutex, &sched_cond);
  546. STARPU_PTHREAD_MUTEX_LOCK(sched_mutex);
  547. prev_exp_len = _starpu_fifo_get_exp_len_prev_task_list(fifo, task, worker, nimpl, &fifo_ntasks);
  548. STARPU_PTHREAD_MUTEX_UNLOCK(sched_mutex);
  549. }
  550. }
  551. exp_end[worker_ctx][nimpl] = exp_start + prev_exp_len;
  552. if (exp_end[worker_ctx][nimpl] > max_exp_end)
  553. max_exp_end = exp_end[worker_ctx][nimpl];
  554. //_STARPU_DEBUG("Scheduler dmda: task length (%lf) worker (%u) kernel (%u) \n", local_task_length[worker][nimpl],worker,nimpl);
  555. if (bundle)
  556. {
  557. /* TODO : conversion time */
  558. local_task_length[worker_ctx][nimpl] = starpu_task_bundle_expected_length(bundle, perf_arch, nimpl);
  559. local_data_penalty[worker_ctx][nimpl] = starpu_task_bundle_expected_data_transfer_time(bundle, memory_node);
  560. local_power[worker_ctx][nimpl] = starpu_task_bundle_expected_power(bundle, perf_arch,nimpl);
  561. }
  562. else
  563. {
  564. local_task_length[worker_ctx][nimpl] = starpu_task_expected_length(task, perf_arch, nimpl);
  565. local_data_penalty[worker_ctx][nimpl] = starpu_task_expected_data_transfer_time(memory_node, task);
  566. local_power[worker_ctx][nimpl] = starpu_task_expected_power(task, perf_arch,nimpl);
  567. double conversion_time = starpu_task_expected_conversion_time(task, perf_arch, nimpl);
  568. if (conversion_time > 0.0)
  569. local_task_length[worker_ctx][nimpl] += conversion_time;
  570. }
  571. double ntasks_end = fifo_ntasks / starpu_worker_get_relative_speedup(perf_arch);
  572. /*
  573. * This implements a default greedy scheduler for the
  574. * case of tasks which have no performance model, or
  575. * whose performance model is not calibrated yet.
  576. *
  577. * It simply uses the number of tasks already pushed to
  578. * the workers, divided by the relative performance of
  579. * a CPU and of a GPU.
  580. *
  581. * This is always computed, but the ntasks_best
  582. * selection is only really used if the task indeed has
  583. * no performance model, or is not calibrated yet.
  584. */
  585. if (ntasks_best == -1
  586. /* Always compute the greedy decision, at least for
  587. * the tasks with no performance model. */
  588. || (!calibrating && ntasks_end < ntasks_best_end)
  589. /* The performance model of this task is not
  590. * calibrated on this worker, try to run it there
  591. * to calibrate it there. */
  592. || (!calibrating && isnan(local_task_length[worker_ctx][nimpl]))
  593. /* the performance model of this task is not
  594. * calibrated on this worker either, rather run it
  595. * there if this one is low on scheduled tasks. */
  596. || (calibrating && isnan(local_task_length[worker_ctx][nimpl]) && ntasks_end < ntasks_best_end)
  597. )
  598. {
  599. ntasks_best_end = ntasks_end;
  600. ntasks_best = worker;
  601. nimpl_best = nimpl;
  602. }
  603. if (isnan(local_task_length[worker_ctx][nimpl]))
  604. /* we are calibrating, we want to speed-up calibration time
  605. * so we privilege non-calibrated tasks (but still
  606. * greedily distribute them to avoid dumb schedules) */
  607. calibrating = 1;
  608. if (isnan(local_task_length[worker_ctx][nimpl])
  609. || _STARPU_IS_ZERO(local_task_length[worker_ctx][nimpl]))
  610. /* there is no prediction available for that task
  611. * with that arch (yet or at all), so switch to a greedy strategy */
  612. unknown = 1;
  613. if (unknown)
  614. continue;
  615. exp_end[worker_ctx][nimpl] = exp_start + prev_exp_len + local_task_length[worker_ctx][nimpl];
  616. if (exp_end[worker_ctx][nimpl] < best_exp_end)
  617. {
  618. /* a better solution was found */
  619. best_exp_end = exp_end[worker_ctx][nimpl];
  620. nimpl_best = nimpl;
  621. }
  622. if (isnan(local_power[worker_ctx][nimpl]))
  623. local_power[worker_ctx][nimpl] = 0.;
  624. }
  625. worker_ctx++;
  626. }
  627. *forced_worker = unknown?ntasks_best:-1;
  628. *forced_impl = unknown?nimpl_best:-1;
  629. *best_exp_endp = best_exp_end;
  630. *max_exp_endp = max_exp_end;
  631. }
  632. static double _dmda_push_task(struct starpu_task *task, unsigned prio, unsigned sched_ctx_id, unsigned simulate, unsigned sorted_decision)
  633. {
  634. /* find the queue */
  635. unsigned worker, worker_ctx = 0;
  636. int best = -1, best_in_ctx = -1;
  637. int selected_impl = 0;
  638. double model_best = 0.0;
  639. double transfer_model_best = 0.0;
  640. /* this flag is set if the corresponding worker is selected because
  641. there is no performance prediction available yet */
  642. int forced_best = -1;
  643. int forced_impl = -1;
  644. struct _starpu_dmda_data *dt = (struct _starpu_dmda_data*)starpu_sched_ctx_get_policy_data(sched_ctx_id);
  645. struct starpu_worker_collection *workers = starpu_sched_ctx_get_worker_collection(sched_ctx_id);
  646. unsigned nworkers_ctx = workers->nworkers;
  647. double local_task_length[nworkers_ctx][STARPU_MAXIMPLEMENTATIONS];
  648. double local_data_penalty[nworkers_ctx][STARPU_MAXIMPLEMENTATIONS];
  649. double local_power[nworkers_ctx][STARPU_MAXIMPLEMENTATIONS];
  650. /* Expected end of this task on the workers */
  651. double exp_end[nworkers_ctx][STARPU_MAXIMPLEMENTATIONS];
  652. /* This is the minimum among the exp_end[] matrix */
  653. double best_exp_end;
  654. /* This is the maximum termination time of already-scheduled tasks over all workers */
  655. double max_exp_end = 0.0;
  656. double fitness[nworkers_ctx][STARPU_MAXIMPLEMENTATIONS];
  657. compute_all_performance_predictions(task,
  658. nworkers_ctx,
  659. local_task_length,
  660. exp_end,
  661. &max_exp_end,
  662. &best_exp_end,
  663. local_data_penalty,
  664. local_power,
  665. &forced_best,
  666. &forced_impl, sched_ctx_id, sorted_decision);
  667. double best_fitness = -1;
  668. unsigned nimpl;
  669. unsigned impl_mask;
  670. if (forced_best == -1)
  671. {
  672. struct starpu_sched_ctx_iterator it;
  673. workers->init_iterator_for_parallel_tasks(workers, &it, task);
  674. while(workers->has_next(workers, &it))
  675. {
  676. worker = workers->get_next(workers, &it);
  677. if (!starpu_worker_can_execute_task_impl(worker, task, &impl_mask))
  678. continue;
  679. for (nimpl = 0; nimpl < STARPU_MAXIMPLEMENTATIONS; nimpl++)
  680. {
  681. if (!(impl_mask & (1U << nimpl)))
  682. {
  683. /* no one on that queue may execute this task */
  684. continue;
  685. }
  686. fitness[worker_ctx][nimpl] = dt->alpha*(exp_end[worker_ctx][nimpl] - best_exp_end)
  687. + dt->beta*(local_data_penalty[worker_ctx][nimpl])
  688. + dt->_gamma*(local_power[worker_ctx][nimpl]);
  689. if (exp_end[worker_ctx][nimpl] > max_exp_end)
  690. {
  691. /* This placement will make the computation
  692. * longer, take into account the idle
  693. * consumption of other cpus */
  694. fitness[worker_ctx][nimpl] += dt->_gamma * dt->idle_power * (exp_end[worker_ctx][nimpl] - max_exp_end) / 1000000.0;
  695. }
  696. if (best == -1 || fitness[worker_ctx][nimpl] < best_fitness)
  697. {
  698. /* we found a better solution */
  699. best_fitness = fitness[worker_ctx][nimpl];
  700. best = worker;
  701. best_in_ctx = worker_ctx;
  702. selected_impl = nimpl;
  703. //_STARPU_DEBUG("best fitness (worker %d) %e = alpha*(%e) + beta(%e) +gamma(%e)\n", worker, best_fitness, exp_end[worker][nimpl] - best_exp_end, local_data_penalty[worker][nimpl], local_power[worker][nimpl]);
  704. }
  705. }
  706. worker_ctx++;
  707. }
  708. }
  709. STARPU_ASSERT(forced_best != -1 || best != -1);
  710. if (forced_best != -1)
  711. {
  712. /* there is no prediction available for that task
  713. * with that arch we want to speed-up calibration time
  714. * so we force this measurement */
  715. best = forced_best;
  716. selected_impl = forced_impl;
  717. model_best = 0.0;
  718. transfer_model_best = 0.0;
  719. }
  720. else if (task->bundle)
  721. {
  722. struct starpu_perfmodel_arch* perf_arch = starpu_worker_get_perf_archtype(best_in_ctx, sched_ctx_id);
  723. unsigned memory_node = starpu_worker_get_memory_node(best);
  724. model_best = starpu_task_expected_length(task, perf_arch, selected_impl);
  725. transfer_model_best = starpu_task_expected_data_transfer_time(memory_node, task);
  726. }
  727. else
  728. {
  729. model_best = local_task_length[best_in_ctx][selected_impl];
  730. transfer_model_best = local_data_penalty[best_in_ctx][selected_impl];
  731. }
  732. //_STARPU_DEBUG("Scheduler dmda: kernel (%u)\n", best_impl);
  733. starpu_task_set_implementation(task, selected_impl);
  734. if(!simulate)
  735. {
  736. /* we should now have the best worker in variable "best" */
  737. return push_task_on_best_worker(task, best, model_best, transfer_model_best, prio, sched_ctx_id);
  738. }
  739. else
  740. {
  741. // double max_len = (max_exp_end - starpu_timing_now());
  742. /* printf("%d: dmda max_exp_end %lf best_exp_end %lf max_len %lf \n", sched_ctx_id, max_exp_end/1000000.0, best_exp_end/1000000.0, max_len/1000000.0); */
  743. return exp_end[best_in_ctx][selected_impl] ;
  744. }
  745. }
  746. static int dmda_push_sorted_decision_task(struct starpu_task *task)
  747. {
  748. return _dmda_push_task(task, 1, task->sched_ctx, 0, 1);
  749. }
  750. static int dmda_push_sorted_task(struct starpu_task *task)
  751. {
  752. #ifdef STARPU_DEVEL
  753. #warning TODO: after defining a scheduling window, use that instead of empty_ctx_tasks
  754. #endif
  755. return _dmda_push_task(task, 1, task->sched_ctx, 0, 0);
  756. }
  757. static int dm_push_task(struct starpu_task *task)
  758. {
  759. return _dm_push_task(task, 0, task->sched_ctx);
  760. }
  761. static int dmda_push_task(struct starpu_task *task)
  762. {
  763. STARPU_ASSERT(task);
  764. return _dmda_push_task(task, 0, task->sched_ctx, 0, 0);
  765. }
  766. static double dmda_simulate_push_task(struct starpu_task *task)
  767. {
  768. STARPU_ASSERT(task);
  769. return _dmda_push_task(task, 0, task->sched_ctx, 1, 0);
  770. }
  771. static double dmda_simulate_push_sorted_task(struct starpu_task *task)
  772. {
  773. STARPU_ASSERT(task);
  774. return _dmda_push_task(task, 1, task->sched_ctx, 1, 0);
  775. }
  776. static double dmda_simulate_push_sorted_decision_task(struct starpu_task *task)
  777. {
  778. STARPU_ASSERT(task);
  779. return _dmda_push_task(task, 1, task->sched_ctx, 1, 1);
  780. }
  781. static void dmda_add_workers(unsigned sched_ctx_id, int *workerids, unsigned nworkers)
  782. {
  783. struct _starpu_dmda_data *dt = (struct _starpu_dmda_data*)starpu_sched_ctx_get_policy_data(sched_ctx_id);
  784. int workerid;
  785. unsigned i;
  786. for (i = 0; i < nworkers; i++)
  787. {
  788. workerid = workerids[i];
  789. /* if the worker has alreadry belonged to this context
  790. the queue and the synchronization variables have been already initialized */
  791. if(dt->queue_array[workerid] == NULL)
  792. dt->queue_array[workerid] = _starpu_create_fifo();
  793. if(dt->num_priorities != -1)
  794. {
  795. dt->queue_array[workerid]->exp_len_per_priority = (double*)malloc(dt->num_priorities*sizeof(double));
  796. dt->queue_array[workerid]->ntasks_per_priority = (unsigned*)malloc(dt->num_priorities*sizeof(unsigned));
  797. int j;
  798. for(j = 0; j < dt->num_priorities; j++)
  799. {
  800. dt->queue_array[workerid]->exp_len_per_priority[j] = 0.0;
  801. dt->queue_array[workerid]->ntasks_per_priority[j] = 0;
  802. }
  803. }
  804. }
  805. }
  806. static void dmda_remove_workers(unsigned sched_ctx_id, int *workerids, unsigned nworkers)
  807. {
  808. struct _starpu_dmda_data *dt = (struct _starpu_dmda_data*)starpu_sched_ctx_get_policy_data(sched_ctx_id);
  809. int workerid;
  810. unsigned i;
  811. for (i = 0; i < nworkers; i++)
  812. {
  813. workerid = workerids[i];
  814. if(dt->queue_array[workerid] != NULL)
  815. {
  816. if(dt->num_priorities != -1)
  817. {
  818. free(dt->queue_array[workerid]->exp_len_per_priority);
  819. free(dt->queue_array[workerid]->ntasks_per_priority);
  820. }
  821. _starpu_destroy_fifo(dt->queue_array[workerid]);
  822. dt->queue_array[workerid] = NULL;
  823. }
  824. }
  825. }
  826. static void initialize_dmda_policy(unsigned sched_ctx_id)
  827. {
  828. struct _starpu_dmda_data *dt = (struct _starpu_dmda_data*)malloc(sizeof(struct _starpu_dmda_data));
  829. starpu_sched_ctx_set_policy_data(sched_ctx_id, (void*)dt);
  830. dt->queue_array = (struct _starpu_fifo_taskq**)malloc(STARPU_NMAXWORKERS*sizeof(struct _starpu_fifo_taskq*));
  831. int i;
  832. for(i = 0; i < STARPU_NMAXWORKERS; i++)
  833. dt->queue_array[i] = NULL;
  834. dt->alpha = starpu_get_env_float_default("STARPU_SCHED_ALPHA", _STARPU_SCHED_ALPHA_DEFAULT);
  835. dt->beta = starpu_get_env_float_default("STARPU_SCHED_BETA", _STARPU_SCHED_BETA_DEFAULT);
  836. dt->_gamma = starpu_get_env_float_default("STARPU_SCHED_GAMMA", _STARPU_SCHED_GAMMA_DEFAULT);
  837. dt->idle_power = starpu_get_env_float_default("STARPU_IDLE_POWER", 0.0);
  838. if(starpu_sched_ctx_min_priority_is_set(sched_ctx_id) != 0 && starpu_sched_ctx_max_priority_is_set(sched_ctx_id) != 0)
  839. dt->num_priorities = starpu_sched_ctx_get_max_priority(sched_ctx_id) - starpu_sched_ctx_get_min_priority(sched_ctx_id) + 1;
  840. else
  841. dt->num_priorities = -1;
  842. #ifdef STARPU_USE_TOP
  843. /* FIXME: broken, needs to access context variable */
  844. starpu_top_register_parameter_float("DMDA_ALPHA", &alpha,
  845. alpha_minimum, alpha_maximum, param_modified);
  846. starpu_top_register_parameter_float("DMDA_BETA", &beta,
  847. beta_minimum, beta_maximum, param_modified);
  848. starpu_top_register_parameter_float("DMDA_GAMMA", &_gamma,
  849. gamma_minimum, gamma_maximum, param_modified);
  850. starpu_top_register_parameter_float("DMDA_IDLE_POWER", &idle_power,
  851. idle_power_minimum, idle_power_maximum, param_modified);
  852. #endif /* !STARPU_USE_TOP */
  853. }
  854. static void initialize_dmda_sorted_policy(unsigned sched_ctx_id)
  855. {
  856. initialize_dmda_policy(sched_ctx_id);
  857. /* The application may use any integer */
  858. if (starpu_sched_ctx_min_priority_is_set(sched_ctx_id) == 0)
  859. starpu_sched_ctx_set_min_priority(sched_ctx_id, INT_MIN);
  860. if (starpu_sched_ctx_max_priority_is_set(sched_ctx_id) == 0)
  861. starpu_sched_ctx_set_max_priority(sched_ctx_id, INT_MAX);
  862. }
  863. static void deinitialize_dmda_policy(unsigned sched_ctx_id)
  864. {
  865. struct _starpu_dmda_data *dt = (struct _starpu_dmda_data*)starpu_sched_ctx_get_policy_data(sched_ctx_id);
  866. _STARPU_DEBUG("total_task_cnt %ld ready_task_cnt %ld -> %f\n", dt->total_task_cnt, dt->ready_task_cnt, (100.0f*dt->ready_task_cnt)/dt->total_task_cnt);
  867. free(dt->queue_array);
  868. free(dt);
  869. }
  870. /* dmda_pre_exec_hook is called right after the data transfer is done and right
  871. * before the computation to begin, it is useful to update more precisely the
  872. * value of the expected start, end, length, etc... */
  873. static void dmda_pre_exec_hook(struct starpu_task *task)
  874. {
  875. unsigned sched_ctx_id = starpu_sched_ctx_get_ctx_for_task(task);
  876. int workerid = starpu_worker_get_id();
  877. struct _starpu_dmda_data *dt = (struct _starpu_dmda_data*)starpu_sched_ctx_get_policy_data(sched_ctx_id);
  878. struct _starpu_fifo_taskq *fifo = dt->queue_array[workerid];
  879. double model = task->predicted;
  880. starpu_pthread_mutex_t *sched_mutex;
  881. starpu_pthread_cond_t *sched_cond;
  882. starpu_worker_get_sched_condition(workerid, &sched_mutex, &sched_cond);
  883. /* Once the task is executing, we can update the predicted amount
  884. * of work. */
  885. STARPU_PTHREAD_MUTEX_LOCK(sched_mutex);
  886. if(!isnan(model))
  887. {
  888. /* We now start the computation, get rid of it in the completion
  889. * prediction */
  890. fifo->exp_len-= model;
  891. fifo->exp_start = starpu_timing_now() + model;
  892. fifo->exp_end= fifo->exp_start + fifo->exp_len;
  893. if(dt->num_priorities != -1)
  894. {
  895. int i;
  896. int task_prio = _normalize_prio(task->priority, dt->num_priorities, task->sched_ctx);
  897. for(i = 0; i <= task_prio; i++)
  898. fifo->exp_len_per_priority[i] -= model;
  899. }
  900. }
  901. STARPU_PTHREAD_MUTEX_UNLOCK(sched_mutex);
  902. }
  903. static void dmda_push_task_notify(struct starpu_task *task, int workerid, int perf_workerid, unsigned sched_ctx_id)
  904. {
  905. struct _starpu_dmda_data *dt = (struct _starpu_dmda_data*)starpu_sched_ctx_get_policy_data(sched_ctx_id);
  906. struct _starpu_fifo_taskq *fifo = dt->queue_array[workerid];
  907. /* Compute the expected penality */
  908. struct starpu_perfmodel_arch *perf_arch = starpu_worker_get_perf_archtype(perf_workerid, sched_ctx_id);
  909. unsigned memory_node = starpu_worker_get_memory_node(workerid);
  910. double predicted = starpu_task_expected_length(task, perf_arch,
  911. starpu_task_get_implementation(task));
  912. double predicted_transfer = starpu_task_expected_data_transfer_time(memory_node, task);
  913. starpu_pthread_mutex_t *sched_mutex;
  914. starpu_pthread_cond_t *sched_cond;
  915. starpu_worker_get_sched_condition(workerid, &sched_mutex, &sched_cond);
  916. /* Update the predictions */
  917. STARPU_PTHREAD_MUTEX_LOCK(sched_mutex);
  918. /* Sometimes workers didn't take the tasks as early as we expected */
  919. fifo->exp_start = isnan(fifo->exp_start) ? starpu_timing_now() : STARPU_MAX(fifo->exp_start, starpu_timing_now());
  920. fifo->exp_end = fifo->exp_start + fifo->exp_len;
  921. /* If there is no prediction available, we consider the task has a null length */
  922. if (!isnan(predicted_transfer))
  923. {
  924. if (starpu_timing_now() + predicted_transfer < fifo->exp_end)
  925. {
  926. /* We may hope that the transfer will be finished by
  927. * the start of the task. */
  928. predicted_transfer = 0;
  929. }
  930. else
  931. {
  932. /* The transfer will not be finished by then, take the
  933. * remainder into account */
  934. predicted_transfer = (starpu_timing_now() + predicted_transfer) - fifo->exp_end;
  935. }
  936. task->predicted_transfer = predicted_transfer;
  937. fifo->exp_end += predicted_transfer;
  938. fifo->exp_len += predicted_transfer;
  939. if(dt->num_priorities != -1)
  940. {
  941. int i;
  942. int task_prio = _normalize_prio(task->priority, dt->num_priorities, task->sched_ctx);
  943. for(i = 0; i <= task_prio; i++)
  944. fifo->exp_len_per_priority[i] += predicted_transfer;
  945. }
  946. }
  947. /* If there is no prediction available, we consider the task has a null length */
  948. if (!isnan(predicted))
  949. {
  950. task->predicted = predicted;
  951. fifo->exp_end += predicted;
  952. fifo->exp_len += predicted;
  953. if(dt->num_priorities != -1)
  954. {
  955. int i;
  956. int task_prio = _normalize_prio(task->priority, dt->num_priorities, task->sched_ctx);
  957. for(i = 0; i <= task_prio; i++)
  958. fifo->exp_len_per_priority[i] += predicted;
  959. }
  960. }
  961. if(dt->num_priorities != -1)
  962. {
  963. int i;
  964. int task_prio = _normalize_prio(task->priority, dt->num_priorities, task->sched_ctx);
  965. for(i = 0; i <= task_prio; i++)
  966. fifo->ntasks_per_priority[i]++;
  967. }
  968. fifo->ntasks++;
  969. STARPU_PTHREAD_MUTEX_UNLOCK(sched_mutex);
  970. }
  971. static void dmda_post_exec_hook(struct starpu_task * task)
  972. {
  973. unsigned sched_ctx_id = starpu_sched_ctx_get_ctx_for_task(task);
  974. struct _starpu_dmda_data *dt = (struct _starpu_dmda_data*)starpu_sched_ctx_get_policy_data(sched_ctx_id);
  975. int workerid = starpu_worker_get_id();
  976. struct _starpu_fifo_taskq *fifo = dt->queue_array[workerid];
  977. starpu_pthread_mutex_t *sched_mutex;
  978. starpu_pthread_cond_t *sched_cond;
  979. starpu_worker_get_sched_condition(workerid, &sched_mutex, &sched_cond);
  980. STARPU_PTHREAD_MUTEX_LOCK(sched_mutex);
  981. fifo->exp_start = starpu_timing_now();
  982. fifo->exp_end = fifo->exp_start + fifo->exp_len;
  983. STARPU_PTHREAD_MUTEX_UNLOCK(sched_mutex);
  984. }
  985. struct starpu_sched_policy _starpu_sched_dm_policy =
  986. {
  987. .init_sched = initialize_dmda_policy,
  988. .deinit_sched = deinitialize_dmda_policy,
  989. .add_workers = dmda_add_workers ,
  990. .remove_workers = dmda_remove_workers,
  991. .push_task = dm_push_task,
  992. .simulate_push_task = NULL,
  993. .pop_task = dmda_pop_task,
  994. .pre_exec_hook = dmda_pre_exec_hook,
  995. .post_exec_hook = dmda_post_exec_hook,
  996. .pop_every_task = dmda_pop_every_task,
  997. .policy_name = "dm",
  998. .policy_description = "performance model",
  999. .worker_type = STARPU_WORKER_LIST,
  1000. };
  1001. struct starpu_sched_policy _starpu_sched_dmda_policy =
  1002. {
  1003. .init_sched = initialize_dmda_policy,
  1004. .deinit_sched = deinitialize_dmda_policy,
  1005. .add_workers = dmda_add_workers ,
  1006. .remove_workers = dmda_remove_workers,
  1007. .push_task = dmda_push_task,
  1008. .simulate_push_task = dmda_simulate_push_task,
  1009. .push_task_notify = dmda_push_task_notify,
  1010. .pop_task = dmda_pop_task,
  1011. .pre_exec_hook = dmda_pre_exec_hook,
  1012. .post_exec_hook = dmda_post_exec_hook,
  1013. .pop_every_task = dmda_pop_every_task,
  1014. .policy_name = "dmda",
  1015. .policy_description = "data-aware performance model",
  1016. .worker_type = STARPU_WORKER_LIST,
  1017. };
  1018. struct starpu_sched_policy _starpu_sched_dmda_sorted_policy =
  1019. {
  1020. .init_sched = initialize_dmda_sorted_policy,
  1021. .deinit_sched = deinitialize_dmda_policy,
  1022. .add_workers = dmda_add_workers ,
  1023. .remove_workers = dmda_remove_workers,
  1024. .push_task = dmda_push_sorted_task,
  1025. .simulate_push_task = dmda_simulate_push_sorted_task,
  1026. .push_task_notify = dmda_push_task_notify,
  1027. .pop_task = dmda_pop_ready_task,
  1028. .pre_exec_hook = dmda_pre_exec_hook,
  1029. .post_exec_hook = dmda_post_exec_hook,
  1030. .pop_every_task = dmda_pop_every_task,
  1031. .policy_name = "dmdas",
  1032. .policy_description = "data-aware performance model (sorted)",
  1033. .worker_type = STARPU_WORKER_LIST,
  1034. };
  1035. struct starpu_sched_policy _starpu_sched_dmda_sorted_decision_policy =
  1036. {
  1037. .init_sched = initialize_dmda_sorted_policy,
  1038. .deinit_sched = deinitialize_dmda_policy,
  1039. .add_workers = dmda_add_workers ,
  1040. .remove_workers = dmda_remove_workers,
  1041. .push_task = dmda_push_sorted_decision_task,
  1042. .simulate_push_task = dmda_simulate_push_sorted_decision_task,
  1043. .push_task_notify = dmda_push_task_notify,
  1044. .pop_task = dmda_pop_ready_task,
  1045. .pre_exec_hook = dmda_pre_exec_hook,
  1046. .post_exec_hook = dmda_post_exec_hook,
  1047. .pop_every_task = dmda_pop_every_task,
  1048. .policy_name = "dmdasd",
  1049. .policy_description = "data-aware performance model (sorted decision)",
  1050. .worker_type = STARPU_WORKER_LIST,
  1051. };
  1052. struct starpu_sched_policy _starpu_sched_dmda_ready_policy =
  1053. {
  1054. .init_sched = initialize_dmda_policy,
  1055. .deinit_sched = deinitialize_dmda_policy,
  1056. .add_workers = dmda_add_workers ,
  1057. .remove_workers = dmda_remove_workers,
  1058. .push_task = dmda_push_task,
  1059. .simulate_push_task = dmda_simulate_push_task,
  1060. .push_task_notify = dmda_push_task_notify,
  1061. .pop_task = dmda_pop_ready_task,
  1062. .pre_exec_hook = dmda_pre_exec_hook,
  1063. .post_exec_hook = dmda_post_exec_hook,
  1064. .pop_every_task = dmda_pop_every_task,
  1065. .policy_name = "dmdar",
  1066. .policy_description = "data-aware performance model (ready)",
  1067. .worker_type = STARPU_WORKER_LIST,
  1068. };