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