CudaKernels.cpp 14.1 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
/* -------------------------------------------------------------------------- *
 *                                   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.                                     *
 * -------------------------------------------------------------------------- */

32
33
#include "OpenMMContext.h"

34
35
#include "CudaKernels.h"
#include "CudaStreamImpl.h"
36
37
#include "LangevinIntegrator.h"
#include "ReferencePlatform.h"
38
#include "internal/OpenMMContextImpl.h"
39
#include "kernels/gputypes.h"
40
#include "kernels/cudaKernels.h"
41
42
43
44
45
46
47
48
49
50
#include <cmath>

extern "C" int gpuSetConstants( gpuContext gpu );

using namespace OpenMM;
using namespace std;

CudaCalcStandardMMForceFieldKernel::~CudaCalcStandardMMForceFieldKernel() {
}

51
52
53
54
55
56
57
void CudaCalcStandardMMForceFieldKernel::initialize(const System& system, const StandardMMForceField& force, const std::vector<std::set<int> >& exclusions) {
    numAtoms = force.getNumAtoms();
    numBonds = force.getNumBonds();
    numAngles = force.getNumAngles();
    numPeriodicTorsions = force.getNumPeriodicTorsions();
    numRBTorsions = force.getNumRBTorsions();
    num14 = force.getNumNonbonded14();
58
    const float RadiansToDegrees = 180.0/3.14159265;
59
    _gpuContext* gpu = data.gpu;
60
61
62
    
    // Initialize bonds.
    
63
64
65
66
67
68
    {
        vector<int> atom1(numBonds);
        vector<int> atom2(numBonds);
        vector<float> length(numBonds);
        vector<float> k(numBonds);
        for (int i = 0; i < numBonds; i++) {
69
70
71
72
            double lengthValue, kValue;
            force.getBondParameters(i, atom1[i], atom2[i], lengthValue, kValue);
            length[i] = (float) lengthValue;
            k[i] = (float) kValue;
73
74
        }
        gpuSetBondParameters(gpu, atom1, atom2, length, k);
75
76
77
78
    }
    
    // Initialize angles.
    
79
80
81
82
83
84
85
    {
        vector<int> atom1(numAngles);
        vector<int> atom2(numAngles);
        vector<int> atom3(numAngles);
        vector<float> angle(numAngles);
        vector<float> k(numAngles);
        for (int i = 0; i < numAngles; i++) {
86
87
88
89
            double angleValue, kValue;
            force.getAngleParameters(i, atom1[i], atom2[i], atom3[i], angleValue, kValue);
            angle[i] = (float) (angleValue*RadiansToDegrees);
            k[i] = (float) kValue;
90
91
        }
        gpuSetBondAngleParameters(gpu, atom1, atom2, atom3, angle, k);
92
93
94
95
    }

    // Initialize periodic torsions.
    
96
97
98
99
100
101
102
103
104
    {
        vector<int> atom1(numPeriodicTorsions);
        vector<int> atom2(numPeriodicTorsions);
        vector<int> atom3(numPeriodicTorsions);
        vector<int> atom4(numPeriodicTorsions);
        vector<float> k(numPeriodicTorsions);
        vector<float> phase(numPeriodicTorsions);
        vector<int> periodicity(numPeriodicTorsions);
        for (int i = 0; i < numPeriodicTorsions; i++) {
105
106
107
108
            double kValue, phaseValue;
            force.getPeriodicTorsionParameters(i, atom1[i], atom2[i], atom3[i], atom4[i], periodicity[i], phaseValue, kValue);
            k[i] = (float) kValue;
            phase[i] = (float) (phaseValue*RadiansToDegrees);
109
110
        }
        gpuSetDihedralParameters(gpu, atom1, atom2, atom3, atom4, k, phase, periodicity);
111
112
113
114
    }
    
    // Initialize Ryckaert-Bellemans torsions.
    
115
116
117
118
119
120
121
122
123
124
125
126
    {
        vector<int> atom1(numRBTorsions);
        vector<int> atom2(numRBTorsions);
        vector<int> atom3(numRBTorsions);
        vector<int> atom4(numRBTorsions);
        vector<float> c0(numRBTorsions);
        vector<float> c1(numRBTorsions);
        vector<float> c2(numRBTorsions);
        vector<float> c3(numRBTorsions);
        vector<float> c4(numRBTorsions);
        vector<float> c5(numRBTorsions);
        for (int i = 0; i < numRBTorsions; i++) {
127
128
129
130
131
132
133
134
            double c[6];
            force.getRBTorsionParameters(i, atom1[i], atom2[i], atom3[i], atom4[i], c[0], c[1], c[2], c[3], c[4], c[5]);
            c0[i] = (float) c[0];
            c1[i] = (float) c[1];
            c2[i] = (float) c[2];
            c3[i] = (float) c[3];
            c4[i] = (float) c[4];
            c5[i] = (float) c[5];
135
136
        }
        gpuSetRbDihedralParameters(gpu, atom1, atom2, atom3, atom4, c0, c1, c2, c3, c4, c5);
137
138
139
140
    }
    
    // Initialize nonbonded interactions.
    
141
142
143
144
145
146
147
148
    {
        vector<int> atom(numAtoms);
        vector<float> c6(numAtoms);
        vector<float> c12(numAtoms);
        vector<float> q(numAtoms);
        vector<char> symbol;
        vector<vector<int> > exclusionList(numAtoms);
        for (int i = 0; i < numAtoms; i++) {
149
150
            double charge, radius, depth;
            force.getAtomParameters(i, charge, radius, depth);
151
            atom[i] = i;
152
153
154
            q[i] = (float) charge;
            c6[i] = (float) (4*depth*pow(radius, 6.0));
            c12[i] = (float) (4*depth*pow(radius, 12.0));
155
156
157
158
            exclusionList[i] = vector<int>(exclusions[i].begin(), exclusions[i].end());
            exclusionList[i].push_back(i);
        }
        gpuSetCoulombParameters(gpu, 138.935485f, atom, c6, c12, q, symbol, exclusionList);
159
160
161
162
    }

    // Initialize 1-4 nonbonded interactions.
    
163
164
165
166
167
168
169
170
    {
        vector<int> atom1(num14);
        vector<int> atom2(num14);
        vector<float> c6(num14);
        vector<float> c12(num14);
        vector<float> q1(num14);
        vector<float> q2(num14);
        for (int i = 0; i < num14; i++) {
171
172
173
174
175
176
177
            double charge, sig, eps;
            force.getNonbonded14Parameters(i, atom1[i], atom2[i], charge, sig, eps);
            c6[i] = (float) (4*eps*pow(sig, 6.0));
            c12[i] = (float) (4*eps*pow(sig, 12.0));
            float q = (float) std::sqrt(charge);
            q1[i] = q;
            q2[i] = q;
178
        }
179
        gpuSetLJ14Parameters(gpu, 138.935485f, 1.0f, atom1, atom2, c6, c12, q1, q2);
180
181
182
    }
}

