"include/cuhash/debugging.h" did not exist on "0757c45b1e361bbc703a5e1e212ec16f8ed0d339"
CudaArray.h 7.36 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.               *
 *                                                                            *
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
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
32
#include "openmm/common/windowsExportCommon.h"
#include "openmm/common/ArrayInterface.h"
33
34
35
36
37
38
39
#include <cuda.h>
#include <iostream>
#include <sstream>
#include <vector>

namespace OpenMM {

40
41
class CudaContext;

42
43
44
45
46
/**
 * 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.
 */

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

} // namespace OpenMM

#endif /*OPENMM_CUDAARRAY_H_*/