sc_hypervisor.c 44 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318
  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. unsigned imposed_resize = 0;
  20. unsigned type_of_tasks_known = 0;
  21. struct starpu_sched_ctx_performance_counters* perf_counters = NULL;
  22. static void notify_idle_cycle(unsigned sched_ctx, int worker, double idle_time);
  23. static void notify_pushed_task(unsigned sched_ctx, int worker);
  24. static void notify_post_exec_task(struct starpu_task *task, size_t data_size, uint32_t footprint,
  25. int hypervisor_tag, double flops);
  26. static void notify_poped_task(unsigned sched_ctx, int worker);
  27. static void notify_submitted_job(struct starpu_task *task, unsigned footprint, size_t data_size);
  28. static void notify_empty_ctx(unsigned sched_ctx, struct starpu_task *task);
  29. static void notify_delete_context(unsigned sched_ctx);
  30. extern struct sc_hypervisor_policy idle_policy;
  31. extern struct sc_hypervisor_policy app_driven_policy;
  32. extern struct sc_hypervisor_policy gflops_rate_policy;
  33. #ifdef STARPU_HAVE_GLPK_H
  34. extern struct sc_hypervisor_policy feft_lp_policy;
  35. extern struct sc_hypervisor_policy teft_lp_policy;
  36. extern struct sc_hypervisor_policy ispeed_lp_policy;
  37. extern struct sc_hypervisor_policy throughput_lp_policy;
  38. #endif // STARPU_HAVE_GLPK_
  39. extern struct sc_hypervisor_policy ispeed_policy;
  40. static struct sc_hypervisor_policy *predefined_policies[] =
  41. {
  42. &idle_policy,
  43. &app_driven_policy,
  44. #ifdef STARPU_HAVE_GLPK_H
  45. &feft_lp_policy,
  46. &teft_lp_policy,
  47. &ispeed_lp_policy,
  48. &throughput_lp_policy,
  49. #endif // STARPU_HAVE_GLPK_H
  50. &gflops_rate_policy,
  51. &ispeed_policy
  52. };
  53. static void _load_hypervisor_policy(struct sc_hypervisor_policy *policy)
  54. {
  55. STARPU_ASSERT(policy);
  56. hypervisor.policy.name = policy->name;
  57. hypervisor.policy.size_ctxs = policy->size_ctxs;
  58. hypervisor.policy.resize_ctxs = policy->resize_ctxs;
  59. hypervisor.policy.handle_poped_task = policy->handle_poped_task;
  60. hypervisor.policy.handle_pushed_task = policy->handle_pushed_task;
  61. hypervisor.policy.handle_idle_cycle = policy->handle_idle_cycle;
  62. hypervisor.policy.handle_idle_end = policy->handle_idle_end;
  63. hypervisor.policy.handle_post_exec_hook = policy->handle_post_exec_hook;
  64. hypervisor.policy.handle_submitted_job = policy->handle_submitted_job;
  65. hypervisor.policy.end_ctx = policy->end_ctx;
  66. }
  67. static struct sc_hypervisor_policy *_find_hypervisor_policy_from_name(const char *policy_name)
  68. {
  69. if (!policy_name)
  70. return NULL;
  71. unsigned i;
  72. for (i = 0; i < sizeof(predefined_policies)/sizeof(predefined_policies[0]); i++)
  73. {
  74. struct sc_hypervisor_policy *p;
  75. p = predefined_policies[i];
  76. if (p->name)
  77. {
  78. if (strcmp(policy_name, p->name) == 0) {
  79. /* we found a policy with the requested name */
  80. return p;
  81. }
  82. }
  83. }
  84. fprintf(stderr, "Warning: hypervisor policy \"%s\" was not found, try \"help\" to get a list\n", policy_name);
  85. /* nothing was found */
  86. return NULL;
  87. }
  88. static void display_sched_help_message(void)
  89. {
  90. const char* policy_name = getenv("SC_HYPERVISOR_POLICY");
  91. if (policy_name && (strcmp(policy_name, "help") == 0))
  92. {
  93. fprintf(stderr, "SC_HYPERVISOR_POLICY can be either of\n");
  94. /* display the description of all predefined policies */
  95. unsigned i;
  96. for (i = 0; i < sizeof(predefined_policies)/sizeof(predefined_policies[0]); i++)
  97. {
  98. struct sc_hypervisor_policy *p = predefined_policies[i];
  99. if (p->name)
  100. {
  101. fprintf(stderr, "%s\n", p->name);
  102. }
  103. }
  104. }
  105. }
  106. static struct sc_hypervisor_policy *_select_hypervisor_policy(struct sc_hypervisor_policy* hypervisor_policy)
  107. {
  108. struct sc_hypervisor_policy *selected_policy = NULL;
  109. if(hypervisor_policy && hypervisor_policy->custom)
  110. return hypervisor_policy;
  111. /* we look if the application specified the name of a policy to load */
  112. const char *policy_name;
  113. if (hypervisor_policy && hypervisor_policy->name)
  114. {
  115. policy_name = hypervisor_policy->name;
  116. }
  117. else
  118. {
  119. policy_name = getenv("SC_HYPERVISOR_POLICY");
  120. }
  121. if (policy_name)
  122. selected_policy = _find_hypervisor_policy_from_name(policy_name);
  123. /* Perhaps there was no policy that matched the name */
  124. if (selected_policy)
  125. return selected_policy;
  126. /* If no policy was specified, we use the idle policy as a default */
  127. return &idle_policy;
  128. }
  129. /* initializez the performance counters that starpu will use to retrive hints for resizing */
  130. void* sc_hypervisor_init(struct sc_hypervisor_policy *hypervisor_policy)
  131. {
  132. /* Perhaps we have to display some help */
  133. display_sched_help_message();
  134. hypervisor.min_tasks = 0;
  135. hypervisor.nsched_ctxs = 0;
  136. char* vel_gap = getenv("SC_HYPERVISOR_MAX_SPEED_GAP");
  137. hypervisor.max_speed_gap = vel_gap ? atof(vel_gap) : SC_SPEED_MAX_GAP_DEFAULT;
  138. char* crit = getenv("SC_HYPERVISOR_TRIGGER_RESIZE");
  139. hypervisor.resize_criteria = !crit ? SC_IDLE : strcmp(crit,"idle") == 0 ? SC_IDLE : (strcmp(crit,"speed") == 0 ? SC_SPEED : SC_NOTHING);
  140. starpu_pthread_mutex_init(&act_hypervisor_mutex, NULL);
  141. hypervisor.start_executing_time = starpu_timing_now();
  142. int i;
  143. for(i = 0; i < STARPU_NMAX_SCHED_CTXS; i++)
  144. {
  145. hypervisor.resize[i] = 0;
  146. hypervisor.allow_remove[i] = 1;
  147. hypervisor.configurations[i] = NULL;
  148. hypervisor.sr = NULL;
  149. hypervisor.check_min_tasks[i] = 1;
  150. hypervisor.sched_ctxs[i] = STARPU_NMAX_SCHED_CTXS;
  151. hypervisor.sched_ctx_w[i].sched_ctx = STARPU_NMAX_SCHED_CTXS;
  152. hypervisor.sched_ctx_w[i].config = NULL;
  153. hypervisor.sched_ctx_w[i].total_flops = 0.0;
  154. hypervisor.sched_ctx_w[i].submitted_flops = 0.0;
  155. hypervisor.sched_ctx_w[i].remaining_flops = 0.0;
  156. hypervisor.sched_ctx_w[i].start_time = 0.0;
  157. hypervisor.sched_ctx_w[i].real_start_time = 0.0;
  158. hypervisor.sched_ctx_w[i].hyp_react_start_time = 0.0;
  159. hypervisor.sched_ctx_w[i].resize_ack.receiver_sched_ctx = -1;
  160. hypervisor.sched_ctx_w[i].resize_ack.moved_workers = NULL;
  161. hypervisor.sched_ctx_w[i].resize_ack.nmoved_workers = 0;
  162. hypervisor.sched_ctx_w[i].resize_ack.acked_workers = NULL;
  163. starpu_pthread_mutex_init(&hypervisor.sched_ctx_w[i].mutex, NULL);
  164. hypervisor.optimal_v[i] = 0.0;
  165. hypervisor.sched_ctx_w[i].ref_speed[0] = -1.0;
  166. hypervisor.sched_ctx_w[i].ref_speed[1] = -1.0;
  167. hypervisor.sched_ctx_w[i].total_flops_available = 0;
  168. hypervisor.sched_ctx_w[i].to_be_sized = 0;
  169. int j;
  170. for(j = 0; j < STARPU_NMAXWORKERS; j++)
  171. {
  172. hypervisor.sched_ctx_w[i].start_time_w[i] = 0.0;
  173. hypervisor.sched_ctx_w[i].current_idle_time[j] = 0.0;
  174. hypervisor.sched_ctx_w[i].idle_time[j] = 0.0;
  175. hypervisor.sched_ctx_w[i].idle_start_time[j] = 0.0;
  176. hypervisor.sched_ctx_w[i].exec_time[j] = 0.0;
  177. hypervisor.sched_ctx_w[i].exec_start_time[j] = 0.0;
  178. hypervisor.sched_ctx_w[i].pushed_tasks[j] = 0;
  179. hypervisor.sched_ctx_w[i].poped_tasks[j] = 0;
  180. hypervisor.sched_ctx_w[i].elapsed_flops[j] = 0.0;
  181. hypervisor.sched_ctx_w[i].elapsed_data[j] = 0;
  182. hypervisor.sched_ctx_w[i].elapsed_tasks[j] = 0;
  183. hypervisor.sched_ctx_w[i].total_elapsed_flops[j] = 0.0;
  184. hypervisor.sched_ctx_w[i].worker_to_be_removed[j] = 0;
  185. hypervisor.sched_ctx_w[i].compute_idle[j] = 1;
  186. hypervisor.sched_ctx_w[i].compute_partial_idle[j] = 0;
  187. }
  188. }
  189. struct sc_hypervisor_policy *selected_hypervisor_policy = _select_hypervisor_policy(hypervisor_policy);
  190. _load_hypervisor_policy(selected_hypervisor_policy);
  191. perf_counters = (struct starpu_sched_ctx_performance_counters*)malloc(sizeof(struct starpu_sched_ctx_performance_counters));
  192. perf_counters->notify_idle_cycle = notify_idle_cycle;
  193. perf_counters->notify_pushed_task = notify_pushed_task;
  194. perf_counters->notify_poped_task = notify_poped_task;
  195. perf_counters->notify_post_exec_task = notify_post_exec_task;
  196. perf_counters->notify_submitted_job = notify_submitted_job;
  197. perf_counters->notify_empty_ctx = notify_empty_ctx;
  198. perf_counters->notify_delete_context = notify_delete_context;
  199. starpu_sched_ctx_notify_hypervisor_exists();
  200. return (void*)perf_counters;
  201. }
  202. const char* sc_hypervisor_get_policy()
  203. {
  204. return hypervisor.policy.name;
  205. }
  206. /* the user can forbid the resizing process*/
  207. void sc_hypervisor_stop_resize(unsigned sched_ctx)
  208. {
  209. imposed_resize = 1;
  210. hypervisor.resize[sched_ctx] = 0;
  211. }
  212. /* the user can restart the resizing process*/
  213. void sc_hypervisor_start_resize(unsigned sched_ctx)
  214. {
  215. imposed_resize = 1;
  216. hypervisor.resize[sched_ctx] = 1;
  217. }
  218. static void _print_current_time()
  219. {
  220. if(!getenv("SC_HYPERVISOR_STOP_PRINT"))
  221. {
  222. double curr_time = starpu_timing_now();
  223. double elapsed_time = (curr_time - hypervisor.start_executing_time) / 1000000.0; /* in seconds */
  224. fprintf(stdout, "Time: %lf\n", elapsed_time);
  225. int i;
  226. for(i = 0; i < STARPU_NMAX_SCHED_CTXS; i++)
  227. {
  228. if(hypervisor.sched_ctxs[i] != STARPU_NMAX_SCHED_CTXS)
  229. {
  230. struct sc_hypervisor_wrapper *sc_w = &hypervisor.sched_ctx_w[hypervisor.sched_ctxs[i]];
  231. double cpu_speed = sc_hypervisor_get_speed(sc_w, STARPU_CPU_WORKER);
  232. double cuda_speed = sc_hypervisor_get_speed(sc_w, STARPU_CUDA_WORKER);
  233. int ncpus = sc_hypervisor_get_nworkers_ctx(sc_w->sched_ctx, STARPU_CPU_WORKER);
  234. int ncuda = sc_hypervisor_get_nworkers_ctx(sc_w->sched_ctx, STARPU_CUDA_WORKER);
  235. fprintf(stdout, "%d: cpu_v = %lf cuda_v = %lf ncpus = %d ncuda = %d\n", hypervisor.sched_ctxs[i], cpu_speed, cuda_speed, ncpus, ncuda);
  236. }
  237. }
  238. }
  239. return;
  240. }
  241. void sc_hypervisor_shutdown(void)
  242. {
  243. int i;
  244. for(i = 0; i < STARPU_NMAX_SCHED_CTXS; i++)
  245. {
  246. if(hypervisor.sched_ctxs[i] != STARPU_NMAX_SCHED_CTXS && hypervisor.nsched_ctxs > 0)
  247. {
  248. sc_hypervisor_stop_resize(hypervisor.sched_ctxs[i]);
  249. sc_hypervisor_unregister_ctx(hypervisor.sched_ctxs[i]);
  250. starpu_pthread_mutex_destroy(&hypervisor.sched_ctx_w[i].mutex);
  251. }
  252. }
  253. perf_counters->notify_idle_cycle = NULL;
  254. perf_counters->notify_pushed_task = NULL;
  255. perf_counters->notify_poped_task = NULL;
  256. perf_counters->notify_post_exec_task = NULL;
  257. perf_counters->notify_delete_context = NULL;
  258. free(perf_counters);
  259. perf_counters = NULL;
  260. starpu_pthread_mutex_destroy(&act_hypervisor_mutex);
  261. }
  262. /* the hypervisor is in charge only of the contexts registered to it*/
  263. void sc_hypervisor_register_ctx(unsigned sched_ctx, double total_flops)
  264. {
  265. starpu_pthread_mutex_lock(&act_hypervisor_mutex);
  266. hypervisor.configurations[sched_ctx] = NULL;
  267. hypervisor.resize_requests[sched_ctx] = NULL;
  268. starpu_pthread_mutex_init(&hypervisor.conf_mut[sched_ctx], NULL);
  269. starpu_pthread_mutex_init(&hypervisor.resize_mut[sched_ctx], NULL);
  270. _add_config(sched_ctx);
  271. hypervisor.sched_ctx_w[sched_ctx].sched_ctx = sched_ctx;
  272. hypervisor.sched_ctxs[hypervisor.nsched_ctxs++] = sched_ctx;
  273. hypervisor.sched_ctx_w[sched_ctx].total_flops = total_flops;
  274. hypervisor.sched_ctx_w[sched_ctx].remaining_flops = total_flops;
  275. hypervisor.resize[sched_ctx] = 1;
  276. starpu_pthread_mutex_unlock(&act_hypervisor_mutex);
  277. }
  278. static int _get_first_free_sched_ctx(unsigned *sched_ctxs, int nsched_ctxs)
  279. {
  280. int i;
  281. for(i = 0; i < nsched_ctxs; i++)
  282. if(sched_ctxs[i] == STARPU_NMAX_SCHED_CTXS)
  283. return i;
  284. return STARPU_NMAX_SCHED_CTXS;
  285. }
  286. /* rearange array of sched_ctxs in order not to have {MAXVAL, MAXVAL, 5, MAXVAL, 7}
  287. and have instead {5, 7, MAXVAL, MAXVAL, MAXVAL}
  288. it is easier afterwards to iterate the array
  289. */
  290. static void _rearange_sched_ctxs(unsigned *sched_ctxs, int old_nsched_ctxs)
  291. {
  292. int first_free_id = STARPU_NMAX_SCHED_CTXS;
  293. int i;
  294. for(i = 0; i < old_nsched_ctxs; i++)
  295. {
  296. if(sched_ctxs[i] != STARPU_NMAX_SCHED_CTXS)
  297. {
  298. first_free_id = _get_first_free_sched_ctx(sched_ctxs, old_nsched_ctxs);
  299. if(first_free_id != STARPU_NMAX_SCHED_CTXS)
  300. {
  301. sched_ctxs[first_free_id] = sched_ctxs[i];
  302. sched_ctxs[i] = STARPU_NMAX_SCHED_CTXS;
  303. }
  304. }
  305. }
  306. }
  307. /* unregistered contexts will no longer be resized */
  308. void sc_hypervisor_unregister_ctx(unsigned sched_ctx)
  309. {
  310. if(hypervisor.policy.end_ctx)
  311. hypervisor.policy.end_ctx(sched_ctx);
  312. starpu_pthread_mutex_lock(&act_hypervisor_mutex);
  313. unsigned i;
  314. for(i = 0; i < hypervisor.nsched_ctxs; i++)
  315. {
  316. if(hypervisor.sched_ctxs[i] == sched_ctx)
  317. {
  318. hypervisor.sched_ctxs[i] = STARPU_NMAX_SCHED_CTXS;
  319. break;
  320. }
  321. }
  322. _rearange_sched_ctxs(hypervisor.sched_ctxs, hypervisor.nsched_ctxs);
  323. hypervisor.nsched_ctxs--;
  324. hypervisor.sched_ctx_w[sched_ctx].sched_ctx = STARPU_NMAX_SCHED_CTXS;
  325. _remove_config(sched_ctx);
  326. starpu_pthread_mutex_destroy(&hypervisor.conf_mut[sched_ctx]);
  327. starpu_pthread_mutex_destroy(&hypervisor.resize_mut[sched_ctx]);
  328. if(hypervisor.nsched_ctxs == 1)
  329. sc_hypervisor_stop_resize(hypervisor.sched_ctxs[0]);
  330. starpu_pthread_mutex_unlock(&act_hypervisor_mutex);
  331. }
  332. double _get_max_speed_gap()
  333. {
  334. return hypervisor.max_speed_gap;
  335. }
  336. unsigned sc_hypervisor_get_resize_criteria()
  337. {
  338. return hypervisor.resize_criteria;
  339. }
  340. static int get_ntasks( int *tasks)
  341. {
  342. int ntasks = 0;
  343. int j;
  344. for(j = 0; j < STARPU_NMAXWORKERS; j++)
  345. {
  346. ntasks += tasks[j];
  347. }
  348. return ntasks;
  349. }
  350. int sc_hypervisor_get_nworkers_ctx(unsigned sched_ctx, enum starpu_worker_archtype arch)
  351. {
  352. int nworkers_ctx = 0;
  353. struct starpu_worker_collection *workers = starpu_sched_ctx_get_worker_collection(sched_ctx);
  354. int worker;
  355. struct starpu_sched_ctx_iterator it;
  356. if(workers->init_iterator)
  357. workers->init_iterator(workers, &it);
  358. while(workers->has_next(workers, &it))
  359. {
  360. worker = workers->get_next(workers, &it);
  361. enum starpu_worker_archtype curr_arch = starpu_worker_get_type(worker);
  362. if(curr_arch == arch || arch == STARPU_ANY_WORKER)
  363. nworkers_ctx++;
  364. }
  365. return nworkers_ctx;
  366. }
  367. static void _set_elapsed_flops_per_sched_ctx(unsigned sched_ctx, double val)
  368. {
  369. int i;
  370. for(i = 0; i < STARPU_NMAXWORKERS; i++)
  371. {
  372. hypervisor.sched_ctx_w[sched_ctx].elapsed_flops[i] = val;
  373. if(val == 0)
  374. {
  375. hypervisor.sched_ctx_w[sched_ctx].elapsed_data[i] = 0;
  376. hypervisor.sched_ctx_w[sched_ctx].elapsed_tasks[i] = 0;
  377. }
  378. }
  379. }
  380. double sc_hypervisor_get_elapsed_flops_per_sched_ctx(struct sc_hypervisor_wrapper* sc_w)
  381. {
  382. double ret_val = 0.0;
  383. struct starpu_worker_collection *workers = starpu_sched_ctx_get_worker_collection(sc_w->sched_ctx);
  384. int worker;
  385. struct starpu_sched_ctx_iterator it;
  386. if(workers->init_iterator)
  387. workers->init_iterator(workers, &it);
  388. while(workers->has_next(workers, &it))
  389. {
  390. worker = workers->get_next(workers, &it);
  391. ret_val += sc_w->elapsed_flops[worker];
  392. }
  393. return ret_val;
  394. }
  395. double sc_hypervisor_get_total_elapsed_flops_per_sched_ctx(struct sc_hypervisor_wrapper* sc_w)
  396. {
  397. double ret_val = 0.0;
  398. struct starpu_worker_collection *workers = starpu_sched_ctx_get_worker_collection(sc_w->sched_ctx);
  399. int worker;
  400. struct starpu_sched_ctx_iterator it;
  401. if(workers->init_iterator)
  402. workers->init_iterator(workers, &it);
  403. while(workers->has_next(workers, &it))
  404. {
  405. worker = workers->get_next(workers, &it);
  406. ret_val += sc_w->total_elapsed_flops[worker];
  407. }
  408. return ret_val;
  409. }
  410. void _reset_resize_sample_info(unsigned sender_sched_ctx, unsigned receiver_sched_ctx)
  411. {
  412. double start_time = starpu_timing_now();
  413. if(sender_sched_ctx != STARPU_NMAX_SCHED_CTXS)
  414. {
  415. /* info concerning only the gflops_rate strateg */
  416. struct sc_hypervisor_wrapper *sender_sc_w = &hypervisor.sched_ctx_w[sender_sched_ctx];
  417. sender_sc_w->start_time = start_time;
  418. _set_elapsed_flops_per_sched_ctx(sender_sched_ctx, 0.0);
  419. int i;
  420. for(i = 0; i < STARPU_NMAXWORKERS; i++)
  421. {
  422. sender_sc_w->start_time_w[i] = start_time;
  423. sender_sc_w->idle_time[i] = 0.0;
  424. sender_sc_w->idle_start_time[i] = 0.0;
  425. hypervisor.sched_ctx_w[sender_sched_ctx].exec_time[i] = 0.0;
  426. 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;
  427. }
  428. }
  429. if(receiver_sched_ctx != STARPU_NMAX_SCHED_CTXS)
  430. {
  431. struct sc_hypervisor_wrapper *receiver_sc_w = &hypervisor.sched_ctx_w[receiver_sched_ctx];
  432. receiver_sc_w->start_time = start_time;
  433. _set_elapsed_flops_per_sched_ctx(receiver_sched_ctx, 0.0);
  434. int i;
  435. for(i = 0; i < STARPU_NMAXWORKERS; i++)
  436. {
  437. receiver_sc_w->start_time_w[i] = (receiver_sc_w->start_time_w[i] != 0.0) ? starpu_timing_now() : 0.0;
  438. receiver_sc_w->idle_time[i] = 0.0;
  439. receiver_sc_w->idle_start_time[i] = (receiver_sc_w->exec_start_time[i] != 0.0) ? 0.0 : starpu_timing_now();
  440. 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;
  441. hypervisor.sched_ctx_w[receiver_sched_ctx].exec_time[i] = 0.0;
  442. }
  443. }
  444. }
  445. /* actually move the workers: the cpus are moved, gpus are only shared */
  446. /* forbids another resize request before this one is take into account */
  447. void sc_hypervisor_move_workers(unsigned sender_sched_ctx, unsigned receiver_sched_ctx, int* workers_to_move, unsigned nworkers_to_move, unsigned now)
  448. {
  449. if(nworkers_to_move > 0 && hypervisor.resize[sender_sched_ctx])
  450. {
  451. _print_current_time();
  452. unsigned j;
  453. printf("resize ctx %d with %d workers", sender_sched_ctx, nworkers_to_move);
  454. for(j = 0; j < nworkers_to_move; j++)
  455. printf(" %d", workers_to_move[j]);
  456. printf("\n");
  457. starpu_trace_user_event(1);
  458. hypervisor.allow_remove[receiver_sched_ctx] = 0;
  459. starpu_sched_ctx_add_workers(workers_to_move, nworkers_to_move, receiver_sched_ctx);
  460. if(now)
  461. {
  462. unsigned j;
  463. printf("remove now from ctx %d:", sender_sched_ctx);
  464. for(j = 0; j < nworkers_to_move; j++)
  465. printf(" %d", workers_to_move[j]);
  466. printf("\n");
  467. starpu_sched_ctx_remove_workers(workers_to_move, nworkers_to_move, sender_sched_ctx);
  468. hypervisor.allow_remove[receiver_sched_ctx] = 1;
  469. _reset_resize_sample_info(sender_sched_ctx, receiver_sched_ctx);
  470. }
  471. else
  472. {
  473. int ret = starpu_pthread_mutex_trylock(&hypervisor.sched_ctx_w[sender_sched_ctx].mutex);
  474. if(ret != EBUSY)
  475. {
  476. hypervisor.sched_ctx_w[sender_sched_ctx].resize_ack.receiver_sched_ctx = receiver_sched_ctx;
  477. hypervisor.sched_ctx_w[sender_sched_ctx].resize_ack.moved_workers = (int*)malloc(nworkers_to_move * sizeof(int));
  478. hypervisor.sched_ctx_w[sender_sched_ctx].resize_ack.nmoved_workers = nworkers_to_move;
  479. hypervisor.sched_ctx_w[sender_sched_ctx].resize_ack.acked_workers = (int*)malloc(nworkers_to_move * sizeof(int));
  480. unsigned i;
  481. for(i = 0; i < nworkers_to_move; i++)
  482. {
  483. hypervisor.sched_ctx_w[sender_sched_ctx].current_idle_time[workers_to_move[i]] = 0.0;
  484. hypervisor.sched_ctx_w[sender_sched_ctx].resize_ack.moved_workers[i] = workers_to_move[i];
  485. hypervisor.sched_ctx_w[sender_sched_ctx].resize_ack.acked_workers[i] = 0;
  486. }
  487. hypervisor.resize[sender_sched_ctx] = 0;
  488. if(imposed_resize) imposed_resize = 0;
  489. starpu_pthread_mutex_unlock(&hypervisor.sched_ctx_w[sender_sched_ctx].mutex);
  490. }
  491. }
  492. struct sc_hypervisor_policy_config *new_config = sc_hypervisor_get_config(receiver_sched_ctx);
  493. unsigned i;
  494. for(i = 0; i < nworkers_to_move; i++)
  495. 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;
  496. }
  497. return;
  498. }
  499. void sc_hypervisor_add_workers_to_sched_ctx(int* workers_to_add, unsigned nworkers_to_add, unsigned sched_ctx)
  500. {
  501. if(nworkers_to_add > 0 && hypervisor.resize[sched_ctx])
  502. {
  503. _print_current_time();
  504. unsigned j;
  505. printf("add to ctx %d:", sched_ctx);
  506. for(j = 0; j < nworkers_to_add; j++)
  507. printf(" %d", workers_to_add[j]);
  508. printf("\n");
  509. starpu_sched_ctx_add_workers(workers_to_add, nworkers_to_add, sched_ctx);
  510. starpu_sched_ctx_set_priority(workers_to_add, nworkers_to_add, sched_ctx, 1);
  511. struct sc_hypervisor_policy_config *new_config = sc_hypervisor_get_config(sched_ctx);
  512. unsigned i;
  513. for(i = 0; i < nworkers_to_add; i++)
  514. 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;
  515. _reset_resize_sample_info(STARPU_NMAX_SCHED_CTXS, sched_ctx);
  516. }
  517. return;
  518. }
  519. unsigned sc_hypervisor_can_resize(unsigned sched_ctx)
  520. {
  521. return hypervisor.resize[sched_ctx];
  522. }
  523. void sc_hypervisor_remove_workers_from_sched_ctx(int* workers_to_remove, unsigned nworkers_to_remove, unsigned sched_ctx, unsigned now)
  524. {
  525. if(nworkers_to_remove > 0 && hypervisor.resize[sched_ctx] && hypervisor.allow_remove[sched_ctx])
  526. {
  527. _print_current_time();
  528. unsigned nworkers = 0;
  529. int workers[nworkers_to_remove];
  530. if(now)
  531. {
  532. unsigned j;
  533. printf("remove explicitley now from ctx %d:", sched_ctx);
  534. for(j = 0; j < nworkers_to_remove; j++)
  535. printf(" %d", workers_to_remove[j]);
  536. printf("\n");
  537. starpu_sched_ctx_remove_workers(workers_to_remove, nworkers_to_remove, sched_ctx);
  538. _reset_resize_sample_info(sched_ctx, STARPU_NMAX_SCHED_CTXS);
  539. }
  540. else
  541. {
  542. printf("try to remove from ctx %d: ", sched_ctx);
  543. unsigned j;
  544. for(j = 0; j < nworkers_to_remove; j++)
  545. printf(" %d", workers_to_remove[j]);
  546. printf("\n");
  547. int ret = starpu_pthread_mutex_trylock(&hypervisor.sched_ctx_w[sched_ctx].mutex);
  548. if(ret != EBUSY)
  549. {
  550. unsigned i;
  551. for(i = 0; i < nworkers_to_remove; i++)
  552. if(starpu_sched_ctx_contains_worker(workers_to_remove[i], sched_ctx))
  553. workers[nworkers++] = workers_to_remove[i];
  554. hypervisor.sched_ctx_w[sched_ctx].resize_ack.receiver_sched_ctx = -1;
  555. hypervisor.sched_ctx_w[sched_ctx].resize_ack.moved_workers = (int*)malloc(nworkers_to_remove * sizeof(int));
  556. hypervisor.sched_ctx_w[sched_ctx].resize_ack.nmoved_workers = (int)nworkers;
  557. hypervisor.sched_ctx_w[sched_ctx].resize_ack.acked_workers = (int*)malloc(nworkers_to_remove * sizeof(int));
  558. for(i = 0; i < nworkers; i++)
  559. {
  560. hypervisor.sched_ctx_w[sched_ctx].current_idle_time[workers[i]] = 0.0;
  561. hypervisor.sched_ctx_w[sched_ctx].resize_ack.moved_workers[i] = workers[i];
  562. hypervisor.sched_ctx_w[sched_ctx].resize_ack.acked_workers[i] = 0;
  563. }
  564. hypervisor.resize[sched_ctx] = 0;
  565. if(imposed_resize) imposed_resize = 0;
  566. starpu_pthread_mutex_unlock(&hypervisor.sched_ctx_w[sched_ctx].mutex);
  567. }
  568. }
  569. }
  570. return;
  571. }
  572. static unsigned _ack_resize_completed(unsigned sched_ctx, int worker)
  573. {
  574. if(worker != -1 && !starpu_sched_ctx_contains_worker(worker, sched_ctx))
  575. return 0;
  576. struct sc_hypervisor_resize_ack *resize_ack = NULL;
  577. unsigned sender_sched_ctx = STARPU_NMAX_SCHED_CTXS;
  578. int i;
  579. for(i = 0; i < STARPU_NMAX_SCHED_CTXS; i++)
  580. {
  581. if(hypervisor.sched_ctxs[i] != STARPU_NMAX_SCHED_CTXS)
  582. {
  583. struct sc_hypervisor_wrapper *sc_w = &hypervisor.sched_ctx_w[hypervisor.sched_ctxs[i]];
  584. starpu_pthread_mutex_lock(&sc_w->mutex);
  585. unsigned only_remove = 0;
  586. if(sc_w->resize_ack.receiver_sched_ctx == -1 && hypervisor.sched_ctxs[i] != sched_ctx &&
  587. sc_w->resize_ack.nmoved_workers > 0 && starpu_sched_ctx_contains_worker(worker, hypervisor.sched_ctxs[i]))
  588. {
  589. int j;
  590. for(j = 0; j < sc_w->resize_ack.nmoved_workers; j++)
  591. if(sc_w->resize_ack.moved_workers[j] == worker)
  592. {
  593. only_remove = 1;
  594. _reset_resize_sample_info(sched_ctx, STARPU_NMAX_SCHED_CTXS);
  595. starpu_pthread_mutex_unlock(&sc_w->mutex);
  596. break;
  597. }
  598. }
  599. if(only_remove ||
  600. (sc_w->resize_ack.receiver_sched_ctx != -1 && sc_w->resize_ack.receiver_sched_ctx == (int)sched_ctx))
  601. {
  602. resize_ack = &sc_w->resize_ack;
  603. sender_sched_ctx = hypervisor.sched_ctxs[i];
  604. starpu_pthread_mutex_unlock(&sc_w->mutex);
  605. break;
  606. }
  607. starpu_pthread_mutex_unlock(&sc_w->mutex);
  608. }
  609. }
  610. /* if there is no ctx waiting for its ack return 1*/
  611. if(resize_ack == NULL)
  612. {
  613. return 1;
  614. }
  615. int ret = starpu_pthread_mutex_trylock(&hypervisor.sched_ctx_w[sender_sched_ctx].mutex);
  616. if(ret != EBUSY)
  617. {
  618. int *moved_workers = resize_ack->moved_workers;
  619. int nmoved_workers = resize_ack->nmoved_workers;
  620. int *acked_workers = resize_ack->acked_workers;
  621. if(worker != -1)
  622. {
  623. for(i = 0; i < nmoved_workers; i++)
  624. {
  625. int moved_worker = moved_workers[i];
  626. if(moved_worker == worker && acked_workers[i] == 0)
  627. {
  628. acked_workers[i] = 1;
  629. }
  630. }
  631. }
  632. int nacked_workers = 0;
  633. for(i = 0; i < nmoved_workers; i++)
  634. {
  635. nacked_workers += (acked_workers[i] == 1);
  636. }
  637. unsigned resize_completed = (nacked_workers == nmoved_workers);
  638. int receiver_sched_ctx = sched_ctx;
  639. if(resize_completed)
  640. {
  641. /* if the permission to resize is not allowed by the user don't do it
  642. whatever the application says */
  643. if(!((hypervisor.resize[sender_sched_ctx] == 0 || hypervisor.resize[receiver_sched_ctx] == 0) && imposed_resize))
  644. {
  645. /* int j; */
  646. /* printf("remove after ack from ctx %d:", sender_sched_ctx); */
  647. /* for(j = 0; j < nmoved_workers; j++) */
  648. /* printf(" %d", moved_workers[j]); */
  649. /* printf("\n"); */
  650. starpu_sched_ctx_remove_workers(moved_workers, nmoved_workers, sender_sched_ctx);
  651. _reset_resize_sample_info(sender_sched_ctx, receiver_sched_ctx);
  652. hypervisor.resize[sender_sched_ctx] = 1;
  653. hypervisor.allow_remove[receiver_sched_ctx] = 1;
  654. /* if the user allowed resizing leave the decisions to the application */
  655. if(imposed_resize) imposed_resize = 0;
  656. resize_ack->receiver_sched_ctx = -1;
  657. resize_ack->nmoved_workers = 0;
  658. free(resize_ack->moved_workers);
  659. free(resize_ack->acked_workers);
  660. }
  661. starpu_pthread_mutex_unlock(&hypervisor.sched_ctx_w[sender_sched_ctx].mutex);
  662. return resize_completed;
  663. }
  664. starpu_pthread_mutex_unlock(&hypervisor.sched_ctx_w[sender_sched_ctx].mutex);
  665. }
  666. return 0;
  667. }
  668. /* Enqueue a resize request for 'sched_ctx', to be executed when the
  669. * 'task_tag' tasks of 'sched_ctx' complete. */
  670. void sc_hypervisor_post_resize_request(unsigned sched_ctx, int task_tag)
  671. {
  672. struct resize_request_entry *entry;
  673. entry = malloc(sizeof *entry);
  674. STARPU_ASSERT(entry != NULL);
  675. entry->sched_ctx = sched_ctx;
  676. entry->task_tag = task_tag;
  677. starpu_pthread_mutex_lock(&hypervisor.resize_mut[sched_ctx]);
  678. HASH_ADD_INT(hypervisor.resize_requests[sched_ctx], task_tag, entry);
  679. starpu_pthread_mutex_unlock(&hypervisor.resize_mut[sched_ctx]);
  680. }
  681. void sc_hypervisor_resize_ctxs(unsigned *sched_ctxs, int nsched_ctxs , int *workers, int nworkers)
  682. {
  683. if(hypervisor.policy.resize_ctxs)
  684. hypervisor.policy.resize_ctxs(sched_ctxs, nsched_ctxs, workers, nworkers);
  685. }
  686. void _sc_hypervisor_allow_compute_idle(unsigned sched_ctx, int worker, unsigned allow)
  687. {
  688. hypervisor.sched_ctx_w[sched_ctx].compute_idle[worker] = allow;
  689. }
  690. void sc_hypervisor_update_resize_interval(unsigned *sched_ctxs, int nsched_ctxs)
  691. {
  692. unsigned sched_ctx;
  693. int total_max_nworkers = 0;
  694. int max_cpus = starpu_cpu_worker_get_count();
  695. double max_workers_idle_time[nsched_ctxs];
  696. unsigned configured = 0;
  697. int i;
  698. for(i = 0; i < nsched_ctxs; i++)
  699. {
  700. sched_ctx = sched_ctxs[i];
  701. if(hypervisor.sched_ctx_w[sched_ctx].to_be_sized) continue;
  702. struct sc_hypervisor_policy_config *config = sc_hypervisor_get_config(sched_ctx);
  703. struct starpu_worker_collection *workers = starpu_sched_ctx_get_worker_collection(sched_ctx);
  704. int worker;
  705. struct starpu_sched_ctx_iterator it;
  706. if(workers->init_iterator)
  707. workers->init_iterator(workers, &it);
  708. double elapsed_time_worker[STARPU_NMAXWORKERS];
  709. double norm_idle_time = 0.0;
  710. double end_time = starpu_timing_now();
  711. while(workers->has_next(workers, &it))
  712. {
  713. double idle_time = 0.0;
  714. worker = workers->get_next(workers, &it);
  715. if(hypervisor.sched_ctx_w[sched_ctx].compute_idle[worker])
  716. {
  717. if(hypervisor.sched_ctx_w[sched_ctx].start_time_w[worker] == 0.0)
  718. elapsed_time_worker[worker] = 0.0;
  719. else
  720. elapsed_time_worker[worker] = (end_time - hypervisor.sched_ctx_w[sched_ctx].start_time_w[worker]) / 1000000.0;
  721. if(hypervisor.sched_ctx_w[sched_ctx].idle_start_time[worker] == 0.0)
  722. {
  723. idle_time = hypervisor.sched_ctx_w[sched_ctx].idle_time[worker]; /* in seconds */
  724. }
  725. else
  726. {
  727. double idle = (end_time - hypervisor.sched_ctx_w[sched_ctx].idle_start_time[worker]) / 1000000.0; /* in seconds */
  728. idle_time = hypervisor.sched_ctx_w[sched_ctx].idle_time[worker] + idle;
  729. }
  730. norm_idle_time += (elapsed_time_worker[worker] == 0.0 ? 0.0 : (idle_time / elapsed_time_worker[worker]));
  731. /* printf("%d/%d: start time %lf elapsed time %lf idle time %lf norm_idle_time %lf \n", */
  732. /* worker, sched_ctx, hypervisor.sched_ctx_w[sched_ctx].start_time_w[worker], elapsed_time_worker[worker], idle_time, norm_idle_time); */
  733. }
  734. }
  735. double norm_exec_time = 0.0;
  736. for(worker = 0; worker < STARPU_NMAXWORKERS; worker++)
  737. {
  738. double exec_time = 0.0;
  739. if(hypervisor.sched_ctx_w[sched_ctx].start_time_w[worker] == 0.0)
  740. elapsed_time_worker[worker] = 0.0;
  741. else
  742. elapsed_time_worker[worker] = (end_time - hypervisor.sched_ctx_w[sched_ctx].start_time_w[worker]) / 1000000.0;
  743. if(hypervisor.sched_ctx_w[sched_ctx].exec_start_time[worker] == 0.0)
  744. {
  745. exec_time = hypervisor.sched_ctx_w[sched_ctx].exec_time[worker];
  746. }
  747. else
  748. {
  749. double current_exec_time = (end_time - hypervisor.sched_ctx_w[sched_ctx].exec_start_time[worker]) / 1000000.0; /* in seconds */
  750. exec_time = hypervisor.sched_ctx_w[sched_ctx].exec_time[worker] + current_exec_time;
  751. }
  752. norm_exec_time += elapsed_time_worker[worker] == 0.0 ? 0.0 : exec_time / elapsed_time_worker[worker];
  753. }
  754. double curr_time = starpu_timing_now();
  755. double elapsed_time = (curr_time - hypervisor.sched_ctx_w[sched_ctx].start_time) / 1000000.0; /* in seconds */
  756. int nready_tasks = starpu_get_nready_tasks_of_sched_ctx(sched_ctx);
  757. if(norm_idle_time >= 0.9)
  758. {
  759. config->max_nworkers = lrint(norm_exec_time);
  760. }
  761. else
  762. {
  763. if(norm_idle_time < 0.1)
  764. config->max_nworkers = lrint(norm_exec_time) + nready_tasks - 1; //workers->nworkers + hypervisor.sched_ctx_w[sched_ctx].nready_tasks - 1;
  765. else
  766. config->max_nworkers = lrint(norm_exec_time);
  767. }
  768. // config->max_nworkers = hypervisor.sched_ctx_w[sched_ctx].nready_tasks - 1;
  769. if(config->max_nworkers < 0)
  770. config->max_nworkers = 0;
  771. if(config->max_nworkers > max_cpus)
  772. config->max_nworkers = max_cpus;
  773. printf("%d: ready tasks %d idle for long %lf norm_idle_time %lf elapsed_time %lf norm_exec_time %lf nworker %d max %d \n",
  774. sched_ctx, nready_tasks, max_workers_idle_time[i], norm_idle_time, elapsed_time, norm_exec_time, workers->nworkers, config->max_nworkers);
  775. total_max_nworkers += config->max_nworkers;
  776. configured = 1;
  777. }
  778. /*if the sum of the max cpus is smaller than the total cpus available
  779. increase the max for the ones having more ready tasks to exec */
  780. if(configured && total_max_nworkers < max_cpus)
  781. {
  782. int diff = max_cpus - total_max_nworkers;
  783. int max_nready = -1;
  784. unsigned max_nready_sched_ctx = sched_ctxs[0];
  785. for(i = 0; i < nsched_ctxs; i++)
  786. {
  787. int nready_tasks = starpu_get_nready_tasks_of_sched_ctx(sched_ctxs[i]);
  788. if(max_nready < nready_tasks)
  789. {
  790. max_nready = nready_tasks;
  791. max_nready_sched_ctx = sched_ctxs[i];
  792. }
  793. }
  794. struct sc_hypervisor_policy_config *config = sc_hypervisor_get_config(max_nready_sched_ctx);
  795. config->max_nworkers += diff;
  796. printf("%d: redib max_nworkers incr %d \n", max_nready_sched_ctx, config->max_nworkers);
  797. }
  798. }
  799. /* notifies the hypervisor that a new task was pushed on the queue of the worker */
  800. static void notify_pushed_task(unsigned sched_ctx, int worker)
  801. {
  802. hypervisor.sched_ctx_w[sched_ctx].pushed_tasks[worker]++;
  803. if(hypervisor.sched_ctx_w[sched_ctx].total_flops != 0.0 && hypervisor.sched_ctx_w[sched_ctx].start_time == 0.0)
  804. hypervisor.sched_ctx_w[sched_ctx].start_time = starpu_timing_now();
  805. if(hypervisor.sched_ctx_w[sched_ctx].total_flops != 0.0 && hypervisor.sched_ctx_w[sched_ctx].start_time_w[worker] == 0.0)
  806. hypervisor.sched_ctx_w[sched_ctx].start_time_w[worker] = starpu_timing_now();
  807. if(hypervisor.sched_ctx_w[sched_ctx].total_flops != 0.0 && hypervisor.sched_ctx_w[sched_ctx].real_start_time == 0.0)
  808. hypervisor.sched_ctx_w[sched_ctx].real_start_time = starpu_timing_now();
  809. int ntasks = get_ntasks(hypervisor.sched_ctx_w[sched_ctx].pushed_tasks);
  810. if((hypervisor.min_tasks == 0 || (!(hypervisor.resize[sched_ctx] == 0 && imposed_resize) && ntasks == hypervisor.min_tasks)) && hypervisor.check_min_tasks[sched_ctx])
  811. {
  812. hypervisor.resize[sched_ctx] = 1;
  813. if(imposed_resize) imposed_resize = 0;
  814. hypervisor.check_min_tasks[sched_ctx] = 0;
  815. }
  816. if(hypervisor.policy.handle_pushed_task)
  817. hypervisor.policy.handle_pushed_task(sched_ctx, worker);
  818. }
  819. unsigned choose_ctx_to_steal(int worker)
  820. {
  821. int j;
  822. int ns = hypervisor.nsched_ctxs;
  823. int max_ready_tasks = 0;
  824. unsigned chosen_ctx = STARPU_NMAX_SCHED_CTXS;
  825. for(j = 0; j < ns; j++)
  826. {
  827. unsigned other_ctx = hypervisor.sched_ctxs[j];
  828. int nready = starpu_get_nready_tasks_of_sched_ctx(other_ctx);
  829. if(!starpu_sched_ctx_contains_worker(worker, other_ctx) && max_ready_tasks < nready)
  830. {
  831. max_ready_tasks = nready;
  832. chosen_ctx = other_ctx;
  833. }
  834. }
  835. return chosen_ctx;
  836. }
  837. /* notifies the hypervisor that the worker spent another cycle in idle time */
  838. static void notify_idle_cycle(unsigned sched_ctx, int worker, double idle_time)
  839. {
  840. struct sc_hypervisor_wrapper *sc_w = &hypervisor.sched_ctx_w[sched_ctx];
  841. sc_w->current_idle_time[worker] += idle_time;
  842. if(sc_w->idle_start_time[worker] == 0.0 && sc_w->hyp_react_start_time != 0.0)
  843. sc_w->idle_start_time[worker] = starpu_timing_now();
  844. if(sc_w->idle_start_time[worker] > 0.0)
  845. {
  846. double end_time = starpu_timing_now();
  847. sc_w->idle_time[worker] += (end_time - sc_w->idle_start_time[worker]) / 1000000.0; /* in seconds */
  848. }
  849. hypervisor.sched_ctx_w[sched_ctx].idle_start_time[worker] = starpu_timing_now();
  850. if(hypervisor.resize[sched_ctx] && hypervisor.policy.handle_idle_cycle)
  851. {
  852. if(sc_w->sched_ctx != STARPU_NMAX_SCHED_CTXS && sc_w->hyp_react_start_time != 0.0)
  853. {
  854. if(sc_hypervisor_check_idle(sched_ctx, worker))
  855. {
  856. int ret = starpu_pthread_mutex_trylock(&act_hypervisor_mutex);
  857. if(ret != EBUSY)
  858. {
  859. int j;
  860. int ns = hypervisor.nsched_ctxs;
  861. unsigned idle_everywhere = 1;
  862. for(j = 0; j < ns; j++)
  863. {
  864. if(starpu_sched_ctx_contains_worker(worker, hypervisor.sched_ctxs[j]))
  865. {
  866. if(!sc_hypervisor_check_idle(hypervisor.sched_ctxs[j], worker))
  867. idle_everywhere = 0;
  868. }
  869. }
  870. if(idle_everywhere)
  871. {
  872. unsigned other_ctx = choose_ctx_to_steal(worker);
  873. if(other_ctx != STARPU_NMAX_SCHED_CTXS)
  874. {
  875. sc_hypervisor_add_workers_to_sched_ctx(&worker, 1, other_ctx);
  876. starpu_sched_ctx_set_priority(&worker, 1, other_ctx, 0);
  877. _sc_hypervisor_allow_compute_idle(other_ctx, worker, 0);
  878. }
  879. }
  880. starpu_pthread_mutex_unlock(&act_hypervisor_mutex);
  881. }
  882. // hypervisor.policy.handle_idle_cycle(sched_ctx, worker);
  883. }
  884. }
  885. }
  886. return;
  887. }
  888. /* notifies the hypervisor that the worker is no longer idle and a new task was pushed on its queue */
  889. static void notify_poped_task(unsigned sched_ctx, int worker)
  890. {
  891. if(hypervisor.sched_ctx_w[sched_ctx].start_time_w[worker] == 0.0)
  892. hypervisor.sched_ctx_w[sched_ctx].start_time_w[worker] = starpu_timing_now();
  893. hypervisor.sched_ctx_w[sched_ctx].exec_start_time[worker] = starpu_timing_now();
  894. if(hypervisor.sched_ctx_w[sched_ctx].idle_start_time[worker] > 0.0)
  895. {
  896. int ns = hypervisor.nsched_ctxs;
  897. int j;
  898. for(j = 0; j < ns; j++)
  899. {
  900. if(hypervisor.sched_ctxs[j] != sched_ctx)
  901. {
  902. if(hypervisor.sched_ctx_w[hypervisor.sched_ctxs[j]].idle_start_time[worker] > 0.0)
  903. hypervisor.sched_ctx_w[hypervisor.sched_ctxs[j]].compute_partial_idle[worker] = 1;
  904. }
  905. }
  906. double end_time = starpu_timing_now();
  907. double idle = (end_time - hypervisor.sched_ctx_w[sched_ctx].idle_start_time[worker]) / 1000000.0; /* in seconds */
  908. if(hypervisor.sched_ctx_w[sched_ctx].compute_partial_idle[worker])
  909. hypervisor.sched_ctx_w[sched_ctx].idle_time[worker] += idle / 2.0;
  910. else
  911. hypervisor.sched_ctx_w[sched_ctx].idle_time[worker] += idle;
  912. hypervisor.sched_ctx_w[sched_ctx].compute_partial_idle[worker] = 0;
  913. hypervisor.sched_ctx_w[sched_ctx].idle_start_time[worker] = 0.0;
  914. }
  915. if(hypervisor.resize[sched_ctx])
  916. hypervisor.sched_ctx_w[sched_ctx].current_idle_time[worker] = 0.0;
  917. if(hypervisor.policy.handle_idle_end)
  918. hypervisor.policy.handle_idle_end(sched_ctx, worker);
  919. }
  920. /* notifies the hypervisor that a tagged task has just been executed */
  921. static void notify_post_exec_task(struct starpu_task *task, size_t data_size, uint32_t footprint, int task_tag, double flops)
  922. {
  923. unsigned sched_ctx = task->sched_ctx;
  924. int worker = starpu_worker_get_id();
  925. if(hypervisor.sched_ctx_w[sched_ctx].exec_start_time[worker] != 0.0)
  926. {
  927. double current_time = starpu_timing_now();
  928. hypervisor.sched_ctx_w[sched_ctx].exec_time[worker] += (current_time -
  929. hypervisor.sched_ctx_w[sched_ctx].exec_start_time[worker]) / 1000000.0; /* in seconds */
  930. hypervisor.sched_ctx_w[sched_ctx].exec_start_time[worker] = 0.0;
  931. }
  932. hypervisor.sched_ctx_w[sched_ctx].poped_tasks[worker]++;
  933. hypervisor.sched_ctx_w[sched_ctx].elapsed_flops[worker] += flops;
  934. hypervisor.sched_ctx_w[sched_ctx].elapsed_data[worker] += data_size ;
  935. hypervisor.sched_ctx_w[sched_ctx].elapsed_tasks[worker]++ ;
  936. hypervisor.sched_ctx_w[sched_ctx].total_elapsed_flops[worker] += flops;
  937. starpu_pthread_mutex_lock(&hypervisor.sched_ctx_w[sched_ctx].mutex);
  938. hypervisor.sched_ctx_w[sched_ctx].remaining_flops -= flops;
  939. if(_sc_hypervisor_use_lazy_resize())
  940. _ack_resize_completed(sched_ctx, worker);
  941. starpu_pthread_mutex_unlock(&hypervisor.sched_ctx_w[sched_ctx].mutex);
  942. if(hypervisor.resize[sched_ctx])
  943. {
  944. if(hypervisor.policy.handle_poped_task)
  945. {
  946. if(hypervisor.sched_ctx_w[sched_ctx].hyp_react_start_time == 0.0)
  947. hypervisor.sched_ctx_w[sched_ctx].hyp_react_start_time = starpu_timing_now();
  948. double curr_time = starpu_timing_now();
  949. double elapsed_time = (curr_time - hypervisor.sched_ctx_w[sched_ctx].hyp_react_start_time) / 1000000.0; /* in seconds */
  950. if(hypervisor.sched_ctx_w[sched_ctx].sched_ctx != STARPU_NMAX_SCHED_CTXS && elapsed_time > hypervisor.sched_ctx_w[sched_ctx].config->time_sample)
  951. {
  952. hypervisor.policy.handle_poped_task(sched_ctx, worker, task, footprint);
  953. hypervisor.sched_ctx_w[sched_ctx].hyp_react_start_time = starpu_timing_now();
  954. }
  955. }
  956. }
  957. /* starpu_pthread_mutex_lock(&act_hypervisor_mutex); */
  958. /* _ack_resize_completed(sched_ctx, worker); */
  959. /* starpu_pthread_mutex_unlock(&act_hypervisor_mutex); */
  960. if(hypervisor.sched_ctx_w[sched_ctx].poped_tasks[worker] % 200 == 0)
  961. _print_current_time();
  962. if(task_tag <= 0)
  963. return;
  964. unsigned conf_sched_ctx;
  965. unsigned i;
  966. unsigned ns = hypervisor.nsched_ctxs;
  967. for(i = 0; i < ns; i++)
  968. {
  969. struct configuration_entry *entry;
  970. conf_sched_ctx = hypervisor.sched_ctxs[i];
  971. starpu_pthread_mutex_lock(&hypervisor.conf_mut[conf_sched_ctx]);
  972. HASH_FIND_INT(hypervisor.configurations[conf_sched_ctx], &task_tag, entry);
  973. if (entry != NULL)
  974. {
  975. struct sc_hypervisor_policy_config *config = entry->configuration;
  976. sc_hypervisor_set_config(conf_sched_ctx, config);
  977. HASH_DEL(hypervisor.configurations[conf_sched_ctx], entry);
  978. free(config);
  979. }
  980. starpu_pthread_mutex_unlock(&hypervisor.conf_mut[conf_sched_ctx]);
  981. }
  982. if(hypervisor.resize[sched_ctx])
  983. {
  984. starpu_pthread_mutex_lock(&hypervisor.resize_mut[sched_ctx]);
  985. if(hypervisor.policy.handle_post_exec_hook)
  986. {
  987. /* Check whether 'task_tag' is in the 'resize_requests' set. */
  988. struct resize_request_entry *entry;
  989. HASH_FIND_INT(hypervisor.resize_requests[sched_ctx], &task_tag, entry);
  990. if (entry != NULL)
  991. {
  992. hypervisor.policy.handle_post_exec_hook(sched_ctx, task_tag);
  993. HASH_DEL(hypervisor.resize_requests[sched_ctx], entry);
  994. free(entry);
  995. }
  996. }
  997. starpu_pthread_mutex_unlock(&hypervisor.resize_mut[sched_ctx]);
  998. }
  999. return;
  1000. }
  1001. static void notify_submitted_job(struct starpu_task *task, uint32_t footprint, size_t data_size)
  1002. {
  1003. unsigned sched_ctx = task->sched_ctx;
  1004. starpu_pthread_mutex_lock(&hypervisor.sched_ctx_w[sched_ctx].mutex);
  1005. hypervisor.sched_ctx_w[sched_ctx].submitted_flops += task->flops;
  1006. starpu_pthread_mutex_unlock(&hypervisor.sched_ctx_w[sched_ctx].mutex);
  1007. if(hypervisor.policy.handle_submitted_job && !type_of_tasks_known)
  1008. hypervisor.policy.handle_submitted_job(task->cl, task->sched_ctx, footprint, data_size);
  1009. }
  1010. static void notify_empty_ctx(unsigned sched_ctx_id, struct starpu_task *task)
  1011. {
  1012. sc_hypervisor_resize_ctxs(NULL, -1 , NULL, -1);
  1013. }
  1014. void sc_hypervisor_set_type_of_task(struct starpu_codelet *cl, unsigned sched_ctx, uint32_t footprint, size_t data_size)
  1015. {
  1016. type_of_tasks_known = 1;
  1017. if(hypervisor.policy.handle_submitted_job)
  1018. hypervisor.policy.handle_submitted_job(cl, sched_ctx, footprint, data_size);
  1019. }
  1020. static void notify_delete_context(unsigned sched_ctx)
  1021. {
  1022. _print_current_time();
  1023. sc_hypervisor_unregister_ctx(sched_ctx);
  1024. }
  1025. void sc_hypervisor_size_ctxs(unsigned *sched_ctxs, int nsched_ctxs, int *workers, int nworkers)
  1026. {
  1027. // starpu_pthread_mutex_lock(&act_hypervisor_mutex);
  1028. unsigned curr_nsched_ctxs = sched_ctxs == NULL ? hypervisor.nsched_ctxs : (unsigned)nsched_ctxs;
  1029. unsigned *curr_sched_ctxs = sched_ctxs == NULL ? hypervisor.sched_ctxs : sched_ctxs;
  1030. // starpu_pthread_mutex_unlock(&act_hypervisor_mutex);
  1031. unsigned s;
  1032. for(s = 0; s < curr_nsched_ctxs; s++)
  1033. hypervisor.resize[curr_sched_ctxs[s]] = 1;
  1034. if(hypervisor.policy.size_ctxs)
  1035. hypervisor.policy.size_ctxs(curr_sched_ctxs, curr_nsched_ctxs, workers, nworkers);
  1036. }
  1037. struct sc_hypervisor_wrapper* sc_hypervisor_get_wrapper(unsigned sched_ctx)
  1038. {
  1039. return &hypervisor.sched_ctx_w[sched_ctx];
  1040. }
  1041. unsigned* sc_hypervisor_get_sched_ctxs()
  1042. {
  1043. return hypervisor.sched_ctxs;
  1044. }
  1045. int sc_hypervisor_get_nsched_ctxs()
  1046. {
  1047. int ns;
  1048. ns = hypervisor.nsched_ctxs;
  1049. return ns;
  1050. }
  1051. int _sc_hypervisor_use_lazy_resize(void)
  1052. {
  1053. char* lazy = getenv("SC_HYPERVISOR_LAZY_RESIZE");
  1054. return lazy ? atoi(lazy) : 1;
  1055. }
  1056. void sc_hypervisor_save_size_req(unsigned *sched_ctxs, int nsched_ctxs, int *workers, int nworkers)
  1057. {
  1058. hypervisor.sr = (struct size_request*)malloc(sizeof(struct size_request));
  1059. hypervisor.sr->sched_ctxs = sched_ctxs;
  1060. hypervisor.sr->nsched_ctxs = nsched_ctxs;
  1061. hypervisor.sr->workers = workers;
  1062. hypervisor.sr->nworkers = nworkers;
  1063. }
  1064. unsigned sc_hypervisor_get_size_req(unsigned **sched_ctxs, int* nsched_ctxs, int **workers, int *nworkers)
  1065. {
  1066. if(hypervisor.sr != NULL)
  1067. {
  1068. *sched_ctxs = hypervisor.sr->sched_ctxs;
  1069. *nsched_ctxs = hypervisor.sr->nsched_ctxs;
  1070. *workers = hypervisor.sr->workers;
  1071. *nworkers = hypervisor.sr->nworkers;
  1072. return 1;
  1073. }
  1074. return 0;
  1075. }
  1076. void sc_hypervisor_free_size_req(void)
  1077. {
  1078. if(hypervisor.sr != NULL)
  1079. {
  1080. free(hypervisor.sr);
  1081. hypervisor.sr = NULL;
  1082. }
  1083. }
  1084. double _get_optimal_v(unsigned sched_ctx)
  1085. {
  1086. return hypervisor.optimal_v[sched_ctx];
  1087. }
  1088. void _set_optimal_v(unsigned sched_ctx, double optimal_v)
  1089. {
  1090. hypervisor.optimal_v[sched_ctx] = optimal_v;
  1091. }
  1092. static struct types_of_workers* _init_structure_types_of_workers(void)
  1093. {
  1094. struct types_of_workers *tw = (struct types_of_workers*)malloc(sizeof(struct types_of_workers));
  1095. tw->ncpus = 0;
  1096. tw->ncuda = 0;
  1097. tw->nw = 0;
  1098. return tw;
  1099. }
  1100. struct types_of_workers* sc_hypervisor_get_types_of_workers(int *workers, unsigned nworkers)
  1101. {
  1102. struct types_of_workers *tw = _init_structure_types_of_workers();
  1103. unsigned w;
  1104. for(w = 0; w < nworkers; w++)
  1105. {
  1106. enum starpu_worker_archtype arch = workers == NULL ? starpu_worker_get_type((int)w) : starpu_worker_get_type(workers[w]);
  1107. if(arch == STARPU_CPU_WORKER)
  1108. tw->ncpus++;
  1109. if(arch == STARPU_CUDA_WORKER)
  1110. tw->ncuda++;
  1111. }
  1112. if(tw->ncpus > 0) tw->nw++;
  1113. if(tw->ncuda > 0) tw->nw++;
  1114. return tw;
  1115. }
  1116. void sc_hypervisor_update_diff_total_flops(unsigned sched_ctx, double diff_total_flops)
  1117. {
  1118. starpu_pthread_mutex_lock(&hypervisor.sched_ctx_w[sched_ctx].mutex);
  1119. hypervisor.sched_ctx_w[sched_ctx].total_flops += diff_total_flops;
  1120. hypervisor.sched_ctx_w[sched_ctx].remaining_flops += diff_total_flops;
  1121. starpu_pthread_mutex_unlock(&hypervisor.sched_ctx_w[sched_ctx].mutex);
  1122. }
  1123. void sc_hypervisor_update_diff_elapsed_flops(unsigned sched_ctx, double diff_elapsed_flops)
  1124. {
  1125. int workerid = starpu_worker_get_id();
  1126. if(workerid != -1)
  1127. {
  1128. // starpu_pthread_mutex_lock(&hypervisor.sched_ctx_w[sched_ctx].mutex);
  1129. hypervisor.sched_ctx_w[sched_ctx].elapsed_flops[workerid] += diff_elapsed_flops;
  1130. hypervisor.sched_ctx_w[sched_ctx].total_elapsed_flops[workerid] += diff_elapsed_flops;
  1131. // starpu_pthread_mutex_unlock(&hypervisor.sched_ctx_w[sched_ctx].mutex);
  1132. }
  1133. }