-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathLibXPUInfo_NVML.cpp
268 lines (243 loc) · 8.76 KB
/
LibXPUInfo_NVML.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
// Copyright (C) 2024 Intel Corporation
// SPDX-License-Identifier: Apache-2.0
/* To enable NVML,
1. Get package from https://developer.download.nvidia.com/compute/cuda/redist/cuda_nvml_dev/windows-x86_64/, currently cuda_nvml_dev-windows-x86_64-12.5.39-archive.zip
2. Extract contents to $(SolutionDir)external\NVML
3. Add XPUINFO_USE_NVML to preprocessor definitions for LibXPUInfo project
*/
#ifdef XPUINFO_USE_NVML
#include "LibXPUInfo.h"
#include "DebugStream.h"
#include "LibXPUInfo_Util.h"
#include "nvml.h"
#pragma comment(lib, "nvml.lib")
#ifdef _WIN32
#include <delayimp.h>
#endif
#if 0 // For experimenting with NVML
#define PRINT_IF_SUCCESS(var, funcName) if (NVML_SUCCESS==result) std::cout << #funcName << " -> " << #var << ": " << var << std::endl;
#else
#define PRINT_IF_SUCCESS(var, funcName)
#endif
namespace XI
{
void Device::initNVMLDevice(nvmlDevice_t device)
{
nvmlReturn_t result;
char name[NVML_DEVICE_NAME_BUFFER_SIZE];
String nameStr;
UI32 LinkGen = 0;
UI32 LinkWidth = 0;
result = nvmlDeviceGetName(device, name, NVML_DEVICE_NAME_BUFFER_SIZE);
if (NVML_SUCCESS == result)
{
nameStr = name;
}
// Ampere has 64 or 128 CUDA cores per SM
UI32 numGPUCores = 0;
result = nvmlDeviceGetNumGpuCores(device, &numGPUCores);
if (NVML_SUCCESS == result)
{
// Overwrite OpenCL result which is SM, use CUDA cores
m_props.NumComputeUnits = (I32)numGPUCores;
}
nvmlEnableState_t mode = NVML_FEATURE_DISABLED;
result = nvmlDeviceGetPowerManagementMode(device, &mode);
if (NVML_SUCCESS == result)
{
}
UI32 powerLimit = 0;
UI32 minPowerLimit = 0;
result = nvmlDeviceGetPowerUsage(device, &powerLimit); // Seems to be current power draw
PRINT_IF_SUCCESS(powerLimit, nvmlDeviceGetPowerUsage);
result = nvmlDeviceGetPowerManagementDefaultLimit(device, &powerLimit); // dnw
PRINT_IF_SUCCESS(powerLimit, nvmlDeviceGetPowerManagementDefaultLimit);
result = nvmlDeviceGetPowerManagementLimit(device, &powerLimit); // dnw
PRINT_IF_SUCCESS(powerLimit, nvmlDeviceGetPowerManagementLimit);
result = nvmlDeviceGetPowerManagementLimitConstraints(device, &minPowerLimit, &powerLimit); // dnw
PRINT_IF_SUCCESS(minPowerLimit, nvmlDeviceGetPowerManagementLimitConstraints);
PRINT_IF_SUCCESS(powerLimit, nvmlDeviceGetPowerManagementLimitConstraints);
result = nvmlDeviceGetEnforcedPowerLimit(device, &powerLimit); // dnw - should the the one we want
PRINT_IF_SUCCESS(powerLimit, nvmlDeviceGetEnforcedPowerLimit);
if (NVML_SUCCESS == result)
{
updateIfDstNotSet(m_props.PackageTDP, (I32)(powerLimit / 1000));
}
UI32 busWidth = 0;
result = nvmlDeviceGetMemoryBusWidth(device, &busWidth);
PRINT_IF_SUCCESS(busWidth, nvmlDeviceGetMemoryBusWidth);
nvmlMemory_t memoryInfo;
result = nvmlDeviceGetMemoryInfo(device, &memoryInfo);
PRINT_IF_SUCCESS(memoryInfo.total, nvmlDeviceGetMemoryInfo);
UI32 freqSM = 0, freqGfx = 0, freqMem=0;
//result = nvmlDeviceGetClock(device, NVML_CLOCK_SM, NVML_CLOCK_ID_APP_CLOCK_DEFAULT, &freqSM);
result = nvmlDeviceGetMaxClockInfo(device, NVML_CLOCK_SM, &freqSM);
if (NVML_SUCCESS == result)
{
updateIfDstNotSet(m_props.FreqMaxMHz, (I32)freqSM);
}
PRINT_IF_SUCCESS(freqSM, nvmlDeviceGetMaxClockInfo);
result = nvmlDeviceGetMaxClockInfo(device, NVML_CLOCK_GRAPHICS, &freqGfx);
PRINT_IF_SUCCESS(freqGfx, nvmlDeviceGetMaxClockInfo);
result = nvmlDeviceGetMaxClockInfo(device, NVML_CLOCK_MEM, &freqMem);
PRINT_IF_SUCCESS(freqMem, nvmlDeviceGetMaxClockInfo);
UI32 freqVideo = 0;
result = nvmlDeviceGetMaxClockInfo(device, NVML_CLOCK_VIDEO, &freqVideo);
PRINT_IF_SUCCESS(freqVideo, nvmlDeviceGetMaxClockInfo);
unsigned int clockMHz = 0;
result = nvmlDeviceGetClock(device, NVML_CLOCK_GRAPHICS, NVML_CLOCK_ID_CURRENT, &clockMHz);
PRINT_IF_SUCCESS(clockMHz, nvmlDeviceGetClock);
nvmlUtilization_t utilization = {};
result = nvmlDeviceGetUtilizationRates(device, &utilization);
PRINT_IF_SUCCESS(utilization.gpu, nvmlDeviceGetClock);
PRINT_IF_SUCCESS(utilization.memory, nvmlDeviceGetClock);
result = nvmlDeviceGetCurrPcieLinkGeneration(device, &LinkGen);
if (NVML_SUCCESS == result)
{
result = nvmlDeviceGetCurrPcieLinkWidth(device, &LinkWidth);
if (NVML_SUCCESS == result)
{
updateIfDstNotSet(m_props.PCICurrentGen, (I32)LinkGen);
updateIfDstNotSet(m_props.PCICurrentWidth, (I32)LinkWidth);
}
}
nvmlDeviceArchitecture_t arch = 0;
result = nvmlDeviceGetArchitecture(device, &arch);
if (NVML_SUCCESS == result)
{
if (m_props.DeviceGenerationID < 0)
{
m_props.DeviceGenerationID = arch;
m_props.DeviceGenerationAPI = API_TYPE_NVML;
}
}
#if 0 // TODO: calculate MemoryBandWidthMax
if (freqMem && busWidth)
{
I64 memBW_Bps = (busWidth / 8 * freqMem) * (1024*1024ULL);
// TODO: Not correct - about half of actual
//updateIfDstNotSet(m_props.MemoryBandWidthMax, memBW_Bps);
}
#endif
validAPIs = validAPIs | API_TYPE_NVML;
m_nvmlDevice = device;
}
#ifdef XPUINFO_USE_TELEMETRYTRACKER
bool TelemetryTracker::RecordNVML(TimedRecord& rec)
{
nvmlDevice_t dev = m_Device->getHandle_NVML();
bool bRet = false;
if (dev)
{
unsigned int clockMHz = 0;
auto result = nvmlDeviceGetClock(dev, NVML_CLOCK_GRAPHICS, NVML_CLOCK_ID_CURRENT, &clockMHz);
if (NVML_SUCCESS == result)
{
rec.freq = clockMHz;
if (m_records.size() == 0)
{
m_ResultMask = (TelemetryItem)(m_ResultMask | TELEMETRYITEM_FREQUENCY);
}
bRet = true;
}
nvmlUtilization_t utilization = {};
result = nvmlDeviceGetUtilizationRates(dev, &utilization);
if (NVML_SUCCESS == result)
{
rec.activity_compute = utilization.gpu;
rec.activity_global = utilization.memory;
if (m_records.size() == 0)
{
m_ResultMask = (TelemetryItem)(m_ResultMask | TELEMETRYITEM_RENDER_COMPUTE_ACTIVITY| TELEMETRYITEM_GLOBAL_ACTIVITY);
}
bRet = true;
}
}
return bRet;
}
#endif
#ifdef _WIN32
static nvmlReturn_t safeInitNVML()
{
nvmlReturn_t result = NVML_ERROR_UNINITIALIZED;
__try
{
result = nvmlInit();
}
__except (GetExceptionCode() == VcppException(ERROR_SEVERITY_ERROR, ERROR_MOD_NOT_FOUND)
? EXCEPTION_EXECUTE_HANDLER
: EXCEPTION_CONTINUE_SEARCH)
{
result = NVML_ERROR_UNINITIALIZED;
}
return result;
}
#endif
void XPUInfo::initNVML()
{
#ifdef _WIN32
nvmlReturn_t result = safeInitNVML();
#else
nvmlReturn_t result = nvmlInit();
#endif
if (NVML_SUCCESS == result)
{
UI32 device_count=0;
result = nvmlDeviceGetCount(&device_count);
if (NVML_SUCCESS == result)
{
m_UsedAPIs = m_UsedAPIs | API_TYPE_NVML;
for (UI32 i = 0; i < device_count; ++i)
{
nvmlDevice_t device = nullptr;
result = nvmlDeviceGetHandleByIndex(i, &device);
if (NVML_SUCCESS == result)
{
nvmlPciInfo_t pci;
result = nvmlDeviceGetPciInfo(device, &pci);
if (NVML_SUCCESS == result)
{
PCIAddressType pciAddr;
pciAddr.bus = pci.bus;
pciAddr.device = pci.device;
pciAddr.domain = pci.domain;
pciAddr.function = 0;
String dbdf = pci.busId;
auto posLastDot = dbdf.rfind('.');
String funcStr = dbdf.substr(posLastDot + 1);
if (funcStr[0] != '0')
{
pciAddr.function = atoi(funcStr.c_str());
}
for (auto& [luid, dev] : m_Devices)
{
if (dev->getProperties().PCIAddress == pciAddr)
{
dev->initNVMLDevice(device);
break;
}
}
}
}
}
}
else
{
DebugStream dStr(true);
dStr << "Failed to query device count: " << nvmlErrorString(result) << std::endl;
}
}
}
void XPUInfo::shutdownNVML()
{
if (m_UsedAPIs & API_TYPE_NVML)
{
#ifdef _DEBUG
auto result =
#endif
nvmlShutdown();
XPUINFO_DEBUG_REQUIRE(NVML_SUCCESS == result);
}
}
} // XI
#endif // XPUINFO_USE_LEVELZERO