Intel®threading Building Blocks (INTEL®TBB) Developer guide Chinese parallelizing Data flow and dependence graphs parallelization of data flow and dependency graphs

Source: Internet
Author: User

Https://www.threadingbuildingblocks.org/docs/help/index.htm

Parallelizing Data Flow and Dependency Graphs

In addition to loop parallelism, the intel®threading Building Blocks (INTEL®TBB) library also supports graph parallelism . It's possible to create graphs that's highly scalable, but it's also possible to create graphs that's completely sequ Ential.

In addition to cyclic parallelization, TBB also supports graph parallelization. This makes it possible to create highly scalable diagrams, as well as create diagrams that are executed in a fully sequential

Using graph parallelism, computations is represented by nodes and the communication channels between these computations a Re represented by edges. When a node in the graph receives a message, a task was spawned to execute its body object on the incoming message. Messages flow through the graph across the edges that connect the nodes. The following sections present, examples of applications, can be expressed as graphs. For more information on tasks, see the See Also section below.

In graph parallelization, the computation is represented as a node, and the communication channel between the computations is expressed as an edge. When a node receives a message, a task is executed. The message flows through the diagram through the edges of the connection node. Here are two examples of

The following figure shows a  streaming  or  Data Flow  application where a Sequence of values are processed as each value passes through the nodes in the graph. In this example, the sequence are created by a function f. For each value in the Sequence, g squares the value and h& Nbsp;cubes the Value. j then takes each of the squared and cubed values and adds them to a Global sum. After all values in the sequence is completely processed, sum is equal to the sum of the sequence of squares and cubes from 1 to 10. In a streaming or data flow graph, the values actually flow across the edges; The output of one node becomes the input of its successor (s).

is an application of streaming or data flow

Simple Data Flow Graph

The following graphic shows a different form of graph application. In this example, a dependence graph was used to establish a partial ordering among the steps for making a peanut butter and Jelly Sandwich. In this partial ordering, you must first get the bread before spreading the peanut butter or jelly on the bread. You must spread in the peanut butter before you put away the peanut butter jar, and likewise spread on the jelly before yo U put away the jelly jar. And, you need to spread on both the peanut butter and jelly before putting the both slices of bread together. This was a partial ordering because, for example, it doesn ' t matter if you spread on the peanut butter first or the jelly F Irst. It also doesn ' t matter if you finish making the sandwich before putting away the jars.

Is the application of another graph, which is performed in the form of dependence graph to express tasks.

dependence Graph for Making a Sandwich

While it can inferred that resources, such as the bread, or the jelly jar, was shared between ordered steps, it is not Explicit in the graph. Instead, only the required ordering of steps are explicit in a dependence graph. For example, you must "put jelly on 1 slice" before "Put Away jelly jar".

The flow graph interface in the Intel TBB Library allows your to express data flow and dependence graphs such as these, as Well as more complicated graphs this include cycles, conditionals, buffering and more. If you express your application using the Flow graph interface, the runtime library spawns tasks to exploit the Parallelis M that's present in the graph. For example, in the first example above, perhaps, different values might is squared in parallel, or the same value Migh T is squared and cubed in parallel. Likewise in the second example, the peanut butter might is spread on one slice of bread in parallel with the jelly being s Pread on the other slice. The interface expresses what's legal to execute in parallel, but allows the runtime library to choose at runtime what wil l am executed in parallel.

TBB allows you to express data flow and dependence graphs. and more complex diagrams, such as cycle, condition, buffering.

The support for graph parallelism are contained within the namespace Tbb::flow and is defined in the flow_graph.h H Eader file.

Parent topic:Parallelizing Data flow and dependence Graphssee alsotask-based programmingbasic Flow Graph conceptsgraph Application Cate Goriesflow graph Tips and Tricks basic flow graph Concepts fundamental concept flow graph Basics:graph Object graph

Conceptually a flow graph is a collection of nodes and edges. Each node belongs to exactly one graph and edges is made only between nodes in the same graph. In the flow graph interface, a graph object represents this collection of nodes and edges, and are used for invoking whole Graph operations such as waiting for all tasks related to the graph to complete, resetting the state of all nodes in the G Raph, and canceling the execution of all nodes in the graph.

