-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #163 from firedrakeproject/timing_prepostproc
Timing object for pre/postproc callbacks
- Loading branch information
Showing
3 changed files
with
71 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,59 @@ | ||
from mpi4py import MPI | ||
|
||
__all__ = ['Timer', 'SolverTimer'] | ||
|
||
|
||
class Timer: | ||
''' | ||
Time multiple similar actions. | ||
''' | ||
def __init__(self): | ||
self.times = [] | ||
|
||
def start_timing(self): | ||
''' | ||
Start timing an action. This should be the last statement before the action starts. | ||
''' | ||
self.times.append(MPI.Wtime()) | ||
|
||
def stop_timing(self): | ||
''' | ||
Stop timing an action. This should be the first statement after the action stops. | ||
''' | ||
etime = MPI.Wtime() | ||
stime = self.times[-1] | ||
self.times[-1] = etime - stime | ||
|
||
def total_time(self): | ||
''' | ||
The total duration of all actions timed. | ||
''' | ||
return sum(self.times) | ||
|
||
def ntimes(self): | ||
''' | ||
The total number of actions timed. | ||
''' | ||
return len(self.times) | ||
|
||
def average_time(self): | ||
''' | ||
The average duration of an action. | ||
''' | ||
return self.total_time()/self.ntimes() | ||
|
||
|
||
class SolverTimer(Timer): | ||
''' | ||
Time multiple solves and print out total/average etc times. | ||
''' | ||
def string(self, timesteps_per_solve=1, ndigits=None): | ||
rnd = lambda x: x if ndigits is None else round(x, ndigits) | ||
total_time = self.total_time() | ||
average_time = self.average_time() | ||
timestep_time = average_time/timesteps_per_solve | ||
string = ''\ | ||
+ f'Total solution time: {rnd(total_time)}\n' \ | ||
+ f'Average solve solution time: {rnd(average_time)}\n' \ | ||
+ f'Average timestep solution time: {rnd(timestep_time)}' | ||
return string |