-4
A
回答
0
http://eliang.blogspot.com.by/2011/05/getting-nvidia-gpu-usage-in-c.html?m=1
//
// Getting Nvidia GPU Usage
//
// Reference: Open Hardware Monitor (http://code.google.com/p/open-hardware-monitor)
//
#include <windows.h>
#include <iostream>
// magic numbers, do not change them
#define NVAPI_MAX_PHYSICAL_GPUS 64
#define NVAPI_MAX_USAGES_PER_GPU 34
// function pointer types
typedef int *(*NvAPI_QueryInterface_t)(unsigned int offset);
typedef int (*NvAPI_Initialize_t)();
typedef int (*NvAPI_EnumPhysicalGPUs_t)(int **handles, int *count);
typedef int (*NvAPI_GPU_GetUsages_t)(int *handle, unsigned int *usages);
int main()
{
HMODULE hmod = LoadLibraryA("nvapi.dll");
if (hmod == NULL)
{
std::cerr << "Couldn't find nvapi.dll" << std::endl;
return 1;
}
// nvapi.dll internal function pointers
NvAPI_QueryInterface_t NvAPI_QueryInterface = NULL;
NvAPI_Initialize_t NvAPI_Initialize = NULL;
NvAPI_EnumPhysicalGPUs_t NvAPI_EnumPhysicalGPUs = NULL;
NvAPI_GPU_GetUsages_t NvAPI_GPU_GetUsages = NULL;
// nvapi_QueryInterface is a function used to retrieve other internal functions in nvapi.dll
NvAPI_QueryInterface = (NvAPI_QueryInterface_t) GetProcAddress(hmod, "nvapi_QueryInterface");
// some useful internal functions that aren't exported by nvapi.dll
NvAPI_Initialize = (NvAPI_Initialize_t) (*NvAPI_QueryInterface)(0x0150E828);
NvAPI_EnumPhysicalGPUs = (NvAPI_EnumPhysicalGPUs_t) (*NvAPI_QueryInterface)(0xE5AC921F);
NvAPI_GPU_GetUsages = (NvAPI_GPU_GetUsages_t) (*NvAPI_QueryInterface)(0x189A1FDF);
if (NvAPI_Initialize == NULL || NvAPI_EnumPhysicalGPUs == NULL ||
NvAPI_EnumPhysicalGPUs == NULL || NvAPI_GPU_GetUsages == NULL)
{
std::cerr << "Couldn't get functions in nvapi.dll" << std::endl;
return 2;
}
// initialize NvAPI library, call it once before calling any other NvAPI functions
(*NvAPI_Initialize)();
int gpuCount = 0;
int *gpuHandles[NVAPI_MAX_PHYSICAL_GPUS] = { NULL };
unsigned int gpuUsages[NVAPI_MAX_USAGES_PER_GPU] = { 0 };
// gpuUsages[0] must be this value, otherwise NvAPI_GPU_GetUsages won't work
gpuUsages[0] = (NVAPI_MAX_USAGES_PER_GPU * 4) | 0x10000;
(*NvAPI_EnumPhysicalGPUs)(gpuHandles, &gpuCount);
// print GPU usage every second
for (int i = 0; i < 100; i++)
{
(*NvAPI_GPU_GetUsages)(gpuHandles[0], gpuUsages);
int usage = gpuUsages[3];
std::cout << "GPU Usage: " << usage << std::endl;
Sleep(1000);
}
return 0;
}
相關問題
- 1. 音頻負載百分比indicatior
- 2. 如何讀取GPU負載?
- 3. 負整數百分比
- 4. SQL獲取百分比
- 5. 獲取間隔百分比
- 6. JavaScript獲取百分比
- 7. 用jquery獲取百分比
- 8. SQL:獲取百分比
- 9. Powershell獲取CPU百分比
- 10. 比較字段和獲取百分比
- 11. 如何從Firebase存儲下載時獲取百分比下載?
- 12. OpenGL:如何獲得GPU使用百分比?
- 13. 你如何計算nvidia(cuda capable),gpu卡上的負載?
- 14. GPU中工作項負載的侷限性? CUDA/OpenCL
- 15. 負循環百分比和值
- 16. 負數和正數百分比計算
- 17. 獲取圖像的百分比Swift
- 18. 獲取數字的百分比。 PHP
- 19. 從php獲取%的百分比與php
- 20. 如何使用MDX獲取百分比
- 21. Excel-dna獲取百分比值
- 22. 獲取失敗百分比列
- 23. 獲取組合中的百分比
- 24. 如何在oracle中獲取百分比?
- 25. 獲取CPU使用百分比
- 26. 在數據表中獲取百分比
- 27. 獲取值的百分比在Elasticsearch
- 28. 獲取引薦來源百分比
- 29. 獲取變化的百分比
- 30. 獲取每個級別的百分比
https://developer.nvidia.com/nvidia-management-library-nvml – talonmies