AmoebaCudaKernels.cpp 8.82 KB
Newer Older
1
2
3
4
5
6
7
8
/* -------------------------------------------------------------------------- *
 *                               OpenMMAmoeba                                 *
 * -------------------------------------------------------------------------- *
 * 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) 2008-2021 Stanford University and the Authors.      *
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
 * Authors: Peter Eastman, Mark Friedrichs                                    *
 * 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/>.      *
 * -------------------------------------------------------------------------- */

27
28
29
#ifdef WIN32
  #define _USE_MATH_DEFINES // Needed to get M_PI
#endif
30
31
#include "AmoebaCudaKernels.h"
#include "CudaAmoebaKernelSources.h"
32
#include "openmm/common/ContextSelector.h"
33
#include "openmm/internal/ContextImpl.h"
34
#include "openmm/internal/AmoebaGeneralizedKirkwoodForceImpl.h"
35
36
37
#include "openmm/internal/AmoebaMultipoleForceImpl.h"
#include "openmm/internal/AmoebaWcaDispersionForceImpl.h"
#include "openmm/internal/AmoebaTorsionTorsionForceImpl.h"
38
#include "openmm/internal/AmoebaVdwForceImpl.h"
39
40
#include "openmm/internal/NonbondedForceImpl.h"
#include "CudaBondedUtilities.h"
41
#include "CudaFFT3D.h"
42
43
#include "CudaForceInfo.h"
#include "CudaKernelSources.h"
44
#include "SimTKOpenMMRealType.h"
Peter Eastman's avatar
Peter Eastman committed
45
#include "jama_lu.h"
46

47
#include <algorithm>
48
49
50
51
52
53
54
55
#include <cmath>
#ifdef _MSC_VER
#include <windows.h>
#endif

using namespace OpenMM;
using namespace std;

Peter Eastman's avatar
Peter Eastman committed
56
#define CHECK_RESULT(result, prefix) \
57
58
    if (result != CUDA_SUCCESS) { \
        std::stringstream m; \
Peter Eastman's avatar
Peter Eastman committed
59
        m<<prefix<<": "<<cu.getErrorString(result)<<" ("<<result<<")"<<" at "<<__FILE__<<":"<<__LINE__; \
60
61
62
        throw OpenMMException(m.str());\
    }

63
64
65
66
67
68
69
70
71
static void setPeriodicBoxArgs(ComputeContext& cc, ComputeKernel kernel, int index) {
    Vec3 a, b, c;
    cc.getPeriodicBoxVectors(a, b, c);
    if (cc.getUseDoublePrecision()) {
        kernel->setArg(index++, mm_double4(a[0], b[1], c[2], 0.0));
        kernel->setArg(index++, mm_double4(1.0/a[0], 1.0/b[1], 1.0/c[2], 0.0));
        kernel->setArg(index++, mm_double4(a[0], a[1], a[2], 0.0));
        kernel->setArg(index++, mm_double4(b[0], b[1], b[2], 0.0));
        kernel->setArg(index, mm_double4(c[0], c[1], c[2], 0.0));
72
    }
73
74
75
76
77
78
    else {
        kernel->setArg(index++, mm_float4((float) a[0], (float) b[1], (float) c[2], 0.0f));
        kernel->setArg(index++, mm_float4(1.0f/(float) a[0], 1.0f/(float) b[1], 1.0f/(float) c[2], 0.0f));
        kernel->setArg(index++, mm_float4((float) a[0], (float) a[1], (float) a[2], 0.0f));
        kernel->setArg(index++, mm_float4((float) b[0], (float) b[1], (float) b[2], 0.0f));
        kernel->setArg(index, mm_float4((float) c[0], (float) c[1], (float) c[2], 0.0f));
79
80
81
    }
}

82
83
84
85
86
/* -------------------------------------------------------------------------- *
 *                             AmoebaMultipole                                *
 * -------------------------------------------------------------------------- */

CudaCalcAmoebaMultipoleForceKernel::~CudaCalcAmoebaMultipoleForceKernel() {
87
    ContextSelector selector(cc);
88
89
90
91
    if (hasInitializedFFT)
        cufftDestroy(fft);
}

92
void CudaCalcAmoebaMultipoleForceKernel::initialize(const System& system, const AmoebaMultipoleForce& force) {
93
    CommonCalcAmoebaMultipoleForceKernel::initialize(system, force);
94
    if (usePME) {
95
        ContextSelector selector(cc);
96
        cufftResult result = cufftPlan3d(&fft, gridSizeX, gridSizeY, gridSizeZ, cc.getUseDoublePrecision() ? CUFFT_Z2Z : CUFFT_C2C);
97
        if (result != CUFFT_SUCCESS)
98
            throw OpenMMException("Error initializing FFT: "+cc.intToString(result));
99
100
        hasInitializedFFT = true;
    }
101
102
}

