1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130
|
# Copyright (C) 2018 Michal Habera
#
# This file is part of DOLFINx (https://www.fenicsproject.org)
#
# SPDX-License-Identifier: LGPL-3.0-or-later
"""General tools for timing and configuration."""
import functools
import typing
from dolfinx import cpp as _cpp
from dolfinx.cpp.common import (
IndexMap,
git_commit_hash,
has_adios2,
has_complex_ufcx_kernels,
has_debug,
has_kahip,
has_parmetis,
has_petsc,
has_petsc4py,
has_ptscotch,
has_slepc,
ufcx_signature,
)
__all__ = [
"IndexMap",
"Timer",
"timed",
"git_commit_hash",
"has_adios2",
"has_complex_ufcx_kernels",
"has_debug",
"has_kahip",
"has_parmetis",
"has_petsc",
"has_petsc4py",
"has_ptscotch",
"has_slepc",
"ufcx_signature",
]
TimingType = _cpp.common.TimingType
Reduction = _cpp.common.Reduction
def timing(task: str):
return _cpp.common.timing(task)
def list_timings(comm, timing_types: list, reduction=Reduction.max):
"""Print out a summary of all Timer measurements, with a choice of
wall time, system time or user time. When used in parallel, a
reduction is applied across all processes. By default, the maximum
time is shown."""
_cpp.common.list_timings(comm, timing_types, reduction)
class Timer:
"""A timer can be used for timing tasks. The basic usage is::
with Timer(\"Some costly operation\"):
costly_call_1()
costly_call_2()
or::
with Timer() as t:
costly_call_1()
costly_call_2()
print(\"Elapsed time so far: %s\" % t.elapsed()[0])
The timer is started when entering context manager and timing
ends when exiting it. It is also possible to start and stop a
timer explicitly by::
t = Timer(\"Some costly operation\")
t.start()
costly_call()
t.stop()
and retrieve timing data using::
t.elapsed()
Timings are stored globally (if task name is given) and
may be printed using functions ``timing``, ``timings``,
``list_timings``, ``dump_timings_to_xml``, e.g.::
list_timings(comm, [TimingType.wall, TimingType.user])
"""
_cpp_object: _cpp.common.Timer
def __init__(self, name: typing.Optional[str] = None):
self._cpp_object = _cpp.common.Timer(name)
def __enter__(self):
self._cpp_object.start()
return self
def __exit__(self, *args):
self._cpp_object.stop()
def start(self):
self._cpp_object.start()
def stop(self):
return self._cpp_object.stop()
def resume(self):
self._cpp_object.resume()
def elapsed(self):
return self._cpp_object.elapsed()
def timed(task: str):
"""Decorator for timing functions."""
def decorator(func):
@functools.wraps(func)
def wrapper(*args, **kwargs):
with Timer(task):
return func(*args, **kwargs)
return wrapper
return decorator
|