aboutsummaryrefslogtreecommitdiff
path: root/Carpet/Carpet/src/TimerNode.cc
blob: 1753e4e5d0cf074217c0cf89cb7a16dea3d661c0 (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
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
/*
 *
 * The MIT License
 *
 * Copyright (c) 1997-2010 Center for the Simulation of Accidental Fires and
 * Explosions (CSAFE), and  Scientific Computing and Imaging Institute (SCI),
 * University of Utah.
 *
 * License for the specific language governing rights and limitations under
 * Permission is hereby granted, free of charge, to any person obtaining a
 * copy of this software and associated documentation files (the "Software"),
 * to deal in the Software without restriction, including without limitation
 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
 * and/or sell copies of the Software, and to permit persons to whom the
 * Software is furnished to do so, subject to the following conditions:
 *
 * The above copyright notice and this permission notice shall be included
 * in all copies or substantial portions of the Software.
 *
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
 * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
 * DEALINGS IN THE SOFTWARE.
 *
 * */

#include <algorithm>
#include <cassert>
#include <iomanip>
#include <iostream>
#include <fstream>
#include <map>
#include <ostream>
#include <string>
#include <utility>

#include "TimerNode.hh"

namespace Carpet {

  using namespace std;

  TimerNode::TimerNode(TimerTree *tree, string name):
    d_name(name), d_parent(0), d_tree(tree), d_running(0), timer(0)
  {
  }

  TimerNode::~TimerNode()
  {
    for(map<string,TimerNode*>::iterator iter=d_children.begin();
        iter!=d_children.end(); ++iter)
    {
      delete iter->second;
    }
    d_children.clear();
    delete timer;
  }

  string TimerNode::pathName() const
  {
    assert(d_parent != this);
    if (d_parent)
      return d_parent->pathName() + string("/") + getName();
    else
      return getName();
  }

  void TimerNode::instantiate()
  {
    assert(!d_running);
    d_parent = d_tree->current;
    d_tree->current = this;
    if (timer == 0)
      timer = new CactusTimer(pathName());
    d_tree->current = d_parent;
  }

  void TimerNode::start()
  {
    assert(!d_running);

    d_running = true;
    d_parent = d_tree->current;
    d_tree->current = this;
    if (timer == 0)
      timer = new CactusTimer(pathName());
    assert(timer);
    timer->start();
  }

  void TimerNode::stop()
  {
    if (d_running)
    {
      // A timer can only be stopped if it is the current timer
      if(this != d_tree->current)
        CCTK_VWarn (0, __LINE__, __FILE__, CCTK_THORNSTRING,
                    "Tried to stop non-current timer '%s'", getName().c_str());

      timer->stop();

      d_running = false;
      d_tree->current = d_parent;
    }
    else
    {
      assert(0); // Timer is not running
    }
  }

  /// Get the name of the timer
  string TimerNode::getName() const
  {
    assert(d_name.length() > 0);
    return d_name;
  }

  /// Determine if the timer is running
  bool TimerNode::isRunning() const
  {
    return d_running;
  }

  /// Find the child timer that matches the name provided.  If it is
  /// not found then a new timer with that name is allocated.
  TimerNode* TimerNode::getChildTimer(string name)
  {
    // Find child
    TimerNode *child=d_children[name];

    // If the pointer is null then allocate it
    if(child == 0)
      d_children[name] = child = new TimerNode(d_tree, name);

    return child;
  }

  /// Get the time measured by this timer
  double TimerNode::getTime()
  {
    return timer->getTime();
  }

  /// Get the names of all clocks of this timer
  vector<pair<string,string> > TimerNode::getAllTimerNames() const
  {
    return timer->getAllTimerNames();
  }

  /// Get the values of all clocks of this timer
  vector<double> TimerNode::getAllTimerValues()
  {
    return timer->getAllTimerValues();
  }

  /// Print this node and its children as an ASCII tree
  void TimerNode::print(ostream& out, double total, int level,
                        double threshold, int precision)
  {
    string space;

    // Compute the level of indentation for this depth
    for(int i=0;i<level-1;i++)
      space += "| ";

    if (level != 0)
      space += "|_";

    const int pcw = 6;
    const int tw = 8;
    const int tnw = 40;         // timer name
    const int vw = 9;           // clock values
    const streamsize oldprecision = out.precision();
    const ios_base::fmtflags oldflags = out.flags();

    const double t = getTime();
    const vector<double> values = getAllTimerValues();
    const string hyphens = string(precision-1, '-');
    const string spaces  = string(precision-1, ' ');

    if (level == 0)
    {
      const vector<pair<string,string> > names = getAllTimerNames();
      
      out << "--------" << hyphens << "--------" << hyphens
          << string(tnw+2, '-');
      for (size_t i=0; i<values.size(); ++i) {
        out << string(vw+2, '-');
      }
      out << "\n";
      
      out << "Time    " << spaces  << "  Time  " << spaces
          << "  " << setw(tnw) << left << "Timer" << right;
      for (size_t i=0; i<names.size(); ++i) {
        out << "  " << setw(vw) << names[i].first.substr(0,vw);
      }
      out << "\n";
      
      out << "percent " << spaces  << "  secs  " << spaces
          << "  " << setw(tnw) << "     ";
      for (size_t i=0; i<names.size(); ++i) {
        out << "  " << setw(vw) << names[i].second.substr(0,vw);
      }
      out << "\n";
      
      out << "--------" << hyphens << "--------" << hyphens
          << string(tnw+2, '-');
      for (size_t i=0; i<values.size(); ++i) {
        out << string(vw+2, '-');
      }
      out << "\n";
    }

    // Print this timer value
    out << fixed << setw(pcw) << setprecision(precision)
        << 100.0 * t / total << "%"
        << " " << fixed << setw(tw) << setprecision(precision) << t
        << "  " << space << setw(max(size_t(0), tnw - space.length())) << left
        << d_name.substr(0, max(size_t(10), tnw - space.length())) << right;
    for (size_t i=0; i<values.size(); ++i) {
      out.unsetf(ios_base::floatfield);
      out << "  " << setw(vw) << setprecision(vw-5) << values[i];
    }
    out << "\n";

    double children_time = 0;
    bool printed_children = false;

    // Recursively print the children
    for(map<string,TimerNode*>::iterator iter = d_children.begin();
        iter != d_children.end(); iter++)
    {
      if (iter->second->getTime() * 100.0 / total > threshold)
      {
        iter->second->print(out,total,level+1,threshold,precision);
        printed_children = true;
      }
      children_time += iter->second->getTime();
    }

    if (d_children.size() > 0 && printed_children) {
      const double untimed = t - children_time;
      
      if (100.0 * untimed / total > threshold) {
        // Print the untimed portion
        out << fixed << setw(pcw) << setprecision(1)
            << 100.0 * untimed / total << "%"
            << " " << fixed << setw(tw) << setprecision(1) << untimed
            << "  | " << space << "untimed" << "\n";
      }
    }
    out.precision (oldprecision);
    out.setf (oldflags);

    if (level == 0) {
      out << "--------" << hyphens << "--------" << hyphens
          << string(tnw+2, '-');
      for (size_t i=0; i<values.size(); ++i) {
        out << string(vw+2, '-');
      }
      out << "\n";
    }
  }

  void TimerNode::outputXML(const string &out_dir, int proc)
  {
    ostringstream filenamebuf;
    filenamebuf << out_dir << "/timertree." << proc << ".xml";
    string filenamestr = filenamebuf.str();
    const char * filename = filenamestr.c_str();
    ofstream file;
    file.open (filename, ios::out | ios::trunc);

    printXML(file,0);

    file.close();
    assert (file.good());
  }

  /// Print this node and its children as an XML file
  void TimerNode::printXML(ostream& out, int level)
  {
    string space;

    // Compute the level of indentation for this node
    for(int i=0;i<level;i++)
      space=space+"  ";

    out << space << "<timer name = " << "\"" << escapeForXML(d_name) << "\"> ";
    out << getTime() << " ";

    // For compactness, only use multiple lines if there are children
    if (d_children.size() != 0)
    {
      out << "\n";

      // Recursively print the children
      for(map<string,TimerNode*>::iterator iter=d_children.begin();iter!=d_children.end();++iter)
        iter->second->printXML(out,level+1);
      out << space;
    }

    out << "</timer>" << "\n";
  }

  /// Make a string suitable for inclusion in an XML file
  string TimerNode::escapeForXML(const string &s) const
  {
    // XML attributes cannot contain unescaped angle-brackets.  As a
    // simple solution to this, replace them with | characters.

    string s2(s);
    using std::string;
    using std::replace;

    replace(s2.begin(), s2.end(), '<', '|');
    replace(s2.begin(), s2.end(), '>', '|');
    replace(s2.begin(), s2.end(), '&', '|');

    return s2;
  }
}