Boost logo

Boost Users :

Subject: Re: [Boost-users] [Boost.MPI] question about serializing customobject
From: Cristobal Navarro (axischire_at_[hidden])
Date: 2010-08-26 16:03:31


i made some progress while we were mailing between us.

first of all, im trying to see if my object can be serialized / deserialized
in the common way without going into MPI yet.
objects are Lattices, they have Nodes and Edges. nothing more.

this is how i've setup serialization for all three types of classes:

#NODE
class Node{
    private:
        friend class boost::serialization::access;
        template<class Archive>
        void serialize(Archive & ar, const unsigned int version){
            ar & sp;
            ar & id;
            ar & ext;
            ar & x;
            ar & y;
            ar & z;
            ar & eMap;
        }
    public:
        bool sp; //special flag
        int id, ext;
        float x, y, z;
        multimap<int,int> eMap;
        //plus some methods which i didnt put
};

#CLASS EDGE
class Edge{
    private:
        friend class boost::serialization::access;
        template<class Archive>
        void serialize(Archive & ar, const unsigned int version){
            ar & sp;
            ar & dead;
            ar & n1;
            ar & n2;
            ar & id;
        }
    public:
        int n1;
        int n2;
        int id;
        bool sp;
        bool dead;
        //plus some methods, constructors, etc
};

#finally, CLASS LATTICE

class Lattice{
    private:
            friend class boost::serialization::access;
            template<class Archive>
            void serialize(Archive & ar, const unsigned int version){
                ar & numNodes; //int
                ar & numEdges; //int
                ar & nodes; //map
                ar & edges; //map
                //ar & key; //! had to comment this attribute, stringstream
couldn't be serialized
                ar & keyLists; //list < list> >
                ar & corrupt;//bool
                ar & rec;//bool
                ar & nop;//int
            }

    public:
            int numNodes;
            int numEdges;
            map<int, Edge> edges;
            map<int, Node> nodes;
            stringstream key;
            list< list<int> > keyLists;
            bool corrupt;
            bool rec;
            int nop;
            //plus constructors, methods, etc
}

it compiles and links ok, and when i test serialization / deserialization
with the following code..:

                printf("serializing to binlat.x...\n");
                //!file mode
                /*
                std::ofstream ofs("binlat.x", ios::binary);
                boost::archive::binary_oarchive oa(ofs);
                */
                oa << *(this->lat);
                ofs.close();
                lat->print(); //check what lattice im serializing
                //!Deserialization - read file binlat.x and reconstruct
                printf("reconstructing serialized binlat.x...\n");
                Lattice nl;
                std::ifstream ifs("binlat.x", ios::binary);
                boost::archive::binary_iarchive ia(ifs);
                //!deserialize object
                ia >> nl;
                ifs.close();
                nl.print();

i get bad errors when deserializing, complaining about a bool and char.
serializing to binlat.x...
Lattice::print().....START
Nodes=2 Edges= 1
Node[0] sp=1 ext=1
 Linked to edge 2 (Node 3 or 0 )
Node[3] sp=1 ext=2
 Linked to edge 2 (Node 3 or 0 )
Lattice::print().....END
[enter]
reconstructing swerialized binlat.x...
[enter]
plattice: /usr/local/include/boost/archive/basic_binary_iprimitive.hpp:98:
void boost::archive::basic_binary_iprimitive<Archive, Elem, Tr>::load(bool&)
[with Archive = boost::archive::binary_iarchive, Elem = char, Tr =
std::char_traits<char>]: Assertion `0 == i || 1 == i' failed.
[lenovo00:29384] *** Process received signal ***
[lenovo00:29384] Signal: Aborted (6)
[lenovo00:29384] Signal code: (-6)
[lenovo00:29384] [ 0] [0xfea410]
[lenovo00:29384] [ 1] /lib/tls/i686/cmov/libc.so.6(abort+0x182) [0xd41a82]
[lenovo00:29384] [ 2] /lib/tls/i686/cmov/libc.so.6(__assert_fail+0xf8)
[0xd37718]
[lenovo00:29384] [ 3]
..
..
*** End of error message ***

i started debugging, and realized that the problem is completely gone when i
remove "edges" from the serialization members of "Lattice" object.
its weird, because "edge" is a map of <int, Edge> and much simplier than
"nodes" which does work. obviously i cannot continue without edges, so i
need to fix it somehow, but i dont understand the error related to edges,
maybe is something im not seeing.

dont get distracted by the messy code, i had to edit it a little to focus on
the case.
regards
Cristobal

On Thu, Aug 26, 2010 at 4:35 PM, Robert Ramey <ramey_at_[hidden]> wrote:

> > Change the "would" into "is": it is possible to mix Boost.MPI with
> > Boost.Serialization. Any objext that can be serialized can be sent by
> > Boost.MPI. This was actually the basic idea behind Boost.MPI, to use
> > Boost.Serialization to pack and unpack MPI buffers, or to create
> > custom MPI datatypes.
> On thing that I never understood is why you need MPI data types at all.
> If one is serializing (packing) to a binary array, and sending that, what
> hae MPI data types have to do with it.
>
> If one were using heterogenious machines, I could understand the
> usage of MPI types. But as I understand it, the MPI serialization presumes
> that the machines are binary compatible. So I'm just not seeing this.
>
> Robert Ramey
>
>
> _______________________________________________
> Boost-users mailing list
> Boost-users_at_[hidden]
> http://lists.boost.org/mailman/listinfo.cgi/boost-users
>



Boost-users list run by williamkempf at hotmail.com, kalb at libertysoft.com, bjorn.karlsson at readsoft.com, gregod at cs.rpi.edu, wekempf at cox.net