deque_modeling_policy_data_aware.c 32 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057
  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. if (exp_end[worker_ctx][nimpl] < best_exp_end)
  513. {
  514. /* a better solution was found */
  515. best_exp_end = exp_end[worker_ctx][nimpl];
  516. nimpl_best = nimpl;
  517. }
  518. if (isnan(local_power[worker_ctx][nimpl]))
  519. local_power[worker_ctx][nimpl] = 0.;
  520. }
  521. worker_ctx++;
  522. }
  523. *forced_worker = unknown?ntasks_best:-1;
  524. *forced_impl = unknown?nimpl_best:-1;
  525. *best_exp_endp = best_exp_end;
  526. *max_exp_endp = max_exp_end;
  527. }
  528. static int _dmda_push_task(struct starpu_task *task, unsigned prio, unsigned sched_ctx_id)
  529. {
  530. /* find the queue */
  531. unsigned worker, worker_ctx = 0;
  532. int best = -1, best_in_ctx = -1;
  533. int selected_impl = 0;
  534. double model_best = 0.0;
  535. double transfer_model_best = 0.0;
  536. /* this flag is set if the corresponding worker is selected because
  537. there is no performance prediction available yet */
  538. int forced_best = -1;
  539. int forced_impl = -1;
  540. struct _starpu_dmda_data *dt = (struct _starpu_dmda_data*)starpu_sched_ctx_get_policy_data(sched_ctx_id);
  541. struct starpu_worker_collection *workers = starpu_sched_ctx_get_worker_collection(sched_ctx_id);
  542. unsigned nworkers_ctx = workers->nworkers;
  543. double local_task_length[STARPU_NMAXWORKERS][STARPU_MAXIMPLEMENTATIONS];
  544. double local_data_penalty[STARPU_NMAXWORKERS][STARPU_MAXIMPLEMENTATIONS];
  545. double local_power[STARPU_NMAXWORKERS][STARPU_MAXIMPLEMENTATIONS];
  546. /* Expected end of this task on the workers */
  547. double exp_end[STARPU_NMAXWORKERS][STARPU_MAXIMPLEMENTATIONS];
  548. /* This is the minimum among the exp_end[] matrix */
  549. double best_exp_end;
  550. /* This is the maximum termination time of already-scheduled tasks over all workers */
  551. double max_exp_end = 0.0;
  552. double fitness[nworkers_ctx][STARPU_MAXIMPLEMENTATIONS];
  553. struct starpu_sched_ctx_iterator it;
  554. if(workers->init_iterator)
  555. workers->init_iterator(workers, &it);
  556. compute_all_performance_predictions(task,
  557. local_task_length,
  558. exp_end,
  559. &max_exp_end,
  560. &best_exp_end,
  561. local_data_penalty,
  562. local_power,
  563. &forced_best,
  564. &forced_impl, sched_ctx_id);
  565. double best_fitness = -1;
  566. unsigned nimpl;
  567. if (forced_best == -1)
  568. {
  569. while(workers->has_next(workers, &it))
  570. {
  571. worker = workers->get_next(workers, &it);
  572. for (nimpl = 0; nimpl < STARPU_MAXIMPLEMENTATIONS; nimpl++)
  573. {
  574. if (!starpu_worker_can_execute_task(worker, task, nimpl))
  575. {
  576. /* no one on that queue may execute this task */
  577. continue;
  578. }
  579. fitness[worker_ctx][nimpl] = dt->alpha*(exp_end[worker_ctx][nimpl] - best_exp_end)
  580. + dt->beta*(local_data_penalty[worker_ctx][nimpl])
  581. + dt->_gamma*(local_power[worker_ctx][nimpl]);
  582. if (exp_end[worker_ctx][nimpl] > max_exp_end)
  583. {
  584. /* This placement will make the computation
  585. * longer, take into account the idle
  586. * consumption of other cpus */
  587. fitness[worker_ctx][nimpl] += dt->_gamma * dt->idle_power * (exp_end[worker_ctx][nimpl] - max_exp_end) / 1000000.0;
  588. }
  589. if (best == -1 || fitness[worker_ctx][nimpl] < best_fitness)
  590. {
  591. /* we found a better solution */
  592. best_fitness = fitness[worker_ctx][nimpl];
  593. best = worker;
  594. best_in_ctx = worker_ctx;
  595. selected_impl = nimpl;
  596. //_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]);
  597. }
  598. }
  599. worker_ctx++;
  600. }
  601. }
  602. STARPU_ASSERT(forced_best != -1 || best != -1);
  603. if (forced_best != -1)
  604. {
  605. /* there is no prediction available for that task
  606. * with that arch we want to speed-up calibration time
  607. * so we force this measurement */
  608. best = forced_best;
  609. selected_impl = forced_impl;
  610. model_best = 0.0;
  611. transfer_model_best = 0.0;
  612. }
  613. else if (task->bundle)
  614. {
  615. enum starpu_perfmodel_archtype perf_arch = starpu_worker_get_perf_archtype(best_in_ctx);
  616. unsigned memory_node = starpu_worker_get_memory_node(best);
  617. model_best = starpu_task_expected_length(task, perf_arch, selected_impl);
  618. transfer_model_best = starpu_task_expected_data_transfer_time(memory_node, task);
  619. }
  620. else
  621. {
  622. model_best = local_task_length[best_in_ctx][selected_impl];
  623. transfer_model_best = local_data_penalty[best_in_ctx][selected_impl];
  624. }
  625. //_STARPU_DEBUG("Scheduler dmda: kernel (%u)\n", best_impl);
  626. starpu_task_set_implementation(task, selected_impl);
  627. /* we should now have the best worker in variable "best" */
  628. return push_task_on_best_worker(task, best, model_best, transfer_model_best, prio, sched_ctx_id);
  629. }
  630. static int dmda_push_sorted_task(struct starpu_task *task)
  631. {
  632. #ifdef STARPU_DEVEL
  633. #warning TODO: after defining a scheduling window, use that instead of empty_ctx_tasks
  634. #endif
  635. return _dmda_push_task(task, 1, task->sched_ctx);
  636. }
  637. static int dm_push_task(struct starpu_task *task)
  638. {
  639. return _dm_push_task(task, 0, task->sched_ctx);
  640. }
  641. static int dmda_push_task(struct starpu_task *task)
  642. {
  643. STARPU_ASSERT(task);
  644. return _dmda_push_task(task, 0, task->sched_ctx);
  645. }
  646. static void dmda_add_workers(unsigned sched_ctx_id, int *workerids, unsigned nworkers)
  647. {
  648. struct _starpu_dmda_data *dt = (struct _starpu_dmda_data*)starpu_sched_ctx_get_policy_data(sched_ctx_id);
  649. int workerid;
  650. unsigned i;
  651. for (i = 0; i < nworkers; i++)
  652. {
  653. workerid = workerids[i];
  654. /* if the worker has alreadry belonged to this context
  655. the queue and the synchronization variables have been already initialized */
  656. if(dt->queue_array[workerid] == NULL)
  657. dt->queue_array[workerid] = _starpu_create_fifo();
  658. }
  659. }
  660. static void dmda_remove_workers(unsigned sched_ctx_id, int *workerids, unsigned nworkers)
  661. {
  662. struct _starpu_dmda_data *dt = (struct _starpu_dmda_data*)starpu_sched_ctx_get_policy_data(sched_ctx_id);
  663. int workerid;
  664. unsigned i;
  665. for (i = 0; i < nworkers; i++)
  666. {
  667. workerid = workerids[i];
  668. if(dt->queue_array[workerid] != NULL)
  669. {
  670. _starpu_destroy_fifo(dt->queue_array[workerid]);
  671. dt->queue_array[workerid] = NULL;
  672. }
  673. }
  674. }
  675. static void initialize_dmda_policy(unsigned sched_ctx_id)
  676. {
  677. starpu_sched_ctx_create_worker_collection(sched_ctx_id, STARPU_WORKER_LIST);
  678. struct _starpu_dmda_data *dt = (struct _starpu_dmda_data*)malloc(sizeof(struct _starpu_dmda_data));
  679. dt->alpha = _STARPU_SCHED_ALPHA_DEFAULT;
  680. dt->beta = _STARPU_SCHED_BETA_DEFAULT;
  681. dt->_gamma = _STARPU_SCHED_GAMMA_DEFAULT;
  682. dt->idle_power = 0.0;
  683. starpu_sched_ctx_set_policy_data(sched_ctx_id, (void*)dt);
  684. dt->queue_array = (struct _starpu_fifo_taskq**)malloc(STARPU_NMAXWORKERS*sizeof(struct _starpu_fifo_taskq*));
  685. int i;
  686. for(i = 0; i < STARPU_NMAXWORKERS; i++)
  687. dt->queue_array[i] = NULL;
  688. const char *strval_alpha = getenv("STARPU_SCHED_ALPHA");
  689. if (strval_alpha)
  690. dt->alpha = atof(strval_alpha);
  691. const char *strval_beta = getenv("STARPU_SCHED_BETA");
  692. if (strval_beta)
  693. dt->beta = atof(strval_beta);
  694. const char *strval_gamma = getenv("STARPU_SCHED_GAMMA");
  695. if (strval_gamma)
  696. dt->_gamma = atof(strval_gamma);
  697. const char *strval_idle_power = getenv("STARPU_IDLE_POWER");
  698. if (strval_idle_power)
  699. dt->idle_power = atof(strval_idle_power);
  700. #ifdef STARPU_USE_TOP
  701. starpu_top_register_parameter_float("DMDA_ALPHA", &alpha,
  702. alpha_minimum, alpha_maximum, param_modified);
  703. starpu_top_register_parameter_float("DMDA_BETA", &beta,
  704. beta_minimum, beta_maximum, param_modified);
  705. starpu_top_register_parameter_float("DMDA_GAMMA", &_gamma,
  706. gamma_minimum, gamma_maximum, param_modified);
  707. starpu_top_register_parameter_float("DMDA_IDLE_POWER", &idle_power,
  708. idle_power_minimum, idle_power_maximum, param_modified);
  709. #endif /* !STARPU_USE_TOP */
  710. }
  711. static void initialize_dmda_sorted_policy(unsigned sched_ctx_id)
  712. {
  713. initialize_dmda_policy(sched_ctx_id);
  714. /* The application may use any integer */
  715. starpu_sched_ctx_set_min_priority(sched_ctx_id, INT_MIN);
  716. starpu_sched_ctx_set_max_priority(sched_ctx_id, INT_MAX);
  717. }
  718. static void deinitialize_dmda_policy(unsigned sched_ctx_id)
  719. {
  720. struct _starpu_dmda_data *dt = (struct _starpu_dmda_data*)starpu_sched_ctx_get_policy_data(sched_ctx_id);
  721. _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);
  722. free(dt->queue_array);
  723. free(dt);
  724. starpu_sched_ctx_delete_worker_collection(sched_ctx_id);
  725. }
  726. /* dmda_pre_exec_hook is called right after the data transfer is done and right
  727. * before the computation to begin, it is useful to update more precisely the
  728. * value of the expected start, end, length, etc... */
  729. static void dmda_pre_exec_hook(struct starpu_task *task)
  730. {
  731. unsigned sched_ctx_id = task->sched_ctx;
  732. int workerid = starpu_worker_get_id();
  733. struct _starpu_dmda_data *dt = (struct _starpu_dmda_data*)starpu_sched_ctx_get_policy_data(sched_ctx_id);
  734. struct _starpu_fifo_taskq *fifo = dt->queue_array[workerid];
  735. double model = task->predicted;
  736. double transfer_model = task->predicted_transfer;
  737. starpu_pthread_mutex_t *sched_mutex;
  738. starpu_pthread_cond_t *sched_cond;
  739. starpu_worker_get_sched_condition(workerid, &sched_mutex, &sched_cond);
  740. /* Once the task is executing, we can update the predicted amount
  741. * of work. */
  742. STARPU_PTHREAD_MUTEX_LOCK(sched_mutex);
  743. if(!isnan(model))
  744. {
  745. fifo->exp_len-= model;
  746. fifo->exp_start = starpu_timing_now() + model;
  747. fifo->exp_end= fifo->exp_start + fifo->exp_len;
  748. }
  749. STARPU_PTHREAD_MUTEX_UNLOCK(sched_mutex);
  750. }
  751. static void dmda_push_task_notify(struct starpu_task *task, int workerid, unsigned sched_ctx_id)
  752. {
  753. struct _starpu_dmda_data *dt = (struct _starpu_dmda_data*)starpu_sched_ctx_get_policy_data(sched_ctx_id);
  754. struct _starpu_fifo_taskq *fifo = dt->queue_array[workerid];
  755. /* Compute the expected penality */
  756. enum starpu_perfmodel_archtype perf_arch = starpu_worker_get_perf_archtype(workerid);
  757. unsigned memory_node = starpu_worker_get_memory_node(workerid);
  758. double predicted = starpu_task_expected_length(task, perf_arch,
  759. starpu_task_get_implementation(task));
  760. double predicted_transfer = starpu_task_expected_data_transfer_time(memory_node, task);
  761. starpu_pthread_mutex_t *sched_mutex;
  762. starpu_pthread_cond_t *sched_cond;
  763. starpu_worker_get_sched_condition(workerid, &sched_mutex, &sched_cond);
  764. /* Update the predictions */
  765. STARPU_PTHREAD_MUTEX_LOCK(sched_mutex);
  766. /* Sometimes workers didn't take the tasks as early as we expected */
  767. fifo->exp_start = STARPU_MAX(fifo->exp_start, starpu_timing_now());
  768. fifo->exp_end = fifo->exp_start + fifo->exp_len;
  769. /* If there is no prediction available, we consider the task has a null length */
  770. if (!isnan(predicted_transfer))
  771. {
  772. if (starpu_timing_now() + predicted_transfer < fifo->exp_end)
  773. {
  774. /* We may hope that the transfer will be finished by
  775. * the start of the task. */
  776. predicted_transfer = 0;
  777. }
  778. else
  779. {
  780. /* The transfer will not be finished by then, take the
  781. * remainder into account */
  782. predicted_transfer = (starpu_timing_now() + predicted_transfer) - fifo->exp_end;
  783. }
  784. task->predicted_transfer = predicted_transfer;
  785. fifo->exp_end += predicted_transfer;
  786. fifo->exp_len += predicted_transfer;
  787. }
  788. /* If there is no prediction available, we consider the task has a null length */
  789. if (!isnan(predicted))
  790. {
  791. task->predicted = predicted;
  792. fifo->exp_end += predicted;
  793. fifo->exp_len += predicted;
  794. }
  795. fifo->ntasks++;
  796. STARPU_PTHREAD_MUTEX_UNLOCK(sched_mutex);
  797. }
  798. static void dmda_post_exec_hook(struct starpu_task * task)
  799. {
  800. struct _starpu_dmda_data *dt = (struct _starpu_dmda_data*)starpu_sched_ctx_get_policy_data(task->sched_ctx);
  801. int workerid = starpu_worker_get_id();
  802. struct _starpu_fifo_taskq *fifo = dt->queue_array[workerid];
  803. starpu_pthread_mutex_t *sched_mutex;
  804. starpu_pthread_cond_t *sched_cond;
  805. starpu_worker_get_sched_condition(workerid, &sched_mutex, &sched_cond);
  806. STARPU_PTHREAD_MUTEX_LOCK(sched_mutex);
  807. if(task->execute_on_a_specific_worker)
  808. fifo->ntasks--;
  809. fifo->exp_start = starpu_timing_now();
  810. fifo->exp_end = fifo->exp_start + fifo->exp_len;
  811. STARPU_PTHREAD_MUTEX_UNLOCK(sched_mutex);
  812. }
  813. struct starpu_sched_policy _starpu_sched_dm_policy =
  814. {
  815. .init_sched = initialize_dmda_policy,
  816. .deinit_sched = deinitialize_dmda_policy,
  817. .add_workers = dmda_add_workers ,
  818. .remove_workers = dmda_remove_workers,
  819. .push_task = dm_push_task,
  820. .pop_task = dmda_pop_task,
  821. .pre_exec_hook = NULL,
  822. .post_exec_hook = dmda_post_exec_hook,
  823. .pop_every_task = dmda_pop_every_task,
  824. .policy_name = "dm",
  825. .policy_description = "performance model"
  826. };
  827. struct starpu_sched_policy _starpu_sched_dmda_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 = dmda_push_task,
  834. .push_task_notify = dmda_push_task_notify,
  835. .pop_task = dmda_pop_task,
  836. .pre_exec_hook = dmda_pre_exec_hook,
  837. .post_exec_hook = dmda_post_exec_hook,
  838. .pop_every_task = dmda_pop_every_task,
  839. .policy_name = "dmda",
  840. .policy_description = "data-aware performance model"
  841. };
  842. struct starpu_sched_policy _starpu_sched_dmda_sorted_policy =
  843. {
  844. .init_sched = initialize_dmda_sorted_policy,
  845. .deinit_sched = deinitialize_dmda_policy,
  846. .add_workers = dmda_add_workers ,
  847. .remove_workers = dmda_remove_workers,
  848. .push_task = dmda_push_sorted_task,
  849. .push_task_notify = dmda_push_task_notify,
  850. .pop_task = dmda_pop_ready_task,
  851. .pre_exec_hook = dmda_pre_exec_hook,
  852. .post_exec_hook = dmda_post_exec_hook,
  853. .pop_every_task = dmda_pop_every_task,
  854. .policy_name = "dmdas",
  855. .policy_description = "data-aware performance model (sorted)"
  856. };
  857. struct starpu_sched_policy _starpu_sched_dmda_ready_policy =
  858. {
  859. .init_sched = initialize_dmda_policy,
  860. .deinit_sched = deinitialize_dmda_policy,
  861. .add_workers = dmda_add_workers ,
  862. .remove_workers = dmda_remove_workers,
  863. .push_task = dmda_push_task,
  864. .push_task_notify = dmda_push_task_notify,
  865. .pop_task = dmda_pop_ready_task,
  866. .pre_exec_hook = dmda_pre_exec_hook,
  867. .post_exec_hook = dmda_post_exec_hook,
  868. .pop_every_task = dmda_pop_every_task,
  869. .policy_name = "dmdar",
  870. .policy_description = "data-aware performance model (ready)"
  871. };