View on GitHub

Notes

reference notes

Message Passing Interface (MPI)

Overview

Evolution of MPI

Current Adaptability

How MPI Works

General Program Structure of MPI

Communicators and Group

Rank

MPI APIs

Examples

Example in C/C++

#include "mpi.h"
#include <stdio.h>

int main(int argc, char** argv)
{
    int rank, numtasks, rc, len;
    char hostname[MPI_MAX_PROCESSOR_NAME];

    rc = MPI_Init(&argc, &argv);
    if (rc != MPI_SUCCESS) {
        printf("Error.\n");
        MPI_Abort(MPI_COMM_WORLD, rc);
    }

    MPI_Comm_size(MPI_COMM_WORLD, &numtasks);
    MPI_Comm_rank(MPI_COMM_WORLD, &rank);
    MPI_Get_processor_name(hostname, &len);

    printf("Number of tasks: %d My rank: %d on host %s\n", numtasks, rank, hostname);

    MPI_Finalize();
    return 0;
}

Example 2:

#include <mpi.h>
#include <stdio.h>
#include <unistd.h>

int main(int argc, char** argv)
{
    int rank;
    char hostname[256];

    MPI_Init(&argc, &argv);
    MPI_Comm_rank(MPI_COMM_WORLD, &rank);
    MPI_Get_processor_name(hostname, 255);

    printf("Hello world! I am process number: %d on host %s\n", rank, hostname);

    MPI_Finalize();
    return 0;
}

Output Example

hpc-login3: ls -l slurm-*.out
-rw------- 1 ttrojan rds 0 Mar 22 11:00 slurm-12240.out

hpc-login3: more slurm-12240.out
Running on 8 processors: hpc
Running ./mpitest
Hello world! I am process number: 0 on host hpc0632
Hello world! I am process number: 1 on host hpc0632
Hello world! I am process number: 2 on host hpc0632
Hello world! I am process number: 3 on host hpc0632
Hello world! I am process number: 4 on host hpc0632
Hello world! I am process number: 5 on host hpc0632
Hello world! I am process number: 6 on host hpc0632
Hello world! I am process number: 7 on host hpc0632
Done Thu Mar 22 11:00:06 PDT 2018

MPI_Init

MPI_Init (&argc, &argv)

MPI_Comm_size

MPI_Comm_size(comm, &size)

MPI_Abort

MPI_Get_processor_name

MPI_Get_processor_name(&name, &resultlength)

MPI_Get_version

MPI_Get_version(&version, &subversion)

MPI_Initialized

MPI_Initialized(&flag)

MPI_Wtime & MPI_Wtick

MPI_Wtime()
MPI_Wtick()

Example

#include "mpi.h"
#include <windows.h>
#include <stdio.h>

int main(int argc, char *argv[])
{
    double t1, t2;
    MPI_Init(0, 0);
    t1 = MPI_Wtime();
    Sleep(1000);
    t2 = MPI_Wtime();
    printf("MPI_Wtime measured a 1 second sleep to be: %1.2f\n", t2 - t1);
    fflush(stdout);
    MPI_Finalize();
    return 0;
}

MPI_Finalize

MPI_Finalize()

Point-to-Point

Operations

Point-to-Point Operations

Types of Send/Receive Routines

Routines

Routines – Arguments

MPI Pre-defined Datatype

Data type Description
MPI_CHAR signed char
MPI_INT signed int
MPI_FLOAT Float
MPI_DOUBLE Double

MPI Send()

MPI_Send(mymin, 2, MPI_INT, 0, MYMIN_MSG, MPI_COMM_WORLD);

MPI Recv()

MPI_Recv(othermin, 2, MPI_INT, i, MYMIN_MSG, MPI_COMM_WORLD, &status);

Buffering

Blocking

Order

Fairness

Scope

Collective Communication

Routines

Types of Collective Operations

Collective Operations Routines

MPI_Barrier

MPI_Bcast

MPI_Scatter

MPI_Gather

MPI_Allgather

MPI_Reduce

MPI_Allreduce

MPI_Reduce_scatter

For more details, refer to LLNL MPI Tutorial.