-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathxpmclient.h
265 lines (195 loc) · 5.24 KB
/
xpmclient.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
/*
* xpmclient.h
*
* Created on: 01.05.2014
* Author: mad
*/
#ifndef XPMCLIENT_H_
#define XPMCLIENT_H_
#include <gmp.h>
#include <gmpxx.h>
#include "baseclient.h"
#include "opencl.h"
#include "uint256.h"
#include "sha256.h"
#define FERMAT_PIPELINES 2
#define PW 512 // Pipeline width (number of hashes to store)
#define SW 16 // maximum number of sieves in one iteration
#define MSO 128*1024 // max sieve output
#define MFS 2*SW*MSO // max fermat size
const unsigned maxHashPrimorial = 16;
extern unsigned gPrimes[96*1024];
extern std::vector<unsigned> gPrimes2;
extern cl_program gProgram;
struct stats_t {
unsigned id;
unsigned errors;
unsigned fps;
double primeprob;
double cpd;
stats_t(){
id = 0;
errors = 0;
fps = 0;
primeprob = 0;
cpd = 0;
}
};
struct config_t {
cl_uint N;
cl_uint SIZE;
cl_uint STRIPES;
cl_uint WIDTH;
cl_uint PCOUNT;
cl_uint TARGET;
};
template<typename T> class lifoBuffer {
private:
T *_data;
size_t _size;
size_t _readPos;
size_t _writePos;
size_t nextPos(size_t pos) { return (pos+1) % _size; }
public:
lifoBuffer(size_t size) : _size(size), _readPos(0), _writePos(0) {
_data = new T[size];
}
size_t readPos() const { return _readPos; }
size_t writePos() const { return _writePos; }
T *data() const { return _data; }
T& get(size_t index) const { return _data[index]; }
bool empty() const {
return _readPos == _writePos;
}
size_t remaining() const {
return _writePos >= _readPos ?
_writePos - _readPos :
_size - (_readPos - _writePos);
}
void clear() {
_readPos = _writePos;
}
size_t push(const T& element) {
size_t oldWritePos = _writePos;
size_t nextWritePos = nextPos(_writePos);
if (nextWritePos != _readPos) {
_data[_writePos] = element;
_writePos = nextWritePos;
}
return oldWritePos;
}
size_t pop() {
size_t oldReadPos = _readPos;
if (!empty())
_readPos = nextPos(_readPos);
return oldReadPos;
}
};
class PrimeMiner {
public:
struct block_t {
static const int CURRENT_VERSION = 2;
int version;
uint256 hashPrevBlock;
uint256 hashMerkleRoot;
unsigned int time;
unsigned int bits;
unsigned int nonce;
};
struct search_t {
clBuffer<cl_uint> midstate;
clBuffer<cl_uint> found;
clBuffer<cl_uint> primorialBitField;
clBuffer<cl_uint> count;
};
struct hash_t {
unsigned iter;
unsigned nonce;
unsigned time;
uint256 hash;
mpz_class shash;
mpz_class primorial;
unsigned primorialIdx;
};
struct fermat_t {
cl_uint index;
cl_uint hashid;
cl_uchar origin;
cl_uchar chainpos;
cl_uchar type;
cl_uchar reserved;
};
struct info_t {
clBuffer<fermat_t> info;
clBuffer<cl_uint> count;
};
struct pipeline_t {
unsigned current;
unsigned bsize;
clBuffer<cl_uint> input;
clBuffer<cl_uchar> output;
info_t buffer[2];
};
struct sieve_t {
info_t cunningham1[1];
info_t cunningham2[1];
};
PrimeMiner(unsigned id, unsigned threads, unsigned hashprim, unsigned prim, unsigned sievePerRound, unsigned depth, unsigned LSize);
~PrimeMiner();
bool Initialize(cl_device_id dev);
static void InvokeMining(void *args, zctx_t *ctx, void *pipe);
config_t getConfig() { return mConfig; }
bool MakeExit;
private:
void FermatInit(pipeline_t &fermat, unsigned mfs);
void FermatDispatch(pipeline_t &fermat,
clBuffer<fermat_t> sieveBuffers[SW][FERMAT_PIPELINES][2],
clBuffer<cl_uint> candidatesCountBuffers[SW][2],
unsigned pipelineIdx,
int ridx,
int widx,
uint64_t &testCount,
uint64_t &fermatCount,
cl_kernel fermatKernel,
unsigned sievePerRound);
void Mining(zctx_t *ctx, void *pipe);
unsigned mID;
unsigned mThreads;
config_t mConfig;
unsigned mPrimorial;
unsigned mSievePerRound;
unsigned mHashPrimorial;
unsigned mBlockSize;
cl_uint mDepth;
unsigned mLSize;
cl_command_queue mSmall;
cl_command_queue mBig;
cl_kernel mHashMod;
cl_kernel mSieveSetup;
cl_kernel mSieve;
cl_kernel mSieveSearch;
cl_kernel mFermatSetup;
cl_kernel mFermatKernel352;
cl_kernel mFermatKernel320;
cl_kernel mFermatCheck;
info_t final;
};
class XPMClient : public BaseClient {
public:
XPMClient(zctx_t* ctx);
virtual ~XPMClient();
bool Initialize(Configuration* cfg, bool benchmarkOnly = false);
void NotifyBlock(const proto::Block& block);
void TakeWork(const proto::Work& work);
int GetStats(proto::ClientStats& stats);
void Toggle();
void setup_adl();
private:
std::vector<std::pair<PrimeMiner*, void*> > mWorkers;
void dumpSieveConstants(unsigned weaveDepth,
unsigned threadsNum,
unsigned windowSize,
unsigned *primes,
std::ostream &file);
};
#endif /* XPMCLIENT_H_ */