ngscopeclient v0.2
Loading...
Searching...
No Matches
ScratchBufferManager.h
Go to the documentation of this file.
1/***********************************************************************************************************************
2* *
3* libscopehal *
4* *
5* Copyright (c) 2012-2026 Andrew D. Zonenberg and contributors *
6* All rights reserved. *
7* *
8* Redistribution and use in source and binary forms, with or without modification, are permitted provided that the *
9* following conditions are met: *
10* *
11* * Redistributions of source code must retain the above copyright notice, this list of conditions, and the *
12* following disclaimer. *
13* *
14* * Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the *
15* following disclaimer in the documentation and/or other materials provided with the distribution. *
16* *
17* * Neither the name of the author nor the names of any contributors may be used to endorse or promote products *
18* derived from this software without specific prior written permission. *
19* *
20* THIS SOFTWARE IS PROVIDED BY THE AUTHORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED *
21* TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL *
22* THE AUTHORS BE HELD LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES *
23* (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR *
24* BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT *
25* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE *
26* POSSIBILITY OF SUCH DAMAGE. *
27* *
28***********************************************************************************************************************/
29
36#ifndef ScratchBufferManager_h
37#define ScratchBufferManager_h
38
44{
45public:
46
48 // Pool IDs
49
50 enum PoolID_uint8
51 {
52 //Roughly one uint8_t per sample in the waveform, GPU resident
53 U8_GPU_WAVEFORM
54 };
55
56 enum PoolID_uint32
57 {
58 //Roughly one uint32_t per sample in the waveform, GPU resident
59 U32_GPU_WAVEFORM
60 };
61
62 enum PoolID_float32
63 {
64 //Roughly one float32 per sample in the waveform, GPU resident
65 F32_GPU_WAVEFORM
66 };
67
68 enum PoolID_int64
69 {
70 //Roughly one int64_t per sample in the waveform, GPU resident
71 I64_GPU_WAVEFORM,
72
73 //Small buffers (a few values per thread), GPU resident
74 I64_GPU_SMALL
75 };
76
78 // System stats
79
80 static size_t GetTotalSize();
81 static size_t GetPoolSize(PoolID_uint8 id);
82 static size_t GetPoolSize(PoolID_uint32 id);
83 static size_t GetPoolSize(PoolID_float32 id);
84 static size_t GetPoolSize(PoolID_int64 id);
85
87 // Memory pressure and cleanup
88
89 static void clear();
91
93 // The pools
94
95 static std::shared_ptr< AcceleratorBuffer<uint8_t> > Allocate(PoolID_uint8 pool);
96 static std::shared_ptr< AcceleratorBuffer<uint32_t> > Allocate(PoolID_uint32 pool);
97 static std::shared_ptr< AcceleratorBuffer<float> > Allocate(PoolID_float32 pool);
98 static std::shared_ptr< AcceleratorBuffer<int64_t> > Allocate(PoolID_int64 pool);
99
100 static void Free(std::shared_ptr< AcceleratorBuffer<uint8_t> >& p, PoolID_uint8 pool);
101 static void Free(std::shared_ptr< AcceleratorBuffer<uint32_t> >& p, PoolID_uint32 pool);
102 static void Free(std::shared_ptr< AcceleratorBuffer<float> >& p, PoolID_float32 pool);
103 static void Free(std::shared_ptr< AcceleratorBuffer<int64_t> >& p, PoolID_int64 pool);
104
105protected:
106
108 static std::mutex m_poolMutex;
109
111 static std::list< std::shared_ptr<AcceleratorBuffer<uint8_t> > > m_pool_u8_gpu_waveform;
112
114 static std::list< std::shared_ptr<AcceleratorBuffer<uint32_t> > > m_pool_u32_gpu_waveform;
115
117 static std::list< std::shared_ptr<AcceleratorBuffer<float> > > m_pool_f32_gpu_waveform;
118
120 static std::list< std::shared_ptr<AcceleratorBuffer<int64_t> > > m_pool_i64_gpu_waveform;
121
123 static std::list< std::shared_ptr<AcceleratorBuffer<int64_t> > > m_pool_i64_gpu_small;
124};
125
129template<class ID, class T>
131{
132public:
133
135 : m_ptr(nullptr)
136 , m_pool(static_cast<ID>(0))
137 {}
138
139 //move one scratchbuffer to another, disconnecting the original one
141 : m_ptr(rhs.m_ptr)
142 , m_pool(rhs.m_pool)
143 {
144 rhs.m_ptr = nullptr;
145 }
146
147 ScratchBuffer(ID id)
148 : m_ptr(ScratchBufferManager::Allocate(id))
149 , m_pool(id)
150 {}
151
152 virtual ~ScratchBuffer()
153 {
154 if(m_ptr != nullptr)
155 ScratchBufferManager::Free(m_ptr, m_pool);
156 }
157
158 //move one scratchbuffer to another, disconnecting the original one
159 auto& operator=(ScratchBuffer<ID, T>&& rhs)
160 {
161 m_ptr = rhs.m_ptr;
162 m_pool = rhs.m_pool;
163
164 rhs.m_ptr = nullptr;
165 return *this;
166 }
167
170 { return *m_ptr.get(); }
171
174 { return m_ptr.get(); }
175
176protected:
177
179 std::shared_ptr< AcceleratorBuffer<T> > m_ptr;
180
183};
184
185//needs to be after ScratchBufferManager declaration
186template<class T>
188{ m_usedScratchBuffers.emplace(std::make_unique<T>(std::move(buf))); }
189
194
195#endif
MemoryPressureLevel
Levels of memory pressure.
Definition AcceleratorBuffer.h:172
MemoryPressureType
Types of memory pressure.
Definition AcceleratorBuffer.h:189
Definition AcceleratorBuffer.h:204
std::set< std::unique_ptr< ScratchBufferBase > > m_usedScratchBuffers
Scratch buffers used by jobs in the queue.
Definition QueueHandle.h:126
void MarkScratchBufferUsed(T &buf)
Mark a scratch buffer as in use by command buffers pending for this queue.
Definition ScratchBufferManager.h:187
Definition QueueHandle.h:50
Memory pool for temporary working buffers.
Definition ScratchBufferManager.h:44
static bool OnMemoryPressure(MemoryPressureLevel level, MemoryPressureType type, size_t requestedSize)
Called when we run out of (probably) VRAM.
Definition ScratchBufferManager.cpp:174
static std::list< std::shared_ptr< AcceleratorBuffer< uint8_t > > > m_pool_u8_gpu_waveform
Pool for U8_GPU_WAVEFORM.
Definition ScratchBufferManager.h:111
static std::list< std::shared_ptr< AcceleratorBuffer< uint32_t > > > m_pool_u32_gpu_waveform
Pool for U32_GPU_WAVEFORM.
Definition ScratchBufferManager.h:114
static void clear()
Flush all cached buffers.
Definition ScratchBufferManager.cpp:161
static std::mutex m_poolMutex
Mutex for all pools.
Definition ScratchBufferManager.h:108
static size_t GetTotalSize()
Get the total number of bytes in scratch buffers.
Definition ScratchBufferManager.cpp:51
static std::list< std::shared_ptr< AcceleratorBuffer< int64_t > > > m_pool_i64_gpu_waveform
Pool for I64_GPU_WAVEFORM.
Definition ScratchBufferManager.h:120
static std::list< std::shared_ptr< AcceleratorBuffer< float > > > m_pool_f32_gpu_waveform
Pool for F32_GPU_WAVEFORM.
Definition ScratchBufferManager.h:117
static size_t GetPoolSize(PoolID_uint8 id)
Get the total number of bytes in a specific scratch buffer pool.
Definition ScratchBufferManager.cpp:65
static std::list< std::shared_ptr< AcceleratorBuffer< int64_t > > > m_pool_i64_gpu_small
Pool for I64_GPU_SMALL.
Definition ScratchBufferManager.h:123
RAII helper for allocations.
Definition ScratchBufferManager.h:131
AcceleratorBuffer< T > & operator*()
Get the underlying temporary.
Definition ScratchBufferManager.h:169
std::shared_ptr< AcceleratorBuffer< T > > m_ptr
The underlying buffer pointer.
Definition ScratchBufferManager.h:179
AcceleratorBuffer< T > * operator->()
Get the underlying temporary.
Definition ScratchBufferManager.h:173
ID m_pool
The pool we were allocated from.
Definition ScratchBufferManager.h:182