sched_ctx_hypervisor.c 27 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856
  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 <sched_ctx_hypervisor_intern.h>
  17. #include <common/uthash.h>
  18. #include <starpu_config.h>
  19. unsigned imposed_resize = 0;
  20. struct starpu_sched_ctx_performance_counters* perf_counters = NULL;
  21. static void notify_idle_cycle(unsigned sched_ctx, int worker, double idle_time);
  22. static void notify_pushed_task(unsigned sched_ctx, int worker);
  23. static void notify_poped_task(unsigned sched_ctx, int worker, double flops, size_t data_size);
  24. static void notify_post_exec_hook(unsigned sched_ctx, int taskid);
  25. static void notify_idle_end(unsigned sched_ctx, int worker);
  26. static void notify_submitted_job(struct starpu_task *task, unsigned footprint);
  27. extern struct sched_ctx_hypervisor_policy idle_policy;
  28. extern struct sched_ctx_hypervisor_policy app_driven_policy;
  29. extern struct sched_ctx_hypervisor_policy gflops_rate_policy;
  30. #ifdef STARPU_HAVE_GLPK_H
  31. extern struct sched_ctx_hypervisor_policy lp_policy;
  32. extern struct sched_ctx_hypervisor_policy lp2_policy;
  33. extern struct sched_ctx_hypervisor_policy ispeed_lp_policy;
  34. #endif // STARPU_HAVE_GLPK_
  35. extern struct sched_ctx_hypervisor_policy ispeed_policy;
  36. static struct sched_ctx_hypervisor_policy *predefined_policies[] =
  37. {
  38. &idle_policy,
  39. &app_driven_policy,
  40. #ifdef STARPU_HAVE_GLPK_H
  41. &lp_policy,
  42. &lp2_policy,
  43. &ispeed_lp_policy,
  44. #endif // STARPU_HAVE_GLPK_H
  45. &gflops_rate_policy,
  46. &ispeed_policy
  47. };
  48. static void _load_hypervisor_policy(struct sched_ctx_hypervisor_policy *policy)
  49. {
  50. STARPU_ASSERT(policy);
  51. hypervisor.policy.name = policy->name;
  52. hypervisor.policy.size_ctxs = policy->size_ctxs;
  53. hypervisor.policy.handle_poped_task = policy->handle_poped_task;
  54. hypervisor.policy.handle_pushed_task = policy->handle_pushed_task;
  55. hypervisor.policy.handle_idle_cycle = policy->handle_idle_cycle;
  56. hypervisor.policy.handle_idle_end = policy->handle_idle_end;
  57. hypervisor.policy.handle_post_exec_hook = policy->handle_post_exec_hook;
  58. hypervisor.policy.handle_submitted_job = policy->handle_submitted_job;
  59. hypervisor.policy.end_ctx = policy->end_ctx;
  60. }
  61. static struct sched_ctx_hypervisor_policy *_find_hypervisor_policy_from_name(const char *policy_name)
  62. {
  63. if (!policy_name)
  64. return NULL;
  65. unsigned i;
  66. for (i = 0; i < sizeof(predefined_policies)/sizeof(predefined_policies[0]); i++)
  67. {
  68. struct sched_ctx_hypervisor_policy *p;
  69. p = predefined_policies[i];
  70. if (p->name)
  71. {
  72. if (strcmp(policy_name, p->name) == 0) {
  73. /* we found a policy with the requested name */
  74. return p;
  75. }
  76. }
  77. }
  78. fprintf(stderr, "Warning: hypervisor policy \"%s\" was not found, try \"help\" to get a list\n", policy_name);
  79. /* nothing was found */
  80. return NULL;
  81. }
  82. static struct sched_ctx_hypervisor_policy *_select_hypervisor_policy(struct sched_ctx_hypervisor_policy* hypervisor_policy)
  83. {
  84. struct sched_ctx_hypervisor_policy *selected_policy = NULL;
  85. if(hypervisor_policy && hypervisor_policy->custom)
  86. return hypervisor_policy;
  87. /* we look if the application specified the name of a policy to load */
  88. const char *policy_name;
  89. if (hypervisor_policy && hypervisor_policy->name)
  90. {
  91. policy_name = hypervisor_policy->name;
  92. }
  93. else
  94. {
  95. policy_name = getenv("HYPERVISOR_POLICY");
  96. }
  97. if (policy_name)
  98. selected_policy = _find_hypervisor_policy_from_name(policy_name);
  99. /* Perhaps there was no policy that matched the name */
  100. if (selected_policy)
  101. return selected_policy;
  102. /* If no policy was specified, we use the idle policy as a default */
  103. return &idle_policy;
  104. }
  105. /* initializez the performance counters that starpu will use to retrive hints for resizing */
  106. struct starpu_sched_ctx_performance_counters* sched_ctx_hypervisor_init(struct sched_ctx_hypervisor_policy *hypervisor_policy)
  107. {
  108. hypervisor.min_tasks = 0;
  109. hypervisor.nsched_ctxs = 0;
  110. pthread_mutex_init(&act_hypervisor_mutex, NULL);
  111. hypervisor.start_executing_time = starpu_timing_now();
  112. int i;
  113. for(i = 0; i < STARPU_NMAX_SCHED_CTXS; i++)
  114. {
  115. hypervisor.resize[i] = 0;
  116. hypervisor.configurations[i] = NULL;
  117. hypervisor.sr = NULL;
  118. hypervisor.check_min_tasks[i] = 1;
  119. hypervisor.sched_ctxs[i] = STARPU_NMAX_SCHED_CTXS;
  120. hypervisor.sched_ctx_w[i].sched_ctx = STARPU_NMAX_SCHED_CTXS;
  121. hypervisor.sched_ctx_w[i].config = NULL;
  122. hypervisor.sched_ctx_w[i].total_flops = 0.0;
  123. hypervisor.sched_ctx_w[i].submitted_flops = 0.0;
  124. hypervisor.sched_ctx_w[i].remaining_flops = 0.0;
  125. hypervisor.sched_ctx_w[i].start_time = 0.0;
  126. hypervisor.sched_ctx_w[i].resize_ack.receiver_sched_ctx = -1;
  127. hypervisor.sched_ctx_w[i].resize_ack.moved_workers = NULL;
  128. hypervisor.sched_ctx_w[i].resize_ack.nmoved_workers = 0;
  129. hypervisor.sched_ctx_w[i].resize_ack.acked_workers = NULL;
  130. pthread_mutex_init(&hypervisor.sched_ctx_w[i].mutex, NULL);
  131. int j;
  132. for(j = 0; j < STARPU_NMAXWORKERS; j++)
  133. {
  134. hypervisor.sched_ctx_w[i].current_idle_time[j] = 0.0;
  135. hypervisor.sched_ctx_w[i].pushed_tasks[j] = 0;
  136. hypervisor.sched_ctx_w[i].poped_tasks[j] = 0;
  137. hypervisor.sched_ctx_w[i].elapsed_flops[j] = 0.0;
  138. hypervisor.sched_ctx_w[i].elapsed_data[j] = 0;
  139. hypervisor.sched_ctx_w[i].total_elapsed_flops[j] = 0.0;
  140. hypervisor.sched_ctx_w[i].worker_to_be_removed[j] = 0;
  141. hypervisor.sched_ctx_w[i].ref_velocity[j] = -1.0;
  142. }
  143. }
  144. struct sched_ctx_hypervisor_policy *selected_hypervisor_policy = _select_hypervisor_policy(hypervisor_policy);
  145. _load_hypervisor_policy(selected_hypervisor_policy);
  146. perf_counters = (struct starpu_sched_ctx_performance_counters*)malloc(sizeof(struct starpu_sched_ctx_performance_counters));
  147. perf_counters->notify_idle_cycle = notify_idle_cycle;
  148. perf_counters->notify_pushed_task = notify_pushed_task;
  149. perf_counters->notify_poped_task = notify_poped_task;
  150. perf_counters->notify_post_exec_hook = notify_post_exec_hook;
  151. perf_counters->notify_idle_end = notify_idle_end;
  152. perf_counters->notify_submitted_job = notify_submitted_job;
  153. starpu_sched_ctx_notify_hypervisor_exists();
  154. return perf_counters;
  155. }
  156. const char* sched_ctx_hypervisor_get_policy()
  157. {
  158. return hypervisor.policy.name;
  159. }
  160. /* the user can forbid the resizing process*/
  161. void sched_ctx_hypervisor_stop_resize(unsigned sched_ctx)
  162. {
  163. imposed_resize = 1;
  164. hypervisor.resize[sched_ctx] = 0;
  165. }
  166. /* the user can restart the resizing process*/
  167. void sched_ctx_hypervisor_start_resize(unsigned sched_ctx)
  168. {
  169. imposed_resize = 1;
  170. hypervisor.resize[sched_ctx] = 1;
  171. }
  172. void sched_ctx_hypervisor_shutdown(void)
  173. {
  174. printf("shutdown\n");
  175. int i;
  176. for(i = 0; i < STARPU_NMAX_SCHED_CTXS; i++)
  177. {
  178. if(hypervisor.sched_ctxs[i] != STARPU_NMAX_SCHED_CTXS && hypervisor.nsched_ctxs > 0)
  179. {
  180. sched_ctx_hypervisor_stop_resize(hypervisor.sched_ctxs[i]);
  181. sched_ctx_hypervisor_unregister_ctx(hypervisor.sched_ctxs[i]);
  182. pthread_mutex_destroy(&hypervisor.sched_ctx_w[i].mutex);
  183. }
  184. }
  185. perf_counters->notify_idle_cycle = NULL;
  186. perf_counters->notify_pushed_task = NULL;
  187. perf_counters->notify_poped_task = NULL;
  188. perf_counters->notify_post_exec_hook = NULL;
  189. perf_counters->notify_idle_end = NULL;
  190. free(perf_counters);
  191. perf_counters = NULL;
  192. pthread_mutex_destroy(&act_hypervisor_mutex);
  193. }
  194. /* the hypervisor is in charge only of the contexts registered to it*/
  195. void sched_ctx_hypervisor_register_ctx(unsigned sched_ctx, double total_flops)
  196. {
  197. pthread_mutex_lock(&act_hypervisor_mutex);
  198. hypervisor.configurations[sched_ctx] = NULL;
  199. hypervisor.resize_requests[sched_ctx] = NULL;
  200. pthread_mutex_init(&hypervisor.conf_mut[sched_ctx], NULL);
  201. pthread_mutex_init(&hypervisor.resize_mut[sched_ctx], NULL);
  202. _add_config(sched_ctx);
  203. hypervisor.sched_ctx_w[sched_ctx].sched_ctx = sched_ctx;
  204. hypervisor.sched_ctxs[hypervisor.nsched_ctxs++] = sched_ctx;
  205. hypervisor.sched_ctx_w[sched_ctx].total_flops = total_flops;
  206. hypervisor.sched_ctx_w[sched_ctx].remaining_flops = total_flops;
  207. if(strcmp(hypervisor.policy.name, "app_driven") == 0)
  208. hypervisor.resize[sched_ctx] = 1;
  209. pthread_mutex_unlock(&act_hypervisor_mutex);
  210. }
  211. static int _get_first_free_sched_ctx(int *sched_ctxs, unsigned nsched_ctxs)
  212. {
  213. int i;
  214. for(i = 0; i < nsched_ctxs; i++)
  215. if(sched_ctxs[i] == STARPU_NMAX_SCHED_CTXS)
  216. return i;
  217. return STARPU_NMAX_SCHED_CTXS;
  218. }
  219. /* rearange array of sched_ctxs in order not to have {MAXVAL, MAXVAL, 5, MAXVAL, 7}
  220. and have instead {5, 7, MAXVAL, MAXVAL, MAXVAL}
  221. it is easier afterwards to iterate the array
  222. */
  223. static void _rearange_sched_ctxs(int *sched_ctxs, int old_nsched_ctxs)
  224. {
  225. int first_free_id = STARPU_NMAX_SCHED_CTXS;
  226. int i;
  227. for(i = 0; i < old_nsched_ctxs; i++)
  228. {
  229. if(sched_ctxs[i] != STARPU_NMAX_SCHED_CTXS)
  230. {
  231. first_free_id = _get_first_free_sched_ctx(sched_ctxs, old_nsched_ctxs);
  232. if(first_free_id != STARPU_NMAX_SCHED_CTXS)
  233. {
  234. sched_ctxs[first_free_id] = sched_ctxs[i];
  235. sched_ctxs[i] = STARPU_NMAX_SCHED_CTXS;
  236. }
  237. }
  238. }
  239. }
  240. /* unregistered contexts will no longer be resized */
  241. void sched_ctx_hypervisor_unregister_ctx(unsigned sched_ctx)
  242. {
  243. if(hypervisor.policy.end_ctx)
  244. hypervisor.policy.end_ctx(sched_ctx);
  245. pthread_mutex_lock(&act_hypervisor_mutex);
  246. unsigned i;
  247. for(i = 0; i < hypervisor.nsched_ctxs; i++)
  248. {
  249. if(hypervisor.sched_ctxs[i] == sched_ctx)
  250. {
  251. hypervisor.sched_ctxs[i] = STARPU_NMAX_SCHED_CTXS;
  252. break;
  253. }
  254. }
  255. _rearange_sched_ctxs(hypervisor.sched_ctxs, hypervisor.nsched_ctxs);
  256. hypervisor.nsched_ctxs--;
  257. hypervisor.sched_ctx_w[sched_ctx].sched_ctx = STARPU_NMAX_SCHED_CTXS;
  258. _remove_config(sched_ctx);
  259. /* free(hypervisor.configurations[sched_ctx]); */
  260. /* free(hypervisor.resize_requests[sched_ctx]); */
  261. pthread_mutex_destroy(&hypervisor.conf_mut[sched_ctx]);
  262. pthread_mutex_destroy(&hypervisor.resize_mut[sched_ctx]);
  263. if(hypervisor.nsched_ctxs == 1)
  264. sched_ctx_hypervisor_stop_resize(hypervisor.sched_ctxs[0]);
  265. pthread_mutex_unlock(&act_hypervisor_mutex);
  266. }
  267. static void _print_current_time()
  268. {
  269. double curr_time = starpu_timing_now();
  270. double elapsed_time = (curr_time - hypervisor.start_executing_time) / 1000000.0; /* in seconds */
  271. printf("Time: %lf \n", elapsed_time);
  272. }
  273. static int get_ntasks( int *tasks)
  274. {
  275. int ntasks = 0;
  276. int j;
  277. for(j = 0; j < STARPU_NMAXWORKERS; j++)
  278. {
  279. ntasks += tasks[j];
  280. }
  281. return ntasks;
  282. }
  283. static void _get_cpus(int *workers, int nworkers, int *cpus, int *ncpus)
  284. {
  285. int i, worker;
  286. *ncpus = 0;
  287. for(i = 0; i < nworkers; i++)
  288. {
  289. worker = workers[i];
  290. enum starpu_archtype arch = starpu_worker_get_type(worker);
  291. if(arch == STARPU_CPU_WORKER)
  292. cpus[(*ncpus)++] = worker;
  293. }
  294. }
  295. int sched_ctx_hypervisor_get_nworkers_ctx(unsigned sched_ctx, enum starpu_archtype arch)
  296. {
  297. int nworkers_ctx = 0;
  298. struct starpu_sched_ctx_worker_collection *workers = starpu_sched_ctx_get_worker_collection(sched_ctx);
  299. int worker;
  300. struct starpu_sched_ctx_iterator it;
  301. if(workers->init_iterator)
  302. workers->init_iterator(workers, &it);
  303. while(workers->has_next(workers, &it))
  304. {
  305. worker = workers->get_next(workers, &it);
  306. enum starpu_archtype curr_arch = starpu_worker_get_type(worker);
  307. if(curr_arch == arch || arch == STARPU_ANY_WORKER)
  308. nworkers_ctx++;
  309. }
  310. return nworkers_ctx;
  311. }
  312. static void _set_elapsed_flops_per_sched_ctx(unsigned sched_ctx, double val)
  313. {
  314. int i;
  315. for(i = 0; i < STARPU_NMAXWORKERS; i++)
  316. {
  317. hypervisor.sched_ctx_w[sched_ctx].elapsed_flops[i] = val;
  318. if(val == 0)
  319. hypervisor.sched_ctx_w[sched_ctx].elapsed_data[i] = 0;
  320. }
  321. }
  322. double sched_ctx_hypervisor_get_elapsed_flops_per_sched_ctx(struct sched_ctx_hypervisor_wrapper* sc_w)
  323. {
  324. double ret_val = 0.0;
  325. int i;
  326. for(i = 0; i < STARPU_NMAXWORKERS; i++)
  327. ret_val += sc_w->elapsed_flops[i];
  328. return ret_val;
  329. }
  330. double sched_ctx_hypervisor_get_total_elapsed_flops_per_sched_ctx(struct sched_ctx_hypervisor_wrapper* sc_w)
  331. {
  332. double ret_val = 0.0;
  333. int i;
  334. for(i = 0; i < STARPU_NMAXWORKERS; i++)
  335. ret_val += sc_w->total_elapsed_flops[i];
  336. return ret_val;
  337. }
  338. void _reset_resize_sample_info(unsigned sender_sched_ctx, unsigned receiver_sched_ctx)
  339. {
  340. /* info concerning only the gflops_rate strateg */
  341. struct sched_ctx_hypervisor_wrapper *sender_sc_w = &hypervisor.sched_ctx_w[sender_sched_ctx];
  342. struct sched_ctx_hypervisor_wrapper *receiver_sc_w = &hypervisor.sched_ctx_w[receiver_sched_ctx];
  343. double start_time = starpu_timing_now();
  344. sender_sc_w->start_time = start_time;
  345. sender_sc_w->remaining_flops = sender_sc_w->remaining_flops - sched_ctx_hypervisor_get_elapsed_flops_per_sched_ctx(sender_sc_w);
  346. _set_elapsed_flops_per_sched_ctx(sender_sched_ctx, 0.0);
  347. receiver_sc_w->start_time = start_time;
  348. receiver_sc_w->remaining_flops = receiver_sc_w->remaining_flops - sched_ctx_hypervisor_get_elapsed_flops_per_sched_ctx(receiver_sc_w);
  349. _set_elapsed_flops_per_sched_ctx(receiver_sched_ctx, 0.0);
  350. }
  351. /* actually move the workers: the cpus are moved, gpus are only shared */
  352. /* forbids another resize request before this one is take into account */
  353. void sched_ctx_hypervisor_move_workers(unsigned sender_sched_ctx, unsigned receiver_sched_ctx, int* workers_to_move, unsigned nworkers_to_move, unsigned now)
  354. {
  355. if(nworkers_to_move > 0 && hypervisor.resize[sender_sched_ctx])// && hypervisor.resize[receiver_sched_ctx])
  356. {
  357. _print_current_time();
  358. int j;
  359. printf("resize ctx %d with", sender_sched_ctx);
  360. for(j = 0; j < nworkers_to_move; j++)
  361. printf(" %d", workers_to_move[j]);
  362. printf("\n");
  363. starpu_sched_ctx_add_workers(workers_to_move, nworkers_to_move, receiver_sched_ctx);
  364. if(now)
  365. {
  366. int j;
  367. printf("remove now from ctx %d:", sender_sched_ctx);
  368. for(j = 0; j < nworkers_to_move; j++)
  369. printf(" %d", workers_to_move[j]);
  370. printf("\n");
  371. starpu_sched_ctx_remove_workers(workers_to_move, nworkers_to_move, sender_sched_ctx);
  372. _reset_resize_sample_info(sender_sched_ctx, receiver_sched_ctx);
  373. }
  374. else
  375. {
  376. int ret = pthread_mutex_trylock(&hypervisor.sched_ctx_w[sender_sched_ctx].mutex);
  377. if(ret != EBUSY)
  378. {
  379. hypervisor.sched_ctx_w[sender_sched_ctx].resize_ack.receiver_sched_ctx = receiver_sched_ctx;
  380. hypervisor.sched_ctx_w[sender_sched_ctx].resize_ack.moved_workers = (int*)malloc(nworkers_to_move * sizeof(int));
  381. hypervisor.sched_ctx_w[sender_sched_ctx].resize_ack.nmoved_workers = nworkers_to_move;
  382. hypervisor.sched_ctx_w[sender_sched_ctx].resize_ack.acked_workers = (int*)malloc(nworkers_to_move * sizeof(int));
  383. int i;
  384. for(i = 0; i < nworkers_to_move; i++)
  385. {
  386. hypervisor.sched_ctx_w[sender_sched_ctx].current_idle_time[workers_to_move[i]] = 0.0;
  387. hypervisor.sched_ctx_w[sender_sched_ctx].resize_ack.moved_workers[i] = workers_to_move[i];
  388. hypervisor.sched_ctx_w[sender_sched_ctx].resize_ack.acked_workers[i] = 0;
  389. }
  390. hypervisor.resize[sender_sched_ctx] = 0;
  391. // hypervisor.resize[receiver_sched_ctx] = 0;
  392. pthread_mutex_unlock(&hypervisor.sched_ctx_w[sender_sched_ctx].mutex);
  393. }
  394. }
  395. struct sched_ctx_hypervisor_policy_config *new_config = sched_ctx_hypervisor_get_config(receiver_sched_ctx);
  396. int i;
  397. for(i = 0; i < nworkers_to_move; i++)
  398. 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;
  399. }
  400. return;
  401. }
  402. void sched_ctx_hypervisor_add_workers_to_sched_ctx(int* workers_to_add, unsigned nworkers_to_add, unsigned sched_ctx)
  403. {
  404. if(nworkers_to_add > 0 && hypervisor.resize[sched_ctx])
  405. {
  406. _print_current_time();
  407. int j;
  408. printf("add to ctx %d:", sched_ctx);
  409. for(j = 0; j < nworkers_to_add; j++)
  410. printf(" %d", workers_to_add[j]);
  411. printf("\n");
  412. starpu_sched_ctx_add_workers(workers_to_add, nworkers_to_add, sched_ctx);
  413. struct sched_ctx_hypervisor_policy_config *new_config = sched_ctx_hypervisor_get_config(sched_ctx);
  414. int i;
  415. for(i = 0; i < nworkers_to_add; i++)
  416. 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;
  417. }
  418. return;
  419. }
  420. unsigned sched_ctx_hypervisor_can_resize(unsigned sched_ctx)
  421. {
  422. return hypervisor.resize[sched_ctx];
  423. }
  424. void sched_ctx_hypervisor_remove_workers_from_sched_ctx(int* workers_to_remove, unsigned nworkers_to_remove, unsigned sched_ctx, unsigned now)
  425. {
  426. if(nworkers_to_remove > 0 && hypervisor.resize[sched_ctx])
  427. {
  428. _print_current_time();
  429. int nworkers=0;
  430. int workers[nworkers_to_remove];
  431. if(now)
  432. {
  433. int j;
  434. printf("remove explicitley now from ctx %d:", sched_ctx);
  435. for(j = 0; j < nworkers_to_remove; j++)
  436. printf(" %d", workers_to_remove[j]);
  437. printf("\n");
  438. starpu_sched_ctx_remove_workers(workers_to_remove, nworkers_to_remove, sched_ctx);
  439. }
  440. else
  441. {
  442. int ret = pthread_mutex_trylock(&hypervisor.sched_ctx_w[sched_ctx].mutex);
  443. if(ret != EBUSY)
  444. {
  445. int i;
  446. for(i = 0; i < nworkers_to_remove; i++)
  447. if(starpu_sched_ctx_contains_worker(workers_to_remove[i], sched_ctx))
  448. workers[nworkers++] = workers_to_remove[i];
  449. hypervisor.sched_ctx_w[sched_ctx].resize_ack.receiver_sched_ctx = -1;
  450. hypervisor.sched_ctx_w[sched_ctx].resize_ack.moved_workers = (int*)malloc(nworkers_to_remove * sizeof(int));
  451. hypervisor.sched_ctx_w[sched_ctx].resize_ack.nmoved_workers = nworkers;
  452. hypervisor.sched_ctx_w[sched_ctx].resize_ack.acked_workers = (int*)malloc(nworkers_to_remove * sizeof(int));
  453. for(i = 0; i < nworkers; i++)
  454. {
  455. hypervisor.sched_ctx_w[sched_ctx].current_idle_time[workers[i]] = 0.0;
  456. hypervisor.sched_ctx_w[sched_ctx].resize_ack.moved_workers[i] = workers[i];
  457. hypervisor.sched_ctx_w[sched_ctx].resize_ack.acked_workers[i] = 0;
  458. }
  459. hypervisor.resize[sched_ctx] = 0;
  460. pthread_mutex_unlock(&hypervisor.sched_ctx_w[sched_ctx].mutex);
  461. }
  462. }
  463. }
  464. return;
  465. }
  466. static unsigned _ack_resize_completed(unsigned sched_ctx, int worker)
  467. {
  468. if(worker != -1 && !starpu_sched_ctx_contains_worker(worker, sched_ctx))
  469. return 0;
  470. struct sched_ctx_hypervisor_resize_ack *resize_ack = NULL;
  471. unsigned sender_sched_ctx = STARPU_NMAX_SCHED_CTXS;
  472. int i;
  473. for(i = 0; i < STARPU_NMAX_SCHED_CTXS; i++)
  474. {
  475. if(hypervisor.sched_ctxs[i] != STARPU_NMAX_SCHED_CTXS)
  476. {
  477. struct sched_ctx_hypervisor_wrapper *sc_w = &hypervisor.sched_ctx_w[hypervisor.sched_ctxs[i]];
  478. pthread_mutex_lock(&sc_w->mutex);
  479. unsigned only_remove = 0;
  480. if(sc_w->resize_ack.receiver_sched_ctx == -1 && hypervisor.sched_ctxs[i] != sched_ctx &&
  481. sc_w->resize_ack.nmoved_workers > 0 && starpu_sched_ctx_contains_worker(worker, hypervisor.sched_ctxs[i]))
  482. {
  483. int j;
  484. for(j = 0; j < sc_w->resize_ack.nmoved_workers; j++)
  485. if(sc_w->resize_ack.moved_workers[j] == worker)
  486. {
  487. only_remove = 1;
  488. break;
  489. }
  490. }
  491. if(only_remove ||
  492. (sc_w->resize_ack.receiver_sched_ctx != -1 && sc_w->resize_ack.receiver_sched_ctx == sched_ctx))
  493. {
  494. resize_ack = &sc_w->resize_ack;
  495. sender_sched_ctx = hypervisor.sched_ctxs[i];
  496. pthread_mutex_unlock(&sc_w->mutex);
  497. break;
  498. }
  499. pthread_mutex_unlock(&sc_w->mutex);
  500. }
  501. }
  502. /* if there is no ctx waiting for its ack return 1*/
  503. if(resize_ack == NULL)
  504. return 1;
  505. int ret = pthread_mutex_trylock(&hypervisor.sched_ctx_w[sender_sched_ctx].mutex);
  506. if(ret != EBUSY)
  507. {
  508. int *moved_workers = resize_ack->moved_workers;
  509. int nmoved_workers = resize_ack->nmoved_workers;
  510. int *acked_workers = resize_ack->acked_workers;
  511. if(worker != -1)
  512. {
  513. for(i = 0; i < nmoved_workers; i++)
  514. {
  515. int moved_worker = moved_workers[i];
  516. if(moved_worker == worker && acked_workers[i] == 0)
  517. {
  518. acked_workers[i] = 1;
  519. }
  520. }
  521. }
  522. int nacked_workers = 0;
  523. for(i = 0; i < nmoved_workers; i++)
  524. {
  525. nacked_workers += (acked_workers[i] == 1);
  526. }
  527. unsigned resize_completed = (nacked_workers == nmoved_workers);
  528. int receiver_sched_ctx = sched_ctx;
  529. if(resize_completed)
  530. {
  531. /* if the permission to resize is not allowed by the user don't do it
  532. whatever the application says */
  533. if(!((hypervisor.resize[sender_sched_ctx] == 0 || hypervisor.resize[receiver_sched_ctx] == 0) && imposed_resize))
  534. {
  535. /* int j; */
  536. /* printf("remove after ack from ctx %d:", sender_sched_ctx); */
  537. /* for(j = 0; j < nmoved_workers; j++) */
  538. /* printf(" %d", moved_workers[j]); */
  539. /* printf("\n"); */
  540. starpu_sched_ctx_remove_workers(moved_workers, nmoved_workers, sender_sched_ctx);
  541. _reset_resize_sample_info(sender_sched_ctx, receiver_sched_ctx);
  542. hypervisor.resize[sender_sched_ctx] = 1;
  543. // hypervisor.resize[receiver_sched_ctx] = 1;
  544. /* if the user allowed resizing leave the decisions to the application */
  545. if(imposed_resize) imposed_resize = 0;
  546. resize_ack->receiver_sched_ctx = -1;
  547. resize_ack->nmoved_workers = 0;
  548. free(resize_ack->moved_workers);
  549. free(resize_ack->acked_workers);
  550. }
  551. pthread_mutex_unlock(&hypervisor.sched_ctx_w[sender_sched_ctx].mutex);
  552. return resize_completed;
  553. }
  554. pthread_mutex_unlock(&hypervisor.sched_ctx_w[sender_sched_ctx].mutex);
  555. }
  556. return 0;
  557. }
  558. /* Enqueue a resize request for 'sched_ctx', to be executed when the
  559. * 'task_tag' tasks of 'sched_ctx' complete. */
  560. void sched_ctx_hypervisor_resize(unsigned sched_ctx, int task_tag)
  561. {
  562. struct resize_request_entry *entry;
  563. entry = malloc(sizeof *entry);
  564. STARPU_ASSERT(entry != NULL);
  565. entry->sched_ctx = sched_ctx;
  566. entry->task_tag = task_tag;
  567. pthread_mutex_lock(&hypervisor.resize_mut[sched_ctx]);
  568. HASH_ADD_INT(hypervisor.resize_requests[sched_ctx], task_tag, entry);
  569. pthread_mutex_unlock(&hypervisor.resize_mut[sched_ctx]);
  570. }
  571. /* notifies the hypervisor that the worker is no longer idle and a new task was pushed on its queue */
  572. static void notify_idle_end(unsigned sched_ctx, int worker)
  573. {
  574. if(hypervisor.resize[sched_ctx])
  575. hypervisor.sched_ctx_w[sched_ctx].current_idle_time[worker] = 0.0;
  576. if(hypervisor.policy.handle_idle_end)
  577. hypervisor.policy.handle_idle_end(sched_ctx, worker);
  578. }
  579. /* notifies the hypervisor that the worker spent another cycle in idle time */
  580. static void notify_idle_cycle(unsigned sched_ctx, int worker, double idle_time)
  581. {
  582. if(hypervisor.resize[sched_ctx])
  583. {
  584. struct sched_ctx_hypervisor_wrapper *sc_w = &hypervisor.sched_ctx_w[sched_ctx];
  585. sc_w->current_idle_time[worker] += idle_time;
  586. if(hypervisor.policy.handle_idle_cycle)
  587. {
  588. hypervisor.policy.handle_idle_cycle(sched_ctx, worker);
  589. }
  590. }
  591. return;
  592. }
  593. /* notifies the hypervisor that a new task was pushed on the queue of the worker */
  594. static void notify_pushed_task(unsigned sched_ctx, int worker)
  595. {
  596. hypervisor.sched_ctx_w[sched_ctx].pushed_tasks[worker]++;
  597. if(hypervisor.sched_ctx_w[sched_ctx].total_flops != 0.0 && hypervisor.sched_ctx_w[sched_ctx].start_time == 0.0)
  598. hypervisor.sched_ctx_w[sched_ctx].start_time = starpu_timing_now();
  599. int ntasks = get_ntasks(hypervisor.sched_ctx_w[sched_ctx].pushed_tasks);
  600. if((hypervisor.min_tasks == 0 || (!(hypervisor.resize[sched_ctx] == 0 && imposed_resize) && ntasks == hypervisor.min_tasks)) && hypervisor.check_min_tasks[sched_ctx])
  601. {
  602. hypervisor.resize[sched_ctx] = 1;
  603. if(imposed_resize) imposed_resize = 0;
  604. hypervisor.check_min_tasks[sched_ctx] = 0;
  605. }
  606. if(hypervisor.policy.handle_pushed_task)
  607. hypervisor.policy.handle_pushed_task(sched_ctx, worker);
  608. }
  609. /* notifies the hypervisor that a task was poped from the queue of the worker */
  610. static void notify_poped_task(unsigned sched_ctx, int worker, double elapsed_flops, size_t data_size)
  611. {
  612. hypervisor.sched_ctx_w[sched_ctx].poped_tasks[worker]++;
  613. hypervisor.sched_ctx_w[sched_ctx].elapsed_flops[worker] += elapsed_flops;
  614. hypervisor.sched_ctx_w[sched_ctx].elapsed_data[worker] += data_size ;
  615. hypervisor.sched_ctx_w[sched_ctx].total_elapsed_flops[worker] += elapsed_flops;
  616. hypervisor.sched_ctx_w[sched_ctx].remaining_flops -= elapsed_flops; //sched_ctx_hypervisor_get_elapsed_flops_per_sched_ctx(&hypervisor.sched_ctx_w[sched_ctx]);
  617. if(hypervisor.resize[sched_ctx])
  618. {
  619. if(hypervisor.policy.handle_poped_task)
  620. hypervisor.policy.handle_poped_task(sched_ctx, worker);
  621. }
  622. _ack_resize_completed(sched_ctx, worker);
  623. }
  624. /* notifies the hypervisor that a tagged task has just been executed */
  625. static void notify_post_exec_hook(unsigned sched_ctx, int task_tag)
  626. {
  627. STARPU_ASSERT(task_tag > 0);
  628. unsigned conf_sched_ctx;
  629. int i;
  630. pthread_mutex_lock(&act_hypervisor_mutex);
  631. unsigned ns = hypervisor.nsched_ctxs;
  632. pthread_mutex_unlock(&act_hypervisor_mutex);
  633. for(i = 0; i < ns; i++)
  634. {
  635. struct configuration_entry *entry;
  636. conf_sched_ctx = hypervisor.sched_ctxs[i];
  637. pthread_mutex_lock(&hypervisor.conf_mut[conf_sched_ctx]);
  638. HASH_FIND_INT(hypervisor.configurations[conf_sched_ctx], &task_tag, entry);
  639. if (entry != NULL)
  640. {
  641. struct sched_ctx_hypervisor_policy_config *config = entry->configuration;
  642. sched_ctx_hypervisor_set_config(conf_sched_ctx, config);
  643. HASH_DEL(hypervisor.configurations[conf_sched_ctx], entry);
  644. free(config);
  645. }
  646. pthread_mutex_unlock(&hypervisor.conf_mut[conf_sched_ctx]);
  647. }
  648. if(hypervisor.resize[sched_ctx])
  649. {
  650. pthread_mutex_lock(&hypervisor.resize_mut[sched_ctx]);
  651. if(hypervisor.policy.handle_post_exec_hook)
  652. {
  653. /* Check whether 'task_tag' is in the 'resize_requests' set. */
  654. struct resize_request_entry *entry;
  655. HASH_FIND_INT(hypervisor.resize_requests[sched_ctx], &task_tag, entry);
  656. if (entry != NULL)
  657. {
  658. hypervisor.policy.handle_post_exec_hook(sched_ctx,
  659. task_tag);
  660. HASH_DEL(hypervisor.resize_requests[sched_ctx], entry);
  661. free(entry);
  662. }
  663. }
  664. pthread_mutex_unlock(&hypervisor.resize_mut[sched_ctx]);
  665. }
  666. return;
  667. }
  668. static void notify_submitted_job(struct starpu_task *task, uint32_t footprint)
  669. {
  670. pthread_mutex_lock(&act_hypervisor_mutex);
  671. hypervisor.sched_ctx_w[task->sched_ctx].submitted_flops += task->flops;
  672. pthread_mutex_unlock(&act_hypervisor_mutex);
  673. if(hypervisor.policy.handle_submitted_job)
  674. hypervisor.policy.handle_submitted_job(task, footprint);
  675. }
  676. void sched_ctx_hypervisor_size_ctxs(int *sched_ctxs, int nsched_ctxs, int *workers, int nworkers)
  677. {
  678. pthread_mutex_lock(&act_hypervisor_mutex);
  679. int curr_nsched_ctxs = sched_ctxs == NULL ? hypervisor.nsched_ctxs : nsched_ctxs;
  680. int *curr_sched_ctxs = sched_ctxs == NULL ? hypervisor.sched_ctxs : sched_ctxs;
  681. pthread_mutex_unlock(&act_hypervisor_mutex);
  682. int s;
  683. for(s = 0; s < curr_nsched_ctxs; s++)
  684. hypervisor.resize[curr_sched_ctxs[s]] = 1;
  685. if(hypervisor.policy.size_ctxs)
  686. hypervisor.policy.size_ctxs(curr_sched_ctxs, curr_nsched_ctxs, workers, nworkers);
  687. }
  688. struct sched_ctx_hypervisor_wrapper* sched_ctx_hypervisor_get_wrapper(unsigned sched_ctx)
  689. {
  690. return &hypervisor.sched_ctx_w[sched_ctx];
  691. }
  692. int* sched_ctx_hypervisor_get_sched_ctxs()
  693. {
  694. return hypervisor.sched_ctxs;
  695. }
  696. int sched_ctx_hypervisor_get_nsched_ctxs()
  697. {
  698. int ns;
  699. ns = hypervisor.nsched_ctxs;
  700. return ns;
  701. }
  702. void sched_ctx_hypervisor_save_size_req(int *sched_ctxs, int nsched_ctxs, int *workers, int nworkers)
  703. {
  704. hypervisor.sr = (struct size_request*)malloc(sizeof(struct size_request));
  705. hypervisor.sr->sched_ctxs = sched_ctxs;
  706. hypervisor.sr->nsched_ctxs = nsched_ctxs;
  707. hypervisor.sr->workers = workers;
  708. hypervisor.sr->nworkers = nworkers;
  709. }
  710. unsigned sched_ctx_hypervisor_get_size_req(int **sched_ctxs, int* nsched_ctxs, int **workers, int *nworkers)
  711. {
  712. if(hypervisor.sr != NULL)
  713. {
  714. *sched_ctxs = hypervisor.sr->sched_ctxs;
  715. *nsched_ctxs = hypervisor.sr->nsched_ctxs;
  716. *workers = hypervisor.sr->workers;
  717. *nworkers = hypervisor.sr->nworkers;
  718. return 1;
  719. }
  720. return 0;
  721. }
  722. void sched_ctx_hypervisor_free_size_req(void)
  723. {
  724. if(hypervisor.sr != NULL)
  725. {
  726. free(hypervisor.sr);
  727. hypervisor.sr = NULL;
  728. }
  729. }