CudaKernels.cpp 15.5 KB
Newer Older
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
/* -------------------------------------------------------------------------- *
 *                                   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.               *
 *                                                                            *
 * Portions copyright (c) 2008 Stanford University and the Authors.           *
 * Authors: Peter Eastman                                                     *
 * Contributors:                                                              *
 *                                                                            *
 * Permission is hereby granted, free of charge, to any person obtaining a    *
 * copy of this software and associated documentation files (the "Software"), *
 * to deal in the Software without restriction, including without limitation  *
 * the rights to use, copy, modify, merge, publish, distribute, sublicense,   *
 * and/or sell copies of the Software, and to permit persons to whom the      *
 * Software is furnished to do so, subject to the following conditions:       *
 *                                                                            *
 * The above copyright notice and this permission notice shall be included in *
 * all copies or substantial portions of the Software.                        *
 *                                                                            *
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR *
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,   *
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL    *
 * THE AUTHORS, CONTRIBUTORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM,    *
 * DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR      *
 * OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE  *
 * USE OR OTHER DEALINGS IN THE SOFTWARE.                                     *
 * -------------------------------------------------------------------------- */

#include "CudaKernels.h"
#include "CudaStreamImpl.h"
#include "kernels/gputypes.h"
#include <cmath>

extern "C" int gpuSetConstants( gpuContext gpu );

using namespace OpenMM;
using namespace std;

CudaCalcStandardMMForceFieldKernel::~CudaCalcStandardMMForceFieldKernel() {
}

