数组序列化 [英] serialization of arrays

查看:68
本文介绍了数组序列化的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

大家好,


我一直在尝试创建一种序列化C ++对象的简单方法,所以我可以在不同的处理器之间发送消息(b $ b)使用MPI)。非常简单的东西

如下所示:


模板< class StorageType>

void发送(StorageType& c,int dest ,int tag,MPI_Comm comm){


typedef typename StorageType :: ValueType ValueType;


int size = c.size()* sizeof (ValueType);

char * buffer [size];

memcpy(缓冲区,& c [0],大小);


MPI_Send(& buffer,size,MPI_BYTE,dest,tag,comm);

}


我用整数数组测试了模板化函数它的工作原理

罚款。但是,当我尝试使用

boost库提供的dynamic_bitset时,我无法传递对c [0]的引用,因为operator&这个班级是私人的
私人。有没有另一种有效的方法可以做一个按位

复制???


谢谢大家,


a2

解决方案

2007-09-03 22:48,aaragon写道:


大家好,


我一直在尝试创建一种序列化C ++对象的简单方法,所以我可以在不同的处理器之间发送消息(使用MPI)。非常简单的东西

如下所示:


模板< class StorageType>

void发送(StorageType& c,int dest ,int tag,MPI_Comm comm){


typedef typename StorageType :: ValueType ValueType;


int size = c.size()* sizeof (ValueType);

char * buffer [size];

memcpy(缓冲区,& c [0],大小);


MPI_Send(& buffer,size,MPI_BYTE,dest,tag,comm);

}


我用整数数组测试了模板化函数它的工作原理

罚款。但是,当我尝试使用

boost库提供的dynamic_bitset时,我无法传递对c [0]的引用,因为operator&这个班级是私人的
私人。有没有另一种有效的方法来做一个按位

复制???



我不知道dynamic_bitset,但是std :: bitset有一个to_string()

方法将内容转换为a字符串为1和0,如果bitset

不大,你仍然可以从中获得相当好的效率。

也是一个to_ulong()方法将它转换为unsigned long,但是

仅适用于小位集。我怀疑

的dynamic_bitset至少有一个to_string()方法。


如果小缓冲区很重要,你必须编写读取的代码
来自bitset的
值并在char数组中设置正确的位,它不是太多的工作,但只是发送一个字符串要容易得多。 />

-

Erik Wikstr?m


aaragon写道:


大家好,


我一直在尝试创建一种序列化C ++对象的简单方法,所以我可以发送
不同处理器之间的消息(使用MPI)。非常简单的东西

如下所示:


模板< class StorageType>

void发送(StorageType& c,int dest ,int tag,MPI_Comm comm){



为什么不const StorageType& ?


>

typedef typename StorageType :: ValueType ValueType;


int size = c.size()* sizeof(ValueType);

char * buffer [size];



^^^^^^^^^^^^不确定这是否是标准C ++。


memcpy(缓冲区,& c [0],大小);


MPI_Send(& buffer,size,MPI_BYTE,dest,tag,comm);



为什么不把它写成:


MPI_Send(

reinterpret_cast< const char * >(& c [0]),

尺寸,MPI_BYTE,dest,tag,comm

);


....虽然你还没有处理endian问题。


}


我测试了模板化函数使用整数数组并且它可以工作

罚款。但是,当我尝试使用

boost库提供的dynamic_bitset时,我无法传递对c [0]的引用,因为operator&这个班级是私人的
私人。有没有另一种有效的方法来做一个按位

复制???



听起来你需要为各种不同的

类型做一些非常不同的事情。


通常的方法是使用特征。课程和专业。


顺便说一下,你的序列化有一些严重的缺陷,因为你已经写了它。这取决于接收者是否具有相同的字节顺序

作为寄件人。


这是我所暗示的开头。

模板< typename T>

struct serialization_traits; //未知类型的空类。


模板<>

struct serialization_traits< int:serialization_traits_pod< int {};


模板<>

struct serialization_traits< char:serialization_traits_pod< char {};


模板< typename T>

struct serialization_traits< std :: vector< T:

serialization_traits_container< std :: vector< T {};

这里,serialization_traits_pod,serialization_traits_container等

需要定义允许你这样做的方法:

模板< class StorageType>

void发送(StorageType& c,int dest,int tag,MPI_Comm comm){


typedef typename serialization_traits< StorageType> :: ValueType

ValueType;


typename serialization_traits< StorageType> :: Serializer

serializer(c);


MPI_Send(c.buffer(),c.size(),MPI_BYTE,dest,tag,comm);

}


在2007-09-03 22:48,aaragon写道:


>大家好,
我一直在尝试创建一种序列化C ++对象的简单方法,因此我可以在不同的处理器之间发送消息(使用MPI)。东西



