"openmmapi/include/NonbondedForce.h" did not exist on "dfe03fe9089f8d6f1fd6c2a6acae4392e5984f53"
OpenCLKernels.h 14.2 KB
Newer Older
1
2
3
4
5
6
7
8
9
10
11
#ifndef OPENMM_OPENCLKERNELS_H_
#define OPENMM_OPENCLKERNELS_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-2019 Stanford University and the Authors.      *
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
 * Authors: Peter Eastman                                                     *
 * Contributors:                                                              *
 *                                                                            *
 * 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.                                        *
 *                                                                            *
 * 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.                        *
 *                                                                            *
 * 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/>.      *
 * -------------------------------------------------------------------------- */

#include "OpenCLPlatform.h"
31
32
#include "OpenCLArray.h"
#include "OpenCLContext.h"
33
34
#include "OpenCLFFT3D.h"
#include "OpenCLSort.h"
35
36
#include "openmm/kernels.h"
#include "openmm/System.h"
37
#include "openmm/common/CommonKernels.h"
38
39
40

namespace OpenMM {

41
/**
42
43
44
 * 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.
45
 */
46
class OpenCLCalcForcesAndEnergyKernel : public CalcForcesAndEnergyKernel {
47
public:
48
    OpenCLCalcForcesAndEnergyKernel(std::string name, const Platform& platform, OpenCLContext& cl) : CalcForcesAndEnergyKernel(name, platform), cl(cl) {
49
50
51
52
53
54
55
56
    }
    /**
     * Initialize the kernel.
     *
     * @param system     the System this kernel will be applied to
     */
    void initialize(const System& system);
    /**
57
     * This is called at the beginning of each force/energy computation, before calcForcesAndEnergy() has been called on
58
     * any ForceImpl.
59
     *
60
61
62
     * @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
63
     * @param groups        a set of bit flags for which force groups to include
64
     */
65
    void beginComputation(ContextImpl& context, bool includeForce, bool includeEnergy, int groups);
66
    /**
67
     * This is called at the end of each force/energy computation, after calcForcesAndEnergy() has been called on
68
69
     * every ForceImpl.
     *
70
71
72
     * @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
73
     * @param groups        a set of bit flags for which force groups to include
74
75
     * @param valid         the method may set this to false to indicate the results are invalid and the force/energy
     *                      calculation should be repeated
76
     * @return the potential energy of the system.  This value is added to all values returned by ForceImpls'
77
     * calcForcesAndEnergy() methods.  That is, each force kernel may <i>either</i> return its contribution to the
78
79
     * energy directly, <i>or</i> add it to an internal buffer so that it will be included here.
     */
80
    double finishComputation(ContextImpl& context, bool includeForce, bool includeEnergy, int groups, bool& valid);
81
private:
82
   OpenCLContext& cl;
83
84
85
};

/**
86
87
 * This kernel provides methods for setting and retrieving various state data: time, positions,
 * velocities, and forces.
88
 */
89
class OpenCLUpdateStateDataKernel : public UpdateStateDataKernel {
90
public:
91
    OpenCLUpdateStateDataKernel(std::string name, const Platform& platform, OpenCLContext& cl) : UpdateStateDataKernel(name, platform), cl(cl) {
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
    }
    /**
     * 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
     */
    double getTime(const ContextImpl& context) const;
    /**
     * Set the current time (in picoseconds).
     *
     * @param context    the context in which to execute this kernel
     */
    void setTime(ContextImpl& context, double time);
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
    /**
     * 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);
141
142
143
144
145
146
    /**
     * Get the current derivatives of the energy with respect to context parameters.
     *
     * @param derivs  on exit, this contains the derivatives
     */
    void getEnergyParameterDerivatives(ContextImpl& context, std::map<std::string, double>& derivs);
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
    /**
     * 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
     */
162
    void setPeriodicBoxVectors(ContextImpl& context, const Vec3& a, const Vec3& b, const Vec3& c);
Peter Eastman's avatar
Peter Eastman committed
163
164
165
166
167
168
169
170
171
172
173
174
    /**
     * Create a checkpoint recording the current state of the Context.
     * 
     * @param stream    an output stream the checkpoint data should be written to
     */
    void createCheckpoint(ContextImpl& context, std::ostream& stream);
    /**
     * Load a checkpoint that was written by createCheckpoint().
     * 
     * @param stream    an input stream the checkpoint data should be read from
     */
    void loadCheckpoint(ContextImpl& context, std::istream& stream);
175
private:
176
    OpenCLContext& cl;
177
};
178

179
180
181
182
183
/**
 * This kernel is invoked by NonbondedForce to calculate the forces acting on the system.
 */
class OpenCLCalcNonbondedForceKernel : public CalcNonbondedForceKernel {
public:
184
    OpenCLCalcNonbondedForceKernel(std::string name, const Platform& platform, OpenCLContext& cl, const System& system) : CalcNonbondedForceKernel(name, platform),
185
            hasInitializedKernel(false), cl(cl), sort(NULL), fft(NULL), dispersionFft(NULL), pmeio(NULL), usePmeQueue(false) {
186
187
188
189
190
191
192
193
194
195
    }
    ~OpenCLCalcNonbondedForceKernel();
    /**
     * Initialize the kernel.
     *
     * @param system     the System this kernel will be applied to
     * @param force      the NonbondedForce this kernel will be used for
     */
    void initialize(const System& system, const NonbondedForce& force);
    /**
196
     * Execute the kernel to calculate the forces and/or energy.
197
     *
198
199
200
     * @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
201
202
     * @param includeDirect  true if direct space interactions should be included
     * @param includeReciprocal  true if reciprocal space interactions should be included
203
     * @return the potential energy due to the force
204
     */
205
    double execute(ContextImpl& context, bool includeForces, bool includeEnergy, bool includeDirect, bool includeReciprocal);
206
207
208
209
210
211
212
    /**
     * Copy changed parameters over to a context.
     *
     * @param context    the context to copy parameters to
     * @param force      the NonbondedForce to copy the parameters from
     */
    void copyParametersToContext(ContextImpl& context, const NonbondedForce& force);
213
214
    /**
     * Get the parameters being used for PME.
215
     *
216
217
218
219
220
221
     * @param alpha   the separation parameter
     * @param nx      the number of grid points along the X axis
     * @param ny      the number of grid points along the Y axis
     * @param nz      the number of grid points along the Z axis
     */
    void getPMEParameters(double& alpha, int& nx, int& ny, int& nz) const;
222
223
224
225
226
227
228
229
    /**
     * Get the parameters being used for the dispersion term in LJPME.
     *
     * @param alpha   the separation parameter
     * @param nx      the number of grid points along the X axis
     * @param ny      the number of grid points along the Y axis
     * @param nz      the number of grid points along the Z axis
     */
230
    void getLJPMEParameters(double& alpha, int& nx, int& ny, int& nz) const;
231
private:
232
233
234
235
236
237
238
239
240
    class SortTrait : public OpenCLSort::SortTrait {
        int getDataSize() const {return 8;}
        int getKeySize() const {return 4;}
        const char* getDataType() const {return "int2";}
        const char* getKeyType() const {return "int";}
        const char* getMinKey() const {return "INT_MIN";}
        const char* getMaxKey() const {return "INT_MAX";}
        const char* getMaxValue() const {return "(int2) (INT_MAX, INT_MAX)";}
        const char* getSortKey() const {return "value.y";}
241
    };
242
    class ForceInfo;
243
244
245
    class PmeIO;
    class PmePreComputation;
    class PmePostComputation;
246
247
    class SyncQueuePreComputation;
    class SyncQueuePostComputation;
248
    OpenCLContext& cl;
249
    ForceInfo* info;
250
    bool hasInitializedKernel;
251
    OpenCLArray charges;
peastman's avatar
peastman committed
252
253
    OpenCLArray sigmaEpsilon;
    OpenCLArray exceptionParams;
254
255
    OpenCLArray exclusionAtoms;
    OpenCLArray exclusionParams;
256
257
258
259
260
261
262
    OpenCLArray baseParticleParams;
    OpenCLArray baseExceptionParams;
    OpenCLArray particleParamOffsets;
    OpenCLArray exceptionParamOffsets;
    OpenCLArray particleOffsetIndices;
    OpenCLArray exceptionOffsetIndices;
    OpenCLArray globalParams;
peastman's avatar
peastman committed
263
    OpenCLArray cosSinSums;
Peter Eastman's avatar
Peter Eastman committed
264
    OpenCLArray pmeGrid1;
peastman's avatar
peastman committed
265
266
267
268
269
270
271
272
273
274
275
    OpenCLArray pmeGrid2;
    OpenCLArray pmeBsplineModuliX;
    OpenCLArray pmeBsplineModuliY;
    OpenCLArray pmeBsplineModuliZ;
    OpenCLArray pmeDispersionBsplineModuliX;
    OpenCLArray pmeDispersionBsplineModuliY;
    OpenCLArray pmeDispersionBsplineModuliZ;
    OpenCLArray pmeBsplineTheta;
    OpenCLArray pmeAtomRange;
    OpenCLArray pmeAtomGridIndex;
    OpenCLArray pmeEnergyBuffer;
276
    OpenCLSort* sort;
277
278
    cl::CommandQueue pmeQueue;
    cl::Event pmeSyncEvent;
279
    OpenCLFFT3D* fft;
280
    OpenCLFFT3D* dispersionFft;
281
282
    Kernel cpuPme;
    PmeIO* pmeio;
283
    SyncQueuePostComputation* syncQueue;
Peter Eastman's avatar
Peter Eastman committed
284
    cl::Kernel computeParamsKernel, computeExclusionParamsKernel;
285
286
    cl::Kernel ewaldSumsKernel;
    cl::Kernel ewaldForcesKernel;
287
    cl::Kernel pmeAtomRangeKernel;
288
    cl::Kernel pmeDispersionAtomRangeKernel;
289
    cl::Kernel pmeZIndexKernel;
290
    cl::Kernel pmeDispersionZIndexKernel;
291
292
    cl::Kernel pmeGridIndexKernel;
    cl::Kernel pmeDispersionGridIndexKernel;
293
    cl::Kernel pmeSpreadChargeKernel;
294
    cl::Kernel pmeDispersionSpreadChargeKernel;
295
    cl::Kernel pmeFinishSpreadChargeKernel;
296
    cl::Kernel pmeDispersionFinishSpreadChargeKernel;
297
    cl::Kernel pmeConvolutionKernel;
298
    cl::Kernel pmeDispersionConvolutionKernel;
299
    cl::Kernel pmeEvalEnergyKernel;
300
    cl::Kernel pmeDispersionEvalEnergyKernel;
301
    cl::Kernel pmeInterpolateForceKernel;
302
    cl::Kernel pmeDispersionInterpolateForceKernel;
303
    std::map<std::string, std::string> pmeDefines;
304
    std::vector<std::pair<int, int> > exceptionAtoms;
305
306
    std::vector<std::string> paramNames;
    std::vector<double> paramValues;
307
    double ewaldSelfEnergy, dispersionCoefficient, alpha, dispersionAlpha;
308
    int gridSizeX, gridSizeY, gridSizeZ;
309
    int dispersionGridSizeX, dispersionGridSizeY, dispersionGridSizeZ;
Peter Eastman's avatar
Peter Eastman committed
310
    bool hasCoulomb, hasLJ, usePmeQueue, doLJPME, usePosqCharges, recomputeParams, hasOffsets;
311
    NonbondedMethod nonbondedMethod;
312
    static const int PmeOrder = 5;
313
314
};

315
/**
316
 * This kernel is invoked by CustomCVForce to calculate the forces acting on the system and the energy of the system.
317
 */
318
class OpenCLCalcCustomCVForceKernel : public CommonCalcCustomCVForceKernel {
319
public:
320
    OpenCLCalcCustomCVForceKernel(std::string name, const Platform& platform, ComputeContext& cc) : CommonCalcCustomCVForceKernel(name, platform, cc) {
321
    }
322
323
    ComputeContext& getInnerComputeContext(ContextImpl& innerContext) {
        return *reinterpret_cast<OpenCLPlatform::PlatformData*>(innerContext.getPlatformData())->contexts[0];
324
    }
325
};
326

327
328
329
} // namespace OpenMM

#endif /*OPENMM_OPENCLKERNELS_H_*/