deque_modeling_policy_data_aware.c 33 KB

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