parallel_heft.c 16 KB

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