Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
46 changes: 40 additions & 6 deletions kernel/arm64/gemm_kernel_sme2.c
Original file line number Diff line number Diff line change
Expand Up @@ -62,15 +62,49 @@ static inline void s2p_krange(BLASLONG i0, BLASLONG j0, int wm, int wn, BLASLONG
#ifdef HAVE_SME2_GEMM
#pragma clang attribute push(__attribute__((target("sme2"))), apply_to = function)

typedef void (*s2p_bfn)(BLASLONG, const FLOAT *, int, const FLOAT *, int, FLOAT *, BLASLONG, FLOAT, int) S2_S S2_ZA;
typedef void (*s2p_bfn)(BLASLONG, const FLOAT *, int, const FLOAT *, int, FLOAT *, BLASLONG, FLOAT, int,
BLASLONG) S2_S S2_ZA;

#if defined(TRMMKERNEL) && defined(LEFT)
/* Steps [l0, l1) at the diagonal: tile Q only from step g + Q * VL on (TRANSA: before step g + (Q + 1) * VL). */
S2_INL void s2p_mac_diag(const int TN, BLASLONG l0, BLASLONG l1, BLASLONG g, const FLOAT *a, int wm, const FLOAT *b,
int wn) S2_S S2_ZA {
const svbool_t pt = S2_PT(), pb = S2_PW(0, wn);
for (BLASLONG l = l0; l < l1; ++l) {
const s2_v bv = S2_LD1(pb, b + l * wn);
const s2_v4 a0 = s2_ld4n(a + l * wm, wm);
const s2_v4 a1 = TN > 4 ? s2_ld4n(a + l * wm + 4 * S2_VL, wm - 4 * S2_VL) : a0;
const BLASLONG q = (l - g) / S2_VL;
#ifndef TRANSA
S2_UNROLL for (int Q = 0; Q < TN; ++Q) if (Q <= q) s2_mopa(Q, pt, bv, s2_pick(a0, a1, Q));
#else
S2_UNROLL for (int Q = 0; Q < TN; ++Q) if (Q >= q) s2_mopa(Q, pt, bv, s2_pick(a0, a1, Q));
#endif
}
}
/* Left TRMM: the diagonal steps through s2p_mac_diag (g: the step of the diagonal element in row 0). */
#ifndef TRANSA
#define S2P_MAC_BLK(TN, BF, SUB) \
const BLASLONG t = s2_clip(g + (TN - 1) * S2_VL, kb); \
s2p_mac_diag(TN, 0, t, g, a, wm, b, wn); \
s2p_mac(TN, BF, SUB, kb - t, a + t * wm, wm, b + t * wn, wn);
#else
#define S2P_MAC_BLK(TN, BF, SUB) \
const BLASLONG t = s2_clip(g + S2_VL, kb); \
s2p_mac(TN, BF, SUB, t, a, wm, b, wn); \
s2p_mac_diag(TN, t, kb, g, a, wm, b, wn);
#endif
#else
#define S2P_MAC_BLK(TN, BF, SUB) s2p_mac(TN, BF, SUB, kb, a, wm, b, wn);
#endif

/* Block of wm rows and wn columns of C; cm 0: C += A B, 1: C -= A B, 2: C += alpha A B, 3: C = alpha A B. */
#define S2P_DEF(TN, BF, SUB) \
S2_NOINL void s2p_blk_##TN##_##BF##_##SUB(BLASLONG kb, const FLOAT *a, int wm, const FLOAT *b, int wn, FLOAT *C, \
BLASLONG ldc, FLOAT alpha, int cm) S2_S S2_ZA { \
BLASLONG ldc, FLOAT alpha, int cm, BLASLONG g) S2_S S2_ZA { \
if (cm < 2) s2_c_load(1, TN, C, ldc, wn, wm, S2_LOAD, (FLOAT)1); \
else svzero_za(); \
s2p_mac(TN, BF, SUB, kb, a, wm, b, wn); \
S2P_MAC_BLK(TN, BF, SUB) \
if (cm < 2) s2_c_store(1, TN, C, ldc, wn, wm); \
else s2_c_store_alpha(TN, C, ldc, wn, wm, alpha, cm == 2); \
}
Expand Down Expand Up @@ -105,9 +139,9 @@ __arm_new("za") __arm_locally_streaming static void s2p_drive(BLASLONG m, BLASLO
const int wm = m - i0 < S2_NR ? (int)(m - i0) : S2_NR;
BLASLONG ks, kl;
s2p_krange(i0, j0, wm, wn, k, offset, &ks, &kl);
s2p_blk[(wm + S2_VL - 1) / S2_VL - 1][wn == S2_VL][cm == 1](kl, sa + i0 * k + ks * wm, wm,
sb + j0 * k + ks * wn, wn, C + i0 + j0 * ldc,
ldc, alpha, cm);
s2p_blk[(wm + S2_VL - 1) / S2_VL - 1][wn == S2_VL][cm == 1](kl, sa + i0 * k + ks * wm, wm, sb + j0 * k + ks * wn,
wn, C + i0 + j0 * ldc, ldc, alpha, cm,
offset + i0 - ks);
}
j0 += wn;
}
Expand Down
30 changes: 30 additions & 0 deletions kernel/arm64/sme2_gemm_copy.h
Original file line number Diff line number Diff line change
Expand Up @@ -46,11 +46,13 @@ static inline BLASLONG s2c_clip(BLASLONG x, BLASLONG lo, BLASLONG hi) { return x
#define S2C_WL(a, b) svwhilelt_b64_s64(a, b)
#define S2C_LD svld1_f64
#define S2C_ST svst1_f64
#define S2C_DUP svdup_n_f64
#else
#define S2C_VL 16
#define S2C_WL(a, b) svwhilelt_b32_s64(a, b)
#define S2C_LD svld1_f32
#define S2C_ST svst1_f32
#define S2C_DUP svdup_n_f32
#endif

static inline __attribute__((always_inline)) void s2c_rows_s(BLASLONG m, BLASLONG w, const FLOAT *a, BLASLONG lda,
Expand Down Expand Up @@ -252,4 +254,32 @@ static inline void s2c_zero(BLASLONG m, BLASLONG w, FLOAT *b) {
if (m > 0) memset(b, 0, (size_t)(m * w) * sizeof(FLOAT));
}

/* Rows i1..i2 of a diagonal block, diagonal in column d0 + i: zeros left (zl) or right of it, 1 there with unit. */
static inline void s2c_tri(BLASLONG i1, BLASLONG i2, BLASLONG w, BLASLONG d0, int zl, int unit, FLOAT *b) {
for (BLASLONG i = i1; i < i2; ++i) {
const BLASLONG d = d0 + i;
FLOAT *bi = b + i * w;
for (BLASLONG c = zl ? 0 : d + 1; c < (zl ? d : w); ++c) bi[c] = ZERO;
if (unit) bi[d] = ONE;
}
}

#ifdef HAVE_SME2_GEMM
static inline __attribute__((always_inline)) void s2c_zero_s(BLASLONG m, BLASLONG w, FLOAT *b) __arm_streaming {
for (BLASLONG k = 0; k < m * w; k += S2C_VL) S2C_ST(S2C_WL(k, m * w), b + k, S2C_DUP(0));
}

static inline __attribute__((always_inline)) void s2c_tri_s(BLASLONG i1, BLASLONG i2, BLASLONG w, BLASLONG d0, int zl,
int unit, FLOAT *b) __arm_streaming {
for (BLASLONG i = i1; i < i2; ++i) {
const BLASLONG d = d0 + i;
for (BLASLONG c = 0; c < w; c += S2C_VL) {
const svbool_t pw = S2C_WL(c, w), pd = S2C_WL(c, d), pd1 = S2C_WL(c, d + 1);
S2C_ST(zl ? pd : svbic_b_z(pw, pw, pd1), b + i * w + c, S2C_DUP(0));
if (unit) S2C_ST(svbic_b_z(pw, pd1, pd), b + i * w + c, S2C_DUP(1));
}
}
}
#endif

#endif
22 changes: 18 additions & 4 deletions kernel/arm64/sme2_gemm_tile.h
Original file line number Diff line number Diff line change
Expand Up @@ -508,15 +508,29 @@ S2_INL void s2_c_store(const int TM, const int TN, FLOAT *C, BLASLONG ldc, int m
}
}

/* alpha z, plus the C row at p (add). */
S2_INL s2_v s2_alpha_v(s2_v z, FLOAT *p, svbool_t pg, FLOAT alpha, int add) S2_S {
return add ? S2_MLA(S2_LD1(pg, p), z, alpha) : S2_MUL(z, alpha);
}
/* C = C + alpha ZA (add) or C = alpha ZA from a row of TN tiles: rows < mrows, columns < ncols. */
S2_INL void s2_c_store_alpha(const int TN, FLOAT *C, BLASLONG ldc, int mrows, int ncols, FLOAT alpha, int add) S2_S
S2_ZA {
for (int s = 0; s < mrows; ++s) {
FLOAT *row = C + (BLASLONG)s * ldc;
int s = 0;
for (; s + 4 <= mrows; s += 4) { /* four rows per ZA move (MOVA vg4) */
S2_UNROLL for (int Q = 0; Q < TN; ++Q) {
const svbool_t pg = S2_PW(Q * S2_VL, ncols);
const s2_v4 q = s2_rdh4(Q, s);
S2_UNROLL for (int u = 0; u < 4; ++u) {
FLOAT *p = C + (BLASLONG)(s + u) * ldc + Q * S2_VL;
S2_ST1(pg, p, s2_alpha_v(s2_get4(q, u), p, pg, alpha, add));
}
}
}
for (; s < mrows; ++s) {
S2_UNROLL for (int Q = 0; Q < TN; ++Q) {
const svbool_t pg = S2_PW(Q * S2_VL, ncols);
const s2_v z = s2_rdh(Q, s);
S2_ST1(pg, row + Q * S2_VL, add ? S2_MLA(S2_LD1(pg, row + Q * S2_VL), z, alpha) : S2_MUL(z, alpha));
FLOAT *p = C + (BLASLONG)s * ldc + Q * S2_VL;
S2_ST1(pg, p, s2_alpha_v(s2_rdh(Q, s), p, pg, alpha, add));
}
}
}
Expand Down
63 changes: 36 additions & 27 deletions kernel/arm64/trmm_ncopy_sme2.c
Original file line number Diff line number Diff line change
Expand Up @@ -31,41 +31,50 @@ THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
#include "sme2_gemm_copy.h"

#ifndef LOWER
#define KEEP(r, c) ((r) < (c))
#define ZL 1
#else
#define KEEP(r, c) ((r) > (c))
#define ZL 0
#endif
#ifdef UNIT
#define DIAG(v) ONE
#define UNITD 1
#else
#define DIAG(v) (v)
#define UNITD 0
#endif

int CNAME(BLASLONG m, BLASLONG n, FLOAT *a, BLASLONG lda, BLASLONG posX, BLASLONG posY, FLOAT *b) {
for (BLASLONG j0 = 0; j0 < n; j0 += S2C_W) {
const BLASLONG w = n - j0 < S2C_W ? n - j0 : S2C_W, lo = posY + j0;
const FLOAT *ap = a + posX + lo * lda;
const BLASLONG i1 = s2c_clip(lo - posX, 0, m), i2 = s2c_clip(lo + w - posX, i1, m);
#ifndef LOWER
s2c_cols(i1, w, ap, lda, b);
s2c_zero(m - i2, w, b + i2 * w);
#if ZL
#define PART(COPY, ZERO) \
COPY(i2, w, ap, lda, b); \
ZERO(m - i2, w, b + i2 * w)
#else
s2c_zero(i1, w, b);
s2c_cols(m - i2, w, ap + i2, lda, b + i2 * w);
#define PART(COPY, ZERO) \
ZERO(i1, w, b); \
COPY(m - i1, w, ap + i1, lda, b + i1 * w)
#endif
/* Diagonal block: a copy, then the zero side of the diagonal (column d of row i) and the unit diagonal. */
s2c_cols(i2 - i1, w, ap + i1, lda, b + i1 * w);
for (BLASLONG i = i1; i < i2; ++i) {
const BLASLONG d = posX + i - lo;
FLOAT *bi = b + i * w;
if (KEEP(0, 1)) {
for (BLASLONG c = 0; c < d; ++c) bi[c] = ZERO;
} else {
for (BLASLONG c = d + 1; c < w; ++c) bi[c] = ZERO;
}
bi[d] = DIAG(bi[d]);
}
b += m * w;
/* Panels of w columns: the rows on the kept side of the diagonal copied, the others zeroed, then the diagonal block. */
#define PANELS(COPY, ZERO, TRI) \
for (BLASLONG j0 = 0, w; j0 < n; j0 += w, b += m * w) { \
w = n - j0 < S2C_W ? n - j0 : S2C_W; \
const BLASLONG lo = posY + j0, i1 = s2c_clip(lo - posX, 0, m), i2 = s2c_clip(lo + w - posX, i1, m); \
const FLOAT *ap = a + posX + lo * lda; \
PART(COPY, ZERO); \
TRI(i1, i2, w, posX - lo, ZL, UNITD, b); \
}

#ifdef HAVE_SME2_GEMM
__arm_new("za") __arm_locally_streaming static
__attribute__((noinline)) void panels_sme(BLASLONG m, BLASLONG n, FLOAT *a, BLASLONG lda, BLASLONG posX,
BLASLONG posY, FLOAT *b) {
PANELS(s2c_cols_s, s2c_zero_s, s2c_tri_s)
}
#endif

int CNAME(BLASLONG m, BLASLONG n, FLOAT *a, BLASLONG lda, BLASLONG posX, BLASLONG posY, FLOAT *b) {
#ifdef HAVE_SME2_GEMM
if (m * n >= S2C_SME && s2_usable()) {
panels_sme(m, n, a, lda, posX, posY, b);
return 0;
}
#endif
PANELS(s2c_cols, s2c_zero, s2c_tri)
return 0;
}
62 changes: 35 additions & 27 deletions kernel/arm64/trmm_tcopy_sme2.c
Original file line number Diff line number Diff line change
Expand Up @@ -31,41 +31,49 @@ THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
#include "sme2_gemm_copy.h"

#ifndef LOWER
#define KEEP(r, c) ((r) > (c))
#define ZL 0
#else
#define KEEP(r, c) ((r) < (c))
#define ZL 1
#endif
#ifdef UNIT
#define DIAG(v) ONE
#define UNITD 1
#else
#define DIAG(v) (v)
#define UNITD 0
#endif

int CNAME(BLASLONG m, BLASLONG n, FLOAT *a, BLASLONG lda, BLASLONG posX, BLASLONG posY, FLOAT *b) {
for (BLASLONG j0 = 0; j0 < n; j0 += S2C_W) {
const BLASLONG w = n - j0 < S2C_W ? n - j0 : S2C_W, lo = posY + j0;
const FLOAT *ap = a + lo + posX * lda;
const BLASLONG i1 = s2c_clip(lo - posX, 0, m), i2 = s2c_clip(lo + w - posX, i1, m);
#ifndef LOWER
s2c_zero(i1, w, b);
s2c_rows(m - i2, w, ap + i2 * lda, lda, b + i2 * w);
#if ZL
#define PART(COPY, ZERO) \
COPY(i2, w, ap, lda, b); \
ZERO(m - i2, w, b + i2 * w)
#else
s2c_rows(i1, w, ap, lda, b);
s2c_zero(m - i2, w, b + i2 * w);
#define PART(COPY, ZERO) \
ZERO(i1, w, b); \
COPY(m - i1, w, ap + i1 * lda, lda, b + i1 * w)
#endif
/* Diagonal block: a copy, then the zero side of the diagonal (column d of row i) and the unit diagonal. */
s2c_rows(i2 - i1, w, ap + i1 * lda, lda, b + i1 * w);
for (BLASLONG i = i1; i < i2; ++i) {
const BLASLONG d = posX + i - lo;
FLOAT *bi = b + i * w;
if (KEEP(0, 1)) {
for (BLASLONG c = 0; c < d; ++c) bi[c] = ZERO;
} else {
for (BLASLONG c = d + 1; c < w; ++c) bi[c] = ZERO;
}
bi[d] = DIAG(bi[d]);
}
b += m * w;
/* Panels of w columns: the rows on the kept side of the diagonal copied, the others zeroed, then the diagonal block. */
#define PANELS(COPY, ZERO, TRI) \
for (BLASLONG j0 = 0, w; j0 < n; j0 += w, b += m * w) { \
w = n - j0 < S2C_W ? n - j0 : S2C_W; \
const BLASLONG lo = posY + j0, i1 = s2c_clip(lo - posX, 0, m), i2 = s2c_clip(lo + w - posX, i1, m); \
const FLOAT *ap = a + lo + posX * lda; \
PART(COPY, ZERO); \
TRI(i1, i2, w, posX - lo, ZL, UNITD, b); \
}

#ifdef HAVE_SME2_GEMM
__arm_locally_streaming static __attribute__((noinline)) void panels_sme(BLASLONG m, BLASLONG n, FLOAT *a, BLASLONG lda,
BLASLONG posX, BLASLONG posY, FLOAT *b) {
PANELS(s2c_rows_s, s2c_zero_s, s2c_tri_s)
}
#endif

int CNAME(BLASLONG m, BLASLONG n, FLOAT *a, BLASLONG lda, BLASLONG posX, BLASLONG posY, FLOAT *b) {
#ifdef HAVE_SME2_GEMM
if (m * n >= S2C_SME && s2_usable()) {
panels_sme(m, n, a, lda, posX, posY, b);
return 0;
}
#endif
PANELS(s2c_rows, s2c_zero, s2c_tri)
return 0;
}
Loading