CudaKernels.h 16.4 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
#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.               *
 *                                                                            *
 * 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.                                     *
 * -------------------------------------------------------------------------- */

35
#include "CudaPlatform.h"
36
#include "kernels.h"
37
#include "kernels/gpuTypes.h"
38
#include "System.h"
39
40
41
42
43
44
45
46
47

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

namespace OpenMM {

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

/**
 * 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);
    /**
     * Execute the kernel to calculate the forces.
     * 
     * @param context    the context in which to execute this kernel
     */
    void executeForces(OpenMMContextImpl& context);
    /**
     * Execute the kernel to calculate the energy.
     * 
     * @param context    the context in which to execute this kernel
     * @return the potential energy due to the HarmonicBondForce
     */
    double executeEnergy(OpenMMContextImpl& context);
private:
    int numBonds;
    CudaPlatform::PlatformData& data;
    System& system;
};

/**
 * 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);
    /**
     * Execute the kernel to calculate the forces.
     * 
     * @param context    the context in which to execute this kernel
     */
    void executeForces(OpenMMContextImpl& context);
    /**
     * Execute the kernel to calculate the energy.
     * 
     * @param context    the context in which to execute this kernel
     * @return the potential energy due to the HarmonicAngleForce
     */
    double executeEnergy(OpenMMContextImpl& context);
private:
    int numAngles;
    CudaPlatform::PlatformData& data;
    System& system;
};

/**
 * 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);
    /**
     * Execute the kernel to calculate the forces.
     * 
     * @param context    the context in which to execute this kernel
     */
    void executeForces(OpenMMContextImpl& context);
    /**
     * Execute the kernel to calculate the energy.
     * 
     * @param context    the context in which to execute this kernel
     * @return the potential energy due to the PeriodicTorsionForce
     */
    double executeEnergy(OpenMMContextImpl& context);
private:
    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);
    /**
     * Execute the kernel to calculate the forces.
     * 
     * @param context    the context in which to execute this kernel
     */
    void executeForces(OpenMMContextImpl& context);
    /**
     * Execute the kernel to calculate the energy.
     * 
     * @param context    the context in which to execute this kernel
     * @return the potential energy due to the RBTorsionForce
     */
    double executeEnergy(OpenMMContextImpl& context);
private:
    int numTorsions;
    CudaPlatform::PlatformData& data;
    System& system;
};

185
/**
186
 * This kernel is invoked by NonbondedForce to calculate the forces acting on the system.
187
 */
188
class CudaCalcNonbondedForceKernel : public CalcNonbondedForceKernel {
189
public:
190
    CudaCalcNonbondedForceKernel(std::string name, const Platform& platform, CudaPlatform::PlatformData& data, System& system) : CalcNonbondedForceKernel(name, platform), data(data), system(system) {
191
    }
192
    ~CudaCalcNonbondedForceKernel();
193
    /**
194
     * Initialize the kernel.
195
     * 
196
     * @param system     the System this kernel will be applied to
197
     * @param force      the NonbondedForce this kernel will be used for
Peter Eastman's avatar
Peter Eastman committed
198
     * @param exclusions the i'th element lists the indices of all particles with which the i'th particle should not interact through
199
200
     *                   nonbonded forces.  Bonded 1-4 pairs are also included in this list, since they should be omitted from
     *                   the standard nonbonded calculation.
201
     */
202
    void initialize(const System& system, const NonbondedForce& force, const std::vector<std::set<int> >& exclusions);
203
204
205
    /**
     * Execute the kernel to calculate the forces.
     * 
206
     * @param context    the context in which to execute this kernel
207
     */
208
    void executeForces(OpenMMContextImpl& context);
209
210
211
    /**
     * Execute the kernel to calculate the energy.
     * 
212
     * @param context    the context in which to execute this kernel
213
     * @return the potential energy due to the NonbondedForce
214
     */
215
    double executeEnergy(OpenMMContextImpl& context);
216
private:
217
    CudaPlatform::PlatformData& data;
Peter Eastman's avatar
Peter Eastman committed
218
    int numParticles, num14;
219
    System& system;
220
221
};

