Skip to content

Commit d0ba257

Browse files
authored
Merge pull request #3704 from XiWeiGu/loongarch64_dynamic_arch
LoongArch64: Add DYNAMIC_ARCH support
2 parents 78da6a7 + fbfe1da commit d0ba257

File tree

8 files changed

+218
-40
lines changed

8 files changed

+218
-40
lines changed

Makefile.system

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -680,6 +680,10 @@ ifeq ($(ARCH), mips64)
680680
DYNAMIC_CORE = LOONGSON3R3 LOONGSON3R4
681681
endif
682682

683+
ifeq ($(ARCH), loongarch64)
684+
DYNAMIC_CORE = LOONGSON3R5 LOONGSON2K1000 LOONGSONGENERIC
685+
endif
686+
683687
ifeq ($(ARCH), zarch)
684688
DYNAMIC_CORE = ZARCH_GENERIC
685689

driver/others/Makefile

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,11 +27,15 @@ else
2727
ifeq ($(ARCH),mips64)
2828
COMMONOBJS += dynamic_mips64.$(SUFFIX)
2929
else
30+
ifeq ($(ARCH),loongarch64)
31+
COMMONOBJS += dynamic_loongarch64.$(SUFFIX)
32+
else
3033
COMMONOBJS += dynamic.$(SUFFIX)
3134
endif
3235
endif
3336
endif
3437
endif
38+
endif
3539
else
3640
COMMONOBJS += parameter.$(SUFFIX)
3741
endif
@@ -99,11 +103,15 @@ else
99103
ifeq ($(ARCH),mips64)
100104
HPLOBJS = memory.$(SUFFIX) xerbla.$(SUFFIX) dynamic_mips64.$(SUFFIX)
101105
else
106+
ifeq ($(ARCH),loongarch64)
107+
HPLOBJS = memory.$(SUFFIX) xerbla.$(SUFFIX) dynamic_loongarch64.$(SUFFIX)
108+
else
102109
HPLOBJS = memory.$(SUFFIX) xerbla.$(SUFFIX) dynamic.$(SUFFIX)
103110
endif
104111
endif
105112
endif
106113
endif
114+
endif
107115
else
108116
HPLOBJS = memory.$(SUFFIX) xerbla.$(SUFFIX) parameter.$(SUFFIX)
109117
endif

driver/others/dynamic_loongarch64.c

Lines changed: 128 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,128 @@
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+
28+
#include "common.h"
29+
30+
extern gotoblas_t gotoblas_LOONGSON3R5;
31+
extern gotoblas_t gotoblas_LOONGSON2K1000;
32+
extern gotoblas_t gotoblas_LOONGSONGENERIC;
33+
34+
extern void openblas_warning(int verbose, const char * msg);
35+
36+
#define NUM_CORETYPES 3
37+
38+
static char *corename[] = {
39+
"loongson3r5",
40+
"loongson2k1000",
41+
"loongsongeneric",
42+
"unknown"
43+
};
44+
45+
char *gotoblas_corename(void) {
46+
if (gotoblas == &gotoblas_LOONGSON3R5) return corename[0];
47+
if (gotoblas == &gotoblas_LOONGSON2K1000) return corename[1];
48+
if (gotoblas == &gotoblas_LOONGSONGENERIC) return corename[2];
49+
return corename[NUM_CORETYPES];
50+
}
51+
52+
static gotoblas_t *force_coretype(char *coretype) {
53+
int i;
54+
int found = -1;
55+
char message[128];
56+
57+
for ( i=0 ; i < NUM_CORETYPES; i++)
58+
{
59+
if (!strncasecmp(coretype, corename[i], 20))
60+
{
61+
found = i;
62+
break;
63+
}
64+
}
65+
66+
switch (found)
67+
{
68+
case 0: return (&gotoblas_LOONGSON3R5);
69+
case 1: return (&gotoblas_LOONGSON2K1000);
70+
case 2: return (&gotoblas_LOONGSONGENERIC);
71+
}
72+
snprintf(message, 128, "Core not found: %s\n", coretype);
73+
openblas_warning(1, message);
74+
return NULL;
75+
}
76+
77+
#define LASX_MASK 1<<7
78+
#define LSX_MASK 1<<6
79+
#define LOONGARCH_CFG2 0x02
80+
81+
static gotoblas_t *get_coretype(void) {
82+
int ret = 0;
83+
__asm__ volatile (
84+
"cpucfg %0, %1 \n\t"
85+
: "+&r"(ret)
86+
: "r"(LOONGARCH_CFG2)
87+
);
88+
89+
if (ret & LASX_MASK)
90+
return &gotoblas_LOONGSON3R5;
91+
else if (ret & LSX_MASK)
92+
return &gotoblas_LOONGSON2K1000;
93+
else
94+
return &gotoblas_LOONGSONGENERIC;
95+
}
96+
97+
void gotoblas_dynamic_init(void) {
98+
char coremsg[128];
99+
char coren[22];
100+
char *p;
101+
102+
if (gotoblas) return;
103+
104+
p = getenv("OPENBLAS_CORETYPE");
105+
if ( p )
106+
{
107+
gotoblas = force_coretype(p);
108+
}
109+
else
110+
{
111+
gotoblas = get_coretype();
112+
}
113+
114+
if (gotoblas && gotoblas->init) {
115+
strncpy(coren, gotoblas_corename(), 20);
116+
sprintf(coremsg, "Core: %s\n", coren);
117+
openblas_warning(2, coremsg);
118+
gotoblas -> init();
119+
} else {
120+
openblas_warning(0, "OpenBLAS : Architecture Initialization failed. No initialization function found.\n");
121+
exit(1);
122+
}
123+
124+
}
125+
126+
void gotoblas_dynamic_quit(void) {
127+
gotoblas = NULL;
128+
}

