xref: /libCEED/include/ceed/jit-source/hip/hip-ref-operator-assemble-diagonal.h (revision fab7d8a4739c1f3003bebf7c4e30d5f80b320293)
1 // Copyright (c) 2017-2022, 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 HIP operator diagonal assembly
10 #ifndef CEED_HIP_REF_OPERATOR_ASSEMBLE_DIAGONAL_H
11 #define CEED_HIP_REF_OPERATOR_ASSEMBLE_DIAGONAL_H
12 
13 #include <ceed.h>
14 
15 #if USE_CEEDSIZE
16 typedef CeedSize IndexType;
17 #else
18 typedef CeedInt IndexType;
19 #endif
20 
21 //------------------------------------------------------------------------------
22 // Get basis pointer
23 //------------------------------------------------------------------------------
24 static __device__ __inline__ void GetBasisPointer(const CeedScalar **basis_ptr, CeedEvalMode eval_modes, const CeedScalar *identity,
25                                                   const CeedScalar *interp, const CeedScalar *grad, const CeedScalar *div, const CeedScalar *curl) {
26   switch (eval_modes) {
27     case CEED_EVAL_NONE:
28       *basis_ptr = identity;
29       break;
30     case CEED_EVAL_INTERP:
31       *basis_ptr = interp;
32       break;
33     case CEED_EVAL_GRAD:
34       *basis_ptr = grad;
35       break;
36     case CEED_EVAL_DIV:
37       *basis_ptr = div;
38       break;
39     case CEED_EVAL_CURL:
40       *basis_ptr = curl;
41       break;
42     case CEED_EVAL_WEIGHT:
43       break;  // Caught by QF assembly
44   }
45 }
46 
47 //------------------------------------------------------------------------------
48 // Core code for diagonal assembly
49 //------------------------------------------------------------------------------
50 extern "C" __launch_bounds__(BLOCK_SIZE) __global__
51     void LinearDiagonal(const CeedInt num_elem, const CeedScalar *identity, const CeedScalar *interp_in, const CeedScalar *grad_in,
52                         const CeedScalar *div_in, const CeedScalar *curl_in, const CeedScalar *interp_out, const CeedScalar *grad_out,
53                         const CeedScalar *div_out, const CeedScalar *curl_out, const CeedEvalMode *eval_modes_in, const CeedEvalMode *eval_modes_out,
54                         const CeedScalar *__restrict__ assembled_qf_array, CeedScalar *__restrict__ elem_diag_array) {
55   const int tid = threadIdx.x;  // Running with P threads
56 
57   if (tid >= NUM_NODES) return;
58 
59   // Compute the diagonal of B^T D B
60   // Each element
61   for (IndexType e = blockIdx.x * blockDim.z + threadIdx.z; e < num_elem; e += gridDim.x * blockDim.z) {
62     // Each basis eval mode pair
63     IndexType    d_out               = 0;
64     CeedEvalMode eval_modes_out_prev = CEED_EVAL_NONE;
65 
66     for (IndexType e_out = 0; e_out < NUM_EVAL_MODES_OUT; e_out++) {
67       IndexType         d_in               = 0;
68       CeedEvalMode      eval_modes_in_prev = CEED_EVAL_NONE;
69       const CeedScalar *b_t                = NULL;
70 
71       GetBasisPointer(&b_t, eval_modes_out[e_out], identity, interp_out, grad_out, div_out, curl_out);
72       if (e_out == 0 || eval_modes_out[e_out] != eval_modes_out_prev) d_out = 0;
73       else b_t = &b_t[(++d_out) * NUM_QPTS * NUM_NODES];
74       eval_modes_out_prev = eval_modes_out[e_out];
75 
76       for (IndexType e_in = 0; e_in < NUM_EVAL_MODES_IN; e_in++) {
77         const CeedScalar *b = NULL;
78 
79         GetBasisPointer(&b, eval_modes_in[e_in], identity, interp_in, grad_in, div_in, curl_in);
80         if (e_in == 0 || eval_modes_in[e_in] != eval_modes_in_prev) d_in = 0;
81         else b = &b[(++d_in) * NUM_QPTS * NUM_NODES];
82         eval_modes_in_prev = eval_modes_in[e_in];
83 
84         // Each component
85         for (IndexType comp_out = 0; comp_out < NUM_COMP; comp_out++) {
86 #if USE_POINT_BLOCK
87           // Point block diagonal
88           for (IndexType comp_in = 0; comp_in < NUM_COMP; comp_in++) {
89             CeedScalar e_value = 0.;
90 
91             // Each qpoint/node pair
92             for (IndexType q = 0; q < NUM_QPTS; q++) {
93               const CeedScalar qf_value =
94                   assembled_qf_array[((((e_in * NUM_COMP + comp_in) * NUM_EVAL_MODES_OUT + e_out) * NUM_COMP + comp_out) * num_elem + e) * NUM_QPTS +
95                                      q];
96 
97               e_value += b_t[q * NUM_NODES + tid] * qf_value * b[q * NUM_NODES + tid];
98             }
99             elem_diag_array[((comp_out * NUM_COMP + comp_in) * num_elem + e) * NUM_NODES + tid] += e_value;
100           }
101 #else
102           // Diagonal only
103           CeedScalar e_value = 0.;
104 
105           // Each qpoint/node pair
106           for (IndexType q = 0; q < NUM_QPTS; q++) {
107             const CeedScalar qf_value =
108                 assembled_qf_array[((((e_in * NUM_COMP + comp_out) * NUM_EVAL_MODES_OUT + e_out) * NUM_COMP + comp_out) * num_elem + e) * NUM_QPTS +
109                                    q];
110 
111             e_value += b_t[q * NUM_NODES + tid] * qf_value * b[q * NUM_NODES + tid];
112           }
113           elem_diag_array[(comp_out * num_elem + e) * NUM_NODES + tid] += e_value;
114 #endif
115         }
116       }
117     }
118   }
119 }
120 
121 //------------------------------------------------------------------------------
122 
123 #endif  // CEED_HIP_REF_OPERATOR_ASSEMBLE_DIAGONAL_H
124