xref: /libCEED/backends/ref/ceed-ref-tensor.c (revision 07d5b98a8feba68a643190b8ea9bcdac5c3e6570)
1 // Copyright (c) 2017-2024, Lawrence Livermore National Security, LLC and other CEED contributors.
2 // All Rights Reserved. See the top-level LICENSE and NOTICE files for details.
3 //
4 // SPDX-License-Identifier: BSD-2-Clause
5 //
6 // This file is part of CEED:  http://github.com/ceed
7 
8 #include <ceed.h>
9 #include <ceed/backend.h>
10 
11 #include "ceed-ref.h"
12 
13 //------------------------------------------------------------------------------
14 // Tensor Contract Apply
15 //------------------------------------------------------------------------------
16 static int CeedTensorContractApply_Ref(CeedTensorContract contract, CeedInt A, CeedInt B, CeedInt C, CeedInt J, const CeedScalar *restrict t,
17                                        CeedTransposeMode t_mode, const CeedInt add, const CeedScalar *restrict u, CeedScalar *restrict v) {
18   CeedInt t_stride_0 = B, t_stride_1 = 1;
19 
20   if (t_mode == CEED_TRANSPOSE) {
21     t_stride_0 = 1;
22     t_stride_1 = J;
23   }
24 
25   if (!add) {
26     for (CeedInt q = 0; q < A * J * C; q++) v[q] = (CeedScalar)0.0;
27   }
28 
29   for (CeedInt a = 0; a < A; a++) {
30     for (CeedInt b = 0; b < B; b++) {
31       for (CeedInt j = 0; j < J; j++) {
32         CeedScalar tq = t[j * t_stride_0 + b * t_stride_1];
33         for (CeedInt c = 0; c < C; c++) v[(a * J + j) * C + c] += tq * u[(a * B + b) * C + c];
34       }
35     }
36   }
37   return CEED_ERROR_SUCCESS;
38 }
39 
40 //------------------------------------------------------------------------------
41 // Tensor Contract Destroy
42 //------------------------------------------------------------------------------
43 static int CeedTensorContractDestroy_Ref(CeedTensorContract contract) { return CEED_ERROR_SUCCESS; }
44 
45 //------------------------------------------------------------------------------
46 // Tensor Contract Create
47 //------------------------------------------------------------------------------
48 int CeedTensorContractCreate_Ref(CeedTensorContract contract) {
49   Ceed ceed;
50 
51   CeedCallBackend(CeedTensorContractGetCeed(contract, &ceed));
52   CeedCallBackend(CeedSetBackendFunction(ceed, "TensorContract", contract, "Apply", CeedTensorContractApply_Ref));
53   CeedCallBackend(CeedSetBackendFunction(ceed, "TensorContract", contract, "Destroy", CeedTensorContractDestroy_Ref));
54   return CEED_ERROR_SUCCESS;
55 }
56 
57 //------------------------------------------------------------------------------
58