Skip to content

Commit 1640e5f

Browse files
committed
Merge remote-tracking branch 'origin/dvm_logging' into dvm-concurrency-amelioration
2 parents c30b732 + f1755f8 commit 1640e5f

1 file changed

Lines changed: 38 additions & 15 deletions

File tree

‎ipsframework/services.py‎

Lines changed: 38 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -162,6 +162,10 @@ def launch(executable: Any, task_name: str, working_dir: str | os.PathLike, *arg
162162
log.error(f'DVM URI file {dvm_uri_file} does not exist')
163163
else:
164164
log.debug(f'Using DVM URI file: {dvm_uri_file}')
165+
if worker.dvm_proc.poll() is not None:
166+
log.error(
167+
f'DVM (prte) exited with code {worker.dvm_proc.returncode}, see {worker.dvm_log_file}'
168+
)
165169

166170
# PMIX_SERVER_URI41 is used by prun to figure out how to talk to the DVM
167171
# It can be defined in `task_env` or in `os.environ`, so we look in
@@ -3218,19 +3222,22 @@ def send_ensemble_instance_to_portal(ensemble_name: str, data_path: Path) -> Non
32183222

32193223

32203224
class DVMPlugin(WorkerPlugin):
3221-
def __init__(self, logger, oversubscribe=False, hwthreads=False):
3225+
def __init__(self, logger, working_dir, oversubscribe=False, hwthreads=False):
32223226
"""
32233227
Dask worker plugin to launch and manage an OpenMPI PRTE DVM on each
32243228
worker node.
32253229
32263230
:param logger: Logger object
3231+
:param working_dir: Directory for the DVM URI and log files; shared by
3232+
all workers, so file names are suffixed with host and PID.
32273233
:param oversubscribe: Whether to allow oversubscription of nodes
32283234
when launching the ensemble runs. Default is False.
32293235
:param hwthreads: Whether to use hardware threads
32303236
"""
32313237
super().__init__()
32323238

32333239
self.logger = logger
3240+
self.working_dir = Path(working_dir)
32343241
self.oversubscribe = oversubscribe
32353242
self.hwthreads = hwthreads
32363243

@@ -3254,7 +3261,11 @@ def setup(self, worker: Worker):
32543261
worker.logger = self.logger
32553262

32563263
self.logger.info('Launching DVM')
3257-
self.worker.dvm_uri_file = f'/tmp/dvm.uri.{os.getpid()}'
3264+
# The working dir is shared by all workers (possibly across nodes), so
3265+
# each worker needs a unique suffix.
3266+
suffix = f'{socket.gethostname()}.{os.getpid()}'
3267+
self.worker.dvm_uri_file = str(self.working_dir / f'dvm.uri.{suffix}')
3268+
Path(self.worker.dvm_uri_file).unlink(missing_ok=True) # never read a stale URI
32583269
# The command to launch the DVM. Note that we use `prte` instead of
32593270
# `mpirun` or `prun`. This is because we want to launch the DVM
32603271
# itself, not a specific application. The DVM will then manage the
@@ -3287,27 +3298,33 @@ def setup(self, worker: Worker):
32873298
# This environment variable is specific to OpenMPI's PRTE
32883299
os.environ['PRTE_MCA_rmaps_default_mapping_policy'] = mapping_policy
32893300

3301+
# prte output goes to a file, not a PIPE: an undrained pipe can fill
3302+
# up and block the DVM, and the file survives for post-mortems.
3303+
self.worker.dvm_log_file = self.working_dir / f'dvm.log.{suffix}'
3304+
self.worker.dvm_log = open(self.worker.dvm_log_file, 'w')
32903305
try:
32913306
self.logger.debug(f'Executing command: {command!s}')
32923307
self.worker.dvm_proc = subprocess.Popen(
3293-
command, stdout=subprocess.PIPE, stderr=subprocess.STDOUT
3308+
command, stdout=self.worker.dvm_log, stderr=subprocess.STDOUT
32943309
)
32953310
except Exception as e:
32963311
print(f'Exception during setting up DVM: {e}', file=sys.stderr)
32973312
console.print(Traceback.from_exception(type(e), e, e.__traceback__))
3313+
raise
32983314

3299-
# If there was an exception, dump any stdout/stderr we have
3300-
if hasattr(self.worker.dvm_proc, 'stdout'):
3301-
print(self.worker.dvm_proc.stdout, file=sys.stderr, flush=True)
3302-
print(self.worker.dvm_proc.stderr, file=sys.stderr, flush=True)
3303-
3304-
# TODO What if there was a subprocess exception?
3305-
3306-
ready = self.worker.dvm_proc.stdout.readline()
3307-
self.logger.info(f'Ready Message : {ready}')
3315+
# Wait for prte to write its URI; fail loudly if it dies or stalls.
3316+
uri_path = Path(self.worker.dvm_uri_file)
3317+
deadline = time.time() + 30
3318+
while not (uri_path.exists() and uri_path.stat().st_size > 0):
3319+
if self.worker.dvm_proc.poll() is not None or time.time() > deadline:
3320+
raise RuntimeError(
3321+
f'DVM failed to start (exit code {self.worker.dvm_proc.poll()}), '
3322+
f'see {self.worker.dvm_log_file}:\n{self.worker.dvm_log_file.read_text()}'
3323+
)
3324+
time.sleep(0.1)
33083325

33093326
with open(self.worker.dvm_uri_file, 'r') as f:
3310-
self.worker.dvm_uri = f.readline()
3327+
self.worker.dvm_uri = f.readline().strip()
33113328
self.logger.debug(f'Read DVM URI: {self.worker.dvm_uri}')
33123329

33133330
os.environ['PMIX_SERVER_URI41'] = self.worker.dvm_uri
@@ -3333,6 +3350,7 @@ def teardown(self, worker: Worker):
33333350
# kill the process directly.
33343351
self.worker.dvm_proc.terminate()
33353352
self.worker.dvm_proc.kill()
3353+
self.worker.dvm_log.close()
33363354

33373355
self.logger.info('DVM shutdown')
33383356

@@ -3652,7 +3670,7 @@ def _make_worker_args(
36523670
# TODO better error handling than just re-raising the exception
36533671
raise
36543672

3655-
dask_nodes = 1 if dask_nodes is None else dask_nodes
3673+
dask_nodes = dask_nodes or 1 # None or 0 (nodes unspecified) -> 1
36563674
self.services.info(
36573675
f'Launching {dask_nodes} Dask workers with {dask_ppw} processes per worker'
36583676
)
@@ -3752,7 +3770,12 @@ def _make_worker_args(
37523770
# the DVM for the workers so that OpenMPI can work properly.
37533771
self.services.debug('Registering DVMPlugin')
37543772
self.dask_client.register_plugin(
3755-
DVMPlugin(logger=services.logger, oversubscribe=oversubscribe, hwthreads=hwthreads)
3773+
DVMPlugin(
3774+
logger=services.logger,
3775+
working_dir=services.get_working_dir(),
3776+
oversubscribe=oversubscribe,
3777+
hwthreads=hwthreads,
3778+
)
37563779
)
37573780
self.services.debug('Registered DVMPlugin')
37583781

0 commit comments

Comments
 (0)