kernel/loongarch64/KERNEL

Lines changed: 16 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -108,10 +108,10 @@ SGEMMINCOPY = ../generic/gemm_ncopy_2.c
108108
SGEMMITCOPY = ../generic/gemm_tcopy_2.c
109109
SGEMMONCOPY = ../generic/gemm_ncopy_8.c
110110
SGEMMOTCOPY = ../generic/gemm_tcopy_8.c
111-
SGEMMINCOPYOBJ = sgemm_incopy.o
112-
SGEMMITCOPYOBJ = sgemm_itcopy.o
113-
SGEMMONCOPYOBJ = sgemm_oncopy.o
114-
SGEMMOTCOPYOBJ = sgemm_otcopy.o
111+
SGEMMINCOPYOBJ = sgemm_incopy$(TSUFFIX).$(SUFFIX)
112+
SGEMMITCOPYOBJ = sgemm_itcopy$(TSUFFIX).$(SUFFIX)
113+
SGEMMONCOPYOBJ = sgemm_oncopy$(TSUFFIX).$(SUFFIX)
114+
SGEMMOTCOPYOBJ = sgemm_otcopy$(TSUFFIX).$(SUFFIX)
115115
endif
116116

117117
ifndef DGEMMKERNEL
@@ -120,10 +120,10 @@ DGEMMINCOPY = ../generic/gemm_ncopy_2.c
120120
DGEMMITCOPY = ../generic/gemm_tcopy_2.c
121121
DGEMMONCOPY = ../generic/gemm_ncopy_8.c
122122
DGEMMOTCOPY = ../generic/gemm_tcopy_8.c
123-
DGEMMINCOPYOBJ = dgemm_incopy.o
124-
DGEMMITCOPYOBJ = dgemm_itcopy.o
125-
DGEMMONCOPYOBJ = dgemm_oncopy.o
126-
DGEMMOTCOPYOBJ = dgemm_otcopy.o
123+
DGEMMINCOPYOBJ = dgemm_incopy$(TSUFFIX).$(SUFFIX)
124+
DGEMMITCOPYOBJ = dgemm_itcopy$(TSUFFIX).$(SUFFIX)
125+
DGEMMONCOPYOBJ = dgemm_oncopy$(TSUFFIX).$(SUFFIX)
126+
DGEMMOTCOPYOBJ = dgemm_otcopy$(TSUFFIX).$(SUFFIX)
127127
endif
128128

