Running a Graph Synchronously
This section describes the main APIs involved and calling examples.
Overview
This feature is not supported by the
This section describes how to build and run a constructed graph to generate a result. The following figure illustrates the API call sequence.

- GEInitializeV2: initializes the system and allocates resources. (This API can be called before graph construction.)
- GeSession constructor: creates a Session object and allocates Session resources.
- AddGraph: adds a graph to the Session object.
- RunGraph: runs the graph.
- GEFinalizeV2: destroys system allocations.
Example
- Include the header file.
1#include "ge_api_v2.h"
- Allocate system resources.
After a graph is defined, call GEInitializeV2 to initialize the system (or call it before defining a graph) and allocate system resources. See the following code snippet.
1 2 3
std::map<AscendString, AscendString>config = {{"ge.exec.deviceId", "0"}, {"ge.graphRunMode", "1"}}; Status ret = ge::GEInitializeV2(config);
Set the GE initialization configuration by using config. Configure ge.exec.deviceId to specify the device where a GE instance runs, and ge.graphRunMode to specify the graph run mode (set to 0 for online inference and 1 for training). For more configurations, see Command-Line Options.
- Add a graph object and run the graph.
To run a graph, create the Session object, call the AddGraph API to add the graph, and then call the RunGraph API to run the graph. See the following code snippet.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40
std::map<AscendString, AscendString>options; // Create a session object. ge::GeSession *session = new GeSession(options); // Check whether the session is created successfully. if(session == nullptr) { std::cout << "Create session failed." << std::endl; // ... // ... // Destroy allocations. ge::GEFinalizeV2(); return FAILED; } // Prepare the graph ID to be added to the session and create an empty graph object. uint32_t conv_graph_id = 0; ge::Graph conv_graph; // Add the graph to the session. Status ret = session->AddGraph(conv_graph_id, conv_graph); if(ret != SUCCESS) { // ... // ... // Destroy allocations. ge::GEFinalizeV2(); delete session; return FAILED; } // Prepare input and output tensors. std::vector<gert::Tensor> input_cov; std::vector<gert::Tensor> output_cov; // Run the graph with the specified ID. ret = session->RunGraph(conv_graph_id, input_cov, output_cov); if(ret != SUCCESS) { // ... // ... // Destroy allocations and the session. ge::GEFinalizeV2(); delete session; return FAILED; }
Set the run configuration by using options. For details, see the Session constructor.
The graph execution result will be saved to the output_cov tensor.
- Call GEFinalizeV2 to destroy allocations.
1ret = ge::GEFinalizeV2();