简体   繁体   English

MPI_Sendrecv_replace()死锁问题

[英]MPI_Sendrecv_replace() dead-lock issue

I'm doing my homework whith following assignment: 我正在完成作业后的作业:

Every process takes a double as an input. 每个进程都需要一个double作为输入。 Using function MPI_Sendrecv_replace() swap all doubles with processes of opposite rank (first & last, second & last but one, ...). 使用函数MPI_Sendrecv_replace()将所有双倍交换为相反等级的进程(第一个,最后一个,第二个和最后一个,但是......)。 In every process output recieved number. 在每个过程输出收到的数字。

So here is the code that I wrote. 所以这是我写的代码。

#include "mpi.h"
#include <stdio.h>
#include "pt4.h"

int main(int argc, char *argv[])
{
    MPI_Init(&argc,&argv);
    int flag;
    MPI_Initialized(&flag);
    if (flag == 0)
        return;
    int rank, size;
    MPI_Comm_size(MPI_COMM_WORLD, &size);
    MPI_Comm_rank(MPI_COMM_WORLD, &rank);

    double n;
    pt >> n; // pt is a stream provided by side library (works perfectly fine)

    int oppositeRank = (size - 1) - rank;

    if (rank != oppositeRank)
    {
        MPI_Status status;
        MPI_Sendrecv_replace(&n, 1, MPI_DOUBLE, oppositeRank, 0, 
                             rank, 0, MPI_COMM_WORLD, &status);
    }

    pt << n;
    MPI_Finalize();
    return 0;
}

Although this code compiles without any problems, it never stops. 虽然这段代码编译没有任何问题,但它永远不会停止。 So the question is why? 所以问题是为什么? What am I doing wrong? 我究竟做错了什么?

Replace this: 替换这个:

        MPI_Sendrecv_replace(&n, 1, MPI_DOUBLE, oppositeRank, 0, 
                         rank, 0, MPI_COMM_WORLD, &status);

with this: 有了这个:

        MPI_Sendrecv_replace(&n, 1, MPI_DOUBLE, oppositeRank, 0, 
                         oppositeRank, 0, MPI_COMM_WORLD, &status);

You may find this documentation page useful. 您可能会发现此文档页面很有用。

This function sends the buffer to a processor ( dest , or the 4th argument) and receives from another ( source , the 6th argument). 此函数将缓冲区发送到处理器( dest或第4个参数)并从另一个( source ,第6个参数)接收。 To do a swap you send to another rank and receive from that same rank. 要进行交换,您可以发送到另一个等级并从同一等级接收。 In your case you were sending to the opposite rank and receiving from yourself, which would never come, hence the deadlock. 在你的情况下,你发送到相反的级别并从自己接收,这将永远不会来,因此僵局。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM