ParallelFind Parallel Find Include the Header ParallelFind_1ParallelFindIncludeTheHeader What is a Find Algorithm? ParallelFind_1WhatIsAFindAlgorithm Create a Parallel Find-If Task ParallelFind_1CreateAParallelFindIfTask Capture Iterators by Reference ParallelFind_1ParallelFindCaptureIteratorsByReference Create a Parallel Find-If-Not Task ParallelFind_1CreateAParallelFindIfNotTask Find the Smallest and the Largest Elements ParallelFind_1ParallelFindMinMaxElement Configure a Partitioner ParallelFind_1ParallelFindConfigureAPartitioner Taskflow provides template functions for constructing tasks to perform parallel iterations over ranges of items. Include the Header You need to include the header file, taskflow/algorithm/find.hpp, for using parallel-find algorithms. #include<taskflow/algorithm/find.hpp> What is a Find Algorithm? A find algorithm allows you to find an element in a range [first, last) that satisfies a specific criteria. The algorithm returns an iterator to the first found element in the range or returns last if there is no such iterator. Taskflow provides the following parallel-find algorithms: tf::Taskflow::find_if(B first, E last, T& result, UOP predicate, P&& part) tf::Taskflow::find_if_not(B first, E last, T& result, UOP predicate, P&& part) tf::Taskflow::min_element(B first, E last, T& result, C comp, P&& part) tf::Taskflow::max_element(B first, E last, T& result, C comp, P&& part) Create a Parallel Find-If Task tf::Taskflow::find_if performs parallel iterations to find the first element in the range [first, last) that makes the given predicate return true. It resembles a parallel implementation of the following loop: template<typenameInputIt,typenameUnaryPredicate> InputItfind_if(InputItfirst,InputItlast,UnaryPredicatepredicate){ for(;first!=last;++first){ if(predicate(*first)){ returnfirst; } } returnlast; } 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: std::vector<int>input={1,9,22,3,-6,13,12,0,9,11}; std::vector<int>::iteratorresult; taskflow.find_if( input.begin(),input.end(),[](inti){returni==22;},result ); executor.run(taskflow); assert(*result==22); Capture Iterators by Reference You can pass iterators by reference using std::ref 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. std::vector<int>input; std::vector<int>::iteratorresult,first,last; //tasktosetuptherangeiterators tf::Taskinit=taskflow.emplace([&](){ input={1,9,22,3,-6,13,12,0,9,11}; first=input.begin(), last=input.end(); }); //tasktoperformparallelfind tf::Tasktask=taskflow.find_if( std::ref(first),std::ref(last),result,[](inti){returni==22;} ); init.precede(task); executor.run(taskflow); assert(*result==22); In the above example, when init finishes, input has been initialized to 10 elements with first and last pointing to the data range of input. The find-if task will then work on this initialized range as a result of passing iterators by reference. Create a Parallel Find-If-Not Task tf::Taskflow::find_if_not performs parallel iterations to find the first element in the range [first, last) that makes the given predicate return false. It resembles a parallel implementation of the following loop: template<typenameInputIt,typenameUnaryPredicate> InputItfind_if(InputItfirst,InputItlast,UnaryPredicatepredicate){ for(;first!=last;++first){ if(!predicate(*first)){ returnfirst; } } returnlast; } The example below creates a task to find the element that is NOT equal to 22 from an input range of 10 elements. The result will be stored in the forth argument passed by reference: std::vector<int>input={1,1,22,1,1,1,1,1,1,1}; std::vector<int>::iteratorresult; taskflow.find_if_not( input.begin(),input.end(),result,[](inti){returni==1;} ); executor.run(taskflow); assert(*result==22); Similar to Capture Iterators by Reference, iterators of tf::Taskflow::find_if_not are templated to allow passing iterators by reference using std::ref. 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. Find the Smallest and the Largest Elements tf::Taskflow::min_element finds the smallest element in a range [first, last) 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 result: std::vector<int>input={1,1,1,1,1,-1,1,1,1,1}; std::vector<int>::iteratorresult; taskflow.min_element( input.begin(),input.end(),std::less<int>(),result ); executor.run(taskflow).wait(); assert(*result==-1); Similarly, tf::Taskflow::max_element finds the largest element in a range [first, last) 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 result: std::vector<int>input={1,1,1,1,1,2,1,1,1,1}; std::vector<int>::iteratorresult; taskflow.max_element( input.begin(),input.end(),std::less<int>(),result ); executor.run(taskflow).wait(); assert(*result==2); When using tf::Taskflow::max_element to find the large element, we will still need to use std::less as our comparison function. Details can be referred to std::max_element. Configure a Partitioner You can configure a partitioner for parallel-find tasks (tf::Taskflow::find_if, tf::Taskflow::find_if_not, tf::Taskflow::min_element, tf::Taskflow::max_element) 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: std::vector<int>vec(1024,-1); std::vector<int>::iteratorresult; tf::ExecutionPolicy<tf::StaticPartitioner>static_partitioner; tf::ExecutionPolicy<tf::GuidedPartitioner>guided_partitioner; //createaparallel-findtaskwithastaticpartitioner taskflow.find_if( vec.begin(),vec.end(),result,[&](inti){returni==-1;},static_partitioner ); //createaparallel-findtaskwithaguidedpartitioner taskflow.find_if( vec.begin(),vec.end(),result,[&](inti){returni==-1;},guided_partitioner ); By default, parallel-find tasks use tf::DefaultPartitioner if no partitioner is specified.