PSCF v1.5.0
cuda/RField.h
1#ifndef PSCF_R_FIELD_CU_H
2#define PSCF_R_FIELD_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 <pscf/backend/cuda/CUT.h> // template argument
12#include <pscf/backend/cuda/BackendArray.h> // base class template
13#include <pscf/backend/cuda/cudaTypes.h> // base class argument
14#include <pscf/math/IntVec.h> // member
15
16// Forward declarations
17namespace Pscf {
18 template <typename Data, class T> class HostSendArray;
19 template <int D, class T> class RField;
20}
21
22namespace Pscf {
23
24 using namespace Util;
25
33 template <int D>
34 class RField<D,CUT>
35 : public BackendArray<cudaReal,CUT>
36 {
37
38 public:
39
43 RField();
44
53
61 RField(RField<D,CUT> const& other);
62
68 virtual ~RField();
69
77 void allocate(IntVec<D> const & meshDimensions);
78
82 void deallocate();
83
93 void associate(BackendArray<cudaReal,CUT>& arr, int beginId,
95
110 operator = (RField<D,CUT> const & other);
111
123 RField<D,CUT>& operator = (HostSendArray<cudaReal,CUT> const & other);
124
128 IntVec<D> const & meshDimensions() const;
129
136 template <class Archive>
137 void serialize(Archive& ar, const unsigned int version);
138
139 private:
140
141 // Vector containing number of grid points in each direction.
142 IntVec<D> meshDimensions_;
143
144 // Make private to prevent allocation without mesh dimensions.
146
147 // Make private to prevent association without mesh dimensions.
149
150 // Make private to prevent assignment without mesh dimensions.
151 using BackendArray<cudaReal,CUT>::operator=;
152
153 };
154
155 // Explicit instantiation declarations
156 extern template class RField<1,CUT>;
157 extern template class RField<2,CUT>;
158 extern template class RField<3,CUT>;
159
160} // namespace Pscf
161
162#include <pscf/backend/cuda/HostSendArray.h>
163#include <util/global.h>
164
165// Inline and template member functions
166namespace Pscf {
167
168 /*
169 * Return mesh dimensions by constant reference.
170 */
171 template <int D> inline
172 IntVec<D> const &
174 { return meshDimensions_; }
175
176 /*
177 * Serialize an RField to/from an Archive.
178 */
179 template <int D>
180 template <class Archive>
181 void RField<D,CUT>::serialize(Archive& ar, const unsigned int version)
182 {
183 int capacity;
184 if (Archive::is_saving()) {
185 capacity = capacity_;
186 }
187 ar & capacity;
188 if (Archive::is_loading()) {
189 if (!isAllocated()) {
190 if (capacity > 0) {
191 allocate(capacity);
192 }
193 } else {
194 if (capacity != capacity_) {
195 UTIL_THROW("Inconsistent Field capacities");
196 }
197 }
198 }
199
200 if (isAllocated()) {
201 HostSendArray<cudaReal,CUT> tempData(capacity);
202 tempData = this; // copy this object's data from device to host
203 for (int i = 0; i < capacity_; ++i) {
204 ar & tempData[i];
205 }
206 }
207 ar & meshDimensions_;
208 }
209
210} // namespace Pscf
211#endif
Backend identifier class for the GPU-enabled CUDA backend.
Definition CUT.h:31
An IntVec<D, T> is a D-component vector of elements of integer type T.
Definition IntVec.h:27
RField()
Default constructor.
void allocate(IntVec< D > const &meshDimensions)
Allocate the underlying array for data on a regular mesh.
void serialize(Archive &ar, const unsigned int version)
Serialize a Field to/from an Archive.
void deallocate()
Deallocate the underlying array.
void associate(BackendArray< cudaReal, CUT > &arr, int beginId, IntVec< D > const &meshDimensions)
Associate this object with a slice of another BackendArray.
IntVec< D > const & meshDimensions() const
Return mesh dimensions by constant reference.
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.
cufftDoubleReal cudaReal
Real number type used in CPU code that uses FFTW.
Definition cudaTypes.h:35