deque_modeling_policy_data_aware.c 32 KB

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