用NVAPI获取GPU信息

通过nvapi可以获得GPU的温度和性能参数。主页在这里:https://developer.nvidia.com/nvapi

我的代码基础是来自互联网,不过略加变化。首先是获取物理GPU,然后是获取温度,最后是获取GPU性能。另外,获取GPU性能不需要自己通过offset从dll中导出函数。


#include "stdafx.h"
#include "R313-developer/nvapi.h"


int _tmain(int argc, _TCHAR* argv[])
{
    NvAPI_Status ret = NVAPI_OK;
    int i=0;

    NvDisplayHandle hDisplay_a[NVAPI_MAX_PHYSICAL_GPUS*2] = {0};

    ret = NvAPI_Initialize();

    if (!ret == NVAPI_OK){
        NvAPI_ShortString str;
        NvAPI_GetErrorMessage(ret, str);
        printf("NVAPI NvAPI_Initialize: %s\n", str);
		return 1;
    }

    NvAPI_ShortString ver;

    NvAPI_GetInterfaceVersionString(ver);
    printf("NVAPI Version: %s\n", ver);

    NvU32 cnt;

    NvPhysicalGpuHandle phys;

    ret = NvAPI_EnumPhysicalGPUs(&phys, &cnt);

    if (!ret == NVAPI_OK){
        NvAPI_ShortString string;
        NvAPI_GetErrorMessage(ret, string);
        printf("NVAPI NvAPI_EnumPhysicalGPUs: %s\n", string);
		return 1;
    }

    NvAPI_ShortString name;

    NV_GPU_THERMAL_SETTINGS thermal;

    ret = NvAPI_GPU_GetFullName(phys, name);
    if (!ret == NVAPI_OK){
        NvAPI_ShortString string;
        NvAPI_GetErrorMessage(ret, string);
        printf("NVAPI NvAPI_GPU_GetFullName: %s\n", string);
		return 1;
    }

    printf("Name: %s\n", name);
    thermal.version = NV_GPU_THERMAL_SETTINGS_VER;
    ret = NvAPI_GPU_GetThermalSettings(phys,0, &thermal);

    if (!ret == NVAPI_OK){
        NvAPI_ShortString string;
        NvAPI_GetErrorMessage(ret, string);
        printf("NVAPI NvAPI_GPU_GetThermalSettings: %s\n", string);
		return 1;
    }
	
	printf("Temp: %u C\n", static_cast<unsigned>(thermal.sensor[0].currentTemp));

	NV_GPU_DYNAMIC_PSTATES_INFO_EX infoEx;
	infoEx.version = NV_GPU_DYNAMIC_PSTATES_INFO_EX_VER;
	ret = NvAPI_GPU_GetDynamicPstatesInfoEx(phys, &infoEx);

	if (!ret == NVAPI_OK) {
        NvAPI_ShortString string;
        NvAPI_GetErrorMessage(ret, string);
        printf("NVAPI NvAPI_GPU_GetDynamicPstatesInfoEx: %s\n", string);
		return 1;
	}

	//printf("Physical GPU utilization%s", infoEx.utilization[0].);	
    return 0;


你可能感兴趣的:(用NVAPI获取GPU信息)