Skip to content

Commit ad6eabf

Browse files
author
Diptorup Deb
committed
Add a helper function to canonicalize device identifiers.
1 parent ce37749 commit ad6eabf

File tree

1 file changed

+42
-12
lines changed

1 file changed

+42
-12
lines changed

dpctl-capi/source/dpctl_sycl_device_manager.cpp

Lines changed: 42 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -66,6 +66,37 @@ 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 My Param doc
80+
* @return {return} My Param doc
81+
*/
82+
int canonicalize_device_id(int device_id)
83+
{ // If the identifier is 0 (UNKNOWN_DEVICE) return 0.
84+
if (!device_id)
85+
return 0;
86+
87+
// Check if the device identifier has a backend specified. If not, then
88+
// toggle all backend specifier bits, i.e. set the backend to
89+
// DPCTL_ALL_BACKENDS.
90+
if (!(device_id & DPCTL_ALL_BACKENDS))
91+
device_id |= DPCTL_ALL_BACKENDS;
92+
93+
// Check if a device type was specified. If not, set device type to ALL.
94+
if (!(device_id & ~DPCTL_ALL_BACKENDS))
95+
device_id |= DPCTL_ALL;
96+
97+
return device_id;
98+
}
99+
69100
struct DeviceCacheBuilder
70101
{
71102
using DeviceCache = std::unordered_map<device, context>;
@@ -146,12 +177,18 @@ DPCTLDeviceMgr_GetDevices(int device_identifier)
146177
{
147178
std::vector<DPCTLSyclDeviceRef> *Devices = nullptr;
148179

180+
device_identifier = canonicalize_device_id(device_identifier);
181+
149182
try {
150183
Devices = new std::vector<DPCTLSyclDeviceRef>();
151184
} catch (std::bad_alloc const &ba) {
152185
delete Devices;
153186
return nullptr;
154187
}
188+
189+
if (!device_identifier)
190+
return wrap(Devices);
191+
155192
const auto &root_devices = device::get_devices();
156193
default_selector mRanker;
157194

@@ -195,6 +232,10 @@ int DPCTLDeviceMgr_GetPositionInDevices(__dpctl_keep DPCTLSyclDeviceRef DRef,
195232
return not_found;
196233
}
197234

235+
device_identifier = canonicalize_device_id(device_identifier);
236+
if (!device_identifier)
237+
return not_found;
238+
198239
const auto &root_devices = device::get_devices();
199240
default_selector mRanker;
200241
int index = not_found;
@@ -225,21 +266,10 @@ size_t DPCTLDeviceMgr_GetNumDevices(int device_identifier)
225266
size_t nDevices = 0;
226267
auto &cache = DeviceCacheBuilder::getDeviceCache();
227268

228-
// If the identifier is 0 (UNKNOWN_DEVICE) return 0.
269+
device_identifier = canonicalize_device_id(device_identifier);
229270
if (!device_identifier)
230271
return 0;
231272

232-
// Check if the device identifier has a backend specified. If not, then
233-
// make sure we match with all backends. The lower 6 bits are for the
234-
// device types, so we blank out those bits to check if any of the
235-
// higher bits reserved for the backend types was non-zero.
236-
if (!((device_identifier & DPCTL_ALL_BACKENDS) >> 6))
237-
device_identifier |= DPCTL_ALL_BACKENDS;
238-
239-
// Check if a device type was specified. If not, set device type to ALL.
240-
if (!(device_identifier & ~DPCTL_ALL_BACKENDS))
241-
device_identifier |= DPCTL_ALL;
242-
243273
for (const auto &entry : cache) {
244274
auto Bty(DPCTL_SyclBackendToDPCTLBackendType(
245275
entry.first.get_platform().get_backend()));

0 commit comments

Comments
 (0)