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.5 kB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199
  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. extern gotoblas_t gotoblas_LOONGSON3R3;
  43. extern gotoblas_t gotoblas_LOONGSON3R4;
  44. extern gotoblas_t gotoblas_MIPS64_GENERIC;
  45. extern void openblas_warning(int verbose, const char * msg);
  46. #define NUM_CORETYPES 3
  47. static char *corename[] = {
  48. "MIPS64_GENERIC"
  49. "loongson3r3",
  50. "loongson3r4",
  51. "UNKNOWN"
  52. };
  53. char *gotoblas_corename(void) {
  54. if (gotoblas == &gotoblas_MIPS64_GENERIC) return corename[0];
  55. if (gotoblas == &gotoblas_LOONGSON3R3) return corename[1];
  56. if (gotoblas == &gotoblas_LOONGSON3R4) return corename[2];
  57. return corename[NUM_CORETYPES];
  58. }
  59. static gotoblas_t *force_coretype(char *coretype) {
  60. int i;
  61. int found = -1;
  62. char message[128];
  63. for ( i=0 ; i < NUM_CORETYPES; i++)
  64. {
  65. if (!strncasecmp(coretype, corename[i], 20))
  66. {
  67. found = i;
  68. break;
  69. }
  70. }
  71. switch (found)
  72. {
  73. case 0: return (&gotoblas_MIPS64_GENERIC);
  74. case 1: return (&gotoblas_LOONGSON3R3);
  75. case 2: return (&gotoblas_LOONGSON3R4);
  76. }
  77. snprintf(message, 128, "Core not found: %s\n", coretype);
  78. openblas_warning(1, message);
  79. return NULL;
  80. }
  81. #if (defined OS_LINUX || defined OS_ANDROID)
  82. #define MMI_MASK 0x00000010
  83. #define MSA_MASK 0x00000020
  84. static gotoblas_t *get_coretype_from_cpucfg(void) {
  85. int flag = 0;
  86. __asm__ volatile(
  87. ".set push \n\t"
  88. ".set noat \n\t"
  89. ".insn \n\t"
  90. "dli $1, 0x01 \n\t"
  91. ".word (0xc8080118) \n\t"
  92. "move %0, $1 \n\t"
  93. ".set pop \n\t"
  94. : "=r"(flag)
  95. :
  96. :
  97. );
  98. if (flag & MSA_MASK)
  99. return (&gotoblas_LOONGSON3R4);
  100. if (flag & MMI_MASK)
  101. return (&gotoblas_LOONGSON3R3);
  102. return NULL;
  103. }
  104. static gotoblas_t *get_coretype_from_cpuinfo(void) {
  105. #ifdef __linux
  106. FILE *infile;
  107. char buffer[512], *p;
  108. p = (char *)NULL;
  109. //Check model name for Loongson3
  110. infile = fopen("/proc/cpuinfo", "r");
  111. while (fgets(buffer, sizeof(buffer), infile)){
  112. if (!strncmp("model name", buffer, 10)){
  113. p = strchr(buffer, ':') + 2;
  114. break;
  115. }
  116. }
  117. fclose(infile);
  118. if(p != NULL){
  119. if (strstr(p, "Loongson-3A3000") || strstr(p, "Loongson-3B3000"))
  120. return (&gotoblas_LOONGSON3R3);
  121. else if(strstr(p, "Loongson-3A4000") || strstr(p, "Loongson-3B4000"))
  122. return (&gotoblas_LOONGSON3R4);
  123. else
  124. return NULL;
  125. }
  126. #endif
  127. return NULL;
  128. }
  129. #endif
  130. static gotoblas_t *get_coretype(void) {
  131. #if (!defined OS_LINUX && !defined OS_ANDROID)
  132. return NULL;
  133. #else
  134. if (!(getauxval(AT_HWCAP) & HWCAP_LOONGSON_CPUCFG))
  135. return get_coretype_from_cpucfg();
  136. else
  137. return get_coretype_from_cpuinfo();
  138. #endif
  139. }
  140. void gotoblas_dynamic_init(void) {
  141. char coremsg[128];
  142. char coren[22];
  143. char *p;
  144. if (gotoblas) return;
  145. p = getenv("OPENBLAS_CORETYPE");
  146. if ( p )
  147. {
  148. gotoblas = force_coretype(p);
  149. }
  150. else
  151. {
  152. gotoblas = get_coretype();
  153. }
  154. if (gotoblas == NULL)
  155. {
  156. snprintf(coremsg, 128, "Falling back to MIPS64_GENEIRC\n");
  157. openblas_warning(1, coremsg);
  158. gotoblas = &gotoblas_MIPS64_GENERIC;
  159. }
  160. if (gotoblas && gotoblas->init) {
  161. strncpy(coren, gotoblas_corename(), 20);
  162. sprintf(coremsg, "Core: %s\n", coren);
  163. openblas_warning(2, coremsg);
  164. gotoblas -> init();
  165. } else {
  166. openblas_warning(0, "OpenBLAS : Architecture Initialization failed. No initialization function found.\n");
  167. exit(1);
  168. }
  169. }
  170. void gotoblas_dynamic_quit(void) {
  171. gotoblas = NULL;
  172. }