Skip to content
Open
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
5 changes: 5 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -17,13 +17,17 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
* Added support for building `dpctl` with AdaptiveCpp as the SYCL implementation, selected with `--sycl-provider=AdaptiveCpp` in `scripts/build_locally.py` or `-DDPCTL_SYCL_PROVIDER=AdaptiveCpp` [gh-2384](https://github.com/IntelPython/dpctl/pull/2384)
* Added `dpctl.backend_type.openmp` for the AdaptiveCpp OpenMP host device [gh-2384](https://github.com/IntelPython/dpctl/pull/2384)
* Added `DPCTLService_GetSyclProvider` C-API function and `dpctl._diagnostics.sycl_provider`, which report the SYCL implementation `dpctl` was built with [gh-2384](https://github.com/IntelPython/dpctl/pull/2384)
* Added `custom` device type to filter selector strings [gh-2420](https://github.com/IntelPython/dpctl/pull/2420)

### Changed
* Object lifetime during offload is now managed by a list of releases deferred until the events that gate them have completed, rather than by a `host_task`. `dpctl` keeps one thread for looking over the list and running the releases that have come due, started by the first release deferred and again in the child of a `fork`, which takes the GIL to do so and lets a release call into the interpreter and free USM allocations [gh-2359](https://github.com/IntelPython/dpctl/pull/2359)
* `import_dpctl()` now returns 0 on success and -1 with a Python exception set on failure, and `dpctl4pybind11.hpp` raises that exception rather than failing on the first C-API call [gh-2359](https://github.com/IntelPython/dpctl/pull/2359)
* The event returned by `dpctl.SyclQueue.keep_args_alive`, `dpctl::utils::keep_args_alive` and `dpctl.SyclQueue._submit_keep_args_alive` is now for an empty kernel that gates the deferred release rather than for a `host_task` that performs it, so its completion means that the objects are no longer in use rather than that they have been released [gh-2359](https://github.com/IntelPython/dpctl/pull/2359)
* `dpctl.utils.SequentialOrderManager[q].submitted_events` includes the queue's last event for in-order queues when `sycl_ext_oneapi_in_order_queue_events` is available, and `wait()` waits for all work on an in-order queue [gh-2299](https://github.com/IntelPython/dpctl/pull/2299)
* Pickled `dpctl.memory.MemoryUSMShared`, `MemoryUSMDevice`, and `MemoryUSMHost` objects keep their queue instead of being restored on the default device. Pickling memory whose queue can not be pickled raises `pickle.PicklingError` [gh-2406](https://github.com/IntelPython/dpctl/pull/2406)
* Filter selector strings are implemented in `dpctl` instead of relying on the deprecated DPC++ `sycl_ext_oneapi_filter_selector` extension, and device numbers in a list of filters, such as `"0,1"`, now refer to the same devices as when used in separate filters [gh-2420](https://github.com/IntelPython/dpctl/pull/2420)
* `dpctl.SyclDevice.filter_string` and `dpctl.SyclDevice.get_filter_string` raise `ValueError` for backends and device types not supported in filter selector strings, instead of returning a string containing `"unknown"` [gh-2420](https://github.com/IntelPython/dpctl/pull/2420)
* A filter selector string with a filter that has no components, such as `":"` or `"gpu:0,:"`, is rejected as malformed instead of selecting any device [gh-2420](https://github.com/IntelPython/dpctl/pull/2420)

### Deprecated
* Deprecated `dpctl.SyclQueue._submit_keep_args_alive` in favor of `dpctl.SyclQueue.keep_args_alive`, which it now calls and which behaves identically [gh-2359](https://github.com/IntelPython/dpctl/pull/2359)
Expand All @@ -42,6 +46,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
* `dpctl.SyclPlatform.get_devices` no longer prints Unified Runtime warnings for `device_type="custom"` on non-OpenCL platforms, where custom devices are not supported [gh-2306](https://github.com/IntelPython/dpctl/pull/2306)
* Fixed building with the open source DPC++ compiler and CMake 4.5 or newer, which identifies it as `DPClang` instead of `Clang` [gh-2433](https://github.com/IntelPython/dpctl/pull/2433)
* Building with the open source DPC++ toolchain no longer requires the toolchain to bundle the OpenCL ICD loader, which `dpctl` loads at run time [gh-2435](https://github.com/IntelPython/dpctl/pull/2435)
* `DPCTLDeviceSelector_Score` returns correct scores for filter selectors with device numbers [gh-2420](https://github.com/IntelPython/dpctl/pull/2420)

### Maintenance
* Removed the `-fno-sycl-id-queries-fit-in-int` compile option, which none of the `dpctl` extensions need since `dpctl.tensor` moved to `dpnp` [gh-2413](https://github.com/IntelPython/dpctl/pull/2413)
Expand Down
21 changes: 13 additions & 8 deletions docs/doc_sources/api_reference/dpctl/filter_selector_string.rst
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,10 @@
Filter Selector String
======================

Filter selector string is a way to refer to unpartitioned SYCL devices
proposed in :oneapi_filter_selection:`sycl_ext_oneapi_filter_selector <>`
extension to SYCL standard.

This document captures aspects of the referenced document relevant
to :mod:`dpctl`.
Filter selector string is a way to refer to unpartitioned SYCL devices.
:mod:`dpctl` implements the syntax of the DPC++
:oneapi_filter_selection:`sycl_ext_oneapi_filter_selector <>` extension
without relying on the extension itself.

A filter selector string defines one or more filters, which must be
separated using ``","`` character. A filter is specified as a
Expand All @@ -18,15 +16,18 @@ triple of the form:

Backend:DeviceType:RelativeDeviceNumber

Every element of the triple is optional, but a filter must contain at
least one component.
Every element of the triple is optional and the elements may be given in
any order, but a filter must contain at least one component.

``Backend`` specifies the desired backend of targeted devices, while
``DeviceType`` specifies the type of targeted devices.
``RelativeDeviceNumber`` refers to the number of the device that matches
any other given requirements, starting from 0 to marking the
"first device that matches the requirements".

If several devices match, the one with the highest default selector
score is selected.

Attempting to use a non-conforming string in places where filter selector
string is expected will raise an exception.

Expand All @@ -39,12 +40,16 @@ Supported values for ``Backend`` are:
- level_zero
- hip

When :mod:`dpctl` is built with AdaptiveCpp, ``openmp`` selects the
AdaptiveCpp OpenMP host device.

Supported values for ``DeviceType`` are:

.. list-table::

* - accelerator
- cpu
- custom
- gpu

Filter selector strings can be used as arguments to constructors of
Expand Down
103 changes: 61 additions & 42 deletions dpctl/_sycl_device.pyx
Original file line number Diff line number Diff line change
Expand Up @@ -242,6 +242,7 @@ cdef tuple _to_enum_tuple(


cdef str _backend_type_to_filter_string_part(_backend_type BTy):
""" Returns the filter string part for the backend, or ``None``."""
if BTy == _backend_type._CUDA:
return "cuda"
elif BTy == _backend_type._HIP:
Expand All @@ -253,20 +254,57 @@ cdef str _backend_type_to_filter_string_part(_backend_type BTy):
elif BTy == _backend_type._OPENMP:
return "openmp"
else:
return "unknown"
return None


cdef str _device_type_to_filter_string_part(_device_type DTy):
""" Returns the filter string part for the device type, or ``None``."""
if DTy == _device_type._ACCELERATOR:
return "accelerator"
elif DTy == _device_type._AUTOMATIC:
return "automatic"
elif DTy == _device_type._CPU:
return "cpu"
elif DTy == _device_type._CUSTOM:
return "custom"
elif DTy == _device_type._GPU:
return "gpu"
else:
return "unknown"
return None


cdef str _make_filter_string(
DPCTLSyclDeviceRef DRef,
bint include_backend,
bint include_device_type,
int64_t relId,
):
""" Joins the requested parts of the filter string of a root device.

Raises ``ValueError`` if the backend or device type has no filter
string representation.
"""
cdef list parts = []
if include_backend:
be_str = _backend_type_to_filter_string_part(
DPCTLDevice_GetBackend(DRef)
)
if be_str is None:
raise ValueError(
"The backend of this SyclDevice is not supported in filter "

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nit: The "unknown" in filter_string.split(":")[:2] check in SyclDevice.__reduce__ (from gh-2406) can no longer be true: filter_string now raises ValueError here, and __reduce__ already turns that into PicklingError. The check and the "unknown" line of its docstring could go.

"strings"
)
parts.append(be_str)
if include_device_type:
dt_str = _device_type_to_filter_string_part(
DPCTLDevice_GetDeviceType(DRef)
)
if dt_str is None:
raise ValueError(
"The device type of this SyclDevice is not supported in "
"filter strings"
)
parts.append(dt_str)
parts.append(str(relId))
return ":".join(parts)


cdef void _init_helper(_SyclDevice device, DPCTLSyclDeviceRef DRef) except *:
Expand Down Expand Up @@ -324,22 +362,17 @@ cdef inline void _raise_invalid_peer_access(
]
cdef _backend_type BTy1 = DPCTLDevice_GetBackend(dev._device_ref)
cdef _backend_type BTy2 = DPCTLDevice_GetBackend(peer.get_device_ref())
be1 = _backend_type_to_filter_string_part(BTy1) or "unknown"
be2 = _backend_type_to_filter_string_part(BTy2) or "unknown"
if (BTy1 != BTy2):
raise ValueError(
f"Device with backend {_backend_type_to_filter_string_part(BTy1)} "
"cannot peer access device with backend "
f"{_backend_type_to_filter_string_part(BTy2)}"
f"Device with backend {be1} "
f"cannot peer access device with backend {be2}"
)
if (BTy1 not in _peer_access_backends):
raise ValueError(
"Peer access not supported for backend "
f"{_backend_type_to_filter_string_part(BTy1)}"
)
raise ValueError(f"Peer access not supported for backend {be1}")
if (BTy2 not in _peer_access_backends):
raise ValueError(
"Peer access not supported for backend "
f"{_backend_type_to_filter_string_part(BTy2)}"
)
raise ValueError(f"Peer access not supported for backend {be2}")
if (dev == peer):
raise ValueError(
"Peer access cannot be enabled between a device and itself"
Expand All @@ -362,20 +395,15 @@ def _cached_filter_string(d : SyclDevice):
if the device is a root (unpartitioned) device.

Raises:
ValueError: if the input device is a sub-device.
ValueError: if the input device is a sub-device, or its backend or
device type is not supported in filter strings.
"""
cdef _backend_type BTy
cdef _device_type DTy
cdef int64_t relId = -1
cdef SyclDevice cd = <SyclDevice> d
relId = DPCTLDeviceMgr_GetRelativeId(cd._device_ref)
if (relId == -1):
raise ValueError("This SyclDevice is not a root device")
BTy = DPCTLDevice_GetBackend(cd._device_ref)
br_str = _backend_type_to_filter_string_part(BTy)
DTy = DPCTLDevice_GetDeviceType(cd._device_ref)
dt_str = _device_type_to_filter_string_part(DTy)
return ":".join((br_str, dt_str, str(relId)))
return _make_filter_string(cd._device_ref, True, True, relId)


def _rebuild_device(str filter_string, str name):
Expand Down Expand Up @@ -406,7 +434,7 @@ cdef class SyclDevice(_SyclDevice):

- by directly passing in a filter string to the class
constructor. The filter string needs to conform to the
:oneapi_filter_selection:`DPC++ filter selector SYCL extension <>`.
:ref:`filter selector string <filter_selector_string>` syntax.

:Example:

Expand Down Expand Up @@ -2702,6 +2730,9 @@ cdef class SyclDevice(_SyclDevice):
ValueError:
If the device is a sub-device.

If the backend or device type of the device is not
supported in filter strings.

:Example:
.. code-block:: python

Expand Down Expand Up @@ -2808,6 +2839,9 @@ cdef class SyclDevice(_SyclDevice):
If no match for the device was found in the vector
returned by ``sycl::device::get_devices()``

If the requested backend or device type of the device is
not supported in filter strings.

:Example:
.. code-block:: python

Expand All @@ -2822,8 +2856,6 @@ cdef class SyclDevice(_SyclDevice):
"""
cdef int relId = -1
cdef DPCTLSyclDeviceRef pDRef = NULL
cdef _device_type DTy
cdef _backend_type BTy

if include_backend:
if include_device_type:
Expand All @@ -2845,22 +2877,9 @@ cdef class SyclDevice(_SyclDevice):
DPCTLDevice_Delete(pDRef)
raise ValueError("This SyclDevice is not a root device")
else:
if include_backend:
BTy = DPCTLDevice_GetBackend(self._device_ref)
be_str = _backend_type_to_filter_string_part(BTy)
if include_device_type:
DTy = DPCTLDevice_GetDeviceType(self._device_ref)
dt_str = _device_type_to_filter_string_part(DTy)
return ":".join((be_str, dt_str, str(relId)))
else:
return ":".join((be_str, str(relId)))
else:
if include_device_type:
DTy = DPCTLDevice_GetDeviceType(self._device_ref)
dt_str = _device_type_to_filter_string_part(DTy)
return ":".join((dt_str, str(relId)))
else:
return str(relId)
return _make_filter_string(
self._device_ref, include_backend, include_device_type, relId
)

def get_unpartitioned_parent_device(self):
""" get_unpartitioned_parent_device()
Expand Down
8 changes: 8 additions & 0 deletions dpctl/tests/_device_attributes_checks.py
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,14 @@
"opencl:gpu:-1",
"cuda:cpu:0",
"abc",
"OPENCL",
"opencl:gpu:cpu",
"opencl:level_zero",
"0:1",
"opencl:gpu:0:0",
"",
":",
"gpu:0,:",
1,
]

Expand Down
2 changes: 1 addition & 1 deletion libsyclinterface/helper/include/dpctl_utils_helper.h
Original file line number Diff line number Diff line change
Expand Up @@ -312,7 +312,7 @@ int64_t DPCTL_GetRelativeDeviceId(const sycl::device &Device);

/*!
* @brief Gives the filter string which would select given root device if
* used as argument to ``sycl::ext::oneapi::filter_selector``. Throws exception
* used as argument to ``DPCTLFilterSelector_Create``. Throws exception
* if filter string can not be constructed.
*
* @param Device A ``sycl::device`` object whose filter selector
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@
#pragma once

#include "Support/DllExport.h"
#include <optional>
#include <string>
#include <sycl/sycl.hpp>
#include <vector>
Expand Down Expand Up @@ -72,20 +73,32 @@ class DPCTL_API dpctl_cpu_selector : public dpctl_device_selector
int operator()(const sycl::device &d) const override;
};

/*!
* @brief Selects devices matching a filter selector string.
*
* The string is a comma-separated list of filters, each of the form
* ``backend:device_type:device_num`` with every component optional and in
* any order, but at least one given. ``device_num`` counts devices from
* ``sycl::device::get_devices()`` that match the filter's backend and device
* type and that ``sycl::default_selector_v`` does not reject. Matching devices
* are scored by ``sycl::default_selector_v``.
*
* Throws ``std::invalid_argument`` if the string is malformed.
*/
class DPCTL_API dpctl_filter_selector : public dpctl_device_selector
{
public:
dpctl_filter_selector(const std::string &fs);
explicit dpctl_filter_selector(const std::string &fs);
int operator()(const sycl::device &d) const override;

private:
#ifndef __ADAPTIVECPP__
sycl::ext::oneapi::filter_selector _impl;
#else
// AdaptiveCpp has no filter_selector extension, so the filter string is
// resolved to devices when the selector is constructed.
std::vector<sycl::device> _matches;
#endif
struct filter
{
std::optional<sycl::backend> backend;
std::optional<sycl::info::device_type> device_type;
std::optional<sycl::device> device;
};
std::vector<filter> _filters;
};

} // namespace syclinterface
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -65,12 +65,12 @@ DPCTL_API
__dpctl_give DPCTLSyclDeviceSelectorRef DPCTLCPUSelector_Create(void);

/*!
* @brief Returns an opaque wrapper for sycl::ext::oneapi::filter_selector
* object based on the passed in filter string.
* @brief Returns a device selector for the passed in filter string.
*
* @param filter_str A C string providing a filter based on which to
* create a device selector.
* @return An opaque pointer to a sycl::ext::oneapi::filter_selector object.
* @return An opaque pointer to a device selector, or nullptr if the
* filter string is malformed.
* @ingroup DeviceSelectors
*/
DPCTL_API
Expand Down
Loading
Loading