PSCF v1.5.0
cpp/HostSendArray.h
1#ifndef PSCF_HOST_SEND_ARRAY_CP_H
2#define PSCF_HOST_SEND_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/Array.h> // base class template
12#include <util/misc/CountedReference.h> // member
13#include <pscf/backend/cpp/CPT.h> // template argument
14
15// Forward declaration
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 HostSendArray;
26
107 template <typename Data>
108 class HostSendArray<Data, CPT> final : public Array<Data>
109 {
110
111 public:
112
117
118 // Default constructor.
119 HostSendArray() = default;
120
121 // Copy construction (delete).
122 HostSendArray(HostSendArray<Data,CPT> const &) = delete;
123
127 ~HostSendArray();
128
129 // Assignment (delete).
130 HostSendArray<Data,CPT>&
131 operator = (HostSendArray<Data,CPT> const&) = delete;
132
145 void associate(BackendArray<Data,CPT> & other);
146
154 void dissociate();
155
156 private:
157
159 CountedReference ref_;
160
161 // Hide inherited protected members by making them private
162 using Array<Data>::data_;
163 using Array<Data>::capacity_;
164
165 };
166
167 // Explicit instantiation declarations
168 extern template class HostSendArray<double,CPT>;
169 extern template class HostSendArray<fftw_complex,CPT>;
170
171} // namespace Pscf
172
173#include <pscf/backend/cpp/BackendArray.h>
174#include <util/global.h>
175
176namespace Pscf {
177
178 // Member functions
179
180 /*
181 * Destructor.
182 */
183 template <typename Data>
184 HostSendArray<Data,CPT>::~HostSendArray()
185 {
186 if (ref_.isAssociated()) {
187 ref_.dissociate(); // decrements counter of source array
188 }
189 }
190
191 /*
192 * Associate this object with a backend array.
193 */
194 template <typename Data>
195 void
196 HostSendArray<Data,CPT>::associate(BackendArray<Data,CPT> & source)
197 {
198 UTIL_CHECK(source.isAllocated());
199 UTIL_CHECK(!ref_.isAssociated());
200
201 // Copy data pointer and size
202 data_ = source.cArray();
203 capacity_ = source.capacity();
204
205 // Associate ReferenceCounter of the source array with this
206 ref_.associate(source);
207
208 // On exit, the ReferenceCounter sub-object of the data source is
209 // incremented and the ref_ CountedReference member variable of
210 // this object holds a pointer to that ReferenceCounter.
211 }
212
213 /*
214 * Release pre-existing association with a backend array.
215 */
216 template <typename Data>
217 void HostSendArray<Data,CPT>::dissociate()
218 {
220 UTIL_CHECK(ref_.isAssociated());
221
222 data_ = nullptr;
223 capacity_ = 0;
224 ref_.dissociate(); // decrements reference counter of source array
225 }
226
227}
228#endif
Backend identifier class for the C++ serial CPU backend.
Definition CPT.h:32
void dissociate()
Release association with a backend array.
CPT BackendIdClass
Backend identifier class typename alias.
void associate(BackendArray< Data, CPT > &other)
Associate this object with a backend array.
Data * data_
Pointer to an array of Data elements.
Definition Array.h:141
int capacity_
Allocated size of the data_ array.
Definition Array.h:144
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.