Hedgehog  3.1.0
A library to generate hybrid pipeline workflow systems
Loading...
Searching...
No Matches
abstract_memory_manager.h
Go to the documentation of this file.
1// NIST-developed software is provided by NIST as a public service. You may use, copy and distribute copies of the
2// software in any medium, provided that you keep intact this entire notice. You may improve, modify and create
3// derivative works of the software or any portion of the software, and you may copy and distribute such modifications
4// or works. Modified works should carry a notice stating that you changed the software and should note the date and
5// nature of any such change. Please explicitly acknowledge the National Institute of Standards and Technology as the
6// source of the software. NIST-developed software is expressly provided "AS IS." NIST MAKES NO WARRANTY OF ANY KIND,
7// EXPRESS, IMPLIED, IN FACT OR ARISING BY OPERATION OF LAW, INCLUDING, WITHOUT LIMITATION, THE IMPLIED WARRANTY OF
8// MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE, NON-INFRINGEMENT AND DATA ACCURACY. NIST NEITHER REPRESENTS NOR
9// WARRANTS THAT THE OPERATION OF THE SOFTWARE WILL BE UNINTERRUPTED OR ERROR-FREE, OR THAT ANY DEFECTS WILL BE
10// CORRECTED. NIST DOES NOT WARRANT OR MAKE ANY REPRESENTATIONS REGARDING THE USE OF THE SOFTWARE OR THE RESULTS
11// THEREOF, INCLUDING BUT NOT LIMITED TO THE CORRECTNESS, ACCURACY, RELIABILITY, OR USEFULNESS OF THE SOFTWARE. You
12// are solely responsible for determining the appropriateness of using and distributing the software and you assume
13// all risks associated with its use, including but not limited to the risks and costs of program errors, compliance
14// with applicable laws, damage to or loss of data, programs or equipment, and the unavailability or interruption of
15// operation. This software is not intended to be used in any situation where a failure could cause risk of injury or
16// damage to property. The software developed by NIST employees is not subject to copyright protection within the
17// United States.
18
19#ifndef HEDGEHOG_ABSTRACT_MEMORY_MANAGER_H_
20#define HEDGEHOG_ABSTRACT_MEMORY_MANAGER_H_
21
22#pragma once
23
24#include <memory>
25#include <mutex>
26
27#include "../pool.h"
28#include "../../../tools/nvtx_profiler.h"
29
31namespace hh {
32
33#ifndef DOXYGEN_SHOULD_SKIP_THIS
34
36class ManagedMemory;
37#endif //DOXYGEN_SHOULD_SKIP_THIS
38
42 private:
43 int deviceId_ = 0;
44 bool initialized_ = false;
45 std::unique_ptr<tool::Pool<ManagedMemory>> pool_ = {};
46 std::shared_ptr<NvtxProfiler> profiler_ = nullptr;
47 protected:
48 std::mutex memoryManagerMutex_ = {};
49
50 public:
53 explicit AbstractMemoryManager(size_t const &capacity) :
54 deviceId_(0),
55 pool_(std::make_unique<tool::Pool<ManagedMemory>>(capacity > 0 ? capacity : 1)) {}
56
58 virtual ~AbstractMemoryManager() = default;
59
62 [[nodiscard]] int deviceId() const { return deviceId_; }
63
67 [[nodiscard]] size_t currentSize() {
69 auto s = this->pool()->size();
70 memoryManagerMutex_.unlock();
71 return s;
72 }
73
76 [[nodiscard]] size_t capacity() const {
77 return this->pool()->capacity();
78 };
79
82 void deviceId(int deviceId) { deviceId_ = deviceId; }
83
86 void profiler(const std::shared_ptr<NvtxProfiler> &profiler) { this->profiler_ = profiler; }
87
90 virtual std::shared_ptr<ManagedMemory> getManagedMemory() = 0;
91
96 virtual void recycleMemory(std::shared_ptr<ManagedMemory> const &managedMemory) = 0;
97
100 virtual std::shared_ptr<AbstractMemoryManager> copy() = 0;
101
104 virtual void initialize() = 0;
105
108 [[nodiscard]] virtual std::string managedType() const = 0;
109
110 protected:
113 [[nodiscard]] std::shared_ptr<NvtxProfiler> const &profiler() const {
114 return profiler_;
115 }
116
119 [[nodiscard]] std::unique_ptr<tool::Pool<ManagedMemory>> const &pool() const { return pool_; }
120
123 [[nodiscard]] bool isInitialized() const { return initialized_; }
124
127 [[nodiscard]] std::mutex &memoryManagerMutex() { return memoryManagerMutex_; }
128
130 void initialized() { initialized_ = true; }
131};
132}
133
134#endif //HEDGEHOG_ABSTRACT_MEMORY_MANAGER_H_
Hedgehog main namespace.
Abstraction used to manage an user type with a memory manager.
Abstract Memory manager.
std::shared_ptr< NvtxProfiler > profiler_
NVTX profiler instance to follow memory manager state.
std::mutex memoryManagerMutex_
Mutex for user interface.
int deviceId() const
Device Id accessor.
virtual void initialize()=0
Initialize the memory manager.
virtual std::shared_ptr< ManagedMemory > getManagedMemory()=0
Get an available managed memory, block if none are available.
std::shared_ptr< NvtxProfiler > const & profiler() const
Accessor to NVTX profiler.
AbstractMemoryManager(size_t const &capacity)
Only used constructor.
bool isInitialized() const
Initialized flag accessor.
std::mutex & memoryManagerMutex()
User api mutex accessor.
size_t capacity() const
Capacity accessor.
void profiler(const std::shared_ptr< NvtxProfiler > &profiler)
NVTX profiler setter.
virtual ~AbstractMemoryManager()=default
Default destructor.
void deviceId(int deviceId)
Device id setter.
virtual std::string managedType() const =0
Return the real managed type under the form of a string.
std::unique_ptr< tool::Pool< ManagedMemory > > pool_
Inside pool to store the data.
std::unique_ptr< tool::Pool< ManagedMemory > > const & pool() const
Inside pool accessor.
virtual std::shared_ptr< AbstractMemoryManager > copy()=0
Virtual copy method used for task duplication and execution pipeline.
size_t currentSize()
Return the current size of the inside pool.
int deviceId_
Device Id of linked task.
bool initialized_
Flag to determine if AbstractMemoryManager has been initialized.
void initialized()
Flag the memory manager has initialized.
virtual void recycleMemory(std::shared_ptr< ManagedMemory > const &managedMemory)=0
Recycle memory.