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. 321617c04Sjeremylt // 43d8e8822SJeremy L Thompson // SPDX-License-Identifier: BSD-2-Clause 521617c04Sjeremylt // 63d8e8822SJeremy L Thompson // This file is part of CEED: http://github.com/ceed 721617c04Sjeremylt 8ec3da8bcSJed Brown #include <ceed/backend.h> 9*2b730f8bSJeremy L Thompson #include <ceed/ceed.h> 103d576824SJeremy L Thompson #include <stddef.h> 11*2b730f8bSJeremy L Thompson 1221617c04Sjeremylt #include "ceed-ref.h" 1321617c04Sjeremylt 14f10650afSjeremylt //------------------------------------------------------------------------------ 15f10650afSjeremylt // QFunction Apply 16f10650afSjeremylt //------------------------------------------------------------------------------ 17*2b730f8bSJeremy L Thompson static int CeedQFunctionApply_Ref(CeedQFunction qf, CeedInt Q, CeedVector *U, CeedVector *V) { 18aedaa0e5Sjeremylt CeedQFunction_Ref *impl; 19*2b730f8bSJeremy L Thompson CeedCallBackend(CeedQFunctionGetData(qf, &impl)); 20aedaa0e5Sjeremylt 21441428dfSJeremy L Thompson void *ctx_data = NULL; 22*2b730f8bSJeremy L Thompson CeedCallBackend(CeedQFunctionGetContextData(qf, CEED_MEM_HOST, &ctx_data)); 23aedaa0e5Sjeremylt 24692c2638Sjeremylt CeedQFunctionUser f = NULL; 25*2b730f8bSJeremy L Thompson CeedCallBackend(CeedQFunctionGetUserFunction(qf, &f)); 26aedaa0e5Sjeremylt 27d1d35e2fSjeremylt CeedInt num_in, num_out; 28*2b730f8bSJeremy L Thompson CeedCallBackend(CeedQFunctionGetNumArgs(qf, &num_in, &num_out)); 29aedaa0e5Sjeremylt 30*2b730f8bSJeremy L Thompson for (CeedInt i = 0; i < num_in; i++) { 31*2b730f8bSJeremy L Thompson CeedCallBackend(CeedVectorGetArrayRead(U[i], CEED_MEM_HOST, &impl->inputs[i])); 32aedaa0e5Sjeremylt } 33*2b730f8bSJeremy L Thompson for (CeedInt i = 0; i < num_out; i++) { 34*2b730f8bSJeremy L Thompson CeedCallBackend(CeedVectorGetArrayWrite(V[i], CEED_MEM_HOST, &impl->outputs[i])); 35aedaa0e5Sjeremylt } 36aedaa0e5Sjeremylt 37*2b730f8bSJeremy L Thompson CeedCallBackend(f(ctx_data, Q, impl->inputs, impl->outputs)); 38aedaa0e5Sjeremylt 39*2b730f8bSJeremy L Thompson for (CeedInt i = 0; i < num_in; i++) { 40*2b730f8bSJeremy L Thompson CeedCallBackend(CeedVectorRestoreArrayRead(U[i], &impl->inputs[i])); 41aedaa0e5Sjeremylt } 42*2b730f8bSJeremy L Thompson for (CeedInt i = 0; i < num_out; i++) { 43*2b730f8bSJeremy L Thompson CeedCallBackend(CeedVectorRestoreArray(V[i], &impl->outputs[i])); 44aedaa0e5Sjeremylt } 45*2b730f8bSJeremy L Thompson CeedCallBackend(CeedQFunctionRestoreContextData(qf, &ctx_data)); 46aedaa0e5Sjeremylt 47e15f9bd0SJeremy L Thompson return CEED_ERROR_SUCCESS; 4821617c04Sjeremylt } 4921617c04Sjeremylt 50f10650afSjeremylt //------------------------------------------------------------------------------ 51f10650afSjeremylt // QFunction Destroy 52f10650afSjeremylt //------------------------------------------------------------------------------ 5321617c04Sjeremylt static int CeedQFunctionDestroy_Ref(CeedQFunction qf) { 54aedaa0e5Sjeremylt CeedQFunction_Ref *impl; 55*2b730f8bSJeremy L Thompson CeedCallBackend(CeedQFunctionGetData(qf, &impl)); 56aedaa0e5Sjeremylt 57*2b730f8bSJeremy L Thompson CeedCallBackend(CeedFree(&impl->inputs)); 58*2b730f8bSJeremy L Thompson CeedCallBackend(CeedFree(&impl->outputs)); 59*2b730f8bSJeremy L Thompson CeedCallBackend(CeedFree(&impl)); 60aedaa0e5Sjeremylt 61e15f9bd0SJeremy L Thompson return CEED_ERROR_SUCCESS; 6221617c04Sjeremylt } 6321617c04Sjeremylt 64f10650afSjeremylt //------------------------------------------------------------------------------ 65f10650afSjeremylt // QFunction Create 66f10650afSjeremylt //------------------------------------------------------------------------------ 6721617c04Sjeremylt int CeedQFunctionCreate_Ref(CeedQFunction qf) { 68fe2413ffSjeremylt Ceed ceed; 69*2b730f8bSJeremy L Thompson CeedCallBackend(CeedQFunctionGetCeed(qf, &ceed)); 70fe2413ffSjeremylt 71aedaa0e5Sjeremylt CeedQFunction_Ref *impl; 72*2b730f8bSJeremy L Thompson CeedCallBackend(CeedCalloc(1, &impl)); 73*2b730f8bSJeremy L Thompson CeedCallBackend(CeedCalloc(CEED_FIELD_MAX, &impl->inputs)); 74*2b730f8bSJeremy L Thompson CeedCallBackend(CeedCalloc(CEED_FIELD_MAX, &impl->outputs)); 75*2b730f8bSJeremy L Thompson CeedCallBackend(CeedQFunctionSetData(qf, impl)); 76aedaa0e5Sjeremylt 77*2b730f8bSJeremy L Thompson CeedCallBackend(CeedSetBackendFunction(ceed, "QFunction", qf, "Apply", CeedQFunctionApply_Ref)); 78*2b730f8bSJeremy L Thompson CeedCallBackend(CeedSetBackendFunction(ceed, "QFunction", qf, "Destroy", CeedQFunctionDestroy_Ref)); 79aedaa0e5Sjeremylt 80e15f9bd0SJeremy L Thompson return CEED_ERROR_SUCCESS; 8121617c04Sjeremylt } 82f10650afSjeremylt //------------------------------------------------------------------------------ 83