internal static extern int ADL2_Adapter_AdapterInfo_Get(IntPtr context, ref ADLAdapterInfoArray lpInfo, int iInputSize);
static void Main(string[] args) { int ADLRet = -1; int NumberOfAdapters = 0; int NumberOfDisplays = 0; if (null != ADL.ADL_Main_Control_Create) // Second parameter is 1: Get only the present adapters ADLRet = ADL.ADL_Main_Control_Create(ADL.ADL_Main_Memory_Alloc, 1); if (ADL.ADL_SUCCESS == ADLRet) { if (null != ADL.ADL_Adapter_NumberOfAdapters_Get) { ADL.ADL_Adapter_NumberOfAdapters_Get(ref NumberOfAdapters); } Console.WriteLine("Number Of Adapters: " + NumberOfAdapters.ToString() + "\n"); if (0 < NumberOfAdapters) { // Get OS adpater info from ADL ADLAdapterInfoArray OSAdapterInfoData; OSAdapterInfoData = new ADLAdapterInfoArray(); if (null != ADL.ADL_Adapter_AdapterInfo_Get) { IntPtr AdapterBuffer = IntPtr.Zero; int size = Marshal.SizeOf(OSAdapterInfoData); AdapterBuffer = Marshal.AllocCoTaskMem((int)size); Marshal.StructureToPtr(OSAdapterInfoData, AdapterBuffer, false); if (null != ADL.ADL_Adapter_AdapterInfo_Get) { ADLRet = ADL.ADL_Adapter_AdapterInfo_Get(AdapterBuffer, size); if (ADL.ADL_SUCCESS == ADLRet) { OSAdapterInfoData = (ADLAdapterInfoArray)Marshal.PtrToStructure(AdapterBuffer, OSAdapterInfoData.GetType()); int IsActive = 0; for (int i = 0; i < NumberOfAdapters; i++) { // Check if the adapter is active if (null != ADL.ADL_Adapter_Active_Get) ADLRet = ADL.ADL_Adapter_Active_Get(OSAdapterInfoData.ADLAdapterInfo[i].AdapterIndex, ref IsActive); if (ADL.ADL_SUCCESS == ADLRet) { Console.WriteLine("Adapter is : " + (0 == IsActive ? "DISABLED" : "ENABLED")); Console.WriteLine("Adapter Index: " + OSAdapterInfoData.ADLAdapterInfo[i].AdapterIndex.ToString()); Console.WriteLine("Adapter UDID : " + OSAdapterInfoData.ADLAdapterInfo[i].UDID); Console.WriteLine("Bus No : " + OSAdapterInfoData.ADLAdapterInfo[i].BusNumber.ToString()); Console.WriteLine("Driver No : " + OSAdapterInfoData.ADLAdapterInfo[i].DriverNumber.ToString()); Console.WriteLine("Function No : " + OSAdapterInfoData.ADLAdapterInfo[i].FunctionNumber.ToString()); Console.WriteLine("Vendor ID : " + OSAdapterInfoData.ADLAdapterInfo[i].VendorID.ToString()); Console.WriteLine("Adapter Name : " + OSAdapterInfoData.ADLAdapterInfo[i].AdapterName); Console.WriteLine("Display Name : " + OSAdapterInfoData.ADLAdapterInfo[i].DisplayName); Console.WriteLine("Present : " + (0 == OSAdapterInfoData.ADLAdapterInfo[i].Present ? "No" : "Yes")); Console.WriteLine("Exist : " + (0 == OSAdapterInfoData.ADLAdapterInfo[i].Exist ? "No" : "Yes")); Console.WriteLine("Driver Path : " + OSAdapterInfoData.ADLAdapterInfo[i].DriverPath); Console.WriteLine("Driver Path X: " + OSAdapterInfoData.ADLAdapterInfo[i].DriverPathExt); Console.WriteLine("PNP String : " + OSAdapterInfoData.ADLAdapterInfo[i].PNPString); // Obtain information about displays ADLDisplayInfo oneDisplayInfo = new ADLDisplayInfo(); if (null != ADL.ADL_Display_DisplayInfo_Get) { IntPtr DisplayBuffer = IntPtr.Zero; int j = 0; // Force the display detection and get the Display Info. Use 0 as last parameter to NOT force detection ADLRet = ADL.ADL_Display_DisplayInfo_Get(OSAdapterInfoData.ADLAdapterInfo[i].AdapterIndex, ref NumberOfDisplays, out DisplayBuffer, 1); if (ADL.ADL_SUCCESS == ADLRet) { List<ADLDisplayInfo> DisplayInfoData = new List<ADLDisplayInfo>(); for (j = 0; j < NumberOfDisplays; j++) { oneDisplayInfo = (ADLDisplayInfo)Marshal.PtrToStructure(new IntPtr(DisplayBuffer.ToInt32() + j * Marshal.SizeOf(oneDisplayInfo)), oneDisplayInfo.GetType()); DisplayInfoData.Add(oneDisplayInfo); } Console.WriteLine("\nTotal Number of Displays supported: " + NumberOfDisplays.ToString()); Console.WriteLine("\nDispID AdpID Type OutType CnctType Connected Mapped InfoValue DisplayName "); for (j = 0; j < NumberOfDisplays; j++) { int InfoValue = DisplayInfoData[j].DisplayInfoValue; string StrConnected = (1 == (InfoValue & 1)) ? "Yes" : "No "; string StrMapped = (2 == (InfoValue & 2)) ? "Yes" : "No "; int AdpID = DisplayInfoData[j].DisplayID.DisplayLogicalAdapterIndex; string StrAdpID = (AdpID < 0) ? "--" : AdpID.ToString("d2"); Console.WriteLine(DisplayInfoData[j].DisplayID.DisplayLogicalIndex.ToString() + " " + StrAdpID + " " + DisplayInfoData[j].DisplayType.ToString() + " " + DisplayInfoData[j].DisplayOutputType.ToString() + " " + DisplayInfoData[j].DisplayConnector.ToString() + " " + StrConnected + " " + StrMapped + " " + InfoValue.ToString("x4") + " " + DisplayInfoData[j].DisplayName.ToString()); } Console.WriteLine(); } else { Console.WriteLine("ADL_Display_DisplayInfo_Get() returned error code " + ADLRet.ToString()); } // Release the memory for the DisplayInfo structure if (IntPtr.Zero != DisplayBuffer) Marshal.FreeCoTaskMem(DisplayBuffer); } } } } else { Console.WriteLine("ADL_Adapter_AdapterInfo_Get() returned error code " + ADLRet.ToString()); } } // Release the memory for the AdapterInfo structure if (IntPtr.Zero != AdapterBuffer) Marshal.FreeCoTaskMem(AdapterBuffer); } } if (null != ADL.ADL_Main_Control_Destroy) ADL.ADL_Main_Control_Destroy(); } else { Console.WriteLine("ADL_Main_Control_Create() returned error code " + ADLRet.ToString()); Console.WriteLine("\nCheck if ADL is properly installed!\n"); } Console.WriteLine("Press ENTER to EXIT"); Console.ReadLine(); }
internal static extern int ADL_Adapter_AdapterInfo_Get(ref ADLAdapterInfoArray info, int inputSize);
public static void QueryAMD() { const int AMD_VENDOR_ID = 1002; Helpers.ConsolePrint(TAG, "QueryAMD START"); #region AMD driver check, ADL returns 0 // check the driver version bool EnableOptimizedVersion = true; Dictionary<string, bool> deviceDriverOld = new Dictionary<string, bool>(); string minerPath = MinerPaths.sgminer_5_5_0_general; bool ShowWarningDialog = false; foreach (var vidContrllr in AvaliableVideoControllers) { Helpers.ConsolePrint(TAG, String.Format("Checking AMD device (driver): {0} ({1})", vidContrllr.Name, vidContrllr.DriverVersion)); deviceDriverOld[vidContrllr.Name] = false; // TODO checking radeon drivers only? if ((vidContrllr.Name.Contains("AMD") || vidContrllr.Name.Contains("Radeon")) && ShowWarningDialog == false) { Version AMDDriverVersion = new Version(vidContrllr.DriverVersion); if (AMDDriverVersion.Major < 15) { ShowWarningDialog = true; deviceDriverOld[vidContrllr.Name] = true; Helpers.ConsolePrint(TAG, "WARNING!!! Old AMD GPU driver detected! All optimized versions disabled, mining " + "speed will not be optimal. Consider upgrading AMD GPU driver. Recommended AMD GPU driver version is 15.7.1."); } else if (AMDDriverVersion.Major == 16 && AMDDriverVersion.Minor >= 150) { if (MinersDownloadManager.IsMinerBinFolder()) { // TODO why this copy? string src = System.IO.Path.GetDirectoryName(Application.ExecutablePath) + "\\" + minerPath.Split('\\')[0] + "\\" + minerPath.Split('\\')[1] + "\\kernel"; foreach (var file in Directory.GetFiles(src)) { string dest = Environment.GetFolderPath(Environment.SpecialFolder.LocalApplicationData) + "\\Temp\\" + System.IO.Path.GetFileName(file); if (!File.Exists(dest)) File.Copy(file, dest, false); } } } } } if (ConfigManager.GeneralConfig.ShowDriverVersionWarning && ShowWarningDialog == true) { Form WarningDialog = new DriverVersionConfirmationDialog(); WarningDialog.ShowDialog(); WarningDialog = null; } #endregion // AMD driver check // get platform version showMessageAndStep(International.GetText("Compute_Device_Query_Manager_AMD_Query")); List<OpenCLDevice> amdOCLDevices = new List<OpenCLDevice>(); string AMDOpenCLPlatformStringKey = ""; if (IsOpenCLQuerrySuccess) { bool amdPlatformNumFound = false; foreach (var oclEl in OpenCLJSONData) { if (oclEl.PlatformName.Contains("AMD") || oclEl.PlatformName.Contains("amd")) { amdPlatformNumFound = true; AMDOpenCLPlatformStringKey = oclEl.PlatformName; Avaliable.AMDOpenCLPlatformNum = oclEl.PlatformNum; amdOCLDevices = oclEl.Devices; Helpers.ConsolePrint(TAG, String.Format("AMD platform found: Key: {0}, Num: {1}", AMDOpenCLPlatformStringKey, Avaliable.AMDOpenCLPlatformNum.ToString())); break; } } if (amdPlatformNumFound) { // get only AMD gpus { foreach (var oclDev in amdOCLDevices) { if (oclDev._CL_DEVICE_TYPE.Contains("GPU")) { amdGpus.Add(oclDev); } } } bool isBusID_OK = true; // check if buss ids are unuque and different from -1 { HashSet<int> bus_ids = new HashSet<int>(); foreach (var amdOclDev in amdGpus) { if (amdOclDev.AMD_BUS_ID < 0) { isBusID_OK = false; break; } bus_ids.Add(amdOclDev.AMD_BUS_ID); } // check if unique isBusID_OK = isBusID_OK && bus_ids.Count == amdGpus.Count; } if (amdGpus.Count == 0) { Helpers.ConsolePrint(TAG, "AMD GPUs count is 0"); } else { // print BUS id status if (isBusID_OK) { Helpers.ConsolePrint(TAG, "AMD Bus IDs are unique and valid. OK"); } else { Helpers.ConsolePrint(TAG, "AMD Bus IDs IS INVALID. Using fallback AMD detection mode"); } Helpers.ConsolePrint(TAG, "AMD GPUs count : " + amdGpus.Count.ToString()); Helpers.ConsolePrint(TAG, "AMD Getting device name and serial from ADL"); // ADL bool isAdlInit = true; // ADL does not get devices in order map devices by bus number // bus id, <name, uuid> Dictionary<int, Tuple3<string, string, string>> _busIdsInfo = new Dictionary<int, Tuple3<string, string, string>>(); List<string> _amdDeviceName = new List<string>(); List<string> _amdDeviceUUID = new List<string>(); try { int ADLRet = -1; int NumberOfAdapters = 0; if (null != ADL.ADL_Main_Control_Create) // Second parameter is 1: Get only the present adapters ADLRet = ADL.ADL_Main_Control_Create(ADL.ADL_Main_Memory_Alloc, 1); if (ADL.ADL_SUCCESS == ADLRet) { if (null != ADL.ADL_Adapter_NumberOfAdapters_Get) { ADL.ADL_Adapter_NumberOfAdapters_Get(ref NumberOfAdapters); } Helpers.ConsolePrint(TAG, "Number Of Adapters: " + NumberOfAdapters.ToString()); if (0 < NumberOfAdapters) { // Get OS adpater info from ADL ADLAdapterInfoArray OSAdapterInfoData; OSAdapterInfoData = new ADLAdapterInfoArray(); if (null != ADL.ADL_Adapter_AdapterInfo_Get) { IntPtr AdapterBuffer = IntPtr.Zero; int size = Marshal.SizeOf(OSAdapterInfoData); AdapterBuffer = Marshal.AllocCoTaskMem((int)size); Marshal.StructureToPtr(OSAdapterInfoData, AdapterBuffer, false); if (null != ADL.ADL_Adapter_AdapterInfo_Get) { ADLRet = ADL.ADL_Adapter_AdapterInfo_Get(AdapterBuffer, size); if (ADL.ADL_SUCCESS == ADLRet) { OSAdapterInfoData = (ADLAdapterInfoArray)Marshal.PtrToStructure(AdapterBuffer, OSAdapterInfoData.GetType()); int IsActive = 0; for (int i = 0; i < NumberOfAdapters; i++) { // Check if the adapter is active if (null != ADL.ADL_Adapter_Active_Get) ADLRet = ADL.ADL_Adapter_Active_Get(OSAdapterInfoData.ADLAdapterInfo[i].AdapterIndex, ref IsActive); if (ADL.ADL_SUCCESS == ADLRet) { // we are looking for amd // TODO check discrete and integrated GPU separation var vendorID = OSAdapterInfoData.ADLAdapterInfo[i].VendorID; var devName = OSAdapterInfoData.ADLAdapterInfo[i].AdapterName; if (vendorID == AMD_VENDOR_ID || devName.ToLower().Contains("amd") || devName.ToLower().Contains("radeon") || devName.ToLower().Contains("firepro")) { string PNPStr = OSAdapterInfoData.ADLAdapterInfo[i].PNPString; // find vi controller pnp string infSection = ""; foreach (var v_ctrl in AvaliableVideoControllers) { if(v_ctrl.PNPDeviceID == PNPStr) { infSection = v_ctrl.InfSection; } } var backSlashLast = PNPStr.LastIndexOf('\\'); var serial = PNPStr.Substring(backSlashLast, PNPStr.Length - backSlashLast); var end_0 = serial.IndexOf('&'); var end_1 = serial.IndexOf('&', end_0 + 1); // get serial serial = serial.Substring(end_0 + 1, (end_1 - end_0) - 1); var udid = OSAdapterInfoData.ADLAdapterInfo[i].UDID; var pciVen_id_strSize = 21; // PCI_VEN_XXXX&DEV_XXXX var uuid = udid.Substring(0, pciVen_id_strSize) + "_" + serial; int budId = OSAdapterInfoData.ADLAdapterInfo[i].BusNumber; if (!_amdDeviceUUID.Contains(uuid)) { try { Helpers.ConsolePrint(TAG, String.Format("ADL device added BusNumber:{0} NAME:{1} UUID:{2}"), budId, devName, uuid); } catch { } _amdDeviceUUID.Add(uuid); //_busIds.Add(OSAdapterInfoData.ADLAdapterInfo[i].BusNumber); _amdDeviceName.Add(devName); if (!_busIdsInfo.ContainsKey(budId)) { var nameUuid = new Tuple3<string, string, string>(devName, uuid, infSection); _busIdsInfo.Add(budId, nameUuid); } } } } } } else { Helpers.ConsolePrint(TAG, "ADL_Adapter_AdapterInfo_Get() returned error code " + ADLRet.ToString()); isAdlInit = false; } } // Release the memory for the AdapterInfo structure if (IntPtr.Zero != AdapterBuffer) Marshal.FreeCoTaskMem(AdapterBuffer); } } if (null != ADL.ADL_Main_Control_Destroy) ADL.ADL_Main_Control_Destroy(); } else { // TODO Helpers.ConsolePrint(TAG, "ADL_Main_Control_Create() returned error code " + ADLRet.ToString()); Helpers.ConsolePrint(TAG, "Check if ADL is properly installed!"); isAdlInit = false; } } catch (Exception ex) { Helpers.ConsolePrint(TAG, "AMD ADL exception: " + ex.Message); isAdlInit = false; } /////// // AMD device creation (in NHM context) if (isAdlInit && isBusID_OK) { Helpers.ConsolePrint(TAG, "Using AMD device creation DEFAULT Reliable mappings"); if (amdGpus.Count == _amdDeviceUUID.Count) { Helpers.ConsolePrint(TAG, "AMD OpenCL and ADL AMD query COUNTS GOOD/SAME"); } else { Helpers.ConsolePrint(TAG, "AMD OpenCL and ADL AMD query COUNTS DIFFERENT/BAD"); } StringBuilder stringBuilder = new StringBuilder(); stringBuilder.AppendLine(""); stringBuilder.AppendLine("QueryAMD [DEFAULT query] devices: "); for (int i_id = 0; i_id < amdGpus.Count; ++i_id) { Avaliable.HasAMD = true; int busID = amdGpus[i_id].AMD_BUS_ID; if (busID != -1 && _busIdsInfo.ContainsKey(busID)) { var deviceName = _busIdsInfo[busID].Item1; var newAmdDev = new AmdGpuDevice(amdGpus[i_id], deviceDriverOld[deviceName], _busIdsInfo[busID].Item3); newAmdDev.DeviceName = deviceName; newAmdDev.UUID = _busIdsInfo[busID].Item2; bool isDisabledGroup = ConfigManager.GeneralConfig.DeviceDetection.DisableDetectionAMD; string skipOrAdd = isDisabledGroup ? "SKIPED" : "ADDED"; string isDisabledGroupStr = isDisabledGroup ? " (AMD group disabled)" : ""; string etherumCapableStr = newAmdDev.IsEtherumCapable() ? "YES" : "NO"; Avaliable.AllAvaliableDevices.Add( new ComputeDevice(newAmdDev, ++GPUCount, false)); // just in case try { stringBuilder.AppendLine(String.Format("\t{0} device{1}:", skipOrAdd, isDisabledGroupStr)); stringBuilder.AppendLine(String.Format("\t\tID: {0}", newAmdDev.DeviceID.ToString())); stringBuilder.AppendLine(String.Format("\t\tNAME: {0}", newAmdDev.DeviceName)); stringBuilder.AppendLine(String.Format("\t\tCODE_NAME: {0}", newAmdDev.Codename)); stringBuilder.AppendLine(String.Format("\t\tUUID: {0}", newAmdDev.UUID)); stringBuilder.AppendLine(String.Format("\t\tMEMORY: {0}", newAmdDev.DeviceGlobalMemory.ToString())); stringBuilder.AppendLine(String.Format("\t\tETHEREUM: {0}", etherumCapableStr)); } catch { } } else { stringBuilder.AppendLine(String.Format("\tDevice not added, Bus No. {0} not found:", busID)); } } Helpers.ConsolePrint(TAG, stringBuilder.ToString()); } else { Helpers.ConsolePrint(TAG, "Using AMD device creation FALLBACK UnReliable mappings"); StringBuilder stringBuilder = new StringBuilder(); stringBuilder.AppendLine(""); stringBuilder.AppendLine("QueryAMD [FALLBACK query] devices: "); // get video AMD controllers and sort them by RAM // (find a way to get PCI BUS Numbers from PNPDeviceID) List<VideoControllerData> AMDVideoControllers = new List<VideoControllerData>(); foreach (var vcd in AvaliableVideoControllers) { if (vcd.Name.ToLower().Contains("amd") || vcd.Name.ToLower().Contains("radeon") || vcd.Name.ToLower().Contains("firepro")) { AMDVideoControllers.Add(vcd); } } // sort by ram not ideal AMDVideoControllers.Sort((a, b) => (int)(a.AdapterRAM - b.AdapterRAM)); amdGpus.Sort((a, b) => (int)(a._CL_DEVICE_GLOBAL_MEM_SIZE - b._CL_DEVICE_GLOBAL_MEM_SIZE)); int minCount = Math.Min(AMDVideoControllers.Count, amdGpus.Count); for (int i = 0; i < minCount; ++i) { Avaliable.HasAMD = true; var deviceName = AMDVideoControllers[i].Name; if(AMDVideoControllers[i].InfSection == null) AMDVideoControllers[i].InfSection = ""; var newAmdDev = new AmdGpuDevice(amdGpus[i], deviceDriverOld[deviceName], AMDVideoControllers[i].InfSection); newAmdDev.DeviceName = deviceName; newAmdDev.UUID = "UNUSED"; bool isDisabledGroup = ConfigManager.GeneralConfig.DeviceDetection.DisableDetectionAMD; string skipOrAdd = isDisabledGroup ? "SKIPED" : "ADDED"; string isDisabledGroupStr = isDisabledGroup ? " (AMD group disabled)" : ""; string etherumCapableStr = newAmdDev.IsEtherumCapable() ? "YES" : "NO"; Avaliable.AllAvaliableDevices.Add( new ComputeDevice(newAmdDev, ++GPUCount, true)); // just in case try { stringBuilder.AppendLine(String.Format("\t{0} device{1}:", skipOrAdd, isDisabledGroupStr)); stringBuilder.AppendLine(String.Format("\t\tID: {0}", newAmdDev.DeviceID.ToString())); stringBuilder.AppendLine(String.Format("\t\tNAME: {0}", newAmdDev.DeviceName)); stringBuilder.AppendLine(String.Format("\t\tCODE_NAME: {0}", newAmdDev.Codename)); stringBuilder.AppendLine(String.Format("\t\tUUID: {0}", newAmdDev.UUID)); stringBuilder.AppendLine(String.Format("\t\tMEMORY: {0}", newAmdDev.DeviceGlobalMemory.ToString())); stringBuilder.AppendLine(String.Format("\t\tETHEREUM: {0}", etherumCapableStr)); } catch { } } Helpers.ConsolePrint(TAG, stringBuilder.ToString()); } } } // end is amdPlatformNumFound } // end is OpenCLSuccess Helpers.ConsolePrint(TAG, "QueryAMD END"); }