Skip to content

Commit

Permalink
Optimize work dispatch
Browse files Browse the repository at this point in the history
No need to dispatch work to miners which eventually call back farm to get noncescrambler and segmentWidth computing startNonce for n times.

Farm should compute startnonce only once for each miner and dispatch work.
  • Loading branch information
AndreaLanfranchi committed Oct 13, 2018
1 parent e1ce6e8 commit 4f3e483
Show file tree
Hide file tree
Showing 4 changed files with 24 additions and 49 deletions.
10 changes: 7 additions & 3 deletions libethcore/Farm.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -91,11 +91,15 @@ void Farm::shuffle()

void Farm::setWork(WorkPackage const& _wp)
{
// Set work to each miner
// Set work to each miner giving it's own starting nonce
Guard l(x_minerWork);
m_work = _wp;
for (auto const& m : m_miners)
m->setWork(m_work);
uint64_t _startNonce = (m_work.exSizeBits > 0 ? m_work.startNonce : m_nonce_scrambler);
for (unsigned int i = 0; i < m_miners.size(); i++)
{
m_work.startNonce = _startNonce + ((uint64_t)i << m_nonce_segment_with);
m_miners.at(i)->setWork(m_work);
}
}

void Farm::setSealers(std::map<std::string, SealerDescriptor> const& _sealers)
Expand Down
19 changes: 0 additions & 19 deletions libethcore/Miner.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -106,26 +106,7 @@ void Miner::setWork(WorkPackage const& _work)
if (paused())
m_work.header = h256();
else
{

m_work = _work;
if (m_work.exSizeBits >= 0)
{
// This can support up to 2^c_log2MaxMiners devices.
m_work.startNonce =
m_work.startNonce +
((uint64_t)m_index << (64 - LOG2_MAX_MINERS - m_work.exSizeBits));
}
else
{
// Each GPU is given a non-overlapping 2^40 range to search
// return farm.get_nonce_scrambler() + ((uint64_t) m_index << 40);

// Now segment size is adjustable
m_work.startNonce = FarmFace::f().get_nonce_scrambler() +
((uint64_t)m_index << FarmFace::f().get_segment_width());
}
}

#ifdef DEV_BUILD
m_workSwitchStart = std::chrono::steady_clock::now();
Expand Down
3 changes: 1 addition & 2 deletions libethcore/Miner.h
Original file line number Diff line number Diff line change
Expand Up @@ -265,8 +265,7 @@ class FarmFace
* @brief A miner - a member and adoptee of the Farm.
* @warning Not threadsafe. It is assumed Farm will synchronise calls to/from this class.
*/
#define LOG2_MAX_MINERS 5u
#define MAX_MINERS (1u << LOG2_MAX_MINERS)
#define MAX_MINERS 32U