void CudaCalcStandardMMForceFieldKernel::initialize(const vector<vector<int> >& bondIndices, const vector<vector<double> >& bondParameters,
        const vector<vector<int> >& angleIndices, const vector<vector<double> >& angleParameters,
        const vector<vector<int> >& periodicTorsionIndices, const vector<vector<double> >& periodicTorsionParameters,
        const vector<vector<int> >& rbTorsionIndices, const vector<vector<double> >& rbTorsionParameters,
        const vector<vector<int> >& bonded14Indices, double lj14Scale, double coulomb14Scale,
        const vector<set<int> >& exclusions, const vector<vector<double> >& nonbondedParameters,
        NonbondedMethod nonbondedMethod, double nonbondedCutoff, double periodicBoxSize[3]) {
    numAtoms = nonbondedParameters.size();
    numBonds = bondIndices.size();
    numAngles = angleIndices.size();
    numPeriodicTorsions = periodicTorsionIndices.size();
    numRBTorsions = rbTorsionIndices.size();
    num14 = bonded14Indices.size();
    
    // Initialize bonds.
    
    gpu->sim.bonds                      = numBonds;
    CUDAStream<int4>* psBondID          = new CUDAStream<int4>(numBonds, 1);
    gpu->psBondID                       = psBondID;
    gpu->sim.pBondID                    = psBondID->_pDevStream[0];
    CUDAStream<float2>* psBondParameter = new CUDAStream<float2>(numBonds, 1);
    gpu->psBondParameter                = psBondParameter;
    gpu->sim.pBondParameter             = psBondParameter->_pDevStream[0];
    for (int i = 0; i < numBonds; i++ ) {
        psBondID->_pSysStream[0][i].x        = bondIndices[i][0];
        psBondID->_pSysStream[0][i].y        = bondIndices[i][1];
        psBondID->_pSysStream[0][i].z        = gpu->pOutputBufferCounter[psBondID->_pSysStream[0][i].x]++;
        psBondID->_pSysStream[0][i].w        = gpu->pOutputBufferCounter[psBondID->_pSysStream[0][i].y]++;
        psBondParameter->_pSysStream[0][i].x = bondParameters[i][0];
        psBondParameter->_pSysStream[0][i].y = bondParameters[i][1];
    }
    psBondID->Upload();
    psBondParameter->Upload();
    
    // Initialize angles.
    
    gpu->sim.bond_angles                     = numAngles;
    CUDAStream<int4>* psBondAngleID1         = new CUDAStream<int4>(numAngles, 1);
    gpu->psBondAngleID1                      = psBondAngleID1;
    gpu->sim.pBondAngleID1                   = psBondAngleID1->_pDevStream[0];
    CUDAStream<int2>* psBondAngleID2         = new CUDAStream<int2>(numAngles, 1);
    gpu->psBondAngleID2                      = psBondAngleID2;
    gpu->sim.pBondAngleID2                   = psBondAngleID2->_pDevStream[0];
    CUDAStream<float2>* psBondAngleParameter = new CUDAStream<float2>(numAngles, 1);
    gpu->psBondAngleParameter                = psBondAngleParameter;
    gpu->sim.pBondAngleParameter             = psBondAngleParameter->_pDevStream[0];
    for (int i = 0; i < numAngles; i++) {
        psBondAngleID1->_pSysStream[0][i].x         = angleIndices[i][0];
        psBondAngleID1->_pSysStream[0][i].y         = angleIndices[i][1];
        psBondAngleID1->_pSysStream[0][i].z         = angleIndices[i][2];
        psBondAngleID1->_pSysStream[0][i].w         = gpu->pOutputBufferCounter[psBondAngleID1->_pSysStream[0][i].x]++;
        psBondAngleID2->_pSysStream[0][i].x         = gpu->pOutputBufferCounter[psBondAngleID1->_pSysStream[0][i].y]++;
        psBondAngleID2->_pSysStream[0][i].y         = gpu->pOutputBufferCounter[psBondAngleID1->_pSysStream[0][i].z]++;
        psBondAngleParameter->_pSysStream[0][i].x   = angleParameters[i][0]*180.0/M_PI;
        psBondAngleParameter->_pSysStream[0][i].y   = angleParameters[i][1];
    }
    psBondAngleID1->Upload();
    psBondAngleID2->Upload();
    psBondAngleParameter->Upload();

    // Initialize periodic torsions.
    
    gpu->sim.dihedrals = numPeriodicTorsions;
    CUDAStream<int4>* psDihedralID1             = new CUDAStream<int4>(numPeriodicTorsions, 1);
    gpu->psDihedralID1                          = psDihedralID1;
    gpu->sim.pDihedralID1                       = psDihedralID1->_pDevStream[0];
    CUDAStream<int4>* psDihedralID2             = new CUDAStream<int4>(numPeriodicTorsions, 1);
    gpu->psDihedralID2                          = psDihedralID2;
    gpu->sim.pDihedralID2                       = psDihedralID2->_pDevStream[0];
    CUDAStream<float4>* psDihedralParameter     = new CUDAStream<float4>(numPeriodicTorsions, 1);
    gpu->psDihedralParameter                    = psDihedralParameter;
    gpu->sim.pDihedralParameter                 = psDihedralParameter->_pDevStream[0];
    for (int i = 0; i < numPeriodicTorsions; i++) {
        psDihedralID1->_pSysStream[0][i].x              = periodicTorsionIndices[i][0];
        psDihedralID1->_pSysStream[0][i].y              = periodicTorsionIndices[i][1];
        psDihedralID1->_pSysStream[0][i].z              = periodicTorsionIndices[i][2];
        psDihedralID1->_pSysStream[0][i].w              = periodicTorsionIndices[i][3];
        psDihedralID2->_pSysStream[0][i].x              = gpu->pOutputBufferCounter[psDihedralID1->_pSysStream[0][i].x]++;
        psDihedralID2->_pSysStream[0][i].y              = gpu->pOutputBufferCounter[psDihedralID1->_pSysStream[0][i].y]++;
        psDihedralID2->_pSysStream[0][i].z              = gpu->pOutputBufferCounter[psDihedralID1->_pSysStream[0][i].z]++;
        psDihedralID2->_pSysStream[0][i].w              = gpu->pOutputBufferCounter[psDihedralID1->_pSysStream[0][i].w]++;
        psDihedralParameter->_pSysStream[0][i].x        = periodicTorsionParameters[i][0];
        psDihedralParameter->_pSysStream[0][i].y        = periodicTorsionParameters[i][1];
        psDihedralParameter->_pSysStream[0][i].z        = periodicTorsionParameters[i][2];
        psDihedralParameter->_pSysStream[0][i].w        = 0.0f;
    }
    psDihedralID1->Upload();
    psDihedralID2->Upload();
    psDihedralParameter->Upload();
    
    // Initialize Ryckaert-Bellemans torsions.
    
    gpu->sim.rb_dihedrals = numRBTorsions;
    CUDAStream<int4>* psRbDihedralID1           = new CUDAStream<int4>(numRBTorsions, 1);
    gpu->psRbDihedralID1                        = psRbDihedralID1;
    gpu->sim.pRbDihedralID1                     = psRbDihedralID1->_pDevStream[0];
    CUDAStream<int4>* psRbDihedralID2           = new CUDAStream<int4>(numRBTorsions, 1);
    gpu->psRbDihedralID2                        = psRbDihedralID2;
    gpu->sim.pRbDihedralID2                     = psRbDihedralID2->_pDevStream[0];
    CUDAStream<float4>* psRbDihedralParameter1  = new CUDAStream<float4>(numRBTorsions, 1);
    gpu->psRbDihedralParameter1                 = psRbDihedralParameter1;
    gpu->sim.pRbDihedralParameter1              = psRbDihedralParameter1->_pDevStream[0];
    CUDAStream<float2>* psRbDihedralParameter2  = new CUDAStream<float2>(numRBTorsions, 1);	
    gpu->psRbDihedralParameter2                 = psRbDihedralParameter2;
    gpu->sim.pRbDihedralParameter2              = psRbDihedralParameter2->_pDevStream[0];
    for (int i = 0; i < numRBTorsions; i++) {
        psRbDihedralID1->_pSysStream[0][i].x            = rbTorsionIndices[i][0];
        psRbDihedralID1->_pSysStream[0][i].y            = rbTorsionIndices[i][1];
        psRbDihedralID1->_pSysStream[0][i].z            = rbTorsionIndices[i][2];
        psRbDihedralID1->_pSysStream[0][i].w            = rbTorsionIndices[i][3];
        psRbDihedralID2->_pSysStream[0][i].x            = gpu->pOutputBufferCounter[psRbDihedralID1->_pSysStream[0][i].x]++;
        psRbDihedralID2->_pSysStream[0][i].y            = gpu->pOutputBufferCounter[psRbDihedralID1->_pSysStream[0][i].y]++;
        psRbDihedralID2->_pSysStream[0][i].z            = gpu->pOutputBufferCounter[psRbDihedralID1->_pSysStream[0][i].z]++;
        psRbDihedralID2->_pSysStream[0][i].w            = gpu->pOutputBufferCounter[psRbDihedralID1->_pSysStream[0][i].w]++;
        psRbDihedralParameter1->_pSysStream[0][i].x     = rbTorsionParameters[i][0];
        psRbDihedralParameter1->_pSysStream[0][i].y     = rbTorsionParameters[i][1];
        psRbDihedralParameter1->_pSysStream[0][i].z     = rbTorsionParameters[i][2];
        psRbDihedralParameter1->_pSysStream[0][i].w     = rbTorsionParameters[i][3];
        psRbDihedralParameter2->_pSysStream[0][i].x     = rbTorsionParameters[i][4];
        psRbDihedralParameter2->_pSysStream[0][i].y     = rbTorsionParameters[i][5];
    }
    psRbDihedralID1->Upload();
    psRbDihedralID2->Upload();
    psRbDihedralParameter1->Upload();
    psRbDihedralParameter2->Upload();
    
    // Initialize nonbonded interactions.
    
    for (int i = 0; i < numAtoms; i++) {
        gpu->psPosq4->_pSysStream[0][i].w = nonbondedParameters[i][0];
        gpu->psSigEps2->_pSysStream[0][i].x = nonbondedParameters[i][1];
        gpu->psSigEps2->_pSysStream[0][i].y = nonbondedParameters[i][2];
    }
    gpu->psPosq4->Upload();
    gpu->psSigEps2->Upload();

    // Initialize 1-4 nonbonded interactions.
    
    gpu->sim.LJ14s                              = num14;
    CUDAStream<int4>* psLJ14ID                  = new CUDAStream<int4>(num14, 1);
    gpu->psLJ14ID                               = psLJ14ID;
    gpu->sim.pLJ14ID                            = psLJ14ID->_pDevStream[0];
    CUDAStream<float4>* psLJ14Parameter         = new CUDAStream<float4>(num14, 1);
    gpu->psLJ14Parameter                        = psLJ14Parameter;
    gpu->sim.pLJ14Parameter                     = psLJ14Parameter->_pDevStream[0];
    double sqrtEps = std::sqrt(138.935485);
    for (int i = 0; i < num14; i++) {
        int atom1 = bonded14Indices[i][0];
        int atom2 = bonded14Indices[i][1];
        double atom1params[] = {0.5*nonbondedParameters[atom1][1], 2.0*sqrt(nonbondedParameters[atom1][2]), nonbondedParameters[atom1][0]*sqrtEps};
        double atom2params[] = {0.5*nonbondedParameters[atom2][1], 2.0*sqrt(nonbondedParameters[atom2][2]), nonbondedParameters[atom2][0]*sqrtEps};
        psLJ14ID->_pSysStream[0][i].x          = atom1;
        psLJ14ID->_pSysStream[0][i].y          = atom2;
        psLJ14ID->_pSysStream[0][i].z          = gpu->pOutputBufferCounter[psLJ14ID->_pSysStream[0][i].x]++;
        psLJ14ID->_pSysStream[0][i].w          = gpu->pOutputBufferCounter[psLJ14ID->_pSysStream[0][i].y]++;
        psLJ14Parameter->_pSysStream[0][i].x   = atom1params[0]+atom2params[0];
        psLJ14Parameter->_pSysStream[0][i].y   = lj14Scale*(atom1params[1]*atom2params[1]);
        psLJ14Parameter->_pSysStream[0][i].z   = coulomb14Scale*(atom1params[2]*atom2params[2]);
    }
    psLJ14ID->Upload();
    psLJ14Parameter->Upload();
    
    // Initialize exclusions.
    
    // TODO
    
    // Finish initialization.
    
    gpuSetConstants(gpu);
}

