QDP++
qdp_pool_allocator.cc
Go to the documentation of this file.
1
4
5#include "qdp.h"
6#include "qdp_config.h"
9#include <vector>
10#include <map>
11#include <new>
12#include <cstdio>
13
14
15#undef DEBUG_POOL_ALLOCATOR
16
17namespace QDP {
18namespace Allocator {
19
20 QDPPoolAllocator::QDPPoolAllocator() : _PoolSize(0),
21 _MyMem(nullptr), _LargePool(nullptr) {}
22
23
24 QDPPoolAllocator::~QDPPoolAllocator() {
25 if ( _LargePool ) delete _LargePool;
26 if ( _MyMem ) {
27 delete [] _MyMem;
28 }
29 _LargePool = nullptr;
30 _MyMem = nullptr;
31 _PoolSize = 0;
32 }
33
34 void
35 QDPPoolAllocator::init(size_t PoolSizeInMB)
36 {
37
38 QDPIO::cout << "Initializing TBB Pool Allocator" << std::endl;
39 if( _MyMem != nullptr ) {
40 QDPIO::cout << "Allocator Already Inited. Aborting" << std::endl;
41 QDP_abort(1);
42 }
43 theAllocMap.clear();
44
45 QDPIO::cout << std::flush;
46 _PoolSize = PoolSizeInMB*1024*1024;
47
48 QDPIO::cout << "Intializing TBB Fixed Pool Allocator: Allocating " << PoolSizeInMB << " MB" << std::endl;
49
50 _MyMem = new (std::nothrow) unsigned char [ _PoolSize ];
51 if ( _MyMem == nullptr) {
52 QDPIO::cout << "Unable to allocate " << _PoolSize <<" bytes" << std::endl;
53 QDPIO::cout << "Aborting" <<std::endl;
54 QDP_abort(1);
55 }
56
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;
60 QDPIO::cout << "Aborting" <<std::endl;
61 QDP_abort(1);
62 }
63 }
64
65 void*
67 const MemoryPoolHint& mem_pool_hint=DEFAULT)
68 {
69 size_t BytesToAlloc;
70 BytesToAlloc = n_bytes;
71 BytesToAlloc += QDP_ALIGNMENT_SIZE;
72
73
74 unsigned char* Unaligned = (unsigned char *)(_LargePool->malloc(BytesToAlloc));
75
76 unsigned char* Aligned = (unsigned char *)
77 ( ( (unsigned long)Unaligned + (QDP_ALIGNMENT_SIZE-1) ) & ~(QDP_ALIGNMENT_SIZE - 1));
78
79#ifdef DEBUG_POOL_ALLOCATOR
80 QDPIO::cout << " Allocated: " << BytesToAlloc << " Bytes, Unaligend=" <<(unsigned long) Unaligned
81 << " Aligned=" << (unsigned long) Aligned << std::endl;
82#endif
83 // Insert into the map
84 auto r = theAllocMap.insert(std::make_pair(Aligned, PoolMemInfo{BytesToAlloc,Unaligned}));
85
86 // Check success of insertion.
87 if( ! r.second ) {
88 //QDPIO::cerr << "Failed to insert (unaligned,aligned) pair into map" << std::endl;
89 QDP_abort(1);
90 }
91
92 // Return the aligned pointer
93 return (void *)Aligned;
94 }
95
96 void QDPPoolAllocator::free(void *mem)
97 {
98 try {
99 // Look it up.
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;
104#endif
105
106 _LargePool->free(d.Unaligned);
107
108#ifdef DEBUG_POOL_ALLOCATOR
109 QDPIO::cout << "PoolAlloc::free: Erasing Addr from theAllocMap... ";
110#endif
111
112 // Delete _InUseMap entry
113 theAllocMap.erase((unsigned char *)mem);
114
115#ifdef DEBUG_POOL_ALLOCATOR
116 QDPIO::cout << " ... done" <<std::endl;
117#endif
118 }
119 catch(...) {
120 //QDPIO::cout << "QDPPoolAllocate::free threw exception" << std::endl;
121 QDP_abort(1);
122 }
123 }
124
125 void QDPPoolAllocator::pushFunc(const char * func,int line) {}
128 {
129 if ( Layout::primaryNode() ) {
130
131 QDPIO::cout << "Dumping memory map" << std::endl;
132 for( auto j = theAllocMap.begin(); j != theAllocMap.end(); j++) {
133 const unsigned long unaligned = (unsigned long)((*j).first);
134 const PoolMemInfo& mem = (*j).second;
135
136 printf("mem= 0x%lx unaligned= 0x%lx size=%d\n", (unsigned long)unaligned,
137 (unsigned long)mem.Unaligned, mem.Size);
138 }
139 }
140 }
141} // namespace Allocator
142} // namespace QDP
void * allocate(size_t n_bytes, const MemoryPoolHint &mem_pool_hint)
void pushFunc(const char *func, int line)
bool primaryNode()
Returns whether this is the primary node.
StandardOutputStream cout
Definition qdp_stdio.cc:21
Yet another random number generator.
void QDP_abort(int status)
Panic button.
Primary include file for QDP.
#define QDP_ALIGNMENT_SIZE
Definition qdp.h:82
Default memory allocator for QDP.
Default memory allocator for QDP.