Skip to content

Commit 3a7e4ab

Browse files
Tsung-Wei HuangTsung-Wei Huang
authored andcommitted
updated core
1 parent 2768de5 commit 3a7e4ab

File tree

5 files changed

+31
-13
lines changed

5 files changed

+31
-13
lines changed

README.md

Lines changed: 26 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -213,7 +213,8 @@ The table below summarizes its commonly used methods.
213213
| placeholder | none | task | insert a node without any work; work can be assigned later |
214214
| linearize | task list | none | create a linear dependency in the given task list |
215215
| parallel_for | beg, end, callable, group | task pair | apply the callable in parallel and group-by-group to the result of dereferencing every iterator in the range |
216-
| reduce | beg, end, res, op, group | task pair | apply a binary operator group-by-group to reduce a range of elements to a single result |
216+
| reduce | beg, end, res, bop, group | task pair | apply a binary operator group-by-group to reduce a range of elements to a single result |
217+
| transform_reduce | beg, end, res, bop, uop, group | task pair | apply a unary operator to each element in the range and reduce the returns to a single result group-by-group through a binary operator |
217218
| dispatch | none | future | dispatch the current graph and return a shared future to block on completeness |
218219
| silent_dispatch | none | none | dispatch the current graph |
219220
| wait_for_all | none | none | dispatch the current graph and block until all graphs including previously dispatched ones finish |
@@ -279,13 +280,13 @@ auto [S, T] = tf.parallel_for(
279280
v.end(), // end of range
280281
[] (int i) {
281282
std::cout << "parallel in " << i << '\n';
282-
}
283+
},
284+
1 // execute one task at a time
283285
);
284-
285286
// add dependencies via S and T.
286287
```
287288
288-
By default, the group size is 1. Changing the group size can force intra-group tasks to run sequentially
289+
Changing the group size can force intra-group tasks to run sequentially
289290
and inter-group tasks to run in parallel.
290291
Depending on applications, different group sizes can result in significant performance hit.
291292
@@ -304,13 +305,13 @@ auto [S, T] = tf.parallel_for(
304305
);
305306
```
306307

307-
### *reduce*
308+
### *reduce/transform_reduce*
308309

309310
The method `reduce` creates a subgraph that applies a binary operator to a range of items in a container.
310311
The result will be stored in the referenced `res` object passed to the method.
311312
It is your responsibility to assign it a correct initial value to reduce.
312313

313-
<img align="right" width="50%" src="image/reduce.png">
314+
<img align="right" width="45%" src="image/reduce.png">
314315

315316
```cpp
316317
auto v = {1, 2, 3, 4};
@@ -319,13 +320,13 @@ auto [S, T] = tf.reduce(
319320
v.begin(), // beg of range
320321
v.end(), // end of range
321322
sum, // pass by reference
322-
std::plus<int>()
323+
std::plus<int>(),
324+
1 // execute one task at a time
323325
);
324-
325326
// add dependencies via S and T.
326327
```
327328
328-
By default, the group size is 1. Changing the group size can force intra-group tasks to run sequentially
329+
Changing the group size can force intra-group tasks to run sequentially
329330
and inter-group tasks to run in parallel.
330331
Depending on applications, different group sizes can result in significant performance hit.
331332
@@ -339,6 +340,21 @@ auto [S, T] = tf.reduce(
339340
);
340341
```
341342

343+
The method `transform_reduce` is similar to reduce, except it applies a unary operator before reduction.
344+
This is particular useful when you need additional data processing to reduce a range of elements.
345+
346+
```cpp
347+
auto v = { {1, 5}, {6, 4}, {-6, 4} };
348+
int min = std::numeric_limits<int>::max();
349+
auto [S, T] = tf.transform_reduce(v.begin(), v.end(), min,
350+
[] (int l, int r) { return std::min(l, r); },
351+
[] (const Data& d) { return a*a + 2*a*b + b*b; }
352+
);
353+
```
354+
355+
All reduce methods have overloads of no group size,
356+
in which the workload is evenly partitioned across threads.
357+
342358
### *dispatch/silent_dispatch/wait_for_all*
343359
Dispatching a taskflow graph will schedule threads to execute the current graph and return immediately.
344360
The method `dispatch` gives you a future object to probe the execution progress while
@@ -445,6 +461,7 @@ To use Cpp-Taskflow, you only need a C++17 compiler:
445461
# Compile Unit Tests and Examples
446462
Cpp-Taskflow uses [CMake](https://cmake.org/) to build examples and unit tests.
447463
We recommend using out-of-source build.
464+
448465
```bash
449466
~$ cmake --version # must be at least 3.9 or higher
450467
~$ mkdir build

example/parallel_for.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ void taskflow(int N) {
3232
tf::Taskflow tf;
3333
tf.parallel_for(range, [&] (const int i) {
3434
printf("fib[%d]=%d\n", i, fib(i));
35-
}, 1);
35+
});
3636
tf.wait_for_all();
3737

3838
auto tend = std::chrono::steady_clock::now();

image/reduce.png

16.4 KB
Loading

taskflow.hpp

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -93,7 +93,7 @@ template <typename T>
9393
inline constexpr bool is_iterable_v = is_iterable<T>::value;
9494

9595
//-------------------------------------------------------------------------------------------------
96-
// Threadpool definition
96+
// Utility
9797
//-------------------------------------------------------------------------------------------------
9898

9999
// Struct: MoveOnCopy
@@ -111,7 +111,9 @@ struct MoveOnCopy {
111111
template <typename T>
112112
MoveOnCopy(T&&) -> MoveOnCopy<T>;
113113

114-
// ------------------------------------------------------------------------------------------------
114+
//-------------------------------------------------------------------------------------------------
115+
// Threadpool definition
116+
//-------------------------------------------------------------------------------------------------
115117

116118
// Class: Threadpool
117119
class Threadpool {

unittest/taskflow.cpp

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -358,7 +358,6 @@ TEST_CASE("Taskflow.ReduceMax") {
358358
REQUIRE(test == gold);
359359
}
360360
}
361-
362361
}
363362

364363
/*// --------------------------------------------------------

0 commit comments

Comments
 (0)