40 #ifndef MAT_ALLOCATORMANAGER_HEADER 41 #define MAT_ALLOCATORMANAGER_HEADER 58 void init(
size_t noOfRealsPerBuffer_,
59 size_t noOfBuffers_) {
64 throw std::runtime_error(
"Error in AllocatorManager: " 65 "attempt to re-initialize with different parameters.");
67 if(noOfRealsPerBuffer_ <= 0 || noOfBuffers_ <= 0)
68 throw std::runtime_error(
"Error in AllocatorManager: bad input to init().");
76 pthread_mutex_lock(&
mutex);
78 typename std::list< Allocator<Treal>* >::iterator it =
list.begin();
79 while(it !=
list.end()) {
80 if(!(*it)->isFull()) {
82 Treal* ptr = (*it)->alloc();
83 pthread_mutex_unlock(&
mutex);
101 catch (
const std::bad_alloc & e) {
103 size_t totNoOfBytesAllocated =
list.size() * noOfBytesPerAllocator;
104 std::cerr <<
"Error in AllocatorManager::alloc(): std::bad_alloc exception caught. Usage before error: list.size() = " <<
list.size()
105 <<
" --> " << (double)totNoOfBytesAllocated/1000000000 <<
" GB used. peakListSize = " <<
peakListSize << std::endl;
106 pthread_mutex_unlock(&
mutex);
110 std::cerr <<
"Error in AllocatorManager::alloc(): exception caught (but not std::bad_alloc!?!)." << std::endl;
111 pthread_mutex_unlock(&
mutex);
112 throw std::runtime_error(
"Error in AllocatorManager::alloc(): exception caught (but not std::bad_alloc!?!).");
114 list.push_back(newAllocator);
117 Treal* ptr = newAllocator->
alloc();
118 pthread_mutex_unlock(&
mutex);
122 pthread_mutex_lock(&
mutex);
124 typename std::list< Allocator<Treal>* >::iterator it =
list.begin();
125 while(it !=
list.end()) {
126 if((*it)->ownsPtr(ptr)) {
129 if((*it)->isEmpty()) {
133 pthread_mutex_unlock(&
mutex);
139 pthread_mutex_unlock(&
mutex);
146 size_t totNoOfBytesAllocated =
list.size() * noOfBytesPerAllocator;
147 size_t peakNoOfBytesAllocated =
peakListSize * noOfBytesPerAllocator;
148 size_t totNoOfBytesUsed = 0;
150 typename std::list< Allocator<Treal>* >::iterator it =
list.begin();
151 while(it !=
list.end()) {
152 totNoOfBytesUsed += (size_t)((*it)->getNoOfOccupiedSlots()) *
noOfRealsPerBuffer *
sizeof(Treal);
155 std::stringstream ss;
156 ss <<
"AllocatorManager statistics: ";
157 ss << std::setprecision(3)
160 <<
" list.size(): " <<
list.size()
162 <<
"Allocated: " << (double)totNoOfBytesAllocated / 1e9 <<
" GB, " 163 <<
"Used: " << (
double)totNoOfBytesUsed / 1e9 <<
" GB, " 164 <<
"Peak alloc: " << (double)peakNoOfBytesAllocated/ 1e9 <<
" GB.";
169 pthread_mutex_init(&
mutex, NULL);
173 std::cerr <<
"Error in AllocatorManager destructor: not empty." << std::endl;
177 typename std::list< Allocator<Treal>* >::iterator it =
list.begin();
178 while(it !=
list.end()) {
183 std::list< Allocator<Treal>* >
list;
void init(size_t noOfRealsPerBuffer_, size_t noOfBuffers_)
Definition: AllocatorManager.h:58
AllocatorManager()
Definition: AllocatorManager.h:168
std::string getStatistics()
Definition: AllocatorManager.h:141
Treal * alloc()
Definition: Allocator.h:73
Definition: AllocatorManager.h:55
static AllocatorManager & instance()
pthread_mutex_t mutex
Definition: AllocatorManager.h:186
Code for Allocator class used for memory allocation/deallocation in matrix library.
Definition: allocate.cc:39
void free(Treal *ptr)
Definition: AllocatorManager.h:121
Definition: Allocator.h:51
std::list< Allocator< Treal > *> list
Definition: AllocatorManager.h:183
size_t noOfBuffers
Definition: AllocatorManager.h:185
~AllocatorManager()
Definition: AllocatorManager.h:171
size_t noOfRealsPerBuffer
Definition: AllocatorManager.h:184
Treal * alloc(size_t n)
Definition: AllocatorManager.h:73
size_t peakListSize
Definition: AllocatorManager.h:187