28.1 File structure

Within the ElmtOps directory there are a series of sub-directories which Function short name from table 28.1. In configuration using CMake the filename is used to identify which operators are available for compilation and so adding a new elemental operator MyOps would require a new directory of the same name.

Also wihtin the ElmtOps directory you will find a series of .hpp files with the naming format Operator{FunctionShortName}.hpp where the FunctionShortName again refers to that provided in table 28.1. These files contain the parent class definition typically names Operator{FunctionShortName}. As indicated by figure 28.1 from doxygen all of these classes inherit public methods from the OperatorElmt class which is defined in OperatorElmt.hpp which also inherits from the Operator class defined in the file Common/Operator.hpp file in the MultiRegions library.


pict

Figure 28.1 inheritance of element operator class


As indicated in figure 28.1, three classes also inherit from the class OperatorBwdTrans and this relates to a CPU/AVX sum factorisation implementation, a Standard matrix CPU implementation and a GPU implementation with variants for Cuda, Kokkos and SYCL. We will next discuss this formatting for each of these implementations.

28.1.1 Sum factorisation Matrix operators on CPUs with and without AVX

Sum factorisation utilises the tensor product nature of the expansions and permits a series of one-dimensional type operations. Using the SIMD library approach discussed in section 4.11 we are able to reuse the same code for both a scalar and vector approach which can accomdate the sum factorisation over each elements at a time in the case of a scalar approach or multiple elements in the case of an AVX acceleration where the number of elements depends on the AVX instruction set. More details of the AVX implementation can be found in reference [57].

The derived implementation class should be placed in a file named

{FunctionShortName}BlockOpSerialAVXSumFac.hpp

where as previously discussed FunctionSHortName is proivded in table 28.1 for each operator.

In this file the size of the SIMD type is selected from definining simd_t, i.e.

