xref: /libCEED/rust/libceed-sys/c-src/include/ceed/jit-source/magma/magma-basis-interp-2d.h (revision db2becc9f302fe8eb3a32ace50ce3f3a5d42e6c4)
15aed82e4SJeremy L Thompson // Copyright (c) 2017-2024, 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 
83c1e2affSSebastian Grimberg /// @file
93c1e2affSSebastian Grimberg /// Internal header for MAGMA tensor basis interpolation in 1D
103c1e2affSSebastian Grimberg 
113c1e2affSSebastian Grimberg #include "magma-common-tensor.h"
123c1e2affSSebastian Grimberg 
13f80f4a74SSebastian Grimberg // macros to abstract access of shared memory and reg. file
143c1e2affSSebastian Grimberg #define sT(i, j) sT[(j) * P + (i)]
15f80f4a74SSebastian Grimberg #define sTmp(i, j, ldw) sTmp[(j) * (ldw) + (i)]
16f80f4a74SSebastian Grimberg 
179e0c01faSSebastian Grimberg ////////////////////////////////////////////////////////////////////////////////
18f80f4a74SSebastian Grimberg // interp basis action (2D)
193c1e2affSSebastian Grimberg template <typename T, int DIM_U, int DIM_V, int NUM_COMP, int P, int Q, int rU_SIZE, int rV_SIZE>
207132caa0SSebastian Grimberg static __device__ __inline__ void magma_interp_2d_device(const T *sT, T rU[DIM_U][NUM_COMP][rU_SIZE], T rV[DIM_V][NUM_COMP][rV_SIZE], const int tx,
217132caa0SSebastian Grimberg                                                          T rTmp, T *swork) {
22f80f4a74SSebastian Grimberg   // Assumptions
233c1e2affSSebastian Grimberg   // 1. 1D threads of size max(P,Q)
243c1e2affSSebastian Grimberg   // 2. input:  rU[DIM_U x NUM_COMP x rU_SIZE] in registers (per thread)
253c1e2affSSebastian Grimberg   // 3. output: rV[DIM_V x NUM_COMP x rV_SIZE] in registers (per thread)
26f80f4a74SSebastian Grimberg   // 4. Two products per component
273c1e2affSSebastian Grimberg   //  4.1 Batch P of (1xP) matrices times (PxQ) matrix => Batch P of (1xQ) matrices
283c1e2affSSebastian Grimberg   //  4.2 Batch 1 of (QxP) matrix   times (PxQ) matrix => (QxQ) matrix
29f80f4a74SSebastian Grimberg   // 5. Each thread computes one row of the output of each product
30f80f4a74SSebastian Grimberg   // 6. Sync is recommended before and after the call
31f80f4a74SSebastian Grimberg 
323c1e2affSSebastian Grimberg   for (int comp = 0; comp < NUM_COMP; comp++) {
333c1e2affSSebastian Grimberg     // 1st product -- Batch P of (1xP) matrices [reg] x (PxQ) [shmem] => Batch P of (1xQ) matrices
343c1e2affSSebastian Grimberg     // the batch output P x (1xQ) is written on the fly to shmem
353c1e2affSSebastian Grimberg     if (tx < P) {
36f80f4a74SSebastian Grimberg       const int batchid = tx;
37f80f4a74SSebastian Grimberg       const int sld     = 1;
383c1e2affSSebastian Grimberg       T        *sTmp    = swork + batchid * (1 * Q);
393c1e2affSSebastian Grimberg       for (int j = 0; j < Q; j++) {
40f80f4a74SSebastian Grimberg         rTmp = 0.0;
413c1e2affSSebastian Grimberg         for (int i = 0; i < P; i++) {
423c1e2affSSebastian Grimberg           rTmp += rU[0][comp][i] * sT(i, j);
43f80f4a74SSebastian Grimberg         }
44f80f4a74SSebastian Grimberg         sTmp(0, j, sld) = rTmp;
45f80f4a74SSebastian Grimberg       }
463c1e2affSSebastian Grimberg     }  // end of: if (tx < P)
47f80f4a74SSebastian Grimberg     __syncthreads();
48f80f4a74SSebastian Grimberg 
493c1e2affSSebastian Grimberg     // 2nd product -- Batch 1 of a (QxP) matrix [shmem] x (PxQ) [shmem] => (QxQ) matrix [reg]
503c1e2affSSebastian Grimberg     if (tx < Q) {
51f80f4a74SSebastian Grimberg       const int batchid = 0;
523c1e2affSSebastian Grimberg       const int sld     = Q;
533c1e2affSSebastian Grimberg       T        *sTmp    = swork + batchid * (Q * P);
543c1e2affSSebastian Grimberg       for (int j = 0; j < Q; j++) {
55f80f4a74SSebastian Grimberg         rTmp = 0.0;
563c1e2affSSebastian Grimberg         for (int i = 0; i < P; i++) {
57f80f4a74SSebastian Grimberg           rTmp += sTmp(tx, i, sld) * sT(i, j);
58f80f4a74SSebastian Grimberg         }
593c1e2affSSebastian Grimberg         rV[0][comp][j] += rTmp;
60f80f4a74SSebastian Grimberg       }
61f80f4a74SSebastian Grimberg     }
62f80f4a74SSebastian Grimberg     __syncthreads();
63f80f4a74SSebastian Grimberg   }
64f80f4a74SSebastian Grimberg }
65f80f4a74SSebastian Grimberg 
669e0c01faSSebastian Grimberg ////////////////////////////////////////////////////////////////////////////////
673c1e2affSSebastian Grimberg extern "C" __launch_bounds__(MAGMA_BASIS_BOUNDS(BASIS_MAX_P_Q, MAGMA_MAXTHREADS_2D)) __global__
68f80f4a74SSebastian Grimberg     void magma_interpn_2d_kernel(const CeedScalar *dT, const CeedScalar *dU, const int estrdU, const int cstrdU, CeedScalar *dV, const int estrdV,
69f80f4a74SSebastian Grimberg                                  const int cstrdV, const int nelem) {
70f80f4a74SSebastian Grimberg   MAGMA_DEVICE_SHARED(CeedScalar, shared_data)
71f80f4a74SSebastian Grimberg 
72f80f4a74SSebastian Grimberg   const int tx      = threadIdx.x;
73f80f4a74SSebastian Grimberg   const int ty      = threadIdx.y;
74f80f4a74SSebastian Grimberg   const int elem_id = (blockIdx.x * blockDim.y) + ty;
75f80f4a74SSebastian Grimberg 
76f80f4a74SSebastian Grimberg   if (elem_id >= nelem) return;
77f80f4a74SSebastian Grimberg 
783c1e2affSSebastian Grimberg   CeedScalar rU[1][BASIS_NUM_COMP][BASIS_P] = {0.0};  // for a non-fused operator BASIS_DIM is always 1
793c1e2affSSebastian Grimberg   CeedScalar rV[1][BASIS_NUM_COMP][BASIS_Q] = {0.0};  // for a non-fused operator BASIS_DIM is always 1
80f80f4a74SSebastian Grimberg   CeedScalar rTmp                           = 0.0;
81f80f4a74SSebastian Grimberg 
82f80f4a74SSebastian Grimberg   // shift global memory pointers by elem stride
83f80f4a74SSebastian Grimberg   dU += elem_id * estrdU;
84f80f4a74SSebastian Grimberg   dV += elem_id * estrdV;
85f80f4a74SSebastian Grimberg 
86f80f4a74SSebastian Grimberg   // assign shared memory pointers
873c1e2affSSebastian Grimberg   CeedScalar *sT   = (CeedScalar *)shared_data;
883c1e2affSSebastian Grimberg   CeedScalar *sTmp = sT + BASIS_P * BASIS_Q;
893c1e2affSSebastian Grimberg   sTmp += ty * (BASIS_P * BASIS_MAX_P_Q);
90f80f4a74SSebastian Grimberg 
91f80f4a74SSebastian Grimberg   // read T
92f80f4a74SSebastian Grimberg   if (ty == 0) {
939e0c01faSSebastian Grimberg     read_T_notrans_gm2sm<BASIS_P, BASIS_Q>(tx, dT, sT);
94f80f4a74SSebastian Grimberg   }
95f80f4a74SSebastian Grimberg 
96f80f4a74SSebastian Grimberg   // read U -- there is a sync at the end of this function
979e0c01faSSebastian Grimberg   read_U_2d<CeedScalar, BASIS_P, 1, BASIS_NUM_COMP, BASIS_P, 0>(dU, cstrdU, rU, sTmp, tx);
98f80f4a74SSebastian Grimberg 
999e0c01faSSebastian Grimberg   // no sync needed here -- read_U_2d already syncs at the end
1007132caa0SSebastian Grimberg   magma_interp_2d_device<CeedScalar, 1, 1, BASIS_NUM_COMP, BASIS_P, BASIS_Q, BASIS_P, BASIS_Q>(sT, rU, rV, tx, rTmp, sTmp);
101f80f4a74SSebastian Grimberg   __syncthreads();
102f80f4a74SSebastian Grimberg 
103f80f4a74SSebastian Grimberg   // write V
1049e0c01faSSebastian Grimberg   write_V_2d<CeedScalar, BASIS_Q, 1, BASIS_NUM_COMP, BASIS_Q, 0>(dV, cstrdV, rV, tx);
105f80f4a74SSebastian Grimberg }
106f80f4a74SSebastian Grimberg 
1079e0c01faSSebastian Grimberg ////////////////////////////////////////////////////////////////////////////////
1083c1e2affSSebastian Grimberg extern "C" __launch_bounds__(MAGMA_BASIS_BOUNDS(BASIS_MAX_P_Q, MAGMA_MAXTHREADS_2D)) __global__
109f80f4a74SSebastian Grimberg     void magma_interpt_2d_kernel(const CeedScalar *dT, const CeedScalar *dU, const int estrdU, const int cstrdU, CeedScalar *dV, const int estrdV,
110f80f4a74SSebastian Grimberg                                  const int cstrdV, const int nelem) {
111f80f4a74SSebastian Grimberg   MAGMA_DEVICE_SHARED(CeedScalar, shared_data)
112f80f4a74SSebastian Grimberg 
113f80f4a74SSebastian Grimberg   const int tx      = threadIdx.x;
114f80f4a74SSebastian Grimberg   const int ty      = threadIdx.y;
115f80f4a74SSebastian Grimberg   const int elem_id = (blockIdx.x * blockDim.y) + ty;
116f80f4a74SSebastian Grimberg 
117f80f4a74SSebastian Grimberg   if (elem_id >= nelem) return;
118f80f4a74SSebastian Grimberg 
1193c1e2affSSebastian Grimberg   CeedScalar rU[1][BASIS_NUM_COMP][BASIS_Q] = {0.0};  // for a non-fused operator BASIS_DIM is always 1
1203c1e2affSSebastian Grimberg   CeedScalar rV[1][BASIS_NUM_COMP][BASIS_P] = {0.0};  // for a non-fused operator BASIS_DIM is always 1
121f80f4a74SSebastian Grimberg   CeedScalar rTmp                           = 0.0;
122f80f4a74SSebastian Grimberg 
123f80f4a74SSebastian Grimberg   // shift global memory pointers by elem stride
124f80f4a74SSebastian Grimberg   dU += elem_id * estrdU;
125f80f4a74SSebastian Grimberg   dV += elem_id * estrdV;
126f80f4a74SSebastian Grimberg 
127f80f4a74SSebastian Grimberg   // assign shared memory pointers
1283c1e2affSSebastian Grimberg   CeedScalar *sT   = (CeedScalar *)shared_data;
1293c1e2affSSebastian Grimberg   CeedScalar *sTmp = sT + BASIS_Q * BASIS_P;
1303c1e2affSSebastian Grimberg   sTmp += ty * (BASIS_Q * BASIS_MAX_P_Q);
131f80f4a74SSebastian Grimberg 
132f80f4a74SSebastian Grimberg   // read T
133f80f4a74SSebastian Grimberg   if (ty == 0) {
1349e0c01faSSebastian Grimberg     read_T_trans_gm2sm<BASIS_Q, BASIS_P>(tx, dT, sT);
135f80f4a74SSebastian Grimberg   }
136f80f4a74SSebastian Grimberg 
137f80f4a74SSebastian Grimberg   // read U -- there is a sync at the end of this function
1389e0c01faSSebastian Grimberg   read_U_2d<CeedScalar, BASIS_Q, 1, BASIS_NUM_COMP, BASIS_Q, 0>(dU, cstrdU, rU, sTmp, tx);
139f80f4a74SSebastian Grimberg 
1409e0c01faSSebastian Grimberg   // no sync needed here -- read_U_2d already syncs at the end
1417132caa0SSebastian Grimberg   magma_interp_2d_device<CeedScalar, 1, 1, BASIS_NUM_COMP, BASIS_Q, BASIS_P, BASIS_Q, BASIS_P>(sT, rU, rV, tx, rTmp, sTmp);
142f80f4a74SSebastian Grimberg   __syncthreads();
143f80f4a74SSebastian Grimberg 
144f80f4a74SSebastian Grimberg   // write V
1459e0c01faSSebastian Grimberg   write_V_2d<CeedScalar, BASIS_P, 1, BASIS_NUM_COMP, BASIS_P, 0>(dV, cstrdV, rV, tx);
146f80f4a74SSebastian Grimberg }
147*db2becc9SJeremy L Thompson 
148*db2becc9SJeremy L Thompson ////////////////////////////////////////////////////////////////////////////////
149*db2becc9SJeremy L Thompson extern "C" __launch_bounds__(MAGMA_BASIS_BOUNDS(BASIS_MAX_P_Q, MAGMA_MAXTHREADS_2D)) __global__
150*db2becc9SJeremy L Thompson     void magma_interpta_2d_kernel(const CeedScalar *dT, const CeedScalar *dU, const int estrdU, const int cstrdU, CeedScalar *dV, const int estrdV,
151*db2becc9SJeremy L Thompson                                   const int cstrdV, const int nelem) {
152*db2becc9SJeremy L Thompson   MAGMA_DEVICE_SHARED(CeedScalar, shared_data)
153*db2becc9SJeremy L Thompson 
154*db2becc9SJeremy L Thompson   const int tx      = threadIdx.x;
155*db2becc9SJeremy L Thompson   const int ty      = threadIdx.y;
156*db2becc9SJeremy L Thompson   const int elem_id = (blockIdx.x * blockDim.y) + ty;
157*db2becc9SJeremy L Thompson 
158*db2becc9SJeremy L Thompson   if (elem_id >= nelem) return;
159*db2becc9SJeremy L Thompson 
160*db2becc9SJeremy L Thompson   CeedScalar rU[1][BASIS_NUM_COMP][BASIS_Q] = {0.0};  // for a non-fused operator BASIS_DIM is always 1
161*db2becc9SJeremy L Thompson   CeedScalar rV[1][BASIS_NUM_COMP][BASIS_P] = {0.0};  // for a non-fused operator BASIS_DIM is always 1
162*db2becc9SJeremy L Thompson   CeedScalar rTmp                           = 0.0;
163*db2becc9SJeremy L Thompson 
164*db2becc9SJeremy L Thompson   // shift global memory pointers by elem stride
165*db2becc9SJeremy L Thompson   dU += elem_id * estrdU;
166*db2becc9SJeremy L Thompson   dV += elem_id * estrdV;
167*db2becc9SJeremy L Thompson 
168*db2becc9SJeremy L Thompson   // assign shared memory pointers
169*db2becc9SJeremy L Thompson   CeedScalar *sT   = (CeedScalar *)shared_data;
170*db2becc9SJeremy L Thompson   CeedScalar *sTmp = sT + BASIS_Q * BASIS_P;
171*db2becc9SJeremy L Thompson   sTmp += ty * (BASIS_Q * BASIS_MAX_P_Q);
172*db2becc9SJeremy L Thompson 
173*db2becc9SJeremy L Thompson   // read T
174*db2becc9SJeremy L Thompson   if (ty == 0) {
175*db2becc9SJeremy L Thompson     read_T_trans_gm2sm<BASIS_Q, BASIS_P>(tx, dT, sT);
176*db2becc9SJeremy L Thompson   }
177*db2becc9SJeremy L Thompson 
178*db2becc9SJeremy L Thompson   // read U -- there is a sync at the end of this function
179*db2becc9SJeremy L Thompson   read_U_2d<CeedScalar, BASIS_Q, 1, BASIS_NUM_COMP, BASIS_Q, 0>(dU, cstrdU, rU, sTmp, tx);
180*db2becc9SJeremy L Thompson 
181*db2becc9SJeremy L Thompson   // no sync needed here -- read_U_2d already syncs at the end
182*db2becc9SJeremy L Thompson   magma_interp_2d_device<CeedScalar, 1, 1, BASIS_NUM_COMP, BASIS_Q, BASIS_P, BASIS_Q, BASIS_P>(sT, rU, rV, tx, rTmp, sTmp);
183*db2becc9SJeremy L Thompson   __syncthreads();
184*db2becc9SJeremy L Thompson 
185*db2becc9SJeremy L Thompson   // sum into V
186*db2becc9SJeremy L Thompson   sum_V_2d<CeedScalar, BASIS_P, 1, BASIS_NUM_COMP, BASIS_P, 0>(dV, cstrdV, rV, tx);
187*db2becc9SJeremy L Thompson }
188