|
| 1 | +.. _creating_tasks_with_parallel_invoke: |
| 2 | + |
| 3 | +Creating Tasks with parallel_invoke |
| 4 | +=================================== |
| 5 | + |
| 6 | +Suppose you want to search a binary tree for the node that contains a specific value. |
| 7 | +Here is sequential code to do this: |
| 8 | + |
| 9 | +Nodes are represented by ``struct TreeNode``: |
| 10 | + |
| 11 | +.. literalinclude:: ./examples/task_examples.cpp |
| 12 | + :language: c++ |
| 13 | + :start-after: /*begin_treenode*/ |
| 14 | + :end-before: /*end_treenode*/ |
| 15 | + |
| 16 | +The function ``serial_tree_search`` is a recursive algorithm that checks the current node |
| 17 | +and, if the value is not found, calls itself on the left and right subtree. |
| 18 | + |
| 19 | +.. literalinclude:: ./examples/task_examples.cpp |
| 20 | + :language: c++ |
| 21 | + :start-after: /*begin_search_serial*/ |
| 22 | + :end-before: /*end_search_serial*/ |
| 23 | + |
| 24 | + |
| 25 | +To improve performance, you can use ``oneapi::tbb::parallel_invoke`` to search the tree |
| 26 | +in parallel: |
| 27 | + |
| 28 | +A recursive base case is used after a minimum size threshold is reached to avoid parallel overheads. |
| 29 | +Since more than one thread can call the base case concurrently as part of the same tree, ``result`` |
| 30 | +is held in an atomic variable. |
| 31 | + |
| 32 | +.. literalinclude:: ./examples/task_examples.cpp |
| 33 | + :language: c++ |
| 34 | + :start-after: /*begin_sequential_tree_search*/ |
| 35 | + :end-before: /*end_sequential_tree_search*/ |
| 36 | + |
| 37 | +The function ``oneapi::tbb::parallel_invoke`` runs multiple independent tasks in parallel. |
| 38 | +Here is a function ``parallel_invoke_search``, where two lambdas are passed that define tasks |
| 39 | +that search the left and right subtrees of the current node in parallel: |
| 40 | + |
| 41 | +.. literalinclude:: ./examples/task_examples.cpp |
| 42 | + :language: c++ |
| 43 | + :start-after: /*begin_parallel_invoke_search*/ |
| 44 | + :end-before: /*end_parallel_invoke_search*/ |
| 45 | + |
| 46 | +If the value is found, the pointer to the node that contains the value is stored |
| 47 | +in the ``std::atomic<TreeNode*> result``. This example uses recursion to create many tasks, instead of |
| 48 | +just two. The depth of the parallel recursion is limited by the ``depth_threshold`` parameter. After this depth is |
| 49 | +reached, the search falls back to a sequential approach. The value of ``result`` is periodically checked |
| 50 | +to see if the value has been found by other concurrent tasks, and if so, the search in the current task is |
| 51 | +terminated. Because multiple threads may access ``result`` concurrently, an atomic variable is used, even |
| 52 | +from the sequential base case. |
| 53 | + |
| 54 | +Because ``oneapi::tbb::parallel_invoke`` is a fork-join algorithm, each level of the recursion does not |
| 55 | +complete until both the left and right subtrees have completed. |
0 commit comments