Neko 1.99.11
A portable framework for high-order spectral element flow simulations
Loading...
Searching...
No Matches
convect_scalar_kernel.h
Go to the documentation of this file.
1#ifndef __MATH_CONVECT_SCALAR_KERNEL_H__
2#define __MATH_CONVECT_SCALAR_KERNEL_H__
3
4#include "elem_block.h"
5/*
6 Copyright (c) 2021-2025, The Neko Authors
7 All rights reserved.
8
9 Redistribution and use in source and binary forms, with or without
10 modification, are permitted provided that the following conditions
11 are met:
12
13 * Redistributions of source code must retain the above copyright
14 notice, this list of conditions and the following disclaimer.
15
16 * Redistributions in binary form must reproduce the above
17 copyright notice, this list of conditions and the following
18 disclaimer in the documentation and/or other materials provided
19 with the distribution.
20
21 * Neither the name of the authors nor the names of its
22 contributors may be used to endorse or promote products derived
23 from this software without specific prior written permission.
24
25 THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
26 "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
27 LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
28 FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
29 COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
30 INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
31 BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
32 LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
33 CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
34 LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
35 ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
36 POSSIBILITY OF SUCH DAMAGE.
37*/
38
42
43template< typename T, const int LX, const int CHUNKS >
44__global__ void convect_scalar_kernel_1d(T * __restrict__ du,
45 const T * __restrict__ u,
46 const T * __restrict__ cr,
47 const T * __restrict__ cs,
48 const T * __restrict__ ct,
49 const T * __restrict__ dx,
50 const T * __restrict__ dy,
51 const T * __restrict__ dz) {
52
53 __shared__ T shu[LX * LX * LX];
54
55 __shared__ T shcr[LX * LX * LX];
56 __shared__ T shcs[LX * LX * LX];
57 __shared__ T shct[LX * LX * LX];
58
59 __shared__ T shdx[LX * LX];
60 __shared__ T shdy[LX * LX];
61 __shared__ T shdz[LX * LX];
62
63 const int e = blockIdx.x;
64 const int iii = threadIdx.x;
65 const int nchunks = (LX * LX * LX - 1) / CHUNKS + 1;
66 const int ele = e*LX*LX*LX;
67
68 if (iii < (LX * LX)) {
69 shdx[iii] = dx[iii];
70 shdy[iii] = dy[iii];
71 shdz[iii] = dz[iii];
72 }
73
74 int l = iii;
75 while(l < (LX * LX * LX)) {
76 shu[l] = u[l + ele];
77
78 shcr[l] = cr[l + ele];
79 shcs[l] = cs[l + ele];
80 shct[l] = ct[l + ele];
81
82 l = l + CHUNKS;
83 }
84
86
87 for (int n = 0; n < nchunks; n++) {
88 const int ijk = iii + n * CHUNKS;
89 const int jk = ijk / LX;
90 const int i = ijk - jk * LX;
91 const int k = jk / LX;
92 const int j = jk - k * LX;
93 if ( i < LX && j < LX && k < LX) {
94 T rtmp = 0.0;
95 T stmp = 0.0;
96 T ttmp = 0.0;
97 for (int l = 0; l < LX; l++) {
98 rtmp += shdx[i + l * LX] * shu[l + j * LX + k * LX * LX];
99 stmp += shdy[j + l * LX] * shu[i + l * LX + k * LX * LX];
100 ttmp += shdz[k + l * LX] * shu[i + j * LX + l * LX * LX];
101 }
102
103 du[ijk + e * LX * LX * LX] = shcr[ijk] * rtmp
104 + shcs[ijk] * stmp
105 + shct[ijk] * ttmp;
106 }
107 }
108}
109
110template< typename T, const int LX, const int EB >
111__global__ void NEKO_EB_BOUNDS(LX*LX*EB)
112 convect_scalar_kernel_kstep(T * __restrict__ du,
113 const T * __restrict__ u,
114 const T * __restrict__ cr,
115 const T * __restrict__ cs,
116 const T * __restrict__ ct,
117 const T * __restrict__ dx,
118 const T * __restrict__ dy,
119 const T * __restrict__ dz,
120 const int nelv) {
121
122 __shared__ T shu[EB * LX * LX];
123
124 __shared__ T shdx[LX*LX];
125 __shared__ T shdy[LX*LX];
126 __shared__ T shdz[LX*LX];
127
128 static_assert(sizeof(shu) +
129 sizeof(shdx) +
130 sizeof(shdy) +
131 sizeof(shdz)
133 "kstep block exceeds the shared memory budget");
134
135 const int eb = (EB == 1) ? 0 : threadIdx.z;
136 const int e_blk = blockIdx.x * EB + eb;
137 /* Threads past the last element still have to reach the barriers in
138 the k loop, so clamp their reads and drop their stores rather than
139 returning early. At EB == 1 this all constant folds away */
140 const bool active = (EB == 1) ? true : (e_blk < nelv);
141 const int e = active ? e_blk : (nelv - 1);
142 const int sh = eb * LX * LX;
143 const int j = threadIdx.y;
144 const int i = threadIdx.x;
145 const int ij = i + j * LX;
146 const int ele = e*LX*LX*LX;
147
148 if (eb == 0) {
149 shdx[ij] = dx[ij];
150 shdy[ij] = dy[ij];
151 shdz[ij] = dz[ij];
152 }
153
154 T ru[LX];
155 T rcr[LX];
156 T rcs[LX];
157 T rct[LX];
158
159#pragma unroll LX
160 for (int k = 0; k < LX; ++k) {
161 ru[k] = u[ij + k*LX*LX + ele];
162 rcr[k] = cr[ij + k*LX*LX + ele];
163 rcs[k] = cs[ij + k*LX*LX + ele];
164 rct[k] = ct[ij + k*LX*LX + ele];
165 }
166
168
169#pragma unroll
170 for (int k = 0; k < LX; ++k) {
171 const int ijk = ij + k*LX*LX;
172 T ttmp = 0.0;
173 shu[sh + ij] = ru[k];
174#pragma unroll
175 for (int l = 0; l < LX; l++) {
176 ttmp += shdz[k+l*LX] * ru[l];
177 }
179
180 T rtmp = 0.0;
181 T stmp = 0.0;
182#pragma unroll
183 for (int l = 0; l < LX; l++) {
184 rtmp += shdx[i+l*LX] * shu[sh + l+j*LX];
185 stmp += shdy[j+l*LX] * shu[sh + i+l*LX];
186 }
187
188 if (active) {
189 du[ijk + ele] = rcr[k] * rtmp
190 + rcs[k] * stmp
191 + rct[k] * ttmp;
192 }
194 }
195}
196
197
198#endif // __MATH_CONVECT_SCALAR_KERNEL_H__
const bool active
__shared__ T shdy[LX *LX]
__shared__ T shu[EB *LX *LX]
const int sh
const int eb
__shared__ T shdz[LX *LX]
__global__ void const T *__restrict__ const T *__restrict__ const T *__restrict__ const T *__restrict__ const T *__restrict__ const T *__restrict__ const T *__restrict__ const T *__restrict__ const T *__restrict__ const T *__restrict__ const T *__restrict__ const int *__restrict__ const int nelv
const int i
const int ij
__global__ void const T *__restrict__ const T *__restrict__ const T *__restrict__ const T *__restrict__ dz
__global__ void const T *__restrict__ const T *__restrict__ dx
__global__ void const T *__restrict__ u
const int e
__global__ void const T *__restrict__ const T *__restrict__ const T *__restrict__ dy
const int e_blk
T ru[LX]
const int ele
const int j
shdx[ij]
T rcr[LX]
__global__ void const T *__restrict__ const T *__restrict__ cr
__global__ void const T *__restrict__ const T *__restrict__ const T *__restrict__ cs
T rct[LX]
__global__ void convect_scalar_kernel_1d(T *__restrict__ du, const T *__restrict__ u, const T *__restrict__ cr, const T *__restrict__ cs, const T *__restrict__ ct, const T *__restrict__ dx, const T *__restrict__ dy, const T *__restrict__ dz)
T rcs[LX]
__global__ void const T *__restrict__ const T *__restrict__ const T *__restrict__ const T *__restrict__ ct
__syncthreads()
#define NEKO_EB_MAX_SMEM
Definition elem_block.h:60
#define NEKO_EB_BOUNDS(NT)
Definition elem_block.h:95