aboutsummaryrefslogtreecommitdiff
path: root/Carpet/Carpet/src/Timers.cc
blob: f783c92de0bee87171d3b41308f562f7e1a4836c (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
#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>
#include <TimerNode.hh>
#include "variables.hh"


namespace Carpet {
  
  using namespace std;

/*********************************************************************
 Timer
 *********************************************************************/

  /// Create a timer with a given name, but do not start it, and do
  /// not associate it with a point in the timer hierarchy.
  Timer::Timer (const string &name_p) : d_name(name_p)
  {
    d_tree = &main_timer_tree;
  }

  Timer::Timer (const string &name_p, TimerTree *tree) : d_name(name_p), d_tree(tree)
  {
  }

  /// Destroy a timer
  Timer::~Timer ()
  {
  }

  /// Insert the timer into the tree of timers as a child of the most
  /// recently started timer that has not been stopped. Don't start
  /// the timer. This routine ensures a timer is created even if it is
  /// never started.
  void Timer::instantiate ()
  {
    TimerNode *current_timer = d_tree->current;
    assert(current_timer);
    current_timer->getChildTimer(name())->instantiate();
  }

  /// Start the timer and insert it into the tree of timers as a child
  /// of the most recently started timer that has not been stopped.
  void Timer::start ()
  {
    TimerNode *current_timer = d_tree->current;
    if (not d_tree->root) return; // do nothing if there is no root
    assert(current_timer);
    current_timer->getChildTimer(name())->start();
  }

  /// Stop the timer - it must be the most recently started timer
  void Timer::stop ()
  {
    TimerNode *current = d_tree->current;
    if (not d_tree->root) return; // do nothing if there is no root
    if (current->getName() != name())
      CCTK_VWarn (0, __LINE__, __FILE__, CCTK_THORNSTRING,
                  "Trying to stop enclosing timer '%s' before enclosed time '%s'",
                  name().c_str(), current->getName().c_str());
    current->stop();
  }

  /// Return the name of the timer
  string Timer::name () const
  {
    return d_name;
  }

  /// Return the current time of the timer as a double
  double Timer::getTime ()
  {
    return d_tree->current->getTime();
  }
} // namespace Carpet