28.5 The fused element-local operators

SolverCore::AdvectionFusedElmtOp and SolverCore::AdvDiffFusedElmtOp compute the element-local part of a weak discontinuous Galerkin right-hand side,

∂u         a       v
∂t-+ ∇ ⋅(F  (u )- F  (u, ∇u )) = 0,
(28.2)

in one pass over the elements of each block: the first for an advective flux alone, the second for an advective and a viscous flux. For every element they form

  1. the state u on the element-local traces, as PhysTraceExtract does;

  2. for the advection-diffusion operator, the gradient ∇u, as PhysDeriv does;

  3. the fluxes Fa and, for the advection-diffusion operator, Fv, point by point;

  4. the negated derivative inner products of the flux vector, -(∇ϕ,Fv - Fa) for every variable ((∇ϕ,Fa) for the advection operator), at the quadrature points, as IProductWRTDerivBase does before its basis transpose;

  5. for the advection-diffusion operator, the element’s own outward normal viscous flux Fv ⋅n on its traces and, on the elements a caller lists, the gradient on the traces.

The gradient and the fluxes are held in workspace rather than in fields, so the separate chain’s volume-sized intermediate fields, and its passes over them, are gone. The right-hand side closes, as it does after the separate operators, with the trace flux and PhysLiftProject, whose physical-space inner products f are the operators’ output.

The stages the two passes share, the state on the traces and the derivative inner product, are the kernels of the advection operator, in library/SolverCore/Advection/AdvectionFusedElmt. The advection-diffusion operator, in library/SolverCore/AdvDiffusion/AdvDiffFusedElmt, calls them between its viscous stages; its block operators derive from the advection ones, whose shape, sizes and trace packing they share, and its implementations from the classes that hold what both implementations of an execution space share (AdvectionFusedElmtHostBlock and AdvectionFusedElmtDeviceBlock).

The viscous trace data are those of the average-of-normal-fluxes form of the interior-penalty flux, ViscousTraceFlux=AverageOfNormalFluxes in the redesign compressible flow solver, in which each interface averages the two elements’ own normal viscous fluxes. The gradient trace serves the traces that need the flux of averages instead, such as boundary traces: its elements are listed with

1void SetGradientTrace( 
2    LibUtilities::Field<TData, FieldState::Phys> *gradTrace, 
3    const std::vector<std::vector<unsigned int>> *elements);

which takes, for every block, the sorted indices of its listed elements; gradTrace holds those elements only, in list order, with NDIM ×NVAR components.

28.5.1 The flux policies

The operators are generic over a flux policy, a trivially copyable type that supplies the dimension, the number of variables and of auxiliary fields, a name, and the fluxes at one point. A policy of the advection operator supplies the advective flux:

1template <unsigned int DIM> struct EulerModel 
2{ 
3    static constexpr unsigned int NDIM = DIM; 
4    static constexpr unsigned int NVAR = DIM + 2; 
5    static constexpr unsigned int NAUX = 0; 
6    static constexpr const char *name  = "EulerModel2D"; // per DIM 
7 
8    EulerModel() = default; 
9    explicit EulerModel( 
10        const LibUtilities::SessionReaderSharedPtr &session); 
11 
12    template <typename TValue> 
13    NEK_HOSTDEVICE_INLINE void AdvFlux( 
14        const TValue (&state)[NVAR], const TValue *aux, 
15        TValue (&advFlux)[NDIM][NVAR]) const; 
16};

and a policy of the advection-diffusion operator the viscous flux as well:

1template <typename TValue> 
2NEK_HOSTDEVICE_INLINE void ViscFlux( 
3    const TValue (&state)[NVAR], const TValue (&grad)[NDIM][NVAR], 
4    const TValue *aux, TValue (&viscFlux)[NDIM][NVAR]) const;

Fluxes and gradients are direction-major: advFlux[d][n] is the flux of variable n in direction d. aux holds the policy’s NAUX auxiliary values at the point, such as a scaling of the viscosity, and is null when it has none. The entry points are templates on the value type, called with the SIMD type on Serial and AVX and with the scalar on the device, so one policy serves every execution space. The constructor from the session reads the policy’s parameters; the operator calls it once per block. The unit tests’ Euler and Navier–Stokes policies, in TestFusedElmtPolicies.hpp, are examples of each.

28.5.2 Registration and selection

Each operator follows the element-operator layout in its directory, with every file name prefixed by AdvectionFusedElmt or AdvDiffFusedElmt:

The block implementations are templated on the policy as well as on the execution space, implementation and data type. The policy belongs to the code that uses the operator, a solver or a unit test, so SolverCore cannot generate their registrations: the code supplying a policy registers the block operators for it, one call per policy,

