0
我計算PI(3.14)的值通過從限制0曲線4/1 +(X * X)下找到曲線下面積來計算面積到1.以下是C中的MPI程序。什麼是錯誤在以下MPI程序由矩形區域求和方法
然而,當有一個過程,它使正確的值。如果我給一個以上的過程則僅具有秩0的過程給出了一些值,而其他進程給予0.0作爲其本地計算值
什麼錯誤有下面的代碼?一個進程和多個進程
#include<mpi.h>
#include<math.h>
#include<stdio.h>
#define MAX_NAME 80
int main(int argc,char *argv[])
{
MPI_INIT(&argc,&argv);
int rank,nprocs,len;
double i=0.0;
double n=1000000000.0;
double PI25DT =3.141592653589793238462643;
double mypi,pi,step,sum,x;
char name[MAX_NAME];
double start_time,end_time,computation_time;
MPI_Comm_size(MPI_COMM_WORLD,&nprocs);
MPI_Comm_rank(MPI_COMM_WORLD,&rank);
MPI_Get_processor_name(name,&len);
start_time=MPI_Wtime();
sum=0.0;
step=1.0/(double)n;
x=0.0;
x=(double)rank*(n/nprocs);
x=x+step;
double temp=x;
for(i=temp;i<(temp+(n/nprocs));i=i+1.0)
{
sum+=step*(4.0/(1.0+(double)(x*x)));
x=x+step;
}
mypi=sum;
printf("\nProcessor: %d Name: %s Sum: %.16f \n",rank,name,mypi);
MPI_Barrier(MPI_COMM_WORLD);
MPI_Reduce(&mypi,&pi,1,MPI_DOUBLE,MPI_SUM,0,MPI_COMM_WORLD);
if(rank==0)
{
printf("\nValue of Pi is %.16f approximately .Error is %.16f \n",pi,fabs(pi-PI25DT));
end_time=MPI_Wtime();
computation_time=end_time-start_time;
printf("\nComputation time is: %f seconds.\n",computation_time);
}
MPI_Finalize();
}
輸出上面的代碼是:
「我沒有得到正確的PI值作爲答案。」 - >什麼答案呢碼產生? – chux
'N/nprocs'是整數除法。這是正確的'x = rank *(n/nprocs);'? – chux
此代碼是一個進程工作的罰款。如果我給nprocs> 1,則其給出錯誤的值 – ComputerMaster