math/doc/html/math_toolkit/tradoffs.html
2019-10-31 17:55:35 +00:00

180 lines
15 KiB
HTML

<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=US-ASCII">
<title>Trading Accuracy for Performance</title>
<link rel="stylesheet" href="../math.css" type="text/css">
<meta name="generator" content="DocBook XSL Stylesheets V1.79.1">
<link rel="home" href="../index.html" title="Math Toolkit 2.11.0">
<link rel="up" href="../perf.html" title="Chapter&#160;21.&#160;Performance">
<link rel="prev" href="getting_best.html" title="Getting the Best Performance from this Library: Compiler and Compiler Options">
<link rel="next" href="multiprecision.html" title="Cost of High-Precision Non-built-in Floating-point">
</head>
<body bgcolor="white" text="black" link="#0000FF" vlink="#840084" alink="#0000FF">
<table cellpadding="2" width="100%"><tr>
<td valign="top"><img alt="Boost C++ Libraries" width="277" height="86" src="../../../../../boost.png"></td>
<td align="center"><a href="../../../../../index.html">Home</a></td>
<td align="center"><a href="../../../../../libs/libraries.htm">Libraries</a></td>
<td align="center"><a href="http://www.boost.org/users/people.html">People</a></td>
<td align="center"><a href="http://www.boost.org/users/faq.html">FAQ</a></td>
<td align="center"><a href="../../../../../more/index.htm">More</a></td>
</tr></table>
<hr>
<div class="spirit-nav">
<a accesskey="p" href="getting_best.html"><img src="../../../../../doc/src/images/prev.png" alt="Prev"></a><a accesskey="u" href="../perf.html"><img src="../../../../../doc/src/images/up.png" alt="Up"></a><a accesskey="h" href="../index.html"><img src="../../../../../doc/src/images/home.png" alt="Home"></a><a accesskey="n" href="multiprecision.html"><img src="../../../../../doc/src/images/next.png" alt="Next"></a>
</div>
<div class="section">
<div class="titlepage"><div><div><h2 class="title" style="clear: both">
<a name="math_toolkit.tradoffs"></a><a class="link" href="tradoffs.html" title="Trading Accuracy for Performance">Trading Accuracy for Performance</a>
</h2></div></div></div>
<p>
There are a number of <a class="link" href="../policy.html" title="Chapter&#160;20.&#160;Policies: Controlling Precision, Error Handling etc">Policies</a> that can be used
to trade accuracy for performance:
</p>
<div class="itemizedlist"><ul class="itemizedlist" style="list-style-type: disc; ">
<li class="listitem">
Internal promotion: by default functions with <code class="computeroutput"><span class="keyword">float</span></code>
arguments are evaluated at <code class="computeroutput"><span class="keyword">double</span></code>
precision internally to ensure full precision in the result. Similarly
<code class="computeroutput"><span class="keyword">double</span></code> precision functions
are evaluated at <code class="computeroutput"><span class="keyword">long</span> <span class="keyword">double</span></code>
precision internally by default. Changing these defaults can have a significant
speed advantage at the expense of accuracy, note also that evaluating using
<code class="computeroutput"><span class="keyword">float</span></code> internally may result
in numerical instability for some of the more complex algorithms, we suggest
you use this option with care.
</li>
<li class="listitem">
Target accuracy: just because you choose to evaluate at <code class="computeroutput"><span class="keyword">double</span></code>
precision doesn't mean you necessarily want to target full 16-digit accuracy,
if you wish you can change the default (full machine precision) to whatever
is "good enough" for your particular use case.
</li>
</ul></div>
<p>
For example, suppose you want to evaluate <code class="computeroutput"><span class="keyword">double</span></code>
precision functions at <code class="computeroutput"><span class="keyword">double</span></code>
precision internally, you can change the global default by passing <code class="computeroutput"><span class="special">-</span><span class="identifier">DBOOST_MATH_PROMOTE_DOUBLE_POLICY</span><span class="special">=</span><span class="keyword">false</span></code> on the
command line, or at the point of call via something like this:
</p>
<pre class="programlisting"><span class="keyword">double</span> <span class="identifier">val</span> <span class="special">=</span> <span class="identifier">boost</span><span class="special">::</span><span class="identifier">math</span><span class="special">::</span><span class="identifier">erf</span><span class="special">(</span><span class="identifier">my_argument</span><span class="special">,</span> <span class="identifier">boost</span><span class="special">::</span><span class="identifier">math</span><span class="special">::</span><span class="identifier">policies</span><span class="special">::</span><span class="identifier">make_policy</span><span class="special">(</span><span class="identifier">boost</span><span class="special">::</span><span class="identifier">math</span><span class="special">::</span><span class="identifier">policies</span><span class="special">::</span><span class="identifier">promote_double</span><span class="special">&lt;</span><span class="keyword">false</span><span class="special">&gt;()));</span>
</pre>
<p>
However, an easier option might be:
</p>
<pre class="programlisting"><span class="preprocessor">#include</span> <span class="special">&lt;</span><span class="identifier">boost</span><span class="special">/</span><span class="identifier">math</span><span class="special">/</span><span class="identifier">special_functions</span><span class="special">.</span><span class="identifier">hpp</span><span class="special">&gt;</span> <span class="comment">// Or any individual special function header</span>
<span class="keyword">namespace</span> <span class="identifier">math</span><span class="special">{</span>
<span class="keyword">namespace</span> <span class="identifier">precise</span><span class="special">{</span>
<span class="comment">//</span>
<span class="comment">// Define a Policy for accurate evaluation - this is the same as the default, unless</span>
<span class="comment">// someone has changed the global defaults.</span>
<span class="comment">//</span>
<span class="keyword">typedef</span> <span class="identifier">boost</span><span class="special">::</span><span class="identifier">math</span><span class="special">::</span><span class="identifier">policies</span><span class="special">::</span><span class="identifier">policy</span><span class="special">&lt;&gt;</span> <span class="identifier">accurate_policy</span><span class="special">;</span>
<span class="comment">//</span>
<span class="comment">// Invoke BOOST_MATH_DECLARE_SPECIAL_FUNCTIONS to declare</span>
<span class="comment">// functions that use the above policy. Note no trailing</span>
<span class="comment">// ";" required on the macro call:</span>
<span class="comment">//</span>
<span class="identifier">BOOST_MATH_DECLARE_SPECIAL_FUNCTIONS</span><span class="special">(</span><span class="identifier">accurate_policy</span><span class="special">)</span>
<span class="special">}</span>
<span class="keyword">namespace</span> <span class="identifier">fast</span><span class="special">{</span>
<span class="comment">//</span>
<span class="comment">// Define a Policy for fast evaluation:</span>
<span class="comment">//</span>
<span class="keyword">using</span> <span class="keyword">namespace</span> <span class="identifier">boost</span><span class="special">::</span><span class="identifier">math</span><span class="special">::</span><span class="identifier">polcies</span><span class="special">;</span>
<span class="keyword">typedef</span> <span class="identifier">policy</span><span class="special">&lt;</span><span class="identifier">promote_double</span><span class="special">&lt;</span><span class="keyword">false</span><span class="special">&gt;</span> <span class="special">&gt;</span> <span class="identifier">fast_policy</span><span class="special">;</span>
<span class="comment">//</span>
<span class="comment">// Invoke BOOST_MATH_DECLARE_SPECIAL_FUNCTIONS:</span>
<span class="comment">//</span>
<span class="identifier">BOOST_MATH_DECLARE_SPECIAL_FUNCTIONS</span><span class="special">(</span><span class="identifier">fast_policy</span><span class="special">)</span>
<span class="special">}</span>
<span class="special">}</span>
</pre>
<p>
And now one can call:
</p>
<pre class="programlisting"><span class="identifier">math</span><span class="special">::</span><span class="identifier">accurate</span><span class="special">::</span><span class="identifier">tgamma</span><span class="special">(</span><span class="identifier">x</span><span class="special">);</span>
</pre>
<p>
For the "accurate" version of tgamma, and:
</p>
<pre class="programlisting"><span class="identifier">math</span><span class="special">::</span><span class="identifier">fast</span><span class="special">::</span><span class="identifier">tgamma</span><span class="special">(</span><span class="identifier">x</span><span class="special">);</span>
</pre>
<p>
For the faster version.
</p>
<p>
Had we wished to change the target precision (to 9 decimal places) as well
as the evaluation type used, we might have done:
</p>
<pre class="programlisting"><span class="keyword">namespace</span> <span class="identifier">math</span><span class="special">{</span>
<span class="keyword">namespace</span> <span class="identifier">fast</span><span class="special">{</span>
<span class="comment">//</span>
<span class="comment">// Define a Policy for fast evaluation:</span>
<span class="comment">//</span>
<span class="keyword">using</span> <span class="keyword">namespace</span> <span class="identifier">boost</span><span class="special">::</span><span class="identifier">math</span><span class="special">::</span><span class="identifier">polcies</span><span class="special">;</span>
<span class="keyword">typedef</span> <span class="identifier">policy</span><span class="special">&lt;</span><span class="identifier">promote_double</span><span class="special">&lt;</span><span class="keyword">false</span><span class="special">&gt;,</span> <span class="identifier">digits10</span><span class="special">&lt;</span><span class="number">9</span><span class="special">&gt;</span> <span class="special">&gt;</span> <span class="identifier">fast_policy</span><span class="special">;</span>
<span class="comment">//</span>
<span class="comment">// Invoke BOOST_MATH_DECLARE_SPECIAL_FUNCTIONS:</span>
<span class="comment">//</span>
<span class="identifier">BOOST_MATH_DECLARE_SPECIAL_FUNCTIONS</span><span class="special">(</span><span class="identifier">fast_policy</span><span class="special">)</span>
<span class="special">}</span>
<span class="special">}</span>
</pre>
<p>
One can do a similar thing with the distribution classes:
</p>
<pre class="programlisting"><span class="preprocessor">#include</span> <span class="special">&lt;</span><span class="identifier">boost</span><span class="special">/</span><span class="identifier">math</span><span class="special">/</span><span class="identifier">distributions</span><span class="special">.</span><span class="identifier">hpp</span><span class="special">&gt;</span> <span class="comment">// or any individual distribution header</span>
<span class="keyword">namespace</span> <span class="identifier">math</span><span class="special">{</span> <span class="keyword">namespace</span> <span class="identifier">fast</span><span class="special">{</span>
<span class="comment">//</span>
<span class="comment">// Define a policy for fastest possible evaluation:</span>
<span class="comment">//</span>
<span class="keyword">using</span> <span class="keyword">namespace</span> <span class="identifier">boost</span><span class="special">::</span><span class="identifier">math</span><span class="special">::</span><span class="identifier">polcies</span><span class="special">;</span>
<span class="keyword">typedef</span> <span class="identifier">policy</span><span class="special">&lt;</span><span class="identifier">promote_float</span><span class="special">&lt;</span><span class="keyword">false</span><span class="special">&gt;</span> <span class="special">&gt;</span> <span class="identifier">fast_float_policy</span><span class="special">;</span>
<span class="comment">//</span>
<span class="comment">// Invoke BOOST_MATH_DECLARE_DISTRIBUTIONS</span>
<span class="comment">//</span>
<span class="identifier">BOOST_MATH_DECLARE_DISTRIBUTIONS</span><span class="special">(</span><span class="keyword">float</span><span class="special">,</span> <span class="identifier">fast_float_policy</span><span class="special">)</span>
<span class="special">}}</span> <span class="comment">// namespaces</span>
<span class="comment">//</span>
<span class="comment">// And use:</span>
<span class="comment">//</span>
<span class="keyword">float</span> <span class="identifier">p_val</span> <span class="special">=</span> <span class="identifier">cdf</span><span class="special">(</span><span class="identifier">math</span><span class="special">::</span><span class="identifier">fast</span><span class="special">::</span><span class="identifier">normal</span><span class="special">(</span><span class="number">1.0f</span><span class="special">,</span> <span class="number">3.0f</span><span class="special">),</span> <span class="number">0.25f</span><span class="special">);</span>
</pre>
<p>
Here's how these options change the relative performance of the distributions
on Linux:
</p>
<p>
[table_Distribution_performance_comparison_for_different_performance_options_with_GNU_C_version_5_1_0_on_linux]
</p>
</div>
<table xmlns:rev="http://www.cs.rpi.edu/~gregod/boost/tools/doc/revision" width="100%"><tr>
<td align="left"></td>
<td align="right"><div class="copyright-footer">Copyright &#169; 2006-2019 Nikhar
Agrawal, Anton Bikineev, Paul A. Bristow, Marco Guazzone, Christopher Kormanyos,
Hubert Holin, Bruno Lalande, John Maddock, Jeremy Murphy, Matthew Pulver, Johan
R&#229;de, Gautam Sewani, Benjamin Sobotta, Nicholas Thompson, Thijs van den Berg,
Daryle Walker and Xiaogang Zhang<p>
Distributed under the Boost Software License, Version 1.0. (See accompanying
file LICENSE_1_0.txt or copy at <a href="http://www.boost.org/LICENSE_1_0.txt" target="_top">http://www.boost.org/LICENSE_1_0.txt</a>)
</p>
</div></td>
</tr></table>
<hr>
<div class="spirit-nav">
<a accesskey="p" href="getting_best.html"><img src="../../../../../doc/src/images/prev.png" alt="Prev"></a><a accesskey="u" href="../perf.html"><img src="../../../../../doc/src/images/up.png" alt="Up"></a><a accesskey="h" href="../index.html"><img src="../../../../../doc/src/images/home.png" alt="Home"></a><a accesskey="n" href="multiprecision.html"><img src="../../../../../doc/src/images/next.png" alt="Next"></a>
</div>
</body>
</html>