Rosenbrock

Python Numpy

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

def rosen(x):
    return np.sum(
        100.0*(x[1:]-x[:-1]**2.0)**2.0 + \
        (1-x[:-1])**2.0
    )

def main():
    B = util.Benchmark()                        # Initialize Benchpress
    N, T = B.size                               # Grab command-line arguments

    if B.inputfn:
        dataset = B.load_array()
    else:
        dataset = np.arange(
            N,
            dtype=B.dtype
        ) / B.dtype(N)                       # Create psuedo-data

    if B.dumpinput:
        B.dump_arrays("rosenbrock", {'input': dataset})

    B.start()                                   # Sample wall-clock start
    res = 0.0
    for _ in range(0, T):                      # Do T trials of..
        res += rosen(dataset)                   # ..executing rosenbrock.
        util.Benchmark().flush()
    res /= T
    B.stop()                                    # Sample wall-clock stop
    B.pprint()                                  # Print elapsed wall-clock etc.

    R = np.zeros((1), dtype=B.dtype)
    R[0] = res
    if B.outputfn:
        B.tofile(B.outputfn, {'res':R})

    if B.verbose:                               # Print more, such as results
        print(R)

if __name__ == "__main__":
    main()

C99 Seq

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

double rosenbrock(int nelements, double* x)
{
    double sum = 0.0;
    for(int i=0; i<nelements-2; ++i) {
        sum += 100.0*pow((x[i+1] - pow(x[i], 2)), 2) + pow((1-x[i]), 2);
    }
    return sum;
}

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

    // Create the pseudo-data
    double* dataset = (double*)malloc(sizeof(double)*nelements);

    for(int i=0; i<nelements; ++i) {
        dataset[i] = i/(double)nelements;
    }

    bp.timer_start();                               // Start timer
    double res = 0.0;
    for(int i=0; i<trials; ++i) {
        res += rosenbrock(nelements, dataset);      // Run benchmark
    }
    res /= trials;
    bp.timer_stop();                                // Stop timer
    
    bp.print("rosenbrock(c99_seq)");                // Print results..
    if (bp.args.verbose) {                          // ..and value.
        printf("Result = %f\n", res);
    }

    return 0;
}

Cpp11 Bxx

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

using namespace std;
using namespace bxx;

double rosenbrock(multi_array<double>& x)
{
    return scalar<double>(sum(
        100.0 * pow(x[_ABF()]-pow(x[_ABL()], 2.0), 2.0) +
        pow(1.0-x[_ABL()], 2.0)
    ));
}

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

    multi_array<double> dataset;                    // Create pseudo-data
    dataset = range<double>(nelements) / (double)nelements;

    Runtime::instance().flush();
    bp.timer_start();                               // Start timer
    double res = 0.0;
    for(int i=0; i<trials; ++i) {
        res = rosenbrock(dataset);                  // Run benchmark
    }
    Runtime::instance().flush();
    bp.timer_stop();                                // Stop timer
    bp.print("rosenbrock(cpp11_bxx)");
    if (bp.args.verbose) {                          // ..and value.
        cout << fixed << setprecision(11)
			 << "Result = " << res << endl;
    }

    return 0;
}

Cpp11 Omp

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

using namespace std;

double rosenbrock(int nelements, double* x)
{
    double sum = 0.0;
    #pragma omp parallel for reduction(+:sum)
    for(int i=0; i<nelements-2; ++i) {
        sum += 100.0*pow((x[i+1] - pow(x[i], 2)), 2) + pow((1-x[i]), 2);
    }
    return sum;
}

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

    // Create the pseudo-data
    double* dataset = (double*)malloc(sizeof(double)*nelements);

    #pragma omp parallel for
    for(int i=0; i<nelements; ++i) {
        dataset[i] = i/(double)nelements;
    }

    bp.timer_start();                               // Start timer
    double res = 0.0;
    for(int i=0; i<trials; ++i) {
        res += rosenbrock(nelements, dataset);      // Run benchmark
    }
    res /= trials;
    bp.timer_stop();                                // Stop timer
    bp.print("rosenbrock(cpp11_omp)");
    if (bp.args.verbose) {                          // ..and value.
        cout << fixed << setprecision(11)
			 << "Result = " << res << endl;
    }

    return 0;
}

Cpp11 Seq

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

using namespace std;

double rosenbrock(int nelements, double* x)
{
    double sum = 0.0;
    for(int i=0; i<nelements-2; ++i) {
        sum += 100.0*pow((x[i+1] - pow(x[i], 2)), 2) + pow((1-x[i]), 2);
    }
    return sum;
}

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

    // Create the pseudo-data
    double* dataset = (double*)malloc(sizeof(double)*nelements);

    for(int i=0; i<nelements; ++i) {
        dataset[i] = i/(double)nelements;
    }

    bp.timer_start();                               // Start timer
    double res = 0.0;
    for(int i=0; i<trials; ++i) {
        res += rosenbrock(nelements, dataset);      // Run benchmark
    }
    res /= trials;
    bp.timer_stop();                                // Stop timer
    bp.print("rosenbrock(cpp11_seq)");
    if (bp.args.verbose) {                          // ..and value.
        cout << fixed << setprecision(11)
			 << "Result = " << res << endl;
    }

    return 0;
}