-
Notifications
You must be signed in to change notification settings - Fork 2
/
legacy_kernel.cu
422 lines (351 loc) · 16 KB
/
legacy_kernel.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
//
// Kernel that runs best on Legacy (Compute 1.x) devices
//
// -full half-warp based memory coalescing
// -high consumption of shared memory
//
// NOTE: compile this .cu module for compute_10,sm_10 with --maxrregcount=64
//
#ifdef WIN32
#include <windows.h>
#endif
#include <stdio.h>
#include <time.h>
#include <sys/time.h>
#include <unistd.h>
#include <cuda.h>
#include "legacy_kernel.h"
#if WIN32
#ifdef _WIN64
#define _64BIT_ALIGN 1
#else
#define _64BIT_ALIGN 0
#endif
#else
#if __x86_64__
#define _64BIT_ALIGN 1
#else
#define _64BIT_ALIGN 0
#endif
#endif
#define THREADS_PER_WU 1 // single thread per hash
#define LOOKUP_GAP 1 // no support for LOOKUP_GAP
// forward references
template <int WARPS_PER_BLOCK> __global__ void legacy_scrypt_core_kernelA(uint32_t *g_idata, unsigned int N);
template <int WARPS_PER_BLOCK> __global__ void legacy_scrypt_core_kernelB(uint32_t *g_odata, unsigned int N);
template <int WARPS_PER_BLOCK, int TEX_DIM> __global__ void legacy_scrypt_core_kernelB_tex(uint32_t *g_odata, unsigned int N);
// scratchbuf constants (pointers to scratch buffer for each warp, i.e. 32 hashes)
__constant__ uint32_t* c_V[TOTAL_WARP_LIMIT];
// using texture references for the "tex" variants of the B kernels
texture<uint2, 1, cudaReadModeElementType> texRef1D_2_V;
texture<uint2, 2, cudaReadModeElementType> texRef2D_2_V;
LegacyKernel::LegacyKernel() : KernelInterface()
{
}
bool LegacyKernel::bindtexture_1D(uint32_t *d_V, size_t size)
{
cudaChannelFormatDesc channelDesc2 = cudaCreateChannelDesc<uint2>();
texRef1D_2_V.normalized = 0;
texRef1D_2_V.filterMode = cudaFilterModePoint;
texRef1D_2_V.addressMode[0] = cudaAddressModeClamp;
checkCudaErrors(cudaBindTexture(NULL, &texRef1D_2_V, d_V, &channelDesc2, size));
return true;
}
bool LegacyKernel::bindtexture_2D(uint32_t *d_V, int width, int height, size_t pitch)
{
cudaChannelFormatDesc channelDesc2 = cudaCreateChannelDesc<uint2>();
texRef2D_2_V.normalized = 0;
texRef2D_2_V.filterMode = cudaFilterModePoint;
texRef2D_2_V.addressMode[0] = cudaAddressModeClamp;
texRef2D_2_V.addressMode[1] = cudaAddressModeClamp;
checkCudaErrors(cudaBindTexture2D(NULL, &texRef2D_2_V, d_V, &channelDesc2, width, height, pitch));
return true;
}
bool LegacyKernel::unbindtexture_1D()
{
checkCudaErrors(cudaUnbindTexture(texRef1D_2_V));
return true;
}
bool LegacyKernel::unbindtexture_2D()
{
checkCudaErrors(cudaUnbindTexture(texRef2D_2_V));
return true;
}
void LegacyKernel::set_scratchbuf_constants(int MAXWARPS, uint32_t** h_V)
{
checkCudaErrors(cudaMemcpyToSymbol(c_V, h_V, MAXWARPS*sizeof(uint32_t*), 0, cudaMemcpyHostToDevice));
}
bool LegacyKernel::run_kernel(dim3 grid, dim3 threads, int WARPS_PER_BLOCK, int thr_id, cudaStream_t stream, uint32_t* d_idata, uint32_t* d_odata, unsigned int N,unsigned int LUGA, bool interactive, bool benchmark, int texture_cache)
{
bool success = true;
// clear CUDA's error variable
cudaGetLastError();
int sleeptime = 100;
int situation = 0;
// Optional sleep in between kernels
if (!benchmark && interactive) {
checkCudaErrors(MyStreamSynchronize(stream, ++situation, thr_id));
usleep(sleeptime);
}
// First phase: Sequential writes to scratchpad.
switch (WARPS_PER_BLOCK) {
case 1: legacy_scrypt_core_kernelA<1><<< grid, threads, 0, stream >>>(d_idata, N); break;
case 2: legacy_scrypt_core_kernelA<2><<< grid, threads, 0, stream >>>(d_idata, N); break;
case 3: legacy_scrypt_core_kernelA<3><<< grid, threads, 0, stream >>>(d_idata, N); break;
#if EXTRA_WARPS
case 4: legacy_scrypt_core_kernelA<4><<< grid, threads, 0, stream >>>(d_idata, N); break;
case 5: legacy_scrypt_core_kernelA<5><<< grid, threads, 0, stream >>>(d_idata, N); break;
case 6: legacy_scrypt_core_kernelA<6><<< grid, threads, 0, stream >>>(d_idata, N); break;
case 7: legacy_scrypt_core_kernelA<7><<< grid, threads, 0, stream >>>(d_idata, N); break;
case 8: legacy_scrypt_core_kernelA<8><<< grid, threads, 0, stream >>>(d_idata, N); break;
#endif
default: success = false; break;
}
// Optional millisecond sleep in between kernels
if (!benchmark && interactive) {
checkCudaErrors(MyStreamSynchronize(stream, ++situation, thr_id));
usleep(sleeptime);
}
// Second phase: Random read access from scratchpad.
if (texture_cache)
{
if (texture_cache == 1)
{
switch (WARPS_PER_BLOCK) {
case 1: legacy_scrypt_core_kernelB_tex<1,1><<< grid, threads, 0, stream >>>(d_odata, N); break;
case 2: legacy_scrypt_core_kernelB_tex<2,1><<< grid, threads, 0, stream >>>(d_odata, N); break;
case 3: legacy_scrypt_core_kernelB_tex<3,1><<< grid, threads, 0, stream >>>(d_odata, N); break;
#if EXTRA_WARPS
case 4: legacy_scrypt_core_kernelB_tex<4,1><<< grid, threads, 0, stream >>>(d_odata, N); break;
case 5: legacy_scrypt_core_kernelB_tex<5,1><<< grid, threads, 0, stream >>>(d_odata, N); break;
case 6: legacy_scrypt_core_kernelB_tex<6,1><<< grid, threads, 0, stream >>>(d_odata, N); break;
case 7: legacy_scrypt_core_kernelB_tex<7,1><<< grid, threads, 0, stream >>>(d_odata, N); break;
case 8: legacy_scrypt_core_kernelB_tex<8,1><<< grid, threads, 0, stream >>>(d_odata, N); break;
#endif
default: success = false; break;
}
}
else if (texture_cache == 2)
{
switch (WARPS_PER_BLOCK) {
case 1: legacy_scrypt_core_kernelB_tex<1,2><<< grid, threads, 0, stream >>>(d_odata, N); break;
case 2: legacy_scrypt_core_kernelB_tex<2,2><<< grid, threads, 0, stream >>>(d_odata, N); break;
case 3: legacy_scrypt_core_kernelB_tex<3,2><<< grid, threads, 0, stream >>>(d_odata, N); break;
#if EXTRA_WARPS
case 4: legacy_scrypt_core_kernelB_tex<4,2><<< grid, threads, 0, stream >>>(d_odata, N); break;
case 5: legacy_scrypt_core_kernelB_tex<5,2><<< grid, threads, 0, stream >>>(d_odata, N); break;
case 6: legacy_scrypt_core_kernelB_tex<6,2><<< grid, threads, 0, stream >>>(d_odata, N); break;
case 7: legacy_scrypt_core_kernelB_tex<7,2><<< grid, threads, 0, stream >>>(d_odata, N); break;
case 8: legacy_scrypt_core_kernelB_tex<8,2><<< grid, threads, 0, stream >>>(d_odata, N); break;
#endif
default: success = false; break;
}
} else success = false;
}
else
{
switch (WARPS_PER_BLOCK) {
case 1: legacy_scrypt_core_kernelB<1><<< grid, threads, 0, stream >>>(d_odata, N); break;
case 2: legacy_scrypt_core_kernelB<2><<< grid, threads, 0, stream >>>(d_odata, N); break;
case 3: legacy_scrypt_core_kernelB<3><<< grid, threads, 0, stream >>>(d_odata, N); break;
#if EXTRA_WARPS
case 4: legacy_scrypt_core_kernelB<4><<< grid, threads, 0, stream >>>(d_odata, N); break;
case 5: legacy_scrypt_core_kernelB<5><<< grid, threads, 0, stream >>>(d_odata, N); break;
case 6: legacy_scrypt_core_kernelB<6><<< grid, threads, 0, stream >>>(d_odata, N); break;
case 7: legacy_scrypt_core_kernelB<7><<< grid, threads, 0, stream >>>(d_odata, N); break;
case 8: legacy_scrypt_core_kernelB<8><<< grid, threads, 0, stream >>>(d_odata, N); break;
#endif
default: success = false; break;
}
}
// catch any kernel launch failures
if (cudaPeekAtLastError() != cudaSuccess) success = false;
return success;
}
#define ROTL7(a0,a1,a2,a3,a00,a10,a20,a30){\
a0^=(((a00)<<7) | ((a00)>>25) );\
a1^=(((a10)<<7) | ((a10)>>25) );\
a2^=(((a20)<<7) | ((a20)>>25) );\
a3^=(((a30)<<7) | ((a30)>>25) );\
};\
#define ROTL9(a0,a1,a2,a3,a00,a10,a20,a30){\
a0^=(((a00)<<9) | ((a00)>>23) );\
a1^=(((a10)<<9) | ((a10)>>23) );\
a2^=(((a20)<<9) | ((a20)>>23) );\
a3^=(((a30)<<9) | ((a30)>>23) );\
};\
#define ROTL13(a0,a1,a2,a3,a00,a10,a20,a30){\
a0^=(((a00)<<13) | ((a00)>>19) );\
a1^=(((a10)<<13) | ((a10)>>19) );\
a2^=(((a20)<<13) | ((a20)>>19) );\
a3^=(((a30)<<13) | ((a30)>>19) );\
};\
#define ROTL18(a0,a1,a2,a3,a00,a10,a20,a30){\
a0^=(((a00)<<18) | ((a00)>>14) );\
a1^=(((a10)<<18) | ((a10)>>14) );\
a2^=(((a20)<<18) | ((a20)>>14) );\
a3^=(((a30)<<18) | ((a30)>>14) );\
};\
static __host__ __device__ void xor_salsa8(uint32_t * const B, const uint32_t * const C)
{
uint32_t x0 = (B[ 0] ^= C[ 0]), x1 = (B[ 1] ^= C[ 1]), x2 = (B[ 2] ^= C[ 2]), x3 = (B[ 3] ^= C[ 3]);
uint32_t x4 = (B[ 4] ^= C[ 4]), x5 = (B[ 5] ^= C[ 5]), x6 = (B[ 6] ^= C[ 6]), x7 = (B[ 7] ^= C[ 7]);
uint32_t x8 = (B[ 8] ^= C[ 8]), x9 = (B[ 9] ^= C[ 9]), xa = (B[10] ^= C[10]), xb = (B[11] ^= C[11]);
uint32_t xc = (B[12] ^= C[12]), xd = (B[13] ^= C[13]), xe = (B[14] ^= C[14]), xf = (B[15] ^= C[15]);
/* Operate on columns. */
ROTL7(x4,x9,xe,x3,x0+xc,x1+x5,x6+xa,xb+xf);
ROTL9(x8,xd,x2,x7,x0+x4,x5+x9,xa+xe,x3+xf);
ROTL13(xc,x1,x6,xb,x4+x8,x9+xd,x2+xe,x3+x7);
ROTL18(x0,x5,xa,xf,x8+xc,x1+xd,x2+x6,x7+xb);
/* Operate on rows. */
ROTL7(x1,x6,xb,xc,x0+x3,x4+x5,x9+xa,xe+xf);
ROTL9(x2,x7,x8,xd,x0+x1,x5+x6,xa+xb,xc+xf);
ROTL13(x3,x4,x9,xe,x1+x2,x6+x7,x8+xb,xc+xd);
ROTL18(x0,x5,xa,xf,x2+x3,x4+x7,x8+x9,xd+xe);
/* Operate on columns. */
ROTL7(x4,x9,xe,x3,x0+xc,x1+x5,x6+xa,xb+xf);
ROTL9(x8,xd,x2,x7,x0+x4,x5+x9,xa+xe,x3+xf);
ROTL13(xc,x1,x6,xb,x4+x8,x9+xd,x2+xe,x3+x7);
ROTL18(x0,x5,xa,xf,x8+xc,x1+xd,x2+x6,x7+xb);
/* Operate on rows. */
ROTL7(x1,x6,xb,xc,x0+x3,x4+x5,x9+xa,xe+xf);
ROTL9(x2,x7,x8,xd,x0+x1,x5+x6,xa+xb,xc+xf);
ROTL13(x3,x4,x9,xe,x1+x2,x6+x7,x8+xb,xc+xd);
ROTL18(x0,x5,xa,xf,x2+x3,x4+x7,x8+x9,xd+xe);
/* Operate on columns. */
ROTL7(x4,x9,xe,x3,x0+xc,x1+x5,x6+xa,xb+xf);
ROTL9(x8,xd,x2,x7,x0+x4,x5+x9,xa+xe,x3+xf);
ROTL13(xc,x1,x6,xb,x4+x8,x9+xd,x2+xe,x3+x7);
ROTL18(x0,x5,xa,xf,x8+xc,x1+xd,x2+x6,x7+xb);
/* Operate on rows. */
ROTL7(x1,x6,xb,xc,x0+x3,x4+x5,x9+xa,xe+xf);
ROTL9(x2,x7,x8,xd,x0+x1,x5+x6,xa+xb,xc+xf);
ROTL13(x3,x4,x9,xe,x1+x2,x6+x7,x8+xb,xc+xd);
ROTL18(x0,x5,xa,xf,x2+x3,x4+x7,x8+x9,xd+xe);
/* Operate on columns. */
ROTL7(x4,x9,xe,x3,x0+xc,x1+x5,x6+xa,xb+xf);
ROTL9(x8,xd,x2,x7,x0+x4,x5+x9,xa+xe,x3+xf);
ROTL13(xc,x1,x6,xb,x4+x8,x9+xd,x2+xe,x3+x7);
ROTL18(x0,x5,xa,xf,x8+xc,x1+xd,x2+x6,x7+xb);
/* Operate on rows. */
ROTL7(x1,x6,xb,xc,x0+x3,x4+x5,x9+xa,xe+xf);
ROTL9(x2,x7,x8,xd,x0+x1,x5+x6,xa+xb,xc+xf);
ROTL13(x3,x4,x9,xe,x1+x2,x6+x7,x8+xb,xc+xd);
ROTL18(x0,x5,xa,xf,x2+x3,x4+x7,x8+x9,xd+xe);
B[ 0] += x0; B[ 1] += x1; B[ 2] += x2; B[ 3] += x3; B[ 4] += x4; B[ 5] += x5; B[ 6] += x6; B[ 7] += x7;
B[ 8] += x8; B[ 9] += x9; B[10] += xa; B[11] += xb; B[12] += xc; B[13] += xd; B[14] += xe; B[15] += xf;
}
static __host__ __device__ uint2& operator^=(uint2& left, const uint2& right)
{
left.x ^= right.x;
left.y ^= right.y;
return left;
}
////////////////////////////////////////////////////////////////////////////////
//! Scrypt core kernel with higher shared memory use (faster on older devices)
//! @param g_idata input data in global memory
//! @param g_odata output data in global memory
////////////////////////////////////////////////////////////////////////////////
template <int WARPS_PER_BLOCK> __global__ void
legacy_scrypt_core_kernelA(uint32_t *g_idata, unsigned int N)
{
__shared__ uint32_t X[WARPS_PER_BLOCK][WU_PER_WARP][32+1+_64BIT_ALIGN]; // +1 to resolve bank conflicts
volatile int warpIdx = threadIdx.x / warpSize;
volatile int warpThread = threadIdx.x % warpSize;
// variables supporting the large memory transaction magic
unsigned int Y = warpThread/16;
unsigned int Z = 2*(warpThread%16);
// add block specific offsets
int offset = blockIdx.x * WU_PER_BLOCK + warpIdx * WU_PER_WARP;
g_idata += 32 * offset;
uint32_t * V = c_V[offset / WU_PER_WARP] + SCRATCH*Y + Z;
uint32_t ((*XB)[32+1+_64BIT_ALIGN]) = (uint32_t (*)[32+1+_64BIT_ALIGN])&X[warpIdx][Y][Z];
uint32_t *XX = X[warpIdx][warpThread];
{
#pragma unroll 16
for (int wu=0; wu < 32; wu+=2)
*((uint2*)XB[wu]) = *((uint2*)(&g_idata[32*(wu+Y)+Z]));
#pragma unroll 16
for (int wu=0; wu < 32; wu+=2)
*((uint2*)(&V[SCRATCH*wu])) = *((uint2*)XB[wu]);
for (int i = 1; i < 1024; i++)
{
xor_salsa8(&XX[0], &XX[16]);
xor_salsa8(&XX[16], &XX[0]);
#pragma unroll 16
for (int wu=0; wu < 32; wu+=2)
*((uint2*)(&V[SCRATCH*wu + i*32])) = *((uint2*)XB[wu]);
}
}
}
template <int WARPS_PER_BLOCK> __global__ void
legacy_scrypt_core_kernelB(uint32_t *g_odata, unsigned int N)
{
__shared__ uint32_t X[WARPS_PER_BLOCK][WU_PER_WARP][32+1+_64BIT_ALIGN]; // +1 to resolve bank conflicts
volatile int warpIdx = threadIdx.x / warpSize;
volatile int warpThread = threadIdx.x % warpSize;
// variables supporting the large memory transaction magic
unsigned int Y = warpThread/16;
unsigned int Z = 2*(warpThread%16);
// add block specific offsets
int offset = blockIdx.x * WU_PER_BLOCK + warpIdx * WU_PER_WARP;
g_odata += 32 * offset;
uint32_t * V = c_V[offset / WU_PER_WARP] + SCRATCH*Y + Z;
uint32_t ((*XB)[32+1+_64BIT_ALIGN]) = (uint32_t (*)[32+1+_64BIT_ALIGN])&X[warpIdx][Y][Z];
uint32_t *XX = X[warpIdx][warpThread];
{
#pragma unroll 16
for (int wu=0; wu < 32; wu+=2)
*((uint2*)XB[wu]) = *((uint2*)(&V[SCRATCH*wu + 1023*32]));
xor_salsa8(&XX[0], &XX[16]);
xor_salsa8(&XX[16], &XX[0]);
for (int i = 0; i < 1024; i++)
{
#pragma unroll 16
for (int wu=0; wu < 32; wu+=2)
*((uint2*)XB[wu]) ^= *((uint2*)(&V[SCRATCH*wu + 32*(X[warpIdx][wu+Y][16] & 1023)]));
xor_salsa8(&XX[0], &XX[16]);
xor_salsa8(&XX[16], &XX[0]);
}
#pragma unroll 16
for (int wu=0; wu < 32; wu+=2)
*((uint2*)(&g_odata[32*(wu+Y)+Z])) = *((uint2*)XB[wu]);
}
}
template <int WARPS_PER_BLOCK, int TEX_DIM> __global__ void
legacy_scrypt_core_kernelB_tex(uint32_t *g_odata, unsigned int N)
{
__shared__ uint32_t X[WARPS_PER_BLOCK][WU_PER_WARP][32+1+_64BIT_ALIGN]; // +1 to resolve bank conflicts
volatile int warpIdx = threadIdx.x / warpSize;
volatile int warpThread = threadIdx.x % warpSize;
// variables supporting the large memory transaction magic
unsigned int Y = warpThread/16;
unsigned int Z = 2*(warpThread%16);
// add block specific offsets
int offset = blockIdx.x * WU_PER_BLOCK + warpIdx * WU_PER_WARP;
g_odata += 32 * offset;
uint32_t ((*XB)[32+1+_64BIT_ALIGN]) = (uint32_t (*)[32+1+_64BIT_ALIGN])&X[warpIdx][Y][Z];
uint32_t *XX = X[warpIdx][warpThread];
{
#pragma unroll 16
for (int wu=0; wu < 32; wu+=2)
*((uint2*)XB[wu]) = ((TEX_DIM == 1) ?
tex1Dfetch(texRef1D_2_V, (SCRATCH*(offset+wu+Y) + 1023*32 + Z)/2) :
tex2D(texRef2D_2_V, 0.5f + (32*1023 + Z)/2, 0.5f + (offset+wu+Y)));
xor_salsa8(&XX[0], &XX[16]);
xor_salsa8(&XX[16], &XX[0]);
for (int i = 0; i < 1024; i++)
{
#pragma unroll 16
for (int wu=0; wu < 32; wu+=2)
*((uint2*)XB[wu]) ^= ((TEX_DIM == 1) ?
tex1Dfetch(texRef1D_2_V, (SCRATCH*(offset+wu+Y) + 32*(X[warpIdx][wu+Y][16] & 1023) + Z)/2) :
tex2D(texRef2D_2_V, 0.5f + (32*(X[warpIdx][wu+Y][16] & 1023) + Z)/2, 0.5f + (offset+wu+Y)));
xor_salsa8(&XX[0], &XX[16]);
xor_salsa8(&XX[16], &XX[0]);
}
#pragma unroll 16
for (int wu=0; wu < 32; wu+=2)
*((uint2*)(&g_odata[32*(wu+Y)+Z])) = *((uint2*)XB[wu]);
}
}