class Miner : public Worker
{
Expand Down
41 changes: 16 additions & 25 deletions libpoolprotocols/PoolManager.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,6 @@ PoolManager::PoolManager(
m_failoverTimeout = failoverTimeout;

p_client->onConnected([&]() {

{
Guard l(m_activeConnectionMutex);
m_lastConnectedHost = m_connections.at(m_activeConnectionIdx).Host();
Expand All @@ -44,7 +43,6 @@ PoolManager::PoolManager(
{
m_failovertimer.cancel();
}

}

if (!Farm::f().isMining())
Expand All @@ -70,11 +68,9 @@ PoolManager::PoolManager(
m_submithrtimer.expires_from_now(boost::posix_time::seconds(m_hrReportingInterval));
m_submithrtimer.async_wait(m_io_strand.wrap(boost::bind(
&PoolManager::submithrtimer_elapsed, this, boost::asio::placeholders::error)));

});

p_client->onDisconnected([&]() {

cnote << "Disconnected from " + m_lastConnectedHost << p_client->ActiveEndPoint();

// Clear current connection
Expand All @@ -100,13 +96,16 @@ PoolManager::PoolManager(
Farm::f().pause();
g_io_service.post(m_io_strand.wrap(boost::bind(&PoolManager::rotateConnect, this)));
}

});

p_client->onWorkReceived([&](WorkPackage const& wp) {

cnote << "Job: " EthWhite "#" << wp.header.abridged() << EthReset " "
<< m_lastConnectedHost << p_client->ActiveEndPoint();

if (wp.epoch != m_lastEpoch)
{
cnote << "New epoch " EthWhite << wp.epoch << EthReset;
m_lastEpoch = wp.epoch;
m_epochChanges.fetch_add(1, std::memory_order_relaxed);
}
if (wp.boundary != m_lastBoundary)
{
using namespace boost::multiprecision;
Expand All @@ -117,23 +116,18 @@ PoolManager::PoolManager(
const uint256_t divisor(string("0x") + m_lastBoundary.hex());
std::stringstream ss;
m_lastDifficulty = double(dividend / divisor);
ss << fixed << setprecision(2) << m_lastDifficulty / 1000000000.0
<< "K megahash";
ss << fixed << setprecision(2) << m_lastDifficulty / 1000000000.0 << "K megahash";
cnote << "Pool difficulty: " EthWhite << ss.str() << EthReset;
}
if (wp.epoch != m_lastEpoch)
{
cnote << "New epoch " EthWhite << wp.epoch << EthReset;
m_lastEpoch = wp.epoch;
m_epochChanges.fetch_add(1, std::memory_order_relaxed);
}

cnote << "Job: " EthWhite "#" << wp.header.abridged() << EthReset " " << m_lastConnectedHost
<< p_client->ActiveEndPoint();

Farm::f().setWork(wp);
});

p_client->onSolutionAccepted([&](bool const& stale,
std::chrono::milliseconds const& elapsedMs, unsigned const& miner_index) {

p_client->onSolutionAccepted([&](bool const& stale, std::chrono::milliseconds const& elapsedMs,
unsigned const& miner_index) {
std::stringstream ss;
ss << std::setw(4) << std::setfill(' ') << elapsedMs.count() << " ms."
<< " " << m_lastConnectedHost + p_client->ActiveEndPoint();
Expand All @@ -142,9 +136,8 @@ PoolManager::PoolManager(
Farm::f().acceptedSolution(stale, miner_index);
});

p_client->onSolutionRejected([&](bool const& stale,
std::chrono::milliseconds const& elapsedMs, unsigned const& miner_index) {

p_client->onSolutionRejected([&](bool const& stale, std::chrono::milliseconds const& elapsedMs,
unsigned const& miner_index) {
std::stringstream ss;
ss << std::setw(4) << std::setfill(' ') << elapsedMs.count() << "ms."
<< " " << m_lastConnectedHost + p_client->ActiveEndPoint();
Expand All @@ -154,7 +147,6 @@ PoolManager::PoolManager(
});

Farm::f().onSolutionFound([&](const Solution& sol) {

// Solution should passthrough only if client is
// properly connected. Otherwise we'll have the bad behavior
// to log nonce submission but receive no response
Expand Down Expand Up @@ -323,7 +315,7 @@ void PoolManager::start()
g_io_service.post(m_io_strand.wrap(boost::bind(&PoolManager::rotateConnect, this)));
}

void PoolManager::rotateConnect()
void PoolManager::rotateConnect()
{
if (p_client->isConnected())
return;
Expand Down Expand Up @@ -432,7 +424,6 @@ void PoolManager::submithrtimer_elapsed(const boost::system::error_code& ec)
m_submithrtimer.expires_from_now(boost::posix_time::seconds(m_hrReportingInterval));
m_submithrtimer.async_wait(m_io_strand.wrap(boost::bind(
&PoolManager::submithrtimer_elapsed, this, boost::asio::placeholders::error)));

}
}
}
Expand Down

0 comments on commit 4f3e483

Please sign in to comment.