deque_modeling_policy_data_aware.c 32 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069
  1. /* StarPU --- Runtime system for heterogeneous multicore architectures.
  2. *
  3. * Copyright (C) 2010-2013 Université de Bordeaux 1
  4. * Copyright (C) 2010, 2011, 2012, 2013 Centre National de la Recherche Scientifique
  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. };
  45. static double idle_power = 0.0;
  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 const float alpha_minimum=0;
  60. static const float alpha_maximum=10.0;
  61. static const float beta_minimum=0;
  62. static const float beta_maximum=10.0;
  63. static const float gamma_minimum=0;
  64. static const float gamma_maximum=10000.0;
  65. static const float idle_power_minimum=0;
  66. static const float idle_power_maximum=10000.0;
  67. #endif /* !STARPU_USE_TOP */
  68. static int count_non_ready_buffers(struct starpu_task *task, unsigned node)
  69. {
  70. int cnt = 0;
  71. unsigned nbuffers = task->cl->nbuffers;
  72. unsigned index;
  73. for (index = 0; index < nbuffers; index++)
  74. {
  75. starpu_data_handle_t handle;
  76. handle = STARPU_TASK_GET_HANDLE(task, index);
  77. int is_valid;
  78. starpu_data_query_status(handle, node, NULL, &is_valid, NULL);
  79. if (!is_valid)
  80. cnt++;
  81. }
  82. return cnt;
  83. }
  84. #ifdef STARPU_USE_TOP
  85. static void param_modified(struct starpu_top_param* d)
  86. {
  87. #ifdef STARPU_DEVEL
  88. #warning FIXME: get sched ctx to get alpha/beta/gamma/idle values
  89. #endif
  90. /* Just to show parameter modification. */
  91. fprintf(stderr,
  92. "%s has been modified : "
  93. "alpha=%f|beta=%f|gamma=%f|idle_power=%f !\n",
  94. d->name, alpha,beta,_gamma, idle_power);
  95. }
  96. #endif /* !STARPU_USE_TOP */
  97. static struct starpu_task *_starpu_fifo_pop_first_ready_task(struct _starpu_fifo_taskq *fifo_queue, unsigned node)
  98. {
  99. struct starpu_task *task = NULL, *current;
  100. if (fifo_queue->ntasks == 0)
  101. return NULL;
  102. if (fifo_queue->ntasks > 0)
  103. {
  104. fifo_queue->ntasks--;
  105. task = starpu_task_list_front(&fifo_queue->taskq);
  106. if (STARPU_UNLIKELY(!task))
  107. return NULL;
  108. int first_task_priority = task->priority;
  109. current = task;
  110. int non_ready_best = INT_MAX;
  111. while (current)
  112. {
  113. int priority = current->priority;
  114. if (priority >= first_task_priority)
  115. {
  116. int non_ready = count_non_ready_buffers(current, node);
  117. if (non_ready < non_ready_best)
  118. {
  119. non_ready_best = non_ready;
  120. task = current;
  121. if (non_ready == 0)
  122. break;
  123. }
  124. }
  125. current = current->next;
  126. }
  127. starpu_task_list_erase(&fifo_queue->taskq, task);
  128. _STARPU_TRACE_JOB_POP(task, 0);
  129. }
  130. return task;
  131. }
  132. static struct starpu_task *dmda_pop_ready_task(unsigned sched_ctx_id)
  133. {
  134. struct _starpu_dmda_data *dt = (struct _starpu_dmda_data*)starpu_sched_ctx_get_policy_data(sched_ctx_id);
  135. struct starpu_task *task;
  136. int workerid = starpu_worker_get_id();
  137. struct _starpu_fifo_taskq *fifo = dt->queue_array[workerid];
  138. unsigned node = starpu_worker_get_memory_node(workerid);
  139. task = _starpu_fifo_pop_first_ready_task(fifo, node);
  140. if (task)
  141. {
  142. double model = task->predicted;
  143. if(!isnan(model))
  144. {
  145. fifo->exp_len -= model;
  146. fifo->exp_start = starpu_timing_now() + model;
  147. fifo->exp_end = fifo->exp_start + fifo->exp_len;
  148. }
  149. #ifdef STARPU_VERBOSE
  150. if (task->cl)
  151. {
  152. int non_ready = count_non_ready_buffers(task, node);
  153. if (non_ready == 0)
  154. dt->ready_task_cnt++;
  155. }
  156. dt->total_task_cnt++;
  157. #endif
  158. }
  159. return task;
  160. }
  161. static struct starpu_task *dmda_pop_task(unsigned sched_ctx_id)
  162. {
  163. struct _starpu_dmda_data *dt = (struct _starpu_dmda_data*)starpu_sched_ctx_get_policy_data(sched_ctx_id);
  164. struct starpu_task *task;
  165. int workerid = starpu_worker_get_id();
  166. struct _starpu_fifo_taskq *fifo = dt->queue_array[workerid];
  167. STARPU_ASSERT_MSG(fifo, "worker %d does not belong to ctx %d anymore \n", workerid, sched_ctx_id);
  168. task = _starpu_fifo_pop_local_task(fifo);
  169. if (task)
  170. {
  171. double model = task->predicted;
  172. if(!isnan(model))
  173. {
  174. fifo->exp_len -= model;
  175. fifo->exp_start = starpu_timing_now() + model;
  176. fifo->exp_end = fifo->exp_start + fifo->exp_len;
  177. }
  178. #ifdef STARPU_VERBOSE
  179. if (task->cl)
  180. {
  181. int non_ready = count_non_ready_buffers(task, starpu_worker_get_memory_node(workerid));
  182. if (non_ready == 0)
  183. dt->ready_task_cnt++;
  184. }
  185. dt->total_task_cnt++;
  186. #endif
  187. }
  188. return task;
  189. }
  190. static struct starpu_task *dmda_pop_every_task(unsigned sched_ctx_id)
  191. {
  192. struct _starpu_dmda_data *dt = (struct _starpu_dmda_data*)starpu_sched_ctx_get_policy_data(sched_ctx_id);
  193. struct starpu_task *new_list;
  194. int workerid = starpu_worker_get_id();
  195. struct _starpu_fifo_taskq *fifo = dt->queue_array[workerid];
  196. starpu_pthread_mutex_t *sched_mutex;
  197. starpu_pthread_cond_t *sched_cond;
  198. starpu_worker_get_sched_condition(workerid, &sched_mutex, &sched_cond);
  199. STARPU_PTHREAD_MUTEX_LOCK(sched_mutex);
  200. new_list = _starpu_fifo_pop_every_task(fifo, workerid);
  201. STARPU_PTHREAD_MUTEX_UNLOCK(sched_mutex);
  202. while (new_list)
  203. {
  204. double model = new_list->predicted;
  205. if(!isnan(model))
  206. {
  207. fifo->exp_len -= model;
  208. fifo->exp_start = starpu_timing_now() + model;
  209. fifo->exp_end = fifo->exp_start + fifo->exp_len;
  210. }
  211. new_list = new_list->next;
  212. }
  213. return new_list;
  214. }
  215. static int push_task_on_best_worker(struct starpu_task *task, int best_workerid,
  216. double predicted, double predicted_transfer,
  217. int prio, unsigned sched_ctx_id)
  218. {
  219. struct _starpu_dmda_data *dt = (struct _starpu_dmda_data*)starpu_sched_ctx_get_policy_data(sched_ctx_id);
  220. /* make sure someone coule execute that task ! */
  221. STARPU_ASSERT(best_workerid != -1);
  222. struct _starpu_fifo_taskq *fifo = dt->queue_array[best_workerid];
  223. starpu_pthread_mutex_t *sched_mutex;
  224. starpu_pthread_cond_t *sched_cond;
  225. starpu_worker_get_sched_condition(best_workerid, &sched_mutex, &sched_cond);
  226. #ifdef STARPU_USE_SC_HYPERVISOR
  227. starpu_sched_ctx_call_pushed_task_cb(best_workerid, sched_ctx_id);
  228. #endif //STARPU_USE_SC_HYPERVISOR
  229. STARPU_PTHREAD_MUTEX_LOCK(sched_mutex);
  230. /* Sometimes workers didn't take the tasks as early as we expected */
  231. fifo->exp_start = STARPU_MAX(fifo->exp_start, starpu_timing_now());
  232. fifo->exp_end = fifo->exp_start + fifo->exp_len;
  233. if (starpu_timing_now() + predicted_transfer < fifo->exp_end)
  234. {
  235. /* We may hope that the transfer will be finished by
  236. * the start of the task. */
  237. predicted_transfer = 0;
  238. }
  239. else
  240. {
  241. /* The transfer will not be finished by then, take the
  242. * remainder into account */
  243. predicted_transfer += starpu_timing_now();
  244. predicted_transfer -= fifo->exp_end;
  245. }
  246. if(!isnan(predicted_transfer))
  247. {
  248. fifo->exp_end += predicted_transfer;
  249. fifo->exp_len += predicted_transfer;
  250. }
  251. if(!isnan(predicted))
  252. {
  253. fifo->exp_end += predicted;
  254. fifo->exp_len += predicted;
  255. }
  256. STARPU_PTHREAD_MUTEX_UNLOCK(sched_mutex);
  257. task->predicted = predicted;
  258. task->predicted_transfer = predicted_transfer;
  259. #ifdef STARPU_USE_TOP
  260. starpu_top_task_prevision(task, best_workerid,
  261. (unsigned long long)(fifo->exp_end-predicted)/1000,
  262. (unsigned long long)fifo->exp_end/1000);
  263. #endif /* !STARPU_USE_TOP */
  264. if (starpu_get_prefetch_flag())
  265. {
  266. unsigned memory_node = starpu_worker_get_memory_node(best_workerid);
  267. starpu_prefetch_task_input_on_node(task, memory_node);
  268. }
  269. #ifdef HAVE_AYUDAME_H
  270. if (AYU_event)
  271. {
  272. int id = best_workerid;
  273. AYU_event(AYU_ADDTASKTOQUEUE, _starpu_get_job_associated_to_task(task)->job_id, &id);
  274. }
  275. #endif
  276. int ret = 0;
  277. if (prio)
  278. {
  279. STARPU_PTHREAD_MUTEX_LOCK(sched_mutex);
  280. ret =_starpu_fifo_push_sorted_task(dt->queue_array[best_workerid], task);
  281. STARPU_PTHREAD_COND_SIGNAL(sched_cond);
  282. starpu_push_task_end(task);
  283. STARPU_PTHREAD_MUTEX_UNLOCK(sched_mutex);
  284. }
  285. else
  286. {
  287. STARPU_PTHREAD_MUTEX_LOCK(sched_mutex);
  288. starpu_task_list_push_back (&dt->queue_array[best_workerid]->taskq, task);
  289. dt->queue_array[best_workerid]->ntasks++;
  290. dt->queue_array[best_workerid]->nprocessed++;
  291. STARPU_PTHREAD_COND_SIGNAL(sched_cond);
  292. starpu_push_task_end(task);
  293. STARPU_PTHREAD_MUTEX_UNLOCK(sched_mutex);
  294. }
  295. return ret;
  296. }
  297. /* TODO: factorize with dmda!! */
  298. static int _dm_push_task(struct starpu_task *task, unsigned prio, unsigned sched_ctx_id)
  299. {
  300. struct _starpu_dmda_data *dt = (struct _starpu_dmda_data*)starpu_sched_ctx_get_policy_data(sched_ctx_id);
  301. unsigned worker, worker_ctx = 0;
  302. int best = -1;
  303. double best_exp_end = 0.0;
  304. double model_best = 0.0;
  305. double transfer_model_best = 0.0;
  306. int ntasks_best = -1;
  307. double ntasks_best_end = 0.0;
  308. int calibrating = 0;
  309. /* A priori, we know all estimations */
  310. int unknown = 0;
  311. unsigned best_impl = 0;
  312. unsigned nimpl;
  313. struct starpu_worker_collection *workers = starpu_sched_ctx_get_worker_collection(sched_ctx_id);
  314. struct starpu_sched_ctx_iterator it;
  315. if(workers->init_iterator)
  316. workers->init_iterator(workers, &it);
  317. while(workers->has_next(workers, &it))
  318. {
  319. worker = workers->get_next(workers, &it);
  320. struct _starpu_fifo_taskq *fifo = dt->queue_array[worker];
  321. unsigned memory_node = starpu_worker_get_memory_node(worker);
  322. enum starpu_perfmodel_archtype perf_arch = starpu_worker_get_perf_archtype(worker);
  323. /* Sometimes workers didn't take the tasks as early as we expected */
  324. starpu_pthread_mutex_t *sched_mutex;
  325. starpu_pthread_cond_t *sched_cond;
  326. starpu_worker_get_sched_condition(worker, &sched_mutex, &sched_cond);
  327. STARPU_PTHREAD_MUTEX_LOCK(sched_mutex);
  328. fifo->exp_start = STARPU_MAX(fifo->exp_start, starpu_timing_now());
  329. fifo->exp_end = fifo->exp_start + fifo->exp_len;
  330. STARPU_PTHREAD_MUTEX_UNLOCK(sched_mutex);
  331. for (nimpl = 0; nimpl < STARPU_MAXIMPLEMENTATIONS; nimpl++)
  332. {
  333. if (!starpu_worker_can_execute_task(worker, task, nimpl))
  334. {
  335. /* no one on that queue may execute this task */
  336. // worker_ctx++;
  337. continue;
  338. }
  339. double exp_end;
  340. double local_length = starpu_task_expected_length(task, perf_arch, nimpl);
  341. double local_penalty = starpu_task_expected_data_transfer_time(memory_node, task);
  342. double ntasks_end = fifo->ntasks / starpu_worker_get_relative_speedup(perf_arch);
  343. //_STARPU_DEBUG("Scheduler dm: task length (%lf) worker (%u) kernel (%u) \n", local_length,worker,nimpl);
  344. /*
  345. * This implements a default greedy scheduler for the
  346. * case of tasks which have no performance model, or
  347. * whose performance model is not calibrated yet.
  348. *
  349. * It simply uses the number of tasks already pushed to
  350. * the workers, divided by the relative performance of
  351. * a CPU and of a GPU.
  352. *
  353. * This is always computed, but the ntasks_best
  354. * selection is only really used if the task indeed has
  355. * no performance model, or is not calibrated yet.
  356. */
  357. if (ntasks_best == -1
  358. /* Always compute the greedy decision, at least for
  359. * the tasks with no performance model. */
  360. || (!calibrating && ntasks_end < ntasks_best_end)
  361. /* The performance model of this task is not
  362. * calibrated on this worker, try to run it there
  363. * to calibrate it there. */
  364. || (!calibrating && isnan(local_length))
  365. /* the performance model of this task is not
  366. * calibrated on this worker either, rather run it
  367. * there if this one is low on scheduled tasks. */
  368. || (calibrating && isnan(local_length) && ntasks_end < ntasks_best_end)
  369. )
  370. {
  371. ntasks_best_end = ntasks_end;
  372. ntasks_best = worker;
  373. best_impl = nimpl;
  374. }
  375. if (isnan(local_length))
  376. /* we are calibrating, we want to speed-up calibration time
  377. * so we privilege non-calibrated tasks (but still
  378. * greedily distribute them to avoid dumb schedules) */
  379. calibrating = 1;
  380. if (isnan(local_length) || _STARPU_IS_ZERO(local_length))
  381. /* there is no prediction available for that task
  382. * with that arch yet, so switch to a greedy strategy */
  383. unknown = 1;
  384. if (unknown)
  385. continue;
  386. exp_end = fifo->exp_start + fifo->exp_len + local_length;
  387. if (best == -1 || exp_end < best_exp_end)
  388. {
  389. /* a better solution was found */
  390. best_exp_end = exp_end;
  391. best = worker;
  392. model_best = local_length;
  393. transfer_model_best = local_penalty;
  394. best_impl = nimpl;
  395. }
  396. }
  397. worker_ctx++;
  398. }
  399. if (unknown)
  400. {
  401. best = ntasks_best;
  402. model_best = 0.0;
  403. transfer_model_best = 0.0;
  404. }
  405. //_STARPU_DEBUG("Scheduler dm: kernel (%u)\n", best_impl);
  406. starpu_task_set_implementation(task, best_impl);
  407. /* we should now have the best worker in variable "best" */
  408. return push_task_on_best_worker(task, best,
  409. model_best, transfer_model_best, prio, sched_ctx_id);
  410. }
  411. /* TODO: factorise CPU computations, expensive with a lot of cores */
  412. static void compute_all_performance_predictions(struct starpu_task *task,
  413. double local_task_length[STARPU_NMAXWORKERS][STARPU_MAXIMPLEMENTATIONS],
  414. double exp_end[STARPU_NMAXWORKERS][STARPU_MAXIMPLEMENTATIONS],
  415. double *max_exp_endp,
  416. double *best_exp_endp,
  417. double local_data_penalty[STARPU_NMAXWORKERS][STARPU_MAXIMPLEMENTATIONS],
  418. double local_power[STARPU_NMAXWORKERS][STARPU_MAXIMPLEMENTATIONS],
  419. int *forced_worker, int *forced_impl, unsigned sched_ctx_id)
  420. {
  421. int calibrating = 0;
  422. double max_exp_end = DBL_MIN;
  423. double best_exp_end = DBL_MAX;
  424. int ntasks_best = -1;
  425. int nimpl_best = 0;
  426. double ntasks_best_end = 0.0;
  427. /* A priori, we know all estimations */
  428. int unknown = 0;
  429. unsigned worker, worker_ctx = 0;
  430. unsigned nimpl;
  431. starpu_task_bundle_t bundle = task->bundle;
  432. struct _starpu_dmda_data *dt = (struct _starpu_dmda_data*)starpu_sched_ctx_get_policy_data(sched_ctx_id);
  433. struct starpu_worker_collection *workers = starpu_sched_ctx_get_worker_collection(sched_ctx_id);
  434. struct starpu_sched_ctx_iterator it;
  435. if(workers->init_iterator)
  436. workers->init_iterator(workers, &it);
  437. while(workers->has_next(workers, &it))
  438. {
  439. worker = workers->get_next(workers, &it);
  440. struct _starpu_fifo_taskq *fifo = dt->queue_array[worker];
  441. enum starpu_perfmodel_archtype perf_arch = starpu_worker_get_perf_archtype(worker);
  442. unsigned memory_node = starpu_worker_get_memory_node(worker);
  443. /* Sometimes workers didn't take the tasks as early as we expected */
  444. starpu_pthread_mutex_t *sched_mutex;
  445. starpu_pthread_cond_t *sched_cond;
  446. starpu_worker_get_sched_condition(worker, &sched_mutex, &sched_cond);
  447. STARPU_PTHREAD_MUTEX_LOCK(sched_mutex);
  448. fifo->exp_start = STARPU_MAX(fifo->exp_start, starpu_timing_now());
  449. STARPU_PTHREAD_MUTEX_UNLOCK(sched_mutex);
  450. for(nimpl = 0; nimpl < STARPU_MAXIMPLEMENTATIONS; nimpl++)
  451. {
  452. if (!starpu_worker_can_execute_task(worker, task, nimpl))
  453. {
  454. /* no one on that queue may execute this task */
  455. continue;
  456. }
  457. STARPU_ASSERT_MSG(fifo != NULL, "worker %d ctx %d\n", worker, sched_ctx_id);
  458. exp_end[worker_ctx][nimpl] = fifo->exp_start + fifo->exp_len;
  459. if (exp_end[worker_ctx][nimpl] > max_exp_end)
  460. max_exp_end = exp_end[worker_ctx][nimpl];
  461. //_STARPU_DEBUG("Scheduler dmda: task length (%lf) worker (%u) kernel (%u) \n", local_task_length[worker][nimpl],worker,nimpl);
  462. if (bundle)
  463. {
  464. /* TODO : conversion time */
  465. local_task_length[worker_ctx][nimpl] = starpu_task_bundle_expected_length(bundle, perf_arch, nimpl);
  466. local_data_penalty[worker_ctx][nimpl] = starpu_task_bundle_expected_data_transfer_time(bundle, memory_node);
  467. local_power[worker_ctx][nimpl] = starpu_task_bundle_expected_power(bundle, perf_arch,nimpl);
  468. }
  469. else
  470. {
  471. local_task_length[worker_ctx][nimpl] = starpu_task_expected_length(task, perf_arch, nimpl);
  472. local_data_penalty[worker_ctx][nimpl] = starpu_task_expected_data_transfer_time(memory_node, task);
  473. local_power[worker_ctx][nimpl] = starpu_task_expected_power(task, perf_arch,nimpl);
  474. double conversion_time = starpu_task_expected_conversion_time(task, perf_arch, nimpl);
  475. if (conversion_time > 0.0)
  476. local_task_length[worker_ctx][nimpl] += conversion_time;
  477. }
  478. double ntasks_end = fifo->ntasks / starpu_worker_get_relative_speedup(perf_arch);
  479. /*
  480. * This implements a default greedy scheduler for the
  481. * case of tasks which have no performance model, or
  482. * whose performance model is not calibrated yet.
  483. *
  484. * It simply uses the number of tasks already pushed to
  485. * the workers, divided by the relative performance of
  486. * a CPU and of a GPU.
  487. *
  488. * This is always computed, but the ntasks_best
  489. * selection is only really used if the task indeed has
  490. * no performance model, or is not calibrated yet.
  491. */
  492. if (ntasks_best == -1
  493. /* Always compute the greedy decision, at least for
  494. * the tasks with no performance model. */
  495. || (!calibrating && ntasks_end < ntasks_best_end)
  496. /* The performance model of this task is not
  497. * calibrated on this worker, try to run it there
  498. * to calibrate it there. */
  499. || (!calibrating && isnan(local_task_length[worker_ctx][nimpl]))
  500. /* the performance model of this task is not
  501. * calibrated on this worker either, rather run it
  502. * there if this one is low on scheduled tasks. */
  503. || (calibrating && isnan(local_task_length[worker_ctx][nimpl]) && ntasks_end < ntasks_best_end)
  504. )
  505. {
  506. ntasks_best_end = ntasks_end;
  507. ntasks_best = worker;
  508. nimpl_best = nimpl;
  509. }
  510. if (isnan(local_task_length[worker_ctx][nimpl]))
  511. /* we are calibrating, we want to speed-up calibration time
  512. * so we privilege non-calibrated tasks (but still
  513. * greedily distribute them to avoid dumb schedules) */
  514. calibrating = 1;
  515. if (isnan(local_task_length[worker_ctx][nimpl])
  516. || _STARPU_IS_ZERO(local_task_length[worker_ctx][nimpl]))
  517. /* there is no prediction available for that task
  518. * with that arch (yet or at all), so switch to a greedy strategy */
  519. unknown = 1;
  520. if (unknown)
  521. continue;
  522. exp_end[worker_ctx][nimpl] = fifo->exp_start + fifo->exp_len + local_task_length[worker_ctx][nimpl];
  523. if (exp_end[worker_ctx][nimpl] < best_exp_end)
  524. {
  525. /* a better solution was found */
  526. best_exp_end = exp_end[worker_ctx][nimpl];
  527. nimpl_best = nimpl;
  528. }
  529. if (isnan(local_power[worker_ctx][nimpl]))
  530. local_power[worker_ctx][nimpl] = 0.;
  531. }
  532. worker_ctx++;
  533. }
  534. *forced_worker = unknown?ntasks_best:-1;
  535. *forced_impl = unknown?nimpl_best:-1;
  536. *best_exp_endp = best_exp_end;
  537. *max_exp_endp = max_exp_end;
  538. }
  539. static int _dmda_push_task(struct starpu_task *task, unsigned prio, unsigned sched_ctx_id)
  540. {
  541. /* find the queue */
  542. unsigned worker, worker_ctx = 0;
  543. int best = -1, best_in_ctx = -1;
  544. int selected_impl = 0;
  545. double model_best = 0.0;
  546. double transfer_model_best = 0.0;
  547. /* this flag is set if the corresponding worker is selected because
  548. there is no performance prediction available yet */
  549. int forced_best = -1;
  550. int forced_impl = -1;
  551. struct _starpu_dmda_data *dt = (struct _starpu_dmda_data*)starpu_sched_ctx_get_policy_data(sched_ctx_id);
  552. struct starpu_worker_collection *workers = starpu_sched_ctx_get_worker_collection(sched_ctx_id);
  553. unsigned nworkers_ctx = workers->nworkers;
  554. double local_task_length[STARPU_NMAXWORKERS][STARPU_MAXIMPLEMENTATIONS];
  555. double local_data_penalty[STARPU_NMAXWORKERS][STARPU_MAXIMPLEMENTATIONS];
  556. double local_power[STARPU_NMAXWORKERS][STARPU_MAXIMPLEMENTATIONS];
  557. /* Expected end of this task on the workers */
  558. double exp_end[STARPU_NMAXWORKERS][STARPU_MAXIMPLEMENTATIONS];
  559. /* This is the minimum among the exp_end[] matrix */
  560. double best_exp_end;
  561. /* This is the maximum termination time of already-scheduled tasks over all workers */
  562. double max_exp_end = 0.0;
  563. double fitness[nworkers_ctx][STARPU_MAXIMPLEMENTATIONS];
  564. struct starpu_sched_ctx_iterator it;
  565. if(workers->init_iterator)
  566. workers->init_iterator(workers, &it);
  567. compute_all_performance_predictions(task,
  568. local_task_length,
  569. exp_end,
  570. &max_exp_end,
  571. &best_exp_end,
  572. local_data_penalty,
  573. local_power,
  574. &forced_best,
  575. &forced_impl, sched_ctx_id);
  576. double best_fitness = -1;
  577. unsigned nimpl;
  578. if (forced_best == -1)
  579. {
  580. while(workers->has_next(workers, &it))
  581. {
  582. worker = workers->get_next(workers, &it);
  583. for (nimpl = 0; nimpl < STARPU_MAXIMPLEMENTATIONS; nimpl++)
  584. {
  585. if (!starpu_worker_can_execute_task(worker, task, nimpl))
  586. {
  587. /* no one on that queue may execute this task */
  588. continue;
  589. }
  590. fitness[worker_ctx][nimpl] = dt->alpha*(exp_end[worker_ctx][nimpl] - best_exp_end)
  591. + dt->beta*(local_data_penalty[worker_ctx][nimpl])
  592. + dt->_gamma*(local_power[worker_ctx][nimpl]);
  593. if (exp_end[worker_ctx][nimpl] > max_exp_end)
  594. {
  595. /* This placement will make the computation
  596. * longer, take into account the idle
  597. * consumption of other cpus */
  598. fitness[worker_ctx][nimpl] += dt->_gamma * dt->idle_power * (exp_end[worker_ctx][nimpl] - max_exp_end) / 1000000.0;
  599. }
  600. if (best == -1 || fitness[worker_ctx][nimpl] < best_fitness)
  601. {
  602. /* we found a better solution */
  603. best_fitness = fitness[worker_ctx][nimpl];
  604. best = worker;
  605. best_in_ctx = worker_ctx;
  606. selected_impl = nimpl;
  607. //_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]);
  608. }
  609. }
  610. worker_ctx++;
  611. }
  612. }
  613. STARPU_ASSERT(forced_best != -1 || best != -1);
  614. if (forced_best != -1)
  615. {
  616. /* there is no prediction available for that task
  617. * with that arch we want to speed-up calibration time
  618. * so we force this measurement */
  619. best = forced_best;
  620. selected_impl = forced_impl;
  621. model_best = 0.0;
  622. transfer_model_best = 0.0;
  623. }
  624. else if (task->bundle)
  625. {
  626. enum starpu_perfmodel_archtype perf_arch = starpu_worker_get_perf_archtype(best_in_ctx);
  627. unsigned memory_node = starpu_worker_get_memory_node(best);
  628. model_best = starpu_task_expected_length(task, perf_arch, selected_impl);
  629. transfer_model_best = starpu_task_expected_data_transfer_time(memory_node, task);
  630. }
  631. else
  632. {
  633. model_best = local_task_length[best_in_ctx][selected_impl];
  634. transfer_model_best = local_data_penalty[best_in_ctx][selected_impl];
  635. }
  636. //_STARPU_DEBUG("Scheduler dmda: kernel (%u)\n", best_impl);
  637. starpu_task_set_implementation(task, selected_impl);
  638. /* we should now have the best worker in variable "best" */
  639. return push_task_on_best_worker(task, best, model_best, transfer_model_best, prio, sched_ctx_id);
  640. }
  641. static int dmda_push_sorted_task(struct starpu_task *task)
  642. {
  643. #ifdef STARPU_DEVEL
  644. #warning TODO: after defining a scheduling window, use that instead of empty_ctx_tasks
  645. #endif
  646. return _dmda_push_task(task, 1, task->sched_ctx);
  647. }
  648. static int dm_push_task(struct starpu_task *task)
  649. {
  650. return _dm_push_task(task, 0, task->sched_ctx);
  651. }
  652. static int dmda_push_task(struct starpu_task *task)
  653. {
  654. STARPU_ASSERT(task);
  655. return _dmda_push_task(task, 0, task->sched_ctx);
  656. }
  657. static void dmda_add_workers(unsigned sched_ctx_id, int *workerids, unsigned nworkers)
  658. {
  659. struct _starpu_dmda_data *dt = (struct _starpu_dmda_data*)starpu_sched_ctx_get_policy_data(sched_ctx_id);
  660. int workerid;
  661. unsigned i;
  662. for (i = 0; i < nworkers; i++)
  663. {
  664. workerid = workerids[i];
  665. /* if the worker has alreadry belonged to this context
  666. the queue and the synchronization variables have been already initialized */
  667. if(dt->queue_array[workerid] == NULL)
  668. dt->queue_array[workerid] = _starpu_create_fifo();
  669. }
  670. }
  671. static void dmda_remove_workers(unsigned sched_ctx_id, int *workerids, unsigned nworkers)
  672. {
  673. struct _starpu_dmda_data *dt = (struct _starpu_dmda_data*)starpu_sched_ctx_get_policy_data(sched_ctx_id);
  674. int workerid;
  675. unsigned i;
  676. for (i = 0; i < nworkers; i++)
  677. {
  678. workerid = workerids[i];
  679. if(dt->queue_array[workerid] != NULL)
  680. {
  681. _starpu_destroy_fifo(dt->queue_array[workerid]);
  682. dt->queue_array[workerid] = NULL;
  683. }
  684. }
  685. }
  686. static void initialize_dmda_policy(unsigned sched_ctx_id)
  687. {
  688. starpu_sched_ctx_create_worker_collection(sched_ctx_id, STARPU_WORKER_LIST);
  689. struct _starpu_dmda_data *dt = (struct _starpu_dmda_data*)malloc(sizeof(struct _starpu_dmda_data));
  690. dt->alpha = _STARPU_SCHED_ALPHA_DEFAULT;
  691. dt->beta = _STARPU_SCHED_BETA_DEFAULT;
  692. dt->_gamma = _STARPU_SCHED_GAMMA_DEFAULT;
  693. dt->idle_power = 0.0;
  694. starpu_sched_ctx_set_policy_data(sched_ctx_id, (void*)dt);
  695. dt->queue_array = (struct _starpu_fifo_taskq**)malloc(STARPU_NMAXWORKERS*sizeof(struct _starpu_fifo_taskq*));
  696. int i;
  697. for(i = 0; i < STARPU_NMAXWORKERS; i++)
  698. dt->queue_array[i] = NULL;
  699. const char *strval_alpha = getenv("STARPU_SCHED_ALPHA");
  700. if (strval_alpha)
  701. dt->alpha = atof(strval_alpha);
  702. const char *strval_beta = getenv("STARPU_SCHED_BETA");
  703. if (strval_beta)
  704. dt->beta = atof(strval_beta);
  705. const char *strval_gamma = getenv("STARPU_SCHED_GAMMA");
  706. if (strval_gamma)
  707. dt->_gamma = atof(strval_gamma);
  708. const char *strval_idle_power = getenv("STARPU_IDLE_POWER");
  709. if (strval_idle_power)
  710. dt->idle_power = atof(strval_idle_power);
  711. #ifdef STARPU_USE_TOP
  712. starpu_top_register_parameter_float("DMDA_ALPHA", &alpha,
  713. alpha_minimum, alpha_maximum, param_modified);
  714. starpu_top_register_parameter_float("DMDA_BETA", &beta,
  715. beta_minimum, beta_maximum, param_modified);
  716. starpu_top_register_parameter_float("DMDA_GAMMA", &_gamma,
  717. gamma_minimum, gamma_maximum, param_modified);
  718. starpu_top_register_parameter_float("DMDA_IDLE_POWER", &idle_power,
  719. idle_power_minimum, idle_power_maximum, param_modified);
  720. #endif /* !STARPU_USE_TOP */
  721. }
  722. static void initialize_dmda_sorted_policy(unsigned sched_ctx_id)
  723. {
  724. initialize_dmda_policy(sched_ctx_id);
  725. /* The application may use any integer */
  726. starpu_sched_ctx_set_min_priority(sched_ctx_id, INT_MIN);
  727. starpu_sched_ctx_set_max_priority(sched_ctx_id, INT_MAX);
  728. }
  729. static void deinitialize_dmda_policy(unsigned sched_ctx_id)
  730. {
  731. struct _starpu_dmda_data *dt = (struct _starpu_dmda_data*)starpu_sched_ctx_get_policy_data(sched_ctx_id);
  732. _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);
  733. free(dt->queue_array);
  734. free(dt);
  735. starpu_sched_ctx_delete_worker_collection(sched_ctx_id);
  736. }
  737. /* dmda_pre_exec_hook is called right after the data transfer is done and right
  738. * before the computation to begin, it is useful to update more precisely the
  739. * value of the expected start, end, length, etc... */
  740. static void dmda_pre_exec_hook(struct starpu_task *task)
  741. {
  742. unsigned sched_ctx_id = task->sched_ctx;
  743. int workerid = starpu_worker_get_id();
  744. struct _starpu_dmda_data *dt = (struct _starpu_dmda_data*)starpu_sched_ctx_get_policy_data(sched_ctx_id);
  745. struct _starpu_fifo_taskq *fifo = dt->queue_array[workerid];
  746. double model = task->predicted;
  747. double transfer_model = task->predicted_transfer;
  748. starpu_pthread_mutex_t *sched_mutex;
  749. starpu_pthread_cond_t *sched_cond;
  750. starpu_worker_get_sched_condition(workerid, &sched_mutex, &sched_cond);
  751. /* Once the task is executing, we can update the predicted amount
  752. * of work. */
  753. STARPU_PTHREAD_MUTEX_LOCK(sched_mutex);
  754. if(!isnan(transfer_model))
  755. {
  756. fifo->exp_len-= transfer_model;
  757. fifo->exp_start = starpu_timing_now() + model;
  758. fifo->exp_end= fifo->exp_start + fifo->exp_len;
  759. }
  760. STARPU_PTHREAD_MUTEX_UNLOCK(sched_mutex);
  761. }
  762. static void dmda_push_task_notify(struct starpu_task *task, int workerid, unsigned sched_ctx_id)
  763. {
  764. struct _starpu_dmda_data *dt = (struct _starpu_dmda_data*)starpu_sched_ctx_get_policy_data(sched_ctx_id);
  765. struct _starpu_fifo_taskq *fifo = dt->queue_array[workerid];
  766. /* Compute the expected penality */
  767. enum starpu_perfmodel_archtype perf_arch = starpu_worker_get_perf_archtype(workerid);
  768. unsigned memory_node = starpu_worker_get_memory_node(workerid);
  769. double predicted = starpu_task_expected_length(task, perf_arch,
  770. starpu_task_get_implementation(task));
  771. double predicted_transfer = starpu_task_expected_data_transfer_time(memory_node, task);
  772. starpu_pthread_mutex_t *sched_mutex;
  773. starpu_pthread_cond_t *sched_cond;
  774. starpu_worker_get_sched_condition(workerid, &sched_mutex, &sched_cond);
  775. /* Update the predictions */
  776. STARPU_PTHREAD_MUTEX_LOCK(sched_mutex);
  777. /* Sometimes workers didn't take the tasks as early as we expected */
  778. fifo->exp_start = STARPU_MAX(fifo->exp_start, starpu_timing_now());
  779. fifo->exp_end = fifo->exp_start + fifo->exp_len;
  780. /* If there is no prediction available, we consider the task has a null length */
  781. if (!isnan(predicted_transfer))
  782. {
  783. if (starpu_timing_now() + predicted_transfer < fifo->exp_end)
  784. {
  785. /* We may hope that the transfer will be finished by
  786. * the start of the task. */
  787. predicted_transfer = 0;
  788. }
  789. else
  790. {
  791. /* The transfer will not be finished by then, take the
  792. * remainder into account */
  793. predicted_transfer = (starpu_timing_now() + predicted_transfer) - fifo->exp_end;
  794. }
  795. task->predicted_transfer = predicted_transfer;
  796. fifo->exp_end += predicted_transfer;
  797. fifo->exp_len += predicted_transfer;
  798. }
  799. /* If there is no prediction available, we consider the task has a null length */
  800. if (!isnan(predicted))
  801. {
  802. task->predicted = predicted;
  803. fifo->exp_end += predicted;
  804. fifo->exp_len += predicted;
  805. }
  806. fifo->ntasks++;
  807. STARPU_PTHREAD_MUTEX_UNLOCK(sched_mutex);
  808. }
  809. static void dmda_post_exec_hook(struct starpu_task * task)
  810. {
  811. struct _starpu_dmda_data *dt = (struct _starpu_dmda_data*)starpu_sched_ctx_get_policy_data(task->sched_ctx);
  812. int workerid = starpu_worker_get_id();
  813. struct _starpu_fifo_taskq *fifo = dt->queue_array[workerid];
  814. starpu_pthread_mutex_t *sched_mutex;
  815. starpu_pthread_cond_t *sched_cond;
  816. starpu_worker_get_sched_condition(workerid, &sched_mutex, &sched_cond);
  817. STARPU_PTHREAD_MUTEX_LOCK(sched_mutex);
  818. if(task->execute_on_a_specific_worker)
  819. fifo->ntasks--;
  820. fifo->exp_start = starpu_timing_now();
  821. fifo->exp_end = fifo->exp_start + fifo->exp_len;
  822. STARPU_PTHREAD_MUTEX_UNLOCK(sched_mutex);
  823. }
  824. struct starpu_sched_policy _starpu_sched_dm_policy =
  825. {
  826. .init_sched = initialize_dmda_policy,
  827. .deinit_sched = deinitialize_dmda_policy,
  828. .add_workers = dmda_add_workers ,
  829. .remove_workers = dmda_remove_workers,
  830. .push_task = dm_push_task,
  831. .pop_task = dmda_pop_task,
  832. .pre_exec_hook = NULL,
  833. .post_exec_hook = dmda_post_exec_hook,
  834. .pop_every_task = dmda_pop_every_task,
  835. .policy_name = "dm",
  836. .policy_description = "performance model"
  837. };
  838. struct starpu_sched_policy _starpu_sched_dmda_policy =
  839. {
  840. .init_sched = initialize_dmda_policy,
  841. .deinit_sched = deinitialize_dmda_policy,
  842. .add_workers = dmda_add_workers ,
  843. .remove_workers = dmda_remove_workers,
  844. .push_task = dmda_push_task,
  845. .push_task_notify = dmda_push_task_notify,
  846. .pop_task = dmda_pop_task,
  847. .pre_exec_hook = dmda_pre_exec_hook,
  848. .post_exec_hook = dmda_post_exec_hook,
  849. .pop_every_task = dmda_pop_every_task,
  850. .policy_name = "dmda",
  851. .policy_description = "data-aware performance model"
  852. };
  853. struct starpu_sched_policy _starpu_sched_dmda_sorted_policy =
  854. {
  855. .init_sched = initialize_dmda_sorted_policy,
  856. .deinit_sched = deinitialize_dmda_policy,
  857. .add_workers = dmda_add_workers ,
  858. .remove_workers = dmda_remove_workers,
  859. .push_task = dmda_push_sorted_task,
  860. .push_task_notify = dmda_push_task_notify,
  861. .pop_task = dmda_pop_ready_task,
  862. .pre_exec_hook = dmda_pre_exec_hook,
  863. .post_exec_hook = dmda_post_exec_hook,
  864. .pop_every_task = dmda_pop_every_task,
  865. .policy_name = "dmdas",
  866. .policy_description = "data-aware performance model (sorted)"
  867. };
  868. struct starpu_sched_policy _starpu_sched_dmda_ready_policy =
  869. {
  870. .init_sched = initialize_dmda_policy,
  871. .deinit_sched = deinitialize_dmda_policy,
  872. .add_workers = dmda_add_workers ,
  873. .remove_workers = dmda_remove_workers,
  874. .push_task = dmda_push_task,
  875. .push_task_notify = dmda_push_task_notify,
  876. .pop_task = dmda_pop_ready_task,
  877. .pre_exec_hook = dmda_pre_exec_hook,
  878. .post_exec_hook = dmda_post_exec_hook,
  879. .pop_every_task = dmda_pop_every_task,
  880. .policy_name = "dmdar",
  881. .policy_description = "data-aware performance model (ready)"
  882. };