From 1680c040e3b8b0e45268f4d82612d3da1196b63f Mon Sep 17 00:00:00 2001 From: Hu Chunming <2657262686@qq.com> Date: Mon, 12 Aug 2024 18:36:15 +0800 Subject: [PATCH] setDevice改用Context --- src/ai_platform/MultiSourceProcess.cpp | 1 - src/decoder/dvpp/DvppDecoder.cpp | 38 +++++++++++++++++++++++--------------- src/decoder/dvpp/DvppDecoder.h | 1 + src/decoder/dvpp/DvppStreamDecoder.cpp | 6 +++--- src/util/JpegUtil.cpp | 28 ++++++++++------------------ src/util/JpegUtil.h | 1 - src/util/vpc_util.cpp | 10 +--------- test/main.cpp | 7 ------- test/vpc_test.cpp1 | 6 ------ 9 files changed, 38 insertions(+), 60 deletions(-) diff --git a/src/ai_platform/MultiSourceProcess.cpp b/src/ai_platform/MultiSourceProcess.cpp index d525905..ecffb89 100755 --- a/src/ai_platform/MultiSourceProcess.cpp +++ b/src/ai_platform/MultiSourceProcess.cpp @@ -818,7 +818,6 @@ int CMultiSourceProcess::algorthim_process_thread(){ } aclrtDestroyContext(ctx); - aclrtResetDevice(m_devId); LOG_INFO("algorthim_process_thread exit."); return 0; diff --git a/src/decoder/dvpp/DvppDecoder.cpp b/src/decoder/dvpp/DvppDecoder.cpp index 4117a97..2f1e902 100644 --- a/src/decoder/dvpp/DvppDecoder.cpp +++ b/src/decoder/dvpp/DvppDecoder.cpp @@ -332,6 +332,12 @@ int DvppDecoder::getVdecType(int videoType, int profile) post_decoded_cbk = cfg.post_decoded_cbk; do{ + aclError ret = aclrtCreateContext(&m_context, m_dvpp_deviceId); + if (ret != ACL_ERROR_NONE) { + LOG_ERROR("[{}]-aclrtCreateContext failed !", m_dec_name); + break; + } + // DvppSourceManager 创建时包含 aclInit,析构时包含 aclFinalize DvppSourceManager* pSrcMgr = DvppSourceManager::getInstance(); m_dvpp_channel = pSrcMgr->getChannel(m_dvpp_deviceId); @@ -434,9 +440,9 @@ static int snap_count = 0; DeviceMemory* DvppDecoder::snapshot(){ - aclError ret = aclrtSetDevice(m_dvpp_deviceId); + int ret = aclrtSetCurrentContext(m_context); if(ret != ACL_ERROR_NONE){ - LOG_ERROR("[{}]-aclrtSetDevice failed !", m_dec_name); + LOG_ERROR("[{}]- aclrtSetCurrentContext failed", m_dec_name); return nullptr; } @@ -465,12 +471,6 @@ DeviceMemory* DvppDecoder::snapshot(){ break; } - ret = aclrtResetDevice(m_dvpp_deviceId); - if(ret != ACL_ERROR_NONE){ - LOG_ERROR("[{}]-aclrtResetDevice failed !", m_dec_name); - return nullptr; - } - return snapshot_mem; } @@ -515,11 +515,15 @@ void DvppDecoder::read_thread() { this ); - CHECK_AND_RETURN_NOVALUE(aclrtSetDevice(m_dvpp_deviceId), "aclrtSetDevice failed!"); - aclvdecChannelDesc *vdecChannelDesc = nullptr; do { + int ret = aclrtSetCurrentContext(m_context); + if(ret != ACL_ERROR_NONE){ + LOG_ERROR("[{}]- aclrtSetCurrentContext failed", m_dec_name); + break; + } + vdecChannelDesc = aclvdecCreateChannelDesc(); if (vdecChannelDesc == nullptr) { LOG_ERROR("[{}]- aclvdecCreateChannelDesc failed", m_dec_name); @@ -639,8 +643,6 @@ void DvppDecoder::read_thread() { vdecChannelDesc = nullptr; } - CHECK_NOT_RETURN(aclrtResetDevice(m_dvpp_deviceId), "aclrtResetDevice failed"); - m_bRunning=false; m_bExitReportThd = true; @@ -783,7 +785,7 @@ void DvppDecoder::doVdppVdecCallBack(acldvppStreamDesc *input, acldvppPicDesc *o m_out_count++; - CHECK_AND_RETURN_NOVALUE(aclrtSetDevice(m_dvpp_deviceId), "aclrtSetDevice failed"); + CHECK_AND_RETURN_NOVALUE(aclrtSetCurrentContext(m_context), "aclrtSetCurrentContext failed"); void *inputDataDev = acldvppGetStreamDescData(input); acldvppFree(inputDataDev); @@ -857,8 +859,6 @@ void DvppDecoder::doVdppVdecCallBack(acldvppStreamDesc *input, acldvppPicDesc *o CHECK_NOT_RETURN(acldvppDestroyStreamDesc(input), "acldvppDestroyStreamDesc failed"); CHECK_NOT_RETURN(acldvppDestroyPicDesc(output), "acldvppDestroyPicDesc failed"); - - CHECK_NOT_RETURN(aclrtResetDevice(m_dvpp_deviceId), "aclrtResetDevice failed"); } bool DvppDecoder::sendVdecEos(aclvdecChannelDesc *vdecChannelDesc) { @@ -930,6 +930,14 @@ void DvppDecoder::display_thread() { } void DvppDecoder::release_dvpp(){ + if(m_context){ + aclError ret = aclrtDestroyContext(m_context); + if(ret != ACL_ERROR_NONE){ + LOG_ERROR("[{}]- aclrtDestroyContext failed !", m_dec_name); + } + m_context = nullptr; + } + if(m_dvpp_channel >= 0){ DvppSourceManager* pSrcMgr = DvppSourceManager::getInstance(); pSrcMgr->releaseChannel(m_dvpp_deviceId, m_dvpp_channel); diff --git a/src/decoder/dvpp/DvppDecoder.h b/src/decoder/dvpp/DvppDecoder.h index 1c0b9dc..35ce529 100644 --- a/src/decoder/dvpp/DvppDecoder.h +++ b/src/decoder/dvpp/DvppDecoder.h @@ -117,6 +117,7 @@ private: // 解码 int m_dvpp_deviceId {-1}; int m_dvpp_channel {-1}; + aclrtContext m_context{nullptr}; acldvppStreamFormat m_enType; const void * m_postDecArg {nullptr}; diff --git a/src/decoder/dvpp/DvppStreamDecoder.cpp b/src/decoder/dvpp/DvppStreamDecoder.cpp index 921ab20..03b3114 100644 --- a/src/decoder/dvpp/DvppStreamDecoder.cpp +++ b/src/decoder/dvpp/DvppStreamDecoder.cpp @@ -120,7 +120,7 @@ int DvppStreamDecoder::getVdecType(int videoType) void DvppStreamDecoder::doProcessReport(){ aclrtContext ctx; - ret = aclrtCreateContext(&ctx, m_deviceId); + aclError ret = aclrtCreateContext(&ctx, m_deviceId); if (ret != ACL_ERROR_NONE) { // cout << "aclrtCreateContext failed " << endl; LOG_ERROR("aclrtCreateContext failed !"); @@ -152,7 +152,7 @@ void DvppStreamDecoder::doVdppVdecCallBack(acldvppStreamDesc *input, acldvppPicD m_out_count++; - CHECK_AND_RETURN_NOVALUE(aclrtSetDevice(m_dvpp_deviceId), "aclrtSetDevice failed"); + CHECK_AND_RETURN_NOVALUE(aclrtSetDevice(m_deviceId), "aclrtSetDevice failed"); void *inputDataDev = acldvppGetStreamDescData(input); acldvppFree(inputDataDev); @@ -238,7 +238,7 @@ void DvppStreamDecoder::doVdppVdecCallBack(acldvppStreamDesc *input, acldvppPicD CHECK_NOT_RETURN(acldvppDestroyStreamDesc(input), "acldvppDestroyStreamDesc failed"); CHECK_NOT_RETURN(acldvppDestroyPicDesc(output), "acldvppDestroyPicDesc failed"); - CHECK_NOT_RETURN(aclrtResetDevice(m_dvpp_deviceId), "aclrtResetDevice failed"); + CHECK_NOT_RETURN(aclrtResetDevice(m_deviceId), "aclrtResetDevice failed"); } DvppDataMemory* DvppStreamDecoder::GetFrame() { diff --git a/src/util/JpegUtil.cpp b/src/util/JpegUtil.cpp index e28284b..655f5af 100755 --- a/src/util/JpegUtil.cpp +++ b/src/util/JpegUtil.cpp @@ -12,9 +12,7 @@ int JpegUtil::jpeg_init(int32_t devId){ aclError ret; /* 2.Run the management resource application, including Device, Context, Stream */ - aclrtSetDevice(deviceId_); aclrtCreateContext(&context_, deviceId_); - aclrtCreateStream(&stream_); // channel 准备 dvppChannelDesc_ = acldvppCreateChannelDesc(); @@ -28,21 +26,12 @@ int JpegUtil::jpeg_init(int32_t devId){ void JpegUtil::jpeg_release(){ aclError ret; - ret = aclrtSetDevice(deviceId_); aclrtSetCurrentContext(context_); ret = acldvppDestroyChannel(dvppChannelDesc_); ret = acldvppDestroyChannelDesc(dvppChannelDesc_); dvppChannelDesc_ = nullptr; - if (stream_ != nullptr) { - ret = aclrtDestroyStream(stream_); - if (ret != ACL_SUCCESS) { - LOG_ERROR("destroy stream failed"); - } - stream_ = nullptr; - } - acldvppDestroyJpegeConfig(jpegeConfig_); if (context_ != nullptr) { @@ -52,11 +41,6 @@ void JpegUtil::jpeg_release(){ } context_ = nullptr; } - - ret = aclrtResetDevice(deviceId_); - if (ret != ACL_SUCCESS) { - LOG_ERROR("reset device failed"); - } } int32_t JpegUtil::jpege_save(char* pcData , uint32_t dataLen, string out_file_name) @@ -78,7 +62,6 @@ int32_t JpegUtil::jpege_save(char* pcData , uint32_t dataLen, string out_file_na bool JpegUtil::jpeg_encode(acldvppPicDesc *encodeInputDesc_, string out_file_name) { aclError aclRet ; - aclRet = aclrtSetDevice(deviceId_); aclrtSetCurrentContext(context_); // 8. 申请输出内存,申请Device内存encodeOutBufferDev_,存放编码后的输出数据 @@ -96,6 +79,8 @@ bool JpegUtil::jpeg_encode(acldvppPicDesc *encodeInputDesc_, string out_file_nam } bool bRet = false; + aclrtStream stream_; + aclrtCreateStream(&stream_); do { // 9. 执行异步编码,再调用aclrtSynchronizeStream接口阻塞程序运行,直到指定Stream中的所有任务都完成 aclRet = acldvppJpegEncodeAsync(dvppChannelDesc_, encodeInputDesc_, encodeOutBufferDev_, &outBufferSize, jpegeConfig_, stream_); @@ -136,10 +121,17 @@ bool JpegUtil::jpeg_encode(acldvppPicDesc *encodeInputDesc_, string out_file_nam bRet = true; } while (0); + + if (stream_ != nullptr) { + ret = aclrtDestroyStream(stream_); + if (ret != ACL_SUCCESS) { + LOG_ERROR("destroy stream failed"); + } + stream_ = nullptr; + } // 释放掉输入输出的device内存 (void)acldvppFree(encodeOutBufferDev_); encodeOutBufferDev_ = nullptr; - aclRet = aclrtResetDevice(deviceId_); return bRet; } \ No newline at end of file diff --git a/src/util/JpegUtil.h b/src/util/JpegUtil.h index 8e74d42..ec2c096 100755 --- a/src/util/JpegUtil.h +++ b/src/util/JpegUtil.h @@ -23,7 +23,6 @@ private: private: int32_t deviceId_; aclrtContext context_; - aclrtStream stream_; acldvppChannelDesc *dvppChannelDesc_; acldvppJpegeConfig *jpegeConfig_ ; }; diff --git a/src/util/vpc_util.cpp b/src/util/vpc_util.cpp index 91b9c81..9515154 100755 --- a/src/util/vpc_util.cpp +++ b/src/util/vpc_util.cpp @@ -29,8 +29,7 @@ static uint32_t AlignSize(uint32_t origSize, uint32_t alignment){ void VPCUtil::release() { aclError ret; - // ret = aclrtSetDevice(deviceId_); - // aclrtSetCurrentContext(context_); + aclrtSetCurrentContext(context_); ret = acldvppDestroyChannel(dvppChannelDesc_); ret = acldvppDestroyChannelDesc(dvppChannelDesc_); @@ -44,10 +43,6 @@ void VPCUtil::release() } LOG_INFO("end to destroy context"); - ret = aclrtResetDevice(deviceId_); - if (ret != ACL_SUCCESS) { - LOG_ERROR("reset device failed"); - } LOG_INFO("end to reset device is %d", deviceId_); } @@ -108,7 +103,6 @@ vpc_img_info VPCUtil::crop(DeviceMemory *devMem, video_object_info obj) { } aclError ret; - aclrtSetDevice(deviceId_); ret = aclrtSetCurrentContext(context_); if (ret != ACL_SUCCESS) { LOG_ERROR("aclrtSetCurrentContext failed"); @@ -194,7 +188,6 @@ int VPCUtil::init(int32_t devId){ deviceId_ = devId; aclError ret; - aclrtSetDevice(deviceId_); aclrtCreateContext(&context_, deviceId_); // channel 准备 @@ -212,7 +205,6 @@ vector VPCUtil::crop_batch(DeviceMemory *devMem, vector