xref: /libCEED/rust/libceed-sys/c-src/backends/ref/ceed-ref-tensor.c (revision 3d8e882215d238700cdceb37404f76ca7fa24eaa)
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>
102f86a920SJeremy L Thompson #include "ceed-ref.h"
112f86a920SJeremy L Thompson 
12f10650afSjeremylt //------------------------------------------------------------------------------
13f10650afSjeremylt // Tensor Contract Apply
14f10650afSjeremylt //------------------------------------------------------------------------------
154820c5c4SJeremy L Thompson static int CeedTensorContractApply_Ref(CeedTensorContract contract, CeedInt A,
162f86a920SJeremy L Thompson                                        CeedInt B, CeedInt C, CeedInt J,
172f86a920SJeremy L Thompson                                        const CeedScalar *restrict t,
18d1d35e2fSjeremylt                                        CeedTransposeMode t_mode, const CeedInt add,
192f86a920SJeremy L Thompson                                        const CeedScalar *restrict u,
202f86a920SJeremy L Thompson                                        CeedScalar *restrict v) {
21d1d35e2fSjeremylt   CeedInt t_stride_0 = B, t_stride_1 = 1;
22d1d35e2fSjeremylt   if (t_mode == CEED_TRANSPOSE) {
23d1d35e2fSjeremylt     t_stride_0 = 1; t_stride_1 = J;
242f86a920SJeremy L Thompson   }
252f86a920SJeremy L Thompson 
26d1d35e2fSjeremylt   if (!add)
272f86a920SJeremy L Thompson     for (CeedInt q=0; q<A*J*C; q++)
282f86a920SJeremy L Thompson       v[q] = (CeedScalar) 0.0;
292f86a920SJeremy L Thompson 
302f86a920SJeremy L Thompson   for (CeedInt a=0; a<A; a++)
312f86a920SJeremy L Thompson     for (CeedInt b=0; b<B; b++)
322f86a920SJeremy L Thompson       for (CeedInt j=0; j<J; j++) {
33d1d35e2fSjeremylt         CeedScalar tq = t[j*t_stride_0 + b*t_stride_1];
342f86a920SJeremy L Thompson         for (CeedInt c=0; c<C; c++)
352f86a920SJeremy L Thompson           v[(a*J+j)*C+c] += tq * u[(a*B+b)*C+c];
362f86a920SJeremy L Thompson       }
37e15f9bd0SJeremy L Thompson   return CEED_ERROR_SUCCESS;
382f86a920SJeremy L Thompson }
392f86a920SJeremy L Thompson 
40f10650afSjeremylt //------------------------------------------------------------------------------
41f10650afSjeremylt // Tensor Contract Destroy
42f10650afSjeremylt //------------------------------------------------------------------------------
432f86a920SJeremy L Thompson static int CeedTensorContractDestroy_Ref(CeedTensorContract contract) {
44e15f9bd0SJeremy L Thompson   return CEED_ERROR_SUCCESS;
452f86a920SJeremy L Thompson }
462f86a920SJeremy L Thompson 
47f10650afSjeremylt //------------------------------------------------------------------------------
48f10650afSjeremylt // Tensor Contract Create
49f10650afSjeremylt //------------------------------------------------------------------------------
50c71e1dcdSjeremylt int CeedTensorContractCreate_Ref(CeedBasis basis, CeedTensorContract contract) {
512f86a920SJeremy L Thompson   int ierr;
522f86a920SJeremy L Thompson   Ceed ceed;
53e15f9bd0SJeremy L Thompson   ierr = CeedTensorContractGetCeed(contract, &ceed); CeedChkBackend(ierr);
542f86a920SJeremy L Thompson 
552f86a920SJeremy L Thompson   ierr = CeedSetBackendFunction(ceed, "TensorContract", contract, "Apply",
56e15f9bd0SJeremy L Thompson                                 CeedTensorContractApply_Ref); CeedChkBackend(ierr);
572f86a920SJeremy L Thompson   ierr = CeedSetBackendFunction(ceed, "TensorContract", contract, "Destroy",
58e15f9bd0SJeremy L Thompson                                 CeedTensorContractDestroy_Ref); CeedChkBackend(ierr);
592f86a920SJeremy L Thompson 
60e15f9bd0SJeremy L Thompson   return CEED_ERROR_SUCCESS;
612f86a920SJeremy L Thompson }
62f10650afSjeremylt //------------------------------------------------------------------------------
63