xref: /libCEED/backends/cuda-ref/ceed-cuda-ref-operator.c (revision 86e107299058a5a08bd170fc9397de9f32c01778)
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
30ca735530SJeremy L Thompson   for (CeedInt i = 0; i < impl->num_inputs + impl->num_outputs; i++) {
31ca735530SJeremy L Thompson     CeedCallBackend(CeedVectorDestroy(&impl->e_vecs[i]));
320d0321e0SJeremy L Thompson   }
33ca735530SJeremy L Thompson   CeedCallBackend(CeedFree(&impl->e_vecs));
34c1222711SJeremy L Thompson   CeedCallBackend(CeedFree(&impl->input_states));
350d0321e0SJeremy L Thompson 
36ca735530SJeremy L Thompson   for (CeedInt i = 0; i < impl->num_inputs; i++) {
37ca735530SJeremy L Thompson     CeedCallBackend(CeedVectorDestroy(&impl->q_vecs_in[i]));
380d0321e0SJeremy L Thompson   }
39ca735530SJeremy L Thompson   CeedCallBackend(CeedFree(&impl->q_vecs_in));
400d0321e0SJeremy L Thompson 
41ca735530SJeremy L Thompson   for (CeedInt i = 0; i < impl->num_outputs; i++) {
42ca735530SJeremy L Thompson     CeedCallBackend(CeedVectorDestroy(&impl->q_vecs_out[i]));
430d0321e0SJeremy L Thompson   }
44ca735530SJeremy L Thompson   CeedCallBackend(CeedFree(&impl->q_vecs_out));
45756ca9e9SJeremy L Thompson   CeedCallBackend(CeedVectorDestroy(&impl->point_coords_elem));
460d0321e0SJeremy L Thompson 
470d0321e0SJeremy L Thompson   // QFunction assembly data
48ca735530SJeremy L Thompson   for (CeedInt i = 0; i < impl->num_active_in; i++) {
49ca735530SJeremy L Thompson     CeedCallBackend(CeedVectorDestroy(&impl->qf_active_in[i]));
500d0321e0SJeremy L Thompson   }
51ca735530SJeremy L Thompson   CeedCallBackend(CeedFree(&impl->qf_active_in));
520d0321e0SJeremy L Thompson 
530d0321e0SJeremy L Thompson   // Diag data
540d0321e0SJeremy L Thompson   if (impl->diag) {
550d0321e0SJeremy L Thompson     Ceed ceed;
56ca735530SJeremy L Thompson 
572b730f8bSJeremy L Thompson     CeedCallBackend(CeedOperatorGetCeed(op, &ceed));
58cbfe683aSSebastian Grimberg     if (impl->diag->module) {
592b730f8bSJeremy L Thompson       CeedCallCuda(ceed, cuModuleUnload(impl->diag->module));
60cbfe683aSSebastian Grimberg     }
61cbfe683aSSebastian Grimberg     if (impl->diag->module_point_block) {
62cbfe683aSSebastian Grimberg       CeedCallCuda(ceed, cuModuleUnload(impl->diag->module_point_block));
63cbfe683aSSebastian Grimberg     }
64004e4986SSebastian Grimberg     CeedCallCuda(ceed, cudaFree(impl->diag->d_eval_modes_in));
65004e4986SSebastian Grimberg     CeedCallCuda(ceed, cudaFree(impl->diag->d_eval_modes_out));
662b730f8bSJeremy L Thompson     CeedCallCuda(ceed, cudaFree(impl->diag->d_identity));
67ca735530SJeremy L Thompson     CeedCallCuda(ceed, cudaFree(impl->diag->d_interp_in));
68ca735530SJeremy L Thompson     CeedCallCuda(ceed, cudaFree(impl->diag->d_interp_out));
69ca735530SJeremy L Thompson     CeedCallCuda(ceed, cudaFree(impl->diag->d_grad_in));
70ca735530SJeremy L Thompson     CeedCallCuda(ceed, cudaFree(impl->diag->d_grad_out));
71004e4986SSebastian Grimberg     CeedCallCuda(ceed, cudaFree(impl->diag->d_div_in));
72004e4986SSebastian Grimberg     CeedCallCuda(ceed, cudaFree(impl->diag->d_div_out));
73004e4986SSebastian Grimberg     CeedCallCuda(ceed, cudaFree(impl->diag->d_curl_in));
74004e4986SSebastian Grimberg     CeedCallCuda(ceed, cudaFree(impl->diag->d_curl_out));
75004e4986SSebastian Grimberg     CeedCallBackend(CeedElemRestrictionDestroy(&impl->diag->diag_rstr));
76506b1a0cSSebastian Grimberg     CeedCallBackend(CeedElemRestrictionDestroy(&impl->diag->point_block_diag_rstr));
77ca735530SJeremy L Thompson     CeedCallBackend(CeedVectorDestroy(&impl->diag->elem_diag));
78ca735530SJeremy L Thompson     CeedCallBackend(CeedVectorDestroy(&impl->diag->point_block_elem_diag));
790d0321e0SJeremy L Thompson   }
802b730f8bSJeremy L Thompson   CeedCallBackend(CeedFree(&impl->diag));
810d0321e0SJeremy L Thompson 
82cc132f9aSnbeams   if (impl->asmb) {
83cc132f9aSnbeams     Ceed ceed;
84ca735530SJeremy L Thompson 
852b730f8bSJeremy L Thompson     CeedCallBackend(CeedOperatorGetCeed(op, &ceed));
862b730f8bSJeremy L Thompson     CeedCallCuda(ceed, cuModuleUnload(impl->asmb->module));
872b730f8bSJeremy L Thompson     CeedCallCuda(ceed, cudaFree(impl->asmb->d_B_in));
882b730f8bSJeremy L Thompson     CeedCallCuda(ceed, cudaFree(impl->asmb->d_B_out));
89cc132f9aSnbeams   }
902b730f8bSJeremy L Thompson   CeedCallBackend(CeedFree(&impl->asmb));
91cc132f9aSnbeams 
922b730f8bSJeremy L Thompson   CeedCallBackend(CeedFree(&impl));
930d0321e0SJeremy L Thompson   return CEED_ERROR_SUCCESS;
940d0321e0SJeremy L Thompson }
950d0321e0SJeremy L Thompson 
960d0321e0SJeremy L Thompson //------------------------------------------------------------------------------
970d0321e0SJeremy L Thompson // Setup infields or outfields
980d0321e0SJeremy L Thompson //------------------------------------------------------------------------------
99756ca9e9SJeremy L Thompson static int CeedOperatorSetupFields_Cuda(CeedQFunction qf, CeedOperator op, bool is_input, bool is_at_points, CeedVector *e_vecs, CeedVector *q_vecs,
100756ca9e9SJeremy L Thompson                                         CeedInt start_e, CeedInt num_fields, CeedInt Q, CeedInt num_elem) {
1010d0321e0SJeremy L Thompson   Ceed                ceed;
102ca735530SJeremy L Thompson   CeedQFunctionField *qf_fields;
103ca735530SJeremy L Thompson   CeedOperatorField  *op_fields;
1040d0321e0SJeremy L Thompson 
105ca735530SJeremy L Thompson   CeedCallBackend(CeedOperatorGetCeed(op, &ceed));
106ca735530SJeremy L Thompson   if (is_input) {
107ca735530SJeremy L Thompson     CeedCallBackend(CeedOperatorGetFields(op, NULL, &op_fields, NULL, NULL));
108ca735530SJeremy L Thompson     CeedCallBackend(CeedQFunctionGetFields(qf, NULL, &qf_fields, NULL, NULL));
1090d0321e0SJeremy L Thompson   } else {
110ca735530SJeremy L Thompson     CeedCallBackend(CeedOperatorGetFields(op, NULL, NULL, NULL, &op_fields));
111ca735530SJeremy L Thompson     CeedCallBackend(CeedQFunctionGetFields(qf, NULL, NULL, NULL, &qf_fields));
1120d0321e0SJeremy L Thompson   }
1130d0321e0SJeremy L Thompson 
1140d0321e0SJeremy L Thompson   // Loop over fields
115ca735530SJeremy L Thompson   for (CeedInt i = 0; i < num_fields; i++) {
116004e4986SSebastian Grimberg     bool         is_strided = false, skip_restriction = false;
117004e4986SSebastian Grimberg     CeedSize     q_size;
118004e4986SSebastian Grimberg     CeedInt      size;
119004e4986SSebastian Grimberg     CeedEvalMode eval_mode;
120ca735530SJeremy L Thompson     CeedBasis    basis;
1210d0321e0SJeremy L Thompson 
122004e4986SSebastian Grimberg     CeedCallBackend(CeedQFunctionFieldGetEvalMode(qf_fields[i], &eval_mode));
123004e4986SSebastian Grimberg     if (eval_mode != CEED_EVAL_WEIGHT) {
124edb2538eSJeremy L Thompson       CeedElemRestriction elem_rstr;
125ca735530SJeremy L Thompson 
1260d0321e0SJeremy L Thompson       // Check whether this field can skip the element restriction:
127004e4986SSebastian Grimberg       // Must be passive input, with eval_mode NONE, and have a strided restriction with CEED_STRIDES_BACKEND.
128004e4986SSebastian Grimberg       CeedCallBackend(CeedOperatorFieldGetElemRestriction(op_fields[i], &elem_rstr));
1290d0321e0SJeremy L Thompson 
1300d0321e0SJeremy L Thompson       // First, check whether the field is input or output:
131ca735530SJeremy L Thompson       if (is_input) {
132ca735530SJeremy L Thompson         CeedVector vec;
133ca735530SJeremy L Thompson 
134004e4986SSebastian Grimberg         // Check for passive input
135ca735530SJeremy L Thompson         CeedCallBackend(CeedOperatorFieldGetVector(op_fields[i], &vec));
136ca735530SJeremy L Thompson         if (vec != CEED_VECTOR_ACTIVE) {
137004e4986SSebastian Grimberg           // Check eval_mode
138004e4986SSebastian Grimberg           if (eval_mode == CEED_EVAL_NONE) {
1390d0321e0SJeremy L Thompson             // Check for strided restriction
140edb2538eSJeremy L Thompson             CeedCallBackend(CeedElemRestrictionIsStrided(elem_rstr, &is_strided));
141ca735530SJeremy L Thompson             if (is_strided) {
1420d0321e0SJeremy L Thompson               // Check if vector is already in preferred backend ordering
143edb2538eSJeremy L Thompson               CeedCallBackend(CeedElemRestrictionHasBackendStrides(elem_rstr, &skip_restriction));
1440d0321e0SJeremy L Thompson             }
1450d0321e0SJeremy L Thompson           }
1460d0321e0SJeremy L Thompson         }
1470d0321e0SJeremy L Thompson       }
148ca735530SJeremy L Thompson       if (skip_restriction) {
149ea61e9acSJeremy L Thompson         // We do not need an E-Vector, but will use the input field vector's data directly in the operator application.
150004e4986SSebastian Grimberg         e_vecs[i + start_e] = NULL;
1510d0321e0SJeremy L Thompson       } else {
152004e4986SSebastian Grimberg         CeedCallBackend(CeedElemRestrictionCreateVector(elem_rstr, NULL, &e_vecs[i + start_e]));
1530d0321e0SJeremy L Thompson       }
1540d0321e0SJeremy L Thompson     }
1550d0321e0SJeremy L Thompson 
156004e4986SSebastian Grimberg     switch (eval_mode) {
1570d0321e0SJeremy L Thompson       case CEED_EVAL_NONE:
158ca735530SJeremy L Thompson         CeedCallBackend(CeedQFunctionFieldGetSize(qf_fields[i], &size));
159ca735530SJeremy L Thompson         q_size = (CeedSize)num_elem * Q * size;
160ca735530SJeremy L Thompson         CeedCallBackend(CeedVectorCreate(ceed, q_size, &q_vecs[i]));
1610d0321e0SJeremy L Thompson         break;
1620d0321e0SJeremy L Thompson       case CEED_EVAL_INTERP:
1630d0321e0SJeremy L Thompson       case CEED_EVAL_GRAD:
164004e4986SSebastian Grimberg       case CEED_EVAL_DIV:
165004e4986SSebastian Grimberg       case CEED_EVAL_CURL:
166ca735530SJeremy L Thompson         CeedCallBackend(CeedQFunctionFieldGetSize(qf_fields[i], &size));
167ca735530SJeremy L Thompson         q_size = (CeedSize)num_elem * Q * size;
168ca735530SJeremy L Thompson         CeedCallBackend(CeedVectorCreate(ceed, q_size, &q_vecs[i]));
1690d0321e0SJeremy L Thompson         break;
1700d0321e0SJeremy L Thompson       case CEED_EVAL_WEIGHT:  // Only on input fields
171ca735530SJeremy L Thompson         CeedCallBackend(CeedOperatorFieldGetBasis(op_fields[i], &basis));
172ca735530SJeremy L Thompson         q_size = (CeedSize)num_elem * Q;
173ca735530SJeremy L Thompson         CeedCallBackend(CeedVectorCreate(ceed, q_size, &q_vecs[i]));
174756ca9e9SJeremy L Thompson         if (is_at_points) {
175756ca9e9SJeremy L Thompson           CeedInt num_points[num_elem];
176756ca9e9SJeremy L Thompson 
177756ca9e9SJeremy L Thompson           for (CeedInt i = 0; i < num_elem; i++) num_points[i] = Q;
178756ca9e9SJeremy L Thompson           CeedCallBackend(
179756ca9e9SJeremy L Thompson               CeedBasisApplyAtPoints(basis, num_elem, num_points, CEED_NOTRANSPOSE, CEED_EVAL_WEIGHT, CEED_VECTOR_NONE, CEED_VECTOR_NONE, q_vecs[i]));
180756ca9e9SJeremy L Thompson         } else {
181ca735530SJeremy L Thompson           CeedCallBackend(CeedBasisApply(basis, num_elem, CEED_NOTRANSPOSE, CEED_EVAL_WEIGHT, CEED_VECTOR_NONE, q_vecs[i]));
182756ca9e9SJeremy L Thompson         }
1830d0321e0SJeremy L Thompson         break;
1840d0321e0SJeremy L Thompson     }
1850d0321e0SJeremy L Thompson   }
1860d0321e0SJeremy L Thompson   return CEED_ERROR_SUCCESS;
1870d0321e0SJeremy L Thompson }
1880d0321e0SJeremy L Thompson 
1890d0321e0SJeremy L Thompson //------------------------------------------------------------------------------
190ea61e9acSJeremy L Thompson // CeedOperator needs to connect all the named fields (be they active or passive) to the named inputs and outputs of its CeedQFunction.
1910d0321e0SJeremy L Thompson //------------------------------------------------------------------------------
1920d0321e0SJeremy L Thompson static int CeedOperatorSetup_Cuda(CeedOperator op) {
1930d0321e0SJeremy L Thompson   Ceed                ceed;
194ca735530SJeremy L Thompson   bool                is_setup_done;
195ca735530SJeremy L Thompson   CeedInt             Q, num_elem, num_input_fields, num_output_fields;
196ca735530SJeremy L Thompson   CeedQFunctionField *qf_input_fields, *qf_output_fields;
1970d0321e0SJeremy L Thompson   CeedQFunction       qf;
198ca735530SJeremy L Thompson   CeedOperatorField  *op_input_fields, *op_output_fields;
199ca735530SJeremy L Thompson   CeedOperator_Cuda  *impl;
200ca735530SJeremy L Thompson 
201ca735530SJeremy L Thompson   CeedCallBackend(CeedOperatorIsSetupDone(op, &is_setup_done));
202ca735530SJeremy L Thompson   if (is_setup_done) return CEED_ERROR_SUCCESS;
203ca735530SJeremy L Thompson 
204ca735530SJeremy L Thompson   CeedCallBackend(CeedOperatorGetCeed(op, &ceed));
205ca735530SJeremy L Thompson   CeedCallBackend(CeedOperatorGetData(op, &impl));
2062b730f8bSJeremy L Thompson   CeedCallBackend(CeedOperatorGetQFunction(op, &qf));
2072b730f8bSJeremy L Thompson   CeedCallBackend(CeedOperatorGetNumQuadraturePoints(op, &Q));
208ca735530SJeremy L Thompson   CeedCallBackend(CeedOperatorGetNumElements(op, &num_elem));
209ca735530SJeremy L Thompson   CeedCallBackend(CeedOperatorGetFields(op, &num_input_fields, &op_input_fields, &num_output_fields, &op_output_fields));
210ca735530SJeremy L Thompson   CeedCallBackend(CeedQFunctionGetFields(qf, NULL, &qf_input_fields, NULL, &qf_output_fields));
2110d0321e0SJeremy L Thompson 
2120d0321e0SJeremy L Thompson   // Allocate
213ca735530SJeremy L Thompson   CeedCallBackend(CeedCalloc(num_input_fields + num_output_fields, &impl->e_vecs));
214c1222711SJeremy L Thompson   CeedCallBackend(CeedCalloc(CEED_FIELD_MAX, &impl->input_states));
215ca735530SJeremy L Thompson   CeedCallBackend(CeedCalloc(CEED_FIELD_MAX, &impl->q_vecs_in));
216ca735530SJeremy L Thompson   CeedCallBackend(CeedCalloc(CEED_FIELD_MAX, &impl->q_vecs_out));
217ca735530SJeremy L Thompson   impl->num_inputs  = num_input_fields;
218ca735530SJeremy L Thompson   impl->num_outputs = num_output_fields;
2190d0321e0SJeremy L Thompson 
220ca735530SJeremy L Thompson   // Set up infield and outfield e_vecs and q_vecs
2210d0321e0SJeremy L Thompson   // Infields
222756ca9e9SJeremy L Thompson   CeedCallBackend(CeedOperatorSetupFields_Cuda(qf, op, true, false, impl->e_vecs, impl->q_vecs_in, 0, num_input_fields, Q, num_elem));
2230d0321e0SJeremy L Thompson   // Outfields
224756ca9e9SJeremy L Thompson   CeedCallBackend(
225756ca9e9SJeremy L Thompson       CeedOperatorSetupFields_Cuda(qf, op, false, false, impl->e_vecs, impl->q_vecs_out, num_input_fields, num_output_fields, Q, num_elem));
2260d0321e0SJeremy L Thompson 
2272b730f8bSJeremy L Thompson   CeedCallBackend(CeedOperatorSetSetupDone(op));
2280d0321e0SJeremy L Thompson   return CEED_ERROR_SUCCESS;
2290d0321e0SJeremy L Thompson }
2300d0321e0SJeremy L Thompson 
2310d0321e0SJeremy L Thompson //------------------------------------------------------------------------------
2320d0321e0SJeremy L Thompson // Setup Operator Inputs
2330d0321e0SJeremy L Thompson //------------------------------------------------------------------------------
234ca735530SJeremy L Thompson static inline int CeedOperatorSetupInputs_Cuda(CeedInt num_input_fields, CeedQFunctionField *qf_input_fields, CeedOperatorField *op_input_fields,
235004e4986SSebastian Grimberg                                                CeedVector in_vec, const bool skip_active, CeedScalar *e_data[2 * CEED_FIELD_MAX],
2360d0321e0SJeremy L Thompson                                                CeedOperator_Cuda *impl, CeedRequest *request) {
237ca735530SJeremy L Thompson   for (CeedInt i = 0; i < num_input_fields; i++) {
238004e4986SSebastian Grimberg     CeedEvalMode        eval_mode;
2390d0321e0SJeremy L Thompson     CeedVector          vec;
240edb2538eSJeremy L Thompson     CeedElemRestriction elem_rstr;
2410d0321e0SJeremy L Thompson 
2420d0321e0SJeremy L Thompson     // Get input vector
243ca735530SJeremy L Thompson     CeedCallBackend(CeedOperatorFieldGetVector(op_input_fields[i], &vec));
2440d0321e0SJeremy L Thompson     if (vec == CEED_VECTOR_ACTIVE) {
245004e4986SSebastian Grimberg       if (skip_active) continue;
246ca735530SJeremy L Thompson       else vec = in_vec;
2470d0321e0SJeremy L Thompson     }
2480d0321e0SJeremy L Thompson 
249004e4986SSebastian Grimberg     CeedCallBackend(CeedQFunctionFieldGetEvalMode(qf_input_fields[i], &eval_mode));
250004e4986SSebastian Grimberg     if (eval_mode == CEED_EVAL_WEIGHT) {  // Skip
2510d0321e0SJeremy L Thompson     } else {
252004e4986SSebastian Grimberg       // Get input vector
253004e4986SSebastian Grimberg       CeedCallBackend(CeedOperatorFieldGetVector(op_input_fields[i], &vec));
2540d0321e0SJeremy L Thompson       // Get input element restriction
255edb2538eSJeremy L Thompson       CeedCallBackend(CeedOperatorFieldGetElemRestriction(op_input_fields[i], &elem_rstr));
256ca735530SJeremy L Thompson       if (vec == CEED_VECTOR_ACTIVE) vec = in_vec;
2570d0321e0SJeremy L Thompson       // Restrict, if necessary
258ca735530SJeremy L Thompson       if (!impl->e_vecs[i]) {
2590d0321e0SJeremy L Thompson         // No restriction for this field; read data directly from vec.
260ca735530SJeremy L Thompson         CeedCallBackend(CeedVectorGetArrayRead(vec, CEED_MEM_DEVICE, (const CeedScalar **)&e_data[i]));
2610d0321e0SJeremy L Thompson       } else {
262c1222711SJeremy L Thompson         uint64_t state;
263c1222711SJeremy L Thompson 
264c1222711SJeremy L Thompson         CeedCallBackend(CeedVectorGetState(vec, &state));
265c1222711SJeremy L Thompson         if (state != impl->input_states[i]) {
266edb2538eSJeremy L Thompson           CeedCallBackend(CeedElemRestrictionApply(elem_rstr, CEED_NOTRANSPOSE, vec, impl->e_vecs[i], request));
267c1222711SJeremy L Thompson           impl->input_states[i] = state;
268c1222711SJeremy L Thompson         }
2690d0321e0SJeremy L Thompson         // Get evec
270ca735530SJeremy L Thompson         CeedCallBackend(CeedVectorGetArrayRead(impl->e_vecs[i], CEED_MEM_DEVICE, (const CeedScalar **)&e_data[i]));
2710d0321e0SJeremy L Thompson       }
2720d0321e0SJeremy L Thompson     }
2730d0321e0SJeremy L Thompson   }
2740d0321e0SJeremy L Thompson   return CEED_ERROR_SUCCESS;
2750d0321e0SJeremy L Thompson }
2760d0321e0SJeremy L Thompson 
2770d0321e0SJeremy L Thompson //------------------------------------------------------------------------------
2780d0321e0SJeremy L Thompson // Input Basis Action
2790d0321e0SJeremy L Thompson //------------------------------------------------------------------------------
280ca735530SJeremy L Thompson static inline int CeedOperatorInputBasis_Cuda(CeedInt num_elem, CeedQFunctionField *qf_input_fields, CeedOperatorField *op_input_fields,
281004e4986SSebastian Grimberg                                               CeedInt num_input_fields, const bool skip_active, CeedScalar *e_data[2 * CEED_FIELD_MAX],
2822b730f8bSJeremy L Thompson                                               CeedOperator_Cuda *impl) {
283ca735530SJeremy L Thompson   for (CeedInt i = 0; i < num_input_fields; i++) {
284ca735530SJeremy L Thompson     CeedInt             elem_size, size;
285004e4986SSebastian Grimberg     CeedEvalMode        eval_mode;
286edb2538eSJeremy L Thompson     CeedElemRestriction elem_rstr;
2870d0321e0SJeremy L Thompson     CeedBasis           basis;
2880d0321e0SJeremy L Thompson 
2890d0321e0SJeremy L Thompson     // Skip active input
290004e4986SSebastian Grimberg     if (skip_active) {
2910d0321e0SJeremy L Thompson       CeedVector vec;
292ca735530SJeremy L Thompson 
293ca735530SJeremy L Thompson       CeedCallBackend(CeedOperatorFieldGetVector(op_input_fields[i], &vec));
2942b730f8bSJeremy L Thompson       if (vec == CEED_VECTOR_ACTIVE) continue;
2950d0321e0SJeremy L Thompson     }
296004e4986SSebastian Grimberg     // Get elem_size, eval_mode, size
297edb2538eSJeremy L Thompson     CeedCallBackend(CeedOperatorFieldGetElemRestriction(op_input_fields[i], &elem_rstr));
298edb2538eSJeremy L Thompson     CeedCallBackend(CeedElemRestrictionGetElementSize(elem_rstr, &elem_size));
299004e4986SSebastian Grimberg     CeedCallBackend(CeedQFunctionFieldGetEvalMode(qf_input_fields[i], &eval_mode));
300ca735530SJeremy L Thompson     CeedCallBackend(CeedQFunctionFieldGetSize(qf_input_fields[i], &size));
3010d0321e0SJeremy L Thompson     // Basis action
302004e4986SSebastian Grimberg     switch (eval_mode) {
3030d0321e0SJeremy L Thompson       case CEED_EVAL_NONE:
304ca735530SJeremy L Thompson         CeedCallBackend(CeedVectorSetArray(impl->q_vecs_in[i], CEED_MEM_DEVICE, CEED_USE_POINTER, e_data[i]));
3050d0321e0SJeremy L Thompson         break;
3060d0321e0SJeremy L Thompson       case CEED_EVAL_INTERP:
3070d0321e0SJeremy L Thompson       case CEED_EVAL_GRAD:
308004e4986SSebastian Grimberg       case CEED_EVAL_DIV:
309004e4986SSebastian Grimberg       case CEED_EVAL_CURL:
310ca735530SJeremy L Thompson         CeedCallBackend(CeedOperatorFieldGetBasis(op_input_fields[i], &basis));
311004e4986SSebastian Grimberg         CeedCallBackend(CeedBasisApply(basis, num_elem, CEED_NOTRANSPOSE, eval_mode, impl->e_vecs[i], impl->q_vecs_in[i]));
3120d0321e0SJeremy L Thompson         break;
3130d0321e0SJeremy L Thompson       case CEED_EVAL_WEIGHT:
3140d0321e0SJeremy L Thompson         break;  // No action
3150d0321e0SJeremy L Thompson     }
3160d0321e0SJeremy L Thompson   }
3170d0321e0SJeremy L Thompson   return CEED_ERROR_SUCCESS;
3180d0321e0SJeremy L Thompson }
3190d0321e0SJeremy L Thompson 
3200d0321e0SJeremy L Thompson //------------------------------------------------------------------------------
3210d0321e0SJeremy L Thompson // Restore Input Vectors
3220d0321e0SJeremy L Thompson //------------------------------------------------------------------------------
323ca735530SJeremy L Thompson static inline int CeedOperatorRestoreInputs_Cuda(CeedInt num_input_fields, CeedQFunctionField *qf_input_fields, CeedOperatorField *op_input_fields,
324004e4986SSebastian Grimberg                                                  const bool skip_active, CeedScalar *e_data[2 * CEED_FIELD_MAX], CeedOperator_Cuda *impl) {
325ca735530SJeremy L Thompson   for (CeedInt i = 0; i < num_input_fields; i++) {
326004e4986SSebastian Grimberg     CeedEvalMode eval_mode;
3270d0321e0SJeremy L Thompson     CeedVector   vec;
3280d0321e0SJeremy L Thompson 
3290d0321e0SJeremy L Thompson     // Skip active input
330004e4986SSebastian Grimberg     if (skip_active) {
331ca735530SJeremy L Thompson       CeedCallBackend(CeedOperatorFieldGetVector(op_input_fields[i], &vec));
3322b730f8bSJeremy L Thompson       if (vec == CEED_VECTOR_ACTIVE) continue;
3330d0321e0SJeremy L Thompson     }
334004e4986SSebastian Grimberg     CeedCallBackend(CeedQFunctionFieldGetEvalMode(qf_input_fields[i], &eval_mode));
335004e4986SSebastian Grimberg     if (eval_mode == CEED_EVAL_WEIGHT) {  // Skip
3360d0321e0SJeremy L Thompson     } else {
337ca735530SJeremy L Thompson       if (!impl->e_vecs[i]) {  // This was a skip_restriction case
338ca735530SJeremy L Thompson         CeedCallBackend(CeedOperatorFieldGetVector(op_input_fields[i], &vec));
339ca735530SJeremy L Thompson         CeedCallBackend(CeedVectorRestoreArrayRead(vec, (const CeedScalar **)&e_data[i]));
3400d0321e0SJeremy L Thompson       } else {
341ca735530SJeremy L Thompson         CeedCallBackend(CeedVectorRestoreArrayRead(impl->e_vecs[i], (const CeedScalar **)&e_data[i]));
3420d0321e0SJeremy L Thompson       }
3430d0321e0SJeremy L Thompson     }
3440d0321e0SJeremy L Thompson   }
3450d0321e0SJeremy L Thompson   return CEED_ERROR_SUCCESS;
3460d0321e0SJeremy L Thompson }
3470d0321e0SJeremy L Thompson 
3480d0321e0SJeremy L Thompson //------------------------------------------------------------------------------
3490d0321e0SJeremy L Thompson // Apply and add to output
3500d0321e0SJeremy L Thompson //------------------------------------------------------------------------------
351ca735530SJeremy L Thompson static int CeedOperatorApplyAdd_Cuda(CeedOperator op, CeedVector in_vec, CeedVector out_vec, CeedRequest *request) {
352ca735530SJeremy L Thompson   CeedInt             Q, num_elem, elem_size, num_input_fields, num_output_fields, size;
353ca735530SJeremy L Thompson   CeedScalar         *e_data[2 * CEED_FIELD_MAX] = {NULL};
354ca735530SJeremy L Thompson   CeedQFunctionField *qf_input_fields, *qf_output_fields;
3550d0321e0SJeremy L Thompson   CeedQFunction       qf;
356004e4986SSebastian Grimberg   CeedOperatorField  *op_input_fields, *op_output_fields;
357004e4986SSebastian Grimberg   CeedOperator_Cuda  *impl;
358ca735530SJeremy L Thompson 
359ca735530SJeremy L Thompson   CeedCallBackend(CeedOperatorGetData(op, &impl));
3602b730f8bSJeremy L Thompson   CeedCallBackend(CeedOperatorGetQFunction(op, &qf));
3612b730f8bSJeremy L Thompson   CeedCallBackend(CeedOperatorGetNumQuadraturePoints(op, &Q));
362ca735530SJeremy L Thompson   CeedCallBackend(CeedOperatorGetNumElements(op, &num_elem));
363ca735530SJeremy L Thompson   CeedCallBackend(CeedOperatorGetFields(op, &num_input_fields, &op_input_fields, &num_output_fields, &op_output_fields));
364ca735530SJeremy L Thompson   CeedCallBackend(CeedQFunctionGetFields(qf, NULL, &qf_input_fields, NULL, &qf_output_fields));
3650d0321e0SJeremy L Thompson 
3660d0321e0SJeremy L Thompson   // Setup
3672b730f8bSJeremy L Thompson   CeedCallBackend(CeedOperatorSetup_Cuda(op));
3680d0321e0SJeremy L Thompson 
369004e4986SSebastian Grimberg   // Input Evecs and Restriction
370ca735530SJeremy L Thompson   CeedCallBackend(CeedOperatorSetupInputs_Cuda(num_input_fields, qf_input_fields, op_input_fields, in_vec, false, e_data, impl, request));
3710d0321e0SJeremy L Thompson 
3720d0321e0SJeremy L Thompson   // Input basis apply if needed
373ca735530SJeremy L Thompson   CeedCallBackend(CeedOperatorInputBasis_Cuda(num_elem, qf_input_fields, op_input_fields, num_input_fields, false, e_data, impl));
3740d0321e0SJeremy L Thompson 
3750d0321e0SJeremy L Thompson   // Output pointers, as necessary
376ca735530SJeremy L Thompson   for (CeedInt i = 0; i < num_output_fields; i++) {
377004e4986SSebastian Grimberg     CeedEvalMode eval_mode;
378004e4986SSebastian Grimberg 
379004e4986SSebastian Grimberg     CeedCallBackend(CeedQFunctionFieldGetEvalMode(qf_output_fields[i], &eval_mode));
380004e4986SSebastian Grimberg     if (eval_mode == CEED_EVAL_NONE) {
3810d0321e0SJeremy L Thompson       // Set the output Q-Vector to use the E-Vector data directly.
382ca735530SJeremy L Thompson       CeedCallBackend(CeedVectorGetArrayWrite(impl->e_vecs[i + impl->num_inputs], CEED_MEM_DEVICE, &e_data[i + num_input_fields]));
383ca735530SJeremy L Thompson       CeedCallBackend(CeedVectorSetArray(impl->q_vecs_out[i], CEED_MEM_DEVICE, CEED_USE_POINTER, e_data[i + num_input_fields]));
3840d0321e0SJeremy L Thompson     }
3850d0321e0SJeremy L Thompson   }
3860d0321e0SJeremy L Thompson 
3870d0321e0SJeremy L Thompson   // Q function
388ca735530SJeremy L Thompson   CeedCallBackend(CeedQFunctionApply(qf, num_elem * Q, impl->q_vecs_in, impl->q_vecs_out));
3890d0321e0SJeremy L Thompson 
3900d0321e0SJeremy L Thompson   // Output basis apply if needed
391ca735530SJeremy L Thompson   for (CeedInt i = 0; i < num_output_fields; i++) {
392004e4986SSebastian Grimberg     CeedEvalMode        eval_mode;
393edb2538eSJeremy L Thompson     CeedElemRestriction elem_rstr;
394ca735530SJeremy L Thompson     CeedBasis           basis;
395ca735530SJeremy L Thompson 
396004e4986SSebastian Grimberg     // Get elem_size, eval_mode, size
397edb2538eSJeremy L Thompson     CeedCallBackend(CeedOperatorFieldGetElemRestriction(op_output_fields[i], &elem_rstr));
398edb2538eSJeremy L Thompson     CeedCallBackend(CeedElemRestrictionGetElementSize(elem_rstr, &elem_size));
399004e4986SSebastian Grimberg     CeedCallBackend(CeedQFunctionFieldGetEvalMode(qf_output_fields[i], &eval_mode));
400ca735530SJeremy L Thompson     CeedCallBackend(CeedQFunctionFieldGetSize(qf_output_fields[i], &size));
4010d0321e0SJeremy L Thompson     // Basis action
402004e4986SSebastian Grimberg     switch (eval_mode) {
4030d0321e0SJeremy L Thompson       case CEED_EVAL_NONE:
404004e4986SSebastian Grimberg         break;  // No action
4050d0321e0SJeremy L Thompson       case CEED_EVAL_INTERP:
4060d0321e0SJeremy L Thompson       case CEED_EVAL_GRAD:
407004e4986SSebastian Grimberg       case CEED_EVAL_DIV:
408004e4986SSebastian Grimberg       case CEED_EVAL_CURL:
409ca735530SJeremy L Thompson         CeedCallBackend(CeedOperatorFieldGetBasis(op_output_fields[i], &basis));
410004e4986SSebastian Grimberg         CeedCallBackend(CeedBasisApply(basis, num_elem, CEED_TRANSPOSE, eval_mode, impl->q_vecs_out[i], impl->e_vecs[i + impl->num_inputs]));
4110d0321e0SJeremy L Thompson         break;
4120d0321e0SJeremy L Thompson       // LCOV_EXCL_START
4130d0321e0SJeremy L Thompson       case CEED_EVAL_WEIGHT: {
4146e536b99SJeremy L Thompson         return CeedError(CeedOperatorReturnCeed(op), CEED_ERROR_BACKEND, "CEED_EVAL_WEIGHT cannot be an output evaluation mode");
4150d0321e0SJeremy L Thompson         // LCOV_EXCL_STOP
4160d0321e0SJeremy L Thompson       }
4170d0321e0SJeremy L Thompson     }
418004e4986SSebastian Grimberg   }
4190d0321e0SJeremy L Thompson 
4200d0321e0SJeremy L Thompson   // Output restriction
421ca735530SJeremy L Thompson   for (CeedInt i = 0; i < num_output_fields; i++) {
422004e4986SSebastian Grimberg     CeedEvalMode        eval_mode;
423ca735530SJeremy L Thompson     CeedVector          vec;
424edb2538eSJeremy L Thompson     CeedElemRestriction elem_rstr;
425ca735530SJeremy L Thompson 
4260d0321e0SJeremy L Thompson     // Restore evec
427004e4986SSebastian Grimberg     CeedCallBackend(CeedQFunctionFieldGetEvalMode(qf_output_fields[i], &eval_mode));
428004e4986SSebastian Grimberg     if (eval_mode == CEED_EVAL_NONE) {
429ca735530SJeremy L Thompson       CeedCallBackend(CeedVectorRestoreArray(impl->e_vecs[i + impl->num_inputs], &e_data[i + num_input_fields]));
4300d0321e0SJeremy L Thompson     }
4310d0321e0SJeremy L Thompson     // Get output vector
432ca735530SJeremy L Thompson     CeedCallBackend(CeedOperatorFieldGetVector(op_output_fields[i], &vec));
4330d0321e0SJeremy L Thompson     // Restrict
434edb2538eSJeremy L Thompson     CeedCallBackend(CeedOperatorFieldGetElemRestriction(op_output_fields[i], &elem_rstr));
4350d0321e0SJeremy L Thompson     // Active
436ca735530SJeremy L Thompson     if (vec == CEED_VECTOR_ACTIVE) vec = out_vec;
4370d0321e0SJeremy L Thompson 
438edb2538eSJeremy L Thompson     CeedCallBackend(CeedElemRestrictionApply(elem_rstr, CEED_TRANSPOSE, impl->e_vecs[i + impl->num_inputs], vec, request));
4390d0321e0SJeremy L Thompson   }
4400d0321e0SJeremy L Thompson 
4410d0321e0SJeremy L Thompson   // Restore input arrays
442ca735530SJeremy L Thompson   CeedCallBackend(CeedOperatorRestoreInputs_Cuda(num_input_fields, qf_input_fields, op_input_fields, false, e_data, impl));
4430d0321e0SJeremy L Thompson   return CEED_ERROR_SUCCESS;
4440d0321e0SJeremy L Thompson }
4450d0321e0SJeremy L Thompson 
4460d0321e0SJeremy L Thompson //------------------------------------------------------------------------------
447756ca9e9SJeremy L Thompson // CeedOperator needs to connect all the named fields (be they active or passive) to the named inputs and outputs of its CeedQFunction.
448756ca9e9SJeremy L Thompson //------------------------------------------------------------------------------
449756ca9e9SJeremy L Thompson static int CeedOperatorSetupAtPoints_Cuda(CeedOperator op) {
450756ca9e9SJeremy L Thompson   Ceed                ceed;
451756ca9e9SJeremy L Thompson   bool                is_setup_done;
452756ca9e9SJeremy L Thompson   CeedInt             max_num_points = -1, num_elem, num_input_fields, num_output_fields;
453756ca9e9SJeremy L Thompson   CeedQFunctionField *qf_input_fields, *qf_output_fields;
454756ca9e9SJeremy L Thompson   CeedQFunction       qf;
455756ca9e9SJeremy L Thompson   CeedOperatorField  *op_input_fields, *op_output_fields;
456756ca9e9SJeremy L Thompson   CeedOperator_Cuda  *impl;
457756ca9e9SJeremy L Thompson 
458756ca9e9SJeremy L Thompson   CeedCallBackend(CeedOperatorIsSetupDone(op, &is_setup_done));
459756ca9e9SJeremy L Thompson   if (is_setup_done) return CEED_ERROR_SUCCESS;
460756ca9e9SJeremy L Thompson 
461756ca9e9SJeremy L Thompson   CeedCallBackend(CeedOperatorGetCeed(op, &ceed));
462756ca9e9SJeremy L Thompson   CeedCallBackend(CeedOperatorGetData(op, &impl));
463756ca9e9SJeremy L Thompson   CeedCallBackend(CeedOperatorGetQFunction(op, &qf));
464756ca9e9SJeremy L Thompson   CeedCallBackend(CeedOperatorGetNumElements(op, &num_elem));
465756ca9e9SJeremy L Thompson   CeedCallBackend(CeedOperatorGetFields(op, &num_input_fields, &op_input_fields, &num_output_fields, &op_output_fields));
466756ca9e9SJeremy L Thompson   CeedCallBackend(CeedQFunctionGetFields(qf, NULL, &qf_input_fields, NULL, &qf_output_fields));
467756ca9e9SJeremy L Thompson   {
468756ca9e9SJeremy L Thompson     CeedElemRestriction elem_rstr = NULL;
469756ca9e9SJeremy L Thompson 
470756ca9e9SJeremy L Thompson     CeedCallBackend(CeedOperatorAtPointsGetPoints(op, &elem_rstr, NULL));
471756ca9e9SJeremy L Thompson     CeedCallBackend(CeedElemRestrictionGetMaxPointsInElement(elem_rstr, &max_num_points));
472756ca9e9SJeremy L Thompson   }
473756ca9e9SJeremy L Thompson   impl->max_num_points = max_num_points;
474756ca9e9SJeremy L Thompson 
475756ca9e9SJeremy L Thompson   // Allocate
476756ca9e9SJeremy L Thompson   CeedCallBackend(CeedCalloc(num_input_fields + num_output_fields, &impl->e_vecs));
477756ca9e9SJeremy L Thompson   CeedCallBackend(CeedCalloc(CEED_FIELD_MAX, &impl->input_states));
478756ca9e9SJeremy L Thompson   CeedCallBackend(CeedCalloc(CEED_FIELD_MAX, &impl->q_vecs_in));
479756ca9e9SJeremy L Thompson   CeedCallBackend(CeedCalloc(CEED_FIELD_MAX, &impl->q_vecs_out));
480756ca9e9SJeremy L Thompson   impl->num_inputs  = num_input_fields;
481756ca9e9SJeremy L Thompson   impl->num_outputs = num_output_fields;
482756ca9e9SJeremy L Thompson 
483756ca9e9SJeremy L Thompson   // Set up infield and outfield e_vecs and q_vecs
484756ca9e9SJeremy L Thompson   // Infields
485756ca9e9SJeremy L Thompson   CeedCallBackend(CeedOperatorSetupFields_Cuda(qf, op, true, true, impl->e_vecs, impl->q_vecs_in, 0, num_input_fields, max_num_points, num_elem));
486756ca9e9SJeremy L Thompson   // Outfields
487756ca9e9SJeremy L Thompson   CeedCallBackend(CeedOperatorSetupFields_Cuda(qf, op, false, true, impl->e_vecs, impl->q_vecs_out, num_input_fields, num_output_fields,
488756ca9e9SJeremy L Thompson                                                max_num_points, num_elem));
489756ca9e9SJeremy L Thompson 
490756ca9e9SJeremy L Thompson   CeedCallBackend(CeedOperatorSetSetupDone(op));
491756ca9e9SJeremy L Thompson   return CEED_ERROR_SUCCESS;
492756ca9e9SJeremy L Thompson }
493756ca9e9SJeremy L Thompson 
494756ca9e9SJeremy L Thompson //------------------------------------------------------------------------------
49567d9480aSJeremy L Thompson // Input Basis Action AtPoints
496756ca9e9SJeremy L Thompson //------------------------------------------------------------------------------
497756ca9e9SJeremy L Thompson static inline int CeedOperatorInputBasisAtPoints_Cuda(CeedInt num_elem, const CeedInt *num_points, CeedQFunctionField *qf_input_fields,
498756ca9e9SJeremy L Thompson                                                       CeedOperatorField *op_input_fields, CeedInt num_input_fields, const bool skip_active,
499756ca9e9SJeremy L Thompson                                                       CeedScalar *e_data[2 * CEED_FIELD_MAX], CeedOperator_Cuda *impl) {
500756ca9e9SJeremy L Thompson   for (CeedInt i = 0; i < num_input_fields; i++) {
501756ca9e9SJeremy L Thompson     CeedInt             elem_size, size;
502756ca9e9SJeremy L Thompson     CeedEvalMode        eval_mode;
503756ca9e9SJeremy L Thompson     CeedElemRestriction elem_rstr;
504756ca9e9SJeremy L Thompson     CeedBasis           basis;
505756ca9e9SJeremy L Thompson 
506756ca9e9SJeremy L Thompson     // Skip active input
507756ca9e9SJeremy L Thompson     if (skip_active) {
508756ca9e9SJeremy L Thompson       CeedVector vec;
509756ca9e9SJeremy L Thompson 
510756ca9e9SJeremy L Thompson       CeedCallBackend(CeedOperatorFieldGetVector(op_input_fields[i], &vec));
511756ca9e9SJeremy L Thompson       if (vec == CEED_VECTOR_ACTIVE) continue;
512756ca9e9SJeremy L Thompson     }
513756ca9e9SJeremy L Thompson     // Get elem_size, eval_mode, size
514756ca9e9SJeremy L Thompson     CeedCallBackend(CeedOperatorFieldGetElemRestriction(op_input_fields[i], &elem_rstr));
515756ca9e9SJeremy L Thompson     CeedCallBackend(CeedElemRestrictionGetElementSize(elem_rstr, &elem_size));
516756ca9e9SJeremy L Thompson     CeedCallBackend(CeedQFunctionFieldGetEvalMode(qf_input_fields[i], &eval_mode));
517756ca9e9SJeremy L Thompson     CeedCallBackend(CeedQFunctionFieldGetSize(qf_input_fields[i], &size));
518756ca9e9SJeremy L Thompson     // Basis action
519756ca9e9SJeremy L Thompson     switch (eval_mode) {
520756ca9e9SJeremy L Thompson       case CEED_EVAL_NONE:
521756ca9e9SJeremy L Thompson         CeedCallBackend(CeedVectorSetArray(impl->q_vecs_in[i], CEED_MEM_DEVICE, CEED_USE_POINTER, e_data[i]));
522756ca9e9SJeremy L Thompson         break;
523756ca9e9SJeremy L Thompson       case CEED_EVAL_INTERP:
524756ca9e9SJeremy L Thompson       case CEED_EVAL_GRAD:
525756ca9e9SJeremy L Thompson       case CEED_EVAL_DIV:
526756ca9e9SJeremy L Thompson       case CEED_EVAL_CURL:
527756ca9e9SJeremy L Thompson         CeedCallBackend(CeedOperatorFieldGetBasis(op_input_fields[i], &basis));
528756ca9e9SJeremy L Thompson         CeedCallBackend(CeedBasisApplyAtPoints(basis, num_elem, num_points, CEED_NOTRANSPOSE, eval_mode, impl->point_coords_elem, impl->e_vecs[i],
529756ca9e9SJeremy L Thompson                                                impl->q_vecs_in[i]));
530756ca9e9SJeremy L Thompson         break;
531756ca9e9SJeremy L Thompson       case CEED_EVAL_WEIGHT:
532756ca9e9SJeremy L Thompson         break;  // No action
533756ca9e9SJeremy L Thompson     }
534756ca9e9SJeremy L Thompson   }
535756ca9e9SJeremy L Thompson   return CEED_ERROR_SUCCESS;
536756ca9e9SJeremy L Thompson }
537756ca9e9SJeremy L Thompson 
538756ca9e9SJeremy L Thompson //------------------------------------------------------------------------------
53967d9480aSJeremy L Thompson // Apply and add to output AtPoints
540756ca9e9SJeremy L Thompson //------------------------------------------------------------------------------
541756ca9e9SJeremy L Thompson static int CeedOperatorApplyAddAtPoints_Cuda(CeedOperator op, CeedVector in_vec, CeedVector out_vec, CeedRequest *request) {
542756ca9e9SJeremy L Thompson   CeedInt             max_num_points, num_elem, elem_size, num_input_fields, num_output_fields, size;
543756ca9e9SJeremy L Thompson   CeedScalar         *e_data[2 * CEED_FIELD_MAX] = {NULL};
544756ca9e9SJeremy L Thompson   CeedQFunctionField *qf_input_fields, *qf_output_fields;
545756ca9e9SJeremy L Thompson   CeedQFunction       qf;
546756ca9e9SJeremy L Thompson   CeedOperatorField  *op_input_fields, *op_output_fields;
547756ca9e9SJeremy L Thompson   CeedOperator_Cuda  *impl;
548756ca9e9SJeremy L Thompson 
549756ca9e9SJeremy L Thompson   CeedCallBackend(CeedOperatorGetData(op, &impl));
550756ca9e9SJeremy L Thompson   CeedCallBackend(CeedOperatorGetQFunction(op, &qf));
551756ca9e9SJeremy L Thompson   CeedCallBackend(CeedOperatorGetNumElements(op, &num_elem));
552756ca9e9SJeremy L Thompson   CeedCallBackend(CeedOperatorGetFields(op, &num_input_fields, &op_input_fields, &num_output_fields, &op_output_fields));
553756ca9e9SJeremy L Thompson   CeedCallBackend(CeedQFunctionGetFields(qf, NULL, &qf_input_fields, NULL, &qf_output_fields));
554756ca9e9SJeremy L Thompson   CeedInt num_points[num_elem];
555756ca9e9SJeremy L Thompson 
556756ca9e9SJeremy L Thompson   // Setup
557756ca9e9SJeremy L Thompson   CeedCallBackend(CeedOperatorSetupAtPoints_Cuda(op));
558756ca9e9SJeremy L Thompson   max_num_points = impl->max_num_points;
559756ca9e9SJeremy L Thompson   for (CeedInt i = 0; i < num_elem; i++) num_points[i] = max_num_points;
560756ca9e9SJeremy L Thompson 
561756ca9e9SJeremy L Thompson   // Input Evecs and Restriction
562756ca9e9SJeremy L Thompson   CeedCallBackend(CeedOperatorSetupInputs_Cuda(num_input_fields, qf_input_fields, op_input_fields, in_vec, false, e_data, impl, request));
563756ca9e9SJeremy L Thompson 
564756ca9e9SJeremy L Thompson   // Get point coordinates
565756ca9e9SJeremy L Thompson   if (!impl->point_coords_elem) {
566756ca9e9SJeremy L Thompson     CeedVector          point_coords = NULL;
567756ca9e9SJeremy L Thompson     CeedElemRestriction rstr_points  = NULL;
568756ca9e9SJeremy L Thompson 
569756ca9e9SJeremy L Thompson     CeedCallBackend(CeedOperatorAtPointsGetPoints(op, &rstr_points, &point_coords));
570756ca9e9SJeremy L Thompson     CeedCallBackend(CeedElemRestrictionCreateVector(rstr_points, NULL, &impl->point_coords_elem));
571756ca9e9SJeremy L Thompson     CeedCallBackend(CeedElemRestrictionApply(rstr_points, CEED_NOTRANSPOSE, point_coords, impl->point_coords_elem, request));
572756ca9e9SJeremy L Thompson   }
573756ca9e9SJeremy L Thompson 
574756ca9e9SJeremy L Thompson   // Input basis apply if needed
575756ca9e9SJeremy L Thompson   CeedCallBackend(CeedOperatorInputBasisAtPoints_Cuda(num_elem, num_points, qf_input_fields, op_input_fields, num_input_fields, false, e_data, impl));
576756ca9e9SJeremy L Thompson 
577756ca9e9SJeremy L Thompson   // Output pointers, as necessary
578756ca9e9SJeremy L Thompson   for (CeedInt i = 0; i < num_output_fields; i++) {
579756ca9e9SJeremy L Thompson     CeedEvalMode eval_mode;
580756ca9e9SJeremy L Thompson 
581756ca9e9SJeremy L Thompson     CeedCallBackend(CeedQFunctionFieldGetEvalMode(qf_output_fields[i], &eval_mode));
582756ca9e9SJeremy L Thompson     if (eval_mode == CEED_EVAL_NONE) {
583756ca9e9SJeremy L Thompson       // Set the output Q-Vector to use the E-Vector data directly.
584756ca9e9SJeremy L Thompson       CeedCallBackend(CeedVectorGetArrayWrite(impl->e_vecs[i + impl->num_inputs], CEED_MEM_DEVICE, &e_data[i + num_input_fields]));
585756ca9e9SJeremy L Thompson       CeedCallBackend(CeedVectorSetArray(impl->q_vecs_out[i], CEED_MEM_DEVICE, CEED_USE_POINTER, e_data[i + num_input_fields]));
586756ca9e9SJeremy L Thompson     }
587756ca9e9SJeremy L Thompson   }
588756ca9e9SJeremy L Thompson 
589756ca9e9SJeremy L Thompson   // Q function
590756ca9e9SJeremy L Thompson   CeedCallBackend(CeedQFunctionApply(qf, num_elem * max_num_points, impl->q_vecs_in, impl->q_vecs_out));
591756ca9e9SJeremy L Thompson 
592756ca9e9SJeremy L Thompson   // Output basis apply if needed
593756ca9e9SJeremy L Thompson   for (CeedInt i = 0; i < num_output_fields; i++) {
594756ca9e9SJeremy L Thompson     CeedEvalMode        eval_mode;
595756ca9e9SJeremy L Thompson     CeedElemRestriction elem_rstr;
596756ca9e9SJeremy L Thompson     CeedBasis           basis;
597756ca9e9SJeremy L Thompson 
598756ca9e9SJeremy L Thompson     // Get elem_size, eval_mode, size
599756ca9e9SJeremy L Thompson     CeedCallBackend(CeedOperatorFieldGetElemRestriction(op_output_fields[i], &elem_rstr));
600756ca9e9SJeremy L Thompson     CeedCallBackend(CeedElemRestrictionGetElementSize(elem_rstr, &elem_size));
601756ca9e9SJeremy L Thompson     CeedCallBackend(CeedQFunctionFieldGetEvalMode(qf_output_fields[i], &eval_mode));
602756ca9e9SJeremy L Thompson     CeedCallBackend(CeedQFunctionFieldGetSize(qf_output_fields[i], &size));
603756ca9e9SJeremy L Thompson     // Basis action
604756ca9e9SJeremy L Thompson     switch (eval_mode) {
605756ca9e9SJeremy L Thompson       case CEED_EVAL_NONE:
606756ca9e9SJeremy L Thompson         break;  // No action
607756ca9e9SJeremy L Thompson       case CEED_EVAL_INTERP:
608756ca9e9SJeremy L Thompson       case CEED_EVAL_GRAD:
609756ca9e9SJeremy L Thompson       case CEED_EVAL_DIV:
610756ca9e9SJeremy L Thompson       case CEED_EVAL_CURL:
611756ca9e9SJeremy L Thompson         CeedCallBackend(CeedOperatorFieldGetBasis(op_output_fields[i], &basis));
612756ca9e9SJeremy L Thompson         CeedCallBackend(CeedBasisApplyAtPoints(basis, num_elem, num_points, CEED_TRANSPOSE, eval_mode, impl->point_coords_elem, impl->q_vecs_out[i],
613756ca9e9SJeremy L Thompson                                                impl->e_vecs[i + impl->num_inputs]));
614756ca9e9SJeremy L Thompson         break;
615756ca9e9SJeremy L Thompson       // LCOV_EXCL_START
616756ca9e9SJeremy L Thompson       case CEED_EVAL_WEIGHT: {
617756ca9e9SJeremy L Thompson         return CeedError(CeedOperatorReturnCeed(op), CEED_ERROR_BACKEND, "CEED_EVAL_WEIGHT cannot be an output evaluation mode");
618756ca9e9SJeremy L Thompson         // LCOV_EXCL_STOP
619756ca9e9SJeremy L Thompson       }
620756ca9e9SJeremy L Thompson     }
621756ca9e9SJeremy L Thompson   }
622756ca9e9SJeremy L Thompson 
623756ca9e9SJeremy L Thompson   // Output restriction
624756ca9e9SJeremy L Thompson   for (CeedInt i = 0; i < num_output_fields; i++) {
625756ca9e9SJeremy L Thompson     CeedEvalMode        eval_mode;
626756ca9e9SJeremy L Thompson     CeedVector          vec;
627756ca9e9SJeremy L Thompson     CeedElemRestriction elem_rstr;
628756ca9e9SJeremy L Thompson 
629756ca9e9SJeremy L Thompson     // Restore evec
630756ca9e9SJeremy L Thompson     CeedCallBackend(CeedQFunctionFieldGetEvalMode(qf_output_fields[i], &eval_mode));
631756ca9e9SJeremy L Thompson     if (eval_mode == CEED_EVAL_NONE) {
632756ca9e9SJeremy L Thompson       CeedCallBackend(CeedVectorRestoreArray(impl->e_vecs[i + impl->num_inputs], &e_data[i + num_input_fields]));
633756ca9e9SJeremy L Thompson     }
634756ca9e9SJeremy L Thompson     // Get output vector
635756ca9e9SJeremy L Thompson     CeedCallBackend(CeedOperatorFieldGetVector(op_output_fields[i], &vec));
636756ca9e9SJeremy L Thompson     // Restrict
637756ca9e9SJeremy L Thompson     CeedCallBackend(CeedOperatorFieldGetElemRestriction(op_output_fields[i], &elem_rstr));
638756ca9e9SJeremy L Thompson     // Active
639756ca9e9SJeremy L Thompson     if (vec == CEED_VECTOR_ACTIVE) vec = out_vec;
640756ca9e9SJeremy L Thompson 
641756ca9e9SJeremy L Thompson     CeedCallBackend(CeedElemRestrictionApply(elem_rstr, CEED_TRANSPOSE, impl->e_vecs[i + impl->num_inputs], vec, request));
642756ca9e9SJeremy L Thompson   }
643756ca9e9SJeremy L Thompson 
644756ca9e9SJeremy L Thompson   // Restore input arrays
645756ca9e9SJeremy L Thompson   CeedCallBackend(CeedOperatorRestoreInputs_Cuda(num_input_fields, qf_input_fields, op_input_fields, false, e_data, impl));
646756ca9e9SJeremy L Thompson   return CEED_ERROR_SUCCESS;
647756ca9e9SJeremy L Thompson }
648756ca9e9SJeremy L Thompson 
649756ca9e9SJeremy L Thompson //------------------------------------------------------------------------------
650004e4986SSebastian Grimberg // Linear QFunction Assembly Core
6510d0321e0SJeremy L Thompson //------------------------------------------------------------------------------
6522b730f8bSJeremy L Thompson static inline int CeedOperatorLinearAssembleQFunctionCore_Cuda(CeedOperator op, bool build_objects, CeedVector *assembled, CeedElemRestriction *rstr,
6530d0321e0SJeremy L Thompson                                                                CeedRequest *request) {
654ca735530SJeremy L Thompson   Ceed                ceed, ceed_parent;
655ca735530SJeremy L Thompson   CeedInt             num_active_in, num_active_out, Q, num_elem, num_input_fields, num_output_fields, size;
656ca735530SJeremy L Thompson   CeedScalar         *assembled_array, *e_data[2 * CEED_FIELD_MAX] = {NULL};
657ca735530SJeremy L Thompson   CeedVector         *active_inputs;
658ca735530SJeremy L Thompson   CeedQFunctionField *qf_input_fields, *qf_output_fields;
659ca735530SJeremy L Thompson   CeedQFunction       qf;
660ca735530SJeremy L Thompson   CeedOperatorField  *op_input_fields, *op_output_fields;
661ca735530SJeremy L Thompson   CeedOperator_Cuda  *impl;
662ca735530SJeremy L Thompson 
6632b730f8bSJeremy L Thompson   CeedCallBackend(CeedOperatorGetCeed(op, &ceed));
664ca735530SJeremy L Thompson   CeedCallBackend(CeedOperatorGetFallbackParentCeed(op, &ceed_parent));
665e984cf9aSJeremy L Thompson   CeedCallBackend(CeedOperatorGetData(op, &impl));
666e984cf9aSJeremy L Thompson   CeedCallBackend(CeedOperatorGetNumQuadraturePoints(op, &Q));
667ca735530SJeremy L Thompson   CeedCallBackend(CeedOperatorGetNumElements(op, &num_elem));
668e984cf9aSJeremy L Thompson   CeedCallBackend(CeedOperatorGetQFunction(op, &qf));
669ca735530SJeremy L Thompson   CeedCallBackend(CeedQFunctionGetFields(qf, NULL, &qf_input_fields, NULL, &qf_output_fields));
670ca735530SJeremy L Thompson   CeedCallBackend(CeedOperatorGetFields(op, &num_input_fields, &op_input_fields, &num_output_fields, &op_output_fields));
671ca735530SJeremy L Thompson   active_inputs = impl->qf_active_in;
672ca735530SJeremy L Thompson   num_active_in = impl->num_active_in, num_active_out = impl->num_active_out;
6730d0321e0SJeremy L Thompson 
6740d0321e0SJeremy L Thompson   // Setup
6752b730f8bSJeremy L Thompson   CeedCallBackend(CeedOperatorSetup_Cuda(op));
6760d0321e0SJeremy L Thompson 
677004e4986SSebastian Grimberg   // Input Evecs and Restriction
678ca735530SJeremy L Thompson   CeedCallBackend(CeedOperatorSetupInputs_Cuda(num_input_fields, qf_input_fields, op_input_fields, NULL, true, e_data, impl, request));
6790d0321e0SJeremy L Thompson 
6800d0321e0SJeremy L Thompson   // Count number of active input fields
681ca735530SJeremy L Thompson   if (!num_active_in) {
682ca735530SJeremy L Thompson     for (CeedInt i = 0; i < num_input_fields; i++) {
683ca735530SJeremy L Thompson       CeedScalar *q_vec_array;
684ca735530SJeremy L Thompson       CeedVector  vec;
685ca735530SJeremy L Thompson 
6860d0321e0SJeremy L Thompson       // Get input vector
687ca735530SJeremy L Thompson       CeedCallBackend(CeedOperatorFieldGetVector(op_input_fields[i], &vec));
6880d0321e0SJeremy L Thompson       // Check if active input
6890d0321e0SJeremy L Thompson       if (vec == CEED_VECTOR_ACTIVE) {
690ca735530SJeremy L Thompson         CeedCallBackend(CeedQFunctionFieldGetSize(qf_input_fields[i], &size));
691ca735530SJeremy L Thompson         CeedCallBackend(CeedVectorSetValue(impl->q_vecs_in[i], 0.0));
692ca735530SJeremy L Thompson         CeedCallBackend(CeedVectorGetArray(impl->q_vecs_in[i], CEED_MEM_DEVICE, &q_vec_array));
693ca735530SJeremy L Thompson         CeedCallBackend(CeedRealloc(num_active_in + size, &active_inputs));
6940d0321e0SJeremy L Thompson         for (CeedInt field = 0; field < size; field++) {
695004e4986SSebastian Grimberg           CeedSize q_size = (CeedSize)Q * num_elem;
696004e4986SSebastian Grimberg 
697ca735530SJeremy L Thompson           CeedCallBackend(CeedVectorCreate(ceed, q_size, &active_inputs[num_active_in + field]));
698ca735530SJeremy L Thompson           CeedCallBackend(
699ca735530SJeremy L Thompson               CeedVectorSetArray(active_inputs[num_active_in + field], CEED_MEM_DEVICE, CEED_USE_POINTER, &q_vec_array[field * Q * num_elem]));
7000d0321e0SJeremy L Thompson         }
701ca735530SJeremy L Thompson         num_active_in += size;
702ca735530SJeremy L Thompson         CeedCallBackend(CeedVectorRestoreArray(impl->q_vecs_in[i], &q_vec_array));
7030d0321e0SJeremy L Thompson       }
7040d0321e0SJeremy L Thompson     }
705ca735530SJeremy L Thompson     impl->num_active_in = num_active_in;
706ca735530SJeremy L Thompson     impl->qf_active_in  = active_inputs;
7070d0321e0SJeremy L Thompson   }
7080d0321e0SJeremy L Thompson 
7090d0321e0SJeremy L Thompson   // Count number of active output fields
710ca735530SJeremy L Thompson   if (!num_active_out) {
711ca735530SJeremy L Thompson     for (CeedInt i = 0; i < num_output_fields; i++) {
712ca735530SJeremy L Thompson       CeedVector vec;
713ca735530SJeremy L Thompson 
7140d0321e0SJeremy L Thompson       // Get output vector
715ca735530SJeremy L Thompson       CeedCallBackend(CeedOperatorFieldGetVector(op_output_fields[i], &vec));
7160d0321e0SJeremy L Thompson       // Check if active output
7170d0321e0SJeremy L Thompson       if (vec == CEED_VECTOR_ACTIVE) {
718ca735530SJeremy L Thompson         CeedCallBackend(CeedQFunctionFieldGetSize(qf_output_fields[i], &size));
719ca735530SJeremy L Thompson         num_active_out += size;
7200d0321e0SJeremy L Thompson       }
7210d0321e0SJeremy L Thompson     }
722ca735530SJeremy L Thompson     impl->num_active_out = num_active_out;
7230d0321e0SJeremy L Thompson   }
7240d0321e0SJeremy L Thompson 
7250d0321e0SJeremy L Thompson   // Check sizes
726ca735530SJeremy L Thompson   CeedCheck(num_active_in > 0 && num_active_out > 0, ceed, CEED_ERROR_BACKEND, "Cannot assemble QFunction without active inputs and outputs");
7270d0321e0SJeremy L Thompson 
7280d0321e0SJeremy L Thompson   // Build objects if needed
7290d0321e0SJeremy L Thompson   if (build_objects) {
730004e4986SSebastian Grimberg     CeedSize l_size     = (CeedSize)num_elem * Q * num_active_in * num_active_out;
731ca735530SJeremy L Thompson     CeedInt  strides[3] = {1, num_elem * Q, Q}; /* *NOPAD* */
732004e4986SSebastian Grimberg 
733004e4986SSebastian Grimberg     // Create output restriction
734ca735530SJeremy L Thompson     CeedCallBackend(CeedElemRestrictionCreateStrided(ceed_parent, num_elem, Q, num_active_in * num_active_out,
7350a5597ceSJeremy L Thompson                                                      (CeedSize)num_active_in * (CeedSize)num_active_out * (CeedSize)num_elem * (CeedSize)Q, strides,
7360a5597ceSJeremy L Thompson                                                      rstr));
7370d0321e0SJeremy L Thompson     // Create assembled vector
738ca735530SJeremy L Thompson     CeedCallBackend(CeedVectorCreate(ceed_parent, l_size, assembled));
7390d0321e0SJeremy L Thompson   }
7402b730f8bSJeremy L Thompson   CeedCallBackend(CeedVectorSetValue(*assembled, 0.0));
741ca735530SJeremy L Thompson   CeedCallBackend(CeedVectorGetArray(*assembled, CEED_MEM_DEVICE, &assembled_array));
7420d0321e0SJeremy L Thompson 
7430d0321e0SJeremy L Thompson   // Input basis apply
744ca735530SJeremy L Thompson   CeedCallBackend(CeedOperatorInputBasis_Cuda(num_elem, qf_input_fields, op_input_fields, num_input_fields, true, e_data, impl));
7450d0321e0SJeremy L Thompson 
7460d0321e0SJeremy L Thompson   // Assemble QFunction
747ca735530SJeremy L Thompson   for (CeedInt in = 0; in < num_active_in; in++) {
7480d0321e0SJeremy L Thompson     // Set Inputs
749ca735530SJeremy L Thompson     CeedCallBackend(CeedVectorSetValue(active_inputs[in], 1.0));
750ca735530SJeremy L Thompson     if (num_active_in > 1) {
751ca735530SJeremy L Thompson       CeedCallBackend(CeedVectorSetValue(active_inputs[(in + num_active_in - 1) % num_active_in], 0.0));
7520d0321e0SJeremy L Thompson     }
7530d0321e0SJeremy L Thompson     // Set Outputs
754ca735530SJeremy L Thompson     for (CeedInt out = 0; out < num_output_fields; out++) {
755ca735530SJeremy L Thompson       CeedVector vec;
756ca735530SJeremy L Thompson 
7570d0321e0SJeremy L Thompson       // Get output vector
758ca735530SJeremy L Thompson       CeedCallBackend(CeedOperatorFieldGetVector(op_output_fields[out], &vec));
7590d0321e0SJeremy L Thompson       // Check if active output
7600d0321e0SJeremy L Thompson       if (vec == CEED_VECTOR_ACTIVE) {
761ca735530SJeremy L Thompson         CeedCallBackend(CeedVectorSetArray(impl->q_vecs_out[out], CEED_MEM_DEVICE, CEED_USE_POINTER, assembled_array));
762ca735530SJeremy L Thompson         CeedCallBackend(CeedQFunctionFieldGetSize(qf_output_fields[out], &size));
763ca735530SJeremy L Thompson         assembled_array += size * Q * num_elem;  // Advance the pointer by the size of the output
7640d0321e0SJeremy L Thompson       }
7650d0321e0SJeremy L Thompson     }
7660d0321e0SJeremy L Thompson     // Apply QFunction
767ca735530SJeremy L Thompson     CeedCallBackend(CeedQFunctionApply(qf, Q * num_elem, impl->q_vecs_in, impl->q_vecs_out));
7680d0321e0SJeremy L Thompson   }
7690d0321e0SJeremy L Thompson 
770ca735530SJeremy L Thompson   // Un-set output q_vecs to prevent accidental overwrite of Assembled
771ca735530SJeremy L Thompson   for (CeedInt out = 0; out < num_output_fields; out++) {
772ca735530SJeremy L Thompson     CeedVector vec;
773ca735530SJeremy L Thompson 
7740d0321e0SJeremy L Thompson     // Get output vector
775ca735530SJeremy L Thompson     CeedCallBackend(CeedOperatorFieldGetVector(op_output_fields[out], &vec));
7760d0321e0SJeremy L Thompson     // Check if active output
7770d0321e0SJeremy L Thompson     if (vec == CEED_VECTOR_ACTIVE) {
778ca735530SJeremy L Thompson       CeedCallBackend(CeedVectorTakeArray(impl->q_vecs_out[out], CEED_MEM_DEVICE, NULL));
7790d0321e0SJeremy L Thompson     }
7800d0321e0SJeremy L Thompson   }
7810d0321e0SJeremy L Thompson 
7820d0321e0SJeremy L Thompson   // Restore input arrays
783ca735530SJeremy L Thompson   CeedCallBackend(CeedOperatorRestoreInputs_Cuda(num_input_fields, qf_input_fields, op_input_fields, true, e_data, impl));
7840d0321e0SJeremy L Thompson 
7850d0321e0SJeremy L Thompson   // Restore output
786ca735530SJeremy L Thompson   CeedCallBackend(CeedVectorRestoreArray(*assembled, &assembled_array));
7870d0321e0SJeremy L Thompson   return CEED_ERROR_SUCCESS;
7880d0321e0SJeremy L Thompson }
7890d0321e0SJeremy L Thompson 
7900d0321e0SJeremy L Thompson //------------------------------------------------------------------------------
7910d0321e0SJeremy L Thompson // Assemble Linear QFunction
7920d0321e0SJeremy L Thompson //------------------------------------------------------------------------------
7932b730f8bSJeremy L Thompson static int CeedOperatorLinearAssembleQFunction_Cuda(CeedOperator op, CeedVector *assembled, CeedElemRestriction *rstr, CeedRequest *request) {
7942b730f8bSJeremy L Thompson   return CeedOperatorLinearAssembleQFunctionCore_Cuda(op, true, assembled, rstr, request);
7950d0321e0SJeremy L Thompson }
7960d0321e0SJeremy L Thompson 
7970d0321e0SJeremy L Thompson //------------------------------------------------------------------------------
7980d0321e0SJeremy L Thompson // Update Assembled Linear QFunction
7990d0321e0SJeremy L Thompson //------------------------------------------------------------------------------
8002b730f8bSJeremy L Thompson static int CeedOperatorLinearAssembleQFunctionUpdate_Cuda(CeedOperator op, CeedVector assembled, CeedElemRestriction rstr, CeedRequest *request) {
8012b730f8bSJeremy L Thompson   return CeedOperatorLinearAssembleQFunctionCore_Cuda(op, false, &assembled, &rstr, request);
8020d0321e0SJeremy L Thompson }
8030d0321e0SJeremy L Thompson 
8040d0321e0SJeremy L Thompson //------------------------------------------------------------------------------
805004e4986SSebastian Grimberg // Assemble Diagonal Setup
8060d0321e0SJeremy L Thompson //------------------------------------------------------------------------------
807cbfe683aSSebastian Grimberg static inline int CeedOperatorAssembleDiagonalSetup_Cuda(CeedOperator op) {
8080d0321e0SJeremy L Thompson   Ceed                ceed;
809004e4986SSebastian Grimberg   CeedInt             num_input_fields, num_output_fields, num_eval_modes_in = 0, num_eval_modes_out = 0;
810cbfe683aSSebastian Grimberg   CeedInt             q_comp, num_nodes, num_qpts;
811004e4986SSebastian Grimberg   CeedEvalMode       *eval_modes_in = NULL, *eval_modes_out = NULL;
812ca735530SJeremy L Thompson   CeedBasis           basis_in = NULL, basis_out = NULL;
813ca735530SJeremy L Thompson   CeedQFunctionField *qf_fields;
8140d0321e0SJeremy L Thompson   CeedQFunction       qf;
815ca735530SJeremy L Thompson   CeedOperatorField  *op_fields;
816ca735530SJeremy L Thompson   CeedOperator_Cuda  *impl;
817ca735530SJeremy L Thompson 
818ca735530SJeremy L Thompson   CeedCallBackend(CeedOperatorGetCeed(op, &ceed));
8192b730f8bSJeremy L Thompson   CeedCallBackend(CeedOperatorGetQFunction(op, &qf));
820ca735530SJeremy L Thompson   CeedCallBackend(CeedQFunctionGetNumArgs(qf, &num_input_fields, &num_output_fields));
8210d0321e0SJeremy L Thompson 
8220d0321e0SJeremy L Thompson   // Determine active input basis
823ca735530SJeremy L Thompson   CeedCallBackend(CeedOperatorGetFields(op, NULL, &op_fields, NULL, NULL));
824ca735530SJeremy L Thompson   CeedCallBackend(CeedQFunctionGetFields(qf, NULL, &qf_fields, NULL, NULL));
825ca735530SJeremy L Thompson   for (CeedInt i = 0; i < num_input_fields; i++) {
8260d0321e0SJeremy L Thompson     CeedVector vec;
827ca735530SJeremy L Thompson 
828ca735530SJeremy L Thompson     CeedCallBackend(CeedOperatorFieldGetVector(op_fields[i], &vec));
8290d0321e0SJeremy L Thompson     if (vec == CEED_VECTOR_ACTIVE) {
830004e4986SSebastian Grimberg       CeedBasis    basis;
831004e4986SSebastian Grimberg       CeedEvalMode eval_mode;
832ca735530SJeremy L Thompson 
833004e4986SSebastian Grimberg       CeedCallBackend(CeedOperatorFieldGetBasis(op_fields[i], &basis));
834004e4986SSebastian Grimberg       CeedCheck(!basis_in || basis_in == basis, ceed, CEED_ERROR_BACKEND,
835004e4986SSebastian Grimberg                 "Backend does not implement operator diagonal assembly with multiple active bases");
836004e4986SSebastian Grimberg       basis_in = basis;
837004e4986SSebastian Grimberg       CeedCallBackend(CeedQFunctionFieldGetEvalMode(qf_fields[i], &eval_mode));
838004e4986SSebastian Grimberg       CeedCallBackend(CeedBasisGetNumQuadratureComponents(basis_in, eval_mode, &q_comp));
839004e4986SSebastian Grimberg       if (eval_mode != CEED_EVAL_WEIGHT) {
840004e4986SSebastian Grimberg         // q_comp = 1 if CEED_EVAL_NONE, CEED_EVAL_WEIGHT caught by QF assembly
841004e4986SSebastian Grimberg         CeedCallBackend(CeedRealloc(num_eval_modes_in + q_comp, &eval_modes_in));
842004e4986SSebastian Grimberg         for (CeedInt d = 0; d < q_comp; d++) eval_modes_in[num_eval_modes_in + d] = eval_mode;
843004e4986SSebastian Grimberg         num_eval_modes_in += q_comp;
8440d0321e0SJeremy L Thompson       }
8450d0321e0SJeremy L Thompson     }
8460d0321e0SJeremy L Thompson   }
8470d0321e0SJeremy L Thompson 
8480d0321e0SJeremy L Thompson   // Determine active output basis
849ca735530SJeremy L Thompson   CeedCallBackend(CeedOperatorGetFields(op, NULL, NULL, NULL, &op_fields));
850ca735530SJeremy L Thompson   CeedCallBackend(CeedQFunctionGetFields(qf, NULL, NULL, NULL, &qf_fields));
851ca735530SJeremy L Thompson   for (CeedInt i = 0; i < num_output_fields; i++) {
8520d0321e0SJeremy L Thompson     CeedVector vec;
853ca735530SJeremy L Thompson 
854ca735530SJeremy L Thompson     CeedCallBackend(CeedOperatorFieldGetVector(op_fields[i], &vec));
8550d0321e0SJeremy L Thompson     if (vec == CEED_VECTOR_ACTIVE) {
856004e4986SSebastian Grimberg       CeedBasis    basis;
857004e4986SSebastian Grimberg       CeedEvalMode eval_mode;
858ca735530SJeremy L Thompson 
859004e4986SSebastian Grimberg       CeedCallBackend(CeedOperatorFieldGetBasis(op_fields[i], &basis));
860004e4986SSebastian Grimberg       CeedCheck(!basis_out || basis_out == basis, ceed, CEED_ERROR_BACKEND,
861004e4986SSebastian Grimberg                 "Backend does not implement operator diagonal assembly with multiple active bases");
862004e4986SSebastian Grimberg       basis_out = basis;
863004e4986SSebastian Grimberg       CeedCallBackend(CeedQFunctionFieldGetEvalMode(qf_fields[i], &eval_mode));
864004e4986SSebastian Grimberg       CeedCallBackend(CeedBasisGetNumQuadratureComponents(basis_out, eval_mode, &q_comp));
865004e4986SSebastian Grimberg       if (eval_mode != CEED_EVAL_WEIGHT) {
866004e4986SSebastian Grimberg         // q_comp = 1 if CEED_EVAL_NONE, CEED_EVAL_WEIGHT caught by QF assembly
867004e4986SSebastian Grimberg         CeedCallBackend(CeedRealloc(num_eval_modes_out + q_comp, &eval_modes_out));
868004e4986SSebastian Grimberg         for (CeedInt d = 0; d < q_comp; d++) eval_modes_out[num_eval_modes_out + d] = eval_mode;
869004e4986SSebastian Grimberg         num_eval_modes_out += q_comp;
8700d0321e0SJeremy L Thompson       }
8710d0321e0SJeremy L Thompson     }
8720d0321e0SJeremy L Thompson   }
8730d0321e0SJeremy L Thompson 
8740d0321e0SJeremy L Thompson   // Operator data struct
8752b730f8bSJeremy L Thompson   CeedCallBackend(CeedOperatorGetData(op, &impl));
8762b730f8bSJeremy L Thompson   CeedCallBackend(CeedCalloc(1, &impl->diag));
8770d0321e0SJeremy L Thompson   CeedOperatorDiag_Cuda *diag = impl->diag;
878ca735530SJeremy L Thompson 
879cbfe683aSSebastian Grimberg   // Basis matrices
880004e4986SSebastian Grimberg   CeedCallBackend(CeedBasisGetNumNodes(basis_in, &num_nodes));
881004e4986SSebastian Grimberg   if (basis_in == CEED_BASIS_NONE) num_qpts = num_nodes;
882004e4986SSebastian Grimberg   else CeedCallBackend(CeedBasisGetNumQuadraturePoints(basis_in, &num_qpts));
883004e4986SSebastian Grimberg   const CeedInt interp_bytes     = num_nodes * num_qpts * sizeof(CeedScalar);
884004e4986SSebastian Grimberg   const CeedInt eval_modes_bytes = sizeof(CeedEvalMode);
885004e4986SSebastian Grimberg   bool          has_eval_none    = false;
8860d0321e0SJeremy L Thompson 
8870d0321e0SJeremy L Thompson   // CEED_EVAL_NONE
888004e4986SSebastian Grimberg   for (CeedInt i = 0; i < num_eval_modes_in; i++) has_eval_none = has_eval_none || (eval_modes_in[i] == CEED_EVAL_NONE);
889004e4986SSebastian Grimberg   for (CeedInt i = 0; i < num_eval_modes_out; i++) has_eval_none = has_eval_none || (eval_modes_out[i] == CEED_EVAL_NONE);
890004e4986SSebastian Grimberg   if (has_eval_none) {
8910d0321e0SJeremy L Thompson     CeedScalar *identity = NULL;
892ca735530SJeremy L Thompson 
893004e4986SSebastian Grimberg     CeedCallBackend(CeedCalloc(num_nodes * num_qpts, &identity));
894ca735530SJeremy L Thompson     for (CeedInt i = 0; i < (num_nodes < num_qpts ? num_nodes : num_qpts); i++) identity[i * num_nodes + i] = 1.0;
895ca735530SJeremy L Thompson     CeedCallCuda(ceed, cudaMalloc((void **)&diag->d_identity, interp_bytes));
896ca735530SJeremy L Thompson     CeedCallCuda(ceed, cudaMemcpy(diag->d_identity, identity, interp_bytes, cudaMemcpyHostToDevice));
897004e4986SSebastian Grimberg     CeedCallBackend(CeedFree(&identity));
8980d0321e0SJeremy L Thompson   }
8990d0321e0SJeremy L Thompson 
900004e4986SSebastian Grimberg   // CEED_EVAL_INTERP, CEED_EVAL_GRAD, CEED_EVAL_DIV, and CEED_EVAL_CURL
901004e4986SSebastian Grimberg   for (CeedInt in = 0; in < 2; in++) {
902004e4986SSebastian Grimberg     CeedFESpace fespace;
903004e4986SSebastian Grimberg     CeedBasis   basis = in ? basis_in : basis_out;
9040d0321e0SJeremy L Thompson 
905004e4986SSebastian Grimberg     CeedCallBackend(CeedBasisGetFESpace(basis, &fespace));
906004e4986SSebastian Grimberg     switch (fespace) {
907004e4986SSebastian Grimberg       case CEED_FE_SPACE_H1: {
908004e4986SSebastian Grimberg         CeedInt           q_comp_interp, q_comp_grad;
909004e4986SSebastian Grimberg         const CeedScalar *interp, *grad;
910004e4986SSebastian Grimberg         CeedScalar       *d_interp, *d_grad;
9110d0321e0SJeremy L Thompson 
912004e4986SSebastian Grimberg         CeedCallBackend(CeedBasisGetNumQuadratureComponents(basis, CEED_EVAL_INTERP, &q_comp_interp));
913004e4986SSebastian Grimberg         CeedCallBackend(CeedBasisGetNumQuadratureComponents(basis, CEED_EVAL_GRAD, &q_comp_grad));
9140d0321e0SJeremy L Thompson 
915004e4986SSebastian Grimberg         CeedCallBackend(CeedBasisGetInterp(basis, &interp));
916004e4986SSebastian Grimberg         CeedCallCuda(ceed, cudaMalloc((void **)&d_interp, interp_bytes * q_comp_interp));
917004e4986SSebastian Grimberg         CeedCallCuda(ceed, cudaMemcpy(d_interp, interp, interp_bytes * q_comp_interp, cudaMemcpyHostToDevice));
918004e4986SSebastian Grimberg         CeedCallBackend(CeedBasisGetGrad(basis, &grad));
919004e4986SSebastian Grimberg         CeedCallCuda(ceed, cudaMalloc((void **)&d_grad, interp_bytes * q_comp_grad));
920004e4986SSebastian Grimberg         CeedCallCuda(ceed, cudaMemcpy(d_grad, grad, interp_bytes * q_comp_grad, cudaMemcpyHostToDevice));
921004e4986SSebastian Grimberg         if (in) {
922004e4986SSebastian Grimberg           diag->d_interp_in = d_interp;
923004e4986SSebastian Grimberg           diag->d_grad_in   = d_grad;
924004e4986SSebastian Grimberg         } else {
925004e4986SSebastian Grimberg           diag->d_interp_out = d_interp;
926004e4986SSebastian Grimberg           diag->d_grad_out   = d_grad;
927004e4986SSebastian Grimberg         }
928004e4986SSebastian Grimberg       } break;
929004e4986SSebastian Grimberg       case CEED_FE_SPACE_HDIV: {
930004e4986SSebastian Grimberg         CeedInt           q_comp_interp, q_comp_div;
931004e4986SSebastian Grimberg         const CeedScalar *interp, *div;
932004e4986SSebastian Grimberg         CeedScalar       *d_interp, *d_div;
933004e4986SSebastian Grimberg 
934004e4986SSebastian Grimberg         CeedCallBackend(CeedBasisGetNumQuadratureComponents(basis, CEED_EVAL_INTERP, &q_comp_interp));
935004e4986SSebastian Grimberg         CeedCallBackend(CeedBasisGetNumQuadratureComponents(basis, CEED_EVAL_DIV, &q_comp_div));
936004e4986SSebastian Grimberg 
937004e4986SSebastian Grimberg         CeedCallBackend(CeedBasisGetInterp(basis, &interp));
938004e4986SSebastian Grimberg         CeedCallCuda(ceed, cudaMalloc((void **)&d_interp, interp_bytes * q_comp_interp));
939004e4986SSebastian Grimberg         CeedCallCuda(ceed, cudaMemcpy(d_interp, interp, interp_bytes * q_comp_interp, cudaMemcpyHostToDevice));
940004e4986SSebastian Grimberg         CeedCallBackend(CeedBasisGetDiv(basis, &div));
941004e4986SSebastian Grimberg         CeedCallCuda(ceed, cudaMalloc((void **)&d_div, interp_bytes * q_comp_div));
942004e4986SSebastian Grimberg         CeedCallCuda(ceed, cudaMemcpy(d_div, div, interp_bytes * q_comp_div, cudaMemcpyHostToDevice));
943004e4986SSebastian Grimberg         if (in) {
944004e4986SSebastian Grimberg           diag->d_interp_in = d_interp;
945004e4986SSebastian Grimberg           diag->d_div_in    = d_div;
946004e4986SSebastian Grimberg         } else {
947004e4986SSebastian Grimberg           diag->d_interp_out = d_interp;
948004e4986SSebastian Grimberg           diag->d_div_out    = d_div;
949004e4986SSebastian Grimberg         }
950004e4986SSebastian Grimberg       } break;
951004e4986SSebastian Grimberg       case CEED_FE_SPACE_HCURL: {
952004e4986SSebastian Grimberg         CeedInt           q_comp_interp, q_comp_curl;
953004e4986SSebastian Grimberg         const CeedScalar *interp, *curl;
954004e4986SSebastian Grimberg         CeedScalar       *d_interp, *d_curl;
955004e4986SSebastian Grimberg 
956004e4986SSebastian Grimberg         CeedCallBackend(CeedBasisGetNumQuadratureComponents(basis, CEED_EVAL_INTERP, &q_comp_interp));
957004e4986SSebastian Grimberg         CeedCallBackend(CeedBasisGetNumQuadratureComponents(basis, CEED_EVAL_CURL, &q_comp_curl));
958004e4986SSebastian Grimberg 
959004e4986SSebastian Grimberg         CeedCallBackend(CeedBasisGetInterp(basis, &interp));
960004e4986SSebastian Grimberg         CeedCallCuda(ceed, cudaMalloc((void **)&d_interp, interp_bytes * q_comp_interp));
961004e4986SSebastian Grimberg         CeedCallCuda(ceed, cudaMemcpy(d_interp, interp, interp_bytes * q_comp_interp, cudaMemcpyHostToDevice));
962004e4986SSebastian Grimberg         CeedCallBackend(CeedBasisGetCurl(basis, &curl));
963004e4986SSebastian Grimberg         CeedCallCuda(ceed, cudaMalloc((void **)&d_curl, interp_bytes * q_comp_curl));
964004e4986SSebastian Grimberg         CeedCallCuda(ceed, cudaMemcpy(d_curl, curl, interp_bytes * q_comp_curl, cudaMemcpyHostToDevice));
965004e4986SSebastian Grimberg         if (in) {
966004e4986SSebastian Grimberg           diag->d_interp_in = d_interp;
967004e4986SSebastian Grimberg           diag->d_curl_in   = d_curl;
968004e4986SSebastian Grimberg         } else {
969004e4986SSebastian Grimberg           diag->d_interp_out = d_interp;
970004e4986SSebastian Grimberg           diag->d_curl_out   = d_curl;
971004e4986SSebastian Grimberg         }
972004e4986SSebastian Grimberg       } break;
973004e4986SSebastian Grimberg     }
974004e4986SSebastian Grimberg   }
975004e4986SSebastian Grimberg 
976004e4986SSebastian Grimberg   // Arrays of eval_modes
977004e4986SSebastian Grimberg   CeedCallCuda(ceed, cudaMalloc((void **)&diag->d_eval_modes_in, num_eval_modes_in * eval_modes_bytes));
978004e4986SSebastian Grimberg   CeedCallCuda(ceed, cudaMemcpy(diag->d_eval_modes_in, eval_modes_in, num_eval_modes_in * eval_modes_bytes, cudaMemcpyHostToDevice));
979004e4986SSebastian Grimberg   CeedCallCuda(ceed, cudaMalloc((void **)&diag->d_eval_modes_out, num_eval_modes_out * eval_modes_bytes));
980004e4986SSebastian Grimberg   CeedCallCuda(ceed, cudaMemcpy(diag->d_eval_modes_out, eval_modes_out, num_eval_modes_out * eval_modes_bytes, cudaMemcpyHostToDevice));
981004e4986SSebastian Grimberg   CeedCallBackend(CeedFree(&eval_modes_in));
982004e4986SSebastian Grimberg   CeedCallBackend(CeedFree(&eval_modes_out));
9830d0321e0SJeremy L Thompson   return CEED_ERROR_SUCCESS;
9840d0321e0SJeremy L Thompson }
9850d0321e0SJeremy L Thompson 
9860d0321e0SJeremy L Thompson //------------------------------------------------------------------------------
987cbfe683aSSebastian Grimberg // Assemble Diagonal Setup (Compilation)
988cbfe683aSSebastian Grimberg //------------------------------------------------------------------------------
989cbfe683aSSebastian Grimberg static inline int CeedOperatorAssembleDiagonalSetupCompile_Cuda(CeedOperator op, CeedInt use_ceedsize_idx, const bool is_point_block) {
990cbfe683aSSebastian Grimberg   Ceed                ceed;
99122070f95SJeremy L Thompson   char               *diagonal_kernel_source;
99222070f95SJeremy L Thompson   const char         *diagonal_kernel_path;
993cbfe683aSSebastian Grimberg   CeedInt             num_input_fields, num_output_fields, num_eval_modes_in = 0, num_eval_modes_out = 0;
994cbfe683aSSebastian Grimberg   CeedInt             num_comp, q_comp, num_nodes, num_qpts;
995cbfe683aSSebastian Grimberg   CeedBasis           basis_in = NULL, basis_out = NULL;
996cbfe683aSSebastian Grimberg   CeedQFunctionField *qf_fields;
997cbfe683aSSebastian Grimberg   CeedQFunction       qf;
998cbfe683aSSebastian Grimberg   CeedOperatorField  *op_fields;
999cbfe683aSSebastian Grimberg   CeedOperator_Cuda  *impl;
1000cbfe683aSSebastian Grimberg 
1001cbfe683aSSebastian Grimberg   CeedCallBackend(CeedOperatorGetCeed(op, &ceed));
1002cbfe683aSSebastian Grimberg   CeedCallBackend(CeedOperatorGetQFunction(op, &qf));
1003cbfe683aSSebastian Grimberg   CeedCallBackend(CeedQFunctionGetNumArgs(qf, &num_input_fields, &num_output_fields));
1004cbfe683aSSebastian Grimberg 
1005cbfe683aSSebastian Grimberg   // Determine active input basis
1006cbfe683aSSebastian Grimberg   CeedCallBackend(CeedOperatorGetFields(op, NULL, &op_fields, NULL, NULL));
1007cbfe683aSSebastian Grimberg   CeedCallBackend(CeedQFunctionGetFields(qf, NULL, &qf_fields, NULL, NULL));
1008cbfe683aSSebastian Grimberg   for (CeedInt i = 0; i < num_input_fields; i++) {
1009cbfe683aSSebastian Grimberg     CeedVector vec;
1010cbfe683aSSebastian Grimberg 
1011cbfe683aSSebastian Grimberg     CeedCallBackend(CeedOperatorFieldGetVector(op_fields[i], &vec));
1012cbfe683aSSebastian Grimberg     if (vec == CEED_VECTOR_ACTIVE) {
1013cbfe683aSSebastian Grimberg       CeedEvalMode eval_mode;
1014cbfe683aSSebastian Grimberg 
1015cbfe683aSSebastian Grimberg       CeedCallBackend(CeedOperatorFieldGetBasis(op_fields[i], &basis_in));
1016cbfe683aSSebastian Grimberg       CeedCallBackend(CeedQFunctionFieldGetEvalMode(qf_fields[i], &eval_mode));
1017cbfe683aSSebastian Grimberg       CeedCallBackend(CeedBasisGetNumQuadratureComponents(basis_in, eval_mode, &q_comp));
1018cbfe683aSSebastian Grimberg       if (eval_mode != CEED_EVAL_WEIGHT) {
1019cbfe683aSSebastian Grimberg         num_eval_modes_in += q_comp;
1020cbfe683aSSebastian Grimberg       }
1021cbfe683aSSebastian Grimberg     }
1022cbfe683aSSebastian Grimberg   }
1023cbfe683aSSebastian Grimberg 
1024cbfe683aSSebastian Grimberg   // Determine active output basis
1025cbfe683aSSebastian Grimberg   CeedCallBackend(CeedOperatorGetFields(op, NULL, NULL, NULL, &op_fields));
1026cbfe683aSSebastian Grimberg   CeedCallBackend(CeedQFunctionGetFields(qf, NULL, NULL, NULL, &qf_fields));
1027cbfe683aSSebastian Grimberg   for (CeedInt i = 0; i < num_output_fields; i++) {
1028cbfe683aSSebastian Grimberg     CeedVector vec;
1029cbfe683aSSebastian Grimberg 
1030cbfe683aSSebastian Grimberg     CeedCallBackend(CeedOperatorFieldGetVector(op_fields[i], &vec));
1031cbfe683aSSebastian Grimberg     if (vec == CEED_VECTOR_ACTIVE) {
1032cbfe683aSSebastian Grimberg       CeedEvalMode eval_mode;
1033cbfe683aSSebastian Grimberg 
1034cbfe683aSSebastian Grimberg       CeedCallBackend(CeedOperatorFieldGetBasis(op_fields[i], &basis_out));
1035cbfe683aSSebastian Grimberg       CeedCallBackend(CeedQFunctionFieldGetEvalMode(qf_fields[i], &eval_mode));
1036cbfe683aSSebastian Grimberg       CeedCallBackend(CeedBasisGetNumQuadratureComponents(basis_out, eval_mode, &q_comp));
1037cbfe683aSSebastian Grimberg       if (eval_mode != CEED_EVAL_WEIGHT) {
1038cbfe683aSSebastian Grimberg         num_eval_modes_out += q_comp;
1039cbfe683aSSebastian Grimberg       }
1040cbfe683aSSebastian Grimberg     }
1041cbfe683aSSebastian Grimberg   }
1042cbfe683aSSebastian Grimberg 
1043cbfe683aSSebastian Grimberg   // Operator data struct
1044cbfe683aSSebastian Grimberg   CeedCallBackend(CeedOperatorGetData(op, &impl));
1045cbfe683aSSebastian Grimberg   CeedOperatorDiag_Cuda *diag = impl->diag;
1046cbfe683aSSebastian Grimberg 
1047cbfe683aSSebastian Grimberg   // Assemble kernel
1048cbfe683aSSebastian Grimberg   CUmodule *module          = is_point_block ? &diag->module_point_block : &diag->module;
1049cbfe683aSSebastian Grimberg   CeedInt   elems_per_block = 1;
1050cbfe683aSSebastian Grimberg   CeedCallBackend(CeedBasisGetNumNodes(basis_in, &num_nodes));
1051cbfe683aSSebastian Grimberg   CeedCallBackend(CeedBasisGetNumComponents(basis_in, &num_comp));
1052cbfe683aSSebastian Grimberg   if (basis_in == CEED_BASIS_NONE) num_qpts = num_nodes;
1053cbfe683aSSebastian Grimberg   else CeedCallBackend(CeedBasisGetNumQuadraturePoints(basis_in, &num_qpts));
1054cbfe683aSSebastian Grimberg   CeedCallBackend(CeedGetJitAbsolutePath(ceed, "ceed/jit-source/cuda/cuda-ref-operator-assemble-diagonal.h", &diagonal_kernel_path));
1055cbfe683aSSebastian Grimberg   CeedDebug256(ceed, CEED_DEBUG_COLOR_SUCCESS, "----- Loading Diagonal Assembly Kernel Source -----\n");
1056cbfe683aSSebastian Grimberg   CeedCallBackend(CeedLoadSourceToBuffer(ceed, diagonal_kernel_path, &diagonal_kernel_source));
1057cbfe683aSSebastian Grimberg   CeedDebug256(ceed, CEED_DEBUG_COLOR_SUCCESS, "----- Loading Diagonal Assembly Source Complete! -----\n");
1058cbfe683aSSebastian Grimberg   CeedCallCuda(ceed, CeedCompile_Cuda(ceed, diagonal_kernel_source, module, 8, "NUM_EVAL_MODES_IN", num_eval_modes_in, "NUM_EVAL_MODES_OUT",
1059cbfe683aSSebastian Grimberg                                       num_eval_modes_out, "NUM_COMP", num_comp, "NUM_NODES", num_nodes, "NUM_QPTS", num_qpts, "USE_CEEDSIZE",
1060cbfe683aSSebastian Grimberg                                       use_ceedsize_idx, "USE_POINT_BLOCK", is_point_block ? 1 : 0, "BLOCK_SIZE", num_nodes * elems_per_block));
1061cbfe683aSSebastian Grimberg   CeedCallCuda(ceed, CeedGetKernel_Cuda(ceed, *module, "LinearDiagonal", is_point_block ? &diag->LinearPointBlock : &diag->LinearDiagonal));
1062cbfe683aSSebastian Grimberg   CeedCallBackend(CeedFree(&diagonal_kernel_path));
1063cbfe683aSSebastian Grimberg   CeedCallBackend(CeedFree(&diagonal_kernel_source));
1064cbfe683aSSebastian Grimberg   return CEED_ERROR_SUCCESS;
1065cbfe683aSSebastian Grimberg }
1066cbfe683aSSebastian Grimberg 
1067cbfe683aSSebastian Grimberg //------------------------------------------------------------------------------
1068004e4986SSebastian Grimberg // Assemble Diagonal Core
10690d0321e0SJeremy L Thompson //------------------------------------------------------------------------------
1070ca735530SJeremy L Thompson static inline int CeedOperatorAssembleDiagonalCore_Cuda(CeedOperator op, CeedVector assembled, CeedRequest *request, const bool is_point_block) {
10710d0321e0SJeremy L Thompson   Ceed                ceed;
1072cbfe683aSSebastian Grimberg   CeedInt             num_elem, num_nodes;
1073ca735530SJeremy L Thompson   CeedScalar         *elem_diag_array;
1074ca735530SJeremy L Thompson   const CeedScalar   *assembled_qf_array;
1075004e4986SSebastian Grimberg   CeedVector          assembled_qf   = NULL, elem_diag;
1076004e4986SSebastian Grimberg   CeedElemRestriction assembled_rstr = NULL, rstr_in, rstr_out, diag_rstr;
10770d0321e0SJeremy L Thompson   CeedOperator_Cuda  *impl;
1078ca735530SJeremy L Thompson 
1079ca735530SJeremy L Thompson   CeedCallBackend(CeedOperatorGetCeed(op, &ceed));
10802b730f8bSJeremy L Thompson   CeedCallBackend(CeedOperatorGetData(op, &impl));
10810d0321e0SJeremy L Thompson 
10820d0321e0SJeremy L Thompson   // Assemble QFunction
1083004e4986SSebastian Grimberg   CeedCallBackend(CeedOperatorLinearAssembleQFunctionBuildOrUpdate(op, &assembled_qf, &assembled_rstr, request));
1084004e4986SSebastian Grimberg   CeedCallBackend(CeedElemRestrictionDestroy(&assembled_rstr));
1085004e4986SSebastian Grimberg   CeedCallBackend(CeedVectorGetArrayRead(assembled_qf, CEED_MEM_DEVICE, &assembled_qf_array));
10860d0321e0SJeremy L Thompson 
1087cbfe683aSSebastian Grimberg   // Setup
1088cbfe683aSSebastian Grimberg   if (!impl->diag) CeedCallBackend(CeedOperatorAssembleDiagonalSetup_Cuda(op));
1089cbfe683aSSebastian Grimberg   CeedOperatorDiag_Cuda *diag = impl->diag;
1090cbfe683aSSebastian Grimberg 
1091cbfe683aSSebastian Grimberg   assert(diag != NULL);
1092cbfe683aSSebastian Grimberg 
1093cbfe683aSSebastian Grimberg   // Assemble kernel if needed
1094cbfe683aSSebastian Grimberg   if ((!is_point_block && !diag->LinearDiagonal) || (is_point_block && !diag->LinearPointBlock)) {
1095cbfe683aSSebastian Grimberg     CeedSize assembled_length, assembled_qf_length;
1096cbfe683aSSebastian Grimberg     CeedInt  use_ceedsize_idx = 0;
1097f7c1b517Snbeams     CeedCallBackend(CeedVectorGetLength(assembled, &assembled_length));
1098ca735530SJeremy L Thompson     CeedCallBackend(CeedVectorGetLength(assembled_qf, &assembled_qf_length));
1099ca735530SJeremy L Thompson     if ((assembled_length > INT_MAX) || (assembled_qf_length > INT_MAX)) use_ceedsize_idx = 1;
1100f7c1b517Snbeams 
1101cbfe683aSSebastian Grimberg     CeedCallBackend(CeedOperatorAssembleDiagonalSetupCompile_Cuda(op, use_ceedsize_idx, is_point_block));
1102cbfe683aSSebastian Grimberg   }
11030d0321e0SJeremy L Thompson 
1104004e4986SSebastian Grimberg   // Restriction and diagonal vector
1105004e4986SSebastian Grimberg   CeedCallBackend(CeedOperatorGetActiveElemRestrictions(op, &rstr_in, &rstr_out));
1106004e4986SSebastian Grimberg   CeedCheck(rstr_in == rstr_out, ceed, CEED_ERROR_BACKEND,
1107004e4986SSebastian Grimberg             "Cannot assemble operator diagonal with different input and output active element restrictions");
1108004e4986SSebastian Grimberg   if (!is_point_block && !diag->diag_rstr) {
1109004e4986SSebastian Grimberg     CeedCallBackend(CeedElemRestrictionCreateUnsignedCopy(rstr_out, &diag->diag_rstr));
1110004e4986SSebastian Grimberg     CeedCallBackend(CeedElemRestrictionCreateVector(diag->diag_rstr, NULL, &diag->elem_diag));
1111004e4986SSebastian Grimberg   } else if (is_point_block && !diag->point_block_diag_rstr) {
1112004e4986SSebastian Grimberg     CeedCallBackend(CeedOperatorCreateActivePointBlockRestriction(rstr_out, &diag->point_block_diag_rstr));
1113004e4986SSebastian Grimberg     CeedCallBackend(CeedElemRestrictionCreateVector(diag->point_block_diag_rstr, NULL, &diag->point_block_elem_diag));
11140d0321e0SJeremy L Thompson   }
1115004e4986SSebastian Grimberg   diag_rstr = is_point_block ? diag->point_block_diag_rstr : diag->diag_rstr;
1116004e4986SSebastian Grimberg   elem_diag = is_point_block ? diag->point_block_elem_diag : diag->elem_diag;
1117ca735530SJeremy L Thompson   CeedCallBackend(CeedVectorSetValue(elem_diag, 0.0));
11180d0321e0SJeremy L Thompson 
111991db28b6SZach Atkins   // Only assemble diagonal if the basis has nodes, otherwise inputs are null pointers
1120004e4986SSebastian Grimberg   CeedCallBackend(CeedElemRestrictionGetElementSize(diag_rstr, &num_nodes));
1121004e4986SSebastian Grimberg   if (num_nodes > 0) {
11220d0321e0SJeremy L Thompson     // Assemble element operator diagonals
1123ca735530SJeremy L Thompson     CeedCallBackend(CeedElemRestrictionGetNumElements(diag_rstr, &num_elem));
1124004e4986SSebastian Grimberg     CeedCallBackend(CeedVectorGetArray(elem_diag, CEED_MEM_DEVICE, &elem_diag_array));
11250d0321e0SJeremy L Thompson 
11260d0321e0SJeremy L Thompson     // Compute the diagonal of B^T D B
1127004e4986SSebastian Grimberg     CeedInt elems_per_block = 1;
1128004e4986SSebastian Grimberg     CeedInt grid            = CeedDivUpInt(num_elem, elems_per_block);
1129004e4986SSebastian Grimberg     void   *args[]          = {(void *)&num_elem,      &diag->d_identity,       &diag->d_interp_in,  &diag->d_grad_in, &diag->d_div_in,
1130004e4986SSebastian Grimberg                                &diag->d_curl_in,       &diag->d_interp_out,     &diag->d_grad_out,   &diag->d_div_out, &diag->d_curl_out,
1131004e4986SSebastian Grimberg                                &diag->d_eval_modes_in, &diag->d_eval_modes_out, &assembled_qf_array, &elem_diag_array};
1132004e4986SSebastian Grimberg 
1133ca735530SJeremy L Thompson     if (is_point_block) {
1134004e4986SSebastian Grimberg       CeedCallBackend(CeedRunKernelDim_Cuda(ceed, diag->LinearPointBlock, grid, num_nodes, 1, elems_per_block, args));
11350d0321e0SJeremy L Thompson     } else {
1136004e4986SSebastian Grimberg       CeedCallBackend(CeedRunKernelDim_Cuda(ceed, diag->LinearDiagonal, grid, num_nodes, 1, elems_per_block, args));
11370d0321e0SJeremy L Thompson     }
11380d0321e0SJeremy L Thompson 
11390d0321e0SJeremy L Thompson     // Restore arrays
1140ca735530SJeremy L Thompson     CeedCallBackend(CeedVectorRestoreArray(elem_diag, &elem_diag_array));
1141ca735530SJeremy L Thompson     CeedCallBackend(CeedVectorRestoreArrayRead(assembled_qf, &assembled_qf_array));
114291db28b6SZach Atkins   }
11430d0321e0SJeremy L Thompson 
11440d0321e0SJeremy L Thompson   // Assemble local operator diagonal
1145ca735530SJeremy L Thompson   CeedCallBackend(CeedElemRestrictionApply(diag_rstr, CEED_TRANSPOSE, elem_diag, assembled, request));
11460d0321e0SJeremy L Thompson 
11470d0321e0SJeremy L Thompson   // Cleanup
1148ca735530SJeremy L Thompson   CeedCallBackend(CeedVectorDestroy(&assembled_qf));
11490d0321e0SJeremy L Thompson   return CEED_ERROR_SUCCESS;
11500d0321e0SJeremy L Thompson }
11510d0321e0SJeremy L Thompson 
11520d0321e0SJeremy L Thompson //------------------------------------------------------------------------------
11530d0321e0SJeremy L Thompson // Assemble Linear Diagonal
11540d0321e0SJeremy L Thompson //------------------------------------------------------------------------------
11552b730f8bSJeremy L Thompson static int CeedOperatorLinearAssembleAddDiagonal_Cuda(CeedOperator op, CeedVector assembled, CeedRequest *request) {
11562b730f8bSJeremy L Thompson   CeedCallBackend(CeedOperatorAssembleDiagonalCore_Cuda(op, assembled, request, false));
11576aa95790SJeremy L Thompson   return CEED_ERROR_SUCCESS;
11580d0321e0SJeremy L Thompson }
11590d0321e0SJeremy L Thompson 
11600d0321e0SJeremy L Thompson //------------------------------------------------------------------------------
11610d0321e0SJeremy L Thompson // Assemble Linear Point Block Diagonal
11620d0321e0SJeremy L Thompson //------------------------------------------------------------------------------
11632b730f8bSJeremy L Thompson static int CeedOperatorLinearAssembleAddPointBlockDiagonal_Cuda(CeedOperator op, CeedVector assembled, CeedRequest *request) {
11642b730f8bSJeremy L Thompson   CeedCallBackend(CeedOperatorAssembleDiagonalCore_Cuda(op, assembled, request, true));
11656aa95790SJeremy L Thompson   return CEED_ERROR_SUCCESS;
11660d0321e0SJeremy L Thompson }
11670d0321e0SJeremy L Thompson 
11680d0321e0SJeremy L Thompson //------------------------------------------------------------------------------
1169004e4986SSebastian Grimberg // Single Operator Assembly Setup
1170cc132f9aSnbeams //------------------------------------------------------------------------------
1171f7c1b517Snbeams static int CeedSingleOperatorAssembleSetup_Cuda(CeedOperator op, CeedInt use_ceedsize_idx) {
1172cc132f9aSnbeams   Ceed                ceed;
1173004e4986SSebastian Grimberg   Ceed_Cuda          *cuda_data;
117422070f95SJeremy L Thompson   char               *assembly_kernel_source;
117522070f95SJeremy L Thompson   const char         *assembly_kernel_path;
1176004e4986SSebastian Grimberg   CeedInt             num_input_fields, num_output_fields, num_eval_modes_in = 0, num_eval_modes_out = 0;
11773b38d1dfSJeremy L Thompson   CeedInt             elem_size_in, num_qpts_in = 0, num_comp_in, elem_size_out, num_qpts_out, num_comp_out, q_comp;
1178004e4986SSebastian Grimberg   CeedEvalMode       *eval_modes_in = NULL, *eval_modes_out = NULL;
1179ca735530SJeremy L Thompson   CeedElemRestriction rstr_in = NULL, rstr_out = NULL;
1180ca735530SJeremy L Thompson   CeedBasis           basis_in = NULL, basis_out = NULL;
1181ca735530SJeremy L Thompson   CeedQFunctionField *qf_fields;
1182ca735530SJeremy L Thompson   CeedQFunction       qf;
1183ca735530SJeremy L Thompson   CeedOperatorField  *input_fields, *output_fields;
1184cc132f9aSnbeams   CeedOperator_Cuda  *impl;
1185ca735530SJeremy L Thompson 
1186ca735530SJeremy L Thompson   CeedCallBackend(CeedOperatorGetCeed(op, &ceed));
11872b730f8bSJeremy L Thompson   CeedCallBackend(CeedOperatorGetData(op, &impl));
1188cc132f9aSnbeams 
1189cc132f9aSnbeams   // Get intput and output fields
11902b730f8bSJeremy L Thompson   CeedCallBackend(CeedOperatorGetFields(op, &num_input_fields, &input_fields, &num_output_fields, &output_fields));
1191cc132f9aSnbeams 
1192cc132f9aSnbeams   // Determine active input basis eval mode
11932b730f8bSJeremy L Thompson   CeedCallBackend(CeedOperatorGetQFunction(op, &qf));
11942b730f8bSJeremy L Thompson   CeedCallBackend(CeedQFunctionGetFields(qf, NULL, &qf_fields, NULL, NULL));
1195cc132f9aSnbeams   for (CeedInt i = 0; i < num_input_fields; i++) {
1196cc132f9aSnbeams     CeedVector vec;
1197ca735530SJeremy L Thompson 
11982b730f8bSJeremy L Thompson     CeedCallBackend(CeedOperatorFieldGetVector(input_fields[i], &vec));
1199cc132f9aSnbeams     if (vec == CEED_VECTOR_ACTIVE) {
1200004e4986SSebastian Grimberg       CeedBasis    basis;
1201ca735530SJeremy L Thompson       CeedEvalMode eval_mode;
1202ca735530SJeremy L Thompson 
1203004e4986SSebastian Grimberg       CeedCallBackend(CeedOperatorFieldGetBasis(input_fields[i], &basis));
1204004e4986SSebastian Grimberg       CeedCheck(!basis_in || basis_in == basis, ceed, CEED_ERROR_BACKEND, "Backend does not implement operator assembly with multiple active bases");
1205004e4986SSebastian Grimberg       basis_in = basis;
12062b730f8bSJeremy L Thompson       CeedCallBackend(CeedOperatorFieldGetElemRestriction(input_fields[i], &rstr_in));
1207004e4986SSebastian Grimberg       CeedCallBackend(CeedElemRestrictionGetElementSize(rstr_in, &elem_size_in));
1208004e4986SSebastian Grimberg       if (basis_in == CEED_BASIS_NONE) num_qpts_in = elem_size_in;
1209004e4986SSebastian Grimberg       else CeedCallBackend(CeedBasisGetNumQuadraturePoints(basis_in, &num_qpts_in));
12102b730f8bSJeremy L Thompson       CeedCallBackend(CeedQFunctionFieldGetEvalMode(qf_fields[i], &eval_mode));
1211004e4986SSebastian Grimberg       CeedCallBackend(CeedBasisGetNumQuadratureComponents(basis_in, eval_mode, &q_comp));
1212004e4986SSebastian Grimberg       if (eval_mode != CEED_EVAL_WEIGHT) {
1213004e4986SSebastian Grimberg         // q_comp = 1 if CEED_EVAL_NONE, CEED_EVAL_WEIGHT caught by QF Assembly
1214004e4986SSebastian Grimberg         CeedCallBackend(CeedRealloc(num_eval_modes_in + q_comp, &eval_modes_in));
1215004e4986SSebastian Grimberg         for (CeedInt d = 0; d < q_comp; d++) {
1216004e4986SSebastian Grimberg           eval_modes_in[num_eval_modes_in + d] = eval_mode;
1217cc132f9aSnbeams         }
1218004e4986SSebastian Grimberg         num_eval_modes_in += q_comp;
1219cc132f9aSnbeams       }
1220cc132f9aSnbeams     }
1221cc132f9aSnbeams   }
1222cc132f9aSnbeams 
1223cc132f9aSnbeams   // Determine active output basis; basis_out and rstr_out only used if same as input, TODO
12242b730f8bSJeremy L Thompson   CeedCallBackend(CeedQFunctionGetFields(qf, NULL, NULL, NULL, &qf_fields));
1225cc132f9aSnbeams   for (CeedInt i = 0; i < num_output_fields; i++) {
1226cc132f9aSnbeams     CeedVector vec;
1227ca735530SJeremy L Thompson 
12282b730f8bSJeremy L Thompson     CeedCallBackend(CeedOperatorFieldGetVector(output_fields[i], &vec));
1229cc132f9aSnbeams     if (vec == CEED_VECTOR_ACTIVE) {
1230004e4986SSebastian Grimberg       CeedBasis    basis;
1231ca735530SJeremy L Thompson       CeedEvalMode eval_mode;
1232ca735530SJeremy L Thompson 
1233004e4986SSebastian Grimberg       CeedCallBackend(CeedOperatorFieldGetBasis(output_fields[i], &basis));
1234004e4986SSebastian Grimberg       CeedCheck(!basis_out || basis_out == basis, ceed, CEED_ERROR_BACKEND,
1235004e4986SSebastian Grimberg                 "Backend does not implement operator assembly with multiple active bases");
1236004e4986SSebastian Grimberg       basis_out = basis;
12372b730f8bSJeremy L Thompson       CeedCallBackend(CeedOperatorFieldGetElemRestriction(output_fields[i], &rstr_out));
1238004e4986SSebastian Grimberg       CeedCallBackend(CeedElemRestrictionGetElementSize(rstr_out, &elem_size_out));
1239004e4986SSebastian Grimberg       if (basis_out == CEED_BASIS_NONE) num_qpts_out = elem_size_out;
1240004e4986SSebastian Grimberg       else CeedCallBackend(CeedBasisGetNumQuadraturePoints(basis_out, &num_qpts_out));
1241004e4986SSebastian Grimberg       CeedCheck(num_qpts_in == num_qpts_out, ceed, CEED_ERROR_UNSUPPORTED,
1242004e4986SSebastian Grimberg                 "Active input and output bases must have the same number of quadrature points");
12432b730f8bSJeremy L Thompson       CeedCallBackend(CeedQFunctionFieldGetEvalMode(qf_fields[i], &eval_mode));
1244004e4986SSebastian Grimberg       CeedCallBackend(CeedBasisGetNumQuadratureComponents(basis_out, eval_mode, &q_comp));
1245004e4986SSebastian Grimberg       if (eval_mode != CEED_EVAL_WEIGHT) {
1246004e4986SSebastian Grimberg         // q_comp = 1 if CEED_EVAL_NONE, CEED_EVAL_WEIGHT caught by QF Assembly
1247004e4986SSebastian Grimberg         CeedCallBackend(CeedRealloc(num_eval_modes_out + q_comp, &eval_modes_out));
1248004e4986SSebastian Grimberg         for (CeedInt d = 0; d < q_comp; d++) {
1249004e4986SSebastian Grimberg           eval_modes_out[num_eval_modes_out + d] = eval_mode;
1250004e4986SSebastian Grimberg         }
1251004e4986SSebastian Grimberg         num_eval_modes_out += q_comp;
1252cc132f9aSnbeams       }
1253cc132f9aSnbeams     }
1254cc132f9aSnbeams   }
1255004e4986SSebastian Grimberg   CeedCheck(num_eval_modes_in > 0 && num_eval_modes_out > 0, ceed, CEED_ERROR_UNSUPPORTED, "Cannot assemble operator without inputs/outputs");
1256cc132f9aSnbeams 
12572b730f8bSJeremy L Thompson   CeedCallBackend(CeedCalloc(1, &impl->asmb));
1258cc132f9aSnbeams   CeedOperatorAssemble_Cuda *asmb = impl->asmb;
1259004e4986SSebastian Grimberg   asmb->elems_per_block           = 1;
1260004e4986SSebastian Grimberg   asmb->block_size_x              = elem_size_in;
1261004e4986SSebastian Grimberg   asmb->block_size_y              = elem_size_out;
1262ca735530SJeremy L Thompson 
12632b730f8bSJeremy L Thompson   CeedCallBackend(CeedGetData(ceed, &cuda_data));
1264004e4986SSebastian Grimberg   bool fallback = asmb->block_size_x * asmb->block_size_y * asmb->elems_per_block > cuda_data->device_prop.maxThreadsPerBlock;
1265004e4986SSebastian Grimberg 
1266004e4986SSebastian Grimberg   if (fallback) {
1267004e4986SSebastian Grimberg     // Use fallback kernel with 1D threadblock
1268004e4986SSebastian Grimberg     asmb->block_size_y = 1;
1269004e4986SSebastian Grimberg   }
1270004e4986SSebastian Grimberg 
1271004e4986SSebastian Grimberg   // Compile kernels
1272004e4986SSebastian Grimberg   CeedCallBackend(CeedElemRestrictionGetNumComponents(rstr_in, &num_comp_in));
1273004e4986SSebastian Grimberg   CeedCallBackend(CeedElemRestrictionGetNumComponents(rstr_out, &num_comp_out));
12742b730f8bSJeremy L Thompson   CeedCallBackend(CeedGetJitAbsolutePath(ceed, "ceed/jit-source/cuda/cuda-ref-operator-assemble.h", &assembly_kernel_path));
127523d4529eSJeremy L Thompson   CeedDebug256(ceed, CEED_DEBUG_COLOR_SUCCESS, "----- Loading Assembly Kernel Source -----\n");
12762b730f8bSJeremy L Thompson   CeedCallBackend(CeedLoadSourceToBuffer(ceed, assembly_kernel_path, &assembly_kernel_source));
127723d4529eSJeremy L Thompson   CeedDebug256(ceed, CEED_DEBUG_COLOR_SUCCESS, "----- Loading Assembly Source Complete! -----\n");
1278004e4986SSebastian Grimberg   CeedCallBackend(CeedCompile_Cuda(ceed, assembly_kernel_source, &asmb->module, 10, "NUM_EVAL_MODES_IN", num_eval_modes_in, "NUM_EVAL_MODES_OUT",
1279004e4986SSebastian Grimberg                                    num_eval_modes_out, "NUM_COMP_IN", num_comp_in, "NUM_COMP_OUT", num_comp_out, "NUM_NODES_IN", elem_size_in,
1280004e4986SSebastian Grimberg                                    "NUM_NODES_OUT", elem_size_out, "NUM_QPTS", num_qpts_in, "BLOCK_SIZE",
1281cbfe683aSSebastian Grimberg                                    asmb->block_size_x * asmb->block_size_y * asmb->elems_per_block, "BLOCK_SIZE_Y", asmb->block_size_y,
1282cbfe683aSSebastian Grimberg                                    "USE_CEEDSIZE", use_ceedsize_idx));
1283004e4986SSebastian Grimberg   CeedCallBackend(CeedGetKernel_Cuda(ceed, asmb->module, "LinearAssemble", &asmb->LinearAssemble));
12842b730f8bSJeremy L Thompson   CeedCallBackend(CeedFree(&assembly_kernel_path));
12852b730f8bSJeremy L Thompson   CeedCallBackend(CeedFree(&assembly_kernel_source));
1286cc132f9aSnbeams 
1287004e4986SSebastian Grimberg   // Load into B_in, in order that they will be used in eval_modes_in
1288004e4986SSebastian Grimberg   {
1289004e4986SSebastian Grimberg     const CeedInt in_bytes           = elem_size_in * num_qpts_in * num_eval_modes_in * sizeof(CeedScalar);
1290004e4986SSebastian Grimberg     CeedInt       d_in               = 0;
1291004e4986SSebastian Grimberg     CeedEvalMode  eval_modes_in_prev = CEED_EVAL_NONE;
1292004e4986SSebastian Grimberg     bool          has_eval_none      = false;
1293004e4986SSebastian Grimberg     CeedScalar   *identity           = NULL;
1294ca735530SJeremy L Thompson 
1295004e4986SSebastian Grimberg     for (CeedInt i = 0; i < num_eval_modes_in; i++) {
1296004e4986SSebastian Grimberg       has_eval_none = has_eval_none || (eval_modes_in[i] == CEED_EVAL_NONE);
1297004e4986SSebastian Grimberg     }
1298004e4986SSebastian Grimberg     if (has_eval_none) {
1299004e4986SSebastian Grimberg       CeedCallBackend(CeedCalloc(elem_size_in * num_qpts_in, &identity));
1300004e4986SSebastian 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;
1301004e4986SSebastian Grimberg     }
1302cc132f9aSnbeams 
1303004e4986SSebastian Grimberg     CeedCallCuda(ceed, cudaMalloc((void **)&asmb->d_B_in, in_bytes));
1304004e4986SSebastian Grimberg     for (CeedInt i = 0; i < num_eval_modes_in; i++) {
1305004e4986SSebastian Grimberg       const CeedScalar *h_B_in;
1306ca735530SJeremy L Thompson 
1307004e4986SSebastian Grimberg       CeedCallBackend(CeedOperatorGetBasisPointer(basis_in, eval_modes_in[i], identity, &h_B_in));
1308004e4986SSebastian Grimberg       CeedCallBackend(CeedBasisGetNumQuadratureComponents(basis_in, eval_modes_in[i], &q_comp));
1309004e4986SSebastian Grimberg       if (q_comp > 1) {
1310004e4986SSebastian Grimberg         if (i == 0 || eval_modes_in[i] != eval_modes_in_prev) d_in = 0;
1311004e4986SSebastian Grimberg         else h_B_in = &h_B_in[(++d_in) * elem_size_in * num_qpts_in];
1312004e4986SSebastian Grimberg       }
1313004e4986SSebastian Grimberg       eval_modes_in_prev = eval_modes_in[i];
1314ca735530SJeremy L Thompson 
1315004e4986SSebastian 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),
1316004e4986SSebastian Grimberg                                     cudaMemcpyHostToDevice));
1317004e4986SSebastian Grimberg     }
1318004e4986SSebastian Grimberg 
1319004e4986SSebastian Grimberg     if (identity) {
1320004e4986SSebastian Grimberg       CeedCallBackend(CeedFree(&identity));
1321cc132f9aSnbeams     }
1322cc132f9aSnbeams   }
1323cc132f9aSnbeams 
1324004e4986SSebastian Grimberg   // Load into B_out, in order that they will be used in eval_modes_out
1325004e4986SSebastian Grimberg   {
1326004e4986SSebastian Grimberg     const CeedInt out_bytes           = elem_size_out * num_qpts_out * num_eval_modes_out * sizeof(CeedScalar);
1327004e4986SSebastian Grimberg     CeedInt       d_out               = 0;
1328004e4986SSebastian Grimberg     CeedEvalMode  eval_modes_out_prev = CEED_EVAL_NONE;
1329004e4986SSebastian Grimberg     bool          has_eval_none       = false;
1330004e4986SSebastian Grimberg     CeedScalar   *identity            = NULL;
1331ca735530SJeremy L Thompson 
1332004e4986SSebastian Grimberg     for (CeedInt i = 0; i < num_eval_modes_out; i++) {
1333004e4986SSebastian Grimberg       has_eval_none = has_eval_none || (eval_modes_out[i] == CEED_EVAL_NONE);
1334004e4986SSebastian Grimberg     }
1335004e4986SSebastian Grimberg     if (has_eval_none) {
1336004e4986SSebastian Grimberg       CeedCallBackend(CeedCalloc(elem_size_out * num_qpts_out, &identity));
1337004e4986SSebastian 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;
1338cc132f9aSnbeams     }
1339cc132f9aSnbeams 
1340004e4986SSebastian Grimberg     CeedCallCuda(ceed, cudaMalloc((void **)&asmb->d_B_out, out_bytes));
1341004e4986SSebastian Grimberg     for (CeedInt i = 0; i < num_eval_modes_out; i++) {
1342004e4986SSebastian Grimberg       const CeedScalar *h_B_out;
1343ca735530SJeremy L Thompson 
1344004e4986SSebastian Grimberg       CeedCallBackend(CeedOperatorGetBasisPointer(basis_out, eval_modes_out[i], identity, &h_B_out));
1345004e4986SSebastian Grimberg       CeedCallBackend(CeedBasisGetNumQuadratureComponents(basis_out, eval_modes_out[i], &q_comp));
1346004e4986SSebastian Grimberg       if (q_comp > 1) {
1347004e4986SSebastian Grimberg         if (i == 0 || eval_modes_out[i] != eval_modes_out_prev) d_out = 0;
1348004e4986SSebastian Grimberg         else h_B_out = &h_B_out[(++d_out) * elem_size_out * num_qpts_out];
1349004e4986SSebastian Grimberg       }
1350004e4986SSebastian Grimberg       eval_modes_out_prev = eval_modes_out[i];
1351ca735530SJeremy L Thompson 
1352004e4986SSebastian 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),
1353004e4986SSebastian Grimberg                                     cudaMemcpyHostToDevice));
1354004e4986SSebastian Grimberg     }
1355004e4986SSebastian Grimberg 
1356004e4986SSebastian Grimberg     if (identity) {
1357004e4986SSebastian Grimberg       CeedCallBackend(CeedFree(&identity));
1358cc132f9aSnbeams     }
1359cc132f9aSnbeams   }
1360cc132f9aSnbeams   return CEED_ERROR_SUCCESS;
1361cc132f9aSnbeams }
1362cc132f9aSnbeams 
1363cc132f9aSnbeams //------------------------------------------------------------------------------
1364cefa2673SJeremy L Thompson // Assemble matrix data for COO matrix of assembled operator.
1365cefa2673SJeremy L Thompson // The sparsity pattern is set by CeedOperatorLinearAssembleSymbolic.
1366cefa2673SJeremy L Thompson //
1367ea61e9acSJeremy L Thompson // Note that this (and other assembly routines) currently assume only one active input restriction/basis per operator (could have multiple basis eval
1368cefa2673SJeremy L Thompson // modes).
1369cefa2673SJeremy L Thompson // TODO: allow multiple active input restrictions/basis objects
1370cc132f9aSnbeams //------------------------------------------------------------------------------
13712b730f8bSJeremy L Thompson static int CeedSingleOperatorAssemble_Cuda(CeedOperator op, CeedInt offset, CeedVector values) {
1372cc132f9aSnbeams   Ceed                ceed;
1373ca735530SJeremy L Thompson   CeedSize            values_length = 0, assembled_qf_length = 0;
1374004e4986SSebastian Grimberg   CeedInt             use_ceedsize_idx = 0, num_elem_in, num_elem_out, elem_size_in, elem_size_out;
1375ca735530SJeremy L Thompson   CeedScalar         *values_array;
1376004e4986SSebastian Grimberg   const CeedScalar   *assembled_qf_array;
1377ca735530SJeremy L Thompson   CeedVector          assembled_qf   = NULL;
1378004e4986SSebastian Grimberg   CeedElemRestriction assembled_rstr = NULL, rstr_in, rstr_out;
1379004e4986SSebastian Grimberg   CeedRestrictionType rstr_type_in, rstr_type_out;
1380004e4986SSebastian Grimberg   const bool         *orients_in = NULL, *orients_out = NULL;
1381004e4986SSebastian Grimberg   const CeedInt8     *curl_orients_in = NULL, *curl_orients_out = NULL;
1382cc132f9aSnbeams   CeedOperator_Cuda  *impl;
1383ca735530SJeremy L Thompson 
1384ca735530SJeremy L Thompson   CeedCallBackend(CeedOperatorGetCeed(op, &ceed));
13852b730f8bSJeremy L Thompson   CeedCallBackend(CeedOperatorGetData(op, &impl));
1386cc132f9aSnbeams 
1387cc132f9aSnbeams   // Assemble QFunction
1388004e4986SSebastian Grimberg   CeedCallBackend(CeedOperatorLinearAssembleQFunctionBuildOrUpdate(op, &assembled_qf, &assembled_rstr, CEED_REQUEST_IMMEDIATE));
1389004e4986SSebastian Grimberg   CeedCallBackend(CeedElemRestrictionDestroy(&assembled_rstr));
1390004e4986SSebastian Grimberg   CeedCallBackend(CeedVectorGetArrayRead(assembled_qf, CEED_MEM_DEVICE, &assembled_qf_array));
1391cc132f9aSnbeams 
1392f7c1b517Snbeams   CeedCallBackend(CeedVectorGetLength(values, &values_length));
1393f7c1b517Snbeams   CeedCallBackend(CeedVectorGetLength(assembled_qf, &assembled_qf_length));
1394f7c1b517Snbeams   if ((values_length > INT_MAX) || (assembled_qf_length > INT_MAX)) use_ceedsize_idx = 1;
1395004e4986SSebastian Grimberg 
1396f7c1b517Snbeams   // Setup
1397004e4986SSebastian Grimberg   if (!impl->asmb) CeedCallBackend(CeedSingleOperatorAssembleSetup_Cuda(op, use_ceedsize_idx));
1398004e4986SSebastian Grimberg   CeedOperatorAssemble_Cuda *asmb = impl->asmb;
1399004e4986SSebastian Grimberg 
1400004e4986SSebastian Grimberg   assert(asmb != NULL);
1401004e4986SSebastian Grimberg 
1402004e4986SSebastian Grimberg   // Assemble element operator
1403004e4986SSebastian Grimberg   CeedCallBackend(CeedVectorGetArray(values, CEED_MEM_DEVICE, &values_array));
1404004e4986SSebastian Grimberg   values_array += offset;
1405004e4986SSebastian Grimberg 
1406004e4986SSebastian Grimberg   CeedCallBackend(CeedOperatorGetActiveElemRestrictions(op, &rstr_in, &rstr_out));
1407004e4986SSebastian Grimberg   CeedCallBackend(CeedElemRestrictionGetNumElements(rstr_in, &num_elem_in));
1408004e4986SSebastian Grimberg   CeedCallBackend(CeedElemRestrictionGetElementSize(rstr_in, &elem_size_in));
1409004e4986SSebastian Grimberg 
1410004e4986SSebastian Grimberg   CeedCallBackend(CeedElemRestrictionGetType(rstr_in, &rstr_type_in));
1411004e4986SSebastian Grimberg   if (rstr_type_in == CEED_RESTRICTION_ORIENTED) {
1412004e4986SSebastian Grimberg     CeedCallBackend(CeedElemRestrictionGetOrientations(rstr_in, CEED_MEM_DEVICE, &orients_in));
1413004e4986SSebastian Grimberg   } else if (rstr_type_in == CEED_RESTRICTION_CURL_ORIENTED) {
1414004e4986SSebastian Grimberg     CeedCallBackend(CeedElemRestrictionGetCurlOrientations(rstr_in, CEED_MEM_DEVICE, &curl_orients_in));
1415004e4986SSebastian Grimberg   }
1416004e4986SSebastian Grimberg 
1417004e4986SSebastian Grimberg   if (rstr_in != rstr_out) {
1418004e4986SSebastian Grimberg     CeedCallBackend(CeedElemRestrictionGetNumElements(rstr_out, &num_elem_out));
1419004e4986SSebastian Grimberg     CeedCheck(num_elem_in == num_elem_out, ceed, CEED_ERROR_UNSUPPORTED,
1420004e4986SSebastian Grimberg               "Active input and output operator restrictions must have the same number of elements");
1421004e4986SSebastian Grimberg     CeedCallBackend(CeedElemRestrictionGetElementSize(rstr_out, &elem_size_out));
1422004e4986SSebastian Grimberg 
1423004e4986SSebastian Grimberg     CeedCallBackend(CeedElemRestrictionGetType(rstr_out, &rstr_type_out));
1424004e4986SSebastian Grimberg     if (rstr_type_out == CEED_RESTRICTION_ORIENTED) {
1425004e4986SSebastian Grimberg       CeedCallBackend(CeedElemRestrictionGetOrientations(rstr_out, CEED_MEM_DEVICE, &orients_out));
1426004e4986SSebastian Grimberg     } else if (rstr_type_out == CEED_RESTRICTION_CURL_ORIENTED) {
1427004e4986SSebastian Grimberg       CeedCallBackend(CeedElemRestrictionGetCurlOrientations(rstr_out, CEED_MEM_DEVICE, &curl_orients_out));
1428004e4986SSebastian Grimberg     }
1429004e4986SSebastian Grimberg   } else {
1430004e4986SSebastian Grimberg     elem_size_out    = elem_size_in;
1431004e4986SSebastian Grimberg     orients_out      = orients_in;
1432004e4986SSebastian Grimberg     curl_orients_out = curl_orients_in;
1433f7c1b517Snbeams   }
1434f7c1b517Snbeams 
1435cc132f9aSnbeams   // Compute B^T D B
1436004e4986SSebastian Grimberg   CeedInt shared_mem =
1437004e4986SSebastian 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)) *
1438004e4986SSebastian Grimberg       sizeof(CeedScalar);
1439004e4986SSebastian Grimberg   CeedInt grid   = CeedDivUpInt(num_elem_in, asmb->elems_per_block);
1440004e4986SSebastian Grimberg   void   *args[] = {(void *)&num_elem_in, &asmb->d_B_in,     &asmb->d_B_out,      &orients_in,  &curl_orients_in,
1441004e4986SSebastian Grimberg                     &orients_out,         &curl_orients_out, &assembled_qf_array, &values_array};
1442ca735530SJeremy L Thompson 
14432b730f8bSJeremy L Thompson   CeedCallBackend(
1444004e4986SSebastian Grimberg       CeedRunKernelDimShared_Cuda(ceed, asmb->LinearAssemble, grid, asmb->block_size_x, asmb->block_size_y, asmb->elems_per_block, shared_mem, args));
1445cc132f9aSnbeams 
1446cc132f9aSnbeams   // Restore arrays
14472b730f8bSJeremy L Thompson   CeedCallBackend(CeedVectorRestoreArray(values, &values_array));
1448004e4986SSebastian Grimberg   CeedCallBackend(CeedVectorRestoreArrayRead(assembled_qf, &assembled_qf_array));
1449cc132f9aSnbeams 
1450cc132f9aSnbeams   // Cleanup
14512b730f8bSJeremy L Thompson   CeedCallBackend(CeedVectorDestroy(&assembled_qf));
1452004e4986SSebastian Grimberg   if (rstr_type_in == CEED_RESTRICTION_ORIENTED) {
1453004e4986SSebastian Grimberg     CeedCallBackend(CeedElemRestrictionRestoreOrientations(rstr_in, &orients_in));
1454004e4986SSebastian Grimberg   } else if (rstr_type_in == CEED_RESTRICTION_CURL_ORIENTED) {
1455004e4986SSebastian Grimberg     CeedCallBackend(CeedElemRestrictionRestoreCurlOrientations(rstr_in, &curl_orients_in));
1456004e4986SSebastian Grimberg   }
1457004e4986SSebastian Grimberg   if (rstr_in != rstr_out) {
1458004e4986SSebastian Grimberg     if (rstr_type_out == CEED_RESTRICTION_ORIENTED) {
1459004e4986SSebastian Grimberg       CeedCallBackend(CeedElemRestrictionRestoreOrientations(rstr_out, &orients_out));
1460004e4986SSebastian Grimberg     } else if (rstr_type_out == CEED_RESTRICTION_CURL_ORIENTED) {
1461004e4986SSebastian Grimberg       CeedCallBackend(CeedElemRestrictionRestoreCurlOrientations(rstr_out, &curl_orients_out));
1462004e4986SSebastian Grimberg     }
1463004e4986SSebastian Grimberg   }
1464cc132f9aSnbeams   return CEED_ERROR_SUCCESS;
1465cc132f9aSnbeams }
1466cc132f9aSnbeams 
1467cc132f9aSnbeams //------------------------------------------------------------------------------
1468756ca9e9SJeremy L Thompson // Assemble Linear QFunction AtPoints
1469756ca9e9SJeremy L Thompson //------------------------------------------------------------------------------
1470756ca9e9SJeremy L Thompson static int CeedOperatorLinearAssembleQFunctionAtPoints_Cuda(CeedOperator op, CeedVector *assembled, CeedElemRestriction *rstr, CeedRequest *request) {
1471756ca9e9SJeremy L Thompson   return CeedError(CeedOperatorReturnCeed(op), CEED_ERROR_BACKEND, "Backend does not implement CeedOperatorLinearAssembleQFunction");
1472756ca9e9SJeremy L Thompson }
1473756ca9e9SJeremy L Thompson 
1474756ca9e9SJeremy L Thompson //------------------------------------------------------------------------------
1475756ca9e9SJeremy L Thompson // Assemble Linear Diagonal AtPoints
1476756ca9e9SJeremy L Thompson //------------------------------------------------------------------------------
1477756ca9e9SJeremy L Thompson static int CeedOperatorLinearAssembleAddDiagonalAtPoints_Cuda(CeedOperator op, CeedVector assembled, CeedRequest *request) {
1478382e9c83SJeremy L Thompson   CeedInt             max_num_points, num_elem, num_input_fields, num_output_fields;
1479349fb27dSJeremy L Thompson   CeedScalar         *e_data[2 * CEED_FIELD_MAX] = {NULL};
1480349fb27dSJeremy L Thompson   CeedQFunctionField *qf_input_fields, *qf_output_fields;
1481349fb27dSJeremy L Thompson   CeedQFunction       qf;
1482349fb27dSJeremy L Thompson   CeedOperatorField  *op_input_fields, *op_output_fields;
1483349fb27dSJeremy L Thompson   CeedOperator_Cuda  *impl;
1484349fb27dSJeremy L Thompson 
1485349fb27dSJeremy L Thompson   CeedCallBackend(CeedOperatorGetData(op, &impl));
1486349fb27dSJeremy L Thompson   CeedCallBackend(CeedOperatorGetQFunction(op, &qf));
1487349fb27dSJeremy L Thompson   CeedCallBackend(CeedOperatorGetNumElements(op, &num_elem));
1488349fb27dSJeremy L Thompson   CeedCallBackend(CeedOperatorGetFields(op, &num_input_fields, &op_input_fields, &num_output_fields, &op_output_fields));
1489349fb27dSJeremy L Thompson   CeedCallBackend(CeedQFunctionGetFields(qf, NULL, &qf_input_fields, NULL, &qf_output_fields));
1490349fb27dSJeremy L Thompson   CeedInt num_points[num_elem];
1491349fb27dSJeremy L Thompson 
1492349fb27dSJeremy L Thompson   // Setup
1493349fb27dSJeremy L Thompson   CeedCallBackend(CeedOperatorSetupAtPoints_Cuda(op));
1494349fb27dSJeremy L Thompson   max_num_points = impl->max_num_points;
1495349fb27dSJeremy L Thompson   for (CeedInt i = 0; i < num_elem; i++) num_points[i] = max_num_points;
1496349fb27dSJeremy L Thompson 
1497349fb27dSJeremy L Thompson   // Input Evecs and Restriction
1498349fb27dSJeremy L Thompson   CeedCallBackend(CeedOperatorSetupInputs_Cuda(num_input_fields, qf_input_fields, op_input_fields, NULL, true, e_data, impl, request));
1499349fb27dSJeremy L Thompson 
1500349fb27dSJeremy L Thompson   // Get point coordinates
1501349fb27dSJeremy L Thompson   if (!impl->point_coords_elem) {
1502349fb27dSJeremy L Thompson     CeedVector          point_coords = NULL;
1503349fb27dSJeremy L Thompson     CeedElemRestriction rstr_points  = NULL;
1504349fb27dSJeremy L Thompson 
1505349fb27dSJeremy L Thompson     CeedCallBackend(CeedOperatorAtPointsGetPoints(op, &rstr_points, &point_coords));
1506349fb27dSJeremy L Thompson     CeedCallBackend(CeedElemRestrictionCreateVector(rstr_points, NULL, &impl->point_coords_elem));
1507349fb27dSJeremy L Thompson     CeedCallBackend(CeedElemRestrictionApply(rstr_points, CEED_NOTRANSPOSE, point_coords, impl->point_coords_elem, request));
1508349fb27dSJeremy L Thompson   }
1509349fb27dSJeremy L Thompson 
1510382e9c83SJeremy L Thompson   // Clear active input Qvecs
1511382e9c83SJeremy L Thompson   for (CeedInt i = 0; i < num_input_fields; i++) {
1512382e9c83SJeremy L Thompson     CeedVector vec;
1513382e9c83SJeremy L Thompson 
1514382e9c83SJeremy L Thompson     CeedCallBackend(CeedOperatorFieldGetVector(op_input_fields[i], &vec));
1515382e9c83SJeremy L Thompson     if (vec != CEED_VECTOR_ACTIVE) continue;
1516382e9c83SJeremy L Thompson     CeedCallBackend(CeedVectorSetValue(impl->e_vecs[i], 0.0));
1517382e9c83SJeremy L Thompson     CeedCallBackend(CeedVectorSetValue(impl->q_vecs_in[i], 0.0));
1518382e9c83SJeremy L Thompson   }
1519382e9c83SJeremy L Thompson 
1520349fb27dSJeremy L Thompson   // Input basis apply if needed
1521349fb27dSJeremy L Thompson   CeedCallBackend(CeedOperatorInputBasisAtPoints_Cuda(num_elem, num_points, qf_input_fields, op_input_fields, num_input_fields, true, e_data, impl));
1522349fb27dSJeremy L Thompson 
1523349fb27dSJeremy L Thompson   // Output pointers, as necessary
1524349fb27dSJeremy L Thompson   for (CeedInt i = 0; i < num_output_fields; i++) {
1525349fb27dSJeremy L Thompson     CeedEvalMode eval_mode;
1526349fb27dSJeremy L Thompson 
1527349fb27dSJeremy L Thompson     CeedCallBackend(CeedQFunctionFieldGetEvalMode(qf_output_fields[i], &eval_mode));
1528349fb27dSJeremy L Thompson     if (eval_mode == CEED_EVAL_NONE) {
1529349fb27dSJeremy L Thompson       // Set the output Q-Vector to use the E-Vector data directly.
1530349fb27dSJeremy L Thompson       CeedCallBackend(CeedVectorGetArrayWrite(impl->e_vecs[i + impl->num_inputs], CEED_MEM_DEVICE, &e_data[i + num_input_fields]));
1531349fb27dSJeremy L Thompson       CeedCallBackend(CeedVectorSetArray(impl->q_vecs_out[i], CEED_MEM_DEVICE, CEED_USE_POINTER, e_data[i + num_input_fields]));
1532349fb27dSJeremy L Thompson     }
1533349fb27dSJeremy L Thompson   }
1534349fb27dSJeremy L Thompson 
1535349fb27dSJeremy L Thompson   // Loop over active fields
1536349fb27dSJeremy L Thompson   for (CeedInt i = 0; i < num_input_fields; i++) {
1537382e9c83SJeremy L Thompson     bool                is_active_at_points = true;
1538382e9c83SJeremy L Thompson     CeedInt             elem_size = 1, num_comp_active = 1, e_vec_size = 0;
1539382e9c83SJeremy L Thompson     CeedRestrictionType rstr_type;
1540382e9c83SJeremy L Thompson     CeedVector          vec;
1541382e9c83SJeremy L Thompson     CeedElemRestriction elem_rstr;
1542382e9c83SJeremy L Thompson 
1543382e9c83SJeremy L Thompson     CeedCallBackend(CeedOperatorFieldGetVector(op_input_fields[i], &vec));
1544382e9c83SJeremy L Thompson     // -- Skip non-active input
1545382e9c83SJeremy L Thompson     if (vec != CEED_VECTOR_ACTIVE) continue;
1546382e9c83SJeremy L Thompson 
1547382e9c83SJeremy L Thompson     // -- Get active restriction type
1548382e9c83SJeremy L Thompson     CeedCallBackend(CeedOperatorFieldGetElemRestriction(op_input_fields[i], &elem_rstr));
1549382e9c83SJeremy L Thompson     CeedCallBackend(CeedElemRestrictionGetType(elem_rstr, &rstr_type));
1550382e9c83SJeremy L Thompson     is_active_at_points = rstr_type == CEED_RESTRICTION_POINTS;
1551382e9c83SJeremy L Thompson     if (!is_active_at_points) CeedCallBackend(CeedElemRestrictionGetElementSize(elem_rstr, &elem_size));
1552382e9c83SJeremy L Thompson     else elem_size = max_num_points;
1553382e9c83SJeremy L Thompson     CeedCallBackend(CeedElemRestrictionGetNumComponents(elem_rstr, &num_comp_active));
1554382e9c83SJeremy L Thompson 
1555382e9c83SJeremy L Thompson     e_vec_size = elem_size * num_comp_active;
1556382e9c83SJeremy L Thompson     for (CeedInt s = 0; s < e_vec_size; s++) {
1557349fb27dSJeremy L Thompson       bool         is_active_input = false;
1558349fb27dSJeremy L Thompson       CeedEvalMode eval_mode;
1559349fb27dSJeremy L Thompson       CeedVector   vec;
1560349fb27dSJeremy L Thompson       CeedBasis    basis;
1561349fb27dSJeremy L Thompson 
1562349fb27dSJeremy L Thompson       CeedCallBackend(CeedOperatorFieldGetVector(op_input_fields[i], &vec));
1563349fb27dSJeremy L Thompson       // Skip non-active input
1564349fb27dSJeremy L Thompson       is_active_input = vec == CEED_VECTOR_ACTIVE;
1565349fb27dSJeremy L Thompson       if (!is_active_input) continue;
1566349fb27dSJeremy L Thompson 
1567349fb27dSJeremy L Thompson       // Update unit vector
1568349fb27dSJeremy L Thompson       else CeedCallBackend(CeedVectorSetValueStrided(impl->e_vecs[i], s - 1, e_vec_size, 0.0));
1569349fb27dSJeremy L Thompson       CeedCallBackend(CeedVectorSetValueStrided(impl->e_vecs[i], s, e_vec_size, 1.0));
1570349fb27dSJeremy L Thompson 
1571349fb27dSJeremy L Thompson       // Basis action
1572349fb27dSJeremy L Thompson       CeedCallBackend(CeedQFunctionFieldGetEvalMode(qf_input_fields[i], &eval_mode));
1573349fb27dSJeremy L Thompson       switch (eval_mode) {
1574349fb27dSJeremy L Thompson         case CEED_EVAL_NONE:
1575349fb27dSJeremy L Thompson           CeedCallBackend(CeedVectorSetArray(impl->q_vecs_in[i], CEED_MEM_DEVICE, CEED_USE_POINTER, e_data[i]));
1576349fb27dSJeremy L Thompson           break;
1577349fb27dSJeremy L Thompson         case CEED_EVAL_INTERP:
1578349fb27dSJeremy L Thompson         case CEED_EVAL_GRAD:
1579349fb27dSJeremy L Thompson         case CEED_EVAL_DIV:
1580349fb27dSJeremy L Thompson         case CEED_EVAL_CURL:
1581349fb27dSJeremy L Thompson           CeedCallBackend(CeedOperatorFieldGetBasis(op_input_fields[i], &basis));
1582349fb27dSJeremy L Thompson           CeedCallBackend(CeedBasisApplyAtPoints(basis, num_elem, num_points, CEED_NOTRANSPOSE, eval_mode, impl->point_coords_elem, impl->e_vecs[i],
1583349fb27dSJeremy L Thompson                                                  impl->q_vecs_in[i]));
1584349fb27dSJeremy L Thompson           break;
1585349fb27dSJeremy L Thompson         case CEED_EVAL_WEIGHT:
1586349fb27dSJeremy L Thompson           break;  // No action
1587349fb27dSJeremy L Thompson       }
1588349fb27dSJeremy L Thompson 
1589349fb27dSJeremy L Thompson       // Q function
1590349fb27dSJeremy L Thompson       CeedCallBackend(CeedQFunctionApply(qf, num_elem * max_num_points, impl->q_vecs_in, impl->q_vecs_out));
1591349fb27dSJeremy L Thompson 
1592349fb27dSJeremy L Thompson       // Output basis apply if needed
1593382e9c83SJeremy L Thompson       for (CeedInt j = 0; j < num_output_fields; j++) {
1594349fb27dSJeremy L Thompson         bool                is_active_output = false;
1595382e9c83SJeremy L Thompson         CeedInt             elem_size        = 0;
1596382e9c83SJeremy L Thompson         CeedRestrictionType rstr_type;
1597349fb27dSJeremy L Thompson         CeedEvalMode        eval_mode;
1598349fb27dSJeremy L Thompson         CeedVector          vec;
1599349fb27dSJeremy L Thompson         CeedElemRestriction elem_rstr;
1600349fb27dSJeremy L Thompson         CeedBasis           basis;
1601349fb27dSJeremy L Thompson 
1602382e9c83SJeremy L Thompson         CeedCallBackend(CeedOperatorFieldGetVector(op_output_fields[j], &vec));
1603382e9c83SJeremy L Thompson         // ---- Skip non-active output
1604349fb27dSJeremy L Thompson         is_active_output = vec == CEED_VECTOR_ACTIVE;
1605349fb27dSJeremy L Thompson         if (!is_active_output) continue;
1606349fb27dSJeremy L Thompson 
1607382e9c83SJeremy L Thompson         // ---- Check if elem size matches
1608382e9c83SJeremy L Thompson         CeedCallBackend(CeedOperatorFieldGetElemRestriction(op_output_fields[j], &elem_rstr));
1609382e9c83SJeremy L Thompson         CeedCallBackend(CeedElemRestrictionGetType(elem_rstr, &rstr_type));
1610382e9c83SJeremy L Thompson         if (is_active_at_points && rstr_type != CEED_RESTRICTION_POINTS) continue;
1611382e9c83SJeremy L Thompson         if (rstr_type == CEED_RESTRICTION_POINTS) {
1612382e9c83SJeremy L Thompson           CeedCallBackend(CeedElemRestrictionGetMaxPointsInElement(elem_rstr, &elem_size));
1613382e9c83SJeremy L Thompson         } else {
1614382e9c83SJeremy L Thompson           CeedCallBackend(CeedElemRestrictionGetElementSize(elem_rstr, &elem_size));
1615382e9c83SJeremy L Thompson         }
1616382e9c83SJeremy L Thompson         {
1617382e9c83SJeremy L Thompson           CeedInt num_comp = 0;
1618382e9c83SJeremy L Thompson 
1619382e9c83SJeremy L Thompson           CeedCallBackend(CeedElemRestrictionGetNumComponents(elem_rstr, &num_comp));
1620382e9c83SJeremy L Thompson           if (e_vec_size != num_comp * elem_size) continue;
1621382e9c83SJeremy L Thompson         }
1622382e9c83SJeremy L Thompson 
1623349fb27dSJeremy L Thompson         // Basis action
1624382e9c83SJeremy L Thompson         CeedCallBackend(CeedQFunctionFieldGetEvalMode(qf_output_fields[j], &eval_mode));
1625349fb27dSJeremy L Thompson         switch (eval_mode) {
1626349fb27dSJeremy L Thompson           case CEED_EVAL_NONE:
1627382e9c83SJeremy L Thompson             CeedCallBackend(CeedVectorRestoreArray(impl->e_vecs[j + impl->num_inputs], &e_data[j + num_input_fields]));
1628349fb27dSJeremy L Thompson             break;
1629349fb27dSJeremy L Thompson           case CEED_EVAL_INTERP:
1630349fb27dSJeremy L Thompson           case CEED_EVAL_GRAD:
1631349fb27dSJeremy L Thompson           case CEED_EVAL_DIV:
1632349fb27dSJeremy L Thompson           case CEED_EVAL_CURL:
1633382e9c83SJeremy L Thompson             CeedCallBackend(CeedOperatorFieldGetBasis(op_output_fields[j], &basis));
1634382e9c83SJeremy L Thompson             CeedCallBackend(CeedBasisApplyAtPoints(basis, num_elem, num_points, CEED_TRANSPOSE, eval_mode, impl->point_coords_elem,
1635382e9c83SJeremy L Thompson                                                    impl->q_vecs_out[j], impl->e_vecs[j + impl->num_inputs]));
1636349fb27dSJeremy L Thompson             break;
1637349fb27dSJeremy L Thompson           // LCOV_EXCL_START
1638349fb27dSJeremy L Thompson           case CEED_EVAL_WEIGHT: {
1639349fb27dSJeremy L Thompson             return CeedError(CeedOperatorReturnCeed(op), CEED_ERROR_BACKEND, "CEED_EVAL_WEIGHT cannot be an output evaluation mode");
1640349fb27dSJeremy L Thompson             // LCOV_EXCL_STOP
1641349fb27dSJeremy L Thompson           }
1642349fb27dSJeremy L Thompson         }
1643349fb27dSJeremy L Thompson 
1644349fb27dSJeremy L Thompson         // Mask output e-vec
1645382e9c83SJeremy L Thompson         CeedCallBackend(CeedVectorPointwiseMult(impl->e_vecs[j + impl->num_inputs], impl->e_vecs[i], impl->e_vecs[j + impl->num_inputs]));
1646349fb27dSJeremy L Thompson 
1647349fb27dSJeremy L Thompson         // Restrict
1648382e9c83SJeremy L Thompson         CeedCallBackend(CeedOperatorFieldGetElemRestriction(op_output_fields[j], &elem_rstr));
1649382e9c83SJeremy L Thompson         CeedCallBackend(CeedElemRestrictionApply(elem_rstr, CEED_TRANSPOSE, impl->e_vecs[j + impl->num_inputs], assembled, request));
1650349fb27dSJeremy L Thompson 
1651349fb27dSJeremy L Thompson         // Reset q_vec for
1652349fb27dSJeremy L Thompson         if (eval_mode == CEED_EVAL_NONE) {
1653382e9c83SJeremy L Thompson           CeedCallBackend(CeedVectorGetArrayWrite(impl->e_vecs[j + impl->num_inputs], CEED_MEM_DEVICE, &e_data[j + num_input_fields]));
1654382e9c83SJeremy L Thompson           CeedCallBackend(CeedVectorSetArray(impl->q_vecs_out[j], CEED_MEM_DEVICE, CEED_USE_POINTER, e_data[j + num_input_fields]));
1655349fb27dSJeremy L Thompson         }
1656349fb27dSJeremy L Thompson       }
1657382e9c83SJeremy L Thompson 
1658382e9c83SJeremy L Thompson       // Reset vec
1659*86e10729SJeremy L Thompson       if (s == e_vec_size - 1) {
1660*86e10729SJeremy L Thompson         CeedCallBackend(CeedVectorSetValueStrided(impl->e_vecs[i], s, e_vec_size, 0.0));
1661*86e10729SJeremy L Thompson         CeedCallBackend(CeedVectorSetValue(impl->q_vecs_in[i], 0.0));
1662*86e10729SJeremy L Thompson       }
1663349fb27dSJeremy L Thompson     }
1664349fb27dSJeremy L Thompson 
1665349fb27dSJeremy L Thompson     // Restore CEED_EVAL_NONE
1666349fb27dSJeremy L Thompson     for (CeedInt i = 0; i < num_output_fields; i++) {
1667349fb27dSJeremy L Thompson       CeedEvalMode        eval_mode;
1668349fb27dSJeremy L Thompson       CeedElemRestriction elem_rstr;
1669349fb27dSJeremy L Thompson 
1670349fb27dSJeremy L Thompson       // Get eval_mode
1671349fb27dSJeremy L Thompson       CeedCallBackend(CeedOperatorFieldGetElemRestriction(op_output_fields[i], &elem_rstr));
1672349fb27dSJeremy L Thompson       CeedCallBackend(CeedQFunctionFieldGetEvalMode(qf_output_fields[i], &eval_mode));
1673349fb27dSJeremy L Thompson 
1674349fb27dSJeremy L Thompson       // Restore evec
1675349fb27dSJeremy L Thompson       CeedCallBackend(CeedQFunctionFieldGetEvalMode(qf_output_fields[i], &eval_mode));
1676349fb27dSJeremy L Thompson       if (eval_mode == CEED_EVAL_NONE) {
1677349fb27dSJeremy L Thompson         CeedCallBackend(CeedVectorRestoreArray(impl->e_vecs[i + impl->num_inputs], &e_data[i + num_input_fields]));
1678349fb27dSJeremy L Thompson       }
1679349fb27dSJeremy L Thompson     }
1680382e9c83SJeremy L Thompson   }
1681349fb27dSJeremy L Thompson 
1682349fb27dSJeremy L Thompson   // Restore input arrays
1683349fb27dSJeremy L Thompson   CeedCallBackend(CeedOperatorRestoreInputs_Cuda(num_input_fields, qf_input_fields, op_input_fields, true, e_data, impl));
1684349fb27dSJeremy L Thompson   return CEED_ERROR_SUCCESS;
1685756ca9e9SJeremy L Thompson }
1686756ca9e9SJeremy L Thompson 
1687756ca9e9SJeremy L Thompson //------------------------------------------------------------------------------
16880d0321e0SJeremy L Thompson // Create operator
16890d0321e0SJeremy L Thompson //------------------------------------------------------------------------------
16900d0321e0SJeremy L Thompson int CeedOperatorCreate_Cuda(CeedOperator op) {
16910d0321e0SJeremy L Thompson   Ceed               ceed;
16920d0321e0SJeremy L Thompson   CeedOperator_Cuda *impl;
16930d0321e0SJeremy L Thompson 
1694ca735530SJeremy L Thompson   CeedCallBackend(CeedOperatorGetCeed(op, &ceed));
16952b730f8bSJeremy L Thompson   CeedCallBackend(CeedCalloc(1, &impl));
16962b730f8bSJeremy L Thompson   CeedCallBackend(CeedOperatorSetData(op, impl));
16970d0321e0SJeremy L Thompson 
16982b730f8bSJeremy L Thompson   CeedCallBackend(CeedSetBackendFunction(ceed, "Operator", op, "LinearAssembleQFunction", CeedOperatorLinearAssembleQFunction_Cuda));
16992b730f8bSJeremy L Thompson   CeedCallBackend(CeedSetBackendFunction(ceed, "Operator", op, "LinearAssembleQFunctionUpdate", CeedOperatorLinearAssembleQFunctionUpdate_Cuda));
17002b730f8bSJeremy L Thompson   CeedCallBackend(CeedSetBackendFunction(ceed, "Operator", op, "LinearAssembleAddDiagonal", CeedOperatorLinearAssembleAddDiagonal_Cuda));
17012b730f8bSJeremy L Thompson   CeedCallBackend(
17022b730f8bSJeremy L Thompson       CeedSetBackendFunction(ceed, "Operator", op, "LinearAssembleAddPointBlockDiagonal", CeedOperatorLinearAssembleAddPointBlockDiagonal_Cuda));
17032b730f8bSJeremy L Thompson   CeedCallBackend(CeedSetBackendFunction(ceed, "Operator", op, "LinearAssembleSingle", CeedSingleOperatorAssemble_Cuda));
17042b730f8bSJeremy L Thompson   CeedCallBackend(CeedSetBackendFunction(ceed, "Operator", op, "ApplyAdd", CeedOperatorApplyAdd_Cuda));
17052b730f8bSJeremy L Thompson   CeedCallBackend(CeedSetBackendFunction(ceed, "Operator", op, "Destroy", CeedOperatorDestroy_Cuda));
17060d0321e0SJeremy L Thompson   return CEED_ERROR_SUCCESS;
17070d0321e0SJeremy L Thompson }
17080d0321e0SJeremy L Thompson 
17090d0321e0SJeremy L Thompson //------------------------------------------------------------------------------
171067d9480aSJeremy L Thompson // Create operator AtPoints
1711756ca9e9SJeremy L Thompson //------------------------------------------------------------------------------
1712756ca9e9SJeremy L Thompson int CeedOperatorCreateAtPoints_Cuda(CeedOperator op) {
1713756ca9e9SJeremy L Thompson   Ceed               ceed;
1714756ca9e9SJeremy L Thompson   CeedOperator_Cuda *impl;
1715756ca9e9SJeremy L Thompson 
1716756ca9e9SJeremy L Thompson   CeedCallBackend(CeedOperatorGetCeed(op, &ceed));
1717756ca9e9SJeremy L Thompson   CeedCallBackend(CeedCalloc(1, &impl));
1718756ca9e9SJeremy L Thompson   CeedCallBackend(CeedOperatorSetData(op, impl));
1719756ca9e9SJeremy L Thompson 
1720756ca9e9SJeremy L Thompson   CeedCallBackend(CeedSetBackendFunction(ceed, "Operator", op, "LinearAssembleQFunction", CeedOperatorLinearAssembleQFunctionAtPoints_Cuda));
1721756ca9e9SJeremy L Thompson   CeedCallBackend(CeedSetBackendFunction(ceed, "Operator", op, "LinearAssembleAddDiagonal", CeedOperatorLinearAssembleAddDiagonalAtPoints_Cuda));
1722756ca9e9SJeremy L Thompson   CeedCallBackend(CeedSetBackendFunction(ceed, "Operator", op, "ApplyAdd", CeedOperatorApplyAddAtPoints_Cuda));
1723756ca9e9SJeremy L Thompson   CeedCallBackend(CeedSetBackendFunction(ceed, "Operator", op, "Destroy", CeedOperatorDestroy_Cuda));
1724756ca9e9SJeremy L Thompson   return CEED_ERROR_SUCCESS;
1725756ca9e9SJeremy L Thompson }
1726756ca9e9SJeremy L Thompson 
1727756ca9e9SJeremy L Thompson //------------------------------------------------------------------------------
1728