feft_lp_policy.c 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370
  1. /* StarPU --- Runtime system for heterogeneous multicore architectures.
  2. *
  3. * Copyright (C) 2011 - 2013 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 "sc_hypervisor_lp.h"
  17. #include "sc_hypervisor_policy.h"
  18. #include <starpu_config.h>
  19. #include <sys/time.h>
  20. int resize_no = 0;
  21. #ifdef STARPU_HAVE_GLPK_H
  22. static void _try_resizing(unsigned *sched_ctxs, int nsched_ctxs, int *workers, int nworkers)
  23. {
  24. /* for vite */
  25. int ns = sched_ctxs == NULL ? sc_hypervisor_get_nsched_ctxs() : nsched_ctxs;
  26. #ifdef STARPU_SC_HYPERVISOR_DEBUG
  27. printf("resize_no = %d %d ctxs\n", resize_no, ns);
  28. #endif
  29. if(ns <= 0) return;
  30. unsigned *curr_sched_ctxs = sched_ctxs == NULL ? sc_hypervisor_get_sched_ctxs() : sched_ctxs;
  31. unsigned curr_nworkers = nworkers == -1 ? starpu_worker_get_count() : (unsigned)nworkers;
  32. struct types_of_workers *tw = sc_hypervisor_get_types_of_workers(workers, curr_nworkers);
  33. int nw = tw->nw;
  34. double nworkers_per_ctx[ns][nw];
  35. int total_nw[nw];
  36. sc_hypervisor_group_workers_by_type(tw, total_nw);
  37. unsigned can_redistrib = 0;
  38. struct timeval start_time;
  39. struct timeval end_time;
  40. gettimeofday(&start_time, NULL);
  41. double vmax = sc_hypervisor_lp_get_nworkers_per_ctx(ns, nw, nworkers_per_ctx, total_nw, tw, sched_ctxs);
  42. gettimeofday(&end_time, NULL);
  43. long diff_s = end_time.tv_sec - start_time.tv_sec;
  44. long diff_us = end_time.tv_usec - start_time.tv_usec;
  45. __attribute__((unused)) float timing = (float)(diff_s*1000000 + diff_us)/1000;
  46. if(vmax != -1.0)
  47. {
  48. /* int nworkers_per_ctx_rounded[ns][nw]; */
  49. /* sc_hypervisor_lp_round_double_to_int(ns, nw, nworkers_per_ctx, nworkers_per_ctx_rounded); */
  50. /* // sc_hypervisor_lp_redistribute_resources_in_ctxs(ns, nw, nworkers_per_ctx_rounded, nworkers_per_ctx, curr_sched_ctxs, tw); */
  51. /* sc_hypervisor_lp_distribute_resources_in_ctxs(curr_sched_ctxs, ns, nw, nworkers_per_ctx_rounded, nworkers_per_ctx, workers, curr_nworkers, tw); */
  52. sc_hypervisor_lp_distribute_floating_no_resources_in_ctxs(curr_sched_ctxs, ns, nw, nworkers_per_ctx, workers, curr_nworkers, tw);
  53. sc_hypervisor_lp_share_remaining_resources(ns, curr_sched_ctxs, curr_nworkers, workers);
  54. }
  55. #ifdef STARPU_SC_HYPERVISOR_DEBUG
  56. printf("*****finished resize \n");
  57. #endif
  58. return;
  59. }
  60. static void _try_resizing_hierarchically(unsigned levels, unsigned current_level, unsigned *sched_ctxs, unsigned nsched_ctxs, int *pus, int npus)
  61. {
  62. if(levels == 0)
  63. return;
  64. _try_resizing(sched_ctxs, nsched_ctxs, pus, npus);
  65. int s;
  66. for(s = 0; s < nsched_ctxs; s++)
  67. {
  68. unsigned *sched_ctxs_child;
  69. int nsched_ctxs_child = 0;
  70. sc_hypervisor_get_ctxs_on_level(&sched_ctxs_child, &nsched_ctxs_child, current_level+1, sched_ctxs[s]);
  71. if(nsched_ctxs_child > 0)
  72. {
  73. int *pus_father;
  74. unsigned npus_father = 0;
  75. npus_father = starpu_sched_ctx_get_workers_list(sched_ctxs[s], &pus_father);
  76. _try_resizing_hierarchically(levels-1, current_level+1, sched_ctxs_child, nsched_ctxs_child, pus_father, npus_father);
  77. free(pus_father);
  78. free(sched_ctxs_child);
  79. }
  80. }
  81. return;
  82. }
  83. static int _get_min_level(unsigned *sched_ctxs, int nsched_ctxs)
  84. {
  85. int min = sc_hypervisor_get_nhierarchy_levels();
  86. int s;
  87. for(s = 0; s < nsched_ctxs; s++)
  88. {
  89. int level = starpu_sched_ctx_get_hierarchy_level(sched_ctxs[s]);
  90. if(level < min)
  91. min = level;
  92. }
  93. return min;
  94. }
  95. static int _get_first_level(unsigned *sched_ctxs, int nsched_ctxs, unsigned *first_level, int *nsched_ctxs_first_level)
  96. {
  97. int min = _get_min_level(sched_ctxs, nsched_ctxs);
  98. int s;
  99. for(s = 0; s < nsched_ctxs; s++)
  100. if(starpu_sched_ctx_get_hierarchy_level(sched_ctxs[s]) == min)
  101. first_level[(*nsched_ctxs_first_level)++] = sched_ctxs[s];
  102. return min;
  103. }
  104. static void _resize(unsigned *sched_ctxs, int nsched_ctxs, int *workers, int nworkers)
  105. {
  106. #ifdef STARPU_USE_FXT
  107. starpu_fxt_trace_user_event(resize_no);
  108. #endif
  109. unsigned nhierarchy_levels = sc_hypervisor_get_nhierarchy_levels();
  110. if(nhierarchy_levels > 1)
  111. {
  112. if(nsched_ctxs == -1)
  113. {
  114. unsigned *sched_ctxs2;
  115. int nsched_ctxs2;
  116. sc_hypervisor_get_ctxs_on_level(&sched_ctxs2, &nsched_ctxs2, 0, STARPU_NMAX_SCHED_CTXS);
  117. if(nsched_ctxs2 > 0)
  118. {
  119. _try_resizing_hierarchically(nhierarchy_levels, 0, sched_ctxs2, nsched_ctxs2, workers, nworkers);
  120. free(sched_ctxs2);
  121. }
  122. }
  123. else
  124. {
  125. unsigned first_level[nsched_ctxs];
  126. int nsched_ctxs_first_level = 0;
  127. int min = _get_first_level(sched_ctxs, nsched_ctxs, first_level, &nsched_ctxs_first_level);
  128. _try_resizing_hierarchically(nhierarchy_levels, min, first_level, nsched_ctxs_first_level, workers, nworkers);
  129. }
  130. }
  131. else
  132. _try_resizing(sched_ctxs, nsched_ctxs, workers, nworkers);
  133. resize_no++;
  134. }
  135. static void _resize_if_speed_diff(unsigned sched_ctx, int worker)
  136. {
  137. unsigned nhierarchy_levels = sc_hypervisor_get_nhierarchy_levels();
  138. if(nhierarchy_levels > 1)
  139. {
  140. unsigned current_level = starpu_sched_ctx_get_hierarchy_level(sched_ctx);
  141. if(current_level == 0)
  142. {
  143. _resize(NULL, -1, NULL, -1);
  144. return;
  145. }
  146. unsigned father = starpu_sched_ctx_get_inheritor(sched_ctx);
  147. int level;
  148. int *pus_father_old = NULL;
  149. unsigned npus_father_old = 0;
  150. unsigned *sched_ctxs_old = NULL;
  151. int nsched_ctxs_old = 0;
  152. unsigned is_speed_diff = 0;
  153. unsigned last_level_diff = 0;
  154. for(level = current_level ; level >= 0; level--)
  155. {
  156. int *pus_father = NULL;
  157. int npus_father = -1;
  158. if(level > 0)
  159. npus_father = starpu_sched_ctx_get_workers_list(father, &pus_father);
  160. unsigned *sched_ctxs = NULL;
  161. int nsched_ctxs = 0;
  162. is_speed_diff = sc_hypervisor_check_speed_gap_btw_ctxs_on_level(level, pus_father, npus_father, father, &sched_ctxs, &nsched_ctxs);
  163. if(!is_speed_diff)
  164. {
  165. if(level == current_level)
  166. {
  167. if(pus_father)
  168. free(pus_father);
  169. if(sched_ctxs)
  170. free(sched_ctxs);
  171. pus_father = NULL;
  172. sched_ctxs = NULL;
  173. break;
  174. }
  175. else
  176. {
  177. _resize(sched_ctxs_old, nsched_ctxs_old, pus_father_old, npus_father_old);
  178. if(pus_father_old)
  179. free(pus_father_old);
  180. if(sched_ctxs_old)
  181. free(sched_ctxs_old);
  182. pus_father_old = NULL;
  183. sched_ctxs_old = NULL;
  184. if(pus_father)
  185. free(pus_father);
  186. if(nsched_ctxs > 0)
  187. free(sched_ctxs);
  188. pus_father = NULL;
  189. sched_ctxs = NULL;
  190. break;
  191. }
  192. }
  193. if(pus_father_old)
  194. free(pus_father_old);
  195. if(sched_ctxs_old)
  196. free(sched_ctxs_old);
  197. pus_father_old = pus_father;
  198. sched_ctxs_old = sched_ctxs;
  199. npus_father_old = npus_father;
  200. nsched_ctxs_old = nsched_ctxs;
  201. father = level > 1 ? starpu_sched_ctx_get_inheritor(father) : STARPU_NMAX_SCHED_CTXS;
  202. }
  203. if(is_speed_diff)
  204. {
  205. if(pus_father_old)
  206. free(pus_father_old);
  207. if(sched_ctxs_old)
  208. free(sched_ctxs_old);
  209. _resize(NULL, -1, NULL, -1);
  210. }
  211. }
  212. else if(sc_hypervisor_check_speed_gap_btw_ctxs(NULL, -1, NULL, -1))
  213. {
  214. _resize(NULL, -1, NULL, -1);
  215. }
  216. return;
  217. }
  218. static void feft_lp_handle_poped_task(unsigned sched_ctx, int worker,
  219. __attribute__((unused))struct starpu_task *task, __attribute__((unused))uint32_t footprint)
  220. {
  221. unsigned criteria = sc_hypervisor_get_resize_criteria();
  222. if(criteria != SC_NOTHING && criteria == SC_SPEED)
  223. {
  224. int ret = starpu_pthread_mutex_trylock(&act_hypervisor_mutex);
  225. if(ret != EBUSY)
  226. {
  227. _resize_if_speed_diff(sched_ctx, worker);
  228. starpu_pthread_mutex_unlock(&act_hypervisor_mutex);
  229. }
  230. }
  231. }
  232. static void feft_lp_size_ctxs(unsigned *sched_ctxs, int nsched_ctxs, int *workers, int nworkers)
  233. {
  234. starpu_pthread_mutex_lock(&act_hypervisor_mutex);
  235. struct sc_hypervisor_wrapper* sc_w = NULL;
  236. int s = 0;
  237. for(s = 0; s < nsched_ctxs; s++)
  238. {
  239. sc_w = sc_hypervisor_get_wrapper(sched_ctxs[s]);
  240. sc_w->to_be_sized = 1;
  241. }
  242. _resize(sched_ctxs, nsched_ctxs, workers, nworkers);
  243. #ifdef STARPU_SC_HYPERVISOR_DEBUG
  244. printf("finished size ctxs\n");
  245. #endif
  246. starpu_pthread_mutex_unlock(&act_hypervisor_mutex);
  247. }
  248. static void _resize_leaves(int worker)
  249. {
  250. unsigned s, s2;
  251. unsigned *sched_ctxs = NULL;
  252. unsigned nsched_ctxs = starpu_worker_get_sched_ctx_list(worker, &sched_ctxs);
  253. unsigned workers_sched_ctxs[nsched_ctxs];
  254. unsigned nworkers_sched_ctxs = 0;
  255. struct sc_hypervisor_wrapper *sc_w = NULL;
  256. for(s = 0; s < nsched_ctxs; s++)
  257. {
  258. sc_w = sc_hypervisor_get_wrapper(sched_ctxs[s]);
  259. if(sc_w->sched_ctx != STARPU_NMAX_SCHED_CTXS)
  260. {
  261. workers_sched_ctxs[nworkers_sched_ctxs++] = sched_ctxs[s];
  262. }
  263. }
  264. free(sched_ctxs);
  265. unsigned leaves[nsched_ctxs];
  266. unsigned nleaves = 0;
  267. sc_hypervisor_get_leaves(workers_sched_ctxs, nworkers_sched_ctxs, leaves, &nleaves);
  268. for(s = 0; s < nleaves; s++)
  269. _resize_if_speed_diff(leaves[s], worker);
  270. }
  271. static void feft_lp_handle_idle_cycle(unsigned sched_ctx, int worker)
  272. {
  273. unsigned criteria = sc_hypervisor_get_resize_criteria();
  274. if(criteria != SC_NOTHING)// && criteria == SC_IDLE)
  275. {
  276. int ret = starpu_pthread_mutex_trylock(&act_hypervisor_mutex);
  277. if(ret != EBUSY)
  278. {
  279. // printf("trigger idle \n");
  280. _resize_leaves(worker);
  281. starpu_pthread_mutex_unlock(&act_hypervisor_mutex);
  282. }
  283. }
  284. }
  285. static void feft_lp_resize_ctxs(unsigned *sched_ctxs, int nsched_ctxs ,
  286. int *workers, int nworkers)
  287. {
  288. int ret = starpu_pthread_mutex_trylock(&act_hypervisor_mutex);
  289. if(ret != EBUSY)
  290. {
  291. struct sc_hypervisor_wrapper* sc_w = NULL;
  292. int s = 0;
  293. for(s = 0; s < nsched_ctxs; s++)
  294. {
  295. sc_w = sc_hypervisor_get_wrapper(sched_ctxs[s]);
  296. if((sc_w->submitted_flops + (0.1*sc_w->total_flops)) < sc_w->total_flops)
  297. {
  298. starpu_pthread_mutex_unlock(&act_hypervisor_mutex);
  299. return;
  300. }
  301. }
  302. _resize(sched_ctxs, nsched_ctxs, workers, nworkers);
  303. starpu_pthread_mutex_unlock(&act_hypervisor_mutex);
  304. }
  305. }
  306. struct sc_hypervisor_policy feft_lp_policy = {
  307. .size_ctxs = feft_lp_size_ctxs,
  308. .resize_ctxs = feft_lp_resize_ctxs,
  309. .handle_poped_task = feft_lp_handle_poped_task,
  310. .handle_pushed_task = NULL,
  311. .handle_idle_cycle = feft_lp_handle_idle_cycle,
  312. .handle_idle_end = NULL,
  313. .handle_post_exec_hook = NULL,
  314. .handle_submitted_job = NULL,
  315. .end_ctx = NULL,
  316. .custom = 0,
  317. .name = "feft_lp"
  318. };
  319. #endif /* STARPU_HAVE_GLPK_H */