更新时间:2021-03-18 GMT+08:00
C++接口使用示例(根据配置文件创建Graph)
/** * @file graph_cplusplus_api_example.cpp * * Copyright(C), 2017 - 2017, Huawei Tech. Co., Ltd. ALL RIGHTS RESERVED. * * @Source Files for HIAI Graph Orchestration * * @version 1.0 * */ #include <unistd.h> #include <thread> #include <fstream> #include <algorithm> #include "hiaiengine/api.h" #include "user_def_data_type.h" /************************************************************************** * Normally the mandatory steps to setup the whole HIAI graph include: * 1) call HIAI_Init() API to perform global system initialization; * 2) create HIAI_CreateGraph or Graph::CreateGraph to create and start graph * 3) set profile config if necessary * 4) call HIAI_DestoryGraph or Graph::DestroyGraph to destroy the graph finally * ***************************************************************************/ // Just define variables which are used in only this example // and are not required in real user case. #define MATRIX_USER_SPECIFY_ENGINE_PRIORITY (10) static uint32_t g_graph_id = 100; static uint32_t src_engine_id = 1000; // 定义在graph.prototxt static uint32_t dest_engine_id = 1002; // 定义在graph.prototxt static std::string graph_config_proto_file = "./config/graph.prototxt"; // Define End namespace hiai { class GraphDataRecvInterface: public DataRecvInterface { public: GraphDataRecvInterface() { } /** * @ingroup hiaiengine * @brief 读取数据,保存 * @param [in]输入数据 * @return HIAI Status */ HIAI_StatusT RecvData(const std::shared_ptr<void>& message) { // 转换成具体的消息类型,并处理相关消息, 例如: // shared_ptr<std::string> data = // std::static_pointer_cast<std::string>(message); return HIAI_OK; } private: }; } /************************************************************************* * HIAIEngine Example * Graph: * SrcEngine<-->Engine<-->DestEngine * *************************************************************************/ HIAI_StatusT HIAI_InitAndStartGraph() { // Step1: Global System Initialization before using Matrix HIAI_StatusT status = HIAI_Init(0); // Step2: Create and Start the Graph status = hiai::Graph::CreateGraph(graph_config_proto_file); if (status != HIAI_OK) { HIAI_ENGINE_LOG(status, "Fail to start graph"); return status; } // Step3: 设置回调函数用于接收数据 std::shared_ptr<hiai::Graph> graph = hiai::Graph::GetInstance(g_graph_id); if (nullptr == graph) { HIAI_ENGINE_LOG("Fail to get the graph-%u", g_graph_id); return status; } // Specify the port id (default to zero) hiai::EnginePortID target_port_config; target_port_config.graph_id = g_graph_id; target_port_config.engine_id = dest_engine_id; target_port_config.port_id = 0; graph->SetDataRecvFunctor(target_port_config, std::shared_ptr<hiai::GraphDataRecvInterface>( new hiai::GraphDataRecvInterface())); return HIAI_OK; } // User Application Main Example int main() { HIAI_StatusT ret = HIAI_OK; // 创建流程 ret = HIAI_InitAndStartGraph(); if(HIAI_OK != ret) { HIAI_ENGINE_LOG("Fail to start graph");; return -1; } // 读取数据,往流程灌输数据 std::shared_ptr<hiai::Graph> graph = hiai::Graph::GetInstance(g_graph_id); if (nullptr == graph) { HIAI_ENGINE_LOG("Fail to get the graph-%u", g_graph_id); return -1; } hiai::EnginePortID engine_id; engine_id.graph_id = g_graph_id; engine_id.engine_id = src_engine_id; engine_id.port_id = 0; bool is_end_of_data = false; while (!is_end_of_data) { std::shared_ptr<UserDefDataType> user_def_msg(new UserDefDataType); // 读取数据并填充 UserDefDataType,此数据类型注册见数据类型注册章节 // 往Graph灌输数据 if (HIAI_OK != graph->SendData(engine_id, "UserDefDataType", std::static_pointer_cast<void>(user_def_msg))) { // 队列满时返回发送数据失败,由业务逻辑处理是否重发或丢弃 HIAI_ENGINE_LOG("Fail to send data to the graph-%u", g_graph_id); break; } } // 处理结束时,删除整个Graph hiai::Graph::DestroyGraph(g_graph_id); return 0; }
父主题: 接口使用示例