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 10 kB

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