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_loongarch64.c 5.1 kB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185
  1. /*******************************************************************************
  2. Copyright (c) 2022, 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 permission.
  16. THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
  17. AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  18. IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  19. ARE DISCLAIMED. IN NO EVENT SHALL THE OPENBLAS PROJECT OR CONTRIBUTORS BE
  20. LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
  21. DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
  22. SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
  23. CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
  24. OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE
  25. USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  26. *******************************************************************************/
  27. #include <sys/auxv.h>
  28. #include "common.h"
  29. #define NUM_CORETYPES 6
  30. #define LOONGARCH_CFG0 0x00
  31. #define LA_HWCAP_LSX (1U << 4)
  32. #define LA_HWCAP_LASX (1U << 5)
  33. #define PRID_SERIES_MASK 0xf000
  34. #define PRID_SERIES_LA264 0xa000
  35. #define PRID_SERIES_LA364 0xb000
  36. #define PRID_SERIES_LA464 0xc000
  37. #define PRID_SERIES_LA664 0xd000
  38. extern gotoblas_t gotoblas_LA64_GENERIC;
  39. extern gotoblas_t gotoblas_LA264;
  40. extern gotoblas_t gotoblas_LA464;
  41. extern void openblas_warning(int verbose, const char * msg);
  42. static char *corename[] = {
  43. "la64_generic",
  44. "la264",
  45. "la464",
  46. "loongsongeneric",
  47. "loongson2k1000",
  48. "loongson3r5",
  49. "unknown"
  50. };
  51. char *gotoblas_corename(void) {
  52. if (gotoblas == &gotoblas_LA64_GENERIC) return corename[0];
  53. if (gotoblas == &gotoblas_LA264) return corename[1];
  54. if (gotoblas == &gotoblas_LA464) return corename[2];
  55. return corename[NUM_CORETYPES];
  56. }
  57. static gotoblas_t *force_coretype(char *coretype) {
  58. int i;
  59. int found = -1;
  60. char message[128];
  61. for ( i=0 ; i < NUM_CORETYPES; i++)
  62. {
  63. if (!strncasecmp(coretype, corename[i], 20))
  64. {
  65. found = i;
  66. break;
  67. }
  68. }
  69. switch (found)
  70. {
  71. case 0: return (&gotoblas_LA64_GENERIC);
  72. case 1: return (&gotoblas_LA264);
  73. case 2: return (&gotoblas_LA464);
  74. case 3: return (&gotoblas_LA64_GENERIC);
  75. case 4: return (&gotoblas_LA264);
  76. case 5: return (&gotoblas_LA464);
  77. }
  78. snprintf(message, 128, "Core not found: %s\n", coretype);
  79. openblas_warning(1, message);
  80. return NULL;
  81. }
  82. /* Detect whether the OS supports the LASX instruction set */
  83. static int os_support_lasx() {
  84. int hwcap = (int)getauxval(AT_HWCAP);
  85. if (hwcap & LA_HWCAP_LASX)
  86. return 1;
  87. else
  88. return 0;
  89. }
  90. /* Detect whether the OS supports the LSX instruction set */
  91. static int os_support_lsx() {
  92. int hwcap = (int)getauxval(AT_HWCAP);
  93. if (hwcap & LA_HWCAP_LSX)
  94. return 1;
  95. else
  96. return 0;
  97. }
  98. static uint32_t get_prid() {
  99. uint32_t reg = 0;
  100. __asm__ volatile (
  101. "cpucfg %0, %1 \n\t"
  102. : "+&r"(reg)
  103. : "r"(LOONGARCH_CFG0)
  104. );
  105. return reg;
  106. }
  107. /* Select core at runtime based on the
  108. * cpu name and SIMD instructions supported
  109. * by the system
  110. */
  111. static gotoblas_t *get_coretype(void) {
  112. uint32_t prid = get_prid();
  113. switch (prid & PRID_SERIES_MASK) {
  114. case (PRID_SERIES_LA464):
  115. case (PRID_SERIES_LA664):
  116. if (os_support_lasx())
  117. return &gotoblas_LA464;
  118. else if (os_support_lsx())
  119. return &gotoblas_LA264;
  120. else
  121. return &gotoblas_LA64_GENERIC;
  122. break;
  123. case (PRID_SERIES_LA264):
  124. case (PRID_SERIES_LA364):
  125. if (os_support_lsx())
  126. return &gotoblas_LA264;
  127. else
  128. return &gotoblas_LA64_GENERIC;
  129. break;
  130. default:
  131. return &gotoblas_LA64_GENERIC;
  132. break;
  133. }
  134. }
  135. void gotoblas_dynamic_init(void) {
  136. char coremsg[128];
  137. char coren[22];
  138. char *p;
  139. if (gotoblas) return;
  140. p = getenv("OPENBLAS_CORETYPE");
  141. if ( p )
  142. {
  143. gotoblas = force_coretype(p);
  144. }
  145. else
  146. {
  147. gotoblas = get_coretype();
  148. }
  149. if (gotoblas && gotoblas->init) {
  150. strncpy(coren, gotoblas_corename(), 20);
  151. sprintf(coremsg, "Core: %s\n", coren);
  152. openblas_warning(2, coremsg);
  153. gotoblas -> init();
  154. } else {
  155. openblas_warning(0, "OpenBLAS : Architecture Initialization failed. No initialization function found.\n");
  156. exit(1);
  157. }
  158. }
  159. void gotoblas_dynamic_quit(void) {
  160. gotoblas = NULL;
  161. }