-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathwrite.cpp
More file actions
1888 lines (1816 loc) · 59.1 KB
/
write.cpp
File metadata and controls
1888 lines (1816 loc) · 59.1 KB
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
#ifndef _GNU_SOURCE
#define _GNU_SOURCE
#endif
#ifdef _NO_VECTOR
#ifndef _NO_VECTOR_HASHING
#define _NO_VECTOR_HASHING
#endif
#ifndef _NO_VECTOR_PARTITIONING
#define _NO_VECTOR_PARTITIONING
#endif
#endif
#ifdef _NO_VECTOR_HASHING
#ifdef _NO_VECTOR_PARTITIONING
#ifndef _NO_VECTOR
#define _NO_VECTOR
#endif
#endif
#endif
#ifndef _NO_VECTOR
#include <immintrin.h>
#endif
#include <iostream>
#include <string>
#include <sys/types.h>
#include <sys/stat.h>
#include <pthread.h>
#include <stdlib.h>
#include <stdint.h>
#include <assert.h>
#include <stdio.h>
#include <sched.h>
#include <time.h>
#include <sys/time.h>
#include <math.h>
#include "rand.h"
#include "sched.h"
#include "hj.h"
#include "malloc.h"
#include <numa.h>
#define R 0
#ifdef MCDRAM
#include <hbwmalloc.h>
#endif
#ifndef NEXT_POW_2
int BUFFER_SIZE=64;
int PREFETCH_DISTANCE=0;
/**
* compute the next number, greater than or equal to 32-bit unsigned v.
* taken from "bit twiddling hacks":
* http://graphics.stanford.edu/~seander/bithacks.html
*/
#define NEXT_POW_2(V) \
do { \
V--; \
V |= V >> 1; \
V |= V >> 2; \
V |= V >> 4; \
V |= V >> 8; \
V |= V >> 16; \
V++; \
} while(0)
#endif
typedef struct rand_state_64 {
uint64_t num[313];
size_t index;
} rand64_t;
rand64_t *rand64_init(uint64_t seed)
{
rand64_t *state = (rand64_t*)malloc(sizeof(rand64_t));
uint64_t *n = state->num;
size_t i;
n[0] = seed;
for (i = 0 ; i != 311 ; ++i)
n[i + 1] = 6364136223846793005ull *
(n[i] ^ (n[i] >> 62)) + i + 1;
state->index = 312;
return state;
}
__m512i simd_hash(__m512i k, __m512i Nbins)
{
__m512i permute_2 = _mm512_set_epi32(14, 15, 12, 13, 10, 11, 8, 9, 6, 7, 4, 5, 2, 3, 0, 1);
__m512i blend_0 = _mm512_set1_epi32(0);
__mmask16 blend_interleave = _mm512_int2mask(21845);
__m512i Nbins2 = _mm512_permutevar_epi32 (permute_2,Nbins);
Nbins=_mm512_mask_blend_epi32(blend_interleave,blend_0,Nbins);
Nbins2=_mm512_mask_blend_epi32(blend_interleave,blend_0,Nbins2);
__m512i k2=_mm512_permutevar_epi32 (permute_2,k);
k=_mm512_mask_blend_epi32(blend_interleave,blend_0,k);
k2=_mm512_mask_blend_epi32(blend_interleave,blend_0,k2);
k=_mm512_mul_epu32 (k,Nbins);
k2=_mm512_mul_epu32 (k2,Nbins2);
k=_mm512_permutevar_epi32 (permute_2,k);
k=_mm512_mask_blend_epi32(blend_interleave,k2,k);
return k;
}
__m512i _mm512_fmadd_epi32(__m512i a, __m512i b, __m512i c)
{
__m512i temp=_mm512_mullo_epi32(a,b);
temp=_mm512_add_epi32 (temp,c);
return temp;
}
uint64_t rand64_next(rand64_t *state)
{
uint64_t x, *n = state->num;
if (state->index == 312) {
size_t i = 0;
do {
x = n[i] & 0xffffffff80000000ull;
x |= n[i + 1] & 0x7fffffffull;
n[i] = n[i + 156] ^ (x >> 1);
n[i] ^= 0xb5026f5aa96619e9ull & -(x & 1);
} while (++i != 156);
n[312] = n[0];
do {
x = n[i] & 0xffffffff80000000ull;
x |= n[i + 1] & 0x7fffffffull;
n[i] = n[i - 156] ^ (x >> 1);
n[i] ^= 0xb5026f5aa96619e9ull & -(x & 1);
} while (++i != 312);
state->index = 0;
}
x = n[state->index++];
x ^= (x >> 29) & 0x5555555555555555ull;
x ^= (x << 17) & 0x71d67fffeda60000ull;
x ^= (x << 37) & 0xfff7eee000000000ull;
x ^= (x >> 43);
return x;
}
typedef struct rand_state_32 {
uint32_t num[625];
size_t index;
} rand32_t;
rand32_t *rand32_init(uint32_t seed)
{
rand32_t *state = (rand32_t*)malloc(sizeof(rand32_t));
uint32_t *n = state->num;
size_t i;
n[0] = seed;
for (i = 0 ; i != 623 ; ++i)
n[i + 1] = 0x6c078965 * (n[i] ^ (n[i] >> 30));
state->index = 624;
return state;
}
uint32_t rand32_next(rand32_t *state)
{
uint32_t y, *n = state->num;
if (state->index == 624) {
size_t i = 0;
do {
y = n[i] & 0x80000000;
y += n[i + 1] & 0x7fffffff;
n[i] = n[i + 397] ^ (y >> 1);
n[i] ^= 0x9908b0df & -(y & 1);
} while (++i != 227);
n[624] = n[0];
do {
y = n[i] & 0x80000000;
y += n[i + 1] & 0x7fffffff;
n[i] = n[i - 227] ^ (y >> 1);
n[i] ^= 0x9908b0df & -(y & 1);
} while (++i != 624);
state->index = 0;
}
y = n[state->index++];
y ^= (y >> 11);
y ^= (y << 7) & 0x9d2c5680;
y ^= (y << 15) & 0xefc60000;
y ^= (y >> 18);
return y;
}
uint64_t thread_time(void)
{
struct timespec t;
//assert(clock_gettime(CLOCK_THREAD_CPUTIME_ID, &t) == 0);
return t.tv_sec * 1000 * 1000 * 1000 + t.tv_nsec;
}
uint64_t real_time(void)
{
struct timespec t;
//assert(clock_gettime(CLOCK_REALTIME, &t) == 0);
return t.tv_sec * 1000 * 1000 * 1000 + t.tv_nsec;
}
double mysecond()
{
struct timeval tp;
struct timezone tzp;
int i;
i = gettimeofday(&tp,&tzp);
return ( (double) tp.tv_sec + (double) tp.tv_usec * 1.e-6 );
}
int hardware_threads(void)
{
char name[64];
struct stat st;
int threads = -1;
do {
sprintf(name, "/sys/devices/system/cpu/cpu%d", ++threads);
} while (stat(name, &st) == 0);
return threads;
}
void bind_thread(int thread, int threads)
{
size_t size = CPU_ALLOC_SIZE(threads);
cpu_set_t *cpu_set = CPU_ALLOC(threads);
//assert(cpu_set != NULL);
CPU_ZERO_S(size, cpu_set);
CPU_SET_S(thread, size, cpu_set);
//assert(pthread_setaffinity_np(pthread_self(), size, cpu_set) == 0);
CPU_FREE(cpu_set);
}
void *numa_malloc(size_t size, int node)
{
void *ptr = NULL;
//ptr=numa_alloc_onnode(size, node);
ptr=memalign(64, size);// ? NULL : ptr;
return ptr;
}
void *mamalloc(size_t size)
{
void *ptr = NULL;
ptr=memalign(64, size);// ? NULL : ptr;
return ptr;
}
void *align(const void *p)
{
size_t i = 63 & (size_t) p;
return (void*) (i ? p + 64 - i : p);
}
int power_of_2(uint64_t x)
{
return x > 0 && (x & (x - 1)) == 0;
}
int odd_prime(uint64_t x)
{
uint64_t d;
for (d = 3 ; d * d <= x ; d += 2)
if (x % d == 0) return 0;
return 1;
}
#ifndef _NO_VECTOR_HASHING
void set(uint64_t *dst, size_t size, uint32_t value)
{
uint64_t *dst_end = &dst[size];
uint64_t *dst_aligned = (uint64_t *)align(dst);
__m512i x = _mm512_set1_epi64(value);
while (dst != dst_end && dst != dst_aligned)
*dst++ = value;
dst_aligned = &dst[(dst_end - dst) & ~7];
while (dst != dst_aligned) {
_mm512_store_epi64(dst, x);
dst += 8;
}
while (dst != dst_end)
*dst++ = value;
}
void build(const uint32_t *keys, const uint32_t *vals, size_t size,
uint64_t *table, size_t buckets,
const uint32_t factor[2], uint32_t empty)
{
set(table, buckets, empty);
// set constants
__m512i mask_1 = _mm512_set1_epi32(1);
__m512i mask_empty = _mm512_set1_epi32(empty);
__m512i mask_factor_1 = _mm512_set1_epi32(factor[0]);
__m512i mask_factor_2 = _mm512_set1_epi32(factor[1]);
__m512i mask_buckets = _mm512_set1_epi32(buckets);
__m512i mask_buckets_minus_1 = _mm512_set1_epi32(buckets - 1);
__m512i mask_pack = _mm512_set_epi32(15, 7, 14, 6, 13, 5, 12, 4, 11, 3, 10, 2, 9, 1, 8, 0);
__mmask16 blend_0000 = _mm512_int2mask(0x0000);
__mmask16 blend_AAAA = _mm512_int2mask(0xAAAA);
__mmask16 blend_5555 = _mm512_int2mask(0x5555);
// main loop
size_t i = 0;
size_t size_minus_16 = size - 16;
__mmask16 k = _mm512_kxnor(k, k);
__m512i key, val, off;
if (size >= 16) do {
// replace invalid keys & payloads
//key = (key, k, &keys[i]);
//key = _mm512_mask_loadunpackhi_epi32(key, k, &keys[i + 16]);
key = _mm512_mask_expandloadu_epi32 (key, k, &keys[i]);
val = _mm512_mask_expandloadu_epi32 (val, k, &vals[i]);
//val = _mm512_mask_loadunpacklo_epi32(val, k, &vals[i]);
//val = _mm512_mask_loadunpackhi_epi32(val, k, &vals[i + 16]);
// update
off = _mm512_mask_xor_epi32(off, k, off, off);
i += _mm_countbits_64(_mm512_kconcatlo_64(blend_0000, k));
// hash keys
__m512i factors = _mm512_mask_blend_epi32(k, mask_factor_2, mask_factor_1);
__m512i buckets = _mm512_mask_blend_epi32(k, mask_buckets_minus_1, mask_buckets);
__m512i hash = _mm512_mullo_epi32(key, factors);
hash = simd_hash(hash, buckets);
// combine with old offset and fix overflows
off = _mm512_add_epi32(off, hash);
k = _mm512_cmpge_epu32_mask(off, mask_buckets);
off = _mm512_mask_sub_epi32(off, k, off, mask_buckets);
// load keys from table and detect conflicts
__m512i tab = _mm512_i32gather_epi32(off, table, 8);
k = _mm512_cmpeq_epi32_mask(tab, mask_empty);
_mm512_mask_i32scatter_epi32(table, k, off, mask_pack, 8);
tab = _mm512_mask_i32gather_epi32(tab, k, off, table, 8);
k = _mm512_mask_cmpeq_epi32_mask(k, tab, mask_pack);
// mix keys and payloads in pairs
__m512i key_tmp = _mm512_permutevar_epi32(mask_pack, key);
__m512i val_tmp = _mm512_permutevar_epi32(mask_pack, val);
__m512i lo = _mm512_mask_blend_epi32(blend_AAAA, key_tmp, _mm512_swizzle_epi32(val_tmp, _MM_SWIZ_REG_CDAB));
__m512i hi = _mm512_mask_blend_epi32(blend_5555, val_tmp, _mm512_swizzle_epi32(key_tmp, _MM_SWIZ_REG_CDAB));
// store valid pairs
_mm512_mask_i32loscatter_epi64(table, k, off, lo, 8);
__mmask16 rev_k = _mm512_kunpackb (k,k>>8);
__m512i rev_off = _mm512_permute4f128_epi32(off, _MM_PERM_BADC);
_mm512_mask_i32loscatter_epi64(table, rev_k, rev_off, hi, 8);
off = _mm512_add_epi32(off, mask_1);
} while (i <= size_minus_16);
// save last items
uint32_t keys_last[32];
uint32_t vals_last[32];
k = _mm512_knot(k);
_mm512_mask_compressstoreu_epi32(&keys_last[0], k, key);
_mm512_mask_compressstoreu_epi32(&vals_last[0], k, val);
//_mm512_mask_packstorelo_epi32(&keys_last[0], k, key);
//_mm512_mask_packstorehi_epi32(&keys_last[16], k, key);
//_mm512_mask_packstorelo_epi32(&vals_last[0], k, val);
//_mm512_mask_packstorehi_epi32(&vals_last[16], k, val);
size_t j = _mm_countbits_64(_mm512_kconcatlo_64(blend_0000, k));
for (; i != size ; ++i, ++j) {
keys_last[j] = keys[i];
vals_last[j] = vals[i];
}
// process last items in scalar code
for (i = 0 ; i != j ; ++i) {
uint32_t k = keys_last[i];
uint64_t p = vals_last[i];
p = (p << 32) | k;
uint64_t h1 = (uint32_t) (k * factor[0]);
uint64_t h2 = (uint32_t) (k * factor[1]);
h1 = (h1 * buckets) >> 32;
h2 = ((h2 * (buckets - 1)) >> 32) + 1;
while (empty != (uint32_t) table[h1]) {
h1 += h2;
if (h1 >= buckets)
h1 -= buckets;
}
table[h1] = p;
}
}
size_t probe(const uint32_t *keys, const uint32_t *vals, size_t size,
const uint64_t *table, size_t buckets,
const uint32_t factor[2], uint32_t empty,
uint32_t *keys_buf, uint32_t *vals_buf, uint32_t *tabs_buf,
uint32_t *keys_out, uint32_t *vals_out, uint32_t *tabs_out,
size_t offset, size_t buffer_size, size_t block_size,
size_t block_limit, volatile size_t *counter, int flush)
{
// generate masks
__m512i mask_1 = _mm512_set1_epi32(1);
__m512i mask_empty = _mm512_set1_epi32(empty);
__m512i mask_factor_1 = _mm512_set1_epi32(factor[0]);
__m512i mask_factor_2 = _mm512_set1_epi32(factor[1]);
__m512i mask_buckets = _mm512_set1_epi32(buckets);
__m512i mask_buckets_minus_1 = _mm512_set1_epi32(buckets - 1);
__m512i mask_unpack = _mm512_set_epi32(15, 13, 11, 9, 7, 5, 3, 1, 14, 12, 10, 8, 6, 4, 2, 0);
__mmask16 blend_0000 = _mm512_int2mask(0x0000);
__mmask16 blend_AAAA = _mm512_int2mask(0xAAAA);
__mmask16 blend_5555 = _mm512_int2mask(0x5555);
// main loop
size_t b, i = 0;
size_t j = offset & (buffer_size - 1);
size_t o = offset & ~(buffer_size - 1);
const size_t size_vec = size - 16;
__mmask16 k = _mm512_kxnor(k, k);
__m512i key, val, off;
if (size >= 16) do {
// replace invalid keys & payloads
key = _mm512_mask_expandloadu_epi32 (key, k, &keys[i]);
val = _mm512_mask_expandloadu_epi32 (val, k, &vals[i]);
//key = _mm512_mask_loadunpacklo_epi32(key, k, &keys[i]);
//key = _mm512_mask_loadunpackhi_epi32(key, k, &keys[i + 16]);
//val = _mm512_mask_loadunpacklo_epi32(val, k, &vals[i]);
//val = _mm512_mask_loadunpackhi_epi32(val, k, &vals[i + 16]);
// update offsets
off = _mm512_mask_xor_epi32(off, k, off, off);
i += _mm_countbits_64(_mm512_kconcatlo_64(blend_0000, k));
// hash keys using either 1st or 2nd function
__m512i factors = _mm512_mask_blend_epi32(k, mask_factor_2, mask_factor_1);
__m512i buckets = _mm512_mask_blend_epi32(k, mask_buckets_minus_1, mask_buckets);
__m512i hash = _mm512_mullo_epi32(key, factors);
//hash = _mm512_mulhi_epu16(hash, buckets);
hash = simd_hash(hash, buckets);
// combine with old offset and fix overflows
off = _mm512_add_epi32(off, hash);
k = _mm512_cmpge_epu32_mask(off, mask_buckets);
off = _mm512_mask_sub_epi32(off, k, off, mask_buckets);
// load keys from table and update offsets
__m512i lo = _mm512_i32logather_epi64(off, table, 8);
__m512i rev = _mm512_permute4f128_epi32(off, _MM_PERM_BADC);
__m512i hi = _mm512_i32logather_epi64(rev, table, 8);
off = _mm512_add_epi32(off, mask_1);
// split keys and payloads
__m512i tab_key = _mm512_mask_blend_epi32(blend_AAAA, lo, _mm512_swizzle_epi32(hi, _MM_SWIZ_REG_CDAB));
__m512i tab_val = _mm512_mask_blend_epi32(blend_5555, hi, _mm512_swizzle_epi32(lo, _MM_SWIZ_REG_CDAB));
tab_key = _mm512_permutevar_epi32(mask_unpack, tab_key);
tab_val = _mm512_permutevar_epi32(mask_unpack, tab_val);
// compare
__mmask16 m = _mm512_cmpeq_epi32_mask(tab_key, key);
k = _mm512_cmpeq_epi32_mask(tab_key, mask_empty);
#ifdef _UNIQUE
k = _mm512_kor(k, m);
#endif
// partitions_aligned store matches
_mm512_mask_compressstoreu_epi32(&keys_buf[j + 0], m, key);
_mm512_mask_compressstoreu_epi32(&vals_buf[j + 0], m, val);
_mm512_mask_compressstoreu_epi32(&tabs_buf[j + 0], m, tab_val);
j += _mm_countbits_64(_mm512_kconcatlo_64(blend_0000, m));
if (j >= buffer_size) {
for (b = 0 ; b != buffer_size ; b += 16, o += 16) {
__m512 x = _mm512_load_ps(&keys_buf[b]);
__m512 y = _mm512_load_ps(&vals_buf[b]);
__m512 z = _mm512_load_ps(&tabs_buf[b]);
_mm512_stream_ps (&keys_out[o], x);
_mm512_stream_ps (&vals_out[o], y);
_mm512_stream_ps (&tabs_out[o], z);
}
j -= buffer_size;
if (j) {
__m512i x = _mm512_load_epi32(&keys_buf[b]);
__m512i y = _mm512_load_epi32(&vals_buf[b]);
__m512i z = _mm512_load_epi32(&tabs_buf[b]);
_mm512_store_epi32 (keys_buf, x);
_mm512_store_epi32 (vals_buf, y);
_mm512_store_epi32 (tabs_buf, z);
}
if ((o & (block_size - 1)) == 0) {
o = __sync_fetch_and_add(counter, 1);
assert(o <= block_limit);
o *= block_size;
}
}
} while (i <= size_vec);
off = _mm512_sub_epi32(off, mask_1);
// save last items
uint32_t keys_last[32];
uint32_t vals_last[32];
uint32_t offs_last[32];
k = _mm512_knot(k);
_mm512_mask_compressstoreu_epi32 (&keys_last[0], k, key);
_mm512_mask_compressstoreu_epi32 (&vals_last[0], k, val);
_mm512_mask_compressstoreu_epi32 (&offs_last[0], k, off);
size_t l = _mm_countbits_64(_mm512_kconcatlo_64(blend_0000, k));
for (; i != size ; ++i, ++l) {
keys_last[l] = keys[i];
vals_last[l] = vals[i];
offs_last[l] = buckets;
}
// process last items in scalar code
uint32_t factor_1 = factor[0];
uint32_t factor_2 = factor[1];
for (i = 0 ; i != l ; ++i) {
uint32_t key = keys_last[i];
uint32_t val = vals_last[i];
uint64_t h1 = offs_last[i];
uint64_t h2 = (uint32_t) (key * factor_2);
h2 = ((h2 * (buckets - 1)) >> 32) + 1;
if (h1 == buckets) {
h1 = (uint32_t) (key * factor_1);
h1 = (h1 * buckets) >> 32;
} else {
h1 += h2;
if (h1 >= buckets)
h1 -= buckets;
}
uint64_t tab = table[h1];
while (empty != (uint32_t) tab) {
if (key == (uint32_t) tab) {
keys_buf[j] = key;
vals_buf[j] = val;
tabs_buf[j] = tab >> 32;
if (++j == buffer_size) {
for (j = b = 0 ; b != buffer_size ; b += 16, o += 16) {
__m512 x = _mm512_load_ps(&keys_buf[b]);
__m512 y = _mm512_load_ps(&vals_buf[b]);
__m512 z = _mm512_load_ps(&tabs_buf[b]);
_mm512_stream_ps(&keys_out[o], x);
_mm512_stream_ps(&vals_out[o], y);
_mm512_stream_ps(&tabs_out[o], z);
}
if ((o & (block_size - 1)) == 0) {
o = __sync_fetch_and_add(counter, 1);
assert(o <= block_limit);
o *= block_size;
}
}
}
h1 += h2;
if (h1 >= buckets)
h1 -= buckets;
tab = table[h1];
}
}
if (!flush) o += j;
else for (b = 0 ; b != j ; ++b, ++o) {
keys_out[o] = keys_buf[b];
vals_out[o] = vals_buf[b];
tabs_out[o] = tabs_buf[b];
}
return o;
}
#else
#endif
void build_s(const uint32_t *keys, const uint32_t *vals, size_t size,
uint64_t *table, size_t buckets,
const uint32_t factor[2], uint32_t empty)
{
size_t i;
uint32_t factor_1 = factor[0];
uint32_t factor_2 = factor[1];
for (i = 0 ; i != buckets ; ++i)
table[i] = empty;
for (i = 0 ; i != size ; ++i) {
uint32_t k = keys[i];
uint64_t p = vals[i];
p = (p << 32) | k;
uint64_t h1 = (uint32_t) (k * factor_1);
h1 = (h1 * buckets) >> 32;
if (empty != (uint32_t) table[h1]) {
uint64_t h2 = (uint32_t) (k * factor_2);
h2 = ((h2 * (buckets - 1)) >> 32) + 1;
do {
h1 += h2;
if (h1 >= buckets)
h1 -= buckets;
} while (empty != (uint32_t) table[h1]);
}
table[h1] = p;
}
}
size_t probe_s(const uint32_t *keys, const uint32_t *vals, size_t size,
const uint64_t *table, size_t buckets,
const uint32_t factor[2], uint32_t empty,
uint32_t *keys_buf, uint32_t *vals_buf, uint32_t *tabs_buf,
uint32_t *keys_out, uint32_t *vals_out, uint32_t *tabs_out,
size_t offset, size_t buffer_size, size_t block_size,
size_t block_limit, volatile size_t *counter, int flush)
{
size_t i, o = offset;
uint32_t factor_1 = factor[0];
uint32_t factor_2 = factor[1];
for (i = 0 ; i != size ; ++i) {
uint32_t k = keys[i];
uint32_t v = vals[i];
uint64_t h1 = (uint32_t) k * factor_1;
h1 = (h1 * buckets) >> 32;
uint64_t t = table[h1];
if (empty != (uint32_t) t) {
uint64_t h2 = (uint32_t) k * factor_2;
h2 = ((h2 * (buckets - 1)) >> 32) + 1;
do {
if (k == (uint32_t) t) {
tabs_out[o] = t >> 32;
vals_out[o] = v;
keys_out[o] = k;
if ((++o & (block_size - 1)) == 0) {
o = __sync_fetch_and_add(counter, 1);
assert(o <= block_limit);
o *= block_size;
}
#ifdef _UNIQUE
break;
#endif
}
h1 += h2;
if (h1 >= buckets)
h1 -= buckets;
t = table[h1];
} while (empty != (uint32_t) t);
}
}
return o;
}
void flush(const uint32_t *counts, const uint32_t *offsets, const uint64_t *buffers,
uint32_t *keys_out, uint32_t *vals_out, size_t partitions)
{
size_t p;
for (p = 0 ; p != partitions ; ++p) {
const uint64_t *buf = &buffers[p * BUFFER_SIZE];
size_t o = offsets[p];
size_t c = counts[p];
size_t e = o & (BUFFER_SIZE-1);
size_t b = e > c ? e - c : 0;
o -= e - b;
while (b != e) {
uint64_t key_val = buf[b++];
keys_out[o] = key_val;
vals_out[o] = key_val >> 32;
o++;
}
}
}
void histogram_s(const uint32_t *keys, size_t size, uint32_t *counts,
uint32_t factor, size_t partitions)
{
size_t i, p;
for (p = 0 ; p != partitions ; ++p)
counts[p] = 0;
for (i = 0 ; i != size ; ++i) {
p = (uint32_t) (keys[i] * factor);
p = (p * partitions) >> 32;
counts[p]++;
}
}
void partition_s(const uint32_t *keys, const uint32_t *vals, size_t size,
const uint32_t *counts, uint32_t *keys_out, uint32_t *vals_out,
uint32_t factor, size_t partitions)
{
size_t align_output = 0;
uint32_t *keys_out_aligned = (uint32_t *)align(keys_out);
uint32_t *vals_out_aligned = (uint32_t *)align(vals_out);
assert(keys_out_aligned - keys_out ==
vals_out_aligned - vals_out);
if (keys_out != keys_out_aligned) {
assert(keys_out_aligned - keys_out < 16);
align_output = 16 - (keys_out_aligned - keys_out);
}
keys_out -= align_output;
vals_out -= align_output;
size_t i, p;
uint32_t offsets[partitions];
uint64_t buffers_space[(partitions << 4) + 7];
uint64_t *buffers = (uint64_t *)align(buffers_space);
for (i = p = 0 ; p != partitions ; ++p) {
offsets[p] = i + align_output;
i += counts[p];
}
assert(i == size);
for (i = 0 ; i != size ; ++i) {
uint32_t key = keys[i];
uint64_t key_val = vals[i];
key_val = (key_val << 32) | key;
p = (uint32_t) (key * factor);
p = (p * partitions) >> 32;
size_t o = offsets[p]++;
size_t b = o & 15;
uint64_t *buf = &buffers[p << 4];
buf[b] = key_val;
if (b != 15) ;
else if (o != 15) {
uint32_t *k_out = &keys_out[o - 15];
uint32_t *v_out = &vals_out[o - 15];
for (b = 0 ; b != 16 ; ++b) {
key_val = buf[b];
k_out[b] = key_val;
v_out[b] = key_val >> 32;
}
} else {
o = align_output;
while (p) o += counts[--p];
for (; o != 16 ; ++o) {
key_val = buf[o];
keys_out[o] = key_val;
vals_out[o] = key_val >> 32;
}
}
}
flush(counts, offsets, buffers, keys_out, vals_out, partitions);
}
#ifndef _NO_VECTOR_PARTITIONING
void histogram(const uint32_t *keys, size_t size, uint32_t *counts,
uint32_t factor, size_t partitions)
{
// partition vector space
uint32_t parts_space[31];
uint32_t *parts = (uint32_t *)align(parts_space);
// create masks
__mmask16 blend_0 = _mm512_int2mask(0);
__m512i mask_0 = _mm512_set1_epi32(0);
__m512i mask_1 = _mm512_set1_epi32(1);
__m512i mask_16 = _mm512_set1_epi32(16);
__m512i mask_255 = _mm512_set1_epi32(255);
__m512i mask_factor = _mm512_set1_epi32(factor);
__m512i mask_partitions = _mm512_set1_epi32(partitions);
__m512i mask_lanes = _mm512_set_epi32(15, 14, 13, 12, 11, 10, 9, 8, 7, 6, 5, 4, 3, 2, 1, 0);
// reset counts
size_t p, partitions_x16 = partitions << 4;
uint32_t all_counts_space[partitions_x16 + 127];
uint32_t *all_counts = (uint32_t *)align(all_counts_space);
for (p = 0 ; p < partitions_x16 ; p += 16)
_mm512_store_epi32(&all_counts[p], mask_0);
for (p = 0 ; p != partitions ; ++p)
counts[p] = 0;
// before alignment
const uint32_t *keys_end = &keys[size];
const uint32_t *keys_aligned = (uint32_t *)align(keys);
while (keys != keys_end && keys != keys_aligned) {
uint32_t key = *keys++;
p = (uint32_t) (key * factor);
p = (p * partitions) >> 32;
counts[p]++;
}
// aligned
keys_aligned = &keys[(keys_end - keys) & -16];
while (keys != keys_aligned) {
//printf ("align\n");
__m512i key = _mm512_load_epi32(keys);
keys += 16;
__m512i part = _mm512_mullo_epi32(key, mask_factor);
//part = _mm512_mulhi_epi32(part, mask_partitions);
part = simd_hash(part, mask_partitions);
__m512i part_lanes = _mm512_fmadd_epi32(part, mask_16, mask_lanes);
__m512i count = _mm512_i32gather_epi32(part_lanes, all_counts, 4);
__mmask16 k = _mm512_cmpeq_epi32_mask(count, mask_255);
count = _mm512_add_epi32(count, mask_1);
count = _mm512_and_epi32(count, mask_255);
_mm512_i32scatter_epi32(all_counts, part_lanes, count, 4);
if (!_mm512_kortestz(k, k)) {
_mm512_store_epi32(parts, part);
size_t mask = _mm512_kconcatlo_64(blend_0, k);
size_t b = _mm_tzcnt_64(mask);
do {
p = parts[b];
counts[p] += 256;
//b = _mm_tzcnti_64(b, mask);
mask=mask&(~(1<<b));
b = _mm_tzcnt_64(mask);
} while (b != 64);
}
}
// after alignment
while (keys != keys_end) {
uint32_t key = *keys++;
p = (uint32_t) (key * factor);
p = (p * partitions) >> 32;
counts[p]++;
}
// merge counts
for (p = 0 ; p != partitions ; ++p) {
__m512i sum = _mm512_load_epi32(&all_counts[p << 4]);
counts[p] += _mm512_reduce_add_epi32(sum);
}
#ifdef BG
size_t i;
for (i = p = 0 ; p != partitions ; ++p)
i += counts[p];
//assert(i == size);
#endif
}
void partition_shared(const uint32_t *keys, const uint32_t *vals, size_t size,
uint32_t *offsets, uint64_t *buffers, uint32_t *keys_out,
uint32_t *vals_out, uint32_t factor, size_t partitions)
{
size_t i, p;
assert(partitions > 1);
assert(keys_out == align(keys_out));
assert(vals_out == align(vals_out));
// conflict space
uint32_t conflicts_space[partitions + 15];
uint32_t *conflicts = (uint32_t *)align(conflicts_space);
// partition vector space
uint32_t parts_space[31];
uint32_t *parts = (uint32_t *)align(parts_space);
// generate masks
__m512i mask_factor = _mm512_set1_epi32(factor);
__m512i mask_partitions = _mm512_set1_epi32(partitions);
__m512i mask_pack = _mm512_set_epi32(15, 7, 14, 6, 13, 5, 12, 4, 11, 3, 10, 2, 9, 1, 8, 0);
__m512i mask_unpack = _mm512_set_epi32(15, 13, 11, 9, 7, 5, 3, 1, 14, 12, 10, 8, 6, 4, 2, 0);
__m512i mask_16 = _mm512_set1_epi32(BUFFER_SIZE);
__m512i mask_15 = _mm512_set1_epi32(BUFFER_SIZE-1);
__m512i mask_1 = _mm512_set1_epi32(1);
__mmask16 blend_AAAA = _mm512_int2mask(0xAAAA);
__mmask16 blend_5555 = _mm512_int2mask(0x5555);
__mmask16 blend_0000 = _mm512_int2mask(0x0000);
// unaligned loop
size_t size_minus_16 = size - 16;
__mmask16 k = _mm512_kxnor(k, k);
__m512i key, val;
i = 0;
if (size >= 16) do {
// load new keys and payloads
key = _mm512_mask_expandloadu_epi32 (key, k, &keys[i]);
val = _mm512_mask_expandloadu_epi32 (val, k, &vals[i]);
//_mm_prefetch((char*)&keys[i+16*PREFETCH_DISTANCE], _MM_HINT_T1);
//_mm_prefetch((char*)&vals[i+16*PREFETCH_DISTANCE], _MM_HINT_T1);
// hash keys
__m512i part = _mm512_mullo_epi32(key, mask_factor);
part = simd_hash(part, mask_partitions);
// detect conflicts
_mm512_i32scatter_epi32(conflicts, part, mask_pack, 4);
__m512i back = _mm512_i32gather_epi32(part, conflicts, 4);
size_t c = _mm512_kconcatlo_64(blend_0000, k);
k = _mm512_cmpeq_epi32_mask(back, mask_pack);//1, if equal
// split the data into low and high part
__m512i key_tmp = _mm512_permutevar_epi32(mask_pack, key);//shuffle across lanes according to the index
__m512i val_tmp = _mm512_permutevar_epi32(mask_pack, val);
__m512i lo = _mm512_mask_blend_epi32(blend_AAAA, key_tmp, _mm512_swizzle_epi32(val_tmp, _MM_SWIZ_REG_CDAB));
__m512i hi = _mm512_mask_blend_epi32(blend_5555, val_tmp, _mm512_swizzle_epi32(key_tmp, _MM_SWIZ_REG_CDAB));
// update offsets and detect conflits
__m512i offset = _mm512_mask_i32gather_epi32(offset, k, part, offsets, 4);
__m512i offset_plus_1 = _mm512_add_epi32(offset, mask_1);
_mm512_mask_i32scatter_epi32(offsets, k, part, offset_plus_1, 4);
// compute block offsets
offset = _mm512_and_epi32(offset, mask_15);
__mmask16 eq = _mm512_mask_cmpeq_epi32_mask(k, offset, mask_15);
offset = _mm512_fmadd_epi32(part, mask_16, offset);//mask_16 is the buffer size
// write interleaved keys and payloads to buffers
_mm512_mask_i32loscatter_epi64(buffers, k, offset, lo, 8);
offset = _mm512_permute4f128_epi32(offset, _MM_PERM_BCDC);
__mmask16 r_k = _mm512_kunpackb (k,k>>8);
_mm512_mask_i32loscatter_epi64(buffers, r_k, offset, hi, 8);
c = _mm_countbits_64(c);
// flush full blocks (taken ~ 65%)
if (!_mm512_kortestz(eq, eq)) {
_mm512_store_epi32(parts, part);//needs to be in cache, not stream here
size_t mask = _mm512_kconcatlo_64(blend_0000, eq);
size_t b = _mm_tzcnt_64(mask);
do {
size_t l = parts[b];
size_t o = offsets[l];
int i,loop=BUFFER_SIZE/16;
//l <<= 4;
l*=BUFFER_SIZE;//l <<= 5;
for (i=0;i<loop;++i)
{
lo = _mm512_load_epi64(&buffers[l + 0 + 16 * i]);
hi = _mm512_load_epi64(&buffers[l + 8 + 16 * i]);
key_tmp = _mm512_mask_blend_epi32(blend_AAAA, lo, _mm512_swizzle_epi32(hi, _MM_SWIZ_REG_CDAB));
val_tmp = _mm512_mask_blend_epi32(blend_5555, hi, _mm512_swizzle_epi32(lo, _MM_SWIZ_REG_CDAB));
key_tmp = _mm512_permutevar_epi32(mask_unpack, key_tmp);
val_tmp = _mm512_permutevar_epi32(mask_unpack, val_tmp);
_mm512_stream_ps(&keys_out[o - BUFFER_SIZE + 16 * i], _mm512_castsi512_ps(key_tmp));
_mm512_stream_ps(&vals_out[o - BUFFER_SIZE + 16 * i], _mm512_castsi512_ps(val_tmp));
}
mask=mask&(~(1<<b));
b = _mm_tzcnt_64(mask);
} while (b != 64);
}
i += c;
} while (i <= size_minus_16);
// store last items in stack
uint32_t tmp_key[32];
uint32_t tmp_rid[32];
k = _mm512_knot(k);
_mm512_mask_compressstoreu_epi32(&tmp_key[0], k, key);
_mm512_mask_compressstoreu_epi32(&tmp_rid[0], k, val);
size_t c = _mm_countbits_64(_mm512_kconcatlo_64(blend_0000, k));
assert(size - i + c <= 32);
for (; i != size ; ++i, ++c) {
tmp_key[c] = keys[i];
tmp_rid[c] = vals[i];
}
// partition last items using scalar code
for (i = 0 ; i != c ; ++i) {
uint64_t kv = tmp_rid[i];
kv = (kv << 32) | tmp_key[i];
p = (uint32_t) (factor * (uint32_t) kv);
p = (p * partitions) >> 32;
uint64_t *buf = &buffers[p * BUFFER_SIZE];
size_t o = offsets[p]++;
size_t b = o & (BUFFER_SIZE-1);
buf[b] = kv;
if (b == BUFFER_SIZE-1) {
int iii=0,loop=BUFFER_SIZE/16;
for (iii=0;iii<loop;++iii)
{
__m512i lo_out = _mm512_load_epi64(&buf[0+16*iii]);
__m512i hi_out = _mm512_load_epi64(&buf[8+16*iii]);
key = _mm512_mask_blend_epi32(blend_AAAA, lo_out, _mm512_swizzle_epi32(hi_out, _MM_SWIZ_REG_CDAB));
val = _mm512_mask_blend_epi32(blend_5555, hi_out, _mm512_swizzle_epi32(lo_out, _MM_SWIZ_REG_CDAB));
key = _mm512_permutevar_epi32(mask_unpack, key);
val = _mm512_permutevar_epi32(mask_unpack, val);
_mm512_stream_ps(&keys_out[o - BUFFER_SIZE + 1 +16 * iii], _mm512_castsi512_ps(key));
_mm512_stream_ps(&vals_out[o - BUFFER_SIZE + 1 +16 * iii], _mm512_castsi512_ps(val));
}
}
}
}
void partition(const uint32_t *keys, const uint32_t *vals, size_t size,
const uint32_t *counts, uint32_t *keys_out, uint32_t *vals_out,
uint32_t factor, size_t partitions)
{
size_t i, p;
#ifdef BG
uint64_t key_checksum = 0;
uint64_t val_checksum = 0;
for (i = 0 ; i != size ; ++i) {
key_checksum += keys[i];
val_checksum += vals[i];
}
#endif
// conflict space
uint32_t conflicts_space[partitions + 15];
uint32_t *conflicts = (uint32_t *)align(conflicts_space);
// partition vector space
uint32_t parts_space[31];
uint32_t *parts = (uint32_t *)align(parts_space);
// buffer space
uint64_t buffers_space[(partitions * BUFFER_SIZE) + 7];
uint64_t *buffers = (uint64_t *)align(buffers_space);
// offset space
uint32_t offsets_space[partitions + 15];
uint32_t *offsets = (uint32_t *)align(offsets_space);
// compute offset to align output
uint32_t *keys_out_aligned = (uint32_t *)align(keys_out);
uint32_t *vals_out_aligned = (uint32_t *)align(vals_out);
size_t to_align_keys = keys_out_aligned - keys_out;
size_t to_align_vals = vals_out_aligned - vals_out;
assert(to_align_keys == to_align_vals);
size_t align_output = 16 - to_align_keys;
keys_out -= align_output;
vals_out -= align_output;
// initialize offsets
for (i = p = 0 ; p != partitions ; ++p) {
offsets[p] = i + align_output;
i += counts[p];
}
//assert(i == size);
// generate masks
__m512i mask_factor = _mm512_set1_epi32(factor);
__m512i mask_partitions = _mm512_set1_epi32(partitions);
__m512i mask_pack = _mm512_set_epi32(15, 7, 14, 6, 13, 5, 12, 4, 11, 3, 10, 2, 9, 1, 8, 0);
__m512i mask_unpack = _mm512_set_epi32(15, 13, 11, 9, 7, 5, 3, 1, 14, 12, 10, 8, 6, 4, 2, 0);
__m512i mask_16 = _mm512_set1_epi32(BUFFER_SIZE);
__m512i mask_15 = _mm512_set1_epi32(BUFFER_SIZE-1);
__m512i mask_1 = _mm512_set1_epi32(1);
__mmask16 blend_AAAA = _mm512_int2mask(0xAAAA);
__mmask16 blend_5555 = _mm512_int2mask(0x5555);
__mmask16 blend_0000 = _mm512_int2mask(0x0000);
// unaligned loop
size_t size_minus_16 = size - 16;
__mmask16 k = _mm512_kxnor(k, k);
__m512i key, val;
i = 0;
if (size >= 16) do {
// load new keys and payloads
key = _mm512_mask_expandloadu_epi32 (key, k, &keys[i]);
val = _mm512_mask_expandloadu_epi32 (val, k, &vals[i]);
//_mm_prefetch((char*)&keys[i+16*PREFETCH_DISTANCE], _MM_HINT_T1);
//_mm_prefetch((char*)&vals[i+16*PREFETCH_DISTANCE], _MM_HINT_T1);
//key = _mm512_mask_loadunpacklo_epi32(key, k, &keys[i]);
//key = _mm512_mask_loadunpackhi_epi32(key, k, &keys[i + 16]);
//val = _mm512_mask_loadunpacklo_epi32(val, k, &vals[i]);
//val = _mm512_mask_loadunpackhi_epi32(val, k, &vals[i + 16]);
// hash keys
__m512i part = _mm512_mullo_epi32(key, mask_factor);
//part = _mm512_mulhi_epi32(part, mask_partitions);
part = simd_hash(part, mask_partitions);
// detect conflicts
_mm512_i32scatter_epi32(conflicts, part, mask_pack, 4);
__m512i back = _mm512_i32gather_epi32(part, conflicts, 4);
size_t c = _mm512_kconcatlo_64(blend_0000, k);
k = _mm512_cmpeq_epi32_mask(back, mask_pack);
// split the data into low and high part
__m512i key_tmp = _mm512_permutevar_epi32(mask_pack, key);
__m512i val_tmp = _mm512_permutevar_epi32(mask_pack, val);
__m512i lo = _mm512_mask_blend_epi32(blend_AAAA, key_tmp, _mm512_swizzle_epi32(val_tmp, _MM_SWIZ_REG_CDAB));
__m512i hi = _mm512_mask_blend_epi32(blend_5555, val_tmp, _mm512_swizzle_epi32(key_tmp, _MM_SWIZ_REG_CDAB));
// update offsets and detect conflits
__m512i offset = _mm512_mask_i32gather_epi32(offset, k, part, offsets, 4);
__m512i offset_plus_1 = _mm512_add_epi32(offset, mask_1);
_mm512_mask_i32scatter_epi32(offsets, k, part, offset_plus_1, 4);
// compute block offsets
offset = _mm512_and_epi32(offset, mask_15);
__mmask16 eq = _mm512_mask_cmpeq_epi32_mask(k, offset, mask_15);
offset = _mm512_fmadd_epi32(part, mask_16, offset);
// write interleaved keys and payloads to buffers
_mm512_mask_i32loscatter_epi64(buffers, k, offset, lo, 8);