Skip to content

Commit 1f4f769

Browse files
committed
WIP: Towards SVM memory pool
1 parent 0dcd53e commit 1f4f769

5 files changed

Lines changed: 312 additions & 85 deletions

File tree

pyopencl/tools.py

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@
3333

3434
import numpy as np
3535
from pytools import memoize, memoize_method
36-
from pyopencl._cl import bitlog2 # noqa: F401
36+
from pyopencl._cl import bitlog2, get_cl_header_version # noqa: F401
3737
from pytools.persistent_dict import KeyBuilder as KeyBuilderBase
3838

3939
import re
@@ -59,10 +59,19 @@ def _register_types():
5959
# {{{ imported names
6060

6161
from pyopencl._cl import ( # noqa
62-
PooledBuffer as PooledBuffer,
62+
_tools_PooledBuffer as PooledBuffer,
6363
_tools_DeferredAllocator as DeferredAllocator,
6464
_tools_ImmediateAllocator as ImmediateAllocator,
65-
MemoryPool as MemoryPool)
65+
_tools_MemoryPool as MemoryPool,
66+
)
67+
68+
69+
if get_cl_header_version() >= (2, 0):
70+
from pyopencl._cl import ( # noqa
71+
_tools_SVMemoryPool as SVMemoryPool,
72+
_tools_PooledSVM as PooledSVM,
73+
_tools_SVMAllocator as SVMAllocator,
74+
)
6675

6776
# }}}
6877

src/mempool.hpp

Lines changed: 12 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -233,7 +233,8 @@ namespace PYGPU_PACKAGE
233233
std::cout
234234
<< "[pool] allocation of size " << size << " served from bin " << bin_nr
235235
<< " which contained " << bin.size() << " entries" << std::endl;
236-
return pop_block_from_bin(bin, size);
236+
return m_allocator->hand_out_existing_block(
237+
pop_block_from_bin(bin, size));
237238
}
238239

239240
size_type alloc_sz = alloc_size(bin_nr);
@@ -256,7 +257,8 @@ namespace PYGPU_PACKAGE
256257

257258
m_allocator->try_release_blocks();
258259
if (bin.size())
259-
return pop_block_from_bin(bin, size);
260+
return m_allocator->hand_out_existing_block(
261+
pop_block_from_bin(bin, size));
260262

261263
if (m_trace)
262264
std::cout << "[pool] allocation still OOM after GC" << std::endl;
@@ -282,7 +284,7 @@ namespace PYGPU_PACKAGE
282284
"failed to free memory for allocation");
283285
}
284286

285-
void free(pointer_type p, size_type size)
287+
void free(pointer_type &&p, size_type size)
286288
{
287289
--m_active_blocks;
288290
m_active_bytes -= size;
@@ -291,7 +293,7 @@ namespace PYGPU_PACKAGE
291293
if (!m_stop_holding)
292294
{
293295
inc_held_blocks();
294-
get_bin(bin_nr).push_back(p);
296+
get_bin(bin_nr).push_back(std::move(p));
295297

296298
if (m_trace)
297299
std::cout << "[pool] block of size " << size << " returned to bin "
@@ -300,7 +302,7 @@ namespace PYGPU_PACKAGE
300302
}
301303
else
302304
{
303-
m_allocator->free(p);
305+
m_allocator->free(std::move(p));
304306
m_managed_bytes -= alloc_size(bin_nr);
305307
}
306308
}
@@ -313,7 +315,7 @@ namespace PYGPU_PACKAGE
313315

314316
while (bin.size())
315317
{
316-
m_allocator->free(bin.back());
318+
m_allocator->free(std::move(bin.back()));
317319
m_managed_bytes -= alloc_size(bin_pair.first);
318320
bin.pop_back();
319321

@@ -353,7 +355,7 @@ namespace PYGPU_PACKAGE
353355

354356
if (bin.size())
355357
{
356-
m_allocator->free(bin.back());
358+
m_allocator->free(std::move(bin.back()));
357359
m_managed_bytes -= alloc_size(bin_pair.first);
358360
bin.pop_back();
359361

@@ -379,7 +381,7 @@ namespace PYGPU_PACKAGE
379381

380382
pointer_type pop_block_from_bin(bin_t &bin, size_type size)
381383
{
382-
pointer_type result = bin.back();
384+
pointer_type result(std::move(bin.back()));
383385
bin.pop_back();
384386

385387
dec_held_blocks();
@@ -399,7 +401,7 @@ namespace PYGPU_PACKAGE
399401
typedef typename Pool::pointer_type pointer_type;
400402
typedef typename Pool::size_type size_type;
401403

402-
private:
404+
protected:
403405
PYGPU_SHARED_PTR<pool_type> m_pool;
404406

405407
pointer_type m_ptr;
@@ -421,7 +423,7 @@ namespace PYGPU_PACKAGE
421423
{
422424
if (m_valid)
423425
{
424-
m_pool->free(m_ptr, m_size);
426+
m_pool->free(std::move(m_ptr), m_size);
425427
m_valid = false;
426428
}
427429
else
@@ -435,16 +437,8 @@ namespace PYGPU_PACKAGE
435437
#endif
436438
);
437439
}
438-
439-
pointer_type ptr() const
440-
{ return m_ptr; }
441-
442-
size_type size() const
443-
{ return m_size; }
444440
};
445441
}
446442

447443

