Skip to content
This repository was archived by the owner on Apr 24, 2022. It is now read-only.

Commit 99d19cc

Browse files
committed
getNewWork hook function for openCL GPU thread
1 parent 163ddf5 commit 99d19cc

File tree

4 files changed

+105
-15
lines changed

4 files changed

+105
-15
lines changed

libethash-cl/ethash_cl_miner.cpp

Lines changed: 41 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -532,15 +532,47 @@ void ethash_cl_miner::search(uint8_t const* header, uint64_t target, search_hook
532532
for (unsigned i = 0; i != num_found; ++i)
533533
nonces[i] = batch.start_nonce + results[i + 1];
534534

535-
m_queue.enqueueUnmapMemObject(m_searchBuffer[batch.buf], results);
536-
bool exit = num_found && hook.found(nonces, num_found);
537-
exit |= hook.searched(batch.start_nonce, m_globalWorkSize); // always report searched before exit
538-
if (exit)
539-
break;
540-
541-
// reset search buffer if we're still going
542-
if (num_found)
543-
m_queue.enqueueWriteBuffer(m_searchBuffer[batch.buf], true, 0, 4, &c_zero);
535+
m_queue.enqueueUnmapMemObject(m_searchBuffer[batch.buf], results);
536+
//bool exit = num_found && hook.found(nonces, num_found);
537+
// should not exit when a nonce is found.
538+
// instead, continue mining on the same header
539+
bool exit = false;
540+
541+
if (num_found) {
542+
// only call hook.found to report solutions
543+
bool found_result = hook.found(nonces, num_found);
544+
}
545+
546+
// check if new work is pending
547+
bool pendingNewWork = hook.searched(batch.start_nonce, m_globalWorkSize);
548+
549+
if (pendingNewWork) {
550+
JobForGPU new_work = hook.getNewWork();
551+
552+
// routine below is copy/pasted from above
553+
m_queue.enqueueWriteBuffer(m_header, false, 0, 32, new_work.header);
554+
for (unsigned i = 0; i != c_bufferCount; ++i)
555+
m_queue.enqueueWriteBuffer(m_searchBuffer[i], false, 0, 4, &c_zero);
556+
557+
#if CL_VERSION_1_2 && 0
558+
cl::Event pre_return_event;
559+
if (!m_opencl_1_1)
560+
m_queue.enqueueBarrierWithWaitList(NULL, &pre_return_event);
561+
else
562+
#endif
563+
m_queue.finish();
564+
565+
// TODO: check if target is different before bothering to set it?
566+
m_searchKernel.setArg(4, new_work.target);
567+
568+
if (_ethStratum) start_nonce = new_work.startN;
569+
else start_nonce = uniform_int_distribution<uint64_t>()(engine);
570+
} else {
571+
// reset search buffer if we're still going
572+
if (num_found) {
573+
m_queue.enqueueWriteBuffer(m_searchBuffer[batch.buf], true, 0, 4, &c_zero);
574+
}
575+
}
544576

545577
pending.pop();
546578
}

libethash-cl/ethash_cl_miner.h

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,13 @@
2424
#include <functional>
2525
#include <libethash/ethash.h>
2626

27+
struct JobForGPU
28+
{
29+
uint8_t const* header;
30+
uint64_t target;
31+
uint64_t startN;
32+
};
33+
2734
class ethash_cl_miner
2835
{
2936
private:
@@ -37,6 +44,7 @@ class ethash_cl_miner
3744
// reports progress, return true to abort
3845
virtual bool found(uint64_t const* nonces, uint32_t count) = 0;
3946
virtual bool searched(uint64_t start_nonce, uint32_t count) = 0;
47+
virtual JobForGPU getNewWork() = 0;
4048
};
4149

4250
ethash_cl_miner();

libethcore/EthashGPUMiner.cpp

Lines changed: 54 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -68,6 +68,12 @@ class EthashCLHook: public ethash_cl_miner::search_hook
6868
UniqueGuard l(x_all);
6969
m_aborted = m_abort = false;
7070
}
71+
72+
void notifyNewWork()
73+
{
74+
UniqueGuard l(x_all);
75+
m_workIsNew = true;
76+
}
7177

