Skip to content

Commit

Permalink
logger fixed
Browse files Browse the repository at this point in the history
  • Loading branch information
EwoutH committed Oct 27, 2023
1 parent e8a11f0 commit 82ad53c
Show file tree
Hide file tree
Showing 3 changed files with 17 additions and 11 deletions.
19 changes: 11 additions & 8 deletions ema_workbench/em_framework/evaluators.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
import sys
import threading
import warnings
import logging

from ema_workbench.em_framework.samplers import AbstractSampler
from .callbacks import DefaultCallback
Expand Down Expand Up @@ -419,10 +420,13 @@ def evaluate_experiments(self, scenarios, policies, callback, combine="factorial
experiment_runner = None


def mpi_initializer(models):
def mpi_initializer(models, logger_level):
global experiment_runner
experiment_runner = ExperimentRunner(models)

# Configure logger based on the passed level and adjusted format
logging.basicConfig(level=logger_level, format="[%(processName)s/%(levelname)s] %(message)s")


class MPIEvaluator(BaseEvaluator):
"""Evaluator for experiments using MPI Pool Executor from mpi4py"""
Expand All @@ -440,7 +444,7 @@ def initialize(self):
models.extend(self._msis)

# Use the initializer function to set up the ExperimentRunner for all the worker processes
self._pool = MPIPoolExecutor(initializer=mpi_initializer, initargs=(models,))
self._pool = MPIPoolExecutor(initializer=mpi_initializer, initargs=(models, _logger.level))
_logger.info(f"MPI pool started with {self._pool._max_workers} workers")
if self._pool._max_workers <= 10:
_logger.warning(
Expand All @@ -460,15 +464,15 @@ def evaluate_experiments(self, scenarios, policies, callback, combine="factorial
packed = [(experiment, experiment.model_name) for experiment in experiments]

# Use the pool to execute in parallel
_logger.debug(
_logger.info(
f"MPIEvaluator: Starting {len(packed)} experiments using MPI pool with {self._pool._max_workers} workers"
)
results = self._pool.map(run_experiment_mpi, packed)

_logger.debug(f"MPIEvaluator: Completed all {len(packed)} experiments")
_logger.info(f"MPIEvaluator: Completed all {len(packed)} experiments")
for experiment, outcomes in results:
callback(experiment, outcomes)
_logger.debug(f"MPIEvaluator: Callback completed for all {len(packed)} experiments")
_logger.info(f"MPIEvaluator: Callback completed for all {len(packed)} experiments")


def run_experiment_mpi(packed_data):
Expand All @@ -477,13 +481,12 @@ def run_experiment_mpi(packed_data):
rank = COMM_WORLD.Get_rank()

experiment, model_name = packed_data
# TODO logger: They don't seem to use the main module logger/syntax, fix this
_logger.debug(f"MPI Rank {rank}: starting {experiment}")
_logger.info(f"MPI Rank {rank}: starting {repr(experiment)}")

# Use the global ExperimentRunner created by the initializer
outcomes = experiment_runner.run_experiment(experiment)

_logger.debug(f"MPI Rank {rank}: completed {experiment}")
_logger.info(f"MPI Rank {rank}: completed {experiment}")

return experiment, outcomes

Expand Down
6 changes: 5 additions & 1 deletion ema_workbench/util/ema_logging.py
Original file line number Diff line number Diff line change
Expand Up @@ -178,7 +178,7 @@ def get_rootlogger():
return _rootlogger


def log_to_stderr(level=None):
def log_to_stderr(level=None, set_root_logger_levels=False):
"""
Turn on logging and add a handler which prints to stderr
Expand Down Expand Up @@ -206,4 +206,8 @@ def log_to_stderr(level=None):
logger.addHandler(handler)
logger.propagate = False

if set_root_logger_levels:
for _, mod_logger in _module_loggers.items():
mod_logger.setLevel(level)

return logger
3 changes: 1 addition & 2 deletions scripts/ema_model.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,7 @@ def some_model(x1=None, x2=None, x3=None):


if __name__ == "__main__":
ema_logging.log_to_stderr(level=10)
# TODO: Test logging to file
ema_logging.log_to_stderr(level=20, set_root_logger_levels=True)

model = Model("simpleModel", function=some_model) # instantiate the model

Expand Down

0 comments on commit 82ad53c

Please sign in to comment.