xref: /libCEED/backends/cuda-gen/ceed-cuda-gen-operator-build.cpp (revision 0183ed61035d97ff853cf8c8e722c0fda76e54df)
1d275d636SJeremy L Thompson // Copyright (c) 2017-2025, 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.
3241a4b83SYohann //
43d8e8822SJeremy L Thompson // SPDX-License-Identifier: BSD-2-Clause
5241a4b83SYohann //
63d8e8822SJeremy L Thompson // This file is part of CEED:  http://github.com/ceed
73d576824SJeremy L Thompson 
8b8e71988SJeremy L Thompson #define CEED_DEBUG_COLOR 12
97df94212SJeremy L Thompson 
1049aac155SJeremy L Thompson #include <ceed.h>
11ec3da8bcSJed Brown #include <ceed/backend.h>
12*0183ed61SJeremy L Thompson #include <ceed/gen-tools.h>
139e201c85SYohann #include <ceed/jit-tools.h>
143d576824SJeremy L Thompson #include <cuda_runtime.h>
152b730f8bSJeremy L Thompson 
16241a4b83SYohann #include <iostream>
17241a4b83SYohann #include <sstream>
18b2165e7aSSebastian Grimberg #include <string>
192b730f8bSJeremy L Thompson 
200d0321e0SJeremy L Thompson #include "../cuda-ref/ceed-cuda-ref.h"
21241a4b83SYohann #include "../cuda-shared/ceed-cuda-shared.h"
2249aac155SJeremy L Thompson #include "../cuda/ceed-cuda-common.h"
232b730f8bSJeremy L Thompson #include "../cuda/ceed-cuda-compile.h"
242b730f8bSJeremy L Thompson #include "ceed-cuda-gen.h"
25241a4b83SYohann 
2645a787f7SJeremy L Thompson struct FieldReuse_Cuda {
2745a787f7SJeremy L Thompson   CeedInt      index;
2845a787f7SJeremy L Thompson   bool         is_input;
2945a787f7SJeremy L Thompson   CeedEvalMode eval_mode;
3045a787f7SJeremy L Thompson };
3145a787f7SJeremy L Thompson 
32ab213215SJeremy L Thompson //------------------------------------------------------------------------------
334b3e95d5SJeremy L Thompson // Determine type of operator
344b3e95d5SJeremy L Thompson //------------------------------------------------------------------------------
354b3e95d5SJeremy L Thompson static int CeedOperatorBuildKernelData_Cuda_gen(Ceed ceed, CeedInt num_input_fields, CeedOperatorField *op_input_fields,
364b3e95d5SJeremy L Thompson                                                 CeedQFunctionField *qf_input_fields, CeedInt num_output_fields, CeedOperatorField *op_output_fields,
37412e5683SJeremy L Thompson                                                 CeedQFunctionField *qf_output_fields, CeedInt *max_P, CeedInt *max_P_1d, CeedInt *Q, CeedInt *Q_1d,
38412e5683SJeremy L Thompson                                                 CeedInt *max_dim, bool *is_all_tensor, bool *use_3d_slices) {
39412e5683SJeremy L Thompson   // Check if all are tensor
40412e5683SJeremy L Thompson   *is_all_tensor = true;
414b3e95d5SJeremy L Thompson   for (CeedInt i = 0; i < num_input_fields; i++) {
424b3e95d5SJeremy L Thompson     CeedBasis basis;
434b3e95d5SJeremy L Thompson 
444b3e95d5SJeremy L Thompson     CeedCallBackend(CeedOperatorFieldGetBasis(op_input_fields[i], &basis));
454b3e95d5SJeremy L Thompson     if (basis != CEED_BASIS_NONE) {
464b3e95d5SJeremy L Thompson       bool is_field_tensor;
474b3e95d5SJeremy L Thompson 
484b3e95d5SJeremy L Thompson       CeedCallBackend(CeedBasisIsTensor(basis, &is_field_tensor));
49412e5683SJeremy L Thompson       *is_all_tensor = *is_all_tensor && is_field_tensor;
504b3e95d5SJeremy L Thompson     }
51681d0ea7SJeremy L Thompson     CeedCallBackend(CeedBasisDestroy(&basis));
524b3e95d5SJeremy L Thompson   }
534b3e95d5SJeremy L Thompson   for (CeedInt i = 0; i < num_output_fields; i++) {
544b3e95d5SJeremy L Thompson     CeedBasis basis;
554b3e95d5SJeremy L Thompson 
564b3e95d5SJeremy L Thompson     CeedCallBackend(CeedOperatorFieldGetBasis(op_output_fields[i], &basis));
574b3e95d5SJeremy L Thompson     if (basis != CEED_BASIS_NONE) {
584b3e95d5SJeremy L Thompson       bool is_field_tensor;
594b3e95d5SJeremy L Thompson 
604b3e95d5SJeremy L Thompson       CeedCallBackend(CeedBasisIsTensor(basis, &is_field_tensor));
61412e5683SJeremy L Thompson       *is_all_tensor = *is_all_tensor && is_field_tensor;
62412e5683SJeremy L Thompson     }
63412e5683SJeremy L Thompson     CeedCallBackend(CeedBasisDestroy(&basis));
64412e5683SJeremy L Thompson   }
65412e5683SJeremy L Thompson 
66412e5683SJeremy L Thompson   // Find max_P, max_P_1d, Q, and Q_1d
67412e5683SJeremy L Thompson   bool is_all_3d = true;
68412e5683SJeremy L Thompson 
69412e5683SJeremy L Thompson   *max_P    = 0;
70412e5683SJeremy L Thompson   *max_P_1d = 0;
71412e5683SJeremy L Thompson   *Q        = 0;
72412e5683SJeremy L Thompson   *Q_1d     = 0;
73412e5683SJeremy L Thompson   for (CeedInt i = 0; i < num_input_fields; i++) {
74412e5683SJeremy L Thompson     CeedBasis basis;
75412e5683SJeremy L Thompson 
76412e5683SJeremy L Thompson     CeedCallBackend(CeedOperatorFieldGetBasis(op_input_fields[i], &basis));
77412e5683SJeremy L Thompson     if (basis != CEED_BASIS_NONE) {
78412e5683SJeremy L Thompson       bool    is_field_tensor;
79412e5683SJeremy L Thompson       CeedInt field_dim = 0, field_P = 0, field_P_1d = 0, field_Q = 0, field_Q_1d = 0;
80412e5683SJeremy L Thompson 
81412e5683SJeremy L Thompson       // Check if 3D
824b3e95d5SJeremy L Thompson       CeedCallBackend(CeedBasisGetDimension(basis, &field_dim));
83412e5683SJeremy L Thompson       is_all_3d = is_all_3d && (field_dim == 3);
84412e5683SJeremy L Thompson       *max_dim  = CeedIntMax(*max_dim, field_dim);
85412e5683SJeremy L Thompson 
86412e5683SJeremy L Thompson       // Collect P, P_1d, Q, and Q_1d
87412e5683SJeremy L Thompson       CeedCallBackend(CeedBasisGetNumNodes(basis, &field_P));
88412e5683SJeremy L Thompson       *max_P = CeedIntMax(*max_P, field_P);
89412e5683SJeremy L Thompson       CeedCallBackend(CeedBasisIsTensor(basis, &is_field_tensor));
90412e5683SJeremy L Thompson       if (is_field_tensor) {
91412e5683SJeremy L Thompson         CeedCallBackend(CeedBasisGetNumNodes1D(basis, &field_P_1d));
92412e5683SJeremy L Thompson         *max_P_1d = CeedIntMax(*max_P_1d, field_P_1d);
93412e5683SJeremy L Thompson       }
94412e5683SJeremy L Thompson       CeedCallBackend(CeedBasisGetNumQuadraturePoints(basis, &field_Q));
95412e5683SJeremy L Thompson       CeedCheck(*Q == 0 || field_Q == *Q, ceed, CEED_ERROR_BACKEND, "Quadrature spaces must be compatible");
96412e5683SJeremy L Thompson       *Q = field_Q;
97412e5683SJeremy L Thompson       if (is_field_tensor) {
98412e5683SJeremy L Thompson         CeedCallBackend(CeedBasisGetNumQuadraturePoints1D(basis, &field_Q_1d));
994b3e95d5SJeremy L Thompson         CeedCheck(*Q_1d == 0 || field_Q_1d == *Q_1d, ceed, CEED_ERROR_BACKEND, "Quadrature spaces must be compatible");
1004b3e95d5SJeremy L Thompson         *Q_1d = field_Q_1d;
1014b3e95d5SJeremy L Thompson       }
102412e5683SJeremy L Thompson     }
103412e5683SJeremy L Thompson     CeedCallBackend(CeedBasisDestroy(&basis));
104412e5683SJeremy L Thompson   }
105412e5683SJeremy L Thompson   for (CeedInt i = 0; i < num_output_fields; i++) {
106412e5683SJeremy L Thompson     CeedBasis basis;
107412e5683SJeremy L Thompson 
108412e5683SJeremy L Thompson     CeedCallBackend(CeedOperatorFieldGetBasis(op_output_fields[i], &basis));
109412e5683SJeremy L Thompson     if (basis != CEED_BASIS_NONE) {
110412e5683SJeremy L Thompson       bool    is_field_tensor;
111412e5683SJeremy L Thompson       CeedInt field_dim = 0, field_P = 0, field_P_1d = 0, field_Q = 0, field_Q_1d = 0;
112412e5683SJeremy L Thompson 
113412e5683SJeremy L Thompson       // Check if 3D
114412e5683SJeremy L Thompson       CeedCallBackend(CeedBasisGetDimension(basis, &field_dim));
115412e5683SJeremy L Thompson       is_all_3d = is_all_3d && (field_dim == 3);
116412e5683SJeremy L Thompson       *max_dim  = CeedIntMax(*max_dim, field_dim);
117412e5683SJeremy L Thompson 
118412e5683SJeremy L Thompson       // Collect P, P_1d, Q, and Q_1d
119412e5683SJeremy L Thompson       CeedCallBackend(CeedBasisGetNumNodes(basis, &field_P));
120412e5683SJeremy L Thompson       *max_P = CeedIntMax(*max_P, field_P);
121412e5683SJeremy L Thompson       CeedCallBackend(CeedBasisIsTensor(basis, &is_field_tensor));
122412e5683SJeremy L Thompson       if (is_field_tensor) {
123412e5683SJeremy L Thompson         CeedCallBackend(CeedBasisGetNumNodes1D(basis, &field_P_1d));
124412e5683SJeremy L Thompson         *max_P_1d = CeedIntMax(*max_P_1d, field_P_1d);
125412e5683SJeremy L Thompson       }
126412e5683SJeremy L Thompson       CeedCallBackend(CeedBasisGetNumQuadraturePoints(basis, &field_Q));
127412e5683SJeremy L Thompson       CeedCheck(*Q == 0 || field_Q == *Q, ceed, CEED_ERROR_BACKEND, "Quadrature spaces must be compatible");
128412e5683SJeremy L Thompson       *Q = field_Q;
129412e5683SJeremy L Thompson       if (is_field_tensor) {
130412e5683SJeremy L Thompson         CeedCallBackend(CeedBasisGetNumQuadraturePoints1D(basis, &field_Q_1d));
131412e5683SJeremy L Thompson         CeedCheck(*Q_1d == 0 || field_Q_1d == *Q_1d, ceed, CEED_ERROR_BACKEND, "Quadrature spaces must be compatible");
132412e5683SJeremy L Thompson         *Q_1d = field_Q_1d;
133412e5683SJeremy L Thompson       }
134412e5683SJeremy L Thompson     }
135681d0ea7SJeremy L Thompson     CeedCallBackend(CeedBasisDestroy(&basis));
1364b3e95d5SJeremy L Thompson   }
1374b3e95d5SJeremy L Thompson 
1384b3e95d5SJeremy L Thompson   // Only use 3D collocated gradient parallelization strategy when gradient is computed
1394b3e95d5SJeremy L Thompson   *use_3d_slices = false;
140412e5683SJeremy L Thompson   if (is_all_3d && *is_all_tensor) {
1414b3e95d5SJeremy L Thompson     bool was_grad_found = false;
1424b3e95d5SJeremy L Thompson 
1434b3e95d5SJeremy L Thompson     for (CeedInt i = 0; i < num_input_fields; i++) {
1444b3e95d5SJeremy L Thompson       CeedEvalMode eval_mode;
1454b3e95d5SJeremy L Thompson 
1464b3e95d5SJeremy L Thompson       CeedCallBackend(CeedQFunctionFieldGetEvalMode(qf_input_fields[i], &eval_mode));
1474b3e95d5SJeremy L Thompson       if (eval_mode == CEED_EVAL_GRAD) {
1484b3e95d5SJeremy L Thompson         CeedBasis_Cuda_shared *basis_data;
1494b3e95d5SJeremy L Thompson         CeedBasis              basis;
1504b3e95d5SJeremy L Thompson 
1514b3e95d5SJeremy L Thompson         CeedCallBackend(CeedOperatorFieldGetBasis(op_input_fields[i], &basis));
1524b3e95d5SJeremy L Thompson         CeedCallBackend(CeedBasisGetData(basis, &basis_data));
1534b3e95d5SJeremy L Thompson         *use_3d_slices = basis_data->d_collo_grad_1d && (was_grad_found ? *use_3d_slices : true);
1544b3e95d5SJeremy L Thompson         was_grad_found = true;
155681d0ea7SJeremy L Thompson         CeedCallBackend(CeedBasisDestroy(&basis));
1564b3e95d5SJeremy L Thompson       }
1574b3e95d5SJeremy L Thompson     }
1584b3e95d5SJeremy L Thompson     for (CeedInt i = 0; i < num_output_fields; i++) {
1594b3e95d5SJeremy L Thompson       CeedEvalMode eval_mode;
1604b3e95d5SJeremy L Thompson 
1614b3e95d5SJeremy L Thompson       CeedCallBackend(CeedQFunctionFieldGetEvalMode(qf_output_fields[i], &eval_mode));
1624b3e95d5SJeremy L Thompson       if (eval_mode == CEED_EVAL_GRAD) {
1634b3e95d5SJeremy L Thompson         CeedBasis_Cuda_shared *basis_data;
1644b3e95d5SJeremy L Thompson         CeedBasis              basis;
1654b3e95d5SJeremy L Thompson 
1664b3e95d5SJeremy L Thompson         CeedCallBackend(CeedOperatorFieldGetBasis(op_output_fields[i], &basis));
1674b3e95d5SJeremy L Thompson         CeedCallBackend(CeedBasisGetData(basis, &basis_data));
1684b3e95d5SJeremy L Thompson         *use_3d_slices = basis_data->d_collo_grad_1d && (was_grad_found ? *use_3d_slices : true);
1694b3e95d5SJeremy L Thompson         was_grad_found = true;
170681d0ea7SJeremy L Thompson         CeedCallBackend(CeedBasisDestroy(&basis));
1714b3e95d5SJeremy L Thompson       }
1724b3e95d5SJeremy L Thompson     }
1734b3e95d5SJeremy L Thompson   }
1744b3e95d5SJeremy L Thompson   return CEED_ERROR_SUCCESS;
1754b3e95d5SJeremy L Thompson }
1764b3e95d5SJeremy L Thompson 
1774b3e95d5SJeremy L Thompson //------------------------------------------------------------------------------
1784b3e95d5SJeremy L Thompson // Setup fields
1794b3e95d5SJeremy L Thompson //------------------------------------------------------------------------------
180*0183ed61SJeremy L Thompson static int CeedOperatorBuildKernelFieldData_Cuda_gen(std::ostringstream &code, CeedOperator_Cuda_gen *data, Tab &tab, CeedInt i,
181*0183ed61SJeremy L Thompson                                                      CeedOperatorField op_field, CeedQFunctionField qf_field, FieldReuse_Cuda field_reuse,
182*0183ed61SJeremy L Thompson                                                      CeedInt max_dim, CeedInt Q, CeedInt Q_1d, bool is_input, bool is_all_tensor, bool is_at_points,
183*0183ed61SJeremy L Thompson                                                      bool use_3d_slices) {
184412e5683SJeremy L Thompson   bool      is_tensor = true;
185412e5683SJeremy L Thompson   CeedBasis basis;
186412e5683SJeremy L Thompson   CeedCallBackend(CeedOperatorFieldGetBasis(op_field, &basis));
187412e5683SJeremy L Thompson   if (basis != CEED_BASIS_NONE) CeedCallBackend(CeedBasisIsTensor(basis, &is_tensor));
188412e5683SJeremy L Thompson 
18959fa3f92SJeremy L Thompson   const char            *field_name;
1904b3e95d5SJeremy L Thompson   std::string            var_suffix = (is_input ? "_in_" : "_out_") + std::to_string(i);
191dc007f05SJeremy L Thompson   std::string            P_name = (is_tensor ? "P_1d" : "P") + var_suffix, Q_name = is_tensor ? "Q_1d" : "Q";
1924b3e95d5SJeremy L Thompson   std::string            option_name = (is_input ? "inputs" : "outputs");
1934b3e95d5SJeremy L Thompson   CeedEvalMode           eval_mode   = CEED_EVAL_NONE;
1948014c5e7SJeremy L Thompson   CeedInt                elem_size = 0, num_comp = 0, dim = max_dim, P_1d = 0;
1954b3e95d5SJeremy L Thompson   CeedElemRestriction    elem_rstr;
1964b3e95d5SJeremy L Thompson   CeedBasis_Cuda_shared *basis_data;
1974b3e95d5SJeremy L Thompson 
1980a2a6492SJeremy L Thompson   // Field reuse info
19945a787f7SJeremy L Thompson   bool use_previous_field = field_reuse.index != -1;
2000a2a6492SJeremy L Thompson 
20159fa3f92SJeremy L Thompson   CeedCallBackend(CeedOperatorFieldGetName(op_field, &field_name));
202*0183ed61SJeremy L Thompson   code << tab << "// -- " << (is_input ? "Input" : "Output") << " field " << i << ": " << field_name << "\n";
2034b3e95d5SJeremy L Thompson 
2044b3e95d5SJeremy L Thompson   // Get field data
2054b3e95d5SJeremy L Thompson   CeedCallBackend(CeedOperatorFieldGetElemRestriction(op_field, &elem_rstr));
2064b3e95d5SJeremy L Thompson   if (elem_rstr != CEED_ELEMRESTRICTION_NONE) {
2074b3e95d5SJeremy L Thompson     CeedCallBackend(CeedElemRestrictionGetElementSize(elem_rstr, &elem_size));
2084b3e95d5SJeremy L Thompson     CeedCallBackend(CeedElemRestrictionGetNumComponents(elem_rstr, &num_comp));
2094b3e95d5SJeremy L Thompson   }
210681d0ea7SJeremy L Thompson   CeedCallBackend(CeedElemRestrictionDestroy(&elem_rstr));
2114b3e95d5SJeremy L Thompson   if (basis != CEED_BASIS_NONE) {
2124b3e95d5SJeremy L Thompson     CeedCallBackend(CeedBasisGetData(basis, &basis_data));
213412e5683SJeremy L Thompson     CeedCallBackend(CeedBasisGetDimension(basis, &dim));
214dc007f05SJeremy L Thompson     if (is_tensor) CeedCallBackend(CeedBasisGetNumNodes1D(basis, &P_1d));
215dc007f05SJeremy L Thompson     else CeedCallBackend(CeedBasisGetNumNodes(basis, &P_1d));
2164b3e95d5SJeremy L Thompson   }
2174b3e95d5SJeremy L Thompson   CeedCallBackend(CeedQFunctionFieldGetEvalMode(qf_field, &eval_mode));
2184b3e95d5SJeremy L Thompson 
2194b3e95d5SJeremy L Thompson   // Set field constants
220*0183ed61SJeremy L Thompson   code << tab << "const CeedInt dim" << var_suffix << " = " << dim << ";\n";
221412e5683SJeremy L Thompson   if (is_tensor && !is_all_tensor) {
222412e5683SJeremy L Thompson     CeedInt P = 0;
223412e5683SJeremy L Thompson 
224412e5683SJeremy L Thompson     CeedCallBackend(CeedBasisGetNumNodes(basis, &P));
225*0183ed61SJeremy L Thompson     code << tab << "const CeedInt P" << var_suffix << " = " << (basis == CEED_BASIS_NONE ? Q : P) << ";\n";
226412e5683SJeremy L Thompson   }
227*0183ed61SJeremy L Thompson   code << tab << "const CeedInt " << P_name << " = " << (basis == CEED_BASIS_NONE ? Q_1d : P_1d) << ";\n";
228343e3094SJeremy L Thompson   if (eval_mode != CEED_EVAL_WEIGHT) {
229*0183ed61SJeremy L Thompson     code << tab << "const CeedInt num_comp" << var_suffix << " = " << num_comp << ";\n";
2304b3e95d5SJeremy L Thompson   }
2314b3e95d5SJeremy L Thompson 
2324b3e95d5SJeremy L Thompson   // Load basis data
233*0183ed61SJeremy L Thompson   code << tab << "// EvalMode: " << CeedEvalModes[eval_mode] << "\n";
2344b3e95d5SJeremy L Thompson   switch (eval_mode) {
2354b3e95d5SJeremy L Thompson     case CEED_EVAL_NONE:
2364b3e95d5SJeremy L Thompson       break;
2374b3e95d5SJeremy L Thompson     case CEED_EVAL_INTERP:
2388b97b69aSJeremy L Thompson       if (is_at_points) {
2398b97b69aSJeremy L Thompson         // AtPoints
2408b97b69aSJeremy L Thompson         if (!basis_data->d_chebyshev_interp_1d) {
2418b97b69aSJeremy L Thompson           CeedSize    interp_bytes;
2428b97b69aSJeremy L Thompson           CeedScalar *chebyshev_interp_1d;
2438b97b69aSJeremy L Thompson 
2448b97b69aSJeremy L Thompson           interp_bytes = P_1d * Q_1d * sizeof(CeedScalar);
2458b97b69aSJeremy L Thompson           CeedCallBackend(CeedCalloc(P_1d * Q_1d, &chebyshev_interp_1d));
2468b97b69aSJeremy L Thompson           CeedCallBackend(CeedBasisGetChebyshevInterp1D(basis, chebyshev_interp_1d));
2478b97b69aSJeremy L Thompson           CeedCallCuda(CeedBasisReturnCeed(basis), cudaMalloc((void **)&basis_data->d_chebyshev_interp_1d, interp_bytes));
2488b97b69aSJeremy L Thompson           CeedCallCuda(CeedBasisReturnCeed(basis),
2498b97b69aSJeremy L Thompson                        cudaMemcpy(basis_data->d_chebyshev_interp_1d, chebyshev_interp_1d, interp_bytes, cudaMemcpyHostToDevice));
2508b97b69aSJeremy L Thompson           CeedCallBackend(CeedFree(&chebyshev_interp_1d));
2518b97b69aSJeremy L Thompson         }
2528b97b69aSJeremy L Thompson         if (is_input) data->B.inputs[i] = basis_data->d_chebyshev_interp_1d;
2538b97b69aSJeremy L Thompson         else data->B.outputs[i] = basis_data->d_chebyshev_interp_1d;
2548b97b69aSJeremy L Thompson       } else {
2558b97b69aSJeremy L Thompson         // Standard quadrature
2564b3e95d5SJeremy L Thompson         if (is_input) data->B.inputs[i] = basis_data->d_interp_1d;
2574b3e95d5SJeremy L Thompson         else data->B.outputs[i] = basis_data->d_interp_1d;
2588b97b69aSJeremy L Thompson       }
2590a2a6492SJeremy L Thompson       if (use_previous_field) {
26045a787f7SJeremy L Thompson         std::string reuse_var = "s_B" + ((field_reuse.is_input ? "_in_" : "_out_") + std::to_string(field_reuse.index));
2610a2a6492SJeremy L Thompson 
262*0183ed61SJeremy L Thompson         code << tab << "CeedScalar *s_B" << var_suffix << " = " << reuse_var << ";\n";
2630a2a6492SJeremy L Thompson       } else {
264*0183ed61SJeremy L Thompson         code << tab << "__shared__ CeedScalar s_B" << var_suffix << "[" << P_name << "*" << Q_name << "];\n";
265*0183ed61SJeremy L Thompson         code << tab << "LoadMatrix<" << P_name << ", " << Q_name << ">(data, B." << option_name << "[" << i << "], s_B" << var_suffix << ");\n";
2660a2a6492SJeremy L Thompson       }
2674b3e95d5SJeremy L Thompson       break;
2684b3e95d5SJeremy L Thompson     case CEED_EVAL_GRAD:
2698b97b69aSJeremy L Thompson       if (is_at_points) {
2708b97b69aSJeremy L Thompson         // AtPoints
2718b97b69aSJeremy L Thompson         if (!basis_data->d_chebyshev_interp_1d) {
2728b97b69aSJeremy L Thompson           CeedSize    interp_bytes;
2738b97b69aSJeremy L Thompson           CeedScalar *chebyshev_interp_1d;
2748b97b69aSJeremy L Thompson 
2758b97b69aSJeremy L Thompson           interp_bytes = P_1d * Q_1d * sizeof(CeedScalar);
2768b97b69aSJeremy L Thompson           CeedCallBackend(CeedCalloc(P_1d * Q_1d, &chebyshev_interp_1d));
2778b97b69aSJeremy L Thompson           CeedCallBackend(CeedBasisGetChebyshevInterp1D(basis, chebyshev_interp_1d));
2788b97b69aSJeremy L Thompson           CeedCallCuda(CeedBasisReturnCeed(basis), cudaMalloc((void **)&basis_data->d_chebyshev_interp_1d, interp_bytes));
2798b97b69aSJeremy L Thompson           CeedCallCuda(CeedBasisReturnCeed(basis),
2808b97b69aSJeremy L Thompson                        cudaMemcpy(basis_data->d_chebyshev_interp_1d, chebyshev_interp_1d, interp_bytes, cudaMemcpyHostToDevice));
2818b97b69aSJeremy L Thompson           CeedCallBackend(CeedFree(&chebyshev_interp_1d));
2828b97b69aSJeremy L Thompson         }
2838b97b69aSJeremy L Thompson         if (is_input) data->B.inputs[i] = basis_data->d_chebyshev_interp_1d;
2848b97b69aSJeremy L Thompson         else data->B.outputs[i] = basis_data->d_chebyshev_interp_1d;
2858b97b69aSJeremy L Thompson       } else {
2868b97b69aSJeremy L Thompson         // Standard quadrature
2874b3e95d5SJeremy L Thompson         if (is_input) data->B.inputs[i] = basis_data->d_interp_1d;
2884b3e95d5SJeremy L Thompson         else data->B.outputs[i] = basis_data->d_interp_1d;
2898b97b69aSJeremy L Thompson       }
290dc007f05SJeremy L Thompson       if (is_tensor) {
2910a2a6492SJeremy L Thompson         if (use_previous_field) {
29245a787f7SJeremy L Thompson           std::string reuse_var = "s_B" + ((field_reuse.is_input ? "_in_" : "_out_") + std::to_string(field_reuse.index));
2930a2a6492SJeremy L Thompson 
294*0183ed61SJeremy L Thompson           code << tab << "CeedScalar *s_B" << var_suffix << " = " << reuse_var << ";\n";
2950a2a6492SJeremy L Thompson         } else {
296*0183ed61SJeremy L Thompson           code << tab << "__shared__ CeedScalar s_B" << var_suffix << "[" << P_name << "*" << Q_name << "];\n";
297*0183ed61SJeremy L Thompson           code << tab << "LoadMatrix<" << P_name << ", " << Q_name << ">(data, B." << option_name << "[" << i << "], s_B" << var_suffix << ");\n";
298dc007f05SJeremy L Thompson         }
2990a2a6492SJeremy L Thompson       }
3008b97b69aSJeremy L Thompson       if (is_at_points) break;  // No G mat for AtPoints
3014b3e95d5SJeremy L Thompson       if (use_3d_slices) {
3024b3e95d5SJeremy L Thompson         if (is_input) data->G.inputs[i] = basis_data->d_collo_grad_1d;
3034b3e95d5SJeremy L Thompson         else data->G.outputs[i] = basis_data->d_collo_grad_1d;
30445a787f7SJeremy L Thompson         if (use_previous_field && field_reuse.eval_mode == CEED_EVAL_GRAD) {
30545a787f7SJeremy L Thompson           std::string reuse_var = "s_G" + ((field_reuse.is_input ? "_in_" : "_out_") + std::to_string(field_reuse.index));
3060a2a6492SJeremy L Thompson 
307*0183ed61SJeremy L Thompson           code << tab << "CeedScalar *s_G" << var_suffix << " = " << reuse_var << ";\n";
3080a2a6492SJeremy L Thompson         } else {
309*0183ed61SJeremy L Thompson           code << tab << "__shared__ CeedScalar s_G" << var_suffix << "[" << Q_name << "*" << Q_name << "];\n";
310*0183ed61SJeremy L Thompson           code << tab << "LoadMatrix<" << Q_name << ", " << Q_name << ">(data, G." << option_name << "[" << i << "], s_G" << var_suffix << ");\n";
3110a2a6492SJeremy L Thompson         }
3124b3e95d5SJeremy L Thompson       } else {
3134b3e95d5SJeremy L Thompson         bool has_collo_grad = basis_data->d_collo_grad_1d;
3144b3e95d5SJeremy L Thompson 
3154b3e95d5SJeremy L Thompson         if (is_input) data->G.inputs[i] = has_collo_grad ? basis_data->d_collo_grad_1d : basis_data->d_grad_1d;
3164b3e95d5SJeremy L Thompson         else data->G.outputs[i] = has_collo_grad ? basis_data->d_collo_grad_1d : basis_data->d_grad_1d;
3174b3e95d5SJeremy L Thompson         if (has_collo_grad) {
31845a787f7SJeremy L Thompson           if (use_previous_field && field_reuse.eval_mode == CEED_EVAL_GRAD) {
31945a787f7SJeremy L Thompson             std::string reuse_var = "s_G" + ((field_reuse.is_input ? "_in_" : "_out_") + std::to_string(field_reuse.index));
3200a2a6492SJeremy L Thompson 
321*0183ed61SJeremy L Thompson             code << tab << "CeedScalar *s_G" << var_suffix << " = " << reuse_var << ";\n";
3220a2a6492SJeremy L Thompson           } else {
323*0183ed61SJeremy L Thompson             code << tab << "__shared__ CeedScalar s_G" << var_suffix << "[" << Q_name << "*" << Q_name << "];\n";
324*0183ed61SJeremy L Thompson             code << tab << "LoadMatrix<" << Q_name << ", " << Q_name << ">(data, G." << option_name << "[" << i << "], s_G" << var_suffix << ");\n";
3250a2a6492SJeremy L Thompson           }
3260a2a6492SJeremy L Thompson         } else {
32745a787f7SJeremy L Thompson           if (use_previous_field && field_reuse.eval_mode == CEED_EVAL_GRAD) {
32845a787f7SJeremy L Thompson             std::string reuse_var = "s_G" + ((field_reuse.is_input ? "_in_" : "_out_") + std::to_string(field_reuse.index));
3290a2a6492SJeremy L Thompson 
330*0183ed61SJeremy L Thompson             code << tab << "CeedScalar *s_G" << var_suffix << " = " << reuse_var << ";\n";
3314b3e95d5SJeremy L Thompson           } else {
332*0183ed61SJeremy L Thompson             code << tab << "__shared__ CeedScalar s_G" << var_suffix << "[" << P_name << "*" << Q_name << (is_tensor ? "" : "*dim")
333412e5683SJeremy L Thompson                  << (is_tensor ? "" : var_suffix) << "];\n";
334*0183ed61SJeremy L Thompson             code << tab << "LoadMatrix<" << P_name << ", " << Q_name << (is_tensor ? "" : "*dim") << (is_tensor ? "" : var_suffix) << ">(data, G."
335412e5683SJeremy L Thompson                  << option_name << "[" << i << "], s_G" << var_suffix << ");\n";
3364b3e95d5SJeremy L Thompson           }
3374b3e95d5SJeremy L Thompson         }
3380a2a6492SJeremy L Thompson       }
3394b3e95d5SJeremy L Thompson       break;
3404b3e95d5SJeremy L Thompson     case CEED_EVAL_WEIGHT:
3414b3e95d5SJeremy L Thompson       break;  // No action
3424b3e95d5SJeremy L Thompson       // LCOV_EXCL_START
3434b3e95d5SJeremy L Thompson     case CEED_EVAL_DIV:
3444b3e95d5SJeremy L Thompson     case CEED_EVAL_CURL:
3454b3e95d5SJeremy L Thompson       break;  // TODO: Not implemented
3464b3e95d5SJeremy L Thompson               // LCOV_EXCL_STOP
3474b3e95d5SJeremy L Thompson   }
3488b97b69aSJeremy L Thompson   CeedCallBackend(CeedBasisDestroy(&basis));
3494b3e95d5SJeremy L Thompson   return CEED_ERROR_SUCCESS;
3504b3e95d5SJeremy L Thompson }
3514b3e95d5SJeremy L Thompson 
3524b3e95d5SJeremy L Thompson //------------------------------------------------------------------------------
3534b3e95d5SJeremy L Thompson // Restriction
3544b3e95d5SJeremy L Thompson //------------------------------------------------------------------------------
355*0183ed61SJeremy L Thompson static int CeedOperatorBuildKernelRestriction_Cuda_gen(std::ostringstream &code, CeedOperator_Cuda_gen *data, Tab &tab, CeedInt i,
356*0183ed61SJeremy L Thompson                                                        CeedInt field_input_buffer[], CeedOperatorField op_field, CeedQFunctionField qf_field,
357*0183ed61SJeremy L Thompson                                                        CeedInt max_dim, CeedInt Q_1d, bool is_input, bool is_all_tensor, bool is_at_points,
358*0183ed61SJeremy L Thompson                                                        bool use_3d_slices) {
3594b3e95d5SJeremy L Thompson   std::string               var_suffix = (is_input ? "_in_" : "_out_") + std::to_string(i);
360412e5683SJeremy L Thompson   std::string               P_name     = (is_all_tensor ? "P_1d" : "P") + var_suffix;
3614b3e95d5SJeremy L Thompson   CeedEvalMode              eval_mode  = CEED_EVAL_NONE;
362412e5683SJeremy L Thompson   CeedInt                   elem_size = 0, num_comp = 0;
3634b3e95d5SJeremy L Thompson   CeedSize                  l_size;
364f815fac9SJeremy L Thompson   CeedRestrictionType       rstr_type = CEED_RESTRICTION_STANDARD;
3654b3e95d5SJeremy L Thompson   CeedElemRestriction_Cuda *rstr_data;
3664b3e95d5SJeremy L Thompson   CeedElemRestriction       elem_rstr;
3674b3e95d5SJeremy L Thompson 
3684b3e95d5SJeremy L Thompson   // Get field data
3694b3e95d5SJeremy L Thompson   CeedCallBackend(CeedOperatorFieldGetElemRestriction(op_field, &elem_rstr));
3704b3e95d5SJeremy L Thompson   if (elem_rstr != CEED_ELEMRESTRICTION_NONE) {
371f815fac9SJeremy L Thompson     CeedCallBackend(CeedElemRestrictionGetType(elem_rstr, &rstr_type));
3724b3e95d5SJeremy L Thompson     CeedCallBackend(CeedElemRestrictionGetElementSize(elem_rstr, &elem_size));
3734b3e95d5SJeremy L Thompson     CeedCallBackend(CeedElemRestrictionGetNumComponents(elem_rstr, &num_comp));
3744b3e95d5SJeremy L Thompson     CeedCallBackend(CeedElemRestrictionGetData(elem_rstr, &rstr_data));
3754b3e95d5SJeremy L Thompson   }
3764b3e95d5SJeremy L Thompson   CeedCallBackend(CeedQFunctionFieldGetEvalMode(qf_field, &eval_mode));
3774b3e95d5SJeremy L Thompson 
3784b3e95d5SJeremy L Thompson   // Restriction
3794b3e95d5SJeremy L Thompson   if (is_input) {
3804b3e95d5SJeremy L Thompson     // Input
381e93651e5SJeremy L Thompson     if (field_input_buffer[i] != i) {
382e93651e5SJeremy L Thompson       std::string buffer_name = "r_e_in_" + std::to_string(field_input_buffer[i]);
383e93651e5SJeremy L Thompson 
384e93651e5SJeremy L Thompson       // Restriction was already done for previous input
385*0183ed61SJeremy L Thompson       code << tab << "CeedScalar *r_e" << var_suffix << " = " << buffer_name << ";\n";
3868b97b69aSJeremy L Thompson     } else if (eval_mode != CEED_EVAL_WEIGHT && !((eval_mode == CEED_EVAL_NONE) && use_3d_slices && is_at_points)) {
3878b97b69aSJeremy L Thompson       if (eval_mode == CEED_EVAL_NONE && rstr_type != CEED_RESTRICTION_POINTS) {
388e93651e5SJeremy L Thompson         // No basis action, so r_e_in_* in also r_q_in_* and needs to be allocated
389*0183ed61SJeremy L Thompson         code << tab << "CeedScalar r_e" << var_suffix << "[num_comp" << var_suffix << "*" << P_name << "];\n";
3908b97b69aSJeremy L Thompson       } else if (rstr_type != CEED_RESTRICTION_POINTS) {
391e93651e5SJeremy L Thompson         // Otherwise we're using the scratch space
392*0183ed61SJeremy L Thompson         code << tab << "CeedScalar *r_e" << var_suffix << " = r_e_scratch;\n";
393e93651e5SJeremy L Thompson       }
394f815fac9SJeremy L Thompson       switch (rstr_type) {
395f815fac9SJeremy L Thompson         case CEED_RESTRICTION_STANDARD: {
3964b3e95d5SJeremy L Thompson           CeedInt comp_stride;
3974b3e95d5SJeremy L Thompson 
3984b3e95d5SJeremy L Thompson           CeedCallBackend(CeedElemRestrictionGetLVectorSize(elem_rstr, &l_size));
399*0183ed61SJeremy L Thompson           code << tab << "const CeedInt l_size" << var_suffix << " = " << l_size << ";\n";
4004b3e95d5SJeremy L Thompson           CeedCallBackend(CeedElemRestrictionGetCompStride(elem_rstr, &comp_stride));
401*0183ed61SJeremy L Thompson           code << tab << "const CeedInt comp_stride" << var_suffix << " = " << comp_stride << ";\n";
4024b3e95d5SJeremy L Thompson           data->indices.inputs[i] = (CeedInt *)rstr_data->d_offsets;
403*0183ed61SJeremy L Thompson           code << tab << "ReadLVecStandard" << (is_all_tensor ? max_dim : 1) << "d<num_comp" << var_suffix << ", comp_stride" << var_suffix << ", "
404*0183ed61SJeremy L Thompson                << P_name << ">(data, l_size" << var_suffix << ", elem, indices.inputs[" << i << "], d" << var_suffix << ", r_e" << var_suffix
405*0183ed61SJeremy L Thompson                << ");\n";
406f815fac9SJeremy L Thompson           break;
407f815fac9SJeremy L Thompson         }
408f815fac9SJeremy L Thompson         case CEED_RESTRICTION_STRIDED: {
4094b3e95d5SJeremy L Thompson           bool    has_backend_strides;
4104b3e95d5SJeremy L Thompson           CeedInt num_elem;
4114b3e95d5SJeremy L Thompson 
4124b3e95d5SJeremy L Thompson           CeedCallBackend(CeedElemRestrictionHasBackendStrides(elem_rstr, &has_backend_strides));
4134b3e95d5SJeremy L Thompson           CeedCallBackend(CeedElemRestrictionGetNumElements(elem_rstr, &num_elem));
4144b3e95d5SJeremy L Thompson           CeedInt strides[3] = {1, elem_size * num_elem, elem_size};
4154b3e95d5SJeremy L Thompson 
4164b3e95d5SJeremy L Thompson           if (!has_backend_strides) {
4174b3e95d5SJeremy L Thompson             CeedCallBackend(CeedElemRestrictionGetStrides(elem_rstr, strides));
4184b3e95d5SJeremy L Thompson           }
419*0183ed61SJeremy L Thompson           code << tab << "const CeedInt strides" << var_suffix << "_0 = " << strides[0] << ", strides" << var_suffix << "_1 = " << strides[1]
420*0183ed61SJeremy L Thompson                << ", strides" << var_suffix << "_2 = " << strides[2] << ";\n";
421*0183ed61SJeremy L Thompson           code << tab << "ReadLVecStrided" << (is_all_tensor ? max_dim : 1) << "d<num_comp" << var_suffix << ", " << P_name << ", strides"
422*0183ed61SJeremy L Thompson                << var_suffix << "_0, strides" << var_suffix << "_1, strides" << var_suffix << "_2>(data, elem, d" << var_suffix << ", r_e"
423*0183ed61SJeremy L Thompson                << var_suffix << ");\n";
424f815fac9SJeremy L Thompson           break;
425f815fac9SJeremy L Thompson         }
4268b97b69aSJeremy L Thompson         case CEED_RESTRICTION_POINTS: {
4278b97b69aSJeremy L Thompson           CeedInt comp_stride;
4288b97b69aSJeremy L Thompson 
4298b97b69aSJeremy L Thompson           CeedCallBackend(CeedElemRestrictionGetCompStride(elem_rstr, &comp_stride));
430*0183ed61SJeremy L Thompson           code << tab << "const CeedInt comp_stride" << var_suffix << " = " << comp_stride << ";\n";
4318b97b69aSJeremy L Thompson           data->indices.inputs[i] = (CeedInt *)rstr_data->d_offsets;
4328b97b69aSJeremy L Thompson           break;
4338b97b69aSJeremy L Thompson         }
434f815fac9SJeremy L Thompson         // LCOV_EXCL_START
435f815fac9SJeremy L Thompson         case CEED_RESTRICTION_ORIENTED:
436f815fac9SJeremy L Thompson         case CEED_RESTRICTION_CURL_ORIENTED:
437f815fac9SJeremy L Thompson           break;  // TODO: Not implemented
438f815fac9SJeremy L Thompson                   // LCOV_EXCL_STOP
4394b3e95d5SJeremy L Thompson       }
4404b3e95d5SJeremy L Thompson     }
4414b3e95d5SJeremy L Thompson   } else {
4424b3e95d5SJeremy L Thompson     // Output
443f815fac9SJeremy L Thompson     switch (rstr_type) {
444f815fac9SJeremy L Thompson       case CEED_RESTRICTION_STANDARD: {
4454b3e95d5SJeremy L Thompson         CeedInt comp_stride;
4464b3e95d5SJeremy L Thompson 
4474b3e95d5SJeremy L Thompson         CeedCallBackend(CeedElemRestrictionGetLVectorSize(elem_rstr, &l_size));
448*0183ed61SJeremy L Thompson         code << tab << "const CeedInt l_size" << var_suffix << " = " << l_size << ";\n";
4494b3e95d5SJeremy L Thompson         CeedCallBackend(CeedElemRestrictionGetCompStride(elem_rstr, &comp_stride));
450*0183ed61SJeremy L Thompson         code << tab << "const CeedInt comp_stride" << var_suffix << " = " << comp_stride << ";\n";
4514b3e95d5SJeremy L Thompson         data->indices.outputs[i] = (CeedInt *)rstr_data->d_offsets;
452*0183ed61SJeremy L Thompson         code << tab << "WriteLVecStandard" << (is_all_tensor ? max_dim : 1) << "d<num_comp" << var_suffix << ", comp_stride" << var_suffix << ", "
453*0183ed61SJeremy L Thompson              << P_name << ">(data, l_size" << var_suffix << ", elem, indices.outputs[" << i << "], r_e" << var_suffix << ", d" << var_suffix
454*0183ed61SJeremy L Thompson              << ");\n";
455f815fac9SJeremy L Thompson         break;
456f815fac9SJeremy L Thompson       }
457f815fac9SJeremy L Thompson       case CEED_RESTRICTION_STRIDED: {
4584b3e95d5SJeremy L Thompson         bool    has_backend_strides;
4594b3e95d5SJeremy L Thompson         CeedInt num_elem;
4604b3e95d5SJeremy L Thompson 
4614b3e95d5SJeremy L Thompson         CeedCallBackend(CeedElemRestrictionHasBackendStrides(elem_rstr, &has_backend_strides));
4624b3e95d5SJeremy L Thompson         CeedCallBackend(CeedElemRestrictionGetNumElements(elem_rstr, &num_elem));
4634b3e95d5SJeremy L Thompson         CeedInt strides[3] = {1, elem_size * num_elem, elem_size};
4644b3e95d5SJeremy L Thompson 
4654b3e95d5SJeremy L Thompson         if (!has_backend_strides) {
4664b3e95d5SJeremy L Thompson           CeedCallBackend(CeedElemRestrictionGetStrides(elem_rstr, strides));
4674b3e95d5SJeremy L Thompson         }
468*0183ed61SJeremy L Thompson         code << tab << "const CeedInt strides" << var_suffix << "_0 = " << strides[0] << ", strides" << var_suffix << "_1 = " << strides[1]
469*0183ed61SJeremy L Thompson              << ", strides" << var_suffix << "_2 = " << strides[2] << ";\n";
470*0183ed61SJeremy L Thompson         code << tab << "WriteLVecStrided" << (is_all_tensor ? max_dim : 1) << "d<num_comp" << var_suffix << ", " << P_name << ", strides"
471*0183ed61SJeremy L Thompson              << var_suffix << "_0, strides" << var_suffix << "_1, strides" << var_suffix << "_2>(data, elem, r_e" << var_suffix << ", d" << var_suffix
472*0183ed61SJeremy L Thompson              << ");\n";
473f815fac9SJeremy L Thompson         break;
474f815fac9SJeremy L Thompson       }
4758b97b69aSJeremy L Thompson       case CEED_RESTRICTION_POINTS:
4768b97b69aSJeremy L Thompson         data->indices.outputs[i] = (CeedInt *)rstr_data->d_offsets;
4778b97b69aSJeremy L Thompson         break;
478f815fac9SJeremy L Thompson       // LCOV_EXCL_START
479f815fac9SJeremy L Thompson       case CEED_RESTRICTION_ORIENTED:
480f815fac9SJeremy L Thompson       case CEED_RESTRICTION_CURL_ORIENTED:
481f815fac9SJeremy L Thompson         break;  // TODO: Not implemented
482f815fac9SJeremy L Thompson                 // LCOV_EXCL_STOP
4834b3e95d5SJeremy L Thompson     }
4844b3e95d5SJeremy L Thompson   }
485681d0ea7SJeremy L Thompson   CeedCallBackend(CeedElemRestrictionDestroy(&elem_rstr));
4864b3e95d5SJeremy L Thompson   return CEED_ERROR_SUCCESS;
4874b3e95d5SJeremy L Thompson }
4884b3e95d5SJeremy L Thompson 
4894b3e95d5SJeremy L Thompson //------------------------------------------------------------------------------
4904b3e95d5SJeremy L Thompson // Basis
4914b3e95d5SJeremy L Thompson //------------------------------------------------------------------------------
492*0183ed61SJeremy L Thompson static int CeedOperatorBuildKernelBasis_Cuda_gen(std::ostringstream &code, CeedOperator_Cuda_gen *data, Tab &tab, CeedInt i,
493*0183ed61SJeremy L Thompson                                                  CeedOperatorField op_field, CeedQFunctionField qf_field, CeedInt max_dim, CeedInt Q_1d,
494*0183ed61SJeremy L Thompson                                                  bool is_input, bool is_all_tensor, bool is_at_points, bool use_3d_slices) {
495412e5683SJeremy L Thompson   bool      is_tensor = true;
496412e5683SJeremy L Thompson   CeedBasis basis;
497412e5683SJeremy L Thompson   CeedCallBackend(CeedOperatorFieldGetBasis(op_field, &basis));
498412e5683SJeremy L Thompson   CeedCallBackend(CeedBasisIsTensor(basis, &is_tensor));
499412e5683SJeremy L Thompson 
5004b3e95d5SJeremy L Thompson   std::string         var_suffix = (is_input ? "_in_" : "_out_") + std::to_string(i);
501dc007f05SJeremy L Thompson   std::string         P_name = (is_tensor ? "P_1d" : "P") + var_suffix, Q_name = is_tensor ? "Q_1d" : "Q";
5024b3e95d5SJeremy L Thompson   CeedEvalMode        eval_mode = CEED_EVAL_NONE;
503412e5683SJeremy L Thompson   CeedInt             dim = max_dim, elem_size = 0, num_comp = 0, P_1d = 0;
5044b3e95d5SJeremy L Thompson   CeedElemRestriction elem_rstr;
5054b3e95d5SJeremy L Thompson 
5064b3e95d5SJeremy L Thompson   // Get field data
5074b3e95d5SJeremy L Thompson   CeedCallBackend(CeedOperatorFieldGetElemRestriction(op_field, &elem_rstr));
5084b3e95d5SJeremy L Thompson   if (elem_rstr != CEED_ELEMRESTRICTION_NONE) {
5094b3e95d5SJeremy L Thompson     CeedCallBackend(CeedElemRestrictionGetElementSize(elem_rstr, &elem_size));
5104b3e95d5SJeremy L Thompson     CeedCallBackend(CeedElemRestrictionGetNumComponents(elem_rstr, &num_comp));
5114b3e95d5SJeremy L Thompson   }
512681d0ea7SJeremy L Thompson   CeedCallBackend(CeedElemRestrictionDestroy(&elem_rstr));
5134b3e95d5SJeremy L Thompson   if (basis != CEED_BASIS_NONE) {
514412e5683SJeremy L Thompson     CeedCallBackend(CeedBasisGetDimension(basis, &dim));
515dc007f05SJeremy L Thompson     if (is_tensor) CeedCallBackend(CeedBasisGetNumNodes1D(basis, &P_1d));
516dc007f05SJeremy L Thompson     else CeedCallBackend(CeedBasisGetNumNodes(basis, &P_1d));
5174b3e95d5SJeremy L Thompson   }
5184b3e95d5SJeremy L Thompson   CeedCallBackend(CeedQFunctionFieldGetEvalMode(qf_field, &eval_mode));
5194b3e95d5SJeremy L Thompson 
5204b3e95d5SJeremy L Thompson   // Basis
521*0183ed61SJeremy L Thompson   code << tab << "// EvalMode: " << CeedEvalModes[eval_mode] << "\n";
5224b3e95d5SJeremy L Thompson   if (is_input) {
5234b3e95d5SJeremy L Thompson     switch (eval_mode) {
5244b3e95d5SJeremy L Thompson       case CEED_EVAL_NONE:
5258b97b69aSJeremy L Thompson         if (!use_3d_slices && !is_at_points) {
526*0183ed61SJeremy L Thompson           code << tab << "CeedScalar *r_q" << var_suffix << " = r_e" << var_suffix << ";\n";
5274b3e95d5SJeremy L Thompson         }
5284b3e95d5SJeremy L Thompson         break;
5294b3e95d5SJeremy L Thompson       case CEED_EVAL_INTERP:
5308b97b69aSJeremy L Thompson         if (is_at_points) {
531dc007f05SJeremy L Thompson           std::string function_name = (dim == 1 ? "Interp" : "InterpTensor") + std::to_string(dim) + "d";
532dc007f05SJeremy L Thompson 
533*0183ed61SJeremy L Thompson           code << tab << "CeedScalar r_c" << var_suffix << "[num_comp" << var_suffix << "*" << (dim >= 3 ? Q_name : "1") << "];\n";
534*0183ed61SJeremy L Thompson           code << tab << function_name << "<num_comp" << var_suffix << ", " << P_name << ", " << Q_name << ", OP_T_1D>(data, r_e" << var_suffix
53599421279SJeremy L Thompson                << ", s_B" << var_suffix << ", r_c" << var_suffix << ");\n";
5368b97b69aSJeremy L Thompson         } else {
537412e5683SJeremy L Thompson           std::string function_name = is_tensor
538412e5683SJeremy L Thompson                                           ? ((dim == 1 ? "Interp" : "InterpTensor") + std::to_string(dim) + "d" + (is_all_tensor ? "" : "Flattened"))
539412e5683SJeremy L Thompson                                           : "InterpNonTensor";
540c433aabcSJeremy L Thompson           std::string op_t_1d_name  = (is_all_tensor || !is_tensor) ? "OP_T_1D" : (P_1d > Q_1d ? P_name : Q_name);
541dc007f05SJeremy L Thompson 
542*0183ed61SJeremy L Thompson           code << tab << "CeedScalar r_q" << var_suffix << "[num_comp" << var_suffix << "*" << (is_all_tensor && (dim >= 3) ? Q_name : "1") << "];\n";
543*0183ed61SJeremy L Thompson           code << tab << function_name << "<num_comp" << var_suffix << ", " << P_name << ", " << Q_name << ", " << op_t_1d_name << ">(data, r_e"
544c433aabcSJeremy L Thompson                << var_suffix << ", s_B" << var_suffix << ", r_q" << var_suffix << ");\n";
5458b97b69aSJeremy L Thompson         }
5464b3e95d5SJeremy L Thompson         break;
5474b3e95d5SJeremy L Thompson       case CEED_EVAL_GRAD:
5488b97b69aSJeremy L Thompson         if (is_at_points) {
549dc007f05SJeremy L Thompson           std::string function_name = (dim == 1 ? "Interp" : "InterpTensor") + std::to_string(dim) + "d";
550dc007f05SJeremy L Thompson 
551*0183ed61SJeremy L Thompson           code << tab << "CeedScalar r_c" << var_suffix << "[num_comp" << var_suffix << "*" << (dim >= 3 ? Q_name : "1") << "];\n";
552*0183ed61SJeremy L Thompson           code << tab << function_name << "<num_comp" << var_suffix << ", " << P_name << ", " << Q_name << ", OP_T_1D>(data, r_e" << var_suffix
55399421279SJeremy L Thompson                << ", s_B" << var_suffix << ", r_c" << var_suffix << ");\n";
5548b97b69aSJeremy L Thompson         } else if (use_3d_slices) {
555dc007f05SJeremy L Thompson           std::string function_name = (dim > 1 ? "InterpTensor" : "Interp") + std::to_string(dim) + "d";
556dc007f05SJeremy L Thompson 
557*0183ed61SJeremy L Thompson           code << tab << "CeedScalar r_q" << var_suffix << "[num_comp" << var_suffix << "*" << Q_name << "];\n";
558*0183ed61SJeremy L Thompson           code << tab << function_name << "<num_comp" << var_suffix << ", " << P_name << ", " << Q_name << ", OP_T_1D>(data, r_e" << var_suffix
55999421279SJeremy L Thompson                << ", s_B" << var_suffix << ", r_q" << var_suffix << ");\n";
560dc007f05SJeremy L Thompson         } else if (is_tensor) {
561dc007f05SJeremy L Thompson           bool        is_collocated = dim == 3 && Q_1d >= P_1d;
562412e5683SJeremy L Thompson           std::string function_name = (dim == 1 ? "Grad" : (is_collocated ? "GradTensorCollocated" : "GradTensor")) + std::to_string(dim) + "d" +
563412e5683SJeremy L Thompson                                       (is_all_tensor ? "" : "Flattened");
564c433aabcSJeremy L Thompson           std::string op_t_1d_name = is_all_tensor ? "OP_T_1D" : (P_1d > Q_1d ? P_name : Q_name);
565dc007f05SJeremy L Thompson 
566*0183ed61SJeremy L Thompson           code << tab << "CeedScalar r_q" << var_suffix << "[num_comp" << var_suffix << "*dim" << var_suffix << "*"
567259057edSJeremy L Thompson                << (is_all_tensor && dim >= 3 ? Q_name : "1") << "];\n";
568*0183ed61SJeremy L Thompson           code << tab << function_name << "<num_comp" << var_suffix << ", " << P_name << ", " << Q_name << ", " << op_t_1d_name << ">(data, r_e"
569c433aabcSJeremy L Thompson                << var_suffix << ", s_B" << var_suffix << ", s_G" << var_suffix << ", r_q" << var_suffix << ");\n";
5704b3e95d5SJeremy L Thompson         } else {
571dc007f05SJeremy L Thompson           std::string function_name = "GradNonTensor";
572dc007f05SJeremy L Thompson 
573*0183ed61SJeremy L Thompson           code << tab << "CeedScalar r_q" << var_suffix << "[num_comp" << var_suffix << "*dim" << var_suffix << "];\n";
574*0183ed61SJeremy L Thompson           code << tab << function_name << "<num_comp" << var_suffix << ", dim" << var_suffix << ", " << P_name << ", " << Q_name
575c433aabcSJeremy L Thompson                << ", OP_T_1D>(data, r_e" << var_suffix << ", s_G" << var_suffix << ", r_q" << var_suffix << ");\n";
5764b3e95d5SJeremy L Thompson         }
5774b3e95d5SJeremy L Thompson         break;
5784b3e95d5SJeremy L Thompson       case CEED_EVAL_WEIGHT: {
5798b97b69aSJeremy L Thompson         if (is_at_points) {
580*0183ed61SJeremy L Thompson           code << tab << "// Nothing to do AtPoints\n";
5818b97b69aSJeremy L Thompson         } else {
5824b3e95d5SJeremy L Thompson           CeedBasis_Cuda_shared *basis_data;
583412e5683SJeremy L Thompson           std::string            function_name = is_tensor
584412e5683SJeremy L Thompson                                                      ? ((dim == 1 ? "Weight" : "WeightTensor") + std::to_string(dim) + "d" + (is_all_tensor ? "" : "Flattened"))
585412e5683SJeremy L Thompson                                                      : "WeightNonTensor";
5864b3e95d5SJeremy L Thompson 
587*0183ed61SJeremy L Thompson           code << tab << "CeedScalar r_q" << var_suffix << "[" << (is_all_tensor && (dim >= 3) ? Q_name : "1") << "];\n";
5884b3e95d5SJeremy L Thompson           CeedCallBackend(CeedBasisGetData(basis, &basis_data));
5894b3e95d5SJeremy L Thompson           data->W = basis_data->d_q_weight_1d;
590*0183ed61SJeremy L Thompson           code << tab << function_name << "<" << P_name << ", " << Q_name << ">(data, W, r_q" << var_suffix << ");\n";
5918b97b69aSJeremy L Thompson         }
5924b3e95d5SJeremy L Thompson         break;
5934b3e95d5SJeremy L Thompson       }
5944b3e95d5SJeremy L Thompson       // LCOV_EXCL_START
5954b3e95d5SJeremy L Thompson       case CEED_EVAL_DIV:
5964b3e95d5SJeremy L Thompson       case CEED_EVAL_CURL:
5974b3e95d5SJeremy L Thompson         break;  // TODO: Not implemented
5984b3e95d5SJeremy L Thompson                 // LCOV_EXCL_STOP
5994b3e95d5SJeremy L Thompson     }
6004b3e95d5SJeremy L Thompson   } else {
6014b3e95d5SJeremy L Thompson     switch (eval_mode) {
6024b3e95d5SJeremy L Thompson       case CEED_EVAL_NONE:
603*0183ed61SJeremy L Thompson         code << tab << "CeedScalar *r_e" << var_suffix << " = r_q" << var_suffix << ";\n";
6044b3e95d5SJeremy L Thompson         break;  // No action
6054b3e95d5SJeremy L Thompson       case CEED_EVAL_INTERP:
606*0183ed61SJeremy L Thompson         code << tab << "CeedScalar *r_e" << var_suffix << " = r_e_scratch;\n";
6078b97b69aSJeremy L Thompson         if (is_at_points) {
608dc007f05SJeremy L Thompson           std::string function_name = (dim == 1 ? "InterpTranspose" : "InterpTransposeTensor") + std::to_string(dim) + "d";
609dc007f05SJeremy L Thompson 
610*0183ed61SJeremy L Thompson           code << tab << function_name << "<num_comp" << var_suffix << ", " << P_name << ", " << Q_name << ", OP_T_1D>(data, r_c" << var_suffix
61199421279SJeremy L Thompson                << ", s_B" << var_suffix << ", r_e" << var_suffix << ");\n";
6128b97b69aSJeremy L Thompson         } else {
613dc007f05SJeremy L Thompson           std::string function_name =
614412e5683SJeremy L Thompson               is_tensor ? ((dim == 1 ? "InterpTranspose" : "InterpTransposeTensor") + std::to_string(dim) + "d" + (is_all_tensor ? "" : "Flattened"))
615412e5683SJeremy L Thompson                         : "InterpTransposeNonTensor";
616c433aabcSJeremy L Thompson           std::string op_t_1d_name = (is_all_tensor || !is_tensor) ? "OP_T_1D" : (P_1d > Q_1d ? P_name : Q_name);
617dc007f05SJeremy L Thompson 
618*0183ed61SJeremy L Thompson           code << tab << function_name << "<num_comp" << var_suffix << ", " << P_name << ", " << Q_name << ", " << op_t_1d_name << ">(data, r_q"
619c433aabcSJeremy L Thompson                << var_suffix << ", s_B" << var_suffix << ", r_e" << var_suffix << ");\n";
6208b97b69aSJeremy L Thompson         }
6214b3e95d5SJeremy L Thompson         break;
6224b3e95d5SJeremy L Thompson       case CEED_EVAL_GRAD:
623*0183ed61SJeremy L Thompson         code << tab << "CeedScalar *r_e" << var_suffix << " = r_e_scratch;\n";
6248b97b69aSJeremy L Thompson         if (is_at_points) {
625dc007f05SJeremy L Thompson           std::string function_name = (dim == 1 ? "InterpTranspose" : "InterpTransposeTensor") + std::to_string(dim) + "d";
626dc007f05SJeremy L Thompson 
627*0183ed61SJeremy L Thompson           code << tab << function_name << "<num_comp" << var_suffix << ", " << P_name << ", " << Q_name << ", OP_T_1D>(data, r_c" << var_suffix
62899421279SJeremy L Thompson                << ", s_B" << var_suffix << ", r_e" << var_suffix << ");\n";
6298b97b69aSJeremy L Thompson         } else if (use_3d_slices) {
630dc007f05SJeremy L Thompson           std::string function_name = (dim == 1 ? "InterpTranspose" : "InterpTransposeTensor") + std::to_string(dim) + "d";
631dc007f05SJeremy L Thompson 
632*0183ed61SJeremy L Thompson           code << tab << function_name << "<num_comp" << var_suffix << ", " << P_name << ", " << Q_name << ", OP_T_1D>(data, r_q" << var_suffix
63399421279SJeremy L Thompson                << ", s_B" << var_suffix << ", r_e" << var_suffix << ");\n";
634dc007f05SJeremy L Thompson         } else if (is_tensor) {
635dc007f05SJeremy L Thompson           bool        is_collocated = dim == 3 && Q_1d >= P_1d;
636412e5683SJeremy L Thompson           std::string function_name = (dim == 1 ? "GradTranspose" : (is_collocated ? "GradTransposeTensorCollocated" : "GradTransposeTensor")) +
637412e5683SJeremy L Thompson                                       std::to_string(dim) + "d" + (is_all_tensor ? "" : "Flattened");
638c433aabcSJeremy L Thompson           std::string op_t_1d_name = is_all_tensor ? "OP_T_1D" : (P_1d > Q_1d ? P_name : Q_name);
639dc007f05SJeremy L Thompson 
640*0183ed61SJeremy L Thompson           code << tab << function_name << "<num_comp" << var_suffix << ", " << P_name << ", " << Q_name << ", " << op_t_1d_name << ">(data, r_q"
641c433aabcSJeremy L Thompson                << var_suffix << ", s_B" << var_suffix << ", s_G" << var_suffix << ", r_e" << var_suffix << ");\n";
6424b3e95d5SJeremy L Thompson         } else {
643dc007f05SJeremy L Thompson           std::string function_name = "GradTransposeNonTensor";
644dc007f05SJeremy L Thompson 
645*0183ed61SJeremy L Thompson           code << tab << function_name << "<num_comp" << var_suffix << ", dim" << var_suffix << ", " << P_name << ", " << Q_name
646c433aabcSJeremy L Thompson                << ", OP_T_1D>(data, r_q" << var_suffix << ", s_G" << var_suffix << ", r_e" << var_suffix << ");\n";
6474b3e95d5SJeremy L Thompson         }
6484b3e95d5SJeremy L Thompson         break;
6494b3e95d5SJeremy L Thompson       // LCOV_EXCL_START
6504b3e95d5SJeremy L Thompson       case CEED_EVAL_WEIGHT:
6514b3e95d5SJeremy L Thompson         break;  // Should not occur
6524b3e95d5SJeremy L Thompson       case CEED_EVAL_DIV:
6534b3e95d5SJeremy L Thompson       case CEED_EVAL_CURL:
6544b3e95d5SJeremy L Thompson         break;  // TODO: Not implemented
6554b3e95d5SJeremy L Thompson                 // LCOV_EXCL_STOP
6564b3e95d5SJeremy L Thompson     }
6574b3e95d5SJeremy L Thompson   }
658681d0ea7SJeremy L Thompson   CeedCallBackend(CeedBasisDestroy(&basis));
6594b3e95d5SJeremy L Thompson   return CEED_ERROR_SUCCESS;
6604b3e95d5SJeremy L Thompson }
6614b3e95d5SJeremy L Thompson 
6624b3e95d5SJeremy L Thompson //------------------------------------------------------------------------------
6634b3e95d5SJeremy L Thompson // QFunction
6644b3e95d5SJeremy L Thompson //------------------------------------------------------------------------------
665*0183ed61SJeremy L Thompson static int CeedOperatorBuildKernelQFunction_Cuda_gen(std::ostringstream &code, CeedOperator_Cuda_gen *data, Tab &tab, CeedInt max_dim,
666*0183ed61SJeremy L Thompson                                                      CeedInt max_num_points, CeedInt num_input_fields, CeedOperatorField *op_input_fields,
6678b97b69aSJeremy L Thompson                                                      CeedQFunctionField *qf_input_fields, CeedInt num_output_fields,
6688b97b69aSJeremy L Thompson                                                      CeedOperatorField *op_output_fields, CeedQFunctionField *qf_output_fields,
669412e5683SJeremy L Thompson                                                      std::string qfunction_name, CeedInt Q_1d, bool is_all_tensor, bool is_at_points,
670dc007f05SJeremy L Thompson                                                      bool use_3d_slices) {
671412e5683SJeremy L Thompson   std::string         Q_name    = is_all_tensor ? "Q_1d" : "Q";
6724b3e95d5SJeremy L Thompson   CeedEvalMode        eval_mode = CEED_EVAL_NONE;
6734b3e95d5SJeremy L Thompson   CeedElemRestriction elem_rstr;
6744b3e95d5SJeremy L Thompson 
6758b97b69aSJeremy L Thompson   // Setup output arrays
676*0183ed61SJeremy L Thompson   code << "\n";
677*0183ed61SJeremy L Thompson   code << tab << "// -- Output field setup\n";
6784b3e95d5SJeremy L Thompson   for (CeedInt i = 0; i < num_output_fields; i++) {
67959fa3f92SJeremy L Thompson     const char *field_name;
6804b3e95d5SJeremy L Thompson     std::string var_suffix = "_out_" + std::to_string(i);
6814b3e95d5SJeremy L Thompson 
68259fa3f92SJeremy L Thompson     CeedCallBackend(CeedOperatorFieldGetName(op_output_fields[i], &field_name));
683*0183ed61SJeremy L Thompson     code << tab << "// ---- Output field " << i << ": " << field_name << "\n";
6844b3e95d5SJeremy L Thompson     CeedCallBackend(CeedQFunctionFieldGetEvalMode(qf_output_fields[i], &eval_mode));
6858b97b69aSJeremy L Thompson     switch (eval_mode) {
6868b97b69aSJeremy L Thompson       case CEED_EVAL_NONE:
6878b97b69aSJeremy L Thompson         if (is_at_points) {
688*0183ed61SJeremy L Thompson           code << tab << "CeedScalar r_q" << var_suffix << "[num_comp" << var_suffix << "];\n";
6898b97b69aSJeremy L Thompson         } else {
690*0183ed61SJeremy L Thompson           code << tab << "CeedScalar r_q" << var_suffix << "[num_comp" << var_suffix << "*" << (is_all_tensor && (max_dim >= 3) ? Q_name : "1")
691412e5683SJeremy L Thompson                << "];\n";
6924b3e95d5SJeremy L Thompson         }
6938b97b69aSJeremy L Thompson         break;
6948b97b69aSJeremy L Thompson       case CEED_EVAL_INTERP:
6958b97b69aSJeremy L Thompson         if (is_at_points) {
6968b97b69aSJeremy L Thompson           // Accumulator for point data
697*0183ed61SJeremy L Thompson           code << tab << "CeedScalar r_c" << var_suffix << "[num_comp" << var_suffix << "*" << (max_dim >= 3 ? Q_name : "1") << "];\n";
698*0183ed61SJeremy L Thompson           code << tab << "for (CeedInt i = 0; i < num_comp" << var_suffix << "*" << (max_dim >= 3 ? Q_name : "1") << "; i++) r_c" << var_suffix
699b8245c6cSJeremy L Thompson                << "[i] = 0.0;\n";
7008b97b69aSJeremy L Thompson         } else {
701*0183ed61SJeremy L Thompson           code << tab << "CeedScalar r_q" << var_suffix << "[num_comp" << var_suffix << "*" << (is_all_tensor && (max_dim >= 3) ? Q_name : "1")
702412e5683SJeremy L Thompson                << "];\n";
7038b97b69aSJeremy L Thompson         }
7048b97b69aSJeremy L Thompson         break;
7058b97b69aSJeremy L Thompson       case CEED_EVAL_GRAD:
7068b97b69aSJeremy L Thompson         if (is_at_points) {
7078b97b69aSJeremy L Thompson           // Accumulator for point data
708*0183ed61SJeremy L Thompson           code << tab << "CeedScalar r_c" << var_suffix << "[num_comp" << var_suffix << "*" << (max_dim >= 3 ? Q_name : "1") << "];\n";
709*0183ed61SJeremy L Thompson           code << tab << "for (CeedInt i = 0; i < num_comp" << var_suffix << "*" << (max_dim >= 3 ? Q_name : "1") << "; i++) r_c" << var_suffix
710b8245c6cSJeremy L Thompson                << "[i] = 0.0;\n";
7118b97b69aSJeremy L Thompson         } else if (use_3d_slices) {
7124b3e95d5SJeremy L Thompson           // Accumulator for gradient slices
713*0183ed61SJeremy L Thompson           code << tab << "CeedScalar r_q" << var_suffix << "[num_comp" << var_suffix << "*" << Q_name << "];\n";
714*0183ed61SJeremy L Thompson           code << tab << "for (CeedInt i = 0; i < num_comp" << var_suffix << "*" << Q_name << "; i++) r_q" << var_suffix << "[i] = 0.0;\n";
7154b3e95d5SJeremy L Thompson         } else {
716*0183ed61SJeremy L Thompson           code << tab << "CeedScalar r_q" << var_suffix << "[num_comp" << var_suffix << "*dim" << var_suffix << "*"
717412e5683SJeremy L Thompson                << (is_all_tensor && (max_dim >= 3) ? Q_name : "1") << "];\n";
7184b3e95d5SJeremy L Thompson         }
7198b97b69aSJeremy L Thompson         break;
7208b97b69aSJeremy L Thompson       case CEED_EVAL_WEIGHT:
7218b97b69aSJeremy L Thompson         break;
7228b97b69aSJeremy L Thompson         // LCOV_EXCL_START
7238b97b69aSJeremy L Thompson       case CEED_EVAL_DIV:
7248b97b69aSJeremy L Thompson       case CEED_EVAL_CURL:
7258b97b69aSJeremy L Thompson         break;  // TODO: Not implemented
7268b97b69aSJeremy L Thompson                 // LCOV_EXCL_STOP
7274b3e95d5SJeremy L Thompson     }
7284b3e95d5SJeremy L Thompson   }
7294b3e95d5SJeremy L Thompson 
7308b97b69aSJeremy L Thompson   if (is_at_points) {
7318b97b69aSJeremy L Thompson     // We need to handle batches of points
732*0183ed61SJeremy L Thompson     code << "\n";
733*0183ed61SJeremy L Thompson     code << tab << "// Note: Using batches of points\n";
734*0183ed61SJeremy L Thompson     code << tab << "const CeedInt point_loop_bound = (blockDim.x*blockDim.y) * ceil((1.0*max_num_points) / (blockDim.x*blockDim.y));\n\n";
735*0183ed61SJeremy L Thompson     code << tab << "#pragma unroll\n";
736*0183ed61SJeremy L Thompson     code << tab << "for (CeedInt i = threadIdx.x + threadIdx.y*blockDim.x; i < point_loop_bound; i += blockDim.x*blockDim.y) {\n";
737*0183ed61SJeremy L Thompson     tab.push();
738*0183ed61SJeremy L Thompson     code << tab << "const CeedInt p = i % max_num_points;\n\n";
7398b97b69aSJeremy L Thompson 
740*0183ed61SJeremy L Thompson     code << tab << "// -- Coordinates\n";
741*0183ed61SJeremy L Thompson     code << tab << "CeedScalar r_x[max_dim];\n";
742*0183ed61SJeremy L Thompson     code << tab << "ReadPoint<max_dim, coords_comp_stride, max_num_points>(data, elem, p, max_num_points, points.indices, points.coords, r_x);\n\n";
7438b97b69aSJeremy L Thompson 
744*0183ed61SJeremy L Thompson     code << tab << "// -- Input fields\n";
7458b97b69aSJeremy L Thompson     for (CeedInt i = 0; i < num_input_fields; i++) {
74659fa3f92SJeremy L Thompson       const char *field_name;
7478b97b69aSJeremy L Thompson       std::string var_suffix = "_in_" + std::to_string(i);
748f725b54bSJeremy L Thompson       std::string P_name     = "P_1d" + var_suffix;
7498b97b69aSJeremy L Thompson 
75059fa3f92SJeremy L Thompson       CeedCallBackend(CeedOperatorFieldGetName(op_input_fields[i], &field_name));
751*0183ed61SJeremy L Thompson       code << tab << "// ---- Input field " << i << ": " << field_name << "\n";
7528b97b69aSJeremy L Thompson       CeedCallBackend(CeedQFunctionFieldGetEvalMode(qf_input_fields[i], &eval_mode));
7538b97b69aSJeremy L Thompson       // Basis action
754*0183ed61SJeremy L Thompson       code << tab << "// EvalMode: " << CeedEvalModes[eval_mode] << "\n";
7558b97b69aSJeremy L Thompson       switch (eval_mode) {
7568b97b69aSJeremy L Thompson         case CEED_EVAL_NONE:
757*0183ed61SJeremy L Thompson           code << tab << "CeedScalar r_s" << var_suffix << "[num_comp" << var_suffix << "];\n";
758*0183ed61SJeremy L Thompson           code << tab << "ReadPoint<num_comp" << var_suffix << ", comp_stride" << var_suffix
7598b97b69aSJeremy L Thompson                << ", max_num_points>(data, elem, p, max_num_points, indices.inputs[" << i << "], d" << var_suffix << ", r_s" << var_suffix << ");\n";
7608b97b69aSJeremy L Thompson           break;
7618b97b69aSJeremy L Thompson         case CEED_EVAL_INTERP:
762*0183ed61SJeremy L Thompson           code << tab << "CeedScalar r_s" << var_suffix << "[num_comp" << var_suffix << "];\n";
763*0183ed61SJeremy L Thompson           code << tab << "InterpAtPoints" << max_dim << "d<num_comp" << var_suffix << ", max_num_points, " << P_name << ", " << Q_name
764412e5683SJeremy L Thompson                << ">(data, i, r_c" << var_suffix << ", r_x, r_s" << var_suffix << ");\n";
7658b97b69aSJeremy L Thompson           break;
7668b97b69aSJeremy L Thompson         case CEED_EVAL_GRAD:
767*0183ed61SJeremy L Thompson           code << tab << "CeedScalar r_s" << var_suffix << "[num_comp" << var_suffix << "*dim" << var_suffix << "];\n";
768*0183ed61SJeremy L Thompson           code << tab << "GradAtPoints" << max_dim << "d<num_comp" << var_suffix << ", max_num_points, " << P_name << ", " << Q_name
769412e5683SJeremy L Thompson                << ">(data, i, r_c" << var_suffix << ", r_x, r_s" << var_suffix << ");\n";
7708b97b69aSJeremy L Thompson           break;
7718b97b69aSJeremy L Thompson         case CEED_EVAL_WEIGHT:
772*0183ed61SJeremy L Thompson           code << tab << "CeedScalar r_s" << var_suffix << "[1];\n";
773*0183ed61SJeremy L Thompson           code << tab << "r_s" << var_suffix << "[0] = 1.0;\n";
7748b97b69aSJeremy L Thompson           break;
7758b97b69aSJeremy L Thompson           // LCOV_EXCL_START
7768b97b69aSJeremy L Thompson         case CEED_EVAL_DIV:
7778b97b69aSJeremy L Thompson         case CEED_EVAL_CURL:
7788b97b69aSJeremy L Thompson           break;  // TODO: Not implemented
7798b97b69aSJeremy L Thompson                   // LCOV_EXCL_STOP
7808b97b69aSJeremy L Thompson       }
7818b97b69aSJeremy L Thompson     }
782*0183ed61SJeremy L Thompson     code << "\n";
783*0183ed61SJeremy L Thompson     code << tab << "// -- Output fields\n";
7848b97b69aSJeremy L Thompson     for (CeedInt i = 0; i < num_output_fields; i++) {
78559fa3f92SJeremy L Thompson       const char *field_name;
7868b97b69aSJeremy L Thompson       std::string var_suffix = "_out_" + std::to_string(i);
7878b97b69aSJeremy L Thompson 
78859fa3f92SJeremy L Thompson       CeedCallBackend(CeedOperatorFieldGetName(op_output_fields[i], &field_name));
789*0183ed61SJeremy L Thompson       code << tab << "// ---- Output field " << i << ": " << field_name << "\n";
7908b97b69aSJeremy L Thompson       CeedCallBackend(CeedQFunctionFieldGetEvalMode(qf_output_fields[i], &eval_mode));
7918b97b69aSJeremy L Thompson       // Basis action
7928b97b69aSJeremy L Thompson       switch (eval_mode) {
7938b97b69aSJeremy L Thompson         case CEED_EVAL_NONE:
794*0183ed61SJeremy L Thompson           code << tab << "CeedScalar r_s" << var_suffix << "[num_comp" << var_suffix << "];\n";
7958b97b69aSJeremy L Thompson           break;
7968b97b69aSJeremy L Thompson         case CEED_EVAL_INTERP:
797*0183ed61SJeremy L Thompson           code << tab << "CeedScalar r_s" << var_suffix << "[num_comp" << var_suffix << "];\n";
7988b97b69aSJeremy L Thompson           break;
7998b97b69aSJeremy L Thompson         case CEED_EVAL_GRAD:
800*0183ed61SJeremy L Thompson           code << tab << "CeedScalar r_s" << var_suffix << "[num_comp" << var_suffix << "*dim" << var_suffix << "];\n";
8018b97b69aSJeremy L Thompson           break;
8028b97b69aSJeremy L Thompson           // LCOV_EXCL_START
8038b97b69aSJeremy L Thompson         case CEED_EVAL_WEIGHT:
8048b97b69aSJeremy L Thompson           break;  // Should not occur
8058b97b69aSJeremy L Thompson         case CEED_EVAL_DIV:
8068b97b69aSJeremy L Thompson         case CEED_EVAL_CURL:
8078b97b69aSJeremy L Thompson           break;  // TODO: Not implemented
8088b97b69aSJeremy L Thompson                   // LCOV_EXCL_STOP
8098b97b69aSJeremy L Thompson       }
8108b97b69aSJeremy L Thompson     }
8118b97b69aSJeremy L Thompson 
8128b97b69aSJeremy L Thompson   } else if (use_3d_slices) {
8134b3e95d5SJeremy L Thompson     // We treat quadrature points per slice in 3d to save registers
814*0183ed61SJeremy L Thompson     code << "\n";
815*0183ed61SJeremy L Thompson     code << tab << "// Note: Using planes of 3D elements\n";
816*0183ed61SJeremy L Thompson     code << tab << "#pragma unroll\n";
817*0183ed61SJeremy L Thompson     code << tab << "for (CeedInt q = 0; q < " << Q_name << "; q++) {\n";
818*0183ed61SJeremy L Thompson     tab.push();
819*0183ed61SJeremy L Thompson     code << tab << "// -- Input fields\n";
8204b3e95d5SJeremy L Thompson     for (CeedInt i = 0; i < num_input_fields; i++) {
82159fa3f92SJeremy L Thompson       const char *field_name;
8224b3e95d5SJeremy L Thompson       std::string var_suffix = "_in_" + std::to_string(i);
8234b3e95d5SJeremy L Thompson 
82459fa3f92SJeremy L Thompson       CeedCallBackend(CeedOperatorFieldGetName(op_input_fields[i], &field_name));
825*0183ed61SJeremy L Thompson       code << tab << "// ---- Input field " << i << ": " << field_name << "\n";
8264b3e95d5SJeremy L Thompson       CeedCallBackend(CeedQFunctionFieldGetEvalMode(qf_input_fields[i], &eval_mode));
8274b3e95d5SJeremy L Thompson       // Basis action
828*0183ed61SJeremy L Thompson       code << tab << "// EvalMode: " << CeedEvalModes[eval_mode] << "\n";
8294b3e95d5SJeremy L Thompson       switch (eval_mode) {
8304b3e95d5SJeremy L Thompson         case CEED_EVAL_NONE:
8314b3e95d5SJeremy L Thompson           bool is_strided;
8324b3e95d5SJeremy L Thompson 
833*0183ed61SJeremy L Thompson           code << tab << "CeedScalar r_s" << var_suffix << "[num_comp" << var_suffix << "];\n";
8344b3e95d5SJeremy L Thompson 
8354b3e95d5SJeremy L Thompson           CeedCallBackend(CeedOperatorFieldGetElemRestriction(op_input_fields[i], &elem_rstr));
8364b3e95d5SJeremy L Thompson           CeedCallBackend(CeedElemRestrictionIsStrided(elem_rstr, &is_strided));
8374b3e95d5SJeremy L Thompson           if (is_strided) {
8384b3e95d5SJeremy L Thompson             bool    has_backend_strides;
8394b3e95d5SJeremy L Thompson             CeedInt num_elem, elem_size;
8404b3e95d5SJeremy L Thompson 
8414b3e95d5SJeremy L Thompson             CeedCallBackend(CeedElemRestrictionGetElementSize(elem_rstr, &elem_size));
8424b3e95d5SJeremy L Thompson             CeedCallBackend(CeedElemRestrictionHasBackendStrides(elem_rstr, &has_backend_strides));
8434b3e95d5SJeremy L Thompson             CeedCallBackend(CeedElemRestrictionGetNumElements(elem_rstr, &num_elem));
8444b3e95d5SJeremy L Thompson             CeedInt strides[3] = {1, elem_size * num_elem, elem_size};
8454b3e95d5SJeremy L Thompson 
8464b3e95d5SJeremy L Thompson             if (!has_backend_strides) {
8474b3e95d5SJeremy L Thompson               CeedCallBackend(CeedElemRestrictionGetStrides(elem_rstr, strides));
8484b3e95d5SJeremy L Thompson             }
849*0183ed61SJeremy L Thompson             code << tab << "const CeedInt strides" << var_suffix << "_0 = " << strides[0] << ", strides" << var_suffix << "_1 = " << strides[1]
850*0183ed61SJeremy L Thompson                  << ", strides" << var_suffix << "_2 = " << strides[2] << ";\n";
851*0183ed61SJeremy L Thompson             code << tab << "ReadEVecSliceStrided3d<num_comp" << var_suffix << ", " << Q_name << ", strides" << var_suffix << "_0, strides"
852*0183ed61SJeremy L Thompson                  << var_suffix << "_1, strides" << var_suffix << "_2>(data, elem, q, d" << var_suffix << ", r_s" << var_suffix << ");\n";
8534b3e95d5SJeremy L Thompson           } else {
8544b3e95d5SJeremy L Thompson             CeedSize                  l_size = 0;
8554b3e95d5SJeremy L Thompson             CeedInt                   comp_stride;
8564b3e95d5SJeremy L Thompson             CeedElemRestriction_Cuda *rstr_data;
8574b3e95d5SJeremy L Thompson 
8584b3e95d5SJeremy L Thompson             CeedCallBackend(CeedElemRestrictionGetLVectorSize(elem_rstr, &l_size));
859*0183ed61SJeremy L Thompson             code << tab << "const CeedInt l_size" << var_suffix << " = " << l_size << ";\n";
8604b3e95d5SJeremy L Thompson             CeedCallBackend(CeedElemRestrictionGetCompStride(elem_rstr, &comp_stride));
861*0183ed61SJeremy L Thompson             code << tab << "const CeedInt comp_stride" << var_suffix << " = " << comp_stride << ";\n";
8624b3e95d5SJeremy L Thompson             CeedCallBackend(CeedElemRestrictionGetData(elem_rstr, &rstr_data));
8634b3e95d5SJeremy L Thompson             data->indices.inputs[i] = (CeedInt *)rstr_data->d_offsets;
864*0183ed61SJeremy L Thompson             code << tab << "ReadEVecSliceStandard3d<num_comp" << var_suffix << ", comp_stride" << var_suffix << ", " << Q_name << ">(data, l_size"
865*0183ed61SJeremy L Thompson                  << var_suffix << ", elem, q, indices.inputs[" << i << "], d" << var_suffix << ", r_s" << var_suffix << ");\n";
8664b3e95d5SJeremy L Thompson           }
867681d0ea7SJeremy L Thompson           CeedCallBackend(CeedElemRestrictionDestroy(&elem_rstr));
8684b3e95d5SJeremy L Thompson           break;
8694b3e95d5SJeremy L Thompson         case CEED_EVAL_INTERP:
870*0183ed61SJeremy L Thompson           code << tab << "CeedScalar r_s" << var_suffix << "[num_comp" << var_suffix << "];\n";
871*0183ed61SJeremy L Thompson           code << tab << "for (CeedInt j = 0; j < num_comp" << var_suffix << "; j++) {\n";
872*0183ed61SJeremy L Thompson           tab.push();
8734b3e95d5SJeremy L Thompson           code << "r_s" << var_suffix << "[j] = r_q" << var_suffix << "[q + j*" << Q_name << "];\n";
874*0183ed61SJeremy L Thompson           tab.pop();
875*0183ed61SJeremy L Thompson           code << tab << "}\n";
8764b3e95d5SJeremy L Thompson           break;
8774b3e95d5SJeremy L Thompson         case CEED_EVAL_GRAD:
878*0183ed61SJeremy L Thompson           code << tab << "CeedScalar r_s" << var_suffix << "[num_comp" << var_suffix << "*dim" << var_suffix << "];\n";
879*0183ed61SJeremy L Thompson           code << tab << "GradColloSlice3d<num_comp" << var_suffix << ", " << Q_name << ", OP_T_1D>(data, q, r_q" << var_suffix << ", s_G"
88099421279SJeremy L Thompson                << var_suffix << ", r_s" << var_suffix << ");\n";
8814b3e95d5SJeremy L Thompson           break;
8824b3e95d5SJeremy L Thompson         case CEED_EVAL_WEIGHT:
883*0183ed61SJeremy L Thompson           code << tab << "CeedScalar r_s" << var_suffix << "[1];\n";
884*0183ed61SJeremy L Thompson           code << tab << "r_s" << var_suffix << "[0] = r_q" << var_suffix << "[q];\n";
8858b97b69aSJeremy L Thompson           break;
8864b3e95d5SJeremy L Thompson           // LCOV_EXCL_START
8874b3e95d5SJeremy L Thompson         case CEED_EVAL_DIV:
8884b3e95d5SJeremy L Thompson         case CEED_EVAL_CURL:
8894b3e95d5SJeremy L Thompson           break;  // TODO: Not implemented
8904b3e95d5SJeremy L Thompson                   // LCOV_EXCL_STOP
8914b3e95d5SJeremy L Thompson       }
8924b3e95d5SJeremy L Thompson     }
893*0183ed61SJeremy L Thompson     code << "\n";
894*0183ed61SJeremy L Thompson     code << tab << "// -- Output fields\n";
8954b3e95d5SJeremy L Thompson     for (CeedInt i = 0; i < num_output_fields; i++) {
89659fa3f92SJeremy L Thompson       const char *field_name;
8974b3e95d5SJeremy L Thompson       std::string var_suffix = "_out_" + std::to_string(i);
8984b3e95d5SJeremy L Thompson 
89959fa3f92SJeremy L Thompson       CeedCallBackend(CeedOperatorFieldGetName(op_output_fields[i], &field_name));
900*0183ed61SJeremy L Thompson       code << tab << "// ---- Output field " << i << ": " << field_name << "\n";
9014b3e95d5SJeremy L Thompson       CeedCallBackend(CeedQFunctionFieldGetEvalMode(qf_output_fields[i], &eval_mode));
9024b3e95d5SJeremy L Thompson       // Basis action
9034b3e95d5SJeremy L Thompson       switch (eval_mode) {
9044b3e95d5SJeremy L Thompson         case CEED_EVAL_NONE:
905*0183ed61SJeremy L Thompson           code << tab << "CeedScalar r_s" << var_suffix << "[num_comp" << var_suffix << "];\n";
9068b97b69aSJeremy L Thompson           break;
9074b3e95d5SJeremy L Thompson         case CEED_EVAL_INTERP:
908*0183ed61SJeremy L Thompson           code << tab << "CeedScalar r_s" << var_suffix << "[num_comp" << var_suffix << "];\n";
9094b3e95d5SJeremy L Thompson           break;
9104b3e95d5SJeremy L Thompson         case CEED_EVAL_GRAD:
911*0183ed61SJeremy L Thompson           code << tab << "CeedScalar r_s" << var_suffix << "[num_comp" << var_suffix << "*dim" << var_suffix << "];\n";
9124b3e95d5SJeremy L Thompson           break;
9134b3e95d5SJeremy L Thompson           // LCOV_EXCL_START
9144b3e95d5SJeremy L Thompson         case CEED_EVAL_WEIGHT:
9154b3e95d5SJeremy L Thompson           break;  // Should not occur
9164b3e95d5SJeremy L Thompson         case CEED_EVAL_DIV:
9174b3e95d5SJeremy L Thompson         case CEED_EVAL_CURL:
9184b3e95d5SJeremy L Thompson           break;  // TODO: Not implemented
9194b3e95d5SJeremy L Thompson                   // LCOV_EXCL_STOP
9204b3e95d5SJeremy L Thompson       }
9214b3e95d5SJeremy L Thompson     }
9224b3e95d5SJeremy L Thompson   } else {
923*0183ed61SJeremy L Thompson     code << "\n";
924*0183ed61SJeremy L Thompson     code << tab << "// Note: Using full elements\n";
925*0183ed61SJeremy L Thompson     code << tab << "{\n";
926*0183ed61SJeremy L Thompson     tab.push();
927*0183ed61SJeremy L Thompson     code << tab << "// -- Input fields\n";
9284b3e95d5SJeremy L Thompson     for (CeedInt i = 0; i < num_input_fields; i++) {
92959fa3f92SJeremy L Thompson       const char *field_name;
93059fa3f92SJeremy L Thompson 
93159fa3f92SJeremy L Thompson       CeedCallBackend(CeedOperatorFieldGetName(op_input_fields[i], &field_name));
932*0183ed61SJeremy L Thompson       code << tab << "// ---- Input field " << i << ": " << field_name << "\n";
933*0183ed61SJeremy L Thompson       code << tab << "CeedScalar *r_s_in_" << i << " = r_q_in_" << i << ";\n";
9344b3e95d5SJeremy L Thompson     }
935*0183ed61SJeremy L Thompson     code << tab << "// -- Output fields\n";
9364b3e95d5SJeremy L Thompson     for (CeedInt i = 0; i < num_output_fields; i++) {
93759fa3f92SJeremy L Thompson       const char *field_name;
93859fa3f92SJeremy L Thompson 
93959fa3f92SJeremy L Thompson       CeedCallBackend(CeedOperatorFieldGetName(op_output_fields[i], &field_name));
940*0183ed61SJeremy L Thompson       code << tab << "// ---- Output field " << i << ": " << field_name << "\n";
941*0183ed61SJeremy L Thompson       code << tab << "CeedScalar *r_s_out_" << i << " = r_q_out_" << i << ";\n";
9424b3e95d5SJeremy L Thompson     }
9434b3e95d5SJeremy L Thompson   }
9444b3e95d5SJeremy L Thompson 
9454b3e95d5SJeremy L Thompson   // Input and output buffers
946*0183ed61SJeremy L Thompson   code << "\n";
947*0183ed61SJeremy L Thompson   code << tab << "// -- QFunction inputs and outputs\n";
948*0183ed61SJeremy L Thompson   code << tab << "// ---- Inputs\n";
949*0183ed61SJeremy L Thompson   code << tab << "CeedScalar *inputs[" << CeedIntMax(num_input_fields, 1) << "];\n";
9504b3e95d5SJeremy L Thompson   for (CeedInt i = 0; i < num_input_fields; i++) {
95159fa3f92SJeremy L Thompson     const char *field_name;
95259fa3f92SJeremy L Thompson 
95359fa3f92SJeremy L Thompson     CeedCallBackend(CeedOperatorFieldGetName(op_input_fields[i], &field_name));
954*0183ed61SJeremy L Thompson     code << tab << "// ------ Input field " << i << ": " << field_name << "\n";
955*0183ed61SJeremy L Thompson     code << tab << "inputs[" << i << "] = r_s_in_" << i << ";\n";
9564b3e95d5SJeremy L Thompson   }
957*0183ed61SJeremy L Thompson   code << tab << "// ---- Outputs\n";
958*0183ed61SJeremy L Thompson   code << tab << "CeedScalar *outputs[" << CeedIntMax(num_output_fields, 1) << "];\n";
9594b3e95d5SJeremy L Thompson   for (CeedInt i = 0; i < num_output_fields; i++) {
96059fa3f92SJeremy L Thompson     const char *field_name;
96159fa3f92SJeremy L Thompson 
96259fa3f92SJeremy L Thompson     CeedCallBackend(CeedOperatorFieldGetName(op_output_fields[i], &field_name));
963*0183ed61SJeremy L Thompson     code << tab << "// ------ Output field " << i << ": " << field_name << "\n";
964*0183ed61SJeremy L Thompson     code << tab << "outputs[" << i << "] = r_s_out_" << i << ";\n";
9654b3e95d5SJeremy L Thompson   }
9664b3e95d5SJeremy L Thompson 
9674b3e95d5SJeremy L Thompson   // Apply QFunction
968*0183ed61SJeremy L Thompson   code << "\n";
969*0183ed61SJeremy L Thompson   code << tab << "// -- Apply QFunction\n";
970*0183ed61SJeremy L Thompson   code << tab << "" << qfunction_name << "(ctx, ";
971412e5683SJeremy L Thompson   if (max_dim != 3 || is_at_points || use_3d_slices || !is_all_tensor) {
9724b3e95d5SJeremy L Thompson     code << "1";
9734b3e95d5SJeremy L Thompson   } else {
974dc007f05SJeremy L Thompson     code << Q_name;
9754b3e95d5SJeremy L Thompson   }
9764b3e95d5SJeremy L Thompson   code << ", inputs, outputs);\n";
9774b3e95d5SJeremy L Thompson 
9788b97b69aSJeremy L Thompson   if (is_at_points) {
9798b97b69aSJeremy L Thompson     // Map back to coefficients
980*0183ed61SJeremy L Thompson     code << "\n";
981*0183ed61SJeremy L Thompson     code << tab << "// -- Output fields\n";
9828b97b69aSJeremy L Thompson     for (CeedInt i = 0; i < num_output_fields; i++) {
98359fa3f92SJeremy L Thompson       const char *field_name;
9848b97b69aSJeremy L Thompson       std::string var_suffix = "_out_" + std::to_string(i);
9858b97b69aSJeremy L Thompson       std::string P_name     = "P_1d" + var_suffix;
9868b97b69aSJeremy L Thompson 
98759fa3f92SJeremy L Thompson       CeedCallBackend(CeedOperatorFieldGetName(op_output_fields[i], &field_name));
988*0183ed61SJeremy L Thompson       code << tab << "// ---- Output field " << i << ": " << field_name << "\n";
9898b97b69aSJeremy L Thompson       CeedCallBackend(CeedQFunctionFieldGetEvalMode(qf_output_fields[i], &eval_mode));
9908b97b69aSJeremy L Thompson       // Basis action
991*0183ed61SJeremy L Thompson       code << tab << "// EvalMode: " << CeedEvalModes[eval_mode] << "\n";
9928b97b69aSJeremy L Thompson       switch (eval_mode) {
9938b97b69aSJeremy L Thompson         case CEED_EVAL_NONE: {
9948b97b69aSJeremy L Thompson           CeedInt             comp_stride;
9958b97b69aSJeremy L Thompson           CeedElemRestriction elem_rstr;
9968b97b69aSJeremy L Thompson 
9978b97b69aSJeremy L Thompson           CeedCallBackend(CeedOperatorFieldGetElemRestriction(op_output_fields[i], &elem_rstr));
9988b97b69aSJeremy L Thompson           CeedCallBackend(CeedElemRestrictionGetCompStride(elem_rstr, &comp_stride));
9998b97b69aSJeremy L Thompson           CeedCallBackend(CeedElemRestrictionDestroy(&elem_rstr));
1000*0183ed61SJeremy L Thompson           code << tab << "const CeedInt comp_stride" << var_suffix << " = " << comp_stride << ";\n";
1001*0183ed61SJeremy L Thompson           code << tab << "WritePoint<num_comp" << var_suffix << ", comp_stride" << var_suffix
10028b97b69aSJeremy L Thompson                << ", max_num_points>(data, elem, i, points.num_per_elem[elem], indices.outputs[" << i << "]"
10038b97b69aSJeremy L Thompson                << ", r_s" << var_suffix << ", d" << var_suffix << ");\n";
10048b97b69aSJeremy L Thompson           break;
10058b97b69aSJeremy L Thompson         }
10068b97b69aSJeremy L Thompson         case CEED_EVAL_INTERP:
1007*0183ed61SJeremy L Thompson           code << tab << "if (i >= points.num_per_elem[elem]) {\n";
1008*0183ed61SJeremy L Thompson           tab.push();
1009*0183ed61SJeremy L Thompson           code << tab << "for (CeedInt j = 0; j < num_comp" << var_suffix << "; j++) r_s" << var_suffix << "[j] = 0.0;\n";
1010*0183ed61SJeremy L Thompson           tab.pop();
1011*0183ed61SJeremy L Thompson           code << tab << "}\n";
1012*0183ed61SJeremy L Thompson           code << tab << "InterpTransposeAtPoints" << max_dim << "d<num_comp" << var_suffix << ", max_num_points, " << P_name << ", " << Q_name
1013f725b54bSJeremy L Thompson                << ">(data, i, r_s" << var_suffix << ", r_x, r_c" << var_suffix << ");\n";
10148b97b69aSJeremy L Thompson           break;
10158b97b69aSJeremy L Thompson         case CEED_EVAL_GRAD:
1016*0183ed61SJeremy L Thompson           code << tab << "if (i >= points.num_per_elem[elem]) {\n";
1017*0183ed61SJeremy L Thompson           tab.push();
1018*0183ed61SJeremy L Thompson           code << tab << "for (CeedInt j = 0; j < num_comp" << var_suffix << "*dim" << var_suffix << "; j++) r_s" << var_suffix << "[j] = 0.0;\n";
1019*0183ed61SJeremy L Thompson           tab.pop();
1020*0183ed61SJeremy L Thompson           code << tab << "}\n";
1021*0183ed61SJeremy L Thompson           code << tab << "GradTransposeAtPoints" << max_dim << "d<num_comp" << var_suffix << ", max_num_points, " << P_name << ", " << Q_name
1022f725b54bSJeremy L Thompson                << ">(data, i, r_s" << var_suffix << ", r_x, r_c" << var_suffix << ");\n";
10238b97b69aSJeremy L Thompson           break;
10248b97b69aSJeremy L Thompson           // LCOV_EXCL_START
10258b97b69aSJeremy L Thompson         case CEED_EVAL_WEIGHT:
10268b97b69aSJeremy L Thompson           break;  // Should not occur
10278b97b69aSJeremy L Thompson         case CEED_EVAL_DIV:
10288b97b69aSJeremy L Thompson         case CEED_EVAL_CURL:
10298b97b69aSJeremy L Thompson           break;  // TODO: Not implemented
10308b97b69aSJeremy L Thompson                   // LCOV_EXCL_STOP
10318b97b69aSJeremy L Thompson       }
10328b97b69aSJeremy L Thompson     }
10338b97b69aSJeremy L Thompson   } else if (use_3d_slices) {
10344b3e95d5SJeremy L Thompson     // Copy or apply transpose grad, if needed
1035*0183ed61SJeremy L Thompson     code << "\n";
1036*0183ed61SJeremy L Thompson     code << tab << "// -- Output fields\n";
10374b3e95d5SJeremy L Thompson     for (CeedInt i = 0; i < num_output_fields; i++) {
103859fa3f92SJeremy L Thompson       const char *field_name;
10394b3e95d5SJeremy L Thompson       std::string var_suffix = "_out_" + std::to_string(i);
10404b3e95d5SJeremy L Thompson       std::string P_name     = "P_1d" + var_suffix;
10414b3e95d5SJeremy L Thompson 
104259fa3f92SJeremy L Thompson       CeedCallBackend(CeedOperatorFieldGetName(op_output_fields[i], &field_name));
1043*0183ed61SJeremy L Thompson       code << tab << "// ---- Output field " << i << ": " << field_name << "\n";
10444b3e95d5SJeremy L Thompson       CeedCallBackend(CeedQFunctionFieldGetEvalMode(qf_output_fields[i], &eval_mode));
10454b3e95d5SJeremy L Thompson       // Basis action
1046*0183ed61SJeremy L Thompson       code << tab << "// EvalMode: " << CeedEvalModes[eval_mode] << "\n";
10474b3e95d5SJeremy L Thompson       switch (eval_mode) {
10484b3e95d5SJeremy L Thompson         case CEED_EVAL_NONE:
1049*0183ed61SJeremy L Thompson           code << tab << "for (CeedInt j = 0; j < num_comp" << var_suffix << " ; j++) {\n";
1050*0183ed61SJeremy L Thompson           tab.push();
1051*0183ed61SJeremy L Thompson           code << tab << "r_q" << var_suffix << "[q + j*" << Q_name << "] = r_s" << var_suffix << "[j];\n";
1052*0183ed61SJeremy L Thompson           tab.pop();
1053*0183ed61SJeremy L Thompson           code << tab << "}\n";
10548b97b69aSJeremy L Thompson           break;
10554b3e95d5SJeremy L Thompson         case CEED_EVAL_INTERP:
1056*0183ed61SJeremy L Thompson           code << tab << "for (CeedInt j = 0; j < num_comp" << var_suffix << " ; j++) {\n";
1057*0183ed61SJeremy L Thompson           tab.push();
1058*0183ed61SJeremy L Thompson           code << tab << "r_q" << var_suffix << "[q + j*" << Q_name << "] = r_s" << var_suffix << "[j];\n";
1059*0183ed61SJeremy L Thompson           tab.pop();
1060*0183ed61SJeremy L Thompson           code << tab << "}\n";
10614b3e95d5SJeremy L Thompson           break;
10624b3e95d5SJeremy L Thompson         case CEED_EVAL_GRAD:
1063*0183ed61SJeremy L Thompson           code << tab << "GradColloSliceTranspose3d<num_comp" << var_suffix << ", " << Q_name << ", OP_T_1D>(data, q, r_s" << var_suffix << ", s_G"
1064f815fac9SJeremy L Thompson                << var_suffix << ", r_q" << var_suffix << ");\n";
10654b3e95d5SJeremy L Thompson           break;
10664b3e95d5SJeremy L Thompson           // LCOV_EXCL_START
10674b3e95d5SJeremy L Thompson         case CEED_EVAL_WEIGHT:
10684b3e95d5SJeremy L Thompson           break;  // Should not occur
10694b3e95d5SJeremy L Thompson         case CEED_EVAL_DIV:
10704b3e95d5SJeremy L Thompson         case CEED_EVAL_CURL:
10714b3e95d5SJeremy L Thompson           break;  // TODO: Not implemented
10724b3e95d5SJeremy L Thompson                   // LCOV_EXCL_STOP
10734b3e95d5SJeremy L Thompson       }
10744b3e95d5SJeremy L Thompson     }
10754b3e95d5SJeremy L Thompson   }
1076*0183ed61SJeremy L Thompson   tab.pop();
1077*0183ed61SJeremy L Thompson   code << tab << "}\n";
10784b3e95d5SJeremy L Thompson   return CEED_ERROR_SUCCESS;
10794b3e95d5SJeremy L Thompson }
10804b3e95d5SJeremy L Thompson 
10814b3e95d5SJeremy L Thompson //------------------------------------------------------------------------------
1082b2165e7aSSebastian Grimberg // Build single operator kernel
1083ab213215SJeremy L Thompson //------------------------------------------------------------------------------
1084ddae5012SJeremy L Thompson extern "C" int CeedOperatorBuildKernel_Cuda_gen(CeedOperator op, bool *is_good_build) {
1085412e5683SJeremy L Thompson   bool                    is_all_tensor = true, is_all_nontensor = true, is_at_points = false, use_3d_slices = false;
1086241a4b83SYohann   Ceed                    ceed;
1087efa41df3SJeremy L Thompson   CeedInt                 Q = 0, Q_1d = 0, num_input_fields, num_output_fields, max_dim = 1, max_num_points = 0, coords_comp_stride = 0;
1088ca735530SJeremy L Thompson   CeedQFunctionField     *qf_input_fields, *qf_output_fields;
1089ca735530SJeremy L Thompson   CeedQFunction_Cuda_gen *qf_data;
1090ca735530SJeremy L Thompson   CeedQFunction           qf;
1091ca735530SJeremy L Thompson   CeedOperatorField      *op_input_fields, *op_output_fields;
1092ca735530SJeremy L Thompson   CeedOperator_Cuda_gen  *data;
10934b3e95d5SJeremy L Thompson   std::ostringstream      code;
1094*0183ed61SJeremy L Thompson   Tab                     tab;
10954b3e95d5SJeremy L Thompson 
1096ddae5012SJeremy L Thompson   CeedCallBackend(CeedOperatorGetData(op, &data));
10974b3e95d5SJeremy L Thompson   {
10984b3e95d5SJeremy L Thompson     bool is_setup_done;
1099ca735530SJeremy L Thompson 
1100ca735530SJeremy L Thompson     CeedCallBackend(CeedOperatorIsSetupDone(op, &is_setup_done));
1101ddae5012SJeremy L Thompson     if (is_setup_done) {
1102ddae5012SJeremy L Thompson       *is_good_build = !data->use_fallback;
1103ddae5012SJeremy L Thompson       return CEED_ERROR_SUCCESS;
1104ddae5012SJeremy L Thompson     }
1105ddae5012SJeremy L Thompson   }
1106ddae5012SJeremy L Thompson 
1107ddae5012SJeremy L Thompson   // Check field compatibility
11088d12f40eSJeremy L Thompson   CeedCallBackend(CeedOperatorGetFields(op, &num_input_fields, &op_input_fields, &num_output_fields, &op_output_fields));
1109ddae5012SJeremy L Thompson   {
1110412e5683SJeremy L Thompson     bool has_shared_bases = true;
1111ddae5012SJeremy L Thompson 
1112ddae5012SJeremy L Thompson     for (CeedInt i = 0; i < num_input_fields; i++) {
1113ddae5012SJeremy L Thompson       CeedBasis basis;
1114ddae5012SJeremy L Thompson 
1115ddae5012SJeremy L Thompson       CeedCallBackend(CeedOperatorFieldGetBasis(op_input_fields[i], &basis));
1116ddae5012SJeremy L Thompson       if (basis != CEED_BASIS_NONE) {
1117ddae5012SJeremy L Thompson         bool        is_tensor = true;
1118ddae5012SJeremy L Thompson         const char *resource;
1119ddae5012SJeremy L Thompson         char       *resource_root;
1120ddae5012SJeremy L Thompson         Ceed        basis_ceed;
1121ddae5012SJeremy L Thompson 
1122ddae5012SJeremy L Thompson         CeedCallBackend(CeedBasisIsTensor(basis, &is_tensor));
1123c9192acaSJeremy L Thompson         is_all_tensor    = is_all_tensor && is_tensor;
112445a787f7SJeremy L Thompson         is_all_nontensor = is_all_nontensor && !is_tensor;
1125ddae5012SJeremy L Thompson         CeedCallBackend(CeedBasisGetCeed(basis, &basis_ceed));
1126ddae5012SJeremy L Thompson         CeedCallBackend(CeedGetResource(basis_ceed, &resource));
1127ddae5012SJeremy L Thompson         CeedCallBackend(CeedGetResourceRoot(basis_ceed, resource, ":", &resource_root));
1128c9192acaSJeremy L Thompson         has_shared_bases = has_shared_bases && !strcmp(resource_root, "/gpu/cuda/shared");
1129ddae5012SJeremy L Thompson         CeedCallBackend(CeedFree(&resource_root));
1130ddae5012SJeremy L Thompson         CeedCallBackend(CeedDestroy(&basis_ceed));
1131ddae5012SJeremy L Thompson       }
1132ddae5012SJeremy L Thompson       CeedCallBackend(CeedBasisDestroy(&basis));
11334b3e95d5SJeremy L Thompson     }
1134ca735530SJeremy L Thompson 
1135ddae5012SJeremy L Thompson     for (CeedInt i = 0; i < num_output_fields; i++) {
1136ddae5012SJeremy L Thompson       CeedBasis basis;
1137ddae5012SJeremy L Thompson 
1138ddae5012SJeremy L Thompson       CeedCallBackend(CeedOperatorFieldGetBasis(op_output_fields[i], &basis));
1139ddae5012SJeremy L Thompson       if (basis != CEED_BASIS_NONE) {
1140ddae5012SJeremy L Thompson         bool        is_tensor = true;
1141ddae5012SJeremy L Thompson         const char *resource;
1142ddae5012SJeremy L Thompson         char       *resource_root;
1143ddae5012SJeremy L Thompson         Ceed        basis_ceed;
1144ddae5012SJeremy L Thompson 
1145ddae5012SJeremy L Thompson         CeedCallBackend(CeedBasisIsTensor(basis, &is_tensor));
1146c9192acaSJeremy L Thompson         is_all_tensor    = is_all_tensor && is_tensor;
1147c9192acaSJeremy L Thompson         is_all_nontensor = is_all_nontensor && !is_tensor;
1148ddae5012SJeremy L Thompson 
1149ddae5012SJeremy L Thompson         CeedCallBackend(CeedBasisGetCeed(basis, &basis_ceed));
1150ddae5012SJeremy L Thompson         CeedCallBackend(CeedGetResource(basis_ceed, &resource));
1151ddae5012SJeremy L Thompson         CeedCallBackend(CeedGetResourceRoot(basis_ceed, resource, ":", &resource_root));
1152c9192acaSJeremy L Thompson         has_shared_bases = has_shared_bases && !strcmp(resource_root, "/gpu/cuda/shared");
1153ddae5012SJeremy L Thompson         CeedCallBackend(CeedFree(&resource_root));
1154ddae5012SJeremy L Thompson         CeedCallBackend(CeedDestroy(&basis_ceed));
1155ddae5012SJeremy L Thompson       }
1156ddae5012SJeremy L Thompson       CeedCallBackend(CeedBasisDestroy(&basis));
1157ddae5012SJeremy L Thompson     }
1158ddae5012SJeremy L Thompson     // -- Fallback to ref if not all bases are shared
1159412e5683SJeremy L Thompson     if (!has_shared_bases) {
1160ddae5012SJeremy L Thompson       *is_good_build = false;
1161ddae5012SJeremy L Thompson       return CEED_ERROR_SUCCESS;
1162ddae5012SJeremy L Thompson     }
1163ddae5012SJeremy L Thompson   }
1164ca735530SJeremy L Thompson   CeedCallBackend(CeedOperatorGetCeed(op, &ceed));
1165ca735530SJeremy L Thompson   CeedCallBackend(CeedOperatorGetQFunction(op, &qf));
1166ca735530SJeremy L Thompson   CeedCallBackend(CeedQFunctionGetData(qf, &qf_data));
1167ca735530SJeremy L Thompson   CeedCallBackend(CeedQFunctionGetFields(qf, NULL, &qf_input_fields, NULL, &qf_output_fields));
1168241a4b83SYohann 
11694b3e95d5SJeremy L Thompson   // Get operator data
11708b97b69aSJeremy L Thompson   CeedCallBackend(CeedOperatorIsAtPoints(op, &is_at_points));
1171412e5683SJeremy L Thompson   {
1172efa41df3SJeremy L Thompson     CeedInt max_P = 0, max_P_1d = 0;
1173412e5683SJeremy L Thompson 
1174412e5683SJeremy L Thompson     CeedCallBackend(CeedOperatorBuildKernelData_Cuda_gen(ceed, num_input_fields, op_input_fields, qf_input_fields, num_output_fields,
1175412e5683SJeremy L Thompson                                                          op_output_fields, qf_output_fields, &max_P, &max_P_1d, &Q, &Q_1d, &max_dim, &is_all_tensor,
1176412e5683SJeremy L Thompson                                                          &use_3d_slices));
1177412e5683SJeremy L Thompson     data->max_P_1d = is_all_tensor ? max_P_1d : max_P;
1178412e5683SJeremy L Thompson   }
1179412e5683SJeremy L Thompson   if (max_dim == 0) max_dim = 1;
1180412e5683SJeremy L Thompson   data->dim = max_dim;
11818b97b69aSJeremy L Thompson   if (is_at_points) {
11828b97b69aSJeremy L Thompson     CeedElemRestriction_Cuda *rstr_data;
11838b97b69aSJeremy L Thompson     CeedElemRestriction       rstr_points = NULL;
11844b3e95d5SJeremy L Thompson 
11858b97b69aSJeremy L Thompson     CeedCallBackend(CeedOperatorAtPointsGetPoints(op, &rstr_points, NULL));
11868b97b69aSJeremy L Thompson     CeedCallBackend(CeedElemRestrictionGetMaxPointsInElement(rstr_points, &max_num_points));
11878b97b69aSJeremy L Thompson     CeedCallBackend(CeedElemRestrictionGetCompStride(rstr_points, &coords_comp_stride));
11888b97b69aSJeremy L Thompson     CeedCallBackend(CeedElemRestrictionGetData(rstr_points, &rstr_data));
11898b97b69aSJeremy L Thompson     data->points.indices = (CeedInt *)rstr_data->d_offsets;
11908b97b69aSJeremy L Thompson     CeedCallBackend(CeedElemRestrictionDestroy(&rstr_points));
11918b97b69aSJeremy L Thompson   }
11928b97b69aSJeremy L Thompson   if (is_at_points) use_3d_slices = false;
11938b97b69aSJeremy L Thompson   if (Q_1d == 0) {
11948b97b69aSJeremy L Thompson     if (is_at_points) Q_1d = max_num_points;
11958b97b69aSJeremy L Thompson     else CeedCallBackend(CeedOperatorGetNumQuadraturePoints(op, &Q_1d));
11964b3e95d5SJeremy L Thompson   }
1197c433aabcSJeremy L Thompson   if (Q == 0) Q = Q_1d;
1198c433aabcSJeremy L Thompson   data->Q    = Q;
11994b3e95d5SJeremy L Thompson   data->Q_1d = Q_1d;
12004b3e95d5SJeremy L Thompson 
12010b454692Sjeremylt   // Check for restriction only identity operator
12024b3e95d5SJeremy L Thompson   {
12034b3e95d5SJeremy L Thompson     bool is_identity_qf;
12044b3e95d5SJeremy L Thompson 
12052b730f8bSJeremy L Thompson     CeedCallBackend(CeedQFunctionIsIdentity(qf, &is_identity_qf));
12060b454692Sjeremylt     if (is_identity_qf) {
12079e201c85SYohann       CeedEvalMode eval_mode_in, eval_mode_out;
1208ca735530SJeremy L Thompson 
12092b730f8bSJeremy L Thompson       CeedCallBackend(CeedQFunctionFieldGetEvalMode(qf_input_fields[0], &eval_mode_in));
12102b730f8bSJeremy L Thompson       CeedCallBackend(CeedQFunctionFieldGetEvalMode(qf_output_fields[0], &eval_mode_out));
12116574a04fSJeremy L Thompson       CeedCheck(eval_mode_in != CEED_EVAL_NONE || eval_mode_out != CEED_EVAL_NONE, ceed, CEED_ERROR_BACKEND,
12126574a04fSJeremy L Thompson                 "Backend does not implement restriction only identity operators");
12130b454692Sjeremylt     }
12144b3e95d5SJeremy L Thompson   }
12150b454692Sjeremylt 
1216f1a13f77SYohann Dudouit   // Add atomicAdd function for old NVidia architectures
12174b3e95d5SJeremy L Thompson   {
12184b3e95d5SJeremy L Thompson     Ceed_Cuda            *ceed_data;
12194b3e95d5SJeremy L Thompson     struct cudaDeviceProp prop;
12204b3e95d5SJeremy L Thompson 
12212b730f8bSJeremy L Thompson     CeedCallBackend(CeedGetData(ceed, &ceed_data));
12222b730f8bSJeremy L Thompson     CeedCallBackend(cudaGetDeviceProperties(&prop, ceed_data->device_id));
122380a9ef05SNatalie Beams     if ((prop.major < 6) && (CEED_SCALAR_TYPE != CEED_SCALAR_FP32)) {
1224*0183ed61SJeremy L Thompson       code << tab << "// AtomicAdd fallback source\n";
1225*0183ed61SJeremy L Thompson       code << tab << "#include <ceed/jit-source/cuda/cuda-atomic-add-fallback.h>\n\n";
1226f1a13f77SYohann Dudouit     }
12274b3e95d5SJeremy L Thompson   }
1228f1a13f77SYohann Dudouit 
12299e201c85SYohann   // Load basis source files
1230412e5683SJeremy L Thompson   if (!is_all_nontensor) {
1231*0183ed61SJeremy L Thompson     code << tab << "// Tensor basis source\n";
1232*0183ed61SJeremy L Thompson     code << tab << "#include <ceed/jit-source/cuda/cuda-shared-basis-tensor-templates.h>\n\n";
1233412e5683SJeremy L Thompson   }
1234412e5683SJeremy L Thompson   if (!is_all_tensor) {
1235*0183ed61SJeremy L Thompson     code << tab << "// Non-tensor basis source\n";
1236*0183ed61SJeremy L Thompson     code << tab << "#include <ceed/jit-source/cuda/cuda-shared-basis-nontensor-templates.h>\n\n";
1237dc007f05SJeremy L Thompson   }
1238c8e372f0SJeremy L Thompson   if (!is_all_tensor && !is_all_nontensor) {
1239c8e372f0SJeremy L Thompson     code << "// Tensor basis source\n";
1240c8e372f0SJeremy L Thompson     code << "#include <ceed/jit-source/cuda/cuda-shared-basis-tensor-flattened-templates.h>\n\n";
1241c8e372f0SJeremy L Thompson   }
1242dc007f05SJeremy L Thompson   if (is_at_points) {
12438b97b69aSJeremy L Thompson     code << "// AtPoints basis source\n";
12448b97b69aSJeremy L Thompson     code << "#include <ceed/jit-source/cuda/cuda-shared-basis-tensor-at-points-templates.h>\n\n";
1245dc007f05SJeremy L Thompson   }
12469c25dd66SJeremy L Thompson   code << "// CodeGen operator source\n";
12479c25dd66SJeremy L Thompson   code << "#include <ceed/jit-source/cuda/cuda-gen-templates.h>\n\n";
1248241a4b83SYohann 
12494b3e95d5SJeremy L Thompson   // Get QFunction name
12504b3e95d5SJeremy L Thompson   std::string qfunction_name(qf_data->qfunction_name);
12514b3e95d5SJeremy L Thompson   std::string operator_name;
12524b3e95d5SJeremy L Thompson 
125309095acaSJeremy L Thompson   operator_name = "CeedKernelCudaGenOperator_" + qfunction_name;
12544d537eeaSYohann 
12559e201c85SYohann   // Define CEED_Q_VLA
1256*0183ed61SJeremy L Thompson   code << "\n" << tab << "#undef CEED_Q_VLA\n";
1257412e5683SJeremy L Thompson   if (max_dim != 3 || is_at_points || use_3d_slices || !is_all_tensor) {
1258*0183ed61SJeremy L Thompson     code << tab << "#define CEED_Q_VLA 1\n\n";
12599e201c85SYohann   } else {
1260*0183ed61SJeremy L Thompson     code << tab << "#define CEED_Q_VLA " << Q_1d << "\n\n";
12619e201c85SYohann   }
12629e201c85SYohann 
12634b3e95d5SJeremy L Thompson   // Add user QFunction source
12644b3e95d5SJeremy L Thompson   {
12659c25dd66SJeremy L Thompson     const char *source_path;
12664b3e95d5SJeremy L Thompson 
12679c25dd66SJeremy L Thompson     CeedCallBackend(CeedQFunctionGetSourcePath(qf, &source_path));
12689c25dd66SJeremy L Thompson     CeedCheck(source_path, ceed, CEED_ERROR_UNSUPPORTED, "/gpu/cuda/gen backend requires QFunction source code file");
12699c25dd66SJeremy L Thompson 
1270*0183ed61SJeremy L Thompson     code << tab << "// User QFunction source\n";
1271*0183ed61SJeremy L Thompson     code << tab << "#include \"" << source_path << "\"\n\n";
12724b3e95d5SJeremy L Thompson   }
1273241a4b83SYohann 
1274241a4b83SYohann   // Setup
1275*0183ed61SJeremy L Thompson   code << "\n" << tab << "// -----------------------------------------------------------------------------\n";
1276*0183ed61SJeremy L Thompson   code << tab << "// Operator Kernel\n";
1277*0183ed61SJeremy L Thompson   code << tab << "// \n";
1278*0183ed61SJeremy L Thompson   code << tab << "// d_[in,out]_i:   CeedVector device array\n";
1279*0183ed61SJeremy L Thompson   code << tab << "// r_[in,out]_e_i: Element vector register\n";
1280*0183ed61SJeremy L Thompson   code << tab << "// r_[in,out]_q_i: Quadrature space vector register\n";
1281*0183ed61SJeremy L Thompson   code << tab << "// r_[in,out]_c_i: AtPoints Chebyshev coefficients register\n";
1282*0183ed61SJeremy L Thompson   code << tab << "// r_[in,out]_s_i: Quadrature space slice vector register\n";
1283*0183ed61SJeremy L Thompson   code << tab << "// \n";
1284*0183ed61SJeremy L Thompson   code << tab << "// s_B_[in,out]_i: Interpolation matrix, shared memory\n";
1285*0183ed61SJeremy L Thompson   code << tab << "// s_G_[in,out]_i: Gradient matrix, shared memory\n";
1286*0183ed61SJeremy L Thompson   code << tab << "// -----------------------------------------------------------------------------\n";
1287*0183ed61SJeremy L Thompson   code << tab << "extern \"C\" __global__ void " << operator_name
12888b97b69aSJeremy L Thompson        << "(CeedInt num_elem, void* ctx, FieldsInt_Cuda indices, Fields_Cuda fields, Fields_Cuda B, Fields_Cuda G, CeedScalar *W, Points_Cuda "
12898b97b69aSJeremy L Thompson           "points) {\n";
1290*0183ed61SJeremy L Thompson   tab.push();
12914b3e95d5SJeremy L Thompson 
12924b3e95d5SJeremy L Thompson   // Scratch buffers
12939e201c85SYohann   for (CeedInt i = 0; i < num_input_fields; i++) {
12944b3e95d5SJeremy L Thompson     CeedEvalMode eval_mode;
12954b3e95d5SJeremy L Thompson 
12962b730f8bSJeremy L Thompson     CeedCallBackend(CeedQFunctionFieldGetEvalMode(qf_input_fields[i], &eval_mode));
12979e201c85SYohann     if (eval_mode != CEED_EVAL_WEIGHT) {  // Skip CEED_EVAL_WEIGHT
1298*0183ed61SJeremy L Thompson       code << tab << "const CeedScalar *__restrict__ d_in_" << i << " = fields.inputs[" << i << "];\n";
1299241a4b83SYohann     }
1300241a4b83SYohann   }
13019e201c85SYohann   for (CeedInt i = 0; i < num_output_fields; i++) {
1302*0183ed61SJeremy L Thompson     code << tab << "CeedScalar *__restrict__ d_out_" << i << " = fields.outputs[" << i << "];\n";
1303241a4b83SYohann   }
13041da99368SJeremy L Thompson 
1305*0183ed61SJeremy L Thompson   code << tab << "const CeedInt max_dim = " << max_dim << ";\n";
1306412e5683SJeremy L Thompson   if (!is_all_tensor) {
1307*0183ed61SJeremy L Thompson     code << tab << "const CeedInt Q = " << Q << ";\n";
1308412e5683SJeremy L Thompson   }
1309412e5683SJeremy L Thompson   if (!is_all_nontensor) {
1310*0183ed61SJeremy L Thompson     code << tab << "const CeedInt Q_1d = " << Q_1d << ";\n";
1311412e5683SJeremy L Thompson   }
13128b97b69aSJeremy L Thompson   if (is_at_points) {
1313*0183ed61SJeremy L Thompson     code << tab << "const CeedInt max_num_points = " << max_num_points << ";\n";
1314*0183ed61SJeremy L Thompson     code << tab << "const CeedInt coords_comp_stride = " << coords_comp_stride << ";\n";
13158b97b69aSJeremy L Thompson   }
13161da99368SJeremy L Thompson 
13174b3e95d5SJeremy L Thompson   // Shared data
1318*0183ed61SJeremy L Thompson   code << tab << "extern __shared__ CeedScalar slice[];\n";
1319*0183ed61SJeremy L Thompson   code << tab << "SharedData_Cuda data;\n";
1320*0183ed61SJeremy L Thompson   code << tab << "data.t_id_x = threadIdx.x;\n";
1321*0183ed61SJeremy L Thompson   code << tab << "data.t_id_y = threadIdx.y;\n";
1322*0183ed61SJeremy L Thompson   code << tab << "data.t_id_z = threadIdx.z;\n";
1323*0183ed61SJeremy L Thompson   code << tab << "data.t_id   = threadIdx.x + threadIdx.y*blockDim.x + threadIdx.z*blockDim.y*blockDim.x;\n";
1324*0183ed61SJeremy L Thompson   code << tab << "data.slice  = slice + data.t_id_z*OP_T_1D" << ((!is_all_tensor || max_dim == 1) ? "" : "*OP_T_1D") << ";\n";
1325920dcdc4Sjeremylt 
13260a2a6492SJeremy L Thompson   // -- Determine input mat reuse
132745a787f7SJeremy L Thompson   FieldReuse_Cuda input_matrix_reuse[CEED_FIELD_MAX];
13280a2a6492SJeremy L Thompson 
13290a2a6492SJeremy L Thompson   for (CeedInt i = 0; i < num_input_fields; i++) {
133045a787f7SJeremy L Thompson     input_matrix_reuse[i].index = -1;
13310a2a6492SJeremy L Thompson   }
13320a2a6492SJeremy L Thompson   for (CeedInt i = 0; i < num_input_fields; i++) {
1333412e5683SJeremy L Thompson     bool         is_tensor = true;
13340a2a6492SJeremy L Thompson     CeedEvalMode eval_mode_i;
13350a2a6492SJeremy L Thompson     CeedBasis    basis_i;
13360a2a6492SJeremy L Thompson 
13370a2a6492SJeremy L Thompson     CeedCallBackend(CeedQFunctionFieldGetEvalMode(qf_input_fields[i], &eval_mode_i));
13380a2a6492SJeremy L Thompson     if (eval_mode_i == CEED_EVAL_WEIGHT) continue;
13390a2a6492SJeremy L Thompson     CeedCallBackend(CeedOperatorFieldGetBasis(op_input_fields[i], &basis_i));
1340412e5683SJeremy L Thompson     CeedCallBackend(CeedBasisIsTensor(basis_i, &is_tensor));
134145a787f7SJeremy L Thompson     for (CeedInt j = 0; (input_matrix_reuse[i].index == -1) && (j < i); j++) {
13420a2a6492SJeremy L Thompson       CeedEvalMode eval_mode_j;
13430a2a6492SJeremy L Thompson       CeedBasis    basis_j;
13440a2a6492SJeremy L Thompson 
13450a2a6492SJeremy L Thompson       CeedCallBackend(CeedQFunctionFieldGetEvalMode(qf_input_fields[j], &eval_mode_j));
13460a2a6492SJeremy L Thompson       if (eval_mode_j == CEED_EVAL_WEIGHT) continue;
13470a2a6492SJeremy L Thompson       CeedCallBackend(CeedOperatorFieldGetBasis(op_input_fields[j], &basis_j));
13480a2a6492SJeremy L Thompson       if (basis_i == basis_j) {
13490a2a6492SJeremy L Thompson         if (is_tensor) {
135045a787f7SJeremy L Thompson           input_matrix_reuse[i].index     = j;
135145a787f7SJeremy L Thompson           input_matrix_reuse[i].is_input  = true;
135245a787f7SJeremy L Thompson           input_matrix_reuse[i].eval_mode = eval_mode_j;
13530a2a6492SJeremy L Thompson         } else {
13540a2a6492SJeremy L Thompson           // For non-tensor can only re-use with the same eval mode
13550a2a6492SJeremy L Thompson           if (eval_mode_i == eval_mode_j) {
135645a787f7SJeremy L Thompson             input_matrix_reuse[i].index     = j;
135745a787f7SJeremy L Thompson             input_matrix_reuse[i].is_input  = true;
135845a787f7SJeremy L Thompson             input_matrix_reuse[i].eval_mode = eval_mode_j;
13590a2a6492SJeremy L Thompson           }
13600a2a6492SJeremy L Thompson         }
13610a2a6492SJeremy L Thompson       }
13620a2a6492SJeremy L Thompson       CeedCallBackend(CeedBasisDestroy(&basis_j));
13630a2a6492SJeremy L Thompson     }
13640a2a6492SJeremy L Thompson     CeedCallBackend(CeedBasisDestroy(&basis_i));
13650a2a6492SJeremy L Thompson   }
13660a2a6492SJeremy L Thompson 
13670a2a6492SJeremy L Thompson   // -- Determine output mat reuse
136845a787f7SJeremy L Thompson   FieldReuse_Cuda output_matrix_reuse[CEED_FIELD_MAX];
13690a2a6492SJeremy L Thompson 
13700a2a6492SJeremy L Thompson   for (CeedInt i = 0; i < num_output_fields; i++) {
137145a787f7SJeremy L Thompson     output_matrix_reuse[i].index = -1;
13720a2a6492SJeremy L Thompson   }
13730a2a6492SJeremy L Thompson   for (CeedInt i = 0; i < num_output_fields; i++) {
1374412e5683SJeremy L Thompson     bool         is_tensor = true;
13750a2a6492SJeremy L Thompson     CeedEvalMode eval_mode_i;
13760a2a6492SJeremy L Thompson     CeedBasis    basis_i;
13770a2a6492SJeremy L Thompson 
13780a2a6492SJeremy L Thompson     CeedCallBackend(CeedQFunctionFieldGetEvalMode(qf_output_fields[i], &eval_mode_i));
13790a2a6492SJeremy L Thompson     CeedCallBackend(CeedOperatorFieldGetBasis(op_output_fields[i], &basis_i));
1380412e5683SJeremy L Thompson     CeedCallBackend(CeedBasisIsTensor(basis_i, &is_tensor));
138145a787f7SJeremy L Thompson     for (CeedInt j = 0; (output_matrix_reuse[i].index == -1) && (j < num_input_fields); j++) {
13820a2a6492SJeremy L Thompson       CeedEvalMode eval_mode_j;
13830a2a6492SJeremy L Thompson       CeedBasis    basis_j;
13840a2a6492SJeremy L Thompson 
13850a2a6492SJeremy L Thompson       CeedCallBackend(CeedQFunctionFieldGetEvalMode(qf_input_fields[j], &eval_mode_j));
13860a2a6492SJeremy L Thompson       if (eval_mode_j == CEED_EVAL_WEIGHT) continue;
13870a2a6492SJeremy L Thompson       CeedCallBackend(CeedOperatorFieldGetBasis(op_input_fields[j], &basis_j));
13880a2a6492SJeremy L Thompson       if (basis_i == basis_j) {
13890a2a6492SJeremy L Thompson         if (is_tensor) {
139045a787f7SJeremy L Thompson           output_matrix_reuse[i].index     = j;
139145a787f7SJeremy L Thompson           output_matrix_reuse[i].is_input  = true;
139245a787f7SJeremy L Thompson           output_matrix_reuse[i].eval_mode = eval_mode_j;
13930a2a6492SJeremy L Thompson         } else {
13940a2a6492SJeremy L Thompson           // For non-tensor can only re-use with the same eval mode
13950a2a6492SJeremy L Thompson           if (eval_mode_i == eval_mode_j) {
139645a787f7SJeremy L Thompson             output_matrix_reuse[i].index     = j;
139745a787f7SJeremy L Thompson             output_matrix_reuse[i].is_input  = true;
139845a787f7SJeremy L Thompson             output_matrix_reuse[i].eval_mode = eval_mode_j;
13990a2a6492SJeremy L Thompson           }
14000a2a6492SJeremy L Thompson         }
14010a2a6492SJeremy L Thompson       }
14020a2a6492SJeremy L Thompson       CeedCallBackend(CeedBasisDestroy(&basis_j));
14030a2a6492SJeremy L Thompson     }
140445a787f7SJeremy L Thompson     for (CeedInt j = 0; (output_matrix_reuse[i].index == -1) && (j < i); j++) {
14050a2a6492SJeremy L Thompson       CeedEvalMode eval_mode_j;
14060a2a6492SJeremy L Thompson       CeedBasis    basis_j;
14070a2a6492SJeremy L Thompson 
14080a2a6492SJeremy L Thompson       CeedCallBackend(CeedQFunctionFieldGetEvalMode(qf_output_fields[j], &eval_mode_j));
14090a2a6492SJeremy L Thompson       if (eval_mode_j == CEED_EVAL_WEIGHT) continue;
14100a2a6492SJeremy L Thompson       CeedCallBackend(CeedOperatorFieldGetBasis(op_output_fields[j], &basis_j));
14110a2a6492SJeremy L Thompson       if (basis_i == basis_j) {
14120a2a6492SJeremy L Thompson         if (is_tensor) {
141345a787f7SJeremy L Thompson           output_matrix_reuse[i].index     = j;
141445a787f7SJeremy L Thompson           output_matrix_reuse[i].is_input  = false;
141545a787f7SJeremy L Thompson           output_matrix_reuse[i].eval_mode = eval_mode_j;
14160a2a6492SJeremy L Thompson         } else {
14170a2a6492SJeremy L Thompson           // For non-tensor can only re-use with the same eval mode
14180a2a6492SJeremy L Thompson           if (eval_mode_i == eval_mode_j) {
141945a787f7SJeremy L Thompson             output_matrix_reuse[i].index     = j;
142045a787f7SJeremy L Thompson             output_matrix_reuse[i].is_input  = false;
142145a787f7SJeremy L Thompson             output_matrix_reuse[i].eval_mode = eval_mode_j;
14220a2a6492SJeremy L Thompson           }
14230a2a6492SJeremy L Thompson         }
14240a2a6492SJeremy L Thompson       }
14250a2a6492SJeremy L Thompson       CeedCallBackend(CeedBasisDestroy(&basis_j));
14260a2a6492SJeremy L Thompson     }
14270a2a6492SJeremy L Thompson     CeedCallBackend(CeedBasisDestroy(&basis_i));
14280a2a6492SJeremy L Thompson   }
14290a2a6492SJeremy L Thompson 
1430ac421f39SYohann   // Initialize constants, and matrices B and G
1431*0183ed61SJeremy L Thompson   code << "\n" << tab << "// Input field constants and basis data\n";
14329e201c85SYohann   for (CeedInt i = 0; i < num_input_fields; i++) {
1433*0183ed61SJeremy L Thompson     CeedCallBackend(CeedOperatorBuildKernelFieldData_Cuda_gen(code, data, tab, i, op_input_fields[i], qf_input_fields[i], input_matrix_reuse[i],
1434*0183ed61SJeremy L Thompson                                                               max_dim, Q, Q_1d, true, is_all_tensor, is_at_points, use_3d_slices));
1435920dcdc4Sjeremylt   }
1436*0183ed61SJeremy L Thompson   code << "\n" << tab << "// Output field constants and basis data\n";
14379e201c85SYohann   for (CeedInt i = 0; i < num_output_fields; i++) {
1438*0183ed61SJeremy L Thompson     CeedCallBackend(CeedOperatorBuildKernelFieldData_Cuda_gen(code, data, tab, i, op_output_fields[i], qf_output_fields[i], output_matrix_reuse[i],
14398014c5e7SJeremy L Thompson                                                               max_dim, Q, Q_1d, false, is_all_tensor, is_at_points, use_3d_slices));
14404b3e95d5SJeremy L Thompson   }
1441920dcdc4Sjeremylt 
14424b3e95d5SJeremy L Thompson   // Loop over all elements
1443*0183ed61SJeremy L Thompson   code << "\n" << tab << "// Element loop\n";
1444*0183ed61SJeremy L Thompson   code << tab << "__syncthreads();\n";
1445*0183ed61SJeremy L Thompson   code << tab << "for (CeedInt elem = blockIdx.x*blockDim.z + threadIdx.z; elem < num_elem; elem += gridDim.x*blockDim.z) {\n";
1446*0183ed61SJeremy L Thompson   tab.push();
14474b3e95d5SJeremy L Thompson 
1448e93651e5SJeremy L Thompson   // -- Compute minimum buffer space needed
14498b97b69aSJeremy L Thompson   CeedInt max_rstr_buffer_size = 1;
1450e93651e5SJeremy L Thompson 
14519e201c85SYohann   for (CeedInt i = 0; i < num_input_fields; i++) {
14526de40545SJeremy L Thompson     CeedEvalMode eval_mode;
14536de40545SJeremy L Thompson 
14546de40545SJeremy L Thompson     CeedCallBackend(CeedQFunctionFieldGetEvalMode(qf_input_fields[i], &eval_mode));
14556de40545SJeremy L Thompson     if (eval_mode != CEED_EVAL_NONE && eval_mode != CEED_EVAL_WEIGHT) {
1456a61b1c91SJeremy L Thompson       CeedInt             num_comp;
1457e93651e5SJeremy L Thompson       CeedElemRestriction elem_rstr;
1458e93651e5SJeremy L Thompson 
1459e93651e5SJeremy L Thompson       CeedCallBackend(CeedOperatorFieldGetElemRestriction(op_input_fields[i], &elem_rstr));
1460e93651e5SJeremy L Thompson       CeedCallBackend(CeedElemRestrictionGetNumComponents(elem_rstr, &num_comp));
1461a61b1c91SJeremy L Thompson       max_rstr_buffer_size = CeedIntMax(max_rstr_buffer_size, num_comp * (is_all_tensor && (max_dim >= 3) ? Q_1d : 1));
1462681d0ea7SJeremy L Thompson       CeedCallBackend(CeedElemRestrictionDestroy(&elem_rstr));
1463e93651e5SJeremy L Thompson     }
14646de40545SJeremy L Thompson   }
1465e93651e5SJeremy L Thompson   for (CeedInt i = 0; i < num_output_fields; i++) {
14666de40545SJeremy L Thompson     CeedEvalMode eval_mode;
14676de40545SJeremy L Thompson 
14686de40545SJeremy L Thompson     CeedCallBackend(CeedQFunctionFieldGetEvalMode(qf_output_fields[i], &eval_mode));
14696de40545SJeremy L Thompson     if (eval_mode != CEED_EVAL_NONE) {
1470a61b1c91SJeremy L Thompson       CeedInt             num_comp;
1471e93651e5SJeremy L Thompson       CeedElemRestriction elem_rstr;
1472e93651e5SJeremy L Thompson 
1473e93651e5SJeremy L Thompson       CeedCallBackend(CeedOperatorFieldGetElemRestriction(op_output_fields[i], &elem_rstr));
1474e93651e5SJeremy L Thompson       CeedCallBackend(CeedElemRestrictionGetNumComponents(elem_rstr, &num_comp));
1475a61b1c91SJeremy L Thompson       max_rstr_buffer_size = CeedIntMax(max_rstr_buffer_size, num_comp * (is_all_tensor && (max_dim >= 3) ? Q_1d : 1));
1476681d0ea7SJeremy L Thompson       CeedCallBackend(CeedElemRestrictionDestroy(&elem_rstr));
1477e93651e5SJeremy L Thompson     }
14786de40545SJeremy L Thompson   }
1479*0183ed61SJeremy L Thompson   code << tab << "// Scratch restriction buffer space\n";
1480*0183ed61SJeremy L Thompson   code << tab << "CeedScalar r_e_scratch[" << max_rstr_buffer_size << "];\n";
1481e93651e5SJeremy L Thompson 
1482e93651e5SJeremy L Thompson   // -- Determine best input field processing order
1483e93651e5SJeremy L Thompson   CeedInt field_rstr_in_buffer[CEED_FIELD_MAX], input_field_order[CEED_FIELD_MAX];
1484e93651e5SJeremy L Thompson 
1485e93651e5SJeremy L Thompson   for (CeedInt i = 0; i < num_input_fields; i++) {
1486e93651e5SJeremy L Thompson     field_rstr_in_buffer[i] = -1;
1487e93651e5SJeremy L Thompson     input_field_order[i]    = -1;
1488e93651e5SJeremy L Thompson   }
1489e93651e5SJeremy L Thompson   {
1490e93651e5SJeremy L Thompson     bool    is_ordered[CEED_FIELD_MAX];
1491e93651e5SJeremy L Thompson     CeedInt curr_index = 0;
1492e93651e5SJeremy L Thompson 
1493e93651e5SJeremy L Thompson     for (CeedInt i = 0; i < num_input_fields; i++) is_ordered[i] = false;
1494e93651e5SJeremy L Thompson     for (CeedInt i = 0; i < num_input_fields; i++) {
1495e93651e5SJeremy L Thompson       CeedVector          vec_i;
1496e93651e5SJeremy L Thompson       CeedElemRestriction rstr_i;
1497e93651e5SJeremy L Thompson 
1498e93651e5SJeremy L Thompson       if (is_ordered[i]) continue;
1499e93651e5SJeremy L Thompson       field_rstr_in_buffer[i]       = i;
1500e93651e5SJeremy L Thompson       is_ordered[i]                 = true;
1501e93651e5SJeremy L Thompson       input_field_order[curr_index] = i;
1502e93651e5SJeremy L Thompson       curr_index++;
1503034f99fdSJeremy L Thompson       CeedCallBackend(CeedOperatorFieldGetVector(op_input_fields[i], &vec_i));
1504e93651e5SJeremy L Thompson       if (vec_i == CEED_VECTOR_NONE) continue;  // CEED_EVAL_WEIGHT
1505e93651e5SJeremy L Thompson       CeedCallBackend(CeedOperatorFieldGetElemRestriction(op_input_fields[i], &rstr_i));
1506e93651e5SJeremy L Thompson       for (CeedInt j = i + 1; j < num_input_fields; j++) {
1507e93651e5SJeremy L Thompson         CeedVector          vec_j;
1508e93651e5SJeremy L Thompson         CeedElemRestriction rstr_j;
1509e93651e5SJeremy L Thompson 
1510e93651e5SJeremy L Thompson         CeedCallBackend(CeedOperatorFieldGetVector(op_input_fields[j], &vec_j));
1511e93651e5SJeremy L Thompson         CeedCallBackend(CeedOperatorFieldGetElemRestriction(op_input_fields[j], &rstr_j));
1512e93651e5SJeremy L Thompson         if (rstr_i == rstr_j && vec_i == vec_j) {
1513e93651e5SJeremy L Thompson           field_rstr_in_buffer[j]       = i;
1514e93651e5SJeremy L Thompson           is_ordered[j]                 = true;
1515e93651e5SJeremy L Thompson           input_field_order[curr_index] = j;
1516e93651e5SJeremy L Thompson           curr_index++;
1517e93651e5SJeremy L Thompson         }
1518681d0ea7SJeremy L Thompson         CeedCallBackend(CeedVectorDestroy(&vec_j));
1519681d0ea7SJeremy L Thompson         CeedCallBackend(CeedElemRestrictionDestroy(&rstr_j));
1520e93651e5SJeremy L Thompson       }
1521681d0ea7SJeremy L Thompson       CeedCallBackend(CeedVectorDestroy(&vec_i));
1522681d0ea7SJeremy L Thompson       CeedCallBackend(CeedElemRestrictionDestroy(&rstr_i));
1523e93651e5SJeremy L Thompson     }
1524e93651e5SJeremy L Thompson   }
1525e93651e5SJeremy L Thompson 
1526e93651e5SJeremy L Thompson   // -- Input restriction and basis
1527*0183ed61SJeremy L Thompson   code << "\n" << tab << "// -- Input field restrictions and basis actions\n";
1528e93651e5SJeremy L Thompson   for (CeedInt i = 0; i < num_input_fields; i++) {
152959fa3f92SJeremy L Thompson     const char   *field_name;
153059fa3f92SJeremy L Thompson     const CeedInt f = input_field_order[i];
1531e93651e5SJeremy L Thompson 
153259fa3f92SJeremy L Thompson     CeedCallBackend(CeedOperatorFieldGetName(op_input_fields[f], &field_name));
1533*0183ed61SJeremy L Thompson     code << tab << "// ---- Input field " << f << ": " << field_name << "\n";
1534920dcdc4Sjeremylt 
15354b3e95d5SJeremy L Thompson     // ---- Restriction
1536*0183ed61SJeremy L Thompson     CeedCallBackend(CeedOperatorBuildKernelRestriction_Cuda_gen(code, data, tab, f, field_rstr_in_buffer, op_input_fields[f], qf_input_fields[f],
1537*0183ed61SJeremy L Thompson                                                                 max_dim, Q_1d, true, is_all_tensor, is_at_points, use_3d_slices));
15389a2291e3SJeremy L Thompson 
15394b3e95d5SJeremy L Thompson     // ---- Basis action
1540*0183ed61SJeremy L Thompson     CeedCallBackend(CeedOperatorBuildKernelBasis_Cuda_gen(code, data, tab, f, op_input_fields[f], qf_input_fields[f], max_dim, Q_1d, true,
1541*0183ed61SJeremy L Thompson                                                           is_all_tensor, is_at_points, use_3d_slices));
1542920dcdc4Sjeremylt   }
1543920dcdc4Sjeremylt 
15444b3e95d5SJeremy L Thompson   // -- Q function
1545*0183ed61SJeremy L Thompson   CeedCallBackend(CeedOperatorBuildKernelQFunction_Cuda_gen(code, data, tab, max_dim, max_num_points, num_input_fields, op_input_fields,
1546*0183ed61SJeremy L Thompson                                                             qf_input_fields, num_output_fields, op_output_fields, qf_output_fields, qfunction_name,
1547*0183ed61SJeremy L Thompson                                                             Q_1d, is_all_tensor, is_at_points, use_3d_slices));
1548ca735530SJeremy L Thompson 
15494b3e95d5SJeremy L Thompson   // -- Output basis and restriction
1550*0183ed61SJeremy L Thompson   code << "\n" << tab << "// -- Output field basis action and restrictions\n";
15519e201c85SYohann   for (CeedInt i = 0; i < num_output_fields; i++) {
155259fa3f92SJeremy L Thompson     const char *field_name;
155359fa3f92SJeremy L Thompson 
155459fa3f92SJeremy L Thompson     CeedCallBackend(CeedOperatorFieldGetName(op_output_fields[i], &field_name));
1555*0183ed61SJeremy L Thompson     code << tab << "// ---- Output field " << i << ": " << field_name << "\n";
1556ca735530SJeremy L Thompson 
15574b3e95d5SJeremy L Thompson     // ---- Basis action
1558*0183ed61SJeremy L Thompson     CeedCallBackend(CeedOperatorBuildKernelBasis_Cuda_gen(code, data, tab, i, op_output_fields[i], qf_output_fields[i], max_dim, Q_1d, false,
1559412e5683SJeremy L Thompson                                                           is_all_tensor, is_at_points, use_3d_slices));
15609a2291e3SJeremy L Thompson 
15614b3e95d5SJeremy L Thompson     // ---- Restriction
1562*0183ed61SJeremy L Thompson     CeedCallBackend(CeedOperatorBuildKernelRestriction_Cuda_gen(code, data, tab, i, NULL, op_output_fields[i], qf_output_fields[i], max_dim, Q_1d,
1563*0183ed61SJeremy L Thompson                                                                 false, is_all_tensor, is_at_points, use_3d_slices));
1564ac421f39SYohann   }
1565241a4b83SYohann 
15664b3e95d5SJeremy L Thompson   // Close loop and function
1567*0183ed61SJeremy L Thompson   tab.pop();
1568*0183ed61SJeremy L Thompson   code << tab << "}\n";
1569*0183ed61SJeremy L Thompson   tab.pop();
1570*0183ed61SJeremy L Thompson   code << tab << "}\n";
1571*0183ed61SJeremy L Thompson   code << tab << "// -----------------------------------------------------------------------------\n\n";
1572241a4b83SYohann 
15733a2968d6SJeremy L Thompson   // Compile
1574ddae5012SJeremy L Thompson   {
1575ddae5012SJeremy L Thompson     bool          is_compile_good = false;
1576412e5683SJeremy L Thompson     const CeedInt T_1d            = CeedIntMax(is_all_tensor ? Q_1d : Q, data->max_P_1d);
1577ddae5012SJeremy L Thompson 
1578a61b1c91SJeremy L Thompson     data->thread_1d = T_1d;
1579412e5683SJeremy L Thompson     CeedCallBackend(CeedTryCompile_Cuda(ceed, code.str().c_str(), &is_compile_good, &data->module, 1, "OP_T_1D", T_1d));
1580ddae5012SJeremy L Thompson     if (is_compile_good) {
1581ddae5012SJeremy L Thompson       *is_good_build = true;
1582eb7e6cafSJeremy L Thompson       CeedCallBackend(CeedGetKernel_Cuda(ceed, data->module, operator_name.c_str(), &data->op));
1583ddae5012SJeremy L Thompson     } else {
1584ddae5012SJeremy L Thompson       *is_good_build     = false;
1585ddae5012SJeremy L Thompson       data->use_fallback = true;
1586ddae5012SJeremy L Thompson     }
1587ddae5012SJeremy L Thompson   }
15882b730f8bSJeremy L Thompson   CeedCallBackend(CeedOperatorSetSetupDone(op));
15899bc66399SJeremy L Thompson   CeedCallBackend(CeedDestroy(&ceed));
1590c11e12f4SJeremy L Thompson   CeedCallBackend(CeedQFunctionDestroy(&qf));
1591e15f9bd0SJeremy L Thompson   return CEED_ERROR_SUCCESS;
1592241a4b83SYohann }
15932a86cc9dSSebastian Grimberg 
1594ab213215SJeremy L Thompson //------------------------------------------------------------------------------
1595*0183ed61SJeremy L Thompson // Build AtPoints assembly operator kernel
1596*0183ed61SJeremy L Thompson //------------------------------------------------------------------------------
1597*0183ed61SJeremy L Thompson static int CeedOperatorBuildKernelAssemblyAtPoints_Cuda_gen(CeedOperator op, bool is_full, bool *is_good_build) {
1598*0183ed61SJeremy L Thompson   bool                    is_all_tensor = true, is_at_points = false, use_3d_slices = false;
1599*0183ed61SJeremy L Thompson   Ceed                    ceed;
1600*0183ed61SJeremy L Thompson   CeedInt                 Q, Q_1d, num_input_fields, num_output_fields, max_dim = 1, max_num_points = 0, coords_comp_stride = 0;
1601*0183ed61SJeremy L Thompson   CeedQFunctionField     *qf_input_fields, *qf_output_fields;
1602*0183ed61SJeremy L Thompson   CeedQFunction_Cuda_gen *qf_data;
1603*0183ed61SJeremy L Thompson   CeedQFunction           qf;
1604*0183ed61SJeremy L Thompson   CeedOperatorField      *op_input_fields, *op_output_fields;
1605*0183ed61SJeremy L Thompson   CeedOperator_Cuda_gen  *data;
1606*0183ed61SJeremy L Thompson   std::ostringstream      code;
1607*0183ed61SJeremy L Thompson   Tab                     tab;
1608*0183ed61SJeremy L Thompson 
1609*0183ed61SJeremy L Thompson   // Check compatibility
1610*0183ed61SJeremy L Thompson   CeedCallBackend(CeedOperatorGetCeed(op, &ceed));
1611*0183ed61SJeremy L Thompson   CeedCallBackend(CeedOperatorIsAtPoints(op, &is_at_points));
1612*0183ed61SJeremy L Thompson   CeedCheck(is_at_points, ceed, CEED_ERROR_BACKEND, "Only AtPoints operator assembly supported");
1613*0183ed61SJeremy L Thompson 
1614*0183ed61SJeremy L Thompson   // Retrieve operator data
1615*0183ed61SJeremy L Thompson   CeedCallBackend(CeedOperatorGetData(op, &data));
1616*0183ed61SJeremy L Thompson   Q       = data->Q;
1617*0183ed61SJeremy L Thompson   Q_1d    = data->Q_1d;
1618*0183ed61SJeremy L Thompson   max_dim = data->dim;
1619*0183ed61SJeremy L Thompson   {
1620*0183ed61SJeremy L Thompson     CeedElemRestriction rstr_points = NULL;
1621*0183ed61SJeremy L Thompson 
1622*0183ed61SJeremy L Thompson     CeedCallBackend(CeedOperatorAtPointsGetPoints(op, &rstr_points, NULL));
1623*0183ed61SJeremy L Thompson     CeedCallBackend(CeedElemRestrictionGetMaxPointsInElement(rstr_points, &max_num_points));
1624*0183ed61SJeremy L Thompson     CeedCallBackend(CeedElemRestrictionGetCompStride(rstr_points, &coords_comp_stride));
1625*0183ed61SJeremy L Thompson     CeedCallBackend(CeedElemRestrictionDestroy(&rstr_points));
1626*0183ed61SJeremy L Thompson   }
1627*0183ed61SJeremy L Thompson   CeedCallBackend(CeedOperatorGetQFunction(op, &qf));
1628*0183ed61SJeremy L Thompson   CeedCallBackend(CeedQFunctionGetData(qf, &qf_data));
1629*0183ed61SJeremy L Thompson   CeedCallBackend(CeedQFunctionGetFields(qf, NULL, &qf_input_fields, NULL, &qf_output_fields));
1630*0183ed61SJeremy L Thompson   CeedCallBackend(CeedOperatorGetFields(op, &num_input_fields, &op_input_fields, &num_output_fields, &op_output_fields));
1631*0183ed61SJeremy L Thompson 
1632*0183ed61SJeremy L Thompson   // Add atomicAdd function for old NVidia architectures
1633*0183ed61SJeremy L Thompson   {
1634*0183ed61SJeremy L Thompson     Ceed_Cuda            *ceed_data;
1635*0183ed61SJeremy L Thompson     struct cudaDeviceProp prop;
1636*0183ed61SJeremy L Thompson 
1637*0183ed61SJeremy L Thompson     CeedCallBackend(CeedGetData(ceed, &ceed_data));
1638*0183ed61SJeremy L Thompson     CeedCallBackend(cudaGetDeviceProperties(&prop, ceed_data->device_id));
1639*0183ed61SJeremy L Thompson     if ((prop.major < 6) && (CEED_SCALAR_TYPE != CEED_SCALAR_FP32)) {
1640*0183ed61SJeremy L Thompson       code << tab << "// AtomicAdd fallback source\n";
1641*0183ed61SJeremy L Thompson       code << tab << "#include <ceed/jit-source/cuda/cuda-atomic-add-fallback.h>\n\n";
1642*0183ed61SJeremy L Thompson     }
1643*0183ed61SJeremy L Thompson   }
1644*0183ed61SJeremy L Thompson 
1645*0183ed61SJeremy L Thompson   // Load basis source files
1646*0183ed61SJeremy L Thompson   code << tab << "// Tensor basis source\n";
1647*0183ed61SJeremy L Thompson   code << tab << "#include <ceed/jit-source/cuda/cuda-shared-basis-tensor-templates.h>\n\n";
1648*0183ed61SJeremy L Thompson   code << tab << "// AtPoints basis source\n";
1649*0183ed61SJeremy L Thompson   code << tab << "#include <ceed/jit-source/cuda/cuda-shared-basis-tensor-at-points-templates.h>\n\n";
1650*0183ed61SJeremy L Thompson   code << tab << "// CodeGen operator source\n";
1651*0183ed61SJeremy L Thompson   code << tab << "#include <ceed/jit-source/cuda/cuda-gen-templates.h>\n\n";
1652*0183ed61SJeremy L Thompson 
1653*0183ed61SJeremy L Thompson   // Get QFunction name
1654*0183ed61SJeremy L Thompson   std::string qfunction_name(qf_data->qfunction_name);
1655*0183ed61SJeremy L Thompson   std::string operator_name;
1656*0183ed61SJeremy L Thompson 
1657*0183ed61SJeremy L Thompson   if (is_full) {
1658*0183ed61SJeremy L Thompson     operator_name = "CeedKernelCudaGenOperatorFullAssembly_" + qfunction_name;
1659*0183ed61SJeremy L Thompson   } else {
1660*0183ed61SJeremy L Thompson     operator_name = "CeedKernelCudaGenOperatorDiagonalAssembly_" + qfunction_name;
1661*0183ed61SJeremy L Thompson   }
1662*0183ed61SJeremy L Thompson 
1663*0183ed61SJeremy L Thompson   // Define CEED_Q_VLA
1664*0183ed61SJeremy L Thompson   code << "\n" << tab << "#undef CEED_Q_VLA\n";
1665*0183ed61SJeremy L Thompson   code << tab << "#define CEED_Q_VLA 1\n\n";
1666*0183ed61SJeremy L Thompson 
1667*0183ed61SJeremy L Thompson   // Add user QFunction source
1668*0183ed61SJeremy L Thompson   {
1669*0183ed61SJeremy L Thompson     const char *source_path;
1670*0183ed61SJeremy L Thompson 
1671*0183ed61SJeremy L Thompson     CeedCallBackend(CeedQFunctionGetSourcePath(qf, &source_path));
1672*0183ed61SJeremy L Thompson     CeedCheck(source_path, ceed, CEED_ERROR_UNSUPPORTED, "/gpu/cuda/gen backend requires QFunction source code file");
1673*0183ed61SJeremy L Thompson 
1674*0183ed61SJeremy L Thompson     code << tab << "// User QFunction source\n";
1675*0183ed61SJeremy L Thompson     code << tab << "#include \"" << source_path << "\"\n\n";
1676*0183ed61SJeremy L Thompson   }
1677*0183ed61SJeremy L Thompson 
1678*0183ed61SJeremy L Thompson   // Setup
1679*0183ed61SJeremy L Thompson   code << "\n" << tab << "// -----------------------------------------------------------------------------\n";
1680*0183ed61SJeremy L Thompson   code << tab << "// Operator Assembly Kernel\n";
1681*0183ed61SJeremy L Thompson   code << tab << "// \n";
1682*0183ed61SJeremy L Thompson   code << tab << "// d_[in,out]_i:   CeedVector device array\n";
1683*0183ed61SJeremy L Thompson   code << tab << "// r_[in,out]_e_i: Element vector register\n";
1684*0183ed61SJeremy L Thompson   code << tab << "// r_[in,out]_q_i: Quadrature space vector register\n";
1685*0183ed61SJeremy L Thompson   code << tab << "// r_[in,out]_c_i: AtPoints Chebyshev coefficients register\n";
1686*0183ed61SJeremy L Thompson   code << tab << "// r_[in,out]_s_i: Quadrature space slice vector register\n";
1687*0183ed61SJeremy L Thompson   code << tab << "// \n";
1688*0183ed61SJeremy L Thompson   code << tab << "// s_B_[in,out]_i: Interpolation matrix, shared memory\n";
1689*0183ed61SJeremy L Thompson   code << tab << "// s_G_[in,out]_i: Gradient matrix, shared memory\n";
1690*0183ed61SJeremy L Thompson   code << tab << "// -----------------------------------------------------------------------------\n";
1691*0183ed61SJeremy L Thompson   code << tab << "extern \"C\" __global__ void " << operator_name
1692*0183ed61SJeremy L Thompson        << "(CeedInt num_elem, void* ctx, FieldsInt_Cuda indices, Fields_Cuda fields, Fields_Cuda B, Fields_Cuda G, CeedScalar *W, Points_Cuda "
1693*0183ed61SJeremy L Thompson           "points, CeedScalar *__restrict__ values_array) {\n";
1694*0183ed61SJeremy L Thompson   tab.push();
1695*0183ed61SJeremy L Thompson 
1696*0183ed61SJeremy L Thompson   // Scratch buffers
1697*0183ed61SJeremy L Thompson   for (CeedInt i = 0; i < num_input_fields; i++) {
1698*0183ed61SJeremy L Thompson     CeedEvalMode eval_mode;
1699*0183ed61SJeremy L Thompson 
1700*0183ed61SJeremy L Thompson     CeedCallBackend(CeedQFunctionFieldGetEvalMode(qf_input_fields[i], &eval_mode));
1701*0183ed61SJeremy L Thompson     if (eval_mode != CEED_EVAL_WEIGHT) {  // Skip CEED_EVAL_WEIGHT
1702*0183ed61SJeremy L Thompson       code << tab << "const CeedScalar *__restrict__ d_in_" << i << " = fields.inputs[" << i << "];\n";
1703*0183ed61SJeremy L Thompson     }
1704*0183ed61SJeremy L Thompson   }
1705*0183ed61SJeremy L Thompson   for (CeedInt i = 0; i < num_output_fields; i++) {
1706*0183ed61SJeremy L Thompson     code << tab << "CeedScalar *__restrict__ d_out_" << i << " = fields.outputs[" << i << "];\n";
1707*0183ed61SJeremy L Thompson   }
1708*0183ed61SJeremy L Thompson 
1709*0183ed61SJeremy L Thompson   code << tab << "const CeedInt max_dim = " << max_dim << ";\n";
1710*0183ed61SJeremy L Thompson   code << tab << "const CeedInt Q_1d = " << Q_1d << ";\n";
1711*0183ed61SJeremy L Thompson   code << tab << "const CeedInt max_num_points = " << max_num_points << ";\n";
1712*0183ed61SJeremy L Thompson   code << tab << "const CeedInt coords_comp_stride = " << coords_comp_stride << ";\n";
1713*0183ed61SJeremy L Thompson 
1714*0183ed61SJeremy L Thompson   // Shared data
1715*0183ed61SJeremy L Thompson   code << tab << "extern __shared__ CeedScalar slice[];\n";
1716*0183ed61SJeremy L Thompson   code << tab << "SharedData_Cuda data;\n";
1717*0183ed61SJeremy L Thompson   code << tab << "data.t_id_x = threadIdx.x;\n";
1718*0183ed61SJeremy L Thompson   code << tab << "data.t_id_y = threadIdx.y;\n";
1719*0183ed61SJeremy L Thompson   code << tab << "data.t_id_z = threadIdx.z;\n";
1720*0183ed61SJeremy L Thompson   code << tab << "data.t_id   = threadIdx.x + threadIdx.y*blockDim.x + threadIdx.z*blockDim.y*blockDim.x;\n";
1721*0183ed61SJeremy L Thompson   code << tab << "data.slice  = slice + data.t_id_z*OP_T_1D" << ((!is_all_tensor || max_dim == 1) ? "" : "*OP_T_1D") << ";\n";
1722*0183ed61SJeremy L Thompson 
1723*0183ed61SJeremy L Thompson   // -- Determine input mat reuse
1724*0183ed61SJeremy L Thompson   FieldReuse_Cuda input_matrix_reuse[CEED_FIELD_MAX];
1725*0183ed61SJeremy L Thompson 
1726*0183ed61SJeremy L Thompson   for (CeedInt i = 0; i < num_input_fields; i++) {
1727*0183ed61SJeremy L Thompson     input_matrix_reuse[i].index = -1;
1728*0183ed61SJeremy L Thompson   }
1729*0183ed61SJeremy L Thompson   for (CeedInt i = 0; i < num_input_fields; i++) {
1730*0183ed61SJeremy L Thompson     CeedEvalMode eval_mode_i;
1731*0183ed61SJeremy L Thompson     CeedBasis    basis_i;
1732*0183ed61SJeremy L Thompson 
1733*0183ed61SJeremy L Thompson     CeedCallBackend(CeedQFunctionFieldGetEvalMode(qf_input_fields[i], &eval_mode_i));
1734*0183ed61SJeremy L Thompson     if (eval_mode_i == CEED_EVAL_WEIGHT) continue;
1735*0183ed61SJeremy L Thompson     CeedCallBackend(CeedOperatorFieldGetBasis(op_input_fields[i], &basis_i));
1736*0183ed61SJeremy L Thompson     for (CeedInt j = 0; (input_matrix_reuse[i].index == -1) && (j < i); j++) {
1737*0183ed61SJeremy L Thompson       CeedEvalMode eval_mode_j;
1738*0183ed61SJeremy L Thompson       CeedBasis    basis_j;
1739*0183ed61SJeremy L Thompson 
1740*0183ed61SJeremy L Thompson       CeedCallBackend(CeedQFunctionFieldGetEvalMode(qf_input_fields[j], &eval_mode_j));
1741*0183ed61SJeremy L Thompson       if (eval_mode_j == CEED_EVAL_WEIGHT) continue;
1742*0183ed61SJeremy L Thompson       CeedCallBackend(CeedOperatorFieldGetBasis(op_input_fields[j], &basis_j));
1743*0183ed61SJeremy L Thompson       if (basis_i == basis_j) {
1744*0183ed61SJeremy L Thompson         input_matrix_reuse[i].index     = j;
1745*0183ed61SJeremy L Thompson         input_matrix_reuse[i].is_input  = true;
1746*0183ed61SJeremy L Thompson         input_matrix_reuse[i].eval_mode = eval_mode_j;
1747*0183ed61SJeremy L Thompson       }
1748*0183ed61SJeremy L Thompson       CeedCallBackend(CeedBasisDestroy(&basis_j));
1749*0183ed61SJeremy L Thompson     }
1750*0183ed61SJeremy L Thompson     CeedCallBackend(CeedBasisDestroy(&basis_i));
1751*0183ed61SJeremy L Thompson   }
1752*0183ed61SJeremy L Thompson 
1753*0183ed61SJeremy L Thompson   // -- Determine output mat reuse
1754*0183ed61SJeremy L Thompson   FieldReuse_Cuda output_matrix_reuse[CEED_FIELD_MAX];
1755*0183ed61SJeremy L Thompson 
1756*0183ed61SJeremy L Thompson   for (CeedInt i = 0; i < num_output_fields; i++) {
1757*0183ed61SJeremy L Thompson     output_matrix_reuse[i].index = -1;
1758*0183ed61SJeremy L Thompson   }
1759*0183ed61SJeremy L Thompson   for (CeedInt i = 0; i < num_output_fields; i++) {
1760*0183ed61SJeremy L Thompson     CeedEvalMode eval_mode_i;
1761*0183ed61SJeremy L Thompson     CeedBasis    basis_i;
1762*0183ed61SJeremy L Thompson 
1763*0183ed61SJeremy L Thompson     CeedCallBackend(CeedQFunctionFieldGetEvalMode(qf_output_fields[i], &eval_mode_i));
1764*0183ed61SJeremy L Thompson     CeedCallBackend(CeedOperatorFieldGetBasis(op_output_fields[i], &basis_i));
1765*0183ed61SJeremy L Thompson     for (CeedInt j = 0; (output_matrix_reuse[i].index == -1) && (j < num_input_fields); j++) {
1766*0183ed61SJeremy L Thompson       CeedEvalMode eval_mode_j;
1767*0183ed61SJeremy L Thompson       CeedBasis    basis_j;
1768*0183ed61SJeremy L Thompson 
1769*0183ed61SJeremy L Thompson       CeedCallBackend(CeedQFunctionFieldGetEvalMode(qf_input_fields[j], &eval_mode_j));
1770*0183ed61SJeremy L Thompson       if (eval_mode_j == CEED_EVAL_WEIGHT) continue;
1771*0183ed61SJeremy L Thompson       CeedCallBackend(CeedOperatorFieldGetBasis(op_input_fields[j], &basis_j));
1772*0183ed61SJeremy L Thompson       if (basis_i == basis_j) {
1773*0183ed61SJeremy L Thompson         output_matrix_reuse[i].index     = j;
1774*0183ed61SJeremy L Thompson         output_matrix_reuse[i].is_input  = true;
1775*0183ed61SJeremy L Thompson         output_matrix_reuse[i].eval_mode = eval_mode_j;
1776*0183ed61SJeremy L Thompson       }
1777*0183ed61SJeremy L Thompson       CeedCallBackend(CeedBasisDestroy(&basis_j));
1778*0183ed61SJeremy L Thompson     }
1779*0183ed61SJeremy L Thompson     for (CeedInt j = 0; (output_matrix_reuse[i].index == -1) && (j < i); j++) {
1780*0183ed61SJeremy L Thompson       CeedEvalMode eval_mode_j;
1781*0183ed61SJeremy L Thompson       CeedBasis    basis_j;
1782*0183ed61SJeremy L Thompson 
1783*0183ed61SJeremy L Thompson       CeedCallBackend(CeedQFunctionFieldGetEvalMode(qf_output_fields[j], &eval_mode_j));
1784*0183ed61SJeremy L Thompson       if (eval_mode_j == CEED_EVAL_WEIGHT) continue;
1785*0183ed61SJeremy L Thompson       CeedCallBackend(CeedOperatorFieldGetBasis(op_output_fields[j], &basis_j));
1786*0183ed61SJeremy L Thompson       if (basis_i == basis_j) {
1787*0183ed61SJeremy L Thompson         output_matrix_reuse[i].index     = j;
1788*0183ed61SJeremy L Thompson         output_matrix_reuse[i].is_input  = false;
1789*0183ed61SJeremy L Thompson         output_matrix_reuse[i].eval_mode = eval_mode_j;
1790*0183ed61SJeremy L Thompson       }
1791*0183ed61SJeremy L Thompson       CeedCallBackend(CeedBasisDestroy(&basis_j));
1792*0183ed61SJeremy L Thompson     }
1793*0183ed61SJeremy L Thompson     CeedCallBackend(CeedBasisDestroy(&basis_i));
1794*0183ed61SJeremy L Thompson   }
1795*0183ed61SJeremy L Thompson 
1796*0183ed61SJeremy L Thompson   // Initialize constants, and matrices B and G
1797*0183ed61SJeremy L Thompson   code << "\n" << tab << "// Input field constants and basis data\n";
1798*0183ed61SJeremy L Thompson   for (CeedInt i = 0; i < num_input_fields; i++) {
1799*0183ed61SJeremy L Thompson     CeedCallBackend(CeedOperatorBuildKernelFieldData_Cuda_gen(code, data, tab, i, op_input_fields[i], qf_input_fields[i], input_matrix_reuse[i],
1800*0183ed61SJeremy L Thompson                                                               max_dim, Q, Q_1d, true, is_all_tensor, is_at_points, use_3d_slices));
1801*0183ed61SJeremy L Thompson   }
1802*0183ed61SJeremy L Thompson   code << "\n" << tab << "// Output field constants and basis data\n";
1803*0183ed61SJeremy L Thompson   for (CeedInt i = 0; i < num_output_fields; i++) {
1804*0183ed61SJeremy L Thompson     CeedCallBackend(CeedOperatorBuildKernelFieldData_Cuda_gen(code, data, tab, i, op_output_fields[i], qf_output_fields[i], output_matrix_reuse[i],
1805*0183ed61SJeremy L Thompson                                                               max_dim, Q, Q_1d, false, is_all_tensor, is_at_points, use_3d_slices));
1806*0183ed61SJeremy L Thompson   }
1807*0183ed61SJeremy L Thompson 
1808*0183ed61SJeremy L Thompson   // Loop over all elements
1809*0183ed61SJeremy L Thompson   code << "\n" << tab << "// Element loop\n";
1810*0183ed61SJeremy L Thompson   code << tab << "__syncthreads();\n";
1811*0183ed61SJeremy L Thompson   code << tab << "for (CeedInt elem = blockIdx.x*blockDim.z + threadIdx.z; elem < num_elem; elem += gridDim.x*blockDim.z) {\n";
1812*0183ed61SJeremy L Thompson   tab.push();
1813*0183ed61SJeremy L Thompson 
1814*0183ed61SJeremy L Thompson   // -- Compute minimum buffer space needed
1815*0183ed61SJeremy L Thompson   CeedInt max_rstr_buffer_size = 1;
1816*0183ed61SJeremy L Thompson 
1817*0183ed61SJeremy L Thompson   for (CeedInt i = 0; i < num_input_fields; i++) {
1818*0183ed61SJeremy L Thompson     CeedEvalMode eval_mode;
1819*0183ed61SJeremy L Thompson 
1820*0183ed61SJeremy L Thompson     CeedCallBackend(CeedQFunctionFieldGetEvalMode(qf_input_fields[i], &eval_mode));
1821*0183ed61SJeremy L Thompson     if (eval_mode != CEED_EVAL_NONE && eval_mode != CEED_EVAL_WEIGHT) {
1822*0183ed61SJeremy L Thompson       CeedInt             num_comp;
1823*0183ed61SJeremy L Thompson       CeedElemRestriction elem_rstr;
1824*0183ed61SJeremy L Thompson 
1825*0183ed61SJeremy L Thompson       CeedCallBackend(CeedOperatorFieldGetElemRestriction(op_input_fields[i], &elem_rstr));
1826*0183ed61SJeremy L Thompson       CeedCallBackend(CeedElemRestrictionGetNumComponents(elem_rstr, &num_comp));
1827*0183ed61SJeremy L Thompson       max_rstr_buffer_size = CeedIntMax(max_rstr_buffer_size, num_comp * (is_all_tensor && (max_dim >= 3) ? Q_1d : 1));
1828*0183ed61SJeremy L Thompson       CeedCallBackend(CeedElemRestrictionDestroy(&elem_rstr));
1829*0183ed61SJeremy L Thompson     }
1830*0183ed61SJeremy L Thompson   }
1831*0183ed61SJeremy L Thompson   for (CeedInt i = 0; i < num_output_fields; i++) {
1832*0183ed61SJeremy L Thompson     CeedEvalMode eval_mode;
1833*0183ed61SJeremy L Thompson 
1834*0183ed61SJeremy L Thompson     CeedCallBackend(CeedQFunctionFieldGetEvalMode(qf_output_fields[i], &eval_mode));
1835*0183ed61SJeremy L Thompson     if (eval_mode != CEED_EVAL_NONE) {
1836*0183ed61SJeremy L Thompson       CeedInt             num_comp;
1837*0183ed61SJeremy L Thompson       CeedElemRestriction elem_rstr;
1838*0183ed61SJeremy L Thompson 
1839*0183ed61SJeremy L Thompson       CeedCallBackend(CeedOperatorFieldGetElemRestriction(op_output_fields[i], &elem_rstr));
1840*0183ed61SJeremy L Thompson       CeedCallBackend(CeedElemRestrictionGetNumComponents(elem_rstr, &num_comp));
1841*0183ed61SJeremy L Thompson       max_rstr_buffer_size = CeedIntMax(max_rstr_buffer_size, num_comp * (is_all_tensor && (max_dim >= 3) ? Q_1d : 1));
1842*0183ed61SJeremy L Thompson       CeedCallBackend(CeedElemRestrictionDestroy(&elem_rstr));
1843*0183ed61SJeremy L Thompson     }
1844*0183ed61SJeremy L Thompson   }
1845*0183ed61SJeremy L Thompson   code << tab << "// Scratch restriction buffer space\n";
1846*0183ed61SJeremy L Thompson   code << tab << "CeedScalar r_e_scratch[" << max_rstr_buffer_size << "];\n";
1847*0183ed61SJeremy L Thompson 
1848*0183ed61SJeremy L Thompson   // -- Determine best input field processing order
1849*0183ed61SJeremy L Thompson   CeedInt field_rstr_in_buffer[CEED_FIELD_MAX], input_field_order[CEED_FIELD_MAX];
1850*0183ed61SJeremy L Thompson 
1851*0183ed61SJeremy L Thompson   for (CeedInt i = 0; i < num_input_fields; i++) {
1852*0183ed61SJeremy L Thompson     field_rstr_in_buffer[i] = -1;
1853*0183ed61SJeremy L Thompson     input_field_order[i]    = -1;
1854*0183ed61SJeremy L Thompson   }
1855*0183ed61SJeremy L Thompson   {
1856*0183ed61SJeremy L Thompson     bool    is_ordered[CEED_FIELD_MAX];
1857*0183ed61SJeremy L Thompson     CeedInt curr_index = 0;
1858*0183ed61SJeremy L Thompson 
1859*0183ed61SJeremy L Thompson     for (CeedInt i = 0; i < num_input_fields; i++) is_ordered[i] = false;
1860*0183ed61SJeremy L Thompson     for (CeedInt i = 0; i < num_input_fields; i++) {
1861*0183ed61SJeremy L Thompson       CeedVector          vec_i;
1862*0183ed61SJeremy L Thompson       CeedElemRestriction rstr_i;
1863*0183ed61SJeremy L Thompson 
1864*0183ed61SJeremy L Thompson       if (is_ordered[i]) continue;
1865*0183ed61SJeremy L Thompson       field_rstr_in_buffer[i]       = i;
1866*0183ed61SJeremy L Thompson       is_ordered[i]                 = true;
1867*0183ed61SJeremy L Thompson       input_field_order[curr_index] = i;
1868*0183ed61SJeremy L Thompson       curr_index++;
1869*0183ed61SJeremy L Thompson       CeedCallBackend(CeedOperatorFieldGetVector(op_input_fields[i], &vec_i));
1870*0183ed61SJeremy L Thompson       if (vec_i == CEED_VECTOR_NONE) continue;  // CEED_EVAL_WEIGHT
1871*0183ed61SJeremy L Thompson       CeedCallBackend(CeedOperatorFieldGetElemRestriction(op_input_fields[i], &rstr_i));
1872*0183ed61SJeremy L Thompson       for (CeedInt j = i + 1; j < num_input_fields; j++) {
1873*0183ed61SJeremy L Thompson         CeedVector          vec_j;
1874*0183ed61SJeremy L Thompson         CeedElemRestriction rstr_j;
1875*0183ed61SJeremy L Thompson 
1876*0183ed61SJeremy L Thompson         CeedCallBackend(CeedOperatorFieldGetVector(op_input_fields[j], &vec_j));
1877*0183ed61SJeremy L Thompson         CeedCallBackend(CeedOperatorFieldGetElemRestriction(op_input_fields[j], &rstr_j));
1878*0183ed61SJeremy L Thompson         if (rstr_i == rstr_j && vec_i == vec_j) {
1879*0183ed61SJeremy L Thompson           field_rstr_in_buffer[j]       = i;
1880*0183ed61SJeremy L Thompson           is_ordered[j]                 = true;
1881*0183ed61SJeremy L Thompson           input_field_order[curr_index] = j;
1882*0183ed61SJeremy L Thompson           curr_index++;
1883*0183ed61SJeremy L Thompson         }
1884*0183ed61SJeremy L Thompson         CeedCallBackend(CeedVectorDestroy(&vec_j));
1885*0183ed61SJeremy L Thompson         CeedCallBackend(CeedElemRestrictionDestroy(&rstr_j));
1886*0183ed61SJeremy L Thompson       }
1887*0183ed61SJeremy L Thompson       CeedCallBackend(CeedVectorDestroy(&vec_i));
1888*0183ed61SJeremy L Thompson       CeedCallBackend(CeedElemRestrictionDestroy(&rstr_i));
1889*0183ed61SJeremy L Thompson     }
1890*0183ed61SJeremy L Thompson   }
1891*0183ed61SJeremy L Thompson 
1892*0183ed61SJeremy L Thompson   // -- Input restriction and basis
1893*0183ed61SJeremy L Thompson   code << "\n" << tab << "// -- Input field restrictions and basis actions\n";
1894*0183ed61SJeremy L Thompson   CeedInt active_field_index = -1;
1895*0183ed61SJeremy L Thompson 
1896*0183ed61SJeremy L Thompson   for (CeedInt i = 0; i < num_input_fields; i++) {
1897*0183ed61SJeremy L Thompson     bool          is_active = false;
1898*0183ed61SJeremy L Thompson     const char   *field_name;
1899*0183ed61SJeremy L Thompson     const CeedInt f = input_field_order[i];
1900*0183ed61SJeremy L Thompson 
1901*0183ed61SJeremy L Thompson     {
1902*0183ed61SJeremy L Thompson       CeedVector vec;
1903*0183ed61SJeremy L Thompson 
1904*0183ed61SJeremy L Thompson       CeedCallBackend(CeedOperatorFieldGetVector(op_input_fields[f], &vec));
1905*0183ed61SJeremy L Thompson       is_active = vec == CEED_VECTOR_ACTIVE;
1906*0183ed61SJeremy L Thompson       CeedCallBackend(CeedVectorDestroy(&vec));
1907*0183ed61SJeremy L Thompson     }
1908*0183ed61SJeremy L Thompson 
1909*0183ed61SJeremy L Thompson     CeedCallBackend(CeedOperatorFieldGetName(op_input_fields[f], &field_name));
1910*0183ed61SJeremy L Thompson     code << tab << "// ---- Input field " << f << ": " << field_name << "\n";
1911*0183ed61SJeremy L Thompson 
1912*0183ed61SJeremy L Thompson     if (is_active) {
1913*0183ed61SJeremy L Thompson       std::string var_suffix = "_in_" + std::to_string(f);
1914*0183ed61SJeremy L Thompson 
1915*0183ed61SJeremy L Thompson       code << tab << "// Active field - no restriction or basis action here\n";
1916*0183ed61SJeremy L Thompson       if (active_field_index == -1) {
1917*0183ed61SJeremy L Thompson         active_field_index = f;
1918*0183ed61SJeremy L Thompson         code << tab << "CeedScalar r_e" << var_suffix << "[num_comp" << var_suffix << "*" << (max_dim >= 3 ? "P_1d" + var_suffix : "1")
1919*0183ed61SJeremy L Thompson              << "] = {0.0};\n";
1920*0183ed61SJeremy L Thompson       } else {
1921*0183ed61SJeremy L Thompson         code << tab << "CeedScalar *r_e" << var_suffix << " = r_e_in_" << active_field_index << ";\n";
1922*0183ed61SJeremy L Thompson       }
1923*0183ed61SJeremy L Thompson     } else {
1924*0183ed61SJeremy L Thompson       // ---- Restriction
1925*0183ed61SJeremy L Thompson       CeedCallBackend(CeedOperatorBuildKernelRestriction_Cuda_gen(code, data, tab, f, field_rstr_in_buffer, op_input_fields[f], qf_input_fields[f],
1926*0183ed61SJeremy L Thompson                                                                   max_dim, Q_1d, true, is_all_tensor, is_at_points, use_3d_slices));
1927*0183ed61SJeremy L Thompson 
1928*0183ed61SJeremy L Thompson       // ---- Basis action
1929*0183ed61SJeremy L Thompson       CeedCallBackend(CeedOperatorBuildKernelBasis_Cuda_gen(code, data, tab, f, op_input_fields[f], qf_input_fields[f], max_dim, Q_1d, true,
1930*0183ed61SJeremy L Thompson                                                             is_all_tensor, is_at_points, use_3d_slices));
1931*0183ed61SJeremy L Thompson     }
1932*0183ed61SJeremy L Thompson   }
1933*0183ed61SJeremy L Thompson 
1934*0183ed61SJeremy L Thompson   // -- Loop over active field
1935*0183ed61SJeremy L Thompson   std::string active_var_suffix = "_in_" + std::to_string(active_field_index);
1936*0183ed61SJeremy L Thompson 
1937*0183ed61SJeremy L Thompson   code << "\n" << tab << "// Loop over nodes in active field\n";
1938*0183ed61SJeremy L Thompson   code << tab << "for (CeedInt n = 0; n < num_comp" << active_var_suffix << "*P_1d" << active_var_suffix
1939*0183ed61SJeremy L Thompson        << (max_dim > 1 ? "*P_1d" + active_var_suffix : "") << (max_dim > 2 ? "*P_1d" + active_var_suffix : "") << "; n++) {\n";
1940*0183ed61SJeremy L Thompson   tab.push();
1941*0183ed61SJeremy L Thompson 
1942*0183ed61SJeremy L Thompson   // -- Set current active node and component to 1
1943*0183ed61SJeremy L Thompson   code << tab << "// Set current active node and component to 1.0\n";
1944*0183ed61SJeremy L Thompson   code << tab << "SetEVecStandard" << max_dim << "d_Single<num_comp" << active_var_suffix << ", P_1d" << active_var_suffix << ">(data, n, 1.0, r_e"
1945*0183ed61SJeremy L Thompson        << active_var_suffix << ");\n\n";
1946*0183ed61SJeremy L Thompson 
1947*0183ed61SJeremy L Thompson   for (CeedInt i = 0; i < num_input_fields; i++) {
1948*0183ed61SJeremy L Thompson     bool          is_active = false;
1949*0183ed61SJeremy L Thompson     const char   *field_name;
1950*0183ed61SJeremy L Thompson     const CeedInt f = input_field_order[i];
1951*0183ed61SJeremy L Thompson 
1952*0183ed61SJeremy L Thompson     {
1953*0183ed61SJeremy L Thompson       CeedVector vec;
1954*0183ed61SJeremy L Thompson 
1955*0183ed61SJeremy L Thompson       CeedCallBackend(CeedOperatorFieldGetVector(op_input_fields[f], &vec));
1956*0183ed61SJeremy L Thompson       is_active = vec == CEED_VECTOR_ACTIVE;
1957*0183ed61SJeremy L Thompson       CeedCallBackend(CeedVectorDestroy(&vec));
1958*0183ed61SJeremy L Thompson     }
1959*0183ed61SJeremy L Thompson     if (!is_active) continue;
1960*0183ed61SJeremy L Thompson 
1961*0183ed61SJeremy L Thompson     CeedCallBackend(CeedOperatorFieldGetName(op_input_fields[f], &field_name));
1962*0183ed61SJeremy L Thompson     code << tab << "// ---- Input field " << f << ": " << field_name << "\n";
1963*0183ed61SJeremy L Thompson 
1964*0183ed61SJeremy L Thompson     // ---- Basis action
1965*0183ed61SJeremy L Thompson     CeedCallBackend(CeedOperatorBuildKernelBasis_Cuda_gen(code, data, tab, f, op_input_fields[f], qf_input_fields[f], max_dim, Q_1d, true,
1966*0183ed61SJeremy L Thompson                                                           is_all_tensor, is_at_points, use_3d_slices));
1967*0183ed61SJeremy L Thompson   }
1968*0183ed61SJeremy L Thompson 
1969*0183ed61SJeremy L Thompson   // -- Q function
1970*0183ed61SJeremy L Thompson   CeedCallBackend(CeedOperatorBuildKernelQFunction_Cuda_gen(code, data, tab, max_dim, max_num_points, num_input_fields, op_input_fields,
1971*0183ed61SJeremy L Thompson                                                             qf_input_fields, num_output_fields, op_output_fields, qf_output_fields, qfunction_name,
1972*0183ed61SJeremy L Thompson                                                             Q_1d, is_all_tensor, is_at_points, use_3d_slices));
1973*0183ed61SJeremy L Thompson 
1974*0183ed61SJeremy L Thompson   // -- Output basis and restriction
1975*0183ed61SJeremy L Thompson   code << "\n" << tab << "// -- Output field basis action and restrictions\n";
1976*0183ed61SJeremy L Thompson   for (CeedInt i = 0; i < num_output_fields; i++) {
1977*0183ed61SJeremy L Thompson     bool        is_active = false;
1978*0183ed61SJeremy L Thompson     const char *field_name;
1979*0183ed61SJeremy L Thompson 
1980*0183ed61SJeremy L Thompson     {
1981*0183ed61SJeremy L Thompson       CeedVector vec;
1982*0183ed61SJeremy L Thompson 
1983*0183ed61SJeremy L Thompson       CeedCallBackend(CeedOperatorFieldGetVector(op_output_fields[i], &vec));
1984*0183ed61SJeremy L Thompson       is_active = vec == CEED_VECTOR_ACTIVE;
1985*0183ed61SJeremy L Thompson       CeedCallBackend(CeedVectorDestroy(&vec));
1986*0183ed61SJeremy L Thompson     }
1987*0183ed61SJeremy L Thompson     if (!is_active) continue;
1988*0183ed61SJeremy L Thompson 
1989*0183ed61SJeremy L Thompson     CeedCallBackend(CeedOperatorFieldGetName(op_output_fields[i], &field_name));
1990*0183ed61SJeremy L Thompson     code << tab << "// ---- Output field " << i << ": " << field_name << "\n";
1991*0183ed61SJeremy L Thompson 
1992*0183ed61SJeremy L Thompson     // ---- Basis action
1993*0183ed61SJeremy L Thompson     CeedCallBackend(CeedOperatorBuildKernelBasis_Cuda_gen(code, data, tab, i, op_output_fields[i], qf_output_fields[i], max_dim, Q_1d, false,
1994*0183ed61SJeremy L Thompson                                                           is_all_tensor, is_at_points, use_3d_slices));
1995*0183ed61SJeremy L Thompson 
1996*0183ed61SJeremy L Thompson     // ---- Restriction
1997*0183ed61SJeremy L Thompson     if (is_full) {
1998*0183ed61SJeremy L Thompson       // TODO: UPDATE OUTPUTS FOR FULL ASSEMBLY
1999*0183ed61SJeremy L Thompson     } else {
2000*0183ed61SJeremy L Thompson       std::string         var_suffix = "_out_" + std::to_string(i);
2001*0183ed61SJeremy L Thompson       CeedInt             comp_stride;
2002*0183ed61SJeremy L Thompson       CeedSize            l_size;
2003*0183ed61SJeremy L Thompson       CeedElemRestriction elem_rstr;
2004*0183ed61SJeremy L Thompson 
2005*0183ed61SJeremy L Thompson       CeedCallBackend(CeedOperatorFieldGetElemRestriction(op_output_fields[i], &elem_rstr));
2006*0183ed61SJeremy L Thompson       CeedCallBackend(CeedElemRestrictionGetLVectorSize(elem_rstr, &l_size));
2007*0183ed61SJeremy L Thompson       code << tab << "const CeedInt l_size" << var_suffix << " = " << l_size << ";\n";
2008*0183ed61SJeremy L Thompson       CeedCallBackend(CeedElemRestrictionGetCompStride(elem_rstr, &comp_stride));
2009*0183ed61SJeremy L Thompson       code << tab << "const CeedInt comp_stride" << var_suffix << " = " << comp_stride << ";\n";
2010*0183ed61SJeremy L Thompson       code << tab << "WriteLVecStandard" << max_dim << "d_Single<num_comp" << var_suffix << ", comp_stride" << var_suffix << ", P_1d" + var_suffix
2011*0183ed61SJeremy L Thompson            << ">(data, l_size" << var_suffix << ", elem, n, indices.outputs[" << i << "], r_e" << var_suffix << ", values_array);\n";
2012*0183ed61SJeremy L Thompson       CeedCallBackend(CeedElemRestrictionDestroy(&elem_rstr));
2013*0183ed61SJeremy L Thompson     }
2014*0183ed61SJeremy L Thompson   }
2015*0183ed61SJeremy L Thompson 
2016*0183ed61SJeremy L Thompson   // -- Reset current active node and component
2017*0183ed61SJeremy L Thompson   code << "\n" << tab << "// Reset current active node and component to 0.0\n";
2018*0183ed61SJeremy L Thompson   code << tab << "SetEVecStandard" << max_dim << "d_Single<num_comp" << active_var_suffix << ", P_1d" << active_var_suffix << ">(data, n, 0.0, r_e"
2019*0183ed61SJeremy L Thompson        << active_var_suffix << ");\n";
2020*0183ed61SJeremy L Thompson 
2021*0183ed61SJeremy L Thompson   // -- End of loop over active field
2022*0183ed61SJeremy L Thompson   tab.pop();
2023*0183ed61SJeremy L Thompson   code << tab << "}\n";
2024*0183ed61SJeremy L Thompson 
2025*0183ed61SJeremy L Thompson   // Close loop and function
2026*0183ed61SJeremy L Thompson   tab.pop();
2027*0183ed61SJeremy L Thompson   code << tab << "}\n";
2028*0183ed61SJeremy L Thompson   tab.pop();
2029*0183ed61SJeremy L Thompson   code << tab << "}\n";
2030*0183ed61SJeremy L Thompson   code << tab << "// -----------------------------------------------------------------------------\n\n";
2031*0183ed61SJeremy L Thompson 
2032*0183ed61SJeremy L Thompson   // Compile
2033*0183ed61SJeremy L Thompson   {
2034*0183ed61SJeremy L Thompson     bool          is_compile_good = false;
2035*0183ed61SJeremy L Thompson     const CeedInt T_1d            = CeedIntMax(is_all_tensor ? Q_1d : Q, data->max_P_1d);
2036*0183ed61SJeremy L Thompson 
2037*0183ed61SJeremy L Thompson     data->thread_1d = T_1d;
2038*0183ed61SJeremy L Thompson     CeedCallBackend(CeedTryCompile_Cuda(ceed, code.str().c_str(), &is_compile_good,
2039*0183ed61SJeremy L Thompson                                         is_full ? &data->module_assemble_full : &data->module_assemble_diagonal, 1, "OP_T_1D", T_1d));
2040*0183ed61SJeremy L Thompson     if (is_compile_good) {
2041*0183ed61SJeremy L Thompson       *is_good_build = true;
2042*0183ed61SJeremy L Thompson       CeedCallBackend(CeedGetKernel_Cuda(ceed, is_full ? data->module_assemble_full : data->module_assemble_diagonal, operator_name.c_str(),
2043*0183ed61SJeremy L Thompson                                          is_full ? &data->assemble_full : &data->assemble_diagonal));
2044*0183ed61SJeremy L Thompson     } else {
2045*0183ed61SJeremy L Thompson       *is_good_build              = false;
2046*0183ed61SJeremy L Thompson       data->use_assembly_fallback = true;
2047*0183ed61SJeremy L Thompson     }
2048*0183ed61SJeremy L Thompson   }
2049*0183ed61SJeremy L Thompson   CeedCallBackend(CeedDestroy(&ceed));
2050*0183ed61SJeremy L Thompson   CeedCallBackend(CeedQFunctionDestroy(&qf));
2051*0183ed61SJeremy L Thompson   return CEED_ERROR_SUCCESS;
2052*0183ed61SJeremy L Thompson }
2053*0183ed61SJeremy L Thompson 
2054*0183ed61SJeremy L Thompson extern "C" int CeedOperatorBuildKernelDiagonalAssemblyAtPoints_Cuda_gen(CeedOperator op, bool *is_good_build) {
2055*0183ed61SJeremy L Thompson   return CeedOperatorBuildKernelAssemblyAtPoints_Cuda_gen(op, false, is_good_build);
2056*0183ed61SJeremy L Thompson }
2057*0183ed61SJeremy L Thompson 
2058*0183ed61SJeremy L Thompson //------------------------------------------------------------------------------
2059