-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbenchmarks_2pi_8cpp-example.htm
More file actions
190 lines (190 loc) · 13.6 KB
/
benchmarks_2pi_8cpp-example.htm
File metadata and controls
190 lines (190 loc) · 13.6 KB
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
179
180
181
182
183
184
185
186
187
188
189
190
<!-- HTML header for doxygen 1.9.5-->
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "https://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" lang="en-US">
<head>
<!-- Global site tag (gtag.js) - Google Analytics -->
<script async src="https://www.googletagmanager.com/gtag/js?id=UA-130950618-1"></script>
<script>
window.dataLayer = window.dataLayer || [];
function gtag(){dataLayer.push(arguments);}
gtag('js', new Date());
gtag('config', 'UA-130950618-1');
</script>
<meta http-equiv="Content-Type" content="text/xhtml;charset=UTF-8"/>
<meta http-equiv="X-UA-Compatible" content="IE=11"/>
<meta name="generator" content="Doxygen 1.9.5"/>
<meta name="viewport" content="width=device-width, initial-scale=1"/>
<title>ArrayFire: benchmarks/pi.cpp</title>
<link href="tabs.css" rel="stylesheet" type="text/css"/>
<script type="text/javascript" src="jquery.js"></script>
<script type="text/javascript" src="dynsections.js"></script>
<link href="navtree.css" rel="stylesheet" type="text/css"/>
<script type="text/javascript" src="resize.js"></script>
<script type="text/javascript" src="navtreedata.js"></script>
<script type="text/javascript" src="navtree.js"></script>
<script type="text/x-mathjax-config">
MathJax.Hub.Config({
extensions: ["tex2jax.js"],
jax: ["input/TeX","output/HTML-CSS"],
});
</script>
<script type="text/javascript" async="async" src="https://cdnjs.cloudflare.com/ajax/libs/mathjax/2.7.1/MathJax.js"></script>
<link href="doxygen.css" rel="stylesheet" type="text/css" />
<link href="arrayfire.css" rel="stylesheet" type="text/css"/>
<link href="doxygen-awesome.css" rel="stylesheet" type="text/css"/>
<link href="doxygen-awesome-sidebar-only.css" rel="stylesheet" type="text/css"/>
<script type="text/javascript" src="doxygen-awesome-darkmode-toggle.js"></script>
<script type="text/javascript" src="doxygen-awesome-fragment-copy-button.js"></script>
<script type="text/javascript" src="doxygen-awesome-interactive-toc.js"></script>
<script type="text/javascript">
DoxygenAwesomeDarkModeToggle.init()
DoxygenAwesomeInteractiveToc.init()
DoxygenAwesomeFragmentCopyButton.init()
</script>
</head>
<body>
<div id="top"><!-- do not remove this div, it is closed by doxygen! -->
<div id="titlearea">
<table cellspacing="2" cellpadding="2" width="100%">
<tbody>
<tr id="projectrow">
<td id="projectlogo"><a href="index.htm"><img alt="Logo" src="arrayfire_logo.png"/></a></td>
</tr>
<tr id="projectrow">
<td>
<div id="projectbrief">A high-performance general-purpose compute library</div>
</td>
</tr>
<!--END !PROJECT_NAME-->
<div>
<td id="gsearch">
<script async src="https://cse.google.com/cse.js?cx=004356362924927882526:zup3ehe-7bs"></script>
<div class="gcse-search"></div>
</td>
</div>
</tr>
<tr>
<td>
<div id="togglediv"></div>
</td>
</tr>
</tbody>
</table>
</div>
<!-- end header part --><!-- Generated by Doxygen 1.9.5 -->
<div id="navrow1" class="tabs">
<ul class="tablist">
<li><a href="index.htm"><span>Main Page</span></a></li>
<li><a href="tutorials.htm"><span>Tutorials</span></a></li>
<li><a href="modules.htm"><span>Functions</span></a></li>
<li><a href="releasenotes.htm"><span>Release Notes</span></a></li>
<li><a href="examples.htm"><span>Examples</span></a></li>
</ul>
</div>
</div><!-- top -->
<div id="side-nav" class="ui-resizable side-nav-resizable">
<div id="nav-tree">
<div id="nav-tree-contents">
<div id="nav-sync" class="sync"></div>
</div>
</div>
<div id="splitbar" style="-moz-user-select:none;"
class="ui-resizable-handle">
</div>
</div>
<script type="text/javascript">
/* @license magnet:?xt=urn:btih:d3d9a9a6595521f9666a5e94cc830dab83b65699&dn=expat.txt MIT */
$(document).ready(function(){initNavTree('benchmarks_2pi_8cpp-example.htm',''); initResizable(); });
/* @license-end */
</script>
<div id="doc-content">
<div class="header">
<div class="headertitle"><div class="title">benchmarks/pi.cpp</div></div>
</div><!--header-->
<div class="contents">
<div class="fragment"><div class="line"><span class="comment">/*******************************************************</span></div>
<div class="line"><span class="comment"> * Copyright (c) 2014, ArrayFire</span></div>
<div class="line"><span class="comment"> * All rights reserved.</span></div>
<div class="line"><span class="comment"> *</span></div>
<div class="line"><span class="comment"> * This file is distributed under 3-clause BSD license.</span></div>
<div class="line"><span class="comment"> * The complete license agreement can be obtained at:</span></div>
<div class="line"><span class="comment"> * http://arrayfire.com/licenses/BSD-3-Clause</span></div>
<div class="line"><span class="comment"> ********************************************************/</span></div>
<div class="line"> </div>
<div class="line"><span class="comment">/*</span></div>
<div class="line"><span class="comment"> monte-carlo estimation of PI</span></div>
<div class="line"><span class="comment"></span> </div>
<div class="line"><span class="comment"> algorithm:</span></div>
<div class="line"><span class="comment"> - generate random (x,y) samples uniformly</span></div>
<div class="line"><span class="comment"> - count what percent fell inside (top quarter) of unit circle</span></div>
<div class="line"><span class="comment">*/</span></div>
<div class="line"> </div>
<div class="line"><span class="preprocessor">#include <<a class="code" href="arrayfire_8h.htm">arrayfire.h</a>></span></div>
<div class="line"><span class="preprocessor">#include <math.h></span></div>
<div class="line"><span class="preprocessor">#include <stdio.h></span></div>
<div class="line"><span class="preprocessor">#include <cstdlib></span></div>
<div class="line"><span class="keyword">using namespace </span><a class="code hl_namespace" href="namespaceaf.htm">af</a>;</div>
<div class="line"> </div>
<div class="line"><span class="comment">// generate millions of random samples</span></div>
<div class="line"><span class="keyword">static</span> <span class="keywordtype">int</span> samples = 20e6;</div>
<div class="line"> </div>
<div class="line"><span class="comment">/* Self-contained code to run host and device estimates of PI. Note that</span></div>
<div class="line"><span class="comment"> each is generating its own random values, so the estimates of PI</span></div>
<div class="line"><span class="comment"> will differ. */</span></div>
<div class="line"><span class="keyword">static</span> <span class="keywordtype">double</span> pi_device() {</div>
<div class="line"> <a id="_a0" name="_a0"></a><a class="code hl_class" href="classaf_1_1array.htm">array</a> x = randu(samples, <a id="a1" name="a1"></a><a class="code hl_enumvalue" href="defines_8h.htm#a023d8ac325fb14f1712a52fb0940b1d5a82ea90203678bdd0b547068f0a76524b">f32</a>), y = randu(samples, <a class="code hl_enumvalue" href="defines_8h.htm#a023d8ac325fb14f1712a52fb0940b1d5a82ea90203678bdd0b547068f0a76524b">f32</a>);</div>
<div class="line"> <span class="keywordflow">return</span> 4.0 * sum<float>(sqrt(x * x + y * y) < 1) / samples;</div>
<div class="line">}</div>
<div class="line"> </div>
<div class="line"><span class="keyword">static</span> <span class="keywordtype">double</span> pi_host() {</div>
<div class="line"> <span class="keywordtype">int</span> count = 0;</div>
<div class="line"> <span class="keywordflow">for</span> (<span class="keywordtype">int</span> i = 0; i < samples; ++i) {</div>
<div class="line"> <span class="keywordtype">float</span> x = float(rand()) / float(RAND_MAX);</div>
<div class="line"> <span class="keywordtype">float</span> y = float(rand()) / float(RAND_MAX);</div>
<div class="line"> <span class="keywordflow">if</span> (sqrt(x * x + y * y) < 1) count++;</div>
<div class="line"> }</div>
<div class="line"> <span class="keywordflow">return</span> 4.0 * <a class="code hl_function" href="group__reduce__func__count.htm#ga93235c9215589d1a6448e8cda3903b92">count</a> / samples;</div>
<div class="line">}</div>
<div class="line"> </div>
<div class="line"><span class="comment">// void wrappers for timeit()</span></div>
<div class="line"><span class="keyword">static</span> <span class="keywordtype">void</span> device_wrapper() { pi_device(); }</div>
<div class="line"><span class="keyword">static</span> <span class="keywordtype">void</span> host_wrapper() { pi_host(); }</div>
<div class="line"> </div>
<div class="line"><span class="keywordtype">int</span> main(<span class="keywordtype">int</span> argc, <span class="keywordtype">char</span>** argv) {</div>
<div class="line"> <span class="keywordflow">try</span> {</div>
<div class="line"> <span class="keywordtype">int</span> device = argc > 1 ? atoi(argv[1]) : 0;</div>
<div class="line"> <a class="code hl_function" href="group__device__func__set.htm#gad7c36828c21e7efc06a067bbc47d8d3b">setDevice</a>(device);</div>
<div class="line"> <a class="code hl_function" href="group__device__func__info.htm#ga3e22b6ff6e0940bb31577bea2863ca27">info</a>();</div>
<div class="line"> </div>
<div class="line"> printf(<span class="stringliteral">"device: %.5f seconds to estimate pi = %.5f\n"</span>,</div>
<div class="line"> <a class="code hl_function" href="namespaceaf.htm#a08955393264b77be242246c0b1148687">timeit</a>(device_wrapper), pi_device());</div>
<div class="line"> printf(<span class="stringliteral">" host: %.5f seconds to estimate pi = %.5f\n"</span>,</div>
<div class="line"> <a class="code hl_function" href="namespaceaf.htm#a08955393264b77be242246c0b1148687">timeit</a>(host_wrapper), pi_host());</div>
<div class="line"> } <span class="keywordflow">catch</span> (<a id="_a2" name="_a2"></a><a class="code hl_class" href="classaf_1_1exception.htm">exception</a>& e) {</div>
<div class="line"> fprintf(stderr, <span class="stringliteral">"%s\n"</span>, e.<a id="a3" name="a3"></a><a class="code hl_function" href="classaf_1_1exception.htm#aa884ff67408851f00e50cf971a4dfd05">what</a>());</div>
<div class="line"> <span class="keywordflow">throw</span>;</div>
<div class="line"> }</div>
<div class="line"> </div>
<div class="line"> <span class="keywordflow">return</span> 0;</div>
<div class="line">}</div>
<div class="ttc" id="aarrayfire_8h_htm"><div class="ttname"><a href="arrayfire_8h.htm">arrayfire.h</a></div></div>
<div class="ttc" id="aclassaf_1_1array_htm"><div class="ttname"><a href="classaf_1_1array.htm">af::array</a></div><div class="ttdoc">A multi dimensional data container.</div><div class="ttdef"><b>Definition:</b> <a href="array_8h_source.htm#l00037">array.h:37</a></div></div>
<div class="ttc" id="aclassaf_1_1exception_htm"><div class="ttname"><a href="classaf_1_1exception.htm">af::exception</a></div><div class="ttdoc">An ArrayFire exception class.</div><div class="ttdef"><b>Definition:</b> <a href="exception_8h_source.htm#l00021">exception.h:22</a></div></div>
<div class="ttc" id="aclassaf_1_1exception_htm_aa884ff67408851f00e50cf971a4dfd05"><div class="ttname"><a href="classaf_1_1exception.htm#aa884ff67408851f00e50cf971a4dfd05">af::exception::what</a></div><div class="ttdeci">virtual const char * what() const</div><div class="ttdoc">Returns an error message for the exception in a string format.</div><div class="ttdef"><b>Definition:</b> <a href="exception_8h_source.htm#l00046">exception.h:46</a></div></div>
<div class="ttc" id="adefines_8h_htm_a023d8ac325fb14f1712a52fb0940b1d5a82ea90203678bdd0b547068f0a76524b"><div class="ttname"><a href="defines_8h.htm#a023d8ac325fb14f1712a52fb0940b1d5a82ea90203678bdd0b547068f0a76524b">f32</a></div><div class="ttdeci">@ f32</div><div class="ttdoc">32-bit floating point values</div><div class="ttdef"><b>Definition:</b> <a href="defines_8h_source.htm#l00211">defines.h:211</a></div></div>
<div class="ttc" id="agroup__device__func__info_htm_ga3e22b6ff6e0940bb31577bea2863ca27"><div class="ttname"><a href="group__device__func__info.htm#ga3e22b6ff6e0940bb31577bea2863ca27">af::info</a></div><div class="ttdeci">AFAPI void info()</div></div>
<div class="ttc" id="agroup__device__func__set_htm_gad7c36828c21e7efc06a067bbc47d8d3b"><div class="ttname"><a href="group__device__func__set.htm#gad7c36828c21e7efc06a067bbc47d8d3b">af::setDevice</a></div><div class="ttdeci">AFAPI void setDevice(const int device)</div><div class="ttdoc">Sets the current device.</div></div>
<div class="ttc" id="agroup__reduce__func__count_htm_ga93235c9215589d1a6448e8cda3903b92"><div class="ttname"><a href="group__reduce__func__count.htm#ga93235c9215589d1a6448e8cda3903b92">af::count</a></div><div class="ttdeci">AFAPI array count(const array &in, const int dim=-1)</div><div class="ttdoc">C++ Interface to count non-zero values in an array along a given dimension.</div></div>
<div class="ttc" id="anamespaceaf_htm"><div class="ttname"><a href="namespaceaf.htm">af</a></div><div class="ttdef"><b>Definition:</b> <a href="algorithm_8h_source.htm#l00014">algorithm.h:15</a></div></div>
<div class="ttc" id="anamespaceaf_htm_a08955393264b77be242246c0b1148687"><div class="ttname"><a href="namespaceaf.htm#a08955393264b77be242246c0b1148687">af::timeit</a></div><div class="ttdeci">AFAPI double timeit(void(*fn)())</div></div>
</div><!-- fragment --> </div><!-- contents -->
</div><!-- doc-content -->
<!-- HTML footer for doxygen 1.9.3-->
<!-- start footer part -->
<div id="nav-path" class="navpath"><!-- id is needed for treeview function! -->
<ul>
<li class="footer">Generated by <a href="https://www.doxygen.org/index.html"><img class="footer" src="doxygen.svg" width="104" height="31" alt="doxygen"/></a> 1.9.5 </li>
</ul>
</div>
</body>
</html>