Loading...
Searching...
No Matches
CudaVmmAllocator.hpp
Go to the documentation of this file.
1#pragma once
2
38
39#include <score_plugin_gfx_export.h>
40
41#include <cstddef>
42
43namespace score::gfx::interop
44{
45
47class SCORE_PLUGIN_GFX_EXPORT CudaVmmAllocation
48{
49public:
50 CudaVmmAllocation() noexcept = default;
51
52 CudaVmmAllocation(CudaVmmAllocation&& other) noexcept
53 : m_cu(other.m_cu)
54 , m_dptr(other.m_dptr)
55 , m_handle(other.m_handle)
56 , m_size(other.m_size)
57 , m_handleReleased(other.m_handleReleased)
58 {
59 other.detach();
60 }
61
62 CudaVmmAllocation& operator=(CudaVmmAllocation&& other) noexcept
63 {
64 if(this != &other)
65 {
66 destroy();
67 m_cu = other.m_cu;
68 m_dptr = other.m_dptr;
69 m_handle = other.m_handle;
70 m_size = other.m_size;
71 m_handleReleased = other.m_handleReleased;
72 other.detach();
73 }
74 return *this;
75 }
76
77 CudaVmmAllocation(const CudaVmmAllocation&) = delete;
78 CudaVmmAllocation& operator=(const CudaVmmAllocation&) = delete;
79
80 ~CudaVmmAllocation() { destroy(); }
81
83 bool valid() const noexcept { return m_cu && m_dptr != 0 && m_size > 0; }
84
87 CUdeviceptr devicePointer() const noexcept { return m_dptr; }
88
91 std::size_t size() const noexcept { return m_size; }
92
98 int exportPosixFd() const noexcept;
99
129 int exportDmaBufFd(bool pciBar1 = false) const noexcept;
130
135 void releaseHandleEarly() noexcept
136 {
137 if(m_cu && m_handle && !m_handleReleased)
138 {
139 m_cu->memRelease(m_handle);
140 m_handleReleased = true;
141 }
142 }
143
144private:
145 friend class CudaVmmAllocator;
146
147 void detach() noexcept
148 {
149 m_cu = nullptr;
150 m_dptr = 0;
151 m_handle = 0;
152 m_size = 0;
153 m_handleReleased = false;
154 }
155
156 void destroy() noexcept
157 {
158 if(!m_cu)
159 return;
160 if(m_dptr != 0 && m_size > 0)
161 {
162 m_cu->memUnmap(m_dptr, m_size);
163 m_cu->memAddressFree(m_dptr, m_size);
164 }
165 if(m_handle && !m_handleReleased)
166 m_cu->memRelease(m_handle);
167 detach();
168 }
169
171 CUdeviceptr m_dptr{};
172 CUmemGenericAllocationHandle m_handle{};
173 std::size_t m_size{};
174 bool m_handleReleased{};
175};
176
179class SCORE_PLUGIN_GFX_EXPORT CudaVmmAllocator
180{
181public:
199 static CudaVmmAllocation allocate(
200 score::gfx::CudaFunctions& cu, int device, std::size_t bytes,
201 bool exportable = false) noexcept;
202
206 static std::size_t alignedSize(
207 score::gfx::CudaFunctions& cu, int device, std::size_t bytes) noexcept;
208};
209
210} // namespace score::gfx::interop
Shared dlopen'd CUDA driver-API table for score-plugin-gfx.
Definition CudaVmmAllocator.hpp:48
CUdeviceptr devicePointer() const noexcept
Definition CudaVmmAllocator.hpp:87
bool valid() const noexcept
Definition CudaVmmAllocator.hpp:83
std::size_t size() const noexcept
Definition CudaVmmAllocator.hpp:91
Definition CudaVmmAllocator.hpp:180
Runtime-loaded CUDA driver-API table. Owns the libcuda handle.
Definition CudaFunctions.hpp:359