xref: /libCEED/include/ceed/jit-source/cuda/cuda-ref-basis-tensor.h (revision db2becc9f302fe8eb3a32ace50ce3f3a5d42e6c4)
15aed82e4SJeremy L Thompson // Copyright (c) 2017-2024, Lawrence Livermore National Security, LLC and other CEED contributors.
2a0154adeSJed Brown // All Rights Reserved. See the top-level LICENSE and NOTICE files for details.
3a0154adeSJed Brown //
4a0154adeSJed Brown // SPDX-License-Identifier: BSD-2-Clause
5a0154adeSJed Brown //
6a0154adeSJed Brown // This file is part of CEED:  http://github.com/ceed
7a0154adeSJed Brown 
8b2165e7aSSebastian Grimberg /// @file
9b2165e7aSSebastian Grimberg /// Internal header for CUDA tensor product basis
10b2165e7aSSebastian Grimberg 
11c9c2c079SJeremy L Thompson #include <ceed.h>
12a0154adeSJed Brown 
13a0154adeSJed Brown //------------------------------------------------------------------------------
14a0154adeSJed Brown // Tensor Basis Kernels
15a0154adeSJed Brown //------------------------------------------------------------------------------
16a0154adeSJed Brown 
17a0154adeSJed Brown //------------------------------------------------------------------------------
18a0154adeSJed Brown // Interp
19a0154adeSJed Brown //------------------------------------------------------------------------------
200b2e4913SJeremy L Thompson extern "C" __global__ void Interp(const CeedInt num_elem, const CeedInt is_transpose, const CeedScalar *__restrict__ interp_1d,
212b730f8bSJeremy L Thompson                                   const CeedScalar *__restrict__ u, CeedScalar *__restrict__ v) {
22a0154adeSJed Brown   const CeedInt i = threadIdx.x;
23a0154adeSJed Brown 
24a0154adeSJed Brown   __shared__ CeedScalar s_mem[BASIS_Q_1D * BASIS_P_1D + 2 * BASIS_BUF_LEN];
25a0154adeSJed Brown   CeedScalar           *s_interp_1d = s_mem;
26a0154adeSJed Brown   CeedScalar           *s_buffer_1  = s_mem + BASIS_Q_1D * BASIS_P_1D;
27a0154adeSJed Brown   CeedScalar           *s_buffer_2  = s_buffer_1 + BASIS_BUF_LEN;
28a0154adeSJed Brown   for (CeedInt k = i; k < BASIS_Q_1D * BASIS_P_1D; k += blockDim.x) {
29a0154adeSJed Brown     s_interp_1d[k] = interp_1d[k];
30a0154adeSJed Brown   }
31a0154adeSJed Brown 
320b2e4913SJeremy L Thompson   const CeedInt P             = is_transpose ? BASIS_Q_1D : BASIS_P_1D;
330b2e4913SJeremy L Thompson   const CeedInt Q             = is_transpose ? BASIS_P_1D : BASIS_Q_1D;
340b2e4913SJeremy L Thompson   const CeedInt stride_0      = is_transpose ? 1 : BASIS_P_1D;
350b2e4913SJeremy L Thompson   const CeedInt stride_1      = is_transpose ? BASIS_P_1D : 1;
360b2e4913SJeremy L Thompson   const CeedInt u_stride      = is_transpose ? BASIS_NUM_QPTS : BASIS_NUM_NODES;
370b2e4913SJeremy L Thompson   const CeedInt v_stride      = is_transpose ? BASIS_NUM_NODES : BASIS_NUM_QPTS;
380b2e4913SJeremy L Thompson   const CeedInt u_comp_stride = num_elem * (is_transpose ? BASIS_NUM_QPTS : BASIS_NUM_NODES);
390b2e4913SJeremy L Thompson   const CeedInt v_comp_stride = num_elem * (is_transpose ? BASIS_NUM_NODES : BASIS_NUM_QPTS);
400b2e4913SJeremy L Thompson   const CeedInt u_size        = is_transpose ? BASIS_NUM_QPTS : BASIS_NUM_NODES;
41a0154adeSJed Brown 
42a0154adeSJed Brown   // Apply basis element by element
43a0154adeSJed Brown   for (CeedInt elem = blockIdx.x; elem < num_elem; elem += gridDim.x) {
44a0154adeSJed Brown     for (CeedInt comp = 0; comp < BASIS_NUM_COMP; comp++) {
45*db2becc9SJeremy L Thompson       const CeedScalar *cur_u = &u[elem * u_stride + comp * u_comp_stride];
46*db2becc9SJeremy L Thompson       CeedScalar       *cur_v = &v[elem * v_stride + comp * v_comp_stride];
47672b0f2aSSebastian Grimberg       CeedInt           pre   = u_size;
48672b0f2aSSebastian Grimberg       CeedInt           post  = 1;
49ca735530SJeremy L Thompson 
50a0154adeSJed Brown       for (CeedInt d = 0; d < BASIS_DIM; d++) {
51a0154adeSJed Brown         __syncthreads();
522a86cc9dSSebastian Grimberg         // Update buffers used
53a0154adeSJed Brown         pre /= P;
542d903c70SJeremy L Thompson         const CeedScalar *in       = d == 0 ? cur_u : (d % 2 ? s_buffer_2 : s_buffer_1);
55a0154adeSJed Brown         CeedScalar       *out      = d == BASIS_DIM - 1 ? cur_v : (d % 2 ? s_buffer_1 : s_buffer_2);
56672b0f2aSSebastian Grimberg         const CeedInt     writeLen = pre * post * Q;
57a0154adeSJed Brown 
58a0154adeSJed Brown         // Contract along middle index
59a0154adeSJed Brown         for (CeedInt k = i; k < writeLen; k += blockDim.x) {
60a0154adeSJed Brown           const CeedInt c   = k % post;
61a0154adeSJed Brown           const CeedInt j   = (k / post) % Q;
62a0154adeSJed Brown           const CeedInt a   = k / (post * Q);
63*db2becc9SJeremy L Thompson           CeedScalar    v_k = 0;
64672b0f2aSSebastian Grimberg 
65*db2becc9SJeremy L Thompson           for (CeedInt b = 0; b < P; b++) v_k += s_interp_1d[j * stride_0 + b * stride_1] * in[(a * P + b) * post + c];
66*db2becc9SJeremy L Thompson           if (is_transpose && d == BASIS_DIM - 1) out[k] += v_k;
67*db2becc9SJeremy L Thompson           else out[k] = v_k;
68a0154adeSJed Brown         }
69a0154adeSJed Brown         post *= Q;
70a0154adeSJed Brown       }
71a0154adeSJed Brown     }
72a0154adeSJed Brown   }
73a0154adeSJed Brown }
74a0154adeSJed Brown 
75a0154adeSJed Brown //------------------------------------------------------------------------------
76a0154adeSJed Brown // Grad
77a0154adeSJed Brown //------------------------------------------------------------------------------
780b2e4913SJeremy L Thompson extern "C" __global__ void Grad(const CeedInt num_elem, const CeedInt is_transpose, const CeedScalar *__restrict__ interp_1d,
792b730f8bSJeremy L Thompson                                 const CeedScalar *__restrict__ grad_1d, const CeedScalar *__restrict__ u, CeedScalar *__restrict__ v) {
80a0154adeSJed Brown   const CeedInt i = threadIdx.x;
81a0154adeSJed Brown 
82a0154adeSJed Brown   __shared__ CeedScalar s_mem[2 * (BASIS_Q_1D * BASIS_P_1D + BASIS_BUF_LEN)];
83a0154adeSJed Brown   CeedScalar           *s_interp_1d = s_mem;
84a0154adeSJed Brown   CeedScalar           *s_grad_1d   = s_interp_1d + BASIS_Q_1D * BASIS_P_1D;
85a0154adeSJed Brown   CeedScalar           *s_buffer_1  = s_grad_1d + BASIS_Q_1D * BASIS_P_1D;
86a0154adeSJed Brown   CeedScalar           *s_buffer_2  = s_buffer_1 + BASIS_BUF_LEN;
87a0154adeSJed Brown   for (CeedInt k = i; k < BASIS_Q_1D * BASIS_P_1D; k += blockDim.x) {
88a0154adeSJed Brown     s_interp_1d[k] = interp_1d[k];
89a0154adeSJed Brown     s_grad_1d[k]   = grad_1d[k];
90a0154adeSJed Brown   }
91a0154adeSJed Brown 
920b2e4913SJeremy L Thompson   const CeedInt P             = is_transpose ? BASIS_Q_1D : BASIS_P_1D;
930b2e4913SJeremy L Thompson   const CeedInt Q             = is_transpose ? BASIS_P_1D : BASIS_Q_1D;
940b2e4913SJeremy L Thompson   const CeedInt stride_0      = is_transpose ? 1 : BASIS_P_1D;
950b2e4913SJeremy L Thompson   const CeedInt stride_1      = is_transpose ? BASIS_P_1D : 1;
960b2e4913SJeremy L Thompson   const CeedInt u_stride      = is_transpose ? BASIS_NUM_QPTS : BASIS_NUM_NODES;
970b2e4913SJeremy L Thompson   const CeedInt v_stride      = is_transpose ? BASIS_NUM_NODES : BASIS_NUM_QPTS;
980b2e4913SJeremy L Thompson   const CeedInt u_comp_stride = num_elem * (is_transpose ? BASIS_NUM_QPTS : BASIS_NUM_NODES);
990b2e4913SJeremy L Thompson   const CeedInt v_comp_stride = num_elem * (is_transpose ? BASIS_NUM_NODES : BASIS_NUM_QPTS);
1000b2e4913SJeremy L Thompson   const CeedInt u_dim_stride  = is_transpose ? num_elem * BASIS_NUM_QPTS * BASIS_NUM_COMP : 0;
1010b2e4913SJeremy L Thompson   const CeedInt v_dim_stride  = is_transpose ? 0 : num_elem * BASIS_NUM_QPTS * BASIS_NUM_COMP;
102a0154adeSJed Brown 
103a0154adeSJed Brown   // Apply basis element by element
104a0154adeSJed Brown   for (CeedInt elem = blockIdx.x; elem < num_elem; elem += gridDim.x) {
105a0154adeSJed Brown     for (CeedInt comp = 0; comp < BASIS_NUM_COMP; comp++) {
106a0154adeSJed Brown       // dim*dim contractions for grad
107a0154adeSJed Brown       for (CeedInt dim_1 = 0; dim_1 < BASIS_DIM; dim_1++) {
1080b2e4913SJeremy L Thompson         CeedInt           pre   = is_transpose ? BASIS_NUM_QPTS : BASIS_NUM_NODES;
109a0154adeSJed Brown         CeedInt           post  = 1;
110*db2becc9SJeremy L Thompson         const CeedScalar *cur_u = &u[elem * u_stride + dim_1 * u_dim_stride + comp * u_comp_stride];
111*db2becc9SJeremy L Thompson         CeedScalar       *cur_v = &v[elem * v_stride + dim_1 * v_dim_stride + comp * v_comp_stride];
112ca735530SJeremy L Thompson 
113a0154adeSJed Brown         for (CeedInt dim_2 = 0; dim_2 < BASIS_DIM; dim_2++) {
114a0154adeSJed Brown           __syncthreads();
115b2165e7aSSebastian Grimberg           // Update buffers used
116a0154adeSJed Brown           pre /= P;
117a0154adeSJed Brown           const CeedScalar *op       = dim_1 == dim_2 ? s_grad_1d : s_interp_1d;
1182b730f8bSJeremy L Thompson           const CeedScalar *in       = dim_2 == 0 ? cur_u : (dim_2 % 2 ? s_buffer_2 : s_buffer_1);
1192b730f8bSJeremy L Thompson           CeedScalar       *out      = dim_2 == BASIS_DIM - 1 ? cur_v : (dim_2 % 2 ? s_buffer_1 : s_buffer_2);
120672b0f2aSSebastian Grimberg           const CeedInt     writeLen = pre * post * Q;
121a0154adeSJed Brown 
122a0154adeSJed Brown           // Contract along middle index
123a0154adeSJed Brown           for (CeedInt k = i; k < writeLen; k += blockDim.x) {
124a0154adeSJed Brown             const CeedInt c   = k % post;
125a0154adeSJed Brown             const CeedInt j   = (k / post) % Q;
126a0154adeSJed Brown             const CeedInt a   = k / (post * Q);
127a0154adeSJed Brown             CeedScalar    v_k = 0;
128ca735530SJeremy L Thompson 
1292b730f8bSJeremy L Thompson             for (CeedInt b = 0; b < P; b++) v_k += op[j * stride_0 + b * stride_1] * in[(a * P + b) * post + c];
1300b2e4913SJeremy L Thompson             if (is_transpose && dim_2 == BASIS_DIM - 1) out[k] += v_k;
1312b730f8bSJeremy L Thompson             else out[k] = v_k;
132a0154adeSJed Brown           }
133a0154adeSJed Brown           post *= Q;
134a0154adeSJed Brown         }
135a0154adeSJed Brown       }
136a0154adeSJed Brown     }
137a0154adeSJed Brown   }
138a0154adeSJed Brown }
139a0154adeSJed Brown 
140a0154adeSJed Brown //------------------------------------------------------------------------------
141a0154adeSJed Brown // 1D quadrature weights
142a0154adeSJed Brown //------------------------------------------------------------------------------
1432b730f8bSJeremy L Thompson __device__ void Weight1d(const CeedInt num_elem, const CeedScalar *q_weight_1d, CeedScalar *w) {
144a0154adeSJed Brown   const CeedInt i = threadIdx.x;
145ca735530SJeremy L Thompson 
146a0154adeSJed Brown   if (i < BASIS_Q_1D) {
147a0154adeSJed Brown     const size_t elem = blockIdx.x;
148ca735530SJeremy L Thompson 
1492b730f8bSJeremy L Thompson     if (elem < num_elem) w[elem * BASIS_Q_1D + i] = q_weight_1d[i];
150a0154adeSJed Brown   }
151a0154adeSJed Brown }
152a0154adeSJed Brown 
153a0154adeSJed Brown //------------------------------------------------------------------------------
154a0154adeSJed Brown // 2D quadrature weights
155a0154adeSJed Brown //------------------------------------------------------------------------------
1562b730f8bSJeremy L Thompson __device__ void Weight2d(const CeedInt num_elem, const CeedScalar *q_weight_1d, CeedScalar *w) {
157a0154adeSJed Brown   const CeedInt i = threadIdx.x;
158a0154adeSJed Brown   const CeedInt j = threadIdx.y;
159ca735530SJeremy L Thompson 
160a0154adeSJed Brown   if (i < BASIS_Q_1D && j < BASIS_Q_1D) {
161a0154adeSJed Brown     const size_t elem = blockIdx.x;
162ca735530SJeremy L Thompson 
163a0154adeSJed Brown     if (elem < num_elem) {
164a0154adeSJed Brown       const size_t ind = (elem * BASIS_Q_1D + j) * BASIS_Q_1D + i;
165ca735530SJeremy L Thompson 
166a0154adeSJed Brown       w[ind] = q_weight_1d[i] * q_weight_1d[j];
167a0154adeSJed Brown     }
168a0154adeSJed Brown   }
169a0154adeSJed Brown }
170a0154adeSJed Brown 
171a0154adeSJed Brown //------------------------------------------------------------------------------
172a0154adeSJed Brown // 3D quadrature weights
173a0154adeSJed Brown //------------------------------------------------------------------------------
1742b730f8bSJeremy L Thompson __device__ void Weight3d(const CeedInt num_elem, const CeedScalar *q_weight_1d, CeedScalar *w) {
175a0154adeSJed Brown   const CeedInt i = threadIdx.x;
176a0154adeSJed Brown   const CeedInt j = threadIdx.y;
177ca735530SJeremy L Thompson 
178a0154adeSJed Brown   if (i < BASIS_Q_1D && j < BASIS_Q_1D) {
179a0154adeSJed Brown     const size_t elem = blockIdx.x;
180ca735530SJeremy L Thompson 
181a0154adeSJed Brown     if (elem < num_elem) {
182a0154adeSJed Brown       for (CeedInt k = 0; k < BASIS_Q_1D; k++) {
183a0154adeSJed Brown         const size_t ind = ((elem * BASIS_Q_1D + k) * BASIS_Q_1D + j) * BASIS_Q_1D + i;
184ca735530SJeremy L Thompson 
185a0154adeSJed Brown         w[ind] = q_weight_1d[i] * q_weight_1d[j] * q_weight_1d[k];
186a0154adeSJed Brown       }
187a0154adeSJed Brown     }
188a0154adeSJed Brown   }
189a0154adeSJed Brown }
190a0154adeSJed Brown 
191a0154adeSJed Brown //------------------------------------------------------------------------------
192a0154adeSJed Brown // Quadrature weights
193a0154adeSJed Brown //------------------------------------------------------------------------------
1942b730f8bSJeremy L Thompson extern "C" __global__ void Weight(const CeedInt num_elem, const CeedScalar *__restrict__ q_weight_1d, CeedScalar *__restrict__ v) {
1952b730f8bSJeremy L Thompson   if (BASIS_DIM == 1) Weight1d(num_elem, q_weight_1d, v);
1962b730f8bSJeremy L Thompson   else if (BASIS_DIM == 2) Weight2d(num_elem, q_weight_1d, v);
1972b730f8bSJeremy L Thompson   else if (BASIS_DIM == 3) Weight3d(num_elem, q_weight_1d, v);
198a0154adeSJed Brown }
199