Skip to content

Commit 103635c

Browse files
Added DPCTLDeviceMgr_GetRelativeId(DRef)
This computes the relative id (position in the vector) inside p.get_devices( sycl_device_type(DRef) ). Returns -1 is the device was not found (expected to happen for sub-devices).
1 parent 0f4f61e commit 103635c

File tree

3 files changed

+37
-0
lines changed

3 files changed

+37
-0
lines changed

dpctl-capi/include/dpctl_sycl_device_manager.h

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -116,4 +116,15 @@ size_t DPCTLDeviceMgr_GetNumDevices(int device_identifier);
116116
DPCTL_API
117117
void DPCTLDeviceMgr_PrintDeviceInfo(__dpctl_keep const DPCTLSyclDeviceRef DRef);
118118

119+
/*!
120+
* @brief Gives the index of the given device in the vector returned get_devices
121+
* for the platform associated with DRef for the device type of DRef.
122+
*
123+
* @param DRef A #DPCTLSyclDeviceRef opaque pointer.
124+
* @ingroup DeviceManager
125+
*/
126+
DPCTL_API
127+
int64_t
128+
DPCTLDeviceMgr_GetRelativeId(__dpctl_keep const DPCTLSyclDeviceRef DRef);
129+
119130
DPCTL_C_EXTERN_C_END

dpctl-capi/source/dpctl_sycl_device_manager.cpp

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -194,3 +194,22 @@ void DPCTLDeviceMgr_PrintDeviceInfo(__dpctl_keep const DPCTLSyclDeviceRef DRef)
194194
std::cout << "Device is not valid (NULL). Cannot print device info.\n";
195195
}
196196
}
197+
198+
int64_t DPCTLDeviceMgr_GetRelativeId(__dpctl_keep const DPCTLSyclDeviceRef DRef)
199+
{
200+
auto Device = unwrap(DRef);
201+
202+
if (Device) {
203+
auto p = Device->get_platform();
204+
auto dt = Device->get_info<sycl::info::device::device_type>();
205+
auto dev_vec = p.get_devices(dt);
206+
int64_t id = 0;
207+
for (auto &d_i : dev_vec) {
208+
if (*Device == d_i)
209+
return id;
210+
++id;
211+
}
212+
return -1;
213+
}
214+
return -1;
215+
}

dpctl-capi/tests/test_sycl_device_manager.cpp

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,13 @@ struct TestDPCTLDeviceManager : public ::testing::TestWithParam<const char *>
5656
}
5757
};
5858

59+
TEST_P(TestDPCTLDeviceManager, Chk_GetRelativeId)
60+
{
61+
int64_t rel_id = -1;
62+
EXPECT_NO_FATAL_FAILURE(rel_id = DPCTLDeviceMgr_GetRelativeId(DRef));
63+
EXPECT_FALSE(rel_id == -1);
64+
}
65+
5966
TEST_P(TestDPCTLDeviceManager, Chk_PrintDeviceInfo)
6067
{
6168
EXPECT_NO_FATAL_FAILURE(DPCTLDeviceMgr_PrintDeviceInfo(DRef));

0 commit comments

Comments
 (0)