1 // Copyright (c) 2017-2018, Lawrence Livermore National Security, LLC. 2 // Produced at the Lawrence Livermore National Laboratory. LLNL-CODE-734707. 3 // All Rights reserved. See files LICENSE and NOTICE for details. 4 // 5 // This file is part of CEED, a collection of benchmarks, miniapps, software 6 // libraries and APIs for efficient high-order finite element and spectral 7 // element discretizations for exascale applications. For more information and 8 // source code availability see http://github.com/ceed. 9 // 10 // The CEED research is supported by the Exascale Computing Project 17-SC-20-SC, 11 // a collaborative effort of two U.S. Department of Energy organizations (Office 12 // of Science and the National Nuclear Security Administration) responsible for 13 // the planning and preparation of a capable exascale ecosystem, including 14 // software, applications, hardware, advanced system engineering and early 15 // testbed platforms, in support of the nation's exascale computing imperative. 16 17 #include <ceed-impl.h> 18 #include <string.h> 19 #include "ceed-ref.h" 20 21 // Contracts on the middle index 22 // NOTRANSPOSE: V_ajc = T_jb U_abc 23 // TRANSPOSE: V_ajc = T_bj U_abc 24 // If Add != 0, "=" is replaced by "+=" 25 static int CeedTensorContract_Ref(Ceed ceed, 26 CeedInt A, CeedInt B, CeedInt C, CeedInt J, 27 const CeedScalar *t, CeedTransposeMode tmode, 28 const CeedInt Add, 29 const CeedScalar *u, CeedScalar *v) { 30 CeedInt tstride0 = B, tstride1 = 1; 31 if (tmode == CEED_TRANSPOSE) { 32 tstride0 = 1; tstride1 = J; 33 } 34 35 if (!Add) { 36 for (CeedInt q=0; q<A*J*C; q++) { 37 v[q] = (CeedScalar) 0.0; 38 } 39 } 40 41 const CeedScalar *uP = u; 42 for (CeedInt a=0; a<A; a++) { 43 for (CeedInt b=0; b<B; b++) { 44 CeedScalar *vP = v + a * J * C; 45 for (CeedInt j=0; j<J; j++) { 46 CeedScalar tq = t[j*tstride0 + b*tstride1]; 47 for (CeedInt c=0; c<C; c++) { 48 *vP += tq * uP[c]; 49 vP++; 50 } 51 } 52 uP += C; 53 } 54 } 55 return 0; 56 } 57 58 static int CeedBasisApply_Ref(CeedBasis basis, CeedTransposeMode tmode, 59 CeedEvalMode emode, 60 const CeedScalar *u, CeedScalar *v) { 61 int ierr; 62 const CeedInt dim = basis->dim; 63 const CeedInt ndof = basis->ndof; 64 const CeedInt nqpt = ndof*CeedPowInt(basis->Q1d, dim); 65 const CeedInt add = (tmode == CEED_TRANSPOSE); 66 67 if (tmode == CEED_TRANSPOSE) { 68 const CeedInt vsize = ndof*CeedPowInt(basis->P1d, dim); 69 for (CeedInt i = 0; i < vsize; i++) 70 v[i] = (CeedScalar) 0; 71 } 72 if (emode & CEED_EVAL_INTERP) { 73 CeedInt P = basis->P1d, Q = basis->Q1d; 74 if (tmode == CEED_TRANSPOSE) { 75 P = basis->Q1d; Q = basis->P1d; 76 } 77 CeedInt pre = ndof*CeedPowInt(P, dim-1), post = 1; 78 CeedScalar tmp[2][ndof*Q*CeedPowInt(P>Q?P:Q, dim-1)]; 79 for (CeedInt d=0; d<dim; d++) { 80 ierr = CeedTensorContract_Ref(basis->ceed, pre, P, post, Q, basis->interp1d, 81 tmode, add&&(d==dim-1), 82 d==0?u:tmp[d%2], d==dim-1?v:tmp[(d+1)%2]); 83 CeedChk(ierr); 84 pre /= P; 85 post *= Q; 86 } 87 if (tmode == CEED_NOTRANSPOSE) { 88 v += nqpt; 89 } else { 90 u += nqpt; 91 } 92 } 93 if (emode & CEED_EVAL_GRAD) { 94 CeedInt P = basis->P1d, Q = basis->Q1d; 95 // In CEED_NOTRANSPOSE mode: 96 // u is (P^dim x nc), column-major layout (nc = ndof) 97 // v is (Q^dim x nc x dim), column-major layout (nc = ndof) 98 // In CEED_TRANSPOSE mode, the sizes of u and v are switched. 99 if (tmode == CEED_TRANSPOSE) { 100 P = basis->Q1d, Q = basis->P1d; 101 } 102 CeedScalar tmp[2][ndof*Q*CeedPowInt(P>Q?P:Q, dim-1)]; 103 for (CeedInt p = 0; p < dim; p++) { 104 CeedInt pre = ndof*CeedPowInt(P, dim-1), post = 1; 105 for (CeedInt d=0; d<dim; d++) { 106 ierr = CeedTensorContract_Ref(basis->ceed, pre, P, post, Q, 107 (p==d)?basis->grad1d:basis->interp1d, 108 tmode, add&&(d==dim-1), 109 d==0?u:tmp[d%2], d==dim-1?v:tmp[(d+1)%2]); 110 CeedChk(ierr); 111 pre /= P; 112 post *= Q; 113 } 114 if (tmode == CEED_NOTRANSPOSE) { 115 v += nqpt; 116 } else { 117 u += nqpt; 118 } 119 } 120 } 121 if (emode & CEED_EVAL_WEIGHT) { 122 if (tmode == CEED_TRANSPOSE) 123 return CeedError(basis->ceed, 1, 124 "CEED_EVAL_WEIGHT incompatible with CEED_TRANSPOSE"); 125 CeedInt Q = basis->Q1d; 126 for (CeedInt d=0; d<dim; d++) { 127 CeedInt pre = CeedPowInt(Q, dim-d-1), post = CeedPowInt(Q, d); 128 for (CeedInt i=0; i<pre; i++) { 129 for (CeedInt j=0; j<Q; j++) { 130 for (CeedInt k=0; k<post; k++) { 131 v[(i*Q + j)*post + k] = basis->qweight1d[j] 132 * (d == 0 ? 1 : v[(i*Q + j)*post + k]); 133 } 134 } 135 } 136 } 137 } 138 return 0; 139 } 140 141 static int CeedBasisDestroy_Ref(CeedBasis basis) { 142 return 0; 143 } 144 145 int CeedBasisCreateTensorH1_Ref(Ceed ceed, CeedInt dim, CeedInt P1d, 146 CeedInt Q1d, const CeedScalar *interp1d, 147 const CeedScalar *grad1d, 148 const CeedScalar *qref1d, 149 const CeedScalar *qweight1d, 150 CeedBasis basis) { 151 basis->Apply = CeedBasisApply_Ref; 152 basis->Destroy = CeedBasisDestroy_Ref; 153 return 0; 154 } 155