iscc: support transitive closure
[barvinok.git] / options.c
blobe561045b5cd03a1966aefee712b71a4777f8e3ff
1 #include <assert.h>
2 #include <unistd.h>
3 #include <barvinok/options.h>
4 #include <barvinok/util.h>
5 #include "argp.h"
6 #include "config.h"
8 #define MAXRAYS (POL_NO_DUAL | POL_INTEGER)
10 #define ALLOC(type) (type*)malloc(sizeof(type))
12 void barvinok_stats_clear(struct barvinok_stats *stats)
14 memset(stats, 0, sizeof(*stats));
17 void barvinok_stats_print(struct barvinok_stats *stats, FILE *out)
19 fprintf(out, "Base cones: %d\n", stats->base_cones);
20 if (stats->volume_simplices)
21 fprintf(out, "Volume simplices: %d\n", stats->volume_simplices);
22 if (stats->topcom_chambers) {
23 fprintf(out, "TOPCOM empty chambers: %d\n",
24 stats->topcom_empty_chambers);
25 fprintf(out, "TOPCOM chambers: %d\n", stats->topcom_chambers);
26 fprintf(out, "TOPCOM distinct chambers: %d\n",
27 stats->topcom_distinct_chambers);
29 if (stats->gbr_solved_lps)
30 fprintf(out, "LPs solved during GBR: %d\n", stats->gbr_solved_lps);
31 if (stats->bernoulli_sums)
32 fprintf(out, "Bernoulli sums: %d\n", stats->bernoulli_sums);
35 struct barvinok_options *barvinok_options_new_with_defaults()
37 struct barvinok_options *options = ALLOC(struct barvinok_options);
38 if (!options)
39 return NULL;
41 options->stats = ALLOC(struct barvinok_stats);
42 if (!options->stats) {
43 free(options);
44 return NULL;
47 barvinok_stats_clear(options->stats);
49 options->LLL_a = 1;
50 options->LLL_b = 1;
52 options->MaxRays = MAXRAYS;
54 #ifdef USE_INCREMENTAL_BF
55 options->incremental_specialization = 2;
56 #elif defined USE_INCREMENTAL_DF
57 options->incremental_specialization = 1;
58 #else
59 options->incremental_specialization = 0;
60 #endif
61 options->max_index = 1;
62 options->primal = 0;
63 #ifdef USE_MODULO
64 options->lookup_table = 0;
65 #else
66 options->lookup_table = 1;
67 #endif
68 options->count_sample_infinite = 1;
69 options->try_Delaunay_triangulation = 0;
71 options->chambers = BV_CHAMBERS_POLYLIB;
73 options->polynomial_approximation = BV_APPROX_SIGN_NONE;
74 options->approximation_method = BV_APPROX_NONE;
75 options->scale_flags = 0;
76 options->volume_triangulate = BV_VOL_VERTEX;
78 #ifdef HAVE_LIBGLPK
79 options->gbr_lp_solver = BV_GBR_GLPK;
80 #elif defined HAVE_LIBCDDGMP
81 options->gbr_lp_solver = BV_GBR_CDD;
82 #else
83 options->gbr_lp_solver = BV_GBR_PIP;
84 #endif
86 #ifdef HAVE_LIBGLPK
87 options->lp_solver = BV_LP_GLPK;
88 #elif defined HAVE_LIBCDDGMP
89 options->lp_solver = BV_LP_CDD;
90 #else
91 options->lp_solver = BV_LP_PIP;
92 #endif
94 options->summation = BV_SUM_LAURENT;
96 options->bernstein_optimize = BV_BERNSTEIN_NONE;
98 options->bernstein_recurse = BV_BERNSTEIN_FACTORS;
100 options->integer_hull = BV_HULL_GBR;
102 options->verbose = 0;
104 options->print_stats = 0;
106 options->gbr_only_first = 0;
108 return options;
111 void barvinok_options_free(struct barvinok_options *options)
113 free(options->stats);
114 free(options);
117 enum {
118 SCALE_FAST,
119 SCALE_SLOW,
120 SCALE_NARROW,
121 SCALE_NARROW2,
122 SCALE_CHAMBER,
125 const char *scale_opts[] = {
126 "fast",
127 "slow",
128 "narrow",
129 "narrow2",
130 "chamber",
131 NULL
134 static struct argp_option approx_argp_options[] = {
135 { "polynomial-approximation", BV_OPT_POLAPPROX, "lower|upper", 1 },
136 { "approximation-method", BV_OPT_APPROX, "scale|drop|volume|bernoulli", 0,
137 "method to use in polynomial approximation [default: drop]" },
138 { "scale-options", BV_OPT_SCALE,
139 "fast|slow,narrow|narrow2,chamber", 0 },
140 { "volume-triangulation", BV_OPT_VOL, "lift|vertex|barycenter", 0,
141 "type of triangulation to perform in volume computation [default: vertex]" },
142 { 0 }
145 static struct argp_option barvinok_argp_options[] = {
146 { "index", BV_OPT_MAXINDEX, "int", 0,
147 "maximal index of simple cones in decomposition" },
148 { "primal", BV_OPT_PRIMAL, 0, 0 },
149 { "table", BV_OPT_TABLE, 0, 0 },
150 { "specialization", BV_OPT_SPECIALIZATION, "[bf|df|random|todd]" },
151 #ifdef POINTS2TRIANGS_PATH
152 { "chamber-decomposition", BV_OPT_CHAMBERS, "polylib|topcom", 0,
153 "tool to use for chamber decomposition [default: polylib]" },
154 #endif
155 { "gbr", BV_OPT_GBR,
156 #if defined(HAVE_LIBGLPK) && defined(HAVE_LIBCDDGMP)
157 "cdd|glpk|pip|pip-dual",
158 #elif defined(HAVE_LIBGLPK)
159 "glpk|pip|pip-dual",
160 #elif defined(HAVE_LIBCDDGMP)
161 "cdd|pip|pip-dual",
162 #else
163 "pip|pip-dual",
164 #endif
165 0, "lp solver to use for basis reduction "
166 #ifdef HAVE_LIBGLPK
167 "[default: glpk]"
168 #elif defined HAVE_LIBCDDGMP
169 "[default: cdd]"
170 #else
171 "[default: pip]"
172 #endif
174 { "lp", BV_OPT_LP,
175 #if defined(HAVE_LIBGLPK) && defined(HAVE_LIBCDDGMP)
176 "cdd|cddf|glpk|pip|polylib",
177 #elif defined(HAVE_LIBGLPK)
178 "glpk|pip|polylib",
179 #elif defined(HAVE_LIBCDDGMP)
180 "cdd|cddf|pip|polylib",
181 #else
182 "pip|polylib",
183 #endif
184 0, "lp solver to use "
185 #if defined(HAVE_LIBGLPK)
186 "[default: glpk]",
187 #elif defined(HAVE_LIBCDDGMP)
188 "[default: cdd]",
189 #else
190 "[default: pip]",
191 #endif
193 { "summation", BV_OPT_SUM, "box|bernoulli|euler|laurent", 0,
194 "[default: laurent]" },
195 { "bernstein-recurse", BV_OPT_RECURSE, "none|factors|intervals|full", 0,
196 "[default: factors]" },
197 { "recurse", BV_OPT_RECURSE, "",
198 OPTION_ALIAS | OPTION_HIDDEN },
199 { "integer-hull", BV_OPT_HULL,
200 #ifdef USE_ZSOLVE
201 "gbr|hilbert",
202 #else
203 "gbr",
204 #endif
205 0, "[default: gbr]" },
206 { "version", 'V', 0, 0 },
207 { "verbose", 'v' },
208 { "print-stats", BV_OPT_PRINT_STATS, 0, 0 },
209 { 0 }
212 static error_t approx_parse_opt(int key, char *arg, struct argp_state *state)
214 struct barvinok_options *options = state->input;
215 char *subopt;
217 switch (key) {
218 case BV_OPT_POLAPPROX:
219 if (!arg) {
220 options->polynomial_approximation = BV_APPROX_SIGN_APPROX;
221 if (options->approximation_method == BV_APPROX_NONE)
222 options->approximation_method = BV_APPROX_SCALE;
223 } else {
224 if (!strcmp(arg, "lower"))
225 options->polynomial_approximation = BV_APPROX_SIGN_LOWER;
226 else if (!strcmp(arg, "upper"))
227 options->polynomial_approximation = BV_APPROX_SIGN_UPPER;
228 if (options->approximation_method == BV_APPROX_NONE)
229 options->approximation_method = BV_APPROX_DROP;
231 break;
232 case BV_OPT_APPROX:
233 if (options->polynomial_approximation == BV_APPROX_SIGN_NONE)
234 options->polynomial_approximation = BV_APPROX_SIGN_APPROX;
235 if (!strcmp(arg, "scale"))
236 options->approximation_method = BV_APPROX_SCALE;
237 else if (!strcmp(arg, "drop"))
238 options->approximation_method = BV_APPROX_DROP;
239 else if (!strcmp(arg, "volume"))
240 options->approximation_method = BV_APPROX_VOLUME;
241 else if (!strcmp(arg, "bernoulli"))
242 options->approximation_method = BV_APPROX_BERNOULLI;
243 else
244 argp_error(state, "unknown value for --approximation-method option");
245 break;
246 case BV_OPT_SCALE:
247 options->approximation_method = BV_APPROX_SCALE;
248 while (*arg != '\0')
249 switch (getsubopt(&arg, scale_opts, &subopt)) {
250 case SCALE_FAST:
251 options->scale_flags |= BV_APPROX_SCALE_FAST;
252 break;
253 case SCALE_SLOW:
254 options->scale_flags &= ~BV_APPROX_SCALE_FAST;
255 break;
256 case SCALE_NARROW:
257 options->scale_flags |= BV_APPROX_SCALE_NARROW;
258 options->scale_flags &= ~BV_APPROX_SCALE_NARROW2;
259 break;
260 case SCALE_NARROW2:
261 options->scale_flags |= BV_APPROX_SCALE_NARROW2;
262 options->scale_flags &= ~BV_APPROX_SCALE_NARROW;
263 break;
264 case SCALE_CHAMBER:
265 options->scale_flags |= BV_APPROX_SCALE_CHAMBER;
266 break;
267 default:
268 argp_error(state, "unknown suboption '%s'\n", subopt);
270 break;
271 case BV_OPT_VOL:
272 if (!strcmp(arg, "lift"))
273 options->volume_triangulate = BV_VOL_LIFT;
274 else if (!strcmp(arg, "vertex"))
275 options->volume_triangulate = BV_VOL_VERTEX;
276 else if (!strcmp(arg, "barycenter"))
277 options->volume_triangulate = BV_VOL_BARYCENTER;
278 break;
279 case ARGP_KEY_END:
280 if (options->polynomial_approximation == BV_APPROX_SIGN_NONE &&
281 options->approximation_method != BV_APPROX_NONE) {
282 fprintf(stderr,
283 "no polynomial approximation selected; reseting approximation method\n");
284 options->approximation_method = BV_APPROX_NONE;
286 break;
287 default:
288 return ARGP_ERR_UNKNOWN;
290 return 0;
293 static error_t barvinok_parse_opt(int key, char *arg, struct argp_state *state)
295 struct barvinok_options *options = state->input;
296 char *subopt;
298 switch (key) {
299 case ARGP_KEY_INIT:
300 state->child_inputs[0] = options;
301 break;
302 case 'v':
303 options->verbose++;
304 break;
305 case 'V':
306 printf(barvinok_version());
307 exit(0);
308 case BV_OPT_SPECIALIZATION:
309 if (!strcmp(arg, "bf"))
310 options->incremental_specialization = BV_SPECIALIZATION_BF;
311 else if (!strcmp(arg, "df"))
312 options->incremental_specialization = BV_SPECIALIZATION_DF;
313 else if (!strcmp(arg, "random"))
314 options->incremental_specialization = BV_SPECIALIZATION_RANDOM;
315 else if (!strcmp(arg, "todd"))
316 options->incremental_specialization = BV_SPECIALIZATION_TODD;
317 break;
318 case BV_OPT_PRIMAL:
319 options->primal = 1;
320 break;
321 case BV_OPT_TABLE:
322 options->lookup_table = 1;
323 break;
324 case BV_OPT_CHAMBERS:
325 if (!strcmp(arg, "polylib"))
326 options->chambers = BV_CHAMBERS_POLYLIB;
327 if (!strcmp(arg, "topcom"))
328 options->chambers = BV_CHAMBERS_TOPCOM;
329 break;
330 case BV_OPT_GBR:
331 if (!strcmp(arg, "cdd"))
332 options->gbr_lp_solver = BV_GBR_CDD;
333 if (!strcmp(arg, "glpk"))
334 options->gbr_lp_solver = BV_GBR_GLPK;
335 if (!strcmp(arg, "pip"))
336 options->gbr_lp_solver = BV_GBR_PIP;
337 if (!strcmp(arg, "pip-dual"))
338 options->gbr_lp_solver = BV_GBR_PIP_DUAL;
339 break;
340 case BV_OPT_LP:
341 if (!strcmp(arg, "cdd"))
342 options->lp_solver = BV_LP_CDD;
343 if (!strcmp(arg, "cddf"))
344 options->lp_solver = BV_LP_CDDF;
345 if (!strcmp(arg, "glpk"))
346 options->lp_solver = BV_LP_GLPK;
347 if (!strcmp(arg, "pip"))
348 options->lp_solver = BV_LP_PIP;
349 if (!strcmp(arg, "polylib"))
350 options->lp_solver = BV_LP_POLYLIB;
351 break;
352 case BV_OPT_MAXINDEX:
353 options->max_index = strtoul(arg, NULL, 0);
354 break;
355 case BV_OPT_SUM:
356 if (!strcmp(arg, "box"))
357 options->summation = BV_SUM_BOX;
358 else if (!strcmp(arg, "barvinok"))
359 options->summation = BV_SUM_BOX;
360 else if (!strcmp(arg, "euler"))
361 options->summation = BV_SUM_EULER;
362 else if (!strcmp(arg, "bernoulli"))
363 options->summation = BV_SUM_BERNOULLI;
364 else if (!strcmp(arg, "laurent"))
365 options->summation = BV_SUM_LAURENT;
366 else if (!strcmp(arg, "laurent_old"))
367 options->summation = BV_SUM_LAURENT_OLD;
368 else
369 argp_error(state, "unknown summation method '%s'\n", arg);
370 break;
371 case BV_OPT_RECURSE:
372 if (!strcmp(arg, "none"))
373 options->bernstein_recurse = 0;
374 else if (!strcmp(arg, "factors"))
375 options->bernstein_recurse = BV_BERNSTEIN_FACTORS;
376 else if (!strcmp(arg, "intervals"))
377 options->bernstein_recurse = BV_BERNSTEIN_INTERVALS;
378 else if (!strcmp(arg, "full"))
379 options->bernstein_recurse =
380 BV_BERNSTEIN_FACTORS | BV_BERNSTEIN_INTERVALS;
381 break;
382 case BV_OPT_HULL:
383 if (!strcmp(arg, "gbr"))
384 options->integer_hull = BV_HULL_GBR;
385 else if (!strcmp(arg, "hilbert"))
386 options->integer_hull = BV_HULL_HILBERT;
387 break;
388 case BV_OPT_PRINT_STATS:
389 options->print_stats = 1;
390 break;
391 default:
392 return ARGP_ERR_UNKNOWN;
394 return 0;
397 static struct argp approx_argp = {
398 approx_argp_options, approx_parse_opt, 0, 0
401 static struct argp_child barvinok_children[] = {
402 { &approx_argp, 0, "polynomial approximation", BV_GRP_APPROX },
403 { 0 }
406 struct argp barvinok_argp = {
407 barvinok_argp_options, barvinok_parse_opt, 0, 0, barvinok_children