Make file-based SMC storage MPI-safe by guarding save_step on rank 0#50
Draft
Copilot wants to merge 2 commits into
Draft
Make file-based SMC storage MPI-safe by guarding save_step on rank 0#50Copilot wants to merge 2 commits into
Copilot wants to merge 2 commits into
Conversation
Copilot
AI
changed the title
[WIP] Fix SMC storage to be MPI-compatible
Make file-based SMC storage MPI-safe by guarding save_step on rank 0
Jun 16, 2026
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
When running with
mpirunand a non-parallel kernel (e.g.,VectorMCMC),@rank_zero_run_onlyonSamplerBase._save_stepcatches anAttributeError(no_rankon the kernel) and falls back to callingstorage.save_step()on all ranks simultaneously, causing concurrent HDF5/Pickle writes and crashes. The workaround was an awkward rank-conditional context manager split.Changes
smcpy/utils/mpi_utils.py— addsis_mpi_rank_zero(): checksmpi4py.MPI.COMM_WORLD.Get_rank() == 0, returnsTrueif mpi4py is unavailable. Decouples the rank check from the MCMC kernel's_rankattribute.smcpy/utils/storage.py—HDF5Storage.save_stepandPickleStorage.save_stepearly-return whenis_mpi_rank_zero()isFalse. The storage is now independently MPI-safe regardless of kernel type.is_mpi_rank_zero()across no-MPI, rank-0, and rank-N cases; parametrized tests confirmingsave_stepis a no-op on non-zero ranks for both file storage backends.With this change, the desired single-context API works with MPI: