CudaKernels.cpp 22.8 KB
Newer Older
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) 2008-2009 Stanford University and the Authors.      *
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
 * 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
#include <cmath>

extern "C" int gpuSetConstants( gpuContext gpu );

using namespace OpenMM;
using namespace std;

48
49
static void calcForces(OpenMMContextImpl& context, CudaPlatform::PlatformData& data) {
    _gpuContext* gpu = data.gpu;
50
    if (data.nonbondedMethod != NO_CUTOFF && data.stepCount%100 == 0)
51
52
53
54
        gpuReorderAtoms(gpu);
    data.stepCount++;
    kClearForces(gpu);
    if (gpu->bIncludeGBSA) {
55
        gpu->bRecalculateBornRadii = true;
56
57
58
59
        kCalculateCDLJObcGbsaForces1(gpu);
        kReduceObcGbsaBornForces(gpu);
        kCalculateObcGbsaForces2(gpu);
    }
60
    else if (data.hasNonbonded) {
61
62
63
        kCalculateCDLJForces(gpu);
    }
    kCalculateLocalForces(gpu);
64
65
66
67
    if (gpu->bIncludeGBSA)
        kReduceBornSumAndForces(gpu);
    else
        kReduceForces(gpu);
68
69
}

70
71
72
static double calcEnergy(OpenMMContextImpl& context, System& system) {
    // We don't currently have GPU kernels to calculate energy, so instead we have the reference
    // platform do it.  This is VERY slow.
73

74
75
76
77
78
79
80
81
82
83
84
85
86
87
    LangevinIntegrator integrator(0.0, 1.0, 0.0);
    ReferencePlatform platform;
    OpenMMContext refContext(system, integrator, platform);
    const Stream& positions = context.getPositions();
    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;
    refContext.setPositions(pos);
    return refContext.getState(State::Energy).getPotentialEnergy();
}

88
89
90
void CudaInitializeForcesKernel::initialize(const System& system) {
}

91
void CudaInitializeForcesKernel::execute(OpenMMContextImpl& context) {
92
93
}

94
95
96
97
98
99
100
CudaCalcHarmonicBondForceKernel::~CudaCalcHarmonicBondForceKernel() {
}

void CudaCalcHarmonicBondForceKernel::initialize(const System& system, const HarmonicBondForce& force) {
    if (data.primaryKernel == NULL)
        data.primaryKernel = this;
    data.hasBonds = true;
101
    numBonds = force.getNumBonds();
Peter Eastman's avatar
Peter Eastman committed
102
103
    vector<int> particle1(numBonds);
    vector<int> particle2(numBonds);
104
105
106
107
    vector<float> length(numBonds);
    vector<float> k(numBonds);
    for (int i = 0; i < numBonds; i++) {
        double lengthValue, kValue;
Peter Eastman's avatar
Peter Eastman committed
108
        force.getBondParameters(i, particle1[i], particle2[i], lengthValue, kValue);
109
110
111
        length[i] = (float) lengthValue;
        k[i] = (float) kValue;
    }
Peter Eastman's avatar
Peter Eastman committed
112
    gpuSetBondParameters(data.gpu, particle1, particle2, length, k);
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
}

void CudaCalcHarmonicBondForceKernel::executeForces(OpenMMContextImpl& context) {
    if (data.primaryKernel == this)
        calcForces(context, data);
}

double CudaCalcHarmonicBondForceKernel::executeEnergy(OpenMMContextImpl& context) {
    if (data.primaryKernel == this)
        return calcEnergy(context, system);
    return 0.0;
}

CudaCalcHarmonicAngleForceKernel::~CudaCalcHarmonicAngleForceKernel() {
}

void CudaCalcHarmonicAngleForceKernel::initialize(const System& system, const HarmonicAngleForce& force) {
    if (data.primaryKernel == NULL)
        data.primaryKernel = this;
    data.hasAngles = true;
133
    numAngles = force.getNumAngles();
134
    const float RadiansToDegrees = (float) (180.0/3.14159265);
Peter Eastman's avatar
Peter Eastman committed
135
136
137
    vector<int> particle1(numAngles);
    vector<int> particle2(numAngles);
    vector<int> particle3(numAngles);
138
139
140
141
    vector<float> angle(numAngles);
    vector<float> k(numAngles);
    for (int i = 0; i < numAngles; i++) {
        double angleValue, kValue;
Peter Eastman's avatar
Peter Eastman committed
142
        force.getAngleParameters(i, particle1[i], particle2[i], particle3[i], angleValue, kValue);
143
144
        angle[i] = (float) (angleValue*RadiansToDegrees);
        k[i] = (float) kValue;
145
    }
Peter Eastman's avatar
Peter Eastman committed
146
    gpuSetBondAngleParameters(data.gpu, particle1, particle2, particle3, angle, k);
147
}
148

