xref: /libCEED/backends/ref/ceed-ref-basis.c (revision c44a85f41cd2820e6e38bc7468b238fa89afb40f)
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 
3521617c04Sjeremylt   if (!Add) {
361c19f432SSteven Roberts     for (CeedInt q=0; q<A*J*C; q++) {
371c19f432SSteven Roberts       v[q] = (CeedScalar) 0.0;
3821617c04Sjeremylt     }
391c19f432SSteven Roberts   }
401c19f432SSteven Roberts 
411c19f432SSteven Roberts   for (CeedInt a=0; a<A; a++) {
4221617c04Sjeremylt     for (CeedInt b=0; b<B; b++) {
431c19f432SSteven Roberts       for (CeedInt j=0; j<J; j++) {
441c19f432SSteven Roberts         CeedScalar tq = t[j*tstride0 + b*tstride1];
4521617c04Sjeremylt         for (CeedInt c=0; c<C; c++) {
46282be9a0SSteven Roberts           v[(a*J+j)*C+c] += tq * u[(a*B+b)*C+c];
4721617c04Sjeremylt         }
4821617c04Sjeremylt       }
4921617c04Sjeremylt     }
5021617c04Sjeremylt   }
5121617c04Sjeremylt   return 0;
5221617c04Sjeremylt }
5321617c04Sjeremylt 
54d3181881Sjeremylt static int CeedBasisApply_Ref(CeedBasis basis, CeedInt nelem,
55d3181881Sjeremylt                               CeedTransposeMode tmode, CeedEvalMode emode,
5621617c04Sjeremylt                               const CeedScalar *u, CeedScalar *v) {
5721617c04Sjeremylt   int ierr;
5821617c04Sjeremylt   const CeedInt dim = basis->dim;
590f5de9e9Sjeremylt   const CeedInt ncomp = basis->ncomp;
600f5de9e9Sjeremylt   const CeedInt nqpt = ncomp*CeedPowInt(basis->Q1d, dim);
6121617c04Sjeremylt   const CeedInt add = (tmode == CEED_TRANSPOSE);
6221617c04Sjeremylt 
63*c44a85f4Sjeremylt   if (nelem != 1)
64*c44a85f4Sjeremylt     return CeedError(basis->ceed, 1,
65*c44a85f4Sjeremylt                      "This backend does not support BasisApply for multiple elements");
66*c44a85f4Sjeremylt 
6721617c04Sjeremylt   if (tmode == CEED_TRANSPOSE) {
680f5de9e9Sjeremylt     const CeedInt vsize = ncomp*CeedPowInt(basis->P1d, dim);
6921617c04Sjeremylt     for (CeedInt i = 0; i < vsize; i++)
7021617c04Sjeremylt       v[i] = (CeedScalar) 0;
7121617c04Sjeremylt   }
7221617c04Sjeremylt   if (emode & CEED_EVAL_INTERP) {
7321617c04Sjeremylt     CeedInt P = basis->P1d, Q = basis->Q1d;
7421617c04Sjeremylt     if (tmode == CEED_TRANSPOSE) {
7521617c04Sjeremylt       P = basis->Q1d; Q = basis->P1d;
7621617c04Sjeremylt     }
770f5de9e9Sjeremylt     CeedInt pre = ncomp*CeedPowInt(P, dim-1), post = 1;
780f5de9e9Sjeremylt     CeedScalar tmp[2][ncomp*Q*CeedPowInt(P>Q?P:Q, dim-1)];
7921617c04Sjeremylt     for (CeedInt d=0; d<dim; d++) {
8021617c04Sjeremylt       ierr = CeedTensorContract_Ref(basis->ceed, pre, P, post, Q, basis->interp1d,
8121617c04Sjeremylt                                     tmode, add&&(d==dim-1),
8221617c04Sjeremylt                                     d==0?u:tmp[d%2], d==dim-1?v:tmp[(d+1)%2]);
8321617c04Sjeremylt       CeedChk(ierr);
8421617c04Sjeremylt       pre /= P;
8521617c04Sjeremylt       post *= Q;
8621617c04Sjeremylt     }
8721617c04Sjeremylt     if (tmode == CEED_NOTRANSPOSE) {
8821617c04Sjeremylt       v += nqpt;
8921617c04Sjeremylt     } else {
9021617c04Sjeremylt       u += nqpt;
9121617c04Sjeremylt     }
9221617c04Sjeremylt   }
9321617c04Sjeremylt   if (emode & CEED_EVAL_GRAD) {
9421617c04Sjeremylt     CeedInt P = basis->P1d, Q = basis->Q1d;
9521617c04Sjeremylt     // In CEED_NOTRANSPOSE mode:
960f5de9e9Sjeremylt     // u is (P^dim x nc), column-major layout (nc = ncomp)
970f5de9e9Sjeremylt     // v is (Q^dim x nc x dim), column-major layout (nc = ncomp)
9821617c04Sjeremylt     // In CEED_TRANSPOSE mode, the sizes of u and v are switched.
9921617c04Sjeremylt     if (tmode == CEED_TRANSPOSE) {
10021617c04Sjeremylt       P = basis->Q1d, Q = basis->P1d;
10121617c04Sjeremylt     }
1020f5de9e9Sjeremylt     CeedScalar tmp[2][ncomp*Q*CeedPowInt(P>Q?P:Q, dim-1)];
10321617c04Sjeremylt     for (CeedInt p = 0; p < dim; p++) {
1040f5de9e9Sjeremylt       CeedInt pre = ncomp*CeedPowInt(P, dim-1), post = 1;
10521617c04Sjeremylt       for (CeedInt d=0; d<dim; d++) {
10621617c04Sjeremylt         ierr = CeedTensorContract_Ref(basis->ceed, pre, P, post, Q,
10721617c04Sjeremylt                                       (p==d)?basis->grad1d:basis->interp1d,
10821617c04Sjeremylt                                       tmode, add&&(d==dim-1),
10921617c04Sjeremylt                                       d==0?u:tmp[d%2], d==dim-1?v:tmp[(d+1)%2]);
11021617c04Sjeremylt         CeedChk(ierr);
11121617c04Sjeremylt         pre /= P;
11221617c04Sjeremylt         post *= Q;
11321617c04Sjeremylt       }
11421617c04Sjeremylt       if (tmode == CEED_NOTRANSPOSE) {
11521617c04Sjeremylt         v += nqpt;
11621617c04Sjeremylt       } else {
11721617c04Sjeremylt         u += nqpt;
11821617c04Sjeremylt       }
11921617c04Sjeremylt     }
12021617c04Sjeremylt   }
12121617c04Sjeremylt   if (emode & CEED_EVAL_WEIGHT) {
12221617c04Sjeremylt     if (tmode == CEED_TRANSPOSE)
12321617c04Sjeremylt       return CeedError(basis->ceed, 1,
12421617c04Sjeremylt                        "CEED_EVAL_WEIGHT incompatible with CEED_TRANSPOSE");
12521617c04Sjeremylt     CeedInt Q = basis->Q1d;
12621617c04Sjeremylt     for (CeedInt d=0; d<dim; d++) {
12721617c04Sjeremylt       CeedInt pre = CeedPowInt(Q, dim-d-1), post = CeedPowInt(Q, d);
12821617c04Sjeremylt       for (CeedInt i=0; i<pre; i++) {
12921617c04Sjeremylt         for (CeedInt j=0; j<Q; j++) {
13021617c04Sjeremylt           for (CeedInt k=0; k<post; k++) {
13121617c04Sjeremylt             v[(i*Q + j)*post + k] = basis->qweight1d[j]
13221617c04Sjeremylt                                     * (d == 0 ? 1 : v[(i*Q + j)*post + k]);
13321617c04Sjeremylt           }
13421617c04Sjeremylt         }
13521617c04Sjeremylt       }
13621617c04Sjeremylt     }
13721617c04Sjeremylt   }
13821617c04Sjeremylt   return 0;
13921617c04Sjeremylt }
14021617c04Sjeremylt 
14121617c04Sjeremylt static int CeedBasisDestroy_Ref(CeedBasis basis) {
14221617c04Sjeremylt   return 0;
14321617c04Sjeremylt }
14421617c04Sjeremylt 
14521617c04Sjeremylt int CeedBasisCreateTensorH1_Ref(Ceed ceed, CeedInt dim, CeedInt P1d,
14621617c04Sjeremylt                                 CeedInt Q1d, const CeedScalar *interp1d,
14721617c04Sjeremylt                                 const CeedScalar *grad1d,
14821617c04Sjeremylt                                 const CeedScalar *qref1d,
14921617c04Sjeremylt                                 const CeedScalar *qweight1d,
15021617c04Sjeremylt                                 CeedBasis basis) {
15121617c04Sjeremylt   basis->Apply = CeedBasisApply_Ref;
15221617c04Sjeremylt   basis->Destroy = CeedBasisDestroy_Ref;
15321617c04Sjeremylt   return 0;
15421617c04Sjeremylt }
155