# scope_profiler.mpi_launch

*module*

Detection of whether the process was launched by an MPI launcher.

Importing ``mpi4py.MPI`` calls ``MPI_Init``, and any collective (``bcast``,
``Barrier``, ...) issued afterwards costs something even on a single process.
A plain ``python script.py`` run should therefore never touch MPI at all, even
when mpi4py happens to be installed. This module answers the only question
that decides it: was this process started by ``mpirun``/``mpiexec``/``srun``
(or an equivalent launcher)?

The answer is read from the environment the launcher sets up, so it is
available before mpi4py is imported.

[View source](https://github.com/max-models/scope-profiler/blob/devel/src/scope_profiler/mpi_launch.py#L1-L1)

## scope_profiler.mpi_launch.LAUNCHER_ENV_VARS

*attribute* · *module attribute*

```python
LAUNCHER_ENV_VARS = ('OMPI_COMM_WORLD_RANK', 'PMI_RANK', 'PMIX_RANK', 'MV2_COMM_WORLD_RANK', 'MPI_LOCALRANKID', 'ALPS_APP_PE', 'PALS_RANKID')
```

[View source](https://github.com/max-models/scope-profiler/blob/devel/src/scope_profiler/mpi_launch.py#L24-L32)

## scope_profiler.mpi_launch.get_comm

*function*

```python
def get_comm()
```

Return ``MPI.COMM_WORLD``, or None when MPI must not be used.

The decision is made entirely by :func:`launched_under_mpi`, so a plain
``python script.py`` run never touches MPI. Override it with the
``SCOPE_PROFILER_MPI`` environment variable when a launcher this module
does not recognize is in play.

Returns
-------
mpi4py.MPI.Intracomm or None
    The world communicator, or None if this is not an MPI run or mpi4py
    is unavailable.

[View source](https://github.com/max-models/scope-profiler/blob/devel/src/scope_profiler/mpi_launch.py#L86-L110)

## scope_profiler.mpi_launch.launched_under_mpi

*function*

```python
def launched_under_mpi() -> bool
```

Whether this process was started by an MPI launcher.

Returns
-------
bool
    True if a launcher's per-rank environment variable is present, or if
    the application itself already initialized MPI (in which case using
    the communicator is free). ``SCOPE_PROFILER_MPI=0``/``1`` overrides
    the detection.

[View source](https://github.com/max-models/scope-profiler/blob/devel/src/scope_profiler/mpi_launch.py#L55-L83)
