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