---
title: 同步运行Graph
description: "本节介绍同步运行Graph涉及的主要接口以及调用示例。"
url: https://www.hiascend.com/document/detail/zh/canncommercial/latest/programug/graphdevg/atlasag_25_0033.html
sourcePath: /source/zh/canncommercial/900/programug/graphdevg/atlasag_25_0033.html
indexId: 3d5df24008afe341bf81fcda24c3eea049eb6e88341607d6c7f015f291da4c6e70
---
# 同步运行Graph

本节介绍同步运行Graph涉及的主要接口以及调用示例。

#### 功能介绍

Atlas 200I/500 A2 推理产品 不支持该特性。

构建完Graph之后，如果您希望直接编译并运行Graph，得到图的运行结果，可以参考本节内容。涉及的主要接口如下，编译并运行Graph的Session样例可参考graph_run(https://gitee.com/ascend/samples/tree/master/cplusplus/level1_single_api/8_graphrun/graph_run)。


1. 调用  GEInitializeV2(https://www.hiascend.comdocument/detail/zh/canncommercial/900/API/ascendgraphapi/atlasgeapi_07_0176.html)
进行系统初始化（也可在Graph构建前调用），申请系统资源。
2. 调用  GeSession构造函数(https://www.hiascend.comdocument/detail/zh/canncommercial/900/API/ascendgraphapi/atlasgeapi_07_0282.html)
创建Session类对象，申请Session资源。
3. 调用  AddGraph(https://www.hiascend.comdocument/detail/zh/canncommercial/900/API/ascendgraphapi/atlasgeapi_07_0170.html)
在Session类对象中添加定义好的图。
4. 调用  RunGraph(https://www.hiascend.comdocument/detail/zh/canncommercial/900/API/ascendgraphapi/atlasgeapi_07_0185.html)
运行图。
5. 调用  GEFinalizeV2(https://www.hiascend.comdocument/detail/zh/canncommercial/900/API/ascendgraphapi/atlasgeapi_07_0173.html)
，释放系统资源。


#### 开发示例

1. 包含的头文件。
  1 #include "ge_api_v2.h"

2. 申请系统资源。
  Graph定义完成后，调用GEInitializeV2进行系统初始化（也可在Graph定义前调用），申请系统资源。示例代码如下：

  1 2 3 std::map<AscendString, AscendString>config = {{"ge.exec.deviceId", "0"}, {"ge.graphRunMode", "1"}}; Status ret = ge::GEInitializeV2(config);

  可以通过config配置传入ge运行的初始化信息，配置参数ge.exec.deviceId和ge.graphRunMode，分别用于指定GE实例运行设备，图执行模式（在线推理请配置为0，训练请配置为1）。更多配置请参考options参数说明(https://www.hiascend.comdocument/detail/zh/canncommercial/900/API/ascendgraphapi/atlasgeapi_07_0150.html)。

3. 添加Graph对象并运行Graph。
  若想使定义好的Graph运行起来，首先，要创建一个Session对象，然后调用AddGraph接口添加Graph，再调用RunGraph接口运行Graph。示例代码如下： 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; // 创建Session对象 ge::GeSession *session = new GeSession(options); // 判断Session是否创建成功 if(session == nullptr) { std::cout << "Create session failed." << std::endl; // ... // ... // 释放资源 ge::GEFinalizeV2(); return FAILED; } // 准备将要添加到Session的Graph ID，并创建空的Graph对象 uint32_t conv_graph_id = 0; ge::Graph conv_graph; // 将Graph添加到Session Status ret = session->AddGraph(conv_graph_id, conv_graph); if(ret != SUCCESS) { // ... // ... // 释放资源 ge::GEFinalizeV2(); delete session; return FAILED; } // 准备输入输出张量 std::vector<gert::Tensor> input_cov; std::vector<gert::Tensor> output_cov; // 运行指定ID的Graph ret = session->RunGraph(conv_graph_id, input_cov, output_cov); if(ret != SUCCESS) { // ... // ... // 释放资源并销毁Session ge::GEFinalizeV2(); delete session; return FAILED; }

  用户可以通过传入options配置图运行相关配置信息，相关配置请参考Session构造函数(https://www.hiascend.comdocument/detail/zh/canncommercial/900/API/ascendgraphapi/atlasgeapi_07_0282.html)。

  其中图运行完之后的数据保存在Tensor output_cov中。

4. 图运行完之后，通过GEFinalizeV2释放资源。
  1 ret = ge::GEFinalizeV2();
