<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>
<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><refrefid="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><refrefid="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><refrefid="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><refrefid="classtf_1_1FlowBuilder_1a6be5d7f053a868647c3b9e0d9cdf6b68"kindref="member">tf::Taskflow::max_element(B first, E last, T& result, C comp, P part)</ref></para>
<title>Create a Parallel Find-If Task</title><para><refrefid="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>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>
<title>Capture Iterators by Reference</title><para>You can pass iterators by reference using <ulinkurl="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>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>
<title>Create a Parallel Find-If-Not Task</title><para><refrefid="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>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>Similar to <refrefid="ParallelFind_1ParallelFindCaptureIteratorsByReference"kindref="member">Capture Iterators by Reference</ref>, iterators of <refrefid="classtf_1_1FlowBuilder_1a95fa2719fa7bbe7d171cf474ddb06726"kindref="member">tf::Taskflow::find_if_not</ref> are templated to allow passing iterators by reference using <ulinkurl="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>
<title>Find the Smallest and the Largest Elements</title><para><refrefid="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>Similarly, <refrefid="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><simplesectkind="attention"><para>When using <refrefid="classtf_1_1FlowBuilder_1a6be5d7f053a868647c3b9e0d9cdf6b68"kindref="member">tf::Taskflow::max_element</ref> to find the large element, we will still need to use <refrefid="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 <ulinkurl="https://en.cppreference.com/w/cpp/algorithm/max_element">std::max_element</ulink>.</para>
<title>Configure a Partitioner</title><para>You can configure a partitioner for parallel-find tasks (<refrefid="classtf_1_1FlowBuilder_1a46a96f5889e6ac87b1ff8d6313b5f471"kindref="member">tf::Taskflow::find_if</ref>, <refrefid="classtf_1_1FlowBuilder_1a95fa2719fa7bbe7d171cf474ddb06726"kindref="member">tf::Taskflow::find_if_not</ref>, <refrefid="classtf_1_1FlowBuilder_1a6bf43eeaa81900084a472be1d36d46a6"kindref="member">tf::Taskflow::min_element</ref>, <refrefid="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><simplesectkind="attention"><para>By default, parallel-find tasks use <refrefid="namespacetf_1ace2c5adcd5039483eebb6dbdbb6f33e3"kindref="member">tf::DefaultPartitioner</ref> if no partitioner is specified. </para>