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> 92b730f8bSJeremy L Thompson #include <ceed/backend.h> 102b730f8bSJeremy L Thompson #include <ceed/ceed.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 24ea61e9acSJeremy L Thompson @param[in] ceed Ceed object where the CeedTensorContract will be created 25ea61e9acSJeremy L Thompson @param[in] basis CeedBasis for which the tensor contraction will be used 26ea61e9acSJeremy L Thompson @param[out] contract Address of the variable where the newly created CeedTensorContract will be stored. 272f86a920SJeremy L Thompson 282f86a920SJeremy L Thompson @return An error code: 0 - success, otherwise - failure 292f86a920SJeremy L Thompson 307a982d89SJeremy L. Thompson @ref Backend 312f86a920SJeremy L Thompson **/ 322b730f8bSJeremy L Thompson int CeedTensorContractCreate(Ceed ceed, CeedBasis basis, CeedTensorContract *contract) { 332f86a920SJeremy L Thompson if (!ceed->TensorContractCreate) { 342f86a920SJeremy L Thompson Ceed delegate; 352b730f8bSJeremy L Thompson CeedCall(CeedGetObjectDelegate(ceed, &delegate, "TensorContract")); 362f86a920SJeremy L Thompson 372b730f8bSJeremy L Thompson if (!delegate) { 38c042f62fSJeremy L Thompson // LCOV_EXCL_START 392b730f8bSJeremy L Thompson return CeedError(ceed, CEED_ERROR_UNSUPPORTED, "Backend does not support TensorContractCreate"); 40c042f62fSJeremy L Thompson // LCOV_EXCL_STOP 412b730f8bSJeremy L Thompson } 422f86a920SJeremy L Thompson 432b730f8bSJeremy L Thompson CeedCall(CeedTensorContractCreate(delegate, basis, contract)); 44e15f9bd0SJeremy L Thompson return CEED_ERROR_SUCCESS; 452f86a920SJeremy L Thompson } 462f86a920SJeremy L Thompson 472b730f8bSJeremy L Thompson CeedCall(CeedCalloc(1, contract)); 482f86a920SJeremy L Thompson 492f86a920SJeremy L Thompson (*contract)->ceed = ceed; 502b730f8bSJeremy L Thompson CeedCall(CeedReference(ceed)); 512b730f8bSJeremy L Thompson CeedCall(ceed->TensorContractCreate(basis, *contract)); 52e15f9bd0SJeremy L Thompson return CEED_ERROR_SUCCESS; 537a982d89SJeremy L. Thompson } 542f86a920SJeremy L Thompson 552f86a920SJeremy L Thompson /** 562f86a920SJeremy L Thompson @brief Apply tensor contraction 572f86a920SJeremy L Thompson 582f86a920SJeremy L Thompson Contracts on the middle index 592f86a920SJeremy L Thompson NOTRANSPOSE: v_ajc = t_jb u_abc 602f86a920SJeremy L Thompson TRANSPOSE: v_ajc = t_bj u_abc 612f86a920SJeremy L Thompson If add != 0, "=" is replaced by "+=" 622f86a920SJeremy L Thompson 63ea61e9acSJeremy L Thompson @param[in] contract CeedTensorContract to use 64ea61e9acSJeremy L Thompson @param[in] A First index of u, v 65ea61e9acSJeremy L Thompson @param[in] B Middle index of u, one index of t 66ea61e9acSJeremy L Thompson @param[in] C Last index of u, v 67ea61e9acSJeremy L Thompson @param[in] J Middle index of v, one index of t 682f86a920SJeremy L Thompson @param[in] t Tensor array to contract against 69ea61e9acSJeremy L Thompson @param[in] t_mode Transpose mode for t, \ref CEED_NOTRANSPOSE for t_jb \ref CEED_TRANSPOSE for t_bj 70ea61e9acSJeremy L Thompson @param[in] add Add mode 712f86a920SJeremy L Thompson @param[in] u Input array 722f86a920SJeremy L Thompson @param[out] v Output array 732f86a920SJeremy L Thompson 742f86a920SJeremy L Thompson @return An error code: 0 - success, otherwise - failure 752f86a920SJeremy L Thompson 767a982d89SJeremy L. Thompson @ref Backend 772f86a920SJeremy L Thompson **/ 782b730f8bSJeremy L Thompson int CeedTensorContractApply(CeedTensorContract contract, CeedInt A, CeedInt B, CeedInt C, CeedInt J, const CeedScalar *restrict t, 792b730f8bSJeremy L Thompson CeedTransposeMode t_mode, const CeedInt add, const CeedScalar *restrict u, CeedScalar *restrict v) { 802b730f8bSJeremy L Thompson CeedCall(contract->Apply(contract, A, B, C, J, t, t_mode, add, u, v)); 81e15f9bd0SJeremy L Thompson return CEED_ERROR_SUCCESS; 827a982d89SJeremy L. Thompson } 832f86a920SJeremy L Thompson 842f86a920SJeremy L Thompson /** 852f86a920SJeremy L Thompson @brief Get Ceed associated with a CeedTensorContract 862f86a920SJeremy L Thompson 87ea61e9acSJeremy L Thompson @param[in] contract CeedTensorContract 882f86a920SJeremy L Thompson @param[out] ceed Variable to store Ceed 892f86a920SJeremy L Thompson 902f86a920SJeremy L Thompson @return An error code: 0 - success, otherwise - failure 912f86a920SJeremy L Thompson 927a982d89SJeremy L. Thompson @ref Backend 932f86a920SJeremy L Thompson **/ 942f86a920SJeremy L Thompson int CeedTensorContractGetCeed(CeedTensorContract contract, Ceed *ceed) { 952f86a920SJeremy L Thompson *ceed = contract->ceed; 96e15f9bd0SJeremy L Thompson return CEED_ERROR_SUCCESS; 977a982d89SJeremy L. Thompson } 982f86a920SJeremy L Thompson 992f86a920SJeremy L Thompson /** 1002f86a920SJeremy L Thompson @brief Get backend data of a CeedTensorContract 1012f86a920SJeremy L Thompson 102ea61e9acSJeremy L Thompson @param[in] contract CeedTensorContract 1032f86a920SJeremy L Thompson @param[out] data Variable to store data 1042f86a920SJeremy L Thompson 1052f86a920SJeremy L Thompson @return An error code: 0 - success, otherwise - failure 1062f86a920SJeremy L Thompson 1077a982d89SJeremy L. Thompson @ref Backend 1082f86a920SJeremy L Thompson **/ 109777ff853SJeremy L Thompson int CeedTensorContractGetData(CeedTensorContract contract, void *data) { 110777ff853SJeremy L Thompson *(void **)data = contract->data; 111e15f9bd0SJeremy L Thompson return CEED_ERROR_SUCCESS; 1122f86a920SJeremy L Thompson } 1132f86a920SJeremy L Thompson 1142f86a920SJeremy L Thompson /** 1152f86a920SJeremy L Thompson @brief Set backend data of a CeedTensorContract 1162f86a920SJeremy L Thompson 117ea61e9acSJeremy L Thompson @param[in,out] contract CeedTensorContract 118ea61e9acSJeremy L Thompson @param[in] data Data to set 1192f86a920SJeremy L Thompson 1202f86a920SJeremy L Thompson @return An error code: 0 - success, otherwise - failure 1212f86a920SJeremy L Thompson 1227a982d89SJeremy L. Thompson @ref Backend 1232f86a920SJeremy L Thompson **/ 124777ff853SJeremy L Thompson int CeedTensorContractSetData(CeedTensorContract contract, void *data) { 125777ff853SJeremy L Thompson contract->data = data; 126e15f9bd0SJeremy L Thompson return CEED_ERROR_SUCCESS; 1272f86a920SJeremy L Thompson } 1282f86a920SJeremy L Thompson 1292f86a920SJeremy L Thompson /** 13034359f16Sjeremylt @brief Increment the reference counter for a CeedTensorContract 13134359f16Sjeremylt 132ea61e9acSJeremy L Thompson @param[in,out] contract CeedTensorContract to increment the reference counter 13334359f16Sjeremylt 13434359f16Sjeremylt @return An error code: 0 - success, otherwise - failure 13534359f16Sjeremylt 13634359f16Sjeremylt @ref Backend 13734359f16Sjeremylt **/ 1389560d06aSjeremylt int CeedTensorContractReference(CeedTensorContract contract) { 13934359f16Sjeremylt contract->ref_count++; 14034359f16Sjeremylt return CEED_ERROR_SUCCESS; 14134359f16Sjeremylt } 14234359f16Sjeremylt 14334359f16Sjeremylt /** 1442f86a920SJeremy L Thompson @brief Destroy a CeedTensorContract 1452f86a920SJeremy L Thompson 146ea61e9acSJeremy L Thompson @param[in,out] contract CeedTensorContract to destroy 1472f86a920SJeremy L Thompson 1482f86a920SJeremy L Thompson @return An error code: 0 - success, otherwise - failure 1492f86a920SJeremy L Thompson 1507a982d89SJeremy L. Thompson @ref Backend 1512f86a920SJeremy L Thompson **/ 1522f86a920SJeremy L Thompson int CeedTensorContractDestroy(CeedTensorContract *contract) { 153*ad6481ceSJeremy L Thompson if (!*contract || --(*contract)->ref_count > 0) { 154*ad6481ceSJeremy L Thompson *contract = NULL; 155*ad6481ceSJeremy L Thompson return CEED_ERROR_SUCCESS; 156*ad6481ceSJeremy L Thompson } 1572f86a920SJeremy L Thompson if ((*contract)->Destroy) { 1582b730f8bSJeremy L Thompson CeedCall((*contract)->Destroy(*contract)); 1592f86a920SJeremy L Thompson } 1602b730f8bSJeremy L Thompson CeedCall(CeedDestroy(&(*contract)->ceed)); 1612b730f8bSJeremy L Thompson CeedCall(CeedFree(contract)); 162e15f9bd0SJeremy L Thompson return CEED_ERROR_SUCCESS; 1637a982d89SJeremy L. Thompson } 1642f86a920SJeremy L Thompson 1652f86a920SJeremy L Thompson /// @} 166