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); 37 const std::string& GetBackendName(size_t backendID); 38 39 // Register backend by C++ API 40 OH_NN_ReturnCode RegisterBackend( 41 const std::string& backendName, std::function<std::shared_ptr<Backend>()> creator); 42 void RemoveBackend(const std::string& backendName); 43 44 static BackendManager& GetInstance(); 45 46 private: 47 BackendManager() = default; 48 BackendManager(const BackendManager&) = delete; 49 BackendManager& operator=(const BackendManager&) = delete; 50 virtual ~BackendManager(); 51 bool IsValidBackend(std::shared_ptr<Backend> backend) const; 52 53 private: 54 std::vector<size_t> m_backendIDs; 55 std::unordered_map<size_t, std::string> m_backendNames; 56 std::string m_emptyBackendName; 57 // key is the name of backend. 58 std::unordered_map<size_t, std::shared_ptr<Backend>> m_backends; 59 std::mutex m_mtx; 60 std::unordered_map<std::string, std::vector<size_t>> m_backendIDGroup; 61 }; 62 } // namespace NeuralNetworkRuntime 63 } // namespace OHOS 64 #endif // NEURAL_NETWORK_CORE_BACKEND_MANAGER_H 65