149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
void CudaCalcHarmonicAngleForceKernel::executeForces(OpenMMContextImpl& context) {
    if (data.primaryKernel == this)
        calcForces(context, data);
}

double CudaCalcHarmonicAngleForceKernel::executeEnergy(OpenMMContextImpl& context) {
    if (data.primaryKernel == this)
        return calcEnergy(context, system);
    return 0.0;
}

CudaCalcPeriodicTorsionForceKernel::~CudaCalcPeriodicTorsionForceKernel() {
}

void CudaCalcPeriodicTorsionForceKernel::initialize(const System& system, const PeriodicTorsionForce& force) {
    if (data.primaryKernel == NULL)
        data.primaryKernel = this;
    data.hasPeriodicTorsions = true;
    numTorsions = force.getNumTorsions();
    const float RadiansToDegrees = 180.0/3.14159265;
Peter Eastman's avatar
Peter Eastman committed
169
170
171
172
    vector<int> particle1(numTorsions);
    vector<int> particle2(numTorsions);
    vector<int> particle3(numTorsions);
    vector<int> particle4(numTorsions);
173
174
175
176
177
    vector<float> k(numTorsions);
    vector<float> phase(numTorsions);
    vector<int> periodicity(numTorsions);
    for (int i = 0; i < numTorsions; i++) {
        double kValue, phaseValue;
Peter Eastman's avatar
Peter Eastman committed
178
        force.getTorsionParameters(i, particle1[i], particle2[i], particle3[i], particle4[i], periodicity[i], phaseValue, kValue);
179
180
        k[i] = (float) kValue;
        phase[i] = (float) (phaseValue*RadiansToDegrees);
181
    }
Peter Eastman's avatar
Peter Eastman committed
182
    gpuSetDihedralParameters(data.gpu, particle1, particle2, particle3, particle4, k, phase, periodicity);
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
}

void CudaCalcPeriodicTorsionForceKernel::executeForces(OpenMMContextImpl& context) {
    if (data.primaryKernel == this)
        calcForces(context, data);
}

double CudaCalcPeriodicTorsionForceKernel::executeEnergy(OpenMMContextImpl& context) {
    if (data.primaryKernel == this)
        return calcEnergy(context, system);
    return 0.0;
}

CudaCalcRBTorsionForceKernel::~CudaCalcRBTorsionForceKernel() {
}

void CudaCalcRBTorsionForceKernel::initialize(const System& system, const RBTorsionForce& force) {
    if (data.primaryKernel == NULL)
        data.primaryKernel = this;
    data.hasRB = true;
    numTorsions = force.getNumTorsions();
Peter Eastman's avatar
Peter Eastman committed
204
205
206
207
    vector<int> particle1(numTorsions);
    vector<int> particle2(numTorsions);
    vector<int> particle3(numTorsions);
    vector<int> particle4(numTorsions);
208
209
210
211
212
213
214
215
    vector<float> c0(numTorsions);
    vector<float> c1(numTorsions);
    vector<float> c2(numTorsions);
    vector<float> c3(numTorsions);
    vector<float> c4(numTorsions);
    vector<float> c5(numTorsions);
    for (int i = 0; i < numTorsions; i++) {
        double c[6];
Peter Eastman's avatar
Peter Eastman committed
216
        force.getTorsionParameters(i, particle1[i], particle2[i], particle3[i], particle4[i], c[0], c[1], c[2], c[3], c[4], c[5]);
217
218
219
220
221
222
        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];
223
    }
Peter Eastman's avatar
Peter Eastman committed
224
    gpuSetRbDihedralParameters(data.gpu, particle1, particle2, particle3, particle4, c0, c1, c2, c3, c4, c5);
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
}

void CudaCalcRBTorsionForceKernel::executeForces(OpenMMContextImpl& context) {
    if (data.primaryKernel == this)
        calcForces(context, data);
}

double CudaCalcRBTorsionForceKernel::executeEnergy(OpenMMContextImpl& context) {
    if (data.primaryKernel == this)
        return calcEnergy(context, system);
    return 0.0;
}

