From 00603c3ebda1d85b4b42f7b5839b543066842b86 Mon Sep 17 00:00:00 2001 From: Andreas Kloeckner Date: Wed, 25 Nov 2020 15:59:13 -0600 Subject: [PATCH 1/3] Add test for, try to fix PointerHolderBase --- src/cpp/cuda.hpp | 4 ++-- src/wrapper/wrap_cudadrv.cpp | 16 ++++++++++------ test/test_driver.py | 14 ++++++++++++++ 3 files changed, 26 insertions(+), 8 deletions(-) diff --git a/src/cpp/cuda.hpp b/src/cpp/cuda.hpp index 78f49a7a..e3f8ef24 100644 --- a/src/cpp/cuda.hpp +++ b/src/cpp/cuda.hpp @@ -1530,9 +1530,9 @@ namespace pycuda { public: virtual ~pointer_holder_base() { } - virtual CUdeviceptr get_pointer() = 0; + virtual CUdeviceptr get_pointer() const = 0; - operator CUdeviceptr() + operator CUdeviceptr() const { return get_pointer(); } py::object as_buffer(size_t size, size_t offset) diff --git a/src/wrapper/wrap_cudadrv.cpp b/src/wrapper/wrap_cudadrv.cpp index a27b836e..7c513a1a 100644 --- a/src/wrapper/wrap_cudadrv.cpp +++ b/src/wrapper/wrap_cudadrv.cpp @@ -109,7 +109,7 @@ namespace public py::wrapper { public: - CUdeviceptr get_pointer() + CUdeviceptr get_pointer() const { return this->get_override("get_pointer")(); } @@ -260,7 +260,7 @@ namespace py_buffer_wrapper buf_wrapper; buf_wrapper.get(dest.ptr(), PyBUF_ANY_CONTIGUOUS | PyBUF_WRITABLE); - CUDAPP_CALL_GUARDED_THREADED(cuMemcpyAtoH, + CUDAPP_CALL_GUARDED_THREADED(cuMemcpyAtoH, (buf_wrapper.m_buf.buf, ary.handle(), index, buf_wrapper.m_buf.len)); } @@ -1271,6 +1271,9 @@ BOOST_PYTHON_MODULE(_driver) .def("get_pointer", py::pure_virtual(&cl::get_pointer)) .def("as_buffer", &cl::as_buffer, (py::arg("size"), py::arg("offset")=0)) + .def("__int__", &cl::operator CUdeviceptr) + .def("__long__", mem_obj_to_long) + .def("__index__", mem_obj_to_long) ; py::implicitly_convertible(); @@ -1280,8 +1283,8 @@ BOOST_PYTHON_MODULE(_driver) typedef device_allocation cl; py::class_("DeviceAllocation", py::no_init) .def("__int__", &cl::operator CUdeviceptr) - .def("__long__", mem_obj_to_long) - .def("__index__", mem_obj_to_long) + .def("__long__", mem_obj_to_long) + .def("__index__", mem_obj_to_long) .def("as_buffer", &cl::as_buffer, (py::arg("size"), py::arg("offset")=0)) .DEF_SIMPLE_METHOD(free) @@ -1296,7 +1299,8 @@ BOOST_PYTHON_MODULE(_driver) py::class_("IPCMemoryHandle", py::init >()) .def("__int__", &cl::operator CUdeviceptr) - .def("__long__", mem_obj_to_long) + .def("__long__", mem_obj_to_long) + .def("__index__", mem_obj_to_long) .DEF_SIMPLE_METHOD(close) ; @@ -1352,7 +1356,7 @@ BOOST_PYTHON_MODULE(_driver) wrp .DEF_SIMPLE_METHOD(get_device_pointer) - .def("attach", &cl::attach, + .def("attach", &cl::attach, (py::arg("mem_flags"), py::arg("stream")=py::object())) ; } diff --git a/test/test_driver.py b/test/test_driver.py index 0c074efe..cf862826 100644 --- a/test/test_driver.py +++ b/test/test_driver.py @@ -1039,6 +1039,20 @@ class TestDriver: test_kernel(grid=(2, 1), block=(1, 1, 1)) +class WrappedAllocation(drv.PointerHolderBase): + def __init__(self, wrapped): + self.wrapped = wrapped + + def get_pointer(self): + return int(self.wrapped) + + +def test_pointer_holder_base(): + alloc = WrappedAllocation(pycuda.driver.mem_alloc(1024)) + gpuarray = pycuda.gpuarray.GPUArray((1024,), np.uint8, gpudata=alloc) + print(gpuarray.get()) + + def test_import_pyopencl_before_pycuda(): try: import pyopencl # noqa -- GitLab From 1feff4d175c35b0fc9a91db4d01add386d5f3e2f Mon Sep 17 00:00:00 2001 From: Andreas Kloeckner Date: Fri, 27 Nov 2020 12:03:16 -0600 Subject: [PATCH 2/3] Add superclass constructor call to WrappedAllocation in test_pointer_holder_base --- pycuda/compyte | 2 +- test/test_driver.py | 1 + 2 files changed, 2 insertions(+), 1 deletion(-) diff --git a/pycuda/compyte b/pycuda/compyte index d1f993da..fbfe788a 160000 --- a/pycuda/compyte +++ b/pycuda/compyte @@ -1 +1 @@ -Subproject commit d1f993daecc03947d9e6e3e60d2a5145ecbf3786 +Subproject commit fbfe788a2dcb190fd241fd42ad047e33bafd85b8 diff --git a/test/test_driver.py b/test/test_driver.py index cf862826..f9bd450a 100644 --- a/test/test_driver.py +++ b/test/test_driver.py @@ -1042,6 +1042,7 @@ class TestDriver: class WrappedAllocation(drv.PointerHolderBase): def __init__(self, wrapped): self.wrapped = wrapped + super().__init__() def get_pointer(self): return int(self.wrapped) -- GitLab From 3c738e62d92f23d1e9b071a44558837ef7ab004a Mon Sep 17 00:00:00 2001 From: Andreas Kloeckner Date: Fri, 27 Nov 2020 12:10:53 -0600 Subject: [PATCH 3/3] Fix test_pointer_holder_base --- test/test_driver.py | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/test/test_driver.py b/test/test_driver.py index f9bd450a..c720cebf 100644 --- a/test/test_driver.py +++ b/test/test_driver.py @@ -1049,9 +1049,9 @@ class WrappedAllocation(drv.PointerHolderBase): def test_pointer_holder_base(): - alloc = WrappedAllocation(pycuda.driver.mem_alloc(1024)) - gpuarray = pycuda.gpuarray.GPUArray((1024,), np.uint8, gpudata=alloc) - print(gpuarray.get()) + alloc = WrappedAllocation(drv.mem_alloc(1024)) + ary = gpuarray.GPUArray((1024,), np.uint8, gpudata=alloc) + print(ary.get()) def test_import_pyopencl_before_pycuda(): -- GitLab