xref: /libCEED/backends/ref/ceed-ref-tensor.c (revision 2f86a9204e4fbd31e43e0982a43b4a40f1fd11a7)
1*2f86a920SJeremy L Thompson // Copyright (c) 2017-2018, Lawrence Livermore National Security, LLC.
2*2f86a920SJeremy L Thompson // Produced at the Lawrence Livermore National Laboratory. LLNL-CODE-734707.
3*2f86a920SJeremy L Thompson // All Rights reserved. See files LICENSE and NOTICE for details.
4*2f86a920SJeremy L Thompson //
5*2f86a920SJeremy L Thompson // This file is part of CEED, a collection of benchmarks, miniapps, software
6*2f86a920SJeremy L Thompson // libraries and APIs for efficient high-order finite element and spectral
7*2f86a920SJeremy L Thompson // element discretizations for exascale applications. For more information and
8*2f86a920SJeremy L Thompson // source code availability see http://github.com/ceed.
9*2f86a920SJeremy L Thompson //
10*2f86a920SJeremy L Thompson // The CEED research is supported by the Exascale Computing Project 17-SC-20-SC,
11*2f86a920SJeremy L Thompson // a collaborative effort of two U.S. Department of Energy organizations (Office
12*2f86a920SJeremy L Thompson // of Science and the National Nuclear Security Administration) responsible for
13*2f86a920SJeremy L Thompson // the planning and preparation of a capable exascale ecosystem, including
14*2f86a920SJeremy L Thompson // software, applications, hardware, advanced system engineering and early
15*2f86a920SJeremy L Thompson // testbed platforms, in support of the nation's exascale computing imperative.
16*2f86a920SJeremy L Thompson 
17*2f86a920SJeremy L Thompson #include <string.h>
18*2f86a920SJeremy L Thompson #include "ceed-ref.h"
19*2f86a920SJeremy L Thompson 
20*2f86a920SJeremy L Thompson int CeedTensorContractApply_Ref(CeedTensorContract contract, CeedInt A,
21*2f86a920SJeremy L Thompson                                 CeedInt B, CeedInt C, CeedInt J,
22*2f86a920SJeremy L Thompson                                 const CeedScalar *restrict t,
23*2f86a920SJeremy L Thompson                                 CeedTransposeMode tmode, const CeedInt Add,
24*2f86a920SJeremy L Thompson                                 const CeedScalar *restrict u,
25*2f86a920SJeremy L Thompson                                 CeedScalar *restrict v) {
26*2f86a920SJeremy L Thompson   CeedInt tstride0 = B, tstride1 = 1;
27*2f86a920SJeremy L Thompson   if (tmode == CEED_TRANSPOSE) {
28*2f86a920SJeremy L Thompson     tstride0 = 1; tstride1 = J;
29*2f86a920SJeremy L Thompson   }
30*2f86a920SJeremy L Thompson 
31*2f86a920SJeremy L Thompson   if (!Add)
32*2f86a920SJeremy L Thompson     for (CeedInt q=0; q<A*J*C; q++)
33*2f86a920SJeremy L Thompson       v[q] = (CeedScalar) 0.0;
34*2f86a920SJeremy L Thompson 
35*2f86a920SJeremy L Thompson   for (CeedInt a=0; a<A; a++)
36*2f86a920SJeremy L Thompson     for (CeedInt b=0; b<B; b++)
37*2f86a920SJeremy L Thompson       for (CeedInt j=0; j<J; j++) {
38*2f86a920SJeremy L Thompson         CeedScalar tq = t[j*tstride0 + b*tstride1];
39*2f86a920SJeremy L Thompson         for (CeedInt c=0; c<C; c++)
40*2f86a920SJeremy L Thompson           v[(a*J+j)*C+c] += tq * u[(a*B+b)*C+c];
41*2f86a920SJeremy L Thompson       }
42*2f86a920SJeremy L Thompson   return 0;
43*2f86a920SJeremy L Thompson }
44*2f86a920SJeremy L Thompson 
45*2f86a920SJeremy L Thompson static int CeedTensorContractDestroy_Ref(CeedTensorContract contract) {
46*2f86a920SJeremy L Thompson   return 0;
47*2f86a920SJeremy L Thompson }
48*2f86a920SJeremy L Thompson 
49*2f86a920SJeremy L Thompson int CeedTensorContractCreate_Ref(CeedTensorContract contract) {
50*2f86a920SJeremy L Thompson   int ierr;
51*2f86a920SJeremy L Thompson   Ceed ceed;
52*2f86a920SJeremy L Thompson   ierr = CeedTensorContractGetCeed(contract, &ceed); CeedChk(ierr);
53*2f86a920SJeremy L Thompson 
54*2f86a920SJeremy L Thompson   ierr = CeedSetBackendFunction(ceed, "TensorContract", contract, "Apply",
55*2f86a920SJeremy L Thompson                                 CeedTensorContractApply_Ref); CeedChk(ierr);
56*2f86a920SJeremy L Thompson   ierr = CeedSetBackendFunction(ceed, "TensorContract", contract, "Destroy",
57*2f86a920SJeremy L Thompson                                 CeedTensorContractDestroy_Ref); CeedChk(ierr);
58*2f86a920SJeremy L Thompson 
59*2f86a920SJeremy L Thompson   return 0;
60*2f86a920SJeremy L Thompson }
61