183
void CudaCalcStandardMMForceFieldKernel::executeForces(OpenMMContextImpl& context) {
184
185
186
187
188
189
190
191
192
193
    _gpuContext* gpu = data.gpu;
    if (data.useOBC) {
        kCalculateCDLJObcGbsaForces1(gpu);
        kReduceObcGbsaBornForces(gpu);
        kCalculateObcGbsaForces2(gpu);
    }
    else {
        kClearForces(gpu);
        kCalculateCDLJForces(gpu);
    }
194
195
    kCalculateLocalForces(gpu);
    kReduceBornSumAndForces(gpu);
196
197
}

198
double CudaCalcStandardMMForceFieldKernel::executeEnergy(OpenMMContextImpl& context) {
199
200
201
202
203
    // We don't currently have GPU kernels to calculate energy, so instead we have the reference
    // platform do it.  This is VERY slow.
    
    LangevinIntegrator integrator(0.0, 1.0, 0.0);
    ReferencePlatform platform;
204
205
    OpenMMContext refContext(system, integrator, platform);
    const Stream& positions = context.getPositions();
206
207
208
209
210
211
    double* posData = new double[positions.getSize()*3];
    positions.saveToArray(posData);
    vector<Vec3> pos(positions.getSize());
    for (int i = 0; i < pos.size(); i++)
        pos[i] = Vec3(posData[3*i], posData[3*i+1], posData[3*i+2]);
    delete[] posData;
212
213
    refContext.setPositions(pos);
    return refContext.getState(State::Energy).getPotentialEnergy();
214
215
}

