isl_sample.c: round_up_in_cone: add memory management annotations
[isl.git] / isl_lp.c
blob2706cd063d28913223817d27e791efdc81557093
1 /*
2 * Copyright 2008-2009 Katholieke Universiteit Leuven
4 * Use of this software is governed by the MIT 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/lp.h>
13 #include <isl_seq.h>
14 #include "isl_tab.h"
15 #include <isl_options_private.h>
16 #include <isl_local_space_private.h>
17 #include <isl_aff_private.h>
18 #include <isl_mat_private.h>
19 #include <isl_val_private.h>
20 #include <isl_vec_private.h>
22 #include <bset_to_bmap.c>
23 #include <set_to_map.c>
25 enum isl_lp_result isl_tab_solve_lp(__isl_keep isl_basic_map *bmap,
26 int maximize, isl_int *f, isl_int denom, isl_int *opt,
27 isl_int *opt_denom, __isl_give isl_vec **sol)
29 struct isl_tab *tab;
30 enum isl_lp_result res;
31 unsigned dim = isl_basic_map_total_dim(bmap);
33 if (maximize)
34 isl_seq_neg(f, f, 1 + dim);
36 bmap = isl_basic_map_gauss(bmap, NULL);
37 tab = isl_tab_from_basic_map(bmap, 0);
38 res = isl_tab_min(tab, f, denom, opt, opt_denom, 0);
39 if (res == isl_lp_ok && sol) {
40 *sol = isl_tab_get_sample_value(tab);
41 if (!*sol)
42 res = isl_lp_error;
44 isl_tab_free(tab);
46 if (maximize)
47 isl_seq_neg(f, f, 1 + dim);
48 if (maximize && opt)
49 isl_int_neg(*opt, *opt);
51 return res;
54 /* Given a basic map "bmap" and an affine combination of the variables "f"
55 * with denominator "denom", set *opt / *opt_denom to the minimal
56 * (or maximal if "maximize" is true) value attained by f/d over "bmap",
57 * assuming the basic map is not empty and the expression cannot attain
58 * arbitrarily small (or large) values.
59 * If opt_denom is NULL, then *opt is rounded up (or down)
60 * to the nearest integer.
61 * The return value reflects the nature of the result (empty, unbounded,
62 * minimal or maximal value returned in *opt).
64 enum isl_lp_result isl_basic_map_solve_lp(struct isl_basic_map *bmap, int max,
65 isl_int *f, isl_int d, isl_int *opt,
66 isl_int *opt_denom,
67 struct isl_vec **sol)
69 if (sol)
70 *sol = NULL;
72 if (!bmap)
73 return isl_lp_error;
75 return isl_tab_solve_lp(bmap, max, f, d, opt, opt_denom, sol);
78 enum isl_lp_result isl_basic_set_solve_lp(struct isl_basic_set *bset, int max,
79 isl_int *f, isl_int d, isl_int *opt,
80 isl_int *opt_denom,
81 struct isl_vec **sol)
83 return isl_basic_map_solve_lp(bset_to_bmap(bset), max,
84 f, d, opt, opt_denom, sol);
87 enum isl_lp_result isl_map_solve_lp(__isl_keep isl_map *map, int max,
88 isl_int *f, isl_int d, isl_int *opt,
89 isl_int *opt_denom,
90 struct isl_vec **sol)
92 int i;
93 isl_int o;
94 isl_int t;
95 isl_int opt_i;
96 isl_int opt_denom_i;
97 enum isl_lp_result res;
98 int max_div;
99 isl_vec *v = NULL;
101 if (!map)
102 return isl_lp_error;
103 if (map->n == 0)
104 return isl_lp_empty;
106 max_div = 0;
107 for (i = 0; i < map->n; ++i)
108 if (map->p[i]->n_div > max_div)
109 max_div = map->p[i]->n_div;
110 if (max_div > 0) {
111 unsigned total = isl_space_dim(map->dim, isl_dim_all);
112 v = isl_vec_alloc(map->ctx, 1 + total + max_div);
113 if (!v)
114 return isl_lp_error;
115 isl_seq_cpy(v->el, f, 1 + total);
116 isl_seq_clr(v->el + 1 + total, max_div);
117 f = v->el;
120 if (!opt && map->n > 1 && sol) {
121 isl_int_init(o);
122 opt = &o;
124 if (map->n > 0)
125 isl_int_init(opt_i);
126 if (map->n > 0 && opt_denom) {
127 isl_int_init(opt_denom_i);
128 isl_int_init(t);
131 res = isl_basic_map_solve_lp(map->p[0], max, f, d,
132 opt, opt_denom, sol);
133 if (res == isl_lp_error || res == isl_lp_unbounded)
134 goto done;
136 if (sol)
137 *sol = NULL;
139 for (i = 1; i < map->n; ++i) {
140 isl_vec *sol_i = NULL;
141 enum isl_lp_result res_i;
142 int better;
144 res_i = isl_basic_map_solve_lp(map->p[i], max, f, d,
145 &opt_i,
146 opt_denom ? &opt_denom_i : NULL,
147 sol ? &sol_i : NULL);
148 if (res_i == isl_lp_error || res_i == isl_lp_unbounded) {
149 res = res_i;
150 goto done;
152 if (res_i == isl_lp_empty)
153 continue;
154 if (res == isl_lp_empty) {
155 better = 1;
156 } else if (!opt_denom) {
157 if (max)
158 better = isl_int_gt(opt_i, *opt);
159 else
160 better = isl_int_lt(opt_i, *opt);
161 } else {
162 isl_int_mul(t, opt_i, *opt_denom);
163 isl_int_submul(t, *opt, opt_denom_i);
164 if (max)
165 better = isl_int_is_pos(t);
166 else
167 better = isl_int_is_neg(t);
169 if (better) {
170 res = res_i;
171 if (opt)
172 isl_int_set(*opt, opt_i);
173 if (opt_denom)
174 isl_int_set(*opt_denom, opt_denom_i);
175 if (sol) {
176 isl_vec_free(*sol);
177 *sol = sol_i;
179 } else
180 isl_vec_free(sol_i);
183 done:
184 isl_vec_free(v);
185 if (map->n > 0 && opt_denom) {
186 isl_int_clear(opt_denom_i);
187 isl_int_clear(t);
189 if (map->n > 0)
190 isl_int_clear(opt_i);
191 if (opt == &o)
192 isl_int_clear(o);
193 return res;
196 enum isl_lp_result isl_set_solve_lp(__isl_keep isl_set *set, int max,
197 isl_int *f, isl_int d, isl_int *opt,
198 isl_int *opt_denom,
199 struct isl_vec **sol)
201 return isl_map_solve_lp(set_to_map(set), max,
202 f, d, opt, opt_denom, sol);
205 /* Return the optimal (rational) value of "obj" over "bset", assuming
206 * that "obj" and "bset" have aligned parameters and divs.
207 * If "max" is set, then the maximal value is computed.
208 * Otherwise, the minimal value is computed.
210 * Return infinity or negative infinity if the optimal value is unbounded and
211 * NaN if "bset" is empty.
213 * Call isl_basic_set_solve_lp and translate the results.
215 static __isl_give isl_val *basic_set_opt_lp(
216 __isl_keep isl_basic_set *bset, int max, __isl_keep isl_aff *obj)
218 isl_ctx *ctx;
219 isl_val *res;
220 enum isl_lp_result lp_res;
222 if (!bset || !obj)
223 return NULL;
225 ctx = isl_aff_get_ctx(obj);
226 res = isl_val_alloc(ctx);
227 if (!res)
228 return NULL;
229 lp_res = isl_basic_set_solve_lp(bset, max, obj->v->el + 1,
230 obj->v->el[0], &res->n, &res->d, NULL);
231 if (lp_res == isl_lp_ok)
232 return isl_val_normalize(res);
233 isl_val_free(res);
234 if (lp_res == isl_lp_error)
235 return NULL;
236 if (lp_res == isl_lp_empty)
237 return isl_val_nan(ctx);
238 if (max)
239 return isl_val_infty(ctx);
240 else
241 return isl_val_neginfty(ctx);
244 /* Return the optimal (rational) value of "obj" over "bset", assuming
245 * that "obj" and "bset" have aligned parameters.
246 * If "max" is set, then the maximal value is computed.
247 * Otherwise, the minimal value is computed.
249 * Return infinity or negative infinity if the optimal value is unbounded and
250 * NaN if "bset" is empty.
252 * Align the divs of "bset" and "obj" and call basic_set_opt_lp.
254 static __isl_give isl_val *isl_basic_set_opt_lp_val_aligned(
255 __isl_keep isl_basic_set *bset, int max, __isl_keep isl_aff *obj)
257 int *exp1 = NULL;
258 int *exp2 = NULL;
259 isl_ctx *ctx;
260 isl_mat *bset_div = NULL;
261 isl_mat *div = NULL;
262 isl_val *res;
263 int bset_n_div, obj_n_div;
265 if (!bset || !obj)
266 return NULL;
268 ctx = isl_aff_get_ctx(obj);
269 if (!isl_space_is_equal(bset->dim, obj->ls->dim))
270 isl_die(ctx, isl_error_invalid,
271 "spaces don't match", return NULL);
273 bset_n_div = isl_basic_set_dim(bset, isl_dim_div);
274 obj_n_div = isl_aff_dim(obj, isl_dim_div);
275 if (bset_n_div == 0 && obj_n_div == 0)
276 return basic_set_opt_lp(bset, max, obj);
278 bset = isl_basic_set_copy(bset);
279 obj = isl_aff_copy(obj);
281 bset_div = isl_basic_set_get_divs(bset);
282 exp1 = isl_alloc_array(ctx, int, bset_n_div);
283 exp2 = isl_alloc_array(ctx, int, obj_n_div);
284 if (!bset_div || (bset_n_div && !exp1) || (obj_n_div && !exp2))
285 goto error;
287 div = isl_merge_divs(bset_div, obj->ls->div, exp1, exp2);
289 bset = isl_basic_set_expand_divs(bset, isl_mat_copy(div), exp1);
290 obj = isl_aff_expand_divs(obj, isl_mat_copy(div), exp2);
292 res = basic_set_opt_lp(bset, max, obj);
294 isl_mat_free(bset_div);
295 isl_mat_free(div);
296 free(exp1);
297 free(exp2);
298 isl_basic_set_free(bset);
299 isl_aff_free(obj);
301 return res;
302 error:
303 isl_mat_free(div);
304 isl_mat_free(bset_div);
305 free(exp1);
306 free(exp2);
307 isl_basic_set_free(bset);
308 isl_aff_free(obj);
309 return NULL;
312 /* Return the optimal (rational) value of "obj" over "bset".
313 * If "max" is set, then the maximal value is computed.
314 * Otherwise, the minimal value is computed.
316 * Return infinity or negative infinity if the optimal value is unbounded and
317 * NaN if "bset" is empty.
319 static __isl_give isl_val *isl_basic_set_opt_lp_val(
320 __isl_keep isl_basic_set *bset, int max, __isl_keep isl_aff *obj)
322 isl_bool equal;
323 isl_val *res;
325 if (!bset || !obj)
326 return NULL;
328 equal = isl_basic_set_space_has_equal_params(bset, obj->ls->dim);
329 if (equal < 0)
330 return NULL;
331 if (equal)
332 return isl_basic_set_opt_lp_val_aligned(bset, max, obj);
334 bset = isl_basic_set_copy(bset);
335 obj = isl_aff_copy(obj);
336 bset = isl_basic_set_align_params(bset, isl_aff_get_domain_space(obj));
337 obj = isl_aff_align_params(obj, isl_basic_set_get_space(bset));
339 res = isl_basic_set_opt_lp_val_aligned(bset, max, obj);
341 isl_basic_set_free(bset);
342 isl_aff_free(obj);
344 return res;
347 /* Return the minimal (rational) value of "obj" over "bset".
349 * Return negative infinity if the minimal value is unbounded and
350 * NaN if "bset" is empty.
352 __isl_give isl_val *isl_basic_set_min_lp_val(__isl_keep isl_basic_set *bset,
353 __isl_keep isl_aff *obj)
355 return isl_basic_set_opt_lp_val(bset, 0, obj);
358 /* Return the maximal (rational) value of "obj" over "bset".
360 * Return infinity if the maximal value is unbounded and
361 * NaN if "bset" is empty.
363 __isl_give isl_val *isl_basic_set_max_lp_val(__isl_keep isl_basic_set *bset,
364 __isl_keep isl_aff *obj)
366 return isl_basic_set_opt_lp_val(bset, 1, obj);