Newer
Older
"""Counts the number of times L{LogManager.tick} is called."""
gather_time = "before"
def __init__(self, name="step"):
LogQuantity.__init__(self, name, "1", "Timesteps")
self.steps = 0
def __call__(self):
result = self.steps
self.steps += 1
return result
class TimestepDuration(PostLogQuantity):
"""Records the CPU time between invocations of
:meth:`LogManager.tick_before` and
:meth:`LogManager.tick_after`.
"""
PostLogQuantity.__init__(self, name, "s", "Time step duration")
def prepare_for_tick(self):
self.last_start = time()
def __call__(self):
now = time()
result = now - self.last_start
del self.last_start
class CPUTime(LogQuantity):
"""Records (monotonically increasing) CPU time."""
def __init__(self, name="t_cpu"):
LogQuantity.__init__(self, name, "s", "Wall time")
self.start = time()
def __call__(self):
return time()-self.start
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
class ETA(LogQuantity):
"""Records an estimate of how long the computation will still take."""
def __init__(self, total_steps, name="t_eta"):
LogQuantity.__init__(self, name, "s", "Estimated remaining duration")
self.steps = 0
self.total_steps = total_steps
self.start = time()
def __call__(self):
fraction_done = self.steps/self.total_steps
self.steps += 1
time_spent = time()-self.start
if fraction_done > 1e-9:
return time_spent/fraction_done-time_spent
else:
return 0
def add_general_quantities(mgr):
"""Add generally applicable L{LogQuantity} objects to C{mgr}."""
mgr.add_quantity(TimestepDuration())
mgr.add_quantity(CPUTime())
mgr.add_quantity(LogUpdateDuration(mgr))
mgr.add_quantity(TimestepCounter())
class SimulationTime(SimulationLogQuantity):
"""Record (monotonically increasing) simulation time."""
def __init__(self, dt, name="t_sim", start=0):
SimulationLogQuantity.__init__(self, dt, name, "s", "Simulation Time")
self.t = 0
def __call__(self):
result = self.t
self.t += self.dt
return result
class Timestep(SimulationLogQuantity):
"""Record the magnitude of the simulated time step."""
def __init__(self, dt, name="dt", unit="s"):
SimulationLogQuantity.__init__(self, dt, name, unit, "Simulation Timestep")
Andreas Klöckner
committed
def __call__(self):
return self.dt
def set_dt(mgr, dt):
"""Set the simulation timestep on L{LogManager} C{mgr} to C{dt}."""
for gd in mgr.after_gather_descriptors:
if isinstance(gd.quantity, DtConsumer):
gd.quantity.set_dt(dt)
Andreas Klöckner
committed
def add_simulation_quantities(mgr, dt=None):
"""Add L{LogQuantity} objects relating to simulation time."""
if dt is not None:
from warnings import warn
warn("Specifying ahead of time is a deprecated practice. "
"Use pytools.log.set_dt() instead.")
Andreas Klöckner
committed
mgr.add_quantity(Timestep(dt))
def add_run_info(mgr):
"""Add generic run metadata, such as command line, host, and time."""
import sys
mgr.set_constant("cmdline", " ".join(sys.argv))
from socket import gethostname
mgr.set_constant("machine", gethostname())
from time import localtime, strftime, time
mgr.set_constant("date", strftime("%a, %d %b %Y %H:%M:%S %Z", localtime()))