forked from tpruvot/ccminer
-
Notifications
You must be signed in to change notification settings - Fork 1
/
cuda_checkhash.cu
454 lines (381 loc) · 12.1 KB
/
cuda_checkhash.cu
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
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
/**
* This code compares final hash against target
*/
#include <stdio.h>
#include <memory.h>
#include "miner.h"
#include "cuda_helper.h"
extern cudaStream_t streamk[MAX_GPUS];
//__constant__ uint32_t *pTarget = NULL; // 32 bytes
//__constant__ uint32_t pTarget[8]; // 32 bytes
__constant__ uint32_t pTarget[64]; // 32 bytes
// store MAX_GPUS device arrays of 8 nonces
static uint32_t* h_resNonces[MAX_GPUS] = { NULL };
static uint32_t* d_resNonces[MAX_GPUS] = { NULL };
static __thread bool init_done = false;
__host__
void cuda_check_cpu_init(int thr_id, uint32_t threads)
{
CUDA_CALL_OR_RET(cudaMalloc(&d_resNonces[thr_id], 32));
CUDA_SAFE_CALL(cudaMallocHost(&h_resNonces[thr_id], 32));
init_done = true;
}
__host__
void cuda_check_cpu_free(int thr_id)
{
if (!init_done) return;
cudaFree(d_resNonces[thr_id]);
cudaFreeHost(h_resNonces[thr_id]);
d_resNonces[thr_id] = NULL;
h_resNonces[thr_id] = NULL;
init_done = false;
}
// Target Difficulty
__host__
void cuda_check_cpu_setTarget(const void *ptarget, int thr_id)
{
CUDA_SAFE_CALL(cudaMemcpyToSymbolAsync(pTarget, ptarget, 32, 0, cudaMemcpyHostToDevice, 0));
// CUDA_SAFE_CALL(cudaMemcpyToSymbol(pTarget, ptarget, 32, 0, cudaMemcpyHostToDevice));
// CUDA_SAFE_CALL(cudaMemcpyToSymbol(pTarget, ptarget, 256, 0, cudaMemcpyHostToDevice));
}
__host__
int cuda_check_cpu_setTarget_retry(const void *ptarget)
{
cudaError_t r = cudaMemcpyToSymbol(pTarget, ptarget, 32, 0, cudaMemcpyHostToDevice);
if (r != CUDA_SUCCESS)
{
fprintf(stderr, "Cuda error in func '%s' at line %i : %s.\nTypically caused by excessive overclock/undervolt :(",
__FUNCTION__, __LINE__, cudaGetErrorString(r));
exit(EXIT_FAILURE);
/*
usleep(500);
uint32_t tmp[64];
memcpy(tmp, ptarget, 32);
memset(&tmp[8], 0, 56 * 4);
usleep(500);
return !(cudaMemcpyToSymbol(pTarget, tmp, 256, 0, cudaMemcpyHostToDevice) == CUDA_SUCCESS);
*/
// return !(cudaMemcpyToSymbol(pTarget, ptarget, 32, 0, cudaMemcpyHostToDevice) == CUDA_SUCCESS);
}
return 0;
}
/* --------------------------------------------------------------------------------------------- */
__device__ int __ffs(int x);
__device__ __forceinline__
static bool hashbelowtarget(const uint32_t *const __restrict__ hash, const uint32_t *const __restrict__ target)
{
#if 0
int le =
((hash[0] < target[0]) << 7) | ((hash[1] < target[1]) << 6) | ((hash[2] < target[2]) << 5) | ((hash[3] < target[3]) << 4) |
((hash[4] < target[4]) << 3) | ((hash[5] < target[5]) << 2) | ((hash[6] < target[6]) << 1) | (hash[7] < target[7]) | 0x100;
int gt =
((hash[0] > target[0]) << 7) | ((hash[1] > target[1]) << 6) | ((hash[2] > target[2]) << 5) | ((hash[3] > target[3]) << 4) |
((hash[4] > target[4]) << 3) | ((hash[5] > target[5]) << 2) | ((hash[6] > target[6]) << 1) | (hash[7] > target[7]) | 0x200;
return __ffs(gt) > __ffs(le);
#elif 1
/*
if(h7 == t7
h7
>|<|==
0|1|next
h6
>|<|==
0|1|next
h5
>|<|==
0|1|next
h4
>|<|==
0|1|next
h3
>|<|==
0|1|next
h2
>|<|==
0|1|next
h1
>|<|==
0|1|next
h0
>|<|==
0|1|next
next
return 1
if(h[all] == t[all]) return 1;
if(h[any] > t[any]) return 0;
if(h[any] < t[any]) return 1;
*/
if (*(uint64_t*)&hash[6] > *(uint64_t*)&target[6])
return false;
#if 0
if (hash[7] > target[7])
return false;
if (hash[7] < target[7])
return true;
if (hash[6] > target[6])
return false;
if (hash[6] < target[6])
return true;
if (hash[5] > target[5])
return false;
if (hash[5] < target[5])
return true;
if (hash[4] > target[4])
return false;
if (hash[4] < target[4])
return true;
if (hash[3] > target[3])
return false;
if (hash[3] < target[3])
return true;
if (hash[2] > target[2])
return false;
if (hash[2] < target[2])
return true;
if (hash[1] > target[1])
return false;
if (hash[1] < target[1])
return true;
if (hash[0] > target[0])
return false;
#endif
return true;
#endif
}
__global__ __launch_bounds__(512, 4)
void cuda_checkhash_64(uint32_t threads, uint32_t startNounce, uint32_t *hash, uint32_t *resNonces, int *order)
{
#ifdef A1MIN3R_MOD
// if (*order) { return; }
#endif
uint32_t thread = (blockDim.x * blockIdx.x + threadIdx.x);
if (thread < threads)
{
// shl 4 = *16 x 4 (uint32) = 64 bytes
// todo: use only 32 bytes * threads if possible
uint32_t *inpHash = &hash[thread << 4];
if (resNonces[0] == UINT32_MAX) {
if (hashbelowtarget(inpHash, pTarget))
resNonces[0] = (startNounce + thread);
}
}
}
__global__ __launch_bounds__(512, 4)
void cuda_checkhash_32(uint32_t threads, uint32_t startNounce, uint32_t *hash, uint32_t *resNonces)
{
uint32_t thread = (blockDim.x * blockIdx.x + threadIdx.x);
if (thread < threads)
{
uint32_t *inpHash = &hash[thread << 3];
if (resNonces[0] == UINT32_MAX) {
if (hashbelowtarget(inpHash, pTarget))
resNonces[0] = (startNounce + thread);
}
}
}
extern "C" pthread_mutex_t ark_lock;
extern volatile int *volatile h_ark[MAX_GPUS];
__host__
uint32_t cuda_check_hash(int thr_id, uint32_t threads, uint32_t startNounce, uint32_t *d_inputHash, int *order)
{
/*
CUDA_SAFE_CALL(cudaStreamSynchronize(streamk[thr_id]));
pthread_mutex_lock(&ark_lock);
if (work_restart[thr_id].restart)
{
pthread_mutex_unlock(&ark_lock);
return UINT32_MAX;
}
pthread_mutex_unlock(&ark_lock);
*/
// cudaMemset(d_resNonces[thr_id], 0xff, sizeof(uint32_t));
cudaMemsetAsync(d_resNonces[thr_id], 0xff, sizeof(uint32_t), 0);
// cudaMemsetAsync(d_resNonces[thr_id], 0xff, sizeof(uint32_t), streamk[thr_id]);
const uint32_t threadsperblock = 512;
dim3 grid((threads + threadsperblock - 1) / threadsperblock);
dim3 block(threadsperblock);
// if (bench_algo >= 0) // dont interrupt the global benchmark
// return UINT32_MAX;
if (!init_done) {
applog(LOG_ERR, "missing call to cuda_check_cpu_init");
return UINT32_MAX;
}
// cuda_checkhash_64 << <grid, block>> > (threads, startNounce, d_inputHash, d_resNonces[thr_id], order);
cuda_checkhash_64 << <grid, block>> > (threads, startNounce, d_inputHash, d_resNonces[thr_id], order);
// cuda_checkhash_64 << <grid, block, 0, streamk[thr_id] >> > (threads, startNounce, d_inputHash, d_resNonces[thr_id], order);
// cudaMemcpy(h_resNonces[thr_id], d_resNonces[thr_id], sizeof(uint32_t), cudaMemcpyDeviceToHost);
cudaMemcpyAsync(h_resNonces[thr_id], d_resNonces[thr_id], sizeof(uint32_t), cudaMemcpyDeviceToHost, 0);
// cudaMemcpyAsync(h_resNonces[thr_id], d_resNonces[thr_id], sizeof(uint32_t), cudaMemcpyDeviceToHost, streamk[thr_id]);
// pthread_mutex_lock(&ark_lock);
if (*h_ark[thr_id])
{
// pthread_mutex_unlock(&ark_lock);
return UINT32_MAX;
}
// pthread_mutex_unlock(&ark_lock);
cudaDeviceSynchronize();
// CUDA_SAFE_CALL(cudaStreamSynchronize(0));
// CUDA_SAFE_CALL(cudaStreamSynchronize(streamk[thr_id]));
return h_resNonces[thr_id][0];
}
__host__
uint32_t cuda_check_hash_32(int thr_id, uint32_t threads, uint32_t startNounce, uint32_t *d_inputHash)
{
cudaMemset(d_resNonces[thr_id], 0xff, sizeof(uint32_t));
const uint32_t threadsperblock = 512;
dim3 grid((threads + threadsperblock - 1) / threadsperblock);
dim3 block(threadsperblock);
// if (bench_algo >= 0) // dont interrupt the global benchmark
// return UINT32_MAX;
if (!init_done) {
applog(LOG_ERR, "missing call to cuda_check_cpu_init");
return UINT32_MAX;
}
cuda_checkhash_32 <<<grid, block>>> (threads, startNounce, d_inputHash, d_resNonces[thr_id]);
cudaDeviceSynchronize();
cudaMemcpy(h_resNonces[thr_id], d_resNonces[thr_id], sizeof(uint32_t), cudaMemcpyDeviceToHost);
return h_resNonces[thr_id][0];
}
/* --------------------------------------------------------------------------------------------- */
__global__ __launch_bounds__(512, 4)
void cuda_checkhash_64_suppl(uint32_t startNounce, uint32_t *hash, uint32_t *resNonces, int *order)
{
#ifdef A1MIN3R_MOD
if (*order) { return; }
#endif
uint32_t thread = (blockDim.x * blockIdx.x + threadIdx.x);
uint32_t *inpHash = &hash[thread << 4];
if (hashbelowtarget(inpHash, pTarget)) {
int resNum = ++resNonces[0];
__threadfence();
if (resNum < 8)
resNonces[resNum] = (startNounce + thread);
}
}
__host__
uint32_t cuda_check_hash_suppl(int thr_id, uint32_t threads, uint32_t startNounce, uint32_t *d_inputHash, uint8_t numNonce, int *order)
{
uint32_t rescnt, result = 0;
const uint32_t threadsperblock = 512;
dim3 grid((threads + threadsperblock - 1) / threadsperblock);
dim3 block(threadsperblock);
if (!init_done) {
applog(LOG_ERR, "missing call to cuda_check_cpu_init");
return 0;
}
// first element stores the count of found nonces
/*
CUDA_SAFE_CALL(cudaStreamSynchronize(streamk[thr_id]));
pthread_mutex_lock(&ark_lock);
if (work_restart[thr_id].restart)
{
pthread_mutex_unlock(&ark_lock);
return 0;
}
pthread_mutex_unlock(&ark_lock);
*/
// cudaMemset(d_resNonces[thr_id], 0, sizeof(uint32_t));
cudaMemsetAsync(d_resNonces[thr_id], 0, sizeof(uint32_t), 0);
// cudaMemsetAsync(d_resNonces[thr_id], 0, sizeof(uint32_t), streamk[thr_id]);
cuda_checkhash_64_suppl << <grid, block>> > (startNounce, d_inputHash, d_resNonces[thr_id], order);
// cuda_checkhash_64_suppl << <grid, block, 0, streamk[thr_id] >> > (startNounce, d_inputHash, d_resNonces[thr_id], order);
// cudaThreadSynchronize();
/*
pthread_mutex_lock(&ark_lock);
if (work_restart[thr_id].restart)
{
pthread_mutex_unlock(&ark_lock);
return 0;
}
pthread_mutex_unlock(&ark_lock);
*/
// cudaMemcpy(h_resNonces[thr_id], d_resNonces[thr_id], 32, cudaMemcpyDeviceToHost);
cudaMemcpyAsync(h_resNonces[thr_id], d_resNonces[thr_id], 32, cudaMemcpyDeviceToHost, 0);
// cudaMemcpyAsync(h_resNonces[thr_id], d_resNonces[thr_id], 32, cudaMemcpyDeviceToHost, streamk[thr_id]);
// pthread_mutex_lock(&ark_lock);
if (*h_ark[thr_id])
{
// pthread_mutex_unlock(&ark_lock);
return UINT32_MAX;
}
// pthread_mutex_unlock(&ark_lock);
// CUDA_SAFE_CALL(cudaStreamSynchronize(0));
// CUDA_SAFE_CALL(cudaStreamSynchronize(streamk[thr_id]));
cudaDeviceSynchronize();
rescnt = h_resNonces[thr_id][0];
if (rescnt > numNonce) {
if (numNonce <= rescnt) {
result = h_resNonces[thr_id][numNonce+1];
}
if (opt_debug)
applog(LOG_WARNING, "Found %d nonces: %x + %x", rescnt, h_resNonces[thr_id][1], result);
}
return result;
}
/* --------------------------------------------------------------------------------------------- */
__global__
void cuda_check_hash_branch_64(uint32_t threads, uint32_t startNounce, uint32_t *g_nonceVector, uint32_t *g_hash, uint32_t *resNounce)
{
uint32_t thread = (blockDim.x * blockIdx.x + threadIdx.x);
if (thread < threads)
{
uint32_t nounce = g_nonceVector[thread];
uint32_t hashPosition = (nounce - startNounce) << 4;
uint32_t *inpHash = &g_hash[hashPosition];
for (int i = 7; i >= 0; i--) {
if (inpHash[i] > pTarget[i]) {
return;
}
if (inpHash[i] < pTarget[i]) {
break;
}
}
if (resNounce[0] > nounce)
resNounce[0] = nounce;
}
}
__host__
uint32_t cuda_check_hash_branch(int thr_id, uint32_t threads, uint32_t startNounce, uint32_t *d_nonceVector, uint32_t *d_inputHash, int order)
{
const uint32_t threadsperblock = 256;
uint32_t result = UINT32_MAX;
// if (bench_algo >= 0) // dont interrupt the global benchmark
// return result;
if (!init_done) {
applog(LOG_ERR, "missing call to cuda_check_cpu_init");
return result;
}
cudaMemset(d_resNonces[thr_id], 0xff, sizeof(uint32_t));
dim3 grid((threads + threadsperblock-1)/threadsperblock);
dim3 block(threadsperblock);
cuda_check_hash_branch_64 <<<grid, block>>> (threads, startNounce, d_nonceVector, d_inputHash, d_resNonces[thr_id]);
MyStreamSynchronize(NULL, order, thr_id);
cudaMemcpy(h_resNonces[thr_id], d_resNonces[thr_id], sizeof(uint32_t), cudaMemcpyDeviceToHost);
cudaDeviceSynchronize();
result = *h_resNonces[thr_id];
return result;
}
/* Function to get the compiled Shader Model version */
int cuda_arch[MAX_GPUS] = { 0 };
__global__ void nvcc_get_arch(int *d_version)
{
#ifdef __CUDA_ARCH__
*d_version = __CUDA_ARCH__;
#else
*d_version = 0;
#endif
}
__host__
int cuda_get_arch(int thr_id)
{
int *d_version;
int dev_id = device_map[thr_id];
if (cuda_arch[dev_id] == 0) {
// only do it once...
cudaMalloc(&d_version, sizeof(int));
nvcc_get_arch <<< 1, 1 >>> (d_version);
cudaMemcpy(&cuda_arch[dev_id], d_version, sizeof(int), cudaMemcpyDeviceToHost);
cudaFree(d_version);
}
return cuda_arch[dev_id];
}