MPI实验三:梯形法求积分

实验三 梯形法求积分

1. 实验目的和要求

梯形法求积分

2.实验环境

安装了docker的PC

3.实验内容及实验数据记录

梯形法求积分
a. mpi_send, 0 for sum
b. mpi_gather, 回收到0号进程的数组,for sum
c. mpi_reduce, MPI_SUM求和

4.算法描述及实验步骤

第一:写函数double f(double x)和double Trap(double left_endpt,double right_endpt,int trap_count,double base_len)来计算pi
在这里插入图片描述

第二:进程 0输入a,b和n,且进程0把a,b,n发送给剩余进程
在这里插入图片描述

第三:各个进程接收
在这里插入图片描述

第四:计算h,每个进程需处理的长度local_n,还有每个进程的起始位置local_a和终点位置local_b
在这里插入图片描述

第五:用mpi_reducem ,mpi_gatherp ,mpi_send接收total_trap
在这里插入图片描述

第六:由进程0打印且求和
在这里插入图片描述

5.调试过程

注意大小写

6. 实验结果

在这里插入图片描述

7.代码

#include<stdio.h>
#include<string.h>
#include<mpi.h>

double f(double x)
{
    double y;
	y=4/(1+x*x); 
	return y;
}
double Trap(double left_endpt,double right_endpt,int trap_count,double base_len)
{
    double estimate,x;
    estimate=(f(left_endpt)+f(right_endpt))/2.0;
    for(int i=1;i<trap_count;i++)
    {
        x=left_endpt+i*base_len;
        estimate+=f(x);
    }
    estimate=estimate*base_len;
    return estimate;
}
int main(int argc,char **argv)
{
    int numProcs,rank;
    double a,b,n;
    MPI_Status status;
    MPI_Init(&argc,&argv);
    MPI_Comm_size(MPI_COMM_WORLD,&numProcs);
    MPI_Comm_rank(MPI_COMM_WORLD,&rank);
    if(rank==0)
    {
        printf("Please input a,b,n\n");
        scanf("%lf%lf%lf",&a,&b,&n);
        for(int i=1;i<numProcs;i++)
        {
            MPI_Send(&a,1,MPI_DOUBLE,i,0,MPI_COMM_WORLD);
            MPI_Send(&b,1,MPI_DOUBLE,i,0,MPI_COMM_WORLD);
            MPI_Send(&n,1,MPI_DOUBLE,i,0,MPI_COMM_WORLD);
        }
    }
    else
    {
        MPI_Recv(&a,1,MPI_DOUBLE,0,0,MPI_COMM_WORLD,MPI_STATUS_IGNORE);
        MPI_Recv(&b,1,MPI_DOUBLE,0,0,MPI_COMM_WORLD,MPI_STATUS_IGNORE);
        MPI_Recv(&n,1,MPI_DOUBLE,0,0,MPI_COMM_WORLD,MPI_STATUS_IGNORE);
    }
    double h,local_n;
    h=(b-a)/n;
    local_n=n/numProcs;
    double get_trap[numProcs];
    double local_a=a+rank*local_n*h;
    double local_b=local_a+local_n*h;
    double local_trap=Trap(local_a,local_b,local_n,h);
    printf("local trap is :%lf\n",local_trap);
    double total_trap;
    MPI_Reduce(&local_trap,&total_trap,1,MPI_DOUBLE,MPI_SUM,0,MPI_COMM_WORLD);
    MPI_Gather(&local_trap,1,MPI_DOUBLE,get_trap,1,MPI_DOUBLE,0,MPI_COMM_WORLD);
    MPI_Send(&local_trap,1,MPI_DOUBLE,0,99,MPI_COMM_WORLD);
    if(rank==0)
    {    
        printf("Use Reduce:Integral from %.0f to %.0f = %lf\n",a,b,total_trap);
        total_trap=0;
        for(int i=0;i<numProcs;i++)
            total_trap+=get_trap[i];
        printf("Use Gather:Integral from %.0f to %.0f = %lf\n",a,b,total_trap);
        total_trap=0;
        for(int i=0;i<numProcs;i++)
        {
            MPI_Probe(MPI_ANY_SOURCE,99,MPI_COMM_WORLD,&status);
            MPI_Recv(get_trap+status.MPI_SOURCE,1,MPI_DOUBLE,status.MPI_SOURCE,99,MPI_COMM_WORLD,&status);
        }
        for(int i=0;i<numProcs;i++)
            total_trap+=get_trap[i];
        printf("Use Send:Integral from %.0f to %.0f = %lf\n",a,b,total_trap);
    }
    MPI_Finalize();
    return 0;
       
}
  • 4
    点赞
  • 41
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
A:以下是梯形积分MPI程序实验: ```c #include <stdio.h> #include <math.h> #include "mpi.h" double func(double x) { return sin(x); } double trap(double local_a, double local_b, int local_n, double h) { double integral; double x; int i; integral = (func(local_a) + func(local_b))/2.0; x = local_a; for (i = 1; i <= local_n-1; i++) { x = x + h; integral = integral + func(x); } integral = integral*h; return integral; } int main(int argc, char** argv) { int my_rank; int p; double a = 0.0; double b = 3.14159; int n = 1024; double h; double local_a; double local_b; int local_n; double integral; double total_integral; int source; int dest = 0; int tag = 0; MPI_Status status; MPI_Init(&argc, &argv); MPI_Comm_rank(MPI_COMM_WORLD, &my_rank); MPI_Comm_size(MPI_COMM_WORLD, &p); h = (b-a)/n; local_n = n/p; local_a = a + my_rank*local_n*h; local_b = local_a + local_n*h; integral = trap(local_a, local_b, local_n, h); if (my_rank == 0) { total_integral = integral; for (source = 1; source < p; source++) { MPI_Recv(&integral, 1, MPI_DOUBLE, source, tag, MPI_COMM_WORLD, &status); total_integral = total_integral + integral; } } else { MPI_Send(&integral, 1, MPI_DOUBLE, dest, tag, MPI_COMM_WORLD); } if (my_rank == 0) { printf("With n = %d trapezoids, our estimate\n", n); printf("of the integral from %f to %f = %f\n", a, b, total_integral); } MPI_Finalize(); } ``` 该程序使用了梯形积分来计算 $sin(x)$ 的从0到$\pi$的积分。程序中首先定义了一个函数func,用于计算被积函数的值。接下来,定义了一个 trap 函数,用于计算局部积分的值。该函数接受了局部区间的起点local_a,终点local_b,局部区间内划分的梯形数量local_n和步长h,并使用梯形积分计算该区间的局部积分。最后,程序使用MPI_Init来初始化MPI环境,调用MPI_Comm_rank和MPI_Comm_size来获取当前进程的排名和进程总数,计算局部积分并发送/接收各进程的积分值,使用Master进程将所有进程的积分值汇总计算总积分值并输出结果,并通过MPI_Finalize结束MPI环境。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值