CudaKernels.h 35.4 KB
Newer Older
1
2
3
4
5
6
7
8
9
10
11
#ifndef OPENMM_CUDAKERNELS_H_
#define OPENMM_CUDAKERNELS_H_

/* -------------------------------------------------------------------------- *
 *                                   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.               *
 *                                                                            *
12
 * Portions copyright (c) 2008-2009 Stanford University and the Authors.      *
13
14
15
 * Authors: Peter Eastman                                                     *
 * Contributors:                                                              *
 *                                                                            *
16
17
18
19
 * 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.                                        *
20
 *                                                                            *
21
22
23
24
 * 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.                        *
25
 *                                                                            *
26
27
 * 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/>.      *
28
29
 * -------------------------------------------------------------------------- */

30
#include "CudaPlatform.h"
31
#include "openmm/kernels.h"
32
#include "kernels/gputypes.h"
33
#include "openmm/System.h"
34
35
36
37
38
39
40
41
42

class CudaAndersenThermostat;
class CudaBrownianDynamics;
class CudaStochasticDynamics;
class CudaShakeAlgorithm;
class CudaVerletDynamics;

namespace OpenMM {

43
44
45
// Export internal cudaOpenMMInitializeIntegration() method so it can be used by NML plugin
void OPENMMCUDA_EXPORT cudaOpenMMInitializeIntegration(const System& system, CudaPlatform::PlatformData& data, const Integrator& integrator);

46
/**
47
48
49
 * This kernel is invoked at the beginning and end of force and energy computations.  It gives the
 * Platform a chance to clear buffers and do other initialization at the beginning, and to do any
 * necessary work at the end to determine the final results.
50
 */
51
class CudaCalcForcesAndEnergyKernel : public CalcForcesAndEnergyKernel {
52
public:
53
    CudaCalcForcesAndEnergyKernel(std::string name, const Platform& platform, CudaPlatform::PlatformData& data) : CalcForcesAndEnergyKernel(name, platform), data(data) {
54
55
56
57
58
59
60
61
    }
    /**
     * Initialize the kernel.
     * 
     * @param system     the System this kernel will be applied to
     */
    void initialize(const System& system);
    /**
62
     * This is called at the beginning of each force/energy computation, before calcForcesAndEnergy() has been called on
63
64
     * any ForceImpl.
     *
65
66
67
     * @param context       the context in which to execute this kernel
     * @param includeForce  true if forces should be computed
     * @param includeEnergy true if potential energy should be computed
68
     */
69
    void beginComputation(ContextImpl& context, bool includeForce, bool includeEnergy);
70
    /**
71
     * This is called at the end of each force/energy computation, after calcForcesAndEnergy() has been called on
72
73
     * every ForceImpl.
     *
74
75
76
     * @param context       the context in which to execute this kernel
     * @param includeForce  true if forces should be computed
     * @param includeEnergy true if potential energy should be computed
77
     * @return the potential energy of the system.  This value is added to all values returned by ForceImpls'
78
     * calcForcesAndEnergy() methods.  That is, each force kernel may <i>either</i> return its contribution to the
79
     * energy directly, <i>or</i> add it to an internal buffer so that it will be included here.
80
     */
81
    double finishComputation(ContextImpl& context, bool includeForce, bool includeEnergy);
82
83
private:
    CudaPlatform::PlatformData& data;
84
};
85

86
/**
87
88
 * This kernel provides methods for setting and retrieving various state data: time, positions,
 * velocities, and forces.
89
 */
90
class CudaUpdateStateDataKernel : public UpdateStateDataKernel {
91
public:
92
    CudaUpdateStateDataKernel(std::string name, const Platform& platform, CudaPlatform::PlatformData& data) : UpdateStateDataKernel(name, platform), data(data) {
93
94
95
96
97
98
99
100
101
102
103
104
    }
    /**
     * Initialize the kernel.
     *
     * @param system     the System this kernel will be applied to
     */
    void initialize(const System& system);
    /**
     * Get the current time (in picoseconds).
     *
     * @param context    the context in which to execute this kernel
     */
105
    double getTime(const ContextImpl& context) const;
106
107
108
109
110
    /**
     * Set the current time (in picoseconds).
     *
     * @param context    the context in which to execute this kernel
     */
111
    void setTime(ContextImpl& context, double time);
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
    /**
     * Get the positions of all particles.
     *
     * @param positions  on exit, this contains the particle positions
     */
    void getPositions(ContextImpl& context, std::vector<Vec3>& positions);
    /**
     * Set the positions of all particles.
     *
     * @param positions  a vector containg the particle positions
     */
    void setPositions(ContextImpl& context, const std::vector<Vec3>& positions);
    /**
     * Get the velocities of all particles.
     *
     * @param velocities  on exit, this contains the particle velocities
     */
    void getVelocities(ContextImpl& context, std::vector<Vec3>& velocities);
    /**
     * Set the velocities of all particles.
     *
     * @param velocities  a vector containg the particle velocities
     */
    void setVelocities(ContextImpl& context, const std::vector<Vec3>& velocities);
    /**
     * Get the current forces on all particles.
     *
     * @param forces  on exit, this contains the forces
     */
    void getForces(ContextImpl& context, std::vector<Vec3>& forces);
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
    /**
     * Get the current periodic box vectors.
     *
     * @param a      on exit, this contains the vector defining the first edge of the periodic box
     * @param b      on exit, this contains the vector defining the second edge of the periodic box
     * @param c      on exit, this contains the vector defining the third edge of the periodic box
     */
    void getPeriodicBoxVectors(ContextImpl& context, Vec3& a, Vec3& b, Vec3& c) const;
    /**
     * Set the current periodic box vectors.
     *
     * @param a      the vector defining the first edge of the periodic box
     * @param b      the vector defining the second edge of the periodic box
     * @param c      the vector defining the third edge of the periodic box
     */
    void setPeriodicBoxVectors(ContextImpl& context, const Vec3& a, const Vec3& b, const Vec3& c) const;
158
159
160
161
private:
    CudaPlatform::PlatformData& data;
};

162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
/**
 * This kernel modifies the positions of particles to enforce distance constraints.
 */
class CudaApplyConstraintsKernel : public ApplyConstraintsKernel {
public:
    CudaApplyConstraintsKernel(std::string name, const Platform& platform, CudaPlatform::PlatformData& data) : ApplyConstraintsKernel(name, platform), data(data) {
    }
    /**
     * Initialize the kernel.
     *
     * @param system     the System this kernel will be applied to
     */
    void initialize(const System& system);
    /**
     * Update particle positions to enforce constraints.
     *
     * @param context    the context in which to execute this kernel
     * @param tol        the distance tolerance within which constraints must be satisfied.
     */
    void apply(ContextImpl& context, double tol);
private:
    CudaPlatform::PlatformData& data;
};

186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
/**
 * This kernel recomputes the positions of virtual sites.
 */
class CudaVirtualSitesKernel : public VirtualSitesKernel {
public:
    CudaVirtualSitesKernel(std::string name, const Platform& platform) : VirtualSitesKernel(name, platform) {
    }
    /**
     * Initialize the kernel.
     *
     * @param system     the System this kernel will be applied to
     */
    void initialize(const System& system);
    /**
     * Compute the virtual site locations.
     *
     * @param context    the context in which to execute this kernel
     */
    void computePositions(ContextImpl& context);
};

207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
/**
 * This kernel is invoked by HarmonicBondForce to calculate the forces acting on the system and the energy of the system.
 */
class CudaCalcHarmonicBondForceKernel : public CalcHarmonicBondForceKernel {
public:
    CudaCalcHarmonicBondForceKernel(std::string name, const Platform& platform, CudaPlatform::PlatformData& data, System& system) : CalcHarmonicBondForceKernel(name, platform), data(data), system(system) {
    }
    ~CudaCalcHarmonicBondForceKernel();
    /**
     * Initialize the kernel.
     * 
     * @param system     the System this kernel will be applied to
     * @param force      the HarmonicBondForce this kernel will be used for
     */
    void initialize(const System& system, const HarmonicBondForce& force);
    /**
223
224
225
226
227
228
     * Execute the kernel to calculate the forces and/or energy.
     *
     * @param context        the context in which to execute this kernel
     * @param includeForces  true if forces should be calculated
     * @param includeEnergy  true if the energy should be calculated
     * @return the potential energy due to the force
229
     */
230
    double execute(ContextImpl& context, bool includeForces, bool includeEnergy);
231
private:
232
    class ForceInfo;
233
234
235
236
237
    int numBonds;
    CudaPlatform::PlatformData& data;
    System& system;
};

238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
/**
 * This kernel is invoked by CustomBondForce to calculate the forces acting on the system and the energy of the system.
 */
class CudaCalcCustomBondForceKernel : public CalcCustomBondForceKernel {
public:
    CudaCalcCustomBondForceKernel(std::string name, const Platform& platform, CudaPlatform::PlatformData& data, System& system) : CalcCustomBondForceKernel(name, platform),
            data(data), system(system) {
    }
    ~CudaCalcCustomBondForceKernel();
    /**
     * Initialize the kernel.
     *
     * @param system     the System this kernel will be applied to
     * @param force      the CustomBondForce this kernel will be used for
     */
    void initialize(const System& system, const CustomBondForce& force);
    /**
255
     * Execute the kernel to calculate the forces and/or energy.
256
     *
257
258
259
260
     * @param context        the context in which to execute this kernel
     * @param includeForces  true if forces should be calculated
     * @param includeEnergy  true if the energy should be calculated
     * @return the potential energy due to the force
261
     */
262
    double execute(ContextImpl& context, bool includeForces, bool includeEnergy);
263
private:
264
    class ForceInfo;
265
266
267
268
269
270
271
272
    void updateGlobalParams(ContextImpl& context);
    int numBonds;
    CudaPlatform::PlatformData& data;
    std::vector<std::string> globalParamNames;
    std::vector<float> globalParamValues;
    System& system;
};

273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
/**
 * This kernel is invoked by HarmonicAngleForce to calculate the forces acting on the system and the energy of the system.
 */
class CudaCalcHarmonicAngleForceKernel : public CalcHarmonicAngleForceKernel {
public:
    CudaCalcHarmonicAngleForceKernel(std::string name, const Platform& platform, CudaPlatform::PlatformData& data, System& system) : CalcHarmonicAngleForceKernel(name, platform), data(data), system(system) {
    }
    ~CudaCalcHarmonicAngleForceKernel();
    /**
     * Initialize the kernel.
     * 
     * @param system     the System this kernel will be applied to
     * @param force      the HarmonicAngleForce this kernel will be used for
     */
    void initialize(const System& system, const HarmonicAngleForce& force);
    /**
289
290
291
292
293
294
     * Execute the kernel to calculate the forces and/or energy.
     *
     * @param context        the context in which to execute this kernel
     * @param includeForces  true if forces should be calculated
     * @param includeEnergy  true if the energy should be calculated
     * @return the potential energy due to the force
295
     */
296
    double execute(ContextImpl& context, bool includeForces, bool includeEnergy);
297
private:
298
    class ForceInfo;
299
300
301
302
303
    int numAngles;
    CudaPlatform::PlatformData& data;
    System& system;
};

304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
/**
 * This kernel is invoked by CustomAngleForce to calculate the forces acting on the system and the energy of the system.
 */
class CudaCalcCustomAngleForceKernel : public CalcCustomAngleForceKernel {
public:
    CudaCalcCustomAngleForceKernel(std::string name, const Platform& platform, CudaPlatform::PlatformData& data, System& system) : CalcCustomAngleForceKernel(name, platform),
            data(data), system(system) {
    }
    ~CudaCalcCustomAngleForceKernel();
    /**
     * Initialize the kernel.
     *
     * @param system     the System this kernel will be applied to
     * @param force      the CustomAngleForce this kernel will be used for
     */
    void initialize(const System& system, const CustomAngleForce& force);
    /**
321
     * Execute the kernel to calculate the forces and/or energy.
322
     *
323
324
325
326
     * @param context        the context in which to execute this kernel
     * @param includeForces  true if forces should be calculated
     * @param includeEnergy  true if the energy should be calculated
     * @return the potential energy due to the force
327
     */
328
    double execute(ContextImpl& context, bool includeForces, bool includeEnergy);
329
private:
330
    class ForceInfo;
331
332
333
334
335
336
337
338
    void updateGlobalParams(ContextImpl& context);
    int numAngles;
    CudaPlatform::PlatformData& data;
    std::vector<std::string> globalParamNames;
    std::vector<float> globalParamValues;
    System& system;
};

339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
/**
 * This kernel is invoked by PeriodicTorsionForce to calculate the forces acting on the system and the energy of the system.
 */
class CudaCalcPeriodicTorsionForceKernel : public CalcPeriodicTorsionForceKernel {
public:
    CudaCalcPeriodicTorsionForceKernel(std::string name, const Platform& platform, CudaPlatform::PlatformData& data, System& system) : CalcPeriodicTorsionForceKernel(name, platform), data(data), system(system) {
    }
    ~CudaCalcPeriodicTorsionForceKernel();
    /**
     * Initialize the kernel.
     * 
     * @param system     the System this kernel will be applied to
     * @param force      the PeriodicTorsionForce this kernel will be used for
     */
    void initialize(const System& system, const PeriodicTorsionForce& force);
    /**
355
356
357
358
359
360
     * Execute the kernel to calculate the forces and/or energy.
     *
     * @param context        the context in which to execute this kernel
     * @param includeForces  true if forces should be calculated
     * @param includeEnergy  true if the energy should be calculated
     * @return the potential energy due to the force
361
     */
362
    double execute(ContextImpl& context, bool includeForces, bool includeEnergy);
363
private:
364
    class ForceInfo;
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
    int numTorsions;
    CudaPlatform::PlatformData& data;
    System& system;
};

/**
 * This kernel is invoked by RBTorsionForce to calculate the forces acting on the system and the energy of the system.
 */
class CudaCalcRBTorsionForceKernel : public CalcRBTorsionForceKernel {
public:
    CudaCalcRBTorsionForceKernel(std::string name, const Platform& platform, CudaPlatform::PlatformData& data, System& system) : CalcRBTorsionForceKernel(name, platform), data(data), system(system) {
    }
    ~CudaCalcRBTorsionForceKernel();
    /**
     * Initialize the kernel.
     * 
     * @param system     the System this kernel will be applied to
     * @param force      the RBTorsionForce this kernel will be used for
     */
    void initialize(const System& system, const RBTorsionForce& force);
    /**
386
387
388
389
390
391
     * Execute the kernel to calculate the forces and/or energy.
     *
     * @param context        the context in which to execute this kernel
     * @param includeForces  true if forces should be calculated
     * @param includeEnergy  true if the energy should be calculated
     * @return the potential energy due to the force
392
     */
393
    double execute(ContextImpl& context, bool includeForces, bool includeEnergy);
394
private:
395
    class ForceInfo;
396
397
398
399
400
    int numTorsions;
    CudaPlatform::PlatformData& data;
    System& system;
};

401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
/**
 * This kernel is invoked by CMAPTorsionForce to calculate the forces acting on the system and the energy of the system.
 */
class CudaCalcCMAPTorsionForceKernel : public CalcCMAPTorsionForceKernel {
public:
    CudaCalcCMAPTorsionForceKernel(std::string name, const Platform& platform, CudaPlatform::PlatformData& data, System& system) :
            CalcCMAPTorsionForceKernel(name, platform), data(data), system(system), coefficients(NULL), mapPositions(NULL),
            torsionIndices(NULL), torsionMaps(NULL) {
    }
    ~CudaCalcCMAPTorsionForceKernel();
    /**
     * Initialize the kernel.
     *
     * @param system     the System this kernel will be applied to
     * @param force      the CMAPTorsionForce this kernel will be used for
     */
    void initialize(const System& system, const CMAPTorsionForce& force);
    /**
419
     * Execute the kernel to calculate the forces and/or energy.
420
     *
421
422
423
424
     * @param context        the context in which to execute this kernel
     * @param includeForces  true if forces should be calculated
     * @param includeEnergy  true if the energy should be calculated
     * @return the potential energy due to the force
425
     */
426
    double execute(ContextImpl& context, bool includeForces, bool includeEnergy);
427
private:
428
    class ForceInfo;
429
430
431
432
433
434
435
436
437
    CudaPlatform::PlatformData& data;
    System& system;
    int numTorsions;
    CUDAStream<float4>* coefficients;
    CUDAStream<int2>* mapPositions;
    CUDAStream<int4>* torsionIndices;
    CUDAStream<int>* torsionMaps;
};

438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
/**
 * This kernel is invoked by CustomTorsionForce to calculate the forces acting on the system and the energy of the system.
 */
class CudaCalcCustomTorsionForceKernel : public CalcCustomTorsionForceKernel {
public:
    CudaCalcCustomTorsionForceKernel(std::string name, const Platform& platform, CudaPlatform::PlatformData& data, System& system) : CalcCustomTorsionForceKernel(name, platform),
            data(data), system(system) {
    }
    ~CudaCalcCustomTorsionForceKernel();
    /**
     * Initialize the kernel.
     *
     * @param system     the System this kernel will be applied to
     * @param force      the CustomTorsionForce this kernel will be used for
     */
    void initialize(const System& system, const CustomTorsionForce& force);
    /**
455
     * Execute the kernel to calculate the forces and/or energy.
456
     *
457
458
459
460
     * @param context        the context in which to execute this kernel
     * @param includeForces  true if forces should be calculated
     * @param includeEnergy  true if the energy should be calculated
     * @return the potential energy due to the force
461
     */
462
    double execute(ContextImpl& context, bool includeForces, bool includeEnergy);
463
private:
464
    class ForceInfo;
465
466
467
468
469
470
471
472
    void updateGlobalParams(ContextImpl& context);
    int numTorsions;
    CudaPlatform::PlatformData& data;
    std::vector<std::string> globalParamNames;
    std::vector<float> globalParamValues;
    System& system;
};

473
/**
474
 * This kernel is invoked by NonbondedForce to calculate the forces acting on the system.
475
 */
476
class CudaCalcNonbondedForceKernel : public CalcNonbondedForceKernel {
477
public:
478
    CudaCalcNonbondedForceKernel(std::string name, const Platform& platform, CudaPlatform::PlatformData& data, System& system) : CalcNonbondedForceKernel(name, platform), data(data), system(system) {
479
    }
480
    ~CudaCalcNonbondedForceKernel();
481
    /**
482
     * Initialize the kernel.
483
     * 
484
     * @param system     the System this kernel will be applied to
485
     * @param force      the NonbondedForce this kernel will be used for
486
     */
487
    void initialize(const System& system, const NonbondedForce& force);
488
    /**
489
490
491
492
493
494
     * Execute the kernel to calculate the forces and/or energy.
     *
     * @param context        the context in which to execute this kernel
     * @param includeForces  true if forces should be calculated
     * @param includeEnergy  true if the energy should be calculated
     * @return the potential energy due to the force
495
     */
496
    double execute(ContextImpl& context, bool includeForces, bool includeEnergy);
497
private:
498
    class ForceInfo;
499
    CudaPlatform::PlatformData& data;
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
    int numParticles;
    System& system;
};

/**
 * This kernel is invoked by CustomNonbondedForce to calculate the forces acting on the system.
 */
class CudaCalcCustomNonbondedForceKernel : public CalcCustomNonbondedForceKernel {
public:
    CudaCalcCustomNonbondedForceKernel(std::string name, const Platform& platform, CudaPlatform::PlatformData& data, System& system) : CalcCustomNonbondedForceKernel(name, platform), data(data), system(system) {
    }
    ~CudaCalcCustomNonbondedForceKernel();
    /**
     * Initialize the kernel.
     *
     * @param system     the System this kernel will be applied to
     * @param force      the CustomNonbondedForce this kernel will be used for
     */
    void initialize(const System& system, const CustomNonbondedForce& force);
    /**
520
     * Execute the kernel to calculate the forces and/or energy.
521
     *
522
523
524
525
     * @param context        the context in which to execute this kernel
     * @param includeForces  true if forces should be calculated
     * @param includeEnergy  true if the energy should be calculated
     * @return the potential energy due to the force
526
     */
527
    double execute(ContextImpl& context, bool includeForces, bool includeEnergy);
528
private:
529
    class ForceInfo;
530
    void updateGlobalParams(ContextImpl& context);
531
532
    CudaPlatform::PlatformData& data;
    int numParticles;
533
534
    std::vector<std::string> globalParamNames;
    std::vector<float> globalParamValues;
535
    System& system;
536
537
};

538
/**
539
 * This kernel is invoked by GBSAOBCForce to calculate the forces acting on the system.
540
 */
541
class CudaCalcGBSAOBCForceKernel : public CalcGBSAOBCForceKernel {
542
public:
543
    CudaCalcGBSAOBCForceKernel(std::string name, const Platform& platform, CudaPlatform::PlatformData& data) : CalcGBSAOBCForceKernel(name, platform), data(data) {
544
    }
545
    ~CudaCalcGBSAOBCForceKernel();
546
    /**
547
     * Initialize the kernel.
548
     * 
549
     * @param system     the System this kernel will be applied to
550
     * @param force      the GBSAOBCForce this kernel will be used for
551
     */
552
    void initialize(const System& system, const GBSAOBCForce& force);
553
    /**
554
555
556
557
558
559
     * Execute the kernel to calculate the forces and/or energy.
     *
     * @param context        the context in which to execute this kernel
     * @param includeForces  true if forces should be calculated
     * @param includeEnergy  true if the energy should be calculated
     * @return the potential energy due to the force
560
     */
561
    double execute(ContextImpl& context, bool includeForces, bool includeEnergy);
Mark Friedrichs's avatar
Mark Friedrichs committed
562
private:
563
    class ForceInfo;
Mark Friedrichs's avatar
Mark Friedrichs committed
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
    CudaPlatform::PlatformData& data;
};

/**
 * This kernel is invoked by GBVIForce to calculate the forces acting on the system.
 */
class CudaCalcGBVIForceKernel : public CalcGBVIForceKernel {
public:
    CudaCalcGBVIForceKernel(std::string name, const Platform& platform, CudaPlatform::PlatformData& data) : CalcGBVIForceKernel(name, platform), data(data) {
    }
    ~CudaCalcGBVIForceKernel();
    /**
     * Initialize the kernel.
     * 
     * @param system      the System this kernel will be applied to
     * @param force       the GBVIForce this kernel will be used for
     * @param scaledRadii the scaled radii (Eq. 5 of Labute paper)
     */
    void initialize(const System& system, const GBVIForce& force, const std::vector<double> & scaledRadii);
    /**
584
585
586
587
588
589
     * Execute the kernel to calculate the forces and/or energy.
     *
     * @param context        the context in which to execute this kernel
     * @param includeForces  true if forces should be calculated
     * @param includeEnergy  true if the energy should be calculated
     * @return the potential energy due to the force
Mark Friedrichs's avatar
Mark Friedrichs committed
590
     */
591
    double execute(ContextImpl& context, bool includeForces, bool includeEnergy);
592
private:
593
    class ForceInfo;
594
595
    CudaPlatform::PlatformData& data;
};
596

597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
/**
 * This kernel is invoked by CustomExternalForce to calculate the forces acting on the system and the energy of the system.
 */
class CudaCalcCustomExternalForceKernel : public CalcCustomExternalForceKernel {
public:
    CudaCalcCustomExternalForceKernel(std::string name, const Platform& platform, CudaPlatform::PlatformData& data, System& system) : CalcCustomExternalForceKernel(name, platform),
            data(data), system(system) {
    }
    ~CudaCalcCustomExternalForceKernel();
    /**
     * Initialize the kernel.
     *
     * @param system     the System this kernel will be applied to
     * @param force      the CustomExternalForce this kernel will be used for
     */
    void initialize(const System& system, const CustomExternalForce& force);
    /**
614
     * Execute the kernel to calculate the forces and/or energy.
615
     *
616
617
618
619
     * @param context        the context in which to execute this kernel
     * @param includeForces  true if forces should be calculated
     * @param includeEnergy  true if the energy should be calculated
     * @return the potential energy due to the force
620
     */
621
    double execute(ContextImpl& context, bool includeForces, bool includeEnergy);
622
private:
623
    class ForceInfo;
624
625
626
627
628
629
630
631
    void updateGlobalParams(ContextImpl& context);
    int numParticles;
    CudaPlatform::PlatformData& data;
    std::vector<std::string> globalParamNames;
    std::vector<float> globalParamValues;
    System& system;
};

632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
/**
 * This kernel is invoked by VerletIntegrator to take one time step.
 */
class CudaIntegrateVerletStepKernel : public IntegrateVerletStepKernel {
public:
    CudaIntegrateVerletStepKernel(std::string name, const Platform& platform, CudaPlatform::PlatformData& data) : IntegrateVerletStepKernel(name, platform), data(data) {
    }
    ~CudaIntegrateVerletStepKernel();
    /**
     * Initialize the kernel.
     * 
     * @param system     the System this kernel will be applied to
     * @param integrator the VerletIntegrator this kernel will be used for
     */
    void initialize(const System& system, const VerletIntegrator& integrator);
    /**
     * Execute the kernel.
     * 
     * @param context    the context in which to execute this kernel
     * @param integrator the VerletIntegrator this kernel is being used for
     */
653
    void execute(ContextImpl& context, const VerletIntegrator& integrator);
654
655
656
657
private:
    CudaPlatform::PlatformData& data;
    double prevStepSize;
};
658
659
660
661
662
663

/**
 * This kernel is invoked by LangevinIntegrator to take one time step.
 */
class CudaIntegrateLangevinStepKernel : public IntegrateLangevinStepKernel {
public:
664
    CudaIntegrateLangevinStepKernel(std::string name, const Platform& platform, CudaPlatform::PlatformData& data) : IntegrateLangevinStepKernel(name, platform), data(data) {
665
666
667
    }
    ~CudaIntegrateLangevinStepKernel();
    /**
Peter Eastman's avatar
Peter Eastman committed
668
     * Initialize the kernel, setting up the particle masses.
669
     * 
670
671
     * @param system     the System this kernel will be applied to
     * @param integrator the LangevinIntegrator this kernel will be used for
672
     */
673
    void initialize(const System& system, const LangevinIntegrator& integrator);
674
675
676
    /**
     * Execute the kernel.
     * 
677
678
     * @param context    the context in which to execute this kernel
     * @param integrator the LangevinIntegrator this kernel is being used for
679
     */
680
    void execute(ContextImpl& context, const LangevinIntegrator& integrator);
681
private:
682
    CudaPlatform::PlatformData& data;
683
684
    double prevTemp, prevFriction, prevStepSize;
};
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706

/**
 * This kernel is invoked by BrownianIntegrator to take one time step.
 */
class CudaIntegrateBrownianStepKernel : public IntegrateBrownianStepKernel {
public:
    CudaIntegrateBrownianStepKernel(std::string name, const Platform& platform, CudaPlatform::PlatformData& data) : IntegrateBrownianStepKernel(name, platform), data(data) {
    }
    ~CudaIntegrateBrownianStepKernel();
    /**
     * Initialize the kernel.
     * 
     * @param system     the System this kernel will be applied to
     * @param integrator the BrownianIntegrator this kernel will be used for
     */
    void initialize(const System& system, const BrownianIntegrator& integrator);
    /**
     * Execute the kernel.
     * 
     * @param context    the context in which to execute this kernel
     * @param integrator the BrownianIntegrator this kernel is being used for
     */
707
    void execute(ContextImpl& context, const BrownianIntegrator& integrator);
708
709
710
711
private:
    CudaPlatform::PlatformData& data;
    double prevTemp, prevFriction, prevStepSize;
};
712

713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
/**
 * This kernel is invoked by VariableVerletIntegrator to take one time step.
 */
class CudaIntegrateVariableVerletStepKernel : public IntegrateVariableVerletStepKernel {
public:
    CudaIntegrateVariableVerletStepKernel(std::string name, const Platform& platform, CudaPlatform::PlatformData& data) : IntegrateVariableVerletStepKernel(name, platform), data(data) {
    }
    ~CudaIntegrateVariableVerletStepKernel();
    /**
     * Initialize the kernel.
     *
     * @param system     the System this kernel will be applied to
     * @param integrator the VerletIntegrator this kernel will be used for
     */
    void initialize(const System& system, const VariableVerletIntegrator& integrator);
    /**
     * Execute the kernel.
     *
     * @param context    the context in which to execute this kernel
     * @param integrator the VerletIntegrator this kernel is being used for
733
     * @param maxTime    the maximum time beyond which the simulation should not be advanced
734
     */
735
    void execute(ContextImpl& context, const VariableVerletIntegrator& integrator, double maxTime);
736
737
738
739
740
private:
    CudaPlatform::PlatformData& data;
    double prevErrorTol;
};

741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
/**
 * This kernel is invoked by VariableLangevinIntegrator to take one time step.
 */
class CudaIntegrateVariableLangevinStepKernel : public IntegrateVariableLangevinStepKernel {
public:
    CudaIntegrateVariableLangevinStepKernel(std::string name, const Platform& platform, CudaPlatform::PlatformData& data) : IntegrateVariableLangevinStepKernel(name, platform), data(data) {
    }
    ~CudaIntegrateVariableLangevinStepKernel();
    /**
     * Initialize the kernel, setting up the particle masses.
     *
     * @param system     the System this kernel will be applied to
     * @param integrator the VariableLangevinIntegrator this kernel will be used for
     */
    void initialize(const System& system, const VariableLangevinIntegrator& integrator);
    /**
     * Execute the kernel.
     *
     * @param context    the context in which to execute this kernel
     * @param integrator the VariableLangevinIntegrator this kernel is being used for
     * @param maxTime    the maximum time beyond which the simulation should not be advanced
     */
    void execute(ContextImpl& context, const VariableLangevinIntegrator& integrator, double maxTime);
private:
    CudaPlatform::PlatformData& data;
    double prevTemp, prevFriction, prevErrorTol;
};

769
770
771
772
773
/**
 * This kernel is invoked by AndersenThermostat at the start of each time step to adjust the particle velocities.
 */
class CudaApplyAndersenThermostatKernel : public ApplyAndersenThermostatKernel {
public:
774
775
    CudaApplyAndersenThermostatKernel(std::string name, const Platform& platform, CudaPlatform::PlatformData& data) : ApplyAndersenThermostatKernel(name, platform),
            data(data), atomGroups(NULL) {
776
777
778
779
780
781
782
783
784
785
786
787
788
789
    }
    ~CudaApplyAndersenThermostatKernel();
    /**
     * Initialize the kernel.
     * 
     * @param system     the System this kernel will be applied to
     * @param thermostat the AndersenThermostat this kernel will be used for
     */
    void initialize(const System& system, const AndersenThermostat& thermostat);
    /**
     * Execute the kernel.
     * 
     * @param context    the context in which to execute this kernel
     */
790
    void execute(ContextImpl& context);
791
792
793
private:
    CudaPlatform::PlatformData& data;
    double prevTemp, prevFrequency, prevStepSize;
794
    CUDAStream<int>* atomGroups;
795
};
796

797
798
799
800
801
802
/**
 * This kernel is invoked by MonteCarloBarostat to adjust the periodic box volume
 */
class CudaApplyMonteCarloBarostatKernel : public ApplyMonteCarloBarostatKernel {
public:
    CudaApplyMonteCarloBarostatKernel(std::string name, const Platform& platform, CudaPlatform::PlatformData& data) : ApplyMonteCarloBarostatKernel(name, platform), data(data),
803
            hasInitializedMolecules(false), moleculeAtoms(NULL), moleculeStartIndex(NULL) {
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
    }
    ~CudaApplyMonteCarloBarostatKernel();
    /**
     * Initialize the kernel.
     *
     * @param system     the System this kernel will be applied to
     * @param barostat   the MonteCarloBarostat this kernel will be used for
     */
    void initialize(const System& system, const MonteCarloBarostat& barostat);
    /**
     * Attempt a Monte Carlo step, scaling particle positions (or cluster centers) by a specified value.
     * This is called BEFORE the periodic box size is modified.  It should begin by translating each particle
     * or cluster into the first periodic box, so that coordinates will still be correct after the box size
     * is changed.
     *
     * @param context    the context in which to execute this kernel
     * @param scale      the scale factor by which to multiply particle positions
     */
    void scaleCoordinates(ContextImpl& context, double scale);
    /**
     * Reject the most recent Monte Carlo step, restoring the particle positions to where they were before
     * scaleCoordinates() was last called.
     *
     * @param context    the context in which to execute this kernel
     */
    void restoreCoordinates(ContextImpl& context);
private:
    CudaPlatform::PlatformData& data;
    bool hasInitializedMolecules;
    int numMolecules;
    CUDAStream<int>* moleculeAtoms;
    CUDAStream<int>* moleculeStartIndex;
};

838
839
840
841
842
/**
 * This kernel is invoked to calculate the kinetic energy of the system.
 */
class CudaCalcKineticEnergyKernel : public CalcKineticEnergyKernel {
public:
843
    CudaCalcKineticEnergyKernel(std::string name, const Platform& platform, CudaPlatform::PlatformData& data) : CalcKineticEnergyKernel(name, platform), data(data) {
844
845
    }
    /**
846
     * Initialize the kernel.
847
     * 
848
     * @param system     the System this kernel will be applied to
849
     */
850
    void initialize(const System& system);
851
852
853
    /**
     * Execute the kernel.
     * 
854
     * @param context    the context in which to execute this kernel
855
     */
856
    double execute(ContextImpl& context);
857
private:
858
    CudaPlatform::PlatformData& data;
859
860
    std::vector<double> masses;
};
861
862
863
864
865
866
867
868
869

/**
 * This kernel is invoked to remove center of mass motion from the system.
 */
class CudaRemoveCMMotionKernel : public RemoveCMMotionKernel {
public:
    CudaRemoveCMMotionKernel(std::string name, const Platform& platform, CudaPlatform::PlatformData& data) : RemoveCMMotionKernel(name, platform), data(data) {
    }
    /**
Peter Eastman's avatar
Peter Eastman committed
870
     * Initialize the kernel, setting up the particle masses.
871
     * 
872
873
     * @param system     the System this kernel will be applied to
     * @param force      the CMMotionRemover this kernel will be used for
874
     */
875
    void initialize(const System& system, const CMMotionRemover& force);
876
877
878
    /**
     * Execute the kernel.
     * 
879
     * @param context    the context in which to execute this kernel
880
     */
881
    void execute(ContextImpl& context);
882
883
884
private:
    CudaPlatform::PlatformData& data;
};
885
886
887
888

} // namespace OpenMM

#endif /*OPENMM_CUDAKERNELS_H_*/