Esempio n. 1
0
        public void LoadGpuState()
        {
            foreach (Gpu gpu in _gpus.Values)
            {
                int        i          = gpu.Index;
                nvmlDevice nvmlDevice = new nvmlDevice();
                var        nvmlReturn = NvmlNativeMethods.nvmlDeviceGetHandleByIndex((uint)i, ref nvmlDevice);
                SetGpuStatus(gpu, nvmlReturn);
                uint power = 0;
                NvmlNativeMethods.nvmlDeviceGetPowerUsage(nvmlDevice, ref power);
                power = (uint)(power / 1000.0);
                uint temp = 0;
                NvmlNativeMethods.nvmlDeviceGetTemperature(nvmlDevice, nvmlTemperatureSensors.Gpu, ref temp);
                uint speed = 0;
                NvmlNativeMethods.nvmlDeviceGetFanSpeed(nvmlDevice, ref speed);
                bool isChanged = gpu.Temperature != temp || gpu.PowerUsage != power || gpu.FanSpeed != speed;
                gpu.Temperature = (int)temp;
                gpu.PowerUsage  = power;
                gpu.FanSpeed    = speed;

                if (isChanged)
                {
                    VirtualRoot.Happened(new GpuStateChangedEvent(gpu));
                }
            }
        }
Esempio n. 2
0
        internal static void InitializeNVML(OpenCLDevice[] pDevices)
        {
            try
            {
                if (NvmlNativeMethods.nvmlInit() == 0)
                {
                    Messenger.Default.Send <MinerOutputMessage>(new MinerOutputMessage()
                    {
                        OutputText = "Successfully initialized Nvidia Managment Library."
                    });

                    uint nvmlDeviceCount = 0;
                    NvmlNativeMethods.nvmlDeviceGetCount(ref nvmlDeviceCount);

                    foreach (var device in pDevices.Where(x => x.ComputeDevice.Vendor.Equals(Consts.VendorNvidia)))
                    {
                        var nvmlDevice = new nvmlDevice();
                        NvmlNativeMethods.nvmlDeviceGetHandleByPciBusId($" 0000:{device.ComputeDevice.PciBusIdNV.ToString("X2")}:00.0", ref nvmlDevice);
                        device.CudaDevice      = nvmlDevice;
                        device.ADLAdapterIndex = device.ComputeDevice.PciBusIdNV + 79;
                    }
                }
            }
            catch
            {
                //baj van :(
                Messenger.Default.Send <MinerOutputMessage>(new MinerOutputMessage()
                {
                    OutputText = "Failed to initialize Nvidia Managment Library."
                });
            }
        }
Esempio n. 3
0
        public void LoadGpuState()
        {
            for (int i = 0; i < deviceCount; i++)
            {
                nvmlDevice nvmlDevice = new nvmlDevice();
                NvmlNativeMethods.nvmlDeviceGetHandleByIndex((uint)i, ref nvmlDevice);
                uint power = 0;
                NvmlNativeMethods.nvmlDeviceGetPowerUsage(nvmlDevice, ref power);
                power = (uint)(power / 1000.0);
                uint temp = 0;
                NvmlNativeMethods.nvmlDeviceGetTemperature(nvmlDevice, nvmlTemperatureSensors.Gpu, ref temp);
                uint speed = 0;
                NvmlNativeMethods.nvmlDeviceGetFanSpeed(nvmlDevice, ref speed);

                Gpu  gpu       = (Gpu)_gpus[i];
                bool isChanged = gpu.Temperature != temp || gpu.PowerUsage != power || gpu.FanSpeed != speed;
                gpu.Temperature = temp;
                gpu.PowerUsage  = power;
                gpu.FanSpeed    = speed;

                if (isChanged)
                {
                    VirtualRoot.Happened(new GpuStateChangedEvent(gpu));
                }
            }
        }
