Вы находитесь на странице: 1из 7

PARALLEL MERGE SORT

#include<stdio.h>
#include<mpi.h>
#include<stdlib.h>

#define SIZE 100

void merge(int *x,int l,int m,int n,int *y)


{
int i,j,k,g;
i=l;
k=l;
j=m+1;
while((i<=m) && (j<=n))
{
if(x[i]<=x[j])
{
y[k] = x[i];
i++;
}
else
{
y[k] = x[j];
j++;
}
k++;
}
if(i>m)
{
for(g=j;g<=n;g++)
{
y[k]=x[g];
k++;
}
}
else
{
for(g=i;g<=m;g++)
{
y[k]=x[g];
k++;
}
}
}
void mpass(int *x, int *y,int n,int l)
{
int i,j;
i=1;
while(i<=(n-(2*l)+1))
{
merge(x,i,(i+l-1),(i+(2*l)-1),y);
i=i+(2*l);
}
if((i+l-1)<n)
merge(x,i,(i+l-1),n,y);
else
{
for(j=i;j<=n;j++)
y[j]=x[j];
}
}

void msort(int *x,int *y,int n)


{
int l;
l=1;
while(l<n)
{
mpass(x,y,n,l);
l = (2*l);
mpass(y,x,n,l);
l = (2*l);
}
}

int main(int arg,char *argv[])


{
int rank,size;
int k,si,c,d,pi,g,s;
int a[SIZE],b[SIZE];

MPI_Init(&arg,&argv);
MPI_Comm_size(MPI_COMM_WORLD,&size);
MPI_Comm_rank(MPI_COMM_WORLD,&rank);
MPI_Status status;

double t1,t2,t3,t4,t5,t6,t7;
int WORKERS=(size-1);
int DSIZE=SIZE/WORKERS;
int EXT=SIZE%WORKERS;
int MASTER=(size-1);
int LAST=(WORKERS-1);

for(k=0;k<SIZE;k++)
a[k]=rand();

if(rank == MASTER)
{
int x,y;
t1 = MPI_Wtime();

for(k=0;k<WORKERS;k++)
{
if(k!=(WORKERS-1))
pi = DSIZE;
else
pi = DSIZE+EXT;
x=DSIZE*k;
y=x+pi-1;
MPI_Send(&x,1,MPI_INT,k,101,MPI_COMM_WORLD);
MPI_Send(&y,1,MPI_INT,k,102,MPI_COMM_WORLD);
MPI_Send((a+x),pi,MPI_INT,k,103,MPI_COMM_WORLD);
}
for(k=0;k<WORKERS;k++)
{
x=k*DSIZE;
MPI_Recv(&s,1,MPI_INT,k,201,MPI_COMM_WORLD,&status);
MPI_Recv((a+x+1),s,MPI_INT,k,202,MPI_COMM_WORLD,&status);
}

msort(a,b,SIZE);
t2 = MPI_Wtime();

printf("\nTHE SORTING RESULT:\n\n");


for(g=1;g<=SIZE;g++)
printf("%10d\t",a[g]);
printf("\n\n");

MPI_Recv(&t6,1,MPI_DOUBLE,LAST,203,MPI_COMM_WORLD,&status);
t3 = t2-t1;
printf("\nThe Computation Time : %lf Seconds",t6);
printf("\nThe Communication Time : %lf Seconds\n\n",t3-t6);
}
else
{
MPI_Recv(&c,1,MPI_INT,MASTER,101,MPI_COMM_WORLD,&status);
MPI_Recv(&d,1,MPI_INT,MASTER,102,MPI_COMM_WORLD,&status);

s=(d-c+1);

int x[s],y[s];
MPI_Recv(&x[1],s,MPI_INT,MASTER,103,MPI_COMM_WORLD,&status);

t4 = MPI_Wtime();
msort(x,y,s);
t5 = MPI_Wtime();

MPI_Send(&s,1,MPI_INT,MASTER,201,MPI_COMM_WORLD);
MPI_Send(&x[1],s,MPI_INT,MASTER,202,MPI_COMM_WORLD);

t6 = t5-t4;
MPI_Send(&t6,1,MPI_DOUBLE,MASTER,203,MPI_COMM_WORLD);
}

MPI_Finalize();
exit(0);
}
Analysis of Parallel Quick Sort
***************************

Total Numbers: 50
Number of Working Processors: 2
THE SORTING RESULT:
35005211 149798315 233665123 278722862 294702567
304089172 336465782 424238335 468703135 521595368
596516649 608413784 628175011 635723058 719885386
756898537 783368690 846930886 859484421 861021530
1025202362 1059961393 1101513929 1102520059 1125898167
1131176229 1189641421 1303455736 1315634022 1350490027
1365180540 1369133069 1540383426 1649760492 1653377373
1656478042 1681692777 1714636915 1726956429 1734575198
1801979802 1804289383 1914544919 1957747793 1967513926
1973594324 2038664370 2044897763 2089018456 2145174067
The Computation Time : 0.000007 Seconds
The Communication Time : 0.040622 Seconds

Total Numbers: 50
Number of Working Processors: 9
THE SORTING RESULT:
35005211 149798315 233665123 278722862 294702567
304089172 336465782 424238335 468703135 521595368
596516649 608413784 628175011 635723058 719885386
756898537 783368690 846930886 859484421 861021530
1025202362 1059961393 1101513929 1102520059 1125898167
1131176229 1189641421 1303455736 1315634022 1350490027
1365180540 1369133069 1540383426 1649760492 1653377373
1656478042 1681692777 1714636915 1726956429 1734575198
1801979802 1804289383 1914544919 1957747793 1967513926
1973594324 2038664370 2044897763 2089018456 2145174067
The Computation Time : 0.000004 Seconds
The Communication Time : 0.039607 Seconds
Analysis of Parallel Merge Sort:
***************************

Computation Time Communication Time


Data Processors
(in Seconds) (in Seconds)

4 0.000356 0.004978

5000 8 0.000159 0.004989

12 0.000100 0.005548
4 0.000654 0.009410

10000 8 0.000341 0.009824

12 0.000210 0.011201
4 0.001113 0.047260

15000 8 0.000510 0.053178

12 0.000350 0.211405

"merge.c" 163L, 3477C written


[mtech17@oscarserver sath]$ mpicc -o p2 merge.c
[mtech17@oscarserver sath]$ mpirun -np 2 p2

THE SORTING RESULT:

35005211 294702567 304089172 336465782 424238335


521595368 596516649 719885386 783368690 846930886
1025202362 1102520059 1189641421 1303455736 1350490027
1365180540 1540383426 1649760492 1681692777 1714636915
1726956429 1804289383 1957747793 1967513926 2044897763
The Computation Time : 0.000008 Seconds
The Communication Time : 0.000288 Seconds

[mtech17@oscarserver sath]$ mpicc -o p3 merge.c


[mtech17@oscarserver sath]$ mpirun -np 3 p3

THE SORTING RESULT:

35005211 294702567 304089172 336465782 424238335


521595368 596516649 719885386 783368690 846930886
1025202362 1102520059 1189641421 1303455736 1350490027
1365180540 1540383426 1649760492 1681692777 1714636915
1726956429 1804289383 1957747793 1967513926 2044897763

The Computation Time : 0.000005 Seconds


The Communication Time : 0.000431 Seconds

Вам также может понравиться