1.. _Mapping_Nodes2Tasks:
2
3Flow Graph Basics: Mapping Nodes to Tasks
4=========================================
5
6
7The following figure shows the timeline for one possible execution of
8the two node graph example in the previous section. The bodies of n and
9m will be referred to as λ\ :sub:`n` and λ\ :sub:`m`, respectively. The
10three calls to try_put spawn three tasks; each one applies the lambda
11expression, λ\ :sub:`n`, on one of the three input messages. Because n
12has unlimited concurrency, these tasks can execute concurrently if there
13are enough threads available. The call to ``g.wait_for_all()`` blocks until
14there are no tasks executing in the graph. As with other ``wait_for_all``
15functions in oneTBB, the thread that calls ``wait_for_all`` is not spinning
16idly during this time, but instead can join in executing other tasks
17from the work pool.
18
19
20.. container:: fignone
21
22
23   **Execution Timeline of a Two Node Graph**
24
25
26   .. container:: imagecenter
27
28
29      |image0|
30
31
32As each task from n finishes, it puts its output to m, since m is a
33successor of n. Unlike node n, m has been constructed with a concurrency
34limit of 1 and therefore does not spawn all tasks immediately. Instead,
35it sequentially spawns tasks to execute its body, λ\ :sub:`m`, on the
36messages in the order that they arrive. When all tasks are complete, the
37call to ``wait_for_all`` returns.
38
39
40.. note::
41   All execution in the flow graph happens asynchronously. The calls to
42   try_put return control to the calling thread quickly, after either
43   immediately spawning a task or buffering the message being passed.
44   Likewise, the body tasks execute the lambda expressions and then put
45   the result to any successor nodes. Only the call to ``wait_for_all``
46   blocks, as it should, and even in this case the calling thread may be
47   used to execute tasks from the oneTBB work pool while it is waiting.
48
49
50The above timeline shows the sequence when there are enough threads to
51execute all of the tasks that can be executed in parallel. If there are
52fewer threads, some spawned tasks will need to wait until a thread is
53available to execute them.
54
55
56.. |image0| image:: Images/execution_timeline2node.jpg
57
58