I'm trying to implement a simple serialization/deserialization method for my code to be able to pass an object over the network using MPI. In an ideal world I would have used Boost.Serialization
and Boost.MPI
for that but they are not installed on some of the clusters I have access to so I'm considering doing this myself.
My strategy is to serialize every object into a std::stringstream
object and then send a message via MPI_Send
using MPI_CHAR
as the datatype. In such a case I would pass std::stringstream::str()::c_str()
as the pointer and std::streaingstream::str()::size()*sizeof(char)
as the size of the message.
I've figured how to serialize everything into a std::stringstream
object. My deserialization method also takes a std::stringstream
object and deserializes everything back. This works fine except I do not know how to create a std::stringstream
object from an array of char
s and avoid the extra copy from the array into the stream. Should I change my deserialization method to directly work with an array of char
using memcpy
instead?
The MPI way of doing this, would be using MPI_Pack
and MPI_Unpack
. Of course that is C and might not be as convenient as something using C++ features. For a simple example see http://www.mcs.anl.gov/research/projects/mpi/tutorial/mpiexmpl/src/bcast/C/pack/solution.html
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With