sc_hypervisor.c 56 KB

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