isl_stream_read_map: fix handling of divs in tuples
[platform/upstream/isl.git] / basis_reduction_templ.c
1 /*
2  * Copyright 2006-2007 Universiteit Leiden
3  * Copyright 2008-2009 Katholieke Universiteit Leuven
4  *
5  * Use of this software is governed by the GNU LGPLv2.1 license
6  *
7  * Written by Sven Verdoolaege, Leiden Institute of Advanced Computer Science,
8  * Universiteit Leiden, Niels Bohrweg 1, 2333 CA Leiden, The Netherlands
9  * and K.U.Leuven, Departement Computerwetenschappen, Celestijnenlaan 200A,
10  * B-3001 Leuven, Belgium
11  */
12
13 #include <stdlib.h>
14 #include <isl_ctx_private.h>
15 #include <isl_map_private.h>
16 #include "isl_basis_reduction.h"
17
18 static void save_alpha(GBR_LP *lp, int first, int n, GBR_type *alpha)
19 {
20         int i;
21
22         for (i = 0; i < n; ++i)
23                 GBR_lp_get_alpha(lp, first + i, &alpha[i]);
24 }
25
26 /* Compute a reduced basis for the set represented by the tableau "tab".
27  * tab->basis, which must be initialized by the calling function to an affine
28  * unimodular basis, is updated to reflect the reduced basis.
29  * The first tab->n_zero rows of the basis (ignoring the constant row)
30  * are assumed to correspond to equalities and are left untouched.
31  * tab->n_zero is updated to reflect any additional equalities that
32  * have been detected in the first rows of the new basis.
33  * The final tab->n_unbounded rows of the basis are assumed to correspond
34  * to unbounded directions and are also left untouched.
35  * In particular this means that the remaining rows are assumed to
36  * correspond to bounded directions.
37  *
38  * This function implements the algorithm described in
39  * "An Implementation of the Generalized Basis Reduction Algorithm
40  *  for Integer Programming" of Cook el al. to compute a reduced basis.
41  * We use \epsilon = 1/4.
42  *
43  * If ctx->opt->gbr_only_first is set, the user is only interested
44  * in the first direction.  In this case we stop the basis reduction when
45  * the width in the first direction becomes smaller than 2.
46  */
47 struct isl_tab *isl_tab_compute_reduced_basis(struct isl_tab *tab)
48 {
49         unsigned dim;
50         struct isl_ctx *ctx;
51         struct isl_mat *B;
52         int unbounded;
53         int i;
54         GBR_LP *lp = NULL;
55         GBR_type F_old, alpha, F_new;
56         int row;
57         isl_int tmp;
58         struct isl_vec *b_tmp;
59         GBR_type *F = NULL;
60         GBR_type *alpha_buffer[2] = { NULL, NULL };
61         GBR_type *alpha_saved;
62         GBR_type F_saved;
63         int use_saved = 0;
64         isl_int mu[2];
65         GBR_type mu_F[2];
66         GBR_type two;
67         GBR_type one;
68         int empty = 0;
69         int fixed = 0;
70         int fixed_saved = 0;
71         int mu_fixed[2];
72         int n_bounded;
73         int gbr_only_first;
74
75         if (!tab)
76                 return NULL;
77
78         if (tab->empty)
79                 return tab;
80
81         ctx = tab->mat->ctx;
82         gbr_only_first = ctx->opt->gbr_only_first;
83         dim = tab->n_var;
84         B = tab->basis;
85         if (!B)
86                 return tab;
87
88         n_bounded = dim - tab->n_unbounded;
89         if (n_bounded <= tab->n_zero + 1)
90                 return tab;
91
92         isl_int_init(tmp);
93         isl_int_init(mu[0]);
94         isl_int_init(mu[1]);
95
96         GBR_init(alpha);
97         GBR_init(F_old);
98         GBR_init(F_new);
99         GBR_init(F_saved);
100         GBR_init(mu_F[0]);
101         GBR_init(mu_F[1]);
102         GBR_init(two);
103         GBR_init(one);
104
105         b_tmp = isl_vec_alloc(ctx, dim);
106         if (!b_tmp)
107                 goto error;
108
109         F = isl_alloc_array(ctx, GBR_type, n_bounded);
110         alpha_buffer[0] = isl_alloc_array(ctx, GBR_type, n_bounded);
111         alpha_buffer[1] = isl_alloc_array(ctx, GBR_type, n_bounded);
112         alpha_saved = alpha_buffer[0];
113
114         if (!F || !alpha_buffer[0] || !alpha_buffer[1])
115                 goto error;
116
117         for (i = 0; i < n_bounded; ++i) {
118                 GBR_init(F[i]);
119                 GBR_init(alpha_buffer[0][i]);
120                 GBR_init(alpha_buffer[1][i]);
121         }
122
123         GBR_set_ui(two, 2);
124         GBR_set_ui(one, 1);
125
126         lp = GBR_lp_init(tab);
127         if (!lp)
128                 goto error;
129
130         i = tab->n_zero;
131
132         GBR_lp_set_obj(lp, B->row[1+i]+1, dim);
133         ctx->stats->gbr_solved_lps++;
134         unbounded = GBR_lp_solve(lp);
135         isl_assert(ctx, !unbounded, goto error);
136         GBR_lp_get_obj_val(lp, &F[i]);
137
138         if (GBR_lt(F[i], one)) {
139                 if (!GBR_is_zero(F[i])) {
140                         empty = GBR_lp_cut(lp, B->row[1+i]+1);
141                         if (empty)
142                                 goto done;
143                         GBR_set_ui(F[i], 0);
144                 }
145                 tab->n_zero++;
146         }
147
148         do {
149                 if (i+1 == tab->n_zero) {
150                         GBR_lp_set_obj(lp, B->row[1+i+1]+1, dim);
151                         ctx->stats->gbr_solved_lps++;
152                         unbounded = GBR_lp_solve(lp);
153                         isl_assert(ctx, !unbounded, goto error);
154                         GBR_lp_get_obj_val(lp, &F_new);
155                         fixed = GBR_lp_is_fixed(lp);
156                         GBR_set_ui(alpha, 0);
157                 } else
158                 if (use_saved) {
159                         row = GBR_lp_next_row(lp);
160                         GBR_set(F_new, F_saved);
161                         fixed = fixed_saved;
162                         GBR_set(alpha, alpha_saved[i]);
163                 } else {
164                         row = GBR_lp_add_row(lp, B->row[1+i]+1, dim);
165                         GBR_lp_set_obj(lp, B->row[1+i+1]+1, dim);
166                         ctx->stats->gbr_solved_lps++;
167                         unbounded = GBR_lp_solve(lp);
168                         isl_assert(ctx, !unbounded, goto error);
169                         GBR_lp_get_obj_val(lp, &F_new);
170                         fixed = GBR_lp_is_fixed(lp);
171
172                         GBR_lp_get_alpha(lp, row, &alpha);
173
174                         if (i > 0)
175                                 save_alpha(lp, row-i, i, alpha_saved);
176
177                         if (GBR_lp_del_row(lp) < 0)
178                                 goto error;
179                 }
180                 GBR_set(F[i+1], F_new);
181
182                 GBR_floor(mu[0], alpha);
183                 GBR_ceil(mu[1], alpha);
184
185                 if (isl_int_eq(mu[0], mu[1]))
186                         isl_int_set(tmp, mu[0]);
187                 else {
188                         int j;
189
190                         for (j = 0; j <= 1; ++j) {
191                                 isl_int_set(tmp, mu[j]);
192                                 isl_seq_combine(b_tmp->el,
193                                                 ctx->one, B->row[1+i+1]+1,
194                                                 tmp, B->row[1+i]+1, dim);
195                                 GBR_lp_set_obj(lp, b_tmp->el, dim);
196                                 ctx->stats->gbr_solved_lps++;
197                                 unbounded = GBR_lp_solve(lp);
198                                 isl_assert(ctx, !unbounded, goto error);
199                                 GBR_lp_get_obj_val(lp, &mu_F[j]);
200                                 mu_fixed[j] = GBR_lp_is_fixed(lp);
201                                 if (i > 0)
202                                         save_alpha(lp, row-i, i, alpha_buffer[j]);
203                         }
204
205                         if (GBR_lt(mu_F[0], mu_F[1]))
206                                 j = 0;
207                         else
208                                 j = 1;
209
210                         isl_int_set(tmp, mu[j]);
211                         GBR_set(F_new, mu_F[j]);
212                         fixed = mu_fixed[j];
213                         alpha_saved = alpha_buffer[j];
214                 }
215                 isl_seq_combine(B->row[1+i+1]+1, ctx->one, B->row[1+i+1]+1,
216                                 tmp, B->row[1+i]+1, dim);
217
218                 if (i+1 == tab->n_zero && fixed) {
219                         if (!GBR_is_zero(F[i+1])) {
220                                 empty = GBR_lp_cut(lp, B->row[1+i+1]+1);
221                                 if (empty)
222                                         goto done;
223                                 GBR_set_ui(F[i+1], 0);
224                         }
225                         tab->n_zero++;
226                 }
227
228                 GBR_set(F_old, F[i]);
229
230                 use_saved = 0;
231                 /* mu_F[0] = 4 * F_new; mu_F[1] = 3 * F_old */
232                 GBR_set_ui(mu_F[0], 4);
233                 GBR_mul(mu_F[0], mu_F[0], F_new);
234                 GBR_set_ui(mu_F[1], 3);
235                 GBR_mul(mu_F[1], mu_F[1], F_old);
236                 if (GBR_lt(mu_F[0], mu_F[1])) {
237                         B = isl_mat_swap_rows(B, 1 + i, 1 + i + 1);
238                         if (i > tab->n_zero) {
239                                 use_saved = 1;
240                                 GBR_set(F_saved, F_new);
241                                 fixed_saved = fixed;
242                                 if (GBR_lp_del_row(lp) < 0)
243                                         goto error;
244                                 --i;
245                         } else {
246                                 GBR_set(F[tab->n_zero], F_new);
247                                 if (gbr_only_first && GBR_lt(F[tab->n_zero], two))
248                                         break;
249
250                                 if (fixed) {
251                                         if (!GBR_is_zero(F[tab->n_zero])) {
252                                                 empty = GBR_lp_cut(lp, B->row[1+tab->n_zero]+1);
253                                                 if (empty)
254                                                         goto done;
255                                                 GBR_set_ui(F[tab->n_zero], 0);
256                                         }
257                                         tab->n_zero++;
258                                 }
259                         }
260                 } else {
261                         GBR_lp_add_row(lp, B->row[1+i]+1, dim);
262                         ++i;
263                 }
264         } while (i < n_bounded - 1);
265
266         if (0) {
267 done:
268                 if (empty < 0) {
269 error:
270                         isl_mat_free(B);
271                         B = NULL;
272                 }
273         }
274
275         GBR_lp_delete(lp);
276
277         if (alpha_buffer[1])
278                 for (i = 0; i < n_bounded; ++i) {
279                         GBR_clear(F[i]);
280                         GBR_clear(alpha_buffer[0][i]);
281                         GBR_clear(alpha_buffer[1][i]);
282                 }
283         free(F);
284         free(alpha_buffer[0]);
285         free(alpha_buffer[1]);
286
287         isl_vec_free(b_tmp);
288
289         GBR_clear(alpha);
290         GBR_clear(F_old);
291         GBR_clear(F_new);
292         GBR_clear(F_saved);
293         GBR_clear(mu_F[0]);
294         GBR_clear(mu_F[1]);
295         GBR_clear(two);
296         GBR_clear(one);
297
298         isl_int_clear(tmp);
299         isl_int_clear(mu[0]);
300         isl_int_clear(mu[1]);
301
302         tab->basis = B;
303
304         return tab;
305 }
306
307 /* Compute an affine form of a reduced basis of the given basic
308  * non-parametric set, which is assumed to be bounded and not
309  * include any integer divisions.
310  * The first column and the first row correspond to the constant term.
311  *
312  * If the input contains any equalities, we first create an initial
313  * basis with the equalities first.  Otherwise, we start off with
314  * the identity matrix.
315  */
316 struct isl_mat *isl_basic_set_reduced_basis(struct isl_basic_set *bset)
317 {
318         struct isl_mat *basis;
319         struct isl_tab *tab;
320
321         if (!bset)
322                 return NULL;
323
324         if (isl_basic_set_dim(bset, isl_dim_div) != 0)
325                 isl_die(bset->ctx, isl_error_invalid,
326                         "no integer division allowed", return NULL);
327         if (isl_basic_set_dim(bset, isl_dim_param) != 0)
328                 isl_die(bset->ctx, isl_error_invalid,
329                         "no parameters allowed", return NULL);
330
331         tab = isl_tab_from_basic_set(bset);
332         if (!tab)
333                 return NULL;
334
335         if (bset->n_eq == 0)
336                 tab->basis = isl_mat_identity(bset->ctx, 1 + tab->n_var);
337         else {
338                 isl_mat *eq;
339                 unsigned nvar = isl_basic_set_total_dim(bset);
340                 eq = isl_mat_sub_alloc(bset->ctx, bset->eq, 0, bset->n_eq,
341                                         1, nvar);
342                 eq = isl_mat_left_hermite(eq, 0, NULL, &tab->basis);
343                 tab->basis = isl_mat_lin_to_aff(tab->basis);
344                 tab->n_zero = bset->n_eq;
345                 isl_mat_free(eq);
346         }
347         tab = isl_tab_compute_reduced_basis(tab);
348         if (!tab)
349                 return NULL;
350
351         basis = isl_mat_copy(tab->basis);
352
353         isl_tab_free(tab);
354
355         return basis;
356 }