The code below creates a graph object and then waits for all tasks spawned by the graph to complete. The call-to Wait_for_all-example returns immediately since this is a trivial graph with no nodes or edges, and Therefore no tasks are spawned.

Graph G;g.wait_for_all ();

Flow Graph basics:nodes Node

A node is a class this inherits from Tbb::flow::graph_node and also typically inherits from TBB: :flow::sender<t>, tbb::flow::receiver<t> or both. A node performs some operation, usually on an incoming message and could generate zero or more output messages. Some nodes require more than one input message or generate more than one output message.

Nodes are used for calculations.

While it's possible to define your own node types by inheriting from Graph_node, sender and receiver, it's M Ore typical that predefined node types is used to construct a graph. The list of predefined nodes is available by the See Also section below.

A function_node is a predefined type available in flow_graph.h and represents a simple function with one Input and one output. The constructor for aFunction_node takes three arguments:

body> Function_node (graph &gconcurrencybody)
Parameter Description
Body

Type of the Body object.

G

The graph the node belongs to.

Concurrency

The concurrency limit for the node. You can use the concurrency limit to control how many invocations of the node is allowed to proceed concurrently, from 1 (serial) to an unlimited number.

Body

User defined Function object, or lambda expression, that's applied to the incoming message to generate the outgoing Messa Ge.

Below is code for creating a, simple graph, that contains a, and a single function_node. In this example, a node n was constructed that belongs to graph G, and have a second argument of 1, which allows at Most 1 invocation of the node to occur concurrently. The body is a lambda expression this prints each value v so it receives, spins for v seconds, prints the value a Gain, and then returns v unmodified. The code for the function spin_for are not provided.

    graph G;    function_node< int, int > N (g, 1, [] (int v), int {         cout << v;        Spin_for (v);        cout << v;        return v;    } );

After the node was constructed in the example above, you can pass messages to it, either by connecting it to other nodes us ing edges or by invoking its function try_put. Using edges is described in the next section.

    N.try_put (1);    N.try_put (2);    N.try_put (3);

You can then wait for the messages-to is processed by calling Wait_for_all on the graph object:

    

In the above example code, the function_node N is created with a concurrency limit of 1. When it receives the message sequence 1, 2 and 3, the node n would spawn a task to apply the body to the first input, 1 . When that task was complete, it would then spawn another task to apply the body to 2. and likewise, the node would wait for that task to complete before spawning a third task to apply the body to 3. The calls to Try_put does not block until a task was spawned; If a node cannot immediately spawn a task to process the Me Ssage, the message is buffered in the node. When it was legal, based on concurrency limits, a task would be spawned to process the next buffered message.

In the above graph, each message is processed sequentially. If However, you construct the node with a different concurrency limit, parallelism can be achieved:

    function_node< int, int > n (g, tbb::flow::unlimited, [] (int v), int {         cout << v;        Spin_for (v);        cout << v;        return v;    } );

You can use Unlimited as the concurrency limit to instruct the library to spawn a task as soon as a message arrives, R Egardless of how many other tasks has been spawned. You can also the use of any specific value, such as 4 or 8, to the limit concurrency to the most 4 or 8, respectively. It is important to remember that spawning a task does not mean creating a thread. So while a graph could spawn many tasks, only the number of threads available in the library ' s thread pool would be used to E Xecute these tasks.

Suppose unlimited in the Function_node constructor instead and call Try_put on the node:

    N.try_put (1);    N.try_put (2);    N.try_put (3);    

The library spawns three tasks, each one applying n ' s lambda expression to one of the messages. If you had a sufficient number of threads available on your system, then all three invocations of the body would occur in Parallel. If However, you had only one thread in the system, they execute sequentially.

Parent topic:Basic Flow Graph conceptssee alsotask scheduleredgesgraph objectmapping Nodes to taskspredefined Node Types


Intel®threading Building Blocks (INTEL®TBB) Developer guide Chinese parallelizing Data flow and dependence graphs parallelization of data flow and dependency graphs

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.