简体   繁体   English

没有 Boost.Serialization 的序列化

[英]Serialization without Boost.Serialization

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.我正在尝试为我的代码实现一个简单的序列化/反序列化方法,以便能够使用 MPI 通过 .network 传递 object。 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.在理想情况下,我会为此使用Boost.SerializationBoost.MPI ,但它们没有安装在我有权访问的某些集群上,所以我正在考虑自己这样做。

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.我的策略是将每个 object 序列化为std::stringstream object,然后使用MPI_CHAR作为数据类型通过MPI_Send发送消息。 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::str()::c_str()作为指针传递,将std::streaingstream::str()::size()*sizeof(char)作为消息的大小传递.

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.我想出了如何将所有内容序列化为std::stringstream object。我的反序列化方法还采用std::stringstream object 并将所有内容反序列化。 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?这工作正常,除了我不知道如何从char数组创建std::stringstream object 并避免从数组中额外复制到 stream 中。我是否应该更改我的反序列化方法以直接使用char数组memcpy代替?

The MPI way of doing this, would be using MPI_Pack and MPI_Unpack . 执行此操作的MPI方式是使用MPI_PackMPI_Unpack Of course that is C and might not be as convenient as something using C++ features. 当然这是C,可能不像使用C ++功能那样方便。 For a simple example see http://www.mcs.anl.gov/research/projects/mpi/tutorial/mpiexmpl/src/bcast/C/pack/solution.html 有关简单示例,请参阅http://www.mcs.anl.gov/research/projects/mpi/tutorial/mpiexmpl/src/bcast/C/pack/solution.html

Use an istrstream , which extracts from a char array. 使用istrstream ,它从char数组中提取。 The header for this is <strstream> . 这个标题是<strstream> And, yes, formally it's deprecated in the C++ Standard. 并且,是的,正式地它在C ++标准中被弃用了。 The committee indulged in a great deal of wishful thinking in its early days. 委员会在其早期就沉迷于大量的一厢情愿的想法。 istrstream is not going to go away. istrstream不会消失。

Although the accepted answer is the right way to go, the concrete question "except I do not know how to create a std::stringstream object from an array of chars" was not answered yet.虽然接受的答案是 go 的正确方法,但具体问题“除了我不知道如何从字符数组创建std::stringstream object”尚未得到回答。

The answer is that effectively you can't.答案是实际上你做不到。

Presumably have an object already in memory and you want so see it as an stringstream , even if you can make it work the stringstream will not be able to grow further in the existing memory (which is an useful property of the stream buffer.).大概在 memory 中已经有一个 object 并且你希望将它视为一个stringstream ,即使你可以让它工作, stringstream也将无法在现有的 memory 中进一步增长(这是 stream 缓冲区的一个有用属性。)。

Also there is no much point into making heroic attempts to not copy data that is going to be send in an MPI message.此外,为了复制将要在 MPI 消息中发送的数据而做出英勇的尝试也没有多大意义。 MPI might end up copying the data internally anyway, to "pinned" memory for example depending on the exact method of communication (buffered, unbuffered, asynchronous, etc). MPI 可能最终会在内部复制数据,例如根据确切的通信方法(缓冲、非缓冲、异步等)“固定”memory。 That is my understanding.这是我的理解。

If the data is too large to be copied at all (eg many gigabytes) then it is better to use MPI_Datatype s which can be a real challenge to handle in generic ways.如果数据太大而根本无法复制(例如许多千兆字节),那么最好使用MPI_Datatype s,这对于以通用方式处理可能是一个真正的挑战。

BTW, the accepted answer is what my MPI library does internally https://gitlab.com/correaa/boost-mpi3 , it is probably what the original Boost.MPI does too.顺便说一句,公认的答案是我的 MPI 库在内部执行的操作 https://gitlab.com/correaa/boost-mpi3 ,它可能也是原始 Boost.MPI 执行的操作。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM