OpenCLArray.h 10.6 KB
Newer Older
1
2
3
4
5
6
7
8
9
10
11
#ifndef OPENMM_OPENCLARRAY_H_
#define OPENMM_OPENCLARRAY_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-2022 Stanford University and the Authors.      *
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
 * 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/>.      *
 * -------------------------------------------------------------------------- */

30
31
32
#define CL_HPP_ENABLE_EXCEPTIONS
#define CL_HPP_TARGET_OPENCL_VERSION 120
#define CL_HPP_MINIMUM_OPENCL_VERSION 120
33
#include "openmm/OpenMMException.h"
34
35
#include "openmm/common/windowsExportCommon.h"
#include "openmm/common/ArrayInterface.h"
peastman's avatar
peastman committed
36
#include "opencl.hpp"
37
38
#include <iostream>
#include <sstream>
39
40
41
42
#include <vector>

namespace OpenMM {

peastman's avatar
peastman committed
43
44
class OpenCLContext;

45
46
/**
 * This class encapsulates an OpenCL Buffer.  It provides a simplified API for working with it,
47
 * and for copying data to and from the OpenCL Buffer.
48
49
 */

50
class OPENMM_EXPORT_COMMON OpenCLArray : public ArrayInterface {
51
52
public:
    /**
53
54
     * Create an OpenCLArray object.  The object is allocated on the heap with the "new" operator.
     * The template argument is the data type of each array element.
55
56
57
58
     *
     * @param context           the context for which to create the array
     * @param size              the number of elements in the array
     * @param name              the name of the array
59
     * @param flags             the set of flags to specify when creating the OpenCL Buffer
60
     */
61
    template <class T>
62
    static OpenCLArray* create(OpenCLContext& context, size_t size, const std::string& name, cl_int flags = CL_MEM_READ_WRITE) {
63
        return new OpenCLArray(context, size, sizeof(T), name, flags);
64
    }
65
    /**
66
67
     * Create an OpenCLArray object that uses a preexisting Buffer.  The object is allocated on the heap with the "new" operator.
     * The template argument is the data type of each array element.
68
69
70
71
72
73
     *
     * @param context           the context for which to create the array
     * @param buffer            the OpenCL Buffer this object encapsulates
     * @param size              the number of elements in the array
     * @param name              the name of the array
     */
74
    template <class T>
75
    static OpenCLArray* create(OpenCLContext& context, cl::Buffer* buffer, size_t size, const std::string& name) {
76
        return new OpenCLArray(context, buffer, size, sizeof(T), name);
77
    }
peastman's avatar
peastman committed
78
79
80
81
82
    /**
     * Create an uninitialized OpenCLArray object.  It does not point to any OpenCL Buffer,
     * and cannot be used until initialize() is called on it.
     */
    OpenCLArray();
83
84
85
86
87
88
89
90
91
    /**
     * Create an OpenCLArray object.
     *
     * @param context           the context for which to create the array
     * @param size              the number of elements in the array
     * @param elementSize       the size of each element in bytes
     * @param name              the name of the array
     * @param flags             the set of flags to specify when creating the OpenCL Buffer
     */
92
    OpenCLArray(OpenCLContext& context, size_t size, int elementSize, const std::string& name, cl_int flags = CL_MEM_READ_WRITE);
93
94
95
96
97
98
99
100
101
    /**
     * Create an OpenCLArray object that uses a preexisting Buffer.
     *
     * @param context           the context for which to create the array
     * @param buffer            the OpenCL Buffer this object encapsulates
     * @param size              the number of elements in the array
     * @param elementSize       the size of each element in bytes
     * @param name              the name of the array
     */
102
    OpenCLArray(OpenCLContext& context, cl::Buffer* buffer, size_t size, int elementSize, const std::string& name);
103
    ~OpenCLArray();
104
105
106
107
108
109
110
111
    /**
     * Initialize this array.
     *
     * @param context           the context for which to create the array
     * @param size              the number of elements in the array
     * @param elementSize       the size of each element in bytes
     * @param name              the name of the array
     */
112
    void initialize(ComputeContext& context, size_t size, int elementSize, const std::string& name);
peastman's avatar
peastman committed
113
114
115
116
117
118
119
120
121
    /**
     * Initialize this object.
     *
     * @param context           the context for which to create the array
     * @param size              the number of elements in the array
     * @param elementSize       the size of each element in bytes
     * @param name              the name of the array
     * @param flags             the set of flags to specify when creating the OpenCL Buffer
     */
122
    void initialize(OpenCLContext& context, size_t size, int elementSize, const std::string& name, cl_int flags);
peastman's avatar
peastman committed
123
124
125
126
127
128
129
130
131
    /**
     * Initialize this object to use a preexisting Buffer.
     *
     * @param context           the context for which to create the array
     * @param buffer            the OpenCL Buffer this object encapsulates
     * @param size              the number of elements in the array
     * @param elementSize       the size of each element in bytes
     * @param name              the name of the array
     */
132
    void initialize(OpenCLContext& context, cl::Buffer* buffer, size_t size, int elementSize, const std::string& name);
peastman's avatar
peastman committed
133
134
135
136
137
138
139
140
141
    /**
     * Initialize this object.  The template argument is the data type of each array element.
     *
     * @param context           the context for which to create the array
     * @param size              the number of elements in the array
     * @param name              the name of the array
     * @param flags             the set of flags to specify when creating the OpenCL Buffer
     */
    template <class T>
142
    void initialize(OpenCLContext& context, size_t size, const std::string& name, cl_int flags = CL_MEM_READ_WRITE) {
peastman's avatar
peastman committed
143
144
145
146
147
148
149
150
151
152
153
154
        initialize(context, size, sizeof(T), name, flags);
    }
    /**
     * Initialize this object to use a preexisting Buffer.  The template argument
     * is the data type of each array element.
     *
     * @param context           the context for which to create the array
     * @param buffer            the OpenCL Buffer this object encapsulates
     * @param size              the number of elements in the array
     * @param name              the name of the array
     */
    template <class T>
155
    void initialize(OpenCLContext& context, cl::Buffer* buffer, size_t size, const std::string& name) {
peastman's avatar
peastman committed
156
157
158
159
160
        initialize(context, buffer, size, sizeof(T), name);
    }
    /**
     * Recreate the internal storage to have a different size.
     */
161
    void resize(size_t size);
peastman's avatar
peastman committed
162
163
164
165
166
167
    /**
     * Get whether this array has been initialized.
     */
    bool isInitialized() const {
        return (buffer != NULL);
    }
168
169
170
    /**
     * Get the size of the array.
     */
171
    size_t getSize() const {
172
173
        return size;
    }
174
175
176
177
178
179
    /**
     * Get the size of each element in bytes.
     */
    int getElementSize() const {
        return elementSize;
    }
180
181
182
    /**
     * Get the name of the array.
     */
183
    const std::string& getName() const {
184
185
        return name;
    }
186
187
188
189
    /**
     * Get the context this array belongs to.
     */
    ComputeContext& getContext();
190
191
192
193
    /**
     * Get the queue in which to perform transfers.
     */
    cl::CommandQueue getQueue() const;
194
195
196
197
198
199
    /**
     * Get the OpenCL Buffer object.
     */
    cl::Buffer& getDeviceBuffer() {
        return *buffer;
    }
200
201
202
    /**
     * Copy the values in a vector to the Buffer.
     */
203
    template <class T>
204
205
    void upload(const std::vector<T>& data, bool convert=false) {
        ArrayInterface::upload(data, convert);
206
    }
207
208
209
    /**
     * Copy the values in the Buffer to a vector.
     */
210
    template <class T>
211
212
    void download(std::vector<T>& data) const {
        ArrayInterface::download(data);
213
    }
214
    /**
215
     * Copy the values from host memory to the array.
216
217
218
     * 
     * @param data     the data to copy
     * @param blocking if true, this call will block until the transfer is complete.
219
     */
220
221
222
223
224
225
226
227
228
229
230
231
    void upload(const void* data, bool blocking=true) {
        uploadSubArray(data, 0, getSize(), blocking);
    }
    /**
     * Copy values from host memory to a subset of the array.
     * 
     * @param data     the data to copy
     * @param offset   the index of the element within the array at which the copy should begin
     * @param elements the number of elements to copy
     * @param blocking if true, this call will block until the transfer is complete.
     */
    void uploadSubArray(const void* data, int offset, int elements, bool blocking=true);
232
    /**
233
     * Copy the values in the Buffer to an array.
234
235
236
     * 
     * @param data     the array to copy the memory to
     * @param blocking if true, this call will block until the transfer is complete.
237
     */
238
    void download(void* data, bool blocking=true) const;
239
    /**
240
241
242
     * Copy the values in the Buffer to a second OpenCLArray.
     * 
     * @param dest     the destination array to copy to
243
     */
244
    void copyTo(ArrayInterface& dest) const;
245
private:
peastman's avatar
peastman committed
246
    OpenCLContext* context;
247
    cl::Buffer* buffer;
248
249
    size_t size;
    int elementSize;
peastman's avatar
peastman committed
250
    cl_int flags;
251
    bool ownsBuffer;
252
253
254
255
256
257
    std::string name;
};

} // namespace OpenMM

#endif /*OPENMM_OPENCLARRAY_H_*/