deque_modeling_policy_data_aware.c 33 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069
  1. /* StarPU --- Runtime system for heterogeneous multicore architectures.
  2. *
  3. * Copyright (C) 2010-2014 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. unsigned nworkers,
  418. double local_task_length[nworkers][STARPU_MAXIMPLEMENTATIONS],
  419. double exp_end[nworkers][STARPU_MAXIMPLEMENTATIONS],
  420. double *max_exp_endp,
  421. double *best_exp_endp,
  422. double local_data_penalty[nworkers][STARPU_MAXIMPLEMENTATIONS],
  423. double local_power[nworkers][STARPU_MAXIMPLEMENTATIONS],
  424. int *forced_worker, int *forced_impl, unsigned sched_ctx_id)
  425. {
  426. int calibrating = 0;
  427. double max_exp_end = DBL_MIN;
  428. double best_exp_end = DBL_MAX;
  429. int ntasks_best = -1;
  430. int nimpl_best = 0;
  431. double ntasks_best_end = 0.0;
  432. /* A priori, we know all estimations */
  433. int unknown = 0;
  434. unsigned worker, worker_ctx = 0;
  435. unsigned nimpl;
  436. starpu_task_bundle_t bundle = task->bundle;
  437. struct _starpu_dmda_data *dt = (struct _starpu_dmda_data*)starpu_sched_ctx_get_policy_data(sched_ctx_id);
  438. struct starpu_worker_collection *workers = starpu_sched_ctx_get_worker_collection(sched_ctx_id);
  439. struct starpu_sched_ctx_iterator it;
  440. if(workers->init_iterator)
  441. workers->init_iterator(workers, &it);
  442. while(workers->has_next(workers, &it))
  443. {
  444. worker = workers->get_next(workers, &it);
  445. struct _starpu_fifo_taskq *fifo = dt->queue_array[worker];
  446. struct starpu_perfmodel_arch* perf_arch = starpu_worker_get_perf_archtype(worker);
  447. unsigned memory_node = starpu_worker_get_memory_node(worker);
  448. /* Sometimes workers didn't take the tasks as early as we expected */
  449. double exp_start = STARPU_MAX(fifo->exp_start, starpu_timing_now());
  450. for(nimpl = 0; nimpl < STARPU_MAXIMPLEMENTATIONS; nimpl++)
  451. {
  452. if (!starpu_worker_can_execute_task(worker, task, nimpl))
  453. {
  454. /* no one on that queue may execute this task */
  455. continue;
  456. }
  457. STARPU_ASSERT_MSG(fifo != NULL, "worker %d ctx %d\n", worker, sched_ctx_id);
  458. exp_end[worker_ctx][nimpl] = exp_start + fifo->exp_len;
  459. if (exp_end[worker_ctx][nimpl] > max_exp_end)
  460. max_exp_end = exp_end[worker_ctx][nimpl];
  461. //_STARPU_DEBUG("Scheduler dmda: task length (%lf) worker (%u) kernel (%u) \n", local_task_length[worker][nimpl],worker,nimpl);
  462. if (bundle)
  463. {
  464. /* TODO : conversion time */
  465. local_task_length[worker_ctx][nimpl] = starpu_task_bundle_expected_length(bundle, perf_arch, nimpl);
  466. local_data_penalty[worker_ctx][nimpl] = starpu_task_bundle_expected_data_transfer_time(bundle, memory_node);
  467. local_power[worker_ctx][nimpl] = starpu_task_bundle_expected_power(bundle, perf_arch,nimpl);
  468. }
  469. else
  470. {
  471. local_task_length[worker_ctx][nimpl] = starpu_task_expected_length(task, perf_arch, nimpl);
  472. local_data_penalty[worker_ctx][nimpl] = starpu_task_expected_data_transfer_time(memory_node, task);
  473. local_power[worker_ctx][nimpl] = starpu_task_expected_power(task, perf_arch,nimpl);
  474. double conversion_time = starpu_task_expected_conversion_time(task, perf_arch, nimpl);
  475. if (conversion_time > 0.0)
  476. local_task_length[worker_ctx][nimpl] += conversion_time;
  477. }
  478. double ntasks_end = fifo->ntasks / starpu_worker_get_relative_speedup(perf_arch);
  479. /*
  480. * This implements a default greedy scheduler for the
  481. * case of tasks which have no performance model, or
  482. * whose performance model is not calibrated yet.
  483. *
  484. * It simply uses the number of tasks already pushed to
  485. * the workers, divided by the relative performance of
  486. * a CPU and of a GPU.
  487. *
  488. * This is always computed, but the ntasks_best
  489. * selection is only really used if the task indeed has
  490. * no performance model, or is not calibrated yet.
  491. */
  492. if (ntasks_best == -1
  493. /* Always compute the greedy decision, at least for
  494. * the tasks with no performance model. */
  495. || (!calibrating && ntasks_end < ntasks_best_end)
  496. /* The performance model of this task is not
  497. * calibrated on this worker, try to run it there
  498. * to calibrate it there. */
  499. || (!calibrating && isnan(local_task_length[worker_ctx][nimpl]))
  500. /* the performance model of this task is not
  501. * calibrated on this worker either, rather run it
  502. * there if this one is low on scheduled tasks. */
  503. || (calibrating && isnan(local_task_length[worker_ctx][nimpl]) && ntasks_end < ntasks_best_end)
  504. )
  505. {
  506. ntasks_best_end = ntasks_end;
  507. ntasks_best = worker;
  508. nimpl_best = nimpl;
  509. }
  510. if (isnan(local_task_length[worker_ctx][nimpl]))
  511. /* we are calibrating, we want to speed-up calibration time
  512. * so we privilege non-calibrated tasks (but still
  513. * greedily distribute them to avoid dumb schedules) */
  514. calibrating = 1;
  515. if (isnan(local_task_length[worker_ctx][nimpl])
  516. || _STARPU_IS_ZERO(local_task_length[worker_ctx][nimpl]))
  517. /* there is no prediction available for that task
  518. * with that arch (yet or at all), so switch to a greedy strategy */
  519. unknown = 1;
  520. if (unknown)
  521. continue;
  522. exp_end[worker_ctx][nimpl] = exp_start + fifo->exp_len + local_task_length[worker_ctx][nimpl];
  523. if (exp_end[worker_ctx][nimpl] < best_exp_end)
  524. {
  525. /* a better solution was found */
  526. best_exp_end = exp_end[worker_ctx][nimpl];
  527. nimpl_best = nimpl;
  528. }
  529. if (isnan(local_power[worker_ctx][nimpl]))
  530. local_power[worker_ctx][nimpl] = 0.;
  531. }
  532. worker_ctx++;
  533. }
  534. *forced_worker = unknown?ntasks_best:-1;
  535. *forced_impl = unknown?nimpl_best:-1;
  536. *best_exp_endp = best_exp_end;
  537. *max_exp_endp = max_exp_end;
  538. }
  539. static int _dmda_push_task(struct starpu_task *task, unsigned prio, unsigned sched_ctx_id)
  540. {
  541. /* find the queue */
  542. unsigned worker, worker_ctx = 0;
  543. int best = -1, best_in_ctx = -1;
  544. int selected_impl = 0;
  545. double model_best = 0.0;
  546. double transfer_model_best = 0.0;
  547. /* this flag is set if the corresponding worker is selected because
  548. there is no performance prediction available yet */
  549. int forced_best = -1;
  550. int forced_impl = -1;
  551. struct _starpu_dmda_data *dt = (struct _starpu_dmda_data*)starpu_sched_ctx_get_policy_data(sched_ctx_id);
  552. struct starpu_worker_collection *workers = starpu_sched_ctx_get_worker_collection(sched_ctx_id);
  553. unsigned nworkers_ctx = workers->nworkers;
  554. double local_task_length[nworkers_ctx][STARPU_MAXIMPLEMENTATIONS];
  555. double local_data_penalty[nworkers_ctx][STARPU_MAXIMPLEMENTATIONS];
  556. double local_power[nworkers_ctx][STARPU_MAXIMPLEMENTATIONS];
  557. /* Expected end of this task on the workers */
  558. double exp_end[nworkers_ctx][STARPU_MAXIMPLEMENTATIONS];
  559. /* This is the minimum among the exp_end[] matrix */
  560. double best_exp_end;
  561. /* This is the maximum termination time of already-scheduled tasks over all workers */
  562. double max_exp_end = 0.0;
  563. double fitness[nworkers_ctx][STARPU_MAXIMPLEMENTATIONS];
  564. struct starpu_sched_ctx_iterator it;
  565. if(workers->init_iterator)
  566. workers->init_iterator(workers, &it);
  567. compute_all_performance_predictions(task,
  568. nworkers_ctx,
  569. local_task_length,
  570. exp_end,
  571. &max_exp_end,
  572. &best_exp_end,
  573. local_data_penalty,
  574. local_power,
  575. &forced_best,
  576. &forced_impl, sched_ctx_id);
  577. double best_fitness = -1;
  578. unsigned nimpl;
  579. if (forced_best == -1)
  580. {
  581. while(workers->has_next(workers, &it))
  582. {
  583. worker = workers->get_next(workers, &it);
  584. for (nimpl = 0; nimpl < STARPU_MAXIMPLEMENTATIONS; nimpl++)
  585. {
  586. if (!starpu_worker_can_execute_task(worker, task, nimpl))
  587. {
  588. /* no one on that queue may execute this task */
  589. continue;
  590. }
  591. fitness[worker_ctx][nimpl] = dt->alpha*(exp_end[worker_ctx][nimpl] - best_exp_end)
  592. + dt->beta*(local_data_penalty[worker_ctx][nimpl])
  593. + dt->_gamma*(local_power[worker_ctx][nimpl]);
  594. if (exp_end[worker_ctx][nimpl] > max_exp_end)
  595. {
  596. /* This placement will make the computation
  597. * longer, take into account the idle
  598. * consumption of other cpus */
  599. fitness[worker_ctx][nimpl] += dt->_gamma * dt->idle_power * (exp_end[worker_ctx][nimpl] - max_exp_end) / 1000000.0;
  600. }
  601. if (best == -1 || fitness[worker_ctx][nimpl] < best_fitness)
  602. {
  603. /* we found a better solution */
  604. best_fitness = fitness[worker_ctx][nimpl];
  605. best = worker;
  606. best_in_ctx = worker_ctx;
  607. selected_impl = nimpl;
  608. //_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]);
  609. }
  610. }
  611. worker_ctx++;
  612. }
  613. }
  614. STARPU_ASSERT(forced_best != -1 || best != -1);
  615. if (forced_best != -1)
  616. {
  617. /* there is no prediction available for that task
  618. * with that arch we want to speed-up calibration time
  619. * so we force this measurement */
  620. best = forced_best;
  621. selected_impl = forced_impl;
  622. model_best = 0.0;
  623. transfer_model_best = 0.0;
  624. }
  625. else if (task->bundle)
  626. {
  627. struct starpu_perfmodel_arch* perf_arch = starpu_worker_get_perf_archtype(best_in_ctx);
  628. unsigned memory_node = starpu_worker_get_memory_node(best);
  629. model_best = starpu_task_expected_length(task, perf_arch, selected_impl);
  630. transfer_model_best = starpu_task_expected_data_transfer_time(memory_node, task);
  631. }
  632. else
  633. {
  634. model_best = local_task_length[best_in_ctx][selected_impl];
  635. transfer_model_best = local_data_penalty[best_in_ctx][selected_impl];
  636. }
  637. //_STARPU_DEBUG("Scheduler dmda: kernel (%u)\n", best_impl);
  638. starpu_task_set_implementation(task, selected_impl);
  639. /* we should now have the best worker in variable "best" */
  640. return push_task_on_best_worker(task, best, model_best, transfer_model_best, prio, sched_ctx_id);
  641. }
  642. static int dmda_push_sorted_task(struct starpu_task *task)
  643. {
  644. #ifdef STARPU_DEVEL
  645. #warning TODO: after defining a scheduling window, use that instead of empty_ctx_tasks
  646. #endif
  647. return _dmda_push_task(task, 1, task->sched_ctx);
  648. }
  649. static int dm_push_task(struct starpu_task *task)
  650. {
  651. return _dm_push_task(task, 0, task->sched_ctx);
  652. }
  653. static int dmda_push_task(struct starpu_task *task)
  654. {
  655. STARPU_ASSERT(task);
  656. return _dmda_push_task(task, 0, task->sched_ctx);
  657. }
  658. static void dmda_add_workers(unsigned sched_ctx_id, int *workerids, unsigned nworkers)
  659. {
  660. struct _starpu_dmda_data *dt = (struct _starpu_dmda_data*)starpu_sched_ctx_get_policy_data(sched_ctx_id);
  661. int workerid;
  662. unsigned i;
  663. for (i = 0; i < nworkers; i++)
  664. {
  665. workerid = workerids[i];
  666. /* if the worker has alreadry belonged to this context
  667. the queue and the synchronization variables have been already initialized */
  668. if(dt->queue_array[workerid] == NULL)
  669. dt->queue_array[workerid] = _starpu_create_fifo();
  670. }
  671. }
  672. static void dmda_remove_workers(unsigned sched_ctx_id, int *workerids, unsigned nworkers)
  673. {
  674. struct _starpu_dmda_data *dt = (struct _starpu_dmda_data*)starpu_sched_ctx_get_policy_data(sched_ctx_id);
  675. int workerid;
  676. unsigned i;
  677. for (i = 0; i < nworkers; i++)
  678. {
  679. workerid = workerids[i];
  680. if(dt->queue_array[workerid] != NULL)
  681. {
  682. _starpu_destroy_fifo(dt->queue_array[workerid]);
  683. dt->queue_array[workerid] = NULL;
  684. }
  685. }
  686. }
  687. static void initialize_dmda_policy(unsigned sched_ctx_id)
  688. {
  689. starpu_sched_ctx_create_worker_collection(sched_ctx_id, STARPU_WORKER_LIST);
  690. struct _starpu_dmda_data *dt = (struct _starpu_dmda_data*)malloc(sizeof(struct _starpu_dmda_data));
  691. dt->alpha = _STARPU_SCHED_ALPHA_DEFAULT;
  692. dt->beta = _STARPU_SCHED_BETA_DEFAULT;
  693. dt->_gamma = _STARPU_SCHED_GAMMA_DEFAULT;
  694. dt->idle_power = 0.0;
  695. starpu_sched_ctx_set_policy_data(sched_ctx_id, (void*)dt);
  696. dt->queue_array = (struct _starpu_fifo_taskq**)malloc(STARPU_NMAXWORKERS*sizeof(struct _starpu_fifo_taskq*));
  697. int i;
  698. for(i = 0; i < STARPU_NMAXWORKERS; i++)
  699. dt->queue_array[i] = NULL;
  700. const char *strval_alpha = getenv("STARPU_SCHED_ALPHA");
  701. if (strval_alpha)
  702. dt->alpha = atof(strval_alpha);
  703. const char *strval_beta = getenv("STARPU_SCHED_BETA");
  704. if (strval_beta)
  705. dt->beta = atof(strval_beta);
  706. const char *strval_gamma = getenv("STARPU_SCHED_GAMMA");
  707. if (strval_gamma)
  708. dt->_gamma = atof(strval_gamma);
  709. const char *strval_idle_power = getenv("STARPU_IDLE_POWER");
  710. if (strval_idle_power)
  711. dt->idle_power = atof(strval_idle_power);
  712. #ifdef STARPU_USE_TOP
  713. /* FIXME: broken, needs to access context variable */
  714. starpu_top_register_parameter_float("DMDA_ALPHA", &alpha,
  715. alpha_minimum, alpha_maximum, param_modified);
  716. starpu_top_register_parameter_float("DMDA_BETA", &beta,
  717. beta_minimum, beta_maximum, param_modified);
  718. starpu_top_register_parameter_float("DMDA_GAMMA", &_gamma,
  719. gamma_minimum, gamma_maximum, param_modified);
  720. starpu_top_register_parameter_float("DMDA_IDLE_POWER", &idle_power,
  721. idle_power_minimum, idle_power_maximum, param_modified);
  722. #endif /* !STARPU_USE_TOP */
  723. }
  724. static void initialize_dmda_sorted_policy(unsigned sched_ctx_id)
  725. {
  726. initialize_dmda_policy(sched_ctx_id);
  727. /* The application may use any integer */
  728. if (starpu_sched_ctx_min_priority_is_set(sched_ctx_id) == 0)
  729. starpu_sched_ctx_set_min_priority(sched_ctx_id, INT_MIN);
  730. if (starpu_sched_ctx_max_priority_is_set(sched_ctx_id) == 0)
  731. starpu_sched_ctx_set_max_priority(sched_ctx_id, INT_MAX);
  732. }
  733. static void deinitialize_dmda_policy(unsigned sched_ctx_id)
  734. {
  735. struct _starpu_dmda_data *dt = (struct _starpu_dmda_data*)starpu_sched_ctx_get_policy_data(sched_ctx_id);
  736. _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);
  737. free(dt->queue_array);
  738. free(dt);
  739. starpu_sched_ctx_delete_worker_collection(sched_ctx_id);
  740. }
  741. /* dmda_pre_exec_hook is called right after the data transfer is done and right
  742. * before the computation to begin, it is useful to update more precisely the
  743. * value of the expected start, end, length, etc... */
  744. static void dmda_pre_exec_hook(struct starpu_task *task)
  745. {
  746. unsigned sched_ctx_id = task->sched_ctx;
  747. int workerid = starpu_worker_get_id();
  748. struct _starpu_dmda_data *dt = (struct _starpu_dmda_data*)starpu_sched_ctx_get_policy_data(sched_ctx_id);
  749. struct _starpu_fifo_taskq *fifo = dt->queue_array[workerid];
  750. double model = task->predicted;
  751. starpu_pthread_mutex_t *sched_mutex;
  752. starpu_pthread_cond_t *sched_cond;
  753. starpu_worker_get_sched_condition(workerid, &sched_mutex, &sched_cond);
  754. /* Once the task is executing, we can update the predicted amount
  755. * of work. */
  756. STARPU_PTHREAD_MUTEX_LOCK(sched_mutex);
  757. if(!isnan(model))
  758. {
  759. /* We now start the computation, get rid of it in the completion
  760. * prediction */
  761. fifo->exp_len-= model;
  762. fifo->exp_start = starpu_timing_now() + model;
  763. fifo->exp_end= fifo->exp_start + fifo->exp_len;
  764. }
  765. STARPU_PTHREAD_MUTEX_UNLOCK(sched_mutex);
  766. }
  767. static void dmda_push_task_notify(struct starpu_task *task, int workerid, int perf_workerid, unsigned sched_ctx_id)
  768. {
  769. struct _starpu_dmda_data *dt = (struct _starpu_dmda_data*)starpu_sched_ctx_get_policy_data(sched_ctx_id);
  770. struct _starpu_fifo_taskq *fifo = dt->queue_array[workerid];
  771. /* Compute the expected penality */
  772. struct starpu_perfmodel_arch *perf_arch = starpu_worker_get_perf_archtype(perf_workerid);
  773. unsigned memory_node = starpu_worker_get_memory_node(workerid);
  774. double predicted = starpu_task_expected_length(task, perf_arch,
  775. starpu_task_get_implementation(task));
  776. double predicted_transfer = starpu_task_expected_data_transfer_time(memory_node, task);
  777. starpu_pthread_mutex_t *sched_mutex;
  778. starpu_pthread_cond_t *sched_cond;
  779. starpu_worker_get_sched_condition(workerid, &sched_mutex, &sched_cond);
  780. /* Update the predictions */
  781. STARPU_PTHREAD_MUTEX_LOCK(sched_mutex);
  782. /* Sometimes workers didn't take the tasks as early as we expected */
  783. fifo->exp_start = STARPU_MAX(fifo->exp_start, starpu_timing_now());
  784. fifo->exp_end = fifo->exp_start + fifo->exp_len;
  785. /* If there is no prediction available, we consider the task has a null length */
  786. if (!isnan(predicted_transfer))
  787. {
  788. if (starpu_timing_now() + predicted_transfer < fifo->exp_end)
  789. {
  790. /* We may hope that the transfer will be finished by
  791. * the start of the task. */
  792. predicted_transfer = 0;
  793. }
  794. else
  795. {
  796. /* The transfer will not be finished by then, take the
  797. * remainder into account */
  798. predicted_transfer = (starpu_timing_now() + predicted_transfer) - fifo->exp_end;
  799. }
  800. task->predicted_transfer = predicted_transfer;
  801. fifo->exp_end += predicted_transfer;
  802. fifo->exp_len += predicted_transfer;
  803. }
  804. /* If there is no prediction available, we consider the task has a null length */
  805. if (!isnan(predicted))
  806. {
  807. task->predicted = predicted;
  808. fifo->exp_end += predicted;
  809. fifo->exp_len += predicted;
  810. }
  811. fifo->ntasks++;
  812. STARPU_PTHREAD_MUTEX_UNLOCK(sched_mutex);
  813. }
  814. static void dmda_post_exec_hook(struct starpu_task * task)
  815. {
  816. struct _starpu_dmda_data *dt = (struct _starpu_dmda_data*)starpu_sched_ctx_get_policy_data(task->sched_ctx);
  817. int workerid = starpu_worker_get_id();
  818. struct _starpu_fifo_taskq *fifo = dt->queue_array[workerid];
  819. starpu_pthread_mutex_t *sched_mutex;
  820. starpu_pthread_cond_t *sched_cond;
  821. starpu_worker_get_sched_condition(workerid, &sched_mutex, &sched_cond);
  822. STARPU_PTHREAD_MUTEX_LOCK(sched_mutex);
  823. if(task->execute_on_a_specific_worker)
  824. fifo->ntasks--;
  825. fifo->exp_start = starpu_timing_now();
  826. fifo->exp_end = fifo->exp_start + fifo->exp_len;
  827. STARPU_PTHREAD_MUTEX_UNLOCK(sched_mutex);
  828. }
  829. struct starpu_sched_policy _starpu_sched_dm_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 = dm_push_task,
  836. .pop_task = dmda_pop_task,
  837. .pre_exec_hook = NULL,
  838. .post_exec_hook = dmda_post_exec_hook,
  839. .pop_every_task = dmda_pop_every_task,
  840. .policy_name = "dm",
  841. .policy_description = "performance model"
  842. };
  843. struct starpu_sched_policy _starpu_sched_dmda_policy =
  844. {
  845. .init_sched = initialize_dmda_policy,
  846. .deinit_sched = deinitialize_dmda_policy,
  847. .add_workers = dmda_add_workers ,
  848. .remove_workers = dmda_remove_workers,
  849. .push_task = dmda_push_task,
  850. .push_task_notify = dmda_push_task_notify,
  851. .pop_task = dmda_pop_task,
  852. .pre_exec_hook = dmda_pre_exec_hook,
  853. .post_exec_hook = dmda_post_exec_hook,
  854. .pop_every_task = dmda_pop_every_task,
  855. .policy_name = "dmda",
  856. .policy_description = "data-aware performance model"
  857. };
  858. struct starpu_sched_policy _starpu_sched_dmda_sorted_policy =
  859. {
  860. .init_sched = initialize_dmda_sorted_policy,
  861. .deinit_sched = deinitialize_dmda_policy,
  862. .add_workers = dmda_add_workers ,
  863. .remove_workers = dmda_remove_workers,
  864. .push_task = dmda_push_sorted_task,
  865. .push_task_notify = dmda_push_task_notify,
  866. .pop_task = dmda_pop_ready_task,
  867. .pre_exec_hook = dmda_pre_exec_hook,
  868. .post_exec_hook = dmda_post_exec_hook,
  869. .pop_every_task = dmda_pop_every_task,
  870. .policy_name = "dmdas",
  871. .policy_description = "data-aware performance model (sorted)"
  872. };
  873. struct starpu_sched_policy _starpu_sched_dmda_ready_policy =
  874. {
  875. .init_sched = initialize_dmda_policy,
  876. .deinit_sched = deinitialize_dmda_policy,
  877. .add_workers = dmda_add_workers ,
  878. .remove_workers = dmda_remove_workers,
  879. .push_task = dmda_push_task,
  880. .push_task_notify = dmda_push_task_notify,
  881. .pop_task = dmda_pop_ready_task,
  882. .pre_exec_hook = dmda_pre_exec_hook,
  883. .post_exec_hook = dmda_post_exec_hook,
  884. .pop_every_task = dmda_pop_every_task,
  885. .policy_name = "dmdar",
  886. .policy_description = "data-aware performance model (ready)"
  887. };