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