lp_programs.c 19 KB

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