File: cohen_acceleration_performance.cpp

package info (click to toggle)
boost1.83 1.83.0-4.2
  • links: PTS, VCS
  • area: main
  • in suites: trixie
  • size: 545,456 kB
  • sloc: cpp: 3,857,086; xml: 125,552; ansic: 34,414; python: 25,887; asm: 5,276; sh: 4,799; ada: 1,681; makefile: 1,629; perl: 1,212; pascal: 1,139; sql: 810; yacc: 478; ruby: 102; lisp: 24; csh: 6
file content (117 lines) | stat: -rw-r--r-- 3,986 bytes parent folder | download | duplicates (14)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
//  (C) Copyright Nick Thompson 2020.
//  Use, modification and distribution are subject to the
//  Boost Software License, Version 1.0. (See accompanying file
//  LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)

#include <random>
#include <iostream>
#include <iomanip>
#include <benchmark/benchmark.h>
#include <boost/math/tools/cohen_acceleration.hpp>
#include <boost/multiprecision/float128.hpp>
#include <boost/multiprecision/mpfr.hpp>
#include <boost/multiprecision/cpp_bin_float.hpp>
#include <boost/core/demangle.hpp>

using boost::multiprecision::number;
using boost::multiprecision::mpfr_float_backend;
using boost::multiprecision::float128;
using boost::multiprecision::cpp_bin_float_50;
using boost::multiprecision::cpp_bin_float_100;
using boost::math::tools::cohen_acceleration;
using boost::math::constants::pi;

template<typename Real>
class G {
public:
    G(){
        k_ = 0;
    }
    
    Real operator()() {
        k_ += 1;
        return 1/(k_*k_);
    }

private:
    Real k_;
};


template<class Real>
void CohenAcceleration(benchmark::State& state)
{
    using std::abs;
    Real x = pi<Real>()*pi<Real>()/12;
    for (auto _ : state)
    {
        auto g = G<Real>();
        x = cohen_acceleration(g);
        benchmark::DoNotOptimize(x);
    }
    if (abs(x - pi<Real>()*pi<Real>()/12) > 16*std::numeric_limits<Real>::epsilon())
    {
        std::cerr << std::setprecision(std::numeric_limits<Real>::max_digits10);
        std::cerr << "Cohen acceleration computed " << x << " on type " << boost::core::demangle(typeid(Real).name()) << "\n";
        std::cerr << "But expected value is       " << pi<Real>()*pi<Real>()/12 << "\n";
    }
}

BENCHMARK_TEMPLATE(CohenAcceleration, float);
BENCHMARK_TEMPLATE(CohenAcceleration, double);
BENCHMARK_TEMPLATE(CohenAcceleration, long double);
BENCHMARK_TEMPLATE(CohenAcceleration, float128);
BENCHMARK_TEMPLATE(CohenAcceleration, cpp_bin_float_50);
BENCHMARK_TEMPLATE(CohenAcceleration, cpp_bin_float_100);
BENCHMARK_TEMPLATE(CohenAcceleration, number<mpfr_float_backend<100>>);
BENCHMARK_TEMPLATE(CohenAcceleration, number<mpfr_float_backend<200>>);
BENCHMARK_TEMPLATE(CohenAcceleration, number<mpfr_float_backend<300>>);
BENCHMARK_TEMPLATE(CohenAcceleration, number<mpfr_float_backend<400>>);
BENCHMARK_TEMPLATE(CohenAcceleration, number<mpfr_float_backend<1000>>);


template<class Real>
void NaiveSum(benchmark::State& state)
{
    using std::abs;
    Real x = pi<Real>()*pi<Real>()/12;
    for (auto _ : state)
    {
        auto g = G<Real>();
        Real term = g();
        x = term;
        bool even = false;
        while (term > std::numeric_limits<Real>::epsilon()/2) {
            term = g();
            if (even) {
                x += term;
                even = false;
            } else {
                x -= term;
                even = true;
            }
        }
        benchmark::DoNotOptimize(x);
    }
    // The accuracy tests don't pass because the sum is ill-conditioned:
    /*if (abs(x - pi<Real>()*pi<Real>()/12) > 16*std::numeric_limits<Real>::epsilon())
    {
        std::cerr << std::setprecision(std::numeric_limits<Real>::max_digits10);
        std::cerr << "Cohen acceleration computed " << x << " on type " << boost::core::demangle(typeid(Real).name()) << "\n";
        std::cerr << "But expected value is       " << pi<Real>()*pi<Real>()/12 << "\n";
    }*/
}

BENCHMARK_TEMPLATE(NaiveSum, float);
BENCHMARK_TEMPLATE(NaiveSum, double);
BENCHMARK_TEMPLATE(NaiveSum, long double);
BENCHMARK_TEMPLATE(NaiveSum, float128);
BENCHMARK_TEMPLATE(NaiveSum, cpp_bin_float_50);
BENCHMARK_TEMPLATE(NaiveSum, cpp_bin_float_100);
BENCHMARK_TEMPLATE(NaiveSum, number<mpfr_float_backend<100>>);
BENCHMARK_TEMPLATE(NaiveSum, number<mpfr_float_backend<200>>);
BENCHMARK_TEMPLATE(NaiveSum, number<mpfr_float_backend<300>>);
BENCHMARK_TEMPLATE(NaiveSum, number<mpfr_float_backend<400>>);
BENCHMARK_TEMPLATE(NaiveSum, number<mpfr_float_backend<1000>>);

BENCHMARK_MAIN();