scheduling: introduce band forest interface
[isl.git] / isl_scan.c
blob64654be4dbf2ae0a702030b8fae69fb4e2db6008
1 /*
2 * Copyright 2008-2009 Katholieke Universiteit Leuven
4 * Use of this software is governed by the GNU LGPLv2.1 license
6 * Written by Sven Verdoolaege, K.U.Leuven, Departement
7 * Computerwetenschappen, Celestijnenlaan 200A, B-3001 Leuven, Belgium
8 */
10 #include <isl_ctx_private.h>
11 #include <isl_map_private.h>
12 #include "isl_basis_reduction.h"
13 #include "isl_scan.h"
14 #include <isl/seq.h>
15 #include "isl_tab.h"
17 struct isl_counter {
18 struct isl_scan_callback callback;
19 isl_int count;
20 isl_int max;
23 static int increment_counter(struct isl_scan_callback *cb,
24 __isl_take isl_vec *sample)
26 struct isl_counter *cnt = (struct isl_counter *)cb;
28 isl_int_add_ui(cnt->count, cnt->count, 1);
30 isl_vec_free(sample);
32 if (isl_int_is_zero(cnt->max) || isl_int_lt(cnt->count, cnt->max))
33 return 0;
34 return -1;
37 static int increment_range(struct isl_scan_callback *cb, isl_int min, isl_int max)
39 struct isl_counter *cnt = (struct isl_counter *)cb;
41 isl_int_add(cnt->count, cnt->count, max);
42 isl_int_sub(cnt->count, cnt->count, min);
43 isl_int_add_ui(cnt->count, cnt->count, 1);
45 if (isl_int_is_zero(cnt->max) || isl_int_lt(cnt->count, cnt->max))
46 return 0;
47 isl_int_set(cnt->count, cnt->max);
48 return -1;
51 /* Call callback->add with the current sample value of the tableau "tab".
53 static int add_solution(struct isl_tab *tab, struct isl_scan_callback *callback)
55 struct isl_vec *sample;
57 if (!tab)
58 return -1;
59 sample = isl_tab_get_sample_value(tab);
60 if (!sample)
61 return -1;
63 return callback->add(callback, sample);
66 static int scan_0D(struct isl_basic_set *bset,
67 struct isl_scan_callback *callback)
69 struct isl_vec *sample;
71 sample = isl_vec_alloc(bset->ctx, 1);
72 isl_basic_set_free(bset);
74 if (!sample)
75 return -1;
77 isl_int_set_si(sample->el[0], 1);
79 return callback->add(callback, sample);
82 /* Look for all integer points in "bset", which is assumed to be bounded,
83 * and call callback->add on each of them.
85 * We first compute a reduced basis for the set and then scan
86 * the set in the directions of this basis.
87 * We basically perform a depth first search, where in each level i
88 * we compute the range in the i-th basis vector direction, given
89 * fixed values in the directions of the previous basis vector.
90 * We then add an equality to the tableau fixing the value in the
91 * direction of the current basis vector to each value in the range
92 * in turn and then continue to the next level.
94 * The search is implemented iteratively. "level" identifies the current
95 * basis vector. "init" is true if we want the first value at the current
96 * level and false if we want the next value.
97 * Solutions are added in the leaves of the search tree, i.e., after
98 * we have fixed a value in each direction of the basis.
100 int isl_basic_set_scan(struct isl_basic_set *bset,
101 struct isl_scan_callback *callback)
103 unsigned dim;
104 struct isl_mat *B = NULL;
105 struct isl_tab *tab = NULL;
106 struct isl_vec *min;
107 struct isl_vec *max;
108 struct isl_tab_undo **snap;
109 int level;
110 int init;
111 enum isl_lp_result res;
113 if (!bset)
114 return -1;
116 dim = isl_basic_set_total_dim(bset);
117 if (dim == 0)
118 return scan_0D(bset, callback);
120 min = isl_vec_alloc(bset->ctx, dim);
121 max = isl_vec_alloc(bset->ctx, dim);
122 snap = isl_alloc_array(bset->ctx, struct isl_tab_undo *, dim);
124 if (!min || !max || !snap)
125 goto error;
127 tab = isl_tab_from_basic_set(bset);
128 if (!tab)
129 goto error;
130 if (isl_tab_extend_cons(tab, dim + 1) < 0)
131 goto error;
133 tab->basis = isl_mat_identity(bset->ctx, 1 + dim);
134 if (1)
135 tab = isl_tab_compute_reduced_basis(tab);
136 if (!tab)
137 goto error;
138 B = isl_mat_copy(tab->basis);
139 if (!B)
140 goto error;
142 level = 0;
143 init = 1;
145 while (level >= 0) {
146 int empty = 0;
147 if (init) {
148 res = isl_tab_min(tab, B->row[1 + level],
149 bset->ctx->one, &min->el[level], NULL, 0);
150 if (res == isl_lp_empty)
151 empty = 1;
152 if (res == isl_lp_error || res == isl_lp_unbounded)
153 goto error;
154 isl_seq_neg(B->row[1 + level] + 1,
155 B->row[1 + level] + 1, dim);
156 res = isl_tab_min(tab, B->row[1 + level],
157 bset->ctx->one, &max->el[level], NULL, 0);
158 isl_seq_neg(B->row[1 + level] + 1,
159 B->row[1 + level] + 1, dim);
160 isl_int_neg(max->el[level], max->el[level]);
161 if (res == isl_lp_empty)
162 empty = 1;
163 if (res == isl_lp_error || res == isl_lp_unbounded)
164 goto error;
165 snap[level] = isl_tab_snap(tab);
166 } else
167 isl_int_add_ui(min->el[level], min->el[level], 1);
169 if (empty || isl_int_gt(min->el[level], max->el[level])) {
170 level--;
171 init = 0;
172 if (level >= 0)
173 if (isl_tab_rollback(tab, snap[level]) < 0)
174 goto error;
175 continue;
177 if (level == dim - 1 && callback->add == increment_counter) {
178 if (increment_range(callback,
179 min->el[level], max->el[level]))
180 goto error;
181 level--;
182 init = 0;
183 if (level >= 0)
184 if (isl_tab_rollback(tab, snap[level]) < 0)
185 goto error;
186 continue;
188 isl_int_neg(B->row[1 + level][0], min->el[level]);
189 if (isl_tab_add_valid_eq(tab, B->row[1 + level]) < 0)
190 goto error;
191 isl_int_set_si(B->row[1 + level][0], 0);
192 if (level < dim - 1) {
193 ++level;
194 init = 1;
195 continue;
197 if (add_solution(tab, callback) < 0)
198 goto error;
199 init = 0;
200 if (isl_tab_rollback(tab, snap[level]) < 0)
201 goto error;
204 isl_tab_free(tab);
205 free(snap);
206 isl_vec_free(min);
207 isl_vec_free(max);
208 isl_basic_set_free(bset);
209 isl_mat_free(B);
210 return 0;
211 error:
212 isl_tab_free(tab);
213 free(snap);
214 isl_vec_free(min);
215 isl_vec_free(max);
216 isl_basic_set_free(bset);
217 isl_mat_free(B);
218 return -1;
221 int isl_set_scan(__isl_take isl_set *set, struct isl_scan_callback *callback)
223 int i;
225 if (!set || !callback)
226 goto error;
228 set = isl_set_cow(set);
229 set = isl_set_make_disjoint(set);
230 set = isl_set_compute_divs(set);
231 if (!set)
232 goto error;
234 for (i = 0; i < set->n; ++i)
235 if (isl_basic_set_scan(isl_basic_set_copy(set->p[i]),
236 callback) < 0)
237 goto error;
239 isl_set_free(set);
240 return 0;
241 error:
242 isl_set_free(set);
243 return -1;
246 int isl_basic_set_count_upto(__isl_keep isl_basic_set *bset,
247 isl_int max, isl_int *count)
249 struct isl_counter cnt = { { &increment_counter } };
251 if (!bset)
252 return -1;
254 isl_int_init(cnt.count);
255 isl_int_init(cnt.max);
257 isl_int_set_si(cnt.count, 0);
258 isl_int_set(cnt.max, max);
259 if (isl_basic_set_scan(isl_basic_set_copy(bset), &cnt.callback) < 0 &&
260 isl_int_lt(cnt.count, cnt.max))
261 goto error;
263 isl_int_set(*count, cnt.count);
264 isl_int_clear(cnt.max);
265 isl_int_clear(cnt.count);
267 return 0;
268 error:
269 isl_int_clear(cnt.count);
270 return -1;
273 int isl_set_count_upto(__isl_keep isl_set *set, isl_int max, isl_int *count)
275 struct isl_counter cnt = { { &increment_counter } };
277 if (!set)
278 return -1;
280 isl_int_init(cnt.count);
281 isl_int_init(cnt.max);
283 isl_int_set_si(cnt.count, 0);
284 isl_int_set(cnt.max, max);
285 if (isl_set_scan(isl_set_copy(set), &cnt.callback) < 0 &&
286 isl_int_lt(cnt.count, cnt.max))
287 goto error;
289 isl_int_set(*count, cnt.count);
290 isl_int_clear(cnt.max);
291 isl_int_clear(cnt.count);
293 return 0;
294 error:
295 isl_int_clear(cnt.count);
296 return -1;
299 int isl_set_count(__isl_keep isl_set *set, isl_int *count)
301 if (!set)
302 return -1;
303 return isl_set_count_upto(set, set->ctx->zero, count);