boost mpi:字符串变量传递 mpi 消息是否有最大长度?

boost mpi: Is there a max length for string variables to pass in an mpi message?

在下面的测试代码中,如果我将 SIZE 参数设置为远高于 960,则不会传输任何消息。在 boost mpi 消息中传递的字符串变量是否有最大长度? 也许字符串序列化有限制,但我无法在文档中找到和限制...... 非常感谢任何帮助。

//compile: mpic++ -Wall gather-002.cpp -o gather-002 -lboost_mpi -lboost_serialization
//run: mpirun -np 4 ./gather-002

#include <boost/mpi.hpp>
#include <iostream>
#include <vector>
#include <cstdlib>
#include <string>

#define SIZE 960

namespace mpi = boost::mpi;
using namespace std;

int main(int argc, char* argv[])
{
   mpi::environment env(argc, argv);
   mpi::communicator world;

   if (world.rank() == 0) { 
         string my_string = "MAIN";
         for (int proc = 0; proc < world.size(); ++proc){
            string outmessage = "";
            for (int i = 0; i < SIZE; i++) outmessage = outmessage + "-";
            world.send(proc, 0, outmessage);
         }

         vector<string> all_strings;
         gather(world, my_string, all_strings, 0);
         for (int proc = 0; proc < world.size(); ++proc) 
            cout << "Process #" << proc << "  " << all_strings[proc] << endl;
   }
   else { 
         string inmessage;
         world.recv(0,0,inmessage);
         gather(world, inmessage, 0);
   }
   return 0;
}

您的程序在 world.send(0, 0, outmessage) 中死锁。

对于足够小的字符串,您的 MPI 库正在调用 non-blocking,程序恰好 运行。当超过 MPI 库用于消息大小的任何阈值时,它会切换到阻塞调用。由于 no-one 正在接收消息,发送无法继续,程序挂起。请注意,标准不需要描述的行为:您不能依赖 MPI 库使用 non-blocking 小尺寸。

来自 MPI 3.1 标准,第 3.2.4 节:

Source = destination is allowed, that is, a process can send a message to itself. (How-ever, it is unsafe to do so with the blocking send and receive operations described above,since this may lead to deadlock.

相关问题:Is the behavior of MPI communication of a rank with itself well-defined?

解决方案是不从进程 0 向它自己发送任何东西。

可以发送的最大大小是INT_MAX,这是由您可以给MPI_Send的最大计数决定的。有关更多信息,请参阅