-
Notifications
You must be signed in to change notification settings - Fork 1.1k
Add tasking documentation to user guide #1791
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from 12 commits
Commits
Show all changes
21 commits
Select commit
Hold shift + click to select a range
4566c85
Add task documentation to user guide
vossmjp 4de1059
Added more details about task_group::wait thread safety
vossmjp d1d766d
Update doc/main/tbb_userguide/Parallelizing_with_Tasks.rst
vossmjp d3f8cd3
Fixed copyright
vossmjp f5b21a7
Update doc/main/tbb_userguide/examples/task_examples.cpp
vossmjp 2f55a41
Update doc/main/tbb_userguide/examples/task_examples.cpp
vossmjp 5a0f134
Addressed review comments
vossmjp c7ea938
Update doc/main/tbb_userguide/task_group_cancellation.rst
vossmjp a592315
Update doc/main/tbb_userguide/creating_tasks_with_task_group.rst
vossmjp 668a17f
Update doc/main/tbb_userguide/task_group_thread_safety.rst
vossmjp bbc34f6
Update doc/main/tbb_userguide/task_group_thread_safety.rst
vossmjp 0430818
Replaced CAS with comment
vossmjp 335c02b
Simplified example
vossmjp 634bba6
Modified other example to match simplification
vossmjp f33559a
Moved cancellation and shortened examples by ref
vossmjp f86c388
Update doc/main/tbb_userguide/examples/task_examples.cpp
vossmjp 16a6423
Update doc/main/tbb_userguide/creating_tasks_with_task_group.rst
vossmjp b3f7578
Update doc/main/tbb_userguide/creating_tasks_with_task_group.rst
vossmjp 030f7f0
Update doc/main/tbb_userguide/task_group_thread_safety.rst
vossmjp 9ec8adc
Apply suggestion from @akukanov
vossmjp e1492fb
Updated cancellation to use task_group functions directly
vossmjp File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
.. _Parallelizing_with_Tasks: | ||
|
||
Parallelizing with Tasks | ||
======================== | ||
|
||
When parallel loops or the flow graph are not sufficient, the |full_name| | ||
library supports parallelization directly with tasks. Tasks | ||
can be created using the function ``oneapi::tbb::parallel_invoke`` or | ||
the class ``oneapi::tbb::task_group``. | ||
|
||
|
||
.. toctree:: | ||
:maxdepth: 4 | ||
|
||
../tbb_userguide/creating_tasks_with_parallel_invoke | ||
../tbb_userguide/creating_tasks_with_task_group | ||
../tbb_userguide/task_group_cancellation | ||
../tbb_userguide/task_group_thread_safety |
55 changes: 55 additions & 0 deletions
55
doc/main/tbb_userguide/creating_tasks_with_parallel_invoke.rst
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,55 @@ | ||
.. _creating_tasks_with_parallel_invoke: | ||
|
||
Creating Tasks with parallel_invoke | ||
=================================== | ||
|
||
Suppose you want to search a binary tree for the node that contains a specific value. | ||
Here is sequential code to do this: | ||
|
||
Nodes are represented by ``struct TreeNode``. | ||
|
||
.. literalinclude:: ./examples/task_examples.cpp | ||
:language: c++ | ||
:start-after: /*begin_treenode*/ | ||
:end-before: /*end_treenode*/ | ||
|
||
The function ``serial_tree_search`` is a recursive algorithm that checks the current node | ||
and, if the value is not found, calls itself on the left and right subtree. | ||
|
||
.. literalinclude:: ./examples/task_examples.cpp | ||
:language: c++ | ||
:start-after: /*begin_search_serial*/ | ||
:end-before: /*end_search_serial*/ | ||
|
||
|
||
To improve performance, you can use ``oneapi::tbb::parallel_invoke`` to search the tree | ||
in parallel: | ||
akukanov marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
A recursive base case is used after a minimum size threshold is reached to avoid parallel overheads. | ||
Since more than one thread can call the base case concurrently as part of the same tree, ``result`` | ||
is held in an atomic variable. | ||
|
||
.. literalinclude:: ./examples/task_examples.cpp | ||
:language: c++ | ||
:start-after: /*begin_sequential_tree_search*/ | ||
:end-before: /*end_sequential_tree_search*/ | ||
|
||
The function ``oneapi::tbb::parallel_invoke`` runs multiple independent tasks in parallel. | ||
Here is a function ``parallel_invoke_search``, where two lambdas are passed that define tasks | ||
that search the left and right subtrees of the current node in parallel: | ||
|
||
.. literalinclude:: ./examples/task_examples.cpp | ||
:language: c++ | ||
:start-after: /*begin_parallel_invoke_search*/ | ||
:end-before: /*end_parallel_invoke_search*/ | ||
|
||
If the value is found the pointer to the node that contains the value is stored | ||
in the ``std::atomic<TreeNode*> result``. This example uses recursion to create many tasks, instead of | ||
just two. The depth of the parallel recursion is limited by the ``depth_threshold`` parameter. After this depth is | ||
reached, the search falls back to a sequential approach. The value of ``result`` is periodically checked | ||
to see if the value has been found by other concurrent tasks, and if so, the search in the current task is | ||
terminated. Because multiple threads may access ``result`` concurrently, an atomic variable is used, even | ||
from the sequential base case. | ||
|
||
Because ``oneapi::tbb::parallel_invoke`` is a fork-join algorithm, each level of the recursion does not | ||
complete until both the left and right subtrees have completed. |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,51 @@ | ||
.. _creating_tasks_with_task_group: | ||
|
||
Creating Tasks with task_group | ||
============================== | ||
|
||
The |full_name| library supports parallelization directly with tasks. The class | ||
``oneapi::tbb::task_group`` is used to run and wait for tasks in a less | ||
structured way than ``oneapi::tbb::parallel_invoke``. It is useful when you want to | ||
create a set of tasks that can be run in parallel, but you do not know how many there | ||
will be in advance. | ||
|
||
Here is code that uses ``oneapi::tbb::task_group`` to implement a parallel search in | ||
a binary tree: | ||
akukanov marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
|
||
Nodes are represented by ``struct TreeNode``. | ||
|
||
.. literalinclude:: ./examples/task_examples.cpp | ||
:language: c++ | ||
:start-after: /*begin_treenode*/ | ||
:end-before: /*end_treenode*/ | ||
|
||
A recursive base case is used after a minimum size threshold is reached to avoid parallel overheads. | ||
Since more than one thread can call the base case concurrently as part of the same tree, ``result`` | ||
is held in an atomic variable. | ||
|
||
.. literalinclude:: ./examples/task_examples.cpp | ||
:language: c++ | ||
:start-after: /*begin_sequential_tree_search*/ | ||
:end-before: /*end_sequential_tree_search*/ | ||
|
||
In ``parallel_tree_search_impl``, ``task_group::run`` is used to create a new task for searching | ||
in the right subtree if both subtrees are valid. The recursion does not wait on the ``task_group`` | ||
at each level and reuses the current task to search one of the subtrees. This can reduce the | ||
overhead of task creation and management, allowing for efficient use of resources. | ||
|
||
.. literalinclude:: ./examples/task_examples.cpp | ||
:language: c++ | ||
:start-after: /*begin_parallel_search*/ | ||
:end-before: /*end_parallel_search*/ | ||
|
||
This example uses recursion to create many tasks. The depth of the parallel recursion is | ||
limited by the ``depth_threshold`` parameter. After this depth is reached, no new tasks | ||
are created. The value of ``result`` is periodically checked to see if the value has been | ||
found by other concurrent tasks, and if so, the search in the current task is terminated. | ||
|
||
In this example, tasks that execute within the ``task_group tg`` add additional tasks | ||
vossmjp marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
by calling ``run`` on the same ``task_group`` object. These calls are thread-safe and the | ||
call to ``tg.wait()`` will block until all of these tasks are complete. Although these | ||
additional tasks might be added from different worker threads, these additions are logically nested | ||
within the top-most calls to ``tg.run``. There is therefore no race in adding these tasks from | ||
vossmjp marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
the worker threads and waiting for them in the main thread. |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.