#include <mpi.h><br>#include <stdio.h><br>#include <stdlib.h><br>#define TRUE 1<br>#include "math.h"<br>#include <time.h><br>int IncOrder(const void *e1, const void *e2 ) <br> {<br> return (*((int *)e1) - *((int *)e2));<br>
}<br><br>DisplayError(char *str)<br>{<br> printf("Error: %s \n",str);<br>} <br><br>int Partition(int *arr, int left, int right) {<br>int i = left, j = right;<br>int tmp;<br>int pivot = arr[(left + right) / 2];<br>
/* partition */<br>while (i <= j) {<br>while (arr[i] < pivot)<br>i++;<br>while (arr[j] > pivot)<br>j--;<br>if (i <= j) {<br>tmp = arr[i];<br>arr[i] = arr[j];<br>arr[j] = tmp;<br>i++;<br>j--;<br>}<br>}<br>return j;<br>
}<br><br>int PowerOf2(int num)<br>{<br> int i;<br><br> i=1;<br><br> while(num>0)<br> {<br> num--;<br> i=i*2;<br> }<br> <br> return i;<br>}<br><br><br>PQuickSort(int *Array,int start,int end,int m,int id,int MyRank)<br>
{<br> int j;<br> int r;<br> int LocalLength;<br> int *tmp;<br> //int newsize;<br> MPI_Status status;<br> LocalLength=-1;<br><br> if(m==0)<br>{<br> if(MyRank==id)<br><br> qsort(Array, end-start, sizeof *Array, IncOrder); <br>
return 0;<br>} <br><br>if(MyRank==id)<br>{<br> r=Partition(Array,start,end); <br> LocalLength=end-r; <br>MPI_Send(&LocalLength,1,MPI_INT,id+PowerOf2(m-1),MyRank,MPI_COMM_WORLD);<br><br>if(LocalLength!=0)<br>
<br>MPI_Send(Array+r+1,LocalLength,MPI_INT,id+PowerOf2(m-1),MyRank,MPI_COMM_WORLD);<br> }<br><br> if(MyRank==id+PowerOf2(m-1))<br> {<br> MPI_Recv(&LocalLength,1,MPI_INT,id,id,MPI_COMM_WORLD,&status);<br>
<br> if(LocalLength!=0)<br> {<br> tmp=(int *)malloc(LocalLength*sizeof(int));<br> if(tmp==0)<br> DisplayError("Malloc memory error!");<br> MPI_Recv(tmp,LocalLength,MPI_INT,id,id,MPI_COMM_WORLD,&status);<br>
}<br> }<br><br> j=r-1-start; <br> MPI_Bcast(&j,1,MPI_INT,id,MPI_COMM_WORLD);<br> if(j>0)<br> PQuickSort(Array,start,r-1,m-1,id,MyRank);<br><br> j=LocalLength; <br> MPI_Bcast(&j,1,MPI_INT,id,MPI_COMM_WORLD);<br>
if(j>0)<br> PQuickSort(tmp,0,LocalLength-1,m-1,id+PowerOf2(m-1),MyRank);<br><br> if((MyRank==id+PowerOf2(m-1)) && (LocalLength!=0))<br> MPI_Send(tmp,LocalLength,MPI_INT,id,id+PowerOf2(m-1),MPI_COMM_WORLD);<br>
<br> if((MyRank==id) && (LocalLength!=0))<br> MPI_Recv(Array+r+1,LocalLength,MPI_INT,id+PowerOf2(m-1),id+PowerOf2 (m-1),MPI_COMM_WORLD,&status);<br><br>}<br><br>int LogBase2(int num)<br>{<br> int i, j;<br>
<br> i=1; <br> j=2;<br><br> while(j<num)<br> {<br> j=j*2;<br> i++;<br> }<br><br> if(j>num)<br> i--;<br><br> return i;<br>}<br><br><br>main(int argc,char *argv[])<br>{<br>
int ArraySize;<br> int *Array;<br> int MyRank, npes;<br> int i;<br> int m ;<br> double t1,t2,t3;<br><br><br> //MPI_Status status;<br> MPI_Init(&argc,&argv);<br> <br>
MPI_Comm_rank(MPI_COMM_WORLD,&MyRank); <br><br> MPI_Comm_size(MPI_COMM_WORLD,&npes); <br> <br> if(MyRank==0)<br> {<br> ArraySize=1000000;<br> printf("%d\n",ArraySize);<br>
Array=(int *)malloc(ArraySize*sizeof(int));<br><br> if(Array==0) <br> printf("Malloc memory error!");<br><br> srand(396);<br> for(i=0;i<ArraySize;i++)<br> {<br>
Array[i]=(int)rand()%1000;<br>
}<br> printf("\n");<br> }<br> <br> m=LogBase2(npes);<br><br> MPI_Bcast(&ArraySize,1,MPI_INT,0,MPI_COMM_WORLD);<br> t1=MPI_Wtime();<br> PQuickSort(Array,0,ArraySize-1,m,0,MyRank);<br>
t2=MPI_Wtime();<br> t3=t2-t1;<br> <br> if(MyRank==0)<br> {<br> for(i=0;i<ArraySize;i++)<br> {<br> printf("%10d",Array[i]);<br> printf("\t");<br>
}<br> printf("\n");<br> printf("MPI_time :%6.3f\n",t3);<br> <br> }<br><br> MPI_Finalize(); <br> return 0;<br>}<br><br>when I increase the number of process this code take more time. :(<br>
<br>