sched_ctx_hypervisor.c 27 KB

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