NEST main@caf0ae8
 
Loading...
Searching...
No Matches
stopwatch_impl.h
Go to the documentation of this file.
1/*
2 * stopwatch_impl.h
3 *
4 * This file is part of NEST.
5 *
6 * Copyright (C) 2004 The NEST Initiative
7 *
8 * NEST is free software: you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License as published by
10 * the Free Software Foundation, either version 2 of the License, or
11 * (at your option) any later version.
12 *
13 * NEST is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU General Public License for more details.
17 *
18 * You should have received a copy of the GNU General Public License
19 * along with NEST. If not, see <http://www.gnu.org/licenses/>.
20 *
21 */
22
23#ifndef STOPWATCH_IMPL_H
24#define STOPWATCH_IMPL_H
25
26#include "kernel_manager.h"
27#include "stopwatch.h"
28
29namespace nest
30{
31
32/* In each of the following methods compile-time-evaluated ifs decide which code is actually included. */
33
34template < StopwatchGranularity detailed_timer, StopwatchParallelism threaded_timer >
35void
37{
38 if constexpr ( enable_timer )
39 {
40 if constexpr ( use_timer_array )
41 {
43 walltime_timer_[ kernel().vp_manager.get_thread_id() ].start();
44 cputime_timer_[ kernel().vp_manager.get_thread_id() ].start();
45 }
46 else
47 {
48// This code applies for MasterOnly timers started/stopped from serial context, but also for
49// Threaded timers called from parallel contexts if `-Dwith-threaded-timers=OFF` for backward
50// compatibility with pre-3.9 timers. Therefore, we need to restrict to the master thread here and
51// cannot assert a single-threaded context.
52#pragma omp master
53 {
54 walltime_timer_.start();
55 cputime_timer_.start();
56 }
57 }
58 }
59}
60
61template < StopwatchGranularity detailed_timer, StopwatchParallelism threaded_timer >
62void
64{
65 if constexpr ( enable_timer )
66 {
67 if constexpr ( use_timer_array )
68 {
70 walltime_timer_[ kernel().vp_manager.get_thread_id() ].stop();
71 cputime_timer_[ kernel().vp_manager.get_thread_id() ].stop();
72 }
73 else
74 {
75#pragma omp master
76 {
77 walltime_timer_.stop();
78 cputime_timer_.stop();
79 }
80 }
81 }
82}
83
84template < StopwatchGranularity detailed_timer, StopwatchParallelism threaded_timer >
85bool
87{
88 if constexpr ( enable_timer )
89 {
90 if constexpr ( use_timer_array )
91 {
93 return walltime_timer_[ kernel().vp_manager.get_thread_id() ].is_running_();
94 }
95 else
96 {
97 bool is_running_ = false;
98#pragma omp master
99 {
100 is_running_ = walltime_timer_.is_running_();
101 }
102 return is_running_;
103 }
104 }
105 return false;
106}
107
108template < StopwatchGranularity detailed_timer, StopwatchParallelism threaded_timer >
109double
111{
112 if constexpr ( enable_timer )
113 {
114 if constexpr ( use_timer_array )
115 {
117 return walltime_timer_[ kernel().vp_manager.get_thread_id() ].elapsed( timeunit );
118 }
119 else
120 {
121 double elapsed = 0.;
122#pragma omp master
123 {
124 elapsed = walltime_timer_.elapsed( timeunit );
125 };
126 return elapsed;
127 }
128 }
129 return 0.f;
130}
131
132template < StopwatchGranularity detailed_timer, StopwatchParallelism threaded_timer >
133void
135 timers::timeunit_t timeunit,
136 std::ostream& os ) const
137{
138 if constexpr ( enable_timer )
139 {
140 if constexpr ( use_timer_array )
141 {
143 walltime_timer_[ kernel().vp_manager.get_thread_id() ].print( msg, timeunit, os );
144 }
145 else
146 {
147#pragma omp master
148 {
149 walltime_timer_.print( msg, timeunit, os );
150 }
151 }
152 }
153}
154
155template < StopwatchGranularity detailed_timer, StopwatchParallelism threaded_timer >
156void
158 const std::string& walltime_name,
159 const std::string& cputime_name ) const
160{
161 if constexpr ( enable_timer )
162 {
164 if constexpr ( use_timer_array )
165 {
166 std::vector< double > wall_times( walltime_timer_.size() );
167 std::transform( walltime_timer_.begin(),
168 walltime_timer_.end(),
169 wall_times.begin(),
170 []( const timers::StopwatchTimer< CLOCK_MONOTONIC >& timer ) { return timer.elapsed(); } );
171 d[ walltime_name ] = wall_times;
172
173 std::vector< double > cpu_times( cputime_timer_.size() );
174 std::transform( cputime_timer_.begin(),
175 cputime_timer_.end(),
176 cpu_times.begin(),
177 []( const timers::StopwatchTimer< CLOCK_THREAD_CPUTIME_ID >& timer ) { return timer.elapsed(); } );
178 d[ cputime_name ] = cpu_times;
179 }
180 else
181 {
182 d[ walltime_name ] = walltime_timer_.elapsed();
183 d[ cputime_name ] = cputime_timer_.elapsed();
184 }
185 }
186}
187
188template < StopwatchGranularity detailed_timer, StopwatchParallelism threaded_timer >
189void
191{
192 if constexpr ( enable_timer )
193 {
195 if constexpr ( use_timer_array )
196 {
197 const size_t num_threads = kernel().vp_manager.get_num_threads();
198 walltime_timer_.resize( num_threads );
199 cputime_timer_.resize( num_threads );
200 for ( size_t i = 0; i < num_threads; ++i )
201 {
202 walltime_timer_[ i ].reset();
203 cputime_timer_[ i ].reset();
204 }
205 }
206 else
207 {
208 walltime_timer_.reset();
209 cputime_timer_.reset();
210 }
211 }
212}
213
214} // namespace nest
215
216#endif
Dictionary class for interface to Python and C++ API.
Definition dictionary.h:213
void reset()
Definition stopwatch_impl.h:190
double elapsed(timers::timeunit_t timeunit=timers::timeunit_t::SECONDS) const
Definition stopwatch_impl.h:110
void stop()
Definition stopwatch_impl.h:63
void print(const std::string &msg="", timers::timeunit_t timeunit=timers::timeunit_t::SECONDS, std::ostream &os=std::cout) const
Definition stopwatch_impl.h:134
void get_status(Dictionary &d, const std::string &walltime_name, const std::string &cputime_name) const
Definition stopwatch_impl.h:157
bool is_running_() const
Definition stopwatch_impl.h:86
void start()
Definition stopwatch_impl.h:36
void assert_thread_parallel() const
Fails if NEST is not in thread-parallel section.
Definition vp_manager.h:200
void assert_single_threaded() const
Fails if NEST is in thread-parallel section.
Definition vp_manager.h:192
size_t get_num_threads() const
Get number of threads.
Definition vp_manager.h:186
size_t get_thread_id() const
Gets ID of local thread.
Definition vp_manager.h:176
This class represents a single timer which measures the execution time of a single thread for a given...
Definition stopwatch.h:114
VPManager vp_manager
Definition kernel_manager.h:234
timeunit_t
Definition stopwatch.h:99
Namespace for the NEST simulation kernel.
Definition beta_normalization_factor.h:33
KernelManager & kernel()
Definition kernel_manager.h:311