QDP++
qdp_default_allocator.cc
Go to the documentation of this file.
1
4
5
6#include "qdp.h"
8
9namespace QDP {
10namespace Allocator {
11
12 // The type returned on map insertion, allows me to check
13 // the insertion was successful.
14 typedef std::pair<MapT::iterator, bool> InsertRetVal;
15
16
20 void*
21 QDPDefaultAllocator::allocate(size_t n_bytes,const MemoryPoolHint& mem_pool_hint) {
22
24 unsigned char *unaligned;
25
27 unsigned char *aligned;
28
29 size_t bytes_to_alloc;
30 bytes_to_alloc = n_bytes;
31 if ( n_bytes % (32*1024) == 0 ) {
32 bytes_to_alloc += 0; // 2 lines bytes to kill cache aliasing
33 }
34 bytes_to_alloc += QDP_ALIGNMENT_SIZE;
35
36 // Try and allocate the memory
37 try {
38 unaligned = new unsigned char[ bytes_to_alloc ];
39 }
40 catch( std::bad_alloc ) {
41 QDPIO::cerr << "Unable to allocate memory in allocate()" << std::endl;
42 throw; // Re throw the bad alloc is the correct behaviour
43
44 }
45
46 // Work out the aligned pointer
47 aligned = (unsigned char *)( ( (unsigned long)unaligned + (QDP_ALIGNMENT_SIZE-1) ) & ~(QDP_ALIGNMENT_SIZE - 1));
48
49#if defined(QDP_DEBUG_MEMORY)
50 // Current location
51 FuncInfo_t& info = infostack.top();
52
53 // Insert into the map
54 InsertRetVal r = the_alignment_map.insert(
55 std::make_pair(aligned, MapVal(unaligned, info.func, info.line, bytes_to_alloc)));
56#else
57 // Insert into the map
58 InsertRetVal r = the_alignment_map.insert(std::make_pair(aligned, unaligned));
59#endif
60
61 // Check success of insertion.
62 if( ! r.second ) {
63 QDPIO::cerr << "Failed to insert (unaligned,aligned) pair into map" << std::endl;
64 QDP_abort(1);
65 }
66
67 // Return the aligned pointer
68 return (void *)aligned;
69 }
70
71
73 void
75 unsigned char* unaligned;
76
77 // Look up the original unaligned pointer in the memory.
78 MapT::iterator iter = the_alignment_map.find((unsigned char*)mem);
79 if( iter != the_alignment_map.end() )
80 {
81#if defined(QDP_DEBUG_MEMORY)
82 // Find the original unaligned pointer
83 unaligned = iter->second.unaligned;
84#else
85 // Find the original unaligned pointer
86 unaligned = iter->second;
87#endif
88
89 // Remove its entry from the map
90 the_alignment_map.erase(iter);
91
92 // Delete the actual unaligned pointer
93 delete [] unaligned;
94 }
95 else {
96 QDPIO::cerr << "Pointer not found in map" << std::endl;
97 QDP_abort(1);
98 }
99 }
100
101
102#if defined(QDP_DEBUG_MEMORY)
104 void
106 {
107 if ( Layout::primaryNode() )
108 {
109 size_t sum = 0;
110 typedef MapT::const_iterator CI;
111 QDPIO::cout << "Dumping memory map" << std::endl;
112 for( CI j = the_alignment_map.begin();
113 j != the_alignment_map.end(); j++)
114 {
115 sum += j->second.bytes;
116 printf("mem= 0x%x bytes= %d bytes/site= %d line= %d func= %s\n", j->first,
117 j->second.bytes, j->second.bytes/Layout::sitesOnNode(), j->second.line, j->second.func.c_str());
118 }
119 printf("total bytes= %d\n", sum);
120 }
121 }
122
123 // Setter
124 void
125 QDPDefaultAllocator::pushFunc(const char* func, int line)
126 {
127 infostack.push(FuncInfo_t(func,line));
128 }
129
130 // Nuker
131 void
133 {
134 if (infostack.empty())
135 {
136 QDPIO::cerr << __func__ << ": invalid pop" << std::endl;
137 QDP_abort(1);
138 }
139
140 infostack.pop();
141 }
142
143
144 static const char* nowhere = "nowhere";
145
146 // Init
147 void
148 QDPDefaultAllocator::init(size_t PoolSizeInMB)
149 {
150 infostack.push(FuncInfo_t(nowhere,0));
151 }
152
153#else
154
156 void
158 {
159 if ( Layout::primaryNode() )
160 {
161 typedef MapT::const_iterator CI;
162 QDPIO::cout << "Dumping memory map" << std::endl;
163 for( CI j = the_alignment_map.begin();
164 j != the_alignment_map.end(); j++)
165 {
166 printf("mem= 0x%lx unaligned= 0x%lx\n", (unsigned long)j->first,
167 (unsigned long)j->second);
168 }
169 }
170 }
171
172 // Setter
173 void
174 QDPDefaultAllocator::pushFunc(const char* func, int line) {}
175
176 // Nuker
177 void
179
180 // Init
181 void
182 QDPDefaultAllocator::init(size_t PoolSizeInMB ) {
183 QDPIO::cout << "Initializing QDPDefaultAllocator." << std::endl;
184
185 }
186
187#endif
188
189} // namespace Allocator
190} // namespace QDP
void free(void *mem)
Free an aligned pointer, which was allocated by us.
void pushFunc(const char *func, int line)
void * allocate(size_t n_bytes, const MemoryPoolHint &mem_pool_hint)
UnaryReturn< C, FnSum >::Type_t sum(const QDPType< T, C > &s1)
OScalar = sum(source).
std::pair< MapT::iterator, bool > InsertRetVal
int sitesOnNode()
Subgrid lattice volume.
bool primaryNode()
Returns whether this is the primary node.
StandardOutputStream cout
Definition qdp_stdio.cc:21
StandardOutputStream cerr
Definition qdp_stdio.cc:22
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.