1 /* 2 * Copyright (c) 2023 Huawei Device Co., Ltd. 3 * Licensed under the Apache License, Version 2.0 (the "License"); 4 * you may not use this file except in compliance with the License. 5 * You may obtain a copy of the License at 6 * 7 * http://www.apache.org/licenses/LICENSE-2.0 8 * 9 * Unless required by applicable law or agreed to in writing, software 10 * distributed under the License is distributed on an "AS IS" BASIS, 11 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 * See the License for the specific language governing permissions and 13 * limitations under the License. 14 */ 15 16 #ifndef NEURAL_NETWORK_CORE_BACKEND_MANAGER_H 17 #define NEURAL_NETWORK_CORE_BACKEND_MANAGER_H 18 19 #include <dlfcn.h> 20 #include <string> 21 #include <vector> 22 #include <memory> 23 #include <unordered_map> 24 #include <unordered_set> 25 #include <mutex> 26 #include <functional> 27 28 #include "backend.h" 29 #include "common/log.h" 30 31 namespace OHOS { 32 namespace NeuralNetworkRuntime { 33 class BackendManager { 34 public: 35 const std::vector<size_t>& GetAllBackendsID(); 36 std::shared_ptr<Backend> GetBackend(size_t backendID) const; 37 const std::string& GetBackendName(size_t backendID); 38 39 // Register backend by C++ API 40 OH_NN_ReturnCode RegisterBackend(std::function<std::shared_ptr<Backend>()> creator); 41 GetInstance()42 static BackendManager& GetInstance() 43 { 44 if (dlopen("libneural_network_runtime_ext.so", RTLD_NOLOAD) == nullptr) { 45 LOGI("dlopen libneural_network_runtime_ext.so."); 46 void* libHandle = dlopen("libneural_network_runtime_ext.so", RTLD_NOW | RTLD_GLOBAL); 47 if (libHandle == nullptr) { 48 LOGW("Failed to dlopen libneural_network_runtime_ext.so."); 49 } 50 } 51 static BackendManager instance; 52 return instance; 53 } 54 55 private: 56 BackendManager() = default; 57 BackendManager(const BackendManager&) = delete; 58 BackendManager& operator=(const BackendManager&) = delete; 59 virtual ~BackendManager(); 60 bool IsValidBackend(std::shared_ptr<Backend> backend) const; 61 62 private: 63 std::vector<size_t> m_backendIDs; 64 std::unordered_map<size_t, std::string> m_backendNames; 65 std::string m_emptyBackendName; 66 // key is the name of backend. 67 std::unordered_map<size_t, std::shared_ptr<Backend>> m_backends; 68 std::mutex m_mtx; 69 }; 70 } // namespace NeuralNetworkRuntime 71 } // namespace OHOS 72 #endif // NEURAL_NETWORK_CORE_BACKEND_MANAGER_H 73