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 849aac155SJeremy L Thompson #include <ceed.h> 9ec3da8bcSJed Brown #include <ceed/backend.h> 103d576824SJeremy L Thompson #include <stddef.h> 112b730f8bSJeremy L Thompson 1221617c04Sjeremylt #include "ceed-ref.h" 1321617c04Sjeremylt 14f10650afSjeremylt //------------------------------------------------------------------------------ 15f10650afSjeremylt // QFunction Apply 16f10650afSjeremylt //------------------------------------------------------------------------------ 172b730f8bSJeremy L Thompson static int CeedQFunctionApply_Ref(CeedQFunction qf, CeedInt Q, CeedVector *U, CeedVector *V) { 18441428dfSJeremy L Thompson void *ctx_data = NULL; 19d1d35e2fSjeremylt CeedInt num_in, num_out; 20*ad70ee2cSJeremy L Thompson CeedQFunctionUser f = NULL; 21*ad70ee2cSJeremy L Thompson CeedQFunction_Ref *impl; 22*ad70ee2cSJeremy L Thompson 23*ad70ee2cSJeremy L Thompson CeedCallBackend(CeedQFunctionGetData(qf, &impl)); 24*ad70ee2cSJeremy L Thompson CeedCallBackend(CeedQFunctionGetContextData(qf, CEED_MEM_HOST, &ctx_data)); 25*ad70ee2cSJeremy L Thompson CeedCallBackend(CeedQFunctionGetUserFunction(qf, &f)); 262b730f8bSJeremy L Thompson CeedCallBackend(CeedQFunctionGetNumArgs(qf, &num_in, &num_out)); 27aedaa0e5Sjeremylt 282b730f8bSJeremy L Thompson for (CeedInt i = 0; i < num_in; i++) { 292b730f8bSJeremy L Thompson CeedCallBackend(CeedVectorGetArrayRead(U[i], CEED_MEM_HOST, &impl->inputs[i])); 30aedaa0e5Sjeremylt } 312b730f8bSJeremy L Thompson for (CeedInt i = 0; i < num_out; i++) { 322b730f8bSJeremy L Thompson CeedCallBackend(CeedVectorGetArrayWrite(V[i], CEED_MEM_HOST, &impl->outputs[i])); 33aedaa0e5Sjeremylt } 34aedaa0e5Sjeremylt 352b730f8bSJeremy L Thompson CeedCallBackend(f(ctx_data, Q, impl->inputs, impl->outputs)); 36aedaa0e5Sjeremylt 372b730f8bSJeremy L Thompson for (CeedInt i = 0; i < num_in; i++) { 382b730f8bSJeremy L Thompson CeedCallBackend(CeedVectorRestoreArrayRead(U[i], &impl->inputs[i])); 39aedaa0e5Sjeremylt } 402b730f8bSJeremy L Thompson for (CeedInt i = 0; i < num_out; i++) { 412b730f8bSJeremy L Thompson CeedCallBackend(CeedVectorRestoreArray(V[i], &impl->outputs[i])); 42aedaa0e5Sjeremylt } 432b730f8bSJeremy L Thompson CeedCallBackend(CeedQFunctionRestoreContextData(qf, &ctx_data)); 44e15f9bd0SJeremy L Thompson return CEED_ERROR_SUCCESS; 4521617c04Sjeremylt } 4621617c04Sjeremylt 47f10650afSjeremylt //------------------------------------------------------------------------------ 48f10650afSjeremylt // QFunction Destroy 49f10650afSjeremylt //------------------------------------------------------------------------------ 5021617c04Sjeremylt static int CeedQFunctionDestroy_Ref(CeedQFunction qf) { 51aedaa0e5Sjeremylt CeedQFunction_Ref *impl; 52aedaa0e5Sjeremylt 53*ad70ee2cSJeremy L Thompson CeedCallBackend(CeedQFunctionGetData(qf, &impl)); 542b730f8bSJeremy L Thompson CeedCallBackend(CeedFree(&impl->inputs)); 552b730f8bSJeremy L Thompson CeedCallBackend(CeedFree(&impl->outputs)); 562b730f8bSJeremy L Thompson CeedCallBackend(CeedFree(&impl)); 57e15f9bd0SJeremy L Thompson return CEED_ERROR_SUCCESS; 5821617c04Sjeremylt } 5921617c04Sjeremylt 60f10650afSjeremylt //------------------------------------------------------------------------------ 61f10650afSjeremylt // QFunction Create 62f10650afSjeremylt //------------------------------------------------------------------------------ 6321617c04Sjeremylt int CeedQFunctionCreate_Ref(CeedQFunction qf) { 64fe2413ffSjeremylt Ceed ceed; 65aedaa0e5Sjeremylt CeedQFunction_Ref *impl; 66*ad70ee2cSJeremy L Thompson 67*ad70ee2cSJeremy L Thompson CeedCallBackend(CeedQFunctionGetCeed(qf, &ceed)); 682b730f8bSJeremy L Thompson CeedCallBackend(CeedCalloc(1, &impl)); 692b730f8bSJeremy L Thompson CeedCallBackend(CeedCalloc(CEED_FIELD_MAX, &impl->inputs)); 702b730f8bSJeremy L Thompson CeedCallBackend(CeedCalloc(CEED_FIELD_MAX, &impl->outputs)); 712b730f8bSJeremy L Thompson CeedCallBackend(CeedQFunctionSetData(qf, impl)); 722b730f8bSJeremy L Thompson CeedCallBackend(CeedSetBackendFunction(ceed, "QFunction", qf, "Apply", CeedQFunctionApply_Ref)); 732b730f8bSJeremy L Thompson CeedCallBackend(CeedSetBackendFunction(ceed, "QFunction", qf, "Destroy", CeedQFunctionDestroy_Ref)); 74e15f9bd0SJeremy L Thompson return CEED_ERROR_SUCCESS; 7521617c04Sjeremylt } 762a86cc9dSSebastian Grimberg 77f10650afSjeremylt //------------------------------------------------------------------------------ 78