forked from swap612/Parallel-All-Pair-Shortest-Path
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathBlocked_Parallel.c
217 lines (188 loc) · 6.37 KB
/
Blocked_Parallel.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
/***********************************************************************************************
* File: Blocked_Parallel.c *
* Description: Blocked Parallel Version of APSP *
* *
* Author: Group-10 (Jatin and Swapnil) *
* *
***********************************************************************************************/
#include <stdio.h>
#include <stdlib.h>
#include "mpi.h"
#include<time.h>
#include<string.h>
#define INF 99999
int min(int a, int B)
{
if (a < B)
return a;
else
return B;
}
int floyd(int **C, int **A, int **B, int block_size)
{
int i, j, k;
for (k = 1; k <= block_size; k++)
for (j = 1; j <= block_size; j++)
for (i = 1; i <= block_size; i++)
{
C[i][j] = min(C[i][j], A[i][k] + B[k][j]);
}
}
int main(int argc, char *argv[])
{
MPI_Init(&argc, &argv);
MPI_Status status;
int myrank, size;
if (argc != 3)
{
printf("Invalid Arguments !! \n");
printf("Usage: %s Filename VerticesCount \n", argv[0]);
return -1;
}
char *inputfile = argv[1];
int V = atoi(argv[2]);
MPI_Comm_rank(MPI_COMM_WORLD, &myrank);
MPI_Comm_size(MPI_COMM_WORLD, &size);
double proc_time, start_time, total_time, sum_time, max_time;
//getting mpi comm size
int B = V / size;
int numbytes = ((V * B) * sizeof(int));
int *sdbuf;
sdbuf = (int *)malloc(numbytes);
// Reading the file input
MPI_File fileHandle;
int mode = MPI_MODE_RDWR;
start_time = MPI_Wtime();
MPI_File_open(MPI_COMM_WORLD, inputfile, mode, MPI_INFO_NULL, &fileHandle);
MPI_Offset disp = (MPI_Offset)myrank * numbytes;
MPI_File_set_view(fileHandle, disp, MPI_INT, MPI_INT, "native", MPI_INFO_NULL);
int result = MPI_File_read(fileHandle, sdbuf,V*B, MPI_INT, &status);
//printf("result=%d\n",result);
MPI_File_close(&fileHandle);
int *myarr = sdbuf;
//temp, 2-d array to store computed pivot
int *temp = (int *)malloc(((B * B)) * sizeof(int));
// for (int i = 0; i < size; i++)
// printf("%d %d \n", myrank, myarr[i]);
int *recvarr = (int *)malloc(((V * V) / size) * sizeof(int));
int k, j;
// printf("B= %d\n",B);
// if (myrank == 0)
// {
// for (int i = 0; i < V*B; i++)
// {
// printf("%d\t", sdbuf[i]);
// if (i % V == V - 1)
// printf("\n");
// }
// }
// // //
for (k = 1; k <= size; k++)
{
printf("%d iteration\n", k);
if (myrank == k-1)
{
for (int k_i = 0; k_i < B; k_i++)
{
for (int j = 0; j < B; j++)
for (int i = 0; i <B; i++)
{
*((myarr + (V * i) + (k - 1) * B + j)) = min(*(myarr + (V * i) + (k - 1) * B + j), *(myarr + (V * i) + (k - 1) * B + k_i) + *(myarr + (V * k_i) + (k - 1) * B + j));
//A[i][j]=min(A[i][j],A[i][k]+A[k][j])
//store to temp array
temp[i * B + j] = *((myarr + (V * i) + (k - 1) * B + j));
}
}
}
// 2.broadcast this pivot block(temp) to all processes
// MPI_Bcast(temp, (B * B), MPI_INT, k - 1, MPI_COMM_WORLD);
//3.computing row ,column pivots blocks
//computing row if myrank is k-1
//4.for the row pivots take the current block and itself to compute the block floyd(KK,Kj)
if (myrank == k - 1)
{
for (int m = 1; m <=size; m++)
{
if (m != k)
{
for (int k_i = 0; k_i < B; k_i++)
for (int j = 0; j < B; j++)
for (int i =0; i < B; i++)
{
*((myarr + (V * i) + (m - 1) * B + j)) = min(*(myarr + (V * i) + (m - 1) * B + j), *(myarr + (V * k_i) + (m - 1) * B + j) + temp[i * B + k_i]);
//A[i][j]=min(A[i][j],A[i][k]+A[k][j])
}
}
}
}
// 5.for the column pivots take the temp block and itself to compute the block (ik,kk)
// computing column
//copy myarr to recvarr
if(myrank==k-1)
{
for (int i = 0; i < V * B; i++)
recvarr[i] = myarr[i];
}
//6.broadcast columns pivots,and calculate.for the row pivots don't send.compute.
MPI_Bcast(recvarr, (V * B), MPI_INT, k - 1, MPI_COMM_WORLD);
int k_i, i, m;
if (myrank != k - 1)
{
for (k_i = 0; k_i < B; k_i++)
for (j = 0; j <B; j++)
for (i = 0; i < B; i++)
{
*((myarr + (V * i) + (k - 1) * B+j) )= min(*(myarr + (V * i) + (k - 1) * B + j), *(myarr + (V * i) + (k - 1) * B + k_i) + *(recvarr + (V * k_i) + (k - 1) * B + j));
//A[i][j]=min(A[i][j],A[i][k]+A[k][j])
}
for (m = 1; m <= size; m++)
{
if (m != k)
{
for (k_i = 0; k_i < B; k_i++)
for (j = 0; j < B; j++)
for (i = 0; i < B; i++)
{
*((myarr + (V * i) + (m - 1) * B + j)) = min(*(myarr + (V * i) + (m - 1) * B + j), *(myarr + (V * i) + (k- 1) * B + k_i) + *(recvarr + (V * k_i) + (m - 1) * B + j));
//A[i][j]=min(A[i][j],A[i][k]+A[k][j])
}
}
}
}
// MPI_Barrier(MPI_COMM_WORLD);
}
// printf("after iteration ---%d--value is--%d\n", myrank, temp);
int *rbuf = NULL;
if (myrank == 0)
rbuf = (int *)malloc((V*V) * sizeof(int));
MPI_Gather(myarr, V*B, MPI_INT, rbuf, V*B, MPI_INT, 0, MPI_COMM_WORLD);
/* if (myrank == 0)
{
for (int i = 0; i < (V*V); i++)
{
printf("%d\t", rbuf[i]);
if (i % V == V - 1)
printf("\n");
}
}
*/
double end_time=MPI_Wtime();
proc_time=end_time-start_time;
MPI_Reduce(&proc_time, &max_time, 1,MPI_DOUBLE, MPI_MAX, 0,MPI_COMM_WORLD);
// Writing the output to file
/* MPI_File fh;
mode = MPI_MODE_CREATE | MPI_MODE_RDWR;
MPI_File_open(MPI_COMM_WORLD, "output.txt", mode, MPI_INFO_NULL, &fh);
disp = (MPI_Offset)myrank * numbytes;
MPI_File_set_view(fh, disp, MPI_INT, MPI_INT, "native", MPI_INFO_NULL);
// int result = MPI_File_read(fileHandle, sdbuf,V*B, MPI_INT, &status);
MPI_File_write_at(fh, disp, myarr, V*B, MPI_INT, &status);
MPI_File_close(&fh);
*/
if(myrank==0)
{
printf(" %d, %d ,%f \n", V, size, max_time);
}
MPI_Finalize();
return 0;
}