sf_performance.hpp 6.0 KB

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