iscc: support ub even if GiNaC support was not compiled in
[barvinok.git] / options.c
blob4df5a799d8da5948ab3787868e9017d51b5a0ad0
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: %ld\n", stats->base_cones);
20 if (stats->volume_simplices)
21 fprintf(out, "Volume simplices: %ld\n", stats->volume_simplices);
22 if (stats->topcom_chambers) {
23 fprintf(out, "TOPCOM empty chambers: %ld\n",
24 stats->topcom_empty_chambers);
25 fprintf(out, "TOPCOM chambers: %ld\n", stats->topcom_chambers);
26 fprintf(out, "TOPCOM distinct chambers: %ld\n",
27 stats->topcom_distinct_chambers);
29 if (stats->gbr_solved_lps)
30 fprintf(out, "LPs solved during GBR: %ld\n", stats->gbr_solved_lps);
31 if (stats->bernoulli_sums)
32 fprintf(out, "Bernoulli sums: %ld\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 #ifdef HAVE_GINAC
72 options->bound = BV_BOUND_BERNSTEIN;
73 #else
74 options->bound = BV_BOUND_RANGE;
75 #endif
76 options->chambers = BV_CHAMBERS_POLYLIB;
78 options->polynomial_approximation = BV_APPROX_SIGN_NONE;
79 options->approximation_method = BV_APPROX_NONE;
80 options->scale_flags = 0;
81 options->volume_triangulate = BV_VOL_VERTEX;
83 #ifdef HAVE_LIBGLPK
84 options->gbr_lp_solver = BV_GBR_GLPK;
85 #elif defined HAVE_LIBCDDGMP
86 options->gbr_lp_solver = BV_GBR_CDD;
87 #else
88 options->gbr_lp_solver = BV_GBR_PIP;
89 #endif
91 #ifdef HAVE_LIBGLPK
92 options->lp_solver = BV_LP_GLPK;
93 #elif defined HAVE_LIBCDDGMP
94 options->lp_solver = BV_LP_CDD;
95 #else
96 options->lp_solver = BV_LP_PIP;
97 #endif
99 options->summation = BV_SUM_LAURENT;
101 options->bernstein_optimize = BV_BERNSTEIN_NONE;
103 options->bernstein_recurse = BV_BERNSTEIN_FACTORS;
105 options->integer_hull = BV_HULL_GBR;
107 options->verbose = 0;
109 options->print_stats = 0;
111 options->gbr_only_first = 0;
113 return options;
116 void barvinok_options_free(struct barvinok_options *options)
118 free(options->stats);
119 free(options);
122 enum {
123 SCALE_FAST,
124 SCALE_SLOW,
125 SCALE_NARROW,
126 SCALE_NARROW2,
127 SCALE_CHAMBER,
130 const char *scale_opts[] = {
131 "fast",
132 "slow",
133 "narrow",
134 "narrow2",
135 "chamber",
136 NULL
139 static struct argp_option approx_argp_options[] = {
140 { "polynomial-approximation", BV_OPT_POLAPPROX, "lower|upper", 1 },
141 { "approximation-method", BV_OPT_APPROX, "scale|drop|volume|bernoulli", 0,
142 "method to use in polynomial approximation [default: drop]" },
143 { "scale-options", BV_OPT_SCALE,
144 "fast|slow,narrow|narrow2,chamber", 0 },
145 { "volume-triangulation", BV_OPT_VOL, "lift|vertex|barycenter", 0,
146 "type of triangulation to perform in volume computation [default: vertex]" },
147 { 0 }
150 static struct argp_option barvinok_argp_options[] = {
151 { "index", BV_OPT_MAXINDEX, "int", 0,
152 "maximal index of simple cones in decomposition" },
153 { "primal", BV_OPT_PRIMAL, 0, 0 },
154 { "table", BV_OPT_TABLE, 0, 0 },
155 { "specialization", BV_OPT_SPECIALIZATION, "[bf|df|random|todd]" },
156 #ifdef HAVE_GINAC
157 { "bound", BV_OPT_BOUND, "bernstein|range", 0,
158 "algorithm to use for computing bounds [default: bernstein]" },
159 #endif
160 #ifdef POINTS2TRIANGS_PATH
161 { "chamber-decomposition", BV_OPT_CHAMBERS, "polylib|topcom", 0,
162 "tool to use for chamber decomposition [default: polylib]" },
163 #endif
164 { "gbr", BV_OPT_GBR,
165 #if defined(HAVE_LIBGLPK) && defined(HAVE_LIBCDDGMP)
166 "cdd|glpk|pip|pip-dual",
167 #elif defined(HAVE_LIBGLPK)
168 "glpk|pip|pip-dual",
169 #elif defined(HAVE_LIBCDDGMP)
170 "cdd|pip|pip-dual",
171 #else
172 "pip|pip-dual",
173 #endif
174 0, "lp solver to use for basis reduction "
175 #ifdef HAVE_LIBGLPK
176 "[default: glpk]"
177 #elif defined HAVE_LIBCDDGMP
178 "[default: cdd]"
179 #else
180 "[default: pip]"
181 #endif
183 { "lp", BV_OPT_LP,
184 #if defined(HAVE_LIBGLPK) && defined(HAVE_LIBCDDGMP)
185 "cdd|cddf|glpk|pip|polylib",
186 #elif defined(HAVE_LIBGLPK)
187 "glpk|pip|polylib",
188 #elif defined(HAVE_LIBCDDGMP)
189 "cdd|cddf|pip|polylib",
190 #else
191 "pip|polylib",
192 #endif
193 0, "lp solver to use "
194 #if defined(HAVE_LIBGLPK)
195 "[default: glpk]",
196 #elif defined(HAVE_LIBCDDGMP)
197 "[default: cdd]",
198 #else
199 "[default: pip]",
200 #endif
202 { "summation", BV_OPT_SUM, "box|bernoulli|euler|laurent", 0,
203 "[default: laurent]" },
204 { "bernstein-recurse", BV_OPT_RECURSE, "none|factors|intervals|full", 0,
205 "[default: factors]" },
206 { "recurse", BV_OPT_RECURSE, "",
207 OPTION_ALIAS | OPTION_HIDDEN },
208 { "integer-hull", BV_OPT_HULL,
209 #ifdef USE_ZSOLVE
210 "gbr|hilbert",
211 #else
212 "gbr",
213 #endif
214 0, "[default: gbr]" },
215 { "version", 'V', 0, 0 },
216 { "verbose", 'v' },
217 { "print-stats", BV_OPT_PRINT_STATS, 0, 0 },
218 { 0 }
221 static error_t approx_parse_opt(int key, char *arg, struct argp_state *state)
223 struct barvinok_options *options = state->input;
224 char *subopt;
226 switch (key) {
227 case BV_OPT_POLAPPROX:
228 if (!arg) {
229 options->polynomial_approximation = BV_APPROX_SIGN_APPROX;
230 if (options->approximation_method == BV_APPROX_NONE)
231 options->approximation_method = BV_APPROX_SCALE;
232 } else {
233 if (!strcmp(arg, "lower"))
234 options->polynomial_approximation = BV_APPROX_SIGN_LOWER;
235 else if (!strcmp(arg, "upper"))
236 options->polynomial_approximation = BV_APPROX_SIGN_UPPER;
237 if (options->approximation_method == BV_APPROX_NONE)
238 options->approximation_method = BV_APPROX_DROP;
240 break;
241 case BV_OPT_APPROX:
242 if (options->polynomial_approximation == BV_APPROX_SIGN_NONE)
243 options->polynomial_approximation = BV_APPROX_SIGN_APPROX;
244 if (!strcmp(arg, "scale"))
245 options->approximation_method = BV_APPROX_SCALE;
246 else if (!strcmp(arg, "drop"))
247 options->approximation_method = BV_APPROX_DROP;
248 else if (!strcmp(arg, "volume"))
249 options->approximation_method = BV_APPROX_VOLUME;
250 else if (!strcmp(arg, "bernoulli"))
251 options->approximation_method = BV_APPROX_BERNOULLI;
252 else
253 argp_error(state, "unknown value for --approximation-method option");
254 break;
255 case BV_OPT_SCALE:
256 options->approximation_method = BV_APPROX_SCALE;
257 while (*arg != '\0')
258 switch (getsubopt(&arg, scale_opts, &subopt)) {
259 case SCALE_FAST:
260 options->scale_flags |= BV_APPROX_SCALE_FAST;
261 break;
262 case SCALE_SLOW:
263 options->scale_flags &= ~BV_APPROX_SCALE_FAST;
264 break;
265 case SCALE_NARROW:
266 options->scale_flags |= BV_APPROX_SCALE_NARROW;
267 options->scale_flags &= ~BV_APPROX_SCALE_NARROW2;
268 break;
269 case SCALE_NARROW2:
270 options->scale_flags |= BV_APPROX_SCALE_NARROW2;
271 options->scale_flags &= ~BV_APPROX_SCALE_NARROW;
272 break;
273 case SCALE_CHAMBER:
274 options->scale_flags |= BV_APPROX_SCALE_CHAMBER;
275 break;
276 default:
277 argp_error(state, "unknown suboption '%s'\n", subopt);
279 break;
280 case BV_OPT_VOL:
281 if (!strcmp(arg, "lift"))
282 options->volume_triangulate = BV_VOL_LIFT;
283 else if (!strcmp(arg, "vertex"))
284 options->volume_triangulate = BV_VOL_VERTEX;
285 else if (!strcmp(arg, "barycenter"))
286 options->volume_triangulate = BV_VOL_BARYCENTER;
287 break;
288 case ARGP_KEY_END:
289 if (options->polynomial_approximation == BV_APPROX_SIGN_NONE &&
290 options->approximation_method != BV_APPROX_NONE) {
291 fprintf(stderr,
292 "no polynomial approximation selected; reseting approximation method\n");
293 options->approximation_method = BV_APPROX_NONE;
295 break;
296 default:
297 return ARGP_ERR_UNKNOWN;
299 return 0;
302 static error_t barvinok_parse_opt(int key, char *arg, struct argp_state *state)
304 struct barvinok_options *options = state->input;
305 char *subopt;
307 switch (key) {
308 case ARGP_KEY_INIT:
309 state->child_inputs[0] = options;
310 break;
311 case 'v':
312 options->verbose++;
313 break;
314 case 'V':
315 printf(barvinok_version());
316 exit(0);
317 case BV_OPT_SPECIALIZATION:
318 if (!strcmp(arg, "bf"))
319 options->incremental_specialization = BV_SPECIALIZATION_BF;
320 else if (!strcmp(arg, "df"))
321 options->incremental_specialization = BV_SPECIALIZATION_DF;
322 else if (!strcmp(arg, "random"))
323 options->incremental_specialization = BV_SPECIALIZATION_RANDOM;
324 else if (!strcmp(arg, "todd"))
325 options->incremental_specialization = BV_SPECIALIZATION_TODD;
326 break;
327 case BV_OPT_PRIMAL:
328 options->primal = 1;
329 break;
330 case BV_OPT_TABLE:
331 options->lookup_table = 1;
332 break;
333 case BV_OPT_BOUND:
334 if (!strcmp(arg, "bernstein"))
335 options->bound = BV_BOUND_BERNSTEIN;
336 if (!strcmp(arg, "range"))
337 options->bound = BV_BOUND_RANGE;
338 break;
339 case BV_OPT_CHAMBERS:
340 if (!strcmp(arg, "polylib"))
341 options->chambers = BV_CHAMBERS_POLYLIB;
342 if (!strcmp(arg, "topcom"))
343 options->chambers = BV_CHAMBERS_TOPCOM;
344 break;
345 case BV_OPT_GBR:
346 if (!strcmp(arg, "cdd"))
347 options->gbr_lp_solver = BV_GBR_CDD;
348 if (!strcmp(arg, "glpk"))
349 options->gbr_lp_solver = BV_GBR_GLPK;
350 if (!strcmp(arg, "pip"))
351 options->gbr_lp_solver = BV_GBR_PIP;
352 if (!strcmp(arg, "pip-dual"))
353 options->gbr_lp_solver = BV_GBR_PIP_DUAL;
354 break;
355 case BV_OPT_LP:
356 if (!strcmp(arg, "cdd"))
357 options->lp_solver = BV_LP_CDD;
358 if (!strcmp(arg, "cddf"))
359 options->lp_solver = BV_LP_CDDF;
360 if (!strcmp(arg, "glpk"))
361 options->lp_solver = BV_LP_GLPK;
362 if (!strcmp(arg, "pip"))
363 options->lp_solver = BV_LP_PIP;
364 if (!strcmp(arg, "polylib"))
365 options->lp_solver = BV_LP_POLYLIB;
366 break;
367 case BV_OPT_MAXINDEX:
368 options->max_index = strtoul(arg, NULL, 0);
369 break;
370 case BV_OPT_SUM:
371 if (!strcmp(arg, "box"))
372 options->summation = BV_SUM_BOX;
373 else if (!strcmp(arg, "barvinok"))
374 options->summation = BV_SUM_BOX;
375 else if (!strcmp(arg, "euler"))
376 options->summation = BV_SUM_EULER;
377 else if (!strcmp(arg, "bernoulli"))
378 options->summation = BV_SUM_BERNOULLI;
379 else if (!strcmp(arg, "laurent"))
380 options->summation = BV_SUM_LAURENT;
381 else if (!strcmp(arg, "laurent_old"))
382 options->summation = BV_SUM_LAURENT_OLD;
383 else
384 argp_error(state, "unknown summation method '%s'\n", arg);
385 break;
386 case BV_OPT_RECURSE:
387 if (!strcmp(arg, "none"))
388 options->bernstein_recurse = 0;
389 else if (!strcmp(arg, "factors"))
390 options->bernstein_recurse = BV_BERNSTEIN_FACTORS;
391 else if (!strcmp(arg, "intervals"))
392 options->bernstein_recurse = BV_BERNSTEIN_INTERVALS;
393 else if (!strcmp(arg, "full"))
394 options->bernstein_recurse =
395 BV_BERNSTEIN_FACTORS | BV_BERNSTEIN_INTERVALS;
396 break;
397 case BV_OPT_HULL:
398 if (!strcmp(arg, "gbr"))
399 options->integer_hull = BV_HULL_GBR;
400 else if (!strcmp(arg, "hilbert"))
401 options->integer_hull = BV_HULL_HILBERT;
402 break;
403 case BV_OPT_PRINT_STATS:
404 options->print_stats = 1;
405 break;
406 default:
407 return ARGP_ERR_UNKNOWN;
409 return 0;
412 static struct argp approx_argp = {
413 approx_argp_options, approx_parse_opt, 0, 0
416 static struct argp_child barvinok_children[] = {
417 { &approx_argp, 0, "polynomial approximation", BV_GRP_APPROX },
418 { 0 }
421 struct argp barvinok_argp = {
422 barvinok_argp_options, barvinok_parse_opt, 0, 0, barvinok_children