aboutsummaryrefslogtreecommitdiff
path: root/Carpet/Carpet/src/Timers.hh
blob: af887cb694f18ef03c8b93819c0b4149c1a53666 (plain)
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
138
139
140
141
#include <iostream>
#include <list>

#include <cctk.h>



namespace Carpet {
  
  using namespace std;
  
  
  
  class Timer;
  
  
  
  // A set of timers
  class TimerSet {
    
    list <Timer *> timers;
    
  public:
    
    // Add a timer
    void
    add (Timer * timer);
    
    // Remove a timer
    void
    remove (Timer * timer);
    
    // Print all timer names
    void
    printNames ()
      const;
    
    // Print timer data
    void
    printData ();
    
    // Write all timer data
    static
    void
    writeData (cGH const * cctkGH,
               char const * filename);
    
  private:
    
    // If filename is not empty, then redirect stdout to a file
    static
    int
    redirect (cGH const * cctkGH,
              char const * filename);
    
    // Redirect stdout back
    static
    void
    unredirect (int oldfd);
    
  }; // class TimerSet
  
  
  
  // A timer, which is a wrapper around a Cactus timer
  class Timer {
    
    int handle;
    bool running;
    
  public:
    
    // Create a new Cactus timer with the given name
    Timer (char const * timername);
    
    // Destroy a timer
    ~Timer ();
    
    // Start the timer
    void
    start ()
    {
      msgStart ();
      running = true;
      CCTK_TimerStartI (handle);
    }
    
    // Stop the timer
    void
    stop ()
    {
      CCTK_TimerStopI (handle);
      running = false;
      msgStop ();
    }
    
    // Reset the timer
    void
    reset ()
    {
      CCTK_TimerResetI (handle);
    }
    
    // Timer name
    char const *
    name ()
      const;
    
    // Print timer data
    void
    printData ();
    
  private:
    
    // Output (debug) messages that a timer is starting or stopping
    void
    msgStart ()
      const;
    
    void
    msgStop ()
      const;
    
  };
  
  
  
  // Macros for using timers in a convenient manner
  
#define TIMING_BEGIN(name)                                \
  {                                                       \
    static Carpet::Timer timer (name);                    \
    timer.start();                                        \
    {
  
#define TIMING_END                              \
  }                                             \
    timer.stop();                               \
} while (0)
  
} // namespace Carpet