Skip to content

Commit 49ded41

Browse files
Merge pull request #623 from diptorupd/fix/issue_601
Correct the logic to return device count.
2 parents 711f868 + 32a2425 commit 49ded41

File tree

6 files changed

+109
-12
lines changed

6 files changed

+109
-12
lines changed

dpctl-capi/source/dpctl_sycl_device_manager.cpp

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -66,6 +66,39 @@ std::string get_device_info_str(const device &Device)
6666
return ss.str();
6767
}
6868

69+
/*!
70+
* @brief Canonicalizes a device identifier bit flag to have a valid (i.e., not
71+
* UNKNOWN) backend and device type bits.
72+
*
73+
* The device id is bit flag that indicates the backend and device type, both
74+
* of which are optional, that are to be queried. The function makes sure if a
75+
* device identifier only provides a device type value the backend is set to
76+
* DPCTL_ALL_BACKENDS. Similarly, if only backend is provided the device type
77+
* is set to DPCTL_ALL.
78+
*
79+
* @param device_id A bit flag storing a backend and a device type value.
80+
* @return Canonicalized bit flag that makes sure neither backend nor device
81+
* type is UNKNOWN (0). For cases where the input device id does not provide
82+
* either one of the values, we set the value to ALL.
83+
*/
84+
int to_canonical_device_id(int device_id)
85+
{ // If the identifier is 0 (UNKNOWN_DEVICE) return 0.
86+
if (!device_id)
87+
return 0;
88+
89+
// Check if the device identifier has a backend specified. If not, then
90+
// toggle all backend specifier bits, i.e. set the backend to
91+
// DPCTL_ALL_BACKENDS.
92+
if (!(device_id & DPCTL_ALL_BACKENDS))
93+
device_id |= DPCTL_ALL_BACKENDS;
94+
95+
// Check if a device type was specified. If not, set device type to ALL.
96+
if (!(device_id & ~DPCTL_ALL_BACKENDS))
97+
device_id |= DPCTL_ALL;
98+
99+
return device_id;
100+
}
101+
69102
struct DeviceCacheBuilder
70103
{
71104
using DeviceCache = std::unordered_map<device, context>;
@@ -146,12 +179,18 @@ DPCTLDeviceMgr_GetDevices(int device_identifier)
146179
{
147180
std::vector<DPCTLSyclDeviceRef> *Devices = nullptr;
148181

182+
device_identifier = to_canonical_device_id(device_identifier);
183+
149184
try {
150185
Devices = new std::vector<DPCTLSyclDeviceRef>();
151186
} catch (std::bad_alloc const &ba) {
152187
delete Devices;
153188
return nullptr;
154189
}
190+
191+
if (!device_identifier)
192+
return wrap(Devices);
193+
155194
const auto &root_devices = device::get_devices();
156195
default_selector mRanker;
157196

@@ -195,6 +234,10 @@ int DPCTLDeviceMgr_GetPositionInDevices(__dpctl_keep DPCTLSyclDeviceRef DRef,
195234
return not_found;
196235
}
197236

237+
device_identifier = to_canonical_device_id(device_identifier);
238+
if (!device_identifier)
239+
return not_found;
240+
198241
const auto &root_devices = device::get_devices();
199242
default_selector mRanker;
200243
int index = not_found;
@@ -224,6 +267,11 @@ size_t DPCTLDeviceMgr_GetNumDevices(int device_identifier)
224267
{
225268
size_t nDevices = 0;
226269
auto &cache = DeviceCacheBuilder::getDeviceCache();
270+
271+
device_identifier = to_canonical_device_id(device_identifier);
272+
if (!device_identifier)
273+
return 0;
274+
227275
for (const auto &entry : cache) {
228276
auto Bty(DPCTL_SyclBackendToDPCTLBackendType(
229277
entry.first.get_platform().get_backend()));

dpctl-capi/tests/test_sycl_device_manager.cpp

Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -137,6 +137,57 @@ INSTANTIATE_TEST_SUITE_P(
137137
DPCTLSyclBackendType::DPCTL_OPENCL |
138138
DPCTLSyclDeviceType::DPCTL_GPU));
139139

140+
struct TestGetNumDevicesForDTy : public ::testing::TestWithParam<int>
141+
{
142+
size_t nDevices = 0;
143+
TestGetNumDevicesForDTy()
144+
{
145+
cl::sycl::info::device_type sycl_dty =
146+
DPCTL_DPCTLDeviceTypeToSyclDeviceType(
147+
DPCTLSyclDeviceType(GetParam()));
148+
149+
auto devices = cl::sycl::device::get_devices(sycl_dty);
150+
EXPECT_TRUE(devices.size() == DPCTLDeviceMgr_GetNumDevices(GetParam()));
151+
}
152+
};
153+
154+
INSTANTIATE_TEST_SUITE_P(
155+
GetDevices,
156+
TestGetNumDevicesForDTy,
157+
::testing::Values(DPCTLSyclDeviceType::DPCTL_ACCELERATOR,
158+
DPCTLSyclDeviceType::DPCTL_ALL,
159+
DPCTLSyclDeviceType::DPCTL_CPU,
160+
DPCTLSyclDeviceType::DPCTL_GPU,
161+
DPCTLSyclDeviceType::DPCTL_HOST_DEVICE));
162+
163+
struct TestGetNumDevicesForBTy : public ::testing::TestWithParam<int>
164+
{
165+
size_t nDevices = 0;
166+
TestGetNumDevicesForBTy()
167+
{
168+
cl::sycl::backend sycl_bty = DPCTL_DPCTLBackendTypeToSyclBackend(
169+
DPCTLSyclBackendType(GetParam()));
170+
171+
auto platforms = cl::sycl::platform::get_platforms();
172+
for (const auto &P : platforms) {
173+
if (P.get_backend() == sycl_bty) {
174+
auto devices = P.get_devices();
175+
EXPECT_TRUE(devices.size() ==
176+
DPCTLDeviceMgr_GetNumDevices(GetParam()));
177+
}
178+
}
179+
}
180+
};
181+
182+
INSTANTIATE_TEST_SUITE_P(
183+
GetDevices,
184+
TestGetNumDevicesForBTy,
185+
::testing::Values(DPCTLSyclBackendType::DPCTL_CUDA,
186+
DPCTLSyclBackendType::DPCTL_ALL_BACKENDS,
187+
DPCTLSyclBackendType::DPCTL_HOST,
188+
DPCTLSyclBackendType::DPCTL_LEVEL_ZERO,
189+
DPCTLSyclBackendType::DPCTL_OPENCL));
190+
140191
struct TestDPCTLDeviceVector : public ::testing::Test
141192
{
142193
};

dpctl/_sycl_device.pyx

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -351,7 +351,7 @@ cdef class SyclDevice(_SyclDevice):
351351
elif DTy == _device_type._GPU:
352352
return device_type.gpu
353353
elif DTy == _device_type._HOST_DEVICE:
354-
return device_type.host_device
354+
return device_type.host
355355
else:
356356
raise ValueError("Unknown device type.")
357357

@@ -1014,8 +1014,7 @@ cdef class SyclDevice(_SyclDevice):
10141014
cdef int64_t relId = -1
10151015

10161016
DTy = DPCTLDevice_GetDeviceType(self._device_ref)
1017-
relId = DPCTLDeviceMgr_GetPositionInDevices(
1018-
self._device_ref, _backend_type._ALL_BACKENDS | DTy)
1017+
relId = DPCTLDeviceMgr_GetPositionInDevices(self._device_ref, DTy)
10191018
return relId
10201019

10211020
cdef int get_backend_ordinal(self):
@@ -1032,8 +1031,7 @@ cdef class SyclDevice(_SyclDevice):
10321031
cdef int64_t relId = -1
10331032

10341033
BTy = DPCTLDevice_GetBackend(self._device_ref)
1035-
relId = DPCTLDeviceMgr_GetPositionInDevices(
1036-
self._device_ref, BTy | _device_type._ALL_DEVICES)
1034+
relId = DPCTLDeviceMgr_GetPositionInDevices(self._device_ref, BTy)
10371035
return relId
10381036

10391037
cdef int get_overall_ordinal(self):

dpctl/_sycl_device_factory.pyx

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -94,7 +94,7 @@ cdef _device_type _string_to_dpctl_sycl_device_ty(str dty_str):
9494
return _device_type._CUSTOM
9595
elif dty_str == "gpu":
9696
return _device_type._GPU
97-
elif dty_str == "host_device":
97+
elif dty_str == "host":
9898
return _device_type._HOST_DEVICE
9999
else:
100100
return _device_type._UNKNOWN_DEVICE
@@ -128,7 +128,7 @@ cdef _device_type _enum_to_dpctl_sycl_device_ty(DTy):
128128
return _device_type._CUSTOM
129129
elif DTy == device_type_t.gpu:
130130
return _device_type._GPU
131-
elif DTy == device_type_t.host_device:
131+
elif DTy == device_type_t.host:
132132
return _device_type._HOST_DEVICE
133133
else:
134134
return _device_type._UNKNOWN_DEVICE
@@ -164,7 +164,7 @@ cpdef list get_devices(backend=backend_type.all, device_type=device_type_t.all):
164164
device_type (optional): Defaults to ``dpctl.device_type.all``.
165165
A :class:`dpctl.device_type` enum value or a string that
166166
specifies a SYCL device type. Currently, accepted values are:
167-
"gpu", "cpu", "accelerator", "host_device", or "all".
167+
"gpu", "cpu", "accelerator", "host", or "all".
168168
Returns:
169169
list: A list of available :class:`dpctl.SyclDevice` instances that
170170
satisfy the provided :class:`dpctl.backend_type` and
@@ -217,7 +217,7 @@ cpdef int get_num_devices(
217217
device_type (optional): Defaults to ``dpctl.device_type.all``.
218218
A :class:`dpctl.device_type` enum value or a string that
219219
specifies a SYCL device type. Currently, accepted values are:
220-
"gpu", "cpu", "accelerator", "host_device", or "all".
220+
"gpu", "cpu", "accelerator", "host", or "all".
221221
Returns:
222222
int: The number of available SYCL devices that satisfy the provided
223223
:class:`dpctl.backend_type` and :class:`dpctl.device_type` values.

dpctl/enum_types.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@ class device_type(Enum):
3939
gpu 1
4040
cpu 2
4141
accelerator 3
42-
host_device 4
42+
host 4
4343
================== ============
4444
"""
4545

@@ -49,7 +49,7 @@ class device_type(Enum):
4949
cpu = auto()
5050
custom = auto()
5151
gpu = auto()
52-
host_device = auto()
52+
host = auto()
5353

5454

5555
class backend_type(Enum):

dpctl/tests/test_sycl_device_factory.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@
2727
(bty.level_zero, dty.gpu),
2828
(bty.opencl, dty.gpu),
2929
(bty.opencl, dty.cpu),
30-
(bty.host, dty.host_device),
30+
(bty.host, dty.host),
3131
]
3232

3333
argument_list_2 = [

0 commit comments

Comments
 (0)