Skip to content

Commit

Permalink
sycl - destroy objects returned from field getters
Browse files Browse the repository at this point in the history
  • Loading branch information
jeremylt committed Sep 6, 2024
1 parent 5862f7b commit de31b36
Show file tree
Hide file tree
Showing 5 changed files with 165 additions and 140 deletions.
20 changes: 7 additions & 13 deletions backends/cuda-ref/ceed-cuda-ref-operator.c
Original file line number Diff line number Diff line change
Expand Up @@ -337,7 +337,6 @@ static inline int CeedOperatorSetupInputs_Cuda(CeedInt num_input_fields, CeedQFu
bool is_active;
CeedEvalMode eval_mode;
CeedVector vec;
CeedElemRestriction elem_rstr;

// Get input vector
CeedCallBackend(CeedOperatorFieldGetVector(op_input_fields[i], &vec));
Expand All @@ -350,8 +349,6 @@ static inline int CeedOperatorSetupInputs_Cuda(CeedInt num_input_fields, CeedQFu
CeedCallBackend(CeedQFunctionFieldGetEvalMode(qf_input_fields[i], &eval_mode));
if (eval_mode == CEED_EVAL_WEIGHT) { // Skip
} else {
// Get input element restriction
CeedCallBackend(CeedOperatorFieldGetElemRestriction(op_input_fields[i], &elem_rstr));
// Restrict, if necessary
if (!impl->e_vecs[i]) {
// No restriction for this field; read data directly from vec.
Expand All @@ -361,12 +358,15 @@ static inline int CeedOperatorSetupInputs_Cuda(CeedInt num_input_fields, CeedQFu

CeedCallBackend(CeedVectorGetState(vec, &state));
if ((state != impl->input_states[i] || vec == in_vec) && !impl->skip_rstr_in[i]) {
CeedElemRestriction elem_rstr;

CeedCallBackend(CeedOperatorFieldGetElemRestriction(op_input_fields[i], &elem_rstr));
CeedCallBackend(CeedElemRestrictionApply(elem_rstr, CEED_NOTRANSPOSE, vec, impl->e_vecs[i], request));
CeedCallBackend(CeedElemRestrictionDestroy(&elem_rstr));
}
impl->input_states[i] = state;
// Get evec
CeedCallBackend(CeedVectorGetArrayRead(impl->e_vecs[i], CEED_MEM_DEVICE, (const CeedScalar **)&e_data[i]));
CeedCallBackend(CeedElemRestrictionDestroy(&elem_rstr));
}
}
if (!is_active) CeedCallBackend(CeedVectorDestroy(&vec));
Expand All @@ -381,10 +381,8 @@ static inline int CeedOperatorInputBasis_Cuda(CeedInt num_elem, CeedQFunctionFie
CeedInt num_input_fields, const bool skip_active, CeedScalar *e_data[2 * CEED_FIELD_MAX],
CeedOperator_Cuda *impl) {
for (CeedInt i = 0; i < num_input_fields; i++) {
CeedInt elem_size, size;
CeedEvalMode eval_mode;
CeedElemRestriction elem_rstr;
CeedBasis basis;
CeedEvalMode eval_mode;
CeedBasis basis;

// Skip active input
if (skip_active) {
Expand All @@ -396,12 +394,8 @@ static inline int CeedOperatorInputBasis_Cuda(CeedInt num_elem, CeedQFunctionFie
CeedCallBackend(CeedVectorDestroy(&vec));
if (is_active) continue;
}
// Get elem_size, eval_mode, size
CeedCallBackend(CeedOperatorFieldGetElemRestriction(op_input_fields[i], &elem_rstr));
CeedCallBackend(CeedElemRestrictionGetElementSize(elem_rstr, &elem_size));
CeedCallBackend(CeedElemRestrictionDestroy(&elem_rstr));
// Get eval_mode
CeedCallBackend(CeedQFunctionFieldGetEvalMode(qf_input_fields[i], &eval_mode));
CeedCallBackend(CeedQFunctionFieldGetSize(qf_input_fields[i], &size));
// Basis action
switch (eval_mode) {
case CEED_EVAL_NONE:
Expand Down
20 changes: 7 additions & 13 deletions backends/hip-ref/ceed-hip-ref-operator.c
Original file line number Diff line number Diff line change
Expand Up @@ -349,9 +349,6 @@ static inline int CeedOperatorSetupInputs_Hip(CeedInt num_input_fields, CeedQFun
CeedCallBackend(CeedQFunctionFieldGetEvalMode(qf_input_fields[i], &eval_mode));
if (eval_mode == CEED_EVAL_WEIGHT) { // Skip
} else {
// Get input element restriction
CeedCallBackend(CeedOperatorFieldGetElemRestriction(op_input_fields[i], &elem_rstr));
if (vec == CEED_VECTOR_ACTIVE) vec = in_vec;
// Restrict, if necessary
if (!impl->e_vecs[i]) {
// No restriction for this field; read data directly from vec.
Expand All @@ -361,13 +358,16 @@ static inline int CeedOperatorSetupInputs_Hip(CeedInt num_input_fields, CeedQFun

CeedCallBackend(CeedVectorGetState(vec, &state));
if ((state != impl->input_states[i] || vec == in_vec) && !impl->skip_rstr_in[i]) {
CeedElemRestriction elem_rstr;

CeedCallBackend(CeedOperatorFieldGetElemRestriction(op_input_fields[i], &elem_rstr));
CeedCallBackend(CeedElemRestrictionApply(elem_rstr, CEED_NOTRANSPOSE, vec, impl->e_vecs[i], request));
CeedCallBackend(CeedElemRestrictionDestroy(&elem_rstr));
}
impl->input_states[i] = state;
// Get evec
CeedCallBackend(CeedVectorGetArrayRead(impl->e_vecs[i], CEED_MEM_DEVICE, (const CeedScalar **)&e_data[i]));
}
CeedCallBackend(CeedElemRestrictionDestroy(&elem_rstr));
}
if (!is_active) CeedCallBackend(CeedVectorDestroy(&vec));
}
Expand All @@ -381,10 +381,8 @@ static inline int CeedOperatorInputBasis_Hip(CeedInt num_elem, CeedQFunctionFiel
CeedInt num_input_fields, const bool skip_active, CeedScalar *e_data[2 * CEED_FIELD_MAX],
CeedOperator_Hip *impl) {
for (CeedInt i = 0; i < num_input_fields; i++) {
CeedInt elem_size, size;
CeedEvalMode eval_mode;
CeedElemRestriction elem_rstr;
CeedBasis basis;
CeedEvalMode eval_mode;
CeedBasis basis;

// Skip active input
if (skip_active) {
Expand All @@ -396,12 +394,8 @@ static inline int CeedOperatorInputBasis_Hip(CeedInt num_elem, CeedQFunctionFiel
CeedCallBackend(CeedVectorDestroy(&vec));
if (is_active) continue;
}
// Get elem_size, eval_mode, size
CeedCallBackend(CeedOperatorFieldGetElemRestriction(op_input_fields[i], &elem_rstr));
CeedCallBackend(CeedElemRestrictionGetElementSize(elem_rstr, &elem_size));
CeedCallBackend(CeedElemRestrictionDestroy(&elem_rstr));
// Get eval_mode
CeedCallBackend(CeedQFunctionFieldGetEvalMode(qf_input_fields[i], &eval_mode));
CeedCallBackend(CeedQFunctionFieldGetSize(qf_input_fields[i], &size));
// Basis action
switch (eval_mode) {
case CEED_EVAL_NONE:
Expand Down
16 changes: 15 additions & 1 deletion backends/sycl-gen/ceed-sycl-gen-operator-build.sycl.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -155,12 +155,12 @@ extern "C" int CeedOperatorBuildKernel_Sycl_gen(CeedOperator op) {
// LCOV_EXCL_STOP
}
}
CeedCallBackend(CeedBasisDestroy(&basis));
}
// Check output bases for Q_1d, dim as well
// The only input basis might be CEED_BASIS_NONE
for (CeedInt i = 0; i < num_output_fields; i++) {
CeedCallBackend(CeedOperatorFieldGetBasis(op_output_fields[i], &basis));

if (basis != CEED_BASIS_NONE) {
bool is_tensor;

Expand All @@ -178,6 +178,7 @@ extern "C" int CeedOperatorBuildKernel_Sycl_gen(CeedOperator op) {
// LCOV_EXCL_STOP
}
}
CeedCallBackend(CeedBasisDestroy(&basis));
}
impl->dim = dim;
impl->Q_1d = Q_1d;
Expand All @@ -196,6 +197,7 @@ extern "C" int CeedOperatorBuildKernel_Sycl_gen(CeedOperator op) {
CeedCallBackend(CeedBasisGetData(basis, &basis_impl));
use_collograd_parallelization = basis_impl->d_collo_grad_1d && (was_grad_found ? use_collograd_parallelization : true);
was_grad_found = true;
CeedCallBackend(CeedBasisDestroy(&basis));
}
}
for (CeedInt i = 0; i < num_output_fields; i++) {
Expand All @@ -205,6 +207,7 @@ extern "C" int CeedOperatorBuildKernel_Sycl_gen(CeedOperator op) {
CeedCallBackend(CeedBasisGetData(basis, &basis_impl));
use_collograd_parallelization = basis_impl->d_collo_grad_1d && (was_grad_found ? use_collograd_parallelization : true);
was_grad_found = true;
CeedCallBackend(CeedBasisDestroy(&basis));
}
}
}
Expand Down Expand Up @@ -273,6 +276,7 @@ extern "C" int CeedOperatorBuildKernel_Sycl_gen(CeedOperator op) {
CeedCallBackend(CeedElemRestrictionGetElementSize(elem_rstr, &elem_size));
CeedCallBackend(CeedQFunctionFieldGetEvalMode(qf_input_fields[i], &eval_mode));
CeedCallBackend(CeedElemRestrictionGetNumComponents(elem_rstr, &num_comp));
CeedCallBackend(CeedElemRestrictionDestroy(&elem_rstr));

// Set field constants
if (eval_mode != CEED_EVAL_WEIGHT) {
Expand Down Expand Up @@ -321,6 +325,7 @@ extern "C" int CeedOperatorBuildKernel_Sycl_gen(CeedOperator op) {
case CEED_EVAL_CURL:
break; // TODO: Not implemented
}
CeedCallBackend(CeedBasisDestroy(&basis));
}

code << "\n // -- Output field constants and basis data --\n";
Expand All @@ -331,6 +336,7 @@ extern "C" int CeedOperatorBuildKernel_Sycl_gen(CeedOperator op) {
CeedCallBackend(CeedElemRestrictionGetElementSize(elem_rstr, &elem_size));
CeedCallBackend(CeedQFunctionFieldGetEvalMode(qf_output_fields[i], &eval_mode));
CeedCallBackend(CeedElemRestrictionGetNumComponents(elem_rstr, &num_comp));
CeedCallBackend(CeedElemRestrictionDestroy(&elem_rstr));

// Set field constants
CeedCallBackend(CeedOperatorFieldGetBasis(op_output_fields[i], &basis));
Expand Down Expand Up @@ -382,6 +388,7 @@ extern "C" int CeedOperatorBuildKernel_Sycl_gen(CeedOperator op) {
}
// LCOV_EXCL_STOP
}
CeedCallBackend(CeedBasisDestroy(&basis));
}
code << "\n // -- Element loop --\n";
code << " work_group_barrier(CLK_LOCAL_MEM_FENCE);\n";
Expand Down Expand Up @@ -431,6 +438,7 @@ extern "C" int CeedOperatorBuildKernel_Sycl_gen(CeedOperator op) {
<< ", num_elem, d_u_" << i << ", r_u_" << i << ");\n";
}
}
CeedCallBackend(CeedElemRestrictionDestroy(&elem_rstr));

// Basis action
code << " // EvalMode: " << CeedEvalModes[eval_mode] << "\n";
Expand All @@ -452,12 +460,14 @@ extern "C" int CeedOperatorBuildKernel_Sycl_gen(CeedOperator op) {
<< i << ", r_t_" << i << ", elem_scratch);\n";
} else {
CeedInt P_1d;

CeedCallBackend(CeedOperatorFieldGetBasis(op_input_fields[i], &basis));
CeedCallBackend(CeedBasisGetNumNodes1D(basis, &P_1d));
code << " CeedScalar r_t_" << i << "[num_comp_in_" << i << "*DIM*Q_1D];\n";
code << " Grad" << (dim > 1 ? "Tensor" : "") << (dim == 3 && Q_1d >= P_1d ? "Collocated" : "") << dim << "d(num_comp_in_" << i
<< ", P_in_" << i << ", Q_1D, r_u_" << i << (dim > 1 ? ", s_B_in_" : "") << (dim > 1 ? std::to_string(i) : "") << ", s_G_in_" << i
<< ", r_t_" << i << ", elem_scratch);\n";
CeedCallBackend(CeedBasisDestroy(&basis));
}
break;
case CEED_EVAL_WEIGHT:
Expand All @@ -466,6 +476,7 @@ extern "C" int CeedOperatorBuildKernel_Sycl_gen(CeedOperator op) {
CeedCallBackend(CeedBasisGetData(basis, &basis_impl));
impl->W = basis_impl->d_q_weight_1d;
code << " Weight" << (dim > 1 ? "Tensor" : "") << dim << "d(Q_1D, W, r_t_" << i << ");\n";
CeedCallBackend(CeedBasisDestroy(&basis));
break; // No action
case CEED_EVAL_DIV:
break; // TODO: Not implemented
Expand Down Expand Up @@ -544,6 +555,7 @@ extern "C" int CeedOperatorBuildKernel_Sycl_gen(CeedOperator op) {
<< "3d(num_comp_in_" << i << ", Q_1D," << strides[0] << ", " << strides[1] << ", " << strides[2] << ", num_elem, q, d_u_" << i
<< ", r_q_" << i << ");\n";
}
CeedCallBackend(CeedElemRestrictionDestroy(&elem_rstr));
break;
case CEED_EVAL_INTERP:
code << " CeedScalar r_q_" << i << "[num_comp_in_" << i << "];\n";
Expand Down Expand Up @@ -690,6 +702,7 @@ extern "C" int CeedOperatorBuildKernel_Sycl_gen(CeedOperator op) {
code << " GradTranspose" << (dim > 1 ? "Tensor" : "") << (dim == 3 && Q_1d >= P_1d ? "Collocated" : "") << dim << "d(num_comp_out_" << i
<< ", P_out_" << i << ", Q_1D, r_tt_" << i << (dim > 1 ? ", s_B_out_" : "") << (dim > 1 ? std::to_string(i) : "") << ", s_G_out_" << i
<< ", r_v_" << i << ", elem_scratch);\n";
CeedCallBackend(CeedBasisDestroy(&basis));
}
break;
// LCOV_EXCL_START
Expand Down Expand Up @@ -734,6 +747,7 @@ extern "C" int CeedOperatorBuildKernel_Sycl_gen(CeedOperator op) {
code << " writeDofsStrided" << dim << "d(num_comp_out_" << i << ",P_out_" << i << "," << strides[0] << "," << strides[1] << "," << strides[2]
<< ", num_elem, r_v_" << i << ", d_v_" << i << ");\n";
}
CeedCallBackend(CeedElemRestrictionDestroy(&elem_rstr));
}

code << " }\n";
Expand Down
20 changes: 16 additions & 4 deletions backends/sycl-gen/ceed-sycl-gen-operator.sycl.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -73,12 +73,15 @@ static int CeedOperatorApplyAdd_Sycl_gen(CeedOperator op, CeedVector input_vec,
if (eval_mode == CEED_EVAL_WEIGHT) { // Skip
impl->fields->inputs[i] = NULL;
} else {
bool is_active;
CeedVector vec;

// Get input vector
CeedCallBackend(CeedOperatorFieldGetVector(op_input_fields[i], &vec));
if (vec == CEED_VECTOR_ACTIVE) vec = input_vec;
is_active = vec == CEED_VECTOR_ACTIVE;
if (is_active) vec = input_vec;
CeedCallBackend(CeedVectorGetArrayRead(vec, CEED_MEM_DEVICE, &impl->fields->inputs[i]));
if (!is_active) CeedCallBackend(CeedVectorDestroy(&vec));
}
}

Expand All @@ -88,11 +91,13 @@ static int CeedOperatorApplyAdd_Sycl_gen(CeedOperator op, CeedVector input_vec,
if (eval_mode == CEED_EVAL_WEIGHT) { // Skip
impl->fields->outputs[i] = NULL;
} else {
bool is_active;
CeedVector vec;

// Get output vector
CeedCallBackend(CeedOperatorFieldGetVector(op_output_fields[i], &vec));
if (vec == CEED_VECTOR_ACTIVE) vec = output_vec;
is_active = vec == CEED_VECTOR_ACTIVE;
if (is_active) vec = output_vec;
output_vecs[i] = vec;
// Check for multiple output modes
CeedInt index = -1;
Expand All @@ -102,6 +107,7 @@ static int CeedOperatorApplyAdd_Sycl_gen(CeedOperator op, CeedVector input_vec,
break;
}
}
if (!is_active) CeedCallBackend(CeedVectorDestroy(&vec));
if (index == -1) {
CeedCallBackend(CeedVectorGetArray(vec, CEED_MEM_DEVICE, &impl->fields->outputs[i]));
} else {
Expand Down Expand Up @@ -152,11 +158,14 @@ static int CeedOperatorApplyAdd_Sycl_gen(CeedOperator op, CeedVector input_vec,
CeedCallBackend(CeedQFunctionFieldGetEvalMode(qf_input_fields[i], &eval_mode));
if (eval_mode == CEED_EVAL_WEIGHT) { // Skip
} else {
bool is_active;
CeedVector vec;

CeedCallBackend(CeedOperatorFieldGetVector(op_input_fields[i], &vec));
if (vec == CEED_VECTOR_ACTIVE) vec = input_vec;
is_active = vec == CEED_VECTOR_ACTIVE;
if (is_active) vec = input_vec;
CeedCallBackend(CeedVectorRestoreArrayRead(vec, &impl->fields->inputs[i]));
if (!is_active) CeedCallBackend(CeedVectorDestroy(&vec));
}
}

Expand All @@ -165,10 +174,12 @@ static int CeedOperatorApplyAdd_Sycl_gen(CeedOperator op, CeedVector input_vec,
CeedCallBackend(CeedQFunctionFieldGetEvalMode(qf_output_fields[i], &eval_mode));
if (eval_mode == CEED_EVAL_WEIGHT) { // Skip
} else {
bool is_active;
CeedVector vec;

CeedCallBackend(CeedOperatorFieldGetVector(op_output_fields[i], &vec));
if (vec == CEED_VECTOR_ACTIVE) vec = output_vec;
is_active = vec == CEED_VECTOR_ACTIVE;
if (is_active) vec = output_vec;
// Check for multiple output modes
CeedInt index = -1;

Expand All @@ -178,6 +189,7 @@ static int CeedOperatorApplyAdd_Sycl_gen(CeedOperator op, CeedVector input_vec,
break;
}
}
if (!is_active) CeedCallBackend(CeedVectorDestroy(&vec));
if (index == -1) {
CeedCallBackend(CeedVectorRestoreArray(vec, &impl->fields->outputs[i]));
}
Expand Down
Loading

0 comments on commit de31b36

Please sign in to comment.