-
Notifications
You must be signed in to change notification settings - Fork 966
Expand file tree
/
Copy pathcuda_allocator.cpp
More file actions
213 lines (180 loc) · 4.85 KB
/
cuda_allocator.cpp
File metadata and controls
213 lines (180 loc) · 4.85 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
/*
* Copyright (c) Meta Platforms, Inc. and affiliates.
* All rights reserved.
*
* This source code is licensed under the BSD-style license found in the
* LICENSE file in the root directory of this source tree.
*/
#include <executorch/backends/cuda/runtime/cuda_allocator.h>
#include <cuda_runtime.h>
#include <executorch/runtime/platform/log.h>
namespace executorch::backends::cuda {
using executorch::runtime::Error;
using executorch::runtime::Result;
using executorch::runtime::etensor::DeviceIndex;
using executorch::runtime::etensor::DeviceType;
Result<void*> CudaAllocator::allocate(size_t nbytes, DeviceIndex index) {
void* ptr = nullptr;
cudaError_t prev_device_err = cudaSuccess;
int prev_device = 0;
if (index >= 0) {
prev_device_err = cudaGetDevice(&prev_device);
if (prev_device_err == cudaSuccess) {
cudaSetDevice(index);
}
}
cudaError_t err = cudaMalloc(&ptr, nbytes);
if (index >= 0 && prev_device_err == cudaSuccess) {
cudaSetDevice(prev_device);
}
if (err != cudaSuccess) {
ET_LOG(
Error,
"cudaMalloc failed: %s (requested %zu bytes on device %d)",
cudaGetErrorString(err),
nbytes,
static_cast<int>(index));
return Error::MemoryAllocationFailed;
}
return ptr;
}
void CudaAllocator::deallocate(void* ptr, DeviceIndex index) {
if (ptr == nullptr) {
return;
}
int prev_device = 0;
cudaError_t prev_device_err = cudaSuccess;
if (index >= 0) {
prev_device_err = cudaGetDevice(&prev_device);
if (prev_device_err == cudaSuccess) {
cudaSetDevice(index);
}
}
cudaError_t err = cudaFree(ptr);
if (index >= 0 && prev_device_err == cudaSuccess) {
cudaSetDevice(prev_device);
}
if (err != cudaSuccess) {
ET_LOG(
Error,
"cudaFree failed: %s (ptr=%p, device %d)",
cudaGetErrorString(err),
ptr,
static_cast<int>(index));
}
}
Error CudaAllocator::copy_host_to_device(
void* dst,
const void* src,
size_t nbytes,
DeviceIndex index) {
int prev_device = 0;
cudaError_t prev_device_err = cudaSuccess;
if (index >= 0) {
prev_device_err = cudaGetDevice(&prev_device);
if (prev_device_err == cudaSuccess) {
cudaSetDevice(index);
}
}
cudaError_t err = cudaMemcpy(dst, src, nbytes, cudaMemcpyHostToDevice);
if (index >= 0 && prev_device_err == cudaSuccess) {
cudaSetDevice(prev_device);
}
if (err != cudaSuccess) {
ET_LOG(
Error,
"cudaMemcpy H2D failed: %s (%zu bytes, device %d)",
cudaGetErrorString(err),
nbytes,
static_cast<int>(index));
return Error::Internal;
}
return Error::Ok;
}
Error CudaAllocator::copy_device_to_host(
void* dst,
const void* src,
size_t nbytes,
DeviceIndex index) {
int prev_device = 0;
cudaError_t prev_device_err = cudaSuccess;
if (index >= 0) {
prev_device_err = cudaGetDevice(&prev_device);
if (prev_device_err == cudaSuccess) {
cudaSetDevice(index);
}
}
cudaError_t err = cudaMemcpy(dst, src, nbytes, cudaMemcpyDeviceToHost);
if (index >= 0 && prev_device_err == cudaSuccess) {
cudaSetDevice(prev_device);
}
if (err != cudaSuccess) {
ET_LOG(
Error,
"cudaMemcpy D2H failed: %s (%zu bytes, device %d)",
cudaGetErrorString(err),
nbytes,
static_cast<int>(index));
return Error::Internal;
}
return Error::Ok;
}
DeviceType CudaAllocator::device_type() const {
return DeviceType::CUDA;
}
CudaAllocator& CudaAllocator::instance() {
static CudaAllocator allocator;
return allocator;
}
Result<void*> CudaAllocator::allocate_async(
size_t nbytes,
DeviceIndex index,
cudaStream_t stream) {
void* ptr = nullptr;
cudaError_t err = cudaMallocAsync(&ptr, nbytes, stream);
if (err != cudaSuccess) {
ET_LOG(
Error,
"cudaMallocAsync failed: %s (requested %zu bytes on device %d)",
cudaGetErrorString(err),
nbytes,
static_cast<int>(index));
return Error::MemoryAllocationFailed;
}
return ptr;
}
void CudaAllocator::deallocate_async(
void* ptr,
DeviceIndex index,
cudaStream_t stream) {
if (ptr == nullptr) {
return;
}
cudaError_t err = cudaFreeAsync(ptr, stream);
if (err != cudaSuccess) {
ET_LOG(
Error,
"cudaFreeAsync failed: %s (ptr=%p, device %d)",
cudaGetErrorString(err),
ptr,
static_cast<int>(index));
}
}
Error CudaAllocator::memcpy_async(
void* dst,
const void* src,
size_t nbytes,
cudaMemcpyKind direction,
cudaStream_t stream) {
cudaError_t err = cudaMemcpyAsync(dst, src, nbytes, direction, stream);
if (err != cudaSuccess) {
ET_LOG(
Error,
"cudaMemcpyAsync failed: %s (%zu bytes)",
cudaGetErrorString(err),
nbytes);
return Error::Internal;
}
return Error::Ok;
}
} // namespace executorch::backends::cuda