MADNESS  0.10.1
Files | Classes | Macros | Functions | Variables
Collaboration diagram for Serialization:

Files

file  archive.cc
 Definitions of serialization functions.
 
file  archive.h
 Interface templates for the archives (serialization).
 
file  archive_type_names.cc
 Defines archive type names for supported (by default) types.
 
file  binary_fstream_archive.cc
 Implements an archive wrapping a binary filestream.
 
file  binary_fstream_archive.h
 Implements an archive wrapping a binary filestream.
 
file  buffer_archive.h
 Implements an archive wrapping a memory buffer.
 
file  mpi_archive.h
 Implements archives to serialize data for MPI.
 
file  parallel_archive.h
 Implements ParallelInputArchive and ParallelOutputArchive for parallel serialization of data.
 
file  text_fstream_archive.cc
 Implements an archive wrapping text filestream.
 
file  text_fstream_archive.h
 Implements an archive wrapping text filestream.
 
file  vector_archive.h
 Implements an archive wrapping an STL vector.
 

Classes

class  madness::archive::archive_array< T >
 Wrapper for dynamic arrays and pointers. More...
 
class  madness::archive::archive_ptr< T >
 Wrapper for an opaque pointer for serialization purposes. More...
 
struct  madness::archive::archive_typeinfo< T >
 Used to enable type checking inside archives. More...
 
struct  madness::archive::ArchiveImpl< Archive, T, Enabler >
 Default implementations of wrap_store and wrap_load. More...
 
struct  madness::archive::ArchiveImpl< Archive, archive_array< T > >
 Partial specialization of ArchiveImpl for archive_array. More...
 
struct  madness::archive::ArchiveImpl< Archive, T[n], std::enable_if_t<!std::is_same_v< T, char > &&is_serializable_v< Archive, T > > >
 Partial specialization of ArchiveImpl for fixed-dimension arrays that redirects to archive_array. More...
 
struct  madness::archive::ArchiveImpl< ParallelInputArchive< localarchiveT >, archive_array< T > >
 Read the archive array and broadcast. More...
 
struct  madness::archive::ArchiveImpl< ParallelInputArchive< localarchiveT >, T >
 Specialization of ArchiveImpl for parallel input archives. More...
 
struct  madness::archive::ArchiveImpl< ParallelInputArchive< localarchiveT >, T[n]>
 Forward a fixed-size array to archive_array. More...
 
struct  madness::archive::ArchiveImpl< ParallelOutputArchive< localarchiveT >, archive_array< T > >
 Write the archive array only from process zero. More...
 
struct  madness::archive::ArchiveImpl< ParallelOutputArchive< localarchiveT >, T >
 Specialization of ArchiveImpl for parallel output archives. More...
 
struct  madness::archive::ArchiveImpl< ParallelOutputArchive< localarchiveT >, T[n]>
 Forward a fixed-size array to archive_array. More...
 
struct  madness::archive::ArchiveLoadImpl< Archive, T, Enabler >
 Default load of an object via serialize(ar, t). More...
 
struct  madness::archive::ArchiveLoadImpl< Archive, std::allocator< T >, std::enable_if_t<!is_future< T >::value &&is_serializable_v< Archive, T > > >
 Deserialize a std::allocator. More...
 
struct  madness::archive::ArchiveLoadImpl< Archive, std::array< T, N >, std::enable_if_t< is_serializable_v< Archive, T > > >
 Deserialize a std::array. MADNESS_ASSERT 's that the size matches. More...
 
struct  madness::archive::ArchiveLoadImpl< Archive, std::complex< T >, std::enable_if_t< is_serializable_v< Archive, T > > >
 Deserialize a complex number. More...
 
struct  madness::archive::ArchiveLoadImpl< Archive, std::list< T, Alloc >, std::enable_if_t<!is_future< T >::value &&is_serializable_v< Archive, T > > >
 Deserialize a std::list. Clears and resizes as necessary. More...
 
struct  madness::archive::ArchiveLoadImpl< Archive, std::map< T, Q, Compare, Alloc >, std::enable_if_t< is_serializable_v< Archive, T > &&is_serializable_v< Archive, Q > > >
 Deserialize an std::map. The map is not cleared; duplicate elements are replaced. More...
 
struct  madness::archive::ArchiveLoadImpl< Archive, std::set< T, Compare, Alloc >, std::enable_if_t<!is_future< T >::value &&is_serializable_v< Archive, T > > >
 Deserialize a std::set. Clears and resizes as necessary. More...
 
struct  madness::archive::ArchiveLoadImpl< Archive, std::string >
 Deserialize a std::string. Clears and resizes as necessary. More...
 
struct  madness::archive::ArchiveLoadImpl< Archive, std::vector< bool, Alloc > >
 Deserialize a std::vector<bool>. Clears and resizes as necessary. More...
 
struct  madness::archive::ArchiveLoadImpl< Archive, std::vector< T, Alloc >, std::enable_if_t<!is_future< T >::value &&is_serializable_v< Archive, T > > >
 Deserialize a std::vector. Clears and resizes as necessary. More...
 
struct  madness::archive::ArchivePrePostImpl< Archive, T >
 Default implementation of the pre/postamble for type checking. More...
 
struct  madness::archive::ArchivePrePostImpl< BufferInputArchive, T >
 Implement pre/postamble load routines for a BufferInputArchive. More...
 
