Skip to content

Commit 6c79e90

Browse files
committed
Adds get_composite_devices method to dpctl.SyclPlatform
This method is only applicable for level_zero backend, returning an empty list for all other backend types
1 parent 30fc999 commit 6c79e90

File tree

4 files changed

+87
-0
lines changed

4 files changed

+87
-0
lines changed

dpctl/_backend.pxd

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -316,6 +316,8 @@ cdef extern from "syclinterface/dpctl_sycl_platform_interface.h":
316316
const DPCTLSyclPlatformRef)
317317
cdef DPCTLDeviceVectorRef DPCTLPlatform_GetDevices(
318318
const DPCTLSyclPlatformRef PRef, _device_type DTy)
319+
cdef DPCTLDeviceVectorRef DPCTLPlatform_GetCompositeDevices(
320+
const DPCTLSyclPlatformRef PRef)
319321

320322

321323
cdef extern from "syclinterface/dpctl_sycl_context_interface.h":

dpctl/_sycl_platform.pyx

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,7 @@ from ._backend cimport ( # noqa: E211
3737
DPCTLPlatform_CreateFromSelector,
3838
DPCTLPlatform_Delete,
3939
DPCTLPlatform_GetBackend,
40+
DPCTLPlatform_GetCompositeDevices,
4041
DPCTLPlatform_GetDefaultContext,
4142
DPCTLPlatform_GetDevices,
4243
DPCTLPlatform_GetName,
@@ -447,6 +448,41 @@ cdef class SyclPlatform(_SyclPlatform):
447448

448449
return devices
449450

451+
def get_composite_devices(self):
452+
"""
453+
Returns the list of composite :class:`dpctl.SyclDevice` objects
454+
associated with :class:`dpctl.SyclPlatform` instance.
455+
456+
Returns:
457+
list:
458+
A :obj:`list` of composite :class:`dpctl.SyclDevice` objects
459+
that belong to this platform.
460+
461+
Raises:
462+
TypeError:
463+
If `device_type` is not a str or :class:`dpctl.device_type`
464+
enum.
465+
ValueError:
466+
If the ``DPCTLPlatform_GetCompositeDevices`` call returned
467+
``NULL`` instead of a ``DPCTLDeviceVectorRef`` object.
468+
"""
469+
cdef DPCTLDeviceVectorRef DVRef = NULL
470+
cdef size_t num_devs
471+
cdef size_t i
472+
cdef DPCTLSyclDeviceRef DRef
473+
474+
DVRef = DPCTLPlatform_GetCompositeDevices(self.get_platform_ref())
475+
if (DVRef is NULL):
476+
raise ValueError("Internal error: NULL device vector encountered")
477+
num_devs = DPCTLDeviceVector_Size(DVRef)
478+
composite_devices = []
479+
for i in range(num_devs):
480+
DRef = DPCTLDeviceVector_GetAt(DVRef, i)
481+
composite_devices.append(SyclDevice._create(DRef))
482+
DPCTLDeviceVector_Delete(DVRef)
483+
484+
return composite_devices
485+
450486

451487
def lsplatform(verbosity=0):
452488
"""

libsyclinterface/include/syclinterface/dpctl_sycl_platform_interface.h

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -193,4 +193,17 @@ __dpctl_give DPCTLDeviceVectorRef
193193
DPCTLPlatform_GetDevices(__dpctl_keep const DPCTLSyclPlatformRef PRef,
194194
DPCTLSyclDeviceType DTy);
195195

196+
/*!
197+
* @brief Returns a vector of composite devices associated with sycl::platform
198+
* referenced by DPCTLSyclPlatformRef object.
199+
*
200+
* @param PRef The DPCTLSyclPlatformRef pointer.
201+
* @return A DPCTLDeviceVectorRef with composite devices associated with
202+
* given PRef.
203+
* @ingroup PlatformInterface
204+
*/
205+
DPCTL_API
206+
__dpctl_give DPCTLDeviceVectorRef
207+
DPCTLPlatform_GetCompositeDevices(__dpctl_keep const DPCTLSyclPlatformRef PRef);
208+
196209
DPCTL_C_EXTERN_C_END

libsyclinterface/source/dpctl_sycl_platform_interface.cpp

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -313,3 +313,39 @@ DPCTLPlatform_GetDevices(__dpctl_keep const DPCTLSyclPlatformRef PRef,
313313
return nullptr;
314314
}
315315
}
316+
317+
__dpctl_give DPCTLDeviceVectorRef
318+
DPCTLPlatform_GetCompositeDevices(__dpctl_keep const DPCTLSyclPlatformRef PRef)
319+
{
320+
auto P = unwrap<platform>(PRef);
321+
if (!P) {
322+
error_handler("Cannot retrieve composite devices from "
323+
"DPCTLSyclPlatformRef as input is a nullptr.",
324+
__FILE__, __func__, __LINE__);
325+
return nullptr;
326+
}
327+
328+
using vecTy = std::vector<DPCTLSyclDeviceRef>;
329+
vecTy *DevicesVectorPtr = nullptr;
330+
try {
331+
DevicesVectorPtr = new vecTy();
332+
} catch (std::exception const &e) {
333+
delete DevicesVectorPtr;
334+
error_handler(e, __FILE__, __func__, __LINE__);
335+
return nullptr;
336+
}
337+
338+
try {
339+
auto composite_devices = P->ext_oneapi_get_composite_devices();
340+
DevicesVectorPtr->reserve(composite_devices.size());
341+
for (const auto &Dev : composite_devices) {
342+
DevicesVectorPtr->emplace_back(
343+
wrap<device>(new device(std::move(Dev))));
344+
}
345+
return wrap<vecTy>(DevicesVectorPtr);
346+
} catch (std::exception const &e) {
347+
delete DevicesVectorPtr;
348+
error_handler(e, __FILE__, __func__, __LINE__);
349+
return nullptr;
350+
}
351+
}

0 commit comments

Comments
 (0)