sc_hypervisor.c 55 KB

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