specfem::MPI¶
specfem::MPI is a static class that provides an interface to MPI
functionality within SPECFEM++. It manages MPI initialization and finalization,
and provides static access to the rank and size of the MPI communicator.
-
class MPI¶
Static MPI wrapper for SPECFEM++.
This class provides a static interface to MPI functionality, eliminating the need to pass MPI pointers throughout the codebase.
Key features:
Static rank and size members accessible globally
Context-managed lifecycle (only Context can initialize/finalize)
Safety checks to prevent use outside Context scope
Minimal API focused on essential MPI operations
Usage:
// After Context is initialized int my_rank = specfem::MPI::get_rank(); int world_size = specfem::MPI::get_size(); specfem::MPI::sync();
Note
This class cannot be instantiated. All members are static.
Note
Only specfem::program::Context can initialize/finalize this class.
Public Static Functions
-
static inline void sync()¶
Synchronize all MPI processes (MPI_Barrier)
- Throws:
Calls – MPI_Abort if called by an excluded rank
-
static inline void sync_all()¶
Synchronize all MPI processes (alias for sync())
- Throws:
Exits – with error code 1 if called outside Context scope
-
static inline int get_rank()¶
Get MPI rank.
- Throws:
Exits – with error code 1 if called outside Context scope
- Returns:
int Current MPI rank
-
static inline int get_size()¶
Get MPI world size.
- Throws:
Exits – with error code 1 if called outside Context scope
- Returns:
int Total number of MPI processes
-
static inline MPI_Comm communicator()¶
Get active communicator for simulations.
Returns the active communicator, which may be a subset communicator (if initialized with nprocs) or MPI_COMM_WORLD (default initialization). For excluded ranks in subset initialization, returns MPI_COMM_NULL.
- Throws:
Exits – with error code 1 if called outside Context scope
- Returns:
MPI_Comm The active communicator for simulations
-
static inline bool main_proc()¶
Check if current process is the main process (rank 0)
- Throws:
Exits – with error code 1 if called outside Context scope
- Returns:
bool True if rank == 0
-
static inline bool is_active()¶
Format filename with processor number using dynamic zero-padding.
For multi-process runs, transforms “dir/stem.ext” into “dir/stem/proc_N.ext” where N is the zero-padded rank. For single-process runs, the filename is returned unchanged.
Examples (size=6, rank=2):
”foo/bar.bin” -> “foo/bar/proc_2.bin”
”foo/bar.bin” (size=100, rank=2) -> “foo/bar/proc_02.bin”
”bar.bin” -> “bar/proc_2.bin”
Check if this rank is part of the active communicator
Returns false for ranks excluded by subset initialization (i.e., ranks >= nprocs when initialized with nprocs). Safe to call on any rank without triggering exit.
- Parameters:
filename – Input filename (can include directory path)
- Throws:
Exits – with error code 1 if called outside Context scope
- Returns:
std::string Formatted filename with processor number
- Returns:
bool True if this rank holds a valid communicator