PSCF v1.1
MemoryIArchive.cpp
1/*
2* Util Package - C++ Utilities for Scientific Computation
3*
4* Copyright 2010 - 2017, The Regents of the University of Minnesota
5* Distributed under the terms of the GNU General Public License.
6*/
7
8#include "MemoryIArchive.h"
9#include "MemoryOArchive.h"
10#include <util/misc/Memory.h>
11
12#include <sstream>
13
14namespace Util
15{
16
17 /*
18 * Constructor.
19 */
21 : buffer_(0),
22 begin_(0),
23 cursor_(0),
24 end_(0),
25 endAllocated_(0),
26 oArchivePtr_(0),
27 capacity_(0),
28 version_(0),
29 ownsData_(false)
30 {}
31
32 /*
33 * Destructor.
34 */
36 {
37 if (buffer_ && ownsData_) {
38 //delete buffer_;
39 Memory::deallocate<Byte>(buffer_, capacity_ + sizeof(size_t));
40 }
41 }
42
43 /*
44 * Allocate a block of memory.
45 */
46 void MemoryIArchive::allocate(size_t capacity)
47 {
48 if (begin_) {
49 UTIL_THROW("Re-allocation is prohibited");
50 }
51 // buffer_ = new Byte[capacity + sizeof(size_t)];
52 Memory::allocate<Byte>(buffer_, capacity + sizeof(size_t));
53 begin_ = buffer_ + sizeof(size_t);
54 cursor_ = begin_;
55 end_ = begin_;
56 endAllocated_ = begin_ + capacity;
57 capacity_ = capacity;
58 ownsData_ = true;
59 }
60
65 {
66 if (isAllocated()) {
67 UTIL_THROW("This MemoryIArchive is already allocated");
68 }
69 buffer_ = other.buffer_;
70 begin_ = other.begin_;
71 cursor_ = other.begin_;
72 end_ = other.cursor_;
73 endAllocated_ = other.endAllocated_;
74 capacity_ = other.capacity_;
75 ownsData_ = false;
76 oArchivePtr_ = &other;
77 other.isLocked_ = true;
78 return *this;
79 }
80
81 /*
82 * Reset cursor to beginning, prepare to reread.
83 */
85 {
86 if (!isAllocated()) {
87 UTIL_THROW("Archive is not allocated");
88 }
89 cursor_ = begin();
90 }
91
92 /*
93 * Clear memory block (reset to empty state).
94 */
96 {
97 if (!isAllocated()) {
98 UTIL_THROW("Archive is not allocated");
99 }
100 if (!ownsData_) {
101 UTIL_THROW("Archive does not own data");
102 }
103 cursor_ = begin();
104 end_ = begin();
105 }
106
107 /*
108 * Release associated data acquired by assignment.
109 */
111 {
112 if (!begin_) {
113 UTIL_THROW("This archive has no associated data");
114 }
115 if (ownsData_) {
116 UTIL_THROW("This archive owns its data");
117 }
118 buffer_ = 0;
119 begin_ = 0;
120 cursor_ = 0;
121 end_ = 0;
122 endAllocated_ = 0;
123 capacity_ = 0;
124 ownsData_ = false;
125 oArchivePtr_->isLocked_ = false;
126 oArchivePtr_ = 0;
127 }
128
129 #ifdef UTIL_MPI
130 /*
131 * Receive a block.
132 */
133 void MemoryIArchive::recv(MPI::Intracomm& comm, int source)
134 {
135 int myRank = comm.Get_rank();
136 int comm_size = comm.Get_size();
137
138 // Preconditions
139 if (source > comm_size - 1 || source < 0) {
140 UTIL_THROW("Source rank out of bounds");
141 }
142 if (source == myRank) {
143 UTIL_THROW("Source and desination identical");
144 }
145
146 size_t recvCapacity = capacity_ + sizeof(size_t);
147 comm.Recv(buffer_, recvCapacity, MPI::UNSIGNED_CHAR, source, 5);
148
149 begin_ = buffer_ + sizeof(size_t);
150 cursor_ = begin_;
151
152 size_t* sizePtr = (size_t*) buffer_;
153 size_t size = *sizePtr;
154 end_ = buffer_ + size;
155 }
156 #endif
157
158 /*
159 * Load a std::string from MemoryIArchive.
160 */
161 template <>
162 void serialize(MemoryIArchive& ar, std::string& data,
163 const unsigned int version)
164 {
165 static std::vector<char> charvec;
166 size_t size;
167 ar.unpack(size);
168 if (size > charvec.capacity()) {
169 charvec.reserve(size + 8);
170 }
171 ar.unpack(&charvec[0], size);
172 data = &charvec[0];
173 }
174
175}
Input archive for packed heterogeneous binary data.
void recv(MPI::Intracomm &comm, int source)
Receive packed data via MPI.
Byte * begin() const
Return pointer to beginning of block.
MemoryIArchive & operator=(MemoryOArchive &other)
Assignment from MemoryOArchive.
void allocate(size_t capacity)
Allocate memory block.
void reset()
Reset the cursor to the beginning (for rereading).
size_t capacity() const
Return capacity in Bytes.
void clear()
Reset to empty state.
~MemoryIArchive()
Destructor.
void unpack(T &data)
Unpack one object of type T.
void release()
Release memory obtained by assignment.
MemoryIArchive()
Constructor.
bool isAllocated() const
Has memory been allocated?
Save archive for packed heterogeneous binary data.
#define UTIL_THROW(msg)
Macro for throwing an Exception, reporting function, file and line number.
Definition: global.h:51
Utility classes for scientific computation.
Definition: accumulators.mod:1