Open MPI logo

Open MPI User's Mailing List Archives

  |   Home   |   Support   |   FAQ   |   all Open MPI User's mailing list

Subject: Re: [OMPI users] MPI and C++ - now Send and Receive of Classes and STL containers
From: John Phillips (phillips_at_[hidden])
Date: 2009-07-05 22:20:23

Luis Vitorio Cargnini wrote:
> Hi,
> So, after some explanation I start to use the bindings of C inside my
> C++ code, then comme my new doubt:
> How to send a object through Send and Recv of MPI ? Because the types
> are CHAR, int, double, long double, you got.
> Someone have any suggestion ?
> Thanks.
> Vitorio.


   If you are sending collections of built in data types (ints, doubles,
that sort of thing), then it may be easy, and it isn't awful. You want
the data in a single stretch of continuous memory. If you are using an
STL vector, this is already true. If you are using some other container,
then no guarantees are provided for whether the memory is continuous.

   Imagine you are using a vector, and you know the number of entries in
that vector. You want to send that vector to processor 2 on the world
communicator with tag 0. Then, the code snippet would be;

std::vector<double> v;

... code that fills v with something ...

int send_error;

send_error = MPI_Send(&v[0], v.size(), MPI_DOUBLE, 2, 0,

   The &v[0] part provides a pointer to the first member of the array
that holds the data for the vector. If you know how long it will be, you
could use that constant instead of using the v.size() function. Knowing
the length also simplifies the send, since the remote process also knows
the length and doesn't need a separate send to provide that information.

   It is also possible to provide a pointer to the start of storage for
the character array that makes up a string. Both of these legacy
friendly interfaces are part of the standard, and should be available on
any reasonable implementation of the STL.

   If you are using a container that is not held in continuous memory,
and the data is all of a single built in data type, then you need to
first serialize the data into a block of continuous memory before
sending it. (If the data block is large, then you may actually have to
divide it into pieces and send them separately.)

   If the data is not a block of all a single built in type, (It may
include several built in types, or it may be a custom data class with
complex internal structure, for example.) then the serialization problem
gets harder. In this case, look at the MPI provided facilities for
dealing with complex data types and compare to the boost provided
facilities. There is an initial learning curve for the boost facilities,
but in the long run it may provide a substantial development time
savings if you need to transmit and receive several complex types. In
most cases, the run time cost is small for using the boost facilities.
(according to the tests run during library development and documented
with the library)

                        John Phillips