c++ - MPI 发送自定义序列化对象(更通用的代码)

标签 c++ serialization boost mpi

问题

我正在尝试找到一种通过 MPI 发送自定义序列化对象(不是自定义 MPI 结构 - 请参阅下面的定义)的正确方法。在阅读了一些 Material 和 stackoverflow 之后,我有一个工作 示例,它使用boost::serialization 并将序列化对象作为stringstream 发送。但是,我当前的解决方案看起来有点hackish,请参见下面的快照(完整代码附在末尾部分)。

My question: Can you give an opinion on the current solution and recommend some industry accepted way to send custom serialized objects?

.

Restriction: Unfortunately boost.mpi is not an option due to its dependency with openmpi which has a TCP related bug on my ubuntu-xenial infrastructure. I use only pure mpich.

自定义对象定义

custom object: In my example the custom object serializes its base class, an std::vector, a boost::shared_ptr and some other simple variables.

MPI 发送/接收快照

这是我如何发送/接收流的小程序快照。

 if (rank == 1) {
        std::stringstream mystream;
        //...more serialization code here

        int len = mystream.str().size();
        MPI_Send( &len, 1, MPI_INT, 1, lentag, MPI_COMM_WORLD );
        MPI_Send( (void *)mystream.str().c_str(), len, MPI_BYTE, 1, datatag, MPI_COMM_WORLD );

} else if (rank == 1) {
        int len;
        MPI_Recv( &len, 1, MPI_INT, 0, lentag, MPI_COMM_WORLD, MPI_STATUS_IGNORE);

        char data[len+1];
        MPI_Recv( data, len, MPI_BYTE, 0, datatag, MPI_COMM_WORLD, MPI_STATUS_IGNORE);
        data[len] = '\0';

        std::stringstream mystream;
        mystream.write((const char*) data, len);

        //...more deserialization code here
}

程序输出

这是程序输出。您可以看到数据已成功从 rank 0 转移到 rank 1

$ mpirun.mpich -np 2 ./mpidata 
Rank 0 sum in 6
Rank 0 vsize out 4
Rank 0 ptr out 30

Rank 1 sum in 6
Rank 1 vsize in 4
Rank 1 ptr in 30

MPI Send/Rcv 完整代码

下面提供了完整的代码。

#include <mpi.h>
#include <iostream>
#include <sstream>
#include <vector>

#include <boost/archive/binary_oarchive.hpp>
#include <boost/archive/binary_iarchive.hpp>
#include <boost/serialization/base_object.hpp>
#include <boost/serialization/shared_ptr.hpp>
#include <boost/serialization/vector.hpp>
#include <boost/serialization/string.hpp>
#include <boost/shared_ptr.hpp>
#include <boost/make_shared.hpp>

// Forward declaration of class boost::serialization::access
namespace boost {
namespace serialization {
class access;
}
}

class Obj {
public:
    // Serialization expects the object to have a default constructor
    Obj() : d1_(-1), d2_(-2) {}
    Obj(int d1, int d2) : d1_(d1), d2_(d2) {}
    bool operator==(const Obj& o) const {
        return d1_ == o.d1_ && d2_ == o.d2_;
    }

    const int sum() const {return d1_+d2_;}
private:
    int d1_;
    int d2_;

    // Allow serialization to access non-public data members.
    friend class boost::serialization::access;

    template<typename Archive>
    void serialize(Archive& ar, const unsigned version) {
        ar & d1_ & d2_; // Simply serialize the data members of Obj
    }
};

class ObjChild : public Obj {

private:
    typedef Obj _Super;

public:
    ObjChild() : Obj(),d1_(-1),dv_{1,2},iptr_(new Obj()) {}
    ObjChild(
            int d1,
            int d2,
            int d1new,
            std::vector<int> const& dv,
            boost::shared_ptr<Obj> const& obj
            ) : Obj(d1,d2),d1_(d1new),dv_(dv),iptr_(obj) {}

    const int sum2() const {return d1_ + sum();}
    const int vsize() const {return dv_.size();}
    const int ptrsum() const {return iptr_->sum();}

private:
    int d1_; // Another d1_
    std::vector<int> dv_;
    boost::shared_ptr<Obj> iptr_;

    // -------------------------------------------------------------
    friend class boost::serialization::access;

    template <typename Archive>
    void serialize(Archive& ar, const unsigned version) {
        ar & boost::serialization::base_object<_Super>(*this);
        ar & d1_;
        ar & dv_;
        ar & iptr_;
    }
    // -------------------------------------------------------------
};

