PSCF v1.5.0
cuda/HostSendArray.h
1#ifndef PSCF_HOST_SEND_ARRAY_CU_H
2#define PSCF_HOST_SEND_ARRAY_CU_H
3
4/*
5* PSCF - Polymer Self-Consistent Field
6*
7* Copyright 2015 - 2026, 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/DArray.h> // base class template
12#include <pscf/backend/cuda/CUT.h> // template argument
13
14namespace Pscf {
15
16 // Forward declarations
17 template <typename Data, typename T> class HostSendArray;
18 template <typename Data, typename T> class BackendArray;
19
20 using namespace Util;
21
92 template <typename Data>
93 class HostSendArray<Data,CUT> : public DArray<Data>
94 {
95
96 public:
97
101 using ValueType = Data;
102
107
108 // Public member functions
109
110 // Default constructor (default)
111 HostSendArray() = default;
112
118 HostSendArray(int capacity);
119
120 // Copy constructor (deleted)
121 HostSendArray(HostSendArray<Data,CUT> const & other) = delete;
122
123 // Destructor (default).
124 ~HostSendArray() = default;
125
126 // Assignment (deleted).
127 HostSendArray<Data,CUT>&
128 operator = (HostSendArray<Data,CUT> const & other) = delete;
129
138 void associate(BackendArray<Data,CUT>& deviceArray);
139
158 HostSendArray<Data,CUT>& operator = (BackendArray<Data,CUT> const & other);
159
175 void copySlice(BackendArray<Data,CUT> const & other, int beginId);
176
185 {}
186
187 // Inherited public member functions (selected)
188 using Array<Data>::operator [];
189 using Array<Data>::cArray;
190 using Array<Data>::isAllocated;
191 using Array<Data>::capacity;
192 using DArray<Data>::allocate;
193 using DArray<Data>::deallocate;
194
195 };
196
197 // Explicit instantiation declarations
198 extern template class HostSendArray<cudaReal,CUT>;
199 extern template class HostSendArray<cudaComplex,CUT>;
200
201} // namespace Pscf
202
203#include <pscf/backend/cuda/BackendArray.h>
204#include <pscf/backend/cuda/cudaErrorCheck.h>
205#include <util/global.h>
206#include <cuda_runtime.h>
207
208namespace Pscf {
209
210 /*
211 * Allocating constructor.
212 */
213 template <typename Data>
214 HostSendArray<Data,CUT>::HostSendArray(int capacity)
215 : DArray<Data>(capacity)
216 {}
217
218 /*
219 * Assignment from a BackendArray<Data,CUT> RHS backend array.
220 */
221 template <typename Data>
223 HostSendArray<Data,CUT>::operator = (BackendArray<Data,CUT> const & other)
224 {
225 // Precondition - RHS array must be allocated
226 UTIL_CHECK(other.isAllocated());
227
228 // Allocate this if necessary
229 if (!isAllocated()) {
230 allocate(other.capacity());
231 }
232
233 // Require equal capacities
235 UTIL_CHECK(capacity() == other.capacity());
236
237 // Copy all elements
238 cudaErrorCheck(
239 cudaMemcpy(Array<Data>::data_,
240 other.cArray(),
241 capacity() * sizeof(Data),
242 cudaMemcpyDeviceToHost)
243 );
244
245 return *this;
246 }
247
248 /*
249 * Copy a slice of the data from a larger BackendArray into this array.
250 */
251 template <typename Data>
252 void HostSendArray<Data,CUT>::copySlice(BackendArray<Data,CUT> const & other,
253 int beginId)
254 {
255 // Preconditions
256 UTIL_CHECK(other.isAllocated());
258 UTIL_CHECK(beginId >= 0);
259 UTIL_CHECK(capacity() + beginId <= other.capacity());
260
261 // Copy all elements
262 cudaErrorCheck(
263 cudaMemcpy(Array<Data>::data_,
264 other.cArray() + beginId,
265 capacity() * sizeof(Data),
266 cudaMemcpyDeviceToHost)
267 );
268 }
269
270 /*
271 * Allocate if not done previously.
272 */
273 template <typename Data>
274 void HostSendArray<Data,CUT>::associate(
275 BackendArray<Data,CUT>& deviceArray)
276 {
277 UTIL_CHECK(deviceArray.isAllocated());
278 const int n = deviceArray.capacity();
279 if (!isAllocated()) {
280 allocate(deviceArray.capacity());
281 }
283 UTIL_CHECK(capacity() == n);
284 // If this was allocated with capacity() == n, do nothing.
285 // If this was allocated with capacity() != n, throw Exception.
286 }
287
288}
289#endif
Backend identifier class for the GPU-enabled CUDA backend.
Definition CUT.h:31
void copySlice(BackendArray< Data, CUT > const &other, int beginId)
Copy a slice of the data from a larger BackendArray into this array.
void associate(BackendArray< Data, CUT > &deviceArray)
Allocate if not allocated previously.
Data ValueType
Data type of each element.
void dissociate()
Release host array.
CUT BackendIdClass
Backend identifier class type.
Array container class template.
Definition Array.h:41
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
Dynamically allocatable contiguous array template.
Definition DArray.h:32
void allocate(int capacity)
Allocate the underlying C array.
Definition DArray.h:262
DArray()
Default constructor.
Definition DArray.h:148
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.