-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathscatter_v1.cpp
971 lines (875 loc) · 36.9 KB
/
scatter_v1.cpp
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
// ================================================================================
// Name : scatter_v1.cpp (v1: 8 cofilters)
// Author : Felix Baessler
// Version : 25.04.2023
// Copyright : Felix Baessler, felix.baessler@gmail.com
// SEE TLDR; VERSION OF THE LICENSE: https://creativecommons.org/licenses/by-nc/4.0/legalcode
// SEE FULL LICENSE DETAILS HERE : https://creativecommons.org/licenses/by-nc/4.0/
//
// Description : Scatter
// - reads the reference data from the master file (n= ns shingles),
// - creates the fingerprint map and
// - writes the result to the map file.
// The map can be viewed as a minimalistic hash table reduced to m one-bit slots.
// Scatter will mark those slots that correspond to the hash value modulo m
// of the reference shingles (reference fingerprint).
// Run on an ordinary laptop, scatter takes about 70 seconds for ns= 1GB.
//
// Demo-String:
// For demonstration, scatter smuggles an artificial 20 byte long demo-string
// into the reference data. Gather doing the same with the test data, this
// string will be detected as a common substring (search for "Demo-String" in
// the code).
//
// Compilation flags:
// -O3 -g3 -Wall : optimization
// -Wl,--stack,0xFFFFFF : long arrays
// if required: upgrade minGW to x86_64 : 64 bit executable
//
// Include files :
// - #include "mingw.thread.h"
// - #include "mingw.mutex.h"
// - #include "mingw.condition_variable.h"
// available from:
// Standard threads implementation currently still missing on MinGW GCC on Windows
// Author: Alexander Vassilev
// https://github.com/meganz/mingw-std-threads
// ================================================================================
#include <iostream>
#include <fstream>
#include <random>
#include "mingw.thread.h"
#include "mingw.mutex.h"
#include "mingw.condition_variable.h"
using namespace std;
// time measurement
using Time = std::chrono::time_point<std::chrono::high_resolution_clock>;
Time start_timer() {
return std::chrono::high_resolution_clock::now();
}
double get_elapsed_time(Time start) {
Time end = std::chrono::high_resolution_clock::now();
std::chrono::duration<double> d = end - start;
std::chrono::microseconds us = std::chrono::duration_cast<std::chrono::microseconds>(d);
return us.count() / 1000.0f;
}
// random cyclic permutations
void rcp_generator(std::mt19937& mt_rand, uint8_t p[]);
// file names
// ----------
// input: read S from the master file
const string master_string_file_name= "C:\\cr\\master.txt";
// output: write the hash map
const string map_file_name_prefix= "C:\\cr\\v1_map_";
// batch size of input buffers and hash values (cf. CONTAINERS)
#define BATCH_SIZE (8*1024)
// GLOBAL PARAMETERS: same values in scatter and gather!
// =================
#define DV 8 // number of diversified hashes == 8 for this implementation (byte packed!!)
#define L 5 // shingle length L
#define LC (L-1) // shingle carry length : LC == L - 1
#define ns 1000000000ULL // length of the reference string s [bytes]
// n is lengthened by the first L-1 test bytes (overlap with reference/test string)
#define n ns // number of reference shingles
#define M_COM 1000000007ULL // modulus of the common hashes
#define B_COM 257ULL // base of the common hashes (first prime > 256)
#define M_DIV 67ULL
// scatter_v1: diversified fingerprint bases
// -----------------------------------------
// 1 cache-line ( 64 bytes)
const uint64_t B_DIV[DV]= {257, 263, 269, 271, 277, 281, 283, 293};
// C_COM= (B_COM ^ L) % M_COM
auto c_com= [](){
uint64_t result = 1ULL;
for (int k = 0; k < L; k++) {
result *= B_COM;
result %= M_COM;
}
return result;
};
const uint64_t C_COM= c_com();
// C_DIV= (B_DIV[id] ^ L) % M_DIV
auto c_div= [](int id){
uint64_t result = 1ULL;
for (int k = 0; k < L; k++) {
result *= B_DIV[id];
result %= M_DIV;
}
return result;
};
const uint64_t C_DIV[DV]= {c_div(0), c_div(1), c_div(2), c_div(3),
c_div(4), c_div(5), c_div(6), c_div(7)};
// THREADS (1,2,3) working on three containers (A,B,C)
// =======
// WORKER 1 : read input
void worker1_thread();
// fill the batch from the reference file
mutex mx1;
condition_variable cv1;
bool cv1_scheduler_enabled= false;
bool cv1_worker1_enabled= false;
double worker1_process_time;
double worker1_waiting_time;
// WORKER 2 : produce hash
void worker2_thread();
// hash the shingles of the batch
void hash_batch(uint8_t s[], uint32_t hash_count, uint64_t com_hash[], uint8_t div_hash[]);
mutex mx2;
condition_variable cv2;
bool cv2_scheduler_enabled= false;
bool cv2_worker2_enabled= false;
double worker2_process_time;
double worker2_waiting_time;
// WORKER 3 : consume hash
void worker3_thread();
// record in the hash map the hash values of the batch
mutex mx3;
condition_variable cv3;
bool cv3_scheduler_enabled= false;
bool cv3_worker3_enabled= false;
double worker3_process_time;
double worker3_waiting_time;
// CONTAINERS (A,B,C)
// ==================
// CONTAINER A
bool ctr_A_busy= false; // true: container A busy (a worker is processing its contents)
uint8_t buf_ctr_A[BATCH_SIZE + LC]; // string buffer: length= buffer size + carry length
uint64_t com_ctr_A[BATCH_SIZE]; // hash buffer : common hashes
uint8_t div_ctr_A[BATCH_SIZE * DV]; // hash buffer : diversified hashes (DV number of cofilters)
// CONTAINER B
bool ctr_B_busy= false; // true: container B busy
uint8_t buf_ctr_B[BATCH_SIZE + LC];
uint64_t com_ctr_B[BATCH_SIZE];
uint8_t div_ctr_B[BATCH_SIZE * DV];
// CONTAINER C
bool ctr_C_busy= false; // true: container C busy
uint8_t buf_ctr_C[BATCH_SIZE + LC];
uint64_t com_ctr_C[BATCH_SIZE];
uint8_t div_ctr_C[BATCH_SIZE * DV];
// THREAD INTERFACE
// ================
// hash map
uint8_t *map;
// cyclic permutation vector
uint8_t shuffle[256];
// ****************************************************************************************************************************
int main() {
uint32_t stage_id; // current stage
uint32_t batch_count; // total number of required batches (>= 3)
// compatible map file: same M_COM, M_DIV and L in scatter and gather!!
string map_file_name= map_file_name_prefix
+ to_string(M_DIV) + "_"
+ to_string(L) + ".txt";
// total number of batches
// -----------------------
// note that the last batch is not necessarily completely full
batch_count= n / BATCH_SIZE;
if ((n - (batch_count * BATCH_SIZE)) > 0) batch_count++;
if (batch_count < 3) exit(10);
printf("\n");
printf("scatter_v1 \n");
printf("========== \n");
printf("master file : %s \n", master_string_file_name.c_str() );
printf("map file : %s \n", map_file_name.c_str());
printf("string s length ns : %llu \t(reference string) \n", ns);
printf("shingle length L : %d \n", L);
printf("carry length LC : %d \n", LC);
printf("batch count : %d \n", batch_count);
printf("batch size : %d \n", BATCH_SIZE);
printf("common modulus : %llu \n", M_COM);
printf("diversity modulus : %llu \n", M_DIV);
printf("\n");
fflush(stdout);
// reset time expenditures
worker1_waiting_time= 0;
worker1_process_time= 0;
worker2_waiting_time= 0;
worker2_process_time= 0;
worker3_waiting_time= 0;
worker3_process_time= 0;
// hash map allocation / reset
// ---------------------------
map= (uint8_t *)malloc(M_COM + M_DIV); if (map == NULL) exit(11);
// reset hash map
memset(map, 0b11111111, M_COM + M_DIV);
// random number initialization with current time
// ==============================================
//mt19937 mt_rand(123456789);
time_t cur_time;
mt19937 mt_rand(time(&cur_time));
//printf("map setup_time : %s \n", ctime(&cur_time));
// generate random cyclic permutations: shuffle
// -----------------------------------
rcp_generator(mt_rand, shuffle);
// printf("first 20 shuffle values: \n");
// for (uint32_t i= 0; i<20; i++) printf(" %d ", shuffle[i]);
// printf("\n");
// elapsed time
double elapsed_time= 0;
Time start_elapsed_time= start_timer();
// work time
double work_time= 0;
Time start_work_time;
// schedule time
double schedule_time= 0;
Time start_schedule_time;
// overhead time
double overhead_time= 0;
Time start_overhead_time;
// start threads (they will wait for the first start-signal)
start_overhead_time= start_timer();
thread worker1(worker1_thread);
thread worker2(worker2_thread);
thread worker3(worker3_thread);
overhead_time+= get_elapsed_time(start_overhead_time);
// first stage: 1A (worker1 processes container A)
{{lock_guard<mutex> lk(mx1); cv1_worker1_enabled= true;} cv1.notify_one();}
{unique_lock<mutex> lk(mx1); cv1.wait(lk, []{return cv1_scheduler_enabled;}); cv1_scheduler_enabled= false;}
// second stage: 1B, 2A
{{lock_guard<mutex> lk(mx1); cv1_worker1_enabled= true;} cv1.notify_one();}
{{lock_guard<mutex> lk(mx2); cv2_worker2_enabled= true;} cv2.notify_one();}
{unique_lock<mutex> lk(mx1); cv1.wait(lk, []{return cv1_scheduler_enabled;}); cv1_scheduler_enabled= false;}
{unique_lock<mutex> lk(mx2); cv2.wait(lk, []{return cv2_scheduler_enabled;}); cv2_scheduler_enabled= false;}
// third stage: 1C, 2B, 3A
// etc...
for (stage_id= 3; stage_id <= batch_count; stage_id++) {
//p cout << "send start-signal to workers \n"; fflush(stdout);
// <== send start-signal to workers
start_schedule_time= start_timer();
{{lock_guard<mutex> lk(mx1); cv1_worker1_enabled= true;} cv1.notify_one();}
{{lock_guard<mutex> lk(mx2); cv2_worker2_enabled= true;} cv2.notify_one();}
{{lock_guard<mutex> lk(mx3); cv3_worker3_enabled= true;} cv3.notify_one();}
schedule_time+= get_elapsed_time(start_schedule_time);
//p cout << "wait for end-signals from workers \n"; fflush(stdout);
// <== wait for end-signal from workers
start_work_time= start_timer();
{unique_lock<mutex> lk(mx1); cv1.wait(lk, []{return cv1_scheduler_enabled;}); cv1_scheduler_enabled= false;}
{unique_lock<mutex> lk(mx2); cv2.wait(lk, []{return cv2_scheduler_enabled;}); cv2_scheduler_enabled= false;}
{unique_lock<mutex> lk(mx3); cv3.wait(lk, []{return cv3_scheduler_enabled;}); cv3_scheduler_enabled= false;}
work_time+= get_elapsed_time(start_work_time);
}
// second last stage (batch_count + 1): 2x, 3x
{{lock_guard<mutex> lk(mx2); cv2_worker2_enabled= true;} cv2.notify_one();}
{{lock_guard<mutex> lk(mx3); cv3_worker3_enabled= true;} cv3.notify_one();}
{unique_lock<mutex> lk(mx2); cv2.wait(lk, []{return cv2_scheduler_enabled;}); cv2_scheduler_enabled= false;}
{unique_lock<mutex> lk(mx3); cv3.wait(lk, []{return cv3_scheduler_enabled;}); cv3_scheduler_enabled= false;}
// last stage (batch_count + 2): 3y
{{lock_guard<mutex> lk(mx3); cv3_worker3_enabled= true;} cv3.notify_one();}
{unique_lock<mutex> lk(mx3); cv3.wait(lk, []{return cv3_scheduler_enabled;}); cv3_scheduler_enabled= false;}
// end threads
start_overhead_time= start_timer();
worker1.join();
worker2.join();
worker3.join();
overhead_time+= get_elapsed_time(start_overhead_time);
elapsed_time= get_elapsed_time(start_elapsed_time);
// result
// ======
// write hash map to disk
// ----------------------
ofstream map_output_stream(map_file_name, ios::binary);
if (!map_output_stream) cerr << "Can't open map output file!";
map_output_stream.write((char *)&cur_time, (int)sizeof(time_t));
map_output_stream.write((char *)map, M_COM + M_DIV);
map_output_stream.close();
printf("\nmap setup_time : %s \n", ctime(&cur_time));
// printf("first 20 map values: \n");
// for (uint32_t i= 0; i<20; i++) printf(" %d ", map[i]);
// printf("\n");
// time expenditure
// ================
printf("\n");
printf("time expenditure [milliseconds]\n");
printf("---------------- \n");
printf("elapsed : %9.0f \n", elapsed_time);
printf("work : %9.0f \n", work_time);
printf("schedule : %9.0f \n", schedule_time);
printf("overhead : %9.0f \n", overhead_time);
printf("worker1 : %9.0f \n", worker1_waiting_time + worker1_process_time);
printf(" - wait : %9.0f \n", worker1_waiting_time);
printf(" - process : %9.0f \n", worker1_process_time);
printf("worker2 : %9.0f \n", worker2_waiting_time + worker2_process_time);
printf(" - wait : %9.0f \n", worker2_waiting_time);
printf(" - process : %9.0f \n", worker2_process_time);
printf("worker3 : %9.0f \n", worker3_waiting_time + worker3_process_time);
printf(" - wait : %9.0f \n", worker3_waiting_time);
printf(" - process : %9.0f \n", worker3_process_time);
fflush(stdout);
}
// ****************************************************************************************************************************
void worker1_thread()
{
SetThreadAffinityMask(GetCurrentThread(), 7ULL);
Time start_time; // start of time measurement
uint8_t *buffer; // byte string buffer
uint8_t *input_buffer; // start at: buffer + LC (carry length)
uint8_t *carry_buffer; // start at: buffer + BATCH_SIZE (buffer size)
uint32_t batch_id; // current batch
uint32_t batch_size; // current batch size
uint32_t last_batch_size; // batch size of the last batch
uint32_t batch_count; // total number of required batches
uint32_t demo_batch_id; // "Demo-String"
batch_count= n / BATCH_SIZE;
demo_batch_id= batch_count / 2;
last_batch_size= n - (batch_count * BATCH_SIZE);
if (last_batch_size > 0) batch_count++;
else last_batch_size= BATCH_SIZE;
if (batch_count == 1) {
// the first batch is also the last one
batch_size= last_batch_size;
} else {
// for full batches:
batch_size= BATCH_SIZE;
}
// attach input stream to master file
ifstream string_input_stream(master_string_file_name, ios::in|ios::binary|ios::ate);
// check stream status
if (!string_input_stream) cerr << "Can't open master file!";
// get/check length of master file
string_input_stream.seekg (0, string_input_stream.end);
// check file size:
if ((uint64_t)string_input_stream.tellg() < ns) {
printf("master file length < ns : %llu \n", ns);
fflush(stdout);
exit(12);
}
fflush(stdout);
// set first artificial carry (skipped by worker3 -> first_batch_j0= LC)
// ---------------------------------------------------------------------
// set position of the input stream at the begin of S0
string_input_stream.seekg (0, string_input_stream.beg);
// locate current carry buffer
carry_buffer= buf_ctr_C + BATCH_SIZE;
// fill carry with dummy values
for (uint32_t j= 0; j < LC; j++) {
carry_buffer[j]= 0;
}
batch_id= 1;
while (true) { // as long as batch_id <= batch_count
// worker1 container A
// ===================
//p cout << "worker1 waits for start-signal from scheduler \n"; fflush(stdout);
// <== worker1 waits for start-signal from scheduler
start_time= start_timer();
{unique_lock<mutex> lk(mx1); cv1.wait(lk, []{return cv1_worker1_enabled;}); cv1_worker1_enabled= false;}
worker1_waiting_time+= get_elapsed_time(start_time);
start_time= start_timer();
//p cout << "worker1 processes container A \n"; fflush(stdout);
//p cout << "2a "; fflush(stdout);
if (ctr_A_busy) exit(13); else ctr_A_busy= true;
// ***********************************************************
// worker1 produces/processes the current batch in container A
// ***********************************************************
// define current buffer
buffer= buf_ctr_A;
// move carry from the previous buffer (buf_ctr_C) to the beginning of the current buffer
for (uint32_t i= 0; i < LC; i++) buffer[i]= carry_buffer[i];
// define current carry buffer
carry_buffer= buffer + BATCH_SIZE;
// define current input buffer
input_buffer= buffer + LC;
// fill input buffer
string_input_stream.read((char *)input_buffer, batch_size);
// check number of bytes read
if (batch_size != string_input_stream.gcount()) exit(14);
// shuffle
for (uint32_t j= 0; j < batch_size; j++) {
input_buffer[j]= shuffle[input_buffer[j]];
}
///*
// "Demo-String"
if (batch_id == demo_batch_id) {
for (uint32_t j= 0; j < 20; j++) {
input_buffer[j]= 0;
}
}
//*/
// this_thread::sleep_for(chrono::milliseconds(200));
//shuffle cout << "worker1 completed container A \n"; fflush(stdout);
//p cout << "2A "; fflush(stdout);
ctr_A_busy= false;
//p cout << "worker1 sends end-signal to scheduler \n"; fflush(stdout);
// ==> worker1 sends end-signal to scheduler
{lock_guard<mutex> lk(mx1); cv1_scheduler_enabled= true;} cv1.notify_one();
worker1_process_time+= get_elapsed_time(start_time);
if (batch_id == batch_count) {
// the current batch was the last one
cout << "worker1 terminates on A \n"; fflush(stdout);
return;
}
if (++batch_id == batch_count) {
// the next batch is the last one
// the last container is not necessarily completely full
//p cout << "worker1 last batch on B \n"; fflush(stdout);
batch_size= last_batch_size;
}
// worker1 container B
// ===================
//p cout << "worker1 waits for start-signal from scheduler \n"; fflush(stdout);
// <== worker1 waits for start-signal from scheduler
start_time= start_timer();
{unique_lock<mutex> lk(mx1); cv1.wait(lk, []{return cv1_worker1_enabled;}); cv1_worker1_enabled= false;}
worker1_waiting_time+= get_elapsed_time(start_time);
start_time= start_timer();
//p cout << "worker1 processes container B \n"; fflush(stdout);
//p cout << "2b "; fflush(stdout);
if (ctr_B_busy) exit(15); else ctr_B_busy= true;
// ***********************************************************
// worker1 produces/processes the current batch in container B
// ***********************************************************
// define current buffer
buffer= buf_ctr_B;
// move carry from the previous buffer (buf_ctr_A) to the beginning of the current buffer
for (uint32_t i= 0; i < LC; i++) buffer[i]= carry_buffer[i];
// define current carry buffer
carry_buffer= buffer + BATCH_SIZE;
// define current input buffer
input_buffer= buffer + LC;
// fill input buffer
string_input_stream.read((char *)input_buffer, batch_size);
// check number of bytes read
if (batch_size != string_input_stream.gcount()) exit(16);
// shuffle
for (uint32_t j= 0; j < batch_size; j++) {
input_buffer[j]= shuffle[input_buffer[j]];
}
///*
// "Demo-String"
if (batch_id == demo_batch_id) {
for (uint32_t j= 0; j < 20; j++) {
input_buffer[j]= 0;
}
}
//*/
// this_thread::sleep_for(chrono::milliseconds(200));
//shuffle cout << "worker1 completed container B \n"; fflush(stdout);
//p cout << "2B "; fflush(stdout);
ctr_B_busy= false;
//p cout << "worker1 sends end-signal to scheduler \n"; fflush(stdout);
// ==> worker1 sends end-signal to scheduler
{lock_guard<mutex> lk(mx1); cv1_scheduler_enabled= true;} cv1.notify_one();
worker1_process_time+= get_elapsed_time(start_time);
if (batch_id == batch_count) {
// the current batch was the last one
cout << "worker1 terminates on B \n"; fflush(stdout);
return;
}
if (++batch_id == batch_count) {
// the next batch is the last one
// the last container is not necessarily completely full
//p cout << "worker1 last batch on C \n"; fflush(stdout);
batch_size= last_batch_size;
}
// worker1 container C
// ===================
//p cout << "worker1 waits for start-signal from scheduler \n"; fflush(stdout);
// <== worker1 waits for start-signal from scheduler
start_time= start_timer();
{unique_lock<mutex> lk(mx1); cv1.wait(lk, []{return cv1_worker1_enabled;}); cv1_worker1_enabled= false;}
worker1_waiting_time+= get_elapsed_time(start_time);
start_time= start_timer();
//p cout << "worker1 processes container C \n"; fflush(stdout);
//p cout << "2c "; fflush(stdout);
if (ctr_C_busy) exit(17); else ctr_C_busy= true;
// ***********************************************************
// worker1 produces/processes the current batch in container C
// ***********************************************************
// define current buffer
buffer= buf_ctr_C;
// move carry from the previous buffer (buf_ctr_B) to the beginning of the current buffer
for (uint32_t i= 0; i < LC; i++) buffer[i]= carry_buffer[i];
// define current carry buffer
carry_buffer= buffer + BATCH_SIZE;
// define current input buffer
input_buffer= buffer + LC;
// fill input buffer
string_input_stream.read((char *)input_buffer, batch_size);
// check number of bytes read
if (batch_size != string_input_stream.gcount()) exit(18);
// shuffle
for (uint32_t j= 0; j < batch_size; j++) {
input_buffer[j]= shuffle[input_buffer[j]];
}
///*
// "Demo-String"
if (batch_id == demo_batch_id) {
for (uint32_t j= 0; j < 20; j++) {
input_buffer[j]= 0;
}
}
//*/
// this_thread::sleep_for(chrono::milliseconds(200));
//p cout << "worker1 completed container C \n"; fflush(stdout);
//p cout << "2C "; fflush(stdout);
ctr_C_busy= false;
//p cout << "worker1 sends end-signal to scheduler \n"; fflush(stdout);
// ==> worker1 sends end-signal to scheduler
{lock_guard<mutex> lk(mx1); cv1_scheduler_enabled= true;} cv1.notify_one();
worker1_process_time+= get_elapsed_time(start_time);
if (batch_id == batch_count) {
// the current batch was the last one
cout << "worker1 terminates on C \n"; fflush(stdout);
return;
}
if (++batch_id == batch_count) {
// the next batch is the last one
// the last container is not necessarily completely full
//p cout << "worker1 last batch on A \n"; fflush(stdout);
batch_size= last_batch_size;
}
}
}
void worker2_thread()
{
SetThreadAffinityMask(GetCurrentThread(), 7ULL);
Time start_time; // start of time measurement
uint32_t batch_id; // current batch
uint32_t batch_size; // current batch size
uint32_t last_batch_size; // batch size of the last batch
uint32_t batch_count; // total number of required batches
batch_count= n / BATCH_SIZE;
last_batch_size= n - (batch_count * BATCH_SIZE);
if (last_batch_size > 0) batch_count++;
else last_batch_size= BATCH_SIZE;
if (batch_count == 1) {
// the first batch is also the last one
batch_size= last_batch_size;
} else {
// for full batches:
batch_size= BATCH_SIZE;
}
batch_id= 1;
while (true) { // as long as batch_id <= batch_count
// worker2 container A
// ===================
//p cout << "worker2 waits for start-signal from scheduler \n"; fflush(stdout);
// <== worker2 waits for start-signal from scheduler
start_time= start_timer();
{unique_lock<mutex> lk(mx2); cv2.wait(lk, []{return cv2_worker2_enabled;}); cv2_worker2_enabled= false;}
worker2_waiting_time+= get_elapsed_time(start_time);
start_time= start_timer();
//p cout << "worker2 processes container A \n"; fflush(stdout);
//p cout << "1a "; fflush(stdout);
if (ctr_A_busy) exit(19); else ctr_A_busy= true;
// ***********************************************************
// worker2 produces/processes the current batch in container A
// ***********************************************************
hash_batch(buf_ctr_A, batch_size, com_ctr_A, div_ctr_A);
// this_thread::sleep_for(chrono::milliseconds(100));
//p cout << "worker2 completed container A \n"; fflush(stdout);
//p cout << "1A "; fflush(stdout);
ctr_A_busy= false;
//p cout << "worker2 sends end-signal to scheduler \n"; fflush(stdout);
// ==> worker2 sends end-signal to scheduler
{lock_guard<mutex> lk(mx2); cv2_scheduler_enabled= true;} cv2.notify_one();
worker2_process_time+= get_elapsed_time(start_time);
if (batch_id == batch_count) {
// the current batch was the last one
cout << "worker2 terminates on A \n"; fflush(stdout);
return;
}
if (++batch_id == batch_count) {
// the next batch is the last one
// the last container is not necessarily completely full
//p cout << "worker2 last batch on B \n"; fflush(stdout);
batch_size= last_batch_size;
}
// worker2 container B
// ===================
//p cout << "worker2 waits for start-signal from scheduler \n"; fflush(stdout);
// <== worker2 waits for start-signal from scheduler
start_time= start_timer();
{unique_lock<mutex> lk(mx2); cv2.wait(lk, []{return cv2_worker2_enabled;}); cv2_worker2_enabled= false;}
worker2_waiting_time+= get_elapsed_time(start_time);
start_time= start_timer();
//p cout << "worker2 processes container B \n"; fflush(stdout);
//p cout << "1b "; fflush(stdout);
if (ctr_B_busy) exit(20); else ctr_B_busy= true;
// ***********************************************************
// worker2 produces/processes the current batch in container B
// ***********************************************************
hash_batch(buf_ctr_B, batch_size, com_ctr_B, div_ctr_B);
// this_thread::sleep_for(chrono::milliseconds(100));
//p cout << "worker2 completed container B \n"; fflush(stdout);
//p cout << "1B "; fflush(stdout);
ctr_B_busy= false;
//p cout << "worker2 sends end-signal to scheduler \n"; fflush(stdout);
// ==> worker2 sends end-signal to scheduler
{lock_guard<mutex> lk(mx2); cv2_scheduler_enabled= true;} cv2.notify_one();
worker2_process_time+= get_elapsed_time(start_time);
if (batch_id == batch_count) {
// the current batch was the last one
cout << "worker2 terminates on B \n"; fflush(stdout);
return;
}
if (++batch_id == batch_count) {
// the next batch is the last one
// the last container is not necessarily completely full
//p cout << "worker2 last batch on C \n"; fflush(stdout);
batch_size= last_batch_size;
}
// worker2 container C
// ===================
//p cout << "worker2 waits for start-signal from scheduler \n"; fflush(stdout);
// <== worker2 waits for start-signal from scheduler
start_time= start_timer();
{unique_lock<mutex> lk(mx2); cv2.wait(lk, []{return cv2_worker2_enabled;}); cv2_worker2_enabled= false;}
worker2_waiting_time+= get_elapsed_time(start_time);
start_time= start_timer();
//p cout << "worker2 processes container C \n"; fflush(stdout);
//p cout << "1c "; fflush(stdout);
if (ctr_C_busy) exit(21); else ctr_C_busy= true;
// ***********************************************************
// worker2 produces/processes the current batch in container C
// ***********************************************************
hash_batch(buf_ctr_C, batch_size, com_ctr_C, div_ctr_C);
// this_thread::sleep_for(chrono::milliseconds(100));
//p cout << "worker2 completed container C \n"; fflush(stdout);
//p cout << "1C "; fflush(stdout);
ctr_C_busy= false;
//p cout << "worker2 sends end-signal to scheduler \n"; fflush(stdout);
// ==> worker2 sends end-signal to scheduler
{lock_guard<mutex> lk(mx2); cv2_scheduler_enabled= true;} cv2.notify_one();
worker2_process_time+= get_elapsed_time(start_time);
if (batch_id == batch_count) {
// the current batch was the last one
cout << "worker2 terminates on C \n"; fflush(stdout);
return;
}
if (++batch_id == batch_count) {
// the next batch is the last one
// the last container is not necessarily completely full
//p cout << "worker2 last batch on A \n"; fflush(stdout);
batch_size= last_batch_size;
}
}
//p cout << "end thread 1 \n"; fflush(stdout);
}
void worker3_thread()
{
SetThreadAffinityMask(GetCurrentThread(), 8ULL);
Time start_time; // start of time measurement
uint32_t batch_id; // current batch
uint32_t first_batch_j0= LC;// skip first (artificial) carry <-------------
uint32_t batch_size; // current batch size
uint32_t last_batch_size; // batch size of the last batch
uint32_t batch_count; // total number of required batches
batch_count= n / BATCH_SIZE;
last_batch_size= n - (batch_count * BATCH_SIZE);
if (last_batch_size > 0) batch_count++;
else last_batch_size= BATCH_SIZE;
if (batch_count == 1) {
// the first batch is also the last one
batch_size= last_batch_size;
} else {
// for full batches:
batch_size= BATCH_SIZE;
}
batch_id= 1;
while (true) { // as long as batch_id <= batch_count
// worker3 container A
// ===================
//p cout << "worker3 waits for start-signal from scheduler \n"; fflush(stdout);
// <== worker3 waits for start-signal from scheduler
start_time= start_timer();
{unique_lock<mutex> lk(mx3); cv3.wait(lk, []{return cv3_worker3_enabled;}); cv3_worker3_enabled= false;}
worker3_waiting_time+= get_elapsed_time(start_time);
start_time= start_timer();
//p cout << "worker3 processes container A \n"; fflush(stdout);
//p cout << "3a "; fflush(stdout);
if (ctr_A_busy) exit(22); else ctr_A_busy= true;
// ***********************************************************
// worker3 produces/processes the current batch in container A
// ***********************************************************
// record in the hash map the hash values of the shingle j
for (uint32_t j= first_batch_j0; j < batch_size; j++) {
// if it doesnt help, it doesnt hurt?
for (uint64_t i= 0; i < ((M_DIV + 32) / 64); i++) {
__builtin_prefetch (&com_ctr_A[j] + i * 64, 1, 3);
}
// keep track of the hash occurrence (TIME CRITICAL)
for (uint8_t id= 0; id < DV; id++) {
// ClearBit
map[com_ctr_A[j] + div_ctr_A[j*DV+id]] &= ~(1<<id);
}
}
// skip only the first (artificial) carry
first_batch_j0= 0;
// this_thread::sleep_for(chrono::milliseconds(300));
//p cout << "worker3 completed container A \n"; fflush(stdout);
//p cout << "3A "; fflush(stdout);
ctr_A_busy= false;
//p cout << "worker3 sends end-signal to scheduler \n"; fflush(stdout);
// ==> worker3 sends end-signal to scheduler
{lock_guard<mutex> lk(mx3); cv3_scheduler_enabled= true;} cv3.notify_one();
worker3_process_time+= get_elapsed_time(start_time);
if (batch_id == batch_count) {
// the current batch was the last one
cout << "worker3 terminates on A \n"; fflush(stdout);
return;
}
if (++batch_id == batch_count) {
// the next batch is the last one
// the last container is not necessarily completely full
//p cout << "worker3 last batch on B \n"; fflush(stdout);
batch_size= last_batch_size;
}
// worker3 container B
// ===================
//p cout << "worker3 waits for start-signal from scheduler \n"; fflush(stdout);
// <== worker3 waits for start-signal from scheduler
start_time= start_timer();
{unique_lock<mutex> lk(mx3); cv3.wait(lk, []{return cv3_worker3_enabled;}); cv3_worker3_enabled= false;}
worker3_waiting_time+= get_elapsed_time(start_time);
start_time= start_timer();
//p cout << "worker3 processes container B \n"; fflush(stdout);
//p cout << "3b "; fflush(stdout);
if (ctr_B_busy) exit(23); else ctr_B_busy= true;
// ***********************************************************
// worker3 produces/processes the current batch in container B
// ***********************************************************
// record in the hash map the hash values of the shingle j
for (uint32_t j= 0; j < batch_size; j++) {
// if it doesnt help, it doesnt hurt?
for (uint64_t i= 0; i < ((M_DIV + 32) / 64); i++) {
__builtin_prefetch (&com_ctr_B[j] + i * 64, 1, 3);
}
// keep track of the hash occurrence (TIME CRITICAL)
for (uint8_t id= 0; id < DV; id++) {
// ClearBit
map[com_ctr_B[j] + div_ctr_B[j*DV+id]] &= ~(1<<id);
}
}
// this_thread::sleep_for(chrono::milliseconds(300));
//p cout << "worker3 completed container B \n"; fflush(stdout);
//p cout << "3B "; fflush(stdout);
ctr_B_busy= false;
//p cout << "worker3 sends end-signal to scheduler \n"; fflush(stdout);
// ==> worker3 sends end-signal to scheduler
{lock_guard<mutex> lk(mx3); cv3_scheduler_enabled= true;} cv3.notify_one();
worker3_process_time+= get_elapsed_time(start_time);
if (batch_id == batch_count) {
// the current batch was the last one
cout << "worker3 terminates on B \n"; fflush(stdout);
return;
}
if (++batch_id == batch_count) {
// the next batch is the last one
// the last container is not necessarily completely full
//p cout << "worker3 last batch on C \n"; fflush(stdout);
batch_size= last_batch_size;
}
// worker3 container C
// ===================
//p cout << "worker3 waits for start-signal from scheduler \n"; fflush(stdout);
// <== worker3 waits for start-signal from scheduler
start_time= start_timer();
{unique_lock<mutex> lk(mx3); cv3.wait(lk, []{return cv3_worker3_enabled;}); cv3_worker3_enabled= false;}
worker3_waiting_time+= get_elapsed_time(start_time);
start_time= start_timer();
//p cout << "worker3 processes container C \n"; fflush(stdout);
//p cout << "3c "; fflush(stdout);
if (ctr_C_busy) exit(24); else ctr_C_busy= true;
// ***********************************************************
// worker3 produces/processes the current batch in container C
// ***********************************************************
// record in the hash map the hash values of the shingle j
for (uint32_t j= 0; j < batch_size; j++) {
// if it doesnt help, it doesnt hurt?
for (uint64_t i= 0; i < ((M_DIV + 32) / 64); i++) {
__builtin_prefetch (&com_ctr_C[j] + i * 64, 1, 3);
}
// keep track of the hash occurrence (TIME CRITICAL)
for (uint8_t id= 0; id < DV; id++) {
// ClearBit
map[com_ctr_C[j] + div_ctr_C[j*DV+id]] &= ~(1<<id);
}
}
// this_thread::sleep_for(chrono::milliseconds(300));
//p cout << "worker3 completed container C \n"; fflush(stdout);
//p cout << "3C "; fflush(stdout);
ctr_C_busy= false;
//p cout << "worker3 sends end-signal to scheduler \n"; fflush(stdout);
// ==> worker3 sends end-signal to scheduler
{lock_guard<mutex> lk(mx3); cv3_scheduler_enabled= true;} cv3.notify_one();
worker3_process_time+= get_elapsed_time(start_time);
if (batch_id == batch_count) {
// the current batch was the last one
cout << "worker3 terminates on C \n"; fflush(stdout);
return;
}
if (++batch_id == batch_count) {
// the next batch is the last one
// the last container is not necessarily completely full
//p cout << "worker3 last batch on A \n"; fflush(stdout);
batch_size= last_batch_size;
}
}
//p cout << "end thread 3 \n"; fflush(stdout);
}
// ****************************************************************************************************************************
inline void update_div_hashes(uint8_t hash[], uint8_t hash1[], const uint8_t s[]) {
for (uint8_t id= 0; id < DV; id++) {
hash1[id]= hash[id]= (256*M_DIV + s[L] + hash[id] * B_DIV[id] - C_DIV[id] * s[0]) % M_DIV;
}
}
void hash_batch(
uint8_t s[], // input : current string buffer
uint32_t hash_count, // input : number of hashes
uint64_t com_hash[], // output: batch of (hash_count) common hashes
uint8_t div_hash[]) // output: batch of (hash_count * DV) diversified hashes
{
// produce batch of hashes (common & diversified)
// for the shingles in the current input buffer s
// compute diversified hashes
// --------------------------
// compute the hashes of the first, leftmost shingles
for (uint8_t id= 0; id < DV; id++) {
div_hash[id]= 0;
for (uint32_t j= 0; j < L; j++) {
div_hash[id]= (div_hash[id] * B_DIV[id] + s[j]) % M_DIV;
}
div_hash[DV+id]= div_hash[id];
}
// compute the hashes of the following shingles (rolling forward)
// note: j= 1!
for (uint32_t j= 1; j < hash_count; j++) {
update_div_hashes(&div_hash[j*DV], &div_hash[(j+1)*DV], s+j-1);
}
// compute common hashes
// ---------------------
// compute the hashes of the first, leftmost shingle
com_hash[0]= 0;
for (uint32_t j= 0; j < L; j++) {
com_hash[0]= (com_hash[0] * B_COM + s[j]) % M_COM;
}
// compute the hashes of the following shingles in the buffer
for (uint32_t j= 0; j < hash_count; j++) {
com_hash[j+1]= ((com_hash[j] + M_COM) * B_COM - C_COM * s[j] + s[j+L]) % M_COM;
}
}
// *********************************************************************************************************************************************
void rcp_generator(std::mt19937& mt_rand, uint8_t p[]) {
std::uniform_int_distribution<uint8_t> dist(0, 255);
// p: random cyclic permutations (see Sattolo / FisherYates)
int i, j;
uint8_t rand; // random number
bool a[256]; // assigned random number
for (j= 0; j < 256; j++) a[j]= false;
for (j= 0; j < 256; j++) {
rand= dist(mt_rand);
while (a[rand]) {rand++;}
a[rand]= true;
p[j]= rand;
}
// test only
for (j= 0; j < 256; j++ ) {
for (i= j+1; i < 256; i++ ) {
if (p[j] == p[i]) {
printf("RCP ERROR\n");
exit(25);
}
}
}
}
// *********************************************************************************************************************************************