/usr/lib/python2.7/dist-packages/pyopencl/mempool.py is in python-pyopencl 2017.2.2-1.
This file is owned by root:root, with mode 0o644.
The actual contents of the file can be viewed below.
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 | from __future__ import division
from __future__ import absolute_import
import six
__copyright__ = """
Copyright (C) 2014 Andreas Kloeckner
"""
__license__ = """
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.
"""
import numpy as np
import pyopencl as cl
from pyopencl.tools import bitlog2
# {{{ allocators
class AllocatorBase(object):
def __call__(self, nbytes):
try_count = 0
while try_count < 2:
try:
return self.allocate(nbytes)
except cl.Error as e:
if not e.is_out_of_memory():
raise
try_count += 1
if try_count == 2:
raise
self.try_release_blocks()
def try_release_blocks(self):
import gc
gc.collect()
def free(self, buf):
buf.release()
class DeferredAllocator(AllocatorBase):
is_deferred = True
def __init__(self, context, mem_flags=cl.mem_flags.READ_WRITE):
self.context = context
self.mem_flags = mem_flags
def allocate(self, nbytes):
return cl.Buffer(self.context, self.mem_flags, nbytes)
_zero = np.array([0, 0, 0, 0], dtype=np.int8)
class ImmediateAllocator(AllocatorBase):
is_deferred = False
def __init__(self, queue, mem_flags=cl.mem_flags.READ_WRITE):
self.context = queue.context
self.queue = queue
self.mem_flags = mem_flags
def allocate(self, nbytes):
buf = cl.Buffer(self.context, self.mem_flags, nbytes)
# Make sure the buffer gets allocated right here and right now.
# This looks (and is) expensive. But immediate allocators
# have their main use in memory pools, whose basic assumption
# is that allocation is too expensive anyway--but they rely
# on exact 'out-of-memory' information.
from pyopencl.cffi_cl import _enqueue_write_buffer
_enqueue_write_buffer(
self.queue, buf,
_zero[:min(len(_zero), nbytes)],
is_blocking=False)
# No need to wait for completion here. clWaitForEvents (e.g.)
# cannot return mem object allocation failures. This implies that
# the buffer is faulted onto the device on enqueue.
return buf
# }}}
# {{{ memory pool
class MemoryPool(object):
mantissa_bits = 2
mantissa_mask = (1 << mantissa_bits) - 1
def __init__(self, allocator):
self.allocator = allocator
self.bin_nr_to_bin = {}
if self.allocator.is_deferred:
from warnings import warn
warn("Memory pools expect non-deferred "
"semantics from their allocators. You passed a deferred "
"allocator, i.e. an allocator whose allocations can turn out to "
"be unavailable long after allocation.", statcklevel=2)
self.active_blocks = 0
self.stop_holding_flag = False
@classmethod
def bin_number(cls, size):
bl2 = bitlog2(size)
mantissa_bits = cls.mantissa_bits
if bl2 >= mantissa_bits:
shifted = size >> (bl2 - mantissa_bits)
else:
shifted = size << (mantissa_bits - bl2)
assert not (size and (shifted & (1 << mantissa_bits)) == 0)
chopped = shifted & cls.mantissa_mask
return bl2 << mantissa_bits | chopped
@classmethod
def alloc_size(cls, bin_nr):
mantissa_bits = cls.mantissa_bits
exponent = bin_nr >> mantissa_bits
mantissa = bin_nr & cls.mantissa_mask
exp_minus_mbits = exponent-mantissa_bits
if exp_minus_mbits >= 0:
ones = (1 << exp_minus_mbits) - 1
head = ((1 << mantissa_bits) | mantissa) << exp_minus_mbits
else:
ones = 0
head = ((1 << mantissa_bits) | mantissa) >> -exp_minus_mbits
assert not (ones & head)
return head | ones
def stop_holding(self):
self.stop_holding_flag = True
self.free_held()
def free_held(self):
for bin_nr, bin_list in six.iteritems(self.bin_nr_to_bin):
while bin_list:
self.allocator.free(bin_list.pop())
@property
def held_blocks(self):
return sum(
len(bin_list)
for bin_list in six.itervalues(self.bin_nr_to_bin))
def allocate(self, size):
bin_nr = self.bin_number(size)
bin_list = self.bin_nr_to_bin.setdefault(bin_nr, [])
alloc_sz = self.alloc_size(bin_nr)
if bin_list:
# if (m_trace)
# std::cout
# << "[pool] allocation of size " << size
# << " served from bin " << bin_nr
# << " which contained " << bin_list.size()
# << " entries" << std::endl;
self.active_blocks += 1
return PooledBuffer(self, bin_list.pop(), alloc_sz)
assert self.bin_number(alloc_sz) == bin_nr
# if (m_trace)
# std::cout << "[pool] allocation of size " << size
# << " required new memory" << std::endl;
try:
result = self.allocator(alloc_sz)
self.active_blocks += 1
return PooledBuffer(self, result, alloc_sz)
except cl.MemoryError:
pass
# if (m_trace)
# std::cout << "[pool] allocation triggered OOM, running GC" << std::endl;
self.allocator.try_release_blocks()
if bin_list:
return bin_list.pop()
# if (m_trace)
# std::cout << "[pool] allocation still OOM after GC" << std::endl;
for _ in self._try_to_free_memory():
try:
result = self.allocator(alloc_sz)
self.active_blocks += 1
return PooledBuffer(self, result, alloc_sz)
except cl.MemoryError:
pass
raise cl.MemoryError(
"failed to free memory for allocation",
routine="memory_pool::allocate",
code=cl.status_code.MEM_OBJECT_ALLOCATION_FAILURE)
__call__ = allocate
def free(self, buf, size):
self.active_blocks -= 1
bin_nr = self.bin_number(size)
if not self.stop_holding_flag:
self.bin_nr_to_bin.setdefault(bin_nr, []).append(buf)
# if (m_trace)
# std::cout << "[pool] block of size " << size << " returned to bin "
# << bin_nr << " which now contains " << get_bin(bin_nr).size()
# << " entries" << std::endl;
else:
self.allocator.free(buf)
def _try_to_free_memory(self):
for bin_nr, bin_list in six.iteritems(self.bin_nr_to_bin):
while bin_list:
self.allocator.free(bin_list.pop())
yield
class PooledBuffer(cl.MemoryObjectHolder):
_id = 'buffer'
def __init__(self, pool, buf, alloc_sz):
self.pool = pool
self.buf = buf
self.ptr = buf.ptr
self._alloc_sz = alloc_sz
def release(self):
self.pool.free(self.buf, self._alloc_sz)
self.buf = None
self.ptr = None
def __del__(self):
if self.buf is not None:
self.release()
# }}}
# vim: foldmethod=marker
|