Imported Upstream version 1.72.0
[platform/upstream/boost.git] / libs / multiprecision / performance / sf_performance.hpp
1 ///////////////////////////////////////////////////////////////
2 //  Copyright 2011 John Maddock. Distributed under the Boost
3 //  Software License, Version 1.0. (See accompanying file
4 //  LICENSE_1_0.txt or copy at https://www.boost.org/LICENSE_1_0.txt
5
6 #define BOOST_MATH_MAX_ROOT_ITERATION_POLICY 750
7 #define BOOST_MATH_PROMOTE_DOUBLE_POLICY false
8
9 #if !defined(TEST_MPFR) && !defined(TEST_MPREAL) && !defined(TEST_MPF) && !defined(TEST_MPREAL) && !defined(TEST_CPP_DEC_FLOAT) && !defined(TEST_MPFR_CLASS) && !defined(TEST_FLOAT) && !defined(TEST_CPP_BIN_FLOAT)
10 #define TEST_MPFR
11 #define TEST_MPF
12 #define TEST_CPP_DEC_FLOAT
13 #define TEST_CPP_BIN_FLOAT
14 //#  define TEST_MPFR_CLASS
15 //#  define TEST_MPREAL
16 #define TEST_FLOAT
17 #endif
18
19 #ifdef TEST_FLOAT
20 #include "arithmetic_backend.hpp"
21 #endif
22 #ifdef TEST_MPFR_CLASS
23 #include <boost/math/bindings/mpfr.hpp>
24 #endif
25 #ifdef TEST_MPFR
26 #include <boost/multiprecision/mpfr.hpp>
27 #endif
28 #ifdef TEST_MPREAL
29 #include <boost/math/bindings/mpreal.hpp>
30 #endif
31 #ifdef TEST_MPF
32 #include <boost/multiprecision/gmp.hpp>
33 #endif
34 #ifdef TEST_CPP_DEC_FLOAT
35 #include <boost/multiprecision/cpp_dec_float.hpp>
36 #endif
37 #ifdef TEST_CPP_BIN_FLOAT
38 #include <boost/multiprecision/cpp_bin_float.hpp>
39 #include <boost/multiprecision/mpfr.hpp>
40 #endif
41 #include <boost/math/special_functions/bessel.hpp>
42 #include <boost/math/tools/rational.hpp>
43 #include <boost/math/distributions/non_central_t.hpp>
44 #include <libs/math/test/table_type.hpp>
45 #include <boost/chrono.hpp>
46 #include <boost/array.hpp>
47 #include <boost/thread.hpp>
48
49 template <class Real>
50 Real test_bessel();
51
52 template <class Clock>
53 struct stopwatch
54 {
55    typedef typename Clock::duration duration;
56    stopwatch()
57    {
58       m_start = Clock::now();
59    }
60    duration elapsed()
61    {
62       return Clock::now() - m_start;
63    }
64    void reset()
65    {
66       m_start = Clock::now();
67    }
68
69  private:
70    typename Clock::time_point m_start;
71 };
72
73 template <class Real>
74 Real test_bessel()
75 {
76    try
77    {
78 #define T double
79 #define SC_(x) x
80 #include "libs/math/test/bessel_i_int_data.ipp"
81 #include "libs/math/test/bessel_i_data.ipp"
82
83       Real r;
84
85       for (unsigned i = 0; i < bessel_i_int_data.size(); ++i)
86       {
87          r += boost::math::cyl_bessel_i(Real(bessel_i_int_data[i][0]), Real(bessel_i_int_data[i][1]));
88       }
89       for (unsigned i = 0; i < bessel_i_data.size(); ++i)
90       {
91          r += boost::math::cyl_bessel_i(Real(bessel_i_data[i][0]), Real(bessel_i_data[i][1]));
92       }
93
94 #include "libs/math/test/bessel_j_int_data.ipp"
95       for (unsigned i = 0; i < bessel_j_int_data.size(); ++i)
96       {
97          r += boost::math::cyl_bessel_j(Real(bessel_j_int_data[i][0]), Real(bessel_j_int_data[i][1]));
98       }
99
100 #include "libs/math/test/bessel_j_data.ipp"
101       for (unsigned i = 0; i < bessel_j_data.size(); ++i)
102       {
103          r += boost::math::cyl_bessel_j(Real(bessel_j_data[i][0]), Real(bessel_j_data[i][1]));
104       }
105
106 #include "libs/math/test/bessel_j_large_data.ipp"
107       for (unsigned i = 0; i < bessel_j_large_data.size(); ++i)
108       {
109          r += boost::math::cyl_bessel_j(Real(bessel_j_large_data[i][0]), Real(bessel_j_large_data[i][1]));
110       }
111
112 #include "libs/math/test/sph_bessel_data.ipp"
113       for (unsigned i = 0; i < sph_bessel_data.size(); ++i)
114       {
115          r += boost::math::sph_bessel(static_cast<unsigned>(sph_bessel_data[i][0]), Real(sph_bessel_data[i][1]));
116       }
117
118       return r;
119    }
120    catch (const std::exception& e)
121    {
122       std::cout << e.what() << std::endl;
123    }
124    return 0;
125 }
126
127 template <class Real>
128 Real test_polynomial()
129 {
130    static const unsigned t[] = {
131        2, 3, 4, 5, 6, 7, 8};
132    Real result = 0;
133    for (Real k = 2; k < 1000; ++k)
134       result += boost::math::tools::evaluate_polynomial(t, k);
135
136    return result;
137 }
138
139 template <class Real>
140 Real test_nct()
141 {
142 #define T double
143 #include "libs/math/test/nct.ipp"
144
145    Real result = 0;
146    for (unsigned i = 0; i < nct.size(); ++i)
147    {
148       try
149       {
150          result += quantile(boost::math::non_central_t_distribution<Real>(nct[i][0], nct[i][1]), nct[i][3]);
151          result += cdf(boost::math::non_central_t_distribution<Real>(nct[i][0], nct[i][1]), nct[i][2]);
152       }
153       catch (const std::exception&)
154       {}
155    }
156    return result;
157 }
158
159 extern unsigned allocation_count;
160
161 template <class Real>
162 void basic_allocation_test(const char* name, Real x)
163 {
164    static const unsigned a[] = {2, 3, 4, 5, 6, 7, 8};
165    allocation_count          = 0;
166    Real result               = (((((a[6] * x + a[5]) * x + a[4]) * x + a[3]) * x + a[2]) * x + a[1]) * x + a[0];
167    std::cout << "Allocation count for type " << name << " = " << allocation_count << std::endl;
168 }
169
170 template <class Real>
171 void poly_allocation_test(const char* name, Real x)
172 {
173    static const unsigned a[] = {2, 3, 4, 5, 6, 7, 8};
174    allocation_count          = 0;
175    Real result               = boost::math::tools::evaluate_polynomial(a, x);
176    std::cout << "Allocation count for type " << name << " = " << allocation_count << std::endl;
177 }
178
179 template <class Real>
180 void time_proc(const char* name, Real (*proc)(), unsigned threads = 1)
181 {
182    try
183    {
184       static Real total = 0;
185       allocation_count  = 0;
186       boost::chrono::duration<double>                 time;
187       stopwatch<boost::chrono::high_resolution_clock> c;
188       total += proc();
189       time = c.elapsed();
190       std::cout << "Time for " << name << " = " << time << std::endl;
191       std::cout << "Total allocations for " << name << " = " << allocation_count << std::endl;
192
193       for (unsigned thread_count = 1; thread_count < threads; ++thread_count)
194       {
195          c.reset();
196          boost::thread_group g;
197          for (unsigned i = 0; i <= thread_count; ++i)
198             g.create_thread(proc);
199          g.join_all();
200          time = c.elapsed();
201          std::cout << "Time for " << name << " (" << (thread_count + 1) << " threads) = " << time << std::endl;
202          std::cout << "Total allocations for " << name << " = " << allocation_count << std::endl;
203       }
204    }
205    catch (const std::exception& e)
206    {
207       std::cout << e.what() << std::endl;
208    }
209 }
210
211 using namespace boost::multiprecision;
212
213 void basic_tests();
214 void bessel_tests();
215 void poly_tests();
216 void nct_tests();