sched_ctx_hypervisor.c 27 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830
  1. /* StarPU --- Runtime system for heterogeneous multicore architectures.
  2. *
  3. * Copyright (C) 2011, 2012 INRIA
  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. #include <sched_ctx_hypervisor_intern.h>
  17. #include <common/uthash.h>
  18. #include <starpu_config.h>
  19. unsigned imposed_resize = 0;
  20. struct starpu_performance_counters* perf_counters = NULL;
  21. static void notify_idle_cycle(unsigned sched_ctx, int worker, double idle_time);
  22. static void notify_pushed_task(unsigned sched_ctx, int worker);
  23. static void notify_poped_task(unsigned sched_ctx, int worker, double flops);
  24. static void notify_post_exec_hook(unsigned sched_ctx, int taskid);
  25. static void notify_idle_end(unsigned sched_ctx, int worker);
  26. static void notify_submitted_job(struct starpu_task *task, unsigned footprint);
  27. extern struct starpu_sched_ctx_hypervisor_policy idle_policy;
  28. extern struct starpu_sched_ctx_hypervisor_policy app_driven_policy;
  29. extern struct starpu_sched_ctx_hypervisor_policy gflops_rate_policy;
  30. #ifdef STARPU_HAVE_GLPK_H
  31. extern struct starpu_sched_ctx_hypervisor_policy lp_policy;
  32. extern struct starpu_sched_ctx_hypervisor_policy lp2_policy;
  33. #endif // STARPU_HAVE_GLPK_H
  34. static struct starpu_sched_ctx_hypervisor_policy *predefined_policies[] =
  35. {
  36. &idle_policy,
  37. &app_driven_policy,
  38. #ifdef STARPU_HAVE_GLPK_H
  39. &lp_policy,
  40. &lp2_policy,
  41. #endif // STARPU_HAVE_GLPK_H
  42. &gflops_rate_policy
  43. };
  44. static void _load_hypervisor_policy(struct starpu_sched_ctx_hypervisor_policy *policy)
  45. {
  46. STARPU_ASSERT(policy);
  47. hypervisor.policy.name = policy->name;
  48. hypervisor.policy.size_ctxs = policy->size_ctxs;
  49. hypervisor.policy.handle_poped_task = policy->handle_poped_task;
  50. hypervisor.policy.handle_pushed_task = policy->handle_pushed_task;
  51. hypervisor.policy.handle_idle_cycle = policy->handle_idle_cycle;
  52. hypervisor.policy.handle_idle_end = policy->handle_idle_end;
  53. hypervisor.policy.handle_post_exec_hook = policy->handle_post_exec_hook;
  54. hypervisor.policy.handle_submitted_job = policy->handle_submitted_job;
  55. }
  56. static struct starpu_sched_ctx_hypervisor_policy *_find_hypervisor_policy_from_name(const char *policy_name)
  57. {
  58. if (!policy_name)
  59. return NULL;
  60. unsigned i;
  61. for (i = 0; i < sizeof(predefined_policies)/sizeof(predefined_policies[0]); i++)
  62. {
  63. struct starpu_sched_ctx_hypervisor_policy *p;
  64. p = predefined_policies[i];
  65. if (p->name)
  66. {
  67. if (strcmp(policy_name, p->name) == 0) {
  68. /* we found a policy with the requested name */
  69. return p;
  70. }
  71. }
  72. }
  73. fprintf(stderr, "Warning: hypervisor policy \"%s\" was not found, try \"help\" to get a list\n", policy_name);
  74. /* nothing was found */
  75. return NULL;
  76. }
  77. static struct starpu_sched_ctx_hypervisor_policy *_select_hypervisor_policy(struct starpu_sched_ctx_hypervisor_policy* hypervisor_policy)
  78. {
  79. struct starpu_sched_ctx_hypervisor_policy *selected_policy = NULL;
  80. if(hypervisor_policy && hypervisor_policy->custom)
  81. return hypervisor_policy;
  82. /* we look if the application specified the name of a policy to load */
  83. const char *policy_name;
  84. if (hypervisor_policy && hypervisor_policy->name)
  85. {
  86. policy_name = hypervisor_policy->name;
  87. }
  88. else
  89. {
  90. policy_name = getenv("HYPERVISOR_POLICY");
  91. }
  92. if (policy_name)
  93. selected_policy = _find_hypervisor_policy_from_name(policy_name);
  94. /* Perhaps there was no policy that matched the name */
  95. if (selected_policy)
  96. return selected_policy;
  97. /* If no policy was specified, we use the idle policy as a default */
  98. return &idle_policy;
  99. }
  100. /* initializez the performance counters that starpu will use to retrive hints for resizing */
  101. struct starpu_performance_counters* sched_ctx_hypervisor_init(struct starpu_sched_ctx_hypervisor_policy *hypervisor_policy)
  102. {
  103. hypervisor.min_tasks = 0;
  104. hypervisor.nsched_ctxs = 0;
  105. pthread_mutex_init(&act_hypervisor_mutex, NULL);
  106. int i;
  107. for(i = 0; i < STARPU_NMAX_SCHED_CTXS; i++)
  108. {
  109. hypervisor.resize[i] = 0;
  110. hypervisor.configurations[i] = NULL;
  111. hypervisor.sr = NULL;
  112. hypervisor.check_min_tasks[i] = 1;
  113. hypervisor.sched_ctxs[i] = STARPU_NMAX_SCHED_CTXS;
  114. hypervisor.sched_ctx_w[i].sched_ctx = STARPU_NMAX_SCHED_CTXS;
  115. hypervisor.sched_ctx_w[i].config = NULL;
  116. hypervisor.sched_ctx_w[i].total_flops = 0.0;
  117. hypervisor.sched_ctx_w[i].submitted_flops = 0.0;
  118. hypervisor.sched_ctx_w[i].remaining_flops = 0.0;
  119. hypervisor.sched_ctx_w[i].start_time = 0.0;
  120. hypervisor.sched_ctx_w[i].resize_ack.receiver_sched_ctx = -1;
  121. hypervisor.sched_ctx_w[i].resize_ack.moved_workers = NULL;
  122. hypervisor.sched_ctx_w[i].resize_ack.nmoved_workers = 0;
  123. hypervisor.sched_ctx_w[i].resize_ack.acked_workers = NULL;
  124. pthread_mutex_init(&hypervisor.sched_ctx_w[i].mutex, NULL);
  125. int j;
  126. for(j = 0; j < STARPU_NMAXWORKERS; j++)
  127. {
  128. hypervisor.sched_ctx_w[i].current_idle_time[j] = 0.0;
  129. hypervisor.sched_ctx_w[i].pushed_tasks[j] = 0;
  130. hypervisor.sched_ctx_w[i].poped_tasks[j] = 0;
  131. hypervisor.sched_ctx_w[i].elapsed_flops[j] = 0.0;
  132. hypervisor.sched_ctx_w[i].total_elapsed_flops[j] = 0.0;
  133. hypervisor.sched_ctx_w[i].worker_to_be_removed[j] = 0;
  134. }
  135. }
  136. struct starpu_sched_ctx_hypervisor_policy *selected_hypervisor_policy = _select_hypervisor_policy(hypervisor_policy);
  137. _load_hypervisor_policy(selected_hypervisor_policy);
  138. perf_counters = (struct starpu_performance_counters*)malloc(sizeof(struct starpu_performance_counters));
  139. perf_counters->notify_idle_cycle = notify_idle_cycle;
  140. perf_counters->notify_pushed_task = notify_pushed_task;
  141. perf_counters->notify_poped_task = notify_poped_task;
  142. perf_counters->notify_post_exec_hook = notify_post_exec_hook;
  143. perf_counters->notify_idle_end = notify_idle_end;
  144. perf_counters->notify_submitted_job = notify_submitted_job;
  145. starpu_notify_hypervisor_exists();
  146. return perf_counters;
  147. }
  148. const char* sched_ctx_hypervisor_get_policy()
  149. {
  150. return hypervisor.policy.name;
  151. }
  152. /* the user can forbid the resizing process*/
  153. void sched_ctx_hypervisor_stop_resize(unsigned sched_ctx)
  154. {
  155. imposed_resize = 1;
  156. hypervisor.resize[sched_ctx] = 0;
  157. }
  158. /* the user can restart the resizing process*/
  159. void sched_ctx_hypervisor_start_resize(unsigned sched_ctx)
  160. {
  161. imposed_resize = 1;
  162. hypervisor.resize[sched_ctx] = 1;
  163. }
  164. void sched_ctx_hypervisor_shutdown(void)
  165. {
  166. printf("shutdown\n");
  167. int i;
  168. for(i = 0; i < STARPU_NMAX_SCHED_CTXS; i++)
  169. {
  170. if(hypervisor.sched_ctxs[i] != STARPU_NMAX_SCHED_CTXS && hypervisor.nsched_ctxs > 0)
  171. {
  172. sched_ctx_hypervisor_stop_resize(hypervisor.sched_ctxs[i]);
  173. sched_ctx_hypervisor_unregister_ctx(hypervisor.sched_ctxs[i]);
  174. pthread_mutex_destroy(&hypervisor.sched_ctx_w[i].mutex);
  175. }
  176. }
  177. perf_counters->notify_idle_cycle = NULL;
  178. perf_counters->notify_pushed_task = NULL;
  179. perf_counters->notify_poped_task = NULL;
  180. perf_counters->notify_post_exec_hook = NULL;
  181. perf_counters->notify_idle_end = NULL;
  182. free(perf_counters);
  183. perf_counters = NULL;
  184. pthread_mutex_destroy(&act_hypervisor_mutex);
  185. }
  186. /* the hypervisor is in charge only of the contexts registered to it*/
  187. void sched_ctx_hypervisor_register_ctx(unsigned sched_ctx, double total_flops)
  188. {
  189. pthread_mutex_lock(&act_hypervisor_mutex);
  190. hypervisor.configurations[sched_ctx] = NULL;
  191. hypervisor.resize_requests[sched_ctx] = NULL;
  192. pthread_mutex_init(&hypervisor.conf_mut[sched_ctx], NULL);
  193. pthread_mutex_init(&hypervisor.resize_mut[sched_ctx], NULL);
  194. _add_config(sched_ctx);
  195. hypervisor.sched_ctx_w[sched_ctx].sched_ctx = sched_ctx;
  196. hypervisor.sched_ctxs[hypervisor.nsched_ctxs++] = sched_ctx;
  197. hypervisor.sched_ctx_w[sched_ctx].total_flops = total_flops;
  198. hypervisor.sched_ctx_w[sched_ctx].remaining_flops = total_flops;
  199. if(strcmp(hypervisor.policy.name, "app_driven") == 0)
  200. hypervisor.resize[sched_ctx] = 1;
  201. pthread_mutex_unlock(&act_hypervisor_mutex);
  202. }
  203. static int _get_first_free_sched_ctx(int *sched_ctxs, unsigned nsched_ctxs)
  204. {
  205. int i;
  206. for(i = 0; i < nsched_ctxs; i++)
  207. if(sched_ctxs[i] == STARPU_NMAX_SCHED_CTXS)
  208. return i;
  209. return STARPU_NMAX_SCHED_CTXS;
  210. }
  211. /* rearange array of sched_ctxs in order not to have {MAXVAL, MAXVAL, 5, MAXVAL, 7}
  212. and have instead {5, 7, MAXVAL, MAXVAL, MAXVAL}
  213. it is easier afterwards to iterate the array
  214. */
  215. static void _rearange_sched_ctxs(int *sched_ctxs, int old_nsched_ctxs)
  216. {
  217. int first_free_id = STARPU_NMAX_SCHED_CTXS;
  218. int i;
  219. for(i = 0; i < old_nsched_ctxs; i++)
  220. {
  221. if(sched_ctxs[i] != STARPU_NMAX_SCHED_CTXS)
  222. {
  223. first_free_id = _get_first_free_sched_ctx(sched_ctxs, old_nsched_ctxs);
  224. if(first_free_id != STARPU_NMAX_SCHED_CTXS)
  225. {
  226. sched_ctxs[first_free_id] = sched_ctxs[i];
  227. sched_ctxs[i] = STARPU_NMAX_SCHED_CTXS;
  228. }
  229. }
  230. }
  231. }
  232. /* unregistered contexts will no longer be resized */
  233. void sched_ctx_hypervisor_unregister_ctx(unsigned sched_ctx)
  234. {
  235. pthread_mutex_lock(&act_hypervisor_mutex);
  236. unsigned i;
  237. for(i = 0; i < hypervisor.nsched_ctxs; i++)
  238. {
  239. if(hypervisor.sched_ctxs[i] == sched_ctx)
  240. {
  241. hypervisor.sched_ctxs[i] = STARPU_NMAX_SCHED_CTXS;
  242. break;
  243. }
  244. }
  245. _rearange_sched_ctxs(hypervisor.sched_ctxs, hypervisor.nsched_ctxs);
  246. hypervisor.nsched_ctxs--;
  247. hypervisor.sched_ctx_w[sched_ctx].sched_ctx = STARPU_NMAX_SCHED_CTXS;
  248. _remove_config(sched_ctx);
  249. /* free(hypervisor.configurations[sched_ctx]); */
  250. /* free(hypervisor.resize_requests[sched_ctx]); */
  251. pthread_mutex_destroy(&hypervisor.conf_mut[sched_ctx]);
  252. pthread_mutex_destroy(&hypervisor.resize_mut[sched_ctx]);
  253. if(hypervisor.nsched_ctxs == 1)
  254. sched_ctx_hypervisor_stop_resize(hypervisor.sched_ctxs[0]);
  255. pthread_mutex_unlock(&act_hypervisor_mutex);
  256. }
  257. static int get_ntasks( int *tasks)
  258. {
  259. int ntasks = 0;
  260. int j;
  261. for(j = 0; j < STARPU_NMAXWORKERS; j++)
  262. {
  263. ntasks += tasks[j];
  264. }
  265. return ntasks;
  266. }
  267. static void _get_cpus(int *workers, int nworkers, int *cpus, int *ncpus)
  268. {
  269. int i, worker;
  270. *ncpus = 0;
  271. for(i = 0; i < nworkers; i++)
  272. {
  273. worker = workers[i];
  274. enum starpu_archtype arch = starpu_worker_get_type(worker);
  275. if(arch == STARPU_CPU_WORKER)
  276. cpus[(*ncpus)++] = worker;
  277. }
  278. }
  279. int get_nworkers_ctx(unsigned sched_ctx, enum starpu_archtype arch)
  280. {
  281. int nworkers_ctx = 0;
  282. struct starpu_sched_ctx_worker_collection *workers = starpu_sched_ctx_get_worker_collection(sched_ctx);
  283. int worker;
  284. struct starpu_iterator it;
  285. if(workers->init_iterator)
  286. workers->init_iterator(workers, &it);
  287. while(workers->has_next(workers, &it))
  288. {
  289. worker = workers->get_next(workers, &it);
  290. enum starpu_archtype curr_arch = starpu_worker_get_type(worker);
  291. if(curr_arch == arch || arch == STARPU_ANY_WORKER)
  292. nworkers_ctx++;
  293. }
  294. return nworkers_ctx;
  295. }
  296. /* actually move the workers: the cpus are moved, gpus are only shared */
  297. /* forbids another resize request before this one is take into account */
  298. void sched_ctx_hypervisor_move_workers(unsigned sender_sched_ctx, unsigned receiver_sched_ctx, int* workers_to_move, unsigned nworkers_to_move, unsigned now)
  299. {
  300. if(nworkers_to_move > 0 && hypervisor.resize[sender_sched_ctx])// && hypervisor.resize[receiver_sched_ctx])
  301. {
  302. /* int j; */
  303. /* printf("resize ctx %d with", sender_sched_ctx); */
  304. /* for(j = 0; j < nworkers_to_move; j++) */
  305. /* printf(" %d", workers_to_move[j]); */
  306. /* printf("\n"); */
  307. int *cpus = (int*) malloc(nworkers_to_move * sizeof(int));
  308. int ncpus;
  309. _get_cpus(workers_to_move, nworkers_to_move, cpus, &ncpus);
  310. // if(ncpus != 0)
  311. // starpu_sched_ctx_remove_workers(cpus, ncpus, sender_sched_ctx);
  312. starpu_sched_ctx_add_workers(workers_to_move, nworkers_to_move, receiver_sched_ctx);
  313. if(now)
  314. {
  315. /* int j; */
  316. /* printf("remove from ctx %d:", sender_sched_ctx); */
  317. /* for(j = 0; j < nworkers_to_move; j++) */
  318. /* printf(" %d", workers_to_move[j]); */
  319. /* printf("\n"); */
  320. starpu_sched_ctx_remove_workers(workers_to_move, nworkers_to_move, sender_sched_ctx);
  321. }
  322. else
  323. {
  324. int ret = pthread_mutex_trylock(&hypervisor.sched_ctx_w[sender_sched_ctx].mutex);
  325. if(ret != EBUSY)
  326. {
  327. hypervisor.sched_ctx_w[sender_sched_ctx].resize_ack.receiver_sched_ctx = receiver_sched_ctx;
  328. hypervisor.sched_ctx_w[sender_sched_ctx].resize_ack.moved_workers = (int*)malloc(nworkers_to_move * sizeof(int));
  329. hypervisor.sched_ctx_w[sender_sched_ctx].resize_ack.nmoved_workers = nworkers_to_move;
  330. hypervisor.sched_ctx_w[sender_sched_ctx].resize_ack.acked_workers = (int*)malloc(nworkers_to_move * sizeof(int));
  331. int i;
  332. for(i = 0; i < nworkers_to_move; i++)
  333. {
  334. hypervisor.sched_ctx_w[sender_sched_ctx].current_idle_time[workers_to_move[i]] = 0.0;
  335. hypervisor.sched_ctx_w[sender_sched_ctx].resize_ack.moved_workers[i] = workers_to_move[i];
  336. hypervisor.sched_ctx_w[sender_sched_ctx].resize_ack.acked_workers[i] = 0;
  337. }
  338. hypervisor.resize[sender_sched_ctx] = 0;
  339. pthread_mutex_unlock(&hypervisor.sched_ctx_w[sender_sched_ctx].mutex);
  340. }
  341. }
  342. struct starpu_sched_ctx_hypervisor_policy_config *new_config = sched_ctx_hypervisor_get_config(receiver_sched_ctx);
  343. int i;
  344. for(i = 0; i < nworkers_to_move; i++)
  345. new_config->max_idle[workers_to_move[i]] = new_config->max_idle[workers_to_move[i]] !=MAX_IDLE_TIME ? new_config->max_idle[workers_to_move[i]] : new_config->new_workers_max_idle;
  346. }
  347. return;
  348. }
  349. void sched_ctx_hypervisor_add_workers_to_sched_ctx(int* workers_to_add, unsigned nworkers_to_add, unsigned sched_ctx)
  350. {
  351. if(nworkers_to_add > 0 && hypervisor.resize[sched_ctx])
  352. {
  353. /* int j; */
  354. /* printf("add to ctx %d:", sched_ctx); */
  355. /* for(j = 0; j < nworkers_to_add; j++) */
  356. /* printf(" %d", workers_to_add[j]); */
  357. /* printf("\n"); */
  358. starpu_sched_ctx_add_workers(workers_to_add, nworkers_to_add, sched_ctx);
  359. struct starpu_sched_ctx_hypervisor_policy_config *new_config = sched_ctx_hypervisor_get_config(sched_ctx);
  360. int i;
  361. for(i = 0; i < nworkers_to_add; i++)
  362. new_config->max_idle[workers_to_add[i]] = new_config->max_idle[workers_to_add[i]] != MAX_IDLE_TIME ? new_config->max_idle[workers_to_add[i]] : new_config->new_workers_max_idle;
  363. }
  364. return;
  365. }
  366. unsigned sched_ctx_hypervisor_can_resize(unsigned sched_ctx)
  367. {
  368. return hypervisor.resize[sched_ctx];
  369. }
  370. void sched_ctx_hypervisor_remove_workers_from_sched_ctx(int* workers_to_remove, unsigned nworkers_to_remove, unsigned sched_ctx, unsigned now)
  371. {
  372. if(nworkers_to_remove > 0 && hypervisor.resize[sched_ctx])
  373. {
  374. int nworkers=0;
  375. int workers[nworkers_to_remove];
  376. if(now)
  377. {
  378. /* int j; */
  379. /* printf("remove from ctx %d:", sched_ctx); */
  380. /* for(j = 0; j < nworkers_to_remove; j++) */
  381. /* printf(" %d", workers_to_remove[j]); */
  382. /* printf("\n"); */
  383. starpu_sched_ctx_remove_workers(workers_to_remove, nworkers_to_remove, sched_ctx);
  384. }
  385. else
  386. {
  387. int ret = pthread_mutex_trylock(&hypervisor.sched_ctx_w[sched_ctx].mutex);
  388. if(ret != EBUSY)
  389. {
  390. int i;
  391. for(i = 0; i < nworkers_to_remove; i++)
  392. if(starpu_sched_ctx_contains_worker(workers_to_remove[i], sched_ctx))
  393. workers[nworkers++] = workers_to_remove[i];
  394. hypervisor.sched_ctx_w[sched_ctx].resize_ack.receiver_sched_ctx = -1;
  395. hypervisor.sched_ctx_w[sched_ctx].resize_ack.moved_workers = (int*)malloc(nworkers_to_remove * sizeof(int));
  396. hypervisor.sched_ctx_w[sched_ctx].resize_ack.nmoved_workers = nworkers;
  397. hypervisor.sched_ctx_w[sched_ctx].resize_ack.acked_workers = (int*)malloc(nworkers_to_remove * sizeof(int));
  398. for(i = 0; i < nworkers; i++)
  399. {
  400. hypervisor.sched_ctx_w[sched_ctx].current_idle_time[workers[i]] = 0.0;
  401. hypervisor.sched_ctx_w[sched_ctx].resize_ack.moved_workers[i] = workers[i];
  402. hypervisor.sched_ctx_w[sched_ctx].resize_ack.acked_workers[i] = 0;
  403. }
  404. hypervisor.resize[sched_ctx] = 0;
  405. pthread_mutex_unlock(&hypervisor.sched_ctx_w[sched_ctx].mutex);
  406. }
  407. }
  408. }
  409. return;
  410. }
  411. static void _set_elapsed_flops_per_sched_ctx(unsigned sched_ctx, double val)
  412. {
  413. int i;
  414. for(i = 0; i < STARPU_NMAXWORKERS; i++)
  415. hypervisor.sched_ctx_w[sched_ctx].elapsed_flops[i] = val;
  416. }
  417. double sched_ctx_hypervisor_get_elapsed_flops_per_sched_ctx(struct starpu_sched_ctx_hypervisor_wrapper* sc_w)
  418. {
  419. double ret_val = 0.0;
  420. int i;
  421. for(i = 0; i < STARPU_NMAXWORKERS; i++)
  422. ret_val += sc_w->elapsed_flops[i];
  423. return ret_val;
  424. }
  425. double sched_ctx_hypervisor_get_total_elapsed_flops_per_sched_ctx(struct starpu_sched_ctx_hypervisor_wrapper* sc_w)
  426. {
  427. double ret_val = 0.0;
  428. int i;
  429. for(i = 0; i < STARPU_NMAXWORKERS; i++)
  430. ret_val += sc_w->total_elapsed_flops[i];
  431. return ret_val;
  432. }
  433. static unsigned _ack_resize_completed(unsigned sched_ctx, int worker)
  434. {
  435. if(worker != -1 && !starpu_sched_ctx_contains_worker(worker, sched_ctx))
  436. return 0;
  437. struct starpu_sched_ctx_hypervisor_resize_ack *resize_ack = NULL;
  438. unsigned sender_sched_ctx = STARPU_NMAX_SCHED_CTXS;
  439. int i;
  440. for(i = 0; i < STARPU_NMAX_SCHED_CTXS; i++)
  441. {
  442. if(hypervisor.sched_ctxs[i] != STARPU_NMAX_SCHED_CTXS)
  443. {
  444. struct starpu_sched_ctx_hypervisor_wrapper *sc_w = &hypervisor.sched_ctx_w[hypervisor.sched_ctxs[i]];
  445. pthread_mutex_lock(&sc_w->mutex);
  446. unsigned only_remove = 0;
  447. if(sc_w->resize_ack.receiver_sched_ctx == -1 && hypervisor.sched_ctxs[i] != sched_ctx &&
  448. sc_w->resize_ack.nmoved_workers > 0 && starpu_sched_ctx_contains_worker(worker, hypervisor.sched_ctxs[i]))
  449. {
  450. int j;
  451. for(j = 0; j < sc_w->resize_ack.nmoved_workers; j++)
  452. if(sc_w->resize_ack.moved_workers[j] == worker)
  453. {
  454. only_remove = 1;
  455. break;
  456. }
  457. }
  458. if(only_remove ||
  459. (sc_w->resize_ack.receiver_sched_ctx != -1 && sc_w->resize_ack.receiver_sched_ctx == sched_ctx))
  460. {
  461. resize_ack = &sc_w->resize_ack;
  462. sender_sched_ctx = hypervisor.sched_ctxs[i];
  463. pthread_mutex_unlock(&sc_w->mutex);
  464. break;
  465. }
  466. pthread_mutex_unlock(&sc_w->mutex);
  467. }
  468. }
  469. /* if there is no ctx waiting for its ack return 1*/
  470. if(resize_ack == NULL)
  471. return 1;
  472. int ret = pthread_mutex_trylock(&hypervisor.sched_ctx_w[sender_sched_ctx].mutex);
  473. if(ret != EBUSY)
  474. {
  475. int *moved_workers = resize_ack->moved_workers;
  476. int nmoved_workers = resize_ack->nmoved_workers;
  477. int *acked_workers = resize_ack->acked_workers;
  478. if(worker != -1)
  479. {
  480. for(i = 0; i < nmoved_workers; i++)
  481. {
  482. int moved_worker = moved_workers[i];
  483. if(moved_worker == worker && acked_workers[i] == 0)
  484. {
  485. acked_workers[i] = 1;
  486. }
  487. }
  488. }
  489. int nacked_workers = 0;
  490. for(i = 0; i < nmoved_workers; i++)
  491. {
  492. nacked_workers += (acked_workers[i] == 1);
  493. }
  494. unsigned resize_completed = (nacked_workers == nmoved_workers);
  495. int receiver_sched_ctx = sched_ctx;
  496. if(resize_completed)
  497. {
  498. /* if the permission to resize is not allowed by the user don't do it
  499. whatever the application says */
  500. if(!((hypervisor.resize[sender_sched_ctx] == 0 || hypervisor.resize[receiver_sched_ctx] == 0) && imposed_resize))
  501. {
  502. /* int j; */
  503. /* printf("remove from ctx %d:", sender_sched_ctx); */
  504. /* for(j = 0; j < nmoved_workers; j++) */
  505. /* printf(" %d", moved_workers[j]); */
  506. /* printf("\n"); */
  507. starpu_sched_ctx_remove_workers(moved_workers, nmoved_workers, sender_sched_ctx);
  508. /* info concerning only the gflops_rate strateg */
  509. struct starpu_sched_ctx_hypervisor_wrapper *sender_sc_w = &hypervisor.sched_ctx_w[sender_sched_ctx];
  510. struct starpu_sched_ctx_hypervisor_wrapper *receiver_sc_w = &hypervisor.sched_ctx_w[receiver_sched_ctx];
  511. double start_time = starpu_timing_now();
  512. sender_sc_w->start_time = start_time;
  513. sender_sc_w->remaining_flops = sender_sc_w->remaining_flops - sched_ctx_hypervisor_get_elapsed_flops_per_sched_ctx(sender_sc_w);
  514. _set_elapsed_flops_per_sched_ctx(sender_sched_ctx, 0.0);
  515. receiver_sc_w->start_time = start_time;
  516. receiver_sc_w->remaining_flops = receiver_sc_w->remaining_flops - sched_ctx_hypervisor_get_elapsed_flops_per_sched_ctx(receiver_sc_w);
  517. _set_elapsed_flops_per_sched_ctx(receiver_sched_ctx, 0.0);
  518. hypervisor.resize[sender_sched_ctx] = 1;
  519. // hypervisor.resize[receiver_sched_ctx] = 1;
  520. /* if the user allowed resizing leave the decisions to the application */
  521. if(imposed_resize) imposed_resize = 0;
  522. resize_ack->receiver_sched_ctx = -1;
  523. resize_ack->nmoved_workers = 0;
  524. free(resize_ack->moved_workers);
  525. free(resize_ack->acked_workers);
  526. }
  527. pthread_mutex_unlock(&hypervisor.sched_ctx_w[sender_sched_ctx].mutex);
  528. return resize_completed;
  529. }
  530. pthread_mutex_unlock(&hypervisor.sched_ctx_w[sender_sched_ctx].mutex);
  531. }
  532. return 0;
  533. }
  534. /* Enqueue a resize request for 'sched_ctx', to be executed when the
  535. * 'task_tag' tasks of 'sched_ctx' complete. */
  536. void sched_ctx_hypervisor_resize(unsigned sched_ctx, int task_tag)
  537. {
  538. struct resize_request_entry *entry;
  539. entry = malloc(sizeof *entry);
  540. STARPU_ASSERT(entry != NULL);
  541. entry->sched_ctx = sched_ctx;
  542. entry->task_tag = task_tag;
  543. pthread_mutex_lock(&hypervisor.resize_mut[sched_ctx]);
  544. HASH_ADD_INT(hypervisor.resize_requests[sched_ctx], task_tag, entry);
  545. pthread_mutex_unlock(&hypervisor.resize_mut[sched_ctx]);
  546. }
  547. /* notifies the hypervisor that the worker is no longer idle and a new task was pushed on its queue */
  548. static void notify_idle_end(unsigned sched_ctx, int worker)
  549. {
  550. if(hypervisor.resize[sched_ctx])
  551. hypervisor.sched_ctx_w[sched_ctx].current_idle_time[worker] = 0.0;
  552. if(hypervisor.policy.handle_idle_end)
  553. hypervisor.policy.handle_idle_end(sched_ctx, worker);
  554. }
  555. /* notifies the hypervisor that the worker spent another cycle in idle time */
  556. static void notify_idle_cycle(unsigned sched_ctx, int worker, double idle_time)
  557. {
  558. if(hypervisor.resize[sched_ctx])
  559. {
  560. struct starpu_sched_ctx_hypervisor_wrapper *sc_w = &hypervisor.sched_ctx_w[sched_ctx];
  561. sc_w->current_idle_time[worker] += idle_time;
  562. if(hypervisor.policy.handle_idle_cycle)
  563. {
  564. hypervisor.policy.handle_idle_cycle(sched_ctx, worker);
  565. }
  566. }
  567. return;
  568. }
  569. /* notifies the hypervisor that a new task was pushed on the queue of the worker */
  570. static void notify_pushed_task(unsigned sched_ctx, int worker)
  571. {
  572. hypervisor.sched_ctx_w[sched_ctx].pushed_tasks[worker]++;
  573. if(hypervisor.sched_ctx_w[sched_ctx].total_flops != 0.0 && hypervisor.sched_ctx_w[sched_ctx].start_time == 0.0)
  574. hypervisor.sched_ctx_w[sched_ctx].start_time = starpu_timing_now();
  575. int ntasks = get_ntasks(hypervisor.sched_ctx_w[sched_ctx].pushed_tasks);
  576. if((hypervisor.min_tasks == 0 || (!(hypervisor.resize[sched_ctx] == 0 && imposed_resize) && ntasks == hypervisor.min_tasks)) && hypervisor.check_min_tasks[sched_ctx])
  577. {
  578. hypervisor.resize[sched_ctx] = 1;
  579. if(imposed_resize) imposed_resize = 0;
  580. hypervisor.check_min_tasks[sched_ctx] = 0;
  581. }
  582. if(hypervisor.policy.handle_pushed_task)
  583. hypervisor.policy.handle_pushed_task(sched_ctx, worker);
  584. }
  585. /* notifies the hypervisor that a task was poped from the queue of the worker */
  586. static void notify_poped_task(unsigned sched_ctx, int worker, double elapsed_flops)
  587. {
  588. hypervisor.sched_ctx_w[sched_ctx].poped_tasks[worker]++;
  589. hypervisor.sched_ctx_w[sched_ctx].elapsed_flops[worker] += elapsed_flops;
  590. hypervisor.sched_ctx_w[sched_ctx].total_elapsed_flops[worker] += elapsed_flops;
  591. hypervisor.sched_ctx_w[sched_ctx].remaining_flops -= elapsed_flops; //sched_ctx_hypervisor_get_elapsed_flops_per_sched_ctx(&hypervisor.sched_ctx_w[sched_ctx]);
  592. if(hypervisor.resize[sched_ctx])
  593. {
  594. if(hypervisor.policy.handle_poped_task)
  595. hypervisor.policy.handle_poped_task(sched_ctx, worker);
  596. }
  597. _ack_resize_completed(sched_ctx, worker);
  598. }
  599. /* notifies the hypervisor that a tagged task has just been executed */
  600. static void notify_post_exec_hook(unsigned sched_ctx, int task_tag)
  601. {
  602. STARPU_ASSERT(task_tag > 0);
  603. unsigned conf_sched_ctx;
  604. int i;
  605. pthread_mutex_lock(&act_hypervisor_mutex);
  606. unsigned ns = hypervisor.nsched_ctxs;
  607. pthread_mutex_unlock(&act_hypervisor_mutex);
  608. for(i = 0; i < ns; i++)
  609. {
  610. struct configuration_entry *entry;
  611. conf_sched_ctx = hypervisor.sched_ctxs[i];
  612. pthread_mutex_lock(&hypervisor.conf_mut[conf_sched_ctx]);
  613. HASH_FIND_INT(hypervisor.configurations[conf_sched_ctx], &task_tag, entry);
  614. if (entry != NULL)
  615. {
  616. struct starpu_sched_ctx_hypervisor_policy_config *config = entry->configuration;
  617. sched_ctx_hypervisor_set_config(conf_sched_ctx, config);
  618. HASH_DEL(hypervisor.configurations[conf_sched_ctx], entry);
  619. free(config);
  620. }
  621. pthread_mutex_unlock(&hypervisor.conf_mut[conf_sched_ctx]);
  622. }
  623. if(hypervisor.resize[sched_ctx])
  624. {
  625. pthread_mutex_lock(&hypervisor.resize_mut[sched_ctx]);
  626. if(hypervisor.policy.handle_post_exec_hook)
  627. {
  628. /* Check whether 'task_tag' is in the 'resize_requests' set. */
  629. struct resize_request_entry *entry;
  630. HASH_FIND_INT(hypervisor.resize_requests[sched_ctx], &task_tag, entry);
  631. if (entry != NULL)
  632. {
  633. hypervisor.policy.handle_post_exec_hook(sched_ctx,
  634. task_tag);
  635. HASH_DEL(hypervisor.resize_requests[sched_ctx], entry);
  636. free(entry);
  637. }
  638. }
  639. pthread_mutex_unlock(&hypervisor.resize_mut[sched_ctx]);
  640. }
  641. return;
  642. }
  643. static void notify_submitted_job(struct starpu_task *task, uint32_t footprint)
  644. {
  645. pthread_mutex_lock(&act_hypervisor_mutex);
  646. hypervisor.sched_ctx_w[task->sched_ctx].submitted_flops += task->flops;
  647. pthread_mutex_unlock(&act_hypervisor_mutex);
  648. if(hypervisor.policy.handle_submitted_job)
  649. hypervisor.policy.handle_submitted_job(task, footprint);
  650. }
  651. void sched_ctx_hypervisor_size_ctxs(int *sched_ctxs, int nsched_ctxs, int *workers, int nworkers)
  652. {
  653. pthread_mutex_lock(&act_hypervisor_mutex);
  654. int curr_nsched_ctxs = sched_ctxs == NULL ? hypervisor.nsched_ctxs : nsched_ctxs;
  655. int *curr_sched_ctxs = sched_ctxs == NULL ? hypervisor.sched_ctxs : sched_ctxs;
  656. pthread_mutex_unlock(&act_hypervisor_mutex);
  657. int s;
  658. for(s = 0; s < curr_nsched_ctxs; s++)
  659. hypervisor.resize[curr_sched_ctxs[s]] = 1;
  660. if(hypervisor.policy.size_ctxs)
  661. hypervisor.policy.size_ctxs(curr_sched_ctxs, curr_nsched_ctxs, workers, nworkers);
  662. }
  663. struct starpu_sched_ctx_hypervisor_wrapper* sched_ctx_hypervisor_get_wrapper(unsigned sched_ctx)
  664. {
  665. return &hypervisor.sched_ctx_w[sched_ctx];
  666. }
  667. int* sched_ctx_hypervisor_get_sched_ctxs()
  668. {
  669. return hypervisor.sched_ctxs;
  670. }
  671. int sched_ctx_hypervisor_get_nsched_ctxs()
  672. {
  673. int ns;
  674. ns = hypervisor.nsched_ctxs;
  675. return ns;
  676. }
  677. void sched_ctx_hypervisor_save_size_req(int *sched_ctxs, int nsched_ctxs, int *workers, int nworkers)
  678. {
  679. hypervisor.sr = (struct size_request*)malloc(sizeof(struct size_request));
  680. hypervisor.sr->sched_ctxs = sched_ctxs;
  681. hypervisor.sr->nsched_ctxs = nsched_ctxs;
  682. hypervisor.sr->workers = workers;
  683. hypervisor.sr->nworkers = nworkers;
  684. }
  685. unsigned sched_ctx_hypervisor_get_size_req(int **sched_ctxs, int* nsched_ctxs, int **workers, int *nworkers)
  686. {
  687. if(hypervisor.sr != NULL)
  688. {
  689. *sched_ctxs = hypervisor.sr->sched_ctxs;
  690. *nsched_ctxs = hypervisor.sr->nsched_ctxs;
  691. *workers = hypervisor.sr->workers;
  692. *nworkers = hypervisor.sr->nworkers;
  693. return 1;
  694. }
  695. return 0;
  696. }
  697. void sched_ctx_hypervisor_free_size_req(void)
  698. {
  699. if(hypervisor.sr != NULL)
  700. {
  701. free(hypervisor.sr);
  702. hypervisor.sr = NULL;
  703. }
  704. }