xref: /libCEED/backends/ref/ceed-ref-basis.c (revision a460c33b23cf49899be5782d753f25b389750a7f)
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 *restrict t, CeedTransposeMode tmode,
28                                   const CeedInt Add,
29                                   const CeedScalar *restrict u, CeedScalar *restrict 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   for (CeedInt a=0; a<A; a++) {
42     for (CeedInt b=0; b<B; b++) {
43       for (CeedInt j=0; j<J; j++) {
44         CeedScalar tq = t[j*tstride0 + b*tstride1];
45         for (CeedInt c=0; c<C; c++) {
46           v[(a*J+j)*C+c] += tq * u[(a*B+b)*C+c];
47         }
48       }
49     }
50   }
51   return 0;
52 }
53 
54 static int CeedBasisApply_Ref(CeedBasis basis, CeedTransposeMode tmode,
55                               CeedEvalMode emode,
56                               const CeedScalar *u, CeedScalar *v) {
57   int ierr;
58   const CeedInt dim = basis->dim;
59   const CeedInt ndof = basis->ndof;
60   const CeedInt nqpt = ndof*CeedPowInt(basis->Q1d, dim);
61   const CeedInt add = (tmode == CEED_TRANSPOSE);
62 
63   if (tmode == CEED_TRANSPOSE) {
64     const CeedInt vsize = ndof*CeedPowInt(basis->P1d, dim);
65     for (CeedInt i = 0; i < vsize; i++)
66       v[i] = (CeedScalar) 0;
67   }
68   if (emode & CEED_EVAL_INTERP) {
69     CeedInt P = basis->P1d, Q = basis->Q1d;
70     if (tmode == CEED_TRANSPOSE) {
71       P = basis->Q1d; Q = basis->P1d;
72     }
73     CeedInt pre = ndof*CeedPowInt(P, dim-1), post = 1;
74     CeedScalar tmp[2][ndof*Q*CeedPowInt(P>Q?P:Q, dim-1)];
75     for (CeedInt d=0; d<dim; d++) {
76       ierr = CeedTensorContract_Ref(basis->ceed, pre, P, post, Q, basis->interp1d,
77                                     tmode, add&&(d==dim-1),
78                                     d==0?u:tmp[d%2], d==dim-1?v:tmp[(d+1)%2]);
79       CeedChk(ierr);
80       pre /= P;
81       post *= Q;
82     }
83     if (tmode == CEED_NOTRANSPOSE) {
84       v += nqpt;
85     } else {
86       u += nqpt;
87     }
88   }
89   if (emode & CEED_EVAL_GRAD) {
90     CeedInt P = basis->P1d, Q = basis->Q1d;
91     // In CEED_NOTRANSPOSE mode:
92     // u is (P^dim x nc), column-major layout (nc = ndof)
93     // v is (Q^dim x nc x dim), column-major layout (nc = ndof)
94     // In CEED_TRANSPOSE mode, the sizes of u and v are switched.
95     if (tmode == CEED_TRANSPOSE) {
96       P = basis->Q1d, Q = basis->P1d;
97     }
98     CeedScalar tmp[2][ndof*Q*CeedPowInt(P>Q?P:Q, dim-1)];
99     for (CeedInt p = 0; p < dim; p++) {
100       CeedInt pre = ndof*CeedPowInt(P, dim-1), post = 1;
101       for (CeedInt d=0; d<dim; d++) {
102         ierr = CeedTensorContract_Ref(basis->ceed, pre, P, post, Q,
103                                       (p==d)?basis->grad1d:basis->interp1d,
104                                       tmode, add&&(d==dim-1),
105                                       d==0?u:tmp[d%2], d==dim-1?v:tmp[(d+1)%2]);
106         CeedChk(ierr);
107         pre /= P;
108         post *= Q;
109       }
110       if (tmode == CEED_NOTRANSPOSE) {
111         v += nqpt;
112       } else {
113         u += nqpt;
114       }
115     }
116   }
117   if (emode & CEED_EVAL_WEIGHT) {
118     if (tmode == CEED_TRANSPOSE)
119       return CeedError(basis->ceed, 1,
120                        "CEED_EVAL_WEIGHT incompatible with CEED_TRANSPOSE");
121     CeedInt Q = basis->Q1d;
122     for (CeedInt d=0; d<dim; d++) {
123       CeedInt pre = CeedPowInt(Q, dim-d-1), post = CeedPowInt(Q, d);
124       for (CeedInt i=0; i<pre; i++) {
125         for (CeedInt j=0; j<Q; j++) {
126           for (CeedInt k=0; k<post; k++) {
127             v[(i*Q + j)*post + k] = basis->qweight1d[j]
128                                     * (d == 0 ? 1 : v[(i*Q + j)*post + k]);
129           }
130         }
131       }
132     }
133   }
134   return 0;
135 }
136 
137 static int CeedBasisDestroy_Ref(CeedBasis basis) {
138   return 0;
139 }
140 
141 int CeedBasisCreateTensorH1_Ref(Ceed ceed, CeedInt dim, CeedInt P1d,
142                                 CeedInt Q1d, const CeedScalar *interp1d,
143                                 const CeedScalar *grad1d,
144                                 const CeedScalar *qref1d,
145                                 const CeedScalar *qweight1d,
146                                 CeedBasis basis) {
147   basis->Apply = CeedBasisApply_Ref;
148   basis->Destroy = CeedBasisDestroy_Ref;
149   return 0;
150 }
151