aboutsummaryrefslogtreecommitdiff
path: root/Carpet/Carpet/src/CactusTimer.cc
blob: a4250f6eeeb673a7c030bbe3b5fa6cc10fc48c82 (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
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
#include <cassert>
#include <cstdio>
#include <cstring>
#include <iomanip>
#include <list>
#include <string>
#include <utility>
#include <vector>

#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;

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

    bool const was_running = running;
    if (was_running) stop();
    const cTimerVal * tv = CCTK_GetClockValue(timer_xml_clock, timer);
    if (was_running) start();

    if (not tv) {
      CCTK_VWarn(CCTK_WARN_ALERT, __LINE__, __FILE__, CCTK_THORNSTRING,
                 "Clock \"%s\" not found for timer #%d \"%s\"",
                 timer_xml_clock, handle, CCTK_TimerName(handle));
      return -1.0;
    }

    return CCTK_TimerClockSeconds(tv);
  }

  void CactusTimer::getGlobalTime(double& avg, double& max)
  {
    const cGH *const cctkGH = 0;
    
    int ierr;
    
    static int op_sum = -1;
    static int op_max = -1;
    if (op_sum<0) op_sum = CCTK_ReductionArrayHandle("sum");
    if (op_max<0) op_max = CCTK_ReductionArrayHandle("maximum");
    
    const double val = getTime();
    const CCTK_REAL val1 = val;
    
    CCTK_REAL sum1;
    ierr = CCTK_ReduceLocScalar(cctkGH, -1, op_sum,
                                &val1, &sum1, CCTK_VARIABLE_REAL);
    if (ierr) {
      CCTK_VWarn(CCTK_WARN_ALERT, __LINE__, __FILE__, CCTK_THORNSTRING,
                 "Error in sum reduction");
    }
    avg = sum1 / CCTK_nProcs(cctkGH);
    
    CCTK_REAL max1;
    ierr = CCTK_ReduceLocScalar(cctkGH, -1, op_max,
                                &val1, &max1, CCTK_VARIABLE_REAL);
    if (ierr) {
      CCTK_VWarn(CCTK_WARN_ALERT, __LINE__, __FILE__, CCTK_THORNSTRING,
                 "Error in maximum reduction");
    }
    max = max1;
  }
  
  vector<pair<string,string> > CactusTimer::getAllTimerNames() const
  {
    DECLARE_CCTK_PARAMETERS;
    
    static cTimerData *timer = NULL;
    if (not timer) timer = CCTK_TimerCreateData();
    assert(timer);
    
    CCTK_TimerI(handle, timer);
    
    vector<pair<string,string> > names(timer->n_vals);
    for (int i=0; i<timer->n_vals; ++i) {
      names[i].first  = timer->vals[i].heading;
      names[i].second = timer->vals[i].units;
    }
    
    return names;
  }
  
  vector<double> CactusTimer::getAllTimerValues()
  {
    DECLARE_CCTK_PARAMETERS;
    
    bool const was_running = running;
    if (was_running) stop();
    
    static cTimerData *timer = NULL;
    if (not timer) timer = CCTK_TimerCreateData();
    assert(timer);
    
    CCTK_TimerI(handle, timer);
    
    vector<double> vals(timer->n_vals);
    for (int i=0; i<timer->n_vals; ++i) {
      switch (timer->vals[i].type) {
      case val_int:    vals[i] = timer->vals[i].val.i; break;
      case val_long:   vals[i] = timer->vals[i].val.l; break;
      case val_double: vals[i] = timer->vals[i].val.d; break;
      default: CCTK_BUILTIN_UNREACHABLE();
      }
    }
    
    if (was_running) start();
    
    return vals;
  }

  // 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