/* GPU energy monitor code: This code (and the header that goes with it) allows for easy querying of the GPU energy sensor Copyright 2022 Texas State University Redistribution and use in source and binary forms, with or without modification, are permitted provided that the following conditions are met: 1. Redistributions of source code must retain the above copyright notice, this list of conditions and the following disclaimer. 2. Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the following disclaimer in the documentation and/or other materials provided with the distribution. 3. Neither the name of the copyright holder nor the names of its contributors may be used to endorse or promote products derived from this software without specific prior written permission. THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. Authors: Alex Fallin and Martin Burtscher URL: The latest version of this code is available at https://cs.txstate.edu/~burtscher/research/bit-flips/. */ #include #include #include #include "gpu_energy_monitor.h" void GPUMonitor::initMonitor(int cuda_device) { nvmlReturn_t result; result = nvmlInit(); if (NVML_SUCCESS != result) { printf("failed to initialize NVML: %s\n", nvmlErrorString(result)); exit(-1); } // Getting the properties to create the bus ID cudaDeviceProp device_prop; cudaGetDeviceProperties(&device_prop, cuda_device); //domain:bus:device.function PCI identifier all in hex std::stringstream bus_stream; bus_stream << std::hex << device_prop.pciDomainID << ':'; bus_stream << std::hex << device_prop.pciBusID << ':'; bus_stream << std::hex << device_prop.pciDeviceID << ".0"; std::string bus_id(bus_stream.str()); result = nvmlDeviceGetHandleByPciBusId_v2(bus_id.c_str(), &device); if (NVML_SUCCESS != result) { printf("failed to get handle for device: %s\n", nvmlErrorString(result)); exit(-1); } unsigned long long energy; result = nvmlDeviceGetTotalEnergyConsumption(device, &energy); if (NVML_SUCCESS != result) { printf("failed to read energy: %s\n", nvmlErrorString(result)); exit(-1); } unsigned int temp; result = nvmlDeviceGetTemperature(device, NVML_TEMPERATURE_GPU, &temp); if (NVML_SUCCESS != result) { printf("failed to read temperature: %s\n", nvmlErrorString(result)); exit(-1); } unsigned int clock_mhz; result = nvmlDeviceGetClock (device, NVML_CLOCK_GRAPHICS, NVML_CLOCK_ID_CURRENT, &clock_mhz); if (NVML_SUCCESS != result) { printf("failed to get graphics clock: %s\n", nvmlErrorString(result)); exit(-1); } result = nvmlDeviceGetClock (device, NVML_CLOCK_SM, NVML_CLOCK_ID_CURRENT, &clock_mhz); if (NVML_SUCCESS != result) { printf("failed to get SM clock: %s\n", nvmlErrorString(result)); exit(-1); } result = nvmlDeviceGetClock (device, NVML_CLOCK_MEM, NVML_CLOCK_ID_CURRENT, &clock_mhz); if (NVML_SUCCESS != result) { printf("failed to get memory clock: %s\n", nvmlErrorString(result)); exit(-1); } device_initialized = true; } unsigned int GPUMonitor::getTemperature() { if (!device_initialized) { printf("GPU monitor is uninitialized\n"); exit(-1); } unsigned int temp; nvmlDeviceGetTemperature(device, NVML_TEMPERATURE_GPU, &temp); return temp; } void GPUMonitor::startEnergy() { if (!device_initialized) { printf("GPU monitor is uninitialized\n"); exit(-1); } nvmlDeviceGetTotalEnergyConsumption(device, &starting_energy); } unsigned long long GPUMonitor::getEnergyConsumption() { if (!device_initialized) { printf("GPU monitor is uninitialized\n"); exit(-1); } unsigned long long new_energy; nvmlDeviceGetTotalEnergyConsumption(device, &new_energy); return new_energy - starting_energy; } unsigned int GPUMonitor::getMemClock() { if (!device_initialized) { printf("GPU monitor is uninitialized\n"); exit(-1); } unsigned int clock_mhz; nvmlDeviceGetClock (device, NVML_CLOCK_MEM, NVML_CLOCK_ID_CURRENT, &clock_mhz); return clock_mhz; } unsigned int GPUMonitor::getSMClock() { if (!device_initialized) { printf("GPU monitor is uninitialized\n"); exit(-1); } unsigned int clock_mhz; nvmlDeviceGetClock (device, NVML_CLOCK_SM, NVML_CLOCK_ID_CURRENT, &clock_mhz); return clock_mhz; } unsigned int GPUMonitor::getGraphicsClock() { if (!device_initialized) { printf("GPU monitor is uninitialized\n"); exit(-1); } unsigned int clock_mhz; nvmlDeviceGetClock (device, NVML_CLOCK_GRAPHICS, NVML_CLOCK_ID_CURRENT, &clock_mhz); return clock_mhz; }