All pastes #544755 Raw Edit

Leibniz PI MPI C++ Application

public cpp v1 · immutable
·published 2007-06-06 16:04 UTC
rendered paste body
#include <iostream>#include <iomanip>#include <cmath>#include "mpi.h"#define REALPI 3.14159265358979323846264338327950288419716939937510using namespace std;int main( int argc, char* argv[] ) {	MPI::Init( argc, argv );	MPI::Intracomm &world = MPI::COMM_WORLD;		unsigned int rank = world.Get_rank();	unsigned int world_size = world.Get_size();			double maxN;	if( rank == 0 ) {		cout << "max n? " << flush;		cin >> maxN;	}		world.Bcast( &maxN, 1, MPI_DOUBLE, 0 );		double start_time = MPI::Wtime();		double myTotal = 0.0;	for( unsigned long int i = rank; i <= maxN; i += world_size ) {		if( i%2 == 0 ) {			myTotal += 4.0 * ((double)1.0 / (((double)i*2.0)+1.0));		} else {			myTotal -= 4.0 * ((double)1.0 / (((double)i*2.0)+1.0));		}	}		double final_total = 0.0;	world.Reduce( &myTotal, &final_total, 1, MPI::DOUBLE, MPI::SUM, 0 );		double end_time = MPI::Wtime();// 	final_total *= 4;		if( rank == 0 ) {		double error = REALPI - final_total;		if( error < 0 ) {			error *= -1;		}		double terror = error;				unsigned int precision = 0;		while( terror < 0.1 ) {			terror /= 0.1;			precision++;		}			cout << setprecision(precision);		cout << "Pi: " << final_total << endl;		cout << setprecision(20);		cout << "Error: " << error << endl;		cout << "GFLOPS: " << ((5*(maxN+1))/(end_time-start_time))/1000/1000/1000 << endl;	}		MPI::Finalize();	return 0;}