sc_hypervisor.c 34 KB

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