Skip to content

Commit

Permalink
refactored GPU implementation
Browse files Browse the repository at this point in the history
  • Loading branch information
d0ugherty committed Aug 13, 2023
1 parent feaf51a commit f75b7aa
Show file tree
Hide file tree
Showing 2 changed files with 83 additions and 18 deletions.
99 changes: 82 additions & 17 deletions gpu.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -12,52 +12,117 @@ Gpu::Gpu(unsigned int index){
initialize_device(index);
}


void Gpu::initialize_device(unsigned int index){
this->index = index;
nvmlDeviceGetHandleByIndex_v2(index, &this->handle);
set_device_features();
set_device_info();
}

/**
* Feature check using bitwise operations
*
*/
void Gpu::set_device_features(){

if(nvmlDeviceGetMemoryInfo(this->handle, &this->memory) == NVML_SUCCESS) {
&this->features |= MEMORY_INFO;
features |= MEMORY_INFO;
}

if(nvmlDeviceGetUtilizationRates(dev.handle, &dev.utilization) == NVML_SUCCESS) {
dev.features |= UTILIZATION_INFO;
if(nvmlDeviceGetUtilizationRates(this->handle, &this->utilization) == NVML_SUCCESS) {
features |= UTILIZATION_INFO;
}

if(nvmlDeviceGetTemperature(dev.handle, NVML_TEMPERATURE_GPU, &dev.temperature) == NVML_SUCCESS) {
dev.features |= TEMPERATURE;
if(nvmlDeviceGetTemperature(this->handle, NVML_TEMPERATURE_GPU, &this->temperature) == NVML_SUCCESS) {
features |= TEMPERATURE;
}

if(nvmlDeviceGetPowerUsage(dev.handle, &dev.power_usage) == NVML_SUCCESS) {
dev.features |= POWER_USAGE;
if(nvmlDeviceGetPowerUsage(this->handle, &this->power_usage) == NVML_SUCCESS) {
features |= POWER_USAGE;
}

if(nvmlDeviceGetClock(dev.handle, NVML_CLOCK_GRAPHICS, NVML_CLOCK_ID_CURRENT, &dev.clock_speed) == NVML_SUCCESS) {
dev.features |= CLOCK_INFO;
if(nvmlDeviceGetClock(this->handle, NVML_CLOCK_GRAPHICS, NVML_CLOCK_ID_CURRENT, &this->clock_speed) == NVML_SUCCESS) {
features |= CLOCK_INFO;
}

if(nvmlDeviceGetFanSpeed(dev.handle, &dev.fan_speed) == NVML_SUCCESS) {
dev.features |= FAN_INFO;
if(nvmlDeviceGetFanSpeed(this->handle, &this->fan_speed) == NVML_SUCCESS) {
features |= FAN_INFO;
}

if(nvmlDeviceGetComputeMode(dev.handle, &dev.mode) == NVML_SUCCESS) {
dev.features |= COMPUTE_MODE;
if(nvmlDeviceGetComputeMode(this->handle, &this->mode) == NVML_SUCCESS) {
features |= COMPUTE_MODE;
}
}
}

/**
* Retrieve GPU statistics
*
*/

void Gpu::set_device_info(){
nvmlDeviceGetPciInfo_v3(this->handle, &this->pci);

nvmlDeviceGetName(this->handle, this->name, sizeof(this->name));
nvmlDeviceGetSerial(this->handle, this->serial, sizeof(this->serial));
nvmlDeviceGetUUID(this->handle, this->uuid, sizeof(this->uuid));

if(this->features & MEMORY_INFO) {
nvmlDeviceGetMemoryInfo(this->handle, &this->memory);
}

if(this->features & UTILIZATION_INFO){
nvmlDeviceGetUtilizationRates(this->handle, &this->utilization);
}

if(this->features & TEMPERATURE){
nvmlDeviceGetTemperature(this->handle,NVML_TEMPERATURE_GPU, &this->temperature);
}

if(this->features & POWER_USAGE) {
nvmlDeviceGetPowerUsage(this->handle, &this->power_usage);
}

if(this->features & CLOCK_INFO){
//nvmlDeviceGetClock(this->handle, NVML_CLOCK_GRAPHICS, NVML_CLOCK_ID_CURRENT, &this->clock_speed);
nvmlDeviceGetClockInfo(this->handle, NVML_CLOCK_GRAPHICS, &this->clock_speed);
}

if(this->features & FAN_INFO){
nvmlDeviceGetFanSpeed(this->handle, &this->fan_speed);
}

if(this->features & COMPUTE_MODE){
nvmlDeviceGetComputeMode(this->handle, &this->mode);
}
}

/**
* Print GPU info to console
*
* TO DO: GUI
*/
void Gpu::display_info(){

std::cout << this->name << " Driver Version: " << driver_version;
std::cout << "TEMPERATURE: " << this->temperature << "C\n";
std::cout << "POWER USAGE: " << this->power_usage <<"mW\n";
std::cout << "LOAD: " << this->utilization.gpu << " Memory: " << this->utilization.memory;
std::cout << "CLOCK SPEED: " << this->clock_speed << "MHz";
std::cout << "FAN SPEED: " << this->fan_speed << " of Max\n";
}

/**
* Print device information to the terminal repetitively
* according to a specified polling interval.
*
* This is a simple, yet inefficient polling function.
*
* TO DO: learn a better way to do this
*
*/
void Gpu::watch_info(unsigned int interval){

while(true) {
display_info();
std::this_thread::sleep_for(std::chrono::seconds(interval));
}
}

2 changes: 1 addition & 1 deletion monitor.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ void Monitor::initialize_device(struct gpu &dev, int index) {
}

/**
* Check for supported GPU features
* Check for supported GPU features
*
*/
void Monitor::get_device_features(struct gpu &dev) {
Expand Down

0 comments on commit f75b7aa

Please sign in to comment.