xref: /libCEED/backends/cuda-ref/ceed-cuda-ref-operator.c (revision 8a21357021cf95ebb58183dc35a646c90195a1f2)
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));
31f8a0df59SJeremy L Thompson   CeedCallBackend(CeedFree(&impl->skip_rstr_out));
32f8a0df59SJeremy 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 //------------------------------------------------------------------------------
102f8a0df59SJeremy L Thompson static int CeedOperatorSetupFields_Cuda(CeedQFunction qf, CeedOperator op, bool is_input, bool is_at_points, bool *skip_rstr, bool *apply_add_basis,
103f8a0df59SJeremy 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   }
189f8a0df59SJeremy 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) {
204f8a0df59SJeremy 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     }
209f8a0df59SJeremy L Thompson   } else {
210f8a0df59SJeremy L Thompson     for (CeedInt i = num_fields - 1; i >= 0; i--) {
211f8a0df59SJeremy L Thompson       CeedVector          vec_i;
212f8a0df59SJeremy L Thompson       CeedElemRestriction rstr_i;
213f8a0df59SJeremy L Thompson 
214f8a0df59SJeremy L Thompson       CeedCallBackend(CeedOperatorFieldGetVector(op_fields[i], &vec_i));
215f8a0df59SJeremy L Thompson       CeedCallBackend(CeedOperatorFieldGetElemRestriction(op_fields[i], &rstr_i));
216f8a0df59SJeremy L Thompson       for (CeedInt j = i - 1; j >= 0; j--) {
217f8a0df59SJeremy L Thompson         CeedVector          vec_j;
218f8a0df59SJeremy L Thompson         CeedElemRestriction rstr_j;
219f8a0df59SJeremy L Thompson 
220f8a0df59SJeremy L Thompson         CeedCallBackend(CeedOperatorFieldGetVector(op_fields[j], &vec_j));
221f8a0df59SJeremy L Thompson         CeedCallBackend(CeedOperatorFieldGetElemRestriction(op_fields[j], &rstr_j));
222f8a0df59SJeremy L Thompson         if (vec_i == vec_j && rstr_i == rstr_j) {
223f8a0df59SJeremy L Thompson           CeedCallBackend(CeedVectorReferenceCopy(e_vecs[i + start_e], &e_vecs[j + start_e]));
224f8a0df59SJeremy L Thompson           skip_rstr[j]       = true;
225f8a0df59SJeremy L Thompson           apply_add_basis[i] = true;
226f8a0df59SJeremy L Thompson         }
227f8a0df59SJeremy L Thompson       }
228f8a0df59SJeremy 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));
259f8a0df59SJeremy L Thompson   CeedCallBackend(CeedCalloc(CEED_FIELD_MAX, &impl->skip_rstr_out));
260f8a0df59SJeremy 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 
26741655a23SJeremy L Thompson   // Set up infield and outfield e-vecs and q-vecs
2680d0321e0SJeremy L Thompson   // Infields
2693aab95c0SJeremy L Thompson   CeedCallBackend(
270f8a0df59SJeremy 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
272f8a0df59SJeremy 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,
273f8a0df59SJeremy L Thompson                                                num_input_fields, num_output_fields, Q, num_elem));
2740d0321e0SJeremy L Thompson 
27541655a23SJeremy L Thompson   // Reuse active e-vecs where able
27641655a23SJeremy L Thompson   {
27741655a23SJeremy L Thompson     CeedInt              num_used  = 0;
27841655a23SJeremy L Thompson     CeedElemRestriction *rstr_used = NULL;
27941655a23SJeremy L Thompson 
28041655a23SJeremy L Thompson     for (CeedInt i = 0; i < num_input_fields; i++) {
28141655a23SJeremy L Thompson       bool                is_used = false;
28241655a23SJeremy L Thompson       CeedVector          vec_i;
28341655a23SJeremy L Thompson       CeedElemRestriction rstr_i;
28441655a23SJeremy L Thompson 
28541655a23SJeremy L Thompson       CeedCallBackend(CeedOperatorFieldGetVector(op_input_fields[i], &vec_i));
28641655a23SJeremy L Thompson       if (vec_i != CEED_VECTOR_ACTIVE) continue;
28741655a23SJeremy L Thompson       CeedCallBackend(CeedOperatorFieldGetElemRestriction(op_input_fields[i], &rstr_i));
28841655a23SJeremy L Thompson       for (CeedInt j = 0; j < num_used; j++) {
28941655a23SJeremy L Thompson         if (rstr_i == rstr_used[i]) is_used = true;
29041655a23SJeremy L Thompson       }
29141655a23SJeremy L Thompson       if (is_used) continue;
29241655a23SJeremy L Thompson       num_used++;
29341655a23SJeremy L Thompson       if (num_used == 1) CeedCallBackend(CeedCalloc(num_used, &rstr_used));
29441655a23SJeremy L Thompson       else CeedCallBackend(CeedRealloc(num_used, &rstr_used));
29541655a23SJeremy L Thompson       rstr_used[num_used - 1] = rstr_i;
29641655a23SJeremy L Thompson       for (CeedInt j = num_output_fields - 1; j >= 0; j--) {
29741655a23SJeremy L Thompson         CeedEvalMode        eval_mode;
29841655a23SJeremy L Thompson         CeedVector          vec_j;
29941655a23SJeremy L Thompson         CeedElemRestriction rstr_j;
30041655a23SJeremy L Thompson 
30141655a23SJeremy L Thompson         CeedCallBackend(CeedOperatorFieldGetVector(op_output_fields[j], &vec_j));
30241655a23SJeremy L Thompson         if (vec_j != CEED_VECTOR_ACTIVE) continue;
30341655a23SJeremy L Thompson         CeedCallBackend(CeedQFunctionFieldGetEvalMode(qf_output_fields[j], &eval_mode));
30441655a23SJeremy L Thompson         if (eval_mode == CEED_EVAL_NONE) continue;
30541655a23SJeremy L Thompson         CeedCallBackend(CeedOperatorFieldGetElemRestriction(op_output_fields[j], &rstr_j));
30641655a23SJeremy L Thompson         if (rstr_i == rstr_j) {
30741655a23SJeremy L Thompson           CeedCallBackend(CeedVectorReferenceCopy(impl->e_vecs[i], &impl->e_vecs[j + impl->num_inputs]));
30841655a23SJeremy L Thompson         }
30941655a23SJeremy L Thompson       }
31041655a23SJeremy L Thompson     }
31141655a23SJeremy L Thompson     CeedCallBackend(CeedFree(&rstr_used));
31241655a23SJeremy L Thompson   }
313*8a213570SJeremy L Thompson   impl->has_shared_e_vecs = true;
3142b730f8bSJeremy L Thompson   CeedCallBackend(CeedOperatorSetSetupDone(op));
3150d0321e0SJeremy L Thompson   return CEED_ERROR_SUCCESS;
3160d0321e0SJeremy L Thompson }
3170d0321e0SJeremy L Thompson 
3180d0321e0SJeremy L Thompson //------------------------------------------------------------------------------
3190d0321e0SJeremy L Thompson // Setup Operator Inputs
3200d0321e0SJeremy L Thompson //------------------------------------------------------------------------------
321ca735530SJeremy L Thompson static inline int CeedOperatorSetupInputs_Cuda(CeedInt num_input_fields, CeedQFunctionField *qf_input_fields, CeedOperatorField *op_input_fields,
322004e4986SSebastian Grimberg                                                CeedVector in_vec, const bool skip_active, CeedScalar *e_data[2 * CEED_FIELD_MAX],
3230d0321e0SJeremy L Thompson                                                CeedOperator_Cuda *impl, CeedRequest *request) {
324ca735530SJeremy L Thompson   for (CeedInt i = 0; i < num_input_fields; i++) {
325004e4986SSebastian Grimberg     CeedEvalMode        eval_mode;
3260d0321e0SJeremy L Thompson     CeedVector          vec;
327edb2538eSJeremy L Thompson     CeedElemRestriction elem_rstr;
3280d0321e0SJeremy L Thompson 
3290d0321e0SJeremy L Thompson     // Get input vector
330ca735530SJeremy L Thompson     CeedCallBackend(CeedOperatorFieldGetVector(op_input_fields[i], &vec));
3310d0321e0SJeremy L Thompson     if (vec == CEED_VECTOR_ACTIVE) {
332004e4986SSebastian Grimberg       if (skip_active) continue;
333ca735530SJeremy L Thompson       else vec = in_vec;
3340d0321e0SJeremy L Thompson     }
3350d0321e0SJeremy L Thompson 
336004e4986SSebastian Grimberg     CeedCallBackend(CeedQFunctionFieldGetEvalMode(qf_input_fields[i], &eval_mode));
337004e4986SSebastian Grimberg     if (eval_mode == CEED_EVAL_WEIGHT) {  // Skip
3380d0321e0SJeremy L Thompson     } else {
339004e4986SSebastian Grimberg       // Get input vector
340004e4986SSebastian Grimberg       CeedCallBackend(CeedOperatorFieldGetVector(op_input_fields[i], &vec));
3410d0321e0SJeremy L Thompson       // Get input element restriction
342edb2538eSJeremy L Thompson       CeedCallBackend(CeedOperatorFieldGetElemRestriction(op_input_fields[i], &elem_rstr));
343ca735530SJeremy L Thompson       if (vec == CEED_VECTOR_ACTIVE) vec = in_vec;
3440d0321e0SJeremy L Thompson       // Restrict, if necessary
345ca735530SJeremy L Thompson       if (!impl->e_vecs[i]) {
3460d0321e0SJeremy L Thompson         // No restriction for this field; read data directly from vec.
347ca735530SJeremy L Thompson         CeedCallBackend(CeedVectorGetArrayRead(vec, CEED_MEM_DEVICE, (const CeedScalar **)&e_data[i]));
3480d0321e0SJeremy L Thompson       } else {
349c1222711SJeremy L Thompson         uint64_t state;
350c1222711SJeremy L Thompson 
351c1222711SJeremy L Thompson         CeedCallBackend(CeedVectorGetState(vec, &state));
35241655a23SJeremy L Thompson         if ((state != impl->input_states[i] || vec == in_vec) && !impl->skip_rstr_in[i]) {
353edb2538eSJeremy L Thompson           CeedCallBackend(CeedElemRestrictionApply(elem_rstr, CEED_NOTRANSPOSE, vec, impl->e_vecs[i], request));
354c1222711SJeremy L Thompson         }
3553aab95c0SJeremy L Thompson         impl->input_states[i] = state;
3560d0321e0SJeremy L Thompson         // Get evec
357ca735530SJeremy L Thompson         CeedCallBackend(CeedVectorGetArrayRead(impl->e_vecs[i], CEED_MEM_DEVICE, (const CeedScalar **)&e_data[i]));
3580d0321e0SJeremy L Thompson       }
3590d0321e0SJeremy L Thompson     }
3600d0321e0SJeremy L Thompson   }
3610d0321e0SJeremy L Thompson   return CEED_ERROR_SUCCESS;
3620d0321e0SJeremy L Thompson }
3630d0321e0SJeremy L Thompson 
3640d0321e0SJeremy L Thompson //------------------------------------------------------------------------------
3650d0321e0SJeremy L Thompson // Input Basis Action
3660d0321e0SJeremy L Thompson //------------------------------------------------------------------------------
367ca735530SJeremy L Thompson static inline int CeedOperatorInputBasis_Cuda(CeedInt num_elem, CeedQFunctionField *qf_input_fields, CeedOperatorField *op_input_fields,
368004e4986SSebastian Grimberg                                               CeedInt num_input_fields, const bool skip_active, CeedScalar *e_data[2 * CEED_FIELD_MAX],
3692b730f8bSJeremy L Thompson                                               CeedOperator_Cuda *impl) {
370ca735530SJeremy L Thompson   for (CeedInt i = 0; i < num_input_fields; i++) {
371ca735530SJeremy L Thompson     CeedInt             elem_size, size;
372004e4986SSebastian Grimberg     CeedEvalMode        eval_mode;
373edb2538eSJeremy L Thompson     CeedElemRestriction elem_rstr;
3740d0321e0SJeremy L Thompson     CeedBasis           basis;
3750d0321e0SJeremy L Thompson 
3760d0321e0SJeremy L Thompson     // Skip active input
377004e4986SSebastian Grimberg     if (skip_active) {
3780d0321e0SJeremy L Thompson       CeedVector vec;
379ca735530SJeremy L Thompson 
380ca735530SJeremy L Thompson       CeedCallBackend(CeedOperatorFieldGetVector(op_input_fields[i], &vec));
3812b730f8bSJeremy L Thompson       if (vec == CEED_VECTOR_ACTIVE) continue;
3820d0321e0SJeremy L Thompson     }
383004e4986SSebastian Grimberg     // Get elem_size, eval_mode, size
384edb2538eSJeremy L Thompson     CeedCallBackend(CeedOperatorFieldGetElemRestriction(op_input_fields[i], &elem_rstr));
385edb2538eSJeremy L Thompson     CeedCallBackend(CeedElemRestrictionGetElementSize(elem_rstr, &elem_size));
386004e4986SSebastian Grimberg     CeedCallBackend(CeedQFunctionFieldGetEvalMode(qf_input_fields[i], &eval_mode));
387ca735530SJeremy L Thompson     CeedCallBackend(CeedQFunctionFieldGetSize(qf_input_fields[i], &size));
3880d0321e0SJeremy L Thompson     // Basis action
389004e4986SSebastian Grimberg     switch (eval_mode) {
3900d0321e0SJeremy L Thompson       case CEED_EVAL_NONE:
391ca735530SJeremy L Thompson         CeedCallBackend(CeedVectorSetArray(impl->q_vecs_in[i], CEED_MEM_DEVICE, CEED_USE_POINTER, e_data[i]));
3920d0321e0SJeremy L Thompson         break;
3930d0321e0SJeremy L Thompson       case CEED_EVAL_INTERP:
3940d0321e0SJeremy L Thompson       case CEED_EVAL_GRAD:
395004e4986SSebastian Grimberg       case CEED_EVAL_DIV:
396004e4986SSebastian Grimberg       case CEED_EVAL_CURL:
397ca735530SJeremy L Thompson         CeedCallBackend(CeedOperatorFieldGetBasis(op_input_fields[i], &basis));
398004e4986SSebastian Grimberg         CeedCallBackend(CeedBasisApply(basis, num_elem, CEED_NOTRANSPOSE, eval_mode, impl->e_vecs[i], impl->q_vecs_in[i]));
3990d0321e0SJeremy L Thompson         break;
4000d0321e0SJeremy L Thompson       case CEED_EVAL_WEIGHT:
4010d0321e0SJeremy L Thompson         break;  // No action
4020d0321e0SJeremy L Thompson     }
4030d0321e0SJeremy L Thompson   }
4040d0321e0SJeremy L Thompson   return CEED_ERROR_SUCCESS;
4050d0321e0SJeremy L Thompson }
4060d0321e0SJeremy L Thompson 
4070d0321e0SJeremy L Thompson //------------------------------------------------------------------------------
4080d0321e0SJeremy L Thompson // Restore Input Vectors
4090d0321e0SJeremy L Thompson //------------------------------------------------------------------------------
410ca735530SJeremy L Thompson static inline int CeedOperatorRestoreInputs_Cuda(CeedInt num_input_fields, CeedQFunctionField *qf_input_fields, CeedOperatorField *op_input_fields,
411004e4986SSebastian Grimberg                                                  const bool skip_active, CeedScalar *e_data[2 * CEED_FIELD_MAX], CeedOperator_Cuda *impl) {
412ca735530SJeremy L Thompson   for (CeedInt i = 0; i < num_input_fields; i++) {
413004e4986SSebastian Grimberg     CeedEvalMode eval_mode;
4140d0321e0SJeremy L Thompson     CeedVector   vec;
4150d0321e0SJeremy L Thompson 
4160d0321e0SJeremy L Thompson     // Skip active input
417004e4986SSebastian Grimberg     if (skip_active) {
418ca735530SJeremy L Thompson       CeedCallBackend(CeedOperatorFieldGetVector(op_input_fields[i], &vec));
4192b730f8bSJeremy L Thompson       if (vec == CEED_VECTOR_ACTIVE) continue;
4200d0321e0SJeremy L Thompson     }
421004e4986SSebastian Grimberg     CeedCallBackend(CeedQFunctionFieldGetEvalMode(qf_input_fields[i], &eval_mode));
422004e4986SSebastian Grimberg     if (eval_mode == CEED_EVAL_WEIGHT) {  // Skip
4230d0321e0SJeremy L Thompson     } else {
424ca735530SJeremy L Thompson       if (!impl->e_vecs[i]) {  // This was a skip_restriction case
425ca735530SJeremy L Thompson         CeedCallBackend(CeedOperatorFieldGetVector(op_input_fields[i], &vec));
426ca735530SJeremy L Thompson         CeedCallBackend(CeedVectorRestoreArrayRead(vec, (const CeedScalar **)&e_data[i]));
4270d0321e0SJeremy L Thompson       } else {
428ca735530SJeremy L Thompson         CeedCallBackend(CeedVectorRestoreArrayRead(impl->e_vecs[i], (const CeedScalar **)&e_data[i]));
4290d0321e0SJeremy L Thompson       }
4300d0321e0SJeremy L Thompson     }
4310d0321e0SJeremy L Thompson   }
4320d0321e0SJeremy L Thompson   return CEED_ERROR_SUCCESS;
4330d0321e0SJeremy L Thompson }
4340d0321e0SJeremy L Thompson 
4350d0321e0SJeremy L Thompson //------------------------------------------------------------------------------
4360d0321e0SJeremy L Thompson // Apply and add to output
4370d0321e0SJeremy L Thompson //------------------------------------------------------------------------------
438ca735530SJeremy L Thompson static int CeedOperatorApplyAdd_Cuda(CeedOperator op, CeedVector in_vec, CeedVector out_vec, CeedRequest *request) {
439ca735530SJeremy L Thompson   CeedInt             Q, num_elem, elem_size, num_input_fields, num_output_fields, size;
440ca735530SJeremy L Thompson   CeedScalar         *e_data[2 * CEED_FIELD_MAX] = {NULL};
441ca735530SJeremy L Thompson   CeedQFunctionField *qf_input_fields, *qf_output_fields;
4420d0321e0SJeremy L Thompson   CeedQFunction       qf;
443004e4986SSebastian Grimberg   CeedOperatorField  *op_input_fields, *op_output_fields;
444004e4986SSebastian Grimberg   CeedOperator_Cuda  *impl;
445ca735530SJeremy L Thompson 
446ca735530SJeremy L Thompson   CeedCallBackend(CeedOperatorGetData(op, &impl));
4472b730f8bSJeremy L Thompson   CeedCallBackend(CeedOperatorGetQFunction(op, &qf));
4482b730f8bSJeremy L Thompson   CeedCallBackend(CeedOperatorGetNumQuadraturePoints(op, &Q));
449ca735530SJeremy L Thompson   CeedCallBackend(CeedOperatorGetNumElements(op, &num_elem));
450ca735530SJeremy L Thompson   CeedCallBackend(CeedOperatorGetFields(op, &num_input_fields, &op_input_fields, &num_output_fields, &op_output_fields));
451ca735530SJeremy L Thompson   CeedCallBackend(CeedQFunctionGetFields(qf, NULL, &qf_input_fields, NULL, &qf_output_fields));
4520d0321e0SJeremy L Thompson 
4530d0321e0SJeremy L Thompson   // Setup
4542b730f8bSJeremy L Thompson   CeedCallBackend(CeedOperatorSetup_Cuda(op));
4550d0321e0SJeremy L Thompson 
456004e4986SSebastian Grimberg   // Input Evecs and Restriction
457ca735530SJeremy L Thompson   CeedCallBackend(CeedOperatorSetupInputs_Cuda(num_input_fields, qf_input_fields, op_input_fields, in_vec, false, e_data, impl, request));
4580d0321e0SJeremy L Thompson 
4590d0321e0SJeremy L Thompson   // Input basis apply if needed
460ca735530SJeremy L Thompson   CeedCallBackend(CeedOperatorInputBasis_Cuda(num_elem, qf_input_fields, op_input_fields, num_input_fields, false, e_data, impl));
4610d0321e0SJeremy L Thompson 
4620d0321e0SJeremy L Thompson   // Output pointers, as necessary
463ca735530SJeremy L Thompson   for (CeedInt i = 0; i < num_output_fields; i++) {
464004e4986SSebastian Grimberg     CeedEvalMode eval_mode;
465004e4986SSebastian Grimberg 
466004e4986SSebastian Grimberg     CeedCallBackend(CeedQFunctionFieldGetEvalMode(qf_output_fields[i], &eval_mode));
467004e4986SSebastian Grimberg     if (eval_mode == CEED_EVAL_NONE) {
4680d0321e0SJeremy L Thompson       // Set the output Q-Vector to use the E-Vector data directly.
469ca735530SJeremy L Thompson       CeedCallBackend(CeedVectorGetArrayWrite(impl->e_vecs[i + impl->num_inputs], CEED_MEM_DEVICE, &e_data[i + num_input_fields]));
470ca735530SJeremy L Thompson       CeedCallBackend(CeedVectorSetArray(impl->q_vecs_out[i], CEED_MEM_DEVICE, CEED_USE_POINTER, e_data[i + num_input_fields]));
4710d0321e0SJeremy L Thompson     }
4720d0321e0SJeremy L Thompson   }
4730d0321e0SJeremy L Thompson 
4740d0321e0SJeremy L Thompson   // Q function
475ca735530SJeremy L Thompson   CeedCallBackend(CeedQFunctionApply(qf, num_elem * Q, impl->q_vecs_in, impl->q_vecs_out));
4760d0321e0SJeremy L Thompson 
47741655a23SJeremy L Thompson   // Restore input arrays
47841655a23SJeremy L Thompson   CeedCallBackend(CeedOperatorRestoreInputs_Cuda(num_input_fields, qf_input_fields, op_input_fields, false, e_data, impl));
47941655a23SJeremy L Thompson 
4800d0321e0SJeremy L Thompson   // Output basis apply if needed
481ca735530SJeremy L Thompson   for (CeedInt i = 0; i < num_output_fields; i++) {
482004e4986SSebastian Grimberg     CeedEvalMode        eval_mode;
483edb2538eSJeremy L Thompson     CeedElemRestriction elem_rstr;
484ca735530SJeremy L Thompson     CeedBasis           basis;
485ca735530SJeremy L Thompson 
486004e4986SSebastian Grimberg     // Get elem_size, eval_mode, size
487edb2538eSJeremy L Thompson     CeedCallBackend(CeedOperatorFieldGetElemRestriction(op_output_fields[i], &elem_rstr));
488edb2538eSJeremy L Thompson     CeedCallBackend(CeedElemRestrictionGetElementSize(elem_rstr, &elem_size));
489004e4986SSebastian Grimberg     CeedCallBackend(CeedQFunctionFieldGetEvalMode(qf_output_fields[i], &eval_mode));
490ca735530SJeremy L Thompson     CeedCallBackend(CeedQFunctionFieldGetSize(qf_output_fields[i], &size));
4910d0321e0SJeremy L Thompson     // Basis action
492004e4986SSebastian Grimberg     switch (eval_mode) {
4930d0321e0SJeremy L Thompson       case CEED_EVAL_NONE:
494004e4986SSebastian Grimberg         break;  // No action
4950d0321e0SJeremy L Thompson       case CEED_EVAL_INTERP:
4960d0321e0SJeremy L Thompson       case CEED_EVAL_GRAD:
497004e4986SSebastian Grimberg       case CEED_EVAL_DIV:
498004e4986SSebastian Grimberg       case CEED_EVAL_CURL:
499ca735530SJeremy L Thompson         CeedCallBackend(CeedOperatorFieldGetBasis(op_output_fields[i], &basis));
500f8a0df59SJeremy L Thompson         if (impl->apply_add_basis_out[i]) {
501f8a0df59SJeremy L Thompson           CeedCallBackend(CeedBasisApplyAdd(basis, num_elem, CEED_TRANSPOSE, eval_mode, impl->q_vecs_out[i], impl->e_vecs[i + impl->num_inputs]));
502f8a0df59SJeremy L Thompson         } else {
503004e4986SSebastian Grimberg           CeedCallBackend(CeedBasisApply(basis, num_elem, CEED_TRANSPOSE, eval_mode, impl->q_vecs_out[i], impl->e_vecs[i + impl->num_inputs]));
504f8a0df59SJeremy L Thompson         }
5050d0321e0SJeremy L Thompson         break;
5060d0321e0SJeremy L Thompson       // LCOV_EXCL_START
5070d0321e0SJeremy L Thompson       case CEED_EVAL_WEIGHT: {
5086e536b99SJeremy L Thompson         return CeedError(CeedOperatorReturnCeed(op), CEED_ERROR_BACKEND, "CEED_EVAL_WEIGHT cannot be an output evaluation mode");
5090d0321e0SJeremy L Thompson         // LCOV_EXCL_STOP
5100d0321e0SJeremy L Thompson       }
5110d0321e0SJeremy L Thompson     }
512004e4986SSebastian Grimberg   }
5130d0321e0SJeremy L Thompson 
5140d0321e0SJeremy L Thompson   // Output restriction
515ca735530SJeremy L Thompson   for (CeedInt i = 0; i < num_output_fields; i++) {
516004e4986SSebastian Grimberg     CeedEvalMode        eval_mode;
517ca735530SJeremy L Thompson     CeedVector          vec;
518edb2538eSJeremy L Thompson     CeedElemRestriction elem_rstr;
519ca735530SJeremy L Thompson 
5200d0321e0SJeremy L Thompson     // Restore evec
521004e4986SSebastian Grimberg     CeedCallBackend(CeedQFunctionFieldGetEvalMode(qf_output_fields[i], &eval_mode));
522004e4986SSebastian Grimberg     if (eval_mode == CEED_EVAL_NONE) {
523ca735530SJeremy L Thompson       CeedCallBackend(CeedVectorRestoreArray(impl->e_vecs[i + impl->num_inputs], &e_data[i + num_input_fields]));
5240d0321e0SJeremy L Thompson     }
525f8a0df59SJeremy L Thompson     if (impl->skip_rstr_out[i]) continue;
5260d0321e0SJeremy L Thompson     // Get output vector
527ca735530SJeremy L Thompson     CeedCallBackend(CeedOperatorFieldGetVector(op_output_fields[i], &vec));
5280d0321e0SJeremy L Thompson     // Restrict
529edb2538eSJeremy L Thompson     CeedCallBackend(CeedOperatorFieldGetElemRestriction(op_output_fields[i], &elem_rstr));
5300d0321e0SJeremy L Thompson     // Active
531ca735530SJeremy L Thompson     if (vec == CEED_VECTOR_ACTIVE) vec = out_vec;
5320d0321e0SJeremy L Thompson 
533edb2538eSJeremy L Thompson     CeedCallBackend(CeedElemRestrictionApply(elem_rstr, CEED_TRANSPOSE, impl->e_vecs[i + impl->num_inputs], vec, request));
5340d0321e0SJeremy L Thompson   }
5350d0321e0SJeremy L Thompson   return CEED_ERROR_SUCCESS;
5360d0321e0SJeremy L Thompson }
5370d0321e0SJeremy L Thompson 
5380d0321e0SJeremy L Thompson //------------------------------------------------------------------------------
539756ca9e9SJeremy L Thompson // CeedOperator needs to connect all the named fields (be they active or passive) to the named inputs and outputs of its CeedQFunction.
540756ca9e9SJeremy L Thompson //------------------------------------------------------------------------------
541756ca9e9SJeremy L Thompson static int CeedOperatorSetupAtPoints_Cuda(CeedOperator op) {
542756ca9e9SJeremy L Thompson   Ceed                ceed;
543756ca9e9SJeremy L Thompson   bool                is_setup_done;
544756ca9e9SJeremy L Thompson   CeedInt             max_num_points = -1, num_elem, num_input_fields, num_output_fields;
545756ca9e9SJeremy L Thompson   CeedQFunctionField *qf_input_fields, *qf_output_fields;
546756ca9e9SJeremy L Thompson   CeedQFunction       qf;
547756ca9e9SJeremy L Thompson   CeedOperatorField  *op_input_fields, *op_output_fields;
548756ca9e9SJeremy L Thompson   CeedOperator_Cuda  *impl;
549756ca9e9SJeremy L Thompson 
550756ca9e9SJeremy L Thompson   CeedCallBackend(CeedOperatorIsSetupDone(op, &is_setup_done));
551756ca9e9SJeremy L Thompson   if (is_setup_done) return CEED_ERROR_SUCCESS;
552756ca9e9SJeremy L Thompson 
553756ca9e9SJeremy L Thompson   CeedCallBackend(CeedOperatorGetCeed(op, &ceed));
554756ca9e9SJeremy L Thompson   CeedCallBackend(CeedOperatorGetData(op, &impl));
555756ca9e9SJeremy L Thompson   CeedCallBackend(CeedOperatorGetQFunction(op, &qf));
556756ca9e9SJeremy L Thompson   CeedCallBackend(CeedOperatorGetNumElements(op, &num_elem));
557756ca9e9SJeremy L Thompson   CeedCallBackend(CeedOperatorGetFields(op, &num_input_fields, &op_input_fields, &num_output_fields, &op_output_fields));
558756ca9e9SJeremy L Thompson   CeedCallBackend(CeedQFunctionGetFields(qf, NULL, &qf_input_fields, NULL, &qf_output_fields));
559756ca9e9SJeremy L Thompson   {
560756ca9e9SJeremy L Thompson     CeedElemRestriction elem_rstr = NULL;
561756ca9e9SJeremy L Thompson 
562756ca9e9SJeremy L Thompson     CeedCallBackend(CeedOperatorAtPointsGetPoints(op, &elem_rstr, NULL));
563756ca9e9SJeremy L Thompson     CeedCallBackend(CeedElemRestrictionGetMaxPointsInElement(elem_rstr, &max_num_points));
564756ca9e9SJeremy L Thompson   }
565756ca9e9SJeremy L Thompson   impl->max_num_points = max_num_points;
566756ca9e9SJeremy L Thompson 
567756ca9e9SJeremy L Thompson   // Allocate
568756ca9e9SJeremy L Thompson   CeedCallBackend(CeedCalloc(num_input_fields + num_output_fields, &impl->e_vecs));
5693aab95c0SJeremy L Thompson   CeedCallBackend(CeedCalloc(CEED_FIELD_MAX, &impl->skip_rstr_in));
570f8a0df59SJeremy L Thompson   CeedCallBackend(CeedCalloc(CEED_FIELD_MAX, &impl->skip_rstr_out));
571f8a0df59SJeremy L Thompson   CeedCallBackend(CeedCalloc(CEED_FIELD_MAX, &impl->apply_add_basis_out));
572756ca9e9SJeremy L Thompson   CeedCallBackend(CeedCalloc(CEED_FIELD_MAX, &impl->input_states));
573756ca9e9SJeremy L Thompson   CeedCallBackend(CeedCalloc(CEED_FIELD_MAX, &impl->q_vecs_in));
574756ca9e9SJeremy L Thompson   CeedCallBackend(CeedCalloc(CEED_FIELD_MAX, &impl->q_vecs_out));
575756ca9e9SJeremy L Thompson   impl->num_inputs  = num_input_fields;
576756ca9e9SJeremy L Thompson   impl->num_outputs = num_output_fields;
577756ca9e9SJeremy L Thompson 
578*8a213570SJeremy L Thompson   // Set up infield and outfield e-vecs and q-vecs
579756ca9e9SJeremy L Thompson   // Infields
580f8a0df59SJeremy 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,
5813aab95c0SJeremy L Thompson                                                max_num_points, num_elem));
582756ca9e9SJeremy L Thompson   // Outfields
583f8a0df59SJeremy 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,
584f8a0df59SJeremy L Thompson                                                num_input_fields, num_output_fields, max_num_points, num_elem));
585756ca9e9SJeremy L Thompson 
586*8a213570SJeremy L Thompson   // Reuse active e-vecs where able
587*8a213570SJeremy L Thompson   {
588*8a213570SJeremy L Thompson     CeedInt              num_used  = 0;
589*8a213570SJeremy L Thompson     CeedElemRestriction *rstr_used = NULL;
590*8a213570SJeremy L Thompson 
591*8a213570SJeremy L Thompson     for (CeedInt i = 0; i < num_input_fields; i++) {
592*8a213570SJeremy L Thompson       bool                is_used = false;
593*8a213570SJeremy L Thompson       CeedVector          vec_i;
594*8a213570SJeremy L Thompson       CeedElemRestriction rstr_i;
595*8a213570SJeremy L Thompson 
596*8a213570SJeremy L Thompson       CeedCallBackend(CeedOperatorFieldGetVector(op_input_fields[i], &vec_i));
597*8a213570SJeremy L Thompson       if (vec_i != CEED_VECTOR_ACTIVE) continue;
598*8a213570SJeremy L Thompson       CeedCallBackend(CeedOperatorFieldGetElemRestriction(op_input_fields[i], &rstr_i));
599*8a213570SJeremy L Thompson       for (CeedInt j = 0; j < num_used; j++) {
600*8a213570SJeremy L Thompson         if (rstr_i == rstr_used[i]) is_used = true;
601*8a213570SJeremy L Thompson       }
602*8a213570SJeremy L Thompson       if (is_used) continue;
603*8a213570SJeremy L Thompson       num_used++;
604*8a213570SJeremy L Thompson       if (num_used == 1) CeedCallBackend(CeedCalloc(num_used, &rstr_used));
605*8a213570SJeremy L Thompson       else CeedCallBackend(CeedRealloc(num_used, &rstr_used));
606*8a213570SJeremy L Thompson       rstr_used[num_used - 1] = rstr_i;
607*8a213570SJeremy L Thompson       for (CeedInt j = num_output_fields - 1; j >= 0; j--) {
608*8a213570SJeremy L Thompson         CeedEvalMode        eval_mode;
609*8a213570SJeremy L Thompson         CeedVector          vec_j;
610*8a213570SJeremy L Thompson         CeedElemRestriction rstr_j;
611*8a213570SJeremy L Thompson 
612*8a213570SJeremy L Thompson         CeedCallBackend(CeedOperatorFieldGetVector(op_output_fields[j], &vec_j));
613*8a213570SJeremy L Thompson         if (vec_j != CEED_VECTOR_ACTIVE) continue;
614*8a213570SJeremy L Thompson         CeedCallBackend(CeedQFunctionFieldGetEvalMode(qf_output_fields[j], &eval_mode));
615*8a213570SJeremy L Thompson         if (eval_mode == CEED_EVAL_NONE) continue;
616*8a213570SJeremy L Thompson         CeedCallBackend(CeedOperatorFieldGetElemRestriction(op_output_fields[j], &rstr_j));
617*8a213570SJeremy L Thompson         if (rstr_i == rstr_j) {
618*8a213570SJeremy L Thompson           CeedCallBackend(CeedVectorReferenceCopy(impl->e_vecs[i], &impl->e_vecs[j + impl->num_inputs]));
619*8a213570SJeremy L Thompson         }
620*8a213570SJeremy L Thompson       }
621*8a213570SJeremy L Thompson     }
622*8a213570SJeremy L Thompson     CeedCallBackend(CeedFree(&rstr_used));
623*8a213570SJeremy L Thompson   }
624*8a213570SJeremy L Thompson   impl->has_shared_e_vecs = true;
625756ca9e9SJeremy L Thompson   CeedCallBackend(CeedOperatorSetSetupDone(op));
626756ca9e9SJeremy L Thompson   return CEED_ERROR_SUCCESS;
627756ca9e9SJeremy L Thompson }
628756ca9e9SJeremy L Thompson 
629756ca9e9SJeremy L Thompson //------------------------------------------------------------------------------
63067d9480aSJeremy L Thompson // Input Basis Action AtPoints
631756ca9e9SJeremy L Thompson //------------------------------------------------------------------------------
632756ca9e9SJeremy L Thompson static inline int CeedOperatorInputBasisAtPoints_Cuda(CeedInt num_elem, const CeedInt *num_points, CeedQFunctionField *qf_input_fields,
633756ca9e9SJeremy L Thompson                                                       CeedOperatorField *op_input_fields, CeedInt num_input_fields, const bool skip_active,
634756ca9e9SJeremy L Thompson                                                       CeedScalar *e_data[2 * CEED_FIELD_MAX], CeedOperator_Cuda *impl) {
635756ca9e9SJeremy L Thompson   for (CeedInt i = 0; i < num_input_fields; i++) {
636756ca9e9SJeremy L Thompson     CeedInt             elem_size, size;
637756ca9e9SJeremy L Thompson     CeedEvalMode        eval_mode;
638756ca9e9SJeremy L Thompson     CeedElemRestriction elem_rstr;
639756ca9e9SJeremy L Thompson     CeedBasis           basis;
640756ca9e9SJeremy L Thompson 
641756ca9e9SJeremy L Thompson     // Skip active input
642756ca9e9SJeremy L Thompson     if (skip_active) {
643756ca9e9SJeremy L Thompson       CeedVector vec;
644756ca9e9SJeremy L Thompson 
645756ca9e9SJeremy L Thompson       CeedCallBackend(CeedOperatorFieldGetVector(op_input_fields[i], &vec));
646756ca9e9SJeremy L Thompson       if (vec == CEED_VECTOR_ACTIVE) continue;
647756ca9e9SJeremy L Thompson     }
648756ca9e9SJeremy L Thompson     // Get elem_size, eval_mode, size
649756ca9e9SJeremy L Thompson     CeedCallBackend(CeedOperatorFieldGetElemRestriction(op_input_fields[i], &elem_rstr));
650756ca9e9SJeremy L Thompson     CeedCallBackend(CeedElemRestrictionGetElementSize(elem_rstr, &elem_size));
651756ca9e9SJeremy L Thompson     CeedCallBackend(CeedQFunctionFieldGetEvalMode(qf_input_fields[i], &eval_mode));
652756ca9e9SJeremy L Thompson     CeedCallBackend(CeedQFunctionFieldGetSize(qf_input_fields[i], &size));
653756ca9e9SJeremy L Thompson     // Basis action
654756ca9e9SJeremy L Thompson     switch (eval_mode) {
655756ca9e9SJeremy L Thompson       case CEED_EVAL_NONE:
656756ca9e9SJeremy L Thompson         CeedCallBackend(CeedVectorSetArray(impl->q_vecs_in[i], CEED_MEM_DEVICE, CEED_USE_POINTER, e_data[i]));
657756ca9e9SJeremy L Thompson         break;
658756ca9e9SJeremy L Thompson       case CEED_EVAL_INTERP:
659756ca9e9SJeremy L Thompson       case CEED_EVAL_GRAD:
660756ca9e9SJeremy L Thompson       case CEED_EVAL_DIV:
661756ca9e9SJeremy L Thompson       case CEED_EVAL_CURL:
662756ca9e9SJeremy L Thompson         CeedCallBackend(CeedOperatorFieldGetBasis(op_input_fields[i], &basis));
663756ca9e9SJeremy L Thompson         CeedCallBackend(CeedBasisApplyAtPoints(basis, num_elem, num_points, CEED_NOTRANSPOSE, eval_mode, impl->point_coords_elem, impl->e_vecs[i],
664756ca9e9SJeremy L Thompson                                                impl->q_vecs_in[i]));
665756ca9e9SJeremy L Thompson         break;
666756ca9e9SJeremy L Thompson       case CEED_EVAL_WEIGHT:
667756ca9e9SJeremy L Thompson         break;  // No action
668756ca9e9SJeremy L Thompson     }
669756ca9e9SJeremy L Thompson   }
670756ca9e9SJeremy L Thompson   return CEED_ERROR_SUCCESS;
671756ca9e9SJeremy L Thompson }
672756ca9e9SJeremy L Thompson 
673756ca9e9SJeremy L Thompson //------------------------------------------------------------------------------
67467d9480aSJeremy L Thompson // Apply and add to output AtPoints
675756ca9e9SJeremy L Thompson //------------------------------------------------------------------------------
676756ca9e9SJeremy L Thompson static int CeedOperatorApplyAddAtPoints_Cuda(CeedOperator op, CeedVector in_vec, CeedVector out_vec, CeedRequest *request) {
677756ca9e9SJeremy L Thompson   CeedInt             max_num_points, num_elem, elem_size, num_input_fields, num_output_fields, size;
678756ca9e9SJeremy L Thompson   CeedScalar         *e_data[2 * CEED_FIELD_MAX] = {NULL};
679756ca9e9SJeremy L Thompson   CeedQFunctionField *qf_input_fields, *qf_output_fields;
680756ca9e9SJeremy L Thompson   CeedQFunction       qf;
681756ca9e9SJeremy L Thompson   CeedOperatorField  *op_input_fields, *op_output_fields;
682756ca9e9SJeremy L Thompson   CeedOperator_Cuda  *impl;
683756ca9e9SJeremy L Thompson 
684756ca9e9SJeremy L Thompson   CeedCallBackend(CeedOperatorGetData(op, &impl));
685756ca9e9SJeremy L Thompson   CeedCallBackend(CeedOperatorGetQFunction(op, &qf));
686756ca9e9SJeremy L Thompson   CeedCallBackend(CeedOperatorGetNumElements(op, &num_elem));
687756ca9e9SJeremy L Thompson   CeedCallBackend(CeedOperatorGetFields(op, &num_input_fields, &op_input_fields, &num_output_fields, &op_output_fields));
688756ca9e9SJeremy L Thompson   CeedCallBackend(CeedQFunctionGetFields(qf, NULL, &qf_input_fields, NULL, &qf_output_fields));
689756ca9e9SJeremy L Thompson   CeedInt num_points[num_elem];
690756ca9e9SJeremy L Thompson 
691756ca9e9SJeremy L Thompson   // Setup
692756ca9e9SJeremy L Thompson   CeedCallBackend(CeedOperatorSetupAtPoints_Cuda(op));
693756ca9e9SJeremy L Thompson   max_num_points = impl->max_num_points;
694756ca9e9SJeremy L Thompson   for (CeedInt i = 0; i < num_elem; i++) num_points[i] = max_num_points;
695756ca9e9SJeremy L Thompson 
696756ca9e9SJeremy L Thompson   // Input Evecs and Restriction
697756ca9e9SJeremy L Thompson   CeedCallBackend(CeedOperatorSetupInputs_Cuda(num_input_fields, qf_input_fields, op_input_fields, in_vec, false, e_data, impl, request));
698756ca9e9SJeremy L Thompson 
699756ca9e9SJeremy L Thompson   // Get point coordinates
700756ca9e9SJeremy L Thompson   if (!impl->point_coords_elem) {
701756ca9e9SJeremy L Thompson     CeedVector          point_coords = NULL;
702756ca9e9SJeremy L Thompson     CeedElemRestriction rstr_points  = NULL;
703756ca9e9SJeremy L Thompson 
704756ca9e9SJeremy L Thompson     CeedCallBackend(CeedOperatorAtPointsGetPoints(op, &rstr_points, &point_coords));
705756ca9e9SJeremy L Thompson     CeedCallBackend(CeedElemRestrictionCreateVector(rstr_points, NULL, &impl->point_coords_elem));
706756ca9e9SJeremy L Thompson     CeedCallBackend(CeedElemRestrictionApply(rstr_points, CEED_NOTRANSPOSE, point_coords, impl->point_coords_elem, request));
707756ca9e9SJeremy L Thompson   }
708756ca9e9SJeremy L Thompson 
709756ca9e9SJeremy L Thompson   // Input basis apply if needed
710756ca9e9SJeremy L Thompson   CeedCallBackend(CeedOperatorInputBasisAtPoints_Cuda(num_elem, num_points, qf_input_fields, op_input_fields, num_input_fields, false, e_data, impl));
711756ca9e9SJeremy L Thompson 
712756ca9e9SJeremy L Thompson   // Output pointers, as necessary
713756ca9e9SJeremy L Thompson   for (CeedInt i = 0; i < num_output_fields; i++) {
714756ca9e9SJeremy L Thompson     CeedEvalMode eval_mode;
715756ca9e9SJeremy L Thompson 
716756ca9e9SJeremy L Thompson     CeedCallBackend(CeedQFunctionFieldGetEvalMode(qf_output_fields[i], &eval_mode));
717756ca9e9SJeremy L Thompson     if (eval_mode == CEED_EVAL_NONE) {
718756ca9e9SJeremy L Thompson       // Set the output Q-Vector to use the E-Vector data directly.
719756ca9e9SJeremy L Thompson       CeedCallBackend(CeedVectorGetArrayWrite(impl->e_vecs[i + impl->num_inputs], CEED_MEM_DEVICE, &e_data[i + num_input_fields]));
720756ca9e9SJeremy L Thompson       CeedCallBackend(CeedVectorSetArray(impl->q_vecs_out[i], CEED_MEM_DEVICE, CEED_USE_POINTER, e_data[i + num_input_fields]));
721756ca9e9SJeremy L Thompson     }
722756ca9e9SJeremy L Thompson   }
723756ca9e9SJeremy L Thompson 
724756ca9e9SJeremy L Thompson   // Q function
725756ca9e9SJeremy L Thompson   CeedCallBackend(CeedQFunctionApply(qf, num_elem * max_num_points, impl->q_vecs_in, impl->q_vecs_out));
726756ca9e9SJeremy L Thompson 
727*8a213570SJeremy L Thompson   // Restore input arrays
728*8a213570SJeremy L Thompson   CeedCallBackend(CeedOperatorRestoreInputs_Cuda(num_input_fields, qf_input_fields, op_input_fields, false, e_data, impl));
729*8a213570SJeremy L Thompson 
730756ca9e9SJeremy L Thompson   // Output basis apply if needed
731756ca9e9SJeremy L Thompson   for (CeedInt i = 0; i < num_output_fields; i++) {
732756ca9e9SJeremy L Thompson     CeedEvalMode        eval_mode;
733756ca9e9SJeremy L Thompson     CeedElemRestriction elem_rstr;
734756ca9e9SJeremy L Thompson     CeedBasis           basis;
735756ca9e9SJeremy L Thompson 
736756ca9e9SJeremy L Thompson     // Get elem_size, eval_mode, size
737756ca9e9SJeremy L Thompson     CeedCallBackend(CeedOperatorFieldGetElemRestriction(op_output_fields[i], &elem_rstr));
738756ca9e9SJeremy L Thompson     CeedCallBackend(CeedElemRestrictionGetElementSize(elem_rstr, &elem_size));
739756ca9e9SJeremy L Thompson     CeedCallBackend(CeedQFunctionFieldGetEvalMode(qf_output_fields[i], &eval_mode));
740756ca9e9SJeremy L Thompson     CeedCallBackend(CeedQFunctionFieldGetSize(qf_output_fields[i], &size));
741756ca9e9SJeremy L Thompson     // Basis action
742756ca9e9SJeremy L Thompson     switch (eval_mode) {
743756ca9e9SJeremy L Thompson       case CEED_EVAL_NONE:
744756ca9e9SJeremy L Thompson         break;  // No action
745756ca9e9SJeremy L Thompson       case CEED_EVAL_INTERP:
746756ca9e9SJeremy L Thompson       case CEED_EVAL_GRAD:
747756ca9e9SJeremy L Thompson       case CEED_EVAL_DIV:
748756ca9e9SJeremy L Thompson       case CEED_EVAL_CURL:
749756ca9e9SJeremy L Thompson         CeedCallBackend(CeedOperatorFieldGetBasis(op_output_fields[i], &basis));
750f8a0df59SJeremy L Thompson         if (impl->apply_add_basis_out[i]) {
751f8a0df59SJeremy L Thompson           CeedCallBackend(CeedBasisApplyAddAtPoints(basis, num_elem, num_points, CEED_TRANSPOSE, eval_mode, impl->point_coords_elem,
752f8a0df59SJeremy L Thompson                                                     impl->q_vecs_out[i], impl->e_vecs[i + impl->num_inputs]));
753f8a0df59SJeremy L Thompson         } else {
754756ca9e9SJeremy L Thompson           CeedCallBackend(CeedBasisApplyAtPoints(basis, num_elem, num_points, CEED_TRANSPOSE, eval_mode, impl->point_coords_elem, impl->q_vecs_out[i],
755756ca9e9SJeremy L Thompson                                                  impl->e_vecs[i + impl->num_inputs]));
756f8a0df59SJeremy L Thompson         }
757756ca9e9SJeremy L Thompson         break;
758756ca9e9SJeremy L Thompson       // LCOV_EXCL_START
759756ca9e9SJeremy L Thompson       case CEED_EVAL_WEIGHT: {
760756ca9e9SJeremy L Thompson         return CeedError(CeedOperatorReturnCeed(op), CEED_ERROR_BACKEND, "CEED_EVAL_WEIGHT cannot be an output evaluation mode");
761756ca9e9SJeremy L Thompson         // LCOV_EXCL_STOP
762756ca9e9SJeremy L Thompson       }
763756ca9e9SJeremy L Thompson     }
764756ca9e9SJeremy L Thompson   }
765756ca9e9SJeremy L Thompson 
766756ca9e9SJeremy L Thompson   // Output restriction
767756ca9e9SJeremy L Thompson   for (CeedInt i = 0; i < num_output_fields; i++) {
768756ca9e9SJeremy L Thompson     CeedEvalMode        eval_mode;
769756ca9e9SJeremy L Thompson     CeedVector          vec;
770756ca9e9SJeremy L Thompson     CeedElemRestriction elem_rstr;
771756ca9e9SJeremy L Thompson 
772756ca9e9SJeremy L Thompson     // Restore evec
773756ca9e9SJeremy L Thompson     CeedCallBackend(CeedQFunctionFieldGetEvalMode(qf_output_fields[i], &eval_mode));
774756ca9e9SJeremy L Thompson     if (eval_mode == CEED_EVAL_NONE) {
775756ca9e9SJeremy L Thompson       CeedCallBackend(CeedVectorRestoreArray(impl->e_vecs[i + impl->num_inputs], &e_data[i + num_input_fields]));
776756ca9e9SJeremy L Thompson     }
777f8a0df59SJeremy L Thompson     if (impl->skip_rstr_out[i]) continue;
778756ca9e9SJeremy L Thompson     // Get output vector
779756ca9e9SJeremy L Thompson     CeedCallBackend(CeedOperatorFieldGetVector(op_output_fields[i], &vec));
780756ca9e9SJeremy L Thompson     // Restrict
781756ca9e9SJeremy L Thompson     CeedCallBackend(CeedOperatorFieldGetElemRestriction(op_output_fields[i], &elem_rstr));
782756ca9e9SJeremy L Thompson     // Active
783756ca9e9SJeremy L Thompson     if (vec == CEED_VECTOR_ACTIVE) vec = out_vec;
784756ca9e9SJeremy L Thompson 
785756ca9e9SJeremy L Thompson     CeedCallBackend(CeedElemRestrictionApply(elem_rstr, CEED_TRANSPOSE, impl->e_vecs[i + impl->num_inputs], vec, request));
786756ca9e9SJeremy L Thompson   }
787756ca9e9SJeremy L Thompson   return CEED_ERROR_SUCCESS;
788756ca9e9SJeremy L Thompson }
789756ca9e9SJeremy L Thompson 
790756ca9e9SJeremy L Thompson //------------------------------------------------------------------------------
791004e4986SSebastian Grimberg // Linear QFunction Assembly Core
7920d0321e0SJeremy L Thompson //------------------------------------------------------------------------------
7932b730f8bSJeremy L Thompson static inline int CeedOperatorLinearAssembleQFunctionCore_Cuda(CeedOperator op, bool build_objects, CeedVector *assembled, CeedElemRestriction *rstr,
7940d0321e0SJeremy L Thompson                                                                CeedRequest *request) {
795ca735530SJeremy L Thompson   Ceed                ceed, ceed_parent;
796ca735530SJeremy L Thompson   CeedInt             num_active_in, num_active_out, Q, num_elem, num_input_fields, num_output_fields, size;
797ca735530SJeremy L Thompson   CeedScalar         *assembled_array, *e_data[2 * CEED_FIELD_MAX] = {NULL};
798ca735530SJeremy L Thompson   CeedVector         *active_inputs;
799ca735530SJeremy L Thompson   CeedQFunctionField *qf_input_fields, *qf_output_fields;
800ca735530SJeremy L Thompson   CeedQFunction       qf;
801ca735530SJeremy L Thompson   CeedOperatorField  *op_input_fields, *op_output_fields;
802ca735530SJeremy L Thompson   CeedOperator_Cuda  *impl;
803ca735530SJeremy L Thompson 
8042b730f8bSJeremy L Thompson   CeedCallBackend(CeedOperatorGetCeed(op, &ceed));
805ca735530SJeremy L Thompson   CeedCallBackend(CeedOperatorGetFallbackParentCeed(op, &ceed_parent));
806e984cf9aSJeremy L Thompson   CeedCallBackend(CeedOperatorGetData(op, &impl));
807e984cf9aSJeremy L Thompson   CeedCallBackend(CeedOperatorGetNumQuadraturePoints(op, &Q));
808ca735530SJeremy L Thompson   CeedCallBackend(CeedOperatorGetNumElements(op, &num_elem));
809e984cf9aSJeremy L Thompson   CeedCallBackend(CeedOperatorGetQFunction(op, &qf));
810ca735530SJeremy L Thompson   CeedCallBackend(CeedQFunctionGetFields(qf, NULL, &qf_input_fields, NULL, &qf_output_fields));
811ca735530SJeremy L Thompson   CeedCallBackend(CeedOperatorGetFields(op, &num_input_fields, &op_input_fields, &num_output_fields, &op_output_fields));
812ca735530SJeremy L Thompson   active_inputs = impl->qf_active_in;
813ca735530SJeremy L Thompson   num_active_in = impl->num_active_in, num_active_out = impl->num_active_out;
8140d0321e0SJeremy L Thompson 
8150d0321e0SJeremy L Thompson   // Setup
8162b730f8bSJeremy L Thompson   CeedCallBackend(CeedOperatorSetup_Cuda(op));
8170d0321e0SJeremy L Thompson 
818004e4986SSebastian Grimberg   // Input Evecs and Restriction
819ca735530SJeremy L Thompson   CeedCallBackend(CeedOperatorSetupInputs_Cuda(num_input_fields, qf_input_fields, op_input_fields, NULL, true, e_data, impl, request));
8200d0321e0SJeremy L Thompson 
8210d0321e0SJeremy L Thompson   // Count number of active input fields
822ca735530SJeremy L Thompson   if (!num_active_in) {
823ca735530SJeremy L Thompson     for (CeedInt i = 0; i < num_input_fields; i++) {
824ca735530SJeremy L Thompson       CeedScalar *q_vec_array;
825ca735530SJeremy L Thompson       CeedVector  vec;
826ca735530SJeremy L Thompson 
8270d0321e0SJeremy L Thompson       // Get input vector
828ca735530SJeremy L Thompson       CeedCallBackend(CeedOperatorFieldGetVector(op_input_fields[i], &vec));
8290d0321e0SJeremy L Thompson       // Check if active input
8300d0321e0SJeremy L Thompson       if (vec == CEED_VECTOR_ACTIVE) {
831ca735530SJeremy L Thompson         CeedCallBackend(CeedQFunctionFieldGetSize(qf_input_fields[i], &size));
832ca735530SJeremy L Thompson         CeedCallBackend(CeedVectorSetValue(impl->q_vecs_in[i], 0.0));
833ca735530SJeremy L Thompson         CeedCallBackend(CeedVectorGetArray(impl->q_vecs_in[i], CEED_MEM_DEVICE, &q_vec_array));
834ca735530SJeremy L Thompson         CeedCallBackend(CeedRealloc(num_active_in + size, &active_inputs));
8350d0321e0SJeremy L Thompson         for (CeedInt field = 0; field < size; field++) {
836004e4986SSebastian Grimberg           CeedSize q_size = (CeedSize)Q * num_elem;
837004e4986SSebastian Grimberg 
838ca735530SJeremy L Thompson           CeedCallBackend(CeedVectorCreate(ceed, q_size, &active_inputs[num_active_in + field]));
839ca735530SJeremy L Thompson           CeedCallBackend(
840ca735530SJeremy L Thompson               CeedVectorSetArray(active_inputs[num_active_in + field], CEED_MEM_DEVICE, CEED_USE_POINTER, &q_vec_array[field * Q * num_elem]));
8410d0321e0SJeremy L Thompson         }
842ca735530SJeremy L Thompson         num_active_in += size;
843ca735530SJeremy L Thompson         CeedCallBackend(CeedVectorRestoreArray(impl->q_vecs_in[i], &q_vec_array));
8440d0321e0SJeremy L Thompson       }
8450d0321e0SJeremy L Thompson     }
846ca735530SJeremy L Thompson     impl->num_active_in = num_active_in;
847ca735530SJeremy L Thompson     impl->qf_active_in  = active_inputs;
8480d0321e0SJeremy L Thompson   }
8490d0321e0SJeremy L Thompson 
8500d0321e0SJeremy L Thompson   // Count number of active output fields
851ca735530SJeremy L Thompson   if (!num_active_out) {
852ca735530SJeremy L Thompson     for (CeedInt i = 0; i < num_output_fields; i++) {
853ca735530SJeremy L Thompson       CeedVector vec;
854ca735530SJeremy L Thompson 
8550d0321e0SJeremy L Thompson       // Get output vector
856ca735530SJeremy L Thompson       CeedCallBackend(CeedOperatorFieldGetVector(op_output_fields[i], &vec));
8570d0321e0SJeremy L Thompson       // Check if active output
8580d0321e0SJeremy L Thompson       if (vec == CEED_VECTOR_ACTIVE) {
859ca735530SJeremy L Thompson         CeedCallBackend(CeedQFunctionFieldGetSize(qf_output_fields[i], &size));
860ca735530SJeremy L Thompson         num_active_out += size;
8610d0321e0SJeremy L Thompson       }
8620d0321e0SJeremy L Thompson     }
863ca735530SJeremy L Thompson     impl->num_active_out = num_active_out;
8640d0321e0SJeremy L Thompson   }
8650d0321e0SJeremy L Thompson 
8660d0321e0SJeremy L Thompson   // Check sizes
867ca735530SJeremy L Thompson   CeedCheck(num_active_in > 0 && num_active_out > 0, ceed, CEED_ERROR_BACKEND, "Cannot assemble QFunction without active inputs and outputs");
8680d0321e0SJeremy L Thompson 
8690d0321e0SJeremy L Thompson   // Build objects if needed
8700d0321e0SJeremy L Thompson   if (build_objects) {
871004e4986SSebastian Grimberg     CeedSize l_size     = (CeedSize)num_elem * Q * num_active_in * num_active_out;
872ca735530SJeremy L Thompson     CeedInt  strides[3] = {1, num_elem * Q, Q}; /* *NOPAD* */
873004e4986SSebastian Grimberg 
874004e4986SSebastian Grimberg     // Create output restriction
875ca735530SJeremy L Thompson     CeedCallBackend(CeedElemRestrictionCreateStrided(ceed_parent, num_elem, Q, num_active_in * num_active_out,
8760a5597ceSJeremy L Thompson                                                      (CeedSize)num_active_in * (CeedSize)num_active_out * (CeedSize)num_elem * (CeedSize)Q, strides,
8770a5597ceSJeremy L Thompson                                                      rstr));
8780d0321e0SJeremy L Thompson     // Create assembled vector
879ca735530SJeremy L Thompson     CeedCallBackend(CeedVectorCreate(ceed_parent, l_size, assembled));
8800d0321e0SJeremy L Thompson   }
8812b730f8bSJeremy L Thompson   CeedCallBackend(CeedVectorSetValue(*assembled, 0.0));
882ca735530SJeremy L Thompson   CeedCallBackend(CeedVectorGetArray(*assembled, CEED_MEM_DEVICE, &assembled_array));
8830d0321e0SJeremy L Thompson 
8840d0321e0SJeremy L Thompson   // Input basis apply
885ca735530SJeremy L Thompson   CeedCallBackend(CeedOperatorInputBasis_Cuda(num_elem, qf_input_fields, op_input_fields, num_input_fields, true, e_data, impl));
8860d0321e0SJeremy L Thompson 
8870d0321e0SJeremy L Thompson   // Assemble QFunction
888ca735530SJeremy L Thompson   for (CeedInt in = 0; in < num_active_in; in++) {
8890d0321e0SJeremy L Thompson     // Set Inputs
890ca735530SJeremy L Thompson     CeedCallBackend(CeedVectorSetValue(active_inputs[in], 1.0));
891ca735530SJeremy L Thompson     if (num_active_in > 1) {
892ca735530SJeremy L Thompson       CeedCallBackend(CeedVectorSetValue(active_inputs[(in + num_active_in - 1) % num_active_in], 0.0));
8930d0321e0SJeremy L Thompson     }
8940d0321e0SJeremy L Thompson     // Set Outputs
895ca735530SJeremy L Thompson     for (CeedInt out = 0; out < num_output_fields; out++) {
896ca735530SJeremy L Thompson       CeedVector vec;
897ca735530SJeremy L Thompson 
8980d0321e0SJeremy L Thompson       // Get output vector
899ca735530SJeremy L Thompson       CeedCallBackend(CeedOperatorFieldGetVector(op_output_fields[out], &vec));
9000d0321e0SJeremy L Thompson       // Check if active output
9010d0321e0SJeremy L Thompson       if (vec == CEED_VECTOR_ACTIVE) {
902ca735530SJeremy L Thompson         CeedCallBackend(CeedVectorSetArray(impl->q_vecs_out[out], CEED_MEM_DEVICE, CEED_USE_POINTER, assembled_array));
903ca735530SJeremy L Thompson         CeedCallBackend(CeedQFunctionFieldGetSize(qf_output_fields[out], &size));
904ca735530SJeremy L Thompson         assembled_array += size * Q * num_elem;  // Advance the pointer by the size of the output
9050d0321e0SJeremy L Thompson       }
9060d0321e0SJeremy L Thompson     }
9070d0321e0SJeremy L Thompson     // Apply QFunction
908ca735530SJeremy L Thompson     CeedCallBackend(CeedQFunctionApply(qf, Q * num_elem, impl->q_vecs_in, impl->q_vecs_out));
9090d0321e0SJeremy L Thompson   }
9100d0321e0SJeremy L Thompson 
911*8a213570SJeremy L Thompson   // Un-set output q-vecs to prevent accidental overwrite of Assembled
912ca735530SJeremy L Thompson   for (CeedInt out = 0; out < num_output_fields; out++) {
913ca735530SJeremy L Thompson     CeedVector vec;
914ca735530SJeremy L Thompson 
9150d0321e0SJeremy L Thompson     // Get output vector
916ca735530SJeremy L Thompson     CeedCallBackend(CeedOperatorFieldGetVector(op_output_fields[out], &vec));
9170d0321e0SJeremy L Thompson     // Check if active output
9180d0321e0SJeremy L Thompson     if (vec == CEED_VECTOR_ACTIVE) {
919ca735530SJeremy L Thompson       CeedCallBackend(CeedVectorTakeArray(impl->q_vecs_out[out], CEED_MEM_DEVICE, NULL));
9200d0321e0SJeremy L Thompson     }
9210d0321e0SJeremy L Thompson   }
9220d0321e0SJeremy L Thompson 
9230d0321e0SJeremy L Thompson   // Restore input arrays
924ca735530SJeremy L Thompson   CeedCallBackend(CeedOperatorRestoreInputs_Cuda(num_input_fields, qf_input_fields, op_input_fields, true, e_data, impl));
9250d0321e0SJeremy L Thompson 
9260d0321e0SJeremy L Thompson   // Restore output
927ca735530SJeremy L Thompson   CeedCallBackend(CeedVectorRestoreArray(*assembled, &assembled_array));
9280d0321e0SJeremy L Thompson   return CEED_ERROR_SUCCESS;
9290d0321e0SJeremy L Thompson }
9300d0321e0SJeremy L Thompson 
9310d0321e0SJeremy L Thompson //------------------------------------------------------------------------------
9320d0321e0SJeremy L Thompson // Assemble Linear QFunction
9330d0321e0SJeremy L Thompson //------------------------------------------------------------------------------
9342b730f8bSJeremy L Thompson static int CeedOperatorLinearAssembleQFunction_Cuda(CeedOperator op, CeedVector *assembled, CeedElemRestriction *rstr, CeedRequest *request) {
9352b730f8bSJeremy L Thompson   return CeedOperatorLinearAssembleQFunctionCore_Cuda(op, true, assembled, rstr, request);
9360d0321e0SJeremy L Thompson }
9370d0321e0SJeremy L Thompson 
9380d0321e0SJeremy L Thompson //------------------------------------------------------------------------------
9390d0321e0SJeremy L Thompson // Update Assembled Linear QFunction
9400d0321e0SJeremy L Thompson //------------------------------------------------------------------------------
9412b730f8bSJeremy L Thompson static int CeedOperatorLinearAssembleQFunctionUpdate_Cuda(CeedOperator op, CeedVector assembled, CeedElemRestriction rstr, CeedRequest *request) {
9422b730f8bSJeremy L Thompson   return CeedOperatorLinearAssembleQFunctionCore_Cuda(op, false, &assembled, &rstr, request);
9430d0321e0SJeremy L Thompson }
9440d0321e0SJeremy L Thompson 
9450d0321e0SJeremy L Thompson //------------------------------------------------------------------------------
946004e4986SSebastian Grimberg // Assemble Diagonal Setup
9470d0321e0SJeremy L Thompson //------------------------------------------------------------------------------
948cbfe683aSSebastian Grimberg static inline int CeedOperatorAssembleDiagonalSetup_Cuda(CeedOperator op) {
9490d0321e0SJeremy L Thompson   Ceed                ceed;
950004e4986SSebastian Grimberg   CeedInt             num_input_fields, num_output_fields, num_eval_modes_in = 0, num_eval_modes_out = 0;
951cbfe683aSSebastian Grimberg   CeedInt             q_comp, num_nodes, num_qpts;
952004e4986SSebastian Grimberg   CeedEvalMode       *eval_modes_in = NULL, *eval_modes_out = NULL;
953ca735530SJeremy L Thompson   CeedBasis           basis_in = NULL, basis_out = NULL;
954ca735530SJeremy L Thompson   CeedQFunctionField *qf_fields;
9550d0321e0SJeremy L Thompson   CeedQFunction       qf;
956ca735530SJeremy L Thompson   CeedOperatorField  *op_fields;
957ca735530SJeremy L Thompson   CeedOperator_Cuda  *impl;
958ca735530SJeremy L Thompson 
959ca735530SJeremy L Thompson   CeedCallBackend(CeedOperatorGetCeed(op, &ceed));
9602b730f8bSJeremy L Thompson   CeedCallBackend(CeedOperatorGetQFunction(op, &qf));
961ca735530SJeremy L Thompson   CeedCallBackend(CeedQFunctionGetNumArgs(qf, &num_input_fields, &num_output_fields));
9620d0321e0SJeremy L Thompson 
9630d0321e0SJeremy L Thompson   // Determine active input basis
964ca735530SJeremy L Thompson   CeedCallBackend(CeedOperatorGetFields(op, NULL, &op_fields, NULL, NULL));
965ca735530SJeremy L Thompson   CeedCallBackend(CeedQFunctionGetFields(qf, NULL, &qf_fields, NULL, NULL));
966ca735530SJeremy L Thompson   for (CeedInt i = 0; i < num_input_fields; i++) {
9670d0321e0SJeremy L Thompson     CeedVector vec;
968ca735530SJeremy L Thompson 
969ca735530SJeremy L Thompson     CeedCallBackend(CeedOperatorFieldGetVector(op_fields[i], &vec));
9700d0321e0SJeremy L Thompson     if (vec == CEED_VECTOR_ACTIVE) {
971004e4986SSebastian Grimberg       CeedBasis    basis;
972004e4986SSebastian Grimberg       CeedEvalMode eval_mode;
973ca735530SJeremy L Thompson 
974004e4986SSebastian Grimberg       CeedCallBackend(CeedOperatorFieldGetBasis(op_fields[i], &basis));
975004e4986SSebastian Grimberg       CeedCheck(!basis_in || basis_in == basis, ceed, CEED_ERROR_BACKEND,
976004e4986SSebastian Grimberg                 "Backend does not implement operator diagonal assembly with multiple active bases");
977004e4986SSebastian Grimberg       basis_in = basis;
978004e4986SSebastian Grimberg       CeedCallBackend(CeedQFunctionFieldGetEvalMode(qf_fields[i], &eval_mode));
979004e4986SSebastian Grimberg       CeedCallBackend(CeedBasisGetNumQuadratureComponents(basis_in, eval_mode, &q_comp));
980004e4986SSebastian Grimberg       if (eval_mode != CEED_EVAL_WEIGHT) {
981004e4986SSebastian Grimberg         // q_comp = 1 if CEED_EVAL_NONE, CEED_EVAL_WEIGHT caught by QF assembly
982004e4986SSebastian Grimberg         CeedCallBackend(CeedRealloc(num_eval_modes_in + q_comp, &eval_modes_in));
983004e4986SSebastian Grimberg         for (CeedInt d = 0; d < q_comp; d++) eval_modes_in[num_eval_modes_in + d] = eval_mode;
984004e4986SSebastian Grimberg         num_eval_modes_in += q_comp;
9850d0321e0SJeremy L Thompson       }
9860d0321e0SJeremy L Thompson     }
9870d0321e0SJeremy L Thompson   }
9880d0321e0SJeremy L Thompson 
9890d0321e0SJeremy L Thompson   // Determine active output basis
990ca735530SJeremy L Thompson   CeedCallBackend(CeedOperatorGetFields(op, NULL, NULL, NULL, &op_fields));
991ca735530SJeremy L Thompson   CeedCallBackend(CeedQFunctionGetFields(qf, NULL, NULL, NULL, &qf_fields));
992ca735530SJeremy L Thompson   for (CeedInt i = 0; i < num_output_fields; i++) {
9930d0321e0SJeremy L Thompson     CeedVector vec;
994ca735530SJeremy L Thompson 
995ca735530SJeremy L Thompson     CeedCallBackend(CeedOperatorFieldGetVector(op_fields[i], &vec));
9960d0321e0SJeremy L Thompson     if (vec == CEED_VECTOR_ACTIVE) {
997004e4986SSebastian Grimberg       CeedBasis    basis;
998004e4986SSebastian Grimberg       CeedEvalMode eval_mode;
999ca735530SJeremy L Thompson 
1000004e4986SSebastian Grimberg       CeedCallBackend(CeedOperatorFieldGetBasis(op_fields[i], &basis));
1001004e4986SSebastian Grimberg       CeedCheck(!basis_out || basis_out == basis, ceed, CEED_ERROR_BACKEND,
1002004e4986SSebastian Grimberg                 "Backend does not implement operator diagonal assembly with multiple active bases");
1003004e4986SSebastian Grimberg       basis_out = basis;
1004004e4986SSebastian Grimberg       CeedCallBackend(CeedQFunctionFieldGetEvalMode(qf_fields[i], &eval_mode));
1005004e4986SSebastian Grimberg       CeedCallBackend(CeedBasisGetNumQuadratureComponents(basis_out, eval_mode, &q_comp));
1006004e4986SSebastian Grimberg       if (eval_mode != CEED_EVAL_WEIGHT) {
1007004e4986SSebastian Grimberg         // q_comp = 1 if CEED_EVAL_NONE, CEED_EVAL_WEIGHT caught by QF assembly
1008004e4986SSebastian Grimberg         CeedCallBackend(CeedRealloc(num_eval_modes_out + q_comp, &eval_modes_out));
1009004e4986SSebastian Grimberg         for (CeedInt d = 0; d < q_comp; d++) eval_modes_out[num_eval_modes_out + d] = eval_mode;
1010004e4986SSebastian Grimberg         num_eval_modes_out += q_comp;
10110d0321e0SJeremy L Thompson       }
10120d0321e0SJeremy L Thompson     }
10130d0321e0SJeremy L Thompson   }
10140d0321e0SJeremy L Thompson 
10150d0321e0SJeremy L Thompson   // Operator data struct
10162b730f8bSJeremy L Thompson   CeedCallBackend(CeedOperatorGetData(op, &impl));
10172b730f8bSJeremy L Thompson   CeedCallBackend(CeedCalloc(1, &impl->diag));
10180d0321e0SJeremy L Thompson   CeedOperatorDiag_Cuda *diag = impl->diag;
1019ca735530SJeremy L Thompson 
1020cbfe683aSSebastian Grimberg   // Basis matrices
1021004e4986SSebastian Grimberg   CeedCallBackend(CeedBasisGetNumNodes(basis_in, &num_nodes));
1022004e4986SSebastian Grimberg   if (basis_in == CEED_BASIS_NONE) num_qpts = num_nodes;
1023004e4986SSebastian Grimberg   else CeedCallBackend(CeedBasisGetNumQuadraturePoints(basis_in, &num_qpts));
1024004e4986SSebastian Grimberg   const CeedInt interp_bytes     = num_nodes * num_qpts * sizeof(CeedScalar);
1025004e4986SSebastian Grimberg   const CeedInt eval_modes_bytes = sizeof(CeedEvalMode);
1026004e4986SSebastian Grimberg   bool          has_eval_none    = false;
10270d0321e0SJeremy L Thompson 
10280d0321e0SJeremy L Thompson   // CEED_EVAL_NONE
1029004e4986SSebastian Grimberg   for (CeedInt i = 0; i < num_eval_modes_in; i++) has_eval_none = has_eval_none || (eval_modes_in[i] == CEED_EVAL_NONE);
1030004e4986SSebastian Grimberg   for (CeedInt i = 0; i < num_eval_modes_out; i++) has_eval_none = has_eval_none || (eval_modes_out[i] == CEED_EVAL_NONE);
1031004e4986SSebastian Grimberg   if (has_eval_none) {
10320d0321e0SJeremy L Thompson     CeedScalar *identity = NULL;
1033ca735530SJeremy L Thompson 
1034004e4986SSebastian Grimberg     CeedCallBackend(CeedCalloc(num_nodes * num_qpts, &identity));
1035ca735530SJeremy L Thompson     for (CeedInt i = 0; i < (num_nodes < num_qpts ? num_nodes : num_qpts); i++) identity[i * num_nodes + i] = 1.0;
1036ca735530SJeremy L Thompson     CeedCallCuda(ceed, cudaMalloc((void **)&diag->d_identity, interp_bytes));
1037ca735530SJeremy L Thompson     CeedCallCuda(ceed, cudaMemcpy(diag->d_identity, identity, interp_bytes, cudaMemcpyHostToDevice));
1038004e4986SSebastian Grimberg     CeedCallBackend(CeedFree(&identity));
10390d0321e0SJeremy L Thompson   }
10400d0321e0SJeremy L Thompson 
1041004e4986SSebastian Grimberg   // CEED_EVAL_INTERP, CEED_EVAL_GRAD, CEED_EVAL_DIV, and CEED_EVAL_CURL
1042004e4986SSebastian Grimberg   for (CeedInt in = 0; in < 2; in++) {
1043004e4986SSebastian Grimberg     CeedFESpace fespace;
1044004e4986SSebastian Grimberg     CeedBasis   basis = in ? basis_in : basis_out;
10450d0321e0SJeremy L Thompson 
1046004e4986SSebastian Grimberg     CeedCallBackend(CeedBasisGetFESpace(basis, &fespace));
1047004e4986SSebastian Grimberg     switch (fespace) {
1048004e4986SSebastian Grimberg       case CEED_FE_SPACE_H1: {
1049004e4986SSebastian Grimberg         CeedInt           q_comp_interp, q_comp_grad;
1050004e4986SSebastian Grimberg         const CeedScalar *interp, *grad;
1051004e4986SSebastian Grimberg         CeedScalar       *d_interp, *d_grad;
10520d0321e0SJeremy L Thompson 
1053004e4986SSebastian Grimberg         CeedCallBackend(CeedBasisGetNumQuadratureComponents(basis, CEED_EVAL_INTERP, &q_comp_interp));
1054004e4986SSebastian Grimberg         CeedCallBackend(CeedBasisGetNumQuadratureComponents(basis, CEED_EVAL_GRAD, &q_comp_grad));
10550d0321e0SJeremy L Thompson 
1056004e4986SSebastian Grimberg         CeedCallBackend(CeedBasisGetInterp(basis, &interp));
1057004e4986SSebastian Grimberg         CeedCallCuda(ceed, cudaMalloc((void **)&d_interp, interp_bytes * q_comp_interp));
1058004e4986SSebastian Grimberg         CeedCallCuda(ceed, cudaMemcpy(d_interp, interp, interp_bytes * q_comp_interp, cudaMemcpyHostToDevice));
1059004e4986SSebastian Grimberg         CeedCallBackend(CeedBasisGetGrad(basis, &grad));
1060004e4986SSebastian Grimberg         CeedCallCuda(ceed, cudaMalloc((void **)&d_grad, interp_bytes * q_comp_grad));
1061004e4986SSebastian Grimberg         CeedCallCuda(ceed, cudaMemcpy(d_grad, grad, interp_bytes * q_comp_grad, cudaMemcpyHostToDevice));
1062004e4986SSebastian Grimberg         if (in) {
1063004e4986SSebastian Grimberg           diag->d_interp_in = d_interp;
1064004e4986SSebastian Grimberg           diag->d_grad_in   = d_grad;
1065004e4986SSebastian Grimberg         } else {
1066004e4986SSebastian Grimberg           diag->d_interp_out = d_interp;
1067004e4986SSebastian Grimberg           diag->d_grad_out   = d_grad;
1068004e4986SSebastian Grimberg         }
1069004e4986SSebastian Grimberg       } break;
1070004e4986SSebastian Grimberg       case CEED_FE_SPACE_HDIV: {
1071004e4986SSebastian Grimberg         CeedInt           q_comp_interp, q_comp_div;
1072004e4986SSebastian Grimberg         const CeedScalar *interp, *div;
1073004e4986SSebastian Grimberg         CeedScalar       *d_interp, *d_div;
1074004e4986SSebastian Grimberg 
1075004e4986SSebastian Grimberg         CeedCallBackend(CeedBasisGetNumQuadratureComponents(basis, CEED_EVAL_INTERP, &q_comp_interp));
1076004e4986SSebastian Grimberg         CeedCallBackend(CeedBasisGetNumQuadratureComponents(basis, CEED_EVAL_DIV, &q_comp_div));
1077004e4986SSebastian Grimberg 
1078004e4986SSebastian Grimberg         CeedCallBackend(CeedBasisGetInterp(basis, &interp));
1079004e4986SSebastian Grimberg         CeedCallCuda(ceed, cudaMalloc((void **)&d_interp, interp_bytes * q_comp_interp));
1080004e4986SSebastian Grimberg         CeedCallCuda(ceed, cudaMemcpy(d_interp, interp, interp_bytes * q_comp_interp, cudaMemcpyHostToDevice));
1081004e4986SSebastian Grimberg         CeedCallBackend(CeedBasisGetDiv(basis, &div));
1082004e4986SSebastian Grimberg         CeedCallCuda(ceed, cudaMalloc((void **)&d_div, interp_bytes * q_comp_div));
1083004e4986SSebastian Grimberg         CeedCallCuda(ceed, cudaMemcpy(d_div, div, interp_bytes * q_comp_div, cudaMemcpyHostToDevice));
1084004e4986SSebastian Grimberg         if (in) {
1085004e4986SSebastian Grimberg           diag->d_interp_in = d_interp;
1086004e4986SSebastian Grimberg           diag->d_div_in    = d_div;
1087004e4986SSebastian Grimberg         } else {
1088004e4986SSebastian Grimberg           diag->d_interp_out = d_interp;
1089004e4986SSebastian Grimberg           diag->d_div_out    = d_div;
1090004e4986SSebastian Grimberg         }
1091004e4986SSebastian Grimberg       } break;
1092004e4986SSebastian Grimberg       case CEED_FE_SPACE_HCURL: {
1093004e4986SSebastian Grimberg         CeedInt           q_comp_interp, q_comp_curl;
1094004e4986SSebastian Grimberg         const CeedScalar *interp, *curl;
1095004e4986SSebastian Grimberg         CeedScalar       *d_interp, *d_curl;
1096004e4986SSebastian Grimberg 
1097004e4986SSebastian Grimberg         CeedCallBackend(CeedBasisGetNumQuadratureComponents(basis, CEED_EVAL_INTERP, &q_comp_interp));
1098004e4986SSebastian Grimberg         CeedCallBackend(CeedBasisGetNumQuadratureComponents(basis, CEED_EVAL_CURL, &q_comp_curl));
1099004e4986SSebastian Grimberg 
1100004e4986SSebastian Grimberg         CeedCallBackend(CeedBasisGetInterp(basis, &interp));
1101004e4986SSebastian Grimberg         CeedCallCuda(ceed, cudaMalloc((void **)&d_interp, interp_bytes * q_comp_interp));
1102004e4986SSebastian Grimberg         CeedCallCuda(ceed, cudaMemcpy(d_interp, interp, interp_bytes * q_comp_interp, cudaMemcpyHostToDevice));
1103004e4986SSebastian Grimberg         CeedCallBackend(CeedBasisGetCurl(basis, &curl));
1104004e4986SSebastian Grimberg         CeedCallCuda(ceed, cudaMalloc((void **)&d_curl, interp_bytes * q_comp_curl));
1105004e4986SSebastian Grimberg         CeedCallCuda(ceed, cudaMemcpy(d_curl, curl, interp_bytes * q_comp_curl, cudaMemcpyHostToDevice));
1106004e4986SSebastian Grimberg         if (in) {
1107004e4986SSebastian Grimberg           diag->d_interp_in = d_interp;
1108004e4986SSebastian Grimberg           diag->d_curl_in   = d_curl;
1109004e4986SSebastian Grimberg         } else {
1110004e4986SSebastian Grimberg           diag->d_interp_out = d_interp;
1111004e4986SSebastian Grimberg           diag->d_curl_out   = d_curl;
1112004e4986SSebastian Grimberg         }
1113004e4986SSebastian Grimberg       } break;
1114004e4986SSebastian Grimberg     }
1115004e4986SSebastian Grimberg   }
1116004e4986SSebastian Grimberg 
1117004e4986SSebastian Grimberg   // Arrays of eval_modes
1118004e4986SSebastian Grimberg   CeedCallCuda(ceed, cudaMalloc((void **)&diag->d_eval_modes_in, num_eval_modes_in * eval_modes_bytes));
1119004e4986SSebastian Grimberg   CeedCallCuda(ceed, cudaMemcpy(diag->d_eval_modes_in, eval_modes_in, num_eval_modes_in * eval_modes_bytes, cudaMemcpyHostToDevice));
1120004e4986SSebastian Grimberg   CeedCallCuda(ceed, cudaMalloc((void **)&diag->d_eval_modes_out, num_eval_modes_out * eval_modes_bytes));
1121004e4986SSebastian Grimberg   CeedCallCuda(ceed, cudaMemcpy(diag->d_eval_modes_out, eval_modes_out, num_eval_modes_out * eval_modes_bytes, cudaMemcpyHostToDevice));
1122004e4986SSebastian Grimberg   CeedCallBackend(CeedFree(&eval_modes_in));
1123004e4986SSebastian Grimberg   CeedCallBackend(CeedFree(&eval_modes_out));
11240d0321e0SJeremy L Thompson   return CEED_ERROR_SUCCESS;
11250d0321e0SJeremy L Thompson }
11260d0321e0SJeremy L Thompson 
11270d0321e0SJeremy L Thompson //------------------------------------------------------------------------------
1128cbfe683aSSebastian Grimberg // Assemble Diagonal Setup (Compilation)
1129cbfe683aSSebastian Grimberg //------------------------------------------------------------------------------
1130cbfe683aSSebastian Grimberg static inline int CeedOperatorAssembleDiagonalSetupCompile_Cuda(CeedOperator op, CeedInt use_ceedsize_idx, const bool is_point_block) {
1131cbfe683aSSebastian Grimberg   Ceed                ceed;
113222070f95SJeremy L Thompson   char               *diagonal_kernel_source;
113322070f95SJeremy L Thompson   const char         *diagonal_kernel_path;
1134cbfe683aSSebastian Grimberg   CeedInt             num_input_fields, num_output_fields, num_eval_modes_in = 0, num_eval_modes_out = 0;
1135cbfe683aSSebastian Grimberg   CeedInt             num_comp, q_comp, num_nodes, num_qpts;
1136cbfe683aSSebastian Grimberg   CeedBasis           basis_in = NULL, basis_out = NULL;
1137cbfe683aSSebastian Grimberg   CeedQFunctionField *qf_fields;
1138cbfe683aSSebastian Grimberg   CeedQFunction       qf;
1139cbfe683aSSebastian Grimberg   CeedOperatorField  *op_fields;
1140cbfe683aSSebastian Grimberg   CeedOperator_Cuda  *impl;
1141cbfe683aSSebastian Grimberg 
1142cbfe683aSSebastian Grimberg   CeedCallBackend(CeedOperatorGetCeed(op, &ceed));
1143cbfe683aSSebastian Grimberg   CeedCallBackend(CeedOperatorGetQFunction(op, &qf));
1144cbfe683aSSebastian Grimberg   CeedCallBackend(CeedQFunctionGetNumArgs(qf, &num_input_fields, &num_output_fields));
1145cbfe683aSSebastian Grimberg 
1146cbfe683aSSebastian Grimberg   // Determine active input basis
1147cbfe683aSSebastian Grimberg   CeedCallBackend(CeedOperatorGetFields(op, NULL, &op_fields, NULL, NULL));
1148cbfe683aSSebastian Grimberg   CeedCallBackend(CeedQFunctionGetFields(qf, NULL, &qf_fields, NULL, NULL));
1149cbfe683aSSebastian Grimberg   for (CeedInt i = 0; i < num_input_fields; i++) {
1150cbfe683aSSebastian Grimberg     CeedVector vec;
1151cbfe683aSSebastian Grimberg 
1152cbfe683aSSebastian Grimberg     CeedCallBackend(CeedOperatorFieldGetVector(op_fields[i], &vec));
1153cbfe683aSSebastian Grimberg     if (vec == CEED_VECTOR_ACTIVE) {
1154cbfe683aSSebastian Grimberg       CeedEvalMode eval_mode;
1155cbfe683aSSebastian Grimberg 
1156cbfe683aSSebastian Grimberg       CeedCallBackend(CeedOperatorFieldGetBasis(op_fields[i], &basis_in));
1157cbfe683aSSebastian Grimberg       CeedCallBackend(CeedQFunctionFieldGetEvalMode(qf_fields[i], &eval_mode));
1158cbfe683aSSebastian Grimberg       CeedCallBackend(CeedBasisGetNumQuadratureComponents(basis_in, eval_mode, &q_comp));
1159cbfe683aSSebastian Grimberg       if (eval_mode != CEED_EVAL_WEIGHT) {
1160cbfe683aSSebastian Grimberg         num_eval_modes_in += q_comp;
1161cbfe683aSSebastian Grimberg       }
1162cbfe683aSSebastian Grimberg     }
1163cbfe683aSSebastian Grimberg   }
1164cbfe683aSSebastian Grimberg 
1165cbfe683aSSebastian Grimberg   // Determine active output basis
1166cbfe683aSSebastian Grimberg   CeedCallBackend(CeedOperatorGetFields(op, NULL, NULL, NULL, &op_fields));
1167cbfe683aSSebastian Grimberg   CeedCallBackend(CeedQFunctionGetFields(qf, NULL, NULL, NULL, &qf_fields));
1168cbfe683aSSebastian Grimberg   for (CeedInt i = 0; i < num_output_fields; i++) {
1169cbfe683aSSebastian Grimberg     CeedVector vec;
1170cbfe683aSSebastian Grimberg 
1171cbfe683aSSebastian Grimberg     CeedCallBackend(CeedOperatorFieldGetVector(op_fields[i], &vec));
1172cbfe683aSSebastian Grimberg     if (vec == CEED_VECTOR_ACTIVE) {
1173cbfe683aSSebastian Grimberg       CeedEvalMode eval_mode;
1174cbfe683aSSebastian Grimberg 
1175cbfe683aSSebastian Grimberg       CeedCallBackend(CeedOperatorFieldGetBasis(op_fields[i], &basis_out));
1176cbfe683aSSebastian Grimberg       CeedCallBackend(CeedQFunctionFieldGetEvalMode(qf_fields[i], &eval_mode));
1177cbfe683aSSebastian Grimberg       CeedCallBackend(CeedBasisGetNumQuadratureComponents(basis_out, eval_mode, &q_comp));
1178cbfe683aSSebastian Grimberg       if (eval_mode != CEED_EVAL_WEIGHT) {
1179cbfe683aSSebastian Grimberg         num_eval_modes_out += q_comp;
1180cbfe683aSSebastian Grimberg       }
1181cbfe683aSSebastian Grimberg     }
1182cbfe683aSSebastian Grimberg   }
1183cbfe683aSSebastian Grimberg 
1184cbfe683aSSebastian Grimberg   // Operator data struct
1185cbfe683aSSebastian Grimberg   CeedCallBackend(CeedOperatorGetData(op, &impl));
1186cbfe683aSSebastian Grimberg   CeedOperatorDiag_Cuda *diag = impl->diag;
1187cbfe683aSSebastian Grimberg 
1188cbfe683aSSebastian Grimberg   // Assemble kernel
1189cbfe683aSSebastian Grimberg   CUmodule *module          = is_point_block ? &diag->module_point_block : &diag->module;
1190cbfe683aSSebastian Grimberg   CeedInt   elems_per_block = 1;
1191cbfe683aSSebastian Grimberg   CeedCallBackend(CeedBasisGetNumNodes(basis_in, &num_nodes));
1192cbfe683aSSebastian Grimberg   CeedCallBackend(CeedBasisGetNumComponents(basis_in, &num_comp));
1193cbfe683aSSebastian Grimberg   if (basis_in == CEED_BASIS_NONE) num_qpts = num_nodes;
1194cbfe683aSSebastian Grimberg   else CeedCallBackend(CeedBasisGetNumQuadraturePoints(basis_in, &num_qpts));
1195cbfe683aSSebastian Grimberg   CeedCallBackend(CeedGetJitAbsolutePath(ceed, "ceed/jit-source/cuda/cuda-ref-operator-assemble-diagonal.h", &diagonal_kernel_path));
1196cbfe683aSSebastian Grimberg   CeedDebug256(ceed, CEED_DEBUG_COLOR_SUCCESS, "----- Loading Diagonal Assembly Kernel Source -----\n");
1197cbfe683aSSebastian Grimberg   CeedCallBackend(CeedLoadSourceToBuffer(ceed, diagonal_kernel_path, &diagonal_kernel_source));
1198cbfe683aSSebastian Grimberg   CeedDebug256(ceed, CEED_DEBUG_COLOR_SUCCESS, "----- Loading Diagonal Assembly Source Complete! -----\n");
1199cbfe683aSSebastian Grimberg   CeedCallCuda(ceed, CeedCompile_Cuda(ceed, diagonal_kernel_source, module, 8, "NUM_EVAL_MODES_IN", num_eval_modes_in, "NUM_EVAL_MODES_OUT",
1200cbfe683aSSebastian Grimberg                                       num_eval_modes_out, "NUM_COMP", num_comp, "NUM_NODES", num_nodes, "NUM_QPTS", num_qpts, "USE_CEEDSIZE",
1201cbfe683aSSebastian Grimberg                                       use_ceedsize_idx, "USE_POINT_BLOCK", is_point_block ? 1 : 0, "BLOCK_SIZE", num_nodes * elems_per_block));
1202cbfe683aSSebastian Grimberg   CeedCallCuda(ceed, CeedGetKernel_Cuda(ceed, *module, "LinearDiagonal", is_point_block ? &diag->LinearPointBlock : &diag->LinearDiagonal));
1203cbfe683aSSebastian Grimberg   CeedCallBackend(CeedFree(&diagonal_kernel_path));
1204cbfe683aSSebastian Grimberg   CeedCallBackend(CeedFree(&diagonal_kernel_source));
1205cbfe683aSSebastian Grimberg   return CEED_ERROR_SUCCESS;
1206cbfe683aSSebastian Grimberg }
1207cbfe683aSSebastian Grimberg 
1208cbfe683aSSebastian Grimberg //------------------------------------------------------------------------------
1209004e4986SSebastian Grimberg // Assemble Diagonal Core
12100d0321e0SJeremy L Thompson //------------------------------------------------------------------------------
1211ca735530SJeremy L Thompson static inline int CeedOperatorAssembleDiagonalCore_Cuda(CeedOperator op, CeedVector assembled, CeedRequest *request, const bool is_point_block) {
12120d0321e0SJeremy L Thompson   Ceed                ceed;
1213cbfe683aSSebastian Grimberg   CeedInt             num_elem, num_nodes;
1214ca735530SJeremy L Thompson   CeedScalar         *elem_diag_array;
1215ca735530SJeremy L Thompson   const CeedScalar   *assembled_qf_array;
1216004e4986SSebastian Grimberg   CeedVector          assembled_qf   = NULL, elem_diag;
1217004e4986SSebastian Grimberg   CeedElemRestriction assembled_rstr = NULL, rstr_in, rstr_out, diag_rstr;
12180d0321e0SJeremy L Thompson   CeedOperator_Cuda  *impl;
1219ca735530SJeremy L Thompson 
1220ca735530SJeremy L Thompson   CeedCallBackend(CeedOperatorGetCeed(op, &ceed));
12212b730f8bSJeremy L Thompson   CeedCallBackend(CeedOperatorGetData(op, &impl));
12220d0321e0SJeremy L Thompson 
12230d0321e0SJeremy L Thompson   // Assemble QFunction
1224004e4986SSebastian Grimberg   CeedCallBackend(CeedOperatorLinearAssembleQFunctionBuildOrUpdate(op, &assembled_qf, &assembled_rstr, request));
1225004e4986SSebastian Grimberg   CeedCallBackend(CeedElemRestrictionDestroy(&assembled_rstr));
1226004e4986SSebastian Grimberg   CeedCallBackend(CeedVectorGetArrayRead(assembled_qf, CEED_MEM_DEVICE, &assembled_qf_array));
12270d0321e0SJeremy L Thompson 
1228cbfe683aSSebastian Grimberg   // Setup
1229cbfe683aSSebastian Grimberg   if (!impl->diag) CeedCallBackend(CeedOperatorAssembleDiagonalSetup_Cuda(op));
1230cbfe683aSSebastian Grimberg   CeedOperatorDiag_Cuda *diag = impl->diag;
1231cbfe683aSSebastian Grimberg 
1232cbfe683aSSebastian Grimberg   assert(diag != NULL);
1233cbfe683aSSebastian Grimberg 
1234cbfe683aSSebastian Grimberg   // Assemble kernel if needed
1235cbfe683aSSebastian Grimberg   if ((!is_point_block && !diag->LinearDiagonal) || (is_point_block && !diag->LinearPointBlock)) {
1236cbfe683aSSebastian Grimberg     CeedSize assembled_length, assembled_qf_length;
1237cbfe683aSSebastian Grimberg     CeedInt  use_ceedsize_idx = 0;
1238f7c1b517Snbeams     CeedCallBackend(CeedVectorGetLength(assembled, &assembled_length));
1239ca735530SJeremy L Thompson     CeedCallBackend(CeedVectorGetLength(assembled_qf, &assembled_qf_length));
1240ca735530SJeremy L Thompson     if ((assembled_length > INT_MAX) || (assembled_qf_length > INT_MAX)) use_ceedsize_idx = 1;
1241f7c1b517Snbeams 
1242cbfe683aSSebastian Grimberg     CeedCallBackend(CeedOperatorAssembleDiagonalSetupCompile_Cuda(op, use_ceedsize_idx, is_point_block));
1243cbfe683aSSebastian Grimberg   }
12440d0321e0SJeremy L Thompson 
1245004e4986SSebastian Grimberg   // Restriction and diagonal vector
1246004e4986SSebastian Grimberg   CeedCallBackend(CeedOperatorGetActiveElemRestrictions(op, &rstr_in, &rstr_out));
1247004e4986SSebastian Grimberg   CeedCheck(rstr_in == rstr_out, ceed, CEED_ERROR_BACKEND,
1248004e4986SSebastian Grimberg             "Cannot assemble operator diagonal with different input and output active element restrictions");
1249004e4986SSebastian Grimberg   if (!is_point_block && !diag->diag_rstr) {
1250004e4986SSebastian Grimberg     CeedCallBackend(CeedElemRestrictionCreateUnsignedCopy(rstr_out, &diag->diag_rstr));
1251004e4986SSebastian Grimberg     CeedCallBackend(CeedElemRestrictionCreateVector(diag->diag_rstr, NULL, &diag->elem_diag));
1252004e4986SSebastian Grimberg   } else if (is_point_block && !diag->point_block_diag_rstr) {
1253004e4986SSebastian Grimberg     CeedCallBackend(CeedOperatorCreateActivePointBlockRestriction(rstr_out, &diag->point_block_diag_rstr));
1254004e4986SSebastian Grimberg     CeedCallBackend(CeedElemRestrictionCreateVector(diag->point_block_diag_rstr, NULL, &diag->point_block_elem_diag));
12550d0321e0SJeremy L Thompson   }
1256004e4986SSebastian Grimberg   diag_rstr = is_point_block ? diag->point_block_diag_rstr : diag->diag_rstr;
1257004e4986SSebastian Grimberg   elem_diag = is_point_block ? diag->point_block_elem_diag : diag->elem_diag;
1258ca735530SJeremy L Thompson   CeedCallBackend(CeedVectorSetValue(elem_diag, 0.0));
12590d0321e0SJeremy L Thompson 
126091db28b6SZach Atkins   // Only assemble diagonal if the basis has nodes, otherwise inputs are null pointers
1261004e4986SSebastian Grimberg   CeedCallBackend(CeedElemRestrictionGetElementSize(diag_rstr, &num_nodes));
1262004e4986SSebastian Grimberg   if (num_nodes > 0) {
12630d0321e0SJeremy L Thompson     // Assemble element operator diagonals
1264ca735530SJeremy L Thompson     CeedCallBackend(CeedElemRestrictionGetNumElements(diag_rstr, &num_elem));
1265004e4986SSebastian Grimberg     CeedCallBackend(CeedVectorGetArray(elem_diag, CEED_MEM_DEVICE, &elem_diag_array));
12660d0321e0SJeremy L Thompson 
12670d0321e0SJeremy L Thompson     // Compute the diagonal of B^T D B
1268004e4986SSebastian Grimberg     CeedInt elems_per_block = 1;
1269004e4986SSebastian Grimberg     CeedInt grid            = CeedDivUpInt(num_elem, elems_per_block);
1270004e4986SSebastian Grimberg     void   *args[]          = {(void *)&num_elem,      &diag->d_identity,       &diag->d_interp_in,  &diag->d_grad_in, &diag->d_div_in,
1271004e4986SSebastian Grimberg                                &diag->d_curl_in,       &diag->d_interp_out,     &diag->d_grad_out,   &diag->d_div_out, &diag->d_curl_out,
1272004e4986SSebastian Grimberg                                &diag->d_eval_modes_in, &diag->d_eval_modes_out, &assembled_qf_array, &elem_diag_array};
1273004e4986SSebastian Grimberg 
1274ca735530SJeremy L Thompson     if (is_point_block) {
1275004e4986SSebastian Grimberg       CeedCallBackend(CeedRunKernelDim_Cuda(ceed, diag->LinearPointBlock, grid, num_nodes, 1, elems_per_block, args));
12760d0321e0SJeremy L Thompson     } else {
1277004e4986SSebastian Grimberg       CeedCallBackend(CeedRunKernelDim_Cuda(ceed, diag->LinearDiagonal, grid, num_nodes, 1, elems_per_block, args));
12780d0321e0SJeremy L Thompson     }
12790d0321e0SJeremy L Thompson 
12800d0321e0SJeremy L Thompson     // Restore arrays
1281ca735530SJeremy L Thompson     CeedCallBackend(CeedVectorRestoreArray(elem_diag, &elem_diag_array));
1282ca735530SJeremy L Thompson     CeedCallBackend(CeedVectorRestoreArrayRead(assembled_qf, &assembled_qf_array));
128391db28b6SZach Atkins   }
12840d0321e0SJeremy L Thompson 
12850d0321e0SJeremy L Thompson   // Assemble local operator diagonal
1286ca735530SJeremy L Thompson   CeedCallBackend(CeedElemRestrictionApply(diag_rstr, CEED_TRANSPOSE, elem_diag, assembled, request));
12870d0321e0SJeremy L Thompson 
12880d0321e0SJeremy L Thompson   // Cleanup
1289ca735530SJeremy L Thompson   CeedCallBackend(CeedVectorDestroy(&assembled_qf));
12900d0321e0SJeremy L Thompson   return CEED_ERROR_SUCCESS;
12910d0321e0SJeremy L Thompson }
12920d0321e0SJeremy L Thompson 
12930d0321e0SJeremy L Thompson //------------------------------------------------------------------------------
12940d0321e0SJeremy L Thompson // Assemble Linear Diagonal
12950d0321e0SJeremy L Thompson //------------------------------------------------------------------------------
12962b730f8bSJeremy L Thompson static int CeedOperatorLinearAssembleAddDiagonal_Cuda(CeedOperator op, CeedVector assembled, CeedRequest *request) {
12972b730f8bSJeremy L Thompson   CeedCallBackend(CeedOperatorAssembleDiagonalCore_Cuda(op, assembled, request, false));
12986aa95790SJeremy L Thompson   return CEED_ERROR_SUCCESS;
12990d0321e0SJeremy L Thompson }
13000d0321e0SJeremy L Thompson 
13010d0321e0SJeremy L Thompson //------------------------------------------------------------------------------
13020d0321e0SJeremy L Thompson // Assemble Linear Point Block Diagonal
13030d0321e0SJeremy L Thompson //------------------------------------------------------------------------------
13042b730f8bSJeremy L Thompson static int CeedOperatorLinearAssembleAddPointBlockDiagonal_Cuda(CeedOperator op, CeedVector assembled, CeedRequest *request) {
13052b730f8bSJeremy L Thompson   CeedCallBackend(CeedOperatorAssembleDiagonalCore_Cuda(op, assembled, request, true));
13066aa95790SJeremy L Thompson   return CEED_ERROR_SUCCESS;
13070d0321e0SJeremy L Thompson }
13080d0321e0SJeremy L Thompson 
13090d0321e0SJeremy L Thompson //------------------------------------------------------------------------------
1310004e4986SSebastian Grimberg // Single Operator Assembly Setup
1311cc132f9aSnbeams //------------------------------------------------------------------------------
1312f7c1b517Snbeams static int CeedSingleOperatorAssembleSetup_Cuda(CeedOperator op, CeedInt use_ceedsize_idx) {
1313cc132f9aSnbeams   Ceed                ceed;
1314004e4986SSebastian Grimberg   Ceed_Cuda          *cuda_data;
131522070f95SJeremy L Thompson   char               *assembly_kernel_source;
131622070f95SJeremy L Thompson   const char         *assembly_kernel_path;
1317004e4986SSebastian Grimberg   CeedInt             num_input_fields, num_output_fields, num_eval_modes_in = 0, num_eval_modes_out = 0;
13183b38d1dfSJeremy L Thompson   CeedInt             elem_size_in, num_qpts_in = 0, num_comp_in, elem_size_out, num_qpts_out, num_comp_out, q_comp;
1319004e4986SSebastian Grimberg   CeedEvalMode       *eval_modes_in = NULL, *eval_modes_out = NULL;
1320ca735530SJeremy L Thompson   CeedElemRestriction rstr_in = NULL, rstr_out = NULL;
1321ca735530SJeremy L Thompson   CeedBasis           basis_in = NULL, basis_out = NULL;
1322ca735530SJeremy L Thompson   CeedQFunctionField *qf_fields;
1323ca735530SJeremy L Thompson   CeedQFunction       qf;
1324ca735530SJeremy L Thompson   CeedOperatorField  *input_fields, *output_fields;
1325cc132f9aSnbeams   CeedOperator_Cuda  *impl;
1326ca735530SJeremy L Thompson 
1327ca735530SJeremy L Thompson   CeedCallBackend(CeedOperatorGetCeed(op, &ceed));
13282b730f8bSJeremy L Thompson   CeedCallBackend(CeedOperatorGetData(op, &impl));
1329cc132f9aSnbeams 
1330cc132f9aSnbeams   // Get intput and output fields
13312b730f8bSJeremy L Thompson   CeedCallBackend(CeedOperatorGetFields(op, &num_input_fields, &input_fields, &num_output_fields, &output_fields));
1332cc132f9aSnbeams 
1333cc132f9aSnbeams   // Determine active input basis eval mode
13342b730f8bSJeremy L Thompson   CeedCallBackend(CeedOperatorGetQFunction(op, &qf));
13352b730f8bSJeremy L Thompson   CeedCallBackend(CeedQFunctionGetFields(qf, NULL, &qf_fields, NULL, NULL));
1336cc132f9aSnbeams   for (CeedInt i = 0; i < num_input_fields; i++) {
1337cc132f9aSnbeams     CeedVector vec;
1338ca735530SJeremy L Thompson 
13392b730f8bSJeremy L Thompson     CeedCallBackend(CeedOperatorFieldGetVector(input_fields[i], &vec));
1340cc132f9aSnbeams     if (vec == CEED_VECTOR_ACTIVE) {
1341004e4986SSebastian Grimberg       CeedBasis    basis;
1342ca735530SJeremy L Thompson       CeedEvalMode eval_mode;
1343ca735530SJeremy L Thompson 
1344004e4986SSebastian Grimberg       CeedCallBackend(CeedOperatorFieldGetBasis(input_fields[i], &basis));
1345004e4986SSebastian Grimberg       CeedCheck(!basis_in || basis_in == basis, ceed, CEED_ERROR_BACKEND, "Backend does not implement operator assembly with multiple active bases");
1346004e4986SSebastian Grimberg       basis_in = basis;
13472b730f8bSJeremy L Thompson       CeedCallBackend(CeedOperatorFieldGetElemRestriction(input_fields[i], &rstr_in));
1348004e4986SSebastian Grimberg       CeedCallBackend(CeedElemRestrictionGetElementSize(rstr_in, &elem_size_in));
1349004e4986SSebastian Grimberg       if (basis_in == CEED_BASIS_NONE) num_qpts_in = elem_size_in;
1350004e4986SSebastian Grimberg       else CeedCallBackend(CeedBasisGetNumQuadraturePoints(basis_in, &num_qpts_in));
13512b730f8bSJeremy L Thompson       CeedCallBackend(CeedQFunctionFieldGetEvalMode(qf_fields[i], &eval_mode));
1352004e4986SSebastian Grimberg       CeedCallBackend(CeedBasisGetNumQuadratureComponents(basis_in, eval_mode, &q_comp));
1353004e4986SSebastian Grimberg       if (eval_mode != CEED_EVAL_WEIGHT) {
1354004e4986SSebastian Grimberg         // q_comp = 1 if CEED_EVAL_NONE, CEED_EVAL_WEIGHT caught by QF Assembly
1355004e4986SSebastian Grimberg         CeedCallBackend(CeedRealloc(num_eval_modes_in + q_comp, &eval_modes_in));
1356004e4986SSebastian Grimberg         for (CeedInt d = 0; d < q_comp; d++) {
1357004e4986SSebastian Grimberg           eval_modes_in[num_eval_modes_in + d] = eval_mode;
1358cc132f9aSnbeams         }
1359004e4986SSebastian Grimberg         num_eval_modes_in += q_comp;
1360cc132f9aSnbeams       }
1361cc132f9aSnbeams     }
1362cc132f9aSnbeams   }
1363cc132f9aSnbeams 
1364cc132f9aSnbeams   // Determine active output basis; basis_out and rstr_out only used if same as input, TODO
13652b730f8bSJeremy L Thompson   CeedCallBackend(CeedQFunctionGetFields(qf, NULL, NULL, NULL, &qf_fields));
1366cc132f9aSnbeams   for (CeedInt i = 0; i < num_output_fields; i++) {
1367cc132f9aSnbeams     CeedVector vec;
1368ca735530SJeremy L Thompson 
13692b730f8bSJeremy L Thompson     CeedCallBackend(CeedOperatorFieldGetVector(output_fields[i], &vec));
1370cc132f9aSnbeams     if (vec == CEED_VECTOR_ACTIVE) {
1371004e4986SSebastian Grimberg       CeedBasis    basis;
1372ca735530SJeremy L Thompson       CeedEvalMode eval_mode;
1373ca735530SJeremy L Thompson 
1374004e4986SSebastian Grimberg       CeedCallBackend(CeedOperatorFieldGetBasis(output_fields[i], &basis));
1375004e4986SSebastian Grimberg       CeedCheck(!basis_out || basis_out == basis, ceed, CEED_ERROR_BACKEND,
1376004e4986SSebastian Grimberg                 "Backend does not implement operator assembly with multiple active bases");
1377004e4986SSebastian Grimberg       basis_out = basis;
13782b730f8bSJeremy L Thompson       CeedCallBackend(CeedOperatorFieldGetElemRestriction(output_fields[i], &rstr_out));
1379004e4986SSebastian Grimberg       CeedCallBackend(CeedElemRestrictionGetElementSize(rstr_out, &elem_size_out));
1380004e4986SSebastian Grimberg       if (basis_out == CEED_BASIS_NONE) num_qpts_out = elem_size_out;
1381004e4986SSebastian Grimberg       else CeedCallBackend(CeedBasisGetNumQuadraturePoints(basis_out, &num_qpts_out));
1382004e4986SSebastian Grimberg       CeedCheck(num_qpts_in == num_qpts_out, ceed, CEED_ERROR_UNSUPPORTED,
1383004e4986SSebastian Grimberg                 "Active input and output bases must have the same number of quadrature points");
13842b730f8bSJeremy L Thompson       CeedCallBackend(CeedQFunctionFieldGetEvalMode(qf_fields[i], &eval_mode));
1385004e4986SSebastian Grimberg       CeedCallBackend(CeedBasisGetNumQuadratureComponents(basis_out, eval_mode, &q_comp));
1386004e4986SSebastian Grimberg       if (eval_mode != CEED_EVAL_WEIGHT) {
1387004e4986SSebastian Grimberg         // q_comp = 1 if CEED_EVAL_NONE, CEED_EVAL_WEIGHT caught by QF Assembly
1388004e4986SSebastian Grimberg         CeedCallBackend(CeedRealloc(num_eval_modes_out + q_comp, &eval_modes_out));
1389004e4986SSebastian Grimberg         for (CeedInt d = 0; d < q_comp; d++) {
1390004e4986SSebastian Grimberg           eval_modes_out[num_eval_modes_out + d] = eval_mode;
1391004e4986SSebastian Grimberg         }
1392004e4986SSebastian Grimberg         num_eval_modes_out += q_comp;
1393cc132f9aSnbeams       }
1394cc132f9aSnbeams     }
1395cc132f9aSnbeams   }
1396004e4986SSebastian Grimberg   CeedCheck(num_eval_modes_in > 0 && num_eval_modes_out > 0, ceed, CEED_ERROR_UNSUPPORTED, "Cannot assemble operator without inputs/outputs");
1397cc132f9aSnbeams 
13982b730f8bSJeremy L Thompson   CeedCallBackend(CeedCalloc(1, &impl->asmb));
1399cc132f9aSnbeams   CeedOperatorAssemble_Cuda *asmb = impl->asmb;
1400004e4986SSebastian Grimberg   asmb->elems_per_block           = 1;
1401004e4986SSebastian Grimberg   asmb->block_size_x              = elem_size_in;
1402004e4986SSebastian Grimberg   asmb->block_size_y              = elem_size_out;
1403ca735530SJeremy L Thompson 
14042b730f8bSJeremy L Thompson   CeedCallBackend(CeedGetData(ceed, &cuda_data));
1405004e4986SSebastian Grimberg   bool fallback = asmb->block_size_x * asmb->block_size_y * asmb->elems_per_block > cuda_data->device_prop.maxThreadsPerBlock;
1406004e4986SSebastian Grimberg 
1407004e4986SSebastian Grimberg   if (fallback) {
1408004e4986SSebastian Grimberg     // Use fallback kernel with 1D threadblock
1409004e4986SSebastian Grimberg     asmb->block_size_y = 1;
1410004e4986SSebastian Grimberg   }
1411004e4986SSebastian Grimberg 
1412004e4986SSebastian Grimberg   // Compile kernels
1413004e4986SSebastian Grimberg   CeedCallBackend(CeedElemRestrictionGetNumComponents(rstr_in, &num_comp_in));
1414004e4986SSebastian Grimberg   CeedCallBackend(CeedElemRestrictionGetNumComponents(rstr_out, &num_comp_out));
14152b730f8bSJeremy L Thompson   CeedCallBackend(CeedGetJitAbsolutePath(ceed, "ceed/jit-source/cuda/cuda-ref-operator-assemble.h", &assembly_kernel_path));
141623d4529eSJeremy L Thompson   CeedDebug256(ceed, CEED_DEBUG_COLOR_SUCCESS, "----- Loading Assembly Kernel Source -----\n");
14172b730f8bSJeremy L Thompson   CeedCallBackend(CeedLoadSourceToBuffer(ceed, assembly_kernel_path, &assembly_kernel_source));
141823d4529eSJeremy L Thompson   CeedDebug256(ceed, CEED_DEBUG_COLOR_SUCCESS, "----- Loading Assembly Source Complete! -----\n");
1419004e4986SSebastian Grimberg   CeedCallBackend(CeedCompile_Cuda(ceed, assembly_kernel_source, &asmb->module, 10, "NUM_EVAL_MODES_IN", num_eval_modes_in, "NUM_EVAL_MODES_OUT",
1420004e4986SSebastian Grimberg                                    num_eval_modes_out, "NUM_COMP_IN", num_comp_in, "NUM_COMP_OUT", num_comp_out, "NUM_NODES_IN", elem_size_in,
1421004e4986SSebastian Grimberg                                    "NUM_NODES_OUT", elem_size_out, "NUM_QPTS", num_qpts_in, "BLOCK_SIZE",
1422cbfe683aSSebastian Grimberg                                    asmb->block_size_x * asmb->block_size_y * asmb->elems_per_block, "BLOCK_SIZE_Y", asmb->block_size_y,
1423cbfe683aSSebastian Grimberg                                    "USE_CEEDSIZE", use_ceedsize_idx));
1424004e4986SSebastian Grimberg   CeedCallBackend(CeedGetKernel_Cuda(ceed, asmb->module, "LinearAssemble", &asmb->LinearAssemble));
14252b730f8bSJeremy L Thompson   CeedCallBackend(CeedFree(&assembly_kernel_path));
14262b730f8bSJeremy L Thompson   CeedCallBackend(CeedFree(&assembly_kernel_source));
1427cc132f9aSnbeams 
1428004e4986SSebastian Grimberg   // Load into B_in, in order that they will be used in eval_modes_in
1429004e4986SSebastian Grimberg   {
1430004e4986SSebastian Grimberg     const CeedInt in_bytes           = elem_size_in * num_qpts_in * num_eval_modes_in * sizeof(CeedScalar);
1431004e4986SSebastian Grimberg     CeedInt       d_in               = 0;
1432004e4986SSebastian Grimberg     CeedEvalMode  eval_modes_in_prev = CEED_EVAL_NONE;
1433004e4986SSebastian Grimberg     bool          has_eval_none      = false;
1434004e4986SSebastian Grimberg     CeedScalar   *identity           = NULL;
1435ca735530SJeremy L Thompson 
1436004e4986SSebastian Grimberg     for (CeedInt i = 0; i < num_eval_modes_in; i++) {
1437004e4986SSebastian Grimberg       has_eval_none = has_eval_none || (eval_modes_in[i] == CEED_EVAL_NONE);
1438004e4986SSebastian Grimberg     }
1439004e4986SSebastian Grimberg     if (has_eval_none) {
1440004e4986SSebastian Grimberg       CeedCallBackend(CeedCalloc(elem_size_in * num_qpts_in, &identity));
1441004e4986SSebastian 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;
1442004e4986SSebastian Grimberg     }
1443cc132f9aSnbeams 
1444004e4986SSebastian Grimberg     CeedCallCuda(ceed, cudaMalloc((void **)&asmb->d_B_in, in_bytes));
1445004e4986SSebastian Grimberg     for (CeedInt i = 0; i < num_eval_modes_in; i++) {
1446004e4986SSebastian Grimberg       const CeedScalar *h_B_in;
1447ca735530SJeremy L Thompson 
1448004e4986SSebastian Grimberg       CeedCallBackend(CeedOperatorGetBasisPointer(basis_in, eval_modes_in[i], identity, &h_B_in));
1449004e4986SSebastian Grimberg       CeedCallBackend(CeedBasisGetNumQuadratureComponents(basis_in, eval_modes_in[i], &q_comp));
1450004e4986SSebastian Grimberg       if (q_comp > 1) {
1451004e4986SSebastian Grimberg         if (i == 0 || eval_modes_in[i] != eval_modes_in_prev) d_in = 0;
1452004e4986SSebastian Grimberg         else h_B_in = &h_B_in[(++d_in) * elem_size_in * num_qpts_in];
1453004e4986SSebastian Grimberg       }
1454004e4986SSebastian Grimberg       eval_modes_in_prev = eval_modes_in[i];
1455ca735530SJeremy L Thompson 
1456004e4986SSebastian 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),
1457004e4986SSebastian Grimberg                                     cudaMemcpyHostToDevice));
1458004e4986SSebastian Grimberg     }
1459004e4986SSebastian Grimberg 
1460004e4986SSebastian Grimberg     if (identity) {
1461004e4986SSebastian Grimberg       CeedCallBackend(CeedFree(&identity));
1462cc132f9aSnbeams     }
1463cc132f9aSnbeams   }
1464cc132f9aSnbeams 
1465004e4986SSebastian Grimberg   // Load into B_out, in order that they will be used in eval_modes_out
1466004e4986SSebastian Grimberg   {
1467004e4986SSebastian Grimberg     const CeedInt out_bytes           = elem_size_out * num_qpts_out * num_eval_modes_out * sizeof(CeedScalar);
1468004e4986SSebastian Grimberg     CeedInt       d_out               = 0;
1469004e4986SSebastian Grimberg     CeedEvalMode  eval_modes_out_prev = CEED_EVAL_NONE;
1470004e4986SSebastian Grimberg     bool          has_eval_none       = false;
1471004e4986SSebastian Grimberg     CeedScalar   *identity            = NULL;
1472ca735530SJeremy L Thompson 
1473004e4986SSebastian Grimberg     for (CeedInt i = 0; i < num_eval_modes_out; i++) {
1474004e4986SSebastian Grimberg       has_eval_none = has_eval_none || (eval_modes_out[i] == CEED_EVAL_NONE);
1475004e4986SSebastian Grimberg     }
1476004e4986SSebastian Grimberg     if (has_eval_none) {
1477004e4986SSebastian Grimberg       CeedCallBackend(CeedCalloc(elem_size_out * num_qpts_out, &identity));
1478004e4986SSebastian 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;
1479cc132f9aSnbeams     }
1480cc132f9aSnbeams 
1481004e4986SSebastian Grimberg     CeedCallCuda(ceed, cudaMalloc((void **)&asmb->d_B_out, out_bytes));
1482004e4986SSebastian Grimberg     for (CeedInt i = 0; i < num_eval_modes_out; i++) {
1483004e4986SSebastian Grimberg       const CeedScalar *h_B_out;
1484ca735530SJeremy L Thompson 
1485004e4986SSebastian Grimberg       CeedCallBackend(CeedOperatorGetBasisPointer(basis_out, eval_modes_out[i], identity, &h_B_out));
1486004e4986SSebastian Grimberg       CeedCallBackend(CeedBasisGetNumQuadratureComponents(basis_out, eval_modes_out[i], &q_comp));
1487004e4986SSebastian Grimberg       if (q_comp > 1) {
1488004e4986SSebastian Grimberg         if (i == 0 || eval_modes_out[i] != eval_modes_out_prev) d_out = 0;
1489004e4986SSebastian Grimberg         else h_B_out = &h_B_out[(++d_out) * elem_size_out * num_qpts_out];
1490004e4986SSebastian Grimberg       }
1491004e4986SSebastian Grimberg       eval_modes_out_prev = eval_modes_out[i];
1492ca735530SJeremy L Thompson 
1493004e4986SSebastian 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),
1494004e4986SSebastian Grimberg                                     cudaMemcpyHostToDevice));
1495004e4986SSebastian Grimberg     }
1496004e4986SSebastian Grimberg 
1497004e4986SSebastian Grimberg     if (identity) {
1498004e4986SSebastian Grimberg       CeedCallBackend(CeedFree(&identity));
1499cc132f9aSnbeams     }
1500cc132f9aSnbeams   }
1501cc132f9aSnbeams   return CEED_ERROR_SUCCESS;
1502cc132f9aSnbeams }
1503cc132f9aSnbeams 
1504cc132f9aSnbeams //------------------------------------------------------------------------------
1505cefa2673SJeremy L Thompson // Assemble matrix data for COO matrix of assembled operator.
1506cefa2673SJeremy L Thompson // The sparsity pattern is set by CeedOperatorLinearAssembleSymbolic.
1507cefa2673SJeremy L Thompson //
1508ea61e9acSJeremy L Thompson // Note that this (and other assembly routines) currently assume only one active input restriction/basis per operator (could have multiple basis eval
1509cefa2673SJeremy L Thompson // modes).
1510cefa2673SJeremy L Thompson // TODO: allow multiple active input restrictions/basis objects
1511cc132f9aSnbeams //------------------------------------------------------------------------------
15122b730f8bSJeremy L Thompson static int CeedSingleOperatorAssemble_Cuda(CeedOperator op, CeedInt offset, CeedVector values) {
1513cc132f9aSnbeams   Ceed                ceed;
1514ca735530SJeremy L Thompson   CeedSize            values_length = 0, assembled_qf_length = 0;
1515004e4986SSebastian Grimberg   CeedInt             use_ceedsize_idx = 0, num_elem_in, num_elem_out, elem_size_in, elem_size_out;
1516ca735530SJeremy L Thompson   CeedScalar         *values_array;
1517004e4986SSebastian Grimberg   const CeedScalar   *assembled_qf_array;
1518ca735530SJeremy L Thompson   CeedVector          assembled_qf   = NULL;
1519004e4986SSebastian Grimberg   CeedElemRestriction assembled_rstr = NULL, rstr_in, rstr_out;
1520004e4986SSebastian Grimberg   CeedRestrictionType rstr_type_in, rstr_type_out;
1521004e4986SSebastian Grimberg   const bool         *orients_in = NULL, *orients_out = NULL;
1522004e4986SSebastian Grimberg   const CeedInt8     *curl_orients_in = NULL, *curl_orients_out = NULL;
1523cc132f9aSnbeams   CeedOperator_Cuda  *impl;
1524ca735530SJeremy L Thompson 
1525ca735530SJeremy L Thompson   CeedCallBackend(CeedOperatorGetCeed(op, &ceed));
15262b730f8bSJeremy L Thompson   CeedCallBackend(CeedOperatorGetData(op, &impl));
1527cc132f9aSnbeams 
1528cc132f9aSnbeams   // Assemble QFunction
1529004e4986SSebastian Grimberg   CeedCallBackend(CeedOperatorLinearAssembleQFunctionBuildOrUpdate(op, &assembled_qf, &assembled_rstr, CEED_REQUEST_IMMEDIATE));
1530004e4986SSebastian Grimberg   CeedCallBackend(CeedElemRestrictionDestroy(&assembled_rstr));
1531004e4986SSebastian Grimberg   CeedCallBackend(CeedVectorGetArrayRead(assembled_qf, CEED_MEM_DEVICE, &assembled_qf_array));
1532cc132f9aSnbeams 
1533f7c1b517Snbeams   CeedCallBackend(CeedVectorGetLength(values, &values_length));
1534f7c1b517Snbeams   CeedCallBackend(CeedVectorGetLength(assembled_qf, &assembled_qf_length));
1535f7c1b517Snbeams   if ((values_length > INT_MAX) || (assembled_qf_length > INT_MAX)) use_ceedsize_idx = 1;
1536004e4986SSebastian Grimberg 
1537f7c1b517Snbeams   // Setup
1538004e4986SSebastian Grimberg   if (!impl->asmb) CeedCallBackend(CeedSingleOperatorAssembleSetup_Cuda(op, use_ceedsize_idx));
1539004e4986SSebastian Grimberg   CeedOperatorAssemble_Cuda *asmb = impl->asmb;
1540004e4986SSebastian Grimberg 
1541004e4986SSebastian Grimberg   assert(asmb != NULL);
1542004e4986SSebastian Grimberg 
1543004e4986SSebastian Grimberg   // Assemble element operator
1544004e4986SSebastian Grimberg   CeedCallBackend(CeedVectorGetArray(values, CEED_MEM_DEVICE, &values_array));
1545004e4986SSebastian Grimberg   values_array += offset;
1546004e4986SSebastian Grimberg 
1547004e4986SSebastian Grimberg   CeedCallBackend(CeedOperatorGetActiveElemRestrictions(op, &rstr_in, &rstr_out));
1548004e4986SSebastian Grimberg   CeedCallBackend(CeedElemRestrictionGetNumElements(rstr_in, &num_elem_in));
1549004e4986SSebastian Grimberg   CeedCallBackend(CeedElemRestrictionGetElementSize(rstr_in, &elem_size_in));
1550004e4986SSebastian Grimberg 
1551004e4986SSebastian Grimberg   CeedCallBackend(CeedElemRestrictionGetType(rstr_in, &rstr_type_in));
1552004e4986SSebastian Grimberg   if (rstr_type_in == CEED_RESTRICTION_ORIENTED) {
1553004e4986SSebastian Grimberg     CeedCallBackend(CeedElemRestrictionGetOrientations(rstr_in, CEED_MEM_DEVICE, &orients_in));
1554004e4986SSebastian Grimberg   } else if (rstr_type_in == CEED_RESTRICTION_CURL_ORIENTED) {
1555004e4986SSebastian Grimberg     CeedCallBackend(CeedElemRestrictionGetCurlOrientations(rstr_in, CEED_MEM_DEVICE, &curl_orients_in));
1556004e4986SSebastian Grimberg   }
1557004e4986SSebastian Grimberg 
1558004e4986SSebastian Grimberg   if (rstr_in != rstr_out) {
1559004e4986SSebastian Grimberg     CeedCallBackend(CeedElemRestrictionGetNumElements(rstr_out, &num_elem_out));
1560004e4986SSebastian Grimberg     CeedCheck(num_elem_in == num_elem_out, ceed, CEED_ERROR_UNSUPPORTED,
1561004e4986SSebastian Grimberg               "Active input and output operator restrictions must have the same number of elements");
1562004e4986SSebastian Grimberg     CeedCallBackend(CeedElemRestrictionGetElementSize(rstr_out, &elem_size_out));
1563004e4986SSebastian Grimberg 
1564004e4986SSebastian Grimberg     CeedCallBackend(CeedElemRestrictionGetType(rstr_out, &rstr_type_out));
1565004e4986SSebastian Grimberg     if (rstr_type_out == CEED_RESTRICTION_ORIENTED) {
1566004e4986SSebastian Grimberg       CeedCallBackend(CeedElemRestrictionGetOrientations(rstr_out, CEED_MEM_DEVICE, &orients_out));
1567004e4986SSebastian Grimberg     } else if (rstr_type_out == CEED_RESTRICTION_CURL_ORIENTED) {
1568004e4986SSebastian Grimberg       CeedCallBackend(CeedElemRestrictionGetCurlOrientations(rstr_out, CEED_MEM_DEVICE, &curl_orients_out));
1569004e4986SSebastian Grimberg     }
1570004e4986SSebastian Grimberg   } else {
1571004e4986SSebastian Grimberg     elem_size_out    = elem_size_in;
1572004e4986SSebastian Grimberg     orients_out      = orients_in;
1573004e4986SSebastian Grimberg     curl_orients_out = curl_orients_in;
1574f7c1b517Snbeams   }
1575f7c1b517Snbeams 
1576cc132f9aSnbeams   // Compute B^T D B
1577004e4986SSebastian Grimberg   CeedInt shared_mem =
1578004e4986SSebastian 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)) *
1579004e4986SSebastian Grimberg       sizeof(CeedScalar);
1580004e4986SSebastian Grimberg   CeedInt grid   = CeedDivUpInt(num_elem_in, asmb->elems_per_block);
1581004e4986SSebastian Grimberg   void   *args[] = {(void *)&num_elem_in, &asmb->d_B_in,     &asmb->d_B_out,      &orients_in,  &curl_orients_in,
1582004e4986SSebastian Grimberg                     &orients_out,         &curl_orients_out, &assembled_qf_array, &values_array};
1583ca735530SJeremy L Thompson 
15842b730f8bSJeremy L Thompson   CeedCallBackend(
1585004e4986SSebastian Grimberg       CeedRunKernelDimShared_Cuda(ceed, asmb->LinearAssemble, grid, asmb->block_size_x, asmb->block_size_y, asmb->elems_per_block, shared_mem, args));
1586cc132f9aSnbeams 
1587cc132f9aSnbeams   // Restore arrays
15882b730f8bSJeremy L Thompson   CeedCallBackend(CeedVectorRestoreArray(values, &values_array));
1589004e4986SSebastian Grimberg   CeedCallBackend(CeedVectorRestoreArrayRead(assembled_qf, &assembled_qf_array));
1590cc132f9aSnbeams 
1591cc132f9aSnbeams   // Cleanup
15922b730f8bSJeremy L Thompson   CeedCallBackend(CeedVectorDestroy(&assembled_qf));
1593004e4986SSebastian Grimberg   if (rstr_type_in == CEED_RESTRICTION_ORIENTED) {
1594004e4986SSebastian Grimberg     CeedCallBackend(CeedElemRestrictionRestoreOrientations(rstr_in, &orients_in));
1595004e4986SSebastian Grimberg   } else if (rstr_type_in == CEED_RESTRICTION_CURL_ORIENTED) {
1596004e4986SSebastian Grimberg     CeedCallBackend(CeedElemRestrictionRestoreCurlOrientations(rstr_in, &curl_orients_in));
1597004e4986SSebastian Grimberg   }
1598004e4986SSebastian Grimberg   if (rstr_in != rstr_out) {
1599004e4986SSebastian Grimberg     if (rstr_type_out == CEED_RESTRICTION_ORIENTED) {
1600004e4986SSebastian Grimberg       CeedCallBackend(CeedElemRestrictionRestoreOrientations(rstr_out, &orients_out));
1601004e4986SSebastian Grimberg     } else if (rstr_type_out == CEED_RESTRICTION_CURL_ORIENTED) {
1602004e4986SSebastian Grimberg       CeedCallBackend(CeedElemRestrictionRestoreCurlOrientations(rstr_out, &curl_orients_out));
1603004e4986SSebastian Grimberg     }
1604004e4986SSebastian Grimberg   }
1605cc132f9aSnbeams   return CEED_ERROR_SUCCESS;
1606cc132f9aSnbeams }
1607cc132f9aSnbeams 
1608cc132f9aSnbeams //------------------------------------------------------------------------------
1609756ca9e9SJeremy L Thompson // Assemble Linear QFunction AtPoints
1610756ca9e9SJeremy L Thompson //------------------------------------------------------------------------------
1611756ca9e9SJeremy L Thompson static int CeedOperatorLinearAssembleQFunctionAtPoints_Cuda(CeedOperator op, CeedVector *assembled, CeedElemRestriction *rstr, CeedRequest *request) {
1612756ca9e9SJeremy L Thompson   return CeedError(CeedOperatorReturnCeed(op), CEED_ERROR_BACKEND, "Backend does not implement CeedOperatorLinearAssembleQFunction");
1613756ca9e9SJeremy L Thompson }
1614756ca9e9SJeremy L Thompson 
1615756ca9e9SJeremy L Thompson //------------------------------------------------------------------------------
1616756ca9e9SJeremy L Thompson // Assemble Linear Diagonal AtPoints
1617756ca9e9SJeremy L Thompson //------------------------------------------------------------------------------
1618756ca9e9SJeremy L Thompson static int CeedOperatorLinearAssembleAddDiagonalAtPoints_Cuda(CeedOperator op, CeedVector assembled, CeedRequest *request) {
1619382e9c83SJeremy L Thompson   CeedInt             max_num_points, num_elem, num_input_fields, num_output_fields;
1620349fb27dSJeremy L Thompson   CeedScalar         *e_data[2 * CEED_FIELD_MAX] = {NULL};
1621349fb27dSJeremy L Thompson   CeedQFunctionField *qf_input_fields, *qf_output_fields;
1622349fb27dSJeremy L Thompson   CeedQFunction       qf;
1623349fb27dSJeremy L Thompson   CeedOperatorField  *op_input_fields, *op_output_fields;
1624349fb27dSJeremy L Thompson   CeedOperator_Cuda  *impl;
1625349fb27dSJeremy L Thompson 
1626349fb27dSJeremy L Thompson   CeedCallBackend(CeedOperatorGetData(op, &impl));
1627349fb27dSJeremy L Thompson   CeedCallBackend(CeedOperatorGetQFunction(op, &qf));
1628349fb27dSJeremy L Thompson   CeedCallBackend(CeedOperatorGetNumElements(op, &num_elem));
1629349fb27dSJeremy L Thompson   CeedCallBackend(CeedOperatorGetFields(op, &num_input_fields, &op_input_fields, &num_output_fields, &op_output_fields));
1630349fb27dSJeremy L Thompson   CeedCallBackend(CeedQFunctionGetFields(qf, NULL, &qf_input_fields, NULL, &qf_output_fields));
1631349fb27dSJeremy L Thompson   CeedInt num_points[num_elem];
1632349fb27dSJeremy L Thompson 
1633349fb27dSJeremy L Thompson   // Setup
1634349fb27dSJeremy L Thompson   CeedCallBackend(CeedOperatorSetupAtPoints_Cuda(op));
1635349fb27dSJeremy L Thompson   max_num_points = impl->max_num_points;
1636349fb27dSJeremy L Thompson   for (CeedInt i = 0; i < num_elem; i++) num_points[i] = max_num_points;
1637349fb27dSJeremy L Thompson 
1638*8a213570SJeremy L Thompson   // Create separate output e-vecs
1639*8a213570SJeremy L Thompson   if (impl->has_shared_e_vecs) {
1640*8a213570SJeremy L Thompson     for (CeedInt i = 0; i < impl->num_outputs; i++) {
1641*8a213570SJeremy L Thompson       CeedCallBackend(CeedVectorDestroy(&impl->q_vecs_out[i]));
1642*8a213570SJeremy L Thompson       CeedCallBackend(CeedVectorDestroy(&impl->e_vecs[impl->num_inputs + i]));
1643*8a213570SJeremy L Thompson     }
1644*8a213570SJeremy 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,
1645*8a213570SJeremy L Thompson                                                  num_input_fields, num_output_fields, max_num_points, num_elem));
1646*8a213570SJeremy L Thompson   }
1647*8a213570SJeremy L Thompson   impl->has_shared_e_vecs = false;
1648*8a213570SJeremy L Thompson 
1649349fb27dSJeremy L Thompson   // Input Evecs and Restriction
1650349fb27dSJeremy L Thompson   CeedCallBackend(CeedOperatorSetupInputs_Cuda(num_input_fields, qf_input_fields, op_input_fields, NULL, true, e_data, impl, request));
1651349fb27dSJeremy L Thompson 
1652349fb27dSJeremy L Thompson   // Get point coordinates
1653349fb27dSJeremy L Thompson   if (!impl->point_coords_elem) {
1654349fb27dSJeremy L Thompson     CeedVector          point_coords = NULL;
1655349fb27dSJeremy L Thompson     CeedElemRestriction rstr_points  = NULL;
1656349fb27dSJeremy L Thompson 
1657349fb27dSJeremy L Thompson     CeedCallBackend(CeedOperatorAtPointsGetPoints(op, &rstr_points, &point_coords));
1658349fb27dSJeremy L Thompson     CeedCallBackend(CeedElemRestrictionCreateVector(rstr_points, NULL, &impl->point_coords_elem));
1659349fb27dSJeremy L Thompson     CeedCallBackend(CeedElemRestrictionApply(rstr_points, CEED_NOTRANSPOSE, point_coords, impl->point_coords_elem, request));
1660349fb27dSJeremy L Thompson   }
1661349fb27dSJeremy L Thompson 
1662382e9c83SJeremy L Thompson   // Clear active input Qvecs
1663382e9c83SJeremy L Thompson   for (CeedInt i = 0; i < num_input_fields; i++) {
1664382e9c83SJeremy L Thompson     CeedVector vec;
1665382e9c83SJeremy L Thompson 
1666382e9c83SJeremy L Thompson     CeedCallBackend(CeedOperatorFieldGetVector(op_input_fields[i], &vec));
1667382e9c83SJeremy L Thompson     if (vec != CEED_VECTOR_ACTIVE) continue;
1668382e9c83SJeremy L Thompson     CeedCallBackend(CeedVectorSetValue(impl->q_vecs_in[i], 0.0));
1669382e9c83SJeremy L Thompson   }
1670382e9c83SJeremy L Thompson 
1671349fb27dSJeremy L Thompson   // Input basis apply if needed
1672349fb27dSJeremy L Thompson   CeedCallBackend(CeedOperatorInputBasisAtPoints_Cuda(num_elem, num_points, qf_input_fields, op_input_fields, num_input_fields, true, e_data, impl));
1673349fb27dSJeremy L Thompson 
1674349fb27dSJeremy L Thompson   // Output pointers, as necessary
1675349fb27dSJeremy L Thompson   for (CeedInt i = 0; i < num_output_fields; i++) {
1676349fb27dSJeremy L Thompson     CeedEvalMode eval_mode;
1677349fb27dSJeremy L Thompson 
1678349fb27dSJeremy L Thompson     CeedCallBackend(CeedQFunctionFieldGetEvalMode(qf_output_fields[i], &eval_mode));
1679349fb27dSJeremy L Thompson     if (eval_mode == CEED_EVAL_NONE) {
1680349fb27dSJeremy L Thompson       // Set the output Q-Vector to use the E-Vector data directly.
1681349fb27dSJeremy L Thompson       CeedCallBackend(CeedVectorGetArrayWrite(impl->e_vecs[i + impl->num_inputs], CEED_MEM_DEVICE, &e_data[i + num_input_fields]));
1682349fb27dSJeremy L Thompson       CeedCallBackend(CeedVectorSetArray(impl->q_vecs_out[i], CEED_MEM_DEVICE, CEED_USE_POINTER, e_data[i + num_input_fields]));
1683349fb27dSJeremy L Thompson     }
1684349fb27dSJeremy L Thompson   }
1685349fb27dSJeremy L Thompson 
1686349fb27dSJeremy L Thompson   // Loop over active fields
1687349fb27dSJeremy L Thompson   for (CeedInt i = 0; i < num_input_fields; i++) {
1688382e9c83SJeremy L Thompson     bool                is_active_at_points = true;
1689382e9c83SJeremy L Thompson     CeedInt             elem_size = 1, num_comp_active = 1, e_vec_size = 0;
1690382e9c83SJeremy L Thompson     CeedRestrictionType rstr_type;
1691382e9c83SJeremy L Thompson     CeedVector          vec;
1692382e9c83SJeremy L Thompson     CeedElemRestriction elem_rstr;
1693382e9c83SJeremy L Thompson 
1694382e9c83SJeremy L Thompson     CeedCallBackend(CeedOperatorFieldGetVector(op_input_fields[i], &vec));
1695382e9c83SJeremy L Thompson     // -- Skip non-active input
1696382e9c83SJeremy L Thompson     if (vec != CEED_VECTOR_ACTIVE) continue;
1697382e9c83SJeremy L Thompson 
1698382e9c83SJeremy L Thompson     // -- Get active restriction type
1699382e9c83SJeremy L Thompson     CeedCallBackend(CeedOperatorFieldGetElemRestriction(op_input_fields[i], &elem_rstr));
1700382e9c83SJeremy L Thompson     CeedCallBackend(CeedElemRestrictionGetType(elem_rstr, &rstr_type));
1701382e9c83SJeremy L Thompson     is_active_at_points = rstr_type == CEED_RESTRICTION_POINTS;
1702382e9c83SJeremy L Thompson     if (!is_active_at_points) CeedCallBackend(CeedElemRestrictionGetElementSize(elem_rstr, &elem_size));
1703382e9c83SJeremy L Thompson     else elem_size = max_num_points;
1704382e9c83SJeremy L Thompson     CeedCallBackend(CeedElemRestrictionGetNumComponents(elem_rstr, &num_comp_active));
1705382e9c83SJeremy L Thompson 
1706382e9c83SJeremy L Thompson     e_vec_size = elem_size * num_comp_active;
1707382e9c83SJeremy L Thompson     for (CeedInt s = 0; s < e_vec_size; s++) {
1708349fb27dSJeremy L Thompson       bool         is_active_input = false;
1709349fb27dSJeremy L Thompson       CeedEvalMode eval_mode;
1710349fb27dSJeremy L Thompson       CeedVector   vec;
1711349fb27dSJeremy L Thompson       CeedBasis    basis;
1712349fb27dSJeremy L Thompson 
1713349fb27dSJeremy L Thompson       CeedCallBackend(CeedOperatorFieldGetVector(op_input_fields[i], &vec));
1714349fb27dSJeremy L Thompson       // Skip non-active input
1715349fb27dSJeremy L Thompson       is_active_input = vec == CEED_VECTOR_ACTIVE;
1716349fb27dSJeremy L Thompson       if (!is_active_input) continue;
1717349fb27dSJeremy L Thompson 
1718349fb27dSJeremy L Thompson       // Update unit vector
171913062808SJeremy L Thompson       if (s == 0) CeedCallBackend(CeedVectorSetValue(impl->e_vecs[i], 0.0));
1720349fb27dSJeremy L Thompson       else CeedCallBackend(CeedVectorSetValueStrided(impl->e_vecs[i], s - 1, e_vec_size, 0.0));
1721349fb27dSJeremy L Thompson       CeedCallBackend(CeedVectorSetValueStrided(impl->e_vecs[i], s, e_vec_size, 1.0));
1722349fb27dSJeremy L Thompson 
1723349fb27dSJeremy L Thompson       // Basis action
1724349fb27dSJeremy L Thompson       CeedCallBackend(CeedQFunctionFieldGetEvalMode(qf_input_fields[i], &eval_mode));
1725349fb27dSJeremy L Thompson       switch (eval_mode) {
1726349fb27dSJeremy L Thompson         case CEED_EVAL_NONE:
1727349fb27dSJeremy L Thompson           CeedCallBackend(CeedVectorSetArray(impl->q_vecs_in[i], CEED_MEM_DEVICE, CEED_USE_POINTER, e_data[i]));
1728349fb27dSJeremy L Thompson           break;
1729349fb27dSJeremy L Thompson         case CEED_EVAL_INTERP:
1730349fb27dSJeremy L Thompson         case CEED_EVAL_GRAD:
1731349fb27dSJeremy L Thompson         case CEED_EVAL_DIV:
1732349fb27dSJeremy L Thompson         case CEED_EVAL_CURL:
1733349fb27dSJeremy L Thompson           CeedCallBackend(CeedOperatorFieldGetBasis(op_input_fields[i], &basis));
1734349fb27dSJeremy L Thompson           CeedCallBackend(CeedBasisApplyAtPoints(basis, num_elem, num_points, CEED_NOTRANSPOSE, eval_mode, impl->point_coords_elem, impl->e_vecs[i],
1735349fb27dSJeremy L Thompson                                                  impl->q_vecs_in[i]));
1736349fb27dSJeremy L Thompson           break;
1737349fb27dSJeremy L Thompson         case CEED_EVAL_WEIGHT:
1738349fb27dSJeremy L Thompson           break;  // No action
1739349fb27dSJeremy L Thompson       }
1740349fb27dSJeremy L Thompson 
1741349fb27dSJeremy L Thompson       // Q function
1742349fb27dSJeremy L Thompson       CeedCallBackend(CeedQFunctionApply(qf, num_elem * max_num_points, impl->q_vecs_in, impl->q_vecs_out));
1743349fb27dSJeremy L Thompson 
1744349fb27dSJeremy L Thompson       // Output basis apply if needed
1745382e9c83SJeremy L Thompson       for (CeedInt j = 0; j < num_output_fields; j++) {
1746349fb27dSJeremy L Thompson         bool                is_active_output = false;
1747382e9c83SJeremy L Thompson         CeedInt             elem_size        = 0;
1748382e9c83SJeremy L Thompson         CeedRestrictionType rstr_type;
1749349fb27dSJeremy L Thompson         CeedEvalMode        eval_mode;
1750349fb27dSJeremy L Thompson         CeedVector          vec;
1751349fb27dSJeremy L Thompson         CeedElemRestriction elem_rstr;
1752349fb27dSJeremy L Thompson         CeedBasis           basis;
1753349fb27dSJeremy L Thompson 
1754382e9c83SJeremy L Thompson         CeedCallBackend(CeedOperatorFieldGetVector(op_output_fields[j], &vec));
1755382e9c83SJeremy L Thompson         // ---- Skip non-active output
1756349fb27dSJeremy L Thompson         is_active_output = vec == CEED_VECTOR_ACTIVE;
1757349fb27dSJeremy L Thompson         if (!is_active_output) continue;
1758349fb27dSJeremy L Thompson 
1759382e9c83SJeremy L Thompson         // ---- Check if elem size matches
1760382e9c83SJeremy L Thompson         CeedCallBackend(CeedOperatorFieldGetElemRestriction(op_output_fields[j], &elem_rstr));
1761382e9c83SJeremy L Thompson         CeedCallBackend(CeedElemRestrictionGetType(elem_rstr, &rstr_type));
1762382e9c83SJeremy L Thompson         if (is_active_at_points && rstr_type != CEED_RESTRICTION_POINTS) continue;
1763382e9c83SJeremy L Thompson         if (rstr_type == CEED_RESTRICTION_POINTS) {
1764382e9c83SJeremy L Thompson           CeedCallBackend(CeedElemRestrictionGetMaxPointsInElement(elem_rstr, &elem_size));
1765382e9c83SJeremy L Thompson         } else {
1766382e9c83SJeremy L Thompson           CeedCallBackend(CeedElemRestrictionGetElementSize(elem_rstr, &elem_size));
1767382e9c83SJeremy L Thompson         }
1768382e9c83SJeremy L Thompson         {
1769382e9c83SJeremy L Thompson           CeedInt num_comp = 0;
1770382e9c83SJeremy L Thompson 
1771382e9c83SJeremy L Thompson           CeedCallBackend(CeedElemRestrictionGetNumComponents(elem_rstr, &num_comp));
1772382e9c83SJeremy L Thompson           if (e_vec_size != num_comp * elem_size) continue;
1773382e9c83SJeremy L Thompson         }
1774382e9c83SJeremy L Thompson 
1775349fb27dSJeremy L Thompson         // Basis action
1776382e9c83SJeremy L Thompson         CeedCallBackend(CeedQFunctionFieldGetEvalMode(qf_output_fields[j], &eval_mode));
1777349fb27dSJeremy L Thompson         switch (eval_mode) {
1778349fb27dSJeremy L Thompson           case CEED_EVAL_NONE:
1779382e9c83SJeremy L Thompson             CeedCallBackend(CeedVectorRestoreArray(impl->e_vecs[j + impl->num_inputs], &e_data[j + num_input_fields]));
1780349fb27dSJeremy L Thompson             break;
1781349fb27dSJeremy L Thompson           case CEED_EVAL_INTERP:
1782349fb27dSJeremy L Thompson           case CEED_EVAL_GRAD:
1783349fb27dSJeremy L Thompson           case CEED_EVAL_DIV:
1784349fb27dSJeremy L Thompson           case CEED_EVAL_CURL:
1785382e9c83SJeremy L Thompson             CeedCallBackend(CeedOperatorFieldGetBasis(op_output_fields[j], &basis));
1786382e9c83SJeremy L Thompson             CeedCallBackend(CeedBasisApplyAtPoints(basis, num_elem, num_points, CEED_TRANSPOSE, eval_mode, impl->point_coords_elem,
1787382e9c83SJeremy L Thompson                                                    impl->q_vecs_out[j], impl->e_vecs[j + impl->num_inputs]));
1788349fb27dSJeremy L Thompson             break;
1789349fb27dSJeremy L Thompson           // LCOV_EXCL_START
1790349fb27dSJeremy L Thompson           case CEED_EVAL_WEIGHT: {
1791349fb27dSJeremy L Thompson             return CeedError(CeedOperatorReturnCeed(op), CEED_ERROR_BACKEND, "CEED_EVAL_WEIGHT cannot be an output evaluation mode");
1792349fb27dSJeremy L Thompson             // LCOV_EXCL_STOP
1793349fb27dSJeremy L Thompson           }
1794349fb27dSJeremy L Thompson         }
1795349fb27dSJeremy L Thompson 
1796349fb27dSJeremy L Thompson         // Mask output e-vec
1797382e9c83SJeremy L Thompson         CeedCallBackend(CeedVectorPointwiseMult(impl->e_vecs[j + impl->num_inputs], impl->e_vecs[i], impl->e_vecs[j + impl->num_inputs]));
1798349fb27dSJeremy L Thompson 
1799349fb27dSJeremy L Thompson         // Restrict
1800382e9c83SJeremy L Thompson         CeedCallBackend(CeedOperatorFieldGetElemRestriction(op_output_fields[j], &elem_rstr));
1801382e9c83SJeremy L Thompson         CeedCallBackend(CeedElemRestrictionApply(elem_rstr, CEED_TRANSPOSE, impl->e_vecs[j + impl->num_inputs], assembled, request));
1802349fb27dSJeremy L Thompson 
1803349fb27dSJeremy L Thompson         // Reset q_vec for
1804349fb27dSJeremy L Thompson         if (eval_mode == CEED_EVAL_NONE) {
1805382e9c83SJeremy L Thompson           CeedCallBackend(CeedVectorGetArrayWrite(impl->e_vecs[j + impl->num_inputs], CEED_MEM_DEVICE, &e_data[j + num_input_fields]));
1806382e9c83SJeremy L Thompson           CeedCallBackend(CeedVectorSetArray(impl->q_vecs_out[j], CEED_MEM_DEVICE, CEED_USE_POINTER, e_data[j + num_input_fields]));
1807349fb27dSJeremy L Thompson         }
1808349fb27dSJeremy L Thompson       }
1809382e9c83SJeremy L Thompson 
1810382e9c83SJeremy L Thompson       // Reset vec
181113062808SJeremy L Thompson       if (s == e_vec_size - 1 && i != num_input_fields - 1) CeedCallBackend(CeedVectorSetValue(impl->q_vecs_in[i], 0.0));
181286e10729SJeremy L Thompson     }
1813349fb27dSJeremy L Thompson   }
1814349fb27dSJeremy L Thompson 
1815349fb27dSJeremy L Thompson   // Restore CEED_EVAL_NONE
1816349fb27dSJeremy L Thompson   for (CeedInt i = 0; i < num_output_fields; i++) {
1817349fb27dSJeremy L Thompson     CeedEvalMode eval_mode;
1818349fb27dSJeremy L Thompson 
1819349fb27dSJeremy L Thompson     // Get eval_mode
1820349fb27dSJeremy L Thompson     CeedCallBackend(CeedQFunctionFieldGetEvalMode(qf_output_fields[i], &eval_mode));
1821349fb27dSJeremy L Thompson 
1822349fb27dSJeremy L Thompson     // Restore evec
1823349fb27dSJeremy L Thompson     CeedCallBackend(CeedQFunctionFieldGetEvalMode(qf_output_fields[i], &eval_mode));
1824349fb27dSJeremy L Thompson     if (eval_mode == CEED_EVAL_NONE) {
1825349fb27dSJeremy L Thompson       CeedCallBackend(CeedVectorRestoreArray(impl->e_vecs[i + impl->num_inputs], &e_data[i + num_input_fields]));
1826349fb27dSJeremy L Thompson     }
1827349fb27dSJeremy L Thompson   }
1828349fb27dSJeremy L Thompson 
1829349fb27dSJeremy L Thompson   // Restore input arrays
1830349fb27dSJeremy L Thompson   CeedCallBackend(CeedOperatorRestoreInputs_Cuda(num_input_fields, qf_input_fields, op_input_fields, true, e_data, impl));
1831349fb27dSJeremy L Thompson   return CEED_ERROR_SUCCESS;
1832756ca9e9SJeremy L Thompson }
1833756ca9e9SJeremy L Thompson 
1834756ca9e9SJeremy L Thompson //------------------------------------------------------------------------------
18350d0321e0SJeremy L Thompson // Create operator
18360d0321e0SJeremy L Thompson //------------------------------------------------------------------------------
18370d0321e0SJeremy L Thompson int CeedOperatorCreate_Cuda(CeedOperator op) {
18380d0321e0SJeremy L Thompson   Ceed               ceed;
18390d0321e0SJeremy L Thompson   CeedOperator_Cuda *impl;
18400d0321e0SJeremy L Thompson 
1841ca735530SJeremy L Thompson   CeedCallBackend(CeedOperatorGetCeed(op, &ceed));
18422b730f8bSJeremy L Thompson   CeedCallBackend(CeedCalloc(1, &impl));
18432b730f8bSJeremy L Thompson   CeedCallBackend(CeedOperatorSetData(op, impl));
18440d0321e0SJeremy L Thompson 
18452b730f8bSJeremy L Thompson   CeedCallBackend(CeedSetBackendFunction(ceed, "Operator", op, "LinearAssembleQFunction", CeedOperatorLinearAssembleQFunction_Cuda));
18462b730f8bSJeremy L Thompson   CeedCallBackend(CeedSetBackendFunction(ceed, "Operator", op, "LinearAssembleQFunctionUpdate", CeedOperatorLinearAssembleQFunctionUpdate_Cuda));
18472b730f8bSJeremy L Thompson   CeedCallBackend(CeedSetBackendFunction(ceed, "Operator", op, "LinearAssembleAddDiagonal", CeedOperatorLinearAssembleAddDiagonal_Cuda));
18482b730f8bSJeremy L Thompson   CeedCallBackend(
18492b730f8bSJeremy L Thompson       CeedSetBackendFunction(ceed, "Operator", op, "LinearAssembleAddPointBlockDiagonal", CeedOperatorLinearAssembleAddPointBlockDiagonal_Cuda));
18502b730f8bSJeremy L Thompson   CeedCallBackend(CeedSetBackendFunction(ceed, "Operator", op, "LinearAssembleSingle", CeedSingleOperatorAssemble_Cuda));
18512b730f8bSJeremy L Thompson   CeedCallBackend(CeedSetBackendFunction(ceed, "Operator", op, "ApplyAdd", CeedOperatorApplyAdd_Cuda));
18522b730f8bSJeremy L Thompson   CeedCallBackend(CeedSetBackendFunction(ceed, "Operator", op, "Destroy", CeedOperatorDestroy_Cuda));
18530d0321e0SJeremy L Thompson   return CEED_ERROR_SUCCESS;
18540d0321e0SJeremy L Thompson }
18550d0321e0SJeremy L Thompson 
18560d0321e0SJeremy L Thompson //------------------------------------------------------------------------------
185767d9480aSJeremy L Thompson // Create operator AtPoints
1858756ca9e9SJeremy L Thompson //------------------------------------------------------------------------------
1859756ca9e9SJeremy L Thompson int CeedOperatorCreateAtPoints_Cuda(CeedOperator op) {
1860756ca9e9SJeremy L Thompson   Ceed               ceed;
1861756ca9e9SJeremy L Thompson   CeedOperator_Cuda *impl;
1862756ca9e9SJeremy L Thompson 
1863756ca9e9SJeremy L Thompson   CeedCallBackend(CeedOperatorGetCeed(op, &ceed));
1864756ca9e9SJeremy L Thompson   CeedCallBackend(CeedCalloc(1, &impl));
1865756ca9e9SJeremy L Thompson   CeedCallBackend(CeedOperatorSetData(op, impl));
1866756ca9e9SJeremy L Thompson 
1867756ca9e9SJeremy L Thompson   CeedCallBackend(CeedSetBackendFunction(ceed, "Operator", op, "LinearAssembleQFunction", CeedOperatorLinearAssembleQFunctionAtPoints_Cuda));
1868756ca9e9SJeremy L Thompson   CeedCallBackend(CeedSetBackendFunction(ceed, "Operator", op, "LinearAssembleAddDiagonal", CeedOperatorLinearAssembleAddDiagonalAtPoints_Cuda));
1869756ca9e9SJeremy L Thompson   CeedCallBackend(CeedSetBackendFunction(ceed, "Operator", op, "ApplyAdd", CeedOperatorApplyAddAtPoints_Cuda));
1870756ca9e9SJeremy L Thompson   CeedCallBackend(CeedSetBackendFunction(ceed, "Operator", op, "Destroy", CeedOperatorDestroy_Cuda));
1871756ca9e9SJeremy L Thompson   return CEED_ERROR_SUCCESS;
1872756ca9e9SJeremy L Thompson }
1873756ca9e9SJeremy L Thompson 
1874756ca9e9SJeremy L Thompson //------------------------------------------------------------------------------
1875