129129
ifndef CGEMMKERNEL
@@ -132,10 +132,10 @@ CGEMMINCOPY = ../generic/zgemm_ncopy_1.c
132132
CGEMMITCOPY = ../generic/zgemm_tcopy_1.c
133133
CGEMMONCOPY = ../generic/zgemm_ncopy_4.c
134134
CGEMMOTCOPY = ../generic/zgemm_tcopy_4.c
135-
CGEMMINCOPYOBJ = cgemm_incopy.o
136-
CGEMMITCOPYOBJ = cgemm_itcopy.o
137-
CGEMMONCOPYOBJ = cgemm_oncopy.o
138-
CGEMMOTCOPYOBJ = cgemm_otcopy.o
135+
CGEMMINCOPYOBJ = cgemm_incopy$(TSUFFIX).$(SUFFIX)
136+
CGEMMITCOPYOBJ = cgemm_itcopy$(TSUFFIX).$(SUFFIX)
137+
CGEMMONCOPYOBJ = cgemm_oncopy$(TSUFFIX).$(SUFFIX)
138+
CGEMMOTCOPYOBJ = cgemm_otcopy$(TSUFFIX).$(SUFFIX)
139139
endif
140140

141141
ifndef ZGEMMKERNEL
@@ -144,10 +144,10 @@ ZGEMMINCOPY = ../generic/zgemm_ncopy_1.c
144144
ZGEMMITCOPY = ../generic/zgemm_tcopy_1.c
145145
ZGEMMONCOPY = ../generic/zgemm_ncopy_4.c
146146
ZGEMMOTCOPY = ../generic/zgemm_tcopy_4.c
147-
ZGEMMINCOPYOBJ = zgemm_incopy.o
148-
ZGEMMITCOPYOBJ = zgemm_itcopy.o
149-
ZGEMMONCOPYOBJ = zgemm_oncopy.o
150-
ZGEMMOTCOPYOBJ = zgemm_otcopy.o
147+
ZGEMMINCOPYOBJ = zgemm_incopy$(TSUFFIX).$(SUFFIX)
148+
ZGEMMITCOPYOBJ = zgemm_itcopy$(TSUFFIX).$(SUFFIX)
149+
ZGEMMONCOPYOBJ = zgemm_oncopy$(TSUFFIX).$(SUFFIX)
150+
ZGEMMOTCOPYOBJ = zgemm_otcopy$(TSUFFIX).$(SUFFIX)
151151
endif
152152

153153
ifndef SGEMM_BETA

kernel/loongarch64/KERNEL.LOONGSON3R5

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,10 +3,10 @@ DGEMMINCOPY = dgemm_ncopy_16.S
33
DGEMMITCOPY = dgemm_tcopy_16.S
44
DGEMMONCOPY = dgemm_ncopy_4.S
55
DGEMMOTCOPY = dgemm_tcopy_4.S
6-
DGEMMINCOPYOBJ = dgemm_incopy.o
7-
DGEMMITCOPYOBJ = dgemm_itcopy.o
8-
DGEMMONCOPYOBJ = dgemm_oncopy.o
9-
DGEMMOTCOPYOBJ = dgemm_otcopy.o
6+
DGEMMINCOPYOBJ = dgemm_incopy$(TSUFFIX).$(SUFFIX)
7+
DGEMMITCOPYOBJ = dgemm_itcopy$(TSUFFIX).$(SUFFIX)
8+
DGEMMONCOPYOBJ = dgemm_oncopy$(TSUFFIX).$(SUFFIX)
9+
DGEMMOTCOPYOBJ = dgemm_otcopy$(TSUFFIX).$(SUFFIX)
1010

1111
DTRSMKERNEL_LN = ../generic/trsm_kernel_LN.c
1212
DTRSMKERNEL_LT = ../generic/trsm_kernel_LT.c

