summaryrefslogtreecommitdiff
path: root/boost/libs/math/doc/html/math_toolkit/tradoffs.html
blob: 93e9f985610781a4bd2c242232ef282f1969e86f (plain)
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
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
<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.5.1">
<link rel="up" href="../perf.html" title="Chapter&#160;16.&#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;15.&#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-2010, 2012-2014 Nikhar Agrawal,
      Anton Bikineev, Paul A. Bristow, Marco Guazzone, Christopher Kormanyos, Hubert
      Holin, Bruno Lalande, John Maddock, Jeremy Murphy, Johan R&#229;de, Gautam Sewani,
      Benjamin Sobotta, 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>