diff --git a/mindspore/ccsrc/minddata/dataset/core/ascend_resource.cc b/mindspore/ccsrc/minddata/dataset/core/ascend_resource.cc index faad212318..aecf6c565a 100644 --- a/mindspore/ccsrc/minddata/dataset/core/ascend_resource.cc +++ b/mindspore/ccsrc/minddata/dataset/core/ascend_resource.cc @@ -94,5 +94,9 @@ Status AscendResource::DeviceDataRelease() { std::shared_ptr AscendResource::GetInstance() { return processor_; } +void *AscendResource::GetContext() { return processor_->GetContext(); } + +void *AscendResource::GetStream() { return processor_->GetStream(); } + } // namespace dataset } // namespace mindspore diff --git a/mindspore/ccsrc/minddata/dataset/core/ascend_resource.h b/mindspore/ccsrc/minddata/dataset/core/ascend_resource.h index 48f3c1711a..3a401d6012 100644 --- a/mindspore/ccsrc/minddata/dataset/core/ascend_resource.h +++ b/mindspore/ccsrc/minddata/dataset/core/ascend_resource.h @@ -48,6 +48,10 @@ class AscendResource : public DeviceResource { Status DeviceDataRelease() override; + void *GetContext() override; + + void *GetStream() override; + private: std::shared_ptr processor_; std::shared_ptr ascend_resource_; diff --git a/mindspore/ccsrc/minddata/dataset/core/device_resource.cc b/mindspore/ccsrc/minddata/dataset/core/device_resource.cc index 1bfe905070..76b368416c 100644 --- a/mindspore/ccsrc/minddata/dataset/core/device_resource.cc +++ b/mindspore/ccsrc/minddata/dataset/core/device_resource.cc @@ -54,5 +54,17 @@ std::shared_ptr DeviceResource::GetInstance() { return nullptr; } +void *DeviceResource::GetContext() { + MS_LOG(ERROR) + << "Is this a device which contains context resource? If yes, please implement GetContext() in the derived class"; + return nullptr; +} + +void *DeviceResource::GetStream() { + MS_LOG(ERROR) + << "Is this a device which contains stream resource? If yes, please implement GetContext() in the derived class"; + return nullptr; +} + } // namespace dataset } // namespace mindspore diff --git a/mindspore/ccsrc/minddata/dataset/core/device_resource.h b/mindspore/ccsrc/minddata/dataset/core/device_resource.h index 107d9c944c..4517221738 100644 --- a/mindspore/ccsrc/minddata/dataset/core/device_resource.h +++ b/mindspore/ccsrc/minddata/dataset/core/device_resource.h @@ -44,6 +44,10 @@ class DeviceResource { virtual std::shared_ptr GetInstance(); virtual Status DeviceDataRelease(); + + virtual void *GetContext(); + + virtual void *GetStream(); }; } // namespace dataset diff --git a/mindspore/ccsrc/minddata/dataset/kernels/image/dvpp/utils/DvppCommon.cc b/mindspore/ccsrc/minddata/dataset/kernels/image/dvpp/utils/DvppCommon.cc index 9a93923c08..8c012a93ee 100644 --- a/mindspore/ccsrc/minddata/dataset/kernels/image/dvpp/utils/DvppCommon.cc +++ b/mindspore/ccsrc/minddata/dataset/kernels/image/dvpp/utils/DvppCommon.cc @@ -1,5 +1,5 @@ /* - * Copyright (c) 2020.Huawei Technologies Co., Ltd. All rights reserved. + * Copyright (c) 2020-2021.Huawei Technologies Co., Ltd. All rights reserved. * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at @@ -25,7 +25,8 @@ static auto g_picDescDeleter = [](acldvppPicDesc *picDesc) { acldvppDestroyPicDe static auto g_roiConfigDeleter = [](acldvppRoiConfig *p) { acldvppDestroyRoiConfig(p); }; static auto g_jpegeConfigDeleter = [](acldvppJpegeConfig *p) { acldvppDestroyJpegeConfig(p); }; -DvppCommon::DvppCommon(aclrtStream dvppStream) : dvppStream_(dvppStream) {} +DvppCommon::DvppCommon(aclrtContext dvppContext, aclrtStream dvppStream) + : dvppContext_(dvppContext), dvppStream_(dvppStream) {} DvppCommon::DvppCommon(const VdecConfig &vdecConfig) : vdecConfig_(vdecConfig) {} @@ -37,10 +38,11 @@ DvppCommon::DvppCommon(const VdecConfig &vdecConfig) : vdecConfig_(vdecConfig) { APP_ERROR DvppCommon::Init(void) { dvppChannelDesc_ = acldvppCreateChannelDesc(); if (dvppChannelDesc_ == nullptr) { - return -1; + return APP_ERR_COMM_INVALID_POINTER; } + APP_ERROR ret = acldvppCreateChannel(dvppChannelDesc_); - if (ret != 0) { + if (ret != APP_ERR_OK) { MS_LOG(ERROR) << "Failed to create dvpp channel: " << GetAppErrCodeInfo(ret) << "."; acldvppDestroyChannelDesc(dvppChannelDesc_); dvppChannelDesc_ = nullptr; @@ -117,7 +119,14 @@ APP_ERROR DvppCommon::DeInit(void) { return DestroyResource(); } - APP_ERROR ret = aclrtSynchronizeStream(dvppStream_); // APP_ERROR ret + // Obtain the dvppContext_ allocated by AscendResource which contains the dvppStream_, they mush bind each other + APP_ERROR ret = aclrtSetCurrentContext(dvppContext_); + if (ret != APP_ERR_OK) { + MS_LOG(ERROR) << "Failed to get ACL context, ret = " << ret; + return ret; + } + + ret = aclrtSynchronizeStream(dvppStream_); // APP_ERROR ret if (ret != APP_ERR_OK) { MS_LOG(ERROR) << "Failed to synchronize stream, ret = " << ret << "."; return ret; @@ -1292,13 +1301,18 @@ APP_ERROR DvppCommon::SinkImageH2D(const RawData &imageInfo, acldvppPixelFormat return APP_ERR_DVPP_OBJ_FUNC_MISMATCH; } + APP_ERROR ret = aclrtSetCurrentContext(dvppContext_); + if (ret != APP_ERR_OK) { + MS_LOG(ERROR) << "Failed to get ACL context, ret = " << ret; + return ret; + } + int32_t components; // Member variable of inputImage_, uint8_t *data will be on device inputImage_ = std::make_shared(); inputImage_->format = format; - APP_ERROR ret = - // GetJpegImageInfo(imageInfo.data.get(), imageInfo.lenOfByte, inputImage_->width, inputImage_->height, components); - GetJpegImageInfo(imageInfo.data, imageInfo.lenOfByte, inputImage_->width, inputImage_->height, components); + ret = GetJpegImageInfo(imageInfo.data, imageInfo.lenOfByte, inputImage_->width, inputImage_->height, components); + if (ret != APP_ERR_OK) { MS_LOG(ERROR) << "Failed to get input image info, ret = " << ret << "."; return ret; @@ -1345,12 +1359,18 @@ APP_ERROR DvppCommon::SinkImageH2D(const RawData &imageInfo) { return APP_ERR_DVPP_OBJ_FUNC_MISMATCH; } + APP_ERROR ret = aclrtSetCurrentContext(dvppContext_); + if (ret != APP_ERR_OK) { + MS_LOG(ERROR) << "Failed to get ACL context, ret = " << ret; + return ret; + } + int32_t components; inputImage_ = std::make_shared(); acldvppPixelFormat format = PIXEL_FORMAT_RGB_888; inputImage_->format = format; - APP_ERROR ret = - GetPngImageInfo(imageInfo.data, imageInfo.lenOfByte, inputImage_->width, inputImage_->height, components); + + ret = GetPngImageInfo(imageInfo.data, imageInfo.lenOfByte, inputImage_->width, inputImage_->height, components); if (ret != APP_ERR_OK) { MS_LOG(ERROR) << "Failed to get input image info, ret = " << ret << "."; return ret; diff --git a/mindspore/ccsrc/minddata/dataset/kernels/image/dvpp/utils/DvppCommon.h b/mindspore/ccsrc/minddata/dataset/kernels/image/dvpp/utils/DvppCommon.h index 9ef95ab632..aa0c7582e9 100644 --- a/mindspore/ccsrc/minddata/dataset/kernels/image/dvpp/utils/DvppCommon.h +++ b/mindspore/ccsrc/minddata/dataset/kernels/image/dvpp/utils/DvppCommon.h @@ -1,5 +1,5 @@ /* - * Copyright (c) 2020.Huawei Technologies Co., Ltd. All rights reserved. + * Copyright (c) 2020-2021.Huawei Technologies Co., Ltd. All rights reserved. * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at @@ -117,7 +117,7 @@ const uint8_t YUV_GREYER_VALUE = 128; // Filling value of the resized YUV im class DvppCommon { public: - explicit DvppCommon(aclrtStream dvppStream); + explicit DvppCommon(aclrtContext dvppContext, aclrtStream dvppStream); explicit DvppCommon(const VdecConfig &vdecConfig); // Need by vdec ~DvppCommon(); APP_ERROR Init(void); @@ -235,7 +235,11 @@ class DvppCommon { std::shared_ptr decodeOutputDesc_ = nullptr; acldvppChannelDesc *dvppChannelDesc_ = nullptr; + + // Ascend resource core: (context, stream) is a pair so must bind with each other to make all function perform well + aclrtContext dvppContext_ = nullptr; aclrtStream dvppStream_ = nullptr; + std::shared_ptr inputImage_ = nullptr; std::shared_ptr decodedImage_ = nullptr; std::shared_ptr encodedImage_ = nullptr; diff --git a/mindspore/ccsrc/minddata/dataset/kernels/image/dvpp/utils/MDAclProcess.cc b/mindspore/ccsrc/minddata/dataset/kernels/image/dvpp/utils/MDAclProcess.cc index bab98df799..60826968f0 100644 --- a/mindspore/ccsrc/minddata/dataset/kernels/image/dvpp/utils/MDAclProcess.cc +++ b/mindspore/ccsrc/minddata/dataset/kernels/image/dvpp/utils/MDAclProcess.cc @@ -1,5 +1,5 @@ /* - * Copyright (c) 2020.Huawei Technologies Co., Ltd. All rights reserved. + * Copyright (c) 2020-2021.Huawei Technologies Co., Ltd. All rights reserved. * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at @@ -112,7 +112,7 @@ APP_ERROR MDAclProcess::Release() { */ APP_ERROR MDAclProcess::InitModule() { // Create Dvpp JpegD object - dvppCommon_ = std::make_shared(stream_); + dvppCommon_ = std::make_shared(context_, stream_); if (dvppCommon_ == nullptr) { MS_LOG(ERROR) << "Failed to create dvppCommon_ object"; return APP_ERR_COMM_INIT_FAIL; @@ -153,20 +153,31 @@ APP_ERROR MDAclProcess::InitResource() { std::shared_ptr MDAclProcess::GetDeviceModule() { return dvppCommon_; } +aclrtContext MDAclProcess::GetContext() { return context_; } + +aclrtStream MDAclProcess::GetStream() { return stream_; } + /* * Sink data from Tensor(On host) to DeviceTensor(On device) * Two cases are different, jpeg and png */ APP_ERROR MDAclProcess::H2D_Sink(const std::shared_ptr &input, std::shared_ptr &device_input) { + // Recall the context created in InitResource() + APP_ERROR ret = aclrtSetCurrentContext(context_); + if (ret != APP_ERR_OK) { + MS_LOG(ERROR) << "Failed to get ACL context, ret = " << ret; + return ret; + } + RawData imageinfo; uint32_t filesize = input->SizeInBytes(); // MS_LOG(INFO) << "Filesize on host is: " << filesize; imageinfo.lenOfByte = filesize; unsigned char *buffer = const_cast(input->GetBuffer()); imageinfo.data = static_cast(buffer); + // Transfer RawData(Raw image) from host to device, which we call sink - APP_ERROR ret; if (IsNonEmptyJPEG(input)) { // case JPEG ret = dvppCommon_->SinkImageH2D(imageinfo, PIXEL_FORMAT_YUV_SEMIPLANAR_420); } else { // case PNG diff --git a/mindspore/ccsrc/minddata/dataset/kernels/image/dvpp/utils/MDAclProcess.h b/mindspore/ccsrc/minddata/dataset/kernels/image/dvpp/utils/MDAclProcess.h index 63540b0e5f..41b790ef93 100644 --- a/mindspore/ccsrc/minddata/dataset/kernels/image/dvpp/utils/MDAclProcess.h +++ b/mindspore/ccsrc/minddata/dataset/kernels/image/dvpp/utils/MDAclProcess.h @@ -1,5 +1,5 @@ /* - * Copyright (c) 2020.Huawei Technologies Co., Ltd. All rights reserved. + * Copyright (c) 2020-2021.Huawei Technologies Co., Ltd. All rights reserved. * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at @@ -57,6 +57,10 @@ class MDAclProcess { APP_ERROR Release(); // Create resource for this sample APP_ERROR InitResource(); + // Get Ascend Resource core: context and stream which are created when InitResource() + aclrtContext GetContext(); + aclrtContext GetStream(); + // Process the result APP_ERROR JPEG_DRC(const RawData &ImageInfo); APP_ERROR JPEG_DRC();