Nektar++
Loading...
Searching...
No Matches
UpwindSolverKernels.hpp
Go to the documentation of this file.
1///////////////////////////////////////////////////////////////////////////////
2//
3// File: UpwindSolverKernels.hpp
4//
5// For more information, please see: http://www.nektar.info
6//
7// The MIT License
8//
9// Copyright (c) 2006 Division of Applied Mathematics, Brown University (USA),
10// Department of Aeronautics, Imperial College London (UK), and Scientific
11// Computing and Imaging Institute, University of Utah (USA).
12//
13// Permission is hereby granted, free of charge, to any person obtaining a
14// copy of this software and associated documentation files (the "Software"),
15// to deal in the Software without restriction, including without limitation
16// the rights to use, copy, modify, merge, publish, distribute, sublicense,
17// and/or sell copies of the Software, and to permit persons to whom the
18// Software is furnished to do so, subject to the following conditions:
19//
20// The above copyright notice and this permission notice shall be included
21// in all copies or substantial portions of the Software.
22//
23// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
24// OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
25// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
26// THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
27// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
28// FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
29// DEALINGS IN THE SOFTWARE.
30//
31// Description:
32//
33///////////////////////////////////////////////////////////////////////////////
34
35#pragma once
36
37// The dimension and shape kernels. NOTE: They are NOT duplicate
38// templated version based on the array size like the
39// operators. HOWEVER, they are forced to be INLINED. The inlining is
40// critical so that when used in the templated version of the operator
41// that loop unrolling occurs.
42
43namespace Nektar::detail
44{
45
46template <typename ExecSpace> struct UpwindSolverKernel
47{
48
49 template <typename TScalar>
51 const size_t blksize, const unsigned velComps, const unsigned fluxComps,
52 const TScalar *velbase, const TScalar *normbase, const TScalar *fwdbase,
53 const TScalar *bwdbase, TScalar *fluxbase)
54 {
55 using std::abs;
56
57 // Explicit vectorisation for AVX backend, vec_t =
58 // tinysimd::simd<TScalar> for AVX, vec_t = TScalar otherwise.
59 using vec_t =
61 TScalar>::type;
62 const unsigned vec_width =
63 (std::is_same_v<ExecSpace, NektarSpaces::AVX>)
65 : 1;
66
67 // Assumes blksize is a whole number of vectors; checked on the host
68 // by FluxKernelLauncher, since a kernel may not assert.
69
70 // Parallelize over points; each i is independent
71 const size_t groupsize = blksize / vec_width;
72 const auto velvec = reinterpret_cast<const vec_t *>(velbase);
73 const auto normvec = reinterpret_cast<const vec_t *>(normbase);
74 const auto fwdvec = reinterpret_cast<const vec_t *>(fwdbase);
75 const auto bwdvec = reinterpret_cast<const vec_t *>(bwdbase);
76 auto fluxvec = reinterpret_cast<vec_t *>(fluxbase);
77
78 // Build nv = v·n on the fly
79 vec_t nv = 0.0;
80 for (unsigned d = 0; d < velComps; ++d)
81 {
82 const vec_t v_i = velvec[d * groupsize];
83 const vec_t n_i = normvec[d * groupsize];
84 nv += v_i * n_i;
85 }
86
87 const vec_t nv_abs = abs(nv);
88
89 // Branchless split: nv_pos=max(nv,0), nv_neg=min(nv,0)
90 const vec_t nv_pos = 0.5 * (nv + nv_abs);
91 const vec_t nv_neg = 0.5 * (nv - nv_abs);
92
93 // Blend without branches:
94 // flux = nv_pos * Fwd + nv_neg * Bwd
95 for (unsigned nc = 0; nc < fluxComps; ++nc)
96 {
97 fluxvec[nc * groupsize] = nv_pos * fwdvec[nc * groupsize] +
98 nv_neg * bwdvec[nc * groupsize];
99 }
100 }
101};
102
103} // namespace Nektar::detail
#define NEK_DEVICE_INLINE
Definition Backends.hpp:283
tinysimd::simd< NekDouble > vec_t
scalarT< T > abs(scalarT< T > in)
Definition scalar.hpp:294
typename abi< ScalarType, width >::type simd
Definition tinysimd.hpp:138
NEK_DEVICE_INLINE void operator()(const size_t blksize, const unsigned velComps, const unsigned fluxComps, const TScalar *velbase, const TScalar *normbase, const TScalar *fwdbase, const TScalar *bwdbase, TScalar *fluxbase)