struct  madness::archive::ArchivePrePostImpl< BufferOutputArchive, T >
 Implement pre/postamble storage routines for a BufferOutputArchive. More...
 
struct  madness::archive::ArchivePrePostImpl< MPIInputArchive, T >
 Implementation of functions for loading the pre/postamble in MPI archives. More...
 
struct  madness::archive::ArchivePrePostImpl< MPIOutputArchive, T >
 Implementation of functions for storing the pre/postamble in MPI archives. More...
 
struct  madness::archive::ArchivePrePostImpl< MPIRawInputArchive, T >
 Implementation of functions for loading the pre/postamble in MPI archives. More...
 
struct  madness::archive::ArchivePrePostImpl< MPIRawOutputArchive, T >
 Implementation of functions for storing the pre/postamble in MPI archives. More...
 
struct  madness::archive::ArchivePrePostImpl< ParallelInputArchive< localarchiveT >, T >
 Disable type info for parallel input archives. More...
 
struct  madness::archive::ArchivePrePostImpl< ParallelOutputArchive< localarchiveT >, T >
 Disable type info for parallel output archives. More...
 
struct  madness::archive::ArchivePrePostImpl< TextFstreamInputArchive, T >
 Implement pre/postamble load routines for a TextFstreamInputArchive. More...
 
struct  madness::archive::ArchivePrePostImpl< TextFstreamOutputArchive, T >
 Implement pre/postamble storage routines for a TextFstreamOutputArchive. More...
 
struct  madness::archive::ArchivePrePostImpl< VectorInputArchive, T >
 Implementation of functions for loading the pre/postamble in Vector archives. More...
 
struct  madness::archive::ArchivePrePostImpl< VectorOutputArchive, T >
 Implementation of functions for storing the pre/postamble in Vector archives. More...
 
struct  madness::archive::ArchiveSerializeImpl< Archive, T, Enabler >
 Default symmetric serialization of a non-fundamental type that has serialize method. More...
 
struct  madness::archive::ArchiveSerializeImpl< Archive, resT(*)(paramT...), std::enable_if_t<!is_default_serializable_v< Archive, resT(*)(paramT...)> > >
 Serialize a function pointer. More...
 
struct  madness::archive::ArchiveSerializeImpl< Archive, resT(objT::*)(paramT...) const, std::enable_if_t<!is_default_serializable_v< Archive, resT(objT::*)(paramT...) const > > >
 Serialize a const member function pointer. More...
 
struct  madness::archive::ArchiveSerializeImpl< Archive, resT(objT::*)(paramT...), std::enable_if_t<!is_default_serializable_v< Archive, resT(objT::*)(paramT...)> > >
 Serialize a member function pointer. More...
 
struct  madness::archive::ArchiveSerializeImpl< Archive, std::optional< T >, std::enable_if_t< is_serializable_v< Archive, T > > >
 Serialize (deserialize) an std::optional. More...
 
struct  madness::archive::ArchiveSerializeImpl< Archive, std::pair< T, Q >, std::enable_if_t< is_serializable_v< Archive, T > &&is_serializable_v< Archive, Q > > >
 Serialize (deserialize) an std::pair. More...
 
struct  madness::archive::ArchiveSerializeImpl< Archive, std::tuple< Types... >, std::enable_if_t<(is_serializable_v< Archive, Types > &&...) > >
 Serialize (deserialize) a std::tuple. More...
 
struct  madness::archive::ArchiveStoreImpl< Archive, T, Enabler >
 Default store of an object via serialize(ar, t). More...
 
struct  madness::archive::ArchiveStoreImpl< Archive, std::allocator< T >, std::enable_if_t<!is_future< T >::value &&is_serializable_v< Archive, T > > >
 Serialize a std::allocator. More...
 
struct  madness::archive::ArchiveStoreImpl< Archive, std::array< T, N >, std::enable_if_t< is_serializable_v< Archive, T > > >
 Serialize a std::array. More...
 
struct  madness::archive::ArchiveStoreImpl< Archive, std::complex< T >, std::enable_if_t< is_serializable_v< Archive, T > > >
 Serialize a complex number. More...
 
struct  madness::archive::ArchiveStoreImpl< Archive, std::list< T, Alloc >, std::enable_if_t<!is_future< T >::value &&is_serializable_v< Archive, T > > >
 Serialize a std::list. More...
 
struct  madness::archive::ArchiveStoreImpl< Archive, std::map< T, Q, Compare, Alloc >, std::enable_if_t< is_serializable_v< Archive, T > &&is_serializable_v< Archive, Q > > >
 Serialize an std::map. More...
 
struct  madness::archive::ArchiveStoreImpl< Archive, std::set< T, Compare, Alloc >, std::enable_if_t<!is_future< T >::value &&is_serializable_v< Archive, T > > >
 Serialize a std::set. More...
 
struct  madness::archive::ArchiveStoreImpl< Archive, std::string >
 Serialize a 'std::string'. More...
 
struct  madness::archive::ArchiveStoreImpl< Archive, std::vector< bool, Alloc > >
 Serialize a std::vector<bool> (as a plain array of bool). More...
 
struct  madness::archive::ArchiveStoreImpl< Archive, std::vector< T, Alloc >, std::enable_if_t<!is_future< T >::value &&is_serializable_v< Archive, T > > >
 Serialize a std::vector. More...
 
class  madness::archive::BaseArchive
 Base class for all archive classes. More...
 
