sched_ctx_hypervisor.c 26 KB

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