#include<stdio.h>
#include<mpi.h>
#include<iostream>
#include<math.h>
using namespace std;
//计算x的积分
double f(double x) {
return x * x;
}
//梯形积分
double Trap(double a, double b, int n, double h){
double estimated = (f(a) + f(b)) / 2.0;
for (int i = 1; i < n; i++) {
//x = a + i * h;
estimated += f(a + i * h);
}
estimated = estimated * h;
return estimated;
}
void Get_input(int my_rank, int comm_sz, double *a_p, double *b_p, int * n_p) {
if (my_rank == 0) {
printf("请输入 a,b,n:\n");
scanf("%lf%lf%d",a,b,n);
}
//将主进程输入的参数广播到其余进程
MPI_Bcast(a_p, 1, MPI_DOUBLE, my_rank, MPI_COMM_WORLD);
MPI_Bcast(b_p, 1, MPI_DOUBLE, my_rank, MPI_COMM_WORLD);
MPI_Bcast(n_p, 1, MPI_INT, my_rank, MPI_COMM_WORLD);
}
int main(int argc, char* argv[]) {
int my_rank, comm_sz; //进程编号,所有进程数目
int n, local_n;
double a, b;//积分上下限
double h, local_a, local_b;
double local_int, total_int;
int source;
//并行部分
MPI_Init(&