Esempio n. 4
0
        internal static void LogNvidiaMonitorManagerState()
        {
            // Enumerate NVAPI handles and map to busid
            var idHandles = InitNvapi();

            foreach (var pair in _nvidiaUUIDAndBusIds)
            {
                var uuid  = pair.Key;
                var busID = pair.Value;


                var nvmlResultStr = "InitalNVMLInitSuccess==FALSE";
                if (InitalNVMLInitSuccess)
                {
                    var nvmlHandle = new nvmlDevice();
                    var nvmlRet    = NvmlNativeMethods.nvmlDeviceGetHandleByUUID(uuid, ref nvmlHandle);
                    if (nvmlRet != nvmlReturn.Success)
                    {
                        nvmlResultStr = $"Failed with code ret {nvmlRet}";
                    }
                    else
                    {
                        nvmlResultStr = nvmlHandle.Pointer.ToString();
                    }
                }
                var nvapiResultStr = "NVAPI found no handle";
                var nvHandle       = new NvPhysicalGpuHandle();
                if (idHandles.TryGetValue(busID, out nvHandle))
                {
                    nvapiResultStr = nvHandle.ptr.ToString();
                }
                Logger.Info($"{Tag}.Init", $"UUID({uuid})-BusID({busID}): NVML_HANDLE({nvmlResultStr}) NVAPI_HANDLE({nvapiResultStr})");
            }
        }
Esempio n. 5
0
        //TODO fix constructor to not include 3rd party handles + refactor inside interface device info (lookup fans speed/temps/...)
        public CudaComputeDevice(CudaDevice cudaDevice, DeviceGroupType group, int gpuCount,
                                 NvPhysicalGpuHandle nvHandle, nvmlDevice nvmlHandle)
            : base((int)cudaDevice.DeviceID,
                   cudaDevice.GetName(),
                   true,
                   group,
                   DeviceType.NVIDIA,
                   string.Format(Translations.Tr("GPU#{0}"), gpuCount),
                   cudaDevice.DeviceGlobalMemory)
        {
            BusID             = cudaDevice.pciBusID;
            SMMajor           = cudaDevice.SM_major;
            SMMinor           = cudaDevice.SM_minor;
            Uuid              = cudaDevice.UUID;
            AlgorithmSettings = DefaultAlgorithms.GetAlgorithmsForDevice(this);
            Index             = ID + AvailableDevices.AvailCpus; // increment by CPU count

            _nvHandle   = nvHandle;
            _nvmlDevice = nvmlHandle;

            ShouldRunEthlargement = cudaDevice.DeviceName.Contains("1080") || cudaDevice.DeviceName.Contains("Titan Xp");
            // plugin device
            var bd = new BaseDevice(DeviceType.NVIDIA, cudaDevice.UUID, cudaDevice.GetName(), (int)cudaDevice.DeviceID);

            PluginDevice = new CUDADevice(bd, cudaDevice.pciBusID, cudaDevice.DeviceGlobalMemory, cudaDevice.SM_major, cudaDevice.SM_minor);
        }
Esempio n. 6
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);
        }
        //TODO fix constructor to not include 3rd party handles + refactor inside interface device info (lookup fans speed/temps/...)
        public CudaComputeDevice(CudaDevice cudaDevice, DeviceGroupType group, int gpuCount,
                                 NvPhysicalGpuHandle nvHandle, nvmlDevice nvmlHandle)
            : base((int)cudaDevice.DeviceID,
                   cudaDevice.GetName(),
                   true,
                   group,
                   DeviceType.NVIDIA,
                   string.Format(Translations.Tr("GPU#{0}"), gpuCount),
                   cudaDevice.DeviceGlobalMemory)
        {
            BusID   = cudaDevice.pciBusID;
            SMMajor = cudaDevice.SM_major;
            SMMinor = cudaDevice.SM_minor;
            // if no nvml loaded fallback ID
            if (string.IsNullOrEmpty(cudaDevice.UUID))
            {
                Uuid = GetUuid(ID, GroupNames.GetGroupName(DeviceGroupType, ID), Name, DeviceGroupType);
            }
            else
            {
                Uuid = cudaDevice.UUID;
            }
            AlgorithmSettings = DefaultAlgorithms.GetAlgorithmsForDevice(this);
            Index             = ID + AvailableDevices.AvailCpus; // increment by CPU count

            _nvHandle   = nvHandle;
            _nvmlDevice = nvmlHandle;

            ShouldRunEthlargement = cudaDevice.DeviceName.Contains("1080") || cudaDevice.DeviceName.Contains("Titan Xp");
        }
