1

我正在尝试使用 MPI-2 功能进行单向通信来编写分布式哈希表程序。每个进程在开始时都会向所有其他进程公开一个内存 (B)。然后,希望每个进程可以在不同步的情况下访问对方的内存。到目前为止,我所拥有的如下所示(来自示例http://mpi.deino.net/mpi_functions/MPI_Put.html的修改代码)。最后,在某些情况下,我得到了错误的 B[99] 值(因运行而异)。我究竟做错了什么?


编辑:我现在通过最后的等待调用解决了上述问题,但我想等待每个 MPI_Get 调用完成。但看起来唯一的选择是需要全局同步的 MPI_Win_fence,或者每个 MPI_Get 调用的 MPI_Win_start/MPI_Win_complete。是否可以如下所示调用后者一次,并在每次 MPI_Get 调用后以某种方式等待?

#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; 
}
4

0 回答 0