parallel_heft.c 13 KB

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