sc_hypervisor.c 41 KB

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