1 /* 2 * Copyright 2022 The Android Open Source Project 3 * 4 * Licensed under the Apache License, Version 2.0 (the "License"); 5 * you may not use this file except in compliance with the License. 6 * You may obtain a copy of the License at 7 * 8 * http://www.apache.org/licenses/LICENSE-2.0 9 * 10 * Unless required by applicable law or agreed to in writing, software 11 * distributed under the License is distributed on an "AS IS" BASIS, 12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 * See the License for the specific language governing permissions and 14 * limitations under the License. 15 */ 16 17 #define LOG_TAG "NnapiInfo" 18 19 #define CONTINUE_IF_ERR(expr) \ 20 { \ 21 int _errCode = (expr); \ 22 if (_errCode != ANEURALNETWORKS_NO_ERROR) { \ 23 std::cerr << #expr << " failed at " << __FILE__ << ":" << __LINE__ << std::endl; \ 24 continue; \ 25 } \ 26 } 27 28 #include <iostream> 29 #include <string> 30 31 #include "NeuralNetworks.h" 32 #include "NeuralNetworksTypes.h" 33 34 namespace { featureLevelString(int64_t featureLevel)35std::string featureLevelString(int64_t featureLevel) { 36 switch (featureLevel) { 37 case ANEURALNETWORKS_FEATURE_LEVEL_1: 38 return "Level 1"; 39 case ANEURALNETWORKS_FEATURE_LEVEL_2: 40 return "Level 2"; 41 case ANEURALNETWORKS_FEATURE_LEVEL_3: 42 return "Level 3"; 43 case ANEURALNETWORKS_FEATURE_LEVEL_4: 44 return "Level 4"; 45 case ANEURALNETWORKS_FEATURE_LEVEL_5: 46 return "Level 5"; 47 case ANEURALNETWORKS_FEATURE_LEVEL_6: 48 return "Level 6"; 49 case ANEURALNETWORKS_FEATURE_LEVEL_7: 50 return "Level 7"; 51 case ANEURALNETWORKS_FEATURE_LEVEL_8: 52 return "Level 8"; 53 default: 54 return "Undefined feature level code"; 55 } 56 } 57 deviceTypeString(int32_t type)58std::string deviceTypeString(int32_t type) { 59 switch (type) { 60 case ANEURALNETWORKS_DEVICE_ACCELERATOR: 61 return "Accelerator"; 62 case ANEURALNETWORKS_DEVICE_CPU: 63 return "CPU"; 64 case ANEURALNETWORKS_DEVICE_GPU: 65 return "GPU"; 66 case ANEURALNETWORKS_DEVICE_OTHER: 67 return "Other"; 68 case ANEURALNETWORKS_DEVICE_UNKNOWN: 69 default: 70 return "Unknown"; 71 } 72 } 73 } // namespace 74 main()75int main() { 76 uint32_t numDevices; 77 int returnCode = ANeuralNetworks_getDeviceCount(&numDevices); 78 if (returnCode != ANEURALNETWORKS_NO_ERROR) { 79 std::cerr << "Error obtaining device count" << std::endl; 80 return 1; 81 } 82 83 std::cout << "Number of devices: " << numDevices << std::endl << std::endl; 84 85 ANeuralNetworksDevice* device = nullptr; 86 int64_t featureLevel; 87 const char* name; 88 int32_t type; 89 const char* version; 90 for (uint32_t i = 0; i < numDevices; i++) { 91 CONTINUE_IF_ERR(ANeuralNetworks_getDevice(i, &device)); 92 CONTINUE_IF_ERR(ANeuralNetworksDevice_getFeatureLevel(device, &featureLevel)); 93 CONTINUE_IF_ERR(ANeuralNetworksDevice_getName(device, &name)); 94 CONTINUE_IF_ERR(ANeuralNetworksDevice_getType(device, &type)); 95 CONTINUE_IF_ERR(ANeuralNetworksDevice_getVersion(device, &version)); 96 97 std::cout << "Device: " << name << std::endl; 98 std::cout << "Feature Level: " << featureLevelString(featureLevel) << std::endl; 99 std::cout << "Type: " << deviceTypeString(type) << std::endl; 100 std::cout << "Version: " << version << std::endl; 101 102 std::cout << std::endl; 103 } 104 105 return 0; 106 }