profilingPstream.H
Go to the documentation of this file.
1 /*---------------------------------------------------------------------------*\
2  ========= |
3  \\ / F ield | OpenFOAM: The Open Source CFD Toolbox
4  \\ / O peration |
5  \\ / A nd | www.openfoam.com
6  \\/ M anipulation |
7 -------------------------------------------------------------------------------
8  Copyright (C) 2019-2023 OpenCFD Ltd.
9 -------------------------------------------------------------------------------
10 License
11  This file is part of OpenFOAM.
12 
13  OpenFOAM is free software: you can redistribute it and/or modify it
14  under the terms of the GNU General Public License as published by
15  the Free Software Foundation, either version 3 of the License, or
16  (at your option) any later version.
17 
18  OpenFOAM is distributed in the hope that it will be useful, but WITHOUT
19  ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
20  FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
21  for more details.
22 
23  You should have received a copy of the GNU General Public License
24  along with OpenFOAM. If not, see <http://www.gnu.org/licenses/>.
25 
26 Class
27  Foam::profilingPstream
28 
29 Description
30  Timers and values for simple (simplistic) mpi-profiling.
31  The entire class behaves as a singleton.
32 
33 SourceFiles
34  profilingPstream.C
35 
36 \*---------------------------------------------------------------------------*/
37 
38 #ifndef Foam_profilingPstream_H
39 #define Foam_profilingPstream_H
40 
41 #include "cpuTime.H"
42 #include "FixedList.H"
43 #include <memory>
44 
45 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
46 
47 namespace Foam
48 {
49 
50 /*---------------------------------------------------------------------------*\
51  Class profilingPstream Declaration
52 \*---------------------------------------------------------------------------*/
53 
54 class profilingPstream
55 {
56 public:
57 
58  // Public Types
59 
60  //- The enumerated timing categories (for times and counts arrays)
61  enum timingType : unsigned
62  {
67  GATHER, // gather (or recv)
68  SCATTER, // scatter (or send)
70  WAIT,
71  OTHER,
72  nCategories // Dimensioning size
73  };
74 
75  //- Fixed-size container for timing values
77 
78  //- Fixed-size container for timing counts
80 
81 
82 private:
83 
84  // Private Static Data
85 
86  //- The timer to use
87  static std::unique_ptr<cpuTime> timer_;
88 
89  //- Is timer in a suspend state?
90  static bool suspend_;
91 
92  //- The accumulated values for various timing categories
93  static timingList times_;
94 
95  //- The timing frequency for various timing categories
96  static countList counts_;
97 
98 
99 public:
100 
101  // Static Member Functions
102 
103  // Management
104 
105  //- True if timer is active (ie, enabled and not suspended)
106  static bool active() noexcept { return !suspend_ && timer_; }
107 
108  //- Create timer for measuring communication or un-suspend existing
109  static void enable();
110 
111  //- Remove timer for measuring communication activity.
112  //- Does not affect times/counts.
113  static void disable() noexcept;
114 
115  //- Reset times/counts. Does not affect the timer itself
116  static void reset();
118  //- Suspend use of timer. Return old status
119  static bool suspend() noexcept
120  {
121  bool old(suspend_);
122  suspend_ = bool(timer_);
123  return old;
124  }
125 
126  //- Resume use of timer (if previously active)
127  static void resume() noexcept
128  {
129  suspend_ = false;
130  }
131 
132 
133  // Timing/Counts
134 
135  //- Access to the timing information
136  static timingList& times() noexcept { return times_; }
137 
138  //- Access to the timing counts
139  static countList& counts() noexcept { return counts_; }
140 
141  //- Access to the timing information for given timing category
142  static double times(const timingType idx)
143  {
144  return times_[idx];
145  }
146 
147  //- Access to the count for given timing category
148  static uint64_t counts(const timingType idx)
149  {
150  return counts_[idx];
151  }
152 
153  //- The total of times
154  static double elapsedTime();
155 
156  //- Update timer prior to measurement
157  static void beginTiming()
158  {
159  if (!suspend_ && timer_)
160  {
161  (void) timer_->cpuTimeIncrement();
162  }
163  }
165  //- Add time increment
166  static void addTime(const timingType idx)
167  {
168  if (!suspend_ && timer_)
169  {
170  times_[idx] += timer_->cpuTimeIncrement();
171  ++counts_[idx];
172  }
173  }
174 
175  //- Add time increment to \em broadcast time
176  static void addBroadcastTime()
177  {
178  addTime(timingType::BROADCAST);
179  }
180 
181  //- Add time increment to \em reduce time
182  static void addReduceTime()
183  {
184  addTime(timingType::REDUCE);
185  }
186 
187  //- Add time increment to \em probe time
188  static void addProbeTime()
189  {
190  addTime(timingType::PROBE);
191  }
192 
193  //- Add time increment to \em request time
194  static void addRequestTime()
195  {
196  addTime(timingType::REQUEST);
197  }
198 
199  //- Add time increment to \em wait time
200  static void addWaitTime()
201  {
202  addTime(timingType::WAIT);
203  }
204 
205  //- Add time increment to \em gather time
206  static void addGatherTime()
207  {
208  addTime(timingType::GATHER);
209  }
210 
211  //- Add time increment to \em scatter time
212  static void addScatterTime()
213  {
214  addTime(timingType::SCATTER);
215  }
216 
217  //- Add time increment to \em allToAll time
218  static void addAllToAllTime()
219  {
220  addTime(timingType::ALL_TO_ALL);
221  }
222 
223  //- Add time increment to \em other time
224  static void addOtherTime()
225  {
226  addTime(timingType::OTHER);
227  }
228 
230  // Output
231 
232  //- Report current information. Uses parallel communication!
233  static void report(const int reportLevel = 0);
234 };
235 
236 
237 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
238 
239 } // End namespace Foam
240 
241 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
242 
243 #endif
244 
245 // ************************************************************************* //
static void addProbeTime()
Add time increment to probe time.
timingType
The enumerated timing categories (for times and counts arrays)
A 1D vector of objects of type <T> with a fixed length <N>.
Definition: HashTable.H:107
static void addReduceTime()
Add time increment to reduce time.
static void addWaitTime()
Add time increment to wait time.
Timers and values for simple (simplistic) mpi-profiling. The entire class behaves as a singleton...
static timingList & times() noexcept
Access to the timing information.
static void addOtherTime()
Add time increment to other time.
static void addAllToAllTime()
Add time increment to allToAll time.
static double elapsedTime()
The total of times.
static void addTime(const timingType idx)
Add time increment.
static void addScatterTime()
Add time increment to scatter time.
const direction noexcept
Definition: Scalar.H:258
static countList & counts() noexcept
Access to the timing counts.
static void beginTiming()
Update timer prior to measurement.
static void resume() noexcept
Resume use of timer (if previously active)
static void addRequestTime()
Add time increment to request time.
static void addBroadcastTime()
Add time increment to broadcast time.
static void reset()
Reset times/counts. Does not affect the timer itself.
static void disable() noexcept
Remove timer for measuring communication activity. Does not affect times/counts.
static bool suspend() noexcept
Suspend use of timer. Return old status.
static bool active() noexcept
True if timer is active (ie, enabled and not suspended)
FixedList< double, timingType::nCategories > timingList
Fixed-size container for timing values.
static void addGatherTime()
Add time increment to gather time.
FixedList< uint64_t, timingType::nCategories > countList
Fixed-size container for timing counts.
static void report(const int reportLevel=0)
Report current information. Uses parallel communication!
static void enable()
Create timer for measuring communication or un-suspend existing.
Namespace for OpenFOAM.