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.

dynamic.c 30 kB

7 years ago
12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037
  1. /*********************************************************************/
  2. /* Copyright 2009, 2010 The University of Texas at Austin. */
  3. /* All rights reserved. */
  4. /* */
  5. /* Redistribution and use in source and binary forms, with or */
  6. /* without modification, are permitted provided that the following */
  7. /* conditions are met: */
  8. /* */
  9. /* 1. Redistributions of source code must retain the above */
  10. /* copyright notice, this list of conditions and the following */
  11. /* disclaimer. */
  12. /* */
  13. /* 2. Redistributions in binary form must reproduce the above */
  14. /* copyright notice, this list of conditions and the following */
  15. /* disclaimer in the documentation and/or other materials */
  16. /* provided with the distribution. */
  17. /* */
  18. /* THIS SOFTWARE IS PROVIDED BY THE UNIVERSITY OF TEXAS AT */
  19. /* AUSTIN ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, */
  20. /* INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF */
  21. /* MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE */
  22. /* DISCLAIMED. IN NO EVENT SHALL THE UNIVERSITY OF TEXAS AT */
  23. /* AUSTIN OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, */
  24. /* INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES */
  25. /* (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE */
  26. /* GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR */
  27. /* BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF */
  28. /* LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT */
  29. /* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT */
  30. /* OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE */
  31. /* POSSIBILITY OF SUCH DAMAGE. */
  32. /* */
  33. /* The views and conclusions contained in the software and */
  34. /* documentation are those of the authors and should not be */
  35. /* interpreted as representing official policies, either expressed */
  36. /* or implied, of The University of Texas at Austin. */
  37. /*********************************************************************/
  38. #include "common.h"
  39. #ifdef _MSC_VER
  40. #define strncasecmp _strnicmp
  41. #define strcasecmp _stricmp
  42. #endif
  43. #ifdef ARCH_X86
  44. #define EXTERN extern
  45. #else
  46. #define EXTERN
  47. #endif
  48. #ifdef DYNAMIC_LIST
  49. extern gotoblas_t gotoblas_PRESCOTT;
  50. #ifdef DYN_ATHLON
  51. extern gotoblas_t gotoblas_ATHLON;
  52. #else
  53. #define gotoblas_ATHLON gotoblas_PRESCOTT
  54. #endif
  55. #ifdef DYN_KATMAI
  56. extern gotoblas_t gotoblas_KATMAI;
  57. #else
  58. #define gotoblas_KATMAI gotoblas_PRESCOTT
  59. #endif
  60. #ifdef DYN_BANIAS
  61. extern gotoblas_t gotoblas_BANIAS;
  62. #else
  63. #define gotoblas_BANIAS gotoblas_PRESCOTT
  64. #endif
  65. #ifdef DYN_COPPERMINE
  66. extern gotoblas_t gotoblas_COPPERMINE;
  67. #else
  68. #define gotoblas_COPPERMINE gotoblas_PRESCOTT
  69. #endif
  70. #ifdef DYN_NORTHWOOD
  71. extern gotoblas_t gotoblas_NORTHWOOD;
  72. #else
  73. #define gotoblas_NORTHWOOD gotoblas_PRESCOTT
  74. #endif
  75. #ifdef DYN_CORE2
  76. extern gotoblas_t gotoblas_CORE2;
  77. #else
  78. #define gotoblas_CORE2 gotoblas_PRESCOTT
  79. #endif
  80. #ifdef DYN_NEHALEM
  81. extern gotoblas_t gotoblas_NEHALEM;
  82. #else
  83. #define gotoblas_NEHALEM gotoblas_PRESCOTT
  84. #endif
  85. #ifdef DYN_BARCELONA
  86. extern gotoblas_t gotoblas_BARCELONA;
  87. #elif defined(DYN_NEHALEM)
  88. #define gotoblas_BARCELONA gotoblas_NEHALEM
  89. #else
  90. #define gotoblas_BARCELONA gotoblas_PRESCOTT
  91. #endif
  92. #ifdef DYN_ATOM
  93. extern gotoblas_t gotoblas_ATOM;
  94. elif defined(DYN_NEHALEM)
  95. #define gotoblas_ATOM gotoblas_NEHALEM
  96. #else
  97. #define gotoblas_ATOM gotoblas_PRESCOTT
  98. #endif
  99. #ifdef DYN_NANO
  100. extern gotoblas_t gotoblas_NANO;
  101. #else
  102. #define gotoblas_NANO gotoblas_PRESCOTT
  103. #endif
  104. #ifdef DYN_PENRYN
  105. extern gotoblas_t gotoblas_PENRYN;
  106. #else
  107. #define gotoblas_PENRYN gotoblas_PRESCOTT
  108. #endif
  109. #ifdef DYN_DUNNINGTON
  110. extern gotoblas_t gotoblas_DUNNINGTON;
  111. #else
  112. #define gotoblas_DUNNINGTON gotoblas_PRESCOTT
  113. #endif
  114. #ifdef DYN_OPTERON
  115. extern gotoblas_t gotoblas_OPTERON;
  116. #else
  117. #define gotoblas_OPTERON gotoblas_PRESCOTT
  118. #endif
  119. #ifdef DYN_OPTERON_SSE3
  120. extern gotoblas_t gotoblas_OPTERON_SSE3;
  121. #else
  122. #define gotoblas_OPTERON_SSE3 gotoblas_PRESCOTT
  123. #endif
  124. #ifdef DYN_BOBCAT
  125. extern gotoblas_t gotoblas_BOBCAT;
  126. #elif defined(DYN_NEHALEM)
  127. #define gotoblas_BOBCAT gotoblas_NEHALEM
  128. #else
  129. #define gotoblas_BOBCAT gotoblas_PRESCOTT
  130. #endif
  131. #ifdef DYN_SANDYBRIDGE
  132. extern gotoblas_t gotoblas_SANDYBRIDGE;
  133. #elif defined(DYN_NEHALEM)
  134. #define gotoblas_SANDYBRIDGE gotoblas_NEHALEM
  135. #else
  136. #define gotoblas_SANDYBRIDGE gotoblas_PRESCOTT
  137. #endif
  138. #ifdef DYN_BULLDOZER
  139. extern gotoblas_t gotoblas_BULLDOZER;
  140. #elif defined(DYN_SANDYBRIDGE)
  141. #define gotoblas_BULLDOZER gotoblas_SANDYBRIDGE
  142. #elif defined(DYN_NEHALEM)
  143. #define gotoblas_BULLDOZER gotoblas_NEHALEM
  144. #else
  145. #define gotoblas_BULLDOZER gotoblas_PRESCOTT
  146. #endif
  147. #ifdef DYN_PILEDRIVER
  148. extern gotoblas_t gotoblas_PILEDRIVER;
  149. #elif defined(DYN_SANDYBRIDGE)
  150. #define gotoblas_PILEDRIVER gotoblas_SANDYBRIDGE
  151. #elif defined(DYN_NEHALEM)
  152. #define gotoblas_PILEDRIVER gotoblas_NEHALEM
  153. #else
  154. #define gotoblas_PILEDRIVER gotoblas_PRESCOTT
  155. #endif
  156. #ifdef DYN_STEAMROLLER
  157. extern gotoblas_t gotoblas_STEAMROLLER;
  158. #elif defined(DYN_SANDYBRIDGE)
  159. #define gotoblas_STEAMROLLER gotoblas_SANDYBRIDGE
  160. #elif defined(DYN_NEHALEM)
  161. #define gotoblas_STEAMROLLER gotoblas_NEHALEM
  162. #else
  163. #define gotoblas_STEAMROLLER gotoblas_PRESCOTT
  164. #endif
  165. #ifdef DYN_EXCAVATOR
  166. extern gotoblas_t gotoblas_EXCAVATOR;
  167. #elif defined(DYN_SANDYBRIDGE)
  168. #define gotoblas_EXCAVATOR gotoblas_SANDYBRIDGE
  169. #elif defined(DYN_NEHALEM)
  170. #define gotoblas_EXCAVATOR gotoblas_NEHALEM
  171. #else
  172. #define gotoblas_EXCAVATOR gotoblas_PRESCOTT
  173. #endif
  174. #ifdef DYN_HASWELL
  175. extern gotoblas_t gotoblas_HASWELL;
  176. #elif defined(DYN_SANDYBRIDGE)
  177. #define gotoblas_HASWELL gotoblas_SANDYBRIDGE
  178. #elif defined(DYN_NEHALEM)
  179. #define gotoblas_HASWELL gotoblas_NEHALEM
  180. #else
  181. #define gotoblas_HASWELL gotoblas_PRESCOTT
  182. #endif
  183. #ifdef DYN_ZEN
  184. extern gotoblas_t gotoblas_ZEN;
  185. #elif defined(DYN_HASWELL)
  186. #define gotoblas_ZEN gotoblas_HASWELL
  187. #elif defined(DYN_SANDYBRIDGE)
  188. #define gotoblas_ZEN gotoblas_SANDYBRIDGE
  189. #elif defined(DYN_NEHALEM)
  190. #define gotoblas_ZEN gotoblas_NEHALEM
  191. #else
  192. #define gotoblas_ZEN gotoblas_PRESCOTT
  193. #endif
  194. #ifdef DYN_SKYLAKEX
  195. extern gotoblas_t gotoblas_SKYLAKEX;
  196. #elif defined(DYN_HASWELL)
  197. #define gotoblas_SKYLAKEX gotoblas_HASWELL
  198. #elif defined(DYN_SANDYBRIDGE)
  199. #define gotoblas_SKYLAKEX gotoblas_SANDYBRIDGE
  200. #elif defined(DYN_NEHALEM)
  201. #define gotoblas_SKYLAKEX gotoblas_NEHALEM
  202. #else
  203. #define gotoblas_SKYLAKEX gotoblas_PRESCOTT
  204. #endif
  205. #ifdef DYN_COOPERLAKE
  206. extern gotoblas_t gotoblas_COOPERLAKE;
  207. #elif defined(DYN_SKYLAKEX)
  208. #define gotoblas_COOPERLAKE gotoblas_SKYLAKEX
  209. #elif defined(DYN_HASWELL)
  210. #define gotoblas_COOPERLAKE gotoblas_HASWELL
  211. #elif defined(DYN_SANDYBRIDGE)
  212. #define gotoblas_COOPERLAKE gotoblas_SANDYBRIDGE
  213. #elif defined(DYN_NEHALEM)
  214. #define gotoblas_COOPERLAKE gotoblas_NEHALEM
  215. #else
  216. #define gotoblas_COOPERLAKE gotoblas_PRESCOTT
  217. #endif
  218. #else // not DYNAMIC_LIST
  219. EXTERN gotoblas_t gotoblas_KATMAI;
  220. EXTERN gotoblas_t gotoblas_COPPERMINE;
  221. EXTERN gotoblas_t gotoblas_NORTHWOOD;
  222. EXTERN gotoblas_t gotoblas_BANIAS;
  223. EXTERN gotoblas_t gotoblas_ATHLON;
  224. extern gotoblas_t gotoblas_PRESCOTT;
  225. extern gotoblas_t gotoblas_CORE2;
  226. extern gotoblas_t gotoblas_NEHALEM;
  227. extern gotoblas_t gotoblas_BARCELONA;
  228. #ifdef DYNAMIC_OLDER
  229. extern gotoblas_t gotoblas_ATOM;
  230. extern gotoblas_t gotoblas_NANO;
  231. extern gotoblas_t gotoblas_PENRYN;
  232. extern gotoblas_t gotoblas_DUNNINGTON;
  233. extern gotoblas_t gotoblas_OPTERON;
  234. extern gotoblas_t gotoblas_OPTERON_SSE3;
  235. extern gotoblas_t gotoblas_BOBCAT;
  236. #else
  237. #define gotoblas_ATOM gotoblas_NEHALEM
  238. #define gotoblas_NANO gotoblas_NEHALEM
  239. #define gotoblas_PENRYN gotoblas_CORE2
  240. #define gotoblas_DUNNINGTON gotoblas_CORE2
  241. #define gotoblas_OPTERON gotoblas_CORE2
  242. #define gotoblas_OPTERON_SSE3 gotoblas_CORE2
  243. #define gotoblas_BOBCAT gotoblas_CORE2
  244. #endif
  245. #ifndef NO_AVX
  246. extern gotoblas_t gotoblas_SANDYBRIDGE;
  247. extern gotoblas_t gotoblas_BULLDOZER;
  248. extern gotoblas_t gotoblas_PILEDRIVER;
  249. extern gotoblas_t gotoblas_STEAMROLLER;
  250. extern gotoblas_t gotoblas_EXCAVATOR;
  251. #ifdef NO_AVX2
  252. #define gotoblas_HASWELL gotoblas_SANDYBRIDGE
  253. #define gotoblas_SKYLAKEX gotoblas_SANDYBRIDGE
  254. #define gotoblas_COOPERLAKE gotoblas_SANDYBRIDGE
  255. #define gotoblas_ZEN gotoblas_SANDYBRIDGE
  256. #else
  257. extern gotoblas_t gotoblas_HASWELL;
  258. extern gotoblas_t gotoblas_ZEN;
  259. #ifndef NO_AVX512
  260. extern gotoblas_t gotoblas_SKYLAKEX;
  261. extern gotoblas_t gotoblas_COOPERLAKE;
  262. #else
  263. #define gotoblas_SKYLAKEX gotoblas_HASWELL
  264. #define gotoblas_COOPERLAKE gotoblas_HASWELL
  265. #endif
  266. #endif
  267. #else
  268. //Use NEHALEM kernels for sandy bridge
  269. #define gotoblas_SANDYBRIDGE gotoblas_NEHALEM
  270. #define gotoblas_HASWELL gotoblas_NEHALEM
  271. #define gotoblas_SKYLAKEX gotoblas_NEHALEM
  272. #define gotoblas_COOPERLAKE gotoblas_NEHALEM
  273. #define gotoblas_BULLDOZER gotoblas_BARCELONA
  274. #define gotoblas_PILEDRIVER gotoblas_BARCELONA
  275. #define gotoblas_STEAMROLLER gotoblas_BARCELONA
  276. #define gotoblas_EXCAVATOR gotoblas_BARCELONA
  277. #define gotoblas_ZEN gotoblas_BARCELONA
  278. #endif
  279. #endif // DYNAMIC_LIST
  280. #define VENDOR_INTEL 1
  281. #define VENDOR_AMD 2
  282. #define VENDOR_CENTAUR 3
  283. #define VENDOR_HYGON 4
  284. #define VENDOR_UNKNOWN 99
  285. #define BITMASK(a, b, c) ((((a) >> (b)) & (c)))
  286. #ifndef NO_AVX
  287. static inline void xgetbv(int op, int * eax, int * edx){
  288. //Use binary code for xgetbv
  289. __asm__ __volatile__
  290. (".byte 0x0f, 0x01, 0xd0": "=a" (*eax), "=d" (*edx) : "c" (op) : "cc");
  291. }
  292. #endif
  293. int support_avx(){
  294. #ifndef NO_AVX
  295. int eax, ebx, ecx, edx;
  296. int ret=0;
  297. cpuid(1, &eax, &ebx, &ecx, &edx);
  298. if ((ecx & (1 << 28)) != 0 && (ecx & (1 << 27)) != 0 && (ecx & (1 << 26)) != 0){
  299. xgetbv(0, &eax, &edx);
  300. if((eax & 6) == 6){
  301. ret=1; //OS support AVX
  302. }
  303. }
  304. return ret;
  305. #else
  306. return 0;
  307. #endif
  308. }
  309. int support_avx2(){
  310. #ifndef NO_AVX2
  311. int eax, ebx, ecx=0, edx;
  312. int ret=0;
  313. if (!support_avx())
  314. return 0;
  315. cpuid(7, &eax, &ebx, &ecx, &edx);
  316. if((ebx & (1<<5)) != 0)
  317. ret=1; //AVX2 flag is set
  318. return ret;
  319. #else
  320. return 0;
  321. #endif
  322. }
  323. int support_avx512(){
  324. #if !defined(NO_AVX) && !defined(NO_AVX512)
  325. int eax, ebx, ecx, edx;
  326. int ret=0;
  327. if (!support_avx())
  328. return 0;
  329. cpuid(7, &eax, &ebx, &ecx, &edx);
  330. if((ebx & (1<<5)) == 0){
  331. ret=0; //cpu does not have avx2 flag
  332. }
  333. if((ebx & (1<<31)) != 0){ //AVX512VL flag is set
  334. xgetbv(0, &eax, &edx);
  335. if((eax & 0xe0) == 0xe0)
  336. ret=1; //OS supports saving zmm register
  337. }
  338. return ret;
  339. #else
  340. return 0;
  341. #endif
  342. }
  343. int support_avx512_bf16(){
  344. #if !defined(NO_AVX) && !defined(NO_AVX512)
  345. int eax, ebx, ecx, edx;
  346. int ret=0;
  347. if (!support_avx512())
  348. return 0;
  349. cpuid_count(7, 1, &eax, &ebx, &ecx, &edx);
  350. if((eax & 32) == 32){
  351. ret=1; // CPUID.7.1:EAX[bit 5] indicates whether avx512_bf16 supported or not
  352. }
  353. return ret;
  354. #else
  355. return 0;
  356. #endif
  357. }
  358. extern void openblas_warning(int verbose, const char * msg);
  359. #define FALLBACK_VERBOSE 1
  360. #define NEHALEM_FALLBACK "OpenBLAS : Your OS does not support AVX instructions. OpenBLAS is using Nehalem kernels as a fallback, which may give poorer performance.\n"
  361. #define SANDYBRIDGE_FALLBACK "OpenBLAS : Your OS does not support AVX2 instructions. OpenBLAS is using Sandybridge kernels as a fallback, which may give poorer performance.\n"
  362. #define HASWELL_FALLBACK "OpenBLAS : Your OS does not support AVX512VL instructions. OpenBLAS is using Haswell kernels as a fallback, which may give poorer performance.\n"
  363. #define BARCELONA_FALLBACK "OpenBLAS : Your OS does not support AVX instructions. OpenBLAS is using Barcelona kernels as a fallback, which may give poorer performance.\n"
  364. static int get_vendor(void){
  365. int eax, ebx, ecx, edx;
  366. union
  367. {
  368. char vchar[16];
  369. int vint[4];
  370. } vendor;
  371. cpuid(0, &eax, &ebx, &ecx, &edx);
  372. *(&vendor.vint[0]) = ebx;
  373. *(&vendor.vint[1]) = edx;
  374. *(&vendor.vint[2]) = ecx;
  375. vendor.vchar[12] = '\0';
  376. if (!strcmp(vendor.vchar, "GenuineIntel")) return VENDOR_INTEL;
  377. if (!strcmp(vendor.vchar, "AuthenticAMD")) return VENDOR_AMD;
  378. if (!strcmp(vendor.vchar, "CentaurHauls")) return VENDOR_CENTAUR;
  379. if (!strcmp(vendor.vchar, "HygonGenuine")) return VENDOR_HYGON;
  380. if ((eax == 0) || ((eax & 0x500) != 0)) return VENDOR_INTEL;
  381. return VENDOR_UNKNOWN;
  382. }
  383. static gotoblas_t *get_coretype(void){
  384. int eax, ebx, ecx, edx;
  385. int family, exfamily, model, vendor, exmodel;
  386. cpuid(1, &eax, &ebx, &ecx, &edx);
  387. family = BITMASK(eax, 8, 0x0f);
  388. exfamily = BITMASK(eax, 20, 0xff);
  389. model = BITMASK(eax, 4, 0x0f);
  390. exmodel = BITMASK(eax, 16, 0x0f);
  391. vendor = get_vendor();
  392. if (vendor == VENDOR_INTEL){
  393. switch (family) {
  394. case 0x6:
  395. switch (exmodel) {
  396. case 0:
  397. if (model <= 0x7) return &gotoblas_KATMAI;
  398. if ((model == 0x8) || (model == 0xa) || (model == 0xb)) return &gotoblas_COPPERMINE;
  399. if ((model == 0x9) || (model == 0xd)) return &gotoblas_BANIAS;
  400. if (model == 14) return &gotoblas_BANIAS;
  401. if (model == 15) return &gotoblas_CORE2;
  402. return NULL;
  403. case 1:
  404. if (model == 6) return &gotoblas_CORE2;
  405. if (model == 7) return &gotoblas_PENRYN;
  406. if (model == 13) return &gotoblas_DUNNINGTON;
  407. if ((model == 10) || (model == 11) || (model == 14) || (model == 15)) return &gotoblas_NEHALEM;
  408. if (model == 12) return &gotoblas_ATOM;
  409. return NULL;
  410. case 2:
  411. //Intel Core (Clarkdale) / Core (Arrandale)
  412. // Pentium (Clarkdale) / Pentium Mobile (Arrandale)
  413. // Xeon (Clarkdale), 32nm
  414. if (model == 5) return &gotoblas_NEHALEM;
  415. //Intel Xeon Processor 5600 (Westmere-EP)
  416. //Xeon Processor E7 (Westmere-EX)
  417. //Xeon E7540
  418. if (model == 12 || model == 14 || model == 15) return &gotoblas_NEHALEM;
  419. //Intel Core i5-2000 /i7-2000 (Sandy Bridge)
  420. //Intel Core i7-3000 / Xeon E5
  421. if (model == 10 || model == 13) {
  422. if(support_avx())
  423. return &gotoblas_SANDYBRIDGE;
  424. else{
  425. openblas_warning(FALLBACK_VERBOSE, NEHALEM_FALLBACK);
  426. return &gotoblas_NEHALEM; //OS doesn't support AVX. Use old kernels.
  427. }
  428. }
  429. return NULL;
  430. case 3:
  431. //Intel Sandy Bridge 22nm (Ivy Bridge?)
  432. if (model == 10 || model == 14) {
  433. if(support_avx())
  434. return &gotoblas_SANDYBRIDGE;
  435. else{
  436. openblas_warning(FALLBACK_VERBOSE, NEHALEM_FALLBACK);
  437. return &gotoblas_NEHALEM; //OS doesn't support AVX. Use old kernels.
  438. }
  439. }
  440. //Intel Haswell
  441. if (model == 12 || model == 15) {
  442. if(support_avx2())
  443. return &gotoblas_HASWELL;
  444. if(support_avx()) {
  445. openblas_warning(FALLBACK_VERBOSE, SANDYBRIDGE_FALLBACK);
  446. return &gotoblas_SANDYBRIDGE;
  447. } else {
  448. openblas_warning(FALLBACK_VERBOSE, NEHALEM_FALLBACK);
  449. return &gotoblas_NEHALEM; //OS doesn't support AVX. Use old kernels.
  450. }
  451. }
  452. //Intel Broadwell
  453. if (model == 13) {
  454. if(support_avx2())
  455. return &gotoblas_HASWELL;
  456. if(support_avx()) {
  457. openblas_warning(FALLBACK_VERBOSE, SANDYBRIDGE_FALLBACK);
  458. return &gotoblas_SANDYBRIDGE;
  459. } else {
  460. openblas_warning(FALLBACK_VERBOSE, NEHALEM_FALLBACK);
  461. return &gotoblas_NEHALEM; //OS doesn't support AVX. Use old kernels.
  462. }
  463. }
  464. if (model == 7) return &gotoblas_ATOM; //Bay Trail
  465. return NULL;
  466. case 4:
  467. //Intel Haswell
  468. if (model == 5 || model == 6) {
  469. if(support_avx2())
  470. return &gotoblas_HASWELL;
  471. if(support_avx()) {
  472. openblas_warning(FALLBACK_VERBOSE, SANDYBRIDGE_FALLBACK);
  473. return &gotoblas_SANDYBRIDGE;
  474. } else {
  475. openblas_warning(FALLBACK_VERBOSE, NEHALEM_FALLBACK);
  476. return &gotoblas_NEHALEM; //OS doesn't support AVX. Use old kernels.
  477. }
  478. }
  479. //Intel Broadwell
  480. if (model == 7 || model == 15) {
  481. if(support_avx2())
  482. return &gotoblas_HASWELL;
  483. if(support_avx()) {
  484. openblas_warning(FALLBACK_VERBOSE, SANDYBRIDGE_FALLBACK);
  485. return &gotoblas_SANDYBRIDGE;
  486. } else {
  487. openblas_warning(FALLBACK_VERBOSE, NEHALEM_FALLBACK);
  488. return &gotoblas_NEHALEM; //OS doesn't support AVX. Use old kernels.
  489. }
  490. }
  491. //Intel Skylake
  492. if (model == 14) {
  493. if(support_avx2())
  494. return &gotoblas_HASWELL;
  495. if(support_avx()) {
  496. openblas_warning(FALLBACK_VERBOSE, SANDYBRIDGE_FALLBACK);
  497. return &gotoblas_SANDYBRIDGE;
  498. } else {
  499. openblas_warning(FALLBACK_VERBOSE, NEHALEM_FALLBACK);
  500. return &gotoblas_NEHALEM; //OS doesn't support AVX. Use old kernels.
  501. }
  502. }
  503. //Intel Braswell / Avoton
  504. if (model == 12 || model == 13) {
  505. return &gotoblas_NEHALEM;
  506. }
  507. return NULL;
  508. case 5:
  509. //Intel Broadwell
  510. if (model == 6) {
  511. if(support_avx2())
  512. return &gotoblas_HASWELL;
  513. if(support_avx()) {
  514. openblas_warning(FALLBACK_VERBOSE, SANDYBRIDGE_FALLBACK);
  515. return &gotoblas_SANDYBRIDGE;
  516. } else {
  517. openblas_warning(FALLBACK_VERBOSE, NEHALEM_FALLBACK);
  518. return &gotoblas_NEHALEM; //OS doesn't support AVX. Use old kernels.
  519. }
  520. }
  521. if (model == 5) {
  522. // Intel Cooperlake
  523. if(support_avx512_bf16())
  524. return &gotoblas_COOPERLAKE;
  525. // Intel Skylake X
  526. if (support_avx512())
  527. return &gotoblas_SKYLAKEX;
  528. if(support_avx2()){
  529. openblas_warning(FALLBACK_VERBOSE, HASWELL_FALLBACK);
  530. return &gotoblas_HASWELL;
  531. }
  532. if(support_avx()) {
  533. openblas_warning(FALLBACK_VERBOSE, SANDYBRIDGE_FALLBACK);
  534. return &gotoblas_SANDYBRIDGE;
  535. } else {
  536. openblas_warning(FALLBACK_VERBOSE, NEHALEM_FALLBACK);
  537. return &gotoblas_NEHALEM;
  538. }
  539. }
  540. //Intel Skylake
  541. if (model == 14) {
  542. if(support_avx2())
  543. return &gotoblas_HASWELL;
  544. if(support_avx()) {
  545. openblas_warning(FALLBACK_VERBOSE, SANDYBRIDGE_FALLBACK);
  546. return &gotoblas_SANDYBRIDGE;
  547. } else {
  548. openblas_warning(FALLBACK_VERBOSE, NEHALEM_FALLBACK);
  549. return &gotoblas_NEHALEM; //OS doesn't support AVX. Use old kernels.
  550. }
  551. }
  552. //Intel Phi Knights Landing
  553. if (model == 7) {
  554. if(support_avx2()){
  555. openblas_warning(FALLBACK_VERBOSE, HASWELL_FALLBACK);
  556. return &gotoblas_HASWELL;
  557. }
  558. if(support_avx()) {
  559. openblas_warning(FALLBACK_VERBOSE, SANDYBRIDGE_FALLBACK);
  560. return &gotoblas_SANDYBRIDGE;
  561. } else {
  562. openblas_warning(FALLBACK_VERBOSE, NEHALEM_FALLBACK);
  563. return &gotoblas_NEHALEM; //OS doesn't support AVX. Use old kernels.
  564. }
  565. }
  566. //Apollo Lake or Denverton
  567. if (model == 12 || model == 15) {
  568. return &gotoblas_NEHALEM;
  569. }
  570. return NULL;
  571. case 6:
  572. if (model == 6) {
  573. // Cannon Lake
  574. if(support_avx2())
  575. return &gotoblas_HASWELL;
  576. if(support_avx()) {
  577. openblas_warning(FALLBACK_VERBOSE, SANDYBRIDGE_FALLBACK);
  578. return &gotoblas_SANDYBRIDGE;
  579. } else {
  580. openblas_warning(FALLBACK_VERBOSE, NEHALEM_FALLBACK);
  581. return &gotoblas_NEHALEM;
  582. }
  583. }
  584. if (model == 10) {
  585. // Ice Lake SP
  586. if(support_avx512_bf16())
  587. return &gotoblas_COOPERLAKE;
  588. if (support_avx512())
  589. return &gotoblas_SKYLAKEX;
  590. if(support_avx2())
  591. return &gotoblas_HASWELL;
  592. if(support_avx()) {
  593. openblas_warning(FALLBACK_VERBOSE, SANDYBRIDGE_FALLBACK);
  594. return &gotoblas_SANDYBRIDGE;
  595. } else {
  596. openblas_warning(FALLBACK_VERBOSE, NEHALEM_FALLBACK);
  597. return &gotoblas_NEHALEM;
  598. }
  599. }
  600. return NULL;
  601. case 7:
  602. if (model == 10) // Goldmont Plus
  603. return &gotoblas_NEHALEM;
  604. if (model == 14) {
  605. // Ice Lake
  606. if (support_avx512())
  607. return &gotoblas_SKYLAKEX;
  608. if(support_avx2()){
  609. openblas_warning(FALLBACK_VERBOSE, HASWELL_FALLBACK);
  610. return &gotoblas_HASWELL;
  611. }
  612. if(support_avx()) {
  613. openblas_warning(FALLBACK_VERBOSE, SANDYBRIDGE_FALLBACK);
  614. return &gotoblas_SANDYBRIDGE;
  615. } else {
  616. openblas_warning(FALLBACK_VERBOSE, NEHALEM_FALLBACK);
  617. return &gotoblas_NEHALEM;
  618. }
  619. }
  620. return NULL;
  621. case 9:
  622. case 8:
  623. if (model == 12) { // Tiger Lake
  624. if (support_avx512())
  625. return &gotoblas_SKYLAKEX;
  626. if(support_avx2()){
  627. openblas_warning(FALLBACK_VERBOSE, HASWELL_FALLBACK);
  628. return &gotoblas_HASWELL;
  629. }
  630. if(support_avx()) {
  631. openblas_warning(FALLBACK_VERBOSE, SANDYBRIDGE_FALLBACK);
  632. return &gotoblas_SANDYBRIDGE;
  633. } else {
  634. openblas_warning(FALLBACK_VERBOSE, NEHALEM_FALLBACK);
  635. return &gotoblas_NEHALEM;
  636. }
  637. }
  638. if (model == 14 ) { // Kaby Lake, Coffee Lake
  639. if(support_avx2())
  640. return &gotoblas_HASWELL;
  641. if(support_avx()) {
  642. openblas_warning(FALLBACK_VERBOSE, SANDYBRIDGE_FALLBACK);
  643. return &gotoblas_SANDYBRIDGE;
  644. } else {
  645. openblas_warning(FALLBACK_VERBOSE, NEHALEM_FALLBACK);
  646. return &gotoblas_NEHALEM; //OS doesn't support AVX. Use old kernels.
  647. }
  648. }
  649. case 10:
  650. if (model == 5 || model == 6) {
  651. if(support_avx2())
  652. return &gotoblas_HASWELL;
  653. if(support_avx()) {
  654. openblas_warning(FALLBACK_VERBOSE, SANDYBRIDGE_FALLBACK);
  655. return &gotoblas_SANDYBRIDGE;
  656. } else {
  657. openblas_warning(FALLBACK_VERBOSE, NEHALEM_FALLBACK);
  658. return &gotoblas_NEHALEM; //OS doesn't support AVX. Use old kernels.
  659. }
  660. }
  661. if (model == 7) {
  662. if (support_avx512())
  663. return &gotoblas_SKYLAKEX;
  664. if(support_avx2())
  665. return &gotoblas_HASWELL;
  666. if(support_avx()) {
  667. openblas_warning(FALLBACK_VERBOSE, SANDYBRIDGE_FALLBACK);
  668. return &gotoblas_SANDYBRIDGE;
  669. } else {
  670. openblas_warning(FALLBACK_VERBOSE, NEHALEM_FALLBACK);
  671. return &gotoblas_NEHALEM; //OS doesn't support AVX. Use old kernels.
  672. }
  673. }
  674. return NULL;
  675. }
  676. case 0xf:
  677. if (model <= 0x2) return &gotoblas_NORTHWOOD;
  678. return &gotoblas_PRESCOTT;
  679. }
  680. }
  681. if (vendor == VENDOR_AMD || vendor == VENDOR_HYGON){
  682. if (family <= 0xe) {
  683. // Verify that CPU has 3dnow and 3dnowext before claiming it is Athlon
  684. cpuid(0x80000000, &eax, &ebx, &ecx, &edx);
  685. if ( (eax & 0xffff) >= 0x01) {
  686. cpuid(0x80000001, &eax, &ebx, &ecx, &edx);
  687. if ((edx & (1 << 30)) == 0 || (edx & (1u << 31)) == 0)
  688. return NULL;
  689. }
  690. else
  691. return NULL;
  692. return &gotoblas_ATHLON;
  693. }
  694. if (family == 0xf){
  695. if ((exfamily == 0) || (exfamily == 2)) {
  696. if (ecx & (1 << 0)) return &gotoblas_OPTERON_SSE3;
  697. else return &gotoblas_OPTERON;
  698. } else if (exfamily == 5 || exfamily == 7) {
  699. return &gotoblas_BOBCAT;
  700. } else if (exfamily == 6) {
  701. if(model == 1){
  702. //AMD Bulldozer Opteron 6200 / Opteron 4200 / AMD FX-Series
  703. if(support_avx())
  704. return &gotoblas_BULLDOZER;
  705. else{
  706. openblas_warning(FALLBACK_VERBOSE, BARCELONA_FALLBACK);
  707. return &gotoblas_BARCELONA; //OS doesn't support AVX. Use old kernels.
  708. }
  709. }else if(model == 2 || model == 3){
  710. //AMD Bulldozer Opteron 6300 / Opteron 4300 / Opteron 3300
  711. if(support_avx())
  712. return &gotoblas_PILEDRIVER;
  713. else{
  714. openblas_warning(FALLBACK_VERBOSE, BARCELONA_FALLBACK);
  715. return &gotoblas_BARCELONA; //OS doesn't support AVX. Use old kernels.
  716. }
  717. }else if(model == 5){
  718. if(support_avx())
  719. return &gotoblas_EXCAVATOR;
  720. else{
  721. openblas_warning(FALLBACK_VERBOSE, BARCELONA_FALLBACK);
  722. return &gotoblas_BARCELONA; //OS doesn't support AVX. Use old kernels.
  723. }
  724. }else if(model == 0 || model == 8){
  725. if (exmodel == 1) {
  726. //AMD Trinity
  727. if(support_avx())
  728. return &gotoblas_PILEDRIVER;
  729. else{
  730. openblas_warning(FALLBACK_VERBOSE, BARCELONA_FALLBACK);
  731. return &gotoblas_BARCELONA; //OS doesn't support AVX. Use old kernels.
  732. }
  733. }else if (exmodel == 3) {
  734. //AMD STEAMROLLER
  735. if(support_avx())
  736. return &gotoblas_STEAMROLLER;
  737. else{
  738. openblas_warning(FALLBACK_VERBOSE, BARCELONA_FALLBACK);
  739. return &gotoblas_BARCELONA; //OS doesn't support AVX. Use old kernels.
  740. }
  741. }else if (exmodel == 6) {
  742. if(support_avx())
  743. return &gotoblas_EXCAVATOR;
  744. else{
  745. openblas_warning(FALLBACK_VERBOSE, BARCELONA_FALLBACK);
  746. return &gotoblas_BARCELONA; //OS doesn't support AVX. Use old kernels.
  747. }
  748. }
  749. }
  750. } else if (exfamily == 8) {
  751. /* if (model == 1 || model == 8) */ {
  752. if(support_avx())
  753. return &gotoblas_ZEN;
  754. else{
  755. openblas_warning(FALLBACK_VERBOSE, BARCELONA_FALLBACK);
  756. return &gotoblas_BARCELONA; //OS doesn't support AVX. Use old kernels.
  757. }
  758. }
  759. } else if (exfamily == 9) {
  760. if(support_avx())
  761. return &gotoblas_ZEN;
  762. else{
  763. openblas_warning(FALLBACK_VERBOSE, BARCELONA_FALLBACK);
  764. return &gotoblas_BARCELONA; //OS doesn't support AVX. Use old kernels.
  765. }
  766. } else if (exfamily == 10) {
  767. if(support_avx())
  768. return &gotoblas_ZEN;
  769. else{
  770. openblas_warning(FALLBACK_VERBOSE, BARCELONA_FALLBACK);
  771. return &gotoblas_BARCELONA; //OS doesn't support AVX. Use old kernels.
  772. }
  773. }else {
  774. return &gotoblas_BARCELONA;
  775. }
  776. }
  777. }
  778. if (vendor == VENDOR_CENTAUR) {
  779. switch (family) {
  780. case 0x6:
  781. return &gotoblas_NANO;
  782. }
  783. }
  784. return NULL;
  785. }
  786. static char *corename[] = {
  787. "Unknown",
  788. "Katmai",
  789. "Coppermine",
  790. "Northwood",
  791. "Prescott",
  792. "Banias",
  793. "Atom",
  794. "Core2",
  795. "Penryn",
  796. "Dunnington",
  797. "Nehalem",
  798. "Athlon",
  799. "Opteron",
  800. "Opteron_SSE3",
  801. "Barcelona",
  802. "Nano",
  803. "Sandybridge",
  804. "Bobcat",
  805. "Bulldozer",
  806. "Piledriver",
  807. "Haswell",
  808. "Steamroller",
  809. "Excavator",
  810. "Zen",
  811. "SkylakeX",
  812. "Cooperlake"
  813. };
  814. char *gotoblas_corename(void) {
  815. if (gotoblas == &gotoblas_KATMAI) return corename[ 1];
  816. if (gotoblas == &gotoblas_COPPERMINE) return corename[ 2];
  817. if (gotoblas == &gotoblas_NORTHWOOD) return corename[ 3];
  818. if (gotoblas == &gotoblas_PRESCOTT) return corename[ 4];
  819. if (gotoblas == &gotoblas_BANIAS) return corename[ 5];
  820. if (gotoblas == &gotoblas_ATOM)
  821. #ifdef DYNAMIC_OLDER
  822. return corename[ 6];
  823. #else
  824. return corename[10];
  825. #endif
  826. if (gotoblas == &gotoblas_CORE2) return corename[ 7];
  827. if (gotoblas == &gotoblas_PENRYN)
  828. #ifdef DYNAMIC_OLDER
  829. return corename[ 8];
  830. #else
  831. return corename[7];
  832. #endif
  833. if (gotoblas == &gotoblas_DUNNINGTON)
  834. #ifdef DYNAMIC_OLDER
  835. return corename[ 9];
  836. #else
  837. return corename[7];
  838. #endif
  839. if (gotoblas == &gotoblas_NEHALEM) return corename[10];
  840. if (gotoblas == &gotoblas_ATHLON) return corename[11];
  841. if (gotoblas == &gotoblas_OPTERON_SSE3)
  842. #ifdef DYNAMIC_OLDER
  843. return corename[12];
  844. #else
  845. return corename[7];
  846. #endif
  847. if (gotoblas == &gotoblas_OPTERON)
  848. #ifdef DYNAMIC_OLDER
  849. return corename[13];
  850. #else
  851. return corename[7];
  852. #endif
  853. if (gotoblas == &gotoblas_BARCELONA) return corename[14];
  854. if (gotoblas == &gotoblas_NANO)
  855. #ifdef DYNAMIC_OLDER
  856. return corename[15];
  857. #else
  858. return corename[10];
  859. #endif
  860. if (gotoblas == &gotoblas_SANDYBRIDGE) return corename[16];
  861. if (gotoblas == &gotoblas_BOBCAT)
  862. #ifdef DYNAMIC_OLDER
  863. return corename[17];
  864. #else
  865. return corename[7];
  866. #endif
  867. if (gotoblas == &gotoblas_BULLDOZER) return corename[18];
  868. if (gotoblas == &gotoblas_PILEDRIVER) return corename[19];
  869. if (gotoblas == &gotoblas_HASWELL) return corename[20];
  870. if (gotoblas == &gotoblas_STEAMROLLER) return corename[21];
  871. if (gotoblas == &gotoblas_EXCAVATOR) return corename[22];
  872. if (gotoblas == &gotoblas_ZEN) return corename[23];
  873. if (gotoblas == &gotoblas_SKYLAKEX) return corename[24];
  874. if (gotoblas == &gotoblas_COOPERLAKE) return corename[25];
  875. return corename[0];
  876. }
  877. static gotoblas_t *force_coretype(char *coretype){
  878. int i ;
  879. int found = -1;
  880. char message[128];
  881. //char mname[20];
  882. for ( i=1 ; i <= 24; i++)
  883. {
  884. if (!strncasecmp(coretype,corename[i],20))
  885. {
  886. found = i;
  887. break;
  888. }
  889. }
  890. if (found < 0)
  891. {
  892. //strncpy(mname,coretype,20);
  893. snprintf(message, 128, "Core not found: %s\n",coretype);
  894. openblas_warning(1, message);
  895. return(NULL);
  896. }
  897. switch (found)
  898. {
  899. case 25: return (&gotoblas_COOPERLAKE);
  900. case 24: return (&gotoblas_SKYLAKEX);
  901. case 23: return (&gotoblas_ZEN);
  902. case 22: return (&gotoblas_EXCAVATOR);
  903. case 21: return (&gotoblas_STEAMROLLER);
  904. case 20: return (&gotoblas_HASWELL);
  905. case 19: return (&gotoblas_PILEDRIVER);
  906. case 18: return (&gotoblas_BULLDOZER);
  907. case 17: return (&gotoblas_BOBCAT);
  908. case 16: return (&gotoblas_SANDYBRIDGE);
  909. case 15: return (&gotoblas_NANO);
  910. case 14: return (&gotoblas_BARCELONA);
  911. case 13: return (&gotoblas_OPTERON);
  912. case 12: return (&gotoblas_OPTERON_SSE3);
  913. case 11: return (&gotoblas_ATHLON);
  914. case 10: return (&gotoblas_NEHALEM);
  915. case 9: return (&gotoblas_DUNNINGTON);
  916. case 8: return (&gotoblas_PENRYN);
  917. case 7: return (&gotoblas_CORE2);
  918. case 6: return (&gotoblas_ATOM);
  919. case 5: return (&gotoblas_BANIAS);
  920. case 4: return (&gotoblas_PRESCOTT);
  921. case 3: return (&gotoblas_NORTHWOOD);
  922. case 2: return (&gotoblas_COPPERMINE);
  923. case 1: return (&gotoblas_KATMAI);
  924. }
  925. return(NULL);
  926. }
  927. void gotoblas_dynamic_init(void) {
  928. char coremsg[128];
  929. char coren[22];
  930. char *p;
  931. if (gotoblas) return;
  932. p = getenv("OPENBLAS_CORETYPE");
  933. if ( p )
  934. {
  935. gotoblas = force_coretype(p);
  936. }
  937. else
  938. {
  939. gotoblas = get_coretype();
  940. }
  941. #ifdef ARCH_X86
  942. if (gotoblas == NULL) gotoblas = &gotoblas_KATMAI;
  943. #else
  944. if (gotoblas == NULL) gotoblas = &gotoblas_PRESCOTT;
  945. /* sanity check, if 64bit pointer we can't have a 32 bit cpu */
  946. if (sizeof(void*) == 8) {
  947. if (gotoblas == &gotoblas_KATMAI ||
  948. gotoblas == &gotoblas_COPPERMINE ||
  949. gotoblas == &gotoblas_NORTHWOOD ||
  950. gotoblas == &gotoblas_BANIAS ||
  951. gotoblas == &gotoblas_ATHLON)
  952. gotoblas = &gotoblas_PRESCOTT;
  953. }
  954. #endif
  955. if (gotoblas && gotoblas -> init) {
  956. strncpy(coren,gotoblas_corename(),20);
  957. sprintf(coremsg, "Core: %s\n",coren);
  958. openblas_warning(2, coremsg);
  959. gotoblas -> init();
  960. } else {
  961. openblas_warning(0, "OpenBLAS : Architecture Initialization failed. No initialization function found.\n");
  962. exit(1);
  963. }
  964. }
  965. void gotoblas_dynamic_quit(void) {
  966. gotoblas = NULL;
  967. }