class  madness::archive::BaseInputArchive
 Base class for input archive classes. More...
 
class  madness::archive::BaseOutputArchive
 Base class for output archive classes. More...
 
class  madness::archive::BaseParallelArchive< Archive >
 Base class for input and output parallel archives. More...
 
class  madness::archive::BinaryFstreamInputArchive
 Wraps an archive around a binary filestream for input. More...
 
class  madness::archive::BinaryFstreamOutputArchive
 Wraps an archive around a binary filestream for output. More...
 
class  madness::archive::BufferInputArchive
 Wraps an archive around a memory buffer for input. More...
 
class  madness::archive::BufferOutputArchive
 Wraps an archive around a memory buffer for output. More...
 
class  madness::archive::MPIInputArchive
 Archive allowing buffering, deserialization of data, and point-to-point communication between processes with MPI. More...
 
class  madness::archive::MPIOutputArchive
 Archive allowing buffering, serialization of data, and point-to-point communication between processes with MPI. More...
 
class  madness::archive::MPIRawInputArchive
 Archive allowing deserialization and point-to-point communication between processes with MPI. More...
 
class  madness::archive::MPIRawOutputArchive
 Archive allowing serialization and point-to-point communication between processes with MPI. More...
 
class  madness::archive::ParallelInputArchive< localarchiveT >
 An archive for storing local or parallel data, wrapping a BinaryFstreamInputArchive. More...
 
class  madness::archive::ParallelOutputArchive< localarchiveT >
 An archive for storing local or parallel data wrapping a BinaryFstreamOutputArchive. More...
 
class  madness::archive::ParallelSerializableObject
 Objects that implement their own parallel archive interface should derive from this class. More...
 
class  madness::archive::TextFstreamInputArchive
 Wraps an archive around a text filestream for input. More...
 
class  madness::archive::TextFstreamOutputArchive
 Wraps an archive around a text filestream for output. More...
 
class  madness::archive::VectorInputArchive
 Wraps an archive around an STL vector for input. More...
 
class  madness::archive::VectorOutputArchive
 Wraps an archive around an STL vector for output. More...
 

Macros

#define ARCHIVE_REGISTER_TYPE(T, cooky)
 Used to associate a type with a cookie value inside archive. More...
 
#define ARCHIVE_REGISTER_TYPE_AND_PTR(T, cooky)
 Used to associate a type and a pointer to the type with a cookie value inside archive. More...
 
#define ARCHIVE_REGISTER_TYPE_AND_PTR_NAMES(T)
 Used to associate names with types and pointers to that type. More...
 
#define ARCHIVE_REGISTER_TYPE_NAME(T)
 Used to associate names with types. More...
 
#define ATI   ::madness::archive::archive_typeinfo
 Alias for archive_typeinfo. More...
 
#define ATN   ::madness::archive::archive_type_names
 Alias for archive_type_names. More...
 

Functions

void madness::archive::archive_initialize_type_names ()
 Initializes the type names for the archives. More...
 
template<class Archive , class T >
std::enable_if_t< is_default_serializable< Archive, T >::value &&is_archive< Archive >::value, void > madness::archive::default_serialize (const Archive &ar, const T &t)
 Redirect serialize(ar, t) to serialize(ar, &t, 1) for fundamental types. More...
 
template<class Archive , class T >
std::enable_if_t< is_output_archive< Archive >::value &&is_default_serializable< Archive, T >::value &&is_function_pointer_v< T >, void > madness::archive::default_serialize (const Archive &ar, const T *t, unsigned int n)
 Serialize an array of fundamental stuff. More...
 
template<typename T >
const char * madness::archive::get_type_name ()
 Returns the name of the type, or unknown if not registered. More...
 
template<class Archive , class T >
std::enable_if_t< is_output_archive_v< Archive >, const Archive & > madness::archive::operator& (const Archive &ar, const T &t)
 Redirect & to ArchiveImpl::wrap_store for output archives. More...
 
template<class Archive , class T >
std::enable_if_t< is_output_archive_v< Archive >, const Archive & > madness::archive::operator<< (const Archive &ar, const T &t)
 Redirect << to ArchiveImpl::wrap_store for output archives. More...
 
template<class Archive , class T >
std::enable_if_t< is_input_archive_v< Archive >, const Archive & > madness::archive::operator>> (const Archive &ar, const T &t)
 Redirect >> to ArchiveImpl::wrap_load for input archives. More...
 
template<class Archive , class T >
std::enable_if_t<(!is_default_serializable< Archive, T >::value &&has_nonmember_serialize_v< T, Archive >) &&is_archive< Archive >::value, void > madness::archive::serialize (const Archive &ar, const T &t)
 Redirect serialize(ar,t) to ArchiveSerializeImpl for non-fundamental types. More...
 
template<class Archive , class T >
std::enable_if_t< ! is_default_serializable< Archive, T >::value &&is_archive< Archive >::value, void > madness::archive::serialize (const Archive &ar, const T *t, unsigned int n)
 Serialize (or deserialize) an array of non-fundamental stuff. More...
 
template<class T >
archive_array< Tmadness::archive::wrap (const T *ptr, unsigned int n)
 Factory function to wrap a dynamically allocated pointer as a typed archive_array. More...
 
template<class T >
archive_array< unsigned char > madness::archive::wrap_opaque (const T &t)
 Factory function to wrap a contiguous scalar as an opaque (uchar) archive_array. More...
 
