Leibnitz Pi

Python Numpy

from __future__ import print_function
from benchpress.benchmarks import util
import numpy as np

bench = util.Benchmark("Leibnitz Pi", "<size>*<niters>")


def leibnitz_pi(N):
    n = np.arange(0, N)
    return np.sum(1.0 / (4 * n + 1) - 1.0 / (4 * n + 3))


def main():
    (size, niter) = bench.args.size

    bench.start()
    pi = 0.0
    for _ in range(niter):
        pi += 4.0 * leibnitz_pi(size)  # Execute benchmark
        bench.flush()
    pi /= niter
    bench.stop()
    bench.pprint()
    if bench.args.verbose:
        print(pi)


if __name__ == "__main__":
    main()

C99 Seq

#include <stdio.h>
#include <bp_util.h>

double leibnitz_pi(int nelements)
{
    double sum = 0.0;
    for(int n=0; n<nelements; ++n) {
        sum += 1.0/(4*n+1) - 1.0/(4*n+3);
    }
    return sum;
}

int main(int argc, char* argv[])
{
    bp_util_type bp = bp_util_create(argc, argv, 1);// Grab arguments
    if (bp.args.has_error) {
        return 1;
    }
    const int nelements = bp.args.sizes[0];

    bp.timer_start();                               // Start timer
    double pi = 4.0*leibnitz_pi(nelements);         // Run benchmark
    bp.timer_stop();                                // Stop timer
    
    bp.print("leibnitz_pi(c99_seq)");               // Print results..
    if (bp.args.verbose) {							// ..and value.
        printf("PI-approximation = %.11f\n", pi);
    }

    return 0;
}

Cpp11 Omp

#include <iostream>
#include <iomanip>
#include <bp_util.h>
#include <omp.h>

using namespace std;

double leibnitz_pi(int nelements)
{
    double sum = 0.0;
    #pragma omp parallel for reduction(+:sum)
    for(int n=0; n<nelements; ++n) {
        sum += 1.0/(4*n+1) - 1.0/(4*n+3);
    }
    return sum;
}

int main(int argc, char* argv[])
{
    bp_util_type bp = bp_util_create(argc, argv, 1);// Grab arguments
    if (bp.args.has_error) {
        return 1;
    }
    const int nelements = bp.args.sizes[0];

    bp.timer_start();                               // Start timer
    double pi = 4.0*leibnitz_pi(nelements);         // Run benchmark
    bp.timer_stop();                                // Stop timer

    bp.print("leibnitz_pi(cpp11_omp)");				// Print results..
    if (bp.args.verbose) {                          // ..and value.
        cout << fixed << setprecision(11)
			 << "PI-approximation = " << pi << endl;
    }

    return 0;
}

Cpp11 Seq

#include <iostream>
#include <iomanip>
#include <bp_util.h>

using namespace std;

double leibnitz_pi(int nelements)
{
    double sum = 0.0;
    for(int n=0; n<nelements; ++n) {
        sum += 1.0/(4*n+1) - 1.0/(4*n+3);
    }
    return sum;
}

int main(int argc, char* argv[])
{
    bp_util_type bp = bp_util_create(argc, argv, 1);// Grab arguments
    if (bp.args.has_error) {
        return 1;
    }
    const int nelements = bp.args.sizes[0];

    bp.timer_start();                               // Start timer
    double pi = 4.0*leibnitz_pi(nelements);         // Run benchmark
    bp.timer_stop();                                // Stop timer

    bp.print("leibnitz_pi(cpp11_seq)");				// Print results..
    if (bp.args.verbose) {                          // ..and value.
        cout << fixed << setprecision(11)
			 << "PI-approximation = " << pi << endl;
    }

    return 0;
}