Skip to content

Commit

Permalink
Changes towards nanobind compatiblity that also work for pybind11
Browse files Browse the repository at this point in the history
  • Loading branch information
inducer committed Oct 2, 2022
1 parent 6b46bf3 commit 06a61b0
Show file tree
Hide file tree
Showing 9 changed files with 119 additions and 111 deletions.
2 changes: 1 addition & 1 deletion pyopencl/invoker.py
Original file line number Diff line number Diff line change
Expand Up @@ -263,7 +263,7 @@ def gen_arg_setting(in_enqueue):
["self", "queue", "global_size", "local_size"]
+ arg_names
+ ["global_offset=None",
"g_times_l=None",
"g_times_l=False",
"allow_empty_ndrange=False",
"wait_for=None"])))

Expand Down
2 changes: 1 addition & 1 deletion src/tools.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ namespace pyopencl
{
namespace py = pybind11;

py::module::import("gc").attr("collect")();
py::module_::import("gc").attr("collect")();
}


Expand Down
8 changes: 4 additions & 4 deletions src/wrap_cl.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -36,10 +36,10 @@ using namespace pyopencl;



extern void pyopencl_expose_constants(py::module &m);
extern void pyopencl_expose_part_1(py::module &m);
extern void pyopencl_expose_part_2(py::module &m);
extern void pyopencl_expose_mempool(py::module &m);
extern void pyopencl_expose_constants(py::module_ &m);
extern void pyopencl_expose_part_1(py::module_ &m);
extern void pyopencl_expose_part_2(py::module_ &m);
extern void pyopencl_expose_mempool(py::module_ &m);

static bool import_numpy_helper()
{
Expand Down
87 changes: 45 additions & 42 deletions src/wrap_cl.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -198,7 +198,7 @@
{ \
for (py::handle py_dev: py_devices) \
devices_vec.push_back( \
(py_dev).cast<device &>().data()); \
py::cast<device &>(py_dev).data()); \
num_devices = devices_vec.size(); \
devices = devices_vec.empty( ) ? nullptr : &devices_vec.front(); \
} \
Expand Down Expand Up @@ -402,7 +402,7 @@
{ \
for (py::handle evt: py_wait_for) \
{ \
event_wait_list.push_back(evt.cast<const event &>().data()); \
event_wait_list.push_back(py::cast<const event &>(evt).data()); \
++num_events_in_wait_list; \
} \
}
Expand Down Expand Up @@ -454,6 +454,7 @@ namespace pyopencl
class command_queue;

// {{{ error

class error : public std::runtime_error
{
private:
Expand Down Expand Up @@ -1278,25 +1279,25 @@ namespace pyopencl
{
for (py::handle prop_tuple_py: py_properties)
{
py::tuple prop_tuple(prop_tuple_py.cast<py::tuple>());
py::tuple prop_tuple(py::cast<py::tuple>(prop_tuple_py));

if (len(prop_tuple) != 2)
throw error("Context", CL_INVALID_VALUE, "property tuple must have length 2");
cl_context_properties prop = prop_tuple[0].cast<cl_context_properties>();
cl_context_properties prop = py::cast<cl_context_properties>(prop_tuple[0]);
props.push_back(prop);

if (prop == CL_CONTEXT_PLATFORM)
{
props.push_back(
reinterpret_cast<cl_context_properties>(
prop_tuple[1].cast<const platform &>().data()));
py::cast<const platform &>(prop_tuple[1]).data()));
}
#if defined(PYOPENCL_GL_SHARING_VERSION) && (PYOPENCL_GL_SHARING_VERSION >= 1)
#if defined(_WIN32)
else if (prop == CL_WGL_HDC_KHR)
{
// size_t is a stand-in for HANDLE, hopefully has the same size.
size_t hnd = (prop_tuple[1]).cast<size_t>();
size_t hnd = py::cast<size_t>(prop_tuple[1]);
props.push_back(hnd);
}
#endif
Expand All @@ -1311,10 +1312,10 @@ namespace pyopencl
#endif
)
{
py::object ctypes = py::module::import("ctypes");
py::object ctypes = py::module_::import("ctypes");
py::object prop = prop_tuple[1], c_void_p = ctypes.attr("c_void_p");
py::object ptr = ctypes.attr("cast")(prop, c_void_p);
props.push_back(ptr.attr("value").cast<cl_context_properties>());
props.push_back(py::cast<cl_context_properties>(ptr.attr("value")));
}
#endif
else
Expand Down Expand Up @@ -1350,7 +1351,7 @@ namespace pyopencl

std::vector<cl_device_id> devices;
for (py::handle py_dev: py_devices)
devices.push_back(py_dev.cast<const device &>().data());
devices.push_back(py::cast<const device &>(py_dev).data());

