xref: /libCEED/backends/ref/ceed-ref-tensor.c (revision c907536f492a0d8a515e9b59267362cd88b5caa9)
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 <string.h>
18 #include "ceed-ref.h"
19 
20 int CeedTensorContractApply_Ref(CeedTensorContract contract, CeedInt A,
21                                 CeedInt B, CeedInt C, CeedInt J,
22                                 const CeedScalar *restrict t,
23                                 CeedTransposeMode tmode, const CeedInt Add,
24                                 const CeedScalar *restrict u,
25                                 CeedScalar *restrict v) {
26   CeedInt tstride0 = B, tstride1 = 1;
27   if (tmode == CEED_TRANSPOSE) {
28     tstride0 = 1; tstride1 = J;
29   }
30 
31   if (!Add)
32     for (CeedInt q=0; q<A*J*C; q++)
33       v[q] = (CeedScalar) 0.0;
34 
35   for (CeedInt a=0; a<A; a++)
36     for (CeedInt b=0; b<B; b++)
37       for (CeedInt j=0; j<J; j++) {
38         CeedScalar tq = t[j*tstride0 + b*tstride1];
39         for (CeedInt c=0; c<C; c++)
40           v[(a*J+j)*C+c] += tq * u[(a*B+b)*C+c];
41       }
42   return 0;
43 }
44 
45 static int CeedTensorContractDestroy_Ref(CeedTensorContract contract) {
46   return 0;
47 }
48 
49 int CeedTensorContractCreate_Ref(CeedBasis basis, CeedTensorContract contract) {
50   int ierr;
51   Ceed ceed;
52   ierr = CeedTensorContractGetCeed(contract, &ceed); CeedChk(ierr);
53 
54   ierr = CeedSetBackendFunction(ceed, "TensorContract", contract, "Apply",
55                                 CeedTensorContractApply_Ref); CeedChk(ierr);
56   ierr = CeedSetBackendFunction(ceed, "TensorContract", contract, "Destroy",
57                                 CeedTensorContractDestroy_Ref); CeedChk(ierr);
58 
59   return 0;
60 }
61