1ADD_POLICY_ELMT_BLOCK_OPERATOR( 
2    ${CMAKE_SOURCE_DIR}/library/SolverCore/Advection/AdvectionFusedElmt 
3    EulerModel2D 
4    "Nektar::UnitTests::EulerModel<2>" 
5    UnitTests/SolverCore/TestFusedElmtPolicies.hpp 2 
6    TestAdvectionFusedElmtSources)

with the operator’s directory, the policy’s name in the generated file names, its C++ type, the header declaring it, its dimension and the list the generated sources are appended to. The generated sources are compiled into the target that list belongs to, the solver or the unit test, not into SolverCore. The function is defined in cmake/NektarCommon.cmake and finds the implementations by file name, as ADD_ELMT_OPERATOR does. It generates the registrations and the per-shape ShapeBlock definitions from the policy counterparts of that macro’s templates, SolverCore/ElmtBlockOpWithPolicyFactoryDec.cpp.in and SolverCore/BlockOpWithPolicyShapeBlock.cpp.in. A <name>BlockOpDeviceSumFacTOP.hpp header, where there is one, serves SumFacTOP on its own. The factory key is BlockAdvectionFusedElmt or BlockAdvDiffFusedElmt, the policy’s name, the execution space and the implementation, so Create() takes the policy by name:

1auto op = SolverCore::AdvectionFusedElmtOp<TData>::Create( 
2    expList, components, "EulerModel2D");

Create() returns null when no block operator is registered for the policy, execution space and implementation, or when one does not take a block of the mesh, so that a caller can fall back to the separate operators.

The implementation is chosen as for any element operator: with --opImpl, or under the key ADVECTIONFUSEDELMT or ADVDIFFFUSEDELMT of the backend sections of the session’s OPTIMISATION/OPERATORS. Serial and AVX implement SumFac; Device implements SumFacTOP. A block whose implementation is named nowhere takes its default (ElmtOp::GetOpImpl(), given the policy’s name so that it checks for the policy’s block operators); on Device that default is SumFac at low order, for which there is no fused block operator, and Create() then returns null. A session can name the implementation for every block:

1<OPTIMISATION> 
2  <OPERATORS> 
3    <DEVICEBACKEND> 
4      <ADVDIFFFUSEDELMT> SumFacTOP </ADVDIFFFUSEDELMT> 
5    </DEVICEBACKEND> 
6  </OPERATORS> 
7</OPTIMISATION>

28.5.3 Implementations

Serial and AVX (SumFac). The kernels process one SIMD vector of elements at a time and differentiate and extract all the variables of an element group together, through the multi-variable line-blocked kernels of PhysDerivSumFacStdKernels.hpp and PhysTraceExtractBlockOpSerialAVXSumFacKernels.hpp, so that each line of a differentiation matrix is read once for every variable. The fields are taken at any interleave width and leave at the width they arrived at.

Device (SumFacTOP). One kernel launch per block, one element per thread block and one thread per quadrature point; thread blocks stride over the elements of a block, and padding elements have their outputs zeroed. The kernel stages the transposed differentiation matrices in shared memory once, then for each element

  1. stages each variable in turn, forms its gradient for the advection-diffusion operator, and extracts its trace;

  2. for the advection-diffusion operator, writes the gradient trace of the listed elements, evaluates the viscous flux and extracts each of its components onto the traces, contracted with the outward normal;

  3. evaluates the advective flux and forms the flux vector;

  4. aligns each variable’s flux with the reference directions in shared memory and applies the transposed sum-factorised derivative, scaled by -1.

Staging one variable at a time bounds the shared memory by ∑ dnq,d2 + NDIMnq + w + nqtr values, with nq the element’s quadrature points, w the trace-extraction workspace and nqtr its trace points (the last for the advection-diffusion operator only), about 36 KB for a hexahedron of 103 points in double precision. The kernel reads and writes at width 1 and reshapes the fields to it and back.

28.5.4 Limits

A block outside these limits is refused, and Create() returns null.

28.5.5 Testing

TestAdvectionFusedElmt and TestAdvDiffFusedElmt compare the operators with the separate chain they replace:

They share one fixture (TestFusedElmt.hpp) and register an Euler policy for the advection operator and a Navier–Stokes one for the advection-diffusion operator, in one, two and three dimensions, and run them on every shape, on straight-sided meshes and, for every shape but the hexahedron, curved ones (a mixed mesh of quadrilaterals and triangles in two dimensions). Each runs at orders 1 to 8 on the modified and the orthogonal bases, each with the expansion’s own points and with one point more per direction, which covers the compile-time and runtime sizes of the size switch, and trace rules that are and are not the volume rules. Every case runs with the fields at width 1 and, where the execution space interleaves, at its vector width, and for the advection-diffusion operator with the gradient trace on every other element and on every element. They run on Serial, AVX and Device.