2 / Copyright (c) 2014 Vicente J. Botet Escriba
4 / Distributed under the Boost Software License, Version 1.0. (See accompanying
5 / file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
8 [//////////////////////////////////////////////////////////]
9 [section:parallel Parallel - Fork-Join -- EXPERIMENTAL]
11 [section:fork_join Fork-Join]
13 [warning These features are experimental and subject to change in future versions. There are not too much tests yet, so it is possible that you can find out some trivial bugs :(]
15 [note These features are based on the [@http://www.open-std.org/jtc1/sc22/wg21/docs/papers/2014/n4088.pdf [* n4088 - Task Region R3]] C++1y proposal from P. Halpern, A. Robison, A. Laksberg, H. Sutter, et al. The text that follows has been adapted from this paper to show the differences.]
17 The major difference respect to the standard proposal is that we are able to use a common executor for several task regions.
20 Up to now, Boost.Thread doesn't implement the parallel algorithms as defined in [@http://www.open-std.org/jtc1/sc22/wg21/docs/papers/2014/n4105.pdf [* n4105 - Information technology – Programming languages, their environments and system software interfaces – Technical Specification for C++ Extensions for Parallelism]].
23 [////////////////////]
24 [section Introduction]
27 This module introduces a C++11/c++14 library function template `task_region` and a library class `task_region_handle`
28 with member functions `run` and `wait` that together enable developers to write expressive and portable fork-join
31 The working draft for the Parallelism TS [@http://www.open-std.org/jtc1/sc22/wg21/docs/papers/2014/n4105.pdf [*N4105]] augments the STL algorithms with the inclusion of parallel execution policies. Programmers use these as a basis to write additional high-level algorithms that can be implemented in terms of the provided parallel algorithms. However, the scope of n4105 does not include lower-level mechanisms to express arbitrary fork-join parallelism
33 The `task_region`, `run` and the `wait` functions provided by this library are based on the `task_group` concept that is a part of the common subset of the PPL and the TBB libraries.
35 [endsect] [/ Introduction]
38 [/////////////////////////]
39 [section:tutorial Tutorial]
41 Consider an example of a parallel traversal of a tree, where a user-provided function compute is applied to each node of the tree, returning the sum of the results:
43 template<typename Func>
44 int traverse(node *n, Func&& compute)
46 int left = 0, right = 0;
47 task_region([&](task_region_handle& tr) {
49 tr.run([&] { left = traverse(n->left, compute); });
51 tr.run([&] { right = traverse(n->right, compute); });
53 return compute(n) + left + right;
56 The example above demonstrates the use of two of the functions proposed in this paper, `task_region` and
57 `task_region_handle::run`.
58 The `task_region` function delineates a region in a program code potentially containing invocations of tasks
59 spawned by the `run` member function of the `task_region_handle` class.
61 The run function spawns a task, a unit of work that is allowed to execute in parallel with respect to the caller.
62 Any parallel tasks spawned by `run` within the `task_region` are joined back to a single thread of execution at
63 the end of the `task_region`.
65 `run` takes a user-provided function object `f` and starts it asynchronously - i.e. it may return before the
66 execution of `f` completes. The implementation's scheduler may choose to run `f` immediately or delay running
67 `f` until compute resources become available.
69 A `task_region_handle` can be constructed only by `task_region` because it has no public constructors.
70 Thus, `run` can be invoked (directly or indirectly) only from a user-provided function passed to `task_region`:
73 void f(task_region_handle& tr)
75 tr.run(g); // OK, invoked from within task_region in h
84 task_region_handle tr; // Error: no public constructor
85 tr.run(g); // No way to call run outside of a task_region
89 [endsect] [/ Tutorial]
92 [section:examples Examples]
94 [section:fib Parallel Fibonacci]
97 This is surely the worst implementation of the Fibonacci function. Anyway, here it is, as it is simple and shows the fork-join structure clearly. `Fibonacci(n) = Fibonacci(n-1) + Fibonacci(n-2)`, so the task decomposition is trivial.
99 int fib_task_region(int n)
101 using boost::experimental::parallel::task_region;
102 using boost::experimental::parallel::task_region_handle;
104 if (n == 0) return 0;
105 if (n == 1) return 1;
110 task_region([&](task_region_handle& trh)
114 n1 = fib_task_region(n - 1);
117 n2 = fib_task_region(n - 2);
125 for (int i = 0; i<10; ++i) {
126 std::cout << fib_task_region(i) << " ";
128 std::cout << std::endl;
132 [section:fibex Parallel Fibonacci - Specific executor]
134 The previous example make use of an implementation defined way to spawn the tasks. Often the user wants to master how the task must be spawned. There is an overload of `task_region` that accept an additional `Executor` parameter and a function that takes as parameter a `task_region_handle_gen<Executor>`. `task_region_handle_gen<Executor>` run uses this executor to spawn the tasks.
137 int fib_task_region_gen( Ex& ex, int n)
139 using boost::experimental::parallel::task_region;
140 using boost::experimental::parallel::task_region_handle_gen;
142 if (n == 0) return 0;
143 if (n == 1) return 1;
148 task_region(ex, [&](task_region_handle_gen<Ex>& trh) // (2)
152 n1 = fib_task_region(n - 1);
155 n2 = fib_task_region(n - 2);
163 boost::basic_thread_pool tp; // (1)
164 for (int i = 0; i<10; ++i) {
165 std::cout << fib_task_region_gen(tp,i) << " ";
167 std::cout << std::endl;
172 The specific executor is declared in line (1) and it is used in line (2).
179 [section:quick_sort Parallel Accumulate]
182 [endsect] [/ Accumulate]
184 [section:quick_sort Parallel Quick Sort]
188 [endsect] [/ QuickSort]
189 [endsect] [/ Examples]
192 [////////////////////////]
193 [section:rationale Design Rationale]
196 [endsect] [/ Design Rationale]
197 [endsect] [/ Fork-Join]
199 [/////////////////////]
200 [section:ref Reference -- EXPERIMENTAL]
202 [/////////////////////////]
203 [section:v1 Parallel V1]
205 [section:exception_list Header `<experimental/exception_list.hpp>`]
209 namespace experimental
216 class exception_list;
224 [/////////////////////////]
225 [section:exception_list Class `exception_list`]
229 namespace experimental
236 class exception_list: public std::exception
239 typedef 'implementation defined' const_iterator;
241 ~exception_list() noexcept {}
243 void add(exception_ptr const& e);
244 size_t size() const noexcept;
245 const_iterator begin() const noexcept;
246 const_iterator end() const noexcept;
247 const char* what() const noexcept;
257 [endsect] [/ exception_list]
259 [endsect] [/ exception_list.hpp]
261 [endsect] [/ Parallel V1]
263 [////////////////////////////////////////////////////////////////////]
264 [section:v2 Parallel V2]
265 [////////////////////////////////////////////////////////////////////]
266 [section:concepts Concepts]
267 [////////////////////////////////////////////////////////////////////]
268 [section:regionCallable Concept `Region_Callable`]
270 [endsect] [/ Region_Callable]
271 [////////////////////////////////////////////////////////////////////]
272 [section:taskCallable Concept `Task_Callable`]
275 [endsect] [/ Task_Callable]
276 [////////////////////////////////////////////////////////////////////]
277 [endsect] [/ Concepts]
278 [////////////////////////////////////////////////////////////////////]
279 [section:task_region Header `<experimental/task_region.hpp>`]
283 namespace experimental
290 class task_canceled_exception;
292 template <class Executor>
293 class task_region_handle_gen;
295 using default_executor = 'implementation defined';
297 class task_region_handle;
299 template <typename Executor, typename F>
300 void task_region_final(Executor& ex, F&& f);
301 template <typename F>
302 void task_region_final(F&& f);
304 template <typename Executor, typename F>
305 void task_region(Executor& ex, F&& f);
306 template <typename F>
307 void task_region(F&& f);
315 [////////////////////////////////////////////////////////////////////]
316 [section:task_canceled_exception Class `task_canceled_exception `]
320 namespace experimental
327 class task_canceled_exception: public std::exception
330 task_canceled_exception() noexcept;
331 task_canceled_exception(const task_canceled_exception&) noexcept;
332 task_canceled_exception& operator=(const task_canceled_exception&) noexcept;
333 virtual const char* what() const noexcept;
341 [endsect] [/ task_canceled_exception]
342 [////////////////////////////////////////////////////////////////////]
343 [section:task_region_handle_gen Template Class `task_region_handle_gen<>`]
347 namespace experimental
354 template <class Executor>
355 class task_region_handle_gen
358 task_region_handle_gen(Executor& ex);
360 ~task_region_handle_gen();
363 task_region_handle_gen(const task_region_handle_gen&) = delete;
364 task_region_handle_gen& operator=(const task_region_handle_gen&) = delete;
365 task_region_handle_gen* operator&() const = delete;
378 [endsect] [/ task_region_handle_gen]
379 [////////////////////////////////////////////////////////////////////]
380 [section:default_executor Class `default_executor `]
384 namespace experimental
391 using default_executor = 'implementation defined';
398 [endsect] [/ default_executor]
399 [////////////////////////////////////////////////////////////////////]
400 [section:task_region_handle Class `task_region_handle `]
404 namespace experimental
411 class task_region_handle :
412 public task_region_handle_gen<default_executor>
415 task_region_handle();
416 task_region_handle(const task_region_handle&) = delete;
417 task_region_handle& operator=(const task_region_handle&) = delete;
418 task_region_handle* operator&() const = delete;
427 [endsect] [/ task_region_handle]
428 [////////////////////////////////////////////////////////////////////]
429 [section:task_region_final Template Function `task_region_final `]
433 namespace experimental
440 template <typename Executor, typename F>
441 void task_region_final(Executor& ex, F&& f);
442 template <typename F>
443 void task_region_final(F&& f);
450 [endsect] [/ task_region_final]
451 [////////////////////////////////////////////////////////////////////]
452 [section:task_region Template Function `task_region `]
456 namespace experimental
463 template <typename Executor, typename F>
464 void task_region(Executor& ex, F&& f);
465 template <typename F>
466 void task_region(F&& f);
473 [endsect] [/ task_region]
478 [endsect] [/ task_region.hpp]
479 [endsect] [/ Parallel V2]
480 [endsect] [/ Reference]
482 [endsect] [/ Parallel]