CudaCalcNonbondedForceKernel::~CudaCalcNonbondedForceKernel() {
}

241
void CudaCalcNonbondedForceKernel::initialize(const System& system, const NonbondedForce& force) {
242
243
244
    if (data.primaryKernel == NULL)
        data.primaryKernel = this;
    data.hasNonbonded = true;
Peter Eastman's avatar
Peter Eastman committed
245
    numParticles = force.getNumParticles();
246
    _gpuContext* gpu = data.gpu;
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261

    // Identify which exceptions are 1-4 interactions.

    vector<pair<int, int> > exclusions;
    vector<int> nb14s;
    for (int i = 0; i < force.getNumExceptions(); i++) {
        int particle1, particle2;
        double chargeProd, sigma, epsilon;
        force.getExceptionParameters(i, particle1, particle2, chargeProd, sigma, epsilon);
        exclusions.push_back(pair<int, int>(particle1, particle2));
        if (chargeProd != 0.0 || epsilon != 0.0)
            nb14s.push_back(i);
    }
    num14 = nb14s.size();

262
263
    // Initialize nonbonded interactions.
    
264
    {
Peter Eastman's avatar
Peter Eastman committed
265
266
267
268
        vector<int> particle(numParticles);
        vector<float> c6(numParticles);
        vector<float> c12(numParticles);
        vector<float> q(numParticles);
269
        vector<char> symbol;
Peter Eastman's avatar
Peter Eastman committed
270
271
        vector<vector<int> > exclusionList(numParticles);
        for (int i = 0; i < numParticles; i++) {
272
            double charge, radius, depth;
Peter Eastman's avatar
Peter Eastman committed
273
274
            force.getParticleParameters(i, charge, radius, depth);
            particle[i] = i;
275
276
277
            q[i] = (float) charge;
            c6[i] = (float) (4*depth*pow(radius, 6.0));
            c12[i] = (float) (4*depth*pow(radius, 12.0));
278
279
            exclusionList[i].push_back(i);
        }
280
281
282
283
        for (int i = 0; i < exclusions.size(); i++) {
            exclusionList[exclusions[i].first].push_back(exclusions[i].second);
            exclusionList[exclusions[i].second].push_back(exclusions[i].first);
        }
284
285
286
287
288
289
290
291
292
293
294
        CudaNonbondedMethod method = NO_CUTOFF;
        if (force.getNonbondedMethod() != NonbondedForce::NoCutoff) {
            gpuSetNonbondedCutoff(gpu, force.getCutoffDistance(), 78.3f);
            method = CUTOFF;
        }
        if (force.getNonbondedMethod() == NonbondedForce::CutoffPeriodic) {
            Vec3 boxVectors[3];
            force.getPeriodicBoxVectors(boxVectors[0], boxVectors[1], boxVectors[2]);
            gpuSetPeriodicBoxSize(gpu, boxVectors[0][0], boxVectors[1][1], boxVectors[2][2]);
            method = PERIODIC;
        }
295
296
297
298
299
300
301
302

        if (force.getNonbondedMethod() == NonbondedForce::Ewald) {
            Vec3 boxVectors[3];
            force.getPeriodicBoxVectors(boxVectors[0], boxVectors[1], boxVectors[2]);
            gpuSetPeriodicBoxSize(gpu, boxVectors[0][0], boxVectors[1][1], boxVectors[2][2]);
            method = EWALD;

        }
303
304
        data.nonbondedMethod = method;
        gpuSetCoulombParameters(gpu, 138.935485f, particle, c6, c12, q, symbol, exclusionList, method);
305
306
307
308
    }

    // Initialize 1-4 nonbonded interactions.
    
309
    {
Peter Eastman's avatar
Peter Eastman committed
310
311
        vector<int> particle1(num14);
        vector<int> particle2(num14);
312
313
314
315
316
        vector<float> c6(num14);
        vector<float> c12(num14);
        vector<float> q1(num14);
        vector<float> q2(num14);
        for (int i = 0; i < num14; i++) {
317
            double charge, sig, eps;
318
            force.getExceptionParameters(nb14s[i], particle1[i], particle2[i], charge, sig, eps);
319
320
            c6[i] = (float) (4*eps*pow(sig, 6.0));
            c12[i] = (float) (4*eps*pow(sig, 12.0));
Peter Eastman's avatar
Peter Eastman committed
321
322
            q1[i] = (float) charge;
            q2[i] = 1.0f;
323
        }
Peter Eastman's avatar
Peter Eastman committed
324
        gpuSetLJ14Parameters(gpu, 138.935485f, 1.0f, particle1, particle2, c6, c12, q1, q2);
325
326
327
    }
}

