OpenCLNonbondedUtilities.h 10.8 KB
Newer Older
1
2
3
4
5
6
7
8
9
10
11
#ifndef OPENMM_OPENCLNONBONDEDUTILITIES_H_
#define OPENMM_OPENCLNONBONDEDUTILITIES_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) 2009-2010 Stanford University and the Authors.      *
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
 * 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 "OpenCLContext.h"
#include "openmm/System.h"
32
#include "OpenCLExpressionUtilities.h"
33
34
35
36
37
38
#include <string>
#include <vector>

namespace OpenMM {

/**
39
 * This class provides a generic interface for calculating nonbonded interactions.  It does this in two
40
 * ways.  First, it can be used to create Kernels that evaluate nonbonded interactions.  Clients
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
 * only need to provide the code for evaluating a single interaction and the list of parameters it depends on.
 * A complete kernel is then synthesized using an appropriate algorithm to evaluate all interactions on all
 * atoms.
 *
 * Second, this class itself creates and invokes a single "default" interaction kernel, allowing several
 * different forces to be evaluated at once for greater efficiency.  Call addInteraction() and addParameter()
 * to add interactions to this default kernel.
 *
 * During each force or energy evaluation, the following sequence of steps takes place:
 *
 * 1. Data structures (e.g. neighbor lists) are calculated to allow nonbonded interactions to be evaluated
 * quickly.
 *
 * 2. calcForces() or calcEnergy() is called on each ForceImpl in the System.
 *
 * 3. Finally, the default interaction kernel is invoked to calculate all interactions that were added
 * to it.
 *
 * This sequence means that the default interaction kernel may depend on quantities that were calculated
 * by ForceImpls during calcForces() or calcEnergy().
61
62
 */

63
class OPENMM_EXPORT OpenCLNonbondedUtilities {
64
public:
65
    class ParameterInfo;
66
67
68
    OpenCLNonbondedUtilities(OpenCLContext& context);
    ~OpenCLNonbondedUtilities();
    /**
69
     * Add a nonbonded interaction to be evaluated by the default interaction kernel.
70
71
72
     *
     * @param usesCutoff     specifies whether a cutoff should be applied to this interaction
     * @param usesPeriodic   specifies whether periodic boundary conditions should be applied to this interaction
73
     * @param usesExclusions specifies whether this interaction uses exclusions.  If this is true, it must have identical exclusions to every other interaction.
74
75
     * @param cutoffDistance the cutoff distance for this interaction (ignored if usesCutoff is false)
     * @param exclusionList  for each atom, specifies the list of other atoms whose interactions should be excluded
76
     * @param kernel         the code to evaluate the interaction
77
     */
78
    void addInteraction(bool usesCutoff, bool usesPeriodic, bool usesExclusions, double cutoffDistance, const std::vector<std::vector<int> >& exclusionList, const std::string& kernel);
79
    /**
80
     * Add a per-atom parameter that the default interaction kernel may depend on.
81
     */
82
    void addParameter(const ParameterInfo& parameter);
83
84
85
86
    /**
     * Add an array (other than a per-atom parameter) that should be passed as an argument to the default interaction kernel.
     */
    void addArgument(const ParameterInfo& parameter);
87
88
89
90
91
92
93
94
95
96
    /**
     * Initialize this object in preparation for a simulation.
     */
    void initialize(const System& system);
    /**
     * Get the number of force buffers required for nonbonded forces.
     */
    int getNumForceBuffers() {
        return numForceBuffers;
    }
97
98
99
100
101
102
103
104
105
106
107
108
    /**
     * Get whether a cutoff is being used.
     */
    bool getUseCutoff() {
        return useCutoff;
    }
    /**
     * Get whether periodic boundary conditions are being used.
     */
    bool getUsePeriodic() {
        return usePeriodic;
    }
109
110
111
112
113
114
    /**
     * Get whether there is one force buffer per atom block.
     */
    bool getForceBufferPerAtomBlock() {
        return forceBufferPerAtomBlock;
    }
115
116
117
118
119
120
    /**
     * Get the cutoff distance.
     */
    double getCutoffDistance() {
        return cutoff;
    }
121
122
123
124
125
126
127
128
129
    /**
     * Prepare to compute interactions.  This updates the neighbor list.
     */
    void prepareInteractions();
    /**
     * Compute the nonbonded interactions.  This will only be executed once after each call to
     * prepareInteractions().  Additional calls return immediately without doing anything.
     */
    void computeInteractions();
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
    /**
     * Get the array containing the center of each atom block.
     */
    OpenCLArray<mm_float4>& getBlockCenters() {
        return *blockCenter;
    }
    /**
     * Get the array containing the dimensions of each atom block.
     */
    OpenCLArray<mm_float4>& getBlockBoundingBoxes() {
        return *blockBoundingBox;
    }
    /**
     * Get the array whose first element contains the number of tiles with interactions.
     */
    OpenCLArray<cl_uint>& getInteractionCount() {
        return *interactionCount;
    }
    /**
     * Get the array containing tiles with interactions.
     */
151
    OpenCLArray<mm_ushort2>& getInteractingTiles() {
152
153
154
155
156
157
158
159
        return *interactingTiles;
    }
    /**
     * Get the array containing flags for tiles with interactions.
     */
    OpenCLArray<cl_uint>& getInteractionFlags() {
        return *interactionFlags;
    }
160
161
162
163
164
165
166
167
168
169
    /**
     * Get the array containing exclusion flags.
     */
    OpenCLArray<cl_uint>& getExclusions() {
        return *exclusions;
    }
    /**
     * Get the array containing the index into the exclusion array for each tile.
     */
    OpenCLArray<cl_uint>& getExclusionIndices() {
170
171
172
173
174
175
176
        return *exclusionIndices;
    }
    /**
     * Get the array listing where the exclusion data starts for each row.
     */
    OpenCLArray<cl_uint>& getExclusionRowIndices() {
        return *exclusionRowIndices;
177
    }
178
179
180
181
182
183
184
    /**
     * Create a Kernel for evaluating a nonbonded interaction.  Cutoffs and periodic boundary conditions
     * are assumed to be the same as those for the default interaction Kernel, since this kernel will use
     * the same neighbor list.
     * 
     * @param source        the source code for evaluating the force and energy
     * @param params        the per-atom parameters this kernel may depend on
185
     * @param arguments     arrays (other than per-atom parameters) that should be passed as arguments to the kernel
186
     * @param useExclusions specifies whether exclusions are applied to this interaction
187
     * @param isSymmetric   specifies whether the interaction is symmetric
188
     */
189
    cl::Kernel createInteractionKernel(const std::string& source, const std::vector<ParameterInfo>& params, const std::vector<ParameterInfo>& arguments, bool useExclusions, bool isSymmetric) const;
190
private:
191
    static int findExclusionIndex(int x, int y, const std::vector<cl_uint>& exclusionIndices, const std::vector<cl_uint>& exclusionRowIndices);
192
193
    OpenCLContext& context;
    cl::Kernel forceKernel;
194
195
196
    cl::Kernel findBlockBoundsKernel;
    cl::Kernel findInteractingBlocksKernel;
    cl::Kernel findInteractionsWithinBlocksKernel;
197
    OpenCLArray<cl_uint>* exclusions;
198
199
    OpenCLArray<cl_uint>* exclusionIndices;
    OpenCLArray<cl_uint>* exclusionRowIndices;
200
    OpenCLArray<mm_ushort2>* interactingTiles;
201
202
203
204
    OpenCLArray<cl_uint>* interactionFlags;
    OpenCLArray<cl_uint>* interactionCount;
    OpenCLArray<mm_float4>* blockCenter;
    OpenCLArray<mm_float4>* blockBoundingBox;
205
206
    std::vector<std::vector<int> > atomExclusions;
    std::vector<ParameterInfo> parameters;
207
    std::vector<ParameterInfo> arguments;
208
    std::string kernelSource;
209
    std::map<std::string, std::string> kernelDefines;
210
    double cutoff;
211
    bool useCutoff, usePeriodic, forceBufferPerAtomBlock;
212
213
214
    int numForceBuffers;
};

215
216
217
218
/**
 * This class stores information about a per-atom parameter that may be used in a nonbonded kernel.
 */

219
220
class OpenCLNonbondedUtilities::ParameterInfo {
public:
221
222
223
    /**
     * Create a ParameterInfo object.
     *
224
225
226
227
228
     * @param name           the name of the parameter
     * @param type           the data type of the parameter's components
     * @param numComponents  the number of components in the parameter
     * @param size           the size of the parameter in bytes
     * @param memory         the memory containing the parameter values
229
     */
230
231
232
233
234
235
    ParameterInfo(const std::string& name, const std::string& componentType, int numComponents, int size, cl::Memory& memory) :
            name(name), componentType(componentType), numComponents(numComponents), size(size), memory(&memory) {
        if (numComponents == 1)
            type = componentType;
        else
            type = componentType+OpenCLExpressionUtilities::intToString(numComponents);
236
    }
237
238
239
    const std::string& getName() const {
        return name;
    }
240
241
242
    const std::string& getComponentType() const {
        return componentType;
    }
243
244
245
    const std::string& getType() const {
        return type;
    }
246
247
248
    int getNumComponents() const {
        return numComponents;
    }
249
250
251
    int getSize() const {
        return size;
    }
252
253
    cl::Memory& getMemory() const {
        return *memory;
254
255
    }
private:
256
    std::string name;
257
    std::string componentType;
258
    std::string type;
259
    int size, numComponents;
260
    cl::Memory* memory;
261
262
263
264
265
};

} // namespace OpenMM

#endif /*OPENMM_OPENCLNONBONDEDUTILITIES_H_*/