222
223
224
225
226
227
228
229
230
/**
 * This kernel is invoked by GBSAOBCForceField to calculate the forces acting on the system.
 */
class CudaCalcGBSAOBCForceFieldKernel : public CalcGBSAOBCForceFieldKernel {
public:
    CudaCalcGBSAOBCForceFieldKernel(std::string name, const Platform& platform, CudaPlatform::PlatformData& data) : CalcGBSAOBCForceFieldKernel(name, platform), data(data) {
    }
    ~CudaCalcGBSAOBCForceFieldKernel();
    /**
231
     * Initialize the kernel.
232
     * 
233
234
     * @param system     the System this kernel will be applied to
     * @param force      the GBSAOBCForceField this kernel will be used for
235
     */
236
    void initialize(const System& system, const GBSAOBCForceField& force);
237
238
239
    /**
     * Execute the kernel to calculate the forces.
     * 
240
     * @param context    the context in which to execute this kernel
241
     */
242
    void executeForces(OpenMMContextImpl& context);
243
244
245
    /**
     * Execute the kernel to calculate the energy.
     * 
246
     * @param context    the context in which to execute this kernel
247
248
     * @return the potential energy due to the GBSAOBCForceField
     */
249
    double executeEnergy(OpenMMContextImpl& context);
250
251
252
private:
    CudaPlatform::PlatformData& data;
};
253
254
255
256
257
258
259
260
261
262
263
//
///**
// * This kernel is invoked by VerletIntegrator to take one time step.
// */
//class CudaIntegrateVerletStepKernel : public IntegrateVerletStepKernel {
//public:
//    CudaIntegrateVerletStepKernel(std::string name, const Platform& platform) : IntegrateVerletStepKernel(name, platform),
//        dynamics(0), shake(0), masses(0), shakeParameters(0), constraintIndices(0) {
//    }
//    ~CudaIntegrateVerletStepKernel();
//    /**
264
//     * Initialize the kernel.
265
//     * 
266
267
//     * @param system     the System this kernel will be applied to
//     * @param integrator the VerletIntegrator this kernel will be used for
268
//     */
269
//    void initialize(const System& system, const VerletIntegrator& integrator);
270
271
272
//    /**
//     * Execute the kernel.
//     * 
273
274
//     * @param context    the context in which to execute this kernel
//     * @param integrator the VerletIntegrator this kernel is being used for
275
//     */
276
//    void execute(OpenMMContextImpl& context, const VerletIntegrator& integrator);
277
278
279
280
281
282
283
284
285
//private:
//    CudaVerletDynamics* dynamics;
//    CudaShakeAlgorithm* shake;
//    RealOpenMM* masses;
//    RealOpenMM** shakeParameters;
//    int** constraintIndices;
//    int numConstraints;
//    double prevStepSize;
//};
286
287
288
289
290
291

/**
 * This kernel is invoked by LangevinIntegrator to take one time step.
 */
