Rosenbrock

Python Numpy

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

bench = util.Benchmark("Rosenbrock", "size*iterations")


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


def main():
    N, T = bench.args.size  # Grab command-line arguments
    dataset = np.arange(N, dtype=bench.dtype) / bench.dtype(N)

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


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