template<class T >
archive_array< unsigned char > madness::archive::wrap_opaque (const T *ptr, unsigned int n)
 Factory function to wrap a pointer to contiguous data as an opaque (uchar) archive_array. More...
 
template<class T >
archive_ptr< Tmadness::archive::wrap_ptr (T *p)
 Wrapper for pointers. More...
 

Variables

const char * madness::archive::archive_type_names [256]
 The list of type names for use in archives. More...
 

function pointer serialization

Note
relative function pointers are represented by std::ptrdiff_t , with member function pointers represented by std::array<std::ptrdiff_t, N> (with N=2 on most common platforms, and a type-dependent constant on some (Microsoft))
Returns
function pointer to serve as the reference for computing relative pointers
Note
the value returned by this function is a pointer to a non-virtual member function, this helps on the platforms that use the parity to distinguish non-virtual and virtual pointers (e.g. Itanium ABI)
std::ptrdiff_t madness::archive::fn_ptr_origin ()
 
template<typename T , typename = std::enable_if_t<std::is_function<T>::value || is_function_pointer<T>::value>>
std::ptrdiff_t madness::archive::to_rel_fn_ptr (const T &fn)
 converts function or (free or static member) function pointer to the relative function pointer More...
 
template<typename T , typename = std::enable_if_t<std::is_member_function_pointer<T>::value>>
auto madness::archive::to_rel_memfn_ptr (const T &fn)
 converts nonstatic member function pointer to the relative equivalent More...
 
template<typename T , typename = std::enable_if_t<is_function_pointer_v<T>>>
T madness::archive::to_abs_fn_ptr (std::ptrdiff_t rel_fn_ptr)
 converts relative free or static member function pointer to the absolute function pointer More...
 
template<typename T , std::size_t N, typename = std::enable_if_t<std::is_member_function_pointer<std::remove_reference_t<T>>::value>>
auto madness::archive::to_abs_memfn_ptr (std::array< std::ptrdiff_t, N > rel_fn_ptr)
 converts relative (nonstatic) member function pointer to the absolute function pointer More...
 

Detailed Description

The programmer should not need to include madness/world/archive.h directly. Instead, include the header file for the actual archive (binary file, text/xml file, vector in memory, etc.) that is desired.

Background

The interface and implementation are deliberately modelled, albeit loosely, upon the boost serialization class (thanks boost!). The major differences are that this archive class does not break cycles and does not automatically store unique copies of data referenced by multiple objects. Also, classes are responsbible for managing their own version information. At the lowest level, the interface to an archive also differs to facilitate vectorization and high-bandwidth data transfer. The implementation employs templates that are almost entirely inlined. This should enable low-overhead use of archives in applications, such as interprocess communication.

How to use an archive?

An archive is a uni-directional stream of typed data to/from disk, memory, or another process. Whether the stream is for input or for output, you can use the & operator to transfer data to/from the stream. If you really want, you can also use the << or >> for output or input, respectively, but there is no reason to do so. The & operator chains just like << for cout or >> for cin. You may discover in archive.h other interfaces but you should not use them — use the & operator! The lower level interfaces will probably not, or only inconsistently, incorporate type information, and may even appear to work when they are not.

Unless type checking has not been implemented by an archive for reasons of efficiency (e.g., message passing) a C-string exception will be thrown on a type-mismatch when deserializing. End-of-file, out-of-memory, and others also generate string exceptions.

Fundamental types (see below), STL complex, vector, strings, pairs and maps, and tensors (int, long, float, double, float_complex, double_complex) all work without you doing anything, as do fixed-dimension arrays of the same (STL allocators are not presently accomodated). For example,

bool finished = false;
int info[3] = {1, 33, 2};
map<int, double> fred;
fred[0] = 55.0; fred[1] = 99.0;
BinaryFstreamOutputArchive ar('restart.dat');
ar & fred & info & finished;
double fred(const coordT &r)
Definition: tdse4.cc:368

Deserializing is identical, except that you need to use an input archive, c.f.,

bool finished;
int info[3];
map<int, double> fred;
BinaryFstreamInputArchive ar('restart.dat');
ar & fred & info & finished;

Variable dimension and dynamically allocated arrays do not have their dimension encoded in their type. The best way to (de)serialize them is to wrap them in an archive_array as follows.

int a[n]; // n is not known at compile time
double *p = new double[n];
ar & wrap(a,n) & wrap(p,n);
char * p(char *buf, const char *name, int k, int initial_level, double thresh, int order)
Definition: derivatives.cc:72
archive_array< T > wrap(const T *, unsigned int)
Factory function to wrap a dynamically allocated pointer as a typed archive_array.
Definition: archive.h:913
static const double a
Definition: nonlinschro.cc:118

The wrap() function template is a factory function to simplify instantiation of a correctly typed archive_array template. Note that when deserializing, you must have first allocated the array — the above code can be used for both serializing and deserializing. If you want the memory to be automatically allocated consider using either an STL vector or a madness tensor.

To transfer the actual value of a pointer to a stream (is this really what you want?) then store an archive_ptr wrapping it. The factory function wrap_ptr() assists in doing this, e.g., here for a function pointer

int foo();
ar & wrap_ptr(foo);
archive_ptr< T > wrap_ptr(T *p)
Wrapper for pointers.
Definition: archive.h:882
User-defined types

