473,396 Members | 1,792 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

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(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,



Sep 3 '07 #1
10 5072
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
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(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);
}
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...@please.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...@mariani.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(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);

}
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...@mariani.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...@mariani.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_traits<StorageType>::Serializer
serializer(c);

MPI_Send(serializer.buffer(), serializer.size(), MPI_BYTE, dest,
tag, comm);
Sep 4 '07 #8
On Sep 4, 5:26 am, Gianni Mariani <gi3nos...@mariani.wswrote:
Gianni Mariani wrote:
aaragon wrote:
On Sep 3, 4:28 pm, Gianni Mariani <gi3nos...@mariani.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_traits<StorageType>::Serializer
serializer(c);

MPI_Send(serializer.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.size()), 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 SerializerDynamicBitset
{

std::vector<char> m_data;

enum { bits_per_byte=8, remainder_bits_size=3 };

public:
SerializerDynamicBitset( 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

"aaragon" <al**************@gmail.comwrote in message
news:11**********************@r29g2000hsg.googlegr oups.com...
On Sep 3, 5:50 pm, "Jeff F" <norepl...@please.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.

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.
Don't you compile/link on your machine and transmit exe's to the cluster?
I'm not sure why they would need boost installed?

Jeff Flinn
Sep 6 '07 #11

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

Similar topics

3
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...
16
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> ....... ..........
3
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...
4
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...
8
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...
0
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...
0
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...
4
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...
11
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...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
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...
0
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...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
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,...

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.