PSCF v1.1
MemoryOArchive.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 "MemoryOArchive.h"
9#include <util/misc/Memory.h>
10
11#include <sstream>
12
13namespace Util
14{
15
16 /*
17 * Constructor.
18 */
20 : buffer_(0),
21 begin_(0),
22 cursor_(0),
23 endAllocated_(0),
24 capacity_(0),
25 version_(0),
26 isLocked_(false),
27 ownsData_(false)
28 {}
29
30 /*
31 * Destructor.
32 */
34 {
35 if (buffer_ && ownsData_) {
36 //delete buffer_;
37 Memory::deallocate<Byte>(buffer_, capacity_ + sizeof(size_t));
38 }
39 }
40
41 /*
42 * Allocate a block of memory.
43 */
44 void MemoryOArchive::allocate(size_t capacity)
45 {
46 if (begin_) {
47 UTIL_THROW("Re-allocation is prohibited");
48 }
49 // buffer_ = new Byte[capacity + sizeof(size_t)];
50 Memory::allocate<Byte>(buffer_, capacity + sizeof(size_t));
51 begin_ = buffer_ + sizeof(size_t);
52 cursor_ = begin();
53 endAllocated_ = begin_ + capacity;
54 capacity_ = capacity;
55 ownsData_ = true;
56 isLocked_ = false;
57 }
58
59 /*
60 * Return cursor to beginning.
61 */
63 {
64 if (!isAllocated()) {
65 UTIL_THROW("Archive is not allocated");
66 }
67 cursor_ = begin();
68 }
69
70 #ifdef UTIL_MPI
71 /*
72 * Send a block.
73 */
74 void MemoryOArchive::send(MPI::Intracomm& comm, int dest)
75 {
76 int comm_size = comm.Get_size();
77 int myRank = comm.Get_rank();
78
79 // Preconditions
80 if (dest > comm_size - 1 || dest < 0) {
81 UTIL_THROW("Destination rank out of bounds");
82 }
83 if (dest == myRank) {
84 UTIL_THROW("Source and desination identical");
85 }
86
87 size_t sendBytes = cursor_ - buffer_;
88 size_t* sizePtr = (size_t*)buffer_;
89 *sizePtr = sendBytes;
90 comm.Send(buffer_, sendBytes, MPI::UNSIGNED_CHAR, dest, 5);
91
92 }
93
94 /*
95 * Send a block (nonblocking)
96 */
97 void MemoryOArchive::iSend(MPI::Intracomm& comm, MPI::Request& req, int dest)
98 {
99 int comm_size = comm.Get_size();
100 int myRank = comm.Get_rank();
101
102 // Preconditions
103 if (dest > comm_size - 1 || dest < 0) {
104 UTIL_THROW("Destination rank out of bounds");
105 }
106 if (dest == myRank) {
107 UTIL_THROW("Source and desination identical");
108 }
109
110 size_t sendBytes = cursor_ - buffer_;
111 size_t* sizePtr = (size_t*)buffer_;
112 *sizePtr = sendBytes;
113 req = comm.Isend(buffer_, sendBytes, MPI::UNSIGNED_CHAR, dest, 5);
114 }
115
116 #endif
117
118}
virtual ~MemoryOArchive()
Destructor.
void iSend(MPI::Intracomm &comm, MPI::Request &req, int dest)
Send packed data via MPI (non-blocking)
virtual void allocate(size_t capacity)
Allocate memory.
bool isAllocated() const
Has memory been allocated?
Byte * begin() const
Return pointer to beginning of block.
size_t capacity() const
Return capacity in Bytes.
MemoryOArchive()
Constructor.
void send(MPI::Intracomm &comm, int dest)
Send packed data via MPI.
void clear()
Resets the cursor to the beginning.
#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