throughput_lp_policy.c 9.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358
  1. /* StarPU --- Runtime system for heterogeneous multicore architectures.
  2. *
  3. * Copyright (C) 2011-2020 Université de Bordeaux, CNRS (LaBRI UMR 5800), 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 <starpu_config.h>
  17. #include "sc_hypervisor_lp.h"
  18. #include "sc_hypervisor_policy.h"
  19. #include <math.h>
  20. #include <sys/time.h>
  21. static double _glp_resolve(int ns, int nw, double speed[ns][nw], double w_in_s[ns][nw], unsigned integer);
  22. static unsigned _compute_max_speed(int ns, int nw, double w_in_s[ns][nw], unsigned *in_sched_ctxs, int *workers)
  23. {
  24. double speed[ns][nw];
  25. unsigned *sched_ctxs = in_sched_ctxs == NULL ? sc_hypervisor_get_sched_ctxs() : in_sched_ctxs;
  26. int w,s;
  27. struct sc_hypervisor_wrapper* sc_w = NULL;
  28. for(s = 0; s < ns; s++)
  29. {
  30. sc_w = sc_hypervisor_get_wrapper(sched_ctxs[s]);
  31. for(w = 0; w < nw; w++)
  32. {
  33. w_in_s[s][w] = 0.0;
  34. int worker = workers == NULL ? w : workers[w];
  35. enum starpu_worker_archtype arch = starpu_worker_get_type(worker);
  36. speed[s][w] = sc_hypervisor_get_speed(sc_w, arch);
  37. }
  38. }
  39. struct timeval start_time;
  40. struct timeval end_time;
  41. gettimeofday(&start_time, NULL);
  42. double res = _glp_resolve(ns, nw, speed, w_in_s, 1);
  43. gettimeofday(&end_time, NULL);
  44. long diff_s = end_time.tv_sec - start_time.tv_sec;
  45. long diff_us = end_time.tv_usec - start_time.tv_usec;
  46. __attribute__((unused)) float timing = (float)(diff_s*1000000 + diff_us)/1000;
  47. if(res > 0.0)
  48. return 1;
  49. return 0;
  50. }
  51. /*
  52. * GNU Linear Programming Kit backend
  53. */
  54. #ifdef STARPU_HAVE_GLPK_H
  55. #include <glpk.h>
  56. static double _glp_resolve(int ns, int nw, double speed[ns][nw], double w_in_s[ns][nw], unsigned integer)
  57. {
  58. int w = 0, s = 0;
  59. glp_prob *lp;
  60. lp = glp_create_prob();
  61. glp_set_prob_name(lp, "StarPU theoretical bound");
  62. glp_set_obj_dir(lp, GLP_MAX);
  63. glp_set_obj_name(lp, "total speed");
  64. {
  65. int ne = 2 * ns * nw /* worker execution time */
  66. + 1
  67. + 1 ; /* glp dumbness */
  68. int n = 1;
  69. int ia[ne], ja[ne];
  70. double ar[ne];
  71. /* Variables: x[s][w]
  72. the acknwoledgment that the worker w belongs to the context s */
  73. glp_add_cols(lp, nw*ns + 1);
  74. for(s = 0; s < ns; s++)
  75. for(w = 0; w < nw; w++)
  76. {
  77. char name[32];
  78. snprintf(name, sizeof(name), "w%ds%dn", w, s);
  79. glp_set_col_name(lp, s*nw+w+1, name);
  80. if (integer)
  81. {
  82. glp_set_col_kind(lp, s*nw+w+1, GLP_IV);
  83. glp_set_col_bnds(lp, s*nw+w+1, GLP_DB, 0, 1);
  84. }
  85. else
  86. glp_set_col_bnds(lp, s*nw+w+1, GLP_DB, 0.0, 1.0);
  87. }
  88. /* vmax should be positif */
  89. /* Z = vmax structural variable, x[s][w] are auxiliar variables */
  90. glp_set_col_name(lp, nw*ns+1, "vmax");
  91. glp_set_col_bnds(lp, nw*ns+1, GLP_LO, 0.0, 0.0);
  92. glp_set_obj_coef(lp, nw*ns+1, 1.);
  93. int curr_row_idx = 0;
  94. /* Total worker speed */
  95. glp_add_rows(lp, 1);
  96. /*sum(x[s][w]*speed[s][w]) >= vmax */
  97. char name[32], title[64];
  98. starpu_worker_get_name(w, name, sizeof(name));
  99. snprintf(title, sizeof(title), "worker %s", name);
  100. glp_set_row_name(lp, curr_row_idx + 1, title);
  101. for(s = 0; s < ns; s++)
  102. {
  103. for (w = 0; w < nw; w++)
  104. {
  105. /* x[s][w] */
  106. ia[n] = curr_row_idx + 1;
  107. ja[n] = s*nw+w+1;
  108. ar[n] = speed[s][w];
  109. n++;
  110. }
  111. }
  112. /* vmax */
  113. ia[n] = curr_row_idx + 1;
  114. ja[n] = nw*ns+1;
  115. ar[n] = (-1);
  116. n++;
  117. glp_set_row_bnds(lp, curr_row_idx + 1, GLP_LO, 0.0, 0.0);
  118. curr_row_idx += 1 ;
  119. /* sum(x[s][w]) = 1 */
  120. glp_add_rows(lp, nw);
  121. for (w = 0; w < nw; w++)
  122. {
  123. char name[32], title[64];
  124. starpu_worker_get_name(w, name, sizeof(name));
  125. snprintf(title, sizeof(title), "w%x", w);
  126. glp_set_row_name(lp, curr_row_idx+w+1, title);
  127. for(s = 0; s < ns; s++)
  128. {
  129. ia[n] = curr_row_idx+w+1;
  130. ja[n] = s*nw+w+1;
  131. ar[n] = 1;
  132. n++;
  133. }
  134. if(integer)
  135. glp_set_row_bnds(lp, curr_row_idx+w+1, GLP_FX, 1, 1);
  136. else
  137. glp_set_row_bnds(lp, curr_row_idx+w+1, GLP_FX, 1.0, 1.0);
  138. }
  139. if(n != ne)
  140. printf("ns= %d nw = %d n = %d ne = %d\n", ns, nw, n, ne);
  141. STARPU_ASSERT(n == ne);
  142. glp_load_matrix(lp, ne-1, ia, ja, ar);
  143. }
  144. glp_smcp parm;
  145. glp_init_smcp(&parm);
  146. parm.msg_lev = GLP_MSG_OFF;
  147. int ret = glp_simplex(lp, &parm);
  148. if (ret)
  149. {
  150. glp_delete_prob(lp);
  151. lp = NULL;
  152. return 0.0;
  153. }
  154. if (integer)
  155. {
  156. glp_iocp iocp;
  157. glp_init_iocp(&iocp);
  158. iocp.msg_lev = GLP_MSG_OFF;
  159. glp_intopt(lp, &iocp);
  160. int stat = glp_mip_status(lp);
  161. /* if we don't have a solution return */
  162. if(stat == GLP_NOFEAS)
  163. {
  164. glp_delete_prob(lp);
  165. lp = NULL;
  166. return 0.0;
  167. }
  168. }
  169. int stat = glp_get_prim_stat(lp);
  170. /* if we don't have a solution return */
  171. if(stat == GLP_NOFEAS)
  172. {
  173. glp_delete_prob(lp);
  174. lp = NULL;
  175. printf("No sol!!!\n");
  176. return 0.0;
  177. }
  178. double res = glp_get_obj_val(lp);
  179. for(s = 0; s < ns; s++)
  180. for(w = 0; w < nw; w++)
  181. {
  182. if (integer)
  183. w_in_s[s][w] = (double)glp_mip_col_val(lp, s*nw+w+1);
  184. else
  185. w_in_s[s][w] = glp_get_col_prim(lp, s*nw+w+1);
  186. }
  187. glp_delete_prob(lp);
  188. return res;
  189. }
  190. static void _try_resizing(unsigned *sched_ctxs, int nsched_ctxs , int *workers, int nworkers)
  191. {
  192. int ns = sched_ctxs == NULL ? sc_hypervisor_get_nsched_ctxs() : nsched_ctxs;
  193. int nw = workers == NULL ? (int)starpu_worker_get_count() : nworkers; /* Number of different workers */
  194. sched_ctxs = sched_ctxs == NULL ? sc_hypervisor_get_sched_ctxs() : sched_ctxs;
  195. double w_in_s[ns][nw];
  196. unsigned found_sol = _compute_max_speed(ns, nw, w_in_s, sched_ctxs, workers);
  197. /* if we did find at least one solution redistribute the resources */
  198. if(found_sol)
  199. {
  200. struct types_of_workers *tw = sc_hypervisor_get_types_of_workers(workers, nw);
  201. int w, s;
  202. double nworkers_per_ctx[ns][tw->nw];
  203. int nworkers_per_ctx_rounded[ns][tw->nw];
  204. for(s = 0; s < ns; s++)
  205. {
  206. for(w = 0; w < nw; w++)
  207. {
  208. nworkers_per_ctx[s][w] = 0.0;
  209. nworkers_per_ctx_rounded[s][w] = 0;
  210. }
  211. }
  212. for(s = 0; s < ns; s++)
  213. {
  214. for(w = 0; w < nw; w++)
  215. {
  216. enum starpu_worker_archtype arch = starpu_worker_get_type(w);
  217. int idx = sc_hypervisor_get_index_for_arch(STARPU_CUDA_WORKER, tw);
  218. nworkers_per_ctx[s][idx] += w_in_s[s][w];
  219. if(arch == STARPU_CUDA_WORKER)
  220. {
  221. if(w_in_s[s][w] >= 0.3)
  222. nworkers_per_ctx_rounded[s][idx]++;
  223. }
  224. else
  225. {
  226. int idx = sc_hypervisor_get_index_for_arch(STARPU_CPU_WORKER, tw);
  227. nworkers_per_ctx[s][idx] += w_in_s[s][w];
  228. if(w_in_s[s][w] > 0.5)
  229. nworkers_per_ctx_rounded[s][idx]++;
  230. }
  231. }
  232. }
  233. /* for(s = 0; s < ns; s++) */
  234. /* printf("%d: cpus = %lf gpus = %lf cpus_round = %d gpus_round = %d\n", s, nworkers[s][1], nworkers[s][0], */
  235. /* nworkers_rounded[s][1], nworkers_rounded[s][0]); */
  236. sc_hypervisor_lp_redistribute_resources_in_ctxs(ns, tw->nw, nworkers_per_ctx_rounded, nworkers_per_ctx, sched_ctxs, tw);
  237. free(tw);
  238. }
  239. }
  240. static void throughput_lp_handle_poped_task(__attribute__((unused))unsigned sched_ctx, __attribute__((unused))int worker,
  241. __attribute__((unused))struct starpu_task *task, __attribute__((unused))uint32_t footprint)
  242. {
  243. int ret = starpu_pthread_mutex_trylock(&act_hypervisor_mutex);
  244. if(ret != EBUSY)
  245. {
  246. unsigned criteria = sc_hypervisor_get_resize_criteria();
  247. if(criteria != SC_NOTHING && criteria == SC_SPEED)
  248. {
  249. if(sc_hypervisor_check_speed_gap_btw_ctxs(NULL, -1, NULL, -1))
  250. {
  251. _try_resizing(NULL, -1, NULL, -1);
  252. }
  253. }
  254. STARPU_PTHREAD_MUTEX_UNLOCK(&act_hypervisor_mutex);
  255. }
  256. }
  257. static void throughput_lp_handle_idle_cycle(unsigned sched_ctx, int worker)
  258. {
  259. int ret = starpu_pthread_mutex_trylock(&act_hypervisor_mutex);
  260. if(ret != EBUSY)
  261. {
  262. unsigned criteria = sc_hypervisor_get_resize_criteria();
  263. if(criteria != SC_NOTHING && criteria == SC_IDLE)
  264. {
  265. if(sc_hypervisor_check_idle(sched_ctx, worker))
  266. {
  267. _try_resizing(NULL, -1, NULL, -1);
  268. // sc_hypervisor_move_workers(sched_ctx, 3 - sched_ctx, &worker, 1, 1);
  269. }
  270. }
  271. STARPU_PTHREAD_MUTEX_UNLOCK(&act_hypervisor_mutex);
  272. }
  273. }
  274. static void throughput_lp_resize_ctxs(unsigned *sched_ctxs, int nsched_ctxs , int *workers, int nworkers)
  275. {
  276. int ret = starpu_pthread_mutex_trylock(&act_hypervisor_mutex);
  277. if(ret != EBUSY)
  278. {
  279. _try_resizing(sched_ctxs, nsched_ctxs, workers, nworkers);
  280. STARPU_PTHREAD_MUTEX_UNLOCK(&act_hypervisor_mutex);
  281. }
  282. }
  283. static void throughput_lp_end_ctx(__attribute__((unused))unsigned sched_ctx)
  284. {
  285. /* struct sc_hypervisor_wrapper* sc_w = sc_hypervisor_get_wrapper(sched_ctx); */
  286. /* int worker; */
  287. /* for(worker = 0; worker < 12; worker++) */
  288. /* printf("%d/%d: speed %lf\n", worker, sched_ctx, sc_w->ref_speed[worker]); */
  289. return;
  290. }
  291. struct sc_hypervisor_policy throughput_lp_policy = {
  292. .size_ctxs = NULL,
  293. .resize_ctxs = throughput_lp_resize_ctxs,
  294. .handle_poped_task = throughput_lp_handle_poped_task,
  295. .handle_pushed_task = NULL,
  296. .handle_idle_cycle = throughput_lp_handle_idle_cycle,
  297. .handle_idle_end = NULL,
  298. .handle_post_exec_hook = NULL,
  299. .handle_submitted_job = NULL,
  300. .end_ctx = throughput_lp_end_ctx,
  301. .init_worker = NULL,
  302. .custom = 0,
  303. .name = "throughput_lp"
  304. };
  305. #endif /* STARPU_HAVE_GLPK_H */