Home | History | Annotate | Line # | Download | only in dist
      1 /*
      2  * Copyright 2008-2009 Katholieke Universiteit Leuven
      3  *
      4  * Use of this software is governed by the MIT license
      5  *
      6  * Written by Sven Verdoolaege, K.U.Leuven, Departement
      7  * Computerwetenschappen, Celestijnenlaan 200A, B-3001 Leuven, Belgium
      8  */
      9 
     10 #ifndef ISL_TAB_H
     11 #define ISL_TAB_H
     12 
     13 #include "isl_int.h"
     14 #include <isl/lp.h>
     15 #include <isl/map.h>
     16 #include <isl/mat.h>
     17 #include <isl/set.h>
     18 #include <isl_config.h>
     19 
     20 struct isl_tab_var {
     21 	int index;
     22 	unsigned is_row : 1;
     23 	unsigned is_nonneg : 1;
     24 	unsigned is_zero : 1;
     25 	unsigned is_redundant : 1;
     26 	unsigned marked : 1;
     27 	unsigned frozen : 1;
     28 	unsigned negated : 1;
     29 };
     30 
     31 enum isl_tab_undo_type {
     32 	isl_tab_undo_bottom,
     33 	isl_tab_undo_rational,
     34 	isl_tab_undo_empty,
     35 	isl_tab_undo_nonneg,
     36 	isl_tab_undo_redundant,
     37 	isl_tab_undo_freeze,
     38 	isl_tab_undo_zero,
     39 	isl_tab_undo_allocate,
     40 	isl_tab_undo_relax,
     41 	isl_tab_undo_unrestrict,
     42 	isl_tab_undo_bmap_ineq,
     43 	isl_tab_undo_bmap_eq,
     44 	isl_tab_undo_bmap_div,
     45 	isl_tab_undo_saved_basis,
     46 	isl_tab_undo_drop_sample,
     47 	isl_tab_undo_saved_samples,
     48 	isl_tab_undo_callback,
     49 };
     50 
     51 struct isl_tab_callback {
     52 	isl_stat (*run)(struct isl_tab_callback *cb);
     53 };
     54 
     55 union isl_tab_undo_val {
     56 	int		var_index;
     57 	int		*col_var;
     58 	int		n;
     59 	struct isl_tab_callback	*callback;
     60 };
     61 
     62 struct isl_tab_undo {
     63 	enum isl_tab_undo_type	type;
     64 	union isl_tab_undo_val	u;
     65 	struct isl_tab_undo	*next;
     66 };
     67 
     68 /* The tableau maintains equality relations.
     69  * Each column and each row is associated to a variable or a constraint.
     70  * The "value" of an inequality constraint is the value of the corresponding
     71  * slack variable.
     72  * The "row_var" and "col_var" arrays map column and row indices
     73  * to indices in the "var" and "con" arrays.  The elements of these
     74  * arrays maintain extra information about the variables and the constraints.
     75  * Each row expresses the corresponding row variable as an affine expression
     76  * of the column variables.
     77  * The first two columns in the matrix contain the common denominator of
     78  * the row and the numerator of the constant term.
     79  * If "M" is set, then the third column represents the "big parameter".
     80  * The third (M = 0) or fourth (M = 1) column
     81  * in the matrix is called column 0 with respect to the col_var array.
     82  * The sample value of the tableau is the value that assigns zero
     83  * to all the column variables and the constant term of each affine
     84  * expression to the corresponding row variable.
     85  * The operations on the tableau maintain the property that the sample
     86  * value satisfies the non-negativity constraints (usually on the slack
     87  * variables).
     88  *
     89  * The big parameter represents an arbitrarily big (and divisible)
     90  * positive number.  If present, then the sign of a row is determined
     91  * lexicographically, with the sign of the big parameter coefficient
     92  * considered first.  The big parameter is only used while
     93  * solving PILP problems.
     94  *
     95  * The first n_dead column variables have their values fixed to zero.
     96  * The corresponding tab_vars are flagged "is_zero".
     97  * Some of the rows that have have zero coefficients in all but
     98  * the dead columns are also flagged "is_zero".
     99  *
    100  * The first n_redundant rows correspond to inequality constraints
    101  * that are always satisfied for any value satisfying the non-redundant
    102  * rows.  The corresponding tab_vars are flagged "is_redundant".
    103  * A row variable that is flagged "is_zero" is also flagged "is_redundant"
    104  * since the constraint has been reduced to 0 = 0 and is therefore always
    105  * satisfied.
    106  *
    107  * There are "n_var" variables in total.  The first "n_param" of these
    108  * are called parameters and the last "n_div" of these are called divs.
    109  * The basic tableau operations makes no distinction between different
    110  * kinds of variables.  These special variables are only used while
    111  * solving PILP problems.
    112  *
    113  * Dead columns and redundant rows are detected on the fly.
    114  * However, the basic operations do not ensure that all dead columns
    115  * or all redundant rows are detected.
    116  * isl_tab_detect_implicit_equalities and isl_tab_detect_redundant can be used
    117  * to perform an exhaustive search for dead columns and redundant rows.
    118  *
    119  * The samples matrix contains "n_sample" integer points that have at some
    120  * point been elements satisfying the tableau.  The first "n_outside"
    121  * of them no longer satisfy the tableau.  They are kept because they
    122  * can be reinstated during rollback when the constraint that cut them
    123  * out is removed.  These samples are only maintained for the context
    124  * tableau while solving PILP problems.
    125  *
    126  * If "preserve" is set, then we want to keep all constraints in the
    127  * tableau, even if they turn out to be redundant.
    128  */
    129 enum isl_tab_row_sign {
    130 	isl_tab_row_unknown = 0,
    131 	isl_tab_row_pos,
    132 	isl_tab_row_neg,
    133 	isl_tab_row_any,
    134 };
    135 struct isl_tab {
    136 	struct isl_mat *mat;
    137 
    138 	unsigned n_row;
    139 	unsigned n_col;
    140 	unsigned n_dead;
    141 	unsigned n_redundant;
    142 
    143 	unsigned n_var;
    144 	unsigned n_param;
    145 	unsigned n_div;
    146 	unsigned max_var;
    147 	unsigned n_con;
    148 	unsigned n_eq;
    149 	unsigned max_con;
    150 	struct isl_tab_var *var;
    151 	struct isl_tab_var *con;
    152 	int *row_var;	/* v >= 0 -> var v;	v < 0 -> con ~v */
    153 	int *col_var;	/* v >= 0 -> var v;	v < 0 -> con ~v */
    154 	enum isl_tab_row_sign *row_sign;
    155 
    156 	struct isl_tab_undo bottom;
    157 	struct isl_tab_undo *top;
    158 
    159 	struct isl_vec *dual;
    160 	struct isl_basic_map *bmap;
    161 
    162 	unsigned n_sample;
    163 	unsigned n_outside;
    164 	int *sample_index;
    165 	struct isl_mat *samples;
    166 
    167 	int n_zero;
    168 	int n_unbounded;
    169 	struct isl_mat *basis;
    170 
    171 	int (*conflict)(int con, void *user);
    172 	void *conflict_user;
    173 
    174 	unsigned strict_redundant : 1;
    175 	unsigned need_undo : 1;
    176 	unsigned preserve : 1;
    177 	unsigned rational : 1;
    178 	unsigned empty : 1;
    179 	unsigned in_undo : 1;
    180 	unsigned M : 1;
    181 	unsigned cone : 1;
    182 };
    183 
    184 struct isl_tab *isl_tab_alloc(struct isl_ctx *ctx,
    185 	unsigned n_row, unsigned n_var, unsigned M);
    186 void isl_tab_free(struct isl_tab *tab);
    187 
    188 isl_ctx *isl_tab_get_ctx(struct isl_tab *tab);
    189 
    190 __isl_give struct isl_tab *isl_tab_from_basic_map(
    191 	__isl_keep isl_basic_map *bmap, int track);
    192 __isl_give struct isl_tab *isl_tab_from_basic_set(
    193 	__isl_keep isl_basic_set *bset, int track);
    194 struct isl_tab *isl_tab_from_recession_cone(__isl_keep isl_basic_set *bset,
    195 	int parametric);
    196 isl_bool isl_tab_cone_is_bounded(struct isl_tab *tab);
    197 __isl_give isl_basic_map *isl_basic_map_update_from_tab(
    198 	__isl_take isl_basic_map *bmap, struct isl_tab *tab);
    199 __isl_give isl_basic_set *isl_basic_set_update_from_tab(
    200 	__isl_take isl_basic_set *bset, struct isl_tab *tab);
    201 int isl_tab_detect_implicit_equalities(struct isl_tab *tab) WARN_UNUSED;
    202 __isl_give isl_basic_map *isl_tab_make_equalities_explicit(struct isl_tab *tab,
    203 	__isl_take isl_basic_map *bmap);
    204 int isl_tab_detect_redundant(struct isl_tab *tab) WARN_UNUSED;
    205 isl_stat isl_tab_restore_redundant(struct isl_tab *tab);
    206 #define ISL_TAB_SAVE_DUAL	(1 << 0)
    207 enum isl_lp_result isl_tab_min(struct isl_tab *tab,
    208 	isl_int *f, isl_int denom, isl_int *opt, isl_int *opt_denom,
    209 	unsigned flags) WARN_UNUSED;
    210 
    211 isl_stat isl_tab_add_ineq(struct isl_tab *tab, isl_int *ineq) WARN_UNUSED;
    212 isl_stat isl_tab_add_eq(struct isl_tab *tab, isl_int *eq) WARN_UNUSED;
    213 int isl_tab_add_valid_eq(struct isl_tab *tab, isl_int *eq) WARN_UNUSED;
    214 
    215 int isl_tab_freeze_constraint(struct isl_tab *tab, int con) WARN_UNUSED;
    216 
    217 isl_stat isl_tab_track_bmap(struct isl_tab *tab, __isl_take isl_basic_map *bmap)
    218 	WARN_UNUSED;
    219 isl_stat isl_tab_track_bset(struct isl_tab *tab, __isl_take isl_basic_set *bset)
    220 	WARN_UNUSED;
    221 __isl_keep isl_basic_set *isl_tab_peek_bset(struct isl_tab *tab);
    222 
    223 int isl_tab_is_equality(struct isl_tab *tab, int con);
    224 int isl_tab_is_redundant(struct isl_tab *tab, int con);
    225 
    226 int isl_tab_sample_is_integer(struct isl_tab *tab);
    227 __isl_give isl_vec *isl_tab_get_sample_value(struct isl_tab *tab);
    228 
    229 enum isl_ineq_type {
    230 	isl_ineq_error = -1,
    231 	isl_ineq_redundant,
    232 	isl_ineq_separate,
    233 	isl_ineq_cut,
    234 	isl_ineq_adj_eq,
    235 	isl_ineq_adj_ineq,
    236 };
    237 
    238 enum isl_ineq_type isl_tab_ineq_type(struct isl_tab *tab, isl_int *ineq);
    239 
    240 struct isl_tab_undo *isl_tab_snap(struct isl_tab *tab);
    241 isl_stat isl_tab_rollback(struct isl_tab *tab, struct isl_tab_undo *snap) WARN_UNUSED;
    242 isl_bool isl_tab_need_undo(struct isl_tab *tab);
    243 void isl_tab_clear_undo(struct isl_tab *tab);
    244 
    245 int isl_tab_relax(struct isl_tab *tab, int con) WARN_UNUSED;
    246 int isl_tab_select_facet(struct isl_tab *tab, int con) WARN_UNUSED;
    247 int isl_tab_unrestrict(struct isl_tab *tab, int con) WARN_UNUSED;
    248 
    249 void isl_tab_dump(__isl_keep struct isl_tab *tab);
    250 
    251 /* Compute maximum instead of minimum. */
    252 #define ISL_OPT_MAX		(1 << 0)
    253 /* Compute full instead of partial optimum; also, domain argument is NULL. */
    254 #define ISL_OPT_FULL		(1 << 1)
    255 /* Result should be free of (unknown) quantified variables. */
    256 #define ISL_OPT_QE		(1 << 2)
    257 __isl_give isl_map *isl_tab_basic_map_partial_lexopt(
    258 	__isl_take isl_basic_map *bmap, __isl_take isl_basic_set *dom,
    259 	__isl_give isl_set **empty, unsigned flags);
    260 __isl_give isl_pw_multi_aff *isl_tab_basic_map_partial_lexopt_pw_multi_aff(
    261 	__isl_take isl_basic_map *bmap, __isl_take isl_basic_set *dom,
    262 	__isl_give isl_set **empty, unsigned flags);
    263 
    264 /* An isl_trivial_region represents a non-triviality region.
    265  * The region is trivial if applying "trivial" to a given sequence
    266  * of variables results in a zero vector.
    267  * pos is the location (starting at 0) of the first variable in the sequence.
    268  */
    269 struct isl_trivial_region {
    270 	int pos;
    271 	isl_mat *trivial;
    272 };
    273 
    274 __isl_give isl_vec *isl_tab_basic_set_non_trivial_lexmin(
    275 	__isl_take isl_basic_set *bset, int n_op, int n_region,
    276 	struct isl_trivial_region *region,
    277 	int (*conflict)(int con, void *user), void *user);
    278 
    279 struct isl_tab_lexmin;
    280 typedef struct isl_tab_lexmin isl_tab_lexmin;
    281 
    282 __isl_give isl_tab_lexmin *isl_tab_lexmin_from_basic_set(
    283 	__isl_take isl_basic_set *bset);
    284 int isl_tab_lexmin_dim(__isl_keep isl_tab_lexmin *tl);
    285 __isl_give isl_tab_lexmin *isl_tab_lexmin_add_eq(__isl_take isl_tab_lexmin *tl,
    286 	isl_int *eq);
    287 __isl_give isl_tab_lexmin *isl_tab_lexmin_cut_to_integer(
    288 	__isl_take isl_tab_lexmin *tl);
    289 __isl_give isl_vec *isl_tab_lexmin_get_solution(__isl_keep isl_tab_lexmin *tl);
    290 __isl_null isl_tab_lexmin *isl_tab_lexmin_free(__isl_take isl_tab_lexmin *tl);
    291 
    292 /* private */
    293 
    294 struct isl_tab_var *isl_tab_var_from_row(struct isl_tab *tab, int i);
    295 int isl_tab_mark_redundant(struct isl_tab *tab, int row) WARN_UNUSED;
    296 int isl_tab_mark_rational(struct isl_tab *tab) WARN_UNUSED;
    297 isl_stat isl_tab_mark_empty(struct isl_tab *tab) WARN_UNUSED;
    298 struct isl_tab *isl_tab_dup(struct isl_tab *tab);
    299 struct isl_tab *isl_tab_product(struct isl_tab *tab1, struct isl_tab *tab2);
    300 int isl_tab_extend_cons(struct isl_tab *tab, unsigned n_new) WARN_UNUSED;
    301 int isl_tab_allocate_con(struct isl_tab *tab) WARN_UNUSED;
    302 int isl_tab_extend_vars(struct isl_tab *tab, unsigned n_new) WARN_UNUSED;
    303 int isl_tab_insert_var(struct isl_tab *tab, int pos) WARN_UNUSED;
    304 int isl_tab_pivot(struct isl_tab *tab, int row, int col) WARN_UNUSED;
    305 int isl_tab_add_row(struct isl_tab *tab, isl_int *line) WARN_UNUSED;
    306 int isl_tab_row_is_redundant(struct isl_tab *tab, int row);
    307 int isl_tab_min_at_most_neg_one(struct isl_tab *tab, struct isl_tab_var *var);
    308 int isl_tab_sign_of_max(struct isl_tab *tab, int con);
    309 int isl_tab_kill_col(struct isl_tab *tab, int col) WARN_UNUSED;
    310 
    311 isl_stat isl_tab_push(struct isl_tab *tab, enum isl_tab_undo_type type)
    312 	WARN_UNUSED;
    313 isl_stat isl_tab_push_var(struct isl_tab *tab,
    314 	enum isl_tab_undo_type type, struct isl_tab_var *var) WARN_UNUSED;
    315 isl_stat isl_tab_push_basis(struct isl_tab *tab) WARN_UNUSED;
    316 
    317 struct isl_tab *isl_tab_init_samples(struct isl_tab *tab) WARN_UNUSED;
    318 int isl_tab_add_sample(struct isl_tab *tab,
    319 	__isl_take isl_vec *sample) WARN_UNUSED;
    320 struct isl_tab *isl_tab_drop_sample(struct isl_tab *tab, int s);
    321 isl_stat isl_tab_save_samples(struct isl_tab *tab) WARN_UNUSED;
    322 
    323 struct isl_tab *isl_tab_detect_equalities(struct isl_tab *tab,
    324 	struct isl_tab *tab_cone) WARN_UNUSED;
    325 isl_bool isl_tab_is_constant(struct isl_tab *tab, int var, isl_int *value);
    326 isl_stat isl_tab_detect_constants(struct isl_tab *tab);
    327 
    328 isl_stat isl_tab_push_callback(struct isl_tab *tab,
    329 	struct isl_tab_callback *callback) WARN_UNUSED;
    330 
    331 int isl_tab_insert_div(struct isl_tab *tab, int pos, __isl_keep isl_vec *div,
    332 	isl_stat (*add_ineq)(void *user, isl_int *), void *user);
    333 int isl_tab_add_div(struct isl_tab *tab, __isl_keep isl_vec *div);
    334 
    335 int isl_tab_shift_var(struct isl_tab *tab, int pos, isl_int shift) WARN_UNUSED;
    336 
    337 isl_stat isl_tab_swap_constraints(struct isl_tab *tab, int con1, int con2);
    338 
    339 #endif
    340