forked from Tencent/ncnn
-
Notifications
You must be signed in to change notification settings - Fork 18
/
Copy pathcpu.cpp
3141 lines (2721 loc) · 76.4 KB
/
cpu.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
// Tencent is pleased to support the open source community by making ncnn available.
//
// Copyright (C) 2017 THL A29 Limited, a Tencent company. All rights reserved.
//
// Licensed under the BSD 3-Clause License (the "License"); you may not use this file except
// in compliance with the License. You may obtain a copy of the License at
//
// https://opensource.org/licenses/BSD-3-Clause
//
// Unless required by applicable law or agreed to in writing, software distributed
// under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR
// CONDITIONS OF ANY KIND, either express or implied. See the License for the
// specific language governing permissions and limitations under the License.
#include "cpu.h"
#include "platform.h"
#include <limits.h>
#ifndef __wasi__
#include <setjmp.h>
#include <signal.h>
#endif // __wasi__
#include <stdio.h>
#include <string.h>
#ifdef _OPENMP
#if NCNN_SIMPLEOMP
#include "simpleomp.h"
#else
#include <omp.h>
#endif
#endif
#if defined(__i386__) || defined(__x86_64__) || defined(_M_IX86) || defined(_M_X64)
#ifdef _MSC_VER
#include <intrin.h> // __cpuid()
#include <immintrin.h> // _xgetbv()
#endif
#if defined(__clang__) || defined(__GNUC__)
#include <cpuid.h> // __get_cpuid() and __cpuid_count()
#endif
#endif
#ifdef __EMSCRIPTEN__
#include <emscripten/threading.h>
#endif
#if defined _WIN32
#define WIN32_LEAN_AND_MEAN
#include <windows.h>
#endif
#if defined __ANDROID__ || defined __linux__
#if defined __ANDROID__
#if __ANDROID_API__ >= 18
#include <sys/auxv.h> // getauxval()
#endif
#include <sys/system_properties.h> // __system_property_get()
#include <dlfcn.h>
#endif
#include <ctype.h>
#include <stdint.h>
#include <fcntl.h>
#include <sys/stat.h>
#include <sys/syscall.h>
#include <unistd.h>
#endif
#if __APPLE__
#include <mach/mach.h>
#include <mach/machine.h>
#include <mach/thread_act.h>
#include <sys/sysctl.h>
#include <sys/types.h>
#include <unistd.h>
#include "TargetConditionals.h"
#if TARGET_OS_IPHONE
#define __IOS__ 1
#endif
// define missing cpu model for old sdk
#ifndef CPUFAMILY_ARM_HURRICANE
#define CPUFAMILY_ARM_HURRICANE 0x67ceee93
#endif
// A11
#ifndef CPUFAMILY_ARM_MONSOON_MISTRAL
#define CPUFAMILY_ARM_MONSOON_MISTRAL 0xe81e7ef6
#endif
// A12
#ifndef CPUFAMILY_ARM_VORTEX_TEMPEST
#define CPUFAMILY_ARM_VORTEX_TEMPEST 0x07d34b9f
#endif
// A13
#ifndef CPUFAMILY_ARM_LIGHTNING_THUNDER
#define CPUFAMILY_ARM_LIGHTNING_THUNDER 0x462504d2
#endif
// A14 / M1
#ifndef CPUFAMILY_ARM_FIRESTORM_ICESTORM
#define CPUFAMILY_ARM_FIRESTORM_ICESTORM 0x1b588bb3
#endif
// A15 / M2
#ifndef CPUFAMILY_ARM_AVALANCHE_BLIZZARD
#define CPUFAMILY_ARM_AVALANCHE_BLIZZARD 0xda33d83d
#endif
// A16
#ifndef CPUFAMILY_ARM_EVEREST_SAWTOOTH
#define CPUFAMILY_ARM_EVEREST_SAWTOOTH 0x8765edea
#endif
// A17
#ifndef CPUFAMILY_ARM_COLL
#define CPUFAMILY_ARM_COLL 0x2876f5b5
#endif
// M3
#ifndef CPUFAMILY_ARM_IBIZA
#define CPUFAMILY_ARM_IBIZA 0xfa33415e
#endif
// M3 Pro
#ifndef CPUFAMILY_ARM_LOBOS
#define CPUFAMILY_ARM_LOBOS 0x5f4dea93
#endif
// M3 Max
#ifndef CPUFAMILY_ARM_PALMA
#define CPUFAMILY_ARM_PALMA 0x72015832
#endif
#endif // __APPLE__
#if defined(__SSE3__)
#include <immintrin.h>
#endif
#if (defined _WIN32 && (__aarch64__ || __arm__))
#define RUAPU_IMPLEMENTATION
#include "ruapu.h"
#endif
// topology info
static int g_cpucount;
static int g_physical_cpucount;
static int g_powersave;
static ncnn::CpuSet g_cpu_affinity_mask_all;
static ncnn::CpuSet g_cpu_affinity_mask_little;
static ncnn::CpuSet g_cpu_affinity_mask_big;
// isa info
#if defined _WIN32
#if __aarch64__
static int g_cpu_support_arm_asimdhp;
static int g_cpu_support_arm_cpuid;
static int g_cpu_support_arm_asimddp;
static int g_cpu_support_arm_asimdfhm;
static int g_cpu_support_arm_bf16;
static int g_cpu_support_arm_i8mm;
static int g_cpu_support_arm_sve;
static int g_cpu_support_arm_sve2;
static int g_cpu_support_arm_svebf16;
static int g_cpu_support_arm_svei8mm;
static int g_cpu_support_arm_svef32mm;
#elif __arm__
static int g_cpu_support_arm_edsp;
static int g_cpu_support_arm_neon;
static int g_cpu_support_arm_vfpv4;
#endif // __aarch64__ || __arm__
#elif defined __ANDROID__ || defined __linux__
static unsigned int g_hwcaps;
static unsigned int g_hwcaps2;
#elif __APPLE__
static unsigned int g_hw_cpufamily;
static cpu_type_t g_hw_cputype;
static cpu_subtype_t g_hw_cpusubtype;
#if __aarch64__
static int g_hw_optional_arm_FEAT_FP16;
static int g_hw_optional_arm_FEAT_DotProd;
static int g_hw_optional_arm_FEAT_FHM;
static int g_hw_optional_arm_FEAT_BF16;
static int g_hw_optional_arm_FEAT_I8MM;
#endif // __aarch64__
#endif
#if defined(__i386__) || defined(__x86_64__) || defined(_M_IX86) || defined(_M_X64)
static int g_cpu_support_x86_avx;
static int g_cpu_support_x86_fma;
static int g_cpu_support_x86_xop;
static int g_cpu_support_x86_f16c;
static int g_cpu_support_x86_avx2;
static int g_cpu_support_x86_avx_vnni;
static int g_cpu_support_x86_avx_vnni_int8;
static int g_cpu_support_x86_avx_vnni_int16;
static int g_cpu_support_x86_avx_ne_convert;
static int g_cpu_support_x86_avx512;
static int g_cpu_support_x86_avx512_vnni;
static int g_cpu_support_x86_avx512_bf16;
static int g_cpu_support_x86_avx512_fp16;
#endif // defined(__i386__) || defined(__x86_64__) || defined(_M_IX86) || defined(_M_X64)
static int g_cpu_level2_cachesize;
static int g_cpu_level3_cachesize;
// misc info
#if defined __ANDROID__ || defined __linux__
#if __aarch64__
static int g_cpu_is_arm_a53_a55;
#endif // __aarch64__
#endif // defined __ANDROID__ || defined __linux__
static bool is_being_debugged()
{
#if defined _WIN32
return IsDebuggerPresent();
#elif defined __ANDROID__ || defined __linux__
// https://stackoverflow.com/questions/3596781/how-to-detect-if-the-current-process-is-being-run-by-gdb
int status_fd = open("/proc/self/status", O_RDONLY);
if (status_fd == -1)
return false;
char buf[4096];
ssize_t num_read = read(status_fd, buf, sizeof(buf) - 1);
close(status_fd);
if (num_read <= 0)
return false;
buf[num_read] = '\0';
const char tracerPidString[] = "TracerPid:";
const char* tracer_pid_ptr = strstr(buf, tracerPidString);
if (!tracer_pid_ptr)
return false;
for (const char* ch = tracer_pid_ptr + sizeof(tracerPidString) - 1; ch <= buf + num_read; ++ch)
{
if (isspace(*ch))
continue;
return isdigit(*ch) != 0 && *ch != '0';
}
return false;
#elif defined __APPLE__
// https://stackoverflow.com/questions/2200277/detecting-debugger-on-mac-os-x
struct kinfo_proc info;
info.kp_proc.p_flag = 0;
int mib[4];
mib[0] = CTL_KERN;
mib[1] = KERN_PROC;
mib[2] = KERN_PROC_PID;
mib[3] = getpid();
size_t size = sizeof(info);
sysctl(mib, sizeof(mib) / sizeof(*mib), &info, &size, NULL, 0);
return ((info.kp_proc.p_flag & P_TRACED) != 0);
#else
// unknown platform :(
fprintf(stderr, "unknown platform!\n");
return false;
#endif
}
#if defined __ANDROID__ || defined __linux__
#define AT_HWCAP 16
#define AT_HWCAP2 26
#if __aarch64__
// from arch/arm64/include/uapi/asm/hwcap.h
#define HWCAP_ASIMD (1 << 1)
#define HWCAP_ASIMDHP (1 << 10)
#define HWCAP_CPUID (1 << 11)
#define HWCAP_ASIMDDP (1 << 20)
#define HWCAP_SVE (1 << 22)
#define HWCAP_ASIMDFHM (1 << 23)
#define HWCAP2_SVE2 (1 << 1)
#define HWCAP2_SVEI8MM (1 << 9)
#define HWCAP2_SVEF32MM (1 << 10)
#define HWCAP2_SVEBF16 (1 << 12)
#define HWCAP2_I8MM (1 << 13)
#define HWCAP2_BF16 (1 << 14)
#else
// from arch/arm/include/uapi/asm/hwcap.h
#define HWCAP_EDSP (1 << 7)
#define HWCAP_NEON (1 << 12)
#define HWCAP_VFPv4 (1 << 16)
#endif
#if __mips__
// from arch/mips/include/uapi/asm/hwcap.h
#define HWCAP_MIPS_MSA (1 << 1)
#define HWCAP_LOONGSON_MMI (1 << 11)
#endif
#if __loongarch64
// from arch/loongarch/include/uapi/asm/hwcap.h
#define HWCAP_LOONGARCH_LSX (1 << 4)
#define HWCAP_LOONGARCH_LASX (1 << 5)
#endif
#if __riscv
// from arch/riscv/include/uapi/asm/hwcap.h
#define COMPAT_HWCAP_ISA_F (1 << ('F' - 'A'))
#define COMPAT_HWCAP_ISA_V (1 << ('V' - 'A'))
#endif
#if defined __ANDROID__
// Probe the system's C library for a 'getauxval' function and call it if
// it exits, or return 0 for failure. This function is available since API
// level 18.
//
// Note that getauxval() can't really be re-implemented here, because
// its implementation does not parse /proc/self/auxv. Instead it depends
// on values that are passed by the kernel at process-init time to the
// C runtime initialization layer.
static unsigned int get_elf_hwcap_from_getauxval(unsigned int type)
{
#if __ANDROID_API__ >= 18
unsigned int hwcap = getauxval(type);
if (hwcap)
return hwcap;
#endif
typedef unsigned long getauxval_func_t(unsigned long);
dlerror();
void* libc_handle = dlopen("libc.so", RTLD_NOW);
if (!libc_handle)
{
NCNN_LOGE("dlopen libc.so failed %s", dlerror());
return 0;
}
unsigned int result = 0;
getauxval_func_t* func = (getauxval_func_t*)dlsym(libc_handle, "getauxval");
if (!func)
{
NCNN_LOGE("dlsym getauxval failed");
}
else
{
// Note: getauxval() returns 0 on failure. Doesn't touch errno.
result = (unsigned int)(*func)(type);
}
dlclose(libc_handle);
return result;
}
#endif // defined __ANDROID__
// extract the ELF HW capabilities bitmap from /proc/self/auxv
static unsigned int get_elf_hwcap_from_proc_self_auxv(unsigned int type)
{
FILE* fp = fopen("/proc/self/auxv", "rb");
if (!fp)
{
NCNN_LOGE("fopen /proc/self/auxv failed");
return 0;
}
#if __aarch64__ || __mips64 || __riscv_xlen == 64 || __loongarch64
struct
{
uint64_t tag;
uint64_t value;
} entry;
#else
struct
{
unsigned int tag;
unsigned int value;
} entry;
#endif
unsigned int result = 0;
while (!feof(fp))
{
int nread = fread((char*)&entry, sizeof(entry), 1, fp);
if (nread != 1)
break;
if (entry.tag == 0 && entry.value == 0)
break;
if (entry.tag == type)
{
result = entry.value;
break;
}
}
fclose(fp);
return result;
}
static unsigned int get_elf_hwcap(unsigned int type)
{
unsigned int hwcap = 0;
#if defined __ANDROID__
hwcap = get_elf_hwcap_from_getauxval(type);
#endif
if (!hwcap)
hwcap = get_elf_hwcap_from_proc_self_auxv(type);
#if defined __ANDROID__
#if __aarch64__
if (type == AT_HWCAP)
{
// samsung exynos9810 on android pre-9 incorrectly reports armv8.2
// for little cores, but big cores only support armv8.0
// drop all armv8.2 features used by ncnn for preventing SIGILLs
// ref https://reviews.llvm.org/D114523
char arch[PROP_VALUE_MAX];
int len = __system_property_get("ro.arch", arch);
if (len > 0 && strncmp(arch, "exynos9810", 10) == 0)
{
hwcap &= ~HWCAP_ASIMDHP;
hwcap &= ~HWCAP_ASIMDDP;
}
}
#endif // __aarch64__
#endif // defined __ANDROID__
return hwcap;
}
#endif // defined __ANDROID__ || defined __linux__
#if __APPLE__
static unsigned int get_hw_cpufamily()
{
unsigned int value = 0;
size_t len = sizeof(value);
sysctlbyname("hw.cpufamily", &value, &len, NULL, 0);
return value;
}
static cpu_type_t get_hw_cputype()
{
cpu_type_t value = 0;
size_t len = sizeof(value);
sysctlbyname("hw.cputype", &value, &len, NULL, 0);
return value;
}
static cpu_subtype_t get_hw_cpusubtype()
{
cpu_subtype_t value = 0;
size_t len = sizeof(value);
sysctlbyname("hw.cpusubtype", &value, &len, NULL, 0);
return value;
}
static int get_hw_capability(const char* cap)
{
int64_t value = 0;
size_t len = sizeof(value);
sysctlbyname(cap, &value, &len, NULL, 0);
return value;
}
#endif // __APPLE__
#if defined(__i386__) || defined(__x86_64__) || defined(_M_IX86) || defined(_M_X64)
static inline void x86_cpuid(int level, unsigned int out[4])
{
#if defined(_MSC_VER) && !defined(__clang__)
__cpuid((int*)out, level);
#elif defined(__clang__) || defined(__GNUC__)
__get_cpuid(level, out, out + 1, out + 2, out + 3);
#else
NCNN_LOGE("x86_cpuid is unknown for current compiler");
out[0] = 0;
out[1] = 0;
out[2] = 0;
out[3] = 0;
#endif
}
static inline void x86_cpuid_sublevel(int level, int sublevel, unsigned int out[4])
{
#if defined(_MSC_VER)
__cpuidex((int*)out, level, sublevel);
#elif defined(__clang__) || defined(__GNUC__)
__cpuid_count(level, sublevel, out[0], out[1], out[2], out[3]);
#else
NCNN_LOGE("x86_cpuid_sublevel is unknown for current compiler");
out[0] = 0;
out[1] = 0;
out[2] = 0;
out[3] = 0;
#endif
}
static inline int x86_get_xcr0()
{
#if defined(_MSC_FULL_VER) && (_MSC_FULL_VER >= 160040219)
return _xgetbv(0);
#elif defined(__i386__) || defined(__x86_64__)
int xcr0 = 0;
asm(".byte 0x0f, 0x01, 0xd0"
: "=a"(xcr0)
: "c"(0)
: "%edx");
return xcr0;
#else
NCNN_LOGE("x86_get_xcr0 is unknown for current compiler");
return 0xffffffff; // assume it will work
#endif
}
static int get_cpu_support_x86_avx()
{
unsigned int cpu_info[4] = {0};
x86_cpuid(0, cpu_info);
int nIds = cpu_info[0];
if (nIds < 1)
return 0;
x86_cpuid(1, cpu_info);
// check AVX XSAVE OSXSAVE
if (!(cpu_info[2] & (1u << 28)) || !(cpu_info[2] & (1u << 26)) || !(cpu_info[2] & (1u << 27)))
return 0;
// check XSAVE enabled by kernel
if ((x86_get_xcr0() & 6) != 6)
return 0;
return 1;
}
static int get_cpu_support_x86_fma()
{
unsigned int cpu_info[4] = {0};
x86_cpuid(0, cpu_info);
int nIds = cpu_info[0];
if (nIds < 7)
return 0;
x86_cpuid(1, cpu_info);
// check AVX XSAVE OSXSAVE
if (!(cpu_info[2] & (1u << 28)) || !(cpu_info[2] & (1u << 26)) || !(cpu_info[2] & (1u << 27)))
return 0;
// check XSAVE enabled by kernel
if ((x86_get_xcr0() & 6) != 6)
return 0;
return cpu_info[2] & (1u << 12);
}
static int get_cpu_support_x86_xop()
{
unsigned int cpu_info[4] = {0};
x86_cpuid(0x80000000, cpu_info);
if (cpu_info[0] < 0x80000001)
return 0;
x86_cpuid(0x80000001, cpu_info);
return cpu_info[2] & (1u << 11);
}
static int get_cpu_support_x86_f16c()
{
unsigned int cpu_info[4] = {0};
x86_cpuid(0, cpu_info);
int nIds = cpu_info[0];
if (nIds < 1)
return 0;
x86_cpuid(1, cpu_info);
return cpu_info[2] & (1u << 29);
}
static int get_cpu_support_x86_avx2()
{
unsigned int cpu_info[4] = {0};
x86_cpuid(0, cpu_info);
int nIds = cpu_info[0];
if (nIds < 7)
return 0;
x86_cpuid(1, cpu_info);
// check AVX XSAVE OSXSAVE
if (!(cpu_info[2] & (1u << 28)) || !(cpu_info[2] & (1u << 26)) || !(cpu_info[2] & (1u << 27)))
return 0;
// check XSAVE enabled by kernel
if ((x86_get_xcr0() & 6) != 6)
return 0;
x86_cpuid_sublevel(7, 0, cpu_info);
return cpu_info[1] & (1u << 5);
}
static int get_cpu_support_x86_avx_vnni()
{
unsigned int cpu_info[4] = {0};
x86_cpuid(0, cpu_info);
int nIds = cpu_info[0];
if (nIds < 7)
return 0;
x86_cpuid(1, cpu_info);
// check AVX XSAVE OSXSAVE
if (!(cpu_info[2] & (1u << 28)) || !(cpu_info[2] & (1u << 26)) || !(cpu_info[2] & (1u << 27)))
return 0;
// check XSAVE enabled by kernel
if ((x86_get_xcr0() & 6) != 6)
return 0;
x86_cpuid_sublevel(7, 1, cpu_info);
return cpu_info[0] & (1u << 4);
}
static int get_cpu_support_x86_avx_vnni_int8()
{
unsigned int cpu_info[4] = {0};
x86_cpuid(0, cpu_info);
int nIds = cpu_info[0];
if (nIds < 7)
return 0;
x86_cpuid(1, cpu_info);
// check AVX XSAVE OSXSAVE
if (!(cpu_info[2] & (1u << 28)) || !(cpu_info[2] & (1u << 26)) || !(cpu_info[2] & (1u << 27)))
return 0;
// check XSAVE enabled by kernel
if ((x86_get_xcr0() & 6) != 6)
return 0;
x86_cpuid_sublevel(7, 1, cpu_info);
return cpu_info[3] & (1u << 4);
}
static int get_cpu_support_x86_avx_vnni_int16()
{
unsigned int cpu_info[4] = {0};
x86_cpuid(0, cpu_info);
int nIds = cpu_info[0];
if (nIds < 7)
return 0;
x86_cpuid(1, cpu_info);
// check AVX XSAVE OSXSAVE
if (!(cpu_info[2] & (1u << 28)) || !(cpu_info[2] & (1u << 26)) || !(cpu_info[2] & (1u << 27)))
return 0;
// check XSAVE enabled by kernel
if ((x86_get_xcr0() & 6) != 6)
return 0;
x86_cpuid_sublevel(7, 1, cpu_info);
return cpu_info[3] & (1u << 10);
}
static int get_cpu_support_x86_avx_ne_convert()
{
unsigned int cpu_info[4] = {0};
x86_cpuid(0, cpu_info);
int nIds = cpu_info[0];
if (nIds < 7)
return 0;
x86_cpuid(1, cpu_info);
// check AVX XSAVE OSXSAVE
if (!(cpu_info[2] & (1u << 28)) || !(cpu_info[2] & (1u << 26)) || !(cpu_info[2] & (1u << 27)))
return 0;
// check XSAVE enabled by kernel
if ((x86_get_xcr0() & 6) != 6)
return 0;
x86_cpuid_sublevel(7, 1, cpu_info);
return cpu_info[3] & (1u << 5);
}
static int get_cpu_support_x86_avx512()
{
#if __APPLE__
return get_hw_capability("hw.optional.avx512f")
&& get_hw_capability("hw.optional.avx512bw")
&& get_hw_capability("hw.optional.avx512cd")
&& get_hw_capability("hw.optional.avx512dq")
&& get_hw_capability("hw.optional.avx512vl");
#else
unsigned int cpu_info[4] = {0};
x86_cpuid(0, cpu_info);
int nIds = cpu_info[0];
if (nIds < 7)
return 0;
x86_cpuid(1, cpu_info);
// check AVX XSAVE OSXSAVE
if (!(cpu_info[2] & (1u << 28)) || !(cpu_info[2] & (1u << 26)) || !(cpu_info[2] & (1u << 27)))
return 0;
// check XSAVE enabled by kernel
if ((x86_get_xcr0() & 6) != 6)
return 0;
// check avx512 XSAVE enabled by kernel
if ((x86_get_xcr0() & 0xe0) != 0xe0)
return 0;
x86_cpuid_sublevel(7, 0, cpu_info);
return (cpu_info[1] & (1u << 16)) && (cpu_info[1] & (1u << 17)) && (cpu_info[1] & (1u << 28)) && (cpu_info[1] & (1u << 30)) && (cpu_info[1] & (1u << 31));
#endif
}
static int get_cpu_support_x86_avx512_vnni()
{
#if __APPLE__
return get_hw_capability("hw.optional.avx512vnni");
#else
unsigned int cpu_info[4] = {0};
x86_cpuid(0, cpu_info);
int nIds = cpu_info[0];
if (nIds < 7)
return 0;
x86_cpuid(1, cpu_info);
// check AVX XSAVE OSXSAVE
if (!(cpu_info[2] & (1u << 28)) || !(cpu_info[2] & (1u << 26)) || !(cpu_info[2] & (1u << 27)))
return 0;
// check XSAVE enabled by kernel
if ((x86_get_xcr0() & 6) != 6)
return 0;
// check avx512 XSAVE enabled by kernel
if ((x86_get_xcr0() & 0xe0) != 0xe0)
return 0;
x86_cpuid_sublevel(7, 0, cpu_info);
return cpu_info[2] & (1u << 11);
#endif
}
static int get_cpu_support_x86_avx512_bf16()
{
#if __APPLE__
return get_hw_capability("hw.optional.avx512bf16");
#else
unsigned int cpu_info[4] = {0};
x86_cpuid(0, cpu_info);
int nIds = cpu_info[0];
if (nIds < 7)
return 0;
x86_cpuid(1, cpu_info);
// check AVX XSAVE OSXSAVE
if (!(cpu_info[2] & (1u << 28)) || !(cpu_info[2] & (1u << 26)) || !(cpu_info[2] & (1u << 27)))
return 0;
// check XSAVE enabled by kernel
if ((x86_get_xcr0() & 6) != 6)
return 0;
x86_cpuid_sublevel(7, 1, cpu_info);
return cpu_info[0] & (1u << 5);
#endif
}
static int get_cpu_support_x86_avx512_fp16()
{
#if __APPLE__
return get_hw_capability("hw.optional.avx512fp16");
#else
unsigned int cpu_info[4] = {0};
x86_cpuid(0, cpu_info);
int nIds = cpu_info[0];
if (nIds < 7)
return 0;
x86_cpuid(1, cpu_info);
// check AVX XSAVE OSXSAVE
if (!(cpu_info[2] & (1u << 28)) || !(cpu_info[2] & (1u << 26)) || !(cpu_info[2] & (1u << 27)))
return 0;
// check XSAVE enabled by kernel
if ((x86_get_xcr0() & 6) != 6)
return 0;
// check avx512 XSAVE enabled by kernel
if ((x86_get_xcr0() & 0xe0) != 0xe0)
return 0;
x86_cpuid_sublevel(7, 0, cpu_info);
return cpu_info[3] & (1u << 23);
#endif
}
#endif // defined(__i386__) || defined(__x86_64__) || defined(_M_IX86) || defined(_M_X64)
static int get_cpucount()
{
int count = 0;
#ifdef __EMSCRIPTEN__
if (emscripten_has_threading_support())
count = emscripten_num_logical_cores();
else
count = 1;
#elif defined _WIN32
SYSTEM_INFO system_info;
GetSystemInfo(&system_info);
count = system_info.dwNumberOfProcessors;
#elif defined __ANDROID__ || defined __linux__
// get cpu count from /proc/cpuinfo
FILE* fp = fopen("/proc/cpuinfo", "rb");
if (!fp)
return 1;
char line[1024];
while (!feof(fp))
{
char* s = fgets(line, 1024, fp);
if (!s)
break;
if (memcmp(line, "processor", 9) == 0)
{
count++;
}
}
fclose(fp);
#elif __APPLE__
size_t len = sizeof(count);
sysctlbyname("hw.ncpu", &count, &len, NULL, 0);
#else
#ifdef _OPENMP
count = omp_get_max_threads();
#else
count = 1;
#endif // _OPENMP
#endif
if (count < 1)
count = 1;
return count;
}
#if defined __ANDROID__ || defined __linux__
static int get_thread_siblings(int cpuid)
{
char path[256];
sprintf(path, "/sys/devices/system/cpu/cpu%d/topology/thread_siblings", cpuid);
FILE* fp = 0; //fopen(path, "rb");
if (fp)
{
int thread_siblings = -1;
int nscan = fscanf(fp, "%x", &thread_siblings);
if (nscan != 1)
{
// ignore
}
fclose(fp);
return thread_siblings;
}
// second try, parse from human-readable thread_siblings_list
sprintf(path, "/sys/devices/system/cpu/cpu%d/topology/thread_siblings_list", cpuid);
fp = fopen(path, "rb");
if (fp)
{
int thread_siblings = -1;
int id0;
char sep;
int id1;
int nscan = fscanf(fp, "%d", &id0);
if (nscan == 1)
{
thread_siblings = (1 << id0);
while (fscanf(fp, "%c%d", &sep, &id1) == 2)
{
if (sep == ',')
{
thread_siblings |= (1 << id1);
}
if (sep == '-' && id0 < id1)
{
for (int i = id0 + 1; i <= id1; i++)
{
thread_siblings |= (1 << i);
}
}
id0 = id1;
}
}
else
{
// ignore
}
fclose(fp);
return thread_siblings;
}
return -1;
}
#endif // defined __ANDROID__ || defined __linux__
static int get_physical_cpucount()
{
int count = 0;
#if defined _WIN32
typedef BOOL(WINAPI * LPFN_GLPI)(PSYSTEM_LOGICAL_PROCESSOR_INFORMATION, PDWORD);
LPFN_GLPI glpi = (LPFN_GLPI)GetProcAddress(GetModuleHandle(TEXT("kernel32")), "GetLogicalProcessorInformation");
if (glpi == NULL)
{
NCNN_LOGE("GetLogicalProcessorInformation is not supported");
return g_cpucount;
}
DWORD return_length = 0;
glpi(NULL, &return_length);
PSYSTEM_LOGICAL_PROCESSOR_INFORMATION buffer = (PSYSTEM_LOGICAL_PROCESSOR_INFORMATION)malloc(return_length);
glpi(buffer, &return_length);
PSYSTEM_LOGICAL_PROCESSOR_INFORMATION ptr = buffer;
DWORD byte_offset = 0;
while (byte_offset + sizeof(SYSTEM_LOGICAL_PROCESSOR_INFORMATION) <= return_length)
{
if (ptr->Relationship == RelationProcessorCore)
{
count++;
}
byte_offset += sizeof(SYSTEM_LOGICAL_PROCESSOR_INFORMATION);
ptr++;
}
free(buffer);
#elif defined __ANDROID__ || defined __linux__
std::vector<int> thread_set;
for (int i = 0; i < g_cpucount; i++)
{
int thread_siblings = get_thread_siblings(i);
if (thread_siblings == -1)
{
// ignore malformed one
continue;
}
bool thread_siblings_exists = false;
for (size_t j = 0; j < thread_set.size(); j++)
{
if (thread_set[j] == thread_siblings)
{
thread_siblings_exists = true;
break;
}
}
if (!thread_siblings_exists)
{
thread_set.push_back(thread_siblings);
count++;
}
}
if (count == 0)
{
// cannot resolve siblings, fallback to all cpu count
count = g_cpucount;
}
#elif __APPLE__
size_t len = sizeof(count);
sysctlbyname("hw.physicalcpu_max", &count, &len, NULL, 0);
#else
count = g_cpucount;
#endif
if (count > g_cpucount)
count = g_cpucount;