options.c: avoid calling isl_alloc_type without valid isl_ctx
[barvinok.git] / barvinok_summate.c
blobe00e33190bad252f4611b5788c3e0fbd9ae74eb1
1 #include <assert.h>
2 #include <isl/stream.h>
3 #include <barvinok/barvinok.h>
4 #include <barvinok/options.h>
5 #include <barvinok/util.h>
6 #include "verify.h"
8 struct options {
9 struct verify_options *verify;
12 ISL_ARGS_START(struct options, options_args)
13 ISL_ARG_CHILD(struct options, verify, NULL,
14 &verify_options_args, "verification")
15 ISL_ARGS_END
17 ISL_ARG_DEF(options, struct options, options_args)
19 struct verify_point_sum {
20 struct verify_point_data vpd;
21 isl_pw_qpolynomial *pwqp;
22 isl_pw_qpolynomial *sum;
24 isl_pw_qpolynomial *fixed;
25 isl_qpolynomial *manual;
28 static int manual_sum(__isl_take isl_point *pnt, void *user)
30 struct verify_point_sum *vps = (struct verify_point_sum *) user;
31 isl_qpolynomial *qp;
33 qp = isl_pw_qpolynomial_eval(isl_pw_qpolynomial_copy(vps->fixed), pnt);
34 vps->manual = isl_qpolynomial_add(vps->manual, qp);
36 return 0;
39 static int verify_point(__isl_take isl_point *pnt, void *user)
41 struct verify_point_sum *vps = (struct verify_point_sum *) user;
42 int i;
43 int ok;
44 unsigned nvar;
45 unsigned nparam;
46 isl_int v;
47 isl_space *space;
48 isl_set *dom;
49 isl_qpolynomial *eval;
50 int r;
51 FILE *out = vps->vpd.options->print_all ? stdout : stderr;
53 vps->vpd.n--;
55 isl_int_init(v);
56 vps->fixed = isl_pw_qpolynomial_copy(vps->pwqp);
57 nparam = isl_pw_qpolynomial_dim(vps->sum, isl_dim_param);
58 for (i = 0; i < nparam; ++i) {
59 isl_point_get_coordinate(pnt, isl_dim_param, i, &v);
60 vps->fixed = isl_pw_qpolynomial_fix_dim(vps->fixed,
61 isl_dim_param, i, v);
64 eval = isl_pw_qpolynomial_eval(isl_pw_qpolynomial_copy(vps->sum),
65 isl_point_copy(pnt));
67 space = isl_pw_qpolynomial_get_domain_space(vps->pwqp);
68 vps->manual = isl_qpolynomial_zero_on_domain(space);
69 dom = isl_pw_qpolynomial_domain(isl_pw_qpolynomial_copy(vps->fixed));
70 r = isl_set_foreach_point(dom, &manual_sum, user);
71 isl_set_free(dom);
72 if (r < 0)
73 goto error;
75 nvar = isl_set_dim(dom, isl_dim_set);
76 vps->manual = isl_qpolynomial_project_domain_on_params(vps->manual);
78 ok = isl_qpolynomial_plain_is_equal(eval, vps->manual);
80 if (vps->vpd.options->print_all || !ok) {
81 isl_ctx *ctx = isl_pw_qpolynomial_get_ctx(vps->pwqp);
82 isl_printer *p = isl_printer_to_file(ctx, out);
83 fprintf(out, "sum(");
84 for (i = 0; i < nparam; ++i) {
85 if (i)
86 fprintf(out, ", ");
87 isl_point_get_coordinate(pnt, isl_dim_param, i, &v);
88 isl_int_print(out, v, 0);
90 fprintf(out, ") = ");
91 p = isl_printer_print_qpolynomial(p, eval);
92 fprintf(out, ", sum(EP) = ");
93 p = isl_printer_print_qpolynomial(p, vps->manual);
94 if (ok)
95 fprintf(out, ". OK\n");
96 else
97 fprintf(out, ". NOT OK\n");
98 isl_printer_free(p);
99 } else if ((vps->vpd.n % vps->vpd.s) == 0) {
100 printf("o");
101 fflush(stdout);
104 if (0) {
105 error:
106 ok = 0;
108 isl_qpolynomial_free(vps->manual);
109 isl_pw_qpolynomial_free(vps->fixed);
110 isl_qpolynomial_free(eval);
111 isl_int_clear(v);
112 isl_point_free(pnt);
114 if (!ok)
115 vps->vpd.error = 1;
117 if (vps->vpd.options->continue_on_error)
118 ok = 1;
120 return (vps->vpd.n >= 1 && ok) ? 0 : -1;
123 static int verify(__isl_keep isl_pw_qpolynomial *pwqp,
124 __isl_take isl_pw_qpolynomial *sum, struct verify_options *options)
126 struct verify_point_sum vps = { { options } };
127 isl_set *context;
128 int r;
130 vps.pwqp = pwqp;
131 vps.sum = sum;
133 context = isl_pw_qpolynomial_domain(isl_pw_qpolynomial_copy(sum));
134 context = verify_context_set_bounds(context, options);
136 r = verify_point_data_init(&vps.vpd, context);
138 if (r == 0)
139 isl_set_foreach_point(context, verify_point, &vps);
140 if (vps.vpd.error)
141 r = -1;
143 isl_set_free(context);
145 verify_point_data_fini(&vps.vpd);
147 return r;
150 int main(int argc, char **argv)
152 int i;
153 int result = 0;
154 isl_ctx *ctx;
155 isl_space *dim;
156 isl_pw_qpolynomial *pwqp;
157 isl_pw_qpolynomial *sum;
158 struct isl_stream *s;
159 struct options *options = options_new_with_defaults();
161 argc = options_parse(options, argc, argv, ISL_ARG_ALL);
162 ctx = isl_ctx_alloc_with_options(&options_args, options);
164 s = isl_stream_new_file(ctx, stdin);
165 pwqp = isl_stream_read_pw_qpolynomial(s);
167 if (options->verify->verify) {
168 isl_space *dim = isl_pw_qpolynomial_get_space(pwqp);
169 unsigned total = isl_space_dim(dim, isl_dim_all);
170 isl_space_free(dim);
171 verify_options_set_range(options->verify, total);
174 sum = isl_pw_qpolynomial_sum(isl_pw_qpolynomial_copy(pwqp));
175 if (options->verify->verify)
176 result = verify(pwqp, sum, options->verify);
177 else {
178 isl_printer *p = isl_printer_to_file(ctx, stdout);
179 p = isl_printer_print_pw_qpolynomial(p, sum);
180 p = isl_printer_end_line(p);
181 isl_printer_free(p);
183 isl_pw_qpolynomial_free(sum);
184 isl_pw_qpolynomial_free(pwqp);
186 isl_stream_free(s);
187 isl_ctx_free(ctx);
188 return result;