xref: /libCEED/rust/libceed-sys/c-src/backends/cuda-ref/ceed-cuda-ref-operator.c (revision f8a0df597ca176fee6b07766b6124704acaa0050)
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
303aab95c0SJeremy L Thompson   CeedCallBackend(CeedFree(&impl->skip_rstr_in));
31*f8a0df59SJeremy L Thompson   CeedCallBackend(CeedFree(&impl->skip_rstr_out));
32*f8a0df59SJeremy L Thompson   CeedCallBackend(CeedFree(&impl->apply_add_basis_out));
33ca735530SJeremy L Thompson   for (CeedInt i = 0; i < impl->num_inputs + impl->num_outputs; i++) {
34ca735530SJeremy L Thompson     CeedCallBackend(CeedVectorDestroy(&impl->e_vecs[i]));
350d0321e0SJeremy L Thompson   }
36ca735530SJeremy L Thompson   CeedCallBackend(CeedFree(&impl->e_vecs));
37c1222711SJeremy L Thompson   CeedCallBackend(CeedFree(&impl->input_states));
380d0321e0SJeremy L Thompson 
39ca735530SJeremy L Thompson   for (CeedInt i = 0; i < impl->num_inputs; i++) {
40ca735530SJeremy L Thompson     CeedCallBackend(CeedVectorDestroy(&impl->q_vecs_in[i]));
410d0321e0SJeremy L Thompson   }
42ca735530SJeremy L Thompson   CeedCallBackend(CeedFree(&impl->q_vecs_in));
430d0321e0SJeremy L Thompson 
44ca735530SJeremy L Thompson   for (CeedInt i = 0; i < impl->num_outputs; i++) {
45ca735530SJeremy L Thompson     CeedCallBackend(CeedVectorDestroy(&impl->q_vecs_out[i]));
460d0321e0SJeremy L Thompson   }
47ca735530SJeremy L Thompson   CeedCallBackend(CeedFree(&impl->q_vecs_out));
48756ca9e9SJeremy L Thompson   CeedCallBackend(CeedVectorDestroy(&impl->point_coords_elem));
490d0321e0SJeremy L Thompson 
500d0321e0SJeremy L Thompson   // QFunction assembly data
51ca735530SJeremy L Thompson   for (CeedInt i = 0; i < impl->num_active_in; i++) {
52ca735530SJeremy L Thompson     CeedCallBackend(CeedVectorDestroy(&impl->qf_active_in[i]));
530d0321e0SJeremy L Thompson   }
54ca735530SJeremy L Thompson   CeedCallBackend(CeedFree(&impl->qf_active_in));
550d0321e0SJeremy L Thompson 
560d0321e0SJeremy L Thompson   // Diag data
570d0321e0SJeremy L Thompson   if (impl->diag) {
580d0321e0SJeremy L Thompson     Ceed ceed;
59ca735530SJeremy L Thompson 
602b730f8bSJeremy L Thompson     CeedCallBackend(CeedOperatorGetCeed(op, &ceed));
61cbfe683aSSebastian Grimberg     if (impl->diag->module) {
622b730f8bSJeremy L Thompson       CeedCallCuda(ceed, cuModuleUnload(impl->diag->module));
63cbfe683aSSebastian Grimberg     }
64cbfe683aSSebastian Grimberg     if (impl->diag->module_point_block) {
65cbfe683aSSebastian Grimberg       CeedCallCuda(ceed, cuModuleUnload(impl->diag->module_point_block));
66cbfe683aSSebastian Grimberg     }
67004e4986SSebastian Grimberg     CeedCallCuda(ceed, cudaFree(impl->diag->d_eval_modes_in));
68004e4986SSebastian Grimberg     CeedCallCuda(ceed, cudaFree(impl->diag->d_eval_modes_out));
692b730f8bSJeremy L Thompson     CeedCallCuda(ceed, cudaFree(impl->diag->d_identity));
70ca735530SJeremy L Thompson     CeedCallCuda(ceed, cudaFree(impl->diag->d_interp_in));
71ca735530SJeremy L Thompson     CeedCallCuda(ceed, cudaFree(impl->diag->d_interp_out));
72ca735530SJeremy L Thompson     CeedCallCuda(ceed, cudaFree(impl->diag->d_grad_in));
73ca735530SJeremy L Thompson     CeedCallCuda(ceed, cudaFree(impl->diag->d_grad_out));
74004e4986SSebastian Grimberg     CeedCallCuda(ceed, cudaFree(impl->diag->d_div_in));
75004e4986SSebastian Grimberg     CeedCallCuda(ceed, cudaFree(impl->diag->d_div_out));
76004e4986SSebastian Grimberg     CeedCallCuda(ceed, cudaFree(impl->diag->d_curl_in));
77004e4986SSebastian Grimberg     CeedCallCuda(ceed, cudaFree(impl->diag->d_curl_out));
78004e4986SSebastian Grimberg     CeedCallBackend(CeedElemRestrictionDestroy(&impl->diag->diag_rstr));
79506b1a0cSSebastian Grimberg     CeedCallBackend(CeedElemRestrictionDestroy(&impl->diag->point_block_diag_rstr));
80ca735530SJeremy L Thompson     CeedCallBackend(CeedVectorDestroy(&impl->diag->elem_diag));
81ca735530SJeremy L Thompson     CeedCallBackend(CeedVectorDestroy(&impl->diag->point_block_elem_diag));
820d0321e0SJeremy L Thompson   }
832b730f8bSJeremy L Thompson   CeedCallBackend(CeedFree(&impl->diag));
840d0321e0SJeremy L Thompson 
85cc132f9aSnbeams   if (impl->asmb) {
86cc132f9aSnbeams     Ceed ceed;
87ca735530SJeremy L Thompson 
882b730f8bSJeremy L Thompson     CeedCallBackend(CeedOperatorGetCeed(op, &ceed));
892b730f8bSJeremy L Thompson     CeedCallCuda(ceed, cuModuleUnload(impl->asmb->module));
902b730f8bSJeremy L Thompson     CeedCallCuda(ceed, cudaFree(impl->asmb->d_B_in));
912b730f8bSJeremy L Thompson     CeedCallCuda(ceed, cudaFree(impl->asmb->d_B_out));
92cc132f9aSnbeams   }
932b730f8bSJeremy L Thompson   CeedCallBackend(CeedFree(&impl->asmb));
94cc132f9aSnbeams 
952b730f8bSJeremy L Thompson   CeedCallBackend(CeedFree(&impl));
960d0321e0SJeremy L Thompson   return CEED_ERROR_SUCCESS;
970d0321e0SJeremy L Thompson }
980d0321e0SJeremy L Thompson 
990d0321e0SJeremy L Thompson //------------------------------------------------------------------------------
1000d0321e0SJeremy L Thompson // Setup infields or outfields
1010d0321e0SJeremy L Thompson //------------------------------------------------------------------------------
102*f8a0df59SJeremy L Thompson static int CeedOperatorSetupFields_Cuda(CeedQFunction qf, CeedOperator op, bool is_input, bool is_at_points, bool *skip_rstr, bool *apply_add_basis,
103*f8a0df59SJeremy L Thompson                                         CeedVector *e_vecs, CeedVector *q_vecs, CeedInt start_e, CeedInt num_fields, CeedInt Q, CeedInt num_elem) {
1040d0321e0SJeremy L Thompson   Ceed                ceed;
105ca735530SJeremy L Thompson   CeedQFunctionField *qf_fields;
106ca735530SJeremy L Thompson   CeedOperatorField  *op_fields;
1070d0321e0SJeremy L Thompson 
108ca735530SJeremy L Thompson   CeedCallBackend(CeedOperatorGetCeed(op, &ceed));
109ca735530SJeremy L Thompson   if (is_input) {
110ca735530SJeremy L Thompson     CeedCallBackend(CeedOperatorGetFields(op, NULL, &op_fields, NULL, NULL));
111ca735530SJeremy L Thompson     CeedCallBackend(CeedQFunctionGetFields(qf, NULL, &qf_fields, NULL, NULL));
1120d0321e0SJeremy L Thompson   } else {
113ca735530SJeremy L Thompson     CeedCallBackend(CeedOperatorGetFields(op, NULL, NULL, NULL, &op_fields));
114ca735530SJeremy L Thompson     CeedCallBackend(CeedQFunctionGetFields(qf, NULL, NULL, NULL, &qf_fields));
1150d0321e0SJeremy L Thompson   }
1160d0321e0SJeremy L Thompson 
1170d0321e0SJeremy L Thompson   // Loop over fields
118ca735530SJeremy L Thompson   for (CeedInt i = 0; i < num_fields; i++) {
119004e4986SSebastian Grimberg     bool         is_strided = false, skip_restriction = false;
120004e4986SSebastian Grimberg     CeedSize     q_size;
121004e4986SSebastian Grimberg     CeedInt      size;
122004e4986SSebastian Grimberg     CeedEvalMode eval_mode;
123ca735530SJeremy L Thompson     CeedBasis    basis;
1240d0321e0SJeremy L Thompson 
125004e4986SSebastian Grimberg     CeedCallBackend(CeedQFunctionFieldGetEvalMode(qf_fields[i], &eval_mode));
126004e4986SSebastian Grimberg     if (eval_mode != CEED_EVAL_WEIGHT) {
127edb2538eSJeremy L Thompson       CeedElemRestriction elem_rstr;
128ca735530SJeremy L Thompson 
1290d0321e0SJeremy L Thompson       // Check whether this field can skip the element restriction:
130004e4986SSebastian Grimberg       // Must be passive input, with eval_mode NONE, and have a strided restriction with CEED_STRIDES_BACKEND.
131004e4986SSebastian Grimberg       CeedCallBackend(CeedOperatorFieldGetElemRestriction(op_fields[i], &elem_rstr));
1320d0321e0SJeremy L Thompson 
1330d0321e0SJeremy L Thompson       // First, check whether the field is input or output:
134ca735530SJeremy L Thompson       if (is_input) {
135ca735530SJeremy L Thompson         CeedVector vec;
136ca735530SJeremy L Thompson 
137004e4986SSebastian Grimberg         // Check for passive input
138ca735530SJeremy L Thompson         CeedCallBackend(CeedOperatorFieldGetVector(op_fields[i], &vec));
139ca735530SJeremy L Thompson         if (vec != CEED_VECTOR_ACTIVE) {
140004e4986SSebastian Grimberg           // Check eval_mode
141004e4986SSebastian Grimberg           if (eval_mode == CEED_EVAL_NONE) {
1420d0321e0SJeremy L Thompson             // Check for strided restriction
143edb2538eSJeremy L Thompson             CeedCallBackend(CeedElemRestrictionIsStrided(elem_rstr, &is_strided));
144ca735530SJeremy L Thompson             if (is_strided) {
1450d0321e0SJeremy L Thompson               // Check if vector is already in preferred backend ordering
146edb2538eSJeremy L Thompson               CeedCallBackend(CeedElemRestrictionHasBackendStrides(elem_rstr, &skip_restriction));
1470d0321e0SJeremy L Thompson             }
1480d0321e0SJeremy L Thompson           }
1490d0321e0SJeremy L Thompson         }
1500d0321e0SJeremy L Thompson       }
151ca735530SJeremy L Thompson       if (skip_restriction) {
152ea61e9acSJeremy L Thompson         // We do not need an E-Vector, but will use the input field vector's data directly in the operator application.
153004e4986SSebastian Grimberg         e_vecs[i + start_e] = NULL;
1540d0321e0SJeremy L Thompson       } else {
155004e4986SSebastian Grimberg         CeedCallBackend(CeedElemRestrictionCreateVector(elem_rstr, NULL, &e_vecs[i + start_e]));
1560d0321e0SJeremy L Thompson       }
1570d0321e0SJeremy L Thompson     }
1580d0321e0SJeremy L Thompson 
159004e4986SSebastian Grimberg     switch (eval_mode) {
1600d0321e0SJeremy L Thompson       case CEED_EVAL_NONE:
161ca735530SJeremy L Thompson         CeedCallBackend(CeedQFunctionFieldGetSize(qf_fields[i], &size));
162ca735530SJeremy L Thompson         q_size = (CeedSize)num_elem * Q * size;
163ca735530SJeremy L Thompson         CeedCallBackend(CeedVectorCreate(ceed, q_size, &q_vecs[i]));
1640d0321e0SJeremy L Thompson         break;
1650d0321e0SJeremy L Thompson       case CEED_EVAL_INTERP:
1660d0321e0SJeremy L Thompson       case CEED_EVAL_GRAD:
167004e4986SSebastian Grimberg       case CEED_EVAL_DIV:
168004e4986SSebastian Grimberg       case CEED_EVAL_CURL:
169ca735530SJeremy L Thompson         CeedCallBackend(CeedQFunctionFieldGetSize(qf_fields[i], &size));
170ca735530SJeremy L Thompson         q_size = (CeedSize)num_elem * Q * size;
171ca735530SJeremy L Thompson         CeedCallBackend(CeedVectorCreate(ceed, q_size, &q_vecs[i]));
1720d0321e0SJeremy L Thompson         break;
1730d0321e0SJeremy L Thompson       case CEED_EVAL_WEIGHT:  // Only on input fields
174ca735530SJeremy L Thompson         CeedCallBackend(CeedOperatorFieldGetBasis(op_fields[i], &basis));
175ca735530SJeremy L Thompson         q_size = (CeedSize)num_elem * Q;
176ca735530SJeremy L Thompson         CeedCallBackend(CeedVectorCreate(ceed, q_size, &q_vecs[i]));
177756ca9e9SJeremy L Thompson         if (is_at_points) {
178756ca9e9SJeremy L Thompson           CeedInt num_points[num_elem];
179756ca9e9SJeremy L Thompson 
180756ca9e9SJeremy L Thompson           for (CeedInt i = 0; i < num_elem; i++) num_points[i] = Q;
181756ca9e9SJeremy L Thompson           CeedCallBackend(
182756ca9e9SJeremy L Thompson               CeedBasisApplyAtPoints(basis, num_elem, num_points, CEED_NOTRANSPOSE, CEED_EVAL_WEIGHT, CEED_VECTOR_NONE, CEED_VECTOR_NONE, q_vecs[i]));
183756ca9e9SJeremy L Thompson         } else {
184ca735530SJeremy L Thompson           CeedCallBackend(CeedBasisApply(basis, num_elem, CEED_NOTRANSPOSE, CEED_EVAL_WEIGHT, CEED_VECTOR_NONE, q_vecs[i]));
185756ca9e9SJeremy L Thompson         }
1860d0321e0SJeremy L Thompson         break;
1870d0321e0SJeremy L Thompson     }
1880d0321e0SJeremy L Thompson   }
189*f8a0df59SJeremy L Thompson   // Drop duplicate restrictions
1903aab95c0SJeremy L Thompson   if (is_input) {
1913aab95c0SJeremy L Thompson     for (CeedInt i = 0; i < num_fields; i++) {
1923aab95c0SJeremy L Thompson       CeedVector          vec_i;
1933aab95c0SJeremy L Thompson       CeedElemRestriction rstr_i;
1943aab95c0SJeremy L Thompson 
1953aab95c0SJeremy L Thompson       CeedCallBackend(CeedOperatorFieldGetVector(op_fields[i], &vec_i));
1963aab95c0SJeremy L Thompson       CeedCallBackend(CeedOperatorFieldGetElemRestriction(op_fields[i], &rstr_i));
1973aab95c0SJeremy L Thompson       for (CeedInt j = i + 1; j < num_fields; j++) {
1983aab95c0SJeremy L Thompson         CeedVector          vec_j;
1993aab95c0SJeremy L Thompson         CeedElemRestriction rstr_j;
2003aab95c0SJeremy L Thompson 
2013aab95c0SJeremy L Thompson         CeedCallBackend(CeedOperatorFieldGetVector(op_fields[j], &vec_j));
2023aab95c0SJeremy L Thompson         CeedCallBackend(CeedOperatorFieldGetElemRestriction(op_fields[j], &rstr_j));
2033aab95c0SJeremy L Thompson         if (vec_i == vec_j && rstr_i == rstr_j) {
204*f8a0df59SJeremy L Thompson           CeedCallBackend(CeedVectorReferenceCopy(e_vecs[i + start_e], &e_vecs[j + start_e]));
2053aab95c0SJeremy L Thompson           skip_rstr[j] = true;
2063aab95c0SJeremy L Thompson         }
2073aab95c0SJeremy L Thompson       }
2083aab95c0SJeremy L Thompson     }
209*f8a0df59SJeremy L Thompson   } else {
210*f8a0df59SJeremy L Thompson     for (CeedInt i = num_fields - 1; i >= 0; i--) {
211*f8a0df59SJeremy L Thompson       CeedVector          vec_i;
212*f8a0df59SJeremy L Thompson       CeedElemRestriction rstr_i;
213*f8a0df59SJeremy L Thompson 
214*f8a0df59SJeremy L Thompson       CeedCallBackend(CeedOperatorFieldGetVector(op_fields[i], &vec_i));
215*f8a0df59SJeremy L Thompson       CeedCallBackend(CeedOperatorFieldGetElemRestriction(op_fields[i], &rstr_i));
216*f8a0df59SJeremy L Thompson       for (CeedInt j = i - 1; j >= 0; j--) {
217*f8a0df59SJeremy L Thompson         CeedVector          vec_j;
218*f8a0df59SJeremy L Thompson         CeedElemRestriction rstr_j;
219*f8a0df59SJeremy L Thompson 
220*f8a0df59SJeremy L Thompson         CeedCallBackend(CeedOperatorFieldGetVector(op_fields[j], &vec_j));
221*f8a0df59SJeremy L Thompson         CeedCallBackend(CeedOperatorFieldGetElemRestriction(op_fields[j], &rstr_j));
222*f8a0df59SJeremy L Thompson         if (vec_i == vec_j && rstr_i == rstr_j) {
223*f8a0df59SJeremy L Thompson           CeedCallBackend(CeedVectorReferenceCopy(e_vecs[i + start_e], &e_vecs[j + start_e]));
224*f8a0df59SJeremy L Thompson           skip_rstr[j]       = true;
225*f8a0df59SJeremy L Thompson           apply_add_basis[i] = true;
226*f8a0df59SJeremy L Thompson         }
227*f8a0df59SJeremy L Thompson       }
228*f8a0df59SJeremy L Thompson     }
2293aab95c0SJeremy L Thompson   }
2300d0321e0SJeremy L Thompson   return CEED_ERROR_SUCCESS;
2310d0321e0SJeremy L Thompson }
2320d0321e0SJeremy L Thompson 
2330d0321e0SJeremy L Thompson //------------------------------------------------------------------------------
234ea61e9acSJeremy L Thompson // CeedOperator needs to connect all the named fields (be they active or passive) to the named inputs and outputs of its CeedQFunction.
2350d0321e0SJeremy L Thompson //------------------------------------------------------------------------------
2360d0321e0SJeremy L Thompson static int CeedOperatorSetup_Cuda(CeedOperator op) {
2370d0321e0SJeremy L Thompson   Ceed                ceed;
238ca735530SJeremy L Thompson   bool                is_setup_done;
239ca735530SJeremy L Thompson   CeedInt             Q, num_elem, num_input_fields, num_output_fields;
240ca735530SJeremy L Thompson   CeedQFunctionField *qf_input_fields, *qf_output_fields;
2410d0321e0SJeremy L Thompson   CeedQFunction       qf;
242ca735530SJeremy L Thompson   CeedOperatorField  *op_input_fields, *op_output_fields;
243ca735530SJeremy L Thompson   CeedOperator_Cuda  *impl;
244ca735530SJeremy L Thompson 
245ca735530SJeremy L Thompson   CeedCallBackend(CeedOperatorIsSetupDone(op, &is_setup_done));
246ca735530SJeremy L Thompson   if (is_setup_done) return CEED_ERROR_SUCCESS;
247ca735530SJeremy L Thompson 
248ca735530SJeremy L Thompson   CeedCallBackend(CeedOperatorGetCeed(op, &ceed));
249ca735530SJeremy L Thompson   CeedCallBackend(CeedOperatorGetData(op, &impl));
2502b730f8bSJeremy L Thompson   CeedCallBackend(CeedOperatorGetQFunction(op, &qf));
2512b730f8bSJeremy L Thompson   CeedCallBackend(CeedOperatorGetNumQuadraturePoints(op, &Q));
252ca735530SJeremy L Thompson   CeedCallBackend(CeedOperatorGetNumElements(op, &num_elem));
253ca735530SJeremy L Thompson   CeedCallBackend(CeedOperatorGetFields(op, &num_input_fields, &op_input_fields, &num_output_fields, &op_output_fields));
254ca735530SJeremy L Thompson   CeedCallBackend(CeedQFunctionGetFields(qf, NULL, &qf_input_fields, NULL, &qf_output_fields));
2550d0321e0SJeremy L Thompson 
2560d0321e0SJeremy L Thompson   // Allocate
257ca735530SJeremy L Thompson   CeedCallBackend(CeedCalloc(num_input_fields + num_output_fields, &impl->e_vecs));
2583aab95c0SJeremy L Thompson   CeedCallBackend(CeedCalloc(CEED_FIELD_MAX, &impl->skip_rstr_in));
259*f8a0df59SJeremy L Thompson   CeedCallBackend(CeedCalloc(CEED_FIELD_MAX, &impl->skip_rstr_out));
260*f8a0df59SJeremy L Thompson   CeedCallBackend(CeedCalloc(CEED_FIELD_MAX, &impl->apply_add_basis_out));
261c1222711SJeremy L Thompson   CeedCallBackend(CeedCalloc(CEED_FIELD_MAX, &impl->input_states));
262ca735530SJeremy L Thompson   CeedCallBackend(CeedCalloc(CEED_FIELD_MAX, &impl->q_vecs_in));
263ca735530SJeremy L Thompson   CeedCallBackend(CeedCalloc(CEED_FIELD_MAX, &impl->q_vecs_out));
264ca735530SJeremy L Thompson   impl->num_inputs  = num_input_fields;
265ca735530SJeremy L Thompson   impl->num_outputs = num_output_fields;
2660d0321e0SJeremy L Thompson 
267ca735530SJeremy L Thompson   // Set up infield and outfield e_vecs and q_vecs
2680d0321e0SJeremy L Thompson   // Infields
2693aab95c0SJeremy L Thompson   CeedCallBackend(
270*f8a0df59SJeremy L Thompson       CeedOperatorSetupFields_Cuda(qf, op, true, false, impl->skip_rstr_in, NULL, impl->e_vecs, impl->q_vecs_in, 0, num_input_fields, Q, num_elem));
2710d0321e0SJeremy L Thompson   // Outfields
272*f8a0df59SJeremy L Thompson   CeedCallBackend(CeedOperatorSetupFields_Cuda(qf, op, false, false, impl->skip_rstr_out, impl->apply_add_basis_out, impl->e_vecs, impl->q_vecs_out,
273*f8a0df59SJeremy L Thompson                                                num_input_fields, num_output_fields, Q, num_elem));
2740d0321e0SJeremy L Thompson 
2752b730f8bSJeremy L Thompson   CeedCallBackend(CeedOperatorSetSetupDone(op));
2760d0321e0SJeremy L Thompson   return CEED_ERROR_SUCCESS;
2770d0321e0SJeremy L Thompson }
2780d0321e0SJeremy L Thompson 
2790d0321e0SJeremy L Thompson //------------------------------------------------------------------------------
2800d0321e0SJeremy L Thompson // Setup Operator Inputs
2810d0321e0SJeremy L Thompson //------------------------------------------------------------------------------
282ca735530SJeremy L Thompson static inline int CeedOperatorSetupInputs_Cuda(CeedInt num_input_fields, CeedQFunctionField *qf_input_fields, CeedOperatorField *op_input_fields,
283004e4986SSebastian Grimberg                                                CeedVector in_vec, const bool skip_active, CeedScalar *e_data[2 * CEED_FIELD_MAX],
2840d0321e0SJeremy L Thompson                                                CeedOperator_Cuda *impl, CeedRequest *request) {
285ca735530SJeremy L Thompson   for (CeedInt i = 0; i < num_input_fields; i++) {
286004e4986SSebastian Grimberg     CeedEvalMode        eval_mode;
2870d0321e0SJeremy L Thompson     CeedVector          vec;
288edb2538eSJeremy L Thompson     CeedElemRestriction elem_rstr;
2890d0321e0SJeremy L Thompson 
2900d0321e0SJeremy L Thompson     // Get input vector
291ca735530SJeremy L Thompson     CeedCallBackend(CeedOperatorFieldGetVector(op_input_fields[i], &vec));
2920d0321e0SJeremy L Thompson     if (vec == CEED_VECTOR_ACTIVE) {
293004e4986SSebastian Grimberg       if (skip_active) continue;
294ca735530SJeremy L Thompson       else vec = in_vec;
2950d0321e0SJeremy L Thompson     }
2960d0321e0SJeremy L Thompson 
297004e4986SSebastian Grimberg     CeedCallBackend(CeedQFunctionFieldGetEvalMode(qf_input_fields[i], &eval_mode));
298004e4986SSebastian Grimberg     if (eval_mode == CEED_EVAL_WEIGHT) {  // Skip
2990d0321e0SJeremy L Thompson     } else {
300004e4986SSebastian Grimberg       // Get input vector
301004e4986SSebastian Grimberg       CeedCallBackend(CeedOperatorFieldGetVector(op_input_fields[i], &vec));
3020d0321e0SJeremy L Thompson       // Get input element restriction
303edb2538eSJeremy L Thompson       CeedCallBackend(CeedOperatorFieldGetElemRestriction(op_input_fields[i], &elem_rstr));
304ca735530SJeremy L Thompson       if (vec == CEED_VECTOR_ACTIVE) vec = in_vec;
3050d0321e0SJeremy L Thompson       // Restrict, if necessary
306ca735530SJeremy L Thompson       if (!impl->e_vecs[i]) {
3070d0321e0SJeremy L Thompson         // No restriction for this field; read data directly from vec.
308ca735530SJeremy L Thompson         CeedCallBackend(CeedVectorGetArrayRead(vec, CEED_MEM_DEVICE, (const CeedScalar **)&e_data[i]));
3090d0321e0SJeremy L Thompson       } else {
310c1222711SJeremy L Thompson         uint64_t state;
311c1222711SJeremy L Thompson 
312c1222711SJeremy L Thompson         CeedCallBackend(CeedVectorGetState(vec, &state));
3133aab95c0SJeremy L Thompson         if (state != impl->input_states[i] && !impl->skip_rstr_in[i]) {
314edb2538eSJeremy L Thompson           CeedCallBackend(CeedElemRestrictionApply(elem_rstr, CEED_NOTRANSPOSE, vec, impl->e_vecs[i], request));
315c1222711SJeremy L Thompson         }
3163aab95c0SJeremy L Thompson         impl->input_states[i] = state;
3170d0321e0SJeremy L Thompson         // Get evec
318ca735530SJeremy L Thompson         CeedCallBackend(CeedVectorGetArrayRead(impl->e_vecs[i], CEED_MEM_DEVICE, (const CeedScalar **)&e_data[i]));
3190d0321e0SJeremy L Thompson       }
3200d0321e0SJeremy L Thompson     }
3210d0321e0SJeremy L Thompson   }
3220d0321e0SJeremy L Thompson   return CEED_ERROR_SUCCESS;
3230d0321e0SJeremy L Thompson }
3240d0321e0SJeremy L Thompson 
3250d0321e0SJeremy L Thompson //------------------------------------------------------------------------------
3260d0321e0SJeremy L Thompson // Input Basis Action
3270d0321e0SJeremy L Thompson //------------------------------------------------------------------------------
328ca735530SJeremy L Thompson static inline int CeedOperatorInputBasis_Cuda(CeedInt num_elem, CeedQFunctionField *qf_input_fields, CeedOperatorField *op_input_fields,
329004e4986SSebastian Grimberg                                               CeedInt num_input_fields, const bool skip_active, CeedScalar *e_data[2 * CEED_FIELD_MAX],
3302b730f8bSJeremy L Thompson                                               CeedOperator_Cuda *impl) {
331ca735530SJeremy L Thompson   for (CeedInt i = 0; i < num_input_fields; i++) {
332ca735530SJeremy L Thompson     CeedInt             elem_size, size;
333004e4986SSebastian Grimberg     CeedEvalMode        eval_mode;
334edb2538eSJeremy L Thompson     CeedElemRestriction elem_rstr;
3350d0321e0SJeremy L Thompson     CeedBasis           basis;
3360d0321e0SJeremy L Thompson 
3370d0321e0SJeremy L Thompson     // Skip active input
338004e4986SSebastian Grimberg     if (skip_active) {
3390d0321e0SJeremy L Thompson       CeedVector vec;
340ca735530SJeremy L Thompson 
341ca735530SJeremy L Thompson       CeedCallBackend(CeedOperatorFieldGetVector(op_input_fields[i], &vec));
3422b730f8bSJeremy L Thompson       if (vec == CEED_VECTOR_ACTIVE) continue;
3430d0321e0SJeremy L Thompson     }
344004e4986SSebastian Grimberg     // Get elem_size, eval_mode, size
345edb2538eSJeremy L Thompson     CeedCallBackend(CeedOperatorFieldGetElemRestriction(op_input_fields[i], &elem_rstr));
346edb2538eSJeremy L Thompson     CeedCallBackend(CeedElemRestrictionGetElementSize(elem_rstr, &elem_size));
347004e4986SSebastian Grimberg     CeedCallBackend(CeedQFunctionFieldGetEvalMode(qf_input_fields[i], &eval_mode));
348ca735530SJeremy L Thompson     CeedCallBackend(CeedQFunctionFieldGetSize(qf_input_fields[i], &size));
3490d0321e0SJeremy L Thompson     // Basis action
350004e4986SSebastian Grimberg     switch (eval_mode) {
3510d0321e0SJeremy L Thompson       case CEED_EVAL_NONE:
352ca735530SJeremy L Thompson         CeedCallBackend(CeedVectorSetArray(impl->q_vecs_in[i], CEED_MEM_DEVICE, CEED_USE_POINTER, e_data[i]));
3530d0321e0SJeremy L Thompson         break;
3540d0321e0SJeremy L Thompson       case CEED_EVAL_INTERP:
3550d0321e0SJeremy L Thompson       case CEED_EVAL_GRAD:
356004e4986SSebastian Grimberg       case CEED_EVAL_DIV:
357004e4986SSebastian Grimberg       case CEED_EVAL_CURL:
358ca735530SJeremy L Thompson         CeedCallBackend(CeedOperatorFieldGetBasis(op_input_fields[i], &basis));
359004e4986SSebastian Grimberg         CeedCallBackend(CeedBasisApply(basis, num_elem, CEED_NOTRANSPOSE, eval_mode, impl->e_vecs[i], impl->q_vecs_in[i]));
3600d0321e0SJeremy L Thompson         break;
3610d0321e0SJeremy L Thompson       case CEED_EVAL_WEIGHT:
3620d0321e0SJeremy L Thompson         break;  // No action
3630d0321e0SJeremy L Thompson     }
3640d0321e0SJeremy L Thompson   }
3650d0321e0SJeremy L Thompson   return CEED_ERROR_SUCCESS;
3660d0321e0SJeremy L Thompson }
3670d0321e0SJeremy L Thompson 
3680d0321e0SJeremy L Thompson //------------------------------------------------------------------------------
3690d0321e0SJeremy L Thompson // Restore Input Vectors
3700d0321e0SJeremy L Thompson //------------------------------------------------------------------------------
371ca735530SJeremy L Thompson static inline int CeedOperatorRestoreInputs_Cuda(CeedInt num_input_fields, CeedQFunctionField *qf_input_fields, CeedOperatorField *op_input_fields,
372004e4986SSebastian Grimberg                                                  const bool skip_active, CeedScalar *e_data[2 * CEED_FIELD_MAX], CeedOperator_Cuda *impl) {
373ca735530SJeremy L Thompson   for (CeedInt i = 0; i < num_input_fields; i++) {
374004e4986SSebastian Grimberg     CeedEvalMode eval_mode;
3750d0321e0SJeremy L Thompson     CeedVector   vec;
3760d0321e0SJeremy L Thompson 
3770d0321e0SJeremy L Thompson     // Skip active input
378004e4986SSebastian Grimberg     if (skip_active) {
379ca735530SJeremy L Thompson       CeedCallBackend(CeedOperatorFieldGetVector(op_input_fields[i], &vec));
3802b730f8bSJeremy L Thompson       if (vec == CEED_VECTOR_ACTIVE) continue;
3810d0321e0SJeremy L Thompson     }
382004e4986SSebastian Grimberg     CeedCallBackend(CeedQFunctionFieldGetEvalMode(qf_input_fields[i], &eval_mode));
383004e4986SSebastian Grimberg     if (eval_mode == CEED_EVAL_WEIGHT) {  // Skip
3840d0321e0SJeremy L Thompson     } else {
385ca735530SJeremy L Thompson       if (!impl->e_vecs[i]) {  // This was a skip_restriction case
386ca735530SJeremy L Thompson         CeedCallBackend(CeedOperatorFieldGetVector(op_input_fields[i], &vec));
387ca735530SJeremy L Thompson         CeedCallBackend(CeedVectorRestoreArrayRead(vec, (const CeedScalar **)&e_data[i]));
3880d0321e0SJeremy L Thompson       } else {
389ca735530SJeremy L Thompson         CeedCallBackend(CeedVectorRestoreArrayRead(impl->e_vecs[i], (const CeedScalar **)&e_data[i]));
3900d0321e0SJeremy L Thompson       }
3910d0321e0SJeremy L Thompson     }
3920d0321e0SJeremy L Thompson   }
3930d0321e0SJeremy L Thompson   return CEED_ERROR_SUCCESS;
3940d0321e0SJeremy L Thompson }
3950d0321e0SJeremy L Thompson 
3960d0321e0SJeremy L Thompson //------------------------------------------------------------------------------
3970d0321e0SJeremy L Thompson // Apply and add to output
3980d0321e0SJeremy L Thompson //------------------------------------------------------------------------------
399ca735530SJeremy L Thompson static int CeedOperatorApplyAdd_Cuda(CeedOperator op, CeedVector in_vec, CeedVector out_vec, CeedRequest *request) {
400ca735530SJeremy L Thompson   CeedInt             Q, num_elem, elem_size, num_input_fields, num_output_fields, size;
401ca735530SJeremy L Thompson   CeedScalar         *e_data[2 * CEED_FIELD_MAX] = {NULL};
402ca735530SJeremy L Thompson   CeedQFunctionField *qf_input_fields, *qf_output_fields;
4030d0321e0SJeremy L Thompson   CeedQFunction       qf;
404004e4986SSebastian Grimberg   CeedOperatorField  *op_input_fields, *op_output_fields;
405004e4986SSebastian Grimberg   CeedOperator_Cuda  *impl;
406ca735530SJeremy L Thompson 
407ca735530SJeremy L Thompson   CeedCallBackend(CeedOperatorGetData(op, &impl));
4082b730f8bSJeremy L Thompson   CeedCallBackend(CeedOperatorGetQFunction(op, &qf));
4092b730f8bSJeremy L Thompson   CeedCallBackend(CeedOperatorGetNumQuadraturePoints(op, &Q));
410ca735530SJeremy L Thompson   CeedCallBackend(CeedOperatorGetNumElements(op, &num_elem));
411ca735530SJeremy L Thompson   CeedCallBackend(CeedOperatorGetFields(op, &num_input_fields, &op_input_fields, &num_output_fields, &op_output_fields));
412ca735530SJeremy L Thompson   CeedCallBackend(CeedQFunctionGetFields(qf, NULL, &qf_input_fields, NULL, &qf_output_fields));
4130d0321e0SJeremy L Thompson 
4140d0321e0SJeremy L Thompson   // Setup
4152b730f8bSJeremy L Thompson   CeedCallBackend(CeedOperatorSetup_Cuda(op));
4160d0321e0SJeremy L Thompson 
417004e4986SSebastian Grimberg   // Input Evecs and Restriction
418ca735530SJeremy L Thompson   CeedCallBackend(CeedOperatorSetupInputs_Cuda(num_input_fields, qf_input_fields, op_input_fields, in_vec, false, e_data, impl, request));
4190d0321e0SJeremy L Thompson 
4200d0321e0SJeremy L Thompson   // Input basis apply if needed
421ca735530SJeremy L Thompson   CeedCallBackend(CeedOperatorInputBasis_Cuda(num_elem, qf_input_fields, op_input_fields, num_input_fields, false, e_data, impl));
4220d0321e0SJeremy L Thompson 
4230d0321e0SJeremy L Thompson   // Output pointers, as necessary
424ca735530SJeremy L Thompson   for (CeedInt i = 0; i < num_output_fields; i++) {
425004e4986SSebastian Grimberg     CeedEvalMode eval_mode;
426004e4986SSebastian Grimberg 
427004e4986SSebastian Grimberg     CeedCallBackend(CeedQFunctionFieldGetEvalMode(qf_output_fields[i], &eval_mode));
428004e4986SSebastian Grimberg     if (eval_mode == CEED_EVAL_NONE) {
4290d0321e0SJeremy L Thompson       // Set the output Q-Vector to use the E-Vector data directly.
430ca735530SJeremy L Thompson       CeedCallBackend(CeedVectorGetArrayWrite(impl->e_vecs[i + impl->num_inputs], CEED_MEM_DEVICE, &e_data[i + num_input_fields]));
431ca735530SJeremy L Thompson       CeedCallBackend(CeedVectorSetArray(impl->q_vecs_out[i], CEED_MEM_DEVICE, CEED_USE_POINTER, e_data[i + num_input_fields]));
4320d0321e0SJeremy L Thompson     }
4330d0321e0SJeremy L Thompson   }
4340d0321e0SJeremy L Thompson 
4350d0321e0SJeremy L Thompson   // Q function
436ca735530SJeremy L Thompson   CeedCallBackend(CeedQFunctionApply(qf, num_elem * Q, impl->q_vecs_in, impl->q_vecs_out));
4370d0321e0SJeremy L Thompson 
4380d0321e0SJeremy L Thompson   // Output basis apply if needed
439ca735530SJeremy L Thompson   for (CeedInt i = 0; i < num_output_fields; i++) {
440004e4986SSebastian Grimberg     CeedEvalMode        eval_mode;
441edb2538eSJeremy L Thompson     CeedElemRestriction elem_rstr;
442ca735530SJeremy L Thompson     CeedBasis           basis;
443ca735530SJeremy L Thompson 
444004e4986SSebastian Grimberg     // Get elem_size, eval_mode, size
445edb2538eSJeremy L Thompson     CeedCallBackend(CeedOperatorFieldGetElemRestriction(op_output_fields[i], &elem_rstr));
446edb2538eSJeremy L Thompson     CeedCallBackend(CeedElemRestrictionGetElementSize(elem_rstr, &elem_size));
447004e4986SSebastian Grimberg     CeedCallBackend(CeedQFunctionFieldGetEvalMode(qf_output_fields[i], &eval_mode));
448ca735530SJeremy L Thompson     CeedCallBackend(CeedQFunctionFieldGetSize(qf_output_fields[i], &size));
4490d0321e0SJeremy L Thompson     // Basis action
450004e4986SSebastian Grimberg     switch (eval_mode) {
4510d0321e0SJeremy L Thompson       case CEED_EVAL_NONE:
452004e4986SSebastian Grimberg         break;  // No action
4530d0321e0SJeremy L Thompson       case CEED_EVAL_INTERP:
4540d0321e0SJeremy L Thompson       case CEED_EVAL_GRAD:
455004e4986SSebastian Grimberg       case CEED_EVAL_DIV:
456004e4986SSebastian Grimberg       case CEED_EVAL_CURL:
457ca735530SJeremy L Thompson         CeedCallBackend(CeedOperatorFieldGetBasis(op_output_fields[i], &basis));
458*f8a0df59SJeremy L Thompson         if (impl->apply_add_basis_out[i]) {
459*f8a0df59SJeremy L Thompson           CeedCallBackend(CeedBasisApplyAdd(basis, num_elem, CEED_TRANSPOSE, eval_mode, impl->q_vecs_out[i], impl->e_vecs[i + impl->num_inputs]));
460*f8a0df59SJeremy L Thompson         } else {
461004e4986SSebastian Grimberg           CeedCallBackend(CeedBasisApply(basis, num_elem, CEED_TRANSPOSE, eval_mode, impl->q_vecs_out[i], impl->e_vecs[i + impl->num_inputs]));
462*f8a0df59SJeremy L Thompson         }
4630d0321e0SJeremy L Thompson         break;
4640d0321e0SJeremy L Thompson       // LCOV_EXCL_START
4650d0321e0SJeremy L Thompson       case CEED_EVAL_WEIGHT: {
4666e536b99SJeremy L Thompson         return CeedError(CeedOperatorReturnCeed(op), CEED_ERROR_BACKEND, "CEED_EVAL_WEIGHT cannot be an output evaluation mode");
4670d0321e0SJeremy L Thompson         // LCOV_EXCL_STOP
4680d0321e0SJeremy L Thompson       }
4690d0321e0SJeremy L Thompson     }
470004e4986SSebastian Grimberg   }
4710d0321e0SJeremy L Thompson 
4720d0321e0SJeremy L Thompson   // Output restriction
473ca735530SJeremy L Thompson   for (CeedInt i = 0; i < num_output_fields; i++) {
474004e4986SSebastian Grimberg     CeedEvalMode        eval_mode;
475ca735530SJeremy L Thompson     CeedVector          vec;
476edb2538eSJeremy L Thompson     CeedElemRestriction elem_rstr;
477ca735530SJeremy L Thompson 
4780d0321e0SJeremy L Thompson     // Restore evec
479004e4986SSebastian Grimberg     CeedCallBackend(CeedQFunctionFieldGetEvalMode(qf_output_fields[i], &eval_mode));
480004e4986SSebastian Grimberg     if (eval_mode == CEED_EVAL_NONE) {
481ca735530SJeremy L Thompson       CeedCallBackend(CeedVectorRestoreArray(impl->e_vecs[i + impl->num_inputs], &e_data[i + num_input_fields]));
4820d0321e0SJeremy L Thompson     }
483*f8a0df59SJeremy L Thompson     if (impl->skip_rstr_out[i]) continue;
4840d0321e0SJeremy L Thompson     // Get output vector
485ca735530SJeremy L Thompson     CeedCallBackend(CeedOperatorFieldGetVector(op_output_fields[i], &vec));
4860d0321e0SJeremy L Thompson     // Restrict
487edb2538eSJeremy L Thompson     CeedCallBackend(CeedOperatorFieldGetElemRestriction(op_output_fields[i], &elem_rstr));
4880d0321e0SJeremy L Thompson     // Active
489ca735530SJeremy L Thompson     if (vec == CEED_VECTOR_ACTIVE) vec = out_vec;
4900d0321e0SJeremy L Thompson 
491edb2538eSJeremy L Thompson     CeedCallBackend(CeedElemRestrictionApply(elem_rstr, CEED_TRANSPOSE, impl->e_vecs[i + impl->num_inputs], vec, request));
4920d0321e0SJeremy L Thompson   }
4930d0321e0SJeremy L Thompson 
4940d0321e0SJeremy L Thompson   // Restore input arrays
495ca735530SJeremy L Thompson   CeedCallBackend(CeedOperatorRestoreInputs_Cuda(num_input_fields, qf_input_fields, op_input_fields, false, e_data, impl));
4960d0321e0SJeremy L Thompson   return CEED_ERROR_SUCCESS;
4970d0321e0SJeremy L Thompson }
4980d0321e0SJeremy L Thompson 
4990d0321e0SJeremy L Thompson //------------------------------------------------------------------------------
500756ca9e9SJeremy L Thompson // CeedOperator needs to connect all the named fields (be they active or passive) to the named inputs and outputs of its CeedQFunction.
501756ca9e9SJeremy L Thompson //------------------------------------------------------------------------------
502756ca9e9SJeremy L Thompson static int CeedOperatorSetupAtPoints_Cuda(CeedOperator op) {
503756ca9e9SJeremy L Thompson   Ceed                ceed;
504756ca9e9SJeremy L Thompson   bool                is_setup_done;
505756ca9e9SJeremy L Thompson   CeedInt             max_num_points = -1, num_elem, num_input_fields, num_output_fields;
506756ca9e9SJeremy L Thompson   CeedQFunctionField *qf_input_fields, *qf_output_fields;
507756ca9e9SJeremy L Thompson   CeedQFunction       qf;
508756ca9e9SJeremy L Thompson   CeedOperatorField  *op_input_fields, *op_output_fields;
509756ca9e9SJeremy L Thompson   CeedOperator_Cuda  *impl;
510756ca9e9SJeremy L Thompson 
511756ca9e9SJeremy L Thompson   CeedCallBackend(CeedOperatorIsSetupDone(op, &is_setup_done));
512756ca9e9SJeremy L Thompson   if (is_setup_done) return CEED_ERROR_SUCCESS;
513756ca9e9SJeremy L Thompson 
514756ca9e9SJeremy L Thompson   CeedCallBackend(CeedOperatorGetCeed(op, &ceed));
515756ca9e9SJeremy L Thompson   CeedCallBackend(CeedOperatorGetData(op, &impl));
516756ca9e9SJeremy L Thompson   CeedCallBackend(CeedOperatorGetQFunction(op, &qf));
517756ca9e9SJeremy L Thompson   CeedCallBackend(CeedOperatorGetNumElements(op, &num_elem));
518756ca9e9SJeremy L Thompson   CeedCallBackend(CeedOperatorGetFields(op, &num_input_fields, &op_input_fields, &num_output_fields, &op_output_fields));
519756ca9e9SJeremy L Thompson   CeedCallBackend(CeedQFunctionGetFields(qf, NULL, &qf_input_fields, NULL, &qf_output_fields));
520756ca9e9SJeremy L Thompson   {
521756ca9e9SJeremy L Thompson     CeedElemRestriction elem_rstr = NULL;
522756ca9e9SJeremy L Thompson 
523756ca9e9SJeremy L Thompson     CeedCallBackend(CeedOperatorAtPointsGetPoints(op, &elem_rstr, NULL));
524756ca9e9SJeremy L Thompson     CeedCallBackend(CeedElemRestrictionGetMaxPointsInElement(elem_rstr, &max_num_points));
525756ca9e9SJeremy L Thompson   }
526756ca9e9SJeremy L Thompson   impl->max_num_points = max_num_points;
527756ca9e9SJeremy L Thompson 
528756ca9e9SJeremy L Thompson   // Allocate
529756ca9e9SJeremy L Thompson   CeedCallBackend(CeedCalloc(num_input_fields + num_output_fields, &impl->e_vecs));
5303aab95c0SJeremy L Thompson   CeedCallBackend(CeedCalloc(CEED_FIELD_MAX, &impl->skip_rstr_in));
531*f8a0df59SJeremy L Thompson   CeedCallBackend(CeedCalloc(CEED_FIELD_MAX, &impl->skip_rstr_out));
532*f8a0df59SJeremy L Thompson   CeedCallBackend(CeedCalloc(CEED_FIELD_MAX, &impl->apply_add_basis_out));
533756ca9e9SJeremy L Thompson   CeedCallBackend(CeedCalloc(CEED_FIELD_MAX, &impl->input_states));
534756ca9e9SJeremy L Thompson   CeedCallBackend(CeedCalloc(CEED_FIELD_MAX, &impl->q_vecs_in));
535756ca9e9SJeremy L Thompson   CeedCallBackend(CeedCalloc(CEED_FIELD_MAX, &impl->q_vecs_out));
536756ca9e9SJeremy L Thompson   impl->num_inputs  = num_input_fields;
537756ca9e9SJeremy L Thompson   impl->num_outputs = num_output_fields;
538756ca9e9SJeremy L Thompson 
539756ca9e9SJeremy L Thompson   // Set up infield and outfield e_vecs and q_vecs
540756ca9e9SJeremy L Thompson   // Infields
541*f8a0df59SJeremy L Thompson   CeedCallBackend(CeedOperatorSetupFields_Cuda(qf, op, true, true, impl->skip_rstr_in, NULL, impl->e_vecs, impl->q_vecs_in, 0, num_input_fields,
5423aab95c0SJeremy L Thompson                                                max_num_points, num_elem));
543756ca9e9SJeremy L Thompson   // Outfields
544*f8a0df59SJeremy L Thompson   CeedCallBackend(CeedOperatorSetupFields_Cuda(qf, op, false, true, impl->skip_rstr_out, impl->apply_add_basis_out, impl->e_vecs, impl->q_vecs_out,
545*f8a0df59SJeremy L Thompson                                                num_input_fields, num_output_fields, max_num_points, num_elem));
546756ca9e9SJeremy L Thompson 
547756ca9e9SJeremy L Thompson   CeedCallBackend(CeedOperatorSetSetupDone(op));
548756ca9e9SJeremy L Thompson   return CEED_ERROR_SUCCESS;
549756ca9e9SJeremy L Thompson }
550756ca9e9SJeremy L Thompson 
551756ca9e9SJeremy L Thompson //------------------------------------------------------------------------------
55267d9480aSJeremy L Thompson // Input Basis Action AtPoints
553756ca9e9SJeremy L Thompson //------------------------------------------------------------------------------
554756ca9e9SJeremy L Thompson static inline int CeedOperatorInputBasisAtPoints_Cuda(CeedInt num_elem, const CeedInt *num_points, CeedQFunctionField *qf_input_fields,
555756ca9e9SJeremy L Thompson                                                       CeedOperatorField *op_input_fields, CeedInt num_input_fields, const bool skip_active,
556756ca9e9SJeremy L Thompson                                                       CeedScalar *e_data[2 * CEED_FIELD_MAX], CeedOperator_Cuda *impl) {
557756ca9e9SJeremy L Thompson   for (CeedInt i = 0; i < num_input_fields; i++) {
558756ca9e9SJeremy L Thompson     CeedInt             elem_size, size;
559756ca9e9SJeremy L Thompson     CeedEvalMode        eval_mode;
560756ca9e9SJeremy L Thompson     CeedElemRestriction elem_rstr;
561756ca9e9SJeremy L Thompson     CeedBasis           basis;
562756ca9e9SJeremy L Thompson 
563756ca9e9SJeremy L Thompson     // Skip active input
564756ca9e9SJeremy L Thompson     if (skip_active) {
565756ca9e9SJeremy L Thompson       CeedVector vec;
566756ca9e9SJeremy L Thompson 
567756ca9e9SJeremy L Thompson       CeedCallBackend(CeedOperatorFieldGetVector(op_input_fields[i], &vec));
568756ca9e9SJeremy L Thompson       if (vec == CEED_VECTOR_ACTIVE) continue;
569756ca9e9SJeremy L Thompson     }
570756ca9e9SJeremy L Thompson     // Get elem_size, eval_mode, size
571756ca9e9SJeremy L Thompson     CeedCallBackend(CeedOperatorFieldGetElemRestriction(op_input_fields[i], &elem_rstr));
572756ca9e9SJeremy L Thompson     CeedCallBackend(CeedElemRestrictionGetElementSize(elem_rstr, &elem_size));
573756ca9e9SJeremy L Thompson     CeedCallBackend(CeedQFunctionFieldGetEvalMode(qf_input_fields[i], &eval_mode));
574756ca9e9SJeremy L Thompson     CeedCallBackend(CeedQFunctionFieldGetSize(qf_input_fields[i], &size));
575756ca9e9SJeremy L Thompson     // Basis action
576756ca9e9SJeremy L Thompson     switch (eval_mode) {
577756ca9e9SJeremy L Thompson       case CEED_EVAL_NONE:
578756ca9e9SJeremy L Thompson         CeedCallBackend(CeedVectorSetArray(impl->q_vecs_in[i], CEED_MEM_DEVICE, CEED_USE_POINTER, e_data[i]));
579756ca9e9SJeremy L Thompson         break;
580756ca9e9SJeremy L Thompson       case CEED_EVAL_INTERP:
581756ca9e9SJeremy L Thompson       case CEED_EVAL_GRAD:
582756ca9e9SJeremy L Thompson       case CEED_EVAL_DIV:
583756ca9e9SJeremy L Thompson       case CEED_EVAL_CURL:
584756ca9e9SJeremy L Thompson         CeedCallBackend(CeedOperatorFieldGetBasis(op_input_fields[i], &basis));
585756ca9e9SJeremy L Thompson         CeedCallBackend(CeedBasisApplyAtPoints(basis, num_elem, num_points, CEED_NOTRANSPOSE, eval_mode, impl->point_coords_elem, impl->e_vecs[i],
586756ca9e9SJeremy L Thompson                                                impl->q_vecs_in[i]));
587756ca9e9SJeremy L Thompson         break;
588756ca9e9SJeremy L Thompson       case CEED_EVAL_WEIGHT:
589756ca9e9SJeremy L Thompson         break;  // No action
590756ca9e9SJeremy L Thompson     }
591756ca9e9SJeremy L Thompson   }
592756ca9e9SJeremy L Thompson   return CEED_ERROR_SUCCESS;
593756ca9e9SJeremy L Thompson }
594756ca9e9SJeremy L Thompson 
595756ca9e9SJeremy L Thompson //------------------------------------------------------------------------------
59667d9480aSJeremy L Thompson // Apply and add to output AtPoints
597756ca9e9SJeremy L Thompson //------------------------------------------------------------------------------
598756ca9e9SJeremy L Thompson static int CeedOperatorApplyAddAtPoints_Cuda(CeedOperator op, CeedVector in_vec, CeedVector out_vec, CeedRequest *request) {
599756ca9e9SJeremy L Thompson   CeedInt             max_num_points, num_elem, elem_size, num_input_fields, num_output_fields, size;
600756ca9e9SJeremy L Thompson   CeedScalar         *e_data[2 * CEED_FIELD_MAX] = {NULL};
601756ca9e9SJeremy L Thompson   CeedQFunctionField *qf_input_fields, *qf_output_fields;
602756ca9e9SJeremy L Thompson   CeedQFunction       qf;
603756ca9e9SJeremy L Thompson   CeedOperatorField  *op_input_fields, *op_output_fields;
604756ca9e9SJeremy L Thompson   CeedOperator_Cuda  *impl;
605756ca9e9SJeremy L Thompson 
606756ca9e9SJeremy L Thompson   CeedCallBackend(CeedOperatorGetData(op, &impl));
607756ca9e9SJeremy L Thompson   CeedCallBackend(CeedOperatorGetQFunction(op, &qf));
608756ca9e9SJeremy L Thompson   CeedCallBackend(CeedOperatorGetNumElements(op, &num_elem));
609756ca9e9SJeremy L Thompson   CeedCallBackend(CeedOperatorGetFields(op, &num_input_fields, &op_input_fields, &num_output_fields, &op_output_fields));
610756ca9e9SJeremy L Thompson   CeedCallBackend(CeedQFunctionGetFields(qf, NULL, &qf_input_fields, NULL, &qf_output_fields));
611756ca9e9SJeremy L Thompson   CeedInt num_points[num_elem];
612756ca9e9SJeremy L Thompson 
613756ca9e9SJeremy L Thompson   // Setup
614756ca9e9SJeremy L Thompson   CeedCallBackend(CeedOperatorSetupAtPoints_Cuda(op));
615756ca9e9SJeremy L Thompson   max_num_points = impl->max_num_points;
616756ca9e9SJeremy L Thompson   for (CeedInt i = 0; i < num_elem; i++) num_points[i] = max_num_points;
617756ca9e9SJeremy L Thompson 
618756ca9e9SJeremy L Thompson   // Input Evecs and Restriction
619756ca9e9SJeremy L Thompson   CeedCallBackend(CeedOperatorSetupInputs_Cuda(num_input_fields, qf_input_fields, op_input_fields, in_vec, false, e_data, impl, request));
620756ca9e9SJeremy L Thompson 
621756ca9e9SJeremy L Thompson   // Get point coordinates
622756ca9e9SJeremy L Thompson   if (!impl->point_coords_elem) {
623756ca9e9SJeremy L Thompson     CeedVector          point_coords = NULL;
624756ca9e9SJeremy L Thompson     CeedElemRestriction rstr_points  = NULL;
625756ca9e9SJeremy L Thompson 
626756ca9e9SJeremy L Thompson     CeedCallBackend(CeedOperatorAtPointsGetPoints(op, &rstr_points, &point_coords));
627756ca9e9SJeremy L Thompson     CeedCallBackend(CeedElemRestrictionCreateVector(rstr_points, NULL, &impl->point_coords_elem));
628756ca9e9SJeremy L Thompson     CeedCallBackend(CeedElemRestrictionApply(rstr_points, CEED_NOTRANSPOSE, point_coords, impl->point_coords_elem, request));
629756ca9e9SJeremy L Thompson   }
630756ca9e9SJeremy L Thompson 
631756ca9e9SJeremy L Thompson   // Input basis apply if needed
632756ca9e9SJeremy L Thompson   CeedCallBackend(CeedOperatorInputBasisAtPoints_Cuda(num_elem, num_points, qf_input_fields, op_input_fields, num_input_fields, false, e_data, impl));
633756ca9e9SJeremy L Thompson 
634756ca9e9SJeremy L Thompson   // Output pointers, as necessary
635756ca9e9SJeremy L Thompson   for (CeedInt i = 0; i < num_output_fields; i++) {
636756ca9e9SJeremy L Thompson     CeedEvalMode eval_mode;
637756ca9e9SJeremy L Thompson 
638756ca9e9SJeremy L Thompson     CeedCallBackend(CeedQFunctionFieldGetEvalMode(qf_output_fields[i], &eval_mode));
639756ca9e9SJeremy L Thompson     if (eval_mode == CEED_EVAL_NONE) {
640756ca9e9SJeremy L Thompson       // Set the output Q-Vector to use the E-Vector data directly.
641756ca9e9SJeremy L Thompson       CeedCallBackend(CeedVectorGetArrayWrite(impl->e_vecs[i + impl->num_inputs], CEED_MEM_DEVICE, &e_data[i + num_input_fields]));
642756ca9e9SJeremy L Thompson       CeedCallBackend(CeedVectorSetArray(impl->q_vecs_out[i], CEED_MEM_DEVICE, CEED_USE_POINTER, e_data[i + num_input_fields]));
643756ca9e9SJeremy L Thompson     }
644756ca9e9SJeremy L Thompson   }
645756ca9e9SJeremy L Thompson 
646756ca9e9SJeremy L Thompson   // Q function
647756ca9e9SJeremy L Thompson   CeedCallBackend(CeedQFunctionApply(qf, num_elem * max_num_points, impl->q_vecs_in, impl->q_vecs_out));
648756ca9e9SJeremy L Thompson 
649756ca9e9SJeremy L Thompson   // Output basis apply if needed
650756ca9e9SJeremy L Thompson   for (CeedInt i = 0; i < num_output_fields; i++) {
651756ca9e9SJeremy L Thompson     CeedEvalMode        eval_mode;
652756ca9e9SJeremy L Thompson     CeedElemRestriction elem_rstr;
653756ca9e9SJeremy L Thompson     CeedBasis           basis;
654756ca9e9SJeremy L Thompson 
655756ca9e9SJeremy L Thompson     // Get elem_size, eval_mode, size
656756ca9e9SJeremy L Thompson     CeedCallBackend(CeedOperatorFieldGetElemRestriction(op_output_fields[i], &elem_rstr));
657756ca9e9SJeremy L Thompson     CeedCallBackend(CeedElemRestrictionGetElementSize(elem_rstr, &elem_size));
658756ca9e9SJeremy L Thompson     CeedCallBackend(CeedQFunctionFieldGetEvalMode(qf_output_fields[i], &eval_mode));
659756ca9e9SJeremy L Thompson     CeedCallBackend(CeedQFunctionFieldGetSize(qf_output_fields[i], &size));
660756ca9e9SJeremy L Thompson     // Basis action
661756ca9e9SJeremy L Thompson     switch (eval_mode) {
662756ca9e9SJeremy L Thompson       case CEED_EVAL_NONE:
663756ca9e9SJeremy L Thompson         break;  // No action
664756ca9e9SJeremy L Thompson       case CEED_EVAL_INTERP:
665756ca9e9SJeremy L Thompson       case CEED_EVAL_GRAD:
666756ca9e9SJeremy L Thompson       case CEED_EVAL_DIV:
667756ca9e9SJeremy L Thompson       case CEED_EVAL_CURL:
668756ca9e9SJeremy L Thompson         CeedCallBackend(CeedOperatorFieldGetBasis(op_output_fields[i], &basis));
669*f8a0df59SJeremy L Thompson         if (impl->apply_add_basis_out[i]) {
670*f8a0df59SJeremy L Thompson           CeedCallBackend(CeedBasisApplyAddAtPoints(basis, num_elem, num_points, CEED_TRANSPOSE, eval_mode, impl->point_coords_elem,
671*f8a0df59SJeremy L Thompson                                                     impl->q_vecs_out[i], impl->e_vecs[i + impl->num_inputs]));
672*f8a0df59SJeremy L Thompson         } else {
673756ca9e9SJeremy L Thompson           CeedCallBackend(CeedBasisApplyAtPoints(basis, num_elem, num_points, CEED_TRANSPOSE, eval_mode, impl->point_coords_elem, impl->q_vecs_out[i],
674756ca9e9SJeremy L Thompson                                                  impl->e_vecs[i + impl->num_inputs]));
675*f8a0df59SJeremy L Thompson         }
676756ca9e9SJeremy L Thompson         break;
677756ca9e9SJeremy L Thompson       // LCOV_EXCL_START
678756ca9e9SJeremy L Thompson       case CEED_EVAL_WEIGHT: {
679756ca9e9SJeremy L Thompson         return CeedError(CeedOperatorReturnCeed(op), CEED_ERROR_BACKEND, "CEED_EVAL_WEIGHT cannot be an output evaluation mode");
680756ca9e9SJeremy L Thompson         // LCOV_EXCL_STOP
681756ca9e9SJeremy L Thompson       }
682756ca9e9SJeremy L Thompson     }
683756ca9e9SJeremy L Thompson   }
684756ca9e9SJeremy L Thompson 
685756ca9e9SJeremy L Thompson   // Output restriction
686756ca9e9SJeremy L Thompson   for (CeedInt i = 0; i < num_output_fields; i++) {
687756ca9e9SJeremy L Thompson     CeedEvalMode        eval_mode;
688756ca9e9SJeremy L Thompson     CeedVector          vec;
689756ca9e9SJeremy L Thompson     CeedElemRestriction elem_rstr;
690756ca9e9SJeremy L Thompson 
691756ca9e9SJeremy L Thompson     // Restore evec
692756ca9e9SJeremy L Thompson     CeedCallBackend(CeedQFunctionFieldGetEvalMode(qf_output_fields[i], &eval_mode));
693756ca9e9SJeremy L Thompson     if (eval_mode == CEED_EVAL_NONE) {
694756ca9e9SJeremy L Thompson       CeedCallBackend(CeedVectorRestoreArray(impl->e_vecs[i + impl->num_inputs], &e_data[i + num_input_fields]));
695756ca9e9SJeremy L Thompson     }
696*f8a0df59SJeremy L Thompson     if (impl->skip_rstr_out[i]) continue;
697756ca9e9SJeremy L Thompson     // Get output vector
698756ca9e9SJeremy L Thompson     CeedCallBackend(CeedOperatorFieldGetVector(op_output_fields[i], &vec));
699756ca9e9SJeremy L Thompson     // Restrict
700756ca9e9SJeremy L Thompson     CeedCallBackend(CeedOperatorFieldGetElemRestriction(op_output_fields[i], &elem_rstr));
701756ca9e9SJeremy L Thompson     // Active
702756ca9e9SJeremy L Thompson     if (vec == CEED_VECTOR_ACTIVE) vec = out_vec;
703756ca9e9SJeremy L Thompson 
704756ca9e9SJeremy L Thompson     CeedCallBackend(CeedElemRestrictionApply(elem_rstr, CEED_TRANSPOSE, impl->e_vecs[i + impl->num_inputs], vec, request));
705756ca9e9SJeremy L Thompson   }
706756ca9e9SJeremy L Thompson 
707756ca9e9SJeremy L Thompson   // Restore input arrays
708756ca9e9SJeremy L Thompson   CeedCallBackend(CeedOperatorRestoreInputs_Cuda(num_input_fields, qf_input_fields, op_input_fields, false, e_data, impl));
709756ca9e9SJeremy L Thompson   return CEED_ERROR_SUCCESS;
710756ca9e9SJeremy L Thompson }
711756ca9e9SJeremy L Thompson 
712756ca9e9SJeremy L Thompson //------------------------------------------------------------------------------
713004e4986SSebastian Grimberg // Linear QFunction Assembly Core
7140d0321e0SJeremy L Thompson //------------------------------------------------------------------------------
7152b730f8bSJeremy L Thompson static inline int CeedOperatorLinearAssembleQFunctionCore_Cuda(CeedOperator op, bool build_objects, CeedVector *assembled, CeedElemRestriction *rstr,
7160d0321e0SJeremy L Thompson                                                                CeedRequest *request) {
717ca735530SJeremy L Thompson   Ceed                ceed, ceed_parent;
718ca735530SJeremy L Thompson   CeedInt             num_active_in, num_active_out, Q, num_elem, num_input_fields, num_output_fields, size;
719ca735530SJeremy L Thompson   CeedScalar         *assembled_array, *e_data[2 * CEED_FIELD_MAX] = {NULL};
720ca735530SJeremy L Thompson   CeedVector         *active_inputs;
721ca735530SJeremy L Thompson   CeedQFunctionField *qf_input_fields, *qf_output_fields;
722ca735530SJeremy L Thompson   CeedQFunction       qf;
723ca735530SJeremy L Thompson   CeedOperatorField  *op_input_fields, *op_output_fields;
724ca735530SJeremy L Thompson   CeedOperator_Cuda  *impl;
725ca735530SJeremy L Thompson 
7262b730f8bSJeremy L Thompson   CeedCallBackend(CeedOperatorGetCeed(op, &ceed));
727ca735530SJeremy L Thompson   CeedCallBackend(CeedOperatorGetFallbackParentCeed(op, &ceed_parent));
728e984cf9aSJeremy L Thompson   CeedCallBackend(CeedOperatorGetData(op, &impl));
729e984cf9aSJeremy L Thompson   CeedCallBackend(CeedOperatorGetNumQuadraturePoints(op, &Q));
730ca735530SJeremy L Thompson   CeedCallBackend(CeedOperatorGetNumElements(op, &num_elem));
731e984cf9aSJeremy L Thompson   CeedCallBackend(CeedOperatorGetQFunction(op, &qf));
732ca735530SJeremy L Thompson   CeedCallBackend(CeedQFunctionGetFields(qf, NULL, &qf_input_fields, NULL, &qf_output_fields));
733ca735530SJeremy L Thompson   CeedCallBackend(CeedOperatorGetFields(op, &num_input_fields, &op_input_fields, &num_output_fields, &op_output_fields));
734ca735530SJeremy L Thompson   active_inputs = impl->qf_active_in;
735ca735530SJeremy L Thompson   num_active_in = impl->num_active_in, num_active_out = impl->num_active_out;
7360d0321e0SJeremy L Thompson 
7370d0321e0SJeremy L Thompson   // Setup
7382b730f8bSJeremy L Thompson   CeedCallBackend(CeedOperatorSetup_Cuda(op));
7390d0321e0SJeremy L Thompson 
740004e4986SSebastian Grimberg   // Input Evecs and Restriction
741ca735530SJeremy L Thompson   CeedCallBackend(CeedOperatorSetupInputs_Cuda(num_input_fields, qf_input_fields, op_input_fields, NULL, true, e_data, impl, request));
7420d0321e0SJeremy L Thompson 
7430d0321e0SJeremy L Thompson   // Count number of active input fields
744ca735530SJeremy L Thompson   if (!num_active_in) {
745ca735530SJeremy L Thompson     for (CeedInt i = 0; i < num_input_fields; i++) {
746ca735530SJeremy L Thompson       CeedScalar *q_vec_array;
747ca735530SJeremy L Thompson       CeedVector  vec;
748ca735530SJeremy L Thompson 
7490d0321e0SJeremy L Thompson       // Get input vector
750ca735530SJeremy L Thompson       CeedCallBackend(CeedOperatorFieldGetVector(op_input_fields[i], &vec));
7510d0321e0SJeremy L Thompson       // Check if active input
7520d0321e0SJeremy L Thompson       if (vec == CEED_VECTOR_ACTIVE) {
753ca735530SJeremy L Thompson         CeedCallBackend(CeedQFunctionFieldGetSize(qf_input_fields[i], &size));
754ca735530SJeremy L Thompson         CeedCallBackend(CeedVectorSetValue(impl->q_vecs_in[i], 0.0));
755ca735530SJeremy L Thompson         CeedCallBackend(CeedVectorGetArray(impl->q_vecs_in[i], CEED_MEM_DEVICE, &q_vec_array));
756ca735530SJeremy L Thompson         CeedCallBackend(CeedRealloc(num_active_in + size, &active_inputs));
7570d0321e0SJeremy L Thompson         for (CeedInt field = 0; field < size; field++) {
758004e4986SSebastian Grimberg           CeedSize q_size = (CeedSize)Q * num_elem;
759004e4986SSebastian Grimberg 
760ca735530SJeremy L Thompson           CeedCallBackend(CeedVectorCreate(ceed, q_size, &active_inputs[num_active_in + field]));
761ca735530SJeremy L Thompson           CeedCallBackend(
762ca735530SJeremy L Thompson               CeedVectorSetArray(active_inputs[num_active_in + field], CEED_MEM_DEVICE, CEED_USE_POINTER, &q_vec_array[field * Q * num_elem]));
7630d0321e0SJeremy L Thompson         }
764ca735530SJeremy L Thompson         num_active_in += size;
765ca735530SJeremy L Thompson         CeedCallBackend(CeedVectorRestoreArray(impl->q_vecs_in[i], &q_vec_array));
7660d0321e0SJeremy L Thompson       }
7670d0321e0SJeremy L Thompson     }
768ca735530SJeremy L Thompson     impl->num_active_in = num_active_in;
769ca735530SJeremy L Thompson     impl->qf_active_in  = active_inputs;
7700d0321e0SJeremy L Thompson   }
7710d0321e0SJeremy L Thompson 
7720d0321e0SJeremy L Thompson   // Count number of active output fields
773ca735530SJeremy L Thompson   if (!num_active_out) {
774ca735530SJeremy L Thompson     for (CeedInt i = 0; i < num_output_fields; i++) {
775ca735530SJeremy L Thompson       CeedVector vec;
776ca735530SJeremy L Thompson 
7770d0321e0SJeremy L Thompson       // Get output vector
778ca735530SJeremy L Thompson       CeedCallBackend(CeedOperatorFieldGetVector(op_output_fields[i], &vec));
7790d0321e0SJeremy L Thompson       // Check if active output
7800d0321e0SJeremy L Thompson       if (vec == CEED_VECTOR_ACTIVE) {
781ca735530SJeremy L Thompson         CeedCallBackend(CeedQFunctionFieldGetSize(qf_output_fields[i], &size));
782ca735530SJeremy L Thompson         num_active_out += size;
7830d0321e0SJeremy L Thompson       }
7840d0321e0SJeremy L Thompson     }
785ca735530SJeremy L Thompson     impl->num_active_out = num_active_out;
7860d0321e0SJeremy L Thompson   }
7870d0321e0SJeremy L Thompson 
7880d0321e0SJeremy L Thompson   // Check sizes
789ca735530SJeremy L Thompson   CeedCheck(num_active_in > 0 && num_active_out > 0, ceed, CEED_ERROR_BACKEND, "Cannot assemble QFunction without active inputs and outputs");
7900d0321e0SJeremy L Thompson 
7910d0321e0SJeremy L Thompson   // Build objects if needed
7920d0321e0SJeremy L Thompson   if (build_objects) {
793004e4986SSebastian Grimberg     CeedSize l_size     = (CeedSize)num_elem * Q * num_active_in * num_active_out;
794ca735530SJeremy L Thompson     CeedInt  strides[3] = {1, num_elem * Q, Q}; /* *NOPAD* */
795004e4986SSebastian Grimberg 
796004e4986SSebastian Grimberg     // Create output restriction
797ca735530SJeremy L Thompson     CeedCallBackend(CeedElemRestrictionCreateStrided(ceed_parent, num_elem, Q, num_active_in * num_active_out,
7980a5597ceSJeremy L Thompson                                                      (CeedSize)num_active_in * (CeedSize)num_active_out * (CeedSize)num_elem * (CeedSize)Q, strides,
7990a5597ceSJeremy L Thompson                                                      rstr));
8000d0321e0SJeremy L Thompson     // Create assembled vector
801ca735530SJeremy L Thompson     CeedCallBackend(CeedVectorCreate(ceed_parent, l_size, assembled));
8020d0321e0SJeremy L Thompson   }
8032b730f8bSJeremy L Thompson   CeedCallBackend(CeedVectorSetValue(*assembled, 0.0));
804ca735530SJeremy L Thompson   CeedCallBackend(CeedVectorGetArray(*assembled, CEED_MEM_DEVICE, &assembled_array));
8050d0321e0SJeremy L Thompson 
8060d0321e0SJeremy L Thompson   // Input basis apply
807ca735530SJeremy L Thompson   CeedCallBackend(CeedOperatorInputBasis_Cuda(num_elem, qf_input_fields, op_input_fields, num_input_fields, true, e_data, impl));
8080d0321e0SJeremy L Thompson 
8090d0321e0SJeremy L Thompson   // Assemble QFunction
810ca735530SJeremy L Thompson   for (CeedInt in = 0; in < num_active_in; in++) {
8110d0321e0SJeremy L Thompson     // Set Inputs
812ca735530SJeremy L Thompson     CeedCallBackend(CeedVectorSetValue(active_inputs[in], 1.0));
813ca735530SJeremy L Thompson     if (num_active_in > 1) {
814ca735530SJeremy L Thompson       CeedCallBackend(CeedVectorSetValue(active_inputs[(in + num_active_in - 1) % num_active_in], 0.0));
8150d0321e0SJeremy L Thompson     }
8160d0321e0SJeremy L Thompson     // Set Outputs
817ca735530SJeremy L Thompson     for (CeedInt out = 0; out < num_output_fields; out++) {
818ca735530SJeremy L Thompson       CeedVector vec;
819ca735530SJeremy L Thompson 
8200d0321e0SJeremy L Thompson       // Get output vector
821ca735530SJeremy L Thompson       CeedCallBackend(CeedOperatorFieldGetVector(op_output_fields[out], &vec));
8220d0321e0SJeremy L Thompson       // Check if active output
8230d0321e0SJeremy L Thompson       if (vec == CEED_VECTOR_ACTIVE) {
824ca735530SJeremy L Thompson         CeedCallBackend(CeedVectorSetArray(impl->q_vecs_out[out], CEED_MEM_DEVICE, CEED_USE_POINTER, assembled_array));
825ca735530SJeremy L Thompson         CeedCallBackend(CeedQFunctionFieldGetSize(qf_output_fields[out], &size));
826ca735530SJeremy L Thompson         assembled_array += size * Q * num_elem;  // Advance the pointer by the size of the output
8270d0321e0SJeremy L Thompson       }
8280d0321e0SJeremy L Thompson     }
8290d0321e0SJeremy L Thompson     // Apply QFunction
830ca735530SJeremy L Thompson     CeedCallBackend(CeedQFunctionApply(qf, Q * num_elem, impl->q_vecs_in, impl->q_vecs_out));
8310d0321e0SJeremy L Thompson   }
8320d0321e0SJeremy L Thompson 
833ca735530SJeremy L Thompson   // Un-set output q_vecs to prevent accidental overwrite of Assembled
834ca735530SJeremy L Thompson   for (CeedInt out = 0; out < num_output_fields; out++) {
835ca735530SJeremy L Thompson     CeedVector vec;
836ca735530SJeremy L Thompson 
8370d0321e0SJeremy L Thompson     // Get output vector
838ca735530SJeremy L Thompson     CeedCallBackend(CeedOperatorFieldGetVector(op_output_fields[out], &vec));
8390d0321e0SJeremy L Thompson     // Check if active output
8400d0321e0SJeremy L Thompson     if (vec == CEED_VECTOR_ACTIVE) {
841ca735530SJeremy L Thompson       CeedCallBackend(CeedVectorTakeArray(impl->q_vecs_out[out], CEED_MEM_DEVICE, NULL));
8420d0321e0SJeremy L Thompson     }
8430d0321e0SJeremy L Thompson   }
8440d0321e0SJeremy L Thompson 
8450d0321e0SJeremy L Thompson   // Restore input arrays
846ca735530SJeremy L Thompson   CeedCallBackend(CeedOperatorRestoreInputs_Cuda(num_input_fields, qf_input_fields, op_input_fields, true, e_data, impl));
8470d0321e0SJeremy L Thompson 
8480d0321e0SJeremy L Thompson   // Restore output
849ca735530SJeremy L Thompson   CeedCallBackend(CeedVectorRestoreArray(*assembled, &assembled_array));
8500d0321e0SJeremy L Thompson   return CEED_ERROR_SUCCESS;
8510d0321e0SJeremy L Thompson }
8520d0321e0SJeremy L Thompson 
8530d0321e0SJeremy L Thompson //------------------------------------------------------------------------------
8540d0321e0SJeremy L Thompson // Assemble Linear QFunction
8550d0321e0SJeremy L Thompson //------------------------------------------------------------------------------
8562b730f8bSJeremy L Thompson static int CeedOperatorLinearAssembleQFunction_Cuda(CeedOperator op, CeedVector *assembled, CeedElemRestriction *rstr, CeedRequest *request) {
8572b730f8bSJeremy L Thompson   return CeedOperatorLinearAssembleQFunctionCore_Cuda(op, true, assembled, rstr, request);
8580d0321e0SJeremy L Thompson }
8590d0321e0SJeremy L Thompson 
8600d0321e0SJeremy L Thompson //------------------------------------------------------------------------------
8610d0321e0SJeremy L Thompson // Update Assembled Linear QFunction
8620d0321e0SJeremy L Thompson //------------------------------------------------------------------------------
8632b730f8bSJeremy L Thompson static int CeedOperatorLinearAssembleQFunctionUpdate_Cuda(CeedOperator op, CeedVector assembled, CeedElemRestriction rstr, CeedRequest *request) {
8642b730f8bSJeremy L Thompson   return CeedOperatorLinearAssembleQFunctionCore_Cuda(op, false, &assembled, &rstr, request);
8650d0321e0SJeremy L Thompson }
8660d0321e0SJeremy L Thompson 
8670d0321e0SJeremy L Thompson //------------------------------------------------------------------------------
868004e4986SSebastian Grimberg // Assemble Diagonal Setup
8690d0321e0SJeremy L Thompson //------------------------------------------------------------------------------
870cbfe683aSSebastian Grimberg static inline int CeedOperatorAssembleDiagonalSetup_Cuda(CeedOperator op) {
8710d0321e0SJeremy L Thompson   Ceed                ceed;
872004e4986SSebastian Grimberg   CeedInt             num_input_fields, num_output_fields, num_eval_modes_in = 0, num_eval_modes_out = 0;
873cbfe683aSSebastian Grimberg   CeedInt             q_comp, num_nodes, num_qpts;
874004e4986SSebastian Grimberg   CeedEvalMode       *eval_modes_in = NULL, *eval_modes_out = NULL;
875ca735530SJeremy L Thompson   CeedBasis           basis_in = NULL, basis_out = NULL;
876ca735530SJeremy L Thompson   CeedQFunctionField *qf_fields;
8770d0321e0SJeremy L Thompson   CeedQFunction       qf;
878ca735530SJeremy L Thompson   CeedOperatorField  *op_fields;
879ca735530SJeremy L Thompson   CeedOperator_Cuda  *impl;
880ca735530SJeremy L Thompson 
881ca735530SJeremy L Thompson   CeedCallBackend(CeedOperatorGetCeed(op, &ceed));
8822b730f8bSJeremy L Thompson   CeedCallBackend(CeedOperatorGetQFunction(op, &qf));
883ca735530SJeremy L Thompson   CeedCallBackend(CeedQFunctionGetNumArgs(qf, &num_input_fields, &num_output_fields));
8840d0321e0SJeremy L Thompson 
8850d0321e0SJeremy L Thompson   // Determine active input basis
886ca735530SJeremy L Thompson   CeedCallBackend(CeedOperatorGetFields(op, NULL, &op_fields, NULL, NULL));
887ca735530SJeremy L Thompson   CeedCallBackend(CeedQFunctionGetFields(qf, NULL, &qf_fields, NULL, NULL));
888ca735530SJeremy L Thompson   for (CeedInt i = 0; i < num_input_fields; i++) {
8890d0321e0SJeremy L Thompson     CeedVector vec;
890ca735530SJeremy L Thompson 
891ca735530SJeremy L Thompson     CeedCallBackend(CeedOperatorFieldGetVector(op_fields[i], &vec));
8920d0321e0SJeremy L Thompson     if (vec == CEED_VECTOR_ACTIVE) {
893004e4986SSebastian Grimberg       CeedBasis    basis;
894004e4986SSebastian Grimberg       CeedEvalMode eval_mode;
895ca735530SJeremy L Thompson 
896004e4986SSebastian Grimberg       CeedCallBackend(CeedOperatorFieldGetBasis(op_fields[i], &basis));
897004e4986SSebastian Grimberg       CeedCheck(!basis_in || basis_in == basis, ceed, CEED_ERROR_BACKEND,
898004e4986SSebastian Grimberg                 "Backend does not implement operator diagonal assembly with multiple active bases");
899004e4986SSebastian Grimberg       basis_in = basis;
900004e4986SSebastian Grimberg       CeedCallBackend(CeedQFunctionFieldGetEvalMode(qf_fields[i], &eval_mode));
901004e4986SSebastian Grimberg       CeedCallBackend(CeedBasisGetNumQuadratureComponents(basis_in, eval_mode, &q_comp));
902004e4986SSebastian Grimberg       if (eval_mode != CEED_EVAL_WEIGHT) {
903004e4986SSebastian Grimberg         // q_comp = 1 if CEED_EVAL_NONE, CEED_EVAL_WEIGHT caught by QF assembly
904004e4986SSebastian Grimberg         CeedCallBackend(CeedRealloc(num_eval_modes_in + q_comp, &eval_modes_in));
905004e4986SSebastian Grimberg         for (CeedInt d = 0; d < q_comp; d++) eval_modes_in[num_eval_modes_in + d] = eval_mode;
906004e4986SSebastian Grimberg         num_eval_modes_in += q_comp;
9070d0321e0SJeremy L Thompson       }
9080d0321e0SJeremy L Thompson     }
9090d0321e0SJeremy L Thompson   }
9100d0321e0SJeremy L Thompson 
9110d0321e0SJeremy L Thompson   // Determine active output basis
912ca735530SJeremy L Thompson   CeedCallBackend(CeedOperatorGetFields(op, NULL, NULL, NULL, &op_fields));
913ca735530SJeremy L Thompson   CeedCallBackend(CeedQFunctionGetFields(qf, NULL, NULL, NULL, &qf_fields));
914ca735530SJeremy L Thompson   for (CeedInt i = 0; i < num_output_fields; i++) {
9150d0321e0SJeremy L Thompson     CeedVector vec;
916ca735530SJeremy L Thompson 
917ca735530SJeremy L Thompson     CeedCallBackend(CeedOperatorFieldGetVector(op_fields[i], &vec));
9180d0321e0SJeremy L Thompson     if (vec == CEED_VECTOR_ACTIVE) {
919004e4986SSebastian Grimberg       CeedBasis    basis;
920004e4986SSebastian Grimberg       CeedEvalMode eval_mode;
921ca735530SJeremy L Thompson 
922004e4986SSebastian Grimberg       CeedCallBackend(CeedOperatorFieldGetBasis(op_fields[i], &basis));
923004e4986SSebastian Grimberg       CeedCheck(!basis_out || basis_out == basis, ceed, CEED_ERROR_BACKEND,
924004e4986SSebastian Grimberg                 "Backend does not implement operator diagonal assembly with multiple active bases");
925004e4986SSebastian Grimberg       basis_out = basis;
926004e4986SSebastian Grimberg       CeedCallBackend(CeedQFunctionFieldGetEvalMode(qf_fields[i], &eval_mode));
927004e4986SSebastian Grimberg       CeedCallBackend(CeedBasisGetNumQuadratureComponents(basis_out, eval_mode, &q_comp));
928004e4986SSebastian Grimberg       if (eval_mode != CEED_EVAL_WEIGHT) {
929004e4986SSebastian Grimberg         // q_comp = 1 if CEED_EVAL_NONE, CEED_EVAL_WEIGHT caught by QF assembly
930004e4986SSebastian Grimberg         CeedCallBackend(CeedRealloc(num_eval_modes_out + q_comp, &eval_modes_out));
931004e4986SSebastian Grimberg         for (CeedInt d = 0; d < q_comp; d++) eval_modes_out[num_eval_modes_out + d] = eval_mode;
932004e4986SSebastian Grimberg         num_eval_modes_out += q_comp;
9330d0321e0SJeremy L Thompson       }
9340d0321e0SJeremy L Thompson     }
9350d0321e0SJeremy L Thompson   }
9360d0321e0SJeremy L Thompson 
9370d0321e0SJeremy L Thompson   // Operator data struct
9382b730f8bSJeremy L Thompson   CeedCallBackend(CeedOperatorGetData(op, &impl));
9392b730f8bSJeremy L Thompson   CeedCallBackend(CeedCalloc(1, &impl->diag));
9400d0321e0SJeremy L Thompson   CeedOperatorDiag_Cuda *diag = impl->diag;
941ca735530SJeremy L Thompson 
942cbfe683aSSebastian Grimberg   // Basis matrices
943004e4986SSebastian Grimberg   CeedCallBackend(CeedBasisGetNumNodes(basis_in, &num_nodes));
944004e4986SSebastian Grimberg   if (basis_in == CEED_BASIS_NONE) num_qpts = num_nodes;
945004e4986SSebastian Grimberg   else CeedCallBackend(CeedBasisGetNumQuadraturePoints(basis_in, &num_qpts));
946004e4986SSebastian Grimberg   const CeedInt interp_bytes     = num_nodes * num_qpts * sizeof(CeedScalar);
947004e4986SSebastian Grimberg   const CeedInt eval_modes_bytes = sizeof(CeedEvalMode);
948004e4986SSebastian Grimberg   bool          has_eval_none    = false;
9490d0321e0SJeremy L Thompson 
9500d0321e0SJeremy L Thompson   // CEED_EVAL_NONE
951004e4986SSebastian Grimberg   for (CeedInt i = 0; i < num_eval_modes_in; i++) has_eval_none = has_eval_none || (eval_modes_in[i] == CEED_EVAL_NONE);
952004e4986SSebastian Grimberg   for (CeedInt i = 0; i < num_eval_modes_out; i++) has_eval_none = has_eval_none || (eval_modes_out[i] == CEED_EVAL_NONE);
953004e4986SSebastian Grimberg   if (has_eval_none) {
9540d0321e0SJeremy L Thompson     CeedScalar *identity = NULL;
955ca735530SJeremy L Thompson 
956004e4986SSebastian Grimberg     CeedCallBackend(CeedCalloc(num_nodes * num_qpts, &identity));
957ca735530SJeremy L Thompson     for (CeedInt i = 0; i < (num_nodes < num_qpts ? num_nodes : num_qpts); i++) identity[i * num_nodes + i] = 1.0;
958ca735530SJeremy L Thompson     CeedCallCuda(ceed, cudaMalloc((void **)&diag->d_identity, interp_bytes));
959ca735530SJeremy L Thompson     CeedCallCuda(ceed, cudaMemcpy(diag->d_identity, identity, interp_bytes, cudaMemcpyHostToDevice));
960004e4986SSebastian Grimberg     CeedCallBackend(CeedFree(&identity));
9610d0321e0SJeremy L Thompson   }
9620d0321e0SJeremy L Thompson 
963004e4986SSebastian Grimberg   // CEED_EVAL_INTERP, CEED_EVAL_GRAD, CEED_EVAL_DIV, and CEED_EVAL_CURL
964004e4986SSebastian Grimberg   for (CeedInt in = 0; in < 2; in++) {
965004e4986SSebastian Grimberg     CeedFESpace fespace;
966004e4986SSebastian Grimberg     CeedBasis   basis = in ? basis_in : basis_out;
9670d0321e0SJeremy L Thompson 
968004e4986SSebastian Grimberg     CeedCallBackend(CeedBasisGetFESpace(basis, &fespace));
969004e4986SSebastian Grimberg     switch (fespace) {
970004e4986SSebastian Grimberg       case CEED_FE_SPACE_H1: {
971004e4986SSebastian Grimberg         CeedInt           q_comp_interp, q_comp_grad;
972004e4986SSebastian Grimberg         const CeedScalar *interp, *grad;
973004e4986SSebastian Grimberg         CeedScalar       *d_interp, *d_grad;
9740d0321e0SJeremy L Thompson 
975004e4986SSebastian Grimberg         CeedCallBackend(CeedBasisGetNumQuadratureComponents(basis, CEED_EVAL_INTERP, &q_comp_interp));
976004e4986SSebastian Grimberg         CeedCallBackend(CeedBasisGetNumQuadratureComponents(basis, CEED_EVAL_GRAD, &q_comp_grad));
9770d0321e0SJeremy L Thompson 
978004e4986SSebastian Grimberg         CeedCallBackend(CeedBasisGetInterp(basis, &interp));
979004e4986SSebastian Grimberg         CeedCallCuda(ceed, cudaMalloc((void **)&d_interp, interp_bytes * q_comp_interp));
980004e4986SSebastian Grimberg         CeedCallCuda(ceed, cudaMemcpy(d_interp, interp, interp_bytes * q_comp_interp, cudaMemcpyHostToDevice));
981004e4986SSebastian Grimberg         CeedCallBackend(CeedBasisGetGrad(basis, &grad));
982004e4986SSebastian Grimberg         CeedCallCuda(ceed, cudaMalloc((void **)&d_grad, interp_bytes * q_comp_grad));
983004e4986SSebastian Grimberg         CeedCallCuda(ceed, cudaMemcpy(d_grad, grad, interp_bytes * q_comp_grad, cudaMemcpyHostToDevice));
984004e4986SSebastian Grimberg         if (in) {
985004e4986SSebastian Grimberg           diag->d_interp_in = d_interp;
986004e4986SSebastian Grimberg           diag->d_grad_in   = d_grad;
987004e4986SSebastian Grimberg         } else {
988004e4986SSebastian Grimberg           diag->d_interp_out = d_interp;
989004e4986SSebastian Grimberg           diag->d_grad_out   = d_grad;
990004e4986SSebastian Grimberg         }
991004e4986SSebastian Grimberg       } break;
992004e4986SSebastian Grimberg       case CEED_FE_SPACE_HDIV: {
993004e4986SSebastian Grimberg         CeedInt           q_comp_interp, q_comp_div;
994004e4986SSebastian Grimberg         const CeedScalar *interp, *div;
995004e4986SSebastian Grimberg         CeedScalar       *d_interp, *d_div;
996004e4986SSebastian Grimberg 
997004e4986SSebastian Grimberg         CeedCallBackend(CeedBasisGetNumQuadratureComponents(basis, CEED_EVAL_INTERP, &q_comp_interp));
998004e4986SSebastian Grimberg         CeedCallBackend(CeedBasisGetNumQuadratureComponents(basis, CEED_EVAL_DIV, &q_comp_div));
999004e4986SSebastian Grimberg 
1000004e4986SSebastian Grimberg         CeedCallBackend(CeedBasisGetInterp(basis, &interp));
1001004e4986SSebastian Grimberg         CeedCallCuda(ceed, cudaMalloc((void **)&d_interp, interp_bytes * q_comp_interp));
1002004e4986SSebastian Grimberg         CeedCallCuda(ceed, cudaMemcpy(d_interp, interp, interp_bytes * q_comp_interp, cudaMemcpyHostToDevice));
1003004e4986SSebastian Grimberg         CeedCallBackend(CeedBasisGetDiv(basis, &div));
1004004e4986SSebastian Grimberg         CeedCallCuda(ceed, cudaMalloc((void **)&d_div, interp_bytes * q_comp_div));
1005004e4986SSebastian Grimberg         CeedCallCuda(ceed, cudaMemcpy(d_div, div, interp_bytes * q_comp_div, cudaMemcpyHostToDevice));
1006004e4986SSebastian Grimberg         if (in) {
1007004e4986SSebastian Grimberg           diag->d_interp_in = d_interp;
1008004e4986SSebastian Grimberg           diag->d_div_in    = d_div;
1009004e4986SSebastian Grimberg         } else {
1010004e4986SSebastian Grimberg           diag->d_interp_out = d_interp;
1011004e4986SSebastian Grimberg           diag->d_div_out    = d_div;
1012004e4986SSebastian Grimberg         }
1013004e4986SSebastian Grimberg       } break;
1014004e4986SSebastian Grimberg       case CEED_FE_SPACE_HCURL: {
1015004e4986SSebastian Grimberg         CeedInt           q_comp_interp, q_comp_curl;
1016004e4986SSebastian Grimberg         const CeedScalar *interp, *curl;
1017004e4986SSebastian Grimberg         CeedScalar       *d_interp, *d_curl;
1018004e4986SSebastian Grimberg 
1019004e4986SSebastian Grimberg         CeedCallBackend(CeedBasisGetNumQuadratureComponents(basis, CEED_EVAL_INTERP, &q_comp_interp));
1020004e4986SSebastian Grimberg         CeedCallBackend(CeedBasisGetNumQuadratureComponents(basis, CEED_EVAL_CURL, &q_comp_curl));
1021004e4986SSebastian Grimberg 
1022004e4986SSebastian Grimberg         CeedCallBackend(CeedBasisGetInterp(basis, &interp));
1023004e4986SSebastian Grimberg         CeedCallCuda(ceed, cudaMalloc((void **)&d_interp, interp_bytes * q_comp_interp));
1024004e4986SSebastian Grimberg         CeedCallCuda(ceed, cudaMemcpy(d_interp, interp, interp_bytes * q_comp_interp, cudaMemcpyHostToDevice));
1025004e4986SSebastian Grimberg         CeedCallBackend(CeedBasisGetCurl(basis, &curl));
1026004e4986SSebastian Grimberg         CeedCallCuda(ceed, cudaMalloc((void **)&d_curl, interp_bytes * q_comp_curl));
1027004e4986SSebastian Grimberg         CeedCallCuda(ceed, cudaMemcpy(d_curl, curl, interp_bytes * q_comp_curl, cudaMemcpyHostToDevice));
1028004e4986SSebastian Grimberg         if (in) {
1029004e4986SSebastian Grimberg           diag->d_interp_in = d_interp;
1030004e4986SSebastian Grimberg           diag->d_curl_in   = d_curl;
1031004e4986SSebastian Grimberg         } else {
1032004e4986SSebastian Grimberg           diag->d_interp_out = d_interp;
1033004e4986SSebastian Grimberg           diag->d_curl_out   = d_curl;
1034004e4986SSebastian Grimberg         }
1035004e4986SSebastian Grimberg       } break;
1036004e4986SSebastian Grimberg     }
1037004e4986SSebastian Grimberg   }
1038004e4986SSebastian Grimberg 
1039004e4986SSebastian Grimberg   // Arrays of eval_modes
1040004e4986SSebastian Grimberg   CeedCallCuda(ceed, cudaMalloc((void **)&diag->d_eval_modes_in, num_eval_modes_in * eval_modes_bytes));
1041004e4986SSebastian Grimberg   CeedCallCuda(ceed, cudaMemcpy(diag->d_eval_modes_in, eval_modes_in, num_eval_modes_in * eval_modes_bytes, cudaMemcpyHostToDevice));
1042004e4986SSebastian Grimberg   CeedCallCuda(ceed, cudaMalloc((void **)&diag->d_eval_modes_out, num_eval_modes_out * eval_modes_bytes));
1043004e4986SSebastian Grimberg   CeedCallCuda(ceed, cudaMemcpy(diag->d_eval_modes_out, eval_modes_out, num_eval_modes_out * eval_modes_bytes, cudaMemcpyHostToDevice));
1044004e4986SSebastian Grimberg   CeedCallBackend(CeedFree(&eval_modes_in));
1045004e4986SSebastian Grimberg   CeedCallBackend(CeedFree(&eval_modes_out));
10460d0321e0SJeremy L Thompson   return CEED_ERROR_SUCCESS;
10470d0321e0SJeremy L Thompson }
10480d0321e0SJeremy L Thompson 
10490d0321e0SJeremy L Thompson //------------------------------------------------------------------------------
1050cbfe683aSSebastian Grimberg // Assemble Diagonal Setup (Compilation)
1051cbfe683aSSebastian Grimberg //------------------------------------------------------------------------------
1052cbfe683aSSebastian Grimberg static inline int CeedOperatorAssembleDiagonalSetupCompile_Cuda(CeedOperator op, CeedInt use_ceedsize_idx, const bool is_point_block) {
1053cbfe683aSSebastian Grimberg   Ceed                ceed;
105422070f95SJeremy L Thompson   char               *diagonal_kernel_source;
105522070f95SJeremy L Thompson   const char         *diagonal_kernel_path;
1056cbfe683aSSebastian Grimberg   CeedInt             num_input_fields, num_output_fields, num_eval_modes_in = 0, num_eval_modes_out = 0;
1057cbfe683aSSebastian Grimberg   CeedInt             num_comp, q_comp, num_nodes, num_qpts;
1058cbfe683aSSebastian Grimberg   CeedBasis           basis_in = NULL, basis_out = NULL;
1059cbfe683aSSebastian Grimberg   CeedQFunctionField *qf_fields;
1060cbfe683aSSebastian Grimberg   CeedQFunction       qf;
1061cbfe683aSSebastian Grimberg   CeedOperatorField  *op_fields;
1062cbfe683aSSebastian Grimberg   CeedOperator_Cuda  *impl;
1063cbfe683aSSebastian Grimberg 
1064cbfe683aSSebastian Grimberg   CeedCallBackend(CeedOperatorGetCeed(op, &ceed));
1065cbfe683aSSebastian Grimberg   CeedCallBackend(CeedOperatorGetQFunction(op, &qf));
1066cbfe683aSSebastian Grimberg   CeedCallBackend(CeedQFunctionGetNumArgs(qf, &num_input_fields, &num_output_fields));
1067cbfe683aSSebastian Grimberg 
1068cbfe683aSSebastian Grimberg   // Determine active input basis
1069cbfe683aSSebastian Grimberg   CeedCallBackend(CeedOperatorGetFields(op, NULL, &op_fields, NULL, NULL));
1070cbfe683aSSebastian Grimberg   CeedCallBackend(CeedQFunctionGetFields(qf, NULL, &qf_fields, NULL, NULL));
1071cbfe683aSSebastian Grimberg   for (CeedInt i = 0; i < num_input_fields; i++) {
1072cbfe683aSSebastian Grimberg     CeedVector vec;
1073cbfe683aSSebastian Grimberg 
1074cbfe683aSSebastian Grimberg     CeedCallBackend(CeedOperatorFieldGetVector(op_fields[i], &vec));
1075cbfe683aSSebastian Grimberg     if (vec == CEED_VECTOR_ACTIVE) {
1076cbfe683aSSebastian Grimberg       CeedEvalMode eval_mode;
1077cbfe683aSSebastian Grimberg 
1078cbfe683aSSebastian Grimberg       CeedCallBackend(CeedOperatorFieldGetBasis(op_fields[i], &basis_in));
1079cbfe683aSSebastian Grimberg       CeedCallBackend(CeedQFunctionFieldGetEvalMode(qf_fields[i], &eval_mode));
1080cbfe683aSSebastian Grimberg       CeedCallBackend(CeedBasisGetNumQuadratureComponents(basis_in, eval_mode, &q_comp));
1081cbfe683aSSebastian Grimberg       if (eval_mode != CEED_EVAL_WEIGHT) {
1082cbfe683aSSebastian Grimberg         num_eval_modes_in += q_comp;
1083cbfe683aSSebastian Grimberg       }
1084cbfe683aSSebastian Grimberg     }
1085cbfe683aSSebastian Grimberg   }
1086cbfe683aSSebastian Grimberg 
1087cbfe683aSSebastian Grimberg   // Determine active output basis
1088cbfe683aSSebastian Grimberg   CeedCallBackend(CeedOperatorGetFields(op, NULL, NULL, NULL, &op_fields));
1089cbfe683aSSebastian Grimberg   CeedCallBackend(CeedQFunctionGetFields(qf, NULL, NULL, NULL, &qf_fields));
1090cbfe683aSSebastian Grimberg   for (CeedInt i = 0; i < num_output_fields; i++) {
1091cbfe683aSSebastian Grimberg     CeedVector vec;
1092cbfe683aSSebastian Grimberg 
1093cbfe683aSSebastian Grimberg     CeedCallBackend(CeedOperatorFieldGetVector(op_fields[i], &vec));
1094cbfe683aSSebastian Grimberg     if (vec == CEED_VECTOR_ACTIVE) {
1095cbfe683aSSebastian Grimberg       CeedEvalMode eval_mode;
1096cbfe683aSSebastian Grimberg 
1097cbfe683aSSebastian Grimberg       CeedCallBackend(CeedOperatorFieldGetBasis(op_fields[i], &basis_out));
1098cbfe683aSSebastian Grimberg       CeedCallBackend(CeedQFunctionFieldGetEvalMode(qf_fields[i], &eval_mode));
1099cbfe683aSSebastian Grimberg       CeedCallBackend(CeedBasisGetNumQuadratureComponents(basis_out, eval_mode, &q_comp));
1100cbfe683aSSebastian Grimberg       if (eval_mode != CEED_EVAL_WEIGHT) {
1101cbfe683aSSebastian Grimberg         num_eval_modes_out += q_comp;
1102cbfe683aSSebastian Grimberg       }
1103cbfe683aSSebastian Grimberg     }
1104cbfe683aSSebastian Grimberg   }
1105cbfe683aSSebastian Grimberg 
1106cbfe683aSSebastian Grimberg   // Operator data struct
1107cbfe683aSSebastian Grimberg   CeedCallBackend(CeedOperatorGetData(op, &impl));
1108cbfe683aSSebastian Grimberg   CeedOperatorDiag_Cuda *diag = impl->diag;
1109cbfe683aSSebastian Grimberg 
1110cbfe683aSSebastian Grimberg   // Assemble kernel
1111cbfe683aSSebastian Grimberg   CUmodule *module          = is_point_block ? &diag->module_point_block : &diag->module;
1112cbfe683aSSebastian Grimberg   CeedInt   elems_per_block = 1;
1113cbfe683aSSebastian Grimberg   CeedCallBackend(CeedBasisGetNumNodes(basis_in, &num_nodes));
1114cbfe683aSSebastian Grimberg   CeedCallBackend(CeedBasisGetNumComponents(basis_in, &num_comp));
1115cbfe683aSSebastian Grimberg   if (basis_in == CEED_BASIS_NONE) num_qpts = num_nodes;
1116cbfe683aSSebastian Grimberg   else CeedCallBackend(CeedBasisGetNumQuadraturePoints(basis_in, &num_qpts));
1117cbfe683aSSebastian Grimberg   CeedCallBackend(CeedGetJitAbsolutePath(ceed, "ceed/jit-source/cuda/cuda-ref-operator-assemble-diagonal.h", &diagonal_kernel_path));
1118cbfe683aSSebastian Grimberg   CeedDebug256(ceed, CEED_DEBUG_COLOR_SUCCESS, "----- Loading Diagonal Assembly Kernel Source -----\n");
1119cbfe683aSSebastian Grimberg   CeedCallBackend(CeedLoadSourceToBuffer(ceed, diagonal_kernel_path, &diagonal_kernel_source));
1120cbfe683aSSebastian Grimberg   CeedDebug256(ceed, CEED_DEBUG_COLOR_SUCCESS, "----- Loading Diagonal Assembly Source Complete! -----\n");
1121cbfe683aSSebastian Grimberg   CeedCallCuda(ceed, CeedCompile_Cuda(ceed, diagonal_kernel_source, module, 8, "NUM_EVAL_MODES_IN", num_eval_modes_in, "NUM_EVAL_MODES_OUT",
1122cbfe683aSSebastian Grimberg                                       num_eval_modes_out, "NUM_COMP", num_comp, "NUM_NODES", num_nodes, "NUM_QPTS", num_qpts, "USE_CEEDSIZE",
1123cbfe683aSSebastian Grimberg                                       use_ceedsize_idx, "USE_POINT_BLOCK", is_point_block ? 1 : 0, "BLOCK_SIZE", num_nodes * elems_per_block));
1124cbfe683aSSebastian Grimberg   CeedCallCuda(ceed, CeedGetKernel_Cuda(ceed, *module, "LinearDiagonal", is_point_block ? &diag->LinearPointBlock : &diag->LinearDiagonal));
1125cbfe683aSSebastian Grimberg   CeedCallBackend(CeedFree(&diagonal_kernel_path));
1126cbfe683aSSebastian Grimberg   CeedCallBackend(CeedFree(&diagonal_kernel_source));
1127cbfe683aSSebastian Grimberg   return CEED_ERROR_SUCCESS;
1128cbfe683aSSebastian Grimberg }
1129cbfe683aSSebastian Grimberg 
1130cbfe683aSSebastian Grimberg //------------------------------------------------------------------------------
1131004e4986SSebastian Grimberg // Assemble Diagonal Core
11320d0321e0SJeremy L Thompson //------------------------------------------------------------------------------
1133ca735530SJeremy L Thompson static inline int CeedOperatorAssembleDiagonalCore_Cuda(CeedOperator op, CeedVector assembled, CeedRequest *request, const bool is_point_block) {
11340d0321e0SJeremy L Thompson   Ceed                ceed;
1135cbfe683aSSebastian Grimberg   CeedInt             num_elem, num_nodes;
1136ca735530SJeremy L Thompson   CeedScalar         *elem_diag_array;
1137ca735530SJeremy L Thompson   const CeedScalar   *assembled_qf_array;
1138004e4986SSebastian Grimberg   CeedVector          assembled_qf   = NULL, elem_diag;
1139004e4986SSebastian Grimberg   CeedElemRestriction assembled_rstr = NULL, rstr_in, rstr_out, diag_rstr;
11400d0321e0SJeremy L Thompson   CeedOperator_Cuda  *impl;
1141ca735530SJeremy L Thompson 
1142ca735530SJeremy L Thompson   CeedCallBackend(CeedOperatorGetCeed(op, &ceed));
11432b730f8bSJeremy L Thompson   CeedCallBackend(CeedOperatorGetData(op, &impl));
11440d0321e0SJeremy L Thompson 
11450d0321e0SJeremy L Thompson   // Assemble QFunction
1146004e4986SSebastian Grimberg   CeedCallBackend(CeedOperatorLinearAssembleQFunctionBuildOrUpdate(op, &assembled_qf, &assembled_rstr, request));
1147004e4986SSebastian Grimberg   CeedCallBackend(CeedElemRestrictionDestroy(&assembled_rstr));
1148004e4986SSebastian Grimberg   CeedCallBackend(CeedVectorGetArrayRead(assembled_qf, CEED_MEM_DEVICE, &assembled_qf_array));
11490d0321e0SJeremy L Thompson 
1150cbfe683aSSebastian Grimberg   // Setup
1151cbfe683aSSebastian Grimberg   if (!impl->diag) CeedCallBackend(CeedOperatorAssembleDiagonalSetup_Cuda(op));
1152cbfe683aSSebastian Grimberg   CeedOperatorDiag_Cuda *diag = impl->diag;
1153cbfe683aSSebastian Grimberg 
1154cbfe683aSSebastian Grimberg   assert(diag != NULL);
1155cbfe683aSSebastian Grimberg 
1156cbfe683aSSebastian Grimberg   // Assemble kernel if needed
1157cbfe683aSSebastian Grimberg   if ((!is_point_block && !diag->LinearDiagonal) || (is_point_block && !diag->LinearPointBlock)) {
1158cbfe683aSSebastian Grimberg     CeedSize assembled_length, assembled_qf_length;
1159cbfe683aSSebastian Grimberg     CeedInt  use_ceedsize_idx = 0;
1160f7c1b517Snbeams     CeedCallBackend(CeedVectorGetLength(assembled, &assembled_length));
1161ca735530SJeremy L Thompson     CeedCallBackend(CeedVectorGetLength(assembled_qf, &assembled_qf_length));
1162ca735530SJeremy L Thompson     if ((assembled_length > INT_MAX) || (assembled_qf_length > INT_MAX)) use_ceedsize_idx = 1;
1163f7c1b517Snbeams 
1164cbfe683aSSebastian Grimberg     CeedCallBackend(CeedOperatorAssembleDiagonalSetupCompile_Cuda(op, use_ceedsize_idx, is_point_block));
1165cbfe683aSSebastian Grimberg   }
11660d0321e0SJeremy L Thompson 
1167004e4986SSebastian Grimberg   // Restriction and diagonal vector
1168004e4986SSebastian Grimberg   CeedCallBackend(CeedOperatorGetActiveElemRestrictions(op, &rstr_in, &rstr_out));
1169004e4986SSebastian Grimberg   CeedCheck(rstr_in == rstr_out, ceed, CEED_ERROR_BACKEND,
1170004e4986SSebastian Grimberg             "Cannot assemble operator diagonal with different input and output active element restrictions");
1171004e4986SSebastian Grimberg   if (!is_point_block && !diag->diag_rstr) {
1172004e4986SSebastian Grimberg     CeedCallBackend(CeedElemRestrictionCreateUnsignedCopy(rstr_out, &diag->diag_rstr));
1173004e4986SSebastian Grimberg     CeedCallBackend(CeedElemRestrictionCreateVector(diag->diag_rstr, NULL, &diag->elem_diag));
1174004e4986SSebastian Grimberg   } else if (is_point_block && !diag->point_block_diag_rstr) {
1175004e4986SSebastian Grimberg     CeedCallBackend(CeedOperatorCreateActivePointBlockRestriction(rstr_out, &diag->point_block_diag_rstr));
1176004e4986SSebastian Grimberg     CeedCallBackend(CeedElemRestrictionCreateVector(diag->point_block_diag_rstr, NULL, &diag->point_block_elem_diag));
11770d0321e0SJeremy L Thompson   }
1178004e4986SSebastian Grimberg   diag_rstr = is_point_block ? diag->point_block_diag_rstr : diag->diag_rstr;
1179004e4986SSebastian Grimberg   elem_diag = is_point_block ? diag->point_block_elem_diag : diag->elem_diag;
1180ca735530SJeremy L Thompson   CeedCallBackend(CeedVectorSetValue(elem_diag, 0.0));
11810d0321e0SJeremy L Thompson 
118291db28b6SZach Atkins   // Only assemble diagonal if the basis has nodes, otherwise inputs are null pointers
1183004e4986SSebastian Grimberg   CeedCallBackend(CeedElemRestrictionGetElementSize(diag_rstr, &num_nodes));
1184004e4986SSebastian Grimberg   if (num_nodes > 0) {
11850d0321e0SJeremy L Thompson     // Assemble element operator diagonals
1186ca735530SJeremy L Thompson     CeedCallBackend(CeedElemRestrictionGetNumElements(diag_rstr, &num_elem));
1187004e4986SSebastian Grimberg     CeedCallBackend(CeedVectorGetArray(elem_diag, CEED_MEM_DEVICE, &elem_diag_array));
11880d0321e0SJeremy L Thompson 
11890d0321e0SJeremy L Thompson     // Compute the diagonal of B^T D B
1190004e4986SSebastian Grimberg     CeedInt elems_per_block = 1;
1191004e4986SSebastian Grimberg     CeedInt grid            = CeedDivUpInt(num_elem, elems_per_block);
1192004e4986SSebastian Grimberg     void   *args[]          = {(void *)&num_elem,      &diag->d_identity,       &diag->d_interp_in,  &diag->d_grad_in, &diag->d_div_in,
1193004e4986SSebastian Grimberg                                &diag->d_curl_in,       &diag->d_interp_out,     &diag->d_grad_out,   &diag->d_div_out, &diag->d_curl_out,
1194004e4986SSebastian Grimberg                                &diag->d_eval_modes_in, &diag->d_eval_modes_out, &assembled_qf_array, &elem_diag_array};
1195004e4986SSebastian Grimberg 
1196ca735530SJeremy L Thompson     if (is_point_block) {
1197004e4986SSebastian Grimberg       CeedCallBackend(CeedRunKernelDim_Cuda(ceed, diag->LinearPointBlock, grid, num_nodes, 1, elems_per_block, args));
11980d0321e0SJeremy L Thompson     } else {
1199004e4986SSebastian Grimberg       CeedCallBackend(CeedRunKernelDim_Cuda(ceed, diag->LinearDiagonal, grid, num_nodes, 1, elems_per_block, args));
12000d0321e0SJeremy L Thompson     }
12010d0321e0SJeremy L Thompson 
12020d0321e0SJeremy L Thompson     // Restore arrays
1203ca735530SJeremy L Thompson     CeedCallBackend(CeedVectorRestoreArray(elem_diag, &elem_diag_array));
1204ca735530SJeremy L Thompson     CeedCallBackend(CeedVectorRestoreArrayRead(assembled_qf, &assembled_qf_array));
120591db28b6SZach Atkins   }
12060d0321e0SJeremy L Thompson 
12070d0321e0SJeremy L Thompson   // Assemble local operator diagonal
1208ca735530SJeremy L Thompson   CeedCallBackend(CeedElemRestrictionApply(diag_rstr, CEED_TRANSPOSE, elem_diag, assembled, request));
12090d0321e0SJeremy L Thompson 
12100d0321e0SJeremy L Thompson   // Cleanup
1211ca735530SJeremy L Thompson   CeedCallBackend(CeedVectorDestroy(&assembled_qf));
12120d0321e0SJeremy L Thompson   return CEED_ERROR_SUCCESS;
12130d0321e0SJeremy L Thompson }
12140d0321e0SJeremy L Thompson 
12150d0321e0SJeremy L Thompson //------------------------------------------------------------------------------
12160d0321e0SJeremy L Thompson // Assemble Linear Diagonal
12170d0321e0SJeremy L Thompson //------------------------------------------------------------------------------
12182b730f8bSJeremy L Thompson static int CeedOperatorLinearAssembleAddDiagonal_Cuda(CeedOperator op, CeedVector assembled, CeedRequest *request) {
12192b730f8bSJeremy L Thompson   CeedCallBackend(CeedOperatorAssembleDiagonalCore_Cuda(op, assembled, request, false));
12206aa95790SJeremy L Thompson   return CEED_ERROR_SUCCESS;
12210d0321e0SJeremy L Thompson }
12220d0321e0SJeremy L Thompson 
12230d0321e0SJeremy L Thompson //------------------------------------------------------------------------------
12240d0321e0SJeremy L Thompson // Assemble Linear Point Block Diagonal
12250d0321e0SJeremy L Thompson //------------------------------------------------------------------------------
12262b730f8bSJeremy L Thompson static int CeedOperatorLinearAssembleAddPointBlockDiagonal_Cuda(CeedOperator op, CeedVector assembled, CeedRequest *request) {
12272b730f8bSJeremy L Thompson   CeedCallBackend(CeedOperatorAssembleDiagonalCore_Cuda(op, assembled, request, true));
12286aa95790SJeremy L Thompson   return CEED_ERROR_SUCCESS;
12290d0321e0SJeremy L Thompson }
12300d0321e0SJeremy L Thompson 
12310d0321e0SJeremy L Thompson //------------------------------------------------------------------------------
1232004e4986SSebastian Grimberg // Single Operator Assembly Setup
1233cc132f9aSnbeams //------------------------------------------------------------------------------
1234f7c1b517Snbeams static int CeedSingleOperatorAssembleSetup_Cuda(CeedOperator op, CeedInt use_ceedsize_idx) {
1235cc132f9aSnbeams   Ceed                ceed;
1236004e4986SSebastian Grimberg   Ceed_Cuda          *cuda_data;
123722070f95SJeremy L Thompson   char               *assembly_kernel_source;
123822070f95SJeremy L Thompson   const char         *assembly_kernel_path;
1239004e4986SSebastian Grimberg   CeedInt             num_input_fields, num_output_fields, num_eval_modes_in = 0, num_eval_modes_out = 0;
12403b38d1dfSJeremy L Thompson   CeedInt             elem_size_in, num_qpts_in = 0, num_comp_in, elem_size_out, num_qpts_out, num_comp_out, q_comp;
1241004e4986SSebastian Grimberg   CeedEvalMode       *eval_modes_in = NULL, *eval_modes_out = NULL;
1242ca735530SJeremy L Thompson   CeedElemRestriction rstr_in = NULL, rstr_out = NULL;
1243ca735530SJeremy L Thompson   CeedBasis           basis_in = NULL, basis_out = NULL;
1244ca735530SJeremy L Thompson   CeedQFunctionField *qf_fields;
1245ca735530SJeremy L Thompson   CeedQFunction       qf;
1246ca735530SJeremy L Thompson   CeedOperatorField  *input_fields, *output_fields;
1247cc132f9aSnbeams   CeedOperator_Cuda  *impl;
1248ca735530SJeremy L Thompson 
1249ca735530SJeremy L Thompson   CeedCallBackend(CeedOperatorGetCeed(op, &ceed));
12502b730f8bSJeremy L Thompson   CeedCallBackend(CeedOperatorGetData(op, &impl));
1251cc132f9aSnbeams 
1252cc132f9aSnbeams   // Get intput and output fields
12532b730f8bSJeremy L Thompson   CeedCallBackend(CeedOperatorGetFields(op, &num_input_fields, &input_fields, &num_output_fields, &output_fields));
1254cc132f9aSnbeams 
1255cc132f9aSnbeams   // Determine active input basis eval mode
12562b730f8bSJeremy L Thompson   CeedCallBackend(CeedOperatorGetQFunction(op, &qf));
12572b730f8bSJeremy L Thompson   CeedCallBackend(CeedQFunctionGetFields(qf, NULL, &qf_fields, NULL, NULL));
1258cc132f9aSnbeams   for (CeedInt i = 0; i < num_input_fields; i++) {
1259cc132f9aSnbeams     CeedVector vec;
1260ca735530SJeremy L Thompson 
12612b730f8bSJeremy L Thompson     CeedCallBackend(CeedOperatorFieldGetVector(input_fields[i], &vec));
1262cc132f9aSnbeams     if (vec == CEED_VECTOR_ACTIVE) {
1263004e4986SSebastian Grimberg       CeedBasis    basis;
1264ca735530SJeremy L Thompson       CeedEvalMode eval_mode;
1265ca735530SJeremy L Thompson 
1266004e4986SSebastian Grimberg       CeedCallBackend(CeedOperatorFieldGetBasis(input_fields[i], &basis));
1267004e4986SSebastian Grimberg       CeedCheck(!basis_in || basis_in == basis, ceed, CEED_ERROR_BACKEND, "Backend does not implement operator assembly with multiple active bases");
1268004e4986SSebastian Grimberg       basis_in = basis;
12692b730f8bSJeremy L Thompson       CeedCallBackend(CeedOperatorFieldGetElemRestriction(input_fields[i], &rstr_in));
1270004e4986SSebastian Grimberg       CeedCallBackend(CeedElemRestrictionGetElementSize(rstr_in, &elem_size_in));
1271004e4986SSebastian Grimberg       if (basis_in == CEED_BASIS_NONE) num_qpts_in = elem_size_in;
1272004e4986SSebastian Grimberg       else CeedCallBackend(CeedBasisGetNumQuadraturePoints(basis_in, &num_qpts_in));
12732b730f8bSJeremy L Thompson       CeedCallBackend(CeedQFunctionFieldGetEvalMode(qf_fields[i], &eval_mode));
1274004e4986SSebastian Grimberg       CeedCallBackend(CeedBasisGetNumQuadratureComponents(basis_in, eval_mode, &q_comp));
1275004e4986SSebastian Grimberg       if (eval_mode != CEED_EVAL_WEIGHT) {
1276004e4986SSebastian Grimberg         // q_comp = 1 if CEED_EVAL_NONE, CEED_EVAL_WEIGHT caught by QF Assembly
1277004e4986SSebastian Grimberg         CeedCallBackend(CeedRealloc(num_eval_modes_in + q_comp, &eval_modes_in));
1278004e4986SSebastian Grimberg         for (CeedInt d = 0; d < q_comp; d++) {
1279004e4986SSebastian Grimberg           eval_modes_in[num_eval_modes_in + d] = eval_mode;
1280cc132f9aSnbeams         }
1281004e4986SSebastian Grimberg         num_eval_modes_in += q_comp;
1282cc132f9aSnbeams       }
1283cc132f9aSnbeams     }
1284cc132f9aSnbeams   }
1285cc132f9aSnbeams 
1286cc132f9aSnbeams   // Determine active output basis; basis_out and rstr_out only used if same as input, TODO
12872b730f8bSJeremy L Thompson   CeedCallBackend(CeedQFunctionGetFields(qf, NULL, NULL, NULL, &qf_fields));
1288cc132f9aSnbeams   for (CeedInt i = 0; i < num_output_fields; i++) {
1289cc132f9aSnbeams     CeedVector vec;
1290ca735530SJeremy L Thompson 
12912b730f8bSJeremy L Thompson     CeedCallBackend(CeedOperatorFieldGetVector(output_fields[i], &vec));
1292cc132f9aSnbeams     if (vec == CEED_VECTOR_ACTIVE) {
1293004e4986SSebastian Grimberg       CeedBasis    basis;
1294ca735530SJeremy L Thompson       CeedEvalMode eval_mode;
1295ca735530SJeremy L Thompson 
1296004e4986SSebastian Grimberg       CeedCallBackend(CeedOperatorFieldGetBasis(output_fields[i], &basis));
1297004e4986SSebastian Grimberg       CeedCheck(!basis_out || basis_out == basis, ceed, CEED_ERROR_BACKEND,
1298004e4986SSebastian Grimberg                 "Backend does not implement operator assembly with multiple active bases");
1299004e4986SSebastian Grimberg       basis_out = basis;
13002b730f8bSJeremy L Thompson       CeedCallBackend(CeedOperatorFieldGetElemRestriction(output_fields[i], &rstr_out));
1301004e4986SSebastian Grimberg       CeedCallBackend(CeedElemRestrictionGetElementSize(rstr_out, &elem_size_out));
1302004e4986SSebastian Grimberg       if (basis_out == CEED_BASIS_NONE) num_qpts_out = elem_size_out;
1303004e4986SSebastian Grimberg       else CeedCallBackend(CeedBasisGetNumQuadraturePoints(basis_out, &num_qpts_out));
1304004e4986SSebastian Grimberg       CeedCheck(num_qpts_in == num_qpts_out, ceed, CEED_ERROR_UNSUPPORTED,
1305004e4986SSebastian Grimberg                 "Active input and output bases must have the same number of quadrature points");
13062b730f8bSJeremy L Thompson       CeedCallBackend(CeedQFunctionFieldGetEvalMode(qf_fields[i], &eval_mode));
1307004e4986SSebastian Grimberg       CeedCallBackend(CeedBasisGetNumQuadratureComponents(basis_out, eval_mode, &q_comp));
1308004e4986SSebastian Grimberg       if (eval_mode != CEED_EVAL_WEIGHT) {
1309004e4986SSebastian Grimberg         // q_comp = 1 if CEED_EVAL_NONE, CEED_EVAL_WEIGHT caught by QF Assembly
1310004e4986SSebastian Grimberg         CeedCallBackend(CeedRealloc(num_eval_modes_out + q_comp, &eval_modes_out));
1311004e4986SSebastian Grimberg         for (CeedInt d = 0; d < q_comp; d++) {
1312004e4986SSebastian Grimberg           eval_modes_out[num_eval_modes_out + d] = eval_mode;
1313004e4986SSebastian Grimberg         }
1314004e4986SSebastian Grimberg         num_eval_modes_out += q_comp;
1315cc132f9aSnbeams       }
1316cc132f9aSnbeams     }
1317cc132f9aSnbeams   }
1318004e4986SSebastian Grimberg   CeedCheck(num_eval_modes_in > 0 && num_eval_modes_out > 0, ceed, CEED_ERROR_UNSUPPORTED, "Cannot assemble operator without inputs/outputs");
1319cc132f9aSnbeams 
13202b730f8bSJeremy L Thompson   CeedCallBackend(CeedCalloc(1, &impl->asmb));
1321cc132f9aSnbeams   CeedOperatorAssemble_Cuda *asmb = impl->asmb;
1322004e4986SSebastian Grimberg   asmb->elems_per_block           = 1;
1323004e4986SSebastian Grimberg   asmb->block_size_x              = elem_size_in;
1324004e4986SSebastian Grimberg   asmb->block_size_y              = elem_size_out;
1325ca735530SJeremy L Thompson 
13262b730f8bSJeremy L Thompson   CeedCallBackend(CeedGetData(ceed, &cuda_data));
1327004e4986SSebastian Grimberg   bool fallback = asmb->block_size_x * asmb->block_size_y * asmb->elems_per_block > cuda_data->device_prop.maxThreadsPerBlock;
1328004e4986SSebastian Grimberg 
1329004e4986SSebastian Grimberg   if (fallback) {
1330004e4986SSebastian Grimberg     // Use fallback kernel with 1D threadblock
1331004e4986SSebastian Grimberg     asmb->block_size_y = 1;
1332004e4986SSebastian Grimberg   }
1333004e4986SSebastian Grimberg 
1334004e4986SSebastian Grimberg   // Compile kernels
1335004e4986SSebastian Grimberg   CeedCallBackend(CeedElemRestrictionGetNumComponents(rstr_in, &num_comp_in));
1336004e4986SSebastian Grimberg   CeedCallBackend(CeedElemRestrictionGetNumComponents(rstr_out, &num_comp_out));
13372b730f8bSJeremy L Thompson   CeedCallBackend(CeedGetJitAbsolutePath(ceed, "ceed/jit-source/cuda/cuda-ref-operator-assemble.h", &assembly_kernel_path));
133823d4529eSJeremy L Thompson   CeedDebug256(ceed, CEED_DEBUG_COLOR_SUCCESS, "----- Loading Assembly Kernel Source -----\n");
13392b730f8bSJeremy L Thompson   CeedCallBackend(CeedLoadSourceToBuffer(ceed, assembly_kernel_path, &assembly_kernel_source));
134023d4529eSJeremy L Thompson   CeedDebug256(ceed, CEED_DEBUG_COLOR_SUCCESS, "----- Loading Assembly Source Complete! -----\n");
1341004e4986SSebastian Grimberg   CeedCallBackend(CeedCompile_Cuda(ceed, assembly_kernel_source, &asmb->module, 10, "NUM_EVAL_MODES_IN", num_eval_modes_in, "NUM_EVAL_MODES_OUT",
1342004e4986SSebastian Grimberg                                    num_eval_modes_out, "NUM_COMP_IN", num_comp_in, "NUM_COMP_OUT", num_comp_out, "NUM_NODES_IN", elem_size_in,
1343004e4986SSebastian Grimberg                                    "NUM_NODES_OUT", elem_size_out, "NUM_QPTS", num_qpts_in, "BLOCK_SIZE",
1344cbfe683aSSebastian Grimberg                                    asmb->block_size_x * asmb->block_size_y * asmb->elems_per_block, "BLOCK_SIZE_Y", asmb->block_size_y,
1345cbfe683aSSebastian Grimberg                                    "USE_CEEDSIZE", use_ceedsize_idx));
1346004e4986SSebastian Grimberg   CeedCallBackend(CeedGetKernel_Cuda(ceed, asmb->module, "LinearAssemble", &asmb->LinearAssemble));
13472b730f8bSJeremy L Thompson   CeedCallBackend(CeedFree(&assembly_kernel_path));
13482b730f8bSJeremy L Thompson   CeedCallBackend(CeedFree(&assembly_kernel_source));
1349cc132f9aSnbeams 
1350004e4986SSebastian Grimberg   // Load into B_in, in order that they will be used in eval_modes_in
1351004e4986SSebastian Grimberg   {
1352004e4986SSebastian Grimberg     const CeedInt in_bytes           = elem_size_in * num_qpts_in * num_eval_modes_in * sizeof(CeedScalar);
1353004e4986SSebastian Grimberg     CeedInt       d_in               = 0;
1354004e4986SSebastian Grimberg     CeedEvalMode  eval_modes_in_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_in; i++) {
1359004e4986SSebastian Grimberg       has_eval_none = has_eval_none || (eval_modes_in[i] == CEED_EVAL_NONE);
1360004e4986SSebastian Grimberg     }
1361004e4986SSebastian Grimberg     if (has_eval_none) {
1362004e4986SSebastian Grimberg       CeedCallBackend(CeedCalloc(elem_size_in * num_qpts_in, &identity));
1363004e4986SSebastian 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;
1364004e4986SSebastian Grimberg     }
1365cc132f9aSnbeams 
1366004e4986SSebastian Grimberg     CeedCallCuda(ceed, cudaMalloc((void **)&asmb->d_B_in, in_bytes));
1367004e4986SSebastian Grimberg     for (CeedInt i = 0; i < num_eval_modes_in; i++) {
1368004e4986SSebastian Grimberg       const CeedScalar *h_B_in;
1369ca735530SJeremy L Thompson 
1370004e4986SSebastian Grimberg       CeedCallBackend(CeedOperatorGetBasisPointer(basis_in, eval_modes_in[i], identity, &h_B_in));
1371004e4986SSebastian Grimberg       CeedCallBackend(CeedBasisGetNumQuadratureComponents(basis_in, eval_modes_in[i], &q_comp));
1372004e4986SSebastian Grimberg       if (q_comp > 1) {
1373004e4986SSebastian Grimberg         if (i == 0 || eval_modes_in[i] != eval_modes_in_prev) d_in = 0;
1374004e4986SSebastian Grimberg         else h_B_in = &h_B_in[(++d_in) * elem_size_in * num_qpts_in];
1375004e4986SSebastian Grimberg       }
1376004e4986SSebastian Grimberg       eval_modes_in_prev = eval_modes_in[i];
1377ca735530SJeremy L Thompson 
1378004e4986SSebastian 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),
1379004e4986SSebastian Grimberg                                     cudaMemcpyHostToDevice));
1380004e4986SSebastian Grimberg     }
1381004e4986SSebastian Grimberg 
1382004e4986SSebastian Grimberg     if (identity) {
1383004e4986SSebastian Grimberg       CeedCallBackend(CeedFree(&identity));
1384cc132f9aSnbeams     }
1385cc132f9aSnbeams   }
1386cc132f9aSnbeams 
1387004e4986SSebastian Grimberg   // Load into B_out, in order that they will be used in eval_modes_out
1388004e4986SSebastian Grimberg   {
1389004e4986SSebastian Grimberg     const CeedInt out_bytes           = elem_size_out * num_qpts_out * num_eval_modes_out * sizeof(CeedScalar);
1390004e4986SSebastian Grimberg     CeedInt       d_out               = 0;
1391004e4986SSebastian Grimberg     CeedEvalMode  eval_modes_out_prev = CEED_EVAL_NONE;
1392004e4986SSebastian Grimberg     bool          has_eval_none       = false;
1393004e4986SSebastian Grimberg     CeedScalar   *identity            = NULL;
1394ca735530SJeremy L Thompson 
1395004e4986SSebastian Grimberg     for (CeedInt i = 0; i < num_eval_modes_out; i++) {
1396004e4986SSebastian Grimberg       has_eval_none = has_eval_none || (eval_modes_out[i] == CEED_EVAL_NONE);
1397004e4986SSebastian Grimberg     }
1398004e4986SSebastian Grimberg     if (has_eval_none) {
1399004e4986SSebastian Grimberg       CeedCallBackend(CeedCalloc(elem_size_out * num_qpts_out, &identity));
1400004e4986SSebastian 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;
1401cc132f9aSnbeams     }
1402cc132f9aSnbeams 
1403004e4986SSebastian Grimberg     CeedCallCuda(ceed, cudaMalloc((void **)&asmb->d_B_out, out_bytes));
1404004e4986SSebastian Grimberg     for (CeedInt i = 0; i < num_eval_modes_out; i++) {
1405004e4986SSebastian Grimberg       const CeedScalar *h_B_out;
1406ca735530SJeremy L Thompson 
1407004e4986SSebastian Grimberg       CeedCallBackend(CeedOperatorGetBasisPointer(basis_out, eval_modes_out[i], identity, &h_B_out));
1408004e4986SSebastian Grimberg       CeedCallBackend(CeedBasisGetNumQuadratureComponents(basis_out, eval_modes_out[i], &q_comp));
1409004e4986SSebastian Grimberg       if (q_comp > 1) {
1410004e4986SSebastian Grimberg         if (i == 0 || eval_modes_out[i] != eval_modes_out_prev) d_out = 0;
1411004e4986SSebastian Grimberg         else h_B_out = &h_B_out[(++d_out) * elem_size_out * num_qpts_out];
1412004e4986SSebastian Grimberg       }
1413004e4986SSebastian Grimberg       eval_modes_out_prev = eval_modes_out[i];
1414ca735530SJeremy L Thompson 
1415004e4986SSebastian 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),
1416004e4986SSebastian Grimberg                                     cudaMemcpyHostToDevice));
1417004e4986SSebastian Grimberg     }
1418004e4986SSebastian Grimberg 
1419004e4986SSebastian Grimberg     if (identity) {
1420004e4986SSebastian Grimberg       CeedCallBackend(CeedFree(&identity));
1421cc132f9aSnbeams     }
1422cc132f9aSnbeams   }
1423cc132f9aSnbeams   return CEED_ERROR_SUCCESS;
1424cc132f9aSnbeams }
1425cc132f9aSnbeams 
1426cc132f9aSnbeams //------------------------------------------------------------------------------
1427cefa2673SJeremy L Thompson // Assemble matrix data for COO matrix of assembled operator.
1428cefa2673SJeremy L Thompson // The sparsity pattern is set by CeedOperatorLinearAssembleSymbolic.
1429cefa2673SJeremy L Thompson //
1430ea61e9acSJeremy L Thompson // Note that this (and other assembly routines) currently assume only one active input restriction/basis per operator (could have multiple basis eval
1431cefa2673SJeremy L Thompson // modes).
1432cefa2673SJeremy L Thompson // TODO: allow multiple active input restrictions/basis objects
1433cc132f9aSnbeams //------------------------------------------------------------------------------
14342b730f8bSJeremy L Thompson static int CeedSingleOperatorAssemble_Cuda(CeedOperator op, CeedInt offset, CeedVector values) {
1435cc132f9aSnbeams   Ceed                ceed;
1436ca735530SJeremy L Thompson   CeedSize            values_length = 0, assembled_qf_length = 0;
1437004e4986SSebastian Grimberg   CeedInt             use_ceedsize_idx = 0, num_elem_in, num_elem_out, elem_size_in, elem_size_out;
1438ca735530SJeremy L Thompson   CeedScalar         *values_array;
1439004e4986SSebastian Grimberg   const CeedScalar   *assembled_qf_array;
1440ca735530SJeremy L Thompson   CeedVector          assembled_qf   = NULL;
1441004e4986SSebastian Grimberg   CeedElemRestriction assembled_rstr = NULL, rstr_in, rstr_out;
1442004e4986SSebastian Grimberg   CeedRestrictionType rstr_type_in, rstr_type_out;
1443004e4986SSebastian Grimberg   const bool         *orients_in = NULL, *orients_out = NULL;
1444004e4986SSebastian Grimberg   const CeedInt8     *curl_orients_in = NULL, *curl_orients_out = NULL;
1445cc132f9aSnbeams   CeedOperator_Cuda  *impl;
1446ca735530SJeremy L Thompson 
1447ca735530SJeremy L Thompson   CeedCallBackend(CeedOperatorGetCeed(op, &ceed));
14482b730f8bSJeremy L Thompson   CeedCallBackend(CeedOperatorGetData(op, &impl));
1449cc132f9aSnbeams 
1450cc132f9aSnbeams   // Assemble QFunction
1451004e4986SSebastian Grimberg   CeedCallBackend(CeedOperatorLinearAssembleQFunctionBuildOrUpdate(op, &assembled_qf, &assembled_rstr, CEED_REQUEST_IMMEDIATE));
1452004e4986SSebastian Grimberg   CeedCallBackend(CeedElemRestrictionDestroy(&assembled_rstr));
1453004e4986SSebastian Grimberg   CeedCallBackend(CeedVectorGetArrayRead(assembled_qf, CEED_MEM_DEVICE, &assembled_qf_array));
1454cc132f9aSnbeams 
1455f7c1b517Snbeams   CeedCallBackend(CeedVectorGetLength(values, &values_length));
1456f7c1b517Snbeams   CeedCallBackend(CeedVectorGetLength(assembled_qf, &assembled_qf_length));
1457f7c1b517Snbeams   if ((values_length > INT_MAX) || (assembled_qf_length > INT_MAX)) use_ceedsize_idx = 1;
1458004e4986SSebastian Grimberg 
1459f7c1b517Snbeams   // Setup
1460004e4986SSebastian Grimberg   if (!impl->asmb) CeedCallBackend(CeedSingleOperatorAssembleSetup_Cuda(op, use_ceedsize_idx));
1461004e4986SSebastian Grimberg   CeedOperatorAssemble_Cuda *asmb = impl->asmb;
1462004e4986SSebastian Grimberg 
1463004e4986SSebastian Grimberg   assert(asmb != NULL);
1464004e4986SSebastian Grimberg 
1465004e4986SSebastian Grimberg   // Assemble element operator
1466004e4986SSebastian Grimberg   CeedCallBackend(CeedVectorGetArray(values, CEED_MEM_DEVICE, &values_array));
1467004e4986SSebastian Grimberg   values_array += offset;
1468004e4986SSebastian Grimberg 
1469004e4986SSebastian Grimberg   CeedCallBackend(CeedOperatorGetActiveElemRestrictions(op, &rstr_in, &rstr_out));
1470004e4986SSebastian Grimberg   CeedCallBackend(CeedElemRestrictionGetNumElements(rstr_in, &num_elem_in));
1471004e4986SSebastian Grimberg   CeedCallBackend(CeedElemRestrictionGetElementSize(rstr_in, &elem_size_in));
1472004e4986SSebastian Grimberg 
1473004e4986SSebastian Grimberg   CeedCallBackend(CeedElemRestrictionGetType(rstr_in, &rstr_type_in));
1474004e4986SSebastian Grimberg   if (rstr_type_in == CEED_RESTRICTION_ORIENTED) {
1475004e4986SSebastian Grimberg     CeedCallBackend(CeedElemRestrictionGetOrientations(rstr_in, CEED_MEM_DEVICE, &orients_in));
1476004e4986SSebastian Grimberg   } else if (rstr_type_in == CEED_RESTRICTION_CURL_ORIENTED) {
1477004e4986SSebastian Grimberg     CeedCallBackend(CeedElemRestrictionGetCurlOrientations(rstr_in, CEED_MEM_DEVICE, &curl_orients_in));
1478004e4986SSebastian Grimberg   }
1479004e4986SSebastian Grimberg 
1480004e4986SSebastian Grimberg   if (rstr_in != rstr_out) {
1481004e4986SSebastian Grimberg     CeedCallBackend(CeedElemRestrictionGetNumElements(rstr_out, &num_elem_out));
1482004e4986SSebastian Grimberg     CeedCheck(num_elem_in == num_elem_out, ceed, CEED_ERROR_UNSUPPORTED,
1483004e4986SSebastian Grimberg               "Active input and output operator restrictions must have the same number of elements");
1484004e4986SSebastian Grimberg     CeedCallBackend(CeedElemRestrictionGetElementSize(rstr_out, &elem_size_out));
1485004e4986SSebastian Grimberg 
1486004e4986SSebastian Grimberg     CeedCallBackend(CeedElemRestrictionGetType(rstr_out, &rstr_type_out));
1487004e4986SSebastian Grimberg     if (rstr_type_out == CEED_RESTRICTION_ORIENTED) {
1488004e4986SSebastian Grimberg       CeedCallBackend(CeedElemRestrictionGetOrientations(rstr_out, CEED_MEM_DEVICE, &orients_out));
1489004e4986SSebastian Grimberg     } else if (rstr_type_out == CEED_RESTRICTION_CURL_ORIENTED) {
1490004e4986SSebastian Grimberg       CeedCallBackend(CeedElemRestrictionGetCurlOrientations(rstr_out, CEED_MEM_DEVICE, &curl_orients_out));
1491004e4986SSebastian Grimberg     }
1492004e4986SSebastian Grimberg   } else {
1493004e4986SSebastian Grimberg     elem_size_out    = elem_size_in;
1494004e4986SSebastian Grimberg     orients_out      = orients_in;
1495004e4986SSebastian Grimberg     curl_orients_out = curl_orients_in;
1496f7c1b517Snbeams   }
1497f7c1b517Snbeams 
1498cc132f9aSnbeams   // Compute B^T D B
1499004e4986SSebastian Grimberg   CeedInt shared_mem =
1500004e4986SSebastian 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)) *
1501004e4986SSebastian Grimberg       sizeof(CeedScalar);
1502004e4986SSebastian Grimberg   CeedInt grid   = CeedDivUpInt(num_elem_in, asmb->elems_per_block);
1503004e4986SSebastian Grimberg   void   *args[] = {(void *)&num_elem_in, &asmb->d_B_in,     &asmb->d_B_out,      &orients_in,  &curl_orients_in,
1504004e4986SSebastian Grimberg                     &orients_out,         &curl_orients_out, &assembled_qf_array, &values_array};
1505ca735530SJeremy L Thompson 
15062b730f8bSJeremy L Thompson   CeedCallBackend(
1507004e4986SSebastian Grimberg       CeedRunKernelDimShared_Cuda(ceed, asmb->LinearAssemble, grid, asmb->block_size_x, asmb->block_size_y, asmb->elems_per_block, shared_mem, args));
1508cc132f9aSnbeams 
1509cc132f9aSnbeams   // Restore arrays
15102b730f8bSJeremy L Thompson   CeedCallBackend(CeedVectorRestoreArray(values, &values_array));
1511004e4986SSebastian Grimberg   CeedCallBackend(CeedVectorRestoreArrayRead(assembled_qf, &assembled_qf_array));
1512cc132f9aSnbeams 
1513cc132f9aSnbeams   // Cleanup
15142b730f8bSJeremy L Thompson   CeedCallBackend(CeedVectorDestroy(&assembled_qf));
1515004e4986SSebastian Grimberg   if (rstr_type_in == CEED_RESTRICTION_ORIENTED) {
1516004e4986SSebastian Grimberg     CeedCallBackend(CeedElemRestrictionRestoreOrientations(rstr_in, &orients_in));
1517004e4986SSebastian Grimberg   } else if (rstr_type_in == CEED_RESTRICTION_CURL_ORIENTED) {
1518004e4986SSebastian Grimberg     CeedCallBackend(CeedElemRestrictionRestoreCurlOrientations(rstr_in, &curl_orients_in));
1519004e4986SSebastian Grimberg   }
1520004e4986SSebastian Grimberg   if (rstr_in != rstr_out) {
1521004e4986SSebastian Grimberg     if (rstr_type_out == CEED_RESTRICTION_ORIENTED) {
1522004e4986SSebastian Grimberg       CeedCallBackend(CeedElemRestrictionRestoreOrientations(rstr_out, &orients_out));
1523004e4986SSebastian Grimberg     } else if (rstr_type_out == CEED_RESTRICTION_CURL_ORIENTED) {
1524004e4986SSebastian Grimberg       CeedCallBackend(CeedElemRestrictionRestoreCurlOrientations(rstr_out, &curl_orients_out));
1525004e4986SSebastian Grimberg     }
1526004e4986SSebastian Grimberg   }
1527cc132f9aSnbeams   return CEED_ERROR_SUCCESS;
1528cc132f9aSnbeams }
1529cc132f9aSnbeams 
1530cc132f9aSnbeams //------------------------------------------------------------------------------
1531756ca9e9SJeremy L Thompson // Assemble Linear QFunction AtPoints
1532756ca9e9SJeremy L Thompson //------------------------------------------------------------------------------
1533756ca9e9SJeremy L Thompson static int CeedOperatorLinearAssembleQFunctionAtPoints_Cuda(CeedOperator op, CeedVector *assembled, CeedElemRestriction *rstr, CeedRequest *request) {
1534756ca9e9SJeremy L Thompson   return CeedError(CeedOperatorReturnCeed(op), CEED_ERROR_BACKEND, "Backend does not implement CeedOperatorLinearAssembleQFunction");
1535756ca9e9SJeremy L Thompson }
1536756ca9e9SJeremy L Thompson 
1537756ca9e9SJeremy L Thompson //------------------------------------------------------------------------------
1538756ca9e9SJeremy L Thompson // Assemble Linear Diagonal AtPoints
1539756ca9e9SJeremy L Thompson //------------------------------------------------------------------------------
1540756ca9e9SJeremy L Thompson static int CeedOperatorLinearAssembleAddDiagonalAtPoints_Cuda(CeedOperator op, CeedVector assembled, CeedRequest *request) {
1541382e9c83SJeremy L Thompson   CeedInt             max_num_points, num_elem, num_input_fields, num_output_fields;
1542349fb27dSJeremy L Thompson   CeedScalar         *e_data[2 * CEED_FIELD_MAX] = {NULL};
1543349fb27dSJeremy L Thompson   CeedQFunctionField *qf_input_fields, *qf_output_fields;
1544349fb27dSJeremy L Thompson   CeedQFunction       qf;
1545349fb27dSJeremy L Thompson   CeedOperatorField  *op_input_fields, *op_output_fields;
1546349fb27dSJeremy L Thompson   CeedOperator_Cuda  *impl;
1547349fb27dSJeremy L Thompson 
1548349fb27dSJeremy L Thompson   CeedCallBackend(CeedOperatorGetData(op, &impl));
1549349fb27dSJeremy L Thompson   CeedCallBackend(CeedOperatorGetQFunction(op, &qf));
1550349fb27dSJeremy L Thompson   CeedCallBackend(CeedOperatorGetNumElements(op, &num_elem));
1551349fb27dSJeremy L Thompson   CeedCallBackend(CeedOperatorGetFields(op, &num_input_fields, &op_input_fields, &num_output_fields, &op_output_fields));
1552349fb27dSJeremy L Thompson   CeedCallBackend(CeedQFunctionGetFields(qf, NULL, &qf_input_fields, NULL, &qf_output_fields));
1553349fb27dSJeremy L Thompson   CeedInt num_points[num_elem];
1554349fb27dSJeremy L Thompson 
1555349fb27dSJeremy L Thompson   // Setup
1556349fb27dSJeremy L Thompson   CeedCallBackend(CeedOperatorSetupAtPoints_Cuda(op));
1557349fb27dSJeremy L Thompson   max_num_points = impl->max_num_points;
1558349fb27dSJeremy L Thompson   for (CeedInt i = 0; i < num_elem; i++) num_points[i] = max_num_points;
1559349fb27dSJeremy L Thompson 
1560349fb27dSJeremy L Thompson   // Input Evecs and Restriction
1561349fb27dSJeremy L Thompson   CeedCallBackend(CeedOperatorSetupInputs_Cuda(num_input_fields, qf_input_fields, op_input_fields, NULL, true, e_data, impl, request));
1562349fb27dSJeremy L Thompson 
1563349fb27dSJeremy L Thompson   // Get point coordinates
1564349fb27dSJeremy L Thompson   if (!impl->point_coords_elem) {
1565349fb27dSJeremy L Thompson     CeedVector          point_coords = NULL;
1566349fb27dSJeremy L Thompson     CeedElemRestriction rstr_points  = NULL;
1567349fb27dSJeremy L Thompson 
1568349fb27dSJeremy L Thompson     CeedCallBackend(CeedOperatorAtPointsGetPoints(op, &rstr_points, &point_coords));
1569349fb27dSJeremy L Thompson     CeedCallBackend(CeedElemRestrictionCreateVector(rstr_points, NULL, &impl->point_coords_elem));
1570349fb27dSJeremy L Thompson     CeedCallBackend(CeedElemRestrictionApply(rstr_points, CEED_NOTRANSPOSE, point_coords, impl->point_coords_elem, request));
1571349fb27dSJeremy L Thompson   }
1572349fb27dSJeremy L Thompson 
1573382e9c83SJeremy L Thompson   // Clear active input Qvecs
1574382e9c83SJeremy L Thompson   for (CeedInt i = 0; i < num_input_fields; i++) {
1575382e9c83SJeremy L Thompson     CeedVector vec;
1576382e9c83SJeremy L Thompson 
1577382e9c83SJeremy L Thompson     CeedCallBackend(CeedOperatorFieldGetVector(op_input_fields[i], &vec));
1578382e9c83SJeremy L Thompson     if (vec != CEED_VECTOR_ACTIVE) continue;
1579382e9c83SJeremy L Thompson     CeedCallBackend(CeedVectorSetValue(impl->q_vecs_in[i], 0.0));
1580382e9c83SJeremy L Thompson   }
1581382e9c83SJeremy L Thompson 
1582349fb27dSJeremy L Thompson   // Input basis apply if needed
1583349fb27dSJeremy L Thompson   CeedCallBackend(CeedOperatorInputBasisAtPoints_Cuda(num_elem, num_points, qf_input_fields, op_input_fields, num_input_fields, true, e_data, impl));
1584349fb27dSJeremy L Thompson 
1585349fb27dSJeremy L Thompson   // Output pointers, as necessary
1586349fb27dSJeremy L Thompson   for (CeedInt i = 0; i < num_output_fields; i++) {
1587349fb27dSJeremy L Thompson     CeedEvalMode eval_mode;
1588349fb27dSJeremy L Thompson 
1589349fb27dSJeremy L Thompson     CeedCallBackend(CeedQFunctionFieldGetEvalMode(qf_output_fields[i], &eval_mode));
1590349fb27dSJeremy L Thompson     if (eval_mode == CEED_EVAL_NONE) {
1591349fb27dSJeremy L Thompson       // Set the output Q-Vector to use the E-Vector data directly.
1592349fb27dSJeremy L Thompson       CeedCallBackend(CeedVectorGetArrayWrite(impl->e_vecs[i + impl->num_inputs], CEED_MEM_DEVICE, &e_data[i + num_input_fields]));
1593349fb27dSJeremy L Thompson       CeedCallBackend(CeedVectorSetArray(impl->q_vecs_out[i], CEED_MEM_DEVICE, CEED_USE_POINTER, e_data[i + num_input_fields]));
1594349fb27dSJeremy L Thompson     }
1595349fb27dSJeremy L Thompson   }
1596349fb27dSJeremy L Thompson 
1597349fb27dSJeremy L Thompson   // Loop over active fields
1598349fb27dSJeremy L Thompson   for (CeedInt i = 0; i < num_input_fields; i++) {
1599382e9c83SJeremy L Thompson     bool                is_active_at_points = true;
1600382e9c83SJeremy L Thompson     CeedInt             elem_size = 1, num_comp_active = 1, e_vec_size = 0;
1601382e9c83SJeremy L Thompson     CeedRestrictionType rstr_type;
1602382e9c83SJeremy L Thompson     CeedVector          vec;
1603382e9c83SJeremy L Thompson     CeedElemRestriction elem_rstr;
1604382e9c83SJeremy L Thompson 
1605382e9c83SJeremy L Thompson     CeedCallBackend(CeedOperatorFieldGetVector(op_input_fields[i], &vec));
1606382e9c83SJeremy L Thompson     // -- Skip non-active input
1607382e9c83SJeremy L Thompson     if (vec != CEED_VECTOR_ACTIVE) continue;
1608382e9c83SJeremy L Thompson 
1609382e9c83SJeremy L Thompson     // -- Get active restriction type
1610382e9c83SJeremy L Thompson     CeedCallBackend(CeedOperatorFieldGetElemRestriction(op_input_fields[i], &elem_rstr));
1611382e9c83SJeremy L Thompson     CeedCallBackend(CeedElemRestrictionGetType(elem_rstr, &rstr_type));
1612382e9c83SJeremy L Thompson     is_active_at_points = rstr_type == CEED_RESTRICTION_POINTS;
1613382e9c83SJeremy L Thompson     if (!is_active_at_points) CeedCallBackend(CeedElemRestrictionGetElementSize(elem_rstr, &elem_size));
1614382e9c83SJeremy L Thompson     else elem_size = max_num_points;
1615382e9c83SJeremy L Thompson     CeedCallBackend(CeedElemRestrictionGetNumComponents(elem_rstr, &num_comp_active));
1616382e9c83SJeremy L Thompson 
1617382e9c83SJeremy L Thompson     e_vec_size = elem_size * num_comp_active;
1618382e9c83SJeremy L Thompson     for (CeedInt s = 0; s < e_vec_size; s++) {
1619349fb27dSJeremy L Thompson       bool         is_active_input = false;
1620349fb27dSJeremy L Thompson       CeedEvalMode eval_mode;
1621349fb27dSJeremy L Thompson       CeedVector   vec;
1622349fb27dSJeremy L Thompson       CeedBasis    basis;
1623349fb27dSJeremy L Thompson 
1624349fb27dSJeremy L Thompson       CeedCallBackend(CeedOperatorFieldGetVector(op_input_fields[i], &vec));
1625349fb27dSJeremy L Thompson       // Skip non-active input
1626349fb27dSJeremy L Thompson       is_active_input = vec == CEED_VECTOR_ACTIVE;
1627349fb27dSJeremy L Thompson       if (!is_active_input) continue;
1628349fb27dSJeremy L Thompson 
1629349fb27dSJeremy L Thompson       // Update unit vector
163013062808SJeremy L Thompson       if (s == 0) CeedCallBackend(CeedVectorSetValue(impl->e_vecs[i], 0.0));
1631349fb27dSJeremy L Thompson       else CeedCallBackend(CeedVectorSetValueStrided(impl->e_vecs[i], s - 1, e_vec_size, 0.0));
1632349fb27dSJeremy L Thompson       CeedCallBackend(CeedVectorSetValueStrided(impl->e_vecs[i], s, e_vec_size, 1.0));
1633349fb27dSJeremy L Thompson 
1634349fb27dSJeremy L Thompson       // Basis action
1635349fb27dSJeremy L Thompson       CeedCallBackend(CeedQFunctionFieldGetEvalMode(qf_input_fields[i], &eval_mode));
1636349fb27dSJeremy L Thompson       switch (eval_mode) {
1637349fb27dSJeremy L Thompson         case CEED_EVAL_NONE:
1638349fb27dSJeremy L Thompson           CeedCallBackend(CeedVectorSetArray(impl->q_vecs_in[i], CEED_MEM_DEVICE, CEED_USE_POINTER, e_data[i]));
1639349fb27dSJeremy L Thompson           break;
1640349fb27dSJeremy L Thompson         case CEED_EVAL_INTERP:
1641349fb27dSJeremy L Thompson         case CEED_EVAL_GRAD:
1642349fb27dSJeremy L Thompson         case CEED_EVAL_DIV:
1643349fb27dSJeremy L Thompson         case CEED_EVAL_CURL:
1644349fb27dSJeremy L Thompson           CeedCallBackend(CeedOperatorFieldGetBasis(op_input_fields[i], &basis));
1645349fb27dSJeremy L Thompson           CeedCallBackend(CeedBasisApplyAtPoints(basis, num_elem, num_points, CEED_NOTRANSPOSE, eval_mode, impl->point_coords_elem, impl->e_vecs[i],
1646349fb27dSJeremy L Thompson                                                  impl->q_vecs_in[i]));
1647349fb27dSJeremy L Thompson           break;
1648349fb27dSJeremy L Thompson         case CEED_EVAL_WEIGHT:
1649349fb27dSJeremy L Thompson           break;  // No action
1650349fb27dSJeremy L Thompson       }
1651349fb27dSJeremy L Thompson 
1652349fb27dSJeremy L Thompson       // Q function
1653349fb27dSJeremy L Thompson       CeedCallBackend(CeedQFunctionApply(qf, num_elem * max_num_points, impl->q_vecs_in, impl->q_vecs_out));
1654349fb27dSJeremy L Thompson 
1655349fb27dSJeremy L Thompson       // Output basis apply if needed
1656382e9c83SJeremy L Thompson       for (CeedInt j = 0; j < num_output_fields; j++) {
1657349fb27dSJeremy L Thompson         bool                is_active_output = false;
1658382e9c83SJeremy L Thompson         CeedInt             elem_size        = 0;
1659382e9c83SJeremy L Thompson         CeedRestrictionType rstr_type;
1660349fb27dSJeremy L Thompson         CeedEvalMode        eval_mode;
1661349fb27dSJeremy L Thompson         CeedVector          vec;
1662349fb27dSJeremy L Thompson         CeedElemRestriction elem_rstr;
1663349fb27dSJeremy L Thompson         CeedBasis           basis;
1664349fb27dSJeremy L Thompson 
1665382e9c83SJeremy L Thompson         CeedCallBackend(CeedOperatorFieldGetVector(op_output_fields[j], &vec));
1666382e9c83SJeremy L Thompson         // ---- Skip non-active output
1667349fb27dSJeremy L Thompson         is_active_output = vec == CEED_VECTOR_ACTIVE;
1668349fb27dSJeremy L Thompson         if (!is_active_output) continue;
1669349fb27dSJeremy L Thompson 
1670382e9c83SJeremy L Thompson         // ---- Check if elem size matches
1671382e9c83SJeremy L Thompson         CeedCallBackend(CeedOperatorFieldGetElemRestriction(op_output_fields[j], &elem_rstr));
1672382e9c83SJeremy L Thompson         CeedCallBackend(CeedElemRestrictionGetType(elem_rstr, &rstr_type));
1673382e9c83SJeremy L Thompson         if (is_active_at_points && rstr_type != CEED_RESTRICTION_POINTS) continue;
1674382e9c83SJeremy L Thompson         if (rstr_type == CEED_RESTRICTION_POINTS) {
1675382e9c83SJeremy L Thompson           CeedCallBackend(CeedElemRestrictionGetMaxPointsInElement(elem_rstr, &elem_size));
1676382e9c83SJeremy L Thompson         } else {
1677382e9c83SJeremy L Thompson           CeedCallBackend(CeedElemRestrictionGetElementSize(elem_rstr, &elem_size));
1678382e9c83SJeremy L Thompson         }
1679382e9c83SJeremy L Thompson         {
1680382e9c83SJeremy L Thompson           CeedInt num_comp = 0;
1681382e9c83SJeremy L Thompson 
1682382e9c83SJeremy L Thompson           CeedCallBackend(CeedElemRestrictionGetNumComponents(elem_rstr, &num_comp));
1683382e9c83SJeremy L Thompson           if (e_vec_size != num_comp * elem_size) continue;
1684382e9c83SJeremy L Thompson         }
1685382e9c83SJeremy L Thompson 
1686349fb27dSJeremy L Thompson         // Basis action
1687382e9c83SJeremy L Thompson         CeedCallBackend(CeedQFunctionFieldGetEvalMode(qf_output_fields[j], &eval_mode));
1688349fb27dSJeremy L Thompson         switch (eval_mode) {
1689349fb27dSJeremy L Thompson           case CEED_EVAL_NONE:
1690382e9c83SJeremy L Thompson             CeedCallBackend(CeedVectorRestoreArray(impl->e_vecs[j + impl->num_inputs], &e_data[j + num_input_fields]));
1691349fb27dSJeremy L Thompson             break;
1692349fb27dSJeremy L Thompson           case CEED_EVAL_INTERP:
1693349fb27dSJeremy L Thompson           case CEED_EVAL_GRAD:
1694349fb27dSJeremy L Thompson           case CEED_EVAL_DIV:
1695349fb27dSJeremy L Thompson           case CEED_EVAL_CURL:
1696382e9c83SJeremy L Thompson             CeedCallBackend(CeedOperatorFieldGetBasis(op_output_fields[j], &basis));
1697382e9c83SJeremy L Thompson             CeedCallBackend(CeedBasisApplyAtPoints(basis, num_elem, num_points, CEED_TRANSPOSE, eval_mode, impl->point_coords_elem,
1698382e9c83SJeremy L Thompson                                                    impl->q_vecs_out[j], impl->e_vecs[j + impl->num_inputs]));
1699349fb27dSJeremy L Thompson             break;
1700349fb27dSJeremy L Thompson           // LCOV_EXCL_START
1701349fb27dSJeremy L Thompson           case CEED_EVAL_WEIGHT: {
1702349fb27dSJeremy L Thompson             return CeedError(CeedOperatorReturnCeed(op), CEED_ERROR_BACKEND, "CEED_EVAL_WEIGHT cannot be an output evaluation mode");
1703349fb27dSJeremy L Thompson             // LCOV_EXCL_STOP
1704349fb27dSJeremy L Thompson           }
1705349fb27dSJeremy L Thompson         }
1706349fb27dSJeremy L Thompson 
1707349fb27dSJeremy L Thompson         // Mask output e-vec
1708382e9c83SJeremy L Thompson         CeedCallBackend(CeedVectorPointwiseMult(impl->e_vecs[j + impl->num_inputs], impl->e_vecs[i], impl->e_vecs[j + impl->num_inputs]));
1709349fb27dSJeremy L Thompson 
1710349fb27dSJeremy L Thompson         // Restrict
1711382e9c83SJeremy L Thompson         CeedCallBackend(CeedOperatorFieldGetElemRestriction(op_output_fields[j], &elem_rstr));
1712382e9c83SJeremy L Thompson         CeedCallBackend(CeedElemRestrictionApply(elem_rstr, CEED_TRANSPOSE, impl->e_vecs[j + impl->num_inputs], assembled, request));
1713349fb27dSJeremy L Thompson 
1714349fb27dSJeremy L Thompson         // Reset q_vec for
1715349fb27dSJeremy L Thompson         if (eval_mode == CEED_EVAL_NONE) {
1716382e9c83SJeremy L Thompson           CeedCallBackend(CeedVectorGetArrayWrite(impl->e_vecs[j + impl->num_inputs], CEED_MEM_DEVICE, &e_data[j + num_input_fields]));
1717382e9c83SJeremy L Thompson           CeedCallBackend(CeedVectorSetArray(impl->q_vecs_out[j], CEED_MEM_DEVICE, CEED_USE_POINTER, e_data[j + num_input_fields]));
1718349fb27dSJeremy L Thompson         }
1719349fb27dSJeremy L Thompson       }
1720382e9c83SJeremy L Thompson 
1721382e9c83SJeremy L Thompson       // Reset vec
172213062808SJeremy L Thompson       if (s == e_vec_size - 1 && i != num_input_fields - 1) CeedCallBackend(CeedVectorSetValue(impl->q_vecs_in[i], 0.0));
172386e10729SJeremy L Thompson     }
1724349fb27dSJeremy L Thompson   }
1725349fb27dSJeremy L Thompson 
1726349fb27dSJeremy L Thompson   // Restore CEED_EVAL_NONE
1727349fb27dSJeremy L Thompson   for (CeedInt i = 0; i < num_output_fields; i++) {
1728349fb27dSJeremy L Thompson     CeedEvalMode eval_mode;
1729349fb27dSJeremy L Thompson 
1730349fb27dSJeremy L Thompson     // Get eval_mode
1731349fb27dSJeremy L Thompson     CeedCallBackend(CeedQFunctionFieldGetEvalMode(qf_output_fields[i], &eval_mode));
1732349fb27dSJeremy L Thompson 
1733349fb27dSJeremy L Thompson     // Restore evec
1734349fb27dSJeremy L Thompson     CeedCallBackend(CeedQFunctionFieldGetEvalMode(qf_output_fields[i], &eval_mode));
1735349fb27dSJeremy L Thompson     if (eval_mode == CEED_EVAL_NONE) {
1736349fb27dSJeremy L Thompson       CeedCallBackend(CeedVectorRestoreArray(impl->e_vecs[i + impl->num_inputs], &e_data[i + num_input_fields]));
1737349fb27dSJeremy L Thompson     }
1738349fb27dSJeremy L Thompson   }
1739349fb27dSJeremy L Thompson 
1740349fb27dSJeremy L Thompson   // Restore input arrays
1741349fb27dSJeremy L Thompson   CeedCallBackend(CeedOperatorRestoreInputs_Cuda(num_input_fields, qf_input_fields, op_input_fields, true, e_data, impl));
1742349fb27dSJeremy L Thompson   return CEED_ERROR_SUCCESS;
1743756ca9e9SJeremy L Thompson }
1744756ca9e9SJeremy L Thompson 
1745756ca9e9SJeremy L Thompson //------------------------------------------------------------------------------
17460d0321e0SJeremy L Thompson // Create operator
17470d0321e0SJeremy L Thompson //------------------------------------------------------------------------------
17480d0321e0SJeremy L Thompson int CeedOperatorCreate_Cuda(CeedOperator op) {
17490d0321e0SJeremy L Thompson   Ceed               ceed;
17500d0321e0SJeremy L Thompson   CeedOperator_Cuda *impl;
17510d0321e0SJeremy L Thompson 
1752ca735530SJeremy L Thompson   CeedCallBackend(CeedOperatorGetCeed(op, &ceed));
17532b730f8bSJeremy L Thompson   CeedCallBackend(CeedCalloc(1, &impl));
17542b730f8bSJeremy L Thompson   CeedCallBackend(CeedOperatorSetData(op, impl));
17550d0321e0SJeremy L Thompson 
17562b730f8bSJeremy L Thompson   CeedCallBackend(CeedSetBackendFunction(ceed, "Operator", op, "LinearAssembleQFunction", CeedOperatorLinearAssembleQFunction_Cuda));
17572b730f8bSJeremy L Thompson   CeedCallBackend(CeedSetBackendFunction(ceed, "Operator", op, "LinearAssembleQFunctionUpdate", CeedOperatorLinearAssembleQFunctionUpdate_Cuda));
17582b730f8bSJeremy L Thompson   CeedCallBackend(CeedSetBackendFunction(ceed, "Operator", op, "LinearAssembleAddDiagonal", CeedOperatorLinearAssembleAddDiagonal_Cuda));
17592b730f8bSJeremy L Thompson   CeedCallBackend(
17602b730f8bSJeremy L Thompson       CeedSetBackendFunction(ceed, "Operator", op, "LinearAssembleAddPointBlockDiagonal", CeedOperatorLinearAssembleAddPointBlockDiagonal_Cuda));
17612b730f8bSJeremy L Thompson   CeedCallBackend(CeedSetBackendFunction(ceed, "Operator", op, "LinearAssembleSingle", CeedSingleOperatorAssemble_Cuda));
17622b730f8bSJeremy L Thompson   CeedCallBackend(CeedSetBackendFunction(ceed, "Operator", op, "ApplyAdd", CeedOperatorApplyAdd_Cuda));
17632b730f8bSJeremy L Thompson   CeedCallBackend(CeedSetBackendFunction(ceed, "Operator", op, "Destroy", CeedOperatorDestroy_Cuda));
17640d0321e0SJeremy L Thompson   return CEED_ERROR_SUCCESS;
17650d0321e0SJeremy L Thompson }
17660d0321e0SJeremy L Thompson 
17670d0321e0SJeremy L Thompson //------------------------------------------------------------------------------
176867d9480aSJeremy L Thompson // Create operator AtPoints
1769756ca9e9SJeremy L Thompson //------------------------------------------------------------------------------
1770756ca9e9SJeremy L Thompson int CeedOperatorCreateAtPoints_Cuda(CeedOperator op) {
1771756ca9e9SJeremy L Thompson   Ceed               ceed;
1772756ca9e9SJeremy L Thompson   CeedOperator_Cuda *impl;
1773756ca9e9SJeremy L Thompson 
1774756ca9e9SJeremy L Thompson   CeedCallBackend(CeedOperatorGetCeed(op, &ceed));
1775756ca9e9SJeremy L Thompson   CeedCallBackend(CeedCalloc(1, &impl));
1776756ca9e9SJeremy L Thompson   CeedCallBackend(CeedOperatorSetData(op, impl));
1777756ca9e9SJeremy L Thompson 
1778756ca9e9SJeremy L Thompson   CeedCallBackend(CeedSetBackendFunction(ceed, "Operator", op, "LinearAssembleQFunction", CeedOperatorLinearAssembleQFunctionAtPoints_Cuda));
1779756ca9e9SJeremy L Thompson   CeedCallBackend(CeedSetBackendFunction(ceed, "Operator", op, "LinearAssembleAddDiagonal", CeedOperatorLinearAssembleAddDiagonalAtPoints_Cuda));
1780756ca9e9SJeremy L Thompson   CeedCallBackend(CeedSetBackendFunction(ceed, "Operator", op, "ApplyAdd", CeedOperatorApplyAddAtPoints_Cuda));
1781756ca9e9SJeremy L Thompson   CeedCallBackend(CeedSetBackendFunction(ceed, "Operator", op, "Destroy", CeedOperatorDestroy_Cuda));
1782756ca9e9SJeremy L Thompson   return CEED_ERROR_SUCCESS;
1783756ca9e9SJeremy L Thompson }
1784756ca9e9SJeremy L Thompson 
1785756ca9e9SJeremy L Thompson //------------------------------------------------------------------------------
1786