Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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::bundle_state::input>`, `sycl::kernel_bundle<sycl::bundle_state::object>` 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)
Expand Down
20 changes: 20 additions & 0 deletions docs/doc_sources/api_reference/dpctl_capi.rst
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,10 @@ Exported typedefs

.. c:struct:: PySyclDeviceType

.. c:struct:: PySyclPlatformObject

.. c:struct:: PySyclPlatformType

.. c:struct:: PySyclContextObject

.. c:struct:: PySyclContextType
Expand Down Expand Up @@ -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`
---------------------------------------

Expand Down
10 changes: 8 additions & 2 deletions dpctl/_sycl_platform.pxd
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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)
Expand Down
17 changes: 17 additions & 0 deletions dpctl/_sycl_platform.pyx
Original file line number Diff line number Diff line change
Expand Up @@ -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)
55 changes: 54 additions & 1 deletion dpctl/apis/include/dpctl4pybind11.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -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_;
Expand All @@ -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);

Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -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),
Expand All @@ -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),
Expand Down Expand Up @@ -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;
Expand All @@ -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;
Expand Down Expand Up @@ -478,6 +496,41 @@ template <> struct type_caster<sycl::device>
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<sycl::platform>
{
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<PySyclPlatformObject *>(source));
value = std::make_unique<sycl::platform>(
*(reinterpret_cast<sycl::platform *>(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<DPCTLSyclPlatformRef>(&src));
return handle(reinterpret_cast<PyObject *>(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.
Expand Down
8 changes: 6 additions & 2 deletions dpctl/apis/include/dpctl_capi.h
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand All @@ -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)
{
Expand Down
41 changes: 41 additions & 0 deletions dpctl/tests/test_sycl_platform.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
# coding: utf-8

import numpy as np
import pytest
import use_queue_device as uqd

import dpctl
Expand Down Expand Up @@ -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)
Original file line number Diff line number Diff line change
Expand Up @@ -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,
)
Expand All @@ -32,14 +34,17 @@
"offloaded_array_mod",
"get_sub_group_sizes",
"get_partition_max_sub_devices",
"get_device_platform",
"get_platform_devices",
]

__doc__ = """
Example pybind11 extension demonstrating binding of dpctl entities to
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.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -95,6 +95,16 @@ std::uint32_t get_partition_max_sub_devices(const sycl::device &d)
return d.get_info<sycl::info::device::partition_max_sub_devices>();
}

sycl::platform get_device_platform(const sycl::device &d)
{
return d.get_platform();
}

std::vector<sycl::device> get_platform_devices(const sycl::platform &p)
{
return p.get_devices();
}

PYBIND11_MODULE(_use_queue_device, m, py::mod_gil_not_used())
{
m.def(
Expand All @@ -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");
}
Loading