-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathCudaArray.h
More file actions
265 lines (214 loc) · 6.02 KB
/
CudaArray.h
File metadata and controls
265 lines (214 loc) · 6.02 KB
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
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
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
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
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
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
/*
* SPDX-FileCopyrightText: Copyright (c) 2023-2024 NVIDIA CORPORATION & AFFILIATES. All rights reserved.
* SPDX-License-Identifier: LicenseRef-NvidiaProprietary
*
* NVIDIA CORPORATION, its affiliates and licensors retain all intellectual
* property and proprietary rights in and to this material, related
* documentation and any modifications thereto. Any use, reproduction,
* disclosure or distribution of this material and related documentation
* without an express license agreement from NVIDIA CORPORATION or
* its affiliates is strictly prohibited.
*/
#pragma once
#include <libntc/ntc.h>
#include <cuda_runtime_api.h>
#include <algorithm>
namespace ntc
{
template <typename T>
class HostArray {
public:
HostArray(IAllocator* allocator)
: m_allocator(allocator)
{ }
HostArray(HostArray&& other) noexcept
{
*this = std::move(other);
}
~HostArray()
{
Deallocate();
}
[[nodiscard]] bool Allocate(size_t length)
{
if (m_hostMemory)
return false;
m_length = length;
m_hostMemory = (T*)m_allocator->Allocate(Size());
return m_hostMemory != nullptr;
}
void Deallocate()
{
if (!m_hostMemory)
return;
m_allocator->Deallocate(m_hostMemory, Size());
m_hostMemory = nullptr;
}
T* HostPtr()
{
return m_hostMemory;
}
const T* HostPtr() const
{
return m_hostMemory;
}
[[nodiscard]] T* HostPtrOffset(size_t offsetInElements)
{
return m_hostMemory ? m_hostMemory + offsetInElements : nullptr;
}
[[nodiscard]] const T* HostPtrOffset(size_t offsetInElements) const
{
return m_hostMemory ? m_hostMemory + offsetInElements : nullptr;
}
size_t Length() const
{
return m_length;
}
size_t Size() const
{
return m_length * sizeof(T);
}
HostArray<T>& operator=(HostArray<T>&& other) noexcept
{
m_length = other.m_length;
m_hostMemory = other.m_hostMemory;
other.m_length = 0;
other.m_hostMemory = nullptr;
return *this;
}
protected:
IAllocator* m_allocator;
size_t m_length = 0;
T* m_hostMemory = nullptr;
};
template <typename T>
class DeviceArray
{
public:
DeviceArray() = default;
DeviceArray(DeviceArray&& other) noexcept
{
*this = std::move(other);
}
~DeviceArray()
{
Deallocate();
}
[[nodiscard]] bool Allocate(size_t length)
{
if (m_deviceMemory)
return false;
m_length = length;
return cudaMalloc((void**)&m_deviceMemory, length * sizeof(T)) == cudaSuccess;
}
bool Deallocate()
{
if (!m_deviceMemory)
return true;
cudaError_t err = cudaFree(m_deviceMemory);
m_deviceMemory = nullptr;
return err == cudaSuccess;
}
[[nodiscard]] T* DevicePtr()
{
return m_deviceMemory;
}
[[nodiscard]] const T* DevicePtr() const
{
return m_deviceMemory;
}
[[nodiscard]] T* DevicePtrOffset(size_t offsetInElements)
{
return m_deviceMemory ? m_deviceMemory + offsetInElements : nullptr;
}
[[nodiscard]] const T* DevicePtrOffset(size_t offsetInElements) const
{
return m_deviceMemory ? m_deviceMemory + offsetInElements : nullptr;
}
size_t Length() const
{
return m_length;
}
size_t Size() const
{
return m_length * sizeof(T);
}
DeviceArray<T>& operator=(DeviceArray<T>&& other) noexcept
{
m_length = other.m_length;
m_deviceMemory = other.m_deviceMemory;
other.m_length = 0;
other.m_deviceMemory = nullptr;
return *this;
}
protected:
size_t m_length = 0;
T* m_deviceMemory = nullptr;
};
template <typename T>
class DeviceAndHostArray : public HostArray<T>, public DeviceArray<T> {
public:
DeviceAndHostArray(IAllocator* allocator)
: HostArray<T>(allocator)
{ }
DeviceAndHostArray(DeviceAndHostArray&& other) noexcept
{
*this = std::move(other);
}
~DeviceAndHostArray()
{
Deallocate();
}
[[nodiscard]] bool Allocate(size_t length)
{
return HostArray<T>::Allocate(length) && DeviceArray<T>::Allocate(length);
}
bool Deallocate()
{
HostArray<T>::Deallocate();
return DeviceArray<T>::Deallocate();
}
// Copies the entire array or a part of it from CPU memory to GPU memory.
// If length == 0 (default), the entire array is copied.
cudaError_t CopyToDevice(size_t length = 0)
{
if (!this->m_deviceMemory)
return cudaErrorInvalidDevicePointer;
if (!this->m_hostMemory)
return cudaErrorInvalidHostPointer;
if (length == 0)
length = HostArray<T>::m_length;
else if (length > HostArray<T>::m_length)
return cudaErrorInvalidValue;
return cudaMemcpy(this->m_deviceMemory, this->m_hostMemory, length * sizeof(T), cudaMemcpyHostToDevice);
}
// Copies the entire array or a part of it from GPU memory to CPU memory.
// If length == 0 (default), the entire array is copied.
cudaError_t CopyToHost(size_t length = 0)
{
if (!this->m_deviceMemory)
return cudaErrorInvalidDevicePointer;
if (!this->m_hostMemory)
return cudaErrorInvalidHostPointer;
if (length == 0)
length = HostArray<T>::m_length;
else if (length > HostArray<T>::m_length)
return cudaErrorInvalidValue;
return cudaMemcpy(this->m_hostMemory, this->m_deviceMemory, length * sizeof(T), cudaMemcpyDeviceToHost);
}
size_t Length() const
{
return HostArray<T>::Length();
}
size_t Size() const
{
return HostArray<T>::Size();
}
DeviceAndHostArray<T>& operator=(DeviceAndHostArray<T>&& other) noexcept
{
DeviceArray<T>::operator=(other);
HostArray<T>::operator=(other);
return *this;
}
};
}