xref: /libCEED/backends/ref/ceed-ref-basis.c (revision ecf6354e9a1bfdf9b8a51c1eb1e730eaa037defc)
121617c04Sjeremylt // Copyright (c) 2017-2018, Lawrence Livermore National Security, LLC.
221617c04Sjeremylt // Produced at the Lawrence Livermore National Laboratory. LLNL-CODE-734707.
321617c04Sjeremylt // All Rights reserved. See files LICENSE and NOTICE for details.
421617c04Sjeremylt //
521617c04Sjeremylt // This file is part of CEED, a collection of benchmarks, miniapps, software
621617c04Sjeremylt // libraries and APIs for efficient high-order finite element and spectral
721617c04Sjeremylt // element discretizations for exascale applications. For more information and
821617c04Sjeremylt // source code availability see http://github.com/ceed.
921617c04Sjeremylt //
1021617c04Sjeremylt // The CEED research is supported by the Exascale Computing Project 17-SC-20-SC,
1121617c04Sjeremylt // a collaborative effort of two U.S. Department of Energy organizations (Office
1221617c04Sjeremylt // of Science and the National Nuclear Security Administration) responsible for
1321617c04Sjeremylt // the planning and preparation of a capable exascale ecosystem, including
1421617c04Sjeremylt // software, applications, hardware, advanced system engineering and early
1521617c04Sjeremylt // testbed platforms, in support of the nation's exascale computing imperative.
1621617c04Sjeremylt 
1721617c04Sjeremylt #include <ceed-impl.h>
1821617c04Sjeremylt #include <string.h>
1921617c04Sjeremylt #include "ceed-ref.h"
2021617c04Sjeremylt 
2121617c04Sjeremylt // Contracts on the middle index
2221617c04Sjeremylt // NOTRANSPOSE: V_ajc = T_jb U_abc
2321617c04Sjeremylt // TRANSPOSE:   V_ajc = T_bj U_abc
2421617c04Sjeremylt // If Add != 0, "=" is replaced by "+="
2521617c04Sjeremylt static int CeedTensorContract_Ref(Ceed ceed,
2621617c04Sjeremylt                                   CeedInt A, CeedInt B, CeedInt C, CeedInt J,
2712170c1eSJed Brown                                   const CeedScalar *restrict t, CeedTransposeMode tmode,
2821617c04Sjeremylt                                   const CeedInt Add,
2912170c1eSJed Brown                                   const CeedScalar *restrict u, CeedScalar *restrict v) {
3021617c04Sjeremylt   CeedInt tstride0 = B, tstride1 = 1;
3121617c04Sjeremylt   if (tmode == CEED_TRANSPOSE) {
3221617c04Sjeremylt     tstride0 = 1; tstride1 = J;
3321617c04Sjeremylt   }
3421617c04Sjeremylt 
35a2b73c81Sjeremylt   if (!Add)
36a2b73c81Sjeremylt     for (CeedInt q=0; q<A*J*C; q++)
371c19f432SSteven Roberts       v[q] = (CeedScalar) 0.0;
381c19f432SSteven Roberts 
39a2b73c81Sjeremylt   for (CeedInt a=0; a<A; a++)
40a2b73c81Sjeremylt     for (CeedInt b=0; b<B; b++)
411c19f432SSteven Roberts       for (CeedInt j=0; j<J; j++) {
421c19f432SSteven Roberts         CeedScalar tq = t[j*tstride0 + b*tstride1];
43a2b73c81Sjeremylt         for (CeedInt c=0; c<C; c++)
44282be9a0SSteven Roberts           v[(a*J+j)*C+c] += tq * u[(a*B+b)*C+c];
4521617c04Sjeremylt       }
4621617c04Sjeremylt   return 0;
4721617c04Sjeremylt }
4821617c04Sjeremylt 
49d3181881Sjeremylt static int CeedBasisApply_Ref(CeedBasis basis, CeedInt nelem,
50d3181881Sjeremylt                               CeedTransposeMode tmode, CeedEvalMode emode,
5121617c04Sjeremylt                               const CeedScalar *u, CeedScalar *v) {
5221617c04Sjeremylt   int ierr;
5321617c04Sjeremylt   const CeedInt dim = basis->dim;
540f5de9e9Sjeremylt   const CeedInt ncomp = basis->ncomp;
55b5cf12eeSjeremylt   const CeedInt nqpt = CeedIntPow(basis->Q1d, dim);
5621617c04Sjeremylt   const CeedInt add = (tmode == CEED_TRANSPOSE);
5721617c04Sjeremylt 
58c44a85f4Sjeremylt   if (nelem != 1)
59c44a85f4Sjeremylt     return CeedError(basis->ceed, 1,
60c44a85f4Sjeremylt                      "This backend does not support BasisApply for multiple elements");
61c44a85f4Sjeremylt 
628d94b059Sjeremylt   // Clear v if operating in transpose
6321617c04Sjeremylt   if (tmode == CEED_TRANSPOSE) {
64b5cf12eeSjeremylt     const CeedInt vsize = ncomp*CeedIntPow(basis->P1d, dim);
6521617c04Sjeremylt     for (CeedInt i = 0; i < vsize; i++)
6621617c04Sjeremylt       v[i] = (CeedScalar) 0;
6721617c04Sjeremylt   }
68a2b73c81Sjeremylt   switch (emode) {
698d94b059Sjeremylt   // Interpolate to/from quadrature points
70a2b73c81Sjeremylt   case CEED_EVAL_INTERP: {
7121617c04Sjeremylt     CeedInt P = basis->P1d, Q = basis->Q1d;
7221617c04Sjeremylt     if (tmode == CEED_TRANSPOSE) {
7321617c04Sjeremylt       P = basis->Q1d; Q = basis->P1d;
7421617c04Sjeremylt     }
75b5cf12eeSjeremylt     CeedInt pre = ncomp*CeedIntPow(P, dim-1), post = 1;
76b5cf12eeSjeremylt     CeedScalar tmp[2][ncomp*Q*CeedIntPow(P>Q?P:Q, dim-1)];
7721617c04Sjeremylt     for (CeedInt d=0; d<dim; d++) {
7821617c04Sjeremylt       ierr = CeedTensorContract_Ref(basis->ceed, pre, P, post, Q, basis->interp1d,
7921617c04Sjeremylt                                     tmode, add&&(d==dim-1),
8021617c04Sjeremylt                                     d==0?u:tmp[d%2], d==dim-1?v:tmp[(d+1)%2]);
8121617c04Sjeremylt       CeedChk(ierr);
8221617c04Sjeremylt       pre /= P;
8321617c04Sjeremylt       post *= Q;
8421617c04Sjeremylt     }
85a2b73c81Sjeremylt   } break;
868d94b059Sjeremylt   // Evaluate the gradient to/from quadrature points
87a2b73c81Sjeremylt   case CEED_EVAL_GRAD: {
8821617c04Sjeremylt     CeedInt P = basis->P1d, Q = basis->Q1d;
8921617c04Sjeremylt     // In CEED_NOTRANSPOSE mode:
90*ecf6354eSJed Brown     // u has shape [dim, ncomp, P^dim, nelem], row-major layout
91*ecf6354eSJed Brown     // v has shape [dim, ncomp, Q^dim, nelem], row-major layout
9221617c04Sjeremylt     // In CEED_TRANSPOSE mode, the sizes of u and v are switched.
9321617c04Sjeremylt     if (tmode == CEED_TRANSPOSE) {
9421617c04Sjeremylt       P = basis->Q1d, Q = basis->P1d;
9521617c04Sjeremylt     }
96b5cf12eeSjeremylt     CeedScalar tmp[2][ncomp*Q*CeedIntPow(P>Q?P:Q, dim-1)];
9721617c04Sjeremylt     for (CeedInt p = 0; p < dim; p++) {
98b5cf12eeSjeremylt       CeedInt pre = ncomp*CeedIntPow(P, dim-1), post = 1;
9921617c04Sjeremylt       for (CeedInt d=0; d<dim; d++) {
10021617c04Sjeremylt         ierr = CeedTensorContract_Ref(basis->ceed, pre, P, post, Q,
10121617c04Sjeremylt                                       (p==d)?basis->grad1d:basis->interp1d,
10221617c04Sjeremylt                                       tmode, add&&(d==dim-1),
103a2b73c81Sjeremylt                                       d==0
104a2b73c81Sjeremylt                                        ? (tmode==CEED_NOTRANSPOSE?u:u+p*ncomp*nqpt)
105a2b73c81Sjeremylt                                        : tmp[d%2],
106a2b73c81Sjeremylt                                       d==dim-1
107a2b73c81Sjeremylt                                        ? (tmode==CEED_TRANSPOSE?v:v+p*ncomp*nqpt)
108a2b73c81Sjeremylt                                        : tmp[(d+1)%2]);
10921617c04Sjeremylt         CeedChk(ierr);
11021617c04Sjeremylt         pre /= P;
11121617c04Sjeremylt         post *= Q;
11221617c04Sjeremylt       }
11321617c04Sjeremylt     }
114a2b73c81Sjeremylt   } break;
1158d94b059Sjeremylt   // Retrieve interpolation weights
116a2b73c81Sjeremylt   case CEED_EVAL_WEIGHT: {
11721617c04Sjeremylt     if (tmode == CEED_TRANSPOSE)
11821617c04Sjeremylt       return CeedError(basis->ceed, 1,
11921617c04Sjeremylt                        "CEED_EVAL_WEIGHT incompatible with CEED_TRANSPOSE");
12021617c04Sjeremylt     CeedInt Q = basis->Q1d;
12121617c04Sjeremylt     for (CeedInt d=0; d<dim; d++) {
122b5cf12eeSjeremylt       CeedInt pre = CeedIntPow(Q, dim-d-1), post = CeedIntPow(Q, d);
123a2b73c81Sjeremylt       for (CeedInt i=0; i<pre; i++)
124a2b73c81Sjeremylt         for (CeedInt j=0; j<Q; j++)
125a2b73c81Sjeremylt           for (CeedInt k=0; k<post; k++)
12621617c04Sjeremylt             v[(i*Q + j)*post + k] = basis->qweight1d[j]
12721617c04Sjeremylt                                     * (d == 0 ? 1 : v[(i*Q + j)*post + k]);
12821617c04Sjeremylt     }
129a2b73c81Sjeremylt   } break;
1308d94b059Sjeremylt   // Evaluate the divergence to/from the quadrature points
131a2b73c81Sjeremylt   case CEED_EVAL_DIV:
132a2b73c81Sjeremylt     return CeedError(basis->ceed, 1, "CEED_EVAL_DIV not supported");
1338d94b059Sjeremylt   // Evaluate the curl to/from the quadrature points
134a2b73c81Sjeremylt   case CEED_EVAL_CURL:
135a2b73c81Sjeremylt     return CeedError(basis->ceed, 1, "CEED_EVAL_CURL not supported");
1368d94b059Sjeremylt   // Take no action, BasisApply should not have been called
137a2b73c81Sjeremylt   case CEED_EVAL_NONE:
138a2b73c81Sjeremylt     return CeedError(basis->ceed, 1, "CEED_EVAL_NONE does not make sense in this context");
13921617c04Sjeremylt    }
14021617c04Sjeremylt   return 0;
14121617c04Sjeremylt }
14221617c04Sjeremylt 
14321617c04Sjeremylt static int CeedBasisDestroy_Ref(CeedBasis basis) {
14421617c04Sjeremylt   return 0;
14521617c04Sjeremylt }
14621617c04Sjeremylt 
14721617c04Sjeremylt int CeedBasisCreateTensorH1_Ref(Ceed ceed, CeedInt dim, CeedInt P1d,
14821617c04Sjeremylt                                 CeedInt Q1d, const CeedScalar *interp1d,
14921617c04Sjeremylt                                 const CeedScalar *grad1d,
15021617c04Sjeremylt                                 const CeedScalar *qref1d,
15121617c04Sjeremylt                                 const CeedScalar *qweight1d,
15221617c04Sjeremylt                                 CeedBasis basis) {
15321617c04Sjeremylt   basis->Apply = CeedBasisApply_Ref;
15421617c04Sjeremylt   basis->Destroy = CeedBasisDestroy_Ref;
15521617c04Sjeremylt   return 0;
15621617c04Sjeremylt }
157