component_heteroprio.c 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567
  1. /* StarPU --- Runtime system for heterogeneous multicore architectures.
  2. *
  3. * Copyright (C) 2013-2020 Université de Bordeaux, CNRS (LaBRI UMR 5800), Inria
  4. * Copyright (C) 2013 Simon Archipoff
  5. * Copyright (C) 2020 Télécom-Sud Paris
  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. /* Heteroprio, which sorts tasks by acceleration factor into buckets, and makes
  19. * GPUs take accelerated tasks first and CPUs take non-accelerated tasks first */
  20. #include <starpu_sched_component.h>
  21. #include "prio_deque.h"
  22. #include <starpu_perfmodel.h>
  23. #include "helper_mct.h"
  24. #include <float.h>
  25. #include <core/sched_policy.h>
  26. #include <core/task.h>
  27. /* Approximation ratio for acceleration factor bucketing
  28. * We will put tasks with +-10% similar acceleration into the same bucket. */
  29. #define APPROX 0.10
  30. struct _starpu_heteroprio_data
  31. {
  32. /* This is an array of priority queues.
  33. * The array is sorted by acceleration factor, most accelerated first */
  34. struct _starpu_prio_deque **bucket;
  35. float *accel;
  36. unsigned naccel;
  37. /* This contains tasks which are not supported on all archs. */
  38. struct _starpu_prio_deque no_accel;
  39. /* This protects all queues */
  40. starpu_pthread_mutex_t mutex;
  41. struct _starpu_mct_data *mct_data;
  42. unsigned batch;
  43. };
  44. static int heteroprio_progress_accel(struct starpu_sched_component *component, struct _starpu_heteroprio_data *data, enum starpu_worker_archtype archtype, int front)
  45. {
  46. struct starpu_task *task = NULL;
  47. starpu_pthread_mutex_t * mutex = &data->mutex;
  48. int j, ret = 1;
  49. double acceleration = INFINITY;
  50. struct _starpu_mct_data * d = data->mct_data;
  51. STARPU_COMPONENT_MUTEX_LOCK(mutex);
  52. if (front)
  53. /* Pick up accelerated tasks first */
  54. for (j = 0; j < (int) data->naccel; j++)
  55. {
  56. task = _starpu_prio_deque_pop_task(data->bucket[j]);
  57. if (task)
  58. break;
  59. }
  60. else
  61. /* Pick up accelerated tasks last */
  62. for (j = (int) data->naccel-1; j >= 0; j--)
  63. {
  64. if (data->batch && 0)
  65. task = _starpu_prio_deque_pop_back_task(data->bucket[j]);
  66. else
  67. task = _starpu_prio_deque_pop_task(data->bucket[j]);
  68. if (task)
  69. break;
  70. }
  71. if (task)
  72. {
  73. acceleration = data->accel[j];
  74. //fprintf(stderr, "for %s thus %s, found task %p in bucket %d: %f\n", starpu_worker_get_type_as_string(archtype), front?"front":"back", task, j, acceleration);
  75. }
  76. STARPU_COMPONENT_MUTEX_UNLOCK(mutex);
  77. if (!task)
  78. return 1;
  79. if (data->batch)
  80. /* In batch mode the fifos below do not use priorities. Do not
  81. * leak a priority for the data prefetches either */
  82. task->priority = INT_MAX;
  83. /* TODO: we might want to prefer to pick up a task whose data is already on some GPU */
  84. struct starpu_sched_component * best_component;
  85. /* Estimated task duration for each child */
  86. double estimated_lengths[component->nchildren];
  87. /* Estimated transfer duration for each child */
  88. double estimated_transfer_length[component->nchildren];
  89. /* Estimated transfer+task termination for each child */
  90. double estimated_ends_with_task[component->nchildren];
  91. /* provided local energy */
  92. double local_energy[component->nchildren];
  93. /* Minimum transfer+task termination of the task over all workers */
  94. double min_exp_end_of_task;
  95. /* Maximum termination of the already-scheduled tasks over all workers */
  96. double max_exp_end_of_workers;
  97. unsigned suitable_components[component->nchildren];
  98. unsigned nsuitable_components;
  99. nsuitable_components = starpu_mct_compute_execution_times(component, task,
  100. estimated_lengths,
  101. estimated_transfer_length,
  102. suitable_components);
  103. if (data->batch && 0)
  104. {
  105. /* In batch mode, we may want to insist on filling workers with tasks
  106. * by ignoring when other workers would finish this. */
  107. unsigned i;
  108. for (i = 0; i < component->nchildren; i++)
  109. {
  110. int idworker;
  111. for(idworker = starpu_bitmap_first(&component->children[i]->workers);
  112. idworker != -1;
  113. idworker = starpu_bitmap_next(&component->children[i]->workers, idworker))
  114. {
  115. if (starpu_worker_get_type(idworker) == archtype)
  116. break;
  117. }
  118. if (idworker == -1)
  119. {
  120. /* Not the targetted arch, avoid it */
  121. /* XXX: INFINITY doesn't seem to be working properly */
  122. estimated_lengths[i] = 1000000000;
  123. estimated_transfer_length[i] = 1000000000;
  124. }
  125. }
  126. }
  127. /* Entering critical section to make sure no two workers
  128. make scheduling decisions at the same time */
  129. STARPU_COMPONENT_MUTEX_LOCK(&d->scheduling_mutex);
  130. starpu_mct_compute_expected_times(component, task,
  131. estimated_lengths,
  132. estimated_transfer_length,
  133. estimated_ends_with_task,
  134. &min_exp_end_of_task, &max_exp_end_of_workers,
  135. suitable_components, nsuitable_components);
  136. /* Compute the energy, if provided*/
  137. starpu_mct_compute_energy(component, task, local_energy, suitable_components, nsuitable_components);
  138. /* And now find out which worker suits best for this task,
  139. * including data transfer */
  140. int best_icomponent = starpu_mct_get_best_component(d, task,
  141. estimated_lengths,
  142. estimated_transfer_length,
  143. estimated_ends_with_task,
  144. local_energy,
  145. min_exp_end_of_task, max_exp_end_of_workers,
  146. suitable_components, nsuitable_components);
  147. if (best_icomponent == -1)
  148. goto out;
  149. best_component = component->children[best_icomponent];
  150. int idworker;
  151. for(idworker = starpu_bitmap_first(&best_component->workers);
  152. idworker != -1;
  153. idworker = starpu_bitmap_next(&best_component->workers, idworker))
  154. {
  155. if (starpu_worker_get_type(idworker) == archtype)
  156. break;
  157. }
  158. if (idworker == -1)
  159. goto out;
  160. /* Ok, we do have a worker there of that type, try to push it there. */
  161. STARPU_ASSERT(!starpu_sched_component_is_worker(best_component));
  162. starpu_sched_task_break(task);
  163. ret = starpu_sched_component_push_task(component,best_component,task);
  164. /* I can now exit the critical section: Pushing the task above ensures that its execution
  165. time will be taken into account for subsequent scheduling decisions */
  166. if (!ret)
  167. {
  168. STARPU_COMPONENT_MUTEX_UNLOCK(&d->scheduling_mutex);
  169. //fprintf(stderr, "pushed %p to %d\n", task, best_icomponent);
  170. /* Great! */
  171. return 0;
  172. }
  173. out:
  174. STARPU_COMPONENT_MUTEX_UNLOCK(&d->scheduling_mutex);
  175. /* No such kind of worker there, or it refused our task, abort */
  176. //fprintf(stderr, "could not push %p to %d actually\n", task, best_icomponent);
  177. /* Could not push to child actually, push that one back */
  178. STARPU_COMPONENT_MUTEX_LOCK(mutex);
  179. for (j = 0; j < (int) data->naccel; j++)
  180. {
  181. if (acceleration == data->accel[j])
  182. {
  183. _starpu_prio_deque_push_front_task(data->bucket[j], task);
  184. break;
  185. }
  186. }
  187. STARPU_ASSERT(j != (int) data->naccel);
  188. STARPU_COMPONENT_MUTEX_UNLOCK(mutex);
  189. //fprintf(stderr, "finished pushing to %d\n", archtype);
  190. return 1;
  191. }
  192. static int heteroprio_progress_noaccel(struct starpu_sched_component *component, struct _starpu_heteroprio_data *data, struct starpu_task *task)
  193. {
  194. struct _starpu_mct_data * d = data->mct_data;
  195. int ret;
  196. struct starpu_sched_component * best_component;
  197. /* Estimated task duration for each child */
  198. double estimated_lengths[component->nchildren];
  199. /* Estimated transfer duration for each child */
  200. double estimated_transfer_length[component->nchildren];
  201. /* Estimated transfer+task termination for each child */
  202. double estimated_ends_with_task[component->nchildren];
  203. /* estimated energy */
  204. double local_energy[component->nchildren];
  205. /* Minimum transfer+task termination of the task over all workers */
  206. double min_exp_end_of_task;
  207. /* Maximum termination of the already-scheduled tasks over all workers */
  208. double max_exp_end_of_workers;
  209. unsigned suitable_components[component->nchildren];
  210. unsigned nsuitable_components;
  211. nsuitable_components = starpu_mct_compute_execution_times(component, task,
  212. estimated_lengths,
  213. estimated_transfer_length,
  214. suitable_components);
  215. /* If no suitable components were found, it means that the perfmodel of
  216. * the task had been purged since it has been pushed on the mct component.
  217. * We should send a push_fail message to its parent so that it will
  218. * be able to reschedule the task properly. */
  219. if(nsuitable_components == 0)
  220. return 1;
  221. /* Entering critical section to make sure no two workers
  222. make scheduling decisions at the same time */
  223. STARPU_COMPONENT_MUTEX_LOCK(&d->scheduling_mutex);
  224. starpu_mct_compute_expected_times(component, task,
  225. estimated_lengths,
  226. estimated_transfer_length,
  227. estimated_ends_with_task,
  228. &min_exp_end_of_task, &max_exp_end_of_workers,
  229. suitable_components, nsuitable_components);
  230. /* Compute the energy, if provided*/
  231. starpu_mct_compute_energy(component, task, local_energy, suitable_components, nsuitable_components);
  232. /* And now find out which worker suits best for this task,
  233. * including data transfer */
  234. int best_icomponent = starpu_mct_get_best_component(d, task,
  235. estimated_lengths,
  236. estimated_transfer_length,
  237. estimated_ends_with_task,
  238. local_energy,
  239. min_exp_end_of_task, max_exp_end_of_workers,
  240. suitable_components, nsuitable_components);
  241. /* If no best component is found, it means that the perfmodel of
  242. * the task had been purged since it has been pushed on the mct component.
  243. * We should send a push_fail message to its parent so that it will
  244. * be able to reschedule the task properly. */
  245. if(best_icomponent == -1)
  246. {
  247. STARPU_COMPONENT_MUTEX_UNLOCK(&d->scheduling_mutex);
  248. return 1;
  249. }
  250. best_component = component->children[best_icomponent];
  251. STARPU_ASSERT(!starpu_sched_component_is_worker(best_component));
  252. ret = starpu_sched_component_push_task(component,best_component,task);
  253. STARPU_COMPONENT_MUTEX_UNLOCK(&d->scheduling_mutex);
  254. return ret;
  255. }
  256. static int heteroprio_progress_one(struct starpu_sched_component *component)
  257. {
  258. struct _starpu_heteroprio_data * data = component->data;
  259. starpu_pthread_mutex_t * mutex = &data->mutex;
  260. struct starpu_task *task;
  261. struct _starpu_prio_deque * no_accel = &data->no_accel;
  262. STARPU_COMPONENT_MUTEX_LOCK(mutex);
  263. task = _starpu_prio_deque_pop_task(no_accel);
  264. STARPU_COMPONENT_MUTEX_UNLOCK(mutex);
  265. if (task)
  266. {
  267. if (heteroprio_progress_noaccel(component, data, task))
  268. {
  269. /* Could not push to child actually, push that one back */
  270. STARPU_COMPONENT_MUTEX_LOCK(mutex);
  271. _starpu_prio_deque_push_front_task(no_accel, task);
  272. STARPU_COMPONENT_MUTEX_UNLOCK(mutex);
  273. }
  274. }
  275. /* Note: this hardcodes acceleration order */
  276. if (!heteroprio_progress_accel(component, data, STARPU_CUDA_WORKER, 1))
  277. return 0;
  278. if (!heteroprio_progress_accel(component, data, STARPU_OPENCL_WORKER, 1))
  279. return 0;
  280. if (!heteroprio_progress_accel(component, data, STARPU_MIC_WORKER, 1))
  281. return 0;
  282. if (!heteroprio_progress_accel(component, data, STARPU_MPI_MS_WORKER, 0))
  283. return 0;
  284. if (!heteroprio_progress_accel(component, data, STARPU_CPU_WORKER, 0))
  285. return 0;
  286. return 1;
  287. }
  288. /* Try to push some tasks below */
  289. static void heteroprio_progress(struct starpu_sched_component *component)
  290. {
  291. STARPU_ASSERT(component && starpu_sched_component_is_heteroprio(component));
  292. while (!heteroprio_progress_one(component))
  293. ;
  294. }
  295. static int heteroprio_push_task(struct starpu_sched_component * component, struct starpu_task * task)
  296. {
  297. STARPU_ASSERT(component && task && starpu_sched_component_is_heteroprio(component));
  298. struct _starpu_heteroprio_data * data = component->data;
  299. starpu_pthread_mutex_t * mutex = &data->mutex;
  300. unsigned nimpl;
  301. double min_expected = INFINITY, max_expected = -INFINITY;
  302. double acceleration;
  303. if (data->batch && 0)
  304. /* Batch mode, we may want to ignore priorities completely */
  305. task->priority = INT_MAX;
  306. /* Compute acceleration between best-performing arch and least-performing arch */
  307. int workerid;
  308. for(workerid = starpu_bitmap_first(&component->workers_in_ctx);
  309. workerid != -1;
  310. workerid = starpu_bitmap_next(&component->workers_in_ctx, workerid))
  311. {
  312. unsigned impl_mask;
  313. if (!starpu_worker_can_execute_task_impl(workerid, task, &impl_mask))
  314. break;
  315. struct starpu_perfmodel_arch* perf_arch = starpu_worker_get_perf_archtype(workerid, task->sched_ctx);
  316. double min_arch = INFINITY;
  317. for (nimpl = 0; nimpl < STARPU_MAXIMPLEMENTATIONS; nimpl++)
  318. {
  319. if (!(impl_mask & (1U << nimpl)))
  320. continue;
  321. double expected = starpu_task_expected_length(task, perf_arch, nimpl);
  322. if (isnan(expected) || expected == 0.)
  323. {
  324. min_arch = expected;
  325. break;
  326. }
  327. if (expected < min_arch)
  328. min_arch = expected;
  329. }
  330. if (isnan(min_arch) || min_arch == 0.)
  331. /* No known execution time, can't do anything here */
  332. break;
  333. STARPU_ASSERT(min_arch != INFINITY);
  334. if (min_arch < min_expected)
  335. min_expected = min_arch;
  336. if (min_arch > max_expected)
  337. max_expected = min_arch;
  338. }
  339. if (workerid == -1)
  340. {
  341. /* All archs can run it */
  342. STARPU_ASSERT(!isnan(min_expected));
  343. STARPU_ASSERT(!isnan(max_expected));
  344. STARPU_ASSERT(min_expected != INFINITY);
  345. STARPU_ASSERT(max_expected != -INFINITY);
  346. acceleration = max_expected / min_expected;
  347. STARPU_ASSERT(!isnan(acceleration));
  348. //fprintf(stderr,"%s: acceleration %f\n", starpu_task_get_name(task), acceleration);
  349. STARPU_COMPONENT_MUTEX_LOCK(mutex);
  350. unsigned i, j;
  351. /* Try to find a bucket with similar acceleration */
  352. for (i = 0; i < data->naccel; i++)
  353. {
  354. if (acceleration >= data->accel[i] * (1 - APPROX) &&
  355. acceleration <= data->accel[i] * (1 + APPROX))
  356. break;
  357. }
  358. if (i == data->naccel)
  359. {
  360. /* Didn't find it, add one */
  361. data->naccel++;
  362. float *newaccel;
  363. _STARPU_MALLOC(newaccel, data->naccel * sizeof(*newaccel));
  364. struct _starpu_prio_deque **newbuckets;
  365. _STARPU_MALLOC(newbuckets, data->naccel * sizeof(*newbuckets));
  366. struct _starpu_prio_deque *newbucket;
  367. _STARPU_MALLOC(newbucket, sizeof(*newbucket));
  368. _starpu_prio_deque_init(newbucket);
  369. int inserted = 0;
  370. for (j = 0; j < data->naccel-1; j++)
  371. {
  372. if (!inserted && acceleration > data->accel[j])
  373. {
  374. /* Insert the new bucket here */
  375. i = j;
  376. newbuckets[j] = newbucket;
  377. newaccel[j] = acceleration;
  378. inserted = 1;
  379. }
  380. newbuckets[j+inserted] = data->bucket[j];
  381. newaccel[j+inserted] = data->accel[j];
  382. }
  383. if (!inserted)
  384. {
  385. /* Insert it last */
  386. newbuckets[data->naccel-1] = newbucket;
  387. newaccel[data->naccel-1] = acceleration;
  388. }
  389. free(data->bucket);
  390. free(data->accel);
  391. data->bucket = newbuckets;
  392. data->accel = newaccel;
  393. }
  394. #if 0
  395. fprintf(stderr,"buckets:");
  396. for (j = 0; j < data->naccel; j++)
  397. {
  398. fprintf(stderr, " %f", data->accel[j]);
  399. }
  400. fprintf(stderr,"\ninserting %p %f to %d\n", task, acceleration, i);
  401. #endif
  402. _starpu_prio_deque_push_back_task(data->bucket[i],task);
  403. STARPU_COMPONENT_MUTEX_UNLOCK(mutex);
  404. }
  405. else
  406. {
  407. /* Not all archs can run it, will resort to HEFT strategy */
  408. acceleration = INFINITY;
  409. //fprintf(stderr,"%s: some archs can't do it\n", starpu_task_get_name(task));
  410. struct _starpu_prio_deque * no_accel = &data->no_accel;
  411. STARPU_COMPONENT_MUTEX_LOCK(mutex);
  412. _starpu_prio_deque_push_back_task(no_accel,task);
  413. STARPU_COMPONENT_MUTEX_UNLOCK(mutex);
  414. }
  415. heteroprio_progress(component);
  416. return 0;
  417. }
  418. static int heteroprio_can_push(struct starpu_sched_component *component, struct starpu_sched_component * to STARPU_ATTRIBUTE_UNUSED)
  419. {
  420. heteroprio_progress(component);
  421. int ret = 0;
  422. unsigned j;
  423. for(j=0; j < component->nparents; j++)
  424. {
  425. if(component->parents[j] == NULL)
  426. continue;
  427. else
  428. {
  429. ret = component->parents[j]->can_push(component->parents[j], component);
  430. if(ret)
  431. break;
  432. }
  433. }
  434. return ret;
  435. }
  436. static void heteroprio_component_deinit_data(struct starpu_sched_component * component)
  437. {
  438. STARPU_ASSERT(starpu_sched_component_is_heteroprio(component));
  439. struct _starpu_heteroprio_data * d = component->data;
  440. struct _starpu_mct_data * mct_d = d->mct_data;
  441. unsigned i;
  442. for (i = 0; i < d->naccel; i++)
  443. {
  444. _starpu_prio_deque_destroy(d->bucket[i]);
  445. free(d->bucket[i]);
  446. }
  447. free(d->bucket);
  448. free(d->accel);
  449. _starpu_prio_deque_destroy(&d->no_accel);
  450. STARPU_PTHREAD_MUTEX_DESTROY(&d->mutex);
  451. STARPU_PTHREAD_MUTEX_DESTROY(&mct_d->scheduling_mutex);
  452. free(mct_d);
  453. free(d);
  454. }
  455. int starpu_sched_component_is_heteroprio(struct starpu_sched_component * component)
  456. {
  457. return component->push_task == heteroprio_push_task;
  458. }
  459. struct starpu_sched_component * starpu_sched_component_heteroprio_create(struct starpu_sched_tree *tree, struct starpu_sched_component_heteroprio_data * params)
  460. {
  461. struct starpu_sched_component * component = starpu_sched_component_create(tree, "heteroprio");
  462. struct _starpu_mct_data *mct_data = starpu_mct_init_parameters(params ? params->mct : NULL);
  463. struct _starpu_heteroprio_data *data;
  464. _STARPU_MALLOC(data, sizeof(*data));
  465. data->bucket = NULL;
  466. data->accel = NULL;
  467. data->naccel = 0;
  468. _starpu_prio_deque_init(&data->no_accel);
  469. STARPU_PTHREAD_MUTEX_INIT(&data->mutex,NULL);
  470. data->mct_data = mct_data;
  471. STARPU_PTHREAD_MUTEX_INIT(&mct_data->scheduling_mutex,NULL);
  472. if (params)
  473. data->batch = params->batch;
  474. else
  475. data->batch = 1;
  476. component->data = data;
  477. component->push_task = heteroprio_push_task;
  478. component->can_push = heteroprio_can_push;
  479. component->deinit_data = heteroprio_component_deinit_data;
  480. return component;
  481. }