Added OclDevice and partially autoconfig.

This commit is contained in:
XMRig 2019-08-22 22:39:36 +07:00
parent 166a68244e
commit ed7216575c
16 changed files with 697 additions and 100 deletions

View file

@ -0,0 +1,252 @@
/* XMRig
* Copyright 2010 Jeff Garzik <jgarzik@pobox.com>
* Copyright 2012-2014 pooler <pooler@litecoinpool.org>
* Copyright 2014 Lucas Jones <https://github.com/lucasjones>
* Copyright 2014-2016 Wolf9466 <https://github.com/OhGodAPet>
* Copyright 2016 Jay D Dee <jayddee246@gmail.com>
* Copyright 2017-2018 XMR-Stak <https://github.com/fireice-uk>, <https://github.com/psychocrypt>
* Copyright 2018-2019 SChernykh <https://github.com/SChernykh>
* Copyright 2016-2019 XMRig <https://github.com/xmrig>, <support@xmrig.com>
*
* 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
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
#include <algorithm>
#include "backend/opencl/OclThreads.h"
#include "backend/opencl/wrappers/OclDevice.h"
#include "backend/opencl/wrappers/OclLib.h"
#include "crypto/cn/CnAlgo.h"
#include "crypto/common/Algorithm.h"
#include "rapidjson/document.h"
namespace xmrig {
constexpr const size_t oneMiB = 1024u * 1024u;
static OclVendor getVendorId(const String &vendor)
{
if (vendor.contains("Advanced Micro Devices") || vendor.contains("AMD")) {
return OCL_VENDOR_AMD;
}
if (vendor.contains("NVIDIA")) {
return OCL_VENDOR_NVIDIA;
}
if (vendor.contains("Intel")) {
return OCL_VENDOR_INTEL;
}
return OCL_VENDOR_UNKNOWN;
}
static OclDevice::Type getType(const String &name)
{
if (name == "gfx900" || name == "gfx901") {
return OclDevice::Vega_10;
}
if (name == "gfx902" || name == "gfx903") {
return OclDevice::Raven;
}
if (name == "gfx906" || name == "gfx907") {
return OclDevice::Vega_20;
}
if (name == "gfx1010") {
return OclDevice::Navi_10;
}
if (name == "gfx804") {
return OclDevice::Lexa;
}
if (name == "Baffin") {
return OclDevice::Baffin;
}
if (name == "gfx803" || name.contains("polaris") || name == "Ellesmere") {
return OclDevice::Polaris;
}
return OclDevice::Unknown;
}
static inline bool isCNv2(const Algorithm &algorithm)
{
return algorithm.family() == Algorithm::CN && CnAlgo<>::base(algorithm) == Algorithm::CN_2;
}
} // namespace xmrig
xmrig::OclDevice::OclDevice(uint32_t index, cl_device_id id, cl_platform_id platform) :
m_id(id),
m_platform(platform),
m_board(OclLib::getDeviceString(id, 0x4038 /* CL_DEVICE_BOARD_NAME_AMD */)),
m_name(OclLib::getDeviceString(id, CL_DEVICE_NAME)),
m_vendor(OclLib::getDeviceString(id, CL_DEVICE_VENDOR)),
m_computeUnits(OclLib::getDeviceUint(id, CL_DEVICE_MAX_COMPUTE_UNITS, 1)),
m_index(index)
{
m_vendorId = getVendorId(m_vendor);
m_type = getType(m_name);
}
size_t xmrig::OclDevice::freeMem() const
{
return std::min<size_t>(OclLib::getDeviceUlong(id(), CL_DEVICE_MAX_MEM_ALLOC_SIZE), globalMem());
}
size_t xmrig::OclDevice::globalMem() const
{
return OclLib::getDeviceUlong(id(), CL_DEVICE_GLOBAL_MEM_SIZE);
}
void xmrig::OclDevice::generate(const Algorithm &algorithm, OclThreads &threads) const
{
uint32_t intensity = getIntensity(algorithm);
if (intensity == 0) {
return;
}
const uint32_t worksize = getWorksize(algorithm);
const uint32_t stridedIndex = getStridedIndex(algorithm);
const uint32_t memChunk = getMemChunk(algorithm);
intensity -= intensity % worksize;
threads.add(OclThread(index(), intensity, worksize, stridedIndex, memChunk));
if ((globalMem() - intensity * 2 * algorithm.l3()) > 128 * oneMiB) {
threads.add(OclThread(index(), intensity, worksize, stridedIndex, memChunk));
}
}
uint32_t xmrig::OclDevice::getIntensity(const Algorithm &algorithm) const
{
if(m_type == Raven) {
return 0;
}
const uint32_t maxIntensity = getPossibleIntensity(algorithm);
if (m_type == Vega_10) {
if (algorithm.family() == Algorithm::CN_HEAVY && m_computeUnits && maxIntensity > 976) {
return 976;
}
return maxIntensity / m_computeUnits * m_computeUnits;
}
uint32_t intensity = (maxIntensity / (8 * m_computeUnits)) * m_computeUnits * 8;
if (intensity == 0) {
return 0;
}
if (m_vendorId == OCL_VENDOR_AMD && (m_type == Lexa || m_type == Baffin || m_computeUnits <= 16)) {
intensity /= 2;
if (algorithm.family() == Algorithm::CN_HEAVY) {
intensity /= 2;
}
}
return intensity;
}
uint32_t xmrig::OclDevice::getMaxThreads(const Algorithm &algorithm) const
{
if (m_vendorId == OCL_VENDOR_NVIDIA && (m_name.contains("P100") || m_name.contains("V100"))) {
return 40000u;
}
const uint32_t ratio = (algorithm.l3() <= oneMiB) ? 2u : 1u;
if (m_type == Vega_10 || m_type == Vega_20) {
if (computeUnits() == 56 && isCNv2(algorithm)) {
return 1792u;
}
return ratio * 2024u;
}
return ratio * 1000u;
}
uint32_t xmrig::OclDevice::getMemChunk(const Algorithm &algorithm) const
{
if ((m_type == Vega_10 || m_type == Vega_20) && (algorithm.family() == Algorithm::CN_PICO || isCNv2(algorithm))) {
return 1;
}
return 2;
}
uint32_t xmrig::OclDevice::getPossibleIntensity(const Algorithm &algorithm) const
{
const uint32_t maxThreads = getMaxThreads(algorithm);
const size_t minFreeMem = (maxThreads == 40000u ? 512u : 128u) * oneMiB;
const size_t availableMem = freeMem() - minFreeMem;
const size_t perThread = algorithm.l3() + 224u;
const uint32_t maxIntensity = static_cast<uint32_t>(availableMem / perThread);
return std::min<uint32_t>(maxThreads, maxIntensity);
}
uint32_t xmrig::OclDevice::getStridedIndex(const Algorithm &algorithm) const
{
if (m_vendorId == OCL_VENDOR_NVIDIA) {
return 0;
}
if (algorithm.family() == Algorithm::CN_PICO || isCNv2(algorithm)) {
return 2;
}
return 1;
}
uint32_t xmrig::OclDevice::getWorksize(const Algorithm &algorithm) const
{
if (m_type == Vega_10 || m_type == Vega_20) {
if (algorithm.family() == Algorithm::CN_PICO) {
return 64;
}
if (isCNv2(algorithm)) {
return 16;
}
}
return 8;
}

