Kahibaro
Discord Login Register

MPI processes

Understanding MPI Processes

In MPI, processes are the fundamental units of execution. Everything in MPI revolves around how these processes are created, identified, and used to cooperate.

This chapter focuses on:

What Is an MPI Process?

An MPI process is:

Crucially:

This is fundamentally different from shared-memory threading models (like OpenMP) where multiple threads share a single address space.

The SPMD Model

Most MPI programs follow the Single Program, Multiple Data (SPMD) pattern:

Conceptually:

Starting MPI Processes

You do not call fork() or create MPI processes yourself. Instead:

Typical usage:

mpirun -np 4 ./my_mpi_program

or with a scheduler:

srun -n 4 ./my_mpi_program

Here:

Process Ranks and `MPI_COMM_WORLD`

Every MPI process is given an integer identifier called a rank within a communicator.

The default communicator that includes all processes in the MPI job is:

Within MPI_COMM_WORLD:

Two fundamental calls:

Minimal MPI skeleton (C-like pseudocode):

#include <mpi.h>
int main(int argc, char *argv[])
{
    MPI_Init(&argc, &argv);  // Start MPI
    int size, rank;
    MPI_Comm_size(MPI_COMM_WORLD, &size); // total # of processes
    MPI_Comm_rank(MPI_COMM_WORLD, &rank); // my process ID (0..size-1)
    // Example: only rank 0 prints a global message
    if (rank == 0) {
        printf("Running with %d MPI processes\n", size);
    }
    // All ranks print their own identity
    printf("Hello from rank %d of %d\n", rank, size);
    MPI_Finalize();  // Cleanly shut down MPI
    return 0;
}

Key ideas:

Process Lifetime and Initialization

For an MPI process, the life cycle is:

  1. The launcher starts the OS process and links it into the MPI job.
  2. The process calls MPI_Init (or MPI_Init_thread).
  3. The process runs MPI and non-MPI code.
  4. The process calls MPI_Finalize.
  5. The OS process exits.

Important points:

MPI Processes and Memory

Each MPI process has its own separate memory:

Example implication:

This separation:

Process Mapping and Placement

How MPI processes are assigned to hardware resources affects performance:

Typical mapping options (launcher-dependent):

Conceptual mapping example:

The logical rank does not have to match the physical core index; mapping is configurable and performance-sensitive but is not controlled by standard MPI calls.

Basic Process-Related MPI Calls

Beyond MPI_Comm_rank and MPI_Comm_size, some common process-related routines include:

These are useful for:

Multiple Communicators and Process Grouping (Overview Only)

Although MPI_COMM_WORLD includes all processes, you can:

At this stage, know only that:

Common Process-Related Patterns

A few typical patterns for working with MPI processes:

Single Root for I/O

Only one process (often rank 0) handles expensive or shared operations, such as:

Pattern:

if (rank == 0) {
    // perform I/O or coordination
}

Data is then broadcast or scattered to other processes using MPI collectives.

Process-Specific Work Decomposition

Processes divide work according to their ranks. For example, for a loop over N elements:

for (int i = rank; i < N; i += size) {
    // each process handles every size-th element
}

or block decomposition:

int chunk = N / size;
int start = rank * chunk;
int end   = (rank == size - 1) ? N : start + chunk;
for (int i = start; i < end; i++) {
    // each process handles a contiguous chunk
}

These patterns exploit the rank to distribute work.

Typical Mistakes Involving MPI Processes

Some frequent errors when dealing with MPI processes:

Summary

Views: 11

Comments

Please login to add a comment.

Don't have an account? Register now!