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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303
  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. #else
  64. //Use NEHALEM kernels for sandy bridge
  65. #define gotoblas_SANDYBRIDGE gotoblas_NEHALEM
  66. #define gotoblas_BULLDOZER gotoblas_BARCELONA
  67. #endif
  68. #define VENDOR_INTEL 1
  69. #define VENDOR_AMD 2
  70. #define VENDOR_CENTAUR 3
  71. #define VENDOR_UNKNOWN 99
  72. #define BITMASK(a, b, c) ((((a) >> (b)) & (c)))
  73. #ifndef NO_AVX
  74. static inline void xgetbv(int op, int * eax, int * edx){
  75. __asm__ __volatile__
  76. ("xgetbv": "=a" (*eax), "=d" (*edx) : "c" (op) : "cc");
  77. }
  78. #endif
  79. int support_avx(){
  80. #ifndef NO_AVX
  81. int eax, ebx, ecx, edx;
  82. int ret=0;
  83. cpuid(1, &eax, &ebx, &ecx, &edx);
  84. if ((ecx & (1 << 28)) != 0 && (ecx & (1 << 27)) != 0){
  85. xgetbv(0, &eax, &edx);
  86. if((eax & 6) == 6){
  87. ret=1; //OS support AVX
  88. }
  89. }
  90. return ret;
  91. #else
  92. return 0;
  93. #endif
  94. }
  95. static int get_vendor(void){
  96. int eax, ebx, ecx, edx;
  97. char vendor[13];
  98. cpuid(0, &eax, &ebx, &ecx, &edx);
  99. *(int *)(&vendor[0]) = ebx;
  100. *(int *)(&vendor[4]) = edx;
  101. *(int *)(&vendor[8]) = ecx;
  102. vendor[12] = (char)0;
  103. if (!strcmp(vendor, "GenuineIntel")) return VENDOR_INTEL;
  104. if (!strcmp(vendor, "AuthenticAMD")) return VENDOR_AMD;
  105. if (!strcmp(vendor, "CentaurHauls")) return VENDOR_CENTAUR;
  106. if ((eax == 0) || ((eax & 0x500) != 0)) return VENDOR_INTEL;
  107. return VENDOR_UNKNOWN;
  108. }
  109. static gotoblas_t *get_coretype(void){
  110. int eax, ebx, ecx, edx;
  111. int family, exfamily, model, vendor, exmodel;
  112. cpuid(1, &eax, &ebx, &ecx, &edx);
  113. family = BITMASK(eax, 8, 0x0f);
  114. exfamily = BITMASK(eax, 20, 0xff);
  115. model = BITMASK(eax, 4, 0x0f);
  116. exmodel = BITMASK(eax, 16, 0x0f);
  117. vendor = get_vendor();
  118. if (vendor == VENDOR_INTEL){
  119. switch (family) {
  120. case 0x6:
  121. switch (exmodel) {
  122. case 0:
  123. if (model <= 0x7) return &gotoblas_KATMAI;
  124. if ((model == 0x8) || (model == 0xa) || (model == 0xb)) return &gotoblas_COPPERMINE;
  125. if ((model == 0x9) || (model == 0xd)) return &gotoblas_BANIAS;
  126. if (model == 14) return &gotoblas_BANIAS;
  127. if (model == 15) return &gotoblas_CORE2;
  128. return NULL;
  129. case 1:
  130. if (model == 6) return &gotoblas_CORE2;
  131. if (model == 7) return &gotoblas_PENRYN;
  132. if (model == 13) return &gotoblas_DUNNINGTON;
  133. if ((model == 10) || (model == 11) || (model == 14) || (model == 15)) return &gotoblas_NEHALEM;
  134. if (model == 12) return &gotoblas_ATOM;
  135. return NULL;
  136. case 2:
  137. //Intel Core (Clarkdale) / Core (Arrandale)
  138. // Pentium (Clarkdale) / Pentium Mobile (Arrandale)
  139. // Xeon (Clarkdale), 32nm
  140. if (model == 5) return &gotoblas_NEHALEM;
  141. //Intel Xeon Processor 5600 (Westmere-EP)
  142. //Xeon Processor E7 (Westmere-EX)
  143. if (model == 12 || model == 15) return &gotoblas_NEHALEM;
  144. //Intel Core i5-2000 /i7-2000 (Sandy Bridge)
  145. //Intel Core i7-3000 / Xeon E5
  146. if (model == 10 || model == 13) {
  147. if(support_avx())
  148. return &gotoblas_SANDYBRIDGE;
  149. else{
  150. fprintf(stderr, "OpenBLAS : Your OS doesn't support AVX. Use Nehalem kernels.\n");
  151. return &gotoblas_NEHALEM; //OS doesn't support AVX. Use old kernels.
  152. }
  153. }
  154. return NULL;
  155. case 3:
  156. //Intel Sandy Bridge 22nm (Ivy Bridge?)
  157. if (model == 10) {
  158. if(support_avx())
  159. return &gotoblas_SANDYBRIDGE;
  160. else{
  161. fprintf(stderr, "OpenBLAS : Your OS doesn't support AVX. Use Nehalem kernels.\n");
  162. return &gotoblas_NEHALEM; //OS doesn't support AVX. Use old kernels.
  163. }
  164. }
  165. return NULL;
  166. }
  167. case 0xf:
  168. if (model <= 0x2) return &gotoblas_NORTHWOOD;
  169. return &gotoblas_PRESCOTT;
  170. }
  171. }
  172. if (vendor == VENDOR_AMD){
  173. if (family <= 0xe) return &gotoblas_ATHLON;
  174. if (family == 0xf){
  175. if ((exfamily == 0) || (exfamily == 2)) {
  176. if (ecx & (1 << 0)) return &gotoblas_OPTERON_SSE3;
  177. else return &gotoblas_OPTERON;
  178. } else if (exfamily == 5) {
  179. return &gotoblas_BOBCAT;
  180. } else if (exfamily == 6) {
  181. //AMD Bulldozer Opteron 6200 / Opteron 4200 / AMD FX-Series
  182. if(support_avx())
  183. return &gotoblas_BULLDOZER;
  184. else{
  185. fprintf(stderr, "OpenBLAS : Your OS doesn't support AVX. Use Barcelona kernels.\n");
  186. return &gotoblas_BARCELONA; //OS doesn't support AVX. Use old kernels.
  187. }
  188. } else {
  189. return &gotoblas_BARCELONA;
  190. }
  191. }
  192. }
  193. if (vendor == VENDOR_CENTAUR) {
  194. switch (family) {
  195. case 0x6:
  196. return &gotoblas_NANO;
  197. break;
  198. }
  199. }
  200. return NULL;
  201. }
  202. static char *corename[] = {
  203. "Unknown",
  204. "Katmai",
  205. "Coppermine",
  206. "Northwood",
  207. "Prescott",
  208. "Banias",
  209. "Atom",
  210. "Core2",
  211. "Penryn",
  212. "Dunnington",
  213. "Nehalem",
  214. "Athlon",
  215. "Opteron",
  216. "Opteron(SSE3)",
  217. "Barcelona",
  218. "Nano",
  219. "Sandybridge",
  220. "Bobcat",
  221. "Bulldozer",
  222. };
  223. char *gotoblas_corename(void) {
  224. if (gotoblas == &gotoblas_KATMAI) return corename[ 1];
  225. if (gotoblas == &gotoblas_COPPERMINE) return corename[ 2];
  226. if (gotoblas == &gotoblas_NORTHWOOD) return corename[ 3];
  227. if (gotoblas == &gotoblas_PRESCOTT) return corename[ 4];
  228. if (gotoblas == &gotoblas_BANIAS) return corename[ 5];
  229. if (gotoblas == &gotoblas_ATOM) return corename[ 6];
  230. if (gotoblas == &gotoblas_CORE2) return corename[ 7];
  231. if (gotoblas == &gotoblas_PENRYN) return corename[ 8];
  232. if (gotoblas == &gotoblas_DUNNINGTON) return corename[ 9];
  233. if (gotoblas == &gotoblas_NEHALEM) return corename[10];
  234. if (gotoblas == &gotoblas_ATHLON) return corename[11];
  235. if (gotoblas == &gotoblas_OPTERON_SSE3) return corename[12];
  236. if (gotoblas == &gotoblas_OPTERON) return corename[13];
  237. if (gotoblas == &gotoblas_BARCELONA) return corename[14];
  238. if (gotoblas == &gotoblas_NANO) return corename[15];
  239. if (gotoblas == &gotoblas_SANDYBRIDGE) return corename[16];
  240. if (gotoblas == &gotoblas_BOBCAT) return corename[17];
  241. if (gotoblas == &gotoblas_BULLDOZER) return corename[18];
  242. return corename[0];
  243. }
  244. void gotoblas_dynamic_init(void) {
  245. if (gotoblas) return;
  246. gotoblas = get_coretype();
  247. #ifdef ARCH_X86
  248. if (gotoblas == NULL) gotoblas = &gotoblas_KATMAI;
  249. #else
  250. if (gotoblas == NULL) gotoblas = &gotoblas_PRESCOTT;
  251. #endif
  252. if (gotoblas && gotoblas -> init) {
  253. gotoblas -> init();
  254. } else {
  255. fprintf(stderr, "OpenBLAS : Architecture Initialization failed. No initialization function found.\n");
  256. exit(1);
  257. }
  258. }
  259. void gotoblas_dynamic_quit(void) {
  260. gotoblas = NULL;
  261. }