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
|
#include <cassert>
#include <cstdio>
#include <cstring>
#include <list>
#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 "Timers.hh"
namespace Carpet {
using namespace std;
// A global timer set
static
TimerSet timerSet;
// Add a timer
void
TimerSet::add (Timer * const timer)
{
timers.push_back (timer);
}
// Remove a timer
void
TimerSet::remove (Timer * const timer)
{
timers.remove (timer);
}
// Print all timer names
void
TimerSet::printNames ()
const
{
printf ("Timer names:\n");
int n = 0;
for (list <Timer *>::const_iterator
itimer = timers.begin(); itimer != timers.end(); ++ itimer)
{
printf (" [%4d] %s\n", n, (* itimer)->name());
++ n;
}
}
// Print all timer data
void
TimerSet::printData ()
{
for (list <Timer *>::const_iterator
itimer = timers.begin(); itimer != timers.end(); ++ itimer)
{
(* itimer)->printData ();
}
printf ("\n");
}
// Print all timer data
void
TimerSet::writeData (cGH const * const cctkGH,
char const * const filename)
{
int const oldfd = redirect (cctkGH, filename);
#if 0
printf ("********************************************************************************\n");
#endif
printf ("# Carpet timing information at iteration %d time %g:\n",
cctkGH->cctk_iteration, (double) cctkGH->cctk_time);
timerSet.printData ();
unredirect (oldfd);
}
// If filename is not empty, then redirect stdout to a file
int
TimerSet::redirect (cGH const * const cctkGH,
char const * const filename)
{
DECLARE_CCTK_PARAMETERS;
if (CCTK_EQUALS (filename, "")) {
return -1;
}
#ifndef HAVE_UNISTD_H
CCTK_WARN (1, "Cannot redirect timer output to a file; the operating system does not support this");
return -1;
#else
int const myproc = CCTK_MyProc (cctkGH);
char fullname [10000];
Util_snprintf (fullname, sizeof fullname,
"%s/%s.%04d.txt", out_dir, filename, myproc);
int flags = O_WRONLY | O_CREAT | O_APPEND; // append
static bool first_time = true;
if (first_time) {
first_time = false;
if (IO_TruncateOutputFiles (cctkGH)) {
flags = O_WRONLY | O_CREAT | O_TRUNC; // truncate
}
}
// Temporarily redirect stdout
fflush (stdout);
int const oldfd = dup (1); // fd 1 is stdout
int const mode = 0644; // rw-r--r--, or a+r u+w
int const fdfile = open (fullname, flags, mode);
if (fdfile < 0) {
CCTK_VWarn (1, __LINE__, __FILE__, CCTK_THORNSTRING,
"Could not open timer output file \"%s\"", fullname);
close (oldfd);
return -1;
}
close (1);
dup (fdfile); // dup to 1, i.e., stdout again
close (fdfile);
return oldfd;
#endif
}
// Redirect stdout back
void
TimerSet::unredirect (int const oldfd)
{
if (oldfd < 0) return;
#ifdef HAVE_UNISTD_H
fflush (stdout);
close (1);
dup (oldfd);
close (oldfd);
#endif
}
// Create a new Cactus timer with the given name
Timer::Timer (char const * const timername)
: running (false)
{
assert (timername);
handle = CCTK_TimerCreate (timername);
assert (handle >= 0);
timerSet.add (this);
}
// Destroy a timer
Timer::~Timer ()
{
timerSet.remove (this);
check (not CCTK_TimerDestroyI (handle));
}
// Timer name
char const *
Timer::name ()
const
{
char const * const timername = CCTK_TimerName (handle);
assert (timername);
return timername;
}
// Print timer data
void
Timer::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());
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;
default:
assert (0);
}
}
printf ("\n");
if (was_running) start();
}
} // namespace Carpet
|