PSCF v1.1
MemoryCounter.h
1#ifndef UTIL_MEMORY_COUNTER_H
2#define UTIL_MEMORY_COUNTER_H
3
4/*
5* Util Package - C++ Utilities for Scientific Computation
6*
7* Copyright 2010 - 2017, The Regents of the University of Minnesota
8* Distributed under the terms of the GNU General Public License.
9*/
10
11#include "serialize.h"
12
13#include <util/space/Vector.h>
14#include <util/space/IntVector.h>
15
16#include <complex>
17
18namespace Util
19{
20
36 {
37
38 public:
39
43 static bool is_saving();
44
48 static bool is_loading();
49
54
59
63 void clear();
64
68 template <typename T>
69 MemoryCounter& operator & (T& data);
70
74 template <typename T>
76
84 template <typename T>
85 void count(const T& data);
86
94 template <typename T>
95 void count(T* array, int n);
96
100 size_t size() const;
101
102 private:
103
105 size_t size_;
106
108 int version_;
109
113 MemoryCounter (const MemoryCounter& other);
114
118 MemoryCounter& operator = (const MemoryCounter& other);
119
120 // friends:
121
122 friend void serialize<>(MemoryCounter&, std::string&, const unsigned int);
123
124 };
125
129 template <typename T>
130 int memorySize(T& data)
131 {
132 MemoryCounter counter;
133 counter & data;
134 return counter.size();
135 }
136
137 // Inline methods
138
140 { return true; }
141
143 { return false; }
144
145 /*
146 * Return capacity in Bytes.
147 */
148 inline size_t MemoryCounter::size() const
149 { return size_; }
150
151 /*
152 * Compute size of one object, default implementation.
153 */
154 template <typename T>
156 {
157 serialize(*this, data, version_);
158 return *this;
159 }
160
161 /*
162 * Compute size of one object, default implementation.
163 */
164 template <typename T>
166 {
167 serialize(*this, data, version_);
168 return *this;
169 }
170
171 // Inline method templates
172
173 /*
174 * Compute size of one object.
175 */
176 template <typename T>
177 inline void MemoryCounter::count(const T& data)
178 { size_ += sizeof(T); }
179
180 /*
181 * Bitwise pack a C-array of objects of type T.
182 */
183 template <typename T>
184 inline void MemoryCounter::count(T* array, int n)
185 { size_ += n*sizeof(T); }
186
187
188 // Explicit specializations of serialize function
189
190 // Serialize functions for primitive C++ types
191
192 /*
193 * Compute size of an bool.
194 */
195 template <>
196 inline void serialize(MemoryCounter& ar, bool& data,
197 const unsigned int version)
198 { ar.count(data); }
199
200 /*
201 * Compute size of a char.
202 */
203 template <>
204 inline void serialize(MemoryCounter& ar, char& data,
205 const unsigned int version)
206 { ar.count(data); }
207
208 /*
209 * Compute size of an unsigned int.
210 */
211 template <>
212 inline void serialize(MemoryCounter& ar, unsigned int& data,
213 const unsigned int version)
214 { ar.count(data); }
215
216 /*
217 * Compute size of an int.
218 */
219 template <>
220 inline void serialize(MemoryCounter& ar, int& data,
221 const unsigned int version)
222 { ar.count(data); }
223
224 /*
225 * Compute size of an unsigned long int.
226 */
227 template <>
228 inline void serialize(MemoryCounter& ar, unsigned long& data,
229 const unsigned int version)
230 { ar.count(data); }
231
232 /*
233 * Compute size of a long int.
234 */
235 template <>
236 inline void serialize(MemoryCounter& ar, long& data,
237 const unsigned int version)
238 { ar.count(data); }
239
240 /*
241 * Compute size of a float.
242 */
243 template <>
244 inline void serialize(MemoryCounter& ar, float& data,
245 const unsigned int version)
246 { ar.count(data); }
247
248 /*
249 * Compute size of a double.
250 */
251 template <>
252 inline void serialize(MemoryCounter& ar, double& data,
253 const unsigned int version)
254 { ar.count(data); }
255
256 // Serialize functions for std library types
257
258 /*
259 * Compute size of a std::complex<float>.
260 */
261 template <>
262 inline void serialize(MemoryCounter& ar, std::complex<float>& data,
263 const unsigned int version)
264 { ar.count(data); }
265
266 /*
267 * Compute size of a std::complex<double>.
268 */
269 template <>
270 inline void serialize(MemoryCounter& ar, std::complex<double>& data,
271 const unsigned int version)
272 { ar.count(data); }
273
274 /*
275 * Compute size of a std::string.
276 */
277 template <>
278 inline void serialize(MemoryCounter& ar, std::string& data,
279 const unsigned int version)
280 { ar.size_ += sizeof(size_t) + (data.size() + 1)*sizeof(char); }
281
282 // Serialize functions for Util namespace types
283
284 /*
285 * Compute size of a Util::Vector.
286 */
287 template <>
288 inline void serialize(MemoryCounter& ar, Vector& data,
289 const unsigned int version)
290 { ar.count(data); }
291
292 /*
293 * Compute size of a Util::IntVector.
294 */
295 template <>
296 inline void serialize(MemoryCounter& ar, IntVector& data,
297 const unsigned int version)
298 { ar.count(data); }
299
300}
301#endif
Archive to computed packed size of a sequence of objects, in bytes.
Definition: MemoryCounter.h:36
MemoryCounter()
Constructor.
MemoryCounter & operator<<(T &data)
Add packed size of one object.
~MemoryCounter()
Destructor.
static bool is_loading()
Returns false.
static bool is_saving()
Returns true.
size_t size() const
Return size required for archive, in Bytes.
void count(const T &data)
Add size of one object in memory.
void clear()
Resets the size counter to zero.
MemoryCounter & operator&(T &data)
Add packed size of one object.
Utility classes for scientific computation.
Definition: accumulators.mod:1
int memorySize(T &data)
Function template to compute memory size of one object.