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. starpu_sched_ctx_set_priority(pus, npus, father, 1);
  334. starpu_sched_ctx_set_priority_on_level(pus, npus, father, 1);
  335. free(pus);
  336. unsigned i;
  337. for(i = 0; i < hypervisor.nsched_ctxs; i++)
  338. {
  339. if(hypervisor.sched_ctxs[i] == sched_ctx)
  340. {
  341. hypervisor.sched_ctxs[i] = STARPU_NMAX_SCHED_CTXS;
  342. break;
  343. }
  344. }
  345. _rearange_sched_ctxs(hypervisor.sched_ctxs, hypervisor.nsched_ctxs);
  346. hypervisor.nsched_ctxs--;
  347. hypervisor.sched_ctx_w[sched_ctx].sched_ctx = STARPU_NMAX_SCHED_CTXS;
  348. _remove_config(sched_ctx);
  349. starpu_pthread_mutex_destroy(&hypervisor.conf_mut[sched_ctx]);
  350. starpu_pthread_mutex_destroy(&hypervisor.resize_mut[sched_ctx]);
  351. if(hypervisor.nsched_ctxs == 1)
  352. sc_hypervisor_stop_resize(hypervisor.sched_ctxs[0]);
  353. starpu_pthread_mutex_unlock(&act_hypervisor_mutex);
  354. }
  355. double _get_max_speed_gap()
  356. {
  357. return hypervisor.max_speed_gap;
  358. }
  359. unsigned sc_hypervisor_get_resize_criteria()
  360. {
  361. return hypervisor.resize_criteria;
  362. }
  363. static int get_ntasks( int *tasks)
  364. {
  365. int ntasks = 0;
  366. int j;
  367. for(j = 0; j < STARPU_NMAXWORKERS; j++)
  368. {
  369. ntasks += tasks[j];
  370. }
  371. return ntasks;
  372. }
  373. int sc_hypervisor_get_nworkers_ctx(unsigned sched_ctx, enum starpu_worker_archtype arch)
  374. {
  375. int nworkers_ctx = 0;
  376. struct starpu_worker_collection *workers = starpu_sched_ctx_get_worker_collection(sched_ctx);
  377. int worker;
  378. struct starpu_sched_ctx_iterator it;
  379. if(workers->init_iterator)
  380. workers->init_iterator(workers, &it);
  381. while(workers->has_next(workers, &it))
  382. {
  383. worker = workers->get_next(workers, &it);
  384. enum starpu_worker_archtype curr_arch = starpu_worker_get_type(worker);
  385. if(curr_arch == arch || arch == STARPU_ANY_WORKER)
  386. nworkers_ctx++;
  387. }
  388. return nworkers_ctx;
  389. }
  390. static void _set_elapsed_flops_per_sched_ctx(unsigned sched_ctx, double val)
  391. {
  392. int i;
  393. for(i = 0; i < STARPU_NMAXWORKERS; i++)
  394. {
  395. hypervisor.sched_ctx_w[sched_ctx].elapsed_flops[i] = val;
  396. if(val == 0)
  397. {
  398. hypervisor.sched_ctx_w[sched_ctx].elapsed_data[i] = 0;
  399. hypervisor.sched_ctx_w[sched_ctx].elapsed_tasks[i] = 0;
  400. }
  401. }
  402. }
  403. double sc_hypervisor_get_elapsed_flops_per_sched_ctx(struct sc_hypervisor_wrapper* sc_w)
  404. {
  405. double ret_val = 0.0;
  406. struct starpu_worker_collection *workers = starpu_sched_ctx_get_worker_collection(sc_w->sched_ctx);
  407. int worker;
  408. struct starpu_sched_ctx_iterator it;
  409. if(workers->init_iterator)
  410. workers->init_iterator(workers, &it);
  411. while(workers->has_next(workers, &it))
  412. {
  413. worker = workers->get_next(workers, &it);
  414. ret_val += sc_w->elapsed_flops[worker];
  415. }
  416. return ret_val;
  417. }
  418. double sc_hypervisor_get_total_elapsed_flops_per_sched_ctx(struct sc_hypervisor_wrapper* sc_w)
  419. {
  420. double ret_val = 0.0;
  421. struct starpu_worker_collection *workers = starpu_sched_ctx_get_worker_collection(sc_w->sched_ctx);
  422. int worker;
  423. struct starpu_sched_ctx_iterator it;
  424. if(workers->init_iterator)
  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. if(workers->init_iterator)
  856. workers->init_iterator(workers, &it);
  857. double elapsed_time_worker[STARPU_NMAXWORKERS];
  858. double norm_idle_time = 0.0;
  859. double end_time = starpu_timing_now();
  860. while(workers->has_next(workers, &it))
  861. {
  862. double idle_time = 0.0;
  863. worker = workers->get_next(workers, &it);
  864. if(hypervisor.sched_ctx_w[sched_ctx].compute_idle[worker])
  865. {
  866. if(hypervisor.sched_ctx_w[sched_ctx].start_time_w[worker] == 0.0)
  867. elapsed_time_worker[worker] = 0.0;
  868. else
  869. elapsed_time_worker[worker] = (end_time - hypervisor.sched_ctx_w[sched_ctx].start_time_w[worker]) / 1000000.0;
  870. if(hypervisor.sched_ctx_w[sched_ctx].idle_start_time[worker] == 0.0)
  871. {
  872. idle_time = hypervisor.sched_ctx_w[sched_ctx].idle_time[worker]; /* in seconds */
  873. }
  874. else
  875. {
  876. double idle = (end_time - hypervisor.sched_ctx_w[sched_ctx].idle_start_time[worker]) / 1000000.0; /* in seconds */
  877. idle_time = hypervisor.sched_ctx_w[sched_ctx].idle_time[worker] + idle;
  878. }
  879. norm_idle_time += (elapsed_time_worker[worker] == 0.0 ? 0.0 : (idle_time / elapsed_time_worker[worker]));
  880. /* printf("%d/%d: start time %lf elapsed time %lf idle time %lf norm_idle_time %lf \n", */
  881. /* worker, sched_ctx, hypervisor.sched_ctx_w[sched_ctx].start_time_w[worker], elapsed_time_worker[worker], idle_time, norm_idle_time); */
  882. }
  883. }
  884. double norm_exec_time = 0.0;
  885. for(worker = 0; worker < STARPU_NMAXWORKERS; worker++)
  886. {
  887. double exec_time = 0.0;
  888. if(hypervisor.sched_ctx_w[sched_ctx].start_time_w[worker] == 0.0)
  889. elapsed_time_worker[worker] = 0.0;
  890. else
  891. elapsed_time_worker[worker] = (end_time - hypervisor.sched_ctx_w[sched_ctx].start_time_w[worker]) / 1000000.0;
  892. if(hypervisor.sched_ctx_w[sched_ctx].exec_start_time[worker] == 0.0)
  893. {
  894. exec_time = hypervisor.sched_ctx_w[sched_ctx].exec_time[worker];
  895. }
  896. else
  897. {
  898. double current_exec_time = 0.0;
  899. if(hypervisor.sched_ctx_w[sched_ctx].exec_start_time[worker] < hypervisor.sched_ctx_w[sched_ctx].start_time)
  900. current_exec_time = (end_time - hypervisor.sched_ctx_w[sched_ctx].start_time) / 1000000.0; /* in seconds */
  901. else
  902. current_exec_time = (end_time - hypervisor.sched_ctx_w[sched_ctx].exec_start_time[worker]) / 1000000.0; /* in seconds */
  903. exec_time = hypervisor.sched_ctx_w[sched_ctx].exec_time[worker] + current_exec_time;
  904. }
  905. norm_exec_time += elapsed_time_worker[worker] == 0.0 ? 0.0 : exec_time / elapsed_time_worker[worker];
  906. }
  907. double curr_time = starpu_timing_now();
  908. double elapsed_time = (curr_time - hypervisor.sched_ctx_w[sched_ctx].start_time) / 1000000.0; /* in seconds */
  909. int nready_tasks = starpu_sched_ctx_get_nready_tasks(sched_ctx);
  910. /* if(norm_idle_time >= 0.9) */
  911. /* { */
  912. /* config->max_nworkers = lrint(norm_exec_time); */
  913. /* } */
  914. /* else */
  915. /* { */
  916. /* if(norm_idle_time < 0.1) */
  917. /* config->max_nworkers = lrint(norm_exec_time) + nready_tasks - 1; //workers->nworkers + hypervisor.sched_ctx_w[sched_ctx].nready_tasks - 1; */
  918. /* else */
  919. /* config->max_nworkers = lrint(norm_exec_time); */
  920. /* } */
  921. config->max_nworkers = lrint(norm_exec_time);
  922. // config->max_nworkers = hypervisor.sched_ctx_w[sched_ctx].nready_tasks - 1;
  923. /* if(config->max_nworkers < 0) */
  924. /* config->max_nworkers = 0; */
  925. /* if(config->max_nworkers > max_workers) */
  926. /* config->max_nworkers = max_workers; */
  927. #ifdef STARPU_SC_HYPERVISOR_DEBUG
  928. printf("%d: ready tasks %d norm_idle_time %lf elapsed_time %lf norm_exec_time %lf nworker %d max %d \n",
  929. sched_ctx, nready_tasks, norm_idle_time, elapsed_time, norm_exec_time, workers->nworkers, config->max_nworkers);
  930. #endif
  931. total_max_nworkers += config->max_nworkers;
  932. configured = 1;
  933. }
  934. unsigned nhierarchy_levels = sc_hypervisor_get_nhierarchy_levels();
  935. if(nhierarchy_levels > 1 && configured)
  936. {
  937. unsigned *sched_ctxs2;
  938. int nsched_ctxs2;
  939. sc_hypervisor_get_ctxs_on_level(&sched_ctxs2, &nsched_ctxs2, 0, STARPU_NMAX_SCHED_CTXS);
  940. if(nsched_ctxs2 > 0)
  941. {
  942. _update_max_hierarchically(sched_ctxs2, nsched_ctxs2);
  943. int s;
  944. int current_total_max_nworkers = 0;
  945. double max_nflops = 0.0;
  946. unsigned max_nflops_sched_ctx = sched_ctxs2[0];
  947. for(s = 0; s < nsched_ctxs2; s++)
  948. {
  949. struct sc_hypervisor_policy_config *config = sc_hypervisor_get_config(sched_ctxs2[s]);
  950. current_total_max_nworkers += config->max_nworkers;
  951. if(max_nflops < hypervisor.sched_ctx_w[sched_ctxs2[s]].remaining_flops)
  952. {
  953. max_nflops = hypervisor.sched_ctx_w[sched_ctxs2[s]].remaining_flops;
  954. max_nflops_sched_ctx = sched_ctxs2[s];
  955. }
  956. }
  957. int max_possible_workers = starpu_worker_get_count();
  958. /*if the sum of the max cpus is smaller than the total cpus available
  959. increase the max for the ones having more ready tasks to exec */
  960. if(current_total_max_nworkers < max_possible_workers)
  961. {
  962. int diff = max_possible_workers - current_total_max_nworkers;
  963. struct sc_hypervisor_policy_config *config = sc_hypervisor_get_config(max_nflops_sched_ctx);
  964. config->max_nworkers += diff;
  965. #ifdef STARPU_SC_HYPERVISOR_DEBUG
  966. printf("%d: redib max_nworkers incr %d \n", max_nflops_sched_ctx, config->max_nworkers);
  967. #endif
  968. _update_max_diff_hierarchically(max_nflops_sched_ctx, diff);
  969. }
  970. }
  971. }
  972. /*if the sum of the max cpus is smaller than the total cpus available
  973. increase the max for the ones having more ready tasks to exec */
  974. /* if(configured && total_max_nworkers < max_workers) */
  975. /* { */
  976. /* int diff = max_workers - total_max_nworkers; */
  977. /* int max_nready = -1; */
  978. /* unsigned max_nready_sched_ctx = sched_ctxs[0]; */
  979. /* for(i = 0; i < nsched_ctxs; i++) */
  980. /* { */
  981. /* int nready_tasks = starpu_sched_ctx_get_nready_tasks(sched_ctxs[i]); */
  982. /* if(max_nready < nready_tasks) */
  983. /* { */
  984. /* max_nready = nready_tasks; */
  985. /* max_nready_sched_ctx = sched_ctxs[i]; */
  986. /* } */
  987. /* } */
  988. /* struct sc_hypervisor_policy_config *config = sc_hypervisor_get_config(max_nready_sched_ctx); */
  989. /* config->max_nworkers += diff; */
  990. /* printf("%d: redib max_nworkers incr %d \n", max_nready_sched_ctx, config->max_nworkers); */
  991. /* } */
  992. }
  993. /* notifies the hypervisor that a new task was pushed on the queue of the worker */
  994. static void notify_pushed_task(unsigned sched_ctx, int worker)
  995. {
  996. hypervisor.sched_ctx_w[sched_ctx].pushed_tasks[worker]++;
  997. if(hypervisor.sched_ctx_w[sched_ctx].total_flops != 0.0 && hypervisor.sched_ctx_w[sched_ctx].start_time == 0.0)
  998. hypervisor.sched_ctx_w[sched_ctx].start_time = starpu_timing_now();
  999. if(hypervisor.sched_ctx_w[sched_ctx].total_flops != 0.0 && hypervisor.sched_ctx_w[sched_ctx].start_time_w[worker] == 0.0)
  1000. {
  1001. hypervisor.sched_ctx_w[sched_ctx].start_time_w[worker] = starpu_timing_now();
  1002. }
  1003. int ntasks = get_ntasks(hypervisor.sched_ctx_w[sched_ctx].pushed_tasks);
  1004. if((hypervisor.min_tasks == 0 || (!(hypervisor.resize[sched_ctx] == 0 && imposed_resize) && ntasks == hypervisor.min_tasks)) && hypervisor.check_min_tasks[sched_ctx])
  1005. {
  1006. hypervisor.resize[sched_ctx] = 1;
  1007. if(imposed_resize) imposed_resize = 0;
  1008. hypervisor.check_min_tasks[sched_ctx] = 0;
  1009. }
  1010. if(hypervisor.policy.handle_pushed_task)
  1011. hypervisor.policy.handle_pushed_task(sched_ctx, worker);
  1012. }
  1013. unsigned choose_ctx_to_steal(int worker)
  1014. {
  1015. int j;
  1016. int ns = hypervisor.nsched_ctxs;
  1017. int max_ready_tasks = 0;
  1018. unsigned chosen_ctx = STARPU_NMAX_SCHED_CTXS;
  1019. for(j = 0; j < ns; j++)
  1020. {
  1021. unsigned other_ctx = hypervisor.sched_ctxs[j];
  1022. int nready = starpu_sched_ctx_get_nready_tasks(other_ctx);
  1023. if(!starpu_sched_ctx_contains_worker(worker, other_ctx) && max_ready_tasks < nready)
  1024. {
  1025. max_ready_tasks = nready;
  1026. chosen_ctx = other_ctx;
  1027. }
  1028. }
  1029. return chosen_ctx;
  1030. }
  1031. /* notifies the hypervisor that the worker spent another cycle in idle time */
  1032. static void notify_idle_cycle(unsigned sched_ctx, int worker, double idle_time)
  1033. {
  1034. struct sc_hypervisor_wrapper *sc_w = &hypervisor.sched_ctx_w[sched_ctx];
  1035. sc_w->current_idle_time[worker] += idle_time;
  1036. if(sc_w->idle_start_time[worker] == 0.0 && sc_w->hyp_react_start_time != 0.0)
  1037. sc_w->idle_start_time[worker] = starpu_timing_now();
  1038. if(sc_w->idle_start_time[worker] > 0.0)
  1039. {
  1040. double end_time = starpu_timing_now();
  1041. sc_w->idle_time[worker] += (end_time - sc_w->idle_start_time[worker]) / 1000000.0; /* in seconds */
  1042. }
  1043. hypervisor.sched_ctx_w[sched_ctx].idle_start_time[worker] = starpu_timing_now();
  1044. if(hypervisor.resize[sched_ctx] && hypervisor.policy.handle_idle_cycle)
  1045. {
  1046. if(sc_w->hyp_react_start_time == 0.0)
  1047. sc_w->hyp_react_start_time = starpu_timing_now();
  1048. double curr_time = starpu_timing_now();
  1049. double elapsed_time = (curr_time - sc_w->hyp_react_start_time) / 1000000.0; /* in seconds */
  1050. if(sc_w->sched_ctx != STARPU_NMAX_SCHED_CTXS && elapsed_time > sc_w->config->time_sample)
  1051. {
  1052. unsigned idle_everywhere = 0;
  1053. unsigned *sched_ctxs = NULL;
  1054. unsigned nsched_ctxs = 0;
  1055. int ret = starpu_pthread_mutex_trylock(&act_hypervisor_mutex);
  1056. if(ret != EBUSY)
  1057. {
  1058. if(sc_hypervisor_check_idle(sched_ctx, worker))
  1059. {
  1060. idle_everywhere = 1;
  1061. nsched_ctxs = starpu_worker_get_sched_ctx_list(worker, &sched_ctxs);
  1062. int s;
  1063. for(s = 0; s < nsched_ctxs; s++)
  1064. {
  1065. if(hypervisor.sched_ctx_w[sched_ctxs[s]].sched_ctx != STARPU_NMAX_SCHED_CTXS)
  1066. {
  1067. if(!sc_hypervisor_check_idle(sched_ctxs[s], worker))
  1068. idle_everywhere = 0;
  1069. }
  1070. }
  1071. free(sched_ctxs);
  1072. }
  1073. starpu_pthread_mutex_unlock(&act_hypervisor_mutex);
  1074. }
  1075. if(idle_everywhere)
  1076. {
  1077. double hyp_overhead_start = starpu_timing_now();
  1078. hypervisor.policy.handle_idle_cycle(sched_ctx, worker);
  1079. double hyp_overhead_end = starpu_timing_now();
  1080. hyp_overhead += (hyp_overhead_end - hyp_overhead_start);
  1081. }
  1082. sc_w->hyp_react_start_time = starpu_timing_now();
  1083. }
  1084. }
  1085. return;
  1086. }
  1087. void _update_real_start_time_hierarchically(unsigned sched_ctx)
  1088. {
  1089. hypervisor.sched_ctx_w[sched_ctx].real_start_time = starpu_timing_now();
  1090. if(starpu_sched_ctx_get_hierarchy_level(sched_ctx) > 0)
  1091. {
  1092. _update_real_start_time_hierarchically(starpu_sched_ctx_get_inheritor(sched_ctx));
  1093. }
  1094. return;
  1095. }
  1096. /* notifies the hypervisor that the worker is no longer idle and a new task was pushed on its queue */
  1097. static void notify_poped_task(unsigned sched_ctx, int worker)
  1098. {
  1099. if(hypervisor.sched_ctx_w[sched_ctx].total_flops != 0.0 && hypervisor.sched_ctx_w[sched_ctx].real_start_time == 0.0)
  1100. _update_real_start_time_hierarchically(sched_ctx);
  1101. if(hypervisor.sched_ctx_w[sched_ctx].start_time_w[worker] == 0.0)
  1102. {
  1103. hypervisor.sched_ctx_w[sched_ctx].start_time_w[worker] = starpu_timing_now();
  1104. }
  1105. hypervisor.sched_ctx_w[sched_ctx].exec_start_time[worker] = starpu_timing_now();
  1106. if(hypervisor.sched_ctx_w[sched_ctx].idle_start_time[worker] > 0.0)
  1107. {
  1108. int ns = hypervisor.nsched_ctxs;
  1109. int j;
  1110. for(j = 0; j < ns; j++)
  1111. {
  1112. if(hypervisor.sched_ctxs[j] != sched_ctx)
  1113. {
  1114. if(hypervisor.sched_ctx_w[hypervisor.sched_ctxs[j]].idle_start_time[worker] > 0.0)
  1115. hypervisor.sched_ctx_w[hypervisor.sched_ctxs[j]].compute_partial_idle[worker] = 1;
  1116. }
  1117. }
  1118. double end_time = starpu_timing_now();
  1119. double idle = (end_time - hypervisor.sched_ctx_w[sched_ctx].idle_start_time[worker]) / 1000000.0; /* in seconds */
  1120. if(hypervisor.sched_ctx_w[sched_ctx].compute_partial_idle[worker])
  1121. hypervisor.sched_ctx_w[sched_ctx].idle_time[worker] += idle / 2.0;
  1122. else
  1123. hypervisor.sched_ctx_w[sched_ctx].idle_time[worker] += idle;
  1124. hypervisor.sched_ctx_w[sched_ctx].compute_partial_idle[worker] = 0;
  1125. hypervisor.sched_ctx_w[sched_ctx].idle_start_time[worker] = 0.0;
  1126. }
  1127. if(hypervisor.resize[sched_ctx])
  1128. hypervisor.sched_ctx_w[sched_ctx].current_idle_time[worker] = 0.0;
  1129. if(hypervisor.policy.handle_idle_end)
  1130. hypervisor.policy.handle_idle_end(sched_ctx, worker);
  1131. }
  1132. static void _update_counters_hierarchically(int worker, unsigned sched_ctx, double flops, size_t data_size)
  1133. {
  1134. hypervisor.sched_ctx_w[sched_ctx].poped_tasks[worker]++;
  1135. hypervisor.sched_ctx_w[sched_ctx].elapsed_flops[worker] += flops;
  1136. hypervisor.sched_ctx_w[sched_ctx].elapsed_data[worker] += data_size ;
  1137. hypervisor.sched_ctx_w[sched_ctx].elapsed_tasks[worker]++ ;
  1138. hypervisor.sched_ctx_w[sched_ctx].total_elapsed_flops[worker] += flops;
  1139. starpu_pthread_mutex_lock(&hypervisor.sched_ctx_w[sched_ctx].mutex);
  1140. hypervisor.sched_ctx_w[sched_ctx].remaining_flops -= flops;
  1141. starpu_pthread_mutex_unlock(&hypervisor.sched_ctx_w[sched_ctx].mutex);
  1142. if(starpu_sched_ctx_get_hierarchy_level(sched_ctx) > 0)
  1143. _update_counters_hierarchically(worker, starpu_sched_ctx_get_inheritor(sched_ctx), flops, data_size);
  1144. return;
  1145. }
  1146. /* notifies the hypervisor that a tagged task has just been executed */
  1147. static void notify_post_exec_task(struct starpu_task *task, size_t data_size, uint32_t footprint, int task_tag, double flops)
  1148. {
  1149. unsigned sched_ctx = task->sched_ctx;
  1150. int worker = starpu_worker_get_id();
  1151. if(hypervisor.sched_ctx_w[sched_ctx].exec_start_time[worker] != 0.0)
  1152. {
  1153. double current_time = starpu_timing_now();
  1154. double exec_time = (current_time -
  1155. hypervisor.sched_ctx_w[sched_ctx].exec_start_time[worker]) / 1000000.0; /* in seconds */
  1156. hypervisor.sched_ctx_w[sched_ctx].exec_time[worker] += exec_time;
  1157. hypervisor.sched_ctx_w[sched_ctx].exec_start_time[worker] = 0.0;
  1158. }
  1159. hypervisor.sched_ctx_w[sched_ctx].poped_tasks[worker]++;
  1160. hypervisor.sched_ctx_w[sched_ctx].elapsed_flops[worker] += flops;
  1161. hypervisor.sched_ctx_w[sched_ctx].elapsed_data[worker] += data_size ;
  1162. hypervisor.sched_ctx_w[sched_ctx].elapsed_tasks[worker]++ ;
  1163. hypervisor.sched_ctx_w[sched_ctx].total_elapsed_flops[worker] += flops;
  1164. starpu_pthread_mutex_lock(&hypervisor.sched_ctx_w[sched_ctx].mutex);
  1165. hypervisor.sched_ctx_w[sched_ctx].remaining_flops -= flops;
  1166. if(_sc_hypervisor_use_lazy_resize())
  1167. _ack_resize_completed(sched_ctx, worker);
  1168. starpu_pthread_mutex_unlock(&hypervisor.sched_ctx_w[sched_ctx].mutex);
  1169. if(starpu_sched_ctx_get_hierarchy_level(sched_ctx) > 0)
  1170. {
  1171. _update_counters_hierarchically(worker, starpu_sched_ctx_get_inheritor(sched_ctx), flops, data_size);
  1172. }
  1173. if(hypervisor.resize[sched_ctx])
  1174. {
  1175. if(hypervisor.policy.handle_poped_task)
  1176. {
  1177. if(hypervisor.sched_ctx_w[sched_ctx].hyp_react_start_time == 0.0)
  1178. hypervisor.sched_ctx_w[sched_ctx].hyp_react_start_time = starpu_timing_now();
  1179. double curr_time = starpu_timing_now();
  1180. double elapsed_time = (curr_time - hypervisor.sched_ctx_w[sched_ctx].hyp_react_start_time) / 1000000.0; /* in seconds */
  1181. if(hypervisor.sched_ctx_w[sched_ctx].sched_ctx != STARPU_NMAX_SCHED_CTXS && elapsed_time > hypervisor.sched_ctx_w[sched_ctx].config->time_sample)
  1182. {
  1183. double hyp_overhead_start = starpu_timing_now();
  1184. hypervisor.policy.handle_poped_task(sched_ctx, worker, task, footprint);
  1185. double hyp_overhead_end = starpu_timing_now();
  1186. hyp_overhead += (hyp_overhead_end - hyp_overhead_start);
  1187. hypervisor.sched_ctx_w[sched_ctx].hyp_react_start_time = starpu_timing_now();
  1188. }
  1189. }
  1190. }
  1191. /* starpu_pthread_mutex_lock(&act_hypervisor_mutex); */
  1192. /* _ack_resize_completed(sched_ctx, worker); */
  1193. /* starpu_pthread_mutex_unlock(&act_hypervisor_mutex); */
  1194. if(hypervisor.sched_ctx_w[sched_ctx].poped_tasks[worker] % 200 == 0)
  1195. _print_current_time();
  1196. if(task_tag <= 0)
  1197. return;
  1198. unsigned conf_sched_ctx;
  1199. unsigned i;
  1200. unsigned ns = hypervisor.nsched_ctxs;
  1201. for(i = 0; i < ns; i++)
  1202. {
  1203. struct configuration_entry *entry;
  1204. conf_sched_ctx = hypervisor.sched_ctxs[i];
  1205. starpu_pthread_mutex_lock(&hypervisor.conf_mut[conf_sched_ctx]);
  1206. HASH_FIND_INT(hypervisor.configurations[conf_sched_ctx], &task_tag, entry);
  1207. if (entry != NULL)
  1208. {
  1209. struct sc_hypervisor_policy_config *config = entry->configuration;
  1210. sc_hypervisor_set_config(conf_sched_ctx, config);
  1211. HASH_DEL(hypervisor.configurations[conf_sched_ctx], entry);
  1212. free(config);
  1213. }
  1214. starpu_pthread_mutex_unlock(&hypervisor.conf_mut[conf_sched_ctx]);
  1215. }
  1216. if(hypervisor.resize[sched_ctx])
  1217. {
  1218. starpu_pthread_mutex_lock(&hypervisor.resize_mut[sched_ctx]);
  1219. if(hypervisor.policy.handle_post_exec_hook)
  1220. {
  1221. /* Check whether 'task_tag' is in the 'resize_requests' set. */
  1222. struct resize_request_entry *entry;
  1223. HASH_FIND_INT(hypervisor.resize_requests[sched_ctx], &task_tag, entry);
  1224. if (entry != NULL)
  1225. {
  1226. hypervisor.policy.handle_post_exec_hook(sched_ctx, task_tag);
  1227. HASH_DEL(hypervisor.resize_requests[sched_ctx], entry);
  1228. free(entry);
  1229. }
  1230. }
  1231. starpu_pthread_mutex_unlock(&hypervisor.resize_mut[sched_ctx]);
  1232. }
  1233. return;
  1234. }
  1235. static void notify_submitted_job(struct starpu_task *task, uint32_t footprint, size_t data_size)
  1236. {
  1237. unsigned sched_ctx = task->sched_ctx;
  1238. starpu_pthread_mutex_lock(&hypervisor.sched_ctx_w[sched_ctx].mutex);
  1239. hypervisor.sched_ctx_w[sched_ctx].submitted_flops += task->flops;
  1240. starpu_pthread_mutex_unlock(&hypervisor.sched_ctx_w[sched_ctx].mutex);
  1241. if(hypervisor.policy.handle_submitted_job && !type_of_tasks_known)
  1242. hypervisor.policy.handle_submitted_job(task->cl, task->sched_ctx, footprint, data_size);
  1243. }
  1244. static void notify_empty_ctx(unsigned sched_ctx_id, struct starpu_task *task)
  1245. {
  1246. sc_hypervisor_resize_ctxs(NULL, -1 , NULL, -1);
  1247. }
  1248. void sc_hypervisor_set_type_of_task(struct starpu_codelet *cl, unsigned sched_ctx, uint32_t footprint, size_t data_size)
  1249. {
  1250. type_of_tasks_known = 1;
  1251. if(hypervisor.policy.handle_submitted_job)
  1252. hypervisor.policy.handle_submitted_job(cl, sched_ctx, footprint, data_size);
  1253. }
  1254. static void notify_delete_context(unsigned sched_ctx)
  1255. {
  1256. _print_current_time();
  1257. sc_hypervisor_unregister_ctx(sched_ctx);
  1258. }
  1259. void sc_hypervisor_size_ctxs(unsigned *sched_ctxs, int nsched_ctxs, int *workers, int nworkers)
  1260. {
  1261. // starpu_pthread_mutex_lock(&act_hypervisor_mutex);
  1262. unsigned curr_nsched_ctxs = sched_ctxs == NULL ? hypervisor.nsched_ctxs : (unsigned)nsched_ctxs;
  1263. unsigned *curr_sched_ctxs = sched_ctxs == NULL ? hypervisor.sched_ctxs : sched_ctxs;
  1264. // starpu_pthread_mutex_unlock(&act_hypervisor_mutex);
  1265. unsigned s;
  1266. for(s = 0; s < curr_nsched_ctxs; s++)
  1267. hypervisor.resize[curr_sched_ctxs[s]] = 1;
  1268. if(hypervisor.policy.size_ctxs)
  1269. hypervisor.policy.size_ctxs(curr_sched_ctxs, curr_nsched_ctxs, workers, nworkers);
  1270. }
  1271. struct sc_hypervisor_wrapper* sc_hypervisor_get_wrapper(unsigned sched_ctx)
  1272. {
  1273. return &hypervisor.sched_ctx_w[sched_ctx];
  1274. }
  1275. unsigned* sc_hypervisor_get_sched_ctxs()
  1276. {
  1277. return hypervisor.sched_ctxs;
  1278. }
  1279. int sc_hypervisor_get_nsched_ctxs()
  1280. {
  1281. int ns;
  1282. ns = hypervisor.nsched_ctxs;
  1283. return ns;
  1284. }
  1285. int _sc_hypervisor_use_lazy_resize(void)
  1286. {
  1287. char* lazy = getenv("SC_HYPERVISOR_LAZY_RESIZE");
  1288. return lazy ? atoi(lazy) : 1;
  1289. }
  1290. void sc_hypervisor_save_size_req(unsigned *sched_ctxs, int nsched_ctxs, int *workers, int nworkers)
  1291. {
  1292. hypervisor.sr = (struct size_request*)malloc(sizeof(struct size_request));
  1293. hypervisor.sr->sched_ctxs = sched_ctxs;
  1294. hypervisor.sr->nsched_ctxs = nsched_ctxs;
  1295. hypervisor.sr->workers = workers;
  1296. hypervisor.sr->nworkers = nworkers;
  1297. }
  1298. unsigned sc_hypervisor_get_size_req(unsigned **sched_ctxs, int* nsched_ctxs, int **workers, int *nworkers)
  1299. {
  1300. if(hypervisor.sr != NULL)
  1301. {
  1302. *sched_ctxs = hypervisor.sr->sched_ctxs;
  1303. *nsched_ctxs = hypervisor.sr->nsched_ctxs;
  1304. *workers = hypervisor.sr->workers;
  1305. *nworkers = hypervisor.sr->nworkers;
  1306. return 1;
  1307. }
  1308. return 0;
  1309. }
  1310. void sc_hypervisor_free_size_req(void)
  1311. {
  1312. if(hypervisor.sr != NULL)
  1313. {
  1314. free(hypervisor.sr);
  1315. hypervisor.sr = NULL;
  1316. }
  1317. }
  1318. double _get_optimal_v(unsigned sched_ctx)
  1319. {
  1320. return hypervisor.optimal_v[sched_ctx];
  1321. }
  1322. void _set_optimal_v(unsigned sched_ctx, double optimal_v)
  1323. {
  1324. hypervisor.optimal_v[sched_ctx] = optimal_v;
  1325. }
  1326. static struct types_of_workers* _init_structure_types_of_workers(void)
  1327. {
  1328. struct types_of_workers *tw = (struct types_of_workers*)malloc(sizeof(struct types_of_workers));
  1329. tw->ncpus = 0;
  1330. tw->ncuda = 0;
  1331. tw->nw = 0;
  1332. return tw;
  1333. }
  1334. struct types_of_workers* sc_hypervisor_get_types_of_workers(int *workers, unsigned nworkers)
  1335. {
  1336. struct types_of_workers *tw = _init_structure_types_of_workers();
  1337. unsigned w;
  1338. for(w = 0; w < nworkers; w++)
  1339. {
  1340. enum starpu_worker_archtype arch = workers == NULL ? starpu_worker_get_type((int)w) : starpu_worker_get_type(workers[w]);
  1341. if(arch == STARPU_CPU_WORKER)
  1342. tw->ncpus++;
  1343. if(arch == STARPU_CUDA_WORKER)
  1344. tw->ncuda++;
  1345. }
  1346. if(tw->ncpus > 0) tw->nw++;
  1347. if(tw->ncuda > 0) tw->nw++;
  1348. return tw;
  1349. }
  1350. void sc_hypervisor_update_diff_total_flops(unsigned sched_ctx, double diff_total_flops)
  1351. {
  1352. // double hyp_overhead_start = starpu_timing_now();
  1353. starpu_pthread_mutex_lock(&hypervisor.sched_ctx_w[sched_ctx].mutex);
  1354. hypervisor.sched_ctx_w[sched_ctx].total_flops += diff_total_flops;
  1355. hypervisor.sched_ctx_w[sched_ctx].remaining_flops += diff_total_flops;
  1356. starpu_pthread_mutex_unlock(&hypervisor.sched_ctx_w[sched_ctx].mutex);
  1357. /* double hyp_overhead_end = starpu_timing_now(); */
  1358. /* hyp_overhead += (hyp_overhead_end - hyp_overhead_start); */
  1359. if(starpu_sched_ctx_get_hierarchy_level(sched_ctx) > 0)
  1360. sc_hypervisor_update_diff_total_flops(starpu_sched_ctx_get_inheritor(sched_ctx), diff_total_flops);
  1361. return;
  1362. }
  1363. void sc_hypervisor_update_diff_elapsed_flops(unsigned sched_ctx, double diff_elapsed_flops)
  1364. {
  1365. // double hyp_overhead_start = starpu_timing_now();
  1366. int workerid = starpu_worker_get_id();
  1367. if(workerid != -1)
  1368. {
  1369. // starpu_pthread_mutex_lock(&hypervisor.sched_ctx_w[sched_ctx].mutex);
  1370. hypervisor.sched_ctx_w[sched_ctx].elapsed_flops[workerid] += diff_elapsed_flops;
  1371. hypervisor.sched_ctx_w[sched_ctx].total_elapsed_flops[workerid] += diff_elapsed_flops;
  1372. // starpu_pthread_mutex_unlock(&hypervisor.sched_ctx_w[sched_ctx].mutex);
  1373. }
  1374. /* double hyp_overhead_end = starpu_timing_now(); */
  1375. /* hyp_overhead += (hyp_overhead_end - hyp_overhead_start); */
  1376. if(starpu_sched_ctx_get_hierarchy_level(sched_ctx) > 0)
  1377. sc_hypervisor_update_diff_elapsed_flops(starpu_sched_ctx_get_inheritor(sched_ctx), diff_elapsed_flops);
  1378. return;
  1379. }
  1380. void sc_hypervisor_get_ctxs_on_level(unsigned **sched_ctxs, int *nsched_ctxs, unsigned hierarchy_level, unsigned father_sched_ctx_id)
  1381. {
  1382. unsigned s;
  1383. *nsched_ctxs = 0;
  1384. *sched_ctxs = (unsigned*)malloc(hypervisor.nsched_ctxs * sizeof(unsigned));
  1385. for(s = 0; s < hypervisor.nsched_ctxs; s++)
  1386. {
  1387. /* if father == STARPU_NMAX_SCHED_CTXS we take all the ctxs in this level */
  1388. if(starpu_sched_ctx_get_hierarchy_level(hypervisor.sched_ctxs[s]) == hierarchy_level &&
  1389. (starpu_sched_ctx_get_inheritor(hypervisor.sched_ctxs[s]) == father_sched_ctx_id || father_sched_ctx_id == STARPU_NMAX_SCHED_CTXS))
  1390. (*sched_ctxs)[(*nsched_ctxs)++] = hypervisor.sched_ctxs[s];
  1391. }
  1392. if(*nsched_ctxs == 0)
  1393. free(*sched_ctxs);
  1394. return;
  1395. }
  1396. unsigned sc_hypervisor_get_nhierarchy_levels(void)
  1397. {
  1398. unsigned nlevels = 0;
  1399. unsigned level = 0;
  1400. unsigned levels[STARPU_NMAX_SCHED_CTXS];
  1401. unsigned s, l;
  1402. for(s = 0; s < hypervisor.nsched_ctxs; s++)
  1403. {
  1404. level = starpu_sched_ctx_get_hierarchy_level(hypervisor.sched_ctxs[s]);
  1405. unsigned found = 0;
  1406. for(l = 0; l < nlevels; l++)
  1407. if(levels[l] == level)
  1408. found = 1;
  1409. if(!found)
  1410. levels[nlevels++] = level;
  1411. }
  1412. return nlevels;
  1413. }
  1414. void sc_hypervisor_get_leaves(unsigned *sched_ctxs, int nsched_ctxs, unsigned *leaves, int *nleaves)
  1415. {
  1416. int s, s2;
  1417. for(s = 0; s < nsched_ctxs; s++)
  1418. {
  1419. unsigned is_someones_father = 0;
  1420. for(s2 = 0; s2 < nsched_ctxs; s2++)
  1421. {
  1422. unsigned father = starpu_sched_ctx_get_inheritor(sched_ctxs[s2]);
  1423. if(sched_ctxs[s] == father)
  1424. {
  1425. is_someones_father = 1;
  1426. break;
  1427. }
  1428. }
  1429. if(!is_someones_father)
  1430. leaves[(*nleaves)++] = sched_ctxs[s];
  1431. }
  1432. return;
  1433. }