473,839 Members | 1,358 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

serialization of arrays

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(StorageTyp e& c, int dest, int tag, MPI_Comm comm) {

typedef typename StorageType::Va lueType ValueType;

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

MPI_Send(&buffe r, 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,



Sep 3 '07 #1
10 5125
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(StorageTyp e& c, int dest, int tag, MPI_Comm comm) {

typedef typename StorageType::Va lueType ValueType;

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

MPI_Send(&buffe r, 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
Sep 3 '07 #2
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(StorageTyp e& c, int dest, int tag, MPI_Comm comm) {
why not "const StorageType&" ?
>
typedef typename StorageType::Va lueType 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(&buffe r, size, MPI_BYTE, dest, tag, comm);
Why not just write it like:

MPI_Send(
reinterpret_cas t<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_t raits; // empty class for unknown types.

template <>
struct serialization_t raits<int: serialization_t raits_pod<int{} ;

template <>
struct serialization_t raits<char: serialization_t raits_pod<char{ };

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

typedef typename serialization_t raits<StorageTy pe>::ValueType
ValueType;

typename serialization_t raits<StorageTy pe>::Serializer
serializer(c);

MPI_Send(c.buff er(), c.size(), MPI_BYTE, dest, tag, comm);
}
Sep 3 '07 #3
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
Sep 3 '07 #4
On Sep 3, 5:50 pm, "Jeff F" <norepl...@plea se.comwrote:
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
I could use the boost library in my machine. However, I doubt that the
cluster I will run the parallel program on will have the latest
version of the boost library.

Sep 3 '07 #5
On Sep 3, 4:28 pm, Gianni Mariani <gi3nos...@mari ani.wswrote:
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(StorageTyp e& c, int dest, int tag, MPI_Comm comm) {

why not "const StorageType&" ?
typedef typename StorageType::Va lueType 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(&buffe r, size, MPI_BYTE, dest, tag, comm);

Why not just write it like:

MPI_Send(
reinterpret_cas t<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_t raits; // empty class for unknown types.

template <>
struct serialization_t raits<int: serialization_t raits_pod<int{} ;

template <>
struct serialization_t raits<char: serialization_t raits_pod<char{ };

template <typename T>
struct serialization_t raits< std::vector<T :
serialization_t raits_container < std::vector<T {};

here, serialization_t raits_pod, serialization_t raits_container etc will
need to define methods that then allow you to do:

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

typedef typename serialization_t raits<StorageTy pe>::ValueType
ValueType;

typename serialization_t raits<StorageTy pe>::Serializer
serializer(c);

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

}
I could create a traits class as you suggested. It is just that I
thought that there was a faster approach to something that looks so
simple. I just needed to create a buffer to pass to the MPI function
but it didn't work with the bitset data structure. I still don't know
how to deal with the bitset since I cannot have a reference pointing
to the first boolean in the bitset so I guess I need to do some kind
of temporary copy anyways.
By the way, the StorageType could be anything, even raw arrays of any
type so functions buffer() and size() don't exist.

Sep 3 '07 #6
aaragon wrote:
On Sep 3, 4:28 pm, Gianni Mariani <gi3nos...@mari ani.wswrote:
....
By the way, the StorageType could be anything, even raw arrays of any
type so functions buffer() and size() don't exist.
I think you need to look at it a little closer. buffer() and size() are
called on an object that is used to convert StorageType to a
serializable buffer and subsequent size.

Also, if StorageType is a float*, what is the size ?
Sep 4 '07 #7
Gianni Mariani wrote:
aaragon wrote:
>On Sep 3, 4:28 pm, Gianni Mariani <gi3nos...@mari ani.wswrote:

...
>By the way, the StorageType could be anything, even raw arrays of any
type so functions buffer() and size() don't exist.


I think you need to look at it a little closer. buffer() and size() are
called on an object that is used to convert StorageType to a
serializable buffer and subsequent size.

Also, if StorageType is a float*, what is the size ?
And I need to type it correctly.

typename serialization_t raits<StorageTy pe>::Serializer
serializer(c);

MPI_Send(serial izer.buffer(), serializer.size (), MPI_BYTE, dest,
tag, comm);
Sep 4 '07 #8
On Sep 4, 5:26 am, Gianni Mariani <gi3nos...@mari ani.wswrote:
Gianni Mariani wrote:
aaragon wrote:
On Sep 3, 4:28 pm, Gianni Mariani <gi3nos...@mari ani.wswrote:
...
By the way, the StorageType could be anything, even raw arrays of any
type so functions buffer() and size() don't exist.
I think you need to look at it a little closer. buffer() and size() are
called on an object that is used to convert StorageType to a
serializable buffer and subsequent size.
Also, if StorageType is a float*, what is the size ?

And I need to type it correctly.

typename serialization_t raits<StorageTy pe>::Serializer
serializer(c);

MPI_Send(serial izer.buffer(), serializer.size (), MPI_BYTE, dest,
tag, comm);
Ok, got the point, but all this still doesn't solve my original
problem, which was to send a boost::dynamic_ bitset using MPI. I will
start a traits class and I guess that I could send it as a string
since the dynamic_bitset supports a function that creates a string
with the boolean values. I'll go back to you guys later. Thanks for
answering.

Sep 5 '07 #9
aaragon wrote:
....
Ok, got the point, but all this still doesn't solve my original
problem, which was to send a boost::dynamic_ bitset using MPI. I will
start a traits class and I guess that I could send it as a string
since the dynamic_bitset supports a function that creates a string
with the boolean values. I'll go back to you guys later. Thanks for
answering.
You will need to "serialize" the bitset which has two components -
number of bits and the bits themselves. Since you can only output
multiples of 8 bits you need to send not only the size in bytes
(serializer.siz e()), you also need to send the number of significant
bits in the "remainder byte".

e.g.

size=1
bits=01010101
^^^ number of bits in the last byte.
this would be the bit sequence "01010".
ex. 2.

size=0
bytes=

no bits

size=2
bits= 10010100 00001111
^^^ number of bits to use in last byte

would mean: 111110010
class SerializerDynam icBitset
{

std::vector<cha r> m_data;

enum { bits_per_byte=8 , remainder_bits_ size=3 };

public:
SerializerDynam icBitset( const boost::dynamic_ bitset & i_data )
: m_data(
(i_data.size() + remainder_bits_ size + bits_per_byte -1)/
bits_per_byte
)
{
m_data[ 0 ] = i_data.size() % bits_per_byte;

unsigned char l_val = 0x08; // bit location

... now iterate through the bit set popping bits from
... the input and pushing it into the m_data array
}

const char * buffer() const
{
return & m_data[0];
}

const size_t size()
{
return m_data.size();
}
};

.... you'll need a corresponding deserializer function.
Sep 5 '07 #10

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

3
3079
by: Franz | last post by:
Let me describe the flow of my program first. 1. Deserialize data from xml file. 2. Addition of "PersonType" class to the AllPersonalData. 3. Serialize data back to the xml file. My question is I have to use an array PersonType in the AllPersonalData class. But if I have to implement the "Addition of PersonalType" (i.e. the step 2), I have to use ArrayList instead of a fixed length array. I know that I can use the ArrayList as an...
16
9558
by: Bob Rock | last post by:
Hello, when serializing an array of elements of a class Classname using XmlSerializer.Serialize() I get an XML like the following: <?xml version="1.0"> <ArrayOfClassname> ....... ....... </ArrayOfClassname>
3
2232
by: Ice | last post by:
All - I'm pretty comfortable with simple XML serialization of objects. However I observed something the other day and I wanted to know if I solved it the right way. Basically if I have a string which contains a single xml node, I can serialize with this line of code: object = (object)serializer.Deserialize(new
4
8717
by: hs | last post by:
Hi I am serializing a dataset using a binary formatter as follows: IFormatter formater = new BinaryFormatter(); formatter.Serialize(stream, ds); // ds=DataSet, stream=MemoryStream .... DataSet ds2 = (DataSet)formatter2.Deserialize(stream2); For the size of my DataSet, its taking 0.8 seconds to serialize and 2.3 seconds to deserialize.
8
2308
by: Pavils Jurjans | last post by:
Hello, I have been developing an Ajax-style framework for couple of years now. Now I am reworking some parts of it. The problem was that I used to use JSON for JavaScript value serialization/deserialization, but this approach has some limitations when it comes to large data - the JavaScript at the client side has too much load working with it. So, I thought, I'd rework the serialization to XML format. The idea is short-as-possible...
0
3038
by: ronnotel | last post by:
I have integrated APIs from a third party into my framework. The third party is essentially a .Net wrapper on top of an existing set of C++ classes and structs. I want to pass-around objects in these native structures as much as possible - that is I don't want to fool around with object serialization/deserialization. Data rates approach several hundred thousand objects per second, hence I simply can't pay the performance penalty this...
0
5934
by: crazyone | last post by:
I've got a gaming framework i'm building and i want to save myself the trouble of reading and writting the complete game data to a custom file and load/save it to an XML file but i'm getting problem serializing my stuff to XML when it comes to collections. I'm currently using .net2 with generic lists to prevent users putting all sorts of stuff in the arrays (Although im sure i'll be the only user of the classes but not the game, anyway)....
4
4820
by: Sahar | last post by:
Hi there, I m trying to return an object (of my own written class) from a web service that contains jagged Arrays as public variables. Asp.Net is showing me the its serialized version on the browser when i invoke the service during test. Code: public class returnType {
11
3705
by: William | last post by:
I'm looking for an example that would show how to serialize a c++ object at it's simplest w/o using any other api's. I have a class that I want to serialize and then pass to my obj-c class so I can send it over the wire. I'm just looking for how to serialize it, then pack it back up on the other end. Any help much appreciated.
0
9855
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9697
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10587
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
10649
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
9426
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
7829
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
7018
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5682
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
3
3136
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.