blob: 6c210861bc92b78e152fddff4712776b873f88d1 [file] [log] [blame]
Jenkinsb3a371b2018-05-23 11:36:53 +01001/*
2 * Copyright (c) 2018 ARM Limited.
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"
28#include "support/ToolchainSupport.h"
29
30#include <array>
31#include <cstdlib>
32#include <cstring>
33#include <fcntl.h>
34#include <fstream>
35#include <map>
36#include <sched.h>
37#include <sys/stat.h>
38#include <sys/types.h>
39#include <unistd.h>
40
41#ifndef BARE_METAL
42#include <regex>
43#include <thread>
44#endif /* BARE_METAL */
45
46#if !defined(BARE_METAL) && (defined(__arm__) || defined(__aarch64__))
47#include <sys/auxv.h>
48
49/* Get HWCAP bits from asm/hwcap.h */
50#include <asm/hwcap.h>
51#endif /* !BARE_METAL */
52
53/* Make sure the bits we care about are defined, just in case asm/hwcap.h is
54 * out of date (or for bare metal mode) */
55#ifndef HWCAP_ASIMDHP
56#define HWCAP_ASIMDHP (1 << 10)
57#endif /* HWCAP_ASIMDHP */
58
59#ifndef HWCAP_CPUID
60#define HWCAP_CPUID (1 << 11)
61#endif /* HWCAP_CPUID */
62
63#ifndef HWCAP_ASIMDDP
64#define HWCAP_ASIMDDP (1 << 20)
65#endif /* HWCAP_ASIMDDP */
66
67namespace
68{
69using namespace arm_compute;
70
71#if !defined(BARE_METAL) && (defined(__arm__) || defined(__aarch64__))
Jenkinsb3a371b2018-05-23 11:36:53 +010072
Jenkins52ba29e2018-08-29 15:32:11 +000073bool model_supports_dot(CPUModel model)
74{
75 switch(model)
76 {
77 case CPUModel::GENERIC_FP16_DOT:
78 case CPUModel::A55r1:
79 return true;
80 default:
81 return false;
82 }
83}
84
85bool model_supports_fp16(CPUModel model)
86{
87 switch(model)
88 {
89 case CPUModel::GENERIC_FP16:
90 case CPUModel::GENERIC_FP16_DOT:
91 case CPUModel::A55r1:
92 return true;
93 default:
94 return false;
95 }
96}
Jenkinsb3a371b2018-05-23 11:36:53 +010097/* Convert an MIDR register value to a CPUModel enum value. */
98CPUModel midr_to_model(const unsigned int midr)
99{
Jenkins52ba29e2018-08-29 15:32:11 +0000100 CPUModel model = CPUModel::GENERIC;
Jenkinsb3a371b2018-05-23 11:36:53 +0100101
102 // Unpack variant and CPU ID
Jenkins52ba29e2018-08-29 15:32:11 +0000103 const int implementer = (midr >> 24) & 0xFF;
104 const int variant = (midr >> 20) & 0xF;
105 const int cpunum = (midr >> 4) & 0xFFF;
Jenkinsb3a371b2018-05-23 11:36:53 +0100106
Jenkins52ba29e2018-08-29 15:32:11 +0000107 if(implementer == 0x41) // Arm CPUs
Jenkinsb3a371b2018-05-23 11:36:53 +0100108 {
Jenkins52ba29e2018-08-29 15:32:11 +0000109 // Only CPUs we have code paths for are detected. All other CPUs can be safely classed as "GENERIC"
110 switch(cpunum)
111 {
112 case 0xd03: // A53
113 case 0xd04: // A35
114 model = CPUModel::A53;
115 break;
116 case 0xd05: // A55
117 if(variant != 0)
118 {
119 model = CPUModel::A55r1;
120 }
121 else
122 {
123 model = CPUModel::A55r0;
124 }
125 break;
126 case 0xd0a: // A75
127 if(variant != 0)
128 {
129 model = CPUModel::GENERIC_FP16_DOT;
130 }
131 else
132 {
133 model = CPUModel::GENERIC_FP16;
134 }
135 break;
136 case 0xd0b: // A76
137 model = CPUModel::GENERIC_FP16_DOT;
138 break;
139 default:
140 model = CPUModel::GENERIC;
141 break;
142 }
Jenkinsb3a371b2018-05-23 11:36:53 +0100143 }
144
145 return model;
146}
147
Jenkins52ba29e2018-08-29 15:32:11 +0000148void populate_models_cpuid(std::vector<CPUModel> &cpusv)
Jenkinsb3a371b2018-05-23 11:36:53 +0100149{
150 // If the CPUID capability is present, MIDR information is provided in /sys. Use that to populate the CPU model table.
151 uint32_t i = 0;
152 for(auto &c : cpusv)
153 {
154 std::stringstream str;
155 str << "/sys/devices/system/cpu/cpu" << i++ << "/regs/identification/midr_el1";
156 std::ifstream file;
157 file.open(str.str(), std::ios::in);
158 if(file.is_open())
159 {
160 std::string line;
161 if(bool(getline(file, line)))
162 {
Jenkins52ba29e2018-08-29 15:32:11 +0000163 const unsigned long midr = support::cpp11::stoul(line, nullptr, support::cpp11::NumericBase::BASE_16);
164 c = midr_to_model(midr & 0xffffffff);
Jenkinsb3a371b2018-05-23 11:36:53 +0100165 }
166 }
167 }
168}
169
Jenkins52ba29e2018-08-29 15:32:11 +0000170void populate_models_cpuinfo(std::vector<CPUModel> &cpusv)
Jenkinsb3a371b2018-05-23 11:36:53 +0100171{
172 // If "long-form" cpuinfo is present, parse that to populate models.
173 std::regex proc_regex("^processor.*(\\d+)$");
174 std::regex imp_regex("^CPU implementer.*0x(..)$");
175 std::regex var_regex("^CPU variant.*0x(.)$");
176 std::regex part_regex("^CPU part.*0x(...)$");
177 std::regex rev_regex("^CPU revision.*(\\d+)$");
178
179 std::ifstream file;
180 file.open("/proc/cpuinfo", std::ios::in);
181
182 if(file.is_open())
183 {
184 std::string line;
185 int midr = 0;
186 int curcpu = -1;
187
188 while(bool(getline(file, line)))
189 {
190 std::smatch match;
191
192 if(std::regex_match(line, match, proc_regex))
193 {
194 std::string id = match[1];
Jenkins52ba29e2018-08-29 15:32:11 +0000195 int newcpu = support::cpp11::stoi(id, nullptr);
Jenkinsb3a371b2018-05-23 11:36:53 +0100196
197 if(curcpu >= 0 && midr == 0)
198 {
199 // Matched a new CPU ID without any description of the previous one - looks like old format.
200 return;
201 }
202
203 if(curcpu >= 0)
204 {
Jenkins52ba29e2018-08-29 15:32:11 +0000205 cpusv[curcpu] = midr_to_model(midr);
Jenkinsb3a371b2018-05-23 11:36:53 +0100206 }
207
208 midr = 0;
209 curcpu = newcpu;
210
211 continue;
212 }
213
214 if(std::regex_match(line, match, imp_regex))
215 {
Jenkins52ba29e2018-08-29 15:32:11 +0000216 int impv = support::cpp11::stoi(match[1], nullptr, support::cpp11::NumericBase::BASE_16);
Jenkinsb3a371b2018-05-23 11:36:53 +0100217 midr |= (impv << 24);
218 continue;
219 }
220
221 if(std::regex_match(line, match, var_regex))
222 {
Jenkins52ba29e2018-08-29 15:32:11 +0000223 int varv = support::cpp11::stoi(match[1], nullptr, support::cpp11::NumericBase::BASE_16);
224 midr |= (varv << 20);
Jenkinsb3a371b2018-05-23 11:36:53 +0100225 continue;
226 }
227
228 if(std::regex_match(line, match, part_regex))
229 {
Jenkins52ba29e2018-08-29 15:32:11 +0000230 int partv = support::cpp11::stoi(match[1], nullptr, support::cpp11::NumericBase::BASE_16);
Jenkinsb3a371b2018-05-23 11:36:53 +0100231 midr |= (partv << 4);
232 continue;
233 }
234
235 if(std::regex_match(line, match, rev_regex))
236 {
Jenkins52ba29e2018-08-29 15:32:11 +0000237 int regv = support::cpp11::stoi(match[1], nullptr);
Jenkinsb3a371b2018-05-23 11:36:53 +0100238 midr |= (regv);
239 midr |= (0xf << 16);
240 continue;
241 }
242 }
243
244 if(curcpu >= 0)
245 {
Jenkins52ba29e2018-08-29 15:32:11 +0000246 cpusv[curcpu] = midr_to_model(midr);
Jenkinsb3a371b2018-05-23 11:36:53 +0100247 }
248 }
249}
250
251int get_max_cpus()
252{
253 int max_cpus = 1;
254#if !defined(BARE_METAL) && (defined(__arm__) || defined(__aarch64__))
255 std::ifstream CPUspresent;
256 CPUspresent.open("/sys/devices/system/cpu/present", std::ios::in);
257 bool success = false;
258
259 if(CPUspresent.is_open())
260 {
261 std::string line;
262
263 if(bool(getline(CPUspresent, line)))
264 {
265 /* The content of this file is a list of ranges or single values, e.g.
266 * 0-5, or 1-3,5,7 or similar. As we are interested in the
267 * max valid ID, we just need to find the last valid
268 * delimiter ('-' or ',') and parse the integer immediately after that.
269 */
270 auto startfrom = line.begin();
271
272 for(auto i = line.begin(); i < line.end(); ++i)
273 {
274 if(*i == '-' || *i == ',')
275 {
276 startfrom = i + 1;
277 }
278 }
279
280 line.erase(line.begin(), startfrom);
281
Jenkins52ba29e2018-08-29 15:32:11 +0000282 max_cpus = support::cpp11::stoi(line, nullptr) + 1;
Jenkinsb3a371b2018-05-23 11:36:53 +0100283 success = true;
284 }
285 }
286
287 // Return std::thread::hardware_concurrency() as a fallback.
288 if(!success)
289 {
290 max_cpus = std::thread::hardware_concurrency();
291 }
292#endif /* BARE_METAL */
Jenkinsb3a371b2018-05-23 11:36:53 +0100293 return max_cpus;
294}
295#endif /* !defined(BARE_METAL) && (defined(__arm__) || defined(__aarch64__)) */
296
297} // namespace
298
299namespace arm_compute
300{
301void get_cpu_configuration(CPUInfo &cpuinfo)
302{
303#if !defined(BARE_METAL) && (defined(__arm__) || defined(__aarch64__))
Jenkins52ba29e2018-08-29 15:32:11 +0000304 bool cpuid = false;
305 bool hwcaps_fp16_support = false;
306 bool hwcaps_dot_support = false;
Jenkinsb3a371b2018-05-23 11:36:53 +0100307
308 const uint32_t hwcaps = getauxval(AT_HWCAP);
309
310 if((hwcaps & HWCAP_CPUID) != 0)
311 {
312 cpuid = true;
313 }
314
315 if((hwcaps & HWCAP_ASIMDHP) != 0)
316 {
Jenkins52ba29e2018-08-29 15:32:11 +0000317 hwcaps_fp16_support = true;
Jenkinsb3a371b2018-05-23 11:36:53 +0100318 }
319
320 if((hwcaps & HWCAP_ASIMDDP) != 0)
321 {
Jenkins52ba29e2018-08-29 15:32:11 +0000322 hwcaps_dot_support = true;
Jenkinsb3a371b2018-05-23 11:36:53 +0100323 }
324
Jenkinsb3a371b2018-05-23 11:36:53 +0100325 const unsigned int max_cpus = get_max_cpus();
326 cpuinfo.set_cpu_num(max_cpus);
Jenkins52ba29e2018-08-29 15:32:11 +0000327 std::vector<CPUModel> percpu(max_cpus, CPUModel::GENERIC);
Jenkinsb3a371b2018-05-23 11:36:53 +0100328 if(cpuid)
329 {
330 populate_models_cpuid(percpu);
331 }
332 else
333 {
334 populate_models_cpuinfo(percpu);
335 }
336 int j(0);
Jenkins52ba29e2018-08-29 15:32:11 +0000337 // Update dot product and FP16 support if all CPUs support these features:
338 bool all_support_dot = true;
339 bool all_support_fp16 = true;
Jenkinsb3a371b2018-05-23 11:36:53 +0100340 for(const auto &v : percpu)
341 {
Jenkins52ba29e2018-08-29 15:32:11 +0000342 all_support_dot &= model_supports_dot(v);
343 all_support_fp16 &= model_supports_fp16(v);
344 cpuinfo.set_cpu_model(j++, v);
Jenkinsb3a371b2018-05-23 11:36:53 +0100345 }
Jenkins52ba29e2018-08-29 15:32:11 +0000346 cpuinfo.set_dotprod(all_support_dot || hwcaps_dot_support);
347 cpuinfo.set_fp16(all_support_fp16 || hwcaps_fp16_support);
Jenkinsb3a371b2018-05-23 11:36:53 +0100348#else /* !defined(BARE_METAL) && (defined(__arm__) || defined(__aarch64__)) */
349 ARM_COMPUTE_UNUSED(cpuinfo);
350#endif /* !defined(BARE_METAL) && (defined(__arm__) || defined(__aarch64__)) */
351}
352
353unsigned int get_threads_hint()
354{
355 unsigned int num_threads_hint = 1;
356
357#ifndef BARE_METAL
358 std::map<std::string, unsigned int> cpu_part_occurrence_map;
359
360 // CPU part regex
361 std::regex cpu_part_rgx(R"(.*CPU part.+?(?=:).+?(?=\w+)(\w+).*)");
362 std::smatch cpu_part_match;
363
364 // Read cpuinfo and get occurrence of each core
365 std::ifstream cpuinfo;
366 cpuinfo.open("/proc/cpuinfo", std::ios::in);
367 if(cpuinfo.is_open())
368 {
369 std::string line;
370 while(bool(getline(cpuinfo, line)))
371 {
372 if(std::regex_search(line.cbegin(), line.cend(), cpu_part_match, cpu_part_rgx))
373 {
374 std::string cpu_part = cpu_part_match[1];
375 if(cpu_part_occurrence_map.find(cpu_part) != cpu_part_occurrence_map.end())
376 {
377 cpu_part_occurrence_map[cpu_part]++;
378 }
379 else
380 {
381 cpu_part_occurrence_map[cpu_part] = 1;
382 }
383 }
384 }
385 }
386
387 // Get min number of threads
388 auto min_common_cores = std::min_element(cpu_part_occurrence_map.begin(), cpu_part_occurrence_map.end(),
389 [](const std::pair<std::string, unsigned int> &p1, const std::pair<std::string, unsigned int> &p2)
390 {
391 return p1.second < p2.second;
392 });
393
394 // Set thread hint
395 num_threads_hint = cpu_part_occurrence_map.empty() ? std::thread::hardware_concurrency() : min_common_cores->second;
396#endif /* BARE_METAL */
397
398 return num_threads_hint;
399}
400
401} // namespace arm_compute