This repository has been archived by the owner on Jan 9, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 582
/
equi_miner.cu
2159 lines (1769 loc) · 58.3 KB
/
equi_miner.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
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*
Equihash solver created by djeZo (l33tsoftw@gmail.com) for NiceHash
Based on CUDA solver by John Tromp released under MIT license.
Some helper functions taken out of OpenCL solver by Marc Bevand
released under MIT license.
cuda_djezo solver is released by NiceHash (www.nicehash.com) under
GPL 3.0 license. If you don't have a copy, you can obtain one from
https://www.gnu.org/licenses/gpl-3.0.txt
*/
/*
The MIT License (MIT)
Copyright (c) 2016 John Tromp
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software, and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
*/
/*
The MIT License (MIT)
Copyright (c) 2016 Marc Bevand
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software, and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
*/
#ifdef WIN32
#include <Windows.h>
#endif
#include <stdint.h>
#include <string.h>
#include <stdlib.h>
#include <stdbool.h>
#include <stdio.h>
#include <assert.h>
#include <functional>
#include <vector>
#include <iostream>
#include <mutex>
#include "eqcuda.hpp"
#include "sm_32_intrinsics.h"
#define WN 200
#define WK 9
#define NDIGITS (WK+1)
#define DIGITBITS (WN/(NDIGITS))
#define PROOFSIZE (1<<WK)
#define BASE (1<<DIGITBITS)
#define NHASHES (2*BASE)
#define HASHESPERBLAKE (512/WN)
#define HASHOUT (HASHESPERBLAKE*WN/8)
#define NBLOCKS ((NHASHES + HASHESPERBLAKE - 1) / HASHESPERBLAKE)
#define BUCKBITS (DIGITBITS - RB)
#define NBUCKETS (1 << BUCKBITS)
#define BUCKMASK (NBUCKETS - 1)
#define SLOTBITS (RB + 2)
#define SLOTRANGE (1 << SLOTBITS)
#define NSLOTS SM
#define SLOTMASK (SLOTRANGE - 1)
#define NRESTS (1 << RB)
#define RESTMASK (NRESTS - 1)
#define CANTORBITS (2 * SLOTBITS - 2)
#define CANTORMASK ((1 << CANTORBITS) - 1)
#define CANTORMAXSQRT (2 * NSLOTS)
#define RB8_NSLOTS 640
#define RB8_NSLOTS_LD 624
#define FD_THREADS 128
// reduce vstudio warnings (__byteperm, blockIdx...)
#ifdef __INTELLISENSE__
#include <device_functions.h>
#include <device_launch_parameters.h>
#define __launch_bounds__(max_tpb, min_blocks)
#define __CUDA_ARCH__ 520
uint32_t __byte_perm(uint32_t x, uint32_t y, uint32_t z);
uint32_t __byte_perm(uint32_t x, uint32_t y, uint32_t z);
uint32_t __shfl(uint32_t x, uint32_t y, uint32_t z);
uint32_t atomicExch(uint32_t *x, uint32_t y);
uint32_t atomicAdd(uint32_t *x, uint32_t y);
void __syncthreads(void);
void __threadfence(void);
void __threadfence_block(void);
uint32_t __ldg(const uint32_t* address);
uint64_t __ldg(const uint64_t* address);
uint4 __ldca(const uint4 *ptr);
u32 __ldca(const u32 *ptr);
u32 umin(const u32, const u32);
u32 umax(const u32, const u32);
#endif
typedef u32 proof[PROOFSIZE];
struct __align__(32) slot
{
u32 hash[8];
};
struct __align__(16) slotsmall
{
u32 hash[4];
};
struct __align__(8) slottiny
{
u32 hash[2];
};
template <u32 RB, u32 SM>
struct equi
{
slot round0trees[4096][RB8_NSLOTS];
slot trees[1][NBUCKETS][NSLOTS];
struct
{
slotsmall treessmall[NSLOTS];
slottiny treestiny[NSLOTS];
} round2trees[NBUCKETS];
struct
{
slotsmall treessmall[NSLOTS];
slottiny treestiny[NSLOTS];
} round3trees[NBUCKETS];
slotsmall treessmall[4][NBUCKETS][NSLOTS];
slottiny treestiny[1][4096][RB8_NSLOTS_LD];
u32 round4bidandsids[NBUCKETS][NSLOTS];
union
{
u64 blake_h[8];
u32 blake_h32[16];
};
struct
{
u32 nslots8[4096];
u32 nslots0[4096];
u32 nslots[9][NBUCKETS];
scontainerreal srealcont;
} edata;
};
__device__ __constant__ const u64 blake_iv[] =
{
0x6a09e667f3bcc908, 0xbb67ae8584caa73b,
0x3c6ef372fe94f82b, 0xa54ff53a5f1d36f1,
0x510e527fade682d1, 0x9b05688c2b3e6c1f,
0x1f83d9abfb41bd6b, 0x5be0cd19137e2179,
};
__device__ __forceinline__ uint2 operator^ (uint2 a, uint2 b)
{
return make_uint2(a.x ^ b.x, a.y ^ b.y);
}
__device__ __forceinline__ uint4 operator^ (uint4 a, uint4 b)
{
return make_uint4(a.x ^ b.x, a.y ^ b.y, a.z ^ b.z, a.w ^ b.w);
}
__device__ __forceinline__ uint2 ROR2(const uint2 a, const int offset)
{
uint2 result;
{
asm("shf.r.wrap.b32 %0, %1, %2, %3;" : "=r"(result.x) : "r"(a.y), "r"(a.x), "r"(offset));
asm("shf.r.wrap.b32 %0, %1, %2, %3;" : "=r"(result.y) : "r"(a.x), "r"(a.y), "r"(offset));
}
return result;
}
__device__ __forceinline__ uint2 SWAPUINT2(uint2 value)
{
return make_uint2(value.y, value.x);
}
__device__ __forceinline__ uint2 ROR24(const uint2 a)
{
uint2 result;
result.x = __byte_perm(a.y, a.x, 0x2107);
result.y = __byte_perm(a.y, a.x, 0x6543);
return result;
}
__device__ __forceinline__ uint2 ROR16(const uint2 a)
{
uint2 result;
result.x = __byte_perm(a.y, a.x, 0x1076);
result.y = __byte_perm(a.y, a.x, 0x5432);
return result;
}
__device__ __forceinline__ void G2(u64 & a, u64 & b, u64 & c, u64 & d, u64 x, u64 y)
{
a = a + b + x;
((uint2*)&d)[0] = SWAPUINT2(((uint2*)&d)[0] ^ ((uint2*)&a)[0]);
c = c + d;
((uint2*)&b)[0] = ROR24(((uint2*)&b)[0] ^ ((uint2*)&c)[0]);
a = a + b + y;
((uint2*)&d)[0] = ROR16(((uint2*)&d)[0] ^ ((uint2*)&a)[0]);
c = c + d;
((uint2*)&b)[0] = ROR2(((uint2*)&b)[0] ^ ((uint2*)&c)[0], 63U);
}
struct packer_default
{
__device__ __forceinline__ static u32 set_bucketid_and_slots(const u32 bucketid, const u32 s0, const u32 s1, const u32 RB, const u32 SM)
{
return (((bucketid << SLOTBITS) | s0) << SLOTBITS) | s1;
}
__device__ __forceinline__ static u32 get_bucketid(const u32 bid, const u32 RB, const u32 SM)
{
// BUCKMASK-ed to prevent illegal memory accesses in case of memory errors
return (bid >> (2 * SLOTBITS)) & BUCKMASK;
}
__device__ __forceinline__ static u32 get_slot0(const u32 bid, const u32 s1, const u32 RB, const u32 SM)
{
return bid & SLOTMASK;
}
__device__ __forceinline__ static u32 get_slot1(const u32 bid, const u32 RB, const u32 SM)
{
return (bid >> SLOTBITS) & SLOTMASK;
}
};
struct packer_cantor
{
__device__ __forceinline__ static u32 cantor(const u32 s0, const u32 s1)
{
u32 a = umax(s0, s1);
u32 b = umin(s0, s1);
return a * (a + 1) / 2 + b;
}
__device__ __forceinline__ static u32 set_bucketid_and_slots(const u32 bucketid, const u32 s0, const u32 s1, const u32 RB, const u32 SM)
{
return (bucketid << CANTORBITS) | cantor(s0, s1);
}
__device__ __forceinline__ static u32 get_bucketid(const u32 bid, const u32 RB, const u32 SM)
{
return (bid >> CANTORBITS) & BUCKMASK;
}
__device__ __forceinline__ static u32 get_slot0(const u32 bid, const u32 s1, const u32 RB, const u32 SM)
{
return ((bid & CANTORMASK) - cantor(0, s1)) & SLOTMASK;
}
__device__ __forceinline__ static u32 get_slot1(const u32 bid, const u32 RB, const u32 SM)
{
u32 k, q, sqr = 8 * (bid & CANTORMASK) + 1;
// this k=sqrt(sqr) computing loop averages 3.4 iterations out of maximum 9
for (k = CANTORMAXSQRT; (q = sqr / k) < k; k = (k + q) / 2);
return ((k - 1) / 2) & SLOTMASK;
}
};
template <u32 RB, u32 SM, typename PACKER>
__global__ void digit_first(equi<RB, SM>* eq, u32 nonce)
{
const u32 block = blockIdx.x * blockDim.x + threadIdx.x;
__shared__ u64 hash_h[8];
u32* hash_h32 = (u32*)hash_h;
if (threadIdx.x < 16)
hash_h32[threadIdx.x] = __ldca(&eq->blake_h32[threadIdx.x]);
__syncthreads();
u64 m = (u64)block << 32 | (u64)nonce;
union
{
u64 v[16];
u32 v32[32];
uint4 v128[8];
};
v[0] = hash_h[0];
v[1] = hash_h[1];
v[2] = hash_h[2];
v[3] = hash_h[3];
v[4] = hash_h[4];
v[5] = hash_h[5];
v[6] = hash_h[6];
v[7] = hash_h[7];
v[8] = blake_iv[0];
v[9] = blake_iv[1];
v[10] = blake_iv[2];
v[11] = blake_iv[3];
v[12] = blake_iv[4] ^ (128 + 16);
v[13] = blake_iv[5];
v[14] = blake_iv[6] ^ 0xffffffffffffffff;
v[15] = blake_iv[7];
// mix 1
G2(v[0], v[4], v[8], v[12], 0, m);
G2(v[1], v[5], v[9], v[13], 0, 0);
G2(v[2], v[6], v[10], v[14], 0, 0);
G2(v[3], v[7], v[11], v[15], 0, 0);
G2(v[0], v[5], v[10], v[15], 0, 0);
G2(v[1], v[6], v[11], v[12], 0, 0);
G2(v[2], v[7], v[8], v[13], 0, 0);
G2(v[3], v[4], v[9], v[14], 0, 0);
// mix 2
G2(v[0], v[4], v[8], v[12], 0, 0);
G2(v[1], v[5], v[9], v[13], 0, 0);
G2(v[2], v[6], v[10], v[14], 0, 0);
G2(v[3], v[7], v[11], v[15], 0, 0);
G2(v[0], v[5], v[10], v[15], m, 0);
G2(v[1], v[6], v[11], v[12], 0, 0);
G2(v[2], v[7], v[8], v[13], 0, 0);
G2(v[3], v[4], v[9], v[14], 0, 0);
// mix 3
G2(v[0], v[4], v[8], v[12], 0, 0);
G2(v[1], v[5], v[9], v[13], 0, 0);
G2(v[2], v[6], v[10], v[14], 0, 0);
G2(v[3], v[7], v[11], v[15], 0, 0);
G2(v[0], v[5], v[10], v[15], 0, 0);
G2(v[1], v[6], v[11], v[12], 0, 0);
G2(v[2], v[7], v[8], v[13], 0, m);
G2(v[3], v[4], v[9], v[14], 0, 0);
// mix 4
G2(v[0], v[4], v[8], v[12], 0, 0);
G2(v[1], v[5], v[9], v[13], 0, m);
G2(v[2], v[6], v[10], v[14], 0, 0);
G2(v[3], v[7], v[11], v[15], 0, 0);
G2(v[0], v[5], v[10], v[15], 0, 0);
G2(v[1], v[6], v[11], v[12], 0, 0);
G2(v[2], v[7], v[8], v[13], 0, 0);
G2(v[3], v[4], v[9], v[14], 0, 0);
// mix 5
G2(v[0], v[4], v[8], v[12], 0, 0);
G2(v[1], v[5], v[9], v[13], 0, 0);
G2(v[2], v[6], v[10], v[14], 0, 0);
G2(v[3], v[7], v[11], v[15], 0, 0);
G2(v[0], v[5], v[10], v[15], 0, m);
G2(v[1], v[6], v[11], v[12], 0, 0);
G2(v[2], v[7], v[8], v[13], 0, 0);
G2(v[3], v[4], v[9], v[14], 0, 0);
// mix 6
G2(v[0], v[4], v[8], v[12], 0, 0);
G2(v[1], v[5], v[9], v[13], 0, 0);
G2(v[2], v[6], v[10], v[14], 0, 0);
G2(v[3], v[7], v[11], v[15], 0, 0);
G2(v[0], v[5], v[10], v[15], 0, 0);
G2(v[1], v[6], v[11], v[12], 0, 0);
G2(v[2], v[7], v[8], v[13], 0, 0);
G2(v[3], v[4], v[9], v[14], m, 0);
// mix 7
G2(v[0], v[4], v[8], v[12], 0, 0);
G2(v[1], v[5], v[9], v[13], m, 0);
G2(v[2], v[6], v[10], v[14], 0, 0);
G2(v[3], v[7], v[11], v[15], 0, 0);
G2(v[0], v[5], v[10], v[15], 0, 0);
G2(v[1], v[6], v[11], v[12], 0, 0);
G2(v[2], v[7], v[8], v[13], 0, 0);
G2(v[3], v[4], v[9], v[14], 0, 0);
// mix 8
G2(v[0], v[4], v[8], v[12], 0, 0);
G2(v[1], v[5], v[9], v[13], 0, 0);
G2(v[2], v[6], v[10], v[14], 0, m);
G2(v[3], v[7], v[11], v[15], 0, 0);
G2(v[0], v[5], v[10], v[15], 0, 0);
G2(v[1], v[6], v[11], v[12], 0, 0);
G2(v[2], v[7], v[8], v[13], 0, 0);
G2(v[3], v[4], v[9], v[14], 0, 0);
// mix 9
G2(v[0], v[4], v[8], v[12], 0, 0);
G2(v[1], v[5], v[9], v[13], 0, 0);
G2(v[2], v[6], v[10], v[14], 0, 0);
G2(v[3], v[7], v[11], v[15], 0, 0);
G2(v[0], v[5], v[10], v[15], 0, 0);
G2(v[1], v[6], v[11], v[12], 0, 0);
G2(v[2], v[7], v[8], v[13], m, 0);
G2(v[3], v[4], v[9], v[14], 0, 0);
// mix 10
G2(v[0], v[4], v[8], v[12], 0, 0);
G2(v[1], v[5], v[9], v[13], 0, 0);
G2(v[2], v[6], v[10], v[14], 0, 0);
G2(v[3], v[7], v[11], v[15], m, 0);
G2(v[0], v[5], v[10], v[15], 0, 0);
G2(v[1], v[6], v[11], v[12], 0, 0);
G2(v[2], v[7], v[8], v[13], 0, 0);
G2(v[3], v[4], v[9], v[14], 0, 0);
// mix 11
G2(v[0], v[4], v[8], v[12], 0, m);
G2(v[1], v[5], v[9], v[13], 0, 0);
G2(v[2], v[6], v[10], v[14], 0, 0);
G2(v[3], v[7], v[11], v[15], 0, 0);
G2(v[0], v[5], v[10], v[15], 0, 0);
G2(v[1], v[6], v[11], v[12], 0, 0);
G2(v[2], v[7], v[8], v[13], 0, 0);
G2(v[3], v[4], v[9], v[14], 0, 0);
// mix 12
G2(v[0], v[4], v[8], v[12], 0, 0);
G2(v[1], v[5], v[9], v[13], 0, 0);
G2(v[2], v[6], v[10], v[14], 0, 0);
G2(v[3], v[7], v[11], v[15], 0, 0);
G2(v[0], v[5], v[10], v[15], m, 0);
G2(v[1], v[6], v[11], v[12], 0, 0);
G2(v[2], v[7], v[8], v[13], 0, 0);
G2(v[3], v[4], v[9], v[14], 0, 0);
v[0] ^= hash_h[0] ^ v[8];
v[1] ^= hash_h[1] ^ v[9];
v[2] ^= hash_h[2] ^ v[10];
v[3] ^= hash_h[3] ^ v[11];
v[4] ^= hash_h[4] ^ v[12];
v[5] ^= hash_h[5] ^ v[13];
v32[12] ^= hash_h32[12] ^ v32[28];
u32 bexor = __byte_perm(v32[0], 0, 0x4012); // first 20 bits
u32 bucketid;
asm("bfe.u32 %0, %1, 12, 12;" : "=r"(bucketid) : "r"(bexor));
u32 slotp = atomicAdd(&eq->edata.nslots0[bucketid], 1);
if (slotp < RB8_NSLOTS)
{
slot* s = &eq->round0trees[bucketid][slotp];
uint4 tt;
tt.x = __byte_perm(v32[0], v32[1], 0x1234);
tt.y = __byte_perm(v32[1], v32[2], 0x1234);
tt.z = __byte_perm(v32[2], v32[3], 0x1234);
tt.w = __byte_perm(v32[3], v32[4], 0x1234);
*(uint4*)(&s->hash[0]) = tt;
tt.x = __byte_perm(v32[4], v32[5], 0x1234);
tt.y = __byte_perm(v32[5], v32[6], 0x1234);
tt.z = 0;
tt.w = block << 1;
*(uint4*)(&s->hash[4]) = tt;
}
bexor = __byte_perm(v32[6], 0, 0x0123);
asm("bfe.u32 %0, %1, 12, 12;" : "=r"(bucketid) : "r"(bexor));
slotp = atomicAdd(&eq->edata.nslots0[bucketid], 1);
if (slotp < RB8_NSLOTS)
{
slot* s = &eq->round0trees[bucketid][slotp];
uint4 tt;
tt.x = __byte_perm(v32[6], v32[7], 0x2345);
tt.y = __byte_perm(v32[7], v32[8], 0x2345);
tt.z = __byte_perm(v32[8], v32[9], 0x2345);
tt.w = __byte_perm(v32[9], v32[10], 0x2345);
*(uint4*)(&s->hash[0]) = tt;
tt.x = __byte_perm(v32[10], v32[11], 0x2345);
tt.y = __byte_perm(v32[11], v32[12], 0x2345);
tt.z = 0;
tt.w = (block << 1) + 1;
*(uint4*)(&s->hash[4]) = tt;
}
}
/*
Functions digit_1 to digit_8 works by the same principle;
Each thread does 2-3 slot loads (loads are coalesced).
Xorwork of slots is loaded into shared memory and is kept in registers (except for digit_1).
At the same time, restbits (8 or 9 bits) in xorwork are used for collisions.
Restbits determine position in ht.
Following next is pair creation. First one (or two) pairs' xorworks are put into global memory
as soon as possible, the rest pairs are saved in shared memory (one u32 per pair - 16 bit indices).
In most cases, all threads have one (or two) pairs so with this trick, we offload memory writes a bit in last step.
In last step we save xorwork of pairs in memory.
*/
template <u32 RB, u32 SM, int SSM, typename PACKER, u32 MAXPAIRS, u32 THREADS>
__global__ void digit_1(equi<RB, SM>* eq)
{
__shared__ u16 ht[256][SSM - 1];
__shared__ uint2 lastword1[RB8_NSLOTS];
__shared__ uint4 lastword2[RB8_NSLOTS];
__shared__ int ht_len[MAXPAIRS];
__shared__ u32 pairs_len;
__shared__ u32 next_pair;
const u32 threadid = threadIdx.x;
const u32 bucketid = blockIdx.x;
// reset hashtable len
if (threadid < 256)
ht_len[threadid] = 0;
else if (threadid == (THREADS - 1))
pairs_len = 0;
else if (threadid == (THREADS - 33))
next_pair = 0;
u32 bsize = umin(eq->edata.nslots0[bucketid], RB8_NSLOTS);
u32 hr[2];
int pos[2];
pos[0] = pos[1] = SSM;
uint2 ta[2];
uint4 tb[2];
u32 si[2];
// enable this to make fully safe shared mem operations;
// disabled gains some speed, but can rarely cause a crash
//__syncthreads();
#pragma unroll
for (u32 i = 0; i != 2; ++i)
{
si[i] = i * THREADS + threadid;
if (si[i] >= bsize) break;
const slot* pslot1 = eq->round0trees[bucketid] + si[i];
// get xhash
uint4 a1 = *(uint4*)(&pslot1->hash[0]);
uint2 a2 = *(uint2*)(&pslot1->hash[4]);
ta[i].x = a1.x;
ta[i].y = a1.y;
lastword1[si[i]] = ta[i];
tb[i].x = a1.z;
tb[i].y = a1.w;
tb[i].z = a2.x;
tb[i].w = a2.y;
lastword2[si[i]] = tb[i];
asm("bfe.u32 %0, %1, 20, 8;" : "=r"(hr[i]) : "r"(ta[i].x));
pos[i] = atomicAdd(&ht_len[hr[i]], 1);
if (pos[i] < (SSM - 1)) ht[hr[i]][pos[i]] = si[i];
}
__syncthreads();
int* pairs = ht_len;
u32 xors[6];
u32 xorbucketid, xorslot;
#pragma unroll
for (u32 i = 0; i != 2; ++i)
{
if (pos[i] >= SSM) continue;
if (pos[i] > 0)
{
u16 p = ht[hr[i]][0];
*(uint2*)(&xors[0]) = ta[i] ^ lastword1[p];
asm("bfe.u32 %0, %1, %2, %3;" : "=r"(xorbucketid) : "r"(xors[0]), "r"(RB), "r"(BUCKBITS));
xorslot = atomicAdd(&eq->edata.nslots[1][xorbucketid], 1);
if (xorslot < NSLOTS)
{
*(uint4*)(&xors[2]) = lastword2[si[i]] ^ lastword2[p];
slot &xs = eq->trees[0][xorbucketid][xorslot];
*(uint4*)(&xs.hash[0]) = *(uint4*)(&xors[1]);
uint4 ttx;
ttx.x = xors[5];
ttx.y = xors[0];
ttx.z = packer_default::set_bucketid_and_slots(bucketid, si[i], p, 8, RB8_NSLOTS);
ttx.w = 0;
*(uint4*)(&xs.hash[4]) = ttx;
}
for (int k = 1; k != pos[i]; ++k)
{
u32 pindex = atomicAdd(&pairs_len, 1);
if (pindex >= MAXPAIRS) break;
u16 prev = ht[hr[i]][k];
pairs[pindex] = __byte_perm(si[i], prev, 0x1054);
}
}
}
__syncthreads();
// process pairs
u32 plen = umin(pairs_len, MAXPAIRS);
u32 i, k;
for (u32 s = atomicAdd(&next_pair, 1); s < plen; s = atomicAdd(&next_pair, 1))
{
int pair = pairs[s];
i = __byte_perm(pair, 0, 0x4510);
k = __byte_perm(pair, 0, 0x4532);
*(uint2*)(&xors[0]) = lastword1[i] ^ lastword1[k];
asm("bfe.u32 %0, %1, %2, %3;" : "=r"(xorbucketid) : "r"(xors[0]), "r"(RB), "r"(BUCKBITS));
xorslot = atomicAdd(&eq->edata.nslots[1][xorbucketid], 1);
if (xorslot < NSLOTS)
{
*(uint4*)(&xors[2]) = lastword2[i] ^ lastword2[k];
slot &xs = eq->trees[0][xorbucketid][xorslot];
*(uint4*)(&xs.hash[0]) = *(uint4*)(&xors[1]);
uint4 ttx;
ttx.x = xors[5];
ttx.y = xors[0];
ttx.z = packer_default::set_bucketid_and_slots(bucketid, i, k, 8, RB8_NSLOTS);
ttx.w = 0;
*(uint4*)(&xs.hash[4]) = ttx;
}
}
}
template <u32 RB, u32 SM, int SSM, typename PACKER, u32 MAXPAIRS, u32 THREADS>
__global__ void digit_2(equi<RB, SM>* eq)
{
__shared__ u16 ht[NRESTS][SSM - 1];
__shared__ u32 lastword1[NSLOTS];
__shared__ uint4 lastword2[NSLOTS];
__shared__ int ht_len[NRESTS];
__shared__ int pairs[MAXPAIRS];
__shared__ u32 pairs_len;
__shared__ u32 next_pair;
const u32 threadid = threadIdx.x;
const u32 bucketid = blockIdx.x;
// reset hashtable len
if (threadid < NRESTS)
ht_len[threadid] = 0;
else if (threadid == (THREADS - 1))
pairs_len = 0;
else if (threadid == (THREADS - 33))
next_pair = 0;
slot* buck = eq->trees[0][bucketid];
u32 bsize = umin(eq->edata.nslots[1][bucketid], NSLOTS);
u32 hr[2];
int pos[2];
pos[0] = pos[1] = SSM;
u32 ta[2];
uint4 tt[2];
u32 si[2];
// enable this to make fully safe shared mem operations;
// disabled gains some speed, but can rarely cause a crash
//__syncthreads();
#pragma unroll
for (u32 i = 0; i != 2; ++i)
{
si[i] = i * THREADS + threadid;
if (si[i] >= bsize) break;
// get slot
const slot* pslot1 = buck + si[i];
uint4 ttx = *(uint4*)(&pslot1->hash[0]);
lastword1[si[i]] = ta[i] = ttx.x;
uint2 tty = *(uint2*)(&pslot1->hash[4]);
tt[i].x = ttx.y;
tt[i].y = ttx.z;
tt[i].z = ttx.w;
tt[i].w = tty.x;
lastword2[si[i]] = tt[i];
hr[i] = tty.y & RESTMASK;
pos[i] = atomicAdd(&ht_len[hr[i]], 1);
if (pos[i] < (SSM - 1)) ht[hr[i]][pos[i]] = si[i];
}
__syncthreads();
u32 xors[5];
u32 xorbucketid, xorslot;
#pragma unroll
for (u32 i = 0; i != 2; ++i)
{
if (pos[i] >= SSM) continue;
if (pos[i] > 0)
{
u16 p = ht[hr[i]][0];
xors[0] = ta[i] ^ lastword1[p];
xorbucketid = xors[0] >> (12 + RB);
xorslot = atomicAdd(&eq->edata.nslots[2][xorbucketid], 1);
if (xorslot < NSLOTS)
{
*(uint4*)(&xors[1]) = tt[i] ^ lastword2[p];
slotsmall &xs = eq->round2trees[xorbucketid].treessmall[xorslot];
*(uint4*)(&xs.hash[0]) = *(uint4*)(&xors[0]);
slottiny &xst = eq->round2trees[xorbucketid].treestiny[xorslot];
uint2 ttx;
ttx.x = xors[4];
ttx.y = PACKER::set_bucketid_and_slots(bucketid, si[i], p, RB, SM);
*(uint2*)(&xst.hash[0]) = ttx;
}
for (int k = 1; k != pos[i]; ++k)
{
u32 pindex = atomicAdd(&pairs_len, 1);
if (pindex >= MAXPAIRS) break;
u16 prev = ht[hr[i]][k];
pairs[pindex] = __byte_perm(si[i], prev, 0x1054);
}
}
}
__syncthreads();
// process pairs
u32 plen = umin(pairs_len, MAXPAIRS);
u32 i, k;
for (u32 s = atomicAdd(&next_pair, 1); s < plen; s = atomicAdd(&next_pair, 1))
{
int pair = pairs[s];
i = __byte_perm(pair, 0, 0x4510);
k = __byte_perm(pair, 0, 0x4532);
xors[0] = lastword1[i] ^ lastword1[k];
xorbucketid = xors[0] >> (12 + RB);
xorslot = atomicAdd(&eq->edata.nslots[2][xorbucketid], 1);
if (xorslot < NSLOTS)
{
*(uint4*)(&xors[1]) = lastword2[i] ^ lastword2[k];
slotsmall &xs = eq->round2trees[xorbucketid].treessmall[xorslot];
*(uint4*)(&xs.hash[0]) = *(uint4*)(&xors[0]);
slottiny &xst = eq->round2trees[xorbucketid].treestiny[xorslot];
uint2 ttx;
ttx.x = xors[4];
ttx.y = PACKER::set_bucketid_and_slots(bucketid, i, k, RB, SM);
*(uint2*)(&xst.hash[0]) = ttx;
}
}
}
template <u32 RB, u32 SM, int SSM, typename PACKER, u32 MAXPAIRS, u32 THREADS>
__global__ void digit_3(equi<RB, SM>* eq)
{
__shared__ u16 ht[NRESTS][(SSM - 1)];
__shared__ uint4 lastword1[NSLOTS];
__shared__ u32 lastword2[NSLOTS];
__shared__ int ht_len[NRESTS];
__shared__ int pairs[MAXPAIRS];
__shared__ u32 pairs_len;
__shared__ u32 next_pair;
const u32 threadid = threadIdx.x;
const u32 bucketid = blockIdx.x;
// reset hashtable len
if (threadid < NRESTS)
ht_len[threadid] = 0;
else if (threadid == (THREADS - 1))
pairs_len = 0;
else if (threadid == (THREADS - 33))
next_pair = 0;
u32 bsize = umin(eq->edata.nslots[2][bucketid], NSLOTS);
u32 hr[2];
int pos[2];
pos[0] = pos[1] = SSM;
u32 si[2];
uint4 tt[2];
u32 ta[2];
// enable this to make fully safe shared mem operations;
// disabled gains some speed, but can rarely cause a crash
//__syncthreads();
#pragma unroll
for (u32 i = 0; i != 2; ++i)
{
si[i] = i * THREADS + threadid;
if (si[i] >= bsize) break;
slotsmall &xs = eq->round2trees[bucketid].treessmall[si[i]];
slottiny &xst = eq->round2trees[bucketid].treestiny[si[i]];
tt[i] = *(uint4*)(&xs.hash[0]);
lastword1[si[i]] = tt[i];
ta[i] = xst.hash[0];
lastword2[si[i]] = ta[i];
asm("bfe.u32 %0, %1, 12, %2;" : "=r"(hr[i]) : "r"(tt[i].x), "r"(RB));
pos[i] = atomicAdd(&ht_len[hr[i]], 1);
if (pos[i] < (SSM - 1)) ht[hr[i]][pos[i]] = si[i];
}
__syncthreads();
u32 xors[5];
u32 bexor, xorbucketid, xorslot;
#pragma unroll
for (u32 i = 0; i != 2; ++i)
{
if (pos[i] >= SSM) continue;
if (pos[i] > 0)
{
u16 p = ht[hr[i]][0];
xors[4] = ta[i] ^ lastword2[p];
if (xors[4] != 0)
{
*(uint4*)(&xors[0]) = tt[i] ^ lastword1[p];
bexor = __byte_perm(xors[0], xors[1], 0x2107);
asm("bfe.u32 %0, %1, %2, %3;" : "=r"(xorbucketid) : "r"(bexor), "r"(RB), "r"(BUCKBITS));
xorslot = atomicAdd(&eq->edata.nslots[3][xorbucketid], 1);
if (xorslot < NSLOTS)
{
slotsmall &xs = eq->round3trees[xorbucketid].treessmall[xorslot];
*(uint4*)(&xs.hash[0]) = *(uint4*)(&xors[1]);
slottiny &xst = eq->round3trees[xorbucketid].treestiny[xorslot];
uint2 ttx;
ttx.x = bexor;
ttx.y = PACKER::set_bucketid_and_slots(bucketid, si[i], p, RB, SM);
*(uint2*)(&xst.hash[0]) = ttx;
}
}
for (int k = 1; k != pos[i]; ++k)
{
u32 pindex = atomicAdd(&pairs_len, 1);
if (pindex >= MAXPAIRS) break;
u16 prev = ht[hr[i]][k];
pairs[pindex] = __byte_perm(si[i], prev, 0x1054);
}
}
}
__syncthreads();
// process pairs
u32 plen = umin(pairs_len, MAXPAIRS);
u32 i, k;
for (u32 s = atomicAdd(&next_pair, 1); s < plen; s = atomicAdd(&next_pair, 1))
{
int pair = pairs[s];
i = __byte_perm(pair, 0, 0x4510);
k = __byte_perm(pair, 0, 0x4532);
xors[4] = lastword2[i] ^ lastword2[k];
if (xors[4] != 0)
{
*(uint4*)(&xors[0]) = lastword1[i] ^ lastword1[k];
bexor = __byte_perm(xors[0], xors[1], 0x2107);
asm("bfe.u32 %0, %1, %2, %3;" : "=r"(xorbucketid) : "r"(bexor), "r"(RB), "r"(BUCKBITS));
xorslot = atomicAdd(&eq->edata.nslots[3][xorbucketid], 1);
if (xorslot < NSLOTS)
{
slotsmall &xs = eq->round3trees[xorbucketid].treessmall[xorslot];
*(uint4*)(&xs.hash[0]) = *(uint4*)(&xors[1]);
slottiny &xst = eq->round3trees[xorbucketid].treestiny[xorslot];
uint2 ttx;
ttx.x = bexor;
ttx.y = PACKER::set_bucketid_and_slots(bucketid, i, k, RB, SM);
*(uint2*)(&xst.hash[0]) = ttx;
}
}
}
}
template <u32 RB, u32 SM, int SSM, typename PACKER, u32 MAXPAIRS, u32 THREADS>
__global__ void digit_4(equi<RB, SM>* eq)
{
__shared__ u16 ht[NRESTS][(SSM - 1)];
__shared__ uint4 lastword[NSLOTS];
__shared__ int ht_len[NRESTS];
__shared__ int pairs[MAXPAIRS];
__shared__ u32 pairs_len;
__shared__ u32 next_pair;
const u32 threadid = threadIdx.x;
const u32 bucketid = blockIdx.x;
// reset hashtable len
if (threadid < NRESTS)
ht_len[threadid] = 0;
else if (threadid == (THREADS - 1))
pairs_len = 0;
else if (threadid == (THREADS - 33))
next_pair = 0;
u32 bsize = umin(eq->edata.nslots[3][bucketid], NSLOTS);
u32 hr[2];
int pos[2];
pos[0] = pos[1] = SSM;
u32 si[2];
uint4 tt[2];
// enable this to make fully safe shared mem operations;
// disabled gains some speed, but can rarely cause a crash
//__syncthreads();
#pragma unroll
for (u32 i = 0; i != 2; ++i)
{
si[i] = i * THREADS + threadid;
if (si[i] >= bsize) break;
slotsmall &xs = eq->round3trees[bucketid].treessmall[si[i]];
slottiny &xst = eq->round3trees[bucketid].treestiny[si[i]];
// get xhash
tt[i] = *(uint4*)(&xs.hash[0]);
lastword[si[i]] = tt[i];
hr[i] = xst.hash[0] & RESTMASK;
pos[i] = atomicAdd(&ht_len[hr[i]], 1);
if (pos[i] < (SSM - 1)) ht[hr[i]][pos[i]] = si[i];
}
__syncthreads();
u32 xors[4];
u32 xorbucketid, xorslot;
#pragma unroll
for (u32 i = 0; i != 2; ++i)
{
if (pos[i] >= SSM) continue;
if (pos[i] > 0)
{
u16 p = ht[hr[i]][0];
*(uint4*)(&xors[0]) = tt[i] ^ lastword[p];
if (xors[3] != 0)
{
asm("bfe.u32 %0, %1, %2, %3;" : "=r"(xorbucketid) : "r"(xors[0]), "r"(4 + RB), "r"(BUCKBITS));