Newer
Older
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
// Abstract memory pool implementation
#ifndef _AFJDFJSDFSD_PYCUDA_HEADER_SEEN_MEMPOOL_HPP
#define _AFJDFJSDFSD_PYCUDA_HEADER_SEEN_MEMPOOL_HPP
#include <boost/ptr_container/ptr_map.hpp>
#include <boost/foreach.hpp>
#include <boost/format.hpp>
#include <bitlog.hpp>
namespace pycuda
{
template <class T>
inline T signed_left_shift(T x, signed shift_amount)
{
if (shift_amount < 0)
return x >> -shift_amount;
else
return x << shift_amount;
}
template <class T>
inline T signed_right_shift(T x, signed shift_amount)
{
if (shift_amount < 0)
return x << -shift_amount;
else
return x >> shift_amount;
}
template<class Allocator>
class memory_pool
{
public:
typedef typename Allocator::pointer_type pointer_type;
typedef typename Allocator::size_type size_type;
private:
typedef boost::uint32_t bin_nr_t;
typedef std::vector<pointer_type> bin_t;
typedef boost::ptr_map<bin_nr_t, bin_t > container_t;
container_t m_container;
typedef typename container_t::value_type bin_pair_t;
Allocator m_allocator;
// A held block is one that's been released by the application, but that
// we are keeping around to dish out again.
unsigned m_held_blocks;
// An active block is one that is in use by the application.
unsigned m_active_blocks;
bool m_stop_holding;
public:
memory_pool()
: m_held_blocks(0), m_active_blocks(0), m_stop_holding(false)
{
}
~memory_pool()
{ free_held(); }
static const unsigned mantissa_bits = 2;
static const unsigned mantissa_mask = (1 << mantissa_bits) - 1;
static bin_nr_t bin_number(size_type size)
{
signed l = bitlog2(size);
size_type shifted = signed_right_shift(size, l-signed(mantissa_bits));
if (size && (shifted & (1 << mantissa_bits)) == 0)
throw std::runtime_error("memory_pool::bin_number: bitlog2 fault");
size_type chopped = shifted & mantissa_mask;
return l << mantissa_bits | chopped;
}
static size_type alloc_size(bin_nr_t bin)
{
bin_nr_t exponent = bin >> mantissa_bits;
bin_nr_t mantissa = bin & mantissa_mask;
size_type ones = signed_left_shift(1,
signed(exponent)-signed(mantissa_bits)
);
if (ones) ones -= 1;
size_type head = signed_left_shift(
(1<<mantissa_bits) | mantissa,
signed(exponent)-signed(mantissa_bits));
if (ones & head)
throw std::runtime_error("memory_pool::alloc_size: bit-counting fault");
return head | ones;
}
protected:
bin_t &get_bin(bin_nr_t bin_nr)
{
typename container_t::iterator it = m_container.find(bin_nr);
if (it == m_container.end())
{
bin_t *new_bin = new bin_t;
m_container.insert(bin_nr, new_bin);
return *new_bin;
}
else
return *it->second;
}
void inc_held_blocks()
{
if (m_held_blocks == 0)
start_holding_blocks();
++m_held_blocks;
}
void dec_held_blocks()
{
--m_held_blocks;
if (m_held_blocks == 0)
stop_holding_blocks();
}
virtual void start_holding_blocks()
{ }
virtual void stop_holding_blocks()
{ }
public:
pointer_type allocate(size_type size)
{
bin_nr_t bin_nr = bin_number(size);
bin_t &bin = get_bin(bin_nr);
if (bin.size())
return pop_block_from_bin(bin, size);
size_type alloc_sz = alloc_size(bin_nr);
assert(bin_number(alloc_sz) == bin);
try { return get_from_allocator(alloc_sz); }
catch (cuda::error &e)
{
// Not OOM? Propagate.
if (e.code() != CUDA_ERROR_OUT_OF_MEMORY)
throw;
}
m_allocator.try_release_blocks();
if (bin.size())
return pop_block_from_bin(bin, size);
while (try_to_free_memory())
{
try { return get_from_allocator(alloc_sz); }
catch (cuda::error &e)
{
// Not OOM? Propagate.
if (e.code() != CUDA_ERROR_OUT_OF_MEMORY)
throw;
}
}
throw cuda::error(
"memory_pool::allocate",
CUDA_ERROR_OUT_OF_MEMORY,
"failed to free memory for allocation");
}
void free(pointer_type p, size_type size)
{
--m_active_blocks;
if (!m_stop_holding)
{
inc_held_blocks();
get_bin(bin_number(size)).push_back(p);
}
else
m_allocator.free(p);
}
void free_held()
{
BOOST_FOREACH(bin_pair_t bin_pair, m_container)
{
bin_t &bin = *bin_pair.second;
while (bin.size())
{
m_allocator.free(bin.back());
bin.pop_back();
dec_held_blocks();
}
}
assert(m_held_blocks == 0);
}
void stop_holding()
{
m_stop_holding = true;
free_held();
}
unsigned active_blocks()
{ return m_active_blocks; }
unsigned held_blocks()
{ return m_held_blocks; }
bool try_to_free_memory()
{
BOOST_FOREACH(bin_pair_t bin_pair,
// free largest stuff first
std::make_pair(m_container.rbegin(), m_container.rend()))
{
bin_t &bin = *bin_pair.second;
if (bin.size())
{
m_allocator.free(bin.back());
bin.pop_back();
dec_held_blocks();
return true;
}
}
return false;
}
private:
pointer_type get_from_allocator(size_type alloc_sz)
{
pointer_type result = m_allocator.allocate(alloc_sz);
++m_active_blocks;
return result;
}
pointer_type pop_block_from_bin(bin_t &bin, size_type size)
{
pointer_type result = bin.back();
bin.pop_back();
dec_held_blocks();
++m_active_blocks;
return result;
}
};
template <class Pool>
class pooled_allocation : public boost::noncopyable
{
public:
typedef Pool pool_type;
typedef typename Pool::pointer_type pointer_type;
typedef typename Pool::size_type size_type;
private:
boost::shared_ptr<pool_type> m_pool;
pointer_type m_ptr;
size_type m_size;
bool m_valid;
public:
pooled_allocation(boost::shared_ptr<pool_type> p, size_type size)
: m_pool(p), m_ptr(p->allocate(size)), m_size(size), m_valid(true)
{ }
~pooled_allocation()
{ free(); }
void free()
{
if (m_valid)
{
m_pool->free(m_ptr, m_size);
m_valid = false;
}
else
throw cuda::error("pooled_device_allocation::free", CUDA_ERROR_INVALID_HANDLE);
}
pointer_type ptr() const
{ return m_ptr; }
size_type size() const
{ return m_size; }
};
}
#endif