sc_hypervisor.c 45 KB

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