Skip to content

Instantly share code, notes, and snippets.

@aprell
Created December 6, 2011 10:19
Show Gist options
  • Save aprell/1437672 to your computer and use it in GitHub Desktop.
Save aprell/1437672 to your computer and use it in GitHub Desktop.
One-sided communication
#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 *buf;
MPI_Win win;
MPI_Init(&argc, &argv);
MPI_Comm_size(MPI_COMM_WORLD, &numprocs);
MPI_Comm_rank(MPI_COMM_WORLD, &ID);
MASTER {
MPI_Alloc_mem(sizeof(int), MPI_INFO_NULL, &buf);
*buf = 0;
}
// Collective call
// Expose a window of one integer
MASTER MPI_Win_create(buf, sizeof(int), 1, MPI_INFO_NULL, MPI_COMM_WORLD, &win);
// Expose nothing
else MPI_Win_create(buf, 0, 1, MPI_INFO_NULL, MPI_COMM_WORLD, &win);
// Passive target communication
// Cooperation from target process not needed for communication
MASTER {
MPI_Barrier(MPI_COMM_WORLD);
printf("Master %d: %d\n", ID, *buf);
} else {
int n = 42;
// Starts access epoch for window win
MPI_Win_lock(MPI_LOCK_EXCLUSIVE, 0, 0, win);
MPI_Put(&n, 1, MPI_INT, 0, 0, 1, MPI_INT, win);
// Ends access epoch for window win
MPI_Win_unlock(0, win);
MPI_Barrier(MPI_COMM_WORLD);
}
// Collective call
MPI_Win_free(&win);
MASTER MPI_Free_mem(buf);
MPI_Finalize();
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment