|
| 1 | +/* |
| 2 | + Copyright (c) Qualcomm Technologies, Inc. and/or its subsidiaries. |
| 3 | + SPDX-License-Identifier: BSD-3-Clause-Clear |
| 4 | +*/ |
| 5 | + |
| 6 | +#include "common.h" |
| 7 | +#include <stdlib.h> |
| 8 | +#include <inttypes.h> |
| 9 | +#include <math.h> |
| 10 | +#include "sme_abi.h" |
| 11 | +#if defined(HAVE_SME) |
| 12 | + |
| 13 | +#if defined(__ARM_FEATURE_SME) && defined(__clang__) && __clang_major__ >= 16 |
| 14 | +#include <arm_sme.h> |
| 15 | +#endif |
| 16 | + |
| 17 | +/* Function prototypes */ |
| 18 | +extern void sgemm_direct_sme1_preprocess(uint64_t nbr, uint64_t nbc,\ |
| 19 | + const float * restrict a, float * a_mod) __asm__("sgemm_direct_sme1_preprocess"); |
| 20 | + |
| 21 | +extern void sgemm_direct_alpha_beta_sme1_2VLx2VL(uint64_t m, uint64_t k, uint64_t n, const float* alpha,\ |
| 22 | + const float *ba, const float *restrict bb, const float* beta,\ |
| 23 | + float *restrict C); |
| 24 | +/* Function Definitions */ |
| 25 | +static uint64_t sve_cntw() { |
| 26 | + uint64_t cnt; |
| 27 | + asm volatile( |
| 28 | + "rdsvl %[res], #1\n" |
| 29 | + "lsr %[res], %[res], #2\n" |
| 30 | + : [res] "=r" (cnt) :: |
| 31 | + ); |
| 32 | + return cnt; |
| 33 | +} |
| 34 | + |
| 35 | +#if defined(__ARM_FEATURE_SME) && defined(__ARM_FEATURE_LOCALLY_STREAMING) && defined(__clang__) && __clang_major__ >= 16 |
| 36 | + |
| 37 | +__arm_new("za") __arm_locally_streaming |
| 38 | +static void ssymm_direct_sme1_preprocessLU(uint64_t nbr, uint64_t nbc, |
| 39 | + const float *restrict a, float *restrict a_mod) |
| 40 | +{ |
| 41 | + // const uint64_t num_rows = nbr; |
| 42 | + // const uint64_t num_cols = nbc; |
| 43 | + const uint64_t svl = svcntw(); |
| 44 | + uint64_t row_batch = svl; |
| 45 | + |
| 46 | + float *restrict pSrc; |
| 47 | + float *restrict pDst; |
| 48 | + for (uint64_t row_idx = 0; row_idx < nbr; row_idx += row_batch) |
| 49 | + { |
| 50 | + row_batch = MIN(row_batch, nbr - row_idx); |
| 51 | + |
| 52 | + // Fill in the lower triangle and Transpose 1SVL x N panel of A |
| 53 | + uint64_t col_batch = svl; |
| 54 | + |
| 55 | + for (uint64_t col_idx = 0; col_idx < nbc; col_idx += col_batch) |
| 56 | + { |
| 57 | + svzero_za(); |
| 58 | + |
| 59 | + if (col_idx == row_idx) |
| 60 | + { |
| 61 | + pSrc = &a[(row_idx)*nbc + col_idx]; |
| 62 | + pDst = &a_mod[(col_idx)*svl + row_idx * nbc]; |
| 63 | + // Load horizontal slices, filling lower elements |
| 64 | + const svbool_t pg_row = svwhilelt_b32_u64(col_idx, nbc); |
| 65 | + for (int64_t row = row_batch - 1; row >= 0; row--) |
| 66 | + { |
| 67 | + svld1_hor_za32(0, row, pg_row, &pSrc[row * nbc]); |
| 68 | + svld1_ver_za32(0, row, pg_row, &pSrc[row * nbc]); |
| 69 | + } |
| 70 | + // Save vertical slices |
| 71 | + col_batch = MIN(col_batch, nbc - col_idx); |
| 72 | + for (uint64_t col = 0; col < col_batch; col++) |
| 73 | + { |
| 74 | + svst1_ver_za32(0, col, svptrue_b32(), &pDst[col * svl]); |
| 75 | + } |
| 76 | + } |
| 77 | + else if (col_idx > row_idx) |
| 78 | + { |
| 79 | + pSrc = &a[(row_idx)*nbc + col_idx]; |
| 80 | + pDst = &a_mod[(col_idx)*svl + row_idx * nbc]; |
| 81 | + // Load horizontal slices |
| 82 | + const svbool_t pg_row = svwhilelt_b32_u64(col_idx, nbc); |
| 83 | + for (uint64_t row = 0; row < row_batch; row++) |
| 84 | + { |
| 85 | + svld1_hor_za32(0, row, pg_row, &pSrc[row * nbc]); |
| 86 | + } |
| 87 | + // Save vertical slices |
| 88 | + col_batch = MIN(col_batch, nbc - col_idx); |
| 89 | + for (uint64_t col = 0; col < col_batch; col++) |
| 90 | + { |
| 91 | + svst1_ver_za32(0, col, svptrue_b32(), &pDst[col * svl]); |
| 92 | + } |
| 93 | + } |
| 94 | + else if (col_idx < row_idx) |
| 95 | + { |
| 96 | + pSrc = &a[row_idx + col_idx * nbc]; |
| 97 | + pDst = &a_mod[(col_idx)*svl + row_idx * nbc]; |
| 98 | + // Load horizontal slices |
| 99 | + const svbool_t pg_row = svwhilelt_b32_u64(row_idx, nbc); |
| 100 | + for (uint64_t row = 0; row < svl; row++) |
| 101 | + { |
| 102 | + svld1_hor_za32(0, row, pg_row, &pSrc[row * nbc]); |
| 103 | + } |
| 104 | + // Save vertical slices |
| 105 | + col_batch = MIN(col_batch, nbc - col_idx); |
| 106 | + for (uint64_t col = 0; col < svl; col++) |
| 107 | + { |
| 108 | + svst1_hor_za32(0, col, svptrue_b32(), &pDst[col * svl]); |
| 109 | + } |
| 110 | + } |
| 111 | + } |
| 112 | + } |
| 113 | +} |
| 114 | + |
| 115 | +// |
| 116 | +__arm_new("za") __arm_locally_streaming |
| 117 | +static void ssymm_direct_sme1_preprocessLL(uint64_t nbr, uint64_t nbc, |
| 118 | + const float *restrict a, float *restrict a_mod) |
| 119 | +{ |
| 120 | + // const uint64_t num_rows = nbr; |
| 121 | + const uint64_t svl = svcntw(); |
| 122 | + uint64_t row_batch = svl; |
| 123 | + |
| 124 | + float *restrict pSrc; |
| 125 | + float *restrict pDst; |
| 126 | + for (uint64_t row_idx = 0; row_idx < nbr; row_idx += row_batch) |
| 127 | + { |
| 128 | + row_batch = MIN(row_batch, nbr - row_idx); |
| 129 | + |
| 130 | + // Fill in the upper triangle and Transpose 1SVL x N panel of A |
| 131 | + uint64_t col_batch = svl; |
| 132 | + |
| 133 | + for (uint64_t col_idx = 0; col_idx < nbc; col_idx += col_batch) |
| 134 | + { |
| 135 | + svzero_za(); |
| 136 | + |
| 137 | + if (col_idx == row_idx) |
| 138 | + { |
| 139 | + pSrc = &a[(row_idx)*nbc + col_idx]; |
| 140 | + pDst = &a_mod[(col_idx)*svl + row_idx * nbc]; |
| 141 | + // Load horizontal slices, filling upper elements |
| 142 | + const svbool_t pg_row = svwhilelt_b32_u64(col_idx, nbc); |
| 143 | + for (uint64_t row = 0; row < row_batch; row++) |
| 144 | + { |
| 145 | + svld1_hor_za32(0, row, pg_row, &pSrc[row * nbc]); |
| 146 | + svld1_ver_za32(0, row, pg_row, &pSrc[row * nbc]); |
| 147 | + } |
| 148 | + // Save vertical slices |
| 149 | + col_batch = MIN(col_batch, nbc - col_idx); |
| 150 | + for (uint64_t col = 0; col < col_batch; col++) |
| 151 | + { |
| 152 | + svst1_ver_za32(0, col, svptrue_b32(), &pDst[col * svl]); |
| 153 | + } |
| 154 | + } |
| 155 | + else if (col_idx > row_idx) |
| 156 | + { |
| 157 | + pSrc = &a[row_idx + col_idx * nbc]; |
| 158 | + pDst = &a_mod[(col_idx)*svl + row_idx * nbc]; |
| 159 | + // Load horizontal slices |
| 160 | + const svbool_t pg_row = svptrue_b32(); |
| 161 | + for (uint64_t row = 0; row < row_batch; row++) |
| 162 | + { |
| 163 | + svld1_hor_za32(0, row, pg_row, &pSrc[row * nbc]); |
| 164 | + } |
| 165 | + // Save vertical slices |
| 166 | + col_batch = MIN(col_batch, nbc - col_idx); |
| 167 | + for (uint64_t col = 0; col < col_batch; col++) |
| 168 | + { |
| 169 | + svst1_hor_za32(0, col, svptrue_b32(), &pDst[col * svl]); |
| 170 | + } |
| 171 | + } |
| 172 | + else if (col_idx < row_idx) |
| 173 | + { |
| 174 | + pSrc = &a[(row_idx)*nbc + col_idx]; |
| 175 | + pDst = &a_mod[(col_idx)*svl + row_idx * nbc]; |
| 176 | + // Load horizontal slices |
| 177 | + const svbool_t pg_row = svwhilelt_b32_u64(col_idx, nbc); |
| 178 | + for (uint64_t row = 0; row < row_batch; row++) |
| 179 | + { |
| 180 | + svld1_hor_za32(0, row, pg_row, &pSrc[row * nbc]); |
| 181 | + } |
| 182 | + // Save vertical slices |
| 183 | + col_batch = MIN(col_batch, nbc - col_idx); |
| 184 | + for (uint64_t col = 0; col < col_batch; col++) |
| 185 | + { |
| 186 | + svst1_ver_za32(0, col, svptrue_b32(), &pDst[col * svl]); |
| 187 | + } |
| 188 | + } |
| 189 | + } |
| 190 | + } |
| 191 | +} |
| 192 | + |
| 193 | +#endif |
| 194 | + |
| 195 | +// |
| 196 | +void CNAME(BLASLONG M, BLASLONG N, float alpha, float *__restrict A, |
| 197 | + BLASLONG strideA, float *__restrict B, BLASLONG strideB, |
| 198 | + float beta, float *__restrict R, BLASLONG strideR) |
| 199 | +{ |
| 200 | + uint64_t vl_elms = sve_cntw(); // vl_elem = 16 |
| 201 | + uint64_t m_mod = ceil((double)M / (double)vl_elms) * vl_elms; |
| 202 | + |
| 203 | + /* Pre-process the left matrix to make it suitable for |
| 204 | + matrix sum of outer-product calculation |
| 205 | + */ |
| 206 | + float *A_mod = (float *)malloc(m_mod * M * sizeof(float)); |
| 207 | + |
| 208 | +#if defined(UPPER) |
| 209 | + ssymm_direct_sme1_preprocessLU(M, M, A, A_mod); |
| 210 | +#elif defined(LOWER) |
| 211 | + ssymm_direct_sme1_preprocessLL(M, M, A, A_mod); |
| 212 | +#endif |
| 213 | + |
| 214 | + /* Calculate C = alpha*A*B + beta*C */ |
| 215 | + sgemm_direct_alpha_beta_sme1_2VLx2VL(M, M, N, &alpha, A_mod, B, &beta, R); |
| 216 | + free(A_mod); |
| 217 | +} |
| 218 | + |
| 219 | +#else |
| 220 | + |
| 221 | +void CNAME (BLASLONG M, BLASLONG N, float alpha, float * __restrict A,\ |
| 222 | + BLASLONG strideA, float * __restrict B, BLASLONG strideB ,\ |
| 223 | + float beta, float * __restrict R, BLASLONG strideR){} |
| 224 | + |
| 225 | +#endif |
0 commit comments