Skip to content

Commit

Permalink
docs: Simplify implementation
Browse files Browse the repository at this point in the history
  • Loading branch information
georgebisbas committed Apr 15, 2024
1 parent 5ff0adf commit e48ff09
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 20 deletions.
14 changes: 7 additions & 7 deletions devito/logger.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,13 +13,13 @@
stream_handler = logging.StreamHandler()
logger.addHandler(stream_handler)

# Add extra logging levels
DEBUG = logging.DEBUG # value=10
# Add extra logging levels (note: INFO has value=20, WARNING has value=30)
DEBUG = logging.DEBUG
PERF = 19
INFO = logging.INFO # value=20
WARNING = logging.WARNING # value=30
ERROR = logging.ERROR # value=40
CRITICAL = logging.CRITICAL # value=50
INFO = logging.INFO
WARNING = logging.WARNING
ERROR = logging.ERROR
CRITICAL = logging.CRITICAL

logging.addLevelName(PERF, "PERF")

Expand Down Expand Up @@ -77,7 +77,7 @@ def set_log_level(level, comm=None):
"""
from devito import configuration

if comm is not None:
if comm is not None and configuration['mpi']:
if comm.rank != 0:
logger.removeHandler(stream_handler)
logger.addHandler(logging.NullHandler())
Expand Down
9 changes: 4 additions & 5 deletions devito/operator/operator.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
from cached_property import cached_property
from sympy import sympify

from devito import mpi_switch_log
from devito import switch_log_level
from devito.arch import compiler_registry, platform_registry
from devito.data import default_allocator
from devito.exceptions import InvalidOperator, ExecutionError
Expand Down Expand Up @@ -873,12 +873,11 @@ def apply(self, **kwargs):
self._postprocess_arguments(args, **kwargs)

# In case MPI is used restrict result logging to one rank only
if configuration['mpi']:
with mpi_switch_log(log_level='DEBUG', comm=args.comm):
return self._emit_apply_profiling(args)
else:
with switch_log_level(comm=args.comm):
return self._emit_apply_profiling(args)

return self._emit_apply_profiling(args)

# Performance profiling
def _emit_build_profiling(self):
if not is_log_enabled_for('PERF'):
Expand Down
18 changes: 10 additions & 8 deletions devito/parameters.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
from devito.tools import Signer, filter_ordered

__all__ = ['configuration', 'init_configuration', 'print_defaults', 'print_state',
'switchconfig', 'mpi_switch_log']
'switchconfig', 'switch_log_level']

# Be EXTREMELY careful when writing to a Parameters dictionary
# Read here for reference: http://wiki.c2.com/?GlobalVariablesAreBad
Expand Down Expand Up @@ -258,24 +258,26 @@ def wrapper(*args, **kwargs):
return wrapper


class mpi_switch_log(switchconfig):
class switch_log_level(object):
"""
A context manager subclassing `switchconfig` to temporarily change
MPI logging.
"""

def __init__(self, **params):
self.params = {k.replace('_', '-'): v for k, v in params.items()}
self.previous = {}
def __init__(self, comm):

comm = self.params.pop('comm')
self.level = configuration['log-level']
self.comm = comm

# Limit logging to rank 0
set_log_level(self.params['log-level'], comm=comm)
set_log_level(self.level, comm)

def __enter__(self):
return

def __exit__(self, exc_type, exc_val, exc_tb):
# Reinstate logging upon exit
set_log_level(self.previous['log-level'])
set_log_level(self.level)
logger.addHandler(stream_handler)


Expand Down

0 comments on commit e48ff09

Please sign in to comment.