Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Improve deletion of timer #176

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 8 additions & 3 deletions src/dxtb/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@
"""

# import timer first to get correct total time
from dxtb._src.timing import timer
from dxtb._src.timing import timer, kill_timer

timer.start("Import")
timer.start("PyTorch", parent_uid="Import")
Expand Down Expand Up @@ -54,20 +54,25 @@

###############################################################################

# stop timers and remove from global namespace
# stop timers and remove PyTorch from global namespace for cleaner API
del torch
timer.stop("dxtb")
timer.stop("Import")

###############################################################################

__all__ = [
"calculators",
"components",
#
"calculators",
"Calculator",
"GFN1_XTB",
"GFN2_XTB",
#
"IndexHelper",
#
"kill_timer",
"timer",
#
"__version__",
]
71 changes: 67 additions & 4 deletions src/dxtb/_src/timing/timer.py
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@

import time

__all__ = ["timer"]
__all__ = ["timer", "create_timer", "kill_timer"]


class TimerError(Exception):
Expand Down Expand Up @@ -188,8 +188,8 @@
) -> None:
self.label = label
self.timers = {}
self._enabled = True
self._subtimer_parent_map = {}
self._enabled: bool = True
self._subtimer_parent_map: dict[str, str] = {}
self._autostart = autostart
self._cuda_sync = cuda_sync
self._only_parents = only_parents
Expand Down Expand Up @@ -368,6 +368,9 @@
self.reset()
self.stop_all()

self.timers.clear()
self._subtimer_parent_map.clear()

Check warning on line 372 in src/dxtb/_src/timing/timer.py

View check run for this annotation

Codecov / codecov/patch

src/dxtb/_src/timing/timer.py#L371-L372

Added lines #L371 - L372 were not covered by tests

def get_time(self, uid: str) -> float:
"""
Get the elapsed time of a timer.
Expand Down Expand Up @@ -463,6 +466,66 @@
precision=precision,
)

def __str__(self) -> str:
"""Return a string representation of the :class:`._Timers` instance."""
timers_repr = ", ".join(
f"'{label}': {timer.elapsed_time:.3f}s"
for label, timer in self.timers.items()
)

return (

Check warning on line 476 in src/dxtb/_src/timing/timer.py

View check run for this annotation

Codecov / codecov/patch

src/dxtb/_src/timing/timer.py#L476

Added line #L476 was not covered by tests
f"{self.__class__.__name__}("
f"label={self.label!r}, "
f"enabled={self._enabled}, "
f"cuda_sync={self._cuda_sync}, "
f"only_parents={self._only_parents}, "
f"timers={{{timers_repr}}}"
f")"
)

def __repr__(self) -> str:
"""Return a string representation of the :class:`._Timers` instance."""
return str(self)

Check warning on line 488 in src/dxtb/_src/timing/timer.py

View check run for this annotation

Codecov / codecov/patch

src/dxtb/_src/timing/timer.py#L488

Added line #L488 was not covered by tests


def create_timer(autostart: bool = True, cuda_sync: bool = False) -> _Timers:
"""
Create a new timer instance.

Parameters
----------
autostart : bool, optional
Whether to start the total timer automatically. Defaults to ``True``.
cuda_sync : bool, optional
Whether to call :func:`torch.cuda.synchronize` after CUDA operations.
Defaults to ``False``.

Returns
-------
_Timers
Instance of the timer class.

Note
----
Delete the timer instance with :func:`.kill_timer` when it is no longer
needed or throws errors when reusing it.
"""
global timer
timer = _Timers(autostart=autostart, cuda_sync=cuda_sync)
return timer


def kill_timer() -> None:
"""Delete the global timer instance."""
global timer
if "timer" not in globals():
raise TimerError(

Check warning on line 522 in src/dxtb/_src/timing/timer.py

View check run for this annotation

Codecov / codecov/patch

src/dxtb/_src/timing/timer.py#L522

Added line #L522 was not covered by tests
"Cannot delete timer instance; timer was never initialized."
)

timer.kill()
del timer

Check warning on line 527 in src/dxtb/_src/timing/timer.py

View check run for this annotation

Codecov / codecov/patch

src/dxtb/_src/timing/timer.py#L526-L527

Added lines #L526 - L527 were not covered by tests
Dismissed Show dismissed Hide dismissed


timer = _Timers(autostart=True, cuda_sync=False)
timer = create_timer(autostart=True, cuda_sync=False)
"""Global instance of the timer class."""
Loading