Page MenuHomec4science

base.py
No OneTemporary

File Metadata

Created
Sat, May 4, 12:54
# Copyright (C) 2018 by the RROMPy authors
#
# This file is part of RROMPy.
#
# RROMPy is free software: you can redistribute it and/or modify
# it under the terms of the GNU Lesser General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# RROMPy is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU Lesser General Public License for more details.
#
# You should have received a copy of the GNU Lesser General Public License
# along with RROMPy. If not, see <http://www.gnu.org/licenses/>.
#
from rrompy.utilities.base.verbosity_depth import updateVerbosityCheckpoint
try:
from mpi4py import MPI
_MPI_IS_LOADED = True
COMM, SELF = MPI.COMM_WORLD, MPI.COMM_SELF
except ImportError:
_MPI_IS_LOADED = False
COMM, SELF = None, None
__all__ = ['_MPI_IS_LOADED', 'COMM', 'SELF', 'poolRank', 'poolSize',
'masterCore', 'updateSerialIndex', 'forceSerialExecution',
'allowParallelExecution', 'forcedSerial']
def poolRank() -> int:
if _MPI_IS_LOADED: return COMM.Get_rank()
return 0
def poolSize() -> int:
if _MPI_IS_LOADED: return COMM.Get_size()
return 1
def masterCore() -> bool:
return forcedSerial() or poolRank() == 0
def updateSerialIndex(n : int = 0, delta : bool = True):
global RROMPy_force_serial
if "RROMPy_force_serial" not in globals(): RROMPy_force_serial = 0
if delta: RROMPy_force_serial += n
else: RROMPy_force_serial = n
if RROMPy_force_serial <= 0: del RROMPy_force_serial
def forceSerialExecution(verbosity : bool = False):
updateSerialIndex(1)
if verbosity and poolSize() > 1: updateVerbosityCheckpoint(1)
def allowParallelExecution(verbosity : bool = False):
updateSerialIndex(-1)
if verbosity and poolSize() > 1:
buffer = updateVerbosityCheckpoint(-1)
if buffer is not None:
msg = COMM.gather(buffer, root = 0)
if masterCore():
out = ""
for ms in msg: out += ms
print(out, end = "", flush = True)
def forcedSerial():
global RROMPy_force_serial
return "RROMPy_force_serial" in globals() and RROMPy_force_serial > 0

Event Timeline