xref: /libCEED/rust/libceed-sys/c-src/backends/ref/ceed-ref-tensor.c (revision d1d35e2f02dc969aee8debf3fd943dd784aa847a)
12f86a920SJeremy L Thompson // Copyright (c) 2017-2018, Lawrence Livermore National Security, LLC.
22f86a920SJeremy L Thompson // Produced at the Lawrence Livermore National Laboratory. LLNL-CODE-734707.
32f86a920SJeremy L Thompson // All Rights reserved. See files LICENSE and NOTICE for details.
42f86a920SJeremy L Thompson //
52f86a920SJeremy L Thompson // This file is part of CEED, a collection of benchmarks, miniapps, software
62f86a920SJeremy L Thompson // libraries and APIs for efficient high-order finite element and spectral
72f86a920SJeremy L Thompson // element discretizations for exascale applications. For more information and
82f86a920SJeremy L Thompson // source code availability see http://github.com/ceed.
92f86a920SJeremy L Thompson //
102f86a920SJeremy L Thompson // The CEED research is supported by the Exascale Computing Project 17-SC-20-SC,
112f86a920SJeremy L Thompson // a collaborative effort of two U.S. Department of Energy organizations (Office
122f86a920SJeremy L Thompson // of Science and the National Nuclear Security Administration) responsible for
132f86a920SJeremy L Thompson // the planning and preparation of a capable exascale ecosystem, including
142f86a920SJeremy L Thompson // software, applications, hardware, advanced system engineering and early
152f86a920SJeremy L Thompson // testbed platforms, in support of the nation's exascale computing imperative.
162f86a920SJeremy L Thompson 
17ec3da8bcSJed Brown #include <ceed/ceed.h>
18ec3da8bcSJed Brown #include <ceed/backend.h>
192f86a920SJeremy L Thompson #include "ceed-ref.h"
202f86a920SJeremy L Thompson 
21f10650afSjeremylt //------------------------------------------------------------------------------
22f10650afSjeremylt // Tensor Contract Apply
23f10650afSjeremylt //------------------------------------------------------------------------------
242f86a920SJeremy L Thompson int CeedTensorContractApply_Ref(CeedTensorContract contract, CeedInt A,
252f86a920SJeremy L Thompson                                 CeedInt B, CeedInt C, CeedInt J,
262f86a920SJeremy L Thompson                                 const CeedScalar *restrict t,
27*d1d35e2fSjeremylt                                 CeedTransposeMode t_mode, const CeedInt add,
282f86a920SJeremy L Thompson                                 const CeedScalar *restrict u,
292f86a920SJeremy L Thompson                                 CeedScalar *restrict v) {
30*d1d35e2fSjeremylt   CeedInt t_stride_0 = B, t_stride_1 = 1;
31*d1d35e2fSjeremylt   if (t_mode == CEED_TRANSPOSE) {
32*d1d35e2fSjeremylt     t_stride_0 = 1; t_stride_1 = J;
332f86a920SJeremy L Thompson   }
342f86a920SJeremy L Thompson 
35*d1d35e2fSjeremylt   if (!add)
362f86a920SJeremy L Thompson     for (CeedInt q=0; q<A*J*C; q++)
372f86a920SJeremy L Thompson       v[q] = (CeedScalar) 0.0;
382f86a920SJeremy L Thompson 
392f86a920SJeremy L Thompson   for (CeedInt a=0; a<A; a++)
402f86a920SJeremy L Thompson     for (CeedInt b=0; b<B; b++)
412f86a920SJeremy L Thompson       for (CeedInt j=0; j<J; j++) {
42*d1d35e2fSjeremylt         CeedScalar tq = t[j*t_stride_0 + b*t_stride_1];
432f86a920SJeremy L Thompson         for (CeedInt c=0; c<C; c++)
442f86a920SJeremy L Thompson           v[(a*J+j)*C+c] += tq * u[(a*B+b)*C+c];
452f86a920SJeremy L Thompson       }
46e15f9bd0SJeremy L Thompson   return CEED_ERROR_SUCCESS;
472f86a920SJeremy L Thompson }
482f86a920SJeremy L Thompson 
49f10650afSjeremylt //------------------------------------------------------------------------------
50f10650afSjeremylt // Tensor Contract Destroy
51f10650afSjeremylt //------------------------------------------------------------------------------
522f86a920SJeremy L Thompson static int CeedTensorContractDestroy_Ref(CeedTensorContract contract) {
53e15f9bd0SJeremy L Thompson   return CEED_ERROR_SUCCESS;
542f86a920SJeremy L Thompson }
552f86a920SJeremy L Thompson 
56f10650afSjeremylt //------------------------------------------------------------------------------
57f10650afSjeremylt // Tensor Contract Create
58f10650afSjeremylt //------------------------------------------------------------------------------
59c71e1dcdSjeremylt int CeedTensorContractCreate_Ref(CeedBasis basis, CeedTensorContract contract) {
602f86a920SJeremy L Thompson   int ierr;
612f86a920SJeremy L Thompson   Ceed ceed;
62e15f9bd0SJeremy L Thompson   ierr = CeedTensorContractGetCeed(contract, &ceed); CeedChkBackend(ierr);
632f86a920SJeremy L Thompson 
642f86a920SJeremy L Thompson   ierr = CeedSetBackendFunction(ceed, "TensorContract", contract, "Apply",
65e15f9bd0SJeremy L Thompson                                 CeedTensorContractApply_Ref); CeedChkBackend(ierr);
662f86a920SJeremy L Thompson   ierr = CeedSetBackendFunction(ceed, "TensorContract", contract, "Destroy",
67e15f9bd0SJeremy L Thompson                                 CeedTensorContractDestroy_Ref); CeedChkBackend(ierr);
682f86a920SJeremy L Thompson 
69e15f9bd0SJeremy L Thompson   return CEED_ERROR_SUCCESS;
702f86a920SJeremy L Thompson }
71f10650afSjeremylt //------------------------------------------------------------------------------
72