Boost logo

Boost Users :

From: Stephan Nies (niestephan_at_[hidden])
Date: 2006-04-28 13:28:29


Hello,
I want to use boost/serialization combined
with the MPI (Message Passing Interface) to
add parallel computation to a traffic simulation
i am working on.

I want to serialize a STL container filled
with custom Objects to send it to another process.
If I don't set "boost::serialization::track_never"
for every class i get a compiler error.

The Example Code (without any MPI) below creates this
error when compiled:

g++ test3.cpp -I/usr/include -L/usr/lib -lboost_serialization -o test3
/usr/include/boost/archive/detail/oserializer.hpp: In function ‘void
boost::archive::save(Archive&, T&) [with Archive =
boost::archive::text_oarchive, T = gps_packet]’:
/usr/include/boost/archive/basic_text_oarchive.hpp:78: instantiated from
‘void boost::archive::basic_text_oarchive<Archive>::save_override(T&, int)
[with T = gps_packet, Archive = boost::archive::text_oarchive]’
/usr/include/boost/archive/detail/interface_oarchive.hpp:78: instantiated
from ‘Archive&
boost::archive::detail::interface_oarchive<Archive>::operator<<(T&) [with T
= gps_packet, Archive = boost::archive::text_oarchive]’
test3.cpp:76: instantiated from here
/usr/include/boost/archive/detail/oserializer.hpp:567: error: invalid
application of ‘sizeof’ to incomplete type
‘boost::STATIC_ASSERTION_FAILURE<false>’
make: *** [test3] Error 1

Here is a short example that creates the above error
based upon the example in the documentation:

---code start--------

#include <iostream>
#include <sstream>
#include <boost/archive/text_oarchive.hpp>
#include <boost/archive/text_iarchive.hpp>
#include <boost/serialization/list.hpp>
#include <list>

class gps_position
{
private:
    friend class boost::serialization::access;
    template<class Archive>
    void serialize(Archive & ar, const unsigned int version)
    {
        ar & degrees;
        ar & minutes;
        ar & seconds;
    }
    int degrees;
    int minutes;
    float seconds;
public:
    gps_position(){};
    gps_position(int d, int m, float s) :
        degrees(d), minutes(m), seconds(s)
    {}
    void pretty_print()
    {
        std::cout << "deg:" << degrees ;
        std::cout << " min:" << minutes ;
        std::cout << " sec:" << seconds << std::endl;
    }
};

typedef std::list<gps_position> list_gps_position;

class gps_packet
{
private:
   friend class boost::serialization::access;
   template<class Archive>
   void serialize(Archive & ar, const unsigned int version)
   {
      ar & gps_list;
   }
public:
   gps_packet(){}
   list_gps_position gps_list;
};

// only when I uncomment the following
// boost::serialization::track_never
// statements programm will
compile
//BOOST_CLASS_IMPLEMENTATION(gps_position,boost::serialization::object_serializable);
//BOOST_CLASS_TRACKING(gps_position,
boost::serialization::track_never);
//BOOST_CLASS_IMPLEMENTATION(gps_packet,boost::serialization::object_serializable);
//BOOST_CLASS_TRACKING(gps_packet, boost::serialization::track_never);

int main() {
    // create come pseudo data
    gps_position g0(1, 2, 3.3);
    gps_position g1(11, 22, 33.33);
    gps_packet packet;
    packet.gps_list.push_back(g0);
    packet.gps_list.push_back(g1);

    // print before transfer
    std::cout << "Before:" << std::endl;
    packet.gps_list.front().pretty_print();
    packet.gps_list.back().pretty_print();

    // serialize to chars in order to use
    // the MPI::CHAR elementary datatype
    std::ostringstream ost;
    boost::archive::text_oarchive oa(ost);
    oa << packet;
    const char* chars = ost.str().data();

    // assume some message passing
    // through MPI::CHAR to happen here
    std::cout << "Transfering:" << std::endl;
    std::cout << chars << std::endl;

    // reconstruction from chars for further
    // computation
    std::istringstream ist(chars);
    boost::archive::text_iarchive ia(ist);
    gps_packet packet_new;
    ia >> packet_new;

    // print transfer result
    std::cout << "After:" << std::endl;
    packet_new.gps_list.front().pretty_print();
    packet_new.gps_list.back().pretty_print();
    return 0;
}

---code end--------

when i uncomment
BOOST_CLASS_TRACKING(gps_position, boost::serialization::track_never);
BOOST_CLASS_TRACKING(gps_packet, boost::serialization::track_never);
i can compile and execute with this output:

  Before:
  deg:1 min:2 sec:3.3
  deg:11 min:22 sec:33.33
  Transfering:
  22 serialization::archive 3 0 0 0 0 2 0 0 1 2 3.3 11 22 33.330002
  After:
  deg:1 min:2 sec:3.3
  deg:11 min:22 sec:33.33

Further Questions:
1. I would also like to use just list<gps_position> or the
  typedef, instead of the wrapper class gps_packet which i need
  to pass something to
  BOOST_CLASS_TRACKING(gps_packet, boost::serialization::track_never);

2. Is it a good Idea to use something
like:
BOOST_CLASS_IMPLEMENTATION(gps_position,boost::serialization::object_serializable);
BOOST_CLASS_TRACKING(gps_position,boost::serialization::track_never);
 since I don't want to store but just pass
 the Information or are there any pitfalls?

Thanks,
Stephan Nies

-- 
When you see this text i am using the webinterface of
my mail provider and i apologize for any commercials 
they may attach below. 
Echte DSL-Flatrate dauerhaft für 0,- Euro*!
"Feel free" mit GMX DSL! http://www.gmx.net/de/go/dsl

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