Skip to content

Commit 8aebd9c

Browse files
authored
Merge pull request dealii#18878 from gassmoeller/track_laps_in_timer
Add the functionality to count laps to the timer class
2 parents 13dafab + e652ed2 commit 8aebd9c

File tree

4 files changed

+71
-1
lines changed

4 files changed

+71
-1
lines changed

include/deal.II/base/timer.h

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -258,6 +258,15 @@ class Timer
258258
double
259259
last_cpu_time() const;
260260

261+
/**
262+
* Return the number of laps that have been timed by calling
263+
* stop() since creation of the timer or the last
264+
* call to reset(). If a timer is currently running
265+
* the current lap is included in the count.
266+
*/
267+
unsigned int
268+
n_laps() const;
269+
261270
private:
262271
/**
263272
* The Timer class stores timing information for two different clocks: a
@@ -370,6 +379,13 @@ class Timer
370379
* number of MPI processes in the MPI_Comm for the total run time.
371380
*/
372381
Utilities::MPI::MinMaxAvg accumulated_wall_time_data;
382+
383+
/**
384+
* The number of laps that have been timed. If
385+
* the timer is currently running
386+
* the current lap is included in the count.
387+
*/
388+
unsigned int n_timed_laps;
373389
};
374390

375391

source/base/timer.cc

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -175,6 +175,9 @@ Timer::Timer(const MPI_Comm mpi_communicator, const bool sync_lap_times_)
175175
void
176176
Timer::start()
177177
{
178+
if (running == false)
179+
++n_timed_laps;
180+
178181
running = true;
179182
#ifdef DEAL_II_WITH_MPI
180183
if (sync_lap_times)
@@ -287,13 +290,22 @@ Timer::reset()
287290
{
288291
wall_times.reset();
289292
cpu_times.reset();
290-
running = false;
293+
running = false;
294+
n_timed_laps = 0;
291295
internal::TimerImplementation::clear_timing_data(last_lap_wall_time_data);
292296
internal::TimerImplementation::clear_timing_data(accumulated_wall_time_data);
293297
}
294298

295299

296300

301+
unsigned int
302+
Timer::n_laps() const
303+
{
304+
return n_timed_laps;
305+
}
306+
307+
308+
297309
/* ---------------------------- TimerOutput -------------------------- */
298310

299311
TimerOutput::TimerOutput(std::ostream &stream,

tests/base/timer_03.cc

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
// ------------------------------------------------------------------------
2+
//
3+
// SPDX-License-Identifier: LGPL-2.1-or-later
4+
// Copyright (C) 2017 - 2018 by the deal.II authors
5+
//
6+
// This file is part of the deal.II library.
7+
//
8+
// Part of the source code is dual licensed under Apache-2.0 WITH
9+
// LLVM-exception OR LGPL-2.1-or-later. Detailed license information
10+
// governing the source code and code contributions can be found in
11+
// LICENSE.md and CONTRIBUTING.md at the top level directory of deal.II.
12+
//
13+
// ------------------------------------------------------------------------
14+
15+
// the same as timer_02.cc but test the function n_laps().
16+
17+
#include <deal.II/base/timer.h>
18+
19+
#include "../tests.h"
20+
21+
22+
int
23+
main()
24+
{
25+
initlog();
26+
27+
Timer t;
28+
29+
AssertThrow(t.n_laps() == 1, ExcInternalError());
30+
t.stop();
31+
AssertThrow(t.n_laps() == 1, ExcInternalError());
32+
t.start();
33+
AssertThrow(t.n_laps() == 2, ExcInternalError());
34+
t.stop();
35+
AssertThrow(t.n_laps() == 2, ExcInternalError());
36+
t.reset();
37+
AssertThrow(t.n_laps() == 0, ExcInternalError());
38+
39+
deallog << "OK" << std::endl;
40+
}

tests/base/timer_03.output

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
2+
DEAL::OK

0 commit comments

Comments
 (0)