xref: /libCEED/backends/cuda-gen/ceed-cuda-gen-operator-build.cpp (revision efa41df35aba6fbf99ee55a3580c03081f41c595)
15aed82e4SJeremy L Thompson // Copyright (c) 2017-2024, Lawrence Livermore National Security, LLC and other CEED contributors.
23d8e8822SJeremy L Thompson // All Rights Reserved. See the top-level LICENSE and NOTICE files for details.
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>
129e201c85SYohann #include <ceed/jit-tools.h>
133d576824SJeremy L Thompson #include <cuda_runtime.h>
142b730f8bSJeremy L Thompson 
15241a4b83SYohann #include <iostream>
16241a4b83SYohann #include <sstream>
17b2165e7aSSebastian Grimberg #include <string>
182b730f8bSJeremy L Thompson 
190d0321e0SJeremy L Thompson #include "../cuda-ref/ceed-cuda-ref.h"
20241a4b83SYohann #include "../cuda-shared/ceed-cuda-shared.h"
2149aac155SJeremy L Thompson #include "../cuda/ceed-cuda-common.h"
222b730f8bSJeremy L Thompson #include "../cuda/ceed-cuda-compile.h"
232b730f8bSJeremy L Thompson #include "ceed-cuda-gen.h"
24241a4b83SYohann 
2545a787f7SJeremy L Thompson struct FieldReuse_Cuda {
2645a787f7SJeremy L Thompson   CeedInt      index;
2745a787f7SJeremy L Thompson   bool         is_input;
2845a787f7SJeremy L Thompson   CeedEvalMode eval_mode;
2945a787f7SJeremy L Thompson };
3045a787f7SJeremy L Thompson 
31ab213215SJeremy L Thompson //------------------------------------------------------------------------------
324b3e95d5SJeremy L Thompson // Determine type of operator
334b3e95d5SJeremy L Thompson //------------------------------------------------------------------------------
344b3e95d5SJeremy L Thompson static int CeedOperatorBuildKernelData_Cuda_gen(Ceed ceed, CeedInt num_input_fields, CeedOperatorField *op_input_fields,
354b3e95d5SJeremy L Thompson                                                 CeedQFunctionField *qf_input_fields, CeedInt num_output_fields, CeedOperatorField *op_output_fields,
36412e5683SJeremy L Thompson                                                 CeedQFunctionField *qf_output_fields, CeedInt *max_P, CeedInt *max_P_1d, CeedInt *Q, CeedInt *Q_1d,
37412e5683SJeremy L Thompson                                                 CeedInt *max_dim, bool *is_all_tensor, bool *use_3d_slices) {
38412e5683SJeremy L Thompson   // Check if all are tensor
39412e5683SJeremy L Thompson   *is_all_tensor = true;
404b3e95d5SJeremy L Thompson   for (CeedInt i = 0; i < num_input_fields; i++) {
414b3e95d5SJeremy L Thompson     CeedBasis basis;
424b3e95d5SJeremy L Thompson 
434b3e95d5SJeremy L Thompson     CeedCallBackend(CeedOperatorFieldGetBasis(op_input_fields[i], &basis));
444b3e95d5SJeremy L Thompson     if (basis != CEED_BASIS_NONE) {
454b3e95d5SJeremy L Thompson       bool is_field_tensor;
464b3e95d5SJeremy L Thompson 
474b3e95d5SJeremy L Thompson       CeedCallBackend(CeedBasisIsTensor(basis, &is_field_tensor));
48412e5683SJeremy L Thompson       *is_all_tensor = *is_all_tensor && is_field_tensor;
494b3e95d5SJeremy L Thompson     }
50681d0ea7SJeremy L Thompson     CeedCallBackend(CeedBasisDestroy(&basis));
514b3e95d5SJeremy L Thompson   }
524b3e95d5SJeremy L Thompson   for (CeedInt i = 0; i < num_output_fields; i++) {
534b3e95d5SJeremy L Thompson     CeedBasis basis;
544b3e95d5SJeremy L Thompson 
554b3e95d5SJeremy L Thompson     CeedCallBackend(CeedOperatorFieldGetBasis(op_output_fields[i], &basis));
564b3e95d5SJeremy L Thompson     if (basis != CEED_BASIS_NONE) {
574b3e95d5SJeremy L Thompson       bool is_field_tensor;
584b3e95d5SJeremy L Thompson 
594b3e95d5SJeremy L Thompson       CeedCallBackend(CeedBasisIsTensor(basis, &is_field_tensor));
60412e5683SJeremy L Thompson       *is_all_tensor = *is_all_tensor && is_field_tensor;
61412e5683SJeremy L Thompson     }
62412e5683SJeremy L Thompson     CeedCallBackend(CeedBasisDestroy(&basis));
63412e5683SJeremy L Thompson   }
64412e5683SJeremy L Thompson 
65412e5683SJeremy L Thompson   // Find max_P, max_P_1d, Q, and Q_1d
66412e5683SJeremy L Thompson   bool is_all_3d = true;
67412e5683SJeremy L Thompson 
68412e5683SJeremy L Thompson   *max_P    = 0;
69412e5683SJeremy L Thompson   *max_P_1d = 0;
70412e5683SJeremy L Thompson   *Q        = 0;
71412e5683SJeremy L Thompson   *Q_1d     = 0;
72412e5683SJeremy L Thompson   for (CeedInt i = 0; i < num_input_fields; i++) {
73412e5683SJeremy L Thompson     CeedBasis basis;
74412e5683SJeremy L Thompson 
75412e5683SJeremy L Thompson     CeedCallBackend(CeedOperatorFieldGetBasis(op_input_fields[i], &basis));
76412e5683SJeremy L Thompson     if (basis != CEED_BASIS_NONE) {
77412e5683SJeremy L Thompson       bool    is_field_tensor;
78412e5683SJeremy L Thompson       CeedInt field_dim = 0, field_P = 0, field_P_1d = 0, field_Q = 0, field_Q_1d = 0;
79412e5683SJeremy L Thompson 
80412e5683SJeremy L Thompson       // Check if 3D
814b3e95d5SJeremy L Thompson       CeedCallBackend(CeedBasisGetDimension(basis, &field_dim));
82412e5683SJeremy L Thompson       is_all_3d = is_all_3d && (field_dim == 3);
83412e5683SJeremy L Thompson       *max_dim  = CeedIntMax(*max_dim, field_dim);
84412e5683SJeremy L Thompson 
85412e5683SJeremy L Thompson       // Collect P, P_1d, Q, and Q_1d
86412e5683SJeremy L Thompson       CeedCallBackend(CeedBasisGetNumNodes(basis, &field_P));
87412e5683SJeremy L Thompson       *max_P = CeedIntMax(*max_P, field_P);
88412e5683SJeremy L Thompson       CeedCallBackend(CeedBasisIsTensor(basis, &is_field_tensor));
89412e5683SJeremy L Thompson       if (is_field_tensor) {
90412e5683SJeremy L Thompson         CeedCallBackend(CeedBasisGetNumNodes1D(basis, &field_P_1d));
91412e5683SJeremy L Thompson         *max_P_1d = CeedIntMax(*max_P_1d, field_P_1d);
92412e5683SJeremy L Thompson       }
93412e5683SJeremy L Thompson       CeedCallBackend(CeedBasisGetNumQuadraturePoints(basis, &field_Q));
94412e5683SJeremy L Thompson       CeedCheck(*Q == 0 || field_Q == *Q, ceed, CEED_ERROR_BACKEND, "Quadrature spaces must be compatible");
95412e5683SJeremy L Thompson       *Q = field_Q;
96412e5683SJeremy L Thompson       if (is_field_tensor) {
97412e5683SJeremy L Thompson         CeedCallBackend(CeedBasisGetNumQuadraturePoints1D(basis, &field_Q_1d));
984b3e95d5SJeremy L Thompson         CeedCheck(*Q_1d == 0 || field_Q_1d == *Q_1d, ceed, CEED_ERROR_BACKEND, "Quadrature spaces must be compatible");
994b3e95d5SJeremy L Thompson         *Q_1d = field_Q_1d;
1004b3e95d5SJeremy L Thompson       }
101412e5683SJeremy L Thompson     }
102412e5683SJeremy L Thompson     CeedCallBackend(CeedBasisDestroy(&basis));
103412e5683SJeremy L Thompson   }
104412e5683SJeremy L Thompson   for (CeedInt i = 0; i < num_output_fields; i++) {
105412e5683SJeremy L Thompson     CeedBasis basis;
106412e5683SJeremy L Thompson 
107412e5683SJeremy L Thompson     CeedCallBackend(CeedOperatorFieldGetBasis(op_output_fields[i], &basis));
108412e5683SJeremy L Thompson     if (basis != CEED_BASIS_NONE) {
109412e5683SJeremy L Thompson       bool    is_field_tensor;
110412e5683SJeremy L Thompson       CeedInt field_dim = 0, field_P = 0, field_P_1d = 0, field_Q = 0, field_Q_1d = 0;
111412e5683SJeremy L Thompson 
112412e5683SJeremy L Thompson       // Check if 3D
113412e5683SJeremy L Thompson       CeedCallBackend(CeedBasisGetDimension(basis, &field_dim));
114412e5683SJeremy L Thompson       is_all_3d = is_all_3d && (field_dim == 3);
115412e5683SJeremy L Thompson       *max_dim  = CeedIntMax(*max_dim, field_dim);
116412e5683SJeremy L Thompson 
117412e5683SJeremy L Thompson       // Collect P, P_1d, Q, and Q_1d
118412e5683SJeremy L Thompson       CeedCallBackend(CeedBasisGetNumNodes(basis, &field_P));
119412e5683SJeremy L Thompson       *max_P = CeedIntMax(*max_P, field_P);
120412e5683SJeremy L Thompson       CeedCallBackend(CeedBasisIsTensor(basis, &is_field_tensor));
121412e5683SJeremy L Thompson       if (is_field_tensor) {
122412e5683SJeremy L Thompson         CeedCallBackend(CeedBasisGetNumNodes1D(basis, &field_P_1d));
123412e5683SJeremy L Thompson         *max_P_1d = CeedIntMax(*max_P_1d, field_P_1d);
124412e5683SJeremy L Thompson       }
125412e5683SJeremy L Thompson       CeedCallBackend(CeedBasisGetNumQuadraturePoints(basis, &field_Q));
126412e5683SJeremy L Thompson       CeedCheck(*Q == 0 || field_Q == *Q, ceed, CEED_ERROR_BACKEND, "Quadrature spaces must be compatible");
127412e5683SJeremy L Thompson       *Q = field_Q;
128412e5683SJeremy L Thompson       if (is_field_tensor) {
129412e5683SJeremy L Thompson         CeedCallBackend(CeedBasisGetNumQuadraturePoints1D(basis, &field_Q_1d));
130412e5683SJeremy L Thompson         CeedCheck(*Q_1d == 0 || field_Q_1d == *Q_1d, ceed, CEED_ERROR_BACKEND, "Quadrature spaces must be compatible");
131412e5683SJeremy L Thompson         *Q_1d = field_Q_1d;
132412e5683SJeremy L Thompson       }
133412e5683SJeremy L Thompson     }
134681d0ea7SJeremy L Thompson     CeedCallBackend(CeedBasisDestroy(&basis));
1354b3e95d5SJeremy L Thompson   }
1364b3e95d5SJeremy L Thompson 
1374b3e95d5SJeremy L Thompson   // Only use 3D collocated gradient parallelization strategy when gradient is computed
1384b3e95d5SJeremy L Thompson   *use_3d_slices = false;
139412e5683SJeremy L Thompson   if (is_all_3d && *is_all_tensor) {
1404b3e95d5SJeremy L Thompson     bool was_grad_found = false;
1414b3e95d5SJeremy L Thompson 
1424b3e95d5SJeremy L Thompson     for (CeedInt i = 0; i < num_input_fields; i++) {
1434b3e95d5SJeremy L Thompson       CeedEvalMode eval_mode;
1444b3e95d5SJeremy L Thompson 
1454b3e95d5SJeremy L Thompson       CeedCallBackend(CeedQFunctionFieldGetEvalMode(qf_input_fields[i], &eval_mode));
1464b3e95d5SJeremy L Thompson       if (eval_mode == CEED_EVAL_GRAD) {
1474b3e95d5SJeremy L Thompson         CeedBasis_Cuda_shared *basis_data;
1484b3e95d5SJeremy L Thompson         CeedBasis              basis;
1494b3e95d5SJeremy L Thompson 
1504b3e95d5SJeremy L Thompson         CeedCallBackend(CeedOperatorFieldGetBasis(op_input_fields[i], &basis));
1514b3e95d5SJeremy L Thompson         CeedCallBackend(CeedBasisGetData(basis, &basis_data));
1524b3e95d5SJeremy L Thompson         *use_3d_slices = basis_data->d_collo_grad_1d && (was_grad_found ? *use_3d_slices : true);
1534b3e95d5SJeremy L Thompson         was_grad_found = true;
154681d0ea7SJeremy L Thompson         CeedCallBackend(CeedBasisDestroy(&basis));
1554b3e95d5SJeremy L Thompson       }
1564b3e95d5SJeremy L Thompson     }
1574b3e95d5SJeremy L Thompson     for (CeedInt i = 0; i < num_output_fields; i++) {
1584b3e95d5SJeremy L Thompson       CeedEvalMode eval_mode;
1594b3e95d5SJeremy L Thompson 
1604b3e95d5SJeremy L Thompson       CeedCallBackend(CeedQFunctionFieldGetEvalMode(qf_output_fields[i], &eval_mode));
1614b3e95d5SJeremy L Thompson       if (eval_mode == CEED_EVAL_GRAD) {
1624b3e95d5SJeremy L Thompson         CeedBasis_Cuda_shared *basis_data;
1634b3e95d5SJeremy L Thompson         CeedBasis              basis;
1644b3e95d5SJeremy L Thompson 
1654b3e95d5SJeremy L Thompson         CeedCallBackend(CeedOperatorFieldGetBasis(op_output_fields[i], &basis));
1664b3e95d5SJeremy L Thompson         CeedCallBackend(CeedBasisGetData(basis, &basis_data));
1674b3e95d5SJeremy L Thompson         *use_3d_slices = basis_data->d_collo_grad_1d && (was_grad_found ? *use_3d_slices : true);
1684b3e95d5SJeremy L Thompson         was_grad_found = true;
169681d0ea7SJeremy L Thompson         CeedCallBackend(CeedBasisDestroy(&basis));
1704b3e95d5SJeremy L Thompson       }
1714b3e95d5SJeremy L Thompson     }
1724b3e95d5SJeremy L Thompson   }
1734b3e95d5SJeremy L Thompson   return CEED_ERROR_SUCCESS;
1744b3e95d5SJeremy L Thompson }
1754b3e95d5SJeremy L Thompson 
1764b3e95d5SJeremy L Thompson //------------------------------------------------------------------------------
1774b3e95d5SJeremy L Thompson // Setup fields
1784b3e95d5SJeremy L Thompson //------------------------------------------------------------------------------
1794b3e95d5SJeremy L Thompson static int CeedOperatorBuildKernelFieldData_Cuda_gen(std::ostringstream &code, CeedOperator_Cuda_gen *data, CeedInt i, CeedOperatorField op_field,
1808014c5e7SJeremy L Thompson                                                      CeedQFunctionField qf_field, FieldReuse_Cuda field_reuse, CeedInt max_dim, CeedInt Q,
1818014c5e7SJeremy L Thompson                                                      CeedInt Q_1d, bool is_input, bool is_all_tensor, bool is_at_points, bool use_3d_slices) {
182412e5683SJeremy L Thompson   bool      is_tensor = true;
183412e5683SJeremy L Thompson   CeedBasis basis;
184412e5683SJeremy L Thompson   CeedCallBackend(CeedOperatorFieldGetBasis(op_field, &basis));
185412e5683SJeremy L Thompson   if (basis != CEED_BASIS_NONE) CeedCallBackend(CeedBasisIsTensor(basis, &is_tensor));
186412e5683SJeremy L Thompson 
18759fa3f92SJeremy L Thompson   const char            *field_name;
1884b3e95d5SJeremy L Thompson   std::string            var_suffix = (is_input ? "_in_" : "_out_") + std::to_string(i);
189dc007f05SJeremy L Thompson   std::string            P_name = (is_tensor ? "P_1d" : "P") + var_suffix, Q_name = is_tensor ? "Q_1d" : "Q";
1904b3e95d5SJeremy L Thompson   std::string            option_name = (is_input ? "inputs" : "outputs");
1914b3e95d5SJeremy L Thompson   CeedEvalMode           eval_mode   = CEED_EVAL_NONE;
1928014c5e7SJeremy L Thompson   CeedInt                elem_size = 0, num_comp = 0, dim = max_dim, P_1d = 0;
1934b3e95d5SJeremy L Thompson   CeedElemRestriction    elem_rstr;
1944b3e95d5SJeremy L Thompson   CeedBasis_Cuda_shared *basis_data;
1954b3e95d5SJeremy L Thompson 
1960a2a6492SJeremy L Thompson   // Field reuse info
19745a787f7SJeremy L Thompson   bool use_previous_field = field_reuse.index != -1;
1980a2a6492SJeremy L Thompson 
19959fa3f92SJeremy L Thompson   CeedCallBackend(CeedOperatorFieldGetName(op_field, &field_name));
20059fa3f92SJeremy L Thompson   code << "  // -- " << (is_input ? "Input" : "Output") << " field " << i << ": " << field_name << "\n";
2014b3e95d5SJeremy L Thompson 
2024b3e95d5SJeremy L Thompson   // Get field data
2034b3e95d5SJeremy L Thompson   CeedCallBackend(CeedOperatorFieldGetElemRestriction(op_field, &elem_rstr));
2044b3e95d5SJeremy L Thompson   if (elem_rstr != CEED_ELEMRESTRICTION_NONE) {
2054b3e95d5SJeremy L Thompson     CeedCallBackend(CeedElemRestrictionGetElementSize(elem_rstr, &elem_size));
2064b3e95d5SJeremy L Thompson     CeedCallBackend(CeedElemRestrictionGetNumComponents(elem_rstr, &num_comp));
2074b3e95d5SJeremy L Thompson   }
208681d0ea7SJeremy L Thompson   CeedCallBackend(CeedElemRestrictionDestroy(&elem_rstr));
2094b3e95d5SJeremy L Thompson   if (basis != CEED_BASIS_NONE) {
2104b3e95d5SJeremy L Thompson     CeedCallBackend(CeedBasisGetData(basis, &basis_data));
211412e5683SJeremy L Thompson     CeedCallBackend(CeedBasisGetDimension(basis, &dim));
212dc007f05SJeremy L Thompson     if (is_tensor) CeedCallBackend(CeedBasisGetNumNodes1D(basis, &P_1d));
213dc007f05SJeremy L Thompson     else CeedCallBackend(CeedBasisGetNumNodes(basis, &P_1d));
2144b3e95d5SJeremy L Thompson   }
2154b3e95d5SJeremy L Thompson   CeedCallBackend(CeedQFunctionFieldGetEvalMode(qf_field, &eval_mode));
2164b3e95d5SJeremy L Thompson 
2174b3e95d5SJeremy L Thompson   // Set field constants
218412e5683SJeremy L Thompson   code << "  const CeedInt dim" << var_suffix << " = " << dim << ";\n";
219412e5683SJeremy L Thompson   if (is_tensor && !is_all_tensor) {
220412e5683SJeremy L Thompson     CeedInt P = 0;
221412e5683SJeremy L Thompson 
222412e5683SJeremy L Thompson     CeedCallBackend(CeedBasisGetNumNodes(basis, &P));
223c433aabcSJeremy L Thompson     code << "  const CeedInt P" << var_suffix << " = " << (basis == CEED_BASIS_NONE ? Q : P) << ";\n";
224412e5683SJeremy L Thompson   }
2254b3e95d5SJeremy L Thompson   code << "  const CeedInt " << P_name << " = " << (basis == CEED_BASIS_NONE ? Q_1d : P_1d) << ";\n";
226343e3094SJeremy L Thompson   if (eval_mode != CEED_EVAL_WEIGHT) {
2274b3e95d5SJeremy L Thompson     code << "  const CeedInt num_comp" << var_suffix << " = " << num_comp << ";\n";
2284b3e95d5SJeremy L Thompson   }
2294b3e95d5SJeremy L Thompson 
2304b3e95d5SJeremy L Thompson   // Load basis data
2314b3e95d5SJeremy L Thompson   code << "  // EvalMode: " << CeedEvalModes[eval_mode] << "\n";
2324b3e95d5SJeremy L Thompson   switch (eval_mode) {
2334b3e95d5SJeremy L Thompson     case CEED_EVAL_NONE:
2344b3e95d5SJeremy L Thompson       break;
2354b3e95d5SJeremy L Thompson     case CEED_EVAL_INTERP:
2368b97b69aSJeremy L Thompson       if (is_at_points) {
2378b97b69aSJeremy L Thompson         // AtPoints
2388b97b69aSJeremy L Thompson         if (!basis_data->d_chebyshev_interp_1d) {
2398b97b69aSJeremy L Thompson           CeedSize    interp_bytes;
2408b97b69aSJeremy L Thompson           CeedScalar *chebyshev_interp_1d;
2418b97b69aSJeremy L Thompson 
2428b97b69aSJeremy L Thompson           interp_bytes = P_1d * Q_1d * sizeof(CeedScalar);
2438b97b69aSJeremy L Thompson           CeedCallBackend(CeedCalloc(P_1d * Q_1d, &chebyshev_interp_1d));
2448b97b69aSJeremy L Thompson           CeedCallBackend(CeedBasisGetChebyshevInterp1D(basis, chebyshev_interp_1d));
2458b97b69aSJeremy L Thompson           CeedCallCuda(CeedBasisReturnCeed(basis), cudaMalloc((void **)&basis_data->d_chebyshev_interp_1d, interp_bytes));
2468b97b69aSJeremy L Thompson           CeedCallCuda(CeedBasisReturnCeed(basis),
2478b97b69aSJeremy L Thompson                        cudaMemcpy(basis_data->d_chebyshev_interp_1d, chebyshev_interp_1d, interp_bytes, cudaMemcpyHostToDevice));
2488b97b69aSJeremy L Thompson           CeedCallBackend(CeedFree(&chebyshev_interp_1d));
2498b97b69aSJeremy L Thompson         }
2508b97b69aSJeremy L Thompson         if (is_input) data->B.inputs[i] = basis_data->d_chebyshev_interp_1d;
2518b97b69aSJeremy L Thompson         else data->B.outputs[i] = basis_data->d_chebyshev_interp_1d;
2528b97b69aSJeremy L Thompson       } else {
2538b97b69aSJeremy L Thompson         // Standard quadrature
2544b3e95d5SJeremy L Thompson         if (is_input) data->B.inputs[i] = basis_data->d_interp_1d;
2554b3e95d5SJeremy L Thompson         else data->B.outputs[i] = basis_data->d_interp_1d;
2568b97b69aSJeremy L Thompson       }
2570a2a6492SJeremy L Thompson       if (use_previous_field) {
25845a787f7SJeremy L Thompson         std::string reuse_var = "s_B" + ((field_reuse.is_input ? "_in_" : "_out_") + std::to_string(field_reuse.index));
2590a2a6492SJeremy L Thompson 
2600a2a6492SJeremy L Thompson         code << "  CeedScalar *s_B" << var_suffix << " = " << reuse_var << ";\n";
2610a2a6492SJeremy L Thompson       } else {
262dc007f05SJeremy L Thompson         code << "  __shared__ CeedScalar s_B" << var_suffix << "[" << P_name << "*" << Q_name << "];\n";
263f815fac9SJeremy L Thompson         code << "  LoadMatrix<" << P_name << ", " << Q_name << ">(data, B." << option_name << "[" << i << "], s_B" << var_suffix << ");\n";
2640a2a6492SJeremy L Thompson       }
2654b3e95d5SJeremy L Thompson       break;
2664b3e95d5SJeremy L Thompson     case CEED_EVAL_GRAD:
2678b97b69aSJeremy L Thompson       if (is_at_points) {
2688b97b69aSJeremy L Thompson         // AtPoints
2698b97b69aSJeremy L Thompson         if (!basis_data->d_chebyshev_interp_1d) {
2708b97b69aSJeremy L Thompson           CeedSize    interp_bytes;
2718b97b69aSJeremy L Thompson           CeedScalar *chebyshev_interp_1d;
2728b97b69aSJeremy L Thompson 
2738b97b69aSJeremy L Thompson           interp_bytes = P_1d * Q_1d * sizeof(CeedScalar);
2748b97b69aSJeremy L Thompson           CeedCallBackend(CeedCalloc(P_1d * Q_1d, &chebyshev_interp_1d));
2758b97b69aSJeremy L Thompson           CeedCallBackend(CeedBasisGetChebyshevInterp1D(basis, chebyshev_interp_1d));
2768b97b69aSJeremy L Thompson           CeedCallCuda(CeedBasisReturnCeed(basis), cudaMalloc((void **)&basis_data->d_chebyshev_interp_1d, interp_bytes));
2778b97b69aSJeremy L Thompson           CeedCallCuda(CeedBasisReturnCeed(basis),
2788b97b69aSJeremy L Thompson                        cudaMemcpy(basis_data->d_chebyshev_interp_1d, chebyshev_interp_1d, interp_bytes, cudaMemcpyHostToDevice));
2798b97b69aSJeremy L Thompson           CeedCallBackend(CeedFree(&chebyshev_interp_1d));
2808b97b69aSJeremy L Thompson         }
2818b97b69aSJeremy L Thompson         if (is_input) data->B.inputs[i] = basis_data->d_chebyshev_interp_1d;
2828b97b69aSJeremy L Thompson         else data->B.outputs[i] = basis_data->d_chebyshev_interp_1d;
2838b97b69aSJeremy L Thompson       } else {
2848b97b69aSJeremy L Thompson         // Standard quadrature
2854b3e95d5SJeremy L Thompson         if (is_input) data->B.inputs[i] = basis_data->d_interp_1d;
2864b3e95d5SJeremy L Thompson         else data->B.outputs[i] = basis_data->d_interp_1d;
2878b97b69aSJeremy L Thompson       }
288dc007f05SJeremy L Thompson       if (is_tensor) {
2890a2a6492SJeremy L Thompson         if (use_previous_field) {
29045a787f7SJeremy L Thompson           std::string reuse_var = "s_B" + ((field_reuse.is_input ? "_in_" : "_out_") + std::to_string(field_reuse.index));
2910a2a6492SJeremy L Thompson 
2920a2a6492SJeremy L Thompson           code << "  CeedScalar *s_B" << var_suffix << " = " << reuse_var << ";\n";
2930a2a6492SJeremy L Thompson         } else {
294dc007f05SJeremy L Thompson           code << "  __shared__ CeedScalar s_B" << var_suffix << "[" << P_name << "*" << Q_name << "];\n";
295f815fac9SJeremy L Thompson           code << "  LoadMatrix<" << P_name << ", " << Q_name << ">(data, B." << option_name << "[" << i << "], s_B" << var_suffix << ");\n";
296dc007f05SJeremy L Thompson         }
2970a2a6492SJeremy L Thompson       }
2988b97b69aSJeremy L Thompson       if (is_at_points) break;  // No G mat for AtPoints
2994b3e95d5SJeremy L Thompson       if (use_3d_slices) {
3004b3e95d5SJeremy L Thompson         if (is_input) data->G.inputs[i] = basis_data->d_collo_grad_1d;
3014b3e95d5SJeremy L Thompson         else data->G.outputs[i] = basis_data->d_collo_grad_1d;
30245a787f7SJeremy L Thompson         if (use_previous_field && field_reuse.eval_mode == CEED_EVAL_GRAD) {
30345a787f7SJeremy L Thompson           std::string reuse_var = "s_G" + ((field_reuse.is_input ? "_in_" : "_out_") + std::to_string(field_reuse.index));
3040a2a6492SJeremy L Thompson 
3050a2a6492SJeremy L Thompson           code << "  CeedScalar *s_G" << var_suffix << " = " << reuse_var << ";\n";
3060a2a6492SJeremy L Thompson         } else {
307dc007f05SJeremy L Thompson           code << "  __shared__ CeedScalar s_G" << var_suffix << "[" << Q_name << "*" << Q_name << "];\n";
308f815fac9SJeremy L Thompson           code << "  LoadMatrix<" << Q_name << ", " << Q_name << ">(data, G." << option_name << "[" << i << "], s_G" << var_suffix << ");\n";
3090a2a6492SJeremy L Thompson         }
3104b3e95d5SJeremy L Thompson       } else {
3114b3e95d5SJeremy L Thompson         bool has_collo_grad = basis_data->d_collo_grad_1d;
3124b3e95d5SJeremy L Thompson 
3134b3e95d5SJeremy L Thompson         if (is_input) data->G.inputs[i] = has_collo_grad ? basis_data->d_collo_grad_1d : basis_data->d_grad_1d;
3144b3e95d5SJeremy L Thompson         else data->G.outputs[i] = has_collo_grad ? basis_data->d_collo_grad_1d : basis_data->d_grad_1d;
3154b3e95d5SJeremy L Thompson         if (has_collo_grad) {
31645a787f7SJeremy L Thompson           if (use_previous_field && field_reuse.eval_mode == CEED_EVAL_GRAD) {
31745a787f7SJeremy L Thompson             std::string reuse_var = "s_G" + ((field_reuse.is_input ? "_in_" : "_out_") + std::to_string(field_reuse.index));
3180a2a6492SJeremy L Thompson 
3190a2a6492SJeremy L Thompson             code << "  CeedScalar *s_G" << var_suffix << " = " << reuse_var << ";\n";
3200a2a6492SJeremy L Thompson           } else {
321dc007f05SJeremy L Thompson             code << "  __shared__ CeedScalar s_G" << var_suffix << "[" << Q_name << "*" << Q_name << "];\n";
322f815fac9SJeremy L Thompson             code << "  LoadMatrix<" << Q_name << ", " << Q_name << ">(data, G." << option_name << "[" << i << "], s_G" << var_suffix << ");\n";
3230a2a6492SJeremy L Thompson           }
3240a2a6492SJeremy L Thompson         } else {
32545a787f7SJeremy L Thompson           if (use_previous_field && field_reuse.eval_mode == CEED_EVAL_GRAD) {
32645a787f7SJeremy L Thompson             std::string reuse_var = "s_G" + ((field_reuse.is_input ? "_in_" : "_out_") + std::to_string(field_reuse.index));
3270a2a6492SJeremy L Thompson 
3280a2a6492SJeremy L Thompson             code << "  CeedScalar *s_G" << var_suffix << " = " << reuse_var << ";\n";
3294b3e95d5SJeremy L Thompson           } else {
330412e5683SJeremy L Thompson             code << "  __shared__ CeedScalar s_G" << var_suffix << "[" << P_name << "*" << Q_name << (is_tensor ? "" : "*dim")
331412e5683SJeremy L Thompson                  << (is_tensor ? "" : var_suffix) << "];\n";
332412e5683SJeremy L Thompson             code << "  LoadMatrix<" << P_name << ", " << Q_name << (is_tensor ? "" : "*dim") << (is_tensor ? "" : var_suffix) << ">(data, G."
333412e5683SJeremy L Thompson                  << option_name << "[" << i << "], s_G" << var_suffix << ");\n";
3344b3e95d5SJeremy L Thompson           }
3354b3e95d5SJeremy L Thompson         }
3360a2a6492SJeremy L Thompson       }
3374b3e95d5SJeremy L Thompson       break;
3384b3e95d5SJeremy L Thompson     case CEED_EVAL_WEIGHT:
3394b3e95d5SJeremy L Thompson       break;  // No action
3404b3e95d5SJeremy L Thompson       // LCOV_EXCL_START
3414b3e95d5SJeremy L Thompson     case CEED_EVAL_DIV:
3424b3e95d5SJeremy L Thompson     case CEED_EVAL_CURL:
3434b3e95d5SJeremy L Thompson       break;  // TODO: Not implemented
3444b3e95d5SJeremy L Thompson               // LCOV_EXCL_STOP
3454b3e95d5SJeremy L Thompson   }
3468b97b69aSJeremy L Thompson   CeedCallBackend(CeedBasisDestroy(&basis));
3474b3e95d5SJeremy L Thompson   return CEED_ERROR_SUCCESS;
3484b3e95d5SJeremy L Thompson }
3494b3e95d5SJeremy L Thompson 
3504b3e95d5SJeremy L Thompson //------------------------------------------------------------------------------
3514b3e95d5SJeremy L Thompson // Restriction
3524b3e95d5SJeremy L Thompson //------------------------------------------------------------------------------
3538014c5e7SJeremy L Thompson static int CeedOperatorBuildKernelRestriction_Cuda_gen(std::ostringstream &code, CeedOperator_Cuda_gen *data, CeedInt i, CeedInt field_input_buffer[],
3548014c5e7SJeremy L Thompson                                                        CeedOperatorField op_field, CeedQFunctionField qf_field, CeedInt max_dim, CeedInt Q_1d,
3558014c5e7SJeremy L Thompson                                                        bool is_input, bool is_all_tensor, bool is_at_points, bool use_3d_slices) {
3564b3e95d5SJeremy L Thompson   std::string               var_suffix = (is_input ? "_in_" : "_out_") + std::to_string(i);
357412e5683SJeremy L Thompson   std::string               P_name     = (is_all_tensor ? "P_1d" : "P") + var_suffix;
3584b3e95d5SJeremy L Thompson   CeedEvalMode              eval_mode  = CEED_EVAL_NONE;
359412e5683SJeremy L Thompson   CeedInt                   elem_size = 0, num_comp = 0;
3604b3e95d5SJeremy L Thompson   CeedSize                  l_size;
361f815fac9SJeremy L Thompson   CeedRestrictionType       rstr_type = CEED_RESTRICTION_STANDARD;
3624b3e95d5SJeremy L Thompson   CeedElemRestriction_Cuda *rstr_data;
3634b3e95d5SJeremy L Thompson   CeedElemRestriction       elem_rstr;
3644b3e95d5SJeremy L Thompson 
3654b3e95d5SJeremy L Thompson   // Get field data
3664b3e95d5SJeremy L Thompson   CeedCallBackend(CeedOperatorFieldGetElemRestriction(op_field, &elem_rstr));
3674b3e95d5SJeremy L Thompson   if (elem_rstr != CEED_ELEMRESTRICTION_NONE) {
368f815fac9SJeremy L Thompson     CeedCallBackend(CeedElemRestrictionGetType(elem_rstr, &rstr_type));
3694b3e95d5SJeremy L Thompson     CeedCallBackend(CeedElemRestrictionGetElementSize(elem_rstr, &elem_size));
3704b3e95d5SJeremy L Thompson     CeedCallBackend(CeedElemRestrictionGetNumComponents(elem_rstr, &num_comp));
3714b3e95d5SJeremy L Thompson     CeedCallBackend(CeedElemRestrictionGetData(elem_rstr, &rstr_data));
3724b3e95d5SJeremy L Thompson   }
3734b3e95d5SJeremy L Thompson   CeedCallBackend(CeedQFunctionFieldGetEvalMode(qf_field, &eval_mode));
3744b3e95d5SJeremy L Thompson 
3754b3e95d5SJeremy L Thompson   // Restriction
3764b3e95d5SJeremy L Thompson   if (is_input) {
3774b3e95d5SJeremy L Thompson     // Input
378e93651e5SJeremy L Thompson     if (field_input_buffer[i] != i) {
379e93651e5SJeremy L Thompson       std::string buffer_name = "r_e_in_" + std::to_string(field_input_buffer[i]);
380e93651e5SJeremy L Thompson 
381e93651e5SJeremy L Thompson       // Restriction was already done for previous input
382e93651e5SJeremy L Thompson       code << "    CeedScalar *r_e" << var_suffix << " = " << buffer_name << ";\n";
3838b97b69aSJeremy L Thompson     } else if (eval_mode != CEED_EVAL_WEIGHT && !((eval_mode == CEED_EVAL_NONE) && use_3d_slices && is_at_points)) {
3848b97b69aSJeremy L Thompson       if (eval_mode == CEED_EVAL_NONE && rstr_type != CEED_RESTRICTION_POINTS) {
385e93651e5SJeremy L Thompson         // No basis action, so r_e_in_* in also r_q_in_* and needs to be allocated
3864b3e95d5SJeremy L Thompson         code << "    CeedScalar r_e" << var_suffix << "[num_comp" << var_suffix << "*" << P_name << "];\n";
3878b97b69aSJeremy L Thompson       } else if (rstr_type != CEED_RESTRICTION_POINTS) {
388e93651e5SJeremy L Thompson         // Otherwise we're using the scratch space
389e93651e5SJeremy L Thompson         code << "    CeedScalar *r_e" << var_suffix << " = r_e_scratch;\n";
390e93651e5SJeremy L Thompson       }
391f815fac9SJeremy L Thompson       switch (rstr_type) {
392f815fac9SJeremy L Thompson         case CEED_RESTRICTION_STANDARD: {
3934b3e95d5SJeremy L Thompson           CeedInt comp_stride;
3944b3e95d5SJeremy L Thompson 
3954b3e95d5SJeremy L Thompson           CeedCallBackend(CeedElemRestrictionGetLVectorSize(elem_rstr, &l_size));
3964b3e95d5SJeremy L Thompson           code << "    const CeedInt l_size" << var_suffix << " = " << l_size << ";\n";
3974b3e95d5SJeremy L Thompson           CeedCallBackend(CeedElemRestrictionGetCompStride(elem_rstr, &comp_stride));
3984b3e95d5SJeremy L Thompson           code << "    // CompStride: " << comp_stride << "\n";
3994b3e95d5SJeremy L Thompson           data->indices.inputs[i] = (CeedInt *)rstr_data->d_offsets;
400412e5683SJeremy L Thompson           code << "    ReadLVecStandard" << (is_all_tensor ? max_dim : 1) << "d<num_comp" << var_suffix << ", " << comp_stride << ", " << P_name
401dc007f05SJeremy L Thompson                << ">(data, l_size" << var_suffix << ", elem, indices.inputs[" << i << "], d" << var_suffix << ", r_e" << var_suffix << ");\n";
402f815fac9SJeremy L Thompson           break;
403f815fac9SJeremy L Thompson         }
404f815fac9SJeremy L Thompson         case CEED_RESTRICTION_STRIDED: {
4054b3e95d5SJeremy L Thompson           bool    has_backend_strides;
4064b3e95d5SJeremy L Thompson           CeedInt num_elem;
4074b3e95d5SJeremy L Thompson 
4084b3e95d5SJeremy L Thompson           CeedCallBackend(CeedElemRestrictionHasBackendStrides(elem_rstr, &has_backend_strides));
4094b3e95d5SJeremy L Thompson           CeedCallBackend(CeedElemRestrictionGetNumElements(elem_rstr, &num_elem));
4104b3e95d5SJeremy L Thompson           CeedInt strides[3] = {1, elem_size * num_elem, elem_size};
4114b3e95d5SJeremy L Thompson 
4124b3e95d5SJeremy L Thompson           if (!has_backend_strides) {
4134b3e95d5SJeremy L Thompson             CeedCallBackend(CeedElemRestrictionGetStrides(elem_rstr, strides));
4144b3e95d5SJeremy L Thompson           }
4154b3e95d5SJeremy L Thompson           code << "    // Strides: {" << strides[0] << ", " << strides[1] << ", " << strides[2] << "}\n";
416412e5683SJeremy L Thompson           code << "    ReadLVecStrided" << (is_all_tensor ? max_dim : 1) << "d<num_comp" << var_suffix << ", " << P_name << ", " << strides[0] << ", "
417dc007f05SJeremy L Thompson                << strides[1] << ", " << strides[2] << ">(data, elem, d" << var_suffix << ", r_e" << var_suffix << ");\n";
418f815fac9SJeremy L Thompson           break;
419f815fac9SJeremy L Thompson         }
4208b97b69aSJeremy L Thompson         case CEED_RESTRICTION_POINTS: {
4218b97b69aSJeremy L Thompson           CeedInt comp_stride;
4228b97b69aSJeremy L Thompson 
4238b97b69aSJeremy L Thompson           CeedCallBackend(CeedElemRestrictionGetCompStride(elem_rstr, &comp_stride));
4248b97b69aSJeremy L Thompson           code << "    const CeedInt comp_stride" << var_suffix << " = " << comp_stride << ";\n";
4258b97b69aSJeremy L Thompson           data->indices.inputs[i] = (CeedInt *)rstr_data->d_offsets;
4268b97b69aSJeremy L Thompson           break;
4278b97b69aSJeremy L Thompson         }
428f815fac9SJeremy L Thompson         // LCOV_EXCL_START
429f815fac9SJeremy L Thompson         case CEED_RESTRICTION_ORIENTED:
430f815fac9SJeremy L Thompson         case CEED_RESTRICTION_CURL_ORIENTED:
431f815fac9SJeremy L Thompson           break;  // TODO: Not implemented
432f815fac9SJeremy L Thompson                   // LCOV_EXCL_STOP
4334b3e95d5SJeremy L Thompson       }
4344b3e95d5SJeremy L Thompson     }
4354b3e95d5SJeremy L Thompson   } else {
4364b3e95d5SJeremy L Thompson     // Output
437f815fac9SJeremy L Thompson     switch (rstr_type) {
438f815fac9SJeremy L Thompson       case CEED_RESTRICTION_STANDARD: {
4394b3e95d5SJeremy L Thompson         CeedInt comp_stride;
4404b3e95d5SJeremy L Thompson 
4414b3e95d5SJeremy L Thompson         CeedCallBackend(CeedElemRestrictionGetLVectorSize(elem_rstr, &l_size));
4424b3e95d5SJeremy L Thompson         code << "    const CeedInt l_size" << var_suffix << " = " << l_size << ";\n";
4434b3e95d5SJeremy L Thompson         CeedCallBackend(CeedElemRestrictionGetCompStride(elem_rstr, &comp_stride));
4444b3e95d5SJeremy L Thompson         code << "    // CompStride: " << comp_stride << "\n";
4454b3e95d5SJeremy L Thompson         data->indices.outputs[i] = (CeedInt *)rstr_data->d_offsets;
446412e5683SJeremy L Thompson         code << "    WriteLVecStandard" << (is_all_tensor ? max_dim : 1) << "d<num_comp" << var_suffix << ", " << comp_stride << ", " << P_name
447dc007f05SJeremy L Thompson              << ">(data, l_size" << var_suffix << ", elem, indices.outputs[" << i << "], r_e" << var_suffix << ", d" << var_suffix << ");\n";
448f815fac9SJeremy L Thompson         break;
449f815fac9SJeremy L Thompson       }
450f815fac9SJeremy L Thompson       case CEED_RESTRICTION_STRIDED: {
4514b3e95d5SJeremy L Thompson         bool    has_backend_strides;
4524b3e95d5SJeremy L Thompson         CeedInt num_elem;
4534b3e95d5SJeremy L Thompson 
4544b3e95d5SJeremy L Thompson         CeedCallBackend(CeedElemRestrictionHasBackendStrides(elem_rstr, &has_backend_strides));
4554b3e95d5SJeremy L Thompson         CeedCallBackend(CeedElemRestrictionGetNumElements(elem_rstr, &num_elem));
4564b3e95d5SJeremy L Thompson         CeedInt strides[3] = {1, elem_size * num_elem, elem_size};
4574b3e95d5SJeremy L Thompson 
4584b3e95d5SJeremy L Thompson         if (!has_backend_strides) {
4594b3e95d5SJeremy L Thompson           CeedCallBackend(CeedElemRestrictionGetStrides(elem_rstr, strides));
4604b3e95d5SJeremy L Thompson         }
4614b3e95d5SJeremy L Thompson         code << "    // Strides: {" << strides[0] << ", " << strides[1] << ", " << strides[2] << "}\n";
462412e5683SJeremy L Thompson         code << "    WriteLVecStrided" << (is_all_tensor ? max_dim : 1) << "d<num_comp" << var_suffix << ", " << P_name << ", " << strides[0] << ", "
463dc007f05SJeremy L Thompson              << strides[1] << ", " << strides[2] << ">(data, elem, r_e" << var_suffix << ", d" << var_suffix << ");\n";
464f815fac9SJeremy L Thompson         break;
465f815fac9SJeremy L Thompson       }
4668b97b69aSJeremy L Thompson       case CEED_RESTRICTION_POINTS:
4678b97b69aSJeremy L Thompson         data->indices.outputs[i] = (CeedInt *)rstr_data->d_offsets;
4688b97b69aSJeremy L Thompson         break;
469f815fac9SJeremy L Thompson       // LCOV_EXCL_START
470f815fac9SJeremy L Thompson       case CEED_RESTRICTION_ORIENTED:
471f815fac9SJeremy L Thompson       case CEED_RESTRICTION_CURL_ORIENTED:
472f815fac9SJeremy L Thompson         break;  // TODO: Not implemented
473f815fac9SJeremy L Thompson                 // LCOV_EXCL_STOP
4744b3e95d5SJeremy L Thompson     }
4754b3e95d5SJeremy L Thompson   }
476681d0ea7SJeremy L Thompson   CeedCallBackend(CeedElemRestrictionDestroy(&elem_rstr));
4774b3e95d5SJeremy L Thompson   return CEED_ERROR_SUCCESS;
4784b3e95d5SJeremy L Thompson }
4794b3e95d5SJeremy L Thompson 
4804b3e95d5SJeremy L Thompson //------------------------------------------------------------------------------
4814b3e95d5SJeremy L Thompson // Basis
4824b3e95d5SJeremy L Thompson //------------------------------------------------------------------------------
4838014c5e7SJeremy L Thompson static int CeedOperatorBuildKernelBasis_Cuda_gen(std::ostringstream &code, CeedOperator_Cuda_gen *data, CeedInt i, CeedOperatorField op_field,
4848014c5e7SJeremy L Thompson                                                  CeedQFunctionField qf_field, CeedInt max_dim, CeedInt Q_1d, bool is_input, bool is_all_tensor,
4858014c5e7SJeremy L Thompson                                                  bool is_at_points, bool use_3d_slices) {
486412e5683SJeremy L Thompson   bool      is_tensor = true;
487412e5683SJeremy L Thompson   CeedBasis basis;
488412e5683SJeremy L Thompson   CeedCallBackend(CeedOperatorFieldGetBasis(op_field, &basis));
489412e5683SJeremy L Thompson   CeedCallBackend(CeedBasisIsTensor(basis, &is_tensor));
490412e5683SJeremy L Thompson 
4914b3e95d5SJeremy L Thompson   std::string         var_suffix = (is_input ? "_in_" : "_out_") + std::to_string(i);
492dc007f05SJeremy L Thompson   std::string         P_name = (is_tensor ? "P_1d" : "P") + var_suffix, Q_name = is_tensor ? "Q_1d" : "Q";
4934b3e95d5SJeremy L Thompson   CeedEvalMode        eval_mode = CEED_EVAL_NONE;
494412e5683SJeremy L Thompson   CeedInt             dim = max_dim, elem_size = 0, num_comp = 0, P_1d = 0;
4954b3e95d5SJeremy L Thompson   CeedElemRestriction elem_rstr;
4964b3e95d5SJeremy L Thompson 
4974b3e95d5SJeremy L Thompson   // Get field data
4984b3e95d5SJeremy L Thompson   CeedCallBackend(CeedOperatorFieldGetElemRestriction(op_field, &elem_rstr));
4994b3e95d5SJeremy L Thompson   if (elem_rstr != CEED_ELEMRESTRICTION_NONE) {
5004b3e95d5SJeremy L Thompson     CeedCallBackend(CeedElemRestrictionGetElementSize(elem_rstr, &elem_size));
5014b3e95d5SJeremy L Thompson     CeedCallBackend(CeedElemRestrictionGetNumComponents(elem_rstr, &num_comp));
5024b3e95d5SJeremy L Thompson   }
503681d0ea7SJeremy L Thompson   CeedCallBackend(CeedElemRestrictionDestroy(&elem_rstr));
5044b3e95d5SJeremy L Thompson   if (basis != CEED_BASIS_NONE) {
505412e5683SJeremy L Thompson     CeedCallBackend(CeedBasisGetDimension(basis, &dim));
506dc007f05SJeremy L Thompson     if (is_tensor) CeedCallBackend(CeedBasisGetNumNodes1D(basis, &P_1d));
507dc007f05SJeremy L Thompson     else CeedCallBackend(CeedBasisGetNumNodes(basis, &P_1d));
5084b3e95d5SJeremy L Thompson   }
5094b3e95d5SJeremy L Thompson   CeedCallBackend(CeedQFunctionFieldGetEvalMode(qf_field, &eval_mode));
5104b3e95d5SJeremy L Thompson 
5114b3e95d5SJeremy L Thompson   // Basis
5124b3e95d5SJeremy L Thompson   code << "    // EvalMode: " << CeedEvalModes[eval_mode] << "\n";
5134b3e95d5SJeremy L Thompson   if (is_input) {
5144b3e95d5SJeremy L Thompson     switch (eval_mode) {
5154b3e95d5SJeremy L Thompson       case CEED_EVAL_NONE:
5168b97b69aSJeremy L Thompson         if (!use_3d_slices && !is_at_points) {
5174b3e95d5SJeremy L Thompson           code << "    CeedScalar *r_q" << var_suffix << " = r_e" << var_suffix << ";\n";
5184b3e95d5SJeremy L Thompson         }
5194b3e95d5SJeremy L Thompson         break;
5204b3e95d5SJeremy L Thompson       case CEED_EVAL_INTERP:
5218b97b69aSJeremy L Thompson         if (is_at_points) {
522dc007f05SJeremy L Thompson           std::string function_name = (dim == 1 ? "Interp" : "InterpTensor") + std::to_string(dim) + "d";
523dc007f05SJeremy L Thompson 
524dc007f05SJeremy L Thompson           code << "    CeedScalar r_c" << var_suffix << "[num_comp" << var_suffix << "*" << (dim >= 3 ? Q_name : "1") << "];\n";
52599421279SJeremy L Thompson           code << "    " << function_name << "<num_comp" << var_suffix << ", " << P_name << ", " << Q_name << ", OP_T_1D>(data, r_e" << var_suffix
52699421279SJeremy L Thompson                << ", s_B" << var_suffix << ", r_c" << var_suffix << ");\n";
5278b97b69aSJeremy L Thompson         } else {
528412e5683SJeremy L Thompson           std::string function_name = is_tensor
529412e5683SJeremy L Thompson                                           ? ((dim == 1 ? "Interp" : "InterpTensor") + std::to_string(dim) + "d" + (is_all_tensor ? "" : "Flattened"))
530412e5683SJeremy L Thompson                                           : "InterpNonTensor";
531c433aabcSJeremy L Thompson           std::string op_t_1d_name  = (is_all_tensor || !is_tensor) ? "OP_T_1D" : (P_1d > Q_1d ? P_name : Q_name);
532dc007f05SJeremy L Thompson 
533259057edSJeremy L Thompson           code << "    CeedScalar r_q" << var_suffix << "[num_comp" << var_suffix << "*" << (is_all_tensor && (dim >= 3) ? Q_name : "1") << "];\n";
534c433aabcSJeremy L Thompson           code << "    " << function_name << "<num_comp" << var_suffix << ", " << P_name << ", " << Q_name << ", " << op_t_1d_name << ">(data, r_e"
535c433aabcSJeremy L Thompson                << var_suffix << ", s_B" << var_suffix << ", r_q" << var_suffix << ");\n";
5368b97b69aSJeremy L Thompson         }
5374b3e95d5SJeremy L Thompson         break;
5384b3e95d5SJeremy L Thompson       case CEED_EVAL_GRAD:
5398b97b69aSJeremy L Thompson         if (is_at_points) {
540dc007f05SJeremy L Thompson           std::string function_name = (dim == 1 ? "Interp" : "InterpTensor") + std::to_string(dim) + "d";
541dc007f05SJeremy L Thompson 
542dc007f05SJeremy L Thompson           code << "    CeedScalar r_c" << var_suffix << "[num_comp" << var_suffix << "*" << (dim >= 3 ? Q_name : "1") << "];\n";
54399421279SJeremy L Thompson           code << "    " << function_name << "<num_comp" << var_suffix << ", " << P_name << ", " << Q_name << ", OP_T_1D>(data, r_e" << var_suffix
54499421279SJeremy L Thompson                << ", s_B" << var_suffix << ", r_c" << var_suffix << ");\n";
5458b97b69aSJeremy L Thompson         } else if (use_3d_slices) {
546dc007f05SJeremy L Thompson           std::string function_name = (dim > 1 ? "InterpTensor" : "Interp") + std::to_string(dim) + "d";
547dc007f05SJeremy L Thompson 
5484b3e95d5SJeremy L Thompson           code << "    CeedScalar r_q" << var_suffix << "[num_comp" << var_suffix << "*" << Q_name << "];\n";
54999421279SJeremy L Thompson           code << "    " << function_name << "<num_comp" << var_suffix << ", " << P_name << ", " << Q_name << ", OP_T_1D>(data, r_e" << var_suffix
55099421279SJeremy L Thompson                << ", s_B" << var_suffix << ", r_q" << var_suffix << ");\n";
551dc007f05SJeremy L Thompson         } else if (is_tensor) {
552dc007f05SJeremy L Thompson           bool        is_collocated = dim == 3 && Q_1d >= P_1d;
553412e5683SJeremy L Thompson           std::string function_name = (dim == 1 ? "Grad" : (is_collocated ? "GradTensorCollocated" : "GradTensor")) + std::to_string(dim) + "d" +
554412e5683SJeremy L Thompson                                       (is_all_tensor ? "" : "Flattened");
555c433aabcSJeremy L Thompson           std::string op_t_1d_name = is_all_tensor ? "OP_T_1D" : (P_1d > Q_1d ? P_name : Q_name);
556dc007f05SJeremy L Thompson 
557259057edSJeremy L Thompson           code << "    CeedScalar r_q" << var_suffix << "[num_comp" << var_suffix << "*dim" << var_suffix << "*"
558259057edSJeremy L Thompson                << (is_all_tensor && dim >= 3 ? Q_name : "1") << "];\n";
559c433aabcSJeremy L Thompson           code << "    " << function_name << "<num_comp" << var_suffix << ", " << P_name << ", " << Q_name << ", " << op_t_1d_name << ">(data, r_e"
560c433aabcSJeremy L Thompson                << var_suffix << ", s_B" << var_suffix << ", s_G" << var_suffix << ", r_q" << var_suffix << ");\n";
5614b3e95d5SJeremy L Thompson         } else {
562dc007f05SJeremy L Thompson           std::string function_name = "GradNonTensor";
563dc007f05SJeremy L Thompson 
564412e5683SJeremy L Thompson           code << "    CeedScalar r_q" << var_suffix << "[num_comp" << var_suffix << "*dim" << var_suffix << "];\n";
565c433aabcSJeremy L Thompson           code << "    " << function_name << "<num_comp" << var_suffix << ", dim" << var_suffix << ", " << P_name << ", " << Q_name
566c433aabcSJeremy L Thompson                << ", OP_T_1D>(data, r_e" << var_suffix << ", s_G" << var_suffix << ", r_q" << var_suffix << ");\n";
5674b3e95d5SJeremy L Thompson         }
5684b3e95d5SJeremy L Thompson         break;
5694b3e95d5SJeremy L Thompson       case CEED_EVAL_WEIGHT: {
5708b97b69aSJeremy L Thompson         if (is_at_points) {
5718b97b69aSJeremy L Thompson           code << "    // Nothing to do AtPoints\n";
5728b97b69aSJeremy L Thompson         } else {
5734b3e95d5SJeremy L Thompson           CeedBasis_Cuda_shared *basis_data;
574412e5683SJeremy L Thompson           std::string            function_name = is_tensor
575412e5683SJeremy L Thompson                                                      ? ((dim == 1 ? "Weight" : "WeightTensor") + std::to_string(dim) + "d" + (is_all_tensor ? "" : "Flattened"))
576412e5683SJeremy L Thompson                                                      : "WeightNonTensor";
5774b3e95d5SJeremy L Thompson 
578259057edSJeremy L Thompson           code << "    CeedScalar r_q" << var_suffix << "[" << (is_all_tensor && (dim >= 3) ? Q_name : "1") << "];\n";
5794b3e95d5SJeremy L Thompson           CeedCallBackend(CeedBasisGetData(basis, &basis_data));
5804b3e95d5SJeremy L Thompson           data->W = basis_data->d_q_weight_1d;
581343e3094SJeremy L Thompson           code << "    " << function_name << "<" << P_name << ", " << Q_name << ">(data, W, r_q" << var_suffix << ");\n";
5828b97b69aSJeremy L Thompson         }
5834b3e95d5SJeremy L Thompson         break;
5844b3e95d5SJeremy L Thompson       }
5854b3e95d5SJeremy L Thompson       // LCOV_EXCL_START
5864b3e95d5SJeremy L Thompson       case CEED_EVAL_DIV:
5874b3e95d5SJeremy L Thompson       case CEED_EVAL_CURL:
5884b3e95d5SJeremy L Thompson         break;  // TODO: Not implemented
5894b3e95d5SJeremy L Thompson                 // LCOV_EXCL_STOP
5904b3e95d5SJeremy L Thompson     }
5914b3e95d5SJeremy L Thompson   } else {
5924b3e95d5SJeremy L Thompson     switch (eval_mode) {
5934b3e95d5SJeremy L Thompson       case CEED_EVAL_NONE:
5944b3e95d5SJeremy L Thompson         code << "    CeedScalar *r_e" << var_suffix << " = r_q" << var_suffix << ";\n";
5954b3e95d5SJeremy L Thompson         break;  // No action
5964b3e95d5SJeremy L Thompson       case CEED_EVAL_INTERP:
597e93651e5SJeremy L Thompson         code << "    CeedScalar *r_e" << var_suffix << " = r_e_scratch;\n";
5988b97b69aSJeremy L Thompson         if (is_at_points) {
599dc007f05SJeremy L Thompson           std::string function_name = (dim == 1 ? "InterpTranspose" : "InterpTransposeTensor") + std::to_string(dim) + "d";
600dc007f05SJeremy L Thompson 
60199421279SJeremy L Thompson           code << "    " << function_name << "<num_comp" << var_suffix << ", " << P_name << ", " << Q_name << ", OP_T_1D>(data, r_c" << var_suffix
60299421279SJeremy L Thompson                << ", s_B" << var_suffix << ", r_e" << var_suffix << ");\n";
6038b97b69aSJeremy L Thompson         } else {
604dc007f05SJeremy L Thompson           std::string function_name =
605412e5683SJeremy L Thompson               is_tensor ? ((dim == 1 ? "InterpTranspose" : "InterpTransposeTensor") + std::to_string(dim) + "d" + (is_all_tensor ? "" : "Flattened"))
606412e5683SJeremy L Thompson                         : "InterpTransposeNonTensor";
607c433aabcSJeremy L Thompson           std::string op_t_1d_name = (is_all_tensor || !is_tensor) ? "OP_T_1D" : (P_1d > Q_1d ? P_name : Q_name);
608dc007f05SJeremy L Thompson 
609c433aabcSJeremy L Thompson           code << "    " << function_name << "<num_comp" << var_suffix << ", " << P_name << ", " << Q_name << ", " << op_t_1d_name << ">(data, r_q"
610c433aabcSJeremy L Thompson                << var_suffix << ", s_B" << var_suffix << ", r_e" << var_suffix << ");\n";
6118b97b69aSJeremy L Thompson         }
6124b3e95d5SJeremy L Thompson         break;
6134b3e95d5SJeremy L Thompson       case CEED_EVAL_GRAD:
614e93651e5SJeremy L Thompson         code << "    CeedScalar *r_e" << var_suffix << " = r_e_scratch;\n";
6158b97b69aSJeremy L Thompson         if (is_at_points) {
616dc007f05SJeremy L Thompson           std::string function_name = (dim == 1 ? "InterpTranspose" : "InterpTransposeTensor") + std::to_string(dim) + "d";
617dc007f05SJeremy L Thompson 
61899421279SJeremy L Thompson           code << "    " << function_name << "<num_comp" << var_suffix << ", " << P_name << ", " << Q_name << ", OP_T_1D>(data, r_c" << var_suffix
61999421279SJeremy L Thompson                << ", s_B" << var_suffix << ", r_e" << var_suffix << ");\n";
6208b97b69aSJeremy L Thompson         } else if (use_3d_slices) {
621dc007f05SJeremy L Thompson           std::string function_name = (dim == 1 ? "InterpTranspose" : "InterpTransposeTensor") + std::to_string(dim) + "d";
622dc007f05SJeremy L Thompson 
62399421279SJeremy L Thompson           code << "    " << function_name << "<num_comp" << var_suffix << ", " << P_name << ", " << Q_name << ", OP_T_1D>(data, r_q" << var_suffix
62499421279SJeremy L Thompson                << ", s_B" << var_suffix << ", r_e" << var_suffix << ");\n";
625dc007f05SJeremy L Thompson         } else if (is_tensor) {
626dc007f05SJeremy L Thompson           bool        is_collocated = dim == 3 && Q_1d >= P_1d;
627412e5683SJeremy L Thompson           std::string function_name = (dim == 1 ? "GradTranspose" : (is_collocated ? "GradTransposeTensorCollocated" : "GradTransposeTensor")) +
628412e5683SJeremy L Thompson                                       std::to_string(dim) + "d" + (is_all_tensor ? "" : "Flattened");
629c433aabcSJeremy L Thompson           std::string op_t_1d_name = is_all_tensor ? "OP_T_1D" : (P_1d > Q_1d ? P_name : Q_name);
630dc007f05SJeremy L Thompson 
631c433aabcSJeremy L Thompson           code << "    " << function_name << "<num_comp" << var_suffix << ", " << P_name << ", " << Q_name << ", " << op_t_1d_name << ">(data, r_q"
632c433aabcSJeremy L Thompson                << var_suffix << ", s_B" << var_suffix << ", s_G" << var_suffix << ", r_e" << var_suffix << ");\n";
6334b3e95d5SJeremy L Thompson         } else {
634dc007f05SJeremy L Thompson           std::string function_name = "GradTransposeNonTensor";
635dc007f05SJeremy L Thompson 
636c433aabcSJeremy L Thompson           code << "    " << function_name << "<num_comp" << var_suffix << ", dim" << var_suffix << ", " << P_name << ", " << Q_name
637c433aabcSJeremy L Thompson                << ", OP_T_1D>(data, r_q" << var_suffix << ", s_G" << var_suffix << ", r_e" << var_suffix << ");\n";
6384b3e95d5SJeremy L Thompson         }
6394b3e95d5SJeremy L Thompson         break;
6404b3e95d5SJeremy L Thompson       // LCOV_EXCL_START
6414b3e95d5SJeremy L Thompson       case CEED_EVAL_WEIGHT:
6424b3e95d5SJeremy L Thompson         break;  // Should not occur
6434b3e95d5SJeremy L Thompson       case CEED_EVAL_DIV:
6444b3e95d5SJeremy L Thompson       case CEED_EVAL_CURL:
6454b3e95d5SJeremy L Thompson         break;  // TODO: Not implemented
6464b3e95d5SJeremy L Thompson                 // LCOV_EXCL_STOP
6474b3e95d5SJeremy L Thompson     }
6484b3e95d5SJeremy L Thompson   }
649681d0ea7SJeremy L Thompson   CeedCallBackend(CeedBasisDestroy(&basis));
6504b3e95d5SJeremy L Thompson   return CEED_ERROR_SUCCESS;
6514b3e95d5SJeremy L Thompson }
6524b3e95d5SJeremy L Thompson 
6534b3e95d5SJeremy L Thompson //------------------------------------------------------------------------------
6544b3e95d5SJeremy L Thompson // QFunction
6554b3e95d5SJeremy L Thompson //------------------------------------------------------------------------------
656412e5683SJeremy L Thompson static int CeedOperatorBuildKernelQFunction_Cuda_gen(std::ostringstream &code, CeedOperator_Cuda_gen *data, CeedInt max_dim, CeedInt max_num_points,
6578b97b69aSJeremy L Thompson                                                      CeedInt num_input_fields, CeedOperatorField *op_input_fields,
6588b97b69aSJeremy L Thompson                                                      CeedQFunctionField *qf_input_fields, CeedInt num_output_fields,
6598b97b69aSJeremy L Thompson                                                      CeedOperatorField *op_output_fields, CeedQFunctionField *qf_output_fields,
660412e5683SJeremy L Thompson                                                      std::string qfunction_name, CeedInt Q_1d, bool is_all_tensor, bool is_at_points,
661dc007f05SJeremy L Thompson                                                      bool use_3d_slices) {
662412e5683SJeremy L Thompson   std::string         Q_name    = is_all_tensor ? "Q_1d" : "Q";
6634b3e95d5SJeremy L Thompson   CeedEvalMode        eval_mode = CEED_EVAL_NONE;
6644b3e95d5SJeremy L Thompson   CeedElemRestriction elem_rstr;
6654b3e95d5SJeremy L Thompson 
6668b97b69aSJeremy L Thompson   // Setup output arrays
6674b3e95d5SJeremy L Thompson   code << "\n    // -- Output field setup\n";
6684b3e95d5SJeremy L Thompson   for (CeedInt i = 0; i < num_output_fields; i++) {
66959fa3f92SJeremy L Thompson     const char *field_name;
6704b3e95d5SJeremy L Thompson     std::string var_suffix = "_out_" + std::to_string(i);
6714b3e95d5SJeremy L Thompson 
67259fa3f92SJeremy L Thompson     CeedCallBackend(CeedOperatorFieldGetName(op_output_fields[i], &field_name));
67359fa3f92SJeremy L Thompson     code << "    // ---- Output field " << i << ": " << field_name << "\n";
6744b3e95d5SJeremy L Thompson     CeedCallBackend(CeedQFunctionFieldGetEvalMode(qf_output_fields[i], &eval_mode));
6758b97b69aSJeremy L Thompson     switch (eval_mode) {
6768b97b69aSJeremy L Thompson       case CEED_EVAL_NONE:
6778b97b69aSJeremy L Thompson         if (is_at_points) {
6788b97b69aSJeremy L Thompson           code << "    CeedScalar r_q" << var_suffix << "[num_comp" << var_suffix << "];\n";
6798b97b69aSJeremy L Thompson         } else {
680412e5683SJeremy L Thompson           code << "    CeedScalar r_q" << var_suffix << "[num_comp" << var_suffix << "*" << (is_all_tensor && (max_dim >= 3) ? Q_name : "1")
681412e5683SJeremy L Thompson                << "];\n";
6824b3e95d5SJeremy L Thompson         }
6838b97b69aSJeremy L Thompson         break;
6848b97b69aSJeremy L Thompson       case CEED_EVAL_INTERP:
6858b97b69aSJeremy L Thompson         if (is_at_points) {
6868b97b69aSJeremy L Thompson           // Accumulator for point data
687412e5683SJeremy L Thompson           code << "    CeedScalar r_c" << var_suffix << "[num_comp" << var_suffix << "*" << (max_dim >= 3 ? Q_name : "1") << "];\n";
688412e5683SJeremy L Thompson           code << "    for (CeedInt i = 0; i < num_comp" << var_suffix << "*" << (max_dim >= 3 ? Q_name : "1") << "; i++) {\n";
6898b97b69aSJeremy L Thompson           code << "      r_c" << var_suffix << "[i] = 0.0;\n";
6908b97b69aSJeremy L Thompson           code << "    }\n";
6918b97b69aSJeremy L Thompson         } else {
692412e5683SJeremy L Thompson           code << "    CeedScalar r_q" << var_suffix << "[num_comp" << var_suffix << "*" << (is_all_tensor && (max_dim >= 3) ? Q_name : "1")
693412e5683SJeremy L Thompson                << "];\n";
6948b97b69aSJeremy L Thompson         }
6958b97b69aSJeremy L Thompson         break;
6968b97b69aSJeremy L Thompson       case CEED_EVAL_GRAD:
6978b97b69aSJeremy L Thompson         if (is_at_points) {
6988b97b69aSJeremy L Thompson           // Accumulator for point data
699412e5683SJeremy L Thompson           code << "    CeedScalar r_c" << var_suffix << "[num_comp" << var_suffix << "*" << (max_dim >= 3 ? Q_name : "1") << "*dim" << var_suffix
700412e5683SJeremy L Thompson                << "];\n";
701412e5683SJeremy L Thompson           code << "    for (CeedInt i = 0; i < num_comp" << var_suffix << "*" << (max_dim >= 3 ? Q_name : "1") << "; i++) {\n";
7028b97b69aSJeremy L Thompson           code << "      r_c" << var_suffix << "[i] = 0.0;\n";
7038b97b69aSJeremy L Thompson           code << "    }\n";
7048b97b69aSJeremy L Thompson         } else if (use_3d_slices) {
7054b3e95d5SJeremy L Thompson           // Accumulator for gradient slices
7064b3e95d5SJeremy L Thompson           code << "    CeedScalar r_q" << var_suffix << "[num_comp" << var_suffix << "*" << Q_name << "];\n";
7074b3e95d5SJeremy L Thompson           code << "    for (CeedInt i = 0; i < num_comp" << var_suffix << "*" << Q_name << "; i++) {\n";
7084b3e95d5SJeremy L Thompson           code << "      r_q" << var_suffix << "[i] = 0.0;\n";
7094b3e95d5SJeremy L Thompson           code << "    }\n";
7104b3e95d5SJeremy L Thompson         } else {
711412e5683SJeremy L Thompson           code << "    CeedScalar r_q" << var_suffix << "[num_comp" << var_suffix << "*dim" << var_suffix << "*"
712412e5683SJeremy L Thompson                << (is_all_tensor && (max_dim >= 3) ? Q_name : "1") << "];\n";
7134b3e95d5SJeremy L Thompson         }
7148b97b69aSJeremy L Thompson         break;
7158b97b69aSJeremy L Thompson       case CEED_EVAL_WEIGHT:
7168b97b69aSJeremy L Thompson         break;
7178b97b69aSJeremy L Thompson         // LCOV_EXCL_START
7188b97b69aSJeremy L Thompson       case CEED_EVAL_DIV:
7198b97b69aSJeremy L Thompson       case CEED_EVAL_CURL:
7208b97b69aSJeremy L Thompson         break;  // TODO: Not implemented
7218b97b69aSJeremy L Thompson                 // LCOV_EXCL_STOP
7224b3e95d5SJeremy L Thompson     }
7234b3e95d5SJeremy L Thompson   }
7244b3e95d5SJeremy L Thompson 
7258b97b69aSJeremy L Thompson   if (is_at_points) {
7268b97b69aSJeremy L Thompson     // We need to handle batches of points
7278b97b69aSJeremy L Thompson     code << "\n    // Note: Using batches of points\n";
7288b97b69aSJeremy L Thompson     code << "    const CeedInt point_loop_bound = (blockDim.x * blockDim.y) * ceil(1.0 * max_num_points / (blockDim.x * blockDim.y));\n\n";
7298b97b69aSJeremy L Thompson     code << "    #pragma unroll\n";
7308b97b69aSJeremy L Thompson     code << "    for (CeedInt i = threadIdx.x + threadIdx.y * blockDim.x; i < point_loop_bound; i += blockDim.x * blockDim.y) {\n";
7318b97b69aSJeremy L Thompson     code << "      const CeedInt p = i % max_num_points;\n\n";
7328b97b69aSJeremy L Thompson 
7338b97b69aSJeremy L Thompson     code << "      // -- Coordinates\n";
734412e5683SJeremy L Thompson     code << "      CeedScalar r_x[max_dim];\n";
735412e5683SJeremy L Thompson     code << "      ReadPoint<max_dim, coords_comp_stride, max_num_points>(data, elem, p, max_num_points, points.indices, points.coords, r_x);\n\n";
7368b97b69aSJeremy L Thompson 
7378b97b69aSJeremy L Thompson     code << "      // -- Input fields\n";
7388b97b69aSJeremy L Thompson     for (CeedInt i = 0; i < num_input_fields; i++) {
73959fa3f92SJeremy L Thompson       const char *field_name;
7408b97b69aSJeremy L Thompson       std::string var_suffix = "_in_" + std::to_string(i);
741f725b54bSJeremy L Thompson       std::string P_name     = "P_1d" + var_suffix;
7428b97b69aSJeremy L Thompson 
74359fa3f92SJeremy L Thompson       CeedCallBackend(CeedOperatorFieldGetName(op_input_fields[i], &field_name));
74459fa3f92SJeremy L Thompson       code << "      // ---- Input field " << i << ": " << field_name << "\n";
7458b97b69aSJeremy L Thompson       CeedCallBackend(CeedQFunctionFieldGetEvalMode(qf_input_fields[i], &eval_mode));
7468b97b69aSJeremy L Thompson       // Basis action
7478b97b69aSJeremy L Thompson       code << "      // EvalMode: " << CeedEvalModes[eval_mode] << "\n";
7488b97b69aSJeremy L Thompson       switch (eval_mode) {
7498b97b69aSJeremy L Thompson         case CEED_EVAL_NONE:
7508b97b69aSJeremy L Thompson           code << "      CeedScalar r_s" << var_suffix << "[num_comp" << var_suffix << "];\n";
7518b97b69aSJeremy L Thompson           code << "      ReadPoint<num_comp" << var_suffix << ", comp_stride" << var_suffix
7528b97b69aSJeremy L Thompson                << ", max_num_points>(data, elem, p, max_num_points, indices.inputs[" << i << "], d" << var_suffix << ", r_s" << var_suffix << ");\n";
7538b97b69aSJeremy L Thompson           break;
7548b97b69aSJeremy L Thompson         case CEED_EVAL_INTERP:
7558b97b69aSJeremy L Thompson           code << "      CeedScalar r_s" << var_suffix << "[num_comp" << var_suffix << "];\n";
756412e5683SJeremy L Thompson           code << "      InterpAtPoints" << max_dim << "d<num_comp" << var_suffix << ", max_num_points, " << P_name << ", " << Q_name
757412e5683SJeremy L Thompson                << ">(data, i, r_c" << var_suffix << ", r_x, r_s" << var_suffix << ");\n";
7588b97b69aSJeremy L Thompson           break;
7598b97b69aSJeremy L Thompson         case CEED_EVAL_GRAD:
760412e5683SJeremy L Thompson           code << "      CeedScalar r_s" << var_suffix << "[num_comp" << var_suffix << "*dim" << var_suffix << "];\n";
761412e5683SJeremy L Thompson           code << "      GradAtPoints" << max_dim << "d<num_comp" << var_suffix << ", max_num_points, " << P_name << ", " << Q_name
762412e5683SJeremy L Thompson                << ">(data, i, r_c" << var_suffix << ", r_x, r_s" << var_suffix << ");\n";
7638b97b69aSJeremy L Thompson           break;
7648b97b69aSJeremy L Thompson         case CEED_EVAL_WEIGHT:
7658b97b69aSJeremy L Thompson           code << "      CeedScalar r_s" << var_suffix << "[1];\n";
7668b97b69aSJeremy L Thompson           code << "      r_s" << var_suffix << "[0] = 1.0;\n";
7678b97b69aSJeremy L Thompson           break;
7688b97b69aSJeremy L Thompson           // LCOV_EXCL_START
7698b97b69aSJeremy L Thompson         case CEED_EVAL_DIV:
7708b97b69aSJeremy L Thompson         case CEED_EVAL_CURL:
7718b97b69aSJeremy L Thompson           break;  // TODO: Not implemented
7728b97b69aSJeremy L Thompson                   // LCOV_EXCL_STOP
7738b97b69aSJeremy L Thompson       }
7748b97b69aSJeremy L Thompson     }
7758b97b69aSJeremy L Thompson     code << "\n      // -- Output fields\n";
7768b97b69aSJeremy L Thompson     for (CeedInt i = 0; i < num_output_fields; i++) {
77759fa3f92SJeremy L Thompson       const char *field_name;
7788b97b69aSJeremy L Thompson       std::string var_suffix = "_out_" + std::to_string(i);
7798b97b69aSJeremy L Thompson 
78059fa3f92SJeremy L Thompson       CeedCallBackend(CeedOperatorFieldGetName(op_output_fields[i], &field_name));
78159fa3f92SJeremy L Thompson       code << "      // ---- Output field " << i << ": " << field_name << "\n";
7828b97b69aSJeremy L Thompson       CeedCallBackend(CeedQFunctionFieldGetEvalMode(qf_output_fields[i], &eval_mode));
7838b97b69aSJeremy L Thompson       // Basis action
7848b97b69aSJeremy L Thompson       switch (eval_mode) {
7858b97b69aSJeremy L Thompson         case CEED_EVAL_NONE:
7868b97b69aSJeremy L Thompson           code << "      CeedScalar r_s" << var_suffix << "[num_comp" << var_suffix << "];\n";
7878b97b69aSJeremy L Thompson           break;
7888b97b69aSJeremy L Thompson         case CEED_EVAL_INTERP:
7898b97b69aSJeremy L Thompson           code << "      CeedScalar r_s" << var_suffix << "[num_comp" << var_suffix << "];\n";
7908b97b69aSJeremy L Thompson           break;
7918b97b69aSJeremy L Thompson         case CEED_EVAL_GRAD:
792412e5683SJeremy L Thompson           code << "      CeedScalar r_s" << var_suffix << "[num_comp" << var_suffix << "*dim" << var_suffix << "];\n";
7938b97b69aSJeremy L Thompson           break;
7948b97b69aSJeremy L Thompson           // LCOV_EXCL_START
7958b97b69aSJeremy L Thompson         case CEED_EVAL_WEIGHT:
7968b97b69aSJeremy L Thompson           break;  // Should not occur
7978b97b69aSJeremy L Thompson         case CEED_EVAL_DIV:
7988b97b69aSJeremy L Thompson         case CEED_EVAL_CURL:
7998b97b69aSJeremy L Thompson           break;  // TODO: Not implemented
8008b97b69aSJeremy L Thompson                   // LCOV_EXCL_STOP
8018b97b69aSJeremy L Thompson       }
8028b97b69aSJeremy L Thompson     }
8038b97b69aSJeremy L Thompson 
8048b97b69aSJeremy L Thompson   } else if (use_3d_slices) {
8054b3e95d5SJeremy L Thompson     // We treat quadrature points per slice in 3d to save registers
8064b3e95d5SJeremy L Thompson     code << "\n    // Note: Using planes of 3D elements\n";
8074b3e95d5SJeremy L Thompson     code << "    #pragma unroll\n";
8084b3e95d5SJeremy L Thompson     code << "    for (CeedInt q = 0; q < " << Q_name << "; q++) {\n";
8094b3e95d5SJeremy L Thompson     code << "      // -- Input fields\n";
8104b3e95d5SJeremy L Thompson     for (CeedInt i = 0; i < num_input_fields; i++) {
81159fa3f92SJeremy L Thompson       const char *field_name;
8124b3e95d5SJeremy L Thompson       std::string var_suffix = "_in_" + std::to_string(i);
8134b3e95d5SJeremy L Thompson 
81459fa3f92SJeremy L Thompson       CeedCallBackend(CeedOperatorFieldGetName(op_input_fields[i], &field_name));
81559fa3f92SJeremy L Thompson       code << "      // ---- Input field " << i << ": " << field_name << "\n";
8164b3e95d5SJeremy L Thompson       CeedCallBackend(CeedQFunctionFieldGetEvalMode(qf_input_fields[i], &eval_mode));
8174b3e95d5SJeremy L Thompson       // Basis action
8184b3e95d5SJeremy L Thompson       code << "      // EvalMode: " << CeedEvalModes[eval_mode] << "\n";
8194b3e95d5SJeremy L Thompson       switch (eval_mode) {
8204b3e95d5SJeremy L Thompson         case CEED_EVAL_NONE:
8214b3e95d5SJeremy L Thompson           bool is_strided;
8224b3e95d5SJeremy L Thompson 
8234b3e95d5SJeremy L Thompson           code << "      CeedScalar r_s" << var_suffix << "[num_comp" << var_suffix << "];\n";
8244b3e95d5SJeremy L Thompson 
8254b3e95d5SJeremy L Thompson           CeedCallBackend(CeedOperatorFieldGetElemRestriction(op_input_fields[i], &elem_rstr));
8264b3e95d5SJeremy L Thompson           CeedCallBackend(CeedElemRestrictionIsStrided(elem_rstr, &is_strided));
8274b3e95d5SJeremy L Thompson           if (is_strided) {
8284b3e95d5SJeremy L Thompson             bool    has_backend_strides;
8294b3e95d5SJeremy L Thompson             CeedInt num_elem, elem_size;
8304b3e95d5SJeremy L Thompson 
8314b3e95d5SJeremy L Thompson             CeedCallBackend(CeedElemRestrictionGetElementSize(elem_rstr, &elem_size));
8324b3e95d5SJeremy L Thompson             CeedCallBackend(CeedElemRestrictionHasBackendStrides(elem_rstr, &has_backend_strides));
8334b3e95d5SJeremy L Thompson             CeedCallBackend(CeedElemRestrictionGetNumElements(elem_rstr, &num_elem));
8344b3e95d5SJeremy L Thompson             CeedInt strides[3] = {1, elem_size * num_elem, elem_size};
8354b3e95d5SJeremy L Thompson 
8364b3e95d5SJeremy L Thompson             if (!has_backend_strides) {
8374b3e95d5SJeremy L Thompson               CeedCallBackend(CeedElemRestrictionGetStrides(elem_rstr, strides));
8384b3e95d5SJeremy L Thompson             }
8394b3e95d5SJeremy L Thompson             code << "      // Strides: {" << strides[0] << ", " << strides[1] << ", " << strides[2] << "}\n";
840f815fac9SJeremy L Thompson             code << "      ReadEVecSliceStrided3d<num_comp" << var_suffix << ", " << Q_name << ", " << strides[0] << ", " << strides[1] << ", "
8414b3e95d5SJeremy L Thompson                  << strides[2] << ">(data, elem, q, d" << var_suffix << ", r_s" << var_suffix << ");\n";
8424b3e95d5SJeremy L Thompson           } else {
8434b3e95d5SJeremy L Thompson             CeedSize                  l_size = 0;
8444b3e95d5SJeremy L Thompson             CeedInt                   comp_stride;
8454b3e95d5SJeremy L Thompson             CeedElemRestriction_Cuda *rstr_data;
8464b3e95d5SJeremy L Thompson 
8474b3e95d5SJeremy L Thompson             CeedCallBackend(CeedElemRestrictionGetLVectorSize(elem_rstr, &l_size));
8484b3e95d5SJeremy L Thompson             code << "      const CeedInt l_size" << var_suffix << " = " << l_size << ";\n";
8494b3e95d5SJeremy L Thompson             CeedCallBackend(CeedElemRestrictionGetCompStride(elem_rstr, &comp_stride));
8504b3e95d5SJeremy L Thompson             code << "      // CompStride: " << comp_stride << "\n";
8514b3e95d5SJeremy L Thompson             CeedCallBackend(CeedElemRestrictionGetData(elem_rstr, &rstr_data));
8524b3e95d5SJeremy L Thompson             data->indices.inputs[i] = (CeedInt *)rstr_data->d_offsets;
853f815fac9SJeremy L Thompson             code << "      ReadEVecSliceStandard3d<num_comp" << var_suffix << ", " << comp_stride << ", " << Q_name << ">(data, l_size" << var_suffix
8544b3e95d5SJeremy L Thompson                  << ", elem, q, indices.inputs[" << i << "], d" << var_suffix << ", r_s" << var_suffix << ");\n";
8554b3e95d5SJeremy L Thompson           }
856681d0ea7SJeremy L Thompson           CeedCallBackend(CeedElemRestrictionDestroy(&elem_rstr));
8574b3e95d5SJeremy L Thompson           break;
8584b3e95d5SJeremy L Thompson         case CEED_EVAL_INTERP:
8594b3e95d5SJeremy L Thompson           code << "      CeedScalar r_s" << var_suffix << "[num_comp" << var_suffix << "];\n";
8604b3e95d5SJeremy L Thompson           code << "      for (CeedInt j = 0; j < num_comp" << var_suffix << "; j++) {\n";
8614b3e95d5SJeremy L Thompson           code << "        r_s" << var_suffix << "[j] = r_q" << var_suffix << "[q + j*" << Q_name << "];\n";
8624b3e95d5SJeremy L Thompson           code << "      }\n";
8634b3e95d5SJeremy L Thompson           break;
8644b3e95d5SJeremy L Thompson         case CEED_EVAL_GRAD:
865412e5683SJeremy L Thompson           code << "      CeedScalar r_s" << var_suffix << "[num_comp" << var_suffix << "*dim" << var_suffix << "];\n";
86699421279SJeremy L Thompson           code << "      GradColloSlice3d<num_comp" << var_suffix << ", " << Q_name << ", OP_T_1D>(data, q, r_q" << var_suffix << ", s_G"
86799421279SJeremy L Thompson                << var_suffix << ", r_s" << var_suffix << ");\n";
8684b3e95d5SJeremy L Thompson           break;
8694b3e95d5SJeremy L Thompson         case CEED_EVAL_WEIGHT:
8704b3e95d5SJeremy L Thompson           code << "      CeedScalar r_s" << var_suffix << "[1];\n";
8714b3e95d5SJeremy L Thompson           code << "      r_s" << var_suffix << "[0] = r_q" << var_suffix << "[q];\n";
8728b97b69aSJeremy L Thompson           break;
8734b3e95d5SJeremy L Thompson           // LCOV_EXCL_START
8744b3e95d5SJeremy L Thompson         case CEED_EVAL_DIV:
8754b3e95d5SJeremy L Thompson         case CEED_EVAL_CURL:
8764b3e95d5SJeremy L Thompson           break;  // TODO: Not implemented
8774b3e95d5SJeremy L Thompson                   // LCOV_EXCL_STOP
8784b3e95d5SJeremy L Thompson       }
8794b3e95d5SJeremy L Thompson     }
8804b3e95d5SJeremy L Thompson     code << "\n      // -- Output fields\n";
8814b3e95d5SJeremy L Thompson     for (CeedInt i = 0; i < num_output_fields; i++) {
88259fa3f92SJeremy L Thompson       const char *field_name;
8834b3e95d5SJeremy L Thompson       std::string var_suffix = "_out_" + std::to_string(i);
8844b3e95d5SJeremy L Thompson 
88559fa3f92SJeremy L Thompson       CeedCallBackend(CeedOperatorFieldGetName(op_output_fields[i], &field_name));
88659fa3f92SJeremy L Thompson       code << "      // ---- Output field " << i << ": " << field_name << "\n";
8874b3e95d5SJeremy L Thompson       CeedCallBackend(CeedQFunctionFieldGetEvalMode(qf_output_fields[i], &eval_mode));
8884b3e95d5SJeremy L Thompson       // Basis action
8894b3e95d5SJeremy L Thompson       switch (eval_mode) {
8904b3e95d5SJeremy L Thompson         case CEED_EVAL_NONE:
8914b3e95d5SJeremy L Thompson           code << "      CeedScalar r_s" << var_suffix << "[num_comp" << var_suffix << "];\n";
8928b97b69aSJeremy L Thompson           break;
8934b3e95d5SJeremy L Thompson         case CEED_EVAL_INTERP:
8944b3e95d5SJeremy L Thompson           code << "      CeedScalar r_s" << var_suffix << "[num_comp" << var_suffix << "];\n";
8954b3e95d5SJeremy L Thompson           break;
8964b3e95d5SJeremy L Thompson         case CEED_EVAL_GRAD:
897412e5683SJeremy L Thompson           code << "      CeedScalar r_s" << var_suffix << "[num_comp" << var_suffix << "*dim" << var_suffix << "];\n";
8984b3e95d5SJeremy L Thompson           break;
8994b3e95d5SJeremy L Thompson           // LCOV_EXCL_START
9004b3e95d5SJeremy L Thompson         case CEED_EVAL_WEIGHT:
9014b3e95d5SJeremy L Thompson           break;  // Should not occur
9024b3e95d5SJeremy L Thompson         case CEED_EVAL_DIV:
9034b3e95d5SJeremy L Thompson         case CEED_EVAL_CURL:
9044b3e95d5SJeremy L Thompson           break;  // TODO: Not implemented
9054b3e95d5SJeremy L Thompson                   // LCOV_EXCL_STOP
9064b3e95d5SJeremy L Thompson       }
9074b3e95d5SJeremy L Thompson     }
9084b3e95d5SJeremy L Thompson   } else {
9094b3e95d5SJeremy L Thompson     code << "\n    // Note: Using full elements\n";
9104b3e95d5SJeremy L Thompson     code << "    {\n";
9114b3e95d5SJeremy L Thompson     code << "      // -- Input fields\n";
9124b3e95d5SJeremy L Thompson     for (CeedInt i = 0; i < num_input_fields; i++) {
91359fa3f92SJeremy L Thompson       const char *field_name;
91459fa3f92SJeremy L Thompson 
91559fa3f92SJeremy L Thompson       CeedCallBackend(CeedOperatorFieldGetName(op_input_fields[i], &field_name));
91659fa3f92SJeremy L Thompson       code << "      // ---- Input field " << i << ": " << field_name << "\n";
9174b3e95d5SJeremy L Thompson       code << "      CeedScalar *r_s_in_" << i << " = r_q_in_" << i << ";\n";
9184b3e95d5SJeremy L Thompson     }
9194b3e95d5SJeremy L Thompson     code << "      // -- Output fields\n";
9204b3e95d5SJeremy L Thompson     for (CeedInt i = 0; i < num_output_fields; i++) {
92159fa3f92SJeremy L Thompson       const char *field_name;
92259fa3f92SJeremy L Thompson 
92359fa3f92SJeremy L Thompson       CeedCallBackend(CeedOperatorFieldGetName(op_output_fields[i], &field_name));
92459fa3f92SJeremy L Thompson       code << "      // ---- Output field " << i << ": " << field_name << "\n";
9254b3e95d5SJeremy L Thompson       code << "      CeedScalar *r_s_out_" << i << " = r_q_out_" << i << ";\n";
9264b3e95d5SJeremy L Thompson     }
9274b3e95d5SJeremy L Thompson   }
9284b3e95d5SJeremy L Thompson 
9294b3e95d5SJeremy L Thompson   // Input and output buffers
9304b3e95d5SJeremy L Thompson   code << "\n      // -- QFunction inputs and outputs\n";
9314b3e95d5SJeremy L Thompson   code << "      // ---- Inputs\n";
9324b3e95d5SJeremy L Thompson   code << "      CeedScalar *inputs[" << CeedIntMax(num_input_fields, 1) << "];\n";
9334b3e95d5SJeremy L Thompson   for (CeedInt i = 0; i < num_input_fields; i++) {
93459fa3f92SJeremy L Thompson     const char *field_name;
93559fa3f92SJeremy L Thompson 
93659fa3f92SJeremy L Thompson     CeedCallBackend(CeedOperatorFieldGetName(op_input_fields[i], &field_name));
93759fa3f92SJeremy L Thompson     code << "      // ------ Input field " << i << ": " << field_name << "\n";
9384b3e95d5SJeremy L Thompson     code << "      inputs[" << i << "] = r_s_in_" << i << ";\n";
9394b3e95d5SJeremy L Thompson   }
9404b3e95d5SJeremy L Thompson   code << "      // ---- Outputs\n";
9414b3e95d5SJeremy L Thompson   code << "      CeedScalar *outputs[" << CeedIntMax(num_output_fields, 1) << "];\n";
9424b3e95d5SJeremy L Thompson   for (CeedInt i = 0; i < num_output_fields; i++) {
94359fa3f92SJeremy L Thompson     const char *field_name;
94459fa3f92SJeremy L Thompson 
94559fa3f92SJeremy L Thompson     CeedCallBackend(CeedOperatorFieldGetName(op_output_fields[i], &field_name));
94659fa3f92SJeremy L Thompson     code << "      // ------ Output field " << i << ": " << field_name << "\n";
9474b3e95d5SJeremy L Thompson     code << "      outputs[" << i << "] = r_s_out_" << i << ";\n";
9484b3e95d5SJeremy L Thompson   }
9494b3e95d5SJeremy L Thompson 
9504b3e95d5SJeremy L Thompson   // Apply QFunction
9514b3e95d5SJeremy L Thompson   code << "\n      // -- Apply QFunction\n";
9524b3e95d5SJeremy L Thompson   code << "      " << qfunction_name << "(ctx, ";
953412e5683SJeremy L Thompson   if (max_dim != 3 || is_at_points || use_3d_slices || !is_all_tensor) {
9544b3e95d5SJeremy L Thompson     code << "1";
9554b3e95d5SJeremy L Thompson   } else {
956dc007f05SJeremy L Thompson     code << Q_name;
9574b3e95d5SJeremy L Thompson   }
9584b3e95d5SJeremy L Thompson   code << ", inputs, outputs);\n";
9594b3e95d5SJeremy L Thompson 
9608b97b69aSJeremy L Thompson   if (is_at_points) {
9618b97b69aSJeremy L Thompson     // Map back to coefficients
9628b97b69aSJeremy L Thompson     code << "\n      // -- Output fields\n";
9638b97b69aSJeremy L Thompson     for (CeedInt i = 0; i < num_output_fields; i++) {
96459fa3f92SJeremy L Thompson       const char *field_name;
9658b97b69aSJeremy L Thompson       std::string var_suffix = "_out_" + std::to_string(i);
9668b97b69aSJeremy L Thompson       std::string P_name     = "P_1d" + var_suffix;
9678b97b69aSJeremy L Thompson 
96859fa3f92SJeremy L Thompson       CeedCallBackend(CeedOperatorFieldGetName(op_output_fields[i], &field_name));
96959fa3f92SJeremy L Thompson       code << "      // ---- Output field " << i << ": " << field_name << "\n";
9708b97b69aSJeremy L Thompson       CeedCallBackend(CeedQFunctionFieldGetEvalMode(qf_output_fields[i], &eval_mode));
9718b97b69aSJeremy L Thompson       // Basis action
9728b97b69aSJeremy L Thompson       code << "      // EvalMode: " << CeedEvalModes[eval_mode] << "\n";
9738b97b69aSJeremy L Thompson       switch (eval_mode) {
9748b97b69aSJeremy L Thompson         case CEED_EVAL_NONE: {
9758b97b69aSJeremy L Thompson           CeedInt             comp_stride;
9768b97b69aSJeremy L Thompson           CeedElemRestriction elem_rstr;
9778b97b69aSJeremy L Thompson 
9788b97b69aSJeremy L Thompson           CeedCallBackend(CeedOperatorFieldGetElemRestriction(op_output_fields[i], &elem_rstr));
9798b97b69aSJeremy L Thompson           CeedCallBackend(CeedElemRestrictionGetCompStride(elem_rstr, &comp_stride));
9808b97b69aSJeremy L Thompson           CeedCallBackend(CeedElemRestrictionDestroy(&elem_rstr));
9818b97b69aSJeremy L Thompson           code << "      const CeedInt comp_stride" << var_suffix << " = " << comp_stride << ";\n";
9828b97b69aSJeremy L Thompson           code << "      WritePoint<num_comp" << var_suffix << ", comp_stride" << var_suffix
9838b97b69aSJeremy L Thompson                << ", max_num_points>(data, elem, i, points.num_per_elem[elem], indices.outputs[" << i << "]"
9848b97b69aSJeremy L Thompson                << ", r_s" << var_suffix << ", d" << var_suffix << ");\n";
9858b97b69aSJeremy L Thompson           break;
9868b97b69aSJeremy L Thompson         }
9878b97b69aSJeremy L Thompson         case CEED_EVAL_INTERP:
9888b97b69aSJeremy L Thompson           code << "      if (i >= points.num_per_elem[elem]) {\n";
9898b97b69aSJeremy L Thompson           code << "        for (CeedInt j = 0; j < num_comp" << var_suffix << "; j++) r_s" << var_suffix << "[j] = 0.0;\n";
9908b97b69aSJeremy L Thompson           code << "      }\n";
991412e5683SJeremy L Thompson           code << "      InterpTransposeAtPoints" << max_dim << "d<num_comp" << var_suffix << ", max_num_points, " << P_name << ", " << Q_name
992f725b54bSJeremy L Thompson                << ">(data, i, r_s" << var_suffix << ", r_x, r_c" << var_suffix << ");\n";
9938b97b69aSJeremy L Thompson           break;
9948b97b69aSJeremy L Thompson         case CEED_EVAL_GRAD:
9958b97b69aSJeremy L Thompson           code << "      if (i >= points.num_per_elem[elem]) {\n";
996412e5683SJeremy L Thompson           code << "        for (CeedInt j = 0; j < num_comp" << var_suffix << "*dim" << var_suffix << "; j++) r_s" << var_suffix << "[j] = 0.0;\n";
9978b97b69aSJeremy L Thompson           code << "      }\n";
998412e5683SJeremy L Thompson           code << "      GradTransposeAtPoints" << max_dim << "d<num_comp" << var_suffix << ", max_num_points, " << P_name << ", " << Q_name
999f725b54bSJeremy L Thompson                << ">(data, i, r_s" << var_suffix << ", r_x, r_c" << var_suffix << ");\n";
10008b97b69aSJeremy L Thompson           break;
10018b97b69aSJeremy L Thompson           // LCOV_EXCL_START
10028b97b69aSJeremy L Thompson         case CEED_EVAL_WEIGHT:
10038b97b69aSJeremy L Thompson           break;  // Should not occur
10048b97b69aSJeremy L Thompson         case CEED_EVAL_DIV:
10058b97b69aSJeremy L Thompson         case CEED_EVAL_CURL:
10068b97b69aSJeremy L Thompson           break;  // TODO: Not implemented
10078b97b69aSJeremy L Thompson                   // LCOV_EXCL_STOP
10088b97b69aSJeremy L Thompson       }
10098b97b69aSJeremy L Thompson     }
10108b97b69aSJeremy L Thompson   } else if (use_3d_slices) {
10114b3e95d5SJeremy L Thompson     // Copy or apply transpose grad, if needed
10128b97b69aSJeremy L Thompson     code << "\n      // -- Output fields\n";
10134b3e95d5SJeremy L Thompson     for (CeedInt i = 0; i < num_output_fields; i++) {
101459fa3f92SJeremy L Thompson       const char *field_name;
10154b3e95d5SJeremy L Thompson       std::string var_suffix = "_out_" + std::to_string(i);
10164b3e95d5SJeremy L Thompson       std::string P_name     = "P_1d" + var_suffix;
10174b3e95d5SJeremy L Thompson 
101859fa3f92SJeremy L Thompson       CeedCallBackend(CeedOperatorFieldGetName(op_output_fields[i], &field_name));
101959fa3f92SJeremy L Thompson       code << "      // ---- Output field " << i << ": " << field_name << "\n";
10204b3e95d5SJeremy L Thompson       CeedCallBackend(CeedQFunctionFieldGetEvalMode(qf_output_fields[i], &eval_mode));
10214b3e95d5SJeremy L Thompson       // Basis action
10224b3e95d5SJeremy L Thompson       code << "      // EvalMode: " << CeedEvalModes[eval_mode] << "\n";
10234b3e95d5SJeremy L Thompson       switch (eval_mode) {
10244b3e95d5SJeremy L Thompson         case CEED_EVAL_NONE:
10254b3e95d5SJeremy L Thompson           code << "      for (CeedInt j = 0; j < num_comp" << var_suffix << " ; j++) {\n";
10264b3e95d5SJeremy L Thompson           code << "        r_q" << var_suffix << "[q + j*" << Q_name << "] = r_s" << var_suffix << "[j];\n";
10274b3e95d5SJeremy L Thompson           code << "      }\n";
10288b97b69aSJeremy L Thompson           break;
10294b3e95d5SJeremy L Thompson         case CEED_EVAL_INTERP:
10304b3e95d5SJeremy L Thompson           code << "      for (CeedInt j = 0; j < num_comp" << var_suffix << " ; j++) {\n";
10314b3e95d5SJeremy L Thompson           code << "        r_q" << var_suffix << "[q + j*" << Q_name << "] = r_s" << var_suffix << "[j];\n";
10324b3e95d5SJeremy L Thompson           code << "      }\n";
10334b3e95d5SJeremy L Thompson           break;
10344b3e95d5SJeremy L Thompson         case CEED_EVAL_GRAD:
103599421279SJeremy L Thompson           code << "      GradColloSliceTranspose3d<num_comp" << var_suffix << ", " << Q_name << ", OP_T_1D>(data, q, r_s" << var_suffix << ", s_G"
1036f815fac9SJeremy L Thompson                << var_suffix << ", r_q" << var_suffix << ");\n";
10374b3e95d5SJeremy L Thompson           break;
10384b3e95d5SJeremy L Thompson           // LCOV_EXCL_START
10394b3e95d5SJeremy L Thompson         case CEED_EVAL_WEIGHT:
10404b3e95d5SJeremy L Thompson           break;  // Should not occur
10414b3e95d5SJeremy L Thompson         case CEED_EVAL_DIV:
10424b3e95d5SJeremy L Thompson         case CEED_EVAL_CURL:
10434b3e95d5SJeremy L Thompson           break;  // TODO: Not implemented
10444b3e95d5SJeremy L Thompson                   // LCOV_EXCL_STOP
10454b3e95d5SJeremy L Thompson       }
10464b3e95d5SJeremy L Thompson     }
10474b3e95d5SJeremy L Thompson   }
10484b3e95d5SJeremy L Thompson   code << "    }\n";
10494b3e95d5SJeremy L Thompson   return CEED_ERROR_SUCCESS;
10504b3e95d5SJeremy L Thompson }
10514b3e95d5SJeremy L Thompson 
10524b3e95d5SJeremy L Thompson //------------------------------------------------------------------------------
1053b2165e7aSSebastian Grimberg // Build single operator kernel
1054ab213215SJeremy L Thompson //------------------------------------------------------------------------------
1055ddae5012SJeremy L Thompson extern "C" int CeedOperatorBuildKernel_Cuda_gen(CeedOperator op, bool *is_good_build) {
1056412e5683SJeremy L Thompson   bool                    is_all_tensor = true, is_all_nontensor = true, is_at_points = false, use_3d_slices = false;
1057241a4b83SYohann   Ceed                    ceed;
1058*efa41df3SJeremy 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;
1059ca735530SJeremy L Thompson   CeedQFunctionField     *qf_input_fields, *qf_output_fields;
1060ca735530SJeremy L Thompson   CeedQFunction_Cuda_gen *qf_data;
1061ca735530SJeremy L Thompson   CeedQFunction           qf;
1062ca735530SJeremy L Thompson   CeedOperatorField      *op_input_fields, *op_output_fields;
1063ca735530SJeremy L Thompson   CeedOperator_Cuda_gen  *data;
10644b3e95d5SJeremy L Thompson   std::ostringstream      code;
10654b3e95d5SJeremy L Thompson 
1066ddae5012SJeremy L Thompson   CeedCallBackend(CeedOperatorGetData(op, &data));
10674b3e95d5SJeremy L Thompson   {
10684b3e95d5SJeremy L Thompson     bool is_setup_done;
1069ca735530SJeremy L Thompson 
1070ca735530SJeremy L Thompson     CeedCallBackend(CeedOperatorIsSetupDone(op, &is_setup_done));
1071ddae5012SJeremy L Thompson     if (is_setup_done) {
1072ddae5012SJeremy L Thompson       *is_good_build = !data->use_fallback;
1073ddae5012SJeremy L Thompson       return CEED_ERROR_SUCCESS;
1074ddae5012SJeremy L Thompson     }
1075ddae5012SJeremy L Thompson   }
1076ddae5012SJeremy L Thompson 
1077ddae5012SJeremy L Thompson   // Check field compatibility
10788d12f40eSJeremy L Thompson   CeedCallBackend(CeedOperatorGetFields(op, &num_input_fields, &op_input_fields, &num_output_fields, &op_output_fields));
1079ddae5012SJeremy L Thompson   {
1080412e5683SJeremy L Thompson     bool has_shared_bases = true;
1081ddae5012SJeremy L Thompson 
1082ddae5012SJeremy L Thompson     for (CeedInt i = 0; i < num_input_fields; i++) {
1083ddae5012SJeremy L Thompson       CeedBasis basis;
1084ddae5012SJeremy L Thompson 
1085ddae5012SJeremy L Thompson       CeedCallBackend(CeedOperatorFieldGetBasis(op_input_fields[i], &basis));
1086ddae5012SJeremy L Thompson       if (basis != CEED_BASIS_NONE) {
1087ddae5012SJeremy L Thompson         bool        is_tensor = true;
1088ddae5012SJeremy L Thompson         const char *resource;
1089ddae5012SJeremy L Thompson         char       *resource_root;
1090ddae5012SJeremy L Thompson         Ceed        basis_ceed;
1091ddae5012SJeremy L Thompson 
1092ddae5012SJeremy L Thompson         CeedCallBackend(CeedBasisIsTensor(basis, &is_tensor));
1093c9192acaSJeremy L Thompson         is_all_tensor    = is_all_tensor && is_tensor;
109445a787f7SJeremy L Thompson         is_all_nontensor = is_all_nontensor && !is_tensor;
1095ddae5012SJeremy L Thompson         CeedCallBackend(CeedBasisGetCeed(basis, &basis_ceed));
1096ddae5012SJeremy L Thompson         CeedCallBackend(CeedGetResource(basis_ceed, &resource));
1097ddae5012SJeremy L Thompson         CeedCallBackend(CeedGetResourceRoot(basis_ceed, resource, ":", &resource_root));
1098c9192acaSJeremy L Thompson         has_shared_bases = has_shared_bases && !strcmp(resource_root, "/gpu/cuda/shared");
1099ddae5012SJeremy L Thompson         CeedCallBackend(CeedFree(&resource_root));
1100ddae5012SJeremy L Thompson         CeedCallBackend(CeedDestroy(&basis_ceed));
1101ddae5012SJeremy L Thompson       }
1102ddae5012SJeremy L Thompson       CeedCallBackend(CeedBasisDestroy(&basis));
11034b3e95d5SJeremy L Thompson     }
1104ca735530SJeremy L Thompson 
1105ddae5012SJeremy L Thompson     for (CeedInt i = 0; i < num_output_fields; i++) {
1106ddae5012SJeremy L Thompson       CeedBasis basis;
1107ddae5012SJeremy L Thompson 
1108ddae5012SJeremy L Thompson       CeedCallBackend(CeedOperatorFieldGetBasis(op_output_fields[i], &basis));
1109ddae5012SJeremy L Thompson       if (basis != CEED_BASIS_NONE) {
1110ddae5012SJeremy L Thompson         bool        is_tensor = true;
1111ddae5012SJeremy L Thompson         const char *resource;
1112ddae5012SJeremy L Thompson         char       *resource_root;
1113ddae5012SJeremy L Thompson         Ceed        basis_ceed;
1114ddae5012SJeremy L Thompson 
1115ddae5012SJeremy L Thompson         CeedCallBackend(CeedBasisIsTensor(basis, &is_tensor));
1116c9192acaSJeremy L Thompson         is_all_tensor    = is_all_tensor && is_tensor;
1117c9192acaSJeremy L Thompson         is_all_nontensor = is_all_nontensor && !is_tensor;
1118ddae5012SJeremy L Thompson 
1119ddae5012SJeremy L Thompson         CeedCallBackend(CeedBasisGetCeed(basis, &basis_ceed));
1120ddae5012SJeremy L Thompson         CeedCallBackend(CeedGetResource(basis_ceed, &resource));
1121ddae5012SJeremy L Thompson         CeedCallBackend(CeedGetResourceRoot(basis_ceed, resource, ":", &resource_root));
1122c9192acaSJeremy L Thompson         has_shared_bases = has_shared_bases && !strcmp(resource_root, "/gpu/cuda/shared");
1123ddae5012SJeremy L Thompson         CeedCallBackend(CeedFree(&resource_root));
1124ddae5012SJeremy L Thompson         CeedCallBackend(CeedDestroy(&basis_ceed));
1125ddae5012SJeremy L Thompson       }
1126ddae5012SJeremy L Thompson       CeedCallBackend(CeedBasisDestroy(&basis));
1127ddae5012SJeremy L Thompson     }
1128ddae5012SJeremy L Thompson     // -- Fallback to ref if not all bases are shared
1129412e5683SJeremy L Thompson     if (!has_shared_bases) {
1130ddae5012SJeremy L Thompson       *is_good_build = false;
1131ddae5012SJeremy L Thompson       return CEED_ERROR_SUCCESS;
1132ddae5012SJeremy L Thompson     }
1133ddae5012SJeremy L Thompson   }
1134ca735530SJeremy L Thompson   CeedCallBackend(CeedOperatorGetCeed(op, &ceed));
1135ca735530SJeremy L Thompson   CeedCallBackend(CeedOperatorGetQFunction(op, &qf));
1136ca735530SJeremy L Thompson   CeedCallBackend(CeedQFunctionGetData(qf, &qf_data));
1137ca735530SJeremy L Thompson   CeedCallBackend(CeedQFunctionGetFields(qf, NULL, &qf_input_fields, NULL, &qf_output_fields));
1138241a4b83SYohann 
11394b3e95d5SJeremy L Thompson   // Get operator data
11408b97b69aSJeremy L Thompson   CeedCallBackend(CeedOperatorIsAtPoints(op, &is_at_points));
1141412e5683SJeremy L Thompson   {
1142*efa41df3SJeremy L Thompson     CeedInt max_P = 0, max_P_1d = 0;
1143412e5683SJeremy L Thompson 
1144412e5683SJeremy L Thompson     CeedCallBackend(CeedOperatorBuildKernelData_Cuda_gen(ceed, num_input_fields, op_input_fields, qf_input_fields, num_output_fields,
1145412e5683SJeremy L Thompson                                                          op_output_fields, qf_output_fields, &max_P, &max_P_1d, &Q, &Q_1d, &max_dim, &is_all_tensor,
1146412e5683SJeremy L Thompson                                                          &use_3d_slices));
1147412e5683SJeremy L Thompson     data->max_P_1d = is_all_tensor ? max_P_1d : max_P;
1148412e5683SJeremy L Thompson   }
1149412e5683SJeremy L Thompson   if (max_dim == 0) max_dim = 1;
1150412e5683SJeremy L Thompson   data->dim = max_dim;
11518b97b69aSJeremy L Thompson   if (is_at_points) {
11528b97b69aSJeremy L Thompson     CeedElemRestriction_Cuda *rstr_data;
11538b97b69aSJeremy L Thompson     CeedElemRestriction       rstr_points = NULL;
11544b3e95d5SJeremy L Thompson 
11558b97b69aSJeremy L Thompson     CeedCallBackend(CeedOperatorAtPointsGetPoints(op, &rstr_points, NULL));
11568b97b69aSJeremy L Thompson     CeedCallBackend(CeedElemRestrictionGetMaxPointsInElement(rstr_points, &max_num_points));
11578b97b69aSJeremy L Thompson     CeedCallBackend(CeedElemRestrictionGetCompStride(rstr_points, &coords_comp_stride));
11588b97b69aSJeremy L Thompson     CeedCallBackend(CeedElemRestrictionGetData(rstr_points, &rstr_data));
11598b97b69aSJeremy L Thompson     data->points.indices = (CeedInt *)rstr_data->d_offsets;
11608b97b69aSJeremy L Thompson     CeedCallBackend(CeedElemRestrictionDestroy(&rstr_points));
11618b97b69aSJeremy L Thompson   }
11628b97b69aSJeremy L Thompson   if (is_at_points) use_3d_slices = false;
11638b97b69aSJeremy L Thompson   if (Q_1d == 0) {
11648b97b69aSJeremy L Thompson     if (is_at_points) Q_1d = max_num_points;
11658b97b69aSJeremy L Thompson     else CeedCallBackend(CeedOperatorGetNumQuadraturePoints(op, &Q_1d));
11664b3e95d5SJeremy L Thompson   }
1167c433aabcSJeremy L Thompson   if (Q == 0) Q = Q_1d;
1168c433aabcSJeremy L Thompson   data->Q    = Q;
11694b3e95d5SJeremy L Thompson   data->Q_1d = Q_1d;
11704b3e95d5SJeremy L Thompson 
11710b454692Sjeremylt   // Check for restriction only identity operator
11724b3e95d5SJeremy L Thompson   {
11734b3e95d5SJeremy L Thompson     bool is_identity_qf;
11744b3e95d5SJeremy L Thompson 
11752b730f8bSJeremy L Thompson     CeedCallBackend(CeedQFunctionIsIdentity(qf, &is_identity_qf));
11760b454692Sjeremylt     if (is_identity_qf) {
11779e201c85SYohann       CeedEvalMode eval_mode_in, eval_mode_out;
1178ca735530SJeremy L Thompson 
11792b730f8bSJeremy L Thompson       CeedCallBackend(CeedQFunctionFieldGetEvalMode(qf_input_fields[0], &eval_mode_in));
11802b730f8bSJeremy L Thompson       CeedCallBackend(CeedQFunctionFieldGetEvalMode(qf_output_fields[0], &eval_mode_out));
11816574a04fSJeremy L Thompson       CeedCheck(eval_mode_in != CEED_EVAL_NONE || eval_mode_out != CEED_EVAL_NONE, ceed, CEED_ERROR_BACKEND,
11826574a04fSJeremy L Thompson                 "Backend does not implement restriction only identity operators");
11830b454692Sjeremylt     }
11844b3e95d5SJeremy L Thompson   }
11850b454692Sjeremylt 
1186f1a13f77SYohann Dudouit   // Add atomicAdd function for old NVidia architectures
11874b3e95d5SJeremy L Thompson   {
11884b3e95d5SJeremy L Thompson     Ceed_Cuda            *ceed_data;
11894b3e95d5SJeremy L Thompson     struct cudaDeviceProp prop;
11904b3e95d5SJeremy L Thompson 
11912b730f8bSJeremy L Thompson     CeedCallBackend(CeedGetData(ceed, &ceed_data));
11922b730f8bSJeremy L Thompson     CeedCallBackend(cudaGetDeviceProperties(&prop, ceed_data->device_id));
119380a9ef05SNatalie Beams     if ((prop.major < 6) && (CEED_SCALAR_TYPE != CEED_SCALAR_FP32)) {
11949c25dd66SJeremy L Thompson       code << "// AtomicAdd fallback source\n";
11959c25dd66SJeremy L Thompson       code << "#include <ceed/jit-source/cuda/cuda-atomic-add-fallback.h>\n\n";
1196f1a13f77SYohann Dudouit     }
11974b3e95d5SJeremy L Thompson   }
1198f1a13f77SYohann Dudouit 
11999e201c85SYohann   // Load basis source files
1200412e5683SJeremy L Thompson   if (!is_all_nontensor) {
12019c25dd66SJeremy L Thompson     code << "// Tensor basis source\n";
12029c25dd66SJeremy L Thompson     code << "#include <ceed/jit-source/cuda/cuda-shared-basis-tensor-templates.h>\n\n";
1203412e5683SJeremy L Thompson   }
1204412e5683SJeremy L Thompson   if (!is_all_tensor) {
1205dc007f05SJeremy L Thompson     code << "// Non-tensor basis source\n";
1206dc007f05SJeremy L Thompson     code << "#include <ceed/jit-source/cuda/cuda-shared-basis-nontensor-templates.h>\n\n";
1207dc007f05SJeremy L Thompson   }
1208c8e372f0SJeremy L Thompson   if (!is_all_tensor && !is_all_nontensor) {
1209c8e372f0SJeremy L Thompson     code << "// Tensor basis source\n";
1210c8e372f0SJeremy L Thompson     code << "#include <ceed/jit-source/cuda/cuda-shared-basis-tensor-flattened-templates.h>\n\n";
1211c8e372f0SJeremy L Thompson   }
1212dc007f05SJeremy L Thompson   if (is_at_points) {
12138b97b69aSJeremy L Thompson     code << "// AtPoints basis source\n";
12148b97b69aSJeremy L Thompson     code << "#include <ceed/jit-source/cuda/cuda-shared-basis-tensor-at-points-templates.h>\n\n";
1215dc007f05SJeremy L Thompson   }
12169c25dd66SJeremy L Thompson   code << "// CodeGen operator source\n";
12179c25dd66SJeremy L Thompson   code << "#include <ceed/jit-source/cuda/cuda-gen-templates.h>\n\n";
1218241a4b83SYohann 
12194b3e95d5SJeremy L Thompson   // Get QFunction name
12204b3e95d5SJeremy L Thompson   std::string qfunction_name(qf_data->qfunction_name);
12214b3e95d5SJeremy L Thompson   std::string operator_name;
12224b3e95d5SJeremy L Thompson 
122309095acaSJeremy L Thompson   operator_name = "CeedKernelCudaGenOperator_" + qfunction_name;
12244d537eeaSYohann 
12259e201c85SYohann   // Define CEED_Q_VLA
12269e201c85SYohann   code << "\n#undef CEED_Q_VLA\n";
1227412e5683SJeremy L Thompson   if (max_dim != 3 || is_at_points || use_3d_slices || !is_all_tensor) {
12289e201c85SYohann     code << "#define CEED_Q_VLA 1\n\n";
12299e201c85SYohann   } else {
12309e201c85SYohann     code << "#define CEED_Q_VLA " << Q_1d << "\n\n";
12319e201c85SYohann   }
12329e201c85SYohann 
12334b3e95d5SJeremy L Thompson   // Add user QFunction source
12344b3e95d5SJeremy L Thompson   {
12359c25dd66SJeremy L Thompson     const char *source_path;
12364b3e95d5SJeremy L Thompson 
12379c25dd66SJeremy L Thompson     CeedCallBackend(CeedQFunctionGetSourcePath(qf, &source_path));
12389c25dd66SJeremy L Thompson     CeedCheck(source_path, ceed, CEED_ERROR_UNSUPPORTED, "/gpu/cuda/gen backend requires QFunction source code file");
12399c25dd66SJeremy L Thompson 
12409c25dd66SJeremy L Thompson     code << "// User QFunction source\n";
12419c25dd66SJeremy L Thompson     code << "#include \"" << source_path << "\"\n\n";
12424b3e95d5SJeremy L Thompson   }
1243241a4b83SYohann 
1244241a4b83SYohann   // Setup
1245818e0025SJeremy L Thompson   code << "\n// -----------------------------------------------------------------------------\n";
12464b3e95d5SJeremy L Thompson   code << "// Operator Kernel\n";
12474b3e95d5SJeremy L Thompson   code << "// \n";
12484b3e95d5SJeremy L Thompson   code << "// d_[in,out]_i:   CeedVector device array\n";
12494b3e95d5SJeremy L Thompson   code << "// r_[in,out]_e_i: Element vector register\n";
12504b3e95d5SJeremy L Thompson   code << "// r_[in,out]_q_i: Quadrature space vector register\n";
12519123fb08SJeremy L Thompson   code << "// r_[in,out]_c_i: AtPoints Chebyshev coefficients register\n";
12524b3e95d5SJeremy L Thompson   code << "// r_[in,out]_s_i: Quadrature space slice vector register\n";
12534b3e95d5SJeremy L Thompson   code << "// \n";
12544b3e95d5SJeremy L Thompson   code << "// s_B_[in,out]_i: Interpolation matrix, shared memory\n";
12554b3e95d5SJeremy L Thompson   code << "// s_G_[in,out]_i: Gradient matrix, shared memory\n";
12564b3e95d5SJeremy L Thompson   code << "// -----------------------------------------------------------------------------\n";
12574b3e95d5SJeremy L Thompson   code << "extern \"C\" __global__ void " << operator_name
12588b97b69aSJeremy L Thompson        << "(CeedInt num_elem, void* ctx, FieldsInt_Cuda indices, Fields_Cuda fields, Fields_Cuda B, Fields_Cuda G, CeedScalar *W, Points_Cuda "
12598b97b69aSJeremy L Thompson           "points) {\n";
12604b3e95d5SJeremy L Thompson 
12614b3e95d5SJeremy L Thompson   // Scratch buffers
12629e201c85SYohann   for (CeedInt i = 0; i < num_input_fields; i++) {
12634b3e95d5SJeremy L Thompson     CeedEvalMode eval_mode;
12644b3e95d5SJeremy L Thompson 
12652b730f8bSJeremy L Thompson     CeedCallBackend(CeedQFunctionFieldGetEvalMode(qf_input_fields[i], &eval_mode));
12669e201c85SYohann     if (eval_mode != CEED_EVAL_WEIGHT) {  // Skip CEED_EVAL_WEIGHT
1267826538b3SJeremy L Thompson       code << "  const CeedScalar *__restrict__ d_in_" << i << " = fields.inputs[" << i << "];\n";
1268241a4b83SYohann     }
1269241a4b83SYohann   }
12709e201c85SYohann   for (CeedInt i = 0; i < num_output_fields; i++) {
1271826538b3SJeremy L Thompson     code << "  CeedScalar *__restrict__ d_out_" << i << " = fields.outputs[" << i << "];\n";
1272241a4b83SYohann   }
12731da99368SJeremy L Thompson 
1274412e5683SJeremy L Thompson   code << "  const CeedInt max_dim = " << max_dim << ";\n";
1275412e5683SJeremy L Thompson   if (!is_all_tensor) {
1276412e5683SJeremy L Thompson     code << "  const CeedInt Q = " << Q << ";\n";
1277412e5683SJeremy L Thompson   }
1278412e5683SJeremy L Thompson   if (!is_all_nontensor) {
1279412e5683SJeremy L Thompson     code << "  const CeedInt Q_1d = " << Q_1d << ";\n";
1280412e5683SJeremy L Thompson   }
12818b97b69aSJeremy L Thompson   if (is_at_points) {
12828b97b69aSJeremy L Thompson     code << "  const CeedInt max_num_points = " << max_num_points << ";\n";
12838b97b69aSJeremy L Thompson     code << "  const CeedInt coords_comp_stride = " << coords_comp_stride << ";\n";
12848b97b69aSJeremy L Thompson   }
12851da99368SJeremy L Thompson 
12864b3e95d5SJeremy L Thompson   // Shared data
1287241a4b83SYohann   code << "  extern __shared__ CeedScalar slice[];\n";
12889e201c85SYohann   code << "  SharedData_Cuda data;\n";
12899e201c85SYohann   code << "  data.t_id_x = threadIdx.x;\n";
12909e201c85SYohann   code << "  data.t_id_y = threadIdx.y;\n";
12919e201c85SYohann   code << "  data.t_id_z = threadIdx.z;\n";
12929e201c85SYohann   code << "  data.t_id  = threadIdx.x + threadIdx.y*blockDim.x + threadIdx.z*blockDim.y*blockDim.x;\n";
1293412e5683SJeremy L Thompson   code << "  data.slice = slice + data.t_id_z*OP_T_1D" << ((!is_all_tensor || max_dim == 1) ? "" : "*OP_T_1D") << ";\n";
1294920dcdc4Sjeremylt 
12950a2a6492SJeremy L Thompson   // -- Determine input mat reuse
129645a787f7SJeremy L Thompson   FieldReuse_Cuda input_matrix_reuse[CEED_FIELD_MAX];
12970a2a6492SJeremy L Thompson 
12980a2a6492SJeremy L Thompson   for (CeedInt i = 0; i < num_input_fields; i++) {
129945a787f7SJeremy L Thompson     input_matrix_reuse[i].index = -1;
13000a2a6492SJeremy L Thompson   }
13010a2a6492SJeremy L Thompson   for (CeedInt i = 0; i < num_input_fields; i++) {
1302412e5683SJeremy L Thompson     bool         is_tensor = true;
13030a2a6492SJeremy L Thompson     CeedEvalMode eval_mode_i;
13040a2a6492SJeremy L Thompson     CeedBasis    basis_i;
13050a2a6492SJeremy L Thompson 
13060a2a6492SJeremy L Thompson     CeedCallBackend(CeedQFunctionFieldGetEvalMode(qf_input_fields[i], &eval_mode_i));
13070a2a6492SJeremy L Thompson     if (eval_mode_i == CEED_EVAL_WEIGHT) continue;
13080a2a6492SJeremy L Thompson     CeedCallBackend(CeedOperatorFieldGetBasis(op_input_fields[i], &basis_i));
1309412e5683SJeremy L Thompson     CeedCallBackend(CeedBasisIsTensor(basis_i, &is_tensor));
131045a787f7SJeremy L Thompson     for (CeedInt j = 0; (input_matrix_reuse[i].index == -1) && (j < i); j++) {
13110a2a6492SJeremy L Thompson       CeedEvalMode eval_mode_j;
13120a2a6492SJeremy L Thompson       CeedBasis    basis_j;
13130a2a6492SJeremy L Thompson 
13140a2a6492SJeremy L Thompson       CeedCallBackend(CeedQFunctionFieldGetEvalMode(qf_input_fields[j], &eval_mode_j));
13150a2a6492SJeremy L Thompson       if (eval_mode_j == CEED_EVAL_WEIGHT) continue;
13160a2a6492SJeremy L Thompson       CeedCallBackend(CeedOperatorFieldGetBasis(op_input_fields[j], &basis_j));
13170a2a6492SJeremy L Thompson       if (basis_i == basis_j) {
13180a2a6492SJeremy L Thompson         if (is_tensor) {
131945a787f7SJeremy L Thompson           input_matrix_reuse[i].index     = j;
132045a787f7SJeremy L Thompson           input_matrix_reuse[i].is_input  = true;
132145a787f7SJeremy L Thompson           input_matrix_reuse[i].eval_mode = eval_mode_j;
13220a2a6492SJeremy L Thompson         } else {
13230a2a6492SJeremy L Thompson           // For non-tensor can only re-use with the same eval mode
13240a2a6492SJeremy L Thompson           if (eval_mode_i == eval_mode_j) {
132545a787f7SJeremy L Thompson             input_matrix_reuse[i].index     = j;
132645a787f7SJeremy L Thompson             input_matrix_reuse[i].is_input  = true;
132745a787f7SJeremy L Thompson             input_matrix_reuse[i].eval_mode = eval_mode_j;
13280a2a6492SJeremy L Thompson           }
13290a2a6492SJeremy L Thompson         }
13300a2a6492SJeremy L Thompson       }
13310a2a6492SJeremy L Thompson       CeedCallBackend(CeedBasisDestroy(&basis_j));
13320a2a6492SJeremy L Thompson     }
13330a2a6492SJeremy L Thompson     CeedCallBackend(CeedBasisDestroy(&basis_i));
13340a2a6492SJeremy L Thompson   }
13350a2a6492SJeremy L Thompson 
13360a2a6492SJeremy L Thompson   // -- Determine output mat reuse
133745a787f7SJeremy L Thompson   FieldReuse_Cuda output_matrix_reuse[CEED_FIELD_MAX];
13380a2a6492SJeremy L Thompson 
13390a2a6492SJeremy L Thompson   for (CeedInt i = 0; i < num_output_fields; i++) {
134045a787f7SJeremy L Thompson     output_matrix_reuse[i].index = -1;
13410a2a6492SJeremy L Thompson   }
13420a2a6492SJeremy L Thompson   for (CeedInt i = 0; i < num_output_fields; i++) {
1343412e5683SJeremy L Thompson     bool         is_tensor = true;
13440a2a6492SJeremy L Thompson     CeedEvalMode eval_mode_i;
13450a2a6492SJeremy L Thompson     CeedBasis    basis_i;
13460a2a6492SJeremy L Thompson 
13470a2a6492SJeremy L Thompson     CeedCallBackend(CeedQFunctionFieldGetEvalMode(qf_output_fields[i], &eval_mode_i));
13480a2a6492SJeremy L Thompson     CeedCallBackend(CeedOperatorFieldGetBasis(op_output_fields[i], &basis_i));
1349412e5683SJeremy L Thompson     CeedCallBackend(CeedBasisIsTensor(basis_i, &is_tensor));
135045a787f7SJeremy L Thompson     for (CeedInt j = 0; (output_matrix_reuse[i].index == -1) && (j < num_input_fields); j++) {
13510a2a6492SJeremy L Thompson       CeedEvalMode eval_mode_j;
13520a2a6492SJeremy L Thompson       CeedBasis    basis_j;
13530a2a6492SJeremy L Thompson 
13540a2a6492SJeremy L Thompson       CeedCallBackend(CeedQFunctionFieldGetEvalMode(qf_input_fields[j], &eval_mode_j));
13550a2a6492SJeremy L Thompson       if (eval_mode_j == CEED_EVAL_WEIGHT) continue;
13560a2a6492SJeremy L Thompson       CeedCallBackend(CeedOperatorFieldGetBasis(op_input_fields[j], &basis_j));
13570a2a6492SJeremy L Thompson       if (basis_i == basis_j) {
13580a2a6492SJeremy L Thompson         if (is_tensor) {
135945a787f7SJeremy L Thompson           output_matrix_reuse[i].index     = j;
136045a787f7SJeremy L Thompson           output_matrix_reuse[i].is_input  = true;
136145a787f7SJeremy L Thompson           output_matrix_reuse[i].eval_mode = eval_mode_j;
13620a2a6492SJeremy L Thompson         } else {
13630a2a6492SJeremy L Thompson           // For non-tensor can only re-use with the same eval mode
13640a2a6492SJeremy L Thompson           if (eval_mode_i == eval_mode_j) {
136545a787f7SJeremy L Thompson             output_matrix_reuse[i].index     = j;
136645a787f7SJeremy L Thompson             output_matrix_reuse[i].is_input  = true;
136745a787f7SJeremy L Thompson             output_matrix_reuse[i].eval_mode = eval_mode_j;
13680a2a6492SJeremy L Thompson           }
13690a2a6492SJeremy L Thompson         }
13700a2a6492SJeremy L Thompson       }
13710a2a6492SJeremy L Thompson       CeedCallBackend(CeedBasisDestroy(&basis_j));
13720a2a6492SJeremy L Thompson     }
137345a787f7SJeremy L Thompson     for (CeedInt j = 0; (output_matrix_reuse[i].index == -1) && (j < i); j++) {
13740a2a6492SJeremy L Thompson       CeedEvalMode eval_mode_j;
13750a2a6492SJeremy L Thompson       CeedBasis    basis_j;
13760a2a6492SJeremy L Thompson 
13770a2a6492SJeremy L Thompson       CeedCallBackend(CeedQFunctionFieldGetEvalMode(qf_output_fields[j], &eval_mode_j));
13780a2a6492SJeremy L Thompson       if (eval_mode_j == CEED_EVAL_WEIGHT) continue;
13790a2a6492SJeremy L Thompson       CeedCallBackend(CeedOperatorFieldGetBasis(op_output_fields[j], &basis_j));
13800a2a6492SJeremy L Thompson       if (basis_i == basis_j) {
13810a2a6492SJeremy L Thompson         if (is_tensor) {
138245a787f7SJeremy L Thompson           output_matrix_reuse[i].index     = j;
138345a787f7SJeremy L Thompson           output_matrix_reuse[i].is_input  = false;
138445a787f7SJeremy L Thompson           output_matrix_reuse[i].eval_mode = eval_mode_j;
13850a2a6492SJeremy L Thompson         } else {
13860a2a6492SJeremy L Thompson           // For non-tensor can only re-use with the same eval mode
13870a2a6492SJeremy L Thompson           if (eval_mode_i == eval_mode_j) {
138845a787f7SJeremy L Thompson             output_matrix_reuse[i].index     = j;
138945a787f7SJeremy L Thompson             output_matrix_reuse[i].is_input  = false;
139045a787f7SJeremy L Thompson             output_matrix_reuse[i].eval_mode = eval_mode_j;
13910a2a6492SJeremy L Thompson           }
13920a2a6492SJeremy L Thompson         }
13930a2a6492SJeremy L Thompson       }
13940a2a6492SJeremy L Thompson       CeedCallBackend(CeedBasisDestroy(&basis_j));
13950a2a6492SJeremy L Thompson     }
13960a2a6492SJeremy L Thompson     CeedCallBackend(CeedBasisDestroy(&basis_i));
13970a2a6492SJeremy L Thompson   }
13980a2a6492SJeremy L Thompson 
1399ac421f39SYohann   // Initialize constants, and matrices B and G
14004b3e95d5SJeremy L Thompson   code << "\n  // Input field constants and basis data\n";
14019e201c85SYohann   for (CeedInt i = 0; i < num_input_fields; i++) {
14028014c5e7SJeremy L Thompson     CeedCallBackend(CeedOperatorBuildKernelFieldData_Cuda_gen(code, data, i, op_input_fields[i], qf_input_fields[i], input_matrix_reuse[i], max_dim,
14038014c5e7SJeremy L Thompson                                                               Q, Q_1d, true, is_all_tensor, is_at_points, use_3d_slices));
1404920dcdc4Sjeremylt   }
14054b3e95d5SJeremy L Thompson   code << "\n  // Output field constants and basis data\n";
14069e201c85SYohann   for (CeedInt i = 0; i < num_output_fields; i++) {
14078014c5e7SJeremy L Thompson     CeedCallBackend(CeedOperatorBuildKernelFieldData_Cuda_gen(code, data, i, op_output_fields[i], qf_output_fields[i], output_matrix_reuse[i],
14088014c5e7SJeremy L Thompson                                                               max_dim, Q, Q_1d, false, is_all_tensor, is_at_points, use_3d_slices));
14094b3e95d5SJeremy L Thompson   }
1410920dcdc4Sjeremylt 
14114b3e95d5SJeremy L Thompson   // Loop over all elements
14124b3e95d5SJeremy L Thompson   code << "\n  // Element loop\n";
1413ac421f39SYohann   code << "  __syncthreads();\n";
14149e201c85SYohann   code << "  for (CeedInt elem = blockIdx.x*blockDim.z + threadIdx.z; elem < num_elem; elem += gridDim.x*blockDim.z) {\n";
14154b3e95d5SJeremy L Thompson 
1416e93651e5SJeremy L Thompson   // -- Compute minimum buffer space needed
14178b97b69aSJeremy L Thompson   CeedInt max_rstr_buffer_size = 1;
1418e93651e5SJeremy L Thompson 
14199e201c85SYohann   for (CeedInt i = 0; i < num_input_fields; i++) {
1420e93651e5SJeremy L Thompson     CeedInt             num_comp, elem_size;
1421e93651e5SJeremy L Thompson     CeedElemRestriction elem_rstr;
1422e93651e5SJeremy L Thompson 
1423e93651e5SJeremy L Thompson     CeedCallBackend(CeedOperatorFieldGetElemRestriction(op_input_fields[i], &elem_rstr));
1424e93651e5SJeremy L Thompson     CeedCallBackend(CeedElemRestrictionGetNumComponents(elem_rstr, &num_comp));
1425e93651e5SJeremy L Thompson     CeedCallBackend(CeedElemRestrictionGetElementSize(elem_rstr, &elem_size));
1426412e5683SJeremy L Thompson     max_rstr_buffer_size = CeedIntMax(max_rstr_buffer_size, num_comp * (is_all_tensor && (max_dim >= 3) ? elem_size : 1));
1427681d0ea7SJeremy L Thompson     CeedCallBackend(CeedElemRestrictionDestroy(&elem_rstr));
1428e93651e5SJeremy L Thompson   }
1429e93651e5SJeremy L Thompson   for (CeedInt i = 0; i < num_output_fields; i++) {
1430e93651e5SJeremy L Thompson     CeedInt             num_comp, elem_size;
1431e93651e5SJeremy L Thompson     CeedElemRestriction elem_rstr;
1432e93651e5SJeremy L Thompson 
1433e93651e5SJeremy L Thompson     CeedCallBackend(CeedOperatorFieldGetElemRestriction(op_output_fields[i], &elem_rstr));
1434e93651e5SJeremy L Thompson     CeedCallBackend(CeedElemRestrictionGetNumComponents(elem_rstr, &num_comp));
1435e93651e5SJeremy L Thompson     CeedCallBackend(CeedElemRestrictionGetElementSize(elem_rstr, &elem_size));
1436412e5683SJeremy L Thompson     max_rstr_buffer_size = CeedIntMax(max_rstr_buffer_size, num_comp * (is_all_tensor && (max_dim >= 3) ? elem_size : 1));
1437681d0ea7SJeremy L Thompson     CeedCallBackend(CeedElemRestrictionDestroy(&elem_rstr));
1438e93651e5SJeremy L Thompson   }
1439e93651e5SJeremy L Thompson   code << "    // Scratch restriction buffer space\n";
1440e93651e5SJeremy L Thompson   code << "    CeedScalar r_e_scratch[" << max_rstr_buffer_size << "];\n";
1441e93651e5SJeremy L Thompson 
1442e93651e5SJeremy L Thompson   // -- Determine best input field processing order
1443e93651e5SJeremy L Thompson   CeedInt field_rstr_in_buffer[CEED_FIELD_MAX], input_field_order[CEED_FIELD_MAX];
1444e93651e5SJeremy L Thompson 
1445e93651e5SJeremy L Thompson   for (CeedInt i = 0; i < num_input_fields; i++) {
1446e93651e5SJeremy L Thompson     field_rstr_in_buffer[i] = -1;
1447e93651e5SJeremy L Thompson     input_field_order[i]    = -1;
1448e93651e5SJeremy L Thompson   }
1449e93651e5SJeremy L Thompson   {
1450e93651e5SJeremy L Thompson     bool    is_ordered[CEED_FIELD_MAX];
1451e93651e5SJeremy L Thompson     CeedInt curr_index = 0;
1452e93651e5SJeremy L Thompson 
1453e93651e5SJeremy L Thompson     for (CeedInt i = 0; i < num_input_fields; i++) is_ordered[i] = false;
1454e93651e5SJeremy L Thompson     for (CeedInt i = 0; i < num_input_fields; i++) {
1455e93651e5SJeremy L Thompson       CeedVector          vec_i;
1456e93651e5SJeremy L Thompson       CeedElemRestriction rstr_i;
1457e93651e5SJeremy L Thompson 
1458e93651e5SJeremy L Thompson       if (is_ordered[i]) continue;
1459e93651e5SJeremy L Thompson       field_rstr_in_buffer[i]       = i;
1460e93651e5SJeremy L Thompson       is_ordered[i]                 = true;
1461e93651e5SJeremy L Thompson       input_field_order[curr_index] = i;
1462e93651e5SJeremy L Thompson       curr_index++;
1463034f99fdSJeremy L Thompson       CeedCallBackend(CeedOperatorFieldGetVector(op_input_fields[i], &vec_i));
1464e93651e5SJeremy L Thompson       if (vec_i == CEED_VECTOR_NONE) continue;  // CEED_EVAL_WEIGHT
1465e93651e5SJeremy L Thompson       CeedCallBackend(CeedOperatorFieldGetElemRestriction(op_input_fields[i], &rstr_i));
1466e93651e5SJeremy L Thompson       for (CeedInt j = i + 1; j < num_input_fields; j++) {
1467e93651e5SJeremy L Thompson         CeedVector          vec_j;
1468e93651e5SJeremy L Thompson         CeedElemRestriction rstr_j;
1469e93651e5SJeremy L Thompson 
1470e93651e5SJeremy L Thompson         CeedCallBackend(CeedOperatorFieldGetVector(op_input_fields[j], &vec_j));
1471e93651e5SJeremy L Thompson         CeedCallBackend(CeedOperatorFieldGetElemRestriction(op_input_fields[j], &rstr_j));
1472e93651e5SJeremy L Thompson         if (rstr_i == rstr_j && vec_i == vec_j) {
1473e93651e5SJeremy L Thompson           field_rstr_in_buffer[j]       = i;
1474e93651e5SJeremy L Thompson           is_ordered[j]                 = true;
1475e93651e5SJeremy L Thompson           input_field_order[curr_index] = j;
1476e93651e5SJeremy L Thompson           curr_index++;
1477e93651e5SJeremy L Thompson         }
1478681d0ea7SJeremy L Thompson         CeedCallBackend(CeedVectorDestroy(&vec_j));
1479681d0ea7SJeremy L Thompson         CeedCallBackend(CeedElemRestrictionDestroy(&rstr_j));
1480e93651e5SJeremy L Thompson       }
1481681d0ea7SJeremy L Thompson       CeedCallBackend(CeedVectorDestroy(&vec_i));
1482681d0ea7SJeremy L Thompson       CeedCallBackend(CeedElemRestrictionDestroy(&rstr_i));
1483e93651e5SJeremy L Thompson     }
1484e93651e5SJeremy L Thompson   }
1485e93651e5SJeremy L Thompson 
1486e93651e5SJeremy L Thompson   // -- Input restriction and basis
1487e93651e5SJeremy L Thompson   code << "\n    // -- Input field restrictions and basis actions\n";
1488e93651e5SJeremy L Thompson   for (CeedInt i = 0; i < num_input_fields; i++) {
148959fa3f92SJeremy L Thompson     const char   *field_name;
149059fa3f92SJeremy L Thompson     const CeedInt f = input_field_order[i];
1491e93651e5SJeremy L Thompson 
149259fa3f92SJeremy L Thompson     CeedCallBackend(CeedOperatorFieldGetName(op_input_fields[f], &field_name));
149359fa3f92SJeremy L Thompson     code << "    // ---- Input field " << f << ": " << field_name << "\n";
1494920dcdc4Sjeremylt 
14954b3e95d5SJeremy L Thompson     // ---- Restriction
14968014c5e7SJeremy L Thompson     CeedCallBackend(CeedOperatorBuildKernelRestriction_Cuda_gen(code, data, f, field_rstr_in_buffer, op_input_fields[f], qf_input_fields[f], max_dim,
1497412e5683SJeremy L Thompson                                                                 Q_1d, true, is_all_tensor, is_at_points, use_3d_slices));
14989a2291e3SJeremy L Thompson 
14994b3e95d5SJeremy L Thompson     // ---- Basis action
15008014c5e7SJeremy L Thompson     CeedCallBackend(CeedOperatorBuildKernelBasis_Cuda_gen(code, data, f, op_input_fields[f], qf_input_fields[f], max_dim, Q_1d, true, is_all_tensor,
1501dc007f05SJeremy L Thompson                                                           is_at_points, use_3d_slices));
1502920dcdc4Sjeremylt   }
1503920dcdc4Sjeremylt 
15044b3e95d5SJeremy L Thompson   // -- Q function
1505412e5683SJeremy L Thompson   CeedCallBackend(CeedOperatorBuildKernelQFunction_Cuda_gen(code, data, max_dim, max_num_points, num_input_fields, op_input_fields, qf_input_fields,
1506412e5683SJeremy L Thompson                                                             num_output_fields, op_output_fields, qf_output_fields, qfunction_name, Q_1d,
1507412e5683SJeremy L Thompson                                                             is_all_tensor, is_at_points, use_3d_slices));
1508ca735530SJeremy L Thompson 
15094b3e95d5SJeremy L Thompson   // -- Output basis and restriction
15104b3e95d5SJeremy L Thompson   code << "\n    // -- Output field basis action and restrictions\n";
15119e201c85SYohann   for (CeedInt i = 0; i < num_output_fields; i++) {
151259fa3f92SJeremy L Thompson     const char *field_name;
151359fa3f92SJeremy L Thompson 
151459fa3f92SJeremy L Thompson     CeedCallBackend(CeedOperatorFieldGetName(op_output_fields[i], &field_name));
151559fa3f92SJeremy L Thompson     code << "    // ---- Output field " << i << ": " << field_name << "\n";
1516ca735530SJeremy L Thompson 
15174b3e95d5SJeremy L Thompson     // ---- Basis action
15188014c5e7SJeremy L Thompson     CeedCallBackend(CeedOperatorBuildKernelBasis_Cuda_gen(code, data, i, op_output_fields[i], qf_output_fields[i], max_dim, Q_1d, false,
1519412e5683SJeremy L Thompson                                                           is_all_tensor, is_at_points, use_3d_slices));
15209a2291e3SJeremy L Thompson 
15214b3e95d5SJeremy L Thompson     // ---- Restriction
15228014c5e7SJeremy L Thompson     CeedCallBackend(CeedOperatorBuildKernelRestriction_Cuda_gen(code, data, i, NULL, op_output_fields[i], qf_output_fields[i], max_dim, Q_1d, false,
1523412e5683SJeremy L Thompson                                                                 is_all_tensor, is_at_points, use_3d_slices));
1524ac421f39SYohann   }
1525241a4b83SYohann 
15264b3e95d5SJeremy L Thompson   // Close loop and function
1527241a4b83SYohann   code << "  }\n";
1528818e0025SJeremy L Thompson   code << "}\n";
1529818e0025SJeremy L Thompson   code << "// -----------------------------------------------------------------------------\n\n";
1530241a4b83SYohann 
15313a2968d6SJeremy L Thompson   // Compile
1532ddae5012SJeremy L Thompson   {
1533ddae5012SJeremy L Thompson     bool          is_compile_good = false;
1534412e5683SJeremy L Thompson     const CeedInt T_1d            = CeedIntMax(is_all_tensor ? Q_1d : Q, data->max_P_1d);
1535ddae5012SJeremy L Thompson 
1536412e5683SJeremy L Thompson     CeedCallBackend(CeedTryCompile_Cuda(ceed, code.str().c_str(), &is_compile_good, &data->module, 1, "OP_T_1D", T_1d));
1537ddae5012SJeremy L Thompson     if (is_compile_good) {
1538ddae5012SJeremy L Thompson       *is_good_build = true;
1539eb7e6cafSJeremy L Thompson       CeedCallBackend(CeedGetKernel_Cuda(ceed, data->module, operator_name.c_str(), &data->op));
1540ddae5012SJeremy L Thompson     } else {
1541ddae5012SJeremy L Thompson       *is_good_build     = false;
1542ddae5012SJeremy L Thompson       data->use_fallback = true;
1543ddae5012SJeremy L Thompson     }
1544ddae5012SJeremy L Thompson   }
15452b730f8bSJeremy L Thompson   CeedCallBackend(CeedOperatorSetSetupDone(op));
15469bc66399SJeremy L Thompson   CeedCallBackend(CeedDestroy(&ceed));
1547c11e12f4SJeremy L Thompson   CeedCallBackend(CeedQFunctionDestroy(&qf));
1548e15f9bd0SJeremy L Thompson   return CEED_ERROR_SUCCESS;
1549241a4b83SYohann }
15502a86cc9dSSebastian Grimberg 
1551ab213215SJeremy L Thompson //------------------------------------------------------------------------------
1552