Neko 1.99.9
A portable framework for high-order spectral element flow simulations
Loading...
Searching...
No Matches
tensor_kernel.h
Go to the documentation of this file.
1#ifndef __MATH_TENSOR_KERNEL_H__
2#define __MATH_TENSOR_KERNEL_H__
3/*
4 Copyright (c) 2021-2022, The Neko Authors
5 All rights reserved.
6
7 Redistribution and use in source and binary forms, with or without
8 modification, are permitted provided that the following conditions
9 are met:
10
11 * Redistributions of source code must retain the above copyright
12 notice, this list of conditions and the following disclaimer.
13
14 * Redistributions in binary form must reproduce the above
15 copyright notice, this list of conditions and the following
16 disclaimer in the documentation and/or other materials provided
17 with the distribution.
18
19 * Neither the name of the authors nor the names of its
20 contributors may be used to endorse or promote products derived
21 from this software without specific prior written permission.
22
23 THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
24 "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
25 LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
26 FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
27 COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
28 INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
29 BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
30 LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
31 CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
32 LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
33 ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
34 POSSIBILITY OF SUCH DAMAGE.
35*/
36template< typename T, const int N >
38 const int nv,
39 const T * __restrict__ u,
40 const int nu,
41 const T * __restrict__ A,
42 const T * __restrict__ Bt,
43 const T * __restrict__ Ct,
44 const int * elements,
45 const int n_points) {
48
49 const int idx = threadIdx.x;
50 const int str = blockDim.x;
51 const int pt = blockIdx.x;
52 const int e = elements[pt];
53
54 /* Stage the element into shared memory before the first contraction.
55 Read straight from global, phase 1 fetches every value of u nv times --
56 once per output row i -- and fetches them badly: with ii decomposing as
57 i + j*nv, consecutive threads vary i and share j, so a warp asks for
58 values nu apart and pulls a whole sector for each one. Staged, the global
59 read is a single contiguous pass and the redundancy is served from shared
60 memory. shwork2 is unused until phase 2 writes it, by which point u is
61 dead, so this costs no extra shared memory. */
62 for (int p = idx; p < nu*nu*nu; p += str)
63 shwork2[p] = u[p + e*nu*nu*nu];
64
66
67 for (int ii = idx; ii< nu*nu*nv; ii += str) {
68 T tmp = 0.0;
69 int j = ii/nv;
70 int i = ii - j*nv;
71 for( int l = 0; l < nu; l++){
72 tmp += A[i+l*nv+pt*nv*nu]*shwork2[l+nu*j];
73 }
74 shwork[ii] = tmp;
75 }
76
78
79 for (int ijk = idx; ijk< nu*nv*nv; ijk += str) {
80 const int jk = ijk / nv;
81 const int i = ijk - jk * nv;
82 const int k = jk / nv;
83 const int j = jk - k * nv;
84 T tmp = 0.0;
85 const int ik2 = i + k*nv*nu;
86 for( int l = 0; l < nu; l++){
87 tmp += Bt[l+j*nu+pt*nv*nu]*shwork[l*nv+ik2];
88 }
89 shwork2[ijk] = tmp;
90 }
91
93
94 for (int ijk = idx; ijk< nv*nv*nv; ijk += str) {
95 const int jk = ijk / nv;
96 const int i = ijk - jk * nv;
97 const int k = jk / nv;
98 const int j = jk - k * nv;
99 T tmp = 0.0;
100 const int ij2 = i + j*nv;
101 for( int l = 0; l < nu; l++){
102 tmp += Ct[l+k*nu+pt*nv*nu]*shwork2[ij2 + l*nv*nv];
103 }
104 v[ijk+pt*nv*nv*nv] = tmp;
105 }
106
107}
108
109
110
111template< typename T, const int N >
113 const int nv,
114 const T * __restrict__ u,
115 const int nu,
116 const T * __restrict__ A,
117 const T * __restrict__ Bt,
118 const T * __restrict__ Ct) {
121
122 const int idx = threadIdx.x;
123 const int str = blockDim.x;
124 const int e = blockIdx.x;
125
126 /* Stage the element into shared memory before the first contraction.
127 Read straight from global, phase 1 fetches every value of u nv times --
128 once per output row i -- and fetches them badly: with ii decomposing as
129 i + j*nv, consecutive threads vary i and share j, so a warp asks for
130 values nu apart and pulls a whole sector for each one. Staged, the global
131 read is a single contiguous pass and the redundancy is served from shared
132 memory. shwork2 is unused until phase 2 writes it, by which point u is
133 dead, so this costs no extra shared memory. */
134 for (int p = idx; p < nu*nu*nu; p += str)
135 shwork2[p] = u[p + e*nu*nu*nu];
136
138
139 for (int ii = idx; ii< nu*nu*nv; ii += str) {
140 T tmp = 0.0;
141 int j = ii/nv;
142 int i = ii - j*nv;
143 for( int l = 0; l < nu; l++){
144 tmp += A[i+l*nv]*shwork2[l+nu*j];
145 }
146 shwork[ii] = tmp;
147 }
148
150
151 for (int ijk = idx; ijk< nu*nv*nv; ijk += str) {
152 const int jk = ijk / nv;
153 const int i = ijk - jk * nv;
154 const int k = jk / nv;
155 const int j = jk - k * nv;
156 T tmp = 0.0;
157 const int ik2 = i + k*nv*nu;
158 for( int l = 0; l < nu; l++){
159 tmp += Bt[l+j*nu]*shwork[l*nv+ik2];
160 }
161 shwork2[ijk] = tmp;
162 }
163
165
166 for (int ijk = idx; ijk< nv*nv*nv; ijk += str) {
167 const int jk = ijk / nv;
168 const int i = ijk - jk * nv;
169 const int k = jk / nv;
170 const int j = jk - k * nv;
171 T tmp = 0.0;
172 const int ij2 = i + j*nv;
173 for( int l = 0; l < nu; l++){
174 tmp += Ct[l+k*nu]*shwork2[ij2 + l*nv*nv];
175 }
176 v[ijk+e*nv*nv*nv] = tmp;
177 }
178}
179
180template< typename T, const int N >
182 const int nv,
183 const T * __restrict__ u,
184 const int nu,
185 const T * __restrict__ A,
186 const T * __restrict__ Bt,
187 const T * __restrict__ Ct) {
188 extern __shared__ T shmem[];
189 T *shwork = shmem;
190 T *shwork2 = shmem + N*N*N;
191
192 const int idx = threadIdx.x;
193 const int str = blockDim.x;
194 const int e = blockIdx.x;
195
196 /* Stage the element into shared memory before the first contraction.
197 Read straight from global, phase 1 fetches every value of u nv times --
198 once per output row i -- and fetches them badly: with ii decomposing as
199 i + j*nv, consecutive threads vary i and share j, so a warp asks for
200 values nu apart and pulls a whole sector for each one. Staged, the global
201 read is a single contiguous pass and the redundancy is served from shared
202 memory. shwork2 is unused until phase 2 writes it, by which point u is
203 dead, so this costs no extra shared memory. */
204 for (int p = idx; p < nu*nu*nu; p += str)
205 shwork2[p] = u[p + e*nu*nu*nu];
206
208
209 for (int ii = idx; ii< nu*nu*nv; ii += str) {
210 T tmp = 0.0;
211 int j = ii/nv;
212 int i = ii - j*nv;
213 for( int l = 0; l < nu; l++){
214 tmp += A[i+l*nv]*shwork2[l+nu*j];
215 }
216 shwork[ii] = tmp;
217 }
218
220
221 for (int ijk = idx; ijk< nu*nv*nv; ijk += str) {
222 const int jk = ijk / nv;
223 const int i = ijk - jk * nv;
224 const int k = jk / nv;
225 const int j = jk - k * nv;
226 T tmp = 0.0;
227 const int ik2 = i + k*nv*nu;
228 for( int l = 0; l < nu; l++){
229 tmp += Bt[l+j*nu]*shwork[l*nv+ik2];
230 }
231 shwork2[ijk] = tmp;
232 }
233
235
236 for (int ijk = idx; ijk< nv*nv*nv; ijk += str) {
237 const int jk = ijk / nv;
238 const int i = ijk - jk * nv;
239 const int k = jk / nv;
240 const int j = jk - k * nv;
241 T tmp = 0.0;
242 const int ij2 = i + j*nv;
243 for( int l = 0; l < nu; l++){
244 tmp += Ct[l+k*nu]*shwork2[ij2 + l*nv*nv];
245 }
246 v[ijk+e*nv*nv*nv] = tmp;
247 }
248}
249
250
251
252#endif // __MATH_TENSOR_KERNEL_H__
__global__ void ale_add_kinematics_kernel(const int n, T *__restrict__ wx, T *__restrict__ wy, T *__restrict__ wz, const T *__restrict__ x_ref, const T *__restrict__ y_ref, const T *__restrict__ z_ref, const T *__restrict__ phi, const T *__restrict__ x, const T *__restrict__ y, const T *__restrict__ z, const kinematics_params_t kin_params)
const int i
__global__ void T *__restrict__ T *__restrict__ const T *__restrict__ u
const int e
__global__ void T *__restrict__ T *__restrict__ const T *__restrict__ const T *__restrict__ v
const int j
__syncthreads()
__global__ void tnsr3d_el_kernel(T *__restrict__ v, const int nv, const T *__restrict__ u, const int nu, const T *__restrict__ A, const T *__restrict__ Bt, const T *__restrict__ Ct, const int *elements, const int n_points)
__global__ void tnsr3d_kernel(T *__restrict__ v, const int nv, const T *__restrict__ u, const int nu, const T *__restrict__ A, const T *__restrict__ Bt, const T *__restrict__ Ct)
__global__ void tnsr3d_kernel_large(T *__restrict__ v, const int nv, const T *__restrict__ u, const int nu, const T *__restrict__ A, const T *__restrict__ Bt, const T *__restrict__ Ct)
Fortran bindings to SHMEM's C API.
Definition shmem.F90:34