7278
protected:
7379
virtual bool found(uint64_t const* _nonces, uint32_t _count) override
@@ -85,14 +91,33 @@ class EthashCLHook: public ethash_cl_miner::search_hook
8591
// std::cerr << "Searched " << _count << " from " << _startNonce << std::endl;
8692
m_owner->accumulateHashes(_count);
8793
m_last = _startNonce + _count;
88-
if (m_abort || m_owner->shouldStop())
89-
return (m_aborted = true);
90-
return false;
94+
//if (m_abort || m_owner->shouldStop())
95+
// return (m_aborted = true);
96+
//return false;
97+
// TODO: still need a way to stop the GPU thread
98+
99+
if (m_workIsNew) {
100+
return true;
101+
} else {
102+
return false;
103+
}
104+
105+
}
106+
107+
virtual JobForGPU getNewWork()
108+
{
109+
UniqueGuard l(x_all);
110+
m_workIsNew = false;
111+
JobForGPU new_work = m_owner->getWork();
112+
printf("new_work header: %02x%02x \n", new_work.header[0], new_work.header[1]);
113+
114+
return new_work;
91115
}
92116

93117
private:
94118
Mutex x_all;
95119
uint64_t m_last;
120+
bool m_workIsNew = false;
96121
bool m_abort = false;
97122
Notified<bool> m_aborted = {true};
98123
EthashGPUMiner* m_owner = nullptr;
@@ -120,6 +145,19 @@ EthashGPUMiner::~EthashGPUMiner()
120145
delete m_hook;
121146
}
122147

148+
JobForGPU EthashGPUMiner::getWork()
149+
{
150+
WorkPackage w_kick = work();
151+
uint64_t upper64OfBoundary = (uint64_t)(u64)((u256)w_kick.boundary >> 192);
152+
uint64_t startN;
153+
if (w_kick.exSizeBits >= 0)
154+
startN = w_kick.startNonce | ((uint64_t)index() << (64 - 4 - w_kick.exSizeBits)); // this can support up to 16 devices
155+
156+
JobForGPU new_work = {w_kick.headerHash.data(), upper64OfBoundary, startN};
157+
158+
return new_work;
159+
}
160+
123161
bool EthashGPUMiner::report(uint64_t _nonce)
124162
{
125163
Nonce n = (Nonce)(u64)_nonce;
@@ -132,7 +170,14 @@ bool EthashGPUMiner::report(uint64_t _nonce)
132170
void EthashGPUMiner::kickOff()
133171
{
134172
m_hook->reset();
135-
startWorking();
173+
174+
if (isWorking()) {
175+
// if already mining, notify of new pending work
176+
m_hook->notifyNewWork();
177+
} else {
178+
// first time running.
179+
startWorking();
180+
}
136181
}
137182

138183
void EthashGPUMiner::workLoop()
@@ -182,8 +227,11 @@ void EthashGPUMiner::workLoop()
182227

183228
void EthashGPUMiner::pause()
184229
{
185-
m_hook->abort();
186-
stopWorking();
230+
if (!isWorking()) {
231+
// only happens on first pause/kickoff cycle
232+
m_hook->abort();
233+
stopWorking();
234+
}
187235
}
188236

189237
std::string EthashGPUMiner::platformInfo()

libethcore/EthashGPUMiner.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@
2727
#include <libdevcore/Worker.h>
2828
#include "EthashAux.h"
2929
#include "Miner.h"
30+
#include <libethash-cl/ethash_cl_miner.h>
3031

3132
class ethash_cl_miner;
3233

@@ -74,6 +75,7 @@ class EthashGPUMiner: public Miner, Worker
7475
private:
7576
void workLoop() override;
7677
bool report(uint64_t _nonce);
78+
JobForGPU getWork();
7779

7880
using Miner::accumulateHashes;
7981

0 commit comments

Comments
 (0)