| 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 | |
| 15 | #ifndef NCNN_CPU_H |
| 16 | #define NCNN_CPU_H |
| 17 | |
| 18 | #include <stddef.h> |
| 19 | |
| 20 | #if defined _WIN32 |
| 21 | #define WIN32_LEAN_AND_MEAN |
| 22 | #include <windows.h> |
| 23 | #endif |
| 24 | #if defined __ANDROID__ || defined __linux__ |
| 25 | #include <sched.h> // cpu_set_t |
| 26 | #endif |
| 27 | |
| 28 | #include "platform.h" |
| 29 | |
| 30 | namespace ncnn { |
| 31 | |
| 32 | class NCNN_EXPORT CpuSet |
| 33 | { |
| 34 | public: |
| 35 | CpuSet(); |
| 36 | void enable(int cpu); |
| 37 | void disable(int cpu); |
| 38 | void disable_all(); |
| 39 | bool is_enabled(int cpu) const; |
| 40 | int num_enabled() const; |
| 41 | |
| 42 | public: |
| 43 | #if defined _WIN32 |
| 44 | ULONG_PTR mask; |
| 45 | #endif |
| 46 | #if defined __ANDROID__ || defined __linux__ |
| 47 | cpu_set_t cpu_set; |
| 48 | #endif |
| 49 | #if __APPLE__ |
| 50 | unsigned int policy; |
| 51 | #endif |
| 52 | }; |
| 53 | |
| 54 | // test optional cpu features |
| 55 | // edsp = armv7 edsp |
| 56 | NCNN_EXPORT int cpu_support_arm_edsp(); |
| 57 | // neon = armv7 neon or aarch64 asimd |
| 58 | NCNN_EXPORT int cpu_support_arm_neon(); |
| 59 | // vfpv4 = armv7 fp16 + fma |
| 60 | NCNN_EXPORT int cpu_support_arm_vfpv4(); |
| 61 | // asimdhp = aarch64 asimd half precision |
| 62 | NCNN_EXPORT int cpu_support_arm_asimdhp(); |
| 63 | // cpuid = aarch64 cpuid info |
| 64 | NCNN_EXPORT int cpu_support_arm_cpuid(); |
| 65 | // asimddp = aarch64 asimd dot product |
| 66 | NCNN_EXPORT int cpu_support_arm_asimddp(); |
| 67 | // asimdfhm = aarch64 asimd fhm |
| 68 | NCNN_EXPORT int cpu_support_arm_asimdfhm(); |
| 69 | // bf16 = aarch64 bf16 |
| 70 | NCNN_EXPORT int cpu_support_arm_bf16(); |
| 71 | // i8mm = aarch64 i8mm |
| 72 | NCNN_EXPORT int cpu_support_arm_i8mm(); |
| 73 | // sve = aarch64 sve |
| 74 | NCNN_EXPORT int cpu_support_arm_sve(); |
| 75 | // sve2 = aarch64 sve2 |
| 76 | NCNN_EXPORT int cpu_support_arm_sve2(); |
| 77 | // svebf16 = aarch64 svebf16 |
| 78 | NCNN_EXPORT int cpu_support_arm_svebf16(); |
| 79 | // svei8mm = aarch64 svei8mm |
| 80 | NCNN_EXPORT int cpu_support_arm_svei8mm(); |
| 81 | // svef32mm = aarch64 svef32mm |
| 82 | NCNN_EXPORT int cpu_support_arm_svef32mm(); |
| 83 | |
| 84 | // avx = x86 avx |
| 85 | NCNN_EXPORT int cpu_support_x86_avx(); |
| 86 | // fma = x86 fma |
| 87 | NCNN_EXPORT int cpu_support_x86_fma(); |
| 88 | // xop = x86 xop |
| 89 | NCNN_EXPORT int cpu_support_x86_xop(); |
| 90 | // f16c = x86 f16c |
| 91 | NCNN_EXPORT int cpu_support_x86_f16c(); |
| 92 | // avx2 = x86 avx2 + fma + f16c |
| 93 | NCNN_EXPORT int cpu_support_x86_avx2(); |
| 94 | // avx_vnni = x86 avx vnni |
| 95 | NCNN_EXPORT int cpu_support_x86_avx_vnni(); |
| 96 | // avx512 = x86 avx512f + avx512cd + avx512bw + avx512dq + avx512vl |
| 97 | NCNN_EXPORT int cpu_support_x86_avx512(); |
| 98 | // avx512_vnni = x86 avx512 vnni |
| 99 | NCNN_EXPORT int cpu_support_x86_avx512_vnni(); |
| 100 | // avx512_bf16 = x86 avx512 bf16 |
| 101 | NCNN_EXPORT int cpu_support_x86_avx512_bf16(); |
| 102 | // avx512_fp16 = x86 avx512 fp16 |
| 103 | NCNN_EXPORT int cpu_support_x86_avx512_fp16(); |
| 104 | |
| 105 | // lsx = loongarch lsx |
| 106 | NCNN_EXPORT int cpu_support_loongarch_lsx(); |
| 107 | // lasx = loongarch lasx |
| 108 | NCNN_EXPORT int cpu_support_loongarch_lasx(); |
| 109 | |
| 110 | // msa = mips mas |
| 111 | NCNN_EXPORT int cpu_support_mips_msa(); |
| 112 | // mmi = loongson mmi |
| 113 | NCNN_EXPORT int cpu_support_loongson_mmi(); |
| 114 | |
| 115 | // v = riscv vector |
| 116 | NCNN_EXPORT int cpu_support_riscv_v(); |
| 117 | // zfh = riscv half-precision float |
| 118 | NCNN_EXPORT int cpu_support_riscv_zfh(); |
| 119 | // vlenb = riscv vector length in bytes |
| 120 | NCNN_EXPORT int cpu_riscv_vlenb(); |
| 121 | |
| 122 | // cpu info |
| 123 | NCNN_EXPORT int get_cpu_count(); |
| 124 | NCNN_EXPORT int get_little_cpu_count(); |
| 125 | NCNN_EXPORT int get_big_cpu_count(); |
| 126 | |
| 127 | NCNN_EXPORT int get_physical_cpu_count(); |
| 128 | NCNN_EXPORT int get_physical_little_cpu_count(); |
| 129 | NCNN_EXPORT int get_physical_big_cpu_count(); |
| 130 | |
| 131 | // cpu l2 varies from 64k to 1M, but l3 can be zero |
| 132 | NCNN_EXPORT int get_cpu_level2_cache_size(); |
| 133 | NCNN_EXPORT int get_cpu_level3_cache_size(); |
| 134 | |
| 135 | // bind all threads on little clusters if powersave enabled |
| 136 | // affects HMP arch cpu like ARM big.LITTLE |
| 137 | // only implemented on android at the moment |
| 138 | // switching powersave is expensive and not thread-safe |
| 139 | // 0 = all cores enabled(default) |
| 140 | // 1 = only little clusters enabled |
| 141 | // 2 = only big clusters enabled |
| 142 | // return 0 if success for setter function |
| 143 | NCNN_EXPORT int get_cpu_powersave(); |
| 144 | NCNN_EXPORT int set_cpu_powersave(int powersave); |
| 145 | |
| 146 | // convenient wrapper |
| 147 | NCNN_EXPORT const CpuSet& get_cpu_thread_affinity_mask(int powersave); |
| 148 | |
| 149 | // set explicit thread affinity |
| 150 | NCNN_EXPORT int set_cpu_thread_affinity(const CpuSet& thread_affinity_mask); |
| 151 | |
| 152 | // runtime thread affinity info |
| 153 | NCNN_EXPORT int is_current_thread_running_on_a53_a55(); |
| 154 | |
| 155 | // misc function wrapper for openmp routines |
| 156 | NCNN_EXPORT int get_omp_num_threads(); |
| 157 | NCNN_EXPORT void set_omp_num_threads(int num_threads); |
| 158 | |
| 159 | NCNN_EXPORT int get_omp_dynamic(); |
| 160 | NCNN_EXPORT void set_omp_dynamic(int dynamic); |
| 161 | |
| 162 | NCNN_EXPORT int get_omp_thread_num(); |
| 163 | |
| 164 | NCNN_EXPORT int get_kmp_blocktime(); |
| 165 | NCNN_EXPORT void set_kmp_blocktime(int time_ms); |
| 166 | |
| 167 | // need to flush denormals on Intel Chipset. |
| 168 | // Other architectures such as ARM can be added as needed. |
| 169 | // 0 = DAZ OFF, FTZ OFF |
| 170 | // 1 = DAZ ON , FTZ OFF |
| 171 | // 2 = DAZ OFF, FTZ ON |
| 172 | // 3 = DAZ ON, FTZ ON |
| 173 | NCNN_EXPORT int get_flush_denormals(); |
| 174 | NCNN_EXPORT int set_flush_denormals(int flush_denormals); |
| 175 | |
| 176 | } // namespace ncnn |
| 177 | |
| 178 | #endif // NCNN_CPU_H |