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

5 years ago
12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822282328242825282628272828282928302831283228332834283528362837283828392840284128422843284428452846284728482849285028512852285328542855285628572858285928602861286228632864286528662867286828692870287128722873287428752876287728782879288028812882288328842885288628872888288928902891289228932894289528962897289828992900290129022903290429052906290729082909291029112912291329142915291629172918291929202921292229232924292529262927292829292930293129322933293429352936293729382939294029412942294329442945294629472948294929502951295229532954295529562957295829592960296129622963296429652966296729682969297029712972297329742975297629772978297929802981298229832984298529862987298829892990299129922993299429952996299729982999300030013002300330043005300630073008300930103011301230133014301530163017301830193020302130223023302430253026302730283029303030313032303330343035303630373038303930403041304230433044304530463047304830493050305130523053305430553056305730583059306030613062306330643065306630673068306930703071307230733074307530763077307830793080308130823083308430853086308730883089309030913092309330943095309630973098309931003101310231033104310531063107310831093110311131123113311431153116311731183119312031213122312331243125312631273128312931303131313231333134313531363137313831393140314131423143314431453146314731483149315031513152315331543155315631573158315931603161316231633164316531663167316831693170317131723173317431753176317731783179318031813182318331843185318631873188318931903191319231933194319531963197319831993200320132023203320432053206320732083209321032113212321332143215321632173218321932203221322232233224322532263227322832293230323132323233323432353236323732383239324032413242324332443245324632473248324932503251
  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 "platform.h"
  16. #include <limits.h>
  17. #ifndef __wasi__
  18. #include <setjmp.h>
  19. #include <signal.h>
  20. #endif // __wasi__
  21. #include <stdio.h>
  22. #include <string.h>
  23. #ifdef _OPENMP
  24. #if NCNN_SIMPLEOMP
  25. #include "simpleomp.h"
  26. #else
  27. #include <omp.h>
  28. #endif
  29. #endif
  30. #if defined(__i386__) || defined(__x86_64__) || defined(_M_IX86) || defined(_M_X64)
  31. #ifdef _MSC_VER
  32. #include <intrin.h> // __cpuid()
  33. #include <immintrin.h> // _xgetbv()
  34. #endif
  35. #if defined(__clang__) || defined(__GNUC__)
  36. #include <cpuid.h> // __get_cpuid() and __cpuid_count()
  37. #endif
  38. #endif
  39. #ifdef __EMSCRIPTEN__
  40. #include <emscripten/threading.h>
  41. #endif
  42. #if defined _WIN32
  43. #define WIN32_LEAN_AND_MEAN
  44. #include <windows.h>
  45. #endif
  46. #if defined __ANDROID__ || defined __OHOS__ || __linux__
  47. #if defined __ANDROID__
  48. #if __ANDROID_API__ >= 18
  49. #include <sys/auxv.h> // getauxval()
  50. #endif
  51. #include <sys/system_properties.h> // __system_property_get()
  52. #include <dlfcn.h>
  53. #endif
  54. #if defined __OHOS__
  55. #include <sys/auxv.h> // getauxval()
  56. #endif
  57. #include <ctype.h>
  58. #include <stdint.h>
  59. #include <fcntl.h>
  60. #include <sys/stat.h>
  61. #include <sys/syscall.h>
  62. #include <unistd.h>
  63. #endif
  64. #if __APPLE__
  65. #include <mach/mach.h>
  66. #include <mach/machine.h>
  67. #include <mach/thread_act.h>
  68. #include <sys/sysctl.h>
  69. #include <sys/types.h>
  70. #include <unistd.h>
  71. #include "TargetConditionals.h"
  72. #if TARGET_OS_IPHONE
  73. #define __IOS__ 1
  74. #endif
  75. // define missing cpu model for old sdk
  76. #ifndef CPUFAMILY_ARM_HURRICANE
  77. #define CPUFAMILY_ARM_HURRICANE 0x67ceee93
  78. #endif
  79. // A11
  80. #ifndef CPUFAMILY_ARM_MONSOON_MISTRAL
  81. #define CPUFAMILY_ARM_MONSOON_MISTRAL 0xe81e7ef6
  82. #endif
  83. // A12
  84. #ifndef CPUFAMILY_ARM_VORTEX_TEMPEST
  85. #define CPUFAMILY_ARM_VORTEX_TEMPEST 0x07d34b9f
  86. #endif
  87. // A13
  88. #ifndef CPUFAMILY_ARM_LIGHTNING_THUNDER
  89. #define CPUFAMILY_ARM_LIGHTNING_THUNDER 0x462504d2
  90. #endif
  91. // A14 / M1
  92. #ifndef CPUFAMILY_ARM_FIRESTORM_ICESTORM
  93. #define CPUFAMILY_ARM_FIRESTORM_ICESTORM 0x1b588bb3
  94. #endif
  95. // A15 / M2
  96. #ifndef CPUFAMILY_ARM_AVALANCHE_BLIZZARD
  97. #define CPUFAMILY_ARM_AVALANCHE_BLIZZARD 0xda33d83d
  98. #endif
  99. // A16
  100. #ifndef CPUFAMILY_ARM_EVEREST_SAWTOOTH
  101. #define CPUFAMILY_ARM_EVEREST_SAWTOOTH 0x8765edea
  102. #endif
  103. // A17
  104. #ifndef CPUFAMILY_ARM_COLL
  105. #define CPUFAMILY_ARM_COLL 0x2876f5b5
  106. #endif
  107. // M3
  108. #ifndef CPUFAMILY_ARM_IBIZA
  109. #define CPUFAMILY_ARM_IBIZA 0xfa33415e
  110. #endif
  111. // M3 Pro
  112. #ifndef CPUFAMILY_ARM_LOBOS
  113. #define CPUFAMILY_ARM_LOBOS 0x5f4dea93
  114. #endif
  115. // M3 Max
  116. #ifndef CPUFAMILY_ARM_PALMA
  117. #define CPUFAMILY_ARM_PALMA 0x72015832
  118. #endif
  119. #endif // __APPLE__
  120. #if defined(__SSE3__)
  121. #include <immintrin.h>
  122. #endif
  123. #if (defined _WIN32 && (__aarch64__ || __arm__))
  124. #define RUAPU_IMPLEMENTATION
  125. #include "ruapu.h"
  126. #endif
  127. // topology info
  128. static int g_cpucount;
  129. static int g_physical_cpucount;
  130. static int g_powersave;
  131. static ncnn::CpuSet g_cpu_affinity_mask_all;
  132. static ncnn::CpuSet g_cpu_affinity_mask_little;
  133. static ncnn::CpuSet g_cpu_affinity_mask_big;
  134. // isa info
  135. #if defined _WIN32
  136. #if __aarch64__
  137. static int g_cpu_support_arm_asimdhp;
  138. static int g_cpu_support_arm_cpuid;
  139. static int g_cpu_support_arm_asimddp;
  140. static int g_cpu_support_arm_asimdfhm;
  141. static int g_cpu_support_arm_bf16;
  142. static int g_cpu_support_arm_i8mm;
  143. static int g_cpu_support_arm_sve;
  144. static int g_cpu_support_arm_sve2;
  145. static int g_cpu_support_arm_svebf16;
  146. static int g_cpu_support_arm_svei8mm;
  147. static int g_cpu_support_arm_svef32mm;
  148. #elif __arm__
  149. static int g_cpu_support_arm_edsp;
  150. static int g_cpu_support_arm_neon;
  151. static int g_cpu_support_arm_vfpv4;
  152. #endif // __aarch64__ || __arm__
  153. #elif defined __ANDROID__ || defined __linux__
  154. static unsigned int g_hwcaps;
  155. static unsigned int g_hwcaps2;
  156. #elif __APPLE__
  157. static unsigned int g_hw_cpufamily;
  158. static cpu_type_t g_hw_cputype;
  159. static cpu_subtype_t g_hw_cpusubtype;
  160. #if __aarch64__
  161. static int g_hw_optional_arm_FEAT_FP16;
  162. static int g_hw_optional_arm_FEAT_DotProd;
  163. static int g_hw_optional_arm_FEAT_FHM;
  164. static int g_hw_optional_arm_FEAT_BF16;
  165. static int g_hw_optional_arm_FEAT_I8MM;
  166. #endif // __aarch64__
  167. #endif
  168. #if defined(__i386__) || defined(__x86_64__) || defined(_M_IX86) || defined(_M_X64)
  169. static int g_cpu_support_x86_avx;
  170. static int g_cpu_support_x86_fma;
  171. static int g_cpu_support_x86_xop;
  172. static int g_cpu_support_x86_f16c;
  173. static int g_cpu_support_x86_avx2;
  174. static int g_cpu_support_x86_avx_vnni;
  175. static int g_cpu_support_x86_avx_vnni_int8;
  176. static int g_cpu_support_x86_avx_vnni_int16;
  177. static int g_cpu_support_x86_avx_ne_convert;
  178. static int g_cpu_support_x86_avx512;
  179. static int g_cpu_support_x86_avx512_vnni;
  180. static int g_cpu_support_x86_avx512_bf16;
  181. static int g_cpu_support_x86_avx512_fp16;
  182. #endif // defined(__i386__) || defined(__x86_64__) || defined(_M_IX86) || defined(_M_X64)
  183. static int g_cpu_level2_cachesize;
  184. static int g_cpu_level3_cachesize;
  185. // misc info
  186. #if defined __ANDROID__ || defined __linux__
  187. #if __aarch64__
  188. static int g_cpu_is_arm_a53_a55;
  189. #endif // __aarch64__
  190. #endif // defined __ANDROID__ || defined __linux__
  191. static bool is_being_debugged()
  192. {
  193. #if defined _WIN32
  194. return IsDebuggerPresent();
  195. #elif defined __ANDROID__ || defined __linux__
  196. // https://stackoverflow.com/questions/3596781/how-to-detect-if-the-current-process-is-being-run-by-gdb
  197. int status_fd = open("/proc/self/status", O_RDONLY);
  198. if (status_fd == -1)
  199. return false;
  200. char buf[4096];
  201. ssize_t num_read = read(status_fd, buf, sizeof(buf) - 1);
  202. close(status_fd);
  203. if (num_read <= 0)
  204. return false;
  205. buf[num_read] = '\0';
  206. const char tracerPidString[] = "TracerPid:";
  207. const char* tracer_pid_ptr = strstr(buf, tracerPidString);
  208. if (!tracer_pid_ptr)
  209. return false;
  210. for (const char* ch = tracer_pid_ptr + sizeof(tracerPidString) - 1; ch <= buf + num_read; ++ch)
  211. {
  212. if (isspace(*ch))
  213. continue;
  214. return isdigit(*ch) != 0 && *ch != '0';
  215. }
  216. return false;
  217. #elif defined __APPLE__
  218. // https://stackoverflow.com/questions/2200277/detecting-debugger-on-mac-os-x
  219. struct kinfo_proc info;
  220. info.kp_proc.p_flag = 0;
  221. int mib[4];
  222. mib[0] = CTL_KERN;
  223. mib[1] = KERN_PROC;
  224. mib[2] = KERN_PROC_PID;
  225. mib[3] = getpid();
  226. size_t size = sizeof(info);
  227. sysctl(mib, sizeof(mib) / sizeof(*mib), &info, &size, NULL, 0);
  228. return ((info.kp_proc.p_flag & P_TRACED) != 0);
  229. #else
  230. // unknown platform :(
  231. fprintf(stderr, "unknown platform!\n");
  232. return false;
  233. #endif
  234. }
  235. #if defined __ANDROID__ || defined __OHOS__ || defined __linux__
  236. #define AT_HWCAP 16
  237. #define AT_HWCAP2 26
  238. #if __aarch64__
  239. // from arch/arm64/include/uapi/asm/hwcap.h
  240. #define HWCAP_ASIMD (1 << 1)
  241. #define HWCAP_ASIMDHP (1 << 10)
  242. #define HWCAP_CPUID (1 << 11)
  243. #define HWCAP_ASIMDDP (1 << 20)
  244. #define HWCAP_SVE (1 << 22)
  245. #define HWCAP_ASIMDFHM (1 << 23)
  246. #define HWCAP2_SVE2 (1 << 1)
  247. #define HWCAP2_SVEI8MM (1 << 9)
  248. #define HWCAP2_SVEF32MM (1 << 10)
  249. #define HWCAP2_SVEBF16 (1 << 12)
  250. #define HWCAP2_I8MM (1 << 13)
  251. #define HWCAP2_BF16 (1 << 14)
  252. #else
  253. // from arch/arm/include/uapi/asm/hwcap.h
  254. #define HWCAP_EDSP (1 << 7)
  255. #define HWCAP_NEON (1 << 12)
  256. #define HWCAP_VFPv4 (1 << 16)
  257. #endif
  258. #if __mips__
  259. // from arch/mips/include/uapi/asm/hwcap.h
  260. #define HWCAP_MIPS_MSA (1 << 1)
  261. #define HWCAP_LOONGSON_MMI (1 << 11)
  262. #endif
  263. #if __loongarch64
  264. // from arch/loongarch/include/uapi/asm/hwcap.h
  265. #define HWCAP_LOONGARCH_LSX (1 << 4)
  266. #define HWCAP_LOONGARCH_LASX (1 << 5)
  267. #endif
  268. #if __riscv
  269. // from arch/riscv/include/uapi/asm/hwcap.h
  270. #define COMPAT_HWCAP_ISA_F (1 << ('F' - 'A'))
  271. #define COMPAT_HWCAP_ISA_V (1 << ('V' - 'A'))
  272. #endif
  273. #if defined __ANDROID__ || defined __OHOS__
  274. // Probe the system's C library for a 'getauxval' function and call it if
  275. // it exits, or return 0 for failure. This function is available since API
  276. // level 18.
  277. //
  278. // HarmonyOS NEXT support `getauxval` directly.
  279. //
  280. // Note that getauxval() can't really be re-implemented here, because
  281. // its implementation does not parse /proc/self/auxv. Instead it depends
  282. // on values that are passed by the kernel at process-init time to the
  283. // C runtime initialization layer.
  284. static unsigned int get_elf_hwcap_from_getauxval(unsigned int type)
  285. {
  286. #if defined __OHOS__
  287. return getauxval(type);
  288. #else
  289. #if __ANDROID_API__ >= 18
  290. unsigned int hwcap = getauxval(type);
  291. if (hwcap)
  292. return hwcap;
  293. #endif
  294. typedef unsigned long getauxval_func_t(unsigned long);
  295. dlerror();
  296. void* libc_handle = dlopen("libc.so", RTLD_NOW);
  297. if (!libc_handle)
  298. {
  299. NCNN_LOGE("dlopen libc.so failed %s", dlerror());
  300. return 0;
  301. }
  302. unsigned int result = 0;
  303. getauxval_func_t* func = (getauxval_func_t*)dlsym(libc_handle, "getauxval");
  304. if (!func)
  305. {
  306. NCNN_LOGE("dlsym getauxval failed");
  307. }
  308. else
  309. {
  310. // Note: getauxval() returns 0 on failure. Doesn't touch errno.
  311. result = (unsigned int)(*func)(type);
  312. }
  313. dlclose(libc_handle);
  314. return result;
  315. #endif
  316. }
  317. #endif // defined __ANDROID__ || defined __OHOS__
  318. // extract the ELF HW capabilities bitmap from /proc/self/auxv
  319. static unsigned int get_elf_hwcap_from_proc_self_auxv(unsigned int type)
  320. {
  321. FILE* fp = fopen("/proc/self/auxv", "rb");
  322. if (!fp)
  323. {
  324. NCNN_LOGE("fopen /proc/self/auxv failed");
  325. return 0;
  326. }
  327. #if __aarch64__ || __mips64 || __riscv_xlen == 64 || __loongarch64
  328. struct
  329. {
  330. uint64_t tag;
  331. uint64_t value;
  332. } entry;
  333. #else
  334. struct
  335. {
  336. unsigned int tag;
  337. unsigned int value;
  338. } entry;
  339. #endif
  340. unsigned int result = 0;
  341. while (!feof(fp))
  342. {
  343. int nread = fread((char*)&entry, sizeof(entry), 1, fp);
  344. if (nread != 1)
  345. break;
  346. if (entry.tag == 0 && entry.value == 0)
  347. break;
  348. if (entry.tag == type)
  349. {
  350. result = entry.value;
  351. break;
  352. }
  353. }
  354. fclose(fp);
  355. return result;
  356. }
  357. static unsigned int get_elf_hwcap(unsigned int type)
  358. {
  359. unsigned int hwcap = 0;
  360. #if defined __ANDROID__ || defined __OHOS__
  361. hwcap = get_elf_hwcap_from_getauxval(type);
  362. #endif
  363. if (!hwcap)
  364. hwcap = get_elf_hwcap_from_proc_self_auxv(type);
  365. #if defined __ANDROID__
  366. #if __aarch64__
  367. if (type == AT_HWCAP)
  368. {
  369. // samsung exynos9810 on android pre-9 incorrectly reports armv8.2
  370. // for little cores, but big cores only support armv8.0
  371. // drop all armv8.2 features used by ncnn for preventing SIGILLs
  372. // ref https://reviews.llvm.org/D114523
  373. char arch[PROP_VALUE_MAX];
  374. int len = __system_property_get("ro.arch", arch);
  375. if (len > 0 && strncmp(arch, "exynos9810", 10) == 0)
  376. {
  377. hwcap &= ~HWCAP_ASIMDHP;
  378. hwcap &= ~HWCAP_ASIMDDP;
  379. }
  380. }
  381. #endif // __aarch64__
  382. #endif // defined __ANDROID__
  383. return hwcap;
  384. }
  385. #endif // defined __ANDROID__ || defined __OHOS__ || defined __linux__
  386. #if __APPLE__
  387. static unsigned int get_hw_cpufamily()
  388. {
  389. unsigned int value = 0;
  390. size_t len = sizeof(value);
  391. sysctlbyname("hw.cpufamily", &value, &len, NULL, 0);
  392. return value;
  393. }
  394. static cpu_type_t get_hw_cputype()
  395. {
  396. cpu_type_t value = 0;
  397. size_t len = sizeof(value);
  398. sysctlbyname("hw.cputype", &value, &len, NULL, 0);
  399. return value;
  400. }
  401. static cpu_subtype_t get_hw_cpusubtype()
  402. {
  403. cpu_subtype_t value = 0;
  404. size_t len = sizeof(value);
  405. sysctlbyname("hw.cpusubtype", &value, &len, NULL, 0);
  406. return value;
  407. }
  408. static int get_hw_capability(const char* cap)
  409. {
  410. int64_t value = 0;
  411. size_t len = sizeof(value);
  412. sysctlbyname(cap, &value, &len, NULL, 0);
  413. return value;
  414. }
  415. #endif // __APPLE__
  416. #if defined(__i386__) || defined(__x86_64__) || defined(_M_IX86) || defined(_M_X64)
  417. static inline void x86_cpuid(int level, unsigned int out[4])
  418. {
  419. #if defined(_MSC_VER) && !defined(__clang__)
  420. __cpuid((int*)out, level);
  421. #elif defined(__clang__) || defined(__GNUC__)
  422. __get_cpuid(level, out, out + 1, out + 2, out + 3);
  423. #else
  424. NCNN_LOGE("x86_cpuid is unknown for current compiler");
  425. out[0] = 0;
  426. out[1] = 0;
  427. out[2] = 0;
  428. out[3] = 0;
  429. #endif
  430. }
  431. static inline void x86_cpuid_sublevel(int level, int sublevel, unsigned int out[4])
  432. {
  433. #if defined(_MSC_VER)
  434. __cpuidex((int*)out, level, sublevel);
  435. #elif defined(__clang__) || defined(__GNUC__)
  436. __cpuid_count(level, sublevel, out[0], out[1], out[2], out[3]);
  437. #else
  438. NCNN_LOGE("x86_cpuid_sublevel is unknown for current compiler");
  439. out[0] = 0;
  440. out[1] = 0;
  441. out[2] = 0;
  442. out[3] = 0;
  443. #endif
  444. }
  445. static inline int x86_get_xcr0()
  446. {
  447. #if defined(_MSC_FULL_VER) && (_MSC_FULL_VER >= 160040219)
  448. return _xgetbv(0);
  449. #elif defined(__i386__) || defined(__x86_64__)
  450. int xcr0 = 0;
  451. asm(".byte 0x0f, 0x01, 0xd0"
  452. : "=a"(xcr0)
  453. : "c"(0)
  454. : "%edx");
  455. return xcr0;
  456. #else
  457. NCNN_LOGE("x86_get_xcr0 is unknown for current compiler");
  458. return 0xffffffff; // assume it will work
  459. #endif
  460. }
  461. static int get_cpu_support_x86_avx()
  462. {
  463. unsigned int cpu_info[4] = {0};
  464. x86_cpuid(0, cpu_info);
  465. int nIds = cpu_info[0];
  466. if (nIds < 1)
  467. return 0;
  468. x86_cpuid(1, cpu_info);
  469. // check AVX XSAVE OSXSAVE
  470. if (!(cpu_info[2] & (1u << 28)) || !(cpu_info[2] & (1u << 26)) || !(cpu_info[2] & (1u << 27)))
  471. return 0;
  472. // check XSAVE enabled by kernel
  473. if ((x86_get_xcr0() & 6) != 6)
  474. return 0;
  475. return 1;
  476. }
  477. static int get_cpu_support_x86_fma()
  478. {
  479. unsigned int cpu_info[4] = {0};
  480. x86_cpuid(0, cpu_info);
  481. int nIds = cpu_info[0];
  482. if (nIds < 7)
  483. return 0;
  484. x86_cpuid(1, cpu_info);
  485. // check AVX XSAVE OSXSAVE
  486. if (!(cpu_info[2] & (1u << 28)) || !(cpu_info[2] & (1u << 26)) || !(cpu_info[2] & (1u << 27)))
  487. return 0;
  488. // check XSAVE enabled by kernel
  489. if ((x86_get_xcr0() & 6) != 6)
  490. return 0;
  491. return cpu_info[2] & (1u << 12);
  492. }
  493. static int get_cpu_support_x86_xop()
  494. {
  495. unsigned int cpu_info[4] = {0};
  496. x86_cpuid(0x80000000, cpu_info);
  497. if (cpu_info[0] < 0x80000001)
  498. return 0;
  499. x86_cpuid(0x80000001, cpu_info);
  500. return cpu_info[2] & (1u << 11);
  501. }
  502. static int get_cpu_support_x86_f16c()
  503. {
  504. unsigned int cpu_info[4] = {0};
  505. x86_cpuid(0, cpu_info);
  506. int nIds = cpu_info[0];
  507. if (nIds < 1)
  508. return 0;
  509. x86_cpuid(1, cpu_info);
  510. return cpu_info[2] & (1u << 29);
  511. }
  512. static int get_cpu_support_x86_avx2()
  513. {
  514. unsigned int cpu_info[4] = {0};
  515. x86_cpuid(0, cpu_info);
  516. int nIds = cpu_info[0];
  517. if (nIds < 7)
  518. return 0;
  519. x86_cpuid(1, cpu_info);
  520. // check AVX XSAVE OSXSAVE
  521. if (!(cpu_info[2] & (1u << 28)) || !(cpu_info[2] & (1u << 26)) || !(cpu_info[2] & (1u << 27)))
  522. return 0;
  523. // check XSAVE enabled by kernel
  524. if ((x86_get_xcr0() & 6) != 6)
  525. return 0;
  526. x86_cpuid_sublevel(7, 0, cpu_info);
  527. return cpu_info[1] & (1u << 5);
  528. }
  529. static int get_cpu_support_x86_avx_vnni()
  530. {
  531. unsigned int cpu_info[4] = {0};
  532. x86_cpuid(0, cpu_info);
  533. int nIds = cpu_info[0];
  534. if (nIds < 7)
  535. return 0;
  536. x86_cpuid(1, cpu_info);
  537. // check AVX XSAVE OSXSAVE
  538. if (!(cpu_info[2] & (1u << 28)) || !(cpu_info[2] & (1u << 26)) || !(cpu_info[2] & (1u << 27)))
  539. return 0;
  540. // check XSAVE enabled by kernel
  541. if ((x86_get_xcr0() & 6) != 6)
  542. return 0;
  543. x86_cpuid_sublevel(7, 1, cpu_info);
  544. return cpu_info[0] & (1u << 4);
  545. }
  546. static int get_cpu_support_x86_avx_vnni_int8()
  547. {
  548. unsigned int cpu_info[4] = {0};
  549. x86_cpuid(0, cpu_info);
  550. int nIds = cpu_info[0];
  551. if (nIds < 7)
  552. return 0;
  553. x86_cpuid(1, cpu_info);
  554. // check AVX XSAVE OSXSAVE
  555. if (!(cpu_info[2] & (1u << 28)) || !(cpu_info[2] & (1u << 26)) || !(cpu_info[2] & (1u << 27)))
  556. return 0;
  557. // check XSAVE enabled by kernel
  558. if ((x86_get_xcr0() & 6) != 6)
  559. return 0;
  560. x86_cpuid_sublevel(7, 1, cpu_info);
  561. return cpu_info[3] & (1u << 4);
  562. }
  563. static int get_cpu_support_x86_avx_vnni_int16()
  564. {
  565. unsigned int cpu_info[4] = {0};
  566. x86_cpuid(0, cpu_info);
  567. int nIds = cpu_info[0];
  568. if (nIds < 7)
  569. return 0;
  570. x86_cpuid(1, cpu_info);
  571. // check AVX XSAVE OSXSAVE
  572. if (!(cpu_info[2] & (1u << 28)) || !(cpu_info[2] & (1u << 26)) || !(cpu_info[2] & (1u << 27)))
  573. return 0;
  574. // check XSAVE enabled by kernel
  575. if ((x86_get_xcr0() & 6) != 6)
  576. return 0;
  577. x86_cpuid_sublevel(7, 1, cpu_info);
  578. return cpu_info[3] & (1u << 10);
  579. }
  580. static int get_cpu_support_x86_avx_ne_convert()
  581. {
  582. unsigned int cpu_info[4] = {0};
  583. x86_cpuid(0, cpu_info);
  584. int nIds = cpu_info[0];
  585. if (nIds < 7)
  586. return 0;
  587. x86_cpuid(1, cpu_info);
  588. // check AVX XSAVE OSXSAVE
  589. if (!(cpu_info[2] & (1u << 28)) || !(cpu_info[2] & (1u << 26)) || !(cpu_info[2] & (1u << 27)))
  590. return 0;
  591. // check XSAVE enabled by kernel
  592. if ((x86_get_xcr0() & 6) != 6)
  593. return 0;
  594. x86_cpuid_sublevel(7, 1, cpu_info);
  595. return cpu_info[3] & (1u << 5);
  596. }
  597. static int get_cpu_support_x86_avx512()
  598. {
  599. #if __APPLE__
  600. return get_hw_capability("hw.optional.avx512f")
  601. && get_hw_capability("hw.optional.avx512bw")
  602. && get_hw_capability("hw.optional.avx512cd")
  603. && get_hw_capability("hw.optional.avx512dq")
  604. && get_hw_capability("hw.optional.avx512vl");
  605. #else
  606. unsigned int cpu_info[4] = {0};
  607. x86_cpuid(0, cpu_info);
  608. int nIds = cpu_info[0];
  609. if (nIds < 7)
  610. return 0;
  611. x86_cpuid(1, cpu_info);
  612. // check AVX XSAVE OSXSAVE
  613. if (!(cpu_info[2] & (1u << 28)) || !(cpu_info[2] & (1u << 26)) || !(cpu_info[2] & (1u << 27)))
  614. return 0;
  615. // check XSAVE enabled by kernel
  616. if ((x86_get_xcr0() & 6) != 6)
  617. return 0;
  618. // check avx512 XSAVE enabled by kernel
  619. if ((x86_get_xcr0() & 0xe0) != 0xe0)
  620. return 0;
  621. x86_cpuid_sublevel(7, 0, cpu_info);
  622. 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));
  623. #endif
  624. }
  625. static int get_cpu_support_x86_avx512_vnni()
  626. {
  627. #if __APPLE__
  628. return get_hw_capability("hw.optional.avx512vnni");
  629. #else
  630. unsigned int cpu_info[4] = {0};
  631. x86_cpuid(0, cpu_info);
  632. int nIds = cpu_info[0];
  633. if (nIds < 7)
  634. return 0;
  635. x86_cpuid(1, cpu_info);
  636. // check AVX XSAVE OSXSAVE
  637. if (!(cpu_info[2] & (1u << 28)) || !(cpu_info[2] & (1u << 26)) || !(cpu_info[2] & (1u << 27)))
  638. return 0;
  639. // check XSAVE enabled by kernel
  640. if ((x86_get_xcr0() & 6) != 6)
  641. return 0;
  642. // check avx512 XSAVE enabled by kernel
  643. if ((x86_get_xcr0() & 0xe0) != 0xe0)
  644. return 0;
  645. x86_cpuid_sublevel(7, 0, cpu_info);
  646. return cpu_info[2] & (1u << 11);
  647. #endif
  648. }
  649. static int get_cpu_support_x86_avx512_bf16()
  650. {
  651. #if __APPLE__
  652. return get_hw_capability("hw.optional.avx512bf16");
  653. #else
  654. unsigned int cpu_info[4] = {0};
  655. x86_cpuid(0, cpu_info);
  656. int nIds = cpu_info[0];
  657. if (nIds < 7)
  658. return 0;
  659. x86_cpuid(1, cpu_info);
  660. // check AVX XSAVE OSXSAVE
  661. if (!(cpu_info[2] & (1u << 28)) || !(cpu_info[2] & (1u << 26)) || !(cpu_info[2] & (1u << 27)))
  662. return 0;
  663. // check XSAVE enabled by kernel
  664. if ((x86_get_xcr0() & 6) != 6)
  665. return 0;
  666. x86_cpuid_sublevel(7, 1, cpu_info);
  667. return cpu_info[0] & (1u << 5);
  668. #endif
  669. }
  670. static int get_cpu_support_x86_avx512_fp16()
  671. {
  672. #if __APPLE__
  673. return get_hw_capability("hw.optional.avx512fp16");
  674. #else
  675. unsigned int cpu_info[4] = {0};
  676. x86_cpuid(0, cpu_info);
  677. int nIds = cpu_info[0];
  678. if (nIds < 7)
  679. return 0;
  680. x86_cpuid(1, cpu_info);
  681. // check AVX XSAVE OSXSAVE
  682. if (!(cpu_info[2] & (1u << 28)) || !(cpu_info[2] & (1u << 26)) || !(cpu_info[2] & (1u << 27)))
  683. return 0;
  684. // check XSAVE enabled by kernel
  685. if ((x86_get_xcr0() & 6) != 6)
  686. return 0;
  687. // check avx512 XSAVE enabled by kernel
  688. if ((x86_get_xcr0() & 0xe0) != 0xe0)
  689. return 0;
  690. x86_cpuid_sublevel(7, 0, cpu_info);
  691. return cpu_info[3] & (1u << 23);
  692. #endif
  693. }
  694. #endif // defined(__i386__) || defined(__x86_64__) || defined(_M_IX86) || defined(_M_X64)
  695. static int get_cpucount()
  696. {
  697. int count = 0;
  698. #ifdef __EMSCRIPTEN__
  699. if (emscripten_has_threading_support())
  700. count = emscripten_num_logical_cores();
  701. else
  702. count = 1;
  703. #elif defined _WIN32
  704. SYSTEM_INFO system_info;
  705. GetSystemInfo(&system_info);
  706. count = system_info.dwNumberOfProcessors;
  707. #elif defined __ANDROID__ || defined __linux__
  708. // get cpu count from /proc/cpuinfo
  709. FILE* fp = fopen("/proc/cpuinfo", "rb");
  710. if (!fp)
  711. return 1;
  712. char line[1024];
  713. while (!feof(fp))
  714. {
  715. char* s = fgets(line, 1024, fp);
  716. if (!s)
  717. break;
  718. if (memcmp(line, "processor", 9) == 0)
  719. {
  720. count++;
  721. }
  722. }
  723. fclose(fp);
  724. #elif __APPLE__
  725. size_t len = sizeof(count);
  726. sysctlbyname("hw.ncpu", &count, &len, NULL, 0);
  727. #else
  728. #ifdef _OPENMP
  729. count = omp_get_max_threads();
  730. #else
  731. count = 1;
  732. #endif // _OPENMP
  733. #endif
  734. if (count < 1)
  735. count = 1;
  736. return count;
  737. }
  738. #if defined __ANDROID__ || defined __linux__
  739. static int get_thread_siblings(int cpuid)
  740. {
  741. char path[256];
  742. sprintf(path, "/sys/devices/system/cpu/cpu%d/topology/thread_siblings", cpuid);
  743. FILE* fp = 0; //fopen(path, "rb");
  744. if (fp)
  745. {
  746. int thread_siblings = -1;
  747. int nscan = fscanf(fp, "%x", &thread_siblings);
  748. if (nscan != 1)
  749. {
  750. // ignore
  751. }
  752. fclose(fp);
  753. return thread_siblings;
  754. }
  755. // second try, parse from human-readable thread_siblings_list
  756. sprintf(path, "/sys/devices/system/cpu/cpu%d/topology/thread_siblings_list", cpuid);
  757. fp = fopen(path, "rb");
  758. if (fp)
  759. {
  760. int thread_siblings = -1;
  761. int id0;
  762. char sep;
  763. int id1;
  764. int nscan = fscanf(fp, "%d", &id0);
  765. if (nscan == 1)
  766. {
  767. thread_siblings = (1 << id0);
  768. while (fscanf(fp, "%c%d", &sep, &id1) == 2)
  769. {
  770. if (sep == ',')
  771. {
  772. thread_siblings |= (1 << id1);
  773. }
  774. if (sep == '-' && id0 < id1)
  775. {
  776. for (int i = id0 + 1; i <= id1; i++)
  777. {
  778. thread_siblings |= (1 << i);
  779. }
  780. }
  781. id0 = id1;
  782. }
  783. }
  784. else
  785. {
  786. // ignore
  787. }
  788. fclose(fp);
  789. return thread_siblings;
  790. }
  791. return -1;
  792. }
  793. #endif // defined __ANDROID__ || defined __linux__
  794. static int get_physical_cpucount()
  795. {
  796. int count = 0;
  797. #if defined _WIN32
  798. typedef BOOL(WINAPI * LPFN_GLPI)(PSYSTEM_LOGICAL_PROCESSOR_INFORMATION, PDWORD);
  799. LPFN_GLPI glpi = (LPFN_GLPI)GetProcAddress(GetModuleHandle(TEXT("kernel32")), "GetLogicalProcessorInformation");
  800. if (glpi == NULL)
  801. {
  802. NCNN_LOGE("GetLogicalProcessorInformation is not supported");
  803. return g_cpucount;
  804. }
  805. DWORD return_length = 0;
  806. glpi(NULL, &return_length);
  807. PSYSTEM_LOGICAL_PROCESSOR_INFORMATION buffer = (PSYSTEM_LOGICAL_PROCESSOR_INFORMATION)malloc(return_length);
  808. glpi(buffer, &return_length);
  809. PSYSTEM_LOGICAL_PROCESSOR_INFORMATION ptr = buffer;
  810. DWORD byte_offset = 0;
  811. while (byte_offset + sizeof(SYSTEM_LOGICAL_PROCESSOR_INFORMATION) <= return_length)
  812. {
  813. if (ptr->Relationship == RelationProcessorCore)
  814. {
  815. count++;
  816. }
  817. byte_offset += sizeof(SYSTEM_LOGICAL_PROCESSOR_INFORMATION);
  818. ptr++;
  819. }
  820. free(buffer);
  821. #elif defined __ANDROID__ || defined __linux__
  822. std::vector<int> thread_set;
  823. for (int i = 0; i < g_cpucount; i++)
  824. {
  825. int thread_siblings = get_thread_siblings(i);
  826. if (thread_siblings == -1)
  827. {
  828. // ignore malformed one
  829. continue;
  830. }
  831. bool thread_siblings_exists = false;
  832. for (size_t j = 0; j < thread_set.size(); j++)
  833. {
  834. if (thread_set[j] == thread_siblings)
  835. {
  836. thread_siblings_exists = true;
  837. break;
  838. }
  839. }
  840. if (!thread_siblings_exists)
  841. {
  842. thread_set.push_back(thread_siblings);
  843. count++;
  844. }
  845. }
  846. if (count == 0)
  847. {
  848. // cannot resolve siblings, fallback to all cpu count
  849. count = g_cpucount;
  850. }
  851. #elif __APPLE__
  852. size_t len = sizeof(count);
  853. sysctlbyname("hw.physicalcpu_max", &count, &len, NULL, 0);
  854. #else
  855. count = g_cpucount;
  856. #endif
  857. if (count > g_cpucount)
  858. count = g_cpucount;
  859. return count;
  860. }
  861. #if defined __ANDROID__ || defined __linux__
  862. static int get_data_cache_size(int cpuid, int level)
  863. {
  864. char path[256];
  865. // discover sysfs cache entry
  866. int indexid = -1;
  867. for (int i = 0;; i++)
  868. {
  869. // check level
  870. {
  871. sprintf(path, "/sys/devices/system/cpu/cpu%d/cache/index%d/level", cpuid, i);
  872. FILE* fp = fopen(path, "rb");
  873. if (!fp)
  874. break;
  875. int cache_level = -1;
  876. int nscan = fscanf(fp, "%d", &cache_level);
  877. fclose(fp);
  878. if (nscan != 1 || cache_level != level)
  879. continue;
  880. }
  881. // check type
  882. {
  883. sprintf(path, "/sys/devices/system/cpu/cpu%d/cache/index%d/type", cpuid, i);
  884. FILE* fp = fopen(path, "rb");
  885. if (!fp)
  886. break;
  887. char type[32];
  888. int nscan = fscanf(fp, "%31s", type);
  889. fclose(fp);
  890. if (nscan != 1 || (strcmp(type, "Data") != 0 && strcmp(type, "Unified") != 0))
  891. continue;
  892. }
  893. indexid = i;
  894. break;
  895. }
  896. if (indexid == -1)
  897. {
  898. // no sysfs entry
  899. return 0;
  900. }
  901. // get size
  902. int cache_size_K = 0;
  903. {
  904. sprintf(path, "/sys/devices/system/cpu/cpu%d/cache/index%d/size", cpuid, indexid);
  905. FILE* fp = fopen(path, "rb");
  906. if (!fp)
  907. return 0;
  908. int nscan = fscanf(fp, "%dK", &cache_size_K);
  909. fclose(fp);
  910. if (nscan != 1)
  911. {
  912. NCNN_LOGE("fscanf cache_size_K error %d", nscan);
  913. return 0;
  914. }
  915. }
  916. // parse shared_cpu_map mask
  917. ncnn::CpuSet shared_cpu_map;
  918. {
  919. sprintf(path, "/sys/devices/system/cpu/cpu%d/cache/index%d/shared_cpu_map", cpuid, indexid);
  920. FILE* fp = fopen(path, "rb");
  921. if (!fp)
  922. return 0;
  923. char shared_cpu_map_str[256];
  924. int nscan = fscanf(fp, "%255s", shared_cpu_map_str);
  925. fclose(fp);
  926. if (nscan != 1)
  927. {
  928. NCNN_LOGE("fscanf shared_cpu_map error %d", nscan);
  929. return 0;
  930. }
  931. int len = strlen(shared_cpu_map_str);
  932. if (shared_cpu_map_str[0] == '0' && shared_cpu_map_str[1] == 'x')
  933. {
  934. // skip leading 0x
  935. len -= 2;
  936. }
  937. int ci = 0;
  938. for (int i = len - 1; i >= 0; i--)
  939. {
  940. char x = shared_cpu_map_str[i];
  941. if (x & 1) shared_cpu_map.enable(ci + 0);
  942. if (x & 2) shared_cpu_map.enable(ci + 1);
  943. if (x & 4) shared_cpu_map.enable(ci + 2);
  944. if (x & 8) shared_cpu_map.enable(ci + 3);
  945. ci += 4;
  946. }
  947. }
  948. if (shared_cpu_map.num_enabled() == 1)
  949. return cache_size_K * 1024;
  950. // resolve physical cpu count in the shared_cpu_map
  951. int shared_physical_cpu_count = 0;
  952. {
  953. std::vector<int> thread_set;
  954. for (int i = 0; i < g_cpucount; i++)
  955. {
  956. if (!shared_cpu_map.is_enabled(i))
  957. continue;
  958. int thread_siblings = get_thread_siblings(i);
  959. if (thread_siblings == -1)
  960. {
  961. // ignore malformed one
  962. continue;
  963. }
  964. bool thread_siblings_exists = false;
  965. for (size_t j = 0; j < thread_set.size(); j++)
  966. {
  967. if (thread_set[j] == thread_siblings)
  968. {
  969. thread_siblings_exists = true;
  970. break;
  971. }
  972. }
  973. if (!thread_siblings_exists)
  974. {
  975. thread_set.push_back(thread_siblings);
  976. shared_physical_cpu_count++;
  977. }
  978. }
  979. }
  980. // return per-physical-core cache size with 4K aligned
  981. cache_size_K = (cache_size_K / shared_physical_cpu_count + 3) / 4 * 4;
  982. return cache_size_K * 1024;
  983. }
  984. static int get_big_cpu_data_cache_size(int level)
  985. {
  986. if (g_cpu_affinity_mask_big.num_enabled() == 0)
  987. {
  988. // smp cpu
  989. return get_data_cache_size(0, level);
  990. }
  991. for (int i = 0; i < g_cpucount; i++)
  992. {
  993. if (g_cpu_affinity_mask_big.is_enabled(i))
  994. {
  995. return get_data_cache_size(i, level);
  996. }
  997. }
  998. // should never reach here, fallback to cpu0
  999. return get_data_cache_size(0, level);
  1000. }
  1001. #endif // defined __ANDROID__ || defined __linux__
  1002. static int get_cpu_level2_cachesize()
  1003. {
  1004. int size = 0;
  1005. #if defined _WIN32
  1006. typedef BOOL(WINAPI * LPFN_GLPI)(PSYSTEM_LOGICAL_PROCESSOR_INFORMATION, PDWORD);
  1007. LPFN_GLPI glpi = (LPFN_GLPI)GetProcAddress(GetModuleHandle(TEXT("kernel32")), "GetLogicalProcessorInformation");
  1008. if (glpi != NULL)
  1009. {
  1010. DWORD return_length = 0;
  1011. glpi(NULL, &return_length);
  1012. PSYSTEM_LOGICAL_PROCESSOR_INFORMATION buffer = (PSYSTEM_LOGICAL_PROCESSOR_INFORMATION)malloc(return_length);
  1013. glpi(buffer, &return_length);
  1014. PSYSTEM_LOGICAL_PROCESSOR_INFORMATION ptr = buffer;
  1015. DWORD byte_offset = 0;
  1016. while (byte_offset + sizeof(SYSTEM_LOGICAL_PROCESSOR_INFORMATION) <= return_length)
  1017. {
  1018. if (ptr->Relationship == RelationCache)
  1019. {
  1020. PCACHE_DESCRIPTOR Cache = &ptr->Cache;
  1021. if (Cache->Level == 2)
  1022. {
  1023. size = std::max(size, (int)Cache->Size);
  1024. }
  1025. }
  1026. byte_offset += sizeof(SYSTEM_LOGICAL_PROCESSOR_INFORMATION);
  1027. ptr++;
  1028. }
  1029. free(buffer);
  1030. }
  1031. #elif defined __ANDROID__ || defined __linux__
  1032. size = get_big_cpu_data_cache_size(2);
  1033. #if defined(_SC_LEVEL2_CACHE_SIZE)
  1034. if (size <= 0)
  1035. size = sysconf(_SC_LEVEL2_CACHE_SIZE);
  1036. #endif
  1037. #elif __APPLE__
  1038. // perflevel 0 is the higher performance cluster
  1039. int cpusperl2 = get_hw_capability("hw.perflevel0.cpusperl2");
  1040. int l2cachesize = get_hw_capability("hw.perflevel0.l2cachesize");
  1041. size = cpusperl2 > 1 ? l2cachesize / cpusperl2 : l2cachesize;
  1042. #endif
  1043. // fallback to a common value
  1044. if (size <= 0)
  1045. {
  1046. #if defined(__i386__) || defined(__x86_64__) || defined(_M_IX86) || defined(_M_X64)
  1047. size = 64 * 1024;
  1048. if (g_cpu_support_x86_avx)
  1049. size = 128 * 1024;
  1050. if (g_cpu_support_x86_avx2)
  1051. size = 256 * 1024;
  1052. if (g_cpu_support_x86_avx512)
  1053. size = 1024 * 1024;
  1054. #elif __aarch64__
  1055. size = 256 * 1024;
  1056. #elif __arm__
  1057. size = 128 * 1024;
  1058. #else
  1059. // is 64k still too large here ?
  1060. size = 64 * 1024;
  1061. #endif
  1062. }
  1063. return size;
  1064. }
  1065. static int get_cpu_level3_cachesize()
  1066. {
  1067. int size = 0;
  1068. #if defined _WIN32
  1069. typedef BOOL(WINAPI * LPFN_GLPI)(PSYSTEM_LOGICAL_PROCESSOR_INFORMATION, PDWORD);
  1070. LPFN_GLPI glpi = (LPFN_GLPI)GetProcAddress(GetModuleHandle(TEXT("kernel32")), "GetLogicalProcessorInformation");
  1071. if (glpi != NULL)
  1072. {
  1073. DWORD return_length = 0;
  1074. glpi(NULL, &return_length);
  1075. PSYSTEM_LOGICAL_PROCESSOR_INFORMATION buffer = (PSYSTEM_LOGICAL_PROCESSOR_INFORMATION)malloc(return_length);
  1076. glpi(buffer, &return_length);
  1077. PSYSTEM_LOGICAL_PROCESSOR_INFORMATION ptr = buffer;
  1078. DWORD byte_offset = 0;
  1079. while (byte_offset + sizeof(SYSTEM_LOGICAL_PROCESSOR_INFORMATION) <= return_length)
  1080. {
  1081. if (ptr->Relationship == RelationCache)
  1082. {
  1083. PCACHE_DESCRIPTOR Cache = &ptr->Cache;
  1084. if (Cache->Level == 3)
  1085. {
  1086. size = std::max(size, (int)Cache->Size);
  1087. }
  1088. }
  1089. byte_offset += sizeof(SYSTEM_LOGICAL_PROCESSOR_INFORMATION);
  1090. ptr++;
  1091. }
  1092. free(buffer);
  1093. }
  1094. #elif defined __ANDROID__ || defined __linux__
  1095. size = get_big_cpu_data_cache_size(3);
  1096. #if defined(_SC_LEVEL3_CACHE_SIZE)
  1097. if (size <= 0)
  1098. size = sysconf(_SC_LEVEL3_CACHE_SIZE);
  1099. #endif
  1100. #elif __APPLE__
  1101. // perflevel 0 is the higher performance cluster
  1102. // get the size shared among all cpus
  1103. size = get_hw_capability("hw.perflevel0.l3cachesize");
  1104. #endif
  1105. // l3 cache size can be zero
  1106. return size;
  1107. }
  1108. #if defined _WIN32
  1109. static ncnn::CpuSet get_smt_cpu_mask()
  1110. {
  1111. ncnn::CpuSet smt_cpu_mask;
  1112. typedef BOOL(WINAPI * LPFN_GLPI)(PSYSTEM_LOGICAL_PROCESSOR_INFORMATION, PDWORD);
  1113. LPFN_GLPI glpi = (LPFN_GLPI)GetProcAddress(GetModuleHandle(TEXT("kernel32")), "GetLogicalProcessorInformation");
  1114. if (glpi == NULL)
  1115. {
  1116. NCNN_LOGE("GetLogicalProcessorInformation is not supported");
  1117. return smt_cpu_mask;
  1118. }
  1119. DWORD return_length = 0;
  1120. glpi(NULL, &return_length);
  1121. PSYSTEM_LOGICAL_PROCESSOR_INFORMATION buffer = (PSYSTEM_LOGICAL_PROCESSOR_INFORMATION)malloc(return_length);
  1122. glpi(buffer, &return_length);
  1123. PSYSTEM_LOGICAL_PROCESSOR_INFORMATION ptr = buffer;
  1124. DWORD byte_offset = 0;
  1125. while (byte_offset + sizeof(SYSTEM_LOGICAL_PROCESSOR_INFORMATION) <= return_length)
  1126. {
  1127. if (ptr->Relationship == RelationProcessorCore)
  1128. {
  1129. ncnn::CpuSet smt_set;
  1130. smt_set.mask = ptr->ProcessorMask;
  1131. if (smt_set.num_enabled() > 1)
  1132. {
  1133. // this core is smt
  1134. smt_cpu_mask.mask |= smt_set.mask;
  1135. }
  1136. }
  1137. byte_offset += sizeof(SYSTEM_LOGICAL_PROCESSOR_INFORMATION);
  1138. ptr++;
  1139. }
  1140. free(buffer);
  1141. return smt_cpu_mask;
  1142. }
  1143. static std::vector<int> get_max_freq_mhz()
  1144. {
  1145. typedef struct _PROCESSOR_POWER_INFORMATION
  1146. {
  1147. ULONG Number;
  1148. ULONG MaxMhz;
  1149. ULONG CurrentMhz;
  1150. ULONG MhzLimit;
  1151. ULONG MaxIdleState;
  1152. ULONG CurrentIdleState;
  1153. } PROCESSOR_POWER_INFORMATION, *PPROCESSOR_POWER_INFORMATION;
  1154. HMODULE powrprof = LoadLibrary(TEXT("powrprof.dll"));
  1155. typedef LONG(WINAPI * LPFN_CNPI)(POWER_INFORMATION_LEVEL, PVOID, ULONG, PVOID, ULONG);
  1156. LPFN_CNPI cnpi = (LPFN_CNPI)GetProcAddress(powrprof, "CallNtPowerInformation");
  1157. if (cnpi == NULL)
  1158. {
  1159. NCNN_LOGE("CallNtPowerInformation is not supported");
  1160. FreeLibrary(powrprof);
  1161. return std::vector<int>(g_cpucount, 0);
  1162. }
  1163. DWORD return_length = sizeof(PROCESSOR_POWER_INFORMATION) * g_cpucount;
  1164. PPROCESSOR_POWER_INFORMATION buffer = (PPROCESSOR_POWER_INFORMATION)malloc(return_length);
  1165. cnpi(ProcessorInformation, NULL, 0, buffer, return_length);
  1166. std::vector<int> ret;
  1167. for (int i = 0; i < g_cpucount; i++)
  1168. {
  1169. ULONG max_mhz = buffer[i].MaxMhz;
  1170. ret.push_back(max_mhz);
  1171. }
  1172. free(buffer);
  1173. FreeLibrary(powrprof);
  1174. return ret;
  1175. }
  1176. static int set_sched_affinity(const ncnn::CpuSet& thread_affinity_mask)
  1177. {
  1178. DWORD_PTR prev_mask = SetThreadAffinityMask(GetCurrentThread(), thread_affinity_mask.mask);
  1179. if (prev_mask == 0)
  1180. {
  1181. NCNN_LOGE("SetThreadAffinityMask failed %d", GetLastError());
  1182. return -1;
  1183. }
  1184. return 0;
  1185. }
  1186. #endif // defined _WIN32
  1187. #if defined __ANDROID__ || defined __linux__
  1188. static int get_max_freq_khz(int cpuid)
  1189. {
  1190. // first try, for all possible cpu
  1191. char path[256];
  1192. sprintf(path, "/sys/devices/system/cpu/cpufreq/stats/cpu%d/time_in_state", cpuid);
  1193. FILE* fp = fopen(path, "rb");
  1194. if (!fp)
  1195. {
  1196. // second try, for online cpu
  1197. sprintf(path, "/sys/devices/system/cpu/cpu%d/cpufreq/stats/time_in_state", cpuid);
  1198. fp = fopen(path, "rb");
  1199. if (fp)
  1200. {
  1201. int max_freq_khz = 0;
  1202. while (!feof(fp))
  1203. {
  1204. int freq_khz = 0;
  1205. int nscan = fscanf(fp, "%d %*d", &freq_khz);
  1206. if (nscan != 1)
  1207. break;
  1208. if (freq_khz > max_freq_khz)
  1209. max_freq_khz = freq_khz;
  1210. }
  1211. fclose(fp);
  1212. if (max_freq_khz != 0)
  1213. return max_freq_khz;
  1214. fp = NULL;
  1215. }
  1216. if (!fp)
  1217. {
  1218. // third try, for online cpu
  1219. sprintf(path, "/sys/devices/system/cpu/cpu%d/cpufreq/cpuinfo_max_freq", cpuid);
  1220. fp = fopen(path, "rb");
  1221. if (!fp)
  1222. return -1;
  1223. int max_freq_khz = -1;
  1224. int nscan = fscanf(fp, "%d", &max_freq_khz);
  1225. if (nscan != 1)
  1226. {
  1227. NCNN_LOGE("fscanf cpuinfo_max_freq error %d", nscan);
  1228. }
  1229. fclose(fp);
  1230. return max_freq_khz;
  1231. }
  1232. }
  1233. int max_freq_khz = 0;
  1234. while (!feof(fp))
  1235. {
  1236. int freq_khz = 0;
  1237. int nscan = fscanf(fp, "%d %*d", &freq_khz);
  1238. if (nscan != 1)
  1239. break;
  1240. if (freq_khz > max_freq_khz)
  1241. max_freq_khz = freq_khz;
  1242. }
  1243. fclose(fp);
  1244. return max_freq_khz;
  1245. }
  1246. static bool is_smt_cpu(int cpuid)
  1247. {
  1248. // https://github.com/torvalds/linux/blob/v6.0/Documentation/ABI/stable/sysfs-devices-system-cpu#L68-72
  1249. char path[256];
  1250. sprintf(path, "/sys/devices/system/cpu/cpu%d/topology/core_cpus_list", cpuid);
  1251. FILE* fp = fopen(path, "rb");
  1252. if (!fp)
  1253. {
  1254. sprintf(path, "/sys/devices/system/cpu/cpu%d/topology/thread_siblings_list", cpuid);
  1255. fp = fopen(path, "rb");
  1256. if (!fp)
  1257. return false;
  1258. }
  1259. bool is_smt = false;
  1260. while (!feof(fp))
  1261. {
  1262. char ch = fgetc(fp);
  1263. if (ch == ',' || ch == '-')
  1264. {
  1265. is_smt = true;
  1266. break;
  1267. }
  1268. }
  1269. fclose(fp);
  1270. return is_smt;
  1271. }
  1272. static int set_sched_affinity(const ncnn::CpuSet& thread_affinity_mask)
  1273. {
  1274. // set affinity for thread
  1275. #if defined(__BIONIC__) && !defined(__OHOS__)
  1276. pid_t pid = gettid();
  1277. #else
  1278. pid_t pid = syscall(SYS_gettid);
  1279. #endif
  1280. int syscallret = syscall(__NR_sched_setaffinity, pid, sizeof(cpu_set_t), &thread_affinity_mask.cpu_set);
  1281. if (syscallret)
  1282. {
  1283. NCNN_LOGE("syscall error %d", syscallret);
  1284. return -1;
  1285. }
  1286. return 0;
  1287. }
  1288. #endif // defined __ANDROID__ || defined __linux__
  1289. #if __APPLE__
  1290. static int set_sched_affinity(const ncnn::CpuSet& thread_affinity_mask)
  1291. {
  1292. // https://developer.apple.com/library/archive/releasenotes/Performance/RN-AffinityAPI/index.html
  1293. // http://www.hybridkernel.com/2015/01/18/binding_threads_to_cores_osx.html
  1294. // https://gist.github.com/Coneko/4234842
  1295. // This is a quite outdated document. Apple will not allow developers to set CPU affinity.
  1296. // In OS X 10.5 it worked, later it became a suggestion to OS X, then in 10.10 or so (as well in later ones), macOS will ignore any affinity settings.
  1297. // see https://github.com/Tencent/ncnn/pull/2335#discussion_r528233919 --- AmeAkio
  1298. int affinity_tag = THREAD_AFFINITY_TAG_NULL;
  1299. for (int i = 0; i < (int)sizeof(thread_affinity_mask.policy) * 8; i++)
  1300. {
  1301. if (thread_affinity_mask.is_enabled(i))
  1302. {
  1303. affinity_tag = i + 1;
  1304. break;
  1305. }
  1306. }
  1307. mach_port_t tid = pthread_mach_thread_np(pthread_self());
  1308. thread_affinity_policy_data_t policy_data;
  1309. policy_data.affinity_tag = affinity_tag;
  1310. int ret = thread_policy_set(tid, THREAD_AFFINITY_POLICY, (thread_policy_t)&policy_data, THREAD_AFFINITY_POLICY_COUNT);
  1311. if (ret && ret != KERN_NOT_SUPPORTED)
  1312. {
  1313. NCNN_LOGE("thread_policy_set error %d", ret);
  1314. return -1;
  1315. }
  1316. return 0;
  1317. }
  1318. #endif // __APPLE__
  1319. static void initialize_cpu_thread_affinity_mask(ncnn::CpuSet& mask_all, ncnn::CpuSet& mask_little, ncnn::CpuSet& mask_big)
  1320. {
  1321. mask_all.disable_all();
  1322. for (int i = 0; i < g_cpucount; i++)
  1323. {
  1324. mask_all.enable(i);
  1325. }
  1326. #if defined _WIN32
  1327. // Check SDK >= Win7
  1328. #if _WIN32_WINNT >= _WIN32_WINNT_WIN7 // win7
  1329. // Load GetLogicalProcessorInformationEx
  1330. HMODULE kernel32 = LoadLibrary(TEXT("kernel32.dll"));
  1331. if (!kernel32)
  1332. {
  1333. NCNN_LOGE("LoadLibrary kernel32.dll failed");
  1334. return;
  1335. }
  1336. typedef BOOL(WINAPI * LPFN_GLPIE)(LOGICAL_PROCESSOR_RELATIONSHIP, PSYSTEM_LOGICAL_PROCESSOR_INFORMATION_EX, PDWORD);
  1337. LPFN_GLPIE glpie = (LPFN_GLPIE)GetProcAddress(kernel32, "GetLogicalProcessorInformationEx");
  1338. if (glpie != NULL)
  1339. {
  1340. DWORD bufferSize = 0;
  1341. glpie(RelationProcessorCore, nullptr, &bufferSize);
  1342. std::vector<BYTE> buffer(bufferSize);
  1343. if (!GetLogicalProcessorInformationEx(RelationProcessorCore,
  1344. (SYSTEM_LOGICAL_PROCESSOR_INFORMATION_EX*)(buffer.data()), &bufferSize))
  1345. {
  1346. NCNN_LOGE("GetLogicalProcessorInformationEx failed");
  1347. return;
  1348. }
  1349. // A map from processor number to whether it is an E core
  1350. std::vector<std::pair<DWORD, bool> > processorCoreType;
  1351. BYTE maxEfficiencyClass = 0; // In a system without E cores, all cores EfficiencyClass is 0
  1352. BYTE* ptr = buffer.data();
  1353. while (ptr < buffer.data() + bufferSize)
  1354. {
  1355. SYSTEM_LOGICAL_PROCESSOR_INFORMATION_EX* info = (SYSTEM_LOGICAL_PROCESSOR_INFORMATION_EX*)ptr;
  1356. if (info->Relationship == RelationProcessorCore)
  1357. {
  1358. // Mingw and some old MSVC do not have EfficiencyClass in PROCESSOR_RELATIONSHIP
  1359. // So we should redefine PROCESSOR_RELATIONSHIP
  1360. // Because ncnn need to support c++98, so we can't use some new features in c++11
  1361. // So there is a ugly implementation
  1362. BYTE efficiencyClass = ((BYTE*)&info->Processor)[1];
  1363. bool isECore = (efficiencyClass == 0);
  1364. maxEfficiencyClass = (std::max)(maxEfficiencyClass, efficiencyClass);
  1365. for (WORD g = 0; g < info->Processor.GroupCount; ++g)
  1366. {
  1367. const GROUP_AFFINITY& ga = info->Processor.GroupMask[g];
  1368. KAFFINITY mask = ga.Mask;
  1369. WORD group = ga.Group;
  1370. for (int bit = 0; bit < 64; ++bit)
  1371. { // for each bit in the mask
  1372. if (mask & (static_cast<KAFFINITY>(1) << bit))
  1373. {
  1374. DWORD processorNumber = group * 64 + bit;
  1375. processorCoreType.push_back(std::pair<DWORD, bool>(processorNumber, isECore));
  1376. }
  1377. }
  1378. }
  1379. }
  1380. ptr += info->Size;
  1381. }
  1382. if (maxEfficiencyClass == 0)
  1383. {
  1384. // All cores are P cores
  1385. mask_little.disable_all();
  1386. mask_big = mask_all;
  1387. }
  1388. else
  1389. {
  1390. for (int i = 0; i < g_cpucount; i++)
  1391. {
  1392. bool isECore = false;
  1393. for (int j = 0; j < processorCoreType.size(); j++)
  1394. {
  1395. std::pair<DWORD, bool> p = processorCoreType[j];
  1396. if (p.first == i)
  1397. {
  1398. isECore = p.second;
  1399. break;
  1400. }
  1401. }
  1402. // fprintf(stderr, "processor %d is %s\n", i, isECore ? "E" : "P");
  1403. if (isECore)
  1404. {
  1405. mask_little.enable(i);
  1406. }
  1407. else
  1408. {
  1409. mask_big.enable(i);
  1410. }
  1411. }
  1412. }
  1413. }
  1414. else
  1415. #endif
  1416. {
  1417. // get max freq mhz for all cores
  1418. int max_freq_mhz_min = INT_MAX;
  1419. int max_freq_mhz_max = 0;
  1420. std::vector<int> cpu_max_freq_mhz = get_max_freq_mhz();
  1421. for (int i = 0; i < g_cpucount; i++)
  1422. {
  1423. int max_freq_mhz = cpu_max_freq_mhz[i];
  1424. // NCNN_LOGE("%d max freq = %d khz", i, max_freq_mhz);
  1425. if (max_freq_mhz > max_freq_mhz_max)
  1426. max_freq_mhz_max = max_freq_mhz;
  1427. if (max_freq_mhz < max_freq_mhz_min)
  1428. max_freq_mhz_min = max_freq_mhz;
  1429. }
  1430. int max_freq_mhz_medium = (max_freq_mhz_min + max_freq_mhz_max) / 2;
  1431. if (max_freq_mhz_medium == max_freq_mhz_max)
  1432. {
  1433. mask_little.disable_all();
  1434. mask_big = mask_all;
  1435. return;
  1436. }
  1437. ncnn::CpuSet smt_cpu_mask = get_smt_cpu_mask();
  1438. for (int i = 0; i < g_cpucount; i++)
  1439. {
  1440. if (smt_cpu_mask.is_enabled(i))
  1441. {
  1442. // always treat smt core as big core
  1443. mask_big.enable(i);
  1444. continue;
  1445. }
  1446. if (cpu_max_freq_mhz[i] < max_freq_mhz_medium)
  1447. mask_little.enable(i);
  1448. else
  1449. mask_big.enable(i);
  1450. }
  1451. }
  1452. #elif defined __ANDROID__ || defined __linux__
  1453. int max_freq_khz_min = INT_MAX;
  1454. int max_freq_khz_max = 0;
  1455. std::vector<int> cpu_max_freq_khz(g_cpucount);
  1456. for (int i = 0; i < g_cpucount; i++)
  1457. {
  1458. int max_freq_khz = get_max_freq_khz(i);
  1459. // NCNN_LOGE("%d max freq = %d khz", i, max_freq_khz);
  1460. cpu_max_freq_khz[i] = max_freq_khz;
  1461. if (max_freq_khz > max_freq_khz_max)
  1462. max_freq_khz_max = max_freq_khz;
  1463. if (max_freq_khz < max_freq_khz_min)
  1464. max_freq_khz_min = max_freq_khz;
  1465. }
  1466. int max_freq_khz_medium = (max_freq_khz_min + max_freq_khz_max) / 2;
  1467. if (max_freq_khz_medium == max_freq_khz_max)
  1468. {
  1469. mask_little.disable_all();
  1470. mask_big = mask_all;
  1471. return;
  1472. }
  1473. for (int i = 0; i < g_cpucount; i++)
  1474. {
  1475. if (is_smt_cpu(i))
  1476. {
  1477. // always treat smt core as big core
  1478. mask_big.enable(i);
  1479. continue;
  1480. }
  1481. if (cpu_max_freq_khz[i] < max_freq_khz_medium)
  1482. mask_little.enable(i);
  1483. else
  1484. mask_big.enable(i);
  1485. }
  1486. #elif __APPLE__
  1487. int nperflevels = get_hw_capability("hw.nperflevels");
  1488. if (nperflevels == 1)
  1489. {
  1490. // smp models
  1491. mask_little.disable_all();
  1492. mask_big = mask_all;
  1493. }
  1494. else
  1495. {
  1496. // two or more clusters, level0 is the high-performance cluster
  1497. int perflevel0_logicalcpu = get_hw_capability("hw.perflevel0.logicalcpu_max");
  1498. for (int i = 0; i < perflevel0_logicalcpu; i++)
  1499. {
  1500. mask_big.enable(i);
  1501. }
  1502. for (int i = perflevel0_logicalcpu; i < g_cpucount; i++)
  1503. {
  1504. mask_little.enable(i);
  1505. }
  1506. }
  1507. #else
  1508. // TODO implement me for other platforms
  1509. mask_little.disable_all();
  1510. mask_big = mask_all;
  1511. #endif
  1512. }
  1513. #if defined __ANDROID__ || defined __linux__
  1514. #if __aarch64__
  1515. union midr_info_t
  1516. {
  1517. struct __attribute__((packed))
  1518. {
  1519. unsigned int revision : 4;
  1520. unsigned int part : 12;
  1521. unsigned int architecture : 4;
  1522. unsigned int variant : 4;
  1523. unsigned int implementer : 8;
  1524. };
  1525. unsigned int midr;
  1526. midr_info_t(unsigned int _midr)
  1527. : midr(_midr)
  1528. {
  1529. }
  1530. };
  1531. static unsigned int get_midr_from_sysfs(int cpuid)
  1532. {
  1533. char path[256];
  1534. sprintf(path, "/sys/devices/system/cpu/cpu%d/regs/identification/midr_el1", cpuid);
  1535. FILE* fp = fopen(path, "rb");
  1536. if (!fp)
  1537. return 0;
  1538. unsigned int midr_el1 = 0;
  1539. int nscan = fscanf(fp, "%x", &midr_el1);
  1540. if (nscan != 1)
  1541. {
  1542. // ignore
  1543. }
  1544. fclose(fp);
  1545. return midr_el1;
  1546. }
  1547. static int get_midr_from_proc_cpuinfo(std::vector<unsigned int>& midrs)
  1548. {
  1549. FILE* fp = fopen("/proc/cpuinfo", "rb");
  1550. if (!fp)
  1551. return -1;
  1552. midrs.resize(g_cpucount, 0);
  1553. int cpuid = -1;
  1554. midr_info_t midr_info(0);
  1555. char line[1024];
  1556. while (!feof(fp))
  1557. {
  1558. char* s = fgets(line, 1024, fp);
  1559. if (!s)
  1560. break;
  1561. if (memcmp(line, "processor", 9) == 0)
  1562. {
  1563. // processor : 4
  1564. int id = -1;
  1565. int nscan = sscanf(line, "%*[^:]: %d", &id);
  1566. if (nscan != 1)
  1567. continue;
  1568. if (cpuid >= 0 && cpuid < g_cpucount)
  1569. {
  1570. if (midr_info.midr == 0)
  1571. {
  1572. // shared midr
  1573. midrs[cpuid] = (unsigned int)-1;
  1574. }
  1575. else
  1576. {
  1577. // save midr and reset
  1578. midrs[cpuid] = midr_info.midr;
  1579. for (int i = 0; i < g_cpucount; i++)
  1580. {
  1581. if (midrs[i] == (unsigned int)-1)
  1582. midrs[i] = midr_info.midr;
  1583. }
  1584. }
  1585. midr_info.midr = 0;
  1586. }
  1587. cpuid = id;
  1588. }
  1589. if (cpuid == -1)
  1590. continue;
  1591. if (memcmp(line, "CPU implementer", 15) == 0)
  1592. {
  1593. // CPU implementer : 0x51
  1594. unsigned int id = 0;
  1595. int nscan = sscanf(line, "%*[^:]: %x", &id);
  1596. if (nscan != 1)
  1597. continue;
  1598. midr_info.implementer = id;
  1599. }
  1600. else if (memcmp(line, "CPU architecture", 16) == 0)
  1601. {
  1602. // CPU architecture: 8
  1603. int id = 0;
  1604. int nscan = sscanf(line, "%*[^:]: %d", &id);
  1605. if (nscan != 1)
  1606. continue;
  1607. midr_info.architecture = id;
  1608. }
  1609. else if (memcmp(line, "CPU variant", 11) == 0)
  1610. {
  1611. // CPU variant : 0xd
  1612. int id = 0;
  1613. int nscan = sscanf(line, "%*[^:]: %x", &id);
  1614. if (nscan != 1)
  1615. continue;
  1616. midr_info.variant = id;
  1617. }
  1618. else if (memcmp(line, "CPU part", 8) == 0)
  1619. {
  1620. // CPU part : 0x804
  1621. int id = 0;
  1622. int nscan = sscanf(line, "%*[^:]: %x", &id);
  1623. if (nscan != 1)
  1624. continue;
  1625. midr_info.part = id;
  1626. }
  1627. else if (memcmp(line, "CPU revision", 12) == 0)
  1628. {
  1629. // CPU revision : 14
  1630. int id = 0;
  1631. int nscan = sscanf(line, "%*[^:]: %d", &id);
  1632. if (nscan != 1)
  1633. continue;
  1634. midr_info.revision = id;
  1635. }
  1636. }
  1637. fclose(fp);
  1638. if (cpuid >= 0 && cpuid < g_cpucount)
  1639. {
  1640. if (midr_info.midr == 0)
  1641. {
  1642. // shared midr
  1643. midrs[cpuid] = (unsigned int)-1;
  1644. }
  1645. else
  1646. {
  1647. // save midr and reset
  1648. midrs[cpuid] = midr_info.midr;
  1649. for (int i = 0; i < g_cpucount; i++)
  1650. {
  1651. if (midrs[i] == (unsigned int)-1)
  1652. midrs[i] = midr_info.midr;
  1653. }
  1654. }
  1655. midr_info.midr = 0;
  1656. }
  1657. // /proc/cpuinfo may only report little/online cores on old kernel
  1658. if (g_cpu_affinity_mask_big.num_enabled() == g_cpucount)
  1659. {
  1660. // assign the remaining unknown midrs for smp cpu
  1661. for (int i = 0; i < g_cpucount; i++)
  1662. {
  1663. if (midrs[i] == 0)
  1664. midrs[i] = midr_info.midr;
  1665. }
  1666. }
  1667. else
  1668. {
  1669. // clear the big core midrs for hmp cpu if they are the same as little cores
  1670. unsigned int little_midr = 0;
  1671. for (int i = 0; i < g_cpucount; i++)
  1672. {
  1673. if (g_cpu_affinity_mask_little.is_enabled(i))
  1674. {
  1675. little_midr = midrs[i];
  1676. break;
  1677. }
  1678. }
  1679. for (int i = 0; i < g_cpucount; i++)
  1680. {
  1681. if (g_cpu_affinity_mask_big.is_enabled(i))
  1682. {
  1683. if (midrs[i] == little_midr)
  1684. {
  1685. midrs[i] = 0;
  1686. }
  1687. }
  1688. }
  1689. }
  1690. return 0;
  1691. }
  1692. // return midr for the current running core
  1693. static unsigned int get_midr_from_register()
  1694. {
  1695. uint64_t midr;
  1696. asm volatile("mrs %0, MIDR_EL1"
  1697. : "=r"(midr));
  1698. return (unsigned int)midr;
  1699. }
  1700. static int get_sched_affinity(ncnn::CpuSet& thread_affinity_mask)
  1701. {
  1702. // get affinity for thread
  1703. #if defined(__BIONIC__) && !defined(__OHOS__)
  1704. pid_t pid = gettid();
  1705. #else
  1706. pid_t pid = syscall(SYS_gettid);
  1707. #endif
  1708. thread_affinity_mask.disable_all();
  1709. int syscallret = syscall(__NR_sched_getaffinity, pid, sizeof(cpu_set_t), &thread_affinity_mask.cpu_set);
  1710. if (syscallret)
  1711. {
  1712. // handle get error silently
  1713. return -1;
  1714. }
  1715. return 0;
  1716. }
  1717. static int midr_is_a53_a55(unsigned int midr)
  1718. {
  1719. // 0x 41 ? f d03 ? = arm cortex-a53
  1720. // 0x 51 ? f 801 ? = qcom kryo200 a53
  1721. // 0x 41 ? f d04 ? = arm cortex-a35
  1722. // 0x 41 ? f d05 ? = arm cortex-a55
  1723. // 0x 51 ? f 803 ? = qcom kryo300 a55
  1724. // 0x 51 ? f 805 ? = qcom kryo400 a55
  1725. midr_info_t midr_info(midr);
  1726. return (midr_info.implementer == 0x41 && midr_info.part == 0xd03)
  1727. || (midr_info.implementer == 0x51 && midr_info.part == 0x801)
  1728. || (midr_info.implementer == 0x41 && midr_info.part == 0xd04)
  1729. || (midr_info.implementer == 0x41 && midr_info.part == 0xd05)
  1730. || (midr_info.implementer == 0x51 && midr_info.part == 0x803)
  1731. || (midr_info.implementer == 0x51 && midr_info.part == 0x805);
  1732. }
  1733. static int detect_cpu_is_arm_a53_a55()
  1734. {
  1735. int a53_a55_cpu_count = 0;
  1736. // first try, iterate /sys/devices/system/cpu/cpuX/regs/identification/midr_el1
  1737. bool sysfs_midr = true;
  1738. for (int i = 0; i < g_cpucount; i++)
  1739. {
  1740. unsigned int midr = 0;
  1741. // for kernel 4.7+
  1742. midr = get_midr_from_sysfs(i);
  1743. if (midr == 0)
  1744. {
  1745. sysfs_midr = false;
  1746. break;
  1747. }
  1748. if (midr_is_a53_a55(midr))
  1749. {
  1750. a53_a55_cpu_count++;
  1751. }
  1752. }
  1753. if (!sysfs_midr)
  1754. {
  1755. // second try, collect midr from /proc/cpuinfo
  1756. std::vector<unsigned int> midrs;
  1757. int ret = get_midr_from_proc_cpuinfo(midrs);
  1758. if (ret == 0 && (int)midrs.size() == g_cpucount)
  1759. {
  1760. for (int i = 0; i < g_cpucount; i++)
  1761. {
  1762. if (midr_is_a53_a55(midrs[i]))
  1763. {
  1764. a53_a55_cpu_count++;
  1765. }
  1766. }
  1767. }
  1768. else
  1769. {
  1770. // third try, assume all aarch64 little cores are a53/a55
  1771. a53_a55_cpu_count = g_cpu_affinity_mask_little.num_enabled();
  1772. }
  1773. }
  1774. if (a53_a55_cpu_count == 0)
  1775. return 0; // all non a53/a55
  1776. if (a53_a55_cpu_count == g_cpucount)
  1777. return 1; // all a53/a55
  1778. // little cores are a53/a55
  1779. return 2;
  1780. }
  1781. #endif // __aarch64__
  1782. #endif // defined __ANDROID__ || defined __linux__
  1783. // the initialization
  1784. static void initialize_global_cpu_info()
  1785. {
  1786. g_cpucount = get_cpucount();
  1787. g_physical_cpucount = get_physical_cpucount();
  1788. g_powersave = 0;
  1789. initialize_cpu_thread_affinity_mask(g_cpu_affinity_mask_all, g_cpu_affinity_mask_little, g_cpu_affinity_mask_big);
  1790. #if (defined _WIN32 && (__aarch64__ || __arm__))
  1791. if (!is_being_debugged())
  1792. {
  1793. ruapu_init();
  1794. }
  1795. #endif
  1796. #if defined _WIN32
  1797. #if __aarch64__
  1798. g_cpu_support_arm_cpuid = ruapu_supports("cpuid");
  1799. g_cpu_support_arm_asimdhp = ruapu_supports("asimdhp") || IsProcessorFeaturePresent(43); // dp implies hp
  1800. g_cpu_support_arm_asimddp = ruapu_supports("asimddp") || IsProcessorFeaturePresent(43); // 43 is PF_ARM_V82_DP_INSTRUCTIONS_AVAILABLE
  1801. g_cpu_support_arm_asimdfhm = ruapu_supports("asimdfhm");
  1802. g_cpu_support_arm_bf16 = ruapu_supports("bf16");
  1803. g_cpu_support_arm_i8mm = ruapu_supports("i8mm");
  1804. g_cpu_support_arm_sve = ruapu_supports("sve");
  1805. g_cpu_support_arm_sve2 = ruapu_supports("sve2");
  1806. g_cpu_support_arm_svebf16 = ruapu_supports("svebf16");
  1807. g_cpu_support_arm_svei8mm = ruapu_supports("svei8mm");
  1808. g_cpu_support_arm_svef32mm = ruapu_supports("svef32mm");
  1809. #elif __arm__
  1810. g_cpu_support_arm_edsp = ruapu_supports("edsp");
  1811. g_cpu_support_arm_neon = 1; // all modern windows arm devices have neon
  1812. g_cpu_support_arm_vfpv4 = ruapu_supports("vfpv4");
  1813. #endif // __aarch64__ || __arm__
  1814. #elif defined __ANDROID__ || defined __linux__
  1815. g_hwcaps = get_elf_hwcap(AT_HWCAP);
  1816. g_hwcaps2 = get_elf_hwcap(AT_HWCAP2);
  1817. #elif __APPLE__
  1818. g_hw_cpufamily = get_hw_cpufamily();
  1819. g_hw_cputype = get_hw_cputype();
  1820. g_hw_cpusubtype = get_hw_cpusubtype();
  1821. #if __aarch64__
  1822. g_hw_optional_arm_FEAT_FP16 = get_hw_capability("hw.optional.arm.FEAT_FP16");
  1823. g_hw_optional_arm_FEAT_DotProd = get_hw_capability("hw.optional.arm.FEAT_DotProd");
  1824. g_hw_optional_arm_FEAT_FHM = get_hw_capability("hw.optional.arm.FEAT_FHM");
  1825. g_hw_optional_arm_FEAT_BF16 = get_hw_capability("hw.optional.arm.FEAT_BF16");
  1826. g_hw_optional_arm_FEAT_I8MM = get_hw_capability("hw.optional.arm.FEAT_I8MM");
  1827. #endif // __aarch64__
  1828. #endif
  1829. #if defined(__i386__) || defined(__x86_64__) || defined(_M_IX86) || defined(_M_X64)
  1830. g_cpu_support_x86_avx = get_cpu_support_x86_avx();
  1831. g_cpu_support_x86_fma = get_cpu_support_x86_fma();
  1832. g_cpu_support_x86_xop = get_cpu_support_x86_xop();
  1833. g_cpu_support_x86_f16c = get_cpu_support_x86_f16c();
  1834. g_cpu_support_x86_avx2 = get_cpu_support_x86_avx2();
  1835. g_cpu_support_x86_avx_vnni = get_cpu_support_x86_avx_vnni();
  1836. g_cpu_support_x86_avx_vnni_int8 = get_cpu_support_x86_avx_vnni_int8();
  1837. g_cpu_support_x86_avx_vnni_int16 = get_cpu_support_x86_avx_vnni_int16();
  1838. g_cpu_support_x86_avx_ne_convert = get_cpu_support_x86_avx_ne_convert();
  1839. g_cpu_support_x86_avx512 = get_cpu_support_x86_avx512();
  1840. g_cpu_support_x86_avx512_vnni = get_cpu_support_x86_avx512_vnni();
  1841. g_cpu_support_x86_avx512_bf16 = get_cpu_support_x86_avx512_bf16();
  1842. g_cpu_support_x86_avx512_fp16 = get_cpu_support_x86_avx512_fp16();
  1843. #endif // defined(__i386__) || defined(__x86_64__) || defined(_M_IX86) || defined(_M_X64)
  1844. g_cpu_level2_cachesize = get_cpu_level2_cachesize();
  1845. g_cpu_level3_cachesize = get_cpu_level3_cachesize();
  1846. #if defined __ANDROID__ || defined __linux__
  1847. #if __aarch64__
  1848. g_cpu_is_arm_a53_a55 = detect_cpu_is_arm_a53_a55();
  1849. #endif // __aarch64__
  1850. #endif // defined __ANDROID__ || defined __linux__
  1851. }
  1852. static int g_cpu_info_initialized = 0;
  1853. static inline void try_initialize_global_cpu_info()
  1854. {
  1855. if (!g_cpu_info_initialized)
  1856. {
  1857. initialize_global_cpu_info();
  1858. g_cpu_info_initialized = 1;
  1859. }
  1860. }
  1861. namespace ncnn {
  1862. #if defined _WIN32
  1863. CpuSet::CpuSet()
  1864. {
  1865. disable_all();
  1866. }
  1867. void CpuSet::enable(int cpu)
  1868. {
  1869. mask |= ((ULONG_PTR)1 << cpu);
  1870. }
  1871. void CpuSet::disable(int cpu)
  1872. {
  1873. mask &= ~((ULONG_PTR)1 << cpu);
  1874. }
  1875. void CpuSet::disable_all()
  1876. {
  1877. mask = 0;
  1878. }
  1879. bool CpuSet::is_enabled(int cpu) const
  1880. {
  1881. return mask & ((ULONG_PTR)1 << cpu);
  1882. }
  1883. int CpuSet::num_enabled() const
  1884. {
  1885. int num_enabled = 0;
  1886. for (int i = 0; i < (int)sizeof(mask) * 8; i++)
  1887. {
  1888. if (is_enabled(i))
  1889. num_enabled++;
  1890. }
  1891. return num_enabled;
  1892. }
  1893. #elif defined __ANDROID__ || defined __linux__
  1894. CpuSet::CpuSet()
  1895. {
  1896. disable_all();
  1897. }
  1898. void CpuSet::enable(int cpu)
  1899. {
  1900. CPU_SET(cpu, &cpu_set);
  1901. }
  1902. void CpuSet::disable(int cpu)
  1903. {
  1904. CPU_CLR(cpu, &cpu_set);
  1905. }
  1906. void CpuSet::disable_all()
  1907. {
  1908. CPU_ZERO(&cpu_set);
  1909. }
  1910. bool CpuSet::is_enabled(int cpu) const
  1911. {
  1912. return CPU_ISSET(cpu, &cpu_set);
  1913. }
  1914. int CpuSet::num_enabled() const
  1915. {
  1916. int num_enabled = 0;
  1917. for (int i = 0; i < (int)sizeof(cpu_set_t) * 8; i++)
  1918. {
  1919. if (is_enabled(i))
  1920. num_enabled++;
  1921. }
  1922. return num_enabled;
  1923. }
  1924. #elif __APPLE__
  1925. CpuSet::CpuSet()
  1926. {
  1927. disable_all();
  1928. }
  1929. void CpuSet::enable(int cpu)
  1930. {
  1931. policy |= ((unsigned int)1 << cpu);
  1932. }
  1933. void CpuSet::disable(int cpu)
  1934. {
  1935. policy &= ~((unsigned int)1 << cpu);
  1936. }
  1937. void CpuSet::disable_all()
  1938. {
  1939. policy = 0;
  1940. }
  1941. bool CpuSet::is_enabled(int cpu) const
  1942. {
  1943. return policy & ((unsigned int)1 << cpu);
  1944. }
  1945. int CpuSet::num_enabled() const
  1946. {
  1947. int num_enabled = 0;
  1948. for (int i = 0; i < (int)sizeof(policy) * 8; i++)
  1949. {
  1950. if (is_enabled(i))
  1951. num_enabled++;
  1952. }
  1953. return num_enabled;
  1954. }
  1955. #else
  1956. CpuSet::CpuSet()
  1957. {
  1958. }
  1959. void CpuSet::enable(int /* cpu */)
  1960. {
  1961. }
  1962. void CpuSet::disable(int /* cpu */)
  1963. {
  1964. }
  1965. void CpuSet::disable_all()
  1966. {
  1967. }
  1968. bool CpuSet::is_enabled(int /* cpu */) const
  1969. {
  1970. return true;
  1971. }
  1972. int CpuSet::num_enabled() const
  1973. {
  1974. return get_cpu_count();
  1975. }
  1976. #endif
  1977. int cpu_support_arm_edsp()
  1978. {
  1979. try_initialize_global_cpu_info();
  1980. #if __arm__ && !__aarch64__
  1981. #if defined _WIN32
  1982. return g_cpu_support_arm_edsp;
  1983. #elif defined __ANDROID__ || defined __linux__
  1984. return g_hwcaps & HWCAP_EDSP;
  1985. #elif __APPLE__
  1986. return g_hw_cputype == CPU_TYPE_ARM;
  1987. #else
  1988. return 0;
  1989. #endif
  1990. #else
  1991. return 0;
  1992. #endif
  1993. }
  1994. int cpu_support_arm_neon()
  1995. {
  1996. try_initialize_global_cpu_info();
  1997. #if __aarch64__
  1998. return 1;
  1999. #elif __arm__
  2000. #if defined _WIN32
  2001. return g_cpu_support_arm_neon;
  2002. #elif defined __ANDROID__ || defined __linux__
  2003. return g_hwcaps & HWCAP_NEON;
  2004. #elif __APPLE__
  2005. return g_hw_cputype == CPU_TYPE_ARM && g_hw_cpusubtype > CPU_SUBTYPE_ARM_V7;
  2006. #else
  2007. return 0;
  2008. #endif
  2009. #else
  2010. return 0;
  2011. #endif
  2012. }
  2013. int cpu_support_arm_vfpv4()
  2014. {
  2015. try_initialize_global_cpu_info();
  2016. #if __aarch64__
  2017. return 1;
  2018. #elif __arm__
  2019. #if defined _WIN32
  2020. return g_cpu_support_arm_vfpv4;
  2021. #elif defined __ANDROID__ || defined __linux__
  2022. return g_hwcaps & HWCAP_VFPv4;
  2023. #elif __APPLE__
  2024. return g_hw_cputype == CPU_TYPE_ARM && g_hw_cpusubtype > CPU_SUBTYPE_ARM_V7S;
  2025. #else
  2026. return 0;
  2027. #endif
  2028. #else
  2029. return 0;
  2030. #endif
  2031. }
  2032. int cpu_support_arm_asimdhp()
  2033. {
  2034. try_initialize_global_cpu_info();
  2035. #if __aarch64__
  2036. #if defined _WIN32
  2037. return g_cpu_support_arm_asimdhp;
  2038. #elif defined __ANDROID__ || defined __linux__
  2039. return g_hwcaps & HWCAP_ASIMDHP;
  2040. #elif __APPLE__
  2041. return g_hw_optional_arm_FEAT_FP16
  2042. || g_hw_cpufamily == CPUFAMILY_ARM_MONSOON_MISTRAL
  2043. || g_hw_cpufamily == CPUFAMILY_ARM_VORTEX_TEMPEST
  2044. || g_hw_cpufamily == CPUFAMILY_ARM_LIGHTNING_THUNDER
  2045. || g_hw_cpufamily == CPUFAMILY_ARM_FIRESTORM_ICESTORM
  2046. || g_hw_cpufamily == CPUFAMILY_ARM_AVALANCHE_BLIZZARD
  2047. || g_hw_cpufamily == CPUFAMILY_ARM_EVEREST_SAWTOOTH
  2048. || g_hw_cpufamily == CPUFAMILY_ARM_COLL
  2049. || g_hw_cpufamily == CPUFAMILY_ARM_IBIZA
  2050. || g_hw_cpufamily == CPUFAMILY_ARM_LOBOS
  2051. || g_hw_cpufamily == CPUFAMILY_ARM_PALMA;
  2052. #else
  2053. return 0;
  2054. #endif
  2055. #else
  2056. return 0;
  2057. #endif
  2058. }
  2059. int cpu_support_arm_cpuid()
  2060. {
  2061. try_initialize_global_cpu_info();
  2062. #if __aarch64__
  2063. #if defined _WIN32
  2064. return g_cpu_support_arm_cpuid;
  2065. #elif defined __ANDROID__ || defined __linux__
  2066. return g_hwcaps & HWCAP_CPUID;
  2067. #elif __APPLE__
  2068. return 0;
  2069. #else
  2070. return 0;
  2071. #endif
  2072. #else
  2073. return 0;
  2074. #endif
  2075. }
  2076. int cpu_support_arm_asimddp()
  2077. {
  2078. try_initialize_global_cpu_info();
  2079. #if __aarch64__
  2080. #if defined _WIN32
  2081. return g_cpu_support_arm_asimddp;
  2082. #elif defined __ANDROID__ || defined __linux__
  2083. return g_hwcaps & HWCAP_ASIMDDP;
  2084. #elif __APPLE__
  2085. return g_hw_optional_arm_FEAT_DotProd
  2086. || g_hw_cpufamily == CPUFAMILY_ARM_LIGHTNING_THUNDER
  2087. || g_hw_cpufamily == CPUFAMILY_ARM_FIRESTORM_ICESTORM
  2088. || g_hw_cpufamily == CPUFAMILY_ARM_AVALANCHE_BLIZZARD
  2089. || g_hw_cpufamily == CPUFAMILY_ARM_EVEREST_SAWTOOTH
  2090. || g_hw_cpufamily == CPUFAMILY_ARM_COLL
  2091. || g_hw_cpufamily == CPUFAMILY_ARM_IBIZA
  2092. || g_hw_cpufamily == CPUFAMILY_ARM_LOBOS
  2093. || g_hw_cpufamily == CPUFAMILY_ARM_PALMA;
  2094. #else
  2095. return 0;
  2096. #endif
  2097. #else
  2098. return 0;
  2099. #endif
  2100. }
  2101. int cpu_support_arm_asimdfhm()
  2102. {
  2103. try_initialize_global_cpu_info();
  2104. #if __aarch64__
  2105. #if defined _WIN32
  2106. return g_cpu_support_arm_asimdfhm;
  2107. #elif defined __ANDROID__ || defined __linux__
  2108. return g_hwcaps & HWCAP_ASIMDFHM;
  2109. #elif __APPLE__
  2110. return g_hw_optional_arm_FEAT_FHM
  2111. || g_hw_cpufamily == CPUFAMILY_ARM_LIGHTNING_THUNDER
  2112. || g_hw_cpufamily == CPUFAMILY_ARM_FIRESTORM_ICESTORM
  2113. || g_hw_cpufamily == CPUFAMILY_ARM_AVALANCHE_BLIZZARD
  2114. || g_hw_cpufamily == CPUFAMILY_ARM_EVEREST_SAWTOOTH
  2115. || g_hw_cpufamily == CPUFAMILY_ARM_COLL
  2116. || g_hw_cpufamily == CPUFAMILY_ARM_IBIZA
  2117. || g_hw_cpufamily == CPUFAMILY_ARM_LOBOS
  2118. || g_hw_cpufamily == CPUFAMILY_ARM_PALMA;
  2119. #else
  2120. return 0;
  2121. #endif
  2122. #else
  2123. return 0;
  2124. #endif
  2125. }
  2126. int cpu_support_arm_bf16()
  2127. {
  2128. try_initialize_global_cpu_info();
  2129. #if __aarch64__
  2130. #if defined _WIN32
  2131. return g_cpu_support_arm_bf16;
  2132. #elif defined __ANDROID__ || defined __linux__
  2133. return g_hwcaps2 & HWCAP2_BF16;
  2134. #elif __APPLE__
  2135. return g_hw_optional_arm_FEAT_BF16
  2136. || g_hw_cpufamily == CPUFAMILY_ARM_AVALANCHE_BLIZZARD
  2137. || g_hw_cpufamily == CPUFAMILY_ARM_EVEREST_SAWTOOTH
  2138. || g_hw_cpufamily == CPUFAMILY_ARM_COLL
  2139. || g_hw_cpufamily == CPUFAMILY_ARM_IBIZA
  2140. || g_hw_cpufamily == CPUFAMILY_ARM_LOBOS
  2141. || g_hw_cpufamily == CPUFAMILY_ARM_PALMA;
  2142. #else
  2143. return 0;
  2144. #endif
  2145. #else
  2146. return 0;
  2147. #endif
  2148. }
  2149. int cpu_support_arm_i8mm()
  2150. {
  2151. try_initialize_global_cpu_info();
  2152. #if __aarch64__
  2153. #if defined _WIN32
  2154. return g_cpu_support_arm_i8mm;
  2155. #elif defined __ANDROID__ || defined __linux__
  2156. return g_hwcaps2 & HWCAP2_I8MM;
  2157. #elif __APPLE__
  2158. return g_hw_optional_arm_FEAT_I8MM
  2159. || g_hw_cpufamily == CPUFAMILY_ARM_AVALANCHE_BLIZZARD
  2160. || g_hw_cpufamily == CPUFAMILY_ARM_EVEREST_SAWTOOTH
  2161. || g_hw_cpufamily == CPUFAMILY_ARM_COLL
  2162. || g_hw_cpufamily == CPUFAMILY_ARM_IBIZA
  2163. || g_hw_cpufamily == CPUFAMILY_ARM_LOBOS
  2164. || g_hw_cpufamily == CPUFAMILY_ARM_PALMA;
  2165. #else
  2166. return 0;
  2167. #endif
  2168. #else
  2169. return 0;
  2170. #endif
  2171. }
  2172. int cpu_support_arm_sve()
  2173. {
  2174. try_initialize_global_cpu_info();
  2175. #if __aarch64__
  2176. #if defined _WIN32
  2177. return g_cpu_support_arm_sve;
  2178. #elif defined __ANDROID__ || defined __linux__
  2179. return g_hwcaps & HWCAP_SVE;
  2180. #elif __APPLE__
  2181. return 0; // no known apple cpu support armv8.6 sve
  2182. #else
  2183. return 0;
  2184. #endif
  2185. #else
  2186. return 0;
  2187. #endif
  2188. }
  2189. int cpu_support_arm_sve2()
  2190. {
  2191. try_initialize_global_cpu_info();
  2192. #if __aarch64__
  2193. #if defined _WIN32
  2194. return g_cpu_support_arm_sve2;
  2195. #elif defined __ANDROID__ || defined __linux__
  2196. return g_hwcaps2 & HWCAP2_SVE2;
  2197. #elif __APPLE__
  2198. return 0; // no known apple cpu support armv8.6 sve2
  2199. #else
  2200. return 0;
  2201. #endif
  2202. #else
  2203. return 0;
  2204. #endif
  2205. }
  2206. int cpu_support_arm_svebf16()
  2207. {
  2208. try_initialize_global_cpu_info();
  2209. #if __aarch64__
  2210. #if defined _WIN32
  2211. return g_cpu_support_arm_svebf16;
  2212. #elif defined __ANDROID__ || defined __linux__
  2213. return g_hwcaps2 & HWCAP2_SVEBF16;
  2214. #elif __APPLE__
  2215. return 0; // no known apple cpu support armv8.6 svebf16
  2216. #else
  2217. return 0;
  2218. #endif
  2219. #else
  2220. return 0;
  2221. #endif
  2222. }
  2223. int cpu_support_arm_svei8mm()
  2224. {
  2225. try_initialize_global_cpu_info();
  2226. #if __aarch64__
  2227. #if defined _WIN32
  2228. return g_cpu_support_arm_svei8mm;
  2229. #elif defined __ANDROID__ || defined __linux__
  2230. return g_hwcaps2 & HWCAP2_SVEI8MM;
  2231. #elif __APPLE__
  2232. return 0; // no known apple cpu support armv8.6 svei8mm
  2233. #else
  2234. return 0;
  2235. #endif
  2236. #else
  2237. return 0;
  2238. #endif
  2239. }
  2240. int cpu_support_arm_svef32mm()
  2241. {
  2242. try_initialize_global_cpu_info();
  2243. #if __aarch64__
  2244. #if defined _WIN32
  2245. return g_cpu_support_arm_svef32mm;
  2246. #elif defined __ANDROID__ || defined __linux__
  2247. return g_hwcaps2 & HWCAP2_SVEF32MM;
  2248. #elif __APPLE__
  2249. return 0; // no known apple cpu support armv8.6 svef32mm
  2250. #else
  2251. return 0;
  2252. #endif
  2253. #else
  2254. return 0;
  2255. #endif
  2256. }
  2257. int cpu_support_x86_avx()
  2258. {
  2259. try_initialize_global_cpu_info();
  2260. #if defined(__i386__) || defined(__x86_64__) || defined(_M_IX86) || defined(_M_X64)
  2261. return g_cpu_support_x86_avx;
  2262. #else
  2263. return 0;
  2264. #endif
  2265. }
  2266. int cpu_support_x86_fma()
  2267. {
  2268. try_initialize_global_cpu_info();
  2269. #if defined(__i386__) || defined(__x86_64__) || defined(_M_IX86) || defined(_M_X64)
  2270. return g_cpu_support_x86_fma;
  2271. #else
  2272. return 0;
  2273. #endif
  2274. }
  2275. int cpu_support_x86_xop()
  2276. {
  2277. try_initialize_global_cpu_info();
  2278. #if defined(__i386__) || defined(__x86_64__) || defined(_M_IX86) || defined(_M_X64)
  2279. return g_cpu_support_x86_xop;
  2280. #else
  2281. return 0;
  2282. #endif
  2283. }
  2284. int cpu_support_x86_f16c()
  2285. {
  2286. try_initialize_global_cpu_info();
  2287. #if defined(__i386__) || defined(__x86_64__) || defined(_M_IX86) || defined(_M_X64)
  2288. return g_cpu_support_x86_f16c;
  2289. #else
  2290. return 0;
  2291. #endif
  2292. }
  2293. int cpu_support_x86_avx2()
  2294. {
  2295. try_initialize_global_cpu_info();
  2296. #if defined(__i386__) || defined(__x86_64__) || defined(_M_IX86) || defined(_M_X64)
  2297. return g_cpu_support_x86_avx2;
  2298. #else
  2299. return 0;
  2300. #endif
  2301. }
  2302. int cpu_support_x86_avx_vnni()
  2303. {
  2304. try_initialize_global_cpu_info();
  2305. #if defined(__i386__) || defined(__x86_64__) || defined(_M_IX86) || defined(_M_X64)
  2306. return g_cpu_support_x86_avx_vnni;
  2307. #else
  2308. return 0;
  2309. #endif
  2310. }
  2311. int cpu_support_x86_avx_vnni_int8()
  2312. {
  2313. try_initialize_global_cpu_info();
  2314. #if defined(__i386__) || defined(__x86_64__) || defined(_M_IX86) || defined(_M_X64)
  2315. return g_cpu_support_x86_avx_vnni_int8;
  2316. #else
  2317. return 0;
  2318. #endif
  2319. }
  2320. int cpu_support_x86_avx_vnni_int16()
  2321. {
  2322. try_initialize_global_cpu_info();
  2323. #if defined(__i386__) || defined(__x86_64__) || defined(_M_IX86) || defined(_M_X64)
  2324. return g_cpu_support_x86_avx_vnni_int16;
  2325. #else
  2326. return 0;
  2327. #endif
  2328. }
  2329. int cpu_support_x86_avx_ne_convert()
  2330. {
  2331. try_initialize_global_cpu_info();
  2332. #if defined(__i386__) || defined(__x86_64__) || defined(_M_IX86) || defined(_M_X64)
  2333. return g_cpu_support_x86_avx_ne_convert;
  2334. #else
  2335. return 0;
  2336. #endif
  2337. }
  2338. int cpu_support_x86_avx512()
  2339. {
  2340. try_initialize_global_cpu_info();
  2341. #if defined(__i386__) || defined(__x86_64__) || defined(_M_IX86) || defined(_M_X64)
  2342. return g_cpu_support_x86_avx512;
  2343. #else
  2344. return 0;
  2345. #endif
  2346. }
  2347. int cpu_support_x86_avx512_vnni()
  2348. {
  2349. try_initialize_global_cpu_info();
  2350. #if defined(__i386__) || defined(__x86_64__) || defined(_M_IX86) || defined(_M_X64)
  2351. return g_cpu_support_x86_avx512_vnni;
  2352. #else
  2353. return 0;
  2354. #endif
  2355. }
  2356. int cpu_support_x86_avx512_bf16()
  2357. {
  2358. try_initialize_global_cpu_info();
  2359. #if defined(__i386__) || defined(__x86_64__) || defined(_M_IX86) || defined(_M_X64)
  2360. return g_cpu_support_x86_avx512_bf16;
  2361. #else
  2362. return 0;
  2363. #endif
  2364. }
  2365. int cpu_support_x86_avx512_fp16()
  2366. {
  2367. try_initialize_global_cpu_info();
  2368. #if defined(__i386__) || defined(__x86_64__) || defined(_M_IX86) || defined(_M_X64)
  2369. return g_cpu_support_x86_avx512_fp16;
  2370. #else
  2371. return 0;
  2372. #endif
  2373. }
  2374. int cpu_support_mips_msa()
  2375. {
  2376. try_initialize_global_cpu_info();
  2377. #if defined __ANDROID__ || defined __linux__
  2378. #if __mips__
  2379. return g_hwcaps & HWCAP_MIPS_MSA;
  2380. #else
  2381. return 0;
  2382. #endif
  2383. #else
  2384. return 0;
  2385. #endif
  2386. }
  2387. int cpu_support_loongarch_lsx()
  2388. {
  2389. try_initialize_global_cpu_info();
  2390. #if defined __ANDROID__ || defined __linux__
  2391. #if __loongarch64
  2392. return g_hwcaps & HWCAP_LOONGARCH_LSX;
  2393. #else
  2394. return 0;
  2395. #endif
  2396. #else
  2397. return 0;
  2398. #endif
  2399. }
  2400. int cpu_support_loongarch_lasx()
  2401. {
  2402. try_initialize_global_cpu_info();
  2403. #if defined __ANDROID__ || defined __linux__
  2404. #if __loongarch64
  2405. return g_hwcaps & HWCAP_LOONGARCH_LASX;
  2406. #else
  2407. return 0;
  2408. #endif
  2409. #else
  2410. return 0;
  2411. #endif
  2412. }
  2413. int cpu_support_loongson_mmi()
  2414. {
  2415. try_initialize_global_cpu_info();
  2416. #if defined __ANDROID__ || defined __linux__
  2417. #if __mips__
  2418. return g_hwcaps & HWCAP_LOONGSON_MMI;
  2419. #else
  2420. return 0;
  2421. #endif
  2422. #else
  2423. return 0;
  2424. #endif
  2425. }
  2426. int cpu_support_riscv_v()
  2427. {
  2428. try_initialize_global_cpu_info();
  2429. #if defined __ANDROID__ || defined __linux__
  2430. #if __riscv
  2431. return g_hwcaps & COMPAT_HWCAP_ISA_V;
  2432. #else
  2433. return 0;
  2434. #endif
  2435. #else
  2436. return 0;
  2437. #endif
  2438. }
  2439. int cpu_support_riscv_zfh()
  2440. {
  2441. try_initialize_global_cpu_info();
  2442. #if defined __ANDROID__ || defined __linux__
  2443. #if __riscv
  2444. // v + f does not imply zfh, but how to discover zfh properly ?
  2445. // upstream issue https://github.com/riscv/riscv-isa-manual/issues/414
  2446. return g_hwcaps & COMPAT_HWCAP_ISA_V && g_hwcaps & COMPAT_HWCAP_ISA_F;
  2447. #else
  2448. return 0;
  2449. #endif
  2450. #else
  2451. return 0;
  2452. #endif
  2453. }
  2454. int cpu_support_riscv_zvfh()
  2455. {
  2456. try_initialize_global_cpu_info();
  2457. #if defined __ANDROID__ || defined __linux__
  2458. #if __riscv
  2459. // v + f does not imply zfh, but how to discover zvfh properly ?
  2460. // upstream issue https://github.com/riscv/riscv-isa-manual/issues/414
  2461. return g_hwcaps & COMPAT_HWCAP_ISA_V && g_hwcaps & COMPAT_HWCAP_ISA_F;
  2462. #else
  2463. return 0;
  2464. #endif
  2465. #else
  2466. return 0;
  2467. #endif
  2468. }
  2469. int cpu_support_riscv_xtheadvector()
  2470. {
  2471. try_initialize_global_cpu_info();
  2472. #if defined __ANDROID__ || defined __linux__
  2473. #if __riscv
  2474. // v + f does not imply zfh, but how to discover zvfh properly ?
  2475. // upstream issue https://github.com/riscv/riscv-isa-manual/issues/414
  2476. return g_hwcaps & COMPAT_HWCAP_ISA_V && g_hwcaps & COMPAT_HWCAP_ISA_F;
  2477. #else
  2478. return 0;
  2479. #endif
  2480. #else
  2481. return 0;
  2482. #endif
  2483. }
  2484. int cpu_riscv_vlenb()
  2485. {
  2486. #if C906
  2487. // FIXME xuantie qemu reports all zero auxv flags
  2488. return 16;
  2489. #endif
  2490. try_initialize_global_cpu_info();
  2491. #if __riscv
  2492. if (!cpu_support_riscv_v())
  2493. return 0;
  2494. int a = 0;
  2495. asm volatile(
  2496. ".word 0xc22026f3 \n" // csrr a3, vlenb
  2497. "mv %0, a3 \n"
  2498. : "=r"(a)
  2499. :
  2500. : "memory", "a3");
  2501. return a;
  2502. #else
  2503. return 0;
  2504. #endif
  2505. }
  2506. int get_cpu_count()
  2507. {
  2508. try_initialize_global_cpu_info();
  2509. return g_cpucount;
  2510. }
  2511. int get_little_cpu_count()
  2512. {
  2513. try_initialize_global_cpu_info();
  2514. return get_cpu_thread_affinity_mask(1).num_enabled();
  2515. }
  2516. int get_big_cpu_count()
  2517. {
  2518. try_initialize_global_cpu_info();
  2519. int big_cpu_count = get_cpu_thread_affinity_mask(2).num_enabled();
  2520. return big_cpu_count ? big_cpu_count : g_cpucount;
  2521. }
  2522. int get_physical_cpu_count()
  2523. {
  2524. try_initialize_global_cpu_info();
  2525. return g_physical_cpucount;
  2526. }
  2527. int get_physical_little_cpu_count()
  2528. {
  2529. try_initialize_global_cpu_info();
  2530. if (g_physical_cpucount == g_cpucount)
  2531. return get_little_cpu_count();
  2532. return g_physical_cpucount * 2 - g_cpucount;
  2533. }
  2534. int get_physical_big_cpu_count()
  2535. {
  2536. try_initialize_global_cpu_info();
  2537. if (g_physical_cpucount == g_cpucount)
  2538. return get_big_cpu_count();
  2539. return g_cpucount - g_physical_cpucount;
  2540. }
  2541. int get_cpu_level2_cache_size()
  2542. {
  2543. try_initialize_global_cpu_info();
  2544. return g_cpu_level2_cachesize;
  2545. }
  2546. int get_cpu_level3_cache_size()
  2547. {
  2548. try_initialize_global_cpu_info();
  2549. return g_cpu_level3_cachesize;
  2550. }
  2551. int get_cpu_powersave()
  2552. {
  2553. try_initialize_global_cpu_info();
  2554. return g_powersave;
  2555. }
  2556. int set_cpu_powersave(int powersave)
  2557. {
  2558. try_initialize_global_cpu_info();
  2559. if (powersave < 0 || powersave > 2)
  2560. {
  2561. NCNN_LOGE("powersave %d not supported", powersave);
  2562. return -1;
  2563. }
  2564. const CpuSet& thread_affinity_mask = get_cpu_thread_affinity_mask(powersave);
  2565. int ret = set_cpu_thread_affinity(thread_affinity_mask);
  2566. if (ret != 0)
  2567. return ret;
  2568. g_powersave = powersave;
  2569. return 0;
  2570. }
  2571. const CpuSet& get_cpu_thread_affinity_mask(int powersave)
  2572. {
  2573. try_initialize_global_cpu_info();
  2574. if (powersave == 0)
  2575. return g_cpu_affinity_mask_all;
  2576. if (powersave == 1)
  2577. return g_cpu_affinity_mask_little;
  2578. if (powersave == 2)
  2579. return g_cpu_affinity_mask_big;
  2580. NCNN_LOGE("powersave %d not supported", powersave);
  2581. // fallback to all cores anyway
  2582. return g_cpu_affinity_mask_all;
  2583. }
  2584. int set_cpu_thread_affinity(const CpuSet& thread_affinity_mask)
  2585. {
  2586. try_initialize_global_cpu_info();
  2587. #if defined __ANDROID__ || defined __linux__ || defined _WIN32
  2588. #ifdef _OPENMP
  2589. int num_threads = thread_affinity_mask.num_enabled();
  2590. // set affinity for each thread
  2591. set_omp_num_threads(num_threads);
  2592. std::vector<int> ssarets(num_threads, 0);
  2593. #pragma omp parallel for num_threads(num_threads)
  2594. for (int i = 0; i < num_threads; i++)
  2595. {
  2596. ssarets[i] = set_sched_affinity(thread_affinity_mask);
  2597. }
  2598. for (int i = 0; i < num_threads; i++)
  2599. {
  2600. if (ssarets[i] != 0)
  2601. return -1;
  2602. }
  2603. #else
  2604. int ssaret = set_sched_affinity(thread_affinity_mask);
  2605. if (ssaret != 0)
  2606. return -1;
  2607. #endif
  2608. return 0;
  2609. #elif __APPLE__
  2610. #ifdef _OPENMP
  2611. int num_threads = thread_affinity_mask.num_enabled();
  2612. // set affinity for each thread
  2613. set_omp_num_threads(num_threads);
  2614. std::vector<int> ssarets(num_threads, 0);
  2615. #pragma omp parallel for num_threads(num_threads)
  2616. for (int i = 0; i < num_threads; i++)
  2617. {
  2618. // assign one core for each thread
  2619. int core = -1 - i;
  2620. for (int j = 0; j < (int)sizeof(thread_affinity_mask.policy) * 8; j++)
  2621. {
  2622. if (thread_affinity_mask.is_enabled(j))
  2623. {
  2624. if (core == -1)
  2625. {
  2626. core = j;
  2627. break;
  2628. }
  2629. else
  2630. {
  2631. core++;
  2632. }
  2633. }
  2634. }
  2635. CpuSet this_thread_affinity_mask;
  2636. if (core != -1 - i)
  2637. {
  2638. this_thread_affinity_mask.enable(core);
  2639. }
  2640. ssarets[i] = set_sched_affinity(this_thread_affinity_mask);
  2641. }
  2642. for (int i = 0; i < num_threads; i++)
  2643. {
  2644. if (ssarets[i] != 0)
  2645. return -1;
  2646. }
  2647. #else
  2648. int ssaret = set_sched_affinity(thread_affinity_mask);
  2649. if (ssaret != 0)
  2650. return -1;
  2651. #endif
  2652. return 0;
  2653. #else
  2654. // TODO
  2655. (void)thread_affinity_mask;
  2656. return -1;
  2657. #endif
  2658. }
  2659. int is_current_thread_running_on_a53_a55()
  2660. {
  2661. try_initialize_global_cpu_info();
  2662. #if defined __ANDROID__ || defined __linux__
  2663. #if __aarch64__
  2664. if (g_cpu_is_arm_a53_a55 == 0)
  2665. return 0; // all non a53/a55
  2666. if (g_cpu_is_arm_a53_a55 == 1)
  2667. return 1; // all a53/a55
  2668. if (g_powersave == 2)
  2669. return 0; // big clusters
  2670. if (g_powersave == 1)
  2671. return 1; // little clusters
  2672. // little cores are a53/a55
  2673. // use cpuid for retrieving midr since kernel 4.7+
  2674. if (cpu_support_arm_cpuid())
  2675. {
  2676. unsigned int midr = get_midr_from_register();
  2677. if (midr)
  2678. return midr_is_a53_a55(midr);
  2679. }
  2680. // check if affinity cpuid is in the little ones
  2681. CpuSet thread_cs;
  2682. int ret = get_sched_affinity(thread_cs);
  2683. if (ret != 0)
  2684. {
  2685. // no affinity capability
  2686. return 0;
  2687. }
  2688. const CpuSet& little_cs = get_cpu_thread_affinity_mask(1);
  2689. for (int i = 0; i < g_cpucount; i++)
  2690. {
  2691. if (!thread_cs.is_enabled(i))
  2692. continue;
  2693. if (!little_cs.is_enabled(i))
  2694. return 0;
  2695. }
  2696. // all affinity cpuids are little core
  2697. return 1;
  2698. #else
  2699. return 0;
  2700. #endif // __aarch64__
  2701. #else
  2702. return 0;
  2703. #endif // defined __ANDROID__ || defined __linux__
  2704. }
  2705. int get_omp_num_threads()
  2706. {
  2707. #ifdef _OPENMP
  2708. return omp_get_num_threads();
  2709. #else
  2710. return 1;
  2711. #endif
  2712. }
  2713. void set_omp_num_threads(int num_threads)
  2714. {
  2715. #ifdef _OPENMP
  2716. omp_set_num_threads(num_threads);
  2717. #else
  2718. (void)num_threads;
  2719. #endif
  2720. }
  2721. int get_omp_dynamic()
  2722. {
  2723. #ifdef _OPENMP
  2724. return omp_get_dynamic();
  2725. #else
  2726. return 0;
  2727. #endif
  2728. }
  2729. void set_omp_dynamic(int dynamic)
  2730. {
  2731. #ifdef _OPENMP
  2732. omp_set_dynamic(dynamic);
  2733. #else
  2734. (void)dynamic;
  2735. #endif
  2736. }
  2737. int get_omp_thread_num()
  2738. {
  2739. #ifdef _OPENMP
  2740. return omp_get_thread_num();
  2741. #else
  2742. return 0;
  2743. #endif
  2744. }
  2745. int get_kmp_blocktime()
  2746. {
  2747. #if defined(_OPENMP) && (__clang__ || defined(_OPENMP_LLVM_RUNTIME))
  2748. return kmp_get_blocktime();
  2749. #else
  2750. return 0;
  2751. #endif
  2752. }
  2753. void set_kmp_blocktime(int time_ms)
  2754. {
  2755. #if defined(_OPENMP) && (__clang__ || defined(_OPENMP_LLVM_RUNTIME))
  2756. kmp_set_blocktime(time_ms);
  2757. #else
  2758. (void)time_ms;
  2759. #endif
  2760. }
  2761. static ncnn::ThreadLocalStorage tls_flush_denormals;
  2762. int get_flush_denormals()
  2763. {
  2764. #if defined(__SSE3__)
  2765. return (int)reinterpret_cast<size_t>(tls_flush_denormals.get());
  2766. #else
  2767. return 0;
  2768. #endif
  2769. }
  2770. int set_flush_denormals(int flush_denormals)
  2771. {
  2772. if (flush_denormals < 0 || flush_denormals > 3)
  2773. {
  2774. NCNN_LOGE("denormals_zero %d not supported", flush_denormals);
  2775. return -1;
  2776. }
  2777. #if defined(__SSE3__)
  2778. if (flush_denormals == 0)
  2779. {
  2780. _MM_SET_DENORMALS_ZERO_MODE(_MM_DENORMALS_ZERO_OFF);
  2781. _MM_SET_FLUSH_ZERO_MODE(_MM_FLUSH_ZERO_OFF);
  2782. }
  2783. else if (flush_denormals == 1)
  2784. {
  2785. _MM_SET_DENORMALS_ZERO_MODE(_MM_DENORMALS_ZERO_ON);
  2786. _MM_SET_FLUSH_ZERO_MODE(_MM_FLUSH_ZERO_OFF);
  2787. }
  2788. else if (flush_denormals == 2)
  2789. {
  2790. _MM_SET_DENORMALS_ZERO_MODE(_MM_DENORMALS_ZERO_OFF);
  2791. _MM_SET_FLUSH_ZERO_MODE(_MM_FLUSH_ZERO_ON);
  2792. }
  2793. else if (flush_denormals == 3)
  2794. {
  2795. _MM_SET_DENORMALS_ZERO_MODE(_MM_DENORMALS_ZERO_ON);
  2796. _MM_SET_FLUSH_ZERO_MODE(_MM_FLUSH_ZERO_ON);
  2797. }
  2798. tls_flush_denormals.set(reinterpret_cast<void*>((size_t)flush_denormals));
  2799. return 0;
  2800. #else
  2801. return 0;
  2802. #endif
  2803. }
  2804. } // namespace ncnn
  2805. #if defined __ANDROID__ && defined(_OPENMP) && __clang__
  2806. #ifdef __cplusplus
  2807. extern "C" {
  2808. #endif
  2809. void __wrap___kmp_affinity_determine_capable(const char* /*env_var*/)
  2810. {
  2811. // the internal affinity routines in llvm openmp call abort on __NR_sched_getaffinity / __NR_sched_setaffinity fails
  2812. // ref KMPNativeAffinity::get_system_affinity/set_system_affinity in openmp/runtime/src/kmp_affinity.h
  2813. // and cpu core goes offline in powersave mode on android, which triggers abort
  2814. // ATM there is no known api for controlling the abort behavior
  2815. // override __kmp_affinity_determine_capable with empty body to disable affinity regardless of KMP_AFFINITY env_var
  2816. // ugly hack works >.< --- nihui
  2817. }
  2818. #ifdef __cplusplus
  2819. } // extern "C"
  2820. #endif
  2821. #endif