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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897
  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. #else // not DYNAMIC_LIST
  206. EXTERN gotoblas_t gotoblas_KATMAI;
  207. EXTERN gotoblas_t gotoblas_COPPERMINE;
  208. EXTERN gotoblas_t gotoblas_NORTHWOOD;
  209. EXTERN gotoblas_t gotoblas_BANIAS;
  210. EXTERN gotoblas_t gotoblas_ATHLON;
  211. extern gotoblas_t gotoblas_PRESCOTT;
  212. extern gotoblas_t gotoblas_CORE2;
  213. extern gotoblas_t gotoblas_NEHALEM;
  214. extern gotoblas_t gotoblas_BARCELONA;
  215. #ifdef DYNAMIC_OLDER
  216. extern gotoblas_t gotoblas_ATOM;
  217. extern gotoblas_t gotoblas_NANO;
  218. extern gotoblas_t gotoblas_PENRYN;
  219. extern gotoblas_t gotoblas_DUNNINGTON;
  220. extern gotoblas_t gotoblas_OPTERON;
  221. extern gotoblas_t gotoblas_OPTERON_SSE3;
  222. extern gotoblas_t gotoblas_BOBCAT;
  223. #else
  224. #define gotoblas_ATOM gotoblas_NEHALEM
  225. #define gotoblas_NANO gotoblas_NEHALEM
  226. #define gotoblas_PENRYN gotoblas_CORE2
  227. #define gotoblas_DUNNINGTON gotoblas_CORE2
  228. #define gotoblas_OPTERON gotoblas_CORE2
  229. #define gotoblas_OPTERON_SSE3 gotoblas_CORE2
  230. #define gotoblas_BOBCAT gotoblas_CORE2
  231. #endif
  232. #ifndef NO_AVX
  233. extern gotoblas_t gotoblas_SANDYBRIDGE;
  234. extern gotoblas_t gotoblas_BULLDOZER;
  235. extern gotoblas_t gotoblas_PILEDRIVER;
  236. extern gotoblas_t gotoblas_STEAMROLLER;
  237. extern gotoblas_t gotoblas_EXCAVATOR;
  238. #ifdef NO_AVX2
  239. #define gotoblas_HASWELL gotoblas_SANDYBRIDGE
  240. #define gotoblas_SKYLAKEX gotoblas_SANDYBRIDGE
  241. #define gotoblas_ZEN gotoblas_SANDYBRIDGE
  242. #else
  243. extern gotoblas_t gotoblas_HASWELL;
  244. extern gotoblas_t gotoblas_ZEN;
  245. #ifndef NO_AVX512
  246. extern gotoblas_t gotoblas_SKYLAKEX;
  247. #else
  248. #define gotoblas_SKYLAKEX gotoblas_HASWELL
  249. #endif
  250. #endif
  251. #else
  252. //Use NEHALEM kernels for sandy bridge
  253. #define gotoblas_SANDYBRIDGE gotoblas_NEHALEM
  254. #define gotoblas_HASWELL gotoblas_NEHALEM
  255. #define gotoblas_SKYLAKEX gotoblas_NEHALEM
  256. #define gotoblas_BULLDOZER gotoblas_BARCELONA
  257. #define gotoblas_PILEDRIVER gotoblas_BARCELONA
  258. #define gotoblas_STEAMROLLER gotoblas_BARCELONA
  259. #define gotoblas_EXCAVATOR gotoblas_BARCELONA
  260. #define gotoblas_ZEN gotoblas_BARCELONA
  261. #endif
  262. #endif // DYNAMIC_LIST
  263. #define VENDOR_INTEL 1
  264. #define VENDOR_AMD 2
  265. #define VENDOR_CENTAUR 3
  266. #define VENDOR_HYGON 4
  267. #define VENDOR_UNKNOWN 99
  268. #define BITMASK(a, b, c) ((((a) >> (b)) & (c)))
  269. #ifndef NO_AVX
  270. static inline void xgetbv(int op, int * eax, int * edx){
  271. //Use binary code for xgetbv
  272. __asm__ __volatile__
  273. (".byte 0x0f, 0x01, 0xd0": "=a" (*eax), "=d" (*edx) : "c" (op) : "cc");
  274. }
  275. #endif
  276. int support_avx(){
  277. #ifndef NO_AVX
  278. int eax, ebx, ecx, edx;
  279. int ret=0;
  280. cpuid(1, &eax, &ebx, &ecx, &edx);
  281. if ((ecx & (1 << 28)) != 0 && (ecx & (1 << 27)) != 0 && (ecx & (1 << 26)) != 0){
  282. xgetbv(0, &eax, &edx);
  283. if((eax & 6) == 6){
  284. ret=1; //OS support AVX
  285. }
  286. }
  287. return ret;
  288. #else
  289. return 0;
  290. #endif
  291. }
  292. int support_avx2(){
  293. #ifndef NO_AVX2
  294. int eax, ebx, ecx=0, edx;
  295. int ret=0;
  296. if (!support_avx())
  297. return 0;
  298. cpuid(7, &eax, &ebx, &ecx, &edx);
  299. if((ebx & (1<<7)) != 0)
  300. ret=1; //OS supports AVX2
  301. return ret;
  302. #else
  303. return 0;
  304. #endif
  305. }
  306. int support_avx512(){
  307. #if !defined(NO_AVX) && !defined(NO_AVX512)
  308. int eax, ebx, ecx, edx;
  309. int ret=0;
  310. if (!support_avx())
  311. return 0;
  312. cpuid(7, &eax, &ebx, &ecx, &edx);
  313. if((ebx & (1<<7)) != 1){
  314. ret=0; //OS does not even support AVX2
  315. }
  316. if((ebx & (1<<31)) != 0){
  317. xgetbv(0, &eax, &edx);
  318. if((eax & 0xe0) == 0xe0)
  319. ret=1; //OS supports AVX512VL
  320. }
  321. return ret;
  322. #else
  323. return 0;
  324. #endif
  325. }
  326. extern void openblas_warning(int verbose, const char * msg);
  327. #define FALLBACK_VERBOSE 1
  328. #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"
  329. #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"
  330. #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"
  331. #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"
  332. static int get_vendor(void){
  333. int eax, ebx, ecx, edx;
  334. union
  335. {
  336. char vchar[16];
  337. int vint[4];
  338. } vendor;
  339. cpuid(0, &eax, &ebx, &ecx, &edx);
  340. *(&vendor.vint[0]) = ebx;
  341. *(&vendor.vint[1]) = edx;
  342. *(&vendor.vint[2]) = ecx;
  343. vendor.vchar[12] = '\0';
  344. if (!strcmp(vendor.vchar, "GenuineIntel")) return VENDOR_INTEL;
  345. if (!strcmp(vendor.vchar, "AuthenticAMD")) return VENDOR_AMD;
  346. if (!strcmp(vendor.vchar, "CentaurHauls")) return VENDOR_CENTAUR;
  347. if (!strcmp(vendor.vchar, "HygonGenuine")) return VENDOR_HYGON;
  348. if ((eax == 0) || ((eax & 0x500) != 0)) return VENDOR_INTEL;
  349. return VENDOR_UNKNOWN;
  350. }
  351. static gotoblas_t *get_coretype(void){
  352. int eax, ebx, ecx, edx;
  353. int family, exfamily, model, vendor, exmodel;
  354. cpuid(1, &eax, &ebx, &ecx, &edx);
  355. family = BITMASK(eax, 8, 0x0f);
  356. exfamily = BITMASK(eax, 20, 0xff);
  357. model = BITMASK(eax, 4, 0x0f);
  358. exmodel = BITMASK(eax, 16, 0x0f);
  359. vendor = get_vendor();
  360. if (vendor == VENDOR_INTEL){
  361. switch (family) {
  362. case 0x6:
  363. switch (exmodel) {
  364. case 0:
  365. if (model <= 0x7) return &gotoblas_KATMAI;
  366. if ((model == 0x8) || (model == 0xa) || (model == 0xb)) return &gotoblas_COPPERMINE;
  367. if ((model == 0x9) || (model == 0xd)) return &gotoblas_BANIAS;
  368. if (model == 14) return &gotoblas_BANIAS;
  369. if (model == 15) return &gotoblas_CORE2;
  370. return NULL;
  371. case 1:
  372. if (model == 6) return &gotoblas_CORE2;
  373. if (model == 7) return &gotoblas_PENRYN;
  374. if (model == 13) return &gotoblas_DUNNINGTON;
  375. if ((model == 10) || (model == 11) || (model == 14) || (model == 15)) return &gotoblas_NEHALEM;
  376. if (model == 12) return &gotoblas_ATOM;
  377. return NULL;
  378. case 2:
  379. //Intel Core (Clarkdale) / Core (Arrandale)
  380. // Pentium (Clarkdale) / Pentium Mobile (Arrandale)
  381. // Xeon (Clarkdale), 32nm
  382. if (model == 5) return &gotoblas_NEHALEM;
  383. //Intel Xeon Processor 5600 (Westmere-EP)
  384. //Xeon Processor E7 (Westmere-EX)
  385. //Xeon E7540
  386. if (model == 12 || model == 14 || model == 15) return &gotoblas_NEHALEM;
  387. //Intel Core i5-2000 /i7-2000 (Sandy Bridge)
  388. //Intel Core i7-3000 / Xeon E5
  389. if (model == 10 || model == 13) {
  390. if(support_avx())
  391. return &gotoblas_SANDYBRIDGE;
  392. else{
  393. openblas_warning(FALLBACK_VERBOSE, NEHALEM_FALLBACK);
  394. return &gotoblas_NEHALEM; //OS doesn't support AVX. Use old kernels.
  395. }
  396. }
  397. return NULL;
  398. case 3:
  399. //Intel Sandy Bridge 22nm (Ivy Bridge?)
  400. if (model == 10 || model == 14) {
  401. if(support_avx())
  402. return &gotoblas_SANDYBRIDGE;
  403. else{
  404. openblas_warning(FALLBACK_VERBOSE, NEHALEM_FALLBACK);
  405. return &gotoblas_NEHALEM; //OS doesn't support AVX. Use old kernels.
  406. }
  407. }
  408. //Intel Haswell
  409. if (model == 12 || model == 15) {
  410. if(support_avx2())
  411. return &gotoblas_HASWELL;
  412. if(support_avx()) {
  413. openblas_warning(FALLBACK_VERBOSE, SANDYBRIDGE_FALLBACK);
  414. return &gotoblas_SANDYBRIDGE;
  415. } else {
  416. openblas_warning(FALLBACK_VERBOSE, NEHALEM_FALLBACK);
  417. return &gotoblas_NEHALEM; //OS doesn't support AVX. Use old kernels.
  418. }
  419. }
  420. //Intel Broadwell
  421. if (model == 13) {
  422. if(support_avx2())
  423. return &gotoblas_HASWELL;
  424. if(support_avx()) {
  425. openblas_warning(FALLBACK_VERBOSE, SANDYBRIDGE_FALLBACK);
  426. return &gotoblas_SANDYBRIDGE;
  427. } else {
  428. openblas_warning(FALLBACK_VERBOSE, NEHALEM_FALLBACK);
  429. return &gotoblas_NEHALEM; //OS doesn't support AVX. Use old kernels.
  430. }
  431. }
  432. if (model == 7) return &gotoblas_ATOM; //Bay Trail
  433. return NULL;
  434. case 4:
  435. //Intel Haswell
  436. if (model == 5 || model == 6) {
  437. if(support_avx2())
  438. return &gotoblas_HASWELL;
  439. if(support_avx()) {
  440. openblas_warning(FALLBACK_VERBOSE, SANDYBRIDGE_FALLBACK);
  441. return &gotoblas_SANDYBRIDGE;
  442. } else {
  443. openblas_warning(FALLBACK_VERBOSE, NEHALEM_FALLBACK);
  444. return &gotoblas_NEHALEM; //OS doesn't support AVX. Use old kernels.
  445. }
  446. }
  447. //Intel Broadwell
  448. if (model == 7 || model == 15) {
  449. if(support_avx2())
  450. return &gotoblas_HASWELL;
  451. if(support_avx()) {
  452. openblas_warning(FALLBACK_VERBOSE, SANDYBRIDGE_FALLBACK);
  453. return &gotoblas_SANDYBRIDGE;
  454. } else {
  455. openblas_warning(FALLBACK_VERBOSE, NEHALEM_FALLBACK);
  456. return &gotoblas_NEHALEM; //OS doesn't support AVX. Use old kernels.
  457. }
  458. }
  459. //Intel Skylake
  460. if (model == 14) {
  461. if(support_avx2())
  462. return &gotoblas_HASWELL;
  463. if(support_avx()) {
  464. openblas_warning(FALLBACK_VERBOSE, SANDYBRIDGE_FALLBACK);
  465. return &gotoblas_SANDYBRIDGE;
  466. } else {
  467. openblas_warning(FALLBACK_VERBOSE, NEHALEM_FALLBACK);
  468. return &gotoblas_NEHALEM; //OS doesn't support AVX. Use old kernels.
  469. }
  470. }
  471. //Intel Braswell / Avoton
  472. if (model == 12 || model == 13) {
  473. return &gotoblas_NEHALEM;
  474. }
  475. return NULL;
  476. case 5:
  477. //Intel Broadwell
  478. if (model == 6) {
  479. if(support_avx2())
  480. return &gotoblas_HASWELL;
  481. if(support_avx()) {
  482. openblas_warning(FALLBACK_VERBOSE, SANDYBRIDGE_FALLBACK);
  483. return &gotoblas_SANDYBRIDGE;
  484. } else {
  485. openblas_warning(FALLBACK_VERBOSE, NEHALEM_FALLBACK);
  486. return &gotoblas_NEHALEM; //OS doesn't support AVX. Use old kernels.
  487. }
  488. }
  489. if (model == 5) {
  490. // Intel Skylake X
  491. if (support_avx512())
  492. return &gotoblas_SKYLAKEX;
  493. if(support_avx2()){
  494. openblas_warning(FALLBACK_VERBOSE, HASWELL_FALLBACK);
  495. return &gotoblas_HASWELL;
  496. }
  497. if(support_avx()) {
  498. openblas_warning(FALLBACK_VERBOSE, SANDYBRIDGE_FALLBACK);
  499. return &gotoblas_SANDYBRIDGE;
  500. } else {
  501. openblas_warning(FALLBACK_VERBOSE, NEHALEM_FALLBACK);
  502. return &gotoblas_NEHALEM;
  503. }
  504. }
  505. //Intel Skylake
  506. if (model == 14) {
  507. if(support_avx2())
  508. return &gotoblas_HASWELL;
  509. if(support_avx()) {
  510. openblas_warning(FALLBACK_VERBOSE, SANDYBRIDGE_FALLBACK);
  511. return &gotoblas_SANDYBRIDGE;
  512. } else {
  513. openblas_warning(FALLBACK_VERBOSE, NEHALEM_FALLBACK);
  514. return &gotoblas_NEHALEM; //OS doesn't support AVX. Use old kernels.
  515. }
  516. }
  517. //Intel Phi Knights Landing
  518. if (model == 7) {
  519. if(support_avx2()){
  520. openblas_warning(FALLBACK_VERBOSE, HASWELL_FALLBACK);
  521. return &gotoblas_HASWELL;
  522. }
  523. if(support_avx()) {
  524. openblas_warning(FALLBACK_VERBOSE, SANDYBRIDGE_FALLBACK);
  525. return &gotoblas_SANDYBRIDGE;
  526. } else {
  527. openblas_warning(FALLBACK_VERBOSE, NEHALEM_FALLBACK);
  528. return &gotoblas_NEHALEM; //OS doesn't support AVX. Use old kernels.
  529. }
  530. }
  531. //Apollo Lake or Denverton
  532. if (model == 12 || model == 15) {
  533. return &gotoblas_NEHALEM;
  534. }
  535. return NULL;
  536. case 6:
  537. if (model == 6) {
  538. // Cannon Lake
  539. if(support_avx2())
  540. return &gotoblas_HASWELL;
  541. if(support_avx()) {
  542. openblas_warning(FALLBACK_VERBOSE, SANDYBRIDGE_FALLBACK);
  543. return &gotoblas_SANDYBRIDGE;
  544. } else {
  545. openblas_warning(FALLBACK_VERBOSE, NEHALEM_FALLBACK);
  546. return &gotoblas_NEHALEM;
  547. }
  548. }
  549. return NULL;
  550. case 7:
  551. if (model == 10) // Goldmont plus
  552. return &gotoblas_NEHALEM;
  553. if (model == 14) {
  554. // Ice Lake
  555. if (support_avx512())
  556. return &gotoblas_SKYLAKEX;
  557. if(support_avx2()){
  558. openblas_warning(FALLBACK_VERBOSE, HASWELL_FALLBACK);
  559. return &gotoblas_HASWELL;
  560. }
  561. if(support_avx()) {
  562. openblas_warning(FALLBACK_VERBOSE, SANDYBRIDGE_FALLBACK);
  563. return &gotoblas_SANDYBRIDGE;
  564. } else {
  565. openblas_warning(FALLBACK_VERBOSE, NEHALEM_FALLBACK);
  566. return &gotoblas_NEHALEM;
  567. }
  568. }
  569. return NULL;
  570. case 9:
  571. case 8:
  572. if (model == 14 ) { // Kaby Lake, Coffee Lake
  573. if(support_avx2())
  574. return &gotoblas_HASWELL;
  575. if(support_avx()) {
  576. openblas_warning(FALLBACK_VERBOSE, SANDYBRIDGE_FALLBACK);
  577. return &gotoblas_SANDYBRIDGE;
  578. } else {
  579. openblas_warning(FALLBACK_VERBOSE, NEHALEM_FALLBACK);
  580. return &gotoblas_NEHALEM; //OS doesn't support AVX. Use old kernels.
  581. }
  582. }
  583. return NULL;
  584. }
  585. case 0xf:
  586. if (model <= 0x2) return &gotoblas_NORTHWOOD;
  587. return &gotoblas_PRESCOTT;
  588. }
  589. }
  590. if (vendor == VENDOR_AMD || vendor == VENDOR_HYGON){
  591. if (family <= 0xe) {
  592. // Verify that CPU has 3dnow and 3dnowext before claiming it is Athlon
  593. cpuid(0x80000000, &eax, &ebx, &ecx, &edx);
  594. if ( (eax & 0xffff) >= 0x01) {
  595. cpuid(0x80000001, &eax, &ebx, &ecx, &edx);
  596. if ((edx & (1 << 30)) == 0 || (edx & (1 << 31)) == 0)
  597. return NULL;
  598. }
  599. else
  600. return NULL;
  601. return &gotoblas_ATHLON;
  602. }
  603. if (family == 0xf){
  604. if ((exfamily == 0) || (exfamily == 2)) {
  605. if (ecx & (1 << 0)) return &gotoblas_OPTERON_SSE3;
  606. else return &gotoblas_OPTERON;
  607. } else if (exfamily == 5) {
  608. return &gotoblas_BOBCAT;
  609. } else if (exfamily == 6) {
  610. if(model == 1){
  611. //AMD Bulldozer Opteron 6200 / Opteron 4200 / AMD FX-Series
  612. if(support_avx())
  613. return &gotoblas_BULLDOZER;
  614. else{
  615. openblas_warning(FALLBACK_VERBOSE, BARCELONA_FALLBACK);
  616. return &gotoblas_BARCELONA; //OS doesn't support AVX. Use old kernels.
  617. }
  618. }else if(model == 2 || model == 3){
  619. //AMD Bulldozer Opteron 6300 / Opteron 4300 / Opteron 3300
  620. if(support_avx())
  621. return &gotoblas_PILEDRIVER;
  622. else{
  623. openblas_warning(FALLBACK_VERBOSE, BARCELONA_FALLBACK);
  624. return &gotoblas_BARCELONA; //OS doesn't support AVX. Use old kernels.
  625. }
  626. }else if(model == 5){
  627. if(support_avx())
  628. return &gotoblas_EXCAVATOR;
  629. else{
  630. openblas_warning(FALLBACK_VERBOSE, BARCELONA_FALLBACK);
  631. return &gotoblas_BARCELONA; //OS doesn't support AVX. Use old kernels.
  632. }
  633. }else if(model == 0 || model == 8){
  634. if (exmodel == 1) {
  635. //AMD Trinity
  636. if(support_avx())
  637. return &gotoblas_PILEDRIVER;
  638. else{
  639. openblas_warning(FALLBACK_VERBOSE, BARCELONA_FALLBACK);
  640. return &gotoblas_BARCELONA; //OS doesn't support AVX. Use old kernels.
  641. }
  642. }else if (exmodel == 3) {
  643. //AMD STEAMROLLER
  644. if(support_avx())
  645. return &gotoblas_STEAMROLLER;
  646. else{
  647. openblas_warning(FALLBACK_VERBOSE, BARCELONA_FALLBACK);
  648. return &gotoblas_BARCELONA; //OS doesn't support AVX. Use old kernels.
  649. }
  650. }else if (exmodel == 6) {
  651. if(support_avx())
  652. return &gotoblas_EXCAVATOR;
  653. else{
  654. openblas_warning(FALLBACK_VERBOSE, BARCELONA_FALLBACK);
  655. return &gotoblas_BARCELONA; //OS doesn't support AVX. Use old kernels.
  656. }
  657. }
  658. }
  659. } else if (exfamily == 8) {
  660. if (model == 1 || model == 8) {
  661. if(support_avx())
  662. return &gotoblas_ZEN;
  663. else{
  664. openblas_warning(FALLBACK_VERBOSE, BARCELONA_FALLBACK);
  665. return &gotoblas_BARCELONA; //OS doesn't support AVX. Use old kernels.
  666. }
  667. }
  668. } else if (exfamily == 9) {
  669. if(support_avx())
  670. return &gotoblas_ZEN;
  671. else{
  672. openblas_warning(FALLBACK_VERBOSE, BARCELONA_FALLBACK);
  673. return &gotoblas_BARCELONA; //OS doesn't support AVX. Use old kernels.
  674. }
  675. }else {
  676. return &gotoblas_BARCELONA;
  677. }
  678. }
  679. }
  680. if (vendor == VENDOR_CENTAUR) {
  681. switch (family) {
  682. case 0x6:
  683. return &gotoblas_NANO;
  684. }
  685. }
  686. return NULL;
  687. }
  688. static char *corename[] = {
  689. "Unknown",
  690. "Katmai",
  691. "Coppermine",
  692. "Northwood",
  693. "Prescott",
  694. "Banias",
  695. "Atom",
  696. "Core2",
  697. "Penryn",
  698. "Dunnington",
  699. "Nehalem",
  700. "Athlon",
  701. "Opteron",
  702. "Opteron_SSE3",
  703. "Barcelona",
  704. "Nano",
  705. "Sandybridge",
  706. "Bobcat",
  707. "Bulldozer",
  708. "Piledriver",
  709. "Haswell",
  710. "Steamroller",
  711. "Excavator",
  712. "Zen",
  713. "SkylakeX"
  714. };
  715. char *gotoblas_corename(void) {
  716. if (gotoblas == &gotoblas_KATMAI) return corename[ 1];
  717. if (gotoblas == &gotoblas_COPPERMINE) return corename[ 2];
  718. if (gotoblas == &gotoblas_NORTHWOOD) return corename[ 3];
  719. if (gotoblas == &gotoblas_PRESCOTT) return corename[ 4];
  720. if (gotoblas == &gotoblas_BANIAS) return corename[ 5];
  721. if (gotoblas == &gotoblas_ATOM) return corename[ 6];
  722. if (gotoblas == &gotoblas_CORE2) return corename[ 7];
  723. if (gotoblas == &gotoblas_PENRYN) return corename[ 8];
  724. if (gotoblas == &gotoblas_DUNNINGTON) return corename[ 9];
  725. if (gotoblas == &gotoblas_NEHALEM) return corename[10];
  726. if (gotoblas == &gotoblas_ATHLON) return corename[11];
  727. if (gotoblas == &gotoblas_OPTERON_SSE3) return corename[12];
  728. if (gotoblas == &gotoblas_OPTERON) return corename[13];
  729. if (gotoblas == &gotoblas_BARCELONA) return corename[14];
  730. if (gotoblas == &gotoblas_NANO) return corename[15];
  731. if (gotoblas == &gotoblas_SANDYBRIDGE) return corename[16];
  732. if (gotoblas == &gotoblas_BOBCAT) return corename[17];
  733. if (gotoblas == &gotoblas_BULLDOZER) return corename[18];
  734. if (gotoblas == &gotoblas_PILEDRIVER) return corename[19];
  735. if (gotoblas == &gotoblas_HASWELL) return corename[20];
  736. if (gotoblas == &gotoblas_STEAMROLLER) return corename[21];
  737. if (gotoblas == &gotoblas_EXCAVATOR) return corename[22];
  738. if (gotoblas == &gotoblas_ZEN) return corename[23];
  739. if (gotoblas == &gotoblas_SKYLAKEX) return corename[24];
  740. return corename[0];
  741. }
  742. static gotoblas_t *force_coretype(char *coretype){
  743. int i ;
  744. int found = -1;
  745. char message[128];
  746. //char mname[20];
  747. for ( i=1 ; i <= 24; i++)
  748. {
  749. if (!strncasecmp(coretype,corename[i],20))
  750. {
  751. found = i;
  752. break;
  753. }
  754. }
  755. if (found < 0)
  756. {
  757. //strncpy(mname,coretype,20);
  758. snprintf(message, 128, "Core not found: %s\n",coretype);
  759. openblas_warning(1, message);
  760. return(NULL);
  761. }
  762. switch (found)
  763. {
  764. case 24: return (&gotoblas_SKYLAKEX);
  765. case 23: return (&gotoblas_ZEN);
  766. case 22: return (&gotoblas_EXCAVATOR);
  767. case 21: return (&gotoblas_STEAMROLLER);
  768. case 20: return (&gotoblas_HASWELL);
  769. case 19: return (&gotoblas_PILEDRIVER);
  770. case 18: return (&gotoblas_BULLDOZER);
  771. case 17: return (&gotoblas_BOBCAT);
  772. case 16: return (&gotoblas_SANDYBRIDGE);
  773. case 15: return (&gotoblas_NANO);
  774. case 14: return (&gotoblas_BARCELONA);
  775. case 13: return (&gotoblas_OPTERON);
  776. case 12: return (&gotoblas_OPTERON_SSE3);
  777. case 11: return (&gotoblas_ATHLON);
  778. case 10: return (&gotoblas_NEHALEM);
  779. case 9: return (&gotoblas_DUNNINGTON);
  780. case 8: return (&gotoblas_PENRYN);
  781. case 7: return (&gotoblas_CORE2);
  782. case 6: return (&gotoblas_ATOM);
  783. case 5: return (&gotoblas_BANIAS);
  784. case 4: return (&gotoblas_PRESCOTT);
  785. case 3: return (&gotoblas_NORTHWOOD);
  786. case 2: return (&gotoblas_COPPERMINE);
  787. case 1: return (&gotoblas_KATMAI);
  788. }
  789. return(NULL);
  790. }
  791. void gotoblas_dynamic_init(void) {
  792. char coremsg[128];
  793. char coren[22];
  794. char *p;
  795. if (gotoblas) return;
  796. p = getenv("OPENBLAS_CORETYPE");
  797. if ( p )
  798. {
  799. gotoblas = force_coretype(p);
  800. }
  801. else
  802. {
  803. gotoblas = get_coretype();
  804. }
  805. #ifdef ARCH_X86
  806. if (gotoblas == NULL) gotoblas = &gotoblas_KATMAI;
  807. #else
  808. if (gotoblas == NULL) gotoblas = &gotoblas_PRESCOTT;
  809. /* sanity check, if 64bit pointer we can't have a 32 bit cpu */
  810. if (sizeof(void*) == 8) {
  811. if (gotoblas == &gotoblas_KATMAI ||
  812. gotoblas == &gotoblas_COPPERMINE ||
  813. gotoblas == &gotoblas_NORTHWOOD ||
  814. gotoblas == &gotoblas_BANIAS ||
  815. gotoblas == &gotoblas_ATHLON)
  816. gotoblas = &gotoblas_PRESCOTT;
  817. }
  818. #endif
  819. if (gotoblas && gotoblas -> init) {
  820. strncpy(coren,gotoblas_corename(),20);
  821. sprintf(coremsg, "Core: %s\n",coren);
  822. openblas_warning(2, coremsg);
  823. gotoblas -> init();
  824. } else {
  825. openblas_warning(0, "OpenBLAS : Architecture Initialization failed. No initialization function found.\n");
  826. exit(1);
  827. }
  828. }
  829. void gotoblas_dynamic_quit(void) {
  830. gotoblas = NULL;
  831. }