同步操作将从 Gitee 极速下载/cpp-taskflow 强制同步,此操作会覆盖自 Fork 仓库以来所做的任何修改,且无法恢复!!!
确定后同步将在后台操作,完成时将刷新页面,请耐心等待。
<?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.12.0" xml:lang="en-US"><compounddef id="ParallelFind" kind="page"><compoundname>ParallelFind</compoundname><title>Parallel Find</title><tableofcontents><tocsect><name>Include the Header</name><reference>ParallelFind_1ParallelFindIncludeTheHeader</reference></tocsect><tocsect><name>What is a Find Algorithm?</name><reference>ParallelFind_1WhatIsAFindAlgorithm</reference></tocsect><tocsect><name>Create a Parallel Find-If Task</name><reference>ParallelFind_1CreateAParallelFindIfTask</reference></tocsect><tocsect><name>Capture Iterators by Reference</name><reference>ParallelFind_1ParallelFindCaptureIteratorsByReference</reference></tocsect><tocsect><name>Create a Parallel Find-If-Not Task</name><reference>ParallelFind_1CreateAParallelFindIfNotTask</reference></tocsect><tocsect><name>Find the Smallest and the Largest Elements</name><reference>ParallelFind_1ParallelFindMinMaxElement</reference></tocsect><tocsect><name>Configure a Partitioner</name><reference>ParallelFind_1ParallelFindConfigureAPartitioner</reference></tocsect></tableofcontents><briefdescription></briefdescription><detaileddescription><para>Taskflow provides template functions for constructing tasks to perform parallel iterations over ranges of items.</para><sect1 id="ParallelFind_1ParallelFindIncludeTheHeader"><title>Include the Header</title><para>You need to include the header file, <computeroutput>taskflow/algorithm/find.hpp</computeroutput>, for using parallel-find algorithms.</para><para><programlisting filename=".cpp"><codeline><highlight class="preprocessor">#include<sp/><taskflow/algorithm/find.hpp></highlight></codeline></programlisting></para></sect1><sect1 id="ParallelFind_1WhatIsAFindAlgorithm"><title>What is a Find Algorithm?</title><para>A find algorithm allows you to find an element in a range <computeroutput>[first, last)</computeroutput> that satisfies a specific criteria. The algorithm returns an iterator to the first found element in the range or returns <computeroutput>last</computeroutput> if there is no such iterator. Taskflow provides the following parallel-find algorithms:</para><para><itemizedlist><listitem><para><ref refid="classtf_1_1FlowBuilder_1a46a96f5889e6ac87b1ff8d6313b5f471" kindref="member">tf::Taskflow::find_if(B first, E last, T& result, UOP predicate, P part)</ref></para></listitem><listitem><para><ref refid="classtf_1_1FlowBuilder_1a95fa2719fa7bbe7d171cf474ddb06726" kindref="member">tf::Taskflow::find_if_not(B first, E last, T& result, UOP predicate, P part)</ref></para></listitem><listitem><para><ref refid="classtf_1_1FlowBuilder_1a6bf43eeaa81900084a472be1d36d46a6" kindref="member">tf::Taskflow::min_element(B first, E last, T& result, C comp, P part)</ref></para></listitem><listitem><para><ref refid="classtf_1_1FlowBuilder_1a6be5d7f053a868647c3b9e0d9cdf6b68" kindref="member">tf::Taskflow::max_element(B first, E last, T& result, C comp, P part)</ref></para></listitem></itemizedlist></para></sect1><sect1 id="ParallelFind_1CreateAParallelFindIfTask"><title>Create a Parallel Find-If Task</title><para><ref refid="classtf_1_1FlowBuilder_1a46a96f5889e6ac87b1ff8d6313b5f471" kindref="member">tf::Taskflow::find_if</ref> performs parallel iterations to find the first element in the range <computeroutput>[first, last)</computeroutput> that makes the given predicate return <computeroutput>true</computeroutput>. It resembles a parallel implementation of the following loop:</para><para><programlisting filename=".cpp"><codeline><highlight class="keyword">template</highlight><highlight class="normal"><</highlight><highlight class="keyword">typename</highlight><highlight class="normal"><sp/>InputIt,<sp/></highlight><highlight class="keyword">typename</highlight><highlight class="normal"><sp/>UnaryPredicate></highlight></codeline><codeline><highlight class="normal">InputIt<sp/>find_if(InputIt<sp/>first,<sp/>InputIt<sp/>last,<sp/>UnaryPredicate<sp/>predicate)<sp/>{</highlight></codeline><codeline><highlight class="normal"><sp/><sp/></highlight><highlight class="keywordflow">for</highlight><highlight class="normal">(;<sp/>first<sp/>!=<sp/>last;<sp/>++first)<sp/>{</highlight></codeline><codeline><highlight class="normal"><sp/><sp/><sp/><sp/></highlight><highlight class="keywordflow">if</highlight><highlight class="normal">(predicate(*first))<sp/>{</highlight></codeline><codeline><highlight class="normal"><sp/><sp/><sp/><sp/><sp/><sp/></highlight><highlight class="keywordflow">return</highlight><highlight class="normal"><sp/>first;</highlight></codeline><codeline><highlight class="normal"><sp/><sp/><sp/><sp/>}</highlight></codeline><codeline><highlight class="normal"><sp/><sp/>}</highlight></codeline><codeline><highlight class="normal"><sp/><sp/></highlight><highlight class="keywordflow">return</highlight><highlight class="normal"><sp/>last;</highlight></codeline><codeline><highlight class="normal">}</highlight></codeline></programlisting></para><para>The example below creates a task to find the element that is equal to 22 from an input range of 10 elements. The result will be stored in the forth argument passed by reference:</para><para><programlisting filename=".cpp"><codeline><highlight class="normal"><ref refid="cpp/container/vector" kindref="compound" external="/Users/twhuang/Code/taskflow/doxygen/cppreference-doxygen-web.tag.xml">std::vector<int></ref><sp/>input<sp/>=<sp/>{1,<sp/>9,<sp/>22,<sp/>3,<sp/>-6,<sp/>13,<sp/>12,<sp/>0,<sp/>9,<sp/>11};</highlight></codeline><codeline><highlight class="normal"><ref refid="cpp/container/vector" kindref="compound" external="/Users/twhuang/Code/taskflow/doxygen/cppreference-doxygen-web.tag.xml">std::vector<int>::iterator</ref><sp/>result;</highlight></codeline><codeline><highlight class="normal">taskflow.find_if(</highlight></codeline><codeline><highlight class="normal"><sp/><sp/>input.begin(),<sp/>input.end(),<sp/>[](</highlight><highlight class="keywordtype">int</highlight><highlight class="normal"><sp/>i){<sp/>return<sp/>i<sp/>==<sp/>22;<sp/>},<sp/>result</highlight></codeline><codeline><highlight class="normal">);</highlight></codeline><codeline><highlight class="normal">executor.run(taskflow);</highlight></codeline><codeline><highlight class="normal">assert(*result<sp/>==<sp/>22);</highlight></codeline></programlisting></para></sect1><sect1 id="ParallelFind_1ParallelFindCaptureIteratorsByReference"><title>Capture Iterators by Reference</title><para>You can pass iterators by reference using <ulink url="https://en.cppreference.com/w/cpp/utility/functional/ref">std::ref</ulink> to marshal parameters update between dependent tasks. This is especially useful when the range iterators are not known at the time of creating a find-if task, but need initialization from another task.</para><para><programlisting filename=".cpp"><codeline><highlight class="normal"><ref refid="cpp/container/vector" kindref="compound" external="/Users/twhuang/Code/taskflow/doxygen/cppreference-doxygen-web.tag.xml">std::vector<int></ref><sp/>input;</highlight></codeline><codeline><highlight class="normal"><ref refid="cpp/container/vector" kindref="compound" external="/Users/twhuang/Code/taskflow/doxygen/cppreference-doxygen-web.tag.xml">std::vector<int>::iterator</ref><sp/>result,<sp/>first,<sp/>last;</highlight></codeline><codeline><highlight class="normal"></highlight></codeline><codeline><highlight class="normal"></highlight><highlight class="comment">//<sp/>task<sp/>to<sp/>set<sp/>up<sp/>the<sp/>range<sp/>iterators</highlight><highlight class="normal"></highlight></codeline><codeline><highlight class="normal"><ref refid="classtf_1_1Task" kindref="compound">tf::Task</ref><sp/>init<sp/>=<sp/>taskflow.emplace([&](){</highlight></codeline><codeline><highlight class="normal"><sp/><sp/>input<sp/>=<sp/>{1,<sp/>9,<sp/>22,<sp/>3,<sp/>-6,<sp/>13,<sp/>12,<sp/>0,<sp/>9,<sp/>11};</highlight></codeline><codeline><highlight class="normal"><sp/><sp/>first<sp/>=<sp/>input.begin(),</highlight></codeline><codeline><highlight class="normal"><sp/><sp/>last<sp/><sp/>=<sp/>input.end();</highlight></codeline><codeline><highlight class="normal">});</highlight></codeline><codeline><highlight class="normal"></highlight></codeline><codeline><highlight class="normal"></highlight><highlight class="comment">//<sp/>task<sp/>to<sp/>perform<sp/>parallel<sp/>find</highlight><highlight class="normal"></highlight></codeline><codeline><highlight class="normal"><ref refid="classtf_1_1Task" kindref="compound">tf::Task</ref><sp/>task<sp/>=<sp/>taskflow.find_if(</highlight></codeline><codeline><highlight class="normal"><sp/><sp/><ref refid="cpp/utility/functional/ref" kindref="compound" external="/Users/twhuang/Code/taskflow/doxygen/cppreference-doxygen-web.tag.xml">std::ref</ref>(first),<sp/><ref refid="cpp/utility/functional/ref" kindref="compound" external="/Users/twhuang/Code/taskflow/doxygen/cppreference-doxygen-web.tag.xml">std::ref</ref>(last),<sp/>result,<sp/>[](</highlight><highlight class="keywordtype">int</highlight><highlight class="normal"><sp/>i){<sp/></highlight><highlight class="keywordflow">return</highlight><highlight class="normal"><sp/>i<sp/>==<sp/>22;<sp/>}</highlight></codeline><codeline><highlight class="normal">);</highlight></codeline><codeline><highlight class="normal"></highlight></codeline><codeline><highlight class="normal">init.<ref refid="classtf_1_1Task_1a8c78c453295a553c1c016e4062da8588" kindref="member">precede</ref>(task);</highlight></codeline><codeline><highlight class="normal"></highlight></codeline><codeline><highlight class="normal">executor.run(taskflow);</highlight></codeline><codeline><highlight class="normal">assert(*result<sp/>==<sp/>22);</highlight></codeline></programlisting></para><para>In the above example, when <computeroutput>init</computeroutput> finishes, <computeroutput>input</computeroutput> has been initialized to 10 elements with <computeroutput>first</computeroutput> and <computeroutput>last</computeroutput> pointing to the data range of <computeroutput>input</computeroutput>. The find-if task will then work on this initialized range as a result of passing iterators by reference.</para></sect1><sect1 id="ParallelFind_1CreateAParallelFindIfNotTask"><title>Create a Parallel Find-If-Not Task</title><para><ref refid="classtf_1_1FlowBuilder_1a95fa2719fa7bbe7d171cf474ddb06726" kindref="member">tf::Taskflow::find_if_not</ref> performs parallel iterations to find the first element in the range <computeroutput>[first, last)</computeroutput> that makes the given predicate return <computeroutput>false</computeroutput>. It resembles a parallel implementation of the following loop:</para><para><programlisting filename=".cpp"><codeline><highlight class="keyword">template</highlight><highlight class="normal"><</highlight><highlight class="keyword">typename</highlight><highlight class="normal"><sp/>InputIt,<sp/></highlight><highlight class="keyword">typename</highlight><highlight class="normal"><sp/>UnaryPredicate></highlight></codeline><codeline><highlight class="normal">InputIt<sp/>find_if(InputIt<sp/>first,<sp/>InputIt<sp/>last,<sp/>UnaryPredicate<sp/>predicate)<sp/>{</highlight></codeline><codeline><highlight class="normal"><sp/><sp/></highlight><highlight class="keywordflow">for</highlight><highlight class="normal">(;<sp/>first<sp/>!=<sp/>last;<sp/>++first)<sp/>{</highlight></codeline><codeline><highlight class="normal"><sp/><sp/><sp/><sp/></highlight><highlight class="keywordflow">if</highlight><highlight class="normal">(!predicate(*first))<sp/>{</highlight></codeline><codeline><highlight class="normal"><sp/><sp/><sp/><sp/><sp/><sp/></highlight><highlight class="keywordflow">return</highlight><highlight class="normal"><sp/>first;</highlight></codeline><codeline><highlight class="normal"><sp/><sp/><sp/><sp/>}</highlight></codeline><codeline><highlight class="normal"><sp/><sp/>}</highlight></codeline><codeline><highlight class="normal"><sp/><sp/></highlight><highlight class="keywordflow">return</highlight><highlight class="normal"><sp/>last;</highlight></codeline><codeline><highlight class="normal">}</highlight></codeline></programlisting></para><para>The example below creates a task to find the element that is <emphasis>NOT</emphasis> equal to 22 from an input range of 10 elements. The result will be stored in the forth argument passed by reference:</para><para><programlisting filename=".cpp"><codeline><highlight class="normal"><ref refid="cpp/container/vector" kindref="compound" external="/Users/twhuang/Code/taskflow/doxygen/cppreference-doxygen-web.tag.xml">std::vector<int></ref><sp/>input<sp/>=<sp/>{1,<sp/>1,<sp/>22,<sp/>1,<sp/>1,<sp/>1,<sp/>1,<sp/>1,<sp/>1,<sp/>1};</highlight></codeline><codeline><highlight class="normal"><ref refid="cpp/container/vector" kindref="compound" external="/Users/twhuang/Code/taskflow/doxygen/cppreference-doxygen-web.tag.xml">std::vector<int>::iterator</ref><sp/>result;</highlight></codeline><codeline><highlight class="normal">taskflow.find_if_not(</highlight></codeline><codeline><highlight class="normal"><sp/><sp/>input.begin(),<sp/>input.end(),<sp/>result,<sp/>[](</highlight><highlight class="keywordtype">int</highlight><highlight class="normal"><sp/>i){<sp/>return<sp/>i<sp/>==<sp/>1;<sp/>}</highlight></codeline><codeline><highlight class="normal">);</highlight></codeline><codeline><highlight class="normal">executor.run(taskflow);</highlight></codeline><codeline><highlight class="normal">assert(*result<sp/>==<sp/>22);</highlight></codeline></programlisting></para><para>Similar to <ref refid="ParallelFind_1ParallelFindCaptureIteratorsByReference" kindref="member">Capture Iterators by Reference</ref>, iterators of <ref refid="classtf_1_1FlowBuilder_1a95fa2719fa7bbe7d171cf474ddb06726" kindref="member">tf::Taskflow::find_if_not</ref> are templated to allow passing iterators by reference using <ulink url="https://en.cppreference.com/w/cpp/utility/functional/ref">std::ref</ulink>. This is especially useful when the range iterators are not known at the time of creating a find-if-not task, but need initialization from another task.</para></sect1><sect1 id="ParallelFind_1ParallelFindMinMaxElement"><title>Find the Smallest and the Largest Elements</title><para><ref refid="classtf_1_1FlowBuilder_1a6bf43eeaa81900084a472be1d36d46a6" kindref="member">tf::Taskflow::min_element</ref> finds the smallest element in a range <computeroutput>[first, last)</computeroutput> using the given comparison function object. The example below finds the smallest element, i.e., -1, from an input range of 10 elements and stores the iterator to that smallest element in <computeroutput>result:</computeroutput> </para><para><programlisting filename=".cpp"><codeline><highlight class="normal"><ref refid="cpp/container/vector" kindref="compound" external="/Users/twhuang/Code/taskflow/doxygen/cppreference-doxygen-web.tag.xml">std::vector<int></ref><sp/>input<sp/>=<sp/>{1,<sp/>1,<sp/>1,<sp/>1,<sp/>1,<sp/>-1,<sp/>1,<sp/>1,<sp/>1,<sp/>1};</highlight></codeline><codeline><highlight class="normal"><ref refid="cpp/container/vector" kindref="compound" external="/Users/twhuang/Code/taskflow/doxygen/cppreference-doxygen-web.tag.xml">std::vector<int>::iterator</ref><sp/>result;</highlight></codeline><codeline><highlight class="normal">taskflow.min_element(</highlight></codeline><codeline><highlight class="normal"><sp/><sp/>input.begin(),<sp/>input.end(),<sp/><ref refid="cpp/utility/functional/less" kindref="compound" external="/Users/twhuang/Code/taskflow/doxygen/cppreference-doxygen-web.tag.xml">std::less<int></ref>(),<sp/>result</highlight></codeline><codeline><highlight class="normal">);</highlight></codeline><codeline><highlight class="normal">executor.run(taskflow).wait();</highlight></codeline><codeline><highlight class="normal">assert(*result<sp/>==<sp/>-1);</highlight></codeline></programlisting></para><para>Similarly, <ref refid="classtf_1_1FlowBuilder_1a6be5d7f053a868647c3b9e0d9cdf6b68" kindref="member">tf::Taskflow::max_element</ref> finds the largest element in a range <computeroutput>[first, last)</computeroutput> using the given comparison function object. The example below finds the largest element, i.e., 2, from an input range of 10 elements and stores the iterator to that largest element in <computeroutput>result:</computeroutput> </para><para><programlisting filename=".cpp"><codeline><highlight class="normal"><ref refid="cpp/container/vector" kindref="compound" external="/Users/twhuang/Code/taskflow/doxygen/cppreference-doxygen-web.tag.xml">std::vector<int></ref><sp/>input<sp/>=<sp/>{1,<sp/>1,<sp/>1,<sp/>1,<sp/>1,<sp/>2,<sp/>1,<sp/>1,<sp/>1,<sp/>1};</highlight></codeline><codeline><highlight class="normal"><ref refid="cpp/container/vector" kindref="compound" external="/Users/twhuang/Code/taskflow/doxygen/cppreference-doxygen-web.tag.xml">std::vector<int>::iterator</ref><sp/>result;</highlight></codeline><codeline><highlight class="normal">taskflow.max_element(</highlight></codeline><codeline><highlight class="normal"><sp/><sp/>input.begin(),<sp/>input.end(),<sp/><ref refid="cpp/utility/functional/less" kindref="compound" external="/Users/twhuang/Code/taskflow/doxygen/cppreference-doxygen-web.tag.xml">std::less<int></ref>(),<sp/>result</highlight></codeline><codeline><highlight class="normal">);</highlight></codeline><codeline><highlight class="normal">executor.run(taskflow).wait();</highlight></codeline><codeline><highlight class="normal">assert(*result<sp/>==<sp/>2);</highlight></codeline></programlisting></para><para><simplesect kind="attention"><para>When using <ref refid="classtf_1_1FlowBuilder_1a6be5d7f053a868647c3b9e0d9cdf6b68" kindref="member">tf::Taskflow::max_element</ref> to find the large element, we will still need to use <ref refid="cpp/utility/functional/less" kindref="compound" external="/Users/twhuang/Code/taskflow/doxygen/cppreference-doxygen-web.tag.xml">std::less</ref> as our comparison function. Details can be referred to <ulink url="https://en.cppreference.com/w/cpp/algorithm/max_element">std::max_element</ulink>.</para></simplesect></para></sect1><sect1 id="ParallelFind_1ParallelFindConfigureAPartitioner"><title>Configure a Partitioner</title><para>You can configure a partitioner for parallel-find tasks (<ref refid="classtf_1_1FlowBuilder_1a46a96f5889e6ac87b1ff8d6313b5f471" kindref="member">tf::Taskflow::find_if</ref>, <ref refid="classtf_1_1FlowBuilder_1a95fa2719fa7bbe7d171cf474ddb06726" kindref="member">tf::Taskflow::find_if_not</ref>, <ref refid="classtf_1_1FlowBuilder_1a6bf43eeaa81900084a472be1d36d46a6" kindref="member">tf::Taskflow::min_element</ref>, <ref refid="classtf_1_1FlowBuilder_1a6be5d7f053a868647c3b9e0d9cdf6b68" kindref="member">tf::Taskflow::max_element</ref>) to run with different scheduling methods, such as guided partitioning, dynamic partitioning, and static partitioning. The following example creates two parallel-find tasks using two different partitioners, one with the static partitioning algorithm and another one with the guided partitioning algorithm:</para><para><programlisting filename=".cpp"><codeline><highlight class="normal"><ref refid="cpp/container/vector" kindref="compound" external="/Users/twhuang/Code/taskflow/doxygen/cppreference-doxygen-web.tag.xml">std::vector<int></ref><sp/>vec(1024,<sp/>-1);</highlight></codeline><codeline><highlight class="normal"><ref refid="cpp/container/vector" kindref="compound" external="/Users/twhuang/Code/taskflow/doxygen/cppreference-doxygen-web.tag.xml">std::vector<int>::iterator</ref><sp/>result;</highlight></codeline><codeline><highlight class="normal"></highlight></codeline><codeline><highlight class="normal"></highlight><highlight class="comment">//<sp/>create<sp/>two<sp/>partitioners<sp/>with<sp/>a<sp/>chunk<sp/>size<sp/>of<sp/>10</highlight><highlight class="normal"></highlight></codeline><codeline><highlight class="normal"><ref refid="classtf_1_1StaticPartitioner" kindref="compound">tf::StaticPartitioner</ref><sp/>static_partitioner(10);</highlight></codeline><codeline><highlight class="normal"><ref refid="classtf_1_1GuidedPartitioner" kindref="compound">tf::GuidedPartitioner</ref><sp/>guided_partitioner(10);</highlight></codeline><codeline><highlight class="normal"></highlight></codeline><codeline><highlight class="normal"></highlight><highlight class="comment">//<sp/>create<sp/>a<sp/>parallel-find<sp/>task<sp/>with<sp/>a<sp/>static<sp/>partitioner</highlight><highlight class="normal"></highlight></codeline><codeline><highlight class="normal">taskflow.find_if(</highlight></codeline><codeline><highlight class="normal"><sp/><sp/>vec.begin(),<sp/>vec.end(),<sp/>result,<sp/>[&](</highlight><highlight class="keywordtype">int</highlight><highlight class="normal"><sp/>i)<sp/>{<sp/>return<sp/>i<sp/>==<sp/>-1;<sp/>},<sp/>static_partitioner</highlight></codeline><codeline><highlight class="normal">);</highlight></codeline><codeline><highlight class="normal"></highlight></codeline><codeline><highlight class="normal"></highlight><highlight class="comment">//<sp/>create<sp/>a<sp/>parallel-find<sp/>task<sp/>with<sp/>a<sp/>guided<sp/>partitioner</highlight><highlight class="normal"></highlight></codeline><codeline><highlight class="normal">taskflow.find_if(</highlight></codeline><codeline><highlight class="normal"><sp/><sp/>vec.begin(),<sp/>vec.end(),<sp/>result,<sp/>[&](</highlight><highlight class="keywordtype">int</highlight><highlight class="normal"><sp/>i)<sp/>{<sp/>return<sp/>i<sp/>==<sp/>-1;<sp/>},<sp/>guided_partitioner</highlight></codeline><codeline><highlight class="normal">);</highlight></codeline></programlisting></para><para><simplesect kind="attention"><para>By default, parallel-find tasks use <ref refid="namespacetf_1ace2c5adcd5039483eebb6dbdbb6f33e3" kindref="member">tf::DefaultPartitioner</ref> if no partitioner is specified. </para></simplesect></para></sect1></detaileddescription><location file="doxygen/algorithms/find.dox"/></compounddef></doxygen>
此处可能存在不合适展示的内容,页面不予展示。您可通过相关编辑功能自查并修改。
如您确认内容无涉及 不当用语 / 纯广告导流 / 暴力 / 低俗色情 / 侵权 / 盗版 / 虚假 / 无价值内容或违法国家有关法律法规的内容,可点击提交进行申诉,我们将尽快为您处理。