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

Last change on this file since 528 was 528, checked in by andreasw, 15 years ago

made timing work for Cygwin/Mingw?

File size: 5.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 All timed tasks. */
175    //@{
176    TimedTask OverallAlgorithm;
177    TimedTask PrintProblemStatistics;
178    TimedTask InitializeIterates;
179    TimedTask ActualizeHessian;
180    TimedTask OutputIteration;
181    TimedTask UpdateBarrierParameter;
182    TimedTask ComputeSearchDirection;
183    TimedTask ComputeAcceptableTrialPoint;
184    TimedTask AcceptTrialPoint;
185    TimedTask CheckConvergence;
186
187    TimedTask PDSystemSolverTotal;
188    TimedTask PDSystemSolverSolveOnce;
189    TimedTask LinearSystemScaling;
190    TimedTask LinearSystemSymbolicFactorization;
191    TimedTask LinearSystemFactorization;
192    TimedTask LinearSystemBackSolve;
193    TimedTask QualityFunctionSearch;
194    TimedTask TryCorrector;
195
196    TimedTask Task1;
197    TimedTask Task2;
198    TimedTask Task3;
199    TimedTask Task4;
200    TimedTask Task5;
201    TimedTask Task6;
202    //@}
203
204  private:
205    /**@name Default Compiler Generated Methods
206     * (Hidden to avoid implicit creation/calling).
207     * These methods are not implemented and
208     * we do not want the compiler to implement
209     * them for us, so we declare them private
210     * and do not define them. This ensures that
211     * they will not be implicitly created/called. */
212    //@{
213    /** Copy Constructor */
214    TimingStatistics(const TimingStatistics&);
215
216    /** Overloaded Equals Operator */
217    void operator=(const TimingStatistics&);
218    //@}
219
220  };
221
222} // namespace Ipopt
223
224#endif
Note: See TracBrowser for help on using the repository browser.