Skip to content

Commit a55ce01

Browse files
Merge pull request #451 from IntelPython/feature/get-devices-ordering
Feature/get devices ordering
2 parents 49373f8 + e19e0f7 commit a55ce01

File tree

4 files changed

+103
-27
lines changed

4 files changed

+103
-27
lines changed

dpctl-capi/include/dpctl_sycl_enum_types.h

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -38,12 +38,12 @@ DPCTL_C_EXTERN_C_BEGIN
3838
enum DPCTLSyclBackendType
3939
{
4040
// clang-format off
41-
DPCTL_CUDA = 1 << 13,
42-
DPCTL_HOST = 1 << 14,
43-
DPCTL_LEVEL_ZERO = 1 << 15,
44-
DPCTL_OPENCL = 1 << 16,
41+
DPCTL_CUDA = 1 << 16,
42+
DPCTL_HOST = 1 << 17,
43+
DPCTL_LEVEL_ZERO = 1 << 18,
44+
DPCTL_OPENCL = 1 << 19,
4545
DPCTL_UNKNOWN_BACKEND = 0,
46-
DPCTL_ALL_BACKENDS = ((1<<10)-1) << 7
46+
DPCTL_ALL_BACKENDS = ((1<<5)-1) << 16
4747
// clang-format on
4848
};
4949

@@ -57,13 +57,13 @@ enum DPCTLSyclDeviceType
5757
// The values should not overlap.
5858

5959
// clang-format off
60-
DPCTL_ACCELERATOR = 1 << 1,
61-
DPCTL_AUTOMATIC = 1 << 2,
62-
DPCTL_CPU = 1 << 3,
63-
DPCTL_CUSTOM = 1 << 4,
64-
DPCTL_GPU = 1 << 5,
65-
DPCTL_HOST_DEVICE = 1 << 6,
66-
DPCTL_ALL = (1 << 7) -1 ,
60+
DPCTL_ACCELERATOR = 1 << 0,
61+
DPCTL_AUTOMATIC = 1 << 1,
62+
DPCTL_CPU = 1 << 2,
63+
DPCTL_CUSTOM = 1 << 3,
64+
DPCTL_GPU = 1 << 4,
65+
DPCTL_HOST_DEVICE = 1 << 5,
66+
DPCTL_ALL = (1 << 6) - 1,
6767
DPCTL_UNKNOWN_DEVICE = 0
6868
// clang-format on
6969
};

dpctl-capi/source/dpctl_sycl_device_manager.cpp

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -146,15 +146,18 @@ DPCTLDeviceMgr_GetDevices(int device_identifier)
146146
} catch (std::bad_alloc const &ba) {
147147
return nullptr;
148148
}
149-
auto &cache = DeviceCacheBuilder::getDeviceCache();
149+
const auto &root_devices = device::get_devices();
150+
default_selector mRanker;
150151

151-
for (const auto &entry : cache) {
152+
for (const auto &root_device : root_devices) {
153+
if (mRanker(root_device) < 0)
154+
continue;
152155
auto Bty(DPCTL_SyclBackendToDPCTLBackendType(
153-
entry.first.get_platform().get_backend()));
156+
root_device.get_platform().get_backend()));
154157
auto Dty(DPCTL_SyclDeviceTypeToDPCTLDeviceType(
155-
entry.first.get_info<info::device::device_type>()));
158+
root_device.get_info<info::device::device_type>()));
156159
if ((device_identifier & Bty) && (device_identifier & Dty)) {
157-
Devices->emplace_back(wrap(new device(entry.first)));
160+
Devices->emplace_back(wrap(new device(root_device)));
158161
}
159162
}
160163
// the wrap function is defined inside dpctl_vector_templ.cpp

dpctl-capi/tests/test_sycl_device_manager.cpp

Lines changed: 72 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -24,10 +24,12 @@
2424
///
2525
//===----------------------------------------------------------------------===//
2626

