<?xml version='1.0' encoding='UTF-8' standalone='no'?>
<doxygen xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="compound.xsd" version="1.9.1" xml:lang="en-US">
  <compounddef id="ParallelSort" kind="page">
    <compoundname>ParallelSort</compoundname>
    <title>Parallel Sort</title>
    <tableofcontents>
      <tocsect>
        <name>Include the Header</name>
        <reference>ParallelSort_1ParallelSortInclude</reference>
    </tocsect>
      <tocsect>
        <name>Sort a Range of Items</name>
        <reference>ParallelSort_1SortARangeOfItems</reference>
    </tocsect>
      <tocsect>
        <name>Sort a Range of Items with a Custom Comparator</name>
        <reference>ParallelSort_1SortARangeOfItemsWithACustomComparator</reference>
    </tocsect>
      <tocsect>
        <name>Enable Stateful Data Passing</name>
        <reference>ParallelSort_1ParallelSortEnableStatefulDataPassing</reference>
    </tocsect>
    </tableofcontents>
    <briefdescription>
    </briefdescription>
    <detaileddescription>
<para>Taskflow provides template functions for constructing tasks to sort ranges of items in parallel.</para>
<sect1 id="ParallelSort_1ParallelSortInclude">
<title>Include the Header</title>
<para>You need to include the header file, <computeroutput>taskflow/algorithm/sort.hpp</computeroutput>, for creating a parallel-sort task.</para>
<para><programlisting filename=".cpp"><codeline><highlight class="preprocessor">#include<sp/>&lt;taskflow/algorithm/sort.hpp&gt;</highlight></codeline>
</programlisting></para>
</sect1>
<sect1 id="ParallelSort_1SortARangeOfItems">
<title>Sort a Range of Items</title>
<para>The task created by <ref refid="classtf_1_1FlowBuilder_1a7d844e9856c7c65b26ccdb83ffdab1d6" kindref="member">tf::Taskflow::sort(B first, E last)</ref> performs parallel sort to rank a range of elements specified by <computeroutput>[first, last)</computeroutput> in increasing order. The given iterators must be <emphasis>random-accessible</emphasis>. The following example creates a task to sort a data vector in increasing order.</para>
<para><programlisting filename=".cpp"><codeline><highlight class="normal"><ref refid="classtf_1_1Taskflow" kindref="compound">tf::Taskflow</ref><sp/>taskflow;</highlight></codeline>
<codeline><highlight class="normal"><ref refid="classtf_1_1Executor" kindref="compound">tf::Executor</ref><sp/>executor;</highlight></codeline>
<codeline><highlight class="normal"></highlight></codeline>
<codeline><highlight class="normal"><ref refid="cpp/container/vector" kindref="compound" external="/home/thuang295/Code/taskflow/doxygen/cppreference-doxygen-web.tag.xml">std::vector&lt;int&gt;</ref><sp/>data<sp/>=<sp/>{1,<sp/>4,<sp/>9,<sp/>2,<sp/>3,<sp/>11,<sp/>-8};</highlight></codeline>
<codeline><highlight class="normal"></highlight></codeline>
<codeline><highlight class="normal"><ref refid="classtf_1_1Task" kindref="compound">tf::Task</ref><sp/><ref refid="cpp/algorithm/sort" kindref="compound" external="/home/thuang295/Code/taskflow/doxygen/cppreference-doxygen-web.tag.xml">sort</ref><sp/>=<sp/>taskflow.<ref refid="classtf_1_1FlowBuilder_1a35e180eb63de6c9f28e43185e837a4fa" kindref="member">sort</ref>(data.begin(),<sp/>data.end());</highlight></codeline>
<codeline><highlight class="normal"></highlight></codeline>
<codeline><highlight class="normal">executor.<ref refid="classtf_1_1Executor_1a8d08f0cb79e7b3780087975d13368a96" kindref="member">run</ref>(taskflow).wait();</highlight></codeline>
<codeline><highlight class="normal"></highlight></codeline>
<codeline><highlight class="normal">assert(std::is_sorted(data.begin(),<sp/>data.end()));</highlight></codeline>
</programlisting></para>
<para><simplesect kind="attention"><para>Elements are compared using the operator <computeroutput>&lt;</computeroutput>.</para>
</simplesect>
</para>
</sect1>
<sect1 id="ParallelSort_1SortARangeOfItemsWithACustomComparator">
<title>Sort a Range of Items with a Custom Comparator</title>
<para><ref refid="classtf_1_1FlowBuilder_1a35e180eb63de6c9f28e43185e837a4fa" kindref="member">tf::Taskflow::sort(B first, E last, C cmp)</ref> is an overload of parallel sort that allows users to specify a custom comparator. The following example sorts a data vector in decreasing order.</para>
<para><programlisting filename=".cpp"><codeline><highlight class="normal"><ref refid="classtf_1_1Taskflow" kindref="compound">tf::Taskflow</ref><sp/>taskflow;</highlight></codeline>
<codeline><highlight class="normal"><ref refid="classtf_1_1Executor" kindref="compound">tf::Executor</ref><sp/>executor;</highlight></codeline>
<codeline><highlight class="normal"></highlight></codeline>
<codeline><highlight class="normal"><ref refid="cpp/container/vector" kindref="compound" external="/home/thuang295/Code/taskflow/doxygen/cppreference-doxygen-web.tag.xml">std::vector&lt;int&gt;</ref><sp/>data<sp/>=<sp/>{1,<sp/>4,<sp/>9,<sp/>2,<sp/>3,<sp/>11,<sp/>-8};</highlight></codeline>
<codeline><highlight class="normal"></highlight></codeline>
<codeline><highlight class="normal"><ref refid="classtf_1_1Task" kindref="compound">tf::Task</ref><sp/><ref refid="cpp/algorithm/sort" kindref="compound" external="/home/thuang295/Code/taskflow/doxygen/cppreference-doxygen-web.tag.xml">sort</ref><sp/>=<sp/>taskflow.<ref refid="classtf_1_1FlowBuilder_1a35e180eb63de6c9f28e43185e837a4fa" kindref="member">sort</ref>(data.begin(),<sp/>data.end(),<sp/></highlight></codeline>
<codeline><highlight class="normal"><sp/><sp/>[](</highlight><highlight class="keywordtype">int</highlight><highlight class="normal"><sp/>a,<sp/></highlight><highlight class="keywordtype">int</highlight><highlight class="normal"><sp/>b)<sp/>{<sp/>return<sp/>a<sp/>&gt;<sp/>b;<sp/>}</highlight></codeline>
<codeline><highlight class="normal">);</highlight></codeline>
<codeline><highlight class="normal"></highlight></codeline>
<codeline><highlight class="normal">executor.<ref refid="classtf_1_1Executor_1a8d08f0cb79e7b3780087975d13368a96" kindref="member">run</ref>(taskflow).wait();</highlight></codeline>
<codeline><highlight class="normal"></highlight></codeline>
<codeline><highlight class="normal">assert(std::is_sorted(data.begin(),<sp/>data.end(),<sp/><ref refid="cpp/utility/functional/greater" kindref="compound" external="/home/thuang295/Code/taskflow/doxygen/cppreference-doxygen-web.tag.xml">std::greater&lt;int&gt;</ref>{}));</highlight></codeline>
</programlisting></para>
<para><simplesect kind="attention"><para><ref refid="classtf_1_1FlowBuilder_1a35e180eb63de6c9f28e43185e837a4fa" kindref="member">tf::Taskflow::sort</ref> is not stable. That is, two or more objects with equal keys may not appear in the same order before sorting.</para>
</simplesect>
</para>
</sect1>
<sect1 id="ParallelSort_1ParallelSortEnableStatefulDataPassing">
<title>Enable Stateful Data Passing</title>
<para>The iterators taken by <ref refid="classtf_1_1FlowBuilder_1a35e180eb63de6c9f28e43185e837a4fa" kindref="member">tf::Taskflow::sort</ref> are templated. You can use <ref refid="cpp/utility/functional/reference_wrapper" kindref="compound" external="/home/thuang295/Code/taskflow/doxygen/cppreference-doxygen-web.tag.xml">std::reference_wrapper</ref> to enable stateful data passing between the sort task and others. The following example creates a task <computeroutput>init</computeroutput> to initialize the data vector and a task <computeroutput>sort</computeroutput> to sort the data in parallel after <computeroutput>init</computeroutput> finishes.</para>
<para><programlisting filename=".cpp"><codeline><highlight class="normal"><ref refid="classtf_1_1Taskflow" kindref="compound">tf::Taskflow</ref><sp/>taskflow;</highlight></codeline>
<codeline><highlight class="normal"><ref refid="classtf_1_1Executor" kindref="compound">tf::Executor</ref><sp/>executor;</highlight></codeline>
<codeline><highlight class="normal"></highlight></codeline>
<codeline><highlight class="normal"><ref refid="cpp/container/vector" kindref="compound" external="/home/thuang295/Code/taskflow/doxygen/cppreference-doxygen-web.tag.xml">std::vector&lt;int&gt;</ref><sp/>data;</highlight></codeline>
<codeline><highlight class="normal"><ref refid="cpp/container/vector" kindref="compound" external="/home/thuang295/Code/taskflow/doxygen/cppreference-doxygen-web.tag.xml">std::vector&lt;int&gt;::iterator</ref><sp/>first,<sp/>last;</highlight></codeline>
<codeline><highlight class="normal"></highlight></codeline>
<codeline><highlight class="normal"><ref refid="classtf_1_1Task" kindref="compound">tf::Task</ref><sp/>init<sp/>=<sp/>taskflow.<ref refid="classtf_1_1FlowBuilder_1a60d7a666cab71ecfa3010b2efb0d6b57" kindref="member">emplace</ref>([&amp;](){<sp/></highlight></codeline>
<codeline><highlight class="normal"><sp/><sp/>data<sp/><sp/>=<sp/>{1,<sp/>4,<sp/>9,<sp/>2,<sp/>3,<sp/>11,<sp/>-8};<sp/></highlight></codeline>
<codeline><highlight class="normal"><sp/><sp/>first<sp/>=<sp/>data.begin();</highlight></codeline>
<codeline><highlight class="normal"><sp/><sp/>last<sp/><sp/>=<sp/>data.end();</highlight></codeline>
<codeline><highlight class="normal">});</highlight></codeline>
<codeline><highlight class="normal"><ref refid="classtf_1_1Task" kindref="compound">tf::Task</ref><sp/><ref refid="cpp/algorithm/sort" kindref="compound" external="/home/thuang295/Code/taskflow/doxygen/cppreference-doxygen-web.tag.xml">sort</ref><sp/>=<sp/>taskflow.<ref refid="classtf_1_1FlowBuilder_1a35e180eb63de6c9f28e43185e837a4fa" kindref="member">sort</ref>(</highlight></codeline>
<codeline><highlight class="normal"><sp/><sp/>std::ref(first),<sp/>std::ref(last),<sp/>[]<sp/>(</highlight><highlight class="keywordtype">int</highlight><highlight class="normal"><sp/>l,<sp/></highlight><highlight class="keywordtype">int</highlight><highlight class="normal"><sp/>r)<sp/>{<sp/></highlight><highlight class="keywordflow">return</highlight><highlight class="normal"><sp/>l<sp/>&lt;<sp/>r;<sp/>}</highlight></codeline>
<codeline><highlight class="normal">);</highlight></codeline>
<codeline><highlight class="normal">init.<ref refid="classtf_1_1Task_1a8c78c453295a553c1c016e4062da8588" kindref="member">precede</ref>(sort);</highlight></codeline>
<codeline><highlight class="normal"></highlight></codeline>
<codeline><highlight class="normal">executor.<ref refid="classtf_1_1Executor_1a8d08f0cb79e7b3780087975d13368a96" kindref="member">run</ref>(taskflow).wait();</highlight></codeline>
<codeline><highlight class="normal"></highlight></codeline>
<codeline><highlight class="normal">assert(std::is_sorted(data.begin(),<sp/>data.end()));</highlight></codeline>
</programlisting> </para>
</sect1>
    </detaileddescription>
    <location file="doxygen/algorithms/sort.dox"/>
  </compounddef>
</doxygen>
