@ljm
2016-11-30T13:30:14.000000Z
字数 677
阅读 1433
experiment
MPI+OpenMP混合编程
下面给出demo程序
#include "mpi.h"
#include "omp.h"
#include <math.h>
#define N 1000000000
int main( int argc, char* argv[] ){
int rank, nproc;
int i,low,up;
double local = 0.0, pi, w, temp;
MPI_Status status;
MPI_Init( &argc, &argv );
MPI_Comm_size( MPI_COMM_WORLD, &nproc );
MPI_Comm_rank( MPI_COMM_WORLD, &rank );
w = 1.0/N; low = rank*(N / nproc); up = low + N/nproc - 1;
#pragma omp parallel for reduction(+:local) private(temp,i)
for (i=low;i<up; i++){
temp = (i+0.5)*w;
local = local + 4.0/(1.0+temp*temp);
}
MPI_Reduce(&local, &pi, 1, MPI_DOUBLE, MPI_SUM, 0,MPI_COMM_WORLD);
if(rank==0) printf("pi = %.20f\n",pi*w);
MPI_Finalize();
}
mpicc -o pi -fopenmp test.c
export OMP_NUM_THREADS=8
mpirun -np 4 -hostfile ./mynodes ./pi