xref: /libCEED/interface/ceed-tensor.c (revision 2f86a9204e4fbd31e43e0982a43b4a40f1fd11a7)
1*2f86a920SJeremy L Thompson // Copyright (c) 2017, Lawrence Livermore National Security, LLC. Produced at
2*2f86a920SJeremy L Thompson // the Lawrence Livermore National Laboratory. LLNL-CODE-734707. All Rights
3*2f86a920SJeremy L Thompson // 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 <ceed-impl.h>
18*2f86a920SJeremy L Thompson #include <ceed-backend.h>
19*2f86a920SJeremy L Thompson 
20*2f86a920SJeremy L Thompson /// @file
21*2f86a920SJeremy L Thompson /// Implementation of backend CeedTensorContract interfaces
22*2f86a920SJeremy L Thompson ///
23*2f86a920SJeremy L Thompson /// @addtogroup CeedBasis
24*2f86a920SJeremy L Thompson /// @{
25*2f86a920SJeremy L Thompson 
26*2f86a920SJeremy L Thompson /**
27*2f86a920SJeremy L Thompson   @brief Create a CeedTensorContract object for a CeedBasis
28*2f86a920SJeremy L Thompson 
29*2f86a920SJeremy L Thompson   @param ceed       A Ceed object where the CeedTensorContract will be created
30*2f86a920SJeremy L Thompson   @param contract   Address of the variable where the newly created
31*2f86a920SJeremy L Thompson                       CeedTensorContract will be stored.
32*2f86a920SJeremy L Thompson 
33*2f86a920SJeremy L Thompson   @return An error code: 0 - success, otherwise - failure
34*2f86a920SJeremy L Thompson 
35*2f86a920SJeremy L Thompson   @ref Advanced
36*2f86a920SJeremy L Thompson **/
37*2f86a920SJeremy L Thompson int CeedTensorContractCreate(Ceed ceed, CeedTensorContract *contract) {
38*2f86a920SJeremy L Thompson   int ierr;
39*2f86a920SJeremy L Thompson 
40*2f86a920SJeremy L Thompson   if (!ceed->TensorContractCreate) {
41*2f86a920SJeremy L Thompson     Ceed delegate;
42*2f86a920SJeremy L Thompson     ierr = CeedGetDelegate(ceed, &delegate); CeedChk(ierr);
43*2f86a920SJeremy L Thompson 
44*2f86a920SJeremy L Thompson     if (!delegate)
45*2f86a920SJeremy L Thompson       return CeedError(ceed, 1,
46*2f86a920SJeremy L Thompson                        "Backend does not support TensorContractCreate");
47*2f86a920SJeremy L Thompson 
48*2f86a920SJeremy L Thompson     ierr = CeedTensorContractCreate(delegate, contract);
49*2f86a920SJeremy L Thompson     CeedChk(ierr);
50*2f86a920SJeremy L Thompson     return 0;
51*2f86a920SJeremy L Thompson   }
52*2f86a920SJeremy L Thompson 
53*2f86a920SJeremy L Thompson   ierr = CeedCalloc(1,contract); CeedChk(ierr);
54*2f86a920SJeremy L Thompson 
55*2f86a920SJeremy L Thompson   (*contract)->ceed = ceed;
56*2f86a920SJeremy L Thompson   ceed->refcount++;
57*2f86a920SJeremy L Thompson   ierr = ceed->TensorContractCreate(*contract);
58*2f86a920SJeremy L Thompson   CeedChk(ierr);
59*2f86a920SJeremy L Thompson   return 0;
60*2f86a920SJeremy L Thompson };
61*2f86a920SJeremy L Thompson 
62*2f86a920SJeremy L Thompson /**
63*2f86a920SJeremy L Thompson   @brief Apply tensor contraction
64*2f86a920SJeremy L Thompson 
65*2f86a920SJeremy L Thompson     Contracts on the middle index
66*2f86a920SJeremy L Thompson     NOTRANSPOSE: v_ajc = t_jb u_abc
67*2f86a920SJeremy L Thompson     TRANSPOSE:   v_ajc = t_bj u_abc
68*2f86a920SJeremy L Thompson     If add != 0, "=" is replaced by "+="
69*2f86a920SJeremy L Thompson 
70*2f86a920SJeremy L Thompson   @param contract   CeedTensorContract to use
71*2f86a920SJeremy L Thompson   @param A          First index of u, v
72*2f86a920SJeremy L Thompson   @param B          Middle index of u, one index of t
73*2f86a920SJeremy L Thompson   @param C          Last index of u, v
74*2f86a920SJeremy L Thompson   @param J          Middle index of v, one index of t
75*2f86a920SJeremy L Thompson   @param[in] t      Tensor array to contract against
76*2f86a920SJeremy L Thompson   @param tmode      Transpose mode for t, \ref CEED_NOTRANSPOSE for t_jb
77*2f86a920SJeremy L Thompson                     \ref CEED_TRANSPOSE for t_bj
78*2f86a920SJeremy L Thompson   @param add        Add mode
79*2f86a920SJeremy L Thompson   @param[in] u      Input array
80*2f86a920SJeremy L Thompson   @param[out] v     Output array
81*2f86a920SJeremy L Thompson 
82*2f86a920SJeremy L Thompson   @return An error code: 0 - success, otherwise - failure
83*2f86a920SJeremy L Thompson 
84*2f86a920SJeremy L Thompson   @ref Advanced
85*2f86a920SJeremy L Thompson **/
86*2f86a920SJeremy L Thompson int CeedTensorContractApply(CeedTensorContract contract, CeedInt A, CeedInt B,
87*2f86a920SJeremy L Thompson                             CeedInt C, CeedInt J, const CeedScalar *restrict t,
88*2f86a920SJeremy L Thompson                             CeedTransposeMode tmode, const CeedInt add,
89*2f86a920SJeremy L Thompson                             const CeedScalar *restrict u,
90*2f86a920SJeremy L Thompson                             CeedScalar *restrict v) {
91*2f86a920SJeremy L Thompson   int ierr;
92*2f86a920SJeremy L Thompson 
93*2f86a920SJeremy L Thompson   ierr = contract->Apply(contract, A, B, C, J, t, tmode, add,  u, v);
94*2f86a920SJeremy L Thompson   CeedChk(ierr);
95*2f86a920SJeremy L Thompson   return 0;
96*2f86a920SJeremy L Thompson };
97*2f86a920SJeremy L Thompson 
98*2f86a920SJeremy L Thompson /**
99*2f86a920SJeremy L Thompson   @brief Get Ceed associated with a CeedTensorContract
100*2f86a920SJeremy L Thompson 
101*2f86a920SJeremy L Thompson   @param contract      CeedTensorContract
102*2f86a920SJeremy L Thompson   @param[out] ceed  Variable to store Ceed
103*2f86a920SJeremy L Thompson 
104*2f86a920SJeremy L Thompson   @return An error code: 0 - success, otherwise - failure
105*2f86a920SJeremy L Thompson 
106*2f86a920SJeremy L Thompson   @ref Advanced
107*2f86a920SJeremy L Thompson **/
108*2f86a920SJeremy L Thompson int CeedTensorContractGetCeed(CeedTensorContract contract, Ceed *ceed) {
109*2f86a920SJeremy L Thompson   *ceed = contract->ceed;
110*2f86a920SJeremy L Thompson 
111*2f86a920SJeremy L Thompson   return 0;
112*2f86a920SJeremy L Thompson };
113*2f86a920SJeremy L Thompson 
114*2f86a920SJeremy L Thompson /**
115*2f86a920SJeremy L Thompson   @brief Get backend data of a CeedTensorContract
116*2f86a920SJeremy L Thompson 
117*2f86a920SJeremy L Thompson   @param contract   CeedTensorContract
118*2f86a920SJeremy L Thompson   @param[out] data  Variable to store data
119*2f86a920SJeremy L Thompson 
120*2f86a920SJeremy L Thompson   @return An error code: 0 - success, otherwise - failure
121*2f86a920SJeremy L Thompson 
122*2f86a920SJeremy L Thompson   @ref Advanced
123*2f86a920SJeremy L Thompson **/
124*2f86a920SJeremy L Thompson int CeedTensorContractGetData(CeedTensorContract contract, void* *data) {
125*2f86a920SJeremy L Thompson   *data = contract->data;
126*2f86a920SJeremy L Thompson   return 0;
127*2f86a920SJeremy L Thompson }
128*2f86a920SJeremy L Thompson 
129*2f86a920SJeremy L Thompson /**
130*2f86a920SJeremy L Thompson   @brief Set backend data of a CeedTensorContract
131*2f86a920SJeremy L Thompson 
132*2f86a920SJeremy L Thompson   @param[out] contract CeedTensorContract
133*2f86a920SJeremy L Thompson   @param data          Data to set
134*2f86a920SJeremy L Thompson 
135*2f86a920SJeremy L Thompson   @return An error code: 0 - success, otherwise - failure
136*2f86a920SJeremy L Thompson 
137*2f86a920SJeremy L Thompson   @ref Advanced
138*2f86a920SJeremy L Thompson **/
139*2f86a920SJeremy L Thompson int CeedTensorContractSetData(CeedTensorContract contract, void* *data) {
140*2f86a920SJeremy L Thompson   contract->data = *data;
141*2f86a920SJeremy L Thompson   return 0;
142*2f86a920SJeremy L Thompson }
143*2f86a920SJeremy L Thompson 
144*2f86a920SJeremy L Thompson /**
145*2f86a920SJeremy L Thompson   @brief Destroy a CeedTensorContract
146*2f86a920SJeremy L Thompson 
147*2f86a920SJeremy L Thompson   @param contract   CeedTensorContract to destroy
148*2f86a920SJeremy L Thompson 
149*2f86a920SJeremy L Thompson   @return An error code: 0 - success, otherwise - failure
150*2f86a920SJeremy L Thompson 
151*2f86a920SJeremy L Thompson   @ref Advanced
152*2f86a920SJeremy L Thompson **/
153*2f86a920SJeremy L Thompson int CeedTensorContractDestroy(CeedTensorContract *contract) {
154*2f86a920SJeremy L Thompson   int ierr;
155*2f86a920SJeremy L Thompson 
156*2f86a920SJeremy L Thompson   if (!*contract || --(*contract)->refcount > 0) return 0;
157*2f86a920SJeremy L Thompson   if ((*contract)->Destroy) {
158*2f86a920SJeremy L Thompson     ierr = (*contract)->Destroy(*contract); CeedChk(ierr);
159*2f86a920SJeremy L Thompson   }
160*2f86a920SJeremy L Thompson   ierr = CeedDestroy(&(*contract)->ceed); CeedChk(ierr);
161*2f86a920SJeremy L Thompson   ierr = CeedFree(contract); CeedChk(ierr);
162*2f86a920SJeremy L Thompson   return 0;
163*2f86a920SJeremy L Thompson };
164*2f86a920SJeremy L Thompson 
165*2f86a920SJeremy L Thompson /// @}
166