Skip to content

Instantly share code, notes, and snippets.

@aprell
Created December 2, 2011 16:12
Show Gist options
  • Save aprell/1423787 to your computer and use it in GitHub Desktop.
Save aprell/1423787 to your computer and use it in GitHub Desktop.
Blocking send and receive
#include <stdio.h>
#include <stdlib.h>
#include <mpi.h>
#define WORKER(id) if (ID == (id))
#define MASTER WORKER(0)
int main(int argc, char *argv[])
{
int numprocs, ID;
int next, prev;
int buf, i;
MPI_Init(&argc, &argv);
MPI_Comm_size(MPI_COMM_WORLD, &numprocs);
MPI_Comm_rank(MPI_COMM_WORLD, &ID);
next = (ID + 1) % numprocs;
prev = (ID - 1 + numprocs) % numprocs;
for (i = 0; i < 2; i++) {
if ((ID + i) % 2 == 0) {
MPI_Send(&ID, 1, MPI_INT, next, 0, MPI_COMM_WORLD);
//MPI_Send(&ID, sizeof(ID), MPI_BYTE, next, 0, MPI_COMM_WORLD);
} else {
MPI_Recv(&buf, 1, MPI_INT, prev, 0, MPI_COMM_WORLD, NULL);
//MPI_Recv(&buf, sizeof(buf), MPI_BYTE, prev, 0, MPI_COMM_WORLD, NULL);
}
}
printf("Worker %d: %d\n", ID, buf);
MPI_Finalize();
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment