PSCF v1.5.0
cuda/FFT.tpp
1#ifndef PSCF_FFT_CU_TPP
2#define PSCF_FFT_CU_TPP
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 "FFT.h"
12#include "RField.h"
13#include "CField.h"
14#include <pscf/backend/cuda/VecOp.h>
15#include <util/global.h>
16
17/*
18* A note about const_casts:
19*
20* The cuFFT library is used in this file to perform discrete Fourier
21* transforms. cuFFT's complex-to-real inverse transform overwrites its
22* input array, but all other out-of-place transforms leave the input
23* array unaltered. However, all transforms in the cuFFT library require
24* non-const pointers to the input array, even though they do not alter
25* the array.
26*
27* In order to maintain const-correctness in PSCF, this FFT class accepts
28* const input arrays for its methods that perform a Fourier transform,
29* unless the transform is expected to modify / overwrite its input (as
30* is the case for complex-to-real inverse transforms). This denotes to
31* the caller of the method that the input array will not be altered,
32* which is an accurate representation of the expected behavior.
33*
34* However, the const-correctness of this FFT class creates a conflict
35* with the cuFFT library's requirement of non-const inputs. This conflict
36* is resolved using a const_cast, in which the const pointer to the input
37* array is made non-const when passed into cuFFT functions. The use of
38* const_cast is reserved only for these few select cases in which we are
39* confident that the input array will not be modified.
40*
41* For more information about the relevant cuFFT methods, see the cuFFT
42* documentation at https://docs.nvidia.com/cuda/cufft/. Unfortunately,
43* there is no explicitly documented guarantee that the transforms do not
44* modify their input, though it is implied. In Section 2.4, it is stated
45* that "[o]ut-of-place complex-to-real FFT will always overwrite input
46* buffer." No such claim is made for any other out-of-place transforms,
47* implying that they do not overwrite their inputs. Further, "[t]he
48* cuFFT API is modeled after FFTW," (beginning of Section 2), and FFTW
49* is much more explicit in their documentation
50* (https://www.fftw.org/fftw3_doc/index.html). In Section 4.3.2, it is
51* stated that, by default, "an out-of-place transform must not change
52* its input array," except for complex-to-real transforms, in which case
53* "no input-preserving algorithms are implemented." Finally, we note
54* that the unit tests for this FFT class check that the input array is
55* unaltered, allowing developers to continually ensure that the cuFFT
56* functions do not modify their input unexpectedly.
57*/
58
59namespace Pscf {
60
61 using namespace Util;
62
63 /*
64 * Default constructor.
65 */
66 template <int D>
68 : meshDimensions_(0),
69 rSize_(0),
70 kSize_(0),
71 rcfPlan_(0),
72 criPlan_(0),
73 ccPlan_(0),
74 isSetup_(false)
75 {}
76
77 /*
78 * Destructor.
79 */
80 template <int D>
82 {
83 if (rcfPlan_) {
84 cufftDestroy(rcfPlan_);
85 }
86 if (criPlan_) {
87 cufftDestroy(criPlan_);
88 }
89 if (ccPlan_) {
90 cufftDestroy(ccPlan_);
91 }
92 }
93
94 /*
95 * Setup grid dimensions, plans and work space.
96 */
97 template <int D>
99 {
100 // Precondition
101 UTIL_CHECK(!isSetup_);
102
103 // Set mesh dimensions and sizes
104 rSize_ = 1;
105 kSize_ = 1;
106 for (int i = 0; i < D; ++i) {
108 meshDimensions_[i] = meshDimensions[i];
109 rSize_ *= meshDimensions[i];
110 if (i < D - 1) {
111 kSize_ *= meshDimensions[i];
112 } else {
113 kSize_ *= (meshDimensions[i]/2 + 1);
114 }
115 }
116
117 // Reallocate kFieldCopy_ array if necessary
118 if (kFieldCopy_.isAllocated()) {
119 if (kFieldCopy_.capacity() != kSize_) {
120 kFieldCopy_.deallocate();
121 kFieldCopy_.allocate(meshDimensions);
122 }
123 }
124
125 // Make FFTW plans (explicit specializations)
126 makePlans();
127
128 isSetup_ = true;
129 }
130
131 /*
132 * Compute forward (real-to-complex) discrete Fourier transform.
133 */
134 template <int D>
136 RFieldDft<D,CUT>& kField) const
137 {
138 // Preconditions
139 UTIL_CHECK(isSetup_);
140 UTIL_CHECK(rField.capacity() == rSize_);
141 UTIL_CHECK(kField.capacity() == kSize_);
142
143 // Perform transform
144 // (See note at top of file explaining this use of const_cast)
145 cufftResult result;
146 #ifdef SINGLE_PRECISION
147 result = cufftExecR2C(rcfPlan_, const_cast<cudaReal*>(rField.cArray()),
148 kField.cArray());
149 #else
150 result = cufftExecD2Z(rcfPlan_, const_cast<cudaReal*>(rField.cArray()),
151 kField.cArray());
152 #endif
153 if (result != CUFFT_SUCCESS) {
154 UTIL_THROW("Failure in cufft real-to-complex forward transform");
155 }
156
157 // Rescale output data in-place
158 cudaReal scale = 1.0/cudaReal(rSize_);
159 VecOp::mulEqS(kField, scale);
160 }
161
162 /*
163 * Compute inverse (complex-to-real) DFT, overwriting the input.
164 */
165 template <int D>
167 RField<D,CUT>& rField) const
168 {
169 // Preconditions
170 UTIL_CHECK(isSetup_);
171 UTIL_CHECK(rField.capacity() == rSize_);
172 UTIL_CHECK(kField.capacity() == kSize_);
173 UTIL_CHECK(rField.meshDimensions() == meshDimensions_);
174 UTIL_CHECK(kField.meshDimensions() == meshDimensions_);
175
176 cufftResult result;
177 #ifdef SINGLE_PRECISION
178 result = cufftExecC2R(criPlan_, kField.cArray(), rField.cArray());
179 #else
180 result = cufftExecZ2D(criPlan_, kField.cArray(), rField.cArray());
181 #endif
182 if (result != CUFFT_SUCCESS) {
183 UTIL_THROW( "Failure in cufft complex-to-real inverse transform");
184 }
185
186 }
187
188 /*
189 * Compute inverse (complex-to-real) DFT without overwriting input.
190 */
191 template <int D>
193 RField<D,CUT>& rField) const
194 {
195 // if kFieldCopy_ has been previously allocated, check size is correct
196 if (kFieldCopy_.isAllocated()) {
197 UTIL_CHECK(kFieldCopy_.capacity() == kSize_);
198 UTIL_CHECK(kFieldCopy_.meshDimensions() == meshDimensions_);
199 }
200
201 // make copy of kField (allocates kFieldCopy_ if necessary)
202 kFieldCopy_ = kField;
203
204 // Perform transform using copy of kField
205 inverseTransformUnsafe(kFieldCopy_, rField);
206 }
207
208 // Complex-to-Complex Transforms
209
210 /*
211 * Execute forward complex-to-complex transform.
212 */
213 template <int D>
215 const
216 {
217 // Preconditions
218 UTIL_CHECK(isSetup_);
219 UTIL_CHECK(rField.capacity() == rSize_);
220 UTIL_CHECK(kField.capacity() == rSize_);
221 UTIL_CHECK(rField.meshDimensions() == meshDimensions_);
222 UTIL_CHECK(kField.meshDimensions() == meshDimensions_);
223
224 // Perform transform
225 // (See note at top of file explaining this use of const_cast)
226 cufftResult result;
227 #ifdef SINGLE_PRECISION
228 result = cufftExecC2C(ccPlan_, const_cast<cudaComplex*>(rField.cArray()),
229 kField.cArray(), CUFFT_FORWARD);
230 #else
231 result = cufftExecZ2Z(ccPlan_, const_cast<cudaComplex*>(rField.cArray()),
232 kField.cArray(), CUFFT_FORWARD);
233 #endif
234 if (result != CUFFT_SUCCESS) {
235 UTIL_THROW("Failure in cufft complex-to-complex forward transform");
236 }
237
238 // Rescale output data in-place
239 cudaReal scale = 1.0/cudaReal(rSize_);
240 VecOp::mulEqS(kField, scale);
241 }
242
243 /*
244 * Execute inverse (complex-to-complex) transform.
245 */
246 template <int D>
248 const
249 {
250 // Preconditions
251 UTIL_CHECK(isSetup_);
252 UTIL_CHECK(rField.capacity() == rSize_);
253 UTIL_CHECK(kField.capacity() == rSize_);
254 UTIL_CHECK(rField.meshDimensions() == meshDimensions_);
255 UTIL_CHECK(kField.meshDimensions() == meshDimensions_);
256
257 // Perform transform
258 // (See note at top of file explaining this use of const_cast)
259 cufftResult result;
260 #ifdef SINGLE_PRECISION
261 result = cufftExecC2C(ccPlan_, const_cast<cudaComplex*>(kField.cArray()),
262 rField.cArray(), CUFFT_INVERSE);
263 #else
264 result = cufftExecZ2Z(ccPlan_, const_cast<cudaComplex*>(kField.cArray()),
265 rField.cArray(), CUFFT_INVERSE);
266 #endif
267 if (result != CUFFT_SUCCESS) {
268 UTIL_THROW( "Failure in cufft complex-to-complex inverse transform");
269 }
270
271 }
272
273 // Static function
274
275 /*
276 * Compute dimensions and size of the mesh for the DFT of real data.
277 */
278 template <int D>
279 void FFT<D,CUT>::computeKMesh(IntVec<D> const & rMeshDimensions,
280 IntVec<D> & kMeshDimensions,
281 int & kSize )
282 {
283 kSize = 1;
284 for (int i = 0; i < D; ++i) {
285 if (i < D - 1) {
286 kMeshDimensions[i] = rMeshDimensions[i];
287 } else {
288 kMeshDimensions[i] = rMeshDimensions[i]/2 + 1;
289 }
290 kSize *= kMeshDimensions[i];
291 }
292 }
293
294} // namespace Pscf
295#endif
void inverseTransformUnsafe(RFieldDft< D, CUT > &kField, RField< D, CUT > &rField) const
Compute inverse (complex-to-real) DFT, overwriting the input.
Definition cuda/FFT.tpp:166
const IntVec< D > & meshDimensions() const
Return the dimensions of the grid for which this was allocated.
Definition cuda/FFT.h:252
FFT()
Default constructor.
Definition cuda/FFT.tpp:67
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_CHECK(condition)
Assertion macro suitable for serial or parallel production code.
Definition global.h:68
#define UTIL_THROW(msg)
Macro for throwing an Exception, reporting function, file and line number.
Definition global.h:49
void mulEqS(Array< double > &a, double b)
Vector-scalar in-place multiplication, a[i] *= b (real).
Definition VecOp.cpp:270
PSCF package top-level namespace.
cufftDoubleComplex cudaComplex
Complex number type used in CPU code that uses FFTW.
Definition cudaTypes.h:22
cufftDoubleReal cudaReal
Real number type used in CPU code that uses FFTW.
Definition cudaTypes.h:35