return NULL;
}
-static struct isl_mat *independent_bounds(struct isl_ctx *ctx,
- struct isl_basic_set *bset)
+static struct isl_mat *independent_bounds(struct isl_basic_set *bset)
{
int i, j, n;
struct isl_mat *dirs = NULL;
+ struct isl_mat *bounds = NULL;
unsigned dim;
if (!bset)
return NULL;
dim = isl_basic_set_n_dim(bset);
+ bounds = isl_mat_alloc(bset->ctx, 1+dim, 1+dim);
+ if (!bounds)
+ return NULL;
+
+ isl_int_set_si(bounds->row[0][0], 1);
+ isl_seq_clr(bounds->row[0]+1, dim);
+ bounds->n_row = 1;
+
if (bset->n_ineq == 0)
- return isl_mat_alloc(ctx, 0, dim);
+ return bounds;
- dirs = isl_mat_alloc(ctx, dim, dim);
- if (!dirs)
+ dirs = isl_mat_alloc(bset->ctx, dim, dim);
+ if (!dirs) {
+ isl_mat_free(bounds);
return NULL;
+ }
isl_seq_cpy(dirs->row[0], bset->ineq[0]+1, dirs->n_col);
+ isl_seq_cpy(bounds->row[1], bset->ineq[0], bounds->n_col);
for (j = 1, n = 1; n < dim && j < bset->n_ineq; ++j) {
int pos;
dirs->row[i] = t;
}
++n;
+ isl_seq_cpy(bounds->row[n], bset->ineq[j], bounds->n_col);
+ }
+ isl_mat_free(dirs);
+ bounds->n_row = 1+n;
+ return bounds;
+}
+
+static void swap_inequality(struct isl_basic_set *bset, int a, int b)
+{
+ isl_int *t = bset->ineq[a];
+ bset->ineq[a] = bset->ineq[b];
+ bset->ineq[b] = t;
+}
+
+/* Skew into positive orthant and project out lineality space.
+ *
+ * We perform a unimodular transformation that turns a selected
+ * maximal set of linearly independent bounds into constraints
+ * on the first dimensions that impose that these first dimensions
+ * are non-negative. In particular, the constraint matrix is lower
+ * triangular with positive entries on the diagonal and negative
+ * entries below.
+ * If "bset" has a lineality space then these constraints (and therefore
+ * all constraints in bset) only involve the first dimensions.
+ * The remaining dimensions then do not appear in any constraints and
+ * we can select any value for them, say zero. We therefore project
+ * out this final dimensions and plug in the value zero later. This
+ * is accomplished by simply dropping the final columns of
+ * the unimodular transformation.
+ */
+static struct isl_basic_set *isl_basic_set_skew_to_positive_orthant(
+ struct isl_basic_set *bset, struct isl_mat **T)
+{
+ struct isl_mat *U = NULL;
+ struct isl_mat *bounds = NULL;
+ int i, j;
+ unsigned old_dim, new_dim;
+
+ *T = NULL;
+ if (!bset)
+ return NULL;
+
+ isl_assert(bset->ctx, isl_basic_set_n_param(bset) == 0, goto error);
+ isl_assert(bset->ctx, bset->n_div == 0, goto error);
+ isl_assert(bset->ctx, bset->n_eq == 0, goto error);
+
+ old_dim = isl_basic_set_n_dim(bset);
+ /* Try to move (multiples of) unit rows up. */
+ for (i = 0, j = 0; i < bset->n_ineq; ++i) {
+ int pos = isl_seq_first_non_zero(bset->ineq[i]+1, old_dim);
+ if (pos < 0)
+ continue;
+ if (isl_seq_first_non_zero(bset->ineq[i]+1+pos+1,
+ old_dim-pos-1) >= 0)
+ continue;
+ if (i != j)
+ swap_inequality(bset, i, j);
+ ++j;
}
- dirs->n_row = n;
- return dirs;
+ bounds = independent_bounds(bset);
+ if (!bounds)
+ goto error;
+ new_dim = bounds->n_row - 1;
+ bounds = isl_mat_left_hermite(bounds, 1, &U, NULL);
+ if (!bounds)
+ goto error;
+ U = isl_mat_drop_cols(U, 1 + new_dim, old_dim - new_dim);
+ bset = isl_basic_set_preimage(bset, isl_mat_copy(U));
+ if (!bset)
+ goto error;
+ *T = U;
+ isl_mat_free(bounds);
+ return bset;
+error:
+ isl_mat_free(bounds);
+ isl_mat_free(U);
+ isl_basic_set_free(bset);
+ return NULL;
}
/* Find a sample integer point, if any, in bset, which is known
{
struct isl_mat *T;
struct isl_vec *sample;
- struct isl_ctx *ctx;
if (!bset)
return NULL;
- ctx = bset->ctx;
bset = isl_basic_set_remove_equalities(bset, &T, NULL);
sample = recurse(bset);
if (!sample || sample->size == 0)
- isl_mat_free(ctx, T);
+ isl_mat_free(T);
else
- sample = isl_mat_vec_product(ctx, T, sample);
+ sample = isl_mat_vec_product(T, sample);
return sample;
}
-/* Given a basic set "bset" and an affine function "f"/"denom",
- * check if bset is bounded and non-empty and if so, return the minimal
- * and maximal value attained by the affine function in "min" and "max".
- * The minimal value is rounded up to the nearest integer, while the
- * maximal value is rounded down.
- * The return value indicates whether the set was empty or unbounded.
+/* Return a matrix containing the equalities of the tableau
+ * in constraint form. The tableau is assumed to have
+ * an associated bset that has been kept up-to-date.
*/
-static enum isl_lp_result basic_set_range(struct isl_basic_set *bset,
- isl_int *f, isl_int denom, isl_int *min, isl_int *max)
+static struct isl_mat *tab_equalities(struct isl_tab *tab)
{
- unsigned dim;
- struct isl_tab *tab;
- enum isl_lp_result res;
+ int i, j;
+ int n_eq;
+ struct isl_mat *eq;
+ struct isl_basic_set *bset;
- if (!bset)
- return isl_lp_error;
- if (isl_basic_set_fast_is_empty(bset))
- return isl_lp_empty;
+ if (!tab)
+ return NULL;
- tab = isl_tab_from_basic_set(bset);
- res = isl_tab_min(bset->ctx, tab, f, denom, min, NULL, 0);
- if (res != isl_lp_ok) {
- isl_tab_free(bset->ctx, tab);
- return res;
- }
- dim = isl_basic_set_total_dim(bset);
- isl_seq_neg(f, f, 1 + dim);
- res = isl_tab_min(bset->ctx, tab, f, denom, max, NULL, 0);
- isl_seq_neg(f, f, 1 + dim);
- isl_int_neg(*max, *max);
+ isl_assert(tab->mat->ctx, tab->bset, return NULL);
+ bset = tab->bset;
+
+ n_eq = tab->n_var - tab->n_col + tab->n_dead;
+ if (tab->empty || n_eq == 0)
+ return isl_mat_alloc(tab->mat->ctx, 0, tab->n_var);
+ if (n_eq == tab->n_var)
+ return isl_mat_identity(tab->mat->ctx, tab->n_var);
- isl_tab_free(bset->ctx, tab);
- return res;
+ eq = isl_mat_alloc(tab->mat->ctx, n_eq, tab->n_var);
+ if (!eq)
+ return NULL;
+ for (i = 0, j = 0; i < tab->n_con; ++i) {
+ if (tab->con[i].is_row)
+ continue;
+ if (tab->con[i].index >= 0 && tab->con[i].index >= tab->n_dead)
+ continue;
+ if (i < bset->n_eq)
+ isl_seq_cpy(eq->row[j], bset->eq[i] + 1, tab->n_var);
+ else
+ isl_seq_cpy(eq->row[j],
+ bset->ineq[i - bset->n_eq] + 1, tab->n_var);
+ ++j;
+ }
+ isl_assert(bset->ctx, j == n_eq, goto error);
+ return eq;
+error:
+ isl_mat_free(eq);
+ return NULL;
}
-/* Perform a basis reduction on "bset" and return the inverse of
- * the new basis, i.e., an affine mapping from the new coordinates to the old,
- * in *T.
+/* Compute and return an initial basis for the bounded tableau "tab".
+ *
+ * If the tableau is either full-dimensional or zero-dimensional,
+ * the we simply return an identity matrix.
+ * Otherwise, we construct a basis whose first directions correspond
+ * to equalities.
*/
-static struct isl_basic_set *basic_set_reduced(struct isl_basic_set *bset,
- struct isl_mat **T)
+static struct isl_mat *initial_basis(struct isl_tab *tab)
{
- struct isl_ctx *ctx;
- unsigned gbr_only_first;
+ int n_eq;
+ struct isl_mat *eq;
+ struct isl_mat *Q;
- *T = NULL;
- if (!bset)
- return NULL;
+ n_eq = tab->n_var - tab->n_col + tab->n_dead;
+ if (tab->empty || n_eq == 0 || n_eq == tab->n_var)
+ return isl_mat_identity(tab->mat->ctx, 1 + tab->n_var);
- ctx = bset->ctx;
+ eq = tab_equalities(tab);
+ eq = isl_mat_left_hermite(eq, 0, NULL, &Q);
+ if (!eq)
+ return NULL;
+ isl_mat_free(eq);
- gbr_only_first = ctx->gbr_only_first;
- ctx->gbr_only_first = 1;
- *T = isl_basic_set_reduced_basis(bset);
- ctx->gbr_only_first = gbr_only_first;
+ Q = isl_mat_lin_to_aff(Q);
+ return Q;
+}
- *T = isl_mat_lin_to_aff(bset->ctx, *T);
- *T = isl_mat_right_inverse(bset->ctx, *T);
+/* Given a tableau representing a set, find and return
+ * an integer point in the set, if there is any.
+ *
+ * We perform a depth first search
+ * for an integer point, by scanning all possible values in the range
+ * attained by a basis vector, where an initial basis may have been set
+ * by the calling function. Otherwise an initial basis that exploits
+ * the equalities in the tableau is created.
+ * tab->n_zero is currently ignored and is clobbered by this function.
+ *
+ * The tableau is allowed to have unbounded direction, but then
+ * the calling function needs to set an initial basis, with the
+ * unbounded directions last and with tab->n_unbounded set
+ * to the number of unbounded directions.
+ * Furthermore, the calling functions needs to add shifted copies
+ * of all constraints involving unbounded directions to ensure
+ * that any feasible rational value in these directions can be rounded
+ * up to yield a feasible integer value.
+ * In particular, let B define the given basis x' = B x
+ * and let T be the inverse of B, i.e., X = T x'.
+ * Let a x + c >= 0 be a constraint of the set represented by the tableau,
+ * or a T x' + c >= 0 in terms of the given basis. Assume that
+ * the bounded directions have an integer value, then we can safely
+ * round up the values for the unbounded directions if we make sure
+ * that x' not only satisfies the original constraint, but also
+ * the constraint "a T x' + c + s >= 0" with s the sum of all
+ * negative values in the last n_unbounded entries of "a T".
+ * The calling function therefore needs to add the constraint
+ * a x + c + s >= 0. The current function then scans the first
+ * directions for an integer value and once those have been found,
+ * it can compute "T ceil(B x)" to yield an integer point in the set.
+ * Note that during the search, the first rows of B may be changed
+ * by a basis reduction, but the last n_unbounded rows of B remain
+ * unaltered and are also not mixed into the first rows.
+ *
+ * The search is implemented iteratively. "level" identifies the current
+ * basis vector. "init" is true if we want the first value at the current
+ * level and false if we want the next value.
+ *
+ * The initial basis is the identity matrix. If the range in some direction
+ * contains more than one integer value, we perform basis reduction based
+ * on the value of ctx->gbr
+ * - ISL_GBR_NEVER: never perform basis reduction
+ * - ISL_GBR_ONCE: only perform basis reduction the first
+ * time such a range is encountered
+ * - ISL_GBR_ALWAYS: always perform basis reduction when
+ * such a range is encountered
+ *
+ * When ctx->gbr is set to ISL_GBR_ALWAYS, then we allow the basis
+ * reduction computation to return early. That is, as soon as it
+ * finds a reasonable first direction.
+ */
+struct isl_vec *isl_tab_sample(struct isl_tab *tab)
+{
+ unsigned dim;
+ unsigned gbr;
+ struct isl_ctx *ctx;
+ struct isl_vec *sample;
+ struct isl_vec *min;
+ struct isl_vec *max;
+ enum isl_lp_result res;
+ int level;
+ int init;
+ int reduced;
+ struct isl_tab_undo **snap;
- bset = isl_basic_set_preimage(bset, isl_mat_copy(bset->ctx, *T));
- if (!bset)
- goto error;
+ if (!tab)
+ return NULL;
+ if (tab->empty)
+ return isl_vec_alloc(tab->mat->ctx, 0);
- return bset;
-error:
- isl_mat_free(ctx, *T);
- *T = NULL;
- return NULL;
-}
+ if (!tab->basis)
+ tab->basis = initial_basis(tab);
+ if (!tab->basis)
+ return NULL;
+ isl_assert(tab->mat->ctx, tab->basis->n_row == tab->n_var + 1,
+ return NULL);
+ isl_assert(tab->mat->ctx, tab->basis->n_col == tab->n_var + 1,
+ return NULL);
+
+ ctx = tab->mat->ctx;
+ dim = tab->n_var;
+ gbr = ctx->gbr;
+
+ if (tab->n_unbounded == tab->n_var) {
+ sample = isl_tab_get_sample_value(tab);
+ sample = isl_mat_vec_product(isl_mat_copy(tab->basis), sample);
+ sample = isl_vec_ceil(sample);
+ sample = isl_mat_vec_inverse_product(isl_mat_copy(tab->basis),
+ sample);
+ return sample;
+ }
-static struct isl_vec *sample_bounded(struct isl_basic_set *bset);
+ if (isl_tab_extend_cons(tab, dim + 1) < 0)
+ return NULL;
-/* Given a basic set "bset" whose first coordinate ranges between
- * "min" and "max", step through all values from min to max, until
- * the slice of bset with the first coordinate fixed to one of these
- * values contains an integer point. If such a point is found, return it.
- * If none of the slices contains any integer point, then bset itself
- * doesn't contain any integer point and an empty sample is returned.
- */
-static struct isl_vec *sample_scan(struct isl_basic_set *bset,
- isl_int min, isl_int max)
-{
- unsigned total;
- struct isl_basic_set *slice = NULL;
- struct isl_vec *sample = NULL;
- isl_int tmp;
+ min = isl_vec_alloc(ctx, dim);
+ max = isl_vec_alloc(ctx, dim);
+ snap = isl_alloc_array(ctx, struct isl_tab_undo *, dim);
- total = isl_basic_set_total_dim(bset);
+ if (!min || !max || !snap)
+ goto error;
- isl_int_init(tmp);
- for (isl_int_set(tmp, min); isl_int_le(tmp, max);
- isl_int_add_ui(tmp, tmp, 1)) {
- int k;
+ level = 0;
+ init = 1;
+ reduced = 0;
+
+ while (level >= 0) {
+ int empty = 0;
+ if (init) {
+ res = isl_tab_min(tab, tab->basis->row[1 + level],
+ ctx->one, &min->el[level], NULL, 0);
+ if (res == isl_lp_empty)
+ empty = 1;
+ isl_assert(ctx, res != isl_lp_unbounded, goto error);
+ if (res == isl_lp_error)
+ goto error;
+ if (!empty && isl_tab_sample_is_integer(tab))
+ break;
+ isl_seq_neg(tab->basis->row[1 + level] + 1,
+ tab->basis->row[1 + level] + 1, dim);
+ res = isl_tab_min(tab, tab->basis->row[1 + level],
+ ctx->one, &max->el[level], NULL, 0);
+ isl_seq_neg(tab->basis->row[1 + level] + 1,
+ tab->basis->row[1 + level] + 1, dim);
+ isl_int_neg(max->el[level], max->el[level]);
+ if (res == isl_lp_empty)
+ empty = 1;
+ isl_assert(ctx, res != isl_lp_unbounded, goto error);
+ if (res == isl_lp_error)
+ goto error;
+ if (!empty && isl_tab_sample_is_integer(tab))
+ break;
+ if (!empty && !reduced && ctx->gbr != ISL_GBR_NEVER &&
+ isl_int_lt(min->el[level], max->el[level])) {
+ unsigned gbr_only_first;
+ if (ctx->gbr == ISL_GBR_ONCE)
+ ctx->gbr = ISL_GBR_NEVER;
+ tab->n_zero = level;
+ gbr_only_first = ctx->gbr_only_first;
+ ctx->gbr_only_first =
+ ctx->gbr == ISL_GBR_ALWAYS;
+ tab = isl_tab_compute_reduced_basis(tab);
+ ctx->gbr_only_first = gbr_only_first;
+ if (!tab || !tab->basis)
+ goto error;
+ reduced = 1;
+ continue;
+ }
+ reduced = 0;
+ snap[level] = isl_tab_snap(tab);
+ } else
+ isl_int_add_ui(min->el[level], min->el[level], 1);
+
+ if (empty || isl_int_gt(min->el[level], max->el[level])) {
+ level--;
+ init = 0;
+ if (level >= 0)
+ isl_tab_rollback(tab, snap[level]);
+ continue;
+ }
+ isl_int_neg(tab->basis->row[1 + level][0], min->el[level]);
+ tab = isl_tab_add_valid_eq(tab, tab->basis->row[1 + level]);
+ isl_int_set_si(tab->basis->row[1 + level][0], 0);
+ if (level + tab->n_unbounded < dim - 1) {
+ ++level;
+ init = 1;
+ continue;
+ }
+ break;
+ }
- slice = isl_basic_set_copy(bset);
- slice = isl_basic_set_cow(slice);
- slice = isl_basic_set_extend_constraints(slice, 1, 0);
- k = isl_basic_set_alloc_equality(slice);
- if (k < 0)
- goto error;
- isl_int_set(slice->eq[k][0], tmp);
- isl_int_set_si(slice->eq[k][1], -1);
- isl_seq_clr(slice->eq[k] + 2, total - 1);
- slice = isl_basic_set_simplify(slice);
- sample = sample_bounded(slice);
- slice = NULL;
+ if (level >= 0) {
+ sample = isl_tab_get_sample_value(tab);
if (!sample)
goto error;
- if (sample->size > 0)
- break;
- isl_vec_free(sample);
- sample = NULL;
- }
- if (!sample)
- sample = empty_sample(bset);
- else
- isl_basic_set_free(bset);
- isl_int_clear(tmp);
+ if (tab->n_unbounded && !isl_int_is_one(sample->el[0])) {
+ sample = isl_mat_vec_product(isl_mat_copy(tab->basis),
+ sample);
+ sample = isl_vec_ceil(sample);
+ sample = isl_mat_vec_inverse_product(
+ isl_mat_copy(tab->basis), sample);
+ }
+ } else
+ sample = isl_vec_alloc(ctx, 0);
+
+ ctx->gbr = gbr;
+ isl_vec_free(min);
+ isl_vec_free(max);
+ free(snap);
return sample;
error:
- isl_basic_set_free(bset);
- isl_basic_set_free(slice);
- isl_int_clear(tmp);
+ ctx->gbr = gbr;
+ isl_vec_free(min);
+ isl_vec_free(max);
+ free(snap);
return NULL;
}
/* Given a basic set that is known to be bounded, find and return
* an integer point in the basic set, if there is any.
*
- * After handling some trivial cases, we check the range of the
- * first coordinate. If this coordinate can only attain one integer
- * value, we are happy. Otherwise, we perform basis reduction and
- * determine the new range.
- *
- * Then we step through all possible values in the range in sample_scan.
- *
- * If any basis reduction was performed, the sample value found, if any,
- * is transformed back to the original space.
+ * After handling some trivial cases, we construct a tableau
+ * and then use isl_tab_sample to find a sample, passing it
+ * the identity matrix as initial basis.
*/
static struct isl_vec *sample_bounded(struct isl_basic_set *bset)
{
unsigned dim;
struct isl_ctx *ctx;
struct isl_vec *sample;
- struct isl_vec *obj = NULL;
- struct isl_mat *T = NULL;
- isl_int min, max;
- enum isl_lp_result res;
+ struct isl_tab *tab = NULL;
if (!bset)
return NULL;
if (isl_basic_set_fast_is_empty(bset))
return empty_sample(bset);
- ctx = bset->ctx;
dim = isl_basic_set_total_dim(bset);
if (dim == 0)
return zero_sample(bset);
if (bset->n_eq > 0)
return sample_eq(bset, sample_bounded);
- isl_int_init(min);
- isl_int_init(max);
- obj = isl_vec_alloc(bset->ctx, 1 + dim);
- if (!obj)
- goto error;
- isl_seq_clr(obj->el, 1+ dim);
- isl_int_set_si(obj->el[1], 1);
+ ctx = bset->ctx;
- res = basic_set_range(bset, obj->el, bset->ctx->one, &min, &max);
- if (res == isl_lp_error)
+ tab = isl_tab_from_basic_set(bset);
+ if (!ISL_F_ISSET(bset, ISL_BASIC_SET_NO_IMPLICIT))
+ tab = isl_tab_detect_implicit_equalities(tab);
+ if (!tab)
goto error;
- isl_assert(bset->ctx, res != isl_lp_unbounded, goto error);
- if (res == isl_lp_empty || isl_int_lt(max, min)) {
- sample = empty_sample(bset);
- goto out;
- }
- if (isl_int_ne(min, max)) {
- bset = basic_set_reduced(bset, &T);
- if (!bset)
- goto error;
+ tab->bset = isl_basic_set_copy(bset);
- res = basic_set_range(bset, obj->el, bset->ctx->one, &min, &max);
- if (res == isl_lp_error)
- goto error;
- isl_assert(bset->ctx, res != isl_lp_unbounded, goto error);
- if (res == isl_lp_empty || isl_int_lt(max, min)) {
- sample = empty_sample(bset);
- goto out;
- }
- }
+ sample = isl_tab_sample(tab);
+ if (!sample)
+ goto error;
- sample = sample_scan(bset, min, max);
-out:
- if (T) {
- if (!sample || sample->size == 0)
- isl_mat_free(ctx, T);
- else
- sample = isl_mat_vec_product(ctx, T, sample);
+ if (sample->size > 0) {
+ isl_vec_free(bset->sample);
+ bset->sample = isl_vec_copy(sample);
}
- isl_vec_free(obj);
- isl_int_clear(min);
- isl_int_clear(max);
+
+ isl_basic_set_free(bset);
+ isl_tab_free(tab);
return sample;
error:
- isl_mat_free(ctx, T);
isl_basic_set_free(bset);
- isl_vec_free(obj);
- isl_int_clear(min);
- isl_int_clear(max);
+ isl_tab_free(tab);
return NULL;
}
return NULL;
tab = isl_tab_from_basic_set(bset);
- sample = isl_tab_get_sample_value(bset->ctx, tab);
- isl_tab_free(bset->ctx, tab);
+ sample = isl_tab_get_sample_value(tab);
+ isl_tab_free(tab);
isl_basic_set_free(bset);
return sample;
}
-/* Given a rational vector, with the denominator in the first element
- * of the vector, round up all coordinates.
- */
-struct isl_vec *isl_vec_ceil(struct isl_vec *vec)
-{
- int i;
-
- vec = isl_vec_cow(vec);
- if (!vec)
- return NULL;
-
- isl_seq_cdiv_q(vec->el + 1, vec->el + 1, vec->el[0], vec->size - 1);
-
- isl_int_set_si(vec->el[0], 1);
-
- return vec;
-}
-
/* Given a linear cone "cone" and a rational point "vec",
* construct a polyhedron with shifted copies of the constraints in "cone",
* i.e., a polyhedron with "cone" as its recession cone, such that each
isl_assert(vec->ctx, vec->size != 0, goto error);
if (isl_int_is_one(vec->el[0])) {
- isl_mat_free(vec->ctx, U);
+ isl_mat_free(U);
isl_basic_set_free(cone);
return vec;
}
vec = isl_vec_ceil(vec);
return vec;
error:
- isl_mat_free(vec ? vec->ctx : cone ? cone->ctx : NULL, U);
+ isl_mat_free(U);
isl_vec_free(vec);
isl_basic_set_free(cone);
return NULL;
* then combined into a single sample point and transformed back
* to the original space.
*/
-static struct isl_vec *sample_with_cone(struct isl_basic_set *bset,
- struct isl_basic_set *cone)
+__isl_give isl_vec *isl_basic_set_sample_with_cone(
+ __isl_take isl_basic_set *bset, __isl_take isl_basic_set *cone)
{
unsigned total;
unsigned cone_dim;
cone_dim = total - cone->n_eq;
M = isl_mat_sub_alloc(bset->ctx, cone->eq, 0, cone->n_eq, 1, total);
- M = isl_mat_left_hermite(bset->ctx, M, 0, &U, NULL);
+ M = isl_mat_left_hermite(M, 0, &U, NULL);
if (!M)
goto error;
- isl_mat_free(bset->ctx, M);
+ isl_mat_free(M);
- U = isl_mat_lin_to_aff(bset->ctx, U);
- bset = isl_basic_set_preimage(bset, isl_mat_copy(bset->ctx, U));
+ U = isl_mat_lin_to_aff(U);
+ bset = isl_basic_set_preimage(bset, isl_mat_copy(U));
bounded = isl_basic_set_copy(bset);
bounded = drop_constraints_involving(bounded, total - cone_dim, cone_dim);
if (!sample || sample->size == 0) {
isl_basic_set_free(bset);
isl_basic_set_free(cone);
- isl_mat_free(ctx, U);
+ isl_mat_free(U);
return sample;
}
bset = plug_in(bset, isl_vec_copy(sample));
cone_sample = rational_sample(bset);
- cone_sample = round_up_in_cone(cone_sample, cone, isl_mat_copy(ctx, U));
+ cone_sample = round_up_in_cone(cone_sample, cone, isl_mat_copy(U));
sample = vec_concat(sample, cone_sample);
- sample = isl_mat_vec_product(ctx, U, sample);
+ sample = isl_mat_vec_product(U, sample);
return sample;
error:
isl_basic_set_free(cone);
* sample_with_cone. Otherwise, we directly perform generalized basis
* reduction.
*/
-static struct isl_vec *gbr_sample_no_lineality(struct isl_basic_set *bset)
+static struct isl_vec *gbr_sample(struct isl_basic_set *bset)
{
unsigned dim;
struct isl_basic_set *cone;
cone = isl_basic_set_recession_cone(isl_basic_set_copy(bset));
if (cone->n_eq < dim)
- return sample_with_cone(bset, cone);
+ return isl_basic_set_sample_with_cone(bset, cone);
isl_basic_set_free(cone);
return sample_bounded(bset);
}
-static struct isl_vec *sample_no_lineality(struct isl_basic_set *bset)
-{
- unsigned dim;
-
- if (isl_basic_set_fast_is_empty(bset))
- return empty_sample(bset);
- if (bset->n_eq > 0)
- return sample_eq(bset, sample_no_lineality);
- dim = isl_basic_set_total_dim(bset);
- if (dim == 0)
- return zero_sample(bset);
- if (dim == 1)
- return interval_sample(bset);
-
- switch (bset->ctx->ilp_solver) {
- case ISL_ILP_PIP:
- return isl_pip_basic_set_sample(bset);
- case ISL_ILP_GBR:
- return gbr_sample_no_lineality(bset);
- }
- isl_assert(bset->ctx, 0, );
- isl_basic_set_free(bset);
- return NULL;
-}
-
-/* Compute an integer point in "bset" with a lineality space that
- * is orthogonal to the constraints in "bounds".
- *
- * We first perform a unimodular transformation on bset that
- * make the constraints in bounds (and therefore all constraints in bset)
- * only involve the first dimensions. The remaining dimensions
- * then do not appear in any constraints and we can select any value
- * for them, say zero. We therefore project out this final dimensions
- * and plug in the value zero later. This is accomplished by simply
- * dropping the final columns of the unimodular transformation.
- */
-static struct isl_vec *sample_lineality(struct isl_basic_set *bset,
- struct isl_mat *bounds)
+static struct isl_vec *pip_sample(struct isl_basic_set *bset)
{
- struct isl_mat *U = NULL;
- unsigned old_dim, new_dim;
- struct isl_vec *sample;
+ struct isl_mat *T;
struct isl_ctx *ctx;
+ struct isl_vec *sample;
- if (!bset || !bounds)
- goto error;
+ bset = isl_basic_set_skew_to_positive_orthant(bset, &T);
+ if (!bset)
+ return NULL;
ctx = bset->ctx;
- old_dim = isl_basic_set_n_dim(bset);
- new_dim = bounds->n_row;
- bounds = isl_mat_left_hermite(ctx, bounds, 0, &U, NULL);
- if (!bounds)
- goto error;
- U = isl_mat_lin_to_aff(ctx, U);
- U = isl_mat_drop_cols(ctx, U, 1 + new_dim, old_dim - new_dim);
- bset = isl_basic_set_preimage(bset, isl_mat_copy(ctx, U));
- if (!bset)
- goto error;
- isl_mat_free(ctx, bounds);
+ sample = isl_pip_basic_set_sample(bset);
- sample = sample_no_lineality(bset);
if (sample && sample->size != 0)
- sample = isl_mat_vec_product(ctx, U, sample);
+ sample = isl_mat_vec_product(T, sample);
else
- isl_mat_free(ctx, U);
+ isl_mat_free(T);
+
return sample;
-error:
- isl_mat_free(ctx, bounds);
- isl_mat_free(ctx, U);
- isl_basic_set_free(bset);
- return NULL;
}
-struct isl_vec *isl_basic_set_sample(struct isl_basic_set *bset)
+static struct isl_vec *basic_set_sample(struct isl_basic_set *bset, int bounded)
{
struct isl_ctx *ctx;
- struct isl_mat *bounds;
unsigned dim;
if (!bset)
return NULL;
bset->sample = NULL;
if (bset->n_eq > 0)
- return sample_eq(bset, isl_basic_set_sample);
+ return sample_eq(bset, bounded ? isl_basic_set_sample_bounded
+ : isl_basic_set_sample_vec);
if (dim == 0)
return zero_sample(bset);
if (dim == 1)
return interval_sample(bset);
- bounds = independent_bounds(ctx, bset);
- if (!bounds)
- goto error;
- if (bounds->n_row == 0) {
- isl_mat_free(ctx, bounds);
- return zero_sample(bset);
+ switch (bset->ctx->ilp_solver) {
+ case ISL_ILP_PIP:
+ return pip_sample(bset);
+ case ISL_ILP_GBR:
+ return bounded ? sample_bounded(bset) : gbr_sample(bset);
}
- if (bounds->n_row < dim)
- return sample_lineality(bset, bounds);
+ isl_assert(bset->ctx, 0, );
+error:
+ isl_basic_set_free(bset);
+ return NULL;
+}
+
+__isl_give isl_vec *isl_basic_set_sample_vec(__isl_take isl_basic_set *bset)
+{
+ return basic_set_sample(bset, 0);
+}
+
+/* Compute an integer sample in "bset", where the caller guarantees
+ * that "bset" is bounded.
+ */
+struct isl_vec *isl_basic_set_sample_bounded(struct isl_basic_set *bset)
+{
+ return basic_set_sample(bset, 1);
+}
+
+__isl_give isl_basic_set *isl_basic_set_from_vec(__isl_take isl_vec *vec)
+{
+ int i;
+ int k;
+ struct isl_basic_set *bset = NULL;
+ struct isl_ctx *ctx;
+ unsigned dim;
- isl_mat_free(ctx, bounds);
- return sample_no_lineality(bset);
+ if (!vec)
+ return NULL;
+ ctx = vec->ctx;
+ isl_assert(ctx, vec->size != 0, goto error);
+
+ bset = isl_basic_set_alloc(ctx, 0, vec->size - 1, 0, vec->size - 1, 0);
+ if (!bset)
+ goto error;
+ dim = isl_basic_set_n_dim(bset);
+ for (i = dim - 1; i >= 0; --i) {
+ k = isl_basic_set_alloc_equality(bset);
+ if (k < 0)
+ goto error;
+ isl_seq_clr(bset->eq[k], 1 + dim);
+ isl_int_neg(bset->eq[k][0], vec->el[1 + i]);
+ isl_int_set(bset->eq[k][1 + i], vec->el[0]);
+ }
+ bset->sample = vec;
+
+ return bset;
error:
isl_basic_set_free(bset);
+ isl_vec_free(vec);
return NULL;
}
+
+__isl_give isl_basic_map *isl_basic_map_sample(__isl_take isl_basic_map *bmap)
+{
+ struct isl_basic_set *bset;
+ struct isl_vec *sample_vec;
+
+ bset = isl_basic_map_underlying_set(isl_basic_map_copy(bmap));
+ sample_vec = isl_basic_set_sample_vec(bset);
+ if (!sample_vec)
+ goto error;
+ if (sample_vec->size == 0) {
+ struct isl_basic_map *sample;
+ sample = isl_basic_map_empty_like(bmap);
+ isl_vec_free(sample_vec);
+ isl_basic_map_free(bmap);
+ return sample;
+ }
+ bset = isl_basic_set_from_vec(sample_vec);
+ return isl_basic_map_overlying_set(bset, bmap);
+error:
+ isl_basic_map_free(bmap);
+ return NULL;
+}
+
+__isl_give isl_basic_map *isl_map_sample(__isl_take isl_map *map)
+{
+ int i;
+ isl_basic_map *sample = NULL;
+
+ if (!map)
+ goto error;
+
+ for (i = 0; i < map->n; ++i) {
+ sample = isl_basic_map_sample(isl_basic_map_copy(map->p[i]));
+ if (!sample)
+ goto error;
+ if (!ISL_F_ISSET(sample, ISL_BASIC_MAP_EMPTY))
+ break;
+ isl_basic_map_free(sample);
+ }
+ if (i == map->n)
+ sample = isl_basic_map_empty_like_map(map);
+ isl_map_free(map);
+ return sample;
+error:
+ isl_map_free(map);
+ return NULL;
+}
+
+__isl_give isl_basic_set *isl_set_sample(__isl_take isl_set *set)
+{
+ return (isl_basic_set *) isl_map_sample((isl_map *)set);
+}