xref: /libCEED/include/ceed/jit-source/magma/magma-basis-weight-3d.h (revision 4f69910b6e3819988a1446e35e0e85e74672bc23)
1 // Copyright (c) 2017-2025, Lawrence Livermore National Security, LLC and other CEED contributors.
2 // All Rights Reserved. See the top-level LICENSE and NOTICE files for details.
3 //
4 // SPDX-License-Identifier: BSD-2-Clause
5 //
6 // This file is part of CEED:  http://github.com/ceed
7 
8 /// @file
9 /// Internal header for MAGMA tensor basis weight in 3D
10 #include "magma-common-tensor.h"
11 
12 ////////////////////////////////////////////////////////////////////////////////
13 // weight basis action -- 3D
14 template <typename T, int DIM, int NUM_COMP, int Q, int i_DIM, int i_COMP>
15 static __device__ __inline__ void magma_weight_3d_device(const T *sTweight, T rV[DIM][NUM_COMP][Q], const int tx) {
16   // Assumptions
17   // 1. 1D thread configuration of size Q^2
18   // 2. rV[][][] matches the storage used in other actions (interp, grad, ... etc)
19   // 3. i_DIM and i_COMP specify which indexes to use in rV,
20   //    since the output per thread is a register array of size Q
21   // 4. Sync is recommended after the call (to make sure sTweight can be overwritten)
22 
23   if (tx < Q * Q) {
24     // x sTweight[j]    for first update
25     // x sTweight[tx%Q] for second update
26     // x sTweight[tx/Q] for third update
27     for (int j = 0; j < Q; j++) {
28       rV[i_DIM][i_COMP][j] = sTweight[j] * sTweight[tx % Q] * sTweight[tx / Q];
29     }
30   }
31 }
32 
33 ////////////////////////////////////////////////////////////////////////////////
34 extern "C" __launch_bounds__(MAGMA_BASIS_BOUNDS(BASIS_Q *BASIS_Q, MAGMA_MAXTHREADS_3D)) __global__
35     void magma_weight_3d_kernel(const CeedScalar *dqweight1d, CeedScalar *dV, const int v_stride, const int nelem) {
36   MAGMA_DEVICE_SHARED(CeedScalar, shared_data)
37 
38   const int tx      = threadIdx.x;
39   const int ty      = threadIdx.y;
40   const int elem_id = (blockIdx.x * blockDim.y) + ty;
41 
42   if (elem_id >= nelem) return;
43 
44   CeedScalar rV[1][1][BASIS_Q];  // allocate with BASIS_DIM=BASIS_NUM_COMP=1, but sizes may differ for a fused operator
45   // global memory pointers
46   dV += elem_id * v_stride;
47 
48   // shared memory pointers
49   CeedScalar *sTweight = (CeedScalar *)shared_data;
50 
51   // read dqweight_1d
52   if (tx < BASIS_Q) {
53     sTweight[tx] = dqweight1d[tx];
54   }
55   __syncthreads();
56 
57   magma_weight_3d_device<CeedScalar, 1, 1, BASIS_Q, 0, 0>(sTweight, rV, tx);
58 
59   // write V
60   if (tx < (BASIS_Q * BASIS_Q)) {
61     for (int j = 0; j < BASIS_Q; j++) {
62       dV[j * (BASIS_Q * BASIS_Q) + tx] = rV[0][0][j];
63     }
64   }
65 }
66