add isl_set_dim_max
authorSven Verdoolaege <skimo@kotnet.org>
Fri, 24 Jun 2011 12:58:50 +0000 (14:58 +0200)
committerSven Verdoolaege <skimo@kotnet.org>
Sat, 25 Jun 2011 20:22:21 +0000 (22:22 +0200)
Signed-off-by: Sven Verdoolaege <skimo@kotnet.org>
doc/user.pod
include/isl/set.h
isl_map.c
isl_test.c

index 1727771..c14f283 100644 (file)
@@ -1625,6 +1625,15 @@ over the points in C<set>, returning the result in C<opt>.
 The return value may be one of C<isl_lp_error>,
 C<isl_lp_ok>, C<isl_lp_unbounded> or C<isl_lp_empty>.
 
+=item * Parametric optimization
+
+       __isl_give isl_pw_aff *isl_set_dim_max(
+               __isl_take isl_set *set, int pos);
+
+Compute the maximum of the given set dimension as a function of the
+parameters, but independently of the other set dimensions.
+For lexicographic optimization, see L<"Lexicographic Optimization">.
+
 =item * Dual
 
 The following functions compute either the set of (rational) coefficient
index 798020c..c27eb46 100644 (file)
@@ -387,6 +387,8 @@ __isl_give isl_basic_set *isl_basic_set_solutions(
        __isl_take isl_basic_set *bset);
 __isl_give isl_basic_set *isl_set_solutions(__isl_take isl_set *set);
 
+__isl_give isl_pw_aff *isl_set_dim_max(__isl_take isl_set *set, int pos);
+
 #if defined(__cplusplus)
 }
 #endif
index 21587ef..fc2be4a 100644 (file)
--- a/isl_map.c
+++ b/isl_map.c
@@ -5018,6 +5018,126 @@ __isl_give isl_set *isl_set_lexmax(__isl_take isl_set *set)
        return (isl_set *)isl_map_lexmax((isl_map *)set);
 }
 
+/* Construct a map that equates the two given dimensions in the given space.
+ */
+static __isl_give isl_map *equate(__isl_take isl_dim *dim,
+       enum isl_dim_type src_type, int src_pos,
+       enum isl_dim_type dst_type, int dst_pos)
+{
+       isl_basic_map *bmap;
+       int k;
+
+       bmap = isl_basic_map_alloc_dim(dim, 0, 1, 0);
+       k = isl_basic_map_alloc_equality(bmap);
+       if (k < 0)
+               goto error;
+       isl_seq_clr(bmap->eq[k], 1 + isl_basic_map_total_dim(bmap));
+       src_pos += isl_basic_map_offset(bmap, src_type);
+       dst_pos += isl_basic_map_offset(bmap, dst_type);
+       isl_int_set_si(bmap->eq[k][src_pos], 1);
+       isl_int_set_si(bmap->eq[k][dst_pos], -1);
+
+       return isl_map_from_basic_map(bmap);
+error:
+       isl_basic_map_free(bmap);
+       return NULL;
+}
+
+/* Extract the first and only affine expression from list
+ * and then add it to *pwaff with the given dom.
+ * This domain is known to be disjoint from other domains
+ * because of the way isl_basic_set_foreach_lexmax works.
+ */
+static int update_dim_max(__isl_take isl_basic_set *dom,
+       __isl_take isl_aff_list *list, void *user)
+{
+       isl_ctx *ctx = isl_basic_set_get_ctx(dom);
+       isl_aff *aff;
+       isl_pw_aff **pwaff = user;
+       isl_pw_aff *pwaff_i;
+
+       if (isl_aff_list_n_aff(list) != 1)
+               isl_die(ctx, isl_error_internal,
+                       "expecting single element list", goto error);
+
+       aff = isl_aff_list_get_aff(list, 0);
+       pwaff_i = isl_pw_aff_alloc(isl_set_from_basic_set(dom), aff);
+
+       *pwaff = isl_pw_aff_add_disjoint(*pwaff, pwaff_i);
+
+       isl_aff_list_free(list);
+
+       return 0;
+error:
+       isl_basic_set_free(dom);
+       isl_aff_list_free(list);
+       return -1;
+}
+
+/* Given a one-dimensional basic set, compute the maximum of that
+ * dimension as an isl_pw_aff.
+ *
+ * The isl_pw_aff is constructed by having isl_basic_set_foreach_lexmax
+ * call update_dim_max on each leaf of the result.
+ */
+static __isl_give isl_pw_aff *basic_set_dim_max(__isl_keep isl_basic_set *bset)
+{
+       isl_dim *dim = isl_basic_set_get_dim(bset);
+       isl_pw_aff *pwaff;
+       int r;
+
+       dim = isl_dim_domain(isl_dim_from_range(dim));
+       pwaff = isl_pw_aff_empty(dim);
+
+       r = isl_basic_set_foreach_lexmax(bset, &update_dim_max, &pwaff);
+       if (r < 0)
+               return isl_pw_aff_free(pwaff);
+
+       return pwaff;
+}
+
+/* Compute the maximum of the given set dimension as a function of the
+ * parameters, but independently of the other set dimensions.
+ *
+ * We first project the set onto the given dimension and then compute
+ * the "lexicographic" maximum in each basic set, combining the results
+ * using isl_pw_aff_max.
+ */
+__isl_give isl_pw_aff *isl_set_dim_max(__isl_take isl_set *set, int pos)
+{
+       int i;
+       isl_map *map;
+       isl_pw_aff *pwaff;
+
+       map = isl_map_from_domain(set);
+       map = isl_map_add_dims(map, isl_dim_out, 1);
+       map = isl_map_intersect(map,
+               equate(isl_map_get_dim(map), isl_dim_in, pos,
+                                            isl_dim_out, 0));
+       set = isl_map_range(map);
+       if (!set)
+               return NULL;
+
+       if (set->n == 0) {
+               isl_dim *dim = isl_set_get_dim(set);
+               dim = isl_dim_domain(isl_dim_from_range(dim));
+               isl_set_free(set);
+               return isl_pw_aff_empty(dim);
+       }
+
+       pwaff = basic_set_dim_max(set->p[0]);
+       for (i = 1; i < set->n; ++i) {
+               isl_pw_aff *pwaff_i;
+
+               pwaff_i = basic_set_dim_max(set->p[i]);
+               pwaff = isl_pw_aff_max(pwaff, pwaff_i);
+       }
+
+       isl_set_free(set);
+
+       return pwaff;
+}
+
 /* Apply a preimage specified by "mat" on the parameters of "bset".
  * bset is assumed to have only parameters and divs.
  */
