PSCF v1.5.0
FftwDRArray.tpp
1#ifndef PSCF_FFTW_DR_ARRAY_TPP
2#define PSCF_FFTW_DR_ARRAY_TPP
3
4/*
5* Util Package - C++ Utilities for Scientific Computation
6*
7* Copyright 2010 - 2017, David Morse (morse012@um.edu) and co-authors
8* Distributed under the terms of the GNU General Public License.
9*/
10
11#include <util/misc/Memory.h>
12
13#include <fftw3.h>
14#include <cstring>
15
16namespace Pscf {
17
18 // Non-inline member function definitions
19
20 /*
21 * Default constructor.
22 */
23 template <typename Data>
26
27 /*
28 * Allocating constructor.
29 */
30 template <typename Data>
33
34 /*
35 * Copy constructor.
36 */
37 template <typename Data>
39 {
40 if (!other.isAllocated()) {
41 UTIL_THROW("Other FftwDRArray must be allocated.");
42 }
43 allocate(other.capacity_);
44 std::memmove(data_, other.cArray(), capacity_*sizeof(Data));
45 }
46
47 /*
48 * Destructor.
49 */
50 template <typename Data>
52 {
53 if (data_) {
54 if (ref_.isAssociated()) {
55 ref_.dissociate();
56 } else {
57 fftw_free(data_); // void C function, cannot throw Exception
60 int nr = ReferenceCounter::nRef();
61 std::cout
62 << std::endl
63 << "Error: Destroying FftwDRArray that is referenced by "
64 << nr << " other(s), thus creating dangling references."
65 << std::endl;
66 }
67 }
68 }
69 data_ = nullptr;
70 capacity_ = 0;
71 }
72
73 /*
74 * Assignment from another FftwDRArray<Data> (deep copy).
75 */
76 template <typename Data>
79 {
80 // Check for self assignment
81 if (this == &other) return *this;
82
83 // Precondition - other array must be allocated
84 UTIL_CHECK (other.isAllocated());
85
86 // If this array is not allocated, then allocate
87 if (!isAllocated()) {
88 allocate(other.capacity());
89 }
90
91 // Copy elements
93 std::memmove(data_, other.cArray(), capacity_*sizeof(Data));
94
95 return *this;
96 }
97
98 /*
99 * Assignment from an Array<Data> (deep copy).
100 */
101 template <typename Data>
103 FftwDRArray<Data>::operator = (Array<Data> const & other)
104 {
105 // Check for self assignment
106 if (dynamic_cast< Array<Data>* >(this) == &other) return *this;
107
108 // Precondition - other array must be allocated
109 UTIL_CHECK(other.capacity() > 0);
110
111 // If this array is not allocated, then allocate
112 if (!isAllocated()) {
113 allocate(other.capacity());
114 }
115
116 // Copy all elements
117 UTIL_CHECK(capacity_ == other.capacity());
118 std::memmove(data_, other.cArray(), capacity_*sizeof(Data));
119
120 return *this;
121 }
122
123 /*
124 * Allocate an underlying C array, which this container then owns.
125 */
126 template <typename Data>
128 {
129 if (capacity <= 0) {
130 UTIL_THROW("Attempt to allocate with capacity <= 0");
131 }
132 if (isAllocated()) {
133 UTIL_THROW("Attempt to allocate a FftwDRArray that has data.");
134 }
135 data_ = (Data*) fftw_malloc(sizeof(Data) * capacity);
138 }
139
140 /*
141 * Deallocate a C array that is owned by this container.
142 */
143 template <typename Data>
145 {
147 UTIL_CHECK(!ref_.isAssociated());
148 fftw_free(data_);
149 data_ = nullptr;
151 capacity_ = 0;
153 }
154
155 /*
156 * Associate this object with a slice of another FftwDRArray.
157 */
158 template <typename Data>
160 int beginId, int capacity)
161 {
162 UTIL_CHECK(owner.isAllocated());
163 UTIL_CHECK(owner.isOwner());
164 UTIL_CHECK(beginId >= 0);
165 UTIL_CHECK(capacity > 0);
166 UTIL_CHECK(beginId + capacity <= owner.capacity());
168 UTIL_CHECK(!ref_.isAssociated());
169
170 // Copy data pointer and capacity
171 data_ = owner.cArray() + beginId;
173
174 // Associate the ReferencecCounter base sub-object of the owner array
175 // with the CountedReference ref_ member variable of this data user.
176 ref_.associate(owner);
177
178 // On exit from CountedReference::associate, the ReferenceCounter
179 // of the data owner is incremented and the CountedReference of
180 // this data user has a pointer to that ReferenceCounter.
181 }
182
183 /*
184 * Associate this object with a slice of a different FftwDRArray.
185 */
186 template <typename Data>
188 { associate(owner, 0, owner.capacity()); }
189
190 /*
191 * Dissociate this object from array slice owned by another object.
192 */
193 template <typename Data>
195 {
197 UTIL_CHECK(ref_.isAssociated());
198
199 data_ = nullptr;
200 capacity_ = 0;
201 ref_.dissociate(); // decrements counter mainained by data owner
202 }
203
204} // namespace Pscf
205#endif
Dynamic reference counted array for use with FFTW library.
Definition FftwDRArray.h:72
virtual void deallocate()
Dellocate an underlying C array that is owned by this container.
FftwDRArray< Data > & operator=(FftwDRArray< Data > const &other)
Assignment from another FftwDRArray<Data> container.
void dissociate()
Dissociate this object from an externally owned array slice.
FftwDRArray()
Default constructor.
void associate(FftwDRArray< Data > &other, int beginId, int capacity)
Associate this object with a slice of a different FftwDRArray.
virtual ~FftwDRArray()
Destructor.
void allocate(int capacity)
Allocate an underlying C array, which this container then owns.
bool isOwner() const
Does this container own a dynamically allocated C array?
Data * cArray()
Return a pointer to the underlying C array (non-const data).
Definition Array.h:222
Data * data_
Pointer to an array of Data elements.
Definition Array.h:141
int capacity() const
Return allocated size.
Definition Array.h:250
bool isAllocated() const
Does this array have associated data?
Definition Array.h:236
int capacity_
Allocated size of the data_ array.
Definition Array.h:144
static void sub(size_t size)
Subtract size of a newly deleted array in bytes from total.
Definition Memory.h:217
static void add(size_t size)
Add size of a newly allocated array in bytes to total.
Definition Memory.h:191
bool hasRefs() const
Does this counter have references (is nRef > 0)?
int nRef() const
Get the number of references to a shareable resource.
#define UTIL_CHECK(condition)
Assertion macro suitable for serial or parallel production code.
Definition global.h:68
#define UTIL_THROW(msg)
Macro for throwing an Exception, reporting function, file and line number.
Definition global.h:49
PSCF package top-level namespace.