328
329
330
void CudaCalcNonbondedForceKernel::executeForces(OpenMMContextImpl& context) {
    if (data.primaryKernel == this)
        calcForces(context, data);
331
332
}

333
334
335
336
double CudaCalcNonbondedForceKernel::executeEnergy(OpenMMContextImpl& context) {
    if (data.primaryKernel == this)
        return calcEnergy(context, system);
    return 0.0;
337
338
}

339
CudaCalcGBSAOBCForceKernel::~CudaCalcGBSAOBCForceKernel() {
340
341
}

342
void CudaCalcGBSAOBCForceKernel::initialize(const System& system, const GBSAOBCForce& force) {
343

Peter Eastman's avatar
Peter Eastman committed
344
    int numParticles = system.getNumParticles();
345
    _gpuContext* gpu = data.gpu;
Peter Eastman's avatar
Peter Eastman committed
346
347
348
349
350
351
352
353
    vector<int> particle(numParticles);
    vector<float> radius(numParticles);
    vector<float> scale(numParticles);
    for (int i = 0; i < numParticles; i++) {
        double charge, particleRadius, scalingFactor;
        force.getParticleParameters(i, charge, particleRadius, scalingFactor);
        particle[i] = i;
        radius[i] = (float) particleRadius;
354
        scale[i] = (float) scalingFactor;
355
    }
356
    gpuSetObcParameters(gpu, (float) force.getSoluteDielectric(), (float) force.getSolventDielectric(), particle, radius, scale);
357
358
}

359
void CudaCalcGBSAOBCForceKernel::executeForces(OpenMMContextImpl& context) {
360
361
}

362
static void initializeIntegration(const System& system, CudaPlatform::PlatformData& data, const Integrator& integrator) {
363
364
365
    
    // Set masses.
    
366
    _gpuContext* gpu = data.gpu;
Peter Eastman's avatar
Peter Eastman committed
367
368
369
370
    int numParticles = system.getNumParticles();
    vector<float> mass(numParticles);
    for (int i = 0; i < numParticles; i++)
        mass[i] = (float) system.getParticleMass(i);
371
372
373
374
    gpuSetMass(gpu, mass);
    
    // Set constraints.
    
375
    int numConstraints = system.getNumConstraints();
Peter Eastman's avatar
Peter Eastman committed
376
377
    vector<int> particle1(numConstraints);
    vector<int> particle2(numConstraints);
378
379
380
381
    vector<float> distance(numConstraints);
    vector<float> invMass1(numConstraints);
    vector<float> invMass2(numConstraints);
    for (int i = 0; i < numConstraints; i++) {
Peter Eastman's avatar
Peter Eastman committed
382
        int particle1Index, particle2Index;
383
        double constraintDistance;
Peter Eastman's avatar
Peter Eastman committed
384
385
386
        system.getConstraintParameters(i, particle1Index, particle2Index, constraintDistance);
        particle1[i] = particle1Index;
        particle2[i] = particle2Index;
387
        distance[i] = (float) constraintDistance;
Peter Eastman's avatar
Peter Eastman committed
388
389
        invMass1[i] = 1.0f/mass[particle1Index];
        invMass2[i] = 1.0f/mass[particle2Index];
390
    }
391
    gpuSetConstraintParameters(gpu, particle1, particle2, distance, invMass1, invMass2, integrator.getConstraintTolerance(), 4);
392
393
394
395
396
397
398
399
400
401
402
403
404
    
    // Initialize any terms that haven't already been handled by a Force.
    
    if (!data.hasBonds)
        gpuSetBondParameters(gpu, vector<int>(), vector<int>(), vector<float>(), vector<float>());
    if (!data.hasAngles)
        gpuSetBondAngleParameters(gpu, vector<int>(), vector<int>(), vector<int>(), vector<float>(), vector<float>());
    if (!data.hasPeriodicTorsions)
        gpuSetDihedralParameters(gpu, vector<int>(), vector<int>(), vector<int>(), vector<int>(), vector<float>(), vector<float>(), vector<int>());
    if (!data.hasRB)
        gpuSetRbDihedralParameters(gpu, vector<int>(), vector<int>(), vector<int>(), vector<int>(), vector<float>(), vector<float>(),
                vector<float>(), vector<float>(), vector<float>(), vector<float>());
    if (!data.hasNonbonded) {
405
        gpuSetCoulombParameters(gpu, 138.935485f, vector<int>(), vector<float>(), vector<float>(), vector<float>(), vector<char>(), vector<vector<int> >(), NO_CUTOFF);
406
407
408
409
        gpuSetLJ14Parameters(gpu, 138.935485f, 1.0f, vector<int>(), vector<int>(), vector<float>(), vector<float>(), vector<float>(), vector<float>());
    }
    
    // Finish initialization.
410

411
412
413
    gpuBuildThreadBlockWorkList(gpu);
    gpuBuildExclusionList(gpu);
    gpuBuildOutputBuffers(gpu);
414
    gpuSetConstants(gpu);
415
416
417
    kClearBornForces(gpu);
    kClearForces(gpu);
    cudaThreadSynchronize();
418
419
}

