现代C++ 用tbb库计算 (a+b) * (a-b)

#include 
#include 

namespace flow = tbb::flow;
namespace tpt = tpf::types;
namespace tcr = tpf::chrono_random;

tpf::sstream print;
auto& endl = tpf::endl;
auto& endL = tpf::endL;

auto& nl = tpf::nl;
auto& nL = tpf::nL;

/*
    计算(a+b)*(a-b)
    (a + b) * (a - b) = a * a - b * b

                (a+b) add
    input ->                 join(add sub) -> mul(add*sub)

                (a-b) sub
*/  

void test_broadcast_node()
{
    constexpr auto N = 100;
    // 定义图
    flow::graph g;
    tpf::sstream prints[2];

    // 定义 a+b
    // 输入 a,b 返回int
    auto fn_add=[](std::tupleconst& tuple) -> int
    {
        //return std::get<0>(tuple) + std::get<1>(tuple);

        // 结构化绑定
        auto& [a,b] = tuple;
        return a+b;
    };

    // 定义 a-b
    auto fn_sub=[](std::tupleconst& tuple) -> int
    {
        // 结构化绑定
        auto& [a,b] = tuple;
        return a - b;
    };

    // 定义 a*b
    auto fn_mul=[](std::tupleconst& tuple) -> int
    {
        // 结构化绑定
        auto& [a,b] = tuple;
        return a * b;
    };

    // 广播节点,绑定首节点 即input
    flow::broadcast_node> nd_broadcast{g};

    // 定义节点,nd_add
    flow::function_node,int> nd_add{g,flow::serial,fn_add};

     // 定义节点,nd_sub
    flow::function_node,int> nd_sub{g,flow::serial,fn_sub};

     // 定义节点,nd_mul
    flow::function_node,int> nd_mul{g,flow::serial,fn_mul};
    
    // 定义join节点
    flow::join_node,flow::queueing> nd_join{g};

    // 创建边 首节点nd_broadcast -> nd_add
    flow::make_edge(nd_broadcast,nd_add);

    // 创建边 首节点nd_broadcast -> nd_sub
    flow::make_edge(nd_broadcast,nd_sub);

    // 创建边 nd_add -> nd_join
    flow::make_edge(nd_add,std::get<0>(nd_join.input_ports()));

    // 创建边 nd_sub -> nd_join
    flow::make_edge(nd_sub,std::get<1>(nd_join.input_ports()));

    // 创建边 nd_join -> nd_mul
    flow::make_edge(nd_join,nd_mul);

    auto fn_output = [&print = prints[1]](int value) ->void
    {
        print << "value: "<< value << nl;
    };

    // 创建节点,输出结果到控制台
    flow::function_node nd_output{g,flow::serial,fn_output};

    // 创建边 nd_num -> nd_output
    flow::make_edge(nd_mul,nd_output);

    // 创建随机数生成器
    auto generator = tcr::random_generator(-5,5);

    // 生成随机tuple 即 a b
    auto random_tuple = [&print = prints[0],&generator]()->std::tuple
    {
        
        auto tuple= std::tuple{generator(),generator()};
        print << "tuple: "<< tuple << nl; // nl = "\n"
        return tuple;
    };

    // 对所有的数据调用图
    for(int i = 0; i < N;++i)
    {
        nd_broadcast.try_put(random_tuple());
    }

    // 等待运行结束
    g.wait_for_all();

    // 输出结果到控制台
    prints[0]<const& tuple) -> int
    {
        //return std::get<0>(tuple) + std::get<1>(tuple);

        // 结构化绑定
        auto& [a,b] = tuple;
        return a+b;
    };

    auto fn_sub=[](std::tupleconst& tuple) -> int
    {
        // 结构化绑定
        auto& [a,b] = tuple;
        return a - b;
    };

    auto fn_mul=[](std::tupleconst& tuple) -> int
    {
        // 结构化绑定
        auto& [a,b] = tuple;
        return a * b;
    };

    auto generator = tcr::random_generator(-5,5);

    auto random_tuple = [&print = prints[0],&generator,N,state=int{}]
        (tbb::flow_control& fc) mutable ->std::tuple
    {
        if(state < N)
        {
            ++state;
            auto tuple= std::tuple{generator(),generator()};
            print << "tuple: "<< tuple << nl; // nl = "\n"
            return tuple;
        }
        else
        {
            fc.stop();
            return {};
        }
    };

    flow::input_node> nd_input{g,random_tuple};
    
    flow::function_node,int> nd_add{g,flow::serial,fn_add};
    flow::function_node,int> nd_sub{g,flow::serial,fn_sub};
    flow::function_node,int> nd_mul{g,flow::serial,fn_mul};
    
    flow::join_node,flow::queueing> nd_join{g};

    flow::make_edge(nd_input,nd_add);
    flow::make_edge(nd_input,nd_sub);

    flow::make_edge(nd_add,std::get<0>(nd_join.input_ports()));
    flow::make_edge(nd_sub,std::get<1>(nd_join.input_ports()));

    flow::make_edge(nd_join,nd_mul);

    auto fn_output = [&print = prints[1]](int value) ->void
    {
        print << "value: "<< value << nl;
    };

    flow::function_node nd_output{g,flow::serial,fn_output};

    flow::make_edge(nd_mul,nd_output);

    nd_input.activate();

    g.wait_for_all();
    prints[0]<

你可能感兴趣的:(C++,c++)