#include<stdio.h>#include<math.h>#include"mpi.h"doublef(double a){return(4.0/(1.0+ a*a));}intmain(int argc,char* argv[]){int i,n=1000000000,myid,numprocs;double pi=0.0,h,a,respective=0.0,startTime,endTime;MPI_Init(&argc,&argv);MPI_Comm_rank(MPI_COMM_WORLD,&myid);MPI_Comm_size(MPI_COMM_WORLD,&numprocs);
MPI_Status status;
h =1.0/(double)n;
startTime =MPI_Wtime();if(myid!=0){for(i=myid+1;i<=n;i+=(numprocs-1)){
a=h*((double)i-0.5);
respective+=f(a);}
respective = h * respective;MPI_Send(&respective,1,MPI_DOUBLE,0,99,MPI_COMM_WORLD);}else{/*myid==0*/for(i=1;i<numprocs;i++){MPI_Recv(&respective,1,MPI_DOUBLE,i,99,MPI_COMM_WORLD,&status);
pi+=respective;}
endTime =MPI_Wtime();printf("\nPI is %f\nTime is : %f\n",pi,endTime - startTime);}MPI_Finalize();return0;}
reduce
#include<stdio.h>#include<math.h>#include"mpi.h"doublefunc(double a){return(4.0/(1.0+ a*a));}intmain(int argc,char* argv[]){int n=1000000000,myid,numprocs;double pi,h,a,respective,startTime,endTime;MPI_Init(&argc,&argv);MPI_Comm_rank(MPI_COMM_WORLD,&myid);MPI_Comm_size(MPI_COMM_WORLD,&numprocs);if(myid==0){
startTime=MPI_Wtime();}
h =1.0/(double)n;
respective =0.0;for(int i=myid+1;i<=n;i+=numprocs)//线程数目{
a = h *((double)i -0.5);
respective +=func(a);}
respective = h * respective;MPI_Reduce(&respective,&pi,1,MPI_DOUBLE,MPI_SUM,0,MPI_COMM_WORLD);if(myid==0){
endTime =MPI_Wtime();printf("\nPI is %f\nTime is : %f\n",pi,endTime - startTime);}MPI_Finalize();return0;}
gatherysk
staticlong num_steps =100000000;double step;voidmain(int argc,char*argv[]){double pi=0.0;
step =1.0/(double)num_steps;double start =MPI_Wtime();MPI_Init(NULL,NULL);int id,size,i,j,k;
MPI_Comm_rank (MPI_COMM_WORLD,&id);MPI_Comm_size(MPI_COMM_WORLD,&size);double x,sum,pi_temp,pi_buffer[size];for(i = id,sum =0.0;i <= num_steps;i = i+size){
x =(i -0.5)* step;
sum +=4.0/(1.0+ x * x);}
pi_temp = sum * step;MPI_Gather(&pi_temp,1, MPI_DOUBLE, pi_buffer,1, MPI_DOUBLE,0, MPI_COM M_WORLD);if(id ==0){for(j=0;j<size;j++){
pi += pi_buffer[k];}printf("pi value is: %f\n", pi);double end =MPI_Wtime();printf("runtime is: %f\n", end - start);printf("process number is: %d\n", size);}MPI_Finalize();}
broadcastysk
#include<mpi.h>#include<stdio.h>staticlong num_steps =100000000;double step;voidmain(int argc,char*argv[]){
step =1.0/(double)num_steps;double start =MPI_Wtime();MPI_Init(NULL,NULL);int id,size,i,j,k;
MPI_Comm_rank (MPI_COMM_WORLD,&id);MPI_Comm_size(MPI_COMM_WORLD,&size);double x,sum,pi_temp[size],pi=0.0;for(i = id,sum =0.0;i <= num_steps;i = i+size){
x =(i -0.5)* step;
sum +=4.0/(1.0+ x * x);}
pi_temp[id]= sum * step;for(j=1;j<size;j++){MPI_Bcast(&pi_temp[j],1, MPI_DOUBLE, j, MPI_COMM_WORLD);}if(id ==0){for(k=0;k<size;k++){
pi += pi_temp[k];}printf("pi value is: %f\n", pi);double end =MPI_Wtime();printf("runtime is: %f\n", end - start);printf("process number is: %d\n", size);}MPI_Finalize();}
reduceysk
#include<mpi.h>#include<stdio.h>staticlong num_steps =100000000;double step;voidmain(int argc,char*argv[]){double pi=0.0;
step =1.0/(double)num_steps;double start =MPI_Wtime();MPI_Init(NULL,NULL);int id,size,i,j,k;
MPI_Comm_rank (MPI_COMM_WORLD,&id);MPI_Comm_size(MPI_COMM_WORLD,&size);double x,sum,pi_temp;for(i = id,sum =0.0;i <= num_steps;i = i+size){
x =(i -0.5)* step;
sum +=4.0/(1.0+ x * x);}
pi_temp = sum * step;MPI_Reduce(&pi_temp,&pi,1, MPI_DOUBLE, MPI_SUM,0, MPI_COMM_WORLD);if(id ==0){printf("pi value is: %f\n", pi);double end =MPI_Wtime();printf("runtime is: %f\n", end - start);printf("process number is: %d\n", size);}MPI_Finalize();}
point2pointysk
#include<mpi.h>#include<stdio.h>staticlong num_steps =100000000;double step;voidmain(int argc,char*argv[]){double pi;
step =1.0/(double)num_steps;double start =MPI_Wtime();MPI_Init(NULL,NULL);double x,sum,pi_temp;int id,size,i,j;
MPI_Comm_rank (MPI_COMM_WORLD,&id);MPI_Comm_size(MPI_COMM_WORLD,&size);for(i = id,sum =0.0;i <= num_steps;i = i+size){
x =(i -0.5)* step;
sum +=4.0/(1.0+ x * x);}
pi_temp = sum * step;if(id !=0){MPI_Send(&pi_temp,1, MPI_DOUBLE,0, id, MPI_COMM_WORLD);}else{
pi = pi_temp;for(j=1;j<size;j++){MPI_Recv(&pi_temp,1, MPI_DOUBLE, j, j, MPI_COMM_WORLD, MPI_STATUS_IGNORE);
pi += pi_temp;}printf("pi value is: %f\n", pi);double end =MPI_Wtime();printf("runtime is: %f\n", end - start);printf("process number is: %d\n", size);}MPI_Finalize();}