简体   繁体   English

有没有办法在不使用 MPI_Wait 的情况下编写非阻塞 MPI 代码?

[英]Is there any way to write non blocking MPI code without using MPI_Wait?

I'm having this code which works fine.我有这个工作正常的代码。 My question is that is it possible to have a non-blocking code without MPI_Wait ?我的问题是,没有MPI_Wait是否可以有非阻塞代码? I am thinking that whether by this way, my code behaves as a blocking mode inadvertently.我在想,是否通过这种方式,我的代码会无意中表现为阻塞模式。 Also, to confirm that this is a non-blocking code, shall I measure execution time?另外,为了确认这是一个非阻塞代码,我应该测量执行时间吗? and if it is faster, I can conclude that this is non-blocking.如果它更快,我可以得出结论,这是非阻塞的。 However, here, since no work is involved between MPI_Wait and calculating of data;但是,在这里,由于MPI_Wait和数据计算之间不涉及任何工作; I think this does not work.我认为这行不通。 So, How can I make sure that this behaves as a non-blocking mode?那么,如何确保这表现为非阻塞模式?

#include <iostream>
#include <mpi.h>
using namespace std;

int main() {
    MPI_Init(NULL, NULL);
    MPI_Request request;
    MPI_Status status;
    int rank, size, data;
    MPI_Comm_rank(MPI_COMM_WORLD, &rank);
    MPI_Comm_size(MPI_COMM_WORLD, &size);



    if (rank > 0) {

        MPI_Irecv(&data, 1, MPI_INT, rank - 1, 0, MPI_COMM_WORLD, &request);
        MPI_Wait(&request, &status);
        cout << "Rank " << rank << " has received message with data " << data << " from rank " << rank - 1 << endl;

    }
    cout << "Hello from rank " << rank << " out of " << size << "   "  << endl;
  

    data = rank;

    MPI_Isend(&data, 1, MPI_INT, (rank + 1) % size, 0, MPI_COMM_WORLD, &request);


    if (rank == 0) {
        MPI_Irecv(&data, 1, MPI_INT, size - 1, 0, MPI_COMM_WORLD, &request);
        MPI_Wait(&request, &status);
        cout << "Rank " << rank << " has received message with data " << data << " from rank " << size - 1 << endl;
    }


    MPI_Finalize();
    return 0;
}

My question is that is it possible to have a non-blocking code without MPI_Wait我的问题是,没有 MPI_Wait 是否可以有非阻塞代码

Yes, one can have a non-blocking code without the MPI_Wait ;是的,没有MPI_Wait可以有一个非阻塞代码; the problem is, however, that the data used on the non-blocking communication routines can get into inconsistent states.然而,问题在于,用于非阻塞通信例程的数据可能会进入不一致的状态。 In other words, one cannot be sure if that data can be safely used (or not).换句话说,人们无法确定是否可以安全地使用这些数据(或不能)。

In the current context, non-blocking simply means that one does not block waiting for the data to be copied to/from the buffer and received/send.在当前上下文中,非阻塞意味着不阻塞等待数据被复制到缓冲区/从缓冲区复制和接收/发送。 After having called the non-blocking routine, one can immediately resume one's computation.调用非阻塞例程后,可以立即恢复计算。

I am thinking that whether by this way, my code behaves as a blocking mode inadvertently.我在想,是否通过这种方式,我的代码会无意中表现为阻塞模式。 Also, to confirm that this is a non-blocking code, shall I measure execution time?另外,为了确认这是一个非阻塞代码,我应该测量执行时间吗? and if it is faster, I can conclude that this is non-blcoking.如果它更快,我可以得出结论,这是非阻塞的。

One can always test, but one can also trust the specification (and/or the implementation) whether a given routine is non-blocking.人们总是可以测试,但也可以信任规范(和/或实现)给定例程是否是非阻塞的。

However, here, since no work is involved between MPI_Wait and calculating of data;但是,在这里,由于 MPI_Wait 和数据计算之间不涉及任何工作; I think this does not work.我认为这行不通。

Indeed, one of the points of using such routines is to overlap computation with communication as long as that computation does not work on the data used on the communication.实际上,使用此类例程的要点之一是将计算与通信重叠,只要该计算不适用于通信中使用的数据。

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

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