Compute Library
 19.08
PoolManager Class Reference

Memory pool manager. More...

#include <PoolManager.h>

Collaboration diagram for PoolManager:
[legend]

Public Member Functions

 PoolManager ()
 Default Constructor. More...
 
 PoolManager (const PoolManager &)=delete
 Prevent instances of this class to be copy constructed. More...
 
PoolManageroperator= (const PoolManager &)=delete
 Prevent instances of this class to be copied. More...
 
 PoolManager (PoolManager &&)=default
 Allow instances of this class to be move constructed. More...
 
PoolManageroperator= (PoolManager &&)=default
 Allow instances of this class to be moved. More...
 
IMemoryPoollock_pool () override
 Locks a pool for execution. More...
 
void unlock_pool (IMemoryPool *pool) override
 Releases memory pool. More...
 
void register_pool (std::unique_ptr< IMemoryPool > pool) override
 Register pool to be managed by the pool. More...
 
std::unique_ptr< IMemoryPoolrelease_pool () override
 Releases a free pool from the managed pools. More...
 
void clear_pools () override
 Clears all pools managed by the pool manager. More...
 
size_t num_pools () const override
 Returns the total number of pools managed by the pool manager. More...
 
- Public Member Functions inherited from IPoolManager
virtual ~IPoolManager ()=default
 Default virtual destructor. More...
 

Detailed Description

Memory pool manager.

Definition at line 41 of file PoolManager.h.

Constructor & Destructor Documentation

◆ PoolManager() [1/3]

Default Constructor.

Definition at line 34 of file PoolManager.cpp.

35  : _free_pools(), _occupied_pools(), _sem(), _mtx()
36 {
37 }

◆ PoolManager() [2/3]

PoolManager ( const PoolManager )
delete

Prevent instances of this class to be copy constructed.

◆ PoolManager() [3/3]

PoolManager ( PoolManager &&  )
default

Allow instances of this class to be move constructed.

Member Function Documentation

◆ clear_pools()

void clear_pools ( )
overridevirtual

Clears all pools managed by the pool manager.

Precondition
All pools must be unoccupied

Implements IPoolManager.

Definition at line 96 of file PoolManager.cpp.

97 {
98  std::lock_guard<arm_compute::Mutex> lock(_mtx);
99  ARM_COMPUTE_ERROR_ON_MSG(!_occupied_pools.empty(), "All pools should be free in order to clear the PoolManager!");
100  _free_pools.clear();
101 
102  // Update semaphore
103  _sem = nullptr;
104 }
#define ARM_COMPUTE_ERROR_ON_MSG(cond,...)
Definition: Error.h:328

References ARM_COMPUTE_ERROR_ON_MSG.

◆ lock_pool()

IMemoryPool * lock_pool ( )
overridevirtual

Locks a pool for execution.

Returns
Locked pool that workload will be mapped on

Implements IPoolManager.

Definition at line 39 of file PoolManager.cpp.

40 {
41  ARM_COMPUTE_ERROR_ON_MSG(_free_pools.empty() && _occupied_pools.empty(), "Haven't setup any pools!");
42 
43  _sem->wait();
44  std::lock_guard<arm_compute::Mutex> lock(_mtx);
45  ARM_COMPUTE_ERROR_ON_MSG(_free_pools.empty(), "Empty pool must exist as semaphore has been signalled");
46  _occupied_pools.splice(std::begin(_occupied_pools), _free_pools, std::begin(_free_pools));
47  return _occupied_pools.front().get();
48 }
#define ARM_COMPUTE_ERROR_ON_MSG(cond,...)
Definition: Error.h:328

References ARM_COMPUTE_ERROR_ON_MSG.

◆ num_pools()

size_t num_pools ( ) const
overridevirtual

Returns the total number of pools managed by the pool manager.

Returns
Number of managed pools

Implements IPoolManager.

Definition at line 106 of file PoolManager.cpp.

