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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219
  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. #define VENDOR_INTEL 1
  60. #define VENDOR_AMD 2
  61. #define VENDOR_CENTAUR 3
  62. #define VENDOR_UNKNOWN 99
  63. #define BITMASK(a, b, c) ((((a) >> (b)) & (c)))
  64. static int get_vendor(void){
  65. int eax, ebx, ecx, edx;
  66. char vendor[13];
  67. cpuid(0, &eax, &ebx, &ecx, &edx);
  68. *(int *)(&vendor[0]) = ebx;
  69. *(int *)(&vendor[4]) = edx;
  70. *(int *)(&vendor[8]) = ecx;
  71. vendor[12] = (char)0;
  72. if (!strcmp(vendor, "GenuineIntel")) return VENDOR_INTEL;
  73. if (!strcmp(vendor, "AuthenticAMD")) return VENDOR_AMD;
  74. if (!strcmp(vendor, "CentaurHauls")) return VENDOR_CENTAUR;
  75. if ((eax == 0) || ((eax & 0x500) != 0)) return VENDOR_INTEL;
  76. return VENDOR_UNKNOWN;
  77. }
  78. static gotoblas_t *get_coretype(void){
  79. int eax, ebx, ecx, edx;
  80. int family, exfamily, model, vendor, exmodel;
  81. cpuid(1, &eax, &ebx, &ecx, &edx);
  82. family = BITMASK(eax, 8, 0x0f);
  83. exfamily = BITMASK(eax, 20, 0xff);
  84. model = BITMASK(eax, 4, 0x0f);
  85. exmodel = BITMASK(eax, 16, 0x0f);
  86. vendor = get_vendor();
  87. if (vendor == VENDOR_INTEL){
  88. switch (family) {
  89. case 0x6:
  90. switch (exmodel) {
  91. case 0:
  92. if (model <= 0x7) return &gotoblas_KATMAI;
  93. if ((model == 0x8) || (model == 0xa) || (model == 0xb)) return &gotoblas_COPPERMINE;
  94. if ((model == 0x9) || (model == 0xd)) return &gotoblas_BANIAS;
  95. if (model == 14) return &gotoblas_BANIAS;
  96. if (model == 15) return &gotoblas_CORE2;
  97. return NULL;
  98. case 1:
  99. if (model == 6) return &gotoblas_CORE2;
  100. if (model == 7) return &gotoblas_PENRYN;
  101. if (model == 13) return &gotoblas_DUNNINGTON;
  102. if ((model == 10) || (model == 11) || (model == 14) || (model == 15)) return &gotoblas_NEHALEM;
  103. if (model == 12) return &gotoblas_ATOM;
  104. return NULL;
  105. }
  106. case 0xf:
  107. if (model <= 0x2) return &gotoblas_NORTHWOOD;
  108. return &gotoblas_PRESCOTT;
  109. }
  110. }
  111. if (vendor == VENDOR_AMD){
  112. if (family <= 0xe) return &gotoblas_ATHLON;
  113. if (family == 0xf){
  114. if ((exfamily == 0) || (exfamily == 2)) {
  115. if (ecx & (1 << 0)) return &gotoblas_OPTERON_SSE3;
  116. else return &gotoblas_OPTERON;
  117. } else {
  118. return &gotoblas_BARCELONA;
  119. }
  120. }
  121. }
  122. if (vendor == VENDOR_CENTAUR) {
  123. switch (family) {
  124. case 0x6:
  125. return &gotoblas_NANO;
  126. break;
  127. }
  128. }
  129. return NULL;
  130. }
  131. static char *corename[] = {
  132. "Unknown",
  133. "Katmai",
  134. "Coppermine",
  135. "Northwood",
  136. "Prescott",
  137. "Banias",
  138. "Atom",
  139. "Core2",
  140. "Penryn",
  141. "Dunnington",
  142. "Nehalem",
  143. "Athlon",
  144. "Opteron",
  145. "Opteron(SSE3)",
  146. "Barcelona",
  147. "Nano",
  148. };
  149. char *gotoblas_corename(void) {
  150. if (gotoblas == &gotoblas_KATMAI) return corename[ 1];
  151. if (gotoblas == &gotoblas_COPPERMINE) return corename[ 2];
  152. if (gotoblas == &gotoblas_NORTHWOOD) return corename[ 3];
  153. if (gotoblas == &gotoblas_PRESCOTT) return corename[ 4];
  154. if (gotoblas == &gotoblas_BANIAS) return corename[ 5];
  155. if (gotoblas == &gotoblas_ATOM) return corename[ 6];
  156. if (gotoblas == &gotoblas_CORE2) return corename[ 7];
  157. if (gotoblas == &gotoblas_PENRYN) return corename[ 8];
  158. if (gotoblas == &gotoblas_DUNNINGTON) return corename[ 9];
  159. if (gotoblas == &gotoblas_NEHALEM) return corename[10];
  160. if (gotoblas == &gotoblas_ATHLON) return corename[11];
  161. if (gotoblas == &gotoblas_OPTERON_SSE3) return corename[12];
  162. if (gotoblas == &gotoblas_OPTERON) return corename[13];
  163. if (gotoblas == &gotoblas_BARCELONA) return corename[14];
  164. if (gotoblas == &gotoblas_NANO) return corename[15];
  165. return corename[0];
  166. }
  167. void gotoblas_dynamic_init(void) {
  168. if (gotoblas) return;
  169. gotoblas = get_coretype();
  170. #ifdef ARCH_X86
  171. if (gotoblas == NULL) gotoblas = &gotoblas_KATMAI;
  172. #else
  173. if (gotoblas == NULL) gotoblas = &gotoblas_PRESCOTT;
  174. #endif
  175. if (gotoblas && gotoblas -> init) {
  176. gotoblas -> init();
  177. } else {
  178. fprintf(stderr, "GotoBLAS : Architecture Initialization failed. No initialization function found.\n");
  179. exit(1);
  180. }
  181. }
  182. void gotoblas_dynamic_quit(void) {
  183. gotoblas = NULL;
  184. }

OpenBLAS is an optimized BLAS library based on GotoBLAS2 1.13 BSD version.

Contributors (1)