使用boost::mpi的MPI消息大小是否有限制?

6
我正在使用boost::mpi和openMPI编写模拟程序,一切都很顺利。但是,当我扩展系统规模并因此需要发送更大的std::vectors时,就会出现错误。
我已将问题简化为以下问题:
#include <boost/mpi.hpp>
#include <boost/mpi/environment.hpp>
#include <boost/mpi/communicator.hpp>
#include <boost/serialization/vector.hpp>
#include <iostream>
#include <vector>
namespace mpi = boost::mpi;

int main() {
    mpi::environment env;
    mpi::communicator world;

    std::vector<char> a;
    std::vector<char> b;
    if (world.rank() == 0) {
        for (size_t i = 1; i < 1E10; i *= 2) {
            a.resize(i);
            std::cout << "a " << a.size();
            world.isend(0, 0, a);
            world.recv(0, 0, b);
            std::cout << "\tB " << b.size() << std::endl;
        }
    }
    return 0;
}

输出结果:

a 1 B 1
a 2 B 2
a 4 B 4
....
a 16384 B 16384
a 32768 B 32768
a 65536 B 65536
a 131072    B 0
a 262144    B 0
a 524288    B 0
a 1048576   B 0
a 2097152   B 0

我知道mpi消息大小有限制,但是65KB对我来说似乎有点低。 有没有一种方法可以发送更大的消息?


根据这里的说法,您甚至不应该接近最大消息大小。但是我不知道出了什么问题。 - Baum mit Augen
如果将 isend 更改为 send 会发生什么?这可能是非阻塞发送导致问题的原因。 - NathanOliver
如果我将isend更改为send,它会在写入65536B 65536行的a后停止(阻塞)。 - tik
@tk - 你能查询recv返回的status吗?这可能会指引你一个方向。 - NathanOliver
@NathanOliver 好的,我试过了:status.error() 总是返回0。 - tik
显示剩余2条评论
1个回答

4
消息大小的限制与 MPI_Send 相同:INT_MAX
问题在于,在调整下一次迭代中的向量 a 的大小之前,您没有等待 isend 完成。这意味着由于向量 a 中的重新分配,isend 将读取无效数据。请注意,缓冲区 a 通过引用传递给 boost::mpi,因此在 isend 操作完成之前不允许更改缓冲区 a
如果使用 valgrind 运行程序,则会在 i = 131072 时立即看到无效读取。
你的程序之所以可以工作到 65536 字节,是因为 OpenMPI 如果消息小于组件 btl_eager_limit,则会直接发送消息。对于 self 组件(发送到自己的进程),它恰好为 128*1024 字节。由于 boost::serializationstd::vector 的大小添加到字节流中,因此一旦使用 128*1024 = 131072 作为输入大小,就会超过这个 eager_limit
要修复代码,请保存从 isend() 返回的 boost::mpi::request 返回值,然后将 wait() 添加到循环的末尾。
#include <boost/mpi.hpp>
#include <boost/mpi/environment.hpp>
#include <boost/mpi/communicator.hpp>
#include <boost/serialization/vector.hpp>
#include <iostream>
#include <vector>
namespace mpi = boost::mpi;

int main() {
    mpi::environment env;
    mpi::communicator world;

    std::vector<char> a;
    std::vector<char> b;
    if (world.rank() == 0) {
        for (size_t i = 1; i < 1E9; i *= 2) {
            a.resize(i);
            std::cout << "a " << a.size();
            mpi::request req = world.isend(0, 0, a);
            world.recv(0, 0, b);
            std::cout << "\tB " << b.size() << std::endl;
            req.wait();
        }
    }
    return 0;
}

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接