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.h 6.6 kB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221
  1. // Copyright 2017 Tencent
  2. // SPDX-License-Identifier: BSD-3-Clause
  3. #ifndef NCNN_CPU_H
  4. #define NCNN_CPU_H
  5. #include <stddef.h>
  6. #include <cstdint>
  7. #if defined _WIN32
  8. #define WIN32_LEAN_AND_MEAN
  9. #include <windows.h>
  10. #endif
  11. #if defined __ANDROID__ || defined __linux__
  12. #include <sched.h> // cpu_set_t
  13. #endif
  14. #include "platform.h"
  15. namespace ncnn {
  16. class NCNN_EXPORT CpuSet
  17. {
  18. public:
  19. CpuSet();
  20. CpuSet(const CpuSet& other);
  21. CpuSet& operator=(const CpuSet& other);
  22. ~CpuSet();
  23. void enable(int cpu);
  24. void disable(int cpu);
  25. void disable_all();
  26. bool is_enabled(int cpu) const;
  27. int num_enabled() const;
  28. // New methods for >64 CPU support
  29. int max_cpu_id() const;
  30. bool is_empty() const;
  31. void set_range(int start_cpu, int end_cpu, bool enabled);
  32. // Platform-specific accessors for backward compatibility
  33. #if defined _WIN32
  34. ULONG_PTR get_legacy_mask() const;
  35. void set_legacy_mask(ULONG_PTR mask);
  36. #endif
  37. #if defined __ANDROID__ || defined __linux__
  38. const cpu_set_t* get_cpu_set() const;
  39. cpu_set_t* get_cpu_set_mutable();
  40. void set_cpu_set(const cpu_set_t* cpuset);
  41. #endif
  42. #if __APPLE__
  43. unsigned int get_legacy_policy() const;
  44. void set_legacy_policy(unsigned int policy);
  45. #endif
  46. private:
  47. void ensure_capacity(int cpu_id);
  48. void copy_from(const CpuSet& other);
  49. // Internal implementation details
  50. static const int FAST_PATH_BITS = 64;
  51. static const int BITS_PER_WORD = 64;
  52. // Fast path for systems with <= 64 CPUs
  53. uint64_t fast_mask;
  54. // Extended path for systems with > 64 CPUs
  55. uint64_t* extended_mask;
  56. int extended_capacity; // in number of uint64_t words
  57. bool use_extended;
  58. // Platform-specific storage for compatibility
  59. #if defined _WIN32
  60. mutable ULONG_PTR legacy_mask_cache;
  61. mutable bool legacy_mask_valid;
  62. #endif
  63. #if defined __ANDROID__ || defined __linux__
  64. mutable cpu_set_t* cpu_set_cache;
  65. mutable bool cpu_set_valid;
  66. #endif
  67. #if __APPLE__
  68. mutable unsigned int legacy_policy_cache;
  69. mutable bool legacy_policy_valid;
  70. #endif
  71. };
  72. // test optional cpu features
  73. // edsp = armv7 edsp
  74. NCNN_EXPORT int cpu_support_arm_edsp();
  75. // neon = armv7 neon or aarch64 asimd
  76. NCNN_EXPORT int cpu_support_arm_neon();
  77. // vfpv4 = armv7 fp16 + fma
  78. NCNN_EXPORT int cpu_support_arm_vfpv4();
  79. // asimdhp = aarch64 asimd half precision
  80. NCNN_EXPORT int cpu_support_arm_asimdhp();
  81. // cpuid = aarch64 cpuid info
  82. NCNN_EXPORT int cpu_support_arm_cpuid();
  83. // asimddp = aarch64 asimd dot product
  84. NCNN_EXPORT int cpu_support_arm_asimddp();
  85. // asimdfhm = aarch64 asimd fhm
  86. NCNN_EXPORT int cpu_support_arm_asimdfhm();
  87. // bf16 = aarch64 bf16
  88. NCNN_EXPORT int cpu_support_arm_bf16();
  89. // i8mm = aarch64 i8mm
  90. NCNN_EXPORT int cpu_support_arm_i8mm();
  91. // sve = aarch64 sve
  92. NCNN_EXPORT int cpu_support_arm_sve();
  93. // sve2 = aarch64 sve2
  94. NCNN_EXPORT int cpu_support_arm_sve2();
  95. // svebf16 = aarch64 svebf16
  96. NCNN_EXPORT int cpu_support_arm_svebf16();
  97. // svei8mm = aarch64 svei8mm
  98. NCNN_EXPORT int cpu_support_arm_svei8mm();
  99. // svef32mm = aarch64 svef32mm
  100. NCNN_EXPORT int cpu_support_arm_svef32mm();
  101. // avx = x86 avx
  102. NCNN_EXPORT int cpu_support_x86_avx();
  103. // fma = x86 fma
  104. NCNN_EXPORT int cpu_support_x86_fma();
  105. // xop = x86 xop
  106. NCNN_EXPORT int cpu_support_x86_xop();
  107. // f16c = x86 f16c
  108. NCNN_EXPORT int cpu_support_x86_f16c();
  109. // avx2 = x86 avx2 + fma + f16c
  110. NCNN_EXPORT int cpu_support_x86_avx2();
  111. // avx_vnni = x86 avx vnni
  112. NCNN_EXPORT int cpu_support_x86_avx_vnni();
  113. // avx_vnni_int8 = x86 avx vnni int8
  114. NCNN_EXPORT int cpu_support_x86_avx_vnni_int8();
  115. // avx_vnni_int16 = x86 avx vnni int16
  116. NCNN_EXPORT int cpu_support_x86_avx_vnni_int16();
  117. // avx_ne_convert = x86 avx ne convert
  118. NCNN_EXPORT int cpu_support_x86_avx_ne_convert();
  119. // avx512 = x86 avx512f + avx512cd + avx512bw + avx512dq + avx512vl
  120. NCNN_EXPORT int cpu_support_x86_avx512();
  121. // avx512_vnni = x86 avx512 vnni
  122. NCNN_EXPORT int cpu_support_x86_avx512_vnni();
  123. // avx512_bf16 = x86 avx512 bf16
  124. NCNN_EXPORT int cpu_support_x86_avx512_bf16();
  125. // avx512_fp16 = x86 avx512 fp16
  126. NCNN_EXPORT int cpu_support_x86_avx512_fp16();
  127. // lsx = loongarch lsx
  128. NCNN_EXPORT int cpu_support_loongarch_lsx();
  129. // lasx = loongarch lasx
  130. NCNN_EXPORT int cpu_support_loongarch_lasx();
  131. // msa = mips mas
  132. NCNN_EXPORT int cpu_support_mips_msa();
  133. // mmi = loongson mmi
  134. NCNN_EXPORT int cpu_support_loongson_mmi();
  135. // v = riscv vector
  136. NCNN_EXPORT int cpu_support_riscv_v();
  137. // zfh = riscv half-precision float
  138. NCNN_EXPORT int cpu_support_riscv_zfh();
  139. // zvfh = riscv vector half-precision float
  140. NCNN_EXPORT int cpu_support_riscv_zvfh();
  141. // xtheadvector = riscv xtheadvector
  142. NCNN_EXPORT int cpu_support_riscv_xtheadvector();
  143. // vlenb = riscv vector length in bytes
  144. NCNN_EXPORT int cpu_riscv_vlenb();
  145. // cpu info
  146. NCNN_EXPORT int get_cpu_count();
  147. NCNN_EXPORT int get_little_cpu_count();
  148. NCNN_EXPORT int get_big_cpu_count();
  149. NCNN_EXPORT int get_physical_cpu_count();
  150. NCNN_EXPORT int get_physical_little_cpu_count();
  151. NCNN_EXPORT int get_physical_big_cpu_count();
  152. // cpu l2 varies from 64k to 1M, but l3 can be zero
  153. NCNN_EXPORT int get_cpu_level2_cache_size();
  154. NCNN_EXPORT int get_cpu_level3_cache_size();
  155. // bind all threads on little clusters if powersave enabled
  156. // affects HMP arch cpu like ARM big.LITTLE
  157. // only implemented on android at the moment
  158. // switching powersave is expensive and not thread-safe
  159. // 0 = all cores enabled(default)
  160. // 1 = only little clusters enabled
  161. // 2 = only big clusters enabled
  162. // return 0 if success for setter function
  163. NCNN_EXPORT int get_cpu_powersave();
  164. NCNN_EXPORT int set_cpu_powersave(int powersave);
  165. // convenient wrapper
  166. NCNN_EXPORT const CpuSet& get_cpu_thread_affinity_mask(int powersave);
  167. // set explicit thread affinity
  168. NCNN_EXPORT int set_cpu_thread_affinity(const CpuSet& thread_affinity_mask);
  169. // runtime thread affinity info
  170. NCNN_EXPORT int is_current_thread_running_on_a53_a55();
  171. // misc function wrapper for openmp routines
  172. NCNN_EXPORT int get_omp_num_threads();
  173. NCNN_EXPORT void set_omp_num_threads(int num_threads);
  174. NCNN_EXPORT int get_omp_dynamic();
  175. NCNN_EXPORT void set_omp_dynamic(int dynamic);
  176. NCNN_EXPORT int get_omp_thread_num();
  177. NCNN_EXPORT int get_kmp_blocktime();
  178. NCNN_EXPORT void set_kmp_blocktime(int time_ms);
  179. // need to flush denormals on Intel Chipset.
  180. // Other architectures such as ARM can be added as needed.
  181. // 0 = DAZ OFF, FTZ OFF
  182. // 1 = DAZ ON , FTZ OFF
  183. // 2 = DAZ OFF, FTZ ON
  184. // 3 = DAZ ON, FTZ ON
  185. NCNN_EXPORT int get_flush_denormals();
  186. NCNN_EXPORT int set_flush_denormals(int flush_denormals);
  187. } // namespace ncnn
  188. #endif // NCNN_CPU_H