parallel_heft.c 14 KB

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