1 // Copyright (c) 2017-2022, 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/ceed.h> 9 #include <ceed/backend.h> 10 #include <stddef.h> 11 #include "ceed-ref.h" 12 13 //------------------------------------------------------------------------------ 14 // QFunction Apply 15 //------------------------------------------------------------------------------ 16 static int CeedQFunctionApply_Ref(CeedQFunction qf, CeedInt Q, 17 CeedVector *U, CeedVector *V) { 18 int ierr; 19 CeedQFunction_Ref *impl; 20 ierr = CeedQFunctionGetData(qf, &impl); CeedChkBackend(ierr); 21 22 void *ctx_data = NULL; 23 ierr = CeedQFunctionGetContextData(qf, CEED_MEM_HOST, &ctx_data); 24 CeedChkBackend(ierr); 25 26 CeedQFunctionUser f = NULL; 27 ierr = CeedQFunctionGetUserFunction(qf, &f); CeedChkBackend(ierr); 28 29 CeedInt num_in, num_out; 30 ierr = CeedQFunctionGetNumArgs(qf, &num_in, &num_out); CeedChkBackend(ierr); 31 32 for (int i = 0; i<num_in; i++) { 33 ierr = CeedVectorGetArrayRead(U[i], CEED_MEM_HOST, &impl->inputs[i]); 34 CeedChkBackend(ierr); 35 } 36 for (int i = 0; i<num_out; i++) { 37 ierr = CeedVectorGetArrayWrite(V[i], CEED_MEM_HOST, &impl->outputs[i]); 38 CeedChkBackend(ierr); 39 } 40 41 ierr = f(ctx_data, Q, impl->inputs, impl->outputs); CeedChkBackend(ierr); 42 43 for (int i = 0; i<num_in; i++) { 44 ierr = CeedVectorRestoreArrayRead(U[i], &impl->inputs[i]); CeedChkBackend(ierr); 45 } 46 for (int i = 0; i<num_out; i++) { 47 ierr = CeedVectorRestoreArray(V[i], &impl->outputs[i]); CeedChkBackend(ierr); 48 } 49 ierr = CeedQFunctionRestoreContextData(qf, &ctx_data); CeedChkBackend(ierr); 50 51 return CEED_ERROR_SUCCESS; 52 } 53 54 //------------------------------------------------------------------------------ 55 // QFunction Destroy 56 //------------------------------------------------------------------------------ 57 static int CeedQFunctionDestroy_Ref(CeedQFunction qf) { 58 int ierr; 59 CeedQFunction_Ref *impl; 60 ierr = CeedQFunctionGetData(qf, &impl); CeedChkBackend(ierr); 61 62 ierr = CeedFree(&impl->inputs); CeedChkBackend(ierr); 63 ierr = CeedFree(&impl->outputs); CeedChkBackend(ierr); 64 ierr = CeedFree(&impl); CeedChkBackend(ierr); 65 66 return CEED_ERROR_SUCCESS; 67 } 68 69 //------------------------------------------------------------------------------ 70 // QFunction Create 71 //------------------------------------------------------------------------------ 72 int CeedQFunctionCreate_Ref(CeedQFunction qf) { 73 int ierr; 74 Ceed ceed; 75 ierr = CeedQFunctionGetCeed(qf, &ceed); CeedChkBackend(ierr); 76 77 CeedQFunction_Ref *impl; 78 ierr = CeedCalloc(1, &impl); CeedChkBackend(ierr); 79 ierr = CeedCalloc(CEED_FIELD_MAX, &impl->inputs); CeedChkBackend(ierr); 80 ierr = CeedCalloc(CEED_FIELD_MAX, &impl->outputs); CeedChkBackend(ierr); 81 ierr = CeedQFunctionSetData(qf, impl); CeedChkBackend(ierr); 82 83 ierr = CeedSetBackendFunction(ceed, "QFunction", qf, "Apply", 84 CeedQFunctionApply_Ref); CeedChkBackend(ierr); 85 ierr = CeedSetBackendFunction(ceed, "QFunction", qf, "Destroy", 86 CeedQFunctionDestroy_Ref); CeedChkBackend(ierr); 87 88 return CEED_ERROR_SUCCESS; 89 } 90 //------------------------------------------------------------------------------ 91