User-defined types require a little more effort. Three cases are distinguished.

We will examine each in turn, but we first need to discuss a little about the implementation.

When transfering an object obj to/from an archive ar with ar & obj, you need to invoke the templated function

template <class Archive, class T>
inline const Archive& operator&(const Archive& ar, T& obj);
auto T(World &world, response_space &f) -> response_space
Definition: global_functions.cc:34
std::enable_if_t< is_output_archive_v< Archive >, const Archive & > operator&(const Archive &ar, const T &t)
Redirect & to ArchiveImpl::wrap_store for output archives.
Definition: archive.h:810

that then invokes other templated functions to redirect to input or output streams as appropriate, manage type checking, etc. We would now like to overload the behavior of these functions in order to accomodate your fancy object. However, function templates cannot be partially specialized. Following the technique recommended here (look for moral#2), each of the templated functions directly calls a member of a templated class. Classes, unlike functions, can be partially specialized, so it is easy to control and predict what is happening. Thus, in order to change the behavior of all archives for an object you just have to provide a partial specialization of the appropriate class(es). Do not overload any of the function templates.

Symmetric intrusive method

Many classes can use the same code for serializing and deserializing. If such a class can be modified, the cleanest way of enabling serialization is to add a templated method as follows.

class A {
float a;
public:
A(float a = 0.0) : a(a) {}
template <class Archive>
inline void serialize(const Archive& ar) {
ar & a;
}
};
real_convolution_3d A(World &world)
Definition: DKops.h:230
Definition: test_ar.cc:118
std::enable_if_t< ! is_default_serializable< Archive, T >::value &&is_archive< Archive >::value, void > serialize(const Archive &ar, const T *t, unsigned int n)
Serialize (or deserialize) an array of non-fundamental stuff.
Definition: archive.h:497

Symmetric non-intrusive method

If a class with symmetric serialization cannot be modified, then you can define an external class template with the following signature in the madness::archive namespace (where Obj is the name of your type).

namespace madness {
namespace archive {
template <class Archive>
struct ArchiveSerializeImpl<Archive,Obj> {
static inline void serialize(const Archive& ar, Obj& obj);
};
}
}
File holds all helper structures necessary for the CC_Operator and CC2 class.
Definition: DFParameters.h:10

For example,

class B {
public:
bool b;
B(bool b = false)
: b(b) {};
};
namespace madness {
namespace archive {
template <class Archive>
struct ArchiveSerializeImpl<Archive, B> {
static inline void serialize(const Archive& ar, B& b) {
ar & b.b;
};
};
}
}
Definition: test_ar.cc:141
static const double b
Definition: nonlinschro.cc:119
Tensor< double > B
Definition: tdse1d.cc:166

Non-symmetric non-intrusive

For classes that do not have symmetric (de)serialization you must define separate partial templates for the functions load and store with these signatures and again in the madness::archive namespace.

namespace madness {
namespace archive {
template <class Archive>
struct ArchiveLoadImpl<Archive, Obj> {
static inline void load(const Archive& ar, Obj& obj);
};
template <class Archive>
struct ArchiveStoreImpl<Archive, Obj> {
static inline void store(const Archive& ar, const Obj& obj);
};
}
}
void load(Function< T, NDIM > &f, const std::string name)
Definition: mra.h:2751

First a simple, but artificial example.

class C {
public:
long c;
C(long c = 0)
: c(c) {};
};
namespace madness {
namespace archive {
template <class Archive>
struct ArchiveLoadImpl<Archive, C> {
static inline void load(const Archive& ar, C& c) {
ar & c.c;
}
};
template <class Archive>
struct ArchiveStoreImpl<Archive, C> {
static inline void store(const Archive& ar, const C& c) {
ar & c.c;
}
};
}
}
Definition: test_ar.cc:170
JLOOP2 NK jnz KLOOP2 mov C
Definition: mtxm_gen.h:12
static const double c
Definition: relops.cc:10

Now a more complicated example that genuinely requires asymmetric load and store.First, a class definition for a simple linked list.

class linked_list {
int value;
linked_list *next;
public:
linked_list(int value = 0)
: value(value), next(0) {};
void append(int value) {
if (next)
next->append(value);
else
next = new linked_list(value);
};
void set_value(int val) {
value = val;
};
int get_value() const {
return value;
};
linked_list* get_next() const {
return next;
};
};
Definition: test_ar.cc:280
void append(int value)
Definition: test_ar.cc:286
std::vector< Function< T, NDIM > > append(const std::vector< Function< T, NDIM > > &lhs, const std::vector< Function< T, NDIM > > &rhs)
combine two vectors
Definition: vmra.h:649

And this is how you (de)serialize it.

namespace madness {
namespace archive {
template <class Archive>
struct ArchiveStoreImpl<Archive, linked_list> {
static void store(const Archive& ar, const linked_list& c) {
ar & c.get_value() & bool(c.get_next());
if (c.get_next())
ar & *c.get_next();
}
};
template <class Archive>
struct ArchiveLoadImpl<Archive, linked_list> {
static void load(const Archive& ar, linked_list& c) {
int value;
bool flag;
ar & value & flag;
c.set_value(value);
if (flag) {
c.append(0);
ar & *c.get_next();
}
}
};
}
}

Given the above implementation of a linked list, you can (de)serialize an entire list using a single statement.

linked_list list(0);
for (int i=1; i<=10; ++i)
list.append(i);
BinaryFstreamOutputArchive ar('list.dat');
ar & list;
Non-default constructor

There are various options for objects that do not have a default constructor. The most appealing and totally non-intrusive approach is to define load/store functions for a pointer to the object. Then in the load method you can deserialize all of the information necessary to invoke the constructor and return a pointer to a new object.

Things that you know are contiguously stored in memory and are painful to serialize with full type safety can be serialized by wrapping opaquely as byte streams using the wrap_opaque() interface. However, this should be regarded as a last resort.

Type checking and registering your own types

To enable type checking for user-defined types you must register them with the system. There are 64 empty slots for user types beginning at cookie=128. Type checked archives (currently all except the MPI archive) store a cookie (byte with value 0-255) with each datum. Unknown (user-defined) types all end up with the same cookie indicating unkown — i.e., no type checking unless you register.

Two steps are required to register your own types (e.g., here for the types Foo and Bar)

  1. In a header file, after including madness/world/archive.h, associate your types and pointers to them with cookie values.
    namespace madness {
    namespace archive {
    }
    }
    Definition: test_future3.cc:37
    ARCHIVE_REGISTER_TYPE_AND_PTR(A, 128)
  2. In a single source file containing your initialization routine, register the name of your types as follows
    #define ARCHIVE_REGISTER_TYPE_AND_PTR_NAMES(T)
    Used to associate names with types and pointers to that type.
    Definition: archive.h:170
    Have a look at the test in madness/world/test_ar.cc to see things in action.
Types of archive

Presently provided are

The buffer and vector archives are bitwise identical to the binary file archive.

Implementing a new archive

Minimally, an archive must derive from either BaseInputArchive or BaseOutputArchive and define for arrays of fundamental types either a load or store method, as appropriate. Additional methods can be provided to manipulate the target stream. Here is a simple, but functional, implementation of a binary file archive.

#include <fstream>
using namespace std;
class OutputArchive : public BaseOutputArchive {
mutable ofstream os;
public:
OutputArchive(const char* filename)
: os(filename, ios_base::binary | ios_base::out | ios_base::trunc)
{};
template <class T>
void store(const T* t, long n) const {
os.write((const char *) t, n*sizeof(T));
}
};
class InputArchive : public BaseInputArchive {
mutable ifstream is;
public:
InputArchive(const char* filename)
: is(filename, ios_base::binary | ios_base::in)
{};
template <class T>
void load(T* t, long n) const {
is.read((char *) t, n*sizeof(T));
}
};
Interface templates for the archives (serialization).
static const char * filename
Definition: legendre.cc:96
Definition: mraimpl.h:50

Macro Definition Documentation

◆ ARCHIVE_REGISTER_TYPE

#define ARCHIVE_REGISTER_TYPE (   T,
  cooky 
)
Value:
template <> \
struct archive_typeinfo< T > { \
static const unsigned char cookie = cooky; \
}

