TBB流图的条件执行 [英] TBB flow graph conditional execution

查看:629
本文介绍了TBB流图的条件执行的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有可能控制的执行路径中TBB流图动态地,使用一个节点作为条件变量的输出,以确定另一节点是否应发射

It possible to control execution path in the TBB Flow Graph dynamically, using output of a node as a condition variable to determine whether another node should be launched?

推荐答案

有几种方法可以动态地控制这里的消息在顺其自然::图:

There are a couple ways to dynamically control where messages go in a flow::graph:

您可以明确提出信息到其他节点中的一个节点的身体。请注意, func_body 放消息给 F1 F2 ,根据其输入值。节点不被 make_edge附(),因为消息的流动不被图形的拓扑来控制:

You can explicitly put messages to other nodes in the body of a node. Notice that func_body puts a message to f1 or f2, depending on the value of its input. The nodes are not attached by make_edge(), because the flow of messages is not controlled by the topology of the graph:

template<typename T>
struct func_body {
    typedef tbb::flow::function_node<T,T> target_node_type;
    target_node_type &my_n1;
    target_node_type &my_n2;
    func_body(target_node_type &node1, target_node_type &node2) : my_n1(node1), my_n2(node2) {}
    tbb::flow::continue_msg operator()(const T& in) {
        // do some computation
        bool send_to_one = in > 0;
        if(send_to_one) my_n1.try_put(in);
        else            my_n2.try_put(in);
        return tbb::flow::continue_msg();  // message is discarded if no successor exists
    }
};

struct otherbody {
    int operator()(const int& in) {
        return in;
    }
};

int
main() {
    tbb::flow::graph g;
    tbb::flow::function_node<int,int> f1(g, tbb::flow::unlimited, otherbody());
    tbb::flow::function_node<int,int> f2(g, tbb::flow::unlimited, otherbody());
    tbb::flow::function_node<int> cn(g, tbb::flow::unlimited, func_body<int>(f1,f2));
}

或者你也可以使用 multifunction_node (注意类型元组的GET 模板中的 TBB ::流量命名空间,而不是的std :: 作为旧文件。在这种情况下,我们重视)公告 F1 F2 multifunction_node 。

Or you can use a multifunction_node (note the types of the tuple and get templates are in the tbb::flow namespace, not std:: as in the old documentation.) Notice in this case we attach f1 and f2 to the output ports of the multifunction_node.

typedef tbb::flow::multifunction_node<int,tbb::flow::tuple<int,int> > mfnode;

struct mfunc_body {
    void operator()(const int& in, mfnode::output_ports_type &op) {
        // do some computation
        bool send_to_one = in > 0;
        if(send_to_one) tbb::flow::get<0>(op).try_put(in);
        else            tbb::flow::get<1>(op).try_put(in);
    }
};

struct otherbody {
    int operator()(const int& in) {
        return in;
    }
};

int
main() {
    tbb::flow::graph g;
    tbb::flow::function_node<int,int> f1(g, tbb::flow::unlimited, otherbody());
    tbb::flow::function_node<int,int> f2(g, tbb::flow::unlimited, otherbody());
    mfnode cn(g, tbb::flow::unlimited, mfunc_body());
    tbb::flow::make_edge(tbb::flow::output_port<0>(cn), f1);
    tbb::flow::make_edge(tbb::flow::output_port<1>(cn), f2);

    // ...
}

目前这两种方法在功能上,相同的;每次将产生一个任务来执行 function_nodes的身体。在未来的 multifunction_node 情况下可以被优化,如果只有一个输出端口 try_put()来不产卵。

Currently these two methods are functionally-identical; each will spawn a task to execute the body of the function_nodes. In the future the multifunction_node case may be optimized to not spawn if only one output port is try_put() to.

这篇关于TBB流图的条件执行的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