kernel/loongarch64/KERNEL.generic

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -11,26 +11,26 @@ ZTRMMKERNEL = ../generic/ztrmmkernel_2x2.c
1111
SGEMMKERNEL = ../generic/gemmkernel_2x2.c
1212
SGEMMONCOPY = ../generic/gemm_ncopy_2.c
1313
SGEMMOTCOPY = ../generic/gemm_tcopy_2.c
14-
SGEMMONCOPYOBJ = sgemm_oncopy.o
15-
SGEMMOTCOPYOBJ = sgemm_otcopy.o
14+
SGEMMONCOPYOBJ = sgemm_oncopy$(TSUFFIX).$(SUFFIX)
15+
SGEMMOTCOPYOBJ = sgemm_otcopy$(TSUFFIX).$(SUFFIX)
1616

1717
DGEMMKERNEL = ../generic/gemmkernel_2x2.c
1818
DGEMMONCOPY = ../generic/gemm_ncopy_2.c
1919
DGEMMOTCOPY = ../generic/gemm_tcopy_2.c
20-
DGEMMONCOPYOBJ = dgemm_oncopy.o
21-
DGEMMOTCOPYOBJ = dgemm_otcopy.o
20+
DGEMMONCOPYOBJ = dgemm_oncopy$(TSUFFIX).$(SUFFIX)
21+
DGEMMOTCOPYOBJ = dgemm_otcopy$(TSUFFIX).$(SUFFIX)
2222

2323
CGEMMKERNEL = ../generic/zgemmkernel_2x2.c
2424
CGEMMONCOPY = ../generic/zgemm_ncopy_2.c
2525
CGEMMOTCOPY = ../generic/zgemm_tcopy_2.c
26-
CGEMMONCOPYOBJ = cgemm_oncopy.o
27-
CGEMMOTCOPYOBJ = cgemm_otcopy.o
26+
CGEMMONCOPYOBJ = cgemm_oncopy$(TSUFFIX).$(SUFFIX)
27+
CGEMMOTCOPYOBJ = cgemm_otcopy$(TSUFFIX).$(SUFFIX)
2828

2929
ZGEMMKERNEL = ../generic/zgemmkernel_2x2.c
3030
ZGEMMONCOPY = ../generic/zgemm_ncopy_2.c
3131
ZGEMMOTCOPY = ../generic/zgemm_tcopy_2.c
32-
ZGEMMONCOPYOBJ = zgemm_oncopy.o
33-
ZGEMMOTCOPYOBJ = zgemm_otcopy.o
32+
ZGEMMONCOPYOBJ = zgemm_oncopy$(TSUFFIX).$(SUFFIX)
33+
ZGEMMOTCOPYOBJ = zgemm_otcopy$(TSUFFIX).$(SUFFIX)
3434

3535
STRSMKERNEL_LN = ../generic/trsm_kernel_LN.c
3636
STRSMKERNEL_LT = ../generic/trsm_kernel_LT.c

