parallel_heft.c 15 KB

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