Apply() by ExampleIn order to apply an element operator to a field in either physical or coefficient representation,
a matrix-matrix multiplication has to be performed. This multiplication is currently
implemented in three variations: the standard matrix (StdMat), the sum-factorisation
(SumFac), and the threaded-by-output-point sum-factorisation (SumFacTOP) approach.
These implementations are wrapped in the Apply() function of each operator class and which
device specific implementation that is executed depends on the execStr and the
implStr with which an operator is initialized. The standard matrix approach is the
most straightforward and involves a direct multiplication of the element operator
matrix with the input field matrix. The sum-factorisation approach on the other
hand, leverages the tensor-product structure of the basis functions to reduce memory
consumption and increase computational intensity. Finally, the threaded-by-output-point
sum-factorisation approach takes the sum-factorisation and assigns a thread to each output
point and it’s associated operations, which can be particularly beneficial when using
higher order polynomial bases due to the elevated amount of work this causes per
thread.
In order to understand the three approaches, we consider the application of the Backward
transform operator. We assume a quadrilateral mesh consisting of N rectangles that each
carry a tensorial bases made up of a one-dimensional expansion of order P in both
directions. This allows us to restrain ourselves to the different implementations of
the Apply() function within what Nektar++ refers to as a block of elements. A
block of elements consists of multiple elements with the same shape, expansion base,
and quadrature. Blocks are also made up of either all "Regular" or all "Deformed"
elements. The notation used in this section is taken from [46]. In order to perform the
backward transform and evaluate the physical values at the Q quadrature points, of the
block we are considering, in each direction, we need to multiply the coefficient
matrix:
|
|
by the operator matrix:
|
|
Here, ϕp(ξi) denotes the p-th one-dimensional basis function evaluated at the i-th quadrature
point for p ∈,i ∈
. The 2D basis functions are constructed
as the tensor product of the 1D basis functions, i.e., ϕpq(ξi,ξj) = ϕp(ξi)ϕq(ξj) for
p,q ∈
,i,j ∈
. The values ûpqe denote the coefficients of the
expansion for element e ∈
.
We denote the resulting physical values at the quadrature points for each element as
ui,je := u. The one-to-one maps χ1e,χ2e take the local coordinates of the
element e to the global coordiantes x1 and x2 respectively. The physical values and the
coefficients relate as follows:
|
|
The described matrix-matrix multiplication contains all operations that need to be performed during the backwards transformation. For later use with the sum factorisation approaches we introduce a visualisation technique for the standard matrix approach as seen in figure 28.2. The individual colours serve to represent one element each in either physical or coefficient space. The matrix visualized by black dots show the operator matrix B, which is applied to the coefficient matrix û on the right to obtain the physical values u on the left. The work assigned to a single thread is determined by the underlying function call as opposed to the implementations introduced later in this section, where a more granular load balancing is performed.
blas, cublas, or hipblas.
In Nektar++, the actual matrix-matrix multiplication is performed using the NekBlas::Gemm
function, which in turn calls blas, cublas, or hipblas depending on the execution space of
the operator and the device backend Nektar++ was built with. The generic interface NekBlas.hpp takes an opaque NekBlas::Handle<ExecSpace>, which only carries the stream
ID; it includes no BLAS library header. Each backend implements the routines in its own
compilation unit (blas.cpp, cuBlas.cpp, hipBlas.cpp, ...), where it looks up its library
handle for the stream. The cuBLAS implementation of NekBlas::Gemm is shown in listing
28.1.
1template <typename TData> 2void Gemm(Handle<NektarSpaces::Device> handle, std::string transposeA, 3 std::string transposeB, const int M, const int N, const int K, 4 const TData alpha, const TData *a, const int lda, const TData *b, 5 const int ldb, const TData beta, TData *c, const int ldc) 6{ 7 auto cublasHandle = GetCuBlasHandle(handle.GetStreamID()); 8 9 auto transA = (transposeA == "N") ? CUBLAS_OP_N : CUBLAS_OP_T; 10 auto transB = (transposeB == "N") ? CUBLAS_OP_N : CUBLAS_OP_T; 11 12 if constexpr (std::is_same_v<TData, float>) 13 { 14 CUBLAS_CHECK(cublasSgemm(cublasHandle, transA, transB, M, N, K, 15 &alpha, a, lda, b, ldb, &beta, c, ldc)); 16 } 17 else if constexpr (std::is_same_v<TData, double>) 18 { 19 CUBLAS_CHECK(cublasDgemm(cublasHandle, transA, transB, M, N, K, 20 &alpha, a, lda, b, ldb, &beta, c, ldc)); 21 } 22}
The arguments of the NekBlas::Gemm call and their meanings are listed in table 28.2.
| Parameter | Role in the Matrix Equation (Z = α ⋅X ⋅Y + β ⋅Z) | Value in BwdTrans |
handle | Opaque handle for the execution space and stream. | NekBlas:: Handle<ExecSpace>:: GetInstance(streamID) |
transposeA | Specifies if matrix X should be transposed. | "N" → no transpose |
transposeB | Specifies if matrix Y should be transposed. | "N" → no transpose |
M | Number of rows in matrices X and Z. | Q2 |
N | Number of columns in matrices Y and Z. | N |
K | Number of columns in X and rows in Y . | P2 |
alpha | The scalar multiplier α. | 1.0 |
a | Pointer to the data for matrix X. | Pointer to operator matrix B |
lda | The leading dimension of matrix X. | Q2 |
b | Pointer to the data for matrix Y . | Pointer to vector matrix û |
ldb | The leading dimension of matrix Y . | P2 |
beta | The scalar multiplier β. | 0.0 |
c | Pointer to the data for the output matrix Z. | Pointer to physical value vector u |
ldc | The leading dimension of matrix Z. | Q2 |
Having introduced the standard matrix approach, we can now turn to the implementation of the sum-factorisation approach. In order to reduce memory consumption and increase computational intensity, the sum-factorisation approach decomposes the operator matrix B into two matrices Be:
|
|
The field coefficient matrix û is decomposed as well:
|
|
and the element-wise computation of u is performed in two "stages" that correspond to the two directions (referred to as direction 0 and direction 1) of our quadrilaterals:
| Beûe | = |
This decomposition results in lighter memory consumption and a higher computational intensity, as the number of operations per memory access is increased. As for the StdMat approach, we can visualize the sum-factorisation approach using the color scheme introduced earlier. The visualization for the sum-factorised approach is shown in figure 28.3. The individual colours serve to represent one element each in either physical or coefficient space. The matrices with entries visualized as black dots are operator matrices Be and (Be)T , which is applied to the coefficient matrix ue on the right to obtain the physical values ue on the left. In the visualization we have rearranged the results into the same memory layout as for the standard matrix approach to better illustrate the equivalence of both approaches. All operations related to a single element and accordingly performed by one thread, were marked using purple bounding boxes and arrows.
This visualization, like the matrix formulation describes the computations related to one
element. In the implementation of Nektar++, one thread handles one element at a time, as can
be seen in the cuda implementation in listing 28.2:
1template <typename tdata> 2nek_device_inline static void BwdTransQuadSumFacKernel( 3 const unsigned int ilane, const unsigned int nm0, const unsigned int nm1, 4 const unsigned int nq0, const unsigned int nq1, 5 const tdata *__restrict__ basis0, const tdata *__restrict__ basis1, 6 const tdata *__restrict__ in, tdata *__restrict__ out, 7 tdata *__restrict__ wsp) 8{ 9 constexpr unsigned int warpsize = nektarspaces::vector_width<tdata>::value; 10 11 for (unsigned int i = 0u; i < nq0; ++i) 12 { 13 // direction 0 14 for (unsigned int q = 0u, cnt_qp = 0u; q < nm1; ++q) 15 { 16 tdata tmp = 0.0; 17#pragma unroll 18 for (unsigned int p = 0u; p < nm0; ++p, ++cnt_qp) 19 { 20 tmp += in[warpsize * cnt_qp + ilane] * basis0[p * nq0 + i]; 21 } 22 wsp[warpsize * q + ilane] = tmp; 23 } 24 25 // direction 1 26 for (unsigned int j = 0u; j < nq1; ++j) 27 { 28 tdata tmp = 0.0; 29#pragma unroll 30 for (unsigned int q = 0u; q < nm1; ++q) 31 { 32 tmp += wsp[warpsize * q + ilane] * basis1[q * nq1 + j]; 33 } 34 out[warpsize * (nq0 * j + i) + ilane] = tmp; 35 } 36 } 37}
cuda back-end are listed in table
28.3:
| Parameter | Role in the Sum-Factorisation Kernel | Value in BwdTrans |
ilane | The unique ID of the current thread within its warp, selects the data for the specific element this thread is processing. | Thread index (0 to warpsize-1) |
nm0 | The number of modes (coefficients) in direction 0. Defines the inner loop size for stage 1. | Num. modes in direction 0, P + 1 |
nm1 | The number of modes in direction 1. Defines the inner loop size for stage 2 and outer loop for stage 1. | Num. modes in direction 1, P + 1 |
nq0 | The number of quadrature points in direction 0. Defines the main outer loop boundary. | Num. quadrature points in direction 0, Q |
nq1 | The number of quadrature points in direction 1. Defines the outer loop boundary for stage 2. | Num. quadrature points in direction 1, Q |
basis0 | Pointer to the 1D basis matrix used in stage 1. | Basis matrix for direction 0, Be |
basis1 | Pointer to the 1D basis matrix used in stage 2. | Basis matrix for direction 1, Be |
in | Pointer to the input array containing the modal coefficients for a batch of elements. | Input coefficients, ûe |
out | Pointer to the output array for the final physical values of the element batch. | Output physical values, ue |
wsp | Pointer to a temporary workspace array for storing the intermediate results between stage 1 and stage 2. | Intermediate results, Beûe |
Finally, the threaded-by-output-point sum-factorisation approach further decomposes the computation by assigning a thread to each output point ui,je and its associated operations. The computation of the value at one output point ui,je is however also performed in two stages, one for each direction:
| ti,pe | = ∑ k=0P û kpe,ϕ k(ξi) ∀i ∈{0,…,Q -1},p ∈{0,…,P} | ||
| ui,je | = ∑ p=0P t i,pϕp(ξj), ∀i,j ∈{0,…,Q -1} |
Like for previous approaches, we can visualize the threaded-by-output-point sum-factorisation approach using the established colour scheme. The matrices with entries visualized as black dots are operator matrices Be and (Be)T , which is applied to the coefficient matrix ue on the right to obtain the physical values ue on the left. The number of operations has not changed compared to the regular sum-factorisation approach, but the way the work is distributed among individual threads is different. In figure 28.4 we have marked all operations that are performed by one thread using pink and purple lines that correspond to the first and second basis in the 0 as well as the 1 direction respectively. Like in the visualization of the sum-factorisation approach, we have rearranged the results into the same memory layout as for the standard matrix approach to better illustrate the equivalence of all approaches.
The implementation of the threaded-by-output-point sum-factorisation approach in Nektar++
is implemented in two stages, just like described above. In listing 28.3 we show the cuda
implementation of the threaded-by-output-point sum-factorisation kernel. The two stages can
clearly be seen in the two main loops, one for each direction and separated by a barrier that
assures that the intermediate results are synchronized between all threads before proceeding to
the second stage.
1template <typename TthreadBlock, typename TData> 2NEK_DEVICE_INLINE static void BwdTransQuadSumFacTOPKernel( 3 const unsigned int nm0, const unsigned int nm1, const unsigned int nq0, 4 const unsigned int nq1, const unsigned int nqTot, 5 const TData *__restrict__ basis0, const TData *__restrict__ basis1, 6 const TData *__restrict__ in, TData *__restrict__ out, 7 TData *__restrict__ wsp, const TthreadBlock &threadBlock) 8{ 9 const unsigned int idx0 = getLocalIdx(threadBlock); 10 const unsigned int stride = getLocalRange(threadBlock); 11 12 // direction 0 13 for (unsigned int idx = idx0; idx < nq0 * nm1; idx += stride) 14 { 15 const unsigned int q = idx % nm1; 16 const unsigned int i = idx / nm1; 17 unsigned int cnt_qp = nm0 * q; 18 19 TData tmp = 0.0; 20#pragma unroll 21 for (unsigned int p = 0u; p < nm0; ++p, ++cnt_qp) 22 { 23 tmp += in[cnt_qp] * basis0[p * nq0 + i]; 24 } 25 wsp[idx] = tmp; 26 } 27 28 localBarrier(threadBlock); 29 30 // direction 1 31 for (unsigned int idx = idx0; idx < nqTot; idx += stride) 32 { 33 const unsigned int i = idx % nq0; 34 const unsigned int j = idx / nq0; 35 unsigned int cnt_iq = nm1 * i; 36 37 TData tmp = 0.0; 38#pragma unroll 39 for (unsigned int q = 0u; q < nm1; ++q, ++cnt_iq) 40 { 41 tmp += wsp[cnt_iq] * basis1[q * nq1 + j]; 42 } 43 out[idx] = tmp; 44 } 45 46 localBarrier(threadBlock); 47}
The arguments and their meanings of the BwdTransQuadSumFacTOPKernel function for the
cuda back-end are listed in the table 28.4.
| Parameter | Role in the Sum-Factorisation Kernel | Value in BwdTrans |
nm0 | The number of modes (coefficients) in direction 0. Defines the inner loop size for stage 1. | Num. modes in dir 0, P + 1 |
nm1 | The number of modes in direction 1. Defines the inner loop size for stage 2 and outer loop for stage 1. | Num. modes in dir 1, P + 1 |
nq0 | The number of quadrature points in direction 0. | Num. quadrature points in dir 0, Q |
nq1 | The number of quadrature points in direction 1. | Num. quadrature points in dir 1, Q |
nqTot | The total number of quadrature points (nq0 ×nq1). Defines the loop boundary for stage 2. | Total quadrature points, Q2 |
basis0 | Pointer to the 1D basis matrix used in stage 1. | Basis matrix for dir 0, Be |
basis1 | Pointer to the 1D basis matrix used in stage 2. | Basis matrix for dir 1, Be |
in | Pointer to the input array containing the modal coefficients for a single element. | Input coefficients, ûe |
out | Pointer to the output array for the final physical values of the single element. | Output physical values, ue |
wsp | Pointer to a workspace array shared by all threads in the block to store intermediate results. | Intermediate results, w |
threadBlock | A handle to the GPU thread block, used to manage thread indices and synchronization. | GPU thread block context |