sc_hypervisor.c 35 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071
  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_poped_task(unsigned sched_ctx, int worker, struct starpu_task *task, size_t data_size, uint32_t footprint);
  25. static void notify_post_exec_hook(unsigned sched_ctx, int taskid);
  26. static void notify_idle_end(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_ready_task(unsigned sched_ctx, struct starpu_task *task);
  29. static void notify_empty_ctx(unsigned sched_ctx, struct starpu_task *task);
  30. static void notify_delete_context(unsigned sched_ctx);
  31. extern struct sc_hypervisor_policy idle_policy;
  32. extern struct sc_hypervisor_policy app_driven_policy;
  33. extern struct sc_hypervisor_policy gflops_rate_policy;
  34. #ifdef STARPU_HAVE_GLPK_H
  35. extern struct sc_hypervisor_policy feft_lp_policy;
  36. extern struct sc_hypervisor_policy teft_lp_policy;
  37. extern struct sc_hypervisor_policy ispeed_lp_policy;
  38. extern struct sc_hypervisor_policy throughput_lp_policy;
  39. #endif // STARPU_HAVE_GLPK_
  40. extern struct sc_hypervisor_policy ispeed_policy;
  41. static struct sc_hypervisor_policy *predefined_policies[] =
  42. {
  43. &idle_policy,
  44. &app_driven_policy,
  45. #ifdef STARPU_HAVE_GLPK_H
  46. &feft_lp_policy,
  47. &teft_lp_policy,
  48. &ispeed_lp_policy,
  49. &throughput_lp_policy,
  50. #endif // STARPU_HAVE_GLPK_H
  51. &gflops_rate_policy,
  52. &ispeed_policy
  53. };
  54. static void _load_hypervisor_policy(struct sc_hypervisor_policy *policy)
  55. {
  56. STARPU_ASSERT(policy);
  57. hypervisor.policy.name = policy->name;
  58. hypervisor.policy.size_ctxs = policy->size_ctxs;
  59. hypervisor.policy.resize_ctxs = policy->resize_ctxs;
  60. hypervisor.policy.handle_poped_task = policy->handle_poped_task;
  61. hypervisor.policy.handle_pushed_task = policy->handle_pushed_task;
  62. hypervisor.policy.handle_idle_cycle = policy->handle_idle_cycle;
  63. hypervisor.policy.handle_idle_end = policy->handle_idle_end;
  64. hypervisor.policy.handle_post_exec_hook = policy->handle_post_exec_hook;
  65. hypervisor.policy.handle_submitted_job = policy->handle_submitted_job;
  66. hypervisor.policy.end_ctx = policy->end_ctx;
  67. }
  68. static struct sc_hypervisor_policy *_find_hypervisor_policy_from_name(const char *policy_name)
  69. {
  70. if (!policy_name)
  71. return NULL;
  72. unsigned i;
  73. for (i = 0; i < sizeof(predefined_policies)/sizeof(predefined_policies[0]); i++)
  74. {
  75. struct sc_hypervisor_policy *p;
  76. p = predefined_policies[i];
  77. if (p->name)
  78. {
  79. if (strcmp(policy_name, p->name) == 0) {
  80. /* we found a policy with the requested name */
  81. return p;
  82. }
  83. }
  84. }
  85. fprintf(stderr, "Warning: hypervisor policy \"%s\" was not found, try \"help\" to get a list\n", policy_name);
  86. /* nothing was found */
  87. return NULL;
  88. }
  89. static void display_sched_help_message(void)
  90. {
  91. const char* policy_name = getenv("SC_HYPERVISOR_POLICY");
  92. if (policy_name && (strcmp(policy_name, "help") == 0))
  93. {
  94. fprintf(stderr, "SC_HYPERVISOR_POLICY can be either of\n");
  95. /* display the description of all predefined policies */
  96. unsigned i;
  97. for (i = 0; i < sizeof(predefined_policies)/sizeof(predefined_policies[0]); i++)
  98. {
  99. struct sc_hypervisor_policy *p = predefined_policies[i];
  100. if (p->name)
  101. {
  102. fprintf(stderr, "%s\n", p->name);
  103. }
  104. }
  105. }
  106. }
  107. static struct sc_hypervisor_policy *_select_hypervisor_policy(struct sc_hypervisor_policy* hypervisor_policy)
  108. {
  109. struct sc_hypervisor_policy *selected_policy = NULL;
  110. if(hypervisor_policy && hypervisor_policy->custom)
  111. return hypervisor_policy;
  112. /* we look if the application specified the name of a policy to load */
  113. const char *policy_name;
  114. if (hypervisor_policy && hypervisor_policy->name)
  115. {
  116. policy_name = hypervisor_policy->name;
  117. }
  118. else
  119. {
  120. policy_name = getenv("SC_HYPERVISOR_POLICY");
  121. }
  122. if (policy_name)
  123. selected_policy = _find_hypervisor_policy_from_name(policy_name);
  124. /* Perhaps there was no policy that matched the name */
  125. if (selected_policy)
  126. return selected_policy;
  127. /* If no policy was specified, we use the idle policy as a default */
  128. return &idle_policy;
  129. }
  130. /* initializez the performance counters that starpu will use to retrive hints for resizing */
  131. void* sc_hypervisor_init(struct sc_hypervisor_policy *hypervisor_policy)
  132. {
  133. /* Perhaps we have to display some help */
  134. display_sched_help_message();
  135. hypervisor.min_tasks = 0;
  136. hypervisor.nsched_ctxs = 0;
  137. char* vel_gap = getenv("SC_HYPERVISOR_MAX_SPEED_GAP");
  138. hypervisor.max_speed_gap = vel_gap ? atof(vel_gap) : SC_SPEED_MAX_GAP_DEFAULT;
  139. char* crit = getenv("SC_HYPERVISOR_TRIGGER_RESIZE");
  140. hypervisor.resize_criteria = !crit ? SC_IDLE : strcmp(crit,"idle") == 0 ? SC_IDLE : (strcmp(crit,"speed") == 0 ? SC_SPEED : SC_NOTHING);
  141. starpu_pthread_mutex_init(&act_hypervisor_mutex, NULL);
  142. hypervisor.start_executing_time = starpu_timing_now();
  143. int i;
  144. for(i = 0; i < STARPU_NMAX_SCHED_CTXS; i++)
  145. {
  146. hypervisor.resize[i] = 0;
  147. hypervisor.allow_remove[i] = 1;
  148. hypervisor.configurations[i] = NULL;
  149. hypervisor.sr = NULL;
  150. hypervisor.check_min_tasks[i] = 1;
  151. hypervisor.sched_ctxs[i] = STARPU_NMAX_SCHED_CTXS;
  152. hypervisor.sched_ctx_w[i].sched_ctx = STARPU_NMAX_SCHED_CTXS;
  153. hypervisor.sched_ctx_w[i].config = NULL;
  154. hypervisor.sched_ctx_w[i].total_flops = 0.0;
  155. hypervisor.sched_ctx_w[i].submitted_flops = 0.0;
  156. hypervisor.sched_ctx_w[i].remaining_flops = 0.0;
  157. hypervisor.sched_ctx_w[i].start_time = 0.0;
  158. hypervisor.sched_ctx_w[i].real_start_time = 0.0;
  159. hypervisor.sched_ctx_w[i].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].ready_flops = 0.0;
  168. hypervisor.sched_ctx_w[i].total_flops_available = 0;
  169. hypervisor.sched_ctx_w[i].nready_tasks = 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].pushed_tasks[j] = 0;
  177. hypervisor.sched_ctx_w[i].poped_tasks[j] = 0;
  178. hypervisor.sched_ctx_w[i].elapsed_flops[j] = 0.0;
  179. hypervisor.sched_ctx_w[i].elapsed_data[j] = 0;
  180. hypervisor.sched_ctx_w[i].elapsed_tasks[j] = 0;
  181. hypervisor.sched_ctx_w[i].total_elapsed_flops[j] = 0.0;
  182. hypervisor.sched_ctx_w[i].worker_to_be_removed[j] = 0;
  183. }
  184. }
  185. struct sc_hypervisor_policy *selected_hypervisor_policy = _select_hypervisor_policy(hypervisor_policy);
  186. _load_hypervisor_policy(selected_hypervisor_policy);
  187. perf_counters = (struct starpu_sched_ctx_performance_counters*)malloc(sizeof(struct starpu_sched_ctx_performance_counters));
  188. perf_counters->notify_idle_cycle = notify_idle_cycle;
  189. perf_counters->notify_pushed_task = notify_pushed_task;
  190. perf_counters->notify_poped_task = notify_poped_task;
  191. perf_counters->notify_post_exec_hook = notify_post_exec_hook;
  192. perf_counters->notify_idle_end = notify_idle_end;
  193. perf_counters->notify_submitted_job = notify_submitted_job;
  194. perf_counters->notify_ready_task = notify_ready_task;
  195. perf_counters->notify_empty_ctx = notify_empty_ctx;
  196. perf_counters->notify_delete_context = notify_delete_context;
  197. starpu_sched_ctx_notify_hypervisor_exists();
  198. return (void*)perf_counters;
  199. }
  200. const char* sc_hypervisor_get_policy()
  201. {
  202. return hypervisor.policy.name;
  203. }
  204. /* the user can forbid the resizing process*/
  205. void sc_hypervisor_stop_resize(unsigned sched_ctx)
  206. {
  207. imposed_resize = 1;
  208. hypervisor.resize[sched_ctx] = 0;
  209. }
  210. /* the user can restart the resizing process*/
  211. void sc_hypervisor_start_resize(unsigned sched_ctx)
  212. {
  213. imposed_resize = 1;
  214. hypervisor.resize[sched_ctx] = 1;
  215. }
  216. static void _print_current_time()
  217. {
  218. if(!getenv("SC_HYPERVISOR_STOP_PRINT"))
  219. {
  220. double curr_time = starpu_timing_now();
  221. double elapsed_time = (curr_time - hypervisor.start_executing_time) / 1000000.0; /* in seconds */
  222. fprintf(stdout, "Time: %lf\n", elapsed_time);
  223. int i;
  224. for(i = 0; i < STARPU_NMAX_SCHED_CTXS; i++)
  225. {
  226. if(hypervisor.sched_ctxs[i] != STARPU_NMAX_SCHED_CTXS)
  227. {
  228. struct sc_hypervisor_wrapper *sc_w = &hypervisor.sched_ctx_w[hypervisor.sched_ctxs[i]];
  229. double cpu_speed = sc_hypervisor_get_speed(sc_w, STARPU_CPU_WORKER);
  230. double cuda_speed = sc_hypervisor_get_speed(sc_w, STARPU_CUDA_WORKER);
  231. int ncpus = sc_hypervisor_get_nworkers_ctx(sc_w->sched_ctx, STARPU_CPU_WORKER);
  232. int ncuda = sc_hypervisor_get_nworkers_ctx(sc_w->sched_ctx, STARPU_CUDA_WORKER);
  233. fprintf(stdout, "%d: cpu_v = %lf cuda_v = %lf ncpus = %d ncuda = %d\n", hypervisor.sched_ctxs[i], cpu_speed, cuda_speed, ncpus, ncuda);
  234. }
  235. }
  236. }
  237. return;
  238. }
  239. void sc_hypervisor_shutdown(void)
  240. {
  241. int i;
  242. for(i = 0; i < STARPU_NMAX_SCHED_CTXS; i++)
  243. {
  244. if(hypervisor.sched_ctxs[i] != STARPU_NMAX_SCHED_CTXS && hypervisor.nsched_ctxs > 0)
  245. {
  246. sc_hypervisor_stop_resize(hypervisor.sched_ctxs[i]);
  247. sc_hypervisor_unregister_ctx(hypervisor.sched_ctxs[i]);
  248. starpu_pthread_mutex_destroy(&hypervisor.sched_ctx_w[i].mutex);
  249. }
  250. }
  251. perf_counters->notify_idle_cycle = NULL;
  252. perf_counters->notify_pushed_task = NULL;
  253. perf_counters->notify_poped_task = NULL;
  254. perf_counters->notify_post_exec_hook = NULL;
  255. perf_counters->notify_idle_end = 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. if(strcmp(hypervisor.policy.name, "app_driven") == 0)
  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. int i;
  384. for(i = 0; i < STARPU_NMAXWORKERS; i++)
  385. ret_val += sc_w->elapsed_flops[i];
  386. return ret_val;
  387. }
  388. double sc_hypervisor_get_total_elapsed_flops_per_sched_ctx(struct sc_hypervisor_wrapper* sc_w)
  389. {
  390. double ret_val = 0.0;
  391. int i;
  392. for(i = 0; i < STARPU_NMAXWORKERS; i++)
  393. ret_val += sc_w->total_elapsed_flops[i];
  394. return ret_val;
  395. }
  396. static void _reset_idle_time(unsigned sched_ctx)
  397. {
  398. int i;
  399. for(i = 0; i < STARPU_NMAXWORKERS; i++)
  400. {
  401. hypervisor.sched_ctx_w[sched_ctx].idle_time[i] = 0.0;
  402. }
  403. return;
  404. }
  405. void _reset_resize_sample_info(unsigned sender_sched_ctx, unsigned receiver_sched_ctx)
  406. {
  407. double start_time = starpu_timing_now();
  408. if(sender_sched_ctx != STARPU_NMAX_SCHED_CTXS)
  409. {
  410. /* info concerning only the gflops_rate strateg */
  411. struct sc_hypervisor_wrapper *sender_sc_w = &hypervisor.sched_ctx_w[sender_sched_ctx];
  412. sender_sc_w->start_time = start_time;
  413. _set_elapsed_flops_per_sched_ctx(sender_sched_ctx, 0.0);
  414. _reset_idle_time(sender_sched_ctx);
  415. }
  416. if(receiver_sched_ctx != STARPU_NMAX_SCHED_CTXS)
  417. {
  418. struct sc_hypervisor_wrapper *receiver_sc_w = &hypervisor.sched_ctx_w[receiver_sched_ctx];
  419. receiver_sc_w->start_time = start_time;
  420. _set_elapsed_flops_per_sched_ctx(receiver_sched_ctx, 0.0);
  421. _reset_idle_time(receiver_sched_ctx);
  422. }
  423. }
  424. /* actually move the workers: the cpus are moved, gpus are only shared */
  425. /* forbids another resize request before this one is take into account */
  426. void sc_hypervisor_move_workers(unsigned sender_sched_ctx, unsigned receiver_sched_ctx, int* workers_to_move, unsigned nworkers_to_move, unsigned now)
  427. {
  428. if(nworkers_to_move > 0 && hypervisor.resize[sender_sched_ctx])
  429. {
  430. _print_current_time();
  431. unsigned j;
  432. printf("resize ctx %d with %d workers", sender_sched_ctx, nworkers_to_move);
  433. for(j = 0; j < nworkers_to_move; j++)
  434. printf(" %d", workers_to_move[j]);
  435. printf("\n");
  436. starpu_trace_user_event(1);
  437. hypervisor.allow_remove[receiver_sched_ctx] = 0;
  438. starpu_sched_ctx_add_workers(workers_to_move, nworkers_to_move, receiver_sched_ctx);
  439. if(now)
  440. {
  441. unsigned j;
  442. printf("remove now from ctx %d:", sender_sched_ctx);
  443. for(j = 0; j < nworkers_to_move; j++)
  444. printf(" %d", workers_to_move[j]);
  445. printf("\n");
  446. starpu_sched_ctx_remove_workers(workers_to_move, nworkers_to_move, sender_sched_ctx);
  447. hypervisor.allow_remove[receiver_sched_ctx] = 1;
  448. _reset_resize_sample_info(sender_sched_ctx, receiver_sched_ctx);
  449. }
  450. else
  451. {
  452. int ret = starpu_pthread_mutex_trylock(&hypervisor.sched_ctx_w[sender_sched_ctx].mutex);
  453. if(ret != EBUSY)
  454. {
  455. hypervisor.sched_ctx_w[sender_sched_ctx].resize_ack.receiver_sched_ctx = receiver_sched_ctx;
  456. hypervisor.sched_ctx_w[sender_sched_ctx].resize_ack.moved_workers = (int*)malloc(nworkers_to_move * sizeof(int));
  457. hypervisor.sched_ctx_w[sender_sched_ctx].resize_ack.nmoved_workers = nworkers_to_move;
  458. hypervisor.sched_ctx_w[sender_sched_ctx].resize_ack.acked_workers = (int*)malloc(nworkers_to_move * sizeof(int));
  459. unsigned i;
  460. for(i = 0; i < nworkers_to_move; i++)
  461. {
  462. hypervisor.sched_ctx_w[sender_sched_ctx].current_idle_time[workers_to_move[i]] = 0.0;
  463. hypervisor.sched_ctx_w[sender_sched_ctx].resize_ack.moved_workers[i] = workers_to_move[i];
  464. hypervisor.sched_ctx_w[sender_sched_ctx].resize_ack.acked_workers[i] = 0;
  465. }
  466. hypervisor.resize[sender_sched_ctx] = 0;
  467. if(imposed_resize) imposed_resize = 0;
  468. starpu_pthread_mutex_unlock(&hypervisor.sched_ctx_w[sender_sched_ctx].mutex);
  469. }
  470. }
  471. struct sc_hypervisor_policy_config *new_config = sc_hypervisor_get_config(receiver_sched_ctx);
  472. unsigned i;
  473. for(i = 0; i < nworkers_to_move; i++)
  474. 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;
  475. }
  476. return;
  477. }
  478. void sc_hypervisor_add_workers_to_sched_ctx(int* workers_to_add, unsigned nworkers_to_add, unsigned sched_ctx)
  479. {
  480. if(nworkers_to_add > 0 && hypervisor.resize[sched_ctx])
  481. {
  482. _print_current_time();
  483. unsigned j;
  484. printf("add to ctx %d:", sched_ctx);
  485. for(j = 0; j < nworkers_to_add; j++)
  486. printf(" %d", workers_to_add[j]);
  487. printf("\n");
  488. starpu_sched_ctx_add_workers(workers_to_add, nworkers_to_add, sched_ctx);
  489. struct sc_hypervisor_policy_config *new_config = sc_hypervisor_get_config(sched_ctx);
  490. unsigned i;
  491. for(i = 0; i < nworkers_to_add; i++)
  492. 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;
  493. _reset_resize_sample_info(STARPU_NMAX_SCHED_CTXS, sched_ctx);
  494. }
  495. return;
  496. }
  497. unsigned sc_hypervisor_can_resize(unsigned sched_ctx)
  498. {
  499. return hypervisor.resize[sched_ctx];
  500. }
  501. void sc_hypervisor_remove_workers_from_sched_ctx(int* workers_to_remove, unsigned nworkers_to_remove, unsigned sched_ctx, unsigned now)
  502. {
  503. if(nworkers_to_remove > 0 && hypervisor.resize[sched_ctx] && hypervisor.allow_remove[sched_ctx])
  504. {
  505. _print_current_time();
  506. unsigned nworkers = 0;
  507. int workers[nworkers_to_remove];
  508. if(now)
  509. {
  510. unsigned j;
  511. printf("remove explicitley now from ctx %d:", sched_ctx);
  512. for(j = 0; j < nworkers_to_remove; j++)
  513. printf(" %d", workers_to_remove[j]);
  514. printf("\n");
  515. starpu_sched_ctx_remove_workers(workers_to_remove, nworkers_to_remove, sched_ctx);
  516. _reset_resize_sample_info(sched_ctx, STARPU_NMAX_SCHED_CTXS);
  517. }
  518. else
  519. {
  520. printf("try to remove from ctx %d: ", sched_ctx);
  521. unsigned j;
  522. for(j = 0; j < nworkers_to_remove; j++)
  523. printf(" %d", workers_to_remove[j]);
  524. printf("\n");
  525. int ret = starpu_pthread_mutex_trylock(&hypervisor.sched_ctx_w[sched_ctx].mutex);
  526. if(ret != EBUSY)
  527. {
  528. unsigned i;
  529. for(i = 0; i < nworkers_to_remove; i++)
  530. if(starpu_sched_ctx_contains_worker(workers_to_remove[i], sched_ctx))
  531. workers[nworkers++] = workers_to_remove[i];
  532. hypervisor.sched_ctx_w[sched_ctx].resize_ack.receiver_sched_ctx = -1;
  533. hypervisor.sched_ctx_w[sched_ctx].resize_ack.moved_workers = (int*)malloc(nworkers_to_remove * sizeof(int));
  534. hypervisor.sched_ctx_w[sched_ctx].resize_ack.nmoved_workers = (int)nworkers;
  535. hypervisor.sched_ctx_w[sched_ctx].resize_ack.acked_workers = (int*)malloc(nworkers_to_remove * sizeof(int));
  536. for(i = 0; i < nworkers; i++)
  537. {
  538. hypervisor.sched_ctx_w[sched_ctx].current_idle_time[workers[i]] = 0.0;
  539. hypervisor.sched_ctx_w[sched_ctx].resize_ack.moved_workers[i] = workers[i];
  540. hypervisor.sched_ctx_w[sched_ctx].resize_ack.acked_workers[i] = 0;
  541. }
  542. hypervisor.resize[sched_ctx] = 0;
  543. if(imposed_resize) imposed_resize = 0;
  544. starpu_pthread_mutex_unlock(&hypervisor.sched_ctx_w[sched_ctx].mutex);
  545. }
  546. }
  547. }
  548. return;
  549. }
  550. static unsigned _ack_resize_completed(unsigned sched_ctx, int worker)
  551. {
  552. if(worker != -1 && !starpu_sched_ctx_contains_worker(worker, sched_ctx))
  553. return 0;
  554. struct sc_hypervisor_resize_ack *resize_ack = NULL;
  555. unsigned sender_sched_ctx = STARPU_NMAX_SCHED_CTXS;
  556. int i;
  557. for(i = 0; i < STARPU_NMAX_SCHED_CTXS; i++)
  558. {
  559. if(hypervisor.sched_ctxs[i] != STARPU_NMAX_SCHED_CTXS)
  560. {
  561. struct sc_hypervisor_wrapper *sc_w = &hypervisor.sched_ctx_w[hypervisor.sched_ctxs[i]];
  562. starpu_pthread_mutex_lock(&sc_w->mutex);
  563. unsigned only_remove = 0;
  564. if(sc_w->resize_ack.receiver_sched_ctx == -1 && hypervisor.sched_ctxs[i] != sched_ctx &&
  565. sc_w->resize_ack.nmoved_workers > 0 && starpu_sched_ctx_contains_worker(worker, hypervisor.sched_ctxs[i]))
  566. {
  567. int j;
  568. for(j = 0; j < sc_w->resize_ack.nmoved_workers; j++)
  569. if(sc_w->resize_ack.moved_workers[j] == worker)
  570. {
  571. only_remove = 1;
  572. _reset_resize_sample_info(sched_ctx, STARPU_NMAX_SCHED_CTXS);
  573. starpu_pthread_mutex_unlock(&sc_w->mutex);
  574. break;
  575. }
  576. }
  577. if(only_remove ||
  578. (sc_w->resize_ack.receiver_sched_ctx != -1 && sc_w->resize_ack.receiver_sched_ctx == (int)sched_ctx))
  579. {
  580. resize_ack = &sc_w->resize_ack;
  581. sender_sched_ctx = hypervisor.sched_ctxs[i];
  582. starpu_pthread_mutex_unlock(&sc_w->mutex);
  583. break;
  584. }
  585. starpu_pthread_mutex_unlock(&sc_w->mutex);
  586. }
  587. }
  588. /* if there is no ctx waiting for its ack return 1*/
  589. if(resize_ack == NULL)
  590. {
  591. return 1;
  592. }
  593. int ret = starpu_pthread_mutex_trylock(&hypervisor.sched_ctx_w[sender_sched_ctx].mutex);
  594. if(ret != EBUSY)
  595. {
  596. int *moved_workers = resize_ack->moved_workers;
  597. int nmoved_workers = resize_ack->nmoved_workers;
  598. int *acked_workers = resize_ack->acked_workers;
  599. if(worker != -1)
  600. {
  601. for(i = 0; i < nmoved_workers; i++)
  602. {
  603. int moved_worker = moved_workers[i];
  604. if(moved_worker == worker && acked_workers[i] == 0)
  605. {
  606. acked_workers[i] = 1;
  607. }
  608. }
  609. }
  610. int nacked_workers = 0;
  611. for(i = 0; i < nmoved_workers; i++)
  612. {
  613. nacked_workers += (acked_workers[i] == 1);
  614. }
  615. unsigned resize_completed = (nacked_workers == nmoved_workers);
  616. int receiver_sched_ctx = sched_ctx;
  617. if(resize_completed)
  618. {
  619. /* if the permission to resize is not allowed by the user don't do it
  620. whatever the application says */
  621. if(!((hypervisor.resize[sender_sched_ctx] == 0 || hypervisor.resize[receiver_sched_ctx] == 0) && imposed_resize))
  622. {
  623. /* int j; */
  624. /* printf("remove after ack from ctx %d:", sender_sched_ctx); */
  625. /* for(j = 0; j < nmoved_workers; j++) */
  626. /* printf(" %d", moved_workers[j]); */
  627. /* printf("\n"); */
  628. starpu_sched_ctx_remove_workers(moved_workers, nmoved_workers, sender_sched_ctx);
  629. _reset_resize_sample_info(sender_sched_ctx, receiver_sched_ctx);
  630. hypervisor.resize[sender_sched_ctx] = 1;
  631. hypervisor.allow_remove[receiver_sched_ctx] = 1;
  632. /* if the user allowed resizing leave the decisions to the application */
  633. if(imposed_resize) imposed_resize = 0;
  634. resize_ack->receiver_sched_ctx = -1;
  635. resize_ack->nmoved_workers = 0;
  636. free(resize_ack->moved_workers);
  637. free(resize_ack->acked_workers);
  638. }
  639. starpu_pthread_mutex_unlock(&hypervisor.sched_ctx_w[sender_sched_ctx].mutex);
  640. return resize_completed;
  641. }
  642. starpu_pthread_mutex_unlock(&hypervisor.sched_ctx_w[sender_sched_ctx].mutex);
  643. }
  644. return 0;
  645. }
  646. /* Enqueue a resize request for 'sched_ctx', to be executed when the
  647. * 'task_tag' tasks of 'sched_ctx' complete. */
  648. void sc_hypervisor_post_resize_request(unsigned sched_ctx, int task_tag)
  649. {
  650. struct resize_request_entry *entry;
  651. entry = malloc(sizeof *entry);
  652. STARPU_ASSERT(entry != NULL);
  653. entry->sched_ctx = sched_ctx;
  654. entry->task_tag = task_tag;
  655. starpu_pthread_mutex_lock(&hypervisor.resize_mut[sched_ctx]);
  656. HASH_ADD_INT(hypervisor.resize_requests[sched_ctx], task_tag, entry);
  657. starpu_pthread_mutex_unlock(&hypervisor.resize_mut[sched_ctx]);
  658. }
  659. void sc_hypervisor_resize_ctxs(unsigned *sched_ctxs, int nsched_ctxs , int *workers, int nworkers)
  660. {
  661. if(hypervisor.policy.resize_ctxs)
  662. hypervisor.policy.resize_ctxs(sched_ctxs, nsched_ctxs, workers, nworkers);
  663. }
  664. /* notifies the hypervisor that the worker is no longer idle and a new task was pushed on its queue */
  665. static void notify_idle_end(unsigned sched_ctx, int worker)
  666. {
  667. if(hypervisor.resize[sched_ctx])
  668. hypervisor.sched_ctx_w[sched_ctx].current_idle_time[worker] = 0.0;
  669. struct sc_hypervisor_wrapper *sc_w = &hypervisor.sched_ctx_w[sched_ctx];
  670. if(sc_w->idle_start_time[worker] != 0.0)
  671. {
  672. double end_time = starpu_timing_now();
  673. sc_w->idle_time[worker] += (end_time - sc_w->idle_start_time[worker]) / 1000000.0; /* in seconds */
  674. sc_w->idle_start_time[worker] = 0.0;
  675. }
  676. if(hypervisor.policy.handle_idle_end)
  677. hypervisor.policy.handle_idle_end(sched_ctx, worker);
  678. }
  679. /* notifies the hypervisor that the worker spent another cycle in idle time */
  680. static void notify_idle_cycle(unsigned sched_ctx, int worker, double idle_time)
  681. {
  682. if(hypervisor.resize[sched_ctx])
  683. {
  684. struct sc_hypervisor_wrapper *sc_w = &hypervisor.sched_ctx_w[sched_ctx];
  685. sc_w->current_idle_time[worker] += idle_time;
  686. if(sc_w->idle_start_time[worker] == 0.0)
  687. sc_w->idle_start_time[worker] = starpu_timing_now();
  688. if(hypervisor.policy.handle_idle_cycle)
  689. {
  690. hypervisor.policy.handle_idle_cycle(sched_ctx, worker);
  691. }
  692. }
  693. return;
  694. }
  695. /* notifies the hypervisor that a new task was pushed on the queue of the worker */
  696. static void notify_pushed_task(unsigned sched_ctx, int worker)
  697. {
  698. hypervisor.sched_ctx_w[sched_ctx].pushed_tasks[worker]++;
  699. if(hypervisor.sched_ctx_w[sched_ctx].total_flops != 0.0 && hypervisor.sched_ctx_w[sched_ctx].start_time == 0.0)
  700. hypervisor.sched_ctx_w[sched_ctx].start_time = starpu_timing_now();
  701. if(hypervisor.sched_ctx_w[sched_ctx].total_flops != 0.0 && hypervisor.sched_ctx_w[sched_ctx].real_start_time == 0.0)
  702. hypervisor.sched_ctx_w[sched_ctx].real_start_time = starpu_timing_now();
  703. int ntasks = get_ntasks(hypervisor.sched_ctx_w[sched_ctx].pushed_tasks);
  704. if((hypervisor.min_tasks == 0 || (!(hypervisor.resize[sched_ctx] == 0 && imposed_resize) && ntasks == hypervisor.min_tasks)) && hypervisor.check_min_tasks[sched_ctx])
  705. {
  706. hypervisor.resize[sched_ctx] = 1;
  707. if(imposed_resize) imposed_resize = 0;
  708. hypervisor.check_min_tasks[sched_ctx] = 0;
  709. }
  710. if(hypervisor.policy.handle_pushed_task)
  711. hypervisor.policy.handle_pushed_task(sched_ctx, worker);
  712. }
  713. /* notifies the hypervisor that a task was poped from the queue of the worker */
  714. static void notify_poped_task(unsigned sched_ctx, int worker, struct starpu_task *task, size_t data_size, uint32_t footprint)
  715. {
  716. hypervisor.sched_ctx_w[sched_ctx].poped_tasks[worker]++;
  717. hypervisor.sched_ctx_w[sched_ctx].elapsed_flops[worker] += task->flops;
  718. hypervisor.sched_ctx_w[sched_ctx].elapsed_data[worker] += data_size ;
  719. hypervisor.sched_ctx_w[sched_ctx].elapsed_tasks[worker]++ ;
  720. hypervisor.sched_ctx_w[sched_ctx].total_elapsed_flops[worker] += task->flops;
  721. starpu_pthread_mutex_lock(&act_hypervisor_mutex);
  722. hypervisor.sched_ctx_w[sched_ctx].remaining_flops -= task->flops;
  723. hypervisor.sched_ctx_w[sched_ctx].nready_tasks--;
  724. hypervisor.sched_ctx_w[sched_ctx].ready_flops -= task->flops;
  725. if(hypervisor.sched_ctx_w[sched_ctx].ready_flops < 0.0)
  726. hypervisor.sched_ctx_w[sched_ctx].ready_flops = 0.0;
  727. starpu_pthread_mutex_unlock(&act_hypervisor_mutex);
  728. if(hypervisor.resize[sched_ctx])
  729. {
  730. if(hypervisor.policy.handle_poped_task)
  731. hypervisor.policy.handle_poped_task(sched_ctx, worker, task, footprint);
  732. }
  733. starpu_pthread_mutex_lock(&act_hypervisor_mutex);
  734. _ack_resize_completed(sched_ctx, worker);
  735. starpu_pthread_mutex_unlock(&act_hypervisor_mutex);
  736. if(hypervisor.sched_ctx_w[sched_ctx].poped_tasks[worker] % 200 == 0)
  737. _print_current_time();
  738. }
  739. /* notifies the hypervisor that a tagged task has just been executed */
  740. static void notify_post_exec_hook(unsigned sched_ctx, int task_tag)
  741. {
  742. STARPU_ASSERT(task_tag > 0);
  743. unsigned conf_sched_ctx;
  744. unsigned i;
  745. starpu_pthread_mutex_lock(&act_hypervisor_mutex);
  746. unsigned ns = hypervisor.nsched_ctxs;
  747. starpu_pthread_mutex_unlock(&act_hypervisor_mutex);
  748. for(i = 0; i < ns; i++)
  749. {
  750. struct configuration_entry *entry;
  751. conf_sched_ctx = hypervisor.sched_ctxs[i];
  752. starpu_pthread_mutex_lock(&hypervisor.conf_mut[conf_sched_ctx]);
  753. HASH_FIND_INT(hypervisor.configurations[conf_sched_ctx], &task_tag, entry);
  754. if (entry != NULL)
  755. {
  756. struct sc_hypervisor_policy_config *config = entry->configuration;
  757. sc_hypervisor_set_config(conf_sched_ctx, config);
  758. HASH_DEL(hypervisor.configurations[conf_sched_ctx], entry);
  759. free(config);
  760. }
  761. starpu_pthread_mutex_unlock(&hypervisor.conf_mut[conf_sched_ctx]);
  762. }
  763. if(hypervisor.resize[sched_ctx])
  764. {
  765. starpu_pthread_mutex_lock(&hypervisor.resize_mut[sched_ctx]);
  766. if(hypervisor.policy.handle_post_exec_hook)
  767. {
  768. /* Check whether 'task_tag' is in the 'resize_requests' set. */
  769. struct resize_request_entry *entry;
  770. HASH_FIND_INT(hypervisor.resize_requests[sched_ctx], &task_tag, entry);
  771. if (entry != NULL)
  772. {
  773. hypervisor.policy.handle_post_exec_hook(sched_ctx, task_tag);
  774. HASH_DEL(hypervisor.resize_requests[sched_ctx], entry);
  775. free(entry);
  776. }
  777. }
  778. starpu_pthread_mutex_unlock(&hypervisor.resize_mut[sched_ctx]);
  779. }
  780. return;
  781. }
  782. static void notify_submitted_job(struct starpu_task *task, uint32_t footprint, size_t data_size)
  783. {
  784. starpu_pthread_mutex_lock(&act_hypervisor_mutex);
  785. hypervisor.sched_ctx_w[task->sched_ctx].submitted_flops += task->flops;
  786. starpu_pthread_mutex_unlock(&act_hypervisor_mutex);
  787. if(hypervisor.policy.handle_submitted_job && !type_of_tasks_known)
  788. hypervisor.policy.handle_submitted_job(task->cl, task->sched_ctx, footprint, data_size);
  789. }
  790. static void notify_ready_task(unsigned sched_ctx_id, struct starpu_task *task)
  791. {
  792. starpu_pthread_mutex_lock(&act_hypervisor_mutex);
  793. hypervisor.sched_ctx_w[sched_ctx_id].nready_tasks++;
  794. hypervisor.sched_ctx_w[sched_ctx_id].ready_flops += task->flops;
  795. starpu_pthread_mutex_unlock(&act_hypervisor_mutex);
  796. }
  797. static void notify_empty_ctx(unsigned sched_ctx_id, struct starpu_task *task)
  798. {
  799. sc_hypervisor_resize_ctxs(NULL, -1 , NULL, -1);
  800. }
  801. void sc_hypervisor_set_type_of_task(struct starpu_codelet *cl, unsigned sched_ctx, uint32_t footprint, size_t data_size)
  802. {
  803. type_of_tasks_known = 1;
  804. if(hypervisor.policy.handle_submitted_job)
  805. hypervisor.policy.handle_submitted_job(cl, sched_ctx, footprint, data_size);
  806. }
  807. static void notify_delete_context(unsigned sched_ctx)
  808. {
  809. _print_current_time();
  810. sc_hypervisor_unregister_ctx(sched_ctx);
  811. }
  812. void sc_hypervisor_size_ctxs(unsigned *sched_ctxs, int nsched_ctxs, int *workers, int nworkers)
  813. {
  814. starpu_pthread_mutex_lock(&act_hypervisor_mutex);
  815. unsigned curr_nsched_ctxs = sched_ctxs == NULL ? hypervisor.nsched_ctxs : (unsigned)nsched_ctxs;
  816. unsigned *curr_sched_ctxs = sched_ctxs == NULL ? hypervisor.sched_ctxs : sched_ctxs;
  817. starpu_pthread_mutex_unlock(&act_hypervisor_mutex);
  818. unsigned s;
  819. for(s = 0; s < curr_nsched_ctxs; s++)
  820. hypervisor.resize[curr_sched_ctxs[s]] = 1;
  821. if(hypervisor.policy.size_ctxs)
  822. hypervisor.policy.size_ctxs(curr_sched_ctxs, curr_nsched_ctxs, workers, nworkers);
  823. }
  824. struct sc_hypervisor_wrapper* sc_hypervisor_get_wrapper(unsigned sched_ctx)
  825. {
  826. return &hypervisor.sched_ctx_w[sched_ctx];
  827. }
  828. unsigned* sc_hypervisor_get_sched_ctxs()
  829. {
  830. return hypervisor.sched_ctxs;
  831. }
  832. int sc_hypervisor_get_nsched_ctxs()
  833. {
  834. int ns;
  835. ns = hypervisor.nsched_ctxs;
  836. return ns;
  837. }
  838. int _sc_hypervisor_use_lazy_resize(void)
  839. {
  840. char* lazy = getenv("SC_HYPERVISOR_LAZY_RESIZE");
  841. return lazy ? atoi(lazy) : 1;
  842. }
  843. void sc_hypervisor_save_size_req(unsigned *sched_ctxs, int nsched_ctxs, int *workers, int nworkers)
  844. {
  845. hypervisor.sr = (struct size_request*)malloc(sizeof(struct size_request));
  846. hypervisor.sr->sched_ctxs = sched_ctxs;
  847. hypervisor.sr->nsched_ctxs = nsched_ctxs;
  848. hypervisor.sr->workers = workers;
  849. hypervisor.sr->nworkers = nworkers;
  850. }
  851. unsigned sc_hypervisor_get_size_req(unsigned **sched_ctxs, int* nsched_ctxs, int **workers, int *nworkers)
  852. {
  853. if(hypervisor.sr != NULL)
  854. {
  855. *sched_ctxs = hypervisor.sr->sched_ctxs;
  856. *nsched_ctxs = hypervisor.sr->nsched_ctxs;
  857. *workers = hypervisor.sr->workers;
  858. *nworkers = hypervisor.sr->nworkers;
  859. return 1;
  860. }
  861. return 0;
  862. }
  863. void sc_hypervisor_free_size_req(void)
  864. {
  865. if(hypervisor.sr != NULL)
  866. {
  867. free(hypervisor.sr);
  868. hypervisor.sr = NULL;
  869. }
  870. }
  871. double _get_optimal_v(unsigned sched_ctx)
  872. {
  873. return hypervisor.optimal_v[sched_ctx];
  874. }
  875. void _set_optimal_v(unsigned sched_ctx, double optimal_v)
  876. {
  877. hypervisor.optimal_v[sched_ctx] = optimal_v;
  878. }
  879. static struct types_of_workers* _init_structure_types_of_workers(void)
  880. {
  881. struct types_of_workers *tw = (struct types_of_workers*)malloc(sizeof(struct types_of_workers));
  882. tw->ncpus = 0;
  883. tw->ncuda = 0;
  884. tw->nw = 0;
  885. return tw;
  886. }
  887. struct types_of_workers* sc_hypervisor_get_types_of_workers(int *workers, unsigned nworkers)
  888. {
  889. struct types_of_workers *tw = _init_structure_types_of_workers();
  890. unsigned w;
  891. for(w = 0; w < nworkers; w++)
  892. {
  893. enum starpu_worker_archtype arch = workers == NULL ? starpu_worker_get_type((int)w) : starpu_worker_get_type(workers[w]);
  894. if(arch == STARPU_CPU_WORKER)
  895. tw->ncpus++;
  896. if(arch == STARPU_CUDA_WORKER)
  897. tw->ncuda++;
  898. }
  899. if(tw->ncpus > 0) tw->nw++;
  900. if(tw->ncuda > 0) tw->nw++;
  901. return tw;
  902. }
  903. void sc_hypervisor_update_diff_total_flops(unsigned sched_ctx, double diff_total_flops)
  904. {
  905. starpu_pthread_mutex_lock(&act_hypervisor_mutex);
  906. hypervisor.sched_ctx_w[sched_ctx].total_flops += diff_total_flops;
  907. hypervisor.sched_ctx_w[sched_ctx].remaining_flops += diff_total_flops;
  908. starpu_pthread_mutex_unlock(&act_hypervisor_mutex);
  909. }
  910. void sc_hypervisor_update_diff_elapsed_flops(unsigned sched_ctx, double diff_elapsed_flops)
  911. {
  912. int workerid = starpu_worker_get_id();
  913. if(workerid != -1)
  914. {
  915. starpu_pthread_mutex_lock(&act_hypervisor_mutex);
  916. hypervisor.sched_ctx_w[sched_ctx].elapsed_flops[workerid] += diff_elapsed_flops;
  917. hypervisor.sched_ctx_w[sched_ctx].total_elapsed_flops[workerid] += diff_elapsed_flops;
  918. starpu_pthread_mutex_unlock(&act_hypervisor_mutex);
  919. }
  920. }