Pi近似のためのこのC MPIアプリケーションは、ランダムに生成されたドットの数である問題サイズごとに常に同じ結果を出力します(npts
)。Pi近似のためにこのC MPIアプリケーションを実行すると、結果は常に同じですか?
#include <stdio.h>
#include <stdlib.h>
#include <math.h>
#include "mpi.h"
int main(int argc, char *argv[]) {
int myid,nprocs;
double PI25DT = 3.141592653589793238462643;
long long npts = 1e10;
long i,mynpts;
long double f,sum,mysum;
long double xmin,xmax,x;
MPI_Init(&argc,&argv);
MPI_Comm_size(MPI_COMM_WORLD,&nprocs);
MPI_Comm_rank(MPI_COMM_WORLD,&myid);
if (myid == 0) {
mynpts = npts - (nprocs-1)*(npts/nprocs);
} else {
mynpts = npts/nprocs;
}
mysum = 0.0;
xmin = 0.0;
xmax = 1.0;
srand(myid);
for (i=0; i<mynpts; i++) {
x = (long double) rand()/RAND_MAX*(xmax-xmin) + xmin;
mysum += 4.0/(1.0 + x*x);
}
MPI_Reduce(&mysum,&sum,1,MPI_LONG_DOUBLE,MPI_SUM,0,MPI_COMM_WORLD);
if (myid == 0) {
f = sum/npts;
printf("PI calculated with %lld points = %.16f \n",npts,f);
printf("Error is: %.16f \n",fabs(f-PI25DT));
}
MPI_Finalize();
}
これは出力です。私は結果がアプリケーションの実行ごとに少し異なると思う。私は128のノードを持つクラスタ上でそれを実行します。あなたはここにPRNGを播種している
$ mpicc pi.c -o /mnt/cluster_128/pi
$ mpirun -np 128 --hostfile hosts_4cores_128.mpi /mnt/cluster_128/pi
PI calculated with 10000000000 points = 3.1415901444578158
Error is: 0.0000025091319773
$ mpirun -np 128 --hostfile hosts_4cores_128.mpi /mnt/cluster_128/pi
PI calculated with 10000000000 points = 3.1415901444578158
Error is: 0.0000025091319773
$ mpirun -np 128 --hostfile hosts_4cores_128.mpi /mnt/cluster_128/pi
PI calculated with 10000000000 points = 3.1415901444578158
Error is: 0.0000025091319773
$ mpirun -np 128 --hostfile hosts_4cores_128.mpi /mnt/cluster_128/pi
PI calculated with 10000000000 points = 3.1415901444578158
Error is: 0.0000025091319773
これは 'MPI_Comm_rank'の出力です。 –
@WeatherVaneは播種に適していますか?この答えを削除します... –
いいえ、コードは 'if(myid == 0)' –