Repository navigation
Expand file tree
/
Copy pathdevice_buffer.cpp
More file actions
85 lines (70 loc) · 1.97 KB
/
Copy pathdevice_buffer.cpp
File metadata and controls
85 lines (70 loc) · 1.97 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
/*
* SPDX-FileCopyrightText: Copyright (c) 2026 NVIDIA CORPORATION & AFFILIATES. All rights reserved.
* SPDX-License-Identifier: Apache-2.0
*/
#include "foundation_pose_nvidia/device_buffer.hpp"
#include "foundation_pose_nvidia/exception.hpp"
#include <stdexcept>
#include <string>
#include <utility>
namespace foundation_pose_nvidia {
void checkCuda(cudaError_t status, const char* what) {
if (status != cudaSuccess) {
throw FoundationPoseError(std::string(what) + ": " + cudaGetErrorString(status));
}
}
DeviceBuffer::DeviceBuffer(DeviceBuffer&& other) noexcept
: ptr_(std::exchange(other.ptr_, nullptr)),
bytes_(std::exchange(other.bytes_, 0)) {}
DeviceBuffer& DeviceBuffer::operator=(DeviceBuffer&& other) noexcept {
if (this != &other) {
reset();
ptr_ = std::exchange(other.ptr_, nullptr);
bytes_ = std::exchange(other.bytes_, 0);
}
return *this;
}
DeviceBuffer::~DeviceBuffer() {
reset();
}
void DeviceBuffer::allocate(std::size_t bytes) {
if (bytes == bytes_ && ptr_ != nullptr) {
return;
}
reset();
if (bytes == 0) {
return;
}
checkCuda(cudaMalloc(&ptr_, bytes), "cudaMalloc");
bytes_ = bytes;
}
void DeviceBuffer::reset() {
if (ptr_ != nullptr) {
cudaFree(ptr_);
ptr_ = nullptr;
bytes_ = 0;
}
}
CudaStream::CudaStream(unsigned int flags) {
checkCuda(cudaStreamCreateWithFlags(&stream_, flags), "cudaStreamCreateWithFlags");
}
CudaStream::CudaStream(CudaStream&& other) noexcept
: stream_(std::exchange(other.stream_, nullptr)) {}
CudaStream& CudaStream::operator=(CudaStream&& other) noexcept {
if (this != &other) {
if (stream_ != nullptr) {
cudaStreamDestroy(stream_);
}
stream_ = std::exchange(other.stream_, nullptr);
}
return *this;
}
CudaStream::~CudaStream() {
if (stream_ != nullptr) {
cudaStreamDestroy(stream_);
}
}
void CudaStream::synchronize() const {
checkCuda(cudaStreamSynchronize(stream_), "cudaStreamSynchronize");
}
} // namespace foundation_pose_nvidia