3

我很难使用 MPI 实现前缀和。我想我遗漏了几行,但我不知道遗漏了哪些以及它们应该放在哪里。这是我所拥有的:

int main(int argc, char** argv){  
 int i, size, nprocs, rank;
 int array[atoi(argv[1])];

int Destination, Destination_tag;
int Source, Source_tag, RecvData;

int len = sizeof(array)/sizeof(int);

for(i = 0; i < size; i++) 
     {
    array[i] = i+rank*size;
}

MPI_Init(&argc, &argv);
MPI_Comm_rank(MPI_COMM_WORLD, &rank);
MPI_Comm_size(MPI_COMM_WORLD, &size);

int id = rank;

      //I believe the error is here
for(i = 0; i < size, i++)
{
    message = (rank - pow(2,x));

    Destination = message ;
    Destination_tag = array.id; 
    MPI_Send(&message, 1, MPI_INT, Destination, Destination_tag, MPI_COMM_WORLD); 

    Source = message ;
    Source_tag = message; 
    MPI_Recv(&RecvData, 1, MPI_INT, Source, Source_tag, MPI_COMM_WORLD, &Status); 
   //End of problem area
    printf("My rank is  %d n =%d \n",i,size); 

    MPI_Finalize();
    return 0;

}

4

2 回答 2

9

MPI_Send不要使用and手动计算前缀总和,而是MPI_Recv使用MPI_Scan. MPI_Scan对 process_{0} 到 process_{your rank} 中的元素进行部分包含缩减,让您可以非常轻松地(并且有效地!)执行前缀求和。

例如,假设单个整数像这样分布在进程中:
进程 0 = 2
进程 1 = 3
进程 2 = 4
进程 3 = 5

调用MPI_ScanusingMPI_SUM作为归约操作后,结果将是:
Process 0 = 2
Process 1 = 5
Process 2 = 9
Process 3 = 14

调用MPI_Exscan进行独占扫描,这意味着归约不会将调用进程持有的数据包含在归约中。结果将如下所示:
Process 0 = undefined (将你的 recv_buffer 设置为 0 否则会有垃圾)
Process 1 = 2
Process 2 = 5
Process 3 = 9

于 2012-11-27T21:04:18.280 回答
0

您的 MPI 发送和接收呼叫可能是错误。一般来说,我被教导说你应该总是告诉 MPI 进程它的发送和接收是通过一个

 if (rank == 0)
    {
      MPI_Send(...
    }

if (rank == 1)
    {
     MPI_Recieve(...
于 2012-11-24T16:01:26.137 回答