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 35*a2b73c81Sjeremylt if (!Add) 36*a2b73c81Sjeremylt for (CeedInt q=0; q<A*J*C; q++) 371c19f432SSteven Roberts v[q] = (CeedScalar) 0.0; 381c19f432SSteven Roberts 39*a2b73c81Sjeremylt for (CeedInt a=0; a<A; a++) 40*a2b73c81Sjeremylt 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]; 43*a2b73c81Sjeremylt 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; 55*a2b73c81Sjeremylt const CeedInt nqpt = CeedPowInt(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 6221617c04Sjeremylt if (tmode == CEED_TRANSPOSE) { 630f5de9e9Sjeremylt const CeedInt vsize = ncomp*CeedPowInt(basis->P1d, dim); 6421617c04Sjeremylt for (CeedInt i = 0; i < vsize; i++) 6521617c04Sjeremylt v[i] = (CeedScalar) 0; 6621617c04Sjeremylt } 67*a2b73c81Sjeremylt switch (emode) { 68*a2b73c81Sjeremylt case 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 } 83*a2b73c81Sjeremylt } break; 84*a2b73c81Sjeremylt case CEED_EVAL_GRAD: { 8521617c04Sjeremylt CeedInt P = basis->P1d, Q = basis->Q1d; 8621617c04Sjeremylt // In CEED_NOTRANSPOSE mode: 87*a2b73c81Sjeremylt // u is [dim, ncomp, P^dim, nelem], column-major layout 88*a2b73c81Sjeremylt // v is [dim, ncomp, Q^dim, nelem], column-major layout 8921617c04Sjeremylt // In CEED_TRANSPOSE mode, the sizes of u and v are switched. 9021617c04Sjeremylt if (tmode == CEED_TRANSPOSE) { 9121617c04Sjeremylt P = basis->Q1d, Q = basis->P1d; 9221617c04Sjeremylt } 930f5de9e9Sjeremylt CeedScalar tmp[2][ncomp*Q*CeedPowInt(P>Q?P:Q, dim-1)]; 9421617c04Sjeremylt for (CeedInt p = 0; p < dim; p++) { 950f5de9e9Sjeremylt CeedInt pre = ncomp*CeedPowInt(P, dim-1), post = 1; 9621617c04Sjeremylt for (CeedInt d=0; d<dim; d++) { 9721617c04Sjeremylt ierr = CeedTensorContract_Ref(basis->ceed, pre, P, post, Q, 9821617c04Sjeremylt (p==d)?basis->grad1d:basis->interp1d, 9921617c04Sjeremylt tmode, add&&(d==dim-1), 100*a2b73c81Sjeremylt d==0 101*a2b73c81Sjeremylt ? (tmode==CEED_NOTRANSPOSE?u:u+p*ncomp*nqpt) 102*a2b73c81Sjeremylt : tmp[d%2], 103*a2b73c81Sjeremylt d==dim-1 104*a2b73c81Sjeremylt ? (tmode==CEED_TRANSPOSE?v:v+p*ncomp*nqpt) 105*a2b73c81Sjeremylt : tmp[(d+1)%2]); 10621617c04Sjeremylt CeedChk(ierr); 10721617c04Sjeremylt pre /= P; 10821617c04Sjeremylt post *= Q; 10921617c04Sjeremylt } 11021617c04Sjeremylt } 111*a2b73c81Sjeremylt } break; 112*a2b73c81Sjeremylt case CEED_EVAL_WEIGHT: { 11321617c04Sjeremylt if (tmode == CEED_TRANSPOSE) 11421617c04Sjeremylt return CeedError(basis->ceed, 1, 11521617c04Sjeremylt "CEED_EVAL_WEIGHT incompatible with CEED_TRANSPOSE"); 11621617c04Sjeremylt CeedInt Q = basis->Q1d; 11721617c04Sjeremylt for (CeedInt d=0; d<dim; d++) { 11821617c04Sjeremylt CeedInt pre = CeedPowInt(Q, dim-d-1), post = CeedPowInt(Q, d); 119*a2b73c81Sjeremylt for (CeedInt i=0; i<pre; i++) 120*a2b73c81Sjeremylt for (CeedInt j=0; j<Q; j++) 121*a2b73c81Sjeremylt for (CeedInt k=0; k<post; k++) 12221617c04Sjeremylt v[(i*Q + j)*post + k] = basis->qweight1d[j] 12321617c04Sjeremylt * (d == 0 ? 1 : v[(i*Q + j)*post + k]); 12421617c04Sjeremylt } 125*a2b73c81Sjeremylt } break; 126*a2b73c81Sjeremylt case CEED_EVAL_DIV: 127*a2b73c81Sjeremylt return CeedError(basis->ceed, 1, "CEED_EVAL_DIV not supported"); 128*a2b73c81Sjeremylt case CEED_EVAL_CURL: 129*a2b73c81Sjeremylt return CeedError(basis->ceed, 1, "CEED_EVAL_CURL not supported"); 130*a2b73c81Sjeremylt case CEED_EVAL_NONE: 131*a2b73c81Sjeremylt return CeedError(basis->ceed, 1, "CEED_EVAL_NONE does not make sense in this context"); 13221617c04Sjeremylt } 13321617c04Sjeremylt return 0; 13421617c04Sjeremylt } 13521617c04Sjeremylt 13621617c04Sjeremylt static int CeedBasisDestroy_Ref(CeedBasis basis) { 13721617c04Sjeremylt return 0; 13821617c04Sjeremylt } 13921617c04Sjeremylt 14021617c04Sjeremylt int CeedBasisCreateTensorH1_Ref(Ceed ceed, CeedInt dim, CeedInt P1d, 14121617c04Sjeremylt CeedInt Q1d, const CeedScalar *interp1d, 14221617c04Sjeremylt const CeedScalar *grad1d, 14321617c04Sjeremylt const CeedScalar *qref1d, 14421617c04Sjeremylt const CeedScalar *qweight1d, 14521617c04Sjeremylt CeedBasis basis) { 14621617c04Sjeremylt basis->Apply = CeedBasisApply_Ref; 14721617c04Sjeremylt basis->Destroy = CeedBasisDestroy_Ref; 14821617c04Sjeremylt return 0; 14921617c04Sjeremylt } 150