-
Notifications
You must be signed in to change notification settings - Fork 19
/
Copy pathMAP_openMP.c
872 lines (581 loc) · 18.2 KB
/
MAP_openMP.c
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
// False sharing
#include<stdio.h>
#include<stdlib.h>
#include<omp.h>
#define size 100000
//Structure of the Map Table
struct table{
int key;
int value;
int fill;
int delet;
};
//Structure of the Element
struct element{
int key;
int value;
};
//Defining Lock for each index of Map Table
omp_lock_t lock[size];
//Hash Function(Should not be linear)
int doubleDashingFunction(int num, int i){
int f1 = (num);
// int f2 = (num%5);
return (f1+(i*i))%size;
}
void insertFun(int insertSize, int (*insertEl)[2], int *workDone, int *searchDone, struct table *mapTable){
#pragma omp for
for(int i=0 ; i<insertSize ; i++){
//taking key and value from input
int key, value, hashedVal;
key = insertEl[i][0];
value = insertEl[i][1];
//counter helps in probing
int counter = 0;
//Run the loop untill the insertion is not marked
while(!workDone[i] && !searchDone[i]){
// printf("tid = %d and key = %d\n", omp_get_thread_num(), key);
hashedVal = doubleDashingFunction(key, counter);
// printf(" hashedVal = %d \n", hashedVal);
//Check the condition if the respective postion is occupied or not
if(mapTable[hashedVal].fill==0 || (mapTable[hashedVal].fill==1 && mapTable[hashedVal].delet==1)){
//lock the respective index of Map Table where we have to insert
omp_set_lock(&lock[hashedVal]);
//Again checking so that other thread should not fill at same location
if(mapTable[hashedVal].fill==0 || (mapTable[hashedVal].fill==1 && mapTable[hashedVal].delet==1)){
mapTable[hashedVal].key = key;
mapTable[hashedVal].value = value;
if(mapTable[hashedVal].fill==0)
mapTable[hashedVal].fill=1;
else
mapTable[hashedVal].delet=0;
workDone[i] = 1;
//Unset the lock
omp_unset_lock(&lock[hashedVal]);
}
else{
counter++;
//After one round of traversal of whole Map Table, break out loop
if(counter>30){
//Unset the lock
omp_unset_lock(&lock[hashedVal]); //I don't think it is required here
break;
}
}
}else{
counter++;
//After one round of traversal of whole Map Table, break out loop
if(counter>30){
break;
}
}
// printf("tid = %d and key = %d value = %d and work=%d and counter = %d\n", omp_get_thread_num(), key, value, workDone[i], counter);
}
}
}
void searchPairFun(int searchSize, int (*searchEl)[2], int *searchDone, struct table *mapTable){
//Start of Directive to run all iteration in parallel
#pragma omp parallel for
for (int i = 0; i < searchSize; ++i)
{
int key, value, hashedVal;
key = searchEl[i][0];
value = searchEl[i][1];
int counter = 0;
while(!searchDone[i]){
hashedVal = doubleDashingFunction(key, counter);
if(!mapTable[hashedVal].fill)
break;
else{
if(mapTable[hashedVal].key == key && mapTable[hashedVal].value == value){
searchDone[i]=1;
}else{
counter++;
//After one round of traversal of whole Map Table, break out loop
if(counter>30)
break;
}
}
}
}
//End of Directive to run all iteration in parallel
}
void searchKeyFun(int searchSize, int *ansCounter, int *searchEl, int *searchDone, struct table *mapTable, int *checkSearch){
//Start of Directive to run all iteration in parallel
#pragma omp parallel for
for(int i=0 ; i<searchSize ; i++){
int key = searchEl[i];
int hashedVal, counter=0;
while(1){
hashedVal = doubleDashingFunction(key, counter);
if(mapTable[hashedVal].fill==0){
break;
}
else{
if(mapTable[hashedVal].key == key){
//printf("\nmatch key = %d\n", key);
checkSearch[hashedVal] = 1;
//Directive to put atomic condition on ansCounter variable
#pragma omp atomic
(*ansCounter)++;
counter++;
searchDone[i] = 1;
}
else{
counter++;
//After one round of traversal of whole Map Table, break out loop
if(counter>30)
break;
}
}
}
}
//End of Directive to run all iteration in parallel
}
void searchKeyAnsInsert(int searchSize, int *insertIndex, int *searchEl, int *searchDone, int *checkSearch, struct table *mapTable, struct element *ans){
//Start of Directive to run all iteration in parallel
#pragma omp parallel for
for(int i=0 ; i<searchSize ; i++){
if(searchDone[i]){
int key = searchEl[i];
int counter = 0;
int hashedVal;
while(1){
hashedVal = doubleDashingFunction(key, counter);
if(hashedVal < 0){
counter++;
continue;
}
if(checkSearch[hashedVal]==1 || mapTable[hashedVal].fill == 0)
break;
if(mapTable[hashedVal].key == key){
//lock the respective index of Map Table so that only 1 thread can fetch the value
omp_set_lock(&lock[hashedVal]);
if(checkSearch[hashedVal] == 0 ){
//printf("\nkey = %d and value = %d\n",mapTable[hashedVal].key, mapTable[hashedVal].value);
ans[(*insertIndex)].key = mapTable[hashedVal].key;
ans[(*insertIndex)].value = mapTable[hashedVal].value;
(*insertIndex)++;
checkSearch[hashedVal] = 1;
}
omp_unset_lock(&lock[hashedVal]);
counter++;
}
else
{
counter++;
}
}
}
}
//End of Directive to run all iteration in parallel
}
void deletePairFun(int deletBatchSize, int (*deletBatchEl)[2], int *checkDel, struct table *mapTable){
//Start of Directive to run all iteration in parallel
#pragma omp parallel for
for(int i=0 ; i<deletBatchSize ; i++){
int key = deletBatchEl[i][0];
int value = deletBatchEl[i][1];
int counter = 0;
int hashedVal;
while(1){
hashedVal = doubleDashingFunction(key, counter);
if(mapTable[hashedVal].fill == 0)
break;
if(mapTable[hashedVal].key == key && mapTable[hashedVal].value == value){
mapTable[hashedVal].key = 0;
mapTable[hashedVal].value = 0;
mapTable[hashedVal].delet = 1;
break;
}
counter++;
//After one round of traversal of whole Map Table, break out loop
if(counter>30)
break;
}
}
//End of Directive to run all iteration in parallel
}
void deletKeyFun(int deletBatchSize, int *deletBatchEl, int *checkDel, struct table *mapTable){
//Start of Directive to run all iteration in parallel
#pragma omp parallel for
for(int i=0 ; i<deletBatchSize ; i++){
int key = deletBatchEl[i];
int counter = 0;
int hashedVal;
while(1){
hashedVal = doubleDashingFunction(key, counter);
if(mapTable[hashedVal].fill == 0)
break;
if(mapTable[hashedVal].key == key && mapTable[hashedVal].delet != 1){
mapTable[hashedVal].key = 0;
mapTable[hashedVal].value = 0;
mapTable[hashedVal].delet = 1;
}
counter++;
//After one round of traversal of whole Map Table, break out loop
if(counter>30)
break;
}
}
//End of Directive to run all iteration in parallel
}
int main(){
//Time variable
double t1,t2;
int num = omp_get_num_threads();
#pragma omp single
printf("No of Threads: = %d\n",num);
//Initialization of locks using parallel threads
#pragma omp for
for(int i = 0; i < size; ++i){
omp_init_lock(&lock[i]);
}
//Handling the operation using condtional variable
int insert=1, insertA=1, search=0, del=0,searchPair=0, searchKey=0, deletKey=0, deletPair=0;
//Creating Map Table Dynamically
struct table *mapTable;
mapTable = (struct table*)malloc(size*sizeof(struct table));
for(int i=0 ; i<size ; i++){
mapTable[i].key = -3;
mapTable[i].value = -3;
mapTable[i].fill = 0;
mapTable[i].delet = 0;
}
//Insertion in Map Data Structure to test insertion before next insertion
if(insert){
//Insertion Batch Size
int insertSize = 10000;
int insertEl[insertSize][2];
t1 = omp_get_wtime();
//for Marking the index of Insertion Batch Element
int *workDone;
workDone = (int*)malloc(insertSize*sizeof(int));
int *searchDone;
searchDone = (int*)malloc(insertSize*sizeof(int));
/*Start of Dummy Input*/
insertEl[0][0] = 59;
insertEl[0][1] = 6;
workDone[0] = 0;
searchDone[0] = 0;
insertEl[1][0] = 59;
insertEl[1][1] = 8;
workDone[1] = 0;
searchDone[1] = 0;
insertEl[2][0] = 59;
insertEl[2][1] = 9;
workDone[2] = 0;
searchDone[2] = 0;
for(int i=0 ; i<insertSize ; i++){
insertEl[i][0] = i;
insertEl[i][1] = i+7 ;
workDone[i] = 0; //This is important
searchDone[i] = 0;
}
/*End of Dummy Input*/
// First need to search that all {key, value} pair present or not
//Start of Directive to run all iteration in parallel
#pragma omp parallel for
for (int i = 0; i < insertSize; ++i)
{
int key, value, hashedVal;
key = insertEl[i][0];
value = insertEl[i][1];
int counter = 0;
while(!searchDone[i]){
hashedVal = doubleDashingFunction(key, counter);
if(!mapTable[hashedVal].fill)
break;
else{
if(mapTable[hashedVal].key == key && mapTable[hashedVal].value == value){
searchDone[i]=1;
}else{
counter++;
//After one round of traversal of whole Map Table, break out loop
if(counter>30)
break;
}
}
}
}
//End of Directive to run all iteration in parallel
//Count the successful search
// int success=0;
// /*Start Output*/
// printf("\n\nSearched Element in the Hash Table:\n\n");
// for(int i=0 ; i<searchSize ; i++){
// if(searchDone[i]){
// // printf("key = %d value = %d \n", searchEl[i][0], searchEl[i][1]);
// success++;
// }
// }
// Search Done
//Start of Directive to run all iteration in parallel
#pragma omp for
for(int i=0 ; i<insertSize ; i++){
//taking key and value from input
int key, value, hashedVal;
key = insertEl[i][0];
value = insertEl[i][1];
//counter helps in probing
int counter = 0;
//Run the loop untill the insertion is not marked
while(!workDone[i] && !searchDone[i]){
// printf("tid = %d and key = %d\n", omp_get_thread_num(), key);
hashedVal = doubleDashingFunction(key, counter);
// printf(" hashedVal = %d \n", hashedVal);
//Check the condition if the respective postion is occupied or not
if(mapTable[hashedVal].fill==0 || (mapTable[hashedVal].fill==1 && mapTable[hashedVal].delet==1)){
//lock the respective index of Map Table where we have to insert
omp_set_lock(&lock[hashedVal]);
//Again checking so that other thread should not fill at same location
if(mapTable[hashedVal].fill==0 || (mapTable[hashedVal].fill==1 && mapTable[hashedVal].delet==1)){
mapTable[hashedVal].key = key;
mapTable[hashedVal].value = value;
if(mapTable[hashedVal].fill==0)
mapTable[hashedVal].fill=1;
else
mapTable[hashedVal].delet=0;
workDone[i] = 1;
//Unset the lock
omp_unset_lock(&lock[hashedVal]);
}
else{
counter++;
//After one round of traversal of whole Map Table, break out loop
if(counter>30){
//Unset the lock
omp_unset_lock(&lock[hashedVal]); //I don't think it is required here
break;
}
}
}else{
counter++;
//After one round of traversal of whole Map Table, break out loop
if(counter>30){
break;
}
}
// printf("tid = %d and key = %d value = %d and work=%d and counter = %d\n", omp_get_thread_num(), key, value, workDone[i], counter);
}
}
//End of Directive to run all iteration in parallel
t2 = omp_get_wtime();
//Number of Successful Insertion
int success=0;
/*Start Output*/
// printf("\n\nElement in the Hash Table:\n\n");
for(int i=0 ; i<size ; i++){
if(mapTable[i].fill==1 && mapTable[i].delet==0){
// printf("key = %d value = %d at i=%d\n", mapTable[i].key, mapTable[i].value,i);
success++;
}
}
printf("\nmap size = %d Insertion sucess = %d and time = %g\n", size, success, t2-t1);
/*End Output*/
}
// Insert again to check duplicate insertion
if(insertA){
printf("\nInsert Again\n");
//Insertion Batch Size
int insertSize = 8000;
int insertEl[insertSize][2];
t1 = omp_get_wtime();
//for Marking the index of Insertion Batch Element
int *workDone;
workDone = (int*)malloc(insertSize*sizeof(int));
int *searchDone;
searchDone = (int*)malloc(insertSize*sizeof(int));
/*Start of Dummy Input*/
insertEl[0][0] = 59;
insertEl[0][1] = 6;
workDone[0] = 0;
searchDone[0] = 0;
insertEl[1][0] = 59;
insertEl[1][1] = 8;
workDone[1] = 0;
searchDone[1] = 0;
insertEl[2][0] = 59;
insertEl[2][1] = 9;
workDone[2] = 0;
searchDone[2] = 0;
for(int i=0 ; i<insertSize ; i++){
insertEl[i][0] = i;
insertEl[i][1] = i+8;
workDone[i] = 0; //This is important
searchDone[i] = 0;
}
/*End of Dummy Input*/
// First need to search that all {key, value} pair present or not
//Start of Directive to run all iteration in parallel
searchPairFun(insertSize, insertEl, searchDone, mapTable);
// Search Done
//Start of Directive to run all iteration in parallel
insertFun(insertSize, insertEl, workDone, searchDone, mapTable);
//End of Directive to run all iteration in parallel
t2 = omp_get_wtime();
//Number of Successful Insertion
int success=0;
/*Start Output*/
// printf("\n\nElement in the Hash Table:\n\n");
for(int i=0 ; i<size ; i++){
if(mapTable[i].fill==1 && mapTable[i].delet==0){
// printf("key = %d value = %d at i=%d\n", mapTable[i].key, mapTable[i].value,i);
success++;
}
}
printf("\nmap size = %d Insertion sucess = %d and time = %g\n", size, success, t2-t1);
/*End Output*/
printf("\nInsert Again End\n");
}
//Search in Map Data Structure
if(search==1){
//Search with pair(key, value)
if(searchPair==1){
//Search Batch Size
int searchSize = 100000;
int searchEl[searchSize][2];
t1 = omp_get_wtime();
//Array to mark the elements has been searched or not
int *searchDone;
searchDone = (int*)malloc(searchSize*sizeof(int));
/*Start of Dummy Input*/
searchEl[0][0] = 0;
searchEl[0][1] = 7;
searchDone[0] = 0;
searchEl[1][0] = 7;
searchEl[1][1] = 14;
searchDone[1] = 0;
searchEl[2][0] = 14;
searchEl[2][1] = 21;
searchDone[2] = 0;
for(int i=3 ; i<searchSize ; i++){
searchEl[i][0] = i;
searchEl[i][1] = 7+i;
searchDone[i] = 0;
}
/*End of Dummy Input*/
searchPairFun(searchSize, searchEl, searchDone, mapTable);
t2 = omp_get_wtime();
//Count the successful search
int success=0;
/*Start Output*/
printf("\n\nSearched Element in the Hash Table:\n\n");
for(int i=0 ; i<searchSize ; i++){
if(searchDone[i]){
// printf("key = %d value = %d \n", searchEl[i][0], searchEl[i][1]);
success++;
}
}
printf("\nSearch sucess = %d and time = %g\n", success, t2-t1);
/*End Output*/
}
//Search with key only
if(searchKey==1){
int searchSize = 100000;
int searchEl[searchSize];
t1 = omp_get_wtime();
/*Start of Dummy Input*/
searchEl[0] = 1;
searchEl[1] = 8;
searchEl[2] = 9;
for(int i=0 ; i<searchSize ; i++){
searchEl[i] = 0;
// searchDone[i] = 0;
}
/*End of Dummy Input*/
int *searchDone = (int*)malloc(searchSize*sizeof(int));
#pragma omp for
for(int i = 0; i < searchSize; ++i){
searchDone[i] = 0;
}
int checkSearch[size] = {0};
//Counter to track number of matched results
int ansCounter=0;
searchKeyFun(searchSize, &ansCounter, searchEl, searchDone, mapTable, checkSearch);
printf("total found = %d\n", ansCounter);
//checkSearch array used to accumulate the results
#pragma omp parallel for
for(int i=0 ; i<size ; i++)
checkSearch[i]=0;
//Store results in ans array
struct element *ans = (struct element*)malloc(ansCounter*sizeof(struct element));
int insertIndex=0;
searchKeyAnsInsert(searchSize, &insertIndex, searchEl, searchDone, checkSearch, mapTable, ans);
t2 = omp_get_wtime();
/*Start Output*/
printf("\n\nNumber of elements found = %d and time = %g\n\n",ansCounter, t2-t1);
printf("\n\nNumber of elements non overlapped found = %d and time = %g\n\n",insertIndex, t2-t1);
//for(int i=0 ; i<insertIndex ; i++){
// printf("key = %d and value = %d\n", ans[i].key, ans[i].value);
//}
/*End Output*/
}//End with key only
}
//Delete operation in Map Data Structure
if(del==1){
//Delete with pair(key, value)
if(deletPair == 1){
int deletBatchSize = 100000;
int deletBatchEl[deletBatchSize][2];
/*Start of Dummy Input*/
printf("Elements to Delete : \n\n");
for(int i=0 ; i<deletBatchSize ; i++){
deletBatchEl[i][0] = 5+i;
deletBatchEl[i][1] = i+7;
// //printf("Dkey = %d Dvalue = %d\n", deletBatchEl[i][0], deletBatchEl[i][1]);
}
printf("\n");
/*End of Dummy Input*/
t1 = omp_get_wtime();
int checkDel[size]={0};
deletePairFun(deletBatchSize, deletBatchEl, checkDel, mapTable);
t2 = omp_get_wtime();
int total=0;
/*Start Output*/
//printf("\n\nElement in the Hash Table:\n\n");
for(int i=0 ; i<size ; i++){
if(mapTable[i].fill==1 && mapTable[i].delet==0){
// printf("key = %d value = %d at i=%d\n", mapTable[i].key, mapTable[i].value,i);
total++;
}
}
printf("\nmap size = %d total reside = %d and time = %g\n", size, total, t2-t1);
/*End Output*/
}
//Delete Element with only key matched
if(deletKey == 1){
int deletBatchSize = 100000;
int deletBatchEl[deletBatchSize];
/*Start of Dummy Input*/
//printf("Elements to Delete : \n\n");
for(int i=0 ; i<deletBatchSize ; i++){
deletBatchEl[i] = i+5;
//printf("Dkey = %d \n", deletBatchEl[i]);
}
printf("\n");
/*End of Dummy Input*/
t1 = omp_get_wtime();
int checkDel[size]={0};
deletKeyFun(deletBatchSize, deletBatchEl, checkDel, mapTable);
t2 = omp_get_wtime();
int total=0;
/*Start Output*/
//printf("\n\nElement in the Hash Table:\n\n");
for(int i=0 ; i<size ; i++){
if(mapTable[i].fill==1 && mapTable[i].delet==0){
//printf("key = %d value = %d at i=%d\n", mapTable[i].key, mapTable[i].value,i);
total++;
}
}
printf("\nmap size = %d total reside = %d and time = %g\n", size, total, t2-t1);
/*End Output*/
}
}
//Destroy the lock
#pragma omp for
for(int i = 0; i < size; ++i){
omp_destroy_lock(&lock[i]);
}
return 0;
}