"plugins/vscode:/vscode.git/clone" did not exist on "0c96184bcfd3760db6fa6e8634f67a7283027436"
OpenCLArray.h 6.98 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-2012 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/OpenMMException.h"
32
#include "windowsExportOpenCL.h"
33
34
#include <iostream>
#include <sstream>
35
36
37
38
39
40
#include <vector>

namespace OpenMM {

/**
 * This class encapsulates an OpenCL Buffer.  It provides a simplified API for working with it,
41
 * and for copying data to and from the OpenCL Buffer.
42
43
 */

44
class OPENMM_EXPORT_OPENCL OpenCLArray {
45
46
public:
    /**
47
48
     * 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.
49
50
51
52
     *
     * @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
53
     * @param flags             the set of flags to specify when creating the OpenCL Buffer
54
     */
55
56
57
    template <class T>
    static OpenCLArray* create(OpenCLContext& context, int size, const std::string& name, cl_int flags = CL_MEM_READ_WRITE) {
        return new OpenCLArray(context, size, sizeof(T), name, flags);
58
    }
59
    /**
60
61
     * 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.
62
63
64
65
66
67
     *
     * @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
     */
68
69
70
    template <class T>
    static OpenCLArray* create(OpenCLContext& context, cl::Buffer* buffer, int size, const std::string& name) {
        return new OpenCLArray(context, buffer, size, sizeof(T), name);
71
    }
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
    /**
     * 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
     */
    OpenCLArray(OpenCLContext& context, int size, int elementSize, const std::string& name, cl_int flags = CL_MEM_READ_WRITE);
    /**
     * 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
     */
    OpenCLArray(OpenCLContext& context, cl::Buffer* buffer, int size, int elementSize, const std::string& name);
    ~OpenCLArray();
93
94
95
    /**
     * Get the size of the array.
     */
96
    int getSize() const {
97
98
        return size;
    }
99
100
101
102
103
104
    /**
     * Get the size of each element in bytes.
     */
    int getElementSize() const {
        return elementSize;
    }
105
106
107
    /**
     * Get the name of the array.
     */
108
    const std::string& getName() const {
109
110
        return name;
    }
111
112
113
114
115
116
    /**
     * Get the OpenCL Buffer object.
     */
    cl::Buffer& getDeviceBuffer() {
        return *buffer;
    }
117
118
119
    /**
     * Copy the values in a vector to the Buffer.
     */
120
121
122
123
    template <class T>
    void upload(const std::vector<T>& data, bool blocking = true) {
        if (sizeof(T) != elementSize || data.size() != size)
            throw OpenMMException("Error uploading array "+name+": The specified vector does not match the size of the array");
124
        upload(&data[0], blocking);
125
    }
126
127
128
    /**
     * Copy the values in the Buffer to a vector.
     */
129
130
131
132
    template <class T>
    void download(std::vector<T>& data, bool blocking = true) const {
        if (sizeof(T) != elementSize)
            throw OpenMMException("Error downloading array "+name+": The specified vector has the wrong element size");
133
134
        if (data.size() != size)
            data.resize(size);
135
        download(&data[0], blocking);
136
    }
137
138
    /**
     * Copy the values in an array to the Buffer.
139
140
141
     * 
     * @param data     the data to copy
     * @param blocking if true, this call will block until the transfer is complete.
142
     */
143
    void upload(const void* data, bool blocking = true);
144
    /**
145
     * Copy the values in the Buffer to an array.
146
147
148
     * 
     * @param data     the array to copy the memory to
     * @param blocking if true, this call will block until the transfer is complete.
149
     */
150
    void download(void* data, bool blocking = true) const;
151
    /**
152
153
154
     * Copy the values in the Buffer to a second OpenCLArray.
     * 
     * @param dest     the destination array to copy to
155
     */
156
    void copyTo(OpenCLArray& dest) const;
157
158
159
private:
    OpenCLContext& context;
    cl::Buffer* buffer;
160
    int size, elementSize;
161
    bool ownsBuffer;
162
163
164
165
166
167
    std::string name;
};

} // namespace OpenMM

#endif /*OPENMM_OPENCLARRAY_H_*/