420
double CudaCalcGBSAOBCForceKernel::executeEnergy(OpenMMContextImpl& context) {
421
	return 0.0;
422
423
424
425
426
427
}

CudaIntegrateVerletStepKernel::~CudaIntegrateVerletStepKernel() {
}

void CudaIntegrateVerletStepKernel::initialize(const System& system, const VerletIntegrator& integrator) {
428
    initializeIntegration(system, data, integrator);
429
430
431
432
433
434
435
436
437
    prevStepSize = -1.0;
}

void CudaIntegrateVerletStepKernel::execute(OpenMMContextImpl& context, const VerletIntegrator& integrator) {
    _gpuContext* gpu = data.gpu;
    double stepSize = integrator.getStepSize();
    if (stepSize != prevStepSize) {
        // Initialize the GPU parameters.
        
438
        gpuSetVerletIntegrationParameters(gpu, (float) stepSize);
439
440
441
442
443
444
        gpuSetConstants(gpu);
        kGenerateRandoms(gpu);
        prevStepSize = stepSize;
    }
    kVerletUpdatePart1(gpu);
    kApplyFirstShake(gpu);
445
    kApplyFirstSettle(gpu);
446
    kApplyFirstLincs(gpu);
447
    if (data.removeCM) {
448
        int step = (int) (context.getTime()/stepSize);
449
450
451
452
453
454
455
456
457
458
        if (step%data.cmMotionFrequency == 0)
            gpu->bCalculateCM = true;
    }
    kVerletUpdatePart2(gpu);
}

CudaIntegrateLangevinStepKernel::~CudaIntegrateLangevinStepKernel() {
}

void CudaIntegrateLangevinStepKernel::initialize(const System& system, const LangevinIntegrator& integrator) {
459
    initializeIntegration(system, data, integrator);
460
461
462
    _gpuContext* gpu = data.gpu;
    gpu->seed = (unsigned long) integrator.getRandomNumberSeed();
    gpuInitializeRandoms(gpu);
463
464
465
    prevStepSize = -1.0;
}

466
void CudaIntegrateLangevinStepKernel::execute(OpenMMContextImpl& context, const LangevinIntegrator& integrator) {
467
    _gpuContext* gpu = data.gpu;
468
469
470
    double temperature = integrator.getTemperature();
    double friction = integrator.getFriction();
    double stepSize = integrator.getStepSize();
471
472
473
474
    if (temperature != prevTemp || friction != prevFriction || stepSize != prevStepSize) {
        // Initialize the GPU parameters.
        
        double tau = (friction == 0.0 ? 0.0 : 1.0/friction);
475
        gpuSetIntegrationParameters(gpu, (float) tau, (float) stepSize, (float) temperature);
476
477
478
479
480
481
482
483
        gpuSetConstants(gpu);
        kGenerateRandoms(gpu);
        prevTemp = temperature;
        prevFriction = friction;
        prevStepSize = stepSize;
    }
    kUpdatePart1(gpu);
    kApplyFirstShake(gpu);
484
    kApplyFirstSettle(gpu);
485
    kApplyFirstLincs(gpu);
486
    if (data.removeCM) {
487
        int step = (int) (context.getTime()/stepSize);
488
489
490
        if (step%data.cmMotionFrequency == 0)
            gpu->bCalculateCM = true;
    }
491
492
    kUpdatePart2(gpu);
    kApplySecondShake(gpu);
493
    kApplySecondSettle(gpu);
494
    kApplySecondLincs(gpu);
495
}
496
497
498
499
500

