2 * Minimizing copies with an exact algorithm using mixed integer programming (MIP).
3 * Problem statement as a 'quadratic 0-1 program with linear constraints' with
4 * n binary variables. Constraints are knapsack (enforce color for each node) and
5 * cliques of ifg (interference constraints).
6 * Transformation into a 'mixed integer program' with n binary variables and
7 * additional 2n real variables. Constraints are the above the transformed
8 * objective function and 'complementary conditions' for two var classes.
11 * NOTE: Unfortunately no good solver is available locally (or even for linking)
12 * We use CPLEX 9.0 which runs on a machine residing at the Rechenzentrum.
16 #include "becopyopt.h"
18 #define DUMP_MPS /**< dumps the problem in "CPLEX"-MPS format. NOT fixed-column-MPS. */
19 #define USE_SOS /**< uses Special Ordered Sets when using MPS */
20 #define DO_SOLVE /**< solve the MPS output with CPLEX */
21 #define DUMP_MATRICES /**< dumps all matrices completely. only recommended for small problems */
22 #define DUMP_LP /**< dumps the problem in LP format. 'human-readable' equations etc... */
23 #define DELETE_FILES /**< deletes all dumped files after use */
24 #define EXPECT_FILENAME "runme" /** name of the expect-script */
26 /* CPLEX-account related stuff */
27 #define SSH_USER_HOST_PATH "kb61@sp-smp.rz.uni-karlsruhe.de"
28 #define SSH_PASSWD "!cplex90"
30 #define DEBUG_LVL SET_LEVEL_1
31 static firm_dbg_module_t *dbg = NULL;
33 #define SLOTS_NUM2POS 256
34 #define SLOTS_LIVING 32
37 * A type storing names of the x variables in the form x[NUMBER]_[COLOR]
39 typedef struct _x_name_t {
44 * For each node taking part in the opt-problem its position in the
45 * x-variable-vector is stored in a set. This set maps the node-nr (given by
46 * benumb) to the position in the vector.
48 typedef struct _num2pos_t {
53 * A type storing the unmodified '0-1 quadratic program' of the form
59 * This problem is called the original problem
61 typedef struct _problem_instance_t {
64 int x_dim, A_dim, B_dim; /**< number of: x variables, rows in A, rows in B */
65 x_name_t *x; /**< stores the names of the x variables. all possible colors for a node are ordered and occupy consecutive entries. lives in obstack ob. */
66 set *num2pos; /**< maps node numbers to positions in x. */
67 sp_matrix_t *Q, *A, *B; /**< the (sparse) matrices of this problem */
69 /* needed only for linearizations */
70 int bigM, maxQij, minQij;
72 /* overhead needed to build this */
78 /* Nodes have consecutive numbers so this hash shoud be fine */
79 #define HASH_NUM(num) num
81 static int set_cmp_num2pos(const void *x, const void *y, size_t size) {
82 return ((num2pos_t *)x)->num != ((num2pos_t *)y)->num;
86 * Sets the first position of node with number num to pos.
87 * See x_name_t *x in _problem_instance_t.
89 static INLINE void pi_set_first_pos(problem_instance_t *pi, int num, int pos) {
93 set_insert(pi->num2pos, &find, sizeof(find), HASH_NUM(num));
97 * Get position by number. (First possible color)
98 * returns -1 if not found.
100 static INLINE int pi_get_first_pos(problem_instance_t *pi, int num) {
101 num2pos_t find, *found;
103 found = set_find(pi->num2pos, &find, sizeof(find), HASH_NUM(num));
105 assert(pi->x[found->pos].n == num && (found->pos == 0 || pi->x[found->pos-1].n != num) && "pi->num2pos is broken!");
112 * Get position by number and color.
113 * returns -1 if not found.
115 static INLINE int pi_get_pos(problem_instance_t *pi, int num, int col) {
116 num2pos_t find, *found;
119 found = set_find(pi->num2pos, &find, sizeof(find), HASH_NUM(num));
123 while (pos < pi->x_dim && pi->x[pos].n == num && pi->x[pos].c < col)
126 if (pi->x[pos].n == num && pi->x[pos].c == col)
132 static INLINE FILE *ffopen(const char *base, const char *ext, const char *mode) {
136 snprintf(buf, sizeof(buf), "%s.%s", base, ext);
137 if (! (out = fopen(buf, mode))) {
138 fprintf(stderr, "Cannot open file %s in mode %s\n", buf, mode);
146 * Dump the raw matrices of the problem to a file for debugging.
148 static void pi_dump_matrices(problem_instance_t *pi) {
150 FILE *out = ffopen(pi->name, "matrix", "wt");
152 DBG((dbg, LEVEL_1, "Dumping raw...\n"));
153 fprintf(out, "\n\nx-names =\n");
154 for (i=0; i<pi->x_dim; ++i)
155 fprintf(out, "%5d %2d\n", pi->x[i].n, pi->x[i].c);
157 fprintf(out, "\n\n-Q =\n");
158 matrix_dump(pi->Q, out, -1);
160 fprintf(out, "\n\nA =\n");
161 matrix_dump(pi->A, out, 1);
163 fprintf(out, "\n\nB =\n");
164 matrix_dump(pi->B, out, 1);
172 * Dumps the problem instance as a MILP. The original problem is transformed into:
174 * udN: Qx -y -s +Me = 0
180 * with M >= max sum Q'ij * x_j
183 static void pi_dump_lp(problem_instance_t *pi) {
186 FILE *out = ffopen(pi->name, "lpo", "wt");
188 DBG((dbg, LEVEL_1, "Dumping lp_org...\n"));
189 /* calc the big M for Q */
190 max_abs_Qij = pi->maxQij;
191 if (-pi->minQij > max_abs_Qij)
192 max_abs_Qij = -pi->minQij;
193 pi->bigM = pi->A_dim * max_abs_Qij;
194 DBG((dbg, LEVEL_2, "BigM = %d\n", pi->bigM));
196 /* generate objective function */
197 fprintf(out, "min: ");
198 for (i=0; i<pi->x_dim; ++i)
199 fprintf(out, "+s%d_%d -%dx%d_%d ", pi->x[i].n, pi->x[i].c, pi->bigM, pi->x[i].n, pi->x[i].c);
200 fprintf(out, ";\n\n");
202 /* constraints for former objective function */
203 for (i=0; i<pi->x_dim; ++i) {
204 matrix_foreach_in_row(pi->Q, i, e) {
207 fprintf(out, "+x%d_%d ", pi->x[e->col].n, pi->x[e->col].c);
209 fprintf(out, "-x%d_%d ", pi->x[e->col].n, pi->x[e->col].c);
211 fprintf(out, "%+dx%d_%d ", Qio, pi->x[e->col].n, pi->x[e->col].c);
213 fprintf(out, "-y%d_%d -s%d_%d +%d= 0;\n", pi->x[i].n, pi->x[i].c, pi->x[i].n, pi->x[i].c, pi->bigM);
215 fprintf(out, "\n\n");
217 /* constraints for (special) complementary condition */
218 for (i=0; i<pi->x_dim; ++i)
219 fprintf(out, "y%d_%d <= %d - %dx%d_%d;\n", pi->x[i].n, pi->x[i].c, 2*pi->bigM, 2*pi->bigM, pi->x[i].n, pi->x[i].c);
220 fprintf(out, "\n\n");
222 /* knapsack constraints */
223 for (i=0; i<pi->A_dim; ++i) {
224 matrix_foreach_in_row(pi->Q, i, e)
225 fprintf(out, "+x%d_%d ", pi->x[e->col].n, pi->x[e->col].c);
226 fprintf(out, " = 1;\n");
228 fprintf(out, "\n\n");
230 /* interference graph constraints */
231 for (i=0; i<pi->B_dim; ++i) {
232 matrix_foreach_in_row(pi->Q, i, e)
233 fprintf(out, "+x%d_%d ", pi->x[e->col].n, pi->x[e->col].c);
234 fprintf(out, " <= 1;\n");
236 fprintf(out, "\n\n");
238 /* integer constraints */
239 fprintf(out, "int x%d_%d", pi->x[0].n, pi->x[0].c);
240 for (i=1; i<pi->x_dim; ++i)
241 fprintf(out, ", x%d_%d", pi->x[i].n, pi->x[i].c);
250 * Dumps an mps file representing the problem. This is NOT the old-style,
251 * fixed-column format. Some white spaces are important, in general spaces
252 * are separators, MARKER-lines are used in COLUMN section to define binaries.
254 //BETTER use last 2 fields in COLUMNS section
255 static void pi_dump_mps(problem_instance_t *pi) {
258 FILE *out = ffopen(pi->name, "mps", "wt");
260 DBG((dbg, LEVEL_1, "Dumping mps...\n"));
261 max_abs_Qij = pi->maxQij;
262 if (-pi->minQij > max_abs_Qij)
263 max_abs_Qij = -pi->minQij;
264 pi->bigM = pi->A_dim * max_abs_Qij;
265 DBG((dbg, LEVEL_2, "BigM = %d\n", pi->bigM));
267 fprintf(out, "NAME %s\n", pi->name);
269 fprintf(out, "ROWS\n");
270 fprintf(out, " N obj\n");
271 for (i=0; i<pi->x_dim; ++i)
272 fprintf(out, " E cQ%d\n", i);
273 for (i=0; i<pi->A_dim; ++i)
274 fprintf(out, " E cA%d\n", i);
275 for (i=0; i<pi->B_dim; ++i)
276 fprintf(out, " L cB%d\n", i);
277 for (i=0; i<pi->x_dim; ++i)
278 fprintf(out, " L cy%d\n", i);
280 fprintf(out, "COLUMNS\n");
281 /* the x vars come first */
282 /* mark them as binaries */
283 fprintf(out, " MARKI0\t'MARKER'\t'INTORG'\n");
286 fprintf(out, " S1 SOS_%d\t'MARKER'\t'SOSORG'\n", sos_cnt++);
288 for (i=0; i<pi->x_dim; ++i) {
290 if (i>0 && pi->x[i].n != pi->x[i-1].n) {
291 fprintf(out, " SOS_%d\t'MARKER'\t'SOSEND'\n", sos_cnt++);
292 fprintf(out, " S1 SOS_%d\t'MARKER'\t'SOSORG'\n", sos_cnt++);
295 /* participation in objective */
296 fprintf(out, " x%d_%d\tobj\t%d\n", pi->x[i].n, pi->x[i].c, -pi->bigM);
298 matrix_foreach_in_col(pi->Q, i, e)
299 fprintf(out, " x%d_%d\tcQ%d\t%d\n", pi->x[i].n, pi->x[i].c, e->row, e->val);
301 matrix_foreach_in_col(pi->A, i, e)
302 fprintf(out, " x%d_%d\tcA%d\t%d\n", pi->x[i].n, pi->x[i].c, e->row, e->val);
304 matrix_foreach_in_col(pi->B, i, e)
305 fprintf(out, " x%d_%d\tcB%d\t%d\n", pi->x[i].n, pi->x[i].c, e->row, e->val);
307 fprintf(out, " x%d_%d\tcy%d\t%d\n", pi->x[i].n, pi->x[i].c, i, 2*pi->bigM);
311 fprintf(out, " SOS_%d\t'MARKER'\t'SOSEND'\n", sos_cnt++);
313 fprintf(out, " MARKI1\t'MARKER'\t'INTEND'\n"); /* end of marking */
315 /* next the s vars */
316 for (i=0; i<pi->x_dim; ++i) {
317 /* participation in objective */
318 fprintf(out, " s%d_%d\tobj\t%d\n", pi->x[i].n, pi->x[i].c, 1);
320 fprintf(out, " s%d_%d\tcQ%d\t%d\n", pi->x[i].n, pi->x[i].c, i, -1);
323 /* next the y vars */
324 for (i=0; i<pi->x_dim; ++i) {
326 fprintf(out, " y%d_%d\tcQ%d\t%d\n", pi->x[i].n, pi->x[i].c, i, -1);
328 fprintf(out, " y%d_%d\tcy%d\t%d\n", pi->x[i].n, pi->x[i].c, i, 1);
331 fprintf(out, "RHS\n");
332 for (i=0; i<pi->x_dim; ++i)
333 fprintf(out, " rhs\tcQ%d\t%d\n", i, -pi->bigM);
334 for (i=0; i<pi->A_dim; ++i)
335 fprintf(out, " rhs\tcA%d\t%d\n", i, 1);
336 for (i=0; i<pi->B_dim; ++i)
337 fprintf(out, " rhs\tcB%d\t%d\n", i, 1);
338 for (i=0; i<pi->x_dim; ++i)
339 fprintf(out, " rhs\tcy%d\t%d\n", i, 2*pi->bigM);
341 fprintf(out, "ENDATA\n");
344 out = ffopen(pi->name, "mst", "wt");
345 fprintf(out, "NAME\n");
346 for (i=0; i<pi->x_dim; ++i) {
350 if (get_irn_color(get_irn_for_graph_nr(pi->irg, n)) == c)
354 fprintf(out, " x%d_%d\t%d\n", n, c, val);
356 fprintf(out, "ENDATA\n");
363 * Invoke an external solver
365 static void pi_solve_ilp(problem_instance_t *pi) {
368 /* write command file for CPLEX */
369 out = ffopen(pi->name, "cmd", "wt");
370 fprintf(out, "read %s.mps\n", pi->name);
371 fprintf(out, "read %s.mst\n", pi->name);
372 fprintf(out, "set mip strategy mipstart 1\n");
373 fprintf(out, "optimize\n");
374 fprintf(out, "set logfile %s.sol\n", pi->name);
375 fprintf(out, "display solution variables 1-%d\n", pi->x_dim);
376 fprintf(out, "set logfile cplex.log\n");
377 fprintf(out, "quit\n");
380 /* write expect-file for copying problem to RZ */
381 out = ffopen(EXPECT_FILENAME, "exp", "wt");
382 fprintf(out, "#! /usr/bin/expect\n");
383 fprintf(out, "spawn scp %s.mps %s.mst %s.cmd %s:\n", pi->name, pi->name, pi->name, SSH_USER_HOST_PATH);
384 fprintf(out, "expect \":\"\n");
385 fprintf(out, "send \"%s\\n\"\n", SSH_PASSWD);
386 fprintf(out, "interact\n");
388 fprintf(out, "spawn ssh %s \"./cplex90 < %s.cmd\"\n", SSH_USER_HOST_PATH, pi->name);
389 fprintf(out, "expect \":\"\n");
390 fprintf(out, "send \"%s\\n\"\n", SSH_PASSWD);
391 fprintf(out, "interact\n");
393 fprintf(out, "spawn scp %s:%s.sol .\n", SSH_USER_HOST_PATH, pi->name);
394 fprintf(out, "expect \":\"\n");
395 fprintf(out, "send \"%s\\n\"\n", SSH_PASSWD);
396 fprintf(out, "interact\n");
399 /* call the expect script */
400 chmod(EXPECT_FILENAME ".exp", 0700);
401 system(EXPECT_FILENAME ".exp");
405 * Sets the colors of irns according to the values of variables found in the
406 * output file of the solver.
408 static void pi_apply_solution(problem_instance_t *pi) {
409 FILE *in = ffopen(pi->name, "sol", "rt");
413 DBG((dbg, LEVEL_1, "Applying solution...\n"));
416 int num = -1, col = -1, val = -1;
417 if (fscanf(in, "x%d_%d %d.%s\n", &num, &col, &val, buf) != 3) {
418 while(fscanf(in, "%1020s\n", buf) != 1);
422 DBG((dbg, LEVEL_1, "x%d_%d = %d\n", num, col, val));
423 set_irn_color(get_irn_for_graph_nr(pi->irg, num), col);
428 #endif /* DO_SOLVE */
431 static void pi_delete_files(problem_instance_t *pi) {
433 int end = snprintf(buf, sizeof(buf), "%s", pi->name);
435 snprintf(buf+end, sizeof(buf)-end, ".matrix");
439 snprintf(buf+end, sizeof(buf)-end, ".mps");
441 snprintf(buf+end, sizeof(buf)-end, ".mst");
443 snprintf(buf+end, sizeof(buf)-end, ".cmd");
445 remove(EXPECT_FILENAME ".exp");
448 snprintf(buf+end, sizeof(buf)-end, ".lp");
455 * Collects all irns in currently processed register class
457 static void pi_collect_x_names(ir_node *block, void *env) {
458 problem_instance_t *pi = env;
459 struct list_head *head = &get_ra_block_info(block)->border_head;
462 list_for_each_entry_reverse(border_t, curr, head, list)
463 if (curr->is_def && curr->is_real) {
465 pi->A_dim++; /* one knapsack constraint for each node */
467 xx.n = get_irn_graph_nr(curr->irn);
468 pi_set_first_pos(pi, xx.n, pi->x_dim);
469 //TODO iterate over all possible colors !!MUST BE IN ORDER!!
470 for (xx.c=0; xx.c<MAX_COLORS; ++xx.c) {
471 if (!is_possible_color(irn, xx.c))
473 DBG((dbg, LEVEL_2, "Adding %n %d\n", curr->irn, xx.c));
474 obstack_grow(&pi->ob, &xx, sizeof(xx));
475 pi->x_dim++; /* one x variable for each node and color */
481 * Checks if all nodes in living are live_out in block block.
483 static INLINE int all_live_out(ir_node *block, pset *living) {
485 for (n = pset_first(living); n; n = pset_next(living))
486 if (!is_live_out(block, n)) {
494 * Finds cliques in the interference graph, considering only nodes
495 * for which the color pi->curr_color is possible. Finds only 'maximal-cliques',
496 * viz cliques which are not conatained in another one.
497 * This is used for the matrix B.
499 static void pi_clique_finder(ir_node *block, void *env) {
500 problem_instance_t *pi = env;
501 enum phase_t {growing, shrinking} phase = growing;
502 struct list_head *head = &get_ra_block_info(block)->border_head;
504 pset *living = pset_new_ptr(SLOTS_LIVING);
506 list_for_each_entry_reverse(border_t, b, head, list) {
507 const ir_node *irn = b->irn;
508 if (!is_possible_color(n, pi->curr_col))
512 DBG((dbg, LEVEL_2, "Def %n\n", irn));
513 pset_insert_ptr(living, irn);
515 } else { /* is_use */
516 DBG((dbg, LEVEL_2, "Use %n\n", irn));
518 /* before shrinking the set, store the current 'maximum' clique;
519 * do NOT if clique is a single node
520 * do NOT if all values are live_out */
521 if (phase == growing && pset_count(living) >= 2 && !all_live_out(block, living)) {
523 for (n = pset_first(living); n; n = pset_next(living)) {
524 int pos = pi_get_pos(pi, get_irn_graph_nr(n), pi->curr_color);
525 matrix_set(pi->B, pi->curr_row, pos, 1);
526 DBG((dbg, LEVEL_2, "B[%d, %d] := %d\n", pi->curr_row, pos, 1));
530 pset_remove_ptr(living, irn);
539 * Generate the initial problem matrices and vectors.
541 static problem_instance_t *new_pi(const copy_opt_t *co) {
542 DBG((dbg, LEVEL_1, "Generating new instance...\n"));
543 problem_instance_t *pi = calloc(1, sizeof(*pi));
545 pi->name = get_entity_name(get_irg_entity(co->irg));
546 pi->num2pos = new_set(set_cmp_num2pos, SLOTS_NUM2POS);
550 * one entry per node and possible color */
551 obstack_init(&pi->ob);
552 dom_tree_walk_irg(co->irg, pi_collect_x_names, NULL, &pi->ob);
553 pi->x = obstack_finish(&pi->ob);
556 * weights for the 'same-color-optimization' target */
559 pi->Q = new_matrix(pi->x_dim, pi->x_dim);
561 list_for_each_entry(unit_t, curr, &co->units, units) {
562 const ir_node *root, *arg;
564 unsigned rootpos, argpos;
567 root = curr->nodes[0];
568 rootnr = get_irn_graph_nr(root);
569 rootpos = pi_get_first_pos(pi, rootnr);
570 for (i = 1; i < curr->node_count; ++i) {
571 int weight = -get_weight(root, arg);
572 arg = curr->nodes[i];
573 argnr = get_irn_graph_nr(arg);
574 argpos = pi_get_first_pos(pi, argnr);
576 DBG((dbg, LEVEL_2, "Q[%n, %n] := %d\n", root, arg, weight));
577 /* for all colors root and arg have in common, set the weight for
578 * this pair in the objective function matrix Q */
579 while (rootpos < pi->x_dim && argpos < pi->x_dim &&
580 pi->x[rootpos].n == rootnr && pi->x[argpos].n == argnr) {
581 if (pi->x[rootpos].c < pi->x[argpos].c)
583 else if (pi->x[rootpos].c > pi->x[argpos].c)
586 matrix_set(pi->Q, rootpos++, argpos++, weight);
588 if (weight < pi->minQij) {
589 DBG((dbg, LEVEL_2, "minQij = %d\n", weight));
592 if (weight > pi->maxQij) {
593 DBG((dbg, LEVEL_2, "maxQij = %d\n", weight));
603 * knapsack constraint for each node */
605 int row = 0, col = 0;
606 pi->A = new_matrix(pi->A_dim, pi->x_dim);
607 while (col < pi->x_dim) {
608 int curr_n = pi->x[col].n;
609 while (col < pi->x_dim && pi->x[col].n == curr_n) {
610 DBG((dbg, LEVEL_2, "A[%d, %d] := %d\n", row, col, 1));
611 matrix_set(pi->A, row, col++, 1);
615 assert(row == pi->A_dim);
619 * interference constraints using exactly those cliques not contained in others. */
621 int color, expected_clipques = pi->A_dim/3 * MAX_COLORS;
622 pi->B = new_matrix(expected_clipques, pi->x_dim);
623 for (color = 0; color < MAX_COLORS; ++color) {
624 pi->curr_color = color;
625 dom_tree_walk_irg(pi->irg, pi_clique_finder, NULL, pi);
627 pi->B_dim = matrix_get_rowcount(pi->B);
634 * clean the problem instance
636 static void free_pi(problem_instance_t *pi) {
640 del_set(pi->num2pos);
641 obstack_free(&pi->ob, NULL);
645 void co_ilp_opt(copy_opt_t *co) {
646 dbg = firm_dbg_register("ir.be.copyoptilp");
647 firm_dbg_set_mask(dbg, DEBUG_LVL);
649 problem_instance_t *pi = new_pi(co);
652 pi_dump_matrices(pi);
665 pi_apply_solution(pi);