Skip to content

Commit 9954fa7

Browse files
DPCTLDeviceMgr_GetPositionInDevices(DRef, device_mask) added
This function is more general that DPCTL_GetRelativeId(DRef). The latter corresponds to DPCTLDeviceMgr_GetPositionInDevices(DRef, DPCTLDevice_GetBackend(DRef) | DPCTLDevice_GetDeviceType(DRef)); The function can be used to generate filter selector based on a pattern, expressed by device_mask (any backend, given type), or (any backend, any type), or (given backend, given type).
1 parent d227ab9 commit 9954fa7

File tree

3 files changed

+57
-3
lines changed

3 files changed

+57
-3
lines changed

dpctl-capi/include/dpctl_sycl_device_manager.h

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -76,6 +76,26 @@ DPCTL_API
7676
__dpctl_give DPCTLDeviceVectorRef
7777
DPCTLDeviceMgr_GetDevices(int device_identifier);
7878

79+
/*!
80+
* @brief Returns an index on the given device in the vector returned by
81+
* #DPCTLDeviceMgr_GetDevices if found, -1 otherwise.
82+
*
83+
* The device_identifier can be a combination of #DPCTLSyclBackendType and
84+
* #DPCTLSyclDeviceType bit flags. The function returns all devices that
85+
* match the specified bit flags.
86+
*
87+
* @param DRef A #DPCTLSyclDeviceRef opaque pointer.
88+
* @param device_identifier A bitflag that can be any combination of
89+
* #DPCTLSyclBackendType and #DPCTLSyclDeviceType
90+
* enum values.
91+
* @return If found, returns the position of the given device in the
92+
* vector that would be returned by #DPCTLDeviceMgr_GetDevices if called
93+
* with the same device_identifier argument.
94+
*/
95+
DPCTL_API
96+
int DPCTLDeviceMgr_GetPositionInDevices(__dpctl_keep DPCTLSyclDeviceRef DRef,
97+
int device_identifier);
98+
7999
/*!
80100
* @brief If the DPCTLSyclDeviceRef argument is a root device, then this
81101
* function returns a cached default SYCL context for that device.

dpctl-capi/source/dpctl_sycl_device_manager.cpp

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -164,6 +164,35 @@ DPCTLDeviceMgr_GetDevices(int device_identifier)
164164
return wrap(Devices);
165165
}
166166

167+
int DPCTLDeviceMgr_GetPositionInDevices(__dpctl_keep DPCTLSyclDeviceRef DRef,
168+
int device_identifier)
169+
{
170+
constexpr int not_found = -1;
171+
if (!DRef) {
172+
return not_found;
173+
}
174+
175+
const auto &root_devices = device::get_devices();
176+
default_selector mRanker;
177+
int index = not_found;
178+
auto reference_device = *(unwrap(DRef));
179+
180+
for (const auto &root_device : root_devices) {
181+
if (mRanker(root_device) < 0)
182+
continue;
183+
auto Bty(DPCTL_SyclBackendToDPCTLBackendType(
184+
root_device.get_platform().get_backend()));
185+
auto Dty(DPCTL_SyclDeviceTypeToDPCTLDeviceType(
186+
root_device.get_info<info::device::device_type>()));
187+
if ((device_identifier & Bty) && (device_identifier & Dty)) {
188+
++index;
189+
if (root_device == reference_device)
190+
return index;
191+
}
192+
}
193+
return not_found;
194+
}
195+
167196
/*!
168197
* Returns the number of available devices for a specific backend and device
169198
* type combination.

dpctl-capi/tests/test_sycl_device_manager.cpp

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -146,12 +146,12 @@ struct TestDPCTLGetDevicesOrdering : public ::testing::TestWithParam<int>
146146
{
147147
DPCTLDeviceVectorRef DV = nullptr;
148148
size_t nDevices = 0;
149+
int device_type_mask;
149150

150151
TestDPCTLGetDevicesOrdering()
151152
{
152-
const int device_type_mask =
153-
(GetParam() & DPCTLSyclDeviceType::DPCTL_ALL) |
154-
DPCTLSyclBackendType::DPCTL_ALL_BACKENDS;
153+
device_type_mask = (GetParam() & DPCTLSyclDeviceType::DPCTL_ALL) |
154+
DPCTLSyclBackendType::DPCTL_ALL_BACKENDS;
155155
EXPECT_NO_FATAL_FAILURE(
156156
DV = DPCTLDeviceMgr_GetDevices(device_type_mask));
157157
EXPECT_TRUE(DV != nullptr);
@@ -179,6 +179,7 @@ TEST_P(TestDPCTLGetDevicesOrdering, ChkConsistencyWithFilterSelector)
179179
std::string fs_device_type, fs;
180180
DPCTLSyclDeviceRef DRef = nullptr, D0Ref = nullptr;
181181
DPCTLSyclDeviceSelectorRef DSRef = nullptr;
182+
int j = -1;
182183
EXPECT_NO_FATAL_FAILURE(DRef = DPCTLDeviceVector_GetAt(DV, i));
183184
EXPECT_NO_FATAL_FAILURE(Dty = DPCTLDevice_GetDeviceType(DRef));
184185
EXPECT_NO_FATAL_FAILURE(
@@ -189,6 +190,10 @@ TEST_P(TestDPCTLGetDevicesOrdering, ChkConsistencyWithFilterSelector)
189190
EXPECT_NO_FATAL_FAILURE(D0Ref = DPCTLDevice_CreateFromSelector(DSRef));
190191
EXPECT_NO_FATAL_FAILURE(DPCTLDeviceSelector_Delete(DSRef));
191192
EXPECT_TRUE(DPCTLDevice_AreEq(DRef, D0Ref));
193+
EXPECT_NO_FATAL_FAILURE(
194+
j = DPCTLDeviceMgr_GetPositionInDevices(DRef, device_type_mask));
195+
EXPECT_TRUE(j >= 0);
196+
EXPECT_TRUE(i == static_cast<decltype(i)>(j));
192197
EXPECT_NO_FATAL_FAILURE(DPCTLDevice_Delete(D0Ref));
193198
EXPECT_NO_FATAL_FAILURE(DPCTLDevice_Delete(DRef));
194199
}

0 commit comments

Comments
 (0)