deque_modeling_policy_data_aware.c 31 KB

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