Skip to content

Commit 58ed935

Browse files
authored
Merge pull request #1861 from CEED/zach/apply-add-active
op - Add `CeedOperatorApplyAddActive()` for only summing into active outputs
2 parents 45e62b5 + 0db52ef commit 58ed935

File tree

2 files changed

+85
-54
lines changed

2 files changed

+85
-54
lines changed

include/ceed/ceed.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -469,6 +469,7 @@ CEED_EXTERN int CeedOperatorGetContextBooleanRead(CeedOperator op, CeedContextF
469469
CEED_EXTERN int CeedOperatorRestoreContextBooleanRead(CeedOperator op, CeedContextFieldLabel field_label, const bool **values);
470470
CEED_EXTERN int CeedOperatorApply(CeedOperator op, CeedVector in, CeedVector out, CeedRequest *request);
471471
CEED_EXTERN int CeedOperatorApplyAdd(CeedOperator op, CeedVector in, CeedVector out, CeedRequest *request);
472+
CEED_EXTERN int CeedOperatorApplyAddActive(CeedOperator op, CeedVector in, CeedVector out, CeedRequest *request);
472473
CEED_EXTERN int CeedOperatorAssemblyDataStrip(CeedOperator op);
473474
CEED_EXTERN int CeedOperatorDestroy(CeedOperator *op);
474475

interface/ceed-operator.c

Lines changed: 84 additions & 54 deletions
Original file line numberDiff line numberDiff line change
@@ -1076,10 +1076,10 @@ int CeedOperatorAtPointsSetPoints(CeedOperator op, CeedElemRestriction rstr_poin
10761076

10771077
/**
10781078
@brief Get a boolean value indicating if the `CeedOperator` was created with `CeedOperatorCreateAtPoints`
1079-
1079+
10801080
@param[in] op `CeedOperator`
10811081
@param[out] is_at_points Variable to store at points status
1082-
1082+
10831083
@return An error code: 0 - success, otherwise - failure
10841084
10851085
@ref User
@@ -2142,7 +2142,7 @@ int CeedOperatorRestoreContextBooleanRead(CeedOperator op, CeedContextFieldLabel
21422142
This computes the action of the operator on the specified (active) input, yielding its (active) output.
21432143
All inputs and outputs must be specified using @ref CeedOperatorSetField().
21442144
2145-
Note: Calling this function asserts that setup is complete and sets the `CeedOperator` as immutable.
2145+
@note Calling this function asserts that setup is complete and sets the `CeedOperator` as immutable.
21462146
21472147
@param[in] op `CeedOperator` to apply
21482148
@param[in] in `CeedVector` containing input state or @ref CEED_VECTOR_NONE if there are no active inputs
@@ -2159,60 +2159,19 @@ int CeedOperatorApply(CeedOperator op, CeedVector in, CeedVector out, CeedReques
21592159
CeedCall(CeedOperatorCheckReady(op));
21602160

21612161
CeedCall(CeedOperatorIsComposite(op, &is_composite));
2162-
if (is_composite) {
2162+
if (is_composite && op->ApplyComposite) {
21632163
// Composite Operator
2164-
if (op->ApplyComposite) {
2165-
CeedCall(op->ApplyComposite(op, in, out, request));
2166-
} else {
2167-
CeedInt num_suboperators;
2168-
CeedOperator *sub_operators;
2169-
2170-
CeedCall(CeedCompositeOperatorGetNumSub(op, &num_suboperators));
2171-
CeedCall(CeedCompositeOperatorGetSubList(op, &sub_operators));
2172-
2173-
// Zero all output vectors
2174-
if (out != CEED_VECTOR_NONE) CeedCall(CeedVectorSetValue(out, 0.0));
2175-
for (CeedInt i = 0; i < num_suboperators; i++) {
2176-
CeedInt num_output_fields;
2177-
CeedOperatorField *output_fields;
2178-
2179-
CeedCall(CeedOperatorGetFields(sub_operators[i], NULL, NULL, &num_output_fields, &output_fields));
2180-
for (CeedInt j = 0; j < num_output_fields; j++) {
2181-
CeedVector vec;
2182-
2183-
CeedCall(CeedOperatorFieldGetVector(output_fields[j], &vec));
2184-
if (vec != CEED_VECTOR_ACTIVE && vec != CEED_VECTOR_NONE) {
2185-
CeedCall(CeedVectorSetValue(vec, 0.0));
2186-
}
2187-
CeedCall(CeedVectorDestroy(&vec));
2188-
}
2189-
}
2190-
// ApplyAdd
2191-
CeedCall(CeedOperatorApplyAdd(op, in, out, request));
2192-
}
2193-
} else {
2164+
CeedCall(op->ApplyComposite(op, in, out, request));
2165+
} else if (!is_composite && op->Apply) {
21942166
// Standard Operator
2195-
if (op->Apply) {
2196-
CeedCall(op->Apply(op, in, out, request));
2197-
} else {
2198-
CeedInt num_output_fields;
2199-
CeedOperatorField *output_fields;
2200-
2201-
CeedCall(CeedOperatorGetFields(op, NULL, NULL, &num_output_fields, &output_fields));
2202-
// Zero all output vectors
2203-
for (CeedInt i = 0; i < num_output_fields; i++) {
2204-
bool is_active;
2205-
CeedVector vec;
2167+
CeedCall(op->Apply(op, in, out, request));
2168+
} else {
2169+
// Standard or composite, default to zeroing out and calling ApplyAddActive
2170+
// Zero active output
2171+
if (out != CEED_VECTOR_NONE) CeedCall(CeedVectorSetValue(out, 0.0));
22062172

2207-
CeedCall(CeedOperatorFieldGetVector(output_fields[i], &vec));
2208-
is_active = vec == CEED_VECTOR_ACTIVE;
2209-
if (is_active) vec = out;
2210-
if (vec != CEED_VECTOR_NONE) CeedCall(CeedVectorSetValue(vec, 0.0));
2211-
if (!is_active) CeedCall(CeedVectorDestroy(&vec));
2212-
}
2213-
// Apply
2214-
if (op->num_elem > 0) CeedCall(op->ApplyAdd(op, in, out, request));
2215-
}
2173+
// ApplyAddActive
2174+
CeedCall(CeedOperatorApplyAddActive(op, in, out, request));
22162175
}
22172176
return CEED_ERROR_SUCCESS;
22182177
}
@@ -2223,6 +2182,10 @@ int CeedOperatorApply(CeedOperator op, CeedVector in, CeedVector out, CeedReques
22232182
This computes the action of the operator on the specified (active) input, yielding its (active) output.
22242183
All inputs and outputs must be specified using @ref CeedOperatorSetField().
22252184
2185+
@note Calling this function asserts that setup is complete and sets the `CeedOperator` as immutable.
2186+
@warning This function adds into ALL outputs, including passive outputs. To only add into the active output, use `CeedOperatorApplyAddActive()`.
2187+
@see `CeedOperatorApplyAddActive()`
2188+
22262189
@param[in] op `CeedOperator` to apply
22272190
@param[in] in `CeedVector` containing input state or @ref CEED_VECTOR_NONE if there are no active inputs
22282191
@param[out] out `CeedVector` to sum in result of applying operator (must be distinct from `in`) or @ref CEED_VECTOR_NONE if there are no active outputs
@@ -2259,6 +2222,73 @@ int CeedOperatorApplyAdd(CeedOperator op, CeedVector in, CeedVector out, CeedReq
22592222
return CEED_ERROR_SUCCESS;
22602223
}
22612224

2225+
/**
2226+
@brief Apply `CeedOperator` to a `CeedVector` and add result to output `CeedVector`. Only sums into active outputs, overwrites passive outputs.
2227+
2228+
This computes the action of the operator on the specified (active) input, yielding its (active) output.
2229+
All inputs and outputs must be specified using @ref CeedOperatorSetField().
2230+
2231+
@note Calling this function asserts that setup is complete and sets the `CeedOperator` as immutable.
2232+
2233+
@param[in] op `CeedOperator` to apply
2234+
@param[in] in `CeedVector` containing input state or @ref CEED_VECTOR_NONE if there are no active inputs
2235+
@param[out] out `CeedVector` to sum in result of applying operator (must be distinct from `in`) or @ref CEED_VECTOR_NONE if there are no active outputs
2236+
@param[in] request Address of @ref CeedRequest for non-blocking completion, else @ref CEED_REQUEST_IMMEDIATE
2237+
2238+
@return An error code: 0 - success, otherwise - failure
2239+
2240+
@ref User
2241+
**/
2242+
int CeedOperatorApplyAddActive(CeedOperator op, CeedVector in, CeedVector out, CeedRequest *request) {
2243+
bool is_composite;
2244+
2245+
CeedCall(CeedOperatorCheckReady(op));
2246+
2247+
CeedCall(CeedOperatorIsComposite(op, &is_composite));
2248+
if (is_composite) {
2249+
// Composite Operator
2250+
CeedInt num_suboperators;
2251+
CeedOperator *sub_operators;
2252+
2253+
CeedCall(CeedCompositeOperatorGetNumSub(op, &num_suboperators));
2254+
CeedCall(CeedCompositeOperatorGetSubList(op, &sub_operators));
2255+
2256+
// Zero all output vectors
2257+
for (CeedInt i = 0; i < num_suboperators; i++) {
2258+
CeedInt num_output_fields;
2259+
CeedOperatorField *output_fields;
2260+
2261+
CeedCall(CeedOperatorGetFields(sub_operators[i], NULL, NULL, &num_output_fields, &output_fields));
2262+
for (CeedInt j = 0; j < num_output_fields; j++) {
2263+
CeedVector vec;
2264+
2265+
CeedCall(CeedOperatorFieldGetVector(output_fields[j], &vec));
2266+
if (vec != CEED_VECTOR_ACTIVE && vec != CEED_VECTOR_NONE) CeedCall(CeedVectorSetValue(vec, 0.0));
2267+
CeedCall(CeedVectorDestroy(&vec));
2268+
}
2269+
}
2270+
// ApplyAdd
2271+
CeedCall(CeedOperatorApplyAdd(op, in, out, request));
2272+
} else {
2273+
// Standard Operator
2274+
CeedInt num_output_fields;
2275+
CeedOperatorField *output_fields;
2276+
2277+
CeedCall(CeedOperatorGetFields(op, NULL, NULL, &num_output_fields, &output_fields));
2278+
// Zero all output vectors
2279+
for (CeedInt i = 0; i < num_output_fields; i++) {
2280+
CeedVector vec;
2281+
2282+
CeedCall(CeedOperatorFieldGetVector(output_fields[i], &vec));
2283+
if (vec != CEED_VECTOR_ACTIVE && vec != CEED_VECTOR_NONE) CeedCall(CeedVectorSetValue(vec, 0.0));
2284+
CeedCall(CeedVectorDestroy(&vec));
2285+
}
2286+
// ApplyAdd
2287+
CeedCall(CeedOperatorApplyAdd(op, in, out, request));
2288+
}
2289+
return CEED_ERROR_SUCCESS;
2290+
}
2291+
22622292
/**
22632293
@brief Destroy temporary assembly data associated with a `CeedOperator`
22642294

0 commit comments

Comments
 (0)