27+
#include "../helper/include/dpctl_utils_helper.h"
2728
#include "dpctl_sycl_device_interface.h"
2829
#include "dpctl_sycl_device_manager.h"
2930
#include "dpctl_sycl_device_selector_interface.h"
3031
#include <gtest/gtest.h>
32+
#include <string>
3133

3234
struct TestDPCTLDeviceManager : public ::testing::TestWithParam<const char *>
3335
{
@@ -81,12 +83,12 @@ INSTANTIATE_TEST_SUITE_P(DeviceMgrFunctions,
8183
"opencl:cpu:0",
8284
"level_zero:gpu:0"));
8385

84-
struct TestDPCTLDeviceVector : public ::testing::TestWithParam<int>
86+
struct TestDPCTLGetDevices : public ::testing::TestWithParam<int>
8587
{
8688
DPCTLDeviceVectorRef DV = nullptr;
8789
size_t nDevices = 0;
8890

89-
TestDPCTLDeviceVector()
91+
TestDPCTLGetDevices()
9092
{
9193
EXPECT_NO_FATAL_FAILURE(DV = DPCTLDeviceMgr_GetDevices(GetParam()));
9294
EXPECT_TRUE(DV != nullptr);
@@ -100,14 +102,14 @@ struct TestDPCTLDeviceVector : public ::testing::TestWithParam<int>
100102
}
101103
}
102104

103-
~TestDPCTLDeviceVector()
105+
~TestDPCTLGetDevices()
104106
{
105107
EXPECT_NO_FATAL_FAILURE(DPCTLDeviceVector_Clear(DV));
106108
EXPECT_NO_FATAL_FAILURE(DPCTLDeviceVector_Delete(DV));
107109
}
108110
};
109111

