Leibnitz Pi

Python Numpy

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

def leibnitz_pi(N):

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

def main():
    B = util.Benchmark()                        # Initialize Benchpress
    try:
        N,I = B.size                            # Grab command-line arguments
    except ValueError:
        N,I = (B.size[0], 1)
    B.start()                                   # Sample wall-clock start
    R = 0.0
    for _ in range(I):
        R += 4.0*leibnitz_pi(N)                      # Execute benchmark
        B.flush()
    B.stop()                                    # Sample wall-clock stop
    B.pprint()                                  # Print elapsed wall-clock etc.
    if B.verbose:                               # Print more, such as results
        print(R)

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 Bxx

#include <iostream>
#include <iomanip>
#include <bxx/bohrium.hpp>
#include <bp_util.h>

using namespace std;
using namespace bxx;

multi_array<double>& leibnitz_pi(int nelements)
{
    multi_array<int> n;
    n = range<int>(nelements);
    return sum(1.0/as<double>((int)4*n+(int)1) -
               1.0/as<double>((int)4*n+(int)3));
}

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];

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

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

    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;
}