deque_modeling_policy_data_aware.c 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625
  1. /* StarPU --- Runtime system for heterogeneous multicore architectures.
  2. *
  3. * Copyright (C) 2010, 2011 Université de Bordeaux 1
  4. * Copyright (C) 2010 Centre National de la Recherche Scientifique
  5. *
  6. * StarPU is free software; you can redistribute it and/or modify
  7. * it under the terms of the GNU Lesser General Public License as published by
  8. * the Free Software Foundation; either version 2.1 of the License, or (at
  9. * your option) any later version.
  10. *
  11. * StarPU is distributed in the hope that it will be useful, but
  12. * WITHOUT ANY WARRANTY; without even the implied warranty of
  13. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
  14. *
  15. * See the GNU Lesser General Public License in COPYING.LGPL for more details.
  16. */
  17. /* Distributed queues using performance modeling to assign tasks */
  18. #include <limits.h>
  19. #include <core/workers.h>
  20. #include <sched_policies/fifo_queues.h>
  21. #include <core/perfmodel/perfmodel.h>
  22. #include <starpu_parameters.h>
  23. static unsigned nworkers;
  24. static struct starpu_fifo_taskq_s *queue_array[STARPU_NMAXWORKERS];
  25. static pthread_cond_t sched_cond[STARPU_NMAXWORKERS];
  26. static pthread_mutex_t sched_mutex[STARPU_NMAXWORKERS];
  27. static double alpha = STARPU_DEFAULT_ALPHA;
  28. static double beta = STARPU_DEFAULT_BETA;
  29. static double _gamma = STARPU_DEFAULT_GAMMA;
  30. #ifdef STARPU_VERBOSE
  31. static long int total_task_cnt = 0;
  32. static long int ready_task_cnt = 0;
  33. #endif
  34. static int count_non_ready_buffers(struct starpu_task *task, uint32_t node)
  35. {
  36. int cnt = 0;
  37. starpu_buffer_descr *descrs = task->buffers;
  38. unsigned nbuffers = task->cl->nbuffers;
  39. unsigned index;
  40. for (index = 0; index < nbuffers; index++)
  41. {
  42. starpu_buffer_descr *descr;
  43. starpu_data_handle handle;
  44. descr = &descrs[index];
  45. handle = descr->handle;
  46. int is_valid;
  47. starpu_data_query_status(handle, node, NULL, &is_valid, NULL);
  48. if (!is_valid)
  49. cnt++;
  50. }
  51. return cnt;
  52. }
  53. static struct starpu_task *_starpu_fifo_pop_first_ready_task(struct starpu_fifo_taskq_s *fifo_queue, unsigned node)
  54. {
  55. struct starpu_task *task = NULL, *current;
  56. if (fifo_queue->ntasks == 0)
  57. return NULL;
  58. if (fifo_queue->ntasks > 0)
  59. {
  60. fifo_queue->ntasks--;
  61. task = starpu_task_list_back(&fifo_queue->taskq);
  62. int first_task_priority = task->priority;
  63. current = task;
  64. int non_ready_best = INT_MAX;
  65. while (current)
  66. {
  67. int priority = current->priority;
  68. if (priority <= first_task_priority)
  69. {
  70. int non_ready = count_non_ready_buffers(current, node);
  71. if (non_ready < non_ready_best)
  72. {
  73. non_ready_best = non_ready;
  74. task = current;
  75. if (non_ready == 0)
  76. break;
  77. }
  78. }
  79. current = current->prev;
  80. }
  81. starpu_task_list_erase(&fifo_queue->taskq, task);
  82. STARPU_TRACE_JOB_POP(task, 0);
  83. }
  84. return task;
  85. }
  86. static struct starpu_task *dmda_pop_ready_task(void)
  87. {
  88. struct starpu_task *task;
  89. int workerid = starpu_worker_get_id();
  90. struct starpu_fifo_taskq_s *fifo = queue_array[workerid];
  91. unsigned node = starpu_worker_get_memory_node(workerid);
  92. task = _starpu_fifo_pop_first_ready_task(fifo, node);
  93. if (task) {
  94. double model = task->predicted;
  95. fifo->exp_len -= model;
  96. fifo->exp_start = starpu_timing_now() + model;
  97. fifo->exp_end = fifo->exp_start + fifo->exp_len;
  98. #ifdef STARPU_VERBOSE
  99. if (task->cl)
  100. {
  101. int non_ready = count_non_ready_buffers(task, starpu_worker_get_memory_node(workerid));
  102. if (non_ready == 0)
  103. ready_task_cnt++;
  104. }
  105. total_task_cnt++;
  106. #endif
  107. }
  108. return task;
  109. }
  110. static struct starpu_task *dmda_pop_task(void)
  111. {
  112. struct starpu_task *task;
  113. int workerid = starpu_worker_get_id();
  114. struct starpu_fifo_taskq_s *fifo = queue_array[workerid];
  115. task = _starpu_fifo_pop_task(fifo, -1);
  116. if (task) {
  117. double model = task->predicted;
  118. fifo->exp_len -= model;
  119. fifo->exp_start = starpu_timing_now() + model;
  120. fifo->exp_end = fifo->exp_start + fifo->exp_len;
  121. #ifdef STARPU_VERBOSE
  122. if (task->cl)
  123. {
  124. int non_ready = count_non_ready_buffers(task, starpu_worker_get_memory_node(workerid));
  125. if (non_ready == 0)
  126. ready_task_cnt++;
  127. }
  128. total_task_cnt++;
  129. #endif
  130. }
  131. return task;
  132. }
  133. static struct starpu_task *dmda_pop_every_task(void)
  134. {
  135. struct starpu_task *new_list;
  136. int workerid = starpu_worker_get_id();
  137. struct starpu_fifo_taskq_s *fifo = queue_array[workerid];
  138. new_list = _starpu_fifo_pop_every_task(fifo, &sched_mutex[workerid], workerid);
  139. while (new_list)
  140. {
  141. double model = new_list->predicted;
  142. fifo->exp_len -= model;
  143. fifo->exp_start = starpu_timing_now() + model;
  144. fifo->exp_end = fifo->exp_start + fifo->exp_len;
  145. new_list = new_list->next;
  146. }
  147. return new_list;
  148. }
  149. int _starpu_fifo_push_sorted_task(struct starpu_fifo_taskq_s *fifo_queue, pthread_mutex_t *sched_mutex, pthread_cond_t *sched_cond, struct starpu_task *task)
  150. {
  151. struct starpu_task_list *list = &fifo_queue->taskq;
  152. PTHREAD_MUTEX_LOCK(sched_mutex);
  153. STARPU_TRACE_JOB_PUSH(task, 0);
  154. if (list->head == NULL)
  155. {
  156. list->head = task;
  157. list->tail = task;
  158. task->prev = NULL;
  159. task->next = NULL;
  160. }
  161. else {
  162. struct starpu_task *current = list->head;
  163. struct starpu_task *prev = NULL;
  164. while (current)
  165. {
  166. if (current->priority >= task->priority)
  167. break;
  168. prev = current;
  169. current = current->next;
  170. }
  171. if (prev == NULL)
  172. {
  173. /* Insert at the front of the list */
  174. list->head->prev = task;
  175. task->prev = NULL;
  176. task->next = list->head;
  177. list->head = task;
  178. }
  179. else {
  180. if (current)
  181. {
  182. /* Insert between prev and current */
  183. task->prev = prev;
  184. prev->next = task;
  185. task->next = current;
  186. current->prev = task;
  187. }
  188. else {
  189. /* Insert at the tail of the list */
  190. list->tail->next = task;
  191. task->next = NULL;
  192. task->prev = list->tail;
  193. list->tail = task;
  194. }
  195. }
  196. }
  197. fifo_queue->ntasks++;
  198. fifo_queue->nprocessed++;
  199. PTHREAD_COND_SIGNAL(sched_cond);
  200. PTHREAD_MUTEX_UNLOCK(sched_mutex);
  201. return 0;
  202. }
  203. static int push_task_on_best_worker(struct starpu_task *task, int best_workerid, double predicted, int prio)
  204. {
  205. /* make sure someone coule execute that task ! */
  206. STARPU_ASSERT(best_workerid != -1);
  207. struct starpu_fifo_taskq_s *fifo;
  208. fifo = queue_array[best_workerid];
  209. fifo->exp_end += predicted;
  210. fifo->exp_len += predicted;
  211. task->predicted = predicted;
  212. unsigned memory_node = starpu_worker_get_memory_node(best_workerid);
  213. if (starpu_get_prefetch_flag())
  214. starpu_prefetch_task_input_on_node(task, memory_node);
  215. switch (prio) {
  216. case 1:
  217. return _starpu_fifo_push_prio_task(queue_array[best_workerid],
  218. &sched_mutex[best_workerid], &sched_cond[best_workerid], task);
  219. case 2:
  220. return _starpu_fifo_push_sorted_task(queue_array[best_workerid],
  221. &sched_mutex[best_workerid], &sched_cond[best_workerid], task);
  222. default:
  223. return _starpu_fifo_push_task(queue_array[best_workerid],
  224. &sched_mutex[best_workerid], &sched_cond[best_workerid], task);
  225. }
  226. }
  227. static int _dm_push_task(struct starpu_task *task, unsigned prio)
  228. {
  229. /* find the queue */
  230. struct starpu_fifo_taskq_s *fifo;
  231. unsigned worker;
  232. int best = -1;
  233. double best_exp_end = 0.0;
  234. double model_best = 0.0;
  235. int ntasks_best = -1;
  236. double ntasks_best_end = 0.0;
  237. /* A priori, we know all estimations */
  238. int unknown = 0;
  239. for (worker = 0; worker < nworkers; worker++)
  240. {
  241. double exp_end;
  242. fifo = queue_array[worker];
  243. fifo->exp_start = STARPU_MAX(fifo->exp_start, starpu_timing_now());
  244. fifo->exp_end = STARPU_MAX(fifo->exp_end, starpu_timing_now());
  245. if (!starpu_worker_may_execute_task(worker, task))
  246. {
  247. /* no one on that queue may execute this task */
  248. continue;
  249. }
  250. enum starpu_perf_archtype perf_arch = starpu_worker_get_perf_archtype(worker);
  251. double local_length = starpu_task_expected_length(task, perf_arch);
  252. double ntasks_end = fifo->ntasks / starpu_worker_get_relative_speedup(perf_arch);
  253. if (ntasks_best == -1 || ntasks_end < ntasks_best_end) {
  254. ntasks_best_end = ntasks_end;
  255. ntasks_best = worker;
  256. }
  257. if (local_length <= 0.0)
  258. /* there is no prediction available for that task
  259. * with that arch yet, we want to speed-up calibration time
  260. * so we switch to distributing tasks greedily */
  261. unknown = 1;
  262. if (unknown)
  263. continue;
  264. exp_end = fifo->exp_start + fifo->exp_len + local_length;
  265. if (best == -1 || exp_end < best_exp_end)
  266. {
  267. /* a better solution was found */
  268. best_exp_end = exp_end;
  269. best = worker;
  270. model_best = local_length;
  271. }
  272. }
  273. if (unknown) {
  274. best = ntasks_best;
  275. model_best = 0.0;
  276. }
  277. /* we should now have the best worker in variable "best" */
  278. return push_task_on_best_worker(task, best, model_best, prio);
  279. }
  280. static int _dmda_push_task(struct starpu_task *task, unsigned prio)
  281. {
  282. /* find the queue */
  283. struct starpu_fifo_taskq_s *fifo;
  284. unsigned worker;
  285. int best = -1;
  286. /* this flag is set if the corresponding worker is selected because
  287. there is no performance prediction available yet */
  288. int forced_best = -1;
  289. double local_task_length[nworkers];
  290. double local_data_penalty[nworkers];
  291. double local_power[nworkers];
  292. double exp_end[nworkers];
  293. double fitness[nworkers];
  294. double best_exp_end = 10e240;
  295. double model_best = 0.0;
  296. double penality_best = 0.0;
  297. int ntasks_best = -1;
  298. double ntasks_best_end = 0.0;
  299. /* A priori, we know all estimations */
  300. int unknown = 0;
  301. for (worker = 0; worker < nworkers; worker++)
  302. {
  303. fifo = queue_array[worker];
  304. fifo->exp_start = STARPU_MAX(fifo->exp_start, starpu_timing_now());
  305. fifo->exp_end = STARPU_MAX(fifo->exp_end, starpu_timing_now());
  306. if (!starpu_worker_may_execute_task(worker, task))
  307. {
  308. /* no one on that queue may execute this task */
  309. continue;
  310. }
  311. enum starpu_perf_archtype perf_arch = starpu_worker_get_perf_archtype(worker);
  312. local_task_length[worker] = starpu_task_expected_length(task, perf_arch);
  313. unsigned memory_node = starpu_worker_get_memory_node(worker);
  314. local_data_penalty[worker] = starpu_data_expected_penalty(memory_node, task);
  315. double ntasks_end = fifo->ntasks / starpu_worker_get_relative_speedup(perf_arch);
  316. if (ntasks_best == -1 || ntasks_end < ntasks_best_end) {
  317. ntasks_best_end = ntasks_end;
  318. ntasks_best = worker;
  319. }
  320. if (local_task_length[worker] <= 0.0)
  321. /* there is no prediction available for that task
  322. * with that arch yet, we want to speed-up calibration time
  323. * so we switch to distributing tasks greedily */
  324. unknown = 1;
  325. if (unknown)
  326. continue;
  327. exp_end[worker] = fifo->exp_start + fifo->exp_len + local_task_length[worker];
  328. if (exp_end[worker] < best_exp_end)
  329. {
  330. /* a better solution was found */
  331. best_exp_end = exp_end[worker];
  332. }
  333. local_power[worker] = starpu_task_expected_power(task, perf_arch);
  334. if (local_power[worker] == -1.0)
  335. local_power[worker] = 0.;
  336. }
  337. if (unknown)
  338. forced_best = ntasks_best;
  339. double best_fitness = -1;
  340. if (forced_best == -1)
  341. {
  342. for (worker = 0; worker < nworkers; worker++)
  343. {
  344. fifo = queue_array[worker];
  345. if (!starpu_worker_may_execute_task(worker, task))
  346. {
  347. /* no one on that queue may execute this task */
  348. continue;
  349. }
  350. fitness[worker] = alpha*(exp_end[worker] - best_exp_end)
  351. + beta*(local_data_penalty[worker])
  352. + _gamma*(local_power[worker]);
  353. if (best == -1 || fitness[worker] < best_fitness)
  354. {
  355. /* we found a better solution */
  356. best_fitness = fitness[worker];
  357. best = worker;
  358. // _STARPU_DEBUG("best fitness (worker %d) %le = alpha*(%le) + beta(%le) +gamma(%le)\n", worker, best_fitness, exp_end[worker] - best_exp_end, local_data_penalty[worker], local_power[worker]);
  359. }
  360. }
  361. }
  362. STARPU_ASSERT(forced_best != -1 || best != -1);
  363. if (forced_best != -1)
  364. {
  365. /* there is no prediction available for that task
  366. * with that arch we want to speed-up calibration time
  367. * so we force this measurement */
  368. best = forced_best;
  369. model_best = 0.0;
  370. penality_best = 0.0;
  371. }
  372. else
  373. {
  374. model_best = local_task_length[best];
  375. penality_best = local_data_penalty[best];
  376. }
  377. /* we should now have the best worker in variable "best" */
  378. return push_task_on_best_worker(task, best, model_best, prio);
  379. }
  380. static int dmda_push_sorted_task(struct starpu_task *task)
  381. {
  382. return _dmda_push_task(task, 2);
  383. }
  384. static int dm_push_prio_task(struct starpu_task *task)
  385. {
  386. return _dm_push_task(task, 1);
  387. }
  388. static int dm_push_task(struct starpu_task *task)
  389. {
  390. if (task->priority > 0)
  391. return _dm_push_task(task, 1);
  392. return _dm_push_task(task, 0);
  393. }
  394. static int dmda_push_prio_task(struct starpu_task *task)
  395. {
  396. return _dmda_push_task(task, 1);
  397. }
  398. static int dmda_push_task(struct starpu_task *task)
  399. {
  400. if (task->priority > 0)
  401. return _dmda_push_task(task, 1);
  402. return _dmda_push_task(task, 0);
  403. }
  404. static void initialize_dmda_policy(struct starpu_machine_topology_s *topology,
  405. __attribute__ ((unused)) struct starpu_sched_policy_s *_policy)
  406. {
  407. nworkers = topology->nworkers;
  408. const char *strval_alpha = getenv("STARPU_SCHED_ALPHA");
  409. if (strval_alpha)
  410. alpha = atof(strval_alpha);
  411. const char *strval_beta = getenv("STARPU_SCHED_BETA");
  412. if (strval_beta)
  413. beta = atof(strval_beta);
  414. const char *strval_gamma = getenv("STARPU_SCHED_GAMMA");
  415. if (strval_gamma)
  416. _gamma = atof(strval_gamma);
  417. unsigned workerid;
  418. for (workerid = 0; workerid < nworkers; workerid++)
  419. {
  420. queue_array[workerid] = _starpu_create_fifo();
  421. PTHREAD_MUTEX_INIT(&sched_mutex[workerid], NULL);
  422. PTHREAD_COND_INIT(&sched_cond[workerid], NULL);
  423. starpu_worker_set_sched_condition(workerid, &sched_cond[workerid], &sched_mutex[workerid]);
  424. }
  425. }
  426. static void initialize_dmda_sorted_policy(struct starpu_machine_topology_s *topology,
  427. struct starpu_sched_policy_s *_policy)
  428. {
  429. initialize_dmda_policy(topology, _policy);
  430. /* The application may use any integer */
  431. starpu_sched_set_min_priority(INT_MIN);
  432. starpu_sched_set_max_priority(INT_MAX);
  433. }
  434. static void deinitialize_dmda_policy(struct starpu_machine_topology_s *topology,
  435. __attribute__ ((unused)) struct starpu_sched_policy_s *_policy)
  436. {
  437. unsigned workerid;
  438. for (workerid = 0; workerid < topology->nworkers; workerid++)
  439. _starpu_destroy_fifo(queue_array[workerid]);
  440. _STARPU_DEBUG("total_task_cnt %ld ready_task_cnt %ld -> %f\n", total_task_cnt, ready_task_cnt, (100.0f*ready_task_cnt)/total_task_cnt);
  441. }
  442. struct starpu_sched_policy_s _starpu_sched_dm_policy = {
  443. .init_sched = initialize_dmda_policy,
  444. .deinit_sched = deinitialize_dmda_policy,
  445. .push_task = dm_push_task,
  446. .push_prio_task = dm_push_prio_task,
  447. .pop_task = dmda_pop_task,
  448. .post_exec_hook = NULL,
  449. .pop_every_task = dmda_pop_every_task,
  450. .policy_name = "dm",
  451. .policy_description = "performance model"
  452. };
  453. struct starpu_sched_policy_s _starpu_sched_dmda_policy = {
  454. .init_sched = initialize_dmda_policy,
  455. .deinit_sched = deinitialize_dmda_policy,
  456. .push_task = dmda_push_task,
  457. .push_prio_task = dmda_push_prio_task,
  458. .pop_task = dmda_pop_task,
  459. .post_exec_hook = NULL,
  460. .pop_every_task = dmda_pop_every_task,
  461. .policy_name = "dmda",
  462. .policy_description = "data-aware performance model"
  463. };
  464. struct starpu_sched_policy_s _starpu_sched_dmda_sorted_policy = {
  465. .init_sched = initialize_dmda_sorted_policy,
  466. .deinit_sched = deinitialize_dmda_policy,
  467. .push_task = dmda_push_sorted_task,
  468. .push_prio_task = dmda_push_sorted_task,
  469. .pop_task = dmda_pop_ready_task,
  470. .post_exec_hook = NULL,
  471. .pop_every_task = dmda_pop_every_task,
  472. .policy_name = "dmdas",
  473. .policy_description = "data-aware performance model (sorted)"
  474. };
  475. struct starpu_sched_policy_s _starpu_sched_dmda_ready_policy = {
  476. .init_sched = initialize_dmda_policy,
  477. .deinit_sched = deinitialize_dmda_policy,
  478. .push_task = dmda_push_task,
  479. .push_prio_task = dmda_push_prio_task,
  480. .pop_task = dmda_pop_ready_task,
  481. .post_exec_hook = NULL,
  482. .pop_every_task = dmda_pop_every_task,
  483. .policy_name = "dmdar",
  484. .policy_description = "data-aware performance model (ready)"
  485. };