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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541
  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. if (model == 7) return &gotoblas_ATOM; //Bay Trail
  206. return NULL;
  207. case 4:
  208. //Intel Haswell
  209. if (model == 5 || model == 6) {
  210. if(support_avx())
  211. return &gotoblas_HASWELL;
  212. else{
  213. openblas_warning(FALLBACK_VERBOSE, NEHALEM_FALLBACK);
  214. return &gotoblas_NEHALEM; //OS doesn't support AVX. Use old kernels.
  215. }
  216. }
  217. //Intel Broadwell
  218. if (model == 7 || model == 15) {
  219. if(support_avx())
  220. return &gotoblas_HASWELL;
  221. else{
  222. openblas_warning(FALLBACK_VERBOSE, NEHALEM_FALLBACK);
  223. return &gotoblas_NEHALEM; //OS doesn't support AVX. Use old kernels.
  224. }
  225. }
  226. //Intel Skylake
  227. if (model == 14) {
  228. if(support_avx())
  229. return &gotoblas_HASWELL;
  230. else{
  231. openblas_warning(FALLBACK_VERBOSE, NEHALEM_FALLBACK);
  232. return &gotoblas_NEHALEM; //OS doesn't support AVX. Use old kernels.
  233. }
  234. }
  235. //Intel Braswell / Avoton
  236. if (model == 12 || model == 13) {
  237. openblas_warning(FALLBACK_VERBOSE, NEHALEM_FALLBACK);
  238. return &gotoblas_NEHALEM;
  239. }
  240. return NULL;
  241. case 5:
  242. //Intel Broadwell
  243. if (model == 6) {
  244. if(support_avx())
  245. return &gotoblas_HASWELL;
  246. else{
  247. openblas_warning(FALLBACK_VERBOSE, NEHALEM_FALLBACK);
  248. return &gotoblas_NEHALEM; //OS doesn't support AVX. Use old kernels.
  249. }
  250. }
  251. //Intel Skylake
  252. if (model == 14 || model == 5) {
  253. if(support_avx())
  254. return &gotoblas_HASWELL;
  255. else{
  256. openblas_warning(FALLBACK_VERBOSE, NEHALEM_FALLBACK);
  257. return &gotoblas_NEHALEM; //OS doesn't support AVX. Use old kernels.
  258. }
  259. }
  260. return NULL;
  261. }
  262. case 0xf:
  263. if (model <= 0x2) return &gotoblas_NORTHWOOD;
  264. return &gotoblas_PRESCOTT;
  265. }
  266. }
  267. if (vendor == VENDOR_AMD){
  268. if (family <= 0xe) {
  269. // Verify that CPU has 3dnow and 3dnowext before claiming it is Athlon
  270. cpuid(0x80000000, &eax, &ebx, &ecx, &edx);
  271. if ( (eax & 0xffff) >= 0x01) {
  272. cpuid(0x80000001, &eax, &ebx, &ecx, &edx);
  273. if ((edx & (1 << 30)) == 0 || (edx & (1 << 31)) == 0)
  274. return NULL;
  275. }
  276. else
  277. return NULL;
  278. return &gotoblas_ATHLON;
  279. }
  280. if (family == 0xf){
  281. if ((exfamily == 0) || (exfamily == 2)) {
  282. if (ecx & (1 << 0)) return &gotoblas_OPTERON_SSE3;
  283. else return &gotoblas_OPTERON;
  284. } else if (exfamily == 5) {
  285. return &gotoblas_BOBCAT;
  286. } else if (exfamily == 6) {
  287. if(model == 1){
  288. //AMD Bulldozer Opteron 6200 / Opteron 4200 / AMD FX-Series
  289. if(support_avx())
  290. return &gotoblas_BULLDOZER;
  291. else{
  292. openblas_warning(FALLBACK_VERBOSE, BARCELONA_FALLBACK);
  293. return &gotoblas_BARCELONA; //OS doesn't support AVX. Use old kernels.
  294. }
  295. }else if(model == 2 || model == 3){
  296. //AMD Bulldozer Opteron 6300 / Opteron 4300 / Opteron 3300
  297. if(support_avx())
  298. return &gotoblas_PILEDRIVER;
  299. else{
  300. openblas_warning(FALLBACK_VERBOSE, BARCELONA_FALLBACK);
  301. return &gotoblas_BARCELONA; //OS doesn't support AVX. Use old kernels.
  302. }
  303. }else if(model == 0){
  304. if (exmodel == 1) {
  305. //AMD Trinity
  306. if(support_avx())
  307. return &gotoblas_PILEDRIVER;
  308. else{
  309. openblas_warning(FALLBACK_VERBOSE, BARCELONA_FALLBACK);
  310. return &gotoblas_BARCELONA; //OS doesn't support AVX. Use old kernels.
  311. }
  312. }else if (exmodel == 3) {
  313. //AMD STEAMROLLER
  314. if(support_avx())
  315. return &gotoblas_STEAMROLLER;
  316. else{
  317. openblas_warning(FALLBACK_VERBOSE, BARCELONA_FALLBACK);
  318. return &gotoblas_BARCELONA; //OS doesn't support AVX. Use old kernels.
  319. }
  320. }else if (exmodel == 6) {
  321. if(support_avx())
  322. return &gotoblas_EXCAVATOR;
  323. else{
  324. openblas_warning(FALLBACK_VERBOSE, BARCELONA_FALLBACK);
  325. return &gotoblas_BARCELONA; //OS doesn't support AVX. Use old kernels.
  326. }
  327. }
  328. }
  329. } else {
  330. return &gotoblas_BARCELONA;
  331. }
  332. }
  333. }
  334. if (vendor == VENDOR_CENTAUR) {
  335. switch (family) {
  336. case 0x6:
  337. return &gotoblas_NANO;
  338. break;
  339. }
  340. }
  341. return NULL;
  342. }
  343. static char *corename[] = {
  344. "Unknown",
  345. "Katmai",
  346. "Coppermine",
  347. "Northwood",
  348. "Prescott",
  349. "Banias",
  350. "Atom",
  351. "Core2",
  352. "Penryn",
  353. "Dunnington",
  354. "Nehalem",
  355. "Athlon",
  356. "Opteron",
  357. "Opteron_SSE3",
  358. "Barcelona",
  359. "Nano",
  360. "Sandybridge",
  361. "Bobcat",
  362. "Bulldozer",
  363. "Piledriver",
  364. "Haswell",
  365. "Steamroller",
  366. "Excavator",
  367. };
  368. char *gotoblas_corename(void) {
  369. if (gotoblas == &gotoblas_KATMAI) return corename[ 1];
  370. if (gotoblas == &gotoblas_COPPERMINE) return corename[ 2];
  371. if (gotoblas == &gotoblas_NORTHWOOD) return corename[ 3];
  372. if (gotoblas == &gotoblas_PRESCOTT) return corename[ 4];
  373. if (gotoblas == &gotoblas_BANIAS) return corename[ 5];
  374. if (gotoblas == &gotoblas_ATOM) return corename[ 6];
  375. if (gotoblas == &gotoblas_CORE2) return corename[ 7];
  376. if (gotoblas == &gotoblas_PENRYN) return corename[ 8];
  377. if (gotoblas == &gotoblas_DUNNINGTON) return corename[ 9];
  378. if (gotoblas == &gotoblas_NEHALEM) return corename[10];
  379. if (gotoblas == &gotoblas_ATHLON) return corename[11];
  380. if (gotoblas == &gotoblas_OPTERON_SSE3) return corename[12];
  381. if (gotoblas == &gotoblas_OPTERON) return corename[13];
  382. if (gotoblas == &gotoblas_BARCELONA) return corename[14];
  383. if (gotoblas == &gotoblas_NANO) return corename[15];
  384. if (gotoblas == &gotoblas_SANDYBRIDGE) return corename[16];
  385. if (gotoblas == &gotoblas_BOBCAT) return corename[17];
  386. if (gotoblas == &gotoblas_BULLDOZER) return corename[18];
  387. if (gotoblas == &gotoblas_PILEDRIVER) return corename[19];
  388. if (gotoblas == &gotoblas_HASWELL) return corename[20];
  389. if (gotoblas == &gotoblas_STEAMROLLER) return corename[21];
  390. if (gotoblas == &gotoblas_EXCAVATOR) return corename[22];
  391. return corename[0];
  392. }
  393. static gotoblas_t *force_coretype(char *coretype){
  394. int i ;
  395. int found = -1;
  396. char message[128];
  397. //char mname[20];
  398. for ( i=1 ; i <= 22; i++)
  399. {
  400. if (!strncasecmp(coretype,corename[i],20))
  401. {
  402. found = i;
  403. break;
  404. }
  405. }
  406. if (found < 0)
  407. {
  408. //strncpy(mname,coretype,20);
  409. snprintf(message, 128, "Core not found: %s\n",coretype);
  410. openblas_warning(1, message);
  411. return(NULL);
  412. }
  413. switch (found)
  414. {
  415. case 22: return (&gotoblas_EXCAVATOR);
  416. case 21: return (&gotoblas_STEAMROLLER);
  417. case 20: return (&gotoblas_HASWELL);
  418. case 19: return (&gotoblas_PILEDRIVER);
  419. case 18: return (&gotoblas_BULLDOZER);
  420. case 17: return (&gotoblas_BOBCAT);
  421. case 16: return (&gotoblas_SANDYBRIDGE);
  422. case 15: return (&gotoblas_NANO);
  423. case 14: return (&gotoblas_BARCELONA);
  424. case 13: return (&gotoblas_OPTERON);
  425. case 12: return (&gotoblas_OPTERON_SSE3);
  426. case 11: return (&gotoblas_ATHLON);
  427. case 10: return (&gotoblas_NEHALEM);
  428. case 9: return (&gotoblas_DUNNINGTON);
  429. case 8: return (&gotoblas_PENRYN);
  430. case 7: return (&gotoblas_CORE2);
  431. case 6: return (&gotoblas_ATOM);
  432. case 5: return (&gotoblas_BANIAS);
  433. case 4: return (&gotoblas_PRESCOTT);
  434. case 3: return (&gotoblas_NORTHWOOD);
  435. case 2: return (&gotoblas_COPPERMINE);
  436. case 1: return (&gotoblas_KATMAI);
  437. }
  438. return(NULL);
  439. }
  440. void gotoblas_dynamic_init(void) {
  441. char coremsg[128];
  442. char coren[22];
  443. char *p;
  444. if (gotoblas) return;
  445. p = getenv("OPENBLAS_CORETYPE");
  446. if ( p )
  447. {
  448. gotoblas = force_coretype(p);
  449. }
  450. else
  451. {
  452. gotoblas = get_coretype();
  453. }
  454. #ifdef ARCH_X86
  455. if (gotoblas == NULL) gotoblas = &gotoblas_KATMAI;
  456. #else
  457. if (gotoblas == NULL) gotoblas = &gotoblas_PRESCOTT;
  458. /* sanity check, if 64bit pointer we can't have a 32 bit cpu */
  459. if (sizeof(void*) == 8) {
  460. if (gotoblas == &gotoblas_KATMAI ||
  461. gotoblas == &gotoblas_COPPERMINE ||
  462. gotoblas == &gotoblas_NORTHWOOD ||
  463. gotoblas == &gotoblas_BANIAS ||
  464. gotoblas == &gotoblas_ATHLON)
  465. gotoblas = &gotoblas_PRESCOTT;
  466. }
  467. #endif
  468. if (gotoblas && gotoblas -> init) {
  469. strncpy(coren,gotoblas_corename(),20);
  470. sprintf(coremsg, "Core: %s\n",coren);
  471. openblas_warning(2, coremsg);
  472. gotoblas -> init();
  473. } else {
  474. openblas_warning(0, "OpenBLAS : Architecture Initialization failed. No initialization function found.\n");
  475. exit(1);
  476. }
  477. }
  478. void gotoblas_dynamic_quit(void) {
  479. gotoblas = NULL;
  480. }