103
104
105
106
107
108
void CudaCalcAmoebaMultipoleForceKernel::computeFFT(bool forward) {
    CudaArray& grid1 = dynamic_cast<CudaContext&>(cc).unwrap(pmeGrid1);
    CudaArray& grid2 = dynamic_cast<CudaContext&>(cc).unwrap(pmeGrid2);
    if (forward) {
        if (cc.getUseDoublePrecision())
            cufftExecZ2Z(fft, (double2*) grid1.getDevicePointer(), (double2*) grid2.getDevicePointer(), CUFFT_FORWARD);
109
        else
110
            cufftExecC2C(fft, (float2*) grid1.getDevicePointer(), (float2*) grid2.getDevicePointer(), CUFFT_FORWARD);
111
    }
112
    else {
113
114
        if (cc.getUseDoublePrecision())
            cufftExecZ2Z(fft, (double2*) grid2.getDevicePointer(), (double2*) grid1.getDevicePointer(), CUFFT_INVERSE);
115
        else
116
            cufftExecC2C(fft, (float2*) grid2.getDevicePointer(), (float2*) grid1.getDevicePointer(), CUFFT_INVERSE);
117
118
119
    }
}

peastman's avatar
peastman committed
120
121
122
123
124
/* -------------------------------------------------------------------------- *
 *                           HippoNonbondedForce                              *
 * -------------------------------------------------------------------------- */

CudaCalcHippoNonbondedForceKernel::~CudaCalcHippoNonbondedForceKernel() {
125
    ContextSelector selector(cc);
peastman's avatar
peastman committed
126
127
128
129
130
131
132
133
134
135
136
    if (sort != NULL)
        delete sort;
    if (hasInitializedFFT) {
        cufftDestroy(fftForward);
        cufftDestroy(fftBackward);
        cufftDestroy(dfftForward);
        cufftDestroy(dfftBackward);
    }
}

void CudaCalcHippoNonbondedForceKernel::initialize(const System& system, const HippoNonbondedForce& force) {
137
    CommonCalcHippoNonbondedForceKernel::initialize(system, force);
peastman's avatar
peastman committed
138
    if (usePME) {
139
        ContextSelector selector(cc);
140
141
142
        CudaContext& cu = dynamic_cast<CudaContext&>(cc);
        sort = new CudaSort(cu, new SortTrait(), cc.getNumAtoms());
        cufftResult result = cufftPlan3d(&fftForward, gridSizeX, gridSizeY, gridSizeZ, cc.getUseDoublePrecision() ? CUFFT_D2Z : CUFFT_R2C);
peastman's avatar
peastman committed
143
        if (result != CUFFT_SUCCESS)
144
145
            throw OpenMMException("Error initializing FFT: "+cc.intToString(result));
        result = cufftPlan3d(&fftBackward, gridSizeX, gridSizeY, gridSizeZ, cc.getUseDoublePrecision() ? CUFFT_Z2D : CUFFT_C2R);
peastman's avatar
peastman committed
146
        if (result != CUFFT_SUCCESS)
147
148
            throw OpenMMException("Error initializing FFT: "+cc.intToString(result));
        result = cufftPlan3d(&dfftForward, dispersionGridSizeX, dispersionGridSizeY, dispersionGridSizeZ, cc.getUseDoublePrecision() ? CUFFT_D2Z : CUFFT_R2C);
peastman's avatar
peastman committed
149
        if (result != CUFFT_SUCCESS)
150
151
            throw OpenMMException("Error initializing FFT: "+cc.intToString(result));
        result = cufftPlan3d(&dfftBackward, dispersionGridSizeX, dispersionGridSizeY, dispersionGridSizeZ, cc.getUseDoublePrecision() ? CUFFT_Z2D : CUFFT_C2R);
peastman's avatar
peastman committed
152
        if (result != CUFFT_SUCCESS)
153
            throw OpenMMException("Error initializing FFT: "+cc.intToString(result));
peastman's avatar
peastman committed
154
155
156
157
        hasInitializedFFT = true;
    }
}

158
159
160
161
162
163
164
void CudaCalcHippoNonbondedForceKernel::computeFFT(bool forward, bool dispersion) {
    CudaArray& grid1 = dynamic_cast<CudaContext&>(cc).unwrap(pmeGrid1);
    CudaArray& grid2 = dynamic_cast<CudaContext&>(cc).unwrap(pmeGrid2);
    if (forward) {
        cufftHandle fft = dispersion ? dfftForward : fftForward;
        if (cc.getUseDoublePrecision())
            cufftExecD2Z(fft, (double*) grid1.getDevicePointer(), (double2*) grid2.getDevicePointer());
peastman's avatar
peastman committed
165
        else
166
            cufftExecR2C(fft, (float*) grid1.getDevicePointer(), (float2*) grid2.getDevicePointer());
peastman's avatar
peastman committed
167
168
    }
    else {
169
170
171
172
173
        cufftHandle fft = dispersion ? dfftBackward : fftBackward;
        if (cc.getUseDoublePrecision())
            cufftExecZ2D(fft, (double2*) grid2.getDevicePointer(), (double*) grid1.getDevicePointer());
        else
            cufftExecC2R(fft, (float2*) grid2.getDevicePointer(), (float*) grid1.getDevicePointer());
peastman's avatar
peastman committed
174
175
176
    }
}

177
178
void CudaCalcHippoNonbondedForceKernel::sortGridIndex() {
    sort->sort(dynamic_cast<CudaContext&>(cc).unwrap(pmeAtomGridIndex));
peastman's avatar
peastman committed
179
}