The computation works as follows:
Each process finds the locally stored vertex in V0 that has the smallest distance from the source. Next the vertex that has the smallest distance over all processes is determined at Process 0 using Reduce operation. This vertex is then broadcast from process 0 to all other processes. Last,all the processes update their distances to reflect the inclusion of the new vertex in Vc.
#include
#include
#include
#include
#include
#include
#define MAX_INT 65535
main(int argc,char *argv[])
{
int input_AL[3][6][2] = {
{{0,1},{1,0},{3,5},{MAX_INT,1},{MAX_INT,MAX_INT},{2,MAX_INT}},
{{3,MAX_INT},{5,1},{0,2},{2,0},{1,4},{MAX_INT,MAX_INT}},
{{MAX_INT,3},{MAX_INT,MAX_INT},{1,MAX_INT},{4,MAX_INT},{0,5},{5,0}}
};
int rank,n,p,nlocal;
int i,j,firstvtx,lastvtx;
int source;
int lminpair[2],gminpair[2];
int *u_vector,u;
int *d,*marker;
MPI_Status status;
MPI_Init(&argc,&argv);
MPI_Comm_size(MPI_COMM_WORLD,&p);
MPI_Comm_rank(MPI_COMM_WORLD,&rank);
n = 6; //number of vertices
source = 1;//vertex b
nlocal = n/p; //number of vertices stored locally
firstvtx = rank*nlocal;//index number of the first vertex stored locally
lastvtx = firstvtx + nlocal - 1;//index of the last vertex stored locally
printf("Input Adjacency List at process %d\n",rank);
for(i=0;i<6;i++)
{
for(j=0;j<2;j++)
{
printf("%d\t", input_AL[rank][i][j]);
}
printf("\n");
}
printf("\n\n");
/*Set the initial distances from source to all other vertices*/
d = (int *)malloc(nlocal *(sizeof(int)));
for(j=0;j
printf("Step 1 :: Minimum Spanning Tree :: Vector d at Process %d\n",rank);
for(j=0;j
printf("\n\n");
/*This array is used to indicate if the shortest path to a vertex has been found or not.If marker[v] is zero, then the shortest path to v has been found,otherwise it has not*/
marker = (int *)malloc(nlocal *(sizeof(int)));
for(j=0;j
/*the process that stores the source vertex,marks it as being seen*/
if(source >= firstvtx && source <= lastvtx)
{
marker[source-firstvtx] = 0;
}
for(i=1;i
/*Find the local vertex that is at the smallest distance from source*/
lminpair[0] = MAX_INT;
lminpair[1] = -1;
for(j=0;j
if(marker[j]&&d[j]
lminpair[0] = d[j];
lminpair[1]=firstvtx+j;
}
}
u_vector =(int *)malloc(2*sizeof(int));
/*Compute the global minimum vertex at Process 0 and insert in into Vc*/
MPI_Reduce(lminpair,gminpair,1,MPI_2INT,MPI_MINLOC,0,MPI_COMM_WORLD);
if(rank == 0)
{
u_vector[0]=gminpair[1];
u=gminpair[1];
printf("Step %d :: GLOBAL MINIMUM VERTEX %d at Process %d\n",i+1,gminpair[1],rank);
}
/*Broadcast global minimum vertex from Process 0 to all other processes*/
MPI_Bcast(u_vector,2,MPI_INT,0,MPI_COMM_WORLD);
u=u_vector[0];
printf("Vertex received at Process %d after Broadcast %d\n",rank,u);
/*the process that stores the minimum vertex,marks it as being seen*/
if(u==lminpair[1])
marker[u-firstvtx] = 0;
/*Update the distances given that u got inserted into Vc*/
for(j=0;j
if(marker[j] && input_AL[rank][u][j] < d[j])
{
d[j] = input_AL[rank][u][j];
}
}
printf("Step %d :: Minimum Spanning Tree :: Vector d at Process %d\n",i+1,rank);
for(j=0;j
printf("\n\n\n");
}//for(i=1;i
printf("---------------------------------------------------------\n");
printf("Minimum Spanning Tree :: Final Vector d at Process %d\n",rank);
for(j=0;j
printf("\n---------------------------------------------------------\n\n\n");
MPI_Finalize();
}//main
No comments:
Post a Comment