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

#include <tpf_output.hpp>
#include <tpf_chrono_random.hpp>

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::tuple<int,int>const& tuple) -> int
    {
        //return std::get<0>(tuple) + std::get<1>(tuple);

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

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

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

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

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

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

     // 定义节点,nd_mul
    flow::function_node<std::tuple<int,int>,int> nd_mul{g,flow::serial,fn_mul};
    
    // 定义join节点
    flow::join_node<std::tuple<int,int>,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<int> nd_output{g,flow::serial,fn_output};

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

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

    // 生成随机tuple 即 a b
    auto random_tuple = [&print = prints[0],&generator]()->std::tuple<int,int>
    {
        
        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]<<endl;
    prints[1]<<endl;
}

void test_input_node()
{
    constexpr auto N = 100;
    flow::graph g;
    tpf::sstream prints[2];

    auto fn_add=[](std::tuple<int,int>const& tuple) -> int
    {
        //return std::get<0>(tuple) + std::get<1>(tuple);

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

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

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

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

    auto random_tuple = [&print = prints[0],&generator,N,state=int{}]
        (tbb::flow_control& fc) mutable ->std::tuple<int,int>
    {
        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<std::tuple<int,int>> nd_input{g,random_tuple};
    
    flow::function_node<std::tuple<int,int>,int> nd_add{g,flow::serial,fn_add};
    flow::function_node<std::tuple<int,int>,int> nd_sub{g,flow::serial,fn_sub};
    flow::function_node<std::tuple<int,int>,int> nd_mul{g,flow::serial,fn_mul};
    
    flow::join_node<std::tuple<int,int>,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<int> nd_output{g,flow::serial,fn_output};

    flow::make_edge(nd_mul,nd_output);

    nd_input.activate();

    g.wait_for_all();
    prints[0]<<endl;
    prints[1]<<endl;
}

int main()
{
    test_input_node();
    //test_broadcast_node();
    return 0;
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值