Nektar++
Timer.cpp
Go to the documentation of this file.
1 ///////////////////////////////////////////////////////////////////////////////
2 //
3 // File: Timer.cpp
4 //
5 // For more information, please see: http://www.nektar.info
6 //
7 // The MIT License
8 //
9 // Copyright (c) 2006 Scientific Computing and Imaging Institute,
10 // University of Utah (USA) and Department of Aeronautics, Imperial
11 // College London (UK).
12 //
13 // Permission is hereby granted, free of charge, to any person obtaining a
14 // copy of this software and associated documentation files (the "Software"),
15 // to deal in the Software without restriction, including without limitation
16 // the rights to use, copy, modify, merge, publish, distribute, sublicense,
17 // and/or sell copies of the Software, and to permit persons to whom the
18 // Software is furnished to do so, subject to the following conditions:
19 //
20 // The above copyright notice and this permission notice shall be included
21 // in all copies or substantial portions of the Software.
22 //
23 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
24 // OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
25 // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
26 // THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
27 // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
28 // FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
29 // DEALINGS IN THE SOFTWARE.
30 //
31 // Description: Time getting class
32 //
33 ///////////////////////////////////////////////////////////////////////////////
34 
37 #include <boost/algorithm/string.hpp>
38 #include <iostream>
39 #include <iomanip>
40 #include <tuple>
41 
42 namespace Nektar
43 {
44 namespace LibUtilities
45 {
46 
48 {
49  m_start = Clock::now();
50 }
51 
53 {
54  m_end = Clock::now();
55 }
56 
58 {
59  return std::chrono::duration_cast<Seconds>(m_end - m_start);
60 }
61 
63 {
64  return Elapsed().count() / static_cast<NekDouble>(n);
65 }
66 
67 void Timer::AccumulateRegion(std::string region, int iolevel)
68 {
69  // search for region
70  auto search = m_elapsedRegion.find(region);
71  if (search == m_elapsedRegion.end())
72  {
73  m_elapsedRegion.insert({region,
74  std::make_tuple<Timer::Seconds, size_t>(this->Elapsed(),1, iolevel)});
75  // update field width
76  m_maxStringWidth = std::max(
77  static_cast<decltype(m_maxStringWidth)>(region.size()),
79  }
80  else
81  {
82  std::get<0>(search->second) += this->Elapsed();
83  std::get<1>(search->second) += 1;
84  }
85 }
86 
88 {
89  std::string def("default");
90  char *argv = new char [def.length()+1];
91  std::strcpy(argv,def.c_str());
94 
95  PrintElapsedRegions(comm);
96 }
97 
99  std::ostream &o,
100  int iolevel)
101 {
102  std::vector<std::string> labels{
103  "Region",
104  "Elapsed time Avg (s)",
105  "Min (s)",
106  "Max (s)",
107  "Count"};
108 
109 
110  if (comm->GetRank() == 0 &&
111  m_elapsedRegion.begin() != m_elapsedRegion.end())
112  {
113  o << "-------------------------------------------\n";
114  o << std::setw(m_maxStringWidth+2) << labels[0] << '\t'
115  << std::setw(10) << labels[1] << '\t'
116  << std::setw(10) << labels[2] << '\t'
117  << std::setw(10) << labels[3] << '\t'
118  << std::setw(10) << labels[4] << '\n';
119  }
120  // first write out execute time
121  auto item = m_elapsedRegion.find("Execute");
122  if(item != m_elapsedRegion.end())
123  {
124  auto elapsedAve = std::get<0>(item->second).count();
125  comm->AllReduce(elapsedAve, LibUtilities::ReduceSum);
126  elapsedAve /= comm->GetSize();
127  auto elapsedMin = std::get<0>(item->second).count();
128  comm->AllReduce(elapsedMin, LibUtilities::ReduceMin);
129  auto elapsedMax = std::get<0>(item->second).count();
130  comm->AllReduce(elapsedMax, LibUtilities::ReduceMax);
131 
132  if (comm->GetRank() == 0)
133  {
134  o << std::setw(m_maxStringWidth+2) << item->first << '\t'
135  << std::setw(10) << elapsedAve << '\t'
136  << std::setw(10) << elapsedMin << '\t'
137  << std::setw(10) << elapsedMax << '\t'
138  << std::setw(10) << std::get<1>(item->second) << '\n';
139  }
140  }
141 
142  // write out all other timings order alphabetically on string
143  for (auto item = m_elapsedRegion.begin();
144  item != m_elapsedRegion.end(); ++item)
145  {
146  if(std::get<2>(item->second) < iolevel)
147  {
148  if(boost::iequals(item->first,"Execute"))
149  {
150  continue;
151  }
152 
153  auto elapsedAve = std::get<0>(item->second).count();
154  comm->AllReduce(elapsedAve, LibUtilities::ReduceSum);
155  elapsedAve /= comm->GetSize();
156  auto elapsedMin = std::get<0>(item->second).count();
157  comm->AllReduce(elapsedMin, LibUtilities::ReduceMin);
158  auto elapsedMax = std::get<0>(item->second).count();
159  comm->AllReduce(elapsedMax, LibUtilities::ReduceMax);
160 
161  if (comm->GetRank() == 0)
162  {
163  o << std::setw(m_maxStringWidth+2) << item->first << '\t'
164  << std::setw(10) << elapsedAve << '\t'
165  << std::setw(10) << elapsedMin << '\t'
166  << std::setw(10) << elapsedMax << '\t'
167  << std::setw(10) << std::get<1>(item->second) << '\n';
168  }
169  }
170  }
171 }
172 // static members init
173 std::map<std::string, std::tuple<Timer::Seconds, size_t, int>>
175 
176 unsigned short Timer::m_maxStringWidth = 10;
177 
178 }
179 } // end Nektar namespace
static void PrintElapsedRegions()
Print elapsed time and call count for each region with default serial communicator.
Definition: Timer.cpp:87
static unsigned short m_maxStringWidth
Definition: Timer.h:91
CounterType m_start
Definition: Timer.h:87
NekDouble TimePerTest(unsigned int n)
Returns amount of seconds per iteration in a test with n iterations.
Definition: Timer.cpp:62
void AccumulateRegion(std::string, int iolevel=0)
Accumulate elapsed time for a region.
Definition: Timer.cpp:67
static std::map< std::string, std::tuple< Seconds, size_t, int > > m_elapsedRegion
Definition: Timer.h:90
std::chrono::duration< NekDouble > Seconds
Definition: Timer.h:57
static std::shared_ptr< DataType > AllocateSharedPtr(const Args &...args)
Allocate a shared pointer from the memory pool.
std::shared_ptr< Comm > CommSharedPtr
Pointer to a Communicator object.
Definition: Comm.h:54
The above copyright notice and this permission notice shall be included.
Definition: CoupledSolver.h:1
double NekDouble