PYOPENCL_PRINT_CALL_TRACE("clCreateContext");
ctx = clCreateContext(
Expand All @@ -1364,7 +1365,7 @@ namespace pyopencl
{
cl_device_type dev_type = CL_DEVICE_TYPE_DEFAULT;
if (py_dev_type.ptr() != Py_None)
dev_type = py_dev_type.cast<cl_device_type>();
dev_type = py::cast<cl_device_type>(py_dev_type);

PYOPENCL_PRINT_CALL_TRACE("clCreateContextFromType");
ctx = clCreateContextFromType(props_ptr, dev_type, 0, 0, &status_code);
Expand Down Expand Up @@ -2029,8 +2030,7 @@ namespace pyopencl
std::vector<cl_event> event_wait_list(len(events));

for (py::handle evt: events)
event_wait_list[num_events_in_wait_list++] =
evt.cast<event &>().data();
event_wait_list[num_events_in_wait_list++] = py::cast<event &>(evt).data();

PYOPENCL_CALL_GUARDED_THREADED(clWaitForEvents, (
PYOPENCL_WAITLIST_ARGS));
Expand Down Expand Up @@ -2088,7 +2088,7 @@ namespace pyopencl
std::vector<cl_event> event_list(len(py_events));

for (py::handle py_evt: py_events)
event_list[num_events++] = py_evt.cast<event &>().data();
event_list[num_events++] = py::cast<event &>(py_evt).data();

PYOPENCL_CALL_GUARDED(clEnqueueWaitForEvents, (
cq.data(), num_events, event_list.empty( ) ? nullptr : &event_list.front()));
Expand Down Expand Up @@ -2195,12 +2195,15 @@ namespace pyopencl
}

memory_object(memory_object &src)
: m_valid(true), m_mem(src.m_mem),
m_hostbuf(std::move(src.m_hostbuf))
: m_valid(true), m_mem(src.m_mem)
{
PYOPENCL_CALL_GUARDED(clRetainMemObject, (m_mem));
}

memory_object(memory_object &&src)
: m_valid(true), m_mem(src.m_mem), m_hostbuf(std::move(src.m_hostbuf))
{ }

memory_object(memory_object_holder const &src)
: m_valid(true), m_mem(src.data())
{
Expand Down Expand Up @@ -2247,7 +2250,7 @@ namespace pyopencl

std::vector<cl_mem> mem_objects;
for (py::handle mo: py_mem_objects)
mem_objects.push_back(mo.cast<const memory_object &>().data());
mem_objects.push_back(py::cast<const memory_object &>(mo).data());

cl_event evt;
PYOPENCL_RETRY_IF_MEM_ERROR(
Expand Down Expand Up @@ -2938,16 +2941,16 @@ namespace pyopencl
cl_mem mem;
if (dims == 2)
{
size_t width = (shape[0]).cast<size_t>();
size_t height = (shape[1]).cast<size_t>();
size_t width = py::cast<size_t>(shape[0]);
size_t height = py::cast<size_t>(shape[1]);

size_t pitch = 0;
if (pitches.ptr() != Py_None)
{
if (py::len(pitches) != 1)
throw pyopencl::error("Image", CL_INVALID_VALUE,
"invalid length of pitch tuple");
pitch = (pitches[0]).cast<size_t>();
pitch = py::cast<size_t>(pitches[0]);
}

// check buffer size
Expand All @@ -2968,9 +2971,9 @@ namespace pyopencl
}
else if (dims == 3)
{
size_t width = (shape[0]).cast<size_t>();
size_t height = (shape[1]).cast<size_t>();
size_t depth = (shape[2]).cast<size_t>();
size_t width = py::cast<size_t>(shape[0]);
size_t height = py::cast<size_t>(shape[1]);
size_t depth = py::cast<size_t>(shape[2]);

size_t pitch_x = 0;
size_t pitch_y = 0;
Expand All @@ -2981,8 +2984,8 @@ namespace pyopencl
throw pyopencl::error("Image", CL_INVALID_VALUE,
"invalid length of pitch tuple");

pitch_x = (pitches[0]).cast<size_t>();
pitch_y = (pitches[1]).cast<size_t>();
pitch_x = py::cast<size_t>(pitches[0]);
pitch_y = py::cast<size_t>(pitches[1]);
}

// check buffer size
Expand Down Expand Up @@ -3851,7 +3854,7 @@ namespace pyopencl

if (!byte_count_py.is_none())
{
size_t byte_count = byte_count_py.cast<size_t>();
size_t byte_count = py::cast<size_t>(byte_count_py);
if (have_size && byte_count > size)
throw error("_enqueue_svm_memcpy", CL_INVALID_VALUE,
"specified byte_count larger than size of source or destination buffers");
Expand Down Expand Up @@ -4414,8 +4417,8 @@ namespace pyopencl
if (py::len(name_hdr_tup) != 2)
throw error("Program.compile", CL_INVALID_VALUE,
"epxected (name, header) tuple in headers list");
std::string name = (name_hdr_tup[0]).cast<std::string>();
program &prg = (name_hdr_tup[1]).cast<program &>();
std::string name = py::cast<std::string>(name_hdr_tup[0]);
program &prg = py::cast<program &>(name_hdr_tup[1]);

header_names.push_back(name);
programs.push_back(prg.data());
Expand Down Expand Up @@ -4497,8 +4500,7 @@ namespace pyopencl

for (size_t i = 0; i < num_devices; ++i)
{
devices.push_back(
(py_devices[i]).cast<device const &>().data());
devices.push_back(py::cast<device const &>(py_devices[i]).data());
const void *buf;
PYOPENCL_BUFFER_SIZE_T len;

Expand Down Expand Up @@ -4620,7 +4622,7 @@ namespace pyopencl
std::vector<cl_program> programs;
for (py::handle py_prg: py_programs)
{
program &prg = (py_prg).cast<program &>();
program &prg = py::cast<program &>(py_prg);
programs.push_back(prg.data());
}

Expand Down Expand Up @@ -4859,15 +4861,15 @@ namespace pyopencl
#if PYOPENCL_CL_VERSION >= 0x2000
try
{
set_arg_svm(arg_index, arg.cast<svm_pointer const &>());
set_arg_svm(arg_index, py::cast<svm_pointer const &>(arg));
return;
}
catch (py::cast_error &) { }
#endif

try
{
set_arg_mem(arg_index, arg.cast<memory_object_holder &>());
set_arg_mem(arg_index, py::cast<memory_object_holder &>(arg));
m_set_arg_prefer_svm = false;
return;
}
Expand All @@ -4877,15 +4879,15 @@ namespace pyopencl
{
try
{
set_arg_mem(arg_index, arg.cast<memory_object_holder &>());
set_arg_mem(arg_index, py::cast<memory_object_holder &>(arg));
return;
}
catch (py::cast_error &) { }

#if PYOPENCL_CL_VERSION >= 0x2000
try
{
set_arg_svm(arg_index, arg.cast<svm_pointer const &>());
set_arg_svm(arg_index, py::cast<svm_pointer const &>(arg));
m_set_arg_prefer_svm = true;
return;
}
Expand All @@ -4895,21 +4897,21 @@ namespace pyopencl

try
{
set_arg_local(arg_index, arg.cast<local_memory>());
set_arg_local(arg_index, py::cast<local_memory>(arg));
return;
}
catch (py::cast_error &) { }

try
{
set_arg_sampler(arg_index, arg.cast<const sampler &>());
set_arg_sampler(arg_index, py::cast<const sampler &>(arg));
return;
}
catch (py::cast_error &) { }

try
{
set_arg_command_queue(arg_index, arg.cast<const command_queue &>());
set_arg_command_queue(arg_index, py::cast<const command_queue &>(arg));
return;
}
catch (py::cast_error &) { }
Expand Down Expand Up @@ -5087,7 +5089,8 @@ namespace pyopencl
std::string msg( \
std::string("when processing arg#") + std::to_string(arg_index+1) \
+ std::string(" (1-based): ") + std::string(err.what())); \
auto mod_cl_ary(py::module::import("pyopencl.array")); \
\
auto mod_cl_ary(py::module_::import("pyopencl.array")); \
auto cls_array(mod_cl_ary.attr("Array")); \
if (arg_value.ptr() && py::isinstance(arg_value, cls_array)) \
msg.append( \
Expand Down Expand Up @@ -5454,7 +5457,7 @@ namespace pyopencl
\
std::vector<cl_mem> mem_objects; \
for (py::handle mo: py_mem_objects) \
mem_objects.push_back((mo).cast<memory_object_holder &>().data()); \
mem_objects.push_back(py::cast<memory_object_holder &>(mo).data()); \
\
cl_event evt; \
PYOPENCL_CALL_GUARDED(clEnqueue##What##GLObjects, ( \
Expand Down Expand Up @@ -5496,7 +5499,7 @@ namespace pyopencl
#if PYOPENCL_CL_VERSION >= 0x1020
if (py_platform.ptr() != Py_None)
{
platform &plat = (py_platform).cast<platform &>();
platform &plat = py::cast<platform &>(py_platform);

func_ptr = (func_ptr_type) clGetExtensionFunctionAddressForPlatform(
plat.data(), "clGetGLContextInfoKHR");
Expand Down Expand Up @@ -5687,7 +5690,7 @@ namespace pyopencl
py::object order_py)
{
memory_object_holder const &mem_obj =
(mem_obj_py).cast<memory_object_holder const &>();
py::cast<memory_object_holder const &>(mem_obj_py);
PyArray_Descr *tp_descr;
if (PyArray_DescrConverter(dtype.ptr(), &tp_descr) != NPY_SUCCEED)
throw py::error_already_set();
Expand All @@ -5707,7 +5710,7 @@ namespace pyopencl
catch (py::cast_error &)
{
for (auto it: shape)
dims.push_back(it.cast<npy_intp>());
dims.push_back(py::cast<npy_intp>(it));
}

NPY_ORDER order = PyArray_CORDER;
Expand Down
Loading

0 comments on commit 06a61b0

Please sign in to comment.