PSCF v1.4.0
HostDArray.h
1#ifndef PSCF_HOST_D_ARRAY_H
2#define PSCF_HOST_D_ARRAY_H
3
4/*
5* PSCF - Polymer Self-Consistent Field
6*
7* Copyright 2015 - 2025, The Regents of the University of Minnesota
8* Distributed under the terms of the GNU General Public License.
9*/
10
11#include <util/containers/DArray.h>
12
13namespace Pscf {
14
15 // Forward declaration
16 template <typename T> class DeviceArray;
17
18 using namespace Util;
19
39 template <typename Data>
40 class HostDArray : public DArray<Data>
41 {
42
43 public:
44
48 using ValueType = Data;
49
54
63
64 HostDArray(HostDArray<Data> const & other) = default;
65
72
78 virtual ~HostDArray();
79
80 HostDArray<Data>& operator = (HostDArray<Data> const & other) = default;
81
100 HostDArray<Data>& operator = (DeviceArray<Data> const & other);
101
117 void copySlice(DeviceArray<Data> const & other, int beginId);
118
119 };
120
121} // namespace Pscf
122
123#include "DeviceArray.h"
124#include "cudaErrorCheck.h"
125#include <util/global.h>
126#include <cuda_runtime.h>
127
128namespace Pscf {
129
130 /*
131 * Default constructor.
132 */
133 template <typename Data>
135 : DArray<Data>()
136 {}
137
138 /*
139 * Allocating constructor.
140 */
141 template <typename Data>
145
146 /*
147 * Copy constructor - deep copy DeviceArray from device to host.
148 */
149 template <typename Data>
151 : DArray<Data>()
152 {
153 // Precondition - RHS array must be allocated
154 if (!other.isAllocated()) {
155 UTIL_THROW("RHS DeviceArray<Data> must be allocated.");
156 }
157
159 cudaErrorCheck( cudaMemcpy(DArray<Data>::cArray(), other.cArray(),
160 DArray<Data>::capacity() * sizeof(Data),
161 cudaMemcpyDeviceToHost) );
162 }
163
164 /*
165 * Destructor.
166 */
167 template <typename Data>
169 {} // DArray base class destructor will deallocate memory
170
171 /*
172 * Assignment from a DeviceArray<Data> RHS device array.
173 */
174 template <typename Data>
177 {
178 // Precondition - RHS array must be allocated
179 if (!other.isAllocated()) {
180 UTIL_THROW("RHS DeviceArray<Data> must be allocated.");
181 }
182
183 // Allocate this if necessary
186 }
187
188 // Require equal capacities
189 if (DArray<Data>::capacity() != other.capacity()) {
190 UTIL_THROW("Cannot assign arrays of unequal capacity");
191 }
192
193 // Copy all elements
194 cudaErrorCheck( cudaMemcpy(DArray<Data>::cArray(), other.cArray(),
195 DArray<Data>::capacity() * sizeof(Data),
196 cudaMemcpyDeviceToHost) );
197
198 return *this;
199 }
200
201 /*
202 * Copy a slice of the data from a larger DeviceArray into this array.
203 */
204 template <typename Data>
206 int beginId)
207 {
208 // Precondition - device array must be allocated
209 if (!other.isAllocated()) {
210 UTIL_THROW("RHS DeviceArray<Data> must be allocated.");
211 }
212
213 // Precondition - host array must be allocated
215 UTIL_THROW("LHS HostDArray<Data> must be allocated.");
216 }
217
218 // Slice must not exceed the capacity of device array
219 if (DArray<Data>::capacity() + beginId > other.capacity()) {
220 UTIL_THROW("Slice must not exceed the capacity of device array.");
221 }
222
223 // Copy all elements
224 cudaErrorCheck( cudaMemcpy(DArray<Data>::cArray(),
225 other.cArray() + beginId,
226 DArray<Data>::capacity() * sizeof(Data),
227 cudaMemcpyDeviceToHost) );
228 }
229
230}
231#endif
Dynamic array on the GPU device with aligned data.
Definition DeviceArray.h:96
bool isAllocated() const
Return true if the array has allocated data, false otherwise.
int capacity() const
Return array capacity.
Data * cArray()
Return pointer to underlying C array.
Template for dynamic array stored in host CPU memory.
Definition HostDArray.h:41
void copySlice(DeviceArray< Data > const &other, int beginId)
Copy a slice of the data from a larger DeviceArray into this array.
Definition HostDArray.h:205
HostDArray()
Default constructor.
Definition HostDArray.h:134
Data ValueType
Data type of each element.
Definition HostDArray.h:48
HostDArray(int capacity)
Allocating constructor.
Definition HostDArray.h:142
HostDArray(DeviceArray< Data > const &other)
Copy constructor (copies from device to host).
Definition HostDArray.h:150
virtual ~HostDArray()
Destructor.
Definition HostDArray.h:168
int capacity() const
Return allocated size.
Definition Array.h:144
void allocate(int capacity)
Allocate the underlying C array.
Definition DArray.h:269
DArray()
Default constructor.
Definition DArray.h:160
bool isAllocated() const
Return true if this DArray has been allocated, false otherwise.
Definition DArray.h:151
File containing preprocessor macros for error handling.
#define UTIL_THROW(msg)
Macro for throwing an Exception, reporting function, file and line number.
Definition global.h:49
PSCF package top-level namespace.