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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364
  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_HASWELL;
  65. #else
  66. //Use NEHALEM kernels for sandy bridge
  67. #define gotoblas_SANDYBRIDGE gotoblas_NEHALEM
  68. #define gotoblas_HASWELL gotoblas_NEHALEM
  69. #define gotoblas_BULLDOZER gotoblas_BARCELONA
  70. #define gotoblas_PILEDRIVER gotoblas_BARCELONA
  71. #endif
  72. #define VENDOR_INTEL 1
  73. #define VENDOR_AMD 2
  74. #define VENDOR_CENTAUR 3
  75. #define VENDOR_UNKNOWN 99
  76. #define BITMASK(a, b, c) ((((a) >> (b)) & (c)))
  77. #ifndef NO_AVX
  78. static inline void xgetbv(int op, int * eax, int * edx){
  79. //Use binary code for xgetbv
  80. __asm__ __volatile__
  81. (".byte 0x0f, 0x01, 0xd0": "=a" (*eax), "=d" (*edx) : "c" (op) : "cc");
  82. }
  83. #endif
  84. int support_avx(){
  85. #ifndef NO_AVX
  86. int eax, ebx, ecx, edx;
  87. int ret=0;
  88. cpuid(1, &eax, &ebx, &ecx, &edx);
  89. if ((ecx & (1 << 28)) != 0 && (ecx & (1 << 27)) != 0 && (ecx & (1 << 26)) != 0){
  90. xgetbv(0, &eax, &edx);
  91. if((eax & 6) == 6){
  92. ret=1; //OS support AVX
  93. }
  94. }
  95. return ret;
  96. #else
  97. return 0;
  98. #endif
  99. }
  100. static int get_vendor(void){
  101. int eax, ebx, ecx, edx;
  102. char vendor[13];
  103. cpuid(0, &eax, &ebx, &ecx, &edx);
  104. *(int *)(&vendor[0]) = ebx;
  105. *(int *)(&vendor[4]) = edx;
  106. *(int *)(&vendor[8]) = ecx;
  107. vendor[12] = (char)0;
  108. if (!strcmp(vendor, "GenuineIntel")) return VENDOR_INTEL;
  109. if (!strcmp(vendor, "AuthenticAMD")) return VENDOR_AMD;
  110. if (!strcmp(vendor, "CentaurHauls")) return VENDOR_CENTAUR;
  111. if ((eax == 0) || ((eax & 0x500) != 0)) return VENDOR_INTEL;
  112. return VENDOR_UNKNOWN;
  113. }
  114. static gotoblas_t *get_coretype(void){
  115. int eax, ebx, ecx, edx;
  116. int family, exfamily, model, vendor, exmodel;
  117. cpuid(1, &eax, &ebx, &ecx, &edx);
  118. family = BITMASK(eax, 8, 0x0f);
  119. exfamily = BITMASK(eax, 20, 0xff);
  120. model = BITMASK(eax, 4, 0x0f);
  121. exmodel = BITMASK(eax, 16, 0x0f);
  122. vendor = get_vendor();
  123. if (vendor == VENDOR_INTEL){
  124. switch (family) {
  125. case 0x6:
  126. switch (exmodel) {
  127. case 0:
  128. if (model <= 0x7) return &gotoblas_KATMAI;
  129. if ((model == 0x8) || (model == 0xa) || (model == 0xb)) return &gotoblas_COPPERMINE;
  130. if ((model == 0x9) || (model == 0xd)) return &gotoblas_BANIAS;
  131. if (model == 14) return &gotoblas_BANIAS;
  132. if (model == 15) return &gotoblas_CORE2;
  133. return NULL;
  134. case 1:
  135. if (model == 6) return &gotoblas_CORE2;
  136. if (model == 7) return &gotoblas_PENRYN;
  137. if (model == 13) return &gotoblas_DUNNINGTON;
  138. if ((model == 10) || (model == 11) || (model == 14) || (model == 15)) return &gotoblas_NEHALEM;
  139. if (model == 12) return &gotoblas_ATOM;
  140. return NULL;
  141. case 2:
  142. //Intel Core (Clarkdale) / Core (Arrandale)
  143. // Pentium (Clarkdale) / Pentium Mobile (Arrandale)
  144. // Xeon (Clarkdale), 32nm
  145. if (model == 5) return &gotoblas_NEHALEM;
  146. //Intel Xeon Processor 5600 (Westmere-EP)
  147. //Xeon Processor E7 (Westmere-EX)
  148. //Xeon E7540
  149. if (model == 12 || model == 14 || model == 15) return &gotoblas_NEHALEM;
  150. //Intel Core i5-2000 /i7-2000 (Sandy Bridge)
  151. //Intel Core i7-3000 / Xeon E5
  152. if (model == 10 || model == 13) {
  153. if(support_avx())
  154. return &gotoblas_SANDYBRIDGE;
  155. else{
  156. fprintf(stderr, "OpenBLAS : Your OS does not support AVX instructions. OpenBLAS is using Nehalem kernels as a fallback, which may give poorer performance.\n");
  157. return &gotoblas_NEHALEM; //OS doesn't support AVX. Use old kernels.
  158. }
  159. }
  160. return NULL;
  161. case 3:
  162. //Intel Sandy Bridge 22nm (Ivy Bridge?)
  163. if (model == 10 || model == 14) {
  164. if(support_avx())
  165. return &gotoblas_SANDYBRIDGE;
  166. else{
  167. fprintf(stderr, "OpenBLAS : Your OS does not support AVX instructions. OpenBLAS is using Nehalem kernels as a fallback, which may give poorer performance.\n");
  168. return &gotoblas_NEHALEM; //OS doesn't support AVX. Use old kernels.
  169. }
  170. }
  171. //Intel Haswell
  172. if (model == 12 || model == 15) {
  173. if(support_avx())
  174. return &gotoblas_HASWELL;
  175. else{
  176. fprintf(stderr, "OpenBLAS : Your OS does not support AVX instructions. OpenBLAS is using Nehalem kernels as a fallback, which may give poorer performance.\n");
  177. return &gotoblas_NEHALEM; //OS doesn't support AVX. Use old kernels.
  178. }
  179. }
  180. return NULL;
  181. case 4:
  182. //Intel Haswell
  183. if (model == 5 || model == 6) {
  184. if(support_avx())
  185. return &gotoblas_HASWELL;
  186. else{
  187. fprintf(stderr, "OpenBLAS : Your OS does not support AVX instructions. OpenBLAS is using Nehalem kernels as a fallback, which may give poorer performance.\n");
  188. return &gotoblas_NEHALEM; //OS doesn't support AVX. Use old kernels.
  189. }
  190. }
  191. return NULL;
  192. }
  193. case 0xf:
  194. if (model <= 0x2) return &gotoblas_NORTHWOOD;
  195. return &gotoblas_PRESCOTT;
  196. }
  197. }
  198. if (vendor == VENDOR_AMD){
  199. if (family <= 0xe) {
  200. // Verify that CPU has 3dnow and 3dnowext before claiming it is Athlon
  201. cpuid(0x80000000, &eax, &ebx, &ecx, &edx);
  202. if (eax & 0xffff >= 0x01) {
  203. cpuid(0x80000001, &eax, &ebx, &ecx, &edx);
  204. if ((edx & (1 << 30)) == 0 || (edx & (1 << 31)) == 0)
  205. return NULL;
  206. }
  207. else
  208. return NULL;
  209. return &gotoblas_ATHLON;
  210. }
  211. if (family == 0xf){
  212. if ((exfamily == 0) || (exfamily == 2)) {
  213. if (ecx & (1 << 0)) return &gotoblas_OPTERON_SSE3;
  214. else return &gotoblas_OPTERON;
  215. } else if (exfamily == 5) {
  216. return &gotoblas_BOBCAT;
  217. } else if (exfamily == 6) {
  218. if(model == 1){
  219. //AMD Bulldozer Opteron 6200 / Opteron 4200 / AMD FX-Series
  220. if(support_avx())
  221. return &gotoblas_BULLDOZER;
  222. else{
  223. fprintf(stderr, "OpenBLAS : Your OS does not support AVX instructions. OpenBLAS is using Barcelona kernels as a fallback, which may give poorer performance.\n");
  224. return &gotoblas_BARCELONA; //OS doesn't support AVX. Use old kernels.
  225. }
  226. }else if(model == 2){
  227. //AMD Bulldozer Opteron 6300 / Opteron 4300 / Opteron 3300
  228. if(support_avx())
  229. return &gotoblas_PILEDRIVER;
  230. else{
  231. fprintf(stderr, "OpenBLAS : Your OS does not support AVX instructions. OpenBLAS is using Barcelona kernels as a fallback, which may give poorer performance.\n");
  232. return &gotoblas_BARCELONA; //OS doesn't support AVX. Use old kernels.
  233. }
  234. }
  235. } else {
  236. return &gotoblas_BARCELONA;
  237. }
  238. }
  239. }
  240. if (vendor == VENDOR_CENTAUR) {
  241. switch (family) {
  242. case 0x6:
  243. return &gotoblas_NANO;
  244. break;
  245. }
  246. }
  247. return NULL;
  248. }
  249. static char *corename[] = {
  250. "Unknown",
  251. "Katmai",
  252. "Coppermine",
  253. "Northwood",
  254. "Prescott",
  255. "Banias",
  256. "Atom",
  257. "Core2",
  258. "Penryn",
  259. "Dunnington",
  260. "Nehalem",
  261. "Athlon",
  262. "Opteron",
  263. "Opteron(SSE3)",
  264. "Barcelona",
  265. "Nano",
  266. "Sandybridge",
  267. "Bobcat",
  268. "Bulldozer",
  269. "Piledriver",
  270. "Haswell",
  271. };
  272. char *gotoblas_corename(void) {
  273. if (gotoblas == &gotoblas_KATMAI) return corename[ 1];
  274. if (gotoblas == &gotoblas_COPPERMINE) return corename[ 2];
  275. if (gotoblas == &gotoblas_NORTHWOOD) return corename[ 3];
  276. if (gotoblas == &gotoblas_PRESCOTT) return corename[ 4];
  277. if (gotoblas == &gotoblas_BANIAS) return corename[ 5];
  278. if (gotoblas == &gotoblas_ATOM) return corename[ 6];
  279. if (gotoblas == &gotoblas_CORE2) return corename[ 7];
  280. if (gotoblas == &gotoblas_PENRYN) return corename[ 8];
  281. if (gotoblas == &gotoblas_DUNNINGTON) return corename[ 9];
  282. if (gotoblas == &gotoblas_NEHALEM) return corename[10];
  283. if (gotoblas == &gotoblas_ATHLON) return corename[11];
  284. if (gotoblas == &gotoblas_OPTERON_SSE3) return corename[12];
  285. if (gotoblas == &gotoblas_OPTERON) return corename[13];
  286. if (gotoblas == &gotoblas_BARCELONA) return corename[14];
  287. if (gotoblas == &gotoblas_NANO) return corename[15];
  288. if (gotoblas == &gotoblas_SANDYBRIDGE) return corename[16];
  289. if (gotoblas == &gotoblas_BOBCAT) return corename[17];
  290. if (gotoblas == &gotoblas_BULLDOZER) return corename[18];
  291. if (gotoblas == &gotoblas_PILEDRIVER) return corename[19];
  292. if (gotoblas == &gotoblas_HASWELL) return corename[20];
  293. return corename[0];
  294. }
  295. void gotoblas_dynamic_init(void) {
  296. if (gotoblas) return;
  297. gotoblas = get_coretype();
  298. #ifdef ARCH_X86
  299. if (gotoblas == NULL) gotoblas = &gotoblas_KATMAI;
  300. #else
  301. if (gotoblas == NULL) gotoblas = &gotoblas_PRESCOTT;
  302. /* sanity check, if 64bit pointer we can't have a 32 bit cpu */
  303. if (sizeof(void*) == 8) {
  304. if (gotoblas == &gotoblas_KATMAI ||
  305. gotoblas == &gotoblas_COPPERMINE ||
  306. gotoblas == &gotoblas_NORTHWOOD ||
  307. gotoblas == &gotoblas_BANIAS ||
  308. gotoblas == &gotoblas_ATHLON)
  309. gotoblas = &gotoblas_PRESCOTT;
  310. }
  311. #endif
  312. if (gotoblas && gotoblas -> init) {
  313. gotoblas -> init();
  314. } else {
  315. fprintf(stderr, "OpenBLAS : Architecture Initialization failed. No initialization function found.\n");
  316. exit(1);
  317. }
  318. }
  319. void gotoblas_dynamic_quit(void) {
  320. gotoblas = NULL;
  321. }