CudaFFT3D.cpp 4.74 KB
Newer Older
peastman's avatar
peastman committed
1
2
3
4
5
6
7
8
/* -------------------------------------------------------------------------- *
 *                                   OpenMM                                   *
 * -------------------------------------------------------------------------- *
 * This is part of the OpenMM molecular simulation toolkit originating from   *
 * Simbios, the NIH National Center for Physics-Based Simulation of           *
 * Biological Structures at Stanford, funded under the NIH Roadmap for        *
 * Medical Research, grant U54 GM072970. See https://simtk.org.               *
 *                                                                            *
9
 * Portions copyright (c) 2009-2025 Stanford University and the Authors.      *
peastman's avatar
peastman committed
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
 * Authors: Peter Eastman                                                     *
 * Contributors:                                                              *
 *                                                                            *
 * This program is free software: you can redistribute it and/or modify       *
 * it under the terms of the GNU Lesser General Public License as published   *
 * by the Free Software Foundation, either version 3 of the License, or       *
 * (at your option) any later version.                                        *
 *                                                                            *
 * This program is distributed in the hope that it will be useful,            *
 * but WITHOUT ANY WARRANTY; without even the implied warranty of             *
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the              *
 * GNU Lesser General Public License for more details.                        *
 *                                                                            *
 * You should have received a copy of the GNU Lesser General Public License   *
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.      *
 * -------------------------------------------------------------------------- */

#include "CudaFFT3D.h"
#include "CudaContext.h"

using namespace OpenMM;

CudaFFT3D::CudaFFT3D(CudaContext& context, int xsize, int ysize, int zsize, bool realToComplex) :
33
34
        context(context), realToComplex(realToComplex), hasInitialized(false) {
    cufftType type1, type2;
peastman's avatar
peastman committed
35
    if (realToComplex) {
36
37
38
        if (context.getUseDoublePrecision()) {
            type1 = CUFFT_D2Z;
            type2 = CUFFT_Z2D;
peastman's avatar
peastman committed
39
        }
40
41
42
        else {
            type1 = CUFFT_R2C;
            type2 = CUFFT_C2R;
peastman's avatar
peastman committed
43
44
45
        }
    }
    else {
46
47
48
49
        if (context.getUseDoublePrecision())
            type1 = type2 = CUFFT_Z2Z;
        else
            type1 = type2 = CUFFT_C2C;
peastman's avatar
peastman committed
50
    }
51
52
53
54
55
56
57
    cufftResult result = cufftPlan3d(&fftForward, xsize, ysize, zsize, type1);
    if (result != CUFFT_SUCCESS)
        throw OpenMMException("Error initializing FFT: "+context.intToString(result));
    result = cufftPlan3d(&fftBackward, xsize, ysize, zsize, type2);
    if (result != CUFFT_SUCCESS)
        throw OpenMMException("Error initializing FFT: "+context.intToString(result));
    hasInitialized = true;
peastman's avatar
peastman committed
58
59
}

60
61
62
63
CudaFFT3D::~CudaFFT3D() {
    if (hasInitialized) {
        cufftDestroy(fftForward);
        cufftDestroy(fftBackward);
peastman's avatar
peastman committed
64
65
66
    }
}

67
68
69
70
71
void CudaFFT3D::execFFT(ArrayInterface& in, ArrayInterface& out, bool forward) {
    CUdeviceptr in2 = context.unwrap(in).getDevicePointer();
    CUdeviceptr out2 = context.unwrap(out).getDevicePointer();
    cufftResult result;
    if (forward) {
72
        cufftSetStream(fftForward, context.getCurrentStream());
73
74
75
76
77
        if (realToComplex) {
            if (context.getUseDoublePrecision())
                result = cufftExecD2Z(fftForward, (double*) in2, (double2*) out2);
            else
                result = cufftExecR2C(fftForward, (float*) in2, (float2*) out2);
peastman's avatar
peastman committed
78
        }
79
80
81
82
83
        else {
            if (context.getUseDoublePrecision())
                result = cufftExecZ2Z(fftForward, (double2*) in2, (double2*) out2, CUFFT_FORWARD);
            else
                result = cufftExecC2C(fftForward, (float2*) in2, (float2*) out2, CUFFT_FORWARD);
peastman's avatar
peastman committed
84
        }
85
86
    }
    else {
87
        cufftSetStream(fftBackward, context.getCurrentStream());
88
89
90
91
92
        if (realToComplex) {
            if (context.getUseDoublePrecision())
                result = cufftExecZ2D(fftBackward, (double2*) in2, (double*) out2);
            else
                result = cufftExecC2R(fftBackward, (float2*) in2, (float*) out2);
peastman's avatar
peastman committed
93
        }
94
95
96
97
98
        else {
            if (context.getUseDoublePrecision())
                result = cufftExecZ2Z(fftBackward, (double2*) in2, (double2*) out2, CUFFT_INVERSE);
            else
                result = cufftExecC2C(fftBackward, (float2*) in2, (float2*) out2, CUFFT_INVERSE);
peastman's avatar
peastman committed
99
100
        }
    }
101
102
    if (result != CUFFT_SUCCESS)
        throw OpenMMException("Error executing FFT: "+context.intToString(result));
peastman's avatar
peastman committed
103
}