OpenCLNonbondedUtilities.h 13.7 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-2013 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
#include <sstream>
34
35
36
37
#include <string>
#include <vector>

namespace OpenMM {
38
39
    
class OpenCLSort;
40
41

/**
42
 * This class provides a generic interface for calculating nonbonded interactions.  It does this in two
43
 * ways.  First, it can be used to create Kernels that evaluate nonbonded interactions.  Clients
44
45
46
47
48
49
50
51
52
53
54
55
56
 * 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.
 *
57
 * 2. calcForcesAndEnergy() is called on each ForceImpl in the System.
58
59
60
61
62
 *
 * 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
63
 * by ForceImpls during calcForcesAndEnergy().
64
65
 */

66
class OPENMM_EXPORT_OPENCL OpenCLNonbondedUtilities {
67
public:
68
    class ParameterInfo;
69
70
71
    OpenCLNonbondedUtilities(OpenCLContext& context);
    ~OpenCLNonbondedUtilities();
    /**
72
     * Add a nonbonded interaction to be evaluated by the default interaction kernel.
73
74
75
     *
     * @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
76
     * @param usesExclusions specifies whether this interaction uses exclusions.  If this is true, it must have identical exclusions to every other interaction.
77
78
     * @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
79
     * @param kernel         the code to evaluate the interaction
80
     * @param forceGroup     the force group in which the interaction should be calculated
81
     */
82
    void addInteraction(bool usesCutoff, bool usesPeriodic, bool usesExclusions, double cutoffDistance, const std::vector<std::vector<int> >& exclusionList, const std::string& kernel, int forceGroup);
83
    /**
84
     * Add a per-atom parameter that the default interaction kernel may depend on.
85
     */
86
    void addParameter(const ParameterInfo& parameter);
87
88
89
90
    /**
     * 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);
91
92
93
94
95
96
    /**
     * Specify the list of exclusions that an interaction outside the default kernel will depend on.
     * 
     * @param exclusionList  for each atom, specifies the list of other atoms whose interactions should be excluded
     */
    void requestExclusions(const std::vector<std::vector<int> >& exclusionList);
97
98
99
100
101
102
103
104
105
106
    /**
     * 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;
    }
107
108
109
110
111
112
    /**
     * Get the number of energy buffers required for nonbonded forces.
     */
    int getNumEnergyBuffers() {
        return numForceThreadBlocks*forceThreadBlockSize;
    }
113
114
115
116
117
118
119
120
121
122
123
124
    /**
     * Get whether a cutoff is being used.
     */
    bool getUseCutoff() {
        return useCutoff;
    }
    /**
     * Get whether periodic boundary conditions are being used.
     */
    bool getUsePeriodic() {
        return usePeriodic;
    }
125
126
127
128
129
130
131
132
133
134
135
136
    /**
     * Get the number of work groups used for computing nonbonded forces.
     */
    int getNumForceThreadBlocks() {
        return numForceThreadBlocks;
    }
    /**
     * Get the size of each work group used for computing nonbonded forces.
     */
    int getForceThreadBlockSize() {
        return forceThreadBlockSize;
    }
137
    /**
138
     * Get the maximum cutoff distance used by any force group.
139
     */
140
    double getMaxCutoffDistance();
Peter Eastman's avatar
Peter Eastman committed
141
142
143
144
    /**
     * Get whether any interactions have been added.
     */
    bool getHasInteractions() {
145
        return (groupCutoff.size() > 0);
146
    }
147
148
149
    /**
     * Prepare to compute interactions.  This updates the neighbor list.
     */
150
    void prepareInteractions(int forceGroups);
151
    /**
152
     * Compute the nonbonded interactions.
153
     */
154
    void computeInteractions(int forceGroups);
155
156
157
158
    /**
     * Check to see if the neighbor list arrays are large enough, and make them bigger if necessary.
     */
    void updateNeighborListSize();
159
160
161
    /**
     * Get the array containing the center of each atom block.
     */
162
    OpenCLArray& getBlockCenters() {
163
164
165
166
167
        return *blockCenter;
    }
    /**
     * Get the array containing the dimensions of each atom block.
     */
168
    OpenCLArray& getBlockBoundingBoxes() {
169
170
171
172
173
        return *blockBoundingBox;
    }
    /**
     * Get the array whose first element contains the number of tiles with interactions.
     */
174
    OpenCLArray& getInteractionCount() {
175
176
177
178
179
        return *interactionCount;
    }
    /**
     * Get the array containing tiles with interactions.
     */
180
    OpenCLArray& getInteractingTiles() {
181
182
183
        return *interactingTiles;
    }
    /**
184
     * Get the array containing the atoms in each tile with interactions.
185
     */
186
187
    OpenCLArray& getInteractingAtoms() {
        return *interactingAtoms;
188
    }
189
190
191
    /**
     * Get the array containing exclusion flags.
     */
192
    OpenCLArray& getExclusions() {
193
194
        return *exclusions;
    }
195
196
197
198
199
200
    /**
     * Get the array containing tiles with exclusions.
     */
    OpenCLArray& getExclusionTiles() {
        return *exclusionTiles;
    }
201
202
203
    /**
     * Get the array containing the index into the exclusion array for each tile.
     */
204
    OpenCLArray& getExclusionIndices() {
205
206
207
208
209
        return *exclusionIndices;
    }
    /**
     * Get the array listing where the exclusion data starts for each row.
     */
210
    OpenCLArray& getExclusionRowIndices() {
211
        return *exclusionRowIndices;
212
    }
213
214
215
216
217
218
219
220
221
222
223
224
    /**
     * Get the index of the first tile this context is responsible for processing.
     */
    int getStartTileIndex() const {
        return startTileIndex;
    }
    /**
     * Get the total number of tiles this context is responsible for processing.
     */
    int getNumTiles() const {
        return numTiles;
    }
225
    /**
226
227
228
229
230
231
232
233
234
     * Set whether to add padding to the cutoff distance when building the neighbor list.
     * This increases the size of the neighbor list (and thus the cost of computing interactions),
     * but also means we don't need to rebuild it every time step.  The default value is true,
     * since usually this improves performance.  For very expensive interactions, however,
     * it may be better to set this to false.
     */
    void setUsePadding(bool padding);
    /**
     * Set the range of atom blocks and tiles that should be processed by this context.
235
     */
236
    void setAtomBlockRange(double startFraction, double endFraction);
237
238
239
240
241
242
243
    /**
     * 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
244
     * @param arguments     arrays (other than per-atom parameters) that should be passed as arguments to the kernel
245
     * @param useExclusions specifies whether exclusions are applied to this interaction
246
     * @param isSymmetric   specifies whether the interaction is symmetric
247
248
249
250
251
252
253
     * @param groups        the set of force groups this kernel is for
     */
    cl::Kernel createInteractionKernel(const std::string& source, const std::vector<ParameterInfo>& params, const std::vector<ParameterInfo>& arguments, bool useExclusions, bool isSymmetric, int groups);
    /**
     * Create the set of kernels that will be needed for a particular combination of force groups.
     * 
     * @param groups    the set of force groups
254
     */
255
    void createKernelsForGroups(int groups);
256
private:
257
    class KernelSet;
258
    class BlockSortTrait;
259
    OpenCLContext& context;
260
    std::map<int, KernelSet> groupKernels;
261
    OpenCLArray* exclusionTiles;
262
263
264
265
    OpenCLArray* exclusions;
    OpenCLArray* exclusionIndices;
    OpenCLArray* exclusionRowIndices;
    OpenCLArray* interactingTiles;
266
    OpenCLArray* interactingAtoms;
267
268
269
    OpenCLArray* interactionCount;
    OpenCLArray* blockCenter;
    OpenCLArray* blockBoundingBox;
270
271
272
273
274
275
    OpenCLArray* sortedBlocks;
    OpenCLArray* sortedBlockCenter;
    OpenCLArray* sortedBlockBoundingBox;
    OpenCLArray* oldPositions;
    OpenCLArray* rebuildNeighborList;
    OpenCLSort* blockSorter;
276
277
    std::vector<std::vector<int> > atomExclusions;
    std::vector<ParameterInfo> parameters;
278
    std::vector<ParameterInfo> arguments;
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
    std::map<int, double> groupCutoff;
    std::map<int, std::string> groupKernelSource;
    double lastCutoff;
    bool useCutoff, usePeriodic, deviceIsCpu, anyExclusions, usePadding, forceRebuildNeighborList;
    int numForceBuffers, startTileIndex, numTiles, startBlockIndex, numBlocks, maxExclusions, numForceThreadBlocks;
    int forceThreadBlockSize, interactingBlocksThreadBlockSize, groupFlags;
};

/**
 * This class stores the kernels to execute for a set of force groups.
 */

class OpenCLNonbondedUtilities::KernelSet {
public:
    bool hasForces;
    double cutoffDistance;
    cl::Kernel forceKernel;
    cl::Kernel findBlockBoundsKernel;
    cl::Kernel sortBoxDataKernel;
    cl::Kernel findInteractingBlocksKernel;
    cl::Kernel findInteractionsWithinBlocksKernel;
300
301
};

302
303
304
305
/**
 * This class stores information about a per-atom parameter that may be used in a nonbonded kernel.
 */

306
307
class OpenCLNonbondedUtilities::ParameterInfo {
public:
308
309
310
    /**
     * Create a ParameterInfo object.
     *
311
312
313
314
315
     * @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
316
     */
317
318
319
320
    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;
321
322
323
324
325
        else {
            std::stringstream s;
            s << componentType << numComponents;
            type = s.str();
        }
326
    }
327
328
329
    const std::string& getName() const {
        return name;
    }
330
331
332
    const std::string& getComponentType() const {
        return componentType;
    }
333
334
335
    const std::string& getType() const {
        return type;
    }
336
337
338
    int getNumComponents() const {
        return numComponents;
    }
339
340
341
    int getSize() const {
        return size;
    }
342
343
    cl::Memory& getMemory() const {
        return *memory;
344
345
    }
private:
346
    std::string name;
347
    std::string componentType;
348
    std::string type;
349
    int size, numComponents;
350
    cl::Memory* memory;
351
352
353
354
355
};

} // namespace OpenMM

#endif /*OPENMM_OPENCLNONBONDEDUTILITIES_H_*/