View file

@ -0,0 +1,100 @@
/* XMRig
* Copyright 2010 Jeff Garzik <jgarzik@pobox.com>
* Copyright 2012-2014 pooler <pooler@litecoinpool.org>
* Copyright 2014 Lucas Jones <https://github.com/lucasjones>
* Copyright 2014-2016 Wolf9466 <https://github.com/OhGodAPet>
* Copyright 2016 Jay D Dee <jayddee246@gmail.com>
* Copyright 2017-2018 XMR-Stak <https://github.com/fireice-uk>, <https://github.com/psychocrypt>
* Copyright 2018-2019 SChernykh <https://github.com/SChernykh>
* Copyright 2016-2019 XMRig <https://github.com/xmrig>, <support@xmrig.com>
*
* 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
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
#ifndef XMRIG_OCLDEVICE_H
#define XMRIG_OCLDEVICE_H
#include <vector>
#include "backend/opencl/wrappers/OclVendor.h"
#include "base/tools/String.h"
typedef struct _cl_device_id *cl_device_id;
typedef struct _cl_platform_id *cl_platform_id;
namespace xmrig {
class Algorithm;
class OclThreads;
class OclDevice
{
public:
enum Type {
Unknown,
Baffin,
Polaris,
Lexa,
Vega_10,
Vega_20,
Raven,
Navi_10
};
OclDevice() = default;
OclDevice(uint32_t index, cl_device_id id, cl_platform_id platform);
size_t freeMem() const;
size_t globalMem() const;
void generate(const Algorithm &algorithm, OclThreads &threads) const;
inline bool isValid() const { return m_id != nullptr && m_platform != nullptr; }
inline cl_device_id id() const { return m_id; }
inline const String &board() const { return m_board.isNull() ? m_name : m_board; }
inline const String &name() const { return m_name; }
inline const String &vendor() const { return m_vendor; }
inline OclVendor vendorId() const { return m_vendorId; }
inline uint32_t computeUnits() const { return m_computeUnits; }
inline uint32_t index() const { return m_index; }
private:
uint32_t getIntensity(const Algorithm &algorithm) const;
uint32_t getMaxThreads(const Algorithm &algorithm) const;
uint32_t getMemChunk(const Algorithm &algorithm) const;
uint32_t getPossibleIntensity(const Algorithm &algorithm) const;
uint32_t getStridedIndex(const Algorithm &algorithm) const;
uint32_t getWorksize(const Algorithm &algorithm) const;
cl_device_id m_id = nullptr;
cl_platform_id m_platform = nullptr;
const String m_board;
const String m_name;
const String m_vendor;
const uint32_t m_computeUnits = 1;
const uint32_t m_index = 0;
OclVendor m_vendorId = OCL_VENDOR_UNKNOWN;
Type m_type = Unknown;
};
} // namespace xmrig
#endif /* XMRIG_OCLDEVICE_H */

