deque_modeling_policy_data_aware.c 33 KB

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