You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

246 lines
6.0 KiB

9 years ago
/*
9 years ago
This file is part of cpp-ethereum.
9 years ago
9 years ago
cpp-ethereum 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.
9 years ago
9 years ago
cpp-ethereum 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.
9 years ago
9 years ago
You should have received a copy of the GNU General Public License
along with cpp-ethereum. If not, see <http://www.gnu.org/licenses/>.
*/
/** @file EthashCUDAMiner.cpp
* @author Gav Wood <i@gavwood.com>
* @date 2014
*
* Determines the PoW algorithm.
9 years ago
*/
#if ETH_ETHASHCUDA || !ETH_TRUE
9 years ago
#if defined(WIN32)
#include <Windows.h>
#endif
9 years ago
#include "EthashCUDAMiner.h"
#include <thread>
#include <chrono>
#include <libethash-cuda/ethash_cuda_miner.h>
9 years ago
using namespace std;
using namespace dev;
using namespace eth;
namespace dev
{
namespace eth
{
class EthashCUDAHook : public ethash_cuda_miner::search_hook
9 years ago
{
public:
EthashCUDAHook(EthashCUDAMiner* _owner) : m_owner(_owner) {}
EthashCUDAHook(EthashCUDAHook const&) = delete;
9 years ago
void abort()
9 years ago
{
9 years ago
{
UniqueGuard l(x_all);
if (m_aborted)
return;
// cdebug << "Attempting to abort";
9 years ago
m_abort = true;
9 years ago
}
// m_abort is true so now searched()/found() will return true to abort the search.
// we hang around on this thread waiting for them to point out that they have aborted since
// otherwise we may end up deleting this object prior to searched()/found() being called.
m_aborted.wait(true);
// for (unsigned timeout = 0; timeout < 100 && !m_aborted; ++timeout)
// std::this_thread::sleep_for(chrono::milliseconds(30));
// if (!m_aborted)
// cwarn << "Couldn't abort. Abandoning OpenCL process.";
}
9 years ago
void reset()
{
UniqueGuard l(x_all);
m_aborted = m_abort = false;
}
protected:
virtual bool found(uint64_t const* _nonces, uint32_t _count) override
{
// dev::operator <<(std::cerr << "Found nonces: ", vector<uint64_t>(_nonces, _nonces + _count)) << std::endl;
for (uint32_t i = 0; i < _count; ++i)
if (m_owner->report(_nonces[i]))
9 years ago
return (m_aborted = true);
return m_owner->shouldStop();
}
9 years ago
virtual bool searched(uint64_t _startNonce, uint32_t _count) override
{
UniqueGuard l(x_all);
// std::cerr << "Searched " << _count << " from " << _startNonce << std::endl;
m_owner->accumulateHashes(_count);
m_last = _startNonce + _count;
if (m_abort || m_owner->shouldStop())
return (m_aborted = true);
return false;
}
9 years ago
private:
Mutex x_all;
uint64_t m_last;
bool m_abort = false;
Notified<bool> m_aborted = { true };
EthashCUDAMiner* m_owner = nullptr;
};
}
9 years ago
}
unsigned EthashCUDAMiner::s_platformId = 0;
unsigned EthashCUDAMiner::s_deviceId = 0;
unsigned EthashCUDAMiner::s_numInstances = 0;
int EthashCUDAMiner::s_devices[16] = { -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1 };
9 years ago
EthashCUDAMiner::EthashCUDAMiner(ConstructionInfo const& _ci) :
GenericMiner<EthashProofOfWork>(_ci),
Worker("cudaminer" + toString(index())),
m_hook( new EthashCUDAHook(this))
9 years ago
{
/*
#if defined(WIN32)
SYSTEM_INFO sysinfo;
GetSystemInfo(&sysinfo);
int num_cpus = sysinfo.dwNumberOfProcessors;
SetThreadAffinityMask(GetCurrentThread(), 1 << (index() % num_cpus));
SetThreadPriority(GetCurrentThread(), THREAD_PRIORITY_HIGHEST);
#endif
*/
9 years ago
}
EthashCUDAMiner::~EthashCUDAMiner()
{
pause();
delete m_miner;
delete m_hook;
}
bool EthashCUDAMiner::report(uint64_t _nonce)
{
Nonce n = (Nonce)(u64)_nonce;
EthashProofOfWork::Result r = EthashAux::eval(work().seedHash, work().headerHash, n);
if (r.value < work().boundary)
9 years ago
return submitProof(Solution{ n, r.mixHash });
9 years ago
return false;
}
void EthashCUDAMiner::kickOff()
{
m_hook->reset();
startWorking();
}
void EthashCUDAMiner::workLoop()
{
// take local copy of work since it may end up being overwritten by kickOff/pause.
try {
WorkPackage w = work();
cnote << "workLoop" << !!m_miner << m_minerSeed << w.seedHash;
if (!m_miner || m_minerSeed != w.seedHash)
{
cnote << "Initialising miner...";
m_minerSeed = w.seedHash;
delete m_miner;
m_miner = new ethash_cuda_miner;
9 years ago
unsigned device = s_devices[index()] > -1 ? s_devices[index()] : index();
9 years ago
EthashAux::FullType dag;
while (true)
{
if ((dag = EthashAux::full(w.seedHash, true)))
break;
if (shouldStop())
{
delete m_miner;
m_miner = nullptr;
return;
}
cnote << "Awaiting DAG";
this_thread::sleep_for(chrono::milliseconds(500));
}
bytesConstRef dagData = dag->data();
m_miner->init(dagData.data(), dagData.size(), device);
9 years ago
}
uint64_t upper64OfBoundary = (uint64_t)(u64)((u256)w.boundary >> 192);
m_miner->search(w.headerHash.data(), upper64OfBoundary, *m_hook);
}
9 years ago
catch (std::runtime_error const& _e)
9 years ago
{
delete m_miner;
m_miner = nullptr;
9 years ago
cwarn << "Error CUDA mining: " << _e.what();
9 years ago
}
}
void EthashCUDAMiner::pause()
{
m_hook->abort();
stopWorking();
}
std::string EthashCUDAMiner::platformInfo()
{
return ethash_cuda_miner::platform_info(s_deviceId);
9 years ago
}
unsigned EthashCUDAMiner::getNumDevices()
{
return ethash_cuda_miner::getNumDevices();
9 years ago
}
void EthashCUDAMiner::listDevices()
{
return ethash_cuda_miner::listDevices();
9 years ago
}
bool EthashCUDAMiner::configureGPU(
unsigned _blockSize,
unsigned _gridSize,
unsigned _numStreams,
9 years ago
unsigned _extraGPUMemory,
unsigned _scheduleFlag,
9 years ago
uint64_t _currentBlock
9 years ago
)
9 years ago
{
_blockSize = ((_blockSize + 7) / 8) * 8;
/*
if (_blockSize != 32 && _blockSize != 64 && _blockSize != 128)
9 years ago
{
cout << "Given localWorkSize of " << toString(_blockSize) << "is invalid. Must be either 32,64 or 128" << endl;
9 years ago
return false;
}
*/
if (!ethash_cuda_miner::configureGPU(
s_devices,
_blockSize,
_gridSize,
_numStreams,
9 years ago
_extraGPUMemory,
_scheduleFlag,
9 years ago
_currentBlock)
)
9 years ago
{
cout << "No CUDA device with sufficient memory was found. Can't CUDA mine. Remove the -U argument" << endl;
9 years ago
return false;
}
return true;
}
#endif