110-
TEST_P(TestDPCTLDeviceVector, ChkGetAt)
112+
TEST_P(TestDPCTLGetDevices, ChkGetAt)
111113
{
112114
for (auto i = 0ul; i < nDevices; ++i) {
113115
DPCTLSyclDeviceRef DRef = nullptr;
@@ -118,14 +120,18 @@ TEST_P(TestDPCTLDeviceVector, ChkGetAt)
118120

119121
INSTANTIATE_TEST_SUITE_P(
120122
GetDevices,
121-
TestDPCTLDeviceVector,
123+
TestDPCTLGetDevices,
122124
::testing::Values(DPCTLSyclBackendType::DPCTL_HOST,
123125
DPCTLSyclBackendType::DPCTL_LEVEL_ZERO,
124126
DPCTLSyclBackendType::DPCTL_OPENCL,
125127
DPCTLSyclBackendType::DPCTL_OPENCL |
126128
DPCTLSyclDeviceType::DPCTL_GPU));
127129

128-
TEST(TestDPCTLDeviceVector, ChkDPCTLDeviceVectorCreate)
130+
struct TestDPCTLDeviceVector : public ::testing::Test
131+
{
132+
};
133+
134+
TEST_F(TestDPCTLDeviceVector, ChkDPCTLDeviceVectorCreate)
129135
{
130136
DPCTLDeviceVectorRef DVRef = nullptr;
131137
size_t nDevices = 0;
@@ -135,3 +141,63 @@ TEST(TestDPCTLDeviceVector, ChkDPCTLDeviceVectorCreate)
135141
EXPECT_TRUE(nDevices == 0);
136142
EXPECT_NO_FATAL_FAILURE(DPCTLDeviceVector_Delete(DVRef));
137143
}
144+
145+
struct TestDPCTLGetDevicesOrdering : public ::testing::TestWithParam<int>
146+
{
147+
DPCTLDeviceVectorRef DV = nullptr;
148+
size_t nDevices = 0;
149+
150+
TestDPCTLGetDevicesOrdering()
151+
{
152+
const int device_type_mask =
153+
(GetParam() & DPCTLSyclDeviceType::DPCTL_ALL) |
154+
DPCTLSyclBackendType::DPCTL_ALL_BACKENDS;
155+
EXPECT_NO_FATAL_FAILURE(
156+
DV = DPCTLDeviceMgr_GetDevices(device_type_mask));
157+
EXPECT_TRUE(DV != nullptr);
158+
EXPECT_NO_FATAL_FAILURE(nDevices = DPCTLDeviceVector_Size(DV));
159+
}
160+
161+
void SetUp()
162+
{
163+
if (!nDevices) {
164+
GTEST_SKIP_("Skipping as no devices returned for identifier");
165+
}
166+
}
167+
168+
~TestDPCTLGetDevicesOrdering()
169+
{
170+
EXPECT_NO_FATAL_FAILURE(DPCTLDeviceVector_Clear(DV));
171+
EXPECT_NO_FATAL_FAILURE(DPCTLDeviceVector_Delete(DV));
172+
}
173+
};
174+
175+
TEST_P(TestDPCTLGetDevicesOrdering, ChkConsistencyWithFilterSelector)
176+
{
177+
for (auto i = 0ul; i < nDevices; ++i) {
178+
DPCTLSyclDeviceType Dty;
179+
std::string fs_device_type, fs;
180+
DPCTLSyclDeviceRef DRef = nullptr, D0Ref = nullptr;
181+
DPCTLSyclDeviceSelectorRef DSRef = nullptr;
182+
EXPECT_NO_FATAL_FAILURE(DRef = DPCTLDeviceVector_GetAt(DV, i));
183+
EXPECT_NO_FATAL_FAILURE(Dty = DPCTLDevice_GetDeviceType(DRef));
184+
EXPECT_NO_FATAL_FAILURE(
185+
fs_device_type = DPCTL_DeviceTypeToStr(
186+
DPCTL_DPCTLDeviceTypeToSyclDeviceType(Dty)));
187+
EXPECT_NO_FATAL_FAILURE(fs = fs_device_type + ":" + std::to_string(i));
188+
EXPECT_NO_FATAL_FAILURE(DSRef = DPCTLFilterSelector_Create(fs.c_str()));
189+
EXPECT_NO_FATAL_FAILURE(D0Ref = DPCTLDevice_CreateFromSelector(DSRef));
190+
EXPECT_NO_FATAL_FAILURE(DPCTLDeviceSelector_Delete(DSRef));
191+
EXPECT_TRUE(DPCTLDevice_AreEq(DRef, D0Ref));
192+
EXPECT_NO_FATAL_FAILURE(DPCTLDevice_Delete(D0Ref));
193+
EXPECT_NO_FATAL_FAILURE(DPCTLDevice_Delete(DRef));
194+
}
195+
}
196+
197+
INSTANTIATE_TEST_SUITE_P(
198+
GetDevices,
199+
TestDPCTLGetDevicesOrdering,
200+
::testing::Values(DPCTLSyclDeviceType::DPCTL_HOST_DEVICE,
201+
DPCTLSyclDeviceType::DPCTL_ACCELERATOR,
202+
DPCTLSyclDeviceType::DPCTL_GPU,
203+
DPCTLSyclDeviceType::DPCTL_CPU));

dpctl/tests/test_sycl_device_factory.py

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -177,10 +177,17 @@ def test_get_devices_with_device_type_enum(device_type):
177177

178178

179179
def test_get_devices_with_device_type_str(device_type_str):
180-
devices = dpctl.get_devices(device_type=device_type_str)
181-
if len(devices):
182-
d = string_to_device_type(device_type_str)
183-
check_if_device_type_matches(devices, d)
180+
num_devices = dpctl.get_num_devices(device_type=device_type_str)
181+
if num_devices > 0:
182+
devices = dpctl.get_devices(device_type=device_type_str)
183+
assert len(devices) == num_devices
184+
dty = string_to_device_type(device_type_str)
185+
check_if_device_type_matches(devices, dty)
184186
check_if_device_type_is_valid(devices)
187+
# check for consistency of ordering between filter selector
188+
# where backend is omitted, but device type and id is specified
189+
for i in range(num_devices):
190+
dev = dpctl.SyclDevice(":".join((device_type_str, str(i))))
191+
assert dev == devices[i]
185192
else:
186193
pytest.skip()

0 commit comments

Comments
 (0)