PSCF v1.5.0
cuda/CField.h
1#ifndef PSCF_C_FIELD_CU_H
2#define PSCF_C_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> // class template argument
12#include <pscf/backend/cuda/BackendArray.h> // base class template
13#include <pscf/backend/cuda/cudaTypes.h> // base class argument
14
15#include <pscf/backend/cuda/HostSendArray.h>
16#include <pscf/math/IntVec.h>
17#include <util/global.h>
18
19namespace Pscf {
20
21 using namespace Util;
22
23 // Declare primary template
24 template <int D, class T> class CField;
25
31 template <int D>
32 class CField<D,CUT>
33 : public BackendArray<cudaComplex,CUT>
34 {
35
36 public:
37
41 CField();
42
51
59 CField(CField<D,CUT> const & other);
60
66 virtual ~CField();
67
75 void allocate(IntVec<D> const & meshDimensions);
76
80 void deallocate();
81
91 void associate(BackendArray<cudaComplex,CUT>& arr, int beginId,
93
107 CField<D,CUT>& operator = (CField<D,CUT> const & other);
108
120 CField<D,CUT>& operator = (HostSendArray<cudaComplex,CUT> const & other);
121
125 IntVec<D> const & meshDimensions() const;
126
133 template <class Archive>
134 void serialize(Archive& ar, const unsigned int version);
135
136 private:
137
138 // Vector containing number of grid points in each dimension.
139 IntVec<D> meshDimensions_;
140
141 // Make private to prevent allocation without mesh dimensions.
143
144 // Make private to prevent association without mesh dimensions.
146
147 // Make private to prevent assignment without mesh dimensions.
148 using BackendArray<cudaComplex,CUT>::operator=;
149
150 };
151
152 /*
153 * Return mesh dimensions by constant reference.
154 */
155 template <int D>
157 { return meshDimensions_; }
158
159 /*
160 * Serialize a Field to/from an Archive.
161 */
162 template <int D>
163 template <class Archive>
164 void CField<D,CUT>::serialize(Archive& ar, const unsigned int version)
165 {
166 int capacity;
167 if (Archive::is_saving()) {
168 capacity = capacity_;
169 }
170 ar & capacity;
171 if (Archive::is_loading()) {
172 if (!isAllocated()) {
173 if (capacity > 0) {
174 allocate(capacity);
175 }
176 } else {
177 if (capacity != capacity_) {
178 UTIL_THROW("Inconsistent Field capacities");
179 }
180 }
181 }
182
183 if (isAllocated()) {
184 HostSendArray<cudaComplex,CUT> tempData(capacity);
185 tempData = this; // copy this object's data from device to host
186 for (int i = 0; i < capacity_; ++i) {
187 ar & tempData[i].x;
188 ar & tempData[i].y;
189 }
190 }
191 ar & meshDimensions_;
192 }
193
194 extern template class CField<1,CUT>;
195 extern template class CField<2,CUT>;
196 extern template class CField<3,CUT>;
197
198}
199#endif
void serialize(Archive &ar, const unsigned int version)
Serialize a Field to/from an Archive.
void allocate(IntVec< D > const &meshDimensions)
Allocate the underlying C array for data on a regular mesh.
void associate(BackendArray< cudaComplex, CUT > &arr, int beginId, IntVec< D > const &meshDimensions)
Associate this object with a slice of another BackendArray.
CField()
Default constructor.
IntVec< D > const & meshDimensions() const
Return mesh dimensions by constant reference.
void deallocate()
Deallocate the underlying array.
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
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.
cufftDoubleComplex cudaComplex
Complex number type used in CPU code that uses FFTW.
Definition cudaTypes.h:22