Used to associate a type with a cookie value inside archive.

Makes a specialization of archive_typeinfo for type T that specifies the correct cookie value.

Parameters
[in]TThe type.
[in]cookyThe cookie value.

◆ ARCHIVE_REGISTER_TYPE_AND_PTR

#define ARCHIVE_REGISTER_TYPE_AND_PTR (   T,
  cooky 
)
Value:
ARCHIVE_REGISTER_TYPE(T*, cooky+64)
#define ARCHIVE_REGISTER_TYPE(T, cooky)
Used to associate a type with a cookie value inside archive.
Definition: archive.h:134

Used to associate a type and a pointer to the type with a cookie value inside archive.

Parameters
[in]TThe type.
[in]cookyThe cookie value.

◆ ARCHIVE_REGISTER_TYPE_AND_PTR_NAMES

#define ARCHIVE_REGISTER_TYPE_AND_PTR_NAMES (   T)
Value:
ARCHIVE_REGISTER_TYPE_NAME(T*)
#define ARCHIVE_REGISTER_TYPE_NAME(T)
Used to associate names with types.
Definition: archive.h:159

Used to associate names with types and pointers to that type.

Parameters
[in]TThe type.

◆ ARCHIVE_REGISTER_TYPE_NAME

#define ARCHIVE_REGISTER_TYPE_NAME (   T)
Value:
if (strcmp( ATN[ATI< T >::cookie], "invalid") ) { \
std::cout << "archive_register_type_name: slot/cookie already in use! " << #T << " " << ATN[ATI< T >::cookie] << std::endl; \
MADNESS_EXCEPTION("archive_register_type_name: slot/cookie already in use!", 0); \
} \
ATN[ATI< T >::cookie] = #T
#define ATN
Alias for archive_type_names.
Definition: archive.h:151

Used to associate names with types.

Parameters
[in]TThe type.

◆ ATI

Alias for archive_typeinfo.

◆ ATN

Alias for archive_type_names.

Function Documentation

◆ archive_initialize_type_names()

void madness::archive::archive_initialize_type_names ( )

◆ default_serialize() [1/2]

template<class Archive , class T >
std::enable_if_t< is_default_serializable<Archive,T>::value && is_archive<Archive>::value, void> madness::archive::default_serialize ( const Archive &  ar,
const T t 
)
inline

Redirect serialize(ar, t) to serialize(ar, &t, 1) for fundamental types.

The function only appears (due to enable_if) if T is serializable and Archive is an archive.

