Skip to content

Instantly share code, notes, and snippets.

@abnvanand
Last active November 12, 2019 17:59
Show Gist options
  • Save abnvanand/6f10029a4335208cbfa371484b982cad to your computer and use it in GitHub Desktop.
Save abnvanand/6f10029a4335208cbfa371484b982cad to your computer and use it in GitHub Desktop.
MPI Snippets

A sequence of collective communications on distinct procs will be matched in the order in which they are executed.

for eg here proc one receives value of x in y and y in x.

#include <bits/stdc++.h>
#include <mpi.h>

using namespace std;


int main() {
    int my_rank;
    int p;
    int x, y;

    MPI_Init(nullptr, nullptr);
    MPI_Comm_rank(MPI_COMM_WORLD, &my_rank);
    MPI_Comm_size(MPI_COMM_WORLD, &p);

    if (my_rank == 0) {
        x = 10, y = 5;
    }

    MPI_Barrier(MPI_COMM_WORLD);

    if (my_rank != 1) {
        MPI_Bcast(&x, 1, MPI_INT, 0, MPI_COMM_WORLD);
        MPI_Bcast(&y, 1, MPI_INT, 0, MPI_COMM_WORLD);
    } else {
        MPI_Bcast(&y, 1, MPI_INT, 0, MPI_COMM_WORLD);
        MPI_Bcast(&x, 1, MPI_INT, 0, MPI_COMM_WORLD);
    }

    cout << "Rank: " << my_rank << " x: " << x << " y: " << y << endl;

    MPI_Finalize();
    return 0;
}

Output:-

Rank: 0 x: 10 y: 5
Rank: 1 x: 5 y: 10
Rank: 2 x: 10 y: 5

It may be tempting to pass the same argument to both operand and result on the root process

eg

/* Illegal call */
MPI_Reduce(&sum, &sum, 1, MPI_INT, MPI_SUM, 0, MPI_COMM_WORLD);

This is called aliasing of arguments and it is illegal to alias out or in/out arguments in any MPI function

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment