nvbench
nvbench copied to clipboard
Detect if GPU is being used for graphical purposes
Recent results show that noise could be increased up to 50% due to X-Server running on the device. To warn users about the noisy environment, we could check if GPU is being used for graphical purposes. It's possible to get this kind of information with NVML:
#include <iostream>
#include <nvml.h>
void chck(nvmlReturn_t status) {
if (status != NVML_SUCCESS) {
throw std::runtime_error(std::string("Failed to initialize NVML: ") + nvmlErrorString(status));
}
}
struct NVML_RAII {
bool initialized = false;
NVML_RAII() {
chck(nvmlInit());
initialized = true;
}
~NVML_RAII() {
if (initialized) {
chck(nvmlShutdown());
}
}
};
int main() {
NVML_RAII nvml_raii;
unsigned int device_count;
chck(nvmlDeviceGetCount(&device_count));
for (unsigned int i = 0; i < device_count; i++) {
nvmlDevice_t device;
char name[NVML_DEVICE_NAME_BUFFER_SIZE];
chck(nvmlDeviceGetHandleByIndex(i, &device));
chck(nvmlDeviceGetName(device, name, NVML_DEVICE_NAME_BUFFER_SIZE));
std::cout << "\t" << name << std::endl;
unsigned int graphics_processes = 0;
nvmlDeviceGetGraphicsRunningProcesses(device, &graphics_processes, nullptr);
if (graphics_processes) {
std::cerr << "X-Server is running, please turn it off (" << graphics_processes << ")" << std::endl;
}
unsigned int compute_processes = 0;
nvmlDeviceGetComputeRunningProcesses(device, &compute_processes, nullptr);
if (compute_processes) {
std::cerr << "Target device is in use (" << compute_processes << ")" << std::endl;
}
}
return 0;
}
The header could be easily overlooked, so we could also bring one's attention to the noisy environment by adding a column to the nvbench output.