public static IEnumerable <GpuDevice> EnumDevices() { var err = Nvml.nvmlInit_v2(); if (err != Nvml.nvmlReturn.NVML_SUCCESS) { yield break; } int cdev; err = Nvml.nvmlDeviceGetCount(out cdev); if (err != Nvml.nvmlReturn.NVML_SUCCESS) { yield break; } for (int i = 0; i < cdev; i++) { yield return(new GpuDevice(i)); } }
public GpuDevice(int deviceId) { DeviceId = deviceId; var err = Nvml.nvmlInit_v2(); if (err == Nvml.nvmlReturn.NVML_SUCCESS) { err = Nvml.nvmlDeviceGetHandleByIndex((uint)deviceId, out _device); if (err == Nvml.nvmlReturn.NVML_SUCCESS) { var name = new StringBuilder(MaxNameLen); var uuid = new StringBuilder(MaxUuidLen); if (Nvml.nvmlDeviceGetName(_device, name, (uint)name.Capacity) == Nvml.nvmlReturn.NVML_SUCCESS && Nvml.nvmlDeviceGetPciInfo(_device, out _pci) == Nvml.nvmlReturn.NVML_SUCCESS && Nvml.nvmlDeviceGetUUID(_device, uuid, (uint)uuid.Capacity) == Nvml.nvmlReturn.NVML_SUCCESS) { Name = name.ToString(); Uuid = uuid.ToString(); return; } } } Name = "<FailedToInitialize>"; }