int main(int argc,char** argv) {

    int size, rank;

    MPI_Init(&argc, &argv);
    MPI_Comm_size(MPI_COMM_WORLD, &size);
    MPI_Comm_rank(MPI_COMM_WORLD, &rank);

    if (size < 2) {
        if (rank == 0)
            std::cerr << "Require at least 2 tasks" << std::endl;
        MPI_Abort(MPI_COMM_WORLD, 1);
    }

   const int lentag=0;
   const int datatag=1;
   if (rank == 0) {

        std::stringstream mystream;

        ObjChild obj(1,3,2,{1,2,3,4},boost::make_shared<Obj>(10,20));

        boost::archive::binary_oarchive oarchive{mystream};
        oarchive << obj;

        std::cout<<"Rank "<< rank << " sum in " << obj.sum2() << std::endl;
        std::cout<<"Rank "<< rank << " vsize out " << obj.vsize() << std::endl;
        std::cout<<"Rank "<< rank << " ptr out " << obj.ptrsum() << std::endl;

        int len = mystream.str().size();
        // Send length, then data
        MPI_Send( &len, 1, MPI_INT, 1, lentag, MPI_COMM_WORLD );
        MPI_Send( (void *)mystream.str().c_str(), len, MPI_BYTE, 1, datatag, MPI_COMM_WORLD );


        } else if (rank == 1) {
            int len;
            MPI_Recv( &len, 1, MPI_INT, 0, lentag, MPI_COMM_WORLD, MPI_STATUS_IGNORE);

            char data[len+1];
            MPI_Recv( data, len, MPI_BYTE, 0, datatag, MPI_COMM_WORLD, MPI_STATUS_IGNORE);
            data[len] = '\0';

            std::stringstream mystream;
            mystream.write((const char*) data, len);

            boost::archive::binary_iarchive iarchive(mystream);

            ObjChild obj;

            iarchive >> obj;

            std::cout<<"Rank "<< rank << " sum in "<< obj.sum2() << std::endl;
            std::cout<<"Rank "<< rank << " vsize in " << obj.vsize() << std::endl;
            std::cout<<"Rank "<< rank << " ptr in " << obj.ptrsum() <<         std::endl;

        }

        MPI_Finalize();
        return EXIT_SUCCESS;
}

最佳答案

如果您正在使用 Boost.Serialization,那么绝对明智的做法是使用 Boost.MPI。这基本上会从通信部分隐藏所有序列化样板,如下所示:

  boost::mpi::environment env;
  boost::mpi::communicator world;
  auto rank = world.rank();

  if (world.size() < 2) {
    if (rank == 0)
      std::cerr << "Require at least 2 tasks" << std::endl;
    MPI_Abort(MPI_COMM_WORLD, 1);
  }

  const int datatag = 1;
  if (rank == 0) {
    ObjChild obj(1, 3, 2, {1, 2, 3, 4}, boost::make_shared<Obj>(10, 20));

    std::cout << "Rank " << rank << " sum in " << obj.sum2() << std::endl;
    std::cout << "Rank " << rank << " vsize out " << obj.vsize() << std::endl;
    std::cout << "Rank " << rank << " ptr out " << obj.ptrsum() << std::endl;

    world.send(1, datatag, obj);
  } else if (rank == 1) {
    ObjChild obj;
    world.recv(0, datatag, obj);

    std::cout << "Rank " << rank << " sum in " << obj.sum2() << std::endl;
    std::cout << "Rank " << rank << " vsize in " << obj.vsize() << std::endl;
    std::cout << "Rank " << rank << " ptr in " << obj.ptrsum() << std::endl;
  }

某些类型(如 POD)可能会受益于额外指定 is_mpi_datatype , 但 ObjChild 由于指针不合格。

不幸的是,尽管 boost 享有盛誉,但 Boost.MPI 似乎几乎没有得到维护,根本问题没有得到解决甚至讨论。对序列化对象的非阻塞通信要特别小心。因此,如果您不愿意自己投资修复东西,我不一定会推荐将 Boost.MPI 用于生产代码。这可能仍然比自己从头开始 build 要好。另请注意,序列化,尤其是 Boost 的实现速度相当慢,可能不适合某些 HPC 用例,在这些用例中,最好将内存布局设计为首先不需要任何序列化或复杂的打包。

关于c++ - MPI 发送自定义序列化对象(更通用的代码),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49958490/

相关文章:

c# - 从 C# 中的 C++ 函数返回数组和结构

c++ - 我的MDI中的拖放操作被CRichEditView拦截,并在CRichEditView::GetDocument中崩溃-如何覆盖它?

c# - 使用 IXmlSerializable 对象序列化对象时出现 InvalidOperationException

c++ - 用于模板化自引用结构的 Boost Fusion 自适应声明

c++ - API 更改时函数参数的常量正确性

java - Gson 枚举序列化

java - 强制 JAX-RS 将我的类序列化为 JSON 对象

c++ - 我们可以将 char[undetermined_during_compile_time_size] 保留在 boost::shared_ptr 中吗?

c++ - 如何正确使用BOOST_THROW_EXCEPTION?

c++ - 重载运算符将函数指针作为参数,我如何检索函数指针的参数