/* File: pi-c.c * * Purpose: Estimates pi using the Leibniz formula (in C). * * NOTE: We use mpicc to compile here because we are using the MPI timing * function (MPI_Wtime). * * Compile: mpicc -g -Wall -o pi-c pi-c.c * Run: ./pi-c */ #include #include #include /* We define pi here so we can check and see how accurate our computation is. */ #define PI 3.141592653589793238462643 int main(int argc, char **argv) { int intervals; printf("Number of intervals: "); fflush(stdout); scanf("%d", &intervals); double time1 = MPI_Wtime(); int i; double sum, total = 0; for (i = 0; i < intervals; ++i) { total += pow(-1, i) / (2 * i + 1); } double time2 = MPI_Wtime(); total = total * 4; printf("Result: %.10lf\n", total); printf("Accuracy: %.10lf\n", PI - total); printf("Time: %.10lf\n", time2 - time1); }