是《MPI与OpenMP并行程序设计-C语言版》的作业;91页里面习题4.11:
通过计算在曲线下图形的面积来计算pi,一个简单的方法是使用矩形规则,将区间【0,1】分成k个等长子区间。用这些高度可以构造k个矩形。这些矩形的面积逼近曲线下图形的面积。
如下是课文中的C程序
![](https://i-blog.csdnimg.cn/blog_migrate/6810355c2f78c12e91b7997a8e8c583a.gif)
![](https://i-blog.csdnimg.cn/blog_migrate/6810355c2f78c12e91b7997a8e8c583a.gif)
![](https://i-blog.csdnimg.cn/blog_migrate/6810355c2f78c12e91b7997a8e8c583a.gif)
![](https://i-blog.csdnimg.cn/blog_migrate/a41954a27d6ad96fa2c2cf816e677448.gif)
![](https://i-blog.csdnimg.cn/blog_migrate/1327ab569c1ae82736693a50b8e33378.gif)
![](https://i-blog.csdnimg.cn/blog_migrate/6a9c071a08f1dae2d3e1c512000eef41.gif)
![](https://i-blog.csdnimg.cn/blog_migrate/6a9c071a08f1dae2d3e1c512000eef41.gif)
![](https://i-blog.csdnimg.cn/blog_migrate/6a9c071a08f1dae2d3e1c512000eef41.gif)
![](https://i-blog.csdnimg.cn/blog_migrate/6a9c071a08f1dae2d3e1c512000eef41.gif)
![](https://i-blog.csdnimg.cn/blog_migrate/6a9c071a08f1dae2d3e1c512000eef41.gif)
![](https://i-blog.csdnimg.cn/blog_migrate/6a9c071a08f1dae2d3e1c512000eef41.gif)
![](https://i-blog.csdnimg.cn/blog_migrate/7ff8d92cded7e0ce15e7ca1acc870052.gif)
![](https://i-blog.csdnimg.cn/blog_migrate/6a9c071a08f1dae2d3e1c512000eef41.gif)
![](https://i-blog.csdnimg.cn/blog_migrate/6a9c071a08f1dae2d3e1c512000eef41.gif)
![](https://i-blog.csdnimg.cn/blog_migrate/717446ca04a6125dc5b6b54e0fa14ab4.gif)
![](https://i-blog.csdnimg.cn/blog_migrate/6a9c071a08f1dae2d3e1c512000eef41.gif)
![](https://i-blog.csdnimg.cn/blog_migrate/6a9c071a08f1dae2d3e1c512000eef41.gif)
![](https://i-blog.csdnimg.cn/blog_migrate/6a9c071a08f1dae2d3e1c512000eef41.gif)
![](https://i-blog.csdnimg.cn/blog_migrate/0196c3df5ea9e936f21e9932cca91014.gif)
现要求编写一个使用矩形规则在100000个区间的并行程序计算pi;
我的程序如下,
#include
<
mpi.h
>
#include < stdio.h >
#include < math.h >
#define intervals 1000000
double area,aa;
int main( int argc, char * argv[])
{ int id; /* Process rank */
int p; /* Number of Processes */
int i;
void pi( int , int );
MPI_Init( & argc, & argv);
MPI_Comm_rank(MPI_COMM_WORLD, & id);
MPI_Comm_size(MPI_COMM_WORLD, & p);
for (i = id;i < intervals;i += p)
{pi(id,i);
aa += area;}
printf( " Area is %f " ,aa);
MPI_Finalize();
return 0 ;
}
void pi( int id, int z){
double ysum;
double xi;
ysum = 0.0 ;
xi = ( 1.0 / intervals) * (z + 0.5 );
ysum = 4.0 / ( 1.0 + xi * xi);
area = ysum * ( 1.0 / intervals);
}
#include < stdio.h >
#include < math.h >
#define intervals 1000000
double area,aa;
int main( int argc, char * argv[])
{ int id; /* Process rank */
int p; /* Number of Processes */
int i;
void pi( int , int );
MPI_Init( & argc, & argv);
MPI_Comm_rank(MPI_COMM_WORLD, & id);
MPI_Comm_size(MPI_COMM_WORLD, & p);
for (i = id;i < intervals;i += p)
{pi(id,i);
aa += area;}
printf( " Area is %f " ,aa);
MPI_Finalize();
return 0 ;
}
void pi( int id, int z){
double ysum;
double xi;
ysum = 0.0 ;
xi = ( 1.0 / intervals) * (z + 0.5 );
ysum = 4.0 / ( 1.0 + xi * xi);
area = ysum * ( 1.0 / intervals);
}
编译之后执行不了,菜鸟的sb程序不知道是哪里出错了呢、