From 3445323db284bc3c0021ec8d953d7d25630ad632 Mon Sep 17 00:00:00 2001 From: Andreas Kloeckner <kloeckner@teramite.(none)> Date: Sat, 10 Apr 2010 13:15:35 -0500 Subject: [PATCH] Allow sort weight in LogManager quantities to determine order. --- pytools/log.py | 50 ++++++++++++++++++++++++++++++++++++++++++-------- 1 file changed, 42 insertions(+), 8 deletions(-) diff --git a/pytools/log.py b/pytools/log.py index 71b9ebc..7f1022a 100644 --- a/pytools/log.py +++ b/pytools/log.py @@ -23,6 +23,9 @@ def time(): # abstract logging interface -------------------------------------------------- class LogQuantity(object): """A source of loggable scalars.""" + + sort_weight = 0 + def __init__(self, name, unit=None, description=None): self.name = name self.unit = unit @@ -49,6 +52,8 @@ class LogQuantity(object): class PostLogQuantity(LogQuantity): """A source of loggable scalars.""" + sort_weight = 0 + def prepare_for_tick(self): pass @@ -636,9 +641,12 @@ class LogManager(object): gd = _GatherDescriptor(quantity, interval) if isinstance(quantity, PostLogQuantity): - self.after_gather_descriptors.append(gd) + gd_list = self.after_gather_descriptors else: - self.before_gather_descriptors.append(gd) + gd_list = self.before_gather_descriptors + + gd_list.append(gd) + gd_list.sort(key=lambda gd: gd.quantity.sort_weight) if isinstance(quantity, MultiLogQuantity): for name, unit, description, def_agg in zip( @@ -946,8 +954,6 @@ class _SubTimer: class IntervalTimer(LogQuantity): """Records elapsed times.""" - gather_time = False - def __init__(self, name, description=None): LogQuantity.__init__(self, name, "s", description) self.elapsed = 0 @@ -971,7 +977,6 @@ class LogUpdateDuration(LogQuantity): """Records how long the last :meth:`LogManager.tick` invocation took.""" # FIXME this is off by one tick - gather_time = "before" def __init__(self, mgr, name="t_log"): LogQuantity.__init__(self, name, "s", "Time spent updating the log") @@ -1026,8 +1031,6 @@ def time_and_count_function(f, timer, counter=None, increment=1): class TimestepCounter(LogQuantity): """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 @@ -1040,12 +1043,42 @@ class TimestepCounter(LogQuantity): -class TimestepDuration(PostLogQuantity): +class StepToStepDuration(PostLogQuantity): """Records the CPU time between invocations of :meth:`LogManager.tick_before` and :meth:`LogManager.tick_after`. """ + def __init__(self, name="t_2step"): + PostLogQuantity.__init__(self, name, "s", "Step-to-step duration") + self.last_start_time = None + self.last2_start_time = None + + def prepare_for_tick(self): + self.last2_start_time = self.last_start_time + self.last_start_time = time() + + def __call__(self): + if self.last2_start_time is None: + return None + else: + return self.last_start_time - self.last2_start_time + + + + + +class TimestepDuration(PostLogQuantity): + """Records the CPU time between the starts of time steps. + :meth:`LogManager.tick_before` and + :meth:`LogManager.tick_after`. + """ + + # We would like to run last, so that if log gathering takes any + # significant time, we catch that, too. (CUDA sync-on-time-taking, + # I'm looking at you.) + sort_weight = 1000 + def __init__(self, name="t_step"): PostLogQuantity.__init__(self, name, "s", "Time step duration") @@ -1100,6 +1133,7 @@ def add_general_quantities(mgr): """Add generally applicable L{LogQuantity} objects to C{mgr}.""" mgr.add_quantity(TimestepDuration()) + mgr.add_quantity(StepToStepDuration()) mgr.add_quantity(CPUTime()) mgr.add_quantity(LogUpdateDuration(mgr)) mgr.add_quantity(TimestepCounter()) -- GitLab