1 /* Test the timeout facility of the PPL C interface library.
2    Copyright (C) 2001-2010 Roberto Bagnara <bagnara@cs.unipr.it>
3    Copyright (C) 2010-2016 BUGSENG srl (http://bugseng.com)
4 
5 This file is part of the Parma Polyhedra Library (PPL).
6 
7 The PPL is free software; you can redistribute it and/or modify it
8 under the terms of the GNU General Public License as published by the
9 Free Software Foundation; either version 3 of the License, or (at your
10 option) any later version.
11 
12 The PPL is distributed in the hope that it will be useful, but WITHOUT
13 ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
14 FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
15 for more details.
16 
17 You should have received a copy of the GNU General Public License
18 along with this program; if not, write to the Free Software Foundation,
19 Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02111-1307, USA.
20 
21 For the most up-to-date information see the Parma Polyhedra Library
22 site: http://bugseng.com/products/ppl/ . */
23 
24 #include "ppl_c_test.h"
25 #include <stdlib.h>
26 #include <stdarg.h>
27 #include <stdio.h>
28 
29 static const char* program_name = 0;
30 
31 static void
my_exit(int status)32 my_exit(int status) {
33   (void) ppl_finalize();
34   exit(status);
35 }
36 
37 static void
fatal(const char * format,...)38 fatal(const char* format, ...) {
39   va_list ap;
40   fprintf(stderr, "%s: ", program_name);
41   va_start(ap, format);
42   vfprintf(stderr, format, ap);
43   va_end(ap);
44   fprintf(stderr, "\n");
45   my_exit(1);
46 }
47 
48 static void
error_handler(enum ppl_enum_error_code code,const char * description)49 error_handler(enum ppl_enum_error_code code,
50               const char* description) {
51   if (check_noisy() || check_very_noisy())
52     fprintf(stderr, "PPL error code %d: %s\n", code, description);
53 #if !PPL_WATCHDOG_OBJECTS_ARE_SUPPORTED
54   /* If Watchdog objects are not supported, a logic error will occur:
55      this is normal. */
56   if (code == PPL_ERROR_LOGIC_ERROR)
57     my_exit(0);
58 #endif /* !PPL_WATCHDOG_OBJECTS_ARE_SUPPORTED */
59 }
60 
61 void
open_hypercube(int dimension,ppl_Polyhedron_t ph)62 open_hypercube(int dimension, ppl_Polyhedron_t ph) {
63   int i;
64   mpz_t z_one;
65   mpz_t z_minus_one;
66   ppl_Coefficient_t coeff_one;
67   ppl_Coefficient_t coeff_minus_one;
68   ppl_Linear_Expression_t le;
69   ppl_Constraint_t c;
70   ppl_Constraint_System_t cs;
71 
72   mpz_init_set_si(z_one, 1);
73   mpz_init_set_si(z_minus_one, -1);
74   ppl_new_Coefficient(&coeff_one);
75   ppl_assign_Coefficient_from_mpz_t(coeff_one, z_one);
76   ppl_new_Coefficient(&coeff_minus_one);
77   ppl_assign_Coefficient_from_mpz_t(coeff_minus_one, z_minus_one);
78   ppl_new_Linear_Expression_with_dimension(&le, dimension);
79   ppl_new_Constraint_System(&cs);
80   for (i = 0; i < dimension; ++i) {
81     ppl_Linear_Expression_add_to_coefficient(le, i, coeff_one);
82     /* Variable(i) > 0 */
83     ppl_new_Constraint(&c, le, PPL_CONSTRAINT_TYPE_GREATER_THAN);
84     ppl_Constraint_System_insert_Constraint(cs, c);
85     ppl_delete_Constraint(c);
86     /* Variable(i) < 1 */
87     ppl_Linear_Expression_add_to_inhomogeneous(le, coeff_minus_one);
88     ppl_new_Constraint(&c, le, PPL_CONSTRAINT_TYPE_LESS_THAN);
89     ppl_Constraint_System_insert_Constraint(cs, c);
90     ppl_delete_Constraint(c);
91     /* Zero `le' */
92     ppl_Linear_Expression_add_to_coefficient(le, i, coeff_minus_one);
93     ppl_Linear_Expression_add_to_inhomogeneous(le, coeff_one);
94   }
95   ppl_Polyhedron_add_constraints(ph, cs);
96   ppl_delete_Constraint_System(cs);
97   ppl_delete_Linear_Expression(le);
98   ppl_delete_Coefficient(coeff_minus_one);
99   ppl_delete_Coefficient(coeff_one);
100   mpz_clear(z_minus_one);
101   mpz_clear(z_one);
102 }
103 
104 void
timed_compute_open_hypercube_generators(int csecs,int max_dimension)105 timed_compute_open_hypercube_generators(int csecs, int max_dimension) {
106   int i;
107   int result;
108   ppl_const_Generator_System_t gs;
109   ppl_Polyhedron_t ph;
110 
111   for (i = 0; i <= max_dimension; ++i) {
112     ppl_new_NNC_Polyhedron_from_space_dimension(&ph, i, 0);
113     open_hypercube(i, ph);
114     ppl_set_timeout(csecs);
115     result = ppl_Polyhedron_get_generators(ph, &gs);
116     ppl_reset_timeout();
117     ppl_delete_Polyhedron(ph);
118     if (result == PPL_TIMEOUT_EXCEPTION)
119       /* Timeout expired */
120       return;
121     else if (result != 0)
122       /* Unexpected error */
123       exit(1);
124   }
125   /* Should not reach this point */
126   exit(1);
127 }
128 
129 int
main(int argc,char ** argv)130 main(int argc, char **argv) {
131   program_name = argv[0];
132 
133   if (argc != 1) {
134     fprintf(stderr, "usage: %s\n", program_name);
135     exit(1);
136   }
137 
138   if (ppl_initialize() < 0)
139     fatal("cannot initialize the Parma Polyhedra Library");
140 
141   if (ppl_set_error_handler(error_handler) < 0)
142     fatal("cannot install the custom error handler");
143 
144   timed_compute_open_hypercube_generators(200, 20);
145 
146   ppl_finalize();
147   return 0;
148 }
149 
150