HTGS  v2.0
The Hybrid Task Graph Scheduler
MemoryData.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 
14 #ifndef HTGS_MEMORYDATA_HPP
15 #define HTGS_MEMORYDATA_HPP
16 
17 #include <stddef.h>
19 #include <htgs/types/MMType.hpp>
22 #include <htgs/api/IData.hpp>
24 
25 namespace htgs {
67 template<class T>
68 class MemoryData : public IData, public std::enable_shared_from_this<MemoryData<T>> {
69  public:
79  std::string memoryManagerName,
80  MMType type) {
81  this->type = type;
83  // TODO: Delete or Add #ifdef
84 // this->address = address;
85  this->memoryManagerName = memoryManagerName;
86  this->allocator = allocator;
87  if (allocator != nullptr)
88  this->size = allocator->size();
89  else
90  this->size = 0;
91  this->pipelineId = 0;
92  this->memoryReleaseRule = nullptr;
93  this->memory = nullptr;
94  }
95 
100  if (memoryReleaseRule) {
101  delete memoryReleaseRule;
102  memoryReleaseRule = nullptr;
103  }
104  }
105 
106  // TODO: Delete or Add #ifdef
107 // /**
108 // * Gets the address of the memory manager that allocated this memory data
109 // * @return the address of the memory manager that allocated the memory data
110 // */
111 // const std::string &getAddress() const {
112 // return address;
113 // }
114 
119  void releaseMemory() {
120  std::shared_ptr<MemoryData<T>> mPtr = std::enable_shared_from_this<MemoryData<T>>::shared_from_this();
121  std::shared_ptr<Connector<MemoryData<T>>> mConn = memoryManagerConnector.lock();
122  mConn->produceData(mPtr);
123  }
124 
132  void setPipelineId(size_t id) { this->pipelineId = id; }
133 
138  size_t getPipelineId() const { return this->pipelineId; }
139 
144  size_t getSize() const { return this->size; }
145 
151  if (memoryReleaseRule) {
152  delete memoryReleaseRule;
153  memoryReleaseRule = nullptr;
154  }
155  this->memoryReleaseRule = rule;
156  }
157 
163  return this->memoryReleaseRule;
164  }
165 
174  return this->memoryReleaseRule->canReleaseMemory();
175  }
176 
183  void memoryUsed() {
184  this->memoryReleaseRule->memoryUsed();
185  }
186 
193  void memAlloc() { this->memory = this->allocator->memAlloc(); }
194 
199  T *get() { return this->memory; }
200 
206  const T &get(size_t idx) const {
207  return *(this->memory + idx);
208  }
209 
215  T &get(size_t idx) {
216  return *(this->memory + idx);
217  }
218 
225  const T &operator[](size_t idx) const {
226  return *(this->memory + idx);
227  }
228 
235  T &operator[](size_t idx) {
236  return *(this->memory + idx);
237  }
238 
245  void memFree() {
246  if (this->memory) {
247  this->allocator->memFree(this->memory);
248  this->memory = nullptr;
249  }
250  }
251 
256  MMType getType() const {
257  return type;
258  }
259 
268  return new MemoryData<T>(this->allocator,
270  // TODO: Delete or Add #ifdef
271 // this->address,
272  this->memoryManagerName,
273  this->type);
274  }
275 
280  void memAlloc(size_t size) {
281  this->memory = this->allocator->memAlloc(size);
282  this->size = size;
283  }
284 
289  const std::string &getMemoryManagerName() const {
290  return memoryManagerName;
291  }
292 
293  private:
295  std::string memoryManagerName;
296  std::weak_ptr<Connector<MemoryData<T>>> memoryManagerConnector;
297  // TODO: Delete or Add #ifdef
298 // std::string address; //!< The address of the memory manager, used to release back
299  size_t pipelineId;
300  T *memory;
301  size_t size;
303  std::shared_ptr<IMemoryAllocator<T>> allocator;
304 };
305 }
306 
307 #endif //HTGS_MEMORY_HPP
std::shared_ptr< IMemoryAllocator< T > > allocator
The allocator associated with the memory.
Definition: MemoryData.hpp:303
const T & operator[](size_t idx) const
Gets the value for memory at index.
Definition: MemoryData.hpp:225
MemoryData< T > * copy()
Creates a copy of this MemoryData.
Definition: MemoryData.hpp:267
void memAlloc()
Allocates the memory that this MemoryData is using.
Definition: MemoryData.hpp:193
const std::string & getMemoryManagerName() const
Gets the memory manager name.
Definition: MemoryData.hpp:289
MMType
The memory manager types.
Definition: MMType.hpp:38
MMType type
The type of memory manager.
Definition: MemoryData.hpp:294
T & operator[](size_t idx)
Gets the value for memory at index.
Definition: MemoryData.hpp:235
size_t getPipelineId() const
Gets the pipelineId associated with the MemoryManager that allocated the memory.
Definition: MemoryData.hpp:138
IMemoryReleaseRule * memoryReleaseRule
The memory release rule associated with the memory.
Definition: MemoryData.hpp:302
Provides the Connector class for managing input/output of AbsData between Tasks.
void memoryUsed()
Updates the state of the memory when it is received by the MemoryManager.
Definition: MemoryData.hpp:183
size_t getSize() const
Gets the size of the memory that was allocated.
Definition: MemoryData.hpp:144
Describes memory allocated by a MemoryManager to manage shared memory across multiple ITask...
Definition: ICudaTask.hpp:28
Manages the input/output of IData between Tasks.
Definition: Connector.hpp:62
void memFree()
Frees the memory that this MemoryData is managing.
Definition: MemoryData.hpp:245
virtual bool canReleaseMemory()=0
Pure virtual function to indicate when memory can be released.
Implements the IData class, which is used for all data types entering/leaving a task graph...
Implements a thread-safe PriorityBlockingQueue.
Defines the Memory Manager types MMType.
Abstract class that describes when memory can be released/reused.
Definition: IMemoryReleaseRule.hpp:73
bool canReleaseMemory()
Checks whether the memory can be recycled/released by the MemoryManager.
Definition: MemoryData.hpp:173
void memAlloc(size_t size)
Allocates the memory that this memory data is managed with the specified size.
Definition: MemoryData.hpp:280
size_t pipelineId
The pipelineId associated with where this memory was managed.
Definition: MemoryData.hpp:299
Abstract class that describes how memory is allocated and freed.
Definition: IMemoryAllocator.hpp:67
MMType getType() const
Gets the type of memory that is associated with the memory manager.
Definition: MemoryData.hpp:256
std::weak_ptr< Connector< MemoryData< T > > > memoryManagerConnector
The pointer to the connector that owns this memory.
Definition: MemoryData.hpp:296
T * memory
The memory.
Definition: MemoryData.hpp:300
void releaseMemory()
Gets the address of the memory manager that allocated this memory data.
Definition: MemoryData.hpp:119
Describes how memory is released.
virtual void memoryUsed()=0
Pure virtual function to update the state of when memory has been used.
~MemoryData()
Destructor that releases IMemoryAllocator memory.
Definition: MemoryData.hpp:99
size_t size
The size of the memory (in elements)
Definition: MemoryData.hpp:301
MemoryData(std::shared_ptr< IMemoryAllocator< T >> allocator, std::weak_ptr< Connector< MemoryData< T >>> memoryManagerConnector, std::string memoryManagerName, MMType type)
Creates MemoryData with the specified IMemoryAllocator.
Definition: MemoryData.hpp:77
void setMemoryReleaseRule(IMemoryReleaseRule *rule)
Sets the memory release rule.
Definition: MemoryData.hpp:150
IMemoryReleaseRule * getMemoryReleaseRule() const
Gets the memory release rule associated with the memory data.
Definition: MemoryData.hpp:162
Definition: Bookkeeper.hpp:23
std::string memoryManagerName
The name of the memory manager that allocated the memory.
Definition: MemoryData.hpp:295
void setPipelineId(size_t id)
Sets the pipelineId associated with the MemoryManager that allocated the memory.
Definition: MemoryData.hpp:132
Defines how memory is allocated and freed.