Graph Construction by Using DataFlow APIs

  1. Construct FlowGraph.
    Use FlowNode and FunctionPp to construct a graph. The following is an example:
     1
     2
     3
     4
     5
     6
     7
     8
     9
    10
    11
    12
    13
    // Construct FlowGraph of DataFlow.
    dflow::FlowGraph flow_graph("flow_graph");
    
    // Construct the input node.
    auto data0 = dflow::FlowData("Data0", 0);
    auto data1 = dflow::FlowData("Data1", 1);
    
    auto node0 = dflow::FlowNode("node0", 2, 1).SetInput(0, data0).SetInput(1, data1);
    // function_pp
    auto pp0 = dflow::FunctionPp("func_pp0")
                   .SetCompileConfig("./add_func.json")
                   .SetInitParam("out_type", ge::DT_UINT32);
    node0.AddPp(&pp0);
    
  2. Run the graph.
    You can use either of the following modes:
    • Session run mode. The following is an example:
       1
       2
       3
       4
       5
       6
       7
       8
       9
      10
      // Create a session.
      ge::Session* session1 = new Session(options);
      assert(session1 != NULL);
      ret = session1->AddGraph(graphId, graph);
      if (!ret) { return; }
      // Run the graph.
      ret = session1->RunGraph(graphId, input, output);
      if (!ret) { return; }
      GEFinalize();
      return;
      
    • DataFlow mode. The following is an example:
       1
       2
       3
       4
       5
       6
       7
       8
       9
      10
      11
      12
      13
      14
      15
      16
      17
      18
      19
      20
      21
      // Execute the graph using the asynchronous DataFlow API.
      ge::DataFlowInfo dataFlowInfo;
      dataFlowInfo.SetStartTime(0);
      dataFlowInfo.SetEndTime(5);
      std::vector<ge::Tensor> inputsData = {inputTensor, inputTensor};
      geRet = session->FeedDataFlowGraph(0, inputsData, dataFlowInfo, 3000);
      if (geRet != ge::SUCCESS)
      {
      	std::cout << "TEST====Feed input data failed============." << std::endl;
      	ge::GEFinalize();
      	return -1;
      }
      // Obtain outputs.
      std::vector<ge::Tensor> outputsData;
      geRet = session->FetchDataFlowGraph(0, outputsData, dataFlowInfo, 3000);
      if (geRet != ge::SUCCESS)
      {
      	std::cout << "TEST====Fetch output data failed." << std::endl;
      	ge::GEFinalize();
      	return -1;
      }