]>
git.proxmox.com Git - ceph.git/blob - ceph/src/boost/libs/multiprecision/performance/rational_determinant_bench.cpp
1 // Copyright 2020 John Maddock. Distributed under the Boost
2 // Software License, Version 1.0. (See accompanying file
3 // LICENSE_1_0.txt or copy at https://www.boost.org/LICENSE_1_0.txt
7 #include <benchmark/benchmark.h>
8 #include <boost/multiprecision/cpp_int.hpp>
9 #include <boost/multiprecision/gmp.hpp>
11 #include <boost/math/special_functions/prime.hpp>
12 #include <boost/math/special_functions/pow.hpp>
16 #include <boost/random/mersenne_twister.hpp>
17 #include <boost/random/uniform_int.hpp>
20 T
generate_random(unsigned bits_wanted
)
22 static boost::random::mt19937 gen
;
23 typedef boost::random::mt19937::result_type random_type
;
27 if (std::numeric_limits
<T
>::is_bounded
&& (bits_wanted
== (unsigned)std::numeric_limits
<T
>::digits
))
29 max_val
= (std::numeric_limits
<T
>::max
)();
30 digits
= std::numeric_limits
<T
>::digits
;
34 max_val
= T(1) << bits_wanted
;
38 unsigned bits_per_r_val
= std::numeric_limits
<random_type
>::digits
- 1;
39 while ((random_type(1) << bits_per_r_val
) > (gen
.max
)())
42 unsigned terms_needed
= digits
/ bits_per_r_val
+ 1;
45 for (unsigned i
= 0; i
< terms_needed
; ++i
)
55 const std::vector
<std::vector
<T
> >& get_matrix_data(unsigned bits
);
58 const std::vector
<std::vector
<boost::multiprecision::cpp_rational
> >& get_matrix_data(unsigned bits
)
60 static std::map
<unsigned, std::vector
<std::vector
<boost::multiprecision::cpp_rational
> > > data
;
61 if (data
[bits
].size() == 0)
63 for (unsigned i
= 0; i
< 100; ++i
)
65 std::vector
<boost::multiprecision::cpp_rational
> matrix
;
66 for (unsigned j
= 0; j
< 9; ++j
)
68 boost::multiprecision::cpp_int
a(generate_random
<boost::multiprecision::cpp_int
>(bits
)), b(generate_random
<boost::multiprecision::cpp_int
>(bits
));
69 matrix
.push_back(boost::multiprecision::cpp_rational(a
, b
));
71 data
[bits
].push_back(matrix
);
78 const std::vector
<std::vector
<T
> >& get_matrix_data(unsigned bits
)
80 static std::map
<unsigned, std::vector
<std::vector
<T
> > > data
;
81 if (data
[bits
].empty())
83 const std::vector
<std::vector
<boost::multiprecision::cpp_rational
> >& d
= get_matrix_data
<boost::multiprecision::cpp_rational
>(bits
);
84 for (unsigned i
= 0; i
< 100; ++i
)
86 std::vector
<T
> matrix
;
87 for (unsigned j
= 0; j
< 9; ++j
)
89 matrix
.push_back(T(d
[i
][j
].str()));
91 data
[bits
].push_back(matrix
);
98 T
determinant(const std::vector
<T
>& data
)
100 const T m01
= data
[0] * data
[4] - data
[3] * data
[1];
101 const T m02
= data
[0] * data
[7] - data
[6] * data
[1];
102 const T m12
= data
[3] * data
[7] - data
[6] * data
[4];
103 return m01
* data
[8] - m02
* data
[5] + m12
* data
[2];
106 template <class Rational
>
107 static void BM_determinant(benchmark::State
& state
)
109 int bits
= state
.range(0);
110 const std::vector
<std::vector
<Rational
> >& data
= get_matrix_data
<Rational
>(bits
);
113 for(unsigned i
= 0; i
< data
.size(); ++i
)
114 benchmark::DoNotOptimize(determinant(data
[i
]));
119 constexpr unsigned lower_range
= 512;
120 constexpr unsigned upper_range
= 1 << 15;
122 BENCHMARK_TEMPLATE(BM_determinant
, boost::multiprecision::cpp_rational
)->RangeMultiplier(2)->Range(lower_range
, upper_range
)->Unit(benchmark::kMillisecond
);
123 BENCHMARK_TEMPLATE(BM_determinant
, boost::multiprecision::mpq_rational
)->RangeMultiplier(2)->Range(lower_range
, upper_range
)->Unit(benchmark::kMillisecond
);
124 BENCHMARK_TEMPLATE(BM_determinant
, mpq_class
)->RangeMultiplier(2)->Range(lower_range
, upper_range
)->Unit(benchmark::kMillisecond
);