xref: /libCEED/rust/libceed-sys/c-src/include/ceed/jit-source/magma/magma-basis-weight-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 weight in 3D
10*3c1e2affSSebastian Grimberg #ifndef CEED_MAGMA_BASIS_WEIGHT_3D_H
11*3c1e2affSSebastian Grimberg #define CEED_MAGMA_BASIS_WEIGHT_3D_H
12*3c1e2affSSebastian Grimberg 
13*3c1e2affSSebastian Grimberg #include "magma-common-tensor.h"
14*3c1e2affSSebastian Grimberg 
15f80f4a74SSebastian Grimberg //////////////////////////////////////////////////////////////////////////////////////////
16f80f4a74SSebastian Grimberg // weight basis action -- 3D
17*3c1e2affSSebastian Grimberg template <typename T, int DIM, int NUM_COMP, int Q, int i_DIM, int i_COMP>
18*3c1e2affSSebastian Grimberg static __device__ __inline__ void magma_weight_3d_device(const T *sTweight, T rV[DIM][NUM_COMP][Q], const int tx) {
19f80f4a74SSebastian Grimberg   // Assumptions
20*3c1e2affSSebastian Grimberg   // 1. 1D thread configuration of size Q^2
21f80f4a74SSebastian Grimberg   // 2. rV[][][] matches the storage used in other actions (interp, grad, ... etc)
22*3c1e2affSSebastian Grimberg   // 3. i_DIM and i_COMP specify which indexes to use in rV,
23*3c1e2affSSebastian Grimberg   //    since the output per thread is a register array of size Q
24f80f4a74SSebastian Grimberg   // 4. Sync is recommended after the call (to make sure sTweight can be overwritten)
25f80f4a74SSebastian Grimberg 
26*3c1e2affSSebastian Grimberg   if (tx < Q * Q) {
27f80f4a74SSebastian Grimberg     // x sTweight[j]    for first update
28*3c1e2affSSebastian Grimberg     // x sTweight[tx%Q] for second update
29*3c1e2affSSebastian Grimberg     // x sTweight[tx/Q] for third update
30*3c1e2affSSebastian Grimberg     for (int j = 0; j < Q; j++) {
31*3c1e2affSSebastian Grimberg       rV[i_DIM][i_COMP][j] = sTweight[j] * sTweight[tx % Q] * sTweight[tx / Q];
32f80f4a74SSebastian Grimberg     }
33f80f4a74SSebastian Grimberg   }
34f80f4a74SSebastian Grimberg }
35f80f4a74SSebastian Grimberg 
36f80f4a74SSebastian Grimberg //////////////////////////////////////////////////////////////////////////////////////////
37*3c1e2affSSebastian Grimberg extern "C" __launch_bounds__(MAGMA_BASIS_BOUNDS(BASIS_Q *BASIS_Q, MAGMA_MAXTHREADS_3D)) __global__
38f80f4a74SSebastian Grimberg     void magma_weight_3d_kernel(const CeedScalar *dqweight1d, CeedScalar *dV, const int v_stride, const int nelem) {
39f80f4a74SSebastian Grimberg   MAGMA_DEVICE_SHARED(CeedScalar, shared_data)
40f80f4a74SSebastian Grimberg 
41f80f4a74SSebastian Grimberg   const int tx      = threadIdx.x;
42f80f4a74SSebastian Grimberg   const int ty      = threadIdx.y;
43f80f4a74SSebastian Grimberg   const int elem_id = (blockIdx.x * blockDim.y) + ty;
44f80f4a74SSebastian Grimberg 
45f80f4a74SSebastian Grimberg   if (elem_id >= nelem) return;
46f80f4a74SSebastian Grimberg 
47*3c1e2affSSebastian Grimberg   CeedScalar rV[1][1][BASIS_Q];  // allocate with BASIS_DIM=BASIS_NUM_COMP=1, but sizes may differ for a fused operator
48f80f4a74SSebastian Grimberg   // global memory pointers
49f80f4a74SSebastian Grimberg   dV += elem_id * v_stride;
50f80f4a74SSebastian Grimberg 
51f80f4a74SSebastian Grimberg   // shared memory pointers
52f80f4a74SSebastian Grimberg   CeedScalar *sTweight = (CeedScalar *)shared_data;
53f80f4a74SSebastian Grimberg 
54f80f4a74SSebastian Grimberg   // read dqweight_1d
55*3c1e2affSSebastian Grimberg   if (tx < BASIS_Q) {
56f80f4a74SSebastian Grimberg     sTweight[tx] = dqweight1d[tx];
57f80f4a74SSebastian Grimberg   }
58f80f4a74SSebastian Grimberg   __syncthreads();
59f80f4a74SSebastian Grimberg 
60*3c1e2affSSebastian Grimberg   magma_weight_3d_device<CeedScalar, 1, 1, BASIS_Q, 0, 0>(sTweight, rV, tx);
61f80f4a74SSebastian Grimberg 
62f80f4a74SSebastian Grimberg   // write V
63*3c1e2affSSebastian Grimberg   if (tx < (BASIS_Q * BASIS_Q)) {
64*3c1e2affSSebastian Grimberg     for (int j = 0; j < BASIS_Q; j++) {
65*3c1e2affSSebastian Grimberg       dV[j * (BASIS_Q * BASIS_Q) + tx] = rV[0][0][j];
66f80f4a74SSebastian Grimberg     }
67f80f4a74SSebastian Grimberg   }
68f80f4a74SSebastian Grimberg }
69*3c1e2affSSebastian Grimberg 
70*3c1e2affSSebastian Grimberg #endif  // CEED_MAGMA_BASIS_WEIGHT_3D_H
71