xref: /libCEED/rust/libceed-sys/c-src/interface/ceed-tensor.c (revision 9560d06a92c065fb7d600a8c20ade8d9a4cda324)
12f86a920SJeremy L Thompson // Copyright (c) 2017, Lawrence Livermore National Security, LLC. Produced at
22f86a920SJeremy L Thompson // the Lawrence Livermore National Laboratory. LLNL-CODE-734707. All Rights
32f86a920SJeremy L Thompson // reserved. See files LICENSE and NOTICE for details.
42f86a920SJeremy L Thompson //
52f86a920SJeremy L Thompson // This file is part of CEED, a collection of benchmarks, miniapps, software
62f86a920SJeremy L Thompson // libraries and APIs for efficient high-order finite element and spectral
72f86a920SJeremy L Thompson // element discretizations for exascale applications. For more information and
82f86a920SJeremy L Thompson // source code availability see http://github.com/ceed.
92f86a920SJeremy L Thompson //
102f86a920SJeremy L Thompson // The CEED research is supported by the Exascale Computing Project 17-SC-20-SC,
112f86a920SJeremy L Thompson // a collaborative effort of two U.S. Department of Energy organizations (Office
122f86a920SJeremy L Thompson // of Science and the National Nuclear Security Administration) responsible for
132f86a920SJeremy L Thompson // the planning and preparation of a capable exascale ecosystem, including
142f86a920SJeremy L Thompson // software, applications, hardware, advanced system engineering and early
152f86a920SJeremy L Thompson // testbed platforms, in support of the nation's exascale computing imperative.
162f86a920SJeremy L Thompson 
17ec3da8bcSJed Brown #include <ceed/ceed.h>
18ec3da8bcSJed Brown #include <ceed/backend.h>
193d576824SJeremy L Thompson #include <ceed-impl.h>
202f86a920SJeremy L Thompson 
212f86a920SJeremy L Thompson /// @file
227a982d89SJeremy L. Thompson /// Implementation of CeedTensorContract interfaces
237a982d89SJeremy L. Thompson 
247a982d89SJeremy L. Thompson /// ----------------------------------------------------------------------------
257a982d89SJeremy L. Thompson /// CeedTensorContract Backend API
267a982d89SJeremy L. Thompson /// ----------------------------------------------------------------------------
277a982d89SJeremy L. Thompson /// @addtogroup CeedBasisBackend
282f86a920SJeremy L Thompson /// @{
292f86a920SJeremy L Thompson 
302f86a920SJeremy L Thompson /**
312f86a920SJeremy L Thompson   @brief Create a CeedTensorContract object for a CeedBasis
322f86a920SJeremy L Thompson 
332f86a920SJeremy L Thompson   @param ceed           A Ceed object where the CeedTensorContract will be created
34288c0443SJeremy L Thompson   @param basis          CeedBasis for which the tensor contraction will be used
35288c0443SJeremy L Thompson   @param[out] contract  Address of the variable where the newly created
362f86a920SJeremy L Thompson                           CeedTensorContract will be stored.
372f86a920SJeremy L Thompson 
382f86a920SJeremy L Thompson   @return An error code: 0 - success, otherwise - failure
392f86a920SJeremy L Thompson 
407a982d89SJeremy L. Thompson   @ref Backend
412f86a920SJeremy L Thompson **/
42c71e1dcdSjeremylt int CeedTensorContractCreate(Ceed ceed, CeedBasis basis,
43c71e1dcdSjeremylt                              CeedTensorContract *contract) {
442f86a920SJeremy L Thompson   int ierr;
452f86a920SJeremy L Thompson 
462f86a920SJeremy L Thompson   if (!ceed->TensorContractCreate) {
472f86a920SJeremy L Thompson     Ceed delegate;
48aefd8378Sjeremylt     ierr = CeedGetObjectDelegate(ceed, &delegate, "TensorContract");
49aefd8378Sjeremylt     CeedChk(ierr);
502f86a920SJeremy L Thompson 
512f86a920SJeremy L Thompson     if (!delegate)
52c042f62fSJeremy L Thompson       // LCOV_EXCL_START
53e15f9bd0SJeremy L Thompson       return CeedError(ceed, CEED_ERROR_UNSUPPORTED,
54e15f9bd0SJeremy L Thompson                        "Backend does not support TensorContractCreate");
55c042f62fSJeremy L Thompson     // LCOV_EXCL_STOP
562f86a920SJeremy L Thompson 
57c71e1dcdSjeremylt     ierr = CeedTensorContractCreate(delegate, basis, contract);
582f86a920SJeremy L Thompson     CeedChk(ierr);
59e15f9bd0SJeremy L Thompson     return CEED_ERROR_SUCCESS;
602f86a920SJeremy L Thompson   }
612f86a920SJeremy L Thompson 
622f86a920SJeremy L Thompson   ierr = CeedCalloc(1, contract); CeedChk(ierr);
632f86a920SJeremy L Thompson 
642f86a920SJeremy L Thompson   (*contract)->ceed = ceed;
65*9560d06aSjeremylt   ierr = CeedReference(ceed); CeedChk(ierr);
66c71e1dcdSjeremylt   ierr = ceed->TensorContractCreate(basis, *contract);
672f86a920SJeremy L Thompson   CeedChk(ierr);
68e15f9bd0SJeremy L Thompson   return CEED_ERROR_SUCCESS;
697a982d89SJeremy L. Thompson }
702f86a920SJeremy L Thompson 
712f86a920SJeremy L Thompson /**
722f86a920SJeremy L Thompson   @brief Apply tensor contraction
732f86a920SJeremy L Thompson 
742f86a920SJeremy L Thompson     Contracts on the middle index
752f86a920SJeremy L Thompson     NOTRANSPOSE: v_ajc = t_jb u_abc
762f86a920SJeremy L Thompson     TRANSPOSE:   v_ajc = t_bj u_abc
772f86a920SJeremy L Thompson     If add != 0, "=" is replaced by "+="
782f86a920SJeremy L Thompson 
792f86a920SJeremy L Thompson   @param contract  CeedTensorContract to use
802f86a920SJeremy L Thompson   @param A         First index of u, v
812f86a920SJeremy L Thompson   @param B         Middle index of u, one index of t
822f86a920SJeremy L Thompson   @param C         Last index of u, v
832f86a920SJeremy L Thompson   @param J         Middle index of v, one index of t
842f86a920SJeremy L Thompson   @param[in] t     Tensor array to contract against
85d1d35e2fSjeremylt   @param t_mode    Transpose mode for t, \ref CEED_NOTRANSPOSE for t_jb
862f86a920SJeremy L Thompson                      \ref CEED_TRANSPOSE for t_bj
872f86a920SJeremy L Thompson   @param add       Add mode
882f86a920SJeremy L Thompson   @param[in] u     Input array
892f86a920SJeremy L Thompson   @param[out] v    Output array
902f86a920SJeremy L Thompson 
912f86a920SJeremy L Thompson   @return An error code: 0 - success, otherwise - failure
922f86a920SJeremy L Thompson 
937a982d89SJeremy L. Thompson   @ref Backend
942f86a920SJeremy L Thompson **/
952f86a920SJeremy L Thompson int CeedTensorContractApply(CeedTensorContract contract, CeedInt A, CeedInt B,
962f86a920SJeremy L Thompson                             CeedInt C, CeedInt J, const CeedScalar *restrict t,
97d1d35e2fSjeremylt                             CeedTransposeMode t_mode, const CeedInt add,
982f86a920SJeremy L Thompson                             const CeedScalar *restrict u,
992f86a920SJeremy L Thompson                             CeedScalar *restrict v) {
1002f86a920SJeremy L Thompson   int ierr;
1012f86a920SJeremy L Thompson 
102d1d35e2fSjeremylt   ierr = contract->Apply(contract, A, B, C, J, t, t_mode, add,  u, v);
1032f86a920SJeremy L Thompson   CeedChk(ierr);
104e15f9bd0SJeremy L Thompson   return CEED_ERROR_SUCCESS;
1057a982d89SJeremy L. Thompson }
1062f86a920SJeremy L Thompson 
1072f86a920SJeremy L Thompson /**
1082f86a920SJeremy L Thompson   @brief Get Ceed associated with a CeedTensorContract
1092f86a920SJeremy L Thompson 
1102f86a920SJeremy L Thompson   @param contract   CeedTensorContract
1112f86a920SJeremy L Thompson   @param[out] ceed  Variable to store Ceed
1122f86a920SJeremy L Thompson 
1132f86a920SJeremy L Thompson   @return An error code: 0 - success, otherwise - failure
1142f86a920SJeremy L Thompson 
1157a982d89SJeremy L. Thompson   @ref Backend
1162f86a920SJeremy L Thompson **/
1172f86a920SJeremy L Thompson int CeedTensorContractGetCeed(CeedTensorContract contract, Ceed *ceed) {
1182f86a920SJeremy L Thompson   *ceed = contract->ceed;
119e15f9bd0SJeremy L Thompson   return CEED_ERROR_SUCCESS;
1207a982d89SJeremy L. Thompson }
1212f86a920SJeremy L Thompson 
1222f86a920SJeremy L Thompson /**
1232f86a920SJeremy L Thompson   @brief Get backend data of a CeedTensorContract
1242f86a920SJeremy L Thompson 
1252f86a920SJeremy L Thompson   @param contract   CeedTensorContract
1262f86a920SJeremy L Thompson   @param[out] data  Variable to store data
1272f86a920SJeremy L Thompson 
1282f86a920SJeremy L Thompson   @return An error code: 0 - success, otherwise - failure
1292f86a920SJeremy L Thompson 
1307a982d89SJeremy L. Thompson   @ref Backend
1312f86a920SJeremy L Thompson **/
132777ff853SJeremy L Thompson int CeedTensorContractGetData(CeedTensorContract contract, void *data) {
133777ff853SJeremy L Thompson   *(void **)data = contract->data;
134e15f9bd0SJeremy L Thompson   return CEED_ERROR_SUCCESS;
1352f86a920SJeremy L Thompson }
1362f86a920SJeremy L Thompson 
1372f86a920SJeremy L Thompson /**
1382f86a920SJeremy L Thompson   @brief Set backend data of a CeedTensorContract
1392f86a920SJeremy L Thompson 
1402f86a920SJeremy L Thompson   @param[out] contract  CeedTensorContract
1412f86a920SJeremy L Thompson   @param data           Data to set
1422f86a920SJeremy L Thompson 
1432f86a920SJeremy L Thompson   @return An error code: 0 - success, otherwise - failure
1442f86a920SJeremy L Thompson 
1457a982d89SJeremy L. Thompson   @ref Backend
1462f86a920SJeremy L Thompson **/
147777ff853SJeremy L Thompson int CeedTensorContractSetData(CeedTensorContract contract, void *data) {
148777ff853SJeremy L Thompson   contract->data = data;
149e15f9bd0SJeremy L Thompson   return CEED_ERROR_SUCCESS;
1502f86a920SJeremy L Thompson }
1512f86a920SJeremy L Thompson 
1522f86a920SJeremy L Thompson /**
15334359f16Sjeremylt   @brief Increment the reference counter for a CeedTensorContract
15434359f16Sjeremylt 
15534359f16Sjeremylt   @param contract  CeedTensorContract to increment the reference counter
15634359f16Sjeremylt 
15734359f16Sjeremylt   @return An error code: 0 - success, otherwise - failure
15834359f16Sjeremylt 
15934359f16Sjeremylt   @ref Backend
16034359f16Sjeremylt **/
161*9560d06aSjeremylt int CeedTensorContractReference(CeedTensorContract contract) {
16234359f16Sjeremylt   contract->ref_count++;
16334359f16Sjeremylt   return CEED_ERROR_SUCCESS;
16434359f16Sjeremylt }
16534359f16Sjeremylt 
16634359f16Sjeremylt /**
1672f86a920SJeremy L Thompson   @brief Destroy a CeedTensorContract
1682f86a920SJeremy L Thompson 
1692f86a920SJeremy L Thompson   @param contract  CeedTensorContract to destroy
1702f86a920SJeremy L Thompson 
1712f86a920SJeremy L Thompson   @return An error code: 0 - success, otherwise - failure
1722f86a920SJeremy L Thompson 
1737a982d89SJeremy L. Thompson   @ref Backend
1742f86a920SJeremy L Thompson **/
1752f86a920SJeremy L Thompson int CeedTensorContractDestroy(CeedTensorContract *contract) {
1762f86a920SJeremy L Thompson   int ierr;
1772f86a920SJeremy L Thompson 
178d1d35e2fSjeremylt   if (!*contract || --(*contract)->ref_count > 0) return CEED_ERROR_SUCCESS;
1792f86a920SJeremy L Thompson   if ((*contract)->Destroy) {
1802f86a920SJeremy L Thompson     ierr = (*contract)->Destroy(*contract); CeedChk(ierr);
1812f86a920SJeremy L Thompson   }
1822f86a920SJeremy L Thompson   ierr = CeedDestroy(&(*contract)->ceed); CeedChk(ierr);
1832f86a920SJeremy L Thompson   ierr = CeedFree(contract); CeedChk(ierr);
184e15f9bd0SJeremy L Thompson   return CEED_ERROR_SUCCESS;
1857a982d89SJeremy L. Thompson }
1862f86a920SJeremy L Thompson 
1872f86a920SJeremy L Thompson /// @}
188