-
Notifications
You must be signed in to change notification settings - Fork 1
/
bfs.c
372 lines (320 loc) · 9.55 KB
/
bfs.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
/*
* Imports
*/
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#include <string.h>
#include <mpi.h>
#include <omp.h>
#include "filing.c"
#include "queue.c"
/* Constants */
#define PRINT_VECS 1 // flag so we can turn off printing when N is large
#define MAX_RAND 100 // max value of elements generated for array
/* Prototypes */
void init_vec(int *vec, int len);
void print_vec(const char *label, int *vec, int len);
/* Functions */
// Fills a vector with random integers in the range [0, MAX_RAND)
void init_vec(int *vec, int len)
{
int i;
for (i = 0; i < len; i++)
{
vec[i] = rand() % MAX_RAND;
}
}
// Prints the given vector to stdout
void print_vec(const char *label, int *vec, int len)
{
#if PRINT_VECS
printf("%s", label);
int i;
for (i = 0; i < len; i++)
{
printf("%d ", vec[i]);
}
printf("\n\n");
#endif
}
void bfs_sequential(int** graph, int source, int n)
{
int visited[n];
struct Queue* q = createQueue();
for(int i = 0; i < n; i++)
visited[i] = 0;
visited[source] = 1;
enqueue(q, source);
while(!isEmpty(q)){
int u = dequeue(q);
for(int v = 0; v < n; v++)
{
if(graph[u][v] == 1)
{
if(visited[v] == 0){
visited[v] = 1;
enqueue(q, v);
}
}
}
}
}
void bfs_sequential_top_down(int** graph, int source, int n)
{
int parent[n];
for(int i = 0; i < n; i++)
parent[i] = -1;
parent[source] = source;
struct Queue* frontier = createQueue();
enqueue(frontier, source);
struct Queue* next = NULL;
while(frontier != NULL)
{
while(!isEmpty(frontier)){
int u = dequeue(frontier);
for(int v = 0; v < n; v++)
{
if(graph[u][v] == 1)
{
if(next == NULL)
{
next = createQueue();
}
if(parent[v] == -1)
{
enqueue(next, v);
parent[v] = u;
}
}
}
}
frontier = next; next = NULL;
}
}
void bfs_sequential_bottom_up(int** graph, int source, int n)
{
int parent[n];
for(int i = 0; i < n; i++)
parent[i] = -1;
parent[source] = source;
struct Queue* frontier = createQueue();
enqueue(frontier, source);
struct Queue* next = NULL;
while(frontier != NULL)
{
for(int u = 0; u < n; u++)
{
if(parent[u] == -1)
{
for(int v = 0; v < n; v++)
{
if(graph[u][v] == 1 && isVInQueue(frontier, v) == 1)
{
if(next == NULL)
{
next = createQueue();
}
enqueue(next, u);
parent[u] = v;
break;
}
}
}
}
frontier = next; next = NULL;
}
}
int main(int argc, char *argv[])
{
// Declare process-related vars
// and initialize MPI
int rank;
int num_procs;
MPI_Init(&argc, &argv);
MPI_Comm_rank(MPI_COMM_WORLD, &rank); //grab this process's rank
MPI_Comm_size(MPI_COMM_WORLD, &num_procs); //grab the total num of processes
int** graph;
double start_time;
double stop_time;
char* file_name = "Sparse500.txt";
int n = get_n(file_name);
// init graph nxn matrix
graph = (int **)malloc(n * sizeof(int *));
for (int i = 0; i < n; i++)
{
graph[i] = (int *)malloc(n * sizeof(int));
for (int j = 0; j < n; j++)
{
graph[i][j] = 0;
}
}
load(graph, file_name);
if(!rank)
{
int edges = 0;
for (int i = 0; i < n; i++)
{
for (int j = 0; j < n; j++)
{
if(graph[i][j] == 1)
edges++;
}
}
printf("Edges: %d\n", edges);
printf("Procs: %d\n", num_procs);
printf("File: %s\n", file_name);
}
srand(time(NULL));
start_time = MPI_Wtime();
int chunk_size = n / num_procs;
int owner[n];
// assign first chunk to proc 0
for(int i = 0; i < chunk_size; i++)
{
owner[i] = 0;
}
// assign othe chunks to respective processors
int curr_owner = 0;
for(int i = chunk_size; i < n; i++)
{
if(i % chunk_size == 0 && curr_owner < num_procs - 1)
curr_owner++;
owner[i] = curr_owner;
}
int source = 0;
int visited[n];
// init visited array for each proc
for(int i = 0; i < n; i++)
visited[i] = 0;
// init queue for each proc
struct Queue* frontier = createQueue();
// if proc 0, enqueue root
if(rank == 0)
{
for(int v = 0; v < n; v++)
{
if(graph[source][v] == 1)
{
enqueue(frontier, v);
}
}
visited[source] = 1;
}
// init 2D send buffer for each proc
int **sendBuf = (int **)malloc(num_procs * sizeof(int *));
for (int i = 0; i < num_procs; i++)
{
sendBuf[i] = (int *)malloc(n * sizeof(int));
for(int j = 0; j < n; j++)
sendBuf[i][j] = 0;
}
// init 1D recv buffer for each proc
int* recvBuf = (int *)malloc(n * sizeof(int));
int sendTo[n];
for(int i = 0; i < n; i++)
sendTo[i] = 0;
// BFS
while(frontier != NULL)
{
struct Queue* remote = createQueue();
struct Queue* local = createQueue();
// set local and remote vertices
assignLocalAndRemoteVertices(local, remote, frontier, rank, owner);
free(frontier);
frontier = NULL;
// add vertices from remote to send buf
while(!isEmpty(remote))
{
int v = dequeue(remote);
sendBuf[owner[v]][v] = 1;
sendTo[owner[v]] = 1;
}
// send respective remote vertices
MPI_Request request;
for(int proc = 0; proc < num_procs; proc++)
{
if(proc != rank && sendTo[proc] == 1)
{
MPI_Isend(sendBuf[proc], n, MPI_INT, proc, 1, MPI_COMM_WORLD, &request);
sendTo[proc] = 0;
}
}
// process local vertices
while(!isEmpty(local))
{
int v = dequeue(local);
if(visited[v] == 0)
{
int u;
#pragma omp parallel for private(u)
for(u = 0; u < n; u++)
if(graph[v][u] == 1)
{
#pragma omp critical
{
if(frontier == NULL)
{
frontier = createQueue();
}
enqueue(frontier, u);
}
}
visited[v] = 1;
}
}
// recv vertices from remote and enqueue neighbours in frontier
MPI_Status status;
MPI_Recv(recvBuf, n, MPI_INT, MPI_ANY_SOURCE, MPI_ANY_TAG, MPI_COMM_WORLD, &status);
int j;
#pragma omp parallel for private(j)
for(j = 0; j < n; j++)
{
if(recvBuf[j] == 1)
{
if(visited[j] == 0)
{
for(int k = 0; k < n; k++)
{
if(graph[j][k] == 1)
{
#pragma omp critical
{
if(frontier == NULL)
{
frontier = createQueue();
}
enqueue(frontier, k);
}
}
}
visited[j] = 1;
}
}
}
}
// wait for all procs to finish -- so execution time noted is correct
MPI_Barrier(MPI_COMM_WORLD);
if (!rank)
{
stop_time = MPI_Wtime();
printf("Total time (sec): %f\n", stop_time - start_time);
start_time = MPI_Wtime();
bfs_sequential(graph, 0, n);
stop_time = MPI_Wtime();
printf("Total time BFS_Sequential (sec): %f\n", stop_time - start_time);
printf("BFS TOP DOWN\n");
start_time = MPI_Wtime();
bfs_sequential_top_down(graph, 0, n);
stop_time = MPI_Wtime();
printf("Total time BFS_Sequential Top-Down (sec): %f\n", stop_time - start_time);
printf("BFS BOTTOM UP\n");
start_time = MPI_Wtime();
bfs_sequential_bottom_up(graph, 0, n);
stop_time = MPI_Wtime();
printf("Total time BFS_Sequential Bottom-Up (sec): %f\n", stop_time - start_time);
}
// Shutdown MPI (important - don't forget!)
MPI_Finalize();
return EXIT_SUCCESS;;
}