1template <typename ExecSpace, typename Implementation, typename TData> 
2class OperatorBwdTransImpl : public OperatorBwdTrans<TData> 
3{ 
4    using simd_t = 
5        typename simd_type_if<std::is_same_v<ExecSpace, NektarSpaces::AVX>, 
6        TData>::type;

where simd_type_if is defind in MultiRegions/Common/Operator.hpp as

1template <bool B, typename TData> struct simd_type_if 
2{ 
3    typedef tinysimd::scalarT<TData> type; 
4}; 
5 
6template <typename TData> struct simd_type_if<true, TData> 
7{ 
8    typedef tinysimd::simd<TData> type; 
9};

Therefore by default simd_t is defined as tinysimd::scalarT<TData> but if ExexSpace is NektarSpaces::AVX then it is set to tinysimd::simd<TData> .

This class holds appropriate intialisation such as refernece to the 1D Expansion basis and also has the specific implementation of the apply() method. This method has a generic form which just loops over the blocks in the form:

1void apply(Field<TData, FieldState::Coeff> &in, 
2           Field<TData, FieldState::Phys> &out) override 
3{ 
4    // Initialize index. 
5    size_t exp_idx = 0; 
6 
7    for (size_t blk = 0; blk < in.GetBlocks().size(); ++blk) 
8    { 
9        m_expPtr = this->m_expansionList->GetExp(exp_idx); 
10 
11        // Block dependent. 
12        auto &inblock  = in.GetBlocks()[blk]; 
13        auto &outblock = out.GetBlocks()[blk]; 
14 
15        this->BlockOperator(inblock, outblock); 
16 
17        // Increment index for next element type. 
18        exp_idx += inblock.GetNumElements(); 
19    } 
20}

The method BlockOperator() then primarily contains a switch statement which calls shape dependent block operators, i.e.

1switch (shapeType) 
2{ 
3    // Segment 
4    case LibUtilities::Seg: 
5    { 
6        SegBlock(inblock, outblock); 
7        break; 
8    } 
9    // Quads 
10    case LibUtilities::Quad: 
11    { 
12        QuadBlock(inblock, outblock); 
13        break; 
14    } 
15    // Triangles 
16    case LibUtilities::Tri: 
17    { 
18        TriBlock(inblock, outblock); 
19        break; 
20    } 
21    .... 
22    }

These shape block operators are then defined in separate files which are auto generated using the Cmake Configure_file() method. This file is named

{FunctionShortName}BlockOpSerialAVXSumFac.cpp.in

and includes another auto generated file of the form BlockOpSwitchCodeShape.h where Shape is Seg,Quad,Tri,Hex,Prism,Pyr,Tet. In these file we use Boost preprocessing to generate a switch statement to call templated versions of the methods Operator1D(), Operator2D() or Operator3D() which are defined in the original file

{FunctionShortName}BlockOpSerialAVXSumFac.hpp.

The switch statement runs over all polynomial or qudarture orders that are defined by the Cmake defined options NEKTAR_SWITCH_MIN and NEKTAR_SWITCH_MAX and assume that each polynomial order is the same and that the standard quadrature orders are applied for each shape. For operators involving both polynomial and quadrature orders the quadrature order runs from the polynomial order to NEKTAR_SWITCH_QMAX_MUL times the polynomial order plus NEKTAR_SWITCH_QMAX_ADD, and the polynomial order from NEKTAR_SWITCH_MIN to NEKTAR_SWITCH_MAX; these are CMake cache variables, by default 2 to 6 modes per direction with up to two points more than modes. A tetrahedron also has templated cases for equal point counts in every direction, the rule a Gauss point set on each collapsed direction gives. A size outside the range runs through the non-templated method and prints a warning naming it once, so raising the range is the remedy when a production case reports one. The boost preprocessing code look in the files similar to Common/BlockOpSwitchCode.h.in. This rather convoluted file structure helps abstract the boost prepropcessing codes and also speeds up the compilation which otherwise is rather slow due the large switch statement that are generated to call the templated code.

Finally the methods Operator1D(), Operator2D() or Operator3D() are defined in a templated and non-templated method for use in the switch statements and to also have a default method for cases whcih are not covered in the templated parameters in the switch statement. These methods also call kernel functions for each method that contain the primary code for each method and can be found in the files

{FunctionShortName}BlockOpSerialAVXSumFacKernels.hpp

and also in the StdRegions/Operators/ directory.

28.1.2 Standard Matrix operators on CPUs

The idea behind the standard matrix approach is to identify, where possible, a matrix operator in standard region which can be used on a whole block of elements. More details on this approach can be found in [58]. This approach is currently only setup for serial operations on a CPU.

If it is possible to define a standard matrix approach the derived implementation class can be defined in a file named

{FunctionShortName}BlockOpSerialAVXStdMat.hpp.

where as previously FunctionShortName is taken from table 28.1. In constructing the method a series of standard matrices are setup for each elemental block. These matrices are of dimension of the total number of quadrature points or expansion modes and so can become quite large for higher polynomial orders.

The apply() method once again looks over each series of blocks and essentially makes calls to a large blas3 DGEMM() matrix-matrix calls invovling the quadrature points or epxansion modes and the number of elements in a block.

28.1.3 GPU using Kokkos, SYCL and native coding

The file and class structure of the GPU devices is anaologous to the Serial/AVX structure. The derived implementation class is defined in the file

{FunctionShortName}BlockOpDeviceSumFac.hpp.

where as previously FunctionShortName is taken from table 28.1. This file contains the required initialisation for a given operator and the apply() method loops over the blocks of elements and evaluate the operation in an identical manner to the previous implementations. As previously outlined the apply() method calls a BlockOperator() method which contains a switch statement to call a shape dependent method, i.e. TriBlock(), QuadBlock(), HexBlock(), ... and as previously these shape dependent methods are auto generated in different files to help with compilation speed and to wrap the primary method in a boost prepocessing auto generated swith statement to allow for the possibility of a templated veresion of the operator to be called if the quadrature and polynomial order of the elements in a block are of equal order polynomial order in all dimensions and have the standard quadrature points used in equi-ordered polynomial expansions. The details for the auto generated shape dependent block operator code can be found in the files

{FunctionShortName}BlockOpDeviceSumFac.cpp.in

and

{FunctionShortName}BlockOpDeviceSumFacQP.cpp.in

where QP refers to Quadrature Point implementation strategy outlined below. The boost preprocessing directives called from these files can be found in the file

Common/BlockOpSwitchCode.h.in.

As mentioned previously the polynomial and quadrature range of the switch is determined by the Cmake defined parameters NEKTAR_SWITCH_MIN and NEKTAR_SWITCH_MAX.

The shape based block operator methods call the methods Operator1D(), Operator2D() or Operator3D() which are defined in the original file

{FunctionShortName}BlockOpDeviceSumFac.hpp.

where there is either a templated verion of the method with polynomial and quadrature order as template values or non-templated method which acts as a default for the switch statements.

THe method Operator1D(), Operator2D() or Operator3D() contain code to check the input and output arrays for the correct interlacing, initialise work space as well as access the required data for he kernel operators such as the basis array or 1D derivaitve matrices. This data is then passed to a kernel method which is contained in

{FunctionShortName}BlockOpDeviceSumFacKernels.hpp.

This file then includes method specific kernels for the Cuda, Kokkos or SYCL implementations. The inclusion of the Cuda, Kokkos and SYCL code depends on the CMAKE options NEKTAR_ENABLE_CUDA, NEKTAR_ENABLE_KOKKOS and NEKTAR_ENABLE_SYCL and the related kernel code can be found in the files:

{FunctionShortName}CUDASumFacKernels.cuh,
{FunctionShortName}KokkosSumFacKernels.cuh,
{FunctionShortName}SYCLSumFacKernels.cuh.

Finally we have two implementations of the sum factorisation kernels, denoted SumFac and SumFacQP. The first SumFac approach maps each elemental operation to a CUDA-thread for a completely vectorised kernel, whilst in the second SumFacQP approach, each element is mapped to a CUDA-block for nested parallelism and requires an indexing to each quadrature point (QP) or modal expansion. Results from [29] show that the first approach is beneficial for elements with low polynomial order, whereas the second approach is beneficial for elements of higher order.