aboutsummaryrefslogtreecommitdiff
path: root/Carpet/Timers/src/TimerTree.cc
blob: 9a715eba1c6fd10579b90438de241d7ef8a7992f (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
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
/*
 *
 * 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 <fstream>
#include <iomanip>
#include <iostream>
#include <map>
#include <ostream>
#include <string>
#include <utility>

#include <dist.hh>
#include <mpi_string.hh>

#include "TimerTree.hh"



namespace Timers {

  using namespace std;

  TimerNode::TimerNode(TimerTree *tree, const string& name):
    d_name(name), d_parent(0), d_tree(tree), d_running(0), d_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 d_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 (!d_timer)
      d_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 (!d_timer)
      d_timer = new CactusTimer(pathName());
    assert(d_timer);
    d_timer->start();
  }

  void TimerNode::stop()
  {
    assert(d_running);
    
    // A timer can only be stopped if it is the current timer
    if (this != d_tree->current)
      CCTK_VError(__LINE__, __FILE__, CCTK_THORNSTRING,
                  "Tried to stop non-current timer '%s'", getName().c_str());
    
    d_timer->stop();
    
    d_running = false;
    d_tree->current = d_parent;
  }
  
  /// Get the name of the timer
  string TimerNode::getName() const
  {
    assert(not d_name.empty());
    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(const string& name)
  {
    // Find child
    TimerNode *child = d_children[name];
    
    // If the pointer is null then allocate it
    if (not child)
      d_children[name] = child = new TimerNode(d_tree, name);
    
    return child;
  }
  
  /// Get the time measured by this timer
  double TimerNode::getTime()
  {
    return d_timer->getTime();
  }
  
  /// Get the global time measured by this timer
  void TimerNode::getGlobalTime(double& avg, double& max)
  {
    return d_timer->getGlobalTime(avg, max);
  }
  
  /// Get the names of all clocks of this timer
  vector<pair<string,string> > TimerNode::getAllTimerNames() const
  {
    return d_timer->getAllTimerNames();
  }
  
  /// Get the values of all clocks of this timer
  vector<double> TimerNode::getAllTimerValues()
  {
    return d_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();
    double tavg, tmax;
    getGlobalTime(tavg, tmax);
    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
          << "--------" << hyphens
          << "--" << string(tnw, '-');
      for (size_t i=0; i<values.size(); ++i) {
        out << "--" << string(vw, '-');
      }
      out << "\n";
      
      // timer names
      out << "Time    " << spaces
          << "  Time  " << spaces
          << " Imblnc " << 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";
      
      // timer units
      out << "percent " << spaces
          << "  secs  " << spaces
          << " percent" << 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
          << "--------" << hyphens
          << "--" << string(tnw, '-');
      for (size_t i=0; i<values.size(); ++i) {
        out << "--" << string(vw, '-');
      }
      out << "\n";
    }
    
    // Print this timer value
    out << fixed << setw(pcw) << setprecision(precision)
        << 100.0 * tavg / total << "%"
        << " " << fixed << setw(tw) << setprecision(precision) << tavg
        << " " << fixed << setw(pcw) << setprecision(precision)
        << 100.0 * (1.0 - tavg / tmax) << "%"
        << "  " << 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";

    // TODO: Don't call getGlobalTime for all timers separately.
    // Instead, call a single function that takes a snapshot of all
    // timers, and reduces these snapshots to average and maximum.
    // While printing, access these snapshots.
    
    //double children_time = 0;
    double children_tavg = 0.0;
    bool printed_children = false;
    
    // Recursively print the children
    for (map<string,TimerNode*>::iterator iter = d_children.begin();
         iter != d_children.end(); iter++)
    {
      const string timername = iter->first;
      const string root_timername =
        CarpetLib::broadcast_string(dist::comm(), 0, timername);
      if (timername != root_timername) {
        CCTK_VError(__LINE__, __FILE__, CCTK_THORNSTRING,
                   "Timers are inconsistent across processes: root process expects timer %s, this process has timer %s instead",
                    root_timername.c_str(), timername.c_str());
      }
      double child_avg, child_max;
      iter->second->getGlobalTime(child_avg, child_max);
      if (child_max * 100.0 / total > threshold) {
        iter->second->print(out,total,level+1,threshold,precision);
        printed_children = true;
      }
      //children_time += iter->second->getTime();
      children_tavg += child_avg;
    }
    {
      const string timername = "[done]";
      const string root_timername =
        CarpetLib::broadcast_string(dist::comm(), 0, timername);
      if (timername != root_timername) {
        CCTK_VError(__LINE__, __FILE__, CCTK_THORNSTRING,
                    "Timers are inconsistent across processes: root process expects timer %s, this process has timer %s instead",
                    root_timername.c_str(), timername.c_str());
      }
    }
    
    if (d_children.size() > 0 && printed_children) {
      //const double untimed = t - children_time;
      const double untimed = tavg - children_tavg;
      
      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
          << "--------" << hyphens
          << "--" << string(tnw, '-');
      for (size_t i=0; i<values.size(); ++i) {
        out << "--" << string(vw, '-');
      }
      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 += "  ";
    
    out << space << "<timer name = " << "\"" << escapeForXML(d_name) << "\"> ";
    out << getTime() << " ";
    const vector<double> values = getAllTimerValues();
    const vector<pair<string,string> > names = getAllTimerNames();

    out << endl;

    for (size_t i=0; i<names.size(); ++i)
    {
      out << space << "  " << "<value clock=\"" << names[i].first << "\" unit=\"" << names[i].second << "\">" << values[i] << "</value>" << endl;
    }
    
    // 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
  {
    ostringstream res;
    for (string::const_iterator si=s.begin(); si!=s.end(); ++si) {
      switch (*si) {
      case '\'': res << "&apos;"; break;
      case '"': res << "&quot;"; break;
      case '&': res << "&amp;"; break;
      case '<': res << "&lt;"; break;
      case '>': res << "&gt;"; break;
      default: res << *si; break;
      }
    }    
    
    return res.str();
  }
  
} // namespace Timers