NimbleSM
NimbleSM is a solid mechanics simulation code for dynamic systems
Loading...
Searching...
No Matches
nimble_timer.h
Go to the documentation of this file.
1/*
2//@HEADER
3// ************************************************************************
4//
5// NimbleSM
6// Copyright 2018
7// National Technology & Engineering Solutions of Sandia, LLC (NTESS)
8//
9// Under the terms of Contract DE-NA0003525 with NTESS, the U.S. Government
10// retains certain rights in this software.
11//
12// Redistribution and use in source and binary forms, with or without
13// modification, are permitted provided that the following conditions are
14// met:
15//
16// 1. Redistributions of source code must retain the above copyright
17// notice, this list of conditions and the following disclaimer.
18//
19// 2. Redistributions in binary form must reproduce the above copyright
20// notice, this list of conditions and the following disclaimer in the
21// documentation and/or other materials provided with the distribution.
22//
23// 3. Neither the name of the Corporation nor the names of the
24// contributors may be used to endorse or promote products derived from
25// this software without specific prior written permission.
26//
27// THIS SOFTWARE IS PROVIDED BY NTESS "AS IS" AND ANY EXPRESS OR IMPLIED
28// WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
29// MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN
30// NO EVENT SHALL NTESS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
31// SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED
32// TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
33// PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
34// LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
35// NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
36// SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
37//
38// Questions? Contact David Littlewood (djlittl@sandia.gov)
39//
40// ************************************************************************
41//@HEADER
42*/
43
44#ifndef NIMBLE_TIMER_H
45#define NIMBLE_TIMER_H
46
47#include <chrono>
48#include <map>
49#include <string>
50
51namespace nimble {
52
54{
55 public:
56 TimeKeeper() : total_time_(0.0) {}
57
58#ifdef NIMBLE_HAVE_VT
59 template <typename ArchiveType>
60 void
61 serialize(ArchiveType& ar)
62 {
63 ar | total_time_;
64 }
65#endif
66
67 inline void
69 {
70 start_time_ = std::chrono::high_resolution_clock::now();
71 }
72
73 inline void
75 {
76 using std::chrono::duration;
77 using std::chrono::duration_cast;
78 end_time_ = std::chrono::high_resolution_clock::now();
79 std::chrono::duration<double> time_increment(0.0);
80 if (end_time_ > start_time_) { time_increment = duration_cast<duration<double>>(end_time_ - start_time_); }
81 total_time_ += time_increment.count();
82 }
83
84 inline double
86 {
87 return total_time_;
88 }
89
90 private:
91 std::chrono::time_point<std::chrono::high_resolution_clock> start_time_;
92 std::chrono::time_point<std::chrono::high_resolution_clock> end_time_;
93 double total_time_;
94};
95
96class Timer
97{
98 public:
99 Timer() = default;
100
101 ~Timer() = default;
102
103#ifdef NIMBLE_HAVE_VT
104 template <typename ArchiveType>
105 void
106 serialize(ArchiveType& ar)
107 {
108 ar | timers_;
109 }
110#endif
111
112 inline void
113 Start(const std::string& name)
114 {
115 TimeKeeper& time_keeper = timers_[name];
116 time_keeper.Start();
117 }
118
119 inline void
120 Stop(const std::string& name)
121 {
122 TimeKeeper& time_keeper = timers_[name];
123 time_keeper.Stop();
124 }
125
126 inline double
127 StopReport(const std::string& name)
128 {
129 double t = 0.0;
130 TimeKeeper& time_keeper = timers_[name];
131 t -= time_keeper.GetElapsedTime();
132 time_keeper.Stop();
133 t += time_keeper.GetElapsedTime();
134 return t;
135 }
136
137 inline double
138 ElapsedTime(const std::string& name) const
139 {
140 return timers_.at(name).GetElapsedTime();
141 }
142
143 std::map<std::string, TimeKeeper> timers_;
144};
145} // namespace nimble
146
147#endif // NIMBLE_TIMER_H
Definition nimble_timer.h:54
double GetElapsedTime() const
Definition nimble_timer.h:85
void Start()
Definition nimble_timer.h:68
TimeKeeper()
Definition nimble_timer.h:56
void Stop()
Definition nimble_timer.h:74
double StopReport(const std::string &name)
Definition nimble_timer.h:127
void Start(const std::string &name)
Definition nimble_timer.h:113
void Stop(const std::string &name)
Definition nimble_timer.h:120
Timer()=default
~Timer()=default
double ElapsedTime(const std::string &name) const
Definition nimble_timer.h:138
std::map< std::string, TimeKeeper > timers_
Definition nimble_timer.h:143
Definition kokkos_contact_manager.h:49