deque_modeling_policy_data_aware.c 32 KB

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