xref: /libCEED/interface/ceed-tensor.c (revision 4548da4e4ef44dc0f2704ad6d48ac0ca4a16bc83)
13d8e8822SJeremy L Thompson // Copyright (c) 2017-2022, Lawrence Livermore National Security, LLC and other CEED contributors.
23d8e8822SJeremy L Thompson // All Rights Reserved. See the top-level LICENSE and NOTICE files for details.
32f86a920SJeremy L Thompson //
43d8e8822SJeremy L Thompson // SPDX-License-Identifier: BSD-2-Clause
52f86a920SJeremy L Thompson //
63d8e8822SJeremy L Thompson // This file is part of CEED:  http://github.com/ceed
72f86a920SJeremy L Thompson 
83d576824SJeremy L Thompson #include <ceed-impl.h>
949aac155SJeremy L Thompson #include <ceed.h>
102b730f8bSJeremy L Thompson #include <ceed/backend.h>
1149aac155SJeremy L Thompson #include <stddef.h>
122f86a920SJeremy L Thompson 
132f86a920SJeremy L Thompson /// @file
147a982d89SJeremy L. Thompson /// Implementation of CeedTensorContract interfaces
157a982d89SJeremy L. Thompson 
167a982d89SJeremy L. Thompson /// ----------------------------------------------------------------------------
177a982d89SJeremy L. Thompson /// CeedTensorContract Backend API
187a982d89SJeremy L. Thompson /// ----------------------------------------------------------------------------
197a982d89SJeremy L. Thompson /// @addtogroup CeedBasisBackend
202f86a920SJeremy L Thompson /// @{
212f86a920SJeremy L Thompson 
222f86a920SJeremy L Thompson /**
232f86a920SJeremy L Thompson   @brief Create a CeedTensorContract object for a CeedBasis
242f86a920SJeremy L Thompson 
25ea61e9acSJeremy L Thompson   @param[in]  ceed     Ceed object where the CeedTensorContract will be created
26ea61e9acSJeremy L Thompson   @param[in]  basis    CeedBasis for which the tensor contraction will be used
27ea61e9acSJeremy L Thompson   @param[out] contract Address of the variable where the newly created CeedTensorContract will be stored.
282f86a920SJeremy L Thompson 
292f86a920SJeremy L Thompson   @return An error code: 0 - success, otherwise - failure
302f86a920SJeremy L Thompson 
317a982d89SJeremy L. Thompson   @ref Backend
322f86a920SJeremy L Thompson **/
332b730f8bSJeremy L Thompson int CeedTensorContractCreate(Ceed ceed, CeedBasis basis, CeedTensorContract *contract) {
342f86a920SJeremy L Thompson   if (!ceed->TensorContractCreate) {
352f86a920SJeremy L Thompson     Ceed delegate;
366574a04fSJeremy L Thompson 
372b730f8bSJeremy L Thompson     CeedCall(CeedGetObjectDelegate(ceed, &delegate, "TensorContract"));
386574a04fSJeremy L Thompson     CeedCheck(delegate, ceed, CEED_ERROR_UNSUPPORTED, "Backend does not support TensorContractCreate");
392b730f8bSJeremy L Thompson     CeedCall(CeedTensorContractCreate(delegate, basis, contract));
40e15f9bd0SJeremy L Thompson     return CEED_ERROR_SUCCESS;
412f86a920SJeremy L Thompson   }
422f86a920SJeremy L Thompson 
432b730f8bSJeremy L Thompson   CeedCall(CeedCalloc(1, contract));
44db002c03SJeremy L Thompson   CeedCall(CeedReferenceCopy(ceed, &(*contract)->ceed));
452b730f8bSJeremy L Thompson   CeedCall(ceed->TensorContractCreate(basis, *contract));
46e15f9bd0SJeremy L Thompson   return CEED_ERROR_SUCCESS;
477a982d89SJeremy L. Thompson }
482f86a920SJeremy L Thompson 
492f86a920SJeremy L Thompson /**
502f86a920SJeremy L Thompson   @brief Apply tensor contraction
512f86a920SJeremy L Thompson 
522f86a920SJeremy L Thompson   Contracts on the middle index
532f86a920SJeremy L Thompson   NOTRANSPOSE: v_ajc = t_jb u_abc
542f86a920SJeremy L Thompson   TRANSPOSE:   v_ajc = t_bj u_abc
552f86a920SJeremy L Thompson   If add != 0, "=" is replaced by "+="
562f86a920SJeremy L Thompson 
57ea61e9acSJeremy L Thompson   @param[in]  contract CeedTensorContract to use
58ea61e9acSJeremy L Thompson   @param[in]  A        First index of u, v
59ea61e9acSJeremy L Thompson   @param[in]  B        Middle index of u, one index of t
60ea61e9acSJeremy L Thompson   @param[in]  C        Last index of u, v
61ea61e9acSJeremy L Thompson   @param[in]  J        Middle index of v, one index of t
622f86a920SJeremy L Thompson   @param[in]  t        Tensor array to contract against
63ea61e9acSJeremy L Thompson   @param[in]  t_mode   Transpose mode for t, \ref CEED_NOTRANSPOSE for t_jb \ref CEED_TRANSPOSE for t_bj
64ea61e9acSJeremy L Thompson   @param[in]  add      Add mode
652f86a920SJeremy L Thompson   @param[in]  u        Input array
662f86a920SJeremy L Thompson   @param[out] v        Output array
672f86a920SJeremy L Thompson 
682f86a920SJeremy L Thompson   @return An error code: 0 - success, otherwise - failure
692f86a920SJeremy L Thompson 
707a982d89SJeremy L. Thompson   @ref Backend
712f86a920SJeremy L Thompson **/
722b730f8bSJeremy L Thompson int CeedTensorContractApply(CeedTensorContract contract, CeedInt A, CeedInt B, CeedInt C, CeedInt J, const CeedScalar *restrict t,
732b730f8bSJeremy L Thompson                             CeedTransposeMode t_mode, const CeedInt add, const CeedScalar *restrict u, CeedScalar *restrict v) {
742b730f8bSJeremy L Thompson   CeedCall(contract->Apply(contract, A, B, C, J, t, t_mode, add, u, v));
75e15f9bd0SJeremy L Thompson   return CEED_ERROR_SUCCESS;
767a982d89SJeremy L. Thompson }
772f86a920SJeremy L Thompson 
782f86a920SJeremy L Thompson /**
79c4e3f59bSSebastian Grimberg   @brief Apply tensor contraction
80c4e3f59bSSebastian Grimberg 
81c4e3f59bSSebastian Grimberg     Contracts on the middle index
82c4e3f59bSSebastian Grimberg     NOTRANSPOSE: v_dajc = t_djb u_abc
83c4e3f59bSSebastian Grimberg     TRANSPOSE:   v_ajc  = t_dbj u_dabc
84c4e3f59bSSebastian Grimberg     If add != 0, "=" is replaced by "+="
85c4e3f59bSSebastian Grimberg 
86c4e3f59bSSebastian Grimberg   @param[in]  contract CeedTensorContract to use
87c4e3f59bSSebastian Grimberg   @param[in]  A        First index of u, second index of v
88c4e3f59bSSebastian Grimberg   @param[in]  B        Middle index of u, one of last two indices of t
89c4e3f59bSSebastian Grimberg   @param[in]  C        Last index of u, v
90c4e3f59bSSebastian Grimberg   @param[in]  D        First index of v, first index of t
91c4e3f59bSSebastian Grimberg   @param[in]  J        Third index of v, one of last two indices of t
92c4e3f59bSSebastian Grimberg   @param[in]  t        Tensor array to contract against
93*4548da4eSSebastian Grimberg   @param[in]  t_mode   Transpose mode for t, \ref CEED_NOTRANSPOSE for t_djb \ref CEED_TRANSPOSE for t_dbj
94c4e3f59bSSebastian Grimberg   @param[in]  add      Add mode
95c4e3f59bSSebastian Grimberg   @param[in]  u        Input array
96c4e3f59bSSebastian Grimberg   @param[out] v        Output array
97c4e3f59bSSebastian Grimberg 
98c4e3f59bSSebastian Grimberg   @return An error code: 0 - success, otherwise - failure
99c4e3f59bSSebastian Grimberg 
100c4e3f59bSSebastian Grimberg   @ref Backend
101c4e3f59bSSebastian Grimberg **/
102c4e3f59bSSebastian Grimberg int CeedTensorContractStridedApply(CeedTensorContract contract, CeedInt A, CeedInt B, CeedInt C, CeedInt D, CeedInt J, const CeedScalar *restrict t,
103c4e3f59bSSebastian Grimberg                                    CeedTransposeMode t_mode, const CeedInt add, const CeedScalar *restrict u, CeedScalar *restrict v) {
104c4e3f59bSSebastian Grimberg   if (t_mode == CEED_TRANSPOSE) {
105c4e3f59bSSebastian Grimberg     for (CeedInt d = 0; d < D; d++) {
106c4e3f59bSSebastian Grimberg       CeedCall(contract->Apply(contract, A, J, C, B, t + d * B * J, t_mode, add, u + d * A * J * C, v));
107c4e3f59bSSebastian Grimberg     }
108c4e3f59bSSebastian Grimberg   } else {
109c4e3f59bSSebastian Grimberg     for (CeedInt d = 0; d < D; d++) {
110c4e3f59bSSebastian Grimberg       CeedCall(contract->Apply(contract, A, B, C, J, t + d * B * J, t_mode, add, u, v + d * A * J * C));
111c4e3f59bSSebastian Grimberg     }
112c4e3f59bSSebastian Grimberg   }
113c4e3f59bSSebastian Grimberg   return CEED_ERROR_SUCCESS;
114c4e3f59bSSebastian Grimberg }
115c4e3f59bSSebastian Grimberg 
116c4e3f59bSSebastian Grimberg /**
1172f86a920SJeremy L Thompson   @brief Get Ceed associated with a CeedTensorContract
1182f86a920SJeremy L Thompson 
119ea61e9acSJeremy L Thompson   @param[in]  contract CeedTensorContract
1202f86a920SJeremy L Thompson   @param[out] ceed     Variable to store Ceed
1212f86a920SJeremy L Thompson 
1222f86a920SJeremy L Thompson   @return An error code: 0 - success, otherwise - failure
1232f86a920SJeremy L Thompson 
1247a982d89SJeremy L. Thompson   @ref Backend
1252f86a920SJeremy L Thompson **/
1262f86a920SJeremy L Thompson int CeedTensorContractGetCeed(CeedTensorContract contract, Ceed *ceed) {
1272f86a920SJeremy L Thompson   *ceed = contract->ceed;
128e15f9bd0SJeremy L Thompson   return CEED_ERROR_SUCCESS;
1297a982d89SJeremy L. Thompson }
1302f86a920SJeremy L Thompson 
1312f86a920SJeremy L Thompson /**
1322f86a920SJeremy L Thompson   @brief Get backend data of a CeedTensorContract
1332f86a920SJeremy L Thompson 
134ea61e9acSJeremy L Thompson   @param[in]  contract CeedTensorContract
1352f86a920SJeremy L Thompson   @param[out] data     Variable to store data
1362f86a920SJeremy L Thompson 
1372f86a920SJeremy L Thompson   @return An error code: 0 - success, otherwise - failure
1382f86a920SJeremy L Thompson 
1397a982d89SJeremy L. Thompson   @ref Backend
1402f86a920SJeremy L Thompson **/
141777ff853SJeremy L Thompson int CeedTensorContractGetData(CeedTensorContract contract, void *data) {
142777ff853SJeremy L Thompson   *(void **)data = contract->data;
143e15f9bd0SJeremy L Thompson   return CEED_ERROR_SUCCESS;
1442f86a920SJeremy L Thompson }
1452f86a920SJeremy L Thompson 
1462f86a920SJeremy L Thompson /**
1472f86a920SJeremy L Thompson   @brief Set backend data of a CeedTensorContract
1482f86a920SJeremy L Thompson 
149ea61e9acSJeremy L Thompson   @param[in,out] contract CeedTensorContract
150ea61e9acSJeremy L Thompson   @param[in]     data     Data to set
1512f86a920SJeremy L Thompson 
1522f86a920SJeremy L Thompson   @return An error code: 0 - success, otherwise - failure
1532f86a920SJeremy L Thompson 
1547a982d89SJeremy L. Thompson   @ref Backend
1552f86a920SJeremy L Thompson **/
156777ff853SJeremy L Thompson int CeedTensorContractSetData(CeedTensorContract contract, void *data) {
157777ff853SJeremy L Thompson   contract->data = data;
158e15f9bd0SJeremy L Thompson   return CEED_ERROR_SUCCESS;
1592f86a920SJeremy L Thompson }
1602f86a920SJeremy L Thompson 
1612f86a920SJeremy L Thompson /**
16234359f16Sjeremylt   @brief Increment the reference counter for a CeedTensorContract
16334359f16Sjeremylt 
164ea61e9acSJeremy L Thompson   @param[in,out] contract CeedTensorContract to increment the reference counter
16534359f16Sjeremylt 
16634359f16Sjeremylt   @return An error code: 0 - success, otherwise - failure
16734359f16Sjeremylt 
16834359f16Sjeremylt   @ref Backend
16934359f16Sjeremylt **/
1709560d06aSjeremylt int CeedTensorContractReference(CeedTensorContract contract) {
17134359f16Sjeremylt   contract->ref_count++;
17234359f16Sjeremylt   return CEED_ERROR_SUCCESS;
17334359f16Sjeremylt }
17434359f16Sjeremylt 
17534359f16Sjeremylt /**
1762f86a920SJeremy L Thompson   @brief Destroy a CeedTensorContract
1772f86a920SJeremy L Thompson 
178ea61e9acSJeremy L Thompson   @param[in,out] contract CeedTensorContract to destroy
1792f86a920SJeremy L Thompson 
1802f86a920SJeremy L Thompson   @return An error code: 0 - success, otherwise - failure
1812f86a920SJeremy L Thompson 
1827a982d89SJeremy L. Thompson   @ref Backend
1832f86a920SJeremy L Thompson **/
1842f86a920SJeremy L Thompson int CeedTensorContractDestroy(CeedTensorContract *contract) {
185ad6481ceSJeremy L Thompson   if (!*contract || --(*contract)->ref_count > 0) {
186ad6481ceSJeremy L Thompson     *contract = NULL;
187ad6481ceSJeremy L Thompson     return CEED_ERROR_SUCCESS;
188ad6481ceSJeremy L Thompson   }
1892f86a920SJeremy L Thompson   if ((*contract)->Destroy) {
1902b730f8bSJeremy L Thompson     CeedCall((*contract)->Destroy(*contract));
1912f86a920SJeremy L Thompson   }
1922b730f8bSJeremy L Thompson   CeedCall(CeedDestroy(&(*contract)->ceed));
1932b730f8bSJeremy L Thompson   CeedCall(CeedFree(contract));
194e15f9bd0SJeremy L Thompson   return CEED_ERROR_SUCCESS;
1957a982d89SJeremy L. Thompson }
1962f86a920SJeremy L Thompson 
1972f86a920SJeremy L Thompson /// @}
198