1 #include "isl_map_private.h"
6 * The implementation of parametric integer linear programming in this file
7 * was inspired by the paper "Parametric Integer Programming" and the
8 * report "Solving systems of affine (in)equalities" by Paul Feautrier
11 * The strategy used for obtaining a feasible solution is different
12 * from the one used in isl_tab.c. In particular, in isl_tab.c,
13 * upon finding a constraint that is not yet satisfied, we pivot
14 * in a row that increases the constant term of row holding the
15 * constraint, making sure the sample solution remains feasible
16 * for all the constraints it already satisfied.
17 * Here, we always pivot in the row holding the constraint,
18 * choosing a column that induces the lexicographically smallest
19 * increment to the sample solution.
21 * By starting out from a sample value that is lexicographically
22 * smaller than any integer point in the problem space, the first
23 * feasible integer sample point we find will also be the lexicographically
24 * smallest. If all variables can be assumed to be non-negative,
25 * then the initial sample value may be chosen equal to zero.
26 * However, we will not make this assumption. Instead, we apply
27 * the "big parameter" trick. Any variable x is then not directly
28 * used in the tableau, but instead it its represented by another
29 * variable x' = M + x, where M is an arbitrarily large (positive)
30 * value. x' is therefore always non-negative, whatever the value of x.
31 * Taking as initial smaple value x' = 0 corresponds to x = -M,
32 * which is always smaller than any possible value of x.
34 * We use the big parameter trick both in the main tableau and
35 * the context tableau, each of course having its own big parameter.
36 * Before doing any real work, we check if all the parameters
37 * happen to be non-negative. If so, we drop the column corresponding
38 * to M from the initial context tableau.
41 /* isl_sol is an interface for constructing a solution to
42 * a parametric integer linear programming problem.
43 * Every time the algorithm reaches a state where a solution
44 * can be read off from the tableau (including cases where the tableau
45 * is empty), the function "add" is called on the isl_sol passed
46 * to find_solutions_main.
48 * The context tableau is owned by isl_sol and is updated incrementally.
50 * There are currently two implementations of this interface,
51 * isl_sol_map, which simply collects the solutions in an isl_map
52 * and (optionally) the parts of the context where there is no solution
54 * isl_sol_for, which calls a user-defined function for each part of
58 struct isl_tab
*context_tab
;
59 struct isl_sol
*(*add
)(struct isl_sol
*sol
, struct isl_tab
*tab
);
60 void (*free
)(struct isl_sol
*sol
);
63 static void sol_free(struct isl_sol
*sol
)
73 struct isl_set
*empty
;
77 static void sol_map_free(struct isl_sol_map
*sol_map
)
79 isl_tab_free(sol_map
->sol
.context_tab
);
80 isl_map_free(sol_map
->map
);
81 isl_set_free(sol_map
->empty
);
85 static void sol_map_free_wrap(struct isl_sol
*sol
)
87 sol_map_free((struct isl_sol_map
*)sol
);
90 static struct isl_sol_map
*add_empty(struct isl_sol_map
*sol
)
92 struct isl_basic_set
*bset
;
96 sol
->empty
= isl_set_grow(sol
->empty
, 1);
97 bset
= isl_basic_set_copy(sol
->sol
.context_tab
->bset
);
98 bset
= isl_basic_set_simplify(bset
);
99 bset
= isl_basic_set_finalize(bset
);
100 sol
->empty
= isl_set_add(sol
->empty
, bset
);
109 /* Add the solution identified by the tableau and the context tableau.
111 * The layout of the variables is as follows.
112 * tab->n_var is equal to the total number of variables in the input
113 * map (including divs that were copied from the context)
114 * + the number of extra divs constructed
115 * Of these, the first tab->n_param and the last tab->n_div variables
116 * correspond to the variables in the context, i.e.,
117 * tab->n_param + tab->n_div = context_tab->n_var
118 * tab->n_param is equal to the number of parameters and input
119 * dimensions in the input map
120 * tab->n_div is equal to the number of divs in the context
122 * If there is no solution, then the basic set corresponding to the
123 * context tableau is added to the set "empty".
125 * Otherwise, a basic map is constructed with the same parameters
126 * and divs as the context, the dimensions of the context as input
127 * dimensions and a number of output dimensions that is equal to
128 * the number of output dimensions in the input map.
129 * The divs in the input map (if any) that do not correspond to any
130 * div in the context do not appear in the solution.
131 * The algorithm will make sure that they have an integer value,
132 * but these values themselves are of no interest.
134 * The constraints and divs of the context are simply copied
135 * fron context_tab->bset.
136 * To extract the value of the output variables, it should be noted
137 * that we always use a big parameter M and so the variable stored
138 * in the tableau is not an output variable x itself, but
139 * x' = M + x (in case of minimization)
141 * x' = M - x (in case of maximization)
142 * If x' appears in a column, then its optimal value is zero,
143 * which means that the optimal value of x is an unbounded number
144 * (-M for minimization and M for maximization).
145 * We currently assume that the output dimensions in the original map
146 * are bounded, so this cannot occur.
147 * Similarly, when x' appears in a row, then the coefficient of M in that
148 * row is necessarily 1.
149 * If the row represents
150 * d x' = c + d M + e(y)
151 * then, in case of minimization, an equality
152 * c + e(y) - d x' = 0
153 * is added, and in case of maximization,
154 * c + e(y) + d x' = 0
156 static struct isl_sol_map
*sol_map_add(struct isl_sol_map
*sol
,
160 struct isl_basic_map
*bmap
= NULL
;
161 struct isl_tab
*context_tab
;
174 return add_empty(sol
);
176 context_tab
= sol
->sol
.context_tab
;
178 n_out
= isl_map_dim(sol
->map
, isl_dim_out
);
179 n_eq
= context_tab
->bset
->n_eq
+ n_out
;
180 n_ineq
= context_tab
->bset
->n_ineq
;
181 nparam
= tab
->n_param
;
182 total
= isl_map_dim(sol
->map
, isl_dim_all
);
183 bmap
= isl_basic_map_alloc_dim(isl_map_get_dim(sol
->map
),
184 tab
->n_div
, n_eq
, 2 * tab
->n_div
+ n_ineq
);
189 ISL_F_SET(bmap
, ISL_BASIC_MAP_RATIONAL
);
190 for (i
= 0; i
< context_tab
->bset
->n_div
; ++i
) {
191 int k
= isl_basic_map_alloc_div(bmap
);
194 isl_seq_cpy(bmap
->div
[k
],
195 context_tab
->bset
->div
[i
], 1 + 1 + nparam
);
196 isl_seq_clr(bmap
->div
[k
] + 1 + 1 + nparam
, total
- nparam
);
197 isl_seq_cpy(bmap
->div
[k
] + 1 + 1 + total
,
198 context_tab
->bset
->div
[i
] + 1 + 1 + nparam
, i
);
200 for (i
= 0; i
< context_tab
->bset
->n_eq
; ++i
) {
201 int k
= isl_basic_map_alloc_equality(bmap
);
204 isl_seq_cpy(bmap
->eq
[k
], context_tab
->bset
->eq
[i
], 1 + nparam
);
205 isl_seq_clr(bmap
->eq
[k
] + 1 + nparam
, total
- nparam
);
206 isl_seq_cpy(bmap
->eq
[k
] + 1 + total
,
207 context_tab
->bset
->eq
[i
] + 1 + nparam
, n_div
);
209 for (i
= 0; i
< context_tab
->bset
->n_ineq
; ++i
) {
210 int k
= isl_basic_map_alloc_inequality(bmap
);
213 isl_seq_cpy(bmap
->ineq
[k
],
214 context_tab
->bset
->ineq
[i
], 1 + nparam
);
215 isl_seq_clr(bmap
->ineq
[k
] + 1 + nparam
, total
- nparam
);
216 isl_seq_cpy(bmap
->ineq
[k
] + 1 + total
,
217 context_tab
->bset
->ineq
[i
] + 1 + nparam
, n_div
);
219 for (i
= tab
->n_param
; i
< total
; ++i
) {
220 int k
= isl_basic_map_alloc_equality(bmap
);
223 isl_seq_clr(bmap
->eq
[k
] + 1, isl_basic_map_total_dim(bmap
));
224 if (!tab
->var
[i
].is_row
) {
226 isl_assert(bmap
->ctx
, !tab
->M
, goto error
);
227 isl_int_set_si(bmap
->eq
[k
][0], 0);
229 isl_int_set_si(bmap
->eq
[k
][1 + i
], 1);
231 isl_int_set_si(bmap
->eq
[k
][1 + i
], -1);
234 row
= tab
->var
[i
].index
;
237 isl_assert(bmap
->ctx
,
238 isl_int_eq(tab
->mat
->row
[row
][2],
239 tab
->mat
->row
[row
][0]),
241 isl_int_set(bmap
->eq
[k
][0], tab
->mat
->row
[row
][1]);
242 for (j
= 0; j
< tab
->n_param
; ++j
) {
244 if (tab
->var
[j
].is_row
)
246 col
= tab
->var
[j
].index
;
247 isl_int_set(bmap
->eq
[k
][1 + j
],
248 tab
->mat
->row
[row
][off
+ col
]);
250 for (j
= 0; j
< tab
->n_div
; ++j
) {
252 if (tab
->var
[tab
->n_var
- tab
->n_div
+j
].is_row
)
254 col
= tab
->var
[tab
->n_var
- tab
->n_div
+j
].index
;
255 isl_int_set(bmap
->eq
[k
][1 + total
+ j
],
256 tab
->mat
->row
[row
][off
+ col
]);
259 isl_int_set(bmap
->eq
[k
][1 + i
],
260 tab
->mat
->row
[row
][0]);
262 isl_int_neg(bmap
->eq
[k
][1 + i
],
263 tab
->mat
->row
[row
][0]);
266 bmap
= isl_basic_map_simplify(bmap
);
267 bmap
= isl_basic_map_finalize(bmap
);
268 sol
->map
= isl_map_grow(sol
->map
, 1);
269 sol
->map
= isl_map_add(sol
->map
, bmap
);
274 isl_basic_map_free(bmap
);
279 static struct isl_sol
*sol_map_add_wrap(struct isl_sol
*sol
,
282 return (struct isl_sol
*)sol_map_add((struct isl_sol_map
*)sol
, tab
);
286 /* Store the "parametric constant" of row "row" of tableau "tab" in "line",
287 * i.e., the constant term and the coefficients of all variables that
288 * appear in the context tableau.
289 * Note that the coefficient of the big parameter M is NOT copied.
290 * The context tableau may not have a big parameter and even when it
291 * does, it is a different big parameter.
293 static void get_row_parameter_line(struct isl_tab
*tab
, int row
, isl_int
*line
)
296 unsigned off
= 2 + tab
->M
;
298 isl_int_set(line
[0], tab
->mat
->row
[row
][1]);
299 for (i
= 0; i
< tab
->n_param
; ++i
) {
300 if (tab
->var
[i
].is_row
)
301 isl_int_set_si(line
[1 + i
], 0);
303 int col
= tab
->var
[i
].index
;
304 isl_int_set(line
[1 + i
], tab
->mat
->row
[row
][off
+ col
]);
307 for (i
= 0; i
< tab
->n_div
; ++i
) {
308 if (tab
->var
[tab
->n_var
- tab
->n_div
+ i
].is_row
)
309 isl_int_set_si(line
[1 + tab
->n_param
+ i
], 0);
311 int col
= tab
->var
[tab
->n_var
- tab
->n_div
+ i
].index
;
312 isl_int_set(line
[1 + tab
->n_param
+ i
],
313 tab
->mat
->row
[row
][off
+ col
]);
318 /* Check if rows "row1" and "row2" have identical "parametric constants",
319 * as explained above.
320 * In this case, we also insist that the coefficients of the big parameter
321 * be the same as the values of the constants will only be the same
322 * if these coefficients are also the same.
324 static int identical_parameter_line(struct isl_tab
*tab
, int row1
, int row2
)
327 unsigned off
= 2 + tab
->M
;
329 if (isl_int_ne(tab
->mat
->row
[row1
][1], tab
->mat
->row
[row2
][1]))
332 if (tab
->M
&& isl_int_ne(tab
->mat
->row
[row1
][2],
333 tab
->mat
->row
[row2
][2]))
336 for (i
= 0; i
< tab
->n_param
+ tab
->n_div
; ++i
) {
337 int pos
= i
< tab
->n_param
? i
:
338 tab
->n_var
- tab
->n_div
+ i
- tab
->n_param
;
341 if (tab
->var
[pos
].is_row
)
343 col
= tab
->var
[pos
].index
;
344 if (isl_int_ne(tab
->mat
->row
[row1
][off
+ col
],
345 tab
->mat
->row
[row2
][off
+ col
]))
351 /* Return an inequality that expresses that the "parametric constant"
352 * should be non-negative.
353 * This function is only called when the coefficient of the big parameter
356 static struct isl_vec
*get_row_parameter_ineq(struct isl_tab
*tab
, int row
)
358 struct isl_vec
*ineq
;
360 ineq
= isl_vec_alloc(tab
->mat
->ctx
, 1 + tab
->n_param
+ tab
->n_div
);
364 get_row_parameter_line(tab
, row
, ineq
->el
);
366 ineq
= isl_vec_normalize(ineq
);
371 /* Return a integer division for use in a parametric cut based on the given row.
372 * In particular, let the parametric constant of the row be
376 * where y_0 = 1, but none of the y_i corresponds to the big parameter M.
377 * The div returned is equal to
379 * floor(\sum_i {-a_i} y_i) = floor((\sum_i (-a_i mod d) y_i)/d)
381 static struct isl_vec
*get_row_parameter_div(struct isl_tab
*tab
, int row
)
385 div
= isl_vec_alloc(tab
->mat
->ctx
, 1 + 1 + tab
->n_param
+ tab
->n_div
);
389 isl_int_set(div
->el
[0], tab
->mat
->row
[row
][0]);
390 get_row_parameter_line(tab
, row
, div
->el
+ 1);
391 div
= isl_vec_normalize(div
);
392 isl_seq_neg(div
->el
+ 1, div
->el
+ 1, div
->size
- 1);
393 isl_seq_fdiv_r(div
->el
+ 1, div
->el
+ 1, div
->el
[0], div
->size
- 1);
398 /* Return a integer division for use in transferring an integrality constraint
400 * In particular, let the parametric constant of the row be
404 * where y_0 = 1, but none of the y_i corresponds to the big parameter M.
405 * The the returned div is equal to
407 * floor(\sum_i {a_i} y_i) = floor((\sum_i (a_i mod d) y_i)/d)
409 static struct isl_vec
*get_row_split_div(struct isl_tab
*tab
, int row
)
413 div
= isl_vec_alloc(tab
->mat
->ctx
, 1 + 1 + tab
->n_param
+ tab
->n_div
);
417 isl_int_set(div
->el
[0], tab
->mat
->row
[row
][0]);
418 get_row_parameter_line(tab
, row
, div
->el
+ 1);
419 div
= isl_vec_normalize(div
);
420 isl_seq_fdiv_r(div
->el
+ 1, div
->el
+ 1, div
->el
[0], div
->size
- 1);
425 /* Construct and return an inequality that expresses an upper bound
427 * In particular, if the div is given by
431 * then the inequality expresses
435 static struct isl_vec
*ineq_for_div(struct isl_basic_set
*bset
, unsigned div
)
439 struct isl_vec
*ineq
;
441 total
= isl_basic_set_total_dim(bset
);
442 div_pos
= 1 + total
- bset
->n_div
+ div
;
444 ineq
= isl_vec_alloc(bset
->ctx
, 1 + total
);
448 isl_seq_cpy(ineq
->el
, bset
->div
[div
] + 1, 1 + total
);
449 isl_int_neg(ineq
->el
[div_pos
], bset
->div
[div
][0]);
453 /* Given a row in the tableau and a div that was created
454 * using get_row_split_div and that been constrained to equality, i.e.,
456 * d = floor(\sum_i {a_i} y_i) = \sum_i {a_i} y_i
458 * replace the expression "\sum_i {a_i} y_i" in the row by d,
459 * i.e., we subtract "\sum_i {a_i} y_i" and add 1 d.
460 * The coefficients of the non-parameters in the tableau have been
461 * verified to be integral. We can therefore simply replace coefficient b
462 * by floor(b). For the coefficients of the parameters we have
463 * floor(a_i) = a_i - {a_i}, while for the other coefficients, we have
466 static struct isl_tab
*set_row_cst_to_div(struct isl_tab
*tab
, int row
, int div
)
469 unsigned off
= 2 + tab
->M
;
471 isl_seq_fdiv_q(tab
->mat
->row
[row
] + 1, tab
->mat
->row
[row
] + 1,
472 tab
->mat
->row
[row
][0], 1 + tab
->M
+ tab
->n_col
);
474 isl_int_set_si(tab
->mat
->row
[row
][0], 1);
476 isl_assert(tab
->mat
->ctx
,
477 !tab
->var
[tab
->n_var
- tab
->n_div
+ div
].is_row
, goto error
);
479 col
= tab
->var
[tab
->n_var
- tab
->n_div
+ div
].index
;
480 isl_int_set_si(tab
->mat
->row
[row
][off
+ col
], 1);
488 /* Check if the (parametric) constant of the given row is obviously
489 * negative, meaning that we don't need to consult the context tableau.
490 * If there is a big parameter and its coefficient is non-zero,
491 * then this coefficient determines the outcome.
492 * Otherwise, we check whether the constant is negative and
493 * all non-zero coefficients of parameters are negative and
494 * belong to non-negative parameters.
496 static int is_obviously_neg(struct isl_tab
*tab
, int row
)
500 unsigned off
= 2 + tab
->M
;
503 if (isl_int_is_pos(tab
->mat
->row
[row
][2]))
505 if (isl_int_is_neg(tab
->mat
->row
[row
][2]))
509 if (isl_int_is_nonneg(tab
->mat
->row
[row
][1]))
511 for (i
= 0; i
< tab
->n_param
; ++i
) {
512 /* Eliminated parameter */
513 if (tab
->var
[i
].is_row
)
515 col
= tab
->var
[i
].index
;
516 if (isl_int_is_zero(tab
->mat
->row
[row
][off
+ col
]))
518 if (!tab
->var
[i
].is_nonneg
)
520 if (isl_int_is_pos(tab
->mat
->row
[row
][off
+ col
]))
523 for (i
= 0; i
< tab
->n_div
; ++i
) {
524 if (tab
->var
[tab
->n_var
- tab
->n_div
+ i
].is_row
)
526 col
= tab
->var
[tab
->n_var
- tab
->n_div
+ i
].index
;
527 if (isl_int_is_zero(tab
->mat
->row
[row
][off
+ col
]))
529 if (!tab
->var
[tab
->n_var
- tab
->n_div
+ i
].is_nonneg
)
531 if (isl_int_is_pos(tab
->mat
->row
[row
][off
+ col
]))
537 /* Check if the (parametric) constant of the given row is obviously
538 * non-negative, meaning that we don't need to consult the context tableau.
539 * If there is a big parameter and its coefficient is non-zero,
540 * then this coefficient determines the outcome.
541 * Otherwise, we check whether the constant is non-negative and
542 * all non-zero coefficients of parameters are positive and
543 * belong to non-negative parameters.
545 static int is_obviously_nonneg(struct isl_tab
*tab
, int row
)
549 unsigned off
= 2 + tab
->M
;
552 if (isl_int_is_pos(tab
->mat
->row
[row
][2]))
554 if (isl_int_is_neg(tab
->mat
->row
[row
][2]))
558 if (isl_int_is_neg(tab
->mat
->row
[row
][1]))
560 for (i
= 0; i
< tab
->n_param
; ++i
) {
561 /* Eliminated parameter */
562 if (tab
->var
[i
].is_row
)
564 col
= tab
->var
[i
].index
;
565 if (isl_int_is_zero(tab
->mat
->row
[row
][off
+ col
]))
567 if (!tab
->var
[i
].is_nonneg
)
569 if (isl_int_is_neg(tab
->mat
->row
[row
][off
+ col
]))
572 for (i
= 0; i
< tab
->n_div
; ++i
) {
573 if (tab
->var
[tab
->n_var
- tab
->n_div
+ i
].is_row
)
575 col
= tab
->var
[tab
->n_var
- tab
->n_div
+ i
].index
;
576 if (isl_int_is_zero(tab
->mat
->row
[row
][off
+ col
]))
578 if (!tab
->var
[tab
->n_var
- tab
->n_div
+ i
].is_nonneg
)
580 if (isl_int_is_neg(tab
->mat
->row
[row
][off
+ col
]))
586 /* Given a row r and two columns, return the column that would
587 * lead to the lexicographically smallest increment in the sample
588 * solution when leaving the basis in favor of the row.
589 * Pivoting with column c will increment the sample value by a non-negative
590 * constant times a_{V,c}/a_{r,c}, with a_{V,c} the elements of column c
591 * corresponding to the non-parametric variables.
592 * If variable v appears in a column c_v, the a_{v,c} = 1 iff c = c_v,
593 * with all other entries in this virtual row equal to zero.
594 * If variable v appears in a row, then a_{v,c} is the element in column c
597 * Let v be the first variable with a_{v,c1}/a_{r,c1} != a_{v,c2}/a_{r,c2}.
598 * Then if a_{v,c1}/a_{r,c1} < a_{v,c2}/a_{r,c2}, i.e.,
599 * a_{v,c2} a_{r,c1} - a_{v,c1} a_{r,c2} > 0, c1 results in the minimal
600 * increment. Otherwise, it's c2.
602 static int lexmin_col_pair(struct isl_tab
*tab
,
603 int row
, int col1
, int col2
, isl_int tmp
)
608 tr
= tab
->mat
->row
[row
] + 2 + tab
->M
;
610 for (i
= tab
->n_param
; i
< tab
->n_var
- tab
->n_div
; ++i
) {
614 if (!tab
->var
[i
].is_row
) {
615 if (tab
->var
[i
].index
== col1
)
617 if (tab
->var
[i
].index
== col2
)
622 if (tab
->var
[i
].index
== row
)
625 r
= tab
->mat
->row
[tab
->var
[i
].index
] + 2 + tab
->M
;
626 s1
= isl_int_sgn(r
[col1
]);
627 s2
= isl_int_sgn(r
[col2
]);
628 if (s1
== 0 && s2
== 0)
635 isl_int_mul(tmp
, r
[col2
], tr
[col1
]);
636 isl_int_submul(tmp
, r
[col1
], tr
[col2
]);
637 if (isl_int_is_pos(tmp
))
639 if (isl_int_is_neg(tmp
))
645 /* Given a row in the tableau, find and return the column that would
646 * result in the lexicographically smallest, but positive, increment
647 * in the sample point.
648 * If there is no such column, then return tab->n_col.
649 * If anything goes wrong, return -1.
651 static int lexmin_pivot_col(struct isl_tab
*tab
, int row
)
654 int col
= tab
->n_col
;
658 tr
= tab
->mat
->row
[row
] + 2 + tab
->M
;
662 for (j
= tab
->n_dead
; j
< tab
->n_col
; ++j
) {
663 if (tab
->col_var
[j
] >= 0 &&
664 (tab
->col_var
[j
] < tab
->n_param
||
665 tab
->col_var
[j
] >= tab
->n_var
- tab
->n_div
))
668 if (!isl_int_is_pos(tr
[j
]))
671 if (col
== tab
->n_col
)
674 col
= lexmin_col_pair(tab
, row
, col
, j
, tmp
);
675 isl_assert(tab
->mat
->ctx
, col
>= 0, goto error
);
685 /* Return the first known violated constraint, i.e., a non-negative
686 * contraint that currently has an either obviously negative value
687 * or a previously determined to be negative value.
689 * If any constraint has a negative coefficient for the big parameter,
690 * if any, then we return one of these first.
692 static int first_neg(struct isl_tab
*tab
)
697 for (row
= tab
->n_redundant
; row
< tab
->n_row
; ++row
) {
698 if (!isl_tab_var_from_row(tab
, row
)->is_nonneg
)
700 if (isl_int_is_neg(tab
->mat
->row
[row
][2]))
703 for (row
= tab
->n_redundant
; row
< tab
->n_row
; ++row
) {
704 if (!isl_tab_var_from_row(tab
, row
)->is_nonneg
)
707 if (tab
->row_sign
[row
] == 0 &&
708 is_obviously_neg(tab
, row
))
709 tab
->row_sign
[row
] = isl_tab_row_neg
;
710 if (tab
->row_sign
[row
] != isl_tab_row_neg
)
712 } else if (!is_obviously_neg(tab
, row
))
719 /* Resolve all known or obviously violated constraints through pivoting.
720 * In particular, as long as we can find any violated constraint, we
721 * look for a pivoting column that would result in the lexicographicallly
722 * smallest increment in the sample point. If there is no such column
723 * then the tableau is infeasible.
725 static struct isl_tab
*restore_lexmin(struct isl_tab
*tab
)
733 while ((row
= first_neg(tab
)) != -1) {
734 col
= lexmin_pivot_col(tab
, row
);
735 if (col
>= tab
->n_col
)
736 return isl_tab_mark_empty(tab
);
739 isl_tab_pivot(tab
, row
, col
);
747 /* Given a row that represents an equality, look for an appropriate
749 * In particular, if there are any non-zero coefficients among
750 * the non-parameter variables, then we take the last of these
751 * variables. Eliminating this variable in terms of the other
752 * variables and/or parameters does not influence the property
753 * that all column in the initial tableau are lexicographically
754 * positive. The row corresponding to the eliminated variable
755 * will only have non-zero entries below the diagonal of the
756 * initial tableau. That is, we transform
762 * If there is no such non-parameter variable, then we are dealing with
763 * pure parameter equality and we pick any parameter with coefficient 1 or -1
764 * for elimination. This will ensure that the eliminated parameter
765 * always has an integer value whenever all the other parameters are integral.
766 * If there is no such parameter then we return -1.
768 static int last_var_col_or_int_par_col(struct isl_tab
*tab
, int row
)
770 unsigned off
= 2 + tab
->M
;
773 for (i
= tab
->n_var
- tab
->n_div
- 1; i
>= 0 && i
>= tab
->n_param
; --i
) {
775 if (tab
->var
[i
].is_row
)
777 col
= tab
->var
[i
].index
;
778 if (col
<= tab
->n_dead
)
780 if (!isl_int_is_zero(tab
->mat
->row
[row
][off
+ col
]))
783 for (i
= tab
->n_dead
; i
< tab
->n_col
; ++i
) {
784 if (isl_int_is_one(tab
->mat
->row
[row
][off
+ i
]))
786 if (isl_int_is_negone(tab
->mat
->row
[row
][off
+ i
]))
792 /* Add an equality that is known to be valid to the tableau.
793 * We first check if we can eliminate a variable or a parameter.
794 * If not, we add the equality as two inequalities.
795 * In this case, the equality was a pure parameter equality and there
796 * is no need to resolve any constraint violations.
798 static struct isl_tab
*add_lexmin_valid_eq(struct isl_tab
*tab
, isl_int
*eq
)
805 r
= isl_tab_add_row(tab
, eq
);
809 r
= tab
->con
[r
].index
;
810 i
= last_var_col_or_int_par_col(tab
, r
);
812 tab
->con
[r
].is_nonneg
= 1;
813 isl_tab_push_var(tab
, isl_tab_undo_nonneg
, &tab
->con
[r
]);
814 isl_seq_neg(eq
, eq
, 1 + tab
->n_var
);
815 r
= isl_tab_add_row(tab
, eq
);
818 tab
->con
[r
].is_nonneg
= 1;
819 isl_tab_push_var(tab
, isl_tab_undo_nonneg
, &tab
->con
[r
]);
821 isl_tab_pivot(tab
, r
, i
);
822 isl_tab_kill_col(tab
, i
);
825 tab
= restore_lexmin(tab
);
834 /* Check if the given row is a pure constant.
836 static int is_constant(struct isl_tab
*tab
, int row
)
838 unsigned off
= 2 + tab
->M
;
840 return isl_seq_first_non_zero(tab
->mat
->row
[row
] + off
+ tab
->n_dead
,
841 tab
->n_col
- tab
->n_dead
) == -1;
844 /* Add an equality that may or may not be valid to the tableau.
845 * If the resulting row is a pure constant, then it must be zero.
846 * Otherwise, the resulting tableau is empty.
848 * If the row is not a pure constant, then we add two inequalities,
849 * each time checking that they can be satisfied.
850 * In the end we try to use one of the two constraints to eliminate
853 static struct isl_tab
*add_lexmin_eq(struct isl_tab
*tab
, isl_int
*eq
)
857 struct isl_tab_undo
*snap
;
861 snap
= isl_tab_snap(tab
);
862 r1
= isl_tab_add_row(tab
, eq
);
865 tab
->con
[r1
].is_nonneg
= 1;
866 isl_tab_push_var(tab
, isl_tab_undo_nonneg
, &tab
->con
[r1
]);
868 row
= tab
->con
[r1
].index
;
869 if (is_constant(tab
, row
)) {
870 if (!isl_int_is_zero(tab
->mat
->row
[row
][1]) ||
871 (tab
->M
&& !isl_int_is_zero(tab
->mat
->row
[row
][2])))
872 return isl_tab_mark_empty(tab
);
873 if (isl_tab_rollback(tab
, snap
) < 0)
878 tab
= restore_lexmin(tab
);
879 if (!tab
|| tab
->empty
)
882 isl_seq_neg(eq
, eq
, 1 + tab
->n_var
);
884 r2
= isl_tab_add_row(tab
, eq
);
887 tab
->con
[r2
].is_nonneg
= 1;
888 isl_tab_push_var(tab
, isl_tab_undo_nonneg
, &tab
->con
[r2
]);
890 tab
= restore_lexmin(tab
);
891 if (!tab
|| tab
->empty
)
894 if (!tab
->con
[r1
].is_row
)
895 isl_tab_kill_col(tab
, tab
->con
[r1
].index
);
896 else if (!tab
->con
[r2
].is_row
)
897 isl_tab_kill_col(tab
, tab
->con
[r2
].index
);
898 else if (isl_int_is_zero(tab
->mat
->row
[tab
->con
[r1
].index
][1])) {
899 unsigned off
= 2 + tab
->M
;
901 int row
= tab
->con
[r1
].index
;
902 i
= isl_seq_first_non_zero(tab
->mat
->row
[row
]+off
+tab
->n_dead
,
903 tab
->n_col
- tab
->n_dead
);
905 isl_tab_pivot(tab
, row
, tab
->n_dead
+ i
);
906 isl_tab_kill_col(tab
, tab
->n_dead
+ i
);
911 tab
->bset
= isl_basic_set_add_ineq(tab
->bset
, eq
);
912 isl_tab_push(tab
, isl_tab_undo_bset_ineq
);
913 isl_seq_neg(eq
, eq
, 1 + tab
->n_var
);
914 tab
->bset
= isl_basic_set_add_ineq(tab
->bset
, eq
);
915 isl_seq_neg(eq
, eq
, 1 + tab
->n_var
);
916 isl_tab_push(tab
, isl_tab_undo_bset_ineq
);
927 /* Add an inequality to the tableau, resolving violations using
930 static struct isl_tab
*add_lexmin_ineq(struct isl_tab
*tab
, isl_int
*ineq
)
937 tab
->bset
= isl_basic_set_add_ineq(tab
->bset
, ineq
);
938 isl_tab_push(tab
, isl_tab_undo_bset_ineq
);
942 r
= isl_tab_add_row(tab
, ineq
);
945 tab
->con
[r
].is_nonneg
= 1;
946 isl_tab_push_var(tab
, isl_tab_undo_nonneg
, &tab
->con
[r
]);
947 if (isl_tab_row_is_redundant(tab
, tab
->con
[r
].index
)) {
948 isl_tab_mark_redundant(tab
, tab
->con
[r
].index
);
952 tab
= restore_lexmin(tab
);
953 if (tab
&& !tab
->empty
&& tab
->con
[r
].is_row
&&
954 isl_tab_row_is_redundant(tab
, tab
->con
[r
].index
))
955 isl_tab_mark_redundant(tab
, tab
->con
[r
].index
);
962 /* Check if the coefficients of the parameters are all integral.
964 static int integer_parameter(struct isl_tab
*tab
, int row
)
968 unsigned off
= 2 + tab
->M
;
970 for (i
= 0; i
< tab
->n_param
; ++i
) {
971 /* Eliminated parameter */
972 if (tab
->var
[i
].is_row
)
974 col
= tab
->var
[i
].index
;
975 if (!isl_int_is_divisible_by(tab
->mat
->row
[row
][off
+ col
],
976 tab
->mat
->row
[row
][0]))
979 for (i
= 0; i
< tab
->n_div
; ++i
) {
980 if (tab
->var
[tab
->n_var
- tab
->n_div
+ i
].is_row
)
982 col
= tab
->var
[tab
->n_var
- tab
->n_div
+ i
].index
;
983 if (!isl_int_is_divisible_by(tab
->mat
->row
[row
][off
+ col
],
984 tab
->mat
->row
[row
][0]))
990 /* Check if the coefficients of the non-parameter variables are all integral.
992 static int integer_variable(struct isl_tab
*tab
, int row
)
995 unsigned off
= 2 + tab
->M
;
997 for (i
= 0; i
< tab
->n_col
; ++i
) {
998 if (tab
->col_var
[i
] >= 0 &&
999 (tab
->col_var
[i
] < tab
->n_param
||
1000 tab
->col_var
[i
] >= tab
->n_var
- tab
->n_div
))
1002 if (!isl_int_is_divisible_by(tab
->mat
->row
[row
][off
+ i
],
1003 tab
->mat
->row
[row
][0]))
1009 /* Check if the constant term is integral.
1011 static int integer_constant(struct isl_tab
*tab
, int row
)
1013 return isl_int_is_divisible_by(tab
->mat
->row
[row
][1],
1014 tab
->mat
->row
[row
][0]);
1017 #define I_CST 1 << 0
1018 #define I_PAR 1 << 1
1019 #define I_VAR 1 << 2
1021 /* Check for first (non-parameter) variable that is non-integer and
1022 * therefore requires a cut.
1023 * For parametric tableaus, there are three parts in a row,
1024 * the constant, the coefficients of the parameters and the rest.
1025 * For each part, we check whether the coefficients in that part
1026 * are all integral and if so, set the corresponding flag in *f.
1027 * If the constant and the parameter part are integral, then the
1028 * current sample value is integral and no cut is required
1029 * (irrespective of whether the variable part is integral).
1031 static int first_non_integer(struct isl_tab
*tab
, int *f
)
1035 for (i
= tab
->n_param
; i
< tab
->n_var
- tab
->n_div
; ++i
) {
1038 if (!tab
->var
[i
].is_row
)
1040 row
= tab
->var
[i
].index
;
1041 if (integer_constant(tab
, row
))
1042 ISL_FL_SET(flags
, I_CST
);
1043 if (integer_parameter(tab
, row
))
1044 ISL_FL_SET(flags
, I_PAR
);
1045 if (ISL_FL_ISSET(flags
, I_CST
) && ISL_FL_ISSET(flags
, I_PAR
))
1047 if (integer_variable(tab
, row
))
1048 ISL_FL_SET(flags
, I_VAR
);
1055 /* Add a (non-parametric) cut to cut away the non-integral sample
1056 * value of the given row.
1058 * If the row is given by
1060 * m r = f + \sum_i a_i y_i
1064 * c = - {-f/m} + \sum_i {a_i/m} y_i >= 0
1066 * The big parameter, if any, is ignored, since it is assumed to be big
1067 * enough to be divisible by any integer.
1068 * If the tableau is actually a parametric tableau, then this function
1069 * is only called when all coefficients of the parameters are integral.
1070 * The cut therefore has zero coefficients for the parameters.
1072 * The current value is known to be negative, so row_sign, if it
1073 * exists, is set accordingly.
1075 * Return the row of the cut or -1.
1077 static int add_cut(struct isl_tab
*tab
, int row
)
1082 unsigned off
= 2 + tab
->M
;
1084 if (isl_tab_extend_cons(tab
, 1) < 0)
1086 r
= isl_tab_allocate_con(tab
);
1090 r_row
= tab
->mat
->row
[tab
->con
[r
].index
];
1091 isl_int_set(r_row
[0], tab
->mat
->row
[row
][0]);
1092 isl_int_neg(r_row
[1], tab
->mat
->row
[row
][1]);
1093 isl_int_fdiv_r(r_row
[1], r_row
[1], tab
->mat
->row
[row
][0]);
1094 isl_int_neg(r_row
[1], r_row
[1]);
1096 isl_int_set_si(r_row
[2], 0);
1097 for (i
= 0; i
< tab
->n_col
; ++i
)
1098 isl_int_fdiv_r(r_row
[off
+ i
],
1099 tab
->mat
->row
[row
][off
+ i
], tab
->mat
->row
[row
][0]);
1101 tab
->con
[r
].is_nonneg
= 1;
1102 isl_tab_push_var(tab
, isl_tab_undo_nonneg
, &tab
->con
[r
]);
1104 tab
->row_sign
[tab
->con
[r
].index
] = isl_tab_row_neg
;
1106 return tab
->con
[r
].index
;
1109 /* Given a non-parametric tableau, add cuts until an integer
1110 * sample point is obtained or until the tableau is determined
1111 * to be integer infeasible.
1112 * As long as there is any non-integer value in the sample point,
1113 * we add an appropriate cut, if possible and resolve the violated
1114 * cut constraint using restore_lexmin.
1115 * If one of the corresponding rows is equal to an integral
1116 * combination of variables/constraints plus a non-integral constant,
1117 * then there is no way to obtain an integer point an we return
1118 * a tableau that is marked empty.
1120 static struct isl_tab
*cut_to_integer_lexmin(struct isl_tab
*tab
)
1130 while ((row
= first_non_integer(tab
, &flags
)) != -1) {
1131 if (ISL_FL_ISSET(flags
, I_VAR
))
1132 return isl_tab_mark_empty(tab
);
1133 row
= add_cut(tab
, row
);
1136 tab
= restore_lexmin(tab
);
1137 if (!tab
|| tab
->empty
)
1146 static struct isl_tab
*drop_sample(struct isl_tab
*tab
, int s
)
1148 if (s
!= tab
->n_outside
)
1149 isl_mat_swap_rows(tab
->samples
, tab
->n_outside
, s
);
1151 isl_tab_push(tab
, isl_tab_undo_drop_sample
);
1156 /* Check whether all the currently active samples also satisfy the inequality
1157 * "ineq" (treated as an equality if eq is set).
1158 * Remove those samples that do not.
1160 static struct isl_tab
*check_samples(struct isl_tab
*tab
, isl_int
*ineq
, int eq
)
1168 isl_assert(tab
->mat
->ctx
, tab
->bset
, goto error
);
1169 isl_assert(tab
->mat
->ctx
, tab
->samples
, goto error
);
1170 isl_assert(tab
->mat
->ctx
, tab
->samples
->n_col
== 1 + tab
->n_var
, goto error
);
1173 for (i
= tab
->n_outside
; i
< tab
->n_sample
; ++i
) {
1175 isl_seq_inner_product(ineq
, tab
->samples
->row
[i
],
1176 1 + tab
->n_var
, &v
);
1177 sgn
= isl_int_sgn(v
);
1178 if (eq
? (sgn
== 0) : (sgn
>= 0))
1180 tab
= drop_sample(tab
, i
);
1192 /* Check whether the sample value of the tableau is finite,
1193 * i.e., either the tableau does not use a big parameter, or
1194 * all values of the variables are equal to the big parameter plus
1195 * some constant. This constant is the actual sample value.
1197 static int sample_is_finite(struct isl_tab
*tab
)
1204 for (i
= 0; i
< tab
->n_var
; ++i
) {
1206 if (!tab
->var
[i
].is_row
)
1208 row
= tab
->var
[i
].index
;
1209 if (isl_int_ne(tab
->mat
->row
[row
][0], tab
->mat
->row
[row
][2]))
1215 /* Check if the context tableau of sol has any integer points.
1216 * Returns -1 if an error occurred.
1217 * If an integer point can be found and if moreover it is finite,
1218 * then it is added to the list of sample values.
1220 * This function is only called when none of the currently active sample
1221 * values satisfies the most recently added constraint.
1223 static int context_is_feasible(struct isl_sol
*sol
)
1225 struct isl_tab_undo
*snap
;
1226 struct isl_tab
*tab
;
1229 if (!sol
|| !sol
->context_tab
)
1232 snap
= isl_tab_snap(sol
->context_tab
);
1233 isl_tab_push_basis(sol
->context_tab
);
1235 sol
->context_tab
= cut_to_integer_lexmin(sol
->context_tab
);
1236 if (!sol
->context_tab
)
1239 tab
= sol
->context_tab
;
1240 if (!tab
->empty
&& sample_is_finite(tab
)) {
1241 struct isl_vec
*sample
;
1243 tab
->samples
= isl_mat_extend(tab
->samples
,
1244 tab
->n_sample
+ 1, tab
->samples
->n_col
);
1248 sample
= isl_tab_get_sample_value(tab
);
1251 isl_seq_cpy(tab
->samples
->row
[tab
->n_sample
],
1252 sample
->el
, sample
->size
);
1253 isl_vec_free(sample
);
1257 feasible
= !sol
->context_tab
->empty
;
1258 if (isl_tab_rollback(sol
->context_tab
, snap
) < 0)
1263 isl_tab_free(sol
->context_tab
);
1264 sol
->context_tab
= NULL
;
1268 /* First check if any of the currently active sample values satisfies
1269 * the inequality "ineq" (an equality if eq is set).
1270 * If not, continue with check_integer_feasible.
1272 static int context_valid_sample_or_feasible(struct isl_sol
*sol
,
1273 isl_int
*ineq
, int eq
)
1277 struct isl_tab
*tab
;
1279 if (!sol
|| !sol
->context_tab
)
1282 tab
= sol
->context_tab
;
1283 isl_assert(tab
->mat
->ctx
, tab
->bset
, return -1);
1284 isl_assert(tab
->mat
->ctx
, tab
->samples
, return -1);
1285 isl_assert(tab
->mat
->ctx
, tab
->samples
->n_col
== 1 + tab
->n_var
, return -1);
1288 for (i
= tab
->n_outside
; i
< tab
->n_sample
; ++i
) {
1290 isl_seq_inner_product(ineq
, tab
->samples
->row
[i
],
1291 1 + tab
->n_var
, &v
);
1292 sgn
= isl_int_sgn(v
);
1293 if (eq
? (sgn
== 0) : (sgn
>= 0))
1298 if (i
< tab
->n_sample
)
1301 return context_is_feasible(sol
);
1304 /* For a div d = floor(f/m), add the constraints
1307 * -(f-(m-1)) + m d >= 0
1309 * Note that the second constraint is the negation of
1313 static struct isl_tab
*add_div_constraints(struct isl_tab
*tab
, unsigned div
)
1317 struct isl_vec
*ineq
;
1322 total
= isl_basic_set_total_dim(tab
->bset
);
1323 div_pos
= 1 + total
- tab
->bset
->n_div
+ div
;
1325 ineq
= ineq_for_div(tab
->bset
, div
);
1329 tab
= add_lexmin_ineq(tab
, ineq
->el
);
1331 isl_seq_neg(ineq
->el
, tab
->bset
->div
[div
] + 1, 1 + total
);
1332 isl_int_set(ineq
->el
[div_pos
], tab
->bset
->div
[div
][0]);
1333 isl_int_add(ineq
->el
[0], ineq
->el
[0], ineq
->el
[div_pos
]);
1334 isl_int_sub_ui(ineq
->el
[0], ineq
->el
[0], 1);
1335 tab
= add_lexmin_ineq(tab
, ineq
->el
);
1345 /* Add a div specified by "div" to both the main tableau and
1346 * the context tableau. In case of the main tableau, we only
1347 * need to add an extra div. In the context tableau, we also
1348 * need to express the meaning of the div.
1349 * Return the index of the div or -1 if anything went wrong.
1351 static int add_div(struct isl_tab
*tab
, struct isl_tab
**context_tab
,
1352 struct isl_vec
*div
)
1357 struct isl_mat
*samples
;
1359 if (isl_tab_extend_vars(*context_tab
, 1) < 0)
1361 r
= isl_tab_allocate_var(*context_tab
);
1364 (*context_tab
)->var
[r
].is_nonneg
= 1;
1365 (*context_tab
)->var
[r
].frozen
= 1;
1367 samples
= isl_mat_extend((*context_tab
)->samples
,
1368 (*context_tab
)->n_sample
, 1 + (*context_tab
)->n_var
);
1369 (*context_tab
)->samples
= samples
;
1372 for (i
= (*context_tab
)->n_outside
; i
< samples
->n_row
; ++i
) {
1373 isl_seq_inner_product(div
->el
+ 1, samples
->row
[i
],
1374 div
->size
- 1, &samples
->row
[i
][samples
->n_col
- 1]);
1375 isl_int_fdiv_q(samples
->row
[i
][samples
->n_col
- 1],
1376 samples
->row
[i
][samples
->n_col
- 1], div
->el
[0]);
1379 (*context_tab
)->bset
= isl_basic_set_extend_dim((*context_tab
)->bset
,
1380 isl_basic_set_get_dim((*context_tab
)->bset
), 1, 0, 2);
1381 k
= isl_basic_set_alloc_div((*context_tab
)->bset
);
1384 isl_seq_cpy((*context_tab
)->bset
->div
[k
], div
->el
, div
->size
);
1385 isl_tab_push((*context_tab
), isl_tab_undo_bset_div
);
1386 *context_tab
= add_div_constraints(*context_tab
, k
);
1390 if (isl_tab_extend_vars(tab
, 1) < 0)
1392 r
= isl_tab_allocate_var(tab
);
1395 if (!(*context_tab
)->M
)
1396 tab
->var
[r
].is_nonneg
= 1;
1397 tab
->var
[r
].frozen
= 1;
1400 return tab
->n_div
- 1;
1402 isl_tab_free(*context_tab
);
1403 *context_tab
= NULL
;
1407 static int find_div(struct isl_tab
*tab
, isl_int
*div
, isl_int denom
)
1410 unsigned total
= isl_basic_set_total_dim(tab
->bset
);
1412 for (i
= 0; i
< tab
->bset
->n_div
; ++i
) {
1413 if (isl_int_ne(tab
->bset
->div
[i
][0], denom
))
1415 if (!isl_seq_eq(tab
->bset
->div
[i
] + 1, div
, total
))
1422 /* Return the index of a div that corresponds to "div".
1423 * We first check if we already have such a div and if not, we create one.
1425 static int get_div(struct isl_tab
*tab
, struct isl_tab
**context_tab
,
1426 struct isl_vec
*div
)
1430 d
= find_div(*context_tab
, div
->el
+ 1, div
->el
[0]);
1434 return add_div(tab
, context_tab
, div
);
1437 /* Add a parametric cut to cut away the non-integral sample value
1439 * Let a_i be the coefficients of the constant term and the parameters
1440 * and let b_i be the coefficients of the variables or constraints
1441 * in basis of the tableau.
1442 * Let q be the div q = floor(\sum_i {-a_i} y_i).
1444 * The cut is expressed as
1446 * c = \sum_i -{-a_i} y_i + \sum_i {b_i} x_i + q >= 0
1448 * If q did not already exist in the context tableau, then it is added first.
1449 * If q is in a column of the main tableau then the "+ q" can be accomplished
1450 * by setting the corresponding entry to the denominator of the constraint.
1451 * If q happens to be in a row of the main tableau, then the corresponding
1452 * row needs to be added instead (taking care of the denominators).
1453 * Note that this is very unlikely, but perhaps not entirely impossible.
1455 * The current value of the cut is known to be negative (or at least
1456 * non-positive), so row_sign is set accordingly.
1458 * Return the row of the cut or -1.
1460 static int add_parametric_cut(struct isl_tab
*tab
, int row
,
1461 struct isl_tab
**context_tab
)
1463 struct isl_vec
*div
;
1469 unsigned off
= 2 + tab
->M
;
1474 if (isl_tab_extend_cons(*context_tab
, 3) < 0)
1477 div
= get_row_parameter_div(tab
, row
);
1481 d
= get_div(tab
, context_tab
, div
);
1485 if (isl_tab_extend_cons(tab
, 1) < 0)
1487 r
= isl_tab_allocate_con(tab
);
1491 r_row
= tab
->mat
->row
[tab
->con
[r
].index
];
1492 isl_int_set(r_row
[0], tab
->mat
->row
[row
][0]);
1493 isl_int_neg(r_row
[1], tab
->mat
->row
[row
][1]);
1494 isl_int_fdiv_r(r_row
[1], r_row
[1], tab
->mat
->row
[row
][0]);
1495 isl_int_neg(r_row
[1], r_row
[1]);
1497 isl_int_set_si(r_row
[2], 0);
1498 for (i
= 0; i
< tab
->n_param
; ++i
) {
1499 if (tab
->var
[i
].is_row
)
1501 col
= tab
->var
[i
].index
;
1502 isl_int_neg(r_row
[off
+ col
], tab
->mat
->row
[row
][off
+ col
]);
1503 isl_int_fdiv_r(r_row
[off
+ col
], r_row
[off
+ col
],
1504 tab
->mat
->row
[row
][0]);
1505 isl_int_neg(r_row
[off
+ col
], r_row
[off
+ col
]);
1507 for (i
= 0; i
< tab
->n_div
; ++i
) {
1508 if (tab
->var
[tab
->n_var
- tab
->n_div
+ i
].is_row
)
1510 col
= tab
->var
[tab
->n_var
- tab
->n_div
+ i
].index
;
1511 isl_int_neg(r_row
[off
+ col
], tab
->mat
->row
[row
][off
+ col
]);
1512 isl_int_fdiv_r(r_row
[off
+ col
], r_row
[off
+ col
],
1513 tab
->mat
->row
[row
][0]);
1514 isl_int_neg(r_row
[off
+ col
], r_row
[off
+ col
]);
1516 for (i
= 0; i
< tab
->n_col
; ++i
) {
1517 if (tab
->col_var
[i
] >= 0 &&
1518 (tab
->col_var
[i
] < tab
->n_param
||
1519 tab
->col_var
[i
] >= tab
->n_var
- tab
->n_div
))
1521 isl_int_fdiv_r(r_row
[off
+ i
],
1522 tab
->mat
->row
[row
][off
+ i
], tab
->mat
->row
[row
][0]);
1524 if (tab
->var
[tab
->n_var
- tab
->n_div
+ d
].is_row
) {
1526 int d_row
= tab
->var
[tab
->n_var
- tab
->n_div
+ d
].index
;
1528 isl_int_gcd(gcd
, tab
->mat
->row
[d_row
][0], r_row
[0]);
1529 isl_int_divexact(r_row
[0], r_row
[0], gcd
);
1530 isl_int_divexact(gcd
, tab
->mat
->row
[d_row
][0], gcd
);
1531 isl_seq_combine(r_row
+ 1, gcd
, r_row
+ 1,
1532 r_row
[0], tab
->mat
->row
[d_row
] + 1,
1533 off
- 1 + tab
->n_col
);
1534 isl_int_mul(r_row
[0], r_row
[0], tab
->mat
->row
[d_row
][0]);
1537 col
= tab
->var
[tab
->n_var
- tab
->n_div
+ d
].index
;
1538 isl_int_set(r_row
[off
+ col
], tab
->mat
->row
[row
][0]);
1541 tab
->con
[r
].is_nonneg
= 1;
1542 isl_tab_push_var(tab
, isl_tab_undo_nonneg
, &tab
->con
[r
]);
1544 tab
->row_sign
[tab
->con
[r
].index
] = isl_tab_row_neg
;
1548 return tab
->con
[r
].index
;
1550 isl_tab_free(*context_tab
);
1551 *context_tab
= NULL
;
1555 /* Construct a tableau for bmap that can be used for computing
1556 * the lexicographic minimum (or maximum) of bmap.
1557 * If not NULL, then dom is the domain where the minimum
1558 * should be computed. In this case, we set up a parametric
1559 * tableau with row signs (initialized to "unknown").
1560 * If M is set, then the tableau will use a big parameter.
1561 * If max is set, then a maximum should be computed instead of a minimum.
1562 * This means that for each variable x, the tableau will contain the variable
1563 * x' = M - x, rather than x' = M + x. This in turn means that the coefficient
1564 * of the variables in all constraints are negated prior to adding them
1567 static struct isl_tab
*tab_for_lexmin(struct isl_basic_map
*bmap
,
1568 struct isl_basic_set
*dom
, unsigned M
, int max
)
1571 struct isl_tab
*tab
;
1573 tab
= isl_tab_alloc(bmap
->ctx
, 2 * bmap
->n_eq
+ bmap
->n_ineq
+ 1,
1574 isl_basic_map_total_dim(bmap
), M
);
1578 tab
->rational
= ISL_F_ISSET(bmap
, ISL_BASIC_MAP_RATIONAL
);
1580 tab
->n_param
= isl_basic_set_total_dim(dom
) - dom
->n_div
;
1581 tab
->n_div
= dom
->n_div
;
1582 tab
->row_sign
= isl_calloc_array(bmap
->ctx
,
1583 enum isl_tab_row_sign
, tab
->mat
->n_row
);
1587 if (ISL_F_ISSET(bmap
, ISL_BASIC_MAP_EMPTY
))
1588 return isl_tab_mark_empty(tab
);
1590 for (i
= tab
->n_param
; i
< tab
->n_var
- tab
->n_div
; ++i
) {
1591 tab
->var
[i
].is_nonneg
= 1;
1592 tab
->var
[i
].frozen
= 1;
1594 for (i
= 0; i
< bmap
->n_eq
; ++i
) {
1596 isl_seq_neg(bmap
->eq
[i
] + 1 + tab
->n_param
,
1597 bmap
->eq
[i
] + 1 + tab
->n_param
,
1598 tab
->n_var
- tab
->n_param
- tab
->n_div
);
1599 tab
= add_lexmin_valid_eq(tab
, bmap
->eq
[i
]);
1601 isl_seq_neg(bmap
->eq
[i
] + 1 + tab
->n_param
,
1602 bmap
->eq
[i
] + 1 + tab
->n_param
,
1603 tab
->n_var
- tab
->n_param
- tab
->n_div
);
1604 if (!tab
|| tab
->empty
)
1607 for (i
= 0; i
< bmap
->n_ineq
; ++i
) {
1609 isl_seq_neg(bmap
->ineq
[i
] + 1 + tab
->n_param
,
1610 bmap
->ineq
[i
] + 1 + tab
->n_param
,
1611 tab
->n_var
- tab
->n_param
- tab
->n_div
);
1612 tab
= add_lexmin_ineq(tab
, bmap
->ineq
[i
]);
1614 isl_seq_neg(bmap
->ineq
[i
] + 1 + tab
->n_param
,
1615 bmap
->ineq
[i
] + 1 + tab
->n_param
,
1616 tab
->n_var
- tab
->n_param
- tab
->n_div
);
1617 if (!tab
|| tab
->empty
)
1626 static struct isl_tab
*context_tab_for_lexmin(struct isl_basic_set
*bset
)
1628 struct isl_tab
*tab
;
1630 bset
= isl_basic_set_cow(bset
);
1633 tab
= tab_for_lexmin((struct isl_basic_map
*)bset
, NULL
, 1, 0);
1639 tab
->samples
= isl_mat_alloc(bset
->ctx
, 1, 1 + tab
->n_var
);
1644 isl_basic_set_free(bset
);
1648 /* Construct an isl_sol_map structure for accumulating the solution.
1649 * If track_empty is set, then we also keep track of the parts
1650 * of the context where there is no solution.
1651 * If max is set, then we are solving a maximization, rather than
1652 * a minimization problem, which means that the variables in the
1653 * tableau have value "M - x" rather than "M + x".
1655 static struct isl_sol_map
*sol_map_init(struct isl_basic_map
*bmap
,
1656 struct isl_basic_set
*dom
, int track_empty
, int max
)
1658 struct isl_sol_map
*sol_map
;
1659 struct isl_tab
*context_tab
;
1662 sol_map
= isl_calloc_type(bset
->ctx
, struct isl_sol_map
);
1667 sol_map
->sol
.add
= &sol_map_add_wrap
;
1668 sol_map
->sol
.free
= &sol_map_free_wrap
;
1669 sol_map
->map
= isl_map_alloc_dim(isl_basic_map_get_dim(bmap
), 1,
1674 context_tab
= context_tab_for_lexmin(isl_basic_set_copy(dom
));
1675 context_tab
= restore_lexmin(context_tab
);
1676 sol_map
->sol
.context_tab
= context_tab
;
1677 f
= context_is_feasible(&sol_map
->sol
);
1682 sol_map
->empty
= isl_set_alloc_dim(isl_basic_set_get_dim(dom
),
1683 1, ISL_SET_DISJOINT
);
1684 if (!sol_map
->empty
)
1688 isl_basic_set_free(dom
);
1691 isl_basic_set_free(dom
);
1692 sol_map_free(sol_map
);
1696 /* For each variable in the context tableau, check if the variable can
1697 * only attain non-negative values. If so, mark the parameter as non-negative
1698 * in the main tableau. This allows for a more direct identification of some
1699 * cases of violated constraints.
1701 static struct isl_tab
*tab_detect_nonnegative_parameters(struct isl_tab
*tab
,
1702 struct isl_tab
*context_tab
)
1705 struct isl_tab_undo
*snap
, *snap2
;
1706 struct isl_vec
*ineq
= NULL
;
1707 struct isl_tab_var
*var
;
1710 if (context_tab
->n_var
== 0)
1713 ineq
= isl_vec_alloc(tab
->mat
->ctx
, 1 + context_tab
->n_var
);
1717 if (isl_tab_extend_cons(context_tab
, 1) < 0)
1720 snap
= isl_tab_snap(context_tab
);
1721 isl_tab_push_basis(context_tab
);
1723 snap2
= isl_tab_snap(context_tab
);
1726 isl_seq_clr(ineq
->el
, ineq
->size
);
1727 for (i
= 0; i
< context_tab
->n_var
; ++i
) {
1728 isl_int_set_si(ineq
->el
[1 + i
], 1);
1729 context_tab
= isl_tab_add_ineq(context_tab
, ineq
->el
);
1730 var
= &context_tab
->con
[context_tab
->n_con
- 1];
1731 if (!context_tab
->empty
&&
1732 !isl_tab_min_at_most_neg_one(context_tab
, var
)) {
1734 if (i
>= tab
->n_param
)
1735 j
= i
- tab
->n_param
+ tab
->n_var
- tab
->n_div
;
1736 tab
->var
[j
].is_nonneg
= 1;
1739 isl_int_set_si(ineq
->el
[1 + i
], 0);
1740 if (isl_tab_rollback(context_tab
, snap2
) < 0)
1744 if (isl_tab_rollback(context_tab
, snap
) < 0)
1747 if (n
== context_tab
->n_var
) {
1748 context_tab
->mat
= isl_mat_drop_cols(context_tab
->mat
, 2, 1);
1760 /* Check whether all coefficients of (non-parameter) variables
1761 * are non-positive, meaning that no pivots can be performed on the row.
1763 static int is_critical(struct isl_tab
*tab
, int row
)
1766 unsigned off
= 2 + tab
->M
;
1768 for (j
= tab
->n_dead
; j
< tab
->n_col
; ++j
) {
1769 if (tab
->col_var
[j
] >= 0 &&
1770 (tab
->col_var
[j
] < tab
->n_param
||
1771 tab
->col_var
[j
] >= tab
->n_var
- tab
->n_div
))
1774 if (isl_int_is_pos(tab
->mat
->row
[row
][off
+ j
]))
1781 /* Check whether the inequality represented by vec is strict over the integers,
1782 * i.e., there are no integer values satisfying the constraint with
1783 * equality. This happens if the gcd of the coefficients is not a divisor
1784 * of the constant term. If so, scale the constraint down by the gcd
1785 * of the coefficients.
1787 static int is_strict(struct isl_vec
*vec
)
1793 isl_seq_gcd(vec
->el
+ 1, vec
->size
- 1, &gcd
);
1794 if (!isl_int_is_one(gcd
)) {
1795 strict
= !isl_int_is_divisible_by(vec
->el
[0], gcd
);
1796 isl_int_fdiv_q(vec
->el
[0], vec
->el
[0], gcd
);
1797 isl_seq_scale_down(vec
->el
+ 1, vec
->el
+ 1, gcd
, vec
->size
-1);
1804 /* Determine the sign of the given row of the main tableau.
1805 * The result is one of
1806 * isl_tab_row_pos: always non-negative; no pivot needed
1807 * isl_tab_row_neg: always non-positive; pivot
1808 * isl_tab_row_any: can be both positive and negative; split
1810 * We first handle some simple cases
1811 * - the row sign may be known already
1812 * - the row may be obviously non-negative
1813 * - the parametric constant may be equal to that of another row
1814 * for which we know the sign. This sign will be either "pos" or
1815 * "any". If it had been "neg" then we would have pivoted before.
1817 * If none of these cases hold, we check the value of the row for each
1818 * of the currently active samples. Based on the signs of these values
1819 * we make an initial determination of the sign of the row.
1821 * all zero -> unk(nown)
1822 * all non-negative -> pos
1823 * all non-positive -> neg
1824 * both negative and positive -> all
1826 * If we end up with "all", we are done.
1827 * Otherwise, we perform a check for positive and/or negative
1828 * values as follows.
1830 * samples neg unk pos
1836 * There is no special sign for "zero", because we can usually treat zero
1837 * as either non-negative or non-positive, whatever works out best.
1838 * However, if the row is "critical", meaning that pivoting is impossible
1839 * then we don't want to limp zero with the non-positive case, because
1840 * then we we would lose the solution for those values of the parameters
1841 * where the value of the row is zero. Instead, we treat 0 as non-negative
1842 * ensuring a split if the row can attain both zero and negative values.
1843 * The same happens when the original constraint was one that could not
1844 * be satisfied with equality by any integer values of the parameters.
1845 * In this case, we normalize the constraint, but then a value of zero
1846 * for the normalized constraint is actually a positive value for the
1847 * original constraint, so again we need to treat zero as non-negative.
1848 * In both these cases, we have the following decision tree instead:
1850 * all non-negative -> pos
1851 * all negative -> neg
1852 * both negative and non-negative -> all
1860 static int row_sign(struct isl_tab
*tab
, struct isl_sol
*sol
, int row
)
1863 struct isl_tab_undo
*snap
= NULL
;
1864 struct isl_vec
*ineq
= NULL
;
1865 int res
= isl_tab_row_unknown
;
1871 struct isl_tab
*context_tab
= sol
->context_tab
;
1873 if (tab
->row_sign
[row
] != isl_tab_row_unknown
)
1874 return tab
->row_sign
[row
];
1875 if (is_obviously_nonneg(tab
, row
))
1876 return isl_tab_row_pos
;
1877 for (row2
= tab
->n_redundant
; row2
< tab
->n_row
; ++row2
) {
1878 if (tab
->row_sign
[row2
] == isl_tab_row_unknown
)
1880 if (identical_parameter_line(tab
, row
, row2
))
1881 return tab
->row_sign
[row2
];
1884 critical
= is_critical(tab
, row
);
1886 isl_assert(tab
->mat
->ctx
, context_tab
->samples
, goto error
);
1887 isl_assert(tab
->mat
->ctx
, context_tab
->samples
->n_col
== 1 + context_tab
->n_var
, goto error
);
1889 ineq
= get_row_parameter_ineq(tab
, row
);
1893 strict
= is_strict(ineq
);
1896 for (i
= context_tab
->n_outside
; i
< context_tab
->n_sample
; ++i
) {
1897 isl_seq_inner_product(context_tab
->samples
->row
[i
], ineq
->el
,
1899 sgn
= isl_int_sgn(tmp
);
1900 if (sgn
> 0 || (sgn
== 0 && (critical
|| strict
))) {
1901 if (res
== isl_tab_row_unknown
)
1902 res
= isl_tab_row_pos
;
1903 if (res
== isl_tab_row_neg
)
1904 res
= isl_tab_row_any
;
1907 if (res
== isl_tab_row_unknown
)
1908 res
= isl_tab_row_neg
;
1909 if (res
== isl_tab_row_pos
)
1910 res
= isl_tab_row_any
;
1912 if (res
== isl_tab_row_any
)
1917 if (res
!= isl_tab_row_any
) {
1918 if (isl_tab_extend_cons(context_tab
, 1) < 0)
1921 snap
= isl_tab_snap(context_tab
);
1922 isl_tab_push_basis(context_tab
);
1925 if (res
== isl_tab_row_unknown
|| res
== isl_tab_row_pos
) {
1926 /* test for negative values */
1928 isl_seq_neg(ineq
->el
, ineq
->el
, ineq
->size
);
1929 isl_int_sub_ui(ineq
->el
[0], ineq
->el
[0], 1);
1931 isl_tab_push_basis(context_tab
);
1932 sol
->context_tab
= add_lexmin_ineq(sol
->context_tab
, ineq
->el
);
1933 feasible
= context_is_feasible(sol
);
1936 context_tab
= sol
->context_tab
;
1938 res
= isl_tab_row_pos
;
1940 res
= (res
== isl_tab_row_unknown
) ? isl_tab_row_neg
1942 if (isl_tab_rollback(context_tab
, snap
) < 0)
1945 if (res
== isl_tab_row_neg
) {
1946 isl_seq_neg(ineq
->el
, ineq
->el
, ineq
->size
);
1947 isl_int_sub_ui(ineq
->el
[0], ineq
->el
[0], 1);
1951 if (res
== isl_tab_row_neg
) {
1952 /* test for positive values */
1954 if (!critical
&& !strict
)
1955 isl_int_sub_ui(ineq
->el
[0], ineq
->el
[0], 1);
1957 isl_tab_push_basis(context_tab
);
1958 sol
->context_tab
= add_lexmin_ineq(sol
->context_tab
, ineq
->el
);
1959 feasible
= context_is_feasible(sol
);
1962 context_tab
= sol
->context_tab
;
1964 res
= isl_tab_row_any
;
1965 if (isl_tab_rollback(context_tab
, snap
) < 0)
1976 static struct isl_sol
*find_solutions(struct isl_sol
*sol
, struct isl_tab
*tab
);
1978 /* Find solutions for values of the parameters that satisfy the given
1981 * We currently take a snapshot of the context tableau that is reset
1982 * when we return from this function, while we make a copy of the main
1983 * tableau, leaving the original main tableau untouched.
1984 * These are fairly arbitrary choices. Making a copy also of the context
1985 * tableau would obviate the need to undo any changes made to it later,
1986 * while taking a snapshot of the main tableau could reduce memory usage.
1987 * If we were to switch to taking a snapshot of the main tableau,
1988 * we would have to keep in mind that we need to save the row signs
1989 * and that we need to do this before saving the current basis
1990 * such that the basis has been restore before we restore the row signs.
1992 static struct isl_sol
*find_in_pos(struct isl_sol
*sol
,
1993 struct isl_tab
*tab
, isl_int
*ineq
)
1995 struct isl_tab_undo
*snap
;
1997 snap
= isl_tab_snap(sol
->context_tab
);
1998 isl_tab_push_basis(sol
->context_tab
);
1999 if (isl_tab_extend_cons(sol
->context_tab
, 1) < 0)
2002 tab
= isl_tab_dup(tab
);
2006 sol
->context_tab
= add_lexmin_ineq(sol
->context_tab
, ineq
);
2007 sol
->context_tab
= check_samples(sol
->context_tab
, ineq
, 0);
2009 sol
= find_solutions(sol
, tab
);
2011 isl_tab_rollback(sol
->context_tab
, snap
);
2014 isl_tab_rollback(sol
->context_tab
, snap
);
2019 /* Record the absence of solutions for those values of the parameters
2020 * that do not satisfy the given inequality with equality.
2022 static struct isl_sol
*no_sol_in_strict(struct isl_sol
*sol
,
2023 struct isl_tab
*tab
, struct isl_vec
*ineq
)
2027 struct isl_tab_undo
*snap
;
2028 snap
= isl_tab_snap(sol
->context_tab
);
2029 isl_tab_push_basis(sol
->context_tab
);
2030 if (isl_tab_extend_cons(sol
->context_tab
, 1) < 0)
2033 isl_int_sub_ui(ineq
->el
[0], ineq
->el
[0], 1);
2035 sol
->context_tab
= add_lexmin_ineq(sol
->context_tab
, ineq
->el
);
2036 f
= context_valid_sample_or_feasible(sol
, ineq
->el
, 0);
2042 sol
= sol
->add(sol
, tab
);
2045 isl_int_add_ui(ineq
->el
[0], ineq
->el
[0], 1);
2047 if (isl_tab_rollback(sol
->context_tab
, snap
) < 0)
2055 /* Given a main tableau where more than one row requires a split,
2056 * determine and return the "best" row to split on.
2058 * Given two rows in the main tableau, if the inequality corresponding
2059 * to the first row is redundant with respect to that of the second row
2060 * in the current tableau, then it is better to split on the second row,
2061 * since in the positive part, both row will be positive.
2062 * (In the negative part a pivot will have to be performed and just about
2063 * anything can happen to the sign of the other row.)
2065 * As a simple heuristic, we therefore select the row that makes the most
2066 * of the other rows redundant.
2068 * Perhaps it would also be useful to look at the number of constraints
2069 * that conflict with any given constraint.
2071 static int best_split(struct isl_tab
*tab
, struct isl_tab
*context_tab
)
2073 struct isl_tab_undo
*snap
, *snap2
;
2079 if (isl_tab_extend_cons(context_tab
, 2) < 0)
2082 snap
= isl_tab_snap(context_tab
);
2083 isl_tab_push_basis(context_tab
);
2084 snap2
= isl_tab_snap(context_tab
);
2086 for (split
= tab
->n_redundant
; split
< tab
->n_row
; ++split
) {
2087 struct isl_tab_undo
*snap3
;
2088 struct isl_vec
*ineq
= NULL
;
2091 if (!isl_tab_var_from_row(tab
, split
)->is_nonneg
)
2093 if (tab
->row_sign
[split
] != isl_tab_row_any
)
2096 ineq
= get_row_parameter_ineq(tab
, split
);
2099 context_tab
= isl_tab_add_ineq(context_tab
, ineq
->el
);
2102 snap3
= isl_tab_snap(context_tab
);
2104 for (row
= tab
->n_redundant
; row
< tab
->n_row
; ++row
) {
2105 struct isl_tab_var
*var
;
2109 if (!isl_tab_var_from_row(tab
, row
)->is_nonneg
)
2111 if (tab
->row_sign
[row
] != isl_tab_row_any
)
2114 ineq
= get_row_parameter_ineq(tab
, row
);
2117 context_tab
= isl_tab_add_ineq(context_tab
, ineq
->el
);
2119 var
= &context_tab
->con
[context_tab
->n_con
- 1];
2120 if (!context_tab
->empty
&&
2121 !isl_tab_min_at_most_neg_one(context_tab
, var
))
2123 if (isl_tab_rollback(context_tab
, snap3
) < 0)
2126 if (best
== -1 || r
> best_r
) {
2130 if (isl_tab_rollback(context_tab
, snap2
) < 0)
2134 if (isl_tab_rollback(context_tab
, snap
) < 0)
2140 /* Compute the lexicographic minimum of the set represented by the main
2141 * tableau "tab" within the context "sol->context_tab".
2142 * On entry the sample value of the main tableau is lexicographically
2143 * less than or equal to this lexicographic minimum.
2144 * Pivots are performed until a feasible point is found, which is then
2145 * necessarily equal to the minimum, or until the tableau is found to
2146 * be infeasible. Some pivots may need to be performed for only some
2147 * feasible values of the context tableau. If so, the context tableau
2148 * is split into a part where the pivot is needed and a part where it is not.
2150 * Whenever we enter the main loop, the main tableau is such that no
2151 * "obvious" pivots need to be performed on it, where "obvious" means
2152 * that the given row can be seen to be negative without looking at
2153 * the context tableau. In particular, for non-parametric problems,
2154 * no pivots need to be performed on the main tableau.
2155 * The caller of find_solutions is responsible for making this property
2156 * hold prior to the first iteration of the loop, while restore_lexmin
2157 * is called before every other iteration.
2159 * Inside the main loop, we first examine the signs of the rows of
2160 * the main tableau within the context of the context tableau.
2161 * If we find a row that is always non-positive for all values of
2162 * the parameters satisfying the context tableau and negative for at
2163 * least one value of the parameters, we perform the appropriate pivot
2164 * and start over. An exception is the case where no pivot can be
2165 * performed on the row. In this case, we require that the sign of
2166 * the row is negative for all values of the parameters (rather than just
2167 * non-positive). This special case is handled inside row_sign, which
2168 * will say that the row can have any sign if it determines that it can
2169 * attain both negative and zero values.
2171 * If we can't find a row that always requires a pivot, but we can find
2172 * one or more rows that require a pivot for some values of the parameters
2173 * (i.e., the row can attain both positive and negative signs), then we split
2174 * the context tableau into two parts, one where we force the sign to be
2175 * non-negative and one where we force is to be negative.
2176 * The non-negative part is handled by a recursive call (through find_in_pos).
2177 * Upon returning from this call, we continue with the negative part and
2178 * perform the required pivot.
2180 * If no such rows can be found, all rows are non-negative and we have
2181 * found a (rational) feasible point. If we only wanted a rational point
2183 * Otherwise, we check if all values of the sample point of the tableau
2184 * are integral for the variables. If so, we have found the minimal
2185 * integral point and we are done.
2186 * If the sample point is not integral, then we need to make a distinction
2187 * based on whether the constant term is non-integral or the coefficients
2188 * of the parameters. Furthermore, in order to decide how to handle
2189 * the non-integrality, we also need to know whether the coefficients
2190 * of the other columns in the tableau are integral. This leads
2191 * to the following table. The first two rows do not correspond
2192 * to a non-integral sample point and are only mentioned for completeness.
2194 * constant parameters other
2197 * int int rat | -> no problem
2199 * rat int int -> fail
2201 * rat int rat -> cut
2204 * rat rat rat | -> parametric cut
2207 * rat rat int | -> split context
2209 * If the parametric constant is completely integral, then there is nothing
2210 * to be done. If the constant term is non-integral, but all the other
2211 * coefficient are integral, then there is nothing that can be done
2212 * and the tableau has no integral solution.
2213 * If, on the other hand, one or more of the other columns have rational
2214 * coeffcients, but the parameter coefficients are all integral, then
2215 * we can perform a regular (non-parametric) cut.
2216 * Finally, if there is any parameter coefficient that is non-integral,
2217 * then we need to involve the context tableau. There are two cases here.
2218 * If at least one other column has a rational coefficient, then we
2219 * can perform a parametric cut in the main tableau by adding a new
2220 * integer division in the context tableau.
2221 * If all other columns have integral coefficients, then we need to
2222 * enforce that the rational combination of parameters (c + \sum a_i y_i)/m
2223 * is always integral. We do this by introducing an integer division
2224 * q = floor((c + \sum a_i y_i)/m) and stipulating that its argument should
2225 * always be integral in the context tableau, i.e., m q = c + \sum a_i y_i.
2226 * Since q is expressed in the tableau as
2227 * c + \sum a_i y_i - m q >= 0
2228 * -c - \sum a_i y_i + m q + m - 1 >= 0
2229 * it is sufficient to add the inequality
2230 * -c - \sum a_i y_i + m q >= 0
2231 * In the part of the context where this inequality does not hold, the
2232 * main tableau is marked as being empty.
2234 static struct isl_sol
*find_solutions(struct isl_sol
*sol
, struct isl_tab
*tab
)
2236 struct isl_tab
**context_tab
;
2241 context_tab
= &sol
->context_tab
;
2245 if ((*context_tab
)->empty
)
2248 for (; tab
&& !tab
->empty
; tab
= restore_lexmin(tab
)) {
2255 for (row
= tab
->n_redundant
; row
< tab
->n_row
; ++row
) {
2256 if (!isl_tab_var_from_row(tab
, row
)->is_nonneg
)
2258 sgn
= row_sign(tab
, sol
, row
);
2261 tab
->row_sign
[row
] = sgn
;
2262 if (sgn
== isl_tab_row_any
)
2264 if (sgn
== isl_tab_row_any
&& split
== -1)
2266 if (sgn
== isl_tab_row_neg
)
2269 if (row
< tab
->n_row
)
2272 struct isl_vec
*ineq
;
2274 split
= best_split(tab
, *context_tab
);
2277 ineq
= get_row_parameter_ineq(tab
, split
);
2281 for (row
= tab
->n_redundant
; row
< tab
->n_row
; ++row
) {
2282 if (!isl_tab_var_from_row(tab
, row
)->is_nonneg
)
2284 if (tab
->row_sign
[row
] == isl_tab_row_any
)
2285 tab
->row_sign
[row
] = isl_tab_row_unknown
;
2287 tab
->row_sign
[split
] = isl_tab_row_pos
;
2288 sol
= find_in_pos(sol
, tab
, ineq
->el
);
2289 tab
->row_sign
[split
] = isl_tab_row_neg
;
2291 isl_seq_neg(ineq
->el
, ineq
->el
, ineq
->size
);
2292 isl_int_sub_ui(ineq
->el
[0], ineq
->el
[0], 1);
2293 *context_tab
= add_lexmin_ineq(*context_tab
, ineq
->el
);
2294 *context_tab
= check_samples(*context_tab
, ineq
->el
, 0);
2302 row
= first_non_integer(tab
, &flags
);
2305 if (ISL_FL_ISSET(flags
, I_PAR
)) {
2306 if (ISL_FL_ISSET(flags
, I_VAR
)) {
2307 tab
= isl_tab_mark_empty(tab
);
2310 row
= add_cut(tab
, row
);
2311 } else if (ISL_FL_ISSET(flags
, I_VAR
)) {
2312 struct isl_vec
*div
;
2313 struct isl_vec
*ineq
;
2315 if (isl_tab_extend_cons(*context_tab
, 3) < 0)
2317 div
= get_row_split_div(tab
, row
);
2320 d
= get_div(tab
, context_tab
, div
);
2324 ineq
= ineq_for_div((*context_tab
)->bset
, d
);
2325 sol
= no_sol_in_strict(sol
, tab
, ineq
);
2326 isl_seq_neg(ineq
->el
, ineq
->el
, ineq
->size
);
2327 *context_tab
= add_lexmin_ineq(*context_tab
, ineq
->el
);
2328 *context_tab
= check_samples(*context_tab
, ineq
->el
, 0);
2332 tab
= set_row_cst_to_div(tab
, row
, d
);
2334 row
= add_parametric_cut(tab
, row
, context_tab
);
2339 sol
= sol
->add(sol
, tab
);
2348 /* Compute the lexicographic minimum of the set represented by the main
2349 * tableau "tab" within the context "sol->context_tab".
2351 * As a preprocessing step, we first transfer all the purely parametric
2352 * equalities from the main tableau to the context tableau, i.e.,
2353 * parameters that have been pivoted to a row.
2354 * These equalities are ignored by the main algorithm, because the
2355 * corresponding rows may not be marked as being non-negative.
2356 * In parts of the context where the added equality does not hold,
2357 * the main tableau is marked as being empty.
2359 static struct isl_sol
*find_solutions_main(struct isl_sol
*sol
,
2360 struct isl_tab
*tab
)
2364 for (row
= tab
->n_redundant
; row
< tab
->n_row
; ++row
) {
2368 if (tab
->row_var
[row
] < 0)
2370 if (tab
->row_var
[row
] >= tab
->n_param
&&
2371 tab
->row_var
[row
] < tab
->n_var
- tab
->n_div
)
2373 if (tab
->row_var
[row
] < tab
->n_param
)
2374 p
= tab
->row_var
[row
];
2376 p
= tab
->row_var
[row
]
2377 + tab
->n_param
- (tab
->n_var
- tab
->n_div
);
2379 if (isl_tab_extend_cons(sol
->context_tab
, 2) < 0)
2382 eq
= isl_vec_alloc(tab
->mat
->ctx
, 1+tab
->n_param
+tab
->n_div
);
2383 get_row_parameter_line(tab
, row
, eq
->el
);
2384 isl_int_neg(eq
->el
[1 + p
], tab
->mat
->row
[row
][0]);
2385 eq
= isl_vec_normalize(eq
);
2387 sol
= no_sol_in_strict(sol
, tab
, eq
);
2389 isl_seq_neg(eq
->el
, eq
->el
, eq
->size
);
2390 sol
= no_sol_in_strict(sol
, tab
, eq
);
2391 isl_seq_neg(eq
->el
, eq
->el
, eq
->size
);
2393 sol
->context_tab
= add_lexmin_eq(sol
->context_tab
, eq
->el
);
2394 context_valid_sample_or_feasible(sol
, eq
->el
, 1);
2395 sol
->context_tab
= check_samples(sol
->context_tab
, eq
->el
, 1);
2399 isl_tab_mark_redundant(tab
, row
);
2401 if (!sol
->context_tab
)
2403 if (sol
->context_tab
->empty
)
2406 row
= tab
->n_redundant
- 1;
2409 return find_solutions(sol
, tab
);
2416 static struct isl_sol_map
*sol_map_find_solutions(struct isl_sol_map
*sol_map
,
2417 struct isl_tab
*tab
)
2419 return (struct isl_sol_map
*)find_solutions_main(&sol_map
->sol
, tab
);
2422 /* Check if integer division "div" of "dom" also occurs in "bmap".
2423 * If so, return its position within the divs.
2424 * If not, return -1.
2426 static int find_context_div(struct isl_basic_map
*bmap
,
2427 struct isl_basic_set
*dom
, unsigned div
)
2430 unsigned b_dim
= isl_dim_total(bmap
->dim
);
2431 unsigned d_dim
= isl_dim_total(dom
->dim
);
2433 if (isl_int_is_zero(dom
->div
[div
][0]))
2435 if (isl_seq_first_non_zero(dom
->div
[div
] + 2 + d_dim
, dom
->n_div
) != -1)
2438 for (i
= 0; i
< bmap
->n_div
; ++i
) {
2439 if (isl_int_is_zero(bmap
->div
[i
][0]))
2441 if (isl_seq_first_non_zero(bmap
->div
[i
] + 2 + d_dim
,
2442 (b_dim
- d_dim
) + bmap
->n_div
) != -1)
2444 if (isl_seq_eq(bmap
->div
[i
], dom
->div
[div
], 2 + d_dim
))
2450 /* The correspondence between the variables in the main tableau,
2451 * the context tableau, and the input map and domain is as follows.
2452 * The first n_param and the last n_div variables of the main tableau
2453 * form the variables of the context tableau.
2454 * In the basic map, these n_param variables correspond to the
2455 * parameters and the input dimensions. In the domain, they correspond
2456 * to the parameters and the set dimensions.
2457 * The n_div variables correspond to the integer divisions in the domain.
2458 * To ensure that everything lines up, we may need to copy some of the
2459 * integer divisions of the domain to the map. These have to be placed
2460 * in the same order as those in the context and they have to be placed
2461 * after any other integer divisions that the map may have.
2462 * This function performs the required reordering.
2464 static struct isl_basic_map
*align_context_divs(struct isl_basic_map
*bmap
,
2465 struct isl_basic_set
*dom
)
2471 for (i
= 0; i
< dom
->n_div
; ++i
)
2472 if (find_context_div(bmap
, dom
, i
) != -1)
2474 other
= bmap
->n_div
- common
;
2475 if (dom
->n_div
- common
> 0) {
2476 bmap
= isl_basic_map_extend_dim(bmap
, isl_dim_copy(bmap
->dim
),
2477 dom
->n_div
- common
, 0, 0);
2481 for (i
= 0; i
< dom
->n_div
; ++i
) {
2482 int pos
= find_context_div(bmap
, dom
, i
);
2484 pos
= isl_basic_map_alloc_div(bmap
);
2487 isl_int_set_si(bmap
->div
[pos
][0], 0);
2489 if (pos
!= other
+ i
)
2490 isl_basic_map_swap_div(bmap
, pos
, other
+ i
);
2494 isl_basic_map_free(bmap
);
2498 /* Compute the lexicographic minimum (or maximum if "max" is set)
2499 * of "bmap" over the domain "dom" and return the result as a map.
2500 * If "empty" is not NULL, then *empty is assigned a set that
2501 * contains those parts of the domain where there is no solution.
2502 * If "bmap" is marked as rational (ISL_BASIC_MAP_RATIONAL),
2503 * then we compute the rational optimum. Otherwise, we compute
2504 * the integral optimum.
2506 * We perform some preprocessing. As the PILP solver does not
2507 * handle implicit equalities very well, we first make sure all
2508 * the equalities are explicitly available.
2509 * We also make sure the divs in the domain are properly order,
2510 * because they will be added one by one in the given order
2511 * during the construction of the solution map.
2513 struct isl_map
*isl_tab_basic_map_partial_lexopt(
2514 struct isl_basic_map
*bmap
, struct isl_basic_set
*dom
,
2515 struct isl_set
**empty
, int max
)
2517 struct isl_tab
*tab
;
2518 struct isl_map
*result
= NULL
;
2519 struct isl_sol_map
*sol_map
= NULL
;
2526 isl_assert(bmap
->ctx
,
2527 isl_basic_map_compatible_domain(bmap
, dom
), goto error
);
2529 bmap
= isl_basic_map_detect_equalities(bmap
);
2532 dom
= isl_basic_set_order_divs(dom
);
2533 bmap
= align_context_divs(bmap
, dom
);
2535 sol_map
= sol_map_init(bmap
, dom
, !!empty
, max
);
2539 if (isl_basic_set_fast_is_empty(sol_map
->sol
.context_tab
->bset
))
2541 else if (isl_basic_map_fast_is_empty(bmap
))
2542 sol_map
= add_empty(sol_map
);
2544 tab
= tab_for_lexmin(bmap
,
2545 sol_map
->sol
.context_tab
->bset
, 1, max
);
2546 tab
= tab_detect_nonnegative_parameters(tab
,
2547 sol_map
->sol
.context_tab
);
2548 sol_map
= sol_map_find_solutions(sol_map
, tab
);
2553 result
= isl_map_copy(sol_map
->map
);
2555 *empty
= isl_set_copy(sol_map
->empty
);
2556 sol_map_free(sol_map
);
2557 isl_basic_map_free(bmap
);
2560 sol_map_free(sol_map
);
2561 isl_basic_map_free(bmap
);
2565 struct isl_sol_for
{
2567 int (*fn
)(__isl_take isl_basic_set
*dom
,
2568 __isl_take isl_mat
*map
, void *user
);
2573 static void sol_for_free(struct isl_sol_for
*sol_for
)
2575 isl_tab_free(sol_for
->sol
.context_tab
);
2579 static void sol_for_free_wrap(struct isl_sol
*sol
)
2581 sol_for_free((struct isl_sol_for
*)sol
);
2584 /* Add the solution identified by the tableau and the context tableau.
2586 * See documentation of sol_map_add for more details.
2588 * Instead of constructing a basic map, this function calls a user
2589 * defined function with the current context as a basic set and
2590 * an affine matrix reprenting the relation between the input and output.
2591 * The number of rows in this matrix is equal to one plus the number
2592 * of output variables. The number of columns is equal to one plus
2593 * the total dimension of the context, i.e., the number of parameters,
2594 * input variables and divs. Since some of the columns in the matrix
2595 * may refer to the divs, the basic set is not simplified.
2596 * (Simplification may reorder or remove divs.)
2598 static struct isl_sol_for
*sol_for_add(struct isl_sol_for
*sol
,
2599 struct isl_tab
*tab
)
2601 struct isl_tab
*context_tab
;
2602 struct isl_basic_set
*bset
;
2603 struct isl_mat
*mat
= NULL
;
2615 context_tab
= sol
->sol
.context_tab
;
2617 n_out
= tab
->n_var
- tab
->n_param
- tab
->n_div
;
2618 mat
= isl_mat_alloc(tab
->mat
->ctx
, 1 + n_out
, 1 + tab
->n_param
+ tab
->n_div
);
2622 isl_seq_clr(mat
->row
[0] + 1, mat
->n_col
- 1);
2623 isl_int_set_si(mat
->row
[0][0], 1);
2624 for (row
= 0; row
< n_out
; ++row
) {
2625 int i
= tab
->n_param
+ row
;
2628 isl_seq_clr(mat
->row
[1 + row
], mat
->n_col
);
2629 if (!tab
->var
[i
].is_row
)
2632 r
= tab
->var
[i
].index
;
2635 isl_assert(mat
->ctx
, isl_int_eq(tab
->mat
->row
[r
][2],
2636 tab
->mat
->row
[r
][0]),
2638 isl_int_set(mat
->row
[1 + row
][0], tab
->mat
->row
[r
][1]);
2639 for (j
= 0; j
< tab
->n_param
; ++j
) {
2641 if (tab
->var
[j
].is_row
)
2643 col
= tab
->var
[j
].index
;
2644 isl_int_set(mat
->row
[1 + row
][1 + j
],
2645 tab
->mat
->row
[r
][off
+ col
]);
2647 for (j
= 0; j
< tab
->n_div
; ++j
) {
2649 if (tab
->var
[tab
->n_var
- tab
->n_div
+j
].is_row
)
2651 col
= tab
->var
[tab
->n_var
- tab
->n_div
+j
].index
;
2652 isl_int_set(mat
->row
[1 + row
][1 + tab
->n_param
+ j
],
2653 tab
->mat
->row
[r
][off
+ col
]);
2655 if (!isl_int_is_one(tab
->mat
->row
[r
][0]))
2656 isl_seq_scale_down(mat
->row
[1 + row
], mat
->row
[1 + row
],
2657 tab
->mat
->row
[r
][0], mat
->n_col
);
2659 isl_seq_neg(mat
->row
[1 + row
], mat
->row
[1 + row
],
2663 bset
= isl_basic_set_dup(context_tab
->bset
);
2664 bset
= isl_basic_set_finalize(bset
);
2666 if (sol
->fn(bset
, isl_mat_copy(mat
), sol
->user
) < 0)
2673 sol_free(&sol
->sol
);
2677 static struct isl_sol
*sol_for_add_wrap(struct isl_sol
*sol
,
2678 struct isl_tab
*tab
)
2680 return (struct isl_sol
*)sol_for_add((struct isl_sol_for
*)sol
, tab
);
2683 static struct isl_sol_for
*sol_for_init(struct isl_basic_map
*bmap
, int max
,
2684 int (*fn
)(__isl_take isl_basic_set
*dom
, __isl_take isl_mat
*map
,
2688 struct isl_sol_for
*sol_for
= NULL
;
2689 struct isl_dim
*dom_dim
;
2690 struct isl_basic_set
*dom
= NULL
;
2691 struct isl_tab
*context_tab
;
2694 sol_for
= isl_calloc_type(bset
->ctx
, struct isl_sol_for
);
2698 dom_dim
= isl_dim_domain(isl_dim_copy(bmap
->dim
));
2699 dom
= isl_basic_set_universe(dom_dim
);
2702 sol_for
->user
= user
;
2704 sol_for
->sol
.add
= &sol_for_add_wrap
;
2705 sol_for
->sol
.free
= &sol_for_free_wrap
;
2707 context_tab
= context_tab_for_lexmin(isl_basic_set_copy(dom
));
2708 context_tab
= restore_lexmin(context_tab
);
2709 sol_for
->sol
.context_tab
= context_tab
;
2710 f
= context_is_feasible(&sol_for
->sol
);
2714 isl_basic_set_free(dom
);
2717 isl_basic_set_free(dom
);
2718 sol_for_free(sol_for
);
2722 static struct isl_sol_for
*sol_for_find_solutions(struct isl_sol_for
*sol_for
,
2723 struct isl_tab
*tab
)
2725 return (struct isl_sol_for
*)find_solutions_main(&sol_for
->sol
, tab
);
2728 int isl_basic_map_foreach_lexopt(__isl_keep isl_basic_map
*bmap
, int max
,
2729 int (*fn
)(__isl_take isl_basic_set
*dom
, __isl_take isl_mat
*map
,
2733 struct isl_sol_for
*sol_for
= NULL
;
2735 bmap
= isl_basic_map_copy(bmap
);
2739 bmap
= isl_basic_map_detect_equalities(bmap
);
2740 sol_for
= sol_for_init(bmap
, max
, fn
, user
);
2742 if (isl_basic_map_fast_is_empty(bmap
))
2745 struct isl_tab
*tab
;
2746 tab
= tab_for_lexmin(bmap
,
2747 sol_for
->sol
.context_tab
->bset
, 1, max
);
2748 tab
= tab_detect_nonnegative_parameters(tab
,
2749 sol_for
->sol
.context_tab
);
2750 sol_for
= sol_for_find_solutions(sol_for
, tab
);
2755 sol_for_free(sol_for
);
2756 isl_basic_map_free(bmap
);
2759 sol_for_free(sol_for
);
2760 isl_basic_map_free(bmap
);
2764 int isl_basic_map_foreach_lexmin(__isl_keep isl_basic_map
*bmap
,
2765 int (*fn
)(__isl_take isl_basic_set
*dom
, __isl_take isl_mat
*map
,
2769 return isl_basic_map_foreach_lexopt(bmap
, 0, fn
, user
);
2772 int isl_basic_map_foreach_lexmax(__isl_keep isl_basic_map
*bmap
,
2773 int (*fn
)(__isl_take isl_basic_set
*dom
, __isl_take isl_mat
*map
,
2777 return isl_basic_map_foreach_lexopt(bmap
, 1, fn
, user
);