From b93a6d1de5f2706a8e20c38076fe9f1e161a6a6c Mon Sep 17 00:00:00 2001 From: Nikita Grigorian Date: Sat, 5 Dec 2026 00:06:49 -0800 Subject: [PATCH] add sycl::platform pybind11 type caster Exports SyclPlatform through the Cython C-API with SyclPlatform_GetPlatformRef and SyclPlatform_Make, and uses them in dpctl4pybind11.hpp. --- CHANGELOG.md | 1 + docs/doc_sources/api_reference/dpctl_capi.rst | 20 +++++++ dpctl/_sycl_platform.pxd | 10 +++- dpctl/_sycl_platform.pyx | 17 ++++++ dpctl/apis/include/dpctl4pybind11.hpp | 55 ++++++++++++++++++- dpctl/apis/include/dpctl_capi.h | 8 ++- dpctl/tests/test_sycl_platform.py | 41 ++++++++++++++ .../tests/test_queue_device.py | 22 ++++++++ .../use_queue_device/__init__.py | 7 ++- .../use_queue_device/_example.cpp | 14 +++++ 10 files changed, 189 insertions(+), 6 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index bd7698d890..843f08ae5b 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -20,6 +20,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0 * Added kernel bundles in `input`, `object` and `executable` states for the kernels compiled into the application: `dpctl.compiler.get_kernel_bundle`, `has_kernel_bundle`, `compile`, `link`, `build` and `join`, the `dpctl.compiler.SyclInputKernelBundle`, `SyclObjectKernelBundle`, `SyclKernelId` and `SyclDeviceImage` classes, `get_kernel_ids` and `is_compatible`, and new `dpctl.compiler.SyclKernelBundle` queries (`state`, `backend`, `sycl_context`, `get_devices`, `is_empty`, `get_kernel_ids`, `has_kernel`, `get_kernel`, `get_device_images`, `contains_specialization_constants`, `native_specialization_constant`), with the matching `DPCTLKernelBundle_*`, `DPCTLInputKernelBundle_*`, `DPCTLObjectKernelBundle_*` and `DPCTLKernelId_*` C-API functions and the `SyclInputKernelBundle_*`, `SyclObjectKernelBundle_*` and `SyclKernelId_*` Cython C-API functions [gh-2440](https://github.com/IntelPython/dpctl/pull/2440) * Added the `selector` argument of `dpctl.compiler.get_kernel_bundle`, a callable that chooses the device images to include, with the `DPCTLKernelBundle_CreateFromContextWithSelector`, `DPCTLInputKernelBundle_CreateFromContextWithSelector`, `DPCTLObjectKernelBundle_CreateFromContextWithSelector` and `DPCTLDeviceImage_HasKernel` C-API functions [gh-2440](https://github.com/IntelPython/dpctl/pull/2440) * Added `dpctl4pybind11.hpp` type casters for `sycl::kernel_bundle`, `sycl::kernel_bundle` and `sycl::kernel_id`, and the `examples/pybind11/use_kernel_bundle` example that sets a SYCL specialization constant on a kernel bundle obtained in Python [gh-2440](https://github.com/IntelPython/dpctl/pull/2440) +* Added a `dpctl4pybind11.hpp` type caster for `sycl::platform`, and the `SyclPlatform_GetPlatformRef` and `SyclPlatform_Make` Cython C-API functions it uses [gh-2442](https://github.com/IntelPython/dpctl/pull/2442) * Added `dpctl.compiler.load_kernel_library` and the `DPCTLKernelId_LoadLibrary` C-API function, which load a shared library with SYCL device code, e.g. compiled for CUDA or HIP devices, and return the ids of its kernels for use with `dpctl.compiler.get_kernel_bundle` [gh-2440](https://github.com/IntelPython/dpctl/pull/2440) * Added `dpctl.compiler.SyclKernel.get_work_group_size`, `get_preferred_work_group_size_multiple`, `get_private_mem_size`, `get_max_num_sub_groups`, `get_max_sub_group_size`, `get_compile_num_sub_groups` and `get_compile_sub_group_size`, which take an optional device of the kernel bundle and raise `ValueError` for another device, and the `DPCTLKernel_Get*ForDevice` and `DPCTLKernel_GetDevices` C-API functions [gh-2440](https://github.com/IntelPython/dpctl/pull/2440) * Added the `DPCTLKernelBundle_CreateFromOCLSourceWithBuildLog` and `DPCTLKernelBundle_CreateFromSpirvWithBuildLog` C-API functions, which return the build log of a failed build [gh-2440](https://github.com/IntelPython/dpctl/pull/2440) diff --git a/docs/doc_sources/api_reference/dpctl_capi.rst b/docs/doc_sources/api_reference/dpctl_capi.rst index 535025b73f..9d96c0619e 100644 --- a/docs/doc_sources/api_reference/dpctl_capi.rst +++ b/docs/doc_sources/api_reference/dpctl_capi.rst @@ -13,6 +13,10 @@ Exported typedefs .. c:struct:: PySyclDeviceType +.. c:struct:: PySyclPlatformObject + +.. c:struct:: PySyclPlatformType + .. c:struct:: PySyclContextObject .. c:struct:: PySyclContextType @@ -77,6 +81,22 @@ API for :c:struct:`PySyclDeviceObject` Note that function does not change the ownership of the ``DRef`` instance and the caller remains responsible for freeing ``DRef`` as appropriate. +API for :c:struct:`PySyclPlatformObject` +---------------------------------------- + +.. c:function:: DPCTLSyclPlatformRef SyclPlatform_GetPlatformRef(struct PySyclPlatformObject *o) + + :param o: Input object + :returns: borrowed instance of :c:struct:`DPCTLSyclPlatformRef` + +.. c:function:: struct PySyclPlatformObject * SyclPlatform_Make(DPCTLSyclPlatformRef PRef) + + :param PRef: instance of :c:struct:`DPCTLSyclPlatformRef` + :returns: new Python object of type :c:struct:`PySyclPlatformType` + + Note that function does not change the ownership of the ``PRef`` instance and + the caller remains responsible for freeing ``PRef`` as appropriate. + API for :c:struct:`PySyclContextObject` --------------------------------------- diff --git a/dpctl/_sycl_platform.pxd b/dpctl/_sycl_platform.pxd index d6247c1940..d886fcd400 100644 --- a/dpctl/_sycl_platform.pxd +++ b/dpctl/_sycl_platform.pxd @@ -26,7 +26,10 @@ from libcpp cimport bool from ._backend cimport DPCTLSyclDeviceSelectorRef, DPCTLSyclPlatformRef -cdef class _SyclPlatform: +cdef public api class _SyclPlatform [ + object Py_SyclPlatformObject, + type Py_SyclPlatformType +]: ''' A helper metaclass to abstract a ``sycl::platform`` instance. ''' cdef DPCTLSyclPlatformRef _platform_ref @@ -35,7 +38,10 @@ cdef class _SyclPlatform: cdef const char *_version -cdef class SyclPlatform(_SyclPlatform): +cdef public api class SyclPlatform(_SyclPlatform) [ + object PySyclPlatformObject, + type PySyclPlatformType +]: @staticmethod cdef SyclPlatform _create(DPCTLSyclPlatformRef dref) cdef int _init_from_cstring(self, const char *string) diff --git a/dpctl/_sycl_platform.pyx b/dpctl/_sycl_platform.pyx index 9327820021..2b0533a5b2 100644 --- a/dpctl/_sycl_platform.pyx +++ b/dpctl/_sycl_platform.pyx @@ -944,3 +944,20 @@ cpdef tuple get_platforms(): DPCTLPlatformVector_Delete(PVRef) return tuple(platforms) + + +cdef api DPCTLSyclPlatformRef SyclPlatform_GetPlatformRef(SyclPlatform pl): + """ + C-API function to get opaque platform reference from + :class:`dpctl.SyclPlatform` instance. + """ + return pl.get_platform_ref() + + +cdef api SyclPlatform SyclPlatform_Make(DPCTLSyclPlatformRef PRef): + """ + C-API function to create :class:`dpctl.SyclPlatform` instance + from the given opaque platform reference. + """ + cdef DPCTLSyclPlatformRef copied_PRef = DPCTLPlatform_Copy(PRef) + return SyclPlatform._create(copied_PRef) diff --git a/dpctl/apis/include/dpctl4pybind11.hpp b/dpctl/apis/include/dpctl4pybind11.hpp index b972096d3a..fb658521d6 100644 --- a/dpctl/apis/include/dpctl4pybind11.hpp +++ b/dpctl/apis/include/dpctl4pybind11.hpp @@ -70,6 +70,8 @@ class dpctl_capi // dpctl type objects PyTypeObject *Py_SyclDeviceType_; PyTypeObject *PySyclDeviceType_; + PyTypeObject *Py_SyclPlatformType_; + PyTypeObject *PySyclPlatformType_; PyTypeObject *Py_SyclContextType_; PyTypeObject *PySyclContextType_; PyTypeObject *Py_SyclEventType_; @@ -89,6 +91,10 @@ class dpctl_capi DPCTLSyclDeviceRef (*SyclDevice_GetDeviceRef_)(PySyclDeviceObject *); PySyclDeviceObject *(*SyclDevice_Make_)(DPCTLSyclDeviceRef); + DPCTLSyclPlatformRef (*SyclPlatform_GetPlatformRef_)( + PySyclPlatformObject *); + PySyclPlatformObject *(*SyclPlatform_Make_)(DPCTLSyclPlatformRef); + DPCTLSyclContextRef (*SyclContext_GetContextRef_)(PySyclContextObject *); PySyclContextObject *(*SyclContext_Make_)(DPCTLSyclContextRef); @@ -141,6 +147,10 @@ class dpctl_capi { return PyObject_TypeCheck(obj, PySyclDeviceType_) != 0; } + bool PySyclPlatform_Check_(PyObject *obj) const + { + return PyObject_TypeCheck(obj, PySyclPlatformType_) != 0; + } bool PySyclContext_Check_(PyObject *obj) const { return PyObject_TypeCheck(obj, PySyclContextType_) != 0; @@ -237,6 +247,7 @@ class dpctl_capi dpctl_capi() : Py_SyclDeviceType_(nullptr), PySyclDeviceType_(nullptr), + Py_SyclPlatformType_(nullptr), PySyclPlatformType_(nullptr), Py_SyclContextType_(nullptr), PySyclContextType_(nullptr), Py_SyclEventType_(nullptr), PySyclEventType_(nullptr), Py_SyclQueueType_(nullptr), PySyclQueueType_(nullptr), @@ -246,7 +257,8 @@ class dpctl_capi PySyclInputKernelBundleType_(nullptr), PySyclObjectKernelBundleType_(nullptr), PySyclKernelType_(nullptr), PySyclKernelIdType_(nullptr), SyclDevice_GetDeviceRef_(nullptr), - SyclDevice_Make_(nullptr), SyclContext_GetContextRef_(nullptr), + SyclDevice_Make_(nullptr), SyclPlatform_GetPlatformRef_(nullptr), + SyclPlatform_Make_(nullptr), SyclContext_GetContextRef_(nullptr), SyclContext_Make_(nullptr), SyclEvent_GetEventRef_(nullptr), SyclEvent_Make_(nullptr), SyclQueue_GetQueueRef_(nullptr), SyclQueue_Make_(nullptr), SyclQueue_DeferRelease_(nullptr), @@ -276,6 +288,8 @@ class dpctl_capi // Python type objects for classes implemented by dpctl this->Py_SyclDeviceType_ = &Py_SyclDeviceType; this->PySyclDeviceType_ = &PySyclDeviceType; + this->Py_SyclPlatformType_ = &Py_SyclPlatformType; + this->PySyclPlatformType_ = &PySyclPlatformType; this->Py_SyclContextType_ = &Py_SyclContextType; this->PySyclContextType_ = &PySyclContextType; this->Py_SyclEventType_ = &Py_SyclEventType; @@ -296,6 +310,10 @@ class dpctl_capi this->SyclDevice_GetDeviceRef_ = SyclDevice_GetDeviceRef; this->SyclDevice_Make_ = SyclDevice_Make; + // SyclPlatform API + this->SyclPlatform_GetPlatformRef_ = SyclPlatform_GetPlatformRef; + this->SyclPlatform_Make_ = SyclPlatform_Make; + // SyclContext API this->SyclContext_GetContextRef_ = SyclContext_GetContextRef; this->SyclContext_Make_ = SyclContext_Make; @@ -478,6 +496,41 @@ template <> struct type_caster DPCTL_TYPE_CASTER(sycl::device, _("dpctl.SyclDevice")); }; +/* This type caster associates ``sycl::platform`` C++ class with + * :class:`dpctl.SyclPlatform` for the purposes of generation of + * Python bindings by pybind11. + */ +template <> struct type_caster +{ +public: + bool load(handle src, bool) + { + PyObject *source = src.ptr(); + auto const &api = ::dpctl::detail::dpctl_capi::get(); + if (api.PySyclPlatform_Check_(source)) { + DPCTLSyclPlatformRef PRef = api.SyclPlatform_GetPlatformRef_( + reinterpret_cast(source)); + value = std::make_unique( + *(reinterpret_cast(PRef))); + return true; + } + else { + throw py::type_error( + "Input is of unexpected type, expected dpctl.SyclPlatform"); + } + } + + static handle cast(sycl::platform src, return_value_policy, handle) + { + auto const &api = ::dpctl::detail::dpctl_capi::get(); + auto tmp = api.SyclPlatform_Make_( + reinterpret_cast(&src)); + return handle(reinterpret_cast(tmp)); + } + + DPCTL_TYPE_CASTER(sycl::platform, _("dpctl.SyclPlatform")); +}; + /* This type caster associates ``sycl::context`` C++ class with * :class:`dpctl.SyclContext` for the purposes of generation of * Python bindings by pybind11. diff --git a/dpctl/apis/include/dpctl_capi.h b/dpctl/apis/include/dpctl_capi.h index 3ccf04e78e..e44f1d93df 100644 --- a/dpctl/apis/include/dpctl_capi.h +++ b/dpctl/apis/include/dpctl_capi.h @@ -37,6 +37,8 @@ #endif #include "dpctl/_sycl_device.h" #include "dpctl/_sycl_device_api.h" +#include "dpctl/_sycl_platform.h" +#include "dpctl/_sycl_platform_api.h" #include "dpctl/_sycl_context.h" #include "dpctl/_sycl_context_api.h" #include "dpctl/_sycl_event.h" @@ -63,8 +65,10 @@ */ static inline int import_dpctl(void) { - if (import_dpctl___sycl_device() < 0 || import_dpctl___sycl_context() < 0 || - import_dpctl___sycl_event() < 0 || import_dpctl___sycl_queue() < 0 || + if (import_dpctl___sycl_device() < 0 || + import_dpctl___sycl_platform() < 0 || + import_dpctl___sycl_context() < 0 || import_dpctl___sycl_event() < 0 || + import_dpctl___sycl_queue() < 0 || import_dpctl__memory___memory() < 0 || import_dpctl__compiler___compiler() < 0) { diff --git a/dpctl/tests/test_sycl_platform.py b/dpctl/tests/test_sycl_platform.py index f887012e45..bf17e95f99 100644 --- a/dpctl/tests/test_sycl_platform.py +++ b/dpctl/tests/test_sycl_platform.py @@ -375,3 +375,44 @@ def test_platform_get_composite_devices(): assert all(d in devices for d in component_devices) else: pytest.skip("No platforms available") + + +def _get_capi_fn(mod, name, signature, restype, argtype): + import ctypes + + cap = mod.__pyx_capi__[name] + cap_ptr_fn = ctypes.pythonapi.PyCapsule_GetPointer + cap_ptr_fn.restype = ctypes.c_void_p + cap_ptr_fn.argtypes = [ctypes.py_object, ctypes.c_char_p] + fn_ptr = cap_ptr_fn(cap, signature) + return ctypes.PYFUNCTYPE(restype, argtype)(fn_ptr) + + +def test_cpython_api_SyclPlatform_GetPlatformRef_and_Make(): + import ctypes + + if not has_sycl_platforms(): + pytest.skip("No SYCL platforms available") + p = dpctl.get_platforms()[0] + mod = sys.modules[p.__class__.__module__] + get_ref_fn = _get_capi_fn( + mod, + "SyclPlatform_GetPlatformRef", + b"DPCTLSyclPlatformRef (struct PySyclPlatformObject *)", + ctypes.c_void_p, + ctypes.py_object, + ) + make_fn = _get_capi_fn( + mod, + "SyclPlatform_Make", + b"struct PySyclPlatformObject *(DPCTLSyclPlatformRef)", + ctypes.py_object, + ctypes.c_void_p, + ) + + p_ref = get_ref_fn(p) + assert p_ref + p2 = make_fn(p_ref) + assert type(p2) is dpctl.SyclPlatform + assert p2 == p + assert get_ref_fn(p2) != p_ref diff --git a/examples/pybind11/use_dpctl_sycl_queue/tests/test_queue_device.py b/examples/pybind11/use_dpctl_sycl_queue/tests/test_queue_device.py index e495c87cd4..29bf45fa69 100644 --- a/examples/pybind11/use_dpctl_sycl_queue/tests/test_queue_device.py +++ b/examples/pybind11/use_dpctl_sycl_queue/tests/test_queue_device.py @@ -17,6 +17,7 @@ # coding: utf-8 import numpy as np +import pytest import use_queue_device as uqd import dpctl @@ -71,3 +72,24 @@ def test_get_partition_max_sub_devices(): assert type(mt) is int assert mt >= 0 assert mt <= d.max_compute_units + + +def test_get_device_platform(): + d = dpctl.SyclDevice() + p = uqd.get_device_platform(d) + assert type(p) is dpctl.SyclPlatform + assert p == d.sycl_platform + + +def test_get_platform_devices(): + p = dpctl.SyclDevice().sycl_platform + devs = uqd.get_platform_devices(p) + assert type(devs) is list + assert all(type(d) is dpctl.SyclDevice for d in devs) + assert devs == list(p.get_devices()) + + +def test_platform_caster_rejects_other_types(): + d = dpctl.SyclDevice() + with pytest.raises(TypeError): + uqd.get_platform_devices(d) diff --git a/examples/pybind11/use_dpctl_sycl_queue/use_queue_device/__init__.py b/examples/pybind11/use_dpctl_sycl_queue/use_queue_device/__init__.py index 461fc2ebda..6f3259092c 100644 --- a/examples/pybind11/use_dpctl_sycl_queue/use_queue_device/__init__.py +++ b/examples/pybind11/use_dpctl_sycl_queue/use_queue_device/__init__.py @@ -19,8 +19,10 @@ from ._use_queue_device import ( get_device_global_mem_size, get_device_local_mem_size, + get_device_platform, get_max_compute_units, get_partition_max_sub_devices, + get_platform_devices, get_sub_group_sizes, offloaded_array_mod, ) @@ -32,6 +34,8 @@ "offloaded_array_mod", "get_sub_group_sizes", "get_partition_max_sub_devices", + "get_device_platform", + "get_platform_devices", ] __doc__ = """ @@ -39,7 +43,8 @@ SYCL entities. dpctl provides type casters that bind ``sycl::queue`` to `dpctl.SyclQueue`, -``sycl::device`` to `dpctl.SyclDevice`, etc. +``sycl::device`` to `dpctl.SyclDevice`, ``sycl::platform`` to +`dpctl.SyclPlatform`, etc. Use of these type casters simplifies writing of Python extensions and compile then using SYCL C++ compilers, such as Intel(R) oneAPI DPC++ compiler. diff --git a/examples/pybind11/use_dpctl_sycl_queue/use_queue_device/_example.cpp b/examples/pybind11/use_dpctl_sycl_queue/use_queue_device/_example.cpp index a82aa591b4..88bfd3b8c7 100644 --- a/examples/pybind11/use_dpctl_sycl_queue/use_queue_device/_example.cpp +++ b/examples/pybind11/use_dpctl_sycl_queue/use_queue_device/_example.cpp @@ -95,6 +95,16 @@ std::uint32_t get_partition_max_sub_devices(const sycl::device &d) return d.get_info(); } +sycl::platform get_device_platform(const sycl::device &d) +{ + return d.get_platform(); +} + +std::vector get_platform_devices(const sycl::platform &p) +{ + return p.get_devices(); +} + PYBIND11_MODULE(_use_queue_device, m, py::mod_gil_not_used()) { m.def( @@ -116,4 +126,8 @@ PYBIND11_MODULE(_use_queue_device, m, py::mod_gil_not_used()) m.def("get_partition_max_sub_devices", &get_partition_max_sub_devices, "Gets info::device::partition_max_sub_devices for given " "dpctl.SyclDevice"); + m.def("get_device_platform", &get_device_platform, + "Gets the platform of given dpctl.SyclDevice"); + m.def("get_platform_devices", &get_platform_devices, + "Gets the devices of given dpctl.SyclPlatform"); }