ispeed_lp_policy.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463
  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 <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. struct ispeed_lp_data
  22. {
  23. double **speed;
  24. double *flops;
  25. double **flops_on_w;
  26. int *workers;
  27. };
  28. /*
  29. * GNU Linear Programming Kit backend
  30. */
  31. #ifdef STARPU_HAVE_GLPK_H
  32. #include <glpk.h>
  33. static double _glp_resolve (int ns, int nw, double final_w_in_s[ns][nw],
  34. unsigned is_integer, double tmax, void *specific_data)
  35. {
  36. struct ispeed_lp_data *sd = (struct ispeed_lp_data *)specific_data;
  37. double **speed = sd->speed;
  38. double *flops = sd->flops;
  39. double **final_flops_on_w = sd->flops_on_w;
  40. double w_in_s[ns][nw];
  41. double flops_on_w[ns][nw];
  42. int w, s;
  43. glp_prob *lp;
  44. // printf("try with tmax %lf\n", tmax);
  45. lp = glp_create_prob();
  46. glp_set_prob_name(lp, "StarPU theoretical bound");
  47. glp_set_obj_dir(lp, GLP_MAX);
  48. glp_set_obj_name(lp, "total execution time");
  49. {
  50. int ne = 5 * ns * nw /* worker execution time */
  51. + 1; /* glp dumbness */
  52. int n = 1;
  53. int ia[ne], ja[ne];
  54. double ar[ne];
  55. /* Variables: number of flops assigned to worker w in context s, and
  56. the acknwoledgment that the worker w belongs to the context s */
  57. glp_add_cols(lp, 2*nw*ns);
  58. #define colnum(w, s) ((s)*nw+(w)+1)
  59. for(s = 0; s < ns; s++)
  60. for(w = 0; w < nw; w++)
  61. glp_set_obj_coef(lp, nw*ns+colnum(w,s), 1.);
  62. for(s = 0; s < ns; s++)
  63. for(w = 0; w < nw; w++)
  64. {
  65. char name[32];
  66. snprintf(name, sizeof(name), "flopsw%ds%dn", w, s);
  67. glp_set_col_name(lp, colnum(w,s), name);
  68. glp_set_col_bnds(lp, colnum(w,s), GLP_LO, 0., 0.);
  69. snprintf(name, sizeof(name), "w%ds%dn", w, s);
  70. glp_set_col_name(lp, nw*ns+colnum(w,s), name);
  71. if (is_integer)
  72. {
  73. glp_set_col_kind(lp, nw*ns+colnum(w, s), GLP_IV);
  74. glp_set_col_bnds(lp, nw*ns+colnum(w,s), GLP_DB, 0, 1);
  75. }
  76. else
  77. glp_set_col_bnds(lp, nw*ns+colnum(w,s), GLP_DB, 0.0, 1.0);
  78. }
  79. int curr_row_idx = 0;
  80. /* Total worker execution time */
  81. glp_add_rows(lp, nw*ns);
  82. /*nflops[s][w]/v[s][w] < x[s][w]*tmax */
  83. for(s = 0; s < ns; s++)
  84. {
  85. for (w = 0; w < nw; w++)
  86. {
  87. char name[32], title[64];
  88. starpu_worker_get_name(w, name, sizeof(name));
  89. snprintf(title, sizeof(title), "worker %s", name);
  90. glp_set_row_name(lp, curr_row_idx+s*nw+w+1, title);
  91. /* nflosp[s][w] */
  92. ia[n] = curr_row_idx+s*nw+w+1;
  93. ja[n] = colnum(w, s);
  94. ar[n] = 1 / speed[s][w];
  95. n++;
  96. /* x[s][w] = 1 | 0 */
  97. ia[n] = curr_row_idx+s*nw+w+1;
  98. ja[n] = nw*ns+colnum(w,s);
  99. ar[n] = (-1) * tmax;
  100. n++;
  101. glp_set_row_bnds(lp, curr_row_idx+s*nw+w+1, GLP_UP, 0.0, 0.0);
  102. }
  103. }
  104. curr_row_idx += nw*ns;
  105. /* sum(flops[s][w]) = flops[s] */
  106. glp_add_rows(lp, ns);
  107. for (s = 0; s < ns; s++)
  108. {
  109. char name[32], title[64];
  110. starpu_worker_get_name(w, name, sizeof(name));
  111. snprintf(title, sizeof(title), "flops %lf ctx%d", flops[s], s);
  112. glp_set_row_name(lp, curr_row_idx+s+1, title);
  113. for (w = 0; w < nw; w++)
  114. {
  115. ia[n] = curr_row_idx+s+1;
  116. ja[n] = colnum(w, s);
  117. ar[n] = 1;
  118. n++;
  119. }
  120. glp_set_row_bnds(lp, curr_row_idx+s+1, GLP_FX, flops[s], flops[s]);
  121. }
  122. curr_row_idx += ns;
  123. /* sum(x[s][w]) = 1 */
  124. glp_add_rows(lp, nw);
  125. for (w = 0; w < nw; w++)
  126. {
  127. char name[32], title[64];
  128. starpu_worker_get_name(w, name, sizeof(name));
  129. snprintf(title, sizeof(title), "w%x", w);
  130. glp_set_row_name(lp, curr_row_idx+w+1, title);
  131. for(s = 0; s < ns; s++)
  132. {
  133. ia[n] = curr_row_idx+w+1;
  134. ja[n] = nw*ns+colnum(w,s);
  135. ar[n] = 1;
  136. n++;
  137. }
  138. if(is_integer)
  139. glp_set_row_bnds(lp, curr_row_idx+w+1, GLP_FX, 1, 1);
  140. else
  141. glp_set_row_bnds(lp, curr_row_idx+w+1, GLP_FX, 1.0, 1.0);
  142. }
  143. curr_row_idx += nw;
  144. /* sum(nflops[s][w]) > 0*/
  145. glp_add_rows(lp, nw);
  146. for (w = 0; w < nw; w++)
  147. {
  148. char name[32], title[64];
  149. starpu_worker_get_name(w, name, sizeof(name));
  150. snprintf(title, sizeof(title), "flopsw%x", w);
  151. glp_set_row_name(lp, curr_row_idx+w+1, title);
  152. for(s = 0; s < ns; s++)
  153. {
  154. ia[n] = curr_row_idx+w+1;
  155. ja[n] = colnum(w,s);
  156. ar[n] = 1;
  157. n++;
  158. }
  159. glp_set_row_bnds(lp, curr_row_idx+w+1, GLP_LO, 0.1, 0.);
  160. }
  161. if(n != ne)
  162. printf("ns= %d nw = %d n = %d ne = %d\n", ns, nw, n, ne);
  163. STARPU_ASSERT(n == ne);
  164. glp_load_matrix(lp, ne-1, ia, ja, ar);
  165. }
  166. glp_smcp parm;
  167. glp_init_smcp(&parm);
  168. parm.msg_lev = GLP_MSG_OFF;
  169. int ret = glp_simplex(lp, &parm);
  170. if (ret)
  171. {
  172. glp_delete_prob(lp);
  173. lp = NULL;
  174. return 0.0;
  175. }
  176. if (is_integer)
  177. {
  178. glp_iocp iocp;
  179. glp_init_iocp(&iocp);
  180. iocp.msg_lev = GLP_MSG_OFF;
  181. glp_intopt(lp, &iocp);
  182. int stat = glp_mip_status(lp);
  183. /* if we don't have a solution return */
  184. if(stat == GLP_NOFEAS)
  185. {
  186. glp_delete_prob(lp);
  187. lp = NULL;
  188. return 0.0;
  189. }
  190. }
  191. int stat = glp_get_prim_stat(lp);
  192. /* if we don't have a solution return */
  193. if(stat == GLP_NOFEAS)
  194. {
  195. glp_delete_prob(lp);
  196. lp = NULL;
  197. return 0.0;
  198. }
  199. double res = glp_get_obj_val(lp);
  200. for(s = 0; s < ns; s++)
  201. for(w = 0; w < nw; w++)
  202. {
  203. flops_on_w[s][w] = glp_get_col_prim(lp, colnum(w, s));
  204. if (is_integer)
  205. w_in_s[s][w] = (double)glp_mip_col_val(lp, nw*ns+colnum(w, s));
  206. else
  207. w_in_s[s][w] = glp_get_col_prim(lp, nw*ns+colnum(w,s));
  208. // printf("w_in_s[s%d][w%d] = %lf flops[s%d][w%d] = %lf \n", s, w, w_in_s[s][w], s, w, flops_on_w[s][w]);
  209. }
  210. glp_delete_prob(lp);
  211. for(s = 0; s < ns; s++)
  212. for(w = 0; w < nw; w++)
  213. {
  214. final_w_in_s[s][w] = w_in_s[s][w];
  215. final_flops_on_w[s][w] = flops_on_w[s][w];
  216. }
  217. return res;
  218. }
  219. static unsigned _compute_flops_distribution_over_ctxs(int ns, int nw, double w_in_s[ns][nw], double **flops_on_w, unsigned *in_sched_ctxs, int *workers)
  220. {
  221. // double flops[ns];
  222. // double speed[ns][nw];
  223. double *flops = (double*)malloc(ns*sizeof(double));
  224. double **speed = (double **)malloc(ns*sizeof(double*));
  225. int i;
  226. for(i = 0; i < ns; i++)
  227. speed[i] = (double*)malloc(nw*sizeof(double));
  228. unsigned *sched_ctxs = in_sched_ctxs == NULL ? sc_hypervisor_get_sched_ctxs() : in_sched_ctxs;
  229. int w,s;
  230. struct sc_hypervisor_wrapper* sc_w = NULL;
  231. for(s = 0; s < ns; s++)
  232. {
  233. sc_w = sc_hypervisor_get_wrapper(sched_ctxs[s]);
  234. for(w = 0; w < nw; w++)
  235. {
  236. w_in_s[s][w] = 0.0;
  237. int worker = workers == NULL ? w : workers[w];
  238. speed[s][w] = sc_hypervisor_get_speed_per_worker(sc_w, worker);
  239. if(speed[s][w] == -1.0)
  240. {
  241. enum starpu_worker_archtype arch = starpu_worker_get_type(worker);
  242. speed[s][w] = sc_hypervisor_get_speed(sc_w, arch);
  243. if(arch == STARPU_CUDA_WORKER)
  244. {
  245. unsigned worker_in_ctx = starpu_sched_ctx_contains_worker(worker, sc_w->sched_ctx);
  246. if(!worker_in_ctx)
  247. {
  248. double transfer_speed = starpu_get_bandwidth_RAM_CUDA(worker) / 1000;
  249. speed[s][w] = (speed[s][w] * transfer_speed) / (speed[s][w] + transfer_speed);
  250. }
  251. }
  252. }
  253. // printf("v[w%d][s%d] = %lf\n",w, s, speed[s][w]);
  254. }
  255. struct sc_hypervisor_policy_config *config = sc_hypervisor_get_config(sched_ctxs[s]);
  256. flops[s] = config->ispeed_ctx_sample/1000000000; /* in gflops */
  257. }
  258. /* take the exec time of the slowest ctx
  259. as starting point and then try to minimize it
  260. as increasing it a little for the faster ctxs */
  261. double tmax = sc_hypervisor_get_slowest_ctx_exec_time();
  262. double smallest_tmax = sc_hypervisor_get_fastest_ctx_exec_time(); //tmax - 0.5*tmax;
  263. // printf("tmax %lf smallest %lf\n", tmax, smallest_tmax);
  264. double tmin = 0.0;
  265. struct ispeed_lp_data specific_data;
  266. specific_data.speed = speed;
  267. specific_data.flops = flops;
  268. specific_data.flops_on_w = flops_on_w;
  269. specific_data.workers = workers;
  270. unsigned found_sol = sc_hypervisor_lp_execute_dichotomy(ns, nw, w_in_s, 1, (void*)&specific_data,
  271. tmin, tmax, smallest_tmax, _glp_resolve);
  272. for(i = 0; i < ns; i++)
  273. free(speed[i]);
  274. free(speed);
  275. return found_sol;
  276. }
  277. static void _try_resizing(unsigned *sched_ctxs, int nsched_ctxs , int *workers, int nworkers)
  278. {
  279. starpu_trace_user_event(2);
  280. int ns = sched_ctxs == NULL ? sc_hypervisor_get_nsched_ctxs() : nsched_ctxs;
  281. unsigned *curr_sched_ctxs = sched_ctxs == NULL ? sc_hypervisor_get_sched_ctxs() : sched_ctxs;
  282. unsigned curr_nworkers = nworkers == -1 ? starpu_worker_get_count() : (unsigned)nworkers;
  283. struct types_of_workers *tw = sc_hypervisor_get_types_of_workers(workers, curr_nworkers);
  284. int nw = tw->nw;
  285. double w_in_s[ns][nw];
  286. // double flops_on_w[ns][nw];
  287. double **flops_on_w = (double**)malloc(ns*sizeof(double*));
  288. int i;
  289. for(i = 0; i < ns; i++)
  290. flops_on_w[i] = (double*)malloc(nw*sizeof(double));
  291. unsigned found_sol = _compute_flops_distribution_over_ctxs(ns, nw, w_in_s, flops_on_w, curr_sched_ctxs, workers);
  292. /* if we did find at least one solution redistribute the resources */
  293. if(found_sol)
  294. {
  295. int w, s;
  296. double nworkers_per_ctx[ns][nw];
  297. int nworkers_per_ctx_rounded[ns][nw];
  298. for(s = 0; s < ns; s++)
  299. {
  300. for(w = 0; w < nw; w++)
  301. {
  302. nworkers_per_ctx[s][w] = 0.0;
  303. nworkers_per_ctx_rounded[s][w] = 0;
  304. }
  305. }
  306. for(s = 0; s < ns; s++)
  307. {
  308. for(w = 0; w < nw; w++)
  309. {
  310. enum starpu_worker_archtype arch = starpu_worker_get_type(w);
  311. int idx = sc_hypervisor_get_index_for_arch(arch, tw);
  312. nworkers_per_ctx[s][idx] += w_in_s[s][w];
  313. if(arch == STARPU_CUDA_WORKER)
  314. {
  315. if(w_in_s[s][w] >= 0.3)
  316. nworkers_per_ctx_rounded[s][0]++;
  317. }
  318. else
  319. {
  320. if(w_in_s[s][w] > 0.5)
  321. nworkers_per_ctx_rounded[s][1]++;
  322. }
  323. }
  324. }
  325. /* for(s = 0; s < ns; s++) */
  326. /* printf("%d: cpus = %lf gpus = %lf cpus_round = %d gpus_round = %d\n", s, nworkers[s][1], nworkers[s][0], */
  327. /* nworkers_rounded[s][1], nworkers_rounded[s][0]); */
  328. sc_hypervisor_lp_redistribute_resources_in_ctxs(ns, nw, nworkers_per_ctx_rounded, nworkers_per_ctx, curr_sched_ctxs, tw);
  329. }
  330. for(i = 0; i < ns; i++)
  331. free(flops_on_w[i]);
  332. free(flops_on_w);
  333. }
  334. static void ispeed_lp_handle_poped_task(__attribute__((unused))unsigned sched_ctx, __attribute__((unused))int worker,
  335. __attribute__((unused))struct starpu_task *task, __attribute__((unused))uint32_t footprint)
  336. {
  337. int ret = starpu_pthread_mutex_trylock(&act_hypervisor_mutex);
  338. if(ret != EBUSY)
  339. {
  340. unsigned criteria = sc_hypervisor_get_resize_criteria();
  341. if(criteria != SC_NOTHING && criteria == SC_SPEED)
  342. {
  343. if(sc_hypervisor_check_speed_gap_btw_ctxs())
  344. {
  345. _try_resizing(NULL, -1, NULL, -1);
  346. }
  347. }
  348. starpu_pthread_mutex_unlock(&act_hypervisor_mutex);
  349. }
  350. }
  351. static void ispeed_lp_handle_idle_cycle(unsigned sched_ctx, int worker)
  352. {
  353. int ret = starpu_pthread_mutex_trylock(&act_hypervisor_mutex);
  354. if(ret != EBUSY)
  355. {
  356. unsigned criteria = sc_hypervisor_get_resize_criteria();
  357. if(criteria != SC_NOTHING && criteria == SC_IDLE)
  358. {
  359. if(sc_hypervisor_check_idle(sched_ctx, worker))
  360. {
  361. _try_resizing(NULL, -1, NULL, -1);
  362. // sc_hypervisor_move_workers(sched_ctx, 3 - sched_ctx, &worker, 1, 1);
  363. }
  364. }
  365. starpu_pthread_mutex_unlock(&act_hypervisor_mutex);
  366. }
  367. }
  368. static void ispeed_lp_resize_ctxs(unsigned *sched_ctxs, int nsched_ctxs , int *workers, int nworkers)
  369. {
  370. int ret = starpu_pthread_mutex_trylock(&act_hypervisor_mutex);
  371. if(ret != EBUSY)
  372. {
  373. _try_resizing(sched_ctxs, nsched_ctxs, workers, nworkers);
  374. starpu_pthread_mutex_unlock(&act_hypervisor_mutex);
  375. }
  376. }
  377. static void ispeed_lp_end_ctx(unsigned sched_ctx)
  378. {
  379. /* struct sc_hypervisor_wrapper* sc_w = sc_hypervisor_get_wrapper(sched_ctx); */
  380. /* int worker; */
  381. /* for(worker = 0; worker < 12; worker++) */
  382. /* printf("%d/%d: speed %lf\n", worker, sched_ctx, sc_w->ref_speed[worker]); */
  383. return;
  384. }
  385. struct sc_hypervisor_policy ispeed_lp_policy = {
  386. .size_ctxs = NULL,
  387. .resize_ctxs = ispeed_lp_resize_ctxs,
  388. .handle_poped_task = ispeed_lp_handle_poped_task,
  389. .handle_pushed_task = NULL,
  390. .handle_idle_cycle = ispeed_lp_handle_idle_cycle,
  391. .handle_idle_end = NULL,
  392. .handle_post_exec_hook = NULL,
  393. .handle_submitted_job = NULL,
  394. .end_ctx = ispeed_lp_end_ctx,
  395. .custom = 0,
  396. .name = "ispeed_lp"
  397. };
  398. #endif /* STARPU_HAVE_GLPK_H */