sched_ctx_hypervisor.c 26 KB

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