From c4c4e8775b35db39388cece940f15bb23648fc5a Mon Sep 17 00:00:00 2001 From: Andreas Kloeckner <inform@tiker.net> Date: Fri, 5 Oct 2012 13:46:07 -0500 Subject: [PATCH] Add mem pool tracing feature. --- pyopencl/array.py | 2 +- src/wrapper/mempool.hpp | 35 +++++++++++++++++++++++++++++++++-- src/wrapper/wrap_mempool.cpp | 8 +++++--- 3 files changed, 39 insertions(+), 6 deletions(-) diff --git a/pyopencl/array.py b/pyopencl/array.py index f3874c7d..88c37a2f 100644 --- a/pyopencl/array.py +++ b/pyopencl/array.py @@ -322,7 +322,7 @@ class Array(object): self.data = cl.Buffer(context, cl.mem_flags.READ_WRITE, nbytes) else: - self.data = self.allocator(self.size * self.dtype.itemsize) + self.data = self.allocator(nbytes) else: self.data = None else: diff --git a/src/wrapper/mempool.hpp b/src/wrapper/mempool.hpp index bbf23275..b40094ad 100644 --- a/src/wrapper/mempool.hpp +++ b/src/wrapper/mempool.hpp @@ -68,11 +68,13 @@ namespace PYGPU_PACKAGE unsigned m_active_blocks; bool m_stop_holding; + int m_trace; public: memory_pool(Allocator const &alloc=Allocator()) : m_allocator(alloc.copy()), - m_held_blocks(0), m_active_blocks(0), m_stop_holding(false) + m_held_blocks(0), m_active_blocks(0), m_stop_holding(false), + m_trace(false) { if (m_allocator->is_deferred()) { @@ -99,6 +101,14 @@ namespace PYGPU_PACKAGE return l << mantissa_bits | chopped; } + void set_trace(bool flag) + { + if (flag) + ++m_trace; + else + --m_trace; + } + static size_type alloc_size(bin_nr_t bin) { bin_nr_t exponent = bin >> mantissa_bits; @@ -158,12 +168,21 @@ namespace PYGPU_PACKAGE bin_t &bin = get_bin(bin_nr); if (bin.size()) + { + if (m_trace) + std::cout + << "[pool] allocation of size " << size << " served from bin " << bin_nr + << " which contained " << bin.size() << " entries" << std::endl; return pop_block_from_bin(bin, size); + } size_type alloc_sz = alloc_size(bin_nr); assert(bin_number(alloc_sz) == bin_nr); + if (m_trace) + std::cout << "[pool] allocation of size " << size << " required new memory" << std::endl; + try { return get_from_allocator(alloc_sz); } catch (PYGPU_PACKAGE::error &e) { @@ -171,10 +190,16 @@ namespace PYGPU_PACKAGE throw; } + if (m_trace) + std::cout << "[pool] allocation triggered OOM, running GC" << std::endl; + m_allocator->try_release_blocks(); if (bin.size()) return pop_block_from_bin(bin, size); + if (m_trace) + std::cout << "[pool] allocation still OOM after GC" << std::endl; + while (try_to_free_memory()) { try { return get_from_allocator(alloc_sz); } @@ -199,11 +224,17 @@ namespace PYGPU_PACKAGE void free(pointer_type p, size_type size) { --m_active_blocks; + bin_nr_t bin_nr = bin_number(size); if (!m_stop_holding) { inc_held_blocks(); - get_bin(bin_number(size)).push_back(p); + get_bin(bin_nr).push_back(p); + + 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 m_allocator->free(p); diff --git a/src/wrapper/wrap_mempool.cpp b/src/wrapper/wrap_mempool.cpp index 4eb6bd8d..73df3bd1 100644 --- a/src/wrapper/wrap_mempool.cpp +++ b/src/wrapper/wrap_mempool.cpp @@ -260,11 +260,11 @@ void pyopencl_expose_mempool() } { - typedef pyopencl::memory_pool<cl_allocator_base> cl; + typedef pyopencl::memory_pool<cl_allocator_base> cls; py::class_< - cl, boost::noncopyable, - boost::shared_ptr<cl> > wrapper("MemoryPool", + cls, boost::noncopyable, + boost::shared_ptr<cls> > wrapper("MemoryPool", py::init<cl_allocator_base const &>() ); wrapper @@ -272,6 +272,8 @@ void pyopencl_expose_mempool() py::return_value_policy<py::manage_new_object>()) .def("__call__", device_pool_allocate, py::return_value_policy<py::manage_new_object>()) + // undoc for now + .DEF_SIMPLE_METHOD(set_trace) ; expose_memory_pool(wrapper); -- GitLab