Pablo Tello | 7fad9b1 | 2018-03-14 17:55:27 +0000 | [diff] [blame] | 1 | /* |
Georgios Pinitas | e8291ac | 2020-02-26 09:58:13 +0000 | [diff] [blame] | 2 | * Copyright (c) 2018-2020 ARM Limited. |
Pablo Tello | 7fad9b1 | 2018-03-14 17:55:27 +0000 | [diff] [blame] | 3 | * |
| 4 | * SPDX-License-Identifier: MIT |
| 5 | * |
| 6 | * Permission is hereby granted, free of charge, to any person obtaining a copy |
| 7 | * of this software and associated documentation files (the "Software"), to |
| 8 | * deal in the Software without restriction, including without limitation the |
| 9 | * rights to use, copy, modify, merge, publish, distribute, sublicense, and/or |
| 10 | * sell copies of the Software, and to permit persons to whom the Software is |
| 11 | * furnished to do so, subject to the following conditions: |
| 12 | * |
| 13 | * The above copyright notice and this permission notice shall be included in all |
| 14 | * copies or substantial portions of the Software. |
| 15 | * |
| 16 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
| 17 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
| 18 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE |
| 19 | * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
| 20 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, |
| 21 | * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE |
| 22 | * SOFTWARE. |
| 23 | */ |
| 24 | #include "arm_compute/runtime/CPUUtils.h" |
| 25 | |
| 26 | #include "arm_compute/core/CPP/CPPTypes.h" |
| 27 | #include "arm_compute/core/Error.h" |
Matthew Bentham | 758b5ba | 2020-03-05 23:37:48 +0000 | [diff] [blame^] | 28 | #include "support/StringSupport.h" |
Pablo Tello | 7fad9b1 | 2018-03-14 17:55:27 +0000 | [diff] [blame] | 29 | |
Matthew Bentham | 758b5ba | 2020-03-05 23:37:48 +0000 | [diff] [blame^] | 30 | #include <algorithm> |
Pablo Tello | 7fad9b1 | 2018-03-14 17:55:27 +0000 | [diff] [blame] | 31 | #include <array> |
| 32 | #include <cstdlib> |
| 33 | #include <cstring> |
Pablo Tello | 7fad9b1 | 2018-03-14 17:55:27 +0000 | [diff] [blame] | 34 | #include <fstream> |
| 35 | #include <map> |
Pablo Tello | 7fad9b1 | 2018-03-14 17:55:27 +0000 | [diff] [blame] | 36 | |
| 37 | #ifndef BARE_METAL |
Michalis Spyrou | 8e5174c | 2018-12-04 11:43:23 +0000 | [diff] [blame] | 38 | /* C++ std::regex takes up a lot of space in the standalone builds */ |
| 39 | #include <regex.h> |
Pablo Tello | 7fad9b1 | 2018-03-14 17:55:27 +0000 | [diff] [blame] | 40 | #include <thread> |
| 41 | #endif /* BARE_METAL */ |
| 42 | |
| 43 | #if !defined(BARE_METAL) && (defined(__arm__) || defined(__aarch64__)) |
| 44 | #include <sys/auxv.h> |
| 45 | |
| 46 | /* Get HWCAP bits from asm/hwcap.h */ |
| 47 | #include <asm/hwcap.h> |
| 48 | #endif /* !BARE_METAL */ |
| 49 | |
| 50 | /* Make sure the bits we care about are defined, just in case asm/hwcap.h is |
| 51 | * out of date (or for bare metal mode) */ |
| 52 | #ifndef HWCAP_ASIMDHP |
Michalis Spyrou | bcfd09a | 2019-05-01 13:03:59 +0100 | [diff] [blame] | 53 | #define HWCAP_ASIMDHP (1 << 10) // NOLINT |
| 54 | #endif /* HWCAP_ASIMDHP */ |
Pablo Tello | 7fad9b1 | 2018-03-14 17:55:27 +0000 | [diff] [blame] | 55 | |
| 56 | #ifndef HWCAP_CPUID |
Michalis Spyrou | bcfd09a | 2019-05-01 13:03:59 +0100 | [diff] [blame] | 57 | #define HWCAP_CPUID (1 << 11) // NOLINT |
| 58 | #endif /* HWCAP_CPUID */ |
Pablo Tello | 7fad9b1 | 2018-03-14 17:55:27 +0000 | [diff] [blame] | 59 | |
| 60 | #ifndef HWCAP_ASIMDDP |
Michalis Spyrou | bcfd09a | 2019-05-01 13:03:59 +0100 | [diff] [blame] | 61 | #define HWCAP_ASIMDDP (1 << 20) // NOLINT |
| 62 | #endif /* HWCAP_ASIMDDP */ |
Pablo Tello | 7fad9b1 | 2018-03-14 17:55:27 +0000 | [diff] [blame] | 63 | |
| 64 | namespace |
| 65 | { |
| 66 | using namespace arm_compute; |
| 67 | |
| 68 | #if !defined(BARE_METAL) && (defined(__arm__) || defined(__aarch64__)) |
Pablo Tello | 7fad9b1 | 2018-03-14 17:55:27 +0000 | [diff] [blame] | 69 | |
Anthony Barbier | 3efb375 | 2018-07-20 15:30:46 +0100 | [diff] [blame] | 70 | bool model_supports_dot(CPUModel model) |
| 71 | { |
| 72 | switch(model) |
| 73 | { |
Anthony Barbier | 5a3ee4f | 2018-07-24 11:24:17 +0100 | [diff] [blame] | 74 | case CPUModel::GENERIC_FP16_DOT: |
Anthony Barbier | 3efb375 | 2018-07-20 15:30:46 +0100 | [diff] [blame] | 75 | case CPUModel::A55r1: |
Anthony Barbier | 3efb375 | 2018-07-20 15:30:46 +0100 | [diff] [blame] | 76 | return true; |
| 77 | default: |
| 78 | return false; |
| 79 | } |
| 80 | } |
| 81 | |
| 82 | bool model_supports_fp16(CPUModel model) |
| 83 | { |
| 84 | switch(model) |
| 85 | { |
Anthony Barbier | 5a3ee4f | 2018-07-24 11:24:17 +0100 | [diff] [blame] | 86 | case CPUModel::GENERIC_FP16: |
| 87 | case CPUModel::GENERIC_FP16_DOT: |
Anthony Barbier | 3efb375 | 2018-07-20 15:30:46 +0100 | [diff] [blame] | 88 | case CPUModel::A55r1: |
Anthony Barbier | 3efb375 | 2018-07-20 15:30:46 +0100 | [diff] [blame] | 89 | return true; |
| 90 | default: |
| 91 | return false; |
| 92 | } |
| 93 | } |
Georgios Pinitas | 1896069 | 2018-12-21 18:18:46 +0000 | [diff] [blame] | 94 | |
Pablo Tello | 7fad9b1 | 2018-03-14 17:55:27 +0000 | [diff] [blame] | 95 | /* Convert an MIDR register value to a CPUModel enum value. */ |
| 96 | CPUModel midr_to_model(const unsigned int midr) |
| 97 | { |
Anthony Barbier | 5a3ee4f | 2018-07-24 11:24:17 +0100 | [diff] [blame] | 98 | CPUModel model = CPUModel::GENERIC; |
Pablo Tello | 7fad9b1 | 2018-03-14 17:55:27 +0000 | [diff] [blame] | 99 | |
| 100 | // Unpack variant and CPU ID |
Anthony Barbier | 5a3ee4f | 2018-07-24 11:24:17 +0100 | [diff] [blame] | 101 | const int implementer = (midr >> 24) & 0xFF; |
| 102 | const int variant = (midr >> 20) & 0xF; |
| 103 | const int cpunum = (midr >> 4) & 0xFFF; |
Pablo Tello | 7fad9b1 | 2018-03-14 17:55:27 +0000 | [diff] [blame] | 104 | |
Anthony Barbier | 5a3ee4f | 2018-07-24 11:24:17 +0100 | [diff] [blame] | 105 | if(implementer == 0x41) // Arm CPUs |
Pablo Tello | 7fad9b1 | 2018-03-14 17:55:27 +0000 | [diff] [blame] | 106 | { |
Anthony Barbier | 5a3ee4f | 2018-07-24 11:24:17 +0100 | [diff] [blame] | 107 | // Only CPUs we have code paths for are detected. All other CPUs can be safely classed as "GENERIC" |
| 108 | switch(cpunum) |
| 109 | { |
| 110 | case 0xd03: // A53 |
| 111 | case 0xd04: // A35 |
| 112 | model = CPUModel::A53; |
| 113 | break; |
| 114 | case 0xd05: // A55 |
| 115 | if(variant != 0) |
| 116 | { |
| 117 | model = CPUModel::A55r1; |
| 118 | } |
| 119 | else |
| 120 | { |
| 121 | model = CPUModel::A55r0; |
| 122 | } |
| 123 | break; |
| 124 | case 0xd0a: // A75 |
| 125 | if(variant != 0) |
| 126 | { |
| 127 | model = CPUModel::GENERIC_FP16_DOT; |
| 128 | } |
| 129 | else |
| 130 | { |
| 131 | model = CPUModel::GENERIC_FP16; |
| 132 | } |
| 133 | break; |
| 134 | case 0xd0b: // A76 |
Isabella Gottardi | be2de40 | 2018-11-21 15:23:49 +0000 | [diff] [blame] | 135 | case 0xd06: |
| 136 | case 0xd0c: |
| 137 | case 0xd0d: |
Anthony Barbier | 5a3ee4f | 2018-07-24 11:24:17 +0100 | [diff] [blame] | 138 | model = CPUModel::GENERIC_FP16_DOT; |
| 139 | break; |
| 140 | default: |
| 141 | model = CPUModel::GENERIC; |
| 142 | break; |
| 143 | } |
Pablo Tello | 7fad9b1 | 2018-03-14 17:55:27 +0000 | [diff] [blame] | 144 | } |
Michalis Spyrou | bcfd09a | 2019-05-01 13:03:59 +0100 | [diff] [blame] | 145 | else if(implementer == 0x48) |
Georgios Pinitas | 1896069 | 2018-12-21 18:18:46 +0000 | [diff] [blame] | 146 | { |
| 147 | // Only CPUs we have code paths for are detected. All other CPUs can be safely classed as "GENERIC" |
| 148 | switch(cpunum) |
| 149 | { |
Michalis Spyrou | bcfd09a | 2019-05-01 13:03:59 +0100 | [diff] [blame] | 150 | case 0xd40: // A76 |
Georgios Pinitas | 1896069 | 2018-12-21 18:18:46 +0000 | [diff] [blame] | 151 | model = CPUModel::GENERIC_FP16_DOT; |
| 152 | break; |
| 153 | default: |
| 154 | model = CPUModel::GENERIC; |
| 155 | break; |
| 156 | } |
| 157 | } |
Pablo Tello | 7fad9b1 | 2018-03-14 17:55:27 +0000 | [diff] [blame] | 158 | |
| 159 | return model; |
| 160 | } |
| 161 | |
Anthony Barbier | 3efb375 | 2018-07-20 15:30:46 +0100 | [diff] [blame] | 162 | void populate_models_cpuid(std::vector<CPUModel> &cpusv) |
Pablo Tello | 7fad9b1 | 2018-03-14 17:55:27 +0000 | [diff] [blame] | 163 | { |
| 164 | // If the CPUID capability is present, MIDR information is provided in /sys. Use that to populate the CPU model table. |
| 165 | uint32_t i = 0; |
| 166 | for(auto &c : cpusv) |
| 167 | { |
| 168 | std::stringstream str; |
| 169 | str << "/sys/devices/system/cpu/cpu" << i++ << "/regs/identification/midr_el1"; |
| 170 | std::ifstream file; |
| 171 | file.open(str.str(), std::ios::in); |
| 172 | if(file.is_open()) |
| 173 | { |
| 174 | std::string line; |
| 175 | if(bool(getline(file, line))) |
| 176 | { |
Georgios Pinitas | 9c2ec7e | 2018-11-12 17:07:18 +0000 | [diff] [blame] | 177 | const uint32_t midr = support::cpp11::stoul(line, nullptr, support::cpp11::NumericBase::BASE_16); |
| 178 | c = midr_to_model(midr & 0xffffffff); |
Pablo Tello | 7fad9b1 | 2018-03-14 17:55:27 +0000 | [diff] [blame] | 179 | } |
| 180 | } |
| 181 | } |
| 182 | } |
| 183 | |
Anthony Barbier | 3efb375 | 2018-07-20 15:30:46 +0100 | [diff] [blame] | 184 | void populate_models_cpuinfo(std::vector<CPUModel> &cpusv) |
Pablo Tello | 7fad9b1 | 2018-03-14 17:55:27 +0000 | [diff] [blame] | 185 | { |
Michalis Spyrou | 8e5174c | 2018-12-04 11:43:23 +0000 | [diff] [blame] | 186 | regex_t proc_regex; |
| 187 | regex_t imp_regex; |
| 188 | regex_t var_regex; |
| 189 | regex_t part_regex; |
| 190 | regex_t rev_regex; |
| 191 | |
| 192 | memset(&proc_regex, 0, sizeof(regex_t)); |
| 193 | memset(&imp_regex, 0, sizeof(regex_t)); |
| 194 | memset(&var_regex, 0, sizeof(regex_t)); |
| 195 | memset(&part_regex, 0, sizeof(regex_t)); |
| 196 | memset(&rev_regex, 0, sizeof(regex_t)); |
| 197 | |
| 198 | int ret_status = 0; |
Pablo Tello | 7fad9b1 | 2018-03-14 17:55:27 +0000 | [diff] [blame] | 199 | // If "long-form" cpuinfo is present, parse that to populate models. |
Michalis Spyrou | 8e5174c | 2018-12-04 11:43:23 +0000 | [diff] [blame] | 200 | ret_status |= regcomp(&proc_regex, R"(^processor.*([[:digit:]]+)$)", REG_EXTENDED); |
| 201 | ret_status |= regcomp(&imp_regex, R"(^CPU implementer.*0x(..)$)", REG_EXTENDED); |
| 202 | ret_status |= regcomp(&var_regex, R"(^CPU variant.*0x(.)$)", REG_EXTENDED); |
| 203 | ret_status |= regcomp(&part_regex, R"(^CPU part.*0x(...)$)", REG_EXTENDED); |
| 204 | ret_status |= regcomp(&rev_regex, R"(^CPU revision.*([[:digit:]]+)$)", REG_EXTENDED); |
| 205 | ARM_COMPUTE_UNUSED(ret_status); |
| 206 | ARM_COMPUTE_ERROR_ON_MSG(ret_status != 0, "Regex compilation failed."); |
Pablo Tello | 7fad9b1 | 2018-03-14 17:55:27 +0000 | [diff] [blame] | 207 | |
| 208 | std::ifstream file; |
| 209 | file.open("/proc/cpuinfo", std::ios::in); |
| 210 | |
| 211 | if(file.is_open()) |
| 212 | { |
| 213 | std::string line; |
| 214 | int midr = 0; |
| 215 | int curcpu = -1; |
| 216 | |
| 217 | while(bool(getline(file, line))) |
| 218 | { |
Michalis Spyrou | bcfd09a | 2019-05-01 13:03:59 +0100 | [diff] [blame] | 219 | std::array<regmatch_t, 2> match; |
| 220 | ret_status = regexec(&proc_regex, line.c_str(), 2, match.data(), 0); |
Michalis Spyrou | 8e5174c | 2018-12-04 11:43:23 +0000 | [diff] [blame] | 221 | if(ret_status == 0) |
Pablo Tello | 7fad9b1 | 2018-03-14 17:55:27 +0000 | [diff] [blame] | 222 | { |
Michalis Spyrou | 8e5174c | 2018-12-04 11:43:23 +0000 | [diff] [blame] | 223 | std::string id = line.substr(match[1].rm_so, (match[1].rm_eo - match[1].rm_so)); |
Pablo Tello | 65f9982 | 2018-05-24 11:40:15 +0100 | [diff] [blame] | 224 | int newcpu = support::cpp11::stoi(id, nullptr); |
Pablo Tello | 7fad9b1 | 2018-03-14 17:55:27 +0000 | [diff] [blame] | 225 | |
| 226 | if(curcpu >= 0 && midr == 0) |
| 227 | { |
| 228 | // Matched a new CPU ID without any description of the previous one - looks like old format. |
| 229 | return; |
| 230 | } |
| 231 | |
| 232 | if(curcpu >= 0) |
| 233 | { |
Anthony Barbier | 3efb375 | 2018-07-20 15:30:46 +0100 | [diff] [blame] | 234 | cpusv[curcpu] = midr_to_model(midr); |
Pablo Tello | 7fad9b1 | 2018-03-14 17:55:27 +0000 | [diff] [blame] | 235 | } |
| 236 | |
| 237 | midr = 0; |
| 238 | curcpu = newcpu; |
| 239 | |
| 240 | continue; |
| 241 | } |
| 242 | |
Michalis Spyrou | bcfd09a | 2019-05-01 13:03:59 +0100 | [diff] [blame] | 243 | ret_status = regexec(&imp_regex, line.c_str(), 2, match.data(), 0); |
Michalis Spyrou | 8e5174c | 2018-12-04 11:43:23 +0000 | [diff] [blame] | 244 | if(ret_status == 0) |
Pablo Tello | 7fad9b1 | 2018-03-14 17:55:27 +0000 | [diff] [blame] | 245 | { |
Michalis Spyrou | 8e5174c | 2018-12-04 11:43:23 +0000 | [diff] [blame] | 246 | std::string subexp = line.substr(match[1].rm_so, (match[1].rm_eo - match[1].rm_so)); |
| 247 | int impv = support::cpp11::stoi(subexp, nullptr, support::cpp11::NumericBase::BASE_16); |
Pablo Tello | 7fad9b1 | 2018-03-14 17:55:27 +0000 | [diff] [blame] | 248 | midr |= (impv << 24); |
Michalis Spyrou | 8e5174c | 2018-12-04 11:43:23 +0000 | [diff] [blame] | 249 | |
Pablo Tello | 7fad9b1 | 2018-03-14 17:55:27 +0000 | [diff] [blame] | 250 | continue; |
| 251 | } |
| 252 | |
Michalis Spyrou | bcfd09a | 2019-05-01 13:03:59 +0100 | [diff] [blame] | 253 | ret_status = regexec(&var_regex, line.c_str(), 2, match.data(), 0); |
Michalis Spyrou | 8e5174c | 2018-12-04 11:43:23 +0000 | [diff] [blame] | 254 | if(ret_status == 0) |
Pablo Tello | 7fad9b1 | 2018-03-14 17:55:27 +0000 | [diff] [blame] | 255 | { |
Michalis Spyrou | 8e5174c | 2018-12-04 11:43:23 +0000 | [diff] [blame] | 256 | std::string subexp = line.substr(match[1].rm_so, (match[1].rm_eo - match[1].rm_so)); |
| 257 | int varv = support::cpp11::stoi(subexp, nullptr, support::cpp11::NumericBase::BASE_16); |
Anthony Barbier | 3efb375 | 2018-07-20 15:30:46 +0100 | [diff] [blame] | 258 | midr |= (varv << 20); |
Michalis Spyrou | 8e5174c | 2018-12-04 11:43:23 +0000 | [diff] [blame] | 259 | |
Pablo Tello | 7fad9b1 | 2018-03-14 17:55:27 +0000 | [diff] [blame] | 260 | continue; |
| 261 | } |
| 262 | |
Michalis Spyrou | bcfd09a | 2019-05-01 13:03:59 +0100 | [diff] [blame] | 263 | ret_status = regexec(&part_regex, line.c_str(), 2, match.data(), 0); |
Michalis Spyrou | 8e5174c | 2018-12-04 11:43:23 +0000 | [diff] [blame] | 264 | if(ret_status == 0) |
Pablo Tello | 7fad9b1 | 2018-03-14 17:55:27 +0000 | [diff] [blame] | 265 | { |
Michalis Spyrou | 8e5174c | 2018-12-04 11:43:23 +0000 | [diff] [blame] | 266 | std::string subexp = line.substr(match[1].rm_so, (match[1].rm_eo - match[1].rm_so)); |
| 267 | int partv = support::cpp11::stoi(subexp, nullptr, support::cpp11::NumericBase::BASE_16); |
Pablo Tello | 7fad9b1 | 2018-03-14 17:55:27 +0000 | [diff] [blame] | 268 | midr |= (partv << 4); |
Michalis Spyrou | 8e5174c | 2018-12-04 11:43:23 +0000 | [diff] [blame] | 269 | |
Pablo Tello | 7fad9b1 | 2018-03-14 17:55:27 +0000 | [diff] [blame] | 270 | continue; |
| 271 | } |
| 272 | |
Michalis Spyrou | bcfd09a | 2019-05-01 13:03:59 +0100 | [diff] [blame] | 273 | ret_status = regexec(&rev_regex, line.c_str(), 2, match.data(), 0); |
Michalis Spyrou | 8e5174c | 2018-12-04 11:43:23 +0000 | [diff] [blame] | 274 | if(ret_status == 0) |
Pablo Tello | 7fad9b1 | 2018-03-14 17:55:27 +0000 | [diff] [blame] | 275 | { |
Michalis Spyrou | 8e5174c | 2018-12-04 11:43:23 +0000 | [diff] [blame] | 276 | std::string subexp = line.substr(match[1].rm_so, (match[1].rm_eo - match[1].rm_so)); |
| 277 | int regv = support::cpp11::stoi(subexp, nullptr); |
Pablo Tello | 7fad9b1 | 2018-03-14 17:55:27 +0000 | [diff] [blame] | 278 | midr |= (regv); |
| 279 | midr |= (0xf << 16); |
Michalis Spyrou | 8e5174c | 2018-12-04 11:43:23 +0000 | [diff] [blame] | 280 | |
Pablo Tello | 7fad9b1 | 2018-03-14 17:55:27 +0000 | [diff] [blame] | 281 | continue; |
| 282 | } |
| 283 | } |
| 284 | |
| 285 | if(curcpu >= 0) |
| 286 | { |
Anthony Barbier | 3efb375 | 2018-07-20 15:30:46 +0100 | [diff] [blame] | 287 | cpusv[curcpu] = midr_to_model(midr); |
Pablo Tello | 7fad9b1 | 2018-03-14 17:55:27 +0000 | [diff] [blame] | 288 | } |
| 289 | } |
Michalis Spyrou | 8e5174c | 2018-12-04 11:43:23 +0000 | [diff] [blame] | 290 | |
| 291 | // Free allocated memory |
| 292 | regfree(&proc_regex); |
| 293 | regfree(&imp_regex); |
| 294 | regfree(&var_regex); |
| 295 | regfree(&part_regex); |
| 296 | regfree(&rev_regex); |
Pablo Tello | 7fad9b1 | 2018-03-14 17:55:27 +0000 | [diff] [blame] | 297 | } |
| 298 | |
| 299 | int get_max_cpus() |
| 300 | { |
Michalis Spyrou | bcfd09a | 2019-05-01 13:03:59 +0100 | [diff] [blame] | 301 | int max_cpus = 1; |
Pablo Tello | 7fad9b1 | 2018-03-14 17:55:27 +0000 | [diff] [blame] | 302 | std::ifstream CPUspresent; |
| 303 | CPUspresent.open("/sys/devices/system/cpu/present", std::ios::in); |
| 304 | bool success = false; |
| 305 | |
| 306 | if(CPUspresent.is_open()) |
| 307 | { |
| 308 | std::string line; |
| 309 | |
| 310 | if(bool(getline(CPUspresent, line))) |
| 311 | { |
| 312 | /* The content of this file is a list of ranges or single values, e.g. |
| 313 | * 0-5, or 1-3,5,7 or similar. As we are interested in the |
| 314 | * max valid ID, we just need to find the last valid |
| 315 | * delimiter ('-' or ',') and parse the integer immediately after that. |
| 316 | */ |
| 317 | auto startfrom = line.begin(); |
| 318 | |
| 319 | for(auto i = line.begin(); i < line.end(); ++i) |
| 320 | { |
| 321 | if(*i == '-' || *i == ',') |
| 322 | { |
| 323 | startfrom = i + 1; |
| 324 | } |
| 325 | } |
| 326 | |
| 327 | line.erase(line.begin(), startfrom); |
| 328 | |
Pablo Tello | 65f9982 | 2018-05-24 11:40:15 +0100 | [diff] [blame] | 329 | max_cpus = support::cpp11::stoi(line, nullptr) + 1; |
Pablo Tello | 7fad9b1 | 2018-03-14 17:55:27 +0000 | [diff] [blame] | 330 | success = true; |
| 331 | } |
| 332 | } |
| 333 | |
| 334 | // Return std::thread::hardware_concurrency() as a fallback. |
| 335 | if(!success) |
| 336 | { |
| 337 | max_cpus = std::thread::hardware_concurrency(); |
| 338 | } |
Pablo Tello | 7fad9b1 | 2018-03-14 17:55:27 +0000 | [diff] [blame] | 339 | return max_cpus; |
| 340 | } |
| 341 | #endif /* !defined(BARE_METAL) && (defined(__arm__) || defined(__aarch64__)) */ |
| 342 | |
| 343 | } // namespace |
| 344 | |
| 345 | namespace arm_compute |
| 346 | { |
| 347 | void get_cpu_configuration(CPUInfo &cpuinfo) |
| 348 | { |
| 349 | #if !defined(BARE_METAL) && (defined(__arm__) || defined(__aarch64__)) |
Anthony Barbier | 5a3ee4f | 2018-07-24 11:24:17 +0100 | [diff] [blame] | 350 | bool cpuid = false; |
| 351 | bool hwcaps_fp16_support = false; |
| 352 | bool hwcaps_dot_support = false; |
Pablo Tello | 7fad9b1 | 2018-03-14 17:55:27 +0000 | [diff] [blame] | 353 | |
| 354 | const uint32_t hwcaps = getauxval(AT_HWCAP); |
| 355 | |
| 356 | if((hwcaps & HWCAP_CPUID) != 0) |
| 357 | { |
| 358 | cpuid = true; |
| 359 | } |
| 360 | |
| 361 | if((hwcaps & HWCAP_ASIMDHP) != 0) |
| 362 | { |
Anthony Barbier | 5a3ee4f | 2018-07-24 11:24:17 +0100 | [diff] [blame] | 363 | hwcaps_fp16_support = true; |
Pablo Tello | 7fad9b1 | 2018-03-14 17:55:27 +0000 | [diff] [blame] | 364 | } |
| 365 | |
Pablo Tello | df3b5bb | 2018-10-09 10:51:51 +0100 | [diff] [blame] | 366 | #if defined(__aarch64__) |
Pablo Tello | 7fad9b1 | 2018-03-14 17:55:27 +0000 | [diff] [blame] | 367 | if((hwcaps & HWCAP_ASIMDDP) != 0) |
| 368 | { |
Anthony Barbier | 5a3ee4f | 2018-07-24 11:24:17 +0100 | [diff] [blame] | 369 | hwcaps_dot_support = true; |
Pablo Tello | 7fad9b1 | 2018-03-14 17:55:27 +0000 | [diff] [blame] | 370 | } |
Pablo Tello | df3b5bb | 2018-10-09 10:51:51 +0100 | [diff] [blame] | 371 | #endif /* defined(__aarch64__) */ |
Pablo Tello | 7fad9b1 | 2018-03-14 17:55:27 +0000 | [diff] [blame] | 372 | |
Pablo Tello | 7fad9b1 | 2018-03-14 17:55:27 +0000 | [diff] [blame] | 373 | const unsigned int max_cpus = get_max_cpus(); |
| 374 | cpuinfo.set_cpu_num(max_cpus); |
Anthony Barbier | 3efb375 | 2018-07-20 15:30:46 +0100 | [diff] [blame] | 375 | std::vector<CPUModel> percpu(max_cpus, CPUModel::GENERIC); |
Pablo Tello | 7fad9b1 | 2018-03-14 17:55:27 +0000 | [diff] [blame] | 376 | if(cpuid) |
| 377 | { |
| 378 | populate_models_cpuid(percpu); |
| 379 | } |
| 380 | else |
| 381 | { |
| 382 | populate_models_cpuinfo(percpu); |
| 383 | } |
| 384 | int j(0); |
Georgios Pinitas | ecae3a1 | 2018-10-09 15:13:12 +0100 | [diff] [blame] | 385 | // Update dot product and FP16 support if one of the CPUs support these features |
| 386 | // We assume that the system does not have mixed architectures |
| 387 | bool one_supports_dot = false; |
| 388 | bool one_supports_fp16 = false; |
Pablo Tello | 7fad9b1 | 2018-03-14 17:55:27 +0000 | [diff] [blame] | 389 | for(const auto &v : percpu) |
| 390 | { |
Georgios Pinitas | 9c2ec7e | 2018-11-12 17:07:18 +0000 | [diff] [blame] | 391 | one_supports_dot = one_supports_dot || model_supports_dot(v); |
| 392 | one_supports_fp16 = one_supports_fp16 || model_supports_fp16(v); |
Anthony Barbier | 3efb375 | 2018-07-20 15:30:46 +0100 | [diff] [blame] | 393 | cpuinfo.set_cpu_model(j++, v); |
Pablo Tello | 7fad9b1 | 2018-03-14 17:55:27 +0000 | [diff] [blame] | 394 | } |
Georgios Pinitas | ecae3a1 | 2018-10-09 15:13:12 +0100 | [diff] [blame] | 395 | cpuinfo.set_dotprod(one_supports_dot || hwcaps_dot_support); |
| 396 | cpuinfo.set_fp16(one_supports_fp16 || hwcaps_fp16_support); |
Pablo Tello | 7fad9b1 | 2018-03-14 17:55:27 +0000 | [diff] [blame] | 397 | #else /* !defined(BARE_METAL) && (defined(__arm__) || defined(__aarch64__)) */ |
| 398 | ARM_COMPUTE_UNUSED(cpuinfo); |
| 399 | #endif /* !defined(BARE_METAL) && (defined(__arm__) || defined(__aarch64__)) */ |
| 400 | } |
| 401 | |
| 402 | unsigned int get_threads_hint() |
| 403 | { |
| 404 | unsigned int num_threads_hint = 1; |
| 405 | |
Georgios Pinitas | e8291ac | 2020-02-26 09:58:13 +0000 | [diff] [blame] | 406 | #if !defined(BARE_METAL) |
Pablo Tello | 7fad9b1 | 2018-03-14 17:55:27 +0000 | [diff] [blame] | 407 | std::map<std::string, unsigned int> cpu_part_occurrence_map; |
| 408 | |
| 409 | // CPU part regex |
Michalis Spyrou | 8e5174c | 2018-12-04 11:43:23 +0000 | [diff] [blame] | 410 | regex_t cpu_part_rgx; |
| 411 | memset(&cpu_part_rgx, 0, sizeof(regex_t)); |
Georgios Pinitas | de6dbfe | 2018-12-24 15:02:55 +0000 | [diff] [blame] | 412 | int ret_status = regcomp(&cpu_part_rgx, R"(.*CPU part.+/?\:[[:space:]]+([[:alnum:]]+).*)", REG_EXTENDED); |
Michalis Spyrou | 8e5174c | 2018-12-04 11:43:23 +0000 | [diff] [blame] | 413 | ARM_COMPUTE_UNUSED(ret_status); |
| 414 | ARM_COMPUTE_ERROR_ON_MSG(ret_status != 0, "Regex compilation failed."); |
Pablo Tello | 7fad9b1 | 2018-03-14 17:55:27 +0000 | [diff] [blame] | 415 | |
| 416 | // Read cpuinfo and get occurrence of each core |
| 417 | std::ifstream cpuinfo; |
| 418 | cpuinfo.open("/proc/cpuinfo", std::ios::in); |
| 419 | if(cpuinfo.is_open()) |
| 420 | { |
| 421 | std::string line; |
| 422 | while(bool(getline(cpuinfo, line))) |
| 423 | { |
Michalis Spyrou | bcfd09a | 2019-05-01 13:03:59 +0100 | [diff] [blame] | 424 | std::array<regmatch_t, 2> match; |
| 425 | ret_status = regexec(&cpu_part_rgx, line.c_str(), 2, match.data(), 0); |
Michalis Spyrou | 8e5174c | 2018-12-04 11:43:23 +0000 | [diff] [blame] | 426 | if(ret_status == 0) |
Pablo Tello | 7fad9b1 | 2018-03-14 17:55:27 +0000 | [diff] [blame] | 427 | { |
Michalis Spyrou | 8e5174c | 2018-12-04 11:43:23 +0000 | [diff] [blame] | 428 | std::string cpu_part = line.substr(match[1].rm_so, (match[1].rm_eo - match[1].rm_so)); |
Pablo Tello | 7fad9b1 | 2018-03-14 17:55:27 +0000 | [diff] [blame] | 429 | if(cpu_part_occurrence_map.find(cpu_part) != cpu_part_occurrence_map.end()) |
| 430 | { |
| 431 | cpu_part_occurrence_map[cpu_part]++; |
| 432 | } |
| 433 | else |
| 434 | { |
| 435 | cpu_part_occurrence_map[cpu_part] = 1; |
| 436 | } |
| 437 | } |
| 438 | } |
| 439 | } |
Michalis Spyrou | 8e5174c | 2018-12-04 11:43:23 +0000 | [diff] [blame] | 440 | regfree(&cpu_part_rgx); |
Pablo Tello | 7fad9b1 | 2018-03-14 17:55:27 +0000 | [diff] [blame] | 441 | |
| 442 | // Get min number of threads |
| 443 | auto min_common_cores = std::min_element(cpu_part_occurrence_map.begin(), cpu_part_occurrence_map.end(), |
| 444 | [](const std::pair<std::string, unsigned int> &p1, const std::pair<std::string, unsigned int> &p2) |
| 445 | { |
| 446 | return p1.second < p2.second; |
| 447 | }); |
| 448 | |
| 449 | // Set thread hint |
| 450 | num_threads_hint = cpu_part_occurrence_map.empty() ? std::thread::hardware_concurrency() : min_common_cores->second; |
Georgios Pinitas | e8291ac | 2020-02-26 09:58:13 +0000 | [diff] [blame] | 451 | #endif /* !defined(BARE_METAL) */ |
Pablo Tello | 7fad9b1 | 2018-03-14 17:55:27 +0000 | [diff] [blame] | 452 | |
| 453 | return num_threads_hint; |
| 454 | } |
| 455 | |
| 456 | } // namespace arm_compute |