#include #include "mpi.h" #include const long long int n = 1000000000; double f( double x ) { return (4.0 / (1.0 + x * x)); } const double start = 0.0; const double end = 1.0; const double PI25DT = 3.141592653589793238462643; int main(int argc, char *argv[]) { int id, np; MPI_Init(&argc, &argv); MPI_Comm_rank(MPI_COMM_WORLD, &id); MPI_Comm_size(MPI_COMM_WORLD, &np); double a = ( end - start ) / n; double P = 0; for ( int i = id; i < n / 2; i += np ) { double b = 4 * f( start + (2*(i+1)-1) * a ) + 2 * f( start + (2*(i+1)) * a ); P += b; } if(id == 0) { double zbir=P; for( int i = 1; i < np; i++) { MPI_Recv(&P, 1, MPI_DOUBLE, i, 20, MPI_COMM_WORLD, NULL); zbir+=P; } double myPi = ( zbir + f( start ) - f( end - a ) ) / 3.0 / n; printf("%.16lf\n", myPi ); printf("Error is %.16f\n", fabs(myPi - PI25DT)); } else MPI_Send(&P, 1, MPI_DOUBLE, 0, 20, MPI_COMM_WORLD); MPI_Finalize(); return 0; }