Boost logo

Boost Users :

From: Ivan Gonzalez (iglpdc_at_[hidden])
Date: 2008-05-16 10:30:18


Thanks very much both Sohail and Matthias for your reply!.

I finally got it working (I didn't have Boost 1.35 in any machine.)
The increase in the performance is *really* nice: in small, but relevant runs
of my code, I got an overall speed-up of around 300% just by serializing the
input/output of the Blitz++ arrays. (This small run saves 1 minute in
writing and later reading around 10^5 maps holding 10^5 doubles each,
compared to the use of operators<<,>> from Blitz++)
 
I'm commenting my experience here for the record.

1) You need Boost 1.35 (prior versions do not have the support for dense
arrays that Sohail mentioned.)

2) You need to build Boost serialization (although in some parts of the
docs says it is headers only) and link your code against it.

3) There is a bug in the library, a fix is here:
http://svn.boost.org/trac/boost/ticket/1822

4) When writing to/reading from a boost::binary_archive open the streams you
pass to the archives in the std::ios::binary mode.

The code relevant for the serialization of the Blitz++ arrays is shown below.
The serialization of the STL container (std::map or whatever) is done by
including the proper header (as explained in the serialization docs.)

/**
 * @typedef Defines a matrix type for the example.
 * Generalizations to higher-order tensors are trivial
 */
typedef blitz::Array<double, 2> my_Matrix;
   
#ifdef SERIALIZE_WITH_BOOST
/**
 * Functions to serialize array. The data() member function in Blitz++
 * gives you a pointer to the first element of the array holding the data.
 * Be careful if you use a non-default Blitz++ order.
 *
 * @see
 * http://www.boost.org/doc/libs/1_35_0/libs/serialization/doc/wrappers.html
 */
namespace boost{ namespace serialization{

    template<class Archive>
        inline void save(
                Archive & ar,
                const ::my_Matrix & t,
                const unsigned int file_version)
        {
            collection_size_type rows(t.rows());
            ar << BOOST_SERIALIZATION_NVP(rows);
            collection_size_type cols(t.cols());
            ar << BOOST_SERIALIZATION_NVP(cols);
            if (rows*cols) // save only if it is not empty
                ar << make_array(t.data(), t.size());
        }
    
    template<class Archive>
        inline void load(
                Archive & ar,
                ::my_Matrix & t,
                const unsigned int file_version)
        {
            collection_size_type rows;
            ar >> BOOST_SERIALIZATION_NVP(rows);
            collection_size_type cols;
            ar >> BOOST_SERIALIZATION_NVP(cols);
            collection_size_type count;
            if (rows*cols) // read only if it is not empty
            {
                // Blitz++ arrays need to be resized
                t.resize(rows,cols);
                ar >> make_array(t.data(), t.size());
            }
        }
    
    template<class Archive>
        inline void serialize(
                Archive & ar,
                ::my_Matrix & t,
                const unsigned int file_version)
        {
            split_free(ar, t, file_version);
        }

}; };// namespace boost::serialization
#endif


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