FZGPUModules 2.0
GPU-accelerated modular compression pipelines
Loading...
Searching...
No Matches
device_buffer.h
Go to the documentation of this file.
1
16#pragma once
17
18#include <cstddef>
19#include <utility>
20
21namespace fz {
22
24struct DeviceSpan {
25 void* data = nullptr;
26 size_t bytes = 0;
27
28 DeviceSpan() = default;
29 DeviceSpan(void* d, size_t n) : data(d), bytes(n) {}
30
31 bool empty() const { return bytes == 0; }
32};
33
36 const void* data = nullptr;
37 size_t bytes = 0;
38
39 ConstDeviceSpan() = default;
40 ConstDeviceSpan(const void* d, size_t n) : data(d), bytes(n) {}
41 ConstDeviceSpan(const DeviceSpan& s) : data(s.data), bytes(s.bytes) {}
42
43 bool empty() const { return bytes == 0; }
44};
45
55public:
56 BorrowedDeviceBuffer() = default;
57 BorrowedDeviceBuffer(void* d, size_t n) : data_(d), bytes_(n) {}
58
59 void* data() const { return data_; }
60 size_t bytes() const { return bytes_; }
61 bool empty() const { return bytes_ == 0; }
62
63 DeviceSpan span() const { return DeviceSpan(data_, bytes_); }
64 ConstDeviceSpan cspan() const { return ConstDeviceSpan(data_, bytes_); }
65
66private:
67 void* data_ = nullptr;
68 size_t bytes_ = 0;
69};
70
71namespace detail {
77void freeDeviceBuffer(void* ptr, int device) noexcept;
78} // namespace detail
79
85public:
86 OwnedDeviceBuffer() = default;
87 OwnedDeviceBuffer(void* d, size_t n, int device)
88 : data_(d), bytes_(n), device_(device) {}
89
90 OwnedDeviceBuffer(const OwnedDeviceBuffer&) = delete;
91 OwnedDeviceBuffer& operator=(const OwnedDeviceBuffer&) = delete;
92
93 OwnedDeviceBuffer(OwnedDeviceBuffer&& other) noexcept
94 : data_(other.data_), bytes_(other.bytes_), device_(other.device_) {
95 other.data_ = nullptr;
96 other.bytes_ = 0;
97 }
98
99 OwnedDeviceBuffer& operator=(OwnedDeviceBuffer&& other) noexcept {
100 if (this != &other) {
101 reset();
102 data_ = other.data_;
103 bytes_ = other.bytes_;
104 device_ = other.device_;
105 other.data_ = nullptr;
106 other.bytes_ = 0;
107 }
108 return *this;
109 }
110
112
113 void* data() const { return data_; }
114 size_t bytes() const { return bytes_; }
115 int device() const { return device_; }
116 bool empty() const { return bytes_ == 0; }
117 explicit operator bool() const { return data_ != nullptr; }
118
119 DeviceSpan span() const { return DeviceSpan(data_, bytes_); }
120 ConstDeviceSpan cspan() const { return ConstDeviceSpan(data_, bytes_); }
121
123 void* release() {
124 void* p = data_;
125 data_ = nullptr;
126 bytes_ = 0;
127 return p;
128 }
129
131 void reset() {
132 if (data_) detail::freeDeviceBuffer(data_, device_);
133 data_ = nullptr;
134 bytes_ = 0;
135 }
136
137private:
138 void* data_ = nullptr;
139 size_t bytes_ = 0;
140 int device_ = 0;
141};
142
143} // namespace fz
Definition device_buffer.h:54
Definition device_buffer.h:84
void * release()
Definition device_buffer.h:123
void reset()
Definition device_buffer.h:131
Definition dag.h:24
Definition device_buffer.h:35
Definition device_buffer.h:24