107 {
108  std::lock_guard<arm_compute::Mutex> lock(_mtx);
109 
110  return _free_pools.size() + _occupied_pools.size();
111 }

◆ operator=() [1/2]

PoolManager& operator= ( const PoolManager )
delete

Prevent instances of this class to be copied.

◆ operator=() [2/2]

PoolManager& operator= ( PoolManager &&  )
default

Allow instances of this class to be moved.

◆ register_pool()

void register_pool ( std::unique_ptr< IMemoryPool pool)
overridevirtual

Register pool to be managed by the pool.

Note
Ownership of the pools is being transferred to the pool manager
Parameters
[in]poolPool to be managed

Implements IPoolManager.

Definition at line 64 of file PoolManager.cpp.

65 {
66  std::lock_guard<arm_compute::Mutex> lock(_mtx);
67  ARM_COMPUTE_ERROR_ON_MSG(!_occupied_pools.empty(), "All pools should be free in order to register a new one!");
68 
69  // Set pool
70  _free_pools.push_front(std::move(pool));
71 
72  // Update semaphore
73  _sem = arm_compute::support::cpp14::make_unique<arm_compute::Semaphore>(_free_pools.size());
74 }
#define ARM_COMPUTE_ERROR_ON_MSG(cond,...)
Definition: Error.h:328

References ARM_COMPUTE_ERROR_ON_MSG.

◆ release_pool()

std::unique_ptr< IMemoryPool > release_pool ( )
overridevirtual

Releases a free pool from the managed pools.

Returns
The released pool in case a free pool existed else nullptr

Implements IPoolManager.

Definition at line 76 of file PoolManager.cpp.

77 {
78  std::lock_guard<arm_compute::Mutex> lock(_mtx);
79  ARM_COMPUTE_ERROR_ON_MSG(!_occupied_pools.empty(), "All pools should be free in order to release one!");
80 
81  if(!_free_pools.empty())
82  {
83  std::unique_ptr<IMemoryPool> pool = std::move(_free_pools.front());
84  ARM_COMPUTE_ERROR_ON(_free_pools.front() != nullptr);
85  _free_pools.pop_front();
86 
87  // Update semaphore
88  _sem = arm_compute::support::cpp14::make_unique<arm_compute::Semaphore>(_free_pools.size());
89 
90  return pool;
91  }
92 
93  return nullptr;
94 }
#define ARM_COMPUTE_ERROR_ON(cond)
If the condition is true then an error message is printed and an exception thrown.
Definition: Error.h:337
#define ARM_COMPUTE_ERROR_ON_MSG(cond,...)
Definition: Error.h:328

References ARM_COMPUTE_ERROR_ON, and ARM_COMPUTE_ERROR_ON_MSG.

◆ unlock_pool()

void unlock_pool ( IMemoryPool pool)
overridevirtual

Releases memory pool.

Parameters
[in]poolMemory pool to release

Implements IPoolManager.

Definition at line 50 of file PoolManager.cpp.

51 {
52  ARM_COMPUTE_ERROR_ON_MSG(_free_pools.empty() && _occupied_pools.empty(), "Haven't setup any pools!");
53 
54  std::lock_guard<arm_compute::Mutex> lock(_mtx);
55  auto it = std::find_if(std::begin(_occupied_pools), std::end(_occupied_pools), [pool](const std::unique_ptr<IMemoryPool> &pool_it)
56  {
57  return pool_it.get() == pool;
58  });
59  ARM_COMPUTE_ERROR_ON_MSG(it == std::end(_occupied_pools), "Pool to be unlocked couldn't be found!");
60  _free_pools.splice(std::begin(_free_pools), _occupied_pools, it);
61  _sem->signal();
62 }
#define ARM_COMPUTE_ERROR_ON_MSG(cond,...)
Definition: Error.h:328

References ARM_COMPUTE_ERROR_ON_MSG.


The documentation for this class was generated from the following files: