Merge branch 'maint'
[platform/upstream/isl.git] / isl_ilp.c
1 /*
2  * Copyright 2008-2009 Katholieke Universiteit Leuven
3  *
4  * Use of this software is governed by the GNU LGPLv2.1 license
5  *
6  * Written by Sven Verdoolaege, K.U.Leuven, Departement
7  * Computerwetenschappen, Celestijnenlaan 200A, B-3001 Leuven, Belgium
8  */
9
10 #include <isl_map_private.h>
11 #include <isl/ilp.h>
12 #include "isl_sample.h"
13 #include <isl/seq.h>
14 #include "isl_equalities.h"
15
16 /* Given a basic set "bset", construct a basic set U such that for
17  * each element x in U, the whole unit box positioned at x is inside
18  * the given basic set.
19  * Note that U may not contain all points that satisfy this property.
20  *
21  * We simply add the sum of all negative coefficients to the constant
22  * term.  This ensures that if x satisfies the resulting constraints,
23  * then x plus any sum of unit vectors satisfies the original constraints.
24  */
25 static struct isl_basic_set *unit_box_base_points(struct isl_basic_set *bset)
26 {
27         int i, j, k;
28         struct isl_basic_set *unit_box = NULL;
29         unsigned total;
30
31         if (!bset)
32                 goto error;
33
34         if (bset->n_eq != 0) {
35                 unit_box = isl_basic_set_empty_like(bset);
36                 isl_basic_set_free(bset);
37                 return unit_box;
38         }
39
40         total = isl_basic_set_total_dim(bset);
41         unit_box = isl_basic_set_alloc_dim(isl_basic_set_get_dim(bset),
42                                         0, 0, bset->n_ineq);
43
44         for (i = 0; i < bset->n_ineq; ++i) {
45                 k = isl_basic_set_alloc_inequality(unit_box);
46                 if (k < 0)
47                         goto error;
48                 isl_seq_cpy(unit_box->ineq[k], bset->ineq[i], 1 + total);
49                 for (j = 0; j < total; ++j) {
50                         if (isl_int_is_nonneg(unit_box->ineq[k][1 + j]))
51                                 continue;
52                         isl_int_add(unit_box->ineq[k][0],
53                                 unit_box->ineq[k][0], unit_box->ineq[k][1 + j]);
54                 }
55         }
56
57         isl_basic_set_free(bset);
58         return unit_box;
59 error:
60         isl_basic_set_free(bset);
61         isl_basic_set_free(unit_box);
62         return NULL;
63 }
64
65 /* Find an integer point in "bset", preferably one that is
66  * close to minimizing "f".
67  *
68  * We first check if we can easily put unit boxes inside bset.
69  * If so, we take the best base point of any of the unit boxes we can find
70  * and round it up to the nearest integer.
71  * If not, we simply pick any integer point in "bset".
72  */
73 static struct isl_vec *initial_solution(struct isl_basic_set *bset, isl_int *f)
74 {
75         enum isl_lp_result res;
76         struct isl_basic_set *unit_box;
77         struct isl_vec *sol;
78
79         unit_box = unit_box_base_points(isl_basic_set_copy(bset));
80
81         res = isl_basic_set_solve_lp(unit_box, 0, f, bset->ctx->one,
82                                         NULL, NULL, &sol);
83         if (res == isl_lp_ok) {
84                 isl_basic_set_free(unit_box);
85                 return isl_vec_ceil(sol);
86         }
87
88         isl_basic_set_free(unit_box);
89
90         return isl_basic_set_sample_vec(isl_basic_set_copy(bset));
91 }
92
93 /* Restrict "bset" to those points with values for f in the interval [l, u].
94  */
95 static struct isl_basic_set *add_bounds(struct isl_basic_set *bset,
96         isl_int *f, isl_int l, isl_int u)
97 {
98         int k;
99         unsigned total;
100
101         total = isl_basic_set_total_dim(bset);
102         bset = isl_basic_set_extend_constraints(bset, 0, 2);
103
104         k = isl_basic_set_alloc_inequality(bset);
105         if (k < 0)
106                 goto error;
107         isl_seq_cpy(bset->ineq[k], f, 1 + total);
108         isl_int_sub(bset->ineq[k][0], bset->ineq[k][0], l);
109
110         k = isl_basic_set_alloc_inequality(bset);
111         if (k < 0)
112                 goto error;
113         isl_seq_neg(bset->ineq[k], f, 1 + total);
114         isl_int_add(bset->ineq[k][0], bset->ineq[k][0], u);
115
116         return bset;
117 error:
118         isl_basic_set_free(bset);
119         return NULL;
120 }
121
122 /* Find an integer point in "bset" that minimizes f (in any) such that
123  * the value of f lies inside the interval [l, u].
124  * Return this integer point if it can be found.
125  * Otherwise, return sol.
126  *
127  * We perform a number of steps until l > u.
128  * In each step, we look for an integer point with value in either
129  * the whole interval [l, u] or half of the interval [l, l+floor(u-l-1/2)].
130  * The choice depends on whether we have found an integer point in the
131  * previous step.  If so, we look for the next point in half of the remaining
132  * interval.
133  * If we find a point, the current solution is updated and u is set
134  * to its value minus 1.
135  * If no point can be found, we update l to the upper bound of the interval
136  * we checked (u or l+floor(u-l-1/2)) plus 1.
137  */
138 static struct isl_vec *solve_ilp_search(struct isl_basic_set *bset,
139         isl_int *f, isl_int *opt, struct isl_vec *sol, isl_int l, isl_int u)
140 {
141         isl_int tmp;
142         int divide = 1;
143
144         isl_int_init(tmp);
145
146         while (isl_int_le(l, u)) {
147                 struct isl_basic_set *slice;
148                 struct isl_vec *sample;
149
150                 if (!divide)
151                         isl_int_set(tmp, u);
152                 else {
153                         isl_int_sub(tmp, u, l);
154                         isl_int_fdiv_q_ui(tmp, tmp, 2);
155                         isl_int_add(tmp, tmp, l);
156                 }
157                 slice = add_bounds(isl_basic_set_copy(bset), f, l, tmp);
158                 sample = isl_basic_set_sample_vec(slice);
159                 if (!sample) {
160                         isl_vec_free(sol);
161                         sol = NULL;
162                         break;
163                 }
164                 if (sample->size > 0) {
165                         isl_vec_free(sol);
166                         sol = sample;
167                         isl_seq_inner_product(f, sol->el, sol->size, opt);
168                         isl_int_sub_ui(u, *opt, 1);
169                         divide = 1;
170                 } else {
171                         isl_vec_free(sample);
172                         if (!divide)
173                                 break;
174                         isl_int_add_ui(l, tmp, 1);
175                         divide = 0;
176                 }
177         }
178
179         isl_int_clear(tmp);
180
181         return sol;
182 }
183
184 /* Find an integer point in "bset" that minimizes f (if any).
185  * If sol_p is not NULL then the integer point is returned in *sol_p.
186  * The optimal value of f is returned in *opt.
187  *
188  * The algorithm maintains a currently best solution and an interval [l, u]
189  * of values of f for which integer solutions could potentially still be found.
190  * The initial value of the best solution so far is any solution.
191  * The initial value of l is minimal value of f over the rationals
192  * (rounded up to the nearest integer).
193  * The initial value of u is the value of f at the initial solution minus 1.
194  *
195  * We then call solve_ilp_search to perform a binary search on the interval.
196  */
197 static enum isl_lp_result solve_ilp(struct isl_basic_set *bset,
198                                       isl_int *f, isl_int *opt,
199                                       struct isl_vec **sol_p)
200 {
201         enum isl_lp_result res;
202         isl_int l, u;
203         struct isl_vec *sol;
204
205         res = isl_basic_set_solve_lp(bset, 0, f, bset->ctx->one,
206                                         opt, NULL, &sol);
207         if (res == isl_lp_ok && isl_int_is_one(sol->el[0])) {
208                 if (sol_p)
209                         *sol_p = sol;
210                 else
211                         isl_vec_free(sol);
212                 return isl_lp_ok;
213         }
214         isl_vec_free(sol);
215         if (res == isl_lp_error || res == isl_lp_empty)
216                 return res;
217
218         sol = initial_solution(bset, f);
219         if (!sol)
220                 return isl_lp_error;
221         if (sol->size == 0) {
222                 isl_vec_free(sol);
223                 return isl_lp_empty;
224         }
225         if (res == isl_lp_unbounded) {
226                 isl_vec_free(sol);
227                 return isl_lp_unbounded;
228         }
229
230         isl_int_init(l);
231         isl_int_init(u);
232
233         isl_int_set(l, *opt);
234
235         isl_seq_inner_product(f, sol->el, sol->size, opt);
236         isl_int_sub_ui(u, *opt, 1);
237
238         sol = solve_ilp_search(bset, f, opt, sol, l, u);
239         if (!sol)
240                 res = isl_lp_error;
241
242         isl_int_clear(l);
243         isl_int_clear(u);
244
245         if (sol_p)
246                 *sol_p = sol;
247         else
248                 isl_vec_free(sol);
249
250         return res;
251 }
252
253 static enum isl_lp_result solve_ilp_with_eq(struct isl_basic_set *bset, int max,
254                                       isl_int *f, isl_int *opt,
255                                       struct isl_vec **sol_p)
256 {
257         unsigned dim;
258         enum isl_lp_result res;
259         struct isl_mat *T = NULL;
260         struct isl_vec *v;
261
262         bset = isl_basic_set_copy(bset);
263         dim = isl_basic_set_total_dim(bset);
264         v = isl_vec_alloc(bset->ctx, 1 + dim);
265         if (!v)
266                 goto error;
267         isl_seq_cpy(v->el, f, 1 + dim);
268         bset = isl_basic_set_remove_equalities(bset, &T, NULL);
269         v = isl_vec_mat_product(v, isl_mat_copy(T));
270         if (!v)
271                 goto error;
272         res = isl_basic_set_solve_ilp(bset, max, v->el, opt, sol_p);
273         isl_vec_free(v);
274         if (res == isl_lp_ok && sol_p) {
275                 *sol_p = isl_mat_vec_product(T, *sol_p);
276                 if (!*sol_p)
277                         res = isl_lp_error;
278         } else
279                 isl_mat_free(T);
280         isl_basic_set_free(bset);
281         return res;
282 error:
283         isl_mat_free(T);
284         isl_basic_set_free(bset);
285         return isl_lp_error;
286 }
287
288 /* Find an integer point in "bset" that minimizes (or maximizes if max is set)
289  * f (if any).
290  * If sol_p is not NULL then the integer point is returned in *sol_p.
291  * The optimal value of f is returned in *opt.
292  *
293  * If there is any equality among the points in "bset", then we first
294  * project it out.  Otherwise, we continue with solve_ilp above.
295  */
296 enum isl_lp_result isl_basic_set_solve_ilp(struct isl_basic_set *bset, int max,
297                                       isl_int *f, isl_int *opt,
298                                       struct isl_vec **sol_p)
299 {
300         unsigned dim;
301         enum isl_lp_result res;
302
303         if (!bset)
304                 return isl_lp_error;
305         if (sol_p)
306                 *sol_p = NULL;
307
308         isl_assert(bset->ctx, isl_basic_set_n_param(bset) == 0, goto error);
309
310         if (isl_basic_set_fast_is_empty(bset))
311                 return isl_lp_empty;
312
313         if (bset->n_eq)
314                 return solve_ilp_with_eq(bset, max, f, opt, sol_p);
315
316         dim = isl_basic_set_total_dim(bset);
317
318         if (max)
319                 isl_seq_neg(f, f, 1 + dim);
320
321         res = solve_ilp(bset, f, opt, sol_p);
322
323         if (max) {
324                 isl_seq_neg(f, f, 1 + dim);
325                 isl_int_neg(*opt, *opt);
326         }
327
328         return res;
329 error:
330         isl_basic_set_free(bset);
331         return isl_lp_error;
332 }