现代C++并行程序 TBB Graph(CUDA Graph,SYCL Graph)

实例所使用的链接文档    

1.Get Started with oneTBB

https://oneapi-src.github.io/oneTBB/GSG/get_started.html

 2.oneapi-src/oneTBB

https://github.com/oneapi-src/oneTBB

3.Using input_node

https://oneapi-src.github.io/oneTBB/main/tbb_userguide/use_input_node.html

4.The functionality deprecated in TBB 2020 and removed in oneTBB

https://www.intel.com/content/dam/develop/external/us/en/documents/tbbrevamp.pdf

5.Flow Graph Basics: Nodes (oneTBB)

https://oneapi-src.github.io/oneTBB/main/tbb_userguide/Nodes.html

6.Getting Started with CUDA Graphs

https://developer.nvidia.com/blog/cuda-graphs/

7. DPC++ Execution Graph

 https://intel.github.io/llvm-docs/doxygen/group__sycl__graph.html
 


#include 

/*
    编译指令
    g++ -std=c++20 graph.cpp -ltbb12 -o g.exe
    clang -std=c++20 graph.cpp -ltbb12 -o c.exe
    cl /EHsc /std:c++20 graph.cpp tbb12.lib /Fe: m.exe
    nvcc -std=c++17 graph.cpp -ltbb12 -o n.exe
    dpcpp -Qstd=c++20 /EHsc -fno-sycl graph.cpp -o d.exe

    demo使用的函数
    flow::graph
    tbb::flow_control
    flow::input_node
    flow::function_node
    flow::make_edge
*/

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

tpf::sstream print;
auto& endl = tpf::endl;
auto nl = "\n";

void test_graph_basic()
{
    /*
        flow::graph
        tbb::flow_control
        flow::input_node
        flow::function_node
        flow::make_edge
    */

   flow::graph g;
   constexpr int N = 100;

   auto inputter = [N,state = int{}](tbb::flow_control& fc) mutable ->int
   {
        if(state < N)
        {
            return state++;
        }
        else
        {
            fc.stop();
            return int{}; // this is for C++ compiler's complaints,or keep C++ compiler silient.
        }

   };

   flow::input_node my_source(g,inputter);

    // 并行执行
    auto parallel_body = [](int i)
    {
        print << i << endl;
    };

   flow::function_node my_node(g,flow::unlimited,parallel_body);
   
   //edge represents data stream flow.
   flow::make_edge(my_source,my_node);

   my_source.activate();

   g.wait_for_all();

   print << endl;
}

void test_graph_better()
{
    // SpinScoped MutexLock
   flow::graph g;
   constexpr int N = 100;

   auto inputter = [N,state = int{}](tbb::flow_control& fc) mutable ->int
   {
        if(state < N)
        {
            return state++;
        }
        else
        {
            fc.stop();
            return int{}; // this is for C++ compiler's complaints,or keep C++ compiler silient.
        }

   };

   flow::input_node my_source(g,inputter);

    tbb::spin_mutex mutex;
    auto parallel_body = [&mutex](int i)
    {
        tbb::spin_mutex::scoped_lock lock{mutex};
        print << i << nl;
    };

   flow::function_node my_node(g,flow::unlimited,parallel_body);
   
   //edge represents data stream flow.
   flow::make_edge(my_source,my_node);

   my_source.activate();

   g.wait_for_all();

   print << endl;
}

int main()
{
    test_graph_better();
    //test_graph_basic();
}

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