Skip to content

Commit 63b154d

Browse files
committed
Add dpctl.get_composite_devices
This leverages oneAPI extension for composite devices to add the free function `ext_oneapi_get_composite_devices` to the main dpctl namespace
1 parent 7aa6fb7 commit 63b154d

File tree

6 files changed

+77
-2
lines changed

6 files changed

+77
-2
lines changed

dpctl/__init__.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,7 @@
3535
SyclSubDeviceCreationError,
3636
)
3737
from ._sycl_device_factory import (
38+
get_composite_devices,
3839
get_devices,
3940
get_num_devices,
4041
has_accelerator_devices,
@@ -85,6 +86,7 @@
8586
"has_gpu_devices",
8687
"has_accelerator_devices",
8788
"has_host_device",
89+
"get_composite_devices",
8890
]
8991
__all__ += [
9092
"SyclEvent",

dpctl/_backend.pxd

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -237,6 +237,7 @@ cdef extern from "syclinterface/dpctl_sycl_device_manager.h":
237237
cdef DPCTLSyclContextRef DPCTLDeviceMgr_GetCachedContext(
238238
const DPCTLSyclDeviceRef DRef)
239239
cdef int64_t DPCTLDeviceMgr_GetRelativeId(const DPCTLSyclDeviceRef DRef)
240+
cdef DPCTLDeviceVectorRef DPCTLDeviceMgr_GetCompositeDevices()
240241

241242

242243
cdef extern from "syclinterface/dpctl_sycl_device_selector_interface.h":

dpctl/_sycl_device_factory.pxd

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@ cpdef SyclDevice select_cpu_device()
3232
cpdef SyclDevice select_default_device()
3333
cpdef SyclDevice select_gpu_device()
3434
cpdef list get_devices(backend=*, device_type=*)
35+
cpdef list get_composite_devices()
3536
cpdef int get_num_devices(backend=*, device_type=*)
3637
cpdef cpp_bool has_gpu_devices()
3738
cpdef cpp_bool has_cpu_devices()

dpctl/_sycl_device_factory.pyx

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@ from ._backend cimport ( # noqa: E211
3131
DPCTLCPUSelector_Create,
3232
DPCTLDefaultSelector_Create,
3333
DPCTLDevice_CreateFromSelector,
34+
DPCTLDeviceMgr_GetCompositeDevices,
3435
DPCTLDeviceMgr_GetDevices,
3536
DPCTLDeviceMgr_GetNumDevices,
3637
DPCTLDeviceSelector_Delete,
@@ -62,6 +63,7 @@ __all__ = [
6263
"has_gpu_devices",
6364
"has_accelerator_devices",
6465
"_cached_default_device",
66+
"get_composite_devices",
6567
]
6668

6769

@@ -202,6 +204,32 @@ cpdef list get_devices(backend=backend_type.all, device_type=device_type_t.all):
202204
return devices
203205

204206

207+
cpdef list get_composite_devices():
208+
"""
209+
Returns a list of the available composite :class:`dpctl.SyclDevice`
210+
instances.
211+
212+
Only available when `ZE_FLAT_DEVICE_HIERARCHY=COMBINED` is set in
213+
the environment, and only for specific Level Zero devices
214+
(i.e., those which expose multiple tiles as root devices).
215+
216+
For more information, see:
217+
https://github.com/intel/llvm/blob/sycl/sycl/doc/extensions/experimental/sycl_ext_oneapi_composite_device.asciidoc
218+
219+
Returns:
220+
list:
221+
A list of available composite :class:`dpctl.SyclDevice` instances.
222+
"""
223+
cdef DPCTLDeviceVectorRef DVRef = NULL
224+
cdef list composite_devices
225+
226+
DVRef = DPCTLDeviceMgr_GetCompositeDevices()
227+
composite_devices = _get_devices(DVRef)
228+
DPCTLDeviceVector_Delete(DVRef)
229+
230+
return composite_devices
231+
232+
205233
cpdef int get_num_devices(
206234
backend=backend_type.all, device_type=device_type_t.all
207235
):

libsyclinterface/include/syclinterface/dpctl_sycl_device_manager.h

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -173,6 +173,17 @@ DPCTL_API
173173
int64_t
174174
DPCTLDeviceMgr_GetRelativeId(__dpctl_keep const DPCTLSyclDeviceRef DRef);
175175

176+
/*!
177+
* @brief Returns a pointer to a std::vector<sycl::DPCTLSyclDeviceRef>
178+
* containing the set of ::DPCTLSyclDeviceRef pointers to the
179+
* available composite devices.
180+
*
181+
* @return A #DPCTLDeviceVectorRef containing #DPCTLSyclDeviceRef objects
182+
* that are composite devices.
183+
*/
184+
DPCTL_API
185+
__dpctl_give DPCTLDeviceVectorRef DPCTLDeviceMgr_GetCompositeDevices();
186+
176187
/*! @} */
177188

178189
DPCTL_C_EXTERN_C_END

libsyclinterface/source/dpctl_sycl_device_manager.cpp

Lines changed: 34 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -112,7 +112,7 @@ struct DeviceCacheBuilder
112112
* avoid the performance overhead of context creation for every queue.
113113
*
114114
* The singleton pattern implemented here ensures that the map is
115-
* created once in a thread-safe manner. Since, the map is ony read
115+
* created once in a thread-safe manner. Since, the map is only read
116116
* post-creation we do not need any further protection to ensure
117117
* thread-safety.
118118
*/
@@ -136,7 +136,7 @@ struct DeviceCacheBuilder
136136

137137
try {
138138
// Per https://github.com/intel/llvm/blob/sycl/sycl/doc/
139-
// extensions/PlatformContext/PlatformContext.adoc
139+
// extensions/supported/sycl_ext_oneapi_default_context.asciidoc
140140
// sycl::queue(D) would create default platform context
141141
// for capable compiler, sycl::context(D) otherwise
142142
auto Q = queue(D);
@@ -357,3 +357,35 @@ int64_t DPCTLDeviceMgr_GetRelativeId(__dpctl_keep const DPCTLSyclDeviceRef DRef)
357357

358358
return -1;
359359
}
360+
361+
/*!
362+
* Returns a list of the available composite devices, or an empty list if
363+
* there are none.
364+
*/
365+
__dpctl_give DPCTLDeviceVectorRef DPCTLDeviceMgr_GetCompositeDevices()
366+
{
367+
using vecTy = std::vector<DPCTLSyclDeviceRef>;
368+
vecTy *Devices = nullptr;
369+
370+
try {
371+
Devices = new std::vector<DPCTLSyclDeviceRef>();
372+
} catch (std::exception const &e) {
373+
delete Devices;
374+
error_handler(e, __FILE__, __func__, __LINE__);
375+
return nullptr;
376+
}
377+
378+
try {
379+
auto composite_devices =
380+
ext::oneapi::experimental::get_composite_devices();
381+
Devices->reserve(composite_devices.size());
382+
for (const auto &CDev : composite_devices) {
383+
Devices->emplace_back(wrap<device>(new device(std::move(CDev))));
384+
}
385+
return wrap<vecTy>(Devices);
386+
} catch (std::exception const &e) {
387+
delete Devices;
388+
error_handler(e, __FILE__, __func__, __LINE__);
389+
return nullptr;
390+
}
391+
}

0 commit comments

Comments
 (0)