-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathLibXPUInfo_DXCore.cpp
445 lines (390 loc) · 19.3 KB
/
LibXPUInfo_DXCore.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
// Copyright (C) 2024 Intel Corporation
// SPDX-License-Identifier: Apache-2.0
#ifdef XPUINFO_USE_DXCORE
#include <initguid.h> // Must be first for <dxcore.h>
#include "LibXPUInfo.h"
#include "LibXPUInfo_Util.h"
#include "DebugStream.h"
#include <delayimp.h>
#include <iomanip>
// GPU/NPU Detection
// https://github.com/microsoft/Windows-Machine-Learning/blob/master/Tools/WinMLRunner/src/LearningModelDeviceHelper.cpp#L100-L219
#include <unknwn.h>
#include <roerrorapi.h>
using namespace winrt;
#ifndef THROW_IF_FAILED
#define THROW_IF_FAILED(hr) \
{ \
HRESULT localHresult = (hr); \
if (FAILED(localHresult)) \
throw hresult_error(localHresult); \
}
#endif
#include <array>
// Note: dxcore.dll needs to be delay-loaded
//#pragma comment(lib, "dxcore.lib")
// For more advanced error-handling, see https://learn.microsoft.com/en-us/cpp/build/reference/error-handling-and-notification?view=msvc-170
static HRESULT CreateDXCore(IDXCoreAdapterFactory** dxCoreFactory)
{
HRESULT hr = 0;
__try
{
hr = DXCoreCreateAdapterFactory(IID_PPV_ARGS(dxCoreFactory));
}
__except (GetExceptionCode() == VcppException(ERROR_SEVERITY_ERROR, ERROR_MOD_NOT_FOUND)
? EXCEPTION_EXECUTE_HANDLER
: EXCEPTION_CONTINUE_SEARCH)
{
// NOTE: We should never see this because XPUInfo::hasDXCore() is called first
std::cout << "********** DXCore.dll delay-load failure **********\n";
hr = HRESULT_FROM_WIN32(ERROR_MOD_NOT_FOUND);
}
return hr;
}
namespace XI
{
// DXCORE_ADAPTER_ATTRIBUTE_D3D12_GENERIC_ML is defined in new DX headers or Windows SDK >= 26100
// See https://github.com/microsoft/DirectX-Headers/releases/tag/v1.614.0
// Needs to be renamed since it is extern "C"
DEFINE_GUID(LIBXPUINFO_DXCORE_ADAPTER_ATTRIBUTE_D3D12_GENERIC_ML, 0xb71b0d41, 0x1088, 0x422f, 0xa2, 0x7c, 0x2, 0x50, 0xb7, 0xd3, 0xa9, 0x88);
// This one should always be reported for NPUs. Needed for PTL NPU.
DEFINE_GUID(LIBXPUINFO_DXCORE_HARDWARE_TYPE_ATTRIBUTE_NPU, 0xd46140c4, 0xadd7, 0x451b, 0x9e, 0x56, 0x6, 0xfe, 0x8c, 0x3b, 0x58, 0xed);
void XPUInfo::initDXCore(bool updateOnly)
{
constexpr bool bPrintInfo = false;
HRESULT hr = CreateDXCore(m_spFactoryDXCore.put());
THROW_IF_FAILED(hr);
const GUID dxGUIDsOlder[] = { DXCORE_ADAPTER_ATTRIBUTE_D3D12_CORE_COMPUTE };
const GUID dxGUIDsNewer[] = { LIBXPUINFO_DXCORE_ADAPTER_ATTRIBUTE_D3D12_GENERIC_ML };
const GUID dxGUIDsNPU[] = { LIBXPUINFO_DXCORE_HARDWARE_TYPE_ATTRIBUTE_NPU };
THROW_IF_FAILED(
m_spFactoryDXCore->CreateAdapterList(ARRAYSIZE(dxGUIDsOlder), dxGUIDsOlder, IID_PPV_ARGS(m_spAdapterList.put())));
THROW_IF_FAILED(
m_spFactoryDXCore->CreateAdapterList(ARRAYSIZE(dxGUIDsNewer), dxGUIDsNewer, IID_PPV_ARGS(m_spAdapterList2.put())));
THROW_IF_FAILED(
m_spFactoryDXCore->CreateAdapterList(ARRAYSIZE(dxGUIDsNPU), dxGUIDsNPU, IID_PPV_ARGS(m_spAdapterListNPU.put())));
UI64 luidMinPower = 0ULL, luidHighPerf = 0ULL;
auto handleSort = [&luidMinPower, &luidHighPerf](const com_ptr<IDXCoreAdapterList>& adapterList)
{
bool bSortHighPerformance = adapterList->IsAdapterPreferenceSupported(DXCoreAdapterPreference::HighPerformance);
bool bSortMinimumPower = adapterList->IsAdapterPreferenceSupported(DXCoreAdapterPreference::MinimumPower);
if (adapterList->GetAdapterCount() > 0)
{
if (bSortMinimumPower)
{
std::array<DXCoreAdapterPreference, 2> prefs = { DXCoreAdapterPreference::Hardware, DXCoreAdapterPreference::MinimumPower };
adapterList->Sort((uint32_t)prefs.size(), prefs.data());
com_ptr<IDXCoreAdapter> currAdapter = nullptr;
THROW_IF_FAILED(adapterList->GetAdapter(0, currAdapter.put()));
LUID curLUID{};
THROW_IF_FAILED(currAdapter->GetProperty(DXCoreAdapterProperty::InstanceLuid, &curLUID));
luidMinPower = LuidToUI64(curLUID);
}
if (bSortHighPerformance)
{
std::array<DXCoreAdapterPreference, 2> prefs = { DXCoreAdapterPreference::Hardware, DXCoreAdapterPreference::HighPerformance };
adapterList->Sort((uint32_t)prefs.size(), prefs.data());
com_ptr<IDXCoreAdapter> currAdapter = nullptr;
THROW_IF_FAILED(adapterList->GetAdapter(0, currAdapter.put()));
LUID curLUID{};
THROW_IF_FAILED(currAdapter->GetProperty(DXCoreAdapterProperty::InstanceLuid, &curLUID));
luidHighPerf = LuidToUI64(curLUID);
}
}
};
if constexpr (bPrintInfo)
printf("Printing available adapters..\n");
auto handleAdapter = [this, luidHighPerf, luidMinPower, updateOnly, bPrintInfo](com_ptr<IDXCoreAdapter>& currAdapter)
{
// If the adapter is a software adapter then don't consider it for index selection
bool isHardware, isIntegrated;
size_t driverDescriptionSize;
THROW_IF_FAILED(currAdapter->GetPropertySize(DXCoreAdapterProperty::DriverDescription,
&driverDescriptionSize));
std::string driverDescription;
driverDescription.resize(driverDescriptionSize);
THROW_IF_FAILED(currAdapter->GetProperty(DXCoreAdapterProperty::IsHardware, &isHardware));
// This will not succeed for NPUs on some versions of Windows
HRESULT hr = currAdapter->GetProperty(DXCoreAdapterProperty::IsIntegrated, &isIntegrated);
bool isIntegratedValid = SUCCEEDED(hr);
THROW_IF_FAILED(currAdapter->GetProperty(DXCoreAdapterProperty::DriverDescription,
driverDescriptionSize, driverDescription.data()));
// HardwareID
DXCoreHardwareID hwID;
THROW_IF_FAILED(currAdapter->GetProperty(DXCoreAdapterProperty::HardwareID, &hwID));
LUID curLUID{};
THROW_IF_FAILED(currAdapter->GetProperty(DXCoreAdapterProperty::InstanceLuid, &curLUID));
union
{
WORD driverVersion[4];
UI64 driverVersion64;
};
THROW_IF_FAILED(currAdapter->GetProperty(DXCoreAdapterProperty::DriverVersion,
sizeof(LARGE_INTEGER), driverVersion));
bool isGraphics = currAdapter->IsAttributeSupported(DXCORE_ADAPTER_ATTRIBUTE_D3D12_GRAPHICS);
//bool isNPU = currAdapter->IsAttributeSupported(LIBXPUINFO_DXCORE_HARDWARE_TYPE_ATTRIBUTE_NPU); // Unused, works
uint64_t DedicatedAdapterMemory;
THROW_IF_FAILED(currAdapter->GetProperty(DXCoreAdapterProperty::DedicatedAdapterMemory, &DedicatedAdapterMemory));
uint64_t DedicatedSystemMemory;
THROW_IF_FAILED(currAdapter->GetProperty(DXCoreAdapterProperty::DedicatedSystemMemory, &DedicatedSystemMemory));
uint64_t SharedSystemMemory;
THROW_IF_FAILED(currAdapter->GetProperty(DXCoreAdapterProperty::SharedSystemMemory, &SharedSystemMemory));
if (isHardware)
{
DXGI_ADAPTER_DESC1 desc1{};
std::wstring descW = convert(driverDescription);
lstrcpynW(desc1.Description, descW.c_str(), 128);
desc1.VendorId = hwID.vendorID;
desc1.DeviceId = hwID.deviceID;
desc1.SubSysId = hwID.subSysID;
desc1.Revision = hwID.revision;
desc1.DedicatedVideoMemory = DedicatedAdapterMemory;
desc1.DedicatedSystemMemory = DedicatedSystemMemory;
desc1.SharedSystemMemory = SharedSystemMemory;
desc1.AdapterLuid = curLUID;
if (bPrintInfo)
{
printf("Description: %s (%s), LUID=0x%llx, Version %hu.%hu.%hu.%hu\n", driverDescription.c_str(),
isIntegratedValid ? (isIntegrated ? "Integrated" : "Discrete") : "UNKNOWN_UMA",
*(uint64_t*)&curLUID,
driverVersion[3],
driverVersion[2],
driverVersion[1],
driverVersion[0]
);
printf("\tDedicated Video: %.3lf, System: %.3lf, Shared: %.3lf\n",
DedicatedAdapterMemory / (1024 * 1024 * 1024.0),
DedicatedSystemMemory / (1024 * 1024 * 1024.0),
SharedSystemMemory / (1024 * 1024 * 1024.0));
}
// TODO: Does the budget and available amount below account for non-DX12 usage?
if (currAdapter->IsQueryStateSupported(DXCoreAdapterState::AdapterMemoryBudget))
{
DXCoreAdapterMemoryBudgetNodeSegmentGroup nsg{};
DXCoreAdapterMemoryBudget memBudget;
THROW_IF_FAILED(currAdapter->QueryState(DXCoreAdapterState::AdapterMemoryBudget, &nsg, &memBudget));
if (bPrintInfo)
{
if (memBudget.budget)
{
printf("\tLocal Mem:\n");
printf("\t\tbudget: %.2lf\n", memBudget.budget / (1024 * 1024 * 1024.0));
printf("\t\tcurrentUsage: %.2lf\n", memBudget.currentUsage / (1024 * 1024 * 1024.0));
printf("\t\tavailableForReservation: %.2lf\n", memBudget.availableForReservation / (1024 * 1024 * 1024.0));
printf("\t\tcurrentReservation: %.2lf\n", memBudget.currentReservation / (1024 * 1024 * 1024.0));
}
}
nsg.segmentGroup = DXCoreSegmentGroup::NonLocal;
THROW_IF_FAILED(currAdapter->QueryState(DXCoreAdapterState::AdapterMemoryBudget, &nsg, &memBudget));
if (bPrintInfo)
{
if (memBudget.budget)
{
printf("\tNonLocal Mem:\n");
printf("\t\tbudget: %.2lf\n", memBudget.budget / (1024 * 1024 * 1024.0));
printf("\t\tcurrentUsage: %.2lf\n", memBudget.currentUsage / (1024 * 1024 * 1024.0));
printf("\t\tavailableForReservation: %.2lf\n", memBudget.availableForReservation / (1024 * 1024 * 1024.0));
printf("\t\tcurrentReservation: %.2lf\n", memBudget.currentReservation / (1024 * 1024 * 1024.0));
}
}
}
if (bPrintInfo)
printf("\n");
DeviceType devType = isGraphics ? DEVICE_TYPE_GPU : DEVICE_TYPE_NPU;
DevicePtr newDevice(new Device((UI32)m_Devices.size(), &desc1, devType, API_TYPE_DXCORE, driverVersion64));
if (!!newDevice)
{
bool isHighPerf = luidHighPerf && (newDevice->getLUID() == luidHighPerf);
bool isMinPower = luidMinPower && (newDevice->getLUID() == luidMinPower);
auto newIt = m_Devices.find(newDevice->getLUID());
if (newIt != m_Devices.end())
{
// Update
//
// Verify driver version
if (newDevice->driverVersion().Valid())
{
if (newDevice->driverVersion().GetAsUI64() !=
driverVersion64)
{
DebugStream dStr(true);
dStr << "ERROR: driverVersion mismatch!";
}
}
if (newIt->second->getType() != devType)
{
DebugStream dStr(true);
dStr << "ERROR: DeviceType mismatch!";
}
newIt->second->initDXCoreDevice(currAdapter, isHighPerf, isMinPower);
m_UsedAPIs = m_UsedAPIs | API_TYPE_DXCORE;
}
else if (!updateOnly)
{
// Add
auto insertResult = m_Devices.insert(std::make_pair(newDevice->getLUID(), newDevice));
if (insertResult.second)
{
insertResult.first->second->initDXCoreDevice(currAdapter, isHighPerf, isMinPower);
m_UsedAPIs = m_UsedAPIs | API_TYPE_DXCORE;
}
}
} // newDevice valid
}
};
const std::vector<decltype(m_spAdapterList)> adapterLists{ m_spAdapterList, m_spAdapterList2, m_spAdapterListNPU };
for (const auto& list : adapterLists)
{
handleSort(list);
for (UINT i = 0; i < list->GetAdapterCount(); i++)
{
com_ptr<IDXCoreAdapter> currAdapter = nullptr;
THROW_IF_FAILED(list->GetAdapter(i, currAdapter.put()));
handleAdapter(currAdapter);
}
}
}
void Device::initDXCoreDevice(com_ptr<IDXCoreAdapter>& pDXCoreAdapter, bool bHighPerf, bool bMinPower)
{
m_pDXCoreAdapter = pDXCoreAdapter;
// HighPerformance, MinimumPower
bool isIntegrated;
bool isDetachable;
HRESULT hr = pDXCoreAdapter->GetProperty(DXCoreAdapterProperty::IsIntegrated, &isIntegrated);
bool isIntegratedValid = SUCCEEDED(hr);
THROW_IF_FAILED(pDXCoreAdapter->GetProperty(DXCoreAdapterProperty::IsDetachable, &isDetachable));
validAPIs = validAPIs | API_TYPE_DXCORE;
if (isIntegratedValid)
{
updateIfDstVal(m_props.UMA, UMA_UNKNOWN,
isIntegrated ? UMA_INTEGRATED : NONUMA_DISCRETE);
}
updateIfDstNotSet(m_props.IsHighPerformance, I8(bHighPerf));
updateIfDstNotSet(m_props.IsMinimumPower, I8(bMinPower));
updateIfDstNotSet(m_props.IsDetachable, I8(isDetachable));
}
DXCoreAdapterMemoryBudget Device::getMemUsage_DXCORE() const
{
DXCoreAdapterMemoryBudget memUsage{};
DXCoreAdapterMemoryBudgetNodeSegmentGroup nsg{}; // Want all-zero
if (XPUInfo::hasDXCore())
{
XPUINFO_REQUIRE(m_pDXCoreAdapter);
THROW_IF_FAILED(m_pDXCoreAdapter->QueryState(DXCoreAdapterState::AdapterMemoryBudget, &nsg, &memUsage));
}
return memUsage;
}
void ScopedRegisterNotification::DXCoreNotificationCallback(DXCoreNotificationType notificationType,
IUnknown* object,
void* context)
{
const ScopedRegisterNotification* pClass = reinterpret_cast<const ScopedRegisterNotification*>(context);
if (pClass->m_NotificationFunc)
{
std::lock_guard<std::mutex> lock(pClass->m_NotificationMutex);
pClass->m_NotificationFunc(notificationType, object, pClass->m_pXI);
}
}
void ScopedRegisterNotification::ExampleNotificationFunc_DXCORE(DXCoreNotificationType notificationType, IUnknown* object, const XPUInfo* pXI)
{
switch (notificationType)
{
case DXCoreNotificationType::AdapterListStale:
{
std::cout << __FUNCTION__ << ": DXCORE Adapter List Changed" << std::endl;
}
break;
case DXCoreNotificationType::AdapterNoLongerValid:
{
winrt::com_ptr<IDXCoreAdapter> pAdapter;
winrt::check_hresult(object->QueryInterface(pAdapter.put()));
LUID curLUID{};
winrt::check_hresult(pAdapter->GetProperty(DXCoreAdapterProperty::InstanceLuid, &curLUID));
auto xiDev = pXI->getDevice(LuidToUI64(curLUID));
std::cout << __FUNCTION__ << ": DXCORE DEVICE LOST: " << convert(xiDev->name()) << std::endl;
}
break;
case DXCoreNotificationType::AdapterBudgetChange:
{
SaveRestoreIOSFlags srFlags(std::cout);
winrt::com_ptr<IDXCoreAdapter> pAdapter;
winrt::check_hresult(object->QueryInterface(pAdapter.put()));
LUID curLUID{};
winrt::check_hresult(pAdapter->GetProperty(DXCoreAdapterProperty::InstanceLuid, &curLUID));
auto xiDev = pXI->getDevice(LuidToUI64(curLUID));
auto newBudget = xiDev->getMemUsage();
std::cout << __FUNCTION__ << ": Budget changed for device " << convert(xiDev->name()) <<
" to " << std::setprecision(4) << newBudget.budget / (1024.0 * 1024*1024) << " GB" << std::endl;
}
break;
default:
XPUINFO_REQUIRE(false);
}
}
void ScopedRegisterNotification::register_DXCORE(UI64 deviceLUID)
{
XPUINFO_REQUIRE(m_pXI);
auto& factory = m_pXI->getDXCoreFactory();
static_assert((int)DXCoreNotificationType::AdapterBudgetChange < 4 /*m_DXCoreEventCookies.size()*/, "Invalid assumption");
auto pDev = m_pXI->getDevice(deviceLUID);
XPUINFO_REQUIRE(pDev);
auto adapter = pDev->getHandle_DXCore();
// TODO: Also handle m_spAdapterList2
auto spAdapterList = m_pXI->getDXCoreAdapterList();
if (factory && adapter && spAdapterList)
{
HRESULT hr = factory->RegisterEventNotification(spAdapterList.get(),
DXCoreNotificationType::AdapterListStale,
DXCoreNotificationCallback,
this,
&m_DXCoreEventCookies[(int)DXCoreNotificationType::AdapterListStale]
);
winrt::check_hresult(hr);
hr = factory->RegisterEventNotification(adapter,
DXCoreNotificationType::AdapterNoLongerValid,
DXCoreNotificationCallback,
this,
&m_DXCoreEventCookies[(int)DXCoreNotificationType::AdapterNoLongerValid]
);
winrt::check_hresult(hr);
if (m_flags & DXCORE_MEM_BUDGET)
{
hr = factory->RegisterEventNotification(adapter,
DXCoreNotificationType::AdapterBudgetChange,
DXCoreNotificationCallback,
this,
&m_DXCoreEventCookies[(int)DXCoreNotificationType::AdapterBudgetChange]
);
winrt::check_hresult(hr);
}
}
if (m_flags & DXCORE_MEM_BUDGET)
{
m_bRegisteredAdapterBudgetChange = true;
}
m_bRegisteredEvents = true;
}
void ScopedRegisterNotification::unregister_DXCORE()
{
if (!m_bRegisteredEvents)
{
return;
}
auto& factory = m_pXI->getDXCoreFactory();
if (factory && m_bRegisteredEvents)
{
winrt::check_hresult(factory->UnregisterEventNotification(m_DXCoreEventCookies[(int)DXCoreNotificationType::AdapterListStale]));
winrt::check_hresult(factory->UnregisterEventNotification(m_DXCoreEventCookies[(int)DXCoreNotificationType::AdapterNoLongerValid]));
if (m_bRegisteredAdapterBudgetChange)
{
winrt::check_hresult(factory->UnregisterEventNotification(m_DXCoreEventCookies[(int)DXCoreNotificationType::AdapterBudgetChange]));
}
if (m_bRegisteredAdapterBudgetChange)
{
m_bRegisteredAdapterBudgetChange = false;
}
m_bRegisteredEvents = false;
}
}
} // namespace XI
#endif // #ifdef XPUINFO_USE_DXCORE