xref: /libCEED/interface/ceed-tensor.c (revision 288c044332e33f37503f09b6484fec9d0a55fba1)
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 
172f86a920SJeremy L Thompson #include <ceed-impl.h>
182f86a920SJeremy L Thompson #include <ceed-backend.h>
192f86a920SJeremy L Thompson 
202f86a920SJeremy L Thompson /// @file
212f86a920SJeremy L Thompson /// Implementation of backend CeedTensorContract interfaces
222f86a920SJeremy L Thompson ///
232f86a920SJeremy L Thompson /// @addtogroup CeedBasis
242f86a920SJeremy L Thompson /// @{
252f86a920SJeremy L Thompson 
262f86a920SJeremy L Thompson /**
272f86a920SJeremy L Thompson   @brief Create a CeedTensorContract object for a CeedBasis
282f86a920SJeremy L Thompson 
292f86a920SJeremy L Thompson   @param ceed          A Ceed object where the CeedTensorContract will be created
30*288c0443SJeremy L Thompson   @param basis         CeedBasis for which the tensor contraction will be used
31*288c0443SJeremy L Thompson   @param[out] contract Address of the variable where the newly created
322f86a920SJeremy L Thompson                          CeedTensorContract will be stored.
332f86a920SJeremy L Thompson 
342f86a920SJeremy L Thompson   @return An error code: 0 - success, otherwise - failure
352f86a920SJeremy L Thompson 
362f86a920SJeremy L Thompson   @ref Advanced
372f86a920SJeremy L Thompson **/
38c71e1dcdSjeremylt int CeedTensorContractCreate(Ceed ceed, CeedBasis basis,
39c71e1dcdSjeremylt                              CeedTensorContract *contract) {
402f86a920SJeremy L Thompson   int ierr;
412f86a920SJeremy L Thompson 
422f86a920SJeremy L Thompson   if (!ceed->TensorContractCreate) {
432f86a920SJeremy L Thompson     Ceed delegate;
44aefd8378Sjeremylt     ierr = CeedGetObjectDelegate(ceed, &delegate, "TensorContract");
45aefd8378Sjeremylt     CeedChk(ierr);
462f86a920SJeremy L Thompson 
472f86a920SJeremy L Thompson     if (!delegate)
482f86a920SJeremy L Thompson       return CeedError(ceed, 1,
492f86a920SJeremy L Thompson                        "Backend does not support TensorContractCreate");
502f86a920SJeremy L Thompson 
51c71e1dcdSjeremylt     ierr = CeedTensorContractCreate(delegate, basis, contract);
522f86a920SJeremy L Thompson     CeedChk(ierr);
532f86a920SJeremy L Thompson     return 0;
542f86a920SJeremy L Thompson   }
552f86a920SJeremy L Thompson 
562f86a920SJeremy L Thompson   ierr = CeedCalloc(1,contract); CeedChk(ierr);
572f86a920SJeremy L Thompson 
582f86a920SJeremy L Thompson   (*contract)->ceed = ceed;
592f86a920SJeremy L Thompson   ceed->refcount++;
60c71e1dcdSjeremylt   ierr = ceed->TensorContractCreate(basis, *contract);
612f86a920SJeremy L Thompson   CeedChk(ierr);
622f86a920SJeremy L Thompson   return 0;
632f86a920SJeremy L Thompson };
642f86a920SJeremy L Thompson 
652f86a920SJeremy L Thompson /**
662f86a920SJeremy L Thompson   @brief Apply tensor contraction
672f86a920SJeremy L Thompson 
682f86a920SJeremy L Thompson     Contracts on the middle index
692f86a920SJeremy L Thompson     NOTRANSPOSE: v_ajc = t_jb u_abc
702f86a920SJeremy L Thompson     TRANSPOSE:   v_ajc = t_bj u_abc
712f86a920SJeremy L Thompson     If add != 0, "=" is replaced by "+="
722f86a920SJeremy L Thompson 
732f86a920SJeremy L Thompson   @param contract   CeedTensorContract to use
742f86a920SJeremy L Thompson   @param A          First index of u, v
752f86a920SJeremy L Thompson   @param B          Middle index of u, one index of t
762f86a920SJeremy L Thompson   @param C          Last index of u, v
772f86a920SJeremy L Thompson   @param J          Middle index of v, one index of t
782f86a920SJeremy L Thompson   @param[in] t      Tensor array to contract against
792f86a920SJeremy L Thompson   @param tmode      Transpose mode for t, \ref CEED_NOTRANSPOSE for t_jb
802f86a920SJeremy L Thompson                     \ref CEED_TRANSPOSE for t_bj
812f86a920SJeremy L Thompson   @param add        Add mode
822f86a920SJeremy L Thompson   @param[in] u      Input array
832f86a920SJeremy L Thompson   @param[out] v     Output array
842f86a920SJeremy L Thompson 
852f86a920SJeremy L Thompson   @return An error code: 0 - success, otherwise - failure
862f86a920SJeremy L Thompson 
872f86a920SJeremy L Thompson   @ref Advanced
882f86a920SJeremy L Thompson **/
892f86a920SJeremy L Thompson int CeedTensorContractApply(CeedTensorContract contract, CeedInt A, CeedInt B,
902f86a920SJeremy L Thompson                             CeedInt C, CeedInt J, const CeedScalar *restrict t,
912f86a920SJeremy L Thompson                             CeedTransposeMode tmode, const CeedInt add,
922f86a920SJeremy L Thompson                             const CeedScalar *restrict u,
932f86a920SJeremy L Thompson                             CeedScalar *restrict v) {
942f86a920SJeremy L Thompson   int ierr;
952f86a920SJeremy L Thompson 
962f86a920SJeremy L Thompson   ierr = contract->Apply(contract, A, B, C, J, t, tmode, add,  u, v);
972f86a920SJeremy L Thompson   CeedChk(ierr);
982f86a920SJeremy L Thompson   return 0;
992f86a920SJeremy L Thompson };
1002f86a920SJeremy L Thompson 
1012f86a920SJeremy L Thompson /**
1022f86a920SJeremy L Thompson   @brief Get Ceed associated with a CeedTensorContract
1032f86a920SJeremy L Thompson 
1042f86a920SJeremy L Thompson   @param contract      CeedTensorContract
1052f86a920SJeremy L Thompson   @param[out] ceed  Variable to store Ceed
1062f86a920SJeremy L Thompson 
1072f86a920SJeremy L Thompson   @return An error code: 0 - success, otherwise - failure
1082f86a920SJeremy L Thompson 
1092f86a920SJeremy L Thompson   @ref Advanced
1102f86a920SJeremy L Thompson **/
1112f86a920SJeremy L Thompson int CeedTensorContractGetCeed(CeedTensorContract contract, Ceed *ceed) {
1122f86a920SJeremy L Thompson   *ceed = contract->ceed;
1132f86a920SJeremy L Thompson 
1142f86a920SJeremy L Thompson   return 0;
1152f86a920SJeremy L Thompson };
1162f86a920SJeremy L Thompson 
1172f86a920SJeremy L Thompson /**
1182f86a920SJeremy L Thompson   @brief Get backend data of a CeedTensorContract
1192f86a920SJeremy L Thompson 
1202f86a920SJeremy L Thompson   @param contract   CeedTensorContract
1212f86a920SJeremy L Thompson   @param[out] data  Variable to store data
1222f86a920SJeremy L Thompson 
1232f86a920SJeremy L Thompson   @return An error code: 0 - success, otherwise - failure
1242f86a920SJeremy L Thompson 
1252f86a920SJeremy L Thompson   @ref Advanced
1262f86a920SJeremy L Thompson **/
1272f86a920SJeremy L Thompson int CeedTensorContractGetData(CeedTensorContract contract, void* *data) {
1282f86a920SJeremy L Thompson   *data = contract->data;
1292f86a920SJeremy L Thompson   return 0;
1302f86a920SJeremy L Thompson }
1312f86a920SJeremy L Thompson 
1322f86a920SJeremy L Thompson /**
1332f86a920SJeremy L Thompson   @brief Set backend data of a CeedTensorContract
1342f86a920SJeremy L Thompson 
1352f86a920SJeremy L Thompson   @param[out] contract CeedTensorContract
1362f86a920SJeremy L Thompson   @param data          Data to set
1372f86a920SJeremy L Thompson 
1382f86a920SJeremy L Thompson   @return An error code: 0 - success, otherwise - failure
1392f86a920SJeremy L Thompson 
1402f86a920SJeremy L Thompson   @ref Advanced
1412f86a920SJeremy L Thompson **/
1422f86a920SJeremy L Thompson int CeedTensorContractSetData(CeedTensorContract contract, void* *data) {
1432f86a920SJeremy L Thompson   contract->data = *data;
1442f86a920SJeremy L Thompson   return 0;
1452f86a920SJeremy L Thompson }
1462f86a920SJeremy L Thompson 
1472f86a920SJeremy L Thompson /**
1482f86a920SJeremy L Thompson   @brief Destroy a CeedTensorContract
1492f86a920SJeremy L Thompson 
1502f86a920SJeremy L Thompson   @param contract   CeedTensorContract to destroy
1512f86a920SJeremy L Thompson 
1522f86a920SJeremy L Thompson   @return An error code: 0 - success, otherwise - failure
1532f86a920SJeremy L Thompson 
1542f86a920SJeremy L Thompson   @ref Advanced
1552f86a920SJeremy L Thompson **/
1562f86a920SJeremy L Thompson int CeedTensorContractDestroy(CeedTensorContract *contract) {
1572f86a920SJeremy L Thompson   int ierr;
1582f86a920SJeremy L Thompson 
1592f86a920SJeremy L Thompson   if (!*contract || --(*contract)->refcount > 0) return 0;
1602f86a920SJeremy L Thompson   if ((*contract)->Destroy) {
1612f86a920SJeremy L Thompson     ierr = (*contract)->Destroy(*contract); CeedChk(ierr);
1622f86a920SJeremy L Thompson   }
1632f86a920SJeremy L Thompson   ierr = CeedDestroy(&(*contract)->ceed); CeedChk(ierr);
1642f86a920SJeremy L Thompson   ierr = CeedFree(contract); CeedChk(ierr);
1652f86a920SJeremy L Thompson   return 0;
1662f86a920SJeremy L Thompson };
1672f86a920SJeremy L Thompson 
1682f86a920SJeremy L Thompson /// @}
169