oneapi-src / oneTBB

oneAPI Threading Building Blocks (oneTBB)
https://oneapi-src.github.io/oneTBB/
Apache License 2.0
5.7k stars 1.02k forks source link

[FR] Move semantics for flow graph #515

Open sdebionne opened 3 years ago

sdebionne commented 3 years ago

When running a graph outside the main thread, it's very tempting to create the graph/nodes in the main thread and then move them to the execution thread. Unfortunately that does not work out of the box as graph (and probably some implementation detail such as spin_mutex) it not movable (and neither copiable).

Ideally I would like to write something like:

{
    // Create the graph, nodes and edges
    input_node_t input(g, tbb::flow::unlimited);
    function_node_t function(g, tbb::flow::unlimited);

    tbb::flow::make_edge(input, function);

    std::cout << "Graph prepared";

    // Run a TBB flow graph asynchronously (using std::async for simplicity)
    auto fut = std::async(std::launch::async,
                          [g_ = std::move(g), input_ = std::move(input), function_ = std::move(function)]() mutable {
                              input_.activate();
                              g_.wait_for_all();

                              std::cout << "Graph finished" << std::endl;
                          });

    // Here the graph, nodes and edges go out of scope
}

A workaround is to wrap the graph with unique_ptr but it's not user friendly.

sdebionne commented 3 years ago

Looking at the code and since the nodes keep a reference to the graph, this FR seems hardly possible.

alexey-katranov commented 3 years ago

As a straightforward workaround, have you tried to wrap graph into std::unique_ptr?

sdebionne commented 3 years ago

Thanks, that's what I end up doing, like I mention in the original comment. As a side question, what is the rational of having nodes keep a reference to a graph that can get dangling vs having the graph own the nodes (and for instance allow to visit them to get the node topology)?

alexey-katranov commented 3 years ago

I believe when flow graph was designed and introduced (in ~2010) nobody seriously thought about move semantics support that was introduced in C+11. Perhaps, it can be redesigned in such aspect but the cost of the effort and broken backward compatibility should be considered.

arunparkugan commented 2 months ago

@anton-potapov is this issue still relevant?