Browse Source

Merge branch 'khronos_icd' of https://github.com/LefterisJP/cpp-ethereum into win_llvm

cl-refactor
Paweł Bylica 10 years ago
parent
commit
999503ac7d
  1. 8
      ethminer/main.cpp
  2. 92
      libethash-cl/ethash_cl_miner.cpp
  3. 1
      libethash-cl/ethash_cl_miner.h

8
ethminer/main.cpp

@ -20,6 +20,14 @@
* Ethereum client. * Ethereum client.
*/ */
// Solves the problem of including windows.h before including winsock.h
// as detailed here:
// http://stackoverflow.com/questions/1372480/c-redefinition-header-files-winsock2-h
#if defined(_WIN32)
#define _WINSOCKAPI_
#include <windows.h>
#endif
#include <thread> #include <thread>
#include <chrono> #include <chrono>
#include <fstream> #include <fstream>

92
libethash-cl/ethash_cl_miner.cpp

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

1
libethash-cl/ethash_cl_miner.h

@ -75,6 +75,7 @@ public:
private: private:
static std::vector<cl::Device> getDevices(std::vector<cl::Platform> const& _platforms, unsigned _platformId); static std::vector<cl::Device> getDevices(std::vector<cl::Platform> const& _platforms, unsigned _platformId);
static std::vector<cl::Platform> getPlatforms();
cl::Context m_context; cl::Context m_context;
cl::CommandQueue m_queue; cl::CommandQueue m_queue;

Loading…
Cancel
Save