From 9bd34c39f9625bc5f5f2ea10b235df1f7cbee5f0 Mon Sep 17 00:00:00 2001 From: Genoil Date: Thu, 16 Apr 2015 23:21:30 +0200 Subject: [PATCH] allow selection of other OpenCL platform than 0, using --opencl-platform n parameters --- eth/main.cpp | 12 ++++++++++++ libethash-cl/ethash_cl_miner.cpp | 24 ++++++++++++++---------- libethash-cl/ethash_cl_miner.h | 4 ++-- libethcore/Ethash.cpp | 5 +++-- libethcore/Ethash.h | 3 +++ 5 files changed, 34 insertions(+), 14 deletions(-) diff --git a/eth/main.cpp b/eth/main.cpp index cdc74b7e6..fa37effc9 100644 --- a/eth/main.cpp +++ b/eth/main.cpp @@ -134,6 +134,7 @@ void help() << " -f,--force-mining Mine even when there are no transactions to mine (default: off)" << endl << " -C,--cpu When mining, use the CPU." << endl << " -G,--opencl When mining use the GPU via OpenCL." << endl + << " --opencl-platform When mining using -G/--opencl use OpenCL platform n (default: 0)." << endl << " --opencl-device When mining using -G/--opencl use OpenCL device n (default: 0)." << endl << "Client networking:" << endl << " --client-name Add a name to your client's version string (default: blank)." << endl @@ -394,6 +395,7 @@ int main(int argc, char** argv) /// Mining options MinerType minerType = MinerType::CPU; + unsigned openclPlatform = 0; unsigned openclDevice = 0; /// File name for import/export. @@ -511,6 +513,15 @@ int main(int argc, char** argv) cerr << "Bad " << arg << " option: " << argv[i] << endl; return -1; } + else if (arg == "--opencl-platform" && i + 1 < argc) + try { + openclPlatform= stol(argv[++i]); + } + catch (...) + { + cerr << "Bad " << arg << " option: " << argv[i] << endl; + return -1; + } else if (arg == "--opencl-device" && i + 1 < argc) try { openclDevice = stol(argv[++i]); @@ -809,6 +820,7 @@ int main(int argc, char** argv) if (sessionSecret) sigKey = KeyPair(sessionSecret); + ProofOfWork::GPUMiner::setDefaultPlatform(openclPlatform); ProofOfWork::GPUMiner::setDefaultDevice(openclDevice); // Two codepaths is necessary since named block require database, but numbered diff --git a/libethash-cl/ethash_cl_miner.cpp b/libethash-cl/ethash_cl_miner.cpp index 172123439..53eabe349 100644 --- a/libethash-cl/ethash_cl_miner.cpp +++ b/libethash-cl/ethash_cl_miner.cpp @@ -57,7 +57,7 @@ ethash_cl_miner::ethash_cl_miner() { } -std::string ethash_cl_miner::platform_info() +std::string ethash_cl_miner::platform_info(unsigned _platformId, unsigned _deviceId) { std::vector platforms; cl::Platform::get(&platforms); @@ -67,21 +67,22 @@ std::string ethash_cl_miner::platform_info() return std::string(); } - // get GPU device of the default platform + // get GPU device of the selected platform std::vector devices; - platforms[0].getDevices(CL_DEVICE_TYPE_ALL, &devices); + unsigned platform_num = std::min(_platformId, platforms.size() - 1); + platforms[platform_num].getDevices(CL_DEVICE_TYPE_ALL, &devices); if (devices.empty()) { debugf("No OpenCL devices found.\n"); return std::string(); } - // use default device - unsigned device_num = 0; + // use selected default device + unsigned device_num = std::min(_deviceId, devices.size() - 1); cl::Device& device = devices[device_num]; std::string device_version = device.getInfo(); - return "{ \"platform\": \"" + platforms[0].getInfo() + "\", \"device\": \"" + device.getInfo() + "\", \"version\": \"" + device_version + "\" }"; + return "{ \"platform\": \"" + platforms[platform_num].getInfo() + "\", \"device\": \"" + device.getInfo() + "\", \"version\": \"" + device_version + "\" }"; } void ethash_cl_miner::finish() @@ -92,7 +93,7 @@ void ethash_cl_miner::finish() } } -bool ethash_cl_miner::init(ethash_params const& params, std::function _fillDAG, unsigned workgroup_size, unsigned _deviceId) +bool ethash_cl_miner::init(ethash_params const& params, std::function _fillDAG, unsigned workgroup_size, unsigned _platformId, unsigned _deviceId) { // store params m_params = params; @@ -106,12 +107,15 @@ bool ethash_cl_miner::init(ethash_params const& params, std::function().c_str()); + // use selected platform + + _platformId = std::min(_platformId, platforms.size() - 1); + + fprintf(stderr, "Using platform: %s\n", platforms[_platformId].getInfo().c_str()); // get GPU device of the default platform std::vector devices; - platforms[0].getDevices(CL_DEVICE_TYPE_ALL, &devices); + platforms[_platformId].getDevices(CL_DEVICE_TYPE_ALL, &devices); if (devices.empty()) { debugf("No OpenCL devices found.\n"); diff --git a/libethash-cl/ethash_cl_miner.h b/libethash-cl/ethash_cl_miner.h index 90793ae97..3046f037b 100644 --- a/libethash-cl/ethash_cl_miner.h +++ b/libethash-cl/ethash_cl_miner.h @@ -31,8 +31,8 @@ public: public: ethash_cl_miner(); - bool init(ethash_params const& params, std::function _fillDAG, unsigned workgroup_size = 64, unsigned _deviceId = 0); - static std::string platform_info(); + bool init(ethash_params const& params, std::function _fillDAG, unsigned workgroup_size = 64, unsigned _platformId = 0, unsigned _deviceId = 0); + static std::string platform_info(unsigned _platformId = 0, unsigned _deviceId = 0); void finish(); void hash(uint8_t* ret, uint8_t const* header, uint64_t nonce, unsigned count); diff --git a/libethcore/Ethash.cpp b/libethcore/Ethash.cpp index 977149e7a..1bd970317 100644 --- a/libethcore/Ethash.cpp +++ b/libethcore/Ethash.cpp @@ -262,6 +262,7 @@ private: Ethash::GPUMiner* m_owner = nullptr; }; +unsigned Ethash::GPUMiner::s_platformId = 0; unsigned Ethash::GPUMiner::s_deviceId = 0; Ethash::GPUMiner::GPUMiner(ConstructionInfo const& _ci): @@ -305,7 +306,7 @@ void Ethash::GPUMiner::workLoop() auto p = EthashAux::params(m_minerSeed); auto cb = [&](void* d) { EthashAux::full(m_minerSeed, bytesRef((byte*)d, p.full_size)); }; - m_miner->init(p, cb, 32, s_deviceId); + m_miner->init(p, cb, 32, s_platformId, s_deviceId); } uint64_t upper64OfBoundary = (uint64_t)(u64)((u256)w.boundary >> 192); @@ -320,7 +321,7 @@ void Ethash::GPUMiner::pause() std::string Ethash::GPUMiner::platformInfo() { - return ethash_cl_miner::platform_info(); + return ethash_cl_miner::platform_info(s_platformId, s_deviceId); } #endif diff --git a/libethcore/Ethash.h b/libethcore/Ethash.h index 077da4460..2bbe7d649 100644 --- a/libethcore/Ethash.h +++ b/libethcore/Ethash.h @@ -86,6 +86,7 @@ public: static unsigned instances() { return std::thread::hardware_concurrency(); } static std::string platformInfo(); + static void setDefaultPlatform(unsigned) {} static void setDefaultDevice(unsigned) {} protected: @@ -113,6 +114,7 @@ public: static unsigned instances() { return 1; } static std::string platformInfo(); + static void setDefaultPlatform(unsigned _id) { s_platformId = _id; } static void setDefaultDevice(unsigned _id) { s_deviceId = _id; } protected: @@ -129,6 +131,7 @@ public: ethash_cl_miner* m_miner = nullptr; h256 m_minerSeed; ///< Last seed in m_miner + static unsigned s_platformId; static unsigned s_deviceId; }; #else