Skip to content

Commit

Permalink
Move ShowProgress.hoc to python
Browse files Browse the repository at this point in the history
  • Loading branch information
jorblancoa committed Feb 22, 2024
1 parent 1d49ad5 commit 086a67c
Show file tree
Hide file tree
Showing 4 changed files with 39 additions and 58 deletions.
50 changes: 0 additions & 50 deletions core/hoc/ShowProgress.hoc

This file was deleted.

4 changes: 2 additions & 2 deletions docs/api/neurodamus.core.rst
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ neurodamus.core package
.. rubric:: Decorators

.. autosummary::
return_neuron_timings
return_neuron_timings_and_progress
mpi_no_errors
run_only_rank0

Expand Down Expand Up @@ -67,7 +67,7 @@ Module API

**Decorators**

.. autofunction:: return_neuron_timings
.. autofunction:: return_neuron_timings_and_progress

.. autofunction:: mpi_no_errors

Expand Down
37 changes: 35 additions & 2 deletions neurodamus/core/_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
from inspect import Signature, signature
from ._mpi import MPI
from ..utils import progressbar
from . import NeurodamusCore as Nd


class ProgressBarRank0(progressbar.Progress):
Expand Down Expand Up @@ -67,8 +68,39 @@ def rank0_wrapper(*args, **kw):
return rank0_wrapper


def return_neuron_timings(f):
"""Decorator to collect and return timings on a neuron run
class SimulationProgress:
def __init__(self):
"""
Utility class which will set up a timer to perioducally check the amount of time lapsed
in the simulation compared to the final tstop value. This is converted into a percentage
of the job complete which is then printed to the console.
"""
self.last_time_check = time.time()
self.sim_start = self.last_time_check
self.update_progress()

@run_only_rank0
def update_progress(self):
"""
Callback function that refreshes the progress value (if enough time has elapsed) and then
inserts the next call into the event queue.
"""
current_time = time.time()
sim_t = Nd.t
sim_tstop = Nd.tstop
if (current_time - self.last_time_check > 0.75) and (sim_t > 0):
self.last_time_check = current_time
sec_remain = (self.last_time_check - self.sim_start) * (sim_tstop / sim_t - 1)
hours = sec_remain / 3600
minutes = (sec_remain % 3600) / 60
seconds = (sec_remain % 3600) % 60
print(f"\r[t={sim_t:5.2f}] Completed {sim_t*100/sim_tstop:2.0f}% "
f"ETA: {hours:02.0f}:{minutes:02.0f}:{seconds:02.0f} ", end='', flush=True)
Nd.cvode.event(sim_t + 1, self.update_progress)


def return_neuron_timings_and_progress(f):
"""Decorator to collect, return timings and show the progress on a neuron run
"""
@wraps(f)
def timings_wrapper(*args, **kw):
Expand All @@ -78,6 +110,7 @@ def timings_wrapper(*args, **kw):
pc = MPI.pc
wait_base = pc.wait_time()

SimulationProgress()
f(*args, **kw) # Discard return values

tdat[0] = pc.wait_time() - wait_base
Expand Down
6 changes: 2 additions & 4 deletions neurodamus/node.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
from collections import namedtuple, defaultdict
from contextlib import contextmanager

from .core import MPI, mpi_no_errors, return_neuron_timings, run_only_rank0
from .core import MPI, mpi_no_errors, return_neuron_timings_and_progress, run_only_rank0
from .core import NeurodamusCore as Nd
from .core.configuration import CircuitConfig, Feature, GlobalConfig, SimConfig
from .core._engine import EngineBase
Expand Down Expand Up @@ -1342,10 +1342,8 @@ def run_all(self):
return timings

# -
@return_neuron_timings
@return_neuron_timings_and_progress
def _run_neuron(self):
Nd.load_hoc("ShowProgress") # TODO: Drop this
_ = Nd.ShowProgress(Nd.cvode, MPI.rank)
self.solve()
logging.info("Simulation finished.")

Expand Down

0 comments on commit 086a67c

Please sign in to comment.