OpenCLContext.h 7.88 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
#ifndef OPENMM_OPENCLCONTEXT_H_
#define OPENMM_OPENCLCONTEXT_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) 2009 Stanford University and the Authors.           *
 * 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/>.      *
 * -------------------------------------------------------------------------- */

#define __CL_ENABLE_EXCEPTIONS
31
#include <cl.hpp>
32
33
34
35
36

namespace OpenMM {

template <class T>
class OpenCLArray;
37
class OpenCLForceInfo;
38
class OpenCLIntegrationUtilities;
39
class System;
40

41
/**
42
43
44
 * We can't use predefined vector types like cl_float4, since different OpenCL implementations currently define
 * them in incompatible ways.  Hopefully that will be fixed in the future.  In the mean time, we define our own
 * types to represent them on the host.
45
46
 */

47
48
49
typedef struct {
    cl_float x, y;
} mm_float2;
50
51
52
typedef struct {
    cl_float x, y, z, w;
} mm_float4;
53
54
55
typedef struct {
    cl_float s0, s1, s2, s3, s4, s5, s6, s7;
} mm_float8;
56
57
58
59
60
61
typedef struct {
    cl_int x, y;
} mm_int2;
typedef struct {
    cl_int x, y, z, w;
} mm_int4;
62
63
64
typedef struct {
    cl_int s0, s1, s2, s3, s4, s5, s6, s7;
} mm_int8;
65

66
67
68
69
70
71
/**
 * This class contains the information associated with a Context by the OpenCL Platform.
 */

class OpenCLContext {
public:
72
73
    static const int ThreadBlockSize = 64;
    static const int TileSize = 32;
74
    OpenCLContext(int numParticles, int deviceIndex);
75
    ~OpenCLContext();
76
77
78
79
80
81
82
83
84
    /**
     * This is called to initialize internal data structures after all Forces in the system
     * have been initialized.
     */
    void initialize(const System& system);
    /**
     * Add an OpenCLForce to this context.
     */
    void addForce(OpenCLForceInfo* force);
85
86
87
88
    /**
     * Get the cl::Context associated with this object.
     */
    cl::Context& getContext() {
89
        return context;
90
91
92
93
94
    }
    /**
     * Get the cl::CommandQueue associated with this object.
     */
    cl::CommandQueue& getQueue() {
95
        return queue;
96
97
98
99
    }
    /**
     * Get the array which contains the position and charge of each atom.
     */
100
    OpenCLArray<mm_float4>& getPosq() {
101
102
103
        return *posq;
    }
    /**
104
     * Get the array which contains the velocity and inverse mass of each atom.
105
     */
106
    OpenCLArray<mm_float4>& getVelm() {
107
108
109
110
111
        return *velm;
    }
    /**
     * Get the array which contains the force on each atom.
     */
112
    OpenCLArray<mm_float4>& getForce() {
113
114
        return *force;
    }
115
116
117
    /**
     * Get the array which contains the buffers in which forces are computed.
     */
118
    OpenCLArray<mm_float4>& getForceBuffers() {
119
120
        return *forceBuffers;
    }
121
122
123
124
125
126
    /**
     * Get the array which contains the buffer in which energy is computed.
     */
    OpenCLArray<cl_float>& getEnergyBuffer() {
        return *energyBuffer;
    }
127
128
129
130
131
132
    /**
     * Get the array which contains the index of each atom.
     */
    OpenCLArray<cl_int>& getAtomIndex() {
        return *atomIndex;
    }
133
134
135
136
137
138
139
140
    /**
     * Load OpenCL source code from a file in the kernels directory.
     */
    std::string loadSourceFromFile(const std::string& filename) const;
    /**
     * Create an OpenCL Program from source code.
     */
    cl::Program createProgram(const std::string source);
141
142
143
144
145
146
147
    /**
     * Execute a kernel.
     *
     * @param kernel    the kernel to execute
     * @param workUnits the maximum number of work units that should be used
     */
    void executeKernel(cl::Kernel& kernel, int workUnits);
148
149
150
151
152
153
154
    /**
     * Set all elements of an array to 0.
     */
    void clearBuffer(OpenCLArray<float>& array);
    /**
     * Set all elements of an array to 0.
     */
155
    void clearBuffer(OpenCLArray<mm_float4>& array);
156
157
158
159
160
161
162
163
    /**
     * Given a collection of buffers packed into an array, sum them and store
     * the sum in the first buffer.
     *
     * @param array       the array containing the buffers to reduce
     * @param numBuffers  the number of buffers packed into the array
     */
    void reduceBuffer(OpenCLArray<mm_float4>& array, int numBuffers);
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
    /**
     * Get the current simulation time.
     */
    double getTime() {
        return time;
    }
    /**
     * Set the current simulation time.
     */
    void setTime(double t) {
        time = t;
    }
    /**
     * Get the number of integration steps that have been taken.
     */
    int getStepCount() {
        return stepCount;
    }
    /**
     * Set the number of integration steps that have been taken.
     */
    void setStepCount(int steps) {
        stepCount = steps;;
    }
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
    /**
     * Get the number of atoms.
     */
    int getNumAtoms() const {
        return numAtoms;
    }
    /**
     * Get the number of atoms, rounded up to a multiple of TileSize.  This is the actual size of
     * most arrays with one element per atom.
     */
    int getPaddedNumAtoms() const {
        return paddedNumAtoms;
    }
    /**
     * Get the number of blocks of TileSize atoms.
     */
    int getNumAtomBlocks() const {
        return numAtomBlocks;
    }
    /**
     * Get the standard number of thread blocks to use when executing kernels.
     */
    int getNumThreadBlocks() const {
        return numThreadBlocks;
    }
    /**
     * Get the total number of tiles used for nonbonded computation.
     */
    int getNumTiles() const {
        return numTiles;
    }
    /**
     * Get the number of force buffers.
     */
    int getNumForceBuffers() const {
        return numForceBuffers;
    }
225
226
227
228
229
230
    /**
     * Get the OpenCLIntegrationUtilities for this context.
     */
    OpenCLIntegrationUtilities& getIntegrationUtilties() {
        return *integration;
    }
231
private:
232
233
    double time;
    int stepCount;
234
235
236
237
238
239
240
241
242
243
244
    int numAtoms;
    int paddedNumAtoms;
    int numAtomBlocks;
    int numTiles;
    int numThreadBlocks;
    int numForceBuffers;
    cl::Context context;
    cl::Device device;
    cl::CommandQueue queue;
    cl::Program utilities;
    cl::Kernel clearBufferKernel;
245
246
    cl::Kernel reduceFloat4Kernel;
    std::vector<OpenCLForceInfo*> forces;
247
248
249
250
    OpenCLArray<mm_float4>* posq;
    OpenCLArray<mm_float4>* velm;
    OpenCLArray<mm_float4>* force;
    OpenCLArray<mm_float4>* forceBuffers;
251
    OpenCLArray<cl_float>* energyBuffer;
252
    OpenCLArray<cl_int>* atomIndex;
253
    OpenCLIntegrationUtilities* integration;
254
255
256
257
258
};

} // namespace OpenMM

#endif /*OPENMM_OPENCLCONTEXT_H_*/