Taskflow provides template functions for constructing tasks to perform parallel iterations over ranges of items.
You need to include the header file, taskflow/algorithm/find.hpp, for using parallel-find algorithms.
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 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:
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:
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.
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.
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:
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:
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.
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:
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:
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: