xref: /libCEED/rust/libceed-sys/c-src/backends/ref/ceed-ref-tensor.c (revision 2a86cc9d4dbfce2964c7e8927a1e6db8d19a41fc)
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 
849aac155SJeremy L Thompson #include <ceed.h>
9ec3da8bcSJed Brown #include <ceed/backend.h>
102b730f8bSJeremy L Thompson 
112f86a920SJeremy L Thompson #include "ceed-ref.h"
122f86a920SJeremy L Thompson 
13f10650afSjeremylt //------------------------------------------------------------------------------
14f10650afSjeremylt // Tensor Contract Apply
15f10650afSjeremylt //------------------------------------------------------------------------------
162b730f8bSJeremy L Thompson static int CeedTensorContractApply_Ref(CeedTensorContract contract, CeedInt A, CeedInt B, CeedInt C, CeedInt J, const CeedScalar *restrict t,
172b730f8bSJeremy L Thompson                                        CeedTransposeMode t_mode, const CeedInt add, const CeedScalar *restrict u, CeedScalar *restrict v) {
18d1d35e2fSjeremylt   CeedInt t_stride_0 = B, t_stride_1 = 1;
19d1d35e2fSjeremylt   if (t_mode == CEED_TRANSPOSE) {
202b730f8bSJeremy L Thompson     t_stride_0 = 1;
212b730f8bSJeremy L Thompson     t_stride_1 = J;
222f86a920SJeremy L Thompson   }
232f86a920SJeremy L Thompson 
242b730f8bSJeremy L Thompson   if (!add) {
252b730f8bSJeremy L Thompson     for (CeedInt q = 0; q < A * J * C; q++) v[q] = (CeedScalar)0.0;
262b730f8bSJeremy L Thompson   }
272f86a920SJeremy L Thompson 
282b730f8bSJeremy L Thompson   for (CeedInt a = 0; a < A; a++) {
292b730f8bSJeremy L Thompson     for (CeedInt b = 0; b < B; b++) {
302f86a920SJeremy L Thompson       for (CeedInt j = 0; j < J; j++) {
31d1d35e2fSjeremylt         CeedScalar tq = t[j * t_stride_0 + b * t_stride_1];
322b730f8bSJeremy L Thompson         for (CeedInt c = 0; c < C; c++) v[(a * J + j) * C + c] += tq * u[(a * B + b) * C + c];
332b730f8bSJeremy L Thompson       }
342b730f8bSJeremy L Thompson     }
352f86a920SJeremy L Thompson   }
36e15f9bd0SJeremy L Thompson   return CEED_ERROR_SUCCESS;
372f86a920SJeremy L Thompson }
382f86a920SJeremy L Thompson 
39f10650afSjeremylt //------------------------------------------------------------------------------
40f10650afSjeremylt // Tensor Contract Destroy
41f10650afSjeremylt //------------------------------------------------------------------------------
422b730f8bSJeremy L Thompson static int CeedTensorContractDestroy_Ref(CeedTensorContract contract) { return CEED_ERROR_SUCCESS; }
432f86a920SJeremy L Thompson 
44f10650afSjeremylt //------------------------------------------------------------------------------
45f10650afSjeremylt // Tensor Contract Create
46f10650afSjeremylt //------------------------------------------------------------------------------
47c71e1dcdSjeremylt int CeedTensorContractCreate_Ref(CeedBasis basis, CeedTensorContract contract) {
482f86a920SJeremy L Thompson   Ceed ceed;
492b730f8bSJeremy L Thompson   CeedCallBackend(CeedTensorContractGetCeed(contract, &ceed));
502f86a920SJeremy L Thompson 
512b730f8bSJeremy L Thompson   CeedCallBackend(CeedSetBackendFunction(ceed, "TensorContract", contract, "Apply", CeedTensorContractApply_Ref));
522b730f8bSJeremy L Thompson   CeedCallBackend(CeedSetBackendFunction(ceed, "TensorContract", contract, "Destroy", CeedTensorContractDestroy_Ref));
532f86a920SJeremy L Thompson 
54e15f9bd0SJeremy L Thompson   return CEED_ERROR_SUCCESS;
552f86a920SJeremy L Thompson }
56*2a86cc9dSSebastian Grimberg 
57f10650afSjeremylt //------------------------------------------------------------------------------
58