![]() |
Taskflow
3.2.0-Master-Branch
|
Taskflow provides template functions for constructing tasks to perform parallel transforms over ranges of items.
You need to include the header file, taskflow/algorithm/transform.hpp
, for creating a parallel-transform task.
Parallel-transform transforms a range of items, possibly with a different type for the transformed data, and stores the result in another range. The task created by tf::Taskflow::transform(B first1, E last1, O d_first, C c) is equivalent to a parallel execution of the following loop:
By default, tf::Taskflow::transform(B first1, E last1, O d_first, C c) creates a task to spawn a subflow (see Dynamic Tasking) that simultaneously applies the callable c
to the object obtained by dereferencing every iterator in the range [first1, last1)
and stores the result in another range beginning at d_first
. It is user's responsibility for ensuring the range is valid within the execution of the parallel-transform task. Taskflow's parallel-transform tasks work on all iterable STL containers.
You can enable stateful iterators by creating a reference wrapper and pass the wrapped iterator to the argument of tf::Taskflow::transform. This is especially useful when the range is not known at the time of creating a parallel-transform task, but through another task.
When init
finishes, the parallel-transform task transform
will see first
pointing to the beginning of src
and last
pointing to the end of src
. Then, it simultaneously transforms these 1000 items by adding one to each element and stores the result in another range starting at d_first
.
You can use the overload, tf::Taskflow::transform(B1 first1, E1 last1, B2 first2, O d_first, C c), to perform parallel transforms on two source ranges pointed by first1
and first2
using the binary operator c
and store the result in another range pointed by d_first
. This method is equivalent to the parallel execution of the following loop:
The following example creates a parallel-transform task that adds two ranges of elements one by one and stores the result in a target range: