更新时间:2021-03-18 GMT+08:00
分享

C语言接口使用示例

/**
* @file graph_c_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 <stdlib.h>
#include <string.h>

#include "hiaiengine/c_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 to create and start graph
*  3) set profile config if necessary
*  4) call HIAI_DestoryGraph 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 const char graph_config_proto_file[] = "llt/hiaiengine/st/examples/config/graph.prototxt";
static const char graph_config_len = sizeof(graph_config_proto_file);

// Define End

// 用户自定义数据接收(recv_data 由框架自动释放)
HIAI_StatusT UserDefDataRecvCallBack(void* recv_data)
{
    // 处理接收到的数据
    return HIAI_OK;
}


/*************************************************************************
* 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_CreateGraph(graph_config_proto_file, graph_config_len);
    if (status != HIAI_OK)
    {
        return status;
    }

    // Step3: 设置回调函数用于接收数据
    HIAI_PortID_t engine_id;
    engine_id.graph_id = g_graph_id;
    engine_id.engine_id = dest_engine_id;
    engine_id.port_id = 0;
    HIAI_SetDataRecvFunctor(engine_id, UserDefDataRecvCallBack);

    return HIAI_OK;
}


// User Application Main Example
int main()
{
    HIAI_StatusT ret = HIAI_OK;

    // 创建流程
    ret = HIAI_InitAndStartGraph();

    if (HIAI_OK != ret)
    {
        return -1;
    }

    // 读取数据,往流程灌输数据
    HIAI_PortID_t engine_id;
    engine_id.graph_id = g_graph_id;
    engine_id.engine_id = src_engine_id;
    engine_id.port_id = 0;

    int is_end_of_data = 0;
    while (!is_end_of_data)
    {
        UserDefDataTypeT* user_def_msg = (UserDefDataTypeT*)malloc(sizeof(UserDefDataTypeT));

        // 读取数据并填充 UserDefDataType,此数据类型注册见数据类型注册章节

        const char * message_name = "UserDefDataType";
        size_t msg_len = strlen(message_name);
        if (HIAI_OK != HIAI_C_SendData(engine_id, message_name, msg_len, user_def_msg))
        {
            // 队列满时返回发送数据失败,由业务逻辑处理是否重发或丢弃
            break;
        }

        free(user_def_msg);
        user_def_msg = nullptr;
    }

    // 处理结束时,删除整个Graph
    HIAI_DestroyGraph(g_graph_id);
    return 0;
}
分享:

    相关文档

    相关产品