20 #ifndef HEDGEHOG_ABSTRACT_MEMORY_MANAGER_H 21 #define HEDGEHOG_ABSTRACT_MEMORY_MANAGER_H 25 #include <condition_variable> 27 #include "../../tools/traits.h" 28 #include "../../tools/logger.h" 29 #include "../../tools/nvtx_profiler.h" 30 #include "../../behavior/memory_manager/pool.h" 34 #ifndef DOXYGEN_SHOULD_SKIP_THIS 35 template<
class ManagedMemory>
39 #endif //DOXYGEN_SHOULD_SKIP_THIS 159 template<
class ManagedMemory,
class =
void>
164 std::unique_ptr<behavior::Pool<ManagedMemory>>
pool_ = {};
179 auto tempCapacity = capacity > 0 ?
capacity : 1;
180 pool_ = std::make_unique<behavior::Pool<ManagedMemory>>(tempCapacity);
188 virtual std::shared_ptr<AbstractMemoryManager<ManagedMemory>>
copy() {
189 return std::make_shared<AbstractMemoryManager<ManagedMemory>>(this->
capacity());
204 std::lock_guard<std::mutex> lk(memoryManagerMutex_);
205 return this->
pool()->size();
211 std::shared_ptr<ManagedMemory> managedMemory;
212 HLOG(4,
"StaticMemoryManager memory pool size = " << this->
currentSize())
213 managedMemory = this->
pool()->pop_front();
215 "StaticMemoryManager After waiting: received: " << managedMemory <<
" pSize: " << (
int) (this->
pool()->size()))
216 return managedMemory;
224 std::lock_guard<std::mutex> lk(memoryManagerMutex_);
225 managedMemory->used();
226 if (managedMemory->canBeRecycled()) {
227 this->profiler_->addReleaseMarker();
228 managedMemory->recycle();
229 this->pool_->push_back(std::dynamic_pointer_cast<ManagedMemory>(managedMemory));
236 std::lock_guard<std::mutex> lk(memoryManagerMutex_);
240 this->
pool()->begin(), this->
pool()->end(),
241 [
this](std::shared_ptr<ManagedMemory> &emptyShared) {
242 emptyShared = std::make_shared<ManagedMemory>();
243 emptyShared->memoryManager(
this);
255 [[nodiscard]] std::unique_ptr<behavior::Pool<ManagedMemory>>
const &
pool()
const {
return pool_; }
259 [[nodiscard]]
size_t capacity()
const {
return this->
pool()->capacity(); };
285 template<
class ManagedMemory>
287 typename
std::enable_if_t<!traits::is_managed_memory_v<ManagedMemory>>> {
292 std::ostringstream oss;
293 oss <<
"Call Memory manager method: " << __FUNCTION__ <<
" without managed memory data.";
294 HLOG_SELF(0, oss.str())
295 throw (std::runtime_error(oss.str()));
301 std::ostringstream oss;
302 oss <<
"Call Memory manager method: " << __FUNCTION__ <<
" without managed memory data.";
303 HLOG_SELF(0, oss.str())
304 throw (std::runtime_error(oss.str()));
309 void profiler(
const std::shared_ptr<NvtxProfiler> &) {
310 std::ostringstream oss;
311 oss <<
"Call Memory manager method: " << __FUNCTION__ <<
" without managed memory data.";
312 HLOG_SELF(0, oss.str())
313 throw (std::runtime_error(oss.str()));
319 virtual std::shared_ptr<AbstractMemoryManager<ManagedMemory>>
copy() {
320 std::ostringstream oss;
321 oss <<
"Call Memory manager method: " << __FUNCTION__ <<
" without managed memory data.";
322 HLOG_SELF(0, oss.str())
323 throw (std::runtime_error(oss.str()));
327 #endif //HEDGEHOG_ABSTRACT_MEMORY_MANAGER_H void recycleMemory(std::shared_ptr< MemoryData< ManagedMemory >> managedMemory)
Recycle memory.
AbstractMemoryManager(size_t const &capacity)
Only used constructor.
virtual void initialize()
Initializer.
virtual ~AbstractMemoryManager()=default
Default destructor.
std::unique_ptr< behavior::Pool< ManagedMemory > > pool_
Inside pool to store the data.
std::shared_ptr< NvtxProfiler > profiler_
NVTX profiler instance to follow memory manager state.
Abstract interface for Hedgehog's Memory manager.
AbstractMemoryManager()=delete
Deleted Default constructor.
bool isInitialized() const
Initialized flag accessor.
size_t currentSize()
Return the current size of the inside pool.
virtual std::shared_ptr< AbstractMemoryManager< ManagedMemory > > copy()
Virtual copy method used for task duplication and execution pipeline.
Memory data interface to use a data type in a Memory manager (AbstractMemoryManager or StaticMemoryMa...
virtual void initializeMemoryManager()
User-definable initialization step for a memory manager.
void deviceId(int)
Device id setter.
void initialized()
Flag the memory manager has initialized.
size_t capacity() const
Capacity accessor.
virtual std::shared_ptr< AbstractMemoryManager< ManagedMemory > > copy()
Copy definition.
std::shared_ptr< ManagedMemory > getManagedMemory()
Get an available managed memory, block if none are available.
virtual void initialize()
Initialize the memory manager.
int deviceId() const
Device Id accessor.
void deviceId(int deviceId)
Device id setter.
void profiler(const std::shared_ptr< NvtxProfiler > &)
Profiler setter.
std::unique_ptr< behavior::Pool< ManagedMemory > > const & pool() const
Inside pool accessor.
std::mutex & memoryManagerMutex()
User api mutex accessor.
bool initialized_
Flag to determine if AbstractMemoryManager has been initialized.
void profiler(const std::shared_ptr< NvtxProfiler > &profiler)
NVTX profiler setter.
int deviceId_
Device Id of linked task.
std::mutex memoryManagerMutex_
Mutex for user interface.