-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathinc_Bat.cpp
More file actions
987 lines (893 loc) · 33.7 KB
/
Copy pathinc_Bat.cpp
File metadata and controls
987 lines (893 loc) · 33.7 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
// #include <omp.h>
// #include <iostream>
// #include <vector>
// #include <fstream>
// #include <sstream>
// #include <unordered_map>
// #include <unordered_set>
// #include <chrono>
// #include <algorithm>
// #include <filesystem>
// #include <iomanip>
// #include <math.h>
// #include <set>
// #include <stdbool.h>
// #include <stdio.h>
// // #define num_threads 64
// int n_threads = 75;
// using namespace std;
// vector<double> time_per_thread;
// double maxi_time = 0;
// // using namespace fs = std::filesystem;
// // vector<int> mis;
// class Graph
// {
// public:
// std::vector<int> edges; // stores destination nodes of edges
// std::vector<long int> indices; // stores index in 'edges' for the start of each node's edge list
// std::vector<int> degree; // stores the degree of each node
// std::vector<bool> nodeStatus;
// std::vector<vector<int>> tvbl;
// std::vector<int> csredges;
// std::vector<int> csrindices;
// int maxNode = -1;
// vector<bool> isolated;
// bool isIndependentSet(vector<int> mis)
// {
// for (int v : mis)
// {
// for (int u : mis)
// {
// if (v != u && this->isAdjacent(v, u))
// {
// return false; // Not an independent set
// }
// }
// }
// return true;
// }
// // This method is used to build the CSR from the edge list
// void buildCSR(const std::vector<std::pair<int, int>> &edgeList)
// {
// // Find the maximum node value
// for (const auto &edge : edgeList)
// {
// maxNode = std::max({maxNode, edge.first, edge.second});
// }
// // Resize the vectors to accommodate the maximum node value
// indices.assign(maxNode + 1, 0); // We need maxNode + 2 to include 0 and maxNode indices
// edges.resize(edgeList.size() * 2, 0);
// degree.assign(maxNode + 1, 0); // Since it's an undirected graph, each edge will be added twice
// nodeStatus.assign(maxNode + 1, false);
// isolated.assign(maxNode + 1, false);
// // Calculate the degree of each node
// for (const auto &edge : edgeList)
// {
// degree[edge.first]++;
// degree[edge.second]++;
// }
// // Populate the indices vector
// for (int i = 1; i <= maxNode; ++i)
// {
// indices[i] = indices[i - 1] + degree[i - 1];
// }
// std::vector<long int> currInd = indices;
// // Populate the edges vector
// std::vector<int> indexCount = degree; // Temporary array to keep track of the current index position for each node
// for (const auto &edge : edgeList)
// {
// edges[currInd[edge.first]++] = edge.second;
// edges[currInd[edge.second]++] = edge.first;
// }
// for (int i = 0; i < degree.size(); i++)
// {
// if (degree[i] == 0)
// {
// nodeStatus[i] = true;
// isolated[i] = true;
// }
// }
// cout << "CSR made" << endl;
// cout << "Degree->" << degree.size() << endl;
// cout << "Indices->" << indices.size() << endl;
// cout << "Edges->" << edges.size() / 2 << endl;
// }
// // Checks if a node 'u' is adjacent to node 'v'
// bool isAdjacent(int u, int v) const
// {
// if (u >= csrindices.size() - 1 || v >= csrindices.size() - 1)
// return false;
// for (int i = csrindices[u]; i < csrindices[u + 1]; ++i)
// {
// if (csredges[i] == v)
// return true;
// }
// return false;
// }
// // Get the neighbors of a node
// std::vector<int> getNeighbors(int u) const
// {
// std::vector<int> neighbors;
// if (u < indices.size() - 1)
// {
// for (int i = csrindices[u]; i < csrindices[u + 1]; ++i)
// {
// neighbors.push_back(csredges[i]);
// }
// }
// if (u == indices.size() - 1)
// {
// for (int i = csrindices[u]; i < csredges.size(); i++)
// {
// neighbors.push_back(csredges[i]);
// }
// }
// return neighbors;
// }
// std::vector<int> getMNeighbors(int u) const
// {
// std::vector<int> neighbors;
// u--;
// if (u < indices.size() - 1)
// {
// for (int i = indices[u]; i < indices[u + 1]; ++i)
// {
// if (edges[i] != -1)
// {
// neighbors.push_back(edges[i]);
// }
// }
// }
// if (u == indices.size() - 1)
// {
// for (int i = indices[u]; i < edges.size(); i++)
// {
// // neighbors.push_back(edges[i]);
// if (edges[i] != -1)
// {
// neighbors.push_back(edges[i]);
// }
// }
// }
// return neighbors;
// }
// };
// void PDMI_INC(Graph &graph, vector<int> &mis, pair<int, int> edge)
// {
// bool firstIn = false, secondIn = false;
// int it = -1;
// bool firstInLocal, secondInLocal;
// double beg, en;
// time_per_thread.clear();
// time_per_thread.resize(n_threads);
// #pragma omp parallel num_threads(n_threads) shared(firstIn, secondIn, it) private(firstInLocal, secondInLocal)
// {
// beg = omp_get_wtime();
// int tid = omp_get_thread_num();
// int start = tid * (mis.size() / n_threads);
// int end = (tid == n_threads - 1) ? mis.size() : (tid + 1) * (mis.size() / n_threads);
// firstInLocal = false, secondInLocal = false;
// for (int i = start; i < end; i++)
// {
// if (edge.first == mis[i])
// {
// firstInLocal = true;
// it = i;
// }
// if (edge.second == mis[i])
// {
// secondInLocal = true;
// }
// }
// #pragma omp atomic update
// firstIn |= firstInLocal;
// #pragma omp atomic update
// secondIn |= secondInLocal;
// en = omp_get_wtime();
// time_per_thread[tid] = (en - beg);
// }
// if (firstIn && secondIn && it != -1)
// {
// mis.erase(mis.begin() + it);
// vector<int> neighbours = graph.getNeighbors(edge.first);
// int p = 0;
// for (int k = 0; k < neighbours.size(); k++)
// {
// bool shouldWeAdd = true;
// // printf("ji");
// for (int l = 0; l < graph.getNeighbors(neighbours[k]).size(); l++)
// {
// bool shouldweAddLocal = true;
// #pragma omp parallel num_threads(n_threads) shared(graph, neighbours, mis, shouldWeAdd) private(shouldweAddLocal)
// {
// beg = omp_get_wtime();
// int tid = omp_get_thread_num();
// int start = tid * (mis.size() / n_threads);
// int end = (tid == n_threads - 1) ? mis.size() : (tid + 1) * (mis.size() / n_threads);
// for (int i = start; i < end; i++)
// {
// if (graph.getNeighbors(neighbours[k])[l] == mis[i])
// {
// shouldweAddLocal = false;
// }
// }
// #pragma omp atomic update
// shouldWeAdd |= shouldweAddLocal;
// end = omp_get_wtime();
// time_per_thread[tid] += end - beg;
// }
// if (shouldWeAdd)
// {
// mis.push_back(neighbours[k]);
// }
// }
// }
// }
// }
// int main(int argc, char *argv[])
// {
// if (argc < 7)
// {
// std::cerr << "Usage: " << argv[0] << " <CSR> <MIS> <Deletion_folder> <Insertion_folder> <num_threads> <Num_batch for deletion> <Num_batch for insertion>" << std::endl;
// return 1;
// }
// cout << "----------------------------------------Start-----------------------------------------" << endl;
// cout << "----------------------------------------INC_BAT------------------------------------------" << endl;
// std::string filename1 = argv[1];
// std::string filename2 = argv[2];
// Graph graph;
// graph = readCSR(filename1, filename2);
// std::vector<int> maximalIndependentSet;
// std::string line;
// // Read MIS------------------------------
// vector<bool> nodeStats(graph.maxNode + 1, false);
// std::ifstream inputFile(argv[2]);
// if (inputFile.is_open())
// {
// while (std::getline(inputFile, line))
// {
// maximalIndependentSet.push_back(std::stoi(line) + 1);
// nodeStats[std::stoi(line) + 1] = true;
// }
// inputFile.close();
// }
// else
// {
// std::cerr << "Unable to open the file for reading" << std::endl;
// }
// vector<int> misforInsertionINC;
// std::copy(maximalIndependentSet.begin(), maximalIndependentSet.end(), std::back_inserter(misforInsertionINC));
// vector<int> misforInsertionBAT;
// std::copy(maximalIndependentSet.begin(), maximalIndependentSet.end(), std::back_inserter(misforInsertionBAT));
// vector<bool> nodeStatsforInsertion(nodeStats.size(), false);
// for (int i = 0; i < nodeStats.size(); i++)
// {
// if (nodeStats[i])
// {
// nodeStatsforInsertion[i] = true;
// }
// }
// std::string folderPath(argv[3]);
// std::vector<std::pair<int, int>> edgeList;
// std::vector<std::vector<std::pair<int, int>>> edgeListsForDeletion;
// for (const auto &entry : std::filesystem::directory_iterator(folderPath))
// {
// if (entry.is_regular_file())
// {
// std::ifstream file(entry.path());
// std::string line;
// while (std::getline(file, line))
// {
// std::istringstream iss(line);
// int src, dest;
// if (iss >> src)
// {
// if (!(iss >> dest))
// {
// dest = -1;
// }
// edgeList.push_back(std::make_pair(src, dest));
// }
// }
// edgeListsForDeletion.push_back(edgeList);
// edgeList.clear();
// }
// }
// std::string folderPath1(argv[4]);
// std::vector<std::vector<std::pair<int, int>>> edgeListsForInsertion;
// for (const auto &entry : std::filesystem::directory_iterator(folderPath1))
// {
// if (entry.is_regular_file())
// {
// std::ifstream file(entry.path());
// std::string line;
// while (std::getline(file, line))
// {
// std::istringstream iss(line);
// int src, dest;
// if (iss >> src)
// {
// if (!(iss >> dest))
// {
// dest = -1;
// }
// edgeList.push_back(std::make_pair(src, dest));
// }
// }
// edgeListsForInsertion.push_back(edgeList);
// edgeList.clear();
// }
// }
// int initialCard = maximalIndependentSet.size();
// n_threads = stoi(argv[5]);
// double startTime = 0, endTime = 0, time_cumm = 0;
// int num_deletion_batches = std::stoi(argv[6]);
// int num_insertion_batches = std::stoi(argv[7]);
// cout<< "Cores USed-: "<< n_threads<<endl;
// cout << "Graph Name->" << filename1 << endl;
// cout <<"Batch Size->"<<edgeListsForInsertion[0].size() <<" | Number of batches->"<<num_insertion_batches<< " | Incremental Edges->" << edgeListsForInsertion[0].size() * num_insertion_batches << endl;
// cout <<"Batch Size->"<<edgeListsForDeletion[0].size() <<" | Number of batches->"<<num_deletion_batches<< " | Deletion Edges->" << edgeListsForInsertion[0].size() * num_insertion_batches << endl;
// cout << "MIS set size->" << maximalIndependentSet.size() << endl;
// //----------------------------------------------INCREMENTAL---------------------------------------------------------
// auto start = omp_get_wtime();
// double time_cum = 0;
// time_per_thread.clear();
// time_per_thread.resize(n_threads, 0.0);
// time_cum = 0;
// start = omp_get_wtime();
// for (int i = 0; i < num_insertion_batches; i++)
// {
// for (int j = 0; j < edgeListsForInsertion[i].size(); j++)
// {
// cout<<edgeListsForInsertion[i][j].first<<endl;
// PDMI_INC(graph, misforInsertionINC, edgeListsForInsertion[i][j]);
// double maxi = 0;
// for (int k = 0; k < time_per_thread.size(); k++)
// {
// // calculate the max element
// if (maxi < time_per_thread[k])
// {
// maxi = time_per_thread[k];
// }
// }
// time_cumm += maxi;
// }
// }
// auto end = omp_get_wtime();
// cout << endl;
// cout << "-------------------------------INCREMENTAL------------------------------" << endl;
// cout << "Cardinality changed by->(Insertion)" << abs(initialCard - static_cast<int>(misforInsertionINC.size())) << endl;
// cout << "Total Incremental Edges Insertion time: " << fixed << setprecision(5) << time_cumm * 1000 << " milliseconds" << endl;
// cout << "Average Incremental Edges Insertion time: " << fixed << setprecision(5) << time_cumm / num_deletion_batches * 1000 << " milliseconds" << endl;
// //----------------------------------------------BATCH_DYNAMIC---------------------------------------------------------
// time_per_thread.clear();
// time_per_thread.resize(n_threads, 0.0);
// time_cumm = 0;
// for (int i = 0; i < num_insertion_batches; i++)
// {
// int block, tid, start, end, it;
// omp_set_num_threads(n_threads);
// block = ceil(static_cast<float>(edgeListsForInsertion[i].size()) / n_threads);
// #pragma omp parallel private(tid, start, end, it) shared(misforInsertionBAT)
// {
// startTime = omp_get_wtime();
// tid = omp_get_thread_num();
// start = tid * block;
// end = std::min((tid + 1) * block, static_cast<int>(edgeListsForInsertion[i].size()));
// for (int j = start; j < end; j++)
// {
// bool firstIn = false, secondIn = false;
// it = 0;
// for (int k = 0; k < misforInsertionBAT.size(); k++)
// {
// if (edgeListsForInsertion[i][j].first == misforInsertionBAT[k])
// {
// {
// firstIn = true;
// it = k;
// }
// }
// if (edgeListsForInsertion[i][j].second == misforInsertionBAT[k])
// {
// {
// secondIn = true;
// }
// }
// }
// if (firstIn && secondIn)
// {
// misforInsertionBAT[it] = -1;
// vector<int> neighbours=graph.getNeighbors(edgeListsForInsertion[i][j].first);
// int p=0;
// for(int k=0;k<neighbours.size();k++)
// {
// bool shouldWeAdd=true;
// for(int l=0;l<graph.getNeighbors(neighbours[k]).size();l++){
// for(int m=0;m<misforInsertionBAT.size();m++){
// if(misforInsertionBAT[m]==graph.getNeighbors(neighbours[k])[l]){
// shouldWeAdd=false;
// break;
// }
// }
// }
// if(shouldWeAdd){
// misforInsertionBAT.push_back(neighbours[k]);
// }
// }
// }
// }
// endTime = omp_get_wtime();
// time_per_thread[tid] = endTime - startTime;
// }
// double maxi = 0;
// for (int k = 0; k < time_per_thread.size(); k++)
// {
// // calculate the max element
// if (maxi < time_per_thread[k])
// {
// maxi = time_per_thread[k];
// }
// }
// time_cumm += maxi;
// }
// int ca = 0;
// for (int i = 0; i < misforInsertionBAT.size(); i++)
// {
// if (misforInsertionBAT[i] == -1)
// {
// ca++;
// }
// }
// cout << endl;
// cout << "-------------------------------BATCHDYNAMIC------------------------------" << endl;
// cout << "Batch size of one small batch is-> " << edgeListsForInsertion[0].size() << endl;
// cout << "Change in Cardinality " << abs(ca) << endl;
// cout << "Parallel Edges Insertion time: " << fixed << setprecision(2) << time_cumm * 1000 << " milliseconds" << endl;
// cout << "Average Parallel Edges Insertion time: " << fixed << setprecision(2) << time_cumm / num_insertion_batches * 1000 << " milliseconds" << endl;
// cout << "---------------------------------------END-----------------------------------------" << endl<<endl<<endl;
// }
/**
* @file inc_Bat.cpp
* @brief Implementation PDMI_INC and PDMI_BAT algorithms
*/
#include <omp.h>
#include <iostream>
#include <vector>
#include <fstream>
#include <sstream>
#include <unordered_map>
#include <unordered_set>
#include <chrono>
#include <algorithm>
#include <filesystem>
#include <iomanip>
#include <math.h>
#include <set>
#include <stdbool.h>
#include <stdio.h>
// Global variables
int n_threads = 75;
using namespace std;
vector<double> time_per_thread;
/**
* @class Graph
* @brief Represents a graph using Compressed Sparse Row (CSR) format
*/
class Graph
{
public:
std::vector<int> csredges;
std::vector<int> csrindices;
int maxNode = -1;
/**
* @brief Checks if the given set of nodes forms an independent set
* @param mis Vector of nodes to check
* @return true if the set is independent, false otherwise
*/
bool isIndependentSet(vector<int> mis)
{
for (int v : mis)
{
for (int u : mis)
{
if (v != u && this->isAdjacent(v, u))
{
return false; // Not an independent set
}
}
}
return true;
}
/**
* @brief Checks if two nodes are adjacent in the graph
* @param u First node
* @param v Second node
* @return true if the nodes are adjacent, false otherwise
*/
bool isAdjacent(int u, int v) const
{
if (u >= csrindices.size() - 1 || v >= csrindices.size() - 1)
return false;
for (int i = csrindices[u]; i < csrindices[u + 1]; ++i)
{
if (csredges[i] == v)
return true;
}
return false;
}
/**
* @brief Gets the neighbors of a given node
* @param u Node to get neighbors for
* @return Vector of neighboring nodes
*/
std::vector<int> getNeighbors(int u) const
{
std::vector<int> neighbors;
if (u < csrindices.size() - 1)
{
for (int i = csrindices[u]; i < csrindices[u + 1]; ++i)
{
neighbors.push_back(csredges[i]);
}
}
if (u == csrindices.size() - 1)
{
for (int i = csrindices[u]; i < csredges.size(); i++)
{
neighbors.push_back(csredges[i]);
}
}
return neighbors;
}
};
Graph readCSR(string filename)
{
Graph graph;
std::vector<int> indices, edges;
std::ifstream file(filename);
if (file.is_open())
{
int nodes, edgesCount;
file >> nodes >> edgesCount;
std::string line;
bool x = 1;
// Read the indices array
while (std::getline(file, line))
{
if (line == "indices->")
{
while (std::getline(file, line))
{
if (line == "list->")
{
x = 0;
break;
}
indices.push_back(std::stoi(line));
}
while (std::getline(file, line))
{
edges.push_back(std::stoi(line) + 1);
}
break;
}
}
}
graph.maxNode = indices.size();
graph.csrindices.resize(indices.size() + 1, 0);
graph.csredges.resize(edges.size(),0);
graph.csrindices[0] = 0;
for (int i = 1; i <= indices.size(); i++)
{
graph.csrindices[i] = indices[i - 1];
}
for (int i = 0; i < edges.size(); i++)
{
graph.csredges[i] = edges[i] ;
}
cout << "Node->" << graph.maxNode + 1 << endl;
cout << "Edges->" << graph.csredges.size() << endl;
return graph;
}
/**
* @brief Performs incremental update of Maximal Independent Set
* @param graph The graph structure
* @param mis Vector representing the Maximal Independent Set
* @param edge Pair of nodes representing the new edge
*/
void PDMI_INC(Graph &graph, vector<int> &mis, pair<int, int> edge)
{
bool firstIn = false, secondIn = false;
int it = -1;
bool firstInLocal, secondInLocal;
double beg, en;
time_per_thread.clear();
time_per_thread.resize(n_threads);
// Parallel section to check if nodes are in MIS
#pragma omp parallel num_threads(n_threads) shared(firstIn, secondIn, it) private(firstInLocal, secondInLocal)
{
beg = omp_get_wtime();
int tid = omp_get_thread_num();
int start = tid * (mis.size() / n_threads);
int end = (tid == n_threads - 1) ? mis.size() : (tid + 1) * (mis.size() / n_threads);
firstInLocal = false, secondInLocal = false;
for (int i = start; i < end; i++)
{
if (edge.first == mis[i])
{
firstInLocal = true;
it = i;
}
if (edge.second == mis[i])
{
secondInLocal = true;
}
}
#pragma omp atomic update
firstIn |= firstInLocal;
#pragma omp atomic update
secondIn |= secondInLocal;
en = omp_get_wtime();
time_per_thread[tid] = (en - beg);
}
// Update MIS if both nodes of the new edge are in MIS
if (firstIn && secondIn && it != -1)
{
mis.erase(mis.begin() + it);
vector<int> neighbours = graph.getNeighbors(edge.first);
int p = 0;
for (int k = 0; k < neighbours.size(); k++)
{
bool shouldWeAdd = true;
for (int l = 0; l < graph.getNeighbors(neighbours[k]).size(); l++)
{
bool shouldweAddLocal = true;
#pragma omp parallel num_threads(n_threads) shared(graph, neighbours, mis, shouldWeAdd) private(shouldweAddLocal)
{
beg = omp_get_wtime();
int tid = omp_get_thread_num();
int start = tid * (mis.size() / n_threads);
int end = (tid == n_threads - 1) ? mis.size() : (tid + 1) * (mis.size() / n_threads);
for (int i = start; i < end; i++)
{
if (graph.getNeighbors(neighbours[k])[l] == mis[i])
{
shouldweAddLocal = false;
}
}
#pragma omp atomic update
shouldWeAdd |= shouldweAddLocal;
end = omp_get_wtime();
time_per_thread[tid] += end - beg;
}
if (shouldWeAdd)
{
mis.push_back(neighbours[k]);
}
}
}
}
}
/**
* @brief Main function to process graph and perform MIS operations
* @param argc Number of command-line arguments
* @param argv Array of command-line arguments
* @return 0 on successful execution
*/
int main(int argc, char *argv[])
{
// Check for correct number of command-line arguments
if (argc < 7)
{
std::cerr << "Usage: " << argv[0] << " <CSR> <MIS> <Deletion_folder> <Insertion_folder> <num_threads> <Num_batch for deletion> <Num_batch for insertion>" << std::endl;
return 1;
}
cout << "----------------------------------------Start-----------------------------------------" << endl;
cout << "----------------------------------------INC_BAT------------------------------------------" << endl;
// Read input files and initialize graph
std::string filename1 = argv[1];
std::string filename2 = argv[2];
Graph graph;
graph = readCSR(filename1);
std::vector<int> maximalIndependentSet;
std::string line;
// Read Maximal Independent Set
std::ifstream inputFile(argv[2]);
if (inputFile.is_open())
{
while (std::getline(inputFile, line))
{
maximalIndependentSet.push_back(std::stoi(line) + 1);
}
inputFile.close();
}
else
{
std::cerr << "Unable to open the file for reading" << std::endl;
}
// Initialize MIS for incremental and batch processing
vector<int> misforInsertionINC(maximalIndependentSet);
vector<int> misforInsertionBAT(maximalIndependentSet);
// Read edge lists for deletion and insertion
std::string folderPath(argv[3]);
std::vector<std::pair<int, int>> edgeList;
std::vector<std::vector<std::pair<int, int>>> edgeListsForDeletion;
for (const auto &entry : std::filesystem::directory_iterator(folderPath))
{
if (entry.is_regular_file())
{
std::ifstream file(entry.path());
std::string line;
while (std::getline(file, line))
{
std::istringstream iss(line);
int src, dest;
if (iss >> src)
{
if (!(iss >> dest))
{
dest = -1;
}
edgeList.push_back(std::make_pair(src, dest));
}
}
edgeListsForDeletion.push_back(edgeList);
edgeList.clear();
}
}
std::string folderPath1(argv[4]);
std::vector<std::vector<std::pair<int, int>>> edgeListsForInsertion;
for (const auto &entry : std::filesystem::directory_iterator(folderPath1))
{
if (entry.is_regular_file())
{
std::ifstream file(entry.path());
std::string line;
while (std::getline(file, line))
{
std::istringstream iss(line);
int src, dest;
if (iss >> src)
{
if (!(iss >> dest))
{
dest = -1;
}
edgeList.push_back(std::make_pair(src, dest));
}
}
edgeListsForInsertion.push_back(edgeList);
edgeList.clear();
}
}
// Initialize parameters
int initialCard = maximalIndependentSet.size();
n_threads = stoi(argv[5]);
double startTime = 0, endTime = 0, time_cumm = 0;
int num_deletion_batches = std::stoi(argv[6]);
int num_insertion_batches = std::stoi(argv[7]);
// Print initial information
cout << "Cores Used-: " << n_threads << endl;
cout << "Graph Name->" << filename1 << endl;
cout << "Batch Size->" << edgeListsForInsertion[0].size() << " | Number of batches->" << num_insertion_batches << " | Incremental Edges->" << edgeListsForInsertion[0].size() * num_insertion_batches << endl;
cout << "Batch Size->" << edgeListsForDeletion[0].size() << " | Number of batches->" << num_deletion_batches << " | Deletion Edges->" << edgeListsForInsertion[0].size() * num_insertion_batches << endl;
cout << "MIS set size->" << maximalIndependentSet.size() << endl;
//----------------------------------------------INCREMENTAL---------------------------------------------------------
auto start = omp_get_wtime();
double time_cum = 0;
time_per_thread.clear();
time_per_thread.resize(n_threads, 0.0);
time_cum = 0;
start = omp_get_wtime();
for (int i = 0; i < num_insertion_batches; i++)
{
for (int j = 0; j < edgeListsForInsertion[i].size(); j++)
{
// cout << edgeListsForInsertion[i][j].first << endl;
PDMI_INC(graph, misforInsertionINC, edgeListsForInsertion[i][j]);
double maxi = 0;
for (int k = 0; k < time_per_thread.size(); k++)
{
// calculate the max element
if (maxi < time_per_thread[k])
{
maxi = time_per_thread[k];
}
}
time_cumm += maxi;
}
}
auto end = omp_get_wtime();
cout << endl;
cout << "-------------------------------INCREMENTAL------------------------------" << endl;
cout << "Cardinality changed by->(Insertion)" << abs(initialCard - static_cast<int>(misforInsertionINC.size())) << endl;
cout << "Total Incremental Edges Insertion time: " << fixed << setprecision(5) << time_cumm * 1000 << " milliseconds" << endl;
cout << "Average Incremental Edges Insertion time: " << fixed << setprecision(5) << time_cumm / num_deletion_batches * 1000 << " milliseconds" << endl;
//----------------------------------------------BATCH_DYNAMIC---------------------------------------------------------
time_per_thread.clear();
time_per_thread.resize(n_threads, 0.0);
time_cumm = 0;
for (int i = 0; i < num_insertion_batches; i++)
{
int block, tid, start, end, it;
omp_set_num_threads(n_threads);
block = ceil(static_cast<float>(edgeListsForInsertion[i].size()) / n_threads);
#pragma omp parallel private(tid, start, end, it) shared(misforInsertionBAT)
{
startTime = omp_get_wtime();
tid = omp_get_thread_num();
start = tid * block;
end = std::min((tid + 1) * block, static_cast<int>(edgeListsForInsertion[i].size()));
for (int j = start; j < end; j++)
{
bool firstIn = false, secondIn = false;
it = 0;
for (int k = 0; k < misforInsertionBAT.size(); k++)
{
if (edgeListsForInsertion[i][j].first == misforInsertionBAT[k])
{
{
firstIn = true;
it = k;
}
}
if (edgeListsForInsertion[i][j].second == misforInsertionBAT[k])
{
{
secondIn = true;
}
}
}
if (firstIn && secondIn)
{
misforInsertionBAT[it] = -1;
vector<int> neighbours = graph.getNeighbors(edgeListsForInsertion[i][j].first);
int p = 0;
for (int k = 0; k < neighbours.size(); k++)
{
bool shouldWeAdd = true;
for (int l = 0; l < graph.getNeighbors(neighbours[k]).size(); l++) {
for (int m = 0; m < misforInsertionBAT.size(); m++) {
if (misforInsertionBAT[m] == graph.getNeighbors(neighbours[k])[l]) {
shouldWeAdd = false;
break;
}
}
}
if (shouldWeAdd) {
misforInsertionBAT.push_back(neighbours[k]);
}
}
}
}
endTime = omp_get_wtime();
time_per_thread[tid] = endTime - startTime;
}
double maxi = 0;
for (int k = 0; k < time_per_thread.size(); k++)
{
// calculate the max element
if (maxi < time_per_thread[k])
{
maxi = time_per_thread[k];
}
}
time_cumm += maxi;
}
int ca = 0;
for (int i = 0; i < misforInsertionBAT.size(); i++)
{
if (misforInsertionBAT[i] == -1)
{
ca++;
}
}
cout << endl;
cout << "-------------------------------BATCHDYNAMIC------------------------------" << endl;
cout << "Batch size of one small batch is-> " << edgeListsForInsertion[0].size() << endl;
cout << "Change in Cardinality " << abs(ca) << endl;
cout << "Parallel Edges Insertion time: " << fixed << setprecision(2) << time_cumm * 1000 << " milliseconds" << endl;
cout << "Average Parallel Edges Insertion time: " << fixed << setprecision(2) << time_cumm / num_insertion_batches * 1000 << " milliseconds" << endl;
cout << "---------------------------------------END-----------------------------------------" << endl << endl << endl;
return 0;
}