Esempio n. 8
0
 private bool TryGetNvmlDevice(int gpuIndex, out nvmlDevice nvmlDevice)
 {
     nvmlDevice = default;
     if (gpuIndex < 0 || gpuIndex >= _nvmlDevices.Count)
     {
         return(false);
     }
     nvmlDevice = _nvmlDevices[gpuIndex];
     return(true);
 }
Esempio n. 9
0
        internal DeviceMonitorNVIDIA(NvapiNvmlInfo info)
        {
            UUID        = info.UUID;
            BusID       = info.BusID;
            _nvHandle   = info.nvHandle;
            _nvmlDevice = info.nvmlHandle;

            try
            {
                var powerInfo = new NvGPUPowerInfo
                {
                    Version = NVAPI.GPU_POWER_INFO_VER,
                    Entries = new NvGPUPowerInfoEntry[4]
                };

                var ret = NVAPI.NvAPI_DLL_ClientPowerPoliciesGetInfo(_nvHandle, ref powerInfo);
                if (ret != NvStatus.OK)
                {
                    throw new Exception(ret.ToString());
                }

                Debug.Assert(powerInfo.Entries.Length == 4);

                if (powerInfo.Entries[0].MinPower == 0 || powerInfo.Entries[0].MaxPower == 0)
                {
                    throw new Exception("Power control not available!");
                }

                _minPowerLimit     = powerInfo.Entries[0].MinPower;
                _maxPowerLimit     = powerInfo.Entries[0].MaxPower;
                _defaultPowerLimit = powerInfo.Entries[0].DefPower;

                PowerLimitsEnabled = true;
                // set to high by default
                var defaultLevel = PowerLevel.High;
                if (!DeviceMonitorManager.DisableDevicePowerModeSettings)
                {
                    var success = SetPowerTarget(defaultLevel);
                    if (!success)
                    {
                        Logger.Info("NVML", $"Cannot set power target ({defaultLevel.ToString()}) for device with BusID={BusID}");
                    }
                }
                else
                {
                    PowerLevel = PowerLevel.Disabled;
                }
            }
            catch (Exception e)
            {
                Logger.Error("NVML", $"Getting power info failed with message \"{e.Message}\", disabling power setting");
                PowerLimitsEnabled = false;
            }
        }
        private nvmlDevice GetNvmlDevice()
        {
            var nvmlHandle = new nvmlDevice();
            var nvmlRet    = NvmlNativeMethods.nvmlDeviceGetHandleByUUID(UUID, ref nvmlHandle);

            if (nvmlRet != nvmlReturn.Success)
            {
                //throw new NvmlException("nvmlDeviceGetHandleByUUID", nvmlRet);
            }
            return(nvmlHandle);
        }
        private bool SetTdp(nvmlDevice nvmlDevice, double tdpPerc, uint defaultLimit)
        {
            // TODO we limit to 100%
            uint currentLimit = (uint)(tdpPerc * (double)defaultLimit) / 100;
            var  ret          = NvmlNativeMethods.nvmlDeviceSetPowerManagementLimit(nvmlDevice, currentLimit);

            if (ret != nvmlReturn.Success)
            {
                throw new NvmlException("nvmlDeviceSetPowerManagementLimit", ret);
            }

            return(true);
        }
        private nvmlDevice GetNvmlDevice()
        {
            if (_nvmlDevice.HasValue)
            {
                return(_nvmlDevice.Value);
            }
            var nvmlHandle = new nvmlDevice();
            var nvmlRet    = NvmlNativeMethods.nvmlDeviceGetHandleByUUID(UUID, ref nvmlHandle);

            if (nvmlRet != nvmlReturn.Success)
            {
                throw new NvmlException("nvmlDeviceGetHandleByUUID", nvmlRet);
            }
            _nvmlDevice = nvmlHandle;
            return(nvmlHandle);
        }
