没有Boost.Serialization的序列化 [英] Serialization without Boost.Serialization
问题描述
我试图实现一个简单的序列化/反序列化方法,我的代码能够通过网络使用MPI传递一个对象。在理想的情况下,我会使用 Boost.Serialization
和 Boost.MPI
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.
我的策略是将每个对象序列化为 std :: stringstream 对象,然后使用
MPI_CHAR
作为数据类型通过 MPI_Send
发送消息。在这种情况下,我会传递 std :: stringstream :: str():: c_str()
作为指针和 std :: streaingstream :: str():: size()* sizeof(char)
作为消息的大小。
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.
转换为 std :: stringstream
对象。我的反序列化方法也需要一个 std :: stringstream
对象,反序列化所有的东西。这工作正常,但我不知道如何从 char
的数组创建 std :: stringstream
避免从数组到流的额外副本。我应该改变我的反序列化方法直接使用 char
的数组使用 memcpy
吗?
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?
推荐答案
这样做的MPI方式将使用 MPI_Pack
和 MPI_Unpack
。当然这是C,可能不像使用C ++功能的东西那么方便。有关简单示例,请参见 http://www.mcs.anl.gov/research/projects/mpi/tutorial/mpiexmpl/src/bcast/C/pack/solution.html
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
这篇关于没有Boost.Serialization的序列化的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!