index e458e58..653b833 100644 (file)
@@ -2266,6 +2266,88 @@ int test_aff(isl_ctx *ctx)
        return 0;
 }
 
+int test_dim_max(isl_ctx *ctx)
+{
+       int equal;
+       const char *str;
+       isl_map *map, *map2;
+       isl_set *set;
+       isl_pw_aff *pwaff;
+
+       str = "[N] -> { [i] : 0 <= i <= min(N,10) }";
+       set = isl_set_read_from_str(ctx, str, -1);
+       pwaff = isl_set_dim_max(set, 0);
+       map = isl_map_from_pw_aff(pwaff);
+       str = "[N] -> { [] -> [10] : N >= 10; [] -> [N] : N <= 9 and N >= 0 }";
+       map2 = isl_map_read_from_str(ctx, str, -1);
+       equal = isl_map_is_equal(map, map2);
+       isl_map_free(map);
+       isl_map_free(map2);
+       if (equal < 0)
+               return -1;
+       if (!equal)
+               isl_die(ctx, isl_error_unknown, "unexpected result", return -1);
+
+       str = "[N] -> { [i] : 0 <= i <= max(2N,N+6) }";
+       set = isl_set_read_from_str(ctx, str, -1);
+       pwaff = isl_set_dim_max(set, 0);
+       map = isl_map_from_pw_aff(pwaff);
+       str = "[N] -> { [] -> [6 + N] : -6 <= N <= 5; [] -> [2N] : N >= 6 }";
+       map2 = isl_map_read_from_str(ctx, str, -1);
+       equal = isl_map_is_equal(map, map2);
+       isl_map_free(map);
+       isl_map_free(map2);
+       if (equal < 0)
+               return -1;
+       if (!equal)
+               isl_die(ctx, isl_error_unknown, "unexpected result", return -1);
+
+       str = "[N] -> { [i] : 0 <= i <= 2N or 0 <= i <= N+6 }";
+       set = isl_set_read_from_str(ctx, str, -1);
+       pwaff = isl_set_dim_max(set, 0);
+       map = isl_map_from_pw_aff(pwaff);
+       str = "[N] -> { [] -> [6 + N] : -6 <= N <= 5; [] -> [2N] : N >= 6 }";
+       map2 = isl_map_read_from_str(ctx, str, -1);
+       equal = isl_map_is_equal(map, map2);
+       isl_map_free(map);
+       isl_map_free(map2);
+       if (equal < 0)
+               return -1;
+       if (!equal)
+               isl_die(ctx, isl_error_unknown, "unexpected result", return -1);
+
+       str = "[N,M] -> { [i,j] -> [([i/16]), i%16, ([j/16]), j%16] : "
+                       "0 <= i < N and 0 <= j < M }";
+       map = isl_map_read_from_str(ctx, str, -1);
+       set = isl_map_range(map);
+
+       pwaff = isl_set_dim_max(isl_set_copy(set), 0);
+       map = isl_map_from_pw_aff(pwaff);
+       str = "[N,M] -> { [] -> [([(N-1)/16])] : M,N > 0 }";
+       map2 = isl_map_read_from_str(ctx, str, -1);
+       equal = isl_map_is_equal(map, map2);
+       isl_map_free(map);
+       isl_map_free(map2);
+
+       pwaff = isl_set_dim_max(isl_set_copy(set), 3);
+       map = isl_map_from_pw_aff(pwaff);
+       str = "[N,M] -> { [] -> [t] : t = min(M-1,15) and M,N > 0 }";
+       map2 = isl_map_read_from_str(ctx, str, -1);
+       if (equal >= 0 && equal)
+               equal = isl_map_is_equal(map, map2);
+       isl_map_free(map);
+       isl_map_free(map2);
+
+       isl_set_free(set);
+
+       if (equal < 0)
+               return -1;
+       if (!equal)
+               isl_die(ctx, isl_error_unknown, "unexpected result", return -1);
+
+       return 0;
+}
+
 int main()
 {
        struct isl_ctx *ctx;
@@ -2274,6 +2356,8 @@ int main()
        assert(srcdir);
 
        ctx = isl_ctx_alloc();
+       if (test_dim_max(ctx) < 0)
+               goto error;
        if (test_aff(ctx) < 0)
                goto error;
        if (test_injective(ctx) < 0)