xref: /libCEED/rust/libceed-sys/c-src/backends/ref/ceed-ref-basis.c (revision d31818812d7be450ff3261a9ad2006c4853912a7)
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 
54*d3181881Sjeremylt static int CeedBasisApply_Ref(CeedBasis basis, CeedInt nelem,
55*d3181881Sjeremylt                               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 
6321617c04Sjeremylt   if (tmode == CEED_TRANSPOSE) {
640f5de9e9Sjeremylt     const CeedInt vsize = ncomp*CeedPowInt(basis->P1d, dim);
6521617c04Sjeremylt     for (CeedInt i = 0; i < vsize; i++)
6621617c04Sjeremylt       v[i] = (CeedScalar) 0;
6721617c04Sjeremylt   }
6821617c04Sjeremylt   if (emode & CEED_EVAL_INTERP) {
6921617c04Sjeremylt     CeedInt P = basis->P1d, Q = basis->Q1d;
7021617c04Sjeremylt     if (tmode == CEED_TRANSPOSE) {
7121617c04Sjeremylt       P = basis->Q1d; Q = basis->P1d;
7221617c04Sjeremylt     }
730f5de9e9Sjeremylt     CeedInt pre = ncomp*CeedPowInt(P, dim-1), post = 1;
740f5de9e9Sjeremylt     CeedScalar tmp[2][ncomp*Q*CeedPowInt(P>Q?P:Q, dim-1)];
7521617c04Sjeremylt     for (CeedInt d=0; d<dim; d++) {
7621617c04Sjeremylt       ierr = CeedTensorContract_Ref(basis->ceed, pre, P, post, Q, basis->interp1d,
7721617c04Sjeremylt                                     tmode, add&&(d==dim-1),
7821617c04Sjeremylt                                     d==0?u:tmp[d%2], d==dim-1?v:tmp[(d+1)%2]);
7921617c04Sjeremylt       CeedChk(ierr);
8021617c04Sjeremylt       pre /= P;
8121617c04Sjeremylt       post *= Q;
8221617c04Sjeremylt     }
8321617c04Sjeremylt     if (tmode == CEED_NOTRANSPOSE) {
8421617c04Sjeremylt       v += nqpt;
8521617c04Sjeremylt     } else {
8621617c04Sjeremylt       u += nqpt;
8721617c04Sjeremylt     }
8821617c04Sjeremylt   }
8921617c04Sjeremylt   if (emode & CEED_EVAL_GRAD) {
9021617c04Sjeremylt     CeedInt P = basis->P1d, Q = basis->Q1d;
9121617c04Sjeremylt     // In CEED_NOTRANSPOSE mode:
920f5de9e9Sjeremylt     // u is (P^dim x nc), column-major layout (nc = ncomp)
930f5de9e9Sjeremylt     // v is (Q^dim x nc x dim), column-major layout (nc = ncomp)
9421617c04Sjeremylt     // In CEED_TRANSPOSE mode, the sizes of u and v are switched.
9521617c04Sjeremylt     if (tmode == CEED_TRANSPOSE) {
9621617c04Sjeremylt       P = basis->Q1d, Q = basis->P1d;
9721617c04Sjeremylt     }
980f5de9e9Sjeremylt     CeedScalar tmp[2][ncomp*Q*CeedPowInt(P>Q?P:Q, dim-1)];
9921617c04Sjeremylt     for (CeedInt p = 0; p < dim; p++) {
1000f5de9e9Sjeremylt       CeedInt pre = ncomp*CeedPowInt(P, dim-1), post = 1;
10121617c04Sjeremylt       for (CeedInt d=0; d<dim; d++) {
10221617c04Sjeremylt         ierr = CeedTensorContract_Ref(basis->ceed, pre, P, post, Q,
10321617c04Sjeremylt                                       (p==d)?basis->grad1d:basis->interp1d,
10421617c04Sjeremylt                                       tmode, add&&(d==dim-1),
10521617c04Sjeremylt                                       d==0?u:tmp[d%2], d==dim-1?v:tmp[(d+1)%2]);
10621617c04Sjeremylt         CeedChk(ierr);
10721617c04Sjeremylt         pre /= P;
10821617c04Sjeremylt         post *= Q;
10921617c04Sjeremylt       }
11021617c04Sjeremylt       if (tmode == CEED_NOTRANSPOSE) {
11121617c04Sjeremylt         v += nqpt;
11221617c04Sjeremylt       } else {
11321617c04Sjeremylt         u += nqpt;
11421617c04Sjeremylt       }
11521617c04Sjeremylt     }
11621617c04Sjeremylt   }
11721617c04Sjeremylt   if (emode & CEED_EVAL_WEIGHT) {
11821617c04Sjeremylt     if (tmode == CEED_TRANSPOSE)
11921617c04Sjeremylt       return CeedError(basis->ceed, 1,
12021617c04Sjeremylt                        "CEED_EVAL_WEIGHT incompatible with CEED_TRANSPOSE");
12121617c04Sjeremylt     CeedInt Q = basis->Q1d;
12221617c04Sjeremylt     for (CeedInt d=0; d<dim; d++) {
12321617c04Sjeremylt       CeedInt pre = CeedPowInt(Q, dim-d-1), post = CeedPowInt(Q, d);
12421617c04Sjeremylt       for (CeedInt i=0; i<pre; i++) {
12521617c04Sjeremylt         for (CeedInt j=0; j<Q; j++) {
12621617c04Sjeremylt           for (CeedInt k=0; k<post; k++) {
12721617c04Sjeremylt             v[(i*Q + j)*post + k] = basis->qweight1d[j]
12821617c04Sjeremylt                                     * (d == 0 ? 1 : v[(i*Q + j)*post + k]);
12921617c04Sjeremylt           }
13021617c04Sjeremylt         }
13121617c04Sjeremylt       }
13221617c04Sjeremylt     }
13321617c04Sjeremylt   }
13421617c04Sjeremylt   return 0;
13521617c04Sjeremylt }
13621617c04Sjeremylt 
13721617c04Sjeremylt static int CeedBasisDestroy_Ref(CeedBasis basis) {
13821617c04Sjeremylt   return 0;
13921617c04Sjeremylt }
14021617c04Sjeremylt 
14121617c04Sjeremylt int CeedBasisCreateTensorH1_Ref(Ceed ceed, CeedInt dim, CeedInt P1d,
14221617c04Sjeremylt                                 CeedInt Q1d, const CeedScalar *interp1d,
14321617c04Sjeremylt                                 const CeedScalar *grad1d,
14421617c04Sjeremylt                                 const CeedScalar *qref1d,
14521617c04Sjeremylt                                 const CeedScalar *qweight1d,
14621617c04Sjeremylt                                 CeedBasis basis) {
14721617c04Sjeremylt   basis->Apply = CeedBasisApply_Ref;
14821617c04Sjeremylt   basis->Destroy = CeedBasisDestroy_Ref;
14921617c04Sjeremylt   return 0;
15021617c04Sjeremylt }
151