27 #ifndef MAT_ALLOCATORMANAGER_HEADER
28 #define MAT_ALLOCATORMANAGER_HEADER
43 void init(
size_t noOfRealsPerBuffer_,
44 size_t noOfBuffers_) {
49 throw std::runtime_error(
"Error in AllocatorManager: "
50 "attempt to re-initialize with different parameters.");
52 if(noOfRealsPerBuffer_ <= 0 || noOfBuffers_ <= 0)
53 throw std::runtime_error(
"Error in AllocatorManager: bad input to init().");
61 pthread_mutex_lock(&
mutex);
63 typename std::list< Allocator<Treal>* >::iterator it =
list.begin();
64 while(it !=
list.end()) {
65 if(!(*it)->isFull()) {
67 Treal* ptr = (*it)->alloc();
68 pthread_mutex_unlock(&
mutex);
76 list.push_back(newAllocator);
79 Treal* ptr = newAllocator->
alloc();
80 pthread_mutex_unlock(&
mutex);
84 pthread_mutex_lock(&
mutex);
86 typename std::list< Allocator<Treal>* >::iterator it =
list.begin();
87 while(it !=
list.end()) {
88 if((*it)->ownsPtr(ptr)) {
91 if((*it)->isEmpty()) {
95 pthread_mutex_unlock(&
mutex);
101 pthread_mutex_unlock(&
mutex);
105 size_t totNoOfBytesAllocated =
list.size() * noOfBytesPerAllocator;
106 size_t peakNoOfBytesAllocated =
peakListSize * noOfBytesPerAllocator;
107 size_t totNoOfBytesUsed = 0;
109 typename std::list< Allocator<Treal>* >::iterator it =
list.begin();
110 while(it !=
list.end()) {
111 totNoOfBytesUsed += (size_t)((*it)->getNoOfOccupiedSlots()) *
noOfRealsPerBuffer *
sizeof(Treal);
114 std::stringstream ss;
115 ss <<
"AllocatorManager statistics: ";
116 ss << std::setprecision(3)
119 <<
" list.size(): " <<
list.size()
121 <<
"Allocated: " << (double)totNoOfBytesAllocated / 1e9 <<
" GB, "
122 <<
"Used: " << (
double)totNoOfBytesUsed / 1e9 <<
" GB, "
123 <<
"Peak alloc: " << (double)peakNoOfBytesAllocated/ 1e9 <<
" GB.";
128 pthread_mutex_init(&
mutex, NULL);
132 throw std::runtime_error(
"Error in AllocatorManager destructor: "
135 typename std::list< Allocator<Treal>* >::iterator it =
list.begin();
136 while(it !=
list.end()) {
141 std::list< Allocator<Treal>* >
list;
void init(size_t noOfRealsPerBuffer_, size_t noOfBuffers_)
Definition: AllocatorManager.h:43
AllocatorManager()
Definition: AllocatorManager.h:127
std::string getStatistics()
Definition: AllocatorManager.h:103
Treal * alloc()
Definition: Allocator.h:57
Definition: AllocatorManager.h:40
static AllocatorManager & instance()
pthread_mutex_t mutex
Definition: AllocatorManager.h:144
void free(Treal *ptr)
Definition: AllocatorManager.h:83
Definition: Allocator.h:35
std::list< Allocator< Treal > * > list
Definition: AllocatorManager.h:141
size_t noOfBuffers
Definition: AllocatorManager.h:143
~AllocatorManager()
Definition: AllocatorManager.h:130
size_t noOfRealsPerBuffer
Definition: AllocatorManager.h:142
Treal * alloc(size_t n)
Definition: AllocatorManager.h:58
size_t peakListSize
Definition: AllocatorManager.h:145