Skip to content

Instantly share code, notes, and snippets.

@jacquerie
Created November 19, 2013 16:26
Show Gist options
  • Save jacquerie/7548078 to your computer and use it in GitHub Desktop.
Save jacquerie/7548078 to your computer and use it in GitHub Desktop.
#include <mpi.h>
#include <stdio.h>
int main (int argc, char** argv) {
int i, rank, size, left, right;
double A, B, SUM;
MPI_Status status;
MPI_Init(&argc, &argv);
MPI_Comm_rank(MPI_COMM_WORLD, &rank);
MPI_Comm_size(MPI_COMM_WORLD, &size);
A = rank;
left = (rank == 0) ? size - 1 : rank - 1;
right = (rank == size - 1) ? 0 : rank + 1;
for (i = 0; i < size; i++) {
MPI_Sendrecv(&A, 1, MPI_DOUBLE, right, 0, &B, 1, MPI_DOUBLE, left, 0, MPI_COMM_WORLD, &status);
SUM += B;
A = B;
}
printf("I am proc %d and SUM = %.2f\n", rank, SUM);
MPI_Finalize();
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment