Skip to content

Commit

Permalink
Switch from pybind11 to nanobind
Browse files Browse the repository at this point in the history
  • Loading branch information
inducer committed Jun 13, 2022
1 parent aa11de8 commit 076dc15
Show file tree
Hide file tree
Showing 17 changed files with 604 additions and 517 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,7 @@ jobs:
CONDA_ENVIRONMENT=.test-conda-env.yml
grep -v ocl-icd .test-conda-env-py3.yml > $CONDA_ENVIRONMENT
curl -L -O https://gitlab.tiker.net/inducer/ci-support/raw/main/build-and-test-py-project-within-miniconda.sh
./configure.py --cxxflags= --ldflags= --cl-libname=OpenCL
./configure.py --cl-libname=OpenCL
. ./build-and-test-py-project-within-miniconda.sh
docs:
Expand Down
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
_skbuild

.pydevproject
.project
.settings
Expand Down
25 changes: 1 addition & 24 deletions .gitlab-ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -178,7 +178,7 @@ Python 3 Conda Apple:
grep -v ocl-icd .test-conda-env-py3.yml > $CONDA_ENVIRONMENT
export CC=gcc
curl -L -O https://gitlab.tiker.net/inducer/ci-support/raw/main/build-and-test-py-project-within-miniconda.sh
./configure.py --cxxflags= --ldflags= --cl-libname=OpenCL
./configure.py --cl-libname=OpenCL
. ./build-and-test-py-project-within-miniconda.sh
tags:
- apple
Expand All @@ -188,29 +188,6 @@ Python 3 Conda Apple:
reports:
junit: test/pytest.xml

PyPy3 POCL:
script:
- export PY_EXE=pypy3
- export PYOPENCL_TEST=portable:pthread

# On pypy, this seems to install old versions from the package index
# independently of whether newer ones are already present.
- rm -f pyproject.toml
- export EXTRA_INSTALL="pybind11 numpy mako"

- export NO_DOCTESTS=1
- curl -L -O https://gitlab.tiker.net/inducer/ci-support/raw/main/build-and-test-py-project.sh
- ". ./build-and-test-py-project.sh"

tags:
- pypy
- pocl
except:
- tags
artifacts:
reports:
junit: test/pytest.xml

Pylint:
script:
- EXTRA_INSTALL="pybind11 numpy mako matplotlib PyOpenGl IPython"
Expand Down
79 changes: 79 additions & 0 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,79 @@
project(pyopencl)

cmake_minimum_required(VERSION 3.17...3.22)

if (NOT SKBUILD)
message(FATAL_ERROR "This CMake file should be executed via scikit-build. "
"Please run\n$ pip install .")
endif()

# {{{ Constrain FindPython to find the Python version used by scikit-build

if (SKBUILD)
message(STATUS "Python_VERSION ${PYTHON_VERSION_STRING}")
message(STATUS "Python_EXECUTABLE ${PYTHON_EXECUTABLE}")
message(STATUS "Python_INCLUDE_DIR ${PYTHON_INCLUDE_DIR}")
message(STATUS "Python_LIBRARIES ${PYTHON_LIBRARY}")
set(Python_VERSION "${PYTHON_VERSION_STRING}")
set(Python_EXECUTABLE "${PYTHON_EXECUTABLE}")
set(Python_INCLUDE_DIR "${PYTHON_INCLUDE_DIR}")
set(Python_LIBRARIES "${PYTHON_LIBRARY}")
endif()
find_package(Python COMPONENTS Interpreter Development.Module REQUIRED)

# }}}

# {{{ Detect nanobind and import it

execute_process(
COMMAND
"${PYTHON_EXECUTABLE}" -c "import nanobind; print(nanobind.cmake_dir())"
OUTPUT_VARIABLE _tmp_dir
OUTPUT_STRIP_TRAILING_WHITESPACE COMMAND_ECHO STDOUT)
list(APPEND CMAKE_PREFIX_PATH "${_tmp_dir}")

# }}}

link_directories(${PYOPENCL_CL_LIB_DIRS})

find_package(nanobind CONFIG REQUIRED)
find_package(NumPy REQUIRED)
find_package(OpenCL REQUIRED)

include_directories(${OpenCL_INCLUDE_DIR} ${NumPy_INCLUDE_DIRS})

