You can not select more than 25 topics Topics must start with a chinese character,a letter or number, can include dashes ('-') and can be up to 35 characters long.

cpu.cpp 11 kB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527
  1. // Tencent is pleased to support the open source community by making ncnn available.
  2. //
  3. // Copyright (C) 2017 THL A29 Limited, a Tencent company. All rights reserved.
  4. //
  5. // Licensed under the BSD 3-Clause License (the "License"); you may not use this file except
  6. // in compliance with the License. You may obtain a copy of the License at
  7. //
  8. // https://opensource.org/licenses/BSD-3-Clause
  9. //
  10. // Unless required by applicable law or agreed to in writing, software distributed
  11. // under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR
  12. // CONDITIONS OF ANY KIND, either express or implied. See the License for the
  13. // specific language governing permissions and limitations under the License.
  14. #include "cpu.h"
  15. #include <stdio.h>
  16. #include <string.h>
  17. #include <vector>
  18. #ifdef _OPENMP
  19. #include <omp.h>
  20. #endif
  21. #ifdef __ANDROID__
  22. #include <sys/syscall.h>
  23. #include <unistd.h>
  24. #include <stdint.h>
  25. #endif
  26. #if __APPLE__
  27. #include "TargetConditionals.h"
  28. #if TARGET_OS_IPHONE
  29. #include <sys/types.h>
  30. #include <sys/sysctl.h>
  31. #include <mach/machine.h>
  32. #define __IOS__ 1
  33. #endif
  34. #endif
  35. namespace ncnn {
  36. #ifdef __ANDROID__
  37. // extract the ELF HW capabilities bitmap from /proc/self/auxv
  38. static unsigned int get_elf_hwcap_from_proc_self_auxv()
  39. {
  40. FILE* fp = fopen("/proc/self/auxv", "rb");
  41. if (!fp)
  42. {
  43. return 0;
  44. }
  45. #define AT_HWCAP 16
  46. #define AT_HWCAP2 26
  47. #if __aarch64__
  48. struct { uint64_t tag; uint64_t value; } entry;
  49. #else
  50. struct { unsigned int tag; unsigned int value; } entry;
  51. #endif
  52. unsigned int result = 0;
  53. while (!feof(fp))
  54. {
  55. int nread = fread((char*)&entry, sizeof(entry), 1, fp);
  56. if (nread != 1)
  57. break;
  58. if (entry.tag == 0 && entry.value == 0)
  59. break;
  60. if (entry.tag == AT_HWCAP)
  61. {
  62. result = entry.value;
  63. break;
  64. }
  65. }
  66. fclose(fp);
  67. return result;
  68. }
  69. static unsigned int g_hwcaps = get_elf_hwcap_from_proc_self_auxv();
  70. #if __aarch64__
  71. // from arch/arm64/include/uapi/asm/hwcap.h
  72. #define HWCAP_ASIMD (1 << 1)
  73. #define HWCAP_ASIMDHP (1 << 10)
  74. #else
  75. // from arch/arm/include/uapi/asm/hwcap.h
  76. #define HWCAP_NEON (1 << 12)
  77. #define HWCAP_VFPv4 (1 << 16)
  78. #endif
  79. #endif // __ANDROID__
  80. #if __IOS__
  81. static unsigned int get_hw_cpufamily()
  82. {
  83. unsigned int value = 0;
  84. size_t len = sizeof(value);
  85. sysctlbyname("hw.cpufamily", &value, &len, NULL, 0);
  86. return value;
  87. }
  88. static cpu_type_t get_hw_cputype()
  89. {
  90. cpu_type_t value = 0;
  91. size_t len = sizeof(value);
  92. sysctlbyname("hw.cputype", &value, &len, NULL, 0);
  93. return value;
  94. }
  95. static cpu_subtype_t get_hw_cpusubtype()
  96. {
  97. cpu_subtype_t value = 0;
  98. size_t len = sizeof(value);
  99. sysctlbyname("hw.cpusubtype", &value, &len, NULL, 0);
  100. return value;
  101. }
  102. static unsigned int g_hw_cpufamily = get_hw_cpufamily();
  103. static cpu_type_t g_hw_cputype = get_hw_cputype();
  104. static cpu_subtype_t g_hw_cpusubtype = get_hw_cpusubtype();
  105. #endif // __IOS__
  106. int cpu_support_arm_neon()
  107. {
  108. #ifdef __ANDROID__
  109. #if __aarch64__
  110. return g_hwcaps & HWCAP_ASIMD;
  111. #else
  112. return g_hwcaps & HWCAP_NEON;
  113. #endif
  114. #elif __IOS__
  115. #if __aarch64__
  116. return g_hw_cputype == CPU_TYPE_ARM64;
  117. #else
  118. return g_hw_cputype == CPU_TYPE_ARM && g_hw_cpusubtype > CPU_SUBTYPE_ARM_V7;
  119. #endif
  120. #else
  121. return 0;
  122. #endif
  123. }
  124. int cpu_support_arm_vfpv4()
  125. {
  126. #ifdef __ANDROID__
  127. #if __aarch64__
  128. // neon always enable fma and fp16
  129. return g_hwcaps & HWCAP_ASIMD;
  130. #else
  131. return g_hwcaps & HWCAP_VFPv4;
  132. #endif
  133. #elif __IOS__
  134. #if __aarch64__
  135. return g_hw_cputype == CPU_TYPE_ARM64;
  136. #else
  137. return g_hw_cputype == CPU_TYPE_ARM && g_hw_cpusubtype > CPU_SUBTYPE_ARM_V7S;
  138. #endif
  139. #else
  140. return 0;
  141. #endif
  142. }
  143. int cpu_support_arm_asimdhp()
  144. {
  145. #ifdef __ANDROID__
  146. #if __aarch64__
  147. return g_hwcaps & HWCAP_ASIMDHP;
  148. #else
  149. return 0;
  150. #endif
  151. #elif __IOS__
  152. #if __aarch64__
  153. #ifndef CPUFAMILY_ARM_HURRICANE
  154. #define CPUFAMILY_ARM_HURRICANE 0x67ceee93
  155. #endif
  156. #ifndef CPUFAMILY_ARM_MONSOON_MISTRAL
  157. #define CPUFAMILY_ARM_MONSOON_MISTRAL 0xe81e7ef6
  158. #endif
  159. return g_hw_cpufamily == CPUFAMILY_ARM_HURRICANE || g_hw_cpufamily == CPUFAMILY_ARM_MONSOON_MISTRAL;
  160. #else
  161. return 0;
  162. #endif
  163. #else
  164. return 0;
  165. #endif
  166. }
  167. static int get_cpucount()
  168. {
  169. #ifdef __ANDROID__
  170. // get cpu count from /proc/cpuinfo
  171. FILE* fp = fopen("/proc/cpuinfo", "rb");
  172. if (!fp)
  173. return 1;
  174. int count = 0;
  175. char line[1024];
  176. while (!feof(fp))
  177. {
  178. char* s = fgets(line, 1024, fp);
  179. if (!s)
  180. break;
  181. if (memcmp(line, "processor", 9) == 0)
  182. {
  183. count++;
  184. }
  185. }
  186. fclose(fp);
  187. if (count < 1)
  188. count = 1;
  189. return count;
  190. #elif __IOS__
  191. int count = 0;
  192. size_t len = sizeof(count);
  193. sysctlbyname("hw.ncpu", &count, &len, NULL, 0);
  194. if (count < 1)
  195. count = 1;
  196. return count;
  197. #else
  198. #ifdef _OPENMP
  199. return omp_get_max_threads();
  200. #else
  201. return 1;
  202. #endif // _OPENMP
  203. #endif
  204. }
  205. static int g_cpucount = get_cpucount();
  206. int get_cpu_count()
  207. {
  208. return g_cpucount;
  209. }
  210. #ifdef __ANDROID__
  211. static int get_max_freq_khz(int cpuid)
  212. {
  213. // first try, for all possible cpu
  214. char path[256];
  215. sprintf(path, "/sys/devices/system/cpu/cpufreq/stats/cpu%d/time_in_state", cpuid);
  216. FILE* fp = fopen(path, "rb");
  217. if (!fp)
  218. {
  219. // second try, for online cpu
  220. sprintf(path, "/sys/devices/system/cpu/cpu%d/cpufreq/stats/time_in_state", cpuid);
  221. fp = fopen(path, "rb");
  222. if (!fp)
  223. {
  224. // third try, for online cpu
  225. sprintf(path, "/sys/devices/system/cpu/cpu%d/cpufreq/cpuinfo_max_freq", cpuid);
  226. fp = fopen(path, "rb");
  227. if (!fp)
  228. return -1;
  229. int max_freq_khz = -1;
  230. fscanf(fp, "%d", &max_freq_khz);
  231. fclose(fp);
  232. return max_freq_khz;
  233. }
  234. }
  235. int max_freq_khz = 0;
  236. while (!feof(fp))
  237. {
  238. int freq_khz = 0;
  239. int nscan = fscanf(fp, "%d %*d", &freq_khz);
  240. if (nscan != 1)
  241. break;
  242. if (freq_khz > max_freq_khz)
  243. max_freq_khz = freq_khz;
  244. }
  245. fclose(fp);
  246. return max_freq_khz;
  247. }
  248. static int set_sched_affinity(const std::vector<int>& cpuids)
  249. {
  250. // cpu_set_t definition
  251. // ref http://stackoverflow.com/questions/16319725/android-set-thread-affinity
  252. #define CPU_SETSIZE 1024
  253. #define __NCPUBITS (8 * sizeof (unsigned long))
  254. typedef struct
  255. {
  256. unsigned long __bits[CPU_SETSIZE / __NCPUBITS];
  257. } cpu_set_t;
  258. #define CPU_SET(cpu, cpusetp) \
  259. ((cpusetp)->__bits[(cpu)/__NCPUBITS] |= (1UL << ((cpu) % __NCPUBITS)))
  260. #define CPU_ZERO(cpusetp) \
  261. memset((cpusetp), 0, sizeof(cpu_set_t))
  262. // set affinity for thread
  263. #ifdef __GLIBC__
  264. pid_t pid = syscall(SYS_gettid);
  265. #else
  266. #ifdef PI3
  267. pid_t pid = getpid();
  268. #else
  269. pid_t pid = gettid();
  270. #endif
  271. #endif
  272. cpu_set_t mask;
  273. CPU_ZERO(&mask);
  274. for (int i=0; i<(int)cpuids.size(); i++)
  275. {
  276. CPU_SET(cpuids[i], &mask);
  277. }
  278. int syscallret = syscall(__NR_sched_setaffinity, pid, sizeof(mask), &mask);
  279. if (syscallret)
  280. {
  281. fprintf(stderr, "syscall error %d\n", syscallret);
  282. return -1;
  283. }
  284. return 0;
  285. }
  286. static int sort_cpuid_by_max_frequency(std::vector<int>& cpuids, int* little_cluster_offset)
  287. {
  288. const int cpu_count = cpuids.size();
  289. *little_cluster_offset = 0;
  290. if (cpu_count == 0)
  291. return 0;
  292. std::vector<int> cpu_max_freq_khz;
  293. cpu_max_freq_khz.resize(cpu_count);
  294. for (int i=0; i<cpu_count; i++)
  295. {
  296. int max_freq_khz = get_max_freq_khz(i);
  297. // printf("%d max freq = %d khz\n", i, max_freq_khz);
  298. cpuids[i] = i;
  299. cpu_max_freq_khz[i] = max_freq_khz;
  300. }
  301. // sort cpuid as big core first
  302. // simple bubble sort
  303. for (int i=0; i<cpu_count; i++)
  304. {
  305. for (int j=i+1; j<cpu_count; j++)
  306. {
  307. if (cpu_max_freq_khz[i] < cpu_max_freq_khz[j])
  308. {
  309. // swap
  310. int tmp = cpuids[i];
  311. cpuids[i] = cpuids[j];
  312. cpuids[j] = tmp;
  313. tmp = cpu_max_freq_khz[i];
  314. cpu_max_freq_khz[i] = cpu_max_freq_khz[j];
  315. cpu_max_freq_khz[j] = tmp;
  316. }
  317. }
  318. }
  319. // SMP
  320. int mid_max_freq_khz = (cpu_max_freq_khz.front() + cpu_max_freq_khz.back()) / 2;
  321. if (mid_max_freq_khz == cpu_max_freq_khz.back())
  322. return 0;
  323. for (int i=0; i<cpu_count; i++)
  324. {
  325. if (cpu_max_freq_khz[i] < mid_max_freq_khz)
  326. {
  327. *little_cluster_offset = i;
  328. break;
  329. }
  330. }
  331. return 0;
  332. }
  333. #endif // __ANDROID__
  334. static int g_powersave = 0;
  335. int get_cpu_powersave()
  336. {
  337. return g_powersave;
  338. }
  339. int set_cpu_powersave(int powersave)
  340. {
  341. #ifdef __ANDROID__
  342. static std::vector<int> sorted_cpuids;
  343. static int little_cluster_offset = 0;
  344. if (sorted_cpuids.empty())
  345. {
  346. // 0 ~ g_cpucount
  347. sorted_cpuids.resize(g_cpucount);
  348. for (int i=0; i<g_cpucount; i++)
  349. {
  350. sorted_cpuids[i] = i;
  351. }
  352. // descent sort by max frequency
  353. sort_cpuid_by_max_frequency(sorted_cpuids, &little_cluster_offset);
  354. }
  355. if (little_cluster_offset == 0 && powersave != 0)
  356. {
  357. powersave = 0;
  358. fprintf(stderr, "SMP cpu powersave not supported\n");
  359. }
  360. // prepare affinity cpuid
  361. std::vector<int> cpuids;
  362. if (powersave == 0)
  363. {
  364. cpuids = sorted_cpuids;
  365. }
  366. else if (powersave == 1)
  367. {
  368. cpuids = std::vector<int>(sorted_cpuids.begin() + little_cluster_offset, sorted_cpuids.end());
  369. }
  370. else if (powersave == 2)
  371. {
  372. cpuids = std::vector<int>(sorted_cpuids.begin(), sorted_cpuids.begin() + little_cluster_offset);
  373. }
  374. else
  375. {
  376. fprintf(stderr, "powersave %d not supported\n", powersave);
  377. return -1;
  378. }
  379. #ifdef _OPENMP
  380. // set affinity for each thread
  381. int num_threads = cpuids.size();
  382. omp_set_num_threads(num_threads);
  383. std::vector<int> ssarets(num_threads, 0);
  384. #pragma omp parallel for
  385. for (int i=0; i<num_threads; i++)
  386. {
  387. ssarets[i] = set_sched_affinity(cpuids);
  388. }
  389. for (int i=0; i<num_threads; i++)
  390. {
  391. if (ssarets[i] != 0)
  392. {
  393. return -1;
  394. }
  395. }
  396. #else
  397. int ssaret = set_sched_affinity(cpuids);
  398. if (ssaret != 0)
  399. {
  400. return -1;
  401. }
  402. #endif
  403. g_powersave = powersave;
  404. return 0;
  405. #elif __IOS__
  406. // thread affinity not supported on ios
  407. return -1;
  408. #else
  409. // TODO
  410. (void) powersave; // Avoid unused parameter warning.
  411. return -1;
  412. #endif
  413. }
  414. int get_omp_num_threads()
  415. {
  416. #ifdef _OPENMP
  417. return omp_get_num_threads();
  418. #else
  419. return 1;
  420. #endif
  421. }
  422. void set_omp_num_threads(int num_threads)
  423. {
  424. #ifdef _OPENMP
  425. omp_set_num_threads(num_threads);
  426. #else
  427. (void)num_threads;
  428. #endif
  429. }
  430. int get_omp_dynamic()
  431. {
  432. #ifdef _OPENMP
  433. return omp_get_dynamic();
  434. #else
  435. return 0;
  436. #endif
  437. }
  438. void set_omp_dynamic(int dynamic)
  439. {
  440. #ifdef _OPENMP
  441. omp_set_dynamic(dynamic);
  442. #else
  443. (void)dynamic;
  444. #endif
  445. }
  446. } // namespace ncnn