source: branches/dev/Algorithm/IpTimingStatistics.hpp @ 529

Last change on this file since 529 was 529, checked in by andreasw, 15 years ago
  • in Vector class, copy cached values in Copy and update them in Scal
  • perform iterative refinement only once for adaptive strategy
  • fix bugs in PDPerturbationHandler
  • avoid some overhead in CalculateQualityFunction?
  • minor changes in timing
File size: 7.7 KB
Line 
1// Copyright (C) 2005, International Business Machines and others.
2// All Rights Reserved.
3// This code is published under the Common Public License.
4//
5// $Id: IpSolveStatistics.hpp 507 2005-08-26 18:33:43Z andreasw $
6//
7// Authors:  Andreas Waechter               IBM    2005-09-19
8
9#ifndef __IPTIMINGSTATISTICS_HPP__
10#define __IPTIMINGSTATISTICS_HPP__
11
12#include "IpReferenced.hpp"
13#include "IpJournalist.hpp"
14
15#ifdef HAVE_CTIME
16# include <ctime>
17#else
18# ifdef HAVE_TIME_H
19#  include <time.h>
20# else
21#  error "don't have header file for time"
22# endif
23#endif
24
25// The following lines are copied from CoinTime.hpp
26// We should probably make some more tests here
27#if defined(_MSC_VER)
28// Turn off compiler warning about long names
29#  pragma warning(disable:4786)
30#else
31// MacOS-X and FreeBSD needs sys/time.h
32# if defined(__MACH__) || defined (__FreeBSD__)
33#  include <sys/time.h>
34# endif
35# if !defined(__MSVCRT__)
36#  include <sys/resource.h>
37# endif
38#endif
39
40namespace Ipopt
41{
42  /** This class is used to collect timing information for a
43   *  particular task. */
44  class TimedTask
45  {
46  public:
47    /**@name Constructors/Destructors */
48    //@{
49    /** Default constructor. */
50    TimedTask()
51        :
52        total_time_(0.),
53        start_called_(false),
54        end_called_(true)
55    {}
56
57    /** Default destructor */
58    ~TimedTask()
59    {}
60    //@}
61
62    /** Method that is called before execution of the task. */
63    void Start()
64    {
65      DBG_ASSERT(end_called_);
66      DBG_ASSERT(!start_called_);
67      end_called_ = false;
68      start_called_ = true;
69      start_time_ = CpuTime();
70    }
71
72    /** Method that is called after execution of the task. */
73    void End()
74    {
75      DBG_ASSERT(!end_called_);
76      DBG_ASSERT(start_called_);
77      end_called_ = true;
78      start_called_ = false;
79      total_time_ += CpuTime() - start_time_;
80    }
81
82    /** Method that is called after execution of the task for which
83     *  timing might have been started.  This only updates the timing
84     *  if the timing has indeed been conducted. This is useful to
85     *  stop timing after catching exceptions. */
86    void EndIfStarted()
87    {
88      if (start_called_) {
89        end_called_ = true;
90        start_called_ = false;
91        total_time_ += CpuTime() - start_time_;
92      }
93      DBG_ASSERT(end_called_);
94    }
95
96    /** Method returning total time spend for task so far. */
97    Number TotalTime() const
98    {
99      DBG_ASSERT(end_called_);
100      return total_time_;
101    }
102
103  private:
104    /**@name Default Compiler Generated Methods (Hidden to avoid
105     * implicit creation/calling).  These methods are not
106     * implemented and we do not want the compiler to implement them
107     * for us, so we declare them private and do not define
108     * them. This ensures that they will not be implicitly
109     * created/called. */
110    //@{
111    /** Copy Constructor */
112    TimedTask(const TimedTask&);
113
114    /** Overloaded Equals Operator */
115    void operator=(const TimedTask&);
116    //@}
117
118    /** Time at beginning of task. */
119    Number start_time_;
120    /** Total time for task measured so far. */
121    Number total_time_;
122
123    /** @name fields for debugging */
124    //@{
125    bool start_called_;
126    bool end_called_;
127    //@}
128
129    // The following lines were taken from CoinTime.hpp in COIN/Coin
130    /** method determining CPU executed since start of program */
131    static inline Number CpuTime()
132    {
133      double cpu_temp;
134#if defined(_MSC_VER) || defined(__MSVCRT__)
135
136      unsigned int ticksnow;        /* clock_t is same as int */
137
138      ticksnow = (unsigned int)clock();
139
140      cpu_temp = (double)((double)ticksnow/CLOCKS_PER_SEC);
141#else
142
143      struct rusage usage;
144      getrusage(RUSAGE_SELF,&usage);
145      cpu_temp = usage.ru_utime.tv_sec;
146      cpu_temp += 1.0e-6*((double) usage.ru_utime.tv_usec);
147#endif
148
149      return cpu_temp;
150    }
151  };
152
153  /** This class collects all timing statistics for Ipopt.
154   */
155  class TimingStatistics : public ReferencedObject
156  {
157  public:
158    /**@name Constructors/Destructors */
159    //@{
160    /** Default constructor. */
161    TimingStatistics()
162    {}
163
164    /** Default destructor */
165    virtual ~TimingStatistics()
166    {}
167    //@}
168
169    /** Method for printing all timing information */
170    void PrintAllTimingStatistics(Journalist& jnlst,
171                                  EJournalLevel level,
172                                  EJournalCategory category) const;
173
174    /**@name Accessor methods to all timed tasks. */
175    //@{
176    TimedTask& OverallAlgorithm()
177    {
178      return OverallAlgorithm_;
179    }
180    TimedTask& PrintProblemStatistics()
181    {
182      return PrintProblemStatistics_;
183    }
184    TimedTask& InitializeIterates()
185    {
186      return InitializeIterates_;
187    }
188    TimedTask& ActualizeHessian()
189    {
190      return ActualizeHessian_;
191    }
192    TimedTask& OutputIteration()
193    {
194      return OutputIteration_;
195    }
196    TimedTask& UpdateBarrierParameter()
197    {
198      return UpdateBarrierParameter_;
199    }
200    TimedTask& ComputeSearchDirection()
201    {
202      return ComputeSearchDirection_;
203    }
204    TimedTask& ComputeAcceptableTrialPoint()
205    {
206      return ComputeAcceptableTrialPoint_;
207    }
208    TimedTask& AcceptTrialPoint()
209    {
210      return AcceptTrialPoint_;
211    }
212    TimedTask& CheckConvergence()
213    {
214      return CheckConvergence_;
215    }
216
217    TimedTask& PDSystemSolverTotal()
218    {
219      return PDSystemSolverTotal_;
220    }
221    TimedTask& PDSystemSolverSolveOnce()
222    {
223      return PDSystemSolverSolveOnce_;
224    }
225    TimedTask& ComputeResiduals()
226    {
227      return ComputeResiduals_;
228    }
229    TimedTask& LinearSystemScaling()
230    {
231      return LinearSystemScaling_;
232    }
233    TimedTask& LinearSystemSymbolicFactorization()
234    {
235      return LinearSystemSymbolicFactorization_;
236    }
237    TimedTask& LinearSystemFactorization()
238    {
239      return LinearSystemFactorization_;
240    }
241    TimedTask& LinearSystemBackSolve()
242    {
243      return LinearSystemBackSolve_;
244    }
245    TimedTask& QualityFunctionSearch()
246    {
247      return QualityFunctionSearch_;
248    }
249    TimedTask& TryCorrector()
250    {
251      return TryCorrector_;
252    }
253
254    TimedTask& Task1()
255    {
256      return Task1_;
257    }
258    TimedTask& Task2()
259    {
260      return Task2_;
261    }
262    TimedTask& Task3()
263    {
264      return Task3_;
265    }
266    TimedTask& Task4()
267    {
268      return Task4_;
269    }
270    TimedTask& Task5()
271    {
272      return Task5_;
273    }
274    TimedTask& Task6()
275    {
276      return Task6_;
277    }
278    //@}
279
280  private:
281    /**@name Default Compiler Generated Methods
282     * (Hidden to avoid implicit creation/calling).
283     * These methods are not implemented and
284     * we do not want the compiler to implement
285     * them for us, so we declare them private
286     * and do not define them. This ensures that
287     * they will not be implicitly created/called. */
288    //@{
289    /** Copy Constructor */
290    TimingStatistics(const TimingStatistics&);
291
292    /** Overloaded Equals Operator */
293    void operator=(const TimingStatistics&);
294    //@}
295
296    /**@name All timed tasks. */
297    //@{
298    TimedTask OverallAlgorithm_;
299    TimedTask PrintProblemStatistics_;
300    TimedTask InitializeIterates_;
301    TimedTask ActualizeHessian_;
302    TimedTask OutputIteration_;
303    TimedTask UpdateBarrierParameter_;
304    TimedTask ComputeSearchDirection_;
305    TimedTask ComputeAcceptableTrialPoint_;
306    TimedTask AcceptTrialPoint_;
307    TimedTask CheckConvergence_;
308
309    TimedTask PDSystemSolverTotal_;
310    TimedTask PDSystemSolverSolveOnce_;
311    TimedTask ComputeResiduals_;
312    TimedTask LinearSystemScaling_;
313    TimedTask LinearSystemSymbolicFactorization_;
314    TimedTask LinearSystemFactorization_;
315    TimedTask LinearSystemBackSolve_;
316    TimedTask QualityFunctionSearch_;
317    TimedTask TryCorrector_;
318
319    TimedTask Task1_;
320    TimedTask Task2_;
321    TimedTask Task3_;
322    TimedTask Task4_;
323    TimedTask Task5_;
324    TimedTask Task6_;
325    //@}
326  };
327
328} // namespace Ipopt
329
330#endif
Note: See TracBrowser for help on using the repository browser.