CudaIntegrateBrownianStepKernel::~CudaIntegrateBrownianStepKernel() {
}

void CudaIntegrateBrownianStepKernel::initialize(const System& system, const BrownianIntegrator& integrator) {
501
    initializeIntegration(system, data, integrator);
502
503
504
    _gpuContext* gpu = data.gpu;
    gpu->seed = (unsigned long) integrator.getRandomNumberSeed();
    gpuInitializeRandoms(gpu);
505
506
507
508
509
510
511
512
513
514
515
516
    prevStepSize = -1.0;
}

void CudaIntegrateBrownianStepKernel::execute(OpenMMContextImpl& context, const BrownianIntegrator& integrator) {
    _gpuContext* gpu = data.gpu;
    double temperature = integrator.getTemperature();
    double friction = integrator.getFriction();
    double stepSize = integrator.getStepSize();
    if (temperature != prevTemp || friction != prevFriction || stepSize != prevStepSize) {
        // Initialize the GPU parameters.
        
        double tau = (friction == 0.0 ? 0.0 : 1.0/friction);
517
        gpuSetBrownianIntegrationParameters(gpu, (float) tau, (float) stepSize, (float) temperature);
518
519
520
521
522
523
524
525
        gpuSetConstants(gpu);
        kGenerateRandoms(gpu);
        prevTemp = temperature;
        prevFriction = friction;
        prevStepSize = stepSize;
    }
    kBrownianUpdatePart1(gpu);
    kApplyFirstShake(gpu);
526
    kApplyFirstSettle(gpu);
527
    kApplyFirstLincs(gpu);
528
    if (data.removeCM) {
529
        int step = (int) (context.getTime()/stepSize);
530
531
532
533
534
        if (step%data.cmMotionFrequency == 0)
            gpu->bCalculateCM = true;
    }
    kBrownianUpdatePart2(gpu);
}
535
536
537
538
539

CudaApplyAndersenThermostatKernel::~CudaApplyAndersenThermostatKernel() {
}

void CudaApplyAndersenThermostatKernel::initialize(const System& system, const AndersenThermostat& thermostat) {
540
541
542
    _gpuContext* gpu = data.gpu;
    gpu->seed = (unsigned long) thermostat.getRandomNumberSeed();
    gpuInitializeRandoms(gpu);
543
544
545
546
547
    prevStepSize = -1.0;
}

void CudaApplyAndersenThermostatKernel::execute(OpenMMContextImpl& context) {
    _gpuContext* gpu = data.gpu;
548
549
    double temperature = context.getParameter(AndersenThermostat::Temperature());
    double frequency = context.getParameter(AndersenThermostat::CollisionFrequency());
550
551
552
553
    double stepSize = context.getIntegrator().getStepSize();
    if (temperature != prevTemp || frequency != prevFrequency || stepSize != prevStepSize) {
        // Initialize the GPU parameters.
        
554
        gpuSetAndersenThermostatParameters(gpu, (float) temperature, (float) (frequency*stepSize));
555
556
557
558
559
560
561
562
        gpuSetConstants(gpu);
        kGenerateRandoms(gpu);
        prevTemp = temperature;
        prevFrequency = frequency;
        prevStepSize = stepSize;
    }
    kCalculateAndersenThermostat(gpu);
}
563

564
void CudaCalcKineticEnergyKernel::initialize(const System& system) {
Peter Eastman's avatar
Peter Eastman committed
565
566
    int numParticles = system.getNumParticles();
    masses.resize(numParticles);
567
    for (int i = 0; i < numParticles; ++i)
Peter Eastman's avatar
Peter Eastman committed
568
        masses[i] = system.getParticleMass(i);
569
570
}

571
double CudaCalcKineticEnergyKernel::execute(OpenMMContextImpl& context) {
572
573
574
    // We don't currently have a GPU kernel to do this, so we retrieve the velocities and calculate the energy
    // on the CPU.
    
575
    const Stream& velocities = context.getVelocities();
576
577
578
579
580
581
582
583
    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;
}
584

585
void CudaRemoveCMMotionKernel::initialize(const System& system, const CMMotionRemover& force) {
586
    data.removeCM = true;
587
    data.cmMotionFrequency = force.getFrequency();
588
589
}

590
void CudaRemoveCMMotionKernel::execute(OpenMMContextImpl& context) {
591
}