Files
simdjson-simdjson/md_doc_implementation_selection.html
T
2024-02-23 22:10:36 +00:00

172 lines
12 KiB
HTML

<!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">
<head>
<meta http-equiv="Content-Type" content="text/xhtml;charset=UTF-8"/>
<meta http-equiv="X-UA-Compatible" content="IE=9"/>
<meta name="generator" content="Doxygen 1.9.1"/>
<meta name="viewport" content="width=device-width, initial-scale=1"/>
<title>simdjson: CPU Architecture-Specific Implementations</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>
<link href="search/search.css" rel="stylesheet" type="text/css"/>
<script type="text/javascript" src="search/searchdata.js"></script>
<script type="text/javascript" src="search/search.js"></script>
<link href="doxygen.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"/>
<link href="doxygen-awesome-sidebar-only-darkmode-toggle.css" rel="stylesheet" type="text/css"/>
</head>
<body>
<div id="top"><!-- do not remove this div, it is closed by doxygen! -->
<div id="titlearea">
<table cellspacing="0" cellpadding="0">
<tbody>
<tr style="height: 56px;">
<td id="projectlogo"><img alt="Logo" src="logotiny.png"/></td>
<td id="projectalign" style="padding-left: 0.5em;">
<div id="projectname">simdjson
&#160;<span id="projectnumber">3.7.0</span>
</div>
<div id="projectbrief">Ridiculously Fast JSON</div>
</td>
</tr>
</tbody>
</table>
</div>
<!-- end header part -->
<!-- Generated by Doxygen 1.9.1 -->
<script type="text/javascript">
/* @license magnet:?xt=urn:btih:cf05388f2679ee054f2beb29a391d25f4e673ac3&amp;dn=gpl-2.0.txt GPL-v2 */
var searchBox = new SearchBox("searchBox", "search",false,'Search','.html');
/* @license-end */
</script>
<script type="text/javascript" src="menudata.js"></script>
<script type="text/javascript" src="menu.js"></script>
<script type="text/javascript">
/* @license magnet:?xt=urn:btih:cf05388f2679ee054f2beb29a391d25f4e673ac3&amp;dn=gpl-2.0.txt GPL-v2 */
$(function() {
initMenu('',true,false,'search.php','Search');
$(document).ready(function() { init_search(); });
});
/* @license-end */</script>
<div id="main-nav"></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:cf05388f2679ee054f2beb29a391d25f4e673ac3&amp;dn=gpl-2.0.txt GPL-v2 */
$(document).ready(function(){initNavTree('md_doc_implementation_selection.html',''); initResizable(); });
/* @license-end */
</script>
<div id="doc-content">
<!-- window showing the filter options -->
<div id="MSearchSelectWindow"
onmouseover="return searchBox.OnSearchSelectShow()"
onmouseout="return searchBox.OnSearchSelectHide()"
onkeydown="return searchBox.OnSearchSelectKey(event)">
</div>
<!-- iframe showing the search results (closed by default) -->
<div id="MSearchResultsWindow">
<iframe src="javascript:void(0)" frameborder="0"
name="MSearchResults" id="MSearchResults">
</iframe>
</div>
<div class="PageDoc"><div class="header">
<div class="headertitle">
<div class="title">CPU Architecture-Specific Implementations </div> </div>
</div><!--header-->
<div class="contents">
<div class="textblock"><ul>
<li><a href="#overview">Overview</a></li>
<li><a href="#runtime-cpu-detection">Runtime CPU Detection</a></li>
<li><a href="#inspecting-the-detected-implementation">Inspecting the Detected Implementation</a></li>
<li><a href="#querying-available-implementations">Querying Available Implementations</a></li>
<li><a href="#manually-selecting-the-implementation">Manually Selecting the Implementation</a></li>
<li><a href="#checking-that-an-implementation-can-run-on-your-system">Checking that an Implementation can Run on your System</a></li>
</ul>
<h1><a class="anchor" id="autotoc_md50"></a>
Overview</h1>
<p>The simdjson library takes advantage of SIMD instruction sets such as NEON, SSE and AVX to achieve much of its speed. Because these instruction sets work differently, simdjson has to compile a different version of the JSON parser for different CPU architectures, often with different algorithms to take better advantage of a given CPU!</p>
<p>The current implementations are:</p><ul>
<li>icelake: AVX-512F, AVX-512_VBMI, AVX-512_VBMI2, AVX-512_DQ, AVX-512512_CD, AVX-512_BW, AVX-512_VL (2019 Intel Ice Lake, Intel Rocket Lake, Intel Sapphire Rapids, AMD Zen 4)</li>
<li>haswell: AVX2 (2013 Intel Haswell or later, all AMD Zen processors)</li>
<li>westmere: SSE4.2 (2010 Westmere or later).</li>
<li>arm64: 64-bit ARMv8-A NEON</li>
<li>ppc64: 64-bit POWER8 and POWER9 with VSX and ALTIVEC extensions. Both big endian and little endian are implemented, depending on the compiler you are using. The library is tested on recent, little-endian, POWER systems.</li>
<li>fallback: A generic implementation that runs on any 64-bit processor.</li>
</ul>
<p>In many cases, you don't know where your compiled binary is going to run, so simdjson automatically compiles <em>all</em> the implementations into the executable. On Intel, it will include 4 implementations (icelake, haswell, westmere and fallback), on 64-bit ARM it will include just one since running dispatching is unnecessary, and on PPC it will include 2 (ppc64 and fallback).</p>
<p>If you know more about where you're going to run and want to save the space, you can disable any of these implementations at compile time with <code>-DSIMDJSON_IMPLEMENTATION_X=0</code> (where X is ICELAKE, HASWELL, WESTMERE, ARM64, PPC64 and FALLBACK).</p>
<p>The simdjson library automatically sets header flags for each implementation as it compiles; there is no need to set architecture-specific flags yourself (e.g., <code>-mavx2</code>, <code>/AVX2</code> or <code>-march=haswell</code>), and it may even break runtime dispatch and your binaries will fail to run on older processors. <em>Note:</em> for POWER9 processors make sure you compile it with <code>-mcpu=power9</code> and <code>-mtune=power9</code> to get maximum performance.</p>
<h1><a class="anchor" id="autotoc_md51"></a>
Runtime CPU Detection</h1>
<p>When you first use simdjson, it will detect the CPU you're running on, and swap over to the fastest implementation for it. This is a small, one-time cost and for many people will be paid the first time they call <code>parse()</code> or <code>load()</code>.</p>
<h1><a class="anchor" id="autotoc_md52"></a>
Inspecting the Detected Implementation</h1>
<p>You can check what implementation is running with <code>active_implementation</code>:</p>
<div class="fragment"><div class="line"> {c++}</div>
<div class="line">cout &lt;&lt; &quot;simdjson v&quot; &lt;&lt; SIMDJSON_VERSION &lt;&lt; endl;</div>
<div class="line">cout &lt;&lt; &quot;Detected the best implementation for your machine: &quot; &lt;&lt; simdjson::get_active_implementation()-&gt;name();</div>
<div class="line">cout &lt;&lt; &quot;(&quot; &lt;&lt; simdjson::get_active_implementation()-&gt;description() &lt;&lt; &quot;)&quot; &lt;&lt; endl;</div>
</div><!-- fragment --><p>Implementation detection will happen in this case when you first call <code>name()</code>.</p>
<h1><a class="anchor" id="autotoc_md53"></a>
Querying Available Implementations</h1>
<p>You can list all available implementations, regardless of which one was selected:</p>
<div class="fragment"><div class="line"> {c++}</div>
<div class="line">for (auto implementation : simdjson::get_available_implementations()) {</div>
<div class="line"> cout &lt;&lt; implementation-&gt;name() &lt;&lt; &quot;: &quot; &lt;&lt; implementation-&gt;description() &lt;&lt; endl;</div>
<div class="line">}</div>
</div><!-- fragment --><p>And look them up by name:</p>
<div class="fragment"><div class="line"> {c++}</div>
<div class="line">cout &lt;&lt; simdjson::get_available_implementations()[&quot;fallback&quot;]-&gt;description() &lt;&lt; endl;</div>
</div><!-- fragment --><p> When an implementation is not available, the bracket call <code><a class="el" href="namespacesimdjson.html#a7e4483a8ae684fdb5b2f1e7d4a8acc69" title="The list of available implementations compiled into simdjson.">simdjson::get_available_implementations()</a>[name]</code> will return the null pointer.</p>
<p>The available implementations have been compiled but may not necessarily be run safely on your system see <a href="#checking-that-an-implementation-can-run-on-your-system">Checking that an Implementation can Run on your System</a>.</p>
<h1><a class="anchor" id="autotoc_md54"></a>
Manually Selecting the Implementation</h1>
<p>If you're trying to do performance tests or see how different implementations of simdjson run, you can select the CPU architecture yourself:</p>
<div class="fragment"><div class="line"> {c++}</div>
<div class="line">// Use the fallback implementation, even though my machine is fast enough for anything</div>
<div class="line">simdjson::get_active_implementation() = simdjson::get_available_implementations()[&quot;fallback&quot;];</div>
</div><!-- fragment --><p>You are responsible for ensuring that the requirements of the selected implementation match your current system. Furthermore, you should check that the implementation is available before setting it to <code><a class="el" href="namespacesimdjson.html#abd8b8fd009878125822fca9a2624f6a6" title="The active implementation.">simdjson::get_active_implementation()</a></code> by comparing it with the null pointer.</p>
<div class="fragment"><div class="line"> {c++}</div>
<div class="line">auto my_implementation = simdjson::get_available_implementations()[&quot;haswell&quot;];</div>
<div class="line">if (! my_implementation) { exit(1); }</div>
<div class="line">if (! my_implementation-&gt;supported_by_runtime_system()) { exit(1); }</div>
<div class="line">simdjson::get_active_implementation() = my_implementation;</div>
</div><!-- fragment --><h1><a class="anchor" id="autotoc_md55"></a>
Checking that an Implementation can Run on your System</h1>
<p>You should call <code>supported_by_runtime_system()</code> to compare the processor's features with the need of the implementation.</p>
<div class="fragment"><div class="line"> {c++}</div>
<div class="line">for (auto implementation : simdjson::get_available_implementations()) {</div>
<div class="line"> if (implementation-&gt;supported_by_runtime_system()) {</div>
<div class="line"> cout &lt;&lt; implementation-&gt;name() &lt;&lt; &quot;: &quot; &lt;&lt; implementation-&gt;description() &lt;&lt; endl;</div>
<div class="line"> }</div>
<div class="line">}</div>
</div><!-- fragment --><p>The call to <code>supported_by_runtime_system()</code> may be relatively expensive. Do not call <code>supported_by_runtime_system()</code> each time you parse a JSON input (for example). It is meant to be called a handful of times at most in the life of a program. </p>
</div></div><!-- contents -->
</div><!-- PageDoc -->
</div><!-- doc-content -->
<!-- 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.1 </li>
</ul>
</div>
</body>
</html>