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