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

性能优化传输示例

(1)使用性能优化方案传输数据,必须对要传输的数据进行手动序列化和反序列化:
// 注:序列化函数在发送端使用,反序列化在接收端使用,所以这个注册函数最好在接收端和发送端都注册一遍;

   结构体
typedef struct
{ 
    uint32_t frameId;
    uint8_t  bufferId;
    uint8_t* image_data;
    uint8_t  image_size;
}TEST_STR;
 
// 序列化TEST_STR结构体,该函数只需要调动注册函数进行注册,参数说明:
// 输入:inputPtr,TEST_STR结构体指针
// 输出:ctrlStr,控制信息地址
         imageData,数据信息指针
         imageLen,数据信息大小
void GetTestStrSearPtr(void* inputPtr, std::string& ctrlStr, uint8_t*& imageData, uint32_t& imageLen)
{
    // 获取结构体buffer
    TEST_STR* test_str = (TEST_STR*)inputPtr;
    ctrlStr = std::string((char*)inputPtr, sizeof(TEST_STR));
 
    // 获取数据信息,直接将结构体的数据指针赋值传递
    imageData = (uint8_t*)test_str->image_data;
    imageLen = test_str->image_size;
}
 
// 反序列化结构体,回传回来的将是结构体buffer和数据块buffer
// 输入:ctrlPtr,控制信息地址
         ctrlLen,控制信息大小
         imageData,数据信息指针
         imageLen,数据信息大小
// 输出:std::shared_ptr<void>,指向结构体的智能指针
std::shared_ptr<void> GetTestStrDearPtr(const char* ctrlPtr, const uint32_t& ctrlLen,const uint8_t* imageData, const uint32_t& imageLen)
{
    // 获取结构体
    TEST_STR* test_str = (TEST_STR*)ctrlPtr;
 
    // 获取传输过来的大内存数据
    // 注:大内存数据最好赋值给智能指针,并注册删除器,如果不是使用智能指针或者没有注册删除器,则需要在释放该内存时手动调用hiai::Graph::ReleaseDataBuffer(void* ptr);
    std::shared_ptr<TEST_STR<uint8_t>> shared_data = std::make_shared<TEST_STR<uint8_t>>();
    shared_data->frameId = test_str->frame_ID;
    shared_data->bufferId= test_str->bufferId;
    shared_data->image_size = imageLen;
    shared_data->image_data.reset(imageData, hiai::Graph::ReleaseDataBuffer);
 
    // 返回智能指针给到Device端Engine使用
    return std::static_pointer_cast<void>(shared_data);
}
// 注册序列化反序列化函数
HIAI_REGISTER_SERIALIZE_FUNC("TEST_STR", TEST_STR, GetTestStrSearPtr, GetTestStrDearPtr);
 
(2) 在发送数据时,需要使用注册的数据类型,另外配合使用HIAI_DMalloc分配数据内存,可以使性能更优
    注:在从host侧向Device侧搬运数据时,使用HIAI_DMalloc方式将很大的提供传输效率,建议优先使用HIAI_DMalloc,该内存接口目前支持0 – (256M Bytes - 96 Bytes)的数据大小,如果数据超出该范围,则需要使用malloc接口进行分配;
    // 使用Dmalloc接口申请数据内存,10000为时延,为10000毫秒,表示如果内存不足,等待10000毫秒;
    HIAI_StatusT get_ret =  HIAIMemory::HIAI_DMalloc(width*align_height*3/2,(void*&)align_buffer, 10000);
 
    // 发送数据,调用该接口后无需调用HIAI_Dfree接口,10000为时延
    graph->SendData(engine_id_0, "TEST_STR", std::static_pointer_cast<void>(align_buffer), 10000);
分享:

    相关文档

    相关产品