Template Parameters
ArchiveThe archive type.
TThe data type.
Parameters
[in]arThe archive.
[in]tThe data to be serialized.

References madness::archive::default_serialize(), and MAD_ARCHIVE_DEBUG.

◆ default_serialize() [2/2]

template<class Archive , class T >
std::enable_if_t< is_output_archive<Archive>::value && is_default_serializable<Archive,T>::value && is_function_pointer_v<T>, void> madness::archive::default_serialize ( const Archive &  ar,
const T t,
unsigned int  n 
)

Serialize an array of fundamental stuff.

The function only appears (via enable_if) if T is serializable and Archive is an output archive.

Template Parameters
ArchiveThe archive type.
TThe type of data in the array.
Parameters
[in]arThe archive.
[in]tPointer to the start of the array.
[in]nNumber of data items to be serialized.

References MAD_ARCHIVE_DEBUG, madness::archive::to_rel_fn_ptr(), and transform().

Referenced by madness::archive::default_serialize(), madness::archive::ArchiveLoadImpl< Archive, T, Enabler >::load(), madness::archive::ArchiveStoreImpl< Archive, T, Enabler >::store(), madness::archive::ArchiveImpl< Archive, archive_array< T > >::wrap_load(), and madness::archive::ArchiveImpl< Archive, archive_array< T > >::wrap_store().

◆ fn_ptr_origin()

std::ptrdiff_t madness::archive::fn_ptr_origin ( )

◆ get_type_name()

template<typename T >
const char* madness::archive::get_type_name ( )

Returns the name of the type, or unknown if not registered.

Template Parameters
TThe data type.
Returns
The name of the type.

References madness::archive::archive_type_names.

◆ operator&()

template<class Archive , class T >
std::enable_if_t<is_output_archive_v<Archive>, const Archive&> madness::archive::operator& ( const Archive &  ar,
const T t 
)
inline

Redirect & to ArchiveImpl::wrap_store for output archives.

The function only appears (due to enable_if) if Archive is an output archive.

Template Parameters
ArchiveThe archive type.
TThe data type.
Parameters
[in]arThe archive.
[in]tThe data.

References madness::archive::ArchiveImpl< Archive, T, Enabler >::wrap_store().

◆ operator<<()

template<class Archive , class T >
std::enable_if_t<is_output_archive_v<Archive>, const Archive&> madness::archive::operator<< ( const Archive &  ar,
const T t 
)
inline

Redirect << to ArchiveImpl::wrap_store for output archives.

The function only appears (due to enable_if) if Archive is an output archive.

Template Parameters
ArchiveThe archive type.
TThe data type.
Parameters
[in]arThe archive.
[in]tThe data.

References MAD_ARCHIVE_DEBUG, madness::archive::ArchiveSerializeImpl< Archive, T, Enabler >::serialize(), and T().

◆ operator>>()

template<class Archive , class T >
std::enable_if_t<is_input_archive_v<Archive>, const Archive&> madness::archive::operator>> ( const Archive &  ar,
const T t 
)
inline

Redirect >> to ArchiveImpl::wrap_load for input archives.

The function only appears (due to enable_if) if Archive is an input archive.

Template Parameters
ArchiveThe archive type.
TThe data type.
Parameters
[in]arThe archive.
[in]tThe data.

References madness::archive::ArchiveImpl< Archive, T, Enabler >::wrap_load().

◆ serialize() [1/2]

template<class Archive , class T >
std::enable_if_t< (!is_default_serializable<Archive,T>::value && has_nonmember_serialize_v<T, Archive>) && is_archive<Archive>::value, void > madness::archive::serialize ( const Archive &  ar,
const T t 
)
inline

Redirect serialize(ar,t) to ArchiveSerializeImpl for non-fundamental types.

The function only appears (due to enable_if) if T is not serializable and Archive is an archive.

Template Parameters
ArchiveThe archive type.
TThe data type.
Parameters
[in]arThe archive.
[in]tThe data to be serialized.

◆ serialize() [2/2]

template<class Archive , class T >
std::enable_if_t< ! is_default_serializable<Archive,T>::value && is_archive<Archive>::value, void> madness::archive::serialize ( const Archive &  ar,
const T t,
unsigned int  n 
)

Serialize (or deserialize) an array of non-fundamental stuff.

The function only appears (via enable_if) if T is not serializable and Archive is an archive.

Template Parameters
ArchiveThe archive type.
TThe type of data in the array.
Parameters
[in]arThe archive.
[in]tPointer to the start of the array.
[in]nNumber of data items to be serialized.

References MAD_ARCHIVE_DEBUG.

Referenced by madness::archive::ArchiveLoadImpl< Archive, T, Enabler >::load(), madness::archive::ArchiveStoreImpl< Archive, T, Enabler >::store(), madness::archive::ArchiveImpl< Archive, archive_array< T > >::wrap_load(), and madness::archive::ArchiveImpl< Archive, archive_array< T > >::wrap_store().

◆ to_abs_fn_ptr()

template<typename T , typename = std::enable_if_t<is_function_pointer_v<T>>>
T madness::archive::to_abs_fn_ptr ( std::ptrdiff_t  rel_fn_ptr)

converts relative free or static member function pointer to the absolute function pointer

Parameters
[in]fna relative function pointer
Returns
absolute function pointer
See also
to_rel_fn_ptr

References madness::archive::fn_ptr_origin(), and T().

