xref: /libCEED/rust/libceed-sys/c-src/include/ceed/jit-source/magma/magma-basis-interp-3d.h (revision 3c1e2aff6d111c93ca8797996aaf987f66b08927)
1f80f4a74SSebastian Grimberg // Copyright (c) 2017-2022, Lawrence Livermore National Security, LLC and other CEED contributors.
2f80f4a74SSebastian Grimberg // All Rights Reserved. See the top-level LICENSE and NOTICE files for details.
3f80f4a74SSebastian Grimberg //
4f80f4a74SSebastian Grimberg // SPDX-License-Identifier: BSD-2-Clause
5f80f4a74SSebastian Grimberg //
6f80f4a74SSebastian Grimberg // This file is part of CEED:  http://github.com/ceed
7f80f4a74SSebastian Grimberg 
8*3c1e2affSSebastian Grimberg /// @file
9*3c1e2affSSebastian Grimberg /// Internal header for MAGMA tensor basis interpolation in 3D
10*3c1e2affSSebastian Grimberg #ifndef CEED_MAGMA_BASIS_INTERP_3D_H
11*3c1e2affSSebastian Grimberg #define CEED_MAGMA_BASIS_INTERP_3D_H
12*3c1e2affSSebastian Grimberg 
13*3c1e2affSSebastian Grimberg #include "magma-common-tensor.h"
14*3c1e2affSSebastian Grimberg 
15f80f4a74SSebastian Grimberg // macros to abstract access of shared memory and reg. file
16*3c1e2affSSebastian Grimberg #define sT(i, j) sT[(j)*P + (i)]
17f80f4a74SSebastian Grimberg #define sTmp(i, j, ldw) sTmp[(j) * (ldw) + (i)]
18f80f4a74SSebastian Grimberg 
19f80f4a74SSebastian Grimberg //////////////////////////////////////////////////////////////////////////////////////////
20f80f4a74SSebastian Grimberg // interp basis action (3D)
21*3c1e2affSSebastian Grimberg template <typename T, int DIM_U, int DIM_V, int NUM_COMP, int P, int Q, int rU_SIZE, int rV_SIZE>
22*3c1e2affSSebastian Grimberg static __device__ __inline__ void magma_interp_3d_device(const T *sT, magma_trans_t transT, T rU[DIM_U][NUM_COMP][rU_SIZE],
23*3c1e2affSSebastian Grimberg                                                          T rV[DIM_V][NUM_COMP][rV_SIZE], const int tx, T rTmp[Q], T *swork) {
24f80f4a74SSebastian Grimberg   // Assumptions
25*3c1e2affSSebastian Grimberg   // 1. 1D threads of size max(P,Q)^2
26*3c1e2affSSebastian Grimberg   // 2. input:  rU[DIM_U x NUM_COMP x rU_SIZE] in registers (per thread)
27*3c1e2affSSebastian Grimberg   // 3. output: rV[DIM_V x NUM_COMP x rV_SIZE] in registers (per thread)
28f80f4a74SSebastian Grimberg   // 4. Three products per component
29*3c1e2affSSebastian Grimberg   //  4.1 Batch P^2 of (1xP) matrices times (PxQ) matrix => Batch P^2 of (1xQ) matrices
30*3c1e2affSSebastian Grimberg   //  4.2 Batch P   of (QxP) matrices times (PxQ) matrix => Batch P   of (QxQ) matrices
31*3c1e2affSSebastian Grimberg   //  4.3 Batch 1   of (Q^2xP_) matrix times (PxQ) matrix => (Q^2xQ_) matrix
32f80f4a74SSebastian Grimberg   // 5. Each thread computes one row of the output of each product
33f80f4a74SSebastian Grimberg   // 6. Sync is recommended before and after the call
34f80f4a74SSebastian Grimberg 
35*3c1e2affSSebastian Grimberg   for (int comp = 0; comp < NUM_COMP; comp++) {
36*3c1e2affSSebastian Grimberg     // Batch P^2 of (1xP) matrices [reg] times (PxQ) matrix [shmem] => Batch P^2 of (1xQ) matrices [shmem]
37*3c1e2affSSebastian Grimberg     if (tx < (P * P)) {
38f80f4a74SSebastian Grimberg       const int batchid = tx;
39f80f4a74SSebastian Grimberg       const int sld     = 1;
40*3c1e2affSSebastian Grimberg       T        *sTmp    = swork + batchid * (1 * Q);
41*3c1e2affSSebastian Grimberg       for (int j = 0; j < Q; j++) {
42f80f4a74SSebastian Grimberg         rTmp[0] = 0.0;
43*3c1e2affSSebastian Grimberg         for (int i = 0; i < P; i++) {
44*3c1e2affSSebastian Grimberg           rTmp[0] += rU[0][comp][i] * sT(i, j);
45f80f4a74SSebastian Grimberg         }
46f80f4a74SSebastian Grimberg         sTmp(0, j, sld) = rTmp[0];
47f80f4a74SSebastian Grimberg       }
48*3c1e2affSSebastian Grimberg     }  // end of: if (tx < P*P)
49f80f4a74SSebastian Grimberg     __syncthreads();
50f80f4a74SSebastian Grimberg 
51*3c1e2affSSebastian Grimberg     // Batch P of (QxP) matrices [shmem] times (PxQ) matrix [shmem] => Batch P of (QxQ) matrices [reg]
52*3c1e2affSSebastian Grimberg     if (tx < (P * Q)) {
53*3c1e2affSSebastian Grimberg       const int batchid = tx / Q;
54*3c1e2affSSebastian Grimberg       const int tx_     = tx % Q;
55*3c1e2affSSebastian Grimberg       const int sld     = Q;
56*3c1e2affSSebastian Grimberg       T        *sTmp    = swork + batchid * (Q * P);  // sTmp is input
57*3c1e2affSSebastian Grimberg       for (int j = 0; j < Q; j++) {
58f80f4a74SSebastian Grimberg         rTmp[j] = 0.0;
59*3c1e2affSSebastian Grimberg         for (int i = 0; i < P; i++) {
60f80f4a74SSebastian Grimberg           rTmp[j] += sTmp(tx_, i, sld) * sT(i, j);
61f80f4a74SSebastian Grimberg         }
62f80f4a74SSebastian Grimberg       }
63f80f4a74SSebastian Grimberg     }
64f80f4a74SSebastian Grimberg     __syncthreads();
65f80f4a74SSebastian Grimberg 
66*3c1e2affSSebastian Grimberg     // write rTmp[] into shmem as batch P of QxQ matrices
67*3c1e2affSSebastian Grimberg     if (tx < (P * Q)) {
68*3c1e2affSSebastian Grimberg       const int batchid = tx / Q;
69*3c1e2affSSebastian Grimberg       const int tx_     = tx % Q;
70*3c1e2affSSebastian Grimberg       const int sld     = Q;
71*3c1e2affSSebastian Grimberg       T        *sTmp    = swork + batchid * (Q * Q);
72*3c1e2affSSebastian Grimberg       for (int j = 0; j < Q; j++) {
73f80f4a74SSebastian Grimberg         sTmp(tx_, j, sld) = rTmp[j];
74f80f4a74SSebastian Grimberg       }
75f80f4a74SSebastian Grimberg     }
76f80f4a74SSebastian Grimberg     __syncthreads();
77f80f4a74SSebastian Grimberg 
78*3c1e2affSSebastian Grimberg     // Batch 1 of (Q^2xP_) matrices [shmem] times (PxQ) matrix [shmem] => Batch 1 of (Q^2xQ_) matrices [reg]
79*3c1e2affSSebastian Grimberg     if (tx < (Q * Q)) {
80*3c1e2affSSebastian Grimberg       // No need to declare batchid = (tx  / Q^2) = always zero
81*3c1e2affSSebastian Grimberg       // No need to declare tx_     = (tx_ % Q^2) = always tx
82*3c1e2affSSebastian Grimberg       const int sld  = Q * Q;
83f80f4a74SSebastian Grimberg       T        *sTmp = swork;
84*3c1e2affSSebastian Grimberg       for (int j = 0; j < Q; j++) {
85f80f4a74SSebastian Grimberg         rTmp[0] = 0.0;
86*3c1e2affSSebastian Grimberg         for (int i = 0; i < P; i++) {
87f80f4a74SSebastian Grimberg           rTmp[0] += sTmp(tx, i, sld) * sT(i, j);
88f80f4a74SSebastian Grimberg         }
89*3c1e2affSSebastian Grimberg         rV[0][comp][j] += rTmp[0];
90f80f4a74SSebastian Grimberg       }
91f80f4a74SSebastian Grimberg     }
92f80f4a74SSebastian Grimberg     __syncthreads();
93f80f4a74SSebastian Grimberg   }
94f80f4a74SSebastian Grimberg }
95f80f4a74SSebastian Grimberg 
96f80f4a74SSebastian Grimberg //////////////////////////////////////////////////////////////////////////////////////////
97*3c1e2affSSebastian Grimberg extern "C" __launch_bounds__(MAGMA_BASIS_BOUNDS(BASIS_MAX_P_Q *BASIS_MAX_P_Q, MAGMA_MAXTHREADS_3D)) __global__
98f80f4a74SSebastian Grimberg     void magma_interpn_3d_kernel(const CeedScalar *dT, const CeedScalar *dU, const int estrdU, const int cstrdU, CeedScalar *dV, const int estrdV,
99f80f4a74SSebastian Grimberg                                  const int cstrdV, const int nelem) {
100f80f4a74SSebastian Grimberg   MAGMA_DEVICE_SHARED(CeedScalar, shared_data)
101f80f4a74SSebastian Grimberg 
102f80f4a74SSebastian Grimberg   const int     tx      = threadIdx.x;
103f80f4a74SSebastian Grimberg   const int     ty      = threadIdx.y;
104f80f4a74SSebastian Grimberg   const int     elem_id = (blockIdx.x * blockDim.y) + ty;
105f80f4a74SSebastian Grimberg   magma_trans_t transT  = MagmaNoTrans;
106f80f4a74SSebastian Grimberg 
107f80f4a74SSebastian Grimberg   if (elem_id >= nelem) return;
108f80f4a74SSebastian Grimberg 
109*3c1e2affSSebastian Grimberg   CeedScalar rU[1][BASIS_NUM_COMP][BASIS_P] = {0.0};  // for a non-fused operator BASIS_DIM is always 1
110*3c1e2affSSebastian Grimberg   CeedScalar rV[1][BASIS_NUM_COMP][BASIS_Q] = {0.0};  // for a non-fused operator BASIS_DIM is always 1
111*3c1e2affSSebastian Grimberg   CeedScalar rTmp[BASIS_Q]                  = {0.0};
112f80f4a74SSebastian Grimberg 
113f80f4a74SSebastian Grimberg   // shift global memory pointers by elem stride
114f80f4a74SSebastian Grimberg   dU += elem_id * estrdU;
115f80f4a74SSebastian Grimberg   dV += elem_id * estrdV;
116f80f4a74SSebastian Grimberg 
117f80f4a74SSebastian Grimberg   // assign shared memory pointers
118*3c1e2affSSebastian Grimberg   CeedScalar *sT   = (CeedScalar *)shared_data;
119*3c1e2affSSebastian Grimberg   CeedScalar *sTmp = sT + BASIS_P * BASIS_Q;
120*3c1e2affSSebastian Grimberg   sTmp += ty * (max(BASIS_P * BASIS_P * BASIS_MAX_P_Q, BASIS_P * BASIS_Q * BASIS_Q));
121f80f4a74SSebastian Grimberg 
122f80f4a74SSebastian Grimberg   // read T
123f80f4a74SSebastian Grimberg   if (ty == 0) {
124*3c1e2affSSebastian Grimberg     dread_T_gm2sm<BASIS_P, BASIS_Q>(tx, transT, dT, sT);
125f80f4a74SSebastian Grimberg   }
126f80f4a74SSebastian Grimberg 
127*3c1e2affSSebastian Grimberg   // read U (idim = 0 for dU, i_DIM = 0 for rU, u_dimstride is always 0)
128*3c1e2affSSebastian Grimberg   readU_3d<CeedScalar, BASIS_P, 1, BASIS_NUM_COMP, BASIS_P, 0>(dU, cstrdU, rU, sTmp, tx);
129f80f4a74SSebastian Grimberg   // there is a sync at the end of this function
130f80f4a74SSebastian Grimberg 
131*3c1e2affSSebastian Grimberg   magma_interp_3d_device<CeedScalar, 1, 1, BASIS_NUM_COMP, BASIS_P, BASIS_Q, BASIS_P, BASIS_Q>(sT, transT, rU, rV, tx, rTmp, sTmp);
132f80f4a74SSebastian Grimberg   __syncthreads();
133f80f4a74SSebastian Grimberg 
134f80f4a74SSebastian Grimberg   // write V
135*3c1e2affSSebastian Grimberg   writeV_3d<CeedScalar, BASIS_Q, 1, BASIS_NUM_COMP, BASIS_Q, 0>(dV, cstrdV, rV, tx);
136f80f4a74SSebastian Grimberg }
137f80f4a74SSebastian Grimberg 
138f80f4a74SSebastian Grimberg //////////////////////////////////////////////////////////////////////////////////////////
139*3c1e2affSSebastian Grimberg extern "C" __launch_bounds__(MAGMA_BASIS_BOUNDS(BASIS_MAX_P_Q *BASIS_MAX_P_Q, MAGMA_MAXTHREADS_3D)) __global__
140f80f4a74SSebastian Grimberg     void magma_interpt_3d_kernel(const CeedScalar *dT, const CeedScalar *dU, const int estrdU, const int cstrdU, CeedScalar *dV, const int estrdV,
141f80f4a74SSebastian Grimberg                                  const int cstrdV, const int nelem) {
142f80f4a74SSebastian Grimberg   MAGMA_DEVICE_SHARED(CeedScalar, shared_data)
143f80f4a74SSebastian Grimberg 
144f80f4a74SSebastian Grimberg   const int     tx      = threadIdx.x;
145f80f4a74SSebastian Grimberg   const int     ty      = threadIdx.y;
146f80f4a74SSebastian Grimberg   const int     elem_id = (blockIdx.x * blockDim.y) + ty;
147f80f4a74SSebastian Grimberg   magma_trans_t transT  = MagmaTrans;
148f80f4a74SSebastian Grimberg 
149f80f4a74SSebastian Grimberg   if (elem_id >= nelem) return;
150f80f4a74SSebastian Grimberg 
151*3c1e2affSSebastian Grimberg   CeedScalar rU[1][BASIS_NUM_COMP][BASIS_Q] = {0.0};  // for a non-fused operator BASIS_DIM is always 1
152*3c1e2affSSebastian Grimberg   CeedScalar rV[1][BASIS_NUM_COMP][BASIS_P] = {0.0};  // for a non-fused operator BASIS_DIM is always 1
153*3c1e2affSSebastian Grimberg   CeedScalar rTmp[BASIS_P]                  = {0.0};
154f80f4a74SSebastian Grimberg 
155f80f4a74SSebastian Grimberg   // shift global memory pointers by elem stride
156f80f4a74SSebastian Grimberg   dU += elem_id * estrdU;
157f80f4a74SSebastian Grimberg   dV += elem_id * estrdV;
158f80f4a74SSebastian Grimberg 
159f80f4a74SSebastian Grimberg   // assign shared memory pointers
160*3c1e2affSSebastian Grimberg   CeedScalar *sT   = (CeedScalar *)shared_data;
161*3c1e2affSSebastian Grimberg   CeedScalar *sTmp = sT + BASIS_Q * BASIS_P;
162*3c1e2affSSebastian Grimberg   sTmp += ty * (max(BASIS_Q * BASIS_Q * BASIS_MAX_P_Q, BASIS_Q * BASIS_P * BASIS_P));
163f80f4a74SSebastian Grimberg 
164f80f4a74SSebastian Grimberg   // read T
165f80f4a74SSebastian Grimberg   if (ty == 0) {
166*3c1e2affSSebastian Grimberg     dread_T_gm2sm<BASIS_Q, BASIS_P>(tx, transT, dT, sT);
167f80f4a74SSebastian Grimberg   }
168f80f4a74SSebastian Grimberg 
169f80f4a74SSebastian Grimberg   // read V
170*3c1e2affSSebastian Grimberg   readV_3d<CeedScalar, BASIS_P, 1, BASIS_NUM_COMP, BASIS_P, 0>(dV, cstrdV, rV, tx);
171f80f4a74SSebastian Grimberg 
172*3c1e2affSSebastian Grimberg   // read U (idim = 0 for dU, i_DIM = 0 for rU, u_dimstride is always 0)
173*3c1e2affSSebastian Grimberg   readU_3d<CeedScalar, BASIS_Q, 1, BASIS_NUM_COMP, BASIS_Q, 0>(dU, cstrdU, rU, sTmp, tx);
174f80f4a74SSebastian Grimberg   // there is a sync at the end of this function
175f80f4a74SSebastian Grimberg 
176*3c1e2affSSebastian Grimberg   magma_interp_3d_device<CeedScalar, 1, 1, BASIS_NUM_COMP, BASIS_Q, BASIS_P, BASIS_Q, BASIS_P>(sT, transT, rU, rV, tx, rTmp, sTmp);
177f80f4a74SSebastian Grimberg   __syncthreads();
178f80f4a74SSebastian Grimberg 
179f80f4a74SSebastian Grimberg   // write V
180*3c1e2affSSebastian Grimberg   writeV_3d<CeedScalar, BASIS_P, 1, BASIS_NUM_COMP, BASIS_P, 0>(dV, cstrdV, rV, tx);
181f80f4a74SSebastian Grimberg }
182*3c1e2affSSebastian Grimberg 
183*3c1e2affSSebastian Grimberg #endif  // CEED_MAGMA_BASIS_INTERP_3D_H
184