parallel_heft.c 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456
  1. /* StarPU --- Runtime system for heterogeneous multicore architectures.
  2. *
  3. * Copyright (C) 2010-2011 Université de Bordeaux 1
  4. * Copyright (C) 2011 Télécom-SudParis
  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 <float.h>
  19. #include <limits.h>
  20. #include <core/workers.h>
  21. #include <core/perfmodel/perfmodel.h>
  22. #include <starpu_parameters.h>
  23. #include <common/barrier.h>
  24. #include <sched_policies/detect_combined_workers.h>
  25. static pthread_mutex_t big_lock;
  26. static unsigned nworkers, ncombinedworkers;
  27. //static enum starpu_perf_archtype applicable_perf_archtypes[STARPU_NARCH_VARIATIONS];
  28. //static unsigned napplicable_perf_archtypes = 0;
  29. static pthread_cond_t sched_cond[STARPU_NMAXWORKERS];
  30. static pthread_mutex_t sched_mutex[STARPU_NMAXWORKERS];
  31. static double alpha = _STARPU_DEFAULT_ALPHA;
  32. static double beta = _STARPU_DEFAULT_BETA;
  33. static double _gamma = _STARPU_DEFAULT_GAMMA;
  34. static double idle_power = 0.0;
  35. static double worker_exp_start[STARPU_NMAXWORKERS];
  36. static double worker_exp_end[STARPU_NMAXWORKERS];
  37. static double worker_exp_len[STARPU_NMAXWORKERS];
  38. static int ntasks[STARPU_NMAXWORKERS];
  39. static void parallel_heft_post_exec_hook(struct starpu_task *task)
  40. {
  41. if (!task->cl || task->execute_on_a_specific_worker)
  42. return;
  43. int workerid = starpu_worker_get_id();
  44. double model = task->predicted;
  45. double transfer_model = task->predicted_transfer;
  46. if (model < 0.0)
  47. model = 0.0;
  48. /* Once we have executed the task, we can update the predicted amount
  49. * of work. */
  50. _STARPU_PTHREAD_MUTEX_LOCK(&sched_mutex[workerid]);
  51. worker_exp_len[workerid] -= model + transfer_model;
  52. worker_exp_start[workerid] = starpu_timing_now();
  53. worker_exp_end[workerid] = worker_exp_start[workerid] + worker_exp_len[workerid];
  54. ntasks[workerid]--;
  55. _STARPU_PTHREAD_MUTEX_UNLOCK(&sched_mutex[workerid]);
  56. }
  57. static int push_task_on_best_worker(struct starpu_task *task, int best_workerid, double exp_end_predicted, int prio)
  58. {
  59. /* make sure someone coule execute that task ! */
  60. STARPU_ASSERT(best_workerid != -1);
  61. /* Is this a basic worker or a combined worker ? */
  62. int nbasic_workers = (int)starpu_worker_get_count();
  63. int is_basic_worker = (best_workerid < nbasic_workers);
  64. unsigned memory_node;
  65. memory_node = starpu_worker_get_memory_node(best_workerid);
  66. if (starpu_get_prefetch_flag())
  67. starpu_prefetch_task_input_on_node(task, memory_node);
  68. int ret = 0;
  69. _STARPU_PTHREAD_MUTEX_LOCK(&big_lock);
  70. if (is_basic_worker)
  71. {
  72. task->predicted = exp_end_predicted - worker_exp_end[best_workerid];
  73. /* TODO */
  74. task->predicted_transfer = 0;
  75. worker_exp_len[best_workerid] += exp_end_predicted - worker_exp_end[best_workerid];
  76. worker_exp_end[best_workerid] = exp_end_predicted;
  77. worker_exp_start[best_workerid] = exp_end_predicted - worker_exp_len[best_workerid];
  78. ntasks[best_workerid]++;
  79. ret = starpu_push_local_task(best_workerid, task, prio);
  80. }
  81. else
  82. {
  83. /* This is a combined worker so we create task aliases */
  84. struct _starpu_combined_worker *combined_worker;
  85. combined_worker = _starpu_get_combined_worker_struct(best_workerid);
  86. int worker_size = combined_worker->worker_size;
  87. int *combined_workerid = combined_worker->combined_workerid;
  88. struct _starpu_job *j = _starpu_get_job_associated_to_task(task);
  89. j->task_size = worker_size;
  90. j->combined_workerid = best_workerid;
  91. j->active_task_alias_count = 0;
  92. _STARPU_PTHREAD_BARRIER_INIT(&j->before_work_barrier, NULL, worker_size);
  93. _STARPU_PTHREAD_BARRIER_INIT(&j->after_work_barrier, NULL, worker_size);
  94. int i;
  95. for (i = 0; i < worker_size; i++)
  96. {
  97. struct starpu_task *alias = _starpu_create_task_alias(task);
  98. int local_worker = combined_workerid[i];
  99. alias->predicted = exp_end_predicted - worker_exp_end[local_worker];
  100. /* TODO */
  101. alias->predicted_transfer = 0;
  102. worker_exp_len[local_worker] += exp_end_predicted - worker_exp_end[local_worker];
  103. worker_exp_end[local_worker] = exp_end_predicted;
  104. worker_exp_start[local_worker] = exp_end_predicted - worker_exp_len[local_worker];
  105. ntasks[local_worker]++;
  106. ret |= starpu_push_local_task(local_worker, alias, prio);
  107. }
  108. }
  109. _STARPU_PTHREAD_MUTEX_UNLOCK(&big_lock);
  110. return ret;
  111. }
  112. static double compute_expected_end(int workerid, double length)
  113. {
  114. if (workerid < (int)nworkers)
  115. {
  116. /* This is a basic worker */
  117. return worker_exp_start[workerid] + worker_exp_len[workerid] + length;
  118. }
  119. else
  120. {
  121. /* This is a combined worker, the expected end is the end for the latest worker */
  122. int worker_size;
  123. int *combined_workerid;
  124. starpu_combined_worker_get_description(workerid, &worker_size, &combined_workerid);
  125. double exp_end = DBL_MIN;
  126. int i;
  127. for (i = 0; i < worker_size; i++)
  128. {
  129. double local_exp_start = worker_exp_start[combined_workerid[i]];
  130. double local_exp_len = worker_exp_len[combined_workerid[i]];
  131. double local_exp_end = local_exp_start + local_exp_len + length;
  132. exp_end = STARPU_MAX(exp_end, local_exp_end);
  133. }
  134. return exp_end;
  135. }
  136. }
  137. static double compute_ntasks_end(int workerid)
  138. {
  139. enum starpu_perf_archtype perf_arch = starpu_worker_get_perf_archtype(workerid);
  140. if (workerid < (int)nworkers)
  141. {
  142. /* This is a basic worker */
  143. return ntasks[workerid] / starpu_worker_get_relative_speedup(perf_arch);
  144. }
  145. else
  146. {
  147. /* This is a combined worker, the expected end is the end for the latest worker */
  148. int worker_size;
  149. int *combined_workerid;
  150. starpu_combined_worker_get_description(workerid, &worker_size, &combined_workerid);
  151. int ntasks_end=0;
  152. int i;
  153. for (i = 0; i < worker_size; i++)
  154. {
  155. /* XXX: this is actually bogus: not all pushed tasks are necessarily parallel... */
  156. ntasks_end = STARPU_MAX(ntasks_end, ntasks[combined_workerid[i]] / starpu_worker_get_relative_speedup(perf_arch));
  157. }
  158. return ntasks_end;
  159. }
  160. }
  161. static int _parallel_heft_push_task(struct starpu_task *task, unsigned prio)
  162. {
  163. unsigned worker;
  164. int best = -1;
  165. /* this flag is set if the corresponding worker is selected because
  166. there is no performance prediction available yet */
  167. int forced_best = -1;
  168. double local_task_length[nworkers+ncombinedworkers][STARPU_MAXIMPLEMENTATIONS];
  169. double local_data_penalty[nworkers+ncombinedworkers][STARPU_MAXIMPLEMENTATIONS];
  170. double local_power[nworkers+ncombinedworkers][STARPU_MAXIMPLEMENTATIONS];
  171. double local_exp_end[nworkers+ncombinedworkers][STARPU_MAXIMPLEMENTATIONS];
  172. double fitness[nworkers+ncombinedworkers][STARPU_MAXIMPLEMENTATIONS];
  173. double max_exp_end = 0.0;
  174. int skip_worker[nworkers+ncombinedworkers][STARPU_MAXIMPLEMENTATIONS];
  175. double best_exp_end = DBL_MAX;
  176. //double penality_best = 0.0;
  177. int ntasks_best = -1;
  178. double ntasks_best_end = 0.0;
  179. int calibrating = 0;
  180. /* A priori, we know all estimations */
  181. int unknown = 0;
  182. for (worker = 0; worker < nworkers; worker++)
  183. {
  184. /* Sometimes workers didn't take the tasks as early as we expected */
  185. worker_exp_start[worker] = STARPU_MAX(worker_exp_start[worker], starpu_timing_now());
  186. worker_exp_end[worker] = worker_exp_start[worker] + worker_exp_len[worker];
  187. if (worker_exp_end[worker] > max_exp_end)
  188. max_exp_end = worker_exp_end[worker];
  189. }
  190. unsigned nimpl;
  191. unsigned best_impl = 0;
  192. for (worker = 0; worker < (nworkers+ncombinedworkers); worker++)
  193. {
  194. for (nimpl = 0; nimpl < STARPU_MAXIMPLEMENTATIONS; nimpl++)
  195. {
  196. if (!starpu_combined_worker_can_execute_task(worker, task, nimpl))
  197. {
  198. /* no one on that queue may execute this task */
  199. skip_worker[worker][nimpl] = 1;
  200. continue;
  201. }
  202. else
  203. {
  204. skip_worker[worker][nimpl] = 0;
  205. }
  206. enum starpu_perf_archtype perf_arch = starpu_worker_get_perf_archtype(worker);
  207. local_task_length[worker][nimpl] = starpu_task_expected_length(task, perf_arch,nimpl);
  208. unsigned memory_node = starpu_worker_get_memory_node(worker);
  209. local_data_penalty[worker][nimpl] = starpu_task_expected_data_transfer_time(memory_node, task);
  210. double ntasks_end = compute_ntasks_end(worker);
  211. if (ntasks_best == -1
  212. || (!calibrating && ntasks_end < ntasks_best_end) /* Not calibrating, take better task */
  213. || (!calibrating && local_task_length[worker][nimpl] == -1.0) /* Not calibrating but this worker is being calibrated */
  214. || (calibrating && local_task_length[worker][nimpl] == -1.0 && ntasks_end < ntasks_best_end) /* Calibrating, compete this worker with other non-calibrated */
  215. )
  216. {
  217. ntasks_best_end = ntasks_end;
  218. ntasks_best = worker;
  219. }
  220. if (local_task_length[worker][nimpl] == -1.0)
  221. /* we are calibrating, we want to speed-up calibration time
  222. * so we privilege non-calibrated tasks (but still
  223. * greedily distribute them to avoid dumb schedules) */
  224. calibrating = 1;
  225. if (local_task_length[worker][nimpl] <= 0.0)
  226. /* there is no prediction available for that task
  227. * with that arch yet, so switch to a greedy strategy */
  228. unknown = 1;
  229. if (unknown)
  230. continue;
  231. local_exp_end[worker][nimpl] = compute_expected_end(worker, local_task_length[worker][nimpl]);
  232. //fprintf(stderr, "WORKER %d -> length %e end %e\n", worker, local_task_length[worker][nimpl], local_exp_end[worker][nimpl]);
  233. if (local_exp_end[worker][nimpl] < best_exp_end)
  234. {
  235. /* a better solution was found */
  236. best_exp_end = local_exp_end[worker][nimpl];
  237. best_impl = nimpl;
  238. }
  239. local_power[worker][nimpl] = starpu_task_expected_power(task, perf_arch,nimpl);
  240. //_STARPU_DEBUG("Scheduler parallel heft: task length (%lf) local power (%lf) worker (%u) kernel (%u) \n", local_task_length[worker][nimpl],local_power[worker][nimpl],worker,nimpl);
  241. if (local_power[worker][nimpl] == -1.0)
  242. local_power[worker][nimpl] = 0.;
  243. } //end for
  244. }
  245. if (unknown)
  246. forced_best = ntasks_best;
  247. double best_fitness = -1;
  248. if (forced_best == -1)
  249. {
  250. for (worker = 0; worker < nworkers+ncombinedworkers; worker++)
  251. {
  252. for (nimpl = 0; nimpl < STARPU_MAXIMPLEMENTATIONS; nimpl++)
  253. {
  254. if (skip_worker[worker][nimpl])
  255. {
  256. /* no one on that queue may execute this task */
  257. continue;
  258. }
  259. fitness[worker][nimpl] = alpha*(local_exp_end[worker][nimpl] - best_exp_end)
  260. + beta*(local_data_penalty[worker][nimpl])
  261. + _gamma*(local_power[worker][nimpl]);
  262. if (local_exp_end[worker][nimpl] > max_exp_end)
  263. /* This placement will make the computation
  264. * longer, take into account the idle
  265. * consumption of other cpus */
  266. fitness[worker][nimpl] += _gamma * idle_power * (local_exp_end[worker][nimpl] - max_exp_end) / 1000000.0;
  267. if (best == -1 || fitness[worker][nimpl] < best_fitness)
  268. {
  269. /* we found a better solution */
  270. best_fitness = fitness[worker][nimpl];
  271. best = worker;
  272. }
  273. // fprintf(stderr, "FITNESS worker %d -> %e local_exp_end %e - local_data_penalty %e\n", worker, fitness[worker][nimpl], local_exp_end[worker][nimpl] - best_exp_end, local_data_penalty[worker][nimpl]);
  274. }
  275. }
  276. }
  277. STARPU_ASSERT(forced_best != -1 || best != -1);
  278. if (forced_best != -1)
  279. {
  280. /* there is no prediction available for that task
  281. * with that arch we want to speed-up calibration time
  282. * so we force this measurement */
  283. best = forced_best;
  284. //penality_best = 0.0;
  285. best_exp_end = local_exp_end[best][nimpl];
  286. }
  287. else
  288. {
  289. //penality_best = local_data_penalty[best][nimpl];
  290. best_exp_end = local_exp_end[best][nimpl];
  291. }
  292. //_STARPU_DEBUG("Scheduler parallel heft: kernel (%u)\n", best_impl);
  293. _starpu_get_job_associated_to_task(task)->nimpl = best_impl;
  294. /* we should now have the best worker in variable "best" */
  295. return push_task_on_best_worker(task, best, best_exp_end, prio);
  296. }
  297. static int parallel_heft_push_task(struct starpu_task *task)
  298. {
  299. if (task->priority == STARPU_MAX_PRIO)
  300. return _parallel_heft_push_task(task, 1);
  301. return _parallel_heft_push_task(task, 0);
  302. }
  303. static void initialize_parallel_heft_policy(struct starpu_machine_topology *topology,
  304. __attribute__ ((unused)) struct starpu_sched_policy *_policy)
  305. {
  306. nworkers = topology->nworkers;
  307. const char *strval_alpha = getenv("STARPU_SCHED_ALPHA");
  308. if (strval_alpha)
  309. alpha = atof(strval_alpha);
  310. const char *strval_beta = getenv("STARPU_SCHED_BETA");
  311. if (strval_beta)
  312. beta = atof(strval_beta);
  313. const char *strval_gamma = getenv("STARPU_SCHED_GAMMA");
  314. if (strval_gamma)
  315. _gamma = atof(strval_gamma);
  316. const char *strval_idle_power = getenv("STARPU_IDLE_POWER");
  317. if (strval_idle_power)
  318. idle_power = atof(strval_idle_power);
  319. _starpu_sched_find_worker_combinations(topology);
  320. ncombinedworkers = topology->ncombinedworkers;
  321. unsigned workerid;
  322. for (workerid = 0; workerid < nworkers; workerid++)
  323. {
  324. worker_exp_start[workerid] = starpu_timing_now();
  325. worker_exp_len[workerid] = 0.0;
  326. worker_exp_end[workerid] = worker_exp_start[workerid];
  327. ntasks[workerid] = 0;
  328. _STARPU_PTHREAD_MUTEX_INIT(&sched_mutex[workerid], NULL);
  329. _STARPU_PTHREAD_COND_INIT(&sched_cond[workerid], NULL);
  330. starpu_worker_set_sched_condition(workerid, &sched_cond[workerid], &sched_mutex[workerid]);
  331. }
  332. _STARPU_PTHREAD_MUTEX_INIT(&big_lock, NULL);
  333. /* We pre-compute an array of all the perfmodel archs that are applicable */
  334. unsigned total_worker_count = nworkers + ncombinedworkers;
  335. unsigned used_perf_archtypes[STARPU_NARCH_VARIATIONS];
  336. memset(used_perf_archtypes, 0, sizeof(used_perf_archtypes));
  337. for (workerid = 0; workerid < total_worker_count; workerid++)
  338. {
  339. enum starpu_perf_archtype perf_archtype = starpu_worker_get_perf_archtype(workerid);
  340. used_perf_archtypes[perf_archtype] = 1;
  341. }
  342. // napplicable_perf_archtypes = 0;
  343. // int arch;
  344. // for (arch = 0; arch < STARPU_NARCH_VARIATIONS; arch++)
  345. // {
  346. // if (used_perf_archtypes[arch])
  347. // applicable_perf_archtypes[napplicable_perf_archtypes++] = arch;
  348. // }
  349. }
  350. /* TODO: use post_exec_hook to fix the expected start */
  351. struct starpu_sched_policy _starpu_sched_parallel_heft_policy =
  352. {
  353. .init_sched = initialize_parallel_heft_policy,
  354. .deinit_sched = NULL,
  355. .push_task = parallel_heft_push_task,
  356. .pop_task = NULL,
  357. .post_exec_hook = parallel_heft_post_exec_hook,
  358. .pop_every_task = NULL,
  359. .policy_name = "pheft",
  360. .policy_description = "parallel HEFT"
  361. };