Esempio n. 13
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);
        }
        public CudaComputeDevice(CudaDevice cudaDevice, DeviceGroupType group, int gpuCount,
                                 NvPhysicalGpuHandle nvHandle, nvmlDevice nvmlHandle)
            : base((int)cudaDevice.DeviceID,
                   cudaDevice.GetName(),
                   true,
                   group,
                   cudaDevice.IsEtherumCapable(),
                   DeviceType.NVIDIA,
                   string.Format(International.GetText("ComputeDevice_Short_Name_NVIDIA_GPU"), gpuCount),
                   cudaDevice.DeviceGlobalMemory)
        {
            BusID             = cudaDevice.pciBusID;
            SMMajor           = cudaDevice.SM_major;
            SMMinor           = cudaDevice.SM_minor;
            Uuid              = cudaDevice.UUID;
            AlgorithmSettings = GroupAlgorithms.CreateForDeviceList(this);
            Index             = ID + ComputeDeviceManager.Available.AvailCpus; // increment by CPU count

            _nvHandle   = nvHandle;
            _nvmlDevice = nvmlHandle;
        }
        public static List <NvapiNvmlInfo> Init(Dictionary <string, int> nvidiaUUIDAndBusIds, bool useNvmlFallback)
        {
            // Enumerate NVAPI handles and map to busid
            var idHandles = InitNvapi();

            if (!useNvmlFallback)
            {
                Logger.Info(Tag, "tryAddNvmlToEnvPath");
                tryAddNvmlToEnvPath();
            }
            else
            {
                Logger.Info(Tag, "tryAddNvmlToEnvPathFallback");
                tryAddNvmlToEnvPathFallback();
            }
            var nvmlInit = InitNvml();
            var ret      = new List <NvapiNvmlInfo>();

            foreach (var pair in nvidiaUUIDAndBusIds)
            {
                var uuid  = pair.Key;
                var busID = pair.Value;

                var nvmlHandle = new nvmlDevice();
                if (nvmlInit)
                {
                    var nvmlRet = NvmlNativeMethods.nvmlDeviceGetHandleByUUID(uuid, ref nvmlHandle);
                    Logger.Info(Tag, "NVML HANDLE:" + $"{(nvmlRet == nvmlReturn.Success ? nvmlHandle.Pointer.ToString() : $"Failed with code ret {ret}")}");
                }
                idHandles.TryGetValue(busID, out var handle);
                var info = new NvapiNvmlInfo
                {
                    UUID       = uuid,
                    BusID      = busID,
                    nvHandle   = handle,
                    nvmlHandle = nvmlHandle
                };
                ret.Add(info);
            }
Esempio n. 16
0
        public void LoadGpuState()
        {
            foreach (Gpu gpu in _gpus.Values)
            {
                int i = gpu.Index;
                if (i == NTMinerRoot.GpuAllId)
                {
                    continue;
                }
                nvmlDevice nvmlDevice = new nvmlDevice();
                var        nvmlReturn = NvmlNativeMethods.nvmlDeviceGetHandleByIndex((uint)i, ref nvmlDevice);
                // 显卡重启了,此时nvmlReturn是Unknown
                if (nvmlReturn == nvmlReturn.Unknown && nvmlShutdownRestartCount < 20)
                {
                    nvmlShutdownRestartCount++;
                    NvmlNativeMethods.nvmlShutdown();
                    _isNvmlInited = false;
                    NvmlInit();
                }
                nvmlReturn = NvmlNativeMethods.nvmlDeviceGetHandleByIndex((uint)i, ref nvmlDevice);
                SetGpuStatus(gpu, nvmlReturn);
                uint power = 0;
                NvmlNativeMethods.nvmlDeviceGetPowerUsage(nvmlDevice, ref power);
                power = (uint)(power / 1000.0);
                uint temp = 0;
                NvmlNativeMethods.nvmlDeviceGetTemperature(nvmlDevice, nvmlTemperatureSensors.Gpu, ref temp);
                uint speed = 0;
                NvmlNativeMethods.nvmlDeviceGetFanSpeed(nvmlDevice, ref speed);
                bool isChanged = gpu.Temperature != temp || gpu.PowerUsage != power || gpu.FanSpeed != speed;
                gpu.Temperature = (int)temp;
                gpu.PowerUsage  = power;
                gpu.FanSpeed    = speed;

                if (isChanged)
                {
                    VirtualRoot.Happened(new GpuStateChangedEvent(gpu));
                }
            }
        }
        public CudaComputeDevice(CudaDevice cudaDevice, DeviceGroupType group, int gpuCount,
                                 NvPhysicalGpuHandle nvHandle, nvmlDevice nvmlHandle)
            : base((int)cudaDevice.DeviceID,
                   cudaDevice.GetName(),
                   true,
                   group,
                   cudaDevice.IsEtherumCapable(),
                   DeviceType.NVIDIA,
                   string.Format(Translations.Tr("GPU#{0}"), gpuCount),
                   cudaDevice.DeviceGlobalMemory)
        {
            BusID             = cudaDevice.pciBusID;
            SMMajor           = cudaDevice.SM_major;
            SMMinor           = cudaDevice.SM_minor;
            Uuid              = cudaDevice.UUID;
            AlgorithmSettings = GroupAlgorithms.CreateForDeviceList(this);
            Index             = ID + AvailableDevices.AvailCpus; // increment by CPU count

            _nvHandle   = nvHandle;
            _nvmlDevice = nvmlHandle;

            ShouldRunEthlargement = cudaDevice.DeviceName.Contains("1080") || cudaDevice.DeviceName.Contains("Titan Xp");
        }
Esempio n. 18
0
        private static Nvidia[] DetectNvidiaGPUS()
        {
            if (NvmlNativeMethods.nvmlInit() == nvmlReturn.Success && NVAPI.IsAvailable)
            {
                uint deviceCount = 0;
                if (NvmlNativeMethods.nvmlDeviceGetCount(ref deviceCount) == nvmlReturn.Success)
                {
                    Nvidia[] nvgpu = new Nvidia[deviceCount];
                    for (uint i = 0; i < deviceCount; ++i)
                    {
                        nvmlDevice device = new nvmlDevice();
                        if (NvmlNativeMethods.nvmlDeviceGetHandleByIndex((uint)i, ref device) == nvmlReturn.Success)
                        {
                            nvgpu[i] = new Nvidia(i, device);
                        }
                    }

                    return(nvgpu);
                }
            }

            return(new Nvidia[0]);
        }
Esempio n. 19
0
        public void ResetHandles(NvapiNvmlInfo info)
        {
            _nvHandle   = info.nvHandle;
            _nvmlDevice = info.nvmlHandle;

            try
            {
                var powerInfo = new NvGPUPowerInfo
                {
                    Version = NVAPI.GPU_POWER_INFO_VER,
                    Entries = new NvGPUPowerInfoEntry[4]
                };

                var ret = NVAPI.NvAPI_DLL_ClientPowerPoliciesGetInfo(_nvHandle, ref powerInfo);
                if (ret != NvStatus.OK)
                {
                    throw new Exception(ret.ToString());
                }

                Debug.Assert(powerInfo.Entries.Length == 4);

                if (powerInfo.Entries[0].MinPower == 0 || powerInfo.Entries[0].MaxPower == 0)
                {
                    throw new Exception("Power control not available!");
                }

                _minPowerLimit     = powerInfo.Entries[0].MinPower;
                _maxPowerLimit     = powerInfo.Entries[0].MaxPower;
                _defaultPowerLimit = powerInfo.Entries[0].DefPower;
            }
            catch (Exception e)
            {
                Logger.Error("NVML", $"Getting power info failed with message \"{e.Message}\", disabling power setting");
                PowerLimitsEnabled = false;
            }
        }
Esempio n. 20
0
        public List <CudaComputeDevice> QueryCudaDevices()
        {
            Helpers.ConsolePrint(Tag, "QueryCudaDevices START");

            var compDevs = new List <CudaComputeDevice>();

            if (!CudaQuery.TryQueryCudaDevices(out var cudaDevs))
            {
                Helpers.ConsolePrint(Tag, "QueryCudaDevices END");
                return(compDevs);
            }

            var stringBuilder = new StringBuilder();

            stringBuilder.AppendLine("");
            stringBuilder.AppendLine("CudaDevicesDetection:");

            // Enumerate NVAPI handles and map to busid
            var idHandles = InitNvapi();

            tryAddNvmlToEnvPath();
            var nvmlInit = InitNvml();

            var numDevs = 0;

            foreach (var cudaDev in cudaDevs)
            {
                // We support SM3.0+
                var skip      = cudaDev.SM_major < 3;
                var skipOrAdd = skip ? "SKIPED" : "ADDED";
                stringBuilder.AppendLine($"\t{skipOrAdd} device:");
                stringBuilder.AppendLine($"\t\tID: {cudaDev.DeviceID}");
                stringBuilder.AppendLine($"\t\tBusID: {cudaDev.pciBusID}");
                stringBuilder.AppendLine($"\t\tNAME: {cudaDev.GetName()}");
                stringBuilder.AppendLine($"\t\tVENDOR: {cudaDev.VendorName}");
                stringBuilder.AppendLine($"\t\tUUID: {cudaDev.UUID}");
                stringBuilder.AppendLine($"\t\tSM: {cudaDev.SM_major}.{cudaDev.SM_minor}");
                stringBuilder.AppendLine($"\t\tMEMORY: {cudaDev.DeviceGlobalMemory}");

                if (skip)
                {
                    continue;
                }

                DeviceGroupType group;
                switch (cudaDev.SM_major)
                {
                case 3:
                    group = DeviceGroupType.NVIDIA_3_x;
                    break;

                case 5:
                    group = DeviceGroupType.NVIDIA_5_x;
                    break;

                case 6:
                    group = DeviceGroupType.NVIDIA_6_x;
                    break;

                default:
                    group = DeviceGroupType.NVIDIA_6_x;
                    break;
                }

                var nvmlHandle = new nvmlDevice();

                if (nvmlInit)
                {
                    var ret = NvmlNativeMethods.nvmlDeviceGetHandleByUUID(cudaDev.UUID, ref nvmlHandle);
                    stringBuilder.AppendLine(
                        "\t\tNVML HANDLE: " +
                        $"{(ret == nvmlReturn.Success ? nvmlHandle.Pointer.ToString() : $"Failed with code ret {ret}")}");
                }

                idHandles.TryGetValue(cudaDev.pciBusID, out var handle);
                compDevs.Add(new CudaComputeDevice(cudaDev, group, ++numDevs, handle, nvmlHandle));
            }
Esempio n. 21
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);
             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);
             }
             gpu.Name        = name;
             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 {
                 double driverVersionNum;
                 if (double.TryParse(_driverVersion, out 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)
                 {
                     NVIDIAOverClock.RefreshGpuState(gpu);
                 }
                 // 这里会耗时5秒
                 foreach (var kv in kvs)
                 {
                     Environment.SetEnvironmentVariable(kv.Key, kv.Value);
                 }
             });
         }
     }
 }
