<br>>version :MPICH2 1.3.1<br>>uname -mo<br> ia64 GNU/Linux <br>>Yah dear 100 is the output. which is the size of array I have I entered.<br>>Yah Friend it is working correctly with out profiling on single and multiple processes. and also after profiling when it run on a single process. <br>
<br>#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 compare(const void *e1, const void *e2 ) <br> {<br>
return (*((int *)e1) - *((int *)e2));<br> }<br>DisplayError(char *str)<br>{<br> printf("Error: %s \n",str);<br>} <br><br>int Partition(int *Array,int start,int end)<br>{<br> int pivo;<br> int i, j;<br>
int tmp;<br><br> pivo=Array[end]; <br> i=start-1; <br><br> for(j=start;j<end;j++)<br> if(Array[j]<=pivo)<br> {<br> i++; <br> tmp=Array[i];<br>
Array[i]=Array[j];<br> Array[j]=tmp;<br> }<br> <br> tmp=Array[i+1];<br> Array[i+1]=Array[end];<br> Array[end]=tmp; <br><br> return i+1;<br>}<br><br>QuickSort(int *Array,int start,int end)<br>
{<br> int r;<br> int i;<br><br> if(start<end)<br> {<br> r=Partition(Array,start,end);<br> QuickSort(Array,start,r-1);<br> QuickSort(Array,r+1,end);<br> }<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 i, j;<br> int r;<br> int LocalLength;<br> int *tmp;<br> //int newsize;<br> MPI_Status status;<br> LocalLength=-1;<br> double t3,t4,t5;<br><br> if(m==0)<br> {<br> if(MyRank==id)<br>
<br> t3=MPI_Wtime();<br> qsort(Array, end-start, sizeof(int), compare); <br> t4=MPI_Wtime();<br> t5=t4-t3;<br> printf("Process with ID %d and time %6.3f \n",MyRank,t5); <br>
<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>
if(LocalLength!=0)<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) DisplayError("Malloc memory error!");<br> MPI_Recv(tmp,LocalLength,MPI_INT,id,id,MPI_COMM_WORLD,&status);<br>
}<br> }<br><br> <br> <br> if(id<=MyRank && MyRank<id+PowerOf2(m-1))<br> {<br> PQuickSort(Array,start,r-1,m-1,id,MyRank);<br> }<br><br> if(MyRank>=id+PowerOf2(m-1))<br>
{<br> PQuickSort(tmp,0,LocalLength-1,m-1,id+PowerOf2(m-1),MyRank);<br> }<br><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, j;<br> int m, r;<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> //printf("%10d",Array[i]);<br>
// printf("\t");<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>> the above code is without profiling and work well<br>Thanks Friend :)<br>