sched_ctx_hypervisor.c 26 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831
  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 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. #endif // STARPU_HAVE_GLPK_H
  34. static struct 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 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 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 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 sched_ctx_hypervisor_policy *_select_hypervisor_policy(struct sched_ctx_hypervisor_policy* hypervisor_policy)
  78. {
  79. struct 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 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 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 sched_ctx_hypervisor_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. struct starpu_iterator it;
  285. if(workers->init_iterator)
  286. workers->init_iterator(workers, &it);
  287. while(workers->has_next(workers, &it))
  288. {
  289. worker = workers->get_next(workers, &it);
  290. enum starpu_archtype curr_arch = starpu_worker_get_type(worker);
  291. if(curr_arch == arch || arch == STARPU_ANY_WORKER)
  292. nworkers_ctx++;
  293. }
  294. return nworkers_ctx;
  295. }
  296. /* actually move the workers: the cpus are moved, gpus are only shared */
  297. /* forbids another resize request before this one is take into account */
  298. void sched_ctx_hypervisor_move_workers(unsigned sender_sched_ctx, unsigned receiver_sched_ctx, int* workers_to_move, unsigned nworkers_to_move, unsigned now)
  299. {
  300. if(nworkers_to_move > 0 && hypervisor.resize[sender_sched_ctx])// && hypervisor.resize[receiver_sched_ctx])
  301. {
  302. int j;
  303. printf("resize ctx %d with", sender_sched_ctx);
  304. for(j = 0; j < nworkers_to_move; j++)
  305. printf(" %d", workers_to_move[j]);
  306. printf("\n");
  307. int *cpus = (int*) malloc(nworkers_to_move * sizeof(int));
  308. int ncpus;
  309. _get_cpus(workers_to_move, nworkers_to_move, cpus, &ncpus);
  310. // if(ncpus != 0)
  311. // starpu_sched_ctx_remove_workers(cpus, ncpus, sender_sched_ctx);
  312. starpu_sched_ctx_add_workers(workers_to_move, nworkers_to_move, receiver_sched_ctx);
  313. if(now)
  314. {
  315. int j;
  316. printf("remove now from ctx %d:", sender_sched_ctx);
  317. for(j = 0; j < nworkers_to_move; j++)
  318. printf(" %d", workers_to_move[j]);
  319. printf("\n");
  320. starpu_sched_ctx_remove_workers(workers_to_move, nworkers_to_move, sender_sched_ctx);
  321. }
  322. else
  323. {
  324. int ret = pthread_mutex_trylock(&hypervisor.sched_ctx_w[sender_sched_ctx].mutex);
  325. if(ret != EBUSY)
  326. {
  327. hypervisor.sched_ctx_w[sender_sched_ctx].resize_ack.receiver_sched_ctx = receiver_sched_ctx;
  328. hypervisor.sched_ctx_w[sender_sched_ctx].resize_ack.moved_workers = (int*)malloc(nworkers_to_move * sizeof(int));
  329. hypervisor.sched_ctx_w[sender_sched_ctx].resize_ack.nmoved_workers = nworkers_to_move;
  330. hypervisor.sched_ctx_w[sender_sched_ctx].resize_ack.acked_workers = (int*)malloc(nworkers_to_move * sizeof(int));
  331. int i;
  332. for(i = 0; i < nworkers_to_move; i++)
  333. {
  334. hypervisor.sched_ctx_w[sender_sched_ctx].current_idle_time[workers_to_move[i]] = 0.0;
  335. hypervisor.sched_ctx_w[sender_sched_ctx].resize_ack.moved_workers[i] = workers_to_move[i];
  336. hypervisor.sched_ctx_w[sender_sched_ctx].resize_ack.acked_workers[i] = 0;
  337. }
  338. hypervisor.resize[sender_sched_ctx] = 0;
  339. // hypervisor.resize[receiver_sched_ctx] = 0;
  340. pthread_mutex_unlock(&hypervisor.sched_ctx_w[sender_sched_ctx].mutex);
  341. }
  342. }
  343. struct sched_ctx_hypervisor_policy_config *new_config = sched_ctx_hypervisor_get_config(receiver_sched_ctx);
  344. int i;
  345. for(i = 0; i < nworkers_to_move; i++)
  346. 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;
  347. }
  348. return;
  349. }
  350. void sched_ctx_hypervisor_add_workers_to_sched_ctx(int* workers_to_add, unsigned nworkers_to_add, unsigned sched_ctx)
  351. {
  352. if(nworkers_to_add > 0 && hypervisor.resize[sched_ctx])
  353. {
  354. int j;
  355. printf("add to ctx %d:", sched_ctx);
  356. for(j = 0; j < nworkers_to_add; j++)
  357. printf(" %d", workers_to_add[j]);
  358. printf("\n");
  359. starpu_sched_ctx_add_workers(workers_to_add, nworkers_to_add, sched_ctx);
  360. struct sched_ctx_hypervisor_policy_config *new_config = sched_ctx_hypervisor_get_config(sched_ctx);
  361. int i;
  362. for(i = 0; i < nworkers_to_add; i++)
  363. 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;
  364. }
  365. return;
  366. }
  367. unsigned sched_ctx_hypervisor_can_resize(unsigned sched_ctx)
  368. {
  369. return hypervisor.resize[sched_ctx];
  370. }
  371. void sched_ctx_hypervisor_remove_workers_from_sched_ctx(int* workers_to_remove, unsigned nworkers_to_remove, unsigned sched_ctx, unsigned now)
  372. {
  373. if(nworkers_to_remove > 0 && hypervisor.resize[sched_ctx])
  374. {
  375. int nworkers=0;
  376. int workers[nworkers_to_remove];
  377. if(now)
  378. {
  379. int j;
  380. printf("remove explicitley now from ctx %d:", sched_ctx);
  381. for(j = 0; j < nworkers_to_remove; j++)
  382. printf(" %d", workers_to_remove[j]);
  383. printf("\n");
  384. starpu_sched_ctx_remove_workers(workers_to_remove, nworkers_to_remove, sched_ctx);
  385. }
  386. else
  387. {
  388. int ret = pthread_mutex_trylock(&hypervisor.sched_ctx_w[sched_ctx].mutex);
  389. if(ret != EBUSY)
  390. {
  391. int i;
  392. for(i = 0; i < nworkers_to_remove; i++)
  393. if(starpu_sched_ctx_contains_worker(workers_to_remove[i], sched_ctx))
  394. workers[nworkers++] = workers_to_remove[i];
  395. hypervisor.sched_ctx_w[sched_ctx].resize_ack.receiver_sched_ctx = -1;
  396. hypervisor.sched_ctx_w[sched_ctx].resize_ack.moved_workers = (int*)malloc(nworkers_to_remove * sizeof(int));
  397. hypervisor.sched_ctx_w[sched_ctx].resize_ack.nmoved_workers = nworkers;
  398. hypervisor.sched_ctx_w[sched_ctx].resize_ack.acked_workers = (int*)malloc(nworkers_to_remove * sizeof(int));
  399. for(i = 0; i < nworkers; i++)
  400. {
  401. hypervisor.sched_ctx_w[sched_ctx].current_idle_time[workers[i]] = 0.0;
  402. hypervisor.sched_ctx_w[sched_ctx].resize_ack.moved_workers[i] = workers[i];
  403. hypervisor.sched_ctx_w[sched_ctx].resize_ack.acked_workers[i] = 0;
  404. }
  405. hypervisor.resize[sched_ctx] = 0;
  406. pthread_mutex_unlock(&hypervisor.sched_ctx_w[sched_ctx].mutex);
  407. }
  408. }
  409. }
  410. return;
  411. }
  412. static void _set_elapsed_flops_per_sched_ctx(unsigned sched_ctx, double val)
  413. {
  414. int i;
  415. for(i = 0; i < STARPU_NMAXWORKERS; i++)
  416. hypervisor.sched_ctx_w[sched_ctx].elapsed_flops[i] = val;
  417. }
  418. double sched_ctx_hypervisor_get_elapsed_flops_per_sched_ctx(struct sched_ctx_hypervisor_wrapper* sc_w)
  419. {
  420. double ret_val = 0.0;
  421. int i;
  422. for(i = 0; i < STARPU_NMAXWORKERS; i++)
  423. ret_val += sc_w->elapsed_flops[i];
  424. return ret_val;
  425. }
  426. double sched_ctx_hypervisor_get_total_elapsed_flops_per_sched_ctx(struct sched_ctx_hypervisor_wrapper* sc_w)
  427. {
  428. double ret_val = 0.0;
  429. int i;
  430. for(i = 0; i < STARPU_NMAXWORKERS; i++)
  431. ret_val += sc_w->total_elapsed_flops[i];
  432. return ret_val;
  433. }
  434. static unsigned _ack_resize_completed(unsigned sched_ctx, int worker)
  435. {
  436. if(worker != -1 && !starpu_sched_ctx_contains_worker(worker, sched_ctx))
  437. return 0;
  438. struct sched_ctx_hypervisor_resize_ack *resize_ack = NULL;
  439. unsigned sender_sched_ctx = STARPU_NMAX_SCHED_CTXS;
  440. int i;
  441. for(i = 0; i < STARPU_NMAX_SCHED_CTXS; i++)
  442. {
  443. if(hypervisor.sched_ctxs[i] != STARPU_NMAX_SCHED_CTXS)
  444. {
  445. struct sched_ctx_hypervisor_wrapper *sc_w = &hypervisor.sched_ctx_w[hypervisor.sched_ctxs[i]];
  446. pthread_mutex_lock(&sc_w->mutex);
  447. unsigned only_remove = 0;
  448. if(sc_w->resize_ack.receiver_sched_ctx == -1 && hypervisor.sched_ctxs[i] != sched_ctx &&
  449. sc_w->resize_ack.nmoved_workers > 0 && starpu_sched_ctx_contains_worker(worker, hypervisor.sched_ctxs[i]))
  450. {
  451. int j;
  452. for(j = 0; j < sc_w->resize_ack.nmoved_workers; j++)
  453. if(sc_w->resize_ack.moved_workers[j] == worker)
  454. {
  455. only_remove = 1;
  456. break;
  457. }
  458. }
  459. if(only_remove ||
  460. (sc_w->resize_ack.receiver_sched_ctx != -1 && sc_w->resize_ack.receiver_sched_ctx == sched_ctx))
  461. {
  462. resize_ack = &sc_w->resize_ack;
  463. sender_sched_ctx = hypervisor.sched_ctxs[i];
  464. pthread_mutex_unlock(&sc_w->mutex);
  465. break;
  466. }
  467. pthread_mutex_unlock(&sc_w->mutex);
  468. }
  469. }
  470. /* if there is no ctx waiting for its ack return 1*/
  471. if(resize_ack == NULL)
  472. return 1;
  473. int ret = pthread_mutex_trylock(&hypervisor.sched_ctx_w[sender_sched_ctx].mutex);
  474. if(ret != EBUSY)
  475. {
  476. int *moved_workers = resize_ack->moved_workers;
  477. int nmoved_workers = resize_ack->nmoved_workers;
  478. int *acked_workers = resize_ack->acked_workers;
  479. if(worker != -1)
  480. {
  481. for(i = 0; i < nmoved_workers; i++)
  482. {
  483. int moved_worker = moved_workers[i];
  484. if(moved_worker == worker && acked_workers[i] == 0)
  485. {
  486. acked_workers[i] = 1;
  487. }
  488. }
  489. }
  490. int nacked_workers = 0;
  491. for(i = 0; i < nmoved_workers; i++)
  492. {
  493. nacked_workers += (acked_workers[i] == 1);
  494. }
  495. unsigned resize_completed = (nacked_workers == nmoved_workers);
  496. int receiver_sched_ctx = sched_ctx;
  497. if(resize_completed)
  498. {
  499. /* if the permission to resize is not allowed by the user don't do it
  500. whatever the application says */
  501. if(!((hypervisor.resize[sender_sched_ctx] == 0 || hypervisor.resize[receiver_sched_ctx] == 0) && imposed_resize))
  502. {
  503. int j;
  504. printf("remove after ack from ctx %d:", sender_sched_ctx);
  505. for(j = 0; j < nmoved_workers; j++)
  506. printf(" %d", moved_workers[j]);
  507. printf("\n");
  508. starpu_sched_ctx_remove_workers(moved_workers, nmoved_workers, sender_sched_ctx);
  509. /* info concerning only the gflops_rate strateg */
  510. struct sched_ctx_hypervisor_wrapper *sender_sc_w = &hypervisor.sched_ctx_w[sender_sched_ctx];
  511. struct sched_ctx_hypervisor_wrapper *receiver_sc_w = &hypervisor.sched_ctx_w[receiver_sched_ctx];
  512. double start_time = starpu_timing_now();
  513. sender_sc_w->start_time = start_time;
  514. sender_sc_w->remaining_flops = sender_sc_w->remaining_flops - sched_ctx_hypervisor_get_elapsed_flops_per_sched_ctx(sender_sc_w);
  515. _set_elapsed_flops_per_sched_ctx(sender_sched_ctx, 0.0);
  516. receiver_sc_w->start_time = start_time;
  517. receiver_sc_w->remaining_flops = receiver_sc_w->remaining_flops - sched_ctx_hypervisor_get_elapsed_flops_per_sched_ctx(receiver_sc_w);
  518. _set_elapsed_flops_per_sched_ctx(receiver_sched_ctx, 0.0);
  519. hypervisor.resize[sender_sched_ctx] = 1;
  520. // hypervisor.resize[receiver_sched_ctx] = 1;
  521. /* if the user allowed resizing leave the decisions to the application */
  522. if(imposed_resize) imposed_resize = 0;
  523. resize_ack->receiver_sched_ctx = -1;
  524. resize_ack->nmoved_workers = 0;
  525. free(resize_ack->moved_workers);
  526. free(resize_ack->acked_workers);
  527. }
  528. pthread_mutex_unlock(&hypervisor.sched_ctx_w[sender_sched_ctx].mutex);
  529. return resize_completed;
  530. }
  531. pthread_mutex_unlock(&hypervisor.sched_ctx_w[sender_sched_ctx].mutex);
  532. }
  533. return 0;
  534. }
  535. /* Enqueue a resize request for 'sched_ctx', to be executed when the
  536. * 'task_tag' tasks of 'sched_ctx' complete. */
  537. void sched_ctx_hypervisor_resize(unsigned sched_ctx, int task_tag)
  538. {
  539. struct resize_request_entry *entry;
  540. entry = malloc(sizeof *entry);
  541. STARPU_ASSERT(entry != NULL);
  542. entry->sched_ctx = sched_ctx;
  543. entry->task_tag = task_tag;
  544. pthread_mutex_lock(&hypervisor.resize_mut[sched_ctx]);
  545. HASH_ADD_INT(hypervisor.resize_requests[sched_ctx], task_tag, entry);
  546. pthread_mutex_unlock(&hypervisor.resize_mut[sched_ctx]);
  547. }
  548. /* notifies the hypervisor that the worker is no longer idle and a new task was pushed on its queue */
  549. static void notify_idle_end(unsigned sched_ctx, int worker)
  550. {
  551. if(hypervisor.resize[sched_ctx])
  552. hypervisor.sched_ctx_w[sched_ctx].current_idle_time[worker] = 0.0;
  553. if(hypervisor.policy.handle_idle_end)
  554. hypervisor.policy.handle_idle_end(sched_ctx, worker);
  555. }
  556. /* notifies the hypervisor that the worker spent another cycle in idle time */
  557. static void notify_idle_cycle(unsigned sched_ctx, int worker, double idle_time)
  558. {
  559. if(hypervisor.resize[sched_ctx])
  560. {
  561. struct sched_ctx_hypervisor_wrapper *sc_w = &hypervisor.sched_ctx_w[sched_ctx];
  562. sc_w->current_idle_time[worker] += idle_time;
  563. if(hypervisor.policy.handle_idle_cycle)
  564. {
  565. hypervisor.policy.handle_idle_cycle(sched_ctx, worker);
  566. }
  567. }
  568. return;
  569. }
  570. /* notifies the hypervisor that a new task was pushed on the queue of the worker */
  571. static void notify_pushed_task(unsigned sched_ctx, int worker)
  572. {
  573. hypervisor.sched_ctx_w[sched_ctx].pushed_tasks[worker]++;
  574. if(hypervisor.sched_ctx_w[sched_ctx].total_flops != 0.0 && hypervisor.sched_ctx_w[sched_ctx].start_time == 0.0)
  575. hypervisor.sched_ctx_w[sched_ctx].start_time = starpu_timing_now();
  576. int ntasks = get_ntasks(hypervisor.sched_ctx_w[sched_ctx].pushed_tasks);
  577. if((hypervisor.min_tasks == 0 || (!(hypervisor.resize[sched_ctx] == 0 && imposed_resize) && ntasks == hypervisor.min_tasks)) && hypervisor.check_min_tasks[sched_ctx])
  578. {
  579. hypervisor.resize[sched_ctx] = 1;
  580. if(imposed_resize) imposed_resize = 0;
  581. hypervisor.check_min_tasks[sched_ctx] = 0;
  582. }
  583. if(hypervisor.policy.handle_pushed_task)
  584. hypervisor.policy.handle_pushed_task(sched_ctx, worker);
  585. }
  586. /* notifies the hypervisor that a task was poped from the queue of the worker */
  587. static void notify_poped_task(unsigned sched_ctx, int worker, double elapsed_flops)
  588. {
  589. hypervisor.sched_ctx_w[sched_ctx].poped_tasks[worker]++;
  590. hypervisor.sched_ctx_w[sched_ctx].elapsed_flops[worker] += elapsed_flops;
  591. hypervisor.sched_ctx_w[sched_ctx].total_elapsed_flops[worker] += elapsed_flops;
  592. 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]);
  593. if(hypervisor.resize[sched_ctx])
  594. {
  595. if(hypervisor.policy.handle_poped_task)
  596. hypervisor.policy.handle_poped_task(sched_ctx, worker);
  597. }
  598. _ack_resize_completed(sched_ctx, worker);
  599. }
  600. /* notifies the hypervisor that a tagged task has just been executed */
  601. static void notify_post_exec_hook(unsigned sched_ctx, int task_tag)
  602. {
  603. STARPU_ASSERT(task_tag > 0);
  604. unsigned conf_sched_ctx;
  605. int i;
  606. pthread_mutex_lock(&act_hypervisor_mutex);
  607. unsigned ns = hypervisor.nsched_ctxs;
  608. pthread_mutex_unlock(&act_hypervisor_mutex);
  609. for(i = 0; i < ns; i++)
  610. {
  611. struct configuration_entry *entry;
  612. conf_sched_ctx = hypervisor.sched_ctxs[i];
  613. pthread_mutex_lock(&hypervisor.conf_mut[conf_sched_ctx]);
  614. HASH_FIND_INT(hypervisor.configurations[conf_sched_ctx], &task_tag, entry);
  615. if (entry != NULL)
  616. {
  617. struct sched_ctx_hypervisor_policy_config *config = entry->configuration;
  618. sched_ctx_hypervisor_set_config(conf_sched_ctx, config);
  619. HASH_DEL(hypervisor.configurations[conf_sched_ctx], entry);
  620. free(config);
  621. }
  622. pthread_mutex_unlock(&hypervisor.conf_mut[conf_sched_ctx]);
  623. }
  624. if(hypervisor.resize[sched_ctx])
  625. {
  626. pthread_mutex_lock(&hypervisor.resize_mut[sched_ctx]);
  627. if(hypervisor.policy.handle_post_exec_hook)
  628. {
  629. /* Check whether 'task_tag' is in the 'resize_requests' set. */
  630. struct resize_request_entry *entry;
  631. HASH_FIND_INT(hypervisor.resize_requests[sched_ctx], &task_tag, entry);
  632. if (entry != NULL)
  633. {
  634. hypervisor.policy.handle_post_exec_hook(sched_ctx,
  635. task_tag);
  636. HASH_DEL(hypervisor.resize_requests[sched_ctx], entry);
  637. free(entry);
  638. }
  639. }
  640. pthread_mutex_unlock(&hypervisor.resize_mut[sched_ctx]);
  641. }
  642. return;
  643. }
  644. static void notify_submitted_job(struct starpu_task *task, uint32_t footprint)
  645. {
  646. pthread_mutex_lock(&act_hypervisor_mutex);
  647. hypervisor.sched_ctx_w[task->sched_ctx].submitted_flops += task->flops;
  648. pthread_mutex_unlock(&act_hypervisor_mutex);
  649. if(hypervisor.policy.handle_submitted_job)
  650. hypervisor.policy.handle_submitted_job(task, footprint);
  651. }
  652. void sched_ctx_hypervisor_size_ctxs(int *sched_ctxs, int nsched_ctxs, int *workers, int nworkers)
  653. {
  654. pthread_mutex_lock(&act_hypervisor_mutex);
  655. int curr_nsched_ctxs = sched_ctxs == NULL ? hypervisor.nsched_ctxs : nsched_ctxs;
  656. int *curr_sched_ctxs = sched_ctxs == NULL ? hypervisor.sched_ctxs : sched_ctxs;
  657. pthread_mutex_unlock(&act_hypervisor_mutex);
  658. int s;
  659. for(s = 0; s < curr_nsched_ctxs; s++)
  660. hypervisor.resize[curr_sched_ctxs[s]] = 1;
  661. if(hypervisor.policy.size_ctxs)
  662. hypervisor.policy.size_ctxs(curr_sched_ctxs, curr_nsched_ctxs, workers, nworkers);
  663. }
  664. struct sched_ctx_hypervisor_wrapper* sched_ctx_hypervisor_get_wrapper(unsigned sched_ctx)
  665. {
  666. return &hypervisor.sched_ctx_w[sched_ctx];
  667. }
  668. int* sched_ctx_hypervisor_get_sched_ctxs()
  669. {
  670. return hypervisor.sched_ctxs;
  671. }
  672. int sched_ctx_hypervisor_get_nsched_ctxs()
  673. {
  674. int ns;
  675. ns = hypervisor.nsched_ctxs;
  676. return ns;
  677. }
  678. void sched_ctx_hypervisor_save_size_req(int *sched_ctxs, int nsched_ctxs, int *workers, int nworkers)
  679. {
  680. hypervisor.sr = (struct size_request*)malloc(sizeof(struct size_request));
  681. hypervisor.sr->sched_ctxs = sched_ctxs;
  682. hypervisor.sr->nsched_ctxs = nsched_ctxs;
  683. hypervisor.sr->workers = workers;
  684. hypervisor.sr->nworkers = nworkers;
  685. }
  686. unsigned sched_ctx_hypervisor_get_size_req(int **sched_ctxs, int* nsched_ctxs, int **workers, int *nworkers)
  687. {
  688. if(hypervisor.sr != NULL)
  689. {
  690. *sched_ctxs = hypervisor.sr->sched_ctxs;
  691. *nsched_ctxs = hypervisor.sr->nsched_ctxs;
  692. *workers = hypervisor.sr->workers;
  693. *nworkers = hypervisor.sr->nworkers;
  694. return 1;
  695. }
  696. return 0;
  697. }
  698. void sched_ctx_hypervisor_free_size_req(void)
  699. {
  700. if(hypervisor.sr != NULL)
  701. {
  702. free(hypervisor.sr);
  703. hypervisor.sr = NULL;
  704. }
  705. }