xref: /libCEED/backends/cuda-gen/ceed-cuda-gen-operator-build.cpp (revision 59fa3f92dbea5a7e09ccfccd0c5c76c4fcbd3373)
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,
364b3e95d5SJeremy L Thompson                                                 CeedQFunctionField *qf_output_fields, CeedInt *max_P_1d, CeedInt *Q_1d, CeedInt *dim, bool *is_tensor,
374b3e95d5SJeremy L Thompson                                                 bool *use_3d_slices) {
384b3e95d5SJeremy L Thompson   // Find dim, P_1d, Q_1d
394b3e95d5SJeremy L Thompson   *max_P_1d  = 0;
404b3e95d5SJeremy L Thompson   *Q_1d      = 0;
414b3e95d5SJeremy L Thompson   *dim       = 0;
424b3e95d5SJeremy L Thompson   *is_tensor = true;
43dc007f05SJeremy L Thompson 
444b3e95d5SJeremy L Thompson   for (CeedInt i = 0; i < num_input_fields; i++) {
454b3e95d5SJeremy L Thompson     CeedBasis basis;
464b3e95d5SJeremy L Thompson 
474b3e95d5SJeremy L Thompson     CeedCallBackend(CeedOperatorFieldGetBasis(op_input_fields[i], &basis));
484b3e95d5SJeremy L Thompson     if (basis != CEED_BASIS_NONE) {
494b3e95d5SJeremy L Thompson       bool    is_field_tensor;
504b3e95d5SJeremy L Thompson       CeedInt field_P_1d = 0, field_Q_1d = 0, field_dim = 0;
514b3e95d5SJeremy L Thompson 
524b3e95d5SJeremy L Thompson       // Collect dim, P_1d, and Q_1d
534b3e95d5SJeremy L Thompson       CeedCallBackend(CeedBasisIsTensor(basis, &is_field_tensor));
544b3e95d5SJeremy L Thompson       *is_tensor = *is_tensor && is_field_tensor;
55dc007f05SJeremy L Thompson       if (is_field_tensor) CeedCallBackend(CeedBasisGetNumNodes1D(basis, &field_P_1d));
56dc007f05SJeremy L Thompson       else CeedCallBackend(CeedBasisGetNumNodes(basis, &field_P_1d));
574b3e95d5SJeremy L Thompson       *max_P_1d = CeedIntMax(*max_P_1d, field_P_1d);
584b3e95d5SJeremy L Thompson       CeedCallBackend(CeedBasisGetDimension(basis, &field_dim));
594b3e95d5SJeremy L Thompson       CeedCheck(*dim == 0 || field_dim == *dim, ceed, CEED_ERROR_BACKEND, "Quadrature spaces must be compatible");
604b3e95d5SJeremy L Thompson       *dim = field_dim;
61dc007f05SJeremy L Thompson       if (is_field_tensor) CeedCallBackend(CeedBasisGetNumQuadraturePoints1D(basis, &field_Q_1d));
62dc007f05SJeremy L Thompson       else CeedCallBackend(CeedBasisGetNumQuadraturePoints(basis, &field_Q_1d));
634b3e95d5SJeremy L Thompson       CeedCheck(*Q_1d == 0 || field_Q_1d == *Q_1d, ceed, CEED_ERROR_BACKEND, "Quadrature spaces must be compatible");
644b3e95d5SJeremy L Thompson       *Q_1d = field_Q_1d;
654b3e95d5SJeremy L Thompson     }
66681d0ea7SJeremy L Thompson     CeedCallBackend(CeedBasisDestroy(&basis));
674b3e95d5SJeremy L Thompson   }
684b3e95d5SJeremy L Thompson   for (CeedInt i = 0; i < num_output_fields; i++) {
694b3e95d5SJeremy L Thompson     CeedBasis basis;
704b3e95d5SJeremy L Thompson 
714b3e95d5SJeremy L Thompson     CeedCallBackend(CeedOperatorFieldGetBasis(op_output_fields[i], &basis));
724b3e95d5SJeremy L Thompson     if (basis != CEED_BASIS_NONE) {
734b3e95d5SJeremy L Thompson       bool    is_field_tensor;
744b3e95d5SJeremy L Thompson       CeedInt field_P_1d = 0, field_Q_1d = 0, field_dim = 0;
754b3e95d5SJeremy L Thompson 
764b3e95d5SJeremy L Thompson       // Collect dim, P_1d, and Q_1d
774b3e95d5SJeremy L Thompson       CeedCallBackend(CeedBasisIsTensor(basis, &is_field_tensor));
784b3e95d5SJeremy L Thompson       *is_tensor = *is_tensor && is_field_tensor;
79dc007f05SJeremy L Thompson       if (is_field_tensor) CeedCallBackend(CeedBasisGetNumNodes1D(basis, &field_P_1d));
80dc007f05SJeremy L Thompson       else CeedCallBackend(CeedBasisGetNumNodes(basis, &field_P_1d));
814b3e95d5SJeremy L Thompson       *max_P_1d = CeedIntMax(*max_P_1d, field_P_1d);
824b3e95d5SJeremy L Thompson       CeedCallBackend(CeedBasisGetDimension(basis, &field_dim));
834b3e95d5SJeremy L Thompson       CeedCheck(*dim == 0 || field_dim == *dim, ceed, CEED_ERROR_BACKEND, "Quadrature spaces must be compatible");
844b3e95d5SJeremy L Thompson       *dim = field_dim;
85dc007f05SJeremy L Thompson       if (is_field_tensor) CeedCallBackend(CeedBasisGetNumQuadraturePoints1D(basis, &field_Q_1d));
86dc007f05SJeremy L Thompson       else CeedCallBackend(CeedBasisGetNumQuadraturePoints(basis, &field_Q_1d));
874b3e95d5SJeremy L Thompson       CeedCheck(*Q_1d == 0 || field_Q_1d == *Q_1d, ceed, CEED_ERROR_BACKEND, "Quadrature spaces must be compatible");
884b3e95d5SJeremy L Thompson       *Q_1d = field_Q_1d;
894b3e95d5SJeremy L Thompson     }
90681d0ea7SJeremy L Thompson     CeedCallBackend(CeedBasisDestroy(&basis));
914b3e95d5SJeremy L Thompson   }
924b3e95d5SJeremy L Thompson 
934b3e95d5SJeremy L Thompson   // Only use 3D collocated gradient parallelization strategy when gradient is computed
944b3e95d5SJeremy L Thompson   *use_3d_slices = false;
954b3e95d5SJeremy L Thompson   if (*dim == 3) {
964b3e95d5SJeremy L Thompson     bool was_grad_found = false;
974b3e95d5SJeremy L Thompson 
984b3e95d5SJeremy L Thompson     for (CeedInt i = 0; i < num_input_fields; i++) {
994b3e95d5SJeremy L Thompson       CeedEvalMode eval_mode;
1004b3e95d5SJeremy L Thompson 
1014b3e95d5SJeremy L Thompson       CeedCallBackend(CeedQFunctionFieldGetEvalMode(qf_input_fields[i], &eval_mode));
1024b3e95d5SJeremy L Thompson       if (eval_mode == CEED_EVAL_GRAD) {
1034b3e95d5SJeremy L Thompson         CeedBasis_Cuda_shared *basis_data;
1044b3e95d5SJeremy L Thompson         CeedBasis              basis;
1054b3e95d5SJeremy L Thompson 
1064b3e95d5SJeremy L Thompson         CeedCallBackend(CeedOperatorFieldGetBasis(op_input_fields[i], &basis));
1074b3e95d5SJeremy L Thompson         CeedCallBackend(CeedBasisGetData(basis, &basis_data));
1084b3e95d5SJeremy L Thompson         *use_3d_slices = basis_data->d_collo_grad_1d && (was_grad_found ? *use_3d_slices : true);
1094b3e95d5SJeremy L Thompson         was_grad_found = true;
110681d0ea7SJeremy L Thompson         CeedCallBackend(CeedBasisDestroy(&basis));
1114b3e95d5SJeremy L Thompson       }
1124b3e95d5SJeremy L Thompson     }
1134b3e95d5SJeremy L Thompson     for (CeedInt i = 0; i < num_output_fields; i++) {
1144b3e95d5SJeremy L Thompson       CeedEvalMode eval_mode;
1154b3e95d5SJeremy L Thompson 
1164b3e95d5SJeremy L Thompson       CeedCallBackend(CeedQFunctionFieldGetEvalMode(qf_output_fields[i], &eval_mode));
1174b3e95d5SJeremy L Thompson       if (eval_mode == CEED_EVAL_GRAD) {
1184b3e95d5SJeremy L Thompson         CeedBasis_Cuda_shared *basis_data;
1194b3e95d5SJeremy L Thompson         CeedBasis              basis;
1204b3e95d5SJeremy L Thompson 
1214b3e95d5SJeremy L Thompson         CeedCallBackend(CeedOperatorFieldGetBasis(op_output_fields[i], &basis));
1224b3e95d5SJeremy L Thompson         CeedCallBackend(CeedBasisGetData(basis, &basis_data));
1234b3e95d5SJeremy L Thompson         *use_3d_slices = basis_data->d_collo_grad_1d && (was_grad_found ? *use_3d_slices : true);
1244b3e95d5SJeremy L Thompson         was_grad_found = true;
125681d0ea7SJeremy L Thompson         CeedCallBackend(CeedBasisDestroy(&basis));
1264b3e95d5SJeremy L Thompson       }
1274b3e95d5SJeremy L Thompson     }
1284b3e95d5SJeremy L Thompson   }
1294b3e95d5SJeremy L Thompson   return CEED_ERROR_SUCCESS;
1304b3e95d5SJeremy L Thompson }
1314b3e95d5SJeremy L Thompson 
1324b3e95d5SJeremy L Thompson //------------------------------------------------------------------------------
1334b3e95d5SJeremy L Thompson // Setup fields
1344b3e95d5SJeremy L Thompson //------------------------------------------------------------------------------
1354b3e95d5SJeremy L Thompson static int CeedOperatorBuildKernelFieldData_Cuda_gen(std::ostringstream &code, CeedOperator_Cuda_gen *data, CeedInt i, CeedOperatorField op_field,
13645a787f7SJeremy L Thompson                                                      CeedQFunctionField qf_field, FieldReuse_Cuda field_reuse, CeedInt Q_1d, bool is_input,
13745a787f7SJeremy L Thompson                                                      bool is_tensor, bool is_at_points, bool use_3d_slices) {
138*59fa3f92SJeremy L Thompson   const char            *field_name;
1394b3e95d5SJeremy L Thompson   std::string            var_suffix = (is_input ? "_in_" : "_out_") + std::to_string(i);
140dc007f05SJeremy L Thompson   std::string            P_name = (is_tensor ? "P_1d" : "P") + var_suffix, Q_name = is_tensor ? "Q_1d" : "Q";
1414b3e95d5SJeremy L Thompson   std::string            option_name = (is_input ? "inputs" : "outputs");
1424b3e95d5SJeremy L Thompson   CeedEvalMode           eval_mode   = CEED_EVAL_NONE;
1434b3e95d5SJeremy L Thompson   CeedInt                elem_size = 0, num_comp = 0, P_1d = 0;
1444b3e95d5SJeremy L Thompson   CeedElemRestriction    elem_rstr;
1454b3e95d5SJeremy L Thompson   CeedBasis_Cuda_shared *basis_data;
1464b3e95d5SJeremy L Thompson   CeedBasis              basis;
1474b3e95d5SJeremy L Thompson 
1480a2a6492SJeremy L Thompson   // Field reuse info
14945a787f7SJeremy L Thompson   bool use_previous_field = field_reuse.index != -1;
1500a2a6492SJeremy L Thompson 
151*59fa3f92SJeremy L Thompson   CeedCallBackend(CeedOperatorFieldGetName(op_field, &field_name));
152*59fa3f92SJeremy L Thompson   code << "  // -- " << (is_input ? "Input" : "Output") << " field " << i << ": " << field_name << "\n";
1534b3e95d5SJeremy L Thompson 
1544b3e95d5SJeremy L Thompson   // Get field data
1554b3e95d5SJeremy L Thompson   CeedCallBackend(CeedOperatorFieldGetElemRestriction(op_field, &elem_rstr));
1564b3e95d5SJeremy L Thompson   if (elem_rstr != CEED_ELEMRESTRICTION_NONE) {
1574b3e95d5SJeremy L Thompson     CeedCallBackend(CeedElemRestrictionGetElementSize(elem_rstr, &elem_size));
1584b3e95d5SJeremy L Thompson     CeedCallBackend(CeedElemRestrictionGetNumComponents(elem_rstr, &num_comp));
1594b3e95d5SJeremy L Thompson   }
160681d0ea7SJeremy L Thompson   CeedCallBackend(CeedElemRestrictionDestroy(&elem_rstr));
1614b3e95d5SJeremy L Thompson   CeedCallBackend(CeedOperatorFieldGetBasis(op_field, &basis));
1624b3e95d5SJeremy L Thompson   if (basis != CEED_BASIS_NONE) {
1634b3e95d5SJeremy L Thompson     CeedCallBackend(CeedBasisGetData(basis, &basis_data));
164dc007f05SJeremy L Thompson     if (is_tensor) CeedCallBackend(CeedBasisGetNumNodes1D(basis, &P_1d));
165dc007f05SJeremy L Thompson     else CeedCallBackend(CeedBasisGetNumNodes(basis, &P_1d));
1664b3e95d5SJeremy L Thompson   }
1674b3e95d5SJeremy L Thompson   CeedCallBackend(CeedQFunctionFieldGetEvalMode(qf_field, &eval_mode));
1684b3e95d5SJeremy L Thompson 
1694b3e95d5SJeremy L Thompson   // Set field constants
1704b3e95d5SJeremy L Thompson   if (eval_mode != CEED_EVAL_WEIGHT) {
1714b3e95d5SJeremy L Thompson     code << "  const CeedInt " << P_name << " = " << (basis == CEED_BASIS_NONE ? Q_1d : P_1d) << ";\n";
1724b3e95d5SJeremy L Thompson     code << "  const CeedInt num_comp" << var_suffix << " = " << num_comp << ";\n";
1734b3e95d5SJeremy L Thompson   }
1744b3e95d5SJeremy L Thompson 
1754b3e95d5SJeremy L Thompson   // Load basis data
1764b3e95d5SJeremy L Thompson   code << "  // EvalMode: " << CeedEvalModes[eval_mode] << "\n";
1774b3e95d5SJeremy L Thompson   switch (eval_mode) {
1784b3e95d5SJeremy L Thompson     case CEED_EVAL_NONE:
1794b3e95d5SJeremy L Thompson       break;
1804b3e95d5SJeremy L Thompson     case CEED_EVAL_INTERP:
1818b97b69aSJeremy L Thompson       if (is_at_points) {
1828b97b69aSJeremy L Thompson         // AtPoints
1838b97b69aSJeremy L Thompson         if (!basis_data->d_chebyshev_interp_1d) {
1848b97b69aSJeremy L Thompson           CeedSize    interp_bytes;
1858b97b69aSJeremy L Thompson           CeedScalar *chebyshev_interp_1d;
1868b97b69aSJeremy L Thompson 
1878b97b69aSJeremy L Thompson           interp_bytes = P_1d * Q_1d * sizeof(CeedScalar);
1888b97b69aSJeremy L Thompson           CeedCallBackend(CeedCalloc(P_1d * Q_1d, &chebyshev_interp_1d));
1898b97b69aSJeremy L Thompson           CeedCallBackend(CeedBasisGetChebyshevInterp1D(basis, chebyshev_interp_1d));
1908b97b69aSJeremy L Thompson           CeedCallCuda(CeedBasisReturnCeed(basis), cudaMalloc((void **)&basis_data->d_chebyshev_interp_1d, interp_bytes));
1918b97b69aSJeremy L Thompson           CeedCallCuda(CeedBasisReturnCeed(basis),
1928b97b69aSJeremy L Thompson                        cudaMemcpy(basis_data->d_chebyshev_interp_1d, chebyshev_interp_1d, interp_bytes, cudaMemcpyHostToDevice));
1938b97b69aSJeremy L Thompson           CeedCallBackend(CeedFree(&chebyshev_interp_1d));
1948b97b69aSJeremy L Thompson         }
1958b97b69aSJeremy L Thompson         if (is_input) data->B.inputs[i] = basis_data->d_chebyshev_interp_1d;
1968b97b69aSJeremy L Thompson         else data->B.outputs[i] = basis_data->d_chebyshev_interp_1d;
1978b97b69aSJeremy L Thompson       } else {
1988b97b69aSJeremy L Thompson         // Standard quadrature
1994b3e95d5SJeremy L Thompson         if (is_input) data->B.inputs[i] = basis_data->d_interp_1d;
2004b3e95d5SJeremy L Thompson         else data->B.outputs[i] = basis_data->d_interp_1d;
2018b97b69aSJeremy L Thompson       }
2020a2a6492SJeremy L Thompson       if (use_previous_field) {
20345a787f7SJeremy L Thompson         std::string reuse_var = "s_B" + ((field_reuse.is_input ? "_in_" : "_out_") + std::to_string(field_reuse.index));
2040a2a6492SJeremy L Thompson 
2050a2a6492SJeremy L Thompson         code << "  CeedScalar *s_B" << var_suffix << " = " << reuse_var << ";\n";
2060a2a6492SJeremy L Thompson       } else {
207dc007f05SJeremy L Thompson         code << "  __shared__ CeedScalar s_B" << var_suffix << "[" << P_name << "*" << Q_name << "];\n";
208f815fac9SJeremy L Thompson         code << "  LoadMatrix<" << P_name << ", " << Q_name << ">(data, B." << option_name << "[" << i << "], s_B" << var_suffix << ");\n";
2090a2a6492SJeremy L Thompson       }
2104b3e95d5SJeremy L Thompson       break;
2114b3e95d5SJeremy L Thompson     case CEED_EVAL_GRAD:
2128b97b69aSJeremy L Thompson       if (is_at_points) {
2138b97b69aSJeremy L Thompson         // AtPoints
2148b97b69aSJeremy L Thompson         if (!basis_data->d_chebyshev_interp_1d) {
2158b97b69aSJeremy L Thompson           CeedSize    interp_bytes;
2168b97b69aSJeremy L Thompson           CeedScalar *chebyshev_interp_1d;
2178b97b69aSJeremy L Thompson 
2188b97b69aSJeremy L Thompson           interp_bytes = P_1d * Q_1d * sizeof(CeedScalar);
2198b97b69aSJeremy L Thompson           CeedCallBackend(CeedCalloc(P_1d * Q_1d, &chebyshev_interp_1d));
2208b97b69aSJeremy L Thompson           CeedCallBackend(CeedBasisGetChebyshevInterp1D(basis, chebyshev_interp_1d));
2218b97b69aSJeremy L Thompson           CeedCallCuda(CeedBasisReturnCeed(basis), cudaMalloc((void **)&basis_data->d_chebyshev_interp_1d, interp_bytes));
2228b97b69aSJeremy L Thompson           CeedCallCuda(CeedBasisReturnCeed(basis),
2238b97b69aSJeremy L Thompson                        cudaMemcpy(basis_data->d_chebyshev_interp_1d, chebyshev_interp_1d, interp_bytes, cudaMemcpyHostToDevice));
2248b97b69aSJeremy L Thompson           CeedCallBackend(CeedFree(&chebyshev_interp_1d));
2258b97b69aSJeremy L Thompson         }
2268b97b69aSJeremy L Thompson         if (is_input) data->B.inputs[i] = basis_data->d_chebyshev_interp_1d;
2278b97b69aSJeremy L Thompson         else data->B.outputs[i] = basis_data->d_chebyshev_interp_1d;
2288b97b69aSJeremy L Thompson       } else {
2298b97b69aSJeremy L Thompson         // Standard quadrature
2304b3e95d5SJeremy L Thompson         if (is_input) data->B.inputs[i] = basis_data->d_interp_1d;
2314b3e95d5SJeremy L Thompson         else data->B.outputs[i] = basis_data->d_interp_1d;
2328b97b69aSJeremy L Thompson       }
233dc007f05SJeremy L Thompson       if (is_tensor) {
2340a2a6492SJeremy L Thompson         if (use_previous_field) {
23545a787f7SJeremy L Thompson           std::string reuse_var = "s_B" + ((field_reuse.is_input ? "_in_" : "_out_") + std::to_string(field_reuse.index));
2360a2a6492SJeremy L Thompson 
2370a2a6492SJeremy L Thompson           code << "  CeedScalar *s_B" << var_suffix << " = " << reuse_var << ";\n";
2380a2a6492SJeremy L Thompson         } else {
239dc007f05SJeremy L Thompson           code << "  __shared__ CeedScalar s_B" << var_suffix << "[" << P_name << "*" << Q_name << "];\n";
240f815fac9SJeremy L Thompson           code << "  LoadMatrix<" << P_name << ", " << Q_name << ">(data, B." << option_name << "[" << i << "], s_B" << var_suffix << ");\n";
241dc007f05SJeremy L Thompson         }
2420a2a6492SJeremy L Thompson       }
2438b97b69aSJeremy L Thompson       if (is_at_points) break;  // No G mat for AtPoints
2444b3e95d5SJeremy L Thompson       if (use_3d_slices) {
2454b3e95d5SJeremy L Thompson         if (is_input) data->G.inputs[i] = basis_data->d_collo_grad_1d;
2464b3e95d5SJeremy L Thompson         else data->G.outputs[i] = basis_data->d_collo_grad_1d;
24745a787f7SJeremy L Thompson         if (use_previous_field && field_reuse.eval_mode == CEED_EVAL_GRAD) {
24845a787f7SJeremy L Thompson           std::string reuse_var = "s_G" + ((field_reuse.is_input ? "_in_" : "_out_") + std::to_string(field_reuse.index));
2490a2a6492SJeremy L Thompson 
2500a2a6492SJeremy L Thompson           code << "  CeedScalar *s_G" << var_suffix << " = " << reuse_var << ";\n";
2510a2a6492SJeremy L Thompson         } else {
252dc007f05SJeremy L Thompson           code << "  __shared__ CeedScalar s_G" << var_suffix << "[" << Q_name << "*" << Q_name << "];\n";
253f815fac9SJeremy L Thompson           code << "  LoadMatrix<" << Q_name << ", " << Q_name << ">(data, G." << option_name << "[" << i << "], s_G" << var_suffix << ");\n";
2540a2a6492SJeremy L Thompson         }
2554b3e95d5SJeremy L Thompson       } else {
2564b3e95d5SJeremy L Thompson         bool has_collo_grad = basis_data->d_collo_grad_1d;
2574b3e95d5SJeremy L Thompson 
2584b3e95d5SJeremy L Thompson         if (is_input) data->G.inputs[i] = has_collo_grad ? basis_data->d_collo_grad_1d : basis_data->d_grad_1d;
2594b3e95d5SJeremy L Thompson         else data->G.outputs[i] = has_collo_grad ? basis_data->d_collo_grad_1d : basis_data->d_grad_1d;
2604b3e95d5SJeremy L Thompson         if (has_collo_grad) {
26145a787f7SJeremy L Thompson           if (use_previous_field && field_reuse.eval_mode == CEED_EVAL_GRAD) {
26245a787f7SJeremy L Thompson             std::string reuse_var = "s_G" + ((field_reuse.is_input ? "_in_" : "_out_") + std::to_string(field_reuse.index));
2630a2a6492SJeremy L Thompson 
2640a2a6492SJeremy L Thompson             code << "  CeedScalar *s_G" << var_suffix << " = " << reuse_var << ";\n";
2650a2a6492SJeremy L Thompson           } else {
266dc007f05SJeremy L Thompson             code << "  __shared__ CeedScalar s_G" << var_suffix << "[" << Q_name << "*" << Q_name << "];\n";
267f815fac9SJeremy L Thompson             code << "  LoadMatrix<" << Q_name << ", " << Q_name << ">(data, G." << option_name << "[" << i << "], s_G" << var_suffix << ");\n";
2680a2a6492SJeremy L Thompson           }
2690a2a6492SJeremy L Thompson         } else {
27045a787f7SJeremy L Thompson           if (use_previous_field && field_reuse.eval_mode == CEED_EVAL_GRAD) {
27145a787f7SJeremy L Thompson             std::string reuse_var = "s_G" + ((field_reuse.is_input ? "_in_" : "_out_") + std::to_string(field_reuse.index));
2720a2a6492SJeremy L Thompson 
2730a2a6492SJeremy L Thompson             code << "  CeedScalar *s_G" << var_suffix << " = " << reuse_var << ";\n";
2744b3e95d5SJeremy L Thompson           } else {
275dc007f05SJeremy L Thompson             code << "  __shared__ CeedScalar s_G" << var_suffix << "[" << P_name << "*" << Q_name << (is_tensor ? "" : "*dim") << "];\n";
276dc007f05SJeremy L Thompson             code << "  LoadMatrix<" << P_name << ", " << Q_name << (is_tensor ? "" : "*dim") << ">(data, G." << option_name << "[" << i << "], s_G"
277dc007f05SJeremy L Thompson                  << var_suffix << ");\n";
2784b3e95d5SJeremy L Thompson           }
2794b3e95d5SJeremy L Thompson         }
2800a2a6492SJeremy L Thompson       }
2814b3e95d5SJeremy L Thompson       break;
2824b3e95d5SJeremy L Thompson     case CEED_EVAL_WEIGHT:
2834b3e95d5SJeremy L Thompson       break;  // No action
2844b3e95d5SJeremy L Thompson       // LCOV_EXCL_START
2854b3e95d5SJeremy L Thompson     case CEED_EVAL_DIV:
2864b3e95d5SJeremy L Thompson     case CEED_EVAL_CURL:
2874b3e95d5SJeremy L Thompson       break;  // TODO: Not implemented
2884b3e95d5SJeremy L Thompson               // LCOV_EXCL_STOP
2894b3e95d5SJeremy L Thompson   }
2908b97b69aSJeremy L Thompson   CeedCallBackend(CeedBasisDestroy(&basis));
2914b3e95d5SJeremy L Thompson   return CEED_ERROR_SUCCESS;
2924b3e95d5SJeremy L Thompson }
2934b3e95d5SJeremy L Thompson 
2944b3e95d5SJeremy L Thompson //------------------------------------------------------------------------------
2954b3e95d5SJeremy L Thompson // Restriction
2964b3e95d5SJeremy L Thompson //------------------------------------------------------------------------------
2974b3e95d5SJeremy L Thompson static int CeedOperatorBuildKernelRestriction_Cuda_gen(std::ostringstream &code, CeedOperator_Cuda_gen *data, CeedInt i, CeedInt dim,
298e93651e5SJeremy L Thompson                                                        CeedInt field_input_buffer[], CeedOperatorField op_field, CeedQFunctionField qf_field,
299dc007f05SJeremy L Thompson                                                        CeedInt Q_1d, bool is_input, bool is_tensor, bool is_at_points, bool use_3d_slices) {
3004b3e95d5SJeremy L Thompson   std::string               var_suffix = (is_input ? "_in_" : "_out_") + std::to_string(i);
301dc007f05SJeremy L Thompson   std::string               P_name     = (is_tensor ? "P_1d" : "P") + var_suffix;
3024b3e95d5SJeremy L Thompson   CeedEvalMode              eval_mode  = CEED_EVAL_NONE;
3034b3e95d5SJeremy L Thompson   CeedInt                   elem_size = 0, num_comp = 0, P_1d = 0;
3044b3e95d5SJeremy L Thompson   CeedSize                  l_size;
305f815fac9SJeremy L Thompson   CeedRestrictionType       rstr_type = CEED_RESTRICTION_STANDARD;
3064b3e95d5SJeremy L Thompson   CeedElemRestriction_Cuda *rstr_data;
3074b3e95d5SJeremy L Thompson   CeedElemRestriction       elem_rstr;
3084b3e95d5SJeremy L Thompson   CeedBasis                 basis;
3094b3e95d5SJeremy L Thompson 
3104b3e95d5SJeremy L Thompson   // Get field data
3114b3e95d5SJeremy L Thompson   CeedCallBackend(CeedOperatorFieldGetElemRestriction(op_field, &elem_rstr));
3124b3e95d5SJeremy L Thompson   if (elem_rstr != CEED_ELEMRESTRICTION_NONE) {
313f815fac9SJeremy L Thompson     CeedCallBackend(CeedElemRestrictionGetType(elem_rstr, &rstr_type));
3144b3e95d5SJeremy L Thompson     CeedCallBackend(CeedElemRestrictionGetElementSize(elem_rstr, &elem_size));
3154b3e95d5SJeremy L Thompson     CeedCallBackend(CeedElemRestrictionGetNumComponents(elem_rstr, &num_comp));
3164b3e95d5SJeremy L Thompson     CeedCallBackend(CeedElemRestrictionGetData(elem_rstr, &rstr_data));
3174b3e95d5SJeremy L Thompson   }
3184b3e95d5SJeremy L Thompson   CeedCallBackend(CeedOperatorFieldGetBasis(op_field, &basis));
3194b3e95d5SJeremy L Thompson   if (basis != CEED_BASIS_NONE) {
320dc007f05SJeremy L Thompson     if (is_tensor) CeedCallBackend(CeedBasisGetNumNodes1D(basis, &P_1d));
321dc007f05SJeremy L Thompson     else CeedCallBackend(CeedBasisGetNumNodes(basis, &P_1d));
3224b3e95d5SJeremy L Thompson   }
323681d0ea7SJeremy L Thompson   CeedCallBackend(CeedBasisDestroy(&basis));
3244b3e95d5SJeremy L Thompson   CeedCallBackend(CeedQFunctionFieldGetEvalMode(qf_field, &eval_mode));
3254b3e95d5SJeremy L Thompson 
3264b3e95d5SJeremy L Thompson   // Restriction
3274b3e95d5SJeremy L Thompson   if (is_input) {
3284b3e95d5SJeremy L Thompson     // Input
329e93651e5SJeremy L Thompson     if (field_input_buffer[i] != i) {
330e93651e5SJeremy L Thompson       std::string buffer_name = "r_e_in_" + std::to_string(field_input_buffer[i]);
331e93651e5SJeremy L Thompson 
332e93651e5SJeremy L Thompson       // Restriction was already done for previous input
333e93651e5SJeremy L Thompson       code << "    CeedScalar *r_e" << var_suffix << " = " << buffer_name << ";\n";
3348b97b69aSJeremy L Thompson     } else if (eval_mode != CEED_EVAL_WEIGHT && !((eval_mode == CEED_EVAL_NONE) && use_3d_slices && is_at_points)) {
3358b97b69aSJeremy L Thompson       if (eval_mode == CEED_EVAL_NONE && rstr_type != CEED_RESTRICTION_POINTS) {
336e93651e5SJeremy L Thompson         // No basis action, so r_e_in_* in also r_q_in_* and needs to be allocated
3374b3e95d5SJeremy L Thompson         code << "    CeedScalar r_e" << var_suffix << "[num_comp" << var_suffix << "*" << P_name << "];\n";
3388b97b69aSJeremy L Thompson       } else if (rstr_type != CEED_RESTRICTION_POINTS) {
339e93651e5SJeremy L Thompson         // Otherwise we're using the scratch space
340e93651e5SJeremy L Thompson         code << "    CeedScalar *r_e" << var_suffix << " = r_e_scratch;\n";
341e93651e5SJeremy L Thompson       }
342f815fac9SJeremy L Thompson       switch (rstr_type) {
343f815fac9SJeremy L Thompson         case CEED_RESTRICTION_STANDARD: {
3444b3e95d5SJeremy L Thompson           CeedInt comp_stride;
3454b3e95d5SJeremy L Thompson 
3464b3e95d5SJeremy L Thompson           CeedCallBackend(CeedElemRestrictionGetLVectorSize(elem_rstr, &l_size));
3474b3e95d5SJeremy L Thompson           code << "    const CeedInt l_size" << var_suffix << " = " << l_size << ";\n";
3484b3e95d5SJeremy L Thompson           CeedCallBackend(CeedElemRestrictionGetCompStride(elem_rstr, &comp_stride));
3494b3e95d5SJeremy L Thompson           code << "    // CompStride: " << comp_stride << "\n";
3504b3e95d5SJeremy L Thompson           data->indices.inputs[i] = (CeedInt *)rstr_data->d_offsets;
351dc007f05SJeremy L Thompson           code << "    ReadLVecStandard" << (is_tensor ? dim : 1) << "d<num_comp" << var_suffix << ", " << comp_stride << ", " << P_name
352dc007f05SJeremy L Thompson                << ">(data, l_size" << var_suffix << ", elem, indices.inputs[" << i << "], d" << var_suffix << ", r_e" << var_suffix << ");\n";
353f815fac9SJeremy L Thompson           break;
354f815fac9SJeremy L Thompson         }
355f815fac9SJeremy L Thompson         case CEED_RESTRICTION_STRIDED: {
3564b3e95d5SJeremy L Thompson           bool    has_backend_strides;
3574b3e95d5SJeremy L Thompson           CeedInt num_elem;
3584b3e95d5SJeremy L Thompson 
3594b3e95d5SJeremy L Thompson           CeedCallBackend(CeedElemRestrictionHasBackendStrides(elem_rstr, &has_backend_strides));
3604b3e95d5SJeremy L Thompson           CeedCallBackend(CeedElemRestrictionGetNumElements(elem_rstr, &num_elem));
3614b3e95d5SJeremy L Thompson           CeedInt strides[3] = {1, elem_size * num_elem, elem_size};
3624b3e95d5SJeremy L Thompson 
3634b3e95d5SJeremy L Thompson           if (!has_backend_strides) {
3644b3e95d5SJeremy L Thompson             CeedCallBackend(CeedElemRestrictionGetStrides(elem_rstr, strides));
3654b3e95d5SJeremy L Thompson           }
3664b3e95d5SJeremy L Thompson           code << "    // Strides: {" << strides[0] << ", " << strides[1] << ", " << strides[2] << "}\n";
367dc007f05SJeremy L Thompson           code << "    ReadLVecStrided" << (is_tensor ? dim : 1) << "d<num_comp" << var_suffix << ", " << P_name << ", " << strides[0] << ", "
368dc007f05SJeremy L Thompson                << strides[1] << ", " << strides[2] << ">(data, elem, d" << var_suffix << ", r_e" << var_suffix << ");\n";
369f815fac9SJeremy L Thompson           break;
370f815fac9SJeremy L Thompson         }
3718b97b69aSJeremy L Thompson         case CEED_RESTRICTION_POINTS: {
3728b97b69aSJeremy L Thompson           CeedInt comp_stride;
3738b97b69aSJeremy L Thompson 
3748b97b69aSJeremy L Thompson           CeedCallBackend(CeedElemRestrictionGetCompStride(elem_rstr, &comp_stride));
3758b97b69aSJeremy L Thompson           code << "    const CeedInt comp_stride" << var_suffix << " = " << comp_stride << ";\n";
3768b97b69aSJeremy L Thompson           data->indices.inputs[i] = (CeedInt *)rstr_data->d_offsets;
3778b97b69aSJeremy L Thompson           break;
3788b97b69aSJeremy L Thompson         }
379f815fac9SJeremy L Thompson         // LCOV_EXCL_START
380f815fac9SJeremy L Thompson         case CEED_RESTRICTION_ORIENTED:
381f815fac9SJeremy L Thompson         case CEED_RESTRICTION_CURL_ORIENTED:
382f815fac9SJeremy L Thompson           break;  // TODO: Not implemented
383f815fac9SJeremy L Thompson                   // LCOV_EXCL_STOP
3844b3e95d5SJeremy L Thompson       }
3854b3e95d5SJeremy L Thompson     }
3864b3e95d5SJeremy L Thompson   } else {
3874b3e95d5SJeremy L Thompson     // Output
388f815fac9SJeremy L Thompson     switch (rstr_type) {
389f815fac9SJeremy L Thompson       case CEED_RESTRICTION_STANDARD: {
3904b3e95d5SJeremy L Thompson         CeedInt comp_stride;
3914b3e95d5SJeremy L Thompson 
3924b3e95d5SJeremy L Thompson         CeedCallBackend(CeedElemRestrictionGetLVectorSize(elem_rstr, &l_size));
3934b3e95d5SJeremy L Thompson         code << "    const CeedInt l_size" << var_suffix << " = " << l_size << ";\n";
3944b3e95d5SJeremy L Thompson         CeedCallBackend(CeedElemRestrictionGetCompStride(elem_rstr, &comp_stride));
3954b3e95d5SJeremy L Thompson         code << "    // CompStride: " << comp_stride << "\n";
3964b3e95d5SJeremy L Thompson         data->indices.outputs[i] = (CeedInt *)rstr_data->d_offsets;
397dc007f05SJeremy L Thompson         code << "    WriteLVecStandard" << (is_tensor ? dim : 1) << "d<num_comp" << var_suffix << ", " << comp_stride << ", " << P_name
398dc007f05SJeremy L Thompson              << ">(data, l_size" << var_suffix << ", elem, indices.outputs[" << i << "], r_e" << var_suffix << ", d" << var_suffix << ");\n";
399f815fac9SJeremy L Thompson         break;
400f815fac9SJeremy L Thompson       }
401f815fac9SJeremy L Thompson       case CEED_RESTRICTION_STRIDED: {
4024b3e95d5SJeremy L Thompson         bool    has_backend_strides;
4034b3e95d5SJeremy L Thompson         CeedInt num_elem;
4044b3e95d5SJeremy L Thompson 
4054b3e95d5SJeremy L Thompson         CeedCallBackend(CeedElemRestrictionHasBackendStrides(elem_rstr, &has_backend_strides));
4064b3e95d5SJeremy L Thompson         CeedCallBackend(CeedElemRestrictionGetNumElements(elem_rstr, &num_elem));
4074b3e95d5SJeremy L Thompson         CeedInt strides[3] = {1, elem_size * num_elem, elem_size};
4084b3e95d5SJeremy L Thompson 
4094b3e95d5SJeremy L Thompson         if (!has_backend_strides) {
4104b3e95d5SJeremy L Thompson           CeedCallBackend(CeedElemRestrictionGetStrides(elem_rstr, strides));
4114b3e95d5SJeremy L Thompson         }
4124b3e95d5SJeremy L Thompson         code << "    // Strides: {" << strides[0] << ", " << strides[1] << ", " << strides[2] << "}\n";
413dc007f05SJeremy L Thompson         code << "    WriteLVecStrided" << (is_tensor ? dim : 1) << "d<num_comp" << var_suffix << ", " << P_name << ", " << strides[0] << ", "
414dc007f05SJeremy L Thompson              << strides[1] << ", " << strides[2] << ">(data, elem, r_e" << var_suffix << ", d" << var_suffix << ");\n";
415f815fac9SJeremy L Thompson         break;
416f815fac9SJeremy L Thompson       }
4178b97b69aSJeremy L Thompson       case CEED_RESTRICTION_POINTS:
4188b97b69aSJeremy L Thompson         data->indices.outputs[i] = (CeedInt *)rstr_data->d_offsets;
4198b97b69aSJeremy L Thompson         break;
420f815fac9SJeremy L Thompson       // LCOV_EXCL_START
421f815fac9SJeremy L Thompson       case CEED_RESTRICTION_ORIENTED:
422f815fac9SJeremy L Thompson       case CEED_RESTRICTION_CURL_ORIENTED:
423f815fac9SJeremy L Thompson         break;  // TODO: Not implemented
424f815fac9SJeremy L Thompson                 // LCOV_EXCL_STOP
4254b3e95d5SJeremy L Thompson     }
4264b3e95d5SJeremy L Thompson   }
427681d0ea7SJeremy L Thompson   CeedCallBackend(CeedElemRestrictionDestroy(&elem_rstr));
4284b3e95d5SJeremy L Thompson   return CEED_ERROR_SUCCESS;
4294b3e95d5SJeremy L Thompson }
4304b3e95d5SJeremy L Thompson 
4314b3e95d5SJeremy L Thompson //------------------------------------------------------------------------------
4324b3e95d5SJeremy L Thompson // Basis
4334b3e95d5SJeremy L Thompson //------------------------------------------------------------------------------
4344b3e95d5SJeremy L Thompson static int CeedOperatorBuildKernelBasis_Cuda_gen(std::ostringstream &code, CeedOperator_Cuda_gen *data, CeedInt i, CeedInt dim,
435dc007f05SJeremy L Thompson                                                  CeedOperatorField op_field, CeedQFunctionField qf_field, CeedInt Q_1d, bool is_input, bool is_tensor,
4368b97b69aSJeremy L Thompson                                                  bool is_at_points, bool use_3d_slices) {
4374b3e95d5SJeremy L Thompson   std::string         var_suffix = (is_input ? "_in_" : "_out_") + std::to_string(i);
438dc007f05SJeremy L Thompson   std::string         P_name = (is_tensor ? "P_1d" : "P") + var_suffix, Q_name = is_tensor ? "Q_1d" : "Q";
4394b3e95d5SJeremy L Thompson   CeedEvalMode        eval_mode = CEED_EVAL_NONE;
4404b3e95d5SJeremy L Thompson   CeedInt             elem_size = 0, num_comp = 0, P_1d = 0;
4414b3e95d5SJeremy L Thompson   CeedElemRestriction elem_rstr;
4424b3e95d5SJeremy L Thompson   CeedBasis           basis;
4434b3e95d5SJeremy L Thompson 
4444b3e95d5SJeremy L Thompson   // Get field data
4454b3e95d5SJeremy L Thompson   CeedCallBackend(CeedOperatorFieldGetElemRestriction(op_field, &elem_rstr));
4464b3e95d5SJeremy L Thompson   if (elem_rstr != CEED_ELEMRESTRICTION_NONE) {
4474b3e95d5SJeremy L Thompson     CeedCallBackend(CeedElemRestrictionGetElementSize(elem_rstr, &elem_size));
4484b3e95d5SJeremy L Thompson     CeedCallBackend(CeedElemRestrictionGetNumComponents(elem_rstr, &num_comp));
4494b3e95d5SJeremy L Thompson   }
450681d0ea7SJeremy L Thompson   CeedCallBackend(CeedElemRestrictionDestroy(&elem_rstr));
4514b3e95d5SJeremy L Thompson   CeedCallBackend(CeedOperatorFieldGetBasis(op_field, &basis));
4524b3e95d5SJeremy L Thompson   if (basis != CEED_BASIS_NONE) {
453dc007f05SJeremy L Thompson     if (is_tensor) CeedCallBackend(CeedBasisGetNumNodes1D(basis, &P_1d));
454dc007f05SJeremy L Thompson     else CeedCallBackend(CeedBasisGetNumNodes(basis, &P_1d));
4554b3e95d5SJeremy L Thompson   }
4564b3e95d5SJeremy L Thompson   CeedCallBackend(CeedQFunctionFieldGetEvalMode(qf_field, &eval_mode));
4574b3e95d5SJeremy L Thompson 
4584b3e95d5SJeremy L Thompson   // Basis
4594b3e95d5SJeremy L Thompson   code << "    // EvalMode: " << CeedEvalModes[eval_mode] << "\n";
4604b3e95d5SJeremy L Thompson   if (is_input) {
4614b3e95d5SJeremy L Thompson     switch (eval_mode) {
4624b3e95d5SJeremy L Thompson       case CEED_EVAL_NONE:
4638b97b69aSJeremy L Thompson         if (!use_3d_slices && !is_at_points) {
4644b3e95d5SJeremy L Thompson           code << "    CeedScalar *r_q" << var_suffix << " = r_e" << var_suffix << ";\n";
4654b3e95d5SJeremy L Thompson         }
4664b3e95d5SJeremy L Thompson         break;
4674b3e95d5SJeremy L Thompson       case CEED_EVAL_INTERP:
4688b97b69aSJeremy L Thompson         if (is_at_points) {
469dc007f05SJeremy L Thompson           std::string function_name = (dim == 1 ? "Interp" : "InterpTensor") + std::to_string(dim) + "d";
470dc007f05SJeremy L Thompson 
471dc007f05SJeremy L Thompson           code << "    CeedScalar r_c" << var_suffix << "[num_comp" << var_suffix << "*" << (dim >= 3 ? Q_name : "1") << "];\n";
472dc007f05SJeremy L Thompson           code << "    " << function_name << "<num_comp" << var_suffix << ", " << P_name << ", " << Q_name << ">(data, r_e" << var_suffix << ", s_B"
473dc007f05SJeremy L Thompson                << var_suffix << ", r_c" << var_suffix << ");\n";
4748b97b69aSJeremy L Thompson         } else {
475dc007f05SJeremy L Thompson           std::string function_name = is_tensor ? ((dim == 1 ? "Interp" : "InterpTensor") + std::to_string(dim) + "d") : "InterpNonTensor";
476dc007f05SJeremy L Thompson 
477dc007f05SJeremy L Thompson           code << "    CeedScalar r_q" << var_suffix << "[num_comp" << var_suffix << "*" << (is_tensor && (dim >= 3) ? Q_name : "1") << "];\n";
478dc007f05SJeremy L Thompson           code << "    " << function_name << "<num_comp" << var_suffix << ", " << P_name << ", " << Q_name << ">(data, r_e" << var_suffix << ", s_B"
479dc007f05SJeremy L Thompson                << var_suffix << ", r_q" << var_suffix << ");\n";
4808b97b69aSJeremy L Thompson         }
4814b3e95d5SJeremy L Thompson         break;
4824b3e95d5SJeremy L Thompson       case CEED_EVAL_GRAD:
4838b97b69aSJeremy L Thompson         if (is_at_points) {
484dc007f05SJeremy L Thompson           std::string function_name = (dim == 1 ? "Interp" : "InterpTensor") + std::to_string(dim) + "d";
485dc007f05SJeremy L Thompson 
486dc007f05SJeremy L Thompson           code << "    CeedScalar r_c" << var_suffix << "[num_comp" << var_suffix << "*" << (dim >= 3 ? Q_name : "1") << "];\n";
487dc007f05SJeremy L Thompson           code << "    " << function_name << "<num_comp" << var_suffix << ", " << P_name << ", " << Q_name << ">(data, r_e" << var_suffix << ", s_B"
488dc007f05SJeremy L Thompson                << var_suffix << ", r_c" << var_suffix << ");\n";
4898b97b69aSJeremy L Thompson         } else if (use_3d_slices) {
490dc007f05SJeremy L Thompson           std::string function_name = (dim > 1 ? "InterpTensor" : "Interp") + std::to_string(dim) + "d";
491dc007f05SJeremy L Thompson 
4924b3e95d5SJeremy L Thompson           code << "    CeedScalar r_q" << var_suffix << "[num_comp" << var_suffix << "*" << Q_name << "];\n";
493dc007f05SJeremy L Thompson           code << "    " << function_name << "<num_comp" << var_suffix << ", " << P_name << ", " << Q_name << ">(data, r_e" << var_suffix << ", s_B"
494dc007f05SJeremy L Thompson                << var_suffix << ", r_q" << var_suffix << ");\n";
495dc007f05SJeremy L Thompson         } else if (is_tensor) {
496dc007f05SJeremy L Thompson           bool        is_collocated = dim == 3 && Q_1d >= P_1d;
497dc007f05SJeremy L Thompson           std::string function_name = (dim == 1 ? "Grad" : (is_collocated ? "GradTensorCollocated" : "GradTensor")) + std::to_string(dim) + "d";
498dc007f05SJeremy L Thompson 
499dc007f05SJeremy L Thompson           code << "    CeedScalar r_q" << var_suffix << "[num_comp" << var_suffix << "*dim*" << (dim >= 3 ? Q_name : "1") << "];\n";
500dc007f05SJeremy L Thompson           code << "    " << function_name << "<num_comp" << var_suffix << ", " << P_name << ", " << Q_name << ">(data, r_e" << var_suffix << ", s_B"
501dc007f05SJeremy L Thompson                << var_suffix << ", s_G" << var_suffix << ", r_q" << var_suffix << ");\n";
5024b3e95d5SJeremy L Thompson         } else {
503dc007f05SJeremy L Thompson           std::string function_name = "GradNonTensor";
504dc007f05SJeremy L Thompson 
505dc007f05SJeremy L Thompson           code << "    CeedScalar r_q" << var_suffix << "[num_comp" << var_suffix << "*dim];\n";
506dc007f05SJeremy L Thompson           code << "    " << function_name << "<num_comp" << var_suffix << ", dim, " << P_name << ", " << Q_name << ">(data, r_e" << var_suffix
507dc007f05SJeremy L Thompson                << ", s_G" << var_suffix << ", r_q" << var_suffix << ");\n";
5084b3e95d5SJeremy L Thompson         }
5094b3e95d5SJeremy L Thompson         break;
5104b3e95d5SJeremy L Thompson       case CEED_EVAL_WEIGHT: {
5118b97b69aSJeremy L Thompson         if (is_at_points) {
5128b97b69aSJeremy L Thompson           code << "    // Nothing to do AtPoints\n";
5138b97b69aSJeremy L Thompson         } else {
5144b3e95d5SJeremy L Thompson           CeedBasis_Cuda_shared *basis_data;
515dc007f05SJeremy L Thompson           std::string            function_name = is_tensor ? ((dim == 1 ? "Weight" : "WeightTensor") + std::to_string(dim) + "d") : "WeightNonTensor";
5164b3e95d5SJeremy L Thompson 
517dc007f05SJeremy L Thompson           code << "    CeedScalar r_q" << var_suffix << "[" << (is_tensor && (dim >= 3) ? Q_name : "1") << "];\n";
5184b3e95d5SJeremy L Thompson           CeedCallBackend(CeedBasisGetData(basis, &basis_data));
5194b3e95d5SJeremy L Thompson           data->W = basis_data->d_q_weight_1d;
520dc007f05SJeremy L Thompson           code << "    " << function_name << "<" << Q_name << ">(data, W, r_q" << var_suffix << ");\n";
5218b97b69aSJeremy L Thompson         }
5224b3e95d5SJeremy L Thompson         break;
5234b3e95d5SJeremy L Thompson       }
5244b3e95d5SJeremy L Thompson       // LCOV_EXCL_START
5254b3e95d5SJeremy L Thompson       case CEED_EVAL_DIV:
5264b3e95d5SJeremy L Thompson       case CEED_EVAL_CURL:
5274b3e95d5SJeremy L Thompson         break;  // TODO: Not implemented
5284b3e95d5SJeremy L Thompson                 // LCOV_EXCL_STOP
5294b3e95d5SJeremy L Thompson     }
5304b3e95d5SJeremy L Thompson   } else {
5314b3e95d5SJeremy L Thompson     switch (eval_mode) {
5324b3e95d5SJeremy L Thompson       case CEED_EVAL_NONE:
5334b3e95d5SJeremy L Thompson         code << "    CeedScalar *r_e" << var_suffix << " = r_q" << var_suffix << ";\n";
5344b3e95d5SJeremy L Thompson         break;  // No action
5354b3e95d5SJeremy L Thompson       case CEED_EVAL_INTERP:
536e93651e5SJeremy L Thompson         code << "    CeedScalar *r_e" << var_suffix << " = r_e_scratch;\n";
5378b97b69aSJeremy L Thompson         if (is_at_points) {
538dc007f05SJeremy L Thompson           std::string function_name = (dim == 1 ? "InterpTranspose" : "InterpTransposeTensor") + std::to_string(dim) + "d";
539dc007f05SJeremy L Thompson 
540dc007f05SJeremy L Thompson           code << "    " << function_name << "<num_comp" << var_suffix << ", " << P_name << ", " << Q_name << ">(data, r_c" << var_suffix << ", s_B"
541dc007f05SJeremy L Thompson                << var_suffix << ", r_e" << var_suffix << ");\n";
5428b97b69aSJeremy L Thompson         } else {
543dc007f05SJeremy L Thompson           std::string function_name =
544dc007f05SJeremy L Thompson               is_tensor ? ((dim == 1 ? "InterpTranspose" : "InterpTransposeTensor") + std::to_string(dim) + "d") : "InterpTransposeNonTensor";
545dc007f05SJeremy L Thompson 
546dc007f05SJeremy L Thompson           code << "    " << function_name << "<num_comp" << var_suffix << ", " << P_name << ", " << Q_name << ">(data, r_q" << var_suffix << ", s_B"
547dc007f05SJeremy L Thompson                << var_suffix << ", r_e" << var_suffix << ");\n";
5488b97b69aSJeremy L Thompson         }
5494b3e95d5SJeremy L Thompson         break;
5504b3e95d5SJeremy L Thompson       case CEED_EVAL_GRAD:
551e93651e5SJeremy L Thompson         code << "    CeedScalar *r_e" << var_suffix << " = r_e_scratch;\n";
5528b97b69aSJeremy L Thompson         if (is_at_points) {
553dc007f05SJeremy L Thompson           std::string function_name = (dim == 1 ? "InterpTranspose" : "InterpTransposeTensor") + std::to_string(dim) + "d";
554dc007f05SJeremy L Thompson 
555dc007f05SJeremy L Thompson           code << "    " << function_name << "<num_comp" << var_suffix << ", " << P_name << ", " << Q_name << ">(data, r_c" << var_suffix << ", s_B"
556dc007f05SJeremy L Thompson                << var_suffix << ", r_e" << var_suffix << ");\n";
5578b97b69aSJeremy L Thompson         } else if (use_3d_slices) {
558dc007f05SJeremy L Thompson           std::string function_name = (dim == 1 ? "InterpTranspose" : "InterpTransposeTensor") + std::to_string(dim) + "d";
559dc007f05SJeremy L Thompson 
560dc007f05SJeremy L Thompson           code << "    " << function_name << "<num_comp" << var_suffix << ", " << P_name << ", " << Q_name << ">(data, r_q" << var_suffix << ", s_B"
561dc007f05SJeremy L Thompson                << var_suffix << ", r_e" << var_suffix << ");\n";
562dc007f05SJeremy L Thompson         } else if (is_tensor) {
563dc007f05SJeremy L Thompson           bool        is_collocated = dim == 3 && Q_1d >= P_1d;
564dc007f05SJeremy L Thompson           std::string function_name =
565dc007f05SJeremy L Thompson               (dim == 1 ? "GradTranspose" : (is_collocated ? "GradTransposeTensorCollocated" : "GradTransposeTensor")) + std::to_string(dim) + "d";
566dc007f05SJeremy L Thompson 
567dc007f05SJeremy L Thompson           code << "    " << function_name << "<num_comp" << var_suffix << ", " << P_name << ", " << Q_name << ">(data, r_q" << var_suffix << ", s_B"
568dc007f05SJeremy L Thompson                << var_suffix << ", s_G" << var_suffix << ", r_e" << var_suffix << ");\n";
5694b3e95d5SJeremy L Thompson         } else {
570dc007f05SJeremy L Thompson           std::string function_name = "GradTransposeNonTensor";
571dc007f05SJeremy L Thompson 
572dc007f05SJeremy L Thompson           code << "    " << function_name << "<num_comp" << var_suffix << ", dim, " << P_name << ", " << Q_name << ">(data, r_q" << var_suffix
573dc007f05SJeremy L Thompson                << ", s_G" << var_suffix << ", r_e" << var_suffix << ");\n";
5744b3e95d5SJeremy L Thompson         }
5754b3e95d5SJeremy L Thompson         break;
5764b3e95d5SJeremy L Thompson       // LCOV_EXCL_START
5774b3e95d5SJeremy L Thompson       case CEED_EVAL_WEIGHT:
5784b3e95d5SJeremy L Thompson         break;  // Should not occur
5794b3e95d5SJeremy L Thompson       case CEED_EVAL_DIV:
5804b3e95d5SJeremy L Thompson       case CEED_EVAL_CURL:
5814b3e95d5SJeremy L Thompson         break;  // TODO: Not implemented
5824b3e95d5SJeremy L Thompson                 // LCOV_EXCL_STOP
5834b3e95d5SJeremy L Thompson     }
5844b3e95d5SJeremy L Thompson   }
585681d0ea7SJeremy L Thompson   CeedCallBackend(CeedBasisDestroy(&basis));
5864b3e95d5SJeremy L Thompson   return CEED_ERROR_SUCCESS;
5874b3e95d5SJeremy L Thompson }
5884b3e95d5SJeremy L Thompson 
5894b3e95d5SJeremy L Thompson //------------------------------------------------------------------------------
5904b3e95d5SJeremy L Thompson // QFunction
5914b3e95d5SJeremy L Thompson //------------------------------------------------------------------------------
5928b97b69aSJeremy L Thompson static int CeedOperatorBuildKernelQFunction_Cuda_gen(std::ostringstream &code, CeedOperator_Cuda_gen *data, CeedInt dim, CeedInt max_num_points,
5938b97b69aSJeremy L Thompson                                                      CeedInt num_input_fields, CeedOperatorField *op_input_fields,
5948b97b69aSJeremy L Thompson                                                      CeedQFunctionField *qf_input_fields, CeedInt num_output_fields,
5958b97b69aSJeremy L Thompson                                                      CeedOperatorField *op_output_fields, CeedQFunctionField *qf_output_fields,
596dc007f05SJeremy L Thompson                                                      std::string qfunction_name, CeedInt Q_1d, bool is_tensor, bool is_at_points,
597dc007f05SJeremy L Thompson                                                      bool use_3d_slices) {
598dc007f05SJeremy L Thompson   std::string         Q_name    = is_tensor ? "Q_1d" : "Q";
5994b3e95d5SJeremy L Thompson   CeedEvalMode        eval_mode = CEED_EVAL_NONE;
6004b3e95d5SJeremy L Thompson   CeedElemRestriction elem_rstr;
6014b3e95d5SJeremy L Thompson 
6028b97b69aSJeremy L Thompson   // Setup output arrays
6034b3e95d5SJeremy L Thompson   code << "\n    // -- Output field setup\n";
6044b3e95d5SJeremy L Thompson   for (CeedInt i = 0; i < num_output_fields; i++) {
605*59fa3f92SJeremy L Thompson     const char *field_name;
6064b3e95d5SJeremy L Thompson     std::string var_suffix = "_out_" + std::to_string(i);
6074b3e95d5SJeremy L Thompson 
608*59fa3f92SJeremy L Thompson     CeedCallBackend(CeedOperatorFieldGetName(op_output_fields[i], &field_name));
609*59fa3f92SJeremy L Thompson     code << "    // ---- Output field " << i << ": " << field_name << "\n";
6104b3e95d5SJeremy L Thompson     CeedCallBackend(CeedQFunctionFieldGetEvalMode(qf_output_fields[i], &eval_mode));
6118b97b69aSJeremy L Thompson     switch (eval_mode) {
6128b97b69aSJeremy L Thompson       case CEED_EVAL_NONE:
6138b97b69aSJeremy L Thompson         if (is_at_points) {
6148b97b69aSJeremy L Thompson           code << "    CeedScalar r_q" << var_suffix << "[num_comp" << var_suffix << "];\n";
6158b97b69aSJeremy L Thompson         } else {
616dc007f05SJeremy L Thompson           code << "    CeedScalar r_q" << var_suffix << "[num_comp" << var_suffix << "*" << (is_tensor && (dim >= 3) ? Q_name : "1") << "];\n";
6174b3e95d5SJeremy L Thompson         }
6188b97b69aSJeremy L Thompson         break;
6198b97b69aSJeremy L Thompson       case CEED_EVAL_INTERP:
6208b97b69aSJeremy L Thompson         if (is_at_points) {
6218b97b69aSJeremy L Thompson           // Accumulator for point data
622dc007f05SJeremy L Thompson           code << "    CeedScalar r_c" << var_suffix << "[num_comp" << var_suffix << "*" << (dim >= 3 ? Q_name : "1") << "];\n";
623dc007f05SJeremy L Thompson           code << "    for (CeedInt i = 0; i < num_comp" << var_suffix << "*" << (dim >= 3 ? Q_name : "1") << "; i++) {\n";
6248b97b69aSJeremy L Thompson           code << "      r_c" << var_suffix << "[i] = 0.0;\n";
6258b97b69aSJeremy L Thompson           code << "    }\n";
6268b97b69aSJeremy L Thompson         } else {
627dc007f05SJeremy L Thompson           code << "    CeedScalar r_q" << var_suffix << "[num_comp" << var_suffix << "*" << (is_tensor && (dim >= 3) ? Q_name : "1") << "];\n";
6288b97b69aSJeremy L Thompson         }
6298b97b69aSJeremy L Thompson         break;
6308b97b69aSJeremy L Thompson       case CEED_EVAL_GRAD:
6318b97b69aSJeremy L Thompson         if (is_at_points) {
6328b97b69aSJeremy L Thompson           // Accumulator for point data
633dc007f05SJeremy L Thompson           code << "    CeedScalar r_c" << var_suffix << "[num_comp" << var_suffix << "*" << (dim >= 3 ? Q_name : "1") << "*dim];\n";
634dc007f05SJeremy L Thompson           code << "    for (CeedInt i = 0; i < num_comp" << var_suffix << "*" << (dim >= 3 ? Q_name : "1") << "; i++) {\n";
6358b97b69aSJeremy L Thompson           code << "      r_c" << var_suffix << "[i] = 0.0;\n";
6368b97b69aSJeremy L Thompson           code << "    }\n";
6378b97b69aSJeremy L Thompson         } else if (use_3d_slices) {
6384b3e95d5SJeremy L Thompson           // Accumulator for gradient slices
6394b3e95d5SJeremy L Thompson           code << "    CeedScalar r_q" << var_suffix << "[num_comp" << var_suffix << "*" << Q_name << "];\n";
6404b3e95d5SJeremy L Thompson           code << "    for (CeedInt i = 0; i < num_comp" << var_suffix << "*" << Q_name << "; i++) {\n";
6414b3e95d5SJeremy L Thompson           code << "      r_q" << var_suffix << "[i] = 0.0;\n";
6424b3e95d5SJeremy L Thompson           code << "    }\n";
6434b3e95d5SJeremy L Thompson         } else {
644dc007f05SJeremy L Thompson           code << "    CeedScalar r_q" << var_suffix << "[num_comp" << var_suffix << "*dim*" << (is_tensor && (dim >= 3) ? Q_name : "1") << "];\n";
6454b3e95d5SJeremy L Thompson         }
6468b97b69aSJeremy L Thompson         break;
6478b97b69aSJeremy L Thompson       case CEED_EVAL_WEIGHT:
6488b97b69aSJeremy L Thompson         break;
6498b97b69aSJeremy L Thompson         // LCOV_EXCL_START
6508b97b69aSJeremy L Thompson       case CEED_EVAL_DIV:
6518b97b69aSJeremy L Thompson       case CEED_EVAL_CURL:
6528b97b69aSJeremy L Thompson         break;  // TODO: Not implemented
6538b97b69aSJeremy L Thompson                 // LCOV_EXCL_STOP
6544b3e95d5SJeremy L Thompson     }
6554b3e95d5SJeremy L Thompson   }
6564b3e95d5SJeremy L Thompson 
6578b97b69aSJeremy L Thompson   if (is_at_points) {
6588b97b69aSJeremy L Thompson     // We need to handle batches of points
6598b97b69aSJeremy L Thompson     code << "\n    // Note: Using batches of points\n";
6608b97b69aSJeremy L Thompson     code << "    const CeedInt point_loop_bound = (blockDim.x * blockDim.y) * ceil(1.0 * max_num_points / (blockDim.x * blockDim.y));\n\n";
6618b97b69aSJeremy L Thompson     code << "    #pragma unroll\n";
6628b97b69aSJeremy L Thompson     code << "    for (CeedInt i = threadIdx.x + threadIdx.y * blockDim.x; i < point_loop_bound; i += blockDim.x * blockDim.y) {\n";
6638b97b69aSJeremy L Thompson     code << "      const CeedInt p = i % max_num_points;\n\n";
6648b97b69aSJeremy L Thompson 
6658b97b69aSJeremy L Thompson     code << "      // -- Coordinates\n";
6668b97b69aSJeremy L Thompson     code << "      CeedScalar r_x[dim];\n";
6678b97b69aSJeremy L Thompson     code << "      ReadPoint<dim, coords_comp_stride, max_num_points>(data, elem, p, max_num_points, points.indices, points.coords, r_x);\n\n";
6688b97b69aSJeremy L Thompson 
6698b97b69aSJeremy L Thompson     code << "      // -- Input fields\n";
6708b97b69aSJeremy L Thompson     for (CeedInt i = 0; i < num_input_fields; i++) {
671*59fa3f92SJeremy L Thompson       const char *field_name;
6728b97b69aSJeremy L Thompson       std::string var_suffix = "_in_" + std::to_string(i);
6738b97b69aSJeremy L Thompson 
674*59fa3f92SJeremy L Thompson       CeedCallBackend(CeedOperatorFieldGetName(op_input_fields[i], &field_name));
675*59fa3f92SJeremy L Thompson       code << "      // ---- Input field " << i << ": " << field_name << "\n";
6768b97b69aSJeremy L Thompson       CeedCallBackend(CeedQFunctionFieldGetEvalMode(qf_input_fields[i], &eval_mode));
6778b97b69aSJeremy L Thompson       // Basis action
6788b97b69aSJeremy L Thompson       code << "      // EvalMode: " << CeedEvalModes[eval_mode] << "\n";
6798b97b69aSJeremy L Thompson       switch (eval_mode) {
6808b97b69aSJeremy L Thompson         case CEED_EVAL_NONE:
6818b97b69aSJeremy L Thompson           code << "      CeedScalar r_s" << var_suffix << "[num_comp" << var_suffix << "];\n";
6828b97b69aSJeremy L Thompson           code << "      ReadPoint<num_comp" << var_suffix << ", comp_stride" << var_suffix
6838b97b69aSJeremy L Thompson                << ", max_num_points>(data, elem, p, max_num_points, indices.inputs[" << i << "], d" << var_suffix << ", r_s" << var_suffix << ");\n";
6848b97b69aSJeremy L Thompson           break;
6858b97b69aSJeremy L Thompson         case CEED_EVAL_INTERP:
6868b97b69aSJeremy L Thompson           code << "      CeedScalar r_s" << var_suffix << "[num_comp" << var_suffix << "];\n";
6878b97b69aSJeremy L Thompson           code << "      InterpAtPoints" << dim << "d<num_comp" << var_suffix << ", max_num_points, " << Q_name << ">(data, i, r_c" << var_suffix
6888b97b69aSJeremy L Thompson                << ", r_x, r_s" << var_suffix << ");\n";
6898b97b69aSJeremy L Thompson           break;
6908b97b69aSJeremy L Thompson         case CEED_EVAL_GRAD:
6918b97b69aSJeremy L Thompson           code << "      CeedScalar r_s" << var_suffix << "[num_comp" << var_suffix << "*dim];\n";
6928b97b69aSJeremy L Thompson           code << "      GradAtPoints" << dim << "d<num_comp" << var_suffix << ", max_num_points, " << Q_name << ">(data, i, r_c" << var_suffix
6938b97b69aSJeremy L Thompson                << ", r_x, r_s" << var_suffix << ");\n";
6948b97b69aSJeremy L Thompson           break;
6958b97b69aSJeremy L Thompson         case CEED_EVAL_WEIGHT:
6968b97b69aSJeremy L Thompson           code << "      CeedScalar r_s" << var_suffix << "[1];\n";
6978b97b69aSJeremy L Thompson           code << "      r_s" << var_suffix << "[0] = 1.0;\n";
6988b97b69aSJeremy L Thompson           break;
6998b97b69aSJeremy L Thompson           // LCOV_EXCL_START
7008b97b69aSJeremy L Thompson         case CEED_EVAL_DIV:
7018b97b69aSJeremy L Thompson         case CEED_EVAL_CURL:
7028b97b69aSJeremy L Thompson           break;  // TODO: Not implemented
7038b97b69aSJeremy L Thompson                   // LCOV_EXCL_STOP
7048b97b69aSJeremy L Thompson       }
7058b97b69aSJeremy L Thompson     }
7068b97b69aSJeremy L Thompson     code << "\n      // -- Output fields\n";
7078b97b69aSJeremy L Thompson     for (CeedInt i = 0; i < num_output_fields; i++) {
708*59fa3f92SJeremy L Thompson       const char *field_name;
7098b97b69aSJeremy L Thompson       std::string var_suffix = "_out_" + std::to_string(i);
7108b97b69aSJeremy L Thompson 
711*59fa3f92SJeremy L Thompson       CeedCallBackend(CeedOperatorFieldGetName(op_output_fields[i], &field_name));
712*59fa3f92SJeremy L Thompson       code << "      // ---- Output field " << i << ": " << field_name << "\n";
7138b97b69aSJeremy L Thompson       CeedCallBackend(CeedQFunctionFieldGetEvalMode(qf_output_fields[i], &eval_mode));
7148b97b69aSJeremy L Thompson       // Basis action
7158b97b69aSJeremy L Thompson       switch (eval_mode) {
7168b97b69aSJeremy L Thompson         case CEED_EVAL_NONE:
7178b97b69aSJeremy L Thompson           code << "      CeedScalar r_s" << var_suffix << "[num_comp" << var_suffix << "];\n";
7188b97b69aSJeremy L Thompson           break;
7198b97b69aSJeremy L Thompson         case CEED_EVAL_INTERP:
7208b97b69aSJeremy L Thompson           code << "      CeedScalar r_s" << var_suffix << "[num_comp" << var_suffix << "];\n";
7218b97b69aSJeremy L Thompson           break;
7228b97b69aSJeremy L Thompson         case CEED_EVAL_GRAD:
7238b97b69aSJeremy L Thompson           code << "      CeedScalar r_s" << var_suffix << "[num_comp" << var_suffix << "*dim];\n";
7248b97b69aSJeremy L Thompson           break;
7258b97b69aSJeremy L Thompson           // LCOV_EXCL_START
7268b97b69aSJeremy L Thompson         case CEED_EVAL_WEIGHT:
7278b97b69aSJeremy L Thompson           break;  // Should not occur
7288b97b69aSJeremy L Thompson         case CEED_EVAL_DIV:
7298b97b69aSJeremy L Thompson         case CEED_EVAL_CURL:
7308b97b69aSJeremy L Thompson           break;  // TODO: Not implemented
7318b97b69aSJeremy L Thompson                   // LCOV_EXCL_STOP
7328b97b69aSJeremy L Thompson       }
7338b97b69aSJeremy L Thompson     }
7348b97b69aSJeremy L Thompson 
7358b97b69aSJeremy L Thompson   } else if (use_3d_slices) {
7364b3e95d5SJeremy L Thompson     // We treat quadrature points per slice in 3d to save registers
7374b3e95d5SJeremy L Thompson     code << "\n    // Note: Using planes of 3D elements\n";
7384b3e95d5SJeremy L Thompson     code << "    #pragma unroll\n";
7394b3e95d5SJeremy L Thompson     code << "    for (CeedInt q = 0; q < " << Q_name << "; q++) {\n";
7404b3e95d5SJeremy L Thompson     code << "      // -- Input fields\n";
7414b3e95d5SJeremy L Thompson     for (CeedInt i = 0; i < num_input_fields; i++) {
742*59fa3f92SJeremy L Thompson       const char *field_name;
7434b3e95d5SJeremy L Thompson       std::string var_suffix = "_in_" + std::to_string(i);
7444b3e95d5SJeremy L Thompson 
745*59fa3f92SJeremy L Thompson       CeedCallBackend(CeedOperatorFieldGetName(op_input_fields[i], &field_name));
746*59fa3f92SJeremy L Thompson       code << "      // ---- Input field " << i << ": " << field_name << "\n";
7474b3e95d5SJeremy L Thompson       CeedCallBackend(CeedQFunctionFieldGetEvalMode(qf_input_fields[i], &eval_mode));
7484b3e95d5SJeremy L Thompson       // Basis action
7494b3e95d5SJeremy L Thompson       code << "      // EvalMode: " << CeedEvalModes[eval_mode] << "\n";
7504b3e95d5SJeremy L Thompson       switch (eval_mode) {
7514b3e95d5SJeremy L Thompson         case CEED_EVAL_NONE:
7524b3e95d5SJeremy L Thompson           bool is_strided;
7534b3e95d5SJeremy L Thompson 
7544b3e95d5SJeremy L Thompson           code << "      CeedScalar r_s" << var_suffix << "[num_comp" << var_suffix << "];\n";
7554b3e95d5SJeremy L Thompson 
7564b3e95d5SJeremy L Thompson           CeedCallBackend(CeedOperatorFieldGetElemRestriction(op_input_fields[i], &elem_rstr));
7574b3e95d5SJeremy L Thompson           CeedCallBackend(CeedElemRestrictionIsStrided(elem_rstr, &is_strided));
7584b3e95d5SJeremy L Thompson           if (is_strided) {
7594b3e95d5SJeremy L Thompson             bool    has_backend_strides;
7604b3e95d5SJeremy L Thompson             CeedInt num_elem, elem_size;
7614b3e95d5SJeremy L Thompson 
7624b3e95d5SJeremy L Thompson             CeedCallBackend(CeedElemRestrictionGetElementSize(elem_rstr, &elem_size));
7634b3e95d5SJeremy L Thompson             CeedCallBackend(CeedElemRestrictionHasBackendStrides(elem_rstr, &has_backend_strides));
7644b3e95d5SJeremy L Thompson             CeedCallBackend(CeedElemRestrictionGetNumElements(elem_rstr, &num_elem));
7654b3e95d5SJeremy L Thompson             CeedInt strides[3] = {1, elem_size * num_elem, elem_size};
7664b3e95d5SJeremy L Thompson 
7674b3e95d5SJeremy L Thompson             if (!has_backend_strides) {
7684b3e95d5SJeremy L Thompson               CeedCallBackend(CeedElemRestrictionGetStrides(elem_rstr, strides));
7694b3e95d5SJeremy L Thompson             }
7704b3e95d5SJeremy L Thompson             code << "      // Strides: {" << strides[0] << ", " << strides[1] << ", " << strides[2] << "}\n";
771f815fac9SJeremy L Thompson             code << "      ReadEVecSliceStrided3d<num_comp" << var_suffix << ", " << Q_name << ", " << strides[0] << ", " << strides[1] << ", "
7724b3e95d5SJeremy L Thompson                  << strides[2] << ">(data, elem, q, d" << var_suffix << ", r_s" << var_suffix << ");\n";
7734b3e95d5SJeremy L Thompson           } else {
7744b3e95d5SJeremy L Thompson             CeedSize                  l_size = 0;
7754b3e95d5SJeremy L Thompson             CeedInt                   comp_stride;
7764b3e95d5SJeremy L Thompson             CeedElemRestriction_Cuda *rstr_data;
7774b3e95d5SJeremy L Thompson 
7784b3e95d5SJeremy L Thompson             CeedCallBackend(CeedElemRestrictionGetLVectorSize(elem_rstr, &l_size));
7794b3e95d5SJeremy L Thompson             code << "      const CeedInt l_size" << var_suffix << " = " << l_size << ";\n";
7804b3e95d5SJeremy L Thompson             CeedCallBackend(CeedElemRestrictionGetCompStride(elem_rstr, &comp_stride));
7814b3e95d5SJeremy L Thompson             code << "      // CompStride: " << comp_stride << "\n";
7824b3e95d5SJeremy L Thompson             CeedCallBackend(CeedElemRestrictionGetData(elem_rstr, &rstr_data));
7834b3e95d5SJeremy L Thompson             data->indices.inputs[i] = (CeedInt *)rstr_data->d_offsets;
784f815fac9SJeremy L Thompson             code << "      ReadEVecSliceStandard3d<num_comp" << var_suffix << ", " << comp_stride << ", " << Q_name << ">(data, l_size" << var_suffix
7854b3e95d5SJeremy L Thompson                  << ", elem, q, indices.inputs[" << i << "], d" << var_suffix << ", r_s" << var_suffix << ");\n";
7864b3e95d5SJeremy L Thompson           }
787681d0ea7SJeremy L Thompson           CeedCallBackend(CeedElemRestrictionDestroy(&elem_rstr));
7884b3e95d5SJeremy L Thompson           break;
7894b3e95d5SJeremy L Thompson         case CEED_EVAL_INTERP:
7904b3e95d5SJeremy L Thompson           code << "      CeedScalar r_s" << var_suffix << "[num_comp" << var_suffix << "];\n";
7914b3e95d5SJeremy L Thompson           code << "      for (CeedInt j = 0; j < num_comp" << var_suffix << "; j++) {\n";
7924b3e95d5SJeremy L Thompson           code << "        r_s" << var_suffix << "[j] = r_q" << var_suffix << "[q + j*" << Q_name << "];\n";
7934b3e95d5SJeremy L Thompson           code << "      }\n";
7944b3e95d5SJeremy L Thompson           break;
7954b3e95d5SJeremy L Thompson         case CEED_EVAL_GRAD:
7964b3e95d5SJeremy L Thompson           code << "      CeedScalar r_s" << var_suffix << "[num_comp" << var_suffix << "*dim];\n";
797f815fac9SJeremy L Thompson           code << "      GradColloSlice3d<num_comp" << var_suffix << ", " << Q_name << ">(data, q, r_q" << var_suffix << ", s_G" << var_suffix
798f815fac9SJeremy L Thompson                << ", r_s" << var_suffix << ");\n";
7994b3e95d5SJeremy L Thompson           break;
8004b3e95d5SJeremy L Thompson         case CEED_EVAL_WEIGHT:
8014b3e95d5SJeremy L Thompson           code << "      CeedScalar r_s" << var_suffix << "[1];\n";
8024b3e95d5SJeremy L Thompson           code << "      r_s" << var_suffix << "[0] = r_q" << var_suffix << "[q];\n";
8038b97b69aSJeremy L Thompson           break;
8044b3e95d5SJeremy L Thompson           // LCOV_EXCL_START
8054b3e95d5SJeremy L Thompson         case CEED_EVAL_DIV:
8064b3e95d5SJeremy L Thompson         case CEED_EVAL_CURL:
8074b3e95d5SJeremy L Thompson           break;  // TODO: Not implemented
8084b3e95d5SJeremy L Thompson                   // LCOV_EXCL_STOP
8094b3e95d5SJeremy L Thompson       }
8104b3e95d5SJeremy L Thompson     }
8114b3e95d5SJeremy L Thompson     code << "\n      // -- Output fields\n";
8124b3e95d5SJeremy L Thompson     for (CeedInt i = 0; i < num_output_fields; i++) {
813*59fa3f92SJeremy L Thompson       const char *field_name;
8144b3e95d5SJeremy L Thompson       std::string var_suffix = "_out_" + std::to_string(i);
8154b3e95d5SJeremy L Thompson 
816*59fa3f92SJeremy L Thompson       CeedCallBackend(CeedOperatorFieldGetName(op_output_fields[i], &field_name));
817*59fa3f92SJeremy L Thompson       code << "      // ---- Output field " << i << ": " << field_name << "\n";
8184b3e95d5SJeremy L Thompson       CeedCallBackend(CeedQFunctionFieldGetEvalMode(qf_output_fields[i], &eval_mode));
8194b3e95d5SJeremy L Thompson       // Basis action
8204b3e95d5SJeremy L Thompson       switch (eval_mode) {
8214b3e95d5SJeremy L Thompson         case CEED_EVAL_NONE:
8224b3e95d5SJeremy L Thompson           code << "      CeedScalar r_s" << var_suffix << "[num_comp" << var_suffix << "];\n";
8238b97b69aSJeremy L Thompson           break;
8244b3e95d5SJeremy L Thompson         case CEED_EVAL_INTERP:
8254b3e95d5SJeremy L Thompson           code << "      CeedScalar r_s" << var_suffix << "[num_comp" << var_suffix << "];\n";
8264b3e95d5SJeremy L Thompson           break;
8274b3e95d5SJeremy L Thompson         case CEED_EVAL_GRAD:
8284b3e95d5SJeremy L Thompson           code << "      CeedScalar r_s" << var_suffix << "[num_comp" << var_suffix << "*dim];\n";
8294b3e95d5SJeremy L Thompson           break;
8304b3e95d5SJeremy L Thompson           // LCOV_EXCL_START
8314b3e95d5SJeremy L Thompson         case CEED_EVAL_WEIGHT:
8324b3e95d5SJeremy L Thompson           break;  // Should not occur
8334b3e95d5SJeremy L Thompson         case CEED_EVAL_DIV:
8344b3e95d5SJeremy L Thompson         case CEED_EVAL_CURL:
8354b3e95d5SJeremy L Thompson           break;  // TODO: Not implemented
8364b3e95d5SJeremy L Thompson                   // LCOV_EXCL_STOP
8374b3e95d5SJeremy L Thompson       }
8384b3e95d5SJeremy L Thompson     }
8394b3e95d5SJeremy L Thompson   } else {
8404b3e95d5SJeremy L Thompson     code << "\n    // Note: Using full elements\n";
8414b3e95d5SJeremy L Thompson     code << "    {\n";
8424b3e95d5SJeremy L Thompson     code << "      // -- Input fields\n";
8434b3e95d5SJeremy L Thompson     for (CeedInt i = 0; i < num_input_fields; i++) {
844*59fa3f92SJeremy L Thompson       const char *field_name;
845*59fa3f92SJeremy L Thompson 
846*59fa3f92SJeremy L Thompson       CeedCallBackend(CeedOperatorFieldGetName(op_input_fields[i], &field_name));
847*59fa3f92SJeremy L Thompson       code << "      // ---- Input field " << i << ": " << field_name << "\n";
8484b3e95d5SJeremy L Thompson       code << "      CeedScalar *r_s_in_" << i << " = r_q_in_" << i << ";\n";
8494b3e95d5SJeremy L Thompson     }
8504b3e95d5SJeremy L Thompson     code << "      // -- Output fields\n";
8514b3e95d5SJeremy L Thompson     for (CeedInt i = 0; i < num_output_fields; i++) {
852*59fa3f92SJeremy L Thompson       const char *field_name;
853*59fa3f92SJeremy L Thompson 
854*59fa3f92SJeremy L Thompson       CeedCallBackend(CeedOperatorFieldGetName(op_output_fields[i], &field_name));
855*59fa3f92SJeremy L Thompson       code << "      // ---- Output field " << i << ": " << field_name << "\n";
8564b3e95d5SJeremy L Thompson       code << "      CeedScalar *r_s_out_" << i << " = r_q_out_" << i << ";\n";
8574b3e95d5SJeremy L Thompson     }
8584b3e95d5SJeremy L Thompson   }
8594b3e95d5SJeremy L Thompson 
8604b3e95d5SJeremy L Thompson   // Input and output buffers
8614b3e95d5SJeremy L Thompson   code << "\n      // -- QFunction inputs and outputs\n";
8624b3e95d5SJeremy L Thompson   code << "      // ---- Inputs\n";
8634b3e95d5SJeremy L Thompson   code << "      CeedScalar *inputs[" << CeedIntMax(num_input_fields, 1) << "];\n";
8644b3e95d5SJeremy L Thompson   for (CeedInt i = 0; i < num_input_fields; i++) {
865*59fa3f92SJeremy L Thompson     const char *field_name;
866*59fa3f92SJeremy L Thompson 
867*59fa3f92SJeremy L Thompson     CeedCallBackend(CeedOperatorFieldGetName(op_input_fields[i], &field_name));
868*59fa3f92SJeremy L Thompson     code << "      // ------ Input field " << i << ": " << field_name << "\n";
8694b3e95d5SJeremy L Thompson     code << "      inputs[" << i << "] = r_s_in_" << i << ";\n";
8704b3e95d5SJeremy L Thompson   }
8714b3e95d5SJeremy L Thompson   code << "      // ---- Outputs\n";
8724b3e95d5SJeremy L Thompson   code << "      CeedScalar *outputs[" << CeedIntMax(num_output_fields, 1) << "];\n";
8734b3e95d5SJeremy L Thompson   for (CeedInt i = 0; i < num_output_fields; i++) {
874*59fa3f92SJeremy L Thompson     const char *field_name;
875*59fa3f92SJeremy L Thompson 
876*59fa3f92SJeremy L Thompson     CeedCallBackend(CeedOperatorFieldGetName(op_output_fields[i], &field_name));
877*59fa3f92SJeremy L Thompson     code << "      // ------ Output field " << i << ": " << field_name << "\n";
8784b3e95d5SJeremy L Thompson     code << "      outputs[" << i << "] = r_s_out_" << i << ";\n";
8794b3e95d5SJeremy L Thompson   }
8804b3e95d5SJeremy L Thompson 
8814b3e95d5SJeremy L Thompson   // Apply QFunction
8824b3e95d5SJeremy L Thompson   code << "\n      // -- Apply QFunction\n";
8834b3e95d5SJeremy L Thompson   code << "      " << qfunction_name << "(ctx, ";
884dc007f05SJeremy L Thompson   if (dim != 3 || is_at_points || use_3d_slices || !is_tensor) {
8854b3e95d5SJeremy L Thompson     code << "1";
8864b3e95d5SJeremy L Thompson   } else {
887dc007f05SJeremy L Thompson     code << Q_name;
8884b3e95d5SJeremy L Thompson   }
8894b3e95d5SJeremy L Thompson   code << ", inputs, outputs);\n";
8904b3e95d5SJeremy L Thompson 
8918b97b69aSJeremy L Thompson   if (is_at_points) {
8928b97b69aSJeremy L Thompson     // Map back to coefficients
8938b97b69aSJeremy L Thompson     code << "\n      // -- Output fields\n";
8948b97b69aSJeremy L Thompson     for (CeedInt i = 0; i < num_output_fields; i++) {
895*59fa3f92SJeremy L Thompson       const char *field_name;
8968b97b69aSJeremy L Thompson       std::string var_suffix = "_out_" + std::to_string(i);
8978b97b69aSJeremy L Thompson       std::string P_name     = "P_1d" + var_suffix;
8988b97b69aSJeremy L Thompson 
899*59fa3f92SJeremy L Thompson       CeedCallBackend(CeedOperatorFieldGetName(op_output_fields[i], &field_name));
900*59fa3f92SJeremy L Thompson       code << "      // ---- Output field " << i << ": " << field_name << "\n";
9018b97b69aSJeremy L Thompson       CeedCallBackend(CeedQFunctionFieldGetEvalMode(qf_output_fields[i], &eval_mode));
9028b97b69aSJeremy L Thompson       // Basis action
9038b97b69aSJeremy L Thompson       code << "      // EvalMode: " << CeedEvalModes[eval_mode] << "\n";
9048b97b69aSJeremy L Thompson       switch (eval_mode) {
9058b97b69aSJeremy L Thompson         case CEED_EVAL_NONE: {
9068b97b69aSJeremy L Thompson           CeedInt             comp_stride;
9078b97b69aSJeremy L Thompson           CeedElemRestriction elem_rstr;
9088b97b69aSJeremy L Thompson 
9098b97b69aSJeremy L Thompson           CeedCallBackend(CeedOperatorFieldGetElemRestriction(op_output_fields[i], &elem_rstr));
9108b97b69aSJeremy L Thompson           CeedCallBackend(CeedElemRestrictionGetCompStride(elem_rstr, &comp_stride));
9118b97b69aSJeremy L Thompson           CeedCallBackend(CeedElemRestrictionDestroy(&elem_rstr));
9128b97b69aSJeremy L Thompson           code << "      const CeedInt comp_stride" << var_suffix << " = " << comp_stride << ";\n";
9138b97b69aSJeremy L Thompson           code << "      WritePoint<num_comp" << var_suffix << ", comp_stride" << var_suffix
9148b97b69aSJeremy L Thompson                << ", max_num_points>(data, elem, i, points.num_per_elem[elem], indices.outputs[" << i << "]"
9158b97b69aSJeremy L Thompson                << ", r_s" << var_suffix << ", d" << var_suffix << ");\n";
9168b97b69aSJeremy L Thompson           break;
9178b97b69aSJeremy L Thompson         }
9188b97b69aSJeremy L Thompson         case CEED_EVAL_INTERP:
9198b97b69aSJeremy L Thompson           code << "      if (i >= points.num_per_elem[elem]) {\n";
9208b97b69aSJeremy L Thompson           code << "        for (CeedInt j = 0; j < num_comp" << var_suffix << "; j++) r_s" << var_suffix << "[j] = 0.0;\n";
9218b97b69aSJeremy L Thompson           code << "      }\n";
9228b97b69aSJeremy L Thompson           code << "      InterpTransposeAtPoints" << dim << "d<num_comp" << var_suffix << ", max_num_points, " << Q_name << ">(data, i, r_s"
9238b97b69aSJeremy L Thompson                << var_suffix << ", r_x, r_c" << var_suffix << ");\n";
9248b97b69aSJeremy L Thompson           break;
9258b97b69aSJeremy L Thompson         case CEED_EVAL_GRAD:
9268b97b69aSJeremy L Thompson           code << "      if (i >= points.num_per_elem[elem]) {\n";
9278b97b69aSJeremy L Thompson           code << "        for (CeedInt j = 0; j < num_comp" << var_suffix << "*dim; j++) r_s" << var_suffix << "[j] = 0.0;\n";
9288b97b69aSJeremy L Thompson           code << "      }\n";
9298b97b69aSJeremy L Thompson           code << "      GradTransposeAtPoints" << dim << "d<num_comp" << var_suffix << ", max_num_points, " << Q_name << ">(data, i, r_s"
9308b97b69aSJeremy L Thompson                << var_suffix << ", r_x, r_c" << var_suffix << ");\n";
9318b97b69aSJeremy L Thompson           break;
9328b97b69aSJeremy L Thompson           // LCOV_EXCL_START
9338b97b69aSJeremy L Thompson         case CEED_EVAL_WEIGHT:
9348b97b69aSJeremy L Thompson           break;  // Should not occur
9358b97b69aSJeremy L Thompson         case CEED_EVAL_DIV:
9368b97b69aSJeremy L Thompson         case CEED_EVAL_CURL:
9378b97b69aSJeremy L Thompson           break;  // TODO: Not implemented
9388b97b69aSJeremy L Thompson                   // LCOV_EXCL_STOP
9398b97b69aSJeremy L Thompson       }
9408b97b69aSJeremy L Thompson     }
9418b97b69aSJeremy L Thompson   } else if (use_3d_slices) {
9424b3e95d5SJeremy L Thompson     // Copy or apply transpose grad, if needed
9438b97b69aSJeremy L Thompson     code << "\n      // -- Output fields\n";
9444b3e95d5SJeremy L Thompson     for (CeedInt i = 0; i < num_output_fields; i++) {
945*59fa3f92SJeremy L Thompson       const char *field_name;
9464b3e95d5SJeremy L Thompson       std::string var_suffix = "_out_" + std::to_string(i);
9474b3e95d5SJeremy L Thompson       std::string P_name     = "P_1d" + var_suffix;
9484b3e95d5SJeremy L Thompson 
949*59fa3f92SJeremy L Thompson       CeedCallBackend(CeedOperatorFieldGetName(op_output_fields[i], &field_name));
950*59fa3f92SJeremy L Thompson       code << "      // ---- Output field " << i << ": " << field_name << "\n";
9514b3e95d5SJeremy L Thompson       CeedCallBackend(CeedQFunctionFieldGetEvalMode(qf_output_fields[i], &eval_mode));
9524b3e95d5SJeremy L Thompson       // Basis action
9534b3e95d5SJeremy L Thompson       code << "      // EvalMode: " << CeedEvalModes[eval_mode] << "\n";
9544b3e95d5SJeremy L Thompson       switch (eval_mode) {
9554b3e95d5SJeremy L Thompson         case CEED_EVAL_NONE:
9564b3e95d5SJeremy L Thompson           code << "      for (CeedInt j = 0; j < num_comp" << var_suffix << " ; j++) {\n";
9574b3e95d5SJeremy L Thompson           code << "        r_q" << var_suffix << "[q + j*" << Q_name << "] = r_s" << var_suffix << "[j];\n";
9584b3e95d5SJeremy L Thompson           code << "      }\n";
9598b97b69aSJeremy L Thompson           break;
9604b3e95d5SJeremy L Thompson         case CEED_EVAL_INTERP:
9614b3e95d5SJeremy L Thompson           code << "      for (CeedInt j = 0; j < num_comp" << var_suffix << " ; j++) {\n";
9624b3e95d5SJeremy L Thompson           code << "        r_q" << var_suffix << "[q + j*" << Q_name << "] = r_s" << var_suffix << "[j];\n";
9634b3e95d5SJeremy L Thompson           code << "      }\n";
9644b3e95d5SJeremy L Thompson           break;
9654b3e95d5SJeremy L Thompson         case CEED_EVAL_GRAD:
966f815fac9SJeremy L Thompson           code << "      GradColloSliceTranspose3d<num_comp" << var_suffix << ", " << Q_name << ">(data, q, r_s" << var_suffix << ", s_G"
967f815fac9SJeremy L Thompson                << var_suffix << ", r_q" << var_suffix << ");\n";
9684b3e95d5SJeremy L Thompson           break;
9694b3e95d5SJeremy L Thompson           // LCOV_EXCL_START
9704b3e95d5SJeremy L Thompson         case CEED_EVAL_WEIGHT:
9714b3e95d5SJeremy L Thompson           break;  // Should not occur
9724b3e95d5SJeremy L Thompson         case CEED_EVAL_DIV:
9734b3e95d5SJeremy L Thompson         case CEED_EVAL_CURL:
9744b3e95d5SJeremy L Thompson           break;  // TODO: Not implemented
9754b3e95d5SJeremy L Thompson                   // LCOV_EXCL_STOP
9764b3e95d5SJeremy L Thompson       }
9774b3e95d5SJeremy L Thompson     }
9784b3e95d5SJeremy L Thompson   }
9794b3e95d5SJeremy L Thompson   code << "    }\n";
9804b3e95d5SJeremy L Thompson   return CEED_ERROR_SUCCESS;
9814b3e95d5SJeremy L Thompson }
9824b3e95d5SJeremy L Thompson 
9834b3e95d5SJeremy L Thompson //------------------------------------------------------------------------------
984b2165e7aSSebastian Grimberg // Build single operator kernel
985ab213215SJeremy L Thompson //------------------------------------------------------------------------------
986ddae5012SJeremy L Thompson extern "C" int CeedOperatorBuildKernel_Cuda_gen(CeedOperator op, bool *is_good_build) {
9878b97b69aSJeremy L Thompson   bool                    is_tensor = true, is_at_points = false, use_3d_slices = false;
988241a4b83SYohann   Ceed                    ceed;
9898b97b69aSJeremy L Thompson   CeedInt                 Q_1d, num_input_fields, num_output_fields, dim = 1, max_num_points = 0, coords_comp_stride = 0;
990ca735530SJeremy L Thompson   CeedQFunctionField     *qf_input_fields, *qf_output_fields;
991ca735530SJeremy L Thompson   CeedQFunction_Cuda_gen *qf_data;
992ca735530SJeremy L Thompson   CeedQFunction           qf;
993ca735530SJeremy L Thompson   CeedOperatorField      *op_input_fields, *op_output_fields;
994ca735530SJeremy L Thompson   CeedOperator_Cuda_gen  *data;
9954b3e95d5SJeremy L Thompson   std::ostringstream      code;
9964b3e95d5SJeremy L Thompson 
997ddae5012SJeremy L Thompson   CeedCallBackend(CeedOperatorGetData(op, &data));
9984b3e95d5SJeremy L Thompson   {
9994b3e95d5SJeremy L Thompson     bool is_setup_done;
1000ca735530SJeremy L Thompson 
1001ca735530SJeremy L Thompson     CeedCallBackend(CeedOperatorIsSetupDone(op, &is_setup_done));
1002ddae5012SJeremy L Thompson     if (is_setup_done) {
1003ddae5012SJeremy L Thompson       *is_good_build = !data->use_fallback;
1004ddae5012SJeremy L Thompson       return CEED_ERROR_SUCCESS;
1005ddae5012SJeremy L Thompson     }
1006ddae5012SJeremy L Thompson   }
1007ddae5012SJeremy L Thompson 
1008ddae5012SJeremy L Thompson   // Check field compatibility
10098d12f40eSJeremy L Thompson   CeedCallBackend(CeedOperatorGetFields(op, &num_input_fields, &op_input_fields, &num_output_fields, &op_output_fields));
1010ddae5012SJeremy L Thompson   {
1011ddae5012SJeremy L Thompson     bool has_shared_bases = true, is_all_tensor = true, is_all_nontensor = true;
1012ddae5012SJeremy L Thompson 
1013ddae5012SJeremy L Thompson     for (CeedInt i = 0; i < num_input_fields; i++) {
1014ddae5012SJeremy L Thompson       CeedBasis basis;
1015ddae5012SJeremy L Thompson 
1016ddae5012SJeremy L Thompson       CeedCallBackend(CeedOperatorFieldGetBasis(op_input_fields[i], &basis));
1017ddae5012SJeremy L Thompson       if (basis != CEED_BASIS_NONE) {
1018ddae5012SJeremy L Thompson         bool        is_tensor = true;
1019ddae5012SJeremy L Thompson         const char *resource;
1020ddae5012SJeremy L Thompson         char       *resource_root;
1021ddae5012SJeremy L Thompson         Ceed        basis_ceed;
1022ddae5012SJeremy L Thompson 
1023ddae5012SJeremy L Thompson         CeedCallBackend(CeedBasisIsTensor(basis, &is_tensor));
1024c9192acaSJeremy L Thompson         is_all_tensor    = is_all_tensor && is_tensor;
102545a787f7SJeremy L Thompson         is_all_nontensor = is_all_nontensor && !is_tensor;
1026ddae5012SJeremy L Thompson         CeedCallBackend(CeedBasisGetCeed(basis, &basis_ceed));
1027ddae5012SJeremy L Thompson         CeedCallBackend(CeedGetResource(basis_ceed, &resource));
1028ddae5012SJeremy L Thompson         CeedCallBackend(CeedGetResourceRoot(basis_ceed, resource, ":", &resource_root));
1029c9192acaSJeremy L Thompson         has_shared_bases = has_shared_bases && !strcmp(resource_root, "/gpu/cuda/shared");
1030ddae5012SJeremy L Thompson         CeedCallBackend(CeedFree(&resource_root));
1031ddae5012SJeremy L Thompson         CeedCallBackend(CeedDestroy(&basis_ceed));
1032ddae5012SJeremy L Thompson       }
1033ddae5012SJeremy L Thompson       CeedCallBackend(CeedBasisDestroy(&basis));
10344b3e95d5SJeremy L Thompson     }
1035ca735530SJeremy L Thompson 
1036ddae5012SJeremy L Thompson     for (CeedInt i = 0; i < num_output_fields; i++) {
1037ddae5012SJeremy L Thompson       CeedBasis basis;
1038ddae5012SJeremy L Thompson 
1039ddae5012SJeremy L Thompson       CeedCallBackend(CeedOperatorFieldGetBasis(op_output_fields[i], &basis));
1040ddae5012SJeremy L Thompson       if (basis != CEED_BASIS_NONE) {
1041ddae5012SJeremy L Thompson         bool        is_tensor = true;
1042ddae5012SJeremy L Thompson         const char *resource;
1043ddae5012SJeremy L Thompson         char       *resource_root;
1044ddae5012SJeremy L Thompson         Ceed        basis_ceed;
1045ddae5012SJeremy L Thompson 
1046ddae5012SJeremy L Thompson         CeedCallBackend(CeedBasisIsTensor(basis, &is_tensor));
1047c9192acaSJeremy L Thompson         is_all_tensor    = is_all_tensor && is_tensor;
1048c9192acaSJeremy L Thompson         is_all_nontensor = is_all_nontensor && !is_tensor;
1049ddae5012SJeremy L Thompson 
1050ddae5012SJeremy L Thompson         CeedCallBackend(CeedBasisGetCeed(basis, &basis_ceed));
1051ddae5012SJeremy L Thompson         CeedCallBackend(CeedGetResource(basis_ceed, &resource));
1052ddae5012SJeremy L Thompson         CeedCallBackend(CeedGetResourceRoot(basis_ceed, resource, ":", &resource_root));
1053c9192acaSJeremy L Thompson         has_shared_bases = has_shared_bases && !strcmp(resource_root, "/gpu/cuda/shared");
1054ddae5012SJeremy L Thompson         CeedCallBackend(CeedFree(&resource_root));
1055ddae5012SJeremy L Thompson         CeedCallBackend(CeedDestroy(&basis_ceed));
1056ddae5012SJeremy L Thompson       }
1057ddae5012SJeremy L Thompson       CeedCallBackend(CeedBasisDestroy(&basis));
1058ddae5012SJeremy L Thompson     }
1059ddae5012SJeremy L Thompson     // -- Fallback to ref if not all bases are shared
1060ddae5012SJeremy L Thompson     if (!has_shared_bases || (!is_all_tensor && !is_all_nontensor)) {
1061ddae5012SJeremy L Thompson       *is_good_build = false;
1062ddae5012SJeremy L Thompson       return CEED_ERROR_SUCCESS;
1063ddae5012SJeremy L Thompson     }
1064ddae5012SJeremy L Thompson   }
1065ca735530SJeremy L Thompson   CeedCallBackend(CeedOperatorGetCeed(op, &ceed));
1066ca735530SJeremy L Thompson   CeedCallBackend(CeedOperatorGetQFunction(op, &qf));
1067ca735530SJeremy L Thompson   CeedCallBackend(CeedQFunctionGetData(qf, &qf_data));
1068ca735530SJeremy L Thompson   CeedCallBackend(CeedQFunctionGetFields(qf, NULL, &qf_input_fields, NULL, &qf_output_fields));
1069241a4b83SYohann 
10704b3e95d5SJeremy L Thompson   // Get operator data
10718b97b69aSJeremy L Thompson   CeedCallBackend(CeedOperatorIsAtPoints(op, &is_at_points));
10724b3e95d5SJeremy L Thompson   CeedCallBackend(CeedOperatorBuildKernelData_Cuda_gen(ceed, num_input_fields, op_input_fields, qf_input_fields, num_output_fields, op_output_fields,
10734b3e95d5SJeremy L Thompson                                                        qf_output_fields, &data->max_P_1d, &Q_1d, &dim, &is_tensor, &use_3d_slices));
10744b3e95d5SJeremy L Thompson   if (dim == 0) dim = 1;
10754b3e95d5SJeremy L Thompson   data->dim = dim;
10768b97b69aSJeremy L Thompson   if (is_at_points) {
10778b97b69aSJeremy L Thompson     CeedElemRestriction_Cuda *rstr_data;
10788b97b69aSJeremy L Thompson     CeedElemRestriction       rstr_points = NULL;
10794b3e95d5SJeremy L Thompson 
10808b97b69aSJeremy L Thompson     CeedCallBackend(CeedOperatorAtPointsGetPoints(op, &rstr_points, NULL));
10818b97b69aSJeremy L Thompson     CeedCallBackend(CeedElemRestrictionGetMaxPointsInElement(rstr_points, &max_num_points));
10828b97b69aSJeremy L Thompson     CeedCallBackend(CeedElemRestrictionGetCompStride(rstr_points, &coords_comp_stride));
10838b97b69aSJeremy L Thompson     CeedCallBackend(CeedElemRestrictionGetData(rstr_points, &rstr_data));
10848b97b69aSJeremy L Thompson     data->points.indices = (CeedInt *)rstr_data->d_offsets;
10858b97b69aSJeremy L Thompson     CeedCallBackend(CeedElemRestrictionDestroy(&rstr_points));
10868b97b69aSJeremy L Thompson   }
10878b97b69aSJeremy L Thompson   if (is_at_points) use_3d_slices = false;
10888b97b69aSJeremy L Thompson   if (Q_1d == 0) {
10898b97b69aSJeremy L Thompson     if (is_at_points) Q_1d = max_num_points;
10908b97b69aSJeremy L Thompson     else CeedCallBackend(CeedOperatorGetNumQuadraturePoints(op, &Q_1d));
10914b3e95d5SJeremy L Thompson   }
10924b3e95d5SJeremy L Thompson   data->Q_1d = Q_1d;
10934b3e95d5SJeremy L Thompson 
10940b454692Sjeremylt   // Check for restriction only identity operator
10954b3e95d5SJeremy L Thompson   {
10964b3e95d5SJeremy L Thompson     bool is_identity_qf;
10974b3e95d5SJeremy L Thompson 
10982b730f8bSJeremy L Thompson     CeedCallBackend(CeedQFunctionIsIdentity(qf, &is_identity_qf));
10990b454692Sjeremylt     if (is_identity_qf) {
11009e201c85SYohann       CeedEvalMode eval_mode_in, eval_mode_out;
1101ca735530SJeremy L Thompson 
11022b730f8bSJeremy L Thompson       CeedCallBackend(CeedQFunctionFieldGetEvalMode(qf_input_fields[0], &eval_mode_in));
11032b730f8bSJeremy L Thompson       CeedCallBackend(CeedQFunctionFieldGetEvalMode(qf_output_fields[0], &eval_mode_out));
11046574a04fSJeremy L Thompson       CeedCheck(eval_mode_in != CEED_EVAL_NONE || eval_mode_out != CEED_EVAL_NONE, ceed, CEED_ERROR_BACKEND,
11056574a04fSJeremy L Thompson                 "Backend does not implement restriction only identity operators");
11060b454692Sjeremylt     }
11074b3e95d5SJeremy L Thompson   }
11080b454692Sjeremylt 
1109f1a13f77SYohann Dudouit   // Add atomicAdd function for old NVidia architectures
11104b3e95d5SJeremy L Thompson   {
11114b3e95d5SJeremy L Thompson     Ceed_Cuda            *ceed_data;
11124b3e95d5SJeremy L Thompson     struct cudaDeviceProp prop;
11134b3e95d5SJeremy L Thompson 
11142b730f8bSJeremy L Thompson     CeedCallBackend(CeedGetData(ceed, &ceed_data));
11152b730f8bSJeremy L Thompson     CeedCallBackend(cudaGetDeviceProperties(&prop, ceed_data->device_id));
111680a9ef05SNatalie Beams     if ((prop.major < 6) && (CEED_SCALAR_TYPE != CEED_SCALAR_FP32)) {
11179c25dd66SJeremy L Thompson       code << "// AtomicAdd fallback source\n";
11189c25dd66SJeremy L Thompson       code << "#include <ceed/jit-source/cuda/cuda-atomic-add-fallback.h>\n\n";
1119f1a13f77SYohann Dudouit     }
11204b3e95d5SJeremy L Thompson   }
1121f1a13f77SYohann Dudouit 
11229e201c85SYohann   // Load basis source files
1123dc007f05SJeremy L Thompson   if (is_tensor) {
11249c25dd66SJeremy L Thompson     code << "// Tensor basis source\n";
11259c25dd66SJeremy L Thompson     code << "#include <ceed/jit-source/cuda/cuda-shared-basis-tensor-templates.h>\n\n";
1126dc007f05SJeremy L Thompson   } else {
1127dc007f05SJeremy L Thompson     code << "// Non-tensor basis source\n";
1128dc007f05SJeremy L Thompson     code << "#include <ceed/jit-source/cuda/cuda-shared-basis-nontensor-templates.h>\n\n";
1129dc007f05SJeremy L Thompson   }
1130dc007f05SJeremy L Thompson   if (is_at_points) {
11318b97b69aSJeremy L Thompson     code << "// AtPoints basis source\n";
11328b97b69aSJeremy L Thompson     code << "#include <ceed/jit-source/cuda/cuda-shared-basis-tensor-at-points-templates.h>\n\n";
1133dc007f05SJeremy L Thompson   }
11349c25dd66SJeremy L Thompson   code << "// CodeGen operator source\n";
11359c25dd66SJeremy L Thompson   code << "#include <ceed/jit-source/cuda/cuda-gen-templates.h>\n\n";
1136241a4b83SYohann 
11374b3e95d5SJeremy L Thompson   // Get QFunction name
11384b3e95d5SJeremy L Thompson   std::string qfunction_name(qf_data->qfunction_name);
11394b3e95d5SJeremy L Thompson   std::string operator_name;
11404b3e95d5SJeremy L Thompson 
114109095acaSJeremy L Thompson   operator_name = "CeedKernelCudaGenOperator_" + qfunction_name;
11424d537eeaSYohann 
11439e201c85SYohann   // Define CEED_Q_VLA
11449e201c85SYohann   code << "\n#undef CEED_Q_VLA\n";
1145dc007f05SJeremy L Thompson   if (dim != 3 || is_at_points || use_3d_slices || !is_tensor) {
11469e201c85SYohann     code << "#define CEED_Q_VLA 1\n\n";
11479e201c85SYohann   } else {
11489e201c85SYohann     code << "#define CEED_Q_VLA " << Q_1d << "\n\n";
11499e201c85SYohann   }
11509e201c85SYohann 
11514b3e95d5SJeremy L Thompson   // Add user QFunction source
11524b3e95d5SJeremy L Thompson   {
11539c25dd66SJeremy L Thompson     const char *source_path;
11544b3e95d5SJeremy L Thompson 
11559c25dd66SJeremy L Thompson     CeedCallBackend(CeedQFunctionGetSourcePath(qf, &source_path));
11569c25dd66SJeremy L Thompson     CeedCheck(source_path, ceed, CEED_ERROR_UNSUPPORTED, "/gpu/cuda/gen backend requires QFunction source code file");
11579c25dd66SJeremy L Thompson 
11589c25dd66SJeremy L Thompson     code << "// User QFunction source\n";
11599c25dd66SJeremy L Thompson     code << "#include \"" << source_path << "\"\n\n";
11604b3e95d5SJeremy L Thompson   }
1161241a4b83SYohann 
1162241a4b83SYohann   // Setup
1163818e0025SJeremy L Thompson   code << "\n// -----------------------------------------------------------------------------\n";
11644b3e95d5SJeremy L Thompson   code << "// Operator Kernel\n";
11654b3e95d5SJeremy L Thompson   code << "// \n";
11664b3e95d5SJeremy L Thompson   code << "// d_[in,out]_i:   CeedVector device array\n";
11674b3e95d5SJeremy L Thompson   code << "// r_[in,out]_e_i: Element vector register\n";
11684b3e95d5SJeremy L Thompson   code << "// r_[in,out]_q_i: Quadrature space vector register\n";
11699123fb08SJeremy L Thompson   code << "// r_[in,out]_c_i: AtPoints Chebyshev coefficients register\n";
11704b3e95d5SJeremy L Thompson   code << "// r_[in,out]_s_i: Quadrature space slice vector register\n";
11714b3e95d5SJeremy L Thompson   code << "// \n";
11724b3e95d5SJeremy L Thompson   code << "// s_B_[in,out]_i: Interpolation matrix, shared memory\n";
11734b3e95d5SJeremy L Thompson   code << "// s_G_[in,out]_i: Gradient matrix, shared memory\n";
11744b3e95d5SJeremy L Thompson   code << "// -----------------------------------------------------------------------------\n";
11754b3e95d5SJeremy L Thompson   code << "extern \"C\" __global__ void " << operator_name
11768b97b69aSJeremy L Thompson        << "(CeedInt num_elem, void* ctx, FieldsInt_Cuda indices, Fields_Cuda fields, Fields_Cuda B, Fields_Cuda G, CeedScalar *W, Points_Cuda "
11778b97b69aSJeremy L Thompson           "points) {\n";
11784b3e95d5SJeremy L Thompson 
11794b3e95d5SJeremy L Thompson   // Scratch buffers
11809e201c85SYohann   for (CeedInt i = 0; i < num_input_fields; i++) {
11814b3e95d5SJeremy L Thompson     CeedEvalMode eval_mode;
11824b3e95d5SJeremy L Thompson 
11832b730f8bSJeremy L Thompson     CeedCallBackend(CeedQFunctionFieldGetEvalMode(qf_input_fields[i], &eval_mode));
11849e201c85SYohann     if (eval_mode != CEED_EVAL_WEIGHT) {  // Skip CEED_EVAL_WEIGHT
11854b3e95d5SJeremy L Thompson       code << "  const CeedScalar *d_in_" << i << " = fields.inputs[" << i << "];\n";
1186241a4b83SYohann     }
1187241a4b83SYohann   }
11889e201c85SYohann   for (CeedInt i = 0; i < num_output_fields; i++) {
11894b3e95d5SJeremy L Thompson     code << "  CeedScalar *d_out_" << i << " = fields.outputs[" << i << "];\n";
1190241a4b83SYohann   }
11911da99368SJeremy L Thompson 
11929e201c85SYohann   code << "  const CeedInt dim = " << dim << ";\n";
1193dc007f05SJeremy L Thompson   code << "  const CeedInt " << (is_tensor ? "Q_1d" : "Q") << " = " << Q_1d << ";\n";
11948b97b69aSJeremy L Thompson   if (is_at_points) {
11958b97b69aSJeremy L Thompson     code << "  const CeedInt max_num_points = " << max_num_points << ";\n";
11968b97b69aSJeremy L Thompson     code << "  const CeedInt coords_comp_stride = " << coords_comp_stride << ";\n";
11978b97b69aSJeremy L Thompson   }
11981da99368SJeremy L Thompson 
11994b3e95d5SJeremy L Thompson   // Shared data
1200241a4b83SYohann   code << "  extern __shared__ CeedScalar slice[];\n";
12019e201c85SYohann   code << "  SharedData_Cuda data;\n";
12029e201c85SYohann   code << "  data.t_id_x = threadIdx.x;\n";
12039e201c85SYohann   code << "  data.t_id_y = threadIdx.y;\n";
12049e201c85SYohann   code << "  data.t_id_z = threadIdx.z;\n";
12059e201c85SYohann   code << "  data.t_id  = threadIdx.x + threadIdx.y*blockDim.x + threadIdx.z*blockDim.y*blockDim.x;\n";
1206dc007f05SJeremy L Thompson   code << "  data.slice = slice + data.t_id_z*T_1D" << ((!is_tensor || dim == 1) ? "" : "*T_1D") << ";\n";
1207920dcdc4Sjeremylt 
12080a2a6492SJeremy L Thompson   // -- Determine input mat reuse
120945a787f7SJeremy L Thompson   FieldReuse_Cuda input_matrix_reuse[CEED_FIELD_MAX];
12100a2a6492SJeremy L Thompson 
12110a2a6492SJeremy L Thompson   for (CeedInt i = 0; i < num_input_fields; i++) {
121245a787f7SJeremy L Thompson     input_matrix_reuse[i].index = -1;
12130a2a6492SJeremy L Thompson   }
12140a2a6492SJeremy L Thompson   for (CeedInt i = 0; i < num_input_fields; i++) {
12150a2a6492SJeremy L Thompson     CeedEvalMode eval_mode_i;
12160a2a6492SJeremy L Thompson     CeedBasis    basis_i;
12170a2a6492SJeremy L Thompson 
12180a2a6492SJeremy L Thompson     CeedCallBackend(CeedQFunctionFieldGetEvalMode(qf_input_fields[i], &eval_mode_i));
12190a2a6492SJeremy L Thompson     if (eval_mode_i == CEED_EVAL_WEIGHT) continue;
12200a2a6492SJeremy L Thompson     CeedCallBackend(CeedOperatorFieldGetBasis(op_input_fields[i], &basis_i));
122145a787f7SJeremy L Thompson     for (CeedInt j = 0; (input_matrix_reuse[i].index == -1) && (j < i); j++) {
12220a2a6492SJeremy L Thompson       CeedEvalMode eval_mode_j;
12230a2a6492SJeremy L Thompson       CeedBasis    basis_j;
12240a2a6492SJeremy L Thompson 
12250a2a6492SJeremy L Thompson       CeedCallBackend(CeedQFunctionFieldGetEvalMode(qf_input_fields[j], &eval_mode_j));
12260a2a6492SJeremy L Thompson       if (eval_mode_j == CEED_EVAL_WEIGHT) continue;
12270a2a6492SJeremy L Thompson       CeedCallBackend(CeedOperatorFieldGetBasis(op_input_fields[j], &basis_j));
12280a2a6492SJeremy L Thompson       if (basis_i == basis_j) {
12290a2a6492SJeremy L Thompson         if (is_tensor) {
123045a787f7SJeremy L Thompson           input_matrix_reuse[i].index     = j;
123145a787f7SJeremy L Thompson           input_matrix_reuse[i].is_input  = true;
123245a787f7SJeremy L Thompson           input_matrix_reuse[i].eval_mode = eval_mode_j;
12330a2a6492SJeremy L Thompson         } else {
12340a2a6492SJeremy L Thompson           // For non-tensor can only re-use with the same eval mode
12350a2a6492SJeremy L Thompson           if (eval_mode_i == eval_mode_j) {
123645a787f7SJeremy L Thompson             input_matrix_reuse[i].index     = j;
123745a787f7SJeremy L Thompson             input_matrix_reuse[i].is_input  = true;
123845a787f7SJeremy L Thompson             input_matrix_reuse[i].eval_mode = eval_mode_j;
12390a2a6492SJeremy L Thompson           }
12400a2a6492SJeremy L Thompson         }
12410a2a6492SJeremy L Thompson       }
12420a2a6492SJeremy L Thompson       CeedCallBackend(CeedBasisDestroy(&basis_j));
12430a2a6492SJeremy L Thompson     }
12440a2a6492SJeremy L Thompson     CeedCallBackend(CeedBasisDestroy(&basis_i));
12450a2a6492SJeremy L Thompson   }
12460a2a6492SJeremy L Thompson 
12470a2a6492SJeremy L Thompson   // -- Determine output mat reuse
124845a787f7SJeremy L Thompson   FieldReuse_Cuda output_matrix_reuse[CEED_FIELD_MAX];
12490a2a6492SJeremy L Thompson 
12500a2a6492SJeremy L Thompson   for (CeedInt i = 0; i < num_output_fields; i++) {
125145a787f7SJeremy L Thompson     output_matrix_reuse[i].index = -1;
12520a2a6492SJeremy L Thompson   }
12530a2a6492SJeremy L Thompson   for (CeedInt i = 0; i < num_output_fields; i++) {
12540a2a6492SJeremy L Thompson     CeedEvalMode eval_mode_i;
12550a2a6492SJeremy L Thompson     CeedBasis    basis_i;
12560a2a6492SJeremy L Thompson 
12570a2a6492SJeremy L Thompson     CeedCallBackend(CeedQFunctionFieldGetEvalMode(qf_output_fields[i], &eval_mode_i));
12580a2a6492SJeremy L Thompson     CeedCallBackend(CeedOperatorFieldGetBasis(op_output_fields[i], &basis_i));
125945a787f7SJeremy L Thompson     for (CeedInt j = 0; (output_matrix_reuse[i].index == -1) && (j < num_input_fields); j++) {
12600a2a6492SJeremy L Thompson       CeedEvalMode eval_mode_j;
12610a2a6492SJeremy L Thompson       CeedBasis    basis_j;
12620a2a6492SJeremy L Thompson 
12630a2a6492SJeremy L Thompson       CeedCallBackend(CeedQFunctionFieldGetEvalMode(qf_input_fields[j], &eval_mode_j));
12640a2a6492SJeremy L Thompson       if (eval_mode_j == CEED_EVAL_WEIGHT) continue;
12650a2a6492SJeremy L Thompson       CeedCallBackend(CeedOperatorFieldGetBasis(op_input_fields[j], &basis_j));
12660a2a6492SJeremy L Thompson       if (basis_i == basis_j) {
12670a2a6492SJeremy L Thompson         if (is_tensor) {
126845a787f7SJeremy L Thompson           output_matrix_reuse[i].index     = j;
126945a787f7SJeremy L Thompson           output_matrix_reuse[i].is_input  = true;
127045a787f7SJeremy L Thompson           output_matrix_reuse[i].eval_mode = eval_mode_j;
12710a2a6492SJeremy L Thompson         } else {
12720a2a6492SJeremy L Thompson           // For non-tensor can only re-use with the same eval mode
12730a2a6492SJeremy L Thompson           if (eval_mode_i == eval_mode_j) {
127445a787f7SJeremy L Thompson             output_matrix_reuse[i].index     = j;
127545a787f7SJeremy L Thompson             output_matrix_reuse[i].is_input  = true;
127645a787f7SJeremy L Thompson             output_matrix_reuse[i].eval_mode = eval_mode_j;
12770a2a6492SJeremy L Thompson           }
12780a2a6492SJeremy L Thompson         }
12790a2a6492SJeremy L Thompson       }
12800a2a6492SJeremy L Thompson       CeedCallBackend(CeedBasisDestroy(&basis_j));
12810a2a6492SJeremy L Thompson     }
128245a787f7SJeremy L Thompson     for (CeedInt j = 0; (output_matrix_reuse[i].index == -1) && (j < i); j++) {
12830a2a6492SJeremy L Thompson       CeedEvalMode eval_mode_j;
12840a2a6492SJeremy L Thompson       CeedBasis    basis_j;
12850a2a6492SJeremy L Thompson 
12860a2a6492SJeremy L Thompson       CeedCallBackend(CeedQFunctionFieldGetEvalMode(qf_output_fields[j], &eval_mode_j));
12870a2a6492SJeremy L Thompson       if (eval_mode_j == CEED_EVAL_WEIGHT) continue;
12880a2a6492SJeremy L Thompson       CeedCallBackend(CeedOperatorFieldGetBasis(op_output_fields[j], &basis_j));
12890a2a6492SJeremy L Thompson       if (basis_i == basis_j) {
12900a2a6492SJeremy L Thompson         if (is_tensor) {
129145a787f7SJeremy L Thompson           output_matrix_reuse[i].index     = j;
129245a787f7SJeremy L Thompson           output_matrix_reuse[i].is_input  = false;
129345a787f7SJeremy L Thompson           output_matrix_reuse[i].eval_mode = eval_mode_j;
12940a2a6492SJeremy L Thompson         } else {
12950a2a6492SJeremy L Thompson           // For non-tensor can only re-use with the same eval mode
12960a2a6492SJeremy L Thompson           if (eval_mode_i == eval_mode_j) {
129745a787f7SJeremy L Thompson             output_matrix_reuse[i].index     = j;
129845a787f7SJeremy L Thompson             output_matrix_reuse[i].is_input  = false;
129945a787f7SJeremy L Thompson             output_matrix_reuse[i].eval_mode = eval_mode_j;
13000a2a6492SJeremy L Thompson           }
13010a2a6492SJeremy L Thompson         }
13020a2a6492SJeremy L Thompson       }
13030a2a6492SJeremy L Thompson       CeedCallBackend(CeedBasisDestroy(&basis_j));
13040a2a6492SJeremy L Thompson     }
13050a2a6492SJeremy L Thompson     CeedCallBackend(CeedBasisDestroy(&basis_i));
13060a2a6492SJeremy L Thompson   }
13070a2a6492SJeremy L Thompson 
1308ac421f39SYohann   // Initialize constants, and matrices B and G
13094b3e95d5SJeremy L Thompson   code << "\n  // Input field constants and basis data\n";
13109e201c85SYohann   for (CeedInt i = 0; i < num_input_fields; i++) {
13110a2a6492SJeremy L Thompson     CeedCallBackend(CeedOperatorBuildKernelFieldData_Cuda_gen(code, data, i, op_input_fields[i], qf_input_fields[i], input_matrix_reuse[i], Q_1d,
13120a2a6492SJeremy L Thompson                                                               true, is_tensor, is_at_points, use_3d_slices));
1313920dcdc4Sjeremylt   }
13144b3e95d5SJeremy L Thompson   code << "\n  // Output field constants and basis data\n";
13159e201c85SYohann   for (CeedInt i = 0; i < num_output_fields; i++) {
13160a2a6492SJeremy L Thompson     CeedCallBackend(CeedOperatorBuildKernelFieldData_Cuda_gen(code, data, i, op_output_fields[i], qf_output_fields[i], output_matrix_reuse[i], Q_1d,
13170a2a6492SJeremy L Thompson                                                               false, is_tensor, is_at_points, use_3d_slices));
13184b3e95d5SJeremy L Thompson   }
1319920dcdc4Sjeremylt 
13204b3e95d5SJeremy L Thompson   // Loop over all elements
13214b3e95d5SJeremy L Thompson   code << "\n  // Element loop\n";
1322ac421f39SYohann   code << "  __syncthreads();\n";
13239e201c85SYohann   code << "  for (CeedInt elem = blockIdx.x*blockDim.z + threadIdx.z; elem < num_elem; elem += gridDim.x*blockDim.z) {\n";
13244b3e95d5SJeremy L Thompson 
1325e93651e5SJeremy L Thompson   // -- Compute minimum buffer space needed
13268b97b69aSJeremy L Thompson   CeedInt max_rstr_buffer_size = 1;
1327e93651e5SJeremy L Thompson 
13289e201c85SYohann   for (CeedInt i = 0; i < num_input_fields; i++) {
1329e93651e5SJeremy L Thompson     CeedInt             num_comp, elem_size;
1330e93651e5SJeremy L Thompson     CeedElemRestriction elem_rstr;
1331e93651e5SJeremy L Thompson 
1332e93651e5SJeremy L Thompson     CeedCallBackend(CeedOperatorFieldGetElemRestriction(op_input_fields[i], &elem_rstr));
1333e93651e5SJeremy L Thompson     CeedCallBackend(CeedElemRestrictionGetNumComponents(elem_rstr, &num_comp));
1334e93651e5SJeremy L Thompson     CeedCallBackend(CeedElemRestrictionGetElementSize(elem_rstr, &elem_size));
1335dc007f05SJeremy L Thompson     max_rstr_buffer_size = CeedIntMax(max_rstr_buffer_size, num_comp * (is_tensor && (dim >= 3) ? elem_size : 1));
1336681d0ea7SJeremy L Thompson     CeedCallBackend(CeedElemRestrictionDestroy(&elem_rstr));
1337e93651e5SJeremy L Thompson   }
1338e93651e5SJeremy L Thompson   for (CeedInt i = 0; i < num_output_fields; i++) {
1339e93651e5SJeremy L Thompson     CeedInt             num_comp, elem_size;
1340e93651e5SJeremy L Thompson     CeedElemRestriction elem_rstr;
1341e93651e5SJeremy L Thompson 
1342e93651e5SJeremy L Thompson     CeedCallBackend(CeedOperatorFieldGetElemRestriction(op_output_fields[i], &elem_rstr));
1343e93651e5SJeremy L Thompson     CeedCallBackend(CeedElemRestrictionGetNumComponents(elem_rstr, &num_comp));
1344e93651e5SJeremy L Thompson     CeedCallBackend(CeedElemRestrictionGetElementSize(elem_rstr, &elem_size));
1345dc007f05SJeremy L Thompson     max_rstr_buffer_size = CeedIntMax(max_rstr_buffer_size, num_comp * (is_tensor && (dim >= 3) ? elem_size : 1));
1346681d0ea7SJeremy L Thompson     CeedCallBackend(CeedElemRestrictionDestroy(&elem_rstr));
1347e93651e5SJeremy L Thompson   }
1348e93651e5SJeremy L Thompson   code << "    // Scratch restriction buffer space\n";
1349e93651e5SJeremy L Thompson   code << "    CeedScalar r_e_scratch[" << max_rstr_buffer_size << "];\n";
1350e93651e5SJeremy L Thompson 
1351e93651e5SJeremy L Thompson   // -- Determine best input field processing order
1352e93651e5SJeremy L Thompson   CeedInt field_rstr_in_buffer[CEED_FIELD_MAX], input_field_order[CEED_FIELD_MAX];
1353e93651e5SJeremy L Thompson 
1354e93651e5SJeremy L Thompson   for (CeedInt i = 0; i < num_input_fields; i++) {
1355e93651e5SJeremy L Thompson     field_rstr_in_buffer[i] = -1;
1356e93651e5SJeremy L Thompson     input_field_order[i]    = -1;
1357e93651e5SJeremy L Thompson   }
1358e93651e5SJeremy L Thompson   {
1359e93651e5SJeremy L Thompson     bool    is_ordered[CEED_FIELD_MAX];
1360e93651e5SJeremy L Thompson     CeedInt curr_index = 0;
1361e93651e5SJeremy L Thompson 
1362e93651e5SJeremy L Thompson     for (CeedInt i = 0; i < num_input_fields; i++) is_ordered[i] = false;
1363e93651e5SJeremy L Thompson     for (CeedInt i = 0; i < num_input_fields; i++) {
1364e93651e5SJeremy L Thompson       CeedVector          vec_i;
1365e93651e5SJeremy L Thompson       CeedElemRestriction rstr_i;
1366e93651e5SJeremy L Thompson 
1367e93651e5SJeremy L Thompson       if (is_ordered[i]) continue;
1368e93651e5SJeremy L Thompson       field_rstr_in_buffer[i]       = i;
1369e93651e5SJeremy L Thompson       is_ordered[i]                 = true;
1370e93651e5SJeremy L Thompson       input_field_order[curr_index] = i;
1371e93651e5SJeremy L Thompson       curr_index++;
1372034f99fdSJeremy L Thompson       CeedCallBackend(CeedOperatorFieldGetVector(op_input_fields[i], &vec_i));
1373e93651e5SJeremy L Thompson       if (vec_i == CEED_VECTOR_NONE) continue;  // CEED_EVAL_WEIGHT
1374e93651e5SJeremy L Thompson       CeedCallBackend(CeedOperatorFieldGetElemRestriction(op_input_fields[i], &rstr_i));
1375e93651e5SJeremy L Thompson       for (CeedInt j = i + 1; j < num_input_fields; j++) {
1376e93651e5SJeremy L Thompson         CeedVector          vec_j;
1377e93651e5SJeremy L Thompson         CeedElemRestriction rstr_j;
1378e93651e5SJeremy L Thompson 
1379e93651e5SJeremy L Thompson         CeedCallBackend(CeedOperatorFieldGetVector(op_input_fields[j], &vec_j));
1380e93651e5SJeremy L Thompson         CeedCallBackend(CeedOperatorFieldGetElemRestriction(op_input_fields[j], &rstr_j));
1381e93651e5SJeremy L Thompson         if (rstr_i == rstr_j && vec_i == vec_j) {
1382e93651e5SJeremy L Thompson           field_rstr_in_buffer[j]       = i;
1383e93651e5SJeremy L Thompson           is_ordered[j]                 = true;
1384e93651e5SJeremy L Thompson           input_field_order[curr_index] = j;
1385e93651e5SJeremy L Thompson           curr_index++;
1386e93651e5SJeremy L Thompson         }
1387681d0ea7SJeremy L Thompson         CeedCallBackend(CeedVectorDestroy(&vec_j));
1388681d0ea7SJeremy L Thompson         CeedCallBackend(CeedElemRestrictionDestroy(&rstr_j));
1389e93651e5SJeremy L Thompson       }
1390681d0ea7SJeremy L Thompson       CeedCallBackend(CeedVectorDestroy(&vec_i));
1391681d0ea7SJeremy L Thompson       CeedCallBackend(CeedElemRestrictionDestroy(&rstr_i));
1392e93651e5SJeremy L Thompson     }
1393e93651e5SJeremy L Thompson   }
1394e93651e5SJeremy L Thompson 
1395e93651e5SJeremy L Thompson   // -- Input restriction and basis
1396e93651e5SJeremy L Thompson   code << "\n    // -- Input field restrictions and basis actions\n";
1397e93651e5SJeremy L Thompson   for (CeedInt i = 0; i < num_input_fields; i++) {
1398*59fa3f92SJeremy L Thompson     const char   *field_name;
1399*59fa3f92SJeremy L Thompson     const CeedInt f = input_field_order[i];
1400e93651e5SJeremy L Thompson 
1401*59fa3f92SJeremy L Thompson     CeedCallBackend(CeedOperatorFieldGetName(op_input_fields[f], &field_name));
1402*59fa3f92SJeremy L Thompson     code << "    // ---- Input field " << f << ": " << field_name << "\n";
1403920dcdc4Sjeremylt 
14044b3e95d5SJeremy L Thompson     // ---- Restriction
1405e93651e5SJeremy L Thompson     CeedCallBackend(CeedOperatorBuildKernelRestriction_Cuda_gen(code, data, f, dim, field_rstr_in_buffer, op_input_fields[f], qf_input_fields[f],
1406dc007f05SJeremy L Thompson                                                                 Q_1d, true, is_tensor, is_at_points, use_3d_slices));
14079a2291e3SJeremy L Thompson 
14084b3e95d5SJeremy L Thompson     // ---- Basis action
1409dc007f05SJeremy L Thompson     CeedCallBackend(CeedOperatorBuildKernelBasis_Cuda_gen(code, data, f, dim, op_input_fields[f], qf_input_fields[f], Q_1d, true, is_tensor,
1410dc007f05SJeremy L Thompson                                                           is_at_points, use_3d_slices));
1411920dcdc4Sjeremylt   }
1412920dcdc4Sjeremylt 
14134b3e95d5SJeremy L Thompson   // -- Q function
14148b97b69aSJeremy L Thompson   CeedCallBackend(CeedOperatorBuildKernelQFunction_Cuda_gen(code, data, dim, max_num_points, num_input_fields, op_input_fields, qf_input_fields,
1415dc007f05SJeremy L Thompson                                                             num_output_fields, op_output_fields, qf_output_fields, qfunction_name, Q_1d, is_tensor,
1416dc007f05SJeremy L Thompson                                                             is_at_points, use_3d_slices));
1417ca735530SJeremy L Thompson 
14184b3e95d5SJeremy L Thompson   // -- Output basis and restriction
14194b3e95d5SJeremy L Thompson   code << "\n    // -- Output field basis action and restrictions\n";
14209e201c85SYohann   for (CeedInt i = 0; i < num_output_fields; i++) {
1421*59fa3f92SJeremy L Thompson     const char *field_name;
1422*59fa3f92SJeremy L Thompson 
1423*59fa3f92SJeremy L Thompson     CeedCallBackend(CeedOperatorFieldGetName(op_output_fields[i], &field_name));
1424*59fa3f92SJeremy L Thompson     code << "    // ---- Output field " << i << ": " << field_name << "\n";
1425ca735530SJeremy L Thompson 
14264b3e95d5SJeremy L Thompson     // ---- Basis action
1427dc007f05SJeremy L Thompson     CeedCallBackend(CeedOperatorBuildKernelBasis_Cuda_gen(code, data, i, dim, op_output_fields[i], qf_output_fields[i], Q_1d, false, is_tensor,
1428dc007f05SJeremy L Thompson                                                           is_at_points, use_3d_slices));
14299a2291e3SJeremy L Thompson 
14304b3e95d5SJeremy L Thompson     // ---- Restriction
14318b97b69aSJeremy L Thompson     CeedCallBackend(CeedOperatorBuildKernelRestriction_Cuda_gen(code, data, i, dim, NULL, op_output_fields[i], qf_output_fields[i], Q_1d, false,
1432dc007f05SJeremy L Thompson                                                                 is_tensor, is_at_points, use_3d_slices));
1433ac421f39SYohann   }
1434241a4b83SYohann 
14354b3e95d5SJeremy L Thompson   // Close loop and function
1436241a4b83SYohann   code << "  }\n";
1437818e0025SJeremy L Thompson   code << "}\n";
1438818e0025SJeremy L Thompson   code << "// -----------------------------------------------------------------------------\n\n";
1439241a4b83SYohann 
14403a2968d6SJeremy L Thompson   // Compile
1441ddae5012SJeremy L Thompson   {
1442ddae5012SJeremy L Thompson     bool is_compile_good = false;
1443ddae5012SJeremy L Thompson 
1444ddae5012SJeremy L Thompson     CeedCallBackend(CeedTryCompile_Cuda(ceed, code.str().c_str(), &is_compile_good, &data->module, 1, "T_1D", CeedIntMax(Q_1d, data->max_P_1d)));
1445ddae5012SJeremy L Thompson     if (is_compile_good) {
1446ddae5012SJeremy L Thompson       *is_good_build = true;
1447eb7e6cafSJeremy L Thompson       CeedCallBackend(CeedGetKernel_Cuda(ceed, data->module, operator_name.c_str(), &data->op));
1448ddae5012SJeremy L Thompson     } else {
1449ddae5012SJeremy L Thompson       *is_good_build     = false;
1450ddae5012SJeremy L Thompson       data->use_fallback = true;
1451ddae5012SJeremy L Thompson     }
1452ddae5012SJeremy L Thompson   }
14532b730f8bSJeremy L Thompson   CeedCallBackend(CeedOperatorSetSetupDone(op));
14549bc66399SJeremy L Thompson   CeedCallBackend(CeedDestroy(&ceed));
1455c11e12f4SJeremy L Thompson   CeedCallBackend(CeedQFunctionDestroy(&qf));
1456e15f9bd0SJeremy L Thompson   return CEED_ERROR_SUCCESS;
1457241a4b83SYohann }
14582a86cc9dSSebastian Grimberg 
1459ab213215SJeremy L Thompson //------------------------------------------------------------------------------
1460