216
217
218
CudaCalcGBSAOBCForceFieldKernel::~CudaCalcGBSAOBCForceFieldKernel() {
}

219
220
void CudaCalcGBSAOBCForceFieldKernel::initialize(const System& system, const GBSAOBCForceField& force) {
    int numAtoms = system.getNumAtoms();
221
222
223
224
225
    _gpuContext* gpu = data.gpu;
    vector<int> atom(numAtoms);
    vector<float> radius(numAtoms);
    vector<float> scale(numAtoms);
    for (int i = 0; i < numAtoms; i++) {
226
227
        double charge, atomRadius, scalingFactor;
        force.getAtomParameters(i, charge, atomRadius, scalingFactor);
228
        atom[i] = i;
229
230
        radius[i] = (float) atomRadius;
        scale[i] = (float) scalingFactor;
231
    }
232
    gpuSetObcParameters(gpu, force.getSoluteDielectric(), force.getSolventDielectric(), atom, radius, scale);
233
234
235
    data.useOBC = true;
}

236
void CudaCalcGBSAOBCForceFieldKernel::executeForces(OpenMMContextImpl& context) {
237
238
}

239
double CudaCalcGBSAOBCForceFieldKernel::executeEnergy(OpenMMContextImpl& context) {
240
241
}

242
243
244
//CudaIntegrateVerletStepKernel::~CudaIntegrateVerletStepKernel() {
//}
//
245
//void CudaIntegrateVerletStepKernel::initialize(const System& system, const VerletIntegrator& integrator) {
246
247
//}
//
248
//void CudaIntegrateVerletStepKernel::execute(OpenMMContextImpl& context, const VerletIntegrator& integrator) {
249
//}
250
251
252
253

CudaIntegrateLangevinStepKernel::~CudaIntegrateLangevinStepKernel() {
}

254
void CudaIntegrateLangevinStepKernel::initialize(const System& system, const LangevinIntegrator& integrator) {
255
256
257
    
    // Set masses.
    
258
    _gpuContext* gpu = data.gpu;
259
260
261
262
    int numAtoms = system.getNumAtoms();
    vector<float> mass(numAtoms);
    for (int i = 0; i < numAtoms; i++)
        mass[i] = (float) system.getAtomMass(i);
263
264
265
266
    gpuSetMass(gpu, mass);
    
    // Set constraints.
    
267
    int numConstraints = system.getNumConstraints();
268
269
270
271
272
273
    vector<int> atom1(numConstraints);
    vector<int> atom2(numConstraints);
    vector<float> distance(numConstraints);
    vector<float> invMass1(numConstraints);
    vector<float> invMass2(numConstraints);
    for (int i = 0; i < numConstraints; i++) {
274
275
276
277
278
279
280
281
        int atom1Index, atom2Index;
        double constraintDistance;
        system.getConstraintParameters(i, atom1Index, atom2Index, constraintDistance);
        atom1[i] = atom1Index;
        atom2[i] = atom2Index;
        distance[i] = (float) constraintDistance;
        invMass1[i] = 1.0f/mass[atom1Index];
        invMass2[i] = 1.0f/mass[atom2Index];
282
283
    }
    gpuSetShakeParameters(gpu, atom1, atom2, distance, invMass1, invMass2);
284
285
286
    gpuBuildThreadBlockWorkList(gpu);
    gpuBuildExclusionList(gpu);
    gpuBuildOutputBuffers(gpu);
287
    gpuSetConstants(gpu);
288
289
290
291
292
    kCalculateObcGbsaBornSum(gpu);
    kReduceObcGbsaBornSum(gpu);
    kClearBornForces(gpu);
    kClearForces(gpu);
    cudaThreadSynchronize();
293
294
295
    prevStepSize = -1.0;
}

