Example #1
0
        public Nvidia(uint i, nvmlDevice device)
        {
            this.deviceIndex = i;
            this.device      = device;
            this.type        = GPUType.NVIDIA;

            nvmlPciInfo pciInfo = new nvmlPciInfo();

            if (NvmlNativeMethods.nvmlDeviceGetPciInfo(device, ref pciInfo) == nvmlReturn.Success)
            {
                this.bus = pciInfo.bus;
            }

            if (NvmlNativeMethods.nvmlDeviceGetSerial(device, out string serialOut) == nvmlReturn.Success)
            {
                this.serial = serialOut;
            }

            if (NvmlNativeMethods.nvmlDeviceGetUUID(device, out string uuidOut) == nvmlReturn.Success)
            {
                this.uuid = uuidOut;
            }

            if (NvmlNativeMethods.nvmlDeviceGetName(device, out string name) == nvmlReturn.Success)
            {
                this.name = name;
            }

            NVAPIhandle = this.FindPhysicalGpuHandleForBus(this.bus);
        }
Example #2
0
        public List <NvGpu> GetGpus()
        {
            List <NvGpu> results = new List <NvGpu>();

            try {
                if (NvmlInit())
                {
                    _nvmlDevices.Clear();
                    uint deviceCount = 0;
                    var  r           = NvmlNativeMethods.nvmlDeviceGetCount(ref deviceCount);
                    CheckResult(r, () => $"{nameof(NvmlNativeMethods.nvmlDeviceGetCount)} {r.ToString()}");
                    for (int i = 0; i < deviceCount; i++)
                    {
                        NvGpu gpu = new NvGpu {
                            GpuIndex = i
                        };
                        nvmlDevice nvmlDevice = new nvmlDevice();
                        r = NvmlNativeMethods.nvmlDeviceGetHandleByIndex((uint)i, ref nvmlDevice);
                        _nvmlDevices.Add(nvmlDevice);
                        CheckResult(r, () => $"{nameof(NvmlNativeMethods.nvmlDeviceGetHandleByIndex)}({((uint)i).ToString()}) {r.ToString()}");
                        r = NvmlNativeMethods.nvmlDeviceGetName(nvmlDevice, out string name);
                        CheckResult(r, () => $"{nameof(NvmlNativeMethods.nvmlDeviceGetName)} {r.ToString()}");
                        nvmlMemory memory = new nvmlMemory();
                        r = NvmlNativeMethods.nvmlDeviceGetMemoryInfo(nvmlDevice, ref memory);
                        CheckResult(r, () => $"{nameof(NvmlNativeMethods.nvmlDeviceGetMemoryInfo)} {r.ToString()}");
                        // short gpu name
                        if (!string.IsNullOrEmpty(name))
                        {
                            name = name.Replace("GeForce GTX ", string.Empty);
                            name = name.Replace("GeForce ", string.Empty);
                        }
                        nvmlPciInfo pci = new nvmlPciInfo();
                        r = NvmlNativeMethods.nvmlDeviceGetPciInfo(nvmlDevice, ref pci);
                        CheckResult(r, () => $"{nameof(NvmlNativeMethods.nvmlDeviceGetPciInfo)} {r.ToString()}");
                        gpu.Name        = name;
                        gpu.BusId       = (int)pci.bus;
                        gpu.TotalMemory = memory.total;
                        results.Add(gpu);
                    }
                }
            }
            catch {
            }

            return(results);
        }
Example #3
0
 public NVIDIAGpuSet(INTMinerRoot root)
 {
     _root           = root;
     this.Properties = new List <GpuSetProperty>();
     if (NvmlInit())
     {
         NvmlNativeMethods.nvmlDeviceGetCount(ref deviceCount);
         for (int i = 0; i < deviceCount; i++)
         {
             Gpu        gpu        = Gpu.Create(i, string.Empty, string.Empty);
             nvmlDevice nvmlDevice = new nvmlDevice();
             var        nvmlReturn = NvmlNativeMethods.nvmlDeviceGetHandleByIndex((uint)i, ref nvmlDevice);
             SetGpuStatus(gpu, nvmlReturn);
             NvmlNativeMethods.nvmlDeviceGetName(nvmlDevice, out string name);
             nvmlMemory memory = new nvmlMemory();
             NvmlNativeMethods.nvmlDeviceGetMemoryInfo(nvmlDevice, ref memory);
             // short gpu name
             if (!string.IsNullOrEmpty(name))
             {
                 name = name.Replace("GeForce GTX ", string.Empty);
                 name = name.Replace("GeForce ", string.Empty);
             }
             nvmlPciInfo pci = new nvmlPciInfo();
             NvmlNativeMethods.nvmlDeviceGetPciInfo(nvmlDevice, ref pci);
             gpu.Name        = name;
             gpu.BusId       = pci.bus.ToString();
             gpu.TotalMemory = memory.total;
             _gpus.Add(i, gpu);
         }
         if (deviceCount > 0)
         {
             NvmlNativeMethods.nvmlSystemGetDriverVersion(out _driverVersion);
             NvmlNativeMethods.nvmlSystemGetNVMLVersion(out string nvmlVersion);
             this.Properties.Add(new GpuSetProperty(GpuSetProperty.DRIVER_VERSION, "驱动版本", _driverVersion));
             try {
                 if (double.TryParse(_driverVersion, out double driverVersionNum))
                 {
                     var item = root.SysDicItemSet.GetSysDicItems("CudaVersion")
                                .Select(a => new { Version = double.Parse(a.Value), a })
                                .OrderByDescending(a => a.Version)
                                .FirstOrDefault(a => driverVersionNum >= a.Version);
                     if (item != null)
                     {
                         this.Properties.Add(new GpuSetProperty("CudaVersion", "Cuda版本", item.a.Code));
                     }
                 }
             }
             catch (Exception e) {
                 Logger.ErrorDebugLine(e);
             }
             this.Properties.Add(new GpuSetProperty("NVMLVersion", "NVML版本", nvmlVersion));
             Dictionary <string, string> kvs = new Dictionary <string, string> {
                 { "CUDA_DEVICE_ORDER", "PCI_BUS_ID" }
             };
             foreach (var kv in kvs)
             {
                 var property = new GpuSetProperty(kv.Key, kv.Key, kv.Value);
                 this.Properties.Add(property);
             }
             Task.Factory.StartNew(() => {
                 foreach (var gpu in _gpus.Values)
                 {
                     OverClock.RefreshGpuState(gpu.Index);
                 }
                 // 这里会耗时5秒
                 foreach (var kv in kvs)
                 {
                     Environment.SetEnvironmentVariable(kv.Key, kv.Value);
                 }
             });
         }
     }
 }
Example #4
0
 public static extern nvmlReturn nvmlDeviceGetPciInfo([In] IntPtr device, [Out] out nvmlPciInfo pci);