448-
449-
450444
#endif

src/wrap_cl.hpp

Lines changed: 17 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1708,7 +1708,13 @@ namespace pyopencl
17081708
src.m_valid = false;
17091709
}
17101710

1711-
command_queue_ref(const command_queue_ref &) = delete;
1711+
command_queue_ref(const command_queue_ref &)
1712+
{
1713+
throw error("command_queue_ref", CL_INVALID_VALUE,
1714+
"command_queue_ref copy constructor is never supposed to be called; "
1715+
"all notional invocations should be eliminated because of NRVO");
1716+
}
1717+
17121718
command_queue_ref &operator=(const command_queue_ref &) = delete;
17131719

17141720
~command_queue_ref()
@@ -3545,7 +3551,7 @@ namespace pyopencl
35453551
class svm_pointer
35463552
{
35473553
public:
3548-
virtual void *ptr() const = 0;
3554+
virtual void *svm_ptr() const = 0;
35493555
// may throw size_not_available
35503556
virtual size_t size() const = 0;
35513557
};
@@ -3578,7 +3584,7 @@ namespace pyopencl
35783584
m_size = ward->m_buf.len;
35793585
}
35803586

3581-
void *ptr() const
3587+
void *svm_ptr() const
35823588
{
35833589
return m_ptr;
35843590
}
@@ -3674,7 +3680,7 @@ namespace pyopencl
36743680
m_allocation = nullptr;
36753681
}
36763682

3677-
void *ptr() const
3683+
void *svm_ptr() const
36783684
{
36793685
return m_allocation;
36803686
}
@@ -3701,7 +3707,7 @@ namespace pyopencl
37013707

37023708
void bind_to_queue(command_queue const &queue)
37033709
{
3704-
if (is_queue_out_of_order(m_queue.data()))
3710+
if (is_queue_out_of_order(queue.data()))
37053711
throw error("SVMAllocation.bind_to_queue", CL_INVALID_VALUE,
37063712
"supplying an out-of-order queue to SVMAllocation is invalid");
37073713

@@ -3794,7 +3800,7 @@ namespace pyopencl
37943800
(
37953801
cq.data(),
37963802
is_blocking,
3797-
dst.ptr(), src.ptr(),
3803+
dst.svm_ptr(), src.svm_ptr(),
37983804
size,
37993805
PYOPENCL_WAITLIST_ARGS,
38003806
&evt
@@ -3856,7 +3862,7 @@ namespace pyopencl
38563862
clEnqueueSVMMemFill,
38573863
(
38583864
cq.data(),
3859-
dst.ptr(), pattern_ptr,
3865+
dst.svm_ptr(), pattern_ptr,
38603866
pattern_len,
38613867
size,
38623868
PYOPENCL_WAITLIST_ARGS,
@@ -3913,7 +3919,7 @@ namespace pyopencl
39133919
cq.data(),
39143920
is_blocking,
39153921
flags,
3916-
svm.ptr(), size,
3922+
svm.svm_ptr(), size,
39173923
PYOPENCL_WAITLIST_ARGS,
39183924
&evt
39193925
));
@@ -3936,7 +3942,7 @@ namespace pyopencl
39363942
clEnqueueSVMUnmap,
39373943
(
39383944
cq.data(),
3939-
svm.ptr(),
3945+
svm.svm_ptr(),
39403946
PYOPENCL_WAITLIST_ARGS,
39413947
&evt
39423948
));
@@ -3964,7 +3970,7 @@ namespace pyopencl
39643970
{
39653971
svm_pointer &svm(py::cast<svm_pointer &>(py_svm));
39663972

3967-
svm_pointers.push_back(svm.ptr());
3973+
svm_pointers.push_back(svm.svm_ptr());
39683974
sizes.push_back(svm.size());
39693975
}
39703976

@@ -4760,7 +4766,7 @@ namespace pyopencl
47604766
void set_arg_svm(cl_uint arg_index, svm_pointer const &wrp)
47614767
{
47624768
PYOPENCL_CALL_GUARDED(clSetKernelArgSVMPointer,
4763-
(m_kernel, arg_index, wrp.ptr()));
4769+
(m_kernel, arg_index, wrp.svm_ptr()));
47644770
}
47654771
#endif
47664772

src/wrap_cl_part_2.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -298,7 +298,7 @@ void pyopencl_expose_part_2(py::module &m)
298298
{
299299
typedef svm_pointer cls;
300300
py::class_<cls>(m, "SVMPointer", py::dynamic_attr())
301-
.def("_ptr_as_int", [](cls &self) { return (intptr_t) self.ptr(); })
301+
.def("_ptr_as_int", [](cls &self) { return (intptr_t) self.svm_ptr(); })
302302
.def("_size", [](cls &self) -> py::object
303303
{
304304
try
@@ -336,7 +336,7 @@ void pyopencl_expose_part_2(py::module &m)
336336
"|std-enqueue-blurb|")
337337
.def(py::self == py::self)
338338
.def(py::self != py::self)
339-
.def("__hash__", [](cls &self) { return (intptr_t) self.ptr(); })
339+
.def("__hash__", [](cls &self) { return (intptr_t) self.svm_ptr(); })
340340
.DEF_SIMPLE_METHOD(bind_to_queue)
341341
.DEF_SIMPLE_METHOD(unbind_from_queue)
342342
;

0 commit comments

Comments
 (0)