Attach Flow Graph to an Arbitrary Task Arena

Attach Flow Graph to an Arbitrary Task Arena#

oneTBB task_arena interface provides mechanisms to guide tasks execution within the arena by setting the preferred computation units, restricting part of computation units, or limiting arena concurrency. In some cases, you may want to apply such mechanisms when a flow graph executes.

During its construction, a graph object attaches to the arena, in which the constructing thread occupies a slot.

This example shows how to set the most performant core type as the preferred one for a graph execution:

    std::vector<tbb::core_type_id> core_types = tbb::info::core_types();
    tbb::task_arena arena(
        tbb::task_arena::constraints{}.set_core_type(core_types.back())
    );

    arena.execute( [&]() {
        graph g;
        function_node< int > f( g, unlimited, []( int ) {
             /*the most performant core type is defined as preferred.*/
        } );
        f.try_put(1);
        g.wait_for_all();
    } );

A graph object can be reattached to a different task_arena by calling the graph::reset() function. It reinitializes and reattaches the graph to the task arena instance, inside which the graph::reset() method is executed.

This example shows how to reattach existing graph to an arena with the most performant core type as the preferred one for a work execution. Whenever a task is spawned on behalf of the graph, it is spawned in the arena of a graph it is attached to, disregarding the arena of the thread that the task is spawned from:

    graph g;
    function_node< int > f( g, unlimited, []( int ) {
        /*the most performant core type is defined as preferred.*/
    } );

    std::vector<tbb::core_type_id> core_types = tbb::info::core_types();
    tbb::task_arena arena(
        tbb::task_arena::constraints{}.set_core_type(core_types.back())
    );

    arena.execute( [&]() {
        g.reset();
    } );
    f.try_put(1);
    g.wait_for_all();

See the following topics to learn more: