deque_modeling_policy_data_aware.c 40 KB

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