-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathreader.hpp
More file actions
385 lines (353 loc) · 12.3 KB
/
Copy pathreader.hpp
File metadata and controls
385 lines (353 loc) · 12.3 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
#pragma once
#include <iostream>
#include <fstream>
#include <vector>
#include <unordered_set>
#include<sys/mman.h>
#include<sys/stat.h>
#include<fcntl.h>
#define chunk_height 3
#define chunk_width 5
#define num_partitions 3
#define root 0
using namespace std;
//This function is for memory mapping. Each reading function would call this with their respective files as arg1
const char* get_file_map_info(const char* fname, size_t& num_bytes, int& world_rank){
int fd = open(fname, O_RDONLY); // 19
if(fd == -1){
if (world_rank == 0){
cerr<< "error opening the file" << endl;
}
return fname; // if there is an error, just returning the filename back
}
struct stat sb; // 20
if(fstat(fd, &sb) == -1){
if (world_rank == 0){
cerr << " error in fstat " << endl ;
}
return fname;
}
num_bytes = sb.st_size;
const char * addr = static_cast<const char*> (mmap(NULL, num_bytes,PROT_READ, MAP_PRIVATE, fd, 0u ));
//addr[0], addr[1] give the first and 2nd character in file
if(addr == MAP_FAILED){
if(world_rank == 0){
cerr << "mmap failed";
}
return fname;
}
return addr;
}
//This function reads the partition id for each vertex from the partition file. The global_SCC disjoint set is also allocated here. Each
void read_partitions(char *argv[], Basic& basic, Graph& graph)
{
char letter = '\0'; // 47
int char_count = 0; // 48 keeps track of number of characters in each file
int lineno=0;
size_t num_bytes_partition = 0; // 14
char* buffer = new char[64]();
const char *partition_pointer = get_file_map_info(argv[6], num_bytes_partition, world_rank);
for (int i = 0; i < num_bytes_partition; i++)
{
letter = partition_pointer[i];
char_count++;
if (letter == '\n')
{
for (int j = 0; j < char_count -1; j++)
{
buffer[j] = partition_pointer[i - (char_count -1) + j];
}
buffer[char_count - 1 ] = '\0';
char_count = 0;
int pid=atoi(buffer);
basic.partition_of_vertex.insert({lineno, pid});
if(world_rank == pid)
{
basic.allocated_vertices.push_back(lineno);
}
lineno++;
}
}
}
//Function for reading the graph. Each process reads only the edges it is allocated by looking up at the partition hash map
void read_graph(char *argv[], Basic &basic, Graph& graph, int world_rank)
{
int node1,node2;
char letter = '\0';
int char_count = 0;
int lineno=0;
size_t num_bytes_input = 0;
char* buffer = new char[64]();
char* token = nullptr;
const char *input_pointer = get_file_map_info(argv[1], num_bytes_input, world_rank);
for (int i = 0; i < num_bytes_input; i++)
{
letter = input_pointer[i];
char_count++;
if (letter == '\n')
{
for (int j = 0; j < char_count -1; j++)
{
buffer[j] = input_pointer[i - (char_count -1) + j];
}
buffer[char_count - 1 ] = '\0';
char_count = 0;
token = strtok(buffer, " ");
node1=atoi(token);
token = strtok(NULL, " ");
node2=atoi(token);
//logic for allocating edges based on partition
if(basic.partition_of_vertex.at(node1) != basic.partition_of_vertex.at(node2)) //Edge across partition
{
//Here we allocate an edge to the two processes that holds the vertices
if(world_rank == basic.partition_of_vertex.at(node1) or world_rank == basic.partition_of_vertex.at(node2))
{
boost::add_edge (node1, node2, graph); //Storing in boost ajacency list.
vector<int> temp;
temp.push_back(node1);
temp.push_back(node2);
basic.allocated_graph.push_back(temp); //Storing in vectors for seperate use
//store border vertices and store the vertices a specific border vertex has outgoing edges to. This is stored seperately and is only used for merging. Not included while performing local SCC.
if(world_rank == basic.partition_of_vertex.at(node1))
{
if(basic.border_out_vertices.find(node1) == basic.border_out_vertices.end())//border vertex not yet added
{
vector<int> borders;
borders.push_back(node2);
basic.border_out_vertices.insert({node1, borders});
}
else //border vertex already exists. Push oppopsite vertex to vector mapped with border vertex
{
basic.border_out_vertices[node1].push_back(node2);
}
}
//Similarly, store borders of incoming edges in another hashmap
if(world_rank == basic.partition_of_vertex.at(node2))
{
if(basic.border_in_vertices.find(node2) == basic.border_in_vertices.end())
{
vector<int> borders;
borders.push_back(node1);
basic.border_in_vertices.insert({node2, borders});
}
else //border vertex already exists. Push oppopsite vertex to vector mapped with border vertex
{
basic.border_in_vertices[node2].push_back(node1);
}
}
}
}
else//Edge within the same partition.
{
//Here we allocate an edge only to the process that holds both the vertices
if(world_rank == basic.partition_of_vertex.at(node1))
{
boost::add_edge (node1, node2, graph); //boost graph
vector<int> temp;
temp.push_back(node1);
temp.push_back(node2);
basic.allocated_graph.push_back(temp); //Storing in vectors for seperate use
}
}
lineno++;
}
}
local_size=boost::num_vertices (graph);
basic.local_scc.reserve(local_size);
}
void read_changes(char *argv[], Basic &basic, Graph& changes, Graph& graph, int world_rank)
{
int node1,node2;
char letter = '\0';
int char_count = 0;
int lineno=0;
size_t num_bytes_changes = 0;
char* buffer = new char[64]();
char* token = nullptr;
const char *changes_pointer = get_file_map_info(argv[3], num_bytes_changes, world_rank);
for (int i = 0; i < num_bytes_changes; i++)
{
letter = changes_pointer[i];
char_count++;
if (letter == '\n')
{
for (int j = 0; j < char_count -1; j++)
{
buffer[j] = changes_pointer[i - (char_count -1) + j];
}
buffer[char_count - 1 ] = '\0';
char_count = 0;
token = strtok(buffer, " ");
node1=atoi(token);
token = strtok(NULL, " ");
node2=atoi(token);
//logic for allocating edges based on partition
if(basic.partition_of_vertex.at(node1) != basic.partition_of_vertex.at(node2)) //Edge across partition
{
//Here we allocate an edge to the two processes that holds the vertices
if(world_rank == basic.partition_of_vertex.at(node1) or world_rank == basic.partition_of_vertex.at(node2))
{
//boost::add_edge (node1, node2, graph); //Storing in boost ajacency list.
//store border vertices and store the vertices a specific border vertex has outgoing edges to. This is stored seperately and is only used for merging. Not included while performing local SCC.
if(world_rank == basic.partition_of_vertex.at(node1))
{
if(basic.border_out_vertices.find(node1) == basic.border_out_vertices.end())//border vertex not yet added
{
vector<int> borders;
borders.push_back(node2);
basic.border_out_vertices.insert({node1, borders});
}
else //border vertex already exists. Push oppopsite vertex to vector mapped with border vertex
{
basic.border_out_vertices[node1].push_back(node2);
}
}
//Similarly, store borders of incoming edges in another hashmap
if(world_rank == basic.partition_of_vertex.at(node2))
{
if(basic.border_in_vertices.find(node2) == basic.border_in_vertices.end())
{
vector<int> borders;
borders.push_back(node1);
basic.border_in_vertices.insert({node2, borders});
}
else //border vertex already exists. Push oppopsite vertex to vector mapped with border vertex
{
basic.border_in_vertices[node2].push_back(node1);
}
}
}
}
else//Edge within the same partition.
{
//Here we allocate an edge only to the process that holds both the vertices
if(world_rank == basic.partition_of_vertex.at(node1))
{
boost::add_edge (node1, node2, changes);
boost::add_edge (node1, node2, graph); //Adding it also to input graph for now to recompute changes. Should remove it when the shared SCC is ready.
vector<int> temp;
temp.push_back(node1);
temp.push_back(node2);
basic.allocated_graph.push_back(temp); //Storing in vectors for seperate use
}
}
lineno++;
}
}
}
void read_sccmap(char *argv[], Basic &basic, int world_rank)
{
char letter = '\0'; // 47
int char_count = 0; // 48 keeps track of number of characters in each file
int lineno=0;
size_t num_bytes_sccmap = 0; // 14
char* buffer = new char[64]();
const char *sccmap_pointer = get_file_map_info(argv[2], num_bytes_sccmap, world_rank);
for (int i = 0; i < num_bytes_sccmap; i++)
{
letter = sccmap_pointer[i];
char_count++;
if (letter == '\n')
{
for (int j = 0; j < char_count -1; j++)
{
buffer[j] = sccmap_pointer[i - (char_count -1) + j];
}
buffer[char_count - 1 ] = '\0';
char_count = 0;
int map=atoi(buffer);
basic.init_scc_of_vertex.insert({lineno, map});
lineno++;
}
}
}
void display(Basic &basic, Graph &graph, int world_rank)
{
ofstream vertex_dump("dump/ver_" + std::to_string(world_rank) + ".txt");
ofstream par_dump("dump/par_" + std::to_string(world_rank) + ".txt");
ofstream scc_dump("dump/file_no_" + std::to_string(world_rank) + ".txt");
ofstream out_dump("dump/rel_" + std::to_string(world_rank) + ".txt");
ofstream inter_dump("dump/int_" + std::to_string(world_rank) + ".txt");
ofstream probe_dump("dump/probe_" + std::to_string(world_rank) + ".txt");
ofstream l_scc_dump("dump/l_scc_" + std::to_string(world_rank) + ".txt");
ofstream updated_result("dump/result" + std::to_string(world_rank) + ".txt");
//ofstream map_dump("dump/b_out_v" + std::to_string(world_rank) + ".txt");
ofstream dump_bor("dump/scc_hash" + std::to_string(world_rank) + ".txt");
ofstream b_in_dump("dump/b_in_v" + std::to_string(world_rank) + ".txt");
ofstream b_out_dump("dump/b_out_v" + std::to_string(world_rank) + ".txt");
//display vertices in partition
for(auto itr:basic.allocated_vertices)
{
vertex_dump<<itr<<endl;
}
vector< vector<int> >::iterator row;
vector<int>::iterator col;
//----------------------------
//Display local scc map
for(auto itr : basic.local_scc_map)
{
l_scc_dump<<itr.first<<" : "<<itr.second<<endl;
}
// //Display borders_out of scc
// for(auto itr:basic.borders_out_of_scc)
// {
// out_dump<<itr.first<<" : ";
// for(auto i : itr.second)
// out_dump<<i<<" ";
// out_dump<<endl;
// }
// //Display borders_in of scc
// for(auto itr:basic.borders_in_of_scc)
// {
// inter_dump<<itr.first<<" : ";
// for(auto i : itr.second)
// inter_dump<<i<<" ";
// inter_dump<<endl;
// }
// //Display local SCC
// for (int i = 0; i < boost::num_vertices (graph); ++i)
// {
// if(basic.partition_of_vertex[i]==world_rank)
// scc_dump << basic.local_scc[i] << " ";
// }
// //display border_in
// for(auto itr : basic.border_in_vertices)
// {
// b_in_dump<<itr.first<<" : ";
// for(auto i : itr.second)
// {
// b_in_dump<<i<<" ";
// }
// b_in_dump<<endl;
// }
// //display border_out
// for(auto itr : basic.border_out_vertices)
// {
// b_out_dump<<itr.first<<" : ";
// for(auto i : itr.second)
// {
// b_out_dump<<i<<" ";
// }
// b_out_dump<<endl;
// }
// //Display meta_in_out
// for(auto itr : basic.meta_in_out)
// {
// dump_bor<<itr.first<<" : ";
// for(auto i : itr.second[0])
// {
// dump_bor<<i<<" ";
// }
// dump_bor<<" :: ";
// for(auto j : itr.second[1])
// {
// dump_bor<<j<<" ";
// }
// dump_bor<<endl;
// }
// //Display partial meta edges
// for(auto itr : basic.partial_meta_edge)
// {
// par_dump<<itr.first<<" "<<itr.second<<endl;
// }
}