Skip to content

Instantly share code, notes, and snippets.

@aprell
Created December 6, 2011 10:22
Show Gist options
  • Save aprell/1437684 to your computer and use it in GitHub Desktop.
Save aprell/1437684 to your computer and use it in GitHub Desktop.
One-sided communication 2
#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;
MPI_Win win;
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;
MPI_Alloc_mem(sizeof(int), MPI_INFO_NULL, &buf);
*buf = 0;
MPI_Win_create(buf, sizeof(int), 1, MPI_INFO_NULL, MPI_COMM_WORLD, &win);
MPI_Win_lock(MPI_LOCK_EXCLUSIVE, next, 0, win);
MPI_Put(&ID, 1, MPI_INT, next, 0, 1, MPI_INT, win);
MPI_Win_unlock(next, win);
MPI_Barrier(MPI_COMM_WORLD);
printf("Worker %d: %d\n", ID, *buf);
MPI_Win_free(&win);
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