/* Program fence.c */ #include "mpi.h" #include int main(int argc, char **argv) { int rank, i, buf[2]; //Start up MPI... MPI_Init(&argc,&argv); MPI_Comm_rank(MPI_COMM_WORLD,&rank); MPI_Comm comm; MPI_Group comm_group, group; MPI_Win win; comm = MPI_COMM_WORLD; for(i=0;i<2;i++) buf[i]=0; //Initialize all buffers to 0 if (rank == 0) { /* Only rank 0 has a nonzero buffer at start */ for(i=0;i<2;i++) buf[i]=(i+1)*3; /* Everyone will retrieve from the buffer on root */ int soi = sizeof(int); MPI_Win_create(buf,soi*2,soi,MPI_INFO_NULL,comm,&win); } else { /* Others only retrieve, so these windows can be size 0 */ MPI_Win_create(NULL,0,sizeof(int),MPI_INFO_NULL,comm,&win); } printf("Before Get on %d: %d %d\n",rank,buf[0],buf[1]); /* No local operations prior to this epoch, so give an assertion */ MPI_Win_fence(MPI_MODE_NOPRECEDE,win); /* Inside the fence, ranks make RMA calls to GET from rank 0 */ if (rank != 0) { MPI_Get(buf,2,MPI_INT,0,0,2,MPI_INT,win); } /* Complete the epoch - this blocks until the MPI_Get is complete - all done with the window, so tell MPI there are no more epochs */ MPI_Win_fence(MPI_MODE_NOSUCCEED,win); printf("After Get on %d: %d %d\n",rank,buf[0],buf[1]); /* Free up our window */ MPI_Win_free(&win); //Shut down... MPI_Finalize(); return(0); }