并行计算学习之用MPI实现梯形积分法

  • 梯形积分法

基本思想是,将x轴上区间划分成n个等长的子区间。估计介于函数图像以及每个子区间内梯形区域的面积。

  设子区间端点为xi和xi+1 ,长度h=xi+1 - xi, 同样的两条垂直线的长度为f(xi)和f(xi+1)

    那么面积为:h/2[ f(xi) + f(xi+1) ]

并行计算学习之用MPI实现梯形积分法

n个区间是等分的,如果两条垂直线包围区域的边界分别为a和b,那么

      h = (b-a) / n

并行计算学习之用MPI实现梯形积分法

  • 设计并行程序的四个步骤
  1. 将问题的解决方案划分成多个任务
  2. 在任务间识别出需要的通信信道(识别出联系,是否需要交流)
  3. 将任务聚合成复合任务 --->减少通信
  4. 在核上分配复合任务
  • 串行代码
/*  Input  a ,b , n*/
h = (b-a)/n
approx  = (f(a) + f(b))/2.0

for(i =1; i <= n-1; i++){
    x_i = a + i * h;
    approx = h * approx
}

approx = h*approx
  •  并行程序

并行计算学习之用MPI实现梯形积分法

     伪代码:

Get a,b,n;
h = (b-a)/n;
local_n = n/comm_sz;
local_a = a + my_rank * local_n * h;
local_b = local_a + local_n*h;
local_integral = Trap(local_a, local_b, local_n, h)  /*Trap函数作用*/

if( my_rank != 0 )
     Send local_integral to process 0;

else /*my_rank == 0*/
      total_integral = local_integral;
      for(proc = 1;proc < comm_sz; proc++){
           Receive local_integral from proc;
           total_integral += local_integral;
     }

if(my_rank == 0)
      print result;

  

int main(void){
     int my_rank , comm_sz, n = 1024, local_n;
     double a = 0.0, b=3.0, h, local_a,local_b;
     double local_int,total_int;
     int source;    

     MPI_Init(NULL,NULL);
     MPI_Comm_rank(MPI_COMM_WORLD, &my_rank);
     MPI_Comm_size(MPI_COMM_WORLD, &comm_sz);
   
     h = (b-a)/n;
     local_n = n/comm_sz;
     
     local_a = a + my_rank * local_n * h;
     local_b = local_a +local_n * h;
     local_int = Trap(local_a , local_b , local_n, h )

    if (my_rank != 0){
            MPI_Send(&local_int , 1 , MPI_DOUBLE , 0, 0 , MPI_COMM_WORLD);
    }else{
             total_int = local_int;
             for(source = 1 ; source < comm_sz ; source++){
                    MPI_Recv(&local_int, 1, MPI_DOUBLE, source, 0, MPI_COMM_WORLD, MPI_STATUS_IGNORE);
                    total_int += local_int;
             }
    }

   if (my_rank == 0 ){
           printf("With n = %d trapezoids, our estimate\n",n);
           printf("of the integral from %f to %f = %.15e\n",a,b,total_int);
   }

   MPI_Finalize();
   return 0;
}                    

  

上一篇:python – MPI信号处理


下一篇:Python调用使用MPI的(fortran)库