From 9bc663991d6482bcb1d60b1f116148f11db83fa1 Mon Sep 17 00:00:00 2001 From: Jeremy L Thompson Date: Tue, 22 Oct 2024 16:55:55 -0600 Subject: [PATCH 1/7] ceed - require *GetCeed ceed to be Destroyed --- backends/avx/ceed-avx-blocked.c | 1 + backends/avx/ceed-avx-serial.c | 1 + backends/blocked/ceed-blocked-operator.c | 9 +- backends/blocked/ceed-blocked.c | 1 + .../cuda-gen/ceed-cuda-gen-operator-build.cpp | 2 +- backends/cuda-gen/ceed-cuda-gen-operator.c | 3 + backends/cuda-gen/ceed-cuda-gen-qfunction.c | 2 +- backends/cuda-gen/ceed-cuda-gen.c | 1 + backends/cuda-ref/ceed-cuda-ref-basis.c | 12 +- backends/cuda-ref/ceed-cuda-ref-operator.c | 35 ++-- .../cuda-ref/ceed-cuda-ref-qfunction-load.cpp | 7 +- backends/cuda-ref/ceed-cuda-ref-qfunction.c | 3 +- .../cuda-ref/ceed-cuda-ref-qfunctioncontext.c | 4 + backends/cuda-ref/ceed-cuda-ref-restriction.c | 6 + backends/cuda-ref/ceed-cuda-ref-vector.c | 17 +- backends/cuda-shared/ceed-cuda-shared-basis.c | 7 +- backends/cuda-shared/ceed-cuda-shared.c | 1 + .../hip-gen/ceed-hip-gen-operator-build.cpp | 2 +- backends/hip-gen/ceed-hip-gen-operator.c | 3 + backends/hip-gen/ceed-hip-gen-qfunction.c | 2 +- backends/hip-gen/ceed-hip-gen.c | 1 + backends/hip-ref/ceed-hip-ref-basis.c | 12 +- backends/hip-ref/ceed-hip-ref-operator.c | 35 ++-- .../hip-ref/ceed-hip-ref-qfunction-load.cpp | 9 +- backends/hip-ref/ceed-hip-ref-qfunction.c | 3 +- .../hip-ref/ceed-hip-ref-qfunctioncontext.c | 4 + backends/hip-ref/ceed-hip-ref-restriction.c | 6 + backends/hip-ref/ceed-hip-ref-vector.c | 17 +- backends/hip-shared/ceed-hip-shared-basis.c | 5 +- backends/hip-shared/ceed-hip-shared.c | 1 + backends/magma/ceed-magma-basis.c | 13 ++ backends/magma/ceed-magma-det.c | 1 + backends/magma/ceed-magma.c | 1 + backends/memcheck/ceed-memcheck-blocked.c | 1 + backends/memcheck/ceed-memcheck-qfunction.c | 5 +- .../memcheck/ceed-memcheck-qfunctioncontext.c | 10 +- backends/memcheck/ceed-memcheck-restriction.c | 1 + backends/memcheck/ceed-memcheck-serial.c | 1 + backends/memcheck/ceed-memcheck-vector.c | 15 +- backends/opt/ceed-opt-blocked.c | 1 + backends/opt/ceed-opt-operator.c | 9 +- backends/opt/ceed-opt-serial.c | 1 + backends/ref/ceed-ref-basis.c | 22 ++- backends/ref/ceed-ref-operator.c | 25 ++- backends/ref/ceed-ref-qfunction.c | 1 + backends/ref/ceed-ref-qfunctioncontext.c | 1 + backends/ref/ceed-ref-restriction.c | 12 +- backends/ref/ceed-ref-tensor.c | 1 + backends/ref/ceed-ref-vector.c | 1 + .../ceed-sycl-gen-operator-build.sycl.cpp | 2 +- .../sycl-gen/ceed-sycl-gen-operator.sycl.cpp | 3 + .../sycl-gen/ceed-sycl-gen-qfunction.sycl.cpp | 2 + backends/sycl-gen/ceed-sycl-gen.sycl.cpp | 2 + .../sycl-ref/ceed-sycl-ref-basis.sycl.cpp | 21 +- .../sycl-ref/ceed-sycl-ref-operator.sycl.cpp | 40 ++-- .../ceed-sycl-ref-qfunction-load.sycl.cpp | 3 +- .../sycl-ref/ceed-sycl-ref-qfunction.sycl.cpp | 3 + .../ceed-sycl-ref-qfunctioncontext.sycl.cpp | 15 +- .../sycl-ref/ceed-sycl-restriction.sycl.cpp | 6 +- backends/sycl-ref/ceed-sycl-vector.sycl.cpp | 32 ++- .../ceed-sycl-shared-basis.sycl.cpp | 4 +- backends/sycl/ceed-sycl-common.sycl.cpp | 4 +- backends/xsmm/ceed-xsmm-blocked.c | 1 + backends/xsmm/ceed-xsmm-serial.c | 1 + backends/xsmm/ceed-xsmm-tensor.c | 8 +- examples/fluids/include/petsc-ceed-utils.h | 1 + examples/fluids/src/mat-ceed.c | 1 - examples/fluids/src/petsc_ops.c | 1 + examples/fluids/src/turb_spanstats.c | 2 + interface/ceed-basis.c | 53 ++--- interface/ceed-cuda.c | 5 +- interface/ceed-elemrestriction.c | 64 +++--- interface/ceed-hip.c | 5 +- interface/ceed-operator.c | 127 ++++++------ interface/ceed-preconditioning.c | 100 +++++----- interface/ceed-qfunction.c | 37 ++-- interface/ceed-qfunctioncontext.c | 50 +++-- interface/ceed-tensor.c | 4 +- interface/ceed-vector.c | 183 ++++++++++-------- interface/ceed.c | 24 ++- tests/t409-qfunction.c | 1 + 81 files changed, 657 insertions(+), 482 deletions(-) diff --git a/backends/avx/ceed-avx-blocked.c b/backends/avx/ceed-avx-blocked.c index c565faa653..bf898f571a 100644 --- a/backends/avx/ceed-avx-blocked.c +++ b/backends/avx/ceed-avx-blocked.c @@ -25,6 +25,7 @@ static int CeedInit_Avx(const char *resource, Ceed ceed) { // Create reference Ceed that implementation will be dispatched through unless overridden CeedCallBackend(CeedInit("/cpu/self/opt/blocked", &ceed_ref)); CeedCallBackend(CeedSetDelegate(ceed, ceed_ref)); + CeedCallBackend(CeedDestroy(&ceed_ref)); CeedCallBackend(CeedSetBackendFunction(ceed, "Ceed", ceed, "TensorContractCreate", CeedTensorContractCreate_Avx)); return CEED_ERROR_SUCCESS; diff --git a/backends/avx/ceed-avx-serial.c b/backends/avx/ceed-avx-serial.c index 5ebe28e19a..5d33900758 100644 --- a/backends/avx/ceed-avx-serial.c +++ b/backends/avx/ceed-avx-serial.c @@ -25,6 +25,7 @@ static int CeedInit_Avx(const char *resource, Ceed ceed) { // Create reference Ceed that implementation will be dispatched through unless overridden CeedCallBackend(CeedInit("/cpu/self/opt/serial", &ceed_ref)); CeedCallBackend(CeedSetDelegate(ceed, ceed_ref)); + CeedCallBackend(CeedDestroy(&ceed_ref)); CeedCallBackend(CeedSetBackendFunction(ceed, "Ceed", ceed, "TensorContractCreate", CeedTensorContractCreate_Avx)); return CEED_ERROR_SUCCESS; diff --git a/backends/blocked/ceed-blocked-operator.c b/backends/blocked/ceed-blocked-operator.c index c9f5ccfd46..2f7b0e6dad 100644 --- a/backends/blocked/ceed-blocked-operator.c +++ b/backends/blocked/ceed-blocked-operator.c @@ -30,7 +30,8 @@ static int CeedOperatorSetupFields_Blocked(CeedQFunction qf, CeedOperator op, bo CeedCallBackend(CeedOperatorGetCeed(op, &ceed)); CeedCallBackend(CeedGetParent(ceed, &ceed_parent)); - if (ceed_parent) ceed = ceed_parent; + CeedCallBackend(CeedReferenceCopy(ceed_parent, &ceed)); + CeedCallBackend(CeedDestroy(&ceed_parent)); } if (is_input) { CeedCallBackend(CeedOperatorGetFields(op, NULL, &op_fields, NULL, NULL)); @@ -105,6 +106,7 @@ static int CeedOperatorSetupFields_Blocked(CeedQFunction qf, CeedOperator op, bo // Empty case - won't occur break; } + CeedCallBackend(CeedDestroy(&ceed_rstr)); CeedCallBackend(CeedElemRestrictionDestroy(&rstr)); CeedCallBackend(CeedElemRestrictionCreateVector(block_rstr[i + start_e], NULL, &e_vecs_full[i + start_e])); } @@ -190,6 +192,7 @@ static int CeedOperatorSetupFields_Blocked(CeedQFunction qf, CeedOperator op, bo CeedCallBackend(CeedElemRestrictionDestroy(&rstr_i)); } } + CeedCallBackend(CeedDestroy(&ceed)); return CEED_ERROR_SUCCESS; } @@ -198,7 +201,6 @@ static int CeedOperatorSetupFields_Blocked(CeedQFunction qf, CeedOperator op, bo //------------------------------------------------------------------------------ static int CeedOperatorSetup_Blocked(CeedOperator op) { bool is_setup_done; - Ceed ceed; CeedInt Q, num_input_fields, num_output_fields; const CeedInt block_size = 8; CeedQFunctionField *qf_input_fields, *qf_output_fields; @@ -209,7 +211,6 @@ static int CeedOperatorSetup_Blocked(CeedOperator op) { CeedCallBackend(CeedOperatorIsSetupDone(op, &is_setup_done)); if (is_setup_done) return CEED_ERROR_SUCCESS; - CeedCallBackend(CeedOperatorGetCeed(op, &ceed)); CeedCallBackend(CeedOperatorGetData(op, &impl)); CeedCallBackend(CeedOperatorGetQFunction(op, &qf)); CeedCallBackend(CeedOperatorGetNumQuadraturePoints(op, &Q)); @@ -707,6 +708,7 @@ static inline int CeedOperatorLinearAssembleQFunctionCore_Blocked(CeedOperator o CeedCallBackend(CeedOperatorRestoreInputs_Blocked(num_input_fields, qf_input_fields, op_input_fields, true, e_data_full, impl)); // Output blocked restriction + CeedCallBackend(CeedDestroy(&ceed)); CeedCallBackend(CeedVectorRestoreArray(l_vec, &l_vec_array)); CeedCallBackend(CeedVectorSetValue(*assembled, 0.0)); CeedCallBackend(CeedElemRestrictionApply(block_rstr, CEED_TRANSPOSE, l_vec, *assembled, request)); @@ -783,6 +785,7 @@ int CeedOperatorCreate_Blocked(CeedOperator op) { CeedCallBackend(CeedSetBackendFunction(ceed, "Operator", op, "LinearAssembleQFunctionUpdate", CeedOperatorLinearAssembleQFunctionUpdate_Blocked)); CeedCallBackend(CeedSetBackendFunction(ceed, "Operator", op, "ApplyAdd", CeedOperatorApplyAdd_Blocked)); CeedCallBackend(CeedSetBackendFunction(ceed, "Operator", op, "Destroy", CeedOperatorDestroy_Blocked)); + CeedCallBackend(CeedDestroy(&ceed)); return CEED_ERROR_SUCCESS; } diff --git a/backends/blocked/ceed-blocked.c b/backends/blocked/ceed-blocked.c index f37338f0d6..ca55c01e45 100644 --- a/backends/blocked/ceed-blocked.c +++ b/backends/blocked/ceed-blocked.c @@ -25,6 +25,7 @@ static int CeedInit_Blocked(const char *resource, Ceed ceed) { // Create reference Ceed that implementation will be dispatched through unless overridden CeedCallBackend(CeedInit("/cpu/self/ref/serial", &ceed_ref)); CeedCallBackend(CeedSetDelegate(ceed, ceed_ref)); + CeedCallBackend(CeedDestroy(&ceed_ref)); CeedCallBackend(CeedSetBackendFunction(ceed, "Ceed", ceed, "OperatorCreate", CeedOperatorCreate_Blocked)); return CEED_ERROR_SUCCESS; diff --git a/backends/cuda-gen/ceed-cuda-gen-operator-build.cpp b/backends/cuda-gen/ceed-cuda-gen-operator-build.cpp index 315db3844f..3103bcbfb7 100644 --- a/backends/cuda-gen/ceed-cuda-gen-operator-build.cpp +++ b/backends/cuda-gen/ceed-cuda-gen-operator-build.cpp @@ -901,8 +901,8 @@ extern "C" int CeedOperatorBuildKernel_Cuda_gen(CeedOperator op) { CeedCallBackend(CeedCompile_Cuda(ceed, code.str().c_str(), &data->module, 1, "T_1D", CeedIntMax(Q_1d, data->max_P_1d))); CeedCallBackend(CeedGetKernel_Cuda(ceed, data->module, operator_name.c_str(), &data->op)); - CeedCallBackend(CeedOperatorSetSetupDone(op)); + CeedCallBackend(CeedDestroy(&ceed)); return CEED_ERROR_SUCCESS; } diff --git a/backends/cuda-gen/ceed-cuda-gen-operator.c b/backends/cuda-gen/ceed-cuda-gen-operator.c index 4c235984fd..7ea1002b21 100644 --- a/backends/cuda-gen/ceed-cuda-gen-operator.c +++ b/backends/cuda-gen/ceed-cuda-gen-operator.c @@ -122,6 +122,7 @@ static int CeedOperatorApplyAdd_Cuda_gen(CeedOperator op, CeedVector input_vec, CeedOperator op_fallback; CeedDebug256(ceed, CEED_DEBUG_COLOR_SUCCESS, "Falling back to /gpu/cuda/ref CeedOperator due to non-tensor bases"); + CeedCallBackend(CeedDestroy(&ceed)); CeedCallBackend(CeedOperatorGetFallback(op, &op_fallback)); CeedCallBackend(CeedOperatorApplyAdd(op_fallback, input_vec, output_vec, request)); return CEED_ERROR_SUCCESS; @@ -251,6 +252,7 @@ static int CeedOperatorApplyAdd_Cuda_gen(CeedOperator op, CeedVector input_vec, // Restore context data CeedCallBackend(CeedQFunctionRestoreInnerContextData(qf, &qf_data->d_c)); + CeedCallBackend(CeedDestroy(&ceed)); return CEED_ERROR_SUCCESS; } @@ -266,6 +268,7 @@ int CeedOperatorCreate_Cuda_gen(CeedOperator op) { CeedCallBackend(CeedOperatorSetData(op, impl)); CeedCallBackend(CeedSetBackendFunction(ceed, "Operator", op, "ApplyAdd", CeedOperatorApplyAdd_Cuda_gen)); CeedCallBackend(CeedSetBackendFunction(ceed, "Operator", op, "Destroy", CeedOperatorDestroy_Cuda_gen)); + CeedCallBackend(CeedDestroy(&ceed)); return CEED_ERROR_SUCCESS; } diff --git a/backends/cuda-gen/ceed-cuda-gen-qfunction.c b/backends/cuda-gen/ceed-cuda-gen-qfunction.c index aec5294a8d..483b520503 100644 --- a/backends/cuda-gen/ceed-cuda-gen-qfunction.c +++ b/backends/cuda-gen/ceed-cuda-gen-qfunction.c @@ -42,11 +42,11 @@ int CeedQFunctionCreate_Cuda_gen(CeedQFunction qf) { CeedCallBackend(CeedCalloc(1, &data)); CeedCallBackend(CeedQFunctionSetData(qf, data)); - // Read QFunction source CeedCallBackend(CeedQFunctionGetKernelName(qf, &data->qfunction_name)); CeedCallBackend(CeedSetBackendFunction(ceed, "QFunction", qf, "Apply", CeedQFunctionApply_Cuda_gen)); CeedCallBackend(CeedSetBackendFunction(ceed, "QFunction", qf, "Destroy", CeedQFunctionDestroy_Cuda_gen)); + CeedCallBackend(CeedDestroy(&ceed)); return CEED_ERROR_SUCCESS; } diff --git a/backends/cuda-gen/ceed-cuda-gen.c b/backends/cuda-gen/ceed-cuda-gen.c index fd4fcef722..0ab817186d 100644 --- a/backends/cuda-gen/ceed-cuda-gen.c +++ b/backends/cuda-gen/ceed-cuda-gen.c @@ -33,6 +33,7 @@ static int CeedInit_Cuda_gen(const char *resource, Ceed ceed) { CeedCallBackend(CeedInit("/gpu/cuda/shared", &ceed_shared)); CeedCallBackend(CeedSetDelegate(ceed, ceed_shared)); + CeedCallBackend(CeedDestroy(&ceed_shared)); CeedCallBackend(CeedSetOperatorFallbackResource(ceed, fallback_resource)); diff --git a/backends/cuda-ref/ceed-cuda-ref-basis.c b/backends/cuda-ref/ceed-cuda-ref-basis.c index 3eca8134c3..d7ab9a4aae 100644 --- a/backends/cuda-ref/ceed-cuda-ref-basis.c +++ b/backends/cuda-ref/ceed-cuda-ref-basis.c @@ -88,6 +88,7 @@ static int CeedBasisApplyCore_Cuda(CeedBasis basis, bool apply_add, const CeedIn CeedCallBackend(CeedVectorRestoreArray(v, &d_v)); if (eval_mode == CEED_EVAL_NONE) CeedCallBackend(CeedVectorSetArray(v, CEED_MEM_DEVICE, CEED_COPY_VALUES, (CeedScalar *)d_u)); if (eval_mode != CEED_EVAL_WEIGHT) CeedCallBackend(CeedVectorRestoreArrayRead(u, &d_u)); + CeedCallBackend(CeedDestroy(&ceed)); return CEED_ERROR_SUCCESS; } @@ -116,7 +117,6 @@ static int CeedBasisApplyAtPointsCore_Cuda(CeedBasis basis, bool apply_add, cons CeedScalar *d_v; CeedBasis_Cuda *data; - CeedCallBackend(CeedBasisGetCeed(basis, &ceed)); CeedCallBackend(CeedBasisGetData(basis, &data)); CeedCallBackend(CeedBasisGetNumQuadraturePoints1D(basis, &Q_1d)); CeedCallBackend(CeedBasisGetDimension(basis, &dim)); @@ -127,6 +127,8 @@ static int CeedBasisApplyAtPointsCore_Cuda(CeedBasis basis, bool apply_add, cons return CEED_ERROR_SUCCESS; } + CeedCallBackend(CeedBasisGetCeed(basis, &ceed)); + // Check padded to uniform number of points per elem for (CeedInt i = 1; i < num_elem; i++) max_num_points = CeedIntMax(max_num_points, num_points[i]); { @@ -244,6 +246,7 @@ static int CeedBasisApplyAtPointsCore_Cuda(CeedBasis basis, bool apply_add, cons CeedCallBackend(CeedVectorRestoreArray(v, &d_v)); if (eval_mode == CEED_EVAL_NONE) CeedCallBackend(CeedVectorSetArray(v, CEED_MEM_DEVICE, CEED_COPY_VALUES, (CeedScalar *)d_u)); if (eval_mode != CEED_EVAL_WEIGHT) CeedCallBackend(CeedVectorRestoreArrayRead(u, &d_u)); + CeedCallBackend(CeedDestroy(&ceed)); return CEED_ERROR_SUCCESS; } @@ -351,6 +354,7 @@ static int CeedBasisApplyNonTensorCore_Cuda(CeedBasis basis, bool apply_add, con CeedCallBackend(CeedVectorRestoreArray(v, &d_v)); if (eval_mode == CEED_EVAL_NONE) CeedCallBackend(CeedVectorSetArray(v, CEED_MEM_DEVICE, CEED_COPY_VALUES, (CeedScalar *)d_u)); if (eval_mode != CEED_EVAL_WEIGHT) CeedCallBackend(CeedVectorRestoreArrayRead(u, &d_u)); + CeedCallBackend(CeedDestroy(&ceed)); return CEED_ERROR_SUCCESS; } @@ -384,6 +388,7 @@ static int CeedBasisDestroy_Cuda(CeedBasis basis) { CeedCallCuda(ceed, cudaFree(data->d_grad_1d)); CeedCallCuda(ceed, cudaFree(data->d_chebyshev_interp_1d)); CeedCallBackend(CeedFree(&data)); + CeedCallBackend(CeedDestroy(&ceed)); return CEED_ERROR_SUCCESS; } @@ -403,6 +408,7 @@ static int CeedBasisDestroyNonTensor_Cuda(CeedBasis basis) { CeedCallCuda(ceed, cudaFree(data->d_div)); CeedCallCuda(ceed, cudaFree(data->d_curl)); CeedCallBackend(CeedFree(&data)); + CeedCallBackend(CeedDestroy(&ceed)); return CEED_ERROR_SUCCESS; } @@ -449,6 +455,7 @@ int CeedBasisCreateTensorH1_Cuda(CeedInt dim, CeedInt P_1d, CeedInt Q_1d, const CeedCallBackend(CeedSetBackendFunction(ceed, "Basis", basis, "ApplyAtPoints", CeedBasisApplyAtPoints_Cuda)); CeedCallBackend(CeedSetBackendFunction(ceed, "Basis", basis, "ApplyAddAtPoints", CeedBasisApplyAddAtPoints_Cuda)); CeedCallBackend(CeedSetBackendFunction(ceed, "Basis", basis, "Destroy", CeedBasisDestroy_Cuda)); + CeedCallBackend(CeedDestroy(&ceed)); return CEED_ERROR_SUCCESS; } @@ -503,6 +510,7 @@ int CeedBasisCreateH1_Cuda(CeedElemTopology topo, CeedInt dim, CeedInt num_nodes CeedCallBackend(CeedSetBackendFunction(ceed, "Basis", basis, "Apply", CeedBasisApplyNonTensor_Cuda)); CeedCallBackend(CeedSetBackendFunction(ceed, "Basis", basis, "ApplyAdd", CeedBasisApplyAddNonTensor_Cuda)); CeedCallBackend(CeedSetBackendFunction(ceed, "Basis", basis, "Destroy", CeedBasisDestroyNonTensor_Cuda)); + CeedCallBackend(CeedDestroy(&ceed)); return CEED_ERROR_SUCCESS; } @@ -557,6 +565,7 @@ int CeedBasisCreateHdiv_Cuda(CeedElemTopology topo, CeedInt dim, CeedInt num_nod CeedCallBackend(CeedSetBackendFunction(ceed, "Basis", basis, "Apply", CeedBasisApplyNonTensor_Cuda)); CeedCallBackend(CeedSetBackendFunction(ceed, "Basis", basis, "ApplyAdd", CeedBasisApplyAddNonTensor_Cuda)); CeedCallBackend(CeedSetBackendFunction(ceed, "Basis", basis, "Destroy", CeedBasisDestroyNonTensor_Cuda)); + CeedCallBackend(CeedDestroy(&ceed)); return CEED_ERROR_SUCCESS; } @@ -611,6 +620,7 @@ int CeedBasisCreateHcurl_Cuda(CeedElemTopology topo, CeedInt dim, CeedInt num_no CeedCallBackend(CeedSetBackendFunction(ceed, "Basis", basis, "Apply", CeedBasisApplyNonTensor_Cuda)); CeedCallBackend(CeedSetBackendFunction(ceed, "Basis", basis, "ApplyAdd", CeedBasisApplyAddNonTensor_Cuda)); CeedCallBackend(CeedSetBackendFunction(ceed, "Basis", basis, "Destroy", CeedBasisDestroyNonTensor_Cuda)); + CeedCallBackend(CeedDestroy(&ceed)); return CEED_ERROR_SUCCESS; } diff --git a/backends/cuda-ref/ceed-cuda-ref-operator.c b/backends/cuda-ref/ceed-cuda-ref-operator.c index 8cb8855ba7..ac7b1cad03 100644 --- a/backends/cuda-ref/ceed-cuda-ref-operator.c +++ b/backends/cuda-ref/ceed-cuda-ref-operator.c @@ -78,10 +78,11 @@ static int CeedOperatorDestroy_Cuda(CeedOperator op) { CeedCallCuda(ceed, cudaFree(impl->diag->d_div_out)); CeedCallCuda(ceed, cudaFree(impl->diag->d_curl_in)); CeedCallCuda(ceed, cudaFree(impl->diag->d_curl_out)); - CeedCallBackend(CeedElemRestrictionDestroy(&impl->diag->diag_rstr)); - CeedCallBackend(CeedElemRestrictionDestroy(&impl->diag->point_block_diag_rstr)); + CeedCallBackend(CeedDestroy(&ceed)); CeedCallBackend(CeedVectorDestroy(&impl->diag->elem_diag)); CeedCallBackend(CeedVectorDestroy(&impl->diag->point_block_elem_diag)); + CeedCallBackend(CeedElemRestrictionDestroy(&impl->diag->diag_rstr)); + CeedCallBackend(CeedElemRestrictionDestroy(&impl->diag->point_block_diag_rstr)); } CeedCallBackend(CeedFree(&impl->diag)); @@ -92,6 +93,7 @@ static int CeedOperatorDestroy_Cuda(CeedOperator op) { CeedCallCuda(ceed, cuModuleUnload(impl->asmb->module)); CeedCallCuda(ceed, cudaFree(impl->asmb->d_B_in)); CeedCallCuda(ceed, cudaFree(impl->asmb->d_B_out)); + CeedCallBackend(CeedDestroy(&ceed)); } CeedCallBackend(CeedFree(&impl->asmb)); @@ -227,6 +229,7 @@ static int CeedOperatorSetupFields_Cuda(CeedQFunction qf, CeedOperator op, bool CeedCallBackend(CeedElemRestrictionDestroy(&rstr_i)); } } + CeedCallBackend(CeedDestroy(&ceed)); return CEED_ERROR_SUCCESS; } @@ -234,7 +237,6 @@ static int CeedOperatorSetupFields_Cuda(CeedQFunction qf, CeedOperator op, bool // CeedOperator needs to connect all the named fields (be they active or passive) to the named inputs and outputs of its CeedQFunction. //------------------------------------------------------------------------------ static int CeedOperatorSetup_Cuda(CeedOperator op) { - Ceed ceed; bool is_setup_done; CeedInt Q, num_elem, num_input_fields, num_output_fields; CeedQFunctionField *qf_input_fields, *qf_output_fields; @@ -245,7 +247,6 @@ static int CeedOperatorSetup_Cuda(CeedOperator op) { CeedCallBackend(CeedOperatorIsSetupDone(op, &is_setup_done)); if (is_setup_done) return CEED_ERROR_SUCCESS; - CeedCallBackend(CeedOperatorGetCeed(op, &ceed)); CeedCallBackend(CeedOperatorGetData(op, &impl)); CeedCallBackend(CeedOperatorGetQFunction(op, &qf)); CeedCallBackend(CeedOperatorGetNumQuadraturePoints(op, &Q)); @@ -587,11 +588,7 @@ static int CeedOperatorApplyAdd_Cuda(CeedOperator op, CeedVector in_vec, CeedVec } // Restrict - if (impl->skip_rstr_out[field]) { - if (!is_active) CeedCallBackend(CeedVectorDestroy(&l_vec)); - continue; - } - { + if (!impl->skip_rstr_out[field]) { CeedElemRestriction elem_rstr; CeedCallBackend(CeedOperatorFieldGetElemRestriction(op_output_fields[field], &elem_rstr)); @@ -603,6 +600,7 @@ static int CeedOperatorApplyAdd_Cuda(CeedOperator op, CeedVector in_vec, CeedVec // Return work vector CeedCallBackend(CeedRestoreWorkVector(ceed, &active_e_vec)); + CeedCallBackend(CeedDestroy(&ceed)); return CEED_ERROR_SUCCESS; } @@ -610,7 +608,6 @@ static int CeedOperatorApplyAdd_Cuda(CeedOperator op, CeedVector in_vec, CeedVec // CeedOperator needs to connect all the named fields (be they active or passive) to the named inputs and outputs of its CeedQFunction. //------------------------------------------------------------------------------ static int CeedOperatorSetupAtPoints_Cuda(CeedOperator op) { - Ceed ceed; bool is_setup_done; CeedInt max_num_points = -1, num_elem, num_input_fields, num_output_fields; CeedQFunctionField *qf_input_fields, *qf_output_fields; @@ -621,7 +618,6 @@ static int CeedOperatorSetupAtPoints_Cuda(CeedOperator op) { CeedCallBackend(CeedOperatorIsSetupDone(op, &is_setup_done)); if (is_setup_done) return CEED_ERROR_SUCCESS; - CeedCallBackend(CeedOperatorGetCeed(op, &ceed)); CeedCallBackend(CeedOperatorGetData(op, &impl)); CeedCallBackend(CeedOperatorGetQFunction(op, &qf)); CeedCallBackend(CeedOperatorGetNumElements(op, &num_elem)); @@ -918,11 +914,7 @@ static int CeedOperatorApplyAddAtPoints_Cuda(CeedOperator op, CeedVector in_vec, } // Restrict - if (impl->skip_rstr_out[field]) { - if (!is_active) CeedCallBackend(CeedVectorDestroy(&l_vec)); - continue; - } - { + if (!impl->skip_rstr_out[field]) { CeedElemRestriction elem_rstr; CeedCallBackend(CeedOperatorFieldGetElemRestriction(op_output_fields[field], &elem_rstr)); @@ -934,6 +926,7 @@ static int CeedOperatorApplyAddAtPoints_Cuda(CeedOperator op, CeedVector in_vec, // Restore work vector CeedCallBackend(CeedRestoreWorkVector(ceed, &active_e_vec)); + CeedCallBackend(CeedDestroy(&ceed)); return CEED_ERROR_SUCCESS; } @@ -1075,6 +1068,8 @@ static inline int CeedOperatorLinearAssembleQFunctionCore_Cuda(CeedOperator op, } // Restore output + CeedCallBackend(CeedDestroy(&ceed)); + CeedCallBackend(CeedDestroy(&ceed_parent)); CeedCallBackend(CeedVectorRestoreArray(*assembled, &assembled_array)); return CEED_ERROR_SUCCESS; } @@ -1276,6 +1271,7 @@ static inline int CeedOperatorAssembleDiagonalSetup_Cuda(CeedOperator op) { CeedCallCuda(ceed, cudaMemcpy(diag->d_eval_modes_out, eval_modes_out, num_eval_modes_out * eval_modes_bytes, cudaMemcpyHostToDevice)); CeedCallBackend(CeedFree(&eval_modes_in)); CeedCallBackend(CeedFree(&eval_modes_out)); + CeedCallBackend(CeedDestroy(&ceed)); CeedCallBackend(CeedBasisDestroy(&basis_in)); CeedCallBackend(CeedBasisDestroy(&basis_out)); return CEED_ERROR_SUCCESS; @@ -1361,6 +1357,7 @@ static inline int CeedOperatorAssembleDiagonalSetupCompile_Cuda(CeedOperator op, num_eval_modes_out, "NUM_COMP", num_comp, "NUM_NODES", num_nodes, "NUM_QPTS", num_qpts, "USE_CEEDSIZE", use_ceedsize_idx, "USE_POINT_BLOCK", is_point_block ? 1 : 0, "BLOCK_SIZE", num_nodes * elems_per_block)); CeedCallCuda(ceed, CeedGetKernel_Cuda(ceed, *module, "LinearDiagonal", is_point_block ? &diag->LinearPointBlock : &diag->LinearDiagonal)); + CeedCallBackend(CeedDestroy(&ceed)); CeedCallBackend(CeedBasisDestroy(&basis_in)); CeedCallBackend(CeedBasisDestroy(&basis_out)); return CEED_ERROR_SUCCESS; @@ -1449,6 +1446,7 @@ static inline int CeedOperatorAssembleDiagonalCore_Cuda(CeedOperator op, CeedVec CeedCallBackend(CeedElemRestrictionApply(diag_rstr, CEED_TRANSPOSE, elem_diag, assembled, request)); // Cleanup + CeedCallBackend(CeedDestroy(&ceed)); CeedCallBackend(CeedVectorDestroy(&assembled_qf)); return CEED_ERROR_SUCCESS; } @@ -1661,6 +1659,7 @@ static int CeedSingleOperatorAssembleSetup_Cuda(CeedOperator op, CeedInt use_cee CeedCallBackend(CeedFree(&identity)); } CeedCallBackend(CeedFree(&eval_modes_out)); + CeedCallBackend(CeedDestroy(&ceed)); CeedCallBackend(CeedElemRestrictionDestroy(&rstr_in)); CeedCallBackend(CeedElemRestrictionDestroy(&rstr_out)); CeedCallBackend(CeedBasisDestroy(&basis_in)); @@ -1769,6 +1768,7 @@ static int CeedSingleOperatorAssemble_Cuda(CeedOperator op, CeedInt offset, Ceed CeedCallBackend(CeedElemRestrictionRestoreCurlOrientations(rstr_out, &curl_orients_out)); } } + CeedCallBackend(CeedDestroy(&ceed)); CeedCallBackend(CeedElemRestrictionDestroy(&rstr_in)); CeedCallBackend(CeedElemRestrictionDestroy(&rstr_out)); return CEED_ERROR_SUCCESS; @@ -2040,6 +2040,7 @@ static int CeedOperatorLinearAssembleAddDiagonalAtPoints_Cuda(CeedOperator op, C // Restore work vector CeedCallBackend(CeedRestoreWorkVector(ceed, &active_e_vec_in)); CeedCallBackend(CeedRestoreWorkVector(ceed, &active_e_vec_out)); + CeedCallBackend(CeedDestroy(&ceed)); return CEED_ERROR_SUCCESS; } @@ -2062,6 +2063,7 @@ int CeedOperatorCreate_Cuda(CeedOperator op) { CeedCallBackend(CeedSetBackendFunction(ceed, "Operator", op, "LinearAssembleSingle", CeedSingleOperatorAssemble_Cuda)); CeedCallBackend(CeedSetBackendFunction(ceed, "Operator", op, "ApplyAdd", CeedOperatorApplyAdd_Cuda)); CeedCallBackend(CeedSetBackendFunction(ceed, "Operator", op, "Destroy", CeedOperatorDestroy_Cuda)); + CeedCallBackend(CeedDestroy(&ceed)); return CEED_ERROR_SUCCESS; } @@ -2080,6 +2082,7 @@ int CeedOperatorCreateAtPoints_Cuda(CeedOperator op) { CeedCallBackend(CeedSetBackendFunction(ceed, "Operator", op, "LinearAssembleAddDiagonal", CeedOperatorLinearAssembleAddDiagonalAtPoints_Cuda)); CeedCallBackend(CeedSetBackendFunction(ceed, "Operator", op, "ApplyAdd", CeedOperatorApplyAddAtPoints_Cuda)); CeedCallBackend(CeedSetBackendFunction(ceed, "Operator", op, "Destroy", CeedOperatorDestroy_Cuda)); + CeedCallBackend(CeedDestroy(&ceed)); return CEED_ERROR_SUCCESS; } diff --git a/backends/cuda-ref/ceed-cuda-ref-qfunction-load.cpp b/backends/cuda-ref/ceed-cuda-ref-qfunction-load.cpp index 2d5540ead8..4eaa18d1c2 100644 --- a/backends/cuda-ref/ceed-cuda-ref-qfunction-load.cpp +++ b/backends/cuda-ref/ceed-cuda-ref-qfunction-load.cpp @@ -28,12 +28,12 @@ extern "C" int CeedQFunctionBuildKernel_Cuda_ref(CeedQFunction qf) { CeedQFunctionField *input_fields, *output_fields; CeedQFunction_Cuda *data; - CeedCallBackend(CeedQFunctionGetCeed(qf, &ceed)); - CeedCallBackend(CeedQFunctionGetData(qf, (void **)&data)); - // QFunction is built + CeedCallBackend(CeedQFunctionGetData(qf, (void **)&data)); if (data->QFunction) return CEED_ERROR_SUCCESS; + CeedCallBackend(CeedQFunctionGetCeed(qf, &ceed)); + // QFunction kernel generation CeedCallBackend(CeedQFunctionGetFields(qf, &num_input_fields, &input_fields, &num_output_fields, &output_fields)); @@ -112,6 +112,7 @@ extern "C" int CeedQFunctionBuildKernel_Cuda_ref(CeedQFunction qf) { // Compile kernel CeedCallBackend(CeedCompile_Cuda(ceed, code.str().c_str(), &data->module, 0)); CeedCallBackend(CeedGetKernel_Cuda(ceed, data->module, kernel_name.c_str(), &data->QFunction)); + CeedCallBackend(CeedDestroy(&ceed)); return CEED_ERROR_SUCCESS; } diff --git a/backends/cuda-ref/ceed-cuda-ref-qfunction.c b/backends/cuda-ref/ceed-cuda-ref-qfunction.c index eec4aea26c..32a02b43dd 100644 --- a/backends/cuda-ref/ceed-cuda-ref-qfunction.c +++ b/backends/cuda-ref/ceed-cuda-ref-qfunction.c @@ -58,6 +58,7 @@ static int CeedQFunctionApply_Cuda(CeedQFunction qf, CeedInt Q, CeedVector *U, C // Restore context CeedCallBackend(CeedQFunctionRestoreInnerContextData(qf, &data->d_c)); + CeedCallBackend(CeedDestroy(&ceed)); return CEED_ERROR_SUCCESS; } @@ -95,13 +96,13 @@ int CeedQFunctionCreate_Cuda(CeedQFunction qf) { CeedCallBackend(CeedCalloc(1, &data)); CeedCallBackend(CeedQFunctionSetData(qf, data)); - // Read QFunction name CeedCallBackend(CeedQFunctionGetKernelName(qf, &data->qfunction_name)); // Register backend functions CeedCallBackend(CeedSetBackendFunction(ceed, "QFunction", qf, "Apply", CeedQFunctionApply_Cuda)); CeedCallBackend(CeedSetBackendFunction(ceed, "QFunction", qf, "Destroy", CeedQFunctionDestroy_Cuda)); CeedCallBackend(CeedSetBackendFunction(ceed, "QFunction", qf, "SetCUDAUserFunction", CeedQFunctionSetCUDAUserFunction_Cuda)); + CeedCallBackend(CeedDestroy(&ceed)); return CEED_ERROR_SUCCESS; } diff --git a/backends/cuda-ref/ceed-cuda-ref-qfunctioncontext.c b/backends/cuda-ref/ceed-cuda-ref-qfunctioncontext.c index 4257265987..5afbb7aa02 100644 --- a/backends/cuda-ref/ceed-cuda-ref-qfunctioncontext.c +++ b/backends/cuda-ref/ceed-cuda-ref-qfunctioncontext.c @@ -37,6 +37,7 @@ static inline int CeedQFunctionContextSyncH2D_Cuda(const CeedQFunctionContext ct impl->d_data = impl->d_data_owned; } CeedCallCuda(ceed, cudaMemcpy(impl->d_data, impl->h_data, ctx_size, cudaMemcpyHostToDevice)); + CeedCallBackend(CeedDestroy(&ceed)); return CEED_ERROR_SUCCESS; } @@ -64,6 +65,7 @@ static inline int CeedQFunctionContextSyncD2H_Cuda(const CeedQFunctionContext ct impl->h_data = impl->h_data_owned; } CeedCallCuda(ceed, cudaMemcpy(impl->h_data, impl->d_data, ctx_size, cudaMemcpyDeviceToHost)); + CeedCallBackend(CeedDestroy(&ceed)); return CEED_ERROR_SUCCESS; } @@ -205,6 +207,7 @@ static int CeedQFunctionContextSetDataDevice_Cuda(const CeedQFunctionContext ctx impl->d_data = data; break; } + CeedCallBackend(CeedDestroy(&ceed)); return CEED_ERROR_SUCCESS; } @@ -335,6 +338,7 @@ int CeedQFunctionContextCreate_Cuda(CeedQFunctionContext ctx) { CeedCallBackend(CeedSetBackendFunction(ceed, "QFunctionContext", ctx, "GetData", CeedQFunctionContextGetData_Cuda)); CeedCallBackend(CeedSetBackendFunction(ceed, "QFunctionContext", ctx, "GetDataRead", CeedQFunctionContextGetDataRead_Cuda)); CeedCallBackend(CeedSetBackendFunction(ceed, "QFunctionContext", ctx, "Destroy", CeedQFunctionContextDestroy_Cuda)); + CeedCallBackend(CeedDestroy(&ceed)); CeedCallBackend(CeedCalloc(1, &impl)); CeedCallBackend(CeedQFunctionContextSetBackendData(ctx, impl)); return CEED_ERROR_SUCCESS; diff --git a/backends/cuda-ref/ceed-cuda-ref-restriction.c b/backends/cuda-ref/ceed-cuda-ref-restriction.c index c4a5c22dda..3ec1a4ef90 100644 --- a/backends/cuda-ref/ceed-cuda-ref-restriction.c +++ b/backends/cuda-ref/ceed-cuda-ref-restriction.c @@ -106,6 +106,7 @@ static inline int CeedElemRestrictionSetupCompile_Cuda(CeedElemRestriction rstr) CeedCallBackend(CeedGetKernel_Cuda(ceed, impl->module, "OffsetTranspose", &impl->ApplyUnorientedTranspose)); } break; } + CeedCallBackend(CeedDestroy(&ceed)); return CEED_ERROR_SUCCESS; } @@ -285,6 +286,7 @@ static inline int CeedElemRestrictionApply_Cuda_Core(CeedElemRestriction rstr, C // Restore arrays CeedCallBackend(CeedVectorRestoreArrayRead(u, &d_u)); CeedCallBackend(CeedVectorRestoreArray(v, &d_v)); + CeedCallBackend(CeedDestroy(&ceed)); return CEED_ERROR_SUCCESS; } @@ -404,6 +406,7 @@ static int CeedElemRestrictionDestroy_Cuda(CeedElemRestriction rstr) { CeedCallBackend(CeedFree(&impl->h_points_per_elem_owned)); CeedCallCuda(ceed, cudaFree((CeedInt *)impl->d_points_per_elem_owned)); CeedCallBackend(CeedFree(&impl)); + CeedCallBackend(CeedDestroy(&ceed)); return CEED_ERROR_SUCCESS; } @@ -485,6 +488,7 @@ static int CeedElemRestrictionOffset_Cuda(const CeedElemRestriction rstr, const CeedCallBackend(CeedFree(&l_vec_indices)); CeedCallBackend(CeedFree(&t_offsets)); CeedCallBackend(CeedFree(&t_indices)); + CeedCallBackend(CeedDestroy(&ceed)); return CEED_ERROR_SUCCESS; } @@ -502,6 +506,7 @@ int CeedElemRestrictionCreate_Cuda(CeedMemType mem_type, CeedCopyMode copy_mode, CeedCallBackend(CeedElemRestrictionGetCeed(rstr, &ceed)); CeedCallBackend(CeedGetParent(ceed, &ceed_parent)); CeedCallBackend(CeedIsDeterministic(ceed_parent, &is_deterministic)); + CeedCallBackend(CeedDestroy(&ceed_parent)); CeedCallBackend(CeedElemRestrictionGetNumElements(rstr, &num_elem)); CeedCallBackend(CeedElemRestrictionGetNumComponents(rstr, &num_comp)); CeedCallBackend(CeedElemRestrictionGetElementSize(rstr, &elem_size)); @@ -649,6 +654,7 @@ int CeedElemRestrictionCreate_Cuda(CeedMemType mem_type, CeedCopyMode copy_mode, CeedSetBackendFunction(ceed, "ElemRestriction", rstr, "GetAtPointsElementOffset", CeedElemRestrictionGetAtPointsElementOffset_Cuda)); } CeedCallBackend(CeedSetBackendFunction(ceed, "ElemRestriction", rstr, "Destroy", CeedElemRestrictionDestroy_Cuda)); + CeedCallBackend(CeedDestroy(&ceed)); return CEED_ERROR_SUCCESS; } diff --git a/backends/cuda-ref/ceed-cuda-ref-vector.c b/backends/cuda-ref/ceed-cuda-ref-vector.c index 9deb6dec82..7365327229 100644 --- a/backends/cuda-ref/ceed-cuda-ref-vector.c +++ b/backends/cuda-ref/ceed-cuda-ref-vector.c @@ -41,10 +41,8 @@ static inline int CeedVectorNeedSync_Cuda(const CeedVector vec, CeedMemType mem_ static inline int CeedVectorSyncH2D_Cuda(const CeedVector vec) { CeedSize length; size_t bytes; - Ceed ceed; CeedVector_Cuda *impl; - CeedCallBackend(CeedVectorGetCeed(vec, &ceed)); CeedCallBackend(CeedVectorGetData(vec, &impl)); CeedCheck(impl->h_array, CeedVectorReturnCeed(vec), CEED_ERROR_BACKEND, "No valid host data to sync to device"); @@ -56,10 +54,10 @@ static inline int CeedVectorSyncH2D_Cuda(const CeedVector vec) { } else if (impl->d_array_owned) { impl->d_array = impl->d_array_owned; } else { - CeedCallCuda(ceed, cudaMalloc((void **)&impl->d_array_owned, bytes)); + CeedCallCuda(CeedVectorReturnCeed(vec), cudaMalloc((void **)&impl->d_array_owned, bytes)); impl->d_array = impl->d_array_owned; } - CeedCallCuda(ceed, cudaMemcpy(impl->d_array, impl->h_array, bytes, cudaMemcpyHostToDevice)); + CeedCallCuda(CeedVectorReturnCeed(vec), cudaMemcpy(impl->d_array, impl->h_array, bytes, cudaMemcpyHostToDevice)); return CEED_ERROR_SUCCESS; } @@ -68,13 +66,11 @@ static inline int CeedVectorSyncH2D_Cuda(const CeedVector vec) { //------------------------------------------------------------------------------ static inline int CeedVectorSyncD2H_Cuda(const CeedVector vec) { CeedSize length; - Ceed ceed; CeedVector_Cuda *impl; - CeedCallBackend(CeedVectorGetCeed(vec, &ceed)); CeedCallBackend(CeedVectorGetData(vec, &impl)); - CeedCheck(impl->d_array, ceed, CEED_ERROR_BACKEND, "No valid device data to sync to host"); + CeedCheck(impl->d_array, CeedVectorReturnCeed(vec), CEED_ERROR_BACKEND, "No valid device data to sync to host"); if (impl->h_array_borrowed) { impl->h_array = impl->h_array_borrowed; @@ -91,7 +87,7 @@ static inline int CeedVectorSyncD2H_Cuda(const CeedVector vec) { CeedCallBackend(CeedVectorGetLength(vec, &length)); size_t bytes = length * sizeof(CeedScalar); - CeedCallCuda(ceed, cudaMemcpy(impl->h_array, impl->d_array, bytes, cudaMemcpyDeviceToHost)); + CeedCallCuda(CeedVectorReturnCeed(vec), cudaMemcpy(impl->h_array, impl->d_array, bytes, cudaMemcpyDeviceToHost)); return CEED_ERROR_SUCCESS; } @@ -202,6 +198,7 @@ static int CeedVectorSetArrayDevice_Cuda(const CeedVector vec, const CeedCopyMod CeedCallBackend(CeedSetDeviceCeedScalarArray_Cuda(ceed, array, copy_mode, length, (const CeedScalar **)&impl->d_array_owned, (const CeedScalar **)&impl->d_array_borrowed, (const CeedScalar **)&impl->d_array)); + CeedCallBackend(CeedDestroy(&ceed)); return CEED_ERROR_SUCCESS; } @@ -611,6 +608,7 @@ static int CeedVectorNorm_Cuda(CeedVector vec, CeedNormType type, CeedScalar *no } } CeedCallBackend(CeedVectorRestoreArrayRead(vec, &d_array)); + CeedCallBackend(CeedDestroy(&ceed)); return CEED_ERROR_SUCCESS; } @@ -689,11 +687,9 @@ int CeedDeviceAXPY_Cuda(CeedScalar *y_array, CeedScalar alpha, CeedScalar *x_arr // Compute y = alpha x + y //------------------------------------------------------------------------------ static int CeedVectorAXPY_Cuda(CeedVector y, CeedScalar alpha, CeedVector x) { - Ceed ceed; CeedSize length; CeedVector_Cuda *y_impl, *x_impl; - CeedCallBackend(CeedVectorGetCeed(y, &ceed)); CeedCallBackend(CeedVectorGetData(y, &y_impl)); CeedCallBackend(CeedVectorGetData(x, &x_impl)); CeedCallBackend(CeedVectorGetLength(y, &length)); @@ -824,6 +820,7 @@ int CeedVectorCreate_Cuda(CeedSize n, CeedVector vec) { CeedCallBackend(CeedSetBackendFunction(ceed, "Vector", vec, "AXPBY", CeedVectorAXPBY_Cuda)); CeedCallBackend(CeedSetBackendFunction(ceed, "Vector", vec, "PointwiseMult", CeedVectorPointwiseMult_Cuda)); CeedCallBackend(CeedSetBackendFunction(ceed, "Vector", vec, "Destroy", CeedVectorDestroy_Cuda)); + CeedCallBackend(CeedDestroy(&ceed)); CeedCallBackend(CeedCalloc(1, &impl)); CeedCallBackend(CeedVectorSetData(vec, impl)); return CEED_ERROR_SUCCESS; diff --git a/backends/cuda-shared/ceed-cuda-shared-basis.c b/backends/cuda-shared/ceed-cuda-shared-basis.c index b1709787ec..bd2467e538 100644 --- a/backends/cuda-shared/ceed-cuda-shared-basis.c +++ b/backends/cuda-shared/ceed-cuda-shared-basis.c @@ -189,6 +189,7 @@ static int CeedBasisApplyTensorCore_Cuda_shared(CeedBasis basis, bool apply_add, CeedCallBackend(CeedVectorRestoreArray(v, &d_v)); if (eval_mode == CEED_EVAL_NONE) CeedCallBackend(CeedVectorSetArray(v, CEED_MEM_DEVICE, CEED_COPY_VALUES, (CeedScalar *)d_u)); if (eval_mode != CEED_EVAL_WEIGHT) CeedCallBackend(CeedVectorRestoreArrayRead(u, &d_u)); + CeedCallBackend(CeedDestroy(&ceed)); return CEED_ERROR_SUCCESS; } @@ -217,7 +218,6 @@ static int CeedBasisApplyAtPointsCore_Cuda_shared(CeedBasis basis, bool apply_ad CeedScalar *d_v; CeedBasis_Cuda_shared *data; - CeedCallBackend(CeedBasisGetCeed(basis, &ceed)); CeedCallBackend(CeedBasisGetData(basis, &data)); CeedCallBackend(CeedBasisGetNumQuadraturePoints1D(basis, &Q_1d)); CeedCallBackend(CeedBasisGetDimension(basis, &dim)); @@ -228,6 +228,8 @@ static int CeedBasisApplyAtPointsCore_Cuda_shared(CeedBasis basis, bool apply_ad return CEED_ERROR_SUCCESS; } + CeedCallBackend(CeedBasisGetCeed(basis, &ceed)); + // Check padded to uniform number of points per elem for (CeedInt i = 1; i < num_elem; i++) max_num_points = CeedIntMax(max_num_points, num_points[i]); { @@ -345,6 +347,7 @@ static int CeedBasisApplyAtPointsCore_Cuda_shared(CeedBasis basis, bool apply_ad CeedCallBackend(CeedVectorRestoreArray(v, &d_v)); if (eval_mode == CEED_EVAL_NONE) CeedCallBackend(CeedVectorSetArray(v, CEED_MEM_DEVICE, CEED_COPY_VALUES, (CeedScalar *)d_u)); if (eval_mode != CEED_EVAL_WEIGHT) CeedCallBackend(CeedVectorRestoreArrayRead(u, &d_u)); + CeedCallBackend(CeedDestroy(&ceed)); return CEED_ERROR_SUCCESS; } @@ -379,6 +382,7 @@ static int CeedBasisDestroy_Cuda_shared(CeedBasis basis) { CeedCallCuda(ceed, cudaFree(data->d_collo_grad_1d)); CeedCallCuda(ceed, cudaFree(data->d_chebyshev_interp_1d)); CeedCallBackend(CeedFree(&data)); + CeedCallBackend(CeedDestroy(&ceed)); return CEED_ERROR_SUCCESS; } @@ -443,6 +447,7 @@ int CeedBasisCreateTensorH1_Cuda_shared(CeedInt dim, CeedInt P_1d, CeedInt Q_1d, CeedCallBackend(CeedSetBackendFunction(ceed, "Basis", basis, "ApplyAtPoints", CeedBasisApplyAtPoints_Cuda_shared)); CeedCallBackend(CeedSetBackendFunction(ceed, "Basis", basis, "ApplyAddAtPoints", CeedBasisApplyAddAtPoints_Cuda_shared)); CeedCallBackend(CeedSetBackendFunction(ceed, "Basis", basis, "Destroy", CeedBasisDestroy_Cuda_shared)); + CeedCallBackend(CeedDestroy(&ceed)); return CEED_ERROR_SUCCESS; } diff --git a/backends/cuda-shared/ceed-cuda-shared.c b/backends/cuda-shared/ceed-cuda-shared.c index 5ab65815cc..baa374af36 100644 --- a/backends/cuda-shared/ceed-cuda-shared.c +++ b/backends/cuda-shared/ceed-cuda-shared.c @@ -33,6 +33,7 @@ static int CeedInit_Cuda_shared(const char *resource, Ceed ceed) { CeedCallBackend(CeedInit("/gpu/cuda/ref", &ceed_ref)); CeedCallBackend(CeedSetDelegate(ceed, ceed_ref)); + CeedCallBackend(CeedDestroy(&ceed_ref)); CeedCallBackend(CeedSetBackendFunction(ceed, "Ceed", ceed, "BasisCreateTensorH1", CeedBasisCreateTensorH1_Cuda_shared)); CeedCallBackend(CeedSetBackendFunction(ceed, "Ceed", ceed, "Destroy", CeedDestroy_Cuda)); diff --git a/backends/hip-gen/ceed-hip-gen-operator-build.cpp b/backends/hip-gen/ceed-hip-gen-operator-build.cpp index f1a876ce26..6a13cd5600 100644 --- a/backends/hip-gen/ceed-hip-gen-operator-build.cpp +++ b/backends/hip-gen/ceed-hip-gen-operator-build.cpp @@ -908,8 +908,8 @@ extern "C" int CeedOperatorBuildKernel_Hip_gen(CeedOperator op) { CeedCallBackend(CeedCompile_Hip(ceed, code.str().c_str(), &data->module, 2, "T_1D", block_sizes[0], "BLOCK_SIZE", block_sizes[0] * block_sizes[1] * block_sizes[2])); CeedCallBackend(CeedGetKernel_Hip(ceed, data->module, operator_name.c_str(), &data->op)); - CeedCallBackend(CeedOperatorSetSetupDone(op)); + CeedCallBackend(CeedDestroy(&ceed)); return CEED_ERROR_SUCCESS; } diff --git a/backends/hip-gen/ceed-hip-gen-operator.c b/backends/hip-gen/ceed-hip-gen-operator.c index fcd58ed76d..a61d1df32f 100644 --- a/backends/hip-gen/ceed-hip-gen-operator.c +++ b/backends/hip-gen/ceed-hip-gen-operator.c @@ -58,6 +58,7 @@ static int CeedOperatorApplyAdd_Hip_gen(CeedOperator op, CeedVector input_vec, C CeedOperator op_fallback; CeedDebug256(ceed, CEED_DEBUG_COLOR_SUCCESS, "Falling back to /gpu/hip/ref CeedOperator due to non-tensor bases"); + CeedCallBackend(CeedDestroy(&ceed)); CeedCallBackend(CeedOperatorGetFallback(op, &op_fallback)); CeedCallBackend(CeedOperatorApplyAdd(op_fallback, input_vec, output_vec, request)); return CEED_ERROR_SUCCESS; @@ -177,6 +178,7 @@ static int CeedOperatorApplyAdd_Hip_gen(CeedOperator op, CeedVector input_vec, C // Restore context data CeedCallBackend(CeedQFunctionRestoreInnerContextData(qf, &qf_data->d_c)); + CeedCallBackend(CeedDestroy(&ceed)); return CEED_ERROR_SUCCESS; } @@ -192,6 +194,7 @@ int CeedOperatorCreate_Hip_gen(CeedOperator op) { CeedCallBackend(CeedOperatorSetData(op, impl)); CeedCallBackend(CeedSetBackendFunction(ceed, "Operator", op, "ApplyAdd", CeedOperatorApplyAdd_Hip_gen)); CeedCallBackend(CeedSetBackendFunction(ceed, "Operator", op, "Destroy", CeedOperatorDestroy_Hip_gen)); + CeedCallBackend(CeedDestroy(&ceed)); return CEED_ERROR_SUCCESS; } diff --git a/backends/hip-gen/ceed-hip-gen-qfunction.c b/backends/hip-gen/ceed-hip-gen-qfunction.c index 32d5653b98..6da2c1d10e 100644 --- a/backends/hip-gen/ceed-hip-gen-qfunction.c +++ b/backends/hip-gen/ceed-hip-gen-qfunction.c @@ -42,11 +42,11 @@ int CeedQFunctionCreate_Hip_gen(CeedQFunction qf) { CeedCallBackend(CeedCalloc(1, &data)); CeedCallBackend(CeedQFunctionSetData(qf, data)); - // Read QFunction source CeedCallBackend(CeedQFunctionGetKernelName(qf, &data->qfunction_name)); CeedCallBackend(CeedSetBackendFunction(ceed, "QFunction", qf, "Apply", CeedQFunctionApply_Hip_gen)); CeedCallBackend(CeedSetBackendFunction(ceed, "QFunction", qf, "Destroy", CeedQFunctionDestroy_Hip_gen)); + CeedCallBackend(CeedDestroy(&ceed)); return CEED_ERROR_SUCCESS; } diff --git a/backends/hip-gen/ceed-hip-gen.c b/backends/hip-gen/ceed-hip-gen.c index d66ceb041a..e867b4eb9e 100644 --- a/backends/hip-gen/ceed-hip-gen.c +++ b/backends/hip-gen/ceed-hip-gen.c @@ -33,6 +33,7 @@ static int CeedInit_Hip_gen(const char *resource, Ceed ceed) { CeedCallBackend(CeedInit("/gpu/hip/shared", &ceed_shared)); CeedCallBackend(CeedSetDelegate(ceed, ceed_shared)); + CeedCallBackend(CeedDestroy(&ceed_shared)); CeedCallBackend(CeedSetOperatorFallbackResource(ceed, fallback_resource)); diff --git a/backends/hip-ref/ceed-hip-ref-basis.c b/backends/hip-ref/ceed-hip-ref-basis.c index 7e7f0e97e4..8fb3d3fa20 100644 --- a/backends/hip-ref/ceed-hip-ref-basis.c +++ b/backends/hip-ref/ceed-hip-ref-basis.c @@ -87,6 +87,7 @@ static int CeedBasisApplyCore_Hip(CeedBasis basis, bool apply_add, const CeedInt CeedCallBackend(CeedVectorRestoreArray(v, &d_v)); if (eval_mode == CEED_EVAL_NONE) CeedCallBackend(CeedVectorSetArray(v, CEED_MEM_DEVICE, CEED_COPY_VALUES, (CeedScalar *)d_u)); if (eval_mode != CEED_EVAL_WEIGHT) CeedCallBackend(CeedVectorRestoreArrayRead(u, &d_u)); + CeedCallBackend(CeedDestroy(&ceed)); return CEED_ERROR_SUCCESS; } @@ -114,7 +115,6 @@ static int CeedBasisApplyAtPointsCore_Hip(CeedBasis basis, bool apply_add, const CeedScalar *d_v; CeedBasis_Hip *data; - CeedCallBackend(CeedBasisGetCeed(basis, &ceed)); CeedCallBackend(CeedBasisGetData(basis, &data)); CeedCallBackend(CeedBasisGetNumQuadraturePoints1D(basis, &Q_1d)); CeedCallBackend(CeedBasisGetDimension(basis, &dim)); @@ -125,6 +125,8 @@ static int CeedBasisApplyAtPointsCore_Hip(CeedBasis basis, bool apply_add, const return CEED_ERROR_SUCCESS; } + CeedCallBackend(CeedBasisGetCeed(basis, &ceed)); + // Check padded to uniform number of points per elem for (CeedInt i = 1; i < num_elem; i++) max_num_points = CeedIntMax(max_num_points, num_points[i]); { @@ -242,6 +244,7 @@ static int CeedBasisApplyAtPointsCore_Hip(CeedBasis basis, bool apply_add, const CeedCallBackend(CeedVectorRestoreArray(v, &d_v)); if (eval_mode == CEED_EVAL_NONE) CeedCallBackend(CeedVectorSetArray(v, CEED_MEM_DEVICE, CEED_COPY_VALUES, (CeedScalar *)d_u)); if (eval_mode != CEED_EVAL_WEIGHT) CeedCallBackend(CeedVectorRestoreArrayRead(u, &d_u)); + CeedCallBackend(CeedDestroy(&ceed)); return CEED_ERROR_SUCCESS; } @@ -346,6 +349,7 @@ static int CeedBasisApplyNonTensorCore_Hip(CeedBasis basis, bool apply_add, cons CeedCallBackend(CeedVectorRestoreArray(v, &d_v)); if (eval_mode == CEED_EVAL_NONE) CeedCallBackend(CeedVectorSetArray(v, CEED_MEM_DEVICE, CEED_COPY_VALUES, (CeedScalar *)d_u)); if (eval_mode != CEED_EVAL_WEIGHT) CeedCallBackend(CeedVectorRestoreArrayRead(u, &d_u)); + CeedCallBackend(CeedDestroy(&ceed)); return CEED_ERROR_SUCCESS; } @@ -379,6 +383,7 @@ static int CeedBasisDestroy_Hip(CeedBasis basis) { CeedCallHip(ceed, hipFree(data->d_grad_1d)); CeedCallHip(ceed, hipFree(data->d_chebyshev_interp_1d)); CeedCallBackend(CeedFree(&data)); + CeedCallBackend(CeedDestroy(&ceed)); return CEED_ERROR_SUCCESS; } @@ -398,6 +403,7 @@ static int CeedBasisDestroyNonTensor_Hip(CeedBasis basis) { CeedCallHip(ceed, hipFree(data->d_div)); CeedCallHip(ceed, hipFree(data->d_curl)); CeedCallBackend(CeedFree(&data)); + CeedCallBackend(CeedDestroy(&ceed)); return CEED_ERROR_SUCCESS; } @@ -444,6 +450,7 @@ int CeedBasisCreateTensorH1_Hip(CeedInt dim, CeedInt P_1d, CeedInt Q_1d, const C CeedCallBackend(CeedSetBackendFunction(ceed, "Basis", basis, "ApplyAtPoints", CeedBasisApplyAtPoints_Hip)); CeedCallBackend(CeedSetBackendFunction(ceed, "Basis", basis, "ApplyAddAtPoints", CeedBasisApplyAddAtPoints_Hip)); CeedCallBackend(CeedSetBackendFunction(ceed, "Basis", basis, "Destroy", CeedBasisDestroy_Hip)); + CeedCallBackend(CeedDestroy(&ceed)); return CEED_ERROR_SUCCESS; } @@ -498,6 +505,7 @@ int CeedBasisCreateH1_Hip(CeedElemTopology topo, CeedInt dim, CeedInt num_nodes, CeedCallBackend(CeedSetBackendFunction(ceed, "Basis", basis, "Apply", CeedBasisApplyNonTensor_Hip)); CeedCallBackend(CeedSetBackendFunction(ceed, "Basis", basis, "ApplyAdd", CeedBasisApplyAddNonTensor_Hip)); CeedCallBackend(CeedSetBackendFunction(ceed, "Basis", basis, "Destroy", CeedBasisDestroyNonTensor_Hip)); + CeedCallBackend(CeedDestroy(&ceed)); return CEED_ERROR_SUCCESS; } @@ -552,6 +560,7 @@ int CeedBasisCreateHdiv_Hip(CeedElemTopology topo, CeedInt dim, CeedInt num_node CeedCallBackend(CeedSetBackendFunction(ceed, "Basis", basis, "Apply", CeedBasisApplyNonTensor_Hip)); CeedCallBackend(CeedSetBackendFunction(ceed, "Basis", basis, "ApplyAdd", CeedBasisApplyAddNonTensor_Hip)); CeedCallBackend(CeedSetBackendFunction(ceed, "Basis", basis, "Destroy", CeedBasisDestroyNonTensor_Hip)); + CeedCallBackend(CeedDestroy(&ceed)); return CEED_ERROR_SUCCESS; } @@ -606,6 +615,7 @@ int CeedBasisCreateHcurl_Hip(CeedElemTopology topo, CeedInt dim, CeedInt num_nod CeedCallBackend(CeedSetBackendFunction(ceed, "Basis", basis, "Apply", CeedBasisApplyNonTensor_Hip)); CeedCallBackend(CeedSetBackendFunction(ceed, "Basis", basis, "ApplyAdd", CeedBasisApplyAddNonTensor_Hip)); CeedCallBackend(CeedSetBackendFunction(ceed, "Basis", basis, "Destroy", CeedBasisDestroyNonTensor_Hip)); + CeedCallBackend(CeedDestroy(&ceed)); return CEED_ERROR_SUCCESS; } diff --git a/backends/hip-ref/ceed-hip-ref-operator.c b/backends/hip-ref/ceed-hip-ref-operator.c index c2e58a1e7f..04733482ee 100644 --- a/backends/hip-ref/ceed-hip-ref-operator.c +++ b/backends/hip-ref/ceed-hip-ref-operator.c @@ -77,10 +77,11 @@ static int CeedOperatorDestroy_Hip(CeedOperator op) { CeedCallHip(ceed, hipFree(impl->diag->d_div_out)); CeedCallHip(ceed, hipFree(impl->diag->d_curl_in)); CeedCallHip(ceed, hipFree(impl->diag->d_curl_out)); - CeedCallBackend(CeedElemRestrictionDestroy(&impl->diag->diag_rstr)); - CeedCallBackend(CeedElemRestrictionDestroy(&impl->diag->point_block_diag_rstr)); + CeedCallBackend(CeedDestroy(&ceed)); CeedCallBackend(CeedVectorDestroy(&impl->diag->elem_diag)); CeedCallBackend(CeedVectorDestroy(&impl->diag->point_block_elem_diag)); + CeedCallBackend(CeedElemRestrictionDestroy(&impl->diag->diag_rstr)); + CeedCallBackend(CeedElemRestrictionDestroy(&impl->diag->point_block_diag_rstr)); } CeedCallBackend(CeedFree(&impl->diag)); @@ -91,6 +92,7 @@ static int CeedOperatorDestroy_Hip(CeedOperator op) { CeedCallHip(ceed, hipModuleUnload(impl->asmb->module)); CeedCallHip(ceed, hipFree(impl->asmb->d_B_in)); CeedCallHip(ceed, hipFree(impl->asmb->d_B_out)); + CeedCallBackend(CeedDestroy(&ceed)); } CeedCallBackend(CeedFree(&impl->asmb)); @@ -226,6 +228,7 @@ static int CeedOperatorSetupFields_Hip(CeedQFunction qf, CeedOperator op, bool i CeedCallBackend(CeedElemRestrictionDestroy(&rstr_i)); } } + CeedCallBackend(CeedDestroy(&ceed)); return CEED_ERROR_SUCCESS; } @@ -233,7 +236,6 @@ static int CeedOperatorSetupFields_Hip(CeedQFunction qf, CeedOperator op, bool i // CeedOperator needs to connect all the named fields (be they active or passive) to the named inputs and outputs of its CeedQFunction. //------------------------------------------------------------------------------ static int CeedOperatorSetup_Hip(CeedOperator op) { - Ceed ceed; bool is_setup_done; CeedInt Q, num_elem, num_input_fields, num_output_fields; CeedQFunctionField *qf_input_fields, *qf_output_fields; @@ -244,7 +246,6 @@ static int CeedOperatorSetup_Hip(CeedOperator op) { CeedCallBackend(CeedOperatorIsSetupDone(op, &is_setup_done)); if (is_setup_done) return CEED_ERROR_SUCCESS; - CeedCallBackend(CeedOperatorGetCeed(op, &ceed)); CeedCallBackend(CeedOperatorGetData(op, &impl)); CeedCallBackend(CeedOperatorGetQFunction(op, &qf)); CeedCallBackend(CeedOperatorGetNumQuadraturePoints(op, &Q)); @@ -585,11 +586,7 @@ static int CeedOperatorApplyAdd_Hip(CeedOperator op, CeedVector in_vec, CeedVect } // Restrict - if (impl->skip_rstr_out[field]) { - if (!is_active) CeedCallBackend(CeedVectorDestroy(&l_vec)); - continue; - } - { + if (!impl->skip_rstr_out[field]) { CeedElemRestriction elem_rstr; CeedCallBackend(CeedOperatorFieldGetElemRestriction(op_output_fields[field], &elem_rstr)); @@ -601,6 +598,7 @@ static int CeedOperatorApplyAdd_Hip(CeedOperator op, CeedVector in_vec, CeedVect // Return work vector CeedCallBackend(CeedRestoreWorkVector(ceed, &active_e_vec)); + CeedCallBackend(CeedDestroy(&ceed)); return CEED_ERROR_SUCCESS; } @@ -608,7 +606,6 @@ static int CeedOperatorApplyAdd_Hip(CeedOperator op, CeedVector in_vec, CeedVect // CeedOperator needs to connect all the named fields (be they active or passive) to the named inputs and outputs of its CeedQFunction. //------------------------------------------------------------------------------ static int CeedOperatorSetupAtPoints_Hip(CeedOperator op) { - Ceed ceed; bool is_setup_done; CeedInt max_num_points = -1, num_elem, num_input_fields, num_output_fields; CeedQFunctionField *qf_input_fields, *qf_output_fields; @@ -619,7 +616,6 @@ static int CeedOperatorSetupAtPoints_Hip(CeedOperator op) { CeedCallBackend(CeedOperatorIsSetupDone(op, &is_setup_done)); if (is_setup_done) return CEED_ERROR_SUCCESS; - CeedCallBackend(CeedOperatorGetCeed(op, &ceed)); CeedCallBackend(CeedOperatorGetData(op, &impl)); CeedCallBackend(CeedOperatorGetQFunction(op, &qf)); CeedCallBackend(CeedOperatorGetNumElements(op, &num_elem)); @@ -915,11 +911,7 @@ static int CeedOperatorApplyAddAtPoints_Hip(CeedOperator op, CeedVector in_vec, } // Restrict - if (impl->skip_rstr_out[field]) { - if (!is_active) CeedCallBackend(CeedVectorDestroy(&l_vec)); - continue; - } - { + if (!impl->skip_rstr_out[field]) { CeedElemRestriction elem_rstr; CeedCallBackend(CeedOperatorFieldGetElemRestriction(op_output_fields[field], &elem_rstr)); @@ -931,6 +923,7 @@ static int CeedOperatorApplyAddAtPoints_Hip(CeedOperator op, CeedVector in_vec, // Restore work vector CeedCallBackend(CeedRestoreWorkVector(ceed, &active_e_vec)); + CeedCallBackend(CeedDestroy(&ceed)); return CEED_ERROR_SUCCESS; } @@ -1072,6 +1065,8 @@ static inline int CeedOperatorLinearAssembleQFunctionCore_Hip(CeedOperator op, b } // Restore output + CeedCallBackend(CeedDestroy(&ceed)); + CeedCallBackend(CeedDestroy(&ceed_parent)); CeedCallBackend(CeedVectorRestoreArray(*assembled, &assembled_array)); return CEED_ERROR_SUCCESS; } @@ -1273,6 +1268,7 @@ static inline int CeedOperatorAssembleDiagonalSetup_Hip(CeedOperator op) { CeedCallHip(ceed, hipMemcpy(diag->d_eval_modes_out, eval_modes_out, num_eval_modes_out * eval_modes_bytes, hipMemcpyHostToDevice)); CeedCallBackend(CeedFree(&eval_modes_in)); CeedCallBackend(CeedFree(&eval_modes_out)); + CeedCallBackend(CeedDestroy(&ceed)); CeedCallBackend(CeedBasisDestroy(&basis_in)); CeedCallBackend(CeedBasisDestroy(&basis_out)); return CEED_ERROR_SUCCESS; @@ -1358,6 +1354,7 @@ static inline int CeedOperatorAssembleDiagonalSetupCompile_Hip(CeedOperator op, num_eval_modes_out, "NUM_COMP", num_comp, "NUM_NODES", num_nodes, "NUM_QPTS", num_qpts, "USE_CEEDSIZE", use_ceedsize_idx, "USE_POINT_BLOCK", is_point_block ? 1 : 0, "BLOCK_SIZE", num_nodes * elems_per_block)); CeedCallHip(ceed, CeedGetKernel_Hip(ceed, *module, "LinearDiagonal", is_point_block ? &diag->LinearPointBlock : &diag->LinearDiagonal)); + CeedCallBackend(CeedDestroy(&ceed)); CeedCallBackend(CeedBasisDestroy(&basis_in)); CeedCallBackend(CeedBasisDestroy(&basis_out)); return CEED_ERROR_SUCCESS; @@ -1446,6 +1443,7 @@ static inline int CeedOperatorAssembleDiagonalCore_Hip(CeedOperator op, CeedVect CeedCallBackend(CeedElemRestrictionApply(diag_rstr, CEED_TRANSPOSE, elem_diag, assembled, request)); // Cleanup + CeedCallBackend(CeedDestroy(&ceed)); CeedCallBackend(CeedVectorDestroy(&assembled_qf)); return CEED_ERROR_SUCCESS; } @@ -1658,6 +1656,7 @@ static int CeedSingleOperatorAssembleSetup_Hip(CeedOperator op, CeedInt use_ceed CeedCallBackend(CeedFree(&identity)); } CeedCallBackend(CeedFree(&eval_modes_out)); + CeedCallBackend(CeedDestroy(&ceed)); CeedCallBackend(CeedElemRestrictionDestroy(&rstr_in)); CeedCallBackend(CeedElemRestrictionDestroy(&rstr_out)); CeedCallBackend(CeedBasisDestroy(&basis_in)); @@ -1766,6 +1765,7 @@ static int CeedSingleOperatorAssemble_Hip(CeedOperator op, CeedInt offset, CeedV CeedCallBackend(CeedElemRestrictionRestoreCurlOrientations(rstr_out, &curl_orients_out)); } } + CeedCallBackend(CeedDestroy(&ceed)); CeedCallBackend(CeedElemRestrictionDestroy(&rstr_in)); CeedCallBackend(CeedElemRestrictionDestroy(&rstr_out)); return CEED_ERROR_SUCCESS; @@ -2037,6 +2037,7 @@ static int CeedOperatorLinearAssembleAddDiagonalAtPoints_Hip(CeedOperator op, Ce // Restore work vector CeedCallBackend(CeedRestoreWorkVector(ceed, &active_e_vec_in)); CeedCallBackend(CeedRestoreWorkVector(ceed, &active_e_vec_out)); + CeedCallBackend(CeedDestroy(&ceed)); return CEED_ERROR_SUCCESS; } @@ -2059,6 +2060,7 @@ int CeedOperatorCreate_Hip(CeedOperator op) { CeedCallBackend(CeedSetBackendFunction(ceed, "Operator", op, "LinearAssembleSingle", CeedSingleOperatorAssemble_Hip)); CeedCallBackend(CeedSetBackendFunction(ceed, "Operator", op, "ApplyAdd", CeedOperatorApplyAdd_Hip)); CeedCallBackend(CeedSetBackendFunction(ceed, "Operator", op, "Destroy", CeedOperatorDestroy_Hip)); + CeedCallBackend(CeedDestroy(&ceed)); return CEED_ERROR_SUCCESS; } @@ -2077,6 +2079,7 @@ int CeedOperatorCreateAtPoints_Hip(CeedOperator op) { CeedCallBackend(CeedSetBackendFunction(ceed, "Operator", op, "LinearAssembleAddDiagonal", CeedOperatorLinearAssembleAddDiagonalAtPoints_Hip)); CeedCallBackend(CeedSetBackendFunction(ceed, "Operator", op, "ApplyAdd", CeedOperatorApplyAddAtPoints_Hip)); CeedCallBackend(CeedSetBackendFunction(ceed, "Operator", op, "Destroy", CeedOperatorDestroy_Hip)); + CeedCallBackend(CeedDestroy(&ceed)); return CEED_ERROR_SUCCESS; } diff --git a/backends/hip-ref/ceed-hip-ref-qfunction-load.cpp b/backends/hip-ref/ceed-hip-ref-qfunction-load.cpp index 2311f8a332..127bd7467d 100644 --- a/backends/hip-ref/ceed-hip-ref-qfunction-load.cpp +++ b/backends/hip-ref/ceed-hip-ref-qfunction-load.cpp @@ -30,13 +30,13 @@ extern "C" int CeedQFunctionBuildKernel_Hip_ref(CeedQFunction qf) { CeedQFunctionField *input_fields, *output_fields; CeedQFunction_Hip *data; - CeedCallBackend(CeedQFunctionGetCeed(qf, &ceed)); - CeedCallBackend(CeedGetData(ceed, &ceed_Hip)); - CeedCallBackend(CeedQFunctionGetData(qf, (void **)&data)); - // QFunction is built + CeedCallBackend(CeedQFunctionGetData(qf, (void **)&data)); if (data->QFunction) return CEED_ERROR_SUCCESS; + CeedCallBackend(CeedQFunctionGetCeed(qf, &ceed)); + CeedCallBackend(CeedGetData(ceed, &ceed_Hip)); + // QFunction kernel generation CeedCallBackend(CeedQFunctionGetFields(qf, &num_input_fields, &input_fields, &num_output_fields, &output_fields)); @@ -116,6 +116,7 @@ extern "C" int CeedQFunctionBuildKernel_Hip_ref(CeedQFunction qf) { // Compile kernel CeedCallBackend(CeedCompile_Hip(ceed, code.str().c_str(), &data->module, 1, "BLOCK_SIZE", ceed_Hip->opt_block_size)); CeedCallBackend(CeedGetKernel_Hip(ceed, data->module, kernel_name.c_str(), &data->QFunction)); + CeedCallBackend(CeedDestroy(&ceed)); return CEED_ERROR_SUCCESS; } diff --git a/backends/hip-ref/ceed-hip-ref-qfunction.c b/backends/hip-ref/ceed-hip-ref-qfunction.c index 18d531ac11..92835b897e 100644 --- a/backends/hip-ref/ceed-hip-ref-qfunction.c +++ b/backends/hip-ref/ceed-hip-ref-qfunction.c @@ -60,6 +60,7 @@ static int CeedQFunctionApply_Hip(CeedQFunction qf, CeedInt Q, CeedVector *U, Ce // Restore context CeedCallBackend(CeedQFunctionRestoreInnerContextData(qf, &data->d_c)); + CeedCallBackend(CeedDestroy(&ceed)); return CEED_ERROR_SUCCESS; } @@ -88,12 +89,12 @@ int CeedQFunctionCreate_Hip(CeedQFunction qf) { CeedCallBackend(CeedQFunctionSetData(qf, data)); CeedCallBackend(CeedQFunctionGetNumArgs(qf, &num_input_fields, &num_output_fields)); - // Read QFunction name CeedCallBackend(CeedQFunctionGetKernelName(qf, &data->qfunction_name)); // Register backend functions CeedCallBackend(CeedSetBackendFunction(ceed, "QFunction", qf, "Apply", CeedQFunctionApply_Hip)); CeedCallBackend(CeedSetBackendFunction(ceed, "QFunction", qf, "Destroy", CeedQFunctionDestroy_Hip)); + CeedCallBackend(CeedDestroy(&ceed)); return CEED_ERROR_SUCCESS; } diff --git a/backends/hip-ref/ceed-hip-ref-qfunctioncontext.c b/backends/hip-ref/ceed-hip-ref-qfunctioncontext.c index 52bf13370b..0d09a2087d 100644 --- a/backends/hip-ref/ceed-hip-ref-qfunctioncontext.c +++ b/backends/hip-ref/ceed-hip-ref-qfunctioncontext.c @@ -37,6 +37,7 @@ static inline int CeedQFunctionContextSyncH2D_Hip(const CeedQFunctionContext ctx impl->d_data = impl->d_data_owned; } CeedCallHip(ceed, hipMemcpy(impl->d_data, impl->h_data, ctx_size, hipMemcpyHostToDevice)); + CeedCallBackend(CeedDestroy(&ceed)); return CEED_ERROR_SUCCESS; } @@ -63,6 +64,7 @@ static inline int CeedQFunctionContextSyncD2H_Hip(const CeedQFunctionContext ctx impl->h_data = impl->h_data_owned; } CeedCallHip(ceed, hipMemcpy(impl->h_data, impl->d_data, ctx_size, hipMemcpyDeviceToHost)); + CeedCallBackend(CeedDestroy(&ceed)); return CEED_ERROR_SUCCESS; } @@ -204,6 +206,7 @@ static int CeedQFunctionContextSetDataDevice_Hip(const CeedQFunctionContext ctx, impl->d_data = data; break; } + CeedCallBackend(CeedDestroy(&ceed)); return CEED_ERROR_SUCCESS; } @@ -334,6 +337,7 @@ int CeedQFunctionContextCreate_Hip(CeedQFunctionContext ctx) { CeedCallBackend(CeedSetBackendFunction(ceed, "QFunctionContext", ctx, "GetData", CeedQFunctionContextGetData_Hip)); CeedCallBackend(CeedSetBackendFunction(ceed, "QFunctionContext", ctx, "GetDataRead", CeedQFunctionContextGetDataRead_Hip)); CeedCallBackend(CeedSetBackendFunction(ceed, "QFunctionContext", ctx, "Destroy", CeedQFunctionContextDestroy_Hip)); + CeedCallBackend(CeedDestroy(&ceed)); CeedCallBackend(CeedCalloc(1, &impl)); CeedCallBackend(CeedQFunctionContextSetBackendData(ctx, impl)); return CEED_ERROR_SUCCESS; diff --git a/backends/hip-ref/ceed-hip-ref-restriction.c b/backends/hip-ref/ceed-hip-ref-restriction.c index ca1d19d7a6..88393e25d3 100644 --- a/backends/hip-ref/ceed-hip-ref-restriction.c +++ b/backends/hip-ref/ceed-hip-ref-restriction.c @@ -108,6 +108,7 @@ static inline int CeedElemRestrictionSetupCompile_Hip(CeedElemRestriction rstr) } break; } + CeedCallBackend(CeedDestroy(&ceed)); return CEED_ERROR_SUCCESS; } @@ -287,6 +288,7 @@ static inline int CeedElemRestrictionApply_Hip_Core(CeedElemRestriction rstr, Ce // Restore arrays CeedCallBackend(CeedVectorRestoreArrayRead(u, &d_u)); CeedCallBackend(CeedVectorRestoreArray(v, &d_v)); + CeedCallBackend(CeedDestroy(&ceed)); return CEED_ERROR_SUCCESS; } @@ -406,6 +408,7 @@ static int CeedElemRestrictionDestroy_Hip(CeedElemRestriction rstr) { CeedCallBackend(CeedFree(&impl->h_points_per_elem_owned)); CeedCallHip(ceed, hipFree((CeedInt *)impl->d_points_per_elem_owned)); CeedCallBackend(CeedFree(&impl)); + CeedCallBackend(CeedDestroy(&ceed)); return CEED_ERROR_SUCCESS; } @@ -487,6 +490,7 @@ static int CeedElemRestrictionOffset_Hip(const CeedElemRestriction rstr, const C CeedCallBackend(CeedFree(&l_vec_indices)); CeedCallBackend(CeedFree(&t_offsets)); CeedCallBackend(CeedFree(&t_indices)); + CeedCallBackend(CeedDestroy(&ceed)); return CEED_ERROR_SUCCESS; } @@ -504,6 +508,7 @@ int CeedElemRestrictionCreate_Hip(CeedMemType mem_type, CeedCopyMode copy_mode, CeedCallBackend(CeedElemRestrictionGetCeed(rstr, &ceed)); CeedCallBackend(CeedGetParent(ceed, &ceed_parent)); CeedCallBackend(CeedIsDeterministic(ceed_parent, &is_deterministic)); + CeedCallBackend(CeedDestroy(&ceed_parent)); CeedCallBackend(CeedElemRestrictionGetNumElements(rstr, &num_elem)); CeedCallBackend(CeedElemRestrictionGetNumComponents(rstr, &num_comp)); CeedCallBackend(CeedElemRestrictionGetElementSize(rstr, &elem_size)); @@ -651,6 +656,7 @@ int CeedElemRestrictionCreate_Hip(CeedMemType mem_type, CeedCopyMode copy_mode, CeedSetBackendFunction(ceed, "ElemRestriction", rstr, "GetAtPointsElementOffset", CeedElemRestrictionGetAtPointsElementOffset_Hip)); } CeedCallBackend(CeedSetBackendFunction(ceed, "ElemRestriction", rstr, "Destroy", CeedElemRestrictionDestroy_Hip)); + CeedCallBackend(CeedDestroy(&ceed)); return CEED_ERROR_SUCCESS; } diff --git a/backends/hip-ref/ceed-hip-ref-vector.c b/backends/hip-ref/ceed-hip-ref-vector.c index 164eb822d6..0a3a3fe3d0 100644 --- a/backends/hip-ref/ceed-hip-ref-vector.c +++ b/backends/hip-ref/ceed-hip-ref-vector.c @@ -39,15 +39,13 @@ static inline int CeedVectorNeedSync_Hip(const CeedVector vec, CeedMemType mem_t // Sync host to device //------------------------------------------------------------------------------ static inline int CeedVectorSyncH2D_Hip(const CeedVector vec) { - Ceed ceed; CeedSize length; size_t bytes; CeedVector_Hip *impl; - CeedCallBackend(CeedVectorGetCeed(vec, &ceed)); CeedCallBackend(CeedVectorGetData(vec, &impl)); - CeedCheck(impl->h_array, ceed, CEED_ERROR_BACKEND, "No valid host data to sync to device"); + CeedCheck(impl->h_array, CeedVectorReturnCeed(vec), CEED_ERROR_BACKEND, "No valid host data to sync to device"); CeedCallBackend(CeedVectorGetLength(vec, &length)); bytes = length * sizeof(CeedScalar); @@ -56,10 +54,10 @@ static inline int CeedVectorSyncH2D_Hip(const CeedVector vec) { } else if (impl->d_array_owned) { impl->d_array = impl->d_array_owned; } else { - CeedCallHip(ceed, hipMalloc((void **)&impl->d_array_owned, bytes)); + CeedCallHip(CeedVectorReturnCeed(vec), hipMalloc((void **)&impl->d_array_owned, bytes)); impl->d_array = impl->d_array_owned; } - CeedCallHip(ceed, hipMemcpy(impl->d_array, impl->h_array, bytes, hipMemcpyHostToDevice)); + CeedCallHip(CeedVectorReturnCeed(vec), hipMemcpy(impl->d_array, impl->h_array, bytes, hipMemcpyHostToDevice)); return CEED_ERROR_SUCCESS; } @@ -67,15 +65,13 @@ static inline int CeedVectorSyncH2D_Hip(const CeedVector vec) { // Sync device to host //------------------------------------------------------------------------------ static inline int CeedVectorSyncD2H_Hip(const CeedVector vec) { - Ceed ceed; CeedSize length; size_t bytes; CeedVector_Hip *impl; - CeedCallBackend(CeedVectorGetCeed(vec, &ceed)); CeedCallBackend(CeedVectorGetData(vec, &impl)); - CeedCheck(impl->d_array, ceed, CEED_ERROR_BACKEND, "No valid device data to sync to host"); + CeedCheck(impl->d_array, CeedVectorReturnCeed(vec), CEED_ERROR_BACKEND, "No valid device data to sync to host"); if (impl->h_array_borrowed) { impl->h_array = impl->h_array_borrowed; @@ -91,7 +87,7 @@ static inline int CeedVectorSyncD2H_Hip(const CeedVector vec) { CeedCallBackend(CeedVectorGetLength(vec, &length)); bytes = length * sizeof(CeedScalar); - CeedCallHip(ceed, hipMemcpy(impl->h_array, impl->d_array, bytes, hipMemcpyDeviceToHost)); + CeedCallHip(CeedVectorReturnCeed(vec), hipMemcpy(impl->h_array, impl->d_array, bytes, hipMemcpyDeviceToHost)); return CEED_ERROR_SUCCESS; } @@ -202,6 +198,7 @@ static int CeedVectorSetArrayDevice_Hip(const CeedVector vec, const CeedCopyMode CeedCallBackend(CeedSetDeviceCeedScalarArray_Hip(ceed, array, copy_mode, length, (const CeedScalar **)&impl->d_array_owned, (const CeedScalar **)&impl->d_array_borrowed, (const CeedScalar **)&impl->d_array)); + CeedCallBackend(CeedDestroy(&ceed)); return CEED_ERROR_SUCCESS; } @@ -574,6 +571,7 @@ static int CeedVectorNorm_Hip(CeedVector vec, CeedNormType type, CeedScalar *nor } } CeedCallBackend(CeedVectorRestoreArrayRead(vec, &d_array)); + CeedCallBackend(CeedDestroy(&ceed)); return CEED_ERROR_SUCCESS; } @@ -786,6 +784,7 @@ int CeedVectorCreate_Hip(CeedSize n, CeedVector vec) { CeedCallBackend(CeedSetBackendFunction(ceed, "Vector", vec, "AXPBY", CeedVectorAXPBY_Hip)); CeedCallBackend(CeedSetBackendFunction(ceed, "Vector", vec, "PointwiseMult", CeedVectorPointwiseMult_Hip)); CeedCallBackend(CeedSetBackendFunction(ceed, "Vector", vec, "Destroy", CeedVectorDestroy_Hip)); + CeedCallBackend(CeedDestroy(&ceed)); CeedCallBackend(CeedCalloc(1, &impl)); CeedCallBackend(CeedVectorSetData(vec, impl)); return CEED_ERROR_SUCCESS; diff --git a/backends/hip-shared/ceed-hip-shared-basis.c b/backends/hip-shared/ceed-hip-shared-basis.c index cdcc28ce07..6d8c858632 100644 --- a/backends/hip-shared/ceed-hip-shared-basis.c +++ b/backends/hip-shared/ceed-hip-shared-basis.c @@ -248,6 +248,7 @@ static int CeedBasisApplyTensorCore_Hip_shared(CeedBasis basis, bool apply_add, CeedCallBackend(CeedVectorRestoreArray(v, &d_v)); if (eval_mode == CEED_EVAL_NONE) CeedCallBackend(CeedVectorSetArray(v, CEED_MEM_DEVICE, CEED_COPY_VALUES, (CeedScalar *)d_u)); if (eval_mode != CEED_EVAL_WEIGHT) CeedCallBackend(CeedVectorRestoreArrayRead(u, &d_u)); + CeedCallBackend(CeedDestroy(&ceed)); return CEED_ERROR_SUCCESS; } @@ -276,7 +277,6 @@ static int CeedBasisApplyAtPointsCore_Hip_shared(CeedBasis basis, bool apply_add CeedScalar *d_v; CeedBasis_Hip_shared *data; - CeedCallBackend(CeedBasisGetCeed(basis, &ceed)); CeedCallBackend(CeedBasisGetData(basis, &data)); CeedCallBackend(CeedBasisGetNumQuadraturePoints1D(basis, &Q_1d)); CeedCallBackend(CeedBasisGetDimension(basis, &dim)); @@ -287,6 +287,8 @@ static int CeedBasisApplyAtPointsCore_Hip_shared(CeedBasis basis, bool apply_add return CEED_ERROR_SUCCESS; } + CeedCallBackend(CeedBasisGetCeed(basis, &ceed)); + // Check padded to uniform number of points per elem for (CeedInt i = 1; i < num_elem; i++) max_num_points = CeedIntMax(max_num_points, num_points[i]); { @@ -507,6 +509,7 @@ int CeedBasisCreateTensorH1_Hip_shared(CeedInt dim, CeedInt P_1d, CeedInt Q_1d, CeedCallBackend(CeedSetBackendFunction(ceed, "Basis", basis, "ApplyAtPoints", CeedBasisApplyAtPoints_Hip_shared)); CeedCallBackend(CeedSetBackendFunction(ceed, "Basis", basis, "ApplyAddAtPoints", CeedBasisApplyAddAtPoints_Hip_shared)); CeedCallBackend(CeedSetBackendFunction(ceed, "Basis", basis, "Destroy", CeedBasisDestroy_Hip_shared)); + CeedCallBackend(CeedDestroy(&ceed)); return CEED_ERROR_SUCCESS; } diff --git a/backends/hip-shared/ceed-hip-shared.c b/backends/hip-shared/ceed-hip-shared.c index f69c78d4ee..01d173e42e 100644 --- a/backends/hip-shared/ceed-hip-shared.c +++ b/backends/hip-shared/ceed-hip-shared.c @@ -33,6 +33,7 @@ static int CeedInit_Hip_shared(const char *resource, Ceed ceed) { CeedCallBackend(CeedInit("/gpu/hip/ref", &ceed_ref)); CeedCallBackend(CeedSetDelegate(ceed, ceed_ref)); + CeedCallBackend(CeedDestroy(&ceed_ref)); CeedCallBackend(CeedSetBackendFunction(ceed, "Ceed", ceed, "BasisCreateTensorH1", CeedBasisCreateTensorH1_Hip_shared)); CeedCallBackend(CeedSetBackendFunction(ceed, "Ceed", ceed, "Destroy", CeedDestroy_Hip)); diff --git a/backends/magma/ceed-magma-basis.c b/backends/magma/ceed-magma-basis.c index 6f5fa0a2ca..e1f39139d6 100644 --- a/backends/magma/ceed-magma-basis.c +++ b/backends/magma/ceed-magma-basis.c @@ -249,6 +249,7 @@ static int CeedBasisApplyCore_Magma(CeedBasis basis, bool apply_add, CeedInt num CeedCallBackend(CeedVectorRestoreArrayRead(u, &d_u)); } CeedCallBackend(CeedVectorRestoreArray(v, &d_v)); + CeedCallBackend(CeedDestroy(&ceed)); return CEED_ERROR_SUCCESS; } @@ -371,6 +372,7 @@ static int CeedBasisApplyNonTensorCore_Magma(CeedBasis basis, bool apply_add, Ce CeedCallBackend(CeedFree(&basis_kernel_source)); for (CeedInt i = 0; i < num_file_paths; i++) CeedCallBackend(CeedFree(&file_paths[i])); CeedCallBackend(CeedFree(&file_paths)); + CeedCallBackend(CeedDestroy(&ceed_delegate)); } } @@ -457,6 +459,7 @@ static int CeedBasisApplyNonTensorCore_Magma(CeedBasis basis, bool apply_add, Ce CeedCallBackend(CeedVectorRestoreArrayRead(u, &d_u)); } CeedCallBackend(CeedVectorRestoreArray(v, &d_v)); + CeedCallBackend(CeedDestroy(&ceed)); return CEED_ERROR_SUCCESS; } @@ -490,6 +493,7 @@ static int CeedBasisDestroy_Magma(CeedBasis basis) { CeedCallBackend(magma_free(impl->d_grad_1d)); if (impl->d_q_weight_1d) CeedCallBackend(magma_free(impl->d_q_weight_1d)); CeedCallBackend(CeedFree(&impl)); + CeedCallBackend(CeedDestroy(&ceed)); return CEED_ERROR_SUCCESS; } @@ -517,6 +521,7 @@ static int CeedBasisDestroyNonTensor_Magma(CeedBasis basis) { CeedCallBackend(magma_free(impl->d_curl)); if (impl->d_q_weight) CeedCallBackend(magma_free(impl->d_q_weight)); CeedCallBackend(CeedFree(&impl)); + CeedCallBackend(CeedDestroy(&ceed)); return CEED_ERROR_SUCCESS; } @@ -626,6 +631,8 @@ int CeedBasisCreateTensorH1_Magma(CeedInt dim, CeedInt P_1d, CeedInt Q_1d, const CeedCallBackend(CeedSetBackendFunction(ceed, "Basis", basis, "ApplyAdd", CeedBasisApplyAdd_Magma)); CeedCallBackend(CeedSetBackendFunction(ceed, "Basis", basis, "ApplyAtPoints", CeedBasisApplyAtPoints_Magma)); CeedCallBackend(CeedSetBackendFunction(ceed, "Basis", basis, "Destroy", CeedBasisDestroy_Magma)); + CeedCallBackend(CeedDestroy(&ceed)); + CeedCallBackend(CeedDestroy(&ceed_delegate)); return CEED_ERROR_SUCCESS; } @@ -680,6 +687,7 @@ int CeedBasisCreateH1_Magma(CeedElemTopology topo, CeedInt dim, CeedInt num_node CeedCallBackend(CeedGetKernelMagma(ceed, impl->module[0], "magma_weight_nontensor", &impl->Weight)); CeedCallBackend(CeedFree(&weight_kernel_path)); CeedCallBackend(CeedFree(&basis_kernel_source)); + CeedCallBackend(CeedDestroy(&ceed_delegate)); } CeedCallBackend(CeedBasisSetData(basis, impl)); @@ -688,6 +696,7 @@ int CeedBasisCreateH1_Magma(CeedElemTopology topo, CeedInt dim, CeedInt num_node CeedCallBackend(CeedSetBackendFunction(ceed, "Basis", basis, "Apply", CeedBasisApplyNonTensor_Magma)); CeedCallBackend(CeedSetBackendFunction(ceed, "Basis", basis, "ApplyAdd", CeedBasisApplyAddNonTensor_Magma)); CeedCallBackend(CeedSetBackendFunction(ceed, "Basis", basis, "Destroy", CeedBasisDestroyNonTensor_Magma)); + CeedCallBackend(CeedDestroy(&ceed)); return CEED_ERROR_SUCCESS; } @@ -742,6 +751,7 @@ int CeedBasisCreateHdiv_Magma(CeedElemTopology topo, CeedInt dim, CeedInt num_no CeedCallBackend(CeedGetKernelMagma(ceed, impl->module[0], "magma_weight_nontensor", &impl->Weight)); CeedCallBackend(CeedFree(&weight_kernel_path)); CeedCallBackend(CeedFree(&basis_kernel_source)); + CeedCallBackend(CeedDestroy(&ceed_delegate)); } CeedCallBackend(CeedBasisSetData(basis, impl)); @@ -750,6 +760,7 @@ int CeedBasisCreateHdiv_Magma(CeedElemTopology topo, CeedInt dim, CeedInt num_no CeedCallBackend(CeedSetBackendFunction(ceed, "Basis", basis, "Apply", CeedBasisApplyNonTensor_Magma)); CeedCallBackend(CeedSetBackendFunction(ceed, "Basis", basis, "ApplyAdd", CeedBasisApplyAddNonTensor_Magma)); CeedCallBackend(CeedSetBackendFunction(ceed, "Basis", basis, "Destroy", CeedBasisDestroyNonTensor_Magma)); + CeedCallBackend(CeedDestroy(&ceed)); return CEED_ERROR_SUCCESS; } @@ -804,6 +815,7 @@ int CeedBasisCreateHcurl_Magma(CeedElemTopology topo, CeedInt dim, CeedInt num_n CeedCallBackend(CeedGetKernelMagma(ceed, impl->module[0], "magma_weight_nontensor", &impl->Weight)); CeedCallBackend(CeedFree(&weight_kernel_path)); CeedCallBackend(CeedFree(&basis_kernel_source)); + CeedCallBackend(CeedDestroy(&ceed_delegate)); } CeedCallBackend(CeedBasisSetData(basis, impl)); @@ -812,6 +824,7 @@ int CeedBasisCreateHcurl_Magma(CeedElemTopology topo, CeedInt dim, CeedInt num_n CeedCallBackend(CeedSetBackendFunction(ceed, "Basis", basis, "Apply", CeedBasisApplyNonTensor_Magma)); CeedCallBackend(CeedSetBackendFunction(ceed, "Basis", basis, "ApplyAdd", CeedBasisApplyAddNonTensor_Magma)); CeedCallBackend(CeedSetBackendFunction(ceed, "Basis", basis, "Destroy", CeedBasisDestroyNonTensor_Magma)); + CeedCallBackend(CeedDestroy(&ceed)); return CEED_ERROR_SUCCESS; } diff --git a/backends/magma/ceed-magma-det.c b/backends/magma/ceed-magma-det.c index 9b7125ccda..5ecbe462e1 100644 --- a/backends/magma/ceed-magma-det.c +++ b/backends/magma/ceed-magma-det.c @@ -35,6 +35,7 @@ static int CeedInit_Magma_Det(const char *resource, Ceed ceed) { CeedCallBackend(CeedInit("/gpu/cuda/magma", &ceed_ref)); #endif CeedCallBackend(CeedSetDelegate(ceed, ceed_ref)); + CeedCallBackend(CeedDestroy(&ceed_ref)); CeedCallBackend(CeedSetBackendFunction(ceed, "Ceed", ceed, "Destroy", CeedDestroy_Magma)); return CEED_ERROR_SUCCESS; diff --git a/backends/magma/ceed-magma.c b/backends/magma/ceed-magma.c index 06254365b9..f325947920 100644 --- a/backends/magma/ceed-magma.c +++ b/backends/magma/ceed-magma.c @@ -36,6 +36,7 @@ static int CeedInit_Magma(const char *resource, Ceed ceed) { CeedCallBackend(CeedInit("/gpu/cuda/ref", &ceed_ref)); #endif CeedCallBackend(CeedSetDelegate(ceed, ceed_ref)); + CeedCallBackend(CeedDestroy(&ceed_ref)); CeedCallBackend(CeedSetBackendFunction(ceed, "Ceed", ceed, "BasisCreateTensorH1", CeedBasisCreateTensorH1_Magma)); CeedCallBackend(CeedSetBackendFunction(ceed, "Ceed", ceed, "BasisCreateH1", CeedBasisCreateH1_Magma)); diff --git a/backends/memcheck/ceed-memcheck-blocked.c b/backends/memcheck/ceed-memcheck-blocked.c index 4d9f557af5..5bc0a5dc10 100644 --- a/backends/memcheck/ceed-memcheck-blocked.c +++ b/backends/memcheck/ceed-memcheck-blocked.c @@ -22,6 +22,7 @@ static int CeedInit_Memcheck(const char *resource, Ceed ceed) { // Create reference Ceed that implementation will be dispatched through unless overridden CeedCallBackend(CeedInit("/cpu/self/ref/blocked", &ceed_ref)); CeedCallBackend(CeedSetDelegate(ceed, ceed_ref)); + CeedCallBackend(CeedDestroy(&ceed_ref)); CeedCallBackend(CeedSetBackendFunction(ceed, "Ceed", ceed, "VectorCreate", CeedVectorCreate_Memcheck)); CeedCallBackend(CeedSetBackendFunction(ceed, "Ceed", ceed, "ElemRestrictionCreate", CeedElemRestrictionCreate_Memcheck)); diff --git a/backends/memcheck/ceed-memcheck-qfunction.c b/backends/memcheck/ceed-memcheck-qfunction.c index 1bb8158584..b73e3c5e90 100644 --- a/backends/memcheck/ceed-memcheck-qfunction.c +++ b/backends/memcheck/ceed-memcheck-qfunction.c @@ -17,7 +17,6 @@ // QFunction Apply //------------------------------------------------------------------------------ static int CeedQFunctionApply_Memcheck(CeedQFunction qf, CeedInt Q, CeedVector *U, CeedVector *V) { - Ceed ceed; void *ctx_data = NULL; int input_block_ids[CEED_FIELD_MAX], output_block_ids[CEED_FIELD_MAX]; CeedInt num_in, num_out; @@ -25,7 +24,6 @@ static int CeedQFunctionApply_Memcheck(CeedQFunction qf, CeedInt Q, CeedVector * CeedQFunctionField *output_fields; CeedQFunction_Memcheck *impl; - CeedCallBackend(CeedQFunctionGetCeed(qf, &ceed)); CeedCallBackend(CeedQFunctionGetData(qf, &impl)); CeedCallBackend(CeedQFunctionGetContextData(qf, CEED_MEM_HOST, &ctx_data)); CeedCallBackend(CeedQFunctionGetUserFunction(qf, &f)); @@ -82,7 +80,7 @@ static int CeedQFunctionApply_Memcheck(CeedQFunction qf, CeedInt Q, CeedVector * CeedCallBackend(CeedQFunctionFieldGetSize(output_fields[i], &field_size)); CeedCallBackend(CeedQFunctionFieldGetName(output_fields[i], &field_name)); for (CeedSize j = 0; j < field_size * (CeedSize)Q; j++) { - CeedCheck(!isnan(impl->outputs[i][j]), ceed, CEED_ERROR_BACKEND, + CeedCheck(!isnan(impl->outputs[i][j]), CeedQFunctionReturnCeed(qf), CEED_ERROR_BACKEND, "QFunction output %" CeedInt_FMT " '%s' entry %" CeedSize_FMT " is NaN after restoring write-only access: %s:%s ", i, field_name, j, kernel_path, kernel_name); } @@ -121,6 +119,7 @@ int CeedQFunctionCreate_Memcheck(CeedQFunction qf) { CeedCallBackend(CeedQFunctionSetData(qf, impl)); CeedCallBackend(CeedSetBackendFunction(ceed, "QFunction", qf, "Apply", CeedQFunctionApply_Memcheck)); CeedCallBackend(CeedSetBackendFunction(ceed, "QFunction", qf, "Destroy", CeedQFunctionDestroy_Memcheck)); + CeedCallBackend(CeedDestroy(&ceed)); return CEED_ERROR_SUCCESS; } diff --git a/backends/memcheck/ceed-memcheck-qfunctioncontext.c b/backends/memcheck/ceed-memcheck-qfunctioncontext.c index 57afe981af..6149a5a3ac 100644 --- a/backends/memcheck/ceed-memcheck-qfunctioncontext.c +++ b/backends/memcheck/ceed-memcheck-qfunctioncontext.c @@ -206,18 +206,16 @@ static int CeedQFunctionContextRestoreData_Memcheck(CeedQFunctionContext ctx) { // QFunctionContext Restore Data Read-Only //------------------------------------------------------------------------------ static int CeedQFunctionContextRestoreDataRead_Memcheck(CeedQFunctionContext ctx) { - Ceed ceed; size_t ctx_size; CeedQFunctionContext_Memcheck *impl; - CeedCallBackend(CeedQFunctionContextGetCeed(ctx, &ceed)); CeedCallBackend(CeedQFunctionContextGetContextSize(ctx, &ctx_size)); CeedCallBackend(CeedQFunctionContextGetBackendData(ctx, &impl)); // Verify no changes made during read-only access bool is_changed = memcmp(impl->data_allocated, impl->data_read_only_copy, ctx_size); - CeedCheck(!is_changed, ceed, CEED_ERROR_BACKEND, "Context data changed while accessed in read-only mode"); + CeedCheck(!is_changed, CeedQFunctionContextReturnCeed(ctx), CEED_ERROR_BACKEND, "Context data changed while accessed in read-only mode"); // Invalidate read-only buffer memset(impl->data_read_only_copy, -42, ctx_size); @@ -230,16 +228,15 @@ static int CeedQFunctionContextRestoreDataRead_Memcheck(CeedQFunctionContext ctx // QFunctionContext destroy user data //------------------------------------------------------------------------------ static int CeedQFunctionContextDataDestroy_Memcheck(CeedQFunctionContext ctx) { - Ceed ceed; CeedMemType data_destroy_mem_type; CeedQFunctionContextDataDestroyUser data_destroy_function; CeedQFunctionContext_Memcheck *impl; - CeedCallBackend(CeedQFunctionContextGetCeed(ctx, &ceed)); CeedCallBackend(CeedQFunctionContextGetBackendData(ctx, &impl)); CeedCallBackend(CeedQFunctionContextGetDataDestroy(ctx, &data_destroy_mem_type, &data_destroy_function)); - CeedCheck(data_destroy_mem_type == CEED_MEM_HOST, ceed, CEED_ERROR_BACKEND, "Can only destroy HOST memory for this backend"); + CeedCheck(data_destroy_mem_type == CEED_MEM_HOST, CeedQFunctionContextReturnCeed(ctx), CEED_ERROR_BACKEND, + "Can only destroy HOST memory for this backend"); // Run user destroy routine if (data_destroy_function) { @@ -305,6 +302,7 @@ int CeedQFunctionContextCreate_Memcheck(CeedQFunctionContext ctx) { CeedCallBackend(CeedSetBackendFunction(ceed, "QFunctionContext", ctx, "RestoreDataRead", CeedQFunctionContextRestoreDataRead_Memcheck)); CeedCallBackend(CeedSetBackendFunction(ceed, "QFunctionContext", ctx, "DataDestroy", CeedQFunctionContextDataDestroy_Memcheck)); CeedCallBackend(CeedSetBackendFunction(ceed, "QFunctionContext", ctx, "Destroy", CeedQFunctionContextDestroy_Memcheck)); + CeedCallBackend(CeedDestroy(&ceed)); CeedCallBackend(CeedCalloc(1, &impl)); CeedCallBackend(CeedQFunctionContextSetBackendData(ctx, impl)); return CEED_ERROR_SUCCESS; diff --git a/backends/memcheck/ceed-memcheck-restriction.c b/backends/memcheck/ceed-memcheck-restriction.c index 35d3016726..57faf28116 100644 --- a/backends/memcheck/ceed-memcheck-restriction.c +++ b/backends/memcheck/ceed-memcheck-restriction.c @@ -768,6 +768,7 @@ int CeedElemRestrictionCreate_Memcheck(CeedMemType mem_type, CeedCopyMode copy_m CeedCallBackend(CeedSetBackendFunction(ceed, "ElemRestriction", rstr, "GetOrientations", CeedElemRestrictionGetOrientations_Memcheck)); CeedCallBackend(CeedSetBackendFunction(ceed, "ElemRestriction", rstr, "GetCurlOrientations", CeedElemRestrictionGetCurlOrientations_Memcheck)); CeedCallBackend(CeedSetBackendFunction(ceed, "ElemRestriction", rstr, "Destroy", CeedElemRestrictionDestroy_Memcheck)); + CeedCallBackend(CeedDestroy(&ceed)); return CEED_ERROR_SUCCESS; } diff --git a/backends/memcheck/ceed-memcheck-serial.c b/backends/memcheck/ceed-memcheck-serial.c index f23a8013e6..433380d6d4 100644 --- a/backends/memcheck/ceed-memcheck-serial.c +++ b/backends/memcheck/ceed-memcheck-serial.c @@ -23,6 +23,7 @@ static int CeedInit_Memcheck(const char *resource, Ceed ceed) { // Create reference Ceed that implementation will be dispatched through unless overridden CeedCallBackend(CeedInit("/cpu/self/ref/serial", &ceed_ref)); CeedCallBackend(CeedSetDelegate(ceed, ceed_ref)); + CeedCallBackend(CeedDestroy(&ceed_ref)); CeedCallBackend(CeedSetBackendFunction(ceed, "Ceed", ceed, "VectorCreate", CeedVectorCreate_Memcheck)); CeedCallBackend(CeedSetBackendFunction(ceed, "Ceed", ceed, "ElemRestrictionCreate", CeedElemRestrictionCreate_Memcheck)); diff --git a/backends/memcheck/ceed-memcheck-vector.c b/backends/memcheck/ceed-memcheck-vector.c index 325fd52c34..52716d5c70 100644 --- a/backends/memcheck/ceed-memcheck-vector.c +++ b/backends/memcheck/ceed-memcheck-vector.c @@ -248,11 +248,9 @@ static int CeedVectorGetArrayWrite_Memcheck(CeedVector vec, CeedMemType mem_type // Vector Restore Array //------------------------------------------------------------------------------ static int CeedVectorRestoreArray_Memcheck(CeedVector vec) { - Ceed ceed; CeedSize length; CeedVector_Memcheck *impl; - CeedCallBackend(CeedVectorGetCeed(vec, &ceed)); CeedCallBackend(CeedVectorGetData(vec, &impl)); CeedCallBackend(CeedVectorGetLength(vec, &length)); @@ -260,7 +258,8 @@ static int CeedVectorRestoreArray_Memcheck(CeedVector vec) { if (impl->is_write_only_access) { for (CeedSize i = 0; i < length; i++) { if (isnan(impl->array_writable_copy[i])) { - CeedDebug256(ceed, CEED_DEBUG_COLOR_WARNING, "WARNING: Vec entry %" CeedSize_FMT " is NaN after restoring write-only access", i); + CeedDebug256(CeedVectorReturnCeed(vec), CEED_DEBUG_COLOR_WARNING, + "WARNING: Vec entry %" CeedSize_FMT " is NaN after restoring write-only access", i); } } impl->is_write_only_access = false; @@ -281,18 +280,16 @@ static int CeedVectorRestoreArray_Memcheck(CeedVector vec) { // Vector Restore Array Read-Only //------------------------------------------------------------------------------ static int CeedVectorRestoreArrayRead_Memcheck(CeedVector vec) { - Ceed ceed; CeedSize length; CeedVector_Memcheck *impl; - CeedCallBackend(CeedVectorGetCeed(vec, &ceed)); CeedCallBackend(CeedVectorGetData(vec, &impl)); CeedCallBackend(CeedVectorGetLength(vec, &length)); // Verify no changes made during read-only access bool is_changed = memcmp(impl->array_allocated, impl->array_read_only_copy, length * sizeof(CeedScalar)); - CeedCheck(!is_changed, ceed, CEED_ERROR_BACKEND, "Array data changed while accessed in read-only mode"); + CeedCheck(!is_changed, CeedVectorReturnCeed(vec), CEED_ERROR_BACKEND, "Array data changed while accessed in read-only mode"); // Invalidate read-only buffer for (CeedSize i = 0; i < length; i++) impl->array_read_only_copy[i] = NAN; @@ -409,9 +406,6 @@ int CeedVectorCreate_Memcheck(CeedSize n, CeedVector vec) { Ceed ceed; CeedVector_Memcheck *impl; - CeedCallBackend(CeedCalloc(1, &impl)); - CeedCallBackend(CeedVectorSetData(vec, impl)); - CeedCallBackend(CeedVectorGetCeed(vec, &ceed)); CeedCallBackend(CeedSetBackendFunction(ceed, "Vector", vec, "HasValidArray", CeedVectorHasValidArray_Memcheck)); CeedCallBackend(CeedSetBackendFunction(ceed, "Vector", vec, "HasBorrowedArrayOfType", CeedVectorHasBorrowedArrayOfType_Memcheck)); @@ -431,6 +425,9 @@ int CeedVectorCreate_Memcheck(CeedSize n, CeedVector vec) { CeedCallBackend(CeedSetBackendFunction(ceed, "Vector", vec, "AXPBY", CeedVectorAXPBY_Memcheck)); CeedCallBackend(CeedSetBackendFunction(ceed, "Vector", vec, "PointwiseMult", CeedVectorPointwiseMult_Memcheck)); CeedCallBackend(CeedSetBackendFunction(ceed, "Vector", vec, "Destroy", CeedVectorDestroy_Memcheck)); + CeedCallBackend(CeedDestroy(&ceed)); + CeedCallBackend(CeedCalloc(1, &impl)); + CeedCallBackend(CeedVectorSetData(vec, impl)); return CEED_ERROR_SUCCESS; } diff --git a/backends/opt/ceed-opt-blocked.c b/backends/opt/ceed-opt-blocked.c index 6b0125f2fa..fcb8140ef1 100644 --- a/backends/opt/ceed-opt-blocked.c +++ b/backends/opt/ceed-opt-blocked.c @@ -37,6 +37,7 @@ static int CeedInit_Opt_Blocked(const char *resource, Ceed ceed) { // Create reference Ceed that implementation will be dispatched through unless overridden CeedCallBackend(CeedInit("/cpu/self/ref/serial", &ceed_ref)); CeedCallBackend(CeedSetDelegate(ceed, ceed_ref)); + CeedCallBackend(CeedDestroy(&ceed_ref)); CeedCallBackend(CeedSetBackendFunction(ceed, "Ceed", ceed, "Destroy", CeedDestroy_Opt)); CeedCallBackend(CeedSetBackendFunction(ceed, "Ceed", ceed, "TensorContractCreate", CeedTensorContractCreate_Opt)); diff --git a/backends/opt/ceed-opt-operator.c b/backends/opt/ceed-opt-operator.c index 8057741208..69f4560a06 100644 --- a/backends/opt/ceed-opt-operator.c +++ b/backends/opt/ceed-opt-operator.c @@ -30,7 +30,8 @@ static int CeedOperatorSetupFields_Opt(CeedQFunction qf, CeedOperator op, bool i CeedCallBackend(CeedOperatorGetCeed(op, &ceed)); CeedCallBackend(CeedGetParent(ceed, &ceed_parent)); - if (ceed_parent) ceed = ceed_parent; + CeedCallBackend(CeedReferenceCopy(ceed_parent, &ceed)); + CeedCallBackend(CeedDestroy(&ceed_parent)); } if (is_input) { CeedCallBackend(CeedOperatorGetFields(op, NULL, &op_fields, NULL, NULL)); @@ -105,6 +106,7 @@ static int CeedOperatorSetupFields_Opt(CeedQFunction qf, CeedOperator op, bool i // Empty case - won't occur break; } + CeedCallBackend(CeedDestroy(&ceed_rstr)); CeedCallBackend(CeedElemRestrictionDestroy(&rstr)); CeedCallBackend(CeedElemRestrictionCreateVector(block_rstr[i + start_e], NULL, &e_vecs_full[i + start_e])); } @@ -193,6 +195,7 @@ static int CeedOperatorSetupFields_Opt(CeedQFunction qf, CeedOperator op, bool i CeedCallBackend(CeedElemRestrictionDestroy(&rstr_i)); } } + CeedCallBackend(CeedDestroy(&ceed)); return CEED_ERROR_SUCCESS; } @@ -214,6 +217,7 @@ static int CeedOperatorSetup_Opt(CeedOperator op) { CeedCallBackend(CeedOperatorGetCeed(op, &ceed)); CeedCallBackend(CeedGetData(ceed, &ceed_impl)); + CeedCallBackend(CeedDestroy(&ceed)); CeedCallBackend(CeedOperatorGetData(op, &impl)); CeedCallBackend(CeedOperatorGetQFunction(op, &qf)); CeedCallBackend(CeedOperatorGetNumQuadraturePoints(op, &Q)); @@ -447,6 +451,7 @@ static int CeedOperatorApplyAdd_Opt(CeedOperator op, CeedVector in_vec, CeedVect CeedCallBackend(CeedOperatorGetCeed(op, &ceed)); CeedCallBackend(CeedGetData(ceed, &ceed_impl)); + CeedCallBackend(CeedDestroy(&ceed)); CeedCallBackend(CeedOperatorGetData(op, &impl)); CeedCallBackend(CeedOperatorGetNumElements(op, &num_elem)); CeedCallBackend(CeedOperatorGetNumQuadraturePoints(op, &Q)); @@ -712,6 +717,7 @@ static inline int CeedOperatorLinearAssembleQFunctionCore_Opt(CeedOperator op, b // Restore input arrays CeedCallBackend(CeedOperatorRestoreInputs_Opt(num_input_fields, qf_input_fields, op_input_fields, e_data, impl)); + CeedCallBackend(CeedDestroy(&ceed)); return CEED_ERROR_SUCCESS; } @@ -790,6 +796,7 @@ int CeedOperatorCreate_Opt(CeedOperator op) { CeedCallBackend(CeedSetBackendFunction(ceed, "Operator", op, "LinearAssembleQFunctionUpdate", CeedOperatorLinearAssembleQFunctionUpdate_Opt)); CeedCallBackend(CeedSetBackendFunction(ceed, "Operator", op, "ApplyAdd", CeedOperatorApplyAdd_Opt)); CeedCallBackend(CeedSetBackendFunction(ceed, "Operator", op, "Destroy", CeedOperatorDestroy_Opt)); + CeedCallBackend(CeedDestroy(&ceed)); return CEED_ERROR_SUCCESS; } diff --git a/backends/opt/ceed-opt-serial.c b/backends/opt/ceed-opt-serial.c index ac506a4ec6..66fc1a9cfb 100644 --- a/backends/opt/ceed-opt-serial.c +++ b/backends/opt/ceed-opt-serial.c @@ -37,6 +37,7 @@ static int CeedInit_Opt_Serial(const char *resource, Ceed ceed) { // Create reference Ceed that implementation will be dispatched through unless overridden CeedCallBackend(CeedInit("/cpu/self/ref/serial", &ceed_ref)); CeedCallBackend(CeedSetDelegate(ceed, ceed_ref)); + CeedCallBackend(CeedDestroy(&ceed_ref)); CeedCallBackend(CeedSetBackendFunction(ceed, "Ceed", ceed, "Destroy", CeedDestroy_Opt)); CeedCallBackend(CeedSetBackendFunction(ceed, "Ceed", ceed, "TensorContractCreate", CeedTensorContractCreate_Opt)); diff --git a/backends/ref/ceed-ref-basis.c b/backends/ref/ceed-ref-basis.c index 121669012a..550f631159 100644 --- a/backends/ref/ceed-ref-basis.c +++ b/backends/ref/ceed-ref-basis.c @@ -18,7 +18,6 @@ //------------------------------------------------------------------------------ static int CeedBasisApplyCore_Ref(CeedBasis basis, bool apply_add, CeedInt num_elem, CeedTransposeMode t_mode, CeedEvalMode eval_mode, CeedVector U, CeedVector V) { - Ceed ceed; bool is_tensor_basis, add = apply_add || (t_mode == CEED_TRANSPOSE); CeedInt dim, num_comp, q_comp, num_nodes, num_qpts; const CeedScalar *u; @@ -26,7 +25,6 @@ static int CeedBasisApplyCore_Ref(CeedBasis basis, bool apply_add, CeedInt num_e CeedTensorContract contract; CeedBasis_Ref *impl; - CeedCallBackend(CeedBasisGetCeed(basis, &ceed)); CeedCallBackend(CeedBasisGetData(basis, &impl)); CeedCallBackend(CeedBasisGetDimension(basis, &dim)); CeedCallBackend(CeedBasisGetNumComponents(basis, &num_comp)); @@ -35,7 +33,7 @@ static int CeedBasisApplyCore_Ref(CeedBasis basis, bool apply_add, CeedInt num_e CeedCallBackend(CeedBasisGetNumQuadraturePoints(basis, &num_qpts)); CeedCallBackend(CeedBasisGetTensorContract(basis, &contract)); if (U != CEED_VECTOR_NONE) CeedCallBackend(CeedVectorGetArrayRead(U, CEED_MEM_HOST, &u)); - else CeedCheck(eval_mode == CEED_EVAL_WEIGHT, ceed, CEED_ERROR_BACKEND, "An input vector is required for this CeedEvalMode"); + else CeedCheck(eval_mode == CEED_EVAL_WEIGHT, CeedBasisReturnCeed(basis), CEED_ERROR_BACKEND, "An input vector is required for this CeedEvalMode"); // Clear v if operating in transpose if (apply_add) CeedCallBackend(CeedVectorGetArray(V, CEED_MEM_HOST, &v)); else CeedCallBackend(CeedVectorGetArrayWrite(V, CEED_MEM_HOST, &v)); @@ -172,7 +170,7 @@ static int CeedBasisApplyCore_Ref(CeedBasis basis, bool apply_add, CeedInt num_e CeedInt Q = Q_1d; const CeedScalar *q_weight_1d; - CeedCheck(t_mode == CEED_NOTRANSPOSE, ceed, CEED_ERROR_BACKEND, "CEED_EVAL_WEIGHT incompatible with CEED_TRANSPOSE"); + CeedCheck(t_mode == CEED_NOTRANSPOSE, CeedBasisReturnCeed(basis), CEED_ERROR_BACKEND, "CEED_EVAL_WEIGHT incompatible with CEED_TRANSPOSE"); CeedCallBackend(CeedBasisGetQWeights(basis, &q_weight_1d)); for (CeedInt d = 0; d < dim; d++) { CeedInt pre = CeedIntPow(Q, dim - d - 1), post = CeedIntPow(Q, d); @@ -191,9 +189,9 @@ static int CeedBasisApplyCore_Ref(CeedBasis basis, bool apply_add, CeedInt num_e // LCOV_EXCL_START case CEED_EVAL_DIV: case CEED_EVAL_CURL: - return CeedError(ceed, CEED_ERROR_BACKEND, "%s not supported", CeedEvalModes[eval_mode]); + return CeedError(CeedBasisReturnCeed(basis), CEED_ERROR_BACKEND, "%s not supported", CeedEvalModes[eval_mode]); case CEED_EVAL_NONE: - return CeedError(ceed, CEED_ERROR_BACKEND, "CEED_EVAL_NONE does not make sense in this context"); + return CeedError(CeedBasisReturnCeed(basis), CEED_ERROR_BACKEND, "CEED_EVAL_NONE does not make sense in this context"); // LCOV_EXCL_STOP } } else { @@ -233,7 +231,7 @@ static int CeedBasisApplyCore_Ref(CeedBasis basis, bool apply_add, CeedInt num_e case CEED_EVAL_WEIGHT: { const CeedScalar *q_weight; - CeedCheck(t_mode == CEED_NOTRANSPOSE, ceed, CEED_ERROR_BACKEND, "CEED_EVAL_WEIGHT incompatible with CEED_TRANSPOSE"); + CeedCheck(t_mode == CEED_NOTRANSPOSE, CeedBasisReturnCeed(basis), CEED_ERROR_BACKEND, "CEED_EVAL_WEIGHT incompatible with CEED_TRANSPOSE"); CeedCallBackend(CeedBasisGetQWeights(basis, &q_weight)); for (CeedInt i = 0; i < num_qpts; i++) { for (CeedInt e = 0; e < num_elem; e++) v[i * num_elem + e] = q_weight[i]; @@ -241,7 +239,7 @@ static int CeedBasisApplyCore_Ref(CeedBasis basis, bool apply_add, CeedInt num_e } break; // LCOV_EXCL_START case CEED_EVAL_NONE: - return CeedError(ceed, CEED_ERROR_BACKEND, "CEED_EVAL_NONE does not make sense in this context"); + return CeedError(CeedBasisReturnCeed(basis), CEED_ERROR_BACKEND, "CEED_EVAL_NONE does not make sense in this context"); // LCOV_EXCL_STOP } } @@ -312,6 +310,8 @@ int CeedBasisCreateTensorH1_Ref(CeedInt dim, CeedInt P_1d, CeedInt Q_1d, const C CeedCallBackend(CeedSetBackendFunction(ceed, "Basis", basis, "Apply", CeedBasisApply_Ref)); CeedCallBackend(CeedSetBackendFunction(ceed, "Basis", basis, "ApplyAdd", CeedBasisApplyAdd_Ref)); CeedCallBackend(CeedSetBackendFunction(ceed, "Basis", basis, "Destroy", CeedBasisDestroyTensor_Ref)); + CeedCallBackend(CeedDestroy(&ceed)); + CeedCallBackend(CeedDestroy(&ceed_parent)); return CEED_ERROR_SUCCESS; } @@ -331,6 +331,8 @@ int CeedBasisCreateH1_Ref(CeedElemTopology topo, CeedInt dim, CeedInt num_nodes, CeedCallBackend(CeedSetBackendFunction(ceed, "Basis", basis, "Apply", CeedBasisApply_Ref)); CeedCallBackend(CeedSetBackendFunction(ceed, "Basis", basis, "ApplyAdd", CeedBasisApplyAdd_Ref)); + CeedCallBackend(CeedDestroy(&ceed)); + CeedCallBackend(CeedDestroy(&ceed_parent)); return CEED_ERROR_SUCCESS; } @@ -350,6 +352,8 @@ int CeedBasisCreateHdiv_Ref(CeedElemTopology topo, CeedInt dim, CeedInt num_node CeedCallBackend(CeedSetBackendFunction(ceed, "Basis", basis, "Apply", CeedBasisApply_Ref)); CeedCallBackend(CeedSetBackendFunction(ceed, "Basis", basis, "ApplyAdd", CeedBasisApplyAdd_Ref)); + CeedCallBackend(CeedDestroy(&ceed)); + CeedCallBackend(CeedDestroy(&ceed_parent)); return CEED_ERROR_SUCCESS; } @@ -369,6 +373,8 @@ int CeedBasisCreateHcurl_Ref(CeedElemTopology topo, CeedInt dim, CeedInt num_nod CeedCallBackend(CeedSetBackendFunction(ceed, "Basis", basis, "Apply", CeedBasisApply_Ref)); CeedCallBackend(CeedSetBackendFunction(ceed, "Basis", basis, "ApplyAdd", CeedBasisApplyAdd_Ref)); + CeedCallBackend(CeedDestroy(&ceed)); + CeedCallBackend(CeedDestroy(&ceed_parent)); return CEED_ERROR_SUCCESS; } diff --git a/backends/ref/ceed-ref-operator.c b/backends/ref/ceed-ref-operator.c index 4c62608d49..fa461f899a 100644 --- a/backends/ref/ceed-ref-operator.c +++ b/backends/ref/ceed-ref-operator.c @@ -30,7 +30,8 @@ static int CeedOperatorSetupFields_Ref(CeedQFunction qf, CeedOperator op, bool i CeedCallBackend(CeedOperatorGetCeed(op, &ceed)); CeedCallBackend(CeedGetParent(ceed, &ceed_parent)); - if (ceed_parent) ceed = ceed_parent; + CeedCallBackend(CeedReferenceCopy(ceed_parent, &ceed)); + CeedCallBackend(CeedDestroy(&ceed_parent)); } if (is_input) { CeedCallBackend(CeedOperatorGetFields(op, NULL, &op_fields, NULL, NULL)); @@ -134,6 +135,7 @@ static int CeedOperatorSetupFields_Ref(CeedQFunction qf, CeedOperator op, bool i CeedCallBackend(CeedElemRestrictionDestroy(&rstr_i)); } } + CeedCallBackend(CeedDestroy(&ceed)); return CEED_ERROR_SUCCESS; } @@ -472,7 +474,7 @@ static int CeedOperatorApplyAdd_Ref(CeedOperator op, CeedVector in_vec, CeedVect //------------------------------------------------------------------------------ static inline int CeedOperatorLinearAssembleQFunctionCore_Ref(CeedOperator op, bool build_objects, CeedVector *assembled, CeedElemRestriction *rstr, CeedRequest *request) { - Ceed ceed, ceed_parent; + Ceed ceed_parent; CeedInt qf_size_in, qf_size_out, Q, num_elem, num_input_fields, num_output_fields; CeedScalar *assembled_array, *e_data_full[2 * CEED_FIELD_MAX] = {NULL}; CeedQFunctionField *qf_input_fields, *qf_output_fields; @@ -480,7 +482,6 @@ static inline int CeedOperatorLinearAssembleQFunctionCore_Ref(CeedOperator op, b CeedOperatorField *op_input_fields, *op_output_fields; CeedOperator_Ref *impl; - CeedCallBackend(CeedOperatorGetCeed(op, &ceed)); CeedCallBackend(CeedOperatorGetFallbackParentCeed(op, &ceed_parent)); CeedCallBackend(CeedOperatorGetData(op, &impl)); qf_size_in = impl->qf_size_in; @@ -495,7 +496,7 @@ static inline int CeedOperatorLinearAssembleQFunctionCore_Ref(CeedOperator op, b CeedCallBackend(CeedOperatorSetup_Ref(op)); // Check for restriction only operator - CeedCheck(!impl->is_identity_rstr_op, ceed, CEED_ERROR_BACKEND, "Assembling restriction only operators is not supported"); + CeedCheck(!impl->is_identity_rstr_op, CeedOperatorReturnCeed(op), CEED_ERROR_BACKEND, "Assembling restriction only operators is not supported"); // Input Evecs and Restriction CeedCallBackend(CeedOperatorSetupInputs_Ref(num_input_fields, qf_input_fields, op_input_fields, NULL, true, e_data_full, impl, request)); @@ -516,7 +517,7 @@ static inline int CeedOperatorLinearAssembleQFunctionCore_Ref(CeedOperator op, b } CeedCallBackend(CeedVectorDestroy(&vec)); } - CeedCheck(qf_size_in > 0, ceed, CEED_ERROR_BACKEND, "Cannot assemble QFunction without active inputs and outputs"); + CeedCheck(qf_size_in > 0, CeedOperatorReturnCeed(op), CEED_ERROR_BACKEND, "Cannot assemble QFunction without active inputs and outputs"); impl->qf_size_in = qf_size_in; } @@ -535,7 +536,7 @@ static inline int CeedOperatorLinearAssembleQFunctionCore_Ref(CeedOperator op, b } CeedCallBackend(CeedVectorDestroy(&vec)); } - CeedCheck(qf_size_out > 0, ceed, CEED_ERROR_BACKEND, "Cannot assemble QFunction without active inputs and outputs"); + CeedCheck(qf_size_out > 0, CeedOperatorReturnCeed(op), CEED_ERROR_BACKEND, "Cannot assemble QFunction without active inputs and outputs"); impl->qf_size_out = qf_size_out; } @@ -644,6 +645,7 @@ static inline int CeedOperatorLinearAssembleQFunctionCore_Ref(CeedOperator op, b // Restore output CeedCallBackend(CeedVectorRestoreArray(*assembled, &assembled_array)); + CeedCallBackend(CeedDestroy(&ceed_parent)); return CEED_ERROR_SUCCESS; } @@ -678,7 +680,8 @@ static int CeedOperatorSetupFieldsAtPoints_Ref(CeedQFunction qf, CeedOperator op CeedCallBackend(CeedOperatorGetCeed(op, &ceed)); CeedCallBackend(CeedGetParent(ceed, &ceed_parent)); - if (ceed_parent) ceed = ceed_parent; + CeedCallBackend(CeedReferenceCopy(ceed_parent, &ceed)); + CeedCallBackend(CeedDestroy(&ceed_parent)); } if (is_input) { CeedCallBackend(CeedOperatorGetFields(op, NULL, &op_fields, NULL, NULL)); @@ -815,6 +818,7 @@ static int CeedOperatorSetupFieldsAtPoints_Ref(CeedQFunction qf, CeedOperator op CeedCallBackend(CeedElemRestrictionDestroy(&rstr_i)); } } + CeedCallBackend(CeedDestroy(&ceed)); return CEED_ERROR_SUCCESS; } @@ -1282,6 +1286,7 @@ static inline int CeedOperatorLinearAssembleQFunctionAtPointsCore_Ref(CeedOperat CeedCallBackend(CeedVectorRestoreArray(*assembled, &assembled_array)); // Cleanup + CeedCallBackend(CeedDestroy(&ceed)); CeedCallBackend(CeedVectorDestroy(&point_coords)); CeedCallBackend(CeedElemRestrictionDestroy(&rstr_points)); return CEED_ERROR_SUCCESS; @@ -1331,7 +1336,8 @@ static int CeedOperatorLinearAssembleAddDiagonalAtPoints_Ref(CeedOperator op, Ce CeedCallBackend(CeedOperatorGetCeed(op, &ceed)); CeedCallBackend(CeedGetParent(ceed, &ceed_parent)); - if (ceed_parent) ceed = ceed_parent; + CeedCallBackend(CeedReferenceCopy(ceed_parent, &ceed)); + CeedCallBackend(CeedDestroy(&ceed_parent)); } // Point coordinates @@ -1530,6 +1536,7 @@ static int CeedOperatorLinearAssembleAddDiagonalAtPoints_Ref(CeedOperator op, Ce CeedCallBackend(CeedOperatorRestoreInputs_Ref(num_input_fields, qf_input_fields, op_input_fields, true, e_data, impl)); // Cleanup + CeedCallBackend(CeedDestroy(&ceed)); CeedCallBackend(CeedVectorDestroy(&in_vec)); CeedCallBackend(CeedVectorDestroy(&out_vec)); CeedCallBackend(CeedVectorDestroy(&point_coords)); @@ -1587,6 +1594,7 @@ int CeedOperatorCreate_Ref(CeedOperator op) { CeedCallBackend(CeedSetBackendFunction(ceed, "Operator", op, "LinearAssembleQFunctionUpdate", CeedOperatorLinearAssembleQFunctionUpdate_Ref)); CeedCallBackend(CeedSetBackendFunction(ceed, "Operator", op, "ApplyAdd", CeedOperatorApplyAdd_Ref)); CeedCallBackend(CeedSetBackendFunction(ceed, "Operator", op, "Destroy", CeedOperatorDestroy_Ref)); + CeedCallBackend(CeedDestroy(&ceed)); return CEED_ERROR_SUCCESS; } @@ -1606,6 +1614,7 @@ int CeedOperatorCreateAtPoints_Ref(CeedOperator op) { CeedCallBackend(CeedSetBackendFunction(ceed, "Operator", op, "LinearAssembleAddDiagonal", CeedOperatorLinearAssembleAddDiagonalAtPoints_Ref)); CeedCallBackend(CeedSetBackendFunction(ceed, "Operator", op, "ApplyAdd", CeedOperatorApplyAddAtPoints_Ref)); CeedCallBackend(CeedSetBackendFunction(ceed, "Operator", op, "Destroy", CeedOperatorDestroy_Ref)); + CeedCallBackend(CeedDestroy(&ceed)); return CEED_ERROR_SUCCESS; } diff --git a/backends/ref/ceed-ref-qfunction.c b/backends/ref/ceed-ref-qfunction.c index d2bbd07ad1..efddda2dcc 100644 --- a/backends/ref/ceed-ref-qfunction.c +++ b/backends/ref/ceed-ref-qfunction.c @@ -71,6 +71,7 @@ int CeedQFunctionCreate_Ref(CeedQFunction qf) { CeedCallBackend(CeedQFunctionSetData(qf, impl)); CeedCallBackend(CeedSetBackendFunction(ceed, "QFunction", qf, "Apply", CeedQFunctionApply_Ref)); CeedCallBackend(CeedSetBackendFunction(ceed, "QFunction", qf, "Destroy", CeedQFunctionDestroy_Ref)); + CeedCallBackend(CeedDestroy(&ceed)); return CEED_ERROR_SUCCESS; } diff --git a/backends/ref/ceed-ref-qfunctioncontext.c b/backends/ref/ceed-ref-qfunctioncontext.c index 9fd2d013db..0d3c8bba36 100644 --- a/backends/ref/ceed-ref-qfunctioncontext.c +++ b/backends/ref/ceed-ref-qfunctioncontext.c @@ -131,6 +131,7 @@ int CeedQFunctionContextCreate_Ref(CeedQFunctionContext ctx) { CeedCallBackend(CeedSetBackendFunction(ceed, "QFunctionContext", ctx, "RestoreData", CeedQFunctionContextRestoreData_Ref)); CeedCallBackend(CeedSetBackendFunction(ceed, "QFunctionContext", ctx, "RestoreDataRead", CeedQFunctionContextRestoreData_Ref)); CeedCallBackend(CeedSetBackendFunction(ceed, "QFunctionContext", ctx, "Destroy", CeedQFunctionContextDestroy_Ref)); + CeedCallBackend(CeedDestroy(&ceed)); CeedCallBackend(CeedCalloc(1, &impl)); CeedCallBackend(CeedQFunctionContextSetBackendData(ctx, impl)); return CEED_ERROR_SUCCESS; diff --git a/backends/ref/ceed-ref-restriction.c b/backends/ref/ceed-ref-restriction.c index 068b9906f8..65b6cf080f 100644 --- a/backends/ref/ceed-ref-restriction.c +++ b/backends/ref/ceed-ref-restriction.c @@ -774,14 +774,11 @@ int CeedElemRestrictionCreate_Ref(CeedMemType mem_type, CeedCopyMode copy_mode, // Check indices for ref or memcheck backends { - Ceed current = ceed, parent = NULL; + Ceed current = ceed, ceed_parent = NULL; - CeedCallBackend(CeedGetParent(current, &parent)); - while (current != parent) { - current = parent; - CeedCallBackend(CeedGetParent(current, &parent)); - } - CeedCallBackend(CeedGetResource(parent, &resource)); + CeedCallBackend(CeedGetParent(current, &ceed_parent)); + CeedCallBackend(CeedGetResource(ceed_parent, &resource)); + CeedCallBackend(CeedDestroy(&ceed_parent)); } if (!strcmp(resource, "/cpu/self/ref/serial") || !strcmp(resource, "/cpu/self/ref/blocked")) { CeedSize l_size; @@ -871,6 +868,7 @@ int CeedElemRestrictionCreate_Ref(CeedMemType mem_type, CeedCopyMode copy_mode, CeedCallBackend(CeedSetBackendFunction(ceed, "ElemRestriction", rstr, "GetOrientations", CeedElemRestrictionGetOrientations_Ref)); CeedCallBackend(CeedSetBackendFunction(ceed, "ElemRestriction", rstr, "GetCurlOrientations", CeedElemRestrictionGetCurlOrientations_Ref)); CeedCallBackend(CeedSetBackendFunction(ceed, "ElemRestriction", rstr, "Destroy", CeedElemRestrictionDestroy_Ref)); + CeedCallBackend(CeedDestroy(&ceed)); return CEED_ERROR_SUCCESS; } diff --git a/backends/ref/ceed-ref-tensor.c b/backends/ref/ceed-ref-tensor.c index a2064cfce6..17499172a6 100644 --- a/backends/ref/ceed-ref-tensor.c +++ b/backends/ref/ceed-ref-tensor.c @@ -51,6 +51,7 @@ int CeedTensorContractCreate_Ref(CeedTensorContract contract) { CeedCallBackend(CeedTensorContractGetCeed(contract, &ceed)); CeedCallBackend(CeedSetBackendFunction(ceed, "TensorContract", contract, "Apply", CeedTensorContractApply_Ref)); CeedCallBackend(CeedSetBackendFunction(ceed, "TensorContract", contract, "Destroy", CeedTensorContractDestroy_Ref)); + CeedCallBackend(CeedDestroy(&ceed)); return CEED_ERROR_SUCCESS; } diff --git a/backends/ref/ceed-ref-vector.c b/backends/ref/ceed-ref-vector.c index f907d232c8..2af3a8770c 100644 --- a/backends/ref/ceed-ref-vector.c +++ b/backends/ref/ceed-ref-vector.c @@ -149,6 +149,7 @@ int CeedVectorCreate_Ref(CeedSize n, CeedVector vec) { CeedCallBackend(CeedSetBackendFunction(ceed, "Vector", vec, "RestoreArray", CeedVectorRestoreArray_Ref)); CeedCallBackend(CeedSetBackendFunction(ceed, "Vector", vec, "RestoreArrayRead", CeedVectorRestoreArrayRead_Ref)); CeedCallBackend(CeedSetBackendFunction(ceed, "Vector", vec, "Destroy", CeedVectorDestroy_Ref)); + CeedCallBackend(CeedDestroy(&ceed)); CeedCallBackend(CeedCalloc(1, &impl)); CeedCallBackend(CeedVectorSetData(vec, impl)); return CEED_ERROR_SUCCESS; diff --git a/backends/sycl-gen/ceed-sycl-gen-operator-build.sycl.cpp b/backends/sycl-gen/ceed-sycl-gen-operator-build.sycl.cpp index ee7aab812c..f1fb58e42f 100644 --- a/backends/sycl-gen/ceed-sycl-gen-operator-build.sycl.cpp +++ b/backends/sycl-gen/ceed-sycl-gen-operator-build.sycl.cpp @@ -780,8 +780,8 @@ extern "C" int CeedOperatorBuildKernel_Sycl_gen(CeedOperator op) { // Load kernel function CeedCallBackend(CeedGetKernel_Sycl(ceed, impl->sycl_module, operator_name, &impl->op)); - CeedCallBackend(CeedOperatorSetSetupDone(op)); + CeedCallBackend(CeedDestroy(&ceed)); return CEED_ERROR_SUCCESS; } diff --git a/backends/sycl-gen/ceed-sycl-gen-operator.sycl.cpp b/backends/sycl-gen/ceed-sycl-gen-operator.sycl.cpp index 0736446f4c..eb74176bbf 100644 --- a/backends/sycl-gen/ceed-sycl-gen-operator.sycl.cpp +++ b/backends/sycl-gen/ceed-sycl-gen-operator.sycl.cpp @@ -58,6 +58,7 @@ static int CeedOperatorApplyAdd_Sycl_gen(CeedOperator op, CeedVector input_vec, CeedOperator op_fallback; CeedDebug256(ceed, CEED_DEBUG_COLOR_SUCCESS, "Falling back to sycl/ref CeedOperator due to non-tensor bases"); + CeedCallBackend(CeedDestroy(&ceed)); CeedCallBackend(CeedOperatorGetFallback(op, &op_fallback)); CeedCallBackend(CeedOperatorApplyAdd(op_fallback, input_vec, output_vec, request)); return CEED_ERROR_SUCCESS; @@ -198,6 +199,7 @@ static int CeedOperatorApplyAdd_Sycl_gen(CeedOperator op, CeedVector input_vec, // Restore context data CeedCallBackend(CeedQFunctionRestoreInnerContextData(qf, &qf_impl->d_c)); + CeedCallBackend(CeedDestroy(&ceed)); return CEED_ERROR_SUCCESS; } @@ -223,6 +225,7 @@ int CeedOperatorCreate_Sycl_gen(CeedOperator op) { CeedCallBackend(CeedSetBackendFunctionCpp(ceed, "Operator", op, "ApplyAdd", CeedOperatorApplyAdd_Sycl_gen)); CeedCallBackend(CeedSetBackendFunctionCpp(ceed, "Operator", op, "Destroy", CeedOperatorDestroy_Sycl_gen)); + CeedCallBackend(CeedDestroy(&ceed)); return CEED_ERROR_SUCCESS; } diff --git a/backends/sycl-gen/ceed-sycl-gen-qfunction.sycl.cpp b/backends/sycl-gen/ceed-sycl-gen-qfunction.sycl.cpp index 05774e6237..e810bfbf7a 100644 --- a/backends/sycl-gen/ceed-sycl-gen-qfunction.sycl.cpp +++ b/backends/sycl-gen/ceed-sycl-gen-qfunction.sycl.cpp @@ -38,6 +38,7 @@ static int CeedQFunctionDestroy_Sycl_gen(CeedQFunction qf) { CeedCallBackend(CeedFree(&impl->qfunction_source)); CeedCallBackend(CeedFree(&impl)); + CeedCallBackend(CeedDestroy(&ceed)); return CEED_ERROR_SUCCESS; } @@ -61,6 +62,7 @@ int CeedQFunctionCreate_Sycl_gen(CeedQFunction qf) { CeedCallBackend(CeedSetBackendFunctionCpp(ceed, "QFunction", qf, "Apply", CeedQFunctionApply_Sycl_gen)); CeedCallBackend(CeedSetBackendFunctionCpp(ceed, "QFunction", qf, "Destroy", CeedQFunctionDestroy_Sycl_gen)); + CeedCallBackend(CeedDestroy(&ceed)); return CEED_ERROR_SUCCESS; } diff --git a/backends/sycl-gen/ceed-sycl-gen.sycl.cpp b/backends/sycl-gen/ceed-sycl-gen.sycl.cpp index 2cee0469a7..2a1ed4ad49 100644 --- a/backends/sycl-gen/ceed-sycl-gen.sycl.cpp +++ b/backends/sycl-gen/ceed-sycl-gen.sycl.cpp @@ -35,12 +35,14 @@ static int CeedInit_Sycl_gen(const char *resource, Ceed ceed) { CeedCallBackend(CeedInit("/gpu/sycl/shared", &ceed_shared)); CeedCallBackend(CeedSetDelegate(ceed, ceed_shared)); CeedCallBackend(CeedSetStream_Sycl(ceed_shared, &(data->sycl_queue))); + CeedCallBackend(CeedDestroy(&ceed_shared)); CeedCallBackend(CeedSetOperatorFallbackResource(ceed, fallback_resource)); Ceed ceed_fallback = NULL; CeedCallBackend(CeedGetOperatorFallbackCeed(ceed, &ceed_fallback)); CeedCallBackend(CeedSetStream_Sycl(ceed_fallback, &(data->sycl_queue))); + CeedCallBackend(CeedDestroy(&ceed_fallback)); CeedCallBackend(CeedSetBackendFunctionCpp(ceed, "Ceed", ceed, "QFunctionCreate", CeedQFunctionCreate_Sycl_gen)); CeedCallBackend(CeedSetBackendFunctionCpp(ceed, "Ceed", ceed, "OperatorCreate", CeedOperatorCreate_Sycl_gen)); diff --git a/backends/sycl-ref/ceed-sycl-ref-basis.sycl.cpp b/backends/sycl-ref/ceed-sycl-ref-basis.sycl.cpp index e5495fe83f..1330d61a6a 100644 --- a/backends/sycl-ref/ceed-sycl-ref-basis.sycl.cpp +++ b/backends/sycl-ref/ceed-sycl-ref-basis.sycl.cpp @@ -328,6 +328,7 @@ static int CeedBasisApply_Sycl(CeedBasis basis, const CeedInt num_elem, CeedTran CeedCallBackend(CeedVectorRestoreArray(v, &d_v)); if (eval_mode == CEED_EVAL_NONE) CeedCallBackend(CeedVectorSetArray(v, CEED_MEM_DEVICE, CEED_COPY_VALUES, (CeedScalar *)d_u)); if (eval_mode != CEED_EVAL_WEIGHT) CeedCallBackend(CeedVectorRestoreArrayRead(u, &d_u)); + CeedCallBackend(CeedDestroy(&ceed)); return CEED_ERROR_SUCCESS; } @@ -504,7 +505,7 @@ static int CeedBasisApplyNonTensor_Sycl(CeedBasis basis, const CeedInt num_elem, CeedCallBackend(CeedVectorRestoreArray(v, &d_v)); if (eval_mode == CEED_EVAL_NONE) CeedCallBackend(CeedVectorSetArray(v, CEED_MEM_DEVICE, CEED_COPY_VALUES, (CeedScalar *)d_u)); if (eval_mode != CEED_EVAL_WEIGHT) CeedCallBackend(CeedVectorRestoreArrayRead(u, &d_u)); - + CeedCallBackend(CeedDestroy(&ceed)); return CEED_ERROR_SUCCESS; } @@ -527,6 +528,7 @@ static int CeedBasisDestroy_Sycl(CeedBasis basis) { CeedCallSycl(ceed, sycl::free(impl->d_grad_1d, data->sycl_context)); CeedCallBackend(CeedFree(&impl)); + CeedCallBackend(CeedDestroy(&ceed)); return CEED_ERROR_SUCCESS; } @@ -549,6 +551,7 @@ static int CeedBasisDestroyNonTensor_Sycl(CeedBasis basis) { CeedCallSycl(ceed, sycl::free(impl->d_grad, data->sycl_context)); CeedCallBackend(CeedFree(&impl)); + CeedCallBackend(CeedDestroy(&ceed)); return CEED_ERROR_SUCCESS; } @@ -557,11 +560,12 @@ static int CeedBasisDestroyNonTensor_Sycl(CeedBasis basis) { //------------------------------------------------------------------------------ int CeedBasisCreateTensorH1_Sycl(CeedInt dim, CeedInt P_1d, CeedInt Q_1d, const CeedScalar *interp_1d, const CeedScalar *grad_1d, const CeedScalar *q_ref_1d, const CeedScalar *q_weight_1d, CeedBasis basis) { - Ceed ceed; - CeedCallBackend(CeedBasisGetCeed(basis, &ceed)); + Ceed ceed; CeedBasis_Sycl *impl; + Ceed_Sycl *data; + + CeedCallBackend(CeedBasisGetCeed(basis, &ceed)); CeedCallBackend(CeedCalloc(1, &impl)); - Ceed_Sycl *data; CeedCallBackend(CeedGetData(ceed, &data)); CeedInt num_comp; @@ -617,6 +621,7 @@ int CeedBasisCreateTensorH1_Sycl(CeedInt dim, CeedInt P_1d, CeedInt Q_1d, const // Register backend functions CeedCallBackend(CeedSetBackendFunctionCpp(ceed, "Basis", basis, "Apply", CeedBasisApply_Sycl)); CeedCallBackend(CeedSetBackendFunctionCpp(ceed, "Basis", basis, "Destroy", CeedBasisDestroy_Sycl)); + CeedCallBackend(CeedDestroy(&ceed)); return CEED_ERROR_SUCCESS; } @@ -625,11 +630,12 @@ int CeedBasisCreateTensorH1_Sycl(CeedInt dim, CeedInt P_1d, CeedInt Q_1d, const //------------------------------------------------------------------------------ int CeedBasisCreateH1_Sycl(CeedElemTopology topo, CeedInt dim, CeedInt num_nodes, CeedInt num_qpts, const CeedScalar *interp, const CeedScalar *grad, const CeedScalar *q_ref, const CeedScalar *q_weight, CeedBasis basis) { - Ceed ceed; - CeedCallBackend(CeedBasisGetCeed(basis, &ceed)); + Ceed ceed; CeedBasisNonTensor_Sycl *impl; + Ceed_Sycl *data; + + CeedCallBackend(CeedBasisGetCeed(basis, &ceed)); CeedCallBackend(CeedCalloc(1, &impl)); - Ceed_Sycl *data; CeedCallBackend(CeedGetData(ceed, &data)); CeedInt num_comp; @@ -668,6 +674,7 @@ int CeedBasisCreateH1_Sycl(CeedElemTopology topo, CeedInt dim, CeedInt num_nodes // Register backend functions CeedCallBackend(CeedSetBackendFunctionCpp(ceed, "Basis", basis, "Apply", CeedBasisApplyNonTensor_Sycl)); CeedCallBackend(CeedSetBackendFunctionCpp(ceed, "Basis", basis, "Destroy", CeedBasisDestroyNonTensor_Sycl)); + CeedCallBackend(CeedDestroy(&ceed)); return CEED_ERROR_SUCCESS; } diff --git a/backends/sycl-ref/ceed-sycl-ref-operator.sycl.cpp b/backends/sycl-ref/ceed-sycl-ref-operator.sycl.cpp index e43981c217..e843015df1 100644 --- a/backends/sycl-ref/ceed-sycl-ref-operator.sycl.cpp +++ b/backends/sycl-ref/ceed-sycl-ref-operator.sycl.cpp @@ -107,6 +107,7 @@ static int CeedOperatorDestroy_Sycl(CeedOperator op) { CeedCallBackend(CeedFree(&impl->asmb)); CeedCallBackend(CeedFree(&impl)); + CeedCallBackend(CeedDestroy(&ceed)); return CEED_ERROR_SUCCESS; } @@ -206,6 +207,7 @@ static int CeedOperatorSetupFields_Sycl(CeedQFunction qf, CeedOperator op, bool break; // TODO: Not implemented } } + CeedCallBackend(CeedDestroy(&ceed)); return CEED_ERROR_SUCCESS; } @@ -214,7 +216,6 @@ static int CeedOperatorSetupFields_Sycl(CeedQFunction qf, CeedOperator op, bool // passive) to the named inputs and outputs of its CeedQFunction. //------------------------------------------------------------------------------ static int CeedOperatorSetup_Sycl(CeedOperator op) { - Ceed ceed; bool is_setup_done; CeedInt Q, num_elem, num_input_fields, num_output_fields; CeedQFunctionField *qf_input_fields, *qf_output_fields; @@ -225,7 +226,6 @@ static int CeedOperatorSetup_Sycl(CeedOperator op) { CeedCallBackend(CeedOperatorIsSetupDone(op, &is_setup_done)); if (is_setup_done) return CEED_ERROR_SUCCESS; - CeedCallBackend(CeedOperatorGetCeed(op, &ceed)); CeedCallBackend(CeedOperatorGetData(op, &impl)); CeedCallBackend(CeedOperatorGetQFunction(op, &qf)); CeedCallBackend(CeedOperatorGetNumQuadraturePoints(op, &Q)); @@ -432,22 +432,14 @@ static int CeedOperatorApplyAdd_Sycl(CeedOperator op, CeedVector in_vec, CeedVec break; } // LCOV_EXCL_START - case CEED_EVAL_WEIGHT: { - Ceed ceed; - - CeedCallBackend(CeedOperatorGetCeed(op, &ceed)); - return CeedError(ceed, CEED_ERROR_BACKEND, "CEED_EVAL_WEIGHT cannot be an output evaluation mode"); + case CEED_EVAL_WEIGHT: + return CeedError(CeedOperatorReturnCeed(op), CEED_ERROR_BACKEND, "CEED_EVAL_WEIGHT cannot be an output evaluation mode"); break; // Should not occur - } case CEED_EVAL_DIV: - case CEED_EVAL_CURL: { - Ceed ceed; - - CeedCallBackend(CeedOperatorGetCeed(op, &ceed)); - return CeedError(ceed, CEED_ERROR_BACKEND, "%s not supported", CeedEvalModes[eval_mode]); + case CEED_EVAL_CURL: + return CeedError(CeedOperatorReturnCeed(op), CEED_ERROR_BACKEND, "%s not supported", CeedEvalModes[eval_mode]); break; // Should not occur - } - // LCOV_EXCL_STOP + // LCOV_EXCL_STOP } } @@ -483,7 +475,7 @@ static int CeedOperatorApplyAdd_Sycl(CeedOperator op, CeedVector in_vec, CeedVec //------------------------------------------------------------------------------ static inline int CeedOperatorLinearAssembleQFunctionCore_Sycl(CeedOperator op, bool build_objects, CeedVector *assembled, CeedElemRestriction *elem_rstr, CeedRequest *request) { - Ceed ceed, ceed_parent; + Ceed ceed_parent; CeedSize q_size; CeedInt num_active_in, num_active_out, Q, num_elem, num_input_fields, num_output_fields, size; CeedScalar *assembled_array, *e_data[2 * CEED_FIELD_MAX] = {NULL}; @@ -493,7 +485,6 @@ static inline int CeedOperatorLinearAssembleQFunctionCore_Sycl(CeedOperator op, CeedOperatorField *op_input_fields, *op_output_fields; CeedOperator_Sycl *impl; - CeedCallBackend(CeedOperatorGetCeed(op, &ceed)); CeedCallBackend(CeedOperatorGetFallbackParentCeed(op, &ceed_parent)); CeedCallBackend(CeedOperatorGetData(op, &impl)); CeedCallBackend(CeedOperatorGetQFunction(op, &qf)); @@ -525,7 +516,7 @@ static inline int CeedOperatorLinearAssembleQFunctionCore_Sycl(CeedOperator op, CeedCallBackend(CeedRealloc(num_active_in + size, &active_in)); for (CeedInt field = 0; field < size; field++) { q_size = (CeedSize)Q * num_elem; - CeedCallBackend(CeedVectorCreate(ceed, q_size, &active_in[num_active_in + field])); + CeedCallBackend(CeedVectorCreate(ceed_parent, q_size, &active_in[num_active_in + field])); CeedCallBackend( CeedVectorSetArray(active_in[num_active_in + field], CEED_MEM_DEVICE, CEED_USE_POINTER, &q_vec_array[field * Q * num_elem])); } @@ -555,7 +546,8 @@ static inline int CeedOperatorLinearAssembleQFunctionCore_Sycl(CeedOperator op, } // Check sizes - CeedCheck(num_active_in > 0 && num_active_out > 0, ceed, CEED_ERROR_BACKEND, "Cannot assemble QFunction without active inputs and outputs"); + CeedCheck(num_active_in > 0 && num_active_out > 0, CeedOperatorReturnCeed(op), CEED_ERROR_BACKEND, + "Cannot assemble QFunction without active inputs and outputs"); // Build objects if needed if (build_objects) { @@ -614,6 +606,7 @@ static inline int CeedOperatorLinearAssembleQFunctionCore_Sycl(CeedOperator op, // Restore output CeedCallBackend(CeedVectorRestoreArray(*assembled, &assembled_array)); + CeedCallBackend(CeedDestroy(&ceed_parent)); return CEED_ERROR_SUCCESS; } @@ -820,6 +813,7 @@ static inline int CeedOperatorAssembleDiagonalSetup_Sycl(CeedOperator op) { CeedCallBackend(CeedElemRestrictionDestroy(&rstr_out)); // Cleanup + CeedCallBackend(CeedDestroy(&ceed)); CeedCallBackend(CeedBasisDestroy(&basis_in)); CeedCallBackend(CeedBasisDestroy(&basis_out)); @@ -921,8 +915,9 @@ static inline int CeedOperatorAssembleDiagonalCore_Sycl(CeedOperator op, CeedVec CeedOperator_Sycl *impl; CeedCallBackend(CeedOperatorGetCeed(op, &ceed)); - CeedCallBackend(CeedOperatorGetData(op, &impl)); CeedCallBackend(CeedGetData(ceed, &sycl_data)); + CeedCallBackend(CeedDestroy(&ceed)); + CeedCallBackend(CeedOperatorGetData(op, &impl)); // Assemble QFunction { @@ -1175,6 +1170,7 @@ static int CeedSingleOperatorAssembleSetup_Sycl(CeedOperator op) { mat_start += dim * elem_size * num_qpts; } } + CeedCallBackend(CeedDestroy(&ceed)); CeedCallBackend(CeedElemRestrictionDestroy(&rstr_in)); CeedCallBackend(CeedElemRestrictionDestroy(&rstr_out)); CeedCallBackend(CeedBasisDestroy(&basis_in)); @@ -1346,8 +1342,9 @@ static int CeedSingleOperatorAssemble_Sycl(CeedOperator op, CeedInt offset, Ceed CeedOperator_Sycl *impl; CeedCallBackend(CeedOperatorGetCeed(op, &ceed)); - CeedCallBackend(CeedOperatorGetData(op, &impl)); CeedCallBackend(CeedGetData(ceed, &sycl_data)); + CeedCallBackend(CeedDestroy(&ceed)); + CeedCallBackend(CeedOperatorGetData(op, &impl)); // Setup if (!impl->asmb) { @@ -1398,6 +1395,7 @@ int CeedOperatorCreate_Sycl(CeedOperator op) { CeedCallBackend(CeedSetBackendFunctionCpp(ceed, "Operator", op, "LinearAssembleSingle", CeedSingleOperatorAssemble_Sycl)); CeedCallBackend(CeedSetBackendFunctionCpp(ceed, "Operator", op, "ApplyAdd", CeedOperatorApplyAdd_Sycl)); CeedCallBackend(CeedSetBackendFunctionCpp(ceed, "Operator", op, "Destroy", CeedOperatorDestroy_Sycl)); + CeedCallBackend(CeedDestroy(&ceed)); return CEED_ERROR_SUCCESS; } diff --git a/backends/sycl-ref/ceed-sycl-ref-qfunction-load.sycl.cpp b/backends/sycl-ref/ceed-sycl-ref-qfunction-load.sycl.cpp index 1d3cf330ad..606a1f45ad 100644 --- a/backends/sycl-ref/ceed-sycl-ref-qfunction-load.sycl.cpp +++ b/backends/sycl-ref/ceed-sycl-ref-qfunction-load.sycl.cpp @@ -35,8 +35,8 @@ extern "C" int CeedQFunctionBuildKernel_Sycl(CeedQFunction qf) { CeedQFunctionField *input_fields, *output_fields; CeedQFunction_Sycl *impl; - CeedCallBackend(CeedQFunctionGetData(qf, (void **)&impl)); // QFunction is built + CeedCallBackend(CeedQFunctionGetData(qf, (void **)&impl)); if (impl->QFunction) return CEED_ERROR_SUCCESS; CeedCallBackend(CeedQFunctionGetCeed(qf, &ceed)); @@ -175,6 +175,7 @@ extern "C" int CeedQFunctionBuildKernel_Sycl(CeedQFunction qf) { CeedCallBackend(CeedFree(&qfunction_source)); CeedCallBackend(CeedFree(&read_write_kernel_path)); CeedCallBackend(CeedFree(&read_write_kernel_source)); + CeedCallBackend(CeedDestroy(&ceed)); return CEED_ERROR_SUCCESS; } diff --git a/backends/sycl-ref/ceed-sycl-ref-qfunction.sycl.cpp b/backends/sycl-ref/ceed-sycl-ref-qfunction.sycl.cpp index 4de8fcf379..6a2c7f060b 100644 --- a/backends/sycl-ref/ceed-sycl-ref-qfunction.sycl.cpp +++ b/backends/sycl-ref/ceed-sycl-ref-qfunction.sycl.cpp @@ -37,6 +37,7 @@ static int CeedQFunctionApply_Sycl(CeedQFunction qf, CeedInt Q, CeedVector *U, C CeedCallBackend(CeedQFunctionGetCeed(qf, &ceed)); CeedCallBackend(CeedGetData(ceed, &ceed_Sycl)); + CeedCallBackend(CeedDestroy(&ceed)); CeedCallBackend(CeedQFunctionGetNumArgs(qf, &num_input_fields, &num_output_fields)); @@ -118,6 +119,7 @@ static int CeedQFunctionDestroy_Sycl(CeedQFunction qf) { delete impl->QFunction; delete impl->sycl_module; CeedCallBackend(CeedFree(&impl)); + CeedCallBackend(CeedDestroy(&ceed)); return CEED_ERROR_SUCCESS; } @@ -134,6 +136,7 @@ int CeedQFunctionCreate_Sycl(CeedQFunction qf) { // Register backend functions CeedCallBackend(CeedSetBackendFunctionCpp(ceed, "QFunction", qf, "Apply", CeedQFunctionApply_Sycl)); CeedCallBackend(CeedSetBackendFunctionCpp(ceed, "QFunction", qf, "Destroy", CeedQFunctionDestroy_Sycl)); + CeedCallBackend(CeedDestroy(&ceed)); return CEED_ERROR_SUCCESS; } diff --git a/backends/sycl-ref/ceed-sycl-ref-qfunctioncontext.sycl.cpp b/backends/sycl-ref/ceed-sycl-ref-qfunctioncontext.sycl.cpp index 1c942a645b..7130a0dead 100644 --- a/backends/sycl-ref/ceed-sycl-ref-qfunctioncontext.sycl.cpp +++ b/backends/sycl-ref/ceed-sycl-ref-qfunctioncontext.sycl.cpp @@ -42,6 +42,7 @@ static inline int CeedQFunctionContextSyncH2D_Sycl(const CeedQFunctionContext ct if (!sycl_data->sycl_queue.is_in_order()) e = {sycl_data->sycl_queue.ext_oneapi_submit_barrier()}; sycl::event copy_event = sycl_data->sycl_queue.memcpy(impl->d_data, impl->h_data, ctx_size, e); CeedCallSycl(ceed, copy_event.wait_and_throw()); + CeedCallBackend(CeedDestroy(&ceed)); return CEED_ERROR_SUCCESS; } @@ -75,6 +76,7 @@ static inline int CeedQFunctionContextSyncD2H_Sycl(const CeedQFunctionContext ct if (!sycl_data->sycl_queue.is_in_order()) e = {sycl_data->sycl_queue.ext_oneapi_submit_barrier()}; sycl::event copy_event = sycl_data->sycl_queue.memcpy(impl->h_data, impl->d_data, ctx_size, e); CeedCallSycl(ceed, copy_event.wait_and_throw()); + CeedCallBackend(CeedDestroy(&ceed)); return CEED_ERROR_SUCCESS; } @@ -229,6 +231,7 @@ static int CeedQFunctionContextSetDataDevice_Sycl(const CeedQFunctionContext ctx impl->d_data = data; } break; } + CeedCallBackend(CeedDestroy(&ceed)); return CEED_ERROR_SUCCESS; } @@ -237,9 +240,6 @@ static int CeedQFunctionContextSetDataDevice_Sycl(const CeedQFunctionContext ctx // freeing any previously allocated data if applicable //------------------------------------------------------------------------------ static int CeedQFunctionContextSetData_Sycl(const CeedQFunctionContext ctx, const CeedMemType mem_type, const CeedCopyMode copy_mode, void *data) { - Ceed ceed; - - CeedCallBackend(CeedQFunctionContextGetCeed(ctx, &ceed)); CeedCallBackend(CeedQFunctionContextSetAllInvalid_Sycl(ctx)); switch (mem_type) { case CEED_MEM_HOST: @@ -260,8 +260,9 @@ static int CeedQFunctionContextTakeData_Sycl(const CeedQFunctionContext ctx, con CeedQFunctionContext_Sycl *impl; CeedCallBackend(CeedQFunctionContextGetCeed(ctx, &ceed)); - CeedCallBackend(CeedQFunctionContextGetBackendData(ctx, &impl)); CeedCallBackend(CeedGetData(ceed, &ceedSycl)); + CeedCallBackend(CeedDestroy(&ceed)); + CeedCallBackend(CeedQFunctionContextGetBackendData(ctx, &impl)); // Order queue if needed if (!ceedSycl->sycl_queue.is_in_order()) ceedSycl->sycl_queue.ext_oneapi_submit_barrier(); @@ -291,11 +292,9 @@ static int CeedQFunctionContextTakeData_Sycl(const CeedQFunctionContext ctx, con // If a different memory type is most up to date, this will perform a copy //------------------------------------------------------------------------------ static int CeedQFunctionContextGetDataCore_Sycl(const CeedQFunctionContext ctx, const CeedMemType mem_type, void *data) { - Ceed ceed; bool need_sync = false; CeedQFunctionContext_Sycl *impl; - CeedCallBackend(CeedQFunctionContextGetCeed(ctx, &ceed)); CeedCallBackend(CeedQFunctionContextGetBackendData(ctx, &impl)); // Sync data to requested mem_type @@ -325,11 +324,9 @@ static int CeedQFunctionContextGetDataRead_Sycl(const CeedQFunctionContext ctx, // Get read/write access to the data //------------------------------------------------------------------------------ static int CeedQFunctionContextGetData_Sycl(const CeedQFunctionContext ctx, const CeedMemType mem_type, void *data) { - Ceed ceed; CeedQFunctionContext_Sycl *impl; CeedCallBackend(CeedQFunctionContextGetBackendData(ctx, &impl)); - CeedCallBackend(CeedQFunctionContextGetCeed(ctx, &ceed)); CeedCallBackend(CeedQFunctionContextGetDataCore_Sycl(ctx, mem_type, data)); // Mark only pointer for requested memory as valid @@ -360,6 +357,7 @@ static int CeedQFunctionContextDestroy_Sycl(const CeedQFunctionContext ctx) { // Wait for all work to finish before freeing memory CeedCallSycl(ceed, sycl_data->sycl_queue.wait_and_throw()); CeedCallSycl(ceed, sycl::free(impl->d_data_owned, sycl_data->sycl_context)); + CeedCallBackend(CeedDestroy(&ceed)); CeedCallBackend(CeedFree(&impl->h_data_owned)); CeedCallBackend(CeedFree(&impl)); return CEED_ERROR_SUCCESS; @@ -380,6 +378,7 @@ int CeedQFunctionContextCreate_Sycl(CeedQFunctionContext ctx) { CeedCallBackend(CeedSetBackendFunctionCpp(ceed, "QFunctionContext", ctx, "GetData", CeedQFunctionContextGetData_Sycl)); CeedCallBackend(CeedSetBackendFunctionCpp(ceed, "QFunctionContext", ctx, "GetDataRead", CeedQFunctionContextGetDataRead_Sycl)); CeedCallBackend(CeedSetBackendFunctionCpp(ceed, "QFunctionContext", ctx, "Destroy", CeedQFunctionContextDestroy_Sycl)); + CeedCallBackend(CeedDestroy(&ceed)); CeedCallBackend(CeedCalloc(1, &impl)); CeedCallBackend(CeedQFunctionContextSetBackendData(ctx, impl)); return CEED_ERROR_SUCCESS; diff --git a/backends/sycl-ref/ceed-sycl-restriction.sycl.cpp b/backends/sycl-ref/ceed-sycl-restriction.sycl.cpp index d85d036587..07a451213b 100644 --- a/backends/sycl-ref/ceed-sycl-restriction.sycl.cpp +++ b/backends/sycl-ref/ceed-sycl-restriction.sycl.cpp @@ -195,6 +195,7 @@ static int CeedElemRestrictionApply_Sycl(CeedElemRestriction rstr, CeedTranspose // Restore arrays CeedCallBackend(CeedVectorRestoreArrayRead(u, &d_u)); CeedCallBackend(CeedVectorRestoreArray(v, &d_v)); + CeedCallBackend(CeedDestroy(&ceed)); return CEED_ERROR_SUCCESS; } @@ -202,10 +203,8 @@ static int CeedElemRestrictionApply_Sycl(CeedElemRestriction rstr, CeedTranspose // Get offsets //------------------------------------------------------------------------------ static int CeedElemRestrictionGetOffsets_Sycl(CeedElemRestriction rstr, CeedMemType m_type, const CeedInt **offsets) { - Ceed ceed; CeedElemRestriction_Sycl *impl; - CeedCallBackend(CeedElemRestrictionGetCeed(rstr, &ceed)); CeedCallBackend(CeedElemRestrictionGetData(rstr, &impl)); switch (m_type) { @@ -240,6 +239,7 @@ static int CeedElemRestrictionDestroy_Sycl(CeedElemRestriction rstr) { CeedCallSycl(ceed, sycl::free(impl->d_t_indices, data->sycl_context)); CeedCallSycl(ceed, sycl::free(impl->d_l_vec_indices, data->sycl_context)); CeedCallBackend(CeedFree(&impl)); + CeedCallBackend(CeedDestroy(&ceed)); return CEED_ERROR_SUCCESS; } @@ -328,6 +328,7 @@ static int CeedElemRestrictionOffset_Sycl(const CeedElemRestriction rstr, const CeedCallBackend(CeedFree(&l_vec_indices)); CeedCallBackend(CeedFree(&t_offsets)); CeedCallBackend(CeedFree(&t_indices)); + CeedCallBackend(CeedDestroy(&ceed)); return CEED_ERROR_SUCCESS; } @@ -472,5 +473,6 @@ int CeedElemRestrictionCreate_Sycl(CeedMemType mem_type, CeedCopyMode copy_mode, CeedCallBackend(CeedSetBackendFunctionCpp(ceed, "ElemRestriction", rstr, "ApplyUnoriented", CeedElemRestrictionApply_Sycl)); CeedCallBackend(CeedSetBackendFunctionCpp(ceed, "ElemRestriction", rstr, "GetOffsets", CeedElemRestrictionGetOffsets_Sycl)); CeedCallBackend(CeedSetBackendFunctionCpp(ceed, "ElemRestriction", rstr, "Destroy", CeedElemRestrictionDestroy_Sycl)); + CeedCallBackend(CeedDestroy(&ceed)); return CEED_ERROR_SUCCESS; } diff --git a/backends/sycl-ref/ceed-sycl-vector.sycl.cpp b/backends/sycl-ref/ceed-sycl-vector.sycl.cpp index 427f51f727..32dda419f0 100644 --- a/backends/sycl-ref/ceed-sycl-vector.sycl.cpp +++ b/backends/sycl-ref/ceed-sycl-vector.sycl.cpp @@ -44,8 +44,9 @@ static inline int CeedVectorSyncH2D_Sycl(const CeedVector vec) { CeedVector_Sycl *impl; CeedCallBackend(CeedVectorGetCeed(vec, &ceed)); - CeedCallBackend(CeedVectorGetData(vec, &impl)); CeedCallBackend(CeedGetData(ceed, &data)); + CeedCallBackend(CeedVectorGetData(vec, &impl)); + CeedCheck(impl->h_array, ceed, CEED_ERROR_BACKEND, "No valid host data to sync to device"); CeedCallBackend(CeedVectorGetLength(vec, &length)); @@ -63,6 +64,7 @@ static inline int CeedVectorSyncH2D_Sycl(const CeedVector vec) { if (!data->sycl_queue.is_in_order()) e = {data->sycl_queue.ext_oneapi_submit_barrier()}; CeedCallSycl(ceed, data->sycl_queue.copy(impl->h_array, impl->d_array, length, e).wait_and_throw()); + CeedCallBackend(CeedDestroy(&ceed)); return CEED_ERROR_SUCCESS; } @@ -76,8 +78,8 @@ static inline int CeedVectorSyncD2H_Sycl(const CeedVector vec) { CeedVector_Sycl *impl; CeedCallBackend(CeedVectorGetCeed(vec, &ceed)); - CeedCallBackend(CeedVectorGetData(vec, &impl)); CeedCallBackend(CeedGetData(ceed, &data)); + CeedCallBackend(CeedVectorGetData(vec, &impl)); CeedCheck(impl->d_array, ceed, CEED_ERROR_BACKEND, "No valid device data to sync to host"); @@ -96,6 +98,7 @@ static inline int CeedVectorSyncD2H_Sycl(const CeedVector vec) { if (!data->sycl_queue.is_in_order()) e = {data->sycl_queue.ext_oneapi_submit_barrier()}; CeedCallSycl(ceed, data->sycl_queue.copy(impl->d_array, impl->h_array, length, e).wait_and_throw()); + CeedCallBackend(CeedDestroy(&ceed)); return CEED_ERROR_SUCCESS; } @@ -244,6 +247,7 @@ static int CeedVectorSetArrayDevice_Sycl(const CeedVector vec, const CeedCopyMod impl->d_array = impl->d_array_borrowed; break; } + CeedCallBackend(CeedDestroy(&ceed)); return CEED_ERROR_SUCCESS; } @@ -295,9 +299,10 @@ static int CeedVectorSetValue_Sycl(CeedVector vec, CeedScalar val) { CeedVector_Sycl *impl; CeedCallBackend(CeedVectorGetCeed(vec, &ceed)); + CeedCallBackend(CeedGetData(ceed, &data)); + CeedCallBackend(CeedDestroy(&ceed)); CeedCallBackend(CeedVectorGetData(vec, &impl)); CeedCallBackend(CeedVectorGetLength(vec, &length)); - CeedCallBackend(CeedGetData(ceed, &data)); // Set value for synced device/host array if (!impl->d_array && !impl->h_array) { @@ -333,8 +338,10 @@ static int CeedVectorTakeArray_Sycl(CeedVector vec, CeedMemType mem_type, CeedSc CeedVector_Sycl *impl; CeedCallBackend(CeedVectorGetCeed(vec, &ceed)); - CeedCallBackend(CeedVectorGetData(vec, &impl)); CeedCallBackend(CeedGetData(ceed, &data)); + CeedCallBackend(CeedVectorGetData(vec, &impl)); + CeedCallBackend(CeedDestroy(&ceed)); + CeedCallBackend(CeedVectorGetData(vec, &impl)); // Order queue if needed if (!data->sycl_queue.is_in_order()) data->sycl_queue.ext_oneapi_submit_barrier(); @@ -447,9 +454,10 @@ static int CeedVectorNorm_Sycl(CeedVector vec, CeedNormType type, CeedScalar *no CeedVector_Sycl *impl; CeedCallBackend(CeedVectorGetCeed(vec, &ceed)); + CeedCallBackend(CeedGetData(ceed, &data)); + CeedCallBackend(CeedDestroy(&ceed)); CeedCallBackend(CeedVectorGetData(vec, &impl)); CeedCallBackend(CeedVectorGetLength(vec, &length)); - CeedCallBackend(CeedGetData(ceed, &data)); // Compute norm CeedCallBackend(CeedVectorGetArrayRead(vec, CEED_MEM_DEVICE, &d_array)); @@ -515,9 +523,10 @@ static int CeedVectorReciprocal_Sycl(CeedVector vec) { CeedVector_Sycl *impl; CeedCallBackend(CeedVectorGetCeed(vec, &ceed)); + CeedCallBackend(CeedGetData(ceed, &data)); + CeedCallBackend(CeedDestroy(&ceed)); CeedCallBackend(CeedVectorGetData(vec, &impl)); CeedCallBackend(CeedVectorGetLength(vec, &length)); - CeedCallBackend(CeedGetData(ceed, &data)); // Set value for synced device/host array if (impl->d_array) CeedCallBackend(CeedDeviceReciprocal_Sycl(data->sycl_queue, impl->d_array, length)); @@ -554,9 +563,10 @@ static int CeedVectorScale_Sycl(CeedVector x, CeedScalar alpha) { CeedVector_Sycl *x_impl; CeedCallBackend(CeedVectorGetCeed(x, &ceed)); + CeedCallBackend(CeedGetData(ceed, &data)); + CeedCallBackend(CeedDestroy(&ceed)); CeedCallBackend(CeedVectorGetData(x, &x_impl)); CeedCallBackend(CeedVectorGetLength(x, &length)); - CeedCallBackend(CeedGetData(ceed, &data)); // Set value for synced device/host array if (x_impl->d_array) CeedCallBackend(CeedDeviceScale_Sycl(data->sycl_queue, x_impl->d_array, alpha, length)); @@ -593,10 +603,11 @@ static int CeedVectorAXPY_Sycl(CeedVector y, CeedScalar alpha, CeedVector x) { CeedVector_Sycl *y_impl, *x_impl; CeedCallBackend(CeedVectorGetCeed(y, &ceed)); + CeedCallBackend(CeedGetData(ceed, &data)); + CeedCallBackend(CeedDestroy(&ceed)); CeedCallBackend(CeedVectorGetData(y, &y_impl)); CeedCallBackend(CeedVectorGetData(x, &x_impl)); CeedCallBackend(CeedVectorGetLength(y, &length)); - CeedCallBackend(CeedGetData(ceed, &data)); // Set value for synced device/host array if (y_impl->d_array) { @@ -639,11 +650,12 @@ static int CeedVectorPointwiseMult_Sycl(CeedVector w, CeedVector x, CeedVector y CeedVector_Sycl *w_impl, *x_impl, *y_impl; CeedCallBackend(CeedVectorGetCeed(w, &ceed)); + CeedCallBackend(CeedGetData(ceed, &data)); + CeedCallBackend(CeedDestroy(&ceed)); CeedCallBackend(CeedVectorGetData(w, &w_impl)); CeedCallBackend(CeedVectorGetData(x, &x_impl)); CeedCallBackend(CeedVectorGetData(y, &y_impl)); CeedCallBackend(CeedVectorGetLength(w, &length)); - CeedCallBackend(CeedGetData(ceed, &data)); // Set value for synced device/host array if (!w_impl->d_array && !w_impl->h_array) { @@ -681,6 +693,7 @@ static int CeedVectorDestroy_Sycl(const CeedVector vec) { CeedCallBackend(CeedFree(&impl->h_array_owned)); CeedCallBackend(CeedFree(&impl)); + CeedCallBackend(CeedDestroy(&ceed)); return CEED_ERROR_SUCCESS; } @@ -711,6 +724,7 @@ int CeedVectorCreate_Sycl(CeedSize n, CeedVector vec) { CeedCallBackend(CeedSetBackendFunctionCpp(ceed, "Vector", vec, "Scale", CeedVectorScale_Sycl)); CeedCallBackend(CeedSetBackendFunctionCpp(ceed, "Vector", vec, "PointwiseMult", CeedVectorPointwiseMult_Sycl)); CeedCallBackend(CeedSetBackendFunctionCpp(ceed, "Vector", vec, "Destroy", CeedVectorDestroy_Sycl)); + CeedCallBackend(CeedDestroy(&ceed)); CeedCallBackend(CeedVectorSetData(vec, impl)); return CEED_ERROR_SUCCESS; } diff --git a/backends/sycl-shared/ceed-sycl-shared-basis.sycl.cpp b/backends/sycl-shared/ceed-sycl-shared-basis.sycl.cpp index d549f6cd4f..7d8302599f 100644 --- a/backends/sycl-shared/ceed-sycl-shared-basis.sycl.cpp +++ b/backends/sycl-shared/ceed-sycl-shared-basis.sycl.cpp @@ -128,7 +128,7 @@ int CeedBasisApplyTensor_Sycl_shared(CeedBasis basis, const CeedInt num_elem, Ce CeedCallBackend(CeedVectorRestoreArray(v, &d_v)); if (eval_mode == CEED_EVAL_NONE) CeedCallBackend(CeedVectorSetArray(v, CEED_MEM_DEVICE, CEED_COPY_VALUES, (CeedScalar *)d_u)); if (eval_mode != CEED_EVAL_WEIGHT) CeedCallBackend(CeedVectorRestoreArrayRead(u, &d_u)); - + CeedCallBackend(CeedDestroy(&ceed)); return CEED_ERROR_SUCCESS; } @@ -157,6 +157,7 @@ static int CeedBasisDestroy_Sycl_shared(CeedBasis basis) { delete impl->sycl_module; CeedCallBackend(CeedFree(&impl)); + CeedCallBackend(CeedDestroy(&ceed)); return CEED_ERROR_SUCCESS; } @@ -277,6 +278,7 @@ int CeedBasisCreateTensorH1_Sycl_shared(CeedInt dim, CeedInt P_1d, CeedInt Q_1d, // Register backend functions CeedCallBackend(CeedSetBackendFunctionCpp(ceed, "Basis", basis, "Apply", CeedBasisApplyTensor_Sycl_shared)); CeedCallBackend(CeedSetBackendFunctionCpp(ceed, "Basis", basis, "Destroy", CeedBasisDestroy_Sycl_shared)); + CeedCallBackend(CeedDestroy(&ceed)); return CEED_ERROR_SUCCESS; } diff --git a/backends/sycl/ceed-sycl-common.sycl.cpp b/backends/sycl/ceed-sycl-common.sycl.cpp index e51405d7fa..176b39cd84 100644 --- a/backends/sycl/ceed-sycl-common.sycl.cpp +++ b/backends/sycl/ceed-sycl-common.sycl.cpp @@ -107,12 +107,14 @@ int CeedSetStream_Sycl(Ceed ceed, void *handle) { if (ceed_delegate) { CeedCallBackend(CeedSetStream_Sycl(ceed_delegate, handle)); } + CeedCallBackend(CeedDestroy(&ceed_delegate)); // Set queue and context for Ceed Fallback object - CeedGetOperatorFallbackCeed(ceed, &ceed_fallback); + CeedCallBackend(CeedGetOperatorFallbackCeed(ceed, &ceed_fallback)); if (ceed_fallback) { CeedCallBackend(CeedSetStream_Sycl(ceed_fallback, handle)); } + CeedCallBackend(CeedDestroy(&ceed_fallback)); return CEED_ERROR_SUCCESS; } diff --git a/backends/xsmm/ceed-xsmm-blocked.c b/backends/xsmm/ceed-xsmm-blocked.c index 90dc19e741..1bd5d724f1 100644 --- a/backends/xsmm/ceed-xsmm-blocked.c +++ b/backends/xsmm/ceed-xsmm-blocked.c @@ -25,6 +25,7 @@ static int CeedInit_Xsmm_Blocked(const char *resource, Ceed ceed) { // Create reference Ceed that implementation will be dispatched through unless overridden CeedCallBackend(CeedInit("/cpu/self/opt/blocked", &ceed_ref)); CeedCallBackend(CeedSetDelegate(ceed, ceed_ref)); + CeedCallBackend(CeedDestroy(&ceed_ref)); CeedCallBackend(CeedSetBackendFunction(ceed, "Ceed", ceed, "TensorContractCreate", CeedTensorContractCreate_Xsmm)); return CEED_ERROR_SUCCESS; diff --git a/backends/xsmm/ceed-xsmm-serial.c b/backends/xsmm/ceed-xsmm-serial.c index 68e51a63e3..69d51b769f 100644 --- a/backends/xsmm/ceed-xsmm-serial.c +++ b/backends/xsmm/ceed-xsmm-serial.c @@ -25,6 +25,7 @@ static int CeedInit_Xsmm_Serial(const char *resource, Ceed ceed) { // Create reference Ceed that implementation will be dispatched through unless overridden CeedCallBackend(CeedInit("/cpu/self/opt/serial", &ceed_ref)); CeedCallBackend(CeedSetDelegate(ceed, ceed_ref)); + CeedCallBackend(CeedDestroy(&ceed_ref)); CeedCallBackend(CeedSetBackendFunction(ceed, "Ceed", ceed, "TensorContractCreate", CeedTensorContractCreate_Xsmm)); return CEED_ERROR_SUCCESS; diff --git a/backends/xsmm/ceed-xsmm-tensor.c b/backends/xsmm/ceed-xsmm-tensor.c index 1dc69b30a8..899726ef09 100644 --- a/backends/xsmm/ceed-xsmm-tensor.c +++ b/backends/xsmm/ceed-xsmm-tensor.c @@ -16,10 +16,6 @@ //------------------------------------------------------------------------------ static int CeedTensorContractApply_Xsmm(CeedTensorContract contract, CeedInt A, CeedInt B, CeedInt C, CeedInt J, const CeedScalar *restrict t, CeedTransposeMode t_mode, const CeedInt add, const CeedScalar *restrict u, CeedScalar *restrict v) { - Ceed ceed; - - CeedCallBackend(CeedTensorContractGetCeed(contract, &ceed)); - if (C == 1) { // Build or query the required kernel const int flags_t = LIBXSMM_GEMM_FLAGS(!t_mode ? 'T' : 'N', 'N'); @@ -33,7 +29,7 @@ static int CeedTensorContractApply_Xsmm(CeedTensorContract contract, CeedInt A, const libxsmm_gemmfunction kernel = libxsmm_dispatch_gemm(gemm_shape, (libxsmm_bitfield)(flags), (libxsmm_bitfield)LIBXSMM_GEMM_PREFETCH_NONE); libxsmm_gemm_param gemm_param; - CeedCheck(kernel, ceed, CEED_ERROR_BACKEND, "LIBXSMM kernel failed to build."); + CeedCheck(kernel, CeedTensorContractReturnCeed(contract), CEED_ERROR_BACKEND, "LIBXSMM kernel failed to build."); // Run kernel gemm_param.a.primary = (CeedScalar *)&t[0]; @@ -53,7 +49,7 @@ static int CeedTensorContractApply_Xsmm(CeedTensorContract contract, CeedInt A, const libxsmm_gemmfunction kernel = libxsmm_dispatch_gemm(gemm_shape, (libxsmm_bitfield)(flags), (libxsmm_bitfield)LIBXSMM_GEMM_PREFETCH_NONE); libxsmm_gemm_param gemm_param; - CeedCheck(kernel, ceed, CEED_ERROR_BACKEND, "LIBXSMM kernel failed to build."); + CeedCheck(kernel, CeedTensorContractReturnCeed(contract), CEED_ERROR_BACKEND, "LIBXSMM kernel failed to build."); // Run kernel gemm_param.b.primary = (CeedScalar *)&t[0]; diff --git a/examples/fluids/include/petsc-ceed-utils.h b/examples/fluids/include/petsc-ceed-utils.h index 936f278ee3..d085ad670f 100644 --- a/examples/fluids/include/petsc-ceed-utils.h +++ b/examples/fluids/include/petsc-ceed-utils.h @@ -220,6 +220,7 @@ static inline PetscErrorCode VecCopyPetscToCeed(Vec X_petsc, CeedVector x_ceed) PetscCall(VecGetArrayReadAndMemType(X_petsc, (const PetscScalar **)&x, &mem_type)); PetscCallCeed(ceed, CeedVectorSetArray(x_ceed, MemTypePetscToCeed(mem_type), CEED_COPY_VALUES, x)); PetscCall(VecRestoreArrayReadAndMemType(X_petsc, (const PetscScalar **)&x)); + PetscCheck(CeedDestroy(&ceed) == CEED_ERROR_SUCCESS, PetscObjectComm((PetscObject)X_petsc), PETSC_ERR_LIB, "Destroying Ceed object failed"); PetscFunctionReturn(PETSC_SUCCESS); } diff --git a/examples/fluids/src/mat-ceed.c b/examples/fluids/src/mat-ceed.c index cd164598ed..358160f678 100644 --- a/examples/fluids/src/mat-ceed.c +++ b/examples/fluids/src/mat-ceed.c @@ -1229,7 +1229,6 @@ PetscErrorCode MatCeedContextCreate(DM dm_x, DM dm_y, Vec X_loc, Vec Y_loc_trans // libCEED objects PetscCheck(CeedOperatorGetCeed(op_mult, &(*ctx)->ceed) == CEED_ERROR_SUCCESS, PETSC_COMM_SELF, PETSC_ERR_LIB, "retrieving Ceed context object failed"); - PetscCallCeed((*ctx)->ceed, CeedReference((*ctx)->ceed)); PetscCallCeed((*ctx)->ceed, CeedOperatorGetActiveVectorLengths(op_mult, &x_loc_len, &y_loc_len)); PetscCallCeed((*ctx)->ceed, CeedOperatorReferenceCopy(op_mult, &(*ctx)->op_mult)); if (op_mult_transpose) PetscCallCeed((*ctx)->ceed, CeedOperatorReferenceCopy(op_mult_transpose, &(*ctx)->op_mult_transpose)); diff --git a/examples/fluids/src/petsc_ops.c b/examples/fluids/src/petsc_ops.c index f40e156af2..b9b51209a3 100644 --- a/examples/fluids/src/petsc_ops.c +++ b/examples/fluids/src/petsc_ops.c @@ -197,6 +197,7 @@ PetscErrorCode CeedOperatorCreateLocalVecs(CeedOperator op, VecType vec_type, MP PetscCall(VecSetType(*output, vec_type)); PetscCall(VecSetSizes(*output, output_size, output_size)); } + PetscCheck(CeedDestroy(&ceed) == CEED_ERROR_SUCCESS, comm, PETSC_ERR_LIB, "Destroying Ceed object failed"); PetscFunctionReturn(PETSC_SUCCESS); } diff --git a/examples/fluids/src/turb_spanstats.c b/examples/fluids/src/turb_spanstats.c index 08f9ef36b9..db0532fdce 100644 --- a/examples/fluids/src/turb_spanstats.c +++ b/examples/fluids/src/turb_spanstats.c @@ -238,6 +238,8 @@ PetscErrorCode SpanStatsSetupDataDestroy(SpanStatsSetupData data) { PetscCallCeed(ceed, CeedVectorDestroy(&data->x_coord)); + PetscCheck(CeedDestroy(&ceed) == CEED_ERROR_SUCCESS, PETSC_COMM_WORLD, PETSC_ERR_LIB, "Destroying Ceed object failed"); + PetscCall(PetscFree(data)); PetscFunctionReturn(PETSC_SUCCESS); } diff --git a/interface/ceed-basis.c b/interface/ceed-basis.c index c6869f2f3b..902207f75e 100644 --- a/interface/ceed-basis.c +++ b/interface/ceed-basis.c @@ -194,16 +194,13 @@ static int CeedScalarView(const char *name, const char *fp_fmt, CeedInt m, CeedI @ref Developer **/ static int CeedBasisCreateProjectionMatrices(CeedBasis basis_from, CeedBasis basis_to, CeedScalar **interp_project, CeedScalar **grad_project) { - Ceed ceed; bool are_both_tensor; CeedInt Q, Q_to, Q_from, P_to, P_from; - CeedCall(CeedBasisGetCeed(basis_to, &ceed)); - // Check for compatible quadrature spaces CeedCall(CeedBasisGetNumQuadraturePoints(basis_to, &Q_to)); CeedCall(CeedBasisGetNumQuadraturePoints(basis_from, &Q_from)); - CeedCheck(Q_to == Q_from, ceed, CEED_ERROR_DIMENSION, + CeedCheck(Q_to == Q_from, CeedBasisReturnCeed(basis_to), CEED_ERROR_DIMENSION, "Bases must have compatible quadrature spaces." " 'basis_from' has %" CeedInt_FMT " points and 'basis_to' has %" CeedInt_FMT, Q_from, Q_to); @@ -231,7 +228,7 @@ static int CeedBasisCreateProjectionMatrices(CeedBasis basis_from, CeedBasis bas CeedCall(CeedBasisGetFESpace(basis_to, &fe_space_to)); CeedCall(CeedBasisGetFESpace(basis_from, &fe_space_from)); - CeedCheck(fe_space_to == fe_space_from, ceed, CEED_ERROR_MINOR, + CeedCheck(fe_space_to == fe_space_from, CeedBasisReturnCeed(basis_to), CEED_ERROR_MINOR, "Bases must both be the same FE space type." " 'basis_from' is a %s and 'basis_to' is a %s", CeedFESpaces[fe_space_from], CeedFESpaces[fe_space_to]); @@ -267,7 +264,7 @@ static int CeedBasisCreateProjectionMatrices(CeedBasis basis_from, CeedBasis bas // Compute interp_to^+, pseudoinverse of interp_to CeedCall(CeedCalloc(Q * q_comp * P_to, &interp_to_inv)); - CeedCall(CeedMatrixPseudoinverse(ceed, interp_to_source, Q * q_comp, P_to, interp_to_inv)); + CeedCall(CeedMatrixPseudoinverse(CeedBasisReturnCeed(basis_to), interp_to_source, Q * q_comp, P_to, interp_to_inv)); // Build matrices CeedInt num_matrices = 1 + (fe_space_to == CEED_FE_SPACE_H1) * (are_both_tensor ? 1 : dim); CeedScalar *input_from[num_matrices], *output_project[num_matrices]; @@ -281,7 +278,7 @@ static int CeedBasisCreateProjectionMatrices(CeedBasis basis_from, CeedBasis bas for (CeedInt m = 0; m < num_matrices; m++) { // output_project = interp_to^+ * interp_from memcpy(interp_from, input_from[m], Q * P_from * q_comp * sizeof(input_from[m][0])); - CeedCall(CeedMatrixMatrixMultiply(ceed, interp_to_inv, input_from[m], output_project[m], P_to, P_from, Q * q_comp)); + CeedCall(CeedMatrixMatrixMultiply(CeedBasisReturnCeed(basis_to), interp_to_inv, input_from[m], output_project[m], P_to, P_from, Q * q_comp)); // Round zero to machine precision for (CeedInt i = 0; i < P_to * P_from; i++) { if (fabs(output_project[m][i]) < 10 * CEED_EPSILON) output_project[m][i] = 0.0; @@ -318,9 +315,7 @@ static int CeedBasisApplyAtPointsCheckDims(CeedBasis basis, CeedInt num_elem, co CeedEvalMode eval_mode, CeedVector x_ref, CeedVector u, CeedVector v) { CeedInt dim, num_comp, num_q_comp, num_nodes, P_1d = 1, Q_1d = 1, total_num_points = 0; CeedSize x_length = 0, u_length = 0, v_length; - Ceed ceed; - CeedCall(CeedBasisGetCeed(basis, &ceed)); CeedCall(CeedBasisGetDimension(basis, &dim)); CeedCall(CeedBasisGetNumNodes1D(basis, &P_1d)); CeedCall(CeedBasisGetNumQuadraturePoints1D(basis, &Q_1d)); @@ -333,13 +328,14 @@ static int CeedBasisApplyAtPointsCheckDims(CeedBasis basis, CeedInt num_elem, co // Check compatibility coordinates vector for (CeedInt i = 0; i < num_elem; i++) total_num_points += num_points[i]; - CeedCheck((x_length >= (CeedSize)total_num_points * (CeedSize)dim) || (eval_mode == CEED_EVAL_WEIGHT), ceed, CEED_ERROR_DIMENSION, + CeedCheck((x_length >= (CeedSize)total_num_points * (CeedSize)dim) || (eval_mode == CEED_EVAL_WEIGHT), CeedBasisReturnCeed(basis), + CEED_ERROR_DIMENSION, "Length of reference coordinate vector incompatible with basis dimension and number of points." " Found reference coordinate vector of length %" CeedSize_FMT ", not of length %" CeedSize_FMT ".", x_length, (CeedSize)total_num_points * (CeedSize)dim); // Check CEED_EVAL_WEIGHT only on CEED_NOTRANSPOSE - CeedCheck(eval_mode != CEED_EVAL_WEIGHT || t_mode == CEED_NOTRANSPOSE, ceed, CEED_ERROR_UNSUPPORTED, + CeedCheck(eval_mode != CEED_EVAL_WEIGHT || t_mode == CEED_NOTRANSPOSE, CeedBasisReturnCeed(basis), CEED_ERROR_UNSUPPORTED, "CEED_EVAL_WEIGHT only supported with CEED_NOTRANSPOSE"); // Check vector lengths to prevent out of bounds issues @@ -364,10 +360,11 @@ static int CeedBasisApplyAtPointsCheckDims(CeedBasis basis, CeedInt num_elem, co case CEED_EVAL_NONE: case CEED_EVAL_DIV: case CEED_EVAL_CURL: - return CeedError(ceed, CEED_ERROR_UNSUPPORTED, "Evaluation at arbitrary points not supported for %s", CeedEvalModes[eval_mode]); + return CeedError(CeedBasisReturnCeed(basis), CEED_ERROR_UNSUPPORTED, "Evaluation at arbitrary points not supported for %s", + CeedEvalModes[eval_mode]); // LCOV_EXCL_STOP } - CeedCheck(has_good_dims, ceed, CEED_ERROR_DIMENSION, "Input/output vectors too short for basis and evaluation mode"); + CeedCheck(has_good_dims, CeedBasisReturnCeed(basis), CEED_ERROR_DIMENSION, "Input/output vectors too short for basis and evaluation mode"); return CEED_ERROR_SUCCESS; } @@ -395,12 +392,10 @@ static int CeedBasisApplyAtPointsCheckDims(CeedBasis basis, CeedInt num_elem, co static int CeedBasisApplyAtPoints_Core(CeedBasis basis, bool apply_add, CeedInt num_elem, const CeedInt *num_points, CeedTransposeMode t_mode, CeedEvalMode eval_mode, CeedVector x_ref, CeedVector u, CeedVector v) { CeedInt dim, num_comp, P_1d = 1, Q_1d = 1, total_num_points = num_points[0]; - Ceed ceed; - CeedCall(CeedBasisGetCeed(basis, &ceed)); CeedCall(CeedBasisGetDimension(basis, &dim)); // Inserting check because clang-tidy doesn't understand this cannot occur - CeedCheck(dim > 0, ceed, CEED_ERROR_UNSUPPORTED, "Malformed CeedBasis, dim > 0 is required"); + CeedCheck(dim > 0, CeedBasisReturnCeed(basis), CEED_ERROR_UNSUPPORTED, "Malformed CeedBasis, dim > 0 is required"); CeedCall(CeedBasisGetNumNodes1D(basis, &P_1d)); CeedCall(CeedBasisGetNumQuadraturePoints1D(basis, &Q_1d)); CeedCall(CeedBasisGetNumComponents(basis, &num_comp)); @@ -410,9 +405,11 @@ static int CeedBasisApplyAtPoints_Core(CeedBasis basis, bool apply_add, CeedInt bool is_tensor_basis; CeedCall(CeedBasisIsTensor(basis, &is_tensor_basis)); - CeedCheck(is_tensor_basis, ceed, CEED_ERROR_UNSUPPORTED, "Evaluation at arbitrary points only supported for tensor product bases"); + CeedCheck(is_tensor_basis, CeedBasisReturnCeed(basis), CEED_ERROR_UNSUPPORTED, + "Evaluation at arbitrary points only supported for tensor product bases"); } - CeedCheck(num_elem == 1, ceed, CEED_ERROR_UNSUPPORTED, "Evaluation at arbitrary points only supported for a single element at a time"); + CeedCheck(num_elem == 1, CeedBasisReturnCeed(basis), CEED_ERROR_UNSUPPORTED, + "Evaluation at arbitrary points only supported for a single element at a time"); if (eval_mode == CEED_EVAL_WEIGHT) { CeedCall(CeedVectorSetValue(v, 1.0)); return CEED_ERROR_SUCCESS; @@ -421,6 +418,7 @@ static int CeedBasisApplyAtPoints_Core(CeedBasis basis, bool apply_add, CeedInt // Build basis mapping from nodes to Chebyshev coefficients CeedScalar *chebyshev_interp_1d, *chebyshev_grad_1d, *chebyshev_q_weight_1d; const CeedScalar *q_ref_1d; + Ceed ceed; CeedCall(CeedCalloc(P_1d * Q_1d, &chebyshev_interp_1d)); CeedCall(CeedCalloc(P_1d * Q_1d, &chebyshev_grad_1d)); @@ -428,6 +426,7 @@ static int CeedBasisApplyAtPoints_Core(CeedBasis basis, bool apply_add, CeedInt CeedCall(CeedBasisGetQRef(basis, &q_ref_1d)); CeedCall(CeedBasisGetChebyshevInterp1D(basis, chebyshev_interp_1d)); + CeedCall(CeedBasisGetCeed(basis, &ceed)); CeedCall(CeedVectorCreate(ceed, num_comp * CeedIntPow(Q_1d, dim), &basis->vec_chebyshev)); CeedCall(CeedBasisCreateTensorH1(ceed, dim, num_comp, P_1d, Q_1d, chebyshev_interp_1d, chebyshev_grad_1d, q_ref_1d, chebyshev_q_weight_1d, &basis->basis_chebyshev)); @@ -436,6 +435,7 @@ static int CeedBasisApplyAtPoints_Core(CeedBasis basis, bool apply_add, CeedInt CeedCall(CeedFree(&chebyshev_interp_1d)); CeedCall(CeedFree(&chebyshev_grad_1d)); CeedCall(CeedFree(&chebyshev_q_weight_1d)); + CeedCall(CeedDestroy(&ceed)); } // Create TensorContract object if needed, such as a basis from the GPU backends @@ -447,7 +447,8 @@ static int CeedBasisApplyAtPoints_Core(CeedBasis basis, bool apply_add, CeedInt // Only need matching tensor contraction dimensions, any type of basis will work CeedCall(CeedBasisCreateTensorH1Lagrange(ceed_ref, dim, num_comp, P_1d, Q_1d, CEED_GAUSS, &basis_ref)); // Note - clang-tidy doesn't know basis_ref->contract must be valid here - CeedCheck(basis_ref && basis_ref->contract, ceed, CEED_ERROR_UNSUPPORTED, "Reference CPU ceed failed to create a tensor contraction object"); + CeedCheck(basis_ref && basis_ref->contract, CeedBasisReturnCeed(basis), CEED_ERROR_UNSUPPORTED, + "Reference CPU ceed failed to create a tensor contraction object"); CeedCall(CeedTensorContractReferenceCopy(basis_ref->contract, &basis->contract)); CeedCall(CeedBasisDestroy(&basis_ref)); CeedCall(CeedDestroy(&ceed_ref)); @@ -628,6 +629,7 @@ int CeedBasisGetCollocatedGrad(CeedBasis basis, CeedScalar *collo_grad_1d) { CeedCall(CeedMatrixMatrixMultiply(ceed, grad_1d, (const CeedScalar *)interp_1d_pinv, collo_grad_1d, Q_1d, Q_1d, P_1d)); CeedCall(CeedFree(&interp_1d_pinv)); + CeedCall(CeedDestroy(&ceed)); return CEED_ERROR_SUCCESS; } @@ -669,6 +671,7 @@ int CeedBasisGetChebyshevInterp1D(CeedBasis basis, CeedScalar *chebyshev_interp_ // Cleanup CeedCall(CeedFree(&C)); CeedCall(CeedFree(&chebyshev_coeffs_1d_inv)); + CeedCall(CeedDestroy(&ceed)); return CEED_ERROR_SUCCESS; } @@ -1328,6 +1331,7 @@ int CeedBasisCreateTensorH1(Ceed ceed, CeedInt dim, CeedInt num_comp, CeedInt P_ CeedCall(CeedGetObjectDelegate(ceed, &delegate, "Basis")); CeedCheck(delegate, ceed, CEED_ERROR_UNSUPPORTED, "Backend does not implement BasisCreateTensorH1"); CeedCall(CeedBasisCreateTensorH1(delegate, dim, num_comp, P_1d, Q_1d, interp_1d, grad_1d, q_ref_1d, q_weight_1d, basis)); + CeedCall(CeedDestroy(&delegate)); return CEED_ERROR_SUCCESS; } @@ -1467,6 +1471,7 @@ int CeedBasisCreateH1(Ceed ceed, CeedElemTopology topo, CeedInt num_comp, CeedIn CeedCall(CeedGetObjectDelegate(ceed, &delegate, "Basis")); CeedCheck(delegate, ceed, CEED_ERROR_UNSUPPORTED, "Backend does not implement BasisCreateH1"); CeedCall(CeedBasisCreateH1(delegate, topo, num_comp, num_nodes, num_qpts, interp, grad, q_ref, q_weight, basis)); + CeedCall(CeedDestroy(&delegate)); return CEED_ERROR_SUCCESS; } @@ -1526,6 +1531,7 @@ int CeedBasisCreateHdiv(Ceed ceed, CeedElemTopology topo, CeedInt num_comp, Ceed CeedCall(CeedGetObjectDelegate(ceed, &delegate, "Basis")); CeedCheck(delegate, ceed, CEED_ERROR_UNSUPPORTED, "Backend does not implement BasisCreateHdiv"); CeedCall(CeedBasisCreateHdiv(delegate, topo, num_comp, num_nodes, num_qpts, interp, div, q_ref, q_weight, basis)); + CeedCall(CeedDestroy(&delegate)); return CEED_ERROR_SUCCESS; } @@ -1585,6 +1591,7 @@ int CeedBasisCreateHcurl(Ceed ceed, CeedElemTopology topo, CeedInt num_comp, Cee CeedCall(CeedGetObjectDelegate(ceed, &delegate, "Basis")); CeedCheck(delegate, ceed, CEED_ERROR_UNSUPPORTED, "Backend does not implement BasisCreateHcurl"); CeedCall(CeedBasisCreateHcurl(delegate, topo, num_comp, num_nodes, num_qpts, interp, curl, q_ref, q_weight, basis)); + CeedCall(CeedDestroy(&delegate)); return CEED_ERROR_SUCCESS; } @@ -1681,6 +1688,7 @@ int CeedBasisCreateProjection(CeedBasis basis_from, CeedBasis basis_to, CeedBasi // Cleanup CeedCall(CeedFree(&interp_project)); CeedCall(CeedFree(&grad_project)); + CeedCall(CeedDestroy(&ceed)); return CEED_ERROR_SUCCESS; } @@ -1806,9 +1814,7 @@ int CeedBasisView(CeedBasis basis, FILE *stream) { static int CeedBasisApplyCheckDims(CeedBasis basis, CeedInt num_elem, CeedTransposeMode t_mode, CeedEvalMode eval_mode, CeedVector u, CeedVector v) { CeedInt dim, num_comp, q_comp, num_nodes, num_qpts; CeedSize u_length = 0, v_length; - Ceed ceed; - CeedCall(CeedBasisGetCeed(basis, &ceed)); CeedCall(CeedBasisGetDimension(basis, &dim)); CeedCall(CeedBasisGetNumComponents(basis, &num_comp)); CeedCall(CeedBasisGetNumQuadratureComponents(basis, eval_mode, &q_comp)); @@ -1834,7 +1840,7 @@ static int CeedBasisApplyCheckDims(CeedBasis basis, CeedInt num_elem, CeedTransp has_good_dims = v_length >= (CeedSize)num_elem * (CeedSize)num_qpts; break; } - CeedCheck(has_good_dims, ceed, CEED_ERROR_DIMENSION, "Input/output vectors too short for basis and evaluation mode"); + CeedCheck(has_good_dims, CeedBasisReturnCeed(basis), CEED_ERROR_DIMENSION, "Input/output vectors too short for basis and evaluation mode"); return CEED_ERROR_SUCCESS; } @@ -1969,7 +1975,8 @@ int CeedBasisApplyAddAtPoints(CeedBasis basis, CeedInt num_elem, const CeedInt * @ref Advanced **/ int CeedBasisGetCeed(CeedBasis basis, Ceed *ceed) { - *ceed = CeedBasisReturnCeed(basis); + *ceed = NULL; + CeedCall(CeedReferenceCopy(CeedBasisReturnCeed(basis), ceed)); return CEED_ERROR_SUCCESS; } diff --git a/interface/ceed-cuda.c b/interface/ceed-cuda.c index c4463b738d..ff28d10d14 100644 --- a/interface/ceed-cuda.c +++ b/interface/ceed-cuda.c @@ -23,10 +23,7 @@ **/ int CeedQFunctionSetCUDAUserFunction(CeedQFunction qf, CUfunction f) { if (!qf->SetCUDAUserFunction) { - Ceed ceed; - - CeedCall(CeedQFunctionGetCeed(qf, &ceed)); - CeedDebug(ceed, "Backend does not support CUfunction pointers for QFunctions."); + CeedDebug(CeedQFunctionReturnCeed(qf), "Backend does not support CUfunction pointers for QFunctions."); } else { CeedCall(qf->SetCUDAUserFunction(qf, f)); } diff --git a/interface/ceed-elemrestriction.c b/interface/ceed-elemrestriction.c index 7d832f1a97..50028e6934 100644 --- a/interface/ceed-elemrestriction.c +++ b/interface/ceed-elemrestriction.c @@ -164,13 +164,12 @@ int CeedElemRestrictionIsAtPoints(CeedElemRestriction rstr, bool *is_points) { **/ int CeedElemRestrictionAtPointsAreCompatible(CeedElemRestriction rstr_a, CeedElemRestriction rstr_b, bool *are_compatible) { CeedInt num_elem_a, num_elem_b, num_points_a, num_points_b; - Ceed ceed; - - CeedCall(CeedElemRestrictionGetCeed(rstr_a, &ceed)); // Cannot compare non-points restrictions - CeedCheck(rstr_a->rstr_type == CEED_RESTRICTION_POINTS, ceed, CEED_ERROR_UNSUPPORTED, "First CeedElemRestriction must be AtPoints"); - CeedCheck(rstr_b->rstr_type == CEED_RESTRICTION_POINTS, ceed, CEED_ERROR_UNSUPPORTED, "Second CeedElemRestriction must be AtPoints"); + CeedCheck(rstr_a->rstr_type == CEED_RESTRICTION_POINTS, CeedElemRestrictionReturnCeed(rstr_a), CEED_ERROR_UNSUPPORTED, + "First CeedElemRestriction must be AtPoints"); + CeedCheck(rstr_b->rstr_type == CEED_RESTRICTION_POINTS, CeedElemRestrictionReturnCeed(rstr_a), CEED_ERROR_UNSUPPORTED, + "Second CeedElemRestriction must be AtPoints"); CeedCall(CeedElemRestrictionGetNumElements(rstr_a, &num_elem_a)); CeedCall(CeedElemRestrictionGetNumElements(rstr_b, &num_elem_b)); @@ -357,14 +356,13 @@ int CeedElemRestrictionRestoreCurlOrientations(CeedElemRestriction rstr, const C int CeedElemRestrictionGetLLayout(CeedElemRestriction rstr, CeedInt layout[3]) { bool has_backend_strides; CeedRestrictionType rstr_type; - Ceed ceed; - CeedCall(CeedElemRestrictionGetCeed(rstr, &ceed)); CeedCall(CeedElemRestrictionGetType(rstr, &rstr_type)); - CeedCheck(rstr_type == CEED_RESTRICTION_STRIDED, ceed, CEED_ERROR_MINOR, "Only strided CeedElemRestriction have strided L-vector layout"); + CeedCheck(rstr_type == CEED_RESTRICTION_STRIDED, CeedElemRestrictionReturnCeed(rstr), CEED_ERROR_MINOR, + "Only strided CeedElemRestriction have strided L-vector layout"); CeedCall(CeedElemRestrictionHasBackendStrides(rstr, &has_backend_strides)); if (has_backend_strides) { - CeedCheck(rstr->l_layout[0], ceed, CEED_ERROR_MINOR, "CeedElemRestriction has no L-vector layout data"); + CeedCheck(rstr->l_layout[0], CeedElemRestrictionReturnCeed(rstr), CEED_ERROR_MINOR, "CeedElemRestriction has no L-vector layout data"); for (CeedInt i = 0; i < 3; i++) layout[i] = rstr->l_layout[i]; } else { CeedCall(CeedElemRestrictionGetStrides(rstr, layout)); @@ -481,12 +479,11 @@ int CeedElemRestrictionGetAtPointsElementOffset(CeedElemRestriction rstr, CeedIn **/ int CeedElemRestrictionSetAtPointsEVectorSize(CeedElemRestriction rstr, CeedSize e_size) { CeedRestrictionType rstr_type; - Ceed ceed; - CeedCall(CeedElemRestrictionGetCeed(rstr, &ceed)); CeedCall(CeedElemRestrictionGetType(rstr, &rstr_type)); - CeedCheck(rstr_type == CEED_RESTRICTION_POINTS, ceed, CEED_ERROR_INCOMPATIBLE, "Can only compute offset for a points CeedElemRestriction"); - CeedCheck(e_size >= rstr->e_size, ceed, CEED_ERROR_INCOMPATIBLE, + CeedCheck(rstr_type == CEED_RESTRICTION_POINTS, CeedElemRestrictionReturnCeed(rstr), CEED_ERROR_INCOMPATIBLE, + "Can only compute offset for a points CeedElemRestriction"); + CeedCheck(e_size >= rstr->e_size, CeedElemRestrictionReturnCeed(rstr), CEED_ERROR_INCOMPATIBLE, "Can only increase the size of the E-vector for the CeedElemRestriction." " Current size: %" CeedSize_FMT " New size: %" CeedSize_FMT, rstr->e_size, e_size); @@ -636,6 +633,7 @@ int CeedElemRestrictionCreate(Ceed ceed, CeedInt num_elem, CeedInt elem_size, Ce CeedCall(CeedGetObjectDelegate(ceed, &delegate, "ElemRestriction")); CeedCheck(delegate, ceed, CEED_ERROR_UNSUPPORTED, "Backend does not implement CeedElemRestrictionCreate"); CeedCall(CeedElemRestrictionCreate(delegate, num_elem, elem_size, num_comp, comp_stride, l_size, mem_type, copy_mode, offsets, rstr)); + CeedCall(CeedDestroy(&delegate)); return CEED_ERROR_SUCCESS; } @@ -693,6 +691,7 @@ int CeedElemRestrictionCreateOriented(Ceed ceed, CeedInt num_elem, CeedInt elem_ CeedCheck(delegate, ceed, CEED_ERROR_UNSUPPORTED, "Backend does not implement CeedElemRestrictionCreateOriented"); CeedCall( CeedElemRestrictionCreateOriented(delegate, num_elem, elem_size, num_comp, comp_stride, l_size, mem_type, copy_mode, offsets, orients, rstr)); + CeedCall(CeedDestroy(&delegate)); return CEED_ERROR_SUCCESS; } @@ -751,6 +750,7 @@ int CeedElemRestrictionCreateCurlOriented(Ceed ceed, CeedInt num_elem, CeedInt e CeedCheck(delegate, ceed, CEED_ERROR_UNSUPPORTED, "Backend does not implement CeedElemRestrictionCreateCurlOriented"); CeedCall(CeedElemRestrictionCreateCurlOriented(delegate, num_elem, elem_size, num_comp, comp_stride, l_size, mem_type, copy_mode, offsets, curl_orients, rstr)); + CeedCall(CeedDestroy(&delegate)); return CEED_ERROR_SUCCESS; } @@ -803,6 +803,7 @@ int CeedElemRestrictionCreateStrided(Ceed ceed, CeedInt num_elem, CeedInt elem_s CeedCall(CeedGetObjectDelegate(ceed, &delegate, "ElemRestriction")); CeedCheck(delegate, ceed, CEED_ERROR_UNSUPPORTED, "Backend does not implement CeedElemRestrictionCreateStrided"); CeedCall(CeedElemRestrictionCreateStrided(delegate, num_elem, elem_size, num_comp, l_size, strides, rstr)); + CeedCall(CeedDestroy(&delegate)); return CEED_ERROR_SUCCESS; } @@ -870,6 +871,7 @@ int CeedElemRestrictionCreateAtPoints(Ceed ceed, CeedInt num_elem, CeedInt num_p CeedCall(CeedGetObjectDelegate(ceed, &delegate, "ElemRestriction")); CeedCheck(delegate, ceed, CEED_ERROR_UNSUPPORTED, "Backend does not implement CeedElemRestrictionCreateAtPoints"); CeedCall(CeedElemRestrictionCreateAtPoints(delegate, num_elem, num_points, num_comp, l_size, mem_type, copy_mode, offsets, rstr)); + CeedCall(CeedDestroy(&delegate)); return CEED_ERROR_SUCCESS; } @@ -933,6 +935,7 @@ int CeedElemRestrictionCreateBlocked(Ceed ceed, CeedInt num_elem, CeedInt elem_s CeedCheck(delegate, ceed, CEED_ERROR_UNSUPPORTED, "Backend does not implement CeedElemRestrictionCreateBlocked"); CeedCall(CeedElemRestrictionCreateBlocked(delegate, num_elem, elem_size, block_size, num_comp, comp_stride, l_size, mem_type, copy_mode, offsets, rstr)); + CeedCall(CeedDestroy(&delegate)); return CEED_ERROR_SUCCESS; } @@ -1002,6 +1005,7 @@ int CeedElemRestrictionCreateBlockedOriented(Ceed ceed, CeedInt num_elem, CeedIn CeedCheck(delegate, ceed, CEED_ERROR_UNSUPPORTED, "Backend does not implement CeedElemRestrictionCreateBlockedOriented"); CeedCall(CeedElemRestrictionCreateBlockedOriented(delegate, num_elem, elem_size, block_size, num_comp, comp_stride, l_size, mem_type, copy_mode, offsets, orients, rstr)); + CeedCall(CeedDestroy(&delegate)); return CEED_ERROR_SUCCESS; } @@ -1074,6 +1078,7 @@ int CeedElemRestrictionCreateBlockedCurlOriented(Ceed ceed, CeedInt num_elem, Ce CeedCheck(delegate, ceed, CEED_ERROR_UNSUPPORTED, "Backend does not implement CeedElemRestrictionCreateBlockedCurlOriented"); CeedCall(CeedElemRestrictionCreateBlockedCurlOriented(delegate, num_elem, elem_size, block_size, num_comp, comp_stride, l_size, mem_type, copy_mode, offsets, curl_orients, rstr)); + CeedCall(CeedDestroy(&delegate)); return CEED_ERROR_SUCCESS; } @@ -1135,6 +1140,7 @@ int CeedElemRestrictionCreateBlockedStrided(Ceed ceed, CeedInt num_elem, CeedInt CeedCall(CeedGetObjectDelegate(ceed, &delegate, "ElemRestriction")); CeedCheck(delegate, ceed, CEED_ERROR_UNSUPPORTED, "Backend does not implement CeedElemRestrictionCreateBlockedStrided"); CeedCall(CeedElemRestrictionCreateBlockedStrided(delegate, num_elem, elem_size, block_size, num_comp, l_size, strides, rstr)); + CeedCall(CeedDestroy(&delegate)); return CEED_ERROR_SUCCESS; } @@ -1269,6 +1275,7 @@ int CeedElemRestrictionCreateVector(CeedElemRestriction rstr, CeedVector *l_vec, CeedCall(CeedElemRestrictionGetEVectorSize(rstr, &e_size)); if (l_vec) CeedCall(CeedVectorCreate(ceed, l_size, l_vec)); if (e_vec) CeedCall(CeedVectorCreate(ceed, e_size, e_vec)); + CeedCall(CeedDestroy(&ceed)); return CEED_ERROR_SUCCESS; } @@ -1289,9 +1296,7 @@ int CeedElemRestrictionCreateVector(CeedElemRestriction rstr, CeedVector *l_vec, int CeedElemRestrictionApply(CeedElemRestriction rstr, CeedTransposeMode t_mode, CeedVector u, CeedVector ru, CeedRequest *request) { CeedSize min_u_len, min_ru_len, len; CeedInt num_elem; - Ceed ceed; - CeedCall(CeedElemRestrictionGetCeed(rstr, &ceed)); if (t_mode == CEED_NOTRANSPOSE) { CeedCall(CeedElemRestrictionGetEVectorSize(rstr, &min_ru_len)); CeedCall(CeedElemRestrictionGetLVectorSize(rstr, &min_u_len)); @@ -1300,11 +1305,11 @@ int CeedElemRestrictionApply(CeedElemRestriction rstr, CeedTransposeMode t_mode, CeedCall(CeedElemRestrictionGetLVectorSize(rstr, &min_ru_len)); } CeedCall(CeedVectorGetLength(u, &len)); - CeedCheck(min_u_len <= len, ceed, CEED_ERROR_DIMENSION, + CeedCheck(min_u_len <= len, CeedElemRestrictionReturnCeed(rstr), CEED_ERROR_DIMENSION, "Input vector size %" CeedInt_FMT " not compatible with element restriction (%" CeedInt_FMT ", %" CeedInt_FMT ")", len, min_ru_len, min_u_len); CeedCall(CeedVectorGetLength(ru, &len)); - CeedCheck(min_ru_len <= len, ceed, CEED_ERROR_DIMENSION, + CeedCheck(min_ru_len <= len, CeedElemRestrictionReturnCeed(rstr), CEED_ERROR_DIMENSION, "Output vector size %" CeedInt_FMT " not compatible with element restriction (%" CeedInt_FMT ", %" CeedInt_FMT ")", len, min_u_len, min_ru_len); CeedCall(CeedElemRestrictionGetNumElements(rstr, &num_elem)); @@ -1331,10 +1336,9 @@ int CeedElemRestrictionApplyAtPointsInElement(CeedElemRestriction rstr, CeedInt CeedRequest *request) { CeedSize min_u_len, min_ru_len, len; CeedInt num_elem; - Ceed ceed; - CeedCall(CeedElemRestrictionGetCeed(rstr, &ceed)); - CeedCheck(rstr->ApplyAtPointsInElement, ceed, CEED_ERROR_UNSUPPORTED, "Backend does not implement CeedElemRestrictionApplyAtPointsInElement"); + CeedCheck(rstr->ApplyAtPointsInElement, CeedElemRestrictionReturnCeed(rstr), CEED_ERROR_UNSUPPORTED, + "Backend does not implement CeedElemRestrictionApplyAtPointsInElement"); if (t_mode == CEED_NOTRANSPOSE) { CeedInt num_points, num_comp; @@ -1352,17 +1356,17 @@ int CeedElemRestrictionApplyAtPointsInElement(CeedElemRestriction rstr, CeedInt CeedCall(CeedElemRestrictionGetLVectorSize(rstr, &min_ru_len)); } CeedCall(CeedVectorGetLength(u, &len)); - CeedCheck(min_u_len <= len, ceed, CEED_ERROR_DIMENSION, + CeedCheck(min_u_len <= len, CeedElemRestrictionReturnCeed(rstr), CEED_ERROR_DIMENSION, "Input vector size %" CeedInt_FMT " not compatible with element restriction (%" CeedInt_FMT ", %" CeedInt_FMT ") for element %" CeedInt_FMT, len, min_ru_len, min_u_len, elem); CeedCall(CeedVectorGetLength(ru, &len)); - CeedCheck(min_ru_len <= len, ceed, CEED_ERROR_DIMENSION, + CeedCheck(min_ru_len <= len, CeedElemRestrictionReturnCeed(rstr), CEED_ERROR_DIMENSION, "Output vector size %" CeedInt_FMT " not compatible with element restriction (%" CeedInt_FMT ", %" CeedInt_FMT ") for element %" CeedInt_FMT, len, min_ru_len, min_u_len, elem); CeedCall(CeedElemRestrictionGetNumElements(rstr, &num_elem)); - CeedCheck(elem < num_elem, ceed, CEED_ERROR_DIMENSION, + CeedCheck(elem < num_elem, CeedElemRestrictionReturnCeed(rstr), CEED_ERROR_DIMENSION, "Cannot retrieve element %" CeedInt_FMT ", element %" CeedInt_FMT " > total elements %" CeedInt_FMT "", elem, elem, num_elem); if (num_elem > 0) CeedCall(rstr->ApplyAtPointsInElement(rstr, elem, t_mode, u, ru, request)); return CEED_ERROR_SUCCESS; @@ -1387,10 +1391,9 @@ int CeedElemRestrictionApplyBlock(CeedElemRestriction rstr, CeedInt block, CeedT CeedRequest *request) { CeedSize min_u_len, min_ru_len, len; CeedInt block_size, num_elem; - Ceed ceed; - CeedCall(CeedElemRestrictionGetCeed(rstr, &ceed)); - CeedCheck(rstr->ApplyBlock, ceed, CEED_ERROR_UNSUPPORTED, "Backend does not implement CeedElemRestrictionApplyBlock"); + CeedCheck(rstr->ApplyBlock, CeedElemRestrictionReturnCeed(rstr), CEED_ERROR_UNSUPPORTED, + "Backend does not implement CeedElemRestrictionApplyBlock"); CeedCall(CeedElemRestrictionGetBlockSize(rstr, &block_size)); if (t_mode == CEED_NOTRANSPOSE) { @@ -1409,15 +1412,15 @@ int CeedElemRestrictionApplyBlock(CeedElemRestriction rstr, CeedInt block, CeedT min_u_len = (CeedSize)block_size * (CeedSize)elem_size * (CeedSize)num_comp; } CeedCall(CeedVectorGetLength(u, &len)); - CeedCheck(min_u_len == len, ceed, CEED_ERROR_DIMENSION, + CeedCheck(min_u_len == len, CeedElemRestrictionReturnCeed(rstr), CEED_ERROR_DIMENSION, "Input vector size %" CeedInt_FMT " not compatible with element restriction (%" CeedInt_FMT ", %" CeedInt_FMT ")", len, min_u_len, min_ru_len); CeedCall(CeedVectorGetLength(ru, &len)); - CeedCheck(min_ru_len == len, ceed, CEED_ERROR_DIMENSION, + CeedCheck(min_ru_len == len, CeedElemRestrictionReturnCeed(rstr), CEED_ERROR_DIMENSION, "Output vector size %" CeedInt_FMT " not compatible with element restriction (%" CeedInt_FMT ", %" CeedInt_FMT ")", len, min_ru_len, min_u_len); CeedCall(CeedElemRestrictionGetNumElements(rstr, &num_elem)); - CeedCheck(block_size * block <= num_elem, ceed, CEED_ERROR_DIMENSION, + CeedCheck(block_size * block <= num_elem, CeedElemRestrictionReturnCeed(rstr), CEED_ERROR_DIMENSION, "Cannot retrieve block %" CeedInt_FMT ", element %" CeedInt_FMT " > total elements %" CeedInt_FMT "", block, block_size * block, num_elem); CeedCall(rstr->ApplyBlock(rstr, block, t_mode, u, ru, request)); @@ -1435,7 +1438,8 @@ int CeedElemRestrictionApplyBlock(CeedElemRestriction rstr, CeedInt block, CeedT @ref Advanced **/ int CeedElemRestrictionGetCeed(CeedElemRestriction rstr, Ceed *ceed) { - *ceed = CeedElemRestrictionReturnCeed(rstr); + *ceed = NULL; + CeedCall(CeedReferenceCopy(CeedElemRestrictionReturnCeed(rstr), ceed)); return CEED_ERROR_SUCCESS; } diff --git a/interface/ceed-hip.c b/interface/ceed-hip.c index cc4a625853..f14df51eb5 100644 --- a/interface/ceed-hip.c +++ b/interface/ceed-hip.c @@ -23,10 +23,7 @@ **/ int CeedQFunctionSetHIPUserFunction(CeedQFunction qf, hipFunction_t f) { if (!qf->SetHIPUserFunction) { - Ceed ceed; - - CeedCall(CeedQFunctionGetCeed(qf, &ceed)); - CeedDebug(ceed, "Backend does not support hipFunction_t pointers for QFunctions."); + CeedDebug(CeedQFunctionReturnCeed(qf), "Backend does not support hipFunction_t pointers for QFunctions."); } else { CeedCall(qf->SetHIPUserFunction(qf, f)); } diff --git a/interface/ceed-operator.c b/interface/ceed-operator.c index 301d5584fa..0bdc136ca1 100644 --- a/interface/ceed-operator.c +++ b/interface/ceed-operator.c @@ -195,10 +195,8 @@ int CeedOperatorGetActiveBasis(CeedOperator op, CeedBasis *active_basis) { int CeedOperatorGetActiveBases(CeedOperator op, CeedBasis *active_input_basis, CeedBasis *active_output_basis) { bool is_composite; CeedInt num_input_fields, num_output_fields; - Ceed ceed; CeedOperatorField *op_input_fields, *op_output_fields; - CeedCall(CeedOperatorGetCeed(op, &ceed)); CeedCall(CeedOperatorIsComposite(op, &is_composite)); CeedCall(CeedOperatorGetFields(op, &num_input_fields, &op_input_fields, &num_output_fields, &op_output_fields)); @@ -213,13 +211,14 @@ int CeedOperatorGetActiveBases(CeedOperator op, CeedBasis *active_input_basis, C CeedBasis basis; CeedCall(CeedOperatorFieldGetBasis(op_input_fields[i], &basis)); - CeedCheck(!*active_input_basis || *active_input_basis == basis, ceed, CEED_ERROR_MINOR, "Multiple active input CeedBases found"); + CeedCheck(!*active_input_basis || *active_input_basis == basis, CeedOperatorReturnCeed(op), CEED_ERROR_MINOR, + "Multiple active input CeedBases found"); if (!*active_input_basis) CeedCall(CeedBasisReferenceCopy(basis, active_input_basis)); CeedCall(CeedBasisDestroy(&basis)); } CeedCall(CeedVectorDestroy(&vec)); } - CeedCheck(*active_input_basis, ceed, CEED_ERROR_INCOMPLETE, "No active input CeedBasis found"); + CeedCheck(*active_input_basis, CeedOperatorReturnCeed(op), CEED_ERROR_INCOMPLETE, "No active input CeedBasis found"); } } if (active_output_basis) { @@ -233,13 +232,14 @@ int CeedOperatorGetActiveBases(CeedOperator op, CeedBasis *active_input_basis, C CeedBasis basis; CeedCall(CeedOperatorFieldGetBasis(op_output_fields[i], &basis)); - CeedCheck(!*active_output_basis || *active_output_basis == basis, ceed, CEED_ERROR_MINOR, "Multiple active output CeedBases found"); + CeedCheck(!*active_output_basis || *active_output_basis == basis, CeedOperatorReturnCeed(op), CEED_ERROR_MINOR, + "Multiple active output CeedBases found"); if (!*active_output_basis) CeedCall(CeedBasisReferenceCopy(basis, active_output_basis)); CeedCall(CeedBasisDestroy(&basis)); } CeedCall(CeedVectorDestroy(&vec)); } - CeedCheck(*active_output_basis, ceed, CEED_ERROR_INCOMPLETE, "No active output CeedBasis found"); + CeedCheck(*active_output_basis, CeedOperatorReturnCeed(op), CEED_ERROR_INCOMPLETE, "No active output CeedBasis found"); } } return CEED_ERROR_SUCCESS; @@ -278,10 +278,8 @@ int CeedOperatorGetActiveElemRestriction(CeedOperator op, CeedElemRestriction *a int CeedOperatorGetActiveElemRestrictions(CeedOperator op, CeedElemRestriction *active_input_rstr, CeedElemRestriction *active_output_rstr) { bool is_composite; CeedInt num_input_fields, num_output_fields; - Ceed ceed; CeedOperatorField *op_input_fields, *op_output_fields; - CeedCall(CeedOperatorGetCeed(op, &ceed)); CeedCall(CeedOperatorIsComposite(op, &is_composite)); CeedCall(CeedOperatorGetFields(op, &num_input_fields, &op_input_fields, &num_output_fields, &op_output_fields)); @@ -296,13 +294,14 @@ int CeedOperatorGetActiveElemRestrictions(CeedOperator op, CeedElemRestriction * CeedElemRestriction rstr; CeedCall(CeedOperatorFieldGetElemRestriction(op_input_fields[i], &rstr)); - CeedCheck(!*active_input_rstr || *active_input_rstr == rstr, ceed, CEED_ERROR_MINOR, "Multiple active input CeedElemRestrictions found"); + CeedCheck(!*active_input_rstr || *active_input_rstr == rstr, CeedOperatorReturnCeed(op), CEED_ERROR_MINOR, + "Multiple active input CeedElemRestrictions found"); if (!*active_input_rstr) CeedCall(CeedElemRestrictionReferenceCopy(rstr, active_input_rstr)); CeedCall(CeedElemRestrictionDestroy(&rstr)); } CeedCall(CeedVectorDestroy(&vec)); } - CeedCheck(*active_input_rstr, ceed, CEED_ERROR_INCOMPLETE, "No active input CeedElemRestriction found"); + CeedCheck(*active_input_rstr, CeedOperatorReturnCeed(op), CEED_ERROR_INCOMPLETE, "No active input CeedElemRestriction found"); } } if (active_output_rstr) { @@ -316,13 +315,14 @@ int CeedOperatorGetActiveElemRestrictions(CeedOperator op, CeedElemRestriction * CeedElemRestriction rstr; CeedCall(CeedOperatorFieldGetElemRestriction(op_output_fields[i], &rstr)); - CeedCheck(!*active_output_rstr || *active_output_rstr == rstr, ceed, CEED_ERROR_MINOR, "Multiple active output CeedElemRestrictions found"); + CeedCheck(!*active_output_rstr || *active_output_rstr == rstr, CeedOperatorReturnCeed(op), CEED_ERROR_MINOR, + "Multiple active output CeedElemRestrictions found"); if (!*active_output_rstr) CeedCall(CeedElemRestrictionReferenceCopy(rstr, active_output_rstr)); CeedCall(CeedElemRestrictionDestroy(&rstr)); } CeedCall(CeedVectorDestroy(&vec)); } - CeedCheck(*active_output_rstr, ceed, CEED_ERROR_INCOMPLETE, "No active output CeedElemRestriction found"); + CeedCheck(*active_output_rstr, CeedOperatorReturnCeed(op), CEED_ERROR_INCOMPLETE, "No active output CeedElemRestriction found"); } } return CEED_ERROR_SUCCESS; @@ -345,10 +345,8 @@ int CeedOperatorGetActiveElemRestrictions(CeedOperator op, CeedElemRestriction * **/ static int CeedOperatorContextSetGeneric(CeedOperator op, CeedContextFieldLabel field_label, CeedContextFieldType field_type, void *values) { bool is_composite = false; - Ceed ceed; - CeedCall(CeedOperatorGetCeed(op, &ceed)); - CeedCheck(field_label, ceed, CEED_ERROR_UNSUPPORTED, "Invalid field label"); + CeedCheck(field_label, CeedOperatorReturnCeed(op), CEED_ERROR_UNSUPPORTED, "Invalid field label"); // Check if field_label and op correspond if (field_label->from_op) { @@ -357,7 +355,7 @@ static int CeedOperatorContextSetGeneric(CeedOperator op, CeedContextFieldLabel for (CeedInt i = 0; i < op->num_context_labels; i++) { if (op->context_labels[i] == field_label) index = i; } - CeedCheck(index != -1, ceed, CEED_ERROR_UNSUPPORTED, "ContextFieldLabel does not correspond to the operator"); + CeedCheck(index != -1, CeedOperatorReturnCeed(op), CEED_ERROR_UNSUPPORTED, "ContextFieldLabel does not correspond to the operator"); } CeedCall(CeedOperatorIsComposite(op, &is_composite)); @@ -367,7 +365,8 @@ static int CeedOperatorContextSetGeneric(CeedOperator op, CeedContextFieldLabel CeedCall(CeedCompositeOperatorGetNumSub(op, &num_sub)); CeedCall(CeedCompositeOperatorGetSubList(op, &sub_operators)); - CeedCheck(num_sub == field_label->num_sub_labels, ceed, CEED_ERROR_UNSUPPORTED, "Composite operator modified after ContextFieldLabel created"); + CeedCheck(num_sub == field_label->num_sub_labels, CeedOperatorReturnCeed(op), CEED_ERROR_UNSUPPORTED, + "Composite operator modified after ContextFieldLabel created"); for (CeedInt i = 0; i < num_sub; i++) { CeedQFunction qf; @@ -386,7 +385,7 @@ static int CeedOperatorContextSetGeneric(CeedOperator op, CeedContextFieldLabel CeedCall(CeedOperatorGetQFunction(op, &qf)); CeedCall(CeedQFunctionGetContext(qf, &ctx)); - CeedCheck(ctx, ceed, CEED_ERROR_UNSUPPORTED, "QFunction does not have context data"); + CeedCheck(ctx, CeedOperatorReturnCeed(op), CEED_ERROR_UNSUPPORTED, "QFunction does not have context data"); CeedCall(CeedQFunctionContextSetGeneric(ctx, field_label, field_type, values)); } CeedCall(CeedOperatorSetQFunctionAssemblyDataUpdateNeeded(op, true)); @@ -412,10 +411,8 @@ static int CeedOperatorContextSetGeneric(CeedOperator op, CeedContextFieldLabel static int CeedOperatorContextGetGenericRead(CeedOperator op, CeedContextFieldLabel field_label, CeedContextFieldType field_type, size_t *num_values, void *values) { bool is_composite = false; - Ceed ceed; - CeedCall(CeedOperatorGetCeed(op, &ceed)); - CeedCheck(field_label, ceed, CEED_ERROR_UNSUPPORTED, "Invalid field label"); + CeedCheck(field_label, CeedOperatorReturnCeed(op), CEED_ERROR_UNSUPPORTED, "Invalid field label"); *(void **)values = NULL; *num_values = 0; @@ -427,7 +424,7 @@ static int CeedOperatorContextGetGenericRead(CeedOperator op, CeedContextFieldLa for (CeedInt i = 0; i < op->num_context_labels; i++) { if (op->context_labels[i] == field_label) index = i; } - CeedCheck(index != -1, ceed, CEED_ERROR_UNSUPPORTED, "ContextFieldLabel does not correspond to the operator"); + CeedCheck(index != -1, CeedOperatorReturnCeed(op), CEED_ERROR_UNSUPPORTED, "ContextFieldLabel does not correspond to the operator"); } CeedCall(CeedOperatorIsComposite(op, &is_composite)); @@ -437,7 +434,8 @@ static int CeedOperatorContextGetGenericRead(CeedOperator op, CeedContextFieldLa CeedCall(CeedCompositeOperatorGetNumSub(op, &num_sub)); CeedCall(CeedCompositeOperatorGetSubList(op, &sub_operators)); - CeedCheck(num_sub == field_label->num_sub_labels, ceed, CEED_ERROR_UNSUPPORTED, "Composite operator modified after ContextFieldLabel created"); + CeedCheck(num_sub == field_label->num_sub_labels, CeedOperatorReturnCeed(op), CEED_ERROR_UNSUPPORTED, + "Composite operator modified after ContextFieldLabel created"); for (CeedInt i = 0; i < num_sub; i++) { CeedQFunction qf; @@ -457,7 +455,7 @@ static int CeedOperatorContextGetGenericRead(CeedOperator op, CeedContextFieldLa CeedCall(CeedOperatorGetQFunction(op, &qf)); CeedCall(CeedQFunctionGetContext(qf, &ctx)); - CeedCheck(ctx, ceed, CEED_ERROR_UNSUPPORTED, "QFunction does not have context data"); + CeedCheck(ctx, CeedOperatorReturnCeed(op), CEED_ERROR_UNSUPPORTED, "QFunction does not have context data"); CeedCall(CeedQFunctionContextGetGenericRead(ctx, field_label, field_type, num_values, values)); } return CEED_ERROR_SUCCESS; @@ -480,10 +478,8 @@ static int CeedOperatorContextGetGenericRead(CeedOperator op, CeedContextFieldLa **/ static int CeedOperatorContextRestoreGenericRead(CeedOperator op, CeedContextFieldLabel field_label, CeedContextFieldType field_type, void *values) { bool is_composite = false; - Ceed ceed; - CeedCall(CeedOperatorGetCeed(op, &ceed)); - CeedCheck(field_label, ceed, CEED_ERROR_UNSUPPORTED, "Invalid field label"); + CeedCheck(field_label, CeedOperatorReturnCeed(op), CEED_ERROR_UNSUPPORTED, "Invalid field label"); // Check if field_label and op correspond if (field_label->from_op) { @@ -492,7 +488,7 @@ static int CeedOperatorContextRestoreGenericRead(CeedOperator op, CeedContextFie for (CeedInt i = 0; i < op->num_context_labels; i++) { if (op->context_labels[i] == field_label) index = i; } - CeedCheck(index != -1, ceed, CEED_ERROR_UNSUPPORTED, "ContextFieldLabel does not correspond to the operator"); + CeedCheck(index != -1, CeedOperatorReturnCeed(op), CEED_ERROR_UNSUPPORTED, "ContextFieldLabel does not correspond to the operator"); } CeedCall(CeedOperatorIsComposite(op, &is_composite)); @@ -502,7 +498,8 @@ static int CeedOperatorContextRestoreGenericRead(CeedOperator op, CeedContextFie CeedCall(CeedCompositeOperatorGetNumSub(op, &num_sub)); CeedCall(CeedCompositeOperatorGetSubList(op, &sub_operators)); - CeedCheck(num_sub == field_label->num_sub_labels, ceed, CEED_ERROR_UNSUPPORTED, "Composite operator modified after ContextFieldLabel created"); + CeedCheck(num_sub == field_label->num_sub_labels, CeedOperatorReturnCeed(op), CEED_ERROR_UNSUPPORTED, + "Composite operator modified after ContextFieldLabel created"); for (CeedInt i = 0; i < num_sub; i++) { CeedQFunction qf; @@ -522,7 +519,7 @@ static int CeedOperatorContextRestoreGenericRead(CeedOperator op, CeedContextFie CeedCall(CeedOperatorGetQFunction(op, &qf)); CeedCall(CeedQFunctionGetContext(qf, &ctx)); - CeedCheck(ctx, ceed, CEED_ERROR_UNSUPPORTED, "QFunction does not have context data"); + CeedCheck(ctx, CeedOperatorReturnCeed(op), CEED_ERROR_UNSUPPORTED, "QFunction does not have context data"); CeedCall(CeedQFunctionContextRestoreGenericRead(ctx, field_label, field_type, values)); } return CEED_ERROR_SUCCESS; @@ -750,6 +747,7 @@ int CeedOperatorCreate(Ceed ceed, CeedQFunction qf, CeedQFunction dqf, CeedQFunc CeedCall(CeedGetObjectDelegate(ceed, &delegate, "Operator")); CeedCheck(delegate, ceed, CEED_ERROR_UNSUPPORTED, "Backend does not implement CeedOperatorCreate"); CeedCall(CeedOperatorCreate(delegate, qf, dqf, dqfT, op)); + CeedCall(CeedDestroy(&delegate)); return CEED_ERROR_SUCCESS; } @@ -792,6 +790,7 @@ int CeedOperatorCreateAtPoints(Ceed ceed, CeedQFunction qf, CeedQFunction dqf, C CeedCall(CeedGetObjectDelegate(ceed, &delegate, "Operator")); CeedCheck(delegate, ceed, CEED_ERROR_UNSUPPORTED, "Backend does not implement CeedOperatorCreateAtPoints"); CeedCall(CeedOperatorCreateAtPoints(delegate, qf, dqf, dqfT, op)); + CeedCall(CeedDestroy(&delegate)); return CEED_ERROR_SUCCESS; } @@ -829,6 +828,7 @@ int CeedCompositeOperatorCreate(Ceed ceed, CeedOperator *op) { CeedCall(CeedGetObjectDelegate(ceed, &delegate, "Operator")); if (delegate) { CeedCall(CeedCompositeOperatorCreate(delegate, op)); + CeedCall(CeedDestroy(&delegate)); return CEED_ERROR_SUCCESS; } } @@ -893,38 +893,38 @@ int CeedOperatorReferenceCopy(CeedOperator op, CeedOperator *op_copy) { int CeedOperatorSetField(CeedOperator op, const char *field_name, CeedElemRestriction rstr, CeedBasis basis, CeedVector vec) { bool is_input = true, is_at_points, is_composite, is_immutable; CeedInt num_elem = 0, num_qpts = 0, num_input_fields, num_output_fields; - Ceed ceed; CeedQFunction qf; CeedQFunctionField qf_field, *qf_input_fields, *qf_output_fields; CeedOperatorField *op_field; - CeedCall(CeedOperatorGetCeed(op, &ceed)); CeedCall(CeedOperatorIsAtPoints(op, &is_at_points)); CeedCall(CeedOperatorIsComposite(op, &is_composite)); CeedCall(CeedOperatorIsImmutable(op, &is_immutable)); - CeedCheck(!is_composite, ceed, CEED_ERROR_INCOMPATIBLE, "Cannot add field to composite operator."); - CeedCheck(!is_immutable, ceed, CEED_ERROR_MAJOR, "Operator cannot be changed after set as immutable"); - CeedCheck(rstr, ceed, CEED_ERROR_INCOMPATIBLE, "CeedElemRestriction rstr for field \"%s\" must be non-NULL.", field_name); - CeedCheck(basis, ceed, CEED_ERROR_INCOMPATIBLE, "CeedBasis basis for field \"%s\" must be non-NULL.", field_name); - CeedCheck(vec, ceed, CEED_ERROR_INCOMPATIBLE, "CeedVector vec for field \"%s\" must be non-NULL.", field_name); + CeedCheck(!is_composite, CeedOperatorReturnCeed(op), CEED_ERROR_INCOMPATIBLE, "Cannot add field to composite operator."); + CeedCheck(!is_immutable, CeedOperatorReturnCeed(op), CEED_ERROR_MAJOR, "Operator cannot be changed after set as immutable"); + CeedCheck(rstr, CeedOperatorReturnCeed(op), CEED_ERROR_INCOMPATIBLE, "CeedElemRestriction rstr for field \"%s\" must be non-NULL.", field_name); + CeedCheck(basis, CeedOperatorReturnCeed(op), CEED_ERROR_INCOMPATIBLE, "CeedBasis basis for field \"%s\" must be non-NULL.", field_name); + CeedCheck(vec, CeedOperatorReturnCeed(op), CEED_ERROR_INCOMPATIBLE, "CeedVector vec for field \"%s\" must be non-NULL.", field_name); CeedCall(CeedElemRestrictionGetNumElements(rstr, &num_elem)); - CeedCheck(rstr == CEED_ELEMRESTRICTION_NONE || !op->has_restriction || num_elem == op->num_elem, ceed, CEED_ERROR_DIMENSION, + CeedCheck(rstr == CEED_ELEMRESTRICTION_NONE || !op->has_restriction || num_elem == op->num_elem, CeedOperatorReturnCeed(op), CEED_ERROR_DIMENSION, "CeedElemRestriction with %" CeedInt_FMT " elements incompatible with prior %" CeedInt_FMT " elements", num_elem, op->num_elem); { CeedRestrictionType rstr_type; CeedCall(CeedElemRestrictionGetType(rstr, &rstr_type)); if (rstr_type == CEED_RESTRICTION_POINTS) { - CeedCheck(is_at_points, ceed, CEED_ERROR_UNSUPPORTED, "CeedElemRestriction AtPoints not supported for standard operator fields"); - CeedCheck(basis == CEED_BASIS_NONE, ceed, CEED_ERROR_UNSUPPORTED, "CeedElemRestriction AtPoints must be used with CEED_BASIS_NONE"); + CeedCheck(is_at_points, CeedOperatorReturnCeed(op), CEED_ERROR_UNSUPPORTED, + "CeedElemRestriction AtPoints not supported for standard operator fields"); + CeedCheck(basis == CEED_BASIS_NONE, CeedOperatorReturnCeed(op), CEED_ERROR_UNSUPPORTED, + "CeedElemRestriction AtPoints must be used with CEED_BASIS_NONE"); if (!op->first_points_rstr) { CeedCall(CeedElemRestrictionReferenceCopy(rstr, &op->first_points_rstr)); } else { bool are_compatible; CeedCall(CeedElemRestrictionAtPointsAreCompatible(op->first_points_rstr, rstr, &are_compatible)); - CeedCheck(are_compatible, ceed, CEED_ERROR_INCOMPATIBLE, + CeedCheck(are_compatible, CeedOperatorReturnCeed(op), CEED_ERROR_INCOMPATIBLE, "CeedElemRestriction must have compatible offsets with previously set CeedElemRestriction"); } } @@ -932,7 +932,7 @@ int CeedOperatorSetField(CeedOperator op, const char *field_name, CeedElemRestri if (basis == CEED_BASIS_NONE) CeedCall(CeedElemRestrictionGetElementSize(rstr, &num_qpts)); else CeedCall(CeedBasisGetNumQuadraturePoints(basis, &num_qpts)); - CeedCheck(op->num_qpts == 0 || num_qpts == op->num_qpts, ceed, CEED_ERROR_DIMENSION, + CeedCheck(op->num_qpts == 0 || num_qpts == op->num_qpts, CeedOperatorReturnCeed(op), CEED_ERROR_DIMENSION, "%s must correspond to the same number of quadrature points as previously added CeedBases. Found %" CeedInt_FMT " quadrature points but expected %" CeedInt_FMT " quadrature points.", basis == CEED_BASIS_NONE ? "CeedElemRestriction" : "CeedBasis", num_qpts, op->num_qpts); @@ -961,10 +961,10 @@ int CeedOperatorSetField(CeedOperator op, const char *field_name, CeedElemRestri } } // LCOV_EXCL_START - return CeedError(ceed, CEED_ERROR_INCOMPLETE, "CeedQFunction has no knowledge of field '%s'", field_name); + return CeedError(CeedOperatorReturnCeed(op), CEED_ERROR_INCOMPLETE, "CeedQFunction has no knowledge of field '%s'", field_name); // LCOV_EXCL_STOP found: - CeedCall(CeedOperatorCheckField(ceed, qf_field, rstr, basis)); + CeedCall(CeedOperatorCheckField(CeedOperatorReturnCeed(op), qf_field, rstr, basis)); CeedCall(CeedCalloc(1, op_field)); if (vec == CEED_VECTOR_ACTIVE) { @@ -973,11 +973,11 @@ int CeedOperatorSetField(CeedOperator op, const char *field_name, CeedElemRestri CeedCall(CeedElemRestrictionGetLVectorSize(rstr, &l_size)); if (is_input) { if (op->input_size == -1) op->input_size = l_size; - CeedCheck(l_size == op->input_size, ceed, CEED_ERROR_INCOMPATIBLE, + CeedCheck(l_size == op->input_size, CeedOperatorReturnCeed(op), CEED_ERROR_INCOMPATIBLE, "LVector size %" CeedSize_FMT " does not match previous size %" CeedSize_FMT "", l_size, op->input_size); } else { if (op->output_size == -1) op->output_size = l_size; - CeedCheck(l_size == op->output_size, ceed, CEED_ERROR_INCOMPATIBLE, + CeedCheck(l_size == op->output_size, CeedOperatorReturnCeed(op), CEED_ERROR_INCOMPATIBLE, "LVector size %" CeedSize_FMT " does not match previous size %" CeedSize_FMT "", l_size, op->output_size); } } @@ -1041,13 +1041,11 @@ int CeedOperatorGetFields(CeedOperator op, CeedInt *num_input_fields, CeedOperat **/ int CeedOperatorAtPointsSetPoints(CeedOperator op, CeedElemRestriction rstr_points, CeedVector point_coords) { bool is_at_points, is_immutable; - Ceed ceed; - CeedCall(CeedOperatorGetCeed(op, &ceed)); CeedCall(CeedOperatorIsAtPoints(op, &is_at_points)); CeedCall(CeedOperatorIsImmutable(op, &is_immutable)); - CeedCheck(is_at_points, ceed, CEED_ERROR_MINOR, "Only defined for operator at points"); - CeedCheck(!is_immutable, ceed, CEED_ERROR_MAJOR, "Operator cannot be changed after set as immutable"); + CeedCheck(is_at_points, CeedOperatorReturnCeed(op), CEED_ERROR_MINOR, "Only defined for operator at points"); + CeedCheck(!is_immutable, CeedOperatorReturnCeed(op), CEED_ERROR_MAJOR, "Operator cannot be changed after set as immutable"); if (!op->first_points_rstr) { CeedCall(CeedElemRestrictionReferenceCopy(rstr_points, &op->first_points_rstr)); @@ -1055,7 +1053,7 @@ int CeedOperatorAtPointsSetPoints(CeedOperator op, CeedElemRestriction rstr_poin bool are_compatible; CeedCall(CeedElemRestrictionAtPointsAreCompatible(op->first_points_rstr, rstr_points, &are_compatible)); - CeedCheck(are_compatible, ceed, CEED_ERROR_INCOMPATIBLE, + CeedCheck(are_compatible, CeedOperatorReturnCeed(op), CEED_ERROR_INCOMPATIBLE, "CeedElemRestriction must have compatible offsets with previously set field CeedElemRestriction"); } @@ -1249,13 +1247,12 @@ int CeedOperatorFieldGetData(CeedOperatorField op_field, const char **field_name */ int CeedCompositeOperatorAddSub(CeedOperator composite_op, CeedOperator sub_op) { bool is_immutable; - Ceed ceed; - CeedCall(CeedOperatorGetCeed(composite_op, &ceed)); - CeedCheck(composite_op->is_composite, ceed, CEED_ERROR_MINOR, "CeedOperator is not a composite operator"); - CeedCheck(composite_op->num_suboperators < CEED_COMPOSITE_MAX, ceed, CEED_ERROR_UNSUPPORTED, "Cannot add additional sub-operators"); + CeedCheck(composite_op->is_composite, CeedOperatorReturnCeed(composite_op), CEED_ERROR_MINOR, "CeedOperator is not a composite operator"); + CeedCheck(composite_op->num_suboperators < CEED_COMPOSITE_MAX, CeedOperatorReturnCeed(composite_op), CEED_ERROR_UNSUPPORTED, + "Cannot add additional sub-operators"); CeedCall(CeedOperatorIsImmutable(composite_op, &is_immutable)); - CeedCheck(!is_immutable, ceed, CEED_ERROR_MAJOR, "Operator cannot be changed after set as immutable"); + CeedCheck(!is_immutable, CeedOperatorReturnCeed(composite_op), CEED_ERROR_MAJOR, "Operator cannot be changed after set as immutable"); { CeedSize input_size, output_size; @@ -1264,8 +1261,8 @@ int CeedCompositeOperatorAddSub(CeedOperator composite_op, CeedOperator sub_op) if (composite_op->input_size == -1) composite_op->input_size = input_size; if (composite_op->output_size == -1) composite_op->output_size = output_size; // Note, a size of -1 means no active vector restriction set, so no incompatibility - CeedCheck((input_size == -1 || input_size == composite_op->input_size) && (output_size == -1 || output_size == composite_op->output_size), ceed, - CEED_ERROR_MAJOR, + CeedCheck((input_size == -1 || input_size == composite_op->input_size) && (output_size == -1 || output_size == composite_op->output_size), + CeedOperatorReturnCeed(composite_op), CEED_ERROR_MAJOR, "Sub-operators must have compatible dimensions; composite operator of shape (%" CeedSize_FMT ", %" CeedSize_FMT ") not compatible with sub-operator of " "shape (%" CeedSize_FMT ", %" CeedSize_FMT ")", @@ -1361,12 +1358,10 @@ int CeedCompositeOperatorGetSubByName(CeedOperator op, const char *op_name, Ceed **/ int CeedOperatorCheckReady(CeedOperator op) { bool is_at_points, is_composite; - Ceed ceed; CeedQFunction qf = NULL; if (op->is_interface_setup) return CEED_ERROR_SUCCESS; - CeedCall(CeedOperatorGetCeed(op, &ceed)); CeedCall(CeedOperatorIsAtPoints(op, &is_at_points)); CeedCall(CeedOperatorIsComposite(op, &is_composite)); if (!is_composite) CeedCall(CeedOperatorGetQFunction(op, &qf)); @@ -1393,11 +1388,12 @@ int CeedOperatorCheckReady(CeedOperator op) { } else { CeedInt num_input_fields, num_output_fields; - CeedCheck(op->num_fields > 0, ceed, CEED_ERROR_INCOMPLETE, "No operator fields set"); + CeedCheck(op->num_fields > 0, CeedOperatorReturnCeed(op), CEED_ERROR_INCOMPLETE, "No operator fields set"); CeedCall(CeedQFunctionGetFields(qf, &num_input_fields, NULL, &num_output_fields, NULL)); - CeedCheck(op->num_fields == num_input_fields + num_output_fields, ceed, CEED_ERROR_INCOMPLETE, "Not all operator fields set"); - CeedCheck(op->has_restriction, ceed, CEED_ERROR_INCOMPLETE, "At least one restriction required"); - CeedCheck(op->num_qpts > 0 || is_at_points, ceed, CEED_ERROR_INCOMPLETE, + CeedCheck(op->num_fields == num_input_fields + num_output_fields, CeedOperatorReturnCeed(op), CEED_ERROR_INCOMPLETE, + "Not all operator fields set"); + CeedCheck(op->has_restriction, CeedOperatorReturnCeed(op), CEED_ERROR_INCOMPLETE, "At least one restriction required"); + CeedCheck(op->num_qpts > 0 || is_at_points, CeedOperatorReturnCeed(op), CEED_ERROR_INCOMPLETE, "At least one non-collocated CeedBasis is required or the number of quadrature points must be set"); } @@ -1612,7 +1608,8 @@ int CeedOperatorViewTerse(CeedOperator op, FILE *stream) { @ref Advanced **/ int CeedOperatorGetCeed(CeedOperator op, Ceed *ceed) { - *ceed = CeedOperatorReturnCeed(op); + *ceed = NULL; + CeedCall(CeedReferenceCopy(CeedOperatorReturnCeed(op), ceed)); return CEED_ERROR_SUCCESS; } diff --git a/interface/ceed-preconditioning.c b/interface/ceed-preconditioning.c index 3e82b38493..937d7fde0d 100644 --- a/interface/ceed-preconditioning.c +++ b/interface/ceed-preconditioning.c @@ -37,15 +37,13 @@ static int CeedQFunctionCreateFallback(Ceed fallback_ceed, CeedQFunction qf, CeedQFunction *qf_fallback) { char *source_path_with_name = NULL; CeedInt num_input_fields, num_output_fields; - Ceed ceed; CeedQFunctionField *input_fields, *output_fields; // Check if NULL qf passed in if (!qf) return CEED_ERROR_SUCCESS; - CeedCall(CeedQFunctionGetCeed(qf, &ceed)); - CeedDebug256(ceed, 1, "---------- CeedOperator Fallback ----------\n"); - CeedDebug(ceed, "Creating fallback CeedQFunction\n"); + CeedDebug256(CeedQFunctionReturnCeed(qf), 1, "---------- CeedOperator Fallback ----------\n"); + CeedDebug(CeedQFunctionReturnCeed(qf), "Creating fallback CeedQFunction\n"); if (qf->source_path) { size_t path_len = strlen(qf->source_path), name_len = strlen(qf->kernel_name); @@ -115,10 +113,11 @@ static int CeedOperatorCreateFallback(CeedOperator op) { // Fallback Ceed CeedCall(CeedOperatorGetCeed(op, &ceed)); CeedCall(CeedGetOperatorFallbackCeed(ceed, &ceed_fallback)); + CeedCall(CeedDestroy(&ceed)); if (!ceed_fallback) return CEED_ERROR_SUCCESS; - CeedDebug256(ceed, 1, "---------- CeedOperator Fallback ----------\n"); - CeedDebug(ceed, "Creating fallback CeedOperator\n"); + CeedDebug256(CeedOperatorReturnCeed(op), 1, "---------- CeedOperator Fallback ----------\n"); + CeedDebug(CeedOperatorReturnCeed(op), "Creating fallback CeedOperator\n"); // Clone Op CeedCall(CeedOperatorIsComposite(op, &is_composite)); @@ -186,6 +185,7 @@ static int CeedOperatorCreateFallback(CeedOperator op) { // The op holds the only reference to op_fallback and is responsible for deleting itself and op_fallback. op->op_fallback = op_fallback; op_fallback->op_fallback_parent = op; + CeedCall(CeedDestroy(&ceed_fallback)); return CEED_ERROR_SUCCESS; } @@ -203,12 +203,10 @@ static int CeedOperatorCreateFallback(CeedOperator op) { **/ static inline int CeedSingleOperatorLinearAssembleAddDiagonal_Mesh(CeedOperator op, CeedRequest *request, const bool is_point_block, CeedVector assembled) { - Ceed ceed; bool is_composite; - CeedCall(CeedOperatorGetCeed(op, &ceed)); CeedCall(CeedOperatorIsComposite(op, &is_composite)); - CeedCheck(!is_composite, ceed, CEED_ERROR_UNSUPPORTED, "Composite operator not supported"); + CeedCheck(!is_composite, CeedOperatorReturnCeed(op), CEED_ERROR_UNSUPPORTED, "Composite operator not supported"); // Assemble QFunction CeedInt layout_qf[3]; @@ -265,7 +263,7 @@ static inline int CeedSingleOperatorLinearAssembleAddDiagonal_Mesh(CeedOperator continue; } // No matching output basis found } - CeedCheck(active_elem_rstrs_in[b_in] == active_elem_rstrs_out[b_out], ceed, CEED_ERROR_UNSUPPORTED, + CeedCheck(active_elem_rstrs_in[b_in] == active_elem_rstrs_out[b_out], CeedOperatorReturnCeed(op), CEED_ERROR_UNSUPPORTED, "Cannot assemble operator diagonal with different input and output active element restrictions"); // Assemble point block diagonal restriction, if needed @@ -390,12 +388,10 @@ static inline int CeedSingleOperatorLinearAssembleAddDiagonal_Mesh(CeedOperator **/ static inline int CeedSingleOperatorLinearAssembleAddDiagonal(CeedOperator op, CeedRequest *request, const bool is_point_block, CeedVector assembled) { - Ceed ceed; bool is_at_points; - CeedCall(CeedOperatorGetCeed(op, &ceed)); CeedCall(CeedOperatorIsAtPoints(op, &is_at_points)); - CeedCheck(!is_at_points, ceed, CEED_ERROR_UNSUPPORTED, "AtPoints operator not supported"); + CeedCheck(!is_at_points, CeedOperatorReturnCeed(op), CEED_ERROR_UNSUPPORTED, "AtPoints operator not supported"); CeedCall(CeedSingleOperatorLinearAssembleAddDiagonal_Mesh(op, request, is_point_block, assembled)); return CEED_ERROR_SUCCESS; } @@ -454,9 +450,9 @@ static int CeedSingleOperatorAssembleSymbolic(CeedOperator op, CeedInt offset, C CeedVector index_vec_in, index_vec_out, elem_dof_in, elem_dof_out; CeedElemRestriction elem_rstr_in, elem_rstr_out, index_elem_rstr_in, index_elem_rstr_out; - CeedCall(CeedOperatorGetCeed(op, &ceed)); CeedCall(CeedOperatorIsComposite(op, &is_composite)); CeedCheck(!is_composite, ceed, CEED_ERROR_UNSUPPORTED, "Composite operator not supported"); + CeedCall(CeedOperatorGetCeed(op, &ceed)); CeedCall(CeedOperatorGetActiveVectorLengths(op, &num_nodes_in, &num_nodes_out)); CeedCall(CeedOperatorGetActiveElemRestrictions(op, &elem_rstr_in, &elem_rstr_out)); @@ -539,6 +535,7 @@ static int CeedSingleOperatorAssembleSymbolic(CeedOperator op, CeedInt offset, C } CeedCall(CeedElemRestrictionDestroy(&elem_rstr_in)); CeedCall(CeedElemRestrictionDestroy(&elem_rstr_out)); + CeedCall(CeedDestroy(&ceed)); return CEED_ERROR_SUCCESS; } @@ -556,12 +553,10 @@ static int CeedSingleOperatorAssembleSymbolic(CeedOperator op, CeedInt offset, C @ref Developer **/ static int CeedSingleOperatorAssemble(CeedOperator op, CeedInt offset, CeedVector values) { - Ceed ceed; bool is_composite; - CeedCall(CeedOperatorGetCeed(op, &ceed)); CeedCall(CeedOperatorIsComposite(op, &is_composite)); - CeedCheck(!is_composite, ceed, CEED_ERROR_UNSUPPORTED, "Composite operator not supported"); + CeedCheck(!is_composite, CeedOperatorReturnCeed(op), CEED_ERROR_UNSUPPORTED, "Composite operator not supported"); // Early exit for empty operator { @@ -615,9 +610,10 @@ static int CeedSingleOperatorAssemble(CeedOperator op, CeedInt offset, CeedVecto CeedCall(CeedOperatorAssemblyDataGetEvalModes(data, &num_active_bases_in, &num_eval_modes_in, &eval_modes_in, NULL, &num_active_bases_out, &num_eval_modes_out, &eval_modes_out, NULL, NULL)); - CeedCheck(num_active_bases_in == 1 && num_active_bases_out == 1, ceed, CEED_ERROR_UNSUPPORTED, + CeedCheck(num_active_bases_in == 1 && num_active_bases_out == 1, CeedOperatorReturnCeed(op), CEED_ERROR_UNSUPPORTED, "Cannot assemble operator with multiple active bases"); - CeedCheck(num_eval_modes_in[0] > 0 && num_eval_modes_out[0] > 0, ceed, CEED_ERROR_UNSUPPORTED, "Cannot assemble operator without inputs/outputs"); + CeedCheck(num_eval_modes_in[0] > 0 && num_eval_modes_out[0] > 0, CeedOperatorReturnCeed(op), CEED_ERROR_UNSUPPORTED, + "Cannot assemble operator without inputs/outputs"); CeedCall(CeedOperatorAssemblyDataGetBases(data, NULL, &active_bases_in, &B_mats_in, NULL, &active_bases_out, &B_mats_out)); CeedCall(CeedOperatorGetActiveElemRestrictions(op, &elem_rstr_in, &elem_rstr_out)); @@ -641,7 +637,7 @@ static int CeedSingleOperatorAssemble(CeedOperator op, CeedInt offset, CeedVecto if (elem_rstr_in != elem_rstr_out) { CeedCall(CeedElemRestrictionGetNumElements(elem_rstr_out, &num_elem_out)); - CeedCheck(num_elem_in == num_elem_out, ceed, CEED_ERROR_UNSUPPORTED, + CeedCheck(num_elem_in == num_elem_out, CeedOperatorReturnCeed(op), CEED_ERROR_UNSUPPORTED, "Active input and output operator restrictions must have the same number of elements." " Input has %" CeedInt_FMT " elements; output has %" CeedInt_FMT "elements.", num_elem_in, num_elem_out); @@ -649,7 +645,7 @@ static int CeedSingleOperatorAssemble(CeedOperator op, CeedInt offset, CeedVecto CeedCall(CeedElemRestrictionGetNumComponents(elem_rstr_out, &num_comp_out)); if (basis_out == CEED_BASIS_NONE) num_qpts_out = elem_size_out; else CeedCall(CeedBasisGetNumQuadraturePoints(basis_out, &num_qpts_out)); - CeedCheck(num_qpts_in == num_qpts_out, ceed, CEED_ERROR_UNSUPPORTED, + CeedCheck(num_qpts_in == num_qpts_out, CeedOperatorReturnCeed(op), CEED_ERROR_UNSUPPORTED, "Active input and output bases must have the same number of quadrature points." " Input has %" CeedInt_FMT " points; output has %" CeedInt_FMT "points.", num_qpts_in, num_qpts_out); @@ -709,7 +705,11 @@ static int CeedSingleOperatorAssemble(CeedOperator op, CeedInt offset, CeedVecto CeedCall(CeedTensorContractApply(contract, 1, num_qpts_in * num_eval_modes_in[0], elem_size_in, elem_size_out, BTD_mat, CEED_NOTRANSPOSE, false, B_mat_in, elem_mat)); } else { + Ceed ceed; + + CeedCall(CeedOperatorGetCeed(op, &ceed)); CeedCall(CeedMatrixMatrixMultiply(ceed, BTD_mat, B_mat_in, elem_mat, elem_size_out, elem_size_in, num_qpts_in * num_eval_modes_in[0])); + CeedCall(CeedDestroy(&ceed)); } // Transform the element matrix if required @@ -768,7 +768,7 @@ static int CeedSingleOperatorAssemble(CeedOperator op, CeedInt offset, CeedVecto } } } - CeedCheck(count == local_num_entries, ceed, CEED_ERROR_MAJOR, "Error computing entries"); + CeedCheck(count == local_num_entries, CeedOperatorReturnCeed(op), CEED_ERROR_MAJOR, "Error computing entries"); CeedCall(CeedVectorRestoreArray(values, &vals)); // Cleanup @@ -807,12 +807,10 @@ static int CeedSingleOperatorAssemble(CeedOperator op, CeedInt offset, CeedVecto static int CeedSingleOperatorAssemblyCountEntries(CeedOperator op, CeedSize *num_entries) { bool is_composite; CeedInt num_elem_in, elem_size_in, num_comp_in, num_elem_out, elem_size_out, num_comp_out; - Ceed ceed; CeedElemRestriction rstr_in, rstr_out; - CeedCall(CeedOperatorGetCeed(op, &ceed)); CeedCall(CeedOperatorIsComposite(op, &is_composite)); - CeedCheck(!is_composite, ceed, CEED_ERROR_UNSUPPORTED, "Composite operator not supported"); + CeedCheck(!is_composite, CeedOperatorReturnCeed(op), CEED_ERROR_UNSUPPORTED, "Composite operator not supported"); CeedCall(CeedOperatorGetActiveElemRestrictions(op, &rstr_in, &rstr_out)); CeedCall(CeedElemRestrictionGetNumElements(rstr_in, &num_elem_in)); @@ -820,7 +818,7 @@ static int CeedSingleOperatorAssemblyCountEntries(CeedOperator op, CeedSize *num CeedCall(CeedElemRestrictionGetNumComponents(rstr_in, &num_comp_in)); if (rstr_in != rstr_out) { CeedCall(CeedElemRestrictionGetNumElements(rstr_out, &num_elem_out)); - CeedCheck(num_elem_in == num_elem_out, ceed, CEED_ERROR_UNSUPPORTED, + CeedCheck(num_elem_in == num_elem_out, CeedOperatorReturnCeed(op), CEED_ERROR_UNSUPPORTED, "Active input and output operator restrictions must have the same number of elements." " Input has %" CeedInt_FMT " elements; output has %" CeedInt_FMT "elements.", num_elem_in, num_elem_out); @@ -1034,6 +1032,7 @@ static int CeedSingleOperatorMultigridLevel(CeedOperator op_fine, CeedVector p_m CeedCall(CeedOperatorCheckReady(*op_coarse)); // Cleanup + CeedCall(CeedDestroy(&ceed)); CeedCall(CeedVectorDestroy(&mult_vec)); CeedCall(CeedElemRestrictionDestroy(&rstr_fine)); CeedCall(CeedElemRestrictionDestroy(&rstr_p_mult_fine)); @@ -1162,6 +1161,7 @@ int CeedOperatorCreateActivePointBlockRestriction(CeedElemRestriction rstr, Ceed // Cleanup CeedCall(CeedElemRestrictionRestoreOffsets(rstr, &offsets)); + CeedCall(CeedDestroy(&ceed)); return CEED_ERROR_SUCCESS; } @@ -1811,7 +1811,9 @@ int CeedOperatorGetFallback(CeedOperator op, CeedOperator *op_fallback) { CeedDebug256(ceed, CEED_DEBUG_COLOR_SUCCESS, "---------- CeedOperator Fallback ----------\n"); CeedDebug(ceed, "Falling back from %s operator at address %p to %s operator at address %p\n", resource, op, resource_fallback, op->op_fallback); + CeedCall(CeedDestroy(&ceed_fallback)); } + CeedCall(CeedDestroy(&ceed)); } *op_fallback = op->op_fallback; return CEED_ERROR_SUCCESS; @@ -1843,7 +1845,9 @@ int CeedOperatorGetFallbackParent(CeedOperator op, CeedOperator *parent) { @ref Backend **/ int CeedOperatorGetFallbackParentCeed(CeedOperator op, Ceed *parent) { - *parent = op->op_fallback_parent ? op->op_fallback_parent->ceed : op->ceed; + *parent = NULL; + if (op->op_fallback_parent) CeedCall(CeedReferenceCopy(op->op_fallback_parent->ceed, parent)); + else CeedCall(CeedReferenceCopy(CeedOperatorReturnCeed(op), parent)); return CEED_ERROR_SUCCESS; } @@ -1883,13 +1887,11 @@ int CeedOperatorLinearAssembleQFunction(CeedOperator op, CeedVector *assembled, CeedCall(op->LinearAssembleQFunction(op, assembled, rstr, request)); } else { // Operator fallback - Ceed ceed; CeedOperator op_fallback; - CeedCall(CeedOperatorGetCeed(op, &ceed)); CeedCall(CeedOperatorGetFallback(op, &op_fallback)); if (op_fallback) CeedCall(CeedOperatorLinearAssembleQFunction(op_fallback, assembled, rstr, request)); - else return CeedError(ceed, CEED_ERROR_UNSUPPORTED, "Backend does not support CeedOperatorLinearAssembleQFunction"); + else return CeedError(CeedOperatorReturnCeed(op), CEED_ERROR_UNSUPPORTED, "Backend does not support CeedOperatorLinearAssembleQFunction"); } return CEED_ERROR_SUCCESS; } @@ -1961,13 +1963,11 @@ int CeedOperatorLinearAssembleQFunctionBuildOrUpdate(CeedOperator op, CeedVector CeedCall(CeedElemRestrictionDestroy(&assembled_rstr)); } else { // Operator fallback - Ceed ceed; CeedOperator op_fallback; - CeedCall(CeedOperatorGetCeed(op, &ceed)); CeedCall(CeedOperatorGetFallback(op, &op_fallback)); if (op_fallback) CeedCall(CeedOperatorLinearAssembleQFunctionBuildOrUpdate(op_fallback, assembled, rstr, request)); - else return CeedError(ceed, CEED_ERROR_UNSUPPORTED, "Backend does not support CeedOperatorLinearAssembleQFunctionUpdate"); + else return CeedError(CeedOperatorReturnCeed(op), CEED_ERROR_UNSUPPORTED, "Backend does not support CeedOperatorLinearAssembleQFunctionUpdate"); } return CEED_ERROR_SUCCESS; } @@ -1992,14 +1992,12 @@ int CeedOperatorLinearAssembleQFunctionBuildOrUpdate(CeedOperator op, CeedVector int CeedOperatorLinearAssembleDiagonal(CeedOperator op, CeedVector assembled, CeedRequest *request) { bool is_composite; CeedSize input_size = 0, output_size = 0; - Ceed ceed; - CeedCall(CeedOperatorGetCeed(op, &ceed)); CeedCall(CeedOperatorCheckReady(op)); CeedCall(CeedOperatorIsComposite(op, &is_composite)); CeedCall(CeedOperatorGetActiveVectorLengths(op, &input_size, &output_size)); - CeedCheck(input_size == output_size, ceed, CEED_ERROR_DIMENSION, "Operator must be square"); + CeedCheck(input_size == output_size, CeedOperatorReturnCeed(op), CEED_ERROR_DIMENSION, "Operator must be square"); // Early exit for empty operator if (!is_composite) { @@ -2054,14 +2052,12 @@ int CeedOperatorLinearAssembleDiagonal(CeedOperator op, CeedVector assembled, Ce int CeedOperatorLinearAssembleAddDiagonal(CeedOperator op, CeedVector assembled, CeedRequest *request) { bool is_composite; CeedSize input_size = 0, output_size = 0; - Ceed ceed; - CeedCall(CeedOperatorGetCeed(op, &ceed)); CeedCall(CeedOperatorCheckReady(op)); CeedCall(CeedOperatorIsComposite(op, &is_composite)); CeedCall(CeedOperatorGetActiveVectorLengths(op, &input_size, &output_size)); - CeedCheck(input_size == output_size, ceed, CEED_ERROR_DIMENSION, "Operator must be square"); + CeedCheck(input_size == output_size, CeedOperatorReturnCeed(op), CEED_ERROR_DIMENSION, "Operator must be square"); // Early exit for empty operator if (!is_composite) { @@ -2115,17 +2111,15 @@ int CeedOperatorLinearAssembleAddDiagonal(CeedOperator op, CeedVector assembled, @ref User **/ int CeedOperatorLinearAssemblePointBlockDiagonalSymbolic(CeedOperator op, CeedSize *num_entries, CeedInt **rows, CeedInt **cols) { - Ceed ceed; bool is_composite; CeedInt num_active_components, num_sub_operators; CeedOperator *sub_operators; - CeedCall(CeedOperatorGetCeed(op, &ceed)); CeedCall(CeedOperatorIsComposite(op, &is_composite)); CeedSize input_size = 0, output_size = 0; CeedCall(CeedOperatorGetActiveVectorLengths(op, &input_size, &output_size)); - CeedCheck(input_size == output_size, ceed, CEED_ERROR_DIMENSION, "Operator must be square"); + CeedCheck(input_size == output_size, CeedOperatorReturnCeed(op), CEED_ERROR_DIMENSION, "Operator must be square"); if (is_composite) { CeedCall(CeedCompositeOperatorGetNumSub(op, &num_sub_operators)); @@ -2155,10 +2149,10 @@ int CeedOperatorLinearAssemblePointBlockDiagonalSymbolic(CeedOperator op, CeedSi CeedInt comp_stride_sub, num_active_components_sub; CeedCall(CeedElemRestrictionGetCompStride(active_elem_rstrs[i], &comp_stride_sub)); - CeedCheck(comp_stride == comp_stride_sub, ceed, CEED_ERROR_DIMENSION, + CeedCheck(comp_stride == comp_stride_sub, CeedOperatorReturnCeed(op), CEED_ERROR_DIMENSION, "Active element restrictions must have the same component stride: %d vs %d", comp_stride, comp_stride_sub); CeedCall(CeedElemRestrictionGetNumComponents(active_elem_rstrs[i], &num_active_components_sub)); - CeedCheck(num_active_components == num_active_components_sub, ceed, CEED_ERROR_INCOMPATIBLE, + CeedCheck(num_active_components == num_active_components_sub, CeedOperatorReturnCeed(op), CEED_ERROR_INCOMPATIBLE, "All suboperators must have the same number of output components." " Previous: %" CeedInt_FMT " Current: %" CeedInt_FMT, num_active_components, num_active_components_sub); @@ -2222,14 +2216,12 @@ int CeedOperatorLinearAssemblePointBlockDiagonalSymbolic(CeedOperator op, CeedSi int CeedOperatorLinearAssemblePointBlockDiagonal(CeedOperator op, CeedVector assembled, CeedRequest *request) { bool is_composite; CeedSize input_size = 0, output_size = 0; - Ceed ceed; - CeedCall(CeedOperatorGetCeed(op, &ceed)); CeedCall(CeedOperatorCheckReady(op)); CeedCall(CeedOperatorIsComposite(op, &is_composite)); CeedCall(CeedOperatorGetActiveVectorLengths(op, &input_size, &output_size)); - CeedCheck(input_size == output_size, ceed, CEED_ERROR_DIMENSION, "Operator must be square"); + CeedCheck(input_size == output_size, CeedOperatorReturnCeed(op), CEED_ERROR_DIMENSION, "Operator must be square"); // Early exit for empty operator if (!is_composite) { @@ -2286,14 +2278,12 @@ int CeedOperatorLinearAssemblePointBlockDiagonal(CeedOperator op, CeedVector ass int CeedOperatorLinearAssembleAddPointBlockDiagonal(CeedOperator op, CeedVector assembled, CeedRequest *request) { bool is_composite; CeedSize input_size = 0, output_size = 0; - Ceed ceed; - CeedCall(CeedOperatorGetCeed(op, &ceed)); CeedCall(CeedOperatorCheckReady(op)); CeedCall(CeedOperatorIsComposite(op, &is_composite)); CeedCall(CeedOperatorGetActiveVectorLengths(op, &input_size, &output_size)); - CeedCheck(input_size == output_size, ceed, CEED_ERROR_DIMENSION, "Operator must be square"); + CeedCheck(input_size == output_size, CeedOperatorReturnCeed(op), CEED_ERROR_DIMENSION, "Operator must be square"); // Early exit for empty operator if (!is_composite) { @@ -2494,19 +2484,19 @@ int CeedCompositeOperatorGetMultiplicity(CeedOperator op, CeedInt num_skip_indic CeedCall(CeedOperatorCheckReady(op)); - CeedCall(CeedOperatorGetCeed(op, &ceed)); - // Zero mult vector CeedCall(CeedVectorSetValue(mult, 0.0)); // Get suboperators CeedCall(CeedCompositeOperatorGetNumSub(op, &num_suboperators)); - CeedCall(CeedCompositeOperatorGetSubList(op, &sub_operators)); if (num_suboperators == 0) return CEED_ERROR_SUCCESS; + CeedCall(CeedCompositeOperatorGetSubList(op, &sub_operators)); // Work vector CeedCall(CeedVectorGetLength(mult, &l_vec_len)); + CeedCall(CeedOperatorGetCeed(op, &ceed)); CeedCall(CeedVectorCreate(ceed, l_vec_len, &ones_l_vec)); + CeedCall(CeedDestroy(&ceed)); CeedCall(CeedVectorSetValue(ones_l_vec, 1.0)); CeedCall(CeedVectorGetArray(mult, CEED_MEM_HOST, &mult_array)); @@ -2642,6 +2632,7 @@ int CeedOperatorMultigridLevelCreateTensorH1(CeedOperator op_fine, CeedVector p_ // Core code CeedCall(CeedSingleOperatorMultigridLevel(op_fine, p_mult_fine, rstr_coarse, basis_coarse, basis_c_to_f, op_coarse, op_prolong, op_restrict)); + CeedCall(CeedDestroy(&ceed)); return CEED_ERROR_SUCCESS; } @@ -2705,6 +2696,7 @@ int CeedOperatorMultigridLevelCreateH1(CeedOperator op_fine, CeedVector p_mult_f // Core code CeedCall(CeedSingleOperatorMultigridLevel(op_fine, p_mult_fine, rstr_coarse, basis_coarse, basis_c_to_f, op_coarse, op_prolong, op_restrict)); + CeedCall(CeedDestroy(&ceed)); return CEED_ERROR_SUCCESS; } @@ -2939,6 +2931,8 @@ int CeedOperatorCreateFDMElementInverse(CeedOperator op, CeedOperator *fdm_inv, CeedCall(CeedOperatorSetField(*fdm_inv, "output", rstr, fdm_basis, CEED_VECTOR_ACTIVE)); // Cleanup + CeedCall(CeedDestroy(&ceed)); + CeedCall(CeedDestroy(&ceed_parent)); CeedCall(CeedVectorDestroy(&q_data)); CeedCall(CeedElemRestrictionDestroy(&rstr)); CeedCall(CeedElemRestrictionDestroy(&rstr_qd_i)); diff --git a/interface/ceed-qfunction.c b/interface/ceed-qfunction.c index 7c0cf3a285..0daa4b98fc 100644 --- a/interface/ceed-qfunction.c +++ b/interface/ceed-qfunction.c @@ -252,6 +252,7 @@ int CeedQFunctionGetSourcePath(CeedQFunction qf, const char **source_path) { } else { CeedCall(CeedGetJitAbsolutePath(ceed, qf->user_source, &absolute_path)); } + CeedCall(CeedDestroy(&ceed)); size_t source_len = strlen(absolute_path) - kernel_name_len - 1; @@ -295,6 +296,7 @@ int CeedQFunctionLoadSourceToBuffer(CeedQFunction qf, const char **source_buffer CeedCall(CeedQFunctionGetCeed(qf, &ceed)); CeedCall(CeedLoadSourceToBuffer(ceed, source_path, &buffer)); + CeedCall(CeedDestroy(&ceed)); *source_buffer = buffer; } return CEED_ERROR_SUCCESS; @@ -628,6 +630,7 @@ int CeedQFunctionCreateInterior(Ceed ceed, CeedInt vec_length, CeedQFunctionUser CeedCall(CeedGetObjectDelegate(ceed, &delegate, "QFunction")); CeedCheck(delegate, ceed, CEED_ERROR_UNSUPPORTED, "Backend does not implement CeedQFunctionCreateInterior"); CeedCall(CeedQFunctionCreateInterior(delegate, vec_length, f, source, qf)); + CeedCall(CeedDestroy(&delegate)); return CEED_ERROR_SUCCESS; } @@ -770,18 +773,16 @@ int CeedQFunctionReferenceCopy(CeedQFunction qf, CeedQFunction *qf_copy) { **/ int CeedQFunctionAddInput(CeedQFunction qf, const char *field_name, CeedInt size, CeedEvalMode eval_mode) { bool is_immutable; - Ceed ceed; - CeedCall(CeedQFunctionGetCeed(qf, &ceed)); CeedCall(CeedQFunctionIsImmutable(qf, &is_immutable)); - CeedCheck(!is_immutable, ceed, CEED_ERROR_MAJOR, "QFunction cannot be changed after set as immutable"); - CeedCheck(eval_mode != CEED_EVAL_WEIGHT || size == 1, ceed, CEED_ERROR_DIMENSION, "CEED_EVAL_WEIGHT should have size 1"); + CeedCheck(!is_immutable, CeedQFunctionReturnCeed(qf), CEED_ERROR_MAJOR, "QFunction cannot be changed after set as immutable"); + CeedCheck(eval_mode != CEED_EVAL_WEIGHT || size == 1, CeedQFunctionReturnCeed(qf), CEED_ERROR_DIMENSION, "CEED_EVAL_WEIGHT should have size 1"); for (CeedInt i = 0; i < qf->num_input_fields; i++) { - CeedCheck(strcmp(field_name, qf->input_fields[i]->field_name), ceed, CEED_ERROR_MINOR, + CeedCheck(strcmp(field_name, qf->input_fields[i]->field_name), CeedQFunctionReturnCeed(qf), CEED_ERROR_MINOR, "CeedQFunction field names must be unique. Duplicate name: %s", field_name); } for (CeedInt i = 0; i < qf->num_output_fields; i++) { - CeedCheck(strcmp(field_name, qf->output_fields[i]->field_name), ceed, CEED_ERROR_MINOR, + CeedCheck(strcmp(field_name, qf->output_fields[i]->field_name), CeedQFunctionReturnCeed(qf), CEED_ERROR_MINOR, "CeedQFunction field names must be unique. Duplicate name: %s", field_name); } CeedCall(CeedQFunctionFieldSet(&qf->input_fields[qf->num_input_fields], field_name, size, eval_mode)); @@ -807,17 +808,18 @@ int CeedQFunctionAddInput(CeedQFunction qf, const char *field_name, CeedInt size **/ int CeedQFunctionAddOutput(CeedQFunction qf, const char *field_name, CeedInt size, CeedEvalMode eval_mode) { bool is_immutable; - Ceed ceed; - CeedCall(CeedQFunctionGetCeed(qf, &ceed)); CeedCall(CeedQFunctionIsImmutable(qf, &is_immutable)); - CeedCheck(!is_immutable, ceed, CEED_ERROR_MAJOR, "CeedQFunction cannot be changed after set as immutable"); - CeedCheck(eval_mode != CEED_EVAL_WEIGHT, ceed, CEED_ERROR_DIMENSION, "Cannot create CeedQFunction output with CEED_EVAL_WEIGHT"); + CeedCheck(!is_immutable, CeedQFunctionReturnCeed(qf), CEED_ERROR_MAJOR, "CeedQFunction cannot be changed after set as immutable"); + CeedCheck(eval_mode != CEED_EVAL_WEIGHT, CeedQFunctionReturnCeed(qf), CEED_ERROR_DIMENSION, + "Cannot create CeedQFunction output with CEED_EVAL_WEIGHT"); for (CeedInt i = 0; i < qf->num_input_fields; i++) { - CeedCheck(strcmp(field_name, qf->input_fields[i]->field_name), ceed, CEED_ERROR_MINOR, "CeedQFunction field names must be unique"); + CeedCheck(strcmp(field_name, qf->input_fields[i]->field_name), CeedQFunctionReturnCeed(qf), CEED_ERROR_MINOR, + "CeedQFunction field names must be unique"); } for (CeedInt i = 0; i < qf->num_output_fields; i++) { - CeedCheck(strcmp(field_name, qf->output_fields[i]->field_name), ceed, CEED_ERROR_MINOR, "CeedQFunction field names must be unique"); + CeedCheck(strcmp(field_name, qf->output_fields[i]->field_name), CeedQFunctionReturnCeed(qf), CEED_ERROR_MINOR, + "CeedQFunction field names must be unique"); } CeedCall(CeedQFunctionFieldSet(&qf->output_fields[qf->num_output_fields], field_name, size, eval_mode)); qf->num_output_fields++; @@ -1009,7 +1011,8 @@ int CeedQFunctionView(CeedQFunction qf, FILE *stream) { @ref Advanced **/ int CeedQFunctionGetCeed(CeedQFunction qf, Ceed *ceed) { - *ceed = CeedQFunctionReturnCeed(qf); + *ceed = NULL; + CeedCall(CeedReferenceCopy(CeedQFunctionReturnCeed(qf), ceed)); return CEED_ERROR_SUCCESS; } @@ -1040,13 +1043,11 @@ Ceed CeedQFunctionReturnCeed(CeedQFunction qf) { return qf->ceed; } **/ int CeedQFunctionApply(CeedQFunction qf, CeedInt Q, CeedVector *u, CeedVector *v) { CeedInt vec_length; - Ceed ceed; - CeedCall(CeedQFunctionGetCeed(qf, &ceed)); - CeedCheck(qf->Apply, ceed, CEED_ERROR_UNSUPPORTED, "Backend does not support CeedQFunctionApply"); + CeedCheck(qf->Apply, CeedQFunctionReturnCeed(qf), CEED_ERROR_UNSUPPORTED, "Backend does not support CeedQFunctionApply"); CeedCall(CeedQFunctionGetVectorLength(qf, &vec_length)); - CeedCheck(Q % vec_length == 0, ceed, CEED_ERROR_DIMENSION, "Number of quadrature points %" CeedInt_FMT " must be a multiple of %" CeedInt_FMT, Q, - qf->vec_length); + CeedCheck(Q % vec_length == 0, CeedQFunctionReturnCeed(qf), CEED_ERROR_DIMENSION, + "Number of quadrature points %" CeedInt_FMT " must be a multiple of %" CeedInt_FMT, Q, qf->vec_length); CeedCall(CeedQFunctionSetImmutable(qf)); CeedCall(qf->Apply(qf, Q, u, v)); return CEED_ERROR_SUCCESS; diff --git a/interface/ceed-qfunctioncontext.c b/interface/ceed-qfunctioncontext.c index 16cc22cebe..a32dee73a9 100644 --- a/interface/ceed-qfunctioncontext.c +++ b/interface/ceed-qfunctioncontext.c @@ -59,12 +59,11 @@ int CeedQFunctionContextRegisterGeneric(CeedQFunctionContext ctx, const char *fi CeedContextFieldType field_type, size_t num_values) { size_t field_size = 0; CeedInt field_index = -1; - Ceed ceed; // Check for duplicate - CeedCall(CeedQFunctionContextGetCeed(ctx, &ceed)); CeedCall(CeedQFunctionContextGetFieldIndex(ctx, field_name, &field_index)); - CeedCheck(field_index == -1, ceed, CEED_ERROR_UNSUPPORTED, "QFunctionContext field with name \"%s\" already registered", field_name); + CeedCheck(field_index == -1, CeedQFunctionContextReturnCeed(ctx), CEED_ERROR_UNSUPPORTED, + "QFunctionContext field with name \"%s\" already registered", field_name); // Allocate space for field data if (ctx->num_fields == 0) { @@ -147,7 +146,8 @@ static int CeedQFunctionContextDestroyData(CeedQFunctionContext ctx) { @ref Backend **/ int CeedQFunctionContextGetCeed(CeedQFunctionContext ctx, Ceed *ceed) { - *ceed = CeedQFunctionContextReturnCeed(ctx); + *ceed = NULL; + CeedCall(CeedReferenceCopy(CeedQFunctionContextReturnCeed(ctx), ceed)); return CEED_ERROR_SUCCESS; } @@ -571,6 +571,7 @@ int CeedQFunctionContextCreate(Ceed ceed, CeedQFunctionContext *ctx) { CeedCall(CeedGetObjectDelegate(ceed, &delegate, "Context")); CeedCheck(delegate, ceed, CEED_ERROR_UNSUPPORTED, "Backend does not implement CeedQFunctionContextCreate"); CeedCall(CeedQFunctionContextCreate(delegate, ctx)); + CeedCall(CeedDestroy(&delegate)); return CEED_ERROR_SUCCESS; } @@ -620,11 +621,9 @@ int CeedQFunctionContextReferenceCopy(CeedQFunctionContext ctx, CeedQFunctionCon @ref User **/ int CeedQFunctionContextSetData(CeedQFunctionContext ctx, CeedMemType mem_type, CeedCopyMode copy_mode, size_t size, void *data) { - Ceed ceed; - - CeedCall(CeedQFunctionContextGetCeed(ctx, &ceed)); - CeedCheck(ctx->SetData, ceed, CEED_ERROR_UNSUPPORTED, "Backend does not support CeedQFunctionContextSetData"); - CeedCheck(ctx->state % 2 == 0, ceed, 1, "Cannot grant CeedQFunctionContext data access, the access lock is already in use"); + CeedCheck(ctx->SetData, CeedQFunctionContextReturnCeed(ctx), CEED_ERROR_UNSUPPORTED, "Backend does not support CeedQFunctionContextSetData"); + CeedCheck(ctx->state % 2 == 0, CeedQFunctionContextReturnCeed(ctx), 1, + "Cannot grant CeedQFunctionContext data access, the access lock is already in use"); CeedCall(CeedQFunctionContextDestroyData(ctx)); ctx->ctx_size = size; @@ -650,17 +649,16 @@ int CeedQFunctionContextSetData(CeedQFunctionContext ctx, CeedMemType mem_type, int CeedQFunctionContextTakeData(CeedQFunctionContext ctx, CeedMemType mem_type, void *data) { void *temp_data = NULL; bool has_valid_data = true, has_borrowed_data_of_type = true; - Ceed ceed; - CeedCall(CeedQFunctionContextGetCeed(ctx, &ceed)); CeedCall(CeedQFunctionContextHasValidData(ctx, &has_valid_data)); - CeedCheck(has_valid_data, ceed, CEED_ERROR_BACKEND, "CeedQFunctionContext has no valid data to take, must set data"); + CeedCheck(has_valid_data, CeedQFunctionContextReturnCeed(ctx), CEED_ERROR_BACKEND, "CeedQFunctionContext has no valid data to take, must set data"); - CeedCheck(ctx->TakeData, ceed, CEED_ERROR_UNSUPPORTED, "Backend does not support CeedQFunctionContextTakeData"); - CeedCheck(ctx->state % 2 == 0, ceed, 1, "Cannot grant CeedQFunctionContext data access, the access lock is already in use"); + CeedCheck(ctx->TakeData, CeedQFunctionContextReturnCeed(ctx), CEED_ERROR_UNSUPPORTED, "Backend does not support CeedQFunctionContextTakeData"); + CeedCheck(ctx->state % 2 == 0, CeedQFunctionContextReturnCeed(ctx), 1, + "Cannot grant CeedQFunctionContext data access, the access lock is already in use"); CeedCall(CeedQFunctionContextHasBorrowedDataOfType(ctx, mem_type, &has_borrowed_data_of_type)); - CeedCheck(has_borrowed_data_of_type, ceed, CEED_ERROR_BACKEND, + CeedCheck(has_borrowed_data_of_type, CeedQFunctionContextReturnCeed(ctx), CEED_ERROR_BACKEND, "CeedQFunctionContext has no borrowed %s data, must set data with CeedQFunctionContextSetData", CeedMemTypes[mem_type]); CeedCall(ctx->TakeData(ctx, mem_type, &temp_data)); @@ -687,15 +685,15 @@ int CeedQFunctionContextTakeData(CeedQFunctionContext ctx, CeedMemType mem_type, **/ int CeedQFunctionContextGetData(CeedQFunctionContext ctx, CeedMemType mem_type, void *data) { bool has_valid_data = true; - Ceed ceed; - CeedCall(CeedQFunctionContextGetCeed(ctx, &ceed)); - CeedCheck(ctx->GetData, ceed, CEED_ERROR_UNSUPPORTED, "Backend does not support CeedQFunctionContextGetData"); - CeedCheck(ctx->state % 2 == 0, ceed, 1, "Cannot grant CeedQFunctionContext data access, the access lock is already in use"); - CeedCheck(ctx->num_readers == 0, ceed, 1, "Cannot grant CeedQFunctionContext data access, a process has read access"); + CeedCheck(ctx->GetData, CeedQFunctionContextReturnCeed(ctx), CEED_ERROR_UNSUPPORTED, "Backend does not support CeedQFunctionContextGetData"); + CeedCheck(ctx->state % 2 == 0, CeedQFunctionContextReturnCeed(ctx), 1, + "Cannot grant CeedQFunctionContext data access, the access lock is already in use"); + CeedCheck(ctx->num_readers == 0, CeedQFunctionContextReturnCeed(ctx), 1, + "Cannot grant CeedQFunctionContext data access, a process has read access"); CeedCall(CeedQFunctionContextHasValidData(ctx, &has_valid_data)); - CeedCheck(has_valid_data, ceed, CEED_ERROR_BACKEND, "CeedQFunctionContext has no valid data to get, must set data"); + CeedCheck(has_valid_data, CeedQFunctionContextReturnCeed(ctx), CEED_ERROR_BACKEND, "CeedQFunctionContext has no valid data to get, must set data"); CeedCall(ctx->GetData(ctx, mem_type, data)); ctx->state++; @@ -721,14 +719,14 @@ int CeedQFunctionContextGetData(CeedQFunctionContext ctx, CeedMemType mem_type, **/ int CeedQFunctionContextGetDataRead(CeedQFunctionContext ctx, CeedMemType mem_type, void *data) { bool has_valid_data = true; - Ceed ceed; - CeedCall(CeedQFunctionContextGetCeed(ctx, &ceed)); - CeedCheck(ctx->GetDataRead, ceed, CEED_ERROR_UNSUPPORTED, "Backend does not support CeedQFunctionContextGetDataRead"); - CeedCheck(ctx->state % 2 == 0, ceed, 1, "Cannot grant CeedQFunctionContext data access, the access lock is already in use"); + CeedCheck(ctx->GetDataRead, CeedQFunctionContextReturnCeed(ctx), CEED_ERROR_UNSUPPORTED, + "Backend does not support CeedQFunctionContextGetDataRead"); + CeedCheck(ctx->state % 2 == 0, CeedQFunctionContextReturnCeed(ctx), 1, + "Cannot grant CeedQFunctionContext data access, the access lock is already in use"); CeedCall(CeedQFunctionContextHasValidData(ctx, &has_valid_data)); - CeedCheck(has_valid_data, ceed, CEED_ERROR_BACKEND, "CeedQFunctionContext has no valid data to get, must set data"); + CeedCheck(has_valid_data, CeedQFunctionContextReturnCeed(ctx), CEED_ERROR_BACKEND, "CeedQFunctionContext has no valid data to get, must set data"); CeedCall(ctx->GetDataRead(ctx, mem_type, data)); ctx->num_readers++; diff --git a/interface/ceed-tensor.c b/interface/ceed-tensor.c index d5b5578589..dd8b81a118 100644 --- a/interface/ceed-tensor.c +++ b/interface/ceed-tensor.c @@ -36,6 +36,7 @@ int CeedTensorContractCreate(Ceed ceed, CeedTensorContract *contract) { CeedCall(CeedGetObjectDelegate(ceed, &delegate, "TensorContract")); CeedCheck(delegate, ceed, CEED_ERROR_UNSUPPORTED, "Backend does not implement CeedTensorContractCreate"); CeedCall(CeedTensorContractCreate(delegate, contract)); + CeedCall(CeedDestroy(&delegate)); return CEED_ERROR_SUCCESS; } @@ -123,7 +124,8 @@ int CeedTensorContractStridedApply(CeedTensorContract contract, CeedInt A, CeedI @ref Backend **/ int CeedTensorContractGetCeed(CeedTensorContract contract, Ceed *ceed) { - *ceed = CeedTensorContractReturnCeed(contract); + *ceed = NULL; + CeedCall(CeedReferenceCopy(CeedTensorContractReturnCeed(contract), ceed)); return CEED_ERROR_SUCCESS; } diff --git a/interface/ceed-vector.c b/interface/ceed-vector.c index b0e277e9d2..839946762b 100644 --- a/interface/ceed-vector.c +++ b/interface/ceed-vector.c @@ -165,6 +165,7 @@ int CeedVectorCreate(Ceed ceed, CeedSize length, CeedVector *vec) { CeedCall(CeedGetObjectDelegate(ceed, &delegate, "Vector")); CeedCheck(delegate, ceed, CEED_ERROR_UNSUPPORTED, "Backend does not implement VectorCreate"); CeedCall(CeedVectorCreate(delegate, length, vec)); + CeedCall(CeedDestroy(&delegate)); return CEED_ERROR_SUCCESS; } @@ -210,17 +211,21 @@ int CeedVectorReferenceCopy(CeedVector vec, CeedVector *vec_copy) { @ref User **/ int CeedVectorCopy(CeedVector vec, CeedVector vec_copy) { - Ceed ceed; CeedMemType mem_type, mem_type_copy; CeedScalar *array; - // Get the preferred memory type - CeedCall(CeedVectorGetCeed(vec, &ceed)); - CeedCall(CeedGetPreferredMemType(ceed, &mem_type)); + // Get the preferred memory types + { + Ceed ceed; + + CeedCall(CeedVectorGetCeed(vec, &ceed)); + CeedCall(CeedGetPreferredMemType(ceed, &mem_type)); + CeedCall(CeedDestroy(&ceed)); - // Get the preferred memory type - CeedCall(CeedVectorGetCeed(vec_copy, &ceed)); - CeedCall(CeedGetPreferredMemType(ceed, &mem_type_copy)); + CeedCall(CeedVectorGetCeed(vec_copy, &ceed)); + CeedCall(CeedGetPreferredMemType(ceed, &mem_type_copy)); + CeedCall(CeedDestroy(&ceed)); + } // Check that both have same memory type if (mem_type != mem_type_copy) mem_type = CEED_MEM_HOST; @@ -231,7 +236,7 @@ int CeedVectorCopy(CeedVector vec, CeedVector vec_copy) { CeedCall(CeedVectorGetLength(vec, &length_vec)); CeedCall(CeedVectorGetLength(vec_copy, &length_copy)); - CeedCheck(length_vec == length_copy, ceed, CEED_ERROR_INCOMPATIBLE, "CeedVectors must have the same length to copy"); + CeedCheck(length_vec == length_copy, CeedVectorReturnCeed(vec), CEED_ERROR_INCOMPATIBLE, "CeedVectors must have the same length to copy"); } // Copy the values from vec to vec_copy @@ -304,13 +309,11 @@ int CeedVectorCopyStrided(CeedVector vec, CeedSize start, CeedInt step, CeedVect **/ int CeedVectorSetArray(CeedVector vec, CeedMemType mem_type, CeedCopyMode copy_mode, CeedScalar *array) { CeedSize length; - Ceed ceed; - - CeedCall(CeedVectorGetCeed(vec, &ceed)); - CeedCheck(vec->SetArray, ceed, CEED_ERROR_UNSUPPORTED, "Backend does not support VectorSetArray"); - CeedCheck(vec->state % 2 == 0, ceed, CEED_ERROR_ACCESS, "Cannot grant CeedVector array access, the access lock is already in use"); - CeedCheck(vec->num_readers == 0, ceed, CEED_ERROR_ACCESS, "Cannot grant CeedVector array access, a process has read access"); + CeedCheck(vec->SetArray, CeedVectorReturnCeed(vec), CEED_ERROR_UNSUPPORTED, "Backend does not support VectorSetArray"); + CeedCheck(vec->state % 2 == 0, CeedVectorReturnCeed(vec), CEED_ERROR_ACCESS, + "Cannot grant CeedVector array access, the access lock is already in use"); + CeedCheck(vec->num_readers == 0, CeedVectorReturnCeed(vec), CEED_ERROR_ACCESS, "Cannot grant CeedVector array access, a process has read access"); CeedCall(CeedVectorGetLength(vec, &length)); if (length > 0) CeedCall(vec->SetArray(vec, mem_type, copy_mode, array)); @@ -329,11 +332,9 @@ int CeedVectorSetArray(CeedVector vec, CeedMemType mem_type, CeedCopyMode copy_m @ref User **/ int CeedVectorSetValue(CeedVector vec, CeedScalar value) { - Ceed ceed; - - CeedCall(CeedVectorGetCeed(vec, &ceed)); - CeedCheck(vec->state % 2 == 0, ceed, CEED_ERROR_ACCESS, "Cannot grant CeedVector array access, the access lock is already in use"); - CeedCheck(vec->num_readers == 0, ceed, CEED_ERROR_ACCESS, "Cannot grant CeedVector array access, a process has read access"); + CeedCheck(vec->state % 2 == 0, CeedVectorReturnCeed(vec), CEED_ERROR_ACCESS, + "Cannot grant CeedVector array access, the access lock is already in use"); + CeedCheck(vec->num_readers == 0, CeedVectorReturnCeed(vec), CEED_ERROR_ACCESS, "Cannot grant CeedVector array access, a process has read access"); if (vec->SetValue) { CeedCall(vec->SetValue(vec, value)); @@ -365,11 +366,9 @@ int CeedVectorSetValue(CeedVector vec, CeedScalar value) { @ref User **/ int CeedVectorSetValueStrided(CeedVector vec, CeedSize start, CeedInt step, CeedScalar value) { - Ceed ceed; - - CeedCall(CeedVectorGetCeed(vec, &ceed)); - CeedCheck(vec->state % 2 == 0, ceed, CEED_ERROR_ACCESS, "Cannot grant CeedVector array access, the access lock is already in use"); - CeedCheck(vec->num_readers == 0, ceed, CEED_ERROR_ACCESS, "Cannot grant CeedVector array access, a process has read access"); + CeedCheck(vec->state % 2 == 0, CeedVectorReturnCeed(vec), CEED_ERROR_ACCESS, + "Cannot grant CeedVector array access, the access lock is already in use"); + CeedCheck(vec->num_readers == 0, CeedVectorReturnCeed(vec), CEED_ERROR_ACCESS, "Cannot grant CeedVector array access, a process has read access"); if (vec->SetValueStrided) { CeedCall(vec->SetValueStrided(vec, start, step, value)); @@ -438,22 +437,20 @@ int CeedVectorSyncArray(CeedVector vec, CeedMemType mem_type) { int CeedVectorTakeArray(CeedVector vec, CeedMemType mem_type, CeedScalar **array) { CeedSize length; CeedScalar *temp_array = NULL; - Ceed ceed; - CeedCall(CeedVectorGetCeed(vec, &ceed)); - CeedCheck(vec->state % 2 == 0, ceed, CEED_ERROR_ACCESS, "Cannot take CeedVector array, the access lock is already in use"); - CeedCheck(vec->num_readers == 0, ceed, CEED_ERROR_ACCESS, "Cannot take CeedVector array, a process has read access"); + CeedCheck(vec->state % 2 == 0, CeedVectorReturnCeed(vec), CEED_ERROR_ACCESS, "Cannot take CeedVector array, the access lock is already in use"); + CeedCheck(vec->num_readers == 0, CeedVectorReturnCeed(vec), CEED_ERROR_ACCESS, "Cannot take CeedVector array, a process has read access"); CeedCall(CeedVectorGetLength(vec, &length)); if (length > 0) { bool has_borrowed_array_of_type = true, has_valid_array = true; CeedCall(CeedVectorHasBorrowedArrayOfType(vec, mem_type, &has_borrowed_array_of_type)); - CeedCheck(has_borrowed_array_of_type, ceed, CEED_ERROR_BACKEND, "CeedVector has no borrowed %s array, must set array with CeedVectorSetArray", - CeedMemTypes[mem_type]); + CeedCheck(has_borrowed_array_of_type, CeedVectorReturnCeed(vec), CEED_ERROR_BACKEND, + "CeedVector has no borrowed %s array, must set array with CeedVectorSetArray", CeedMemTypes[mem_type]); CeedCall(CeedVectorHasValidArray(vec, &has_valid_array)); - CeedCheck(has_valid_array, ceed, CEED_ERROR_BACKEND, + CeedCheck(has_valid_array, CeedVectorReturnCeed(vec), CEED_ERROR_BACKEND, "CeedVector has no valid data to take, must set data with CeedVectorSetValue or CeedVectorSetArray"); CeedCall(vec->TakeArray(vec, mem_type, &temp_array)); @@ -481,19 +478,18 @@ int CeedVectorTakeArray(CeedVector vec, CeedMemType mem_type, CeedScalar **array **/ int CeedVectorGetArray(CeedVector vec, CeedMemType mem_type, CeedScalar **array) { CeedSize length; - Ceed ceed; - CeedCall(CeedVectorGetCeed(vec, &ceed)); - CeedCheck(vec->GetArray, ceed, CEED_ERROR_UNSUPPORTED, "Backend does not support GetArray"); - CeedCheck(vec->state % 2 == 0, ceed, CEED_ERROR_ACCESS, "Cannot grant CeedVector array access, the access lock is already in use"); - CeedCheck(vec->num_readers == 0, ceed, CEED_ERROR_ACCESS, "Cannot grant CeedVector array access, a process has read access"); + CeedCheck(vec->GetArray, CeedVectorReturnCeed(vec), CEED_ERROR_UNSUPPORTED, "Backend does not support GetArray"); + CeedCheck(vec->state % 2 == 0, CeedVectorReturnCeed(vec), CEED_ERROR_ACCESS, + "Cannot grant CeedVector array access, the access lock is already in use"); + CeedCheck(vec->num_readers == 0, CeedVectorReturnCeed(vec), CEED_ERROR_ACCESS, "Cannot grant CeedVector array access, a process has read access"); CeedCall(CeedVectorGetLength(vec, &length)); if (length > 0) { bool has_valid_array = true; CeedCall(CeedVectorHasValidArray(vec, &has_valid_array)); - CeedCheck(has_valid_array, ceed, CEED_ERROR_BACKEND, + CeedCheck(has_valid_array, CeedVectorReturnCeed(vec), CEED_ERROR_BACKEND, "CeedVector has no valid data to read, must set data with CeedVectorSetValue or CeedVectorSetArray"); CeedCall(vec->GetArray(vec, mem_type, array)); @@ -520,18 +516,17 @@ int CeedVectorGetArray(CeedVector vec, CeedMemType mem_type, CeedScalar **array) **/ int CeedVectorGetArrayRead(CeedVector vec, CeedMemType mem_type, const CeedScalar **array) { CeedSize length; - Ceed ceed; - CeedCall(CeedVectorGetCeed(vec, &ceed)); - CeedCheck(vec->GetArrayRead, ceed, CEED_ERROR_UNSUPPORTED, "Backend does not support GetArrayRead"); - CeedCheck(vec->state % 2 == 0, ceed, CEED_ERROR_ACCESS, "Cannot grant CeedVector read-only array access, the access lock is already in use"); + CeedCheck(vec->GetArrayRead, CeedVectorReturnCeed(vec), CEED_ERROR_UNSUPPORTED, "Backend does not support GetArrayRead"); + CeedCheck(vec->state % 2 == 0, CeedVectorReturnCeed(vec), CEED_ERROR_ACCESS, + "Cannot grant CeedVector read-only array access, the access lock is already in use"); CeedCall(CeedVectorGetLength(vec, &length)); if (length > 0) { bool has_valid_array = true; CeedCall(CeedVectorHasValidArray(vec, &has_valid_array)); - CeedCheck(has_valid_array, ceed, CEED_ERROR_BACKEND, + CeedCheck(has_valid_array, CeedVectorReturnCeed(vec), CEED_ERROR_BACKEND, "CeedVector has no valid data to read, must set data with CeedVectorSetValue or CeedVectorSetArray"); CeedCall(vec->GetArrayRead(vec, mem_type, array)); @@ -558,12 +553,11 @@ int CeedVectorGetArrayRead(CeedVector vec, CeedMemType mem_type, const CeedScala **/ int CeedVectorGetArrayWrite(CeedVector vec, CeedMemType mem_type, CeedScalar **array) { CeedSize length; - Ceed ceed; - CeedCall(CeedVectorGetCeed(vec, &ceed)); - CeedCheck(vec->GetArrayWrite, ceed, CEED_ERROR_UNSUPPORTED, "Backend does not support CeedVectorGetArrayWrite"); - CeedCheck(vec->state % 2 == 0, ceed, CEED_ERROR_ACCESS, "Cannot grant CeedVector array access, the access lock is already in use"); - CeedCheck(vec->num_readers == 0, ceed, CEED_ERROR_ACCESS, "Cannot grant CeedVector array access, a process has read access"); + CeedCheck(vec->GetArrayWrite, CeedVectorReturnCeed(vec), CEED_ERROR_UNSUPPORTED, "Backend does not support CeedVectorGetArrayWrite"); + CeedCheck(vec->state % 2 == 0, CeedVectorReturnCeed(vec), CEED_ERROR_ACCESS, + "Cannot grant CeedVector array access, the access lock is already in use"); + CeedCheck(vec->num_readers == 0, CeedVectorReturnCeed(vec), CEED_ERROR_ACCESS, "Cannot grant CeedVector array access, a process has read access"); CeedCall(CeedVectorGetLength(vec, &length)); if (length > 0) { @@ -730,27 +724,36 @@ int CeedVectorAXPY(CeedVector y, CeedScalar alpha, CeedVector x) { CeedSize length_x, length_y; CeedScalar *y_array = NULL; CeedScalar const *x_array = NULL; - Ceed ceed, ceed_parent_x, ceed_parent_y; - CeedCall(CeedVectorGetCeed(y, &ceed)); CeedCall(CeedVectorGetLength(y, &length_y)); CeedCall(CeedVectorGetLength(x, &length_x)); - CeedCheck(length_x == length_y, ceed, CEED_ERROR_UNSUPPORTED, + CeedCheck(length_x == length_y, CeedVectorReturnCeed(y), CEED_ERROR_UNSUPPORTED, "Cannot add vector of different lengths." " x length: %" CeedSize_FMT " y length: %" CeedSize_FMT, length_x, length_y); - CeedCheck(x != y, ceed, CEED_ERROR_UNSUPPORTED, "Cannot use same vector for x and y in CeedVectorAXPY"); + CeedCheck(x != y, CeedVectorReturnCeed(y), CEED_ERROR_UNSUPPORTED, "Cannot use same vector for x and y in CeedVectorAXPY"); CeedCall(CeedVectorHasValidArray(x, &has_valid_array_x)); - CeedCheck(has_valid_array_x, ceed, CEED_ERROR_BACKEND, + CeedCheck(has_valid_array_x, CeedVectorReturnCeed(y), CEED_ERROR_BACKEND, "CeedVector x has no valid data, must set data with CeedVectorSetValue or CeedVectorSetArray"); CeedCall(CeedVectorHasValidArray(y, &has_valid_array_y)); - CeedCheck(has_valid_array_y, ceed, CEED_ERROR_BACKEND, + CeedCheck(has_valid_array_y, CeedVectorReturnCeed(y), CEED_ERROR_BACKEND, "CeedVector y has no valid data, must set data with CeedVectorSetValue or CeedVectorSetArray"); - CeedCall(CeedGetParent(x->ceed, &ceed_parent_x)); - CeedCall(CeedGetParent(y->ceed, &ceed_parent_y)); - CeedCheck(ceed_parent_x == ceed_parent_y, ceed, CEED_ERROR_INCOMPATIBLE, "Vectors x and y must be created by the same Ceed context"); + { + Ceed ceed_x, ceed_y, ceed_parent_x, ceed_parent_y; + + CeedCall(CeedVectorGetCeed(y, &ceed_y)); + CeedCall(CeedVectorGetCeed(x, &ceed_x)); + CeedCall(CeedGetParent(ceed_x, &ceed_parent_x)); + CeedCall(CeedGetParent(ceed_y, &ceed_parent_y)); + CeedCall(CeedDestroy(&ceed_x)); + CeedCall(CeedDestroy(&ceed_y)); + CeedCheck(ceed_parent_x == ceed_parent_y, CeedVectorReturnCeed(y), CEED_ERROR_INCOMPATIBLE, + "Vectors x and y must be created by the same Ceed context"); + CeedCall(CeedDestroy(&ceed_parent_x)); + CeedCall(CeedDestroy(&ceed_parent_y)); + } // Return early for empty vectors if (length_y == 0) return CEED_ERROR_SUCCESS; @@ -792,28 +795,36 @@ int CeedVectorAXPBY(CeedVector y, CeedScalar alpha, CeedScalar beta, CeedVector CeedSize length_x, length_y; CeedScalar *y_array = NULL; CeedScalar const *x_array = NULL; - Ceed ceed, ceed_parent_x, ceed_parent_y; - - CeedCall(CeedVectorGetCeed(y, &ceed)); CeedCall(CeedVectorGetLength(y, &length_y)); CeedCall(CeedVectorGetLength(x, &length_x)); - CeedCheck(length_x == length_y, ceed, CEED_ERROR_UNSUPPORTED, + CeedCheck(length_x == length_y, CeedVectorReturnCeed(y), CEED_ERROR_UNSUPPORTED, "Cannot add vector of different lengths." " x length: %" CeedSize_FMT " y length: %" CeedSize_FMT, length_x, length_y); - CeedCheck(x != y, ceed, CEED_ERROR_UNSUPPORTED, "Cannot use same vector for x and y in CeedVectorAXPBY"); + CeedCheck(x != y, CeedVectorReturnCeed(y), CEED_ERROR_UNSUPPORTED, "Cannot use same vector for x and y in CeedVectorAXPBY"); CeedCall(CeedVectorHasValidArray(x, &has_valid_array_x)); - CeedCheck(has_valid_array_x, ceed, CEED_ERROR_BACKEND, + CeedCheck(has_valid_array_x, CeedVectorReturnCeed(y), CEED_ERROR_BACKEND, "CeedVector x has no valid data, must set data with CeedVectorSetValue or CeedVectorSetArray"); CeedCall(CeedVectorHasValidArray(y, &has_valid_array_y)); - CeedCheck(has_valid_array_y, ceed, CEED_ERROR_BACKEND, + CeedCheck(has_valid_array_y, CeedVectorReturnCeed(y), CEED_ERROR_BACKEND, "CeedVector y has no valid data, must set data with CeedVectorSetValue or CeedVectorSetArray"); - CeedCall(CeedGetParent(x->ceed, &ceed_parent_x)); - CeedCall(CeedGetParent(y->ceed, &ceed_parent_y)); - CeedCheck(ceed_parent_x == ceed_parent_y, ceed, CEED_ERROR_INCOMPATIBLE, "Vectors x and y must be created by the same Ceed context"); + { + Ceed ceed_x, ceed_y, ceed_parent_x, ceed_parent_y; + + CeedCall(CeedVectorGetCeed(y, &ceed_y)); + CeedCall(CeedVectorGetCeed(x, &ceed_x)); + CeedCall(CeedGetParent(ceed_x, &ceed_parent_x)); + CeedCall(CeedGetParent(ceed_y, &ceed_parent_y)); + CeedCall(CeedDestroy(&ceed_x)); + CeedCall(CeedDestroy(&ceed_y)); + CeedCheck(ceed_parent_x == ceed_parent_y, CeedVectorReturnCeed(y), CEED_ERROR_INCOMPATIBLE, + "Vectors x and y must be created by the same Ceed context"); + CeedCall(CeedDestroy(&ceed_parent_x)); + CeedCall(CeedDestroy(&ceed_parent_y)); + } // Return early for empty vectors if (length_y == 0) return CEED_ERROR_SUCCESS; @@ -856,28 +867,39 @@ int CeedVectorPointwiseMult(CeedVector w, CeedVector x, CeedVector y) { CeedScalar *w_array = NULL; CeedScalar const *x_array = NULL, *y_array = NULL; CeedSize length_w, length_x, length_y; - Ceed ceed, ceed_parent_w, ceed_parent_x, ceed_parent_y; - CeedCall(CeedVectorGetCeed(w, &ceed)); CeedCall(CeedVectorGetLength(w, &length_w)); CeedCall(CeedVectorGetLength(x, &length_x)); CeedCall(CeedVectorGetLength(y, &length_y)); - CeedCheck(length_x >= length_w && length_y >= length_w, ceed, CEED_ERROR_UNSUPPORTED, + CeedCheck(length_x >= length_w && length_y >= length_w, CeedVectorReturnCeed(w), CEED_ERROR_UNSUPPORTED, "Cannot pointwise multiply vectors of incompatible lengths." " w length: %" CeedSize_FMT " x length: %" CeedSize_FMT " y length: %" CeedSize_FMT, length_w, length_x, length_y); - CeedCall(CeedGetParent(w->ceed, &ceed_parent_w)); - CeedCall(CeedGetParent(x->ceed, &ceed_parent_x)); - CeedCall(CeedGetParent(y->ceed, &ceed_parent_y)); - CeedCheck(ceed_parent_w == ceed_parent_x && ceed_parent_w == ceed_parent_y, ceed, CEED_ERROR_INCOMPATIBLE, - "Vectors w, x, and y must be created by the same Ceed context"); + { + Ceed ceed_w, ceed_x, ceed_y, ceed_parent_w, ceed_parent_x, ceed_parent_y; + + CeedCall(CeedVectorGetCeed(w, &ceed_w)); + CeedCall(CeedVectorGetCeed(x, &ceed_x)); + CeedCall(CeedVectorGetCeed(y, &ceed_y)); + CeedCall(CeedGetParent(ceed_w, &ceed_parent_w)); + CeedCall(CeedGetParent(ceed_x, &ceed_parent_x)); + CeedCall(CeedGetParent(ceed_y, &ceed_parent_y)); + CeedCall(CeedDestroy(&ceed_w)); + CeedCall(CeedDestroy(&ceed_x)); + CeedCall(CeedDestroy(&ceed_y)); + CeedCheck(ceed_parent_w == ceed_parent_x && ceed_parent_w == ceed_parent_y, CeedVectorReturnCeed(w), CEED_ERROR_INCOMPATIBLE, + "Vectors w, x, and y must be created by the same Ceed context"); + CeedCall(CeedDestroy(&ceed_parent_w)); + CeedCall(CeedDestroy(&ceed_parent_x)); + CeedCall(CeedDestroy(&ceed_parent_y)); + } CeedCall(CeedVectorHasValidArray(x, &has_valid_array_x)); - CeedCheck(has_valid_array_x, ceed, CEED_ERROR_BACKEND, + CeedCheck(has_valid_array_x, CeedVectorReturnCeed(w), CEED_ERROR_BACKEND, "CeedVector x has no valid data, must set data with CeedVectorSetValue or CeedVectorSetArray"); CeedCall(CeedVectorHasValidArray(y, &has_valid_array_y)); - CeedCheck(has_valid_array_y, ceed, CEED_ERROR_BACKEND, + CeedCheck(has_valid_array_y, CeedVectorReturnCeed(w), CEED_ERROR_BACKEND, "CeedVector y has no valid data, must set data with CeedVectorSetValue or CeedVectorSetArray"); // Return early for empty vectors @@ -933,15 +955,13 @@ int CeedVectorReciprocal(CeedVector vec) { bool has_valid_array = true; CeedSize length; CeedScalar *array; - Ceed ceed; - CeedCall(CeedVectorGetCeed(vec, &ceed)); CeedCall(CeedVectorHasValidArray(vec, &has_valid_array)); - CeedCheck(has_valid_array, ceed, CEED_ERROR_BACKEND, + CeedCheck(has_valid_array, CeedVectorReturnCeed(vec), CEED_ERROR_BACKEND, "CeedVector has no valid data to compute reciprocal, must set data with CeedVectorSetValue or CeedVectorSetArray"); // Check if vector data set - CeedCheck(vec->state > 0, ceed, CEED_ERROR_INCOMPLETE, "CeedVector must have data set to take reciprocal"); + CeedCheck(vec->state > 0, CeedVectorReturnCeed(vec), CEED_ERROR_INCOMPLETE, "CeedVector must have data set to take reciprocal"); // Return early for empty vector CeedCall(CeedVectorGetLength(vec, &length)); @@ -1032,7 +1052,8 @@ int CeedVectorView(CeedVector vec, const char *fp_fmt, FILE *stream) { @ref Advanced **/ int CeedVectorGetCeed(CeedVector vec, Ceed *ceed) { - *ceed = CeedVectorReturnCeed(vec); + *ceed = NULL; + CeedCall(CeedReferenceCopy(CeedVectorReturnCeed(vec), ceed)); return CEED_ERROR_SUCCESS; } diff --git a/interface/ceed.c b/interface/ceed.c index 5579cca242..a25e97a6b7 100644 --- a/interface/ceed.c +++ b/interface/ceed.c @@ -517,7 +517,8 @@ int CeedGetParent(Ceed ceed, Ceed *parent) { CeedCall(CeedGetParent(ceed->parent, parent)); return CEED_ERROR_SUCCESS; } - *parent = ceed; + *parent = NULL; + CeedCall(CeedReferenceCopy(ceed, parent)); return CEED_ERROR_SUCCESS; } @@ -532,7 +533,8 @@ int CeedGetParent(Ceed ceed, Ceed *parent) { @ref Backend **/ int CeedGetDelegate(Ceed ceed, Ceed *delegate) { - *delegate = ceed->delegate; + *delegate = NULL; + if (ceed->delegate) CeedCall(CeedReferenceCopy(ceed->delegate, delegate)); return CEED_ERROR_SUCCESS; } @@ -550,7 +552,7 @@ int CeedGetDelegate(Ceed ceed, Ceed *delegate) { @ref Backend **/ int CeedSetDelegate(Ceed ceed, Ceed delegate) { - ceed->delegate = delegate; + CeedCall(CeedReferenceCopy(delegate, &ceed->delegate)); delegate->parent = ceed; return CEED_ERROR_SUCCESS; } @@ -570,7 +572,8 @@ int CeedGetObjectDelegate(Ceed ceed, Ceed *delegate, const char *obj_name) { // Check for object delegate for (CeedInt i = 0; i < ceed->obj_delegate_count; i++) { if (!strcmp(obj_name, ceed->obj_delegates->obj_name)) { - *delegate = ceed->obj_delegates->delegate; + *delegate = NULL; + CeedCall(CeedReferenceCopy(ceed->obj_delegates->delegate, delegate)); return CEED_ERROR_SUCCESS; } } @@ -607,7 +610,7 @@ int CeedSetObjectDelegate(Ceed ceed, Ceed delegate, const char *obj_name) { ceed->obj_delegate_count++; // Set object delegate - ceed->obj_delegates[count].delegate = delegate; + CeedCall(CeedReferenceCopy(delegate, &ceed->obj_delegates[count].delegate)); CeedCall(CeedStringAllocCopy(obj_name, &ceed->obj_delegates[count].obj_name)); // Set delegate parent @@ -679,7 +682,8 @@ int CeedGetOperatorFallbackCeed(Ceed ceed, Ceed *fallback_ceed) { CeedCall(CeedRestoreJitDefines(ceed, &jit_defines)); } } - *fallback_ceed = ceed->op_fallback_ceed; + *fallback_ceed = NULL; + if (ceed->op_fallback_ceed) CeedCall(CeedReferenceCopy(ceed->op_fallback_ceed, fallback_ceed)); return CEED_ERROR_SUCCESS; } @@ -903,6 +907,7 @@ int CeedGetJitSourceRoots(Ceed ceed, CeedInt *num_source_roots, const char ***ji *num_source_roots = ceed_parent->num_jit_source_roots; *jit_source_roots = (const char **)ceed_parent->jit_source_roots; ceed_parent->num_jit_source_roots_readers++; + CeedCall(CeedDestroy(&ceed_parent)); return CEED_ERROR_SUCCESS; } @@ -922,6 +927,7 @@ int CeedRestoreJitSourceRoots(Ceed ceed, const char ***jit_source_roots) { CeedCall(CeedGetParent(ceed, &ceed_parent)); *jit_source_roots = NULL; ceed_parent->num_jit_source_roots_readers--; + CeedCall(CeedDestroy(&ceed_parent)); return CEED_ERROR_SUCCESS; } @@ -945,6 +951,7 @@ int CeedGetJitDefines(Ceed ceed, CeedInt *num_defines, const char ***jit_defines *num_defines = ceed_parent->num_jit_defines; *jit_defines = (const char **)ceed_parent->jit_defines; ceed_parent->num_jit_defines_readers++; + CeedCall(CeedDestroy(&ceed_parent)); return CEED_ERROR_SUCCESS; } @@ -964,6 +971,7 @@ int CeedRestoreJitDefines(Ceed ceed, const char ***jit_defines) { CeedCall(CeedGetParent(ceed, &ceed_parent)); *jit_defines = NULL; ceed_parent->num_jit_defines_readers--; + CeedCall(CeedDestroy(&ceed_parent)); return CEED_ERROR_SUCCESS; } @@ -1251,6 +1259,7 @@ int CeedSetStream(Ceed ceed, void *handle) { if (delegate) CeedCall(CeedSetStream(delegate, handle)); else return CeedError(ceed, CEED_ERROR_UNSUPPORTED, "Backend does not support setting stream"); + CeedCall(CeedDestroy(&delegate)); } return CEED_ERROR_SUCCESS; } @@ -1314,6 +1323,7 @@ int CeedGetPreferredMemType(Ceed ceed, CeedMemType *mem_type) { } else { *mem_type = CEED_MEM_HOST; } + CeedCall(CeedDestroy(&delegate)); } return CEED_ERROR_SUCCESS; } @@ -1360,6 +1370,7 @@ int CeedAddJitSourceRoot(Ceed ceed, const char *jit_source_root) { CeedCall(CeedCalloc(path_length + 1, &ceed_parent->jit_source_roots[index])); memcpy(ceed_parent->jit_source_roots[index], jit_source_root, path_length); ceed_parent->num_jit_source_roots++; + CeedCall(CeedDestroy(&ceed_parent)); return CEED_ERROR_SUCCESS; } @@ -1390,6 +1401,7 @@ int CeedAddJitDefine(Ceed ceed, const char *jit_define) { CeedCall(CeedCalloc(define_length + 1, &ceed_parent->jit_defines[index])); memcpy(ceed_parent->jit_defines[index], jit_define, define_length); ceed_parent->num_jit_defines++; + CeedCall(CeedDestroy(&ceed_parent)); return CEED_ERROR_SUCCESS; } diff --git a/tests/t409-qfunction.c b/tests/t409-qfunction.c index 44029cc797..5f17a5614a 100644 --- a/tests/t409-qfunction.c +++ b/tests/t409-qfunction.c @@ -74,6 +74,7 @@ int main(int argc, char **argv) { CeedQFunctionContextRestoreData(ctx, &ctx_data_new); is_writable = false; CeedQFunctionSetContextWritable(qf, is_writable); + { in[0] = u; out[0] = v; From c11e12f4f794f6cb6040b1c3c2be08186369c43d Mon Sep 17 00:00:00 2001 From: Jeremy L Thompson Date: Wed, 23 Oct 2024 11:36:08 -0600 Subject: [PATCH 2/7] qf - require CeedOpGetQF to be Destroyed --- backends/blocked/ceed-blocked-operator.c | 21 ++++++++++-------- .../cuda-gen/ceed-cuda-gen-operator-build.cpp | 1 + backends/cuda-gen/ceed-cuda-gen-operator.c | 22 +++++++++---------- backends/cuda-ref/ceed-cuda-ref-operator.c | 11 +++++++++- .../hip-gen/ceed-hip-gen-operator-build.cpp | 1 + backends/hip-gen/ceed-hip-gen-operator.c | 20 ++++++++--------- backends/hip-ref/ceed-hip-ref-operator.c | 11 +++++++++- backends/opt/ceed-opt-operator.c | 18 +++++++++------ backends/ref/ceed-ref-operator.c | 22 +++++++++++++------ .../ceed-sycl-gen-operator-build.sycl.cpp | 1 + .../sycl-gen/ceed-sycl-gen-operator.sycl.cpp | 22 +++++++++---------- .../sycl-ref/ceed-sycl-ref-operator.sycl.cpp | 11 +++++++--- interface/ceed-operator.c | 17 +++++++++++++- interface/ceed-preconditioning.c | 2 ++ 14 files changed, 119 insertions(+), 61 deletions(-) diff --git a/backends/blocked/ceed-blocked-operator.c b/backends/blocked/ceed-blocked-operator.c index 2f7b0e6dad..11951e19ba 100644 --- a/backends/blocked/ceed-blocked-operator.c +++ b/backends/blocked/ceed-blocked-operator.c @@ -261,6 +261,7 @@ static int CeedOperatorSetup_Blocked(CeedOperator op) { } CeedCallBackend(CeedOperatorSetSetupDone(op)); + CeedCallBackend(CeedQFunctionDestroy(&qf)); return CEED_ERROR_SUCCESS; } @@ -438,23 +439,23 @@ static int CeedOperatorApplyAdd_Blocked(CeedOperator op, CeedVector in_vec, Ceed CeedOperatorField *op_input_fields, *op_output_fields; CeedOperator_Blocked *impl; - CeedCallBackend(CeedOperatorGetData(op, &impl)); - CeedCallBackend(CeedOperatorGetNumElements(op, &num_elem)); - CeedCallBackend(CeedOperatorGetNumQuadraturePoints(op, &Q)); - CeedCallBackend(CeedOperatorGetQFunction(op, &qf)); - CeedCallBackend(CeedOperatorGetFields(op, &num_input_fields, &op_input_fields, &num_output_fields, &op_output_fields)); - CeedCallBackend(CeedQFunctionGetFields(qf, NULL, &qf_input_fields, NULL, &qf_output_fields)); - const CeedInt num_blocks = (num_elem / block_size) + !!(num_elem % block_size); - // Setup CeedCallBackend(CeedOperatorSetup_Blocked(op)); + CeedCallBackend(CeedOperatorGetData(op, &impl)); + // Restriction only operator if (impl->is_identity_rstr_op) { CeedCallBackend(CeedElemRestrictionApply(impl->block_rstr[0], CEED_NOTRANSPOSE, in_vec, impl->e_vecs_full[0], request)); CeedCallBackend(CeedElemRestrictionApply(impl->block_rstr[1], CEED_TRANSPOSE, impl->e_vecs_full[0], out_vec, request)); return CEED_ERROR_SUCCESS; } + CeedCallBackend(CeedOperatorGetNumElements(op, &num_elem)); + CeedCallBackend(CeedOperatorGetNumQuadraturePoints(op, &Q)); + CeedCallBackend(CeedOperatorGetQFunction(op, &qf)); + CeedCallBackend(CeedOperatorGetFields(op, &num_input_fields, &op_input_fields, &num_output_fields, &op_output_fields)); + CeedCallBackend(CeedQFunctionGetFields(qf, NULL, &qf_input_fields, NULL, &qf_output_fields)); + const CeedInt num_blocks = (num_elem / block_size) + !!(num_elem % block_size); // Input Evecs and Restriction CeedCallBackend(CeedOperatorSetupInputs_Blocked(num_input_fields, qf_input_fields, op_input_fields, in_vec, false, e_data_full, impl, request)); @@ -514,6 +515,7 @@ static int CeedOperatorApplyAdd_Blocked(CeedOperator op, CeedVector in_vec, Ceed // Restore input arrays CeedCallBackend(CeedOperatorRestoreInputs_Blocked(num_input_fields, qf_input_fields, op_input_fields, false, e_data_full, impl)); + CeedCallBackend(CeedQFunctionDestroy(&qf)); return CEED_ERROR_SUCCESS; } @@ -708,10 +710,11 @@ static inline int CeedOperatorLinearAssembleQFunctionCore_Blocked(CeedOperator o CeedCallBackend(CeedOperatorRestoreInputs_Blocked(num_input_fields, qf_input_fields, op_input_fields, true, e_data_full, impl)); // Output blocked restriction - CeedCallBackend(CeedDestroy(&ceed)); CeedCallBackend(CeedVectorRestoreArray(l_vec, &l_vec_array)); CeedCallBackend(CeedVectorSetValue(*assembled, 0.0)); CeedCallBackend(CeedElemRestrictionApply(block_rstr, CEED_TRANSPOSE, l_vec, *assembled, request)); + CeedCallBackend(CeedDestroy(&ceed)); + CeedCallBackend(CeedQFunctionDestroy(&qf)); return CEED_ERROR_SUCCESS; } diff --git a/backends/cuda-gen/ceed-cuda-gen-operator-build.cpp b/backends/cuda-gen/ceed-cuda-gen-operator-build.cpp index 3103bcbfb7..96277aa8cf 100644 --- a/backends/cuda-gen/ceed-cuda-gen-operator-build.cpp +++ b/backends/cuda-gen/ceed-cuda-gen-operator-build.cpp @@ -903,6 +903,7 @@ extern "C" int CeedOperatorBuildKernel_Cuda_gen(CeedOperator op) { CeedCallBackend(CeedGetKernel_Cuda(ceed, data->module, operator_name.c_str(), &data->op)); CeedCallBackend(CeedOperatorSetSetupDone(op)); CeedCallBackend(CeedDestroy(&ceed)); + CeedCallBackend(CeedQFunctionDestroy(&qf)); return CEED_ERROR_SUCCESS; } diff --git a/backends/cuda-gen/ceed-cuda-gen-operator.c b/backends/cuda-gen/ceed-cuda-gen-operator.c index 7ea1002b21..d999dc8caa 100644 --- a/backends/cuda-gen/ceed-cuda-gen-operator.c +++ b/backends/cuda-gen/ceed-cuda-gen-operator.c @@ -103,15 +103,6 @@ static int CeedOperatorApplyAdd_Cuda_gen(CeedOperator op, CeedVector input_vec, CeedOperatorField *op_input_fields, *op_output_fields; CeedOperator_Cuda_gen *data; - CeedCallBackend(CeedOperatorGetCeed(op, &ceed)); - CeedCallBackend(CeedGetData(ceed, &cuda_data)); - CeedCallBackend(CeedOperatorGetData(op, &data)); - CeedCallBackend(CeedOperatorGetQFunction(op, &qf)); - CeedCallBackend(CeedQFunctionGetData(qf, &qf_data)); - CeedCallBackend(CeedOperatorGetNumElements(op, &num_elem)); - CeedCallBackend(CeedOperatorGetFields(op, &num_input_fields, &op_input_fields, &num_output_fields, &op_output_fields)); - CeedCallBackend(CeedQFunctionGetFields(qf, NULL, &qf_input_fields, NULL, &qf_output_fields)); - // Check for tensor-product bases { bool has_tensor_bases; @@ -121,14 +112,22 @@ static int CeedOperatorApplyAdd_Cuda_gen(CeedOperator op, CeedVector input_vec, if (!has_tensor_bases) { CeedOperator op_fallback; - CeedDebug256(ceed, CEED_DEBUG_COLOR_SUCCESS, "Falling back to /gpu/cuda/ref CeedOperator due to non-tensor bases"); - CeedCallBackend(CeedDestroy(&ceed)); + CeedDebug256(CeedOperatorReturnCeed(op), CEED_DEBUG_COLOR_SUCCESS, "Falling back to /gpu/cuda/ref CeedOperator due to non-tensor bases"); CeedCallBackend(CeedOperatorGetFallback(op, &op_fallback)); CeedCallBackend(CeedOperatorApplyAdd(op_fallback, input_vec, output_vec, request)); return CEED_ERROR_SUCCESS; } } + CeedCallBackend(CeedOperatorGetCeed(op, &ceed)); + CeedCallBackend(CeedGetData(ceed, &cuda_data)); + CeedCallBackend(CeedOperatorGetData(op, &data)); + CeedCallBackend(CeedOperatorGetQFunction(op, &qf)); + CeedCallBackend(CeedQFunctionGetData(qf, &qf_data)); + CeedCallBackend(CeedOperatorGetNumElements(op, &num_elem)); + CeedCallBackend(CeedOperatorGetFields(op, &num_input_fields, &op_input_fields, &num_output_fields, &op_output_fields)); + CeedCallBackend(CeedQFunctionGetFields(qf, NULL, &qf_input_fields, NULL, &qf_output_fields)); + // Creation of the operator CeedCallBackend(CeedOperatorBuildKernel_Cuda_gen(op)); @@ -253,6 +252,7 @@ static int CeedOperatorApplyAdd_Cuda_gen(CeedOperator op, CeedVector input_vec, // Restore context data CeedCallBackend(CeedQFunctionRestoreInnerContextData(qf, &qf_data->d_c)); CeedCallBackend(CeedDestroy(&ceed)); + CeedCallBackend(CeedQFunctionDestroy(&qf)); return CEED_ERROR_SUCCESS; } diff --git a/backends/cuda-ref/ceed-cuda-ref-operator.c b/backends/cuda-ref/ceed-cuda-ref-operator.c index ac7b1cad03..fe873c74b5 100644 --- a/backends/cuda-ref/ceed-cuda-ref-operator.c +++ b/backends/cuda-ref/ceed-cuda-ref-operator.c @@ -354,6 +354,7 @@ static int CeedOperatorSetup_Cuda(CeedOperator op) { } } CeedCallBackend(CeedOperatorSetSetupDone(op)); + CeedCallBackend(CeedQFunctionDestroy(&qf)); return CEED_ERROR_SUCCESS; } @@ -601,6 +602,7 @@ static int CeedOperatorApplyAdd_Cuda(CeedOperator op, CeedVector in_vec, CeedVec // Return work vector CeedCallBackend(CeedRestoreWorkVector(ceed, &active_e_vec)); CeedCallBackend(CeedDestroy(&ceed)); + CeedCallBackend(CeedQFunctionDestroy(&qf)); return CEED_ERROR_SUCCESS; } @@ -739,6 +741,7 @@ static int CeedOperatorSetupAtPoints_Cuda(CeedOperator op) { } } CeedCallBackend(CeedOperatorSetSetupDone(op)); + CeedCallBackend(CeedQFunctionDestroy(&qf)); return CEED_ERROR_SUCCESS; } @@ -927,6 +930,7 @@ static int CeedOperatorApplyAddAtPoints_Cuda(CeedOperator op, CeedVector in_vec, // Restore work vector CeedCallBackend(CeedRestoreWorkVector(ceed, &active_e_vec)); CeedCallBackend(CeedDestroy(&ceed)); + CeedCallBackend(CeedQFunctionDestroy(&qf)); return CEED_ERROR_SUCCESS; } @@ -1068,9 +1072,10 @@ static inline int CeedOperatorLinearAssembleQFunctionCore_Cuda(CeedOperator op, } // Restore output + CeedCallBackend(CeedVectorRestoreArray(*assembled, &assembled_array)); CeedCallBackend(CeedDestroy(&ceed)); CeedCallBackend(CeedDestroy(&ceed_parent)); - CeedCallBackend(CeedVectorRestoreArray(*assembled, &assembled_array)); + CeedCallBackend(CeedQFunctionDestroy(&qf)); return CEED_ERROR_SUCCESS; } @@ -1274,6 +1279,7 @@ static inline int CeedOperatorAssembleDiagonalSetup_Cuda(CeedOperator op) { CeedCallBackend(CeedDestroy(&ceed)); CeedCallBackend(CeedBasisDestroy(&basis_in)); CeedCallBackend(CeedBasisDestroy(&basis_out)); + CeedCallBackend(CeedQFunctionDestroy(&qf)); return CEED_ERROR_SUCCESS; } @@ -1360,6 +1366,7 @@ static inline int CeedOperatorAssembleDiagonalSetupCompile_Cuda(CeedOperator op, CeedCallBackend(CeedDestroy(&ceed)); CeedCallBackend(CeedBasisDestroy(&basis_in)); CeedCallBackend(CeedBasisDestroy(&basis_out)); + CeedCallBackend(CeedQFunctionDestroy(&qf)); return CEED_ERROR_SUCCESS; } @@ -1664,6 +1671,7 @@ static int CeedSingleOperatorAssembleSetup_Cuda(CeedOperator op, CeedInt use_cee CeedCallBackend(CeedElemRestrictionDestroy(&rstr_out)); CeedCallBackend(CeedBasisDestroy(&basis_in)); CeedCallBackend(CeedBasisDestroy(&basis_out)); + CeedCallBackend(CeedQFunctionDestroy(&qf)); return CEED_ERROR_SUCCESS; } @@ -2041,6 +2049,7 @@ static int CeedOperatorLinearAssembleAddDiagonalAtPoints_Cuda(CeedOperator op, C CeedCallBackend(CeedRestoreWorkVector(ceed, &active_e_vec_in)); CeedCallBackend(CeedRestoreWorkVector(ceed, &active_e_vec_out)); CeedCallBackend(CeedDestroy(&ceed)); + CeedCallBackend(CeedQFunctionDestroy(&qf)); return CEED_ERROR_SUCCESS; } diff --git a/backends/hip-gen/ceed-hip-gen-operator-build.cpp b/backends/hip-gen/ceed-hip-gen-operator-build.cpp index 6a13cd5600..0819519512 100644 --- a/backends/hip-gen/ceed-hip-gen-operator-build.cpp +++ b/backends/hip-gen/ceed-hip-gen-operator-build.cpp @@ -910,6 +910,7 @@ extern "C" int CeedOperatorBuildKernel_Hip_gen(CeedOperator op) { CeedCallBackend(CeedGetKernel_Hip(ceed, data->module, operator_name.c_str(), &data->op)); CeedCallBackend(CeedOperatorSetSetupDone(op)); CeedCallBackend(CeedDestroy(&ceed)); + CeedCallBackend(CeedQFunctionDestroy(&qf)); return CEED_ERROR_SUCCESS; } diff --git a/backends/hip-gen/ceed-hip-gen-operator.c b/backends/hip-gen/ceed-hip-gen-operator.c index a61d1df32f..11ee4943b8 100644 --- a/backends/hip-gen/ceed-hip-gen-operator.c +++ b/backends/hip-gen/ceed-hip-gen-operator.c @@ -40,14 +40,6 @@ static int CeedOperatorApplyAdd_Hip_gen(CeedOperator op, CeedVector input_vec, C CeedOperatorField *op_input_fields, *op_output_fields; CeedOperator_Hip_gen *data; - CeedCallBackend(CeedOperatorGetCeed(op, &ceed)); - CeedCallBackend(CeedOperatorGetData(op, &data)); - CeedCallBackend(CeedOperatorGetQFunction(op, &qf)); - CeedCallBackend(CeedQFunctionGetData(qf, &qf_data)); - CeedCallBackend(CeedOperatorGetNumElements(op, &num_elem)); - CeedCallBackend(CeedOperatorGetFields(op, &num_input_fields, &op_input_fields, &num_output_fields, &op_output_fields)); - CeedCallBackend(CeedQFunctionGetFields(qf, NULL, &qf_input_fields, NULL, &qf_output_fields)); - // Check for tensor-product bases { bool has_tensor_bases; @@ -57,14 +49,21 @@ static int CeedOperatorApplyAdd_Hip_gen(CeedOperator op, CeedVector input_vec, C if (!has_tensor_bases) { CeedOperator op_fallback; - CeedDebug256(ceed, CEED_DEBUG_COLOR_SUCCESS, "Falling back to /gpu/hip/ref CeedOperator due to non-tensor bases"); - CeedCallBackend(CeedDestroy(&ceed)); + CeedDebug256(CeedOperatorReturnCeed(op), CEED_DEBUG_COLOR_SUCCESS, "Falling back to /gpu/hip/ref CeedOperator due to non-tensor bases"); CeedCallBackend(CeedOperatorGetFallback(op, &op_fallback)); CeedCallBackend(CeedOperatorApplyAdd(op_fallback, input_vec, output_vec, request)); return CEED_ERROR_SUCCESS; } } + CeedCallBackend(CeedOperatorGetCeed(op, &ceed)); + CeedCallBackend(CeedOperatorGetData(op, &data)); + CeedCallBackend(CeedOperatorGetQFunction(op, &qf)); + CeedCallBackend(CeedQFunctionGetData(qf, &qf_data)); + CeedCallBackend(CeedOperatorGetNumElements(op, &num_elem)); + CeedCallBackend(CeedOperatorGetFields(op, &num_input_fields, &op_input_fields, &num_output_fields, &op_output_fields)); + CeedCallBackend(CeedQFunctionGetFields(qf, NULL, &qf_input_fields, NULL, &qf_output_fields)); + // Creation of the operator CeedCallBackend(CeedOperatorBuildKernel_Hip_gen(op)); @@ -179,6 +178,7 @@ static int CeedOperatorApplyAdd_Hip_gen(CeedOperator op, CeedVector input_vec, C // Restore context data CeedCallBackend(CeedQFunctionRestoreInnerContextData(qf, &qf_data->d_c)); CeedCallBackend(CeedDestroy(&ceed)); + CeedCallBackend(CeedQFunctionDestroy(&qf)); return CEED_ERROR_SUCCESS; } diff --git a/backends/hip-ref/ceed-hip-ref-operator.c b/backends/hip-ref/ceed-hip-ref-operator.c index 04733482ee..6f1119084b 100644 --- a/backends/hip-ref/ceed-hip-ref-operator.c +++ b/backends/hip-ref/ceed-hip-ref-operator.c @@ -353,6 +353,7 @@ static int CeedOperatorSetup_Hip(CeedOperator op) { } } CeedCallBackend(CeedOperatorSetSetupDone(op)); + CeedCallBackend(CeedQFunctionDestroy(&qf)); return CEED_ERROR_SUCCESS; } @@ -599,6 +600,7 @@ static int CeedOperatorApplyAdd_Hip(CeedOperator op, CeedVector in_vec, CeedVect // Return work vector CeedCallBackend(CeedRestoreWorkVector(ceed, &active_e_vec)); CeedCallBackend(CeedDestroy(&ceed)); + CeedCallBackend(CeedQFunctionDestroy(&qf)); return CEED_ERROR_SUCCESS; } @@ -737,6 +739,7 @@ static int CeedOperatorSetupAtPoints_Hip(CeedOperator op) { } } CeedCallBackend(CeedOperatorSetSetupDone(op)); + CeedCallBackend(CeedQFunctionDestroy(&qf)); return CEED_ERROR_SUCCESS; } @@ -924,6 +927,7 @@ static int CeedOperatorApplyAddAtPoints_Hip(CeedOperator op, CeedVector in_vec, // Restore work vector CeedCallBackend(CeedRestoreWorkVector(ceed, &active_e_vec)); CeedCallBackend(CeedDestroy(&ceed)); + CeedCallBackend(CeedQFunctionDestroy(&qf)); return CEED_ERROR_SUCCESS; } @@ -1065,9 +1069,10 @@ static inline int CeedOperatorLinearAssembleQFunctionCore_Hip(CeedOperator op, b } // Restore output + CeedCallBackend(CeedVectorRestoreArray(*assembled, &assembled_array)); CeedCallBackend(CeedDestroy(&ceed)); CeedCallBackend(CeedDestroy(&ceed_parent)); - CeedCallBackend(CeedVectorRestoreArray(*assembled, &assembled_array)); + CeedCallBackend(CeedQFunctionDestroy(&qf)); return CEED_ERROR_SUCCESS; } @@ -1271,6 +1276,7 @@ static inline int CeedOperatorAssembleDiagonalSetup_Hip(CeedOperator op) { CeedCallBackend(CeedDestroy(&ceed)); CeedCallBackend(CeedBasisDestroy(&basis_in)); CeedCallBackend(CeedBasisDestroy(&basis_out)); + CeedCallBackend(CeedQFunctionDestroy(&qf)); return CEED_ERROR_SUCCESS; } @@ -1357,6 +1363,7 @@ static inline int CeedOperatorAssembleDiagonalSetupCompile_Hip(CeedOperator op, CeedCallBackend(CeedDestroy(&ceed)); CeedCallBackend(CeedBasisDestroy(&basis_in)); CeedCallBackend(CeedBasisDestroy(&basis_out)); + CeedCallBackend(CeedQFunctionDestroy(&qf)); return CEED_ERROR_SUCCESS; } @@ -1661,6 +1668,7 @@ static int CeedSingleOperatorAssembleSetup_Hip(CeedOperator op, CeedInt use_ceed CeedCallBackend(CeedElemRestrictionDestroy(&rstr_out)); CeedCallBackend(CeedBasisDestroy(&basis_in)); CeedCallBackend(CeedBasisDestroy(&basis_out)); + CeedCallBackend(CeedQFunctionDestroy(&qf)); return CEED_ERROR_SUCCESS; } @@ -2038,6 +2046,7 @@ static int CeedOperatorLinearAssembleAddDiagonalAtPoints_Hip(CeedOperator op, Ce CeedCallBackend(CeedRestoreWorkVector(ceed, &active_e_vec_in)); CeedCallBackend(CeedRestoreWorkVector(ceed, &active_e_vec_out)); CeedCallBackend(CeedDestroy(&ceed)); + CeedCallBackend(CeedQFunctionDestroy(&qf)); return CEED_ERROR_SUCCESS; } diff --git a/backends/opt/ceed-opt-operator.c b/backends/opt/ceed-opt-operator.c index 69f4560a06..679e88163b 100644 --- a/backends/opt/ceed-opt-operator.c +++ b/backends/opt/ceed-opt-operator.c @@ -267,6 +267,7 @@ static int CeedOperatorSetup_Opt(CeedOperator op) { } CeedCallBackend(CeedOperatorSetSetupDone(op)); + CeedCallBackend(CeedQFunctionDestroy(&qf)); return CEED_ERROR_SUCCESS; } @@ -449,21 +450,17 @@ static int CeedOperatorApplyAdd_Opt(CeedOperator op, CeedVector in_vec, CeedVect CeedOperatorField *op_input_fields, *op_output_fields; CeedOperator_Opt *impl; + // Setup + CeedCallBackend(CeedOperatorSetup_Opt(op)); + CeedCallBackend(CeedOperatorGetCeed(op, &ceed)); CeedCallBackend(CeedGetData(ceed, &ceed_impl)); CeedCallBackend(CeedDestroy(&ceed)); CeedCallBackend(CeedOperatorGetData(op, &impl)); CeedCallBackend(CeedOperatorGetNumElements(op, &num_elem)); - CeedCallBackend(CeedOperatorGetNumQuadraturePoints(op, &Q)); - CeedCallBackend(CeedOperatorGetQFunction(op, &qf)); - CeedCallBackend(CeedOperatorGetFields(op, &num_input_fields, &op_input_fields, &num_output_fields, &op_output_fields)); - CeedCallBackend(CeedQFunctionGetFields(qf, NULL, &qf_input_fields, NULL, &qf_output_fields)); const CeedInt block_size = ceed_impl->block_size; const CeedInt num_blocks = (num_elem / block_size) + !!(num_elem % block_size); - // Setup - CeedCallBackend(CeedOperatorSetup_Opt(op)); - // Restriction only operator if (impl->is_identity_rstr_op) { for (CeedInt b = 0; b < num_blocks; b++) { @@ -473,6 +470,11 @@ static int CeedOperatorApplyAdd_Opt(CeedOperator op, CeedVector in_vec, CeedVect return CEED_ERROR_SUCCESS; } + CeedCallBackend(CeedOperatorGetNumQuadraturePoints(op, &Q)); + CeedCallBackend(CeedOperatorGetQFunction(op, &qf)); + CeedCallBackend(CeedOperatorGetFields(op, &num_input_fields, &op_input_fields, &num_output_fields, &op_output_fields)); + CeedCallBackend(CeedQFunctionGetFields(qf, NULL, &qf_input_fields, NULL, &qf_output_fields)); + // Input Evecs and Restriction CeedCallBackend(CeedOperatorSetupInputs_Opt(num_input_fields, qf_input_fields, op_input_fields, in_vec, e_data, impl, request)); @@ -506,6 +508,7 @@ static int CeedOperatorApplyAdd_Opt(CeedOperator op, CeedVector in_vec, CeedVect // Restore input arrays CeedCallBackend(CeedOperatorRestoreInputs_Opt(num_input_fields, qf_input_fields, op_input_fields, e_data, impl)); + CeedCallBackend(CeedQFunctionDestroy(&qf)); return CEED_ERROR_SUCCESS; } @@ -718,6 +721,7 @@ static inline int CeedOperatorLinearAssembleQFunctionCore_Opt(CeedOperator op, b // Restore input arrays CeedCallBackend(CeedOperatorRestoreInputs_Opt(num_input_fields, qf_input_fields, op_input_fields, e_data, impl)); CeedCallBackend(CeedDestroy(&ceed)); + CeedCallBackend(CeedQFunctionDestroy(&qf)); return CEED_ERROR_SUCCESS; } diff --git a/backends/ref/ceed-ref-operator.c b/backends/ref/ceed-ref-operator.c index fa461f899a..b53a8c6150 100644 --- a/backends/ref/ceed-ref-operator.c +++ b/backends/ref/ceed-ref-operator.c @@ -201,6 +201,7 @@ static int CeedOperatorSetup_Ref(CeedOperator op) { } CeedCallBackend(CeedOperatorSetSetupDone(op)); + CeedCallBackend(CeedQFunctionDestroy(&qf)); return CEED_ERROR_SUCCESS; } @@ -383,16 +384,12 @@ static int CeedOperatorApplyAdd_Ref(CeedOperator op, CeedVector in_vec, CeedVect CeedOperatorField *op_input_fields, *op_output_fields; CeedOperator_Ref *impl; - CeedCallBackend(CeedOperatorGetData(op, &impl)); - CeedCallBackend(CeedOperatorGetQFunction(op, &qf)); - CeedCallBackend(CeedOperatorGetNumQuadraturePoints(op, &Q)); - CeedCallBackend(CeedOperatorGetNumElements(op, &num_elem)); - CeedCallBackend(CeedOperatorGetFields(op, &num_input_fields, &op_input_fields, &num_output_fields, &op_output_fields)); - CeedCallBackend(CeedQFunctionGetFields(qf, NULL, &qf_input_fields, NULL, &qf_output_fields)); - // Setup CeedCallBackend(CeedOperatorSetup_Ref(op)); + CeedCallBackend(CeedOperatorGetData(op, &impl)); + CeedCallBackend(CeedOperatorGetFields(op, &num_input_fields, &op_input_fields, &num_output_fields, &op_output_fields)); + // Restriction only operator if (impl->is_identity_rstr_op) { CeedElemRestriction elem_rstr; @@ -406,6 +403,11 @@ static int CeedOperatorApplyAdd_Ref(CeedOperator op, CeedVector in_vec, CeedVect return CEED_ERROR_SUCCESS; } + CeedCallBackend(CeedOperatorGetQFunction(op, &qf)); + CeedCallBackend(CeedOperatorGetNumQuadraturePoints(op, &Q)); + CeedCallBackend(CeedOperatorGetNumElements(op, &num_elem)); + CeedCallBackend(CeedQFunctionGetFields(qf, NULL, &qf_input_fields, NULL, &qf_output_fields)); + // Input Evecs and Restriction CeedCallBackend(CeedOperatorSetupInputs_Ref(num_input_fields, qf_input_fields, op_input_fields, in_vec, false, e_data_full, impl, request)); @@ -466,6 +468,7 @@ static int CeedOperatorApplyAdd_Ref(CeedOperator op, CeedVector in_vec, CeedVect // Restore input arrays CeedCallBackend(CeedOperatorRestoreInputs_Ref(num_input_fields, qf_input_fields, op_input_fields, false, e_data_full, impl)); + CeedCallBackend(CeedQFunctionDestroy(&qf)); return CEED_ERROR_SUCCESS; } @@ -646,6 +649,7 @@ static inline int CeedOperatorLinearAssembleQFunctionCore_Ref(CeedOperator op, b // Restore output CeedCallBackend(CeedVectorRestoreArray(*assembled, &assembled_array)); CeedCallBackend(CeedDestroy(&ceed_parent)); + CeedCallBackend(CeedQFunctionDestroy(&qf)); return CEED_ERROR_SUCCESS; } @@ -873,6 +877,7 @@ static int CeedOperatorSetupAtPoints_Ref(CeedOperator op) { } CeedCallBackend(CeedOperatorSetSetupDone(op)); + CeedCallBackend(CeedQFunctionDestroy(&qf)); return CEED_ERROR_SUCCESS; } @@ -1065,6 +1070,7 @@ static int CeedOperatorApplyAddAtPoints_Ref(CeedOperator op, CeedVector in_vec, // Cleanup point coordinates CeedCallBackend(CeedVectorDestroy(&point_coords)); CeedCallBackend(CeedElemRestrictionDestroy(&rstr_points)); + CeedCallBackend(CeedQFunctionDestroy(&qf)); return CEED_ERROR_SUCCESS; } @@ -1289,6 +1295,7 @@ static inline int CeedOperatorLinearAssembleQFunctionAtPointsCore_Ref(CeedOperat CeedCallBackend(CeedDestroy(&ceed)); CeedCallBackend(CeedVectorDestroy(&point_coords)); CeedCallBackend(CeedElemRestrictionDestroy(&rstr_points)); + CeedCallBackend(CeedQFunctionDestroy(&qf)); return CEED_ERROR_SUCCESS; } @@ -1541,6 +1548,7 @@ static int CeedOperatorLinearAssembleAddDiagonalAtPoints_Ref(CeedOperator op, Ce CeedCallBackend(CeedVectorDestroy(&out_vec)); CeedCallBackend(CeedVectorDestroy(&point_coords)); CeedCallBackend(CeedElemRestrictionDestroy(&rstr_points)); + CeedCallBackend(CeedQFunctionDestroy(&qf)); return CEED_ERROR_SUCCESS; } diff --git a/backends/sycl-gen/ceed-sycl-gen-operator-build.sycl.cpp b/backends/sycl-gen/ceed-sycl-gen-operator-build.sycl.cpp index f1fb58e42f..b3850c05fc 100644 --- a/backends/sycl-gen/ceed-sycl-gen-operator-build.sycl.cpp +++ b/backends/sycl-gen/ceed-sycl-gen-operator-build.sycl.cpp @@ -782,6 +782,7 @@ extern "C" int CeedOperatorBuildKernel_Sycl_gen(CeedOperator op) { CeedCallBackend(CeedGetKernel_Sycl(ceed, impl->sycl_module, operator_name, &impl->op)); CeedCallBackend(CeedOperatorSetSetupDone(op)); CeedCallBackend(CeedDestroy(&ceed)); + CeedCallBackend(CeedQFunctionDestroy(&qf)); return CEED_ERROR_SUCCESS; } diff --git a/backends/sycl-gen/ceed-sycl-gen-operator.sycl.cpp b/backends/sycl-gen/ceed-sycl-gen-operator.sycl.cpp index eb74176bbf..1acd7147ee 100644 --- a/backends/sycl-gen/ceed-sycl-gen-operator.sycl.cpp +++ b/backends/sycl-gen/ceed-sycl-gen-operator.sycl.cpp @@ -39,15 +39,6 @@ static int CeedOperatorApplyAdd_Sycl_gen(CeedOperator op, CeedVector input_vec, CeedOperatorField *op_input_fields, *op_output_fields; CeedOperator_Sycl_gen *impl; - CeedCallBackend(CeedOperatorGetCeed(op, &ceed)); - CeedCallBackend(CeedGetData(ceed, &ceed_Sycl)); - CeedCallBackend(CeedOperatorGetData(op, &impl)); - CeedCallBackend(CeedOperatorGetQFunction(op, &qf)); - CeedCallBackend(CeedQFunctionGetData(qf, &qf_impl)); - CeedCallBackend(CeedOperatorGetNumElements(op, &num_elem)); - CeedCallBackend(CeedOperatorGetFields(op, &num_input_fields, &op_input_fields, &num_output_fields, &op_output_fields)); - CeedCallBackend(CeedQFunctionGetFields(qf, NULL, &qf_input_fields, NULL, &qf_output_fields)); - // Check for tensor-product bases { bool has_tensor_bases; @@ -57,14 +48,22 @@ static int CeedOperatorApplyAdd_Sycl_gen(CeedOperator op, CeedVector input_vec, if (!has_tensor_bases) { CeedOperator op_fallback; - CeedDebug256(ceed, CEED_DEBUG_COLOR_SUCCESS, "Falling back to sycl/ref CeedOperator due to non-tensor bases"); - CeedCallBackend(CeedDestroy(&ceed)); + CeedDebug256(CeedOperatorReturnCeed(op), CEED_DEBUG_COLOR_SUCCESS, "Falling back to sycl/ref CeedOperator due to non-tensor bases"); CeedCallBackend(CeedOperatorGetFallback(op, &op_fallback)); CeedCallBackend(CeedOperatorApplyAdd(op_fallback, input_vec, output_vec, request)); return CEED_ERROR_SUCCESS; } } + CeedCallBackend(CeedOperatorGetCeed(op, &ceed)); + CeedCallBackend(CeedGetData(ceed, &ceed_Sycl)); + CeedCallBackend(CeedOperatorGetData(op, &impl)); + CeedCallBackend(CeedOperatorGetQFunction(op, &qf)); + CeedCallBackend(CeedQFunctionGetData(qf, &qf_impl)); + CeedCallBackend(CeedOperatorGetNumElements(op, &num_elem)); + CeedCallBackend(CeedOperatorGetFields(op, &num_input_fields, &op_input_fields, &num_output_fields, &op_output_fields)); + CeedCallBackend(CeedQFunctionGetFields(qf, NULL, &qf_input_fields, NULL, &qf_output_fields)); + // Creation of the operator CeedCallBackend(CeedOperatorBuildKernel_Sycl_gen(op)); @@ -200,6 +199,7 @@ static int CeedOperatorApplyAdd_Sycl_gen(CeedOperator op, CeedVector input_vec, // Restore context data CeedCallBackend(CeedQFunctionRestoreInnerContextData(qf, &qf_impl->d_c)); CeedCallBackend(CeedDestroy(&ceed)); + CeedCallBackend(CeedQFunctionDestroy(&qf)); return CEED_ERROR_SUCCESS; } diff --git a/backends/sycl-ref/ceed-sycl-ref-operator.sycl.cpp b/backends/sycl-ref/ceed-sycl-ref-operator.sycl.cpp index e843015df1..89148678d3 100644 --- a/backends/sycl-ref/ceed-sycl-ref-operator.sycl.cpp +++ b/backends/sycl-ref/ceed-sycl-ref-operator.sycl.cpp @@ -249,6 +249,7 @@ static int CeedOperatorSetup_Sycl(CeedOperator op) { CeedCallBackend(CeedOperatorSetupFields_Sycl(qf, op, false, impl->e_vecs, impl->q_vecs_out, num_input_fields, num_output_fields, Q, num_elem)); CeedCallBackend(CeedOperatorSetSetupDone(op)); + CeedCallBackend(CeedQFunctionDestroy(&qf)); return CEED_ERROR_SUCCESS; } @@ -467,6 +468,7 @@ static int CeedOperatorApplyAdd_Sycl(CeedOperator op, CeedVector in_vec, CeedVec // Restore input arrays CeedCallBackend(CeedOperatorRestoreInputs_Sycl(num_input_fields, qf_input_fields, op_input_fields, false, e_data, impl)); + CeedCallBackend(CeedQFunctionDestroy(&qf)); return CEED_ERROR_SUCCESS; } @@ -607,6 +609,7 @@ static inline int CeedOperatorLinearAssembleQFunctionCore_Sycl(CeedOperator op, // Restore output CeedCallBackend(CeedVectorRestoreArray(*assembled, &assembled_array)); CeedCallBackend(CeedDestroy(&ceed_parent)); + CeedCallBackend(CeedQFunctionDestroy(&qf)); return CEED_ERROR_SUCCESS; } @@ -812,13 +815,14 @@ static inline int CeedOperatorAssembleDiagonalSetup_Sycl(CeedOperator op) { CeedCallBackend(CeedElemRestrictionReferenceCopy(rstr_out, &diag->diag_rstr)); CeedCallBackend(CeedElemRestrictionDestroy(&rstr_out)); + // Wait for all copies to complete and handle exceptions + CeedCallSycl(ceed, sycl::event::wait_and_throw(copy_events)); + // Cleanup CeedCallBackend(CeedDestroy(&ceed)); CeedCallBackend(CeedBasisDestroy(&basis_in)); CeedCallBackend(CeedBasisDestroy(&basis_out)); - - // Wait for all copies to complete and handle exceptions - CeedCallSycl(ceed, sycl::event::wait_and_throw(copy_events)); + CeedCallBackend(CeedQFunctionDestroy(&qf)); return CEED_ERROR_SUCCESS; } @@ -1175,6 +1179,7 @@ static int CeedSingleOperatorAssembleSetup_Sycl(CeedOperator op) { CeedCallBackend(CeedElemRestrictionDestroy(&rstr_out)); CeedCallBackend(CeedBasisDestroy(&basis_in)); CeedCallBackend(CeedBasisDestroy(&basis_out)); + CeedCallBackend(CeedQFunctionDestroy(&qf)); return CEED_ERROR_SUCCESS; } diff --git a/interface/ceed-operator.c b/interface/ceed-operator.c index 0bdc136ca1..3d6f6dd004 100644 --- a/interface/ceed-operator.c +++ b/interface/ceed-operator.c @@ -148,6 +148,7 @@ int CeedOperatorSingleView(CeedOperator op, bool sub, FILE *stream) { CeedCall(CeedOperatorGetFields(op, &num_input_fields, &op_input_fields, &num_output_fields, &op_output_fields)); CeedCall(CeedOperatorGetQFunction(op, &qf)); CeedCall(CeedQFunctionGetFields(qf, NULL, &qf_input_fields, NULL, &qf_output_fields)); + CeedCall(CeedQFunctionDestroy(&qf)); fprintf(stream, "%s %" CeedInt_FMT " elements with %" CeedInt_FMT " quadrature points each\n", pre, num_elem, num_qpts); fprintf(stream, "%s %" CeedInt_FMT " field%s\n", pre, total_fields, total_fields > 1 ? "s" : ""); @@ -374,6 +375,7 @@ static int CeedOperatorContextSetGeneric(CeedOperator op, CeedContextFieldLabel CeedCall(CeedOperatorGetQFunction(sub_operators[i], &qf)); CeedCall(CeedQFunctionGetContext(qf, &ctx)); + CeedCall(CeedQFunctionDestroy(&qf)); // Try every sub-operator, ok if some sub-operators do not have field if (field_label->sub_labels[i] && ctx) { CeedCall(CeedQFunctionContextSetGeneric(ctx, field_label->sub_labels[i], field_type, values)); @@ -385,6 +387,7 @@ static int CeedOperatorContextSetGeneric(CeedOperator op, CeedContextFieldLabel CeedCall(CeedOperatorGetQFunction(op, &qf)); CeedCall(CeedQFunctionGetContext(qf, &ctx)); + CeedCall(CeedQFunctionDestroy(&qf)); CeedCheck(ctx, CeedOperatorReturnCeed(op), CEED_ERROR_UNSUPPORTED, "QFunction does not have context data"); CeedCall(CeedQFunctionContextSetGeneric(ctx, field_label, field_type, values)); } @@ -443,6 +446,7 @@ static int CeedOperatorContextGetGenericRead(CeedOperator op, CeedContextFieldLa CeedCall(CeedOperatorGetQFunction(sub_operators[i], &qf)); CeedCall(CeedQFunctionGetContext(qf, &ctx)); + CeedCall(CeedQFunctionDestroy(&qf)); // Try every sub-operator, ok if some sub-operators do not have field if (field_label->sub_labels[i] && ctx) { CeedCall(CeedQFunctionContextGetGenericRead(ctx, field_label->sub_labels[i], field_type, num_values, values)); @@ -455,6 +459,7 @@ static int CeedOperatorContextGetGenericRead(CeedOperator op, CeedContextFieldLa CeedCall(CeedOperatorGetQFunction(op, &qf)); CeedCall(CeedQFunctionGetContext(qf, &ctx)); + CeedCall(CeedQFunctionDestroy(&qf)); CeedCheck(ctx, CeedOperatorReturnCeed(op), CEED_ERROR_UNSUPPORTED, "QFunction does not have context data"); CeedCall(CeedQFunctionContextGetGenericRead(ctx, field_label, field_type, num_values, values)); } @@ -507,6 +512,7 @@ static int CeedOperatorContextRestoreGenericRead(CeedOperator op, CeedContextFie CeedCall(CeedOperatorGetQFunction(sub_operators[i], &qf)); CeedCall(CeedQFunctionGetContext(qf, &ctx)); + CeedCall(CeedQFunctionDestroy(&qf)); // Try every sub-operator, ok if some sub-operators do not have field if (field_label->sub_labels[i] && ctx) { CeedCall(CeedQFunctionContextRestoreGenericRead(ctx, field_label->sub_labels[i], field_type, values)); @@ -519,6 +525,7 @@ static int CeedOperatorContextRestoreGenericRead(CeedOperator op, CeedContextFie CeedCall(CeedOperatorGetQFunction(op, &qf)); CeedCall(CeedQFunctionGetContext(qf, &ctx)); + CeedCall(CeedQFunctionDestroy(&qf)); CeedCheck(ctx, CeedOperatorReturnCeed(op), CEED_ERROR_UNSUPPORTED, "QFunction does not have context data"); CeedCall(CeedQFunctionContextRestoreGenericRead(ctx, field_label, field_type, values)); } @@ -640,7 +647,8 @@ int CeedOperatorGetQFunction(CeedOperator op, CeedQFunction *qf) { CeedCall(CeedOperatorIsComposite(op, &is_composite)); CeedCheck(!is_composite, CeedOperatorReturnCeed(op), CEED_ERROR_MINOR, "Not defined for composite operator"); - *qf = op->qf; + *qf = NULL; + CeedCall(CeedQFunctionReferenceCopy(op->qf, qf)); return CEED_ERROR_SUCCESS; } @@ -939,6 +947,7 @@ int CeedOperatorSetField(CeedOperator op, const char *field_name, CeedElemRestri CeedCall(CeedOperatorGetQFunction(op, &qf)); CeedCall(CeedQFunctionGetFields(qf, &num_input_fields, &qf_input_fields, &num_output_fields, &qf_output_fields)); + CeedCall(CeedQFunctionDestroy(&qf)); for (CeedInt i = 0; i < num_input_fields; i++) { const char *qf_field_name; @@ -1021,6 +1030,7 @@ int CeedOperatorGetFields(CeedOperator op, CeedInt *num_input_fields, CeedOperat CeedCall(CeedOperatorGetQFunction(op, &qf)); CeedCall(CeedQFunctionGetFields(qf, num_input_fields, NULL, num_output_fields, NULL)); + CeedCall(CeedQFunctionDestroy(&qf)); if (input_fields) *input_fields = op->input_fields; if (output_fields) *output_fields = op->output_fields; return CEED_ERROR_SUCCESS; @@ -1400,6 +1410,7 @@ int CeedOperatorCheckReady(CeedOperator op) { // Flag as immutable and ready op->is_interface_setup = true; if (qf && qf != CEED_QFUNCTION_NONE) CeedCall(CeedQFunctionSetImmutable(qf)); + CeedCall(CeedQFunctionDestroy(&qf)); if (op->dqf && op->dqf != CEED_QFUNCTION_NONE) CeedCall(CeedQFunctionSetImmutable(op->dqf)); if (op->dqfT && op->dqfT != CEED_QFUNCTION_NONE) CeedCall(CeedQFunctionSetImmutable(op->dqfT)); return CEED_ERROR_SUCCESS; @@ -1699,6 +1710,7 @@ int CeedOperatorGetFlopsEstimate(CeedOperator op, CeedSize *flops) { CeedCall(CeedOperatorGetQFunction(op, &qf)); CeedCall(CeedQFunctionGetFields(qf, &num_input_fields, &qf_input_fields, &num_output_fields, &qf_output_fields)); + CeedCall(CeedQFunctionDestroy(&qf)); CeedCall(CeedOperatorGetFields(op, NULL, &op_input_fields, NULL, &op_output_fields)); CeedCall(CeedOperatorGetNumElements(op, &num_elem)); @@ -1734,6 +1746,7 @@ int CeedOperatorGetFlopsEstimate(CeedOperator op, CeedSize *flops) { CeedCall(CeedOperatorGetNumQuadraturePoints(op, &num_qpts)); CeedCall(CeedOperatorGetQFunction(op, &qf)); CeedCall(CeedQFunctionGetFlopsEstimate(qf, &qf_flops)); + CeedCall(CeedQFunctionDestroy(&qf)); CeedCheck(qf_flops > -1, CeedOperatorReturnCeed(op), CEED_ERROR_INCOMPLETE, "Must set CeedQFunction FLOPs estimate with CeedQFunctionSetUserFlopsEstimate"); *flops += num_elem * num_qpts * qf_flops; @@ -1790,6 +1803,7 @@ int CeedOperatorGetContext(CeedOperator op, CeedQFunctionContext *ctx) { CeedCheck(!is_composite, CeedOperatorReturnCeed(op), CEED_ERROR_INCOMPATIBLE, "Cannot retrieve CeedQFunctionContext for composite operator"); CeedCall(CeedOperatorGetQFunction(op, &qf)); CeedCall(CeedQFunctionGetInnerContext(qf, &qf_ctx)); + CeedCall(CeedQFunctionDestroy(&qf)); if (qf_ctx) CeedCall(CeedQFunctionContextReferenceCopy(qf_ctx, ctx)); return CEED_ERROR_SUCCESS; } @@ -1882,6 +1896,7 @@ int CeedOperatorGetContextFieldLabel(CeedOperator op, const char *field_name, Ce // Single, non-composite operator CeedCall(CeedOperatorGetQFunction(op, &qf)); CeedCall(CeedQFunctionGetInnerContext(qf, &ctx)); + CeedCall(CeedQFunctionDestroy(&qf)); if (ctx) { CeedCall(CeedQFunctionContextGetFieldLabel(ctx, field_name, field_label)); } else { diff --git a/interface/ceed-preconditioning.c b/interface/ceed-preconditioning.c index 937d7fde0d..5535b91661 100644 --- a/interface/ceed-preconditioning.c +++ b/interface/ceed-preconditioning.c @@ -1535,6 +1535,7 @@ int CeedOperatorAssemblyDataCreate(Ceed ceed, CeedOperator op, CeedOperatorAssem } CeedCall(CeedVectorDestroy(&vec)); } + CeedCall(CeedQFunctionDestroy(&qf)); (*data)->num_active_bases_in = num_active_bases_in; (*data)->num_eval_modes_in = num_eval_modes_in; (*data)->eval_modes_in = eval_modes_in; @@ -2938,6 +2939,7 @@ int CeedOperatorCreateFDMElementInverse(CeedOperator op, CeedOperator *fdm_inv, CeedCall(CeedElemRestrictionDestroy(&rstr_qd_i)); CeedCall(CeedBasisDestroy(&basis)); CeedCall(CeedBasisDestroy(&fdm_basis)); + CeedCall(CeedQFunctionDestroy(&qf)); CeedCall(CeedQFunctionDestroy(&qf_fdm)); return CEED_ERROR_SUCCESS; } From 1485364c160e7dd269a03246437f5ec5e1f33a6b Mon Sep 17 00:00:00 2001 From: Jeremy L Thompson Date: Wed, 23 Oct 2024 11:56:46 -0600 Subject: [PATCH 3/7] ctx - require CeedQFGetCtx to be Destroyed --- interface/ceed-fortran.c | 2 ++ interface/ceed-operator.c | 45 +++++++++++++------------------- interface/ceed-preconditioning.c | 1 + interface/ceed-qfunction.c | 8 ++++-- tests/t525-operator.c | 1 + 5 files changed, 28 insertions(+), 29 deletions(-) diff --git a/interface/ceed-fortran.c b/interface/ceed-fortran.c index d3ed061c3e..501d901c2e 100644 --- a/interface/ceed-fortran.c +++ b/interface/ceed-fortran.c @@ -845,6 +845,8 @@ CEED_EXTERN void fCeedQFunctionSetContext(int *qf, int *ctx, int *err) { if (*err) return; fctxdata->inner_ctx = ctx_; *err = CeedQFunctionContextRestoreData(fctx, (void **)&fctxdata); + if (*err) return; + *err = CeedQFunctionContextDestroy(&fctx); } #define fCeedQFunctionView FORTRAN_NAME(ceedqfunctionview, CEEDQFUNCTIONVIEW) diff --git a/interface/ceed-operator.c b/interface/ceed-operator.c index 3d6f6dd004..96fe26a834 100644 --- a/interface/ceed-operator.c +++ b/interface/ceed-operator.c @@ -370,26 +370,22 @@ static int CeedOperatorContextSetGeneric(CeedOperator op, CeedContextFieldLabel "Composite operator modified after ContextFieldLabel created"); for (CeedInt i = 0; i < num_sub; i++) { - CeedQFunction qf; CeedQFunctionContext ctx; - CeedCall(CeedOperatorGetQFunction(sub_operators[i], &qf)); - CeedCall(CeedQFunctionGetContext(qf, &ctx)); - CeedCall(CeedQFunctionDestroy(&qf)); + CeedCall(CeedOperatorGetContext(sub_operators[i], &ctx)); // Try every sub-operator, ok if some sub-operators do not have field - if (field_label->sub_labels[i] && ctx) { + if (ctx && field_label->sub_labels[i]) { CeedCall(CeedQFunctionContextSetGeneric(ctx, field_label->sub_labels[i], field_type, values)); } + CeedCall(CeedQFunctionContextDestroy(&ctx)); } } else { - CeedQFunction qf; CeedQFunctionContext ctx; - CeedCall(CeedOperatorGetQFunction(op, &qf)); - CeedCall(CeedQFunctionGetContext(qf, &ctx)); - CeedCall(CeedQFunctionDestroy(&qf)); + CeedCall(CeedOperatorGetContext(op, &ctx)); CeedCheck(ctx, CeedOperatorReturnCeed(op), CEED_ERROR_UNSUPPORTED, "QFunction does not have context data"); CeedCall(CeedQFunctionContextSetGeneric(ctx, field_label, field_type, values)); + CeedCall(CeedQFunctionContextDestroy(&ctx)); } CeedCall(CeedOperatorSetQFunctionAssemblyDataUpdateNeeded(op, true)); return CEED_ERROR_SUCCESS; @@ -441,27 +437,24 @@ static int CeedOperatorContextGetGenericRead(CeedOperator op, CeedContextFieldLa "Composite operator modified after ContextFieldLabel created"); for (CeedInt i = 0; i < num_sub; i++) { - CeedQFunction qf; CeedQFunctionContext ctx; - CeedCall(CeedOperatorGetQFunction(sub_operators[i], &qf)); - CeedCall(CeedQFunctionGetContext(qf, &ctx)); - CeedCall(CeedQFunctionDestroy(&qf)); + CeedCall(CeedOperatorGetContext(sub_operators[i], &ctx)); // Try every sub-operator, ok if some sub-operators do not have field - if (field_label->sub_labels[i] && ctx) { + if (ctx && field_label->sub_labels[i]) { CeedCall(CeedQFunctionContextGetGenericRead(ctx, field_label->sub_labels[i], field_type, num_values, values)); + CeedCall(CeedQFunctionContextDestroy(&ctx)); return CEED_ERROR_SUCCESS; } + CeedCall(CeedQFunctionContextDestroy(&ctx)); } } else { - CeedQFunction qf; CeedQFunctionContext ctx; - CeedCall(CeedOperatorGetQFunction(op, &qf)); - CeedCall(CeedQFunctionGetContext(qf, &ctx)); - CeedCall(CeedQFunctionDestroy(&qf)); + CeedCall(CeedOperatorGetContext(op, &ctx)); CeedCheck(ctx, CeedOperatorReturnCeed(op), CEED_ERROR_UNSUPPORTED, "QFunction does not have context data"); CeedCall(CeedQFunctionContextGetGenericRead(ctx, field_label, field_type, num_values, values)); + CeedCall(CeedQFunctionContextDestroy(&ctx)); } return CEED_ERROR_SUCCESS; } @@ -507,27 +500,24 @@ static int CeedOperatorContextRestoreGenericRead(CeedOperator op, CeedContextFie "Composite operator modified after ContextFieldLabel created"); for (CeedInt i = 0; i < num_sub; i++) { - CeedQFunction qf; CeedQFunctionContext ctx; - CeedCall(CeedOperatorGetQFunction(sub_operators[i], &qf)); - CeedCall(CeedQFunctionGetContext(qf, &ctx)); - CeedCall(CeedQFunctionDestroy(&qf)); + CeedCall(CeedOperatorGetContext(sub_operators[i], &ctx)); // Try every sub-operator, ok if some sub-operators do not have field - if (field_label->sub_labels[i] && ctx) { + if (ctx && field_label->sub_labels[i]) { CeedCall(CeedQFunctionContextRestoreGenericRead(ctx, field_label->sub_labels[i], field_type, values)); + CeedCall(CeedQFunctionContextDestroy(&ctx)); return CEED_ERROR_SUCCESS; } + CeedCall(CeedQFunctionContextDestroy(&ctx)); } } else { - CeedQFunction qf; CeedQFunctionContext ctx; - CeedCall(CeedOperatorGetQFunction(op, &qf)); - CeedCall(CeedQFunctionGetContext(qf, &ctx)); - CeedCall(CeedQFunctionDestroy(&qf)); + CeedCall(CeedOperatorGetContext(op, &ctx)); CeedCheck(ctx, CeedOperatorReturnCeed(op), CEED_ERROR_UNSUPPORTED, "QFunction does not have context data"); CeedCall(CeedQFunctionContextRestoreGenericRead(ctx, field_label, field_type, values)); + CeedCall(CeedQFunctionContextDestroy(&ctx)); } return CEED_ERROR_SUCCESS; } @@ -1804,6 +1794,7 @@ int CeedOperatorGetContext(CeedOperator op, CeedQFunctionContext *ctx) { CeedCall(CeedOperatorGetQFunction(op, &qf)); CeedCall(CeedQFunctionGetInnerContext(qf, &qf_ctx)); CeedCall(CeedQFunctionDestroy(&qf)); + *ctx = NULL; if (qf_ctx) CeedCall(CeedQFunctionContextReferenceCopy(qf_ctx, ctx)); return CEED_ERROR_SUCCESS; } diff --git a/interface/ceed-preconditioning.c b/interface/ceed-preconditioning.c index 5535b91661..1cc767cb08 100644 --- a/interface/ceed-preconditioning.c +++ b/interface/ceed-preconditioning.c @@ -71,6 +71,7 @@ static int CeedQFunctionCreateFallback(Ceed fallback_ceed, CeedQFunction qf, Cee CeedCall(CeedQFunctionGetContext(qf, &ctx)); CeedCall(CeedQFunctionSetContext(*qf_fallback, ctx)); + CeedCall(CeedQFunctionContextDestroy(&ctx)); } CeedCall(CeedQFunctionGetFields(qf, &num_input_fields, &input_fields, &num_output_fields, &output_fields)); for (CeedInt i = 0; i < num_input_fields; i++) { diff --git a/interface/ceed-qfunction.c b/interface/ceed-qfunction.c index 0daa4b98fc..6936974fa4 100644 --- a/interface/ceed-qfunction.c +++ b/interface/ceed-qfunction.c @@ -330,7 +330,8 @@ int CeedQFunctionGetUserFunction(CeedQFunction qf, CeedQFunctionUser *f) { @ref Backend **/ int CeedQFunctionGetContext(CeedQFunction qf, CeedQFunctionContext *ctx) { - *ctx = qf->ctx; + *ctx = NULL; + if (qf->ctx) CeedCall(CeedQFunctionContextReferenceCopy(qf->ctx, ctx)); return CEED_ERROR_SUCCESS; } @@ -361,6 +362,7 @@ int CeedQFunctionGetContextData(CeedQFunction qf, CeedMemType mem_type, void *da } else { *(void **)data = NULL; } + CeedCall(CeedQFunctionContextDestroy(&ctx)); return CEED_ERROR_SUCCESS; } @@ -387,7 +389,7 @@ int CeedQFunctionRestoreContextData(CeedQFunction qf, void *data) { CeedCall(CeedQFunctionContextRestoreDataRead(ctx, data)); } } - *(void **)data = NULL; + CeedCall(CeedQFunctionContextDestroy(&ctx)); return CEED_ERROR_SUCCESS; } @@ -415,6 +417,7 @@ int CeedQFunctionGetInnerContext(CeedQFunction qf, CeedQFunctionContext *ctx) { } else { *ctx = qf_ctx; } + CeedCall(CeedQFunctionContextDestroy(&qf_ctx)); return CEED_ERROR_SUCCESS; } @@ -730,6 +733,7 @@ int CeedQFunctionCreateIdentity(Ceed ceed, CeedInt size, CeedEvalMode in_mode, C CeedCall(CeedQFunctionGetContext(*qf, &ctx)); CeedCall(CeedQFunctionContextGetFieldLabel(ctx, "size", &size_label)); CeedCall(CeedQFunctionContextSetInt32(ctx, size_label, &size)); + CeedCall(CeedQFunctionContextDestroy(&ctx)); return CEED_ERROR_SUCCESS; } diff --git a/tests/t525-operator.c b/tests/t525-operator.c index 9d0d80442f..9617d94f32 100644 --- a/tests/t525-operator.c +++ b/tests/t525-operator.c @@ -112,6 +112,7 @@ int main(int argc, char **argv) { CeedOperatorGetContext(op_sub_1, &ctx_copy); if (ctx_copy != qf_ctx_sub_1) printf("Incorrect QFunctionContext retrieved"); + CeedQFunctionContextDestroy(&ctx_copy); CeedOperatorGetContext(op_sub_2, &ctx_copy); // Destroys reference to qf_ctx_sub_1 if (ctx_copy != qf_ctx_sub_2) printf("Incorrect QFunctionContext retrieved"); From dc3318a4fa50e4a2bf558a69684a8f36486f60c8 Mon Sep 17 00:00:00 2001 From: Jeremy L Thompson Date: Wed, 23 Oct 2024 12:23:53 -0600 Subject: [PATCH 4/7] doc - update release notes for new Destroy rqmts --- doc/sphinx/source/releasenotes.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/doc/sphinx/source/releasenotes.md b/doc/sphinx/source/releasenotes.md index 4e629b48e0..816187fd1f 100644 --- a/doc/sphinx/source/releasenotes.md +++ b/doc/sphinx/source/releasenotes.md @@ -11,10 +11,10 @@ On this page we provide a summary of the main API changes, new features and exam - Add `bool` field type for `CeedQFunctionContext` and related interfaces to use `bool` fields. - `CEED_BASIS_COLLOCATED` removed; users should only use `CEED_BASIS_NONE`. - Remove unneeded pointer for `CeedElemRestrictionGetELayout`. -- Require use of `Ceed*Destroy()` on Ceed objects returned from `CeedOperatorFieldGet*()`; - Change QFunction source include file handling in JiT compilers - Add `CEED_RUNNING_JIT_PASS` compiler definition for wrapping header files that device JiT compilers cannot read - Users should now prefer `#include ` rather than `#include ` in QFunction source files +- Require use of `Ceed*Destroy()` on Ceed objects returned from `Ceed*Get*()`. ### New features From 3e551a327d6c97f9de071b988b42ffdb7bed19a7 Mon Sep 17 00:00:00 2001 From: Jeremy L Thompson Date: Wed, 23 Oct 2024 13:07:13 -0600 Subject: [PATCH 5/7] rust - update for destroy requirements --- rust/libceed/src/basis.rs | 6 +----- rust/libceed/src/elem_restriction.rs | 6 +----- rust/libceed/src/operator.rs | 6 +----- rust/libceed/src/qfunction.rs | 8 +++----- rust/libceed/src/vector.rs | 6 +----- 5 files changed, 7 insertions(+), 25 deletions(-) diff --git a/rust/libceed/src/basis.rs b/rust/libceed/src/basis.rs index b308076ab6..2d7cdba637 100644 --- a/rust/libceed/src/basis.rs +++ b/rust/libceed/src/basis.rs @@ -326,11 +326,7 @@ impl<'a> Basis<'a> { // Error handling #[doc(hidden)] fn check_error(&self, ierr: i32) -> crate::Result { - let mut ptr = std::ptr::null_mut(); - unsafe { - bind_ceed::CeedBasisGetCeed(self.ptr, &mut ptr); - } - crate::check_error(ptr, ierr) + unsafe { crate::check_error(bind_ceed::CeedBasisReturnCeed(self.ptr), ierr) } } /// Apply basis evaluation from nodes to quadrature points or vice versa diff --git a/rust/libceed/src/elem_restriction.rs b/rust/libceed/src/elem_restriction.rs index e800f56c1e..500de95e80 100644 --- a/rust/libceed/src/elem_restriction.rs +++ b/rust/libceed/src/elem_restriction.rs @@ -320,11 +320,7 @@ impl<'a> ElemRestriction<'a> { // Error handling #[doc(hidden)] fn check_error(&self, ierr: i32) -> crate::Result { - let mut ptr = std::ptr::null_mut(); - unsafe { - bind_ceed::CeedElemRestrictionGetCeed(self.ptr, &mut ptr); - } - crate::check_error(ptr, ierr) + unsafe { crate::check_error(bind_ceed::CeedElemRestrictionReturnCeed(self.ptr), ierr) } } /// Create an Lvector for an ElemRestriction diff --git a/rust/libceed/src/operator.rs b/rust/libceed/src/operator.rs index 809c78f6e6..9f3506a493 100644 --- a/rust/libceed/src/operator.rs +++ b/rust/libceed/src/operator.rs @@ -442,11 +442,7 @@ impl<'a> OperatorCore<'a> { // Error handling #[doc(hidden)] fn check_error(&self, ierr: i32) -> crate::Result { - let mut ptr = std::ptr::null_mut(); - unsafe { - bind_ceed::CeedOperatorGetCeed(self.ptr, &mut ptr); - } - crate::check_error(ptr, ierr) + unsafe { crate::check_error(bind_ceed::CeedOperatorReturnCeed(self.ptr), ierr) } } // Common implementations diff --git a/rust/libceed/src/qfunction.rs b/rust/libceed/src/qfunction.rs index 0d32d01d28..a09c0ea918 100644 --- a/rust/libceed/src/qfunction.rs +++ b/rust/libceed/src/qfunction.rs @@ -442,11 +442,7 @@ impl<'a> QFunctionCore<'a> { // Error handling #[doc(hidden)] fn check_error(&self, ierr: i32) -> crate::Result { - let mut ptr = std::ptr::null_mut(); - unsafe { - bind_ceed::CeedQFunctionGetCeed(self.ptr, &mut ptr); - } - crate::check_error(ptr, ierr) + unsafe { crate::check_error(bind_ceed::CeedQFunctionReturnCeed(self.ptr), ierr) } } // Common implementation @@ -644,6 +640,8 @@ impl<'a> QFunction<'a> { ceed.check_error(ierr)?; ierr = unsafe { bind_ceed::CeedQFunctionSetContext(ptr, qf_ctx_ptr) }; ceed.check_error(ierr)?; + ierr = unsafe { bind_ceed::CeedQFunctionContextDestroy(&mut qf_ctx_ptr) }; + ceed.check_error(ierr)?; Ok(Self { qf_core: QFunctionCore { ptr, diff --git a/rust/libceed/src/vector.rs b/rust/libceed/src/vector.rs index c90d8a295a..9173070180 100644 --- a/rust/libceed/src/vector.rs +++ b/rust/libceed/src/vector.rs @@ -348,11 +348,7 @@ impl<'a> Vector<'a> { // Error handling #[doc(hidden)] fn check_error(&self, ierr: i32) -> crate::Result { - let mut ptr = std::ptr::null_mut(); - unsafe { - bind_ceed::CeedVectorGetCeed(self.ptr, &mut ptr); - } - crate::check_error(ptr, ierr) + unsafe { crate::check_error(bind_ceed::CeedVectorReturnCeed(self.ptr), ierr) } } /// Returns the length of a Vector From 11544396610b36de1cb2f0d18032eefe5c670568 Mon Sep 17 00:00:00 2001 From: Jeremy L Thompson Date: Wed, 23 Oct 2024 14:04:31 -0600 Subject: [PATCH 6/7] rust - tidier error checking --- rust/libceed/src/basis.rs | 8 +++++++- rust/libceed/src/elem_restriction.rs | 8 +++++++- rust/libceed/src/lib.rs | 7 +++++-- rust/libceed/src/operator.rs | 8 +++++++- rust/libceed/src/qfunction.rs | 8 +++++++- rust/libceed/src/vector.rs | 8 +++++++- 6 files changed, 40 insertions(+), 7 deletions(-) diff --git a/rust/libceed/src/basis.rs b/rust/libceed/src/basis.rs index 2d7cdba637..9b8ef714dd 100644 --- a/rust/libceed/src/basis.rs +++ b/rust/libceed/src/basis.rs @@ -323,10 +323,16 @@ impl<'a> Basis<'a> { }) } + // Raw Ceed for error handling + #[doc(hidden)] + fn ceed(&self) -> bind_ceed::Ceed { + unsafe { bind_ceed::CeedBasisReturnCeed(self.ptr) } + } + // Error handling #[doc(hidden)] fn check_error(&self, ierr: i32) -> crate::Result { - unsafe { crate::check_error(bind_ceed::CeedBasisReturnCeed(self.ptr), ierr) } + crate::check_error(|| self.ceed(), ierr) } /// Apply basis evaluation from nodes to quadrature points or vice versa diff --git a/rust/libceed/src/elem_restriction.rs b/rust/libceed/src/elem_restriction.rs index 500de95e80..e2779e876c 100644 --- a/rust/libceed/src/elem_restriction.rs +++ b/rust/libceed/src/elem_restriction.rs @@ -317,10 +317,16 @@ impl<'a> ElemRestriction<'a> { }) } + // Raw Ceed for error handling + #[doc(hidden)] + fn ceed(&self) -> bind_ceed::Ceed { + unsafe { bind_ceed::CeedElemRestrictionReturnCeed(self.ptr) } + } + // Error handling #[doc(hidden)] fn check_error(&self, ierr: i32) -> crate::Result { - unsafe { crate::check_error(bind_ceed::CeedElemRestrictionReturnCeed(self.ptr), ierr) } + crate::check_error(|| self.ceed(), ierr) } /// Create an Lvector for an ElemRestriction diff --git a/rust/libceed/src/lib.rs b/rust/libceed/src/lib.rs index bf7de98e07..36ad38a1c5 100755 --- a/rust/libceed/src/lib.rs +++ b/rust/libceed/src/lib.rs @@ -161,7 +161,10 @@ impl fmt::Display for Error { // Internal error checker // ----------------------------------------------------------------------------- #[doc(hidden)] -pub(crate) fn check_error(ceed_ptr: bind_ceed::Ceed, ierr: i32) -> Result { +pub(crate) fn check_error(ceed_ptr: F, ierr: i32) -> Result +where + F: FnOnce() -> bind_ceed::Ceed, +{ // Return early if code is clean if ierr == bind_ceed::CeedErrorType_CEED_ERROR_SUCCESS { return Ok(ierr); @@ -169,7 +172,7 @@ pub(crate) fn check_error(ceed_ptr: bind_ceed::Ceed, ierr: i32) -> Result { // Retrieve error message let mut ptr: *const std::os::raw::c_char = std::ptr::null_mut(); let c_str = unsafe { - bind_ceed::CeedGetErrorMessage(ceed_ptr, &mut ptr); + bind_ceed::CeedGetErrorMessage(ceed_ptr(), &mut ptr); std::ffi::CStr::from_ptr(ptr) }; let message = c_str.to_string_lossy().to_string(); diff --git a/rust/libceed/src/operator.rs b/rust/libceed/src/operator.rs index 9f3506a493..a1a5116ec2 100644 --- a/rust/libceed/src/operator.rs +++ b/rust/libceed/src/operator.rs @@ -439,10 +439,16 @@ impl<'a> fmt::Display for CompositeOperator<'a> { // Core functionality // ----------------------------------------------------------------------------- impl<'a> OperatorCore<'a> { + // Raw Ceed for error handling + #[doc(hidden)] + fn ceed(&self) -> bind_ceed::Ceed { + unsafe { bind_ceed::CeedOperatorReturnCeed(self.ptr) } + } + // Error handling #[doc(hidden)] fn check_error(&self, ierr: i32) -> crate::Result { - unsafe { crate::check_error(bind_ceed::CeedOperatorReturnCeed(self.ptr), ierr) } + crate::check_error(|| self.ceed(), ierr) } // Common implementations diff --git a/rust/libceed/src/qfunction.rs b/rust/libceed/src/qfunction.rs index a09c0ea918..48c27e781a 100644 --- a/rust/libceed/src/qfunction.rs +++ b/rust/libceed/src/qfunction.rs @@ -439,10 +439,16 @@ impl<'a> fmt::Display for QFunctionByName<'a> { // Core functionality // ----------------------------------------------------------------------------- impl<'a> QFunctionCore<'a> { + // Raw Ceed for error handling + #[doc(hidden)] + fn ceed(&self) -> bind_ceed::Ceed { + unsafe { bind_ceed::CeedQFunctionReturnCeed(self.ptr) } + } + // Error handling #[doc(hidden)] fn check_error(&self, ierr: i32) -> crate::Result { - unsafe { crate::check_error(bind_ceed::CeedQFunctionReturnCeed(self.ptr), ierr) } + crate::check_error(|| self.ceed(), ierr) } // Common implementation diff --git a/rust/libceed/src/vector.rs b/rust/libceed/src/vector.rs index 9173070180..0ffe84dfac 100644 --- a/rust/libceed/src/vector.rs +++ b/rust/libceed/src/vector.rs @@ -345,10 +345,16 @@ impl<'a> Vector<'a> { Ok(x) } + // Raw Ceed for error handling + #[doc(hidden)] + fn ceed(&self) -> bind_ceed::Ceed { + unsafe { bind_ceed::CeedVectorReturnCeed(self.ptr) } + } + // Error handling #[doc(hidden)] fn check_error(&self, ierr: i32) -> crate::Result { - unsafe { crate::check_error(bind_ceed::CeedVectorReturnCeed(self.ptr), ierr) } + crate::check_error(|| self.ceed(), ierr) } /// Returns the length of a Vector From 656ef1e5dc8a79430517f8559593f57085006221 Mon Sep 17 00:00:00 2001 From: Jeremy L Thompson Date: Wed, 23 Oct 2024 15:27:06 -0600 Subject: [PATCH 7/7] rust - directly pass ierr to check_error() --- rust/libceed/src/basis.rs | 36 +++++----- rust/libceed/src/elem_restriction.rs | 47 ++++++------ rust/libceed/src/lib.rs | 4 +- rust/libceed/src/operator.rs | 103 +++++++++++---------------- rust/libceed/src/qfunction.rs | 56 ++++++--------- rust/libceed/src/vector.rs | 67 +++++++---------- 6 files changed, 130 insertions(+), 183 deletions(-) diff --git a/rust/libceed/src/basis.rs b/rust/libceed/src/basis.rs index 9b8ef714dd..a3188f7bfb 100644 --- a/rust/libceed/src/basis.rs +++ b/rust/libceed/src/basis.rs @@ -152,7 +152,7 @@ impl<'a> Basis<'a> { i32::try_from(P1d).unwrap(), i32::try_from(Q1d).unwrap(), ); - let ierr = unsafe { + ceed.check_error(unsafe { bind_ceed::CeedBasisCreateTensorH1( ceed.ptr, dim, @@ -165,8 +165,7 @@ impl<'a> Basis<'a> { qweight1d.as_ptr(), &mut ptr, ) - }; - ceed.check_error(ierr)?; + })?; Ok(Self { ptr, _lifeline: PhantomData, @@ -196,10 +195,9 @@ impl<'a> Basis<'a> { i32::try_from(Q).unwrap(), qmode as bind_ceed::CeedQuadMode, ); - let ierr = unsafe { + ceed.check_error(unsafe { bind_ceed::CeedBasisCreateTensorH1Lagrange(ceed.ptr, dim, ncomp, P, Q, qmode, &mut ptr) - }; - ceed.check_error(ierr)?; + })?; Ok(Self { ptr, _lifeline: PhantomData, @@ -224,7 +222,7 @@ impl<'a> Basis<'a> { i32::try_from(nnodes).unwrap(), i32::try_from(nqpts).unwrap(), ); - let ierr = unsafe { + ceed.check_error(unsafe { bind_ceed::CeedBasisCreateH1( ceed.ptr, topo, @@ -237,8 +235,7 @@ impl<'a> Basis<'a> { qweight.as_ptr(), &mut ptr, ) - }; - ceed.check_error(ierr)?; + })?; Ok(Self { ptr, _lifeline: PhantomData, @@ -263,7 +260,7 @@ impl<'a> Basis<'a> { i32::try_from(nnodes).unwrap(), i32::try_from(nqpts).unwrap(), ); - let ierr = unsafe { + ceed.check_error(unsafe { bind_ceed::CeedBasisCreateHdiv( ceed.ptr, topo, @@ -276,8 +273,7 @@ impl<'a> Basis<'a> { qweight.as_ptr(), &mut ptr, ) - }; - ceed.check_error(ierr)?; + })?; Ok(Self { ptr, _lifeline: PhantomData, @@ -302,7 +298,7 @@ impl<'a> Basis<'a> { i32::try_from(nnodes).unwrap(), i32::try_from(nqpts).unwrap(), ); - let ierr = unsafe { + ceed.check_error(unsafe { bind_ceed::CeedBasisCreateHcurl( ceed.ptr, topo, @@ -315,8 +311,7 @@ impl<'a> Basis<'a> { qweight.as_ptr(), &mut ptr, ) - }; - ceed.check_error(ierr)?; + })?; Ok(Self { ptr, _lifeline: PhantomData, @@ -413,9 +408,9 @@ impl<'a> Basis<'a> { tmode as bind_ceed::CeedTransposeMode, emode as bind_ceed::CeedEvalMode, ); - let ierr = - unsafe { bind_ceed::CeedBasisApply(self.ptr, nelem, tmode, emode, u.ptr, v.ptr) }; - self.check_error(ierr) + self.check_error(unsafe { + bind_ceed::CeedBasisApply(self.ptr, nelem, tmode, emode, u.ptr, v.ptr) + }) } /// Returns the dimension for given Basis @@ -532,8 +527,9 @@ impl<'a> Basis<'a> { /// ``` pub fn create_projection(&self, to: &Self) -> crate::Result { let mut ptr = std::ptr::null_mut(); - let ierr = unsafe { bind_ceed::CeedBasisCreateProjection(self.ptr, to.ptr, &mut ptr) }; - self.check_error(ierr)?; + self.check_error(unsafe { + bind_ceed::CeedBasisCreateProjection(self.ptr, to.ptr, &mut ptr) + })?; Ok(Self { ptr, _lifeline: PhantomData, diff --git a/rust/libceed/src/elem_restriction.rs b/rust/libceed/src/elem_restriction.rs index e2779e876c..1bbf6b0643 100644 --- a/rust/libceed/src/elem_restriction.rs +++ b/rust/libceed/src/elem_restriction.rs @@ -172,7 +172,7 @@ impl<'a> ElemRestriction<'a> { isize::try_from(lsize).unwrap(), mtype as bind_ceed::CeedMemType, ); - let ierr = unsafe { + ceed.check_error(unsafe { bind_ceed::CeedElemRestrictionCreate( ceed.ptr, nelem, @@ -185,8 +185,7 @@ impl<'a> ElemRestriction<'a> { offsets.as_ptr(), &mut ptr, ) - }; - ceed.check_error(ierr)?; + })?; Ok(Self { ptr, _lifeline: PhantomData, @@ -220,7 +219,7 @@ impl<'a> ElemRestriction<'a> { isize::try_from(lsize).unwrap(), mtype as bind_ceed::CeedMemType, ); - let ierr = unsafe { + ceed.check_error(unsafe { bind_ceed::CeedElemRestrictionCreateOriented( ceed.ptr, nelem, @@ -234,8 +233,7 @@ impl<'a> ElemRestriction<'a> { orients.as_ptr(), &mut ptr, ) - }; - ceed.check_error(ierr)?; + })?; Ok(Self { ptr, _lifeline: PhantomData, @@ -262,7 +260,7 @@ impl<'a> ElemRestriction<'a> { isize::try_from(lsize).unwrap(), mtype as bind_ceed::CeedMemType, ); - let ierr = unsafe { + ceed.check_error(unsafe { bind_ceed::CeedElemRestrictionCreateCurlOriented( ceed.ptr, nelem, @@ -276,8 +274,7 @@ impl<'a> ElemRestriction<'a> { curlorients.as_ptr(), &mut ptr, ) - }; - ceed.check_error(ierr)?; + })?; Ok(Self { ptr, _lifeline: PhantomData, @@ -299,7 +296,7 @@ impl<'a> ElemRestriction<'a> { i32::try_from(ncomp).unwrap(), isize::try_from(lsize).unwrap(), ); - let ierr = unsafe { + ceed.check_error(unsafe { bind_ceed::CeedElemRestrictionCreateStrided( ceed.ptr, nelem, @@ -309,8 +306,7 @@ impl<'a> ElemRestriction<'a> { strides.as_ptr(), &mut ptr, ) - }; - ceed.check_error(ierr)?; + })?; Ok(Self { ptr, _lifeline: PhantomData, @@ -352,9 +348,9 @@ impl<'a> ElemRestriction<'a> { pub fn create_lvector<'b>(&self) -> crate::Result> { let mut ptr_lvector = std::ptr::null_mut(); let null = std::ptr::null_mut() as *mut _; - let ierr = - unsafe { bind_ceed::CeedElemRestrictionCreateVector(self.ptr, &mut ptr_lvector, null) }; - self.check_error(ierr)?; + self.check_error(unsafe { + bind_ceed::CeedElemRestrictionCreateVector(self.ptr, &mut ptr_lvector, null) + })?; Vector::from_raw(ptr_lvector) } @@ -381,9 +377,9 @@ impl<'a> ElemRestriction<'a> { pub fn create_evector<'b>(&self) -> crate::Result> { let mut ptr_evector = std::ptr::null_mut(); let null = std::ptr::null_mut() as *mut _; - let ierr = - unsafe { bind_ceed::CeedElemRestrictionCreateVector(self.ptr, null, &mut ptr_evector) }; - self.check_error(ierr)?; + self.check_error(unsafe { + bind_ceed::CeedElemRestrictionCreateVector(self.ptr, null, &mut ptr_evector) + })?; Vector::from_raw(ptr_evector) } @@ -411,10 +407,9 @@ impl<'a> ElemRestriction<'a> { pub fn create_vectors<'b, 'c>(&self) -> crate::Result<(Vector<'b>, Vector<'c>)> { let mut ptr_lvector = std::ptr::null_mut(); let mut ptr_evector = std::ptr::null_mut(); - let ierr = unsafe { + self.check_error(unsafe { bind_ceed::CeedElemRestrictionCreateVector(self.ptr, &mut ptr_lvector, &mut ptr_evector) - }; - self.check_error(ierr)?; + })?; let lvector = Vector::from_raw(ptr_lvector)?; let evector = Vector::from_raw(ptr_evector)?; Ok((lvector, evector)) @@ -460,7 +455,7 @@ impl<'a> ElemRestriction<'a> { /// ``` pub fn apply(&self, tmode: TransposeMode, u: &Vector, ru: &mut Vector) -> crate::Result { let tmode = tmode as bind_ceed::CeedTransposeMode; - let ierr = unsafe { + self.check_error(unsafe { bind_ceed::CeedElemRestrictionApply( self.ptr, tmode, @@ -468,8 +463,7 @@ impl<'a> ElemRestriction<'a> { ru.ptr, bind_ceed::CEED_REQUEST_IMMEDIATE, ) - }; - self.check_error(ierr) + }) } /// Returns the Lvector component stride @@ -630,8 +624,9 @@ impl<'a> ElemRestriction<'a> { /// # } /// ``` pub fn multiplicity(&self, mult: &mut Vector) -> crate::Result { - let ierr = unsafe { bind_ceed::CeedElemRestrictionGetMultiplicity(self.ptr, mult.ptr) }; - self.check_error(ierr) + self.check_error(unsafe { + bind_ceed::CeedElemRestrictionGetMultiplicity(self.ptr, mult.ptr) + }) } } diff --git a/rust/libceed/src/lib.rs b/rust/libceed/src/lib.rs index 36ad38a1c5..b248aa9fa7 100755 --- a/rust/libceed/src/lib.rs +++ b/rust/libceed/src/lib.rs @@ -228,8 +228,8 @@ impl Clone for Ceed { /// ``` fn clone(&self) -> Self { let mut ptr_clone = std::ptr::null_mut(); - let ierr = unsafe { bind_ceed::CeedReferenceCopy(self.ptr, &mut ptr_clone) }; - self.check_error(ierr).expect("failed to clone Ceed"); + self.check_error(unsafe { bind_ceed::CeedReferenceCopy(self.ptr, &mut ptr_clone) }) + .expect("failed to clone Ceed"); Self { ptr: ptr_clone } } } diff --git a/rust/libceed/src/operator.rs b/rust/libceed/src/operator.rs index a1a5116ec2..6f2cb11409 100644 --- a/rust/libceed/src/operator.rs +++ b/rust/libceed/src/operator.rs @@ -33,22 +33,21 @@ impl<'a> OperatorField<'a> { ) -> crate::Result { let vector = { let mut vector_ptr = std::ptr::null_mut(); - let ierr = unsafe { bind_ceed::CeedOperatorFieldGetVector(ptr, &mut vector_ptr) }; - ceed.check_error(ierr)?; + ceed.check_error(unsafe { + bind_ceed::CeedOperatorFieldGetVector(ptr, &mut vector_ptr) + })?; crate::Vector::from_raw(vector_ptr)? }; let elem_restriction = { let mut elem_restriction_ptr = std::ptr::null_mut(); - let ierr = unsafe { + ceed.check_error(unsafe { bind_ceed::CeedOperatorFieldGetElemRestriction(ptr, &mut elem_restriction_ptr) - }; - ceed.check_error(ierr)?; + })?; crate::ElemRestriction::from_raw(elem_restriction_ptr)? }; let basis = { let mut basis_ptr = std::ptr::null_mut(); - let ierr = unsafe { bind_ceed::CeedOperatorFieldGetBasis(ptr, &mut basis_ptr) }; - ceed.check_error(ierr)?; + ceed.check_error(unsafe { bind_ceed::CeedOperatorFieldGetBasis(ptr, &mut basis_ptr) })?; crate::Basis::from_raw(basis_ptr)? }; Ok(Self { @@ -453,88 +452,80 @@ impl<'a> OperatorCore<'a> { // Common implementations pub fn check(&self) -> crate::Result { - let ierr = unsafe { bind_ceed::CeedOperatorCheckReady(self.ptr) }; - self.check_error(ierr) + self.check_error(unsafe { bind_ceed::CeedOperatorCheckReady(self.ptr) }) } pub fn name(&self, name: &str) -> crate::Result { let name_c = CString::new(name).expect("CString::new failed"); - let ierr = unsafe { bind_ceed::CeedOperatorSetName(self.ptr, name_c.as_ptr()) }; - self.check_error(ierr) + self.check_error(unsafe { bind_ceed::CeedOperatorSetName(self.ptr, name_c.as_ptr()) }) } pub fn apply(&self, input: &Vector, output: &mut Vector) -> crate::Result { - let ierr = unsafe { + self.check_error(unsafe { bind_ceed::CeedOperatorApply( self.ptr, input.ptr, output.ptr, bind_ceed::CEED_REQUEST_IMMEDIATE, ) - }; - self.check_error(ierr) + }) } pub fn apply_add(&self, input: &Vector, output: &mut Vector) -> crate::Result { - let ierr = unsafe { + self.check_error(unsafe { bind_ceed::CeedOperatorApplyAdd( self.ptr, input.ptr, output.ptr, bind_ceed::CEED_REQUEST_IMMEDIATE, ) - }; - self.check_error(ierr) + }) } pub fn linear_assemble_diagonal(&self, assembled: &mut Vector) -> crate::Result { - let ierr = unsafe { + self.check_error(unsafe { bind_ceed::CeedOperatorLinearAssembleDiagonal( self.ptr, assembled.ptr, bind_ceed::CEED_REQUEST_IMMEDIATE, ) - }; - self.check_error(ierr) + }) } pub fn linear_assemble_add_diagonal(&self, assembled: &mut Vector) -> crate::Result { - let ierr = unsafe { + self.check_error(unsafe { bind_ceed::CeedOperatorLinearAssembleAddDiagonal( self.ptr, assembled.ptr, bind_ceed::CEED_REQUEST_IMMEDIATE, ) - }; - self.check_error(ierr) + }) } pub fn linear_assemble_point_block_diagonal( &self, assembled: &mut Vector, ) -> crate::Result { - let ierr = unsafe { + self.check_error(unsafe { bind_ceed::CeedOperatorLinearAssemblePointBlockDiagonal( self.ptr, assembled.ptr, bind_ceed::CEED_REQUEST_IMMEDIATE, ) - }; - self.check_error(ierr) + }) } pub fn linear_assemble_add_point_block_diagonal( &self, assembled: &mut Vector, ) -> crate::Result { - let ierr = unsafe { + self.check_error(unsafe { bind_ceed::CeedOperatorLinearAssembleAddPointBlockDiagonal( self.ptr, assembled.ptr, bind_ceed::CEED_REQUEST_IMMEDIATE, ) - }; - self.check_error(ierr) + }) } } @@ -550,7 +541,7 @@ impl<'a> Operator<'a> { dqfT: impl Into>, ) -> crate::Result { let mut ptr = std::ptr::null_mut(); - let ierr = unsafe { + ceed.check_error(unsafe { bind_ceed::CeedOperatorCreate( ceed.ptr, qf.into().to_raw(), @@ -558,8 +549,7 @@ impl<'a> Operator<'a> { dqfT.into().to_raw(), &mut ptr, ) - }; - ceed.check_error(ierr)?; + })?; Ok(Self { op_core: OperatorCore { ptr, @@ -814,7 +804,7 @@ impl<'a> Operator<'a> { ) -> crate::Result { let fieldname = CString::new(fieldname).expect("CString::new failed"); let fieldname = fieldname.as_ptr() as *const i8; - let ierr = unsafe { + self.op_core.check_error(unsafe { bind_ceed::CeedOperatorSetField( self.op_core.ptr, fieldname, @@ -822,8 +812,7 @@ impl<'a> Operator<'a> { b.into().to_raw(), v.into().to_raw(), ) - }; - self.op_core.check_error(ierr)?; + })?; Ok(self) } @@ -866,7 +855,7 @@ impl<'a> Operator<'a> { // Get array of raw C pointers for inputs let mut num_inputs = 0; let mut inputs_ptr = std::ptr::null_mut(); - let ierr = unsafe { + self.op_core.check_error(unsafe { bind_ceed::CeedOperatorGetFields( self.op_core.ptr, &mut num_inputs, @@ -874,8 +863,7 @@ impl<'a> Operator<'a> { std::ptr::null_mut() as *mut bind_ceed::CeedInt, std::ptr::null_mut() as *mut *mut bind_ceed::CeedOperatorField, ) - }; - self.op_core.check_error(ierr)?; + })?; // Convert raw C pointers to fixed length slice let inputs_slice = unsafe { std::slice::from_raw_parts( @@ -885,11 +873,10 @@ impl<'a> Operator<'a> { }; // And finally build vec let ceed = { + let ceed_raw = self.op_core.ceed(); let mut ptr = std::ptr::null_mut(); - let mut ptr_copy = std::ptr::null_mut(); unsafe { - bind_ceed::CeedOperatorGetCeed(self.op_core.ptr, &mut ptr); - bind_ceed::CeedReferenceCopy(ptr, &mut ptr_copy); // refcount + bind_ceed::CeedReferenceCopy(ceed_raw, &mut ptr); // refcount } crate::Ceed { ptr } }; @@ -938,7 +925,7 @@ impl<'a> Operator<'a> { // Get array of raw C pointers for outputs let mut num_outputs = 0; let mut outputs_ptr = std::ptr::null_mut(); - let ierr = unsafe { + self.op_core.check_error(unsafe { bind_ceed::CeedOperatorGetFields( self.op_core.ptr, std::ptr::null_mut() as *mut bind_ceed::CeedInt, @@ -946,8 +933,7 @@ impl<'a> Operator<'a> { &mut num_outputs, &mut outputs_ptr, ) - }; - self.op_core.check_error(ierr)?; + })?; // Convert raw C pointers to fixed length slice let outputs_slice = unsafe { std::slice::from_raw_parts( @@ -957,11 +943,10 @@ impl<'a> Operator<'a> { }; // And finally build vec let ceed = { + let ceed_raw = self.op_core.ceed(); let mut ptr = std::ptr::null_mut(); - let mut ptr_copy = std::ptr::null_mut(); unsafe { - bind_ceed::CeedOperatorGetCeed(self.op_core.ptr, &mut ptr); - bind_ceed::CeedReferenceCopy(ptr, &mut ptr_copy); // refcount + bind_ceed::CeedReferenceCopy(ceed_raw, &mut ptr); // refcount } crate::Ceed { ptr } }; @@ -1724,7 +1709,7 @@ impl<'a> Operator<'a> { let mut ptr_coarse = std::ptr::null_mut(); let mut ptr_prolong = std::ptr::null_mut(); let mut ptr_restrict = std::ptr::null_mut(); - let ierr = unsafe { + self.op_core.check_error(unsafe { bind_ceed::CeedOperatorMultigridLevelCreate( self.op_core.ptr, p_mult_fine.ptr, @@ -1734,8 +1719,7 @@ impl<'a> Operator<'a> { &mut ptr_prolong, &mut ptr_restrict, ) - }; - self.op_core.check_error(ierr)?; + })?; let op_coarse = Operator::from_raw(ptr_coarse)?; let op_prolong = Operator::from_raw(ptr_prolong)?; let op_restrict = Operator::from_raw(ptr_restrict)?; @@ -1914,7 +1898,7 @@ impl<'a> Operator<'a> { let mut ptr_coarse = std::ptr::null_mut(); let mut ptr_prolong = std::ptr::null_mut(); let mut ptr_restrict = std::ptr::null_mut(); - let ierr = unsafe { + self.op_core.check_error(unsafe { bind_ceed::CeedOperatorMultigridLevelCreateTensorH1( self.op_core.ptr, p_mult_fine.ptr, @@ -1925,8 +1909,7 @@ impl<'a> Operator<'a> { &mut ptr_prolong, &mut ptr_restrict, ) - }; - self.op_core.check_error(ierr)?; + })?; let op_coarse = Operator::from_raw(ptr_coarse)?; let op_prolong = Operator::from_raw(ptr_prolong)?; let op_restrict = Operator::from_raw(ptr_restrict)?; @@ -2105,7 +2088,7 @@ impl<'a> Operator<'a> { let mut ptr_coarse = std::ptr::null_mut(); let mut ptr_prolong = std::ptr::null_mut(); let mut ptr_restrict = std::ptr::null_mut(); - let ierr = unsafe { + self.op_core.check_error(unsafe { bind_ceed::CeedOperatorMultigridLevelCreateH1( self.op_core.ptr, p_mult_fine.ptr, @@ -2116,8 +2099,7 @@ impl<'a> Operator<'a> { &mut ptr_prolong, &mut ptr_restrict, ) - }; - self.op_core.check_error(ierr)?; + })?; let op_coarse = Operator::from_raw(ptr_coarse)?; let op_prolong = Operator::from_raw(ptr_prolong)?; let op_restrict = Operator::from_raw(ptr_restrict)?; @@ -2132,8 +2114,7 @@ impl<'a> CompositeOperator<'a> { // Constructor pub fn create(ceed: &crate::Ceed) -> crate::Result { let mut ptr = std::ptr::null_mut(); - let ierr = unsafe { bind_ceed::CeedCompositeOperatorCreate(ceed.ptr, &mut ptr) }; - ceed.check_error(ierr)?; + ceed.check_error(unsafe { bind_ceed::CeedCompositeOperatorCreate(ceed.ptr, &mut ptr) })?; Ok(Self { op_core: OperatorCore { ptr, @@ -2414,9 +2395,9 @@ impl<'a> CompositeOperator<'a> { /// ``` #[allow(unused_mut)] pub fn sub_operator(mut self, subop: &Operator) -> crate::Result { - let ierr = - unsafe { bind_ceed::CeedCompositeOperatorAddSub(self.op_core.ptr, subop.op_core.ptr) }; - self.op_core.check_error(ierr)?; + self.op_core.check_error(unsafe { + bind_ceed::CeedCompositeOperatorAddSub(self.op_core.ptr, subop.op_core.ptr) + })?; Ok(self) } diff --git a/rust/libceed/src/qfunction.rs b/rust/libceed/src/qfunction.rs index 48c27e781a..f5b746eefd 100644 --- a/rust/libceed/src/qfunction.rs +++ b/rust/libceed/src/qfunction.rs @@ -462,17 +462,16 @@ impl<'a> QFunctionCore<'a> { v_c[i] = v[i].ptr; } let Q = i32::try_from(Q).unwrap(); - let ierr = unsafe { + self.check_error(unsafe { bind_ceed::CeedQFunctionApply(self.ptr, Q, u_c.as_mut_ptr(), v_c.as_mut_ptr()) - }; - self.check_error(ierr) + }) } pub fn inputs(&self) -> crate::Result<&[crate::QFunctionField]> { // Get array of raw C pointers for inputs let mut num_inputs = 0; let mut inputs_ptr = std::ptr::null_mut(); - let ierr = unsafe { + self.check_error(unsafe { bind_ceed::CeedQFunctionGetFields( self.ptr, &mut num_inputs, @@ -480,8 +479,7 @@ impl<'a> QFunctionCore<'a> { std::ptr::null_mut() as *mut bind_ceed::CeedInt, std::ptr::null_mut() as *mut *mut bind_ceed::CeedQFunctionField, ) - }; - self.check_error(ierr)?; + })?; // Convert raw C pointers to fixed length slice let inputs_slice = unsafe { std::slice::from_raw_parts( @@ -496,7 +494,7 @@ impl<'a> QFunctionCore<'a> { // Get array of raw C pointers for outputs let mut num_outputs = 0; let mut outputs_ptr = std::ptr::null_mut(); - let ierr = unsafe { + self.check_error(unsafe { bind_ceed::CeedQFunctionGetFields( self.ptr, std::ptr::null_mut() as *mut bind_ceed::CeedInt, @@ -504,8 +502,7 @@ impl<'a> QFunctionCore<'a> { &mut num_outputs, &mut outputs_ptr, ) - }; - self.check_error(ierr)?; + })?; // Convert raw C pointers to fixed length slice let outputs_slice = unsafe { std::slice::from_raw_parts( @@ -611,7 +608,7 @@ impl<'a> QFunction<'a> { // Create QFunction let vlength = i32::try_from(vlength).unwrap(); - let mut ierr = unsafe { + ceed.check_error(unsafe { bind_ceed::CeedQFunctionCreateInterior( ceed.ptr, vlength, @@ -619,14 +616,14 @@ impl<'a> QFunction<'a> { source_c.as_ptr(), &mut ptr, ) - }; - ceed.check_error(ierr)?; + })?; // Set closure let mut qf_ctx_ptr = std::ptr::null_mut(); - ierr = unsafe { bind_ceed::CeedQFunctionContextCreate(ceed.ptr, &mut qf_ctx_ptr) }; - ceed.check_error(ierr)?; - ierr = unsafe { + ceed.check_error(unsafe { + bind_ceed::CeedQFunctionContextCreate(ceed.ptr, &mut qf_ctx_ptr) + })?; + ceed.check_error(unsafe { bind_ceed::CeedQFunctionContextSetData( qf_ctx_ptr, crate::MemType::Host as bind_ceed::CeedMemType, @@ -634,20 +631,16 @@ impl<'a> QFunction<'a> { std::mem::size_of::(), std::mem::transmute(trampoline_data.as_ref()), ) - }; - ceed.check_error(ierr)?; - ierr = unsafe { + })?; + ceed.check_error(unsafe { bind_ceed::CeedQFunctionContextSetDataDestroy( qf_ctx_ptr, crate::MemType::Host as bind_ceed::CeedMemType, Some(destroy_trampoline), ) - }; - ceed.check_error(ierr)?; - ierr = unsafe { bind_ceed::CeedQFunctionSetContext(ptr, qf_ctx_ptr) }; - ceed.check_error(ierr)?; - ierr = unsafe { bind_ceed::CeedQFunctionContextDestroy(&mut qf_ctx_ptr) }; - ceed.check_error(ierr)?; + })?; + ceed.check_error(unsafe { bind_ceed::CeedQFunctionSetContext(ptr, qf_ctx_ptr) })?; + ceed.check_error(unsafe { bind_ceed::CeedQFunctionContextDestroy(&mut qf_ctx_ptr) })?; Ok(Self { qf_core: QFunctionCore { ptr, @@ -767,10 +760,9 @@ impl<'a> QFunction<'a> { i32::try_from(size).unwrap(), emode as bind_ceed::CeedEvalMode, ); - let ierr = unsafe { + self.qf_core.check_error(unsafe { bind_ceed::CeedQFunctionAddInput(self.qf_core.ptr, name_c.as_ptr(), size, emode) - }; - self.qf_core.check_error(ierr)?; + })?; Ok(self) } @@ -817,10 +809,9 @@ impl<'a> QFunction<'a> { i32::try_from(size).unwrap(), emode as bind_ceed::CeedEvalMode, ); - let ierr = unsafe { + self.qf_core.check_error(unsafe { bind_ceed::CeedQFunctionAddOutput(self.qf_core.ptr, name_c.as_ptr(), size, emode) - }; - self.qf_core.check_error(ierr)?; + })?; Ok(self) } @@ -894,10 +885,9 @@ impl<'a> QFunctionByName<'a> { pub fn create(ceed: &crate::Ceed, name: &str) -> crate::Result { let name_c = CString::new(name).expect("CString::new failed"); let mut ptr = std::ptr::null_mut(); - let ierr = unsafe { + ceed.check_error(unsafe { bind_ceed::CeedQFunctionCreateInteriorByName(ceed.ptr, name_c.as_ptr(), &mut ptr) - }; - ceed.check_error(ierr)?; + })?; Ok(Self { qf_core: QFunctionCore { ptr, diff --git a/rust/libceed/src/vector.rs b/rust/libceed/src/vector.rs index 0ffe84dfac..a6fd181907 100644 --- a/rust/libceed/src/vector.rs +++ b/rust/libceed/src/vector.rs @@ -157,16 +157,14 @@ impl<'a> VectorSliceWrapper<'a> { crate::MemType::Host as bind_ceed::CeedMemType, crate::CopyMode::UsePointer as bind_ceed::CeedCopyMode, ); - let ierr = unsafe { + vec.check_error(unsafe { bind_ceed::CeedVectorSetArray( vec.ptr, host, copy_mode, slice.as_ptr() as *mut crate::Scalar, ) - }; - vec.check_error(ierr)?; - + })?; Ok(Self { vector: crate::Vector::from_raw(vec.ptr_copy_mut()?)?, _slice: slice, @@ -247,8 +245,7 @@ impl<'a> Vector<'a> { pub fn create(ceed: &crate::Ceed, n: usize) -> crate::Result { let n = isize::try_from(n).unwrap(); let mut ptr = std::ptr::null_mut(); - let ierr = unsafe { bind_ceed::CeedVectorCreate(ceed.ptr, n, &mut ptr) }; - ceed.check_error(ierr)?; + ceed.check_error(unsafe { bind_ceed::CeedVectorCreate(ceed.ptr, n, &mut ptr) })?; Ok(Self { ptr, _lifeline: PhantomData, @@ -264,8 +261,7 @@ impl<'a> Vector<'a> { fn ptr_copy_mut(&mut self) -> crate::Result { let mut ptr_copy = std::ptr::null_mut(); - let ierr = unsafe { bind_ceed::CeedVectorReferenceCopy(self.ptr, &mut ptr_copy) }; - self.check_error(ierr)?; + self.check_error(unsafe { bind_ceed::CeedVectorReferenceCopy(self.ptr, &mut ptr_copy) })?; Ok(ptr_copy) } @@ -291,8 +287,7 @@ impl<'a> Vector<'a> { /// ``` /// ``` pub fn copy_from(&mut self, vec_source: &crate::Vector) -> crate::Result { - let ierr = unsafe { bind_ceed::CeedVectorCopy(vec_source.ptr, self.ptr) }; - self.check_error(ierr) + self.check_error(unsafe { bind_ceed::CeedVectorCopy(vec_source.ptr, self.ptr) }) } /// Create a Vector from a slice @@ -340,8 +335,7 @@ impl<'a> Vector<'a> { crate::CopyMode::UsePointer as bind_ceed::CeedCopyMode, ); let v = v.as_ptr() as *mut crate::Scalar; - let ierr = unsafe { bind_ceed::CeedVectorSetArray(x.ptr, host, user_pointer, v) }; - ceed.check_error(ierr)?; + ceed.check_error(unsafe { bind_ceed::CeedVectorSetArray(x.ptr, host, user_pointer, v) })?; Ok(x) } @@ -414,8 +408,7 @@ impl<'a> Vector<'a> { /// # } /// ``` pub fn set_value(&mut self, value: crate::Scalar) -> crate::Result { - let ierr = unsafe { bind_ceed::CeedVectorSetValue(self.ptr, value) }; - self.check_error(ierr) + self.check_error(unsafe { bind_ceed::CeedVectorSetValue(self.ptr, value) }) } /// Set values from a slice of the same length @@ -443,15 +436,14 @@ impl<'a> Vector<'a> { crate::MemType::Host as bind_ceed::CeedMemType, crate::CopyMode::CopyValues as bind_ceed::CeedCopyMode, ); - let ierr = unsafe { + self.check_error(unsafe { bind_ceed::CeedVectorSetArray( self.ptr, host, copy_mode, slice.as_ptr() as *mut crate::Scalar, ) - }; - self.check_error(ierr) + }) } /// Wrap a mutable slice in a Vector of the same length @@ -530,9 +522,9 @@ impl<'a> Vector<'a> { /// # } /// ``` pub fn sync(&self, mtype: crate::MemType) -> crate::Result { - let ierr = - unsafe { bind_ceed::CeedVectorSyncArray(self.ptr, mtype as bind_ceed::CeedMemType) }; - self.check_error(ierr) + self.check_error(unsafe { + bind_ceed::CeedVectorSyncArray(self.ptr, mtype as bind_ceed::CeedMemType) + }) } /// Create an immutable view @@ -603,10 +595,9 @@ impl<'a> Vector<'a> { /// ``` pub fn norm(&self, ntype: crate::NormType) -> crate::Result { let mut res: crate::Scalar = 0.0; - let ierr = unsafe { + self.check_error(unsafe { bind_ceed::CeedVectorNorm(self.ptr, ntype as bind_ceed::CeedNormType, &mut res) - }; - self.check_error(ierr)?; + })?; Ok(res) } @@ -631,8 +622,7 @@ impl<'a> Vector<'a> { /// ``` #[allow(unused_mut)] pub fn scale(mut self, alpha: crate::Scalar) -> crate::Result { - let ierr = unsafe { bind_ceed::CeedVectorScale(self.ptr, alpha) }; - self.check_error(ierr)?; + self.check_error(unsafe { bind_ceed::CeedVectorScale(self.ptr, alpha) })?; Ok(self) } @@ -659,8 +649,7 @@ impl<'a> Vector<'a> { /// ``` #[allow(unused_mut)] pub fn axpy(mut self, alpha: crate::Scalar, x: &crate::Vector) -> crate::Result { - let ierr = unsafe { bind_ceed::CeedVectorAXPY(self.ptr, alpha, x.ptr) }; - self.check_error(ierr)?; + self.check_error(unsafe { bind_ceed::CeedVectorAXPY(self.ptr, alpha, x.ptr) })?; Ok(self) } @@ -693,8 +682,7 @@ impl<'a> Vector<'a> { beta: crate::Scalar, x: &crate::Vector, ) -> crate::Result { - let ierr = unsafe { bind_ceed::CeedVectorAXPBY(self.ptr, alpha, beta, x.ptr) }; - self.check_error(ierr)?; + self.check_error(unsafe { bind_ceed::CeedVectorAXPBY(self.ptr, alpha, beta, x.ptr) })?; Ok(self) } @@ -722,8 +710,7 @@ impl<'a> Vector<'a> { /// ``` #[allow(unused_mut)] pub fn pointwise_mult(mut self, x: &crate::Vector, y: &crate::Vector) -> crate::Result { - let ierr = unsafe { bind_ceed::CeedVectorPointwiseMult(self.ptr, x.ptr, y.ptr) }; - self.check_error(ierr)?; + self.check_error(unsafe { bind_ceed::CeedVectorPointwiseMult(self.ptr, x.ptr, y.ptr) })?; Ok(self) } @@ -749,8 +736,7 @@ impl<'a> Vector<'a> { /// ``` #[allow(unused_mut)] pub fn pointwise_scale(mut self, x: &crate::Vector) -> crate::Result { - let ierr = unsafe { bind_ceed::CeedVectorPointwiseMult(self.ptr, self.ptr, x.ptr) }; - self.check_error(ierr)?; + self.check_error(unsafe { bind_ceed::CeedVectorPointwiseMult(self.ptr, self.ptr, x.ptr) })?; Ok(self) } @@ -771,8 +757,9 @@ impl<'a> Vector<'a> { /// ``` #[allow(unused_mut)] pub fn pointwise_square(mut self) -> crate::Result { - let ierr = unsafe { bind_ceed::CeedVectorPointwiseMult(self.ptr, self.ptr, self.ptr) }; - self.check_error(ierr)?; + self.check_error(unsafe { + bind_ceed::CeedVectorPointwiseMult(self.ptr, self.ptr, self.ptr) + })?; Ok(self) } } @@ -793,14 +780,13 @@ impl<'a> VectorView<'a> { /// Construct a VectorView from a Vector reference fn new(vec: &'a Vector) -> crate::Result { let mut array = std::ptr::null(); - let ierr = unsafe { + vec.check_error(unsafe { bind_ceed::CeedVectorGetArrayRead( vec.ptr, crate::MemType::Host as bind_ceed::CeedMemType, &mut array, ) - }; - vec.check_error(ierr)?; + })?; Ok(Self { vec: vec, array: array, @@ -846,14 +832,13 @@ impl<'a> VectorViewMut<'a> { /// Construct a VectorViewMut from a Vector reference fn new(vec: &'a mut Vector) -> crate::Result { let mut ptr = std::ptr::null_mut(); - let ierr = unsafe { + vec.check_error(unsafe { bind_ceed::CeedVectorGetArray( vec.ptr, crate::MemType::Host as bind_ceed::CeedMemType, &mut ptr, ) - }; - vec.check_error(ierr)?; + })?; Ok(Self { vec: vec, array: ptr,