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_mips64.c 5.9 kB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213
  1. /*****************************************************************************
  2. Copyright (c) 2020, The OpenBLAS Project
  3. All rights reserved.
  4. Redistribution and use in source and binary forms, with or without
  5. modification, are permitted provided that the following conditions are
  6. met:
  7. 1. Redistributions of source code must retain the above copyright
  8. notice, this list of conditions and the following disclaimer.
  9. 2. Redistributions in binary form must reproduce the above copyright
  10. notice, this list of conditions and the following disclaimer in
  11. the documentation and/or other materials provided with the
  12. distribution.
  13. 3. Neither the name of the OpenBLAS project nor the names of
  14. its contributors may be used to endorse or promote products
  15. derived from this software without specific prior written
  16. permission.
  17. THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
  18. AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  19. IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  20. ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
  21. LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
  22. DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
  23. SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
  24. CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
  25. OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE
  26. USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  27. **********************************************************************************/
  28. #include <sys/wait.h>
  29. #include <stdio.h>
  30. #include <unistd.h>
  31. #include <stdlib.h>
  32. #include <string.h>
  33. #include <sys/resource.h>
  34. #include "common.h"
  35. #if (defined OS_LINUX || defined OS_ANDROID)
  36. #include <asm/hwcap.h>
  37. #include <sys/auxv.h>
  38. #ifndef HWCAP_LOONGSON_CPUCFG
  39. #define HWCAP_LOONGSON_CPUCFG (1 << 14)
  40. #endif
  41. #endif
  42. #ifdef DYNAMIC_LIST
  43. extern gotoblas_t gotoblas_MIPS64_GENERIC;
  44. #ifdef DYN_LOONGSON3R3
  45. extern gotoblas_t gotoblas_LOONGSON3R3;
  46. #else
  47. #define gotoblas_LOONGSON3R3 gotoblas_MIPS64_GENERIC
  48. #endif
  49. #ifdef DYN_LOONGSON3R4
  50. extern gotoblas_t gotoblas_LOONGSON3R4;
  51. #else
  52. #define gotoblas_LOONGSON3R4 gotoblas_MIPS64_GENERIC
  53. #endif
  54. #else
  55. extern gotoblas_t gotoblas_LOONGSON3R3;
  56. extern gotoblas_t gotoblas_LOONGSON3R4;
  57. extern gotoblas_t gotoblas_MIPS64_GENERIC;
  58. #endif
  59. extern void openblas_warning(int verbose, const char * msg);
  60. #define NUM_CORETYPES 3
  61. static char *corename[] = {
  62. "MIPS64_GENERIC"
  63. "loongson3r3",
  64. "loongson3r4",
  65. "UNKNOWN"
  66. };
  67. char *gotoblas_corename(void) {
  68. if (gotoblas == &gotoblas_MIPS64_GENERIC) return corename[0];
  69. if (gotoblas == &gotoblas_LOONGSON3R3) return corename[1];
  70. if (gotoblas == &gotoblas_LOONGSON3R4) return corename[2];
  71. return corename[NUM_CORETYPES];
  72. }
  73. static gotoblas_t *force_coretype(char *coretype) {
  74. int i;
  75. int found = -1;
  76. char message[128];
  77. for ( i=0 ; i < NUM_CORETYPES; i++)
  78. {
  79. if (!strncasecmp(coretype, corename[i], 20))
  80. {
  81. found = i;
  82. break;
  83. }
  84. }
  85. switch (found)
  86. {
  87. case 0: return (&gotoblas_MIPS64_GENERIC);
  88. case 1: return (&gotoblas_LOONGSON3R3);
  89. case 2: return (&gotoblas_LOONGSON3R4);
  90. }
  91. snprintf(message, 128, "Core not found: %s\n", coretype);
  92. openblas_warning(1, message);
  93. return NULL;
  94. }
  95. #if (defined OS_LINUX || defined OS_ANDROID)
  96. #define MMI_MASK 0x00000010
  97. #define MSA_MASK 0x00000020
  98. static gotoblas_t *get_coretype_from_cpucfg(void) {
  99. int flag = 0;
  100. __asm__ volatile(
  101. ".set push \n\t"
  102. ".set noat \n\t"
  103. ".insn \n\t"
  104. "dli $1, 0x01 \n\t"
  105. ".word (0xc8080118) \n\t"
  106. "move %0, $1 \n\t"
  107. ".set pop \n\t"
  108. : "=r"(flag)
  109. :
  110. :
  111. );
  112. if (flag & MSA_MASK)
  113. return (&gotoblas_LOONGSON3R4);
  114. if (flag & MMI_MASK)
  115. return (&gotoblas_LOONGSON3R3);
  116. return NULL;
  117. }
  118. static gotoblas_t *get_coretype_from_cpuinfo(void) {
  119. #ifdef __linux
  120. FILE *infile;
  121. char buffer[512], *p;
  122. p = (char *)NULL;
  123. //Check model name for Loongson3
  124. infile = fopen("/proc/cpuinfo", "r");
  125. while (fgets(buffer, sizeof(buffer), infile)){
  126. if (!strncmp("model name", buffer, 10)){
  127. p = strchr(buffer, ':') + 2;
  128. break;
  129. }
  130. }
  131. fclose(infile);
  132. if(p != NULL){
  133. if (strstr(p, "Loongson-3A3000") || strstr(p, "Loongson-3B3000"))
  134. return (&gotoblas_LOONGSON3R3);
  135. else if(strstr(p, "Loongson-3A4000") || strstr(p, "Loongson-3B4000"))
  136. return (&gotoblas_LOONGSON3R4);
  137. else
  138. return NULL;
  139. }
  140. #endif
  141. return NULL;
  142. }
  143. #endif
  144. static gotoblas_t *get_coretype(void) {
  145. #if (!defined OS_LINUX && !defined OS_ANDROID)
  146. return NULL;
  147. #else
  148. if (!(getauxval(AT_HWCAP) & HWCAP_LOONGSON_CPUCFG))
  149. return get_coretype_from_cpucfg();
  150. else
  151. return get_coretype_from_cpuinfo();
  152. #endif
  153. }
  154. void gotoblas_dynamic_init(void) {
  155. char coremsg[128];
  156. char coren[22];
  157. char *p;
  158. if (gotoblas) return;
  159. p = getenv("OPENBLAS_CORETYPE");
  160. if ( p )
  161. {
  162. gotoblas = force_coretype(p);
  163. }
  164. else
  165. {
  166. gotoblas = get_coretype();
  167. }
  168. if (gotoblas == NULL)
  169. {
  170. snprintf(coremsg, 128, "Falling back to MIPS64_GENEIRC\n");
  171. openblas_warning(1, coremsg);
  172. gotoblas = &gotoblas_MIPS64_GENERIC;
  173. }
  174. if (gotoblas && gotoblas->init) {
  175. strncpy(coren, gotoblas_corename(), 20);
  176. sprintf(coremsg, "Core: %s\n", coren);
  177. openblas_warning(2, coremsg);
  178. gotoblas -> init();
  179. } else {
  180. openblas_warning(0, "OpenBLAS : Architecture Initialization failed. No initialization function found.\n");
  181. exit(1);
  182. }
  183. }
  184. void gotoblas_dynamic_quit(void) {
  185. gotoblas = NULL;
  186. }