xref: /libCEED/rust/libceed-sys/c-src/backends/ref/ceed-ref-basis.c (revision a2b73c81b297ab7cb43a3fb34592f4ddecd41176)
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   for (CeedInt a=0; a<A; a++)
40     for (CeedInt b=0; b<B; b++)
41       for (CeedInt j=0; j<J; j++) {
42         CeedScalar tq = t[j*tstride0 + b*tstride1];
43         for (CeedInt c=0; c<C; c++)
44           v[(a*J+j)*C+c] += tq * u[(a*B+b)*C+c];
45       }
46   return 0;
47 }
48 
49 static int CeedBasisApply_Ref(CeedBasis basis, CeedInt nelem,
50                               CeedTransposeMode tmode, CeedEvalMode emode,
51                               const CeedScalar *u, CeedScalar *v) {
52   int ierr;
53   const CeedInt dim = basis->dim;
54   const CeedInt ncomp = basis->ncomp;
55   const CeedInt nqpt = CeedPowInt(basis->Q1d, dim);
56   const CeedInt add = (tmode == CEED_TRANSPOSE);
57 
58   if (nelem != 1)
59     return CeedError(basis->ceed, 1,
60                      "This backend does not support BasisApply for multiple elements");
61 
62   if (tmode == CEED_TRANSPOSE) {
63     const CeedInt vsize = ncomp*CeedPowInt(basis->P1d, dim);
64     for (CeedInt i = 0; i < vsize; i++)
65       v[i] = (CeedScalar) 0;
66   }
67   switch (emode) {
68   case 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 = ncomp*CeedPowInt(P, dim-1), post = 1;
74     CeedScalar tmp[2][ncomp*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   } break;
84   case CEED_EVAL_GRAD: {
85     CeedInt P = basis->P1d, Q = basis->Q1d;
86     // In CEED_NOTRANSPOSE mode:
87     // u is [dim, ncomp, P^dim, nelem], column-major layout
88     // v is [dim, ncomp, Q^dim, nelem], column-major layout
89     // In CEED_TRANSPOSE mode, the sizes of u and v are switched.
90     if (tmode == CEED_TRANSPOSE) {
91       P = basis->Q1d, Q = basis->P1d;
92     }
93     CeedScalar tmp[2][ncomp*Q*CeedPowInt(P>Q?P:Q, dim-1)];
94     for (CeedInt p = 0; p < dim; p++) {
95       CeedInt pre = ncomp*CeedPowInt(P, dim-1), post = 1;
96       for (CeedInt d=0; d<dim; d++) {
97         ierr = CeedTensorContract_Ref(basis->ceed, pre, P, post, Q,
98                                       (p==d)?basis->grad1d:basis->interp1d,
99                                       tmode, add&&(d==dim-1),
100                                       d==0
101                                        ? (tmode==CEED_NOTRANSPOSE?u:u+p*ncomp*nqpt)
102                                        : tmp[d%2],
103                                       d==dim-1
104                                        ? (tmode==CEED_TRANSPOSE?v:v+p*ncomp*nqpt)
105                                        : tmp[(d+1)%2]);
106         CeedChk(ierr);
107         pre /= P;
108         post *= Q;
109       }
110     }
111   } break;
112   case CEED_EVAL_WEIGHT: {
113     if (tmode == CEED_TRANSPOSE)
114       return CeedError(basis->ceed, 1,
115                        "CEED_EVAL_WEIGHT incompatible with CEED_TRANSPOSE");
116     CeedInt Q = basis->Q1d;
117     for (CeedInt d=0; d<dim; d++) {
118       CeedInt pre = CeedPowInt(Q, dim-d-1), post = CeedPowInt(Q, d);
119       for (CeedInt i=0; i<pre; i++)
120         for (CeedInt j=0; j<Q; j++)
121           for (CeedInt k=0; k<post; k++)
122             v[(i*Q + j)*post + k] = basis->qweight1d[j]
123                                     * (d == 0 ? 1 : v[(i*Q + j)*post + k]);
124     }
125   } break;
126   case CEED_EVAL_DIV:
127     return CeedError(basis->ceed, 1, "CEED_EVAL_DIV not supported");
128   case CEED_EVAL_CURL:
129     return CeedError(basis->ceed, 1, "CEED_EVAL_CURL not supported");
130   case CEED_EVAL_NONE:
131     return CeedError(basis->ceed, 1, "CEED_EVAL_NONE does not make sense in this context");
132    }
133   return 0;
134 }
135 
136 static int CeedBasisDestroy_Ref(CeedBasis basis) {
137   return 0;
138 }
139 
140 int CeedBasisCreateTensorH1_Ref(Ceed ceed, CeedInt dim, CeedInt P1d,
141                                 CeedInt Q1d, const CeedScalar *interp1d,
142                                 const CeedScalar *grad1d,
143                                 const CeedScalar *qref1d,
144                                 const CeedScalar *qweight1d,
145                                 CeedBasis basis) {
146   basis->Apply = CeedBasisApply_Ref;
147   basis->Destroy = CeedBasisDestroy_Ref;
148   return 0;
149 }
150