1*3d8e8822SJeremy L Thompson // Copyright (c) 2017-2022, Lawrence Livermore National Security, LLC and other CEED contributors. 2*3d8e8822SJeremy L Thompson // All Rights Reserved. See the top-level LICENSE and NOTICE files for details. 32f86a920SJeremy L Thompson // 4*3d8e8822SJeremy L Thompson // SPDX-License-Identifier: BSD-2-Clause 52f86a920SJeremy L Thompson // 6*3d8e8822SJeremy L Thompson // This file is part of CEED: http://github.com/ceed 72f86a920SJeremy L Thompson 8ec3da8bcSJed Brown #include <ceed/ceed.h> 9ec3da8bcSJed Brown #include <ceed/backend.h> 103d576824SJeremy L Thompson #include <ceed-impl.h> 112f86a920SJeremy L Thompson 122f86a920SJeremy L Thompson /// @file 137a982d89SJeremy L. Thompson /// Implementation of CeedTensorContract interfaces 147a982d89SJeremy L. Thompson 157a982d89SJeremy L. Thompson /// ---------------------------------------------------------------------------- 167a982d89SJeremy L. Thompson /// CeedTensorContract Backend API 177a982d89SJeremy L. Thompson /// ---------------------------------------------------------------------------- 187a982d89SJeremy L. Thompson /// @addtogroup CeedBasisBackend 192f86a920SJeremy L Thompson /// @{ 202f86a920SJeremy L Thompson 212f86a920SJeremy L Thompson /** 222f86a920SJeremy L Thompson @brief Create a CeedTensorContract object for a CeedBasis 232f86a920SJeremy L Thompson 242f86a920SJeremy L Thompson @param ceed A Ceed object where the CeedTensorContract will be created 25288c0443SJeremy L Thompson @param basis CeedBasis for which the tensor contraction will be used 26288c0443SJeremy L Thompson @param[out] contract Address of the variable where the newly created 272f86a920SJeremy L Thompson 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 **/ 33c71e1dcdSjeremylt int CeedTensorContractCreate(Ceed ceed, CeedBasis basis, 34c71e1dcdSjeremylt CeedTensorContract *contract) { 352f86a920SJeremy L Thompson int ierr; 362f86a920SJeremy L Thompson 372f86a920SJeremy L Thompson if (!ceed->TensorContractCreate) { 382f86a920SJeremy L Thompson Ceed delegate; 39aefd8378Sjeremylt ierr = CeedGetObjectDelegate(ceed, &delegate, "TensorContract"); 40aefd8378Sjeremylt CeedChk(ierr); 412f86a920SJeremy L Thompson 422f86a920SJeremy L Thompson if (!delegate) 43c042f62fSJeremy L Thompson // LCOV_EXCL_START 44e15f9bd0SJeremy L Thompson return CeedError(ceed, CEED_ERROR_UNSUPPORTED, 45e15f9bd0SJeremy L Thompson "Backend does not support TensorContractCreate"); 46c042f62fSJeremy L Thompson // LCOV_EXCL_STOP 472f86a920SJeremy L Thompson 48c71e1dcdSjeremylt ierr = CeedTensorContractCreate(delegate, basis, contract); 492f86a920SJeremy L Thompson CeedChk(ierr); 50e15f9bd0SJeremy L Thompson return CEED_ERROR_SUCCESS; 512f86a920SJeremy L Thompson } 522f86a920SJeremy L Thompson 532f86a920SJeremy L Thompson ierr = CeedCalloc(1, contract); CeedChk(ierr); 542f86a920SJeremy L Thompson 552f86a920SJeremy L Thompson (*contract)->ceed = ceed; 569560d06aSjeremylt ierr = CeedReference(ceed); CeedChk(ierr); 57c71e1dcdSjeremylt ierr = ceed->TensorContractCreate(basis, *contract); 582f86a920SJeremy L Thompson CeedChk(ierr); 59e15f9bd0SJeremy L Thompson return CEED_ERROR_SUCCESS; 607a982d89SJeremy L. Thompson } 612f86a920SJeremy L Thompson 622f86a920SJeremy L Thompson /** 632f86a920SJeremy L Thompson @brief Apply tensor contraction 642f86a920SJeremy L Thompson 652f86a920SJeremy L Thompson Contracts on the middle index 662f86a920SJeremy L Thompson NOTRANSPOSE: v_ajc = t_jb u_abc 672f86a920SJeremy L Thompson TRANSPOSE: v_ajc = t_bj u_abc 682f86a920SJeremy L Thompson If add != 0, "=" is replaced by "+=" 692f86a920SJeremy L Thompson 702f86a920SJeremy L Thompson @param contract CeedTensorContract to use 712f86a920SJeremy L Thompson @param A First index of u, v 722f86a920SJeremy L Thompson @param B Middle index of u, one index of t 732f86a920SJeremy L Thompson @param C Last index of u, v 742f86a920SJeremy L Thompson @param J Middle index of v, one index of t 752f86a920SJeremy L Thompson @param[in] t Tensor array to contract against 76d1d35e2fSjeremylt @param t_mode Transpose mode for t, \ref CEED_NOTRANSPOSE for t_jb 772f86a920SJeremy L Thompson \ref CEED_TRANSPOSE for t_bj 782f86a920SJeremy L Thompson @param add Add mode 792f86a920SJeremy L Thompson @param[in] u Input array 802f86a920SJeremy L Thompson @param[out] v Output array 812f86a920SJeremy L Thompson 822f86a920SJeremy L Thompson @return An error code: 0 - success, otherwise - failure 832f86a920SJeremy L Thompson 847a982d89SJeremy L. Thompson @ref Backend 852f86a920SJeremy L Thompson **/ 862f86a920SJeremy L Thompson int CeedTensorContractApply(CeedTensorContract contract, CeedInt A, CeedInt B, 872f86a920SJeremy L Thompson CeedInt C, CeedInt J, const CeedScalar *restrict t, 88d1d35e2fSjeremylt CeedTransposeMode t_mode, const CeedInt add, 892f86a920SJeremy L Thompson const CeedScalar *restrict u, 902f86a920SJeremy L Thompson CeedScalar *restrict v) { 912f86a920SJeremy L Thompson int ierr; 922f86a920SJeremy L Thompson 93d1d35e2fSjeremylt ierr = contract->Apply(contract, A, B, C, J, t, t_mode, add, u, v); 942f86a920SJeremy L Thompson CeedChk(ierr); 95e15f9bd0SJeremy L Thompson return CEED_ERROR_SUCCESS; 967a982d89SJeremy L. Thompson } 972f86a920SJeremy L Thompson 982f86a920SJeremy L Thompson /** 992f86a920SJeremy L Thompson @brief Get Ceed associated with a CeedTensorContract 1002f86a920SJeremy L Thompson 1012f86a920SJeremy L Thompson @param contract CeedTensorContract 1022f86a920SJeremy L Thompson @param[out] ceed Variable to store Ceed 1032f86a920SJeremy L Thompson 1042f86a920SJeremy L Thompson @return An error code: 0 - success, otherwise - failure 1052f86a920SJeremy L Thompson 1067a982d89SJeremy L. Thompson @ref Backend 1072f86a920SJeremy L Thompson **/ 1082f86a920SJeremy L Thompson int CeedTensorContractGetCeed(CeedTensorContract contract, Ceed *ceed) { 1092f86a920SJeremy L Thompson *ceed = contract->ceed; 110e15f9bd0SJeremy L Thompson return CEED_ERROR_SUCCESS; 1117a982d89SJeremy L. Thompson } 1122f86a920SJeremy L Thompson 1132f86a920SJeremy L Thompson /** 1142f86a920SJeremy L Thompson @brief Get backend data of a CeedTensorContract 1152f86a920SJeremy L Thompson 1162f86a920SJeremy L Thompson @param contract CeedTensorContract 1172f86a920SJeremy L Thompson @param[out] data Variable to store data 1182f86a920SJeremy L Thompson 1192f86a920SJeremy L Thompson @return An error code: 0 - success, otherwise - failure 1202f86a920SJeremy L Thompson 1217a982d89SJeremy L. Thompson @ref Backend 1222f86a920SJeremy L Thompson **/ 123777ff853SJeremy L Thompson int CeedTensorContractGetData(CeedTensorContract contract, void *data) { 124777ff853SJeremy L Thompson *(void **)data = contract->data; 125e15f9bd0SJeremy L Thompson return CEED_ERROR_SUCCESS; 1262f86a920SJeremy L Thompson } 1272f86a920SJeremy L Thompson 1282f86a920SJeremy L Thompson /** 1292f86a920SJeremy L Thompson @brief Set backend data of a CeedTensorContract 1302f86a920SJeremy L Thompson 1312f86a920SJeremy L Thompson @param[out] contract CeedTensorContract 1322f86a920SJeremy L Thompson @param data Data to set 1332f86a920SJeremy L Thompson 1342f86a920SJeremy L Thompson @return An error code: 0 - success, otherwise - failure 1352f86a920SJeremy L Thompson 1367a982d89SJeremy L. Thompson @ref Backend 1372f86a920SJeremy L Thompson **/ 138777ff853SJeremy L Thompson int CeedTensorContractSetData(CeedTensorContract contract, void *data) { 139777ff853SJeremy L Thompson contract->data = data; 140e15f9bd0SJeremy L Thompson return CEED_ERROR_SUCCESS; 1412f86a920SJeremy L Thompson } 1422f86a920SJeremy L Thompson 1432f86a920SJeremy L Thompson /** 14434359f16Sjeremylt @brief Increment the reference counter for a CeedTensorContract 14534359f16Sjeremylt 14634359f16Sjeremylt @param contract CeedTensorContract to increment the reference counter 14734359f16Sjeremylt 14834359f16Sjeremylt @return An error code: 0 - success, otherwise - failure 14934359f16Sjeremylt 15034359f16Sjeremylt @ref Backend 15134359f16Sjeremylt **/ 1529560d06aSjeremylt int CeedTensorContractReference(CeedTensorContract contract) { 15334359f16Sjeremylt contract->ref_count++; 15434359f16Sjeremylt return CEED_ERROR_SUCCESS; 15534359f16Sjeremylt } 15634359f16Sjeremylt 15734359f16Sjeremylt /** 1582f86a920SJeremy L Thompson @brief Destroy a CeedTensorContract 1592f86a920SJeremy L Thompson 1602f86a920SJeremy L Thompson @param contract CeedTensorContract to destroy 1612f86a920SJeremy L Thompson 1622f86a920SJeremy L Thompson @return An error code: 0 - success, otherwise - failure 1632f86a920SJeremy L Thompson 1647a982d89SJeremy L. Thompson @ref Backend 1652f86a920SJeremy L Thompson **/ 1662f86a920SJeremy L Thompson int CeedTensorContractDestroy(CeedTensorContract *contract) { 1672f86a920SJeremy L Thompson int ierr; 1682f86a920SJeremy L Thompson 169d1d35e2fSjeremylt if (!*contract || --(*contract)->ref_count > 0) return CEED_ERROR_SUCCESS; 1702f86a920SJeremy L Thompson if ((*contract)->Destroy) { 1712f86a920SJeremy L Thompson ierr = (*contract)->Destroy(*contract); CeedChk(ierr); 1722f86a920SJeremy L Thompson } 1732f86a920SJeremy L Thompson ierr = CeedDestroy(&(*contract)->ceed); CeedChk(ierr); 1742f86a920SJeremy L Thompson ierr = CeedFree(contract); CeedChk(ierr); 175e15f9bd0SJeremy L Thompson return CEED_ERROR_SUCCESS; 1767a982d89SJeremy L. Thompson } 1772f86a920SJeremy L Thompson 1782f86a920SJeremy L Thompson /// @} 179