forked from apache/arrow
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcuda_memory.cc
More file actions
297 lines (242 loc) · 9.23 KB
/
Copy pathcuda_memory.cc
File metadata and controls
297 lines (242 loc) · 9.23 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
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
// Licensed to the Apache Software Foundation (ASF) under one
// or more contributor license agreements. See the NOTICE file
// distributed with this work for additional information
// regarding copyright ownership. The ASF licenses this file
// to you under the Apache License, Version 2.0 (the
// "License"); you may not use this file except in compliance
// with the License. You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing,
// software distributed under the License is distributed on an
// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
// KIND, either express or implied. See the License for the
// specific language governing permissions and limitations
// under the License.
#include "arrow/gpu/cuda_memory.h"
#include <algorithm>
#include <cstdint>
#include <cstdlib>
#include <memory>
#include <mutex>
#include <cuda.h>
#include "arrow/buffer.h"
#include "arrow/io/memory.h"
#include "arrow/status.h"
#include "arrow/util/logging.h"
#include "arrow/gpu/cuda_common.h"
#include "arrow/gpu/cuda_context.h"
namespace arrow {
namespace gpu {
// ----------------------------------------------------------------------
// CUDA IPC memory handle
struct CudaIpcMemHandle::CudaIpcMemHandleImpl {
explicit CudaIpcMemHandleImpl(const void* handle) {
memcpy(&ipc_handle, handle, sizeof(CUipcMemHandle));
}
CUipcMemHandle ipc_handle;
};
CudaIpcMemHandle::CudaIpcMemHandle(const void* handle) {
impl_.reset(new CudaIpcMemHandleImpl(handle));
}
CudaIpcMemHandle::~CudaIpcMemHandle() {}
Status CudaIpcMemHandle::FromBuffer(const void* opaque_handle,
std::unique_ptr<CudaIpcMemHandle>* handle) {
*handle = std::unique_ptr<CudaIpcMemHandle>(new CudaIpcMemHandle(opaque_handle));
return Status::OK();
}
Status CudaIpcMemHandle::Serialize(MemoryPool* pool, std::shared_ptr<Buffer>* out) const {
std::shared_ptr<Buffer> buffer;
constexpr size_t kHandleSize = sizeof(CUipcMemHandle);
RETURN_NOT_OK(AllocateBuffer(pool, static_cast<int64_t>(kHandleSize), &buffer));
memcpy(buffer->mutable_data(), &impl_->ipc_handle, kHandleSize);
*out = buffer;
return Status::OK();
}
const void* CudaIpcMemHandle::handle() const { return &impl_->ipc_handle; }
// ----------------------------------------------------------------------
CudaBuffer::CudaBuffer(uint8_t* data, int64_t size,
const std::shared_ptr<CudaContext>& context, bool own_data,
bool is_ipc)
: Buffer(data, size), context_(context), own_data_(own_data), is_ipc_(is_ipc) {
is_mutable_ = true;
mutable_data_ = data;
}
CudaBuffer::~CudaBuffer() { DCHECK(Close().ok()); }
Status CudaBuffer::Close() {
if (own_data_) {
if (is_ipc_) {
CU_RETURN_NOT_OK(cuIpcCloseMemHandle(reinterpret_cast<CUdeviceptr>(mutable_data_)));
} else {
return context_->Free(mutable_data_, size_);
}
}
return Status::OK();
}
CudaBuffer::CudaBuffer(const std::shared_ptr<CudaBuffer>& parent, const int64_t offset,
const int64_t size)
: Buffer(parent, offset, size),
context_(parent->context()),
own_data_(false),
is_ipc_(false) {}
Status CudaBuffer::CopyToHost(const int64_t position, const int64_t nbytes,
uint8_t* out) const {
return context_->CopyDeviceToHost(out, data_ + position, nbytes);
}
Status CudaBuffer::CopyFromHost(const int64_t position, const uint8_t* data,
int64_t nbytes) {
DCHECK_LE(nbytes, size_ - position) << "Copy would overflow buffer";
return context_->CopyHostToDevice(mutable_data_ + position, data, nbytes);
}
Status CudaBuffer::ExportForIpc(std::unique_ptr<CudaIpcMemHandle>* handle) {
if (is_ipc_) {
return Status::Invalid("Buffer has already been exported for IPC");
}
RETURN_NOT_OK(context_->ExportIpcBuffer(mutable_data_, handle));
own_data_ = false;
return Status::OK();
}
CudaHostBuffer::~CudaHostBuffer() {
CudaDeviceManager* manager = nullptr;
DCHECK(CudaDeviceManager::GetInstance(&manager).ok());
DCHECK(manager->FreeHost(mutable_data_, size_).ok());
}
// ----------------------------------------------------------------------
// CudaBufferReader
CudaBufferReader::CudaBufferReader(const std::shared_ptr<CudaBuffer>& buffer)
: io::BufferReader(buffer), cuda_buffer_(buffer), context_(buffer->context()) {}
CudaBufferReader::~CudaBufferReader() {}
Status CudaBufferReader::Read(int64_t nbytes, int64_t* bytes_read, uint8_t* buffer) {
nbytes = std::min(nbytes, size_ - position_);
*bytes_read = nbytes;
RETURN_NOT_OK(context_->CopyDeviceToHost(buffer, data_ + position_, nbytes));
position_ += nbytes;
return Status::OK();
}
Status CudaBufferReader::Read(int64_t nbytes, std::shared_ptr<Buffer>* out) {
int64_t size = std::min(nbytes, size_ - position_);
*out = std::make_shared<CudaBuffer>(cuda_buffer_, position_, size);
position_ += size;
return Status::OK();
}
// ----------------------------------------------------------------------
// CudaBufferWriter
class CudaBufferWriter::CudaBufferWriterImpl {
public:
explicit CudaBufferWriterImpl(const std::shared_ptr<CudaBuffer>& buffer)
: context_(buffer->context()),
buffer_(buffer),
buffer_size_(0),
buffer_position_(0) {
buffer_ = buffer;
DCHECK(buffer->is_mutable()) << "Must pass mutable buffer";
mutable_data_ = buffer->mutable_data();
size_ = buffer->size();
position_ = 0;
}
Status Seek(int64_t position) {
if (position < 0 || position >= size_) {
return Status::IOError("position out of bounds");
}
position_ = position;
return Status::OK();
}
Status Flush() {
if (buffer_size_ > 0 && buffer_position_ > 0) {
// Only need to flush when the write has been buffered
RETURN_NOT_OK(
context_->CopyHostToDevice(mutable_data_ + position_ - buffer_position_,
host_buffer_data_, buffer_position_));
buffer_position_ = 0;
}
return Status::OK();
}
Status Tell(int64_t* position) const {
*position = position_;
return Status::OK();
}
Status Write(const uint8_t* data, int64_t nbytes) {
if (nbytes == 0) {
return Status::OK();
}
if (buffer_size_ > 0) {
if (nbytes + buffer_position_ >= buffer_size_) {
// Reach end of buffer, write everything
RETURN_NOT_OK(Flush());
RETURN_NOT_OK(
context_->CopyHostToDevice(mutable_data_ + position_, data, nbytes));
} else {
// Write bytes to buffer
std::memcpy(host_buffer_data_ + buffer_position_, data, nbytes);
buffer_position_ += nbytes;
}
} else {
// Unbuffered write
RETURN_NOT_OK(context_->CopyHostToDevice(mutable_data_ + position_, data, nbytes));
}
position_ += nbytes;
return Status::OK();
}
Status WriteAt(int64_t position, const uint8_t* data, int64_t nbytes) {
std::lock_guard<std::mutex> guard(lock_);
RETURN_NOT_OK(Seek(position));
return Write(data, nbytes);
}
Status SetBufferSize(const int64_t buffer_size) {
if (buffer_position_ > 0) {
// Flush any buffered data
RETURN_NOT_OK(Flush());
}
RETURN_NOT_OK(AllocateCudaHostBuffer(buffer_size, &host_buffer_));
host_buffer_data_ = host_buffer_->mutable_data();
buffer_size_ = buffer_size;
return Status::OK();
}
int64_t buffer_size() const { return buffer_size_; }
int64_t buffer_position() const { return buffer_position_; }
private:
std::shared_ptr<CudaContext> context_;
std::shared_ptr<CudaBuffer> buffer_;
std::mutex lock_;
uint8_t* mutable_data_;
int64_t size_;
int64_t position_;
// Pinned host buffer for buffering writes on CPU before calling cudaMalloc
int64_t buffer_size_;
int64_t buffer_position_;
std::shared_ptr<CudaHostBuffer> host_buffer_;
uint8_t* host_buffer_data_;
};
CudaBufferWriter::CudaBufferWriter(const std::shared_ptr<CudaBuffer>& buffer) {
impl_.reset(new CudaBufferWriterImpl(buffer));
}
CudaBufferWriter::~CudaBufferWriter() {}
Status CudaBufferWriter::Close() { return Flush(); }
Status CudaBufferWriter::Flush() { return impl_->Flush(); }
Status CudaBufferWriter::Seek(int64_t position) {
if (impl_->buffer_position() > 0) {
RETURN_NOT_OK(Flush());
}
return impl_->Seek(position);
}
Status CudaBufferWriter::Tell(int64_t* position) const { return impl_->Tell(position); }
Status CudaBufferWriter::Write(const uint8_t* data, int64_t nbytes) {
return impl_->Write(data, nbytes);
}
Status CudaBufferWriter::WriteAt(int64_t position, const uint8_t* data, int64_t nbytes) {
return impl_->WriteAt(position, data, nbytes);
}
Status CudaBufferWriter::SetBufferSize(const int64_t buffer_size) {
return impl_->SetBufferSize(buffer_size);
}
int64_t CudaBufferWriter::buffer_size() const { return impl_->buffer_size(); }
int64_t CudaBufferWriter::num_bytes_buffered() const { return impl_->buffer_position(); }
// ----------------------------------------------------------------------
Status AllocateCudaHostBuffer(const int64_t size, std::shared_ptr<CudaHostBuffer>* out) {
CudaDeviceManager* manager = nullptr;
RETURN_NOT_OK(CudaDeviceManager::GetInstance(&manager));
return manager->AllocateHost(size, out);
}
} // namespace gpu
} // namespace arrow