Search code examples
c++boostboost-serializationboost-mpi

Can I tell Boost.MPI which class version to use with Boost.Serialization?


I'm using Boost.MPI to exchange messages between processes. Each message carries one of my classes, serialized using Boost.Serialization. I also use the same serialization code to save that class to a file. What I need to send over MPI is smaller than what I need to save to a file (fewer relevant members). I was thinking that it would be nice to use the class versioning, supported by the Serialization library, to tell Boost.MPI to send the non-default version of the class, but I can't seem to find a way to do so. Do you know if this is possible?


Solution

  • It is not possible to serialize two different versions of the same type in the same binary module. The reason is that the version used is a compile time constant specified using the BOOST_CLASS_VERSION construct (the version number defaults to zero if not specified).

    What you could try is to implement specializations of the serialization member function for your type for specific archive types:

    // generic overload, used for everything except the MPI archives
    template <class Archive>
    void serialize(Archive & ar, const unsigned int version)
    {
        // do 'normal' (file-based) serialization
    }
    
    // overload for MPI archive used while deserialization
    void serialize(boost::mpi::packed_iarchive& ar, const unsigned int version)
    {
        // do MPI deserialization
    }
    
    // overload for MPI archive used while serialization
    void serialize(boost::mpi::packed_oarchive& ar, const unsigned int version)
    {
        // do MPI serialization
    }
    

    Similarily, you could provide overloads when using the split load/save serialization functions.