Академический Документы
Профессиональный Документы
Культура Документы
#include<stdio.h>
#include<mpi.h>
#include<stdlib.h>
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();
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:
***************************
4 0.000356 0.004978
12 0.000100 0.005548
4 0.000654 0.009410
12 0.000210 0.011201
4 0.001113 0.047260
12 0.000350 0.211405