I am trying to use MPI-2 features for one sided communication to write a program for distributed hash table. Each process exposes a memory (B) to all others at the start. Then, hopefully, each process can access the other's memory without synchronization. So far what I have is shown below ( a modified code from an example http://mpi.deino.net/mpi_functions/MPI_Put.html ) . At the end, I get wrong values for B[99] in some cases (differs from run to run). What am I doing wrong?
Edit: I fixed the above problem now with a wait call at the end, but I want to wait for completion of each MPI_Get call. But it looks like the only option is an MPI_Win_fence which requires global synchronization, or MPI_Win_start/MPI_Win_complete for each MPI_Get call. Is it possible to call the latter once as shown below and somehow be able to wait after each MPI_Get call?
#include "mpi.h"
#include "stdio.h"
#define SIZE2 200
int main(int argc, char *argv[])
{
int rank, nprocs, *B;
MPI_Group comm_group;
MPI_Win winl;
MPI_Init(&argc,&argv);
MPI_Comm_size(MPI_COMM_WORLD,&nprocs);
MPI_Comm_rank(MPI_COMM_WORLD,&rank);
MPI_Alloc_mem(SIZE2 * sizeof(int), MPI_INFO_NULL, &B);
for(int i=0; i<SIZE2; i++) B[i] = 0;
MPI_Comm_group(MPI_COMM_WORLD, &comm_group);
MPI_Win_create(B, SIZE2*sizeof(int), sizeof(int),MPI_INFO_NULL, MPI_COMM_WORLD, &winl);
MPI_Win_post(comm_group, 0, winl);
MPI_Win_start(comm_group, 0, winl);
if(rank == 0)
{
int drank = (rank == 0) ? 1 : 0;
int value = 200, value1;
int index = 99;
MPI_Get(&value1, 1, MPI_INT, drank, index+1, 1, MPI_INT, winl);
MPI_Put(&value, 1, MPI_INT, drank, index, 1, MPI_INT, winl);
MPI_Get(&value1, 1, MPI_INT, drank, index+1, 1, MPI_INT, winl);
MPI_Put(&value, 1, MPI_INT, drank, index, 1, MPI_INT, winl);
}
MPI_Win_complete(winl);
MPI_Win_wait(winl);
printf("%d: %d %d\n",rank,B[99],B[100]);
MPI_Group_free(&comm_group);
MPI_Win_free(&winl);
MPI_Free_mem(B);
MPI_Finalize();
return 0;
}