首页 > 解决方案 > boost mpi:在 mpi 消息中传递的字符串变量是否有最大长度?

问题描述

在下面的测试代码中,如果我将 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;
}

标签: c++stringboostmpi

解决方案


您的程序在world.send(0, 0, outmessage).

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

从 MPI 3.1 标准,第 3.2.4 节:

Source=destination 是允许的,即进程可以向自己发送消息。(但是,使用上述阻塞发送和接收操作这样做是不安全的,因为这可能会导致死锁。

相关问题:自身等级的 MPI 通信行为是否定义明确?

解决方案是不要从进程 0 向自身发送任何内容。

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


推荐阅读