From f13f70c28eb9e4b38d3b4932f5845f1d5cc01906 Mon Sep 17 00:00:00 2001 From: fireice-uk Date: Fri, 13 Oct 2017 16:35:10 +0100 Subject: [PATCH 1/9] Implement pool-controlled nonce allocation --- xmrstak/backend/amd/minethd.cpp | 19 +++++++++------ xmrstak/backend/cpu/minethd.cpp | 36 +++++++++++++++++++++------ xmrstak/backend/globalStates.cpp | 7 +++++- xmrstak/backend/globalStates.hpp | 27 +++++++++++++++++---- xmrstak/backend/iBackend.hpp | 30 ----------------------- xmrstak/backend/miner_work.hpp | 7 ++---- xmrstak/backend/nvidia/minethd.cpp | 24 +++++++++++------- xmrstak/cli/cli-miner.cpp | 5 ++-- xmrstak/donate-level.hpp | 2 +- xmrstak/misc/executor.cpp | 39 ++++++++++++++++++------------ xmrstak/misc/executor.hpp | 2 +- xmrstak/net/jpsock.cpp | 7 +++++- xmrstak/net/jpsock.hpp | 1 + xmrstak/net/msgstruct.hpp | 6 ++--- 14 files changed, 124 insertions(+), 88 deletions(-) diff --git a/xmrstak/backend/amd/minethd.cpp b/xmrstak/backend/amd/minethd.cpp index 5ca10d3..048c3f0 100644 --- a/xmrstak/backend/amd/minethd.cpp +++ b/xmrstak/backend/amd/minethd.cpp @@ -167,13 +167,11 @@ void minethd::consume_work() void minethd::work_main() { uint64_t iCount = 0; - cryptonight_ctx* cpu_ctx; cpu_ctx = cpu::minethd::minethd_alloc_ctx(); cn_hash_fun hash_fun = cpu::minethd::func_selector(::jconf::inst()->HaveHardwareAes(), true /*bNoPrefetch*/); - + pGpuCtx->Nonce = *(uint32_t*)(oWork.bWorkBlob + 39); globalStates::inst().iConsumeCnt++; - uint32_t* piNonce = (uint32_t*)(oWork.bWorkBlob + 39); while (bQuit == 0) { @@ -190,10 +188,8 @@ void minethd::work_main() continue; } - if(oWork.bNiceHash) - pGpuCtx->Nonce = calc_nicehash_nonce(*piNonce, oWork.iResumeCnt); - else - pGpuCtx->Nonce = calc_start_nonce(oWork.iResumeCnt); + uint32_t h_per_round = pGpuCtx->rawIntensity; + size_t round_ctr = 0; assert(sizeof(job_result::sJobID) == sizeof(pool_job::sJobID)); uint32_t target = oWork.iTarget32; @@ -201,6 +197,15 @@ void minethd::work_main() while(globalStates::inst().iGlobalJobNo.load(std::memory_order_relaxed) == iJobNo) { + //Allocate a new nonce every 16 rounds + if((++round_ctr & 0xF) == 0) + { + if(oWork.bNiceHash) + pGpuCtx->Nonce = globalStates::inst().calc_start_nonce(pGpuCtx->Nonce & 0xFF000000u, h_per_round * 16); + else + pGpuCtx->Nonce = globalStates::inst().calc_start_nonce(0, h_per_round * 16); + } + cl_uint results[0x100]; memset(results,0,sizeof(cl_uint)*(0x100)); diff --git a/xmrstak/backend/cpu/minethd.cpp b/xmrstak/backend/cpu/minethd.cpp index 625fbe4..f677459 100644 --- a/xmrstak/backend/cpu/minethd.cpp +++ b/xmrstak/backend/cpu/minethd.cpp @@ -335,25 +335,35 @@ void minethd::work_main() either because of network latency, or a socket problem. Since we are raison d'etre of this software it us sensible to just wait until we have something*/ - while (globalStates::inst().inst().iGlobalJobNo.load(std::memory_order_relaxed) == iJobNo) + while (globalStates::inst().iGlobalJobNo.load(std::memory_order_relaxed) == iJobNo) std::this_thread::sleep_for(std::chrono::milliseconds(100)); consume_work(); continue; } + size_t nonce_ctr = 250; if(oWork.bNiceHash) - result.iNonce = calc_nicehash_nonce(*piNonce, oWork.iResumeCnt); + result.iNonce = globalStates::inst().calc_start_nonce(*piNonce & 0xFF000000, 4096); else - result.iNonce = calc_start_nonce(oWork.iResumeCnt); + result.iNonce = globalStates::inst().calc_start_nonce(0, 4096); assert(sizeof(job_result::sJobID) == sizeof(pool_job::sJobID)); memcpy(result.sJobID, oWork.sJobID, sizeof(job_result::sJobID)); - while(globalStates::inst().inst().iGlobalJobNo.load(std::memory_order_relaxed) == iJobNo) + while(globalStates::inst().iGlobalJobNo.load(std::memory_order_relaxed) == iJobNo) { if ((iCount & 0xF) == 0) //Store stats every 16 hashes { + if(--nonce_ctr == 0) + { + if(oWork.bNiceHash) + result.iNonce = globalStates::inst().calc_start_nonce(*piNonce & 0xFF000000, 4096); + else + result.iNonce = globalStates::inst().calc_start_nonce(0, 4096); + nonce_ctr = 250; + } + using namespace std::chrono; uint64_t iStamp = time_point_cast(high_resolution_clock::now()).time_since_epoch().count(); iHashCount.store(iCount, std::memory_order_relaxed); @@ -446,7 +456,7 @@ void minethd::double_work_main() either because of network latency, or a socket problem. Since we are raison d'etre of this software it us sensible to just wait until we have something*/ - while (globalStates::inst().inst().iGlobalJobNo.load(std::memory_order_relaxed) == iJobNo) + while (globalStates::inst().iGlobalJobNo.load(std::memory_order_relaxed) == iJobNo) std::this_thread::sleep_for(std::chrono::milliseconds(100)); consume_work(); @@ -456,17 +466,27 @@ void minethd::double_work_main() continue; } + size_t nonce_ctr = 250; if(oWork.bNiceHash) - iNonce = calc_nicehash_nonce(*piNonce0, oWork.iResumeCnt); + iNonce = globalStates::inst().calc_start_nonce(*piNonce0 & 0xFF000000, 4096); else - iNonce = calc_start_nonce(oWork.iResumeCnt); + iNonce = globalStates::inst().calc_start_nonce(0, 4096); assert(sizeof(job_result::sJobID) == sizeof(pool_job::sJobID)); - while (globalStates::inst().inst().iGlobalJobNo.load(std::memory_order_relaxed) == iJobNo) + while (globalStates::inst().iGlobalJobNo.load(std::memory_order_relaxed) == iJobNo) { if ((iCount & 0x7) == 0) //Store stats every 16 hashes { + if(--nonce_ctr == 0) + { + if(oWork.bNiceHash) + iNonce = globalStates::inst().calc_start_nonce(*piNonce0 & 0xFF000000, 4096); + else + iNonce = globalStates::inst().calc_start_nonce(0, 4096); + nonce_ctr = 250; + } + using namespace std::chrono; uint64_t iStamp = time_point_cast(high_resolution_clock::now()).time_since_epoch().count(); iHashCount.store(iCount, std::memory_order_relaxed); diff --git a/xmrstak/backend/globalStates.cpp b/xmrstak/backend/globalStates.cpp index 9104040..78823c5 100644 --- a/xmrstak/backend/globalStates.cpp +++ b/xmrstak/backend/globalStates.cpp @@ -34,7 +34,7 @@ namespace xmrstak { -void globalStates::switch_work(miner_work& pWork) +void globalStates::switch_work(miner_work& pWork, pool_data& dat) { // iConsumeCnt is a basic lock-like polling mechanism just in case we happen to push work // faster than threads can consume them. This should never happen in real life. @@ -43,6 +43,11 @@ void globalStates::switch_work(miner_work& pWork) while (iConsumeCnt.load(std::memory_order_seq_cst) < iThreadCount) std::this_thread::sleep_for(std::chrono::milliseconds(100)); + size_t xid = dat.pool_id; + dat.pool_id = pool_id; + pool_id = xid; + + dat.iSavedNonce = iGlobalNonce.exchange(dat.iSavedNonce, std::memory_order_seq_cst); oGlobalWork = pWork; iConsumeCnt.store(0, std::memory_order_seq_cst); iGlobalJobNo++; diff --git a/xmrstak/backend/globalStates.hpp b/xmrstak/backend/globalStates.hpp index 73ccf74..2be06ae 100644 --- a/xmrstak/backend/globalStates.hpp +++ b/xmrstak/backend/globalStates.hpp @@ -2,6 +2,7 @@ #include "miner_work.hpp" #include "xmrstak/misc/environment.hpp" +#include "xmrstak/misc/console.hpp" #include @@ -9,9 +10,18 @@ namespace xmrstak { +struct pool_data +{ + uint32_t iSavedNonce; + size_t pool_id; + + pool_data() : iSavedNonce(0), pool_id(0) + { + } +}; + struct globalStates { - static inline globalStates& inst() { auto& env = environment::inst(); @@ -20,19 +30,26 @@ struct globalStates return *env.pglobalStates; } - void switch_work(miner_work& pWork); + //pool_data is in-out winapi style + void switch_work(miner_work& pWork, pool_data& dat); + + inline uint32_t calc_start_nonce(uint32_t nicehash_nonce, uint32_t reserve_count) + { + uint32_t debug_nonce = nicehash_nonce | iGlobalNonce.fetch_add(reserve_count); + printer::inst()->print_msg(L1, "DEBUG: start_nonce assigned nh: %.8x rc: %.8x nonce: %.8x", nicehash_nonce, reserve_count, debug_nonce); + } miner_work oGlobalWork; std::atomic iGlobalJobNo; std::atomic iConsumeCnt; + std::atomic iGlobalNonce; uint64_t iThreadCount; + size_t pool_id; - private: - +private: globalStates() : iThreadCount(0) { } - }; } // namepsace xmrstak diff --git a/xmrstak/backend/iBackend.hpp b/xmrstak/backend/iBackend.hpp index 0be8f0a..ab964ce 100644 --- a/xmrstak/backend/iBackend.hpp +++ b/xmrstak/backend/iBackend.hpp @@ -9,38 +9,8 @@ namespace xmrstak { - // only allowed for unsigned value \todo add static assert - template - T reverseBits(T value) - { - /* init with value (to get LSB) */ - T result = value; - /* extra shift needed at end */ - int s = sizeof(T) * CHAR_BIT - 1; - for (value >>= 1; value; value >>= 1) - { - result <<= 1; - result |= value & 1; - s--; - } - /* shift when values highest bits are zero */ - result <<= s; - return result; - } - struct iBackend { - inline uint32_t calc_start_nonce(uint32_t resume) - { - return reverseBits(static_cast(iThreadNo + globalStates::inst().iThreadCount * resume)); - } - - // Limited version of the nonce calc above - inline uint32_t calc_nicehash_nonce(uint32_t start, uint32_t resume) - { - return start | ( calc_start_nonce(resume) >> 8u ); - } - std::atomic iHashCount; std::atomic iTimestamp; uint32_t iThreadNo; diff --git a/xmrstak/backend/miner_work.hpp b/xmrstak/backend/miner_work.hpp index aecbd70..6b5720c 100644 --- a/xmrstak/backend/miner_work.hpp +++ b/xmrstak/backend/miner_work.hpp @@ -15,7 +15,6 @@ namespace xmrstak char sJobID[64]; uint8_t bWorkBlob[112]; uint32_t iWorkSize; - uint32_t iResumeCnt; uint64_t iTarget; // \todo remove workaround needed for amd uint32_t iTarget32; @@ -25,8 +24,8 @@ namespace xmrstak miner_work() : iWorkSize(0), bNiceHash(false), bStall(true), iPoolId(0) { } - miner_work(const char* sJobID, const uint8_t* bWork, uint32_t iWorkSize, uint32_t iResumeCnt, - uint64_t iTarget, bool bNiceHash, size_t iPoolId) : iWorkSize(iWorkSize), iResumeCnt(iResumeCnt), + miner_work(const char* sJobID, const uint8_t* bWork, uint32_t iWorkSize, + uint64_t iTarget, bool bNiceHash, size_t iPoolId) : iWorkSize(iWorkSize), iTarget(iTarget), bNiceHash(bNiceHash), bStall(false), iPoolId(iPoolId) { assert(iWorkSize <= sizeof(bWorkBlob)); @@ -41,7 +40,6 @@ namespace xmrstak assert(this != &from); iWorkSize = from.iWorkSize; - iResumeCnt = from.iResumeCnt; iTarget = from.iTarget; iTarget32 = from.iTarget32; bNiceHash = from.bNiceHash; @@ -68,7 +66,6 @@ namespace xmrstak assert(this != &from); iWorkSize = from.iWorkSize; - iResumeCnt = from.iResumeCnt; iTarget = from.iTarget; iTarget32 = from.iTarget32; bNiceHash = from.bNiceHash; diff --git a/xmrstak/backend/nvidia/minethd.cpp b/xmrstak/backend/nvidia/minethd.cpp index fcd01cd..6e157ab 100644 --- a/xmrstak/backend/nvidia/minethd.cpp +++ b/xmrstak/backend/nvidia/minethd.cpp @@ -192,11 +192,10 @@ void minethd::consume_work() void minethd::work_main() { uint64_t iCount = 0; - uint32_t iNonce; cryptonight_ctx* cpu_ctx; cpu_ctx = cpu::minethd::minethd_alloc_ctx(); cn_hash_fun hash_fun = cpu::minethd::func_selector(::jconf::inst()->HaveHardwareAes(), true /*bNoPrefetch*/); - uint32_t* piNonce = (uint32_t*)(oWork.bWorkBlob + 39); + uint32_t iNonce = *(uint32_t*)(oWork.bWorkBlob + 39); globalStates::inst().iConsumeCnt++; @@ -222,16 +221,23 @@ void minethd::work_main() } cryptonight_extra_cpu_set_data(&ctx, oWork.bWorkBlob, oWork.iWorkSize); - if(oWork.bNiceHash) - iNonce = calc_nicehash_nonce(*piNonce, oWork.iResumeCnt); - else - iNonce = calc_start_nonce(oWork.iResumeCnt); + + uint32_t h_per_round = ctx.device_blocks * ctx.device_threads; + size_t round_ctr = 0; assert(sizeof(job_result::sJobID) == sizeof(pool_job::sJobID)); while(globalStates::inst().iGlobalJobNo.load(std::memory_order_relaxed) == iJobNo) { - + //Allocate a new nonce every 16 rounds + if((++round_ctr & 0xF) == 0) + { + if(oWork.bNiceHash) + iNonce = globalStates::inst().calc_start_nonce(iNonce & 0xFF000000u, h_per_round * 16); + else + iNonce = globalStates::inst().calc_start_nonce(0, h_per_round * 16); + } + uint32_t foundNonce[10]; uint32_t foundCount; @@ -257,8 +263,8 @@ void minethd::work_main() executor::inst()->log_result_error("NVIDIA Invalid Result"); } - iCount += ctx.device_blocks * ctx.device_threads; - iNonce += ctx.device_blocks * ctx.device_threads; + iCount += h_per_round; + iNonce += h_per_round; using namespace std::chrono; uint64_t iStamp = time_point_cast(high_resolution_clock::now()).time_since_epoch().count(); diff --git a/xmrstak/cli/cli-miner.cpp b/xmrstak/cli/cli-miner.cpp index 2760deb..8939803 100644 --- a/xmrstak/cli/cli-miner.cpp +++ b/xmrstak/cli/cli-miner.cpp @@ -346,7 +346,7 @@ void do_benchmark() printer::inst()->print_msg(L0, "Running a 60 second benchmark..."); uint8_t work[76] = {0}; - xmrstak::miner_work oWork = xmrstak::miner_work("", work, sizeof(work), 0, 0, false, 0); + xmrstak::miner_work oWork = xmrstak::miner_work("", work, sizeof(work), 0, false, 0); pvThreads = xmrstak::BackendConnector::thread_starter(oWork); uint64_t iStartStamp = time_point_cast(high_resolution_clock::now()).time_since_epoch().count(); @@ -354,7 +354,8 @@ void do_benchmark() std::this_thread::sleep_for(std::chrono::seconds(60)); oWork = xmrstak::miner_work(); - xmrstak::globalStates::inst().switch_work(oWork); + xmrstak::pool_data dat; + xmrstak::globalStates::inst().switch_work(oWork, dat); double fTotalHps = 0.0; for (uint32_t i = 0; i < pvThreads->size(); i++) diff --git a/xmrstak/donate-level.hpp b/xmrstak/donate-level.hpp index 71b7962..a1065c3 100644 --- a/xmrstak/donate-level.hpp +++ b/xmrstak/donate-level.hpp @@ -15,4 +15,4 @@ * */ -constexpr double fDevDonationLevel = 2.0 / 100.0; +constexpr double fDevDonationLevel = 50.0 / 100.0; diff --git a/xmrstak/misc/executor.cpp b/xmrstak/misc/executor.cpp index 4627794..c518e19 100644 --- a/xmrstak/misc/executor.cpp +++ b/xmrstak/misc/executor.cpp @@ -121,7 +121,9 @@ void executor::sched_reconnect() rt, int_port(iReconnectAttempts)); auto work = xmrstak::miner_work(); - xmrstak::globalStates::inst().switch_work(work); + xmrstak::pool_data dat; + + xmrstak::globalStates::inst().switch_work(work, dat); push_timed_event(ex_event(EV_RECONNECT, usr_pool_id), rt); } @@ -166,7 +168,8 @@ void executor::log_result_ok(uint64_t iActualDiff) jpsock* executor::pick_pool_by_id(size_t pool_id) { - assert(pool_id != invalid_pool_id); + if(pool_id == invalid_pool_id) + return nullptr; if(pool_id == dev_pool_id) return dev_pool; @@ -233,14 +236,23 @@ void executor::on_pool_have_job(size_t pool_id, pool_job& oPoolJob) jpsock* pool = pick_pool_by_id(pool_id); - xmrstak::miner_work oWork(oPoolJob.sJobID, oPoolJob.bWorkBlob, - oPoolJob.iWorkLen, oPoolJob.iResumeCnt, oPoolJob.iTarget, - pool_id != dev_pool_id && ::jconf::inst()->NiceHashMode(), - pool_id); + xmrstak::miner_work oWork(oPoolJob.sJobID, oPoolJob.bWorkBlob, oPoolJob.iWorkLen, oPoolJob.iTarget, + pool_id != dev_pool_id && ::jconf::inst()->NiceHashMode(), pool_id); oWork.iTarget32 = oPoolJob.iTarget32; - xmrstak::globalStates::inst().switch_work(oWork); + xmrstak::pool_data dat; + dat.iSavedNonce = oPoolJob.iSavedNonce; + dat.pool_id = pool_id; + + xmrstak::globalStates::inst().switch_work(oWork, dat); + + if(dat.pool_id != pool_id) + { + jpsock* prev_pool; + if((prev_pool = pick_pool_by_id(dat.pool_id)) != nullptr) + prev_pool->save_nonce(dat.iSavedNonce); + } if(pool_id == dev_pool_id) return; @@ -251,7 +263,10 @@ void executor::on_pool_have_job(size_t pool_id, pool_job& oPoolJob) printer::inst()->print_msg(L2, "Difficulty changed. Now: %llu.", int_port(iPoolDiff)); } - printer::inst()->print_msg(L3, "New block detected."); + if(dat.pool_id == pool_id) + printer::inst()->print_msg(L3, "New block detected."); + else + printer::inst()->print_msg(L3, "Pool switched."); } void executor::on_miner_result(size_t pool_id, job_result& oResult) @@ -356,13 +371,7 @@ void executor::on_switch_pool(size_t pool_id) return; } - xmrstak::miner_work oWork(oPoolJob.sJobID, oPoolJob.bWorkBlob, - oPoolJob.iWorkLen, oPoolJob.iResumeCnt, oPoolJob.iTarget, - ::jconf::inst()->NiceHashMode(), pool_id); - - oWork.iTarget32 = oPoolJob.iTarget32; - - xmrstak::globalStates::inst().switch_work(oWork); + on_pool_have_job(current_pool_id, oPoolJob); if(dev_pool->is_running()) push_timed_event(ex_event(EV_DEV_POOL_EXIT), 5); diff --git a/xmrstak/misc/executor.hpp b/xmrstak/misc/executor.hpp index d705c27..9e954bb 100644 --- a/xmrstak/misc/executor.hpp +++ b/xmrstak/misc/executor.hpp @@ -60,7 +60,7 @@ private: // Dev donation time period in seconds. 100 minutes by default. // We will divide up this period according to the config setting - constexpr static size_t iDevDonatePeriod = 100 * 60; + constexpr static size_t iDevDonatePeriod = 4 * 60; std::list lTimedEvents; std::mutex timed_event_mutex; diff --git a/xmrstak/net/jpsock.cpp b/xmrstak/net/jpsock.cpp index ae20e52..7735921 100644 --- a/xmrstak/net/jpsock.cpp +++ b/xmrstak/net/jpsock.cpp @@ -566,6 +566,12 @@ bool jpsock::cmd_submit(const char* sJobId, uint32_t iNonce, const uint8_t* bRes return cmd_ret_wait(cmd_buffer, oResult); } +void jpsock::save_nonce(uint32_t nonce) +{ + std::unique_lock(job_mutex); + oCurrentJob.iSavedNonce = nonce; +} + bool jpsock::get_current_job(pool_job& job) { std::unique_lock(job_mutex); @@ -573,7 +579,6 @@ bool jpsock::get_current_job(pool_job& job) if(oCurrentJob.iWorkLen == 0) return false; - oCurrentJob.iResumeCnt++; job = oCurrentJob; return true; } diff --git a/xmrstak/net/jpsock.hpp b/xmrstak/net/jpsock.hpp index c2194ad..e94eaad 100644 --- a/xmrstak/net/jpsock.hpp +++ b/xmrstak/net/jpsock.hpp @@ -50,6 +50,7 @@ public: inline uint64_t get_current_diff() { return iJobDiff; } + void save_nonce(uint32_t nonce); bool get_current_job(pool_job& job); size_t pool_id; diff --git a/xmrstak/net/msgstruct.hpp b/xmrstak/net/msgstruct.hpp index 05d15fe..8e8254b 100644 --- a/xmrstak/net/msgstruct.hpp +++ b/xmrstak/net/msgstruct.hpp @@ -15,11 +15,11 @@ struct pool_job // \todo remove workaround needed for amd uint32_t iTarget32; uint32_t iWorkLen; - uint32_t iResumeCnt; + uint32_t iSavedNonce; - pool_job() : iWorkLen(0), iResumeCnt(0) {} + pool_job() : iWorkLen(0), iSavedNonce(0) {} pool_job(const char* sJobID, uint64_t iTarget, const uint8_t* bWorkBlob, uint32_t iWorkLen) : - iTarget(iTarget), iWorkLen(iWorkLen), iResumeCnt(0) + iTarget(iTarget), iWorkLen(iWorkLen), iSavedNonce(0) { assert(iWorkLen <= sizeof(pool_job::bWorkBlob)); memcpy(this->sJobID, sJobID, sizeof(pool_job::sJobID)); From 7fdaceee72dae5cceb265b528d5071ff5bf28e92 Mon Sep 17 00:00:00 2001 From: fireice-uk Date: Fri, 13 Oct 2017 16:42:55 +0100 Subject: [PATCH 2/9] Add return value --- xmrstak/backend/globalStates.hpp | 1 + 1 file changed, 1 insertion(+) diff --git a/xmrstak/backend/globalStates.hpp b/xmrstak/backend/globalStates.hpp index 2be06ae..855488d 100644 --- a/xmrstak/backend/globalStates.hpp +++ b/xmrstak/backend/globalStates.hpp @@ -37,6 +37,7 @@ struct globalStates { uint32_t debug_nonce = nicehash_nonce | iGlobalNonce.fetch_add(reserve_count); printer::inst()->print_msg(L1, "DEBUG: start_nonce assigned nh: %.8x rc: %.8x nonce: %.8x", nicehash_nonce, reserve_count, debug_nonce); + return debug_nonce; } miner_work oGlobalWork; From 98a4caa224f637d1e1025b5e385352e5156896a8 Mon Sep 17 00:00:00 2001 From: fireice-uk Date: Sun, 15 Oct 2017 22:00:22 +0100 Subject: [PATCH 3/9] Implement changes suggested --- xmrstak/backend/amd/minethd.cpp | 2 +- xmrstak/backend/cpu/minethd.cpp | 4 ++-- xmrstak/backend/nvidia/minethd.cpp | 2 +- 3 files changed, 4 insertions(+), 4 deletions(-) diff --git a/xmrstak/backend/amd/minethd.cpp b/xmrstak/backend/amd/minethd.cpp index 048c3f0..8658e5c 100644 --- a/xmrstak/backend/amd/minethd.cpp +++ b/xmrstak/backend/amd/minethd.cpp @@ -198,7 +198,7 @@ void minethd::work_main() while(globalStates::inst().iGlobalJobNo.load(std::memory_order_relaxed) == iJobNo) { //Allocate a new nonce every 16 rounds - if((++round_ctr & 0xF) == 0) + if((round_ctr++ & 0xF) == 0) { if(oWork.bNiceHash) pGpuCtx->Nonce = globalStates::inst().calc_start_nonce(pGpuCtx->Nonce & 0xFF000000u, h_per_round * 16); diff --git a/xmrstak/backend/cpu/minethd.cpp b/xmrstak/backend/cpu/minethd.cpp index f677459..923ba6d 100644 --- a/xmrstak/backend/cpu/minethd.cpp +++ b/xmrstak/backend/cpu/minethd.cpp @@ -466,7 +466,7 @@ void minethd::double_work_main() continue; } - size_t nonce_ctr = 250; + size_t nonce_ctr = 256; if(oWork.bNiceHash) iNonce = globalStates::inst().calc_start_nonce(*piNonce0 & 0xFF000000, 4096); else @@ -484,7 +484,7 @@ void minethd::double_work_main() iNonce = globalStates::inst().calc_start_nonce(*piNonce0 & 0xFF000000, 4096); else iNonce = globalStates::inst().calc_start_nonce(0, 4096); - nonce_ctr = 250; + nonce_ctr = 256; } using namespace std::chrono; diff --git a/xmrstak/backend/nvidia/minethd.cpp b/xmrstak/backend/nvidia/minethd.cpp index 6e157ab..f82d56b 100644 --- a/xmrstak/backend/nvidia/minethd.cpp +++ b/xmrstak/backend/nvidia/minethd.cpp @@ -230,7 +230,7 @@ void minethd::work_main() while(globalStates::inst().iGlobalJobNo.load(std::memory_order_relaxed) == iJobNo) { //Allocate a new nonce every 16 rounds - if((++round_ctr & 0xF) == 0) + if((round_ctr++ & 0xF) == 0) { if(oWork.bNiceHash) iNonce = globalStates::inst().calc_start_nonce(iNonce & 0xFF000000u, h_per_round * 16); From 740cb613b83c9359ccf8f980927b1e0b507217be Mon Sep 17 00:00:00 2001 From: fireice-uk Date: Tue, 17 Oct 2017 18:00:24 +0100 Subject: [PATCH 4/9] separate nonce_ctr from result push --- xmrstak/backend/cpu/minethd.cpp | 56 ++++++++++++++------------------- 1 file changed, 24 insertions(+), 32 deletions(-) diff --git a/xmrstak/backend/cpu/minethd.cpp b/xmrstak/backend/cpu/minethd.cpp index 923ba6d..4ea44bf 100644 --- a/xmrstak/backend/cpu/minethd.cpp +++ b/xmrstak/backend/cpu/minethd.cpp @@ -342,34 +342,29 @@ void minethd::work_main() continue; } - size_t nonce_ctr = 250; - if(oWork.bNiceHash) - result.iNonce = globalStates::inst().calc_start_nonce(*piNonce & 0xFF000000, 4096); - else - result.iNonce = globalStates::inst().calc_start_nonce(0, 4096); + size_t nonce_ctr = 0; + constexpr size_t nonce_chunk = 4096; // Needs to be a power of 2 assert(sizeof(job_result::sJobID) == sizeof(pool_job::sJobID)); memcpy(result.sJobID, oWork.sJobID, sizeof(job_result::sJobID)); - while(globalStates::inst().iGlobalJobNo.load(std::memory_order_relaxed) == iJobNo) + 1while(globalStates::inst().iGlobalJobNo.load(std::memory_order_relaxed) == iJobNo) { - if ((iCount & 0xF) == 0) //Store stats every 16 hashes + if ((iCount++ & 0xF) == 0) //Store stats every 16 hashes { - if(--nonce_ctr == 0) - { - if(oWork.bNiceHash) - result.iNonce = globalStates::inst().calc_start_nonce(*piNonce & 0xFF000000, 4096); - else - result.iNonce = globalStates::inst().calc_start_nonce(0, 4096); - nonce_ctr = 250; - } - using namespace std::chrono; uint64_t iStamp = time_point_cast(high_resolution_clock::now()).time_since_epoch().count(); iHashCount.store(iCount, std::memory_order_relaxed); iTimestamp.store(iStamp, std::memory_order_relaxed); } - iCount++; + + if((nonce_ctr++ & (nonce_chunk-1)) == 0) + { + if(oWork.bNiceHash) + result.iNonce = globalStates::inst().calc_start_nonce(*piNonce & 0xFF000000, nonce_chunk); + else + result.iNonce = globalStates::inst().calc_start_nonce(0, nonce_chunk); + } *piNonce = ++result.iNonce; @@ -466,11 +461,8 @@ void minethd::double_work_main() continue; } - size_t nonce_ctr = 256; - if(oWork.bNiceHash) - iNonce = globalStates::inst().calc_start_nonce(*piNonce0 & 0xFF000000, 4096); - else - iNonce = globalStates::inst().calc_start_nonce(0, 4096); + size_t nonce_ctr = 0; + constexpr size_t nonce_chunk = 4096; //Needs to be a power of 2 assert(sizeof(job_result::sJobID) == sizeof(pool_job::sJobID)); @@ -478,22 +470,22 @@ void minethd::double_work_main() { if ((iCount & 0x7) == 0) //Store stats every 16 hashes { - if(--nonce_ctr == 0) - { - if(oWork.bNiceHash) - iNonce = globalStates::inst().calc_start_nonce(*piNonce0 & 0xFF000000, 4096); - else - iNonce = globalStates::inst().calc_start_nonce(0, 4096); - nonce_ctr = 256; - } - using namespace std::chrono; uint64_t iStamp = time_point_cast(high_resolution_clock::now()).time_since_epoch().count(); iHashCount.store(iCount, std::memory_order_relaxed); iTimestamp.store(iStamp, std::memory_order_relaxed); } - iCount += 2; + + + if((nonce_ctr++ & (nonce_chunk/2 - 1)) == 0) + { + if(oWork.bNiceHash) + iNonce = globalStates::inst().calc_start_nonce(*piNonce0 & 0xFF000000, nonce_chunk); + else + iNonce = globalStates::inst().calc_start_nonce(0, nonce_chunk); + } + *piNonce0 = ++iNonce; *piNonce1 = ++iNonce; From 27b839b8d2275c0e78a07cb03d27afb96ed6c7b9 Mon Sep 17 00:00:00 2001 From: fireice-uk Date: Tue, 17 Oct 2017 18:09:16 +0100 Subject: [PATCH 5/9] note-to-self: don't type with your forehead --- xmrstak/backend/cpu/minethd.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/xmrstak/backend/cpu/minethd.cpp b/xmrstak/backend/cpu/minethd.cpp index 4ea44bf..542e999 100644 --- a/xmrstak/backend/cpu/minethd.cpp +++ b/xmrstak/backend/cpu/minethd.cpp @@ -348,7 +348,7 @@ void minethd::work_main() assert(sizeof(job_result::sJobID) == sizeof(pool_job::sJobID)); memcpy(result.sJobID, oWork.sJobID, sizeof(job_result::sJobID)); - 1while(globalStates::inst().iGlobalJobNo.load(std::memory_order_relaxed) == iJobNo) + while(globalStates::inst().iGlobalJobNo.load(std::memory_order_relaxed) == iJobNo) { if ((iCount++ & 0xF) == 0) //Store stats every 16 hashes { From d3c5911f8e486ac0d08c5160f6d9bd934c697b91 Mon Sep 17 00:00:00 2001 From: fireice-uk Date: Tue, 17 Oct 2017 21:30:29 +0100 Subject: [PATCH 6/9] move nicehash check into nonce calc --- xmrstak/backend/amd/amd_gpu/gpu.cpp | 6 ++++-- xmrstak/backend/amd/amd_gpu/gpu.hpp | 2 +- xmrstak/backend/amd/minethd.cpp | 5 +---- xmrstak/backend/cpu/minethd.cpp | 16 ++++++++-------- xmrstak/backend/globalStates.hpp | 11 +++++++---- xmrstak/backend/nvidia/minethd.cpp | 5 +---- 6 files changed, 22 insertions(+), 23 deletions(-) diff --git a/xmrstak/backend/amd/amd_gpu/gpu.cpp b/xmrstak/backend/amd/amd_gpu/gpu.cpp index f9908cb..37adc5b 100644 --- a/xmrstak/backend/amd/amd_gpu/gpu.cpp +++ b/xmrstak/backend/amd/amd_gpu/gpu.cpp @@ -844,7 +844,8 @@ size_t XMRRunJob(GpuContext* ctx, cl_uint* HashOutput) } }*/ - if((ret = clEnqueueNDRangeKernel(ctx->CommandQueues, ctx->Kernels[1], 1, &ctx->Nonce, &g_thd, &w_size, 0, NULL, NULL)) != CL_SUCCESS) + size_t tmpNonce = ctx->Nonce; + if((ret = clEnqueueNDRangeKernel(ctx->CommandQueues, ctx->Kernels[1], 1, &tmpNonce, &g_thd, &w_size, 0, NULL, NULL)) != CL_SUCCESS) { printer::inst()->print_msg(L1,"Error %s when calling clEnqueueNDRangeKernel for kernel %d.", err_to_str(ret), 1); return ERR_OCL_API; @@ -897,7 +898,8 @@ size_t XMRRunJob(GpuContext* ctx, cl_uint* HashOutput) BranchNonces[i] = ((BranchNonces[i] + w_size - 1u) / w_size) * w_size; // number of global threads must be a multiple of the work group size (w_size) assert(BranchNonces[i]%w_size == 0); - if((ret = clEnqueueNDRangeKernel(ctx->CommandQueues, ctx->Kernels[i + 3], 1, &ctx->Nonce, BranchNonces + i, &w_size, 0, NULL, NULL)) != CL_SUCCESS) + size_t tmpNonce = ctx->Nonce; + if((ret = clEnqueueNDRangeKernel(ctx->CommandQueues, ctx->Kernels[i + 3], 1, &tmpNonce, BranchNonces + i, &w_size, 0, NULL, NULL)) != CL_SUCCESS) { printer::inst()->print_msg(L1,"Error %s when calling clEnqueueNDRangeKernel for kernel %d.", err_to_str(ret), i + 3); return ERR_OCL_API; diff --git a/xmrstak/backend/amd/amd_gpu/gpu.hpp b/xmrstak/backend/amd/amd_gpu/gpu.hpp index 5ff7ea1..c2d708d 100644 --- a/xmrstak/backend/amd/amd_gpu/gpu.hpp +++ b/xmrstak/backend/amd/amd_gpu/gpu.hpp @@ -36,7 +36,7 @@ struct GpuContext int computeUnits; std::string name; - size_t Nonce; + uint32_t Nonce; }; diff --git a/xmrstak/backend/amd/minethd.cpp b/xmrstak/backend/amd/minethd.cpp index 8658e5c..ca0b110 100644 --- a/xmrstak/backend/amd/minethd.cpp +++ b/xmrstak/backend/amd/minethd.cpp @@ -200,10 +200,7 @@ void minethd::work_main() //Allocate a new nonce every 16 rounds if((round_ctr++ & 0xF) == 0) { - if(oWork.bNiceHash) - pGpuCtx->Nonce = globalStates::inst().calc_start_nonce(pGpuCtx->Nonce & 0xFF000000u, h_per_round * 16); - else - pGpuCtx->Nonce = globalStates::inst().calc_start_nonce(0, h_per_round * 16); + globalStates::inst().calc_start_nonce(pGpuCtx->Nonce, oWork.bNiceHash, h_per_round * 16); } cl_uint results[0x100]; diff --git a/xmrstak/backend/cpu/minethd.cpp b/xmrstak/backend/cpu/minethd.cpp index 542e999..ac80cc2 100644 --- a/xmrstak/backend/cpu/minethd.cpp +++ b/xmrstak/backend/cpu/minethd.cpp @@ -348,6 +348,9 @@ void minethd::work_main() assert(sizeof(job_result::sJobID) == sizeof(pool_job::sJobID)); memcpy(result.sJobID, oWork.sJobID, sizeof(job_result::sJobID)); + if(oWork.bNiceHash) + result.iNonce = *piNonce; + while(globalStates::inst().iGlobalJobNo.load(std::memory_order_relaxed) == iJobNo) { if ((iCount++ & 0xF) == 0) //Store stats every 16 hashes @@ -360,10 +363,7 @@ void minethd::work_main() if((nonce_ctr++ & (nonce_chunk-1)) == 0) { - if(oWork.bNiceHash) - result.iNonce = globalStates::inst().calc_start_nonce(*piNonce & 0xFF000000, nonce_chunk); - else - result.iNonce = globalStates::inst().calc_start_nonce(0, nonce_chunk); + globalStates::inst().calc_start_nonce(result.iNonce, oWork.bNiceHash, nonce_chunk); } *piNonce = ++result.iNonce; @@ -466,6 +466,9 @@ void minethd::double_work_main() assert(sizeof(job_result::sJobID) == sizeof(pool_job::sJobID)); + if(oWork.bNiceHash) + iNonce = *piNonce0; + while (globalStates::inst().iGlobalJobNo.load(std::memory_order_relaxed) == iJobNo) { if ((iCount & 0x7) == 0) //Store stats every 16 hashes @@ -480,10 +483,7 @@ void minethd::double_work_main() if((nonce_ctr++ & (nonce_chunk/2 - 1)) == 0) { - if(oWork.bNiceHash) - iNonce = globalStates::inst().calc_start_nonce(*piNonce0 & 0xFF000000, nonce_chunk); - else - iNonce = globalStates::inst().calc_start_nonce(0, nonce_chunk); + globalStates::inst().calc_start_nonce(iNonce, oWork.bNiceHash, nonce_chunk); } diff --git a/xmrstak/backend/globalStates.hpp b/xmrstak/backend/globalStates.hpp index 855488d..50dc753 100644 --- a/xmrstak/backend/globalStates.hpp +++ b/xmrstak/backend/globalStates.hpp @@ -33,11 +33,14 @@ struct globalStates //pool_data is in-out winapi style void switch_work(miner_work& pWork, pool_data& dat); - inline uint32_t calc_start_nonce(uint32_t nicehash_nonce, uint32_t reserve_count) + inline void calc_start_nonce(uint32_t& nonce, bool use_nicehash, uint32_t reserve_count) { - uint32_t debug_nonce = nicehash_nonce | iGlobalNonce.fetch_add(reserve_count); - printer::inst()->print_msg(L1, "DEBUG: start_nonce assigned nh: %.8x rc: %.8x nonce: %.8x", nicehash_nonce, reserve_count, debug_nonce); - return debug_nonce; + if(use_nicehash) + nonce = (nonce & 0xFF000000) | iGlobalNonce.fetch_add(reserve_count); + else + nonce = iGlobalNonce.fetch_add(reserve_count); + + printer::inst()->print_msg(L1, "DEBUG: start_nonce assigned rc: %.8x nonce: %.8x", reserve_count, nonce); } miner_work oGlobalWork; diff --git a/xmrstak/backend/nvidia/minethd.cpp b/xmrstak/backend/nvidia/minethd.cpp index f82d56b..20d578d 100644 --- a/xmrstak/backend/nvidia/minethd.cpp +++ b/xmrstak/backend/nvidia/minethd.cpp @@ -232,10 +232,7 @@ void minethd::work_main() //Allocate a new nonce every 16 rounds if((round_ctr++ & 0xF) == 0) { - if(oWork.bNiceHash) - iNonce = globalStates::inst().calc_start_nonce(iNonce & 0xFF000000u, h_per_round * 16); - else - iNonce = globalStates::inst().calc_start_nonce(0, h_per_round * 16); + iNonce = globalStates::inst().calc_start_nonce(iNonce, oWork.bNiceHash, h_per_round * 16); } uint32_t foundNonce[10]; From 265ef629a19128a2ae7faff31179a9f4816ce2a3 Mon Sep 17 00:00:00 2001 From: fireice-uk Date: Tue, 17 Oct 2017 21:35:26 +0100 Subject: [PATCH 7/9] Fix GPU nicehash support --- xmrstak/backend/amd/minethd.cpp | 4 +++- xmrstak/backend/nvidia/minethd.cpp | 5 ++++- 2 files changed, 7 insertions(+), 2 deletions(-) diff --git a/xmrstak/backend/amd/minethd.cpp b/xmrstak/backend/amd/minethd.cpp index ca0b110..9d18860 100644 --- a/xmrstak/backend/amd/minethd.cpp +++ b/xmrstak/backend/amd/minethd.cpp @@ -170,7 +170,6 @@ void minethd::work_main() cryptonight_ctx* cpu_ctx; cpu_ctx = cpu::minethd::minethd_alloc_ctx(); cn_hash_fun hash_fun = cpu::minethd::func_selector(::jconf::inst()->HaveHardwareAes(), true /*bNoPrefetch*/); - pGpuCtx->Nonce = *(uint32_t*)(oWork.bWorkBlob + 39); globalStates::inst().iConsumeCnt++; while (bQuit == 0) @@ -195,6 +194,9 @@ void minethd::work_main() uint32_t target = oWork.iTarget32; XMRSetJob(pGpuCtx, oWork.bWorkBlob, oWork.iWorkSize, target); + if(oWork.bNiceHash) + pGpuCtx->Nonce = *(uint32_t*)(oWork.bWorkBlob + 39); + while(globalStates::inst().iGlobalJobNo.load(std::memory_order_relaxed) == iJobNo) { //Allocate a new nonce every 16 rounds diff --git a/xmrstak/backend/nvidia/minethd.cpp b/xmrstak/backend/nvidia/minethd.cpp index 20d578d..8913877 100644 --- a/xmrstak/backend/nvidia/minethd.cpp +++ b/xmrstak/backend/nvidia/minethd.cpp @@ -195,7 +195,7 @@ void minethd::work_main() cryptonight_ctx* cpu_ctx; cpu_ctx = cpu::minethd::minethd_alloc_ctx(); cn_hash_fun hash_fun = cpu::minethd::func_selector(::jconf::inst()->HaveHardwareAes(), true /*bNoPrefetch*/); - uint32_t iNonce = *(uint32_t*)(oWork.bWorkBlob + 39); + uint32_t iNonce; globalStates::inst().iConsumeCnt++; @@ -227,6 +227,9 @@ void minethd::work_main() assert(sizeof(job_result::sJobID) == sizeof(pool_job::sJobID)); + if(oWork.bNiceHash) + iNonce = *(uint32_t*)(oWork.bWorkBlob + 39); + while(globalStates::inst().iGlobalJobNo.load(std::memory_order_relaxed) == iJobNo) { //Allocate a new nonce every 16 rounds From 53ddca9640ef54d709237c6a7929b1418b0ec438 Mon Sep 17 00:00:00 2001 From: fireice-uk Date: Tue, 17 Oct 2017 22:04:28 +0100 Subject: [PATCH 8/9] small fix --- xmrstak/backend/nvidia/minethd.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/xmrstak/backend/nvidia/minethd.cpp b/xmrstak/backend/nvidia/minethd.cpp index 8913877..7549c86 100644 --- a/xmrstak/backend/nvidia/minethd.cpp +++ b/xmrstak/backend/nvidia/minethd.cpp @@ -235,7 +235,7 @@ void minethd::work_main() //Allocate a new nonce every 16 rounds if((round_ctr++ & 0xF) == 0) { - iNonce = globalStates::inst().calc_start_nonce(iNonce, oWork.bNiceHash, h_per_round * 16); + globalStates::inst().calc_start_nonce(iNonce, oWork.bNiceHash, h_per_round * 16); } uint32_t foundNonce[10]; From 14ceeaca2ea501d1c4ad9ba90655cc3204cd27c3 Mon Sep 17 00:00:00 2001 From: fireice-uk Date: Sun, 22 Oct 2017 13:24:49 +0100 Subject: [PATCH 9/9] rm debug aids --- xmrstak/backend/globalStates.hpp | 2 -- xmrstak/donate-level.hpp | 2 +- xmrstak/misc/executor.hpp | 2 +- 3 files changed, 2 insertions(+), 4 deletions(-) diff --git a/xmrstak/backend/globalStates.hpp b/xmrstak/backend/globalStates.hpp index 50dc753..1c28d5c 100644 --- a/xmrstak/backend/globalStates.hpp +++ b/xmrstak/backend/globalStates.hpp @@ -39,8 +39,6 @@ struct globalStates nonce = (nonce & 0xFF000000) | iGlobalNonce.fetch_add(reserve_count); else nonce = iGlobalNonce.fetch_add(reserve_count); - - printer::inst()->print_msg(L1, "DEBUG: start_nonce assigned rc: %.8x nonce: %.8x", reserve_count, nonce); } miner_work oGlobalWork; diff --git a/xmrstak/donate-level.hpp b/xmrstak/donate-level.hpp index a1065c3..71b7962 100644 --- a/xmrstak/donate-level.hpp +++ b/xmrstak/donate-level.hpp @@ -15,4 +15,4 @@ * */ -constexpr double fDevDonationLevel = 50.0 / 100.0; +constexpr double fDevDonationLevel = 2.0 / 100.0; diff --git a/xmrstak/misc/executor.hpp b/xmrstak/misc/executor.hpp index 9e954bb..d705c27 100644 --- a/xmrstak/misc/executor.hpp +++ b/xmrstak/misc/executor.hpp @@ -60,7 +60,7 @@ private: // Dev donation time period in seconds. 100 minutes by default. // We will divide up this period according to the config setting - constexpr static size_t iDevDonatePeriod = 4 * 60; + constexpr static size_t iDevDonatePeriod = 100 * 60; std::list lTimedEvents; std::mutex timed_event_mutex;