diff --git a/libethash-cl/ethash_cl_miner.cpp b/libethash-cl/ethash_cl_miner.cpp index dc806be1b..35700767c 100644 --- a/libethash-cl/ethash_cl_miner.cpp +++ b/libethash-cl/ethash_cl_miner.cpp @@ -78,30 +78,28 @@ ethash_cl_miner::~ethash_cl_miner() finish(); } -// Quite ugly. Saves us a lot of typing on these static functions -// since we can't keep the platforms vector in the class (static class/functions) -// -// LTODO: With a bit of general refactoring this could go away and platforms -// be queried only once and kept in the class. -#define ETHASHCL_GET_PLATFORMS(platforms_, failStmt_) \ - do { \ - try \ - { \ - cl::Platform::get(&platforms_); \ - } \ - catch (cl::Error const& err) \ - { \ - int errCode = err.err(); \ - if (errCode == CL_PLATFORM_NOT_FOUND_KHR) \ - ETHCL_LOG("No OpenCL platforms found"); \ - failStmt_; \ - } \ - } while(0) +std::vector ethash_cl_miner::getPlatforms() +{ + vector platforms; + try + { + cl::Platform::get(&platforms); + } + catch(cl::Error const& err) + { + if (err.err() == CL_PLATFORM_NOT_FOUND_KHR) + ETHCL_LOG("No OpenCL platforms found"); + else + throw err; + } + return platforms; +} string ethash_cl_miner::platform_info(unsigned _platformId, unsigned _deviceId) { - vector platforms; - ETHASHCL_GET_PLATFORMS(platforms, return string()); + vector platforms = getPlatforms(); + if (platforms.empty()) + return string(); // get GPU device of the selected platform unsigned platform_num = min(_platformId, platforms.size() - 1); vector devices = getDevices(platforms, _platformId); @@ -123,24 +121,35 @@ std::vector ethash_cl_miner::getDevices(std::vector co { vector devices; unsigned platform_num = min(_platformId, _platforms.size() - 1); - _platforms[platform_num].getDevices( - s_allowCPU ? CL_DEVICE_TYPE_ALL : ETHCL_QUERIED_DEVICE_TYPES, - &devices - ); + try + { + _platforms[platform_num].getDevices( + s_allowCPU ? CL_DEVICE_TYPE_ALL : ETHCL_QUERIED_DEVICE_TYPES, + &devices + ); + } + catch (cl::Error const& err) + { + // if simply no devices found return empty vector + if (err.err() != CL_DEVICE_NOT_FOUND) + throw err; + } return devices; } unsigned ethash_cl_miner::getNumPlatforms() { - vector platforms; - ETHASHCL_GET_PLATFORMS(platforms, return 0); + vector platforms = getPlatforms(); + if (platforms.empty()) + return 0; return platforms.size(); } unsigned ethash_cl_miner::getNumDevices(unsigned _platformId) { - vector platforms; - ETHASHCL_GET_PLATFORMS(platforms, return 0); + vector platforms = getPlatforms(); + if (platforms.empty()) + return 0; vector devices = getDevices(platforms, _platformId); if (devices.empty()) @@ -200,8 +209,9 @@ unsigned ethash_cl_miner::s_initialGlobalWorkSize = ethash_cl_miner::c_defaultGl bool ethash_cl_miner::searchForAllDevices(function _callback) { - vector platforms; - ETHASHCL_GET_PLATFORMS(platforms, return false); + vector platforms = getPlatforms(); + if (platforms.empty()) + return false; for (unsigned i = 0; i < platforms.size(); ++i) if (searchForAllDevices(i, _callback)) return true; @@ -211,8 +221,9 @@ bool ethash_cl_miner::searchForAllDevices(function _cal bool ethash_cl_miner::searchForAllDevices(unsigned _platformId, function _callback) { - vector platforms; - ETHASHCL_GET_PLATFORMS(platforms, return false); + vector platforms = getPlatforms(); + if (platforms.empty()) + return false; if (_platformId >= platforms.size()) return false; @@ -226,16 +237,18 @@ bool ethash_cl_miner::searchForAllDevices(unsigned _platformId, function _callback) { - vector platforms; - ETHASHCL_GET_PLATFORMS(platforms, return); + vector platforms = getPlatforms(); + if (platforms.empty()) + return; for (unsigned i = 0; i < platforms.size(); ++i) doForAllDevices(i, _callback); } void ethash_cl_miner::doForAllDevices(unsigned _platformId, function _callback) { - vector platforms; - ETHASHCL_GET_PLATFORMS(platforms, return); + vector platforms = getPlatforms(); + if (platforms.empty()) + return; if (_platformId >= platforms.size()) return; @@ -273,8 +286,9 @@ bool ethash_cl_miner::init( // get all platforms try { - vector platforms; - ETHASHCL_GET_PLATFORMS(platforms, return false); + vector platforms = getPlatforms(); + if (platforms.empty()) + return false; // use selected platform _platformId = min(_platformId, platforms.size() - 1); diff --git a/libethash-cl/ethash_cl_miner.h b/libethash-cl/ethash_cl_miner.h index fb28dc04b..3ea2fba4a 100644 --- a/libethash-cl/ethash_cl_miner.h +++ b/libethash-cl/ethash_cl_miner.h @@ -75,6 +75,7 @@ public: private: static std::vector getDevices(std::vector const& _platforms, unsigned _platformId); + static std::vector getPlatforms(); cl::Context m_context; cl::CommandQueue m_queue;