1 /* 2 * Copyright (c) 2022 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_RUNTIME_DEVICE_MANAGER_H 17 #define NEURAL_NETWORK_RUNTIME_DEVICE_MANAGER_H 18 19 #include <string> 20 #include <unordered_set> 21 #include <unordered_map> 22 #include <memory> 23 #include <functional> 24 #include <mutex> 25 26 #include "device.h" 27 #include "interfaces/kits/c/neural_network_runtime_type.h" 28 29 namespace OHOS { 30 namespace NeuralNetworkRuntime { 31 class DeviceManager { 32 public: 33 const std::vector<size_t>& GetAllDeviceId(); 34 std::shared_ptr<Device> GetDevice(size_t deviceId) const; 35 const std::string& GetDeviceName(size_t deviceId); 36 37 // register device from C++ API 38 OH_NN_ReturnCode RegisterDevice(std::function<std::shared_ptr<Device>()> creator); 39 GetInstance()40 static DeviceManager& GetInstance() 41 { 42 static DeviceManager instance; 43 instance.DiscoverHDIDevices(); 44 return instance; 45 } 46 47 private: 48 DeviceManager() = default; 49 DeviceManager(const DeviceManager&) = delete; 50 DeviceManager& operator=(const DeviceManager&) = delete; 51 52 void DiscoverHDIDevices(); 53 std::string GenUniqueName(const std::string& deviceName, const std::string& vendorName) const; 54 bool IsValidDevice(std::shared_ptr<Device> device) const; 55 56 private: 57 std::unordered_set<std::string> m_uniqueName; 58 // key is device id, it is the unique number. 59 std::unordered_map<size_t, std::shared_ptr<Device>> m_devices; 60 std::mutex m_mtx; 61 62 std::string m_tmpDeviceName; 63 std::vector<size_t> m_tmpDeviceIds; 64 }; 65 } // namespace NeuralNetworkRuntime 66 } // namespace OHOS 67 68 #endif // NEURAL_NETWORK_RUNTIME_DEVICE_MANAGER_H