Hello guys
I work on a small parallel program, which calculates pi number from area of a quarter circle. I'm new on ubuntu & C programming.
This is the code.
The question is:Code:#include <stdio.h> #include <mpi.h> #include <math.h> #include <time.h> #define Ndx 600000000 int main (int argc, char* argv[]) { int labindex, nw; MPI_Init (&argc, &argv); /* starts MPI */ MPI_Comm_rank (MPI_COMM_WORLD, &labindex); /* get current process id */ MPI_Comm_size (MPI_COMM_WORLD, &nw); /* get number of processes */ int i; double dx, x, y, sum, pi; double range_start; double te, te1; dx=1./Ndx; range_start=labindex*(1./nw); sum=0; for(i=0;i<Ndx/nw;i++) { x=range_start+i*dx+dx/2; y=sqrt(1-pow(x,2)); sum=sum+dx*y; } MPI_Reduce(&sum,&pi,1,MPI_DOUBLE,MPI_SUM,0,MPI_COMM_WORLD); pi=pi*4; //quarter to full circle if (labindex==0) { te1=clock(); te=((float)te1)/CLOCKS_PER_SEC; printf("pi=%.20f\n",pi); printf("difference is=%.20f\n",pi-acos(-1)); printf("total time elapsed %f s",te); } MPI_Finalize(); return 0; }
My laptop has i7 processor with hyper-threading. And as a guess I should be able to use at most 8 workers/processors when running the program.You may see the outputs of the program below. Strange thing is I'm able to run the code with 80 workers. And it seems quite fast. How can this be?
Thank you
Code:mpicc pi.c -Wall -o pi -lmpi=3.14159265358961548031Code:mpirun -np 1 ./pi
difference is=-0.00000000000017763568
total time elapsed 19.639999
pi=3.14159265358966033332Code:mpirun -np 2 ./pi
difference is=-0.00000000000013278267
total time elapsed 9.570000
pi=3.14159265358984729488Code:mpirun -np 4 ./pi
difference is=0.00000000000005417888
total time elapsed 4.800000
pi=3.14159265358979489235Code:mpirun -np 8 ./pi
difference is=0.00000000000000177636
total time elapsed 2.610000
pi=3.14159265358977268789Code:mpirun -np 80 ./pi
difference is=-0.00000000000002042810
total time elapsed 0.430000



LinkBack URL
About LinkBacks