void CudaCalcStandardMMForceFieldKernel::executeForces(const Stream& positions, Stream& forces) {
}

double CudaCalcStandardMMForceFieldKernel::executeEnergy(const Stream& positions) {
}

//CudaCalcGBSAOBCForceFieldKernel::~CudaCalcGBSAOBCForceFieldKernel() {
//}
//
//void CudaCalcGBSAOBCForceFieldKernel::initialize(const vector<vector<double> >& atomParameters, double solventDielectric, double soluteDielectric) {
//}
//
//void CudaCalcGBSAOBCForceFieldKernel::executeForces(const Stream& positions, Stream& forces) {
//}
//
//double CudaCalcGBSAOBCForceFieldKernel::executeEnergy(const Stream& positions) {
//}
//
//CudaIntegrateVerletStepKernel::~CudaIntegrateVerletStepKernel() {
//}
//
//void CudaIntegrateVerletStepKernel::initialize(const vector<double>& masses, const vector<vector<int> >& constraintIndices,
//}
//
//void CudaIntegrateVerletStepKernel::execute(Stream& positions, Stream& velocities, const Stream& forces, double stepSize) {
//}
//
//CudaIntegrateLangevinStepKernel::~CudaIntegrateLangevinStepKernel() {
//}
//
//void CudaIntegrateLangevinStepKernel::initialize(const vector<double>& masses, const vector<vector<int> >& constraintIndices,
//        const vector<double>& constraintLengths) {
//}
//
//void CudaIntegrateLangevinStepKernel::execute(Stream& positions, Stream& velocities, const Stream& forces, double temperature, double friction, double stepSize) {
//}
//
//CudaIntegrateBrownianStepKernel::~CudaIntegrateBrownianStepKernel() {
//}
//
//void CudaIntegrateBrownianStepKernel::initialize(const vector<double>& masses, const vector<vector<int> >& constraintIndices,
//        const vector<double>& constraintLengths) {
//}
//
//void CudaIntegrateBrownianStepKernel::execute(Stream& positions, Stream& velocities, const Stream& forces, double temperature, double friction, double stepSize) {
//}
//
//CudaApplyAndersenThermostatKernel::~CudaApplyAndersenThermostatKernel() {
//}
//
//void CudaApplyAndersenThermostatKernel::initialize(const vector<double>& masses) {
//}
//
//void CudaApplyAndersenThermostatKernel::execute(Stream& velocities, double temperature, double collisionFrequency, double stepSize) {
//}
//
//void CudaCalcKineticEnergyKernel::initialize(const vector<double>& masses) {
//}
//
//double CudaCalcKineticEnergyKernel::execute(const Stream& velocities) {
//}
//
//void CudaRemoveCMMotionKernel::initialize(const vector<double>& masses) {
//}
//
//void CudaRemoveCMMotionKernel::execute(Stream& velocities) {
//}