Esempio n. 22
0
        public NVIDIAGpuSet(INTMinerRoot root) : this()
        {
            _root = root;
            if (Design.IsInDesignMode)
            {
                return;
            }
            string nvsmiDir = Path.Combine(Environment.GetFolderPath(System.Environment.SpecialFolder.ProgramFiles), "NVIDIA Corporation", "NVSMI");

            if (Directory.Exists(nvsmiDir))
            {
                Windows.NativeMethods.SetDllDirectory(nvsmiDir);
                NvmlNativeMethods.nvmlInit();
                _isNvmlInited = true;
                NvmlNativeMethods.nvmlDeviceGetCount(ref deviceCount);
                for (int i = 0; i < deviceCount; i++)
                {
                    nvmlDevice nvmlDevice = new nvmlDevice();
                    NvmlNativeMethods.nvmlDeviceGetHandleByIndex((uint)i, ref nvmlDevice);
                    uint gClock = 0, mClock = 0;
                    NvmlNativeMethods.nvmlDeviceGetName(nvmlDevice, out string name);
                    NvmlNativeMethods.nvmlDeviceGetMaxClockInfo(nvmlDevice, nvmlClockType.Graphics, ref gClock);
                    NvmlNativeMethods.nvmlDeviceGetMaxClockInfo(nvmlDevice, nvmlClockType.Mem, ref mClock);
                    if (!string.IsNullOrEmpty(name))
                    {
                        name = name.Replace("GeForce ", string.Empty);
                    }
                    Gpu gpu = new Gpu {
                        Index       = i,
                        Name        = name,
                        Temperature = 0,
                        PowerUsage  = 0,
                        FanSpeed    = 0,
                        OverClock   = new NVIDIAOverClock()
                    };
                    _gpus.Add(i, gpu);
                }
                if (deviceCount > 0)
                {
                    NvmlNativeMethods.nvmlSystemGetDriverVersion(out string driverVersion);
                    NvmlNativeMethods.nvmlSystemGetNVMLVersion(out string nvmlVersion);
                    this.Properties.Add(new GpuSetProperty("DriverVersion", "driver version", driverVersion));
                    this.Properties.Add(new GpuSetProperty("NVMLVersion", "NVML version", 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(() => {
                        // 这里会耗时5秒
                        foreach (var kv in kvs)
                        {
                            Environment.SetEnvironmentVariable(kv.Key, kv.Value);
                        }
                        foreach (var gpu in _gpus.Values)
                        {
                            NVIDIAOverClock.RefreshGpuState(gpu);
                        }
                    });
                }
            }
        }
Esempio n. 23
0
                public static void QueryCudaDevices()
                {
                    Helpers.ConsolePrint(Tag, "QueryCudaDevices START");
                    QueryCudaDevices(ref _cudaDevices);

                    if (_cudaDevices != null && _cudaDevices.Count != 0)
                    {
                        Available.HasNvidia = true;
                        var stringBuilder = new StringBuilder();
                        stringBuilder.AppendLine("");
                        stringBuilder.AppendLine("CudaDevicesDetection:");

                        // Enumerate NVAPI handles and map to busid
                        var idHandles = new Dictionary <int, NvPhysicalGpuHandle>();
                        if (NVAPI.IsAvailable)
                        {
                            var handles = new NvPhysicalGpuHandle[NVAPI.MAX_PHYSICAL_GPUS];
                            if (NVAPI.NvAPI_EnumPhysicalGPUs == null)
                            {
                                Helpers.ConsolePrint("NVAPI", "NvAPI_EnumPhysicalGPUs unavailable");
                            }
                            else
                            {
                                var status = NVAPI.NvAPI_EnumPhysicalGPUs(handles, out var _);
                                if (status != NvStatus.OK)
                                {
                                    Helpers.ConsolePrint("NVAPI", "Enum physical GPUs failed with status: " + status);
                                }
                                else
                                {
                                    foreach (var handle in handles)
                                    {
                                        var idStatus = NVAPI.NvAPI_GPU_GetBusID(handle, out var id);
                                        if (idStatus != NvStatus.EXPECTED_PHYSICAL_GPU_HANDLE)
                                        {
                                            if (idStatus != NvStatus.OK)
                                            {
                                                Helpers.ConsolePrint("NVAPI",
                                                                     "Bus ID get failed with status: " + idStatus);
                                            }
                                            else
                                            {
                                                Helpers.ConsolePrint("NVAPI", "Found handle for busid " + id);
                                                idHandles[id] = handle;
                                            }
                                        }
                                    }
                                }
                            }
                        }

                        var nvmlInit = false;
                        try
                        {
                            var ret = NvmlNativeMethods.nvmlInit();
                            if (ret != nvmlReturn.Success)
                            {
                                throw new Exception($"NVML init failed with code {ret}");
                            }
                            nvmlInit = true;
                        }
                        catch (Exception e)
                        {
                            Helpers.ConsolePrint("NVML", e.ToString());
                        }

                        foreach (var cudaDev in _cudaDevices)
                        {
                            // check sm vesrions
                            bool isUnderSM21;
                            {
                                var isUnderSM2Major = cudaDev.SM_major < 2;
                                var isUnderSM1Minor = cudaDev.SM_minor < 1;
                                isUnderSM21 = isUnderSM2Major && isUnderSM1Minor;
                            }
                            //bool isOverSM6 = cudaDev.SM_major > 6;
                            var          skip               = isUnderSM21;
                            var          skipOrAdd          = skip ? "SKIPED" : "ADDED";
                            const string isDisabledGroupStr = ""; // TODO remove
                            var          etherumCapableStr  = cudaDev.IsEtherumCapable() ? "YES" : "NO";
                            stringBuilder.AppendLine($"\t{skipOrAdd} device{isDisabledGroupStr}:");
                            stringBuilder.AppendLine($"\t\tID: {cudaDev.DeviceID}");
                            stringBuilder.AppendLine($"\t\tBusID: {cudaDev.pciBusID}");
                            stringBuilder.AppendLine($"\t\tNAME: {cudaDev.GetName()}");
                            stringBuilder.AppendLine($"\t\tVENDOR: {cudaDev.VendorName}");
                            stringBuilder.AppendLine($"\t\tUUID: {cudaDev.UUID}");
                            stringBuilder.AppendLine($"\t\tSM: {cudaDev.SMVersionString}");
                            stringBuilder.AppendLine($"\t\tMEMORY: {cudaDev.DeviceGlobalMemory}");
                            stringBuilder.AppendLine($"\t\tETHEREUM: {etherumCapableStr}");

                            if (!skip)
                            {
                                DeviceGroupType group;
                                switch (cudaDev.SM_major)
                                {
                                case 2:
                                    group = DeviceGroupType.NVIDIA_2_1;
                                    break;

                                case 3:
                                    group = DeviceGroupType.NVIDIA_3_x;
                                    break;

                                case 5:
                                    group = DeviceGroupType.NVIDIA_5_x;
                                    break;

                                case 6:
                                    group = DeviceGroupType.NVIDIA_6_x;
                                    break;

                                default:
                                    group = DeviceGroupType.NVIDIA_6_x;
                                    break;
                                }

                                var nvmlHandle = new nvmlDevice();

                                if (nvmlInit)
                                {
                                    var ret = NvmlNativeMethods.nvmlDeviceGetHandleByUUID(cudaDev.UUID, ref nvmlHandle);
                                    stringBuilder.AppendLine(
                                        "\t\tNVML HANDLE: " +
                                        $"{(ret == nvmlReturn.Success ? nvmlHandle.Pointer.ToString() : $"Failed with code ret {ret}")}");
                                }

                                idHandles.TryGetValue(cudaDev.pciBusID, out var handle);
                                Available.Devices.Add(
                                    new CudaComputeDevice(cudaDev, group, ++GpuCount, handle, nvmlHandle)
                                    );
                            }
                        }
                        Helpers.ConsolePrint(Tag, stringBuilder.ToString());
                    }