296
void CudaIntegrateLangevinStepKernel::execute(OpenMMContextImpl& context, const LangevinIntegrator& integrator) {
297
    _gpuContext* gpu = data.gpu;
298
299
300
    double temperature = integrator.getTemperature();
    double friction = integrator.getFriction();
    double stepSize = integrator.getStepSize();
301
302
303
304
305
306
307
308
309
310
311
312
313
    if (temperature != prevTemp || friction != prevFriction || stepSize != prevStepSize) {
        // Initialize the GPU parameters.
        
        double tau = (friction == 0.0 ? 0.0 : 1.0/friction);
        gpuSetIntegrationParameters(gpu, tau, stepSize, temperature);
        gpuSetConstants(gpu);
        kGenerateRandoms(gpu);
        prevTemp = temperature;
        prevFriction = friction;
        prevStepSize = stepSize;
    }
    kUpdatePart1(gpu);
    kApplyFirstShake(gpu);
314
315
316
317
318
    if (data.removeCM) {
        int step = context.getTime()/stepSize;
        if (step%data.cmMotionFrequency == 0)
            gpu->bCalculateCM = true;
    }
319
320
321
    kUpdatePart2(gpu);
    kApplySecondShake(gpu);
}
322
323
324
325
//
//CudaIntegrateBrownianStepKernel::~CudaIntegrateBrownianStepKernel() {
//}
//
326
//void CudaIntegrateBrownianStepKernel::initialize(const System& system, const BrownianIntegrator& integrator) {
327
328
//}
//
329
//void CudaIntegrateBrownianStepKernel::execute(OpenMMContextImpl& context, const BrownianIntegrator& integrator) {
330
331
332
333
334
//}
//
//CudaApplyAndersenThermostatKernel::~CudaApplyAndersenThermostatKernel() {
//}
//
335
//void CudaApplyAndersenThermostatKernel::initialize(const System& system, const AndersenThermostat& thermostat) {
336
337
//}
//
338
//void CudaApplyAndersenThermostatKernel::execute(OpenMMContextImpl& context) {
339
//}
340

341
342
343
344
345
void CudaCalcKineticEnergyKernel::initialize(const System& system) {
    int numAtoms = system.getNumAtoms();
    masses.resize(numAtoms);
    for (size_t i = 0; i < numAtoms; ++i)
        masses[i] = system.getAtomMass(i);
346
347
}

348
double CudaCalcKineticEnergyKernel::execute(OpenMMContextImpl& context) {
349
350
351
    // We don't currently have a GPU kernel to do this, so we retrieve the velocities and calculate the energy
    // on the CPU.
    
352
    const Stream& velocities = context.getVelocities();
353
354
355
356
357
358
359
360
    double* v = new double[velocities.getSize()*3];
    velocities.saveToArray(v);
    double energy = 0.0;
    for (size_t i = 0; i < masses.size(); ++i)
        energy += masses[i]*(v[i*3]*v[i*3]+v[i*3+1]*v[i*3+1]+v[i*3+2]*v[i*3+2]);
    delete v;
    return 0.5*energy;
}
361

362
void CudaRemoveCMMotionKernel::initialize(const System& system, const CMMotionRemover& force) {
363
    data.removeCM = true;
364
    data.cmMotionFrequency = force.getFrequency();
365
366
}

367
void CudaRemoveCMMotionKernel::execute(OpenMMContextImpl& context) {
368
}