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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535
  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 ARCH_X86
  40. #define EXTERN extern
  41. #else
  42. #define EXTERN
  43. #endif
  44. EXTERN gotoblas_t gotoblas_KATMAI;
  45. EXTERN gotoblas_t gotoblas_COPPERMINE;
  46. EXTERN gotoblas_t gotoblas_NORTHWOOD;
  47. EXTERN gotoblas_t gotoblas_BANIAS;
  48. EXTERN gotoblas_t gotoblas_ATHLON;
  49. extern gotoblas_t gotoblas_PRESCOTT;
  50. extern gotoblas_t gotoblas_ATOM;
  51. extern gotoblas_t gotoblas_NANO;
  52. extern gotoblas_t gotoblas_CORE2;
  53. extern gotoblas_t gotoblas_PENRYN;
  54. extern gotoblas_t gotoblas_DUNNINGTON;
  55. extern gotoblas_t gotoblas_NEHALEM;
  56. extern gotoblas_t gotoblas_OPTERON;
  57. extern gotoblas_t gotoblas_OPTERON_SSE3;
  58. extern gotoblas_t gotoblas_BARCELONA;
  59. extern gotoblas_t gotoblas_BOBCAT;
  60. #ifndef NO_AVX
  61. extern gotoblas_t gotoblas_SANDYBRIDGE;
  62. extern gotoblas_t gotoblas_BULLDOZER;
  63. extern gotoblas_t gotoblas_PILEDRIVER;
  64. extern gotoblas_t gotoblas_STEAMROLLER;
  65. extern gotoblas_t gotoblas_EXCAVATOR;
  66. #ifdef NO_AVX2
  67. #define gotoblas_HASWELL gotoblas_SANDYBRIDGE
  68. #else
  69. extern gotoblas_t gotoblas_HASWELL;
  70. #endif
  71. #else
  72. //Use NEHALEM kernels for sandy bridge
  73. #define gotoblas_SANDYBRIDGE gotoblas_NEHALEM
  74. #define gotoblas_HASWELL gotoblas_NEHALEM
  75. #define gotoblas_BULLDOZER gotoblas_BARCELONA
  76. #define gotoblas_PILEDRIVER gotoblas_BARCELONA
  77. #define gotoblas_STEAMROLLER gotoblas_BARCELONA
  78. #define gotoblas_EXCAVATOR gotoblas_BARCELONA
  79. #endif
  80. #define VENDOR_INTEL 1
  81. #define VENDOR_AMD 2
  82. #define VENDOR_CENTAUR 3
  83. #define VENDOR_UNKNOWN 99
  84. #define BITMASK(a, b, c) ((((a) >> (b)) & (c)))
  85. #ifndef NO_AVX
  86. static inline void xgetbv(int op, int * eax, int * edx){
  87. //Use binary code for xgetbv
  88. __asm__ __volatile__
  89. (".byte 0x0f, 0x01, 0xd0": "=a" (*eax), "=d" (*edx) : "c" (op) : "cc");
  90. }
  91. #endif
  92. int support_avx(){
  93. #ifndef NO_AVX
  94. int eax, ebx, ecx, edx;
  95. int ret=0;
  96. cpuid(1, &eax, &ebx, &ecx, &edx);
  97. if ((ecx & (1 << 28)) != 0 && (ecx & (1 << 27)) != 0 && (ecx & (1 << 26)) != 0){
  98. xgetbv(0, &eax, &edx);
  99. if((eax & 6) == 6){
  100. ret=1; //OS support AVX
  101. }
  102. }
  103. return ret;
  104. #else
  105. return 0;
  106. #endif
  107. }
  108. extern void openblas_warning(int verbose, const char * msg);
  109. #define FALLBACK_VERBOSE 1
  110. #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"
  111. #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"
  112. static int get_vendor(void){
  113. int eax, ebx, ecx, edx;
  114. union
  115. {
  116. char vchar[16];
  117. int vint[4];
  118. } vendor;
  119. cpuid(0, &eax, &ebx, &ecx, &edx);
  120. *(&vendor.vint[0]) = ebx;
  121. *(&vendor.vint[1]) = edx;
  122. *(&vendor.vint[2]) = ecx;
  123. vendor.vchar[12] = '\0';
  124. if (!strcmp(vendor.vchar, "GenuineIntel")) return VENDOR_INTEL;
  125. if (!strcmp(vendor.vchar, "AuthenticAMD")) return VENDOR_AMD;
  126. if (!strcmp(vendor.vchar, "CentaurHauls")) return VENDOR_CENTAUR;
  127. if ((eax == 0) || ((eax & 0x500) != 0)) return VENDOR_INTEL;
  128. return VENDOR_UNKNOWN;
  129. }
  130. static gotoblas_t *get_coretype(void){
  131. int eax, ebx, ecx, edx;
  132. int family, exfamily, model, vendor, exmodel;
  133. cpuid(1, &eax, &ebx, &ecx, &edx);
  134. family = BITMASK(eax, 8, 0x0f);
  135. exfamily = BITMASK(eax, 20, 0xff);
  136. model = BITMASK(eax, 4, 0x0f);
  137. exmodel = BITMASK(eax, 16, 0x0f);
  138. vendor = get_vendor();
  139. if (vendor == VENDOR_INTEL){
  140. switch (family) {
  141. case 0x6:
  142. switch (exmodel) {
  143. case 0:
  144. if (model <= 0x7) return &gotoblas_KATMAI;
  145. if ((model == 0x8) || (model == 0xa) || (model == 0xb)) return &gotoblas_COPPERMINE;
  146. if ((model == 0x9) || (model == 0xd)) return &gotoblas_BANIAS;
  147. if (model == 14) return &gotoblas_BANIAS;
  148. if (model == 15) return &gotoblas_CORE2;
  149. return NULL;
  150. case 1:
  151. if (model == 6) return &gotoblas_CORE2;
  152. if (model == 7) return &gotoblas_PENRYN;
  153. if (model == 13) return &gotoblas_DUNNINGTON;
  154. if ((model == 10) || (model == 11) || (model == 14) || (model == 15)) return &gotoblas_NEHALEM;
  155. if (model == 12) return &gotoblas_ATOM;
  156. return NULL;
  157. case 2:
  158. //Intel Core (Clarkdale) / Core (Arrandale)
  159. // Pentium (Clarkdale) / Pentium Mobile (Arrandale)
  160. // Xeon (Clarkdale), 32nm
  161. if (model == 5) return &gotoblas_NEHALEM;
  162. //Intel Xeon Processor 5600 (Westmere-EP)
  163. //Xeon Processor E7 (Westmere-EX)
  164. //Xeon E7540
  165. if (model == 12 || model == 14 || model == 15) return &gotoblas_NEHALEM;
  166. //Intel Core i5-2000 /i7-2000 (Sandy Bridge)
  167. //Intel Core i7-3000 / Xeon E5
  168. if (model == 10 || model == 13) {
  169. if(support_avx())
  170. return &gotoblas_SANDYBRIDGE;
  171. else{
  172. openblas_warning(FALLBACK_VERBOSE, NEHALEM_FALLBACK);
  173. return &gotoblas_NEHALEM; //OS doesn't support AVX. Use old kernels.
  174. }
  175. }
  176. return NULL;
  177. case 3:
  178. //Intel Sandy Bridge 22nm (Ivy Bridge?)
  179. if (model == 10 || model == 14) {
  180. if(support_avx())
  181. return &gotoblas_SANDYBRIDGE;
  182. else{
  183. openblas_warning(FALLBACK_VERBOSE, NEHALEM_FALLBACK);
  184. return &gotoblas_NEHALEM; //OS doesn't support AVX. Use old kernels.
  185. }
  186. }
  187. //Intel Haswell
  188. if (model == 12 || model == 15) {
  189. if(support_avx())
  190. return &gotoblas_HASWELL;
  191. else{
  192. openblas_warning(FALLBACK_VERBOSE, NEHALEM_FALLBACK);
  193. return &gotoblas_NEHALEM; //OS doesn't support AVX. Use old kernels.
  194. }
  195. }
  196. //Intel Broadwell
  197. if (model == 13) {
  198. if(support_avx())
  199. return &gotoblas_HASWELL;
  200. else{
  201. openblas_warning(FALLBACK_VERBOSE, NEHALEM_FALLBACK);
  202. return &gotoblas_NEHALEM; //OS doesn't support AVX. Use old kernels.
  203. }
  204. }
  205. return NULL;
  206. case 4:
  207. //Intel Haswell
  208. if (model == 5 || model == 6) {
  209. if(support_avx())
  210. return &gotoblas_HASWELL;
  211. else{
  212. openblas_warning(FALLBACK_VERBOSE, NEHALEM_FALLBACK);
  213. return &gotoblas_NEHALEM; //OS doesn't support AVX. Use old kernels.
  214. }
  215. }
  216. //Intel Broadwell
  217. if (model == 7 || model == 15) {
  218. if(support_avx())
  219. return &gotoblas_HASWELL;
  220. else{
  221. openblas_warning(FALLBACK_VERBOSE, NEHALEM_FALLBACK);
  222. return &gotoblas_NEHALEM; //OS doesn't support AVX. Use old kernels.
  223. }
  224. }
  225. //Intel Skylake
  226. if (model == 14) {
  227. if(support_avx())
  228. return &gotoblas_HASWELL;
  229. else{
  230. openblas_warning(FALLBACK_VERBOSE, NEHALEM_FALLBACK);
  231. return &gotoblas_NEHALEM; //OS doesn't support AVX. Use old kernels.
  232. }
  233. }
  234. return NULL;
  235. case 5:
  236. //Intel Broadwell
  237. if (model == 6) {
  238. if(support_avx())
  239. return &gotoblas_HASWELL;
  240. else{
  241. openblas_warning(FALLBACK_VERBOSE, NEHALEM_FALLBACK);
  242. return &gotoblas_NEHALEM; //OS doesn't support AVX. Use old kernels.
  243. }
  244. }
  245. //Intel Skylake
  246. if (model == 14 || model == 5) {
  247. if(support_avx())
  248. return &gotoblas_HASWELL;
  249. else{
  250. openblas_warning(FALLBACK_VERBOSE, NEHALEM_FALLBACK);
  251. return &gotoblas_NEHALEM; //OS doesn't support AVX. Use old kernels.
  252. }
  253. }
  254. return NULL;
  255. }
  256. case 0xf:
  257. if (model <= 0x2) return &gotoblas_NORTHWOOD;
  258. return &gotoblas_PRESCOTT;
  259. }
  260. }
  261. if (vendor == VENDOR_AMD){
  262. if (family <= 0xe) {
  263. // Verify that CPU has 3dnow and 3dnowext before claiming it is Athlon
  264. cpuid(0x80000000, &eax, &ebx, &ecx, &edx);
  265. if ( (eax & 0xffff) >= 0x01) {
  266. cpuid(0x80000001, &eax, &ebx, &ecx, &edx);
  267. if ((edx & (1 << 30)) == 0 || (edx & (1 << 31)) == 0)
  268. return NULL;
  269. }
  270. else
  271. return NULL;
  272. return &gotoblas_ATHLON;
  273. }
  274. if (family == 0xf){
  275. if ((exfamily == 0) || (exfamily == 2)) {
  276. if (ecx & (1 << 0)) return &gotoblas_OPTERON_SSE3;
  277. else return &gotoblas_OPTERON;
  278. } else if (exfamily == 5) {
  279. return &gotoblas_BOBCAT;
  280. } else if (exfamily == 6) {
  281. if(model == 1){
  282. //AMD Bulldozer Opteron 6200 / Opteron 4200 / AMD FX-Series
  283. if(support_avx())
  284. return &gotoblas_BULLDOZER;
  285. else{
  286. openblas_warning(FALLBACK_VERBOSE, BARCELONA_FALLBACK);
  287. return &gotoblas_BARCELONA; //OS doesn't support AVX. Use old kernels.
  288. }
  289. }else if(model == 2 || model == 3){
  290. //AMD Bulldozer Opteron 6300 / Opteron 4300 / Opteron 3300
  291. if(support_avx())
  292. return &gotoblas_PILEDRIVER;
  293. else{
  294. openblas_warning(FALLBACK_VERBOSE, BARCELONA_FALLBACK);
  295. return &gotoblas_BARCELONA; //OS doesn't support AVX. Use old kernels.
  296. }
  297. }else if(model == 0){
  298. if (exmodel == 1) {
  299. //AMD Trinity
  300. if(support_avx())
  301. return &gotoblas_PILEDRIVER;
  302. else{
  303. openblas_warning(FALLBACK_VERBOSE, BARCELONA_FALLBACK);
  304. return &gotoblas_BARCELONA; //OS doesn't support AVX. Use old kernels.
  305. }
  306. }else if (exmodel == 3) {
  307. //AMD STEAMROLLER
  308. if(support_avx())
  309. return &gotoblas_STEAMROLLER;
  310. else{
  311. openblas_warning(FALLBACK_VERBOSE, BARCELONA_FALLBACK);
  312. return &gotoblas_BARCELONA; //OS doesn't support AVX. Use old kernels.
  313. }
  314. }else if (exmodel == 6) {
  315. if(support_avx())
  316. return &gotoblas_EXCAVATOR;
  317. else{
  318. openblas_warning(FALLBACK_VERBOSE, BARCELONA_FALLBACK);
  319. return &gotoblas_BARCELONA; //OS doesn't support AVX. Use old kernels.
  320. }
  321. }
  322. }
  323. } else {
  324. return &gotoblas_BARCELONA;
  325. }
  326. }
  327. }
  328. if (vendor == VENDOR_CENTAUR) {
  329. switch (family) {
  330. case 0x6:
  331. return &gotoblas_NANO;
  332. break;
  333. }
  334. }
  335. return NULL;
  336. }
  337. static char *corename[] = {
  338. "Unknown",
  339. "Katmai",
  340. "Coppermine",
  341. "Northwood",
  342. "Prescott",
  343. "Banias",
  344. "Atom",
  345. "Core2",
  346. "Penryn",
  347. "Dunnington",
  348. "Nehalem",
  349. "Athlon",
  350. "Opteron",
  351. "Opteron(SSE3)",
  352. "Barcelona",
  353. "Nano",
  354. "Sandybridge",
  355. "Bobcat",
  356. "Bulldozer",
  357. "Piledriver",
  358. "Haswell",
  359. "Steamroller",
  360. "Excavator",
  361. };
  362. char *gotoblas_corename(void) {
  363. if (gotoblas == &gotoblas_KATMAI) return corename[ 1];
  364. if (gotoblas == &gotoblas_COPPERMINE) return corename[ 2];
  365. if (gotoblas == &gotoblas_NORTHWOOD) return corename[ 3];
  366. if (gotoblas == &gotoblas_PRESCOTT) return corename[ 4];
  367. if (gotoblas == &gotoblas_BANIAS) return corename[ 5];
  368. if (gotoblas == &gotoblas_ATOM) return corename[ 6];
  369. if (gotoblas == &gotoblas_CORE2) return corename[ 7];
  370. if (gotoblas == &gotoblas_PENRYN) return corename[ 8];
  371. if (gotoblas == &gotoblas_DUNNINGTON) return corename[ 9];
  372. if (gotoblas == &gotoblas_NEHALEM) return corename[10];
  373. if (gotoblas == &gotoblas_ATHLON) return corename[11];
  374. if (gotoblas == &gotoblas_OPTERON_SSE3) return corename[12];
  375. if (gotoblas == &gotoblas_OPTERON) return corename[13];
  376. if (gotoblas == &gotoblas_BARCELONA) return corename[14];
  377. if (gotoblas == &gotoblas_NANO) return corename[15];
  378. if (gotoblas == &gotoblas_SANDYBRIDGE) return corename[16];
  379. if (gotoblas == &gotoblas_BOBCAT) return corename[17];
  380. if (gotoblas == &gotoblas_BULLDOZER) return corename[18];
  381. if (gotoblas == &gotoblas_PILEDRIVER) return corename[19];
  382. if (gotoblas == &gotoblas_HASWELL) return corename[20];
  383. if (gotoblas == &gotoblas_STEAMROLLER) return corename[21];
  384. if (gotoblas == &gotoblas_EXCAVATOR) return corename[22];
  385. return corename[0];
  386. }
  387. static gotoblas_t *force_coretype(char *coretype){
  388. int i ;
  389. int found = -1;
  390. char message[128];
  391. //char mname[20];
  392. for ( i=1 ; i <= 21; i++)
  393. {
  394. if (!strncasecmp(coretype,corename[i],20))
  395. {
  396. found = i;
  397. break;
  398. }
  399. }
  400. if (found < 0)
  401. {
  402. //strncpy(mname,coretype,20);
  403. snprintf(message, 128, "Core not found: %s\n",coretype);
  404. openblas_warning(1, message);
  405. return(NULL);
  406. }
  407. switch (found)
  408. {
  409. case 22: return (&gotoblas_EXCAVATOR);
  410. case 21: return (&gotoblas_STEAMROLLER);
  411. case 20: return (&gotoblas_HASWELL);
  412. case 19: return (&gotoblas_PILEDRIVER);
  413. case 18: return (&gotoblas_BULLDOZER);
  414. case 17: return (&gotoblas_BOBCAT);
  415. case 16: return (&gotoblas_SANDYBRIDGE);
  416. case 15: return (&gotoblas_NANO);
  417. case 14: return (&gotoblas_BARCELONA);
  418. case 13: return (&gotoblas_OPTERON);
  419. case 12: return (&gotoblas_OPTERON_SSE3);
  420. case 11: return (&gotoblas_ATHLON);
  421. case 10: return (&gotoblas_NEHALEM);
  422. case 9: return (&gotoblas_DUNNINGTON);
  423. case 8: return (&gotoblas_PENRYN);
  424. case 7: return (&gotoblas_CORE2);
  425. case 6: return (&gotoblas_ATOM);
  426. case 5: return (&gotoblas_BANIAS);
  427. case 4: return (&gotoblas_PRESCOTT);
  428. case 3: return (&gotoblas_NORTHWOOD);
  429. case 2: return (&gotoblas_COPPERMINE);
  430. case 1: return (&gotoblas_KATMAI);
  431. }
  432. return(NULL);
  433. }
  434. void gotoblas_dynamic_init(void) {
  435. char coremsg[128];
  436. char coren[22];
  437. char *p;
  438. if (gotoblas) return;
  439. p = getenv("OPENBLAS_CORETYPE");
  440. if ( p )
  441. {
  442. gotoblas = force_coretype(p);
  443. }
  444. else
  445. {
  446. gotoblas = get_coretype();
  447. }
  448. #ifdef ARCH_X86
  449. if (gotoblas == NULL) gotoblas = &gotoblas_KATMAI;
  450. #else
  451. if (gotoblas == NULL) gotoblas = &gotoblas_PRESCOTT;
  452. /* sanity check, if 64bit pointer we can't have a 32 bit cpu */
  453. if (sizeof(void*) == 8) {
  454. if (gotoblas == &gotoblas_KATMAI ||
  455. gotoblas == &gotoblas_COPPERMINE ||
  456. gotoblas == &gotoblas_NORTHWOOD ||
  457. gotoblas == &gotoblas_BANIAS ||
  458. gotoblas == &gotoblas_ATHLON)
  459. gotoblas = &gotoblas_PRESCOTT;
  460. }
  461. #endif
  462. if (gotoblas && gotoblas -> init) {
  463. strncpy(coren,gotoblas_corename(),20);
  464. sprintf(coremsg, "Core: %s\n",coren);
  465. openblas_warning(2, coremsg);
  466. gotoblas -> init();
  467. } else {
  468. openblas_warning(0, "OpenBLAS : Architecture Initialization failed. No initialization function found.\n");
  469. exit(1);
  470. }
  471. }
  472. void gotoblas_dynamic_quit(void) {
  473. gotoblas = NULL;
  474. }