|
|
@ -24,6 +24,7 @@ |
|
|
|
|
|
|
|
#include <cstdio> |
|
|
|
#include <cstdlib> |
|
|
|
#include <fstream> |
|
|
|
#include <iostream> |
|
|
|
#include <assert.h> |
|
|
|
#include <queue> |
|
|
@ -173,9 +174,23 @@ void ethash_cl_miner::finish() |
|
|
|
m_queue.finish(); |
|
|
|
} |
|
|
|
|
|
|
|
bool ethash_cl_miner::init(uint8_t const* _dag, uint64_t _dagSize, unsigned workgroup_size, unsigned _platformId, unsigned _deviceId) |
|
|
|
bool ethash_cl_miner::init( |
|
|
|
uint8_t const* _dag, |
|
|
|
uint64_t _dagSize, |
|
|
|
unsigned workgroup_size, |
|
|
|
unsigned _platformId, |
|
|
|
unsigned _deviceId, |
|
|
|
unsigned _dagChunksNum |
|
|
|
) |
|
|
|
{ |
|
|
|
// for now due to the .cl kernels we can only have either 1 big chunk or 4 chunks
|
|
|
|
assert(_dagChunksNum == 1 || _dagChunksNum == 4); |
|
|
|
// now create the number of chunk buffers
|
|
|
|
m_dagChunksNum = _dagChunksNum; |
|
|
|
|
|
|
|
// get all platforms
|
|
|
|
try |
|
|
|
{ |
|
|
|
std::vector<cl::Platform> platforms; |
|
|
|
cl::Platform::get(&platforms); |
|
|
|
if (platforms.empty()) |
|
|
@ -185,9 +200,7 @@ bool ethash_cl_miner::init(uint8_t const* _dag, uint64_t _dagSize, unsigned work |
|
|
|
} |
|
|
|
|
|
|
|
// use selected platform
|
|
|
|
|
|
|
|
_platformId = std::min<unsigned>(_platformId, platforms.size() - 1); |
|
|
|
|
|
|
|
ETHCL_LOG("Using platform: " << platforms[_platformId].getInfo<CL_PLATFORM_NAME>().c_str()); |
|
|
|
|
|
|
|
// get GPU device of the default platform
|
|
|
@ -220,6 +233,8 @@ bool ethash_cl_miner::init(uint8_t const* _dag, uint64_t _dagSize, unsigned work |
|
|
|
m_workgroup_size = ((workgroup_size + 7) / 8) * 8; |
|
|
|
|
|
|
|
// patch source code
|
|
|
|
// note: ETHASH_CL_MINER_KERNEL is simply ethash_cl_miner_kernel.cl compiled
|
|
|
|
// into a byte array by bin2h.cmake. There is no need to load the file by hand in runtime
|
|
|
|
std::string code(ETHASH_CL_MINER_KERNEL, ETHASH_CL_MINER_KERNEL + ETHASH_CL_MINER_KERNEL_SIZE); |
|
|
|
add_definition(code, "GROUP_SIZE", m_workgroup_size); |
|
|
|
add_definition(code, "DAG_SIZE", (unsigned)(_dagSize / ETHASH_MIX_BYTES)); |
|
|
@ -235,43 +250,89 @@ bool ethash_cl_miner::init(uint8_t const* _dag, uint64_t _dagSize, unsigned work |
|
|
|
try |
|
|
|
{ |
|
|
|
program.build({ device }); |
|
|
|
ETHCL_LOG("Printing program log"); |
|
|
|
ETHCL_LOG(program.getBuildInfo<CL_PROGRAM_BUILD_LOG>(device).c_str()); |
|
|
|
} |
|
|
|
catch (cl::Error err) |
|
|
|
{ |
|
|
|
ETHCL_LOG(program.getBuildInfo<CL_PROGRAM_BUILD_LOG>(device).c_str()); |
|
|
|
return false; |
|
|
|
} |
|
|
|
if (_dagChunksNum == 1) |
|
|
|
{ |
|
|
|
ETHCL_LOG("Loading single big chunk kernels"); |
|
|
|
m_hash_kernel = cl::Kernel(program, "ethash_hash"); |
|
|
|
m_search_kernel = cl::Kernel(program, "ethash_search"); |
|
|
|
} |
|
|
|
else |
|
|
|
{ |
|
|
|
ETHCL_LOG("Loading chunk kernels"); |
|
|
|
m_hash_kernel = cl::Kernel(program, "ethash_hash_chunks"); |
|
|
|
m_search_kernel = cl::Kernel(program, "ethash_search_chunks"); |
|
|
|
} |
|
|
|
|
|
|
|
// create buffer for dag
|
|
|
|
m_dag = cl::Buffer(m_context, CL_MEM_READ_ONLY, _dagSize); |
|
|
|
if (_dagChunksNum == 1) |
|
|
|
{ |
|
|
|
ETHCL_LOG("Creating one big buffer"); |
|
|
|
m_dagChunks.push_back(cl::Buffer(m_context, CL_MEM_READ_ONLY, _dagSize)); |
|
|
|
} |
|
|
|
else |
|
|
|
for (unsigned i = 0; i < _dagChunksNum; i++) |
|
|
|
{ |
|
|
|
// TODO Note: If we ever change to _dagChunksNum other than 4, then the size would need recalculation
|
|
|
|
ETHCL_LOG("Creating buffer for chunk " << i); |
|
|
|
m_dagChunks.push_back(cl::Buffer( |
|
|
|
m_context, |
|
|
|
CL_MEM_READ_ONLY, |
|
|
|
(i == 3) ? (_dagSize - 3 * ((_dagSize >> 9) << 7)) : (_dagSize >> 9) << 7 |
|
|
|
)); |
|
|
|
} |
|
|
|
|
|
|
|
// create buffer for header
|
|
|
|
ETHCL_LOG("Creating buffer for header."); |
|
|
|
m_header = cl::Buffer(m_context, CL_MEM_READ_ONLY, 32); |
|
|
|
|
|
|
|
// compute dag on CPU
|
|
|
|
try { |
|
|
|
m_queue.enqueueWriteBuffer(m_dag, CL_TRUE, 0, _dagSize, _dag); |
|
|
|
if (_dagChunksNum == 1) |
|
|
|
{ |
|
|
|
ETHCL_LOG("Mapping one big chunk."); |
|
|
|
m_queue.enqueueWriteBuffer(m_dagChunks[0], CL_TRUE, 0, _dagSize, _dag); |
|
|
|
} |
|
|
|
else |
|
|
|
{ |
|
|
|
// TODO Note: If we ever change to _dagChunksNum other than 4, then the size would need recalculation
|
|
|
|
void* dag_ptr[4]; |
|
|
|
for (unsigned i = 0; i < _dagChunksNum; i++) |
|
|
|
{ |
|
|
|
ETHCL_LOG("Mapping chunk " << i); |
|
|
|
dag_ptr[i] = m_queue.enqueueMapBuffer(m_dagChunks[i], true, m_opencl_1_1 ? CL_MAP_WRITE : CL_MAP_WRITE_INVALIDATE_REGION, 0, (i == 3) ? (_dagSize - 3 * ((_dagSize >> 9) << 7)) : (_dagSize >> 9) << 7); |
|
|
|
} |
|
|
|
catch (...) |
|
|
|
for (unsigned i = 0; i < _dagChunksNum; i++) |
|
|
|
{ |
|
|
|
// didn't work. shitty driver. try allocating in CPU RAM and manually memcpying it.
|
|
|
|
void* dag_ptr = m_queue.enqueueMapBuffer(m_dag, true, m_opencl_1_1 ? CL_MAP_WRITE : CL_MAP_WRITE_INVALIDATE_REGION, 0, _dagSize); |
|
|
|
memcpy(dag_ptr, _dag, _dagSize); |
|
|
|
m_queue.enqueueUnmapMemObject(m_dag, dag_ptr); |
|
|
|
memcpy(dag_ptr[i], (char *)_dag + i*((_dagSize >> 9) << 7), (i == 3) ? (_dagSize - 3 * ((_dagSize >> 9) << 7)) : (_dagSize >> 9) << 7); |
|
|
|
m_queue.enqueueUnmapMemObject(m_dagChunks[i], dag_ptr[i]); |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
// create mining buffers
|
|
|
|
for (unsigned i = 0; i != c_num_buffers; ++i) |
|
|
|
{ |
|
|
|
ETHCL_LOG("Creating mining buffer " << i); |
|
|
|
m_hash_buf[i] = cl::Buffer(m_context, CL_MEM_WRITE_ONLY | (!m_opencl_1_1 ? CL_MEM_HOST_READ_ONLY : 0), 32 * c_hash_batch_size); |
|
|
|
m_search_buf[i] = cl::Buffer(m_context, CL_MEM_WRITE_ONLY, (c_max_search_results + 1) * sizeof(uint32_t)); |
|
|
|
} |
|
|
|
} |
|
|
|
catch (cl::Error err) |
|
|
|
{ |
|
|
|
ETHCL_LOG(err.what() << "(" << err.err() << ")"); |
|
|
|
return false; |
|
|
|
} |
|
|
|
return true; |
|
|
|
} |
|
|
|
|
|
|
|
void ethash_cl_miner::search(uint8_t const* header, uint64_t target, search_hook& hook) |
|
|
|
{ |
|
|
|
try |
|
|
|
{ |
|
|
|
struct pending_batch |
|
|
|
{ |
|
|
@ -280,7 +341,7 @@ void ethash_cl_miner::search(uint8_t const* header, uint64_t target, search_hook |
|
|
|
}; |
|
|
|
std::queue<pending_batch> pending; |
|
|
|
|
|
|
|
uint32_t const c_zero = 0; |
|
|
|
static uint32_t const c_zero = 0; |
|
|
|
|
|
|
|
// update header constant buffer
|
|
|
|
m_queue.enqueueWriteBuffer(m_header, false, 0, 32, header); |
|
|
@ -295,23 +356,13 @@ void ethash_cl_miner::search(uint8_t const* header, uint64_t target, search_hook |
|
|
|
#endif |
|
|
|
m_queue.finish(); |
|
|
|
|
|
|
|
/*
|
|
|
|
__kernel void ethash_combined_search( |
|
|
|
__global hash32_t* g_hashes, // 0
|
|
|
|
__constant hash32_t const* g_header, // 1
|
|
|
|
__global hash128_t const* g_dag, // 2
|
|
|
|
ulong start_nonce, // 3
|
|
|
|
ulong target, // 4
|
|
|
|
uint isolate // 5
|
|
|
|
) |
|
|
|
*/ |
|
|
|
unsigned argPos = 2; |
|
|
|
m_search_kernel.setArg(1, m_header); |
|
|
|
m_search_kernel.setArg(2, m_dag); |
|
|
|
|
|
|
|
for (unsigned i = 0; i < m_dagChunksNum; ++i, ++argPos) |
|
|
|
m_search_kernel.setArg(argPos, m_dagChunks[i]); |
|
|
|
// pass these to stop the compiler unrolling the loops
|
|
|
|
m_search_kernel.setArg(4, target); |
|
|
|
m_search_kernel.setArg(5, ~0u); |
|
|
|
|
|
|
|
m_search_kernel.setArg(argPos + 1, target); |
|
|
|
m_search_kernel.setArg(argPos + 2, ~0u); |
|
|
|
|
|
|
|
unsigned buf = 0; |
|
|
|
std::random_device engine; |
|
|
@ -320,7 +371,10 @@ void ethash_cl_miner::search(uint8_t const* header, uint64_t target, search_hook |
|
|
|
{ |
|
|
|
// supply output buffer to kernel
|
|
|
|
m_search_kernel.setArg(0, m_search_buf[buf]); |
|
|
|
if (m_dagChunksNum == 1) |
|
|
|
m_search_kernel.setArg(3, start_nonce); |
|
|
|
else |
|
|
|
m_search_kernel.setArg(6, start_nonce); |
|
|
|
|
|
|
|
// execute it!
|
|
|
|
m_queue.enqueueNDRangeKernel(m_search_kernel, cl::NullRange, c_search_batch_size, m_workgroup_size); |
|
|
@ -339,12 +393,9 @@ void ethash_cl_miner::search(uint8_t const* header, uint64_t target, search_hook |
|
|
|
|
|
|
|
uint64_t nonces[c_max_search_results]; |
|
|
|
for (unsigned i = 0; i != num_found; ++i) |
|
|
|
{ |
|
|
|
nonces[i] = batch.start_nonce + results[i + 1]; |
|
|
|
} |
|
|
|
|
|
|
|
m_queue.enqueueUnmapMemObject(m_search_buf[batch.buf], results); |
|
|
|
|
|
|
|
bool exit = num_found && hook.found(nonces, num_found); |
|
|
|
exit |= hook.searched(batch.start_nonce, c_search_batch_size); // always report searched before exit
|
|
|
|
if (exit) |
|
|
@ -364,4 +415,8 @@ void ethash_cl_miner::search(uint8_t const* header, uint64_t target, search_hook |
|
|
|
pre_return_event.wait(); |
|
|
|
#endif |
|
|
|
} |
|
|
|
|
|
|
|
catch (cl::Error err) |
|
|
|
{ |
|
|
|
ETHCL_LOG(err.what() << "(" << err.err() << ")"); |
|
|
|
} |
|
|
|
} |
|
|
|