nanobind_add_module(
_cl
NB_STATIC # Build static libnanobind (the extension module itself remains a shared library)
src/wrap_constants.cpp
src/wrap_cl.cpp
src/wrap_cl_part_1.cpp
src/wrap_cl_part_2.cpp
src/wrap_mempool.cpp
src/bitlog.cpp
)

target_link_libraries(_cl PRIVATE ${OpenCL_LIBRARY})

target_compile_definitions(_cl
PRIVATE
PYGPU_PACKAGE=pyopencl
PYGPU_PYOPENCL
)

if (PYOPENCL_PRETEND_CL_VERSION)
target_compile_definitions(
_cl PRIVATE PYOPENCL_PRETEND_CL_VERSION=${PYOPENCL_PRETEND_CL_VERSION})
endif()

if (PYOPENCL_ENABLE_GL)
target_compile_definitions(_cl PRIVATE HAVE_GL=1)
endif()

if (PYOPENCL_USE_SHIPPED_EXT)
target_compile_definitions(_cl PRIVATE PYOPENCL_USE_SHIPPED_EXT=1)
endif()

install(TARGETS _cl LIBRARY DESTINATION .)

# vim: foldmethod=marker:sw=2
18 changes: 5 additions & 13 deletions pyopencl/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,8 @@

_PYPY = "__pypy__" in sys.builtin_module_names

from pyopencl._errors import Error, MemoryError, LogicError, RuntimeError # noqa: F401, E501

from pyopencl._cl import ( # noqa: F401
get_cl_header_version,
program_kind,
Expand Down Expand Up @@ -115,8 +117,6 @@
version_bits,
khronos_vendor_id,

Error, MemoryError, LogicError, RuntimeError,

Platform,
get_platforms,

Expand Down Expand Up @@ -558,7 +558,7 @@ def build(self, options=None, devices=None, cache_dir=None):
def _build_and_catch_errors(self, build_func, options_bytes, source=None):
try:
return build_func()
except _cl.RuntimeError as e:
except RuntimeError as e:
msg = str(e)
if options_bytes:
msg = msg + "\n(options: %s)" % options_bytes.decode("utf-8")
Expand All @@ -576,7 +576,7 @@ def _build_and_catch_errors(self, build_func, options_bytes, source=None):
code = e.code
routine = e.routine

err = _cl.RuntimeError(
err = RuntimeError(
_cl._ErrorRecord(
msg=msg,
code=code,
Expand Down Expand Up @@ -860,7 +860,7 @@ def kernel_set_arg_types(self, arg_types):
# }}}

from pyopencl.invoker import generate_enqueue_and_set_args
enqueue, my_set_args = \
self._enqueue, self.set_args = \
generate_enqueue_and_set_args(
self.function_name,
len(arg_types), self.num_args,
Expand All @@ -869,14 +869,6 @@ def kernel_set_arg_types(self, arg_types):
work_around_arg_count_bug=work_around_arg_count_bug,
devs=self.context.devices)

# Make ourselves a kernel-specific class, so that we're able to override
# __call__. Inspired by https://stackoverflow.com/a/38541437
class KernelWithCustomEnqueue(type(self)):
__call__ = enqueue
set_args = my_set_args

self.__class__ = KernelWithCustomEnqueue

def kernel_get_work_group_info(self, param, device):
cache_key = (param, device.int_ptr)
try:
Expand Down
37 changes: 37 additions & 0 deletions pyopencl/_errors.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
__copyright__ = "Copyright (C) 2022 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.
"""


class Error(Exception):
pass


class MemoryError(Error):
pass


class LogicError(Error):
pass


class RuntimeError(Error):
pass
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
15 changes: 12 additions & 3 deletions pyproject.toml
Original file line number Diff line number Diff line change
@@ -1,8 +1,17 @@
[build-system]
# Minimum requirements for the build system to execute.
requires = ["setuptools>=42.0.0", "wheel>=0.34.2", "Cython", "oldest-supported-numpy", "pybind11>=2.5.0"] # PEP 508 specifications.
build-backend = "setuptools.build_meta"
requires = [
"setuptools>=42",
"wheel>=0.34.2",
"scikit-build",
"cmake>=3.17",
"numpy",
"oldest-supported-numpy",

"nanobind>=0.0.5",

"ninja; platform_system!='Windows'"
]
build-backend = "setuptools.build_meta"

[tool.cibuildwheel]
test-command = "pytest {project}/test"
Expand Down
Loading

0 comments on commit 076dc15

Please sign in to comment.