class CudaIntegrateLangevinStepKernel : public IntegrateLangevinStepKernel {
public:
292
    CudaIntegrateLangevinStepKernel(std::string name, const Platform& platform, CudaPlatform::PlatformData& data) : IntegrateLangevinStepKernel(name, platform), data(data) {
293
294
295
    }
    ~CudaIntegrateLangevinStepKernel();
    /**
Peter Eastman's avatar
Peter Eastman committed
296
     * Initialize the kernel, setting up the particle masses.
297
     * 
298
299
     * @param system     the System this kernel will be applied to
     * @param integrator the LangevinIntegrator this kernel will be used for
300
     */
301
    void initialize(const System& system, const LangevinIntegrator& integrator);
302
303
304
    /**
     * Execute the kernel.
     * 
305
306
     * @param context    the context in which to execute this kernel
     * @param integrator the LangevinIntegrator this kernel is being used for
307
     */
308
    void execute(OpenMMContextImpl& context, const LangevinIntegrator& integrator);
309
private:
310
    CudaPlatform::PlatformData& data;
311
312
    double prevTemp, prevFriction, prevStepSize;
};
313
314
315
316
317
318
319
320
321
322
323
//
///**
// * This kernel is invoked by BrownianIntegrator to take one time step.
// */
//class CudaIntegrateBrownianStepKernel : public IntegrateBrownianStepKernel {
//public:
//    CudaIntegrateBrownianStepKernel(std::string name, const Platform& platform) : IntegrateBrownianStepKernel(name, platform),
//        dynamics(0), shake(0), masses(0), shakeParameters(0), constraintIndices(0) {
//    }
//    ~CudaIntegrateBrownianStepKernel();
//    /**
324
//     * Initialize the kernel.
325
//     * 
326
327
//     * @param system     the System this kernel will be applied to
//     * @param integrator the BrownianIntegrator this kernel will be used for
328
//     */
329
//    void initialize(const System& system, const BrownianIntegrator& integrator);
330
331
332
//    /**
//     * Execute the kernel.
//     * 
333
334
//     * @param context    the context in which to execute this kernel
//     * @param integrator the BrownianIntegrator this kernel is being used for
335
//     */
336
//    void execute(OpenMMContextImpl& context, const BrownianIntegrator& integrator);
337
338
339
340
341
342
343
344
345
346
347
//private:
//    CudaBrownianDynamics* dynamics;
//    CudaShakeAlgorithm* shake;
//    RealOpenMM* masses;
//    RealOpenMM** shakeParameters;
//    int** constraintIndices;
//    int numConstraints;
//    double prevTemp, prevFriction, prevStepSize;
//};
//
///**
Peter Eastman's avatar
Peter Eastman committed
348
// * This kernel is invoked by AndersenThermostat at the start of each time step to adjust the particle velocities.
349
350
351
352
353
354
355
// */
//class CudaApplyAndersenThermostatKernel : public ApplyAndersenThermostatKernel {
//public:
//    CudaApplyAndersenThermostatKernel(std::string name, const Platform& platform) : ApplyAndersenThermostatKernel(name, platform), thermostat(0) {
//    }
//    ~CudaApplyAndersenThermostatKernel();
//    /**
356
//     * Initialize the kernel.
357
//     * 
358
359
//     * @param system     the System this kernel will be applied to
//     * @param thermostat the AndersenThermostat this kernel will be used for
360
//     */
361
//    void initialize(const System& system, const AndersenThermostat& thermostat);
362
363
364
//    /**
//     * Execute the kernel.
//     * 
365
//     * @param context    the context in which to execute this kernel
366
//     */
367
//    void execute(OpenMMContextImpl& context);
368
369
370
371
//private:
//    CudaAndersenThermostat* thermostat;
//    RealOpenMM* masses;
//};
372
373
374
375
376
377
378
379
380

/**
 * This kernel is invoked to calculate the kinetic energy of the system.
 */
class CudaCalcKineticEnergyKernel : public CalcKineticEnergyKernel {
public:
    CudaCalcKineticEnergyKernel(std::string name, const Platform& platform) : CalcKineticEnergyKernel(name, platform) {
    }
    /**
381
     * Initialize the kernel.
382
     * 
383
     * @param system     the System this kernel will be applied to
384
     */
385
    void initialize(const System& system);
386
387
388
    /**
     * Execute the kernel.
     * 
389
     * @param context    the context in which to execute this kernel
390
     */
391
    double execute(OpenMMContextImpl& context);
392
393
394
private:
    std::vector<double> masses;
};
395
396
397
398
399
400
401
402
403

/**
 * 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
404
     * Initialize the kernel, setting up the particle masses.
405
     * 
406
407
     * @param system     the System this kernel will be applied to
     * @param force      the CMMotionRemover this kernel will be used for
408
     */
409
    void initialize(const System& system, const CMMotionRemover& force);
410
411
412
    /**
     * Execute the kernel.
     * 
413
     * @param context    the context in which to execute this kernel
414
     */
415
    void execute(OpenMMContextImpl& context);
416
417
418
private:
    CudaPlatform::PlatformData& data;
};
419
420
421
422

} // namespace OpenMM

#endif /*OPENMM_CUDAKERNELS_H_*/