Commit 1680c040e3b8b0e45268f4d82612d3da1196b63f

Authored by Hu Chunming
1 parent d17eebb2

setDevice改用Context

src/ai_platform/MultiSourceProcess.cpp
... ... @@ -818,7 +818,6 @@ int CMultiSourceProcess::algorthim_process_thread(){
818 818 }
819 819  
820 820 aclrtDestroyContext(ctx);
821   - aclrtResetDevice(m_devId);
822 821 LOG_INFO("algorthim_process_thread exit.");
823 822  
824 823 return 0;
... ...
src/decoder/dvpp/DvppDecoder.cpp
... ... @@ -332,6 +332,12 @@ int DvppDecoder::getVdecType(int videoType, int profile)
332 332 post_decoded_cbk = cfg.post_decoded_cbk;
333 333  
334 334 do{
  335 + aclError ret = aclrtCreateContext(&m_context, m_dvpp_deviceId);
  336 + if (ret != ACL_ERROR_NONE) {
  337 + LOG_ERROR("[{}]-aclrtCreateContext failed !", m_dec_name);
  338 + break;
  339 + }
  340 +
335 341 // DvppSourceManager 创建时包含 aclInit,析构时包含 aclFinalize
336 342 DvppSourceManager* pSrcMgr = DvppSourceManager::getInstance();
337 343 m_dvpp_channel = pSrcMgr->getChannel(m_dvpp_deviceId);
... ... @@ -434,9 +440,9 @@ static int snap_count = 0;
434 440  
435 441 DeviceMemory* DvppDecoder::snapshot(){
436 442  
437   - aclError ret = aclrtSetDevice(m_dvpp_deviceId);
  443 + int ret = aclrtSetCurrentContext(m_context);
438 444 if(ret != ACL_ERROR_NONE){
439   - LOG_ERROR("[{}]-aclrtSetDevice failed !", m_dec_name);
  445 + LOG_ERROR("[{}]- aclrtSetCurrentContext failed", m_dec_name);
440 446 return nullptr;
441 447 }
442 448  
... ... @@ -465,12 +471,6 @@ DeviceMemory* DvppDecoder::snapshot(){
465 471 break;
466 472 }
467 473  
468   - ret = aclrtResetDevice(m_dvpp_deviceId);
469   - if(ret != ACL_ERROR_NONE){
470   - LOG_ERROR("[{}]-aclrtResetDevice failed !", m_dec_name);
471   - return nullptr;
472   - }
473   -
474 474 return snapshot_mem;
475 475 }
476 476  
... ... @@ -515,11 +515,15 @@ void DvppDecoder::read_thread() {
515 515 this
516 516 );
517 517  
518   - CHECK_AND_RETURN_NOVALUE(aclrtSetDevice(m_dvpp_deviceId), "aclrtSetDevice failed!");
519   -
520 518 aclvdecChannelDesc *vdecChannelDesc = nullptr;
521 519  
522 520 do {
  521 + int ret = aclrtSetCurrentContext(m_context);
  522 + if(ret != ACL_ERROR_NONE){
  523 + LOG_ERROR("[{}]- aclrtSetCurrentContext failed", m_dec_name);
  524 + break;
  525 + }
  526 +
523 527 vdecChannelDesc = aclvdecCreateChannelDesc();
524 528 if (vdecChannelDesc == nullptr) {
525 529 LOG_ERROR("[{}]- aclvdecCreateChannelDesc failed", m_dec_name);
... ... @@ -639,8 +643,6 @@ void DvppDecoder::read_thread() {
639 643 vdecChannelDesc = nullptr;
640 644 }
641 645  
642   - CHECK_NOT_RETURN(aclrtResetDevice(m_dvpp_deviceId), "aclrtResetDevice failed");
643   -
644 646 m_bRunning=false;
645 647  
646 648 m_bExitReportThd = true;
... ... @@ -783,7 +785,7 @@ void DvppDecoder::doVdppVdecCallBack(acldvppStreamDesc *input, acldvppPicDesc *o
783 785  
784 786 m_out_count++;
785 787  
786   - CHECK_AND_RETURN_NOVALUE(aclrtSetDevice(m_dvpp_deviceId), "aclrtSetDevice failed");
  788 + CHECK_AND_RETURN_NOVALUE(aclrtSetCurrentContext(m_context), "aclrtSetCurrentContext failed");
787 789  
788 790 void *inputDataDev = acldvppGetStreamDescData(input);
789 791 acldvppFree(inputDataDev);
... ... @@ -857,8 +859,6 @@ void DvppDecoder::doVdppVdecCallBack(acldvppStreamDesc *input, acldvppPicDesc *o
857 859  
858 860 CHECK_NOT_RETURN(acldvppDestroyStreamDesc(input), "acldvppDestroyStreamDesc failed");
859 861 CHECK_NOT_RETURN(acldvppDestroyPicDesc(output), "acldvppDestroyPicDesc failed");
860   -
861   - CHECK_NOT_RETURN(aclrtResetDevice(m_dvpp_deviceId), "aclrtResetDevice failed");
862 862 }
863 863  
864 864 bool DvppDecoder::sendVdecEos(aclvdecChannelDesc *vdecChannelDesc) {
... ... @@ -930,6 +930,14 @@ void DvppDecoder::display_thread() {
930 930 }
931 931  
932 932 void DvppDecoder::release_dvpp(){
  933 + if(m_context){
  934 + aclError ret = aclrtDestroyContext(m_context);
  935 + if(ret != ACL_ERROR_NONE){
  936 + LOG_ERROR("[{}]- aclrtDestroyContext failed !", m_dec_name);
  937 + }
  938 + m_context = nullptr;
  939 + }
  940 +
933 941 if(m_dvpp_channel >= 0){
934 942 DvppSourceManager* pSrcMgr = DvppSourceManager::getInstance();
935 943 pSrcMgr->releaseChannel(m_dvpp_deviceId, m_dvpp_channel);
... ...
src/decoder/dvpp/DvppDecoder.h
... ... @@ -117,6 +117,7 @@ private:
117 117 // 解码
118 118 int m_dvpp_deviceId {-1};
119 119 int m_dvpp_channel {-1};
  120 + aclrtContext m_context{nullptr};
120 121 acldvppStreamFormat m_enType;
121 122  
122 123 const void * m_postDecArg {nullptr};
... ...
src/decoder/dvpp/DvppStreamDecoder.cpp
... ... @@ -120,7 +120,7 @@ int DvppStreamDecoder::getVdecType(int videoType)
120 120 void DvppStreamDecoder::doProcessReport(){
121 121  
122 122 aclrtContext ctx;
123   - ret = aclrtCreateContext(&ctx, m_deviceId);
  123 + aclError ret = aclrtCreateContext(&ctx, m_deviceId);
124 124 if (ret != ACL_ERROR_NONE) {
125 125 // cout << "aclrtCreateContext failed " << endl;
126 126 LOG_ERROR("aclrtCreateContext failed !");
... ... @@ -152,7 +152,7 @@ void DvppStreamDecoder::doVdppVdecCallBack(acldvppStreamDesc *input, acldvppPicD
152 152  
153 153 m_out_count++;
154 154  
155   - CHECK_AND_RETURN_NOVALUE(aclrtSetDevice(m_dvpp_deviceId), "aclrtSetDevice failed");
  155 + CHECK_AND_RETURN_NOVALUE(aclrtSetDevice(m_deviceId), "aclrtSetDevice failed");
156 156  
157 157 void *inputDataDev = acldvppGetStreamDescData(input);
158 158 acldvppFree(inputDataDev);
... ... @@ -238,7 +238,7 @@ void DvppStreamDecoder::doVdppVdecCallBack(acldvppStreamDesc *input, acldvppPicD
238 238 CHECK_NOT_RETURN(acldvppDestroyStreamDesc(input), "acldvppDestroyStreamDesc failed");
239 239 CHECK_NOT_RETURN(acldvppDestroyPicDesc(output), "acldvppDestroyPicDesc failed");
240 240  
241   - CHECK_NOT_RETURN(aclrtResetDevice(m_dvpp_deviceId), "aclrtResetDevice failed");
  241 + CHECK_NOT_RETURN(aclrtResetDevice(m_deviceId), "aclrtResetDevice failed");
242 242 }
243 243  
244 244 DvppDataMemory* DvppStreamDecoder::GetFrame() {
... ...
src/util/JpegUtil.cpp
... ... @@ -12,9 +12,7 @@ int JpegUtil::jpeg_init(int32_t devId){
12 12  
13 13 aclError ret;
14 14 /* 2.Run the management resource application, including Device, Context, Stream */
15   - aclrtSetDevice(deviceId_);
16 15 aclrtCreateContext(&context_, deviceId_);
17   - aclrtCreateStream(&stream_);
18 16  
19 17 // channel 准备
20 18 dvppChannelDesc_ = acldvppCreateChannelDesc();
... ... @@ -28,21 +26,12 @@ int JpegUtil::jpeg_init(int32_t devId){
28 26  
29 27 void JpegUtil::jpeg_release(){
30 28 aclError ret;
31   - ret = aclrtSetDevice(deviceId_);
32 29 aclrtSetCurrentContext(context_);
33 30  
34 31 ret = acldvppDestroyChannel(dvppChannelDesc_);
35 32 ret = acldvppDestroyChannelDesc(dvppChannelDesc_);
36 33 dvppChannelDesc_ = nullptr;
37 34  
38   - if (stream_ != nullptr) {
39   - ret = aclrtDestroyStream(stream_);
40   - if (ret != ACL_SUCCESS) {
41   - LOG_ERROR("destroy stream failed");
42   - }
43   - stream_ = nullptr;
44   - }
45   -
46 35 acldvppDestroyJpegeConfig(jpegeConfig_);
47 36  
48 37 if (context_ != nullptr) {
... ... @@ -52,11 +41,6 @@ void JpegUtil::jpeg_release(){
52 41 }
53 42 context_ = nullptr;
54 43 }
55   -
56   - ret = aclrtResetDevice(deviceId_);
57   - if (ret != ACL_SUCCESS) {
58   - LOG_ERROR("reset device failed");
59   - }
60 44 }
61 45  
62 46 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
78 62 bool JpegUtil::jpeg_encode(acldvppPicDesc *encodeInputDesc_, string out_file_name) {
79 63  
80 64 aclError aclRet ;
81   - aclRet = aclrtSetDevice(deviceId_);
82 65 aclrtSetCurrentContext(context_);
83 66  
84 67 // 8. 申请输出内存,申请Device内存encodeOutBufferDev_,存放编码后的输出数据
... ... @@ -96,6 +79,8 @@ bool JpegUtil::jpeg_encode(acldvppPicDesc *encodeInputDesc_, string out_file_nam
96 79 }
97 80  
98 81 bool bRet = false;
  82 + aclrtStream stream_;
  83 + aclrtCreateStream(&stream_);
99 84 do {
100 85 // 9. 执行异步编码,再调用aclrtSynchronizeStream接口阻塞程序运行,直到指定Stream中的所有任务都完成
101 86 aclRet = acldvppJpegEncodeAsync(dvppChannelDesc_, encodeInputDesc_, encodeOutBufferDev_, &outBufferSize, jpegeConfig_, stream_);
... ... @@ -136,10 +121,17 @@ bool JpegUtil::jpeg_encode(acldvppPicDesc *encodeInputDesc_, string out_file_nam
136 121  
137 122 bRet = true;
138 123 } while (0);
  124 +
  125 + if (stream_ != nullptr) {
  126 + ret = aclrtDestroyStream(stream_);
  127 + if (ret != ACL_SUCCESS) {
  128 + LOG_ERROR("destroy stream failed");
  129 + }
  130 + stream_ = nullptr;
  131 + }
139 132  
140 133 // 释放掉输入输出的device内存
141 134 (void)acldvppFree(encodeOutBufferDev_);
142 135 encodeOutBufferDev_ = nullptr;
143   - aclRet = aclrtResetDevice(deviceId_);
144 136 return bRet;
145 137 }
146 138 \ No newline at end of file
... ...
src/util/JpegUtil.h
... ... @@ -23,7 +23,6 @@ private:
23 23 private:
24 24 int32_t deviceId_;
25 25 aclrtContext context_;
26   - aclrtStream stream_;
27 26 acldvppChannelDesc *dvppChannelDesc_;
28 27 acldvppJpegeConfig *jpegeConfig_ ;
29 28 };
... ...
src/util/vpc_util.cpp
... ... @@ -29,8 +29,7 @@ static uint32_t AlignSize(uint32_t origSize, uint32_t alignment){
29 29 void VPCUtil::release()
30 30 {
31 31 aclError ret;
32   - // ret = aclrtSetDevice(deviceId_);
33   - // aclrtSetCurrentContext(context_);
  32 + aclrtSetCurrentContext(context_);
34 33  
35 34 ret = acldvppDestroyChannel(dvppChannelDesc_);
36 35 ret = acldvppDestroyChannelDesc(dvppChannelDesc_);
... ... @@ -44,10 +43,6 @@ void VPCUtil::release()
44 43 }
45 44 LOG_INFO("end to destroy context");
46 45  
47   - ret = aclrtResetDevice(deviceId_);
48   - if (ret != ACL_SUCCESS) {
49   - LOG_ERROR("reset device failed");
50   - }
51 46 LOG_INFO("end to reset device is %d", deviceId_);
52 47 }
53 48  
... ... @@ -108,7 +103,6 @@ vpc_img_info VPCUtil::crop(DeviceMemory *devMem, video_object_info obj) {
108 103 }
109 104  
110 105 aclError ret;
111   - aclrtSetDevice(deviceId_);
112 106 ret = aclrtSetCurrentContext(context_);
113 107 if (ret != ACL_SUCCESS) {
114 108 LOG_ERROR("aclrtSetCurrentContext failed");
... ... @@ -194,7 +188,6 @@ int VPCUtil::init(int32_t devId){
194 188 deviceId_ = devId;
195 189  
196 190 aclError ret;
197   - aclrtSetDevice(deviceId_);
198 191 aclrtCreateContext(&context_, deviceId_);
199 192  
200 193 // channel 准备
... ... @@ -212,7 +205,6 @@ vector&lt;vpc_img_info&gt; VPCUtil::crop_batch(DeviceMemory *devMem, vector&lt;video_obje
212 205 }
213 206  
214 207 aclError ret;
215   - aclrtSetDevice(deviceId_);
216 208 ret = aclrtSetCurrentContext(context_);
217 209  
218 210 // 输入
... ...
test/main.cpp
... ... @@ -94,7 +94,6 @@ void dvpp_jpeg_encode(acldvppPicDesc *encodeInputDesc_, string out_file_name){
94 94 // 2.运行管理资源申请(依次申请Device、Context、Stream)
95 95 aclrtContext context_;
96 96 aclrtStream stream_;
97   - aclrtSetDevice(0);
98 97 aclrtCreateContext(&context_, 0);
99 98 aclrtCreateStream(&stream_);
100 99  
... ... @@ -138,7 +137,6 @@ void dvpp_jpeg_encode(acldvppPicDesc *encodeInputDesc_, string out_file_name){
138 137 // 11. 释放运行管理资源(依次释放Stream、Context、Device)
139 138 aclrtDestroyStream(stream_);
140 139 aclrtDestroyContext(context_);
141   - aclrtResetDevice(0);
142 140 }
143 141  
144 142 char* ReadBinFile(std::string fileName, uint32_t &fileSize)
... ... @@ -193,10 +191,6 @@ void DestroyResource()
193 191 }
194 192 INFO_LOG("end to destroy context");
195 193  
196   - ret = aclrtResetDevice(deviceId_);
197   - if (ret != ACL_SUCCESS) {
198   - ERROR_LOG("reset device failed");
199   - }
200 194 INFO_LOG("end to reset device is %d", deviceId_);
201 195  
202 196 ret = aclFinalize();
... ... @@ -213,7 +207,6 @@ int main(int argc, char *argv[])
213 207 aclInit(nullptr);
214 208  
215 209 /* 2.Run the management resource application, including Device, Context, Stream */
216   - aclrtSetDevice(deviceId_);
217 210 aclrtCreateContext(&context_, deviceId_);
218 211 aclrtCreateStream(&stream_);
219 212 aclrtGetRunMode(&runMode);
... ...
test/vpc_test.cpp1
... ... @@ -213,7 +213,6 @@ int vpc_crop(acldvppPicDesc *input){
213 213  
214 214 // 2.运行管理资源申请(依次申请Device、Context)
215 215 aclrtContext g_context;
216   - aclRet = aclrtSetDevice(0);
217 216 aclRet = aclrtCreateContext(&g_context, 0);
218 217  
219 218 // 3.初始化媒体数据处理系统
... ... @@ -328,7 +327,6 @@ int vpc_crop(acldvppPicDesc *input){
328 327  
329 328 // 8. 释放运行管理资源(依次释放Context、Device)
330 329 aclRet = aclrtDestroyContext(g_context);
331   - aclRet = aclrtResetDevice(0);
332 330  
333 331 return 0;
334 332 }
... ... @@ -356,7 +354,6 @@ void dvpp_jpeg_encode(acldvppPicDesc *encodeInputDesc_, string out_file_name){
356 354 // 2.运行管理资源申请(依次申请Device、Context、Stream)
357 355 aclrtContext context_;
358 356 aclrtStream stream_;
359   - aclrtSetDevice(0);
360 357 aclrtCreateContext(&context_, 0);
361 358 aclrtCreateStream(&stream_);
362 359  
... ... @@ -400,7 +397,6 @@ void dvpp_jpeg_encode(acldvppPicDesc *encodeInputDesc_, string out_file_name){
400 397 // 11. 释放运行管理资源(依次释放Stream、Context、Device)
401 398 aclrtDestroyStream(stream_);
402 399 aclrtDestroyContext(context_);
403   - aclrtResetDevice(0);
404 400 }
405 401  
406 402  
... ... @@ -418,7 +414,6 @@ void dvpp_crop(acldvppPicDesc *input_pic_desc){
418 414 // 2.运行管理资源申请(依次申请Device、Context、Stream)
419 415 aclrtContext context_;
420 416 aclrtStream stream_;
421   - ret = aclrtSetDevice(0);
422 417 ret = aclrtCreateContext(&context_, 0);
423 418 ret = aclrtCreateStream(&stream_);
424 419  
... ... @@ -509,7 +504,6 @@ void dvpp_crop(acldvppPicDesc *input_pic_desc){
509 504 // 12. 释放运行管理资源(依次释放Stream、Context、Device)
510 505 aclrtDestroyStream(stream_);
511 506 aclrtDestroyContext(context_);
512   - aclrtResetDevice(0);
513 507  
514 508 }
515 509  
... ...