boost mpi库已经这样做了。虽然不在1.34.1发布的

版本中,但可以从他们的svn / cvs网站上获得。


Jeff Flinn


Hello everyone,

I''ve been trying to create a simple way to serialize C++ objects so I
can send messages among different processors (using MPI). Something
very simple is shown below:

template <class StorageType>
void Send(StorageType& c, int dest, int tag, MPI_Comm comm) {

typedef typename StorageType::ValueType ValueType;

int size = c.size()*sizeof(ValueType);
char* buffer[size];
memcpy(buffer, &c[0], size);

MPI_Send(&buffer, size, MPI_BYTE, dest, tag, comm);
}

I tested the templated function with arrays of integers and it works
fine. However, when I try to use the dynamic_bitset provided by the
boost library, I cannot pass a reference to c[0] because operator& is
private for that class. Is there another efficient way to do a bitwise
copy???

Thank you all,

a2

解决方案

On 2007-09-03 22:48, aaragon wrote:

Hello everyone,

I''ve been trying to create a simple way to serialize C++ objects so I
can send messages among different processors (using MPI). Something
very simple is shown below:

template <class StorageType>
void Send(StorageType& c, int dest, int tag, MPI_Comm comm) {

typedef typename StorageType::ValueType ValueType;

int size = c.size()*sizeof(ValueType);
char* buffer[size];
memcpy(buffer, &c[0], size);

MPI_Send(&buffer, size, MPI_BYTE, dest, tag, comm);
}

I tested the templated function with arrays of integers and it works
fine. However, when I try to use the dynamic_bitset provided by the
boost library, I cannot pass a reference to c[0] because operator& is
private for that class. Is there another efficient way to do a bitwise
copy???

I don''t know about dynamic_bitset, but std::bitset has a to_string()
method that converts the content to a string of 1 and 0, if the bitset
is not to large you can still get quite good efficiency from that. There
is also a to_ulong() method to convert it to an unsigned long, but that
only works for small bitsets. I would suspect that dynamic_bitset at
least has a to_string() method.

If small buffers are important you''ll have to write code that reads the
value from the bitset and sets the correct bits in a char array, it''s
not too much work but just sending a string is a lot easier.

--
Erik Wikstr?m


aaragon wrote:

Hello everyone,

I''ve been trying to create a simple way to serialize C++ objects so I
can send messages among different processors (using MPI). Something
very simple is shown below:

template <class StorageType>
void Send(StorageType& c, int dest, int tag, MPI_Comm comm) {

why not "const StorageType&" ?

>
typedef typename StorageType::ValueType ValueType;

int size = c.size()*sizeof(ValueType);
char* buffer[size];

^^^^^^^^^^^^ not sure if this is standard C++ yet.

memcpy(buffer, &c[0], size);

MPI_Send(&buffer, size, MPI_BYTE, dest, tag, comm);

Why not just write it like:

MPI_Send(
reinterpret_cast<const char*>(&c[0]),
size, MPI_BYTE, dest, tag, comm
);

.... although you still don''t deal with endian issues.

}

I tested the templated function with arrays of integers and it works
fine. However, when I try to use the dynamic_bitset provided by the
boost library, I cannot pass a reference to c[0] because operator& is
private for that class. Is there another efficient way to do a bitwise
copy???

Sounds like you need to do somthing very different for various different
type.

Usual way to do this is using a "traits" class and specialization.

BTW, you have some serious deficiencies in your serialization as you
have written it. It depends on the receiver to have the same endianness
as the sender.

Here is the beginnings of what I am alluding to.
template <typename T>
struct serialization_traits; // empty class for unknown types.

template <>
struct serialization_traits<int: serialization_traits_pod<int{};

template <>
struct serialization_traits<char: serialization_traits_pod<char{};

template <typename T>
struct serialization_traits< std::vector<T :
serialization_traits_container< std::vector<T {};
here, serialization_traits_pod, serialization_traits_container etc will
need to define methods that then allow you to do:
template <class StorageType>
void Send(StorageType& c, int dest, int tag, MPI_Comm comm) {

typedef typename serialization_traits<StorageType>::ValueType
ValueType;

typename serialization_traits<StorageType>::Serializer
serializer(c);

MPI_Send(c.buffer(), c.size(), MPI_BYTE, dest, tag, comm);
}


On 2007-09-03 22:48, aaragon wrote:

>Hello everyone,

I''ve been trying to create a simple way to serialize C++ objects so I
can send messages among different processors (using MPI). Something

The boost mpi library does this already. While not in the 1.34.1 released
version it''s available from their svn/cvs site.

Jeff Flinn


这篇关于数组序列化的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