Noman Javed
Noman Javed

Reputation: 51

Boost::Serialization Mpi Sending array of user defined types

I want to send my Array class using boost Mpi

template<class T>
class Array
{
 private:
  int size;
  T* data;
 public:
  // constructors + other stuff
};

Here T can be any built in type or user defined type. Suppose I have a class complex

struct complex
{
 std::vector<double> real_imag; // contain two elements
};

So the question is how can I send Array<complex> using Boost::Mpi + serialization.

Thanks in anticipation Regards Noman

Upvotes: 1

Views: 2857

Answers (1)

tstollenw
tstollenw

Reputation: 193

Why don't you use STL vector instead of your own Array class. The serialization of STL-vectors is already build in in boost/serialization/vector.hpp. If you want to send an array of complex numbers you could you something like this:

#include <vector>
#include <boost/mpi/environment.hpp>
#include <boost/mpi/communicator.hpp>
#include <boost/serialization/complex.hpp>
#include <boost/serialization/vector.hpp>

using namespace std;
namespace mpi=boost::mpi;

int main (int argc, char *argv[])
{
    mpi::environment env(argc, argv);
    mpi::communicator world;
    int myid=world.rank();
    int NN=world.size();
    int N=10;

    vector< complex<double> >A(N);

    if (myid==0)
    {
            for (int i=0; i!=N; i++)
            {
                    A[i]=complex<double>(i, i);
            }
            world.send(1, 0, A);
    }
    if (myid==1)
    {
            world.recv(0, 0, A);

            cout << "###" << endl;
            for (int i=0; i!=N; i++)
            {
                    cout << A[i] << "\t" ;
            }
            cout << endl;
            cout << "###" << endl;
    }
}

If not, you have to make the datatype which is content of your vector is serializable. If the serialization of that datatype is not part of the boost serialization library you have to write your own serialization. For example for your complex struct from above somethink like this should do the trick (not tested):

namespace boost
{
    namespace serialization
    {
            template<class Archive>
            void serialize(Archive & ar, complex & c, const unsigned int version)
            {
                    ar & c.real_imag;
            }
    }
}

But as I said, the STL complex type is already build in in boost serialization.

Upvotes: 4

Related Questions