HTGS  v2.0
The Hybrid Task Graph Scheduler
MemoryPool.hpp
Go to the documentation of this file.
1 
2 // NIST-developed software is provided by NIST as a public service. You may use, copy and distribute copies of the software in any medium, provided that you keep intact this entire notice. You may improve, modify and create derivative works of the software or any portion of the software, and you may copy and distribute such modifications or works. Modified works should carry a notice stating that you changed the software and should note the date and nature of any such change. Please explicitly acknowledge the National Institute of Standards and Technology as the source of the software.
3 // NIST-developed software is expressly provided "AS IS." NIST MAKES NO WARRANTY OF ANY KIND, EXPRESS, IMPLIED, IN FACT OR ARISING BY OPERATION OF LAW, INCLUDING, WITHOUT LIMITATION, THE IMPLIED WARRANTY OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE, NON-INFRINGEMENT AND DATA ACCURACY. NIST NEITHER REPRESENTS NOR WARRANTS THAT THE OPERATION OF THE SOFTWARE WILL BE UNINTERRUPTED OR ERROR-FREE, OR THAT ANY DEFECTS WILL BE CORRECTED. NIST DOES NOT WARRANT OR MAKE ANY REPRESENTATIONS REGARDING THE USE OF THE SOFTWARE OR THE RESULTS THEREOF, INCLUDING BUT NOT LIMITED TO THE CORRECTNESS, ACCURACY, RELIABILITY, OR USEFULNESS OF THE SOFTWARE.
4 // You are solely responsible for determining the appropriateness of using and distributing the software and you assume all risks associated with its use, including but not limited to the risks and costs of program errors, compliance with applicable laws, damage to or loss of data, programs or equipment, and the unavailability or interruption of operation. This software is not intended to be used in any situation where a failure could cause risk of injury or damage to property. The software developed by NIST employees is not subject to copyright protection within the United States.
5 
13 #ifndef HTGS_MEMORYPOOL_HPP
14 #define HTGS_MEMORYPOOL_HPP
15 
16 #include <list>
17 #include <iostream>
18 #include <memory>
19 #include <htgs/types/Types.hpp>
22 
23 namespace htgs {
24 
25 template<class V>
26 class MemoryData;
27 
38 template<class T>
39 class MemoryPool {
40  public:
47  this->allMemory = new std::list<m_data_t<T>>();
48  this->queueSize = queueSize;
49  }
50 
55  delete memoryQueue;
56  memoryQueue = nullptr;
57 
58  delete allMemory;
59  allMemory = nullptr;
60 
61  }
62 
67  for (m_data_t<T> mem : *allMemory) {
68  if (mem) {
69  mem->memFree();
70  }
71  }
72  }
73 
80  void fillPool(MemoryData<T> *memory, size_t pipelineId, bool allocate) const {
81  size_t remainingSize = this->memoryQueue->remainingCapacity();
82 
83  HTGS_DEBUG("Inserting " << remainingSize << " elements to memory pool");
84 
85  for (size_t i = 0; i < remainingSize; i++) {
86 
87  MemoryData<T> *newMemory = memory->copy();
88  HTGS_DEBUG_VERBOSE("Adding memory " << newMemory);
89 
90  newMemory->setPipelineId(pipelineId);
91 
92  // Allocates only if asked, used for dynamic and user mananaged memory
93  if (allocate)
94  newMemory->memAlloc();
95 
96  std::shared_ptr<MemoryData<T>> shrMem = std::shared_ptr<MemoryData<T>>(newMemory);
97  this->memoryQueue->Enqueue(shrMem);
98  this->allMemory->push_back(shrMem);
99  }
100  }
101 
106  MemoryPool<T> *copy() { return new MemoryPool<T>(this->queueSize); }
107 
114  bool isPoolEmpty() const { return this->memoryQueue->isEmpty(); }
115 
119  void emptyPool(bool free) const {
120  size_t poolSize = this->memoryQueue->size();
121  for (size_t i = 0; i < poolSize; i++) {
122  m_data_t<T> memory = this->memoryQueue->remove();
123  if (free)
124  memory->memFree();
125 
126  }
127  }
128 
134  return this->memoryQueue->Dequeue();
135  }
136 
141  void addMemory(m_data_t <T> o) const {
142  this->memoryQueue->Enqueue(o);
143  }
144 
145  private:
146  std::list<m_data_t <T>> *allMemory;
148  size_t queueSize;
149 
150 };
151 }
152 
153 #endif //HTGS_MEMORYPOOL_HPP
m_data_t< T > getMemory() const
Gets the next piece of memory from the MemoryPool.
Definition: MemoryPool.hpp:133
T remove()
Removes an element from the queue.
Definition: BlockingQueue.hpp:114
MemoryData< T > * copy()
Creates a copy of this MemoryData.
Definition: MemoryData.hpp:267
MemoryPool(size_t queueSize)
Creates a memory pool with the specified size number of elements.
Definition: MemoryPool.hpp:45
void memAlloc()
Allocates the memory that this MemoryData is using.
Definition: MemoryData.hpp:193
bool isEmpty()
Gets whether the queue is empty or not.
Definition: BlockingQueue.hpp:93
#define HTGS_DEBUG_VERBOSE(msg)
Prints a debug message to std:cerr with VERBOSE level.
Definition: debug_message.hpp:75
Describes memory allocated by a MemoryManager to manage shared memory across multiple ITask...
Definition: ICudaTask.hpp:28
size_t size()
Gets the number of elements in the queue.
Definition: BlockingQueue.hpp:101
Creates a thread-safe queue that will wait when no data is available and can block if the queue is fu...
Definition: BlockingQueue.hpp:32
Creates a pool of memory that allocates/frees MemoryData.
Definition: MemoryPool.hpp:39
size_t queueSize
The size of the memory queue.
Definition: MemoryPool.hpp:148
Implements a thread-safe BlockingQueue.
MemoryPool< T > * copy()
Creates a shallow copy of the MemoryPool.
Definition: MemoryPool.hpp:106
void Enqueue(T const &value)
Adds an element into the queue.
Definition: BlockingQueue.hpp:126
~MemoryPool()
Destructor, deallocates all memory allocated by the MemoryPool.
Definition: MemoryPool.hpp:54
void fillPool(MemoryData< T > *memory, size_t pipelineId, bool allocate) const
Fills the pool with memory and specifies the pipelineId to be associated with the MemoryData...
Definition: MemoryPool.hpp:80
#define HTGS_DEBUG(msg)
Prints a debug message to std::cerr with standard level If DEBUG_FLAG is not defined, this equates to a no op Each message includes the file and line number for where the debug is called.
Definition: debug_message.hpp:65
T Dequeue()
Removes an element from the queue.
Definition: BlockingQueue.hpp:162
std::list< m_data_t< T > > * allMemory
The list of all memory that has been allocated by the memory pool.
Definition: MemoryPool.hpp:146
Defines common types used throughout the HTGS API and some of which that are used by users of HTGS su...
size_t remainingCapacity()
Gets the remaining capacity of the queue based on the queueSize.
Definition: BlockingQueue.hpp:79
void emptyPool(bool free) const
Empties the memory pool releasing memory that had been allocated.
Definition: MemoryPool.hpp:119
void addMemory(m_data_t< T > o) const
Adds memory back into the MemoryPool.
Definition: MemoryPool.hpp:141
BlockingQueue< m_data_t< T > > * memoryQueue
A blocking queue for getting/recycling memory.
Definition: MemoryPool.hpp:147
bool isPoolEmpty() const
Gets whether the pool is empty or not.
Definition: MemoryPool.hpp:114
std::shared_ptr< MemoryData< V > > m_data_t
Defines a shared pointer to htgs::MemoryData.
Definition: Types.hpp:101
void releaseAllMemory()
Releases all memory associated with this memory pool.
Definition: MemoryPool.hpp:66
Definition: Bookkeeper.hpp:23
Provides functionality for debug messaging.
void setPipelineId(size_t id)
Sets the pipelineId associated with the MemoryManager that allocated the memory.
Definition: MemoryData.hpp:132