• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 
GetInstance()44     static BackendManager& GetInstance()
45     {
46         // if libneural_network_runtime.so loaded
47         if (dlopen("libneural_network_runtime.so", RTLD_NOLOAD) != nullptr) {
48             // if libneural_network_runtime_ext.so not loaded, try to dlopen it
49             if (dlopen("libneural_network_runtime_ext.so", RTLD_NOLOAD) == nullptr) {
50                 LOGI("dlopen libneural_network_runtime_ext.so.");
51                 void* libHandle = dlopen("libneural_network_runtime_ext.so", RTLD_NOW | RTLD_GLOBAL);
52                 if (libHandle == nullptr) {
53                     LOGW("Failed to dlopen libneural_network_runtime_ext.so.");
54                 }
55             }
56         }
57         static BackendManager instance;
58         return instance;
59     }
60 
61 private:
62     BackendManager() = default;
63     BackendManager(const BackendManager&) = delete;
64     BackendManager& operator=(const BackendManager&) = delete;
65     virtual ~BackendManager();
66     bool IsValidBackend(std::shared_ptr<Backend> backend) const;
67 
68 private:
69     std::vector<size_t> m_backendIDs;
70     std::unordered_map<size_t, std::string> m_backendNames;
71     std::string m_emptyBackendName;
72     // key is the name of backend.
73     std::unordered_map<size_t, std::shared_ptr<Backend>> m_backends;
74     std::mutex m_mtx;
75     std::unordered_map<std::string, std::vector<size_t>> m_backendIDGroup;
76 };
77 }  // namespace NeuralNetworkRuntime
78 }  // namespace OHOS
79 #endif  // NEURAL_NETWORK_CORE_BACKEND_MANAGER_H
80