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 gradient in 2D 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 //////////////////////////////////////////////////////////////////////////////// 187132caa0SSebastian Grimberg // Helper function to add or set into V 197132caa0SSebastian Grimberg template <typename T, bool Add> 207132caa0SSebastian Grimberg struct magma_grad_2d_device_accumulate; 217132caa0SSebastian Grimberg 227132caa0SSebastian Grimberg template <typename T> 237132caa0SSebastian Grimberg struct magma_grad_2d_device_accumulate<T, true> { 247132caa0SSebastian Grimberg static __device__ __inline__ void op(T &rV, const T &rTmp) { rV += rTmp; } 257132caa0SSebastian Grimberg }; 267132caa0SSebastian Grimberg 277132caa0SSebastian Grimberg template <typename T> 287132caa0SSebastian Grimberg struct magma_grad_2d_device_accumulate<T, false> { 297132caa0SSebastian Grimberg static __device__ __inline__ void op(T &rV, const T &rTmp) { rV = rTmp; } 307132caa0SSebastian Grimberg }; 317132caa0SSebastian Grimberg 327132caa0SSebastian Grimberg //////////////////////////////////////////////////////////////////////////////// 33f80f4a74SSebastian Grimberg // grad basis action (2D) 34f80f4a74SSebastian Grimberg // This function is called two times at a higher level for 2D 353c1e2affSSebastian Grimberg // DIM_U -- for the size of rU[DIM_U * NUM_COMP * MAX_P_Q] 363c1e2affSSebastian Grimberg // DIM_V -- for the size of rV[DIM_V * NUM_COMP * MAX_P_Q] 373c1e2affSSebastian Grimberg // i_DIM -- the index of the outermost loop over dimensions in grad 383c1e2affSSebastian Grimberg // i_DIM_U -- which dim index of rU is accessed (always 0 for notrans, 0 or 1 for trans) 393c1e2affSSebastian Grimberg // i_DIM_V -- which dim index of rV is accessed (0 or 1 for notrans, always 0 for trans) 407132caa0SSebastian Grimberg template <typename T, int DIM_U, int DIM_V, int NUM_COMP, int P, int Q, int rU_SIZE, int rV_SIZE, int i_DIM, int i_DIM_U, int i_DIM_V, bool ADD> 413c1e2affSSebastian Grimberg static __device__ __inline__ void magma_grad_2d_device(const T *sTinterp, const T *sTgrad, T rU[DIM_U][NUM_COMP][rU_SIZE], 427132caa0SSebastian Grimberg T rV[DIM_V][NUM_COMP][rV_SIZE], const int tx, T rTmp, T *swork) { 43f80f4a74SSebastian Grimberg // Assumptions 443c1e2affSSebastian Grimberg // 0. This device routine applies grad for one dim only (i_DIM), so it should be called twice for 2D 453c1e2affSSebastian Grimberg // 1. 1D threads of size max(P,Q) 463c1e2affSSebastian Grimberg // 2. input: rU[DIM_U x NUM_COMP x P] in registers (per thread) 473c1e2affSSebastian Grimberg // 3. output: rV[DIM_V x NUM_COMP x Q] in registers (per thread) 48f80f4a74SSebastian Grimberg // 4. Two products per each (dim,component) pair 493c1e2affSSebastian Grimberg // 4.1 Batch P of (1xP) matrices times (PxQ) matrix => Batch P of (1xQ) matrices 503c1e2affSSebastian Grimberg // 4.2 Batch 1 of (QxP) matrix times (PxQ) matrix => (QxQ) matrix 51f80f4a74SSebastian Grimberg // 6. Each thread computes one row of the output of each product 52f80f4a74SSebastian Grimberg // 7. Sync is recommended before and after the call 53f80f4a74SSebastian Grimberg 543c1e2affSSebastian Grimberg for (int comp = 0; comp < NUM_COMP; comp++) { 553c1e2affSSebastian Grimberg // 1st product -- Batch P of (1xP) matrices [reg] x (PxQ) [shmem] => Batch P of (1xQ) matrices 563c1e2affSSebastian Grimberg // the batch output P x (1xQ) is written on the fly to shmem 573c1e2affSSebastian Grimberg if (tx < P) { 58f80f4a74SSebastian Grimberg const int batchid = tx; 59f80f4a74SSebastian Grimberg const int sld = 1; 603c1e2affSSebastian Grimberg const T *sT = (i_DIM == 0) ? sTgrad : sTinterp; 613c1e2affSSebastian Grimberg T *sTmp = swork + batchid * (1 * Q); 623c1e2affSSebastian Grimberg for (int j = 0; j < Q; j++) { 63f80f4a74SSebastian Grimberg rTmp = 0.0; 643c1e2affSSebastian Grimberg for (int i = 0; i < P; i++) { 653c1e2affSSebastian Grimberg rTmp += rU[i_DIM_U][comp][i] * sT(i, j); 66f80f4a74SSebastian Grimberg } 67f80f4a74SSebastian Grimberg sTmp(0, j, sld) = rTmp; 68f80f4a74SSebastian Grimberg } 693c1e2affSSebastian Grimberg } // end of: if (tx < P) 70f80f4a74SSebastian Grimberg __syncthreads(); 71f80f4a74SSebastian Grimberg 723c1e2affSSebastian Grimberg // 2nd product -- Batch 1 of a (QxP) matrix [shmem] x (PxQ) [shmem] => (QxQ) matrix [reg] 733c1e2affSSebastian Grimberg if (tx < Q) { 74f80f4a74SSebastian Grimberg const int batchid = 0; 753c1e2affSSebastian Grimberg const int sld = Q; 763c1e2affSSebastian Grimberg const T *sT = (i_DIM == 1) ? sTgrad : sTinterp; 773c1e2affSSebastian Grimberg T *sTmp = swork + batchid * (Q * P); 783c1e2affSSebastian Grimberg for (int j = 0; j < Q; j++) { 79f80f4a74SSebastian Grimberg rTmp = 0.0; 803c1e2affSSebastian Grimberg for (int i = 0; i < P; i++) { 81f80f4a74SSebastian Grimberg rTmp += sTmp(tx, i, sld) * sT(i, j); 82f80f4a74SSebastian Grimberg } 837132caa0SSebastian Grimberg magma_grad_2d_device_accumulate<T, ADD>::op(rV[i_DIM_V][comp][j], rTmp); 84f80f4a74SSebastian Grimberg } 85f80f4a74SSebastian Grimberg } 86f80f4a74SSebastian Grimberg __syncthreads(); 873c1e2affSSebastian Grimberg } // loop over NUM_COMP 88f80f4a74SSebastian Grimberg } 89f80f4a74SSebastian Grimberg 909e0c01faSSebastian Grimberg //////////////////////////////////////////////////////////////////////////////// 913c1e2affSSebastian Grimberg extern "C" __launch_bounds__(MAGMA_BASIS_BOUNDS(BASIS_MAX_P_Q, MAGMA_MAXTHREADS_2D)) __global__ 92f80f4a74SSebastian Grimberg void magma_gradn_2d_kernel(const CeedScalar *dinterp1d, const CeedScalar *dgrad1d, const CeedScalar *dU, const int estrdU, const int cstrdU, 93f80f4a74SSebastian Grimberg const int dstrdU, CeedScalar *dV, const int estrdV, const int cstrdV, const int dstrdV, const int nelem) { 94f80f4a74SSebastian Grimberg MAGMA_DEVICE_SHARED(CeedScalar, shared_data) 95f80f4a74SSebastian Grimberg 96f80f4a74SSebastian Grimberg const int tx = threadIdx.x; 97f80f4a74SSebastian Grimberg const int ty = threadIdx.y; 98f80f4a74SSebastian Grimberg const int elem_id = (blockIdx.x * blockDim.y) + ty; 99f80f4a74SSebastian Grimberg 100f80f4a74SSebastian Grimberg if (elem_id >= nelem) return; 101f80f4a74SSebastian Grimberg 1023c1e2affSSebastian Grimberg CeedScalar rU[1][BASIS_NUM_COMP][BASIS_P] = {0.0}; // here DIM_U = 1, but might be different for a fused operator 1033c1e2affSSebastian Grimberg CeedScalar rV[1][BASIS_NUM_COMP][BASIS_Q] = {0.0}; // here DIM_V = 1, but might be different for a fused operator 104f80f4a74SSebastian Grimberg CeedScalar rTmp = 0.0; 105f80f4a74SSebastian Grimberg 106f80f4a74SSebastian Grimberg // shift global memory pointers by elem stride 107f80f4a74SSebastian Grimberg dU += elem_id * estrdU; 108f80f4a74SSebastian Grimberg dV += elem_id * estrdV; 109f80f4a74SSebastian Grimberg 110f80f4a74SSebastian Grimberg // assign shared memory pointers 1113c1e2affSSebastian Grimberg CeedScalar *sTinterp = (CeedScalar *)shared_data; 1123c1e2affSSebastian Grimberg CeedScalar *sTgrad = sTinterp + BASIS_P * BASIS_Q; 1133c1e2affSSebastian Grimberg CeedScalar *sTmp = sTgrad + BASIS_P * BASIS_Q; 1143c1e2affSSebastian Grimberg sTmp += ty * (BASIS_P * BASIS_MAX_P_Q); 115f80f4a74SSebastian Grimberg 116f80f4a74SSebastian Grimberg // read T 117f80f4a74SSebastian Grimberg if (ty == 0) { 1189e0c01faSSebastian Grimberg read_T_notrans_gm2sm<BASIS_P, BASIS_Q>(tx, dinterp1d, sTinterp); 1199e0c01faSSebastian Grimberg read_T_notrans_gm2sm<BASIS_P, BASIS_Q>(tx, dgrad1d, sTgrad); 120f80f4a74SSebastian Grimberg } 121f80f4a74SSebastian Grimberg 1223c1e2affSSebastian Grimberg /* read U (idim = 0 for dU, i_DIM = 0 for rU) -- 123f80f4a74SSebastian Grimberg there is a sync at the end of this function */ 1249e0c01faSSebastian Grimberg read_U_2d<CeedScalar, BASIS_P, 1, BASIS_NUM_COMP, BASIS_P, 0>(dU + (0 * dstrdU), cstrdU, rU, sTmp, tx); 125f80f4a74SSebastian Grimberg 1263c1e2affSSebastian Grimberg /* first call (i_DIM = 0, i_DIM_U = 0, i_DIM_V = 0) -- 127f80f4a74SSebastian Grimberg output from rV[0][][] into dV (idim = 0) */ 1287132caa0SSebastian Grimberg magma_grad_2d_device<CeedScalar, 1, 1, BASIS_NUM_COMP, BASIS_P, BASIS_Q, BASIS_P, BASIS_Q, 0, 0, 0, false>(sTinterp, sTgrad, rU, rV, tx, rTmp, 1297132caa0SSebastian Grimberg sTmp); 130f80f4a74SSebastian Grimberg /* there is a sync at the end of magma_grad_2d_device */ 1319e0c01faSSebastian Grimberg write_V_2d<CeedScalar, BASIS_Q, 1, BASIS_NUM_COMP, BASIS_Q, 0>(dV + (0 * dstrdV), cstrdV, rV, tx); 132f80f4a74SSebastian Grimberg 1333c1e2affSSebastian Grimberg /* second call (i_DIM = 1, i_DIM_U = 0, i_DIM_V = 0) -- 134f80f4a74SSebastian Grimberg output from rV[0][][] into dV (idim = 1) */ 1357132caa0SSebastian Grimberg magma_grad_2d_device<CeedScalar, 1, 1, BASIS_NUM_COMP, BASIS_P, BASIS_Q, BASIS_P, BASIS_Q, 1, 0, 0, false>(sTinterp, sTgrad, rU, rV, tx, rTmp, 1367132caa0SSebastian Grimberg sTmp); 137f80f4a74SSebastian Grimberg /* there is a sync at the end of magma_grad_2d_device */ 1389e0c01faSSebastian Grimberg write_V_2d<CeedScalar, BASIS_Q, 1, BASIS_NUM_COMP, BASIS_Q, 0>(dV + (1 * dstrdV), cstrdV, rV, tx); 139f80f4a74SSebastian Grimberg } 140f80f4a74SSebastian Grimberg 1419e0c01faSSebastian Grimberg //////////////////////////////////////////////////////////////////////////////// 1423c1e2affSSebastian Grimberg extern "C" __launch_bounds__(MAGMA_BASIS_BOUNDS(BASIS_MAX_P_Q, MAGMA_MAXTHREADS_2D)) __global__ 143f80f4a74SSebastian Grimberg void magma_gradt_2d_kernel(const CeedScalar *dinterp1d, const CeedScalar *dgrad1d, const CeedScalar *dU, const int estrdU, const int cstrdU, 144f80f4a74SSebastian Grimberg const int dstrdU, CeedScalar *dV, const int estrdV, const int cstrdV, const int dstrdV, const int nelem) { 145f80f4a74SSebastian Grimberg MAGMA_DEVICE_SHARED(CeedScalar, shared_data) 146f80f4a74SSebastian Grimberg 147f80f4a74SSebastian Grimberg const int tx = threadIdx.x; 148f80f4a74SSebastian Grimberg const int ty = threadIdx.y; 149f80f4a74SSebastian Grimberg const int elem_id = (blockIdx.x * blockDim.y) + ty; 150f80f4a74SSebastian Grimberg 151f80f4a74SSebastian Grimberg if (elem_id >= nelem) return; 152f80f4a74SSebastian Grimberg 1533c1e2affSSebastian Grimberg CeedScalar rU[1][BASIS_NUM_COMP][BASIS_Q] = {0.0}; // here DIM_U = 1, but might be different for a fused operator 1543c1e2affSSebastian Grimberg CeedScalar rV[1][BASIS_NUM_COMP][BASIS_P] = {0.0}; // here DIM_V = 1, but might be different for a fused operator 155f80f4a74SSebastian Grimberg CeedScalar rTmp = 0.0; 156f80f4a74SSebastian Grimberg 157f80f4a74SSebastian Grimberg // shift global memory pointers by elem stride 158f80f4a74SSebastian Grimberg dU += elem_id * estrdU; 159f80f4a74SSebastian Grimberg dV += elem_id * estrdV; 160f80f4a74SSebastian Grimberg 161f80f4a74SSebastian Grimberg // assign shared memory pointers 1623c1e2affSSebastian Grimberg CeedScalar *sTinterp = (CeedScalar *)shared_data; 1633c1e2affSSebastian Grimberg CeedScalar *sTgrad = sTinterp + BASIS_Q * BASIS_P; 1643c1e2affSSebastian Grimberg CeedScalar *sTmp = sTgrad + BASIS_Q * BASIS_P; 1653c1e2affSSebastian Grimberg sTmp += ty * (BASIS_Q * BASIS_MAX_P_Q); 166f80f4a74SSebastian Grimberg 167f80f4a74SSebastian Grimberg // read T 168f80f4a74SSebastian Grimberg if (ty == 0) { 1699e0c01faSSebastian Grimberg read_T_trans_gm2sm<BASIS_Q, BASIS_P>(tx, dinterp1d, sTinterp); 1709e0c01faSSebastian Grimberg read_T_trans_gm2sm<BASIS_Q, BASIS_P>(tx, dgrad1d, sTgrad); 171f80f4a74SSebastian Grimberg } 172f80f4a74SSebastian Grimberg __syncthreads(); 173f80f4a74SSebastian Grimberg 1743c1e2affSSebastian Grimberg /* read U (idim = 0 for dU, i_DIM = 0 for rU) -- 175f80f4a74SSebastian Grimberg there is a sync at the end of this function */ 1769e0c01faSSebastian Grimberg read_U_2d<CeedScalar, BASIS_Q, 1, BASIS_NUM_COMP, BASIS_Q, 0>(dU + (0 * dstrdU), cstrdU, rU, sTmp, tx); 1773c1e2affSSebastian Grimberg /* first call (i_DIM = 0, i_DIM_U = 0, i_DIM_V = 0) */ 1787132caa0SSebastian Grimberg magma_grad_2d_device<CeedScalar, 1, 1, BASIS_NUM_COMP, BASIS_Q, BASIS_P, BASIS_Q, BASIS_P, 0, 0, 0, true>(sTinterp, sTgrad, rU, rV, tx, rTmp, sTmp); 179f80f4a74SSebastian Grimberg /* there is a sync at the end of magma_grad_2d_device */ 180f80f4a74SSebastian Grimberg 1813c1e2affSSebastian Grimberg /* read U (idim = 1 for dU, i_DIM = 0 for rU) -- 182f80f4a74SSebastian Grimberg there is a sync at the end of this function */ 1839e0c01faSSebastian Grimberg read_U_2d<CeedScalar, BASIS_Q, 1, BASIS_NUM_COMP, BASIS_Q, 0>(dU + (1 * dstrdU), cstrdU, rU, sTmp, tx); 1843c1e2affSSebastian Grimberg /* second call (i_DIM = 1, i_DIM_U = 0, i_DIM_V = 0) */ 1857132caa0SSebastian Grimberg magma_grad_2d_device<CeedScalar, 1, 1, BASIS_NUM_COMP, BASIS_Q, BASIS_P, BASIS_Q, BASIS_P, 1, 0, 0, true>(sTinterp, sTgrad, rU, rV, tx, rTmp, sTmp); 186f80f4a74SSebastian Grimberg /* there is a sync at the end of magma_grad_2d_device */ 187f80f4a74SSebastian Grimberg 188f80f4a74SSebastian Grimberg // write V 1899e0c01faSSebastian Grimberg write_V_2d<CeedScalar, BASIS_P, 1, BASIS_NUM_COMP, BASIS_P, 0>(dV + (0 * dstrdV), cstrdV, rV, tx); 190f80f4a74SSebastian Grimberg } 191*db2becc9SJeremy L Thompson 192*db2becc9SJeremy L Thompson //////////////////////////////////////////////////////////////////////////////// 193*db2becc9SJeremy L Thompson extern "C" __launch_bounds__(MAGMA_BASIS_BOUNDS(BASIS_MAX_P_Q, MAGMA_MAXTHREADS_2D)) __global__ 194*db2becc9SJeremy L Thompson void magma_gradta_2d_kernel(const CeedScalar *dinterp1d, const CeedScalar *dgrad1d, const CeedScalar *dU, const int estrdU, const int cstrdU, 195*db2becc9SJeremy L Thompson const int dstrdU, CeedScalar *dV, const int estrdV, const int cstrdV, const int dstrdV, const int nelem) { 196*db2becc9SJeremy L Thompson MAGMA_DEVICE_SHARED(CeedScalar, shared_data) 197*db2becc9SJeremy L Thompson 198*db2becc9SJeremy L Thompson const int tx = threadIdx.x; 199*db2becc9SJeremy L Thompson const int ty = threadIdx.y; 200*db2becc9SJeremy L Thompson const int elem_id = (blockIdx.x * blockDim.y) + ty; 201*db2becc9SJeremy L Thompson 202*db2becc9SJeremy L Thompson if (elem_id >= nelem) return; 203*db2becc9SJeremy L Thompson 204*db2becc9SJeremy L Thompson CeedScalar rU[1][BASIS_NUM_COMP][BASIS_Q] = {0.0}; // here DIM_U = 1, but might be different for a fused operator 205*db2becc9SJeremy L Thompson CeedScalar rV[1][BASIS_NUM_COMP][BASIS_P] = {0.0}; // here DIM_V = 1, but might be different for a fused operator 206*db2becc9SJeremy L Thompson CeedScalar rTmp = 0.0; 207*db2becc9SJeremy L Thompson 208*db2becc9SJeremy L Thompson // shift global memory pointers by elem stride 209*db2becc9SJeremy L Thompson dU += elem_id * estrdU; 210*db2becc9SJeremy L Thompson dV += elem_id * estrdV; 211*db2becc9SJeremy L Thompson 212*db2becc9SJeremy L Thompson // assign shared memory pointers 213*db2becc9SJeremy L Thompson CeedScalar *sTinterp = (CeedScalar *)shared_data; 214*db2becc9SJeremy L Thompson CeedScalar *sTgrad = sTinterp + BASIS_Q * BASIS_P; 215*db2becc9SJeremy L Thompson CeedScalar *sTmp = sTgrad + BASIS_Q * BASIS_P; 216*db2becc9SJeremy L Thompson sTmp += ty * (BASIS_Q * BASIS_MAX_P_Q); 217*db2becc9SJeremy L Thompson 218*db2becc9SJeremy L Thompson // read T 219*db2becc9SJeremy L Thompson if (ty == 0) { 220*db2becc9SJeremy L Thompson read_T_trans_gm2sm<BASIS_Q, BASIS_P>(tx, dinterp1d, sTinterp); 221*db2becc9SJeremy L Thompson read_T_trans_gm2sm<BASIS_Q, BASIS_P>(tx, dgrad1d, sTgrad); 222*db2becc9SJeremy L Thompson } 223*db2becc9SJeremy L Thompson __syncthreads(); 224*db2becc9SJeremy L Thompson 225*db2becc9SJeremy L Thompson /* read U (idim = 0 for dU, i_DIM = 0 for rU) -- 226*db2becc9SJeremy L Thompson there is a sync at the end of this function */ 227*db2becc9SJeremy L Thompson read_U_2d<CeedScalar, BASIS_Q, 1, BASIS_NUM_COMP, BASIS_Q, 0>(dU + (0 * dstrdU), cstrdU, rU, sTmp, tx); 228*db2becc9SJeremy L Thompson /* first call (i_DIM = 0, i_DIM_U = 0, i_DIM_V = 0) */ 229*db2becc9SJeremy L Thompson magma_grad_2d_device<CeedScalar, 1, 1, BASIS_NUM_COMP, BASIS_Q, BASIS_P, BASIS_Q, BASIS_P, 0, 0, 0, true>(sTinterp, sTgrad, rU, rV, tx, rTmp, sTmp); 230*db2becc9SJeremy L Thompson /* there is a sync at the end of magma_grad_2d_device */ 231*db2becc9SJeremy L Thompson 232*db2becc9SJeremy L Thompson /* read U (idim = 1 for dU, i_DIM = 0 for rU) -- 233*db2becc9SJeremy L Thompson there is a sync at the end of this function */ 234*db2becc9SJeremy L Thompson read_U_2d<CeedScalar, BASIS_Q, 1, BASIS_NUM_COMP, BASIS_Q, 0>(dU + (1 * dstrdU), cstrdU, rU, sTmp, tx); 235*db2becc9SJeremy L Thompson /* second call (i_DIM = 1, i_DIM_U = 0, i_DIM_V = 0) */ 236*db2becc9SJeremy L Thompson magma_grad_2d_device<CeedScalar, 1, 1, BASIS_NUM_COMP, BASIS_Q, BASIS_P, BASIS_Q, BASIS_P, 1, 0, 0, true>(sTinterp, sTgrad, rU, rV, tx, rTmp, sTmp); 237*db2becc9SJeremy L Thompson /* there is a sync at the end of magma_grad_2d_device */ 238*db2becc9SJeremy L Thompson 239*db2becc9SJeremy L Thompson // sum into V 240*db2becc9SJeremy L Thompson sum_V_2d<CeedScalar, BASIS_P, 1, BASIS_NUM_COMP, BASIS_P, 0>(dV + (0 * dstrdV), cstrdV, rV, tx); 241*db2becc9SJeremy L Thompson } 242