xref: /libCEED/include/ceed/jit-source/hip/hip-ref-operator-assemble.h (revision a2cba4be6838e1a18742bb6cef1b14b2cf87e1c8)
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 #include <ceed/ceed.h>
9 
10 //------------------------------------------------------------------------------
11 // Matrix assembly kernel for low-order elements (2D thread block)
12 //------------------------------------------------------------------------------
13 extern "C" __launch_bounds__(BLOCK_SIZE)
14            __global__ void linearAssemble(const CeedScalar *B_in, const CeedScalar *B_out,
15                    const CeedScalar *__restrict__ qf_array,
16                    CeedScalar *__restrict__ values_array) {
17 
18   // This kernel assumes B_in and B_out have the same number of quadrature points and
19   // basis points.
20   // TODO: expand to more general cases
21   const int i = threadIdx.x; // The output row index of each B^TDB operation
22   const int l = threadIdx.y; // The output column index of each B^TDB operation
23 			     // such that we have (Bout^T)_ij D_jk Bin_kl = C_il
24 
25   // Strides for final output ordering, determined by the reference (interface) implementation of
26   // the symbolic assembly, slowest --> fastest: element, comp_in, comp_out, node_row, node_col
27   const CeedInt comp_out_stride = NNODES * NNODES;
28   const CeedInt comp_in_stride = comp_out_stride * NCOMP;
29   const CeedInt e_stride = comp_in_stride * NCOMP;
30   // Strides for QF array, slowest --> fastest:  emode_in, comp_in, emode_out, comp_out, elem, qpt
31   const CeedInt qe_stride = NQPTS;
32   const CeedInt qcomp_out_stride = NELEM * qe_stride;
33   const CeedInt qemode_out_stride = qcomp_out_stride * NCOMP;
34   const CeedInt qcomp_in_stride = qemode_out_stride * NUMEMODEOUT;
35   const CeedInt qemode_in_stride = qcomp_in_stride * NCOMP;
36 
37   // Loop over each element (if necessary)
38   for (CeedInt e = blockIdx.x*blockDim.z + threadIdx.z; e < NELEM;
39          e += gridDim.x*blockDim.z) {
40     for (CeedInt comp_in = 0; comp_in < NCOMP; comp_in++) {
41       for (CeedInt comp_out = 0; comp_out < NCOMP; comp_out++) {
42         CeedScalar result = 0.0;
43         CeedInt qf_index_comp = qcomp_in_stride * comp_in + qcomp_out_stride * comp_out + qe_stride * e;
44         for (CeedInt emode_in = 0; emode_in < NUMEMODEIN; emode_in++) {
45           CeedInt b_in_index = emode_in * NQPTS * NNODES;
46       	  for (CeedInt emode_out = 0; emode_out < NUMEMODEOUT; emode_out++) {
47             CeedInt b_out_index = emode_out * NQPTS * NNODES;
48             CeedInt qf_index = qf_index_comp + qemode_out_stride * emode_out + qemode_in_stride * emode_in;
49  	        // Perform the B^T D B operation for this 'chunk' of D (the qf_array)
50             for (CeedInt j = 0; j < NQPTS; j++) {
51      	      result += B_out[b_out_index + j * NNODES  + i] * qf_array[qf_index + j] * B_in[b_in_index + j * NNODES + l];
52 	        }
53           } // end of emode_out
54         } // end of emode_in
55         CeedInt val_index = comp_in_stride * comp_in + comp_out_stride * comp_out + e_stride * e + NNODES * i + l;
56    	    values_array[val_index] = result;
57       } // end of out component
58     } // end of in component
59   } // end of element loop
60 }
61 
62 //------------------------------------------------------------------------------
63 // Fallback kernel for larger orders (1D thread block)
64 //------------------------------------------------------------------------------
65 extern "C" __launch_bounds__(BLOCK_SIZE)
66            __global__ void linearAssembleFallback(const CeedScalar *B_in, const CeedScalar *B_out,
67                    const CeedScalar *__restrict__ qf_array,
68                    CeedScalar *__restrict__ values_array) {
69 
70   // This kernel assumes B_in and B_out have the same number of quadrature points and
71   // basis points.
72   // TODO: expand to more general cases
73   const int l = threadIdx.x; // The output column index of each B^TDB operation
74 			     // such that we have (Bout^T)_ij D_jk Bin_kl = C_il
75 
76   // Strides for final output ordering, determined by the reference (interface) implementation of
77   // the symbolic assembly, slowest --> fastest: element, comp_in, comp_out, node_row, node_col
78   const CeedInt comp_out_stride = NNODES * NNODES;
79   const CeedInt comp_in_stride = comp_out_stride * NCOMP;
80   const CeedInt e_stride = comp_in_stride * NCOMP;
81   // Strides for QF array, slowest --> fastest:  emode_in, comp_in, emode_out, comp_out, elem, qpt
82   const CeedInt qe_stride = NQPTS;
83   const CeedInt qcomp_out_stride = NELEM * qe_stride;
84   const CeedInt qemode_out_stride = qcomp_out_stride * NCOMP;
85   const CeedInt qcomp_in_stride = qemode_out_stride * NUMEMODEOUT;
86   const CeedInt qemode_in_stride = qcomp_in_stride * NCOMP;
87 
88     // Loop over each element (if necessary)
89   for (CeedInt e = blockIdx.x*blockDim.z + threadIdx.z; e < NELEM;
90          e += gridDim.x*blockDim.z) {
91     for (CeedInt comp_in = 0; comp_in < NCOMP; comp_in++) {
92       for (CeedInt comp_out = 0; comp_out < NCOMP; comp_out++) {
93         for (CeedInt i = 0; i < NNODES; i++) {
94           CeedScalar result = 0.0;
95           CeedInt qf_index_comp = qcomp_in_stride * comp_in + qcomp_out_stride * comp_out + qe_stride * e;
96           for (CeedInt emode_in = 0; emode_in < NUMEMODEIN; emode_in++) {
97             CeedInt b_in_index = emode_in * NQPTS * NNODES;
98         	for (CeedInt emode_out = 0; emode_out < NUMEMODEOUT; emode_out++) {
99               CeedInt b_out_index = emode_out * NQPTS * NNODES;
100               CeedInt qf_index = qf_index_comp + qemode_out_stride * emode_out + qemode_in_stride * emode_in;
101    	          // Perform the B^T D B operation for this 'chunk' of D (the qf_array)
102               for (CeedInt j = 0; j < NQPTS; j++) {
103        	        result += B_out[b_out_index + j * NNODES  + i] * qf_array[qf_index + j] * B_in[b_in_index + j * NNODES + l];
104   	          }
105             } // end of emode_out
106           } // end of emode_in
107           CeedInt val_index = comp_in_stride * comp_in + comp_out_stride * comp_out + e_stride * e + NNODES * i + l;
108      	  values_array[val_index] = result;
109         } // end of loop over element node index, i
110       } // end of out component
111     } // end of in component
112   } // end of element loop
113 }
114 
115 //------------------------------------------------------------------------------
116