forked from pooler/cpuminer
-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathfermi_kernel.cu
506 lines (427 loc) · 21 KB
/
fermi_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
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
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
//
// Kernel that runs best on Fermi devices
//
// NOTE: compile this .cu module for compute_10,sm_10 with --maxrregcount=124
//
#ifdef WIN32
#include <windows.h>
#endif
#include <stdio.h>
#include <time.h>
#include <sys/time.h>
#include <unistd.h>
#include <cuda.h>
#include "fermi_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
// forward references
template <int WARPS_PER_BLOCK> __global__ void scrypt_core_kernelA(uint32_t *g_idata);
template <int WARPS_PER_BLOCK> __global__ void scrypt_core_kernelB(uint32_t *g_odata);
template <int WARPS_PER_BLOCK, int TEX_DIM> __global__ void scrypt_core_kernelB_tex(uint32_t *g_odata);
// scratchbuf constants (pointers to scratch buffer for each work unit)
__constant__ uint32_t* c_V[1024];
// using texture references for the "tex" variants of the B kernels
texture<uint4, 1, cudaReadModeElementType> texRef1D_4_V;
texture<uint4, 2, cudaReadModeElementType> texRef2D_4_V;
FermiKernel::FermiKernel() : KernelInterface()
{
}
bool FermiKernel::bindtexture_1D(uint32_t *d_V, size_t size)
{
cudaChannelFormatDesc channelDesc4 = cudaCreateChannelDesc<uint4>();
texRef1D_4_V.normalized = 0;
texRef1D_4_V.filterMode = cudaFilterModePoint;
texRef1D_4_V.addressMode[0] = cudaAddressModeClamp;
checkCudaErrors(cudaBindTexture(NULL, &texRef1D_4_V, d_V, &channelDesc4, size));
return true;
}
bool FermiKernel::bindtexture_2D(uint32_t *d_V, int width, int height, size_t pitch)
{
cudaChannelFormatDesc channelDesc4 = cudaCreateChannelDesc<uint4>();
texRef2D_4_V.normalized = 0;
texRef2D_4_V.filterMode = cudaFilterModePoint;
texRef2D_4_V.addressMode[0] = cudaAddressModeClamp;
texRef2D_4_V.addressMode[1] = cudaAddressModeClamp;
checkCudaErrors(cudaBindTexture2D(NULL, &texRef2D_4_V, d_V, &channelDesc4, width, height, pitch));
return true;
}
bool FermiKernel::unbindtexture_1D()
{
checkCudaErrors(cudaUnbindTexture(texRef1D_4_V));
return true;
}
bool FermiKernel::unbindtexture_2D()
{
checkCudaErrors(cudaUnbindTexture(texRef2D_4_V));
return true;
}
void FermiKernel::set_scratchbuf_constants(int MAXWARPS, uint32_t** h_V)
{
checkCudaErrors(cudaMemcpyToSymbol(c_V, h_V, MAXWARPS*sizeof(uint32_t*), 0, cudaMemcpyHostToDevice));
}
bool FermiKernel::run_kernel(dim3 grid, dim3 threads, int WARPS_PER_BLOCK, int thr_id, cudaStream_t stream, uint32_t* d_idata, uint32_t* d_odata, int *mutex, bool interactive, bool benchmark, int texture_cache)
{
bool success = true;
// clear CUDA's error variable
cudaGetLastError();
// First phase: Sequential writes to scratchpad.
switch (WARPS_PER_BLOCK) {
case 1: scrypt_core_kernelA<1><<< grid, threads, 0, stream >>>(d_idata); break;
case 2: scrypt_core_kernelA<2><<< grid, threads, 0, stream >>>(d_idata); break;
case 3: scrypt_core_kernelA<3><<< grid, threads, 0, stream >>>(d_idata); break;
case 4: scrypt_core_kernelA<4><<< grid, threads, 0, stream >>>(d_idata); break;
case 5: scrypt_core_kernelA<5><<< grid, threads, 0, stream >>>(d_idata); break;
case 6: scrypt_core_kernelA<6><<< grid, threads, 0, stream >>>(d_idata); break;
case 7: scrypt_core_kernelA<7><<< grid, threads, 0, stream >>>(d_idata); break;
case 8: scrypt_core_kernelA<8><<< grid, threads, 0, stream >>>(d_idata); break;
case 9: scrypt_core_kernelA<9><<< grid, threads, 0, stream >>>(d_idata); break;
case 10: scrypt_core_kernelA<10><<< grid, threads, 0, stream >>>(d_idata); break;
case 11: scrypt_core_kernelA<11><<< grid, threads, 0, stream >>>(d_idata); break;
case 12: scrypt_core_kernelA<12><<< grid, threads, 0, stream >>>(d_idata); break;
case 13: scrypt_core_kernelA<13><<< grid, threads, 0, stream >>>(d_idata); break;
case 14: scrypt_core_kernelA<14><<< grid, threads, 0, stream >>>(d_idata); break;
case 15: scrypt_core_kernelA<15><<< grid, threads, 0, stream >>>(d_idata); break;
case 16: scrypt_core_kernelA<16><<< grid, threads, 0, stream >>>(d_idata); break;
default: success = false; break;
}
// Optional millisecond sleep in between kernels
if (!benchmark && interactive) {
checkCudaErrors(MyStreamSynchronize(stream, 1, thr_id));
#ifdef WIN32
Sleep(1);
#else
usleep(1000);
#endif
}
// Second phase: Random read access from scratchpad.
if (texture_cache)
{
if (texture_cache == 1)
{
switch (WARPS_PER_BLOCK) {
case 1: scrypt_core_kernelB_tex<1,1><<< grid, threads, 0, stream >>>(d_odata); break;
case 2: scrypt_core_kernelB_tex<2,1><<< grid, threads, 0, stream >>>(d_odata); break;
case 3: scrypt_core_kernelB_tex<3,1><<< grid, threads, 0, stream >>>(d_odata); break;
case 4: scrypt_core_kernelB_tex<4,1><<< grid, threads, 0, stream >>>(d_odata); break;
case 5: scrypt_core_kernelB_tex<5,1><<< grid, threads, 0, stream >>>(d_odata); break;
case 6: scrypt_core_kernelB_tex<6,1><<< grid, threads, 0, stream >>>(d_odata); break;
case 7: scrypt_core_kernelB_tex<7,1><<< grid, threads, 0, stream >>>(d_odata); break;
case 8: scrypt_core_kernelB_tex<8,1><<< grid, threads, 0, stream >>>(d_odata); break;
case 9: scrypt_core_kernelB_tex<9,1><<< grid, threads, 0, stream >>>(d_odata); break;
case 10: scrypt_core_kernelB_tex<10,1><<< grid, threads, 0, stream >>>(d_odata); break;
case 11: scrypt_core_kernelB_tex<11,1><<< grid, threads, 0, stream >>>(d_odata); break;
case 12: scrypt_core_kernelB_tex<12,1><<< grid, threads, 0, stream >>>(d_odata); break;
case 13: scrypt_core_kernelB_tex<13,1><<< grid, threads, 0, stream >>>(d_odata); break;
case 14: scrypt_core_kernelB_tex<14,1><<< grid, threads, 0, stream >>>(d_odata); break;
case 15: scrypt_core_kernelB_tex<15,1><<< grid, threads, 0, stream >>>(d_odata); break;
case 16: scrypt_core_kernelB_tex<16,1><<< grid, threads, 0, stream >>>(d_odata); break;
default: success = false; break;
}
}
else if (texture_cache == 2)
{
switch (WARPS_PER_BLOCK) {
case 1: scrypt_core_kernelB_tex<1,2><<< grid, threads, 0, stream >>>(d_odata); break;
case 2: scrypt_core_kernelB_tex<2,2><<< grid, threads, 0, stream >>>(d_odata); break;
case 3: scrypt_core_kernelB_tex<3,2><<< grid, threads, 0, stream >>>(d_odata); break;
case 4: scrypt_core_kernelB_tex<4,2><<< grid, threads, 0, stream >>>(d_odata); break;
case 5: scrypt_core_kernelB_tex<5,2><<< grid, threads, 0, stream >>>(d_odata); break;
case 6: scrypt_core_kernelB_tex<6,2><<< grid, threads, 0, stream >>>(d_odata); break;
case 7: scrypt_core_kernelB_tex<7,2><<< grid, threads, 0, stream >>>(d_odata); break;
case 8: scrypt_core_kernelB_tex<8,2><<< grid, threads, 0, stream >>>(d_odata); break;
case 9: scrypt_core_kernelB_tex<9,2><<< grid, threads, 0, stream >>>(d_odata); break;
case 10: scrypt_core_kernelB_tex<10,2><<< grid, threads, 0, stream >>>(d_odata); break;
case 11: scrypt_core_kernelB_tex<11,2><<< grid, threads, 0, stream >>>(d_odata); break;
case 12: scrypt_core_kernelB_tex<12,2><<< grid, threads, 0, stream >>>(d_odata); break;
case 13: scrypt_core_kernelB_tex<13,2><<< grid, threads, 0, stream >>>(d_odata); break;
case 14: scrypt_core_kernelB_tex<14,2><<< grid, threads, 0, stream >>>(d_odata); break;
case 15: scrypt_core_kernelB_tex<15,2><<< grid, threads, 0, stream >>>(d_odata); break;
case 16: scrypt_core_kernelB_tex<16,2><<< grid, threads, 0, stream >>>(d_odata); break;
default: success = false; break;
}
} else success = false;
}
else
{
switch (WARPS_PER_BLOCK) {
case 1: scrypt_core_kernelB<1><<< grid, threads, 0, stream >>>(d_odata); break;
case 2: scrypt_core_kernelB<2><<< grid, threads, 0, stream >>>(d_odata); break;
case 3: scrypt_core_kernelB<3><<< grid, threads, 0, stream >>>(d_odata); break;
case 4: scrypt_core_kernelB<4><<< grid, threads, 0, stream >>>(d_odata); break;
case 5: scrypt_core_kernelB<5><<< grid, threads, 0, stream >>>(d_odata); break;
case 6: scrypt_core_kernelB<6><<< grid, threads, 0, stream >>>(d_odata); break;
case 7: scrypt_core_kernelB<7><<< grid, threads, 0, stream >>>(d_odata); break;
case 8: scrypt_core_kernelB<8><<< grid, threads, 0, stream >>>(d_odata); break;
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 __device__ void xor_salsa8(uint32_t *B,uint32_t *C)
{
uint32_t x[16];
x[0]=(B[0] ^= C[0]);
x[1]=(B[1] ^= C[1]);
x[2]=(B[2] ^= C[2]);
x[3]=(B[3] ^= C[3]);
x[4]=(B[4] ^= C[4]);
x[5]=(B[5] ^= C[5]);
x[6]=(B[6] ^= C[6]);
x[7]=(B[7] ^= C[7]);
x[8]=(B[8] ^= C[8]);
x[9]=(B[9] ^= C[9]);
x[10]=(B[10] ^= C[10]);
x[11]=(B[11] ^= C[11]);
x[12]=(B[12] ^= C[12]);
x[13]=(B[13] ^= C[13]);
x[14]=(B[14] ^= C[14]);
x[15]=(B[15] ^= C[15]);
/* Operate on columns. */
ROTL7(x[4],x[9],x[14],x[3],x[0]+x[12],x[1]+x[5],x[6]+x[10],x[11]+x[15]);
ROTL9(x[8],x[13],x[2],x[7],x[0]+x[4],x[5]+x[9],x[10]+x[14],x[3]+x[15]);
ROTL13(x[12],x[1],x[6],x[11],x[4]+x[8],x[9]+x[13],x[2]+x[14],x[3]+x[7]);
ROTL18(x[0],x[5],x[10],x[15],x[8]+x[12],x[1]+x[13],x[2]+x[6],x[7]+x[11]);
/* Operate on rows. */
ROTL7(x[1],x[6],x[11],x[12],x[0]+x[3],x[4]+x[5],x[9]+x[10],x[14]+x[15]);
ROTL9(x[2],x[7],x[8],x[13],x[0]+x[1],x[5]+x[6],x[10]+x[11],x[12]+x[15]);
ROTL13(x[3],x[4],x[9],x[14],x[1]+x[2],x[6]+x[7],x[8]+x[11],x[12]+x[13]);
ROTL18(x[0],x[5],x[10],x[15],x[2]+x[3],x[4]+x[7],x[8]+x[9],x[13]+x[14]);
/* Operate on columns. */
ROTL7(x[4],x[9],x[14],x[3],x[0]+x[12],x[1]+x[5],x[6]+x[10],x[11]+x[15]);
ROTL9(x[8],x[13],x[2],x[7],x[0]+x[4],x[5]+x[9],x[10]+x[14],x[3]+x[15]);
ROTL13(x[12],x[1],x[6],x[11],x[4]+x[8],x[9]+x[13],x[2]+x[14],x[3]+x[7]);
ROTL18(x[0],x[5],x[10],x[15],x[8]+x[12],x[1]+x[13],x[2]+x[6],x[7]+x[11]);
/* Operate on rows. */
ROTL7(x[1],x[6],x[11],x[12],x[0]+x[3],x[4]+x[5],x[9]+x[10],x[14]+x[15]);
ROTL9(x[2],x[7],x[8],x[13],x[0]+x[1],x[5]+x[6],x[10]+x[11],x[12]+x[15]);
ROTL13(x[3],x[4],x[9],x[14],x[1]+x[2],x[6]+x[7],x[8]+x[11],x[12]+x[13]);
ROTL18(x[0],x[5],x[10],x[15],x[2]+x[3],x[4]+x[7],x[8]+x[9],x[13]+x[14]);
/* Operate on columns. */
ROTL7(x[4],x[9],x[14],x[3],x[0]+x[12],x[1]+x[5],x[6]+x[10],x[11]+x[15]);
ROTL9(x[8],x[13],x[2],x[7],x[0]+x[4],x[5]+x[9],x[10]+x[14],x[3]+x[15]);
ROTL13(x[12],x[1],x[6],x[11],x[4]+x[8],x[9]+x[13],x[2]+x[14],x[3]+x[7]);
ROTL18(x[0],x[5],x[10],x[15],x[8]+x[12],x[1]+x[13],x[2]+x[6],x[7]+x[11]);
/* Operate on rows. */
ROTL7(x[1],x[6],x[11],x[12],x[0]+x[3],x[4]+x[5],x[9]+x[10],x[14]+x[15]);
ROTL9(x[2],x[7],x[8],x[13],x[0]+x[1],x[5]+x[6],x[10]+x[11],x[12]+x[15]);
ROTL13(x[3],x[4],x[9],x[14],x[1]+x[2],x[6]+x[7],x[8]+x[11],x[12]+x[13]);
ROTL18(x[0],x[5],x[10],x[15],x[2]+x[3],x[4]+x[7],x[8]+x[9],x[13]+x[14]);
/* Operate on columns. */
ROTL7(x[4],x[9],x[14],x[3],x[0]+x[12],x[1]+x[5],x[6]+x[10],x[11]+x[15]);
ROTL9(x[8],x[13],x[2],x[7],x[0]+x[4],x[5]+x[9],x[10]+x[14],x[3]+x[15]);
ROTL13(x[12],x[1],x[6],x[11],x[4]+x[8],x[9]+x[13],x[2]+x[14],x[3]+x[7]);
ROTL18(x[0],x[5],x[10],x[15],x[8]+x[12],x[1]+x[13],x[2]+x[6],x[7]+x[11]);
/* Operate on rows. */
ROTL7(x[1],x[6],x[11],x[12],x[0]+x[3],x[4]+x[5],x[9]+x[10],x[14]+x[15]);
ROTL9(x[2],x[7],x[8],x[13],x[0]+x[1],x[5]+x[6],x[10]+x[11],x[12]+x[15]);
ROTL13(x[3],x[4],x[9],x[14],x[1]+x[2],x[6]+x[7],x[8]+x[11],x[12]+x[13]);
ROTL18(x[0],x[5],x[10],x[15],x[2]+x[3],x[4]+x[7],x[8]+x[9],x[13]+x[14]);
B[ 0] += x[0]; B[ 1] += x[1]; B[ 2] += x[2]; B[ 3] += x[3]; B[ 4] += x[4]; B[ 5] += x[5]; B[ 6] += x[6]; B[ 7] += x[7];
B[ 8] += x[8]; B[ 9] += x[9]; B[10] += x[10]; B[11] += x[11]; B[12] += x[12]; B[13] += x[13]; B[14] += x[14]; B[15] += x[15];
}
static __device__ uint4& operator^=(uint4& left, const uint4& right)
{
left.x ^= right.x;
left.y ^= right.y;
left.z ^= right.z;
left.w ^= right.w;
return left;
}
////////////////////////////////////////////////////////////////////////////////
//! Scrypt core kernel
//! @param g_idata input data in global memory
//! @param g_odata output data in global memory
////////////////////////////////////////////////////////////////////////////////
template <int WARPS_PER_BLOCK> __global__ void
scrypt_core_kernelA(uint32_t *g_idata)
{
__shared__ uint32_t X[WARPS_PER_BLOCK][WU_PER_WARP][16+1+_64BIT_ALIGN]; // +1 to resolve bank conflicts
volatile int warpIdx = threadIdx.x / warpSize;
volatile int warpThread = threadIdx.x % warpSize;
// add block specific offsets
volatile int offset = blockIdx.x * WU_PER_BLOCK + warpIdx * WU_PER_WARP;
g_idata += 32 * offset;
uint32_t * volatile V = c_V[offset/WU_PER_WARP];
// variables supporting the large memory transaction magic
volatile unsigned int Y = warpThread/4;
volatile unsigned int Z = 4*(warpThread%4);
// registers to store an entire work unit
uint32_t B[16], C[16];
#pragma unroll 4
for (int wu=0; wu < 32; wu+=8)
*((uint4*)(&V[SCRATCH*(wu+Y)+Z])) = *((uint4*)(&X[warpIdx][wu+Y][Z])) = *((uint4*)(&g_idata[32*(wu+Y)+Z]));
#pragma unroll 16
for (int idx=0; idx < 16; idx++) B[idx] = X[warpIdx][warpThread][idx];
#pragma unroll 4
for (int wu=0; wu < 32; wu+=8)
*((uint4*)(&V[SCRATCH*(wu+Y)+16+Z])) = *((uint4*)(&X[warpIdx][wu+Y][Z])) = *((uint4*)(&g_idata[32*(wu+Y)+16+Z]));
#pragma unroll 16
for (int idx=0; idx < 16; idx++) C[idx] = X[warpIdx][warpThread][idx];
for (int i = 1; i < 1024; i++) {
xor_salsa8(B, C); xor_salsa8(C, B);
#pragma unroll 16
for (int idx=0; idx < 16; ++idx) X[warpIdx][warpThread][idx] = B[idx];
#pragma unroll 4
for (int wu=0; wu < 32; wu+=8)
*((uint4*)(&V[SCRATCH*(wu+Y) + i*32 + Z])) = *((uint4*)(&X[warpIdx][wu+Y][Z]));
#pragma unroll 16
for (int idx=0; idx < 16; ++idx) X[warpIdx][warpThread][idx] = C[idx];
#pragma unroll 4
for (int wu=0; wu < 32; wu+=8)
*((uint4*)(&V[SCRATCH*(wu+Y) + i*32 + 16 + Z])) = *((uint4*)(&X[warpIdx][wu+Y][Z]));
}
}
template <int WARPS_PER_BLOCK> __global__ void
scrypt_core_kernelB(uint32_t *g_odata)
{
__shared__ uint32_t X[WARPS_PER_BLOCK][WU_PER_WARP][16+1+_64BIT_ALIGN]; // +1 to resolve bank conflicts
volatile int warpIdx = threadIdx.x / warpSize;
volatile int warpThread = threadIdx.x % warpSize;
// add block specific offsets
volatile int offset = blockIdx.x * WU_PER_BLOCK + warpIdx * WU_PER_WARP;
g_odata += 32 * offset;
uint32_t * volatile V = c_V[offset/WU_PER_WARP];
// variables supporting the large memory transaction magic
volatile unsigned int Y = warpThread/4;
volatile unsigned int Z = 4*(warpThread%4);
// registers to store an entire work unit
uint32_t B[16], C[16];
#pragma unroll 4
for (int wu=0; wu < 32; wu+=8)
*((uint4*)(&X[warpIdx][wu+Y][Z])) = *((uint4*)(&V[SCRATCH*(wu+Y) + 1023*32 + Z]));
#pragma unroll 16
for (int idx=0; idx < 16; idx++) B[idx] = X[warpIdx][warpThread][idx];
#pragma unroll 4
for (int wu=0; wu < 32; wu+=8)
*((uint4*)(&X[warpIdx][wu+Y][Z])) = *((uint4*)(&V[SCRATCH*(wu+Y) + 1023*32 + 16+Z]));
#pragma unroll 16
for (int idx=0; idx < 16; idx++) C[idx] = X[warpIdx][warpThread][idx];
xor_salsa8(B, C); xor_salsa8(C, B);
for (int i = 0; i < 1024; i++) {
X[warpIdx][warpThread][16] = C[0];
#pragma unroll 16
for (int idx=0; idx < 16; ++idx) X[warpIdx][warpThread][idx] = B[idx];
#pragma unroll 4
for (int wu=0; wu < 32; wu+=8)
*((uint4*)(&X[warpIdx][wu+Y][Z])) ^= *((uint4*)(&V[SCRATCH*(wu+Y) + 32*(X[warpIdx][wu+Y][16] & 1023) + Z]));
#pragma unroll 16
for (int idx=0; idx < 16; idx++) B[idx] = X[warpIdx][warpThread][idx];
#pragma unroll 16
for (int idx=0; idx < 16; ++idx) X[warpIdx][warpThread][idx] = C[idx];
#pragma unroll 4
for (int wu=0; wu < 32; wu+=8)
*((uint4*)(&X[warpIdx][wu+Y][Z])) ^= *((uint4*)(&V[SCRATCH*(wu+Y) + 32*(X[warpIdx][wu+Y][16] & 1023) + 16 + Z]));
#pragma unroll 16
for (int idx=0; idx < 16; idx++) C[idx] = X[warpIdx][warpThread][idx];
xor_salsa8(B, C); xor_salsa8(C, B);
}
#pragma unroll 16
for (int idx=0; idx < 16; ++idx) X[warpIdx][warpThread][idx] = B[idx];
#pragma unroll 4
for (int wu=0; wu < 32; wu+=8)
*((uint4*)(&g_odata[32*(wu+Y)+Z])) = *((uint4*)(&X[warpIdx][wu+Y][Z]));
#pragma unroll 16
for (int idx=0; idx < 16; ++idx) X[warpIdx][warpThread][idx] = C[idx];
#pragma unroll 4
for (int wu=0; wu < 32; wu+=8)
*((uint4*)(&g_odata[32*(wu+Y)+16+Z])) = *((uint4*)(&X[warpIdx][wu+Y][Z]));
}
template <int WARPS_PER_BLOCK, int TEX_DIM> __global__ void
scrypt_core_kernelB_tex(uint32_t *g_odata)
{
__shared__ uint32_t X[WARPS_PER_BLOCK][WU_PER_WARP][16+1+_64BIT_ALIGN]; // +1 to resolve bank conflicts
volatile int warpIdx = threadIdx.x / warpSize;
volatile int warpThread = threadIdx.x % warpSize;
// add block specific offsets
volatile int offset = blockIdx.x * WU_PER_BLOCK + warpIdx * WU_PER_WARP;
g_odata += 32 * offset;
// variables supporting the large memory transaction magic
volatile unsigned int Y = warpThread/4;
volatile unsigned int Z = 4*(warpThread%4);
// registers to store an entire work unit
uint32_t B[16], C[16];
#pragma unroll 4
for (int wu=0; wu < 32; wu+=8)
*((uint4*)(&X[warpIdx][wu+Y][Z])) = ((TEX_DIM == 1) ?
tex1Dfetch(texRef1D_4_V, (SCRATCH*(offset+wu+Y) + 1023*32 + Z)/4) :
tex2D(texRef2D_4_V, 0.5f + (32*1023 + Z)/4, 0.5f + (offset+wu+Y)));
#pragma unroll 16
for (int idx=0; idx < 16; idx++) B[idx] = X[warpIdx][warpThread][idx];
#pragma unroll 4
for (int wu=0; wu < 32; wu+=8)
*((uint4*)(&X[warpIdx][wu+Y][Z])) = ((TEX_DIM == 1) ?
tex1Dfetch(texRef1D_4_V, (SCRATCH*(offset+wu+Y) + 1023*32 + 16+Z)/4) :
tex2D(texRef2D_4_V, 0.5f + (32*1023 + 16+Z)/4, 0.5f + (offset+wu+Y)));
#pragma unroll 16
for (int idx=0; idx < 16; idx++) C[idx] = X[warpIdx][warpThread][idx];
xor_salsa8(B, C); xor_salsa8(C, B);
for (int i = 0; i < 1024; i++) {
X[warpIdx][warpThread][16] = C[0];
#pragma unroll 16
for (int idx=0; idx < 16; ++idx) X[warpIdx][warpThread][idx] = B[idx];
#pragma unroll 4
for (int wu=0; wu < 32; wu+=8)
*((uint4*)(&X[warpIdx][wu+Y][Z])) ^= ((TEX_DIM == 1) ?
tex1Dfetch(texRef1D_4_V, (SCRATCH*(offset+wu+Y) + 32*(X[warpIdx][wu+Y][16] & 1023) + Z)/4) :
tex2D(texRef2D_4_V, 0.5f + (32*(X[warpIdx][wu+Y][16] & 1023) + Z)/4, 0.5f + (offset+wu+Y)));
#pragma unroll 16
for (int idx=0; idx < 16; idx++) B[idx] = X[warpIdx][warpThread][idx];
#pragma unroll 16
for (int idx=0; idx < 16; ++idx) X[warpIdx][warpThread][idx] = C[idx];
#pragma unroll 4
for (int wu=0; wu < 32; wu+=8)
*((uint4*)(&X[warpIdx][wu+Y][Z])) ^= ((TEX_DIM == 1) ?
tex1Dfetch(texRef1D_4_V, (SCRATCH*(offset+wu+Y) + 32*(X[warpIdx][wu+Y][16] & 1023) + 16+Z)/4) :
tex2D(texRef2D_4_V, 0.5f + (32*(X[warpIdx][wu+Y][16] & 1023) + 16+Z)/4, 0.5f + (offset+wu+Y)));
#pragma unroll 16
for (int idx=0; idx < 16; idx++) C[idx] = X[warpIdx][warpThread][idx];
xor_salsa8(B, C); xor_salsa8(C, B);
}
#pragma unroll 16
for (int idx=0; idx < 16; ++idx) X[warpIdx][warpThread][idx] = B[idx];
#pragma unroll 4
for (int wu=0; wu < 32; wu+=8)
*((uint4*)(&g_odata[32*(wu+Y)+Z])) = *((uint4*)(&X[warpIdx][wu+Y][Z]));
#pragma unroll 16
for (int idx=0; idx < 16; ++idx) X[warpIdx][warpThread][idx] = C[idx];
#pragma unroll 4
for (int wu=0; wu < 32; wu+=8)
*((uint4*)(&g_odata[32*(wu+Y)+16+Z])) = *((uint4*)(&X[warpIdx][wu+Y][Z]));
}