From: @zhoufeng54 Reviewed-by: @kisnwang,@xu-yfei Signed-off-by: @xu-yfeitags/v1.1.0
| @@ -0,0 +1,60 @@ | |||
| /** | |||
| * Copyright 2020 Huawei Technologies Co., Ltd | |||
| * | |||
| * 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 | |||
| * | |||
| * http://www.apache.org/licenses/LICENSE-2.0 | |||
| * | |||
| * Unless required by applicable law or agreed to in writing, software | |||
| * distributed under the License is distributed on an "AS IS" BASIS, | |||
| * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | |||
| * See the License for the specific language governing permissions and | |||
| * limitations under the License. | |||
| */ | |||
| #include "cxx_api/graph/acl/acl_env_guard.h" | |||
| #include "utils/log_adapter.h" | |||
| #include "acl/acl.h" | |||
| namespace mindspore::api { | |||
| std::weak_ptr<AclEnvGuard> AclEnvGuard::global_acl_env_; | |||
| std::mutex AclEnvGuard::global_acl_env_mutex_; | |||
| AclEnvGuard::AclEnvGuard(std::string_view cfg_file) { | |||
| errno_ = aclInit(cfg_file.data()); | |||
| if (errno_ != ACL_ERROR_NONE) { | |||
| MS_LOG(ERROR) << "Execute aclInit Failed"; | |||
| return; | |||
| } | |||
| MS_LOG(INFO) << "Acl init success"; | |||
| } | |||
| AclEnvGuard::~AclEnvGuard() { | |||
| errno_ = aclFinalize(); | |||
| if (errno_ != ACL_ERROR_NONE) { | |||
| MS_LOG(ERROR) << "Finalize acl failed"; | |||
| } | |||
| MS_LOG(INFO) << "Acl finalize success"; | |||
| } | |||
| std::shared_ptr<AclEnvGuard> AclEnvGuard::GetAclEnv(std::string_view cfg_file) { | |||
| std::shared_ptr<AclEnvGuard> acl_env; | |||
| std::lock_guard<std::mutex> lock(global_acl_env_mutex_); | |||
| acl_env = global_acl_env_.lock(); | |||
| if (acl_env != nullptr) { | |||
| MS_LOG(INFO) << "Acl has been initialized, skip."; | |||
| } else { | |||
| acl_env = std::make_shared<AclEnvGuard>(cfg_file); | |||
| aclError ret = acl_env->GetErrno(); | |||
| if (ret != ACL_ERROR_NONE) { | |||
| MS_LOG(ERROR) << "Execute aclInit Failed"; | |||
| return nullptr; | |||
| } | |||
| global_acl_env_ = acl_env; | |||
| MS_LOG(INFO) << "Acl init success"; | |||
| } | |||
| return acl_env; | |||
| } | |||
| } // namespace mindspore::api | |||
| @@ -0,0 +1,38 @@ | |||
| /** | |||
| * Copyright 2020 Huawei Technologies Co., Ltd | |||
| * | |||
| * 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 | |||
| * | |||
| * http://www.apache.org/licenses/LICENSE-2.0 | |||
| * | |||
| * Unless required by applicable law or agreed to in writing, software | |||
| * distributed under the License is distributed on an "AS IS" BASIS, | |||
| * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | |||
| * See the License for the specific language governing permissions and | |||
| * limitations under the License. | |||
| */ | |||
| #ifndef MINDSPORE_CCSRC_CXX_API_GRAPH_ACL_ACL_ENV_GUARD_H | |||
| #define MINDSPORE_CCSRC_CXX_API_GRAPH_ACL_ACL_ENV_GUARD_H | |||
| #include <memory> | |||
| #include <mutex> | |||
| #include "acl/acl_base.h" | |||
| namespace mindspore::api { | |||
| class __attribute__((visibility("default"))) AclEnvGuard { | |||
| public: | |||
| explicit AclEnvGuard(std::string_view cfg_file); | |||
| ~AclEnvGuard(); | |||
| aclError GetErrno() const { return errno_; } | |||
| static std::shared_ptr<AclEnvGuard> GetAclEnv(std::string_view cfg_file); | |||
| private: | |||
| static std::weak_ptr<AclEnvGuard> global_acl_env_; | |||
| static std::mutex global_acl_env_mutex_; | |||
| aclError errno_; | |||
| }; | |||
| } // namespace mindspore::api | |||
| #endif // MINDSPORE_CCSRC_CXX_API_GRAPH_ACL_ACL_ENV_GUARD_H | |||
| @@ -21,8 +21,6 @@ | |||
| namespace mindspore::api { | |||
| API_FACTORY_REG(GraphCell::GraphImpl, Ascend310, AclGraphImpl); | |||
| std::weak_ptr<AclGraphImpl::AclEnvGuard> AclGraphImpl::global_acl_env_; | |||
| std::mutex AclGraphImpl::global_acl_env_mutex_; | |||
| AclGraphImpl::AclGraphImpl() | |||
| : init_flag_(false), | |||
| @@ -95,24 +93,13 @@ Status AclGraphImpl::InitEnv() { | |||
| return SUCCESS; | |||
| } | |||
| aclError ret; | |||
| { | |||
| std::lock_guard<std::mutex> lock(global_acl_env_mutex_); | |||
| acl_env_ = global_acl_env_.lock(); | |||
| if (acl_env_ != nullptr) { | |||
| MS_LOG(INFO) << "Acl has been initialized, skip."; | |||
| } else { | |||
| acl_env_ = std::make_shared<AclEnvGuard>(""); | |||
| if (acl_env_->GetErrno() != ACL_ERROR_NONE) { | |||
| MS_LOG(ERROR) << "Execute aclInit Failed"; | |||
| return FAILED; | |||
| } | |||
| global_acl_env_ = acl_env_; | |||
| MS_LOG(INFO) << "Acl init success"; | |||
| } | |||
| acl_env_ = AclEnvGuard::GetAclEnv(""); | |||
| if (acl_env_ == nullptr) { | |||
| MS_LOG(ERROR) << "Acl init failed."; | |||
| return FAILED; | |||
| } | |||
| ret = aclrtSetDevice(device_id_); | |||
| aclError ret = aclrtSetDevice(device_id_); | |||
| if (ret != ACL_ERROR_NONE) { | |||
| MS_LOG(ERROR) << "Acl open device " << device_id_ << " failed"; | |||
| return FAILED; | |||
| @@ -245,21 +232,4 @@ Status AclGraphImpl::ConvertToOM() { | |||
| MS_LOG(ERROR) << "Unsupported ModelType " << graph_->ModelType(); | |||
| return FAILED; | |||
| } | |||
| AclGraphImpl::AclEnvGuard::AclEnvGuard(std::string_view cfg_file) { | |||
| errno_ = aclInit(cfg_file.data()); | |||
| if (errno_ != ACL_ERROR_NONE) { | |||
| MS_LOG(ERROR) << "Execute aclInit Failed"; | |||
| return; | |||
| } | |||
| MS_LOG(INFO) << "Acl init success"; | |||
| } | |||
| AclGraphImpl::AclEnvGuard::~AclEnvGuard() { | |||
| errno_ = aclFinalize(); | |||
| if (errno_ != ACL_ERROR_NONE) { | |||
| MS_LOG(ERROR) << "Finalize acl failed"; | |||
| } | |||
| MS_LOG(INFO) << "Acl finalize success"; | |||
| } | |||
| } // namespace mindspore::api | |||
| @@ -23,6 +23,7 @@ | |||
| #include <utility> | |||
| #include "include/api/graph.h" | |||
| #include "cxx_api/graph/acl/model_process.h" | |||
| #include "cxx_api/graph/acl/acl_env_guard.h" | |||
| #include "cxx_api/graph/graph_impl.h" | |||
| #include "cxx_api/factory.h" | |||
| @@ -40,8 +41,6 @@ class AclGraphImpl : public GraphCell::GraphImpl { | |||
| std::vector<DataType> *data_types, std::vector<size_t> *mem_sizes) override; | |||
| private: | |||
| class AclEnvGuard; | |||
| Status ConvertToOM(); | |||
| Status InitEnv(); | |||
| Status FinalizeEnv(); | |||
| @@ -54,20 +53,8 @@ class AclGraphImpl : public GraphCell::GraphImpl { | |||
| aclrtContext context_; | |||
| std::shared_ptr<AclEnvGuard> acl_env_; | |||
| static std::weak_ptr<AclEnvGuard> global_acl_env_; | |||
| static std::mutex global_acl_env_mutex_; | |||
| ModelProcess model_process_; | |||
| }; | |||
| class AclGraphImpl::AclEnvGuard { | |||
| public: | |||
| explicit AclEnvGuard(std::string_view cfg_file); | |||
| ~AclEnvGuard(); | |||
| aclError GetErrno() const { return errno_; } | |||
| private: | |||
| aclError errno_; | |||
| }; | |||
| } // namespace mindspore::api | |||
| #endif // MINDSPORE_CCSRC_CXX_API_GRAPH_ACL_ACL_GRAPH_IMPL_H | |||
| @@ -23,6 +23,7 @@ | |||
| #include "minddata/dataset/kernels/image/dvpp/utils/CommonDataType.h" | |||
| #include "minddata/dataset/core/data_type.h" | |||
| #include "minddata/dataset/kernels/image/dvpp/dvpp_decode_resize_crop_jpeg_op.h" | |||
| #include "include/api/context.h" | |||
| namespace mindspore { | |||
| namespace dataset { | |||
| @@ -44,7 +45,7 @@ Status DvppDecodeResizeCropJpegOp::Compute(const std::shared_ptr<Tensor> &input, | |||
| // applied on device | |||
| ResourceInfo resource; | |||
| resource.aclConfigPath = ""; | |||
| resource.deviceIds.insert(0); // 0 is device id which should be refined later! | |||
| resource.deviceIds.insert(api::Context::Instance().GetDeviceID()); | |||
| std::shared_ptr<ResourceManager> instance = ResourceManager::GetInstance(); | |||
| APP_ERROR ret = instance->InitResource(resource); | |||
| if (ret != APP_ERR_OK) { | |||
| @@ -15,6 +15,8 @@ | |||
| #include "ResourceManager.h" | |||
| #include <algorithm> | |||
| #include <memory> | |||
| #include <string> | |||
| bool ResourceManager::initFlag_ = true; | |||
| std::shared_ptr<ResourceManager> ResourceManager::ptr_ = nullptr; | |||
| @@ -79,7 +81,8 @@ std::shared_ptr<ResourceManager> ResourceManager::GetInstance() { | |||
| } | |||
| APP_ERROR ResourceManager::InitResource(ResourceInfo &resourceInfo) { | |||
| if (!GetInitStatus()) { | |||
| if (acl_env_ != nullptr) { | |||
| MS_LOG(INFO) << "Acl has been initialized, skip."; | |||
| return APP_ERR_OK; | |||
| } | |||
| @@ -87,17 +90,17 @@ APP_ERROR ResourceManager::InitResource(ResourceInfo &resourceInfo) { | |||
| APP_ERROR ret; | |||
| if (aclConfigPath.length() == 0) { | |||
| // Init acl without aclconfig | |||
| ret = aclInit(nullptr); | |||
| acl_env_ = mindspore::api::AclEnvGuard::GetAclEnv(""); | |||
| } else { | |||
| ret = ExistFile(aclConfigPath); | |||
| if (ret != APP_ERR_OK) { | |||
| MS_LOG(ERROR) << "Acl config file not exist, ret = " << ret << "."; | |||
| return ret; | |||
| } | |||
| ret = aclInit(aclConfigPath.c_str()); // Initialize ACL | |||
| acl_env_ = mindspore::api::AclEnvGuard::GetAclEnv(aclConfigPath); | |||
| } | |||
| if (ret != APP_ERR_OK) { | |||
| MS_LOG(ERROR) << "Failed to init acl, ret = " << ret; | |||
| if (acl_env_ == nullptr) { | |||
| MS_LOG(ERROR) << "Failed to init acl."; | |||
| return ret; | |||
| } | |||
| std::copy(resourceInfo.deviceIds.begin(), resourceInfo.deviceIds.end(), std::back_inserter(deviceIds_)); | |||
| @@ -133,4 +136,4 @@ APP_ERROR ResourceManager::InitResource(ResourceInfo &resourceInfo) { | |||
| return APP_ERR_OK; | |||
| } | |||
| aclrtContext ResourceManager::GetContext(int deviceId) { return contexts_[deviceIdMap_[deviceId]]; } | |||
| aclrtContext ResourceManager::GetContext(int deviceId) { return contexts_[deviceIdMap_[deviceId]]; } | |||
| @@ -25,6 +25,7 @@ | |||
| #include "ErrorCode.h" | |||
| #include <sys/stat.h> | |||
| #include "mindspore/core/utils/log_adapter.h" | |||
| #include "mindspore/ccsrc/cxx_api/graph/acl/acl_env_guard.h" | |||
| #define PATH_MAX 4096 | |||
| @@ -83,6 +84,7 @@ class ResourceManager { | |||
| std::vector<int> deviceIds_; | |||
| std::vector<aclrtContext> contexts_; | |||
| std::unordered_map<int, int> deviceIdMap_; // Map of device to index | |||
| std::shared_ptr<mindspore::api::AclEnvGuard> acl_env_; | |||
| }; | |||
| #endif | |||