[关闭]
@ljm 2016-11-30T13:30:14.000000Z 字数 677 阅读 1433

MPI+OpenMP混合编程

experiment


MPI+OpenMP混合编程

demo

下面给出demo程序

  1. #include "mpi.h"
  2. #include "omp.h"
  3. #include <math.h>
  4. #define N 1000000000
  5. int main( int argc, char* argv[] ){
  6. int rank, nproc;
  7. int i,low,up;
  8. double local = 0.0, pi, w, temp;
  9. MPI_Status status;
  10. MPI_Init( &argc, &argv );
  11. MPI_Comm_size( MPI_COMM_WORLD, &nproc );
  12. MPI_Comm_rank( MPI_COMM_WORLD, &rank );
  13. w = 1.0/N; low = rank*(N / nproc); up = low + N/nproc - 1;
  14. #pragma omp parallel for reduction(+:local) private(temp,i)
  15. for (i=low;i<up; i++){
  16. temp = (i+0.5)*w;
  17. local = local + 4.0/(1.0+temp*temp);
  18. }
  19. MPI_Reduce(&local, &pi, 1, MPI_DOUBLE, MPI_SUM, 0,MPI_COMM_WORLD);
  20. if(rank==0) printf("pi = %.20f\n",pi*w);
  21. MPI_Finalize();
  22. }

编译

mpicc -o pi -fopenmp test.c

设置环境变量

export OMP_NUM_THREADS=8

运行

mpirun -np 4 -hostfile ./mynodes ./pi

添加新批注
在作者公开此批注前,只有你和作者可见。
回复批注