|
| 1 | +// Copyright (c) 2017-2022, Lawrence Livermore National Security, LLC and other CEED contributors. |
| 2 | +// All Rights Reserved. See the top-level LICENSE and NOTICE files for details. |
| 3 | +// |
| 4 | +// SPDX-License-Identifier: BSD-2-Clause |
| 5 | +// |
| 6 | +// This file is part of CEED: http://github.com/ceed |
| 7 | + |
| 8 | +#include <ceed/ceed.h> |
| 9 | +#include <ceed/backend.h> |
| 10 | +#ifdef __ARM_FEATURE_SVE |
| 11 | +#include <arm_sve.h> |
| 12 | +#endif |
| 13 | +#include <stdbool.h> |
| 14 | +#include "ceed-sve.h" |
| 15 | + |
| 16 | +//------------------------------------------------------------------------------ |
| 17 | +// Blocked Tensor Contract |
| 18 | +//------------------------------------------------------------------------------ |
| 19 | +static inline int CeedTensorContract_Sve_Blocked(CeedTensorContract contract, |
| 20 | + CeedInt A, CeedInt B, CeedInt C, CeedInt J, const float *restrict t, |
| 21 | + CeedTransposeMode t_mode, const CeedInt add, const float *restrict u, |
| 22 | + float *restrict v, const CeedInt JJ) { |
| 23 | + CeedInt t_stride_0 = B, t_stride_1 = 1; |
| 24 | + if (t_mode == CEED_TRANSPOSE) { |
| 25 | + t_stride_0 = 1; t_stride_1 = J; |
| 26 | + } |
| 27 | + |
| 28 | + for (CeedInt a=0; a<A; a++) |
| 29 | + for (CeedInt b=0; b<B; b++) |
| 30 | + // Blocks of JJ rows |
| 31 | + for (CeedInt j=0; j<(J/JJ)*JJ; j+=JJ) |
| 32 | + for (CeedInt jj=0; jj<JJ; jj++) // unroll |
| 33 | + // C vectorization by compiler |
| 34 | + for (int32_t c=0; c<C; c+=svcntd()) { |
| 35 | + svbool_t pg = svwhilelt_b32(c, C); |
| 36 | + // Load u, v into vectors |
| 37 | + svfloat32_t u_vec = svld1(pg, &u[(a*B+b)*C+c]); |
| 38 | + svfloat32_t v_vec = svld1(pg, &v[(a*J+j+jj)*C+c]); |
| 39 | + // Basis matrix value |
| 40 | + float tq = t[(j+jj)*t_stride_0 + b*t_stride_1]; |
| 41 | + // fmadd |
| 42 | + svst1(pg, &v[(a*J+j+jj)*C+c], svmla_x(pg, v_vec, u_vec, tq)); |
| 43 | + } |
| 44 | + |
| 45 | + // Remainder of rows |
| 46 | + CeedInt j=(J/JJ)*JJ; |
| 47 | + if (j < J) |
| 48 | + for (CeedInt a=0; a<A; a++) |
| 49 | + for (CeedInt b=0; b<B; b++) |
| 50 | + // Blocks of JJ rows |
| 51 | + for (CeedInt jj=0; jj<J-j; jj++) // not unrolled |
| 52 | + // C vectorization by compiler |
| 53 | + for (int32_t c=0; c<C; c+=svcntd()) { |
| 54 | + svbool_t pg = svwhilelt_b32(c, C); |
| 55 | + // Load u, v into vectors |
| 56 | + svfloat32_t u_vec = svld1(pg, &u[(a*B+b)*C+c]); |
| 57 | + svfloat32_t v_vec = svld1(pg, &v[(a*J+j+jj)*C+c]); |
| 58 | + // Basis matrix value |
| 59 | + float tq = t[(j+jj)*t_stride_0 + b*t_stride_1]; |
| 60 | + // fmadd |
| 61 | + svst1(pg, &v[(a*J+j+jj)*C+c], svmla_x(pg, v_vec, u_vec, tq)); |
| 62 | + } |
| 63 | + |
| 64 | + return CEED_ERROR_SUCCESS; |
| 65 | +} |
| 66 | + |
| 67 | +//------------------------------------------------------------------------------ |
| 68 | +// Blocked Tensor Contract |
| 69 | +//------------------------------------------------------------------------------ |
| 70 | +static inline int CeedTensorContract_Sve_Serial(CeedTensorContract contract, |
| 71 | + CeedInt A, CeedInt B, CeedInt C, CeedInt J, const float *restrict t, |
| 72 | + CeedTransposeMode t_mode, const CeedInt add, const float *restrict u, |
| 73 | + float *restrict v, const CeedInt JJ) { |
| 74 | + CeedInt t_stride_0 = B, t_stride_1 = 1; |
| 75 | + if (t_mode == CEED_TRANSPOSE) { |
| 76 | + t_stride_0 = 1; t_stride_1 = J; |
| 77 | + } |
| 78 | + |
| 79 | + for (CeedInt a=0; a<A; a++) |
| 80 | + for (CeedInt b=0; b<B; b++) |
| 81 | + for (CeedInt j=0; j<(J/JJ)*JJ; j+=JJ) |
| 82 | + for (CeedInt jj=0; jj<JJ; jj++) // unroll |
| 83 | + v[a*J+(j+jj)] += t[(j+jj)*t_stride_0 + b*t_stride_1] * u[a*B+b]; |
| 84 | + |
| 85 | + CeedInt j=(J/JJ)*JJ; |
| 86 | + if (j < J) |
| 87 | + for (CeedInt a=0; a<A; a++) |
| 88 | + for (CeedInt b=0; b<B; b++) |
| 89 | + for (CeedInt jj=0; jj<J-j; jj++) // not unrolled |
| 90 | + v[a*J+(j+jj)] += t[(j+jj)*t_stride_0 + b*t_stride_1] * u[a*B+b]; |
| 91 | + |
| 92 | + return CEED_ERROR_SUCCESS; |
| 93 | +} |
| 94 | + |
| 95 | +//------------------------------------------------------------------------------ |
| 96 | +// Tensor Contract - Common Sizes |
| 97 | +//------------------------------------------------------------------------------ |
| 98 | +static int CeedTensorContract_Sve_Blocked_8(CeedTensorContract contract, |
| 99 | + CeedInt A, CeedInt B, CeedInt C, CeedInt J, const float *restrict t, |
| 100 | + CeedTransposeMode t_mode, const CeedInt add, const float *restrict u, |
| 101 | + float *restrict v) { |
| 102 | + return CeedTensorContract_Sve_Blocked(contract, A, B, C, J, t, t_mode, add, u, |
| 103 | + v, 8); |
| 104 | +} |
| 105 | +static int CeedTensorContract_Sve_Serial_8(CeedTensorContract contract, |
| 106 | + CeedInt A, CeedInt B, CeedInt C, CeedInt J, const float *restrict t, |
| 107 | + CeedTransposeMode t_mode, const CeedInt add, const float *restrict u, |
| 108 | + float *restrict v) { |
| 109 | + return CeedTensorContract_Sve_Serial(contract, A, B, C, J, t, t_mode, add, u, v, |
| 110 | + 8); |
| 111 | +} |
| 112 | + |
| 113 | +//------------------------------------------------------------------------------ |
| 114 | +// Tensor Contract Apply |
| 115 | +//------------------------------------------------------------------------------ |
| 116 | +static int CeedTensorContractApply_Sve(CeedTensorContract contract, CeedInt A, |
| 117 | + CeedInt B, CeedInt C, CeedInt J, |
| 118 | + const float *restrict t, |
| 119 | + CeedTransposeMode t_mode, |
| 120 | + const CeedInt add, |
| 121 | + const float *restrict u, |
| 122 | + float *restrict v) { |
| 123 | + if (!add) |
| 124 | + for (CeedInt q=0; q<A*J*C; q++) |
| 125 | + v[q] = (float) 0.0; |
| 126 | + |
| 127 | + if (C == 1) |
| 128 | + CeedTensorContract_Sve_Serial_8(contract, A, B, C, J, t, t_mode, true, u, v); |
| 129 | + else |
| 130 | + CeedTensorContract_Sve_Blocked_8(contract, A, B, C, J, t, t_mode, true, u, v); |
| 131 | + |
| 132 | + return CEED_ERROR_SUCCESS; |
| 133 | +} |
| 134 | + |
| 135 | +//------------------------------------------------------------------------------ |
| 136 | +// Tensor Contract Create |
| 137 | +//------------------------------------------------------------------------------ |
| 138 | +int CeedTensorContractCreate_f32_Sve(CeedBasis basis, |
| 139 | + CeedTensorContract contract) { |
| 140 | + int ierr; |
| 141 | + Ceed ceed; |
| 142 | + ierr = CeedTensorContractGetCeed(contract, &ceed); CeedChkBackend(ierr); |
| 143 | + |
| 144 | + ierr = CeedSetBackendFunction(ceed, "TensorContract", contract, "Apply", |
| 145 | + CeedTensorContractApply_Sve); CeedChkBackend(ierr); |
| 146 | + |
| 147 | + return CEED_ERROR_SUCCESS; |
| 148 | +} |
| 149 | +//------------------------------------------------------------------------------ |
0 commit comments