@@ -66,6 +66,37 @@ std::string get_device_info_str(const device &Device)
66
66
return ss.str ();
67
67
}
68
68
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
+
69
100
struct DeviceCacheBuilder
70
101
{
71
102
using DeviceCache = std::unordered_map<device, context>;
@@ -146,12 +177,18 @@ DPCTLDeviceMgr_GetDevices(int device_identifier)
146
177
{
147
178
std::vector<DPCTLSyclDeviceRef> *Devices = nullptr ;
148
179
180
+ device_identifier = canonicalize_device_id (device_identifier);
181
+
149
182
try {
150
183
Devices = new std::vector<DPCTLSyclDeviceRef>();
151
184
} catch (std::bad_alloc const &ba) {
152
185
delete Devices;
153
186
return nullptr ;
154
187
}
188
+
189
+ if (!device_identifier)
190
+ return wrap (Devices);
191
+
155
192
const auto &root_devices = device::get_devices ();
156
193
default_selector mRanker ;
157
194
@@ -195,6 +232,10 @@ int DPCTLDeviceMgr_GetPositionInDevices(__dpctl_keep DPCTLSyclDeviceRef DRef,
195
232
return not_found;
196
233
}
197
234
235
+ device_identifier = canonicalize_device_id (device_identifier);
236
+ if (!device_identifier)
237
+ return not_found;
238
+
198
239
const auto &root_devices = device::get_devices ();
199
240
default_selector mRanker ;
200
241
int index = not_found;
@@ -225,21 +266,10 @@ size_t DPCTLDeviceMgr_GetNumDevices(int device_identifier)
225
266
size_t nDevices = 0 ;
226
267
auto &cache = DeviceCacheBuilder::getDeviceCache ();
227
268
228
- // If the identifier is 0 (UNKNOWN_DEVICE) return 0.
269
+ device_identifier = canonicalize_device_id (device_identifier);
229
270
if (!device_identifier)
230
271
return 0 ;
231
272
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
-
243
273
for (const auto &entry : cache) {
244
274
auto Bty (DPCTL_SyclBackendToDPCTLBackendType (
245
275
entry.first .get_platform ().get_backend ()));
0 commit comments