Referenced by madness::archive::ArchiveSerializeImpl< Archive, resT(*)(paramT...), std::enable_if_t<!is_default_serializable_v< Archive, resT(*)(paramT...)> > >::serialize().

◆ to_abs_memfn_ptr()

template<typename T , std::size_t N, typename = std::enable_if_t<std::is_member_function_pointer<std::remove_reference_t<T>>::value>>
auto madness::archive::to_abs_memfn_ptr ( std::array< std::ptrdiff_t, N rel_fn_ptr)

converts relative (nonstatic) member function pointer to the absolute function pointer

Parameters
[in]fna relative function pointer
Returns
absolute function pointer
See also
to_rel_memfn_ptr
Note
see notes in to_rel_memfn_ptr re: Microsoft ABI

References madness::archive::fn_ptr_origin().

◆ to_rel_fn_ptr()

template<typename T , typename = std::enable_if_t<std::is_function<T>::value || is_function_pointer<T>::value>>
std::ptrdiff_t madness::archive::to_rel_fn_ptr ( const T fn)

converts function or (free or static member) function pointer to the relative function pointer

Parameters
[in]fna function or function pointer

References madness::archive::fn_ptr_origin(), and T().

Referenced by madness::archive::default_serialize(), madness::archive::ArchiveSerializeImpl< Archive, resT(*)(paramT...), std::enable_if_t<!is_default_serializable_v< Archive, resT(*)(paramT...)> > >::serialize(), and madness::AmArg::set_func().

◆ to_rel_memfn_ptr()

template<typename T , typename = std::enable_if_t<std::is_member_function_pointer<T>::value>>
auto madness::archive::to_rel_memfn_ptr ( const T fn)

◆ wrap()

template<class T >
archive_array< T > madness::archive::wrap ( const T ptr,
unsigned int  n 
)
inline

Factory function to wrap a dynamically allocated pointer as a typed archive_array.

Template Parameters
TThe data type.
Parameters
[in]ptrThe pointer.
[in]nThe number of data elements in the array.
Returns
The wrapped pointer.

Referenced by madness::archive::ArchiveLoadImpl< Archive, Key< NDIM > >::load(), madness::archive::ArchiveLoadImpl< Archive, std::array< T, N >, std::enable_if_t< is_serializable_v< Archive, T > > >::load(), madness::archive::ArchiveLoadImpl< Archive, std::string >::load(), madness::archive::ArchiveLoadImpl< Archive, std::vector< bool, Alloc > >::load(), madness::archive::ArchiveLoadImpl< Archive, std::vector< T, Alloc >, std::enable_if_t<!is_future< T >::value &&is_serializable_v< Archive, T > > >::load(), madness::archive::ArchiveLoadImpl< Archive, GenTensor< T > >::load(), madness::archive::ArchiveLoadImpl< Archive, Tensor< T > >::load(), madness::archive::ArchiveLoadImpl< BinaryFstreamInputArchive, Key< NDIM > >::load(), InputParameters::serialize(), madness::KeyChildIterator< NDIM >::serialize(), madness::SRConf< T >::serialize(), madness::TensorTrain< T >::serialize(), madness::archive::ArchiveStoreImpl< Archive, Key< NDIM > >::store(), madness::archive::ArchiveStoreImpl< Archive, std::array< T, N >, std::enable_if_t< is_serializable_v< Archive, T > > >::store(), madness::archive::ArchiveStoreImpl< Archive, std::string >::store(), madness::archive::ArchiveStoreImpl< Archive, std::vector< bool, Alloc > >::store(), madness::archive::ArchiveStoreImpl< Archive, std::vector< T, Alloc >, std::enable_if_t<!is_future< T >::value &&is_serializable_v< Archive, T > > >::store(), madness::archive::ArchiveStoreImpl< Archive, GenTensor< T > >::store(), madness::archive::ArchiveStoreImpl< Archive, Tensor< T > >::store(), test_in(), test_out(), madness::archive::ArchiveImpl< Archive, T[n], std::enable_if_t<!std::is_same_v< T, char > &&is_serializable_v< Archive, T > > >::wrap_load(), madness::archive::ArchiveImpl< ParallelInputArchive< localarchiveT >, T[n]>::wrap_load(), madness::archive::ArchiveImpl< Archive, T[n], std::enable_if_t<!std::is_same_v< T, char > &&is_serializable_v< Archive, T > > >::wrap_store(), and madness::archive::ArchiveImpl< ParallelOutputArchive< localarchiveT >, T[n]>::wrap_store().

◆ wrap_opaque() [1/2]

template<class T >
archive_array< unsigned char > madness::archive::wrap_opaque ( const T t)
inline

Factory function to wrap a contiguous scalar as an opaque (uchar) archive_array.

Template Parameters
TThe data type.
Parameters
[in]tThe data.
Returns
The wrapped data.

◆ wrap_opaque() [2/2]

template<class T >
archive_array< unsigned char > madness::archive::wrap_opaque ( const T ptr,
unsigned int  n 
)
inline

◆ wrap_ptr()

template<class T >
archive_ptr<T> madness::archive::wrap_ptr ( T p)
inline

Wrapper for pointers.

Template Parameters
TThe type of object being pointed to.
Parameters
[in]pThe pointer.
Returns
The wrapped pointer.

References p().

Variable Documentation

◆ archive_type_names

const char * madness::archive::archive_type_names