xref: /libCEED/interface/ceed-tensor.c (revision 49aac155e7a09736f56fb3abac0f57dab29f7cbf)
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>
9*49aac155SJeremy L Thompson #include <ceed.h>
102b730f8bSJeremy L Thompson #include <ceed/backend.h>
11*49aac155SJeremy 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;
362b730f8bSJeremy L Thompson     CeedCall(CeedGetObjectDelegate(ceed, &delegate, "TensorContract"));
372f86a920SJeremy L Thompson 
382b730f8bSJeremy L Thompson     if (!delegate) {
39c042f62fSJeremy L Thompson       // LCOV_EXCL_START
402b730f8bSJeremy L Thompson       return CeedError(ceed, CEED_ERROR_UNSUPPORTED, "Backend does not support TensorContractCreate");
41c042f62fSJeremy L Thompson       // LCOV_EXCL_STOP
422b730f8bSJeremy L Thompson     }
432f86a920SJeremy L Thompson 
442b730f8bSJeremy L Thompson     CeedCall(CeedTensorContractCreate(delegate, basis, contract));
45e15f9bd0SJeremy L Thompson     return CEED_ERROR_SUCCESS;
462f86a920SJeremy L Thompson   }
472f86a920SJeremy L Thompson 
482b730f8bSJeremy L Thompson   CeedCall(CeedCalloc(1, contract));
492f86a920SJeremy L Thompson 
502f86a920SJeremy L Thompson   (*contract)->ceed = ceed;
512b730f8bSJeremy L Thompson   CeedCall(CeedReference(ceed));
522b730f8bSJeremy L Thompson   CeedCall(ceed->TensorContractCreate(basis, *contract));
53e15f9bd0SJeremy L Thompson   return CEED_ERROR_SUCCESS;
547a982d89SJeremy L. Thompson }
552f86a920SJeremy L Thompson 
562f86a920SJeremy L Thompson /**
572f86a920SJeremy L Thompson   @brief Apply tensor contraction
582f86a920SJeremy L Thompson 
592f86a920SJeremy L Thompson     Contracts on the middle index
602f86a920SJeremy L Thompson     NOTRANSPOSE: v_ajc = t_jb u_abc
612f86a920SJeremy L Thompson     TRANSPOSE:   v_ajc = t_bj u_abc
622f86a920SJeremy L Thompson     If add != 0, "=" is replaced by "+="
632f86a920SJeremy L Thompson 
64ea61e9acSJeremy L Thompson   @param[in]  contract CeedTensorContract to use
65ea61e9acSJeremy L Thompson   @param[in]  A        First index of u, v
66ea61e9acSJeremy L Thompson   @param[in]  B        Middle index of u, one index of t
67ea61e9acSJeremy L Thompson   @param[in]  C        Last index of u, v
68ea61e9acSJeremy L Thompson   @param[in]  J        Middle index of v, one index of t
692f86a920SJeremy L Thompson   @param[in]  t        Tensor array to contract against
70ea61e9acSJeremy L Thompson   @param[in]  t_mode   Transpose mode for t, \ref CEED_NOTRANSPOSE for t_jb \ref CEED_TRANSPOSE for t_bj
71ea61e9acSJeremy L Thompson   @param[in]  add      Add mode
722f86a920SJeremy L Thompson   @param[in]  u        Input array
732f86a920SJeremy L Thompson   @param[out] v        Output array
742f86a920SJeremy L Thompson 
752f86a920SJeremy L Thompson   @return An error code: 0 - success, otherwise - failure
762f86a920SJeremy L Thompson 
777a982d89SJeremy L. Thompson   @ref Backend
782f86a920SJeremy L Thompson **/
792b730f8bSJeremy L Thompson int CeedTensorContractApply(CeedTensorContract contract, CeedInt A, CeedInt B, CeedInt C, CeedInt J, const CeedScalar *restrict t,
802b730f8bSJeremy L Thompson                             CeedTransposeMode t_mode, const CeedInt add, const CeedScalar *restrict u, CeedScalar *restrict v) {
812b730f8bSJeremy L Thompson   CeedCall(contract->Apply(contract, A, B, C, J, t, t_mode, add, u, v));
82e15f9bd0SJeremy L Thompson   return CEED_ERROR_SUCCESS;
837a982d89SJeremy L. Thompson }
842f86a920SJeremy L Thompson 
852f86a920SJeremy L Thompson /**
862f86a920SJeremy L Thompson   @brief Get Ceed associated with a CeedTensorContract
872f86a920SJeremy L Thompson 
88ea61e9acSJeremy L Thompson   @param[in]  contract CeedTensorContract
892f86a920SJeremy L Thompson   @param[out] ceed     Variable to store Ceed
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 CeedTensorContractGetCeed(CeedTensorContract contract, Ceed *ceed) {
962f86a920SJeremy L Thompson   *ceed = contract->ceed;
97e15f9bd0SJeremy L Thompson   return CEED_ERROR_SUCCESS;
987a982d89SJeremy L. Thompson }
992f86a920SJeremy L Thompson 
1002f86a920SJeremy L Thompson /**
1012f86a920SJeremy L Thompson   @brief Get backend data of a CeedTensorContract
1022f86a920SJeremy L Thompson 
103ea61e9acSJeremy L Thompson   @param[in]  contract CeedTensorContract
1042f86a920SJeremy L Thompson   @param[out] data     Variable to store data
1052f86a920SJeremy L Thompson 
1062f86a920SJeremy L Thompson   @return An error code: 0 - success, otherwise - failure
1072f86a920SJeremy L Thompson 
1087a982d89SJeremy L. Thompson   @ref Backend
1092f86a920SJeremy L Thompson **/
110777ff853SJeremy L Thompson int CeedTensorContractGetData(CeedTensorContract contract, void *data) {
111777ff853SJeremy L Thompson   *(void **)data = contract->data;
112e15f9bd0SJeremy L Thompson   return CEED_ERROR_SUCCESS;
1132f86a920SJeremy L Thompson }
1142f86a920SJeremy L Thompson 
1152f86a920SJeremy L Thompson /**
1162f86a920SJeremy L Thompson   @brief Set backend data of a CeedTensorContract
1172f86a920SJeremy L Thompson 
118ea61e9acSJeremy L Thompson   @param[in,out] contract CeedTensorContract
119ea61e9acSJeremy L Thompson   @param[in]     data     Data to set
1202f86a920SJeremy L Thompson 
1212f86a920SJeremy L Thompson   @return An error code: 0 - success, otherwise - failure
1222f86a920SJeremy L Thompson 
1237a982d89SJeremy L. Thompson   @ref Backend
1242f86a920SJeremy L Thompson **/
125777ff853SJeremy L Thompson int CeedTensorContractSetData(CeedTensorContract contract, void *data) {
126777ff853SJeremy L Thompson   contract->data = data;
127e15f9bd0SJeremy L Thompson   return CEED_ERROR_SUCCESS;
1282f86a920SJeremy L Thompson }
1292f86a920SJeremy L Thompson 
1302f86a920SJeremy L Thompson /**
13134359f16Sjeremylt   @brief Increment the reference counter for a CeedTensorContract
13234359f16Sjeremylt 
133ea61e9acSJeremy L Thompson   @param[in,out] contract CeedTensorContract to increment the reference counter
13434359f16Sjeremylt 
13534359f16Sjeremylt   @return An error code: 0 - success, otherwise - failure
13634359f16Sjeremylt 
13734359f16Sjeremylt   @ref Backend
13834359f16Sjeremylt **/
1399560d06aSjeremylt int CeedTensorContractReference(CeedTensorContract contract) {
14034359f16Sjeremylt   contract->ref_count++;
14134359f16Sjeremylt   return CEED_ERROR_SUCCESS;
14234359f16Sjeremylt }
14334359f16Sjeremylt 
14434359f16Sjeremylt /**
1452f86a920SJeremy L Thompson   @brief Destroy a CeedTensorContract
1462f86a920SJeremy L Thompson 
147ea61e9acSJeremy L Thompson   @param[in,out] contract CeedTensorContract to destroy
1482f86a920SJeremy L Thompson 
1492f86a920SJeremy L Thompson   @return An error code: 0 - success, otherwise - failure
1502f86a920SJeremy L Thompson 
1517a982d89SJeremy L. Thompson   @ref Backend
1522f86a920SJeremy L Thompson **/
1532f86a920SJeremy L Thompson int CeedTensorContractDestroy(CeedTensorContract *contract) {
154ad6481ceSJeremy L Thompson   if (!*contract || --(*contract)->ref_count > 0) {
155ad6481ceSJeremy L Thompson     *contract = NULL;
156ad6481ceSJeremy L Thompson     return CEED_ERROR_SUCCESS;
157ad6481ceSJeremy L Thompson   }
1582f86a920SJeremy L Thompson   if ((*contract)->Destroy) {
1592b730f8bSJeremy L Thompson     CeedCall((*contract)->Destroy(*contract));
1602f86a920SJeremy L Thompson   }
1612b730f8bSJeremy L Thompson   CeedCall(CeedDestroy(&(*contract)->ceed));
1622b730f8bSJeremy L Thompson   CeedCall(CeedFree(contract));
163e15f9bd0SJeremy L Thompson   return CEED_ERROR_SUCCESS;
1647a982d89SJeremy L. Thompson }
1652f86a920SJeremy L Thompson 
1662f86a920SJeremy L Thompson /// @}
167