From cd2fd9d7a62b1c3637ea77b041b30bb96f89a625 Mon Sep 17 00:00:00 2001 From: XMRig Date: Fri, 8 Nov 2024 13:03:35 +0700 Subject: [PATCH] Simplified getting PCI topology for the OpenCL backend. --- src/backend/common/misc/PciTopology.h | 17 ++++++++----- src/backend/cuda/wrappers/CudaDevice.cpp | 9 ++++--- src/backend/opencl/wrappers/OclDevice.cpp | 29 ++++++++++++----------- 3 files changed, 30 insertions(+), 25 deletions(-) diff --git a/src/backend/common/misc/PciTopology.h b/src/backend/common/misc/PciTopology.h index af1844c94..acef4d918 100644 --- a/src/backend/common/misc/PciTopology.h +++ b/src/backend/common/misc/PciTopology.h @@ -1,6 +1,6 @@ /* XMRig - * Copyright (c) 2018-2021 SChernykh - * Copyright (c) 2016-2021 XMRig , + * Copyright (c) 2018-2024 SChernykh + * Copyright (c) 2016-2024 XMRig , * * This program is free software: you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by @@ -19,10 +19,8 @@ #ifndef XMRIG_PCITOPOLOGY_H #define XMRIG_PCITOPOLOGY_H - #include - #include "base/tools/String.h" @@ -33,7 +31,14 @@ class PciTopology { public: PciTopology() = default; - PciTopology(uint32_t bus, uint32_t device, uint32_t function) : m_valid(true), m_bus(bus), m_device(device), m_function(function) {} + + template + inline PciTopology(T bus, T device, T function) + : m_valid(true), + m_bus(static_cast(bus)), + m_device(static_cast(device)), + m_function(static_cast(function)) + {} inline bool isEqual(const PciTopology &other) const { return m_valid == other.m_valid && toUint32() == other.toUint32(); } inline bool isValid() const { return m_valid; } @@ -70,4 +75,4 @@ private: } // namespace xmrig -#endif /* XMRIG_PCITOPOLOGY_H */ +#endif // XMRIG_PCITOPOLOGY_H diff --git a/src/backend/cuda/wrappers/CudaDevice.cpp b/src/backend/cuda/wrappers/CudaDevice.cpp index f06fe9401..b160e4287 100644 --- a/src/backend/cuda/wrappers/CudaDevice.cpp +++ b/src/backend/cuda/wrappers/CudaDevice.cpp @@ -5,8 +5,8 @@ * Copyright 2014-2016 Wolf9466 * Copyright 2016 Jay D Dee * Copyright 2017-2018 XMR-Stak , - * Copyright 2018-2020 SChernykh - * Copyright 2016-2020 XMRig , + * Copyright 2018-2024 SChernykh + * Copyright 2016-2024 XMRig , * * This program is free software: you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by @@ -22,7 +22,6 @@ * along with this program. If not, see . */ - #include "backend/cuda/wrappers/CudaDevice.h" #include "3rdparty/rapidjson/document.h" #include "backend/cuda/CudaThreads.h" @@ -41,7 +40,7 @@ xmrig::CudaDevice::CudaDevice(uint32_t index, int32_t bfactor, int32_t bsleep) : m_index(index) { - auto ctx = CudaLib::alloc(index, bfactor, bsleep); + auto *ctx = CudaLib::alloc(index, bfactor, bsleep); if (!CudaLib::deviceInfo(ctx, 0, 0, Algorithm::INVALID)) { CudaLib::release(ctx); @@ -50,7 +49,7 @@ xmrig::CudaDevice::CudaDevice(uint32_t index, int32_t bfactor, int32_t bsleep) : m_ctx = ctx; m_name = CudaLib::deviceName(ctx); - m_topology = PciTopology(CudaLib::deviceUint(ctx, CudaLib::DevicePciBusID), CudaLib::deviceUint(ctx, CudaLib::DevicePciDeviceID), 0); + m_topology = { CudaLib::deviceUint(ctx, CudaLib::DevicePciBusID), CudaLib::deviceUint(ctx, CudaLib::DevicePciDeviceID), 0U }; } diff --git a/src/backend/opencl/wrappers/OclDevice.cpp b/src/backend/opencl/wrappers/OclDevice.cpp index 77a1c2b34..846b59723 100644 --- a/src/backend/opencl/wrappers/OclDevice.cpp +++ b/src/backend/opencl/wrappers/OclDevice.cpp @@ -35,17 +35,18 @@ #include -// NOLINTNEXTLINE(modernize-use-using) -typedef union -{ - struct { cl_uint type; cl_uint data[5]; } raw; - struct { cl_uint type; cl_char unused[17]; cl_char bus; cl_char device; cl_char function; } pcie; -} topology_amd; - - namespace xmrig { +struct topology_amd { + cl_uint type; + cl_char unused[17]; + cl_char bus; + cl_char device; + cl_char function; +}; + + #ifdef XMRIG_ALGO_RANDOMX extern bool ocl_generic_rx_generator(const OclDevice &device, const Algorithm &algorithm, OclThreads &threads); #endif @@ -138,18 +139,18 @@ xmrig::OclDevice::OclDevice(uint32_t index, cl_device_id id, cl_platform_id plat m_type = getType(m_name); if (m_extensions.contains("cl_amd_device_attribute_query")) { - topology_amd topology; - - if (OclLib::getDeviceInfo(id, CL_DEVICE_TOPOLOGY_AMD, sizeof(topology), &topology, nullptr) == CL_SUCCESS && topology.raw.type == CL_DEVICE_TOPOLOGY_TYPE_PCIE_AMD) { - m_topology = PciTopology(static_cast(topology.pcie.bus), static_cast(topology.pcie.device), static_cast(topology.pcie.function)); + topology_amd topology{}; + if (OclLib::getDeviceInfo(id, CL_DEVICE_TOPOLOGY_AMD, sizeof(topology), &topology) == CL_SUCCESS && topology.type == CL_DEVICE_TOPOLOGY_TYPE_PCIE_AMD) { + m_topology = { topology.bus, topology.device, topology.function }; } + m_board = OclLib::getString(id, CL_DEVICE_BOARD_NAME_AMD); } else if (m_extensions.contains("cl_nv_device_attribute_query")) { cl_uint bus = 0; - if (OclLib::getDeviceInfo(id, CL_DEVICE_PCI_BUS_ID_NV, sizeof(bus), &bus, nullptr) == CL_SUCCESS) { + if (OclLib::getDeviceInfo(id, CL_DEVICE_PCI_BUS_ID_NV, sizeof(bus), &bus) == CL_SUCCESS) { cl_uint slot = OclLib::getUint(id, CL_DEVICE_PCI_SLOT_ID_NV); - m_topology = PciTopology(bus, (slot >> 3) & 0xff, slot & 7); + m_topology = { bus, (slot >> 3) & 0xff, slot & 7 }; } } }