Skip to content

Commit a67e240

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 d4df648 commit a67e240

File tree

4 files changed

+87
-0
lines changed

4 files changed

+87
-0
lines changed

dpctl/_backend.pxd

+2
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

+36
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,
@@ -448,6 +449,41 @@ cdef class SyclPlatform(_SyclPlatform):
448449

449450
return devices
450451

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

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

libsyclinterface/include/syclinterface/dpctl_sycl_platform_interface.h

+13
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

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

0 commit comments

Comments
 (0)