aboutsummaryrefslogtreecommitdiff
path: root/Carpet/Carpet/src/CactusTimer.cc
blob: 7e13115be96941c7454f264919d997657484a865 (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
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
#include <cassert>
#include <cstdio>
#include <cstring>
#include <list>
#include <iomanip>

#include <cctk.h>
#include <cctk_Parameters.h>
#include <util_String.h>

#if HAVE_UNISTD_H
#  include <fcntl.h>
#  include <unistd.h>
#endif

#include <defs.hh>

#include "CactusTimer.hh"
#include "TimerSet.hh"

namespace Carpet
{
  using namespace std;

  // Create a new Cactus timer with the given name
  CactusTimer::CactusTimer (const string &timername)
    : running (false)
  {
//    cout << "CactusTimer::CactusTimer(): name = " << timername << endl;
    handle = CCTK_TimerCreate (timername.c_str());
    assert (handle >= 0);

    timerSet.add (this);
  }

  // Destroy a timer
  CactusTimer::~CactusTimer ()
  {
    timerSet.remove (this);
    check (not CCTK_TimerDestroyI (handle));
  }

  // Start the timer
  void CactusTimer::start ()
  {
    msgStart ();
//    cout << "CactusTimer::start this = " << this << endl;
    running = true;
    CCTK_TimerStartI (handle);
  }

  // Stop the timer
  void CactusTimer::stop ()
  {
    CCTK_TimerStopI (handle);
    running = false;
    msgStop ();
  }

  // Reset the timer
  void CactusTimer::reset ()
  {
    CCTK_TimerResetI (handle);
  }

  // Timer name
  string CactusTimer::name () const
  {
    char const * const timername = CCTK_TimerName (handle);
    assert (timername);
    return string(timername);
  }

  double CactusTimer::getTime()
  {
    DECLARE_CCTK_PARAMETERS;

    bool const was_running = running;
    if (was_running) stop();

    static cTimerData * timer = 0;
    if (not timer) timer = CCTK_TimerCreateData ();
    assert (timer);
    CCTK_TimerI (handle, timer);

    double val = 0; // All these timers will be returned as doubles

    const cTimerVal  *tv = CCTK_GetClockValue(timer_xml_clock, timer);

    if (tv != NULL)
    {
      val = CCTK_TimerClockSeconds(tv);
    }
    else
    {
      CCTK_VWarn(1, __LINE__, __FILE__, CCTK_THORNSTRING,
                 "Clock \"%s\" not found for timer #%d \"%s\"",
                 timer_xml_clock, handle, CCTK_TimerName(handle));
      val = -1;
    }

    // for (int i=0; i<timer->n_vals; ++i) {
    //   switch (timer->vals[i].type) {
    //   case val_int:
    //     val = timer->vals[i].val.i;
    //     break;
    //   case val_long:
    //     val = timer->vals[i].val.l;
    //     break;
    //   case val_double:
    //     val = timer->vals[i].val.d;
    //     break;
    //   case val_none:
    //     val = 0; // ??
    //     break;
    //   default:
    //     assert (0);
    //   }
    // }

    if (was_running) start();

    return val;
  }


  // Print timer data
  void CactusTimer::printData ()
  {
    bool const was_running = running;
    if (was_running) stop();

#if 0
    check (not CCTK_TimerPrintDataI (handle, -1)); // -1 means: all clocks
#endif

    static cTimerData * timer = 0;
    if (not timer) timer = CCTK_TimerCreateData ();
    assert (timer);
    CCTK_TimerI (handle, timer);

    static bool firsttime = true;
    if (firsttime) {
      printf ("# 1: timer name");
      for (int i=0; i<timer->n_vals; ++i) {
        printf (" %d: %s [%s]",
                i+2, timer->vals[i].heading, timer->vals[i].units);
      }
      printf ("\n");
      firsttime = false;
    }

    printf ("%s:", name().c_str());
    for (int i=0; i<timer->n_vals; ++i) {
      switch (timer->vals[i].type) {
      case val_int:
        printf (" %d", timer->vals[i].val.i);
        break;
      case val_long:
        printf (" %ld", timer->vals[i].val.l);
        break;
      case val_double:
        printf (" %g", timer->vals[i].val.d);
        break;
      case val_none:
        break;
      default:
        assert (0);
      }
    }
    printf ("\n");

    if (was_running) start();
  }

  // Output (debug) messages that a timer is starting or stopping
  void CactusTimer::msgStart ()  const
  {
    DECLARE_CCTK_PARAMETERS;
    if (timers_verbose) {
      CCTK_VInfo (CCTK_THORNSTRING, "Timer \"%s\" starting", name().c_str());
    }
  }

  void CactusTimer::msgStop () const
  {
    DECLARE_CCTK_PARAMETERS;
    if (timers_verbose) {
      CCTK_VInfo (CCTK_THORNSTRING, "Timer \"%s\" stopping", name().c_str());
    }
  }

  ostream& CactusTimer::serialise(ostream &os)
  {
    os << scientific << setprecision(19) << getTime() << " " << name();
    return os;
  }

}  // namespace Carpet