isl_basic_set_sample: break early if sample found in basic_set_range
[isl.git] / isl_sample.c
blob37045f95991144bf05437cbea4294b964e9be68f
1 #include "isl_sample.h"
2 #include "isl_sample_piplib.h"
3 #include "isl_vec.h"
4 #include "isl_mat.h"
5 #include "isl_seq.h"
6 #include "isl_map_private.h"
7 #include "isl_equalities.h"
8 #include "isl_tab.h"
9 #include "isl_basis_reduction.h"
11 static struct isl_vec *empty_sample(struct isl_basic_set *bset)
13 struct isl_vec *vec;
15 vec = isl_vec_alloc(bset->ctx, 0);
16 isl_basic_set_free(bset);
17 return vec;
20 /* Construct a zero sample of the same dimension as bset.
21 * As a special case, if bset is zero-dimensional, this
22 * function creates a zero-dimensional sample point.
24 static struct isl_vec *zero_sample(struct isl_basic_set *bset)
26 unsigned dim;
27 struct isl_vec *sample;
29 dim = isl_basic_set_total_dim(bset);
30 sample = isl_vec_alloc(bset->ctx, 1 + dim);
31 if (sample) {
32 isl_int_set_si(sample->el[0], 1);
33 isl_seq_clr(sample->el + 1, dim);
35 isl_basic_set_free(bset);
36 return sample;
39 static struct isl_vec *interval_sample(struct isl_basic_set *bset)
41 int i;
42 isl_int t;
43 struct isl_vec *sample;
45 bset = isl_basic_set_simplify(bset);
46 if (!bset)
47 return NULL;
48 if (isl_basic_set_fast_is_empty(bset))
49 return empty_sample(bset);
50 if (bset->n_eq == 0 && bset->n_ineq == 0)
51 return zero_sample(bset);
53 sample = isl_vec_alloc(bset->ctx, 2);
54 isl_int_set_si(sample->block.data[0], 1);
56 if (bset->n_eq > 0) {
57 isl_assert(bset->ctx, bset->n_eq == 1, goto error);
58 isl_assert(bset->ctx, bset->n_ineq == 0, goto error);
59 if (isl_int_is_one(bset->eq[0][1]))
60 isl_int_neg(sample->el[1], bset->eq[0][0]);
61 else {
62 isl_assert(bset->ctx, isl_int_is_negone(bset->eq[0][1]),
63 goto error);
64 isl_int_set(sample->el[1], bset->eq[0][0]);
66 isl_basic_set_free(bset);
67 return sample;
70 isl_int_init(t);
71 if (isl_int_is_one(bset->ineq[0][1]))
72 isl_int_neg(sample->block.data[1], bset->ineq[0][0]);
73 else
74 isl_int_set(sample->block.data[1], bset->ineq[0][0]);
75 for (i = 1; i < bset->n_ineq; ++i) {
76 isl_seq_inner_product(sample->block.data,
77 bset->ineq[i], 2, &t);
78 if (isl_int_is_neg(t))
79 break;
81 isl_int_clear(t);
82 if (i < bset->n_ineq) {
83 isl_vec_free(sample);
84 return empty_sample(bset);
87 isl_basic_set_free(bset);
88 return sample;
89 error:
90 isl_basic_set_free(bset);
91 isl_vec_free(sample);
92 return NULL;
95 static struct isl_mat *independent_bounds(struct isl_ctx *ctx,
96 struct isl_basic_set *bset)
98 int i, j, n;
99 struct isl_mat *dirs = NULL;
100 unsigned dim;
102 if (!bset)
103 return NULL;
105 dim = isl_basic_set_n_dim(bset);
106 if (bset->n_ineq == 0)
107 return isl_mat_alloc(ctx, 0, dim);
109 dirs = isl_mat_alloc(ctx, dim, dim);
110 if (!dirs)
111 return NULL;
112 isl_seq_cpy(dirs->row[0], bset->ineq[0]+1, dirs->n_col);
113 for (j = 1, n = 1; n < dim && j < bset->n_ineq; ++j) {
114 int pos;
116 isl_seq_cpy(dirs->row[n], bset->ineq[j]+1, dirs->n_col);
118 pos = isl_seq_first_non_zero(dirs->row[n], dirs->n_col);
119 if (pos < 0)
120 continue;
121 for (i = 0; i < n; ++i) {
122 int pos_i;
123 pos_i = isl_seq_first_non_zero(dirs->row[i], dirs->n_col);
124 if (pos_i < pos)
125 continue;
126 if (pos_i > pos)
127 break;
128 isl_seq_elim(dirs->row[n], dirs->row[i], pos,
129 dirs->n_col, NULL);
130 pos = isl_seq_first_non_zero(dirs->row[n], dirs->n_col);
131 if (pos < 0)
132 break;
134 if (pos < 0)
135 continue;
136 if (i < n) {
137 int k;
138 isl_int *t = dirs->row[n];
139 for (k = n; k > i; --k)
140 dirs->row[k] = dirs->row[k-1];
141 dirs->row[i] = t;
143 ++n;
145 dirs->n_row = n;
146 return dirs;
149 /* Find a sample integer point, if any, in bset, which is known
150 * to have equalities. If bset contains no integer points, then
151 * return a zero-length vector.
152 * We simply remove the known equalities, compute a sample
153 * in the resulting bset, using the specified recurse function,
154 * and then transform the sample back to the original space.
156 static struct isl_vec *sample_eq(struct isl_basic_set *bset,
157 struct isl_vec *(*recurse)(struct isl_basic_set *))
159 struct isl_mat *T;
160 struct isl_vec *sample;
161 struct isl_ctx *ctx;
163 if (!bset)
164 return NULL;
166 ctx = bset->ctx;
167 bset = isl_basic_set_remove_equalities(bset, &T, NULL);
168 sample = recurse(bset);
169 if (!sample || sample->size == 0)
170 isl_mat_free(ctx, T);
171 else
172 sample = isl_mat_vec_product(ctx, T, sample);
173 return sample;
176 /* Given a basic set "bset" and an affine function "f"/"denom",
177 * check if bset is bounded and non-empty and if so, return the minimal
178 * and maximal value attained by the affine function in "min" and "max".
179 * The minimal value is rounded up to the nearest integer, while the
180 * maximal value is rounded down.
181 * The return value indicates whether the set was empty or unbounded.
183 * If we happen to find an integer point while looking for the minimal
184 * or maximal value, then we record that value in "bset" and return early.
186 static enum isl_lp_result basic_set_range(struct isl_basic_set *bset,
187 isl_int *f, isl_int denom, isl_int *min, isl_int *max)
189 unsigned dim;
190 struct isl_tab *tab;
191 enum isl_lp_result res;
193 if (!bset)
194 return isl_lp_error;
195 if (isl_basic_set_fast_is_empty(bset))
196 return isl_lp_empty;
198 tab = isl_tab_from_basic_set(bset);
199 res = isl_tab_min(bset->ctx, tab, f, denom, min, NULL, 0);
200 if (res != isl_lp_ok)
201 goto done;
203 if (isl_tab_sample_is_integer(bset->ctx, tab)) {
204 isl_vec_free(bset->sample);
205 bset->sample = isl_tab_get_sample_value(bset->ctx, tab);
206 if (!bset->sample)
207 goto error;
208 isl_int_set(*max, *min);
209 goto done;
212 dim = isl_basic_set_total_dim(bset);
213 isl_seq_neg(f, f, 1 + dim);
214 res = isl_tab_min(bset->ctx, tab, f, denom, max, NULL, 0);
215 isl_seq_neg(f, f, 1 + dim);
216 isl_int_neg(*max, *max);
218 if (isl_tab_sample_is_integer(bset->ctx, tab)) {
219 isl_vec_free(bset->sample);
220 bset->sample = isl_tab_get_sample_value(bset->ctx, tab);
221 if (!bset->sample)
222 goto error;
225 done:
226 isl_tab_free(bset->ctx, tab);
227 return res;
228 error:
229 isl_tab_free(bset->ctx, tab);
230 return isl_lp_error;
233 /* Perform a basis reduction on "bset" and return the inverse of
234 * the new basis, i.e., an affine mapping from the new coordinates to the old,
235 * in *T.
237 static struct isl_basic_set *basic_set_reduced(struct isl_basic_set *bset,
238 struct isl_mat **T)
240 struct isl_ctx *ctx;
241 unsigned gbr_only_first;
243 *T = NULL;
244 if (!bset)
245 return NULL;
247 ctx = bset->ctx;
249 gbr_only_first = ctx->gbr_only_first;
250 ctx->gbr_only_first = 1;
251 *T = isl_basic_set_reduced_basis(bset);
252 ctx->gbr_only_first = gbr_only_first;
254 *T = isl_mat_lin_to_aff(bset->ctx, *T);
255 *T = isl_mat_right_inverse(bset->ctx, *T);
257 bset = isl_basic_set_preimage(bset, isl_mat_copy(bset->ctx, *T));
258 if (!bset)
259 goto error;
261 return bset;
262 error:
263 isl_mat_free(ctx, *T);
264 *T = NULL;
265 return NULL;
268 static struct isl_vec *sample_bounded(struct isl_basic_set *bset);
270 /* Given a basic set "bset" whose first coordinate ranges between
271 * "min" and "max", step through all values from min to max, until
272 * the slice of bset with the first coordinate fixed to one of these
273 * values contains an integer point. If such a point is found, return it.
274 * If none of the slices contains any integer point, then bset itself
275 * doesn't contain any integer point and an empty sample is returned.
277 static struct isl_vec *sample_scan(struct isl_basic_set *bset,
278 isl_int min, isl_int max)
280 unsigned total;
281 struct isl_basic_set *slice = NULL;
282 struct isl_vec *sample = NULL;
283 isl_int tmp;
285 total = isl_basic_set_total_dim(bset);
287 isl_int_init(tmp);
288 for (isl_int_set(tmp, min); isl_int_le(tmp, max);
289 isl_int_add_ui(tmp, tmp, 1)) {
290 int k;
292 slice = isl_basic_set_copy(bset);
293 slice = isl_basic_set_cow(slice);
294 slice = isl_basic_set_extend_constraints(slice, 1, 0);
295 k = isl_basic_set_alloc_equality(slice);
296 if (k < 0)
297 goto error;
298 isl_int_set(slice->eq[k][0], tmp);
299 isl_int_set_si(slice->eq[k][1], -1);
300 isl_seq_clr(slice->eq[k] + 2, total - 1);
301 slice = isl_basic_set_simplify(slice);
302 sample = sample_bounded(slice);
303 slice = NULL;
304 if (!sample)
305 goto error;
306 if (sample->size > 0)
307 break;
308 isl_vec_free(sample);
309 sample = NULL;
311 if (!sample)
312 sample = empty_sample(bset);
313 else
314 isl_basic_set_free(bset);
315 isl_int_clear(tmp);
316 return sample;
317 error:
318 isl_basic_set_free(bset);
319 isl_basic_set_free(slice);
320 isl_int_clear(tmp);
321 return NULL;
324 /* Given a basic set that is known to be bounded, find and return
325 * an integer point in the basic set, if there is any.
327 * After handling some trivial cases, we check the range of the
328 * first coordinate. If this coordinate can only attain one integer
329 * value, we are happy. Otherwise, we perform basis reduction and
330 * determine the new range.
332 * Then we step through all possible values in the range in sample_scan.
334 * If any basis reduction was performed, the sample value found, if any,
335 * is transformed back to the original space.
337 static struct isl_vec *sample_bounded(struct isl_basic_set *bset)
339 unsigned dim;
340 struct isl_ctx *ctx;
341 struct isl_vec *sample;
342 struct isl_vec *obj = NULL;
343 struct isl_mat *T = NULL;
344 isl_int min, max;
345 enum isl_lp_result res;
347 if (!bset)
348 return NULL;
350 if (isl_basic_set_fast_is_empty(bset))
351 return empty_sample(bset);
353 ctx = bset->ctx;
354 dim = isl_basic_set_total_dim(bset);
355 if (dim == 0)
356 return zero_sample(bset);
357 if (dim == 1)
358 return interval_sample(bset);
359 if (bset->n_eq > 0)
360 return sample_eq(bset, sample_bounded);
362 isl_int_init(min);
363 isl_int_init(max);
364 obj = isl_vec_alloc(bset->ctx, 1 + dim);
365 if (!obj)
366 goto error;
367 isl_seq_clr(obj->el, 1+ dim);
368 isl_int_set_si(obj->el[1], 1);
370 res = basic_set_range(bset, obj->el, bset->ctx->one, &min, &max);
371 if (res == isl_lp_error)
372 goto error;
373 isl_assert(bset->ctx, res != isl_lp_unbounded, goto error);
374 if (bset->sample) {
375 sample = isl_vec_copy(bset->sample);
376 isl_basic_set_free(bset);
377 goto out;
379 if (res == isl_lp_empty || isl_int_lt(max, min)) {
380 sample = empty_sample(bset);
381 goto out;
384 if (isl_int_ne(min, max)) {
385 bset = basic_set_reduced(bset, &T);
386 if (!bset)
387 goto error;
389 res = basic_set_range(bset, obj->el, bset->ctx->one, &min, &max);
390 if (res == isl_lp_error)
391 goto error;
392 isl_assert(bset->ctx, res != isl_lp_unbounded, goto error);
393 if (bset->sample) {
394 sample = isl_vec_copy(bset->sample);
395 isl_basic_set_free(bset);
396 goto out;
398 if (res == isl_lp_empty || isl_int_lt(max, min)) {
399 sample = empty_sample(bset);
400 goto out;
404 sample = sample_scan(bset, min, max);
405 out:
406 if (T) {
407 if (!sample || sample->size == 0)
408 isl_mat_free(ctx, T);
409 else
410 sample = isl_mat_vec_product(ctx, T, sample);
412 isl_vec_free(obj);
413 isl_int_clear(min);
414 isl_int_clear(max);
415 return sample;
416 error:
417 isl_mat_free(ctx, T);
418 isl_basic_set_free(bset);
419 isl_vec_free(obj);
420 isl_int_clear(min);
421 isl_int_clear(max);
422 return NULL;
425 /* Given a basic set "bset" and a value "sample" for the first coordinates
426 * of bset, plug in these values and drop the corresponding coordinates.
428 * We do this by computing the preimage of the transformation
430 * [ 1 0 ]
431 * x = [ s 0 ] x'
432 * [ 0 I ]
434 * where [1 s] is the sample value and I is the identity matrix of the
435 * appropriate dimension.
437 static struct isl_basic_set *plug_in(struct isl_basic_set *bset,
438 struct isl_vec *sample)
440 int i;
441 unsigned total;
442 struct isl_mat *T;
444 if (!bset || !sample)
445 goto error;
447 total = isl_basic_set_total_dim(bset);
448 T = isl_mat_alloc(bset->ctx, 1 + total, 1 + total - (sample->size - 1));
449 if (!T)
450 goto error;
452 for (i = 0; i < sample->size; ++i) {
453 isl_int_set(T->row[i][0], sample->el[i]);
454 isl_seq_clr(T->row[i] + 1, T->n_col - 1);
456 for (i = 0; i < T->n_col - 1; ++i) {
457 isl_seq_clr(T->row[sample->size + i], T->n_col);
458 isl_int_set_si(T->row[sample->size + i][1 + i], 1);
460 isl_vec_free(sample);
462 bset = isl_basic_set_preimage(bset, T);
463 return bset;
464 error:
465 isl_basic_set_free(bset);
466 isl_vec_free(sample);
467 return NULL;
470 /* Given a basic set "bset", return any (possibly non-integer) point
471 * in the basic set.
473 static struct isl_vec *rational_sample(struct isl_basic_set *bset)
475 struct isl_tab *tab;
476 struct isl_vec *sample;
478 if (!bset)
479 return NULL;
481 tab = isl_tab_from_basic_set(bset);
482 sample = isl_tab_get_sample_value(bset->ctx, tab);
483 isl_tab_free(bset->ctx, tab);
485 isl_basic_set_free(bset);
487 return sample;
490 /* Given a rational vector, with the denominator in the first element
491 * of the vector, round up all coordinates.
493 struct isl_vec *isl_vec_ceil(struct isl_vec *vec)
495 int i;
497 vec = isl_vec_cow(vec);
498 if (!vec)
499 return NULL;
501 isl_seq_cdiv_q(vec->el + 1, vec->el + 1, vec->el[0], vec->size - 1);
503 isl_int_set_si(vec->el[0], 1);
505 return vec;
508 /* Given a linear cone "cone" and a rational point "vec",
509 * construct a polyhedron with shifted copies of the constraints in "cone",
510 * i.e., a polyhedron with "cone" as its recession cone, such that each
511 * point x in this polyhedron is such that the unit box positioned at x
512 * lies entirely inside the affine cone 'vec + cone'.
513 * Any rational point in this polyhedron may therefore be rounded up
514 * to yield an integer point that lies inside said affine cone.
516 * Denote the constraints of cone by "<a_i, x> >= 0" and the rational
517 * point "vec" by v/d.
518 * Let b_i = <a_i, v>. Then the affine cone 'vec + cone' is given
519 * by <a_i, x> - b/d >= 0.
520 * The polyhedron <a_i, x> - ceil{b/d} >= 0 is a subset of this affine cone.
521 * We prefer this polyhedron over the actual affine cone because it doesn't
522 * require a scaling of the constraints.
523 * If each of the vertices of the unit cube positioned at x lies inside
524 * this polyhedron, then the whole unit cube at x lies inside the affine cone.
525 * We therefore impose that x' = x + \sum e_i, for any selection of unit
526 * vectors lies inside the polyhedron, i.e.,
528 * <a_i, x'> - ceil{b/d} = <a_i, x> + sum a_i - ceil{b/d} >= 0
530 * The most stringent of these constraints is the one that selects
531 * all negative a_i, so the polyhedron we are looking for has constraints
533 * <a_i, x> + sum_{a_i < 0} a_i - ceil{b/d} >= 0
535 * Note that if cone were known to have only non-negative rays
536 * (which can be accomplished by a unimodular transformation),
537 * then we would only have to check the points x' = x + e_i
538 * and we only have to add the smallest negative a_i (if any)
539 * instead of the sum of all negative a_i.
541 static struct isl_basic_set *shift_cone(struct isl_basic_set *cone,
542 struct isl_vec *vec)
544 int i, j, k;
545 unsigned total;
547 struct isl_basic_set *shift = NULL;
549 if (!cone || !vec)
550 goto error;
552 isl_assert(cone->ctx, cone->n_eq == 0, goto error);
554 total = isl_basic_set_total_dim(cone);
556 shift = isl_basic_set_alloc_dim(isl_basic_set_get_dim(cone),
557 0, 0, cone->n_ineq);
559 for (i = 0; i < cone->n_ineq; ++i) {
560 k = isl_basic_set_alloc_inequality(shift);
561 if (k < 0)
562 goto error;
563 isl_seq_cpy(shift->ineq[k] + 1, cone->ineq[i] + 1, total);
564 isl_seq_inner_product(shift->ineq[k] + 1, vec->el + 1, total,
565 &shift->ineq[k][0]);
566 isl_int_cdiv_q(shift->ineq[k][0],
567 shift->ineq[k][0], vec->el[0]);
568 isl_int_neg(shift->ineq[k][0], shift->ineq[k][0]);
569 for (j = 0; j < total; ++j) {
570 if (isl_int_is_nonneg(shift->ineq[k][1 + j]))
571 continue;
572 isl_int_add(shift->ineq[k][0],
573 shift->ineq[k][0], shift->ineq[k][1 + j]);
577 isl_basic_set_free(cone);
578 isl_vec_free(vec);
580 return isl_basic_set_finalize(shift);
581 error:
582 isl_basic_set_free(shift);
583 isl_basic_set_free(cone);
584 isl_vec_free(vec);
585 return NULL;
588 /* Given a rational point vec in a (transformed) basic set,
589 * such that cone is the recession cone of the original basic set,
590 * "round up" the rational point to an integer point.
592 * We first check if the rational point just happens to be integer.
593 * If not, we transform the cone in the same way as the basic set,
594 * pick a point x in this cone shifted to the rational point such that
595 * the whole unit cube at x is also inside this affine cone.
596 * Then we simply round up the coordinates of x and return the
597 * resulting integer point.
599 static struct isl_vec *round_up_in_cone(struct isl_vec *vec,
600 struct isl_basic_set *cone, struct isl_mat *U)
602 unsigned total;
604 if (!vec || !cone || !U)
605 goto error;
607 isl_assert(vec->ctx, vec->size != 0, goto error);
608 if (isl_int_is_one(vec->el[0])) {
609 isl_mat_free(vec->ctx, U);
610 isl_basic_set_free(cone);
611 return vec;
614 total = isl_basic_set_total_dim(cone);
615 cone = isl_basic_set_preimage(cone, U);
616 cone = isl_basic_set_remove_dims(cone, 0, total - (vec->size - 1));
618 cone = shift_cone(cone, vec);
620 vec = rational_sample(cone);
621 vec = isl_vec_ceil(vec);
622 return vec;
623 error:
624 isl_mat_free(vec ? vec->ctx : cone ? cone->ctx : NULL, U);
625 isl_vec_free(vec);
626 isl_basic_set_free(cone);
627 return NULL;
630 /* Concatenate two integer vectors, i.e., two vectors with denominator
631 * (stored in element 0) equal to 1.
633 static struct isl_vec *vec_concat(struct isl_vec *vec1, struct isl_vec *vec2)
635 struct isl_vec *vec;
637 if (!vec1 || !vec2)
638 goto error;
639 isl_assert(vec1->ctx, vec1->size > 0, goto error);
640 isl_assert(vec2->ctx, vec2->size > 0, goto error);
641 isl_assert(vec1->ctx, isl_int_is_one(vec1->el[0]), goto error);
642 isl_assert(vec2->ctx, isl_int_is_one(vec2->el[0]), goto error);
644 vec = isl_vec_alloc(vec1->ctx, vec1->size + vec2->size - 1);
645 if (!vec)
646 goto error;
648 isl_seq_cpy(vec->el, vec1->el, vec1->size);
649 isl_seq_cpy(vec->el + vec1->size, vec2->el + 1, vec2->size - 1);
651 isl_vec_free(vec1);
652 isl_vec_free(vec2);
654 return vec;
655 error:
656 isl_vec_free(vec1);
657 isl_vec_free(vec2);
658 return NULL;
661 /* Drop all constraints in bset that involve any of the dimensions
662 * first to first+n-1.
664 static struct isl_basic_set *drop_constraints_involving
665 (struct isl_basic_set *bset, unsigned first, unsigned n)
667 int i;
669 if (!bset)
670 return NULL;
672 bset = isl_basic_set_cow(bset);
674 for (i = bset->n_ineq - 1; i >= 0; --i) {
675 if (isl_seq_first_non_zero(bset->ineq[i] + 1 + first, n) == -1)
676 continue;
677 isl_basic_set_drop_inequality(bset, i);
680 return bset;
683 /* Give a basic set "bset" with recession cone "cone", compute and
684 * return an integer point in bset, if any.
686 * If the recession cone is full-dimensional, then we know that
687 * bset contains an infinite number of integer points and it is
688 * fairly easy to pick one of them.
689 * If the recession cone is not full-dimensional, then we first
690 * transform bset such that the bounded directions appear as
691 * the first dimensions of the transformed basic set.
692 * We do this by using a unimodular transformation that transforms
693 * the equalities in the recession cone to equalities on the first
694 * dimensions.
696 * The transformed set is then projected onto its bounded dimensions.
697 * Note that to compute this projection, we can simply drop all constraints
698 * involving any of the unbounded dimensions since these constraints
699 * cannot be combined to produce a constraint on the bounded dimensions.
700 * To see this, assume that there is such a combination of constraints
701 * that produces a constraint on the bounded dimensions. This means
702 * that some combination of the unbounded dimensions has both an upper
703 * bound and a lower bound in terms of the bounded dimensions, but then
704 * this combination would be a bounded direction too and would have been
705 * transformed into a bounded dimensions.
707 * We then compute a sample value in the bounded dimensions.
708 * If no such value can be found, then the original set did not contain
709 * any integer points and we are done.
710 * Otherwise, we plug in the value we found in the bounded dimensions,
711 * project out these bounded dimensions and end up with a set with
712 * a full-dimensional recession cone.
713 * A sample point in this set is computed by "rounding up" any
714 * rational point in the set.
716 * The sample points in the bounded and unbounded dimensions are
717 * then combined into a single sample point and transformed back
718 * to the original space.
720 static struct isl_vec *sample_with_cone(struct isl_basic_set *bset,
721 struct isl_basic_set *cone)
723 unsigned total;
724 unsigned cone_dim;
725 struct isl_mat *M, *U;
726 struct isl_vec *sample;
727 struct isl_vec *cone_sample;
728 struct isl_ctx *ctx;
729 struct isl_basic_set *bounded;
731 if (!bset || !cone)
732 goto error;
734 ctx = bset->ctx;
735 total = isl_basic_set_total_dim(cone);
736 cone_dim = total - cone->n_eq;
738 M = isl_mat_sub_alloc(bset->ctx, cone->eq, 0, cone->n_eq, 1, total);
739 M = isl_mat_left_hermite(bset->ctx, M, 0, &U, NULL);
740 if (!M)
741 goto error;
742 isl_mat_free(bset->ctx, M);
744 U = isl_mat_lin_to_aff(bset->ctx, U);
745 bset = isl_basic_set_preimage(bset, isl_mat_copy(bset->ctx, U));
747 bounded = isl_basic_set_copy(bset);
748 bounded = drop_constraints_involving(bounded, total - cone_dim, cone_dim);
749 bounded = isl_basic_set_drop_dims(bounded, total - cone_dim, cone_dim);
750 sample = sample_bounded(bounded);
751 if (!sample || sample->size == 0) {
752 isl_basic_set_free(bset);
753 isl_basic_set_free(cone);
754 isl_mat_free(ctx, U);
755 return sample;
757 bset = plug_in(bset, isl_vec_copy(sample));
758 cone_sample = rational_sample(bset);
759 cone_sample = round_up_in_cone(cone_sample, cone, isl_mat_copy(ctx, U));
760 sample = vec_concat(sample, cone_sample);
761 sample = isl_mat_vec_product(ctx, U, sample);
762 return sample;
763 error:
764 isl_basic_set_free(cone);
765 isl_basic_set_free(bset);
766 return NULL;
769 /* Compute and return a sample point in bset using generalized basis
770 * reduction. We first check if the input set has a non-trivial
771 * recession cone. If so, we perform some extra preprocessing in
772 * sample_with_cone. Otherwise, we directly perform generalized basis
773 * reduction.
775 static struct isl_vec *gbr_sample_no_lineality(struct isl_basic_set *bset)
777 unsigned dim;
778 struct isl_basic_set *cone;
780 dim = isl_basic_set_total_dim(bset);
782 cone = isl_basic_set_recession_cone(isl_basic_set_copy(bset));
784 if (cone->n_eq < dim)
785 return sample_with_cone(bset, cone);
787 isl_basic_set_free(cone);
788 return sample_bounded(bset);
791 static struct isl_vec *sample_no_lineality(struct isl_basic_set *bset)
793 unsigned dim;
795 if (isl_basic_set_fast_is_empty(bset))
796 return empty_sample(bset);
797 if (bset->n_eq > 0)
798 return sample_eq(bset, sample_no_lineality);
799 dim = isl_basic_set_total_dim(bset);
800 if (dim == 0)
801 return zero_sample(bset);
802 if (dim == 1)
803 return interval_sample(bset);
805 switch (bset->ctx->ilp_solver) {
806 case ISL_ILP_PIP:
807 return isl_pip_basic_set_sample(bset);
808 case ISL_ILP_GBR:
809 return gbr_sample_no_lineality(bset);
811 isl_assert(bset->ctx, 0, );
812 isl_basic_set_free(bset);
813 return NULL;
816 /* Compute an integer point in "bset" with a lineality space that
817 * is orthogonal to the constraints in "bounds".
819 * We first perform a unimodular transformation on bset that
820 * make the constraints in bounds (and therefore all constraints in bset)
821 * only involve the first dimensions. The remaining dimensions
822 * then do not appear in any constraints and we can select any value
823 * for them, say zero. We therefore project out this final dimensions
824 * and plug in the value zero later. This is accomplished by simply
825 * dropping the final columns of the unimodular transformation.
827 static struct isl_vec *sample_lineality(struct isl_basic_set *bset,
828 struct isl_mat *bounds)
830 struct isl_mat *U = NULL;
831 unsigned old_dim, new_dim;
832 struct isl_vec *sample;
833 struct isl_ctx *ctx;
835 if (!bset || !bounds)
836 goto error;
838 ctx = bset->ctx;
839 old_dim = isl_basic_set_n_dim(bset);
840 new_dim = bounds->n_row;
841 bounds = isl_mat_left_hermite(ctx, bounds, 0, &U, NULL);
842 if (!bounds)
843 goto error;
844 U = isl_mat_lin_to_aff(ctx, U);
845 U = isl_mat_drop_cols(ctx, U, 1 + new_dim, old_dim - new_dim);
846 bset = isl_basic_set_preimage(bset, isl_mat_copy(ctx, U));
847 if (!bset)
848 goto error;
849 isl_mat_free(ctx, bounds);
851 sample = sample_no_lineality(bset);
852 if (sample && sample->size != 0)
853 sample = isl_mat_vec_product(ctx, U, sample);
854 else
855 isl_mat_free(ctx, U);
856 return sample;
857 error:
858 isl_mat_free(ctx, bounds);
859 isl_mat_free(ctx, U);
860 isl_basic_set_free(bset);
861 return NULL;
864 struct isl_vec *isl_basic_set_sample(struct isl_basic_set *bset)
866 struct isl_ctx *ctx;
867 struct isl_mat *bounds;
868 unsigned dim;
869 if (!bset)
870 return NULL;
872 ctx = bset->ctx;
873 if (isl_basic_set_fast_is_empty(bset))
874 return empty_sample(bset);
876 dim = isl_basic_set_n_dim(bset);
877 isl_assert(ctx, isl_basic_set_n_param(bset) == 0, goto error);
878 isl_assert(ctx, bset->n_div == 0, goto error);
880 if (bset->sample && bset->sample->size == 1 + dim) {
881 int contains = isl_basic_set_contains(bset, bset->sample);
882 if (contains < 0)
883 goto error;
884 if (contains) {
885 struct isl_vec *sample = isl_vec_copy(bset->sample);
886 isl_basic_set_free(bset);
887 return sample;
890 isl_vec_free(bset->sample);
891 bset->sample = NULL;
893 if (bset->n_eq > 0)
894 return sample_eq(bset, isl_basic_set_sample);
895 if (dim == 0)
896 return zero_sample(bset);
897 if (dim == 1)
898 return interval_sample(bset);
899 bounds = independent_bounds(ctx, bset);
900 if (!bounds)
901 goto error;
903 if (bounds->n_row == 0) {
904 isl_mat_free(ctx, bounds);
905 return zero_sample(bset);
907 if (bounds->n_row < dim)
908 return sample_lineality(bset, bounds);
910 isl_mat_free(ctx, bounds);
911 return sample_no_lineality(bset);
912 error:
913 isl_basic_set_free(bset);
914 return NULL;