CEPS  24.01
Cardiac ElectroPhysiology Simulator
CepsProfiler.cpp
Go to the documentation of this file.
1 /*~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
2  This file is part of CEPS.
3 
4  CEPS is free software: you can redistribute it and/or modify
5  it under the terms of the GNU General Public License as published by
6  the Free Software Foundation, either version 3 of the License, or
7  (at your option) any later version.
8 
9  CEPS is distributed in the hope that it will be useful,
10  but WITHOUT ANY WARRANTY; without even the implied warranty of
11  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12  GNU General Public License for more details.
13 
14  You should have received a copy of the GNU General Public License
15  along with CEPS (see file LICENSE at root of project).
16  If not, see <https://www.gnu.org/licenses/>.
17 
18 
19  Copyright 2019-2024 Inria, Universite de Bordeaux
20 
21  Authors, in alphabetical order:
22 
23  Pierre-Elliott BECUE, Florian CARO, Yves COUDIERE(*), Andjela DAVIDOVIC,
24  Charlie DOUANLA-LONTSI, Marc FUENTES, Mehdi JUHOOR, Michael LEGUEBE(*),
25  Pauline MIGERDITICHAN, Valentin PANNETIER(*), Nejib ZEMZEMI.
26  * : currently active authors
27 
28 ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~*/
32 
38 
39 #include <regex>
40 
42  m_maxLen(0)
43 {
44 }
45 
47 {
48 }
49 
50 void
52 {
53  if (not m_tmpTimes.contains(key))
54  {
55  m_tmpTimes.insert(std::make_pair(key,clock()));
56  m_seconds .insert(std::make_pair(key,0.e0));
57  m_dsplStrs.insert(std::make_pair(key,displayString));
58  m_maxLen = std::max(m_maxLen,CepsUInt(displayString.length()));
59  }
60  else
61  m_tmpTimes.at(key) = clock();
62  return;
63 }
64 
65 void
67 {
68  m_tmpTimes.at(key) = clock();
69  m_seconds .at(key) = 0.e0;
70  return;
71 }
72 
73 void
75 {
76  clock_t now = clock();
77  m_seconds.at(key) += double(now - m_tmpTimes.at(key)) / (double) CLOCKS_PER_SEC;
78  return;
79 }
80 
81 void
82 Profiler::display(const CepsString& key, std::ostream& os) const
83 {
84  if (ceps::isMaster())
85  os << "# Time spent " << std::left << std::setw(m_maxLen+2) << m_dsplStrs.at(key) + ": "
86  << std::setw(10) << m_seconds.at(key) << " seconds" << std::right << std::endl;
87  return;
88 }
89 
90 void
91 Profiler::display(const CepsString& key) const
92 {
93  display(key,std::cout);
94  return;
95 }
96 
97 void
99 {
100  display(std::cout);
101  return;
102 }
103 
104 void
105 Profiler::display(std::ostream& os) const
106 {
107  // Sort by time spent for better identification of costly tasks
110 
111  auto perm = ceps::getSortPermutation(seconds,std::greater<CepsReal>());
112  ceps::applyPermutation(labels,perm);
113 
114  for (auto label : labels)
115  display(label,os);
116  return;
117 }
118 
119 void
121 {
122  size_t l = localMemoryUsage ();
123  size_t g = globalMemoryUsage ();
124  if (ceps::isMaster())
125  ceps::profilingLog () << xtic << " " << l << " " << g << std::endl;
126  return;
127 }
128 
129 void
131 {
132  if (ifexpr)
133  this->logMemoryUsage (xtic);
134  return;
135 }
136 
137 size_t
139 {
140  #ifndef LINUX
141  // not implemented on other systems than linux
142  return 0;
143  #else
144  std::ifstream fichier ("/proc/self/status");
145  size_t size = 0;
146  for (CepsString line; getline (fichier, line);)
147  {
148  std::regex const pattern {".*VmSize:\\s+(\\d+)\\s*kB.*"};
149  std::smatch match;
150  if (std::regex_match (line, match, pattern))
151  size = std::stoi (match[1]);
152  }
153  return size;
154  #endif
155 }
156 
157 size_t
159 {
160  CepsUInt globalMemorySize = 0;
161  CepsUInt localMemorySize = static_cast<CepsUInt> (localMemoryUsage());
162  ceps::barrier();
163  MPI_Allreduce(&localMemorySize,&globalMemorySize,1,CEPS_MPI_UINT,MPI_SUM,ceps::getCommunicator());
164  return static_cast<size_t>(globalMemorySize);
165 }
Timer class, measure execution time.
std::basic_string< CepsChar > CepsString
C++ format string.
Definition: CepsTypes.hpp:128
std::vector< _Type, _Alloc > CepsVector
C++ vector.
Definition: CepsTypes.hpp:155
bool CepsBool
Booleans.
Definition: CepsTypes.hpp:124
std::make_unsigned_t< CepsInt > CepsUInt
Unsigned version on CepsInt.
Definition: CepsTypes.hpp:109
#define CEPS_MPI_UINT
Definition: CepsTypes.hpp:325
void stop(CepsString lbl)
Stops the measure of a labeled chronometer.
CepsMap< CepsString, clock_t > m_tmpTimes
Temp evals for times.
size_t localMemoryUsage()
Returns an estimation of the memory taken by the current process (in kB)
~Profiler()
Destructor.
CepsMap< CepsString, CepsReal > m_seconds
Times.
Profiler()
Constructor.
void start(CepsString lbl, CepsString dspl="")
Creates or continue a labeled chronometer.
CepsUInt m_maxLen
For nice output.
CepsMap< CepsString, CepsString > m_dsplStrs
Text to display at the end of computation, typically.
void logMemoryUsage(const CepsString &xtic)
Writes a line with memory usage in profiling log, xtic is used to describe what the program is curren...
void display() const
Prints time spent by all chronometers.
size_t globalMemoryUsage()
Returns an estimation of the memory taken by all the MPI process (in kB)
void restart(CepsString lbl)
Resets a labeled chronometer.
void logMemoryUsageIf(CepsBool ifexpr, const CepsString &xtic)
Writes a line with memory usage in profiling log if 'ifexpr' is true, xtic is used to describe what t...
void applyPermutation(CepsVector< _Type, _Alloc > &vec, const CepsVector< CepsUInt > &p)
Apply a permutation to a vector.
Definition: CepsVector.hpp:278
MPI_Comm getCommunicator()
Get the communicator.
CepsVector< CepsUInt > getSortPermutation(const CepsVector< _Type, _Alloc > &vec, _Compare compare)
Get the permutation that would be applied to sort the whole vector with specific comparator.
Definition: CepsVector.hpp:254
const _Type & max(const CepsVector< _Type, _Alloc > &vec)
Returns the maximum of the vector, const version.
Definition: CepsVector.hpp:464
void barrier()
Explicit barrier: wait for all processors before continuing.
CepsVector< _Tp > valuesOf(const CepsMap< _Key, _Tp, _Comp, _Alloc > &m)
Get the values of a map as a vector.
Definition: CepsMap.hpp:57
CepsBool isMaster()
Is calling process the master ?
std::ofstream & profilingLog()
Get the ProfilingLog used in Ceps.
Definition: CepsFlags.cpp:251
CepsVector< _Key > keysOf(const CepsMap< _Key, _Tp, _Comp, _Alloc > &m)
Get the keys of a map as a vector.
Definition: CepsMap.hpp:45