1<html> 2<head> 3<meta http-equiv="Content-Type" content="text/html; charset=UTF-8"> 4<title>Programming</title> 5<link rel="stylesheet" href="../../../../../../../doc/src/boostbook.css" type="text/css"> 6<meta name="generator" content="DocBook XSL Stylesheets V1.79.1"> 7<link rel="home" href="../../../index.html" title="Boost.Sort"> 8<link rel="up" href="../sample_sort.html" title="3.2.- Sample_Sort"> 9<link rel="prev" href="../sample_sort.html" title="3.2.- Sample_Sort"> 10<link rel="next" href="../parallel_stable_sort.html" title="3.3.- Parallel_stable_sort"> 11</head> 12<body bgcolor="white" text="black" link="#0000FF" vlink="#840084" alink="#0000FF"> 13<table cellpadding="2" width="100%"><tr> 14<td valign="top"><img alt="Boost C++ Libraries" width="277" height="86" src="../../../../../../../boost.png"></td> 15<td align="center"><a href="../../../../../../../index.html">Home</a></td> 16<td align="center"><a href="../../../../../../../libs/libraries.htm">Libraries</a></td> 17<td align="center"><a href="http://www.boost.org/users/people.html">People</a></td> 18<td align="center"><a href="http://www.boost.org/users/faq.html">FAQ</a></td> 19<td align="center"><a href="../../../../../../../more/index.htm">More</a></td> 20</tr></table> 21<hr> 22<div class="spirit-nav"> 23<a accesskey="p" href="../sample_sort.html"><img src="../../../../../../../doc/src/images/prev.png" alt="Prev"></a><a accesskey="u" href="../sample_sort.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="../parallel_stable_sort.html"><img src="../../../../../../../doc/src/images/next.png" alt="Next"></a> 24</div> 25<div class="section"> 26<div class="titlepage"><div><div><h4 class="title"> 27<a name="sort.parallel.sample_sort.sample_programming"></a><a class="link" href="sample_programming.html" title="Programming">Programming</a> 28</h4></div></div></div> 29<div class="blockquote"><blockquote class="blockquote"> 30<h5> 31<a name="sort.parallel.sample_sort.sample_programming.h0"></a> 32 <span class="phrase"><a name="sort.parallel.sample_sort.sample_programming.thread_specification"></a></span><a class="link" href="sample_programming.html#sort.parallel.sample_sort.sample_programming.thread_specification"><span class="underline">Thread Specification</span></a> 33 </h5> 34<div class="blockquote"><blockquote class="blockquote"> 35<p> 36 This algorithm has an integer parameter indicating the <span class="bold"><strong>number 37 of threads</strong></span> to use in the sorting process, which always is 38 the last value in the call. The default value (if left unspecified) 39 is the number of HW threads of the machine where the program is running 40 provided by std::thread::hardware_concurrency(). 41 </p> 42<p> 43 If the number is 1 or 0, the algorithm runs with only 1 thread. 44 </p> 45<p> 46 The number of threads is not a fixed number, but is calculated in each 47 execution. The number of threads passed can be greater than the number 48 of hardware threads on the machine. We can pass 100 threads in a machine 49 with 4 HW threads, and in the same mode we can pass a function as (std::thread::hardware_concurrency() 50 / 4 ). If this value is 0, the program is executed with 1 thread. 51 </p> 52</blockquote></div> 53<h5> 54<a name="sort.parallel.sample_sort.sample_programming.h1"></a> 55 <span class="phrase"><a name="sort.parallel.sample_sort.sample_programming.programming"></a></span><a class="link" href="sample_programming.html#sort.parallel.sample_sort.sample_programming.programming"><span class="underline">Programming</span></a> 56 </h5> 57<div class="blockquote"><blockquote class="blockquote"> 58<p> 59 You only need to include the file boost/sort/sort.hpp. 60 </p> 61<p> 62 The algorithm run in the namespace boost::sort 63 </p> 64<pre class="programlisting"><span class="preprocessor">#include</span> <span class="special"><</span><span class="identifier">boost</span><span class="special">/</span><span class="identifier">sort</span><span class="special">/</span><span class="identifier">sort</span><span class="special">.</span><span class="identifier">hpp</span><span class="special">></span> 65 66 67<span class="keyword">template</span> <span class="special"><</span><span class="keyword">class</span> <span class="identifier">iter_t</span><span class="special">></span> 68<span class="keyword">void</span> <span class="identifier">sample_sort</span> <span class="special">(</span><span class="identifier">iter_t</span> <span class="identifier">first</span><span class="special">,</span> <span class="identifier">iter_t</span> <span class="identifier">last</span><span class="special">);</span> 69 70<span class="keyword">template</span> <span class="special"><</span><span class="keyword">class</span> <span class="identifier">iter_t</span><span class="special">,</span> <span class="keyword">typename</span> <span class="identifier">compare</span><span class="special">></span> 71<span class="keyword">void</span> <span class="identifier">sample_sort</span> <span class="special">(</span><span class="identifier">iter_t</span> <span class="identifier">first</span><span class="special">,</span> <span class="identifier">iter_t</span> <span class="identifier">last</span><span class="special">,</span> <span class="identifier">compare</span> <span class="identifier">comp</span><span class="special">);</span> 72 73<span class="keyword">template</span> <span class="special"><</span><span class="keyword">class</span> <span class="identifier">iter_t</span><span class="special">></span> 74<span class="keyword">void</span> <span class="identifier">sample_sort</span> <span class="special">(</span><span class="identifier">iter_t</span> <span class="identifier">first</span><span class="special">,</span> <span class="identifier">iter_t</span> <span class="identifier">last</span><span class="special">,</span> <span class="identifier">uint32_t</span> <span class="identifier">num_thread</span><span class="special">);</span> 75 76<span class="keyword">template</span> <span class="special"><</span><span class="keyword">class</span> <span class="identifier">iter_t</span><span class="special">,</span> <span class="keyword">typename</span> <span class="identifier">compare</span><span class="special">></span> 77<span class="keyword">void</span> <span class="identifier">sample_sort</span> <span class="special">(</span><span class="identifier">iter_t</span> <span class="identifier">first</span><span class="special">,</span> <span class="identifier">iter_t</span> <span class="identifier">last</span><span class="special">,</span> <span class="identifier">compare</span> <span class="identifier">comp</span><span class="special">,</span> <span class="identifier">uint32_t</span> <span class="identifier">num_thread</span><span class="special">);</span> 78</pre> 79<p> 80 This algorithm needs a <span class="bold"><strong>C++11 compliant compiler</strong></span>, 81 and doesn't need any other code or library. Correct operation is not 82 guaranteed with older compilers. 83 </p> 84<p> 85 If the number of threads is unspecified, this uses the result of std::thread::hardware_concurrency(). 86 </p> 87<p> 88 This algorithm uses a <span class="bold"><strong>comparison object</strong></span>, 89 in the same way as the standard library sort algorithms. If not defined, 90 the comparison object is std::less, which uses the < operator internally. 91 </p> 92<p> 93 This algorithm is <span class="bold"><strong>exception safe</strong></span>, 94 meaning that, the exceptions generated by the algorithm guarantee the 95 integrity of the objects to sort, but not their relative order. If 96 the exception is generated inside the objects (in the move or in the 97 copy constructor.. ) the results can be unpredictable. 98 </p> 99</blockquote></div> 100</blockquote></div> 101</div> 102<table xmlns:rev="http://www.cs.rpi.edu/~gregod/boost/tools/doc/revision" width="100%"><tr> 103<td align="left"></td> 104<td align="right"><div class="copyright-footer">Copyright © 2014-2017 Steven 105 Ross, Francisco Tapia, Orson Peters<p> 106 Distributed under the <a href="http://boost.org/LICENSE_1_0.txt" target="_top">Boost 107 Software License, Version 1.0</a>. 108 </p> 109</div></td> 110</tr></table> 111<hr> 112<div class="spirit-nav"> 113<a accesskey="p" href="../sample_sort.html"><img src="../../../../../../../doc/src/images/prev.png" alt="Prev"></a><a accesskey="u" href="../sample_sort.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="../parallel_stable_sort.html"><img src="../../../../../../../doc/src/images/next.png" alt="Next"></a> 114</div> 115</body> 116</html> 117