1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166
|
namespace tf {
/** @page ParallelTransforms Parallel Transforms
%Taskflow provides template functions for constructing tasks to
perform parallel transforms over ranges of items.
@tableofcontents
@section ParallelTransformsInclude Include the Header
You need to include the header file, <tt>taskflow/algorithm/transform.hpp</tt>,
for creating a parallel-transform task.
@code{.cpp}
#include <taskflow/algorithm/transform.hpp>
@endcode
@section ParallelTransformsOverARange Create a Unary 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, P&& part)
is equivalent to a parallel execution of the following loop:
@code{.cpp}
while (first1 != last1) {
*d_first++ = c(*first1++);
}
@endcode
tf::Taskflow::transform
simultaneously applies the callable @c c to the object obtained by dereferencing
every iterator in the range <tt>[first1, last1)</tt> and stores
the result in another range beginning at @c d_first.
It is user's responsibility for ensuring the range is valid
within the execution of the parallel-transform task.
@code{.cpp}
std::vector<int> src = {1, 2, 3, 4, 5};
std::vector<int> tgt(src.size());
taskflow.transform(src.begin(), src.end(), tgt.begin(), [](int i){
std::cout << "transforming item " << i << " to " << i + 1 << '\n';
return i + 1;
});
@endcode
@section ParallelTransformsCaptureIteratorsByReference Capture Iterators by Reference
You can pass iterators by reference using @std_ref
to marshal parameter update between dependent tasks.
This is especially useful when the range is unknown
at the time of creating a parallel-transform task,
but needs initialization from another task.
@code{.cpp}
std::vector<int> src, tgt;
std::vector<int>::iterator first, last, d_first;
tf::Task init = taskflow.emplace([&](){
src.resize(1000);
tgt.resize(1000);
first = src.begin();
last = src.end();
d_first = tgt.begin();
});
tf::Task transform = taskflow.transform(
std::ref(first), std::ref(last), std::ref(d_first),
[&](int i) {
std::cout << "transforming item " << i << " to " << i + 1 << '\n';
return i+1;
}
);
init.precede(transform);
@endcode
When @c init finishes, the parallel-transform task @c transform
will see @c first pointing to the beginning of @c src and
@c last pointing to the end of @c src.
Then, it simultaneously transforms these 1000 items
by adding one to each element and stores the result
in another range starting at @c d_first.
@section ParallelBinaryTransformsOverARange Create a Binary Parallel-Transform Task
You can use the overload,
tf::Taskflow::transform(B1 first1, E1 last1, B2 first2, O d_first, C c, P&& part),
to perform parallel transforms on two source ranges pointed by
@c first1 and @c first2 using the binary
operator @c c
and store the result in another range pointed by @c d_first.
This method is equivalent to the parallel execution of the following loop:
@code{.cpp}
while (first1 != last1) {
*d_first++ = c(*first1++, *first2++);
}
@endcode
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:
@code{.cpp}
std::vector<int> src1 = {1, 2, 3, 4, 5};
std::vector<int> src2 = {5, 4, 3, 2, 1};
std::vector<int> tgt(src1.size());
taskflow.transform(
src1.begin(), src1.end(), src2.begin(), tgt.begin(),
[](int i, int j){
return i + j;
}
);
@endcode
@section ParallelTransformsCfigureAPartitioner Configure a Partitioner
You can configure a partitioner for parallel-transform tasks to run with different
scheduling methods, such as guided partitioning, dynamic partitioning, and static partitioning.
The following example creates two parallel-transform tasks using two different
partitioners, one with the static partitioning algorithm and
another one with the guided partitioning algorithm:
@code{.cpp}
tf::StaticPartitioner static_partitioner;
tf::GuidedPartitioner guided_partitioner;
std::vector<int> src1 = {1, 2, 3, 4, 5};
std::vector<int> src2 = {5, 4, 3, 2, 1};
std::vector<int> tgt1(src1.size());
std::vector<int> tgt2(src2.size());
// create a parallel-transform task with static execution partitioner
taskflow.transform(
src1.begin(), src1.end(), src2.begin(), tgt1.begin(),
[](int i, int j){
return i + j;
},
static_partitioner
);
// create a parallel-transform task with guided execution partitioner
taskflow.transform(
src1.begin(), src1.end(), src2.begin(), tgt2.begin(),
[](int i, int j){
return i + j;
},
guided_partitioner
);
@endcode
@attention
By default, parallel-transform tasks use tf::DefaultPartitioner
if no partitioner is specified.
*/
}
|