lp_programs.c 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725
  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. /*
  17. * GNU Linear Programming Kit backend
  18. */
  19. #include "sc_hypervisor_policy.h"
  20. #include "sc_hypervisor_lp.h"
  21. #ifdef STARPU_HAVE_GLPK_H
  22. double sc_hypervisor_lp_simulate_distrib_tasks(int ns, int nw, int nt, double w_in_s[ns][nw], double tasks[nw][nt],
  23. double times[nw][nt], unsigned is_integer, double tmax, unsigned *in_sched_ctxs,
  24. struct sc_hypervisor_policy_task_pool *tmp_task_pools)
  25. {
  26. struct sc_hypervisor_policy_task_pool * tp;
  27. int t, w, s;
  28. glp_prob *lp;
  29. lp = glp_create_prob();
  30. glp_set_prob_name(lp, "StarPU theoretical bound");
  31. glp_set_obj_dir(lp, GLP_MAX);
  32. glp_set_obj_name(lp, "total execution time");
  33. {
  34. int ne = nt * nw /* worker execution time */
  35. + nw * ns
  36. + nw * (nt + ns)
  37. + 1; /* glp dumbness */
  38. int n = 1;
  39. int ia[ne], ja[ne];
  40. double ar[ne];
  41. /* Variables: number of tasks i assigned to worker j, and tmax */
  42. glp_add_cols(lp, nw*nt+ns*nw);
  43. #define colnum(w, t) ((t)*nw+(w)+1)
  44. for(s = 0; s < ns; s++)
  45. for(w = 0; w < nw; w++)
  46. glp_set_obj_coef(lp, nw*nt+s*nw+w+1, 1.);
  47. for (w = 0; w < nw; w++)
  48. for (t = 0; t < nt; t++)
  49. {
  50. char name[32];
  51. snprintf(name, sizeof(name), "w%dt%dn", w, t);
  52. glp_set_col_name(lp, colnum(w, t), name);
  53. if (is_integer)
  54. {
  55. glp_set_col_kind(lp, colnum(w, t), GLP_IV);
  56. glp_set_col_bnds(lp, colnum(w, t), GLP_LO, 0, 0);
  57. }
  58. else
  59. glp_set_col_bnds(lp, colnum(w, t), GLP_LO, 0.0, 0.0);
  60. }
  61. for(s = 0; s < ns; s++)
  62. for(w = 0; w < nw; w++)
  63. {
  64. char name[32];
  65. snprintf(name, sizeof(name), "w%ds%dn", w, s);
  66. glp_set_col_name(lp, nw*nt+s*nw+w+1, name);
  67. if (is_integer)
  68. {
  69. glp_set_col_kind(lp, nw*nt+s*nw+w+1, GLP_IV);
  70. glp_set_col_bnds(lp, nw*nt+s*nw+w+1, GLP_DB, 0, 1);
  71. }
  72. else
  73. glp_set_col_bnds(lp, nw*nt+s*nw+w+1, GLP_DB, 0.0, 1.0);
  74. }
  75. unsigned *sched_ctxs = in_sched_ctxs == NULL ? sc_hypervisor_get_sched_ctxs() : in_sched_ctxs;
  76. int curr_row_idx = 0;
  77. /* Total worker execution time */
  78. glp_add_rows(lp, nw*ns);
  79. for (t = 0; t < nt; t++)
  80. {
  81. int someone = 0;
  82. for (w = 0; w < nw; w++)
  83. if (!isnan(times[w][t]))
  84. someone = 1;
  85. if (!someone)
  86. {
  87. /* This task does not have any performance model at all, abort */
  88. printf("NO PERF MODELS\n");
  89. glp_delete_prob(lp);
  90. return 0.0;
  91. }
  92. }
  93. /*sum(t[t][w]*n[t][w]) < x[s][w]*tmax */
  94. for(s = 0; s < ns; s++)
  95. {
  96. for (w = 0; w < nw; w++)
  97. {
  98. char name[32], title[64];
  99. starpu_worker_get_name(w, name, sizeof(name));
  100. snprintf(title, sizeof(title), "worker %s", name);
  101. glp_set_row_name(lp, curr_row_idx+s*nw+w+1, title);
  102. for (t = 0, tp = tmp_task_pools; tp; t++, tp = tp->next)
  103. {
  104. if(tp->sched_ctx_id == sched_ctxs[s])
  105. {
  106. ia[n] = curr_row_idx+s*nw+w+1;
  107. ja[n] = colnum(w, t);
  108. if (isnan(times[w][t]))
  109. {
  110. printf("had to insert huge val \n");
  111. ar[n] = 1000000000.;
  112. }
  113. else
  114. ar[n] = times[w][t];
  115. n++;
  116. }
  117. }
  118. /* x[s][w] = 1 | 0 */
  119. ia[n] = curr_row_idx+s*nw+w+1;
  120. ja[n] = nw*nt+s*nw+w+1;
  121. ar[n] = (-1) * tmax;
  122. n++;
  123. if (is_integer)
  124. {
  125. glp_set_row_bnds(lp, curr_row_idx+s*nw+w+1, GLP_UP, 0, 0);
  126. }
  127. else
  128. glp_set_row_bnds(lp, curr_row_idx+s*nw+w+1, GLP_UP, 0.0, 0.0);
  129. }
  130. }
  131. curr_row_idx += nw*ns;
  132. /* Total task completion */
  133. glp_add_rows(lp, nt);
  134. for (t = 0, tp = tmp_task_pools; tp; t++, tp = tp->next)
  135. {
  136. char name[32], title[64];
  137. starpu_worker_get_name(w, name, sizeof(name));
  138. snprintf(title, sizeof(title), "task %s key %x", tp->cl->name, (unsigned) tp->footprint);
  139. glp_set_row_name(lp, curr_row_idx+t+1, title);
  140. for (w = 0; w < nw; w++)
  141. {
  142. ia[n] = curr_row_idx+t+1;
  143. ja[n] = colnum(w, t);
  144. ar[n] = 1;
  145. n++;
  146. }
  147. glp_set_row_bnds(lp, curr_row_idx+t+1, GLP_FX, tp->n, tp->n);
  148. }
  149. curr_row_idx += nt;
  150. /* sum(x[s][i]) = 1 */
  151. glp_add_rows(lp, nw);
  152. for (w = 0; w < nw; w++)
  153. {
  154. char name[32], title[64];
  155. starpu_worker_get_name(w, name, sizeof(name));
  156. snprintf(title, sizeof(title), "w%x", w);
  157. glp_set_row_name(lp, curr_row_idx+w+1, title);
  158. for(s = 0; s < ns; s++)
  159. {
  160. ia[n] = curr_row_idx+w+1;
  161. ja[n] = nw*nt+s*nw+w+1;
  162. ar[n] = 1;
  163. n++;
  164. }
  165. if(is_integer)
  166. glp_set_row_bnds(lp, curr_row_idx+w+1, GLP_FX, 1, 1);
  167. else
  168. glp_set_row_bnds(lp, curr_row_idx+w+1, GLP_FX, 1.0, 1.0);
  169. }
  170. if(n != ne)
  171. printf("ns= %d nw = %d nt = %d n = %d ne = %d\n", ns, nw, nt, n, ne);
  172. STARPU_ASSERT(n == ne);
  173. glp_load_matrix(lp, ne-1, ia, ja, ar);
  174. }
  175. glp_smcp parm;
  176. glp_init_smcp(&parm);
  177. parm.msg_lev = GLP_MSG_OFF;
  178. int ret = glp_simplex(lp, &parm);
  179. /* char str[50]; */
  180. /* sprintf(str, "outpu_lp_%g", tmax); */
  181. /* glp_print_sol(lp, str); */
  182. if (ret)
  183. {
  184. printf("error in simplex\n");
  185. glp_delete_prob(lp);
  186. lp = NULL;
  187. return 0.0;
  188. }
  189. int stat = glp_get_prim_stat(lp);
  190. /* if we don't have a solution return */
  191. if(stat == GLP_NOFEAS)
  192. {
  193. glp_delete_prob(lp);
  194. // printf("no_sol in tmax = %lf\n", tmax);
  195. lp = NULL;
  196. return 0.0;
  197. }
  198. if (is_integer)
  199. {
  200. glp_iocp iocp;
  201. glp_init_iocp(&iocp);
  202. iocp.msg_lev = GLP_MSG_OFF;
  203. // iocp.tm_lim = 1000;
  204. glp_intopt(lp, &iocp);
  205. int stat = glp_mip_status(lp);
  206. /* if we don't have a solution return */
  207. if(stat == GLP_NOFEAS || stat == GLP_ETMLIM || stat == GLP_UNDEF)
  208. {
  209. // printf("no int sol in tmax = %lf\n", tmax);
  210. if(stat == GLP_ETMLIM || stat == GLP_UNDEF)
  211. printf("timeout \n");
  212. glp_delete_prob(lp);
  213. lp = NULL;
  214. return 0.0;
  215. }
  216. }
  217. double res = glp_get_obj_val(lp);
  218. for (w = 0; w < nw; w++)
  219. for (t = 0; t < nt; t++)
  220. if (is_integer)
  221. tasks[w][t] = (double)glp_mip_col_val(lp, colnum(w, t));
  222. else
  223. tasks[w][t] = glp_get_col_prim(lp, colnum(w, t));
  224. /* printf("**********************************************\n"); */
  225. /* printf("for tmax %lf\n", tmax); */
  226. for(s = 0; s < ns; s++)
  227. for(w = 0; w < nw; w++)
  228. {
  229. if (is_integer)
  230. w_in_s[s][w] = (double)glp_mip_col_val(lp, nw*nt+s*nw+w+1);
  231. else
  232. w_in_s[s][w] = glp_get_col_prim(lp, nw*nt+s*nw+w+1);
  233. // printf("w %d in ctx %d = %lf\n", w, s, w_in_s[s][w]);
  234. }
  235. /* printf("\n"); */
  236. /* printf("**********************************************\n"); */
  237. glp_delete_prob(lp);
  238. return res;
  239. }
  240. double sc_hypervisor_lp_simulate_distrib_flops(int ns, int nw, double v[ns][nw], double flops[ns], double res[ns][nw],
  241. int total_nw[nw], unsigned sched_ctxs[ns], double last_vmax)
  242. {
  243. int integer = 1;
  244. int s, w;
  245. glp_prob *lp;
  246. int ne = (ns*nw+1)*(ns+nw)
  247. + 1; /* glp dumbness */
  248. int n = 1;
  249. int ia[ne], ja[ne];
  250. double ar[ne];
  251. lp = glp_create_prob();
  252. glp_set_prob_name(lp, "sample");
  253. glp_set_obj_dir(lp, GLP_MAX);
  254. glp_set_obj_name(lp, "max speed");
  255. /* we add nw*ns columns one for each type of worker in each context
  256. and another column corresponding to the 1/tmax bound (bc 1/tmax is a variable too)*/
  257. glp_add_cols(lp, nw*ns+1);
  258. /* struct sc_hypervisor_wrapper *sc_w = NULL; */
  259. for(s = 0; s < ns; s++)
  260. {
  261. /* sc_w = sc_hypervisor_get_wrapper(sched_ctxs[s]); */
  262. struct sc_hypervisor_policy_config *config = sc_hypervisor_get_config(sched_ctxs[s]);
  263. for(w = 0; w < nw; w++)
  264. {
  265. char name[32];
  266. snprintf(name, sizeof(name), "worker%dctx%d", w, s);
  267. glp_set_col_name(lp, n, name);
  268. if (integer)
  269. {
  270. glp_set_col_kind(lp, n, GLP_IV);
  271. /* if(sc_w->consider_max) */
  272. /* { */
  273. /* if(config->max_nworkers == 0) */
  274. /* glp_set_col_bnds(lp, n, GLP_FX, config->min_nworkers, config->max_nworkers); */
  275. /* else */
  276. /* glp_set_col_bnds(lp, n, GLP_DB, config->min_nworkers, config->max_nworkers); */
  277. /* } */
  278. /* else */
  279. {
  280. if(total_nw[w] == 0)
  281. glp_set_col_bnds(lp, n, GLP_FX, config->min_nworkers, total_nw[w]);
  282. else
  283. glp_set_col_bnds(lp, n, GLP_DB, config->min_nworkers, total_nw[w]);
  284. }
  285. }
  286. else
  287. {
  288. /* if(sc_w->consider_max) */
  289. /* { */
  290. /* if(config->max_nworkers == 0) */
  291. /* glp_set_col_bnds(lp, n, GLP_FX, config->min_nworkers*1.0, config->max_nworkers*1.0); */
  292. /* else */
  293. /* glp_set_col_bnds(lp, n, GLP_DB, config->min_nworkers*1.0, config->max_nworkers*1.0); */
  294. /* #ifdef STARPU_SC_HYPERVISOR_DEBUG */
  295. /* printf("%d****************consider max %lf in lp\n", sched_ctxs[s], config->max_nworkers*1.0); */
  296. /* #endif */
  297. /* } */
  298. /* else */
  299. {
  300. if(total_nw[w] == 0)
  301. glp_set_col_bnds(lp, n, GLP_FX, config->min_nworkers*1.0, total_nw[w]*1.0);
  302. else
  303. glp_set_col_bnds(lp, n, GLP_DB, config->min_nworkers*1.0, total_nw[w]*1.0);
  304. #ifdef STARPU_SC_HYPERVISOR_DEBUG
  305. printf("%d****************don't consider max %d but total %d in lp\n", sched_ctxs[s], config->max_nworkers, total_nw[w]);
  306. #endif
  307. }
  308. }
  309. n++;
  310. }
  311. }
  312. #ifdef STARPU_SC_HYPERVISOR_DEBUG
  313. printf("ns = %d nw = %d\n", ns, nw);
  314. #endif
  315. /*1/tmax should belong to the interval [0.0;1.0]*/
  316. glp_set_col_name(lp, n, "vmax");
  317. // glp_set_col_bnds(lp, n, GLP_DB, 0.0, 1.0);
  318. if(last_vmax != -1.0)
  319. glp_set_col_bnds(lp, n, GLP_LO, last_vmax, last_vmax);
  320. else
  321. glp_set_col_bnds(lp, n, GLP_LO, 0.0, 0.0);
  322. /* Z = 1/tmax -> 1/tmax structural variable, nCPUs & nGPUs in ctx are auxiliar variables */
  323. glp_set_obj_coef(lp, n, 1.0);
  324. n = 1;
  325. /* one row corresponds to one ctx*/
  326. glp_add_rows(lp, ns);
  327. for(s = 0; s < ns; s++)
  328. {
  329. char name[32];
  330. snprintf(name, sizeof(name), "ctx%d", s);
  331. glp_set_row_name(lp, s+1, name);
  332. glp_set_row_bnds(lp, s+1, GLP_LO, 0., 0.);
  333. for(w = 0; w < nw; w++)
  334. {
  335. int s2;
  336. for(s2 = 0; s2 < ns; s2++)
  337. {
  338. if(s2 == s)
  339. {
  340. ia[n] = s+1;
  341. ja[n] = w + nw*s2 + 1;
  342. ar[n] = v[s][w];
  343. // printf("ia[%d]=%d ja[%d]=%d ar[%d]=%lf\n", n, ia[n], n, ja[n], n, ar[n]);
  344. }
  345. else
  346. {
  347. ia[n] = s+1;
  348. ja[n] = w + nw*s2 + 1;
  349. ar[n] = 0.0;
  350. // printf("ia[%d]=%d ja[%d]=%d ar[%d]=%lf\n", n, ia[n], n, ja[n], n, ar[n]);
  351. }
  352. n++;
  353. }
  354. }
  355. /* 1/tmax */
  356. ia[n] = s+1;
  357. ja[n] = ns*nw+1;
  358. ar[n] = (-1) * flops[s];
  359. // printf("ia[%d]=%d ja[%d]=%d ar[%d]=%lf\n", n, ia[n], n, ja[n], n, ar[n]);
  360. n++;
  361. }
  362. /*we add another linear constraint : sum(all cpus) = 9 and sum(all gpus) = 3 */
  363. glp_add_rows(lp, nw);
  364. for(w = 0; w < nw; w++)
  365. {
  366. char name[32];
  367. snprintf(name, sizeof(name), "w%d", w);
  368. glp_set_row_name(lp, ns+w+1, name);
  369. for(s = 0; s < ns; s++)
  370. {
  371. int w2;
  372. for(w2 = 0; w2 < nw; w2++)
  373. {
  374. if(w2 == w)
  375. {
  376. ia[n] = ns+w+1;
  377. ja[n] = w2+s*nw + 1;
  378. ar[n] = 1.0;
  379. // printf("ia[%d]=%d ja[%d]=%d ar[%d]=%lf\n", n, ia[n], n, ja[n], n, ar[n]);
  380. }
  381. else
  382. {
  383. ia[n] = ns+w+1;
  384. ja[n] = w2+s*nw + 1;
  385. ar[n] = 0.0;
  386. // printf("ia[%d]=%d ja[%d]=%d ar[%d]=%lf\n", n, ia[n], n, ja[n], n, ar[n]);
  387. }
  388. n++;
  389. }
  390. }
  391. /* 1/tmax */
  392. ia[n] = ns+w+1;
  393. ja[n] = ns*nw+1;
  394. ar[n] = 0.0;
  395. // printf("ia[%d]=%d ja[%d]=%d ar[%d]=%lf\n", n, ia[n], n, ja[n], n, ar[n]);
  396. n++;
  397. /*sum(all gpus) = 3*/
  398. if(w == 0)
  399. glp_set_row_bnds(lp, ns+w+1, GLP_FX, total_nw[0], total_nw[0]);
  400. /*sum(all cpus) = 9*/
  401. if(w == 1)
  402. glp_set_row_bnds(lp, ns+w+1, GLP_FX, total_nw[1], total_nw[1]);
  403. }
  404. STARPU_ASSERT(n == ne);
  405. glp_load_matrix(lp, ne-1, ia, ja, ar);
  406. glp_smcp parm;
  407. glp_init_smcp(&parm);
  408. parm.msg_lev = GLP_MSG_OFF;
  409. int ret = glp_simplex(lp, &parm);
  410. if (ret)
  411. {
  412. printf("error in simplex\n");
  413. glp_delete_prob(lp);
  414. lp = NULL;
  415. return 0.0;
  416. }
  417. int stat = glp_get_prim_stat(lp);
  418. /* if we don't have a solution return */
  419. if(stat == GLP_NOFEAS)
  420. {
  421. glp_delete_prob(lp);
  422. printf("no_sol\n");
  423. lp = NULL;
  424. return 0.0;
  425. }
  426. if (integer)
  427. {
  428. glp_iocp iocp;
  429. glp_init_iocp(&iocp);
  430. iocp.msg_lev = GLP_MSG_OFF;
  431. glp_intopt(lp, &iocp);
  432. int stat = glp_mip_status(lp);
  433. /* if we don't have a solution return */
  434. if(stat == GLP_NOFEAS)
  435. {
  436. printf("no int sol\n");
  437. glp_delete_prob(lp);
  438. lp = NULL;
  439. return 0.0;
  440. }
  441. }
  442. double vmax = glp_get_obj_val(lp);
  443. #ifdef STARPU_SC_HYPERVISOR_DEBUG
  444. printf("vmax = %lf \n", vmax);
  445. #endif
  446. n = 1;
  447. for(s = 0; s < ns; s++)
  448. {
  449. for(w = 0; w < nw; w++)
  450. {
  451. if (integer)
  452. res[s][w] = (double)glp_mip_col_val(lp, n);
  453. else
  454. res[s][w] = glp_get_col_prim(lp, n);
  455. #ifdef STARPU_SC_HYPERVISOR_DEBUG
  456. printf("%d/%d: res %lf flops = %lf v = %lf\n", w,s, res[s][w], flops[s], v[s][w]);
  457. #endif
  458. n++;
  459. }
  460. }
  461. glp_delete_prob(lp);
  462. return vmax;
  463. }
  464. double sc_hypervisor_lp_simulate_distrib_flops_on_sample(int ns, int nw, double final_w_in_s[ns][nw], unsigned is_integer, double tmax,
  465. double **speed, double flops[ns], double **final_flops_on_w)
  466. {
  467. double w_in_s[ns][nw];
  468. double flops_on_w[ns][nw];
  469. int w, s;
  470. glp_prob *lp;
  471. // printf("try with tmax %lf\n", tmax);
  472. lp = glp_create_prob();
  473. glp_set_prob_name(lp, "StarPU theoretical bound");
  474. glp_set_obj_dir(lp, GLP_MAX);
  475. glp_set_obj_name(lp, "total execution time");
  476. {
  477. int ne = 5 * ns * nw /* worker execution time */
  478. + 1; /* glp dumbness */
  479. int n = 1;
  480. int ia[ne], ja[ne];
  481. double ar[ne];
  482. /* Variables: number of flops assigned to worker w in context s, and
  483. the acknwoledgment that the worker w belongs to the context s */
  484. glp_add_cols(lp, 2*nw*ns);
  485. #define colnum_sample(w, s) ((s)*nw+(w)+1)
  486. for(s = 0; s < ns; s++)
  487. for(w = 0; w < nw; w++)
  488. glp_set_obj_coef(lp, nw*ns+colnum_sample(w,s), 1.);
  489. for(s = 0; s < ns; s++)
  490. for(w = 0; w < nw; w++)
  491. {
  492. char name[32];
  493. snprintf(name, sizeof(name), "flopsw%ds%dn", w, s);
  494. glp_set_col_name(lp, colnum_sample(w,s), name);
  495. glp_set_col_bnds(lp, colnum_sample(w,s), GLP_LO, 0., 0.);
  496. snprintf(name, sizeof(name), "w%ds%dn", w, s);
  497. glp_set_col_name(lp, nw*ns+colnum_sample(w,s), name);
  498. if (is_integer)
  499. {
  500. glp_set_col_kind(lp, nw*ns+colnum_sample(w, s), GLP_IV);
  501. glp_set_col_bnds(lp, nw*ns+colnum_sample(w,s), GLP_DB, 0, 1);
  502. }
  503. else
  504. glp_set_col_bnds(lp, nw*ns+colnum_sample(w,s), GLP_DB, 0.0, 1.0);
  505. }
  506. int curr_row_idx = 0;
  507. /* Total worker execution time */
  508. glp_add_rows(lp, nw*ns);
  509. /*nflops[s][w]/v[s][w] < x[s][w]*tmax */
  510. for(s = 0; s < ns; s++)
  511. {
  512. for (w = 0; w < nw; w++)
  513. {
  514. char name[32], title[64];
  515. starpu_worker_get_name(w, name, sizeof(name));
  516. snprintf(title, sizeof(title), "worker %s", name);
  517. glp_set_row_name(lp, curr_row_idx+s*nw+w+1, title);
  518. /* nflosp[s][w] */
  519. ia[n] = curr_row_idx+s*nw+w+1;
  520. ja[n] = colnum_sample(w, s);
  521. ar[n] = 1 / speed[s][w];
  522. n++;
  523. /* x[s][w] = 1 | 0 */
  524. ia[n] = curr_row_idx+s*nw+w+1;
  525. ja[n] = nw*ns+colnum_sample(w,s);
  526. ar[n] = (-1) * tmax;
  527. n++;
  528. glp_set_row_bnds(lp, curr_row_idx+s*nw+w+1, GLP_UP, 0.0, 0.0);
  529. }
  530. }
  531. curr_row_idx += nw*ns;
  532. /* sum(flops[s][w]) = flops[s] */
  533. glp_add_rows(lp, ns);
  534. for (s = 0; s < ns; s++)
  535. {
  536. char name[32], title[64];
  537. starpu_worker_get_name(w, name, sizeof(name));
  538. snprintf(title, sizeof(title), "flops %lf ctx%d", flops[s], s);
  539. glp_set_row_name(lp, curr_row_idx+s+1, title);
  540. for (w = 0; w < nw; w++)
  541. {
  542. ia[n] = curr_row_idx+s+1;
  543. ja[n] = colnum_sample(w, s);
  544. ar[n] = 1;
  545. n++;
  546. }
  547. glp_set_row_bnds(lp, curr_row_idx+s+1, GLP_FX, flops[s], flops[s]);
  548. }
  549. curr_row_idx += ns;
  550. /* sum(x[s][w]) = 1 */
  551. glp_add_rows(lp, nw);
  552. for (w = 0; w < nw; w++)
  553. {
  554. char name[32], title[64];
  555. starpu_worker_get_name(w, name, sizeof(name));
  556. snprintf(title, sizeof(title), "w%x", w);
  557. glp_set_row_name(lp, curr_row_idx+w+1, title);
  558. for(s = 0; s < ns; s++)
  559. {
  560. ia[n] = curr_row_idx+w+1;
  561. ja[n] = nw*ns+colnum_sample(w,s);
  562. ar[n] = 1;
  563. n++;
  564. }
  565. if(is_integer)
  566. glp_set_row_bnds(lp, curr_row_idx+w+1, GLP_FX, 1, 1);
  567. else
  568. glp_set_row_bnds(lp, curr_row_idx+w+1, GLP_FX, 1.0, 1.0);
  569. }
  570. curr_row_idx += nw;
  571. /* sum(nflops[s][w]) > 0*/
  572. glp_add_rows(lp, nw);
  573. for (w = 0; w < nw; w++)
  574. {
  575. char name[32], title[64];
  576. starpu_worker_get_name(w, name, sizeof(name));
  577. snprintf(title, sizeof(title), "flopsw%x", w);
  578. glp_set_row_name(lp, curr_row_idx+w+1, title);
  579. for(s = 0; s < ns; s++)
  580. {
  581. ia[n] = curr_row_idx+w+1;
  582. ja[n] = colnum_sample(w,s);
  583. ar[n] = 1;
  584. n++;
  585. }
  586. glp_set_row_bnds(lp, curr_row_idx+w+1, GLP_LO, 0.1, 0.);
  587. }
  588. if(n != ne)
  589. printf("ns= %d nw = %d n = %d ne = %d\n", ns, nw, n, ne);
  590. STARPU_ASSERT(n == ne);
  591. glp_load_matrix(lp, ne-1, ia, ja, ar);
  592. }
  593. glp_smcp parm;
  594. glp_init_smcp(&parm);
  595. parm.msg_lev = GLP_MSG_OFF;
  596. int ret = glp_simplex(lp, &parm);
  597. if (ret)
  598. {
  599. glp_delete_prob(lp);
  600. lp = NULL;
  601. return 0.0;
  602. }
  603. if (is_integer)
  604. {
  605. glp_iocp iocp;
  606. glp_init_iocp(&iocp);
  607. iocp.msg_lev = GLP_MSG_OFF;
  608. glp_intopt(lp, &iocp);
  609. int stat = glp_mip_status(lp);
  610. /* if we don't have a solution return */
  611. if(stat == GLP_NOFEAS)
  612. {
  613. glp_delete_prob(lp);
  614. lp = NULL;
  615. return 0.0;
  616. }
  617. }
  618. int stat = glp_get_prim_stat(lp);
  619. /* if we don't have a solution return */
  620. if(stat == GLP_NOFEAS)
  621. {
  622. glp_delete_prob(lp);
  623. lp = NULL;
  624. return 0.0;
  625. }
  626. double res = glp_get_obj_val(lp);
  627. for(s = 0; s < ns; s++)
  628. for(w = 0; w < nw; w++)
  629. {
  630. flops_on_w[s][w] = glp_get_col_prim(lp, colnum_sample(w, s));
  631. if (is_integer)
  632. w_in_s[s][w] = (double)glp_mip_col_val(lp, nw*ns+colnum_sample(w, s));
  633. else
  634. w_in_s[s][w] = glp_get_col_prim(lp, nw*ns+colnum_sample(w,s));
  635. // 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]);
  636. }
  637. glp_delete_prob(lp);
  638. for(s = 0; s < ns; s++)
  639. for(w = 0; w < nw; w++)
  640. {
  641. final_w_in_s[s][w] = w_in_s[s][w];
  642. final_flops_on_w[s][w] = flops_on_w[s][w];
  643. }
  644. return res;
  645. }
  646. #endif // STARPU_HAVE_GLPK_H