-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain_query10_parallel.c
More file actions
763 lines (734 loc) · 33.8 KB
/
Copy pathmain_query10_parallel.c
File metadata and controls
763 lines (734 loc) · 33.8 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
//*"SIMPLIFIED 2 SERVERS QUERY 10 TPC-H - HASH JOIN"*
// Implementation: Marisa Sel Franco *
// HiPES - UFPR *
//***************************************************
// Date | Description *
//***************************************************
// 11/19/20 | Start of implementation *
// 01/28/21 | End of implementation *
//***************************************************
#include "header.h"
int main(int argc, char *argv[]){ // receives arguments via terminal
long int l_tab1, l_tab2, tuples_counter = 0, tuples_counter_a = 0, tuples_counter_b = 0;
uint32_t i = 0, j = 0, l = 0, size;
int hits_counter_a[66], hits_counter_b[66], k = 0;
FILE *input_tab, *output_file; // declare pointer to the files
char line[1000], *token = NULL, *result = NULL, *p, buffer[25], file_name[12] = "tempos";
htype *table_a, *table_b;
ctype counter_a, counter_b;
clock_t ticks[2], totalticks[2];
double elapsedtime, time_register[15];
uint32_t *out_custkey_a, *out_custkey_b;
char **out_name_a, **out_name_b;
// FAZER TRATAMENTO DE ERROS PARA NÚMERO E TIPOS DE ARGUMENTOS
totalticks[0] = ticks[0] = clock();
l_tab1 = strtol(argv[3], &p, 10); // number of rows from the first table
l_tab2 = strtol(argv[5], &p, 10); // number of rows from the second table
if (l_tab1 > l_tab2){ // if malloc returns NULL, there is not enough memory
printf("\nErro. Você inseriu os parâmetros da forma errada.\n");
printf("As informações da menor tabela devem ser colocadas antes dos parâmetros da maior tabela.\n");
printf("Exemplo: ./query10parallel murmur3 customer.tbl 150000 orders.tbl 1500000 8191\n");
printf("\nO programa será encerrado.\n");
exit(1);
}
size = strtol(argv[6], &p, 10); // hash table's size
ticks[1] = clock();
elapsedtime = (ticks[1] - ticks[0]) * 1000.0 / CLOCKS_PER_SEC;
time_register[k] = elapsedtime;
k++;
//TIME STAMP
printf("Tempo decorrido para armazenar parâmetros passados: %g ms\n\n", elapsedtime);
ticks[0] = clock();
// ALLOCATION OF MEMORY SPACES
c_custkey = malloc(l_tab1 * sizeof(uint32_t)); // SF*150000 are populated
if (c_custkey == NULL){ // if malloc returns NULL, there is not enough memory
printf("Erro. Espaço de memória insuficiente.\n");
exit(1);
}
c_name = (char **)malloc(l_tab1 * sizeof(char *)); // fixed text, size 25
if (c_name == NULL){ // if malloc returns NULL, there is not enough memory
printf("Erro. Espaço de memória insuficiente.\n");
exit(1);
}
c_name[0] = (char *)malloc(sizeof(char) * 35 * l_tab1); // allocates the big vector
if (c_name[0] == NULL){ // if malloc returns NULL, there is not enough memory
printf("Erro. Espaço de memória insuficiente.\n");
exit(1);
}
for (l = 0; l < l_tab1; l++){ // rearranges the pointers
c_name[l] = c_name[0] + (l * 35 * sizeof(char));
}
c_address = (char **)malloc(l_tab1 * sizeof(char *)); // variable text, size 40
if (c_address == NULL){ // if malloc returns NULL, there is not enough memory
printf("Erro. Espaço de memória insuficiente.\n");
exit(1);
}
c_address[0] = (char *)malloc(sizeof(char) * 50 * l_tab1); // allocates the big vector
if (c_address[0] == NULL){ // if malloc returns NULL, there is not enough memory
printf("Erro. Espaço de memória insuficiente.\n");
exit(1);
}
for (l = 0; l < l_tab1; l++){ // rearranges the pointers
c_address[l] = c_address[0] + (l * 50 * sizeof(char));
}
c_nationkey = malloc(l_tab1 * sizeof(int)); // 25 nations are populated
if (c_nationkey == NULL){ // if malloc returns NULL, there is not enough memory
printf("Erro. Espaço de memória insuficiente.\n");
exit(1);
}
c_phone = (char **)malloc(l_tab1 * sizeof(char *)); // fixed text, size 15
if (c_phone == NULL){ // if malloc returns NULL, there is not enough memory
printf("Erro. Espaço de memória insuficiente.\n");
exit(1);
}
c_phone[0] = (char *)malloc(sizeof(char) * 25 * l_tab1); // allocates the big vector
if (c_phone[0] == NULL){ // if malloc returns NULL, there is not enough memory
printf("Erro. Espaço de memória insuficiente.\n");
exit(1);
}
for (l = 0; l < l_tab1; l++){ // rearranges the pointers
c_phone[l] = c_phone[0] + (l * 25 * sizeof(char));
}
c_acctbal = malloc(l_tab1 * sizeof(float)); // decimal
if (c_acctbal == NULL){ // if malloc returns NULL, there is not enough memory
printf("Erro. Espaço de memória insuficiente.\n");
exit(1);
}
c_mktsegment = (char **)malloc(l_tab1 * sizeof(char *)); // fixed text, size 10
if (c_mktsegment == NULL){ // if malloc returns NULL, there is not enough memory
printf("Erro. Espaço de memória insuficiente.\n");
exit(1);
}
c_mktsegment[0] = (char *)malloc(sizeof(char) * 20 * l_tab1); // allocates the big vector
if (c_mktsegment[0] == NULL){ // if malloc returns NULL, there is not enough memory
printf("Erro. Espaço de memória insuficiente.\n");
exit(1);
}
for (l = 0; l < l_tab1; l++){ // rearranges the pointers
c_mktsegment[l] = c_mktsegment[0] + (l * 20 * sizeof(char));
}
c_comment = (char **)malloc(l_tab1 * sizeof(char *)); // variable text, size 117
if (c_comment == NULL){ // if malloc returns NULL, there is not enough memory
printf("Erro. Espaço de memória insuficiente.\n");
exit(1);
}
c_comment[0] = (char *)malloc(sizeof(char) * 117 * l_tab1); // allocates the big vector
if (c_comment[0] == NULL){ // if malloc returns NULL, there is not enough memory
printf("Erro. Espaço de memória insuficiente.\n");
exit(1);
}
for (l = 0; l < l_tab1; l++){ // rearranges the pointers
c_comment[l] = c_comment[0] + (l * 117 * sizeof(char));
}
o_orderkey = malloc(l_tab2 * sizeof(uint32_t)); // SF*1.500.000 are sparsely populated
if (o_orderkey == NULL){ // if malloc returns NULL, there is not enough memory
printf("Erro. Espaço de memória insuficiente.\n");
exit(1);
}
o_custkey = malloc(l_tab2 * sizeof(uint32_t)); // SF*1.500.000 are populated
if (o_custkey == NULL){ // if malloc returns NULL, there is not enough memory
printf("Erro. Espaço de memória insuficiente.\n");
exit(1);
}
o_orderstatus = malloc(l_tab2 * sizeof(char *)); // fixed text, size 1
if (o_orderstatus == NULL){ // if malloc returns NULL, there is not enough memory
printf("Erro. Espaço de memória insuficiente.\n");
exit(1);
}
o_orderstatus[0] = (char *)malloc(sizeof(char) * 2 * l_tab2); // allocates the big vector
if (o_orderstatus[0] == NULL){ // if malloc returns NULL, there is not enough memory
printf("Erro. Espaço de memória insuficiente.\n");
exit(1);
}
for (l = 0; l < l_tab2; l++){ // rearranges the pointers
o_orderstatus[l] = o_orderstatus[0] + (l * 2 * sizeof(char));
}
o_totalprice = malloc(l_tab2 * sizeof(float)); // decimal
if (o_totalprice == NULL){ // if malloc returns NULL, there is not enough memory
printf("Erro. Espaço de memória insuficiente.\n");
exit(1);
}
// CHECAR SE É O MAIS ADEQUADO GUARDAR A DATA COMO UMA STRING
o_orderdate = (char **)malloc(l_tab2 * sizeof(char *)); // fixed text, size 10
if (o_orderdate == NULL){ // if malloc returns NULL, there is not enough memory
printf("Erro. Espaço de memória insuficiente.\n");
exit(1);
}
o_orderdate[0] = (char *)malloc(sizeof(char) * 20 * l_tab2); // allocates the big vector
if (o_orderdate[0] == NULL){ // if malloc returns NULL, there is not enough memory
printf("Erro. Espaço de memória insuficiente.\n");
exit(1);
}
for (l = 0; l < l_tab2; l++){ // rearranges the pointers
o_orderdate[l] = o_orderdate[0] + (l * 20 * sizeof(char));
}
o_orderpriority = (char **)malloc(l_tab2 * sizeof(char *)); // fixed text, size 15
if (o_orderpriority == NULL){ // if malloc returns NULL, there is not enough memory
printf("Erro. Espaço de memória insuficiente.\n");
exit(1);
}
o_orderpriority[0] = (char *)malloc(sizeof(char) * 20 * l_tab2); // allocates the big vector
if (o_orderpriority[0] == NULL){ // if malloc returns NULL, there is not enough memory
printf("Erro. Espaço de memória insuficiente.\n");
exit(1);
}
for (l = 0; l < l_tab2; l++){ // rearranges the pointers
o_orderpriority[l] = o_orderpriority[0] + (l * 20 * sizeof(char));
}
o_clerk = (char **)malloc(l_tab2 * sizeof(char *)); // fixed text, size 15
if (o_clerk == NULL){ // if malloc returns NULL, there is not enough memory
printf("Erro. Espaço de memória insuficiente.\n");
exit(1);
}
o_clerk[0] = (char *)malloc(sizeof(char) * 25 * l_tab2); // allocates the big vector
if (o_clerk[0] == NULL){ // if malloc returns NULL, there is not enough memory
printf("Erro. Espaço de memória insuficiente.\n");
exit(1);
}
for (l = 0; l < l_tab2; l++){ // rearranges the pointers
o_clerk[l] = o_clerk[0] + (l * 25 * sizeof(char));
}
o_shippriority = malloc(l_tab2 * sizeof(int)); // integer
if (o_shippriority == NULL){ // if malloc returns NULL, there is not enough memory
printf("Erro. Espaço de memória insuficiente.\n");
exit(1);
}
o_comment = (char **)malloc(l_tab2 * sizeof(char *)); // variable text, size 79
if (o_comment == NULL){ // if malloc returns NULL, there is not enough memory
printf("Erro. Espaço de memória insuficiente.\n");
exit(1);
}
o_comment[0] = (char *)malloc(sizeof(char) * 79 * l_tab2); // allocates the big vector
if (o_comment[0] == NULL){ // if malloc returns NULL, there is not enough memory
printf("Erro. Espaço de memória insuficiente.\n");
exit(1);
}
for (l = 0; l < l_tab2; l++){ // rearranges the pointers
o_comment[l] = o_comment[0] + (l * 79 * sizeof(char));
}
printf("Reservou os espaços de memória para as tabelas de entrada.\n");
ticks[1] = clock();
elapsedtime = (ticks[1] - ticks[0]) * 1000.0 / CLOCKS_PER_SEC;
time_register[k] = elapsedtime;
k++;
//TIME STAMP
printf("Tempo decorrido para reservar os espaços de memória para as tabelas de entrada: %g ms\n\n", elapsedtime);
ticks[0] = clock();
// STORE CUSTOMER TAB
input_tab = fopen(argv[2], "r"); // read the file that is in the element of the argv array
if (!input_tab){
printf("Erro. Não foi possível ler a primeira tabela.\n");
abort();
}
while (1){
token = NULL; // repeat until the end of the file
result = fgets(line, 1000, input_tab); // read a line from input_tab and store it into buffer_line
if (feof(input_tab))
break;
token = strtok(result, "|"); // get the first token
for (j = 0; j < 8; j++){ // repeat until the end of the line
if (j == 0 && i < l_tab1)
if (token != NULL)
c_custkey[i] = (uint32_t) atol(token); // copy token to c_custkey[i] and convert string to unsigned int 32
token = strtok(NULL, "|");
if (j == 0 && i < l_tab1){
if (token != NULL)
strcpy(c_name[i], token); // copy token to c_name[i]
}
if (j == 1 && i < l_tab1){
if (token != NULL)
strcpy(c_address[i], token); // copy token to c_address[i]
}
if (j == 2 && i < l_tab1){
if (token != NULL)
c_nationkey[i] = atoi(token); // copy token to c_nationkey[i] and convert string to integer
}
if (j == 3 && i < l_tab1){
if (token != NULL)
strcpy(c_phone[i], token); // copy token to c_phone[i]
}
if (j == 4 && i < l_tab1){
if (token != NULL)
c_acctbal[i] = atof(token); // copy token to c_acctbal[i] and convert string to float
}
if (j == 5 && i < l_tab1){
if (token != NULL)
strcpy(c_mktsegment[i], token); // copy token to c_mktsegment[i]
}
if (j == 6 && i < l_tab1){
if (token != NULL)
strcpy(c_comment[i], token); // copy token to c_comment[i]
}
}
i++; // increment the counter of the arrays that store the columns
}
fclose(input_tab); // close the file
// STORE ORDER TAB
input_tab = fopen(argv[4], "r"); // read the file that is in the element of the argv array
if (!input_tab){
printf("Erro. Não foi possível ler a segunda tabela.\n");
abort();
}
i = 0; // reset counter i
while (1){
token = NULL; // repeat until the end of the file
result = fgets(line, 1000, input_tab); // read a line from input_tab and store it into buffer_line
if (feof(input_tab))
break;
token = strtok(result, "|"); // get the first token
for (j = 0; j < 9; j++){ // repeat until the end of the line
if (j == 0 && i < l_tab2)
if (token != NULL)
o_orderkey[i] = (uint32_t) atol(token); // copy token to o_orderkey[i] and convert string to unsigned int 32
token = strtok(NULL, "|");
if (j == 0 && i < l_tab2){
if (token != NULL)
o_custkey[i] = (uint32_t) atol(token); // copy token to o_custkey[i] and convert string to unsigned int 32
}
if (j == 1 && i < l_tab2){
if (token != NULL)
strcpy(o_orderstatus[i], token); // copy token to o_orderstatus[i]
}
if (j == 2 && i < l_tab2){
if (token != NULL)
o_totalprice[i] = atol(token); // copy token to o_totalprice[i] and convert string to long int
}
if (j == 3 && i < l_tab2){
if (token != NULL)
strcpy(o_orderdate[i], token); // copy token to o_orderdate[i]
}
if (j == 4 && i < l_tab2){
if (token != NULL)
strcpy(o_orderpriority[i], token); // copy token to o_orderpriority[i]
}
if (j == 5 && i < l_tab2){
if (token != NULL)
strcpy(o_clerk[i], token); // copy token to o_clerk[i]
}
if (j == 6 && i < l_tab2){
if (token != NULL)
o_shippriority[i] = atoi(token); // copy token to o_shippriority[i] and convert string to integer
}
if (j == 7 && i < l_tab2){
if (token != NULL)
strcpy(o_comment[i], token); // copy token to o_comment[i]
}
}
i++; // increment the counter of the arrays that store the columns
}
fclose(input_tab);
printf("Armazenou os dados das tabelas de entrada.\n");
ticks[1] = clock();
elapsedtime = (ticks[1] - ticks[0]) * 1000.0 / CLOCKS_PER_SEC;
time_register[k] = elapsedtime;
k++;
//TIME STAMP
printf("Tempo decorrido para armazenar os dados das tabelas de entrada: %g ms\n\n", elapsedtime);
printf("Vai começar o trecho paralelo do código, simulando um BD distribuído.\n");
ticks[0] = clock();
// HASH JOIN
// INVALID HASH FUNCTION PARAMETER
if ((strcmp(argv[1], "murmur3") != 0) && (strcmp(argv[1], "fnv1a") != 0)){
printf("Opção de função hash inválida. O programa será encerrado.\n");
exit(1);
}
else{
// MURMURHASH3
if (strcmp(argv[1], "murmur3") == 0){
// BUILD PHASE - HASH TABLE CONSTRUCTION USING THE SMALLEST TABLE (RELATION R)
// SERVER 1 SIMULATION: deal with the first half of the data in the smallest table (relation R)
table_a = (malloc(size * sizeof(htype))); // allocation of the hash table_a vector
if (!table_a){
printf("Erro. Espaço de memória insuficiente para criação da tabela hash.\n");
exit(1);
}
init_table(table_a, size); // hash table_a initialization
init_counter(&counter_a); // counter_a initialization
uint32_t key_a, index_a, match_key_a;
for (i = 0; i < l_tab1/2; i++){
key_a = c_custkey[i]; // key_a receives c_custekey[i]
index_a = murmurhash_index(key_a, size); // "index_a" receives an index created using murmur3_32 hash
store(key_a, index_a, table_a, &counter_a); // stores c_custekey[i] in the hash table_a
}
printf("O 'servidor 1' terminou a fase de construção e criou a tabela hash A com Murmur3.\n");
ticks[1] = clock();
elapsedtime = (ticks[1] - ticks[0]) * 1000.0 / CLOCKS_PER_SEC;
time_register[k] = elapsedtime;
k++;
//TIME STAMP
printf("Tempo decorrido para terminar a fase de construção no 'servidor 1', criando a tabela hash A com Murmur3: %g ms\n\n", elapsedtime);
ticks[0] = clock();
// SERVER 2 SIMULATION: deal with the first half of the data in the smallest table (relation R)
table_b = (malloc(size * sizeof(htype))); // allocation of the hash table_b vector
if (!table_b){
printf("Erro. Espaço de memória insuficiente para criação da tabela hash.\n");
exit(1);
}
init_table(table_b, size); // hash table_b initialization
init_counter(&counter_b); // counter_b initialization
uint32_t key_b, index_b, match_key_b;
for (i = l_tab1/2; i < l_tab1; i++){
key_b = c_custkey[i]; // key_b receives c_custekey[i]
index_b = murmurhash_index(key_b, size); // "index_b" receives an index created using murmur3_32 hash
store(key_b, index_b, table_b, &counter_b); // stores c_custekey[i] in the hash table_b
}
printf("O 'servidor 2' terminou a fase de construção e criou a tabela hash B com Murmur3.\n");
ticks[1] = clock();
elapsedtime = (ticks[1] - ticks[0]) * 1000.0 / CLOCKS_PER_SEC;
time_register[k] = elapsedtime;
k++;
//TIME STAMP
printf("Tempo decorrido para terminar a fase de construção no 'servidor 2', criando a tabela hash B com Murmur3: %g ms\n\n", elapsedtime);
ticks[0] = clock();
//printf("Tabela A:\n");
//print_hashtable(table_a, size); // prints the hash table_a
//init_hits_counter(hits_counter_a); // hits_counter_a initialization
//histogram(table_a, hits_counter_a, size); // prints data to make an histogram about collisions in the hash table_a
//printf("\nTabela B:\n");
//print_hashtable(table_b, size); // prints the hash table_b
//init_hits_counter(hits_counter_b); // hits_counter_b initialization
//histogram(table_b, hits_counter_b, size); // prints data to make an histogram about collisions in the hash table_b
// PROBE PHASE - CHECKS MATCHES IN THE LARGEST TABLE
// SERVER 1 SIMULATION
out_custkey_a = malloc(l_tab2/2 * sizeof(uint32_t)); // SF*1500000/2 are populated
if (out_custkey_a == NULL){ // if malloc returns NULL, there is not enough memory
printf("Erro. Espaço de memória insuficiente.\n");
exit(1);
}
init_array(out_custkey_a, l_tab2/2); // out_custkey_a initialization: -1 in all positions
out_name_a = (char **)malloc(l_tab2/2 * sizeof(char *)); // fixed text, size 25
if (out_name_a == NULL){ // if malloc returns NULL, there is not enough memory
printf("Erro. Espaço de memória insuficiente.\n");
exit(1);
}
out_name_a[0] = (char *)malloc(sizeof(char) * 35 * l_tab2/2); // allocates the big vector
if (out_name_a[0] == NULL){ // if malloc returns NULL, there is not enough memory
printf("Erro. Espaço de memória insuficiente.\n");
exit(1);
}
for (i = 0; i < l_tab2/2; i++){ // rearranges the pointers
out_name_a[i] = out_name_a[0] + (i * 35 * sizeof(char));
}
for (i = 0; i < l_tab2/2; i++){
key_a = o_custkey[i]; // key_a receives o_custekey[i]
index_a = murmurhash_index(key_a, size); // "index" receives an index created using murmur3_32 hash
match_key_a = verify(key_a, index_a, table_a); // checks matches beetwen the largest and the smallest table keys
if(match_key_a > 0){ // when it finds a match, it creates query 10 outputs
out_custkey_a[i] = match_key_a; // out_custkey_a[i] receives match_key_a
memcpy(out_name_a[i], c_name[match_key_a - 1], strlen(c_name[match_key_a - 1])+1); // copy c_name[match_key_a - 1] to out_name_a[i]
tuples_counter_a++;
}
else{
match_key_a = verify(key_a, index_a, table_b); // checks matches beetwen the largest and the smallest table keys
if(match_key_a > 0){ // when it finds a match, it creates query 10 outputs
out_custkey_a[i] = match_key_a; // out_custkey[i] receives match_key_a
memcpy(out_name_a[i], c_name[match_key_a - 1], strlen(c_name[match_key_a - 1])+1); // copy c_name[match_key - 1] to out_name[i]
tuples_counter_a++;
}
}
}
printf("O 'servidor 1' terminou a fase de análise.\n");
ticks[1] = clock();
elapsedtime = (ticks[1] - ticks[0]) * 1000.0 / CLOCKS_PER_SEC;
time_register[k] = elapsedtime;
k++;
//TIME STAMP
printf("Tempo decorrido para o 'servidor 1' terminar a fase de análise: %g ms\n\n", elapsedtime);
ticks[0] = clock();
// SERVER 2 SIMULATION
out_custkey_b = malloc(l_tab2/2 * sizeof(uint32_t)); // SF*1500000/2 are populated
if (out_custkey_b == NULL){ // if malloc returns NULL, there is not enough memory
printf("Erro. Espaço de memória insuficiente.\n");
exit(1);
}
init_array(out_custkey_b, l_tab2/2); // out_custkey_a initialization: -1 in all positions
out_name_b = (char **)malloc(l_tab2/2 * sizeof(char *)); // fixed text, size 25
if (out_name_b == NULL){ // if malloc returns NULL, there is not enough memory
printf("Erro. Espaço de memória insuficiente.\n");
exit(1);
}
out_name_b[0] = (char *)malloc(sizeof(char) * 35 * l_tab2/2); // allocates the big vector
if (out_name_b[0] == NULL){ // if malloc returns NULL, there is not enough memory
printf("Erro. Espaço de memória insuficiente.\n");
exit(1);
}
for (i = 0; i < l_tab2/2; i++){ // rearranges the pointers
out_name_b[i] = out_name_b[0] + (i * 35 * sizeof(char));
}
for (i = l_tab2/2; i < l_tab2; i++){
key_b = o_custkey[i]; // key_a receives o_custekey[i]
index_b = murmurhash_index(key_b, size); // "index" receives an index created using murmur3_32 hash
match_key_b = verify(key_b, index_b, table_b); // checks matches beetwen the largest and the smallest table keys
if(match_key_b > 0){ // when it finds a match, it creates query 10 outputs
out_custkey_b[i - l_tab2/2] = match_key_b; // out_custkey_b[i - l_tab2/2] receives match_key_b
memcpy(out_name_b[i - l_tab2/2], c_name[match_key_b - 1], strlen(c_name[match_key_b - 1])+1); // copy c_name[match_key_b - 1] to out_name_b[i]
tuples_counter_b++;
}
else{
match_key_b = verify(key_b, index_b, table_a); // checks matches beetwen the largest and the smallest table keys
if(match_key_b > 0){ // when it finds a match, it creates query 10 outputs
out_custkey_b[i - l_tab2/2] = match_key_b; // out_custkey[i - l_tab2/2] receives match_key_b
memcpy(out_name_b[i - l_tab2/2], c_name[match_key_b - 1], strlen(c_name[match_key_b - 1])+1); // copy c_name[match_key_b - 1] to out_name_b[i]
tuples_counter_b++;
}
}
}
printf("O 'servidor 2' terminou a fase de análise.\n");
ticks[1] = clock();
elapsedtime = (ticks[1] - ticks[0]) * 1000.0 / CLOCKS_PER_SEC;
time_register[k] = elapsedtime;
k++;
//TIME STAMP
printf("Tempo decorrido para o 'servidor 2' terminar a fase de análise: %g ms\n\n", elapsedtime);
ticks[0] = clock();
tuples_counter = tuples_counter_a + tuples_counter_b;
}
//FNV-1A
else{
// BUILD PHASE - HASH TABLE CONSTRUCTION USING THE SMALLEST TABLE
// SERVER 1 SIMULATION: deal with the first half of the data in the smallest table (relation R)
table_a = (malloc(size * sizeof(htype))); // allocation of the hash table_a vector
if (!table_a){
printf("Erro. Espaço de memória insuficiente para criação da tabela hash.\n");
exit(1);
}
init_table(table_a, size); // hash table_a initialization
init_counter(&counter_a); // counter_a initialization
uint32_t key_a, index_a, match_key_a;
for (i = 0; i < l_tab1/2; i++){
key_a = c_custkey[i]; // key_a receives c_custekey[i]
index_a = fnv1a_index(key_a, size); // "index_a" receives an index created using FNV1-a hash
store(key_a, index_a, table_a, &counter_a); // stores c_custekey[i] in the hash table_a
}
printf("O 'servidor 1' criou terminou a fase de construção e criou a tabela hash A com FNV-1a.\n");
ticks[1] = clock();
elapsedtime = (ticks[1] - ticks[0]) * 1000.0 / CLOCKS_PER_SEC;
time_register[k] = elapsedtime;
k++;
//TIME STAMP
printf("Tempo decorrido para terminar a fase de construção no 'servidor 1', criando a tabela hash A com FNV-1a: %g ms\n\n", elapsedtime);
ticks[0] = clock();
// SERVER 2 SIMULATION: deal with the first half of the data in the smallest table (relation R)
table_b = (malloc(size * sizeof(htype))); // allocation of the hash table_b vector
if (!table_b){
printf("Erro. Espaço de memória insuficiente para criação da tabela hash.\n");
exit(1);
}
init_table(table_b, size); // hash table_b initialization
init_counter(&counter_b); // counter_b initialization
uint32_t key_b, index_b, match_key_b;
for (i = l_tab1/2; i < l_tab1; i++){
key_b = c_custkey[i]; // key_b receives c_custekey[i]
index_b = fnv1a_index(key_b, size); // "index_b" receives an index created using FNV1-a hash
store(key_b, index_b, table_b, &counter_b); // stores c_custekey[i] in the hash table_b
}
printf("O 'servidor 2' criou terminou a fase de construção e criou a tabela hash B com FNV-1a.\n");
ticks[1] = clock();
elapsedtime = (ticks[1] - ticks[0]) * 1000.0 / CLOCKS_PER_SEC;
time_register[k] = elapsedtime;
k++;
//TIME STAMP
printf("Tempo decorrido para terminar a fase de construção no 'servidor 2', criando a tabela hash B com FNV-1a: %g ms\n\n", elapsedtime);
ticks[0] = clock();
//printf("Tabela A:\n");
//print_hashtable(table_a, size); // prints the hash table_a
//init_hits_counter(hits_counter_a); // hits_counter_a initialization
//histogram(table_a, hits_counter_a, size); // prints data to make an histogram about collisions in the hash table_a
//printf("Tabela B:\n");
//print_hashtable(table_b, size); // prints the hash table_b
//init_hits_counter(hits_counter_b); // hits_counter_b initialization
//histogram(table_b, hits_counter_b, size); // prints data to make an histogram about collisions in the hash table_b
// PROBE PHASE - CHECKS MATCHES IN THE LARGEST TABLE
// SERVER 1 SIMULATION
out_custkey_a = malloc(l_tab2/2 * sizeof(uint32_t)); // SF*1500000/2 are populated
if (out_custkey_a == NULL){ // if malloc returns NULL, there is not enough memory
printf("Erro. Espaço de memória insuficiente.\n");
exit(1);
}
init_array(out_custkey_a, l_tab2/2); // out_custkey_a initialization: -1 in all positions
out_name_a = (char **)malloc(l_tab2/2 * sizeof(char *)); // fixed text, size 25
if (out_name_a == NULL){ // if malloc returns NULL, there is not enough memory
printf("Erro. Espaço de memória insuficiente.\n");
exit(1);
}
out_name_a[0] = (char *)malloc(sizeof(char) * 35 * l_tab2/2); // allocates the big vector
if (out_name_a[0] == NULL){ // if malloc returns NULL, there is not enough memory
printf("Erro. Espaço de memória insuficiente.\n");
exit(1);
}
for (i = 0; i < l_tab2/2; i++){ // rearranges the pointers
out_name_a[i] = out_name_a[0] + (i * 35 * sizeof(char));
}
for (i = 0; i < l_tab2/2; i++){
key_a = o_custkey[i]; // key_a receives o_custekey[i]
index_a = fnv1a_index(key_a, size); // "index" receives an index created using FNV1-a hash
match_key_a = verify(key_a, index_a, table_a); // checks matches beetwen the largest and the smallest table keys
if(match_key_a > 0){ // when it finds a match, it creates query 10 outputs
out_custkey_a[i] = match_key_a; // out_custkey_a[i] receives match_key_a
memcpy(out_name_a[i], c_name[match_key_a - 1], strlen(c_name[match_key_a - 1])+1); // copy c_name[match_key_a - 1] to out_name_a[i]
tuples_counter_a++;
}
else{
match_key_a = verify(key_a, index_a, table_b); // checks matches beetwen the largest and the smallest table keys
if(match_key_a > 0){ // when it finds a match, it creates query 10 outputs
out_custkey_a[i] = match_key_a; // out_custkey_a[i] receives match_key_a
memcpy(out_name_a[i], c_name[match_key_a - 1], strlen(c_name[match_key_a - 1])+1); // copy c_name[match_key_a - 1] to out_name_a[i]
tuples_counter_a++;
}
}
}
printf("O 'servidor 1' terminou a fase de análise.\n");
ticks[1] = clock();
elapsedtime = (ticks[1] - ticks[0]) * 1000.0 / CLOCKS_PER_SEC;
time_register[k] = elapsedtime;
k++;
//TIME STAMP
printf("Tempo decorrido para o 'servidor 1' terminar a fase de análise: %g ms\n\n", elapsedtime);
ticks[0] = clock();
// SERVER 2 SIMULATION
out_custkey_b = malloc(l_tab2/2 * sizeof(uint32_t)); // SF*1500000/2 are populated
if (out_custkey_b == NULL){ // if malloc returns NULL, there is not enough memory
printf("Erro. Espaço de memória insuficiente.\n");
exit(1);
}
init_array(out_custkey_b, l_tab2/2); // out_custkey_a initialization: -1 in all positions
out_name_b = (char **)malloc(l_tab2/2 * sizeof(char *)); // fixed text, size 25
if (out_name_b == NULL){ // if malloc returns NULL, there is not enough memory
printf("Erro. Espaço de memória insuficiente.\n");
exit(1);
}
out_name_b[0] = (char *)malloc(sizeof(char) * 35 * l_tab2/2); // allocates the big vector
if (out_name_b[0] == NULL){ // if malloc returns NULL, there is not enough memory
printf("Erro. Espaço de memória insuficiente.\n");
exit(1);
}
for (i = 0; i < l_tab2/2; i++){ // rearranges the pointers
out_name_b[i] = out_name_b[0] + (i * 35 * sizeof(char));
}
for (i = l_tab2/2; i < l_tab2; i++){
key_b = o_custkey[i]; // key_a receives o_custekey[i]
index_b = fnv1a_index(key_b, size); // "index" receives an index created using FNV1-a hash
match_key_b = verify(key_b, index_b, table_b); // checks matches beetwen the largest and the smallest table keys
if(match_key_b > 0){ // when it finds a match, it creates query 10 outputs
out_custkey_b[i - l_tab2/2] = match_key_b; // out_custkey_b[i - l_tab2/2] receives match_key_b
memcpy(out_name_b[i - l_tab2/2], c_name[match_key_b - 1], strlen(c_name[match_key_b - 1])+1); // copy c_name[match_key_b - 1] to out_name_b[i]
tuples_counter_b++;
}
else{
match_key_b = verify(key_b, index_b, table_a); // checks matches beetwen the largest and the smallest table keys
if(match_key_b > 0){ // when it finds a match, it creates query 10 outputs
out_custkey_b[i - l_tab2/2] = match_key_b; // out_custkey_b[i - l_tab2/2] receives match_key_b
memcpy(out_name_b[i - l_tab2/2], c_name[match_key_b - 1], strlen(c_name[match_key_b - 1])+1); // copy c_name[match_key_b - 1] to out_name_b[i]
tuples_counter_b++;
}
}
}
printf("O 'servidor 2' terminou a fase de análise.\n");
ticks[1] = clock();
elapsedtime = (ticks[1] - ticks[0]) * 1000.0 / CLOCKS_PER_SEC;
time_register[k] = elapsedtime;
k++;
//TIME STAMP
printf("Tempo decorrido para o 'servidor 2' terminar a fase de análise: %g ms\n\n", elapsedtime);
ticks[0] = clock();
tuples_counter = tuples_counter_a + tuples_counter_b;
}
}
print_query10(out_custkey_a, out_name_a, l_tab2/2); // prints query 10 output - part 1
print_query10(out_custkey_b, out_name_b, l_tab2/2); // prints query 10 output - part 1
printf("\nA saída da 'Query 10' tem %li tuplas.\n", tuples_counter);
ticks[1] = clock();
elapsedtime = (ticks[1] - ticks[0]) * 1000.0 / CLOCKS_PER_SEC;
time_register[k] = elapsedtime;
k++;
//TIME STAMP
printf("Tempo decorrido para imprimir a saída da 'Query 10': %g ms\n\n", elapsedtime);
ticks[0] = clock();
free(c_custkey);
c_custkey = NULL;
free(c_name);
c_name = NULL;
free(c_address);
c_address = NULL;
free(c_nationkey);
c_nationkey = NULL;
free(c_phone);
c_phone = NULL;
free(c_acctbal);
c_acctbal = NULL;
free(c_mktsegment);
c_mktsegment = NULL;
free(c_comment);
c_comment = NULL;
free(o_orderkey);
o_orderkey = NULL;
free(o_custkey);
o_custkey = NULL;
free(o_orderstatus);
o_orderstatus = NULL;
free(o_totalprice);
o_totalprice = NULL;
free(o_clerk);
o_clerk = NULL;
free(o_shippriority);
o_shippriority = NULL;
free(o_comment);
o_comment = NULL;
free(table_a);
table_a = NULL;
free(table_b);
table_b = NULL;
free(out_custkey);
out_custkey = NULL;
free(out_name);
out_name = NULL;
free(out_custkey_a);
out_custkey_a = NULL;
free(out_name_a);
out_name_a = NULL;
free(out_custkey_b);
out_custkey_b = NULL;
free(out_name_b);
out_name_b= NULL;
ticks[1] = clock();
elapsedtime = (ticks[1] - ticks[0]) * 1000.0 / CLOCKS_PER_SEC;
time_register[k] = elapsedtime;
k++;
//TIME STAMP
printf("Tempo decorrido para liberar os espaços de memória: %g ms\n\n", elapsedtime);
totalticks[1] = clock();
elapsedtime = (totalticks[1] - totalticks[0]) * 1000.0 / CLOCKS_PER_SEC;
time_register[k] = elapsedtime;
k++;
//TIME STAMP
printf("Tempo de execução da Query 10 em '2 servidores': %g ms\n\n", elapsedtime);
for (int count = 0; count < k; count++)
printf("%g ms\n", time_register[count]);
output_file = fopen(file_name,"a"); // opens a file named "tempos" for writing
fputs(argv[7], output_file); // execution ID
fputs(";", output_file);
fputs("Query 10 em 2 servidores", output_file); // query
fputs(";", output_file);
sprintf (buffer, "%li", tuples_counter);
fputs(buffer, output_file); // number of tuples from the 'Query 10' output
fputs(";", output_file);
fputs(argv[1], output_file); // hash function type
fputs(";", output_file);
fputs(argv[3], output_file); // size of the smallest relation
fputs(";", output_file);
fputs(argv[5], output_file); // size of the largest relation
fputs(";", output_file);
fputs(argv[6], output_file); // size of the hash table
fputs(";", output_file);
/* Displayed times: parameter storage, allocation of memory spaces for input data, storage of input data,
build phase "server 1", build phase "server 2", probe phase "server 1", probe phase "server 2",
printing of the output , memory release, "Query10" runtime */
for (int count2 = 0; count2 < k; count2++){
sprintf (buffer, "%g", time_register[count2]); // puts the time in the buffer string
fputs(buffer, output_file);
fputs(";", output_file);
}
fputs ("\n", output_file);
fclose(output_file);
return 0;
}