MADNESS 0.10.1
buffer_archive.h
Go to the documentation of this file.
1/*
2 This file is part of MADNESS.
3
4 Copyright (C) 2007,2010 Oak Ridge National Laboratory
5
6 This program is free software; you can redistribute it and/or modify
7 it under the terms of the GNU General Public License as published by
8 the Free Software Foundation; either version 2 of the License, or
9 (at your option) any later version.
10
11 This program is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 GNU General Public License for more details.
15
16 You should have received a copy of the GNU General Public License
17 along with this program; if not, write to the Free Software
18 Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
19
20 For more information please contact:
21
22 Robert J. Harrison
23 Oak Ridge National Laboratory
24 One Bethel Valley Road
25 P.O. Box 2008, MS-6367
26
27 email: harrisonrj@ornl.gov
28 tel: 865-241-3937
29 fax: 865-572-0680
30*/
31
32#ifndef MADNESS_WORLD_BUFFER_ARCHIVE_H__INCLUDED
33#define MADNESS_WORLD_BUFFER_ARCHIVE_H__INCLUDED
34
35/**
36 \file buffer_archive.h
37 \brief Implements an archive wrapping a memory buffer.
38 \ingroup serialization
39*/
40
41#include <type_traits>
43#include <madness/world/print.h>
44#include <cstring>
45
46namespace madness {
47 namespace archive {
48
49 /// \addtogroup serialization
50 /// @{
51
52 /// Wraps an archive around a memory buffer for output.
53
54 /// \note Type checking is disabled for efficiency.
55 ///
56 /// \throw madness::MadnessException in case of buffer overflow.
57 ///
58 /// The default constructor can also be used to count stuff.
60 private:
61 unsigned char * const ptr; ///< The memory buffer.
62 const std::size_t nbyte; ///< Buffer size.
63 mutable std::size_t i; /// Current output location.
64 bool countonly; ///< If true just count, don't copy.
65
66 public:
67 /// Default constructor; the buffer will only count data.
69 : ptr(nullptr), nbyte(0), i(0), countonly(true) {}
70
71 /// Constructor that assigns a buffer.
72
73 /// \param[in] ptr Pointer to the buffer.
74 /// \param[in] nbyte Size of the buffer.
75 BufferOutputArchive(void* ptr, std::size_t nbyte)
76 : ptr((unsigned char *) ptr), nbyte(nbyte), i(0), countonly(false) {}
77
78 /// Stores (counts) data into the memory buffer.
79
80 /// The function only appears (due to \c enable_if) if \c T is
81 /// serializable.
82 /// \tparam T Type of the data to be stored (counted).
83 /// \param[in] t Pointer to the data to be stored (counted).
84 /// \param[in] n Size of data to be stored (counted).
85 template <typename T>
86 inline
87 typename std::enable_if< madness::is_trivially_serializable<T>::value, void >::type
88 store(const T* t, long n) const {
89 std::size_t m = n*sizeof(T);
90 if (countonly) {
91 i += m;
92 }
93 else if (i+m > nbyte) {
94 madness::print("BufferOutputArchive:ptr,nbyte,i,n,m,i+m:",(void *)ptr,nbyte,i,n,m,i+m);
96 }
97 else {
98MADNESS_PRAGMA_GCC(diagnostic push)
99MADNESS_PRAGMA_GCC(diagnostic ignored "-Wmaybe-uninitialized")
100 memcpy(ptr+i, t, m);
101MADNESS_PRAGMA_GCC(diagnostic pop)
102 i += m;
103 }
104 }
105
106 /// Open a buffer with a specific size.
107 void open(std::size_t /*hint*/) {}
108
109 /// Close the archive.
110 void close() {}
111
112 /// Flush the archive.
113 void flush() {}
114
115 /// Determine if this buffer is used for counting.
116
117 /// \return True if this buffer is only used for counting.
118 bool count_only() const { return countonly; }
119
120 /// Return the amount of data stored (counted) in the buffer.
121
122 /// \return The amount of data stored (counted) in the buffer.
123 inline std::size_t size() const {
124 return i;
125 };
126 };
127
128
129 /// Wraps an archive around a memory buffer for input.
130
131 /// \note Type checking is disabled for efficiency.
132 ///
133 /// \throw madness::MadnessException in case of buffer overrun.
135 private:
136 const unsigned char* const ptr; ///< The memory buffer.
137 const std::size_t nbyte; ///< Buffer size.
138 mutable std::size_t i; ///< Current input location.
139
140 public:
141 /// Constructor that assigns a buffer.
142
143 /// \param[in] ptr Pointer to the buffer.
144 /// \param[in] nbyte Size of the buffer.
145 BufferInputArchive(const void* ptr, std::size_t nbyte)
146 : ptr((const unsigned char *) ptr), nbyte(nbyte), i(0) {};
147
148 /// Reads data from the memory buffer.
149
150 /// The function only appears (due to \c enable_if) if \c T is
151 /// serializable.
152 /// \tparam T Type of the data to be read.
153 /// \param[out] t Where to store the read data.
154 /// \param[in] n Size of data to be read.
155 template <class T>
156 inline
157 typename std::enable_if< madness::is_trivially_serializable<T>::value, void >::type
158 load(T* t, long n) const {
159 std::size_t m = n*sizeof(T);
161 memcpy((unsigned char*) t, ptr+i, m);
162 i += m;
163 }
164
165 /// Open the archive.
166 void open() {};
167
168 /// Reset the read location to the beginning of the buffer.
169 void rewind() const {
170 i=0;
171 };
172
173 /// Get the amount of space yet to be read from the buffer.
174
175 /// \return The amount of space yet to be read from the buffer.
176 std::size_t nbyte_avail() const {
177 return nbyte-i;
178 };
179
180 /// Close the archive.
181 void close() {}
182 };
183
184 /// Implement pre/postamble storage routines for a \c BufferOutputArchive.
185
186 /// \note No type checking over the buffer stream, for efficiency.
187 /// \tparam T The type to be stored.
188 template <class T>
190 /// Write the preamble to the archive.
191 static inline void preamble_store(const BufferOutputArchive& /*ar*/) {}
192
193 /// Write the postamble to the archive.
194 static inline void postamble_store(const BufferOutputArchive& /*ar*/) {}
195 };
196
197 /// Implement pre/postamble load routines for a \c BufferInputArchive.
198
199 /// \note No type checking over \c Buffer stream, for efficiency.
200 /// \tparam T The type to be loaded.
201 template <class T>
203 /// Load the preamble.
204 static inline void preamble_load(const BufferInputArchive& /*ar*/) {}
205
206 /// Load the postamble.
207 static inline void postamble_load(const BufferInputArchive& /*ar*/) {}
208 };
209
210 /// @}
211 }
212}
213#endif // MADNESS_WORLD_BUFFER_ARCHIVE_H__INCLUDED
Interface templates for the archives (serialization).
Base class for input archive classes.
Definition archive.h:374
Base class for output archive classes.
Definition archive.h:382
Wraps an archive around a memory buffer for input.
Definition buffer_archive.h:134
const std::size_t nbyte
Buffer size.
Definition buffer_archive.h:137
BufferInputArchive(const void *ptr, std::size_t nbyte)
Constructor that assigns a buffer.
Definition buffer_archive.h:145
std::size_t i
Current input location.
Definition buffer_archive.h:138
void close()
Close the archive.
Definition buffer_archive.h:181
const unsigned char *const ptr
The memory buffer.
Definition buffer_archive.h:136
void rewind() const
Reset the read location to the beginning of the buffer.
Definition buffer_archive.h:169
void open()
Open the archive.
Definition buffer_archive.h:166
std::enable_if< madness::is_trivially_serializable< T >::value, void >::type load(T *t, long n) const
Reads data from the memory buffer.
Definition buffer_archive.h:158
std::size_t nbyte_avail() const
Get the amount of space yet to be read from the buffer.
Definition buffer_archive.h:176
Wraps an archive around a memory buffer for output.
Definition buffer_archive.h:59
BufferOutputArchive()
Default constructor; the buffer will only count data.
Definition buffer_archive.h:68
std::size_t i
Definition buffer_archive.h:63
bool count_only() const
Determine if this buffer is used for counting.
Definition buffer_archive.h:118
void open(std::size_t)
Open a buffer with a specific size.
Definition buffer_archive.h:107
const std::size_t nbyte
Buffer size.
Definition buffer_archive.h:62
bool countonly
Current output location.
Definition buffer_archive.h:64
void close()
Close the archive.
Definition buffer_archive.h:110
BufferOutputArchive(void *ptr, std::size_t nbyte)
Constructor that assigns a buffer.
Definition buffer_archive.h:75
unsigned char *const ptr
The memory buffer.
Definition buffer_archive.h:61
std::enable_if< madness::is_trivially_serializable< T >::value, void >::type store(const T *t, long n) const
Stores (counts) data into the memory buffer.
Definition buffer_archive.h:88
std::size_t size() const
Return the amount of data stored (counted) in the buffer.
Definition buffer_archive.h:123
void flush()
Flush the archive.
Definition buffer_archive.h:113
auto T(World &world, response_space &f) -> response_space
Definition global_functions.cc:34
#define MADNESS_PRAGMA_GCC(x)
Definition madness_config.h:205
#define MADNESS_ASSERT(condition)
Assert a condition that should be free of side-effects since in release builds this might be a no-op.
Definition madness_exception.h:134
Namespace for all elements and tools of MADNESS.
Definition DFParameters.h:10
static double pop(std::vector< double > &v)
Definition SCF.cc:113
void print(const T &t, const Ts &... ts)
Print items to std::cout (items separated by spaces) and terminate with a new line.
Definition print.h:225
std::string type(const PairType &n)
Definition PNOParameters.h:18
Defines simple templates for printing to std::cout "a la Python".
static const double m
Definition relops.cc:9
static void preamble_load(const BufferInputArchive &)
Load the preamble.
Definition buffer_archive.h:204
static void postamble_load(const BufferInputArchive &)
Load the postamble.
Definition buffer_archive.h:207
static void postamble_store(const BufferOutputArchive &)
Write the postamble to the archive.
Definition buffer_archive.h:194
static void preamble_store(const BufferOutputArchive &)
Write the preamble to the archive.
Definition buffer_archive.h:191
Default implementation of the pre/postamble for type checking.
Definition archive.h:509