deque_modeling_policy_data_aware.c 14 KB

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