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