File: watchdog1.cc

package info (click to toggle)
ppl 0.10.2-8
  • links: PTS
  • area: main
  • in suites: squeeze
  • size: 37,976 kB
  • ctags: 16,947
  • sloc: cpp: 168,523; sh: 10,902; makefile: 6,807; perl: 6,208; ansic: 1,300; java: 1,260; ml: 703; sed: 101
file content (137 lines) | stat: -rw-r--r-- 3,663 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
/* Test the timeout facility of the library.
   Copyright (C) 2001-2009 Roberto Bagnara <bagnara@cs.unipr.it>

This file is part of the Parma Polyhedra Library (PPL).

The PPL is free software; you can redistribute it and/or modify it
under the terms of the GNU General Public License as published by the
Free Software Foundation; either version 3 of the License, or (at your
option) any later version.

The PPL is distributed in the hope that it will be useful, but WITHOUT
ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
for more details.

You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software Foundation,
Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02111-1307, USA.

For the most up-to-date information see the Parma Polyhedra Library
site: http://www.cs.unipr.it/ppl/ . */

#include "ppl_test.hh"
#include "pwl.hh"
#include "timings.hh"

namespace {

void
compute_open_hypercube_generators(dimension_type dimension) {
  NNC_Polyhedron hypercube(dimension);
  for (dimension_type i = 0; i < dimension; ++i) {
    Variable x(i);
    hypercube.add_constraint(x > 0);
    hypercube.add_constraint(x < 1);
  }
  (void) hypercube.generators();
}

class Timeout : virtual public std::exception,
		public Parma_Polyhedra_Library::Throwable {
public:
  const char* what() const throw() {
    return "Timeout in watchdog1.cc";
  }

  void throw_me() const {
    throw *this;
  }

  int priority() const {
    return 0;
  }

  Timeout() {
  }

  ~Timeout() throw() {
  }
};

Timeout t;

bool
timed_compute_open_hypercube_generators(dimension_type dimension,
					int hundredth_secs) {
  try {
    Parma_Watchdog_Library::Watchdog
      w(hundredth_secs, abandon_expensive_computations, t);

    start_clock();

    compute_open_hypercube_generators(dimension);
    abandon_expensive_computations = 0;
    return true;
  }
  catch (const Timeout& e) {
    abandon_expensive_computations = 0;
    nout << e.what() << " after ";
    print_clock(nout);
    nout << " s" << endl;
    return false;
  }
#if !PWL_WATCHDOG_OBJECTS_ARE_SUPPORTED
  // If Watchdog objects are not supported, an std::logic_error exception
  // will be thrown: this is normal.
  catch (const std::runtime_error& e) {
  nout << "std::runtime_error exception caught: \n" << e.what() << std::endl;
  exit(0);
}
#endif // !PWL_WATCHDOG_OBJECTS_ARE_SUPPORTED
  catch (...) {
    exit(1);
  }
  // Should never get here.
  return false;
}

} // namespace

#define INIT_TIME 20

int
main() TRY {
  set_handlers();

  // Find a dimension that cannot be computed with a INIT_TIME timeout.
  dimension_type dimension = 0;
  do {
    ++dimension;
    nout << "Trying dimension " << dimension << endl;
  } while (timed_compute_open_hypercube_generators(dimension, INIT_TIME));

  // Now find an upper bound to the time necessary to compute it.
  int upper_bound = INIT_TIME;
  do {
    upper_bound *= 2;
    nout << "Trying upper bound " << upper_bound << endl;
  } while (!timed_compute_open_hypercube_generators(dimension, upper_bound));

  // Search the "exact" time.
  int lower_bound = upper_bound/2;
  do {
    int test_time = (lower_bound+upper_bound)/2;
    nout << "Probing " << test_time << endl;
    if (timed_compute_open_hypercube_generators(dimension, test_time))
      upper_bound = test_time;
    else
      lower_bound = test_time;
  } while (upper_bound-lower_bound > 4);

  nout << "Estimated time for dimension " << dimension
       << ": " << (lower_bound+upper_bound)/2 << " 100th of sec" << endl;

  return 0;
}
CATCH