[PATCH] Add MPI serializer
Jeffrey D. Oldham
oldham at codesourcery.com
Mon Jan 5 21:37:50 UTC 2004
Richard Guenther wrote:
> Hi!
>
> This patch adds the serializer for MPI messaging. This is basically a
> stripped down version of Cheetahs MatchingHandler/Serialize.h. I omitted
> all traces of Cheetah::DELEGATE mechanism which we don't use.
>
> Ok?
Please see the interspersed comments below.
> Richard.
>
>
> 2004Jan02 Richard Guenther <richard.guenther at uni-tuebingen.de>
>
> * src/Tulip/CheetahSerialize.h: new file.
> src/Tulip/Messaging.h: include it, if POOMA_MPI.
>
> --- /home/richard/src/pooma/cvs/r2/src/Tulip/Messaging.h 2003-12-25 12:26:35.000000000 +0100
> +++ Tulip/Messaging.h 2004-01-02 00:40:16.000000000 +0100
> @@ -49,7 +49,12 @@
> // Includes:
> //-----------------------------------------------------------------------------
>
> -#include "Pooma/Pooma.h"
> +#include "Pooma/Configuration.h"
> +
> +#if POOMA_MPI
> +# include "Tulip/CheetahSerialize.h"
> +# include <mpi.h>
> +#endif
>
> #if POOMA_CHEETAH
> # include "Cheetah/Cheetah.h"
> @@ -254,6 +259,6 @@
> // ACL:rcsinfo
> // ----------------------------------------------------------------------
> // $RCSfile: Messaging.h,v $ $Author: pooma $
> -// $Revision: 1.8 $ $Date: 2003/12/25 11:26:35 $
> +// $Revision: 1.7 $ $Date: 2003/10/21 18:47:59 $
> // ----------------------------------------------------------------------
> // ACL:rcsinfo
> #ifndef CHEETAH_MATCHINGHANDLER_SERIALIZE_H
> #define CHEETAH_MATCHINGHANDLER_SERIALIZE_H
>
> //-----------------------------------------------------------------------------
> // Classes:
> // Cheetah
> // Serialize<Tag, T>
> //-----------------------------------------------------------------------------
>
> //-----------------------------------------------------------------------------
> // Overview:
> //
> // Serialize is a simple class that serializes/unserializes items to/from
> // a buffer. It can be partially specialized for different types T,
> // or for different general tags Tag. Provided tags are:
> //
> // 1. 'CHEETAH' is a simple tag type for the default case used by other parts
> // of Cheetah. Objects are instantiated in place in the provided buffer.
Where is number 2?
> // 3. 'ARRAY' serializes arrays. API changes a little from other
> // serialize tags as array length must be provided in serialize methods.
> // Objects are instantiated in place in the provided buffer.
> //
> //-----------------------------------------------------------------------------
>
> //-----------------------------------------------------------------------------
> // Include Files:
> //-----------------------------------------------------------------------------
>
> #include <new>
> #include <string>
>
>
> namespace Cheetah {
>
> //----------------------------------------------------------------------
> //
> // class Serialize
> //
> // Serialize is a class that can be specialized to pack and unpack
> // items of type T to/from a provided buffer of bytes. It is used by
> // the MatchingHandler to prepare and use data sent between MatchingHandler
> // send and request calls. It has two template parameters: a tag, and a data
> // type. The tag can be used to specialize to different categories of
> // serialize operations; the data type indicates the type of data that
> // will be packed or unpacked.
> //
> // Serialize specializations should define the following four static
> // functions:
> //
> // // Return the storage needed to pack the item of type T
> // static int size(const T &item);
> //
> // // Pack an item of type T into the given buffer. Return space used.
> // static int pack(const T &item, char *buffer);
> //
> // // Unpack an item of type T from the given buffer. Set the given
> // // pointer to point at this item. Return bytes unpacked.
> // static int unpack(T* &p, char *buffer);
> //
> // // Delete the item pointed to by the given pointer, that was
> // // unpacked with a previous call to unpack().
> // static void cleanup(T *p);
> //
> // There is a general template for this class that does nothing,
> // one specialization for a tag 'CHEETAH'.
> //
> //----------------------------------------------------------------------
>
>
> //----------------------------------------------------------------------
> // Returns padding necessary for word alignment.
> //----------------------------------------------------------------------
> static inline int padding(int size)
> {
> int extra = size % sizeof(void*);
> return (extra == 0) ? 0 : sizeof(void*) - extra;
> }
>
>
> //----------------------------------------------------------------------
> // CHEETAH serialize specialization
> //----------------------------------------------------------------------
>
> // The general tag type used to specialize Serialize later.
>
> struct CHEETAH
> {
> inline CHEETAH() { }
> inline ~CHEETAH() { }
> };
>
>
> // The general template, that does nothing.
>
> template<class Tag, class T>
> class Serialize { };
>
>
> // A specialization for the CHEETAH tag that provides some default ability
> // to pack items.
>
> template<class T>
> class Serialize< ::Cheetah::CHEETAH, T>
> {
> public:
> // Return the storage needed to pack the item of type T.
> // For the default case, this is just sizeof(T), but perhaps rounded
> // up to be pointer-word-size aligned.
>
> static inline int size(const T &)
> {
Remove the extra blank line.
>
> return sizeof(double) * ((sizeof(T) + sizeof(double) - 1) / sizeof(double));
> /*
> const int off = sizeof(T) % sizeof(void *);
> return (sizeof(T) + (off == 0 ? 0 : sizeof(void *) - off));
> */
Why have the commented out code?
> }
>
> // Pack an item of type T into the given buffer. Return space used.
> // By default, this just does a placement-new into the buffer,
> // assuming the storage required is sizeof(T).
>
> static inline int pack(const T &item, char *buffer)
> {
> new ((void*)buffer) T(item);
> return size(item);
> }
>
> // Unpack an item of type T from the given buffer. Set the given
> // pointer to point at this item. Return bytes unpacked.
> // By default, this just recasts the current buffer pointer.
>
> static inline int unpack(T* &p, char *buffer)
> {
> p = reinterpret_cast<T *>(buffer);
> return size(*p);
> }
>
> // Delete the item pointed to by the given pointer, that was
> // unpacked with a previous call to unpack().
> // By default, this just runs the destructor on the data, which for
> // many things will do nothing.
>
> static inline void cleanup(T *p)
> {
> p->~T();
> }
> };
>
>
> //----------------------------------------------------------------------
> // ARRAY serialize specialization
> //----------------------------------------------------------------------
>
> struct ARRAY
> {
> inline ARRAY() { }
> inline ~ARRAY() { }
> };
>
>
> // A specialization for the POINTER tag that provides marshaling of
> // arrays.
>
> template<class T>
> class Serialize< ::Cheetah::ARRAY, T>
> {
> public:
>
> // Return the storage needed to pack count items of type T,
> // This includes the bytes needed to store the size of the array.
>
> static inline int size(const T* items, const int& count)
> {
> int arraySize = count*sizeof(T);
> return ( Serialize<CHEETAH, int>::size(count)
> + arraySize + padding(arraySize) );
> }
>
> // Pack an item of type T into the given buffer. Return space used.
> // By default, this just does a placement-new into the buffer,
> // assuming the storage required is sizeof(T).
>
> static inline int pack(const T* items, char* buffer, const int& count)
> {
> int n = Serialize<CHEETAH, int>::pack(count, buffer);
> memcpy(n+buffer, items, count*sizeof(T));
> return size(items, count);
> }
>
> // Unpack an item of type T from the given buffer. Set the given
> // pointer to point at this item. Return bytes unpacked.
>
> static inline int unpack(T* &p, char *buffer, int& count)
> {
> int* iPtr;
> int n = Serialize<CHEETAH, int>::unpack(iPtr, buffer);
> count = *iPtr;
> p = reinterpret_cast<T *>(n+buffer);
> return size(p, count);
> }
>
> // Delete the item pointed to by the given pointer, that was unpacked with a
> // previous call to unpack(). By default, this just runs the destructor on
> // the data, which for many things will do nothing. Memory has been
> // allocated from the provided buffer so no freeing of memory need be done
> // here.
>
> static inline void cleanup(T *p)
> {
> p->~T();
> }
> };
>
>
> //
> // This class is used so that serialization routines can be specialized
> // for either delegation (WrappedBool<true>) or CHEETAH
> // (WrappedBool<false>).
> //
>
> template<bool flag> class WrappedBool
> {
> public:
> WrappedBool() {}
> ~WrappedBool() {}
> };
>
> } // namespace Cheetah
>
> #endif // CHEETAH_MATCHINGHANDLER_SERIALIZE_H
--
Jeffrey D. Oldham
oldham at codesourcery.com
More information about the pooma-dev
mailing list