kernel/setparam-ref.c

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1046,6 +1046,34 @@ static void init_parameter(void) {
10461046
#endif
10471047
}
10481048
#else // (ARCH_MIPS64)
1049+
#if (ARCH_LOONGARCH64)
1050+
static void init_parameter(void) {
1051+
1052+
#ifdef BUILD_BFLOAT16
1053+
TABLE_NAME.sbgemm_p = SBGEMM_DEFAULT_P;
1054+
#endif
1055+
TABLE_NAME.sgemm_p = SGEMM_DEFAULT_P;
1056+
TABLE_NAME.dgemm_p = DGEMM_DEFAULT_P;
1057+
TABLE_NAME.cgemm_p = CGEMM_DEFAULT_P;
1058+
TABLE_NAME.zgemm_p = ZGEMM_DEFAULT_P;
1059+
1060+
#ifdef BUILD_BFLOAT16
1061+
TABLE_NAME.sbgemm_r = SBGEMM_DEFAULT_R;
1062+
#endif
1063+
TABLE_NAME.sgemm_r = SGEMM_DEFAULT_R;
1064+
TABLE_NAME.dgemm_r = DGEMM_DEFAULT_R;
1065+
TABLE_NAME.cgemm_r = CGEMM_DEFAULT_R;
1066+
TABLE_NAME.zgemm_r = ZGEMM_DEFAULT_R;
1067+
1068+
#ifdef BUILD_BFLOAT16
1069+
TABLE_NAME.sbgemm_q = SBGEMM_DEFAULT_Q;
1070+
#endif
1071+
TABLE_NAME.sgemm_q = SGEMM_DEFAULT_Q;
1072+
TABLE_NAME.dgemm_q = DGEMM_DEFAULT_Q;
1073+
TABLE_NAME.cgemm_q = CGEMM_DEFAULT_Q;
1074+
TABLE_NAME.zgemm_q = ZGEMM_DEFAULT_Q;
1075+
}
1076+
#else // (ARCH_LOONGARCH64)
10491077
#if (ARCH_POWER)
10501078
static void init_parameter(void) {
10511079

@@ -1899,5 +1927,6 @@ static void init_parameter(void) {
18991927
}
19001928
#endif //POWER
19011929
#endif //ZARCH
1930+
#endif //(ARCH_LOONGARCH64)
19021931
#endif //(ARCH_MIPS64)
19031932
#endif //(ARCH_ARM64)

param.h

Lines changed: 21 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -2857,26 +2857,20 @@ USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
28572857
#define ZGEMM_DEFAULT_UNROLL_M 1
28582858
#define XGEMM_DEFAULT_UNROLL_M 1
28592859

2860-
#define SGEMM_DEFAULT_P sgemm_p
2860+
#define SGEMM_DEFAULT_P 512
28612861
#define DGEMM_DEFAULT_P 32
2862-
#define QGEMM_DEFAULT_P qgemm_p
2863-
#define CGEMM_DEFAULT_P cgemm_p
2864-
#define ZGEMM_DEFAULT_P zgemm_p
2865-
#define XGEMM_DEFAULT_P xgemm_p
2862+
#define CGEMM_DEFAULT_P 128
2863+
#define ZGEMM_DEFAULT_P 128
28662864

2867-
#define SGEMM_DEFAULT_R sgemm_r
2865+
#define SGEMM_DEFAULT_R 12288
28682866
#define DGEMM_DEFAULT_R 858
2869-
#define QGEMM_DEFAULT_R qgemm_r
2870-
#define CGEMM_DEFAULT_R cgemm_r
2871-
#define ZGEMM_DEFAULT_R zgemm_r
2872-
#define XGEMM_DEFAULT_R xgemm_r
2867+
#define CGEMM_DEFAULT_R 4096
2868+
#define ZGEMM_DEFAULT_R 4096
28732869

28742870
#define SGEMM_DEFAULT_Q 128
28752871
#define DGEMM_DEFAULT_Q 152
2876-
#define QGEMM_DEFAULT_Q 128
28772872
#define CGEMM_DEFAULT_Q 128
28782873
#define ZGEMM_DEFAULT_Q 128
2879-
#define XGEMM_DEFAULT_Q 128
28802874

28812875
#define SYMV_P 16
28822876
#endif
@@ -3795,6 +3789,21 @@ Until then, just keep it different than DGEMM_DEFAULT_UNROLL_N to keep copy rout
37953789
#define DGEMM_DEFAULT_R 8192
37963790
#define CGEMM_DEFAULT_R 4096
37973791
#define ZGEMM_DEFAULT_R 4096
3792+
#elif defined(ARCH_LOONGARCH64)
3793+
#define SGEMM_DEFAULT_P 128
3794+
#define DGEMM_DEFAULT_P 128
3795+
#define CGEMM_DEFAULT_P 96
3796+
#define ZGEMM_DEFAULT_P 64
3797+
3798+
#define SGEMM_DEFAULT_Q 240
3799+
#define DGEMM_DEFAULT_Q 120
3800+
#define CGEMM_DEFAULT_Q 120
3801+
#define ZGEMM_DEFAULT_Q 120
3802+
3803+
#define SGEMM_DEFAULT_R 12288
3804+
#define DGEMM_DEFAULT_R 8192
3805+
#define CGEMM_DEFAULT_R 4096
3806+
#define ZGEMM_DEFAULT_R 4096
37983807
#else
37993808
#define SGEMM_DEFAULT_P sgemm_p
38003809
#define DGEMM_DEFAULT_P dgemm_p

0 commit comments

Comments
 (0)