From 5baff1f33db9612282e2cc954491e86fb088475c Mon Sep 17 00:00:00 2001 From: Hu Chunming <2657262686@qq.com> Date: Thu, 8 Aug 2024 15:52:13 +0800 Subject: [PATCH] 优化aclrtDestroyStream --- .vscode/launch.json | 4 ++-- src/ai_engine_module/VPTProcess.cpp | 7 ++++--- src/decoder/dvpp/VpcUtils.cpp | 26 +++++++++++++++++++++----- src/decoder/dvpp/VpcUtils.h | 1 - src/util/JpegUtil.cpp | 19 ++++++++++--------- src/util/JpegUtil.h | 1 - 6 files changed, 37 insertions(+), 21 deletions(-) diff --git a/.vscode/launch.json b/.vscode/launch.json index fc1503d..1b129be 100644 --- a/.vscode/launch.json +++ b/.vscode/launch.json @@ -104,8 +104,8 @@ "args": [], "stopAtEntry": false, "externalConsole": false, - "cwd": "/home/cmhu/vpt_ascend_arm/src/demo", - "program": "/home/cmhu/vpt_ascend_arm/src/demo/build/Debug/outDebug", + "cwd": "/home/cmhu/vpt_ascend_arm/src/decoder/dvpp", + "program": "/home/cmhu/vpt_ascend_arm/src/decoder/dvpp/build/Debug/outDebug", "MIMode": "gdb", "miDebuggerPath": "gdb", "setupCommands": [ diff --git a/src/ai_engine_module/VPTProcess.cpp b/src/ai_engine_module/VPTProcess.cpp index 40d276e..25c83a5 100755 --- a/src/ai_engine_module/VPTProcess.cpp +++ b/src/ai_engine_module/VPTProcess.cpp @@ -70,6 +70,10 @@ int VPTProcess::process_gpu(sy_img * batch_img, vector& tasklist, } do{ + + aclrtSetDevice(m_devId); + int ret = aclrtSetCurrentContext(m_algorthim_ctx); + /* 路数太多时 按照最大batchsize数 拆批次运行 */ int cur_batch_size = m_max_batchsize; int cycleTimes = batchsize / cur_batch_size + (batchsize % cur_batch_size == 0 ? 0 : 1); @@ -80,9 +84,6 @@ int VPTProcess::process_gpu(sy_img * batch_img, vector& tasklist, int startbatch = c*cur_batch_size; vpt_result *real_res = vpt_det_result + startbatch; - - // aclrtSetDevice(m_devId); - int ret = aclrtSetCurrentContext(m_algorthim_ctx); if(ACL_SUCCESS != ret){ break; } diff --git a/src/decoder/dvpp/VpcUtils.cpp b/src/decoder/dvpp/VpcUtils.cpp index aec7c52..84dde71 100644 --- a/src/decoder/dvpp/VpcUtils.cpp +++ b/src/decoder/dvpp/VpcUtils.cpp @@ -17,10 +17,6 @@ VpcUtils::VpcUtils(){ } VpcUtils::~VpcUtils(){ - if(nullptr != stream_){ - aclrtDestroyStream(stream_); - } - if(context_){ aclrtDestroyContext(context_); } @@ -40,7 +36,6 @@ int VpcUtils::init(int devId){ aclrtCreateContext(&context_, m_devId); CHECK_AND_RETURN(aclrtSetCurrentContext(context_), "aclrtSetCurrentContext failed"); - CHECK_AND_RETURN(aclrtCreateStream(&stream_), "aclrtCreateStream failed! "); dvppChannelDesc_ = acldvppCreateChannelDesc(); @@ -79,6 +74,8 @@ DvppDataMemory* VpcUtils::convert2bgr(acldvppPicDesc *inputDesc_, int out_width, acldvppSetPicDescSize(outputDesc_, out_buf_size); aclError ret = ACL_ERROR_NONE; + aclrtStream stream_{nullptr}; + aclrtCreateStream(&stream_); do{ // 9. 执行异步色域转换,再调用aclrtSynchronizeStream接口阻塞程序运行,直到指定Stream中的所有任务都完成 ret = acldvppVpcConvertColorAsync(dvppChannelDesc_, inputDesc_, outputDesc_, stream_); @@ -93,6 +90,11 @@ DvppDataMemory* VpcUtils::convert2bgr(acldvppPicDesc *inputDesc_, int out_width, } }while(0); + if(nullptr != stream_){ + aclrtDestroyStream(stream_); + stream_ = nullptr; + } + acldvppDestroyPicDesc(outputDesc_); if(ret != ACL_ERROR_NONE){ @@ -138,6 +140,8 @@ DvppDataMemory* VpcUtils::convert2bgr(DvppDataMemory* inMem){ acldvppSetPicDescSize(outputDesc_, out_buf_size); aclError ret = ACL_ERROR_NONE; + aclrtStream stream_{nullptr}; + aclrtCreateStream(&stream_); do{ // 9. 执行异步色域转换,再调用aclrtSynchronizeStream接口阻塞程序运行,直到指定Stream中的所有任务都完成 ret = acldvppVpcConvertColorAsync(dvppChannelDesc_, inputDesc_, outputDesc_, stream_); @@ -152,6 +156,11 @@ DvppDataMemory* VpcUtils::convert2bgr(DvppDataMemory* inMem){ } }while(0); + if(nullptr != stream_){ + aclrtDestroyStream(stream_); + stream_ = nullptr; + } + acldvppDestroyPicDesc(outputDesc_); acldvppDestroyPicDesc(inputDesc_); @@ -187,6 +196,8 @@ DvppDataMemory* VpcUtils::resize(acldvppPicDesc *inputDesc_, int out_width, int acldvppResizeConfig *resizeConfig_ = acldvppCreateResizeConfig(); aclError ret = ACL_ERROR_NONE; + aclrtStream stream_{nullptr}; + aclrtCreateStream(&stream_); do{ // 9. 执行异步色域转换,再调用aclrtSynchronizeStream接口阻塞程序运行,直到指定Stream中的所有任务都完成 ret = acldvppVpcResizeAsync(dvppChannelDesc_, inputDesc_, outputDesc_, resizeConfig_, stream_); @@ -201,6 +212,11 @@ DvppDataMemory* VpcUtils::resize(acldvppPicDesc *inputDesc_, int out_width, int } }while(0); + if(nullptr != stream_){ + aclrtDestroyStream(stream_); + stream_ = nullptr; + } + acldvppDestroyResizeConfig(resizeConfig_); acldvppDestroyPicDesc(outputDesc_); diff --git a/src/decoder/dvpp/VpcUtils.h b/src/decoder/dvpp/VpcUtils.h index ae8dcde..2c5e781 100644 --- a/src/decoder/dvpp/VpcUtils.h +++ b/src/decoder/dvpp/VpcUtils.h @@ -16,7 +16,6 @@ public: private: aclrtContext context_{nullptr}; - aclrtStream stream_{nullptr}; int m_devId; acldvppChannelDesc *dvppChannelDesc_ {nullptr}; string m_dec_name; diff --git a/src/util/JpegUtil.cpp b/src/util/JpegUtil.cpp index e28284b..2fcc63a 100755 --- a/src/util/JpegUtil.cpp +++ b/src/util/JpegUtil.cpp @@ -14,7 +14,6 @@ int JpegUtil::jpeg_init(int32_t devId){ /* 2.Run the management resource application, including Device, Context, Stream */ aclrtSetDevice(deviceId_); aclrtCreateContext(&context_, deviceId_); - aclrtCreateStream(&stream_); // channel 准备 dvppChannelDesc_ = acldvppCreateChannelDesc(); @@ -35,14 +34,6 @@ void JpegUtil::jpeg_release(){ 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) { @@ -96,6 +87,8 @@ bool JpegUtil::jpeg_encode(acldvppPicDesc *encodeInputDesc_, string out_file_nam } bool bRet = false; + aclrtStream stream_{nullptr}; + aclrtCreateStream(&stream_); do { // 9. 执行异步编码,再调用aclrtSynchronizeStream接口阻塞程序运行,直到指定Stream中的所有任务都完成 aclRet = acldvppJpegEncodeAsync(dvppChannelDesc_, encodeInputDesc_, encodeOutBufferDev_, &outBufferSize, jpegeConfig_, stream_); @@ -136,6 +129,14 @@ 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_); 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_ ; }; -- libgit2 0.21.4