Skip to content

Commit 5538c76

Browse files
wlemkowsbb-ur
authored andcommitted
Refine default device logic in urDeviceGet (#20036)
1 parent 6824a6c commit 5538c76

1 file changed

Lines changed: 35 additions & 5 deletions

File tree

source/adapters/level_zero/device.cpp

Lines changed: 35 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -104,6 +104,13 @@ ur_result_t urDeviceGet(
104104
// nullptr in this mode.
105105
// - If COMBINED, L0 returns tiles as devices, and zeDeviceGetRootdevice
106106
// returns the card containing a given tile.
107+
108+
// Track best discrete and integrated GPU candidates (device, max compute
109+
// units)
110+
std::pair<ur_device_handle_t, uint32_t> GPUDeviceDiscrete = {nullptr, 0};
111+
std::pair<ur_device_handle_t, uint32_t> GPUDeviceIntegrated = {nullptr, 0};
112+
bool DeviceDefaultGPU = false;
113+
107114
bool isCombinedMode =
108115
std::any_of(Platform->URDevicesCache.begin(),
109116
Platform->URDevicesCache.end(), [](const auto &D) {
@@ -133,8 +140,11 @@ ur_result_t urDeviceGet(
133140
Matched = true;
134141
break;
135142
case UR_DEVICE_TYPE_GPU:
143+
Matched = (D->ZeDeviceProperties->type == ZE_DEVICE_TYPE_GPU);
144+
break;
136145
case UR_DEVICE_TYPE_DEFAULT:
137146
Matched = (D->ZeDeviceProperties->type == ZE_DEVICE_TYPE_GPU);
147+
DeviceDefaultGPU = true;
138148
break;
139149
case UR_DEVICE_TYPE_CPU:
140150
Matched = (D->ZeDeviceProperties->type == ZE_DEVICE_TYPE_CPU);
@@ -156,15 +166,35 @@ ur_result_t urDeviceGet(
156166
isCombinedMode && (D->ZeDeviceProperties->flags &
157167
ZE_DEVICE_PROPERTY_FLAG_SUBDEVICE) == 0;
158168
if (!isComposite) {
159-
MatchedDevices.push_back(D.get());
160-
// For UR_DEVICE_TYPE_DEFAULT only a single device should be returned,
161-
// so exit the loop after first proper match.
162-
if (DeviceType == UR_DEVICE_TYPE_DEFAULT)
163-
break;
169+
// In case of DeviceType is DEFAULT, pick only the most powerful GPU
170+
// device.
171+
if (DeviceDefaultGPU) {
172+
uint32_t maxComputeUnits = 0;
173+
ur_result_t UrRet = ur::level_zero::urDeviceGetInfo(
174+
D.get(), UR_DEVICE_INFO_MAX_COMPUTE_UNITS,
175+
sizeof(maxComputeUnits), &maxComputeUnits, nullptr);
176+
maxComputeUnits = (UrRet == UR_RESULT_SUCCESS) ? maxComputeUnits : 0;
177+
auto &BestGpu =
178+
D->isIntegrated() ? GPUDeviceIntegrated : GPUDeviceDiscrete;
179+
if (!BestGpu.first || maxComputeUnits > BestGpu.second)
180+
BestGpu = std::make_pair(D.get(), maxComputeUnits);
181+
} else {
182+
MatchedDevices.push_back(D.get());
183+
}
164184
}
165185
}
166186
}
167187

188+
// Handle GPU/DEFAULT device selection outside the loop
189+
if (DeviceDefaultGPU) {
190+
// Prefer discrete GPU over integrated GPU
191+
if (GPUDeviceDiscrete.first) {
192+
MatchedDevices = {GPUDeviceDiscrete.first};
193+
} else if (GPUDeviceIntegrated.first) {
194+
MatchedDevices = {GPUDeviceIntegrated.first};
195+
}
196+
}
197+
168198
uint32_t ZeDeviceCount = MatchedDevices.size();
169199

170200
auto N = (std::min)(ZeDeviceCount, NumEntries);

0 commit comments

Comments
 (0)