MPI和OpenMP混合编程,π
#include "stdio.h"
#include "mpi.h"
#include "omp.h"
#include "math.h"
#define NUM_THREADS 8
long int n=10000000;
int main(int argc,char*argv[])
{
int my_rank,numprocs;
long int i,my_n,my_first_i,my_last_i;
double my_pi=0.0,pi,h,x;
MPI_Init(&argc,&argv);
MPI_Comm_size(MPI_COMM_WORLD,&numprocs);
MPI_Comm_rank(MPI_COMM_WORLD,&my_rank);
h=1.0/n;
my_n=n/numprocs;
my_first_i=my_rank*my_n;
my_last_i=my_first_i+my_n;
omp_set_num_threads(NUM_THREADS);
#pragma omp parallel for reduction(+:my_pi)private(x,i)
for(i=my_first_i;i<my_last_i;i++)
{
x=(i+0.5)*h;
my_pi=my_pi+4.0/(1.0+x*x);
}
MPI_Reduce(&my_pi,&pi,1,MPI_DOUBLE,MPI_SUM,0,MPI_COMM_WORLD);
if(my_rank==0)
{
printf("Approximation of pi:%15.13f\n",pi*h);
}
MPI_Finalize();
return 0;
}
// mpicc demo2.c -o demo3 -fopenmp
// mpirun -np 4 ./demo3
编译运行结果如下: