xref: /libCEED/backends/cuda-gen/ceed-cuda-gen-operator-build.cpp (revision 5a5594ffd3205667210320a8dc336d609d13e75f)
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 
25ab213215SJeremy L Thompson //------------------------------------------------------------------------------
264b3e95d5SJeremy L Thompson // Determine type of operator
274b3e95d5SJeremy L Thompson //------------------------------------------------------------------------------
284b3e95d5SJeremy L Thompson static int CeedOperatorBuildKernelData_Cuda_gen(Ceed ceed, CeedInt num_input_fields, CeedOperatorField *op_input_fields,
294b3e95d5SJeremy L Thompson                                                 CeedQFunctionField *qf_input_fields, CeedInt num_output_fields, CeedOperatorField *op_output_fields,
304b3e95d5SJeremy L Thompson                                                 CeedQFunctionField *qf_output_fields, CeedInt *max_P_1d, CeedInt *Q_1d, CeedInt *dim, bool *is_tensor,
314b3e95d5SJeremy L Thompson                                                 bool *use_3d_slices) {
324b3e95d5SJeremy L Thompson   // Find dim, P_1d, Q_1d
334b3e95d5SJeremy L Thompson   *max_P_1d  = 0;
344b3e95d5SJeremy L Thompson   *Q_1d      = 0;
354b3e95d5SJeremy L Thompson   *dim       = 0;
364b3e95d5SJeremy L Thompson   *is_tensor = true;
374b3e95d5SJeremy L Thompson   for (CeedInt i = 0; i < num_input_fields; i++) {
384b3e95d5SJeremy L Thompson     CeedBasis basis;
394b3e95d5SJeremy L Thompson 
404b3e95d5SJeremy L Thompson     CeedCallBackend(CeedOperatorFieldGetBasis(op_input_fields[i], &basis));
414b3e95d5SJeremy L Thompson     if (basis != CEED_BASIS_NONE) {
424b3e95d5SJeremy L Thompson       bool    is_field_tensor;
434b3e95d5SJeremy L Thompson       CeedInt field_P_1d = 0, field_Q_1d = 0, field_dim = 0;
444b3e95d5SJeremy L Thompson 
454b3e95d5SJeremy L Thompson       // Collect dim, P_1d, and Q_1d
464b3e95d5SJeremy L Thompson       CeedCallBackend(CeedBasisIsTensor(basis, &is_field_tensor));
474b3e95d5SJeremy L Thompson       CeedCheck(is_field_tensor, ceed, CEED_ERROR_BACKEND, "Backend does not implement operators with non-tensor basis");
484b3e95d5SJeremy L Thompson       *is_tensor = *is_tensor && is_field_tensor;
494b3e95d5SJeremy L Thompson       CeedCallBackend(CeedBasisGetNumNodes1D(basis, &field_P_1d));
504b3e95d5SJeremy L Thompson       *max_P_1d = CeedIntMax(*max_P_1d, field_P_1d);
514b3e95d5SJeremy L Thompson       CeedCallBackend(CeedBasisGetDimension(basis, &field_dim));
524b3e95d5SJeremy L Thompson       CeedCheck(*dim == 0 || field_dim == *dim, ceed, CEED_ERROR_BACKEND, "Quadrature spaces must be compatible");
534b3e95d5SJeremy L Thompson       *dim = field_dim;
544b3e95d5SJeremy L Thompson       CeedCallBackend(CeedBasisGetNumQuadraturePoints1D(basis, &field_Q_1d));
554b3e95d5SJeremy L Thompson       CeedCheck(*Q_1d == 0 || field_Q_1d == *Q_1d, ceed, CEED_ERROR_BACKEND, "Quadrature spaces must be compatible");
564b3e95d5SJeremy L Thompson       *Q_1d = field_Q_1d;
574b3e95d5SJeremy L Thompson     }
584b3e95d5SJeremy L Thompson   }
594b3e95d5SJeremy L Thompson   for (CeedInt i = 0; i < num_output_fields; i++) {
604b3e95d5SJeremy L Thompson     CeedBasis basis;
614b3e95d5SJeremy L Thompson 
624b3e95d5SJeremy L Thompson     CeedCallBackend(CeedOperatorFieldGetBasis(op_output_fields[i], &basis));
634b3e95d5SJeremy L Thompson     if (basis != CEED_BASIS_NONE) {
644b3e95d5SJeremy L Thompson       bool    is_field_tensor;
654b3e95d5SJeremy L Thompson       CeedInt field_P_1d = 0, field_Q_1d = 0, field_dim = 0;
664b3e95d5SJeremy L Thompson 
674b3e95d5SJeremy L Thompson       // Collect dim, P_1d, and Q_1d
684b3e95d5SJeremy L Thompson       CeedCallBackend(CeedBasisIsTensor(basis, &is_field_tensor));
694b3e95d5SJeremy L Thompson       CeedCheck(is_field_tensor, ceed, CEED_ERROR_BACKEND, "Backend does not implement operators with non-tensor basis");
704b3e95d5SJeremy L Thompson       *is_tensor = *is_tensor && is_field_tensor;
714b3e95d5SJeremy L Thompson       CeedCallBackend(CeedBasisGetNumNodes1D(basis, &field_P_1d));
724b3e95d5SJeremy L Thompson       *max_P_1d = CeedIntMax(*max_P_1d, field_P_1d);
734b3e95d5SJeremy L Thompson       CeedCallBackend(CeedBasisGetDimension(basis, &field_dim));
744b3e95d5SJeremy L Thompson       CeedCheck(*dim == 0 || field_dim == *dim, ceed, CEED_ERROR_BACKEND, "Quadrature spaces must be compatible");
754b3e95d5SJeremy L Thompson       *dim = field_dim;
764b3e95d5SJeremy L Thompson       CeedCallBackend(CeedBasisGetNumQuadraturePoints1D(basis, &field_Q_1d));
774b3e95d5SJeremy L Thompson       CeedCheck(*Q_1d == 0 || field_Q_1d == *Q_1d, ceed, CEED_ERROR_BACKEND, "Quadrature spaces must be compatible");
784b3e95d5SJeremy L Thompson       *Q_1d = field_Q_1d;
794b3e95d5SJeremy L Thompson     }
804b3e95d5SJeremy L Thompson   }
814b3e95d5SJeremy L Thompson 
824b3e95d5SJeremy L Thompson   // Only use 3D collocated gradient parallelization strategy when gradient is computed
834b3e95d5SJeremy L Thompson   *use_3d_slices = false;
844b3e95d5SJeremy L Thompson   if (*dim == 3) {
854b3e95d5SJeremy L Thompson     bool was_grad_found = false;
864b3e95d5SJeremy L Thompson 
874b3e95d5SJeremy L Thompson     for (CeedInt i = 0; i < num_input_fields; i++) {
884b3e95d5SJeremy L Thompson       CeedEvalMode eval_mode;
894b3e95d5SJeremy L Thompson 
904b3e95d5SJeremy L Thompson       CeedCallBackend(CeedQFunctionFieldGetEvalMode(qf_input_fields[i], &eval_mode));
914b3e95d5SJeremy L Thompson       if (eval_mode == CEED_EVAL_GRAD) {
924b3e95d5SJeremy L Thompson         CeedBasis_Cuda_shared *basis_data;
934b3e95d5SJeremy L Thompson         CeedBasis              basis;
944b3e95d5SJeremy L Thompson 
954b3e95d5SJeremy L Thompson         CeedCallBackend(CeedOperatorFieldGetBasis(op_input_fields[i], &basis));
964b3e95d5SJeremy L Thompson         CeedCallBackend(CeedBasisGetData(basis, &basis_data));
974b3e95d5SJeremy L Thompson         *use_3d_slices = basis_data->d_collo_grad_1d && (was_grad_found ? *use_3d_slices : true);
984b3e95d5SJeremy L Thompson         was_grad_found = true;
994b3e95d5SJeremy L Thompson       }
1004b3e95d5SJeremy L Thompson     }
1014b3e95d5SJeremy L Thompson     for (CeedInt i = 0; i < num_output_fields; i++) {
1024b3e95d5SJeremy L Thompson       CeedEvalMode eval_mode;
1034b3e95d5SJeremy L Thompson 
1044b3e95d5SJeremy L Thompson       CeedCallBackend(CeedQFunctionFieldGetEvalMode(qf_output_fields[i], &eval_mode));
1054b3e95d5SJeremy L Thompson       if (eval_mode == CEED_EVAL_GRAD) {
1064b3e95d5SJeremy L Thompson         CeedBasis_Cuda_shared *basis_data;
1074b3e95d5SJeremy L Thompson         CeedBasis              basis;
1084b3e95d5SJeremy L Thompson 
1094b3e95d5SJeremy L Thompson         CeedCallBackend(CeedOperatorFieldGetBasis(op_output_fields[i], &basis));
1104b3e95d5SJeremy L Thompson         CeedCallBackend(CeedBasisGetData(basis, &basis_data));
1114b3e95d5SJeremy L Thompson         *use_3d_slices = basis_data->d_collo_grad_1d && (was_grad_found ? *use_3d_slices : true);
1124b3e95d5SJeremy L Thompson         was_grad_found = true;
1134b3e95d5SJeremy L Thompson       }
1144b3e95d5SJeremy L Thompson     }
1154b3e95d5SJeremy L Thompson   }
1164b3e95d5SJeremy L Thompson   return CEED_ERROR_SUCCESS;
1174b3e95d5SJeremy L Thompson }
1184b3e95d5SJeremy L Thompson 
1194b3e95d5SJeremy L Thompson //------------------------------------------------------------------------------
1204b3e95d5SJeremy L Thompson // Setup fields
1214b3e95d5SJeremy L Thompson //------------------------------------------------------------------------------
1224b3e95d5SJeremy L Thompson static int CeedOperatorBuildKernelFieldData_Cuda_gen(std::ostringstream &code, CeedOperator_Cuda_gen *data, CeedInt i, CeedOperatorField op_field,
1234b3e95d5SJeremy L Thompson                                                      CeedQFunctionField qf_field, CeedInt Q_1d, bool is_input, bool use_3d_slices) {
1244b3e95d5SJeremy L Thompson   std::string            var_suffix = (is_input ? "_in_" : "_out_") + std::to_string(i);
1254b3e95d5SJeremy L Thompson   std::string            P_name = "P_1d" + var_suffix, Q_name = "Q_1d";
1264b3e95d5SJeremy L Thompson   std::string            option_name = (is_input ? "inputs" : "outputs");
1274b3e95d5SJeremy L Thompson   CeedEvalMode           eval_mode   = CEED_EVAL_NONE;
1284b3e95d5SJeremy L Thompson   CeedInt                elem_size = 0, num_comp = 0, P_1d = 0;
1294b3e95d5SJeremy L Thompson   CeedElemRestriction    elem_rstr;
1304b3e95d5SJeremy L Thompson   CeedBasis_Cuda_shared *basis_data;
1314b3e95d5SJeremy L Thompson   CeedBasis              basis;
1324b3e95d5SJeremy L Thompson 
1334b3e95d5SJeremy L Thompson   code << "  // -- " << (is_input ? "Input" : "Output") << " field " << i << "\n";
1344b3e95d5SJeremy L Thompson 
1354b3e95d5SJeremy L Thompson   // Get field data
1364b3e95d5SJeremy L Thompson   CeedCallBackend(CeedOperatorFieldGetElemRestriction(op_field, &elem_rstr));
1374b3e95d5SJeremy L Thompson   if (elem_rstr != CEED_ELEMRESTRICTION_NONE) {
1384b3e95d5SJeremy L Thompson     CeedCallBackend(CeedElemRestrictionGetElementSize(elem_rstr, &elem_size));
1394b3e95d5SJeremy L Thompson     CeedCallBackend(CeedElemRestrictionGetNumComponents(elem_rstr, &num_comp));
1404b3e95d5SJeremy L Thompson   }
1414b3e95d5SJeremy L Thompson   CeedCallBackend(CeedOperatorFieldGetBasis(op_field, &basis));
1424b3e95d5SJeremy L Thompson   if (basis != CEED_BASIS_NONE) {
1434b3e95d5SJeremy L Thompson     CeedCallBackend(CeedBasisGetData(basis, &basis_data));
1444b3e95d5SJeremy L Thompson     CeedCallBackend(CeedBasisGetNumNodes1D(basis, &P_1d));
1454b3e95d5SJeremy L Thompson   }
1464b3e95d5SJeremy L Thompson   CeedCallBackend(CeedQFunctionFieldGetEvalMode(qf_field, &eval_mode));
1474b3e95d5SJeremy L Thompson 
1484b3e95d5SJeremy L Thompson   // Set field constants
1494b3e95d5SJeremy L Thompson   if (eval_mode != CEED_EVAL_WEIGHT) {
1504b3e95d5SJeremy L Thompson     code << "  const CeedInt " << P_name << " = " << (basis == CEED_BASIS_NONE ? Q_1d : P_1d) << ";\n";
1514b3e95d5SJeremy L Thompson     code << "  const CeedInt num_comp" << var_suffix << " = " << num_comp << ";\n";
1524b3e95d5SJeremy L Thompson   }
1534b3e95d5SJeremy L Thompson 
1544b3e95d5SJeremy L Thompson   // Load basis data
1554b3e95d5SJeremy L Thompson   code << "  // EvalMode: " << CeedEvalModes[eval_mode] << "\n";
1564b3e95d5SJeremy L Thompson   switch (eval_mode) {
1574b3e95d5SJeremy L Thompson     case CEED_EVAL_NONE:
1584b3e95d5SJeremy L Thompson       break;
1594b3e95d5SJeremy L Thompson     case CEED_EVAL_INTERP:
1604b3e95d5SJeremy L Thompson       if (is_input) data->B.inputs[i] = basis_data->d_interp_1d;
1614b3e95d5SJeremy L Thompson       else data->B.outputs[i] = basis_data->d_interp_1d;
1624b3e95d5SJeremy L Thompson       code << "  __shared__ CeedScalar s_B" << var_suffix << "[" << P_1d * Q_1d << "];\n";
1634b3e95d5SJeremy L Thompson       code << "  loadMatrix<" << P_name << ", " << Q_name << ">(data, B." << option_name << "[" << i << "], s_B" << var_suffix << ");\n";
1644b3e95d5SJeremy L Thompson       break;
1654b3e95d5SJeremy L Thompson     case CEED_EVAL_GRAD:
1664b3e95d5SJeremy L Thompson       if (is_input) data->B.inputs[i] = basis_data->d_interp_1d;
1674b3e95d5SJeremy L Thompson       else data->B.outputs[i] = basis_data->d_interp_1d;
1684b3e95d5SJeremy L Thompson       code << "  __shared__ CeedScalar s_B" << var_suffix << "[" << P_1d * Q_1d << "];\n";
1694b3e95d5SJeremy L Thompson       code << "  loadMatrix<" << P_name << ", " << Q_name << ">(data, B." << option_name << "[" << i << "], s_B" << var_suffix << ");\n";
1704b3e95d5SJeremy L Thompson       if (use_3d_slices) {
1714b3e95d5SJeremy L Thompson         if (is_input) data->G.inputs[i] = basis_data->d_collo_grad_1d;
1724b3e95d5SJeremy L Thompson         else data->G.outputs[i] = basis_data->d_collo_grad_1d;
1734b3e95d5SJeremy L Thompson         code << "  __shared__ CeedScalar s_G" << var_suffix << "[" << Q_1d * Q_1d << "];\n";
1744b3e95d5SJeremy L Thompson         code << "  loadMatrix<" << Q_name << ", " << Q_name << ">(data, G." << option_name << "[" << i << "], s_G" << var_suffix << ");\n";
1754b3e95d5SJeremy L Thompson       } else {
1764b3e95d5SJeremy L Thompson         bool has_collo_grad = basis_data->d_collo_grad_1d;
1774b3e95d5SJeremy L Thompson 
1784b3e95d5SJeremy L Thompson         if (is_input) data->G.inputs[i] = has_collo_grad ? basis_data->d_collo_grad_1d : basis_data->d_grad_1d;
1794b3e95d5SJeremy L Thompson         else data->G.outputs[i] = has_collo_grad ? basis_data->d_collo_grad_1d : basis_data->d_grad_1d;
1804b3e95d5SJeremy L Thompson         if (has_collo_grad) {
1814b3e95d5SJeremy L Thompson           code << "  __shared__ CeedScalar s_G" << var_suffix << "[" << Q_1d * Q_1d << "];\n";
1824b3e95d5SJeremy L Thompson           code << "  loadMatrix<" << Q_name << ", " << Q_name << ">(data, G." << option_name << "[" << i << "], s_G" << var_suffix << ");\n";
1834b3e95d5SJeremy L Thompson         } else {
1844b3e95d5SJeremy L Thompson           code << "  __shared__ CeedScalar s_G" << var_suffix << "[" << Q_1d * P_1d << "];\n";
1854b3e95d5SJeremy L Thompson           code << "  loadMatrix<" << P_name << ", " << Q_name << ">(data, G." << option_name << "[" << i << "], s_G" << var_suffix << ");\n";
1864b3e95d5SJeremy L Thompson         }
1874b3e95d5SJeremy L Thompson       }
1884b3e95d5SJeremy L Thompson       break;
1894b3e95d5SJeremy L Thompson     case CEED_EVAL_WEIGHT:
1904b3e95d5SJeremy L Thompson       break;  // No action
1914b3e95d5SJeremy L Thompson       // LCOV_EXCL_START
1924b3e95d5SJeremy L Thompson     case CEED_EVAL_DIV:
1934b3e95d5SJeremy L Thompson       break;  // TODO: Not implemented
1944b3e95d5SJeremy L Thompson     case CEED_EVAL_CURL:
1954b3e95d5SJeremy L Thompson       break;  // TODO: Not implemented
1964b3e95d5SJeremy L Thompson               // LCOV_EXCL_STOP
1974b3e95d5SJeremy L Thompson   }
1984b3e95d5SJeremy L Thompson   return CEED_ERROR_SUCCESS;
1994b3e95d5SJeremy L Thompson }
2004b3e95d5SJeremy L Thompson 
2014b3e95d5SJeremy L Thompson //------------------------------------------------------------------------------
2024b3e95d5SJeremy L Thompson // Restriction
2034b3e95d5SJeremy L Thompson //------------------------------------------------------------------------------
2044b3e95d5SJeremy L Thompson static int CeedOperatorBuildKernelRestriction_Cuda_gen(std::ostringstream &code, CeedOperator_Cuda_gen *data, CeedInt i, CeedInt dim,
2054b3e95d5SJeremy L Thompson                                                        CeedOperatorField op_field, CeedQFunctionField qf_field, CeedInt Q_1d, bool is_input,
2064b3e95d5SJeremy L Thompson                                                        bool use_3d_slices) {
2074b3e95d5SJeremy L Thompson   std::string               var_suffix = (is_input ? "_in_" : "_out_") + std::to_string(i);
2084b3e95d5SJeremy L Thompson   std::string               P_name     = "P_1d" + var_suffix;
2094b3e95d5SJeremy L Thompson   CeedEvalMode              eval_mode  = CEED_EVAL_NONE;
2104b3e95d5SJeremy L Thompson   CeedInt                   elem_size = 0, num_comp = 0, P_1d = 0;
2114b3e95d5SJeremy L Thompson   CeedSize                  l_size;
2124b3e95d5SJeremy L Thompson   CeedElemRestriction_Cuda *rstr_data;
2134b3e95d5SJeremy L Thompson   CeedElemRestriction       elem_rstr;
2144b3e95d5SJeremy L Thompson   CeedBasis                 basis;
2154b3e95d5SJeremy L Thompson 
2164b3e95d5SJeremy L Thompson   // Get field data
2174b3e95d5SJeremy L Thompson   CeedCallBackend(CeedOperatorFieldGetElemRestriction(op_field, &elem_rstr));
2184b3e95d5SJeremy L Thompson   if (elem_rstr != CEED_ELEMRESTRICTION_NONE) {
2194b3e95d5SJeremy L Thompson     CeedCallBackend(CeedElemRestrictionGetElementSize(elem_rstr, &elem_size));
2204b3e95d5SJeremy L Thompson     CeedCallBackend(CeedElemRestrictionGetNumComponents(elem_rstr, &num_comp));
2214b3e95d5SJeremy L Thompson     CeedCallBackend(CeedElemRestrictionGetData(elem_rstr, &rstr_data));
2224b3e95d5SJeremy L Thompson   }
2234b3e95d5SJeremy L Thompson   CeedCallBackend(CeedOperatorFieldGetBasis(op_field, &basis));
2244b3e95d5SJeremy L Thompson   if (basis != CEED_BASIS_NONE) {
2254b3e95d5SJeremy L Thompson     CeedCallBackend(CeedBasisGetNumNodes1D(basis, &P_1d));
2264b3e95d5SJeremy L Thompson   }
2274b3e95d5SJeremy L Thompson   CeedCallBackend(CeedQFunctionFieldGetEvalMode(qf_field, &eval_mode));
2284b3e95d5SJeremy L Thompson 
2294b3e95d5SJeremy L Thompson   // Restriction
2304b3e95d5SJeremy L Thompson   if (is_input) {
2314b3e95d5SJeremy L Thompson     // Input
2324b3e95d5SJeremy L Thompson     if (eval_mode != CEED_EVAL_WEIGHT && !((eval_mode == CEED_EVAL_NONE) && use_3d_slices)) {
2334b3e95d5SJeremy L Thompson       bool is_strided;
2344b3e95d5SJeremy L Thompson 
2354b3e95d5SJeremy L Thompson       code << "    CeedScalar r_e" << var_suffix << "[num_comp" << var_suffix << "*" << P_name << "];\n";
2364b3e95d5SJeremy L Thompson       CeedCallBackend(CeedElemRestrictionIsStrided(elem_rstr, &is_strided));
2374b3e95d5SJeremy L Thompson       if (!is_strided) {
2384b3e95d5SJeremy L Thompson         CeedInt comp_stride;
2394b3e95d5SJeremy L Thompson 
2404b3e95d5SJeremy L Thompson         CeedCallBackend(CeedElemRestrictionGetLVectorSize(elem_rstr, &l_size));
2414b3e95d5SJeremy L Thompson         code << "    const CeedInt l_size" << var_suffix << " = " << l_size << ";\n";
2424b3e95d5SJeremy L Thompson         CeedCallBackend(CeedElemRestrictionGetCompStride(elem_rstr, &comp_stride));
2434b3e95d5SJeremy L Thompson         code << "    // CompStride: " << comp_stride << "\n";
2444b3e95d5SJeremy L Thompson         data->indices.inputs[i] = (CeedInt *)rstr_data->d_offsets;
2454b3e95d5SJeremy L Thompson         code << "    readDofsOffset" << dim << "d<num_comp" << var_suffix << ", " << comp_stride << ", " << P_name << ">(data, l_size" << var_suffix
2464b3e95d5SJeremy L Thompson              << ", elem, indices.inputs[" << i << "], d" << var_suffix << ", r_e" << var_suffix << ");\n";
2474b3e95d5SJeremy L Thompson       } else {
2484b3e95d5SJeremy L Thompson         bool    has_backend_strides;
2494b3e95d5SJeremy L Thompson         CeedInt num_elem;
2504b3e95d5SJeremy L Thompson 
2514b3e95d5SJeremy L Thompson         CeedCallBackend(CeedElemRestrictionHasBackendStrides(elem_rstr, &has_backend_strides));
2524b3e95d5SJeremy L Thompson         CeedCallBackend(CeedElemRestrictionGetNumElements(elem_rstr, &num_elem));
2534b3e95d5SJeremy L Thompson         CeedInt strides[3] = {1, elem_size * num_elem, elem_size};
2544b3e95d5SJeremy L Thompson 
2554b3e95d5SJeremy L Thompson         if (!has_backend_strides) {
2564b3e95d5SJeremy L Thompson           CeedCallBackend(CeedElemRestrictionGetStrides(elem_rstr, strides));
2574b3e95d5SJeremy L Thompson         }
2584b3e95d5SJeremy L Thompson         code << "    // Strides: {" << strides[0] << ", " << strides[1] << ", " << strides[2] << "}\n";
2594b3e95d5SJeremy L Thompson         code << "    readDofsStrided" << dim << "d<num_comp" << var_suffix << ", " << P_name << "," << strides[0] << "," << strides[1] << ","
2604b3e95d5SJeremy L Thompson              << strides[2] << ">(data, elem, d" << var_suffix << ", r_e" << var_suffix << ");\n";
2614b3e95d5SJeremy L Thompson       }
2624b3e95d5SJeremy L Thompson     }
2634b3e95d5SJeremy L Thompson   } else {
2644b3e95d5SJeremy L Thompson     // Output
2654b3e95d5SJeremy L Thompson     bool is_strided;
2664b3e95d5SJeremy L Thompson 
2674b3e95d5SJeremy L Thompson     CeedCallBackend(CeedElemRestrictionIsStrided(elem_rstr, &is_strided));
2684b3e95d5SJeremy L Thompson     if (!is_strided) {
2694b3e95d5SJeremy L Thompson       CeedInt comp_stride;
2704b3e95d5SJeremy L Thompson 
2714b3e95d5SJeremy L Thompson       CeedCallBackend(CeedElemRestrictionGetLVectorSize(elem_rstr, &l_size));
2724b3e95d5SJeremy L Thompson       code << "    const CeedInt l_size" << var_suffix << " = " << l_size << ";\n";
2734b3e95d5SJeremy L Thompson       CeedCallBackend(CeedElemRestrictionGetCompStride(elem_rstr, &comp_stride));
2744b3e95d5SJeremy L Thompson       code << "    // CompStride: " << comp_stride << "\n";
2754b3e95d5SJeremy L Thompson       data->indices.outputs[i] = (CeedInt *)rstr_data->d_offsets;
2764b3e95d5SJeremy L Thompson       code << "    writeDofsOffset" << dim << "d<num_comp" << var_suffix << ", " << comp_stride << ", " << P_name << ">(data, l_size" << var_suffix
2774b3e95d5SJeremy L Thompson            << ", elem, indices.outputs[" << i << "], r_e" << var_suffix << ", d" << var_suffix << ");\n";
2784b3e95d5SJeremy L Thompson     } else {
2794b3e95d5SJeremy L Thompson       bool    has_backend_strides;
2804b3e95d5SJeremy L Thompson       CeedInt num_elem;
2814b3e95d5SJeremy L Thompson 
2824b3e95d5SJeremy L Thompson       CeedCallBackend(CeedElemRestrictionHasBackendStrides(elem_rstr, &has_backend_strides));
2834b3e95d5SJeremy L Thompson       CeedCallBackend(CeedElemRestrictionGetNumElements(elem_rstr, &num_elem));
2844b3e95d5SJeremy L Thompson       CeedInt strides[3] = {1, elem_size * num_elem, elem_size};
2854b3e95d5SJeremy L Thompson 
2864b3e95d5SJeremy L Thompson       if (!has_backend_strides) {
2874b3e95d5SJeremy L Thompson         CeedCallBackend(CeedElemRestrictionGetStrides(elem_rstr, strides));
2884b3e95d5SJeremy L Thompson       }
2894b3e95d5SJeremy L Thompson       code << "    // Strides: {" << strides[0] << ", " << strides[1] << ", " << strides[2] << "}\n";
2904b3e95d5SJeremy L Thompson       code << "    writeDofsStrided" << dim << "d<num_comp" << var_suffix << ", " << P_name << "," << strides[0] << "," << strides[1] << ","
2914b3e95d5SJeremy L Thompson            << strides[2] << ">(data, elem, r_e" << var_suffix << ", d" << var_suffix << ");\n";
2924b3e95d5SJeremy L Thompson     }
2934b3e95d5SJeremy L Thompson   }
2944b3e95d5SJeremy L Thompson   return CEED_ERROR_SUCCESS;
2954b3e95d5SJeremy L Thompson }
2964b3e95d5SJeremy L Thompson 
2974b3e95d5SJeremy L Thompson //------------------------------------------------------------------------------
2984b3e95d5SJeremy L Thompson // Basis
2994b3e95d5SJeremy L Thompson //------------------------------------------------------------------------------
3004b3e95d5SJeremy L Thompson static int CeedOperatorBuildKernelBasis_Cuda_gen(std::ostringstream &code, CeedOperator_Cuda_gen *data, CeedInt i, CeedInt dim,
3014b3e95d5SJeremy L Thompson                                                  CeedOperatorField op_field, CeedQFunctionField qf_field, CeedInt Q_1d, bool is_input,
3024b3e95d5SJeremy L Thompson                                                  bool use_3d_slices) {
3034b3e95d5SJeremy L Thompson   std::string         var_suffix = (is_input ? "_in_" : "_out_") + std::to_string(i);
3044b3e95d5SJeremy L Thompson   std::string         P_name = "P_1d" + var_suffix, Q_name = "Q_1d";
3054b3e95d5SJeremy L Thompson   CeedEvalMode        eval_mode = CEED_EVAL_NONE;
3064b3e95d5SJeremy L Thompson   CeedInt             elem_size = 0, num_comp = 0, P_1d = 0;
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) {
3134b3e95d5SJeremy L Thompson     CeedCallBackend(CeedElemRestrictionGetElementSize(elem_rstr, &elem_size));
3144b3e95d5SJeremy L Thompson     CeedCallBackend(CeedElemRestrictionGetNumComponents(elem_rstr, &num_comp));
3154b3e95d5SJeremy L Thompson   }
3164b3e95d5SJeremy L Thompson   CeedCallBackend(CeedOperatorFieldGetBasis(op_field, &basis));
3174b3e95d5SJeremy L Thompson   if (basis != CEED_BASIS_NONE) {
3184b3e95d5SJeremy L Thompson     CeedCallBackend(CeedBasisGetNumNodes1D(basis, &P_1d));
3194b3e95d5SJeremy L Thompson   }
3204b3e95d5SJeremy L Thompson   CeedCallBackend(CeedQFunctionFieldGetEvalMode(qf_field, &eval_mode));
3214b3e95d5SJeremy L Thompson 
3224b3e95d5SJeremy L Thompson   // Basis
3234b3e95d5SJeremy L Thompson   code << "    // EvalMode: " << CeedEvalModes[eval_mode] << "\n";
3244b3e95d5SJeremy L Thompson   if (is_input) {
3254b3e95d5SJeremy L Thompson     switch (eval_mode) {
3264b3e95d5SJeremy L Thompson       case CEED_EVAL_NONE:
3274b3e95d5SJeremy L Thompson         if (!use_3d_slices) {
3284b3e95d5SJeremy L Thompson           code << "    CeedScalar *r_q" << var_suffix << " = r_e" << var_suffix << ";\n";
3294b3e95d5SJeremy L Thompson         }
3304b3e95d5SJeremy L Thompson         break;
3314b3e95d5SJeremy L Thompson       case CEED_EVAL_INTERP:
3324b3e95d5SJeremy L Thompson         code << "    CeedScalar r_q" << var_suffix << "[num_comp" << var_suffix << "*" << Q_name << "];\n";
3334b3e95d5SJeremy L Thompson         code << "    Interp" << (dim > 1 ? "Tensor" : "") << dim << "d<num_comp" << var_suffix << ", P_1d" << var_suffix << ", " << Q_name
3344b3e95d5SJeremy L Thompson              << ">(data, r_e" << var_suffix << ", s_B" << var_suffix << ", r_q" << var_suffix << ");\n";
3354b3e95d5SJeremy L Thompson         break;
3364b3e95d5SJeremy L Thompson       case CEED_EVAL_GRAD:
3374b3e95d5SJeremy L Thompson         if (use_3d_slices) {
3384b3e95d5SJeremy L Thompson           code << "    CeedScalar r_q" << var_suffix << "[num_comp" << var_suffix << "*" << Q_name << "];\n";
3394b3e95d5SJeremy L Thompson           code << "    Interp" << (dim > 1 ? "Tensor" : "") << dim << "d<num_comp" << var_suffix << ", P_1d" << var_suffix << ", " << Q_name
3404b3e95d5SJeremy L Thompson                << ">(data, r_e" << var_suffix << ", s_B" << var_suffix << ", r_q" << var_suffix << ");\n";
3414b3e95d5SJeremy L Thompson         } else {
3424b3e95d5SJeremy L Thompson           code << "    CeedScalar r_q" << var_suffix << "[num_comp" << var_suffix << "*dim*" << Q_name << "];\n";
3434b3e95d5SJeremy L Thompson           code << "    Grad" << (dim > 1 ? "Tensor" : "") << (dim == 3 && Q_1d >= P_1d ? "Collocated" : "") << dim << "d<num_comp" << var_suffix
3444b3e95d5SJeremy L Thompson                << ", P_1d" << var_suffix << ", " << Q_name << ">(data, r_e" << var_suffix << ", s_B" << var_suffix << ", s_G" << var_suffix << ", r_q"
3454b3e95d5SJeremy L Thompson                << var_suffix << ");\n";
3464b3e95d5SJeremy L Thompson         }
3474b3e95d5SJeremy L Thompson         break;
3484b3e95d5SJeremy L Thompson       case CEED_EVAL_WEIGHT: {
3494b3e95d5SJeremy L Thompson         CeedBasis_Cuda_shared *basis_data;
3504b3e95d5SJeremy L Thompson 
3514b3e95d5SJeremy L Thompson         code << "    CeedScalar r_q" << var_suffix << "[" << Q_name << "];\n";
3524b3e95d5SJeremy L Thompson         CeedCallBackend(CeedBasisGetData(basis, &basis_data));
3534b3e95d5SJeremy L Thompson         data->W = basis_data->d_q_weight_1d;
3544b3e95d5SJeremy L Thompson         code << "    Weight" << (dim > 1 ? "Tensor" : "") << dim << "d<" << Q_name << ">(data, W, r_q" << var_suffix << ");\n";
3554b3e95d5SJeremy L Thompson         break;
3564b3e95d5SJeremy L Thompson       }
3574b3e95d5SJeremy L Thompson       // LCOV_EXCL_START
3584b3e95d5SJeremy L Thompson       case CEED_EVAL_DIV:
3594b3e95d5SJeremy L Thompson         break;  // TODO: Not implemented
3604b3e95d5SJeremy L Thompson       case CEED_EVAL_CURL:
3614b3e95d5SJeremy L Thompson         break;  // TODO: Not implemented
3624b3e95d5SJeremy L Thompson                 // LCOV_EXCL_STOP
3634b3e95d5SJeremy L Thompson     }
3644b3e95d5SJeremy L Thompson   } else {
3654b3e95d5SJeremy L Thompson     switch (eval_mode) {
3664b3e95d5SJeremy L Thompson       case CEED_EVAL_NONE:
3674b3e95d5SJeremy L Thompson         code << "    CeedScalar *r_e" << var_suffix << " = r_q" << var_suffix << ";\n";
3684b3e95d5SJeremy L Thompson         break;  // No action
3694b3e95d5SJeremy L Thompson       case CEED_EVAL_INTERP:
3704b3e95d5SJeremy L Thompson         code << "    CeedScalar r_e" << var_suffix << "[num_comp" << var_suffix << "*" << P_name << "];\n";
3714b3e95d5SJeremy L Thompson         code << "    InterpTranspose" << (dim > 1 ? "Tensor" : "") << dim << "d<num_comp" << var_suffix << ", " << P_name << ", " << Q_name
3724b3e95d5SJeremy L Thompson              << ">(data, r_q" << var_suffix << ", s_B" << var_suffix << ", r_e" << var_suffix << ");\n";
3734b3e95d5SJeremy L Thompson         break;
3744b3e95d5SJeremy L Thompson       case CEED_EVAL_GRAD:
3754b3e95d5SJeremy L Thompson         code << "    CeedScalar r_e" << var_suffix << "[num_comp" << var_suffix << "*" << P_name << "];\n";
3764b3e95d5SJeremy L Thompson         if (use_3d_slices) {
3774b3e95d5SJeremy L Thompson           code << "    InterpTranspose" << (dim > 1 ? "Tensor" : "") << dim << "d<num_comp" << var_suffix << ", " << P_name << ", " << Q_name
3784b3e95d5SJeremy L Thompson                << ">(data, r_q" << var_suffix << ", s_B" << var_suffix << ", r_e" << var_suffix << ");\n";
3794b3e95d5SJeremy L Thompson         } else {
3804b3e95d5SJeremy L Thompson           code << "    GradTranspose" << (dim > 1 ? "Tensor" : "") << (dim == 3 && Q_1d >= P_1d ? "Collocated" : "") << dim << "d<num_comp"
3814b3e95d5SJeremy L Thompson                << var_suffix << ", " << P_name << "," << Q_name << ">(data, r_q" << var_suffix << ", s_B" << var_suffix << ", s_G" << var_suffix
3824b3e95d5SJeremy L Thompson                << ", r_e" << var_suffix << ");\n";
3834b3e95d5SJeremy L Thompson         }
3844b3e95d5SJeremy L Thompson         break;
3854b3e95d5SJeremy L Thompson       // LCOV_EXCL_START
3864b3e95d5SJeremy L Thompson       case CEED_EVAL_WEIGHT:
3874b3e95d5SJeremy L Thompson         break;  // Should not occur
3884b3e95d5SJeremy L Thompson       case CEED_EVAL_DIV:
3894b3e95d5SJeremy L Thompson         break;  // TODO: Not implemented
3904b3e95d5SJeremy L Thompson       case CEED_EVAL_CURL:
3914b3e95d5SJeremy L Thompson         break;  // TODO: Not implemented
3924b3e95d5SJeremy L Thompson                 // LCOV_EXCL_STOP
3934b3e95d5SJeremy L Thompson     }
3944b3e95d5SJeremy L Thompson   }
3954b3e95d5SJeremy L Thompson   return CEED_ERROR_SUCCESS;
3964b3e95d5SJeremy L Thompson }
3974b3e95d5SJeremy L Thompson 
3984b3e95d5SJeremy L Thompson //------------------------------------------------------------------------------
3994b3e95d5SJeremy L Thompson // QFunction
4004b3e95d5SJeremy L Thompson //------------------------------------------------------------------------------
4014b3e95d5SJeremy L Thompson static int CeedOperatorBuildKernelQFunction_Cuda_gen(std::ostringstream &code, CeedOperator_Cuda_gen *data, CeedInt dim, CeedInt num_input_fields,
4024b3e95d5SJeremy L Thompson                                                      CeedOperatorField *op_input_fields, CeedQFunctionField *qf_input_fields,
4034b3e95d5SJeremy L Thompson                                                      CeedInt num_output_fields, CeedOperatorField *op_output_fields,
4044b3e95d5SJeremy L Thompson                                                      CeedQFunctionField *qf_output_fields, std::string qfunction_name, CeedInt Q_1d,
4054b3e95d5SJeremy L Thompson                                                      bool use_3d_slices) {
4064b3e95d5SJeremy L Thompson   std::string         Q_name    = "Q_1d";
4074b3e95d5SJeremy L Thompson   CeedEvalMode        eval_mode = CEED_EVAL_NONE;
4084b3e95d5SJeremy L Thompson   CeedElemRestriction elem_rstr;
4094b3e95d5SJeremy L Thompson 
4104b3e95d5SJeremy L Thompson   // Setup output arays
4114b3e95d5SJeremy L Thompson   code << "\n    // -- Output field setup\n";
4124b3e95d5SJeremy L Thompson   for (CeedInt i = 0; i < num_output_fields; i++) {
4134b3e95d5SJeremy L Thompson     std::string var_suffix = "_out_" + std::to_string(i);
4144b3e95d5SJeremy L Thompson 
4154b3e95d5SJeremy L Thompson     code << "    // ---- Output field " << i << "\n";
4164b3e95d5SJeremy L Thompson     CeedCallBackend(CeedQFunctionFieldGetEvalMode(qf_output_fields[i], &eval_mode));
4174b3e95d5SJeremy L Thompson     if (eval_mode == CEED_EVAL_NONE || eval_mode == CEED_EVAL_INTERP) {
4184b3e95d5SJeremy L Thompson       code << "    CeedScalar r_q" << var_suffix << "[num_comp" << var_suffix << "*" << Q_name << "];\n";
4194b3e95d5SJeremy L Thompson     }
4204b3e95d5SJeremy L Thompson     if (eval_mode == CEED_EVAL_GRAD) {
4214b3e95d5SJeremy L Thompson       if (use_3d_slices) {
4224b3e95d5SJeremy L Thompson         // Accumulator for gradient slices
4234b3e95d5SJeremy L Thompson         code << "    CeedScalar r_q" << var_suffix << "[num_comp" << var_suffix << "*" << Q_name << "];\n";
4244b3e95d5SJeremy L Thompson         code << "    for (CeedInt i = 0; i < num_comp" << var_suffix << "*" << Q_name << "; i++) {\n";
4254b3e95d5SJeremy L Thompson         code << "      r_q" << var_suffix << "[i] = 0.0;\n";
4264b3e95d5SJeremy L Thompson         code << "    }\n";
4274b3e95d5SJeremy L Thompson       } else {
4284b3e95d5SJeremy L Thompson         code << "    CeedScalar r_q" << var_suffix << "[num_comp" << var_suffix << "*dim*" << Q_name << "];\n";
4294b3e95d5SJeremy L Thompson       }
4304b3e95d5SJeremy L Thompson     }
4314b3e95d5SJeremy L Thompson   }
4324b3e95d5SJeremy L Thompson 
4334b3e95d5SJeremy L Thompson   // We treat quadrature points per slice in 3d to save registers
4344b3e95d5SJeremy L Thompson   if (use_3d_slices) {
4354b3e95d5SJeremy L Thompson     code << "\n    // Note: Using planes of 3D elements\n";
4364b3e95d5SJeremy L Thompson     code << "#pragma unroll\n";
4374b3e95d5SJeremy L Thompson     code << "    for (CeedInt q = 0; q < " << Q_name << "; q++) {\n";
4384b3e95d5SJeremy L Thompson     code << "      // -- Input fields\n";
4394b3e95d5SJeremy L Thompson     for (CeedInt i = 0; i < num_input_fields; i++) {
4404b3e95d5SJeremy L Thompson       std::string var_suffix = "_in_" + std::to_string(i);
4414b3e95d5SJeremy L Thompson 
4424b3e95d5SJeremy L Thompson       code << "      // ---- Input field " << i << "\n";
4434b3e95d5SJeremy L Thompson       CeedCallBackend(CeedQFunctionFieldGetEvalMode(qf_input_fields[i], &eval_mode));
4444b3e95d5SJeremy L Thompson       // Basis action
4454b3e95d5SJeremy L Thompson       code << "      // EvalMode: " << CeedEvalModes[eval_mode] << "\n";
4464b3e95d5SJeremy L Thompson       switch (eval_mode) {
4474b3e95d5SJeremy L Thompson         case CEED_EVAL_NONE:
4484b3e95d5SJeremy L Thompson           bool is_strided;
4494b3e95d5SJeremy L Thompson 
4504b3e95d5SJeremy L Thompson           code << "      CeedScalar r_s" << var_suffix << "[num_comp" << var_suffix << "];\n";
4514b3e95d5SJeremy L Thompson 
4524b3e95d5SJeremy L Thompson           CeedCallBackend(CeedOperatorFieldGetElemRestriction(op_input_fields[i], &elem_rstr));
4534b3e95d5SJeremy L Thompson           CeedCallBackend(CeedElemRestrictionIsStrided(elem_rstr, &is_strided));
4544b3e95d5SJeremy L Thompson           if (is_strided) {
4554b3e95d5SJeremy L Thompson             bool    has_backend_strides;
4564b3e95d5SJeremy L Thompson             CeedInt num_elem, elem_size;
4574b3e95d5SJeremy L Thompson 
4584b3e95d5SJeremy L Thompson             CeedCallBackend(CeedElemRestrictionGetElementSize(elem_rstr, &elem_size));
4594b3e95d5SJeremy L Thompson             CeedCallBackend(CeedElemRestrictionHasBackendStrides(elem_rstr, &has_backend_strides));
4604b3e95d5SJeremy L Thompson             CeedCallBackend(CeedElemRestrictionGetNumElements(elem_rstr, &num_elem));
4614b3e95d5SJeremy L Thompson             CeedInt strides[3] = {1, elem_size * num_elem, elem_size};
4624b3e95d5SJeremy L Thompson 
4634b3e95d5SJeremy L Thompson             if (!has_backend_strides) {
4644b3e95d5SJeremy L Thompson               CeedCallBackend(CeedElemRestrictionGetStrides(elem_rstr, strides));
4654b3e95d5SJeremy L Thompson             }
4664b3e95d5SJeremy L Thompson             code << "      // Strides: {" << strides[0] << ", " << strides[1] << ", " << strides[2] << "}\n";
4674b3e95d5SJeremy L Thompson             code << "      readSliceQuadsStrided3d<num_comp" << var_suffix << ", " << Q_name << "," << strides[0] << "," << strides[1] << ","
4684b3e95d5SJeremy L Thompson                  << strides[2] << ">(data, elem, q, d" << var_suffix << ", r_s" << var_suffix << ");\n";
4694b3e95d5SJeremy L Thompson           } else {
4704b3e95d5SJeremy L Thompson             CeedSize                  l_size = 0;
4714b3e95d5SJeremy L Thompson             CeedInt                   comp_stride;
4724b3e95d5SJeremy L Thompson             CeedElemRestriction_Cuda *rstr_data;
4734b3e95d5SJeremy L Thompson 
4744b3e95d5SJeremy L Thompson             CeedCallBackend(CeedElemRestrictionGetLVectorSize(elem_rstr, &l_size));
4754b3e95d5SJeremy L Thompson             code << "      const CeedInt l_size" << var_suffix << " = " << l_size << ";\n";
4764b3e95d5SJeremy L Thompson             CeedCallBackend(CeedElemRestrictionGetCompStride(elem_rstr, &comp_stride));
4774b3e95d5SJeremy L Thompson             code << "      // CompStride: " << comp_stride << "\n";
4784b3e95d5SJeremy L Thompson             CeedCallBackend(CeedElemRestrictionGetData(elem_rstr, &rstr_data));
4794b3e95d5SJeremy L Thompson             data->indices.inputs[i] = (CeedInt *)rstr_data->d_offsets;
4804b3e95d5SJeremy L Thompson             code << "      readSliceQuadsOffset3d<num_comp" << var_suffix << ", " << comp_stride << ", " << Q_name << ">(data, l_size" << var_suffix
4814b3e95d5SJeremy L Thompson                  << ", elem, q, indices.inputs[" << i << "], d" << var_suffix << ", r_s" << var_suffix << ");\n";
4824b3e95d5SJeremy L Thompson           }
4834b3e95d5SJeremy L Thompson           break;
4844b3e95d5SJeremy L Thompson         case CEED_EVAL_INTERP:
4854b3e95d5SJeremy L Thompson           code << "      CeedScalar r_s" << var_suffix << "[num_comp" << var_suffix << "];\n";
4864b3e95d5SJeremy L Thompson           code << "      for (CeedInt j = 0; j < num_comp" << var_suffix << "; j++) {\n";
4874b3e95d5SJeremy L Thompson           code << "        r_s" << var_suffix << "[j] = r_q" << var_suffix << "[q + j*" << Q_name << "];\n";
4884b3e95d5SJeremy L Thompson           code << "      }\n";
4894b3e95d5SJeremy L Thompson           break;
4904b3e95d5SJeremy L Thompson         case CEED_EVAL_GRAD:
4914b3e95d5SJeremy L Thompson           code << "      CeedScalar r_s" << var_suffix << "[num_comp" << var_suffix << "*dim];\n";
4924b3e95d5SJeremy L Thompson           code << "      gradCollo3d<num_comp" << var_suffix << ", " << Q_name << ">(data, q, r_q" << var_suffix << ", s_G" << var_suffix << ", r_s"
4934b3e95d5SJeremy L Thompson                << var_suffix << ");\n";
4944b3e95d5SJeremy L Thompson           break;
4954b3e95d5SJeremy L Thompson         case CEED_EVAL_WEIGHT:
4964b3e95d5SJeremy L Thompson           code << "      CeedScalar r_s" << var_suffix << "[1];\n";
4974b3e95d5SJeremy L Thompson           code << "      r_s" << var_suffix << "[0] = r_q" << var_suffix << "[q];\n";
4984b3e95d5SJeremy L Thompson           break;  // No action
4994b3e95d5SJeremy L Thompson                   // LCOV_EXCL_START
5004b3e95d5SJeremy L Thompson         case CEED_EVAL_DIV:
5014b3e95d5SJeremy L Thompson           break;  // TODO: Not implemented
5024b3e95d5SJeremy L Thompson         case CEED_EVAL_CURL:
5034b3e95d5SJeremy L Thompson           break;  // TODO: Not implemented
5044b3e95d5SJeremy L Thompson                   // LCOV_EXCL_STOP
5054b3e95d5SJeremy L Thompson       }
5064b3e95d5SJeremy L Thompson     }
5074b3e95d5SJeremy L Thompson     code << "\n      // -- Output fields\n";
5084b3e95d5SJeremy L Thompson     for (CeedInt i = 0; i < num_output_fields; i++) {
5094b3e95d5SJeremy L Thompson       std::string var_suffix = "_out_" + std::to_string(i);
5104b3e95d5SJeremy L Thompson 
5114b3e95d5SJeremy L Thompson       code << "      // ---- Output field " << i << "\n";
5124b3e95d5SJeremy L Thompson       CeedCallBackend(CeedQFunctionFieldGetEvalMode(qf_output_fields[i], &eval_mode));
5134b3e95d5SJeremy L Thompson       // Basis action
5144b3e95d5SJeremy L Thompson       switch (eval_mode) {
5154b3e95d5SJeremy L Thompson         case CEED_EVAL_NONE:
5164b3e95d5SJeremy L Thompson           code << "      CeedScalar r_s" << var_suffix << "[num_comp" << var_suffix << "];\n";
5174b3e95d5SJeremy L Thompson           break;  // No action
5184b3e95d5SJeremy L Thompson         case CEED_EVAL_INTERP:
5194b3e95d5SJeremy L Thompson           code << "      CeedScalar r_s" << var_suffix << "[num_comp" << var_suffix << "];\n";
5204b3e95d5SJeremy L Thompson           break;
5214b3e95d5SJeremy L Thompson         case CEED_EVAL_GRAD:
5224b3e95d5SJeremy L Thompson           code << "      CeedScalar r_s" << var_suffix << "[num_comp" << var_suffix << "*dim];\n";
5234b3e95d5SJeremy L Thompson           break;
5244b3e95d5SJeremy L Thompson           // LCOV_EXCL_START
5254b3e95d5SJeremy L Thompson         case CEED_EVAL_WEIGHT:
5264b3e95d5SJeremy L Thompson           break;  // Should not occur
5274b3e95d5SJeremy L Thompson         case CEED_EVAL_DIV:
5284b3e95d5SJeremy L Thompson           break;  // TODO: Not implemented
5294b3e95d5SJeremy L Thompson         case CEED_EVAL_CURL:
5304b3e95d5SJeremy L Thompson           break;  // TODO: Not implemented
5314b3e95d5SJeremy L Thompson                   // LCOV_EXCL_STOP
5324b3e95d5SJeremy L Thompson       }
5334b3e95d5SJeremy L Thompson     }
5344b3e95d5SJeremy L Thompson   } else {
5354b3e95d5SJeremy L Thompson     code << "\n    // Note: Using full elements\n";
5364b3e95d5SJeremy L Thompson     code << "    {\n";
5374b3e95d5SJeremy L Thompson     code << "      // -- Input fields\n";
5384b3e95d5SJeremy L Thompson     for (CeedInt i = 0; i < num_input_fields; i++) {
5394b3e95d5SJeremy L Thompson       code << "      // ---- Input field " << i << "\n";
5404b3e95d5SJeremy L Thompson       code << "      CeedScalar *r_s_in_" << i << " = r_q_in_" << i << ";\n";
5414b3e95d5SJeremy L Thompson     }
5424b3e95d5SJeremy L Thompson     code << "      // -- Output fields\n";
5434b3e95d5SJeremy L Thompson     for (CeedInt i = 0; i < num_output_fields; i++) {
5444b3e95d5SJeremy L Thompson       code << "      // ---- Output field " << i << "\n";
5454b3e95d5SJeremy L Thompson       code << "      CeedScalar *r_s_out_" << i << " = r_q_out_" << i << ";\n";
5464b3e95d5SJeremy L Thompson     }
5474b3e95d5SJeremy L Thompson   }
5484b3e95d5SJeremy L Thompson 
5494b3e95d5SJeremy L Thompson   // Input and output buffers
5504b3e95d5SJeremy L Thompson   code << "\n      // -- QFunction inputs and outputs\n";
5514b3e95d5SJeremy L Thompson   code << "      // ---- Inputs\n";
5524b3e95d5SJeremy L Thompson   code << "      CeedScalar *inputs[" << CeedIntMax(num_input_fields, 1) << "];\n";
5534b3e95d5SJeremy L Thompson   for (CeedInt i = 0; i < num_input_fields; i++) {
5544b3e95d5SJeremy L Thompson     code << "      // ------ Input field " << i << "\n";
5554b3e95d5SJeremy L Thompson     code << "      inputs[" << i << "] = r_s_in_" << i << ";\n";
5564b3e95d5SJeremy L Thompson   }
5574b3e95d5SJeremy L Thompson   code << "      // ---- Outputs\n";
5584b3e95d5SJeremy L Thompson   code << "      CeedScalar *outputs[" << CeedIntMax(num_output_fields, 1) << "];\n";
5594b3e95d5SJeremy L Thompson   for (CeedInt i = 0; i < num_output_fields; i++) {
5604b3e95d5SJeremy L Thompson     code << "      // ------ Output field " << i << "\n";
5614b3e95d5SJeremy L Thompson     code << "      outputs[" << i << "] = r_s_out_" << i << ";\n";
5624b3e95d5SJeremy L Thompson   }
5634b3e95d5SJeremy L Thompson 
5644b3e95d5SJeremy L Thompson   // Apply QFunction
5654b3e95d5SJeremy L Thompson   code << "\n      // -- Apply QFunction\n";
5664b3e95d5SJeremy L Thompson   code << "      " << qfunction_name << "(ctx, ";
5674b3e95d5SJeremy L Thompson   if (dim != 3 || use_3d_slices) {
5684b3e95d5SJeremy L Thompson     code << "1";
5694b3e95d5SJeremy L Thompson   } else {
5704b3e95d5SJeremy L Thompson     code << "Q_1d";
5714b3e95d5SJeremy L Thompson   }
5724b3e95d5SJeremy L Thompson   code << ", inputs, outputs);\n";
5734b3e95d5SJeremy L Thompson 
5744b3e95d5SJeremy L Thompson   // Copy or apply transpose grad, if needed
5754b3e95d5SJeremy L Thompson   if (use_3d_slices) {
5764b3e95d5SJeremy L Thompson     code << "      // -- Output fields\n";
5774b3e95d5SJeremy L Thompson     for (CeedInt i = 0; i < num_output_fields; i++) {
5784b3e95d5SJeremy L Thompson       std::string var_suffix = "_out_" + std::to_string(i);
5794b3e95d5SJeremy L Thompson       std::string P_name     = "P_1d" + var_suffix;
5804b3e95d5SJeremy L Thompson 
5814b3e95d5SJeremy L Thompson       code << "      // ---- Output field " << i << "\n";
5824b3e95d5SJeremy L Thompson       CeedCallBackend(CeedQFunctionFieldGetEvalMode(qf_output_fields[i], &eval_mode));
5834b3e95d5SJeremy L Thompson       // Basis action
5844b3e95d5SJeremy L Thompson       code << "      // EvalMode: " << CeedEvalModes[eval_mode] << "\n";
5854b3e95d5SJeremy L Thompson       switch (eval_mode) {
5864b3e95d5SJeremy L Thompson         case CEED_EVAL_NONE:
5874b3e95d5SJeremy L Thompson           code << "      for (CeedInt j = 0; j < num_comp" << var_suffix << " ; j++) {\n";
5884b3e95d5SJeremy L Thompson           code << "        r_q" << var_suffix << "[q + j*" << Q_name << "] = r_s" << var_suffix << "[j];\n";
5894b3e95d5SJeremy L Thompson           code << "      }\n";
5904b3e95d5SJeremy L Thompson           break;  // No action
5914b3e95d5SJeremy L Thompson         case CEED_EVAL_INTERP:
5924b3e95d5SJeremy L Thompson           code << "      for (CeedInt j = 0; j < num_comp" << var_suffix << " ; j++) {\n";
5934b3e95d5SJeremy L Thompson           code << "        r_q" << var_suffix << "[q + j*" << Q_name << "] = r_s" << var_suffix << "[j];\n";
5944b3e95d5SJeremy L Thompson           code << "      }\n";
5954b3e95d5SJeremy L Thompson           break;
5964b3e95d5SJeremy L Thompson         case CEED_EVAL_GRAD:
5974b3e95d5SJeremy L Thompson           code << "      gradColloTranspose3d<num_comp" << var_suffix << ", " << Q_name << ">(data, q, r_s" << var_suffix << ", s_G" << var_suffix
5984b3e95d5SJeremy L Thompson                << ", r_q" << var_suffix << ");\n";
5994b3e95d5SJeremy L Thompson           break;
6004b3e95d5SJeremy L Thompson           // LCOV_EXCL_START
6014b3e95d5SJeremy L Thompson         case CEED_EVAL_WEIGHT:
6024b3e95d5SJeremy L Thompson           break;  // Should not occur
6034b3e95d5SJeremy L Thompson         case CEED_EVAL_DIV:
6044b3e95d5SJeremy L Thompson           break;  // TODO: Not implemented
6054b3e95d5SJeremy L Thompson         case CEED_EVAL_CURL:
6064b3e95d5SJeremy L Thompson           break;  // TODO: Not implemented
6074b3e95d5SJeremy L Thompson                   // LCOV_EXCL_STOP
6084b3e95d5SJeremy L Thompson       }
6094b3e95d5SJeremy L Thompson     }
6104b3e95d5SJeremy L Thompson   }
6114b3e95d5SJeremy L Thompson   code << "    }\n";
6124b3e95d5SJeremy L Thompson   return CEED_ERROR_SUCCESS;
6134b3e95d5SJeremy L Thompson }
6144b3e95d5SJeremy L Thompson 
6154b3e95d5SJeremy L Thompson //------------------------------------------------------------------------------
616b2165e7aSSebastian Grimberg // Build single operator kernel
617ab213215SJeremy L Thompson //------------------------------------------------------------------------------
618eb7e6cafSJeremy L Thompson extern "C" int CeedOperatorBuildKernel_Cuda_gen(CeedOperator op) {
6194b3e95d5SJeremy L Thompson   bool                    is_tensor = true, use_3d_slices = false;
620241a4b83SYohann   Ceed                    ceed;
6214b3e95d5SJeremy L Thompson   CeedInt                 Q_1d, num_input_fields, num_output_fields, dim = 1;
622ca735530SJeremy L Thompson   CeedQFunctionField     *qf_input_fields, *qf_output_fields;
623ca735530SJeremy L Thompson   CeedQFunction_Cuda_gen *qf_data;
624ca735530SJeremy L Thompson   CeedQFunction           qf;
625ca735530SJeremy L Thompson   CeedOperatorField      *op_input_fields, *op_output_fields;
626ca735530SJeremy L Thompson   CeedOperator_Cuda_gen  *data;
6274b3e95d5SJeremy L Thompson   std::ostringstream      code;
6284b3e95d5SJeremy L Thompson 
6294b3e95d5SJeremy L Thompson   {
6304b3e95d5SJeremy L Thompson     bool is_setup_done;
631ca735530SJeremy L Thompson 
632ca735530SJeremy L Thompson     CeedCallBackend(CeedOperatorIsSetupDone(op, &is_setup_done));
633ca735530SJeremy L Thompson     if (is_setup_done) return CEED_ERROR_SUCCESS;
6344b3e95d5SJeremy L Thompson   }
635ca735530SJeremy L Thompson 
636ca735530SJeremy L Thompson   CeedCallBackend(CeedOperatorGetCeed(op, &ceed));
637ca735530SJeremy L Thompson   CeedCallBackend(CeedOperatorGetData(op, &data));
638ca735530SJeremy L Thompson   CeedCallBackend(CeedOperatorGetQFunction(op, &qf));
639ca735530SJeremy L Thompson   CeedCallBackend(CeedQFunctionGetData(qf, &qf_data));
640ca735530SJeremy L Thompson   CeedCallBackend(CeedOperatorGetFields(op, &num_input_fields, &op_input_fields, &num_output_fields, &op_output_fields));
641ca735530SJeremy L Thompson   CeedCallBackend(CeedQFunctionGetFields(qf, NULL, &qf_input_fields, NULL, &qf_output_fields));
642241a4b83SYohann 
6434b3e95d5SJeremy L Thompson   // Get operator data
6444b3e95d5SJeremy L Thompson   CeedCallBackend(CeedOperatorBuildKernelData_Cuda_gen(ceed, num_input_fields, op_input_fields, qf_input_fields, num_output_fields, op_output_fields,
6454b3e95d5SJeremy L Thompson                                                        qf_output_fields, &data->max_P_1d, &Q_1d, &dim, &is_tensor, &use_3d_slices));
6464b3e95d5SJeremy L Thompson   if (dim == 0) dim = 1;
6474b3e95d5SJeremy L Thompson   data->dim = dim;
6484b3e95d5SJeremy L Thompson   if (Q_1d == 0) {
6494b3e95d5SJeremy L Thompson     CeedInt Q;
6504b3e95d5SJeremy L Thompson 
6514b3e95d5SJeremy L Thompson     CeedCallBackend(CeedOperatorGetNumQuadraturePoints(op, &Q));
6524b3e95d5SJeremy L Thompson     Q_1d = Q;
6534b3e95d5SJeremy L Thompson   }
6544b3e95d5SJeremy L Thompson   data->Q_1d = Q_1d;
6554b3e95d5SJeremy L Thompson 
6560b454692Sjeremylt   // Check for restriction only identity operator
6574b3e95d5SJeremy L Thompson   {
6584b3e95d5SJeremy L Thompson     bool is_identity_qf;
6594b3e95d5SJeremy L Thompson 
6602b730f8bSJeremy L Thompson     CeedCallBackend(CeedQFunctionIsIdentity(qf, &is_identity_qf));
6610b454692Sjeremylt     if (is_identity_qf) {
6629e201c85SYohann       CeedEvalMode eval_mode_in, eval_mode_out;
663ca735530SJeremy L Thompson 
6642b730f8bSJeremy L Thompson       CeedCallBackend(CeedQFunctionFieldGetEvalMode(qf_input_fields[0], &eval_mode_in));
6652b730f8bSJeremy L Thompson       CeedCallBackend(CeedQFunctionFieldGetEvalMode(qf_output_fields[0], &eval_mode_out));
6666574a04fSJeremy L Thompson       CeedCheck(eval_mode_in != CEED_EVAL_NONE || eval_mode_out != CEED_EVAL_NONE, ceed, CEED_ERROR_BACKEND,
6676574a04fSJeremy L Thompson                 "Backend does not implement restriction only identity operators");
6680b454692Sjeremylt     }
6694b3e95d5SJeremy L Thompson   }
6700b454692Sjeremylt 
671f1a13f77SYohann Dudouit   // Add atomicAdd function for old NVidia architectures
6724b3e95d5SJeremy L Thompson   {
6734b3e95d5SJeremy L Thompson     Ceed_Cuda            *ceed_data;
6744b3e95d5SJeremy L Thompson     struct cudaDeviceProp prop;
6754b3e95d5SJeremy L Thompson 
6762b730f8bSJeremy L Thompson     CeedCallBackend(CeedGetData(ceed, &ceed_data));
6772b730f8bSJeremy L Thompson     CeedCallBackend(cudaGetDeviceProperties(&prop, ceed_data->device_id));
67880a9ef05SNatalie Beams     if ((prop.major < 6) && (CEED_SCALAR_TYPE != CEED_SCALAR_FP32)) {
67922070f95SJeremy L Thompson       char       *atomic_add_source;
68022070f95SJeremy L Thompson       const char *atomic_add_path;
681ca735530SJeremy L Thompson 
6822b730f8bSJeremy L Thompson       CeedCallBackend(CeedGetJitAbsolutePath(ceed, "ceed/jit-source/cuda/cuda-atomic-add-fallback.h", &atomic_add_path));
68323d4529eSJeremy L Thompson       CeedDebug256(ceed, CEED_DEBUG_COLOR_SUCCESS, "----- Loading Atomic Add Source -----\n");
6842b730f8bSJeremy L Thompson       CeedCallBackend(CeedLoadSourceToBuffer(ceed, atomic_add_path, &atomic_add_source));
6859e201c85SYohann       code << atomic_add_source;
6862b730f8bSJeremy L Thompson       CeedCallBackend(CeedFree(&atomic_add_path));
6872b730f8bSJeremy L Thompson       CeedCallBackend(CeedFree(&atomic_add_source));
688f1a13f77SYohann Dudouit     }
6894b3e95d5SJeremy L Thompson   }
690f1a13f77SYohann Dudouit 
6919e201c85SYohann   // Load basis source files
6924b3e95d5SJeremy L Thompson   // TODO: Add non-tensor, AtPoints
6939e201c85SYohann   {
69422070f95SJeremy L Thompson     char       *tensor_basis_kernel_source;
69522070f95SJeremy L Thompson     const char *tensor_basis_kernel_path;
696ca735530SJeremy L Thompson 
6972b730f8bSJeremy L Thompson     CeedCallBackend(CeedGetJitAbsolutePath(ceed, "ceed/jit-source/cuda/cuda-shared-basis-tensor-templates.h", &tensor_basis_kernel_path));
69823d4529eSJeremy L Thompson     CeedDebug256(ceed, CEED_DEBUG_COLOR_SUCCESS, "----- Loading Tensor Basis Kernel Source -----\n");
6992b730f8bSJeremy L Thompson     CeedCallBackend(CeedLoadSourceToBuffer(ceed, tensor_basis_kernel_path, &tensor_basis_kernel_source));
7009e201c85SYohann     code << tensor_basis_kernel_source;
7012b730f8bSJeremy L Thompson     CeedCallBackend(CeedFree(&tensor_basis_kernel_path));
7022b730f8bSJeremy L Thompson     CeedCallBackend(CeedFree(&tensor_basis_kernel_source));
7039e201c85SYohann   }
7049e201c85SYohann   {
70522070f95SJeremy L Thompson     char       *cuda_gen_template_source;
70622070f95SJeremy L Thompson     const char *cuda_gen_template_path;
707ca735530SJeremy L Thompson 
7082b730f8bSJeremy L Thompson     CeedCallBackend(CeedGetJitAbsolutePath(ceed, "ceed/jit-source/cuda/cuda-gen-templates.h", &cuda_gen_template_path));
70923d4529eSJeremy L Thompson     CeedDebug256(ceed, CEED_DEBUG_COLOR_SUCCESS, "----- Loading Cuda-Gen Template Source -----\n");
7102b730f8bSJeremy L Thompson     CeedCallBackend(CeedLoadSourceToBuffer(ceed, cuda_gen_template_path, &cuda_gen_template_source));
7119e201c85SYohann     code << cuda_gen_template_source;
7122b730f8bSJeremy L Thompson     CeedCallBackend(CeedFree(&cuda_gen_template_path));
7132b730f8bSJeremy L Thompson     CeedCallBackend(CeedFree(&cuda_gen_template_source));
7149e201c85SYohann   }
715241a4b83SYohann 
7164b3e95d5SJeremy L Thompson   // Get QFunction name
7174b3e95d5SJeremy L Thompson   std::string qfunction_name(qf_data->qfunction_name);
7184b3e95d5SJeremy L Thompson   std::string operator_name;
7194b3e95d5SJeremy L Thompson 
72009095acaSJeremy L Thompson   operator_name = "CeedKernelCudaGenOperator_" + qfunction_name;
7214d537eeaSYohann 
7229e201c85SYohann   // Define CEED_Q_VLA
7239e201c85SYohann   code << "\n#undef CEED_Q_VLA\n";
7244b3e95d5SJeremy L Thompson   if (dim != 3 || use_3d_slices) {
7259e201c85SYohann     code << "#define CEED_Q_VLA 1\n\n";
7269e201c85SYohann   } else {
7279e201c85SYohann     code << "#define CEED_Q_VLA " << Q_1d << "\n\n";
7289e201c85SYohann   }
7299e201c85SYohann 
7304b3e95d5SJeremy L Thompson   // Add user QFunction source
7314b3e95d5SJeremy L Thompson   {
7324b3e95d5SJeremy L Thompson     std::string qfunction_source(qf_data->qfunction_source);
7334b3e95d5SJeremy L Thompson 
73409095acaSJeremy L Thompson     code << qfunction_source;
7354b3e95d5SJeremy L Thompson   }
736241a4b83SYohann 
737241a4b83SYohann   // Setup
738818e0025SJeremy L Thompson   code << "\n// -----------------------------------------------------------------------------\n";
7394b3e95d5SJeremy L Thompson   code << "// Operator Kernel\n";
7404b3e95d5SJeremy L Thompson   code << "// \n";
7414b3e95d5SJeremy L Thompson   code << "// d_[in,out]_i:   CeedVector device array\n";
7424b3e95d5SJeremy L Thompson   code << "// r_[in,out]_e_i: Element vector register\n";
7434b3e95d5SJeremy L Thompson   code << "// r_[in,out]_q_i: Quadrature space vector register\n";
7444b3e95d5SJeremy L Thompson   code << "// r_[in,out]_s_i: Quadrature space slice  vector register\n";
7454b3e95d5SJeremy L Thompson   code << "// \n";
7464b3e95d5SJeremy L Thompson   code << "// s_B_[in,out]_i: Interpolation matrix, shared memory\n";
7474b3e95d5SJeremy L Thompson   code << "// s_G_[in,out]_i: Gradient matrix, shared memory\n";
7484b3e95d5SJeremy L Thompson   code << "// -----------------------------------------------------------------------------\n";
7494b3e95d5SJeremy L Thompson   code << "extern \"C\" __global__ void " << operator_name
7502b730f8bSJeremy L Thompson        << "(CeedInt num_elem, void* ctx, FieldsInt_Cuda indices, Fields_Cuda fields, Fields_Cuda B, Fields_Cuda G, CeedScalar *W) {\n";
7514b3e95d5SJeremy L Thompson 
7524b3e95d5SJeremy L Thompson   // Scratch buffers
7539e201c85SYohann   for (CeedInt i = 0; i < num_input_fields; i++) {
7544b3e95d5SJeremy L Thompson     CeedEvalMode eval_mode;
7554b3e95d5SJeremy L Thompson 
7562b730f8bSJeremy L Thompson     CeedCallBackend(CeedQFunctionFieldGetEvalMode(qf_input_fields[i], &eval_mode));
7579e201c85SYohann     if (eval_mode != CEED_EVAL_WEIGHT) {  // Skip CEED_EVAL_WEIGHT
7584b3e95d5SJeremy L Thompson       code << "  const CeedScalar *d_in_" << i << " = fields.inputs[" << i << "];\n";
759241a4b83SYohann     }
760241a4b83SYohann   }
7619e201c85SYohann   for (CeedInt i = 0; i < num_output_fields; i++) {
7624b3e95d5SJeremy L Thompson     code << "  CeedScalar *d_out_" << i << " = fields.outputs[" << i << "];\n";
763241a4b83SYohann   }
7641da99368SJeremy L Thompson 
7659e201c85SYohann   code << "  const CeedInt dim = " << dim << ";\n";
7669e201c85SYohann   code << "  const CeedInt Q_1d = " << Q_1d << ";\n";
7671da99368SJeremy L Thompson 
7684b3e95d5SJeremy L Thompson   // Shared data
769241a4b83SYohann   code << "  extern __shared__ CeedScalar slice[];\n";
7709e201c85SYohann   code << "  SharedData_Cuda data;\n";
7719e201c85SYohann   code << "  data.t_id_x = threadIdx.x;\n";
7729e201c85SYohann   code << "  data.t_id_y = threadIdx.y;\n";
7739e201c85SYohann   code << "  data.t_id_z = threadIdx.z;\n";
7749e201c85SYohann   code << "  data.t_id  = threadIdx.x + threadIdx.y*blockDim.x + threadIdx.z*blockDim.y*blockDim.x;\n";
7759e201c85SYohann   code << "  data.slice = slice + data.t_id_z*T_1D" << (dim > 1 ? "*T_1D" : "") << ";\n";
776920dcdc4Sjeremylt 
777ac421f39SYohann   // Initialize constants, and matrices B and G
7784b3e95d5SJeremy L Thompson   code << "\n  // Input field constants and basis data\n";
7799e201c85SYohann   for (CeedInt i = 0; i < num_input_fields; i++) {
780*5a5594ffSJeremy L Thompson     CeedCallBackend(CeedOperatorBuildKernelFieldData_Cuda_gen(code, data, i, op_input_fields[i], qf_input_fields[i], Q_1d, true, use_3d_slices));
781920dcdc4Sjeremylt   }
7824b3e95d5SJeremy L Thompson   code << "\n  // Output field constants and basis data\n";
7839e201c85SYohann   for (CeedInt i = 0; i < num_output_fields; i++) {
784*5a5594ffSJeremy L Thompson     CeedCallBackend(CeedOperatorBuildKernelFieldData_Cuda_gen(code, data, i, op_output_fields[i], qf_output_fields[i], Q_1d, false, use_3d_slices));
7854b3e95d5SJeremy L Thompson   }
786920dcdc4Sjeremylt 
7874b3e95d5SJeremy L Thompson   // Loop over all elements
7884b3e95d5SJeremy L Thompson   code << "\n  // Element loop\n";
789ac421f39SYohann   code << "  __syncthreads();\n";
7909e201c85SYohann   code << "  for (CeedInt elem = blockIdx.x*blockDim.z + threadIdx.z; elem < num_elem; elem += gridDim.x*blockDim.z) {\n";
7914b3e95d5SJeremy L Thompson 
7924b3e95d5SJeremy L Thompson   // -- Input restriction and basis
7934b3e95d5SJeremy L Thompson   code << "    // -- Input field restrictions and basis actions\n";
7949e201c85SYohann   for (CeedInt i = 0; i < num_input_fields; i++) {
7954b3e95d5SJeremy L Thompson     code << "    // ---- Input field " << i << "\n";
796920dcdc4Sjeremylt 
7974b3e95d5SJeremy L Thompson     // ---- Restriction
7984b3e95d5SJeremy L Thompson     CeedCallBackend(
7994b3e95d5SJeremy L Thompson         CeedOperatorBuildKernelRestriction_Cuda_gen(code, data, i, dim, op_input_fields[i], qf_input_fields[i], Q_1d, true, use_3d_slices));
8009a2291e3SJeremy L Thompson 
8014b3e95d5SJeremy L Thompson     // ---- Basis action
8024b3e95d5SJeremy L Thompson     CeedCallBackend(CeedOperatorBuildKernelBasis_Cuda_gen(code, data, i, dim, op_input_fields[i], qf_input_fields[i], Q_1d, true, use_3d_slices));
803920dcdc4Sjeremylt   }
804920dcdc4Sjeremylt 
8054b3e95d5SJeremy L Thompson   // -- Q function
8064b3e95d5SJeremy L Thompson   CeedCallBackend(CeedOperatorBuildKernelQFunction_Cuda_gen(code, data, dim, num_input_fields, op_input_fields, qf_input_fields, num_output_fields,
8074b3e95d5SJeremy L Thompson                                                             op_output_fields, qf_output_fields, qfunction_name, Q_1d, use_3d_slices));
808ca735530SJeremy L Thompson 
8094b3e95d5SJeremy L Thompson   // -- Output basis and restriction
8104b3e95d5SJeremy L Thompson   code << "\n    // -- Output field basis action and restrictions\n";
8119e201c85SYohann   for (CeedInt i = 0; i < num_output_fields; i++) {
8124b3e95d5SJeremy L Thompson     code << "    // ---- Output field " << i << "\n";
813ca735530SJeremy L Thompson 
8144b3e95d5SJeremy L Thompson     // ---- Basis action
8154b3e95d5SJeremy L Thompson     CeedCallBackend(CeedOperatorBuildKernelBasis_Cuda_gen(code, data, i, dim, op_output_fields[i], qf_output_fields[i], Q_1d, false, use_3d_slices));
8169a2291e3SJeremy L Thompson 
8174b3e95d5SJeremy L Thompson     // ---- Restriction
8184b3e95d5SJeremy L Thompson     CeedCallBackend(
8194b3e95d5SJeremy L Thompson         CeedOperatorBuildKernelRestriction_Cuda_gen(code, data, i, dim, op_output_fields[i], qf_output_fields[i], Q_1d, false, use_3d_slices));
820ac421f39SYohann   }
821241a4b83SYohann 
8224b3e95d5SJeremy L Thompson   // Close loop and function
823241a4b83SYohann   code << "  }\n";
824818e0025SJeremy L Thompson   code << "}\n";
825818e0025SJeremy L Thompson   code << "// -----------------------------------------------------------------------------\n\n";
826241a4b83SYohann 
827ab213215SJeremy L Thompson   // View kernel for debugging
82823d4529eSJeremy L Thompson   CeedDebug256(ceed, CEED_DEBUG_COLOR_SUCCESS, "Generated Operator Kernels:\n");
8293f21f6b1SJeremy L Thompson   CeedDebug(ceed, code.str().c_str());
830241a4b83SYohann 
831eb7e6cafSJeremy L Thompson   CeedCallBackend(CeedCompile_Cuda(ceed, code.str().c_str(), &data->module, 1, "T_1D", CeedIntMax(Q_1d, data->max_P_1d)));
832eb7e6cafSJeremy L Thompson   CeedCallBackend(CeedGetKernel_Cuda(ceed, data->module, operator_name.c_str(), &data->op));
833241a4b83SYohann 
8342b730f8bSJeremy L Thompson   CeedCallBackend(CeedOperatorSetSetupDone(op));
835e15f9bd0SJeremy L Thompson   return CEED_ERROR_SUCCESS;
836241a4b83SYohann }
8372a86cc9dSSebastian Grimberg 
838ab213215SJeremy L Thompson //------------------------------------------------------------------------------
839