deque_modeling_policy_data_aware.c 14 KB

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