Skip to content

Commit

Permalink
Merge pull request #73 from zwicker-group/mpi_guard
Browse files Browse the repository at this point in the history
Prevent writing results in sub-nodes of an MPI program
  • Loading branch information
david-zwicker authored Jun 6, 2024
2 parents faf1eb8 + 350ddcb commit e3a26a3
Showing 1 changed file with 15 additions and 6 deletions.
21 changes: 15 additions & 6 deletions modelrunner/model/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,15 @@
from ..storage import ModeType, StorageGroup, open_storage
from .parameters import DeprecatedParameter, HideParameter, Parameterized

# read state of the current MPI node
try:
from mpi4py import MPI
except ImportError:
mpi_rank: int = 0 # no MPI -> current process is main process
else:
mpi_rank = MPI.COMM_WORLD.rank


if TYPE_CHECKING:
from ..run.results import Result # @UnusedImport

Expand Down Expand Up @@ -223,13 +232,13 @@ def run_from_command_line(

# run the model
result = mdl.get_result()
if mdl.output:
# write the results to a file

# write the results
if mdl.output and mpi_rank == 0:
# Write the results to a file if `output` is specified and if we are on the
# root node of an MPI run (or a serial program). The second check is a
# safe-guard against writing data on sub-nodes during an MPI program.
mdl.write_result(result=result)
# else:
# # display the results on stdout
# storage = MemoryStorage()
# result.to_file(storage)

# close the output file
mdl.close()
Expand Down

0 comments on commit e3a26a3

Please sign in to comment.