15#undef DEBUG_POOL_ALLOCATOR
20 QDPPoolAllocator::QDPPoolAllocator() : _PoolSize(0),
21 _MyMem(nullptr), _LargePool(nullptr) {}
24 QDPPoolAllocator::~QDPPoolAllocator() {
25 if ( _LargePool )
delete _LargePool;
38 QDPIO::cout <<
"Initializing TBB Pool Allocator" << std::endl;
39 if( _MyMem !=
nullptr ) {
40 QDPIO::cout <<
"Allocator Already Inited. Aborting" << std::endl;
46 _PoolSize = PoolSizeInMB*1024*1024;
48 QDPIO::cout <<
"Intializing TBB Fixed Pool Allocator: Allocating " << PoolSizeInMB <<
" MB" << std::endl;
50 _MyMem =
new (std::nothrow)
unsigned char [ _PoolSize ];
51 if ( _MyMem ==
nullptr) {
52 QDPIO::cout <<
"Unable to allocate " << _PoolSize <<
" bytes" << std::endl;
57 _LargePool =
new (std::nothrow ) tbb::fixed_pool((
void *)_MyMem, _PoolSize);
58 if ( _LargePool ==
nullptr) {
59 QDPIO::cout <<
"Unable to allocate fixed pool" << std::endl;
70 BytesToAlloc = n_bytes;
74 unsigned char* Unaligned = (
unsigned char *)(_LargePool->malloc(BytesToAlloc));
76 unsigned char* Aligned = (
unsigned char *)
79#ifdef DEBUG_POOL_ALLOCATOR
80 QDPIO::cout <<
" Allocated: " << BytesToAlloc <<
" Bytes, Unaligend=" <<(
unsigned long) Unaligned
81 <<
" Aligned=" << (
unsigned long) Aligned << std::endl;
84 auto r = theAllocMap.insert(std::make_pair(Aligned,
PoolMemInfo{BytesToAlloc,Unaligned}));
93 return (
void *)Aligned;
100 auto d = theAllocMap[(
unsigned char *)mem];
101#ifdef DEBUG_POOL_ALLOCATOR
102 QDPIO::cout <<
"PoolAlloc::free: Descriptor Found: Size="<< d.Size
103 <<
" Unaligned =" << std::hex <<(
unsigned long)d.Unaligned << std::endl;
106 _LargePool->free(d.Unaligned);
108#ifdef DEBUG_POOL_ALLOCATOR
109 QDPIO::cout <<
"PoolAlloc::free: Erasing Addr from theAllocMap... ";
113 theAllocMap.erase((
unsigned char *)mem);
115#ifdef DEBUG_POOL_ALLOCATOR
132 for(
auto j = theAllocMap.begin(); j != theAllocMap.end(); j++) {
133 const unsigned long unaligned = (
unsigned long)((*j).first);
136 printf(
"mem= 0x%lx unaligned= 0x%lx size=%d\n", (
unsigned long)unaligned,
void * allocate(size_t n_bytes, const MemoryPoolHint &mem_pool_hint)
void init(size_t PoolSizeInMB)
void pushFunc(const char *func, int line)
bool primaryNode()
Returns whether this is the primary node.
StandardOutputStream cout
Yet another random number generator.
void QDP_abort(int status)
Panic button.
Primary include file for QDP.
#define QDP_ALIGNMENT_SIZE
Default memory allocator for QDP.
Default memory allocator for QDP.
unsigned char * Unaligned