View file

@ -478,12 +478,11 @@ cl_program xmrig::OclLib::createProgramWithSource(cl_context context, cl_uint co
}
cl_uint xmrig::OclLib::getDeviceMaxComputeUnits(cl_device_id id)
cl_uint xmrig::OclLib::getDeviceUint(cl_device_id id, cl_device_info param, cl_uint defaultValue)
{
cl_uint count = 1;
OclLib::getDeviceInfo(id, CL_DEVICE_MAX_COMPUTE_UNITS, sizeof(cl_uint), &count);
OclLib::getDeviceInfo(id, param, sizeof(cl_uint), &defaultValue);
return count;
return defaultValue;
}
@ -504,26 +503,11 @@ cl_uint xmrig::OclLib::getNumPlatforms()
}
xmrig::OclVendor xmrig::OclLib::getDeviceVendor(cl_device_id id)
cl_ulong xmrig::OclLib::getDeviceUlong(cl_device_id id, cl_device_info param, cl_ulong defaultValue)
{
static char buf[256] = { 0 };
if (getDeviceInfo(id, CL_DEVICE_VENDOR, sizeof(buf), buf) != CL_SUCCESS) {
return OCL_VENDOR_UNKNOWN;
}
OclLib::getDeviceInfo(id, param, sizeof(cl_ulong), &defaultValue);
if (strstr(buf, "Advanced Micro Devices") != nullptr || strstr(buf, "AMD") != nullptr) {
return OCL_VENDOR_AMD;
}
if (strstr(buf, "NVIDIA") != nullptr) {
return OCL_VENDOR_NVIDIA;
}
if (strstr(buf, "Intel") != nullptr) {
return OCL_VENDOR_INTEL;
}
return OCL_VENDOR_UNKNOWN;
return defaultValue;
}
@ -540,29 +524,17 @@ std::vector<cl_platform_id> xmrig::OclLib::getPlatformIDs()
}
xmrig::String xmrig::OclLib::getDeviceBoardName(cl_device_id id)
xmrig::String xmrig::OclLib::getDeviceString(cl_device_id id, cl_device_info param)
{
constexpr size_t size = 128;
char *buf = new char[size]();
if (getDeviceInfo(id, 0x4038 /* CL_DEVICE_BOARD_NAME_AMD */, size, buf) == CL_SUCCESS) {
return buf;
size_t size = 0;
if (getDeviceInfo(id, param, 0, nullptr, &size) != CL_SUCCESS) {
return String();
}
getDeviceInfo(id, CL_DEVICE_NAME, size, buf);
return buf;
}
xmrig::String xmrig::OclLib::getDeviceName(cl_device_id id)
{
constexpr size_t size = 128;
char *buf = new char[size]();
getDeviceInfo(id, CL_DEVICE_NAME, size, buf);
getDeviceInfo(id, param, size, buf, nullptr);
return buf;
return String(buf);
}

View file

@ -68,12 +68,11 @@ public:
static cl_mem createBuffer(cl_context context, cl_mem_flags flags, size_t size, void *host_ptr, cl_int *errcode_ret);
static cl_program createProgramWithBinary(cl_context context, cl_uint num_devices, const cl_device_id *device_list, const size_t *lengths, const unsigned char **binaries, cl_int *binary_status, cl_int *errcode_ret);
static cl_program createProgramWithSource(cl_context context, cl_uint count, const char **strings, const size_t *lengths, cl_int *errcode_ret);
static cl_uint getDeviceMaxComputeUnits(cl_device_id id);
static cl_uint getDeviceUint(cl_device_id id, cl_device_info param, cl_uint defaultValue = 0);
static cl_uint getNumPlatforms();
static OclVendor getDeviceVendor(cl_device_id id);
static cl_ulong getDeviceUlong(cl_device_id id, cl_device_info param, cl_ulong defaultValue = 0);
static std::vector<cl_platform_id> getPlatformIDs();
static String getDeviceBoardName(cl_device_id id);
static String getDeviceName(cl_device_id id);
static String getDeviceString(cl_device_id id, cl_device_info param);
static String getPlatformInfo(cl_platform_id platform, cl_platform_info param_name);
static String getProgramBuildLog(cl_program program, cl_device_id device);

View file

@ -84,6 +84,31 @@ rapidjson::Value xmrig::OclPlatform::toJSON(rapidjson::Document &doc) const
}
std::vector<xmrig::OclDevice> xmrig::OclPlatform::devices() const
{
std::vector<OclDevice> out;
if (!isValid()) {
return out;
}
cl_uint num_devices = 0;
OclLib::getDeviceIDs(id(), CL_DEVICE_TYPE_GPU, 0, nullptr, &num_devices);
if (num_devices == 0) {
return out;
}
out.reserve(num_devices);
std::vector<cl_device_id> devices(num_devices);
OclLib::getDeviceIDs(id(), CL_DEVICE_TYPE_GPU, num_devices, devices.data(), nullptr);
for (size_t i = 0; i < devices.size(); ++i) {
out.emplace_back(i, devices[i], id());
}
return out;
}
xmrig::String xmrig::OclPlatform::extensions() const
{
return OclLib::getPlatformInfo(id(), CL_PLATFORM_EXTENSIONS);

View file

@ -29,6 +29,7 @@
#include <vector>
#include "backend/opencl/wrappers/OclDevice.h"
#include "base/tools/String.h"
@ -52,6 +53,7 @@ public:
inline size_t index() const { return m_index; }
rapidjson::Value toJSON(rapidjson::Document &doc) const;
std::vector<OclDevice> devices() const;
String extensions() const;
String name() const;
String profile() const;

View file

@ -0,0 +1,43 @@
/* XMRig
* Copyright 2010 Jeff Garzik <jgarzik@pobox.com>
* Copyright 2012-2014 pooler <pooler@litecoinpool.org>
* Copyright 2014 Lucas Jones <https://github.com/lucasjones>
* Copyright 2014-2016 Wolf9466 <https://github.com/OhGodAPet>
* Copyright 2016 Jay D Dee <jayddee246@gmail.com>
* Copyright 2017-2018 XMR-Stak <https://github.com/fireice-uk>, <https://github.com/psychocrypt>
* Copyright 2018-2019 SChernykh <https://github.com/SChernykh>
* Copyright 2016-2019 XMRig <https://github.com/xmrig>, <support@xmrig.com>
*
* 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
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
#ifndef XMRIG_OCLVENDOR_H
#define XMRIG_OCLVENDOR_H
namespace xmrig {
enum OclVendor : unsigned {
OCL_VENDOR_UNKNOWN,
OCL_VENDOR_AMD,
OCL_VENDOR_NVIDIA,
OCL_VENDOR_INTEL
};
} // namespace xmrig
#endif /* XMRIG_OCLVENDOR_H */