xref: /libCEED/rust/libceed-sys/c-src/backends/cuda-ref/ceed-cuda-ref-operator.c (revision 3aab95c02208308af393c9014d0e46d9d208d4a6)
15aed82e4SJeremy L Thompson // Copyright (c) 2017-2024, Lawrence Livermore National Security, LLC and other CEED contributors.
23d8e8822SJeremy L Thompson // All Rights Reserved. See the top-level LICENSE and NOTICE files for details.
30d0321e0SJeremy L Thompson //
43d8e8822SJeremy L Thompson // SPDX-License-Identifier: BSD-2-Clause
50d0321e0SJeremy L Thompson //
63d8e8822SJeremy L Thompson // This file is part of CEED:  http://github.com/ceed
70d0321e0SJeremy L Thompson 
849aac155SJeremy L Thompson #include <ceed.h>
92b730f8bSJeremy L Thompson #include <ceed/backend.h>
102b730f8bSJeremy L Thompson #include <ceed/jit-tools.h>
11c85e8640SSebastian Grimberg #include <assert.h>
120d0321e0SJeremy L Thompson #include <cuda.h>
130d0321e0SJeremy L Thompson #include <cuda_runtime.h>
140d0321e0SJeremy L Thompson #include <stdbool.h>
150d0321e0SJeremy L Thompson #include <string.h>
162b730f8bSJeremy L Thompson 
1749aac155SJeremy L Thompson #include "../cuda/ceed-cuda-common.h"
180d0321e0SJeremy L Thompson #include "../cuda/ceed-cuda-compile.h"
192b730f8bSJeremy L Thompson #include "ceed-cuda-ref.h"
200d0321e0SJeremy L Thompson 
210d0321e0SJeremy L Thompson //------------------------------------------------------------------------------
220d0321e0SJeremy L Thompson // Destroy operator
230d0321e0SJeremy L Thompson //------------------------------------------------------------------------------
240d0321e0SJeremy L Thompson static int CeedOperatorDestroy_Cuda(CeedOperator op) {
250d0321e0SJeremy L Thompson   CeedOperator_Cuda *impl;
26ca735530SJeremy L Thompson 
272b730f8bSJeremy L Thompson   CeedCallBackend(CeedOperatorGetData(op, &impl));
280d0321e0SJeremy L Thompson 
290d0321e0SJeremy L Thompson   // Apply data
30*3aab95c0SJeremy L Thompson   CeedCallBackend(CeedFree(&impl->skip_rstr_in));
31ca735530SJeremy L Thompson   for (CeedInt i = 0; i < impl->num_inputs + impl->num_outputs; i++) {
32ca735530SJeremy L Thompson     CeedCallBackend(CeedVectorDestroy(&impl->e_vecs[i]));
330d0321e0SJeremy L Thompson   }
34ca735530SJeremy L Thompson   CeedCallBackend(CeedFree(&impl->e_vecs));
35c1222711SJeremy L Thompson   CeedCallBackend(CeedFree(&impl->input_states));
360d0321e0SJeremy L Thompson 
37ca735530SJeremy L Thompson   for (CeedInt i = 0; i < impl->num_inputs; i++) {
38ca735530SJeremy L Thompson     CeedCallBackend(CeedVectorDestroy(&impl->q_vecs_in[i]));
390d0321e0SJeremy L Thompson   }
40ca735530SJeremy L Thompson   CeedCallBackend(CeedFree(&impl->q_vecs_in));
410d0321e0SJeremy L Thompson 
42ca735530SJeremy L Thompson   for (CeedInt i = 0; i < impl->num_outputs; i++) {
43ca735530SJeremy L Thompson     CeedCallBackend(CeedVectorDestroy(&impl->q_vecs_out[i]));
440d0321e0SJeremy L Thompson   }
45ca735530SJeremy L Thompson   CeedCallBackend(CeedFree(&impl->q_vecs_out));
46756ca9e9SJeremy L Thompson   CeedCallBackend(CeedVectorDestroy(&impl->point_coords_elem));
470d0321e0SJeremy L Thompson 
480d0321e0SJeremy L Thompson   // QFunction assembly data
49ca735530SJeremy L Thompson   for (CeedInt i = 0; i < impl->num_active_in; i++) {
50ca735530SJeremy L Thompson     CeedCallBackend(CeedVectorDestroy(&impl->qf_active_in[i]));
510d0321e0SJeremy L Thompson   }
52ca735530SJeremy L Thompson   CeedCallBackend(CeedFree(&impl->qf_active_in));
530d0321e0SJeremy L Thompson 
540d0321e0SJeremy L Thompson   // Diag data
550d0321e0SJeremy L Thompson   if (impl->diag) {
560d0321e0SJeremy L Thompson     Ceed ceed;
57ca735530SJeremy L Thompson 
582b730f8bSJeremy L Thompson     CeedCallBackend(CeedOperatorGetCeed(op, &ceed));
59cbfe683aSSebastian Grimberg     if (impl->diag->module) {
602b730f8bSJeremy L Thompson       CeedCallCuda(ceed, cuModuleUnload(impl->diag->module));
61cbfe683aSSebastian Grimberg     }
62cbfe683aSSebastian Grimberg     if (impl->diag->module_point_block) {
63cbfe683aSSebastian Grimberg       CeedCallCuda(ceed, cuModuleUnload(impl->diag->module_point_block));
64cbfe683aSSebastian Grimberg     }
65004e4986SSebastian Grimberg     CeedCallCuda(ceed, cudaFree(impl->diag->d_eval_modes_in));
66004e4986SSebastian Grimberg     CeedCallCuda(ceed, cudaFree(impl->diag->d_eval_modes_out));
672b730f8bSJeremy L Thompson     CeedCallCuda(ceed, cudaFree(impl->diag->d_identity));
68ca735530SJeremy L Thompson     CeedCallCuda(ceed, cudaFree(impl->diag->d_interp_in));
69ca735530SJeremy L Thompson     CeedCallCuda(ceed, cudaFree(impl->diag->d_interp_out));
70ca735530SJeremy L Thompson     CeedCallCuda(ceed, cudaFree(impl->diag->d_grad_in));
71ca735530SJeremy L Thompson     CeedCallCuda(ceed, cudaFree(impl->diag->d_grad_out));
72004e4986SSebastian Grimberg     CeedCallCuda(ceed, cudaFree(impl->diag->d_div_in));
73004e4986SSebastian Grimberg     CeedCallCuda(ceed, cudaFree(impl->diag->d_div_out));
74004e4986SSebastian Grimberg     CeedCallCuda(ceed, cudaFree(impl->diag->d_curl_in));
75004e4986SSebastian Grimberg     CeedCallCuda(ceed, cudaFree(impl->diag->d_curl_out));
76004e4986SSebastian Grimberg     CeedCallBackend(CeedElemRestrictionDestroy(&impl->diag->diag_rstr));
77506b1a0cSSebastian Grimberg     CeedCallBackend(CeedElemRestrictionDestroy(&impl->diag->point_block_diag_rstr));
78ca735530SJeremy L Thompson     CeedCallBackend(CeedVectorDestroy(&impl->diag->elem_diag));
79ca735530SJeremy L Thompson     CeedCallBackend(CeedVectorDestroy(&impl->diag->point_block_elem_diag));
800d0321e0SJeremy L Thompson   }
812b730f8bSJeremy L Thompson   CeedCallBackend(CeedFree(&impl->diag));
820d0321e0SJeremy L Thompson 
83cc132f9aSnbeams   if (impl->asmb) {
84cc132f9aSnbeams     Ceed ceed;
85ca735530SJeremy L Thompson 
862b730f8bSJeremy L Thompson     CeedCallBackend(CeedOperatorGetCeed(op, &ceed));
872b730f8bSJeremy L Thompson     CeedCallCuda(ceed, cuModuleUnload(impl->asmb->module));
882b730f8bSJeremy L Thompson     CeedCallCuda(ceed, cudaFree(impl->asmb->d_B_in));
892b730f8bSJeremy L Thompson     CeedCallCuda(ceed, cudaFree(impl->asmb->d_B_out));
90cc132f9aSnbeams   }
912b730f8bSJeremy L Thompson   CeedCallBackend(CeedFree(&impl->asmb));
92cc132f9aSnbeams 
932b730f8bSJeremy L Thompson   CeedCallBackend(CeedFree(&impl));
940d0321e0SJeremy L Thompson   return CEED_ERROR_SUCCESS;
950d0321e0SJeremy L Thompson }
960d0321e0SJeremy L Thompson 
970d0321e0SJeremy L Thompson //------------------------------------------------------------------------------
980d0321e0SJeremy L Thompson // Setup infields or outfields
990d0321e0SJeremy L Thompson //------------------------------------------------------------------------------
100*3aab95c0SJeremy L Thompson static int CeedOperatorSetupFields_Cuda(CeedQFunction qf, CeedOperator op, bool is_input, bool is_at_points, bool *skip_rstr, CeedVector *e_vecs,
101*3aab95c0SJeremy L Thompson                                         CeedVector *q_vecs, CeedInt start_e, CeedInt num_fields, CeedInt Q, CeedInt num_elem) {
1020d0321e0SJeremy L Thompson   Ceed                ceed;
103ca735530SJeremy L Thompson   CeedQFunctionField *qf_fields;
104ca735530SJeremy L Thompson   CeedOperatorField  *op_fields;
1050d0321e0SJeremy L Thompson 
106ca735530SJeremy L Thompson   CeedCallBackend(CeedOperatorGetCeed(op, &ceed));
107ca735530SJeremy L Thompson   if (is_input) {
108ca735530SJeremy L Thompson     CeedCallBackend(CeedOperatorGetFields(op, NULL, &op_fields, NULL, NULL));
109ca735530SJeremy L Thompson     CeedCallBackend(CeedQFunctionGetFields(qf, NULL, &qf_fields, NULL, NULL));
1100d0321e0SJeremy L Thompson   } else {
111ca735530SJeremy L Thompson     CeedCallBackend(CeedOperatorGetFields(op, NULL, NULL, NULL, &op_fields));
112ca735530SJeremy L Thompson     CeedCallBackend(CeedQFunctionGetFields(qf, NULL, NULL, NULL, &qf_fields));
1130d0321e0SJeremy L Thompson   }
1140d0321e0SJeremy L Thompson 
1150d0321e0SJeremy L Thompson   // Loop over fields
116ca735530SJeremy L Thompson   for (CeedInt i = 0; i < num_fields; i++) {
117004e4986SSebastian Grimberg     bool         is_strided = false, skip_restriction = false;
118004e4986SSebastian Grimberg     CeedSize     q_size;
119004e4986SSebastian Grimberg     CeedInt      size;
120004e4986SSebastian Grimberg     CeedEvalMode eval_mode;
121ca735530SJeremy L Thompson     CeedBasis    basis;
1220d0321e0SJeremy L Thompson 
123004e4986SSebastian Grimberg     CeedCallBackend(CeedQFunctionFieldGetEvalMode(qf_fields[i], &eval_mode));
124004e4986SSebastian Grimberg     if (eval_mode != CEED_EVAL_WEIGHT) {
125edb2538eSJeremy L Thompson       CeedElemRestriction elem_rstr;
126ca735530SJeremy L Thompson 
1270d0321e0SJeremy L Thompson       // Check whether this field can skip the element restriction:
128004e4986SSebastian Grimberg       // Must be passive input, with eval_mode NONE, and have a strided restriction with CEED_STRIDES_BACKEND.
129004e4986SSebastian Grimberg       CeedCallBackend(CeedOperatorFieldGetElemRestriction(op_fields[i], &elem_rstr));
1300d0321e0SJeremy L Thompson 
1310d0321e0SJeremy L Thompson       // First, check whether the field is input or output:
132ca735530SJeremy L Thompson       if (is_input) {
133ca735530SJeremy L Thompson         CeedVector vec;
134ca735530SJeremy L Thompson 
135004e4986SSebastian Grimberg         // Check for passive input
136ca735530SJeremy L Thompson         CeedCallBackend(CeedOperatorFieldGetVector(op_fields[i], &vec));
137ca735530SJeremy L Thompson         if (vec != CEED_VECTOR_ACTIVE) {
138004e4986SSebastian Grimberg           // Check eval_mode
139004e4986SSebastian Grimberg           if (eval_mode == CEED_EVAL_NONE) {
1400d0321e0SJeremy L Thompson             // Check for strided restriction
141edb2538eSJeremy L Thompson             CeedCallBackend(CeedElemRestrictionIsStrided(elem_rstr, &is_strided));
142ca735530SJeremy L Thompson             if (is_strided) {
1430d0321e0SJeremy L Thompson               // Check if vector is already in preferred backend ordering
144edb2538eSJeremy L Thompson               CeedCallBackend(CeedElemRestrictionHasBackendStrides(elem_rstr, &skip_restriction));
1450d0321e0SJeremy L Thompson             }
1460d0321e0SJeremy L Thompson           }
1470d0321e0SJeremy L Thompson         }
1480d0321e0SJeremy L Thompson       }
149ca735530SJeremy L Thompson       if (skip_restriction) {
150ea61e9acSJeremy L Thompson         // We do not need an E-Vector, but will use the input field vector's data directly in the operator application.
151004e4986SSebastian Grimberg         e_vecs[i + start_e] = NULL;
1520d0321e0SJeremy L Thompson       } else {
153004e4986SSebastian Grimberg         CeedCallBackend(CeedElemRestrictionCreateVector(elem_rstr, NULL, &e_vecs[i + start_e]));
1540d0321e0SJeremy L Thompson       }
1550d0321e0SJeremy L Thompson     }
1560d0321e0SJeremy L Thompson 
157004e4986SSebastian Grimberg     switch (eval_mode) {
1580d0321e0SJeremy L Thompson       case CEED_EVAL_NONE:
159ca735530SJeremy L Thompson         CeedCallBackend(CeedQFunctionFieldGetSize(qf_fields[i], &size));
160ca735530SJeremy L Thompson         q_size = (CeedSize)num_elem * Q * size;
161ca735530SJeremy L Thompson         CeedCallBackend(CeedVectorCreate(ceed, q_size, &q_vecs[i]));
1620d0321e0SJeremy L Thompson         break;
1630d0321e0SJeremy L Thompson       case CEED_EVAL_INTERP:
1640d0321e0SJeremy L Thompson       case CEED_EVAL_GRAD:
165004e4986SSebastian Grimberg       case CEED_EVAL_DIV:
166004e4986SSebastian Grimberg       case CEED_EVAL_CURL:
167ca735530SJeremy L Thompson         CeedCallBackend(CeedQFunctionFieldGetSize(qf_fields[i], &size));
168ca735530SJeremy L Thompson         q_size = (CeedSize)num_elem * Q * size;
169ca735530SJeremy L Thompson         CeedCallBackend(CeedVectorCreate(ceed, q_size, &q_vecs[i]));
1700d0321e0SJeremy L Thompson         break;
1710d0321e0SJeremy L Thompson       case CEED_EVAL_WEIGHT:  // Only on input fields
172ca735530SJeremy L Thompson         CeedCallBackend(CeedOperatorFieldGetBasis(op_fields[i], &basis));
173ca735530SJeremy L Thompson         q_size = (CeedSize)num_elem * Q;
174ca735530SJeremy L Thompson         CeedCallBackend(CeedVectorCreate(ceed, q_size, &q_vecs[i]));
175756ca9e9SJeremy L Thompson         if (is_at_points) {
176756ca9e9SJeremy L Thompson           CeedInt num_points[num_elem];
177756ca9e9SJeremy L Thompson 
178756ca9e9SJeremy L Thompson           for (CeedInt i = 0; i < num_elem; i++) num_points[i] = Q;
179756ca9e9SJeremy L Thompson           CeedCallBackend(
180756ca9e9SJeremy L Thompson               CeedBasisApplyAtPoints(basis, num_elem, num_points, CEED_NOTRANSPOSE, CEED_EVAL_WEIGHT, CEED_VECTOR_NONE, CEED_VECTOR_NONE, q_vecs[i]));
181756ca9e9SJeremy L Thompson         } else {
182ca735530SJeremy L Thompson           CeedCallBackend(CeedBasisApply(basis, num_elem, CEED_NOTRANSPOSE, CEED_EVAL_WEIGHT, CEED_VECTOR_NONE, q_vecs[i]));
183756ca9e9SJeremy L Thompson         }
1840d0321e0SJeremy L Thompson         break;
1850d0321e0SJeremy L Thompson     }
1860d0321e0SJeremy L Thompson   }
187*3aab95c0SJeremy L Thompson   // Drop duplicate input restrictions
188*3aab95c0SJeremy L Thompson   if (is_input) {
189*3aab95c0SJeremy L Thompson     for (CeedInt i = 0; i < num_fields; i++) {
190*3aab95c0SJeremy L Thompson       CeedVector          vec_i;
191*3aab95c0SJeremy L Thompson       CeedElemRestriction rstr_i;
192*3aab95c0SJeremy L Thompson 
193*3aab95c0SJeremy L Thompson       CeedCallBackend(CeedOperatorFieldGetVector(op_fields[i], &vec_i));
194*3aab95c0SJeremy L Thompson       CeedCallBackend(CeedOperatorFieldGetElemRestriction(op_fields[i], &rstr_i));
195*3aab95c0SJeremy L Thompson       for (CeedInt j = i + 1; j < num_fields; j++) {
196*3aab95c0SJeremy L Thompson         CeedVector          vec_j;
197*3aab95c0SJeremy L Thompson         CeedElemRestriction rstr_j;
198*3aab95c0SJeremy L Thompson 
199*3aab95c0SJeremy L Thompson         CeedCallBackend(CeedOperatorFieldGetVector(op_fields[j], &vec_j));
200*3aab95c0SJeremy L Thompson         CeedCallBackend(CeedOperatorFieldGetElemRestriction(op_fields[j], &rstr_j));
201*3aab95c0SJeremy L Thompson         if (vec_i == vec_j && rstr_i == rstr_j) {
202*3aab95c0SJeremy L Thompson           CeedCallBackend(CeedVectorReferenceCopy(e_vecs[i], &e_vecs[j]));
203*3aab95c0SJeremy L Thompson           skip_rstr[j] = true;
204*3aab95c0SJeremy L Thompson         }
205*3aab95c0SJeremy L Thompson       }
206*3aab95c0SJeremy L Thompson     }
207*3aab95c0SJeremy L Thompson   }
2080d0321e0SJeremy L Thompson   return CEED_ERROR_SUCCESS;
2090d0321e0SJeremy L Thompson }
2100d0321e0SJeremy L Thompson 
2110d0321e0SJeremy L Thompson //------------------------------------------------------------------------------
212ea61e9acSJeremy L Thompson // CeedOperator needs to connect all the named fields (be they active or passive) to the named inputs and outputs of its CeedQFunction.
2130d0321e0SJeremy L Thompson //------------------------------------------------------------------------------
2140d0321e0SJeremy L Thompson static int CeedOperatorSetup_Cuda(CeedOperator op) {
2150d0321e0SJeremy L Thompson   Ceed                ceed;
216ca735530SJeremy L Thompson   bool                is_setup_done;
217ca735530SJeremy L Thompson   CeedInt             Q, num_elem, num_input_fields, num_output_fields;
218ca735530SJeremy L Thompson   CeedQFunctionField *qf_input_fields, *qf_output_fields;
2190d0321e0SJeremy L Thompson   CeedQFunction       qf;
220ca735530SJeremy L Thompson   CeedOperatorField  *op_input_fields, *op_output_fields;
221ca735530SJeremy L Thompson   CeedOperator_Cuda  *impl;
222ca735530SJeremy L Thompson 
223ca735530SJeremy L Thompson   CeedCallBackend(CeedOperatorIsSetupDone(op, &is_setup_done));
224ca735530SJeremy L Thompson   if (is_setup_done) return CEED_ERROR_SUCCESS;
225ca735530SJeremy L Thompson 
226ca735530SJeremy L Thompson   CeedCallBackend(CeedOperatorGetCeed(op, &ceed));
227ca735530SJeremy L Thompson   CeedCallBackend(CeedOperatorGetData(op, &impl));
2282b730f8bSJeremy L Thompson   CeedCallBackend(CeedOperatorGetQFunction(op, &qf));
2292b730f8bSJeremy L Thompson   CeedCallBackend(CeedOperatorGetNumQuadraturePoints(op, &Q));
230ca735530SJeremy L Thompson   CeedCallBackend(CeedOperatorGetNumElements(op, &num_elem));
231ca735530SJeremy L Thompson   CeedCallBackend(CeedOperatorGetFields(op, &num_input_fields, &op_input_fields, &num_output_fields, &op_output_fields));
232ca735530SJeremy L Thompson   CeedCallBackend(CeedQFunctionGetFields(qf, NULL, &qf_input_fields, NULL, &qf_output_fields));
2330d0321e0SJeremy L Thompson 
2340d0321e0SJeremy L Thompson   // Allocate
235ca735530SJeremy L Thompson   CeedCallBackend(CeedCalloc(num_input_fields + num_output_fields, &impl->e_vecs));
236*3aab95c0SJeremy L Thompson   CeedCallBackend(CeedCalloc(CEED_FIELD_MAX, &impl->skip_rstr_in));
237c1222711SJeremy L Thompson   CeedCallBackend(CeedCalloc(CEED_FIELD_MAX, &impl->input_states));
238ca735530SJeremy L Thompson   CeedCallBackend(CeedCalloc(CEED_FIELD_MAX, &impl->q_vecs_in));
239ca735530SJeremy L Thompson   CeedCallBackend(CeedCalloc(CEED_FIELD_MAX, &impl->q_vecs_out));
240ca735530SJeremy L Thompson   impl->num_inputs  = num_input_fields;
241ca735530SJeremy L Thompson   impl->num_outputs = num_output_fields;
2420d0321e0SJeremy L Thompson 
243ca735530SJeremy L Thompson   // Set up infield and outfield e_vecs and q_vecs
2440d0321e0SJeremy L Thompson   // Infields
245*3aab95c0SJeremy L Thompson   CeedCallBackend(
246*3aab95c0SJeremy L Thompson       CeedOperatorSetupFields_Cuda(qf, op, true, false, impl->skip_rstr_in, impl->e_vecs, impl->q_vecs_in, 0, num_input_fields, Q, num_elem));
2470d0321e0SJeremy L Thompson   // Outfields
248756ca9e9SJeremy L Thompson   CeedCallBackend(
249*3aab95c0SJeremy L Thompson       CeedOperatorSetupFields_Cuda(qf, op, false, false, NULL, impl->e_vecs, impl->q_vecs_out, num_input_fields, num_output_fields, Q, num_elem));
2500d0321e0SJeremy L Thompson 
2512b730f8bSJeremy L Thompson   CeedCallBackend(CeedOperatorSetSetupDone(op));
2520d0321e0SJeremy L Thompson   return CEED_ERROR_SUCCESS;
2530d0321e0SJeremy L Thompson }
2540d0321e0SJeremy L Thompson 
2550d0321e0SJeremy L Thompson //------------------------------------------------------------------------------
2560d0321e0SJeremy L Thompson // Setup Operator Inputs
2570d0321e0SJeremy L Thompson //------------------------------------------------------------------------------
258ca735530SJeremy L Thompson static inline int CeedOperatorSetupInputs_Cuda(CeedInt num_input_fields, CeedQFunctionField *qf_input_fields, CeedOperatorField *op_input_fields,
259004e4986SSebastian Grimberg                                                CeedVector in_vec, const bool skip_active, CeedScalar *e_data[2 * CEED_FIELD_MAX],
2600d0321e0SJeremy L Thompson                                                CeedOperator_Cuda *impl, CeedRequest *request) {
261ca735530SJeremy L Thompson   for (CeedInt i = 0; i < num_input_fields; i++) {
262004e4986SSebastian Grimberg     CeedEvalMode        eval_mode;
2630d0321e0SJeremy L Thompson     CeedVector          vec;
264edb2538eSJeremy L Thompson     CeedElemRestriction elem_rstr;
2650d0321e0SJeremy L Thompson 
2660d0321e0SJeremy L Thompson     // Get input vector
267ca735530SJeremy L Thompson     CeedCallBackend(CeedOperatorFieldGetVector(op_input_fields[i], &vec));
2680d0321e0SJeremy L Thompson     if (vec == CEED_VECTOR_ACTIVE) {
269004e4986SSebastian Grimberg       if (skip_active) continue;
270ca735530SJeremy L Thompson       else vec = in_vec;
2710d0321e0SJeremy L Thompson     }
2720d0321e0SJeremy L Thompson 
273004e4986SSebastian Grimberg     CeedCallBackend(CeedQFunctionFieldGetEvalMode(qf_input_fields[i], &eval_mode));
274004e4986SSebastian Grimberg     if (eval_mode == CEED_EVAL_WEIGHT) {  // Skip
2750d0321e0SJeremy L Thompson     } else {
276004e4986SSebastian Grimberg       // Get input vector
277004e4986SSebastian Grimberg       CeedCallBackend(CeedOperatorFieldGetVector(op_input_fields[i], &vec));
2780d0321e0SJeremy L Thompson       // Get input element restriction
279edb2538eSJeremy L Thompson       CeedCallBackend(CeedOperatorFieldGetElemRestriction(op_input_fields[i], &elem_rstr));
280ca735530SJeremy L Thompson       if (vec == CEED_VECTOR_ACTIVE) vec = in_vec;
2810d0321e0SJeremy L Thompson       // Restrict, if necessary
282ca735530SJeremy L Thompson       if (!impl->e_vecs[i]) {
2830d0321e0SJeremy L Thompson         // No restriction for this field; read data directly from vec.
284ca735530SJeremy L Thompson         CeedCallBackend(CeedVectorGetArrayRead(vec, CEED_MEM_DEVICE, (const CeedScalar **)&e_data[i]));
2850d0321e0SJeremy L Thompson       } else {
286c1222711SJeremy L Thompson         uint64_t state;
287c1222711SJeremy L Thompson 
288c1222711SJeremy L Thompson         CeedCallBackend(CeedVectorGetState(vec, &state));
289*3aab95c0SJeremy L Thompson         if (state != impl->input_states[i] && !impl->skip_rstr_in[i]) {
290edb2538eSJeremy L Thompson           CeedCallBackend(CeedElemRestrictionApply(elem_rstr, CEED_NOTRANSPOSE, vec, impl->e_vecs[i], request));
291c1222711SJeremy L Thompson         }
292*3aab95c0SJeremy L Thompson         impl->input_states[i] = state;
2930d0321e0SJeremy L Thompson         // Get evec
294ca735530SJeremy L Thompson         CeedCallBackend(CeedVectorGetArrayRead(impl->e_vecs[i], CEED_MEM_DEVICE, (const CeedScalar **)&e_data[i]));
2950d0321e0SJeremy L Thompson       }
2960d0321e0SJeremy L Thompson     }
2970d0321e0SJeremy L Thompson   }
2980d0321e0SJeremy L Thompson   return CEED_ERROR_SUCCESS;
2990d0321e0SJeremy L Thompson }
3000d0321e0SJeremy L Thompson 
3010d0321e0SJeremy L Thompson //------------------------------------------------------------------------------
3020d0321e0SJeremy L Thompson // Input Basis Action
3030d0321e0SJeremy L Thompson //------------------------------------------------------------------------------
304ca735530SJeremy L Thompson static inline int CeedOperatorInputBasis_Cuda(CeedInt num_elem, CeedQFunctionField *qf_input_fields, CeedOperatorField *op_input_fields,
305004e4986SSebastian Grimberg                                               CeedInt num_input_fields, const bool skip_active, CeedScalar *e_data[2 * CEED_FIELD_MAX],
3062b730f8bSJeremy L Thompson                                               CeedOperator_Cuda *impl) {
307ca735530SJeremy L Thompson   for (CeedInt i = 0; i < num_input_fields; i++) {
308ca735530SJeremy L Thompson     CeedInt             elem_size, size;
309004e4986SSebastian Grimberg     CeedEvalMode        eval_mode;
310edb2538eSJeremy L Thompson     CeedElemRestriction elem_rstr;
3110d0321e0SJeremy L Thompson     CeedBasis           basis;
3120d0321e0SJeremy L Thompson 
3130d0321e0SJeremy L Thompson     // Skip active input
314004e4986SSebastian Grimberg     if (skip_active) {
3150d0321e0SJeremy L Thompson       CeedVector vec;
316ca735530SJeremy L Thompson 
317ca735530SJeremy L Thompson       CeedCallBackend(CeedOperatorFieldGetVector(op_input_fields[i], &vec));
3182b730f8bSJeremy L Thompson       if (vec == CEED_VECTOR_ACTIVE) continue;
3190d0321e0SJeremy L Thompson     }
320004e4986SSebastian Grimberg     // Get elem_size, eval_mode, size
321edb2538eSJeremy L Thompson     CeedCallBackend(CeedOperatorFieldGetElemRestriction(op_input_fields[i], &elem_rstr));
322edb2538eSJeremy L Thompson     CeedCallBackend(CeedElemRestrictionGetElementSize(elem_rstr, &elem_size));
323004e4986SSebastian Grimberg     CeedCallBackend(CeedQFunctionFieldGetEvalMode(qf_input_fields[i], &eval_mode));
324ca735530SJeremy L Thompson     CeedCallBackend(CeedQFunctionFieldGetSize(qf_input_fields[i], &size));
3250d0321e0SJeremy L Thompson     // Basis action
326004e4986SSebastian Grimberg     switch (eval_mode) {
3270d0321e0SJeremy L Thompson       case CEED_EVAL_NONE:
328ca735530SJeremy L Thompson         CeedCallBackend(CeedVectorSetArray(impl->q_vecs_in[i], CEED_MEM_DEVICE, CEED_USE_POINTER, e_data[i]));
3290d0321e0SJeremy L Thompson         break;
3300d0321e0SJeremy L Thompson       case CEED_EVAL_INTERP:
3310d0321e0SJeremy L Thompson       case CEED_EVAL_GRAD:
332004e4986SSebastian Grimberg       case CEED_EVAL_DIV:
333004e4986SSebastian Grimberg       case CEED_EVAL_CURL:
334ca735530SJeremy L Thompson         CeedCallBackend(CeedOperatorFieldGetBasis(op_input_fields[i], &basis));
335004e4986SSebastian Grimberg         CeedCallBackend(CeedBasisApply(basis, num_elem, CEED_NOTRANSPOSE, eval_mode, impl->e_vecs[i], impl->q_vecs_in[i]));
3360d0321e0SJeremy L Thompson         break;
3370d0321e0SJeremy L Thompson       case CEED_EVAL_WEIGHT:
3380d0321e0SJeremy L Thompson         break;  // No action
3390d0321e0SJeremy L Thompson     }
3400d0321e0SJeremy L Thompson   }
3410d0321e0SJeremy L Thompson   return CEED_ERROR_SUCCESS;
3420d0321e0SJeremy L Thompson }
3430d0321e0SJeremy L Thompson 
3440d0321e0SJeremy L Thompson //------------------------------------------------------------------------------
3450d0321e0SJeremy L Thompson // Restore Input Vectors
3460d0321e0SJeremy L Thompson //------------------------------------------------------------------------------
347ca735530SJeremy L Thompson static inline int CeedOperatorRestoreInputs_Cuda(CeedInt num_input_fields, CeedQFunctionField *qf_input_fields, CeedOperatorField *op_input_fields,
348004e4986SSebastian Grimberg                                                  const bool skip_active, CeedScalar *e_data[2 * CEED_FIELD_MAX], CeedOperator_Cuda *impl) {
349ca735530SJeremy L Thompson   for (CeedInt i = 0; i < num_input_fields; i++) {
350004e4986SSebastian Grimberg     CeedEvalMode eval_mode;
3510d0321e0SJeremy L Thompson     CeedVector   vec;
3520d0321e0SJeremy L Thompson 
3530d0321e0SJeremy L Thompson     // Skip active input
354004e4986SSebastian Grimberg     if (skip_active) {
355ca735530SJeremy L Thompson       CeedCallBackend(CeedOperatorFieldGetVector(op_input_fields[i], &vec));
3562b730f8bSJeremy L Thompson       if (vec == CEED_VECTOR_ACTIVE) continue;
3570d0321e0SJeremy L Thompson     }
358004e4986SSebastian Grimberg     CeedCallBackend(CeedQFunctionFieldGetEvalMode(qf_input_fields[i], &eval_mode));
359004e4986SSebastian Grimberg     if (eval_mode == CEED_EVAL_WEIGHT) {  // Skip
3600d0321e0SJeremy L Thompson     } else {
361ca735530SJeremy L Thompson       if (!impl->e_vecs[i]) {  // This was a skip_restriction case
362ca735530SJeremy L Thompson         CeedCallBackend(CeedOperatorFieldGetVector(op_input_fields[i], &vec));
363ca735530SJeremy L Thompson         CeedCallBackend(CeedVectorRestoreArrayRead(vec, (const CeedScalar **)&e_data[i]));
3640d0321e0SJeremy L Thompson       } else {
365ca735530SJeremy L Thompson         CeedCallBackend(CeedVectorRestoreArrayRead(impl->e_vecs[i], (const CeedScalar **)&e_data[i]));
3660d0321e0SJeremy L Thompson       }
3670d0321e0SJeremy L Thompson     }
3680d0321e0SJeremy L Thompson   }
3690d0321e0SJeremy L Thompson   return CEED_ERROR_SUCCESS;
3700d0321e0SJeremy L Thompson }
3710d0321e0SJeremy L Thompson 
3720d0321e0SJeremy L Thompson //------------------------------------------------------------------------------
3730d0321e0SJeremy L Thompson // Apply and add to output
3740d0321e0SJeremy L Thompson //------------------------------------------------------------------------------
375ca735530SJeremy L Thompson static int CeedOperatorApplyAdd_Cuda(CeedOperator op, CeedVector in_vec, CeedVector out_vec, CeedRequest *request) {
376ca735530SJeremy L Thompson   CeedInt             Q, num_elem, elem_size, num_input_fields, num_output_fields, size;
377ca735530SJeremy L Thompson   CeedScalar         *e_data[2 * CEED_FIELD_MAX] = {NULL};
378ca735530SJeremy L Thompson   CeedQFunctionField *qf_input_fields, *qf_output_fields;
3790d0321e0SJeremy L Thompson   CeedQFunction       qf;
380004e4986SSebastian Grimberg   CeedOperatorField  *op_input_fields, *op_output_fields;
381004e4986SSebastian Grimberg   CeedOperator_Cuda  *impl;
382ca735530SJeremy L Thompson 
383ca735530SJeremy L Thompson   CeedCallBackend(CeedOperatorGetData(op, &impl));
3842b730f8bSJeremy L Thompson   CeedCallBackend(CeedOperatorGetQFunction(op, &qf));
3852b730f8bSJeremy L Thompson   CeedCallBackend(CeedOperatorGetNumQuadraturePoints(op, &Q));
386ca735530SJeremy L Thompson   CeedCallBackend(CeedOperatorGetNumElements(op, &num_elem));
387ca735530SJeremy L Thompson   CeedCallBackend(CeedOperatorGetFields(op, &num_input_fields, &op_input_fields, &num_output_fields, &op_output_fields));
388ca735530SJeremy L Thompson   CeedCallBackend(CeedQFunctionGetFields(qf, NULL, &qf_input_fields, NULL, &qf_output_fields));
3890d0321e0SJeremy L Thompson 
3900d0321e0SJeremy L Thompson   // Setup
3912b730f8bSJeremy L Thompson   CeedCallBackend(CeedOperatorSetup_Cuda(op));
3920d0321e0SJeremy L Thompson 
393004e4986SSebastian Grimberg   // Input Evecs and Restriction
394ca735530SJeremy L Thompson   CeedCallBackend(CeedOperatorSetupInputs_Cuda(num_input_fields, qf_input_fields, op_input_fields, in_vec, false, e_data, impl, request));
3950d0321e0SJeremy L Thompson 
3960d0321e0SJeremy L Thompson   // Input basis apply if needed
397ca735530SJeremy L Thompson   CeedCallBackend(CeedOperatorInputBasis_Cuda(num_elem, qf_input_fields, op_input_fields, num_input_fields, false, e_data, impl));
3980d0321e0SJeremy L Thompson 
3990d0321e0SJeremy L Thompson   // Output pointers, as necessary
400ca735530SJeremy L Thompson   for (CeedInt i = 0; i < num_output_fields; i++) {
401004e4986SSebastian Grimberg     CeedEvalMode eval_mode;
402004e4986SSebastian Grimberg 
403004e4986SSebastian Grimberg     CeedCallBackend(CeedQFunctionFieldGetEvalMode(qf_output_fields[i], &eval_mode));
404004e4986SSebastian Grimberg     if (eval_mode == CEED_EVAL_NONE) {
4050d0321e0SJeremy L Thompson       // Set the output Q-Vector to use the E-Vector data directly.
406ca735530SJeremy L Thompson       CeedCallBackend(CeedVectorGetArrayWrite(impl->e_vecs[i + impl->num_inputs], CEED_MEM_DEVICE, &e_data[i + num_input_fields]));
407ca735530SJeremy L Thompson       CeedCallBackend(CeedVectorSetArray(impl->q_vecs_out[i], CEED_MEM_DEVICE, CEED_USE_POINTER, e_data[i + num_input_fields]));
4080d0321e0SJeremy L Thompson     }
4090d0321e0SJeremy L Thompson   }
4100d0321e0SJeremy L Thompson 
4110d0321e0SJeremy L Thompson   // Q function
412ca735530SJeremy L Thompson   CeedCallBackend(CeedQFunctionApply(qf, num_elem * Q, impl->q_vecs_in, impl->q_vecs_out));
4130d0321e0SJeremy L Thompson 
4140d0321e0SJeremy L Thompson   // Output basis apply if needed
415ca735530SJeremy L Thompson   for (CeedInt i = 0; i < num_output_fields; i++) {
416004e4986SSebastian Grimberg     CeedEvalMode        eval_mode;
417edb2538eSJeremy L Thompson     CeedElemRestriction elem_rstr;
418ca735530SJeremy L Thompson     CeedBasis           basis;
419ca735530SJeremy L Thompson 
420004e4986SSebastian Grimberg     // Get elem_size, eval_mode, size
421edb2538eSJeremy L Thompson     CeedCallBackend(CeedOperatorFieldGetElemRestriction(op_output_fields[i], &elem_rstr));
422edb2538eSJeremy L Thompson     CeedCallBackend(CeedElemRestrictionGetElementSize(elem_rstr, &elem_size));
423004e4986SSebastian Grimberg     CeedCallBackend(CeedQFunctionFieldGetEvalMode(qf_output_fields[i], &eval_mode));
424ca735530SJeremy L Thompson     CeedCallBackend(CeedQFunctionFieldGetSize(qf_output_fields[i], &size));
4250d0321e0SJeremy L Thompson     // Basis action
426004e4986SSebastian Grimberg     switch (eval_mode) {
4270d0321e0SJeremy L Thompson       case CEED_EVAL_NONE:
428004e4986SSebastian Grimberg         break;  // No action
4290d0321e0SJeremy L Thompson       case CEED_EVAL_INTERP:
4300d0321e0SJeremy L Thompson       case CEED_EVAL_GRAD:
431004e4986SSebastian Grimberg       case CEED_EVAL_DIV:
432004e4986SSebastian Grimberg       case CEED_EVAL_CURL:
433ca735530SJeremy L Thompson         CeedCallBackend(CeedOperatorFieldGetBasis(op_output_fields[i], &basis));
434004e4986SSebastian Grimberg         CeedCallBackend(CeedBasisApply(basis, num_elem, CEED_TRANSPOSE, eval_mode, impl->q_vecs_out[i], impl->e_vecs[i + impl->num_inputs]));
4350d0321e0SJeremy L Thompson         break;
4360d0321e0SJeremy L Thompson       // LCOV_EXCL_START
4370d0321e0SJeremy L Thompson       case CEED_EVAL_WEIGHT: {
4386e536b99SJeremy L Thompson         return CeedError(CeedOperatorReturnCeed(op), CEED_ERROR_BACKEND, "CEED_EVAL_WEIGHT cannot be an output evaluation mode");
4390d0321e0SJeremy L Thompson         // LCOV_EXCL_STOP
4400d0321e0SJeremy L Thompson       }
4410d0321e0SJeremy L Thompson     }
442004e4986SSebastian Grimberg   }
4430d0321e0SJeremy L Thompson 
4440d0321e0SJeremy L Thompson   // Output restriction
445ca735530SJeremy L Thompson   for (CeedInt i = 0; i < num_output_fields; i++) {
446004e4986SSebastian Grimberg     CeedEvalMode        eval_mode;
447ca735530SJeremy L Thompson     CeedVector          vec;
448edb2538eSJeremy L Thompson     CeedElemRestriction elem_rstr;
449ca735530SJeremy L Thompson 
4500d0321e0SJeremy L Thompson     // Restore evec
451004e4986SSebastian Grimberg     CeedCallBackend(CeedQFunctionFieldGetEvalMode(qf_output_fields[i], &eval_mode));
452004e4986SSebastian Grimberg     if (eval_mode == CEED_EVAL_NONE) {
453ca735530SJeremy L Thompson       CeedCallBackend(CeedVectorRestoreArray(impl->e_vecs[i + impl->num_inputs], &e_data[i + num_input_fields]));
4540d0321e0SJeremy L Thompson     }
4550d0321e0SJeremy L Thompson     // Get output vector
456ca735530SJeremy L Thompson     CeedCallBackend(CeedOperatorFieldGetVector(op_output_fields[i], &vec));
4570d0321e0SJeremy L Thompson     // Restrict
458edb2538eSJeremy L Thompson     CeedCallBackend(CeedOperatorFieldGetElemRestriction(op_output_fields[i], &elem_rstr));
4590d0321e0SJeremy L Thompson     // Active
460ca735530SJeremy L Thompson     if (vec == CEED_VECTOR_ACTIVE) vec = out_vec;
4610d0321e0SJeremy L Thompson 
462edb2538eSJeremy L Thompson     CeedCallBackend(CeedElemRestrictionApply(elem_rstr, CEED_TRANSPOSE, impl->e_vecs[i + impl->num_inputs], vec, request));
4630d0321e0SJeremy L Thompson   }
4640d0321e0SJeremy L Thompson 
4650d0321e0SJeremy L Thompson   // Restore input arrays
466ca735530SJeremy L Thompson   CeedCallBackend(CeedOperatorRestoreInputs_Cuda(num_input_fields, qf_input_fields, op_input_fields, false, e_data, impl));
4670d0321e0SJeremy L Thompson   return CEED_ERROR_SUCCESS;
4680d0321e0SJeremy L Thompson }
4690d0321e0SJeremy L Thompson 
4700d0321e0SJeremy L Thompson //------------------------------------------------------------------------------
471756ca9e9SJeremy L Thompson // CeedOperator needs to connect all the named fields (be they active or passive) to the named inputs and outputs of its CeedQFunction.
472756ca9e9SJeremy L Thompson //------------------------------------------------------------------------------
473756ca9e9SJeremy L Thompson static int CeedOperatorSetupAtPoints_Cuda(CeedOperator op) {
474756ca9e9SJeremy L Thompson   Ceed                ceed;
475756ca9e9SJeremy L Thompson   bool                is_setup_done;
476756ca9e9SJeremy L Thompson   CeedInt             max_num_points = -1, num_elem, num_input_fields, num_output_fields;
477756ca9e9SJeremy L Thompson   CeedQFunctionField *qf_input_fields, *qf_output_fields;
478756ca9e9SJeremy L Thompson   CeedQFunction       qf;
479756ca9e9SJeremy L Thompson   CeedOperatorField  *op_input_fields, *op_output_fields;
480756ca9e9SJeremy L Thompson   CeedOperator_Cuda  *impl;
481756ca9e9SJeremy L Thompson 
482756ca9e9SJeremy L Thompson   CeedCallBackend(CeedOperatorIsSetupDone(op, &is_setup_done));
483756ca9e9SJeremy L Thompson   if (is_setup_done) return CEED_ERROR_SUCCESS;
484756ca9e9SJeremy L Thompson 
485756ca9e9SJeremy L Thompson   CeedCallBackend(CeedOperatorGetCeed(op, &ceed));
486756ca9e9SJeremy L Thompson   CeedCallBackend(CeedOperatorGetData(op, &impl));
487756ca9e9SJeremy L Thompson   CeedCallBackend(CeedOperatorGetQFunction(op, &qf));
488756ca9e9SJeremy L Thompson   CeedCallBackend(CeedOperatorGetNumElements(op, &num_elem));
489756ca9e9SJeremy L Thompson   CeedCallBackend(CeedOperatorGetFields(op, &num_input_fields, &op_input_fields, &num_output_fields, &op_output_fields));
490756ca9e9SJeremy L Thompson   CeedCallBackend(CeedQFunctionGetFields(qf, NULL, &qf_input_fields, NULL, &qf_output_fields));
491756ca9e9SJeremy L Thompson   {
492756ca9e9SJeremy L Thompson     CeedElemRestriction elem_rstr = NULL;
493756ca9e9SJeremy L Thompson 
494756ca9e9SJeremy L Thompson     CeedCallBackend(CeedOperatorAtPointsGetPoints(op, &elem_rstr, NULL));
495756ca9e9SJeremy L Thompson     CeedCallBackend(CeedElemRestrictionGetMaxPointsInElement(elem_rstr, &max_num_points));
496756ca9e9SJeremy L Thompson   }
497756ca9e9SJeremy L Thompson   impl->max_num_points = max_num_points;
498756ca9e9SJeremy L Thompson 
499756ca9e9SJeremy L Thompson   // Allocate
500756ca9e9SJeremy L Thompson   CeedCallBackend(CeedCalloc(num_input_fields + num_output_fields, &impl->e_vecs));
501*3aab95c0SJeremy L Thompson   CeedCallBackend(CeedCalloc(CEED_FIELD_MAX, &impl->skip_rstr_in));
502756ca9e9SJeremy L Thompson   CeedCallBackend(CeedCalloc(CEED_FIELD_MAX, &impl->input_states));
503756ca9e9SJeremy L Thompson   CeedCallBackend(CeedCalloc(CEED_FIELD_MAX, &impl->q_vecs_in));
504756ca9e9SJeremy L Thompson   CeedCallBackend(CeedCalloc(CEED_FIELD_MAX, &impl->q_vecs_out));
505756ca9e9SJeremy L Thompson   impl->num_inputs  = num_input_fields;
506756ca9e9SJeremy L Thompson   impl->num_outputs = num_output_fields;
507756ca9e9SJeremy L Thompson 
508756ca9e9SJeremy L Thompson   // Set up infield and outfield e_vecs and q_vecs
509756ca9e9SJeremy L Thompson   // Infields
510*3aab95c0SJeremy L Thompson   CeedCallBackend(CeedOperatorSetupFields_Cuda(qf, op, true, true, impl->skip_rstr_in, impl->e_vecs, impl->q_vecs_in, 0, num_input_fields,
511*3aab95c0SJeremy L Thompson                                                max_num_points, num_elem));
512756ca9e9SJeremy L Thompson   // Outfields
513*3aab95c0SJeremy L Thompson   CeedCallBackend(CeedOperatorSetupFields_Cuda(qf, op, false, true, NULL, impl->e_vecs, impl->q_vecs_out, num_input_fields, num_output_fields,
514756ca9e9SJeremy L Thompson                                                max_num_points, num_elem));
515756ca9e9SJeremy L Thompson 
516756ca9e9SJeremy L Thompson   CeedCallBackend(CeedOperatorSetSetupDone(op));
517756ca9e9SJeremy L Thompson   return CEED_ERROR_SUCCESS;
518756ca9e9SJeremy L Thompson }
519756ca9e9SJeremy L Thompson 
520756ca9e9SJeremy L Thompson //------------------------------------------------------------------------------
52167d9480aSJeremy L Thompson // Input Basis Action AtPoints
522756ca9e9SJeremy L Thompson //------------------------------------------------------------------------------
523756ca9e9SJeremy L Thompson static inline int CeedOperatorInputBasisAtPoints_Cuda(CeedInt num_elem, const CeedInt *num_points, CeedQFunctionField *qf_input_fields,
524756ca9e9SJeremy L Thompson                                                       CeedOperatorField *op_input_fields, CeedInt num_input_fields, const bool skip_active,
525756ca9e9SJeremy L Thompson                                                       CeedScalar *e_data[2 * CEED_FIELD_MAX], CeedOperator_Cuda *impl) {
526756ca9e9SJeremy L Thompson   for (CeedInt i = 0; i < num_input_fields; i++) {
527756ca9e9SJeremy L Thompson     CeedInt             elem_size, size;
528756ca9e9SJeremy L Thompson     CeedEvalMode        eval_mode;
529756ca9e9SJeremy L Thompson     CeedElemRestriction elem_rstr;
530756ca9e9SJeremy L Thompson     CeedBasis           basis;
531756ca9e9SJeremy L Thompson 
532756ca9e9SJeremy L Thompson     // Skip active input
533756ca9e9SJeremy L Thompson     if (skip_active) {
534756ca9e9SJeremy L Thompson       CeedVector vec;
535756ca9e9SJeremy L Thompson 
536756ca9e9SJeremy L Thompson       CeedCallBackend(CeedOperatorFieldGetVector(op_input_fields[i], &vec));
537756ca9e9SJeremy L Thompson       if (vec == CEED_VECTOR_ACTIVE) continue;
538756ca9e9SJeremy L Thompson     }
539756ca9e9SJeremy L Thompson     // Get elem_size, eval_mode, size
540756ca9e9SJeremy L Thompson     CeedCallBackend(CeedOperatorFieldGetElemRestriction(op_input_fields[i], &elem_rstr));
541756ca9e9SJeremy L Thompson     CeedCallBackend(CeedElemRestrictionGetElementSize(elem_rstr, &elem_size));
542756ca9e9SJeremy L Thompson     CeedCallBackend(CeedQFunctionFieldGetEvalMode(qf_input_fields[i], &eval_mode));
543756ca9e9SJeremy L Thompson     CeedCallBackend(CeedQFunctionFieldGetSize(qf_input_fields[i], &size));
544756ca9e9SJeremy L Thompson     // Basis action
545756ca9e9SJeremy L Thompson     switch (eval_mode) {
546756ca9e9SJeremy L Thompson       case CEED_EVAL_NONE:
547756ca9e9SJeremy L Thompson         CeedCallBackend(CeedVectorSetArray(impl->q_vecs_in[i], CEED_MEM_DEVICE, CEED_USE_POINTER, e_data[i]));
548756ca9e9SJeremy L Thompson         break;
549756ca9e9SJeremy L Thompson       case CEED_EVAL_INTERP:
550756ca9e9SJeremy L Thompson       case CEED_EVAL_GRAD:
551756ca9e9SJeremy L Thompson       case CEED_EVAL_DIV:
552756ca9e9SJeremy L Thompson       case CEED_EVAL_CURL:
553756ca9e9SJeremy L Thompson         CeedCallBackend(CeedOperatorFieldGetBasis(op_input_fields[i], &basis));
554756ca9e9SJeremy L Thompson         CeedCallBackend(CeedBasisApplyAtPoints(basis, num_elem, num_points, CEED_NOTRANSPOSE, eval_mode, impl->point_coords_elem, impl->e_vecs[i],
555756ca9e9SJeremy L Thompson                                                impl->q_vecs_in[i]));
556756ca9e9SJeremy L Thompson         break;
557756ca9e9SJeremy L Thompson       case CEED_EVAL_WEIGHT:
558756ca9e9SJeremy L Thompson         break;  // No action
559756ca9e9SJeremy L Thompson     }
560756ca9e9SJeremy L Thompson   }
561756ca9e9SJeremy L Thompson   return CEED_ERROR_SUCCESS;
562756ca9e9SJeremy L Thompson }
563756ca9e9SJeremy L Thompson 
564756ca9e9SJeremy L Thompson //------------------------------------------------------------------------------
56567d9480aSJeremy L Thompson // Apply and add to output AtPoints
566756ca9e9SJeremy L Thompson //------------------------------------------------------------------------------
567756ca9e9SJeremy L Thompson static int CeedOperatorApplyAddAtPoints_Cuda(CeedOperator op, CeedVector in_vec, CeedVector out_vec, CeedRequest *request) {
568756ca9e9SJeremy L Thompson   CeedInt             max_num_points, num_elem, elem_size, num_input_fields, num_output_fields, size;
569756ca9e9SJeremy L Thompson   CeedScalar         *e_data[2 * CEED_FIELD_MAX] = {NULL};
570756ca9e9SJeremy L Thompson   CeedQFunctionField *qf_input_fields, *qf_output_fields;
571756ca9e9SJeremy L Thompson   CeedQFunction       qf;
572756ca9e9SJeremy L Thompson   CeedOperatorField  *op_input_fields, *op_output_fields;
573756ca9e9SJeremy L Thompson   CeedOperator_Cuda  *impl;
574756ca9e9SJeremy L Thompson 
575756ca9e9SJeremy L Thompson   CeedCallBackend(CeedOperatorGetData(op, &impl));
576756ca9e9SJeremy L Thompson   CeedCallBackend(CeedOperatorGetQFunction(op, &qf));
577756ca9e9SJeremy L Thompson   CeedCallBackend(CeedOperatorGetNumElements(op, &num_elem));
578756ca9e9SJeremy L Thompson   CeedCallBackend(CeedOperatorGetFields(op, &num_input_fields, &op_input_fields, &num_output_fields, &op_output_fields));
579756ca9e9SJeremy L Thompson   CeedCallBackend(CeedQFunctionGetFields(qf, NULL, &qf_input_fields, NULL, &qf_output_fields));
580756ca9e9SJeremy L Thompson   CeedInt num_points[num_elem];
581756ca9e9SJeremy L Thompson 
582756ca9e9SJeremy L Thompson   // Setup
583756ca9e9SJeremy L Thompson   CeedCallBackend(CeedOperatorSetupAtPoints_Cuda(op));
584756ca9e9SJeremy L Thompson   max_num_points = impl->max_num_points;
585756ca9e9SJeremy L Thompson   for (CeedInt i = 0; i < num_elem; i++) num_points[i] = max_num_points;
586756ca9e9SJeremy L Thompson 
587756ca9e9SJeremy L Thompson   // Input Evecs and Restriction
588756ca9e9SJeremy L Thompson   CeedCallBackend(CeedOperatorSetupInputs_Cuda(num_input_fields, qf_input_fields, op_input_fields, in_vec, false, e_data, impl, request));
589756ca9e9SJeremy L Thompson 
590756ca9e9SJeremy L Thompson   // Get point coordinates
591756ca9e9SJeremy L Thompson   if (!impl->point_coords_elem) {
592756ca9e9SJeremy L Thompson     CeedVector          point_coords = NULL;
593756ca9e9SJeremy L Thompson     CeedElemRestriction rstr_points  = NULL;
594756ca9e9SJeremy L Thompson 
595756ca9e9SJeremy L Thompson     CeedCallBackend(CeedOperatorAtPointsGetPoints(op, &rstr_points, &point_coords));
596756ca9e9SJeremy L Thompson     CeedCallBackend(CeedElemRestrictionCreateVector(rstr_points, NULL, &impl->point_coords_elem));
597756ca9e9SJeremy L Thompson     CeedCallBackend(CeedElemRestrictionApply(rstr_points, CEED_NOTRANSPOSE, point_coords, impl->point_coords_elem, request));
598756ca9e9SJeremy L Thompson   }
599756ca9e9SJeremy L Thompson 
600756ca9e9SJeremy L Thompson   // Input basis apply if needed
601756ca9e9SJeremy L Thompson   CeedCallBackend(CeedOperatorInputBasisAtPoints_Cuda(num_elem, num_points, qf_input_fields, op_input_fields, num_input_fields, false, e_data, impl));
602756ca9e9SJeremy L Thompson 
603756ca9e9SJeremy L Thompson   // Output pointers, as necessary
604756ca9e9SJeremy L Thompson   for (CeedInt i = 0; i < num_output_fields; i++) {
605756ca9e9SJeremy L Thompson     CeedEvalMode eval_mode;
606756ca9e9SJeremy L Thompson 
607756ca9e9SJeremy L Thompson     CeedCallBackend(CeedQFunctionFieldGetEvalMode(qf_output_fields[i], &eval_mode));
608756ca9e9SJeremy L Thompson     if (eval_mode == CEED_EVAL_NONE) {
609756ca9e9SJeremy L Thompson       // Set the output Q-Vector to use the E-Vector data directly.
610756ca9e9SJeremy L Thompson       CeedCallBackend(CeedVectorGetArrayWrite(impl->e_vecs[i + impl->num_inputs], CEED_MEM_DEVICE, &e_data[i + num_input_fields]));
611756ca9e9SJeremy L Thompson       CeedCallBackend(CeedVectorSetArray(impl->q_vecs_out[i], CEED_MEM_DEVICE, CEED_USE_POINTER, e_data[i + num_input_fields]));
612756ca9e9SJeremy L Thompson     }
613756ca9e9SJeremy L Thompson   }
614756ca9e9SJeremy L Thompson 
615756ca9e9SJeremy L Thompson   // Q function
616756ca9e9SJeremy L Thompson   CeedCallBackend(CeedQFunctionApply(qf, num_elem * max_num_points, impl->q_vecs_in, impl->q_vecs_out));
617756ca9e9SJeremy L Thompson 
618756ca9e9SJeremy L Thompson   // Output basis apply if needed
619756ca9e9SJeremy L Thompson   for (CeedInt i = 0; i < num_output_fields; i++) {
620756ca9e9SJeremy L Thompson     CeedEvalMode        eval_mode;
621756ca9e9SJeremy L Thompson     CeedElemRestriction elem_rstr;
622756ca9e9SJeremy L Thompson     CeedBasis           basis;
623756ca9e9SJeremy L Thompson 
624756ca9e9SJeremy L Thompson     // Get elem_size, eval_mode, size
625756ca9e9SJeremy L Thompson     CeedCallBackend(CeedOperatorFieldGetElemRestriction(op_output_fields[i], &elem_rstr));
626756ca9e9SJeremy L Thompson     CeedCallBackend(CeedElemRestrictionGetElementSize(elem_rstr, &elem_size));
627756ca9e9SJeremy L Thompson     CeedCallBackend(CeedQFunctionFieldGetEvalMode(qf_output_fields[i], &eval_mode));
628756ca9e9SJeremy L Thompson     CeedCallBackend(CeedQFunctionFieldGetSize(qf_output_fields[i], &size));
629756ca9e9SJeremy L Thompson     // Basis action
630756ca9e9SJeremy L Thompson     switch (eval_mode) {
631756ca9e9SJeremy L Thompson       case CEED_EVAL_NONE:
632756ca9e9SJeremy L Thompson         break;  // No action
633756ca9e9SJeremy L Thompson       case CEED_EVAL_INTERP:
634756ca9e9SJeremy L Thompson       case CEED_EVAL_GRAD:
635756ca9e9SJeremy L Thompson       case CEED_EVAL_DIV:
636756ca9e9SJeremy L Thompson       case CEED_EVAL_CURL:
637756ca9e9SJeremy L Thompson         CeedCallBackend(CeedOperatorFieldGetBasis(op_output_fields[i], &basis));
638756ca9e9SJeremy L Thompson         CeedCallBackend(CeedBasisApplyAtPoints(basis, num_elem, num_points, CEED_TRANSPOSE, eval_mode, impl->point_coords_elem, impl->q_vecs_out[i],
639756ca9e9SJeremy L Thompson                                                impl->e_vecs[i + impl->num_inputs]));
640756ca9e9SJeremy L Thompson         break;
641756ca9e9SJeremy L Thompson       // LCOV_EXCL_START
642756ca9e9SJeremy L Thompson       case CEED_EVAL_WEIGHT: {
643756ca9e9SJeremy L Thompson         return CeedError(CeedOperatorReturnCeed(op), CEED_ERROR_BACKEND, "CEED_EVAL_WEIGHT cannot be an output evaluation mode");
644756ca9e9SJeremy L Thompson         // LCOV_EXCL_STOP
645756ca9e9SJeremy L Thompson       }
646756ca9e9SJeremy L Thompson     }
647756ca9e9SJeremy L Thompson   }
648756ca9e9SJeremy L Thompson 
649756ca9e9SJeremy L Thompson   // Output restriction
650756ca9e9SJeremy L Thompson   for (CeedInt i = 0; i < num_output_fields; i++) {
651756ca9e9SJeremy L Thompson     CeedEvalMode        eval_mode;
652756ca9e9SJeremy L Thompson     CeedVector          vec;
653756ca9e9SJeremy L Thompson     CeedElemRestriction elem_rstr;
654756ca9e9SJeremy L Thompson 
655756ca9e9SJeremy L Thompson     // Restore evec
656756ca9e9SJeremy L Thompson     CeedCallBackend(CeedQFunctionFieldGetEvalMode(qf_output_fields[i], &eval_mode));
657756ca9e9SJeremy L Thompson     if (eval_mode == CEED_EVAL_NONE) {
658756ca9e9SJeremy L Thompson       CeedCallBackend(CeedVectorRestoreArray(impl->e_vecs[i + impl->num_inputs], &e_data[i + num_input_fields]));
659756ca9e9SJeremy L Thompson     }
660756ca9e9SJeremy L Thompson     // Get output vector
661756ca9e9SJeremy L Thompson     CeedCallBackend(CeedOperatorFieldGetVector(op_output_fields[i], &vec));
662756ca9e9SJeremy L Thompson     // Restrict
663756ca9e9SJeremy L Thompson     CeedCallBackend(CeedOperatorFieldGetElemRestriction(op_output_fields[i], &elem_rstr));
664756ca9e9SJeremy L Thompson     // Active
665756ca9e9SJeremy L Thompson     if (vec == CEED_VECTOR_ACTIVE) vec = out_vec;
666756ca9e9SJeremy L Thompson 
667756ca9e9SJeremy L Thompson     CeedCallBackend(CeedElemRestrictionApply(elem_rstr, CEED_TRANSPOSE, impl->e_vecs[i + impl->num_inputs], vec, request));
668756ca9e9SJeremy L Thompson   }
669756ca9e9SJeremy L Thompson 
670756ca9e9SJeremy L Thompson   // Restore input arrays
671756ca9e9SJeremy L Thompson   CeedCallBackend(CeedOperatorRestoreInputs_Cuda(num_input_fields, qf_input_fields, op_input_fields, false, e_data, impl));
672756ca9e9SJeremy L Thompson   return CEED_ERROR_SUCCESS;
673756ca9e9SJeremy L Thompson }
674756ca9e9SJeremy L Thompson 
675756ca9e9SJeremy L Thompson //------------------------------------------------------------------------------
676004e4986SSebastian Grimberg // Linear QFunction Assembly Core
6770d0321e0SJeremy L Thompson //------------------------------------------------------------------------------
6782b730f8bSJeremy L Thompson static inline int CeedOperatorLinearAssembleQFunctionCore_Cuda(CeedOperator op, bool build_objects, CeedVector *assembled, CeedElemRestriction *rstr,
6790d0321e0SJeremy L Thompson                                                                CeedRequest *request) {
680ca735530SJeremy L Thompson   Ceed                ceed, ceed_parent;
681ca735530SJeremy L Thompson   CeedInt             num_active_in, num_active_out, Q, num_elem, num_input_fields, num_output_fields, size;
682ca735530SJeremy L Thompson   CeedScalar         *assembled_array, *e_data[2 * CEED_FIELD_MAX] = {NULL};
683ca735530SJeremy L Thompson   CeedVector         *active_inputs;
684ca735530SJeremy L Thompson   CeedQFunctionField *qf_input_fields, *qf_output_fields;
685ca735530SJeremy L Thompson   CeedQFunction       qf;
686ca735530SJeremy L Thompson   CeedOperatorField  *op_input_fields, *op_output_fields;
687ca735530SJeremy L Thompson   CeedOperator_Cuda  *impl;
688ca735530SJeremy L Thompson 
6892b730f8bSJeremy L Thompson   CeedCallBackend(CeedOperatorGetCeed(op, &ceed));
690ca735530SJeremy L Thompson   CeedCallBackend(CeedOperatorGetFallbackParentCeed(op, &ceed_parent));
691e984cf9aSJeremy L Thompson   CeedCallBackend(CeedOperatorGetData(op, &impl));
692e984cf9aSJeremy L Thompson   CeedCallBackend(CeedOperatorGetNumQuadraturePoints(op, &Q));
693ca735530SJeremy L Thompson   CeedCallBackend(CeedOperatorGetNumElements(op, &num_elem));
694e984cf9aSJeremy L Thompson   CeedCallBackend(CeedOperatorGetQFunction(op, &qf));
695ca735530SJeremy L Thompson   CeedCallBackend(CeedQFunctionGetFields(qf, NULL, &qf_input_fields, NULL, &qf_output_fields));
696ca735530SJeremy L Thompson   CeedCallBackend(CeedOperatorGetFields(op, &num_input_fields, &op_input_fields, &num_output_fields, &op_output_fields));
697ca735530SJeremy L Thompson   active_inputs = impl->qf_active_in;
698ca735530SJeremy L Thompson   num_active_in = impl->num_active_in, num_active_out = impl->num_active_out;
6990d0321e0SJeremy L Thompson 
7000d0321e0SJeremy L Thompson   // Setup
7012b730f8bSJeremy L Thompson   CeedCallBackend(CeedOperatorSetup_Cuda(op));
7020d0321e0SJeremy L Thompson 
703004e4986SSebastian Grimberg   // Input Evecs and Restriction
704ca735530SJeremy L Thompson   CeedCallBackend(CeedOperatorSetupInputs_Cuda(num_input_fields, qf_input_fields, op_input_fields, NULL, true, e_data, impl, request));
7050d0321e0SJeremy L Thompson 
7060d0321e0SJeremy L Thompson   // Count number of active input fields
707ca735530SJeremy L Thompson   if (!num_active_in) {
708ca735530SJeremy L Thompson     for (CeedInt i = 0; i < num_input_fields; i++) {
709ca735530SJeremy L Thompson       CeedScalar *q_vec_array;
710ca735530SJeremy L Thompson       CeedVector  vec;
711ca735530SJeremy L Thompson 
7120d0321e0SJeremy L Thompson       // Get input vector
713ca735530SJeremy L Thompson       CeedCallBackend(CeedOperatorFieldGetVector(op_input_fields[i], &vec));
7140d0321e0SJeremy L Thompson       // Check if active input
7150d0321e0SJeremy L Thompson       if (vec == CEED_VECTOR_ACTIVE) {
716ca735530SJeremy L Thompson         CeedCallBackend(CeedQFunctionFieldGetSize(qf_input_fields[i], &size));
717ca735530SJeremy L Thompson         CeedCallBackend(CeedVectorSetValue(impl->q_vecs_in[i], 0.0));
718ca735530SJeremy L Thompson         CeedCallBackend(CeedVectorGetArray(impl->q_vecs_in[i], CEED_MEM_DEVICE, &q_vec_array));
719ca735530SJeremy L Thompson         CeedCallBackend(CeedRealloc(num_active_in + size, &active_inputs));
7200d0321e0SJeremy L Thompson         for (CeedInt field = 0; field < size; field++) {
721004e4986SSebastian Grimberg           CeedSize q_size = (CeedSize)Q * num_elem;
722004e4986SSebastian Grimberg 
723ca735530SJeremy L Thompson           CeedCallBackend(CeedVectorCreate(ceed, q_size, &active_inputs[num_active_in + field]));
724ca735530SJeremy L Thompson           CeedCallBackend(
725ca735530SJeremy L Thompson               CeedVectorSetArray(active_inputs[num_active_in + field], CEED_MEM_DEVICE, CEED_USE_POINTER, &q_vec_array[field * Q * num_elem]));
7260d0321e0SJeremy L Thompson         }
727ca735530SJeremy L Thompson         num_active_in += size;
728ca735530SJeremy L Thompson         CeedCallBackend(CeedVectorRestoreArray(impl->q_vecs_in[i], &q_vec_array));
7290d0321e0SJeremy L Thompson       }
7300d0321e0SJeremy L Thompson     }
731ca735530SJeremy L Thompson     impl->num_active_in = num_active_in;
732ca735530SJeremy L Thompson     impl->qf_active_in  = active_inputs;
7330d0321e0SJeremy L Thompson   }
7340d0321e0SJeremy L Thompson 
7350d0321e0SJeremy L Thompson   // Count number of active output fields
736ca735530SJeremy L Thompson   if (!num_active_out) {
737ca735530SJeremy L Thompson     for (CeedInt i = 0; i < num_output_fields; i++) {
738ca735530SJeremy L Thompson       CeedVector vec;
739ca735530SJeremy L Thompson 
7400d0321e0SJeremy L Thompson       // Get output vector
741ca735530SJeremy L Thompson       CeedCallBackend(CeedOperatorFieldGetVector(op_output_fields[i], &vec));
7420d0321e0SJeremy L Thompson       // Check if active output
7430d0321e0SJeremy L Thompson       if (vec == CEED_VECTOR_ACTIVE) {
744ca735530SJeremy L Thompson         CeedCallBackend(CeedQFunctionFieldGetSize(qf_output_fields[i], &size));
745ca735530SJeremy L Thompson         num_active_out += size;
7460d0321e0SJeremy L Thompson       }
7470d0321e0SJeremy L Thompson     }
748ca735530SJeremy L Thompson     impl->num_active_out = num_active_out;
7490d0321e0SJeremy L Thompson   }
7500d0321e0SJeremy L Thompson 
7510d0321e0SJeremy L Thompson   // Check sizes
752ca735530SJeremy L Thompson   CeedCheck(num_active_in > 0 && num_active_out > 0, ceed, CEED_ERROR_BACKEND, "Cannot assemble QFunction without active inputs and outputs");
7530d0321e0SJeremy L Thompson 
7540d0321e0SJeremy L Thompson   // Build objects if needed
7550d0321e0SJeremy L Thompson   if (build_objects) {
756004e4986SSebastian Grimberg     CeedSize l_size     = (CeedSize)num_elem * Q * num_active_in * num_active_out;
757ca735530SJeremy L Thompson     CeedInt  strides[3] = {1, num_elem * Q, Q}; /* *NOPAD* */
758004e4986SSebastian Grimberg 
759004e4986SSebastian Grimberg     // Create output restriction
760ca735530SJeremy L Thompson     CeedCallBackend(CeedElemRestrictionCreateStrided(ceed_parent, num_elem, Q, num_active_in * num_active_out,
7610a5597ceSJeremy L Thompson                                                      (CeedSize)num_active_in * (CeedSize)num_active_out * (CeedSize)num_elem * (CeedSize)Q, strides,
7620a5597ceSJeremy L Thompson                                                      rstr));
7630d0321e0SJeremy L Thompson     // Create assembled vector
764ca735530SJeremy L Thompson     CeedCallBackend(CeedVectorCreate(ceed_parent, l_size, assembled));
7650d0321e0SJeremy L Thompson   }
7662b730f8bSJeremy L Thompson   CeedCallBackend(CeedVectorSetValue(*assembled, 0.0));
767ca735530SJeremy L Thompson   CeedCallBackend(CeedVectorGetArray(*assembled, CEED_MEM_DEVICE, &assembled_array));
7680d0321e0SJeremy L Thompson 
7690d0321e0SJeremy L Thompson   // Input basis apply
770ca735530SJeremy L Thompson   CeedCallBackend(CeedOperatorInputBasis_Cuda(num_elem, qf_input_fields, op_input_fields, num_input_fields, true, e_data, impl));
7710d0321e0SJeremy L Thompson 
7720d0321e0SJeremy L Thompson   // Assemble QFunction
773ca735530SJeremy L Thompson   for (CeedInt in = 0; in < num_active_in; in++) {
7740d0321e0SJeremy L Thompson     // Set Inputs
775ca735530SJeremy L Thompson     CeedCallBackend(CeedVectorSetValue(active_inputs[in], 1.0));
776ca735530SJeremy L Thompson     if (num_active_in > 1) {
777ca735530SJeremy L Thompson       CeedCallBackend(CeedVectorSetValue(active_inputs[(in + num_active_in - 1) % num_active_in], 0.0));
7780d0321e0SJeremy L Thompson     }
7790d0321e0SJeremy L Thompson     // Set Outputs
780ca735530SJeremy L Thompson     for (CeedInt out = 0; out < num_output_fields; out++) {
781ca735530SJeremy L Thompson       CeedVector vec;
782ca735530SJeremy L Thompson 
7830d0321e0SJeremy L Thompson       // Get output vector
784ca735530SJeremy L Thompson       CeedCallBackend(CeedOperatorFieldGetVector(op_output_fields[out], &vec));
7850d0321e0SJeremy L Thompson       // Check if active output
7860d0321e0SJeremy L Thompson       if (vec == CEED_VECTOR_ACTIVE) {
787ca735530SJeremy L Thompson         CeedCallBackend(CeedVectorSetArray(impl->q_vecs_out[out], CEED_MEM_DEVICE, CEED_USE_POINTER, assembled_array));
788ca735530SJeremy L Thompson         CeedCallBackend(CeedQFunctionFieldGetSize(qf_output_fields[out], &size));
789ca735530SJeremy L Thompson         assembled_array += size * Q * num_elem;  // Advance the pointer by the size of the output
7900d0321e0SJeremy L Thompson       }
7910d0321e0SJeremy L Thompson     }
7920d0321e0SJeremy L Thompson     // Apply QFunction
793ca735530SJeremy L Thompson     CeedCallBackend(CeedQFunctionApply(qf, Q * num_elem, impl->q_vecs_in, impl->q_vecs_out));
7940d0321e0SJeremy L Thompson   }
7950d0321e0SJeremy L Thompson 
796ca735530SJeremy L Thompson   // Un-set output q_vecs to prevent accidental overwrite of Assembled
797ca735530SJeremy L Thompson   for (CeedInt out = 0; out < num_output_fields; out++) {
798ca735530SJeremy L Thompson     CeedVector vec;
799ca735530SJeremy L Thompson 
8000d0321e0SJeremy L Thompson     // Get output vector
801ca735530SJeremy L Thompson     CeedCallBackend(CeedOperatorFieldGetVector(op_output_fields[out], &vec));
8020d0321e0SJeremy L Thompson     // Check if active output
8030d0321e0SJeremy L Thompson     if (vec == CEED_VECTOR_ACTIVE) {
804ca735530SJeremy L Thompson       CeedCallBackend(CeedVectorTakeArray(impl->q_vecs_out[out], CEED_MEM_DEVICE, NULL));
8050d0321e0SJeremy L Thompson     }
8060d0321e0SJeremy L Thompson   }
8070d0321e0SJeremy L Thompson 
8080d0321e0SJeremy L Thompson   // Restore input arrays
809ca735530SJeremy L Thompson   CeedCallBackend(CeedOperatorRestoreInputs_Cuda(num_input_fields, qf_input_fields, op_input_fields, true, e_data, impl));
8100d0321e0SJeremy L Thompson 
8110d0321e0SJeremy L Thompson   // Restore output
812ca735530SJeremy L Thompson   CeedCallBackend(CeedVectorRestoreArray(*assembled, &assembled_array));
8130d0321e0SJeremy L Thompson   return CEED_ERROR_SUCCESS;
8140d0321e0SJeremy L Thompson }
8150d0321e0SJeremy L Thompson 
8160d0321e0SJeremy L Thompson //------------------------------------------------------------------------------
8170d0321e0SJeremy L Thompson // Assemble Linear QFunction
8180d0321e0SJeremy L Thompson //------------------------------------------------------------------------------
8192b730f8bSJeremy L Thompson static int CeedOperatorLinearAssembleQFunction_Cuda(CeedOperator op, CeedVector *assembled, CeedElemRestriction *rstr, CeedRequest *request) {
8202b730f8bSJeremy L Thompson   return CeedOperatorLinearAssembleQFunctionCore_Cuda(op, true, assembled, rstr, request);
8210d0321e0SJeremy L Thompson }
8220d0321e0SJeremy L Thompson 
8230d0321e0SJeremy L Thompson //------------------------------------------------------------------------------
8240d0321e0SJeremy L Thompson // Update Assembled Linear QFunction
8250d0321e0SJeremy L Thompson //------------------------------------------------------------------------------
8262b730f8bSJeremy L Thompson static int CeedOperatorLinearAssembleQFunctionUpdate_Cuda(CeedOperator op, CeedVector assembled, CeedElemRestriction rstr, CeedRequest *request) {
8272b730f8bSJeremy L Thompson   return CeedOperatorLinearAssembleQFunctionCore_Cuda(op, false, &assembled, &rstr, request);
8280d0321e0SJeremy L Thompson }
8290d0321e0SJeremy L Thompson 
8300d0321e0SJeremy L Thompson //------------------------------------------------------------------------------
831004e4986SSebastian Grimberg // Assemble Diagonal Setup
8320d0321e0SJeremy L Thompson //------------------------------------------------------------------------------
833cbfe683aSSebastian Grimberg static inline int CeedOperatorAssembleDiagonalSetup_Cuda(CeedOperator op) {
8340d0321e0SJeremy L Thompson   Ceed                ceed;
835004e4986SSebastian Grimberg   CeedInt             num_input_fields, num_output_fields, num_eval_modes_in = 0, num_eval_modes_out = 0;
836cbfe683aSSebastian Grimberg   CeedInt             q_comp, num_nodes, num_qpts;
837004e4986SSebastian Grimberg   CeedEvalMode       *eval_modes_in = NULL, *eval_modes_out = NULL;
838ca735530SJeremy L Thompson   CeedBasis           basis_in = NULL, basis_out = NULL;
839ca735530SJeremy L Thompson   CeedQFunctionField *qf_fields;
8400d0321e0SJeremy L Thompson   CeedQFunction       qf;
841ca735530SJeremy L Thompson   CeedOperatorField  *op_fields;
842ca735530SJeremy L Thompson   CeedOperator_Cuda  *impl;
843ca735530SJeremy L Thompson 
844ca735530SJeremy L Thompson   CeedCallBackend(CeedOperatorGetCeed(op, &ceed));
8452b730f8bSJeremy L Thompson   CeedCallBackend(CeedOperatorGetQFunction(op, &qf));
846ca735530SJeremy L Thompson   CeedCallBackend(CeedQFunctionGetNumArgs(qf, &num_input_fields, &num_output_fields));
8470d0321e0SJeremy L Thompson 
8480d0321e0SJeremy L Thompson   // Determine active input basis
849ca735530SJeremy L Thompson   CeedCallBackend(CeedOperatorGetFields(op, NULL, &op_fields, NULL, NULL));
850ca735530SJeremy L Thompson   CeedCallBackend(CeedQFunctionGetFields(qf, NULL, &qf_fields, NULL, NULL));
851ca735530SJeremy L Thompson   for (CeedInt i = 0; i < num_input_fields; i++) {
8520d0321e0SJeremy L Thompson     CeedVector vec;
853ca735530SJeremy L Thompson 
854ca735530SJeremy L Thompson     CeedCallBackend(CeedOperatorFieldGetVector(op_fields[i], &vec));
8550d0321e0SJeremy L Thompson     if (vec == CEED_VECTOR_ACTIVE) {
856004e4986SSebastian Grimberg       CeedBasis    basis;
857004e4986SSebastian Grimberg       CeedEvalMode eval_mode;
858ca735530SJeremy L Thompson 
859004e4986SSebastian Grimberg       CeedCallBackend(CeedOperatorFieldGetBasis(op_fields[i], &basis));
860004e4986SSebastian Grimberg       CeedCheck(!basis_in || basis_in == basis, ceed, CEED_ERROR_BACKEND,
861004e4986SSebastian Grimberg                 "Backend does not implement operator diagonal assembly with multiple active bases");
862004e4986SSebastian Grimberg       basis_in = basis;
863004e4986SSebastian Grimberg       CeedCallBackend(CeedQFunctionFieldGetEvalMode(qf_fields[i], &eval_mode));
864004e4986SSebastian Grimberg       CeedCallBackend(CeedBasisGetNumQuadratureComponents(basis_in, eval_mode, &q_comp));
865004e4986SSebastian Grimberg       if (eval_mode != CEED_EVAL_WEIGHT) {
866004e4986SSebastian Grimberg         // q_comp = 1 if CEED_EVAL_NONE, CEED_EVAL_WEIGHT caught by QF assembly
867004e4986SSebastian Grimberg         CeedCallBackend(CeedRealloc(num_eval_modes_in + q_comp, &eval_modes_in));
868004e4986SSebastian Grimberg         for (CeedInt d = 0; d < q_comp; d++) eval_modes_in[num_eval_modes_in + d] = eval_mode;
869004e4986SSebastian Grimberg         num_eval_modes_in += q_comp;
8700d0321e0SJeremy L Thompson       }
8710d0321e0SJeremy L Thompson     }
8720d0321e0SJeremy L Thompson   }
8730d0321e0SJeremy L Thompson 
8740d0321e0SJeremy L Thompson   // Determine active output basis
875ca735530SJeremy L Thompson   CeedCallBackend(CeedOperatorGetFields(op, NULL, NULL, NULL, &op_fields));
876ca735530SJeremy L Thompson   CeedCallBackend(CeedQFunctionGetFields(qf, NULL, NULL, NULL, &qf_fields));
877ca735530SJeremy L Thompson   for (CeedInt i = 0; i < num_output_fields; i++) {
8780d0321e0SJeremy L Thompson     CeedVector vec;
879ca735530SJeremy L Thompson 
880ca735530SJeremy L Thompson     CeedCallBackend(CeedOperatorFieldGetVector(op_fields[i], &vec));
8810d0321e0SJeremy L Thompson     if (vec == CEED_VECTOR_ACTIVE) {
882004e4986SSebastian Grimberg       CeedBasis    basis;
883004e4986SSebastian Grimberg       CeedEvalMode eval_mode;
884ca735530SJeremy L Thompson 
885004e4986SSebastian Grimberg       CeedCallBackend(CeedOperatorFieldGetBasis(op_fields[i], &basis));
886004e4986SSebastian Grimberg       CeedCheck(!basis_out || basis_out == basis, ceed, CEED_ERROR_BACKEND,
887004e4986SSebastian Grimberg                 "Backend does not implement operator diagonal assembly with multiple active bases");
888004e4986SSebastian Grimberg       basis_out = basis;
889004e4986SSebastian Grimberg       CeedCallBackend(CeedQFunctionFieldGetEvalMode(qf_fields[i], &eval_mode));
890004e4986SSebastian Grimberg       CeedCallBackend(CeedBasisGetNumQuadratureComponents(basis_out, eval_mode, &q_comp));
891004e4986SSebastian Grimberg       if (eval_mode != CEED_EVAL_WEIGHT) {
892004e4986SSebastian Grimberg         // q_comp = 1 if CEED_EVAL_NONE, CEED_EVAL_WEIGHT caught by QF assembly
893004e4986SSebastian Grimberg         CeedCallBackend(CeedRealloc(num_eval_modes_out + q_comp, &eval_modes_out));
894004e4986SSebastian Grimberg         for (CeedInt d = 0; d < q_comp; d++) eval_modes_out[num_eval_modes_out + d] = eval_mode;
895004e4986SSebastian Grimberg         num_eval_modes_out += q_comp;
8960d0321e0SJeremy L Thompson       }
8970d0321e0SJeremy L Thompson     }
8980d0321e0SJeremy L Thompson   }
8990d0321e0SJeremy L Thompson 
9000d0321e0SJeremy L Thompson   // Operator data struct
9012b730f8bSJeremy L Thompson   CeedCallBackend(CeedOperatorGetData(op, &impl));
9022b730f8bSJeremy L Thompson   CeedCallBackend(CeedCalloc(1, &impl->diag));
9030d0321e0SJeremy L Thompson   CeedOperatorDiag_Cuda *diag = impl->diag;
904ca735530SJeremy L Thompson 
905cbfe683aSSebastian Grimberg   // Basis matrices
906004e4986SSebastian Grimberg   CeedCallBackend(CeedBasisGetNumNodes(basis_in, &num_nodes));
907004e4986SSebastian Grimberg   if (basis_in == CEED_BASIS_NONE) num_qpts = num_nodes;
908004e4986SSebastian Grimberg   else CeedCallBackend(CeedBasisGetNumQuadraturePoints(basis_in, &num_qpts));
909004e4986SSebastian Grimberg   const CeedInt interp_bytes     = num_nodes * num_qpts * sizeof(CeedScalar);
910004e4986SSebastian Grimberg   const CeedInt eval_modes_bytes = sizeof(CeedEvalMode);
911004e4986SSebastian Grimberg   bool          has_eval_none    = false;
9120d0321e0SJeremy L Thompson 
9130d0321e0SJeremy L Thompson   // CEED_EVAL_NONE
914004e4986SSebastian Grimberg   for (CeedInt i = 0; i < num_eval_modes_in; i++) has_eval_none = has_eval_none || (eval_modes_in[i] == CEED_EVAL_NONE);
915004e4986SSebastian Grimberg   for (CeedInt i = 0; i < num_eval_modes_out; i++) has_eval_none = has_eval_none || (eval_modes_out[i] == CEED_EVAL_NONE);
916004e4986SSebastian Grimberg   if (has_eval_none) {
9170d0321e0SJeremy L Thompson     CeedScalar *identity = NULL;
918ca735530SJeremy L Thompson 
919004e4986SSebastian Grimberg     CeedCallBackend(CeedCalloc(num_nodes * num_qpts, &identity));
920ca735530SJeremy L Thompson     for (CeedInt i = 0; i < (num_nodes < num_qpts ? num_nodes : num_qpts); i++) identity[i * num_nodes + i] = 1.0;
921ca735530SJeremy L Thompson     CeedCallCuda(ceed, cudaMalloc((void **)&diag->d_identity, interp_bytes));
922ca735530SJeremy L Thompson     CeedCallCuda(ceed, cudaMemcpy(diag->d_identity, identity, interp_bytes, cudaMemcpyHostToDevice));
923004e4986SSebastian Grimberg     CeedCallBackend(CeedFree(&identity));
9240d0321e0SJeremy L Thompson   }
9250d0321e0SJeremy L Thompson 
926004e4986SSebastian Grimberg   // CEED_EVAL_INTERP, CEED_EVAL_GRAD, CEED_EVAL_DIV, and CEED_EVAL_CURL
927004e4986SSebastian Grimberg   for (CeedInt in = 0; in < 2; in++) {
928004e4986SSebastian Grimberg     CeedFESpace fespace;
929004e4986SSebastian Grimberg     CeedBasis   basis = in ? basis_in : basis_out;
9300d0321e0SJeremy L Thompson 
931004e4986SSebastian Grimberg     CeedCallBackend(CeedBasisGetFESpace(basis, &fespace));
932004e4986SSebastian Grimberg     switch (fespace) {
933004e4986SSebastian Grimberg       case CEED_FE_SPACE_H1: {
934004e4986SSebastian Grimberg         CeedInt           q_comp_interp, q_comp_grad;
935004e4986SSebastian Grimberg         const CeedScalar *interp, *grad;
936004e4986SSebastian Grimberg         CeedScalar       *d_interp, *d_grad;
9370d0321e0SJeremy L Thompson 
938004e4986SSebastian Grimberg         CeedCallBackend(CeedBasisGetNumQuadratureComponents(basis, CEED_EVAL_INTERP, &q_comp_interp));
939004e4986SSebastian Grimberg         CeedCallBackend(CeedBasisGetNumQuadratureComponents(basis, CEED_EVAL_GRAD, &q_comp_grad));
9400d0321e0SJeremy L Thompson 
941004e4986SSebastian Grimberg         CeedCallBackend(CeedBasisGetInterp(basis, &interp));
942004e4986SSebastian Grimberg         CeedCallCuda(ceed, cudaMalloc((void **)&d_interp, interp_bytes * q_comp_interp));
943004e4986SSebastian Grimberg         CeedCallCuda(ceed, cudaMemcpy(d_interp, interp, interp_bytes * q_comp_interp, cudaMemcpyHostToDevice));
944004e4986SSebastian Grimberg         CeedCallBackend(CeedBasisGetGrad(basis, &grad));
945004e4986SSebastian Grimberg         CeedCallCuda(ceed, cudaMalloc((void **)&d_grad, interp_bytes * q_comp_grad));
946004e4986SSebastian Grimberg         CeedCallCuda(ceed, cudaMemcpy(d_grad, grad, interp_bytes * q_comp_grad, cudaMemcpyHostToDevice));
947004e4986SSebastian Grimberg         if (in) {
948004e4986SSebastian Grimberg           diag->d_interp_in = d_interp;
949004e4986SSebastian Grimberg           diag->d_grad_in   = d_grad;
950004e4986SSebastian Grimberg         } else {
951004e4986SSebastian Grimberg           diag->d_interp_out = d_interp;
952004e4986SSebastian Grimberg           diag->d_grad_out   = d_grad;
953004e4986SSebastian Grimberg         }
954004e4986SSebastian Grimberg       } break;
955004e4986SSebastian Grimberg       case CEED_FE_SPACE_HDIV: {
956004e4986SSebastian Grimberg         CeedInt           q_comp_interp, q_comp_div;
957004e4986SSebastian Grimberg         const CeedScalar *interp, *div;
958004e4986SSebastian Grimberg         CeedScalar       *d_interp, *d_div;
959004e4986SSebastian Grimberg 
960004e4986SSebastian Grimberg         CeedCallBackend(CeedBasisGetNumQuadratureComponents(basis, CEED_EVAL_INTERP, &q_comp_interp));
961004e4986SSebastian Grimberg         CeedCallBackend(CeedBasisGetNumQuadratureComponents(basis, CEED_EVAL_DIV, &q_comp_div));
962004e4986SSebastian Grimberg 
963004e4986SSebastian Grimberg         CeedCallBackend(CeedBasisGetInterp(basis, &interp));
964004e4986SSebastian Grimberg         CeedCallCuda(ceed, cudaMalloc((void **)&d_interp, interp_bytes * q_comp_interp));
965004e4986SSebastian Grimberg         CeedCallCuda(ceed, cudaMemcpy(d_interp, interp, interp_bytes * q_comp_interp, cudaMemcpyHostToDevice));
966004e4986SSebastian Grimberg         CeedCallBackend(CeedBasisGetDiv(basis, &div));
967004e4986SSebastian Grimberg         CeedCallCuda(ceed, cudaMalloc((void **)&d_div, interp_bytes * q_comp_div));
968004e4986SSebastian Grimberg         CeedCallCuda(ceed, cudaMemcpy(d_div, div, interp_bytes * q_comp_div, cudaMemcpyHostToDevice));
969004e4986SSebastian Grimberg         if (in) {
970004e4986SSebastian Grimberg           diag->d_interp_in = d_interp;
971004e4986SSebastian Grimberg           diag->d_div_in    = d_div;
972004e4986SSebastian Grimberg         } else {
973004e4986SSebastian Grimberg           diag->d_interp_out = d_interp;
974004e4986SSebastian Grimberg           diag->d_div_out    = d_div;
975004e4986SSebastian Grimberg         }
976004e4986SSebastian Grimberg       } break;
977004e4986SSebastian Grimberg       case CEED_FE_SPACE_HCURL: {
978004e4986SSebastian Grimberg         CeedInt           q_comp_interp, q_comp_curl;
979004e4986SSebastian Grimberg         const CeedScalar *interp, *curl;
980004e4986SSebastian Grimberg         CeedScalar       *d_interp, *d_curl;
981004e4986SSebastian Grimberg 
982004e4986SSebastian Grimberg         CeedCallBackend(CeedBasisGetNumQuadratureComponents(basis, CEED_EVAL_INTERP, &q_comp_interp));
983004e4986SSebastian Grimberg         CeedCallBackend(CeedBasisGetNumQuadratureComponents(basis, CEED_EVAL_CURL, &q_comp_curl));
984004e4986SSebastian Grimberg 
985004e4986SSebastian Grimberg         CeedCallBackend(CeedBasisGetInterp(basis, &interp));
986004e4986SSebastian Grimberg         CeedCallCuda(ceed, cudaMalloc((void **)&d_interp, interp_bytes * q_comp_interp));
987004e4986SSebastian Grimberg         CeedCallCuda(ceed, cudaMemcpy(d_interp, interp, interp_bytes * q_comp_interp, cudaMemcpyHostToDevice));
988004e4986SSebastian Grimberg         CeedCallBackend(CeedBasisGetCurl(basis, &curl));
989004e4986SSebastian Grimberg         CeedCallCuda(ceed, cudaMalloc((void **)&d_curl, interp_bytes * q_comp_curl));
990004e4986SSebastian Grimberg         CeedCallCuda(ceed, cudaMemcpy(d_curl, curl, interp_bytes * q_comp_curl, cudaMemcpyHostToDevice));
991004e4986SSebastian Grimberg         if (in) {
992004e4986SSebastian Grimberg           diag->d_interp_in = d_interp;
993004e4986SSebastian Grimberg           diag->d_curl_in   = d_curl;
994004e4986SSebastian Grimberg         } else {
995004e4986SSebastian Grimberg           diag->d_interp_out = d_interp;
996004e4986SSebastian Grimberg           diag->d_curl_out   = d_curl;
997004e4986SSebastian Grimberg         }
998004e4986SSebastian Grimberg       } break;
999004e4986SSebastian Grimberg     }
1000004e4986SSebastian Grimberg   }
1001004e4986SSebastian Grimberg 
1002004e4986SSebastian Grimberg   // Arrays of eval_modes
1003004e4986SSebastian Grimberg   CeedCallCuda(ceed, cudaMalloc((void **)&diag->d_eval_modes_in, num_eval_modes_in * eval_modes_bytes));
1004004e4986SSebastian Grimberg   CeedCallCuda(ceed, cudaMemcpy(diag->d_eval_modes_in, eval_modes_in, num_eval_modes_in * eval_modes_bytes, cudaMemcpyHostToDevice));
1005004e4986SSebastian Grimberg   CeedCallCuda(ceed, cudaMalloc((void **)&diag->d_eval_modes_out, num_eval_modes_out * eval_modes_bytes));
1006004e4986SSebastian Grimberg   CeedCallCuda(ceed, cudaMemcpy(diag->d_eval_modes_out, eval_modes_out, num_eval_modes_out * eval_modes_bytes, cudaMemcpyHostToDevice));
1007004e4986SSebastian Grimberg   CeedCallBackend(CeedFree(&eval_modes_in));
1008004e4986SSebastian Grimberg   CeedCallBackend(CeedFree(&eval_modes_out));
10090d0321e0SJeremy L Thompson   return CEED_ERROR_SUCCESS;
10100d0321e0SJeremy L Thompson }
10110d0321e0SJeremy L Thompson 
10120d0321e0SJeremy L Thompson //------------------------------------------------------------------------------
1013cbfe683aSSebastian Grimberg // Assemble Diagonal Setup (Compilation)
1014cbfe683aSSebastian Grimberg //------------------------------------------------------------------------------
1015cbfe683aSSebastian Grimberg static inline int CeedOperatorAssembleDiagonalSetupCompile_Cuda(CeedOperator op, CeedInt use_ceedsize_idx, const bool is_point_block) {
1016cbfe683aSSebastian Grimberg   Ceed                ceed;
101722070f95SJeremy L Thompson   char               *diagonal_kernel_source;
101822070f95SJeremy L Thompson   const char         *diagonal_kernel_path;
1019cbfe683aSSebastian Grimberg   CeedInt             num_input_fields, num_output_fields, num_eval_modes_in = 0, num_eval_modes_out = 0;
1020cbfe683aSSebastian Grimberg   CeedInt             num_comp, q_comp, num_nodes, num_qpts;
1021cbfe683aSSebastian Grimberg   CeedBasis           basis_in = NULL, basis_out = NULL;
1022cbfe683aSSebastian Grimberg   CeedQFunctionField *qf_fields;
1023cbfe683aSSebastian Grimberg   CeedQFunction       qf;
1024cbfe683aSSebastian Grimberg   CeedOperatorField  *op_fields;
1025cbfe683aSSebastian Grimberg   CeedOperator_Cuda  *impl;
1026cbfe683aSSebastian Grimberg 
1027cbfe683aSSebastian Grimberg   CeedCallBackend(CeedOperatorGetCeed(op, &ceed));
1028cbfe683aSSebastian Grimberg   CeedCallBackend(CeedOperatorGetQFunction(op, &qf));
1029cbfe683aSSebastian Grimberg   CeedCallBackend(CeedQFunctionGetNumArgs(qf, &num_input_fields, &num_output_fields));
1030cbfe683aSSebastian Grimberg 
1031cbfe683aSSebastian Grimberg   // Determine active input basis
1032cbfe683aSSebastian Grimberg   CeedCallBackend(CeedOperatorGetFields(op, NULL, &op_fields, NULL, NULL));
1033cbfe683aSSebastian Grimberg   CeedCallBackend(CeedQFunctionGetFields(qf, NULL, &qf_fields, NULL, NULL));
1034cbfe683aSSebastian Grimberg   for (CeedInt i = 0; i < num_input_fields; i++) {
1035cbfe683aSSebastian Grimberg     CeedVector vec;
1036cbfe683aSSebastian Grimberg 
1037cbfe683aSSebastian Grimberg     CeedCallBackend(CeedOperatorFieldGetVector(op_fields[i], &vec));
1038cbfe683aSSebastian Grimberg     if (vec == CEED_VECTOR_ACTIVE) {
1039cbfe683aSSebastian Grimberg       CeedEvalMode eval_mode;
1040cbfe683aSSebastian Grimberg 
1041cbfe683aSSebastian Grimberg       CeedCallBackend(CeedOperatorFieldGetBasis(op_fields[i], &basis_in));
1042cbfe683aSSebastian Grimberg       CeedCallBackend(CeedQFunctionFieldGetEvalMode(qf_fields[i], &eval_mode));
1043cbfe683aSSebastian Grimberg       CeedCallBackend(CeedBasisGetNumQuadratureComponents(basis_in, eval_mode, &q_comp));
1044cbfe683aSSebastian Grimberg       if (eval_mode != CEED_EVAL_WEIGHT) {
1045cbfe683aSSebastian Grimberg         num_eval_modes_in += q_comp;
1046cbfe683aSSebastian Grimberg       }
1047cbfe683aSSebastian Grimberg     }
1048cbfe683aSSebastian Grimberg   }
1049cbfe683aSSebastian Grimberg 
1050cbfe683aSSebastian Grimberg   // Determine active output basis
1051cbfe683aSSebastian Grimberg   CeedCallBackend(CeedOperatorGetFields(op, NULL, NULL, NULL, &op_fields));
1052cbfe683aSSebastian Grimberg   CeedCallBackend(CeedQFunctionGetFields(qf, NULL, NULL, NULL, &qf_fields));
1053cbfe683aSSebastian Grimberg   for (CeedInt i = 0; i < num_output_fields; i++) {
1054cbfe683aSSebastian Grimberg     CeedVector vec;
1055cbfe683aSSebastian Grimberg 
1056cbfe683aSSebastian Grimberg     CeedCallBackend(CeedOperatorFieldGetVector(op_fields[i], &vec));
1057cbfe683aSSebastian Grimberg     if (vec == CEED_VECTOR_ACTIVE) {
1058cbfe683aSSebastian Grimberg       CeedEvalMode eval_mode;
1059cbfe683aSSebastian Grimberg 
1060cbfe683aSSebastian Grimberg       CeedCallBackend(CeedOperatorFieldGetBasis(op_fields[i], &basis_out));
1061cbfe683aSSebastian Grimberg       CeedCallBackend(CeedQFunctionFieldGetEvalMode(qf_fields[i], &eval_mode));
1062cbfe683aSSebastian Grimberg       CeedCallBackend(CeedBasisGetNumQuadratureComponents(basis_out, eval_mode, &q_comp));
1063cbfe683aSSebastian Grimberg       if (eval_mode != CEED_EVAL_WEIGHT) {
1064cbfe683aSSebastian Grimberg         num_eval_modes_out += q_comp;
1065cbfe683aSSebastian Grimberg       }
1066cbfe683aSSebastian Grimberg     }
1067cbfe683aSSebastian Grimberg   }
1068cbfe683aSSebastian Grimberg 
1069cbfe683aSSebastian Grimberg   // Operator data struct
1070cbfe683aSSebastian Grimberg   CeedCallBackend(CeedOperatorGetData(op, &impl));
1071cbfe683aSSebastian Grimberg   CeedOperatorDiag_Cuda *diag = impl->diag;
1072cbfe683aSSebastian Grimberg 
1073cbfe683aSSebastian Grimberg   // Assemble kernel
1074cbfe683aSSebastian Grimberg   CUmodule *module          = is_point_block ? &diag->module_point_block : &diag->module;
1075cbfe683aSSebastian Grimberg   CeedInt   elems_per_block = 1;
1076cbfe683aSSebastian Grimberg   CeedCallBackend(CeedBasisGetNumNodes(basis_in, &num_nodes));
1077cbfe683aSSebastian Grimberg   CeedCallBackend(CeedBasisGetNumComponents(basis_in, &num_comp));
1078cbfe683aSSebastian Grimberg   if (basis_in == CEED_BASIS_NONE) num_qpts = num_nodes;
1079cbfe683aSSebastian Grimberg   else CeedCallBackend(CeedBasisGetNumQuadraturePoints(basis_in, &num_qpts));
1080cbfe683aSSebastian Grimberg   CeedCallBackend(CeedGetJitAbsolutePath(ceed, "ceed/jit-source/cuda/cuda-ref-operator-assemble-diagonal.h", &diagonal_kernel_path));
1081cbfe683aSSebastian Grimberg   CeedDebug256(ceed, CEED_DEBUG_COLOR_SUCCESS, "----- Loading Diagonal Assembly Kernel Source -----\n");
1082cbfe683aSSebastian Grimberg   CeedCallBackend(CeedLoadSourceToBuffer(ceed, diagonal_kernel_path, &diagonal_kernel_source));
1083cbfe683aSSebastian Grimberg   CeedDebug256(ceed, CEED_DEBUG_COLOR_SUCCESS, "----- Loading Diagonal Assembly Source Complete! -----\n");
1084cbfe683aSSebastian Grimberg   CeedCallCuda(ceed, CeedCompile_Cuda(ceed, diagonal_kernel_source, module, 8, "NUM_EVAL_MODES_IN", num_eval_modes_in, "NUM_EVAL_MODES_OUT",
1085cbfe683aSSebastian Grimberg                                       num_eval_modes_out, "NUM_COMP", num_comp, "NUM_NODES", num_nodes, "NUM_QPTS", num_qpts, "USE_CEEDSIZE",
1086cbfe683aSSebastian Grimberg                                       use_ceedsize_idx, "USE_POINT_BLOCK", is_point_block ? 1 : 0, "BLOCK_SIZE", num_nodes * elems_per_block));
1087cbfe683aSSebastian Grimberg   CeedCallCuda(ceed, CeedGetKernel_Cuda(ceed, *module, "LinearDiagonal", is_point_block ? &diag->LinearPointBlock : &diag->LinearDiagonal));
1088cbfe683aSSebastian Grimberg   CeedCallBackend(CeedFree(&diagonal_kernel_path));
1089cbfe683aSSebastian Grimberg   CeedCallBackend(CeedFree(&diagonal_kernel_source));
1090cbfe683aSSebastian Grimberg   return CEED_ERROR_SUCCESS;
1091cbfe683aSSebastian Grimberg }
1092cbfe683aSSebastian Grimberg 
1093cbfe683aSSebastian Grimberg //------------------------------------------------------------------------------
1094004e4986SSebastian Grimberg // Assemble Diagonal Core
10950d0321e0SJeremy L Thompson //------------------------------------------------------------------------------
1096ca735530SJeremy L Thompson static inline int CeedOperatorAssembleDiagonalCore_Cuda(CeedOperator op, CeedVector assembled, CeedRequest *request, const bool is_point_block) {
10970d0321e0SJeremy L Thompson   Ceed                ceed;
1098cbfe683aSSebastian Grimberg   CeedInt             num_elem, num_nodes;
1099ca735530SJeremy L Thompson   CeedScalar         *elem_diag_array;
1100ca735530SJeremy L Thompson   const CeedScalar   *assembled_qf_array;
1101004e4986SSebastian Grimberg   CeedVector          assembled_qf   = NULL, elem_diag;
1102004e4986SSebastian Grimberg   CeedElemRestriction assembled_rstr = NULL, rstr_in, rstr_out, diag_rstr;
11030d0321e0SJeremy L Thompson   CeedOperator_Cuda  *impl;
1104ca735530SJeremy L Thompson 
1105ca735530SJeremy L Thompson   CeedCallBackend(CeedOperatorGetCeed(op, &ceed));
11062b730f8bSJeremy L Thompson   CeedCallBackend(CeedOperatorGetData(op, &impl));
11070d0321e0SJeremy L Thompson 
11080d0321e0SJeremy L Thompson   // Assemble QFunction
1109004e4986SSebastian Grimberg   CeedCallBackend(CeedOperatorLinearAssembleQFunctionBuildOrUpdate(op, &assembled_qf, &assembled_rstr, request));
1110004e4986SSebastian Grimberg   CeedCallBackend(CeedElemRestrictionDestroy(&assembled_rstr));
1111004e4986SSebastian Grimberg   CeedCallBackend(CeedVectorGetArrayRead(assembled_qf, CEED_MEM_DEVICE, &assembled_qf_array));
11120d0321e0SJeremy L Thompson 
1113cbfe683aSSebastian Grimberg   // Setup
1114cbfe683aSSebastian Grimberg   if (!impl->diag) CeedCallBackend(CeedOperatorAssembleDiagonalSetup_Cuda(op));
1115cbfe683aSSebastian Grimberg   CeedOperatorDiag_Cuda *diag = impl->diag;
1116cbfe683aSSebastian Grimberg 
1117cbfe683aSSebastian Grimberg   assert(diag != NULL);
1118cbfe683aSSebastian Grimberg 
1119cbfe683aSSebastian Grimberg   // Assemble kernel if needed
1120cbfe683aSSebastian Grimberg   if ((!is_point_block && !diag->LinearDiagonal) || (is_point_block && !diag->LinearPointBlock)) {
1121cbfe683aSSebastian Grimberg     CeedSize assembled_length, assembled_qf_length;
1122cbfe683aSSebastian Grimberg     CeedInt  use_ceedsize_idx = 0;
1123f7c1b517Snbeams     CeedCallBackend(CeedVectorGetLength(assembled, &assembled_length));
1124ca735530SJeremy L Thompson     CeedCallBackend(CeedVectorGetLength(assembled_qf, &assembled_qf_length));
1125ca735530SJeremy L Thompson     if ((assembled_length > INT_MAX) || (assembled_qf_length > INT_MAX)) use_ceedsize_idx = 1;
1126f7c1b517Snbeams 
1127cbfe683aSSebastian Grimberg     CeedCallBackend(CeedOperatorAssembleDiagonalSetupCompile_Cuda(op, use_ceedsize_idx, is_point_block));
1128cbfe683aSSebastian Grimberg   }
11290d0321e0SJeremy L Thompson 
1130004e4986SSebastian Grimberg   // Restriction and diagonal vector
1131004e4986SSebastian Grimberg   CeedCallBackend(CeedOperatorGetActiveElemRestrictions(op, &rstr_in, &rstr_out));
1132004e4986SSebastian Grimberg   CeedCheck(rstr_in == rstr_out, ceed, CEED_ERROR_BACKEND,
1133004e4986SSebastian Grimberg             "Cannot assemble operator diagonal with different input and output active element restrictions");
1134004e4986SSebastian Grimberg   if (!is_point_block && !diag->diag_rstr) {
1135004e4986SSebastian Grimberg     CeedCallBackend(CeedElemRestrictionCreateUnsignedCopy(rstr_out, &diag->diag_rstr));
1136004e4986SSebastian Grimberg     CeedCallBackend(CeedElemRestrictionCreateVector(diag->diag_rstr, NULL, &diag->elem_diag));
1137004e4986SSebastian Grimberg   } else if (is_point_block && !diag->point_block_diag_rstr) {
1138004e4986SSebastian Grimberg     CeedCallBackend(CeedOperatorCreateActivePointBlockRestriction(rstr_out, &diag->point_block_diag_rstr));
1139004e4986SSebastian Grimberg     CeedCallBackend(CeedElemRestrictionCreateVector(diag->point_block_diag_rstr, NULL, &diag->point_block_elem_diag));
11400d0321e0SJeremy L Thompson   }
1141004e4986SSebastian Grimberg   diag_rstr = is_point_block ? diag->point_block_diag_rstr : diag->diag_rstr;
1142004e4986SSebastian Grimberg   elem_diag = is_point_block ? diag->point_block_elem_diag : diag->elem_diag;
1143ca735530SJeremy L Thompson   CeedCallBackend(CeedVectorSetValue(elem_diag, 0.0));
11440d0321e0SJeremy L Thompson 
114591db28b6SZach Atkins   // Only assemble diagonal if the basis has nodes, otherwise inputs are null pointers
1146004e4986SSebastian Grimberg   CeedCallBackend(CeedElemRestrictionGetElementSize(diag_rstr, &num_nodes));
1147004e4986SSebastian Grimberg   if (num_nodes > 0) {
11480d0321e0SJeremy L Thompson     // Assemble element operator diagonals
1149ca735530SJeremy L Thompson     CeedCallBackend(CeedElemRestrictionGetNumElements(diag_rstr, &num_elem));
1150004e4986SSebastian Grimberg     CeedCallBackend(CeedVectorGetArray(elem_diag, CEED_MEM_DEVICE, &elem_diag_array));
11510d0321e0SJeremy L Thompson 
11520d0321e0SJeremy L Thompson     // Compute the diagonal of B^T D B
1153004e4986SSebastian Grimberg     CeedInt elems_per_block = 1;
1154004e4986SSebastian Grimberg     CeedInt grid            = CeedDivUpInt(num_elem, elems_per_block);
1155004e4986SSebastian Grimberg     void   *args[]          = {(void *)&num_elem,      &diag->d_identity,       &diag->d_interp_in,  &diag->d_grad_in, &diag->d_div_in,
1156004e4986SSebastian Grimberg                                &diag->d_curl_in,       &diag->d_interp_out,     &diag->d_grad_out,   &diag->d_div_out, &diag->d_curl_out,
1157004e4986SSebastian Grimberg                                &diag->d_eval_modes_in, &diag->d_eval_modes_out, &assembled_qf_array, &elem_diag_array};
1158004e4986SSebastian Grimberg 
1159ca735530SJeremy L Thompson     if (is_point_block) {
1160004e4986SSebastian Grimberg       CeedCallBackend(CeedRunKernelDim_Cuda(ceed, diag->LinearPointBlock, grid, num_nodes, 1, elems_per_block, args));
11610d0321e0SJeremy L Thompson     } else {
1162004e4986SSebastian Grimberg       CeedCallBackend(CeedRunKernelDim_Cuda(ceed, diag->LinearDiagonal, grid, num_nodes, 1, elems_per_block, args));
11630d0321e0SJeremy L Thompson     }
11640d0321e0SJeremy L Thompson 
11650d0321e0SJeremy L Thompson     // Restore arrays
1166ca735530SJeremy L Thompson     CeedCallBackend(CeedVectorRestoreArray(elem_diag, &elem_diag_array));
1167ca735530SJeremy L Thompson     CeedCallBackend(CeedVectorRestoreArrayRead(assembled_qf, &assembled_qf_array));
116891db28b6SZach Atkins   }
11690d0321e0SJeremy L Thompson 
11700d0321e0SJeremy L Thompson   // Assemble local operator diagonal
1171ca735530SJeremy L Thompson   CeedCallBackend(CeedElemRestrictionApply(diag_rstr, CEED_TRANSPOSE, elem_diag, assembled, request));
11720d0321e0SJeremy L Thompson 
11730d0321e0SJeremy L Thompson   // Cleanup
1174ca735530SJeremy L Thompson   CeedCallBackend(CeedVectorDestroy(&assembled_qf));
11750d0321e0SJeremy L Thompson   return CEED_ERROR_SUCCESS;
11760d0321e0SJeremy L Thompson }
11770d0321e0SJeremy L Thompson 
11780d0321e0SJeremy L Thompson //------------------------------------------------------------------------------
11790d0321e0SJeremy L Thompson // Assemble Linear Diagonal
11800d0321e0SJeremy L Thompson //------------------------------------------------------------------------------
11812b730f8bSJeremy L Thompson static int CeedOperatorLinearAssembleAddDiagonal_Cuda(CeedOperator op, CeedVector assembled, CeedRequest *request) {
11822b730f8bSJeremy L Thompson   CeedCallBackend(CeedOperatorAssembleDiagonalCore_Cuda(op, assembled, request, false));
11836aa95790SJeremy L Thompson   return CEED_ERROR_SUCCESS;
11840d0321e0SJeremy L Thompson }
11850d0321e0SJeremy L Thompson 
11860d0321e0SJeremy L Thompson //------------------------------------------------------------------------------
11870d0321e0SJeremy L Thompson // Assemble Linear Point Block Diagonal
11880d0321e0SJeremy L Thompson //------------------------------------------------------------------------------
11892b730f8bSJeremy L Thompson static int CeedOperatorLinearAssembleAddPointBlockDiagonal_Cuda(CeedOperator op, CeedVector assembled, CeedRequest *request) {
11902b730f8bSJeremy L Thompson   CeedCallBackend(CeedOperatorAssembleDiagonalCore_Cuda(op, assembled, request, true));
11916aa95790SJeremy L Thompson   return CEED_ERROR_SUCCESS;
11920d0321e0SJeremy L Thompson }
11930d0321e0SJeremy L Thompson 
11940d0321e0SJeremy L Thompson //------------------------------------------------------------------------------
1195004e4986SSebastian Grimberg // Single Operator Assembly Setup
1196cc132f9aSnbeams //------------------------------------------------------------------------------
1197f7c1b517Snbeams static int CeedSingleOperatorAssembleSetup_Cuda(CeedOperator op, CeedInt use_ceedsize_idx) {
1198cc132f9aSnbeams   Ceed                ceed;
1199004e4986SSebastian Grimberg   Ceed_Cuda          *cuda_data;
120022070f95SJeremy L Thompson   char               *assembly_kernel_source;
120122070f95SJeremy L Thompson   const char         *assembly_kernel_path;
1202004e4986SSebastian Grimberg   CeedInt             num_input_fields, num_output_fields, num_eval_modes_in = 0, num_eval_modes_out = 0;
12033b38d1dfSJeremy L Thompson   CeedInt             elem_size_in, num_qpts_in = 0, num_comp_in, elem_size_out, num_qpts_out, num_comp_out, q_comp;
1204004e4986SSebastian Grimberg   CeedEvalMode       *eval_modes_in = NULL, *eval_modes_out = NULL;
1205ca735530SJeremy L Thompson   CeedElemRestriction rstr_in = NULL, rstr_out = NULL;
1206ca735530SJeremy L Thompson   CeedBasis           basis_in = NULL, basis_out = NULL;
1207ca735530SJeremy L Thompson   CeedQFunctionField *qf_fields;
1208ca735530SJeremy L Thompson   CeedQFunction       qf;
1209ca735530SJeremy L Thompson   CeedOperatorField  *input_fields, *output_fields;
1210cc132f9aSnbeams   CeedOperator_Cuda  *impl;
1211ca735530SJeremy L Thompson 
1212ca735530SJeremy L Thompson   CeedCallBackend(CeedOperatorGetCeed(op, &ceed));
12132b730f8bSJeremy L Thompson   CeedCallBackend(CeedOperatorGetData(op, &impl));
1214cc132f9aSnbeams 
1215cc132f9aSnbeams   // Get intput and output fields
12162b730f8bSJeremy L Thompson   CeedCallBackend(CeedOperatorGetFields(op, &num_input_fields, &input_fields, &num_output_fields, &output_fields));
1217cc132f9aSnbeams 
1218cc132f9aSnbeams   // Determine active input basis eval mode
12192b730f8bSJeremy L Thompson   CeedCallBackend(CeedOperatorGetQFunction(op, &qf));
12202b730f8bSJeremy L Thompson   CeedCallBackend(CeedQFunctionGetFields(qf, NULL, &qf_fields, NULL, NULL));
1221cc132f9aSnbeams   for (CeedInt i = 0; i < num_input_fields; i++) {
1222cc132f9aSnbeams     CeedVector vec;
1223ca735530SJeremy L Thompson 
12242b730f8bSJeremy L Thompson     CeedCallBackend(CeedOperatorFieldGetVector(input_fields[i], &vec));
1225cc132f9aSnbeams     if (vec == CEED_VECTOR_ACTIVE) {
1226004e4986SSebastian Grimberg       CeedBasis    basis;
1227ca735530SJeremy L Thompson       CeedEvalMode eval_mode;
1228ca735530SJeremy L Thompson 
1229004e4986SSebastian Grimberg       CeedCallBackend(CeedOperatorFieldGetBasis(input_fields[i], &basis));
1230004e4986SSebastian Grimberg       CeedCheck(!basis_in || basis_in == basis, ceed, CEED_ERROR_BACKEND, "Backend does not implement operator assembly with multiple active bases");
1231004e4986SSebastian Grimberg       basis_in = basis;
12322b730f8bSJeremy L Thompson       CeedCallBackend(CeedOperatorFieldGetElemRestriction(input_fields[i], &rstr_in));
1233004e4986SSebastian Grimberg       CeedCallBackend(CeedElemRestrictionGetElementSize(rstr_in, &elem_size_in));
1234004e4986SSebastian Grimberg       if (basis_in == CEED_BASIS_NONE) num_qpts_in = elem_size_in;
1235004e4986SSebastian Grimberg       else CeedCallBackend(CeedBasisGetNumQuadraturePoints(basis_in, &num_qpts_in));
12362b730f8bSJeremy L Thompson       CeedCallBackend(CeedQFunctionFieldGetEvalMode(qf_fields[i], &eval_mode));
1237004e4986SSebastian Grimberg       CeedCallBackend(CeedBasisGetNumQuadratureComponents(basis_in, eval_mode, &q_comp));
1238004e4986SSebastian Grimberg       if (eval_mode != CEED_EVAL_WEIGHT) {
1239004e4986SSebastian Grimberg         // q_comp = 1 if CEED_EVAL_NONE, CEED_EVAL_WEIGHT caught by QF Assembly
1240004e4986SSebastian Grimberg         CeedCallBackend(CeedRealloc(num_eval_modes_in + q_comp, &eval_modes_in));
1241004e4986SSebastian Grimberg         for (CeedInt d = 0; d < q_comp; d++) {
1242004e4986SSebastian Grimberg           eval_modes_in[num_eval_modes_in + d] = eval_mode;
1243cc132f9aSnbeams         }
1244004e4986SSebastian Grimberg         num_eval_modes_in += q_comp;
1245cc132f9aSnbeams       }
1246cc132f9aSnbeams     }
1247cc132f9aSnbeams   }
1248cc132f9aSnbeams 
1249cc132f9aSnbeams   // Determine active output basis; basis_out and rstr_out only used if same as input, TODO
12502b730f8bSJeremy L Thompson   CeedCallBackend(CeedQFunctionGetFields(qf, NULL, NULL, NULL, &qf_fields));
1251cc132f9aSnbeams   for (CeedInt i = 0; i < num_output_fields; i++) {
1252cc132f9aSnbeams     CeedVector vec;
1253ca735530SJeremy L Thompson 
12542b730f8bSJeremy L Thompson     CeedCallBackend(CeedOperatorFieldGetVector(output_fields[i], &vec));
1255cc132f9aSnbeams     if (vec == CEED_VECTOR_ACTIVE) {
1256004e4986SSebastian Grimberg       CeedBasis    basis;
1257ca735530SJeremy L Thompson       CeedEvalMode eval_mode;
1258ca735530SJeremy L Thompson 
1259004e4986SSebastian Grimberg       CeedCallBackend(CeedOperatorFieldGetBasis(output_fields[i], &basis));
1260004e4986SSebastian Grimberg       CeedCheck(!basis_out || basis_out == basis, ceed, CEED_ERROR_BACKEND,
1261004e4986SSebastian Grimberg                 "Backend does not implement operator assembly with multiple active bases");
1262004e4986SSebastian Grimberg       basis_out = basis;
12632b730f8bSJeremy L Thompson       CeedCallBackend(CeedOperatorFieldGetElemRestriction(output_fields[i], &rstr_out));
1264004e4986SSebastian Grimberg       CeedCallBackend(CeedElemRestrictionGetElementSize(rstr_out, &elem_size_out));
1265004e4986SSebastian Grimberg       if (basis_out == CEED_BASIS_NONE) num_qpts_out = elem_size_out;
1266004e4986SSebastian Grimberg       else CeedCallBackend(CeedBasisGetNumQuadraturePoints(basis_out, &num_qpts_out));
1267004e4986SSebastian Grimberg       CeedCheck(num_qpts_in == num_qpts_out, ceed, CEED_ERROR_UNSUPPORTED,
1268004e4986SSebastian Grimberg                 "Active input and output bases must have the same number of quadrature points");
12692b730f8bSJeremy L Thompson       CeedCallBackend(CeedQFunctionFieldGetEvalMode(qf_fields[i], &eval_mode));
1270004e4986SSebastian Grimberg       CeedCallBackend(CeedBasisGetNumQuadratureComponents(basis_out, eval_mode, &q_comp));
1271004e4986SSebastian Grimberg       if (eval_mode != CEED_EVAL_WEIGHT) {
1272004e4986SSebastian Grimberg         // q_comp = 1 if CEED_EVAL_NONE, CEED_EVAL_WEIGHT caught by QF Assembly
1273004e4986SSebastian Grimberg         CeedCallBackend(CeedRealloc(num_eval_modes_out + q_comp, &eval_modes_out));
1274004e4986SSebastian Grimberg         for (CeedInt d = 0; d < q_comp; d++) {
1275004e4986SSebastian Grimberg           eval_modes_out[num_eval_modes_out + d] = eval_mode;
1276004e4986SSebastian Grimberg         }
1277004e4986SSebastian Grimberg         num_eval_modes_out += q_comp;
1278cc132f9aSnbeams       }
1279cc132f9aSnbeams     }
1280cc132f9aSnbeams   }
1281004e4986SSebastian Grimberg   CeedCheck(num_eval_modes_in > 0 && num_eval_modes_out > 0, ceed, CEED_ERROR_UNSUPPORTED, "Cannot assemble operator without inputs/outputs");
1282cc132f9aSnbeams 
12832b730f8bSJeremy L Thompson   CeedCallBackend(CeedCalloc(1, &impl->asmb));
1284cc132f9aSnbeams   CeedOperatorAssemble_Cuda *asmb = impl->asmb;
1285004e4986SSebastian Grimberg   asmb->elems_per_block           = 1;
1286004e4986SSebastian Grimberg   asmb->block_size_x              = elem_size_in;
1287004e4986SSebastian Grimberg   asmb->block_size_y              = elem_size_out;
1288ca735530SJeremy L Thompson 
12892b730f8bSJeremy L Thompson   CeedCallBackend(CeedGetData(ceed, &cuda_data));
1290004e4986SSebastian Grimberg   bool fallback = asmb->block_size_x * asmb->block_size_y * asmb->elems_per_block > cuda_data->device_prop.maxThreadsPerBlock;
1291004e4986SSebastian Grimberg 
1292004e4986SSebastian Grimberg   if (fallback) {
1293004e4986SSebastian Grimberg     // Use fallback kernel with 1D threadblock
1294004e4986SSebastian Grimberg     asmb->block_size_y = 1;
1295004e4986SSebastian Grimberg   }
1296004e4986SSebastian Grimberg 
1297004e4986SSebastian Grimberg   // Compile kernels
1298004e4986SSebastian Grimberg   CeedCallBackend(CeedElemRestrictionGetNumComponents(rstr_in, &num_comp_in));
1299004e4986SSebastian Grimberg   CeedCallBackend(CeedElemRestrictionGetNumComponents(rstr_out, &num_comp_out));
13002b730f8bSJeremy L Thompson   CeedCallBackend(CeedGetJitAbsolutePath(ceed, "ceed/jit-source/cuda/cuda-ref-operator-assemble.h", &assembly_kernel_path));
130123d4529eSJeremy L Thompson   CeedDebug256(ceed, CEED_DEBUG_COLOR_SUCCESS, "----- Loading Assembly Kernel Source -----\n");
13022b730f8bSJeremy L Thompson   CeedCallBackend(CeedLoadSourceToBuffer(ceed, assembly_kernel_path, &assembly_kernel_source));
130323d4529eSJeremy L Thompson   CeedDebug256(ceed, CEED_DEBUG_COLOR_SUCCESS, "----- Loading Assembly Source Complete! -----\n");
1304004e4986SSebastian Grimberg   CeedCallBackend(CeedCompile_Cuda(ceed, assembly_kernel_source, &asmb->module, 10, "NUM_EVAL_MODES_IN", num_eval_modes_in, "NUM_EVAL_MODES_OUT",
1305004e4986SSebastian Grimberg                                    num_eval_modes_out, "NUM_COMP_IN", num_comp_in, "NUM_COMP_OUT", num_comp_out, "NUM_NODES_IN", elem_size_in,
1306004e4986SSebastian Grimberg                                    "NUM_NODES_OUT", elem_size_out, "NUM_QPTS", num_qpts_in, "BLOCK_SIZE",
1307cbfe683aSSebastian Grimberg                                    asmb->block_size_x * asmb->block_size_y * asmb->elems_per_block, "BLOCK_SIZE_Y", asmb->block_size_y,
1308cbfe683aSSebastian Grimberg                                    "USE_CEEDSIZE", use_ceedsize_idx));
1309004e4986SSebastian Grimberg   CeedCallBackend(CeedGetKernel_Cuda(ceed, asmb->module, "LinearAssemble", &asmb->LinearAssemble));
13102b730f8bSJeremy L Thompson   CeedCallBackend(CeedFree(&assembly_kernel_path));
13112b730f8bSJeremy L Thompson   CeedCallBackend(CeedFree(&assembly_kernel_source));
1312cc132f9aSnbeams 
1313004e4986SSebastian Grimberg   // Load into B_in, in order that they will be used in eval_modes_in
1314004e4986SSebastian Grimberg   {
1315004e4986SSebastian Grimberg     const CeedInt in_bytes           = elem_size_in * num_qpts_in * num_eval_modes_in * sizeof(CeedScalar);
1316004e4986SSebastian Grimberg     CeedInt       d_in               = 0;
1317004e4986SSebastian Grimberg     CeedEvalMode  eval_modes_in_prev = CEED_EVAL_NONE;
1318004e4986SSebastian Grimberg     bool          has_eval_none      = false;
1319004e4986SSebastian Grimberg     CeedScalar   *identity           = NULL;
1320ca735530SJeremy L Thompson 
1321004e4986SSebastian Grimberg     for (CeedInt i = 0; i < num_eval_modes_in; i++) {
1322004e4986SSebastian Grimberg       has_eval_none = has_eval_none || (eval_modes_in[i] == CEED_EVAL_NONE);
1323004e4986SSebastian Grimberg     }
1324004e4986SSebastian Grimberg     if (has_eval_none) {
1325004e4986SSebastian Grimberg       CeedCallBackend(CeedCalloc(elem_size_in * num_qpts_in, &identity));
1326004e4986SSebastian Grimberg       for (CeedInt i = 0; i < (elem_size_in < num_qpts_in ? elem_size_in : num_qpts_in); i++) identity[i * elem_size_in + i] = 1.0;
1327004e4986SSebastian Grimberg     }
1328cc132f9aSnbeams 
1329004e4986SSebastian Grimberg     CeedCallCuda(ceed, cudaMalloc((void **)&asmb->d_B_in, in_bytes));
1330004e4986SSebastian Grimberg     for (CeedInt i = 0; i < num_eval_modes_in; i++) {
1331004e4986SSebastian Grimberg       const CeedScalar *h_B_in;
1332ca735530SJeremy L Thompson 
1333004e4986SSebastian Grimberg       CeedCallBackend(CeedOperatorGetBasisPointer(basis_in, eval_modes_in[i], identity, &h_B_in));
1334004e4986SSebastian Grimberg       CeedCallBackend(CeedBasisGetNumQuadratureComponents(basis_in, eval_modes_in[i], &q_comp));
1335004e4986SSebastian Grimberg       if (q_comp > 1) {
1336004e4986SSebastian Grimberg         if (i == 0 || eval_modes_in[i] != eval_modes_in_prev) d_in = 0;
1337004e4986SSebastian Grimberg         else h_B_in = &h_B_in[(++d_in) * elem_size_in * num_qpts_in];
1338004e4986SSebastian Grimberg       }
1339004e4986SSebastian Grimberg       eval_modes_in_prev = eval_modes_in[i];
1340ca735530SJeremy L Thompson 
1341004e4986SSebastian Grimberg       CeedCallCuda(ceed, cudaMemcpy(&asmb->d_B_in[i * elem_size_in * num_qpts_in], h_B_in, elem_size_in * num_qpts_in * sizeof(CeedScalar),
1342004e4986SSebastian Grimberg                                     cudaMemcpyHostToDevice));
1343004e4986SSebastian Grimberg     }
1344004e4986SSebastian Grimberg 
1345004e4986SSebastian Grimberg     if (identity) {
1346004e4986SSebastian Grimberg       CeedCallBackend(CeedFree(&identity));
1347cc132f9aSnbeams     }
1348cc132f9aSnbeams   }
1349cc132f9aSnbeams 
1350004e4986SSebastian Grimberg   // Load into B_out, in order that they will be used in eval_modes_out
1351004e4986SSebastian Grimberg   {
1352004e4986SSebastian Grimberg     const CeedInt out_bytes           = elem_size_out * num_qpts_out * num_eval_modes_out * sizeof(CeedScalar);
1353004e4986SSebastian Grimberg     CeedInt       d_out               = 0;
1354004e4986SSebastian Grimberg     CeedEvalMode  eval_modes_out_prev = CEED_EVAL_NONE;
1355004e4986SSebastian Grimberg     bool          has_eval_none       = false;
1356004e4986SSebastian Grimberg     CeedScalar   *identity            = NULL;
1357ca735530SJeremy L Thompson 
1358004e4986SSebastian Grimberg     for (CeedInt i = 0; i < num_eval_modes_out; i++) {
1359004e4986SSebastian Grimberg       has_eval_none = has_eval_none || (eval_modes_out[i] == CEED_EVAL_NONE);
1360004e4986SSebastian Grimberg     }
1361004e4986SSebastian Grimberg     if (has_eval_none) {
1362004e4986SSebastian Grimberg       CeedCallBackend(CeedCalloc(elem_size_out * num_qpts_out, &identity));
1363004e4986SSebastian Grimberg       for (CeedInt i = 0; i < (elem_size_out < num_qpts_out ? elem_size_out : num_qpts_out); i++) identity[i * elem_size_out + i] = 1.0;
1364cc132f9aSnbeams     }
1365cc132f9aSnbeams 
1366004e4986SSebastian Grimberg     CeedCallCuda(ceed, cudaMalloc((void **)&asmb->d_B_out, out_bytes));
1367004e4986SSebastian Grimberg     for (CeedInt i = 0; i < num_eval_modes_out; i++) {
1368004e4986SSebastian Grimberg       const CeedScalar *h_B_out;
1369ca735530SJeremy L Thompson 
1370004e4986SSebastian Grimberg       CeedCallBackend(CeedOperatorGetBasisPointer(basis_out, eval_modes_out[i], identity, &h_B_out));
1371004e4986SSebastian Grimberg       CeedCallBackend(CeedBasisGetNumQuadratureComponents(basis_out, eval_modes_out[i], &q_comp));
1372004e4986SSebastian Grimberg       if (q_comp > 1) {
1373004e4986SSebastian Grimberg         if (i == 0 || eval_modes_out[i] != eval_modes_out_prev) d_out = 0;
1374004e4986SSebastian Grimberg         else h_B_out = &h_B_out[(++d_out) * elem_size_out * num_qpts_out];
1375004e4986SSebastian Grimberg       }
1376004e4986SSebastian Grimberg       eval_modes_out_prev = eval_modes_out[i];
1377ca735530SJeremy L Thompson 
1378004e4986SSebastian Grimberg       CeedCallCuda(ceed, cudaMemcpy(&asmb->d_B_out[i * elem_size_out * num_qpts_out], h_B_out, elem_size_out * num_qpts_out * sizeof(CeedScalar),
1379004e4986SSebastian Grimberg                                     cudaMemcpyHostToDevice));
1380004e4986SSebastian Grimberg     }
1381004e4986SSebastian Grimberg 
1382004e4986SSebastian Grimberg     if (identity) {
1383004e4986SSebastian Grimberg       CeedCallBackend(CeedFree(&identity));
1384cc132f9aSnbeams     }
1385cc132f9aSnbeams   }
1386cc132f9aSnbeams   return CEED_ERROR_SUCCESS;
1387cc132f9aSnbeams }
1388cc132f9aSnbeams 
1389cc132f9aSnbeams //------------------------------------------------------------------------------
1390cefa2673SJeremy L Thompson // Assemble matrix data for COO matrix of assembled operator.
1391cefa2673SJeremy L Thompson // The sparsity pattern is set by CeedOperatorLinearAssembleSymbolic.
1392cefa2673SJeremy L Thompson //
1393ea61e9acSJeremy L Thompson // Note that this (and other assembly routines) currently assume only one active input restriction/basis per operator (could have multiple basis eval
1394cefa2673SJeremy L Thompson // modes).
1395cefa2673SJeremy L Thompson // TODO: allow multiple active input restrictions/basis objects
1396cc132f9aSnbeams //------------------------------------------------------------------------------
13972b730f8bSJeremy L Thompson static int CeedSingleOperatorAssemble_Cuda(CeedOperator op, CeedInt offset, CeedVector values) {
1398cc132f9aSnbeams   Ceed                ceed;
1399ca735530SJeremy L Thompson   CeedSize            values_length = 0, assembled_qf_length = 0;
1400004e4986SSebastian Grimberg   CeedInt             use_ceedsize_idx = 0, num_elem_in, num_elem_out, elem_size_in, elem_size_out;
1401ca735530SJeremy L Thompson   CeedScalar         *values_array;
1402004e4986SSebastian Grimberg   const CeedScalar   *assembled_qf_array;
1403ca735530SJeremy L Thompson   CeedVector          assembled_qf   = NULL;
1404004e4986SSebastian Grimberg   CeedElemRestriction assembled_rstr = NULL, rstr_in, rstr_out;
1405004e4986SSebastian Grimberg   CeedRestrictionType rstr_type_in, rstr_type_out;
1406004e4986SSebastian Grimberg   const bool         *orients_in = NULL, *orients_out = NULL;
1407004e4986SSebastian Grimberg   const CeedInt8     *curl_orients_in = NULL, *curl_orients_out = NULL;
1408cc132f9aSnbeams   CeedOperator_Cuda  *impl;
1409ca735530SJeremy L Thompson 
1410ca735530SJeremy L Thompson   CeedCallBackend(CeedOperatorGetCeed(op, &ceed));
14112b730f8bSJeremy L Thompson   CeedCallBackend(CeedOperatorGetData(op, &impl));
1412cc132f9aSnbeams 
1413cc132f9aSnbeams   // Assemble QFunction
1414004e4986SSebastian Grimberg   CeedCallBackend(CeedOperatorLinearAssembleQFunctionBuildOrUpdate(op, &assembled_qf, &assembled_rstr, CEED_REQUEST_IMMEDIATE));
1415004e4986SSebastian Grimberg   CeedCallBackend(CeedElemRestrictionDestroy(&assembled_rstr));
1416004e4986SSebastian Grimberg   CeedCallBackend(CeedVectorGetArrayRead(assembled_qf, CEED_MEM_DEVICE, &assembled_qf_array));
1417cc132f9aSnbeams 
1418f7c1b517Snbeams   CeedCallBackend(CeedVectorGetLength(values, &values_length));
1419f7c1b517Snbeams   CeedCallBackend(CeedVectorGetLength(assembled_qf, &assembled_qf_length));
1420f7c1b517Snbeams   if ((values_length > INT_MAX) || (assembled_qf_length > INT_MAX)) use_ceedsize_idx = 1;
1421004e4986SSebastian Grimberg 
1422f7c1b517Snbeams   // Setup
1423004e4986SSebastian Grimberg   if (!impl->asmb) CeedCallBackend(CeedSingleOperatorAssembleSetup_Cuda(op, use_ceedsize_idx));
1424004e4986SSebastian Grimberg   CeedOperatorAssemble_Cuda *asmb = impl->asmb;
1425004e4986SSebastian Grimberg 
1426004e4986SSebastian Grimberg   assert(asmb != NULL);
1427004e4986SSebastian Grimberg 
1428004e4986SSebastian Grimberg   // Assemble element operator
1429004e4986SSebastian Grimberg   CeedCallBackend(CeedVectorGetArray(values, CEED_MEM_DEVICE, &values_array));
1430004e4986SSebastian Grimberg   values_array += offset;
1431004e4986SSebastian Grimberg 
1432004e4986SSebastian Grimberg   CeedCallBackend(CeedOperatorGetActiveElemRestrictions(op, &rstr_in, &rstr_out));
1433004e4986SSebastian Grimberg   CeedCallBackend(CeedElemRestrictionGetNumElements(rstr_in, &num_elem_in));
1434004e4986SSebastian Grimberg   CeedCallBackend(CeedElemRestrictionGetElementSize(rstr_in, &elem_size_in));
1435004e4986SSebastian Grimberg 
1436004e4986SSebastian Grimberg   CeedCallBackend(CeedElemRestrictionGetType(rstr_in, &rstr_type_in));
1437004e4986SSebastian Grimberg   if (rstr_type_in == CEED_RESTRICTION_ORIENTED) {
1438004e4986SSebastian Grimberg     CeedCallBackend(CeedElemRestrictionGetOrientations(rstr_in, CEED_MEM_DEVICE, &orients_in));
1439004e4986SSebastian Grimberg   } else if (rstr_type_in == CEED_RESTRICTION_CURL_ORIENTED) {
1440004e4986SSebastian Grimberg     CeedCallBackend(CeedElemRestrictionGetCurlOrientations(rstr_in, CEED_MEM_DEVICE, &curl_orients_in));
1441004e4986SSebastian Grimberg   }
1442004e4986SSebastian Grimberg 
1443004e4986SSebastian Grimberg   if (rstr_in != rstr_out) {
1444004e4986SSebastian Grimberg     CeedCallBackend(CeedElemRestrictionGetNumElements(rstr_out, &num_elem_out));
1445004e4986SSebastian Grimberg     CeedCheck(num_elem_in == num_elem_out, ceed, CEED_ERROR_UNSUPPORTED,
1446004e4986SSebastian Grimberg               "Active input and output operator restrictions must have the same number of elements");
1447004e4986SSebastian Grimberg     CeedCallBackend(CeedElemRestrictionGetElementSize(rstr_out, &elem_size_out));
1448004e4986SSebastian Grimberg 
1449004e4986SSebastian Grimberg     CeedCallBackend(CeedElemRestrictionGetType(rstr_out, &rstr_type_out));
1450004e4986SSebastian Grimberg     if (rstr_type_out == CEED_RESTRICTION_ORIENTED) {
1451004e4986SSebastian Grimberg       CeedCallBackend(CeedElemRestrictionGetOrientations(rstr_out, CEED_MEM_DEVICE, &orients_out));
1452004e4986SSebastian Grimberg     } else if (rstr_type_out == CEED_RESTRICTION_CURL_ORIENTED) {
1453004e4986SSebastian Grimberg       CeedCallBackend(CeedElemRestrictionGetCurlOrientations(rstr_out, CEED_MEM_DEVICE, &curl_orients_out));
1454004e4986SSebastian Grimberg     }
1455004e4986SSebastian Grimberg   } else {
1456004e4986SSebastian Grimberg     elem_size_out    = elem_size_in;
1457004e4986SSebastian Grimberg     orients_out      = orients_in;
1458004e4986SSebastian Grimberg     curl_orients_out = curl_orients_in;
1459f7c1b517Snbeams   }
1460f7c1b517Snbeams 
1461cc132f9aSnbeams   // Compute B^T D B
1462004e4986SSebastian Grimberg   CeedInt shared_mem =
1463004e4986SSebastian Grimberg       ((curl_orients_in || curl_orients_out ? elem_size_in * elem_size_out : 0) + (curl_orients_in ? elem_size_in * asmb->block_size_y : 0)) *
1464004e4986SSebastian Grimberg       sizeof(CeedScalar);
1465004e4986SSebastian Grimberg   CeedInt grid   = CeedDivUpInt(num_elem_in, asmb->elems_per_block);
1466004e4986SSebastian Grimberg   void   *args[] = {(void *)&num_elem_in, &asmb->d_B_in,     &asmb->d_B_out,      &orients_in,  &curl_orients_in,
1467004e4986SSebastian Grimberg                     &orients_out,         &curl_orients_out, &assembled_qf_array, &values_array};
1468ca735530SJeremy L Thompson 
14692b730f8bSJeremy L Thompson   CeedCallBackend(
1470004e4986SSebastian Grimberg       CeedRunKernelDimShared_Cuda(ceed, asmb->LinearAssemble, grid, asmb->block_size_x, asmb->block_size_y, asmb->elems_per_block, shared_mem, args));
1471cc132f9aSnbeams 
1472cc132f9aSnbeams   // Restore arrays
14732b730f8bSJeremy L Thompson   CeedCallBackend(CeedVectorRestoreArray(values, &values_array));
1474004e4986SSebastian Grimberg   CeedCallBackend(CeedVectorRestoreArrayRead(assembled_qf, &assembled_qf_array));
1475cc132f9aSnbeams 
1476cc132f9aSnbeams   // Cleanup
14772b730f8bSJeremy L Thompson   CeedCallBackend(CeedVectorDestroy(&assembled_qf));
1478004e4986SSebastian Grimberg   if (rstr_type_in == CEED_RESTRICTION_ORIENTED) {
1479004e4986SSebastian Grimberg     CeedCallBackend(CeedElemRestrictionRestoreOrientations(rstr_in, &orients_in));
1480004e4986SSebastian Grimberg   } else if (rstr_type_in == CEED_RESTRICTION_CURL_ORIENTED) {
1481004e4986SSebastian Grimberg     CeedCallBackend(CeedElemRestrictionRestoreCurlOrientations(rstr_in, &curl_orients_in));
1482004e4986SSebastian Grimberg   }
1483004e4986SSebastian Grimberg   if (rstr_in != rstr_out) {
1484004e4986SSebastian Grimberg     if (rstr_type_out == CEED_RESTRICTION_ORIENTED) {
1485004e4986SSebastian Grimberg       CeedCallBackend(CeedElemRestrictionRestoreOrientations(rstr_out, &orients_out));
1486004e4986SSebastian Grimberg     } else if (rstr_type_out == CEED_RESTRICTION_CURL_ORIENTED) {
1487004e4986SSebastian Grimberg       CeedCallBackend(CeedElemRestrictionRestoreCurlOrientations(rstr_out, &curl_orients_out));
1488004e4986SSebastian Grimberg     }
1489004e4986SSebastian Grimberg   }
1490cc132f9aSnbeams   return CEED_ERROR_SUCCESS;
1491cc132f9aSnbeams }
1492cc132f9aSnbeams 
1493cc132f9aSnbeams //------------------------------------------------------------------------------
1494756ca9e9SJeremy L Thompson // Assemble Linear QFunction AtPoints
1495756ca9e9SJeremy L Thompson //------------------------------------------------------------------------------
1496756ca9e9SJeremy L Thompson static int CeedOperatorLinearAssembleQFunctionAtPoints_Cuda(CeedOperator op, CeedVector *assembled, CeedElemRestriction *rstr, CeedRequest *request) {
1497756ca9e9SJeremy L Thompson   return CeedError(CeedOperatorReturnCeed(op), CEED_ERROR_BACKEND, "Backend does not implement CeedOperatorLinearAssembleQFunction");
1498756ca9e9SJeremy L Thompson }
1499756ca9e9SJeremy L Thompson 
1500756ca9e9SJeremy L Thompson //------------------------------------------------------------------------------
1501756ca9e9SJeremy L Thompson // Assemble Linear Diagonal AtPoints
1502756ca9e9SJeremy L Thompson //------------------------------------------------------------------------------
1503756ca9e9SJeremy L Thompson static int CeedOperatorLinearAssembleAddDiagonalAtPoints_Cuda(CeedOperator op, CeedVector assembled, CeedRequest *request) {
1504382e9c83SJeremy L Thompson   CeedInt             max_num_points, num_elem, num_input_fields, num_output_fields;
1505349fb27dSJeremy L Thompson   CeedScalar         *e_data[2 * CEED_FIELD_MAX] = {NULL};
1506349fb27dSJeremy L Thompson   CeedQFunctionField *qf_input_fields, *qf_output_fields;
1507349fb27dSJeremy L Thompson   CeedQFunction       qf;
1508349fb27dSJeremy L Thompson   CeedOperatorField  *op_input_fields, *op_output_fields;
1509349fb27dSJeremy L Thompson   CeedOperator_Cuda  *impl;
1510349fb27dSJeremy L Thompson 
1511349fb27dSJeremy L Thompson   CeedCallBackend(CeedOperatorGetData(op, &impl));
1512349fb27dSJeremy L Thompson   CeedCallBackend(CeedOperatorGetQFunction(op, &qf));
1513349fb27dSJeremy L Thompson   CeedCallBackend(CeedOperatorGetNumElements(op, &num_elem));
1514349fb27dSJeremy L Thompson   CeedCallBackend(CeedOperatorGetFields(op, &num_input_fields, &op_input_fields, &num_output_fields, &op_output_fields));
1515349fb27dSJeremy L Thompson   CeedCallBackend(CeedQFunctionGetFields(qf, NULL, &qf_input_fields, NULL, &qf_output_fields));
1516349fb27dSJeremy L Thompson   CeedInt num_points[num_elem];
1517349fb27dSJeremy L Thompson 
1518349fb27dSJeremy L Thompson   // Setup
1519349fb27dSJeremy L Thompson   CeedCallBackend(CeedOperatorSetupAtPoints_Cuda(op));
1520349fb27dSJeremy L Thompson   max_num_points = impl->max_num_points;
1521349fb27dSJeremy L Thompson   for (CeedInt i = 0; i < num_elem; i++) num_points[i] = max_num_points;
1522349fb27dSJeremy L Thompson 
1523349fb27dSJeremy L Thompson   // Input Evecs and Restriction
1524349fb27dSJeremy L Thompson   CeedCallBackend(CeedOperatorSetupInputs_Cuda(num_input_fields, qf_input_fields, op_input_fields, NULL, true, e_data, impl, request));
1525349fb27dSJeremy L Thompson 
1526349fb27dSJeremy L Thompson   // Get point coordinates
1527349fb27dSJeremy L Thompson   if (!impl->point_coords_elem) {
1528349fb27dSJeremy L Thompson     CeedVector          point_coords = NULL;
1529349fb27dSJeremy L Thompson     CeedElemRestriction rstr_points  = NULL;
1530349fb27dSJeremy L Thompson 
1531349fb27dSJeremy L Thompson     CeedCallBackend(CeedOperatorAtPointsGetPoints(op, &rstr_points, &point_coords));
1532349fb27dSJeremy L Thompson     CeedCallBackend(CeedElemRestrictionCreateVector(rstr_points, NULL, &impl->point_coords_elem));
1533349fb27dSJeremy L Thompson     CeedCallBackend(CeedElemRestrictionApply(rstr_points, CEED_NOTRANSPOSE, point_coords, impl->point_coords_elem, request));
1534349fb27dSJeremy L Thompson   }
1535349fb27dSJeremy L Thompson 
1536382e9c83SJeremy L Thompson   // Clear active input Qvecs
1537382e9c83SJeremy L Thompson   for (CeedInt i = 0; i < num_input_fields; i++) {
1538382e9c83SJeremy L Thompson     CeedVector vec;
1539382e9c83SJeremy L Thompson 
1540382e9c83SJeremy L Thompson     CeedCallBackend(CeedOperatorFieldGetVector(op_input_fields[i], &vec));
1541382e9c83SJeremy L Thompson     if (vec != CEED_VECTOR_ACTIVE) continue;
1542382e9c83SJeremy L Thompson     CeedCallBackend(CeedVectorSetValue(impl->q_vecs_in[i], 0.0));
1543382e9c83SJeremy L Thompson   }
1544382e9c83SJeremy L Thompson 
1545349fb27dSJeremy L Thompson   // Input basis apply if needed
1546349fb27dSJeremy L Thompson   CeedCallBackend(CeedOperatorInputBasisAtPoints_Cuda(num_elem, num_points, qf_input_fields, op_input_fields, num_input_fields, true, e_data, impl));
1547349fb27dSJeremy L Thompson 
1548349fb27dSJeremy L Thompson   // Output pointers, as necessary
1549349fb27dSJeremy L Thompson   for (CeedInt i = 0; i < num_output_fields; i++) {
1550349fb27dSJeremy L Thompson     CeedEvalMode eval_mode;
1551349fb27dSJeremy L Thompson 
1552349fb27dSJeremy L Thompson     CeedCallBackend(CeedQFunctionFieldGetEvalMode(qf_output_fields[i], &eval_mode));
1553349fb27dSJeremy L Thompson     if (eval_mode == CEED_EVAL_NONE) {
1554349fb27dSJeremy L Thompson       // Set the output Q-Vector to use the E-Vector data directly.
1555349fb27dSJeremy L Thompson       CeedCallBackend(CeedVectorGetArrayWrite(impl->e_vecs[i + impl->num_inputs], CEED_MEM_DEVICE, &e_data[i + num_input_fields]));
1556349fb27dSJeremy L Thompson       CeedCallBackend(CeedVectorSetArray(impl->q_vecs_out[i], CEED_MEM_DEVICE, CEED_USE_POINTER, e_data[i + num_input_fields]));
1557349fb27dSJeremy L Thompson     }
1558349fb27dSJeremy L Thompson   }
1559349fb27dSJeremy L Thompson 
1560349fb27dSJeremy L Thompson   // Loop over active fields
1561349fb27dSJeremy L Thompson   for (CeedInt i = 0; i < num_input_fields; i++) {
1562382e9c83SJeremy L Thompson     bool                is_active_at_points = true;
1563382e9c83SJeremy L Thompson     CeedInt             elem_size = 1, num_comp_active = 1, e_vec_size = 0;
1564382e9c83SJeremy L Thompson     CeedRestrictionType rstr_type;
1565382e9c83SJeremy L Thompson     CeedVector          vec;
1566382e9c83SJeremy L Thompson     CeedElemRestriction elem_rstr;
1567382e9c83SJeremy L Thompson 
1568382e9c83SJeremy L Thompson     CeedCallBackend(CeedOperatorFieldGetVector(op_input_fields[i], &vec));
1569382e9c83SJeremy L Thompson     // -- Skip non-active input
1570382e9c83SJeremy L Thompson     if (vec != CEED_VECTOR_ACTIVE) continue;
1571382e9c83SJeremy L Thompson 
1572382e9c83SJeremy L Thompson     // -- Get active restriction type
1573382e9c83SJeremy L Thompson     CeedCallBackend(CeedOperatorFieldGetElemRestriction(op_input_fields[i], &elem_rstr));
1574382e9c83SJeremy L Thompson     CeedCallBackend(CeedElemRestrictionGetType(elem_rstr, &rstr_type));
1575382e9c83SJeremy L Thompson     is_active_at_points = rstr_type == CEED_RESTRICTION_POINTS;
1576382e9c83SJeremy L Thompson     if (!is_active_at_points) CeedCallBackend(CeedElemRestrictionGetElementSize(elem_rstr, &elem_size));
1577382e9c83SJeremy L Thompson     else elem_size = max_num_points;
1578382e9c83SJeremy L Thompson     CeedCallBackend(CeedElemRestrictionGetNumComponents(elem_rstr, &num_comp_active));
1579382e9c83SJeremy L Thompson 
1580382e9c83SJeremy L Thompson     e_vec_size = elem_size * num_comp_active;
1581382e9c83SJeremy L Thompson     for (CeedInt s = 0; s < e_vec_size; s++) {
1582349fb27dSJeremy L Thompson       bool         is_active_input = false;
1583349fb27dSJeremy L Thompson       CeedEvalMode eval_mode;
1584349fb27dSJeremy L Thompson       CeedVector   vec;
1585349fb27dSJeremy L Thompson       CeedBasis    basis;
1586349fb27dSJeremy L Thompson 
1587349fb27dSJeremy L Thompson       CeedCallBackend(CeedOperatorFieldGetVector(op_input_fields[i], &vec));
1588349fb27dSJeremy L Thompson       // Skip non-active input
1589349fb27dSJeremy L Thompson       is_active_input = vec == CEED_VECTOR_ACTIVE;
1590349fb27dSJeremy L Thompson       if (!is_active_input) continue;
1591349fb27dSJeremy L Thompson 
1592349fb27dSJeremy L Thompson       // Update unit vector
159313062808SJeremy L Thompson       if (s == 0) CeedCallBackend(CeedVectorSetValue(impl->e_vecs[i], 0.0));
1594349fb27dSJeremy L Thompson       else CeedCallBackend(CeedVectorSetValueStrided(impl->e_vecs[i], s - 1, e_vec_size, 0.0));
1595349fb27dSJeremy L Thompson       CeedCallBackend(CeedVectorSetValueStrided(impl->e_vecs[i], s, e_vec_size, 1.0));
1596349fb27dSJeremy L Thompson 
1597349fb27dSJeremy L Thompson       // Basis action
1598349fb27dSJeremy L Thompson       CeedCallBackend(CeedQFunctionFieldGetEvalMode(qf_input_fields[i], &eval_mode));
1599349fb27dSJeremy L Thompson       switch (eval_mode) {
1600349fb27dSJeremy L Thompson         case CEED_EVAL_NONE:
1601349fb27dSJeremy L Thompson           CeedCallBackend(CeedVectorSetArray(impl->q_vecs_in[i], CEED_MEM_DEVICE, CEED_USE_POINTER, e_data[i]));
1602349fb27dSJeremy L Thompson           break;
1603349fb27dSJeremy L Thompson         case CEED_EVAL_INTERP:
1604349fb27dSJeremy L Thompson         case CEED_EVAL_GRAD:
1605349fb27dSJeremy L Thompson         case CEED_EVAL_DIV:
1606349fb27dSJeremy L Thompson         case CEED_EVAL_CURL:
1607349fb27dSJeremy L Thompson           CeedCallBackend(CeedOperatorFieldGetBasis(op_input_fields[i], &basis));
1608349fb27dSJeremy L Thompson           CeedCallBackend(CeedBasisApplyAtPoints(basis, num_elem, num_points, CEED_NOTRANSPOSE, eval_mode, impl->point_coords_elem, impl->e_vecs[i],
1609349fb27dSJeremy L Thompson                                                  impl->q_vecs_in[i]));
1610349fb27dSJeremy L Thompson           break;
1611349fb27dSJeremy L Thompson         case CEED_EVAL_WEIGHT:
1612349fb27dSJeremy L Thompson           break;  // No action
1613349fb27dSJeremy L Thompson       }
1614349fb27dSJeremy L Thompson 
1615349fb27dSJeremy L Thompson       // Q function
1616349fb27dSJeremy L Thompson       CeedCallBackend(CeedQFunctionApply(qf, num_elem * max_num_points, impl->q_vecs_in, impl->q_vecs_out));
1617349fb27dSJeremy L Thompson 
1618349fb27dSJeremy L Thompson       // Output basis apply if needed
1619382e9c83SJeremy L Thompson       for (CeedInt j = 0; j < num_output_fields; j++) {
1620349fb27dSJeremy L Thompson         bool                is_active_output = false;
1621382e9c83SJeremy L Thompson         CeedInt             elem_size        = 0;
1622382e9c83SJeremy L Thompson         CeedRestrictionType rstr_type;
1623349fb27dSJeremy L Thompson         CeedEvalMode        eval_mode;
1624349fb27dSJeremy L Thompson         CeedVector          vec;
1625349fb27dSJeremy L Thompson         CeedElemRestriction elem_rstr;
1626349fb27dSJeremy L Thompson         CeedBasis           basis;
1627349fb27dSJeremy L Thompson 
1628382e9c83SJeremy L Thompson         CeedCallBackend(CeedOperatorFieldGetVector(op_output_fields[j], &vec));
1629382e9c83SJeremy L Thompson         // ---- Skip non-active output
1630349fb27dSJeremy L Thompson         is_active_output = vec == CEED_VECTOR_ACTIVE;
1631349fb27dSJeremy L Thompson         if (!is_active_output) continue;
1632349fb27dSJeremy L Thompson 
1633382e9c83SJeremy L Thompson         // ---- Check if elem size matches
1634382e9c83SJeremy L Thompson         CeedCallBackend(CeedOperatorFieldGetElemRestriction(op_output_fields[j], &elem_rstr));
1635382e9c83SJeremy L Thompson         CeedCallBackend(CeedElemRestrictionGetType(elem_rstr, &rstr_type));
1636382e9c83SJeremy L Thompson         if (is_active_at_points && rstr_type != CEED_RESTRICTION_POINTS) continue;
1637382e9c83SJeremy L Thompson         if (rstr_type == CEED_RESTRICTION_POINTS) {
1638382e9c83SJeremy L Thompson           CeedCallBackend(CeedElemRestrictionGetMaxPointsInElement(elem_rstr, &elem_size));
1639382e9c83SJeremy L Thompson         } else {
1640382e9c83SJeremy L Thompson           CeedCallBackend(CeedElemRestrictionGetElementSize(elem_rstr, &elem_size));
1641382e9c83SJeremy L Thompson         }
1642382e9c83SJeremy L Thompson         {
1643382e9c83SJeremy L Thompson           CeedInt num_comp = 0;
1644382e9c83SJeremy L Thompson 
1645382e9c83SJeremy L Thompson           CeedCallBackend(CeedElemRestrictionGetNumComponents(elem_rstr, &num_comp));
1646382e9c83SJeremy L Thompson           if (e_vec_size != num_comp * elem_size) continue;
1647382e9c83SJeremy L Thompson         }
1648382e9c83SJeremy L Thompson 
1649349fb27dSJeremy L Thompson         // Basis action
1650382e9c83SJeremy L Thompson         CeedCallBackend(CeedQFunctionFieldGetEvalMode(qf_output_fields[j], &eval_mode));
1651349fb27dSJeremy L Thompson         switch (eval_mode) {
1652349fb27dSJeremy L Thompson           case CEED_EVAL_NONE:
1653382e9c83SJeremy L Thompson             CeedCallBackend(CeedVectorRestoreArray(impl->e_vecs[j + impl->num_inputs], &e_data[j + num_input_fields]));
1654349fb27dSJeremy L Thompson             break;
1655349fb27dSJeremy L Thompson           case CEED_EVAL_INTERP:
1656349fb27dSJeremy L Thompson           case CEED_EVAL_GRAD:
1657349fb27dSJeremy L Thompson           case CEED_EVAL_DIV:
1658349fb27dSJeremy L Thompson           case CEED_EVAL_CURL:
1659382e9c83SJeremy L Thompson             CeedCallBackend(CeedOperatorFieldGetBasis(op_output_fields[j], &basis));
1660382e9c83SJeremy L Thompson             CeedCallBackend(CeedBasisApplyAtPoints(basis, num_elem, num_points, CEED_TRANSPOSE, eval_mode, impl->point_coords_elem,
1661382e9c83SJeremy L Thompson                                                    impl->q_vecs_out[j], impl->e_vecs[j + impl->num_inputs]));
1662349fb27dSJeremy L Thompson             break;
1663349fb27dSJeremy L Thompson           // LCOV_EXCL_START
1664349fb27dSJeremy L Thompson           case CEED_EVAL_WEIGHT: {
1665349fb27dSJeremy L Thompson             return CeedError(CeedOperatorReturnCeed(op), CEED_ERROR_BACKEND, "CEED_EVAL_WEIGHT cannot be an output evaluation mode");
1666349fb27dSJeremy L Thompson             // LCOV_EXCL_STOP
1667349fb27dSJeremy L Thompson           }
1668349fb27dSJeremy L Thompson         }
1669349fb27dSJeremy L Thompson 
1670349fb27dSJeremy L Thompson         // Mask output e-vec
1671382e9c83SJeremy L Thompson         CeedCallBackend(CeedVectorPointwiseMult(impl->e_vecs[j + impl->num_inputs], impl->e_vecs[i], impl->e_vecs[j + impl->num_inputs]));
1672349fb27dSJeremy L Thompson 
1673349fb27dSJeremy L Thompson         // Restrict
1674382e9c83SJeremy L Thompson         CeedCallBackend(CeedOperatorFieldGetElemRestriction(op_output_fields[j], &elem_rstr));
1675382e9c83SJeremy L Thompson         CeedCallBackend(CeedElemRestrictionApply(elem_rstr, CEED_TRANSPOSE, impl->e_vecs[j + impl->num_inputs], assembled, request));
1676349fb27dSJeremy L Thompson 
1677349fb27dSJeremy L Thompson         // Reset q_vec for
1678349fb27dSJeremy L Thompson         if (eval_mode == CEED_EVAL_NONE) {
1679382e9c83SJeremy L Thompson           CeedCallBackend(CeedVectorGetArrayWrite(impl->e_vecs[j + impl->num_inputs], CEED_MEM_DEVICE, &e_data[j + num_input_fields]));
1680382e9c83SJeremy L Thompson           CeedCallBackend(CeedVectorSetArray(impl->q_vecs_out[j], CEED_MEM_DEVICE, CEED_USE_POINTER, e_data[j + num_input_fields]));
1681349fb27dSJeremy L Thompson         }
1682349fb27dSJeremy L Thompson       }
1683382e9c83SJeremy L Thompson 
1684382e9c83SJeremy L Thompson       // Reset vec
168513062808SJeremy L Thompson       if (s == e_vec_size - 1 && i != num_input_fields - 1) CeedCallBackend(CeedVectorSetValue(impl->q_vecs_in[i], 0.0));
168686e10729SJeremy L Thompson     }
1687349fb27dSJeremy L Thompson   }
1688349fb27dSJeremy L Thompson 
1689349fb27dSJeremy L Thompson   // Restore CEED_EVAL_NONE
1690349fb27dSJeremy L Thompson   for (CeedInt i = 0; i < num_output_fields; i++) {
1691349fb27dSJeremy L Thompson     CeedEvalMode eval_mode;
1692349fb27dSJeremy L Thompson 
1693349fb27dSJeremy L Thompson     // Get eval_mode
1694349fb27dSJeremy L Thompson     CeedCallBackend(CeedQFunctionFieldGetEvalMode(qf_output_fields[i], &eval_mode));
1695349fb27dSJeremy L Thompson 
1696349fb27dSJeremy L Thompson     // Restore evec
1697349fb27dSJeremy L Thompson     CeedCallBackend(CeedQFunctionFieldGetEvalMode(qf_output_fields[i], &eval_mode));
1698349fb27dSJeremy L Thompson     if (eval_mode == CEED_EVAL_NONE) {
1699349fb27dSJeremy L Thompson       CeedCallBackend(CeedVectorRestoreArray(impl->e_vecs[i + impl->num_inputs], &e_data[i + num_input_fields]));
1700349fb27dSJeremy L Thompson     }
1701349fb27dSJeremy L Thompson   }
1702349fb27dSJeremy L Thompson 
1703349fb27dSJeremy L Thompson   // Restore input arrays
1704349fb27dSJeremy L Thompson   CeedCallBackend(CeedOperatorRestoreInputs_Cuda(num_input_fields, qf_input_fields, op_input_fields, true, e_data, impl));
1705349fb27dSJeremy L Thompson   return CEED_ERROR_SUCCESS;
1706756ca9e9SJeremy L Thompson }
1707756ca9e9SJeremy L Thompson 
1708756ca9e9SJeremy L Thompson //------------------------------------------------------------------------------
17090d0321e0SJeremy L Thompson // Create operator
17100d0321e0SJeremy L Thompson //------------------------------------------------------------------------------
17110d0321e0SJeremy L Thompson int CeedOperatorCreate_Cuda(CeedOperator op) {
17120d0321e0SJeremy L Thompson   Ceed               ceed;
17130d0321e0SJeremy L Thompson   CeedOperator_Cuda *impl;
17140d0321e0SJeremy L Thompson 
1715ca735530SJeremy L Thompson   CeedCallBackend(CeedOperatorGetCeed(op, &ceed));
17162b730f8bSJeremy L Thompson   CeedCallBackend(CeedCalloc(1, &impl));
17172b730f8bSJeremy L Thompson   CeedCallBackend(CeedOperatorSetData(op, impl));
17180d0321e0SJeremy L Thompson 
17192b730f8bSJeremy L Thompson   CeedCallBackend(CeedSetBackendFunction(ceed, "Operator", op, "LinearAssembleQFunction", CeedOperatorLinearAssembleQFunction_Cuda));
17202b730f8bSJeremy L Thompson   CeedCallBackend(CeedSetBackendFunction(ceed, "Operator", op, "LinearAssembleQFunctionUpdate", CeedOperatorLinearAssembleQFunctionUpdate_Cuda));
17212b730f8bSJeremy L Thompson   CeedCallBackend(CeedSetBackendFunction(ceed, "Operator", op, "LinearAssembleAddDiagonal", CeedOperatorLinearAssembleAddDiagonal_Cuda));
17222b730f8bSJeremy L Thompson   CeedCallBackend(
17232b730f8bSJeremy L Thompson       CeedSetBackendFunction(ceed, "Operator", op, "LinearAssembleAddPointBlockDiagonal", CeedOperatorLinearAssembleAddPointBlockDiagonal_Cuda));
17242b730f8bSJeremy L Thompson   CeedCallBackend(CeedSetBackendFunction(ceed, "Operator", op, "LinearAssembleSingle", CeedSingleOperatorAssemble_Cuda));
17252b730f8bSJeremy L Thompson   CeedCallBackend(CeedSetBackendFunction(ceed, "Operator", op, "ApplyAdd", CeedOperatorApplyAdd_Cuda));
17262b730f8bSJeremy L Thompson   CeedCallBackend(CeedSetBackendFunction(ceed, "Operator", op, "Destroy", CeedOperatorDestroy_Cuda));
17270d0321e0SJeremy L Thompson   return CEED_ERROR_SUCCESS;
17280d0321e0SJeremy L Thompson }
17290d0321e0SJeremy L Thompson 
17300d0321e0SJeremy L Thompson //------------------------------------------------------------------------------
173167d9480aSJeremy L Thompson // Create operator AtPoints
1732756ca9e9SJeremy L Thompson //------------------------------------------------------------------------------
1733756ca9e9SJeremy L Thompson int CeedOperatorCreateAtPoints_Cuda(CeedOperator op) {
1734756ca9e9SJeremy L Thompson   Ceed               ceed;
1735756ca9e9SJeremy L Thompson   CeedOperator_Cuda *impl;
1736756ca9e9SJeremy L Thompson 
1737756ca9e9SJeremy L Thompson   CeedCallBackend(CeedOperatorGetCeed(op, &ceed));
1738756ca9e9SJeremy L Thompson   CeedCallBackend(CeedCalloc(1, &impl));
1739756ca9e9SJeremy L Thompson   CeedCallBackend(CeedOperatorSetData(op, impl));
1740756ca9e9SJeremy L Thompson 
1741756ca9e9SJeremy L Thompson   CeedCallBackend(CeedSetBackendFunction(ceed, "Operator", op, "LinearAssembleQFunction", CeedOperatorLinearAssembleQFunctionAtPoints_Cuda));
1742756ca9e9SJeremy L Thompson   CeedCallBackend(CeedSetBackendFunction(ceed, "Operator", op, "LinearAssembleAddDiagonal", CeedOperatorLinearAssembleAddDiagonalAtPoints_Cuda));
1743756ca9e9SJeremy L Thompson   CeedCallBackend(CeedSetBackendFunction(ceed, "Operator", op, "ApplyAdd", CeedOperatorApplyAddAtPoints_Cuda));
1744756ca9e9SJeremy L Thompson   CeedCallBackend(CeedSetBackendFunction(ceed, "Operator", op, "Destroy", CeedOperatorDestroy_Cuda));
1745756ca9e9SJeremy L Thompson   return CEED_ERROR_SUCCESS;
1746756ca9e9SJeremy L Thompson }
1747756ca9e9SJeremy L Thompson 
1748756ca9e9SJeremy L Thompson //------------------------------------------------------------------------------
1749