当MPI中发送消息的数量未知时,我应该如何处理? [英] What should I do for recieving when the number of send messages is unknown in MPI?

查看:114
本文介绍了当MPI中发送消息的数量未知时,我应该如何处理?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在MPI中编程。我想发送的东西到另一个处理器,并收到它,但我不知道有多少邮件,我会发送。事实上,发送到另一个处理器的消息的数量取决于我在程序中读取的文件,所以我不知道在另一边应该写多少个接收。我应该使用哪种方法和功能?

I am programming in MPI. I want to send something to another processor and receive it there, but I don't know how many messages I will send. In fact, the number of messages which send to the other processor depends on the file which I am reading it during the program, so I don't know how many receives I should write on the other side. Which method and which function should I use?

推荐答案

您仍然可以使用发送和接收,的消息,告诉接收进程将没有新消息。通常这是通过使用不同的标签发送来处理的。所以你的程序看起来像这样:

You can still use sends and receives, but you would also add a new kind of message that tells the receiving process that there will be no new messages. Usually this is handled by sending with a different tag. So you program would look something like this:

if (sender) {
    while (data_to_send == true) {
        MPI_Send(data, size, datatype, receiving_rank, 0, MPI_COMM_WORLD);
    }
    for (i = 0; i < size; i++) {
        MPI_Send(NULL, 0, MPI_INT, i, 1, MPI_COMM_WORLD);
    }
} else {
    while (1) {
        MPI_Recv(data, size, datatype, 0, MPI_ANY_TAG, MPI_COMM_WORLD, &status);
        if (status.MPI_TAG == 1) break;

        /* Do processing */
    }
}

有一个更好的方法,如果你有非阻塞集合(从MPI-3)。在开始接收数据之前,您发布非阻塞屏障。然后你开始发布非阻塞接收。而不是只等待接收,你使用一个waitany两个请求,当障碍完成,你知道这里不会有任何更多的数据。在发送方,你只是继续发送数据,直到没有更多,然后做一个无阻塞的障碍完成事情。

There is a better way that works if you have non-blocking collectives (from MPI-3). Before you start receiving data, you post a non-blocking barrier. Then you start posting non-blocking receives. Instead of waiting only on the receives, you use a waitany on both requests and when the barrier is done, you know here won't be any more data. On the sender side, you just keep sending data until there's no more, then do a non-blocking barrier to finish things off.

这篇关于当MPI中发送消息的数量未知时,我应该如何处理?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