"vscode:/vscode.git/clone" did not exist on "461fcdf0986b60dc582607fff5b83bdab03fd4a0"
CudaArray.h 7.87 KB
Newer Older
1
2
3
4
5
6
7
8
9
10
11
#ifndef OPENMM_CUDAARRAY_H_
#define OPENMM_CUDAARRAY_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.               *
 *                                                                            *
Peter Eastman's avatar
Peter Eastman committed
12
 * Portions copyright (c) 2009-2018 Stanford University and the Authors.      *
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
 * 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 "openmm/OpenMMException.h"
31
#include "windowsExportCuda.h"
32
33
34
35
36
37
38
#include <cuda.h>
#include <iostream>
#include <sstream>
#include <vector>

namespace OpenMM {

39
40
class CudaContext;

41
42
43
44
45
/**
 * This class encapsulates a block of CUDA device memory.  It provides a simplified API
 * for working with it and for copying data to and from device memory.
 */

46
class OPENMM_EXPORT_CUDA CudaArray {
47
48
49
50
51
public:
    /**
     * Create a CudaArray object.  The object is allocated on the heap with the "new" operator.
     * The template argument is the data type of each array element.
     *
52
     * @param context           the context for which to create the array
53
54
55
56
     * @param size              the number of elements in the array
     * @param name              the name of the array
     */
    template <class T>
57
58
    static CudaArray* create(CudaContext& context, int size, const std::string& name) {
        return new CudaArray(context, size, sizeof(T), name);
59
    }
Peter Eastman's avatar
Peter Eastman committed
60
61
62
63
64
    /**
     * Create an uninitialized CudaArray object.  It does not point to any device memory,
     * and cannot be used until initialize() is called on it.
     */
    CudaArray();
65
66
67
    /**
     * Create a CudaArray object.
     *
68
     * @param context           the context for which to create the array
69
70
71
72
     * @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
     */
73
    CudaArray(CudaContext& context, int size, int elementSize, const std::string& name);
74
    ~CudaArray();
Peter Eastman's avatar
Peter Eastman committed
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
    /**
     * 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
     */
    void initialize(CudaContext& context, int size, int elementSize, const std::string& name);
    /**
     * 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
     */
    template <class T>
    void initialize(CudaContext& context, int size, const std::string& name) {
        initialize(context, size, sizeof(T), name);
    }
    /**
     * Recreate the internal storage to have a different size.
     */
    void resize(int size);
    /**
     * Get whether this array has been initialized.
     */
    bool isInitialized() const {
        return (pointer != 0);
    }
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
    /**
     * Get the number of elements in the array.
     */
    int getSize() const {
        return size;
    }
    /**
     * Get the size of each element in bytes.
     */
    int getElementSize() const {
        return elementSize;
    }
    /**
     * Get the name of the array.
     */
    const std::string& getName() const {
        return name;
    }
    /**
     * Get a pointer to the device memory.
     */
126
    CUdeviceptr& getDevicePointer() {
127
128
129
130
131
132
        return pointer;
    }
    /**
     * Copy the values in a vector to the device memory.
     */
    template <class T>
Peter Eastman's avatar
Peter Eastman committed
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
    void upload(const std::vector<T>& data, bool convert = true) {
        if (convert && data.size() == size && sizeof(T) != elementSize) {
            if (sizeof(T) == 2*elementSize) {
                // Convert values from double to single precision.
                const double* d = reinterpret_cast<const double*>(&data[0]);
                std::vector<float> v(elementSize*size/sizeof(float));
                for (int i = 0; i < v.size(); i++)
                    v[i] = (float) d[i];
                upload(&v[0], true);
                return;
            }
            if (2*sizeof(T) == elementSize) {
                // Convert values from single to double precision.
                const float* d = reinterpret_cast<const float*>(&data[0]);
                std::vector<double> v(elementSize*size/sizeof(double));
                for (int i = 0; i < v.size(); i++)
                    v[i] = (double) d[i];
                upload(&v[0], true);
                return;
            }
        }
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
        if (sizeof(T) != elementSize || data.size() != size)
            throw OpenMMException("Error uploading array "+name+": The specified vector does not match the size of the array");
        upload(&data[0], true);
    }
    /**
     * Copy the values in the Buffer to a vector.
     */
    template <class T>
    void download(std::vector<T>& data) const {
        if (sizeof(T) != elementSize)
            throw OpenMMException("Error downloading array "+name+": The specified vector has the wrong element size");
        if (data.size() != size)
            data.resize(size);
        download(&data[0], true);
    }
    /**
     * Copy the values in an array to the device memory.
     * 
     * @param data     the data to copy
     * @param blocking if true, this call will block until the transfer is complete.  If false,
     *                 the source array  must be in page-locked memory.
     */
176
    void upload(const void* data, bool blocking = true);
177
178
179
180
181
182
183
184
    /**
     * Copy the values in the device memory to an array.
     * 
     * @param data     the array to copy the memory to
     * @param blocking if true, this call will block until the transfer is complete.  If false,
     *                 the destination array must be in page-locked memory.
     */
    void download(void* data, bool blocking = true) const;
185
186
187
188
189
190
    /**
     * Copy the values in the device memory to a second array.
     * 
     * @param dest     the destination array to copy to
     */
    void copyTo(CudaArray& dest) const;
191
private:
Peter Eastman's avatar
Peter Eastman committed
192
    CudaContext* context;
193
194
195
196
197
198
199
200
201
    CUdeviceptr pointer;
    int size, elementSize;
    bool ownsMemory;
    std::string name;
};

} // namespace OpenMM

#endif /*OPENMM_CUDAARRAY_H_*/