PSCF v1.5.0
cpp/HostRecvArray.h
1#ifndef PSCF_HOST_RECV_ARRAY_CP_H
2#define PSCF_HOST_RECV_ARRAY_CP_H
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/containers/ConstArray.h> // base class
12#include <util/misc/CountedReference.h> // member
13#include <pscf/backend/cpp/CPT.h> // template argument
14
15// Forward declarations
16namespace Pscf {
17 template <typename Data, typename T> class BackendArray;
18}
19
20namespace Pscf {
21
22 using namespace Util;
23
24 // Declare primary template
25 template <typename Data, typename T> class HostRecvArray;
26
113 template <typename Data>
114 class HostRecvArray<Data, CPT> final : public ConstArray<Data>
115 {
116
117 public:
118
123
124 // Default constructor.
125 HostRecvArray() = default;
126
127 // Copy construction (delete)
128 HostRecvArray(HostRecvArray<Data,CPT> const & other) = delete;
129
135 ~HostRecvArray();
136
137 // Assignment from another HostRecvArray (delete)
138 HostRecvArray<Data,CPT>&
139 operator = (HostRecvArray<Data,CPT> const&) = delete;
140
150 HostRecvArray(BackendArray<Data,CPT> const & other);
151
164 HostRecvArray<Data,CPT>&
165 operator = (BackendArray<Data,CPT> const & other);
166
172 void dissociate();
173
174 private:
175
177 CountedReference ref_;
178
191 void associate(BackendArray<Data,CPT> const & source);
192
193 using ConstArray<Data>::data_;
194 using ConstArray<Data>::capacity_;
195
196 };
197
198 // Explicit instantiation declarations
199 extern template class HostRecvArray<double,CPT>;
200 extern template class HostRecvArray<fftw_complex,CPT>;
201
202} // namespace Pscf
203
204#include <pscf/backend/cpp/BackendArray.h>
205#include <util/global.h>
206
207namespace Pscf {
208
209 // Public member function definitions
210
211 /*
212 * Destructor.
213 */
214 template <typename Data>
215 HostRecvArray<Data,CPT>::~HostRecvArray()
216 {
217 if (ref_.isAssociated()) {
218 ref_.dissociate(); // decrements counter of source array
219 }
220 }
221
222 /*
223 * Copy construction from a BackendArray.
224 *
225 * Creates an association with a BackendArray that owns the data.
226 */
227 template <typename Data>
228 HostRecvArray<Data,CPT>::HostRecvArray(
229 BackendArray<Data,CPT> const & other)
230 : ConstArray<Data>()
231 { associate(other); }
232
233 /*
234 * Assignment from a BackendArray.
235 *
236 * Creates an association with a BackendArray that owns the data.
237 */
238 template <typename Data>
240 HostRecvArray<Data,CPT>::operator = (BackendArray<Data,CPT> const& other)
241 {
242 associate(other);
243 return *this;
244 }
245
246 /*
247 * Release association with a backend array.
248 */
249 template <typename Data>
250 void HostRecvArray<Data,CPT>::dissociate()
251 {
253 UTIL_CHECK(ref_.isAssociated());
254
255 data_ = nullptr;
256 capacity_ = 0;
257 ref_.dissociate(); // decrements reference counter of source array
258 }
259
260 // Private function
261
262 /*
263 * Associate this object with a backend array.
264 */
265 template <typename Data>
266 void
268 {
269 UTIL_CHECK(source.isAllocated());
270 UTIL_CHECK(!ref_.isAssociated());
271
272 // Copy data pointer and size
273 data_ = const_cast<Data*>( source.cArray() );
274 capacity_ = source.capacity();
275
276 // Note: The const_cast to non-const pointer is permissible because
277 // the public interface of the ConstArray<Data> base class does
278 // not allow modification of array elements.
279
280 // Associate ReferenceCounter of the source array with this
281 ref_.associate(source);
282
283 // On exit, the ReferenceCounter sub-object of the source array is
284 // incremented and the ref_ CountedReference member variable of
285 // this object holds a pointer to that ReferenceCounter.
286 }
287
288}
289#endif
Backend identifier class for the C++ serial CPU backend.
Definition CPT.h:32
CPT BackendIdClass
Backend identifier class typename alias.
void dissociate()
Release association with a backend array.
int capacity_
Allocated size of the data_ array.
Definition ConstArray.h:117
Data * data_
Pointer to an array of Data elements.
Definition ConstArray.h:114
Reference to a shared resource.
File containing preprocessor macros for error handling.
#define UTIL_CHECK(condition)
Assertion macro suitable for serial or parallel production code.
Definition global.h:68
PSCF package top-level namespace.