sc_hypervisor.c 56 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705
  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 <sc_hypervisor_intern.h>
  17. #include <sc_hypervisor_policy.h>
  18. #include <starpu_config.h>
  19. double hyp_overhead = 0.0;
  20. unsigned imposed_resize = 0;
  21. unsigned type_of_tasks_known = 0;
  22. struct starpu_sched_ctx_performance_counters* perf_counters = NULL;
  23. static void notify_idle_cycle(unsigned sched_ctx, int worker, double idle_time);
  24. static void notify_pushed_task(unsigned sched_ctx, int worker);
  25. static void notify_post_exec_task(struct starpu_task *task, size_t data_size, uint32_t footprint,
  26. int hypervisor_tag, double flops);
  27. static void notify_poped_task(unsigned sched_ctx, int worker);
  28. static void notify_submitted_job(struct starpu_task *task, unsigned footprint, size_t data_size);
  29. static void notify_empty_ctx(unsigned sched_ctx, struct starpu_task *task);
  30. static void notify_delete_context(unsigned sched_ctx);
  31. extern struct sc_hypervisor_policy idle_policy;
  32. extern struct sc_hypervisor_policy app_driven_policy;
  33. extern struct sc_hypervisor_policy gflops_rate_policy;
  34. #ifdef STARPU_HAVE_GLPK_H
  35. extern struct sc_hypervisor_policy feft_lp_policy;
  36. extern struct sc_hypervisor_policy teft_lp_policy;
  37. extern struct sc_hypervisor_policy ispeed_lp_policy;
  38. extern struct sc_hypervisor_policy throughput_lp_policy;
  39. #endif // STARPU_HAVE_GLPK_
  40. extern struct sc_hypervisor_policy ispeed_policy;
  41. extern struct sc_hypervisor_policy hard_coded_policy;
  42. extern struct sc_hypervisor_policy perf_count_policy;
  43. static struct sc_hypervisor_policy *predefined_policies[] =
  44. {
  45. &idle_policy,
  46. &app_driven_policy,
  47. #ifdef STARPU_HAVE_GLPK_H
  48. &feft_lp_policy,
  49. &teft_lp_policy,
  50. &ispeed_lp_policy,
  51. &throughput_lp_policy,
  52. #endif // STARPU_HAVE_GLPK_H
  53. &gflops_rate_policy,
  54. &ispeed_policy,
  55. &hard_coded_policy,
  56. &perf_count_policy
  57. };
  58. static void _load_hypervisor_policy(struct sc_hypervisor_policy *policy)
  59. {
  60. STARPU_ASSERT(policy);
  61. hypervisor.policy.name = policy->name;
  62. hypervisor.policy.size_ctxs = policy->size_ctxs;
  63. hypervisor.policy.resize_ctxs = policy->resize_ctxs;
  64. hypervisor.policy.handle_poped_task = policy->handle_poped_task;
  65. hypervisor.policy.handle_pushed_task = policy->handle_pushed_task;
  66. hypervisor.policy.handle_idle_cycle = policy->handle_idle_cycle;
  67. hypervisor.policy.handle_idle_end = policy->handle_idle_end;
  68. hypervisor.policy.handle_post_exec_hook = policy->handle_post_exec_hook;
  69. hypervisor.policy.handle_submitted_job = policy->handle_submitted_job;
  70. hypervisor.policy.end_ctx = policy->end_ctx;
  71. hypervisor.policy.start_ctx = policy->start_ctx;
  72. hypervisor.policy.init_worker = policy->init_worker;
  73. }
  74. static struct sc_hypervisor_policy *_find_hypervisor_policy_from_name(const char *policy_name)
  75. {
  76. if (!policy_name)
  77. return NULL;
  78. unsigned i;
  79. for (i = 0; i < sizeof(predefined_policies)/sizeof(predefined_policies[0]); i++)
  80. {
  81. struct sc_hypervisor_policy *p;
  82. p = predefined_policies[i];
  83. if (p->name)
  84. {
  85. if (strcmp(policy_name, p->name) == 0) {
  86. /* we found a policy with the requested name */
  87. return p;
  88. }
  89. }
  90. }
  91. fprintf(stderr, "Warning: hypervisor policy \"%s\" was not found, try \"help\" to get a list\n", policy_name);
  92. /* nothing was found */
  93. return NULL;
  94. }
  95. static void display_sched_help_message(void)
  96. {
  97. const char* policy_name = getenv("SC_HYPERVISOR_POLICY");
  98. if (policy_name && (strcmp(policy_name, "help") == 0))
  99. {
  100. fprintf(stderr, "SC_HYPERVISOR_POLICY can be either of\n");
  101. /* display the description of all predefined policies */
  102. unsigned i;
  103. for (i = 0; i < sizeof(predefined_policies)/sizeof(predefined_policies[0]); i++)
  104. {
  105. struct sc_hypervisor_policy *p = predefined_policies[i];
  106. if (p->name)
  107. {
  108. fprintf(stderr, "%s\n", p->name);
  109. }
  110. }
  111. }
  112. }
  113. static struct sc_hypervisor_policy *_select_hypervisor_policy(struct sc_hypervisor_policy* hypervisor_policy)
  114. {
  115. struct sc_hypervisor_policy *selected_policy = NULL;
  116. if(hypervisor_policy && hypervisor_policy->custom)
  117. return hypervisor_policy;
  118. /* we look if the application specified the name of a policy to load */
  119. const char *policy_name;
  120. if (hypervisor_policy && hypervisor_policy->name)
  121. {
  122. policy_name = hypervisor_policy->name;
  123. }
  124. else
  125. {
  126. policy_name = getenv("SC_HYPERVISOR_POLICY");
  127. }
  128. if (policy_name)
  129. selected_policy = _find_hypervisor_policy_from_name(policy_name);
  130. /* Perhaps there was no policy that matched the name */
  131. if (selected_policy)
  132. return selected_policy;
  133. /* If no policy was specified, we use the idle policy as a default */
  134. return &idle_policy;
  135. }
  136. /* initializez the performance counters that starpu will use to retrive hints for resizing */
  137. void* sc_hypervisor_init(struct sc_hypervisor_policy *hypervisor_policy)
  138. {
  139. /* Perhaps we have to display some help */
  140. display_sched_help_message();
  141. hypervisor.min_tasks = 0;
  142. hypervisor.nsched_ctxs = 0;
  143. char* vel_gap = getenv("SC_HYPERVISOR_MAX_SPEED_GAP");
  144. hypervisor.max_speed_gap = vel_gap ? atof(vel_gap) : SC_SPEED_MAX_GAP_DEFAULT;
  145. char* crit = getenv("SC_HYPERVISOR_TRIGGER_RESIZE");
  146. hypervisor.resize_criteria = !crit ? SC_IDLE : strcmp(crit,"idle") == 0 ? SC_IDLE : (strcmp(crit,"speed") == 0 ? SC_SPEED : SC_NOTHING);
  147. STARPU_PTHREAD_MUTEX_INIT(&act_hypervisor_mutex, NULL);
  148. // hypervisor.start_executing_time = starpu_timing_now();
  149. int i;
  150. for(i = 0; i < STARPU_NMAX_SCHED_CTXS; i++)
  151. {
  152. hypervisor.resize[i] = 0;
  153. hypervisor.allow_remove[i] = 1;
  154. hypervisor.configurations[i] = NULL;
  155. hypervisor.sr = NULL;
  156. hypervisor.check_min_tasks[i] = 1;
  157. hypervisor.sched_ctxs[i] = STARPU_NMAX_SCHED_CTXS;
  158. hypervisor.sched_ctx_w[i].sched_ctx = STARPU_NMAX_SCHED_CTXS;
  159. hypervisor.sched_ctx_w[i].config = NULL;
  160. hypervisor.sched_ctx_w[i].total_flops = 0.0;
  161. hypervisor.sched_ctx_w[i].submitted_flops = 0.0;
  162. hypervisor.sched_ctx_w[i].remaining_flops = 0.0;
  163. hypervisor.sched_ctx_w[i].start_time = 0.0;
  164. hypervisor.sched_ctx_w[i].real_start_time = 0.0;
  165. hypervisor.sched_ctx_w[i].hyp_react_start_time = 0.0;
  166. hypervisor.sched_ctx_w[i].resize_ack.receiver_sched_ctx = -1;
  167. hypervisor.sched_ctx_w[i].resize_ack.moved_workers = NULL;
  168. hypervisor.sched_ctx_w[i].resize_ack.nmoved_workers = 0;
  169. hypervisor.sched_ctx_w[i].resize_ack.acked_workers = NULL;
  170. STARPU_PTHREAD_MUTEX_INIT(&hypervisor.sched_ctx_w[i].mutex, NULL);
  171. hypervisor.optimal_v[i] = 0.0;
  172. hypervisor.sched_ctx_w[i].ref_speed[0] = -1.0;
  173. hypervisor.sched_ctx_w[i].ref_speed[1] = -1.0;
  174. hypervisor.sched_ctx_w[i].total_flops_available = 0;
  175. hypervisor.sched_ctx_w[i].to_be_sized = 0;
  176. hypervisor.sched_ctx_w[i].consider_max = 0;
  177. int j;
  178. for(j = 0; j < STARPU_NMAXWORKERS; j++)
  179. {
  180. hypervisor.sched_ctx_w[i].start_time_w[i] = 0.0;
  181. hypervisor.sched_ctx_w[i].current_idle_time[j] = 0.0;
  182. hypervisor.sched_ctx_w[i].idle_time[j] = 0.0;
  183. hypervisor.sched_ctx_w[i].idle_start_time[j] = 0.0;
  184. hypervisor.sched_ctx_w[i].exec_time[j] = 0.0;
  185. hypervisor.sched_ctx_w[i].exec_start_time[j] = 0.0;
  186. hypervisor.sched_ctx_w[i].pushed_tasks[j] = 0;
  187. hypervisor.sched_ctx_w[i].poped_tasks[j] = 0;
  188. hypervisor.sched_ctx_w[i].elapsed_flops[j] = 0.0;
  189. hypervisor.sched_ctx_w[i].elapsed_data[j] = 0;
  190. hypervisor.sched_ctx_w[i].elapsed_tasks[j] = 0;
  191. hypervisor.sched_ctx_w[i].total_elapsed_flops[j] = 0.0;
  192. hypervisor.sched_ctx_w[i].worker_to_be_removed[j] = 0;
  193. hypervisor.sched_ctx_w[i].compute_idle[j] = 1;
  194. hypervisor.sched_ctx_w[i].compute_partial_idle[j] = 0;
  195. }
  196. }
  197. struct sc_hypervisor_policy *selected_hypervisor_policy = _select_hypervisor_policy(hypervisor_policy);
  198. _load_hypervisor_policy(selected_hypervisor_policy);
  199. perf_counters = (struct starpu_sched_ctx_performance_counters*)malloc(sizeof(struct starpu_sched_ctx_performance_counters));
  200. perf_counters->notify_idle_cycle = notify_idle_cycle;
  201. perf_counters->notify_pushed_task = notify_pushed_task;
  202. perf_counters->notify_poped_task = notify_poped_task;
  203. perf_counters->notify_post_exec_task = notify_post_exec_task;
  204. perf_counters->notify_submitted_job = notify_submitted_job;
  205. perf_counters->notify_empty_ctx = notify_empty_ctx;
  206. perf_counters->notify_delete_context = notify_delete_context;
  207. starpu_sched_ctx_notify_hypervisor_exists();
  208. return (void*)perf_counters;
  209. }
  210. const char* sc_hypervisor_get_policy()
  211. {
  212. return hypervisor.policy.name;
  213. }
  214. /* the user can forbid the resizing process*/
  215. void sc_hypervisor_stop_resize(unsigned sched_ctx)
  216. {
  217. imposed_resize = 1;
  218. hypervisor.resize[sched_ctx] = 0;
  219. }
  220. /* the user can restart the resizing process*/
  221. void sc_hypervisor_start_resize(unsigned sched_ctx)
  222. {
  223. imposed_resize = 1;
  224. hypervisor.resize[sched_ctx] = 1;
  225. }
  226. static void _print_current_time()
  227. {
  228. char* stop_print = getenv("SC_HYPERVISOR_STOP_PRINT");
  229. int sp = stop_print ? atoi(stop_print) : 1;
  230. if(!sp)
  231. {
  232. if(hypervisor.start_executing_time == 0.0)
  233. {
  234. fprintf(stdout, "Time: %lf\n", -1.0);
  235. return;
  236. }
  237. double curr_time = starpu_timing_now();
  238. double elapsed_time = (curr_time - hypervisor.start_executing_time) / 1000000.0; /* in seconds */
  239. fprintf(stdout, "Time: %lf\n", elapsed_time);
  240. int i;
  241. for(i = 0; i < STARPU_NMAX_SCHED_CTXS; i++)
  242. {
  243. if(hypervisor.sched_ctxs[i] != STARPU_NMAX_SCHED_CTXS)
  244. {
  245. struct sc_hypervisor_wrapper *sc_w = &hypervisor.sched_ctx_w[hypervisor.sched_ctxs[i]];
  246. double cpu_speed = sc_hypervisor_get_speed(sc_w, STARPU_CPU_WORKER);
  247. double cuda_speed = sc_hypervisor_get_speed(sc_w, STARPU_CUDA_WORKER);
  248. int ncpus = sc_hypervisor_get_nworkers_ctx(sc_w->sched_ctx, STARPU_CPU_WORKER);
  249. int ncuda = sc_hypervisor_get_nworkers_ctx(sc_w->sched_ctx, STARPU_CUDA_WORKER);
  250. fprintf(stdout, "%d: cpu_v = %lf cuda_v = %lf ncpus = %d ncuda = %d\n", hypervisor.sched_ctxs[i], cpu_speed, cuda_speed, ncpus, ncuda);
  251. }
  252. }
  253. }
  254. return;
  255. }
  256. void sc_hypervisor_shutdown(void)
  257. {
  258. int i;
  259. for(i = 0; i < STARPU_NMAX_SCHED_CTXS; i++)
  260. {
  261. if(hypervisor.sched_ctxs[i] != STARPU_NMAX_SCHED_CTXS && hypervisor.nsched_ctxs > 0)
  262. {
  263. sc_hypervisor_stop_resize(hypervisor.sched_ctxs[i]);
  264. sc_hypervisor_unregister_ctx(hypervisor.sched_ctxs[i]);
  265. STARPU_PTHREAD_MUTEX_DESTROY(&hypervisor.sched_ctx_w[i].mutex);
  266. }
  267. }
  268. perf_counters->notify_idle_cycle = NULL;
  269. perf_counters->notify_pushed_task = NULL;
  270. perf_counters->notify_poped_task = NULL;
  271. perf_counters->notify_post_exec_task = NULL;
  272. perf_counters->notify_delete_context = NULL;
  273. free(perf_counters);
  274. perf_counters = NULL;
  275. STARPU_PTHREAD_MUTEX_DESTROY(&act_hypervisor_mutex);
  276. }
  277. void sc_hypervisor_print_overhead()
  278. {
  279. // hyp_overhead /= 1000000.0;*
  280. FILE *f;
  281. const char *sched_env = getenv("OVERHEAD_FILE");
  282. if(!sched_env)
  283. f = fopen("overhead_microsec", "a");
  284. else
  285. f = fopen(sched_env, "a");
  286. fprintf(f, "%lf \n", hyp_overhead);
  287. fclose(f);
  288. }
  289. /* the hypervisor is in charge only of the contexts registered to it*/
  290. void sc_hypervisor_register_ctx(unsigned sched_ctx, double total_flops)
  291. {
  292. if(hypervisor.policy.start_ctx)
  293. hypervisor.policy.start_ctx(sched_ctx);
  294. STARPU_PTHREAD_MUTEX_LOCK(&act_hypervisor_mutex);
  295. hypervisor.configurations[sched_ctx] = NULL;
  296. hypervisor.resize_requests[sched_ctx] = NULL;
  297. STARPU_PTHREAD_MUTEX_INIT(&hypervisor.conf_mut[sched_ctx], NULL);
  298. STARPU_PTHREAD_MUTEX_INIT(&hypervisor.resize_mut[sched_ctx], NULL);
  299. _add_config(sched_ctx);
  300. hypervisor.sched_ctx_w[sched_ctx].sched_ctx = sched_ctx;
  301. hypervisor.sched_ctxs[hypervisor.nsched_ctxs++] = sched_ctx;
  302. hypervisor.sched_ctx_w[sched_ctx].total_flops = total_flops;
  303. hypervisor.sched_ctx_w[sched_ctx].remaining_flops = total_flops;
  304. hypervisor.resize[sched_ctx] = 0;//1;
  305. STARPU_PTHREAD_MUTEX_UNLOCK(&act_hypervisor_mutex);
  306. }
  307. static int _get_first_free_sched_ctx(unsigned *sched_ctxs, int nsched_ctxs)
  308. {
  309. int i;
  310. for(i = 0; i < nsched_ctxs; i++)
  311. if(sched_ctxs[i] == STARPU_NMAX_SCHED_CTXS)
  312. return i;
  313. return STARPU_NMAX_SCHED_CTXS;
  314. }
  315. /* rearange array of sched_ctxs in order not to have {MAXVAL, MAXVAL, 5, MAXVAL, 7}
  316. and have instead {5, 7, MAXVAL, MAXVAL, MAXVAL}
  317. it is easier afterwards to iterate the array
  318. */
  319. static void _rearange_sched_ctxs(unsigned *sched_ctxs, int old_nsched_ctxs)
  320. {
  321. int first_free_id = STARPU_NMAX_SCHED_CTXS;
  322. int i;
  323. for(i = 0; i < old_nsched_ctxs; i++)
  324. {
  325. if(sched_ctxs[i] != STARPU_NMAX_SCHED_CTXS)
  326. {
  327. first_free_id = _get_first_free_sched_ctx(sched_ctxs, old_nsched_ctxs);
  328. if(first_free_id != STARPU_NMAX_SCHED_CTXS)
  329. {
  330. sched_ctxs[first_free_id] = sched_ctxs[i];
  331. sched_ctxs[i] = STARPU_NMAX_SCHED_CTXS;
  332. }
  333. }
  334. }
  335. }
  336. /* unregistered contexts will no longer be resized */
  337. void sc_hypervisor_unregister_ctx(unsigned sched_ctx)
  338. {
  339. #ifdef STARPU_SC_HYPERVISOR_DEBUG
  340. printf("unregister ctx %d with remaining flops %lf \n", hypervisor.sched_ctx_w[sched_ctx].sched_ctx, hypervisor.sched_ctx_w[sched_ctx].remaining_flops);
  341. #endif
  342. if(hypervisor.policy.end_ctx)
  343. hypervisor.policy.end_ctx(sched_ctx);
  344. STARPU_PTHREAD_MUTEX_LOCK(&act_hypervisor_mutex);
  345. unsigned father = starpu_sched_ctx_get_inheritor(sched_ctx);
  346. int *pus;
  347. unsigned npus = starpu_sched_ctx_get_workers_list(sched_ctx, &pus);
  348. if(npus)
  349. {
  350. starpu_sched_ctx_set_priority(pus, npus, father, 1);
  351. free(pus);
  352. }
  353. unsigned i;
  354. for(i = 0; i < hypervisor.nsched_ctxs; i++)
  355. {
  356. if(hypervisor.sched_ctxs[i] == sched_ctx)
  357. {
  358. hypervisor.sched_ctxs[i] = STARPU_NMAX_SCHED_CTXS;
  359. break;
  360. }
  361. }
  362. _rearange_sched_ctxs(hypervisor.sched_ctxs, hypervisor.nsched_ctxs);
  363. hypervisor.nsched_ctxs--;
  364. hypervisor.sched_ctx_w[sched_ctx].sched_ctx = STARPU_NMAX_SCHED_CTXS;
  365. _remove_config(sched_ctx);
  366. STARPU_PTHREAD_MUTEX_DESTROY(&hypervisor.conf_mut[sched_ctx]);
  367. STARPU_PTHREAD_MUTEX_DESTROY(&hypervisor.resize_mut[sched_ctx]);
  368. if(hypervisor.nsched_ctxs == 1)
  369. sc_hypervisor_stop_resize(hypervisor.sched_ctxs[0]);
  370. STARPU_PTHREAD_MUTEX_UNLOCK(&act_hypervisor_mutex);
  371. }
  372. void sc_hypervisor_reset_react_start_time(unsigned sched_ctx, unsigned now)
  373. {
  374. if(now)
  375. hypervisor.sched_ctx_w[sched_ctx].hyp_react_start_time = starpu_timing_now();
  376. starpu_sched_ctx_update_start_resizing_sample(sched_ctx, starpu_timing_now());
  377. }
  378. double _get_max_speed_gap()
  379. {
  380. return hypervisor.max_speed_gap;
  381. }
  382. unsigned sc_hypervisor_get_resize_criteria()
  383. {
  384. return hypervisor.resize_criteria;
  385. }
  386. static int get_ntasks( int *tasks)
  387. {
  388. int ntasks = 0;
  389. int j;
  390. for(j = 0; j < STARPU_NMAXWORKERS; j++)
  391. {
  392. ntasks += tasks[j];
  393. }
  394. return ntasks;
  395. }
  396. int sc_hypervisor_get_nworkers_ctx(unsigned sched_ctx, enum starpu_worker_archtype arch)
  397. {
  398. int nworkers_ctx = 0;
  399. struct starpu_worker_collection *workers = starpu_sched_ctx_get_worker_collection(sched_ctx);
  400. int worker;
  401. struct starpu_sched_ctx_iterator it;
  402. workers->init_iterator(workers, &it);
  403. while(workers->has_next(workers, &it))
  404. {
  405. worker = workers->get_next(workers, &it);
  406. enum starpu_worker_archtype curr_arch = starpu_worker_get_type(worker);
  407. if(curr_arch == arch || arch == STARPU_ANY_WORKER)
  408. nworkers_ctx++;
  409. }
  410. return nworkers_ctx;
  411. }
  412. static void _set_elapsed_flops_per_sched_ctx(unsigned sched_ctx, double val)
  413. {
  414. int i;
  415. for(i = 0; i < STARPU_NMAXWORKERS; i++)
  416. {
  417. hypervisor.sched_ctx_w[sched_ctx].elapsed_flops[i] = val;
  418. if(val == 0)
  419. {
  420. hypervisor.sched_ctx_w[sched_ctx].elapsed_data[i] = 0;
  421. hypervisor.sched_ctx_w[sched_ctx].elapsed_tasks[i] = 0;
  422. }
  423. }
  424. }
  425. double sc_hypervisor_get_elapsed_flops_per_sched_ctx(struct sc_hypervisor_wrapper* sc_w)
  426. {
  427. double ret_val = 0.0;
  428. struct starpu_worker_collection *workers = starpu_sched_ctx_get_worker_collection(sc_w->sched_ctx);
  429. int worker;
  430. struct starpu_sched_ctx_iterator it;
  431. workers->init_iterator(workers, &it);
  432. while(workers->has_next(workers, &it))
  433. {
  434. worker = workers->get_next(workers, &it);
  435. ret_val += sc_w->elapsed_flops[worker];
  436. }
  437. return ret_val;
  438. }
  439. double sc_hypervisor_get_total_elapsed_flops_per_sched_ctx(struct sc_hypervisor_wrapper* sc_w)
  440. {
  441. double ret_val = 0.0;
  442. struct starpu_worker_collection *workers = starpu_sched_ctx_get_worker_collection(sc_w->sched_ctx);
  443. int worker;
  444. struct starpu_sched_ctx_iterator it;
  445. workers->init_iterator(workers, &it);
  446. while(workers->has_next(workers, &it))
  447. {
  448. worker = workers->get_next(workers, &it);
  449. ret_val += sc_w->total_elapsed_flops[worker];
  450. }
  451. return ret_val;
  452. }
  453. double sc_hypervisor_get_nready_flops_of_all_sons_of_sched_ctx(unsigned sched_ctx)
  454. {
  455. double ready_flops = starpu_sched_ctx_get_nready_flops(sched_ctx);
  456. unsigned *sched_ctxs;
  457. int nsched_ctxs = 0;
  458. sc_hypervisor_get_ctxs_on_level(&sched_ctxs, &nsched_ctxs, starpu_sched_ctx_get_hierarchy_level(sched_ctx), sched_ctx);
  459. int s;
  460. for(s = 0; s < nsched_ctxs; s++)
  461. ready_flops += sc_hypervisor_get_nready_flops_of_all_sons_of_sched_ctx(sched_ctxs[s]);
  462. //ready_flops += starpu_get_nready_flops_of_sched_ctx(sched_ctxs[s]);
  463. free(sched_ctxs);
  464. return ready_flops;
  465. }
  466. static void _decrement_elapsed_flops_per_worker(unsigned sched_ctx, int worker, double flops)
  467. {
  468. if(starpu_sched_ctx_get_hierarchy_level(sched_ctx) > 0)
  469. {
  470. unsigned father = starpu_sched_ctx_get_inheritor(sched_ctx);
  471. hypervisor.sched_ctx_w[father].elapsed_flops[worker] -= flops;
  472. _decrement_elapsed_flops_per_worker(father, worker, flops);
  473. }
  474. return;
  475. }
  476. void _reset_resize_sample_info(unsigned sender_sched_ctx, unsigned receiver_sched_ctx)
  477. {
  478. double start_time = starpu_timing_now();
  479. if(sender_sched_ctx != STARPU_NMAX_SCHED_CTXS)
  480. {
  481. /* info concerning only the gflops_rate strateg */
  482. struct sc_hypervisor_wrapper *sender_sc_w = &hypervisor.sched_ctx_w[sender_sched_ctx];
  483. sender_sc_w->start_time = start_time;
  484. unsigned nworkers = starpu_worker_get_count();
  485. int i;
  486. for(i = 0; i < nworkers; i++)
  487. {
  488. sender_sc_w->start_time_w[i] = start_time;
  489. sender_sc_w->idle_time[i] = 0.0;
  490. sender_sc_w->idle_start_time[i] = 0.0;
  491. hypervisor.sched_ctx_w[sender_sched_ctx].exec_time[i] = 0.0;
  492. // hypervisor.sched_ctx_w[sender_sched_ctx].exec_start_time[i] = (hypervisor.sched_ctx_w[sender_sched_ctx].exec_start_time[i] != 0.0) ? starpu_timing_now() : 0.0;
  493. _decrement_elapsed_flops_per_worker(sender_sched_ctx, i, hypervisor.sched_ctx_w[sender_sched_ctx].elapsed_flops[i]);
  494. }
  495. _set_elapsed_flops_per_sched_ctx(sender_sched_ctx, 0.0);
  496. }
  497. if(receiver_sched_ctx != STARPU_NMAX_SCHED_CTXS)
  498. {
  499. struct sc_hypervisor_wrapper *receiver_sc_w = &hypervisor.sched_ctx_w[receiver_sched_ctx];
  500. receiver_sc_w->start_time = start_time;
  501. unsigned nworkers = starpu_worker_get_count();
  502. int i;
  503. for(i = 0; i < nworkers; i++)
  504. {
  505. receiver_sc_w->start_time_w[i] = (receiver_sc_w->start_time_w[i] != 0.0) ? starpu_timing_now() : 0.0;
  506. receiver_sc_w->idle_time[i] = 0.0;
  507. receiver_sc_w->idle_start_time[i] = (receiver_sc_w->exec_start_time[i] != 0.0) ? 0.0 : starpu_timing_now();
  508. // hypervisor.sched_ctx_w[receiver_sched_ctx].exec_start_time[i] = (receiver_sc_w->exec_start_time[i] != 0.0) ? starpu_timing_now() : 0.0;
  509. hypervisor.sched_ctx_w[receiver_sched_ctx].exec_time[i] = 0.0;
  510. _decrement_elapsed_flops_per_worker(receiver_sched_ctx, i, hypervisor.sched_ctx_w[receiver_sched_ctx].elapsed_flops[i]);
  511. }
  512. _set_elapsed_flops_per_sched_ctx(receiver_sched_ctx, 0.0);
  513. }
  514. return;
  515. }
  516. /* actually move the workers: the cpus are moved, gpus are only shared */
  517. /* forbids another resize request before this one is take into account */
  518. void sc_hypervisor_move_workers(unsigned sender_sched_ctx, unsigned receiver_sched_ctx, int* workers_to_move, unsigned nworkers_to_move, unsigned now)
  519. {
  520. if(nworkers_to_move > 0 && hypervisor.resize[sender_sched_ctx])
  521. {
  522. _print_current_time();
  523. unsigned j;
  524. #ifdef STARPU_SC_HYPERVISOR_DEBUG
  525. printf("resize ctx %u with %u workers", sender_sched_ctx, nworkers_to_move);
  526. for(j = 0; j < nworkers_to_move; j++)
  527. printf(" %d", workers_to_move[j]);
  528. printf("\n");
  529. #endif
  530. hypervisor.allow_remove[receiver_sched_ctx] = 0;
  531. starpu_sched_ctx_add_workers(workers_to_move, nworkers_to_move, receiver_sched_ctx);
  532. if(now)
  533. {
  534. unsigned j;
  535. #ifdef STARPU_SC_HYPERVISOR_DEBUG
  536. printf("remove now from ctx %u:", sender_sched_ctx);
  537. for(j = 0; j < nworkers_to_move; j++)
  538. printf(" %d", workers_to_move[j]);
  539. printf("\n");
  540. #endif
  541. starpu_sched_ctx_remove_workers(workers_to_move, nworkers_to_move, sender_sched_ctx);
  542. hypervisor.allow_remove[receiver_sched_ctx] = 1;
  543. _reset_resize_sample_info(sender_sched_ctx, receiver_sched_ctx);
  544. }
  545. else
  546. {
  547. int ret = starpu_pthread_mutex_trylock(&hypervisor.sched_ctx_w[sender_sched_ctx].mutex);
  548. if(ret != EBUSY)
  549. {
  550. hypervisor.sched_ctx_w[sender_sched_ctx].resize_ack.receiver_sched_ctx = receiver_sched_ctx;
  551. hypervisor.sched_ctx_w[sender_sched_ctx].resize_ack.moved_workers = (int*)malloc(nworkers_to_move * sizeof(int));
  552. hypervisor.sched_ctx_w[sender_sched_ctx].resize_ack.nmoved_workers = nworkers_to_move;
  553. hypervisor.sched_ctx_w[sender_sched_ctx].resize_ack.acked_workers = (int*)malloc(nworkers_to_move * sizeof(int));
  554. unsigned i;
  555. for(i = 0; i < nworkers_to_move; i++)
  556. {
  557. hypervisor.sched_ctx_w[sender_sched_ctx].current_idle_time[workers_to_move[i]] = 0.0;
  558. hypervisor.sched_ctx_w[sender_sched_ctx].resize_ack.moved_workers[i] = workers_to_move[i];
  559. hypervisor.sched_ctx_w[sender_sched_ctx].resize_ack.acked_workers[i] = 0;
  560. }
  561. hypervisor.resize[sender_sched_ctx] = 0;
  562. if(imposed_resize) imposed_resize = 0;
  563. STARPU_PTHREAD_MUTEX_UNLOCK(&hypervisor.sched_ctx_w[sender_sched_ctx].mutex);
  564. }
  565. }
  566. struct sc_hypervisor_policy_config *new_config = sc_hypervisor_get_config(receiver_sched_ctx);
  567. unsigned i;
  568. for(i = 0; i < nworkers_to_move; i++)
  569. 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;
  570. }
  571. return;
  572. }
  573. void sc_hypervisor_add_workers_to_sched_ctx(int* workers_to_add, unsigned nworkers_to_add, unsigned sched_ctx)
  574. {
  575. if(nworkers_to_add > 0 && hypervisor.resize[sched_ctx])
  576. {
  577. _print_current_time();
  578. unsigned j;
  579. #ifdef STARPU_SC_HYPERVISOR_DEBUG
  580. printf("add to ctx %u:", sched_ctx);
  581. for(j = 0; j < nworkers_to_add; j++)
  582. printf(" %d", workers_to_add[j]);
  583. printf("\n");
  584. #endif
  585. starpu_sched_ctx_add_workers(workers_to_add, nworkers_to_add, sched_ctx);
  586. struct sc_hypervisor_policy_config *new_config = sc_hypervisor_get_config(sched_ctx);
  587. unsigned i;
  588. for(i = 0; i < nworkers_to_add; i++)
  589. 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;
  590. _reset_resize_sample_info(STARPU_NMAX_SCHED_CTXS, sched_ctx);
  591. }
  592. return;
  593. }
  594. unsigned sc_hypervisor_can_resize(unsigned sched_ctx)
  595. {
  596. return hypervisor.resize[sched_ctx];
  597. }
  598. void sc_hypervisor_remove_workers_from_sched_ctx(int* workers_to_remove, unsigned nworkers_to_remove, unsigned sched_ctx, unsigned now)
  599. {
  600. if(nworkers_to_remove > 0 && hypervisor.resize[sched_ctx] && hypervisor.allow_remove[sched_ctx])
  601. {
  602. _print_current_time();
  603. unsigned nworkers = 0;
  604. int workers[nworkers_to_remove];
  605. if(now)
  606. {
  607. unsigned j;
  608. #ifdef STARPU_SC_HYPERVISOR_DEBUG
  609. printf("remove explicitley now from ctx %u:", sched_ctx);
  610. for(j = 0; j < nworkers_to_remove; j++)
  611. printf(" %d", workers_to_remove[j]);
  612. printf("\n");
  613. #endif
  614. starpu_sched_ctx_remove_workers(workers_to_remove, nworkers_to_remove, sched_ctx);
  615. _reset_resize_sample_info(sched_ctx, STARPU_NMAX_SCHED_CTXS);
  616. }
  617. else
  618. {
  619. #ifdef STARPU_SC_HYPERVISOR_DEBUG
  620. printf("try to remove from ctx %u: ", sched_ctx);
  621. unsigned j;
  622. for(j = 0; j < nworkers_to_remove; j++)
  623. printf(" %d", workers_to_remove[j]);
  624. printf("\n");
  625. #endif
  626. int ret = starpu_pthread_mutex_trylock(&hypervisor.sched_ctx_w[sched_ctx].mutex);
  627. if(ret != EBUSY)
  628. {
  629. unsigned i;
  630. for(i = 0; i < nworkers_to_remove; i++)
  631. if(starpu_sched_ctx_contains_worker(workers_to_remove[i], sched_ctx))
  632. workers[nworkers++] = workers_to_remove[i];
  633. hypervisor.sched_ctx_w[sched_ctx].resize_ack.receiver_sched_ctx = -1;
  634. hypervisor.sched_ctx_w[sched_ctx].resize_ack.moved_workers = (int*)malloc(nworkers_to_remove * sizeof(int));
  635. hypervisor.sched_ctx_w[sched_ctx].resize_ack.nmoved_workers = (int)nworkers;
  636. hypervisor.sched_ctx_w[sched_ctx].resize_ack.acked_workers = (int*)malloc(nworkers_to_remove * sizeof(int));
  637. for(i = 0; i < nworkers; i++)
  638. {
  639. hypervisor.sched_ctx_w[sched_ctx].current_idle_time[workers[i]] = 0.0;
  640. hypervisor.sched_ctx_w[sched_ctx].resize_ack.moved_workers[i] = workers[i];
  641. hypervisor.sched_ctx_w[sched_ctx].resize_ack.acked_workers[i] = 0;
  642. }
  643. hypervisor.resize[sched_ctx] = 0;
  644. if(imposed_resize) imposed_resize = 0;
  645. STARPU_PTHREAD_MUTEX_UNLOCK(&hypervisor.sched_ctx_w[sched_ctx].mutex);
  646. }
  647. }
  648. }
  649. return;
  650. }
  651. static unsigned _ack_resize_completed(unsigned sched_ctx, int worker)
  652. {
  653. if(worker != -1 && !starpu_sched_ctx_contains_worker(worker, sched_ctx))
  654. return 0;
  655. struct sc_hypervisor_resize_ack *resize_ack = NULL;
  656. unsigned sender_sched_ctx = STARPU_NMAX_SCHED_CTXS;
  657. int i;
  658. for(i = 0; i < STARPU_NMAX_SCHED_CTXS; i++)
  659. {
  660. if(hypervisor.sched_ctxs[i] != STARPU_NMAX_SCHED_CTXS)
  661. {
  662. struct sc_hypervisor_wrapper *sc_w = &hypervisor.sched_ctx_w[hypervisor.sched_ctxs[i]];
  663. STARPU_PTHREAD_MUTEX_LOCK(&sc_w->mutex);
  664. unsigned only_remove = 0;
  665. if(sc_w->resize_ack.receiver_sched_ctx == -1 && hypervisor.sched_ctxs[i] != sched_ctx &&
  666. sc_w->resize_ack.nmoved_workers > 0 && starpu_sched_ctx_contains_worker(worker, hypervisor.sched_ctxs[i]))
  667. {
  668. int j;
  669. for(j = 0; j < sc_w->resize_ack.nmoved_workers; j++)
  670. if(sc_w->resize_ack.moved_workers[j] == worker)
  671. {
  672. only_remove = 1;
  673. _reset_resize_sample_info(sched_ctx, STARPU_NMAX_SCHED_CTXS);
  674. break;
  675. }
  676. }
  677. if(only_remove ||
  678. (sc_w->resize_ack.receiver_sched_ctx != -1 && sc_w->resize_ack.receiver_sched_ctx == (int)sched_ctx))
  679. {
  680. resize_ack = &sc_w->resize_ack;
  681. sender_sched_ctx = hypervisor.sched_ctxs[i];
  682. STARPU_PTHREAD_MUTEX_UNLOCK(&sc_w->mutex);
  683. break;
  684. }
  685. STARPU_PTHREAD_MUTEX_UNLOCK(&sc_w->mutex);
  686. }
  687. }
  688. /* if there is no ctx waiting for its ack return 1*/
  689. if(resize_ack == NULL)
  690. {
  691. return 1;
  692. }
  693. int ret = starpu_pthread_mutex_trylock(&hypervisor.sched_ctx_w[sender_sched_ctx].mutex);
  694. if(ret != EBUSY)
  695. {
  696. int *moved_workers = resize_ack->moved_workers;
  697. int nmoved_workers = resize_ack->nmoved_workers;
  698. int *acked_workers = resize_ack->acked_workers;
  699. if(worker != -1)
  700. {
  701. for(i = 0; i < nmoved_workers; i++)
  702. {
  703. int moved_worker = moved_workers[i];
  704. if(moved_worker == worker && acked_workers[i] == 0)
  705. {
  706. acked_workers[i] = 1;
  707. }
  708. }
  709. }
  710. int nacked_workers = 0;
  711. for(i = 0; i < nmoved_workers; i++)
  712. {
  713. nacked_workers += (acked_workers[i] == 1);
  714. }
  715. unsigned resize_completed = (nacked_workers == nmoved_workers);
  716. int receiver_sched_ctx = sched_ctx;
  717. if(resize_completed)
  718. {
  719. /* if the permission to resize is not allowed by the user don't do it
  720. whatever the application says */
  721. if(!((hypervisor.resize[sender_sched_ctx] == 0 || hypervisor.resize[receiver_sched_ctx] == 0) && imposed_resize))
  722. {
  723. /* int j; */
  724. /* printf("remove after ack from ctx %d:", sender_sched_ctx); */
  725. /* for(j = 0; j < nmoved_workers; j++) */
  726. /* printf(" %d", moved_workers[j]); */
  727. /* printf("\n"); */
  728. starpu_sched_ctx_remove_workers(moved_workers, nmoved_workers, sender_sched_ctx);
  729. _reset_resize_sample_info(sender_sched_ctx, receiver_sched_ctx);
  730. hypervisor.resize[sender_sched_ctx] = 1;
  731. hypervisor.allow_remove[receiver_sched_ctx] = 1;
  732. /* if the user allowed resizing leave the decisions to the application */
  733. if(imposed_resize) imposed_resize = 0;
  734. resize_ack->receiver_sched_ctx = -1;
  735. resize_ack->nmoved_workers = 0;
  736. free(resize_ack->moved_workers);
  737. free(resize_ack->acked_workers);
  738. }
  739. STARPU_PTHREAD_MUTEX_UNLOCK(&hypervisor.sched_ctx_w[sender_sched_ctx].mutex);
  740. return resize_completed;
  741. }
  742. STARPU_PTHREAD_MUTEX_UNLOCK(&hypervisor.sched_ctx_w[sender_sched_ctx].mutex);
  743. }
  744. return 0;
  745. }
  746. /* Enqueue a resize request for 'sched_ctx', to be executed when the
  747. * 'task_tag' tasks of 'sched_ctx' complete. */
  748. void sc_hypervisor_post_resize_request(unsigned sched_ctx, int task_tag)
  749. {
  750. struct resize_request_entry *entry;
  751. entry = malloc(sizeof *entry);
  752. STARPU_ASSERT(entry != NULL);
  753. entry->sched_ctx = sched_ctx;
  754. entry->task_tag = task_tag;
  755. STARPU_PTHREAD_MUTEX_LOCK(&hypervisor.resize_mut[sched_ctx]);
  756. HASH_ADD_INT(hypervisor.resize_requests[sched_ctx], task_tag, entry);
  757. STARPU_PTHREAD_MUTEX_UNLOCK(&hypervisor.resize_mut[sched_ctx]);
  758. }
  759. void sc_hypervisor_resize_ctxs(unsigned *sched_ctxs, int nsched_ctxs , int *workers, int nworkers)
  760. {
  761. if(hypervisor.policy.resize_ctxs)
  762. hypervisor.policy.resize_ctxs(sched_ctxs, nsched_ctxs, workers, nworkers);
  763. }
  764. void _sc_hypervisor_allow_compute_idle(unsigned sched_ctx, int worker, unsigned allow)
  765. {
  766. hypervisor.sched_ctx_w[sched_ctx].compute_idle[worker] = allow;
  767. }
  768. int _update_max_hierarchically(unsigned *sched_ctxs, int nsched_ctxs)
  769. {
  770. int s;
  771. unsigned leaves[hypervisor.nsched_ctxs];
  772. int nleaves = 0;
  773. sc_hypervisor_get_leaves(hypervisor.sched_ctxs, hypervisor.nsched_ctxs, leaves, &nleaves);
  774. int max = 0;
  775. for(s = 0; s < nsched_ctxs; s++)
  776. {
  777. struct sc_hypervisor_policy_config *config = sc_hypervisor_get_config(sched_ctxs[s]);
  778. unsigned found = 0;
  779. int l = 0;
  780. for(l = 0; l < nleaves; l++)
  781. {
  782. if(leaves[l] == sched_ctxs[s])
  783. {
  784. found = 1;
  785. break;
  786. }
  787. }
  788. if(!found)
  789. {
  790. config->max_nworkers = 0;
  791. int level = starpu_sched_ctx_get_hierarchy_level(sched_ctxs[s]);
  792. unsigned *sched_ctxs_child;
  793. int nsched_ctxs_child = 0;
  794. sc_hypervisor_get_ctxs_on_level(&sched_ctxs_child, &nsched_ctxs_child, level+1, sched_ctxs[s]);
  795. if(nsched_ctxs_child > 0)
  796. {
  797. config->max_nworkers += _update_max_hierarchically(sched_ctxs_child, nsched_ctxs_child);
  798. free(sched_ctxs_child);
  799. int max_possible_workers = starpu_worker_get_count();
  800. if(config->max_nworkers < 0)
  801. config->max_nworkers = 0;
  802. if(config->max_nworkers > max_possible_workers)
  803. config->max_nworkers = max_possible_workers;
  804. }
  805. #ifdef STARPU_SC_HYPERVISOR_DEBUG
  806. printf("ctx %u has max %d \n", sched_ctxs[s], config->max_nworkers);
  807. #endif
  808. }
  809. max += config->max_nworkers;
  810. }
  811. return max;
  812. }
  813. void _update_max_diff_hierarchically(unsigned father, double diff)
  814. {
  815. int level = starpu_sched_ctx_get_hierarchy_level(father);
  816. unsigned *sched_ctxs_child;
  817. int nsched_ctxs_child = 0;
  818. sc_hypervisor_get_ctxs_on_level(&sched_ctxs_child, &nsched_ctxs_child, level+1, father);
  819. if(nsched_ctxs_child > 0)
  820. {
  821. int s;
  822. double total_nflops = 0.0;
  823. for(s = 0; s < nsched_ctxs_child; s++)
  824. {
  825. total_nflops += hypervisor.sched_ctx_w[sched_ctxs_child[s]].remaining_flops < 0.0 ? 0.0 : hypervisor.sched_ctx_w[sched_ctxs_child[s]].remaining_flops;
  826. }
  827. int accumulated_diff = 0;
  828. for(s = 0; s < nsched_ctxs_child; s++)
  829. {
  830. struct sc_hypervisor_policy_config *config = sc_hypervisor_get_config(sched_ctxs_child[s]);
  831. double remaining_flops = hypervisor.sched_ctx_w[sched_ctxs_child[s]].remaining_flops < 0.0 ? 0.0 : hypervisor.sched_ctx_w[sched_ctxs_child[s]].remaining_flops;
  832. int current_diff = total_nflops == 0.0 ? 0.0 : floor((remaining_flops / total_nflops) * diff);
  833. accumulated_diff += current_diff;
  834. if(s == (nsched_ctxs_child - 1) && accumulated_diff < diff)
  835. current_diff += (diff - accumulated_diff);
  836. config->max_nworkers += current_diff;
  837. #ifdef STARPU_SC_HYPERVISOR_DEBUG
  838. printf("%u: redib max_nworkers incr %d diff = %d \n", sched_ctxs_child[s], config->max_nworkers, current_diff);
  839. #endif
  840. _update_max_diff_hierarchically(sched_ctxs_child[s], current_diff);
  841. }
  842. free(sched_ctxs_child);
  843. }
  844. return;
  845. }
  846. void sc_hypervisor_update_resize_interval(unsigned *sched_ctxs, int nsched_ctxs, int max_workers)
  847. {
  848. unsigned leaves[hypervisor.nsched_ctxs];
  849. unsigned nleaves = 0;
  850. sc_hypervisor_get_leaves(hypervisor.sched_ctxs, hypervisor.nsched_ctxs, leaves, &nleaves);
  851. int l;
  852. unsigned sched_ctx;
  853. int total_max_nworkers = 0;
  854. // int max_cpus = starpu_cpu_worker_get_count();
  855. unsigned configured = 0;
  856. int i;
  857. for(i = 0; i < nsched_ctxs; i++)
  858. {
  859. unsigned found = 0;
  860. for(l = 0; l < nleaves; l++)
  861. {
  862. if(leaves[l] == sched_ctxs[i])
  863. {
  864. found = 1;
  865. break;
  866. }
  867. }
  868. if(!found)
  869. continue;
  870. sched_ctx = sched_ctxs[i];
  871. if(hypervisor.sched_ctx_w[sched_ctx].to_be_sized) continue;
  872. struct sc_hypervisor_policy_config *config = sc_hypervisor_get_config(sched_ctx);
  873. struct starpu_worker_collection *workers = starpu_sched_ctx_get_worker_collection(sched_ctx);
  874. int worker;
  875. struct starpu_sched_ctx_iterator it;
  876. workers->init_iterator(workers, &it);
  877. double elapsed_time_worker[STARPU_NMAXWORKERS];
  878. double norm_idle_time = 0.0;
  879. double end_time = starpu_timing_now();
  880. while(workers->has_next(workers, &it))
  881. {
  882. double idle_time = 0.0;
  883. worker = workers->get_next(workers, &it);
  884. if(hypervisor.sched_ctx_w[sched_ctx].compute_idle[worker])
  885. {
  886. if(hypervisor.sched_ctx_w[sched_ctx].start_time_w[worker] == 0.0)
  887. elapsed_time_worker[worker] = 0.0;
  888. else
  889. elapsed_time_worker[worker] = (end_time - hypervisor.sched_ctx_w[sched_ctx].start_time_w[worker]) / 1000000.0;
  890. if(hypervisor.sched_ctx_w[sched_ctx].idle_start_time[worker] == 0.0)
  891. {
  892. idle_time = hypervisor.sched_ctx_w[sched_ctx].idle_time[worker]; /* in seconds */
  893. }
  894. else
  895. {
  896. double idle = (end_time - hypervisor.sched_ctx_w[sched_ctx].idle_start_time[worker]) / 1000000.0; /* in seconds */
  897. idle_time = hypervisor.sched_ctx_w[sched_ctx].idle_time[worker] + idle;
  898. }
  899. norm_idle_time += (elapsed_time_worker[worker] == 0.0 ? 0.0 : (idle_time / elapsed_time_worker[worker]));
  900. /* printf("%d/%d: start time %lf elapsed time %lf idle time %lf norm_idle_time %lf \n", */
  901. /* worker, sched_ctx, hypervisor.sched_ctx_w[sched_ctx].start_time_w[worker], elapsed_time_worker[worker], idle_time, norm_idle_time); */
  902. }
  903. }
  904. double norm_exec_time = 0.0;
  905. for(worker = 0; worker < STARPU_NMAXWORKERS; worker++)
  906. {
  907. double exec_time = 0.0;
  908. if(hypervisor.sched_ctx_w[sched_ctx].start_time_w[worker] == 0.0)
  909. elapsed_time_worker[worker] = 0.0;
  910. else
  911. elapsed_time_worker[worker] = (end_time - hypervisor.sched_ctx_w[sched_ctx].start_time_w[worker]) / 1000000.0;
  912. if(hypervisor.sched_ctx_w[sched_ctx].exec_start_time[worker] == 0.0)
  913. {
  914. exec_time = hypervisor.sched_ctx_w[sched_ctx].exec_time[worker];
  915. }
  916. else
  917. {
  918. double current_exec_time = 0.0;
  919. if(hypervisor.sched_ctx_w[sched_ctx].exec_start_time[worker] < hypervisor.sched_ctx_w[sched_ctx].start_time)
  920. current_exec_time = (end_time - hypervisor.sched_ctx_w[sched_ctx].start_time) / 1000000.0; /* in seconds */
  921. else
  922. current_exec_time = (end_time - hypervisor.sched_ctx_w[sched_ctx].exec_start_time[worker]) / 1000000.0; /* in seconds */
  923. exec_time = hypervisor.sched_ctx_w[sched_ctx].exec_time[worker] + current_exec_time;
  924. }
  925. norm_exec_time += elapsed_time_worker[worker] == 0.0 ? 0.0 : exec_time / elapsed_time_worker[worker];
  926. }
  927. double curr_time = starpu_timing_now();
  928. double elapsed_time = (curr_time - hypervisor.sched_ctx_w[sched_ctx].start_time) / 1000000.0; /* in seconds */
  929. int nready_tasks = starpu_sched_ctx_get_nready_tasks(sched_ctx);
  930. /* if(norm_idle_time >= 0.9) */
  931. /* { */
  932. /* config->max_nworkers = lrint(norm_exec_time); */
  933. /* } */
  934. /* else */
  935. /* { */
  936. /* if(norm_idle_time < 0.1) */
  937. /* config->max_nworkers = lrint(norm_exec_time) + nready_tasks - 1; //workers->nworkers + hypervisor.sched_ctx_w[sched_ctx].nready_tasks - 1; */
  938. /* else */
  939. /* config->max_nworkers = lrint(norm_exec_time); */
  940. /* } */
  941. config->max_nworkers = lrint(norm_exec_time);
  942. // config->max_nworkers = hypervisor.sched_ctx_w[sched_ctx].nready_tasks - 1;
  943. /* if(config->max_nworkers < 0) */
  944. /* config->max_nworkers = 0; */
  945. /* if(config->max_nworkers > max_workers) */
  946. /* config->max_nworkers = max_workers; */
  947. #ifdef STARPU_SC_HYPERVISOR_DEBUG
  948. printf("%u: ready tasks %d norm_idle_time %lf elapsed_time %lf norm_exec_time %lf nworker %d max %d \n",
  949. sched_ctx, nready_tasks, norm_idle_time, elapsed_time, norm_exec_time, workers->nworkers, config->max_nworkers);
  950. #endif
  951. total_max_nworkers += config->max_nworkers;
  952. configured = 1;
  953. }
  954. unsigned nhierarchy_levels = sc_hypervisor_get_nhierarchy_levels();
  955. if(nhierarchy_levels > 1 && configured)
  956. {
  957. unsigned *sched_ctxs2;
  958. int nsched_ctxs2;
  959. sc_hypervisor_get_ctxs_on_level(&sched_ctxs2, &nsched_ctxs2, 0, STARPU_NMAX_SCHED_CTXS);
  960. if(nsched_ctxs2 > 0)
  961. {
  962. _update_max_hierarchically(sched_ctxs2, nsched_ctxs2);
  963. int s;
  964. int current_total_max_nworkers = 0;
  965. double max_nflops = 0.0;
  966. unsigned max_nflops_sched_ctx = sched_ctxs2[0];
  967. for(s = 0; s < nsched_ctxs2; s++)
  968. {
  969. struct sc_hypervisor_policy_config *config = sc_hypervisor_get_config(sched_ctxs2[s]);
  970. current_total_max_nworkers += config->max_nworkers;
  971. if(max_nflops < hypervisor.sched_ctx_w[sched_ctxs2[s]].remaining_flops)
  972. {
  973. max_nflops = hypervisor.sched_ctx_w[sched_ctxs2[s]].remaining_flops;
  974. max_nflops_sched_ctx = sched_ctxs2[s];
  975. }
  976. }
  977. int max_possible_workers = starpu_worker_get_count();
  978. /*if the sum of the max cpus is smaller than the total cpus available
  979. increase the max for the ones having more ready tasks to exec */
  980. if(current_total_max_nworkers < max_possible_workers)
  981. {
  982. int diff = max_possible_workers - current_total_max_nworkers;
  983. struct sc_hypervisor_policy_config *config = sc_hypervisor_get_config(max_nflops_sched_ctx);
  984. config->max_nworkers += diff;
  985. #ifdef STARPU_SC_HYPERVISOR_DEBUG
  986. printf("%u: redib max_nworkers incr %d \n", max_nflops_sched_ctx, config->max_nworkers);
  987. #endif
  988. _update_max_diff_hierarchically(max_nflops_sched_ctx, diff);
  989. }
  990. free(sched_ctxs2);
  991. }
  992. }
  993. /*if the sum of the max cpus is smaller than the total cpus available
  994. increase the max for the ones having more ready tasks to exec */
  995. /* if(configured && total_max_nworkers < max_workers) */
  996. /* { */
  997. /* int diff = max_workers - total_max_nworkers; */
  998. /* int max_nready = -1; */
  999. /* unsigned max_nready_sched_ctx = sched_ctxs[0]; */
  1000. /* for(i = 0; i < nsched_ctxs; i++) */
  1001. /* { */
  1002. /* int nready_tasks = starpu_sched_ctx_get_nready_tasks(sched_ctxs[i]); */
  1003. /* if(max_nready < nready_tasks) */
  1004. /* { */
  1005. /* max_nready = nready_tasks; */
  1006. /* max_nready_sched_ctx = sched_ctxs[i]; */
  1007. /* } */
  1008. /* } */
  1009. /* struct sc_hypervisor_policy_config *config = sc_hypervisor_get_config(max_nready_sched_ctx); */
  1010. /* config->max_nworkers += diff; */
  1011. /* printf("%d: redib max_nworkers incr %d \n", max_nready_sched_ctx, config->max_nworkers); */
  1012. /* } */
  1013. }
  1014. /* notifies the hypervisor that a new task was pushed on the queue of the worker */
  1015. static void notify_pushed_task(unsigned sched_ctx, int worker)
  1016. {
  1017. hypervisor.sched_ctx_w[sched_ctx].pushed_tasks[worker]++;
  1018. if(hypervisor.sched_ctx_w[sched_ctx].total_flops != 0.0 && hypervisor.sched_ctx_w[sched_ctx].start_time == 0.0)
  1019. hypervisor.sched_ctx_w[sched_ctx].start_time = starpu_timing_now();
  1020. if(hypervisor.sched_ctx_w[sched_ctx].total_flops != 0.0 && hypervisor.sched_ctx_w[sched_ctx].start_time_w[worker] == 0.0)
  1021. {
  1022. hypervisor.sched_ctx_w[sched_ctx].start_time_w[worker] = starpu_timing_now();
  1023. }
  1024. int ntasks = get_ntasks(hypervisor.sched_ctx_w[sched_ctx].pushed_tasks);
  1025. if((hypervisor.min_tasks == 0 || (!(hypervisor.resize[sched_ctx] == 0 && imposed_resize) && ntasks == hypervisor.min_tasks)) && hypervisor.check_min_tasks[sched_ctx])
  1026. {
  1027. hypervisor.resize[sched_ctx] = 1;
  1028. if(imposed_resize) imposed_resize = 0;
  1029. hypervisor.check_min_tasks[sched_ctx] = 0;
  1030. }
  1031. if(hypervisor.policy.handle_pushed_task)
  1032. hypervisor.policy.handle_pushed_task(sched_ctx, worker);
  1033. }
  1034. unsigned choose_ctx_to_steal(int worker)
  1035. {
  1036. int j;
  1037. int ns = hypervisor.nsched_ctxs;
  1038. int max_ready_tasks = 0;
  1039. unsigned chosen_ctx = STARPU_NMAX_SCHED_CTXS;
  1040. for(j = 0; j < ns; j++)
  1041. {
  1042. unsigned other_ctx = hypervisor.sched_ctxs[j];
  1043. int nready = starpu_sched_ctx_get_nready_tasks(other_ctx);
  1044. if(!starpu_sched_ctx_contains_worker(worker, other_ctx) && max_ready_tasks < nready)
  1045. {
  1046. max_ready_tasks = nready;
  1047. chosen_ctx = other_ctx;
  1048. }
  1049. }
  1050. return chosen_ctx;
  1051. }
  1052. /* notifies the hypervisor that the worker spent another cycle in idle time */
  1053. static void notify_idle_cycle(unsigned sched_ctx, int worker, double idle_time)
  1054. {
  1055. if(hypervisor.start_executing_time == 0.0) return;
  1056. struct sc_hypervisor_wrapper *sc_w = &hypervisor.sched_ctx_w[sched_ctx];
  1057. sc_w->current_idle_time[worker] += idle_time;
  1058. if(sc_w->idle_start_time[worker] == 0.0 && sc_w->hyp_react_start_time != 0.0)
  1059. sc_w->idle_start_time[worker] = starpu_timing_now();
  1060. if(sc_w->idle_start_time[worker] > 0.0)
  1061. {
  1062. double end_time = starpu_timing_now();
  1063. sc_w->idle_time[worker] += (end_time - sc_w->idle_start_time[worker]) / 1000000.0; /* in seconds */
  1064. }
  1065. hypervisor.sched_ctx_w[sched_ctx].idle_start_time[worker] = starpu_timing_now();
  1066. if(hypervisor.resize[sched_ctx] && hypervisor.policy.handle_idle_cycle)
  1067. {
  1068. if(sc_w->hyp_react_start_time == 0.0)
  1069. sc_hypervisor_reset_react_start_time(sched_ctx, 1);
  1070. double curr_time = starpu_timing_now();
  1071. double elapsed_time = (curr_time - sc_w->hyp_react_start_time) / 1000000.0; /* in seconds */
  1072. if(sc_w->sched_ctx != STARPU_NMAX_SCHED_CTXS && elapsed_time > sc_w->config->time_sample)
  1073. {
  1074. unsigned idle_everywhere = 0;
  1075. unsigned *sched_ctxs = NULL;
  1076. unsigned nsched_ctxs = 0;
  1077. int ret = starpu_pthread_mutex_trylock(&act_hypervisor_mutex);
  1078. if(ret != EBUSY)
  1079. {
  1080. if(sc_hypervisor_check_idle(sched_ctx, worker))
  1081. {
  1082. idle_everywhere = 1;
  1083. nsched_ctxs = starpu_worker_get_sched_ctx_list(worker, &sched_ctxs);
  1084. int s;
  1085. for(s = 0; s < nsched_ctxs; s++)
  1086. {
  1087. if(hypervisor.sched_ctx_w[sched_ctxs[s]].sched_ctx != STARPU_NMAX_SCHED_CTXS)
  1088. {
  1089. if(!sc_hypervisor_check_idle(sched_ctxs[s], worker))
  1090. idle_everywhere = 0;
  1091. }
  1092. }
  1093. free(sched_ctxs);
  1094. }
  1095. STARPU_PTHREAD_MUTEX_UNLOCK(&act_hypervisor_mutex);
  1096. }
  1097. if(idle_everywhere)
  1098. {
  1099. double hyp_overhead_start = starpu_timing_now();
  1100. if(elapsed_time > (sc_w->config->time_sample*2))
  1101. hypervisor.policy.handle_idle_cycle(sched_ctx, worker);
  1102. double hyp_overhead_end = starpu_timing_now();
  1103. hyp_overhead += (hyp_overhead_end - hyp_overhead_start);
  1104. if(elapsed_time > (sc_w->config->time_sample*2))
  1105. sc_hypervisor_reset_react_start_time(sched_ctx, 1);
  1106. else
  1107. sc_hypervisor_reset_react_start_time(sched_ctx, 0);
  1108. }
  1109. }
  1110. }
  1111. return;
  1112. }
  1113. void _update_real_start_time_hierarchically(unsigned sched_ctx)
  1114. {
  1115. hypervisor.sched_ctx_w[sched_ctx].real_start_time = starpu_timing_now();
  1116. if(starpu_sched_ctx_get_hierarchy_level(sched_ctx) > 0)
  1117. {
  1118. _update_real_start_time_hierarchically(starpu_sched_ctx_get_inheritor(sched_ctx));
  1119. }
  1120. return;
  1121. }
  1122. /* notifies the hypervisor that the worker is no longer idle and a new task was pushed on its queue */
  1123. static void notify_poped_task(unsigned sched_ctx, int worker)
  1124. {
  1125. if(hypervisor.start_executing_time == 0.0)
  1126. hypervisor.start_executing_time = starpu_timing_now();
  1127. if(!hypervisor.resize[sched_ctx])
  1128. hypervisor.resize[sched_ctx] = 1;
  1129. if(hypervisor.sched_ctx_w[sched_ctx].total_flops != 0.0 && hypervisor.sched_ctx_w[sched_ctx].real_start_time == 0.0)
  1130. _update_real_start_time_hierarchically(sched_ctx);
  1131. if(hypervisor.sched_ctx_w[sched_ctx].start_time_w[worker] == 0.0)
  1132. {
  1133. hypervisor.sched_ctx_w[sched_ctx].start_time_w[worker] = starpu_timing_now();
  1134. }
  1135. hypervisor.sched_ctx_w[sched_ctx].exec_start_time[worker] = starpu_timing_now();
  1136. if(hypervisor.sched_ctx_w[sched_ctx].idle_start_time[worker] > 0.0)
  1137. {
  1138. int ns = hypervisor.nsched_ctxs;
  1139. int j;
  1140. for(j = 0; j < ns; j++)
  1141. {
  1142. if(hypervisor.sched_ctxs[j] != sched_ctx)
  1143. {
  1144. if(hypervisor.sched_ctx_w[hypervisor.sched_ctxs[j]].idle_start_time[worker] > 0.0)
  1145. hypervisor.sched_ctx_w[hypervisor.sched_ctxs[j]].compute_partial_idle[worker] = 1;
  1146. }
  1147. }
  1148. double end_time = starpu_timing_now();
  1149. double idle = (end_time - hypervisor.sched_ctx_w[sched_ctx].idle_start_time[worker]) / 1000000.0; /* in seconds */
  1150. if(hypervisor.sched_ctx_w[sched_ctx].compute_partial_idle[worker])
  1151. hypervisor.sched_ctx_w[sched_ctx].idle_time[worker] += idle / 2.0;
  1152. else
  1153. hypervisor.sched_ctx_w[sched_ctx].idle_time[worker] += idle;
  1154. hypervisor.sched_ctx_w[sched_ctx].compute_partial_idle[worker] = 0;
  1155. hypervisor.sched_ctx_w[sched_ctx].idle_start_time[worker] = 0.0;
  1156. }
  1157. if(hypervisor.resize[sched_ctx])
  1158. hypervisor.sched_ctx_w[sched_ctx].current_idle_time[worker] = 0.0;
  1159. if(hypervisor.policy.handle_idle_end)
  1160. hypervisor.policy.handle_idle_end(sched_ctx, worker);
  1161. }
  1162. static void _update_counters_hierarchically(int worker, unsigned sched_ctx, double flops, size_t data_size)
  1163. {
  1164. hypervisor.sched_ctx_w[sched_ctx].poped_tasks[worker]++;
  1165. hypervisor.sched_ctx_w[sched_ctx].elapsed_flops[worker] += flops;
  1166. hypervisor.sched_ctx_w[sched_ctx].elapsed_data[worker] += data_size ;
  1167. hypervisor.sched_ctx_w[sched_ctx].elapsed_tasks[worker]++ ;
  1168. hypervisor.sched_ctx_w[sched_ctx].total_elapsed_flops[worker] += flops;
  1169. STARPU_PTHREAD_MUTEX_LOCK(&hypervisor.sched_ctx_w[sched_ctx].mutex);
  1170. hypervisor.sched_ctx_w[sched_ctx].remaining_flops -= flops;
  1171. STARPU_PTHREAD_MUTEX_UNLOCK(&hypervisor.sched_ctx_w[sched_ctx].mutex);
  1172. if(starpu_sched_ctx_get_hierarchy_level(sched_ctx) > 0)
  1173. _update_counters_hierarchically(worker, starpu_sched_ctx_get_inheritor(sched_ctx), flops, data_size);
  1174. return;
  1175. }
  1176. /* notifies the hypervisor that a tagged task has just been executed */
  1177. static void notify_post_exec_task(struct starpu_task *task, size_t data_size, uint32_t footprint, int task_tag, double flops)
  1178. {
  1179. unsigned sched_ctx = task->sched_ctx;
  1180. int worker = starpu_worker_get_id_check();
  1181. if(hypervisor.sched_ctx_w[sched_ctx].exec_start_time[worker] != 0.0)
  1182. {
  1183. double current_time = starpu_timing_now();
  1184. double exec_time = (current_time -
  1185. hypervisor.sched_ctx_w[sched_ctx].exec_start_time[worker]) / 1000000.0; /* in seconds */
  1186. hypervisor.sched_ctx_w[sched_ctx].exec_time[worker] += exec_time;
  1187. hypervisor.sched_ctx_w[sched_ctx].exec_start_time[worker] = 0.0;
  1188. }
  1189. hypervisor.sched_ctx_w[sched_ctx].poped_tasks[worker]++;
  1190. hypervisor.sched_ctx_w[sched_ctx].elapsed_flops[worker] += flops;
  1191. hypervisor.sched_ctx_w[sched_ctx].elapsed_data[worker] += data_size ;
  1192. hypervisor.sched_ctx_w[sched_ctx].elapsed_tasks[worker]++ ;
  1193. hypervisor.sched_ctx_w[sched_ctx].total_elapsed_flops[worker] += flops;
  1194. STARPU_PTHREAD_MUTEX_LOCK(&hypervisor.sched_ctx_w[sched_ctx].mutex);
  1195. hypervisor.sched_ctx_w[sched_ctx].remaining_flops -= flops;
  1196. STARPU_PTHREAD_MUTEX_UNLOCK(&hypervisor.sched_ctx_w[sched_ctx].mutex);
  1197. if(_sc_hypervisor_use_lazy_resize())
  1198. _ack_resize_completed(sched_ctx, worker);
  1199. if(starpu_sched_ctx_get_hierarchy_level(sched_ctx) > 0)
  1200. {
  1201. _update_counters_hierarchically(worker, starpu_sched_ctx_get_inheritor(sched_ctx), flops, data_size);
  1202. }
  1203. if(hypervisor.resize[sched_ctx])
  1204. {
  1205. if(hypervisor.policy.handle_poped_task)
  1206. {
  1207. if(hypervisor.sched_ctx_w[sched_ctx].hyp_react_start_time == 0.0)
  1208. sc_hypervisor_reset_react_start_time(sched_ctx, 1);
  1209. double curr_time = starpu_timing_now();
  1210. double elapsed_time = (curr_time - hypervisor.sched_ctx_w[sched_ctx].hyp_react_start_time) / 1000000.0; /* in seconds */
  1211. if(hypervisor.sched_ctx_w[sched_ctx].sched_ctx != STARPU_NMAX_SCHED_CTXS && elapsed_time > hypervisor.sched_ctx_w[sched_ctx].config->time_sample)
  1212. {
  1213. double hyp_overhead_start = starpu_timing_now();
  1214. if(elapsed_time > (hypervisor.sched_ctx_w[sched_ctx].config->time_sample*2))
  1215. hypervisor.policy.handle_poped_task(sched_ctx, worker, task, footprint);
  1216. double hyp_overhead_end = starpu_timing_now();
  1217. hyp_overhead += (hyp_overhead_end - hyp_overhead_start);
  1218. if(elapsed_time > (hypervisor.sched_ctx_w[sched_ctx].config->time_sample*2))
  1219. sc_hypervisor_reset_react_start_time(sched_ctx, 1);
  1220. else
  1221. sc_hypervisor_reset_react_start_time(sched_ctx, 0);
  1222. }
  1223. else
  1224. /* no need to consider resizing, just remove the task from the pool if the strategy requires it*/
  1225. hypervisor.policy.handle_poped_task(sched_ctx, -2, task, footprint);
  1226. }
  1227. }
  1228. /* STARPU_PTHREAD_MUTEX_LOCK(&act_hypervisor_mutex); */
  1229. /* _ack_resize_completed(sched_ctx, worker); */
  1230. /* STARPU_PTHREAD_MUTEX_UNLOCK(&act_hypervisor_mutex); */
  1231. if(hypervisor.sched_ctx_w[sched_ctx].poped_tasks[worker] % 200 == 0)
  1232. _print_current_time();
  1233. if(task_tag <= 0)
  1234. return;
  1235. unsigned conf_sched_ctx;
  1236. unsigned i;
  1237. unsigned ns = hypervisor.nsched_ctxs;
  1238. for(i = 0; i < ns; i++)
  1239. {
  1240. struct configuration_entry *entry;
  1241. conf_sched_ctx = hypervisor.sched_ctxs[i];
  1242. STARPU_PTHREAD_MUTEX_LOCK(&hypervisor.conf_mut[conf_sched_ctx]);
  1243. HASH_FIND_INT(hypervisor.configurations[conf_sched_ctx], &task_tag, entry);
  1244. if (entry != NULL)
  1245. {
  1246. struct sc_hypervisor_policy_config *config = entry->configuration;
  1247. sc_hypervisor_set_config(conf_sched_ctx, config);
  1248. HASH_DEL(hypervisor.configurations[conf_sched_ctx], entry);
  1249. free(config);
  1250. }
  1251. STARPU_PTHREAD_MUTEX_UNLOCK(&hypervisor.conf_mut[conf_sched_ctx]);
  1252. }
  1253. if(hypervisor.resize[sched_ctx])
  1254. {
  1255. STARPU_PTHREAD_MUTEX_LOCK(&hypervisor.resize_mut[sched_ctx]);
  1256. if(hypervisor.policy.handle_post_exec_hook)
  1257. {
  1258. /* Check whether 'task_tag' is in the 'resize_requests' set. */
  1259. struct resize_request_entry *entry;
  1260. HASH_FIND_INT(hypervisor.resize_requests[sched_ctx], &task_tag, entry);
  1261. if (entry != NULL)
  1262. {
  1263. hypervisor.policy.handle_post_exec_hook(sched_ctx, task_tag);
  1264. HASH_DEL(hypervisor.resize_requests[sched_ctx], entry);
  1265. free(entry);
  1266. }
  1267. }
  1268. STARPU_PTHREAD_MUTEX_UNLOCK(&hypervisor.resize_mut[sched_ctx]);
  1269. }
  1270. return;
  1271. }
  1272. static void notify_submitted_job(struct starpu_task *task, uint32_t footprint, size_t data_size)
  1273. {
  1274. unsigned sched_ctx = task->sched_ctx;
  1275. STARPU_PTHREAD_MUTEX_LOCK(&hypervisor.sched_ctx_w[sched_ctx].mutex);
  1276. hypervisor.sched_ctx_w[sched_ctx].submitted_flops += task->flops;
  1277. STARPU_PTHREAD_MUTEX_UNLOCK(&hypervisor.sched_ctx_w[sched_ctx].mutex);
  1278. /* signaled by the user - no need to wait for them */
  1279. /* if(hypervisor.policy.handle_submitted_job && !type_of_tasks_known) */
  1280. /* hypervisor.policy.handle_submitted_job(task->cl, task->sched_ctx, footprint, data_size); */
  1281. }
  1282. static void notify_empty_ctx(unsigned sched_ctx_id, struct starpu_task *task)
  1283. {
  1284. sc_hypervisor_resize_ctxs(NULL, -1 , NULL, -1);
  1285. }
  1286. void sc_hypervisor_set_type_of_task(struct starpu_codelet *cl, unsigned sched_ctx, uint32_t footprint, size_t data_size)
  1287. {
  1288. type_of_tasks_known = 1;
  1289. if(hypervisor.policy.handle_submitted_job)
  1290. hypervisor.policy.handle_submitted_job(cl, sched_ctx, footprint, data_size);
  1291. }
  1292. static void notify_delete_context(unsigned sched_ctx)
  1293. {
  1294. _print_current_time();
  1295. sc_hypervisor_unregister_ctx(sched_ctx);
  1296. }
  1297. void sc_hypervisor_size_ctxs(unsigned *sched_ctxs, int nsched_ctxs, int *workers, int nworkers)
  1298. {
  1299. // STARPU_PTHREAD_MUTEX_LOCK(&act_hypervisor_mutex);
  1300. unsigned curr_nsched_ctxs = sched_ctxs == NULL ? hypervisor.nsched_ctxs : (unsigned)nsched_ctxs;
  1301. unsigned *curr_sched_ctxs = sched_ctxs == NULL ? hypervisor.sched_ctxs : sched_ctxs;
  1302. // STARPU_PTHREAD_MUTEX_UNLOCK(&act_hypervisor_mutex);
  1303. unsigned s;
  1304. for(s = 0; s < curr_nsched_ctxs; s++)
  1305. hypervisor.resize[curr_sched_ctxs[s]] = 1;
  1306. if(hypervisor.policy.size_ctxs)
  1307. hypervisor.policy.size_ctxs(curr_sched_ctxs, curr_nsched_ctxs, workers, nworkers);
  1308. }
  1309. struct sc_hypervisor_wrapper* sc_hypervisor_get_wrapper(unsigned sched_ctx)
  1310. {
  1311. return &hypervisor.sched_ctx_w[sched_ctx];
  1312. }
  1313. unsigned* sc_hypervisor_get_sched_ctxs()
  1314. {
  1315. return hypervisor.sched_ctxs;
  1316. }
  1317. int sc_hypervisor_get_nsched_ctxs()
  1318. {
  1319. int ns;
  1320. ns = hypervisor.nsched_ctxs;
  1321. return ns;
  1322. }
  1323. int _sc_hypervisor_use_lazy_resize(void)
  1324. {
  1325. char* lazy = getenv("SC_HYPERVISOR_LAZY_RESIZE");
  1326. return lazy ? atoi(lazy) : 1;
  1327. }
  1328. void sc_hypervisor_save_size_req(unsigned *sched_ctxs, int nsched_ctxs, int *workers, int nworkers)
  1329. {
  1330. hypervisor.sr = (struct size_request*)malloc(sizeof(struct size_request));
  1331. hypervisor.sr->sched_ctxs = sched_ctxs;
  1332. hypervisor.sr->nsched_ctxs = nsched_ctxs;
  1333. hypervisor.sr->workers = workers;
  1334. hypervisor.sr->nworkers = nworkers;
  1335. }
  1336. unsigned sc_hypervisor_get_size_req(unsigned **sched_ctxs, int* nsched_ctxs, int **workers, int *nworkers)
  1337. {
  1338. if(hypervisor.sr != NULL)
  1339. {
  1340. *sched_ctxs = hypervisor.sr->sched_ctxs;
  1341. *nsched_ctxs = hypervisor.sr->nsched_ctxs;
  1342. *workers = hypervisor.sr->workers;
  1343. *nworkers = hypervisor.sr->nworkers;
  1344. return 1;
  1345. }
  1346. return 0;
  1347. }
  1348. void sc_hypervisor_free_size_req(void)
  1349. {
  1350. if(hypervisor.sr != NULL)
  1351. {
  1352. free(hypervisor.sr);
  1353. hypervisor.sr = NULL;
  1354. }
  1355. }
  1356. double _get_optimal_v(unsigned sched_ctx)
  1357. {
  1358. return hypervisor.optimal_v[sched_ctx];
  1359. }
  1360. void _set_optimal_v(unsigned sched_ctx, double optimal_v)
  1361. {
  1362. hypervisor.optimal_v[sched_ctx] = optimal_v;
  1363. }
  1364. static struct types_of_workers* _init_structure_types_of_workers(void)
  1365. {
  1366. struct types_of_workers *tw = (struct types_of_workers*)malloc(sizeof(struct types_of_workers));
  1367. tw->ncpus = 0;
  1368. tw->ncuda = 0;
  1369. tw->nw = 0;
  1370. return tw;
  1371. }
  1372. struct types_of_workers* sc_hypervisor_get_types_of_workers(int *workers, unsigned nworkers)
  1373. {
  1374. struct types_of_workers *tw = _init_structure_types_of_workers();
  1375. unsigned w;
  1376. for(w = 0; w < nworkers; w++)
  1377. {
  1378. enum starpu_worker_archtype arch = workers == NULL ? starpu_worker_get_type((int)w) : starpu_worker_get_type(workers[w]);
  1379. if(arch == STARPU_CPU_WORKER)
  1380. tw->ncpus++;
  1381. if(arch == STARPU_CUDA_WORKER)
  1382. tw->ncuda++;
  1383. }
  1384. if(tw->ncpus > 0) tw->nw++;
  1385. if(tw->ncuda > 0) tw->nw++;
  1386. return tw;
  1387. }
  1388. void sc_hypervisor_update_diff_total_flops(unsigned sched_ctx, double diff_total_flops)
  1389. {
  1390. // double hyp_overhead_start = starpu_timing_now();
  1391. STARPU_PTHREAD_MUTEX_LOCK(&hypervisor.sched_ctx_w[sched_ctx].mutex);
  1392. hypervisor.sched_ctx_w[sched_ctx].total_flops += diff_total_flops;
  1393. hypervisor.sched_ctx_w[sched_ctx].remaining_flops += diff_total_flops;
  1394. STARPU_PTHREAD_MUTEX_UNLOCK(&hypervisor.sched_ctx_w[sched_ctx].mutex);
  1395. /* double hyp_overhead_end = starpu_timing_now(); */
  1396. /* hyp_overhead += (hyp_overhead_end - hyp_overhead_start); */
  1397. if(starpu_sched_ctx_get_hierarchy_level(sched_ctx) > 0)
  1398. sc_hypervisor_update_diff_total_flops(starpu_sched_ctx_get_inheritor(sched_ctx), diff_total_flops);
  1399. return;
  1400. }
  1401. void sc_hypervisor_update_diff_elapsed_flops(unsigned sched_ctx, double diff_elapsed_flops)
  1402. {
  1403. // double hyp_overhead_start = starpu_timing_now();
  1404. int workerid = starpu_worker_get_id();
  1405. if(workerid != -1)
  1406. {
  1407. // STARPU_PTHREAD_MUTEX_LOCK(&hypervisor.sched_ctx_w[sched_ctx].mutex);
  1408. hypervisor.sched_ctx_w[sched_ctx].elapsed_flops[workerid] += diff_elapsed_flops;
  1409. hypervisor.sched_ctx_w[sched_ctx].total_elapsed_flops[workerid] += diff_elapsed_flops;
  1410. // STARPU_PTHREAD_MUTEX_UNLOCK(&hypervisor.sched_ctx_w[sched_ctx].mutex);
  1411. }
  1412. /* double hyp_overhead_end = starpu_timing_now(); */
  1413. /* hyp_overhead += (hyp_overhead_end - hyp_overhead_start); */
  1414. if(starpu_sched_ctx_get_hierarchy_level(sched_ctx) > 0)
  1415. sc_hypervisor_update_diff_elapsed_flops(starpu_sched_ctx_get_inheritor(sched_ctx), diff_elapsed_flops);
  1416. return;
  1417. }
  1418. void sc_hypervisor_get_ctxs_on_level(unsigned **sched_ctxs, int *nsched_ctxs, unsigned hierarchy_level, unsigned father_sched_ctx_id)
  1419. {
  1420. unsigned s;
  1421. *nsched_ctxs = 0;
  1422. *sched_ctxs = (unsigned*)malloc(hypervisor.nsched_ctxs * sizeof(unsigned));
  1423. for(s = 0; s < hypervisor.nsched_ctxs; s++)
  1424. {
  1425. /* if father == STARPU_NMAX_SCHED_CTXS we take all the ctxs in this level */
  1426. if(starpu_sched_ctx_get_hierarchy_level(hypervisor.sched_ctxs[s]) == hierarchy_level &&
  1427. (starpu_sched_ctx_get_inheritor(hypervisor.sched_ctxs[s]) == father_sched_ctx_id || father_sched_ctx_id == STARPU_NMAX_SCHED_CTXS))
  1428. (*sched_ctxs)[(*nsched_ctxs)++] = hypervisor.sched_ctxs[s];
  1429. }
  1430. if(*nsched_ctxs == 0)
  1431. {
  1432. free(*sched_ctxs);
  1433. *sched_ctxs = NULL;
  1434. }
  1435. return;
  1436. }
  1437. unsigned sc_hypervisor_get_nhierarchy_levels(void)
  1438. {
  1439. unsigned nlevels = 0;
  1440. unsigned level = 0;
  1441. unsigned levels[STARPU_NMAX_SCHED_CTXS];
  1442. unsigned s, l;
  1443. for(s = 0; s < hypervisor.nsched_ctxs; s++)
  1444. {
  1445. level = starpu_sched_ctx_get_hierarchy_level(hypervisor.sched_ctxs[s]);
  1446. unsigned found = 0;
  1447. for(l = 0; l < nlevels; l++)
  1448. if(levels[l] == level)
  1449. found = 1;
  1450. if(!found)
  1451. levels[nlevels++] = level;
  1452. }
  1453. return nlevels;
  1454. }
  1455. void sc_hypervisor_get_leaves(unsigned *sched_ctxs, int nsched_ctxs, unsigned *leaves, int *nleaves)
  1456. {
  1457. int s, s2;
  1458. for(s = 0; s < nsched_ctxs; s++)
  1459. {
  1460. unsigned is_someones_father = 0;
  1461. for(s2 = 0; s2 < nsched_ctxs; s2++)
  1462. {
  1463. unsigned father = starpu_sched_ctx_get_inheritor(sched_ctxs[s2]);
  1464. if(sched_ctxs[s] == father)
  1465. {
  1466. is_someones_father = 1;
  1467. break;
  1468. }
  1469. }
  1470. if(!is_someones_father)
  1471. leaves[(*nleaves)++] = sched_ctxs[s];
  1472. }
  1473. return;
  1474. }
  1475. void sc_hypervisor_init_worker(int workerid, unsigned sched_ctx)
  1476. {
  1477. if(hypervisor.policy.init_worker)
  1478. hypervisor.policy.init_worker(workerid, sched_ctx);
  1479. }