我写了一个简单的 nvml 测试代码。
在 Linux 上,我可以使用 gcc 毫无问题地编译和运行它。
gcc test.c -I /usr/local/cuda-11.5/include -l:libnvidia-ml.so -o test
但是,如何使用 Visual Studio cl.exe 在 Windows 上做同样的事情?
我尝试了以下,没有运气。
cl.exe test.c /EHsc /I "C:\Program Files\NVIDIA GPU Computing Toolkit\CUDA\v11.5\include" /link /LIBPATH:"c:\Program Files\NVIDIA GPU Computing Toolkit\CUDA\v11 .5\lib\x64"
似乎 cl.exe 没有相当于 gcc 的“-l:libnvidia-ml.so”
#include <stdio.h>
#include <nvml.h>
void list_device()
{
nvmlInit();
unsigned int count;
nvmlDeviceGetCount(&count);
printf("Found %u device \n", count);
nvmlDevice_t device;
nvmlPciInfo_t pci;
char name[NVML_DEVICE_NAME_BUFFER_SIZE];
unsigned int speed;
unsigned int temp;
nvmlTemperatureSensors_t sensorType = NVML_TEMPERATURE_GPU;
nvmlMemory_t memory;
unsigned int power;
unsigned int limit;
nvmlUtilization_t utilization;
int i = 0;
for (i = 0; i < count; i++)
{
nvmlDeviceGetHandleByIndex(i, &device);
nvmlDeviceGetName(device, name, NVML_DEVICE_NAME_BUFFER_SIZE);
nvmlDeviceGetPciInfo(device, &pci);
nvmlDeviceGetFanSpeed(device, &speed);
nvmlDeviceGetTemperature(device, sensorType, &temp);
nvmlDeviceGetMemoryInfo(device, &memory);
nvmlDeviceGetPowerUsage(device, &power);
nvmlDeviceGetEnforcedPowerLimit(device, &limit);
nvmlDeviceGetUtilizationRates(device, &utilization);
printf("%d. %s [%s] \n \
fan speed: %d%% temp: %dC \n \
memory.used: %lluMiB memory.free: %lluMiB memory.total: %lluMiB \n \
power: %uW/%uW \n \
gpu: %u memory: %u \n", \
i, name, pci.busId, \
speed, temp, \
memory.used/1024/1024, memory.free/1024/1024, memory.total/1024/1024, \
power/1000, limit/1000, \
utilization.gpu, utilization.memory);
}
nvmlShutdown();
}
int main(void)
{
list_device();
}