xref: /libCEED/backends/hip-gen/ceed-hip-gen-operator-build.cpp (revision 343e3094792a64f9c2da70ef2256f98e7dc173cf)
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.
37d8d0e25Snbeams //
43d8e8822SJeremy L Thompson // SPDX-License-Identifier: BSD-2-Clause
57d8d0e25Snbeams //
63d8e8822SJeremy L Thompson // This file is part of CEED:  http://github.com/ceed
73d576824SJeremy L Thompson 
87d8d0e25Snbeams #define CEED_DEBUG_COLOR 12
97d8d0e25Snbeams 
1049aac155SJeremy L Thompson #include <ceed.h>
11ec3da8bcSJed Brown #include <ceed/backend.h>
129e201c85SYohann #include <ceed/jit-tools.h>
132b730f8bSJeremy L Thompson 
147d8d0e25Snbeams #include <iostream>
157d8d0e25Snbeams #include <sstream>
162b730f8bSJeremy L Thompson #include <string>
172b730f8bSJeremy L Thompson 
180d0321e0SJeremy L Thompson #include "../hip-ref/ceed-hip-ref.h"
197d8d0e25Snbeams #include "../hip-shared/ceed-hip-shared.h"
20b2165e7aSSebastian Grimberg #include "../hip/ceed-hip-common.h"
217d8d0e25Snbeams #include "../hip/ceed-hip-compile.h"
222b730f8bSJeremy L Thompson #include "ceed-hip-gen.h"
23b3e1519bSnbeams 
2445a787f7SJeremy L Thompson struct FieldReuse_Hip {
2545a787f7SJeremy L Thompson   CeedInt      index;
2645a787f7SJeremy L Thompson   bool         is_input;
2745a787f7SJeremy L Thompson   CeedEvalMode eval_mode;
2845a787f7SJeremy L Thompson };
2945a787f7SJeremy L Thompson 
30b3e1519bSnbeams //------------------------------------------------------------------------------
31b3e1519bSnbeams // Calculate the block size used for launching the operator kernel
32b3e1519bSnbeams //------------------------------------------------------------------------------
332b730f8bSJeremy L Thompson extern "C" int BlockGridCalculate_Hip_gen(const CeedInt dim, const CeedInt num_elem, const CeedInt P_1d, const CeedInt Q_1d, CeedInt *block_sizes) {
343a2968d6SJeremy L Thompson   const CeedInt thread_1d = CeedIntMax(Q_1d, P_1d);
35b3e1519bSnbeams   if (dim == 1) {
363a2968d6SJeremy L Thompson     CeedInt elems_per_block = 64 * thread_1d > 256 ? 256 / thread_1d : 64;
37b7453713SJeremy L Thompson 
389e201c85SYohann     elems_per_block = elems_per_block > 0 ? elems_per_block : 1;
393a2968d6SJeremy L Thompson     block_sizes[0]  = thread_1d;
40b3e1519bSnbeams     block_sizes[1]  = 1;
419e201c85SYohann     block_sizes[2]  = elems_per_block;
42b3e1519bSnbeams   } else if (dim == 2) {
433a2968d6SJeremy L Thompson     const CeedInt elems_per_block = thread_1d < 4 ? 16 : 2;
44b7453713SJeremy L Thompson 
453a2968d6SJeremy L Thompson     block_sizes[0] = thread_1d;
463a2968d6SJeremy L Thompson     block_sizes[1] = thread_1d;
479e201c85SYohann     block_sizes[2] = elems_per_block;
48b3e1519bSnbeams   } else if (dim == 3) {
493a2968d6SJeremy L Thompson     const CeedInt elems_per_block = thread_1d < 6 ? 4 : (thread_1d < 8 ? 2 : 1);
50b7453713SJeremy L Thompson 
513a2968d6SJeremy L Thompson     block_sizes[0] = thread_1d;
523a2968d6SJeremy L Thompson     block_sizes[1] = thread_1d;
539e201c85SYohann     block_sizes[2] = elems_per_block;
54b3e1519bSnbeams   }
55b3e1519bSnbeams   return CEED_ERROR_SUCCESS;
56b3e1519bSnbeams }
57b3e1519bSnbeams 
587d8d0e25Snbeams //------------------------------------------------------------------------------
594b3e95d5SJeremy L Thompson // Determine type of operator
604b3e95d5SJeremy L Thompson //------------------------------------------------------------------------------
614b3e95d5SJeremy L Thompson static int CeedOperatorBuildKernelData_Hip_gen(Ceed ceed, CeedInt num_input_fields, CeedOperatorField *op_input_fields,
624b3e95d5SJeremy L Thompson                                                CeedQFunctionField *qf_input_fields, CeedInt num_output_fields, CeedOperatorField *op_output_fields,
634b3e95d5SJeremy L Thompson                                                CeedQFunctionField *qf_output_fields, CeedInt *max_P_1d, CeedInt *Q_1d, CeedInt *dim, bool *is_tensor,
644b3e95d5SJeremy L Thompson                                                bool *use_3d_slices) {
654b3e95d5SJeremy L Thompson   // Find dim, P_1d, Q_1d
664b3e95d5SJeremy L Thompson   *max_P_1d  = 0;
674b3e95d5SJeremy L Thompson   *Q_1d      = 0;
684b3e95d5SJeremy L Thompson   *dim       = 0;
694b3e95d5SJeremy L Thompson   *is_tensor = true;
709123fb08SJeremy L Thompson 
714b3e95d5SJeremy L Thompson   for (CeedInt i = 0; i < num_input_fields; i++) {
724b3e95d5SJeremy L Thompson     CeedBasis basis;
734b3e95d5SJeremy L Thompson 
744b3e95d5SJeremy L Thompson     CeedCallBackend(CeedOperatorFieldGetBasis(op_input_fields[i], &basis));
754b3e95d5SJeremy L Thompson     if (basis != CEED_BASIS_NONE) {
764b3e95d5SJeremy L Thompson       bool    is_field_tensor;
774b3e95d5SJeremy L Thompson       CeedInt field_P_1d = 0, field_Q_1d = 0, field_dim = 0;
784b3e95d5SJeremy L Thompson 
794b3e95d5SJeremy L Thompson       // Collect dim, P_1d, and Q_1d
804b3e95d5SJeremy L Thompson       CeedCallBackend(CeedBasisIsTensor(basis, &is_field_tensor));
814b3e95d5SJeremy L Thompson       *is_tensor = *is_tensor && is_field_tensor;
829123fb08SJeremy L Thompson       if (is_field_tensor) CeedCallBackend(CeedBasisGetNumNodes1D(basis, &field_P_1d));
839123fb08SJeremy L Thompson       else CeedCallBackend(CeedBasisGetNumNodes(basis, &field_P_1d));
844b3e95d5SJeremy L Thompson       *max_P_1d = CeedIntMax(*max_P_1d, field_P_1d);
854b3e95d5SJeremy L Thompson       CeedCallBackend(CeedBasisGetDimension(basis, &field_dim));
864b3e95d5SJeremy L Thompson       CeedCheck(*dim == 0 || field_dim == *dim, ceed, CEED_ERROR_BACKEND, "Quadrature spaces must be compatible");
874b3e95d5SJeremy L Thompson       *dim = field_dim;
889123fb08SJeremy L Thompson       if (is_field_tensor) CeedCallBackend(CeedBasisGetNumQuadraturePoints1D(basis, &field_Q_1d));
899123fb08SJeremy L Thompson       else CeedCallBackend(CeedBasisGetNumQuadraturePoints(basis, &field_Q_1d));
904b3e95d5SJeremy L Thompson       CeedCheck(*Q_1d == 0 || field_Q_1d == *Q_1d, ceed, CEED_ERROR_BACKEND, "Quadrature spaces must be compatible");
914b3e95d5SJeremy L Thompson       *Q_1d = field_Q_1d;
924b3e95d5SJeremy L Thompson     }
933a2968d6SJeremy L Thompson     CeedCallBackend(CeedBasisDestroy(&basis));
944b3e95d5SJeremy L Thompson   }
954b3e95d5SJeremy L Thompson   for (CeedInt i = 0; i < num_output_fields; i++) {
964b3e95d5SJeremy L Thompson     CeedBasis basis;
974b3e95d5SJeremy L Thompson 
984b3e95d5SJeremy L Thompson     CeedCallBackend(CeedOperatorFieldGetBasis(op_output_fields[i], &basis));
994b3e95d5SJeremy L Thompson     if (basis != CEED_BASIS_NONE) {
1004b3e95d5SJeremy L Thompson       bool    is_field_tensor;
1014b3e95d5SJeremy L Thompson       CeedInt field_P_1d = 0, field_Q_1d = 0, field_dim = 0;
1024b3e95d5SJeremy L Thompson 
1034b3e95d5SJeremy L Thompson       // Collect dim, P_1d, and Q_1d
1044b3e95d5SJeremy L Thompson       CeedCallBackend(CeedBasisIsTensor(basis, &is_field_tensor));
1054b3e95d5SJeremy L Thompson       *is_tensor = *is_tensor && is_field_tensor;
1069123fb08SJeremy L Thompson       if (is_field_tensor) CeedCallBackend(CeedBasisGetNumNodes1D(basis, &field_P_1d));
1079123fb08SJeremy L Thompson       else CeedCallBackend(CeedBasisGetNumNodes(basis, &field_P_1d));
1084b3e95d5SJeremy L Thompson       *max_P_1d = CeedIntMax(*max_P_1d, field_P_1d);
1094b3e95d5SJeremy L Thompson       CeedCallBackend(CeedBasisGetDimension(basis, &field_dim));
1104b3e95d5SJeremy L Thompson       CeedCheck(*dim == 0 || field_dim == *dim, ceed, CEED_ERROR_BACKEND, "Quadrature spaces must be compatible");
1114b3e95d5SJeremy L Thompson       *dim = field_dim;
1129123fb08SJeremy L Thompson       if (is_field_tensor) CeedCallBackend(CeedBasisGetNumQuadraturePoints1D(basis, &field_Q_1d));
1139123fb08SJeremy L Thompson       else CeedCallBackend(CeedBasisGetNumQuadraturePoints(basis, &field_Q_1d));
1144b3e95d5SJeremy L Thompson       CeedCheck(*Q_1d == 0 || field_Q_1d == *Q_1d, ceed, CEED_ERROR_BACKEND, "Quadrature spaces must be compatible");
1154b3e95d5SJeremy L Thompson       *Q_1d = field_Q_1d;
1164b3e95d5SJeremy L Thompson     }
1173a2968d6SJeremy L Thompson     CeedCallBackend(CeedBasisDestroy(&basis));
1184b3e95d5SJeremy L Thompson   }
1194b3e95d5SJeremy L Thompson 
1204b3e95d5SJeremy L Thompson   // Only use 3D collocated gradient parallelization strategy when gradient is computed
1214b3e95d5SJeremy L Thompson   *use_3d_slices = false;
1224b3e95d5SJeremy L Thompson   if (*dim == 3) {
1234b3e95d5SJeremy L Thompson     bool was_grad_found = false;
1244b3e95d5SJeremy L Thompson 
1254b3e95d5SJeremy L Thompson     for (CeedInt i = 0; i < num_input_fields; i++) {
1264b3e95d5SJeremy L Thompson       CeedEvalMode eval_mode;
1274b3e95d5SJeremy L Thompson 
1284b3e95d5SJeremy L Thompson       CeedCallBackend(CeedQFunctionFieldGetEvalMode(qf_input_fields[i], &eval_mode));
1294b3e95d5SJeremy L Thompson       if (eval_mode == CEED_EVAL_GRAD) {
1304b3e95d5SJeremy L Thompson         CeedBasis_Hip_shared *basis_data;
1314b3e95d5SJeremy L Thompson         CeedBasis             basis;
1324b3e95d5SJeremy L Thompson 
1334b3e95d5SJeremy L Thompson         CeedCallBackend(CeedOperatorFieldGetBasis(op_input_fields[i], &basis));
1344b3e95d5SJeremy L Thompson         CeedCallBackend(CeedBasisGetData(basis, &basis_data));
1354b3e95d5SJeremy L Thompson         *use_3d_slices = basis_data->d_collo_grad_1d && (was_grad_found ? *use_3d_slices : true);
1364b3e95d5SJeremy L Thompson         was_grad_found = true;
1373a2968d6SJeremy L Thompson         CeedCallBackend(CeedBasisDestroy(&basis));
1384b3e95d5SJeremy L Thompson       }
1394b3e95d5SJeremy L Thompson     }
1404b3e95d5SJeremy L Thompson     for (CeedInt i = 0; i < num_output_fields; i++) {
1414b3e95d5SJeremy L Thompson       CeedEvalMode eval_mode;
1424b3e95d5SJeremy L Thompson 
1434b3e95d5SJeremy L Thompson       CeedCallBackend(CeedQFunctionFieldGetEvalMode(qf_output_fields[i], &eval_mode));
1444b3e95d5SJeremy L Thompson       if (eval_mode == CEED_EVAL_GRAD) {
1454b3e95d5SJeremy L Thompson         CeedBasis_Hip_shared *basis_data;
1464b3e95d5SJeremy L Thompson         CeedBasis             basis;
1474b3e95d5SJeremy L Thompson 
1484b3e95d5SJeremy L Thompson         CeedCallBackend(CeedOperatorFieldGetBasis(op_output_fields[i], &basis));
1494b3e95d5SJeremy L Thompson         CeedCallBackend(CeedBasisGetData(basis, &basis_data));
1504b3e95d5SJeremy L Thompson         *use_3d_slices = basis_data->d_collo_grad_1d && (was_grad_found ? *use_3d_slices : true);
1514b3e95d5SJeremy L Thompson         was_grad_found = true;
1523a2968d6SJeremy L Thompson         CeedCallBackend(CeedBasisDestroy(&basis));
1534b3e95d5SJeremy L Thompson       }
1544b3e95d5SJeremy L Thompson     }
1554b3e95d5SJeremy L Thompson   }
1564b3e95d5SJeremy L Thompson   return CEED_ERROR_SUCCESS;
1574b3e95d5SJeremy L Thompson }
1584b3e95d5SJeremy L Thompson 
1594b3e95d5SJeremy L Thompson //------------------------------------------------------------------------------
1604b3e95d5SJeremy L Thompson // Setup fields
1614b3e95d5SJeremy L Thompson //------------------------------------------------------------------------------
1624b3e95d5SJeremy L Thompson static int CeedOperatorBuildKernelFieldData_Hip_gen(std::ostringstream &code, CeedOperator_Hip_gen *data, CeedInt i, CeedOperatorField op_field,
16345a787f7SJeremy L Thompson                                                     CeedQFunctionField qf_field, FieldReuse_Hip field_reuse, CeedInt Q_1d, bool is_input,
16445a787f7SJeremy L Thompson                                                     bool is_tensor, bool is_at_points, bool use_3d_slices) {
16559fa3f92SJeremy L Thompson   const char           *field_name;
1664b3e95d5SJeremy L Thompson   std::string           var_suffix = (is_input ? "_in_" : "_out_") + std::to_string(i);
1679123fb08SJeremy L Thompson   std::string           P_name = (is_tensor ? "P_1d" : "P") + var_suffix, Q_name = is_tensor ? "Q_1d" : "Q";
1684b3e95d5SJeremy L Thompson   std::string           option_name = (is_input ? "inputs" : "outputs");
1694b3e95d5SJeremy L Thompson   CeedEvalMode          eval_mode   = CEED_EVAL_NONE;
1704b3e95d5SJeremy L Thompson   CeedInt               elem_size = 0, num_comp = 0, P_1d = 0;
1714b3e95d5SJeremy L Thompson   CeedElemRestriction   elem_rstr;
1724b3e95d5SJeremy L Thompson   CeedBasis_Hip_shared *basis_data;
1734b3e95d5SJeremy L Thompson   CeedBasis             basis;
1744b3e95d5SJeremy L Thompson 
1759ee499e5SJeremy L Thompson   // Field reuse info
17645a787f7SJeremy L Thompson   bool use_previous_field = field_reuse.index != -1;
1779ee499e5SJeremy L Thompson 
17859fa3f92SJeremy L Thompson   CeedCallBackend(CeedOperatorFieldGetName(op_field, &field_name));
17959fa3f92SJeremy L Thompson   code << "  // -- " << (is_input ? "Input" : "Output") << " field " << i << ": " << field_name << "\n";
1804b3e95d5SJeremy L Thompson 
1814b3e95d5SJeremy L Thompson   // Get field data
1824b3e95d5SJeremy L Thompson   CeedCallBackend(CeedOperatorFieldGetElemRestriction(op_field, &elem_rstr));
1834b3e95d5SJeremy L Thompson   if (elem_rstr != CEED_ELEMRESTRICTION_NONE) {
1844b3e95d5SJeremy L Thompson     CeedCallBackend(CeedElemRestrictionGetElementSize(elem_rstr, &elem_size));
1854b3e95d5SJeremy L Thompson     CeedCallBackend(CeedElemRestrictionGetNumComponents(elem_rstr, &num_comp));
1864b3e95d5SJeremy L Thompson   }
1873a2968d6SJeremy L Thompson   CeedCallBackend(CeedElemRestrictionDestroy(&elem_rstr));
1884b3e95d5SJeremy L Thompson   CeedCallBackend(CeedOperatorFieldGetBasis(op_field, &basis));
1894b3e95d5SJeremy L Thompson   if (basis != CEED_BASIS_NONE) {
1904b3e95d5SJeremy L Thompson     CeedCallBackend(CeedBasisGetData(basis, &basis_data));
1919123fb08SJeremy L Thompson     if (is_tensor) CeedCallBackend(CeedBasisGetNumNodes1D(basis, &P_1d));
1929123fb08SJeremy L Thompson     else CeedCallBackend(CeedBasisGetNumNodes(basis, &P_1d));
1934b3e95d5SJeremy L Thompson   }
1944b3e95d5SJeremy L Thompson   CeedCallBackend(CeedQFunctionFieldGetEvalMode(qf_field, &eval_mode));
1954b3e95d5SJeremy L Thompson 
1964b3e95d5SJeremy L Thompson   // Set field constants
1974b3e95d5SJeremy L Thompson   code << "  const CeedInt " << P_name << " = " << (basis == CEED_BASIS_NONE ? Q_1d : P_1d) << ";\n";
198*343e3094SJeremy L Thompson   if (eval_mode != CEED_EVAL_WEIGHT) {
1994b3e95d5SJeremy L Thompson     code << "  const CeedInt num_comp" << var_suffix << " = " << num_comp << ";\n";
2004b3e95d5SJeremy L Thompson   }
2014b3e95d5SJeremy L Thompson 
2024b3e95d5SJeremy L Thompson   // Load basis data
2034b3e95d5SJeremy L Thompson   code << "  // EvalMode: " << CeedEvalModes[eval_mode] << "\n";
2044b3e95d5SJeremy L Thompson   switch (eval_mode) {
2054b3e95d5SJeremy L Thompson     case CEED_EVAL_NONE:
2064b3e95d5SJeremy L Thompson       break;
2074b3e95d5SJeremy L Thompson     case CEED_EVAL_INTERP:
2083a2968d6SJeremy L Thompson       if (is_at_points) {
2093a2968d6SJeremy L Thompson         // AtPoints
2103a2968d6SJeremy L Thompson         if (!basis_data->d_chebyshev_interp_1d) {
2113a2968d6SJeremy L Thompson           CeedSize    interp_bytes;
2123a2968d6SJeremy L Thompson           CeedScalar *chebyshev_interp_1d;
2133a2968d6SJeremy L Thompson 
2143a2968d6SJeremy L Thompson           interp_bytes = P_1d * Q_1d * sizeof(CeedScalar);
2153a2968d6SJeremy L Thompson           CeedCallBackend(CeedCalloc(P_1d * Q_1d, &chebyshev_interp_1d));
2163a2968d6SJeremy L Thompson           CeedCallBackend(CeedBasisGetChebyshevInterp1D(basis, chebyshev_interp_1d));
2173a2968d6SJeremy L Thompson           CeedCallHip(CeedBasisReturnCeed(basis), hipMalloc((void **)&basis_data->d_chebyshev_interp_1d, interp_bytes));
2183a2968d6SJeremy L Thompson           CeedCallHip(CeedBasisReturnCeed(basis),
2193a2968d6SJeremy L Thompson                       hipMemcpy(basis_data->d_chebyshev_interp_1d, chebyshev_interp_1d, interp_bytes, hipMemcpyHostToDevice));
2203a2968d6SJeremy L Thompson           CeedCallBackend(CeedFree(&chebyshev_interp_1d));
2213a2968d6SJeremy L Thompson         }
2223a2968d6SJeremy L Thompson         if (is_input) data->B.inputs[i] = basis_data->d_chebyshev_interp_1d;
2233a2968d6SJeremy L Thompson         else data->B.outputs[i] = basis_data->d_chebyshev_interp_1d;
2243a2968d6SJeremy L Thompson       } else {
2253a2968d6SJeremy L Thompson         // Standard quadrature
2264b3e95d5SJeremy L Thompson         if (is_input) data->B.inputs[i] = basis_data->d_interp_1d;
2274b3e95d5SJeremy L Thompson         else data->B.outputs[i] = basis_data->d_interp_1d;
2283a2968d6SJeremy L Thompson       }
2299ee499e5SJeremy L Thompson       if (use_previous_field) {
23045a787f7SJeremy L Thompson         std::string reuse_var = "s_B" + ((field_reuse.is_input ? "_in_" : "_out_") + std::to_string(field_reuse.index));
2319ee499e5SJeremy L Thompson 
2329ee499e5SJeremy L Thompson         code << "  CeedScalar *s_B" << var_suffix << " = " << reuse_var << ";\n";
2339ee499e5SJeremy L Thompson       } else {
2349123fb08SJeremy L Thompson         code << "  __shared__ CeedScalar s_B" << var_suffix << "[" << P_name << "*" << Q_name << "];\n";
235f815fac9SJeremy L Thompson         code << "  LoadMatrix<" << P_name << ", " << Q_name << ">(data, B." << option_name << "[" << i << "], s_B" << var_suffix << ");\n";
2369ee499e5SJeremy L Thompson       }
2374b3e95d5SJeremy L Thompson       break;
2384b3e95d5SJeremy L Thompson     case CEED_EVAL_GRAD:
2393a2968d6SJeremy L Thompson       if (is_at_points) {
2403a2968d6SJeremy L Thompson         // AtPoints
2413a2968d6SJeremy L Thompson         if (!basis_data->d_chebyshev_interp_1d) {
2423a2968d6SJeremy L Thompson           CeedSize    interp_bytes;
2433a2968d6SJeremy L Thompson           CeedScalar *chebyshev_interp_1d;
2443a2968d6SJeremy L Thompson 
2453a2968d6SJeremy L Thompson           interp_bytes = P_1d * Q_1d * sizeof(CeedScalar);
2463a2968d6SJeremy L Thompson           CeedCallBackend(CeedCalloc(P_1d * Q_1d, &chebyshev_interp_1d));
2473a2968d6SJeremy L Thompson           CeedCallBackend(CeedBasisGetChebyshevInterp1D(basis, chebyshev_interp_1d));
2483a2968d6SJeremy L Thompson           CeedCallHip(CeedBasisReturnCeed(basis), hipMalloc((void **)&basis_data->d_chebyshev_interp_1d, interp_bytes));
2493a2968d6SJeremy L Thompson           CeedCallHip(CeedBasisReturnCeed(basis),
2503a2968d6SJeremy L Thompson                       hipMemcpy(basis_data->d_chebyshev_interp_1d, chebyshev_interp_1d, interp_bytes, hipMemcpyHostToDevice));
2513a2968d6SJeremy L Thompson           CeedCallBackend(CeedFree(&chebyshev_interp_1d));
2523a2968d6SJeremy L Thompson         }
2533a2968d6SJeremy L Thompson         if (is_input) data->B.inputs[i] = basis_data->d_chebyshev_interp_1d;
2543a2968d6SJeremy L Thompson         else data->B.outputs[i] = basis_data->d_chebyshev_interp_1d;
2553a2968d6SJeremy L Thompson       } else {
2563a2968d6SJeremy L Thompson         // Standard quadrature
2574b3e95d5SJeremy L Thompson         if (is_input) data->B.inputs[i] = basis_data->d_interp_1d;
2584b3e95d5SJeremy L Thompson         else data->B.outputs[i] = basis_data->d_interp_1d;
2593a2968d6SJeremy L Thompson       }
2609123fb08SJeremy L Thompson       if (is_tensor) {
2619ee499e5SJeremy L Thompson         if (use_previous_field) {
26245a787f7SJeremy L Thompson           std::string reuse_var = "s_B" + ((field_reuse.is_input ? "_in_" : "_out_") + std::to_string(field_reuse.index));
2639ee499e5SJeremy L Thompson 
2649ee499e5SJeremy L Thompson           code << "  CeedScalar *s_B" << var_suffix << " = " << reuse_var << ";\n";
2659ee499e5SJeremy L Thompson         } else {
2669123fb08SJeremy L Thompson           code << "  __shared__ CeedScalar s_B" << var_suffix << "[" << P_name << "*" << Q_name << "];\n";
267f815fac9SJeremy L Thompson           code << "  LoadMatrix<" << P_name << ", " << Q_name << ">(data, B." << option_name << "[" << i << "], s_B" << var_suffix << ");\n";
2689123fb08SJeremy L Thompson         }
2699ee499e5SJeremy L Thompson       }
2703a2968d6SJeremy L Thompson       if (is_at_points) break;  // No G mat for AtPoints
2714b3e95d5SJeremy L Thompson       if (use_3d_slices) {
2724b3e95d5SJeremy L Thompson         if (is_input) data->G.inputs[i] = basis_data->d_collo_grad_1d;
2734b3e95d5SJeremy L Thompson         else data->G.outputs[i] = basis_data->d_collo_grad_1d;
27445a787f7SJeremy L Thompson         if (use_previous_field && field_reuse.eval_mode == CEED_EVAL_GRAD) {
27545a787f7SJeremy L Thompson           std::string reuse_var = "s_G" + ((field_reuse.is_input ? "_in_" : "_out_") + std::to_string(field_reuse.index));
2769ee499e5SJeremy L Thompson 
2779ee499e5SJeremy L Thompson           code << "  CeedScalar *s_G" << var_suffix << " = " << reuse_var << ";\n";
2789ee499e5SJeremy L Thompson         } else {
2799123fb08SJeremy L Thompson           code << "  __shared__ CeedScalar s_G" << var_suffix << "[" << Q_name << "*" << Q_name << "];\n";
280f815fac9SJeremy L Thompson           code << "  LoadMatrix<" << Q_name << ", " << Q_name << ">(data, G." << option_name << "[" << i << "], s_G" << var_suffix << ");\n";
2819ee499e5SJeremy L Thompson         }
2824b3e95d5SJeremy L Thompson       } else {
2834b3e95d5SJeremy L Thompson         bool has_collo_grad = basis_data->d_collo_grad_1d;
2844b3e95d5SJeremy L Thompson 
2854b3e95d5SJeremy L Thompson         if (is_input) data->G.inputs[i] = has_collo_grad ? basis_data->d_collo_grad_1d : basis_data->d_grad_1d;
2864b3e95d5SJeremy L Thompson         else data->G.outputs[i] = has_collo_grad ? basis_data->d_collo_grad_1d : basis_data->d_grad_1d;
2874b3e95d5SJeremy L Thompson         if (has_collo_grad) {
28845a787f7SJeremy L Thompson           if (use_previous_field && field_reuse.eval_mode == CEED_EVAL_GRAD) {
28945a787f7SJeremy L Thompson             std::string reuse_var = "s_G" + ((field_reuse.is_input ? "_in_" : "_out_") + std::to_string(field_reuse.index));
2909ee499e5SJeremy L Thompson 
2919ee499e5SJeremy L Thompson             code << "  CeedScalar *s_G" << var_suffix << " = " << reuse_var << ";\n";
2929ee499e5SJeremy L Thompson           } else {
2939123fb08SJeremy L Thompson             code << "  __shared__ CeedScalar s_G" << var_suffix << "[" << Q_name << "*" << Q_name << "];\n";
294f815fac9SJeremy L Thompson             code << "  LoadMatrix<" << Q_name << ", " << Q_name << ">(data, G." << option_name << "[" << i << "], s_G" << var_suffix << ");\n";
2959ee499e5SJeremy L Thompson           }
2969ee499e5SJeremy L Thompson         } else {
29745a787f7SJeremy L Thompson           if (use_previous_field && field_reuse.eval_mode == CEED_EVAL_GRAD) {
29845a787f7SJeremy L Thompson             std::string reuse_var = "s_G" + ((field_reuse.is_input ? "_in_" : "_out_") + std::to_string(field_reuse.index));
2999ee499e5SJeremy L Thompson 
3009ee499e5SJeremy L Thompson             code << "  CeedScalar *s_G" << var_suffix << " = " << reuse_var << ";\n";
3014b3e95d5SJeremy L Thompson           } else {
3029123fb08SJeremy L Thompson             code << "  __shared__ CeedScalar s_G" << var_suffix << "[" << P_name << "*" << Q_name << (is_tensor ? "" : "*dim") << "];\n";
3039123fb08SJeremy L Thompson             code << "  LoadMatrix<" << P_name << ", " << Q_name << (is_tensor ? "" : "*dim") << ">(data, G." << option_name << "[" << i << "], s_G"
3049123fb08SJeremy L Thompson                  << var_suffix << ");\n";
3054b3e95d5SJeremy L Thompson           }
3064b3e95d5SJeremy L Thompson         }
3079ee499e5SJeremy L Thompson       }
3084b3e95d5SJeremy L Thompson       break;
3094b3e95d5SJeremy L Thompson     case CEED_EVAL_WEIGHT:
3104b3e95d5SJeremy L Thompson       break;  // No action
3114b3e95d5SJeremy L Thompson       // LCOV_EXCL_START
3124b3e95d5SJeremy L Thompson     case CEED_EVAL_DIV:
3134b3e95d5SJeremy L Thompson     case CEED_EVAL_CURL:
3144b3e95d5SJeremy L Thompson       break;  // TODO: Not implemented
3154b3e95d5SJeremy L Thompson               // LCOV_EXCL_STOP
3164b3e95d5SJeremy L Thompson   }
3173a2968d6SJeremy L Thompson   CeedCallBackend(CeedBasisDestroy(&basis));
3184b3e95d5SJeremy L Thompson   return CEED_ERROR_SUCCESS;
3194b3e95d5SJeremy L Thompson }
3204b3e95d5SJeremy L Thompson 
3214b3e95d5SJeremy L Thompson //------------------------------------------------------------------------------
3224b3e95d5SJeremy L Thompson // Restriction
3234b3e95d5SJeremy L Thompson //------------------------------------------------------------------------------
3244b3e95d5SJeremy L Thompson static int CeedOperatorBuildKernelRestriction_Hip_gen(std::ostringstream &code, CeedOperator_Hip_gen *data, CeedInt i, CeedInt dim,
325e93651e5SJeremy L Thompson                                                       CeedInt field_input_buffer[], CeedOperatorField op_field, CeedQFunctionField qf_field,
3269123fb08SJeremy L Thompson                                                       CeedInt Q_1d, bool is_input, bool is_tensor, bool is_at_points, bool use_3d_slices) {
3274b3e95d5SJeremy L Thompson   std::string              var_suffix = (is_input ? "_in_" : "_out_") + std::to_string(i);
3289123fb08SJeremy L Thompson   std::string              P_name     = (is_tensor ? "P_1d" : "P") + var_suffix;
3294b3e95d5SJeremy L Thompson   CeedEvalMode             eval_mode  = CEED_EVAL_NONE;
3304b3e95d5SJeremy L Thompson   CeedInt                  elem_size = 0, num_comp = 0, P_1d = 0;
3314b3e95d5SJeremy L Thompson   CeedSize                 l_size;
332f815fac9SJeremy L Thompson   CeedRestrictionType      rstr_type = CEED_RESTRICTION_STANDARD;
3334b3e95d5SJeremy L Thompson   CeedElemRestriction_Hip *rstr_data;
3344b3e95d5SJeremy L Thompson   CeedElemRestriction      elem_rstr;
3354b3e95d5SJeremy L Thompson   CeedBasis                basis;
3364b3e95d5SJeremy L Thompson 
3374b3e95d5SJeremy L Thompson   // Get field data
3384b3e95d5SJeremy L Thompson   CeedCallBackend(CeedOperatorFieldGetElemRestriction(op_field, &elem_rstr));
3394b3e95d5SJeremy L Thompson   if (elem_rstr != CEED_ELEMRESTRICTION_NONE) {
340f815fac9SJeremy L Thompson     CeedCallBackend(CeedElemRestrictionGetType(elem_rstr, &rstr_type));
3414b3e95d5SJeremy L Thompson     CeedCallBackend(CeedElemRestrictionGetElementSize(elem_rstr, &elem_size));
3424b3e95d5SJeremy L Thompson     CeedCallBackend(CeedElemRestrictionGetNumComponents(elem_rstr, &num_comp));
3434b3e95d5SJeremy L Thompson     CeedCallBackend(CeedElemRestrictionGetData(elem_rstr, &rstr_data));
3444b3e95d5SJeremy L Thompson   }
3454b3e95d5SJeremy L Thompson   CeedCallBackend(CeedOperatorFieldGetBasis(op_field, &basis));
3464b3e95d5SJeremy L Thompson   if (basis != CEED_BASIS_NONE) {
3479123fb08SJeremy L Thompson     if (is_tensor) CeedCallBackend(CeedBasisGetNumNodes1D(basis, &P_1d));
3489123fb08SJeremy L Thompson     else CeedCallBackend(CeedBasisGetNumNodes(basis, &P_1d));
3494b3e95d5SJeremy L Thompson   }
3503a2968d6SJeremy L Thompson   CeedCallBackend(CeedBasisDestroy(&basis));
3514b3e95d5SJeremy L Thompson   CeedCallBackend(CeedQFunctionFieldGetEvalMode(qf_field, &eval_mode));
3524b3e95d5SJeremy L Thompson 
3534b3e95d5SJeremy L Thompson   // Restriction
3544b3e95d5SJeremy L Thompson   if (is_input) {
3554b3e95d5SJeremy L Thompson     // Input
356e93651e5SJeremy L Thompson     if (field_input_buffer[i] != i) {
357e93651e5SJeremy L Thompson       std::string buffer_name = "r_e_in_" + std::to_string(field_input_buffer[i]);
358e93651e5SJeremy L Thompson 
359e93651e5SJeremy L Thompson       // Restriction was already done for previous input
360e93651e5SJeremy L Thompson       code << "    CeedScalar *r_e" << var_suffix << " = " << buffer_name << ";\n";
3613a2968d6SJeremy L Thompson     } else if (eval_mode != CEED_EVAL_WEIGHT && !((eval_mode == CEED_EVAL_NONE) && use_3d_slices && is_at_points)) {
3623a2968d6SJeremy L Thompson       if (eval_mode == CEED_EVAL_NONE && rstr_type != CEED_RESTRICTION_POINTS) {
363e93651e5SJeremy L Thompson         // No basis action, so r_e_in_* in also r_q_in_* and needs to be allocated
3644b3e95d5SJeremy L Thompson         code << "    CeedScalar r_e" << var_suffix << "[num_comp" << var_suffix << "*" << P_name << "];\n";
3653a2968d6SJeremy L Thompson       } else if (rstr_type != CEED_RESTRICTION_POINTS) {
366e93651e5SJeremy L Thompson         // Otherwise we're using the scratch space
367e93651e5SJeremy L Thompson         code << "    CeedScalar *r_e" << var_suffix << " = r_e_scratch;\n";
368e93651e5SJeremy L Thompson       }
369f815fac9SJeremy L Thompson       switch (rstr_type) {
370f815fac9SJeremy L Thompson         case CEED_RESTRICTION_STANDARD: {
3714b3e95d5SJeremy L Thompson           CeedInt comp_stride;
3724b3e95d5SJeremy L Thompson 
3734b3e95d5SJeremy L Thompson           CeedCallBackend(CeedElemRestrictionGetLVectorSize(elem_rstr, &l_size));
3744b3e95d5SJeremy L Thompson           code << "    const CeedInt l_size" << var_suffix << " = " << l_size << ";\n";
3754b3e95d5SJeremy L Thompson           CeedCallBackend(CeedElemRestrictionGetCompStride(elem_rstr, &comp_stride));
3764b3e95d5SJeremy L Thompson           code << "    // CompStride: " << comp_stride << "\n";
3774b3e95d5SJeremy L Thompson           data->indices.inputs[i] = (CeedInt *)rstr_data->d_offsets;
3789123fb08SJeremy L Thompson           code << "    ReadLVecStandard" << (is_tensor ? dim : 1) << "d<num_comp" << var_suffix << ", " << comp_stride << ", " << P_name
3799123fb08SJeremy L Thompson                << ">(data, l_size" << var_suffix << ", elem, indices.inputs[" << i << "], d" << var_suffix << ", r_e" << var_suffix << ");\n";
380f815fac9SJeremy L Thompson           break;
381f815fac9SJeremy L Thompson         }
382f815fac9SJeremy L Thompson         case CEED_RESTRICTION_STRIDED: {
3834b3e95d5SJeremy L Thompson           bool    has_backend_strides;
3844b3e95d5SJeremy L Thompson           CeedInt num_elem;
3854b3e95d5SJeremy L Thompson 
3864b3e95d5SJeremy L Thompson           CeedCallBackend(CeedElemRestrictionHasBackendStrides(elem_rstr, &has_backend_strides));
3874b3e95d5SJeremy L Thompson           CeedCallBackend(CeedElemRestrictionGetNumElements(elem_rstr, &num_elem));
3884b3e95d5SJeremy L Thompson           CeedInt strides[3] = {1, elem_size * num_elem, elem_size};
3894b3e95d5SJeremy L Thompson 
3904b3e95d5SJeremy L Thompson           if (!has_backend_strides) {
3914b3e95d5SJeremy L Thompson             CeedCallBackend(CeedElemRestrictionGetStrides(elem_rstr, strides));
3924b3e95d5SJeremy L Thompson           }
3934b3e95d5SJeremy L Thompson           code << "    // Strides: {" << strides[0] << ", " << strides[1] << ", " << strides[2] << "}\n";
3949123fb08SJeremy L Thompson           code << "    ReadLVecStrided" << (is_tensor ? dim : 1) << "d<num_comp" << var_suffix << ", " << P_name << ", " << strides[0] << ", "
3959123fb08SJeremy L Thompson                << strides[1] << ", " << strides[2] << ">(data, elem, d" << var_suffix << ", r_e" << var_suffix << ");\n";
396f815fac9SJeremy L Thompson           break;
397f815fac9SJeremy L Thompson         }
3983a2968d6SJeremy L Thompson         case CEED_RESTRICTION_POINTS: {
3993a2968d6SJeremy L Thompson           CeedInt comp_stride;
4003a2968d6SJeremy L Thompson 
4013a2968d6SJeremy L Thompson           CeedCallBackend(CeedElemRestrictionGetCompStride(elem_rstr, &comp_stride));
4023a2968d6SJeremy L Thompson           code << "    const CeedInt comp_stride" << var_suffix << " = " << comp_stride << ";\n";
4033a2968d6SJeremy L Thompson           data->indices.inputs[i] = (CeedInt *)rstr_data->d_offsets;
4043a2968d6SJeremy L Thompson           break;
4053a2968d6SJeremy L Thompson         }
406f815fac9SJeremy L Thompson         // LCOV_EXCL_START
407f815fac9SJeremy L Thompson         case CEED_RESTRICTION_ORIENTED:
408f815fac9SJeremy L Thompson         case CEED_RESTRICTION_CURL_ORIENTED:
409f815fac9SJeremy L Thompson           break;  // TODO: Not implemented
410f815fac9SJeremy L Thompson                   // LCOV_EXCL_STOP
4114b3e95d5SJeremy L Thompson       }
4124b3e95d5SJeremy L Thompson     }
4134b3e95d5SJeremy L Thompson   } else {
4144b3e95d5SJeremy L Thompson     // Output
415f815fac9SJeremy L Thompson     switch (rstr_type) {
416f815fac9SJeremy L Thompson       case CEED_RESTRICTION_STANDARD: {
4174b3e95d5SJeremy L Thompson         CeedInt comp_stride;
4184b3e95d5SJeremy L Thompson 
4194b3e95d5SJeremy L Thompson         CeedCallBackend(CeedElemRestrictionGetLVectorSize(elem_rstr, &l_size));
4204b3e95d5SJeremy L Thompson         code << "    const CeedInt l_size" << var_suffix << " = " << l_size << ";\n";
4214b3e95d5SJeremy L Thompson         CeedCallBackend(CeedElemRestrictionGetCompStride(elem_rstr, &comp_stride));
4224b3e95d5SJeremy L Thompson         code << "    // CompStride: " << comp_stride << "\n";
4234b3e95d5SJeremy L Thompson         data->indices.outputs[i] = (CeedInt *)rstr_data->d_offsets;
4249123fb08SJeremy L Thompson         code << "    WriteLVecStandard" << (is_tensor ? dim : 1) << "d<num_comp" << var_suffix << ", " << comp_stride << ", " << P_name
4259123fb08SJeremy L Thompson              << ">(data, l_size" << var_suffix << ", elem, indices.outputs[" << i << "], r_e" << var_suffix << ", d" << var_suffix << ");\n";
426f815fac9SJeremy L Thompson         break;
427f815fac9SJeremy L Thompson       }
428f815fac9SJeremy L Thompson       case CEED_RESTRICTION_STRIDED: {
4294b3e95d5SJeremy L Thompson         bool    has_backend_strides;
4304b3e95d5SJeremy L Thompson         CeedInt num_elem;
4314b3e95d5SJeremy L Thompson 
4324b3e95d5SJeremy L Thompson         CeedCallBackend(CeedElemRestrictionHasBackendStrides(elem_rstr, &has_backend_strides));
4334b3e95d5SJeremy L Thompson         CeedCallBackend(CeedElemRestrictionGetNumElements(elem_rstr, &num_elem));
4344b3e95d5SJeremy L Thompson         CeedInt strides[3] = {1, elem_size * num_elem, elem_size};
4354b3e95d5SJeremy L Thompson 
4364b3e95d5SJeremy L Thompson         if (!has_backend_strides) {
4374b3e95d5SJeremy L Thompson           CeedCallBackend(CeedElemRestrictionGetStrides(elem_rstr, strides));
4384b3e95d5SJeremy L Thompson         }
4394b3e95d5SJeremy L Thompson         code << "    // Strides: {" << strides[0] << ", " << strides[1] << ", " << strides[2] << "}\n";
4409123fb08SJeremy L Thompson         code << "    WriteLVecStrided" << (is_tensor ? dim : 1) << "d<num_comp" << var_suffix << ", " << P_name << ", " << strides[0] << ", "
4419123fb08SJeremy L Thompson              << strides[1] << ", " << strides[2] << ">(data, elem, r_e" << var_suffix << ", d" << var_suffix << ");\n";
442f815fac9SJeremy L Thompson         break;
443f815fac9SJeremy L Thompson       }
4443a2968d6SJeremy L Thompson       case CEED_RESTRICTION_POINTS:
4453a2968d6SJeremy L Thompson         data->indices.outputs[i] = (CeedInt *)rstr_data->d_offsets;
4463a2968d6SJeremy L Thompson         break;
447f815fac9SJeremy L Thompson       // LCOV_EXCL_START
448f815fac9SJeremy L Thompson       case CEED_RESTRICTION_ORIENTED:
449f815fac9SJeremy L Thompson       case CEED_RESTRICTION_CURL_ORIENTED:
450f815fac9SJeremy L Thompson         break;  // TODO: Not implemented
451f815fac9SJeremy L Thompson                 // LCOV_EXCL_STOP
4524b3e95d5SJeremy L Thompson     }
4534b3e95d5SJeremy L Thompson   }
4543a2968d6SJeremy L Thompson   CeedCallBackend(CeedElemRestrictionDestroy(&elem_rstr));
4554b3e95d5SJeremy L Thompson   return CEED_ERROR_SUCCESS;
4564b3e95d5SJeremy L Thompson }
4574b3e95d5SJeremy L Thompson 
4584b3e95d5SJeremy L Thompson //------------------------------------------------------------------------------
4594b3e95d5SJeremy L Thompson // Basis
4604b3e95d5SJeremy L Thompson //------------------------------------------------------------------------------
4614b3e95d5SJeremy L Thompson static int CeedOperatorBuildKernelBasis_Hip_gen(std::ostringstream &code, CeedOperator_Hip_gen *data, CeedInt i, CeedInt dim,
4629123fb08SJeremy L Thompson                                                 CeedOperatorField op_field, CeedQFunctionField qf_field, CeedInt Q_1d, bool is_input, bool is_tensor,
4633a2968d6SJeremy L Thompson                                                 bool is_at_points, bool use_3d_slices) {
4644b3e95d5SJeremy L Thompson   std::string         var_suffix = (is_input ? "_in_" : "_out_") + std::to_string(i);
4659123fb08SJeremy L Thompson   std::string         P_name = (is_tensor ? "P_1d" : "P") + var_suffix, Q_name = is_tensor ? "Q_1d" : "Q";
4664b3e95d5SJeremy L Thompson   CeedEvalMode        eval_mode = CEED_EVAL_NONE;
4674b3e95d5SJeremy L Thompson   CeedInt             elem_size = 0, num_comp = 0, P_1d = 0;
4684b3e95d5SJeremy L Thompson   CeedElemRestriction elem_rstr;
4694b3e95d5SJeremy L Thompson   CeedBasis           basis;
4704b3e95d5SJeremy L Thompson 
4714b3e95d5SJeremy L Thompson   // Get field data
4724b3e95d5SJeremy L Thompson   CeedCallBackend(CeedOperatorFieldGetElemRestriction(op_field, &elem_rstr));
4734b3e95d5SJeremy L Thompson   if (elem_rstr != CEED_ELEMRESTRICTION_NONE) {
4744b3e95d5SJeremy L Thompson     CeedCallBackend(CeedElemRestrictionGetElementSize(elem_rstr, &elem_size));
4754b3e95d5SJeremy L Thompson     CeedCallBackend(CeedElemRestrictionGetNumComponents(elem_rstr, &num_comp));
4764b3e95d5SJeremy L Thompson   }
4773a2968d6SJeremy L Thompson   CeedCallBackend(CeedElemRestrictionDestroy(&elem_rstr));
4784b3e95d5SJeremy L Thompson   CeedCallBackend(CeedOperatorFieldGetBasis(op_field, &basis));
4794b3e95d5SJeremy L Thompson   if (basis != CEED_BASIS_NONE) {
4809123fb08SJeremy L Thompson     if (is_tensor) CeedCallBackend(CeedBasisGetNumNodes1D(basis, &P_1d));
4819123fb08SJeremy L Thompson     else CeedCallBackend(CeedBasisGetNumNodes(basis, &P_1d));
4824b3e95d5SJeremy L Thompson   }
4834b3e95d5SJeremy L Thompson   CeedCallBackend(CeedQFunctionFieldGetEvalMode(qf_field, &eval_mode));
4844b3e95d5SJeremy L Thompson 
4854b3e95d5SJeremy L Thompson   // Basis
4864b3e95d5SJeremy L Thompson   code << "    // EvalMode: " << CeedEvalModes[eval_mode] << "\n";
4874b3e95d5SJeremy L Thompson   if (is_input) {
4884b3e95d5SJeremy L Thompson     switch (eval_mode) {
4894b3e95d5SJeremy L Thompson       case CEED_EVAL_NONE:
4903a2968d6SJeremy L Thompson         if (!use_3d_slices && !is_at_points) {
4914b3e95d5SJeremy L Thompson           code << "    CeedScalar *r_q" << var_suffix << " = r_e" << var_suffix << ";\n";
4924b3e95d5SJeremy L Thompson         }
4934b3e95d5SJeremy L Thompson         break;
4944b3e95d5SJeremy L Thompson       case CEED_EVAL_INTERP:
4953a2968d6SJeremy L Thompson         if (is_at_points) {
4969123fb08SJeremy L Thompson           std::string function_name = (dim == 1 ? "Interp" : "InterpTensor") + std::to_string(dim) + "d";
4979123fb08SJeremy L Thompson 
4989123fb08SJeremy L Thompson           code << "    CeedScalar r_c" << var_suffix << "[num_comp" << var_suffix << "*" << (dim >= 3 ? Q_name : "1") << "];\n";
4996b92dc4bSJeremy L Thompson           code << "    " << function_name << "<num_comp" << var_suffix << ", " << P_name << ", " << Q_name << ", OP_T_1D>(data, r_e" << var_suffix
5006b92dc4bSJeremy L Thompson                << ", s_B" << var_suffix << ", r_c" << var_suffix << ");\n";
5013a2968d6SJeremy L Thompson         } else {
5029123fb08SJeremy L Thompson           std::string function_name = is_tensor ? ((dim == 1 ? "Interp" : "InterpTensor") + std::to_string(dim) + "d") : "InterpNonTensor";
5039123fb08SJeremy L Thompson 
5049123fb08SJeremy L Thompson           code << "    CeedScalar r_q" << var_suffix << "[num_comp" << var_suffix << "*" << (is_tensor && (dim >= 3) ? Q_name : "1") << "];\n";
5056b92dc4bSJeremy L Thompson           code << "    " << function_name << "<num_comp" << var_suffix << ", " << P_name << ", " << Q_name << ", OP_T_1D>(data, r_e" << var_suffix
5066b92dc4bSJeremy L Thompson                << ", s_B" << var_suffix << ", r_q" << var_suffix << ");\n";
5073a2968d6SJeremy L Thompson         }
5084b3e95d5SJeremy L Thompson         break;
5094b3e95d5SJeremy L Thompson       case CEED_EVAL_GRAD:
5103a2968d6SJeremy L Thompson         if (is_at_points) {
5119123fb08SJeremy L Thompson           std::string function_name = (dim == 1 ? "Interp" : "InterpTensor") + std::to_string(dim) + "d";
5129123fb08SJeremy L Thompson 
5139123fb08SJeremy L Thompson           code << "    CeedScalar r_c" << var_suffix << "[num_comp" << var_suffix << "*" << (dim >= 3 ? Q_name : "1") << "];\n";
5146b92dc4bSJeremy L Thompson           code << "    " << function_name << "<num_comp" << var_suffix << ", " << P_name << ", " << Q_name << ", OP_T_1D>(data, r_e" << var_suffix
5156b92dc4bSJeremy L Thompson                << ", s_B" << var_suffix << ", r_c" << var_suffix << ");\n";
5163a2968d6SJeremy L Thompson         } else if (use_3d_slices) {
5179123fb08SJeremy L Thompson           std::string function_name = (dim > 1 ? "InterpTensor" : "Interp") + std::to_string(dim) + "d";
5189123fb08SJeremy L Thompson 
5194b3e95d5SJeremy L Thompson           code << "    CeedScalar r_q" << var_suffix << "[num_comp" << var_suffix << "*" << Q_name << "];\n";
5206b92dc4bSJeremy L Thompson           code << "    " << function_name << "<num_comp" << var_suffix << ", " << P_name << ", " << Q_name << ", OP_T_1D>(data, r_e" << var_suffix
5216b92dc4bSJeremy L Thompson                << ", s_B" << var_suffix << ", r_q" << var_suffix << ");\n";
5229123fb08SJeremy L Thompson         } else if (is_tensor) {
5239123fb08SJeremy L Thompson           bool        is_collocated = dim == 3 && Q_1d >= P_1d;
5249123fb08SJeremy L Thompson           std::string function_name = (dim == 1 ? "Grad" : (is_collocated ? "GradTensorCollocated" : "GradTensor")) + std::to_string(dim) + "d";
5259123fb08SJeremy L Thompson 
5269123fb08SJeremy L Thompson           code << "    CeedScalar r_q" << var_suffix << "[num_comp" << var_suffix << "*dim*" << (dim >= 3 ? Q_name : "1") << "];\n";
5276b92dc4bSJeremy L Thompson           code << "    " << function_name << "<num_comp" << var_suffix << ", " << P_name << ", " << Q_name << ", OP_T_1D>(data, r_e" << var_suffix
5286b92dc4bSJeremy L Thompson                << ", s_B" << var_suffix << ", s_G" << var_suffix << ", r_q" << var_suffix << ");\n";
5294b3e95d5SJeremy L Thompson         } else {
5309123fb08SJeremy L Thompson           std::string function_name = "GradNonTensor";
5319123fb08SJeremy L Thompson 
5329123fb08SJeremy L Thompson           code << "    CeedScalar r_q" << var_suffix << "[num_comp" << var_suffix << "*dim];\n";
5336b92dc4bSJeremy L Thompson           code << "    " << function_name << "<num_comp" << var_suffix << ", dim, " << P_name << ", " << Q_name << ", OP_T_1D>(data, r_e"
5346b92dc4bSJeremy L Thompson                << var_suffix << ", s_G" << var_suffix << ", r_q" << var_suffix << ");\n";
5354b3e95d5SJeremy L Thompson         }
5364b3e95d5SJeremy L Thompson         break;
5374b3e95d5SJeremy L Thompson       case CEED_EVAL_WEIGHT: {
5383a2968d6SJeremy L Thompson         if (is_at_points) {
5393a2968d6SJeremy L Thompson           code << "    // Nothing to do AtPoints\n";
5403a2968d6SJeremy L Thompson         } else {
5414b3e95d5SJeremy L Thompson           CeedBasis_Hip_shared *basis_data;
5429123fb08SJeremy L Thompson           std::string           function_name = is_tensor ? ((dim == 1 ? "Weight" : "WeightTensor") + std::to_string(dim) + "d") : "WeightNonTensor";
5434b3e95d5SJeremy L Thompson 
5449123fb08SJeremy L Thompson           code << "    CeedScalar r_q" << var_suffix << "[" << (is_tensor && (dim >= 3) ? Q_name : "1") << "];\n";
5454b3e95d5SJeremy L Thompson           CeedCallBackend(CeedBasisGetData(basis, &basis_data));
5464b3e95d5SJeremy L Thompson           data->W = basis_data->d_q_weight_1d;
547*343e3094SJeremy L Thompson           code << "    " << function_name << "<" << P_name << ", " << Q_name << ">(data, W, r_q" << var_suffix << ");\n";
5483a2968d6SJeremy L Thompson         }
5494b3e95d5SJeremy L Thompson         break;
5504b3e95d5SJeremy L Thompson       }
5514b3e95d5SJeremy L Thompson       // LCOV_EXCL_START
5524b3e95d5SJeremy L Thompson       case CEED_EVAL_DIV:
5534b3e95d5SJeremy L Thompson       case CEED_EVAL_CURL:
5544b3e95d5SJeremy L Thompson         break;  // TODO: Not implemented
5554b3e95d5SJeremy L Thompson                 // LCOV_EXCL_STOP
5564b3e95d5SJeremy L Thompson     }
5574b3e95d5SJeremy L Thompson   } else {
5584b3e95d5SJeremy L Thompson     switch (eval_mode) {
5594b3e95d5SJeremy L Thompson       case CEED_EVAL_NONE:
5604b3e95d5SJeremy L Thompson         code << "    CeedScalar *r_e" << var_suffix << " = r_q" << var_suffix << ";\n";
5614b3e95d5SJeremy L Thompson         break;  // No action
5624b3e95d5SJeremy L Thompson       case CEED_EVAL_INTERP:
563e93651e5SJeremy L Thompson         code << "    CeedScalar *r_e" << var_suffix << " = r_e_scratch;\n";
5643a2968d6SJeremy L Thompson         if (is_at_points) {
5659123fb08SJeremy L Thompson           std::string function_name = (dim == 1 ? "InterpTranspose" : "InterpTransposeTensor") + std::to_string(dim) + "d";
5669123fb08SJeremy L Thompson 
5676b92dc4bSJeremy L Thompson           code << "    " << function_name << "<num_comp" << var_suffix << ", " << P_name << ", " << Q_name << ", OP_T_1D>(data, r_c" << var_suffix
5686b92dc4bSJeremy L Thompson                << ", s_B" << var_suffix << ", r_e" << var_suffix << ");\n";
5693a2968d6SJeremy L Thompson         } else {
5709123fb08SJeremy L Thompson           std::string function_name =
5719123fb08SJeremy L Thompson               is_tensor ? ((dim == 1 ? "InterpTranspose" : "InterpTransposeTensor") + std::to_string(dim) + "d") : "InterpTransposeNonTensor";
5729123fb08SJeremy L Thompson 
5736b92dc4bSJeremy L Thompson           code << "    " << function_name << "<num_comp" << var_suffix << ", " << P_name << ", " << Q_name << ", OP_T_1D>(data, r_q" << var_suffix
5746b92dc4bSJeremy L Thompson                << ", s_B" << var_suffix << ", r_e" << var_suffix << ");\n";
5753a2968d6SJeremy L Thompson         }
5764b3e95d5SJeremy L Thompson         break;
5774b3e95d5SJeremy L Thompson       case CEED_EVAL_GRAD:
578e93651e5SJeremy L Thompson         code << "    CeedScalar *r_e" << var_suffix << " = r_e_scratch;\n";
5793a2968d6SJeremy L Thompson         if (is_at_points) {
5809123fb08SJeremy L Thompson           std::string function_name = (dim == 1 ? "InterpTranspose" : "InterpTransposeTensor") + std::to_string(dim) + "d";
5819123fb08SJeremy L Thompson 
5826b92dc4bSJeremy L Thompson           code << "    " << function_name << "<num_comp" << var_suffix << ", " << P_name << ", " << Q_name << ", OP_T_1D>(data, r_c" << var_suffix
5836b92dc4bSJeremy L Thompson                << ", s_B" << var_suffix << ", r_e" << var_suffix << ");\n";
5843a2968d6SJeremy L Thompson         } else if (use_3d_slices) {
5859123fb08SJeremy L Thompson           std::string function_name = (dim == 1 ? "InterpTranspose" : "InterpTransposeTensor") + std::to_string(dim) + "d";
5869123fb08SJeremy L Thompson 
5876b92dc4bSJeremy L Thompson           code << "    " << function_name << "<num_comp" << var_suffix << ", " << P_name << ", " << Q_name << ", OP_T_1D>(data, r_q" << var_suffix
5886b92dc4bSJeremy L Thompson                << ", s_B" << var_suffix << ", r_e" << var_suffix << ");\n";
5899123fb08SJeremy L Thompson         } else if (is_tensor) {
5909123fb08SJeremy L Thompson           bool        is_collocated = dim == 3 && Q_1d >= P_1d;
5919123fb08SJeremy L Thompson           std::string function_name =
5929123fb08SJeremy L Thompson               (dim == 1 ? "GradTranspose" : (is_collocated ? "GradTransposeTensorCollocated" : "GradTransposeTensor")) + std::to_string(dim) + "d";
5939123fb08SJeremy L Thompson 
5946b92dc4bSJeremy L Thompson           code << "    " << function_name << "<num_comp" << var_suffix << ", " << P_name << ", " << Q_name << ", OP_T_1D>(data, r_q" << var_suffix
5956b92dc4bSJeremy L Thompson                << ", s_B" << var_suffix << ", s_G" << var_suffix << ", r_e" << var_suffix << ");\n";
5964b3e95d5SJeremy L Thompson         } else {
5979123fb08SJeremy L Thompson           std::string function_name = "GradTransposeNonTensor";
5989123fb08SJeremy L Thompson 
5996b92dc4bSJeremy L Thompson           code << "    " << function_name << "<num_comp" << var_suffix << ", dim, " << P_name << ", " << Q_name << ", OP_T_1D>(data, r_q"
6006b92dc4bSJeremy L Thompson                << var_suffix << ", s_G" << var_suffix << ", r_e" << var_suffix << ");\n";
6014b3e95d5SJeremy L Thompson         }
6024b3e95d5SJeremy L Thompson         break;
6034b3e95d5SJeremy L Thompson       // LCOV_EXCL_START
6044b3e95d5SJeremy L Thompson       case CEED_EVAL_WEIGHT:
6054b3e95d5SJeremy L Thompson         break;  // Should not occur
6064b3e95d5SJeremy L Thompson       case CEED_EVAL_DIV:
6074b3e95d5SJeremy L Thompson       case CEED_EVAL_CURL:
6084b3e95d5SJeremy L Thompson         break;  // TODO: Not implemented
6094b3e95d5SJeremy L Thompson                 // LCOV_EXCL_STOP
6104b3e95d5SJeremy L Thompson     }
6114b3e95d5SJeremy L Thompson   }
6123a2968d6SJeremy L Thompson   CeedCallBackend(CeedBasisDestroy(&basis));
6134b3e95d5SJeremy L Thompson   return CEED_ERROR_SUCCESS;
6144b3e95d5SJeremy L Thompson }
6154b3e95d5SJeremy L Thompson 
6164b3e95d5SJeremy L Thompson //------------------------------------------------------------------------------
6174b3e95d5SJeremy L Thompson // QFunction
6184b3e95d5SJeremy L Thompson //------------------------------------------------------------------------------
6193a2968d6SJeremy L Thompson static int CeedOperatorBuildKernelQFunction_Hip_gen(std::ostringstream &code, CeedOperator_Hip_gen *data, CeedInt dim, CeedInt max_num_points,
6203a2968d6SJeremy L Thompson                                                     CeedInt num_input_fields, CeedOperatorField *op_input_fields, CeedQFunctionField *qf_input_fields,
6214b3e95d5SJeremy L Thompson                                                     CeedInt num_output_fields, CeedOperatorField *op_output_fields,
6229123fb08SJeremy L Thompson                                                     CeedQFunctionField *qf_output_fields, std::string qfunction_name, CeedInt Q_1d, bool is_tensor,
6239123fb08SJeremy L Thompson                                                     bool is_at_points, bool use_3d_slices) {
6249123fb08SJeremy L Thompson   std::string         Q_name    = is_tensor ? "Q_1d" : "Q";
6254b3e95d5SJeremy L Thompson   CeedEvalMode        eval_mode = CEED_EVAL_NONE;
6264b3e95d5SJeremy L Thompson   CeedElemRestriction elem_rstr;
6274b3e95d5SJeremy L Thompson 
6288b97b69aSJeremy L Thompson   // Setup output arrays
6294b3e95d5SJeremy L Thompson   code << "\n    // -- Output field setup\n";
6304b3e95d5SJeremy L Thompson   for (CeedInt i = 0; i < num_output_fields; i++) {
63159fa3f92SJeremy L Thompson     const char *field_name;
6324b3e95d5SJeremy L Thompson     std::string var_suffix = "_out_" + std::to_string(i);
6334b3e95d5SJeremy L Thompson 
63459fa3f92SJeremy L Thompson     CeedCallBackend(CeedOperatorFieldGetName(op_output_fields[i], &field_name));
63559fa3f92SJeremy L Thompson     code << "    // ---- Output field " << i << ": " << field_name << "\n";
6364b3e95d5SJeremy L Thompson     CeedCallBackend(CeedQFunctionFieldGetEvalMode(qf_output_fields[i], &eval_mode));
6373a2968d6SJeremy L Thompson     switch (eval_mode) {
6383a2968d6SJeremy L Thompson       case CEED_EVAL_NONE:
6393a2968d6SJeremy L Thompson         if (is_at_points) {
6403a2968d6SJeremy L Thompson           code << "    CeedScalar r_q" << var_suffix << "[num_comp" << var_suffix << "];\n";
6413a2968d6SJeremy L Thompson         } else {
6429123fb08SJeremy L Thompson           code << "    CeedScalar r_q" << var_suffix << "[num_comp" << var_suffix << "*" << (is_tensor && (dim >= 3) ? Q_name : "1") << "];\n";
6434b3e95d5SJeremy L Thompson         }
6443a2968d6SJeremy L Thompson         break;
6453a2968d6SJeremy L Thompson       case CEED_EVAL_INTERP:
6463a2968d6SJeremy L Thompson         if (is_at_points) {
6473a2968d6SJeremy L Thompson           // Accumulator for point data
6489123fb08SJeremy L Thompson           code << "    CeedScalar r_c" << var_suffix << "[num_comp" << var_suffix << "*" << (dim >= 3 ? Q_name : "1") << "];\n";
6499123fb08SJeremy L Thompson           code << "    for (CeedInt i = 0; i < num_comp" << var_suffix << "*" << (dim >= 3 ? Q_name : "1") << "; i++) {\n";
6503a2968d6SJeremy L Thompson           code << "      r_c" << var_suffix << "[i] = 0.0;\n";
6513a2968d6SJeremy L Thompson           code << "    }\n";
6523a2968d6SJeremy L Thompson         } else {
6539123fb08SJeremy L Thompson           code << "    CeedScalar r_q" << var_suffix << "[num_comp" << var_suffix << "*" << (is_tensor && (dim >= 3) ? Q_name : "1") << "];\n";
6543a2968d6SJeremy L Thompson         }
6553a2968d6SJeremy L Thompson         break;
6563a2968d6SJeremy L Thompson       case CEED_EVAL_GRAD:
6573a2968d6SJeremy L Thompson         if (is_at_points) {
6583a2968d6SJeremy L Thompson           // Accumulator for point data
6599123fb08SJeremy L Thompson           code << "    CeedScalar r_c" << var_suffix << "[num_comp" << var_suffix << "*" << (dim >= 3 ? Q_name : "1") << "*dim];\n";
6609123fb08SJeremy L Thompson           code << "    for (CeedInt i = 0; i < num_comp" << var_suffix << "*" << (dim >= 3 ? Q_name : "1") << "; i++) {\n";
6613a2968d6SJeremy L Thompson           code << "      r_c" << var_suffix << "[i] = 0.0;\n";
6623a2968d6SJeremy L Thompson           code << "    }\n";
6633a2968d6SJeremy L Thompson         } else if (use_3d_slices) {
6644b3e95d5SJeremy L Thompson           // Accumulator for gradient slices
6654b3e95d5SJeremy L Thompson           code << "    CeedScalar r_q" << var_suffix << "[num_comp" << var_suffix << "*" << Q_name << "];\n";
6664b3e95d5SJeremy L Thompson           code << "    for (CeedInt i = 0; i < num_comp" << var_suffix << "*" << Q_name << "; i++) {\n";
6674b3e95d5SJeremy L Thompson           code << "      r_q" << var_suffix << "[i] = 0.0;\n";
6684b3e95d5SJeremy L Thompson           code << "    }\n";
6694b3e95d5SJeremy L Thompson         } else {
6709123fb08SJeremy L Thompson           code << "    CeedScalar r_q" << var_suffix << "[num_comp" << var_suffix << "*dim*" << (is_tensor && (dim >= 3) ? Q_name : "1") << "];\n";
6714b3e95d5SJeremy L Thompson         }
6723a2968d6SJeremy L Thompson         break;
6733a2968d6SJeremy L Thompson       case CEED_EVAL_WEIGHT:
6743a2968d6SJeremy L Thompson         break;
6753a2968d6SJeremy L Thompson         // LCOV_EXCL_START
6763a2968d6SJeremy L Thompson       case CEED_EVAL_DIV:
6773a2968d6SJeremy L Thompson       case CEED_EVAL_CURL:
6783a2968d6SJeremy L Thompson         break;  // TODO: Not implemented
6793a2968d6SJeremy L Thompson                 // LCOV_EXCL_STOP
6804b3e95d5SJeremy L Thompson     }
6814b3e95d5SJeremy L Thompson   }
6824b3e95d5SJeremy L Thompson 
6833a2968d6SJeremy L Thompson   if (is_at_points) {
6843a2968d6SJeremy L Thompson     // We need to handle batches of points
6853a2968d6SJeremy L Thompson     code << "\n    // Note: Using batches of points\n";
6863a2968d6SJeremy L Thompson     code << "    const CeedInt point_loop_bound = (blockDim.x * blockDim.y) * ceil(1.0 * max_num_points / (blockDim.x * blockDim.y));\n\n";
6873a2968d6SJeremy L Thompson     code << "    #pragma unroll\n";
6883a2968d6SJeremy L Thompson     code << "    for (CeedInt i = threadIdx.x + threadIdx.y * blockDim.x; i < point_loop_bound; i += blockDim.x * blockDim.y) {\n";
6893a2968d6SJeremy L Thompson     code << "      const CeedInt p = i % max_num_points;\n\n";
6903a2968d6SJeremy L Thompson 
6913a2968d6SJeremy L Thompson     code << "      // -- Coordinates\n";
6923a2968d6SJeremy L Thompson     code << "      CeedScalar r_x[dim];\n";
6933a2968d6SJeremy 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";
6943a2968d6SJeremy L Thompson 
6953a2968d6SJeremy L Thompson     code << "      // -- Input fields\n";
6963a2968d6SJeremy L Thompson     for (CeedInt i = 0; i < num_input_fields; i++) {
69759fa3f92SJeremy L Thompson       const char *field_name;
6983a2968d6SJeremy L Thompson       std::string var_suffix = "_in_" + std::to_string(i);
699f725b54bSJeremy L Thompson       std::string P_name     = "P_1d" + var_suffix;
7003a2968d6SJeremy L Thompson 
70159fa3f92SJeremy L Thompson       CeedCallBackend(CeedOperatorFieldGetName(op_input_fields[i], &field_name));
70259fa3f92SJeremy L Thompson       code << "      // ---- Input field " << i << ": " << field_name << "\n";
7033a2968d6SJeremy L Thompson       CeedCallBackend(CeedQFunctionFieldGetEvalMode(qf_input_fields[i], &eval_mode));
7043a2968d6SJeremy L Thompson       // Basis action
7053a2968d6SJeremy L Thompson       code << "      // EvalMode: " << CeedEvalModes[eval_mode] << "\n";
7063a2968d6SJeremy L Thompson       switch (eval_mode) {
7073a2968d6SJeremy L Thompson         case CEED_EVAL_NONE:
7083a2968d6SJeremy L Thompson           code << "      CeedScalar r_s" << var_suffix << "[num_comp" << var_suffix << "];\n";
7093a2968d6SJeremy L Thompson           code << "      ReadPoint<num_comp" << var_suffix << ", comp_stride" << var_suffix
7103a2968d6SJeremy L Thompson                << ", max_num_points>(data, elem, p, max_num_points, indices.inputs[" << i << "], d" << var_suffix << ", r_s" << var_suffix << ");\n";
7113a2968d6SJeremy L Thompson           break;
7123a2968d6SJeremy L Thompson         case CEED_EVAL_INTERP:
7133a2968d6SJeremy L Thompson           code << "      CeedScalar r_s" << var_suffix << "[num_comp" << var_suffix << "];\n";
714f725b54bSJeremy L Thompson           code << "      InterpAtPoints" << dim << "d<num_comp" << var_suffix << ", max_num_points, " << P_name << ", " << Q_name << ">(data, i, r_c"
715f725b54bSJeremy L Thompson                << var_suffix << ", r_x, r_s" << var_suffix << ");\n";
7163a2968d6SJeremy L Thompson           break;
7173a2968d6SJeremy L Thompson         case CEED_EVAL_GRAD:
7183a2968d6SJeremy L Thompson           code << "      CeedScalar r_s" << var_suffix << "[num_comp" << var_suffix << "*dim];\n";
719f725b54bSJeremy L Thompson           code << "      GradAtPoints" << dim << "d<num_comp" << var_suffix << ", max_num_points, " << P_name << ", " << Q_name << ">(data, i, r_c"
720f725b54bSJeremy L Thompson                << var_suffix << ", r_x, r_s" << var_suffix << ");\n";
7213a2968d6SJeremy L Thompson           break;
7223a2968d6SJeremy L Thompson         case CEED_EVAL_WEIGHT:
7233a2968d6SJeremy L Thompson           code << "      CeedScalar r_s" << var_suffix << "[1];\n";
7243a2968d6SJeremy L Thompson           code << "      r_s" << var_suffix << "[0] = 1.0;\n";
7253a2968d6SJeremy L Thompson           break;
7263a2968d6SJeremy L Thompson           // LCOV_EXCL_START
7273a2968d6SJeremy L Thompson         case CEED_EVAL_DIV:
7283a2968d6SJeremy L Thompson         case CEED_EVAL_CURL:
7293a2968d6SJeremy L Thompson           break;  // TODO: Not implemented
7303a2968d6SJeremy L Thompson                   // LCOV_EXCL_STOP
7313a2968d6SJeremy L Thompson       }
7323a2968d6SJeremy L Thompson     }
7333a2968d6SJeremy L Thompson     code << "\n      // -- Output fields\n";
7343a2968d6SJeremy L Thompson     for (CeedInt i = 0; i < num_output_fields; i++) {
73559fa3f92SJeremy L Thompson       const char *field_name;
7363a2968d6SJeremy L Thompson       std::string var_suffix = "_out_" + std::to_string(i);
7373a2968d6SJeremy L Thompson 
73859fa3f92SJeremy L Thompson       CeedCallBackend(CeedOperatorFieldGetName(op_output_fields[i], &field_name));
73959fa3f92SJeremy L Thompson       code << "      // ---- Output field " << i << ": " << field_name << "\n";
7403a2968d6SJeremy L Thompson       CeedCallBackend(CeedQFunctionFieldGetEvalMode(qf_output_fields[i], &eval_mode));
7413a2968d6SJeremy L Thompson       // Basis action
7423a2968d6SJeremy L Thompson       switch (eval_mode) {
7433a2968d6SJeremy L Thompson         case CEED_EVAL_NONE:
7443a2968d6SJeremy L Thompson           code << "      CeedScalar r_s" << var_suffix << "[num_comp" << var_suffix << "];\n";
7453a2968d6SJeremy L Thompson           break;
7463a2968d6SJeremy L Thompson         case CEED_EVAL_INTERP:
7473a2968d6SJeremy L Thompson           code << "      CeedScalar r_s" << var_suffix << "[num_comp" << var_suffix << "];\n";
7483a2968d6SJeremy L Thompson           break;
7493a2968d6SJeremy L Thompson         case CEED_EVAL_GRAD:
7503a2968d6SJeremy L Thompson           code << "      CeedScalar r_s" << var_suffix << "[num_comp" << var_suffix << "*dim];\n";
7513a2968d6SJeremy L Thompson           break;
7523a2968d6SJeremy L Thompson           // LCOV_EXCL_START
7533a2968d6SJeremy L Thompson         case CEED_EVAL_WEIGHT:
7543a2968d6SJeremy L Thompson           break;  // Should not occur
7553a2968d6SJeremy L Thompson         case CEED_EVAL_DIV:
7563a2968d6SJeremy L Thompson         case CEED_EVAL_CURL:
7573a2968d6SJeremy L Thompson           break;  // TODO: Not implemented
7583a2968d6SJeremy L Thompson                   // LCOV_EXCL_STOP
7593a2968d6SJeremy L Thompson       }
7603a2968d6SJeremy L Thompson     }
7613a2968d6SJeremy L Thompson 
7623a2968d6SJeremy L Thompson   } else if (use_3d_slices) {
7634b3e95d5SJeremy L Thompson     // We treat quadrature points per slice in 3d to save registers
7644b3e95d5SJeremy L Thompson     code << "\n    // Note: Using planes of 3D elements\n";
7654b3e95d5SJeremy L Thompson     code << "    #pragma unroll\n";
7664b3e95d5SJeremy L Thompson     code << "    for (CeedInt q = 0; q < " << Q_name << "; q++) {\n";
7674b3e95d5SJeremy L Thompson     code << "      // -- Input fields\n";
7684b3e95d5SJeremy L Thompson     for (CeedInt i = 0; i < num_input_fields; i++) {
76959fa3f92SJeremy L Thompson       const char *field_name;
7704b3e95d5SJeremy L Thompson       std::string var_suffix = "_in_" + std::to_string(i);
7714b3e95d5SJeremy L Thompson 
77259fa3f92SJeremy L Thompson       CeedCallBackend(CeedOperatorFieldGetName(op_input_fields[i], &field_name));
77359fa3f92SJeremy L Thompson       code << "      // ---- Input field " << i << ": " << field_name << "\n";
7744b3e95d5SJeremy L Thompson       CeedCallBackend(CeedQFunctionFieldGetEvalMode(qf_input_fields[i], &eval_mode));
7754b3e95d5SJeremy L Thompson       // Basis action
7764b3e95d5SJeremy L Thompson       code << "      // EvalMode: " << CeedEvalModes[eval_mode] << "\n";
7774b3e95d5SJeremy L Thompson       switch (eval_mode) {
7784b3e95d5SJeremy L Thompson         case CEED_EVAL_NONE:
7794b3e95d5SJeremy L Thompson           bool is_strided;
7804b3e95d5SJeremy L Thompson 
7814b3e95d5SJeremy L Thompson           code << "      CeedScalar r_s" << var_suffix << "[num_comp" << var_suffix << "];\n";
7824b3e95d5SJeremy L Thompson 
7834b3e95d5SJeremy L Thompson           CeedCallBackend(CeedOperatorFieldGetElemRestriction(op_input_fields[i], &elem_rstr));
7844b3e95d5SJeremy L Thompson           CeedCallBackend(CeedElemRestrictionIsStrided(elem_rstr, &is_strided));
7854b3e95d5SJeremy L Thompson           if (is_strided) {
7864b3e95d5SJeremy L Thompson             bool    has_backend_strides;
7874b3e95d5SJeremy L Thompson             CeedInt num_elem, elem_size;
7884b3e95d5SJeremy L Thompson 
7894b3e95d5SJeremy L Thompson             CeedCallBackend(CeedElemRestrictionGetElementSize(elem_rstr, &elem_size));
7904b3e95d5SJeremy L Thompson             CeedCallBackend(CeedElemRestrictionHasBackendStrides(elem_rstr, &has_backend_strides));
7914b3e95d5SJeremy L Thompson             CeedCallBackend(CeedElemRestrictionGetNumElements(elem_rstr, &num_elem));
7924b3e95d5SJeremy L Thompson             CeedInt strides[3] = {1, elem_size * num_elem, elem_size};
7934b3e95d5SJeremy L Thompson 
7944b3e95d5SJeremy L Thompson             if (!has_backend_strides) {
7954b3e95d5SJeremy L Thompson               CeedCallBackend(CeedElemRestrictionGetStrides(elem_rstr, strides));
7964b3e95d5SJeremy L Thompson             }
7974b3e95d5SJeremy L Thompson             code << "      // Strides: {" << strides[0] << ", " << strides[1] << ", " << strides[2] << "}\n";
798f815fac9SJeremy L Thompson             code << "      ReadEVecSliceStrided3d<num_comp" << var_suffix << ", " << Q_name << ", " << strides[0] << ", " << strides[1] << ", "
7994b3e95d5SJeremy L Thompson                  << strides[2] << ">(data, elem, q, d" << var_suffix << ", r_s" << var_suffix << ");\n";
8004b3e95d5SJeremy L Thompson           } else {
8014b3e95d5SJeremy L Thompson             CeedSize                 l_size = 0;
8024b3e95d5SJeremy L Thompson             CeedInt                  comp_stride;
8034b3e95d5SJeremy L Thompson             CeedElemRestriction_Hip *rstr_data;
8044b3e95d5SJeremy L Thompson 
8054b3e95d5SJeremy L Thompson             CeedCallBackend(CeedElemRestrictionGetLVectorSize(elem_rstr, &l_size));
8064b3e95d5SJeremy L Thompson             code << "      const CeedInt l_size" << var_suffix << " = " << l_size << ";\n";
8074b3e95d5SJeremy L Thompson             CeedCallBackend(CeedElemRestrictionGetCompStride(elem_rstr, &comp_stride));
8084b3e95d5SJeremy L Thompson             code << "      // CompStride: " << comp_stride << "\n";
8094b3e95d5SJeremy L Thompson             CeedCallBackend(CeedElemRestrictionGetData(elem_rstr, &rstr_data));
8104b3e95d5SJeremy L Thompson             data->indices.inputs[i] = (CeedInt *)rstr_data->d_offsets;
811f815fac9SJeremy L Thompson             code << "      ReadEVecSliceStandard3d<num_comp" << var_suffix << ", " << comp_stride << ", " << Q_name << ">(data, l_size" << var_suffix
8124b3e95d5SJeremy L Thompson                  << ", elem, q, indices.inputs[" << i << "], d" << var_suffix << ", r_s" << var_suffix << ");\n";
8134b3e95d5SJeremy L Thompson           }
8149123fb08SJeremy L Thompson           CeedCallBackend(CeedElemRestrictionDestroy(&elem_rstr));
8154b3e95d5SJeremy L Thompson           break;
8164b3e95d5SJeremy L Thompson         case CEED_EVAL_INTERP:
8174b3e95d5SJeremy L Thompson           code << "      CeedScalar r_s" << var_suffix << "[num_comp" << var_suffix << "];\n";
8184b3e95d5SJeremy L Thompson           code << "      for (CeedInt j = 0; j < num_comp" << var_suffix << "; j++) {\n";
8194b3e95d5SJeremy L Thompson           code << "        r_s" << var_suffix << "[j] = r_q" << var_suffix << "[q + j*" << Q_name << "];\n";
8204b3e95d5SJeremy L Thompson           code << "      }\n";
8214b3e95d5SJeremy L Thompson           break;
8224b3e95d5SJeremy L Thompson         case CEED_EVAL_GRAD:
8234b3e95d5SJeremy L Thompson           code << "      CeedScalar r_s" << var_suffix << "[num_comp" << var_suffix << "*dim];\n";
8246b92dc4bSJeremy L Thompson           code << "      GradColloSlice3d<num_comp" << var_suffix << ", " << Q_name << ", OP_T_1D>(data, q, r_q" << var_suffix << ", s_G"
8256b92dc4bSJeremy L Thompson                << var_suffix << ", r_s" << var_suffix << ");\n";
8264b3e95d5SJeremy L Thompson           break;
8274b3e95d5SJeremy L Thompson         case CEED_EVAL_WEIGHT:
8284b3e95d5SJeremy L Thompson           code << "      CeedScalar r_s" << var_suffix << "[1];\n";
8294b3e95d5SJeremy L Thompson           code << "      r_s" << var_suffix << "[0] = r_q" << var_suffix << "[q];\n";
8303a2968d6SJeremy L Thompson           break;
8314b3e95d5SJeremy L Thompson           // LCOV_EXCL_START
8324b3e95d5SJeremy L Thompson         case CEED_EVAL_DIV:
8334b3e95d5SJeremy L Thompson         case CEED_EVAL_CURL:
8344b3e95d5SJeremy L Thompson           break;  // TODO: Not implemented
8354b3e95d5SJeremy L Thompson                   // LCOV_EXCL_STOP
8364b3e95d5SJeremy L Thompson       }
8374b3e95d5SJeremy L Thompson     }
8384b3e95d5SJeremy L Thompson     code << "\n      // -- Output fields\n";
8394b3e95d5SJeremy L Thompson     for (CeedInt i = 0; i < num_output_fields; i++) {
84059fa3f92SJeremy L Thompson       const char *field_name;
8414b3e95d5SJeremy L Thompson       std::string var_suffix = "_out_" + std::to_string(i);
8424b3e95d5SJeremy L Thompson 
84359fa3f92SJeremy L Thompson       CeedCallBackend(CeedOperatorFieldGetName(op_output_fields[i], &field_name));
84459fa3f92SJeremy L Thompson       code << "      // ---- Output field " << i << ": " << field_name << "\n";
8454b3e95d5SJeremy L Thompson       CeedCallBackend(CeedQFunctionFieldGetEvalMode(qf_output_fields[i], &eval_mode));
8464b3e95d5SJeremy L Thompson       // Basis action
8474b3e95d5SJeremy L Thompson       switch (eval_mode) {
8484b3e95d5SJeremy L Thompson         case CEED_EVAL_NONE:
8494b3e95d5SJeremy L Thompson           code << "      CeedScalar r_s" << var_suffix << "[num_comp" << var_suffix << "];\n";
8503a2968d6SJeremy L Thompson           break;
8514b3e95d5SJeremy L Thompson         case CEED_EVAL_INTERP:
8524b3e95d5SJeremy L Thompson           code << "      CeedScalar r_s" << var_suffix << "[num_comp" << var_suffix << "];\n";
8534b3e95d5SJeremy L Thompson           break;
8544b3e95d5SJeremy L Thompson         case CEED_EVAL_GRAD:
8554b3e95d5SJeremy L Thompson           code << "      CeedScalar r_s" << var_suffix << "[num_comp" << var_suffix << "*dim];\n";
8564b3e95d5SJeremy L Thompson           break;
8574b3e95d5SJeremy L Thompson           // LCOV_EXCL_START
8584b3e95d5SJeremy L Thompson         case CEED_EVAL_WEIGHT:
8594b3e95d5SJeremy L Thompson           break;  // Should not occur
8604b3e95d5SJeremy L Thompson         case CEED_EVAL_DIV:
8614b3e95d5SJeremy L Thompson         case CEED_EVAL_CURL:
8624b3e95d5SJeremy L Thompson           break;  // TODO: Not implemented
8634b3e95d5SJeremy L Thompson                   // LCOV_EXCL_STOP
8644b3e95d5SJeremy L Thompson       }
8654b3e95d5SJeremy L Thompson     }
8664b3e95d5SJeremy L Thompson   } else {
8674b3e95d5SJeremy L Thompson     code << "\n    // Note: Using full elements\n";
8684b3e95d5SJeremy L Thompson     code << "    {\n";
8694b3e95d5SJeremy L Thompson     code << "      // -- Input fields\n";
8704b3e95d5SJeremy L Thompson     for (CeedInt i = 0; i < num_input_fields; i++) {
87159fa3f92SJeremy L Thompson       const char *field_name;
87259fa3f92SJeremy L Thompson 
87359fa3f92SJeremy L Thompson       CeedCallBackend(CeedOperatorFieldGetName(op_input_fields[i], &field_name));
87459fa3f92SJeremy L Thompson       code << "      // ---- Input field " << i << ": " << field_name << "\n";
8754b3e95d5SJeremy L Thompson       code << "      CeedScalar *r_s_in_" << i << " = r_q_in_" << i << ";\n";
8764b3e95d5SJeremy L Thompson     }
8774b3e95d5SJeremy L Thompson     code << "      // -- Output fields\n";
8784b3e95d5SJeremy L Thompson     for (CeedInt i = 0; i < num_output_fields; i++) {
87959fa3f92SJeremy L Thompson       const char *field_name;
88059fa3f92SJeremy L Thompson 
88159fa3f92SJeremy L Thompson       CeedCallBackend(CeedOperatorFieldGetName(op_output_fields[i], &field_name));
88259fa3f92SJeremy L Thompson       code << "      // ---- Output field " << i << ": " << field_name << "\n";
8834b3e95d5SJeremy L Thompson       code << "      CeedScalar *r_s_out_" << i << " = r_q_out_" << i << ";\n";
8844b3e95d5SJeremy L Thompson     }
8854b3e95d5SJeremy L Thompson   }
8864b3e95d5SJeremy L Thompson 
8874b3e95d5SJeremy L Thompson   // Input and output buffers
8884b3e95d5SJeremy L Thompson   code << "\n      // -- QFunction inputs and outputs\n";
8894b3e95d5SJeremy L Thompson   code << "      // ---- Inputs\n";
8904b3e95d5SJeremy L Thompson   code << "      CeedScalar *inputs[" << CeedIntMax(num_input_fields, 1) << "];\n";
8914b3e95d5SJeremy L Thompson   for (CeedInt i = 0; i < num_input_fields; i++) {
89259fa3f92SJeremy L Thompson     const char *field_name;
89359fa3f92SJeremy L Thompson 
89459fa3f92SJeremy L Thompson     CeedCallBackend(CeedOperatorFieldGetName(op_input_fields[i], &field_name));
89559fa3f92SJeremy L Thompson     code << "      // ------ Input field " << i << ": " << field_name << "\n";
8964b3e95d5SJeremy L Thompson     code << "      inputs[" << i << "] = r_s_in_" << i << ";\n";
8974b3e95d5SJeremy L Thompson   }
8984b3e95d5SJeremy L Thompson   code << "      // ---- Outputs\n";
8994b3e95d5SJeremy L Thompson   code << "      CeedScalar *outputs[" << CeedIntMax(num_output_fields, 1) << "];\n";
9004b3e95d5SJeremy L Thompson   for (CeedInt i = 0; i < num_output_fields; i++) {
90159fa3f92SJeremy L Thompson     const char *field_name;
90259fa3f92SJeremy L Thompson 
90359fa3f92SJeremy L Thompson     CeedCallBackend(CeedOperatorFieldGetName(op_output_fields[i], &field_name));
90459fa3f92SJeremy L Thompson     code << "      // ------ Output field " << i << ": " << field_name << "\n";
9054b3e95d5SJeremy L Thompson     code << "      outputs[" << i << "] = r_s_out_" << i << ";\n";
9064b3e95d5SJeremy L Thompson   }
9074b3e95d5SJeremy L Thompson 
9084b3e95d5SJeremy L Thompson   // Apply QFunction
9094b3e95d5SJeremy L Thompson   code << "\n      // -- Apply QFunction\n";
9104b3e95d5SJeremy L Thompson   code << "      " << qfunction_name << "(ctx, ";
9119123fb08SJeremy L Thompson   if (dim != 3 || is_at_points || use_3d_slices || !is_tensor) {
9124b3e95d5SJeremy L Thompson     code << "1";
9134b3e95d5SJeremy L Thompson   } else {
9149123fb08SJeremy L Thompson     code << Q_name;
9154b3e95d5SJeremy L Thompson   }
9164b3e95d5SJeremy L Thompson   code << ", inputs, outputs);\n";
9174b3e95d5SJeremy L Thompson 
9183a2968d6SJeremy L Thompson   if (is_at_points) {
9193a2968d6SJeremy L Thompson     // Map back to coefficients
9203a2968d6SJeremy L Thompson     code << "\n      // -- Output fields\n";
9213a2968d6SJeremy L Thompson     for (CeedInt i = 0; i < num_output_fields; i++) {
92259fa3f92SJeremy L Thompson       const char *field_name;
9233a2968d6SJeremy L Thompson       std::string var_suffix = "_out_" + std::to_string(i);
9243a2968d6SJeremy L Thompson       std::string P_name     = "P_1d" + var_suffix;
9253a2968d6SJeremy L Thompson 
92659fa3f92SJeremy L Thompson       CeedCallBackend(CeedOperatorFieldGetName(op_output_fields[i], &field_name));
92759fa3f92SJeremy L Thompson       code << "      // ---- Output field " << i << ": " << field_name << "\n";
9283a2968d6SJeremy L Thompson       CeedCallBackend(CeedQFunctionFieldGetEvalMode(qf_output_fields[i], &eval_mode));
9293a2968d6SJeremy L Thompson       // Basis action
9303a2968d6SJeremy L Thompson       code << "      // EvalMode: " << CeedEvalModes[eval_mode] << "\n";
9313a2968d6SJeremy L Thompson       switch (eval_mode) {
9323a2968d6SJeremy L Thompson         case CEED_EVAL_NONE: {
9333a2968d6SJeremy L Thompson           CeedInt             comp_stride;
9343a2968d6SJeremy L Thompson           CeedElemRestriction elem_rstr;
9353a2968d6SJeremy L Thompson 
9363a2968d6SJeremy L Thompson           CeedCallBackend(CeedOperatorFieldGetElemRestriction(op_output_fields[i], &elem_rstr));
9373a2968d6SJeremy L Thompson           CeedCallBackend(CeedElemRestrictionGetCompStride(elem_rstr, &comp_stride));
9383a2968d6SJeremy L Thompson           CeedCallBackend(CeedElemRestrictionDestroy(&elem_rstr));
9393a2968d6SJeremy L Thompson           code << "      const CeedInt comp_stride" << var_suffix << " = " << comp_stride << ";\n";
9403a2968d6SJeremy L Thompson           code << "      WritePoint<num_comp" << var_suffix << ", comp_stride" << var_suffix
9413a2968d6SJeremy L Thompson                << ", max_num_points>(data, elem, i, points.num_per_elem[elem], indices.outputs[" << i << "]"
9423a2968d6SJeremy L Thompson                << ", r_s" << var_suffix << ", d" << var_suffix << ");\n";
9433a2968d6SJeremy L Thompson           break;
9443a2968d6SJeremy L Thompson         }
9453a2968d6SJeremy L Thompson         case CEED_EVAL_INTERP:
9463a2968d6SJeremy L Thompson           code << "      if (i >= points.num_per_elem[elem]) {\n";
9473a2968d6SJeremy L Thompson           code << "        for (CeedInt j = 0; j < num_comp" << var_suffix << "; j++) r_s" << var_suffix << "[j] = 0.0;\n";
9483a2968d6SJeremy L Thompson           code << "      }\n";
949f725b54bSJeremy L Thompson           code << "      InterpTransposeAtPoints" << dim << "d<num_comp" << var_suffix << ", max_num_points, " << P_name << ", " << Q_name
950f725b54bSJeremy L Thompson                << ">(data, i, r_s" << var_suffix << ", r_x, r_c" << var_suffix << ");\n";
9513a2968d6SJeremy L Thompson           break;
9523a2968d6SJeremy L Thompson         case CEED_EVAL_GRAD:
9533a2968d6SJeremy L Thompson           code << "      if (i >= points.num_per_elem[elem]) {\n";
9543a2968d6SJeremy L Thompson           code << "        for (CeedInt j = 0; j < num_comp" << var_suffix << "*dim; j++) r_s" << var_suffix << "[j] = 0.0;\n";
9553a2968d6SJeremy L Thompson           code << "      }\n";
956f725b54bSJeremy L Thompson           code << "      GradTransposeAtPoints" << dim << "d<num_comp" << var_suffix << ", max_num_points, " << P_name << ", " << Q_name
957f725b54bSJeremy L Thompson                << ">(data, i, r_s" << var_suffix << ", r_x, r_c" << var_suffix << ");\n";
9583a2968d6SJeremy L Thompson           break;
9593a2968d6SJeremy L Thompson           // LCOV_EXCL_START
9603a2968d6SJeremy L Thompson         case CEED_EVAL_WEIGHT:
9613a2968d6SJeremy L Thompson           break;  // Should not occur
9623a2968d6SJeremy L Thompson         case CEED_EVAL_DIV:
9633a2968d6SJeremy L Thompson         case CEED_EVAL_CURL:
9643a2968d6SJeremy L Thompson           break;  // TODO: Not implemented
9653a2968d6SJeremy L Thompson                   // LCOV_EXCL_STOP
9663a2968d6SJeremy L Thompson       }
9673a2968d6SJeremy L Thompson     }
9683a2968d6SJeremy L Thompson   } else if (use_3d_slices) {
9694b3e95d5SJeremy L Thompson     // Copy or apply transpose grad, if needed
9703a2968d6SJeremy L Thompson     code << "\n      // -- Output fields\n";
9714b3e95d5SJeremy L Thompson     for (CeedInt i = 0; i < num_output_fields; i++) {
97259fa3f92SJeremy L Thompson       const char *field_name;
9734b3e95d5SJeremy L Thompson       std::string var_suffix = "_out_" + std::to_string(i);
9744b3e95d5SJeremy L Thompson       std::string P_name     = "P_1d" + var_suffix;
9754b3e95d5SJeremy L Thompson 
97659fa3f92SJeremy L Thompson       CeedCallBackend(CeedOperatorFieldGetName(op_output_fields[i], &field_name));
97759fa3f92SJeremy L Thompson       code << "      // ---- Output field " << i << ": " << field_name << "\n";
9784b3e95d5SJeremy L Thompson       CeedCallBackend(CeedQFunctionFieldGetEvalMode(qf_output_fields[i], &eval_mode));
9794b3e95d5SJeremy L Thompson       // Basis action
9804b3e95d5SJeremy L Thompson       code << "      // EvalMode: " << CeedEvalModes[eval_mode] << "\n";
9814b3e95d5SJeremy L Thompson       switch (eval_mode) {
9824b3e95d5SJeremy L Thompson         case CEED_EVAL_NONE:
9834b3e95d5SJeremy L Thompson           code << "      for (CeedInt j = 0; j < num_comp" << var_suffix << " ; j++) {\n";
9844b3e95d5SJeremy L Thompson           code << "        r_q" << var_suffix << "[q + j*" << Q_name << "] = r_s" << var_suffix << "[j];\n";
9854b3e95d5SJeremy L Thompson           code << "      }\n";
9863a2968d6SJeremy L Thompson           break;
9874b3e95d5SJeremy L Thompson         case CEED_EVAL_INTERP:
9884b3e95d5SJeremy L Thompson           code << "      for (CeedInt j = 0; j < num_comp" << var_suffix << " ; j++) {\n";
9894b3e95d5SJeremy L Thompson           code << "        r_q" << var_suffix << "[q + j*" << Q_name << "] = r_s" << var_suffix << "[j];\n";
9904b3e95d5SJeremy L Thompson           code << "      }\n";
9914b3e95d5SJeremy L Thompson           break;
9924b3e95d5SJeremy L Thompson         case CEED_EVAL_GRAD:
9936b92dc4bSJeremy L Thompson           code << "      GradColloSliceTranspose3d<num_comp" << var_suffix << ", " << Q_name << ", OP_T_1D>(data, q, r_s" << var_suffix << ", s_G"
994f815fac9SJeremy L Thompson                << var_suffix << ", r_q" << var_suffix << ");\n";
9954b3e95d5SJeremy L Thompson           break;
9964b3e95d5SJeremy L Thompson           // LCOV_EXCL_START
9974b3e95d5SJeremy L Thompson         case CEED_EVAL_WEIGHT:
9984b3e95d5SJeremy L Thompson           break;  // Should not occur
9994b3e95d5SJeremy L Thompson         case CEED_EVAL_DIV:
10004b3e95d5SJeremy L Thompson         case CEED_EVAL_CURL:
10014b3e95d5SJeremy L Thompson           break;  // TODO: Not implemented
10024b3e95d5SJeremy L Thompson                   // LCOV_EXCL_STOP
10034b3e95d5SJeremy L Thompson       }
10044b3e95d5SJeremy L Thompson     }
10054b3e95d5SJeremy L Thompson   }
10064b3e95d5SJeremy L Thompson   code << "    }\n";
10074b3e95d5SJeremy L Thompson   return CEED_ERROR_SUCCESS;
10084b3e95d5SJeremy L Thompson }
10094b3e95d5SJeremy L Thompson 
10104b3e95d5SJeremy L Thompson //------------------------------------------------------------------------------
10119e201c85SYohann // Build single operator kernel
10127d8d0e25Snbeams //------------------------------------------------------------------------------
10138d12f40eSJeremy L Thompson extern "C" int CeedOperatorBuildKernel_Hip_gen(CeedOperator op, bool *is_good_build) {
10143a2968d6SJeremy L Thompson   bool                   is_tensor = true, is_at_points = false, use_3d_slices = false;
10157d8d0e25Snbeams   Ceed                   ceed;
10163a2968d6SJeremy L Thompson   CeedInt                Q_1d, num_input_fields, num_output_fields, dim = 1, max_num_points = 0, coords_comp_stride = 0;
1017b7453713SJeremy L Thompson   CeedQFunctionField    *qf_input_fields, *qf_output_fields;
1018b7453713SJeremy L Thompson   CeedQFunction_Hip_gen *qf_data;
1019b7453713SJeremy L Thompson   CeedQFunction          qf;
1020b7453713SJeremy L Thompson   CeedOperatorField     *op_input_fields, *op_output_fields;
1021b7453713SJeremy L Thompson   CeedOperator_Hip_gen  *data;
10224b3e95d5SJeremy L Thompson   std::ostringstream     code;
10234b3e95d5SJeremy L Thompson 
10248d12f40eSJeremy L Thompson   CeedCallBackend(CeedOperatorGetData(op, &data));
10254b3e95d5SJeremy L Thompson   {
10264b3e95d5SJeremy L Thompson     bool is_setup_done;
1027b7453713SJeremy L Thompson 
1028b7453713SJeremy L Thompson     CeedCallBackend(CeedOperatorIsSetupDone(op, &is_setup_done));
10298d12f40eSJeremy L Thompson     if (is_setup_done) {
10308d12f40eSJeremy L Thompson       *is_good_build = !data->use_fallback;
10318d12f40eSJeremy L Thompson       return CEED_ERROR_SUCCESS;
10328d12f40eSJeremy L Thompson     }
10334b3e95d5SJeremy L Thompson   }
1034b7453713SJeremy L Thompson 
10358d12f40eSJeremy L Thompson   // Check field compatibility
10368d12f40eSJeremy L Thompson   CeedCallBackend(CeedOperatorGetFields(op, &num_input_fields, &op_input_fields, &num_output_fields, &op_output_fields));
10378d12f40eSJeremy L Thompson   {
10388d12f40eSJeremy L Thompson     bool has_shared_bases = true, is_all_tensor = true, is_all_nontensor = true;
10398d12f40eSJeremy L Thompson 
10408d12f40eSJeremy L Thompson     for (CeedInt i = 0; i < num_input_fields; i++) {
10418d12f40eSJeremy L Thompson       CeedBasis basis;
10428d12f40eSJeremy L Thompson 
10438d12f40eSJeremy L Thompson       CeedCallBackend(CeedOperatorFieldGetBasis(op_input_fields[i], &basis));
10448d12f40eSJeremy L Thompson       if (basis != CEED_BASIS_NONE) {
10458d12f40eSJeremy L Thompson         bool        is_tensor = true;
10468d12f40eSJeremy L Thompson         const char *resource;
10478d12f40eSJeremy L Thompson         char       *resource_root;
10488d12f40eSJeremy L Thompson         Ceed        basis_ceed;
10498d12f40eSJeremy L Thompson 
10508d12f40eSJeremy L Thompson         CeedCallBackend(CeedBasisIsTensor(basis, &is_tensor));
1051c9192acaSJeremy L Thompson         is_all_tensor    = is_all_tensor && is_tensor;
1052c9192acaSJeremy L Thompson         is_all_nontensor = is_all_nontensor && !is_tensor;
10538d12f40eSJeremy L Thompson         CeedCallBackend(CeedBasisGetCeed(basis, &basis_ceed));
10548d12f40eSJeremy L Thompson         CeedCallBackend(CeedGetResource(basis_ceed, &resource));
10558d12f40eSJeremy L Thompson         CeedCallBackend(CeedGetResourceRoot(basis_ceed, resource, ":", &resource_root));
1056c9192acaSJeremy L Thompson         has_shared_bases = has_shared_bases && !strcmp(resource_root, "/gpu/hip/shared");
10578d12f40eSJeremy L Thompson         CeedCallBackend(CeedFree(&resource_root));
10588d12f40eSJeremy L Thompson         CeedCallBackend(CeedDestroy(&basis_ceed));
10598d12f40eSJeremy L Thompson       }
10608d12f40eSJeremy L Thompson       CeedCallBackend(CeedBasisDestroy(&basis));
10618d12f40eSJeremy L Thompson     }
10628d12f40eSJeremy L Thompson 
10638d12f40eSJeremy L Thompson     for (CeedInt i = 0; i < num_output_fields; i++) {
10648d12f40eSJeremy L Thompson       CeedBasis basis;
10658d12f40eSJeremy L Thompson 
10668d12f40eSJeremy L Thompson       CeedCallBackend(CeedOperatorFieldGetBasis(op_output_fields[i], &basis));
10678d12f40eSJeremy L Thompson       if (basis != CEED_BASIS_NONE) {
10688d12f40eSJeremy L Thompson         bool        is_tensor = true;
10698d12f40eSJeremy L Thompson         const char *resource;
10708d12f40eSJeremy L Thompson         char       *resource_root;
10718d12f40eSJeremy L Thompson         Ceed        basis_ceed;
10728d12f40eSJeremy L Thompson 
10738d12f40eSJeremy L Thompson         CeedCallBackend(CeedBasisIsTensor(basis, &is_tensor));
1074c9192acaSJeremy L Thompson         is_all_tensor    = is_all_tensor && is_tensor;
1075c9192acaSJeremy L Thompson         is_all_nontensor = is_all_nontensor && !is_tensor;
10768d12f40eSJeremy L Thompson 
10778d12f40eSJeremy L Thompson         CeedCallBackend(CeedBasisGetCeed(basis, &basis_ceed));
10788d12f40eSJeremy L Thompson         CeedCallBackend(CeedGetResource(basis_ceed, &resource));
10798d12f40eSJeremy L Thompson         CeedCallBackend(CeedGetResourceRoot(basis_ceed, resource, ":", &resource_root));
1080c9192acaSJeremy L Thompson         has_shared_bases = has_shared_bases && !strcmp(resource_root, "/gpu/hip/shared");
10818d12f40eSJeremy L Thompson         CeedCallBackend(CeedFree(&resource_root));
10828d12f40eSJeremy L Thompson         CeedCallBackend(CeedDestroy(&basis_ceed));
10838d12f40eSJeremy L Thompson       }
10848d12f40eSJeremy L Thompson       CeedCallBackend(CeedBasisDestroy(&basis));
10858d12f40eSJeremy L Thompson     }
10868d12f40eSJeremy L Thompson     // -- Fallback to ref if not all bases are shared
10878d12f40eSJeremy L Thompson     if (!has_shared_bases || (!is_all_tensor && !is_all_nontensor)) {
10888d12f40eSJeremy L Thompson       *is_good_build = false;
10898d12f40eSJeremy L Thompson       return CEED_ERROR_SUCCESS;
10908d12f40eSJeremy L Thompson     }
10918d12f40eSJeremy L Thompson   }
1092b7453713SJeremy L Thompson   CeedCallBackend(CeedOperatorGetCeed(op, &ceed));
1093b7453713SJeremy L Thompson   CeedCallBackend(CeedOperatorGetQFunction(op, &qf));
1094b7453713SJeremy L Thompson   CeedCallBackend(CeedQFunctionGetData(qf, &qf_data));
1095b7453713SJeremy L Thompson   CeedCallBackend(CeedQFunctionGetFields(qf, NULL, &qf_input_fields, NULL, &qf_output_fields));
10967d8d0e25Snbeams 
10974b3e95d5SJeremy L Thompson   // Get operator data
10983a2968d6SJeremy L Thompson   CeedCallBackend(CeedOperatorIsAtPoints(op, &is_at_points));
10994b3e95d5SJeremy L Thompson   CeedCallBackend(CeedOperatorBuildKernelData_Hip_gen(ceed, num_input_fields, op_input_fields, qf_input_fields, num_output_fields, op_output_fields,
11004b3e95d5SJeremy L Thompson                                                       qf_output_fields, &data->max_P_1d, &Q_1d, &dim, &is_tensor, &use_3d_slices));
11014b3e95d5SJeremy L Thompson   if (dim == 0) dim = 1;
11024b3e95d5SJeremy L Thompson   data->dim = dim;
11033a2968d6SJeremy L Thompson   if (is_at_points) {
11043a2968d6SJeremy L Thompson     CeedElemRestriction_Hip *rstr_data;
11053a2968d6SJeremy L Thompson     CeedElemRestriction      rstr_points = NULL;
11064b3e95d5SJeremy L Thompson 
11073a2968d6SJeremy L Thompson     CeedCallBackend(CeedOperatorAtPointsGetPoints(op, &rstr_points, NULL));
11083a2968d6SJeremy L Thompson     CeedCallBackend(CeedElemRestrictionGetMaxPointsInElement(rstr_points, &max_num_points));
11093a2968d6SJeremy L Thompson     CeedCallBackend(CeedElemRestrictionGetCompStride(rstr_points, &coords_comp_stride));
11103a2968d6SJeremy L Thompson     CeedCallBackend(CeedElemRestrictionGetData(rstr_points, &rstr_data));
11113a2968d6SJeremy L Thompson     data->points.indices = (CeedInt *)rstr_data->d_offsets;
11123a2968d6SJeremy L Thompson     CeedCallBackend(CeedElemRestrictionDestroy(&rstr_points));
11133a2968d6SJeremy L Thompson   }
11143a2968d6SJeremy L Thompson   if (is_at_points) use_3d_slices = false;
11153a2968d6SJeremy L Thompson   if (Q_1d == 0) {
11163a2968d6SJeremy L Thompson     if (is_at_points) Q_1d = max_num_points;
11173a2968d6SJeremy L Thompson     else CeedCallBackend(CeedOperatorGetNumQuadraturePoints(op, &Q_1d));
11184b3e95d5SJeremy L Thompson   }
11194b3e95d5SJeremy L Thompson   data->Q_1d = Q_1d;
11204b3e95d5SJeremy L Thompson 
11210b454692Sjeremylt   // Check for restriction only identity operator
11224b3e95d5SJeremy L Thompson   {
11234b3e95d5SJeremy L Thompson     bool is_identity_qf;
11244b3e95d5SJeremy L Thompson 
11252b730f8bSJeremy L Thompson     CeedCallBackend(CeedQFunctionIsIdentity(qf, &is_identity_qf));
11260b454692Sjeremylt     if (is_identity_qf) {
11279e201c85SYohann       CeedEvalMode eval_mode_in, eval_mode_out;
1128b7453713SJeremy L Thompson 
11292b730f8bSJeremy L Thompson       CeedCallBackend(CeedQFunctionFieldGetEvalMode(qf_input_fields[0], &eval_mode_in));
11302b730f8bSJeremy L Thompson       CeedCallBackend(CeedQFunctionFieldGetEvalMode(qf_output_fields[0], &eval_mode_out));
11316574a04fSJeremy L Thompson       CeedCheck(eval_mode_in != CEED_EVAL_NONE || eval_mode_out != CEED_EVAL_NONE, ceed, CEED_ERROR_BACKEND,
11326574a04fSJeremy L Thompson                 "Backend does not implement restriction only identity operators");
11330b454692Sjeremylt     }
11344b3e95d5SJeremy L Thompson   }
1135b2165e7aSSebastian Grimberg 
1136b2165e7aSSebastian Grimberg   // Load basis source files
11379123fb08SJeremy L Thompson   if (is_tensor) {
11389c25dd66SJeremy L Thompson     code << "// Tensor basis source\n";
11399c25dd66SJeremy L Thompson     code << "#include <ceed/jit-source/hip/hip-shared-basis-tensor-templates.h>\n\n";
11409123fb08SJeremy L Thompson   } else {
11419123fb08SJeremy L Thompson     code << "// Non-tensor basis source\n";
11429123fb08SJeremy L Thompson     code << "#include <ceed/jit-source/hip/hip-shared-basis-nontensor-templates.h>\n\n";
11439123fb08SJeremy L Thompson   }
11449123fb08SJeremy L Thompson   if (is_at_points) {
11453a2968d6SJeremy L Thompson     code << "// AtPoints basis source\n";
11463a2968d6SJeremy L Thompson     code << "#include <ceed/jit-source/hip/hip-shared-basis-tensor-at-points-templates.h>\n\n";
11479123fb08SJeremy L Thompson   }
11489c25dd66SJeremy L Thompson   code << "// CodeGen operator source\n";
11499c25dd66SJeremy L Thompson   code << "#include <ceed/jit-source/hip/hip-gen-templates.h>\n\n";
11507d8d0e25Snbeams 
11514b3e95d5SJeremy L Thompson   // Get QFunction name
11524b3e95d5SJeremy L Thompson   std::string qfunction_name(qf_data->qfunction_name);
11534b3e95d5SJeremy L Thompson   std::string operator_name;
11544b3e95d5SJeremy L Thompson 
115509095acaSJeremy L Thompson   operator_name = "CeedKernelHipGenOperator_" + qfunction_name;
11567d8d0e25Snbeams 
11579e201c85SYohann   // Define CEED_Q_VLA
11589e201c85SYohann   code << "\n#undef CEED_Q_VLA\n";
11599123fb08SJeremy L Thompson   if (dim != 3 || is_at_points || use_3d_slices || !is_tensor) {
11609e201c85SYohann     code << "#define CEED_Q_VLA 1\n\n";
11619e201c85SYohann   } else {
11629e201c85SYohann     code << "#define CEED_Q_VLA " << Q_1d << "\n\n";
11639e201c85SYohann   }
11649e201c85SYohann 
11654b3e95d5SJeremy L Thompson   // Add user QFunction source
11664b3e95d5SJeremy L Thompson   {
11679c25dd66SJeremy L Thompson     const char *source_path;
11684b3e95d5SJeremy L Thompson 
11699c25dd66SJeremy L Thompson     CeedCallBackend(CeedQFunctionGetSourcePath(qf, &source_path));
11709c25dd66SJeremy L Thompson     CeedCheck(source_path, ceed, CEED_ERROR_UNSUPPORTED, "/gpu/hip/gen backend requires QFunction source code file");
11719c25dd66SJeremy L Thompson 
11729c25dd66SJeremy L Thompson     code << "// User QFunction source\n";
11739c25dd66SJeremy L Thompson     code << "#include \"" << source_path << "\"\n\n";
11744b3e95d5SJeremy L Thompson   }
11757d8d0e25Snbeams 
11767d8d0e25Snbeams   // Setup
11777d8d0e25Snbeams   code << "\n// -----------------------------------------------------------------------------\n";
11784b3e95d5SJeremy L Thompson   code << "// Operator Kernel\n";
11794b3e95d5SJeremy L Thompson   code << "// \n";
11804b3e95d5SJeremy L Thompson   code << "// d_[in,out]_i:   CeedVector device array\n";
11814b3e95d5SJeremy L Thompson   code << "// r_[in,out]_e_i: Element vector register\n";
11824b3e95d5SJeremy L Thompson   code << "// r_[in,out]_q_i: Quadrature space vector register\n";
11839123fb08SJeremy L Thompson   code << "// r_[in,out]_c_i: AtPoints Chebyshev coefficients register\n";
11844b3e95d5SJeremy L Thompson   code << "// r_[in,out]_s_i: Quadrature space slice vector register\n";
11854b3e95d5SJeremy L Thompson   code << "// \n";
11864b3e95d5SJeremy L Thompson   code << "// s_B_[in,out]_i: Interpolation matrix, shared memory\n";
11874b3e95d5SJeremy L Thompson   code << "// s_G_[in,out]_i: Gradient matrix, shared memory\n";
11884b3e95d5SJeremy L Thompson   code << "// -----------------------------------------------------------------------------\n";
1189b3e1519bSnbeams   code << "\nextern \"C\" __launch_bounds__(BLOCK_SIZE)\n";
11902b730f8bSJeremy L Thompson   code << "__global__ void " << operator_name
11913a2968d6SJeremy L Thompson        << "(CeedInt num_elem, void* ctx, FieldsInt_Hip indices, Fields_Hip fields, Fields_Hip B, Fields_Hip G, CeedScalar* W, Points_Hip points) {\n";
11924b3e95d5SJeremy L Thompson 
11934b3e95d5SJeremy L Thompson   // Scratch buffers
11949e201c85SYohann   for (CeedInt i = 0; i < num_input_fields; i++) {
11954b3e95d5SJeremy L Thompson     CeedEvalMode eval_mode;
11964b3e95d5SJeremy L Thompson 
11972b730f8bSJeremy L Thompson     CeedCallBackend(CeedQFunctionFieldGetEvalMode(qf_input_fields[i], &eval_mode));
11989e201c85SYohann     if (eval_mode != CEED_EVAL_WEIGHT) {  // Skip CEED_EVAL_WEIGHT
1199826538b3SJeremy L Thompson       code << "  const CeedScalar *__restrict__ d_in_" << i << " = fields.inputs[" << i << "];\n";
12007d8d0e25Snbeams     }
12017d8d0e25Snbeams   }
12029e201c85SYohann   for (CeedInt i = 0; i < num_output_fields; i++) {
1203826538b3SJeremy L Thompson     code << "  CeedScalar *__restrict__ d_out_" << i << " = fields.outputs[" << i << "];\n";
12047d8d0e25Snbeams   }
12057d8d0e25Snbeams 
12069e201c85SYohann   code << "  const CeedInt dim = " << dim << ";\n";
12079123fb08SJeremy L Thompson   code << "  const CeedInt " << (is_tensor ? "Q_1d" : "Q") << " = " << Q_1d << ";\n";
12083a2968d6SJeremy L Thompson   if (is_at_points) {
12093a2968d6SJeremy L Thompson     code << "  const CeedInt max_num_points = " << max_num_points << ";\n";
12103a2968d6SJeremy L Thompson     code << "  const CeedInt coords_comp_stride = " << coords_comp_stride << ";\n";
12113a2968d6SJeremy L Thompson   }
12127d8d0e25Snbeams 
12134b3e95d5SJeremy L Thompson   // Shared data
12144b3e95d5SJeremy L Thompson   code << "  extern __shared__ CeedScalar slice[];\n";
12159e201c85SYohann   code << "  SharedData_Hip data;\n";
12169e201c85SYohann   code << "  data.t_id_x = threadIdx.x;\n";
12179e201c85SYohann   code << "  data.t_id_y = threadIdx.y;\n";
12189e201c85SYohann   code << "  data.t_id_z = threadIdx.z;\n";
12199e201c85SYohann   code << "  data.t_id  = threadIdx.x + threadIdx.y*blockDim.x + threadIdx.z*blockDim.y*blockDim.x;\n";
12206b92dc4bSJeremy L Thompson   code << "  data.slice = slice + data.t_id_z*OP_T_1D" << ((!is_tensor || dim == 1) ? "" : "*OP_T_1D") << ";\n";
12217d8d0e25Snbeams 
12229ee499e5SJeremy L Thompson   // -- Determine input mat reuse
122345a787f7SJeremy L Thompson   FieldReuse_Hip input_matrix_reuse[CEED_FIELD_MAX];
12249ee499e5SJeremy L Thompson 
12259ee499e5SJeremy L Thompson   for (CeedInt i = 0; i < num_input_fields; i++) {
122645a787f7SJeremy L Thompson     input_matrix_reuse[i].index = -1;
12279ee499e5SJeremy L Thompson   }
12289ee499e5SJeremy L Thompson   for (CeedInt i = 0; i < num_input_fields; i++) {
12299ee499e5SJeremy L Thompson     CeedEvalMode eval_mode_i;
12309ee499e5SJeremy L Thompson     CeedBasis    basis_i;
12319ee499e5SJeremy L Thompson 
12329ee499e5SJeremy L Thompson     CeedCallBackend(CeedQFunctionFieldGetEvalMode(qf_input_fields[i], &eval_mode_i));
12339ee499e5SJeremy L Thompson     if (eval_mode_i == CEED_EVAL_WEIGHT) continue;
12349ee499e5SJeremy L Thompson     CeedCallBackend(CeedOperatorFieldGetBasis(op_input_fields[i], &basis_i));
123545a787f7SJeremy L Thompson     for (CeedInt j = 0; (input_matrix_reuse[i].index == -1) && (j < i); j++) {
12369ee499e5SJeremy L Thompson       CeedEvalMode eval_mode_j;
12379ee499e5SJeremy L Thompson       CeedBasis    basis_j;
12389ee499e5SJeremy L Thompson 
12399ee499e5SJeremy L Thompson       CeedCallBackend(CeedQFunctionFieldGetEvalMode(qf_input_fields[j], &eval_mode_j));
12409ee499e5SJeremy L Thompson       if (eval_mode_j == CEED_EVAL_WEIGHT) continue;
12419ee499e5SJeremy L Thompson       CeedCallBackend(CeedOperatorFieldGetBasis(op_input_fields[j], &basis_j));
12429ee499e5SJeremy L Thompson       if (basis_i == basis_j) {
12439ee499e5SJeremy L Thompson         if (is_tensor) {
124445a787f7SJeremy L Thompson           input_matrix_reuse[i].index     = j;
124545a787f7SJeremy L Thompson           input_matrix_reuse[i].is_input  = true;
124645a787f7SJeremy L Thompson           input_matrix_reuse[i].eval_mode = eval_mode_j;
12479ee499e5SJeremy L Thompson         } else {
12489ee499e5SJeremy L Thompson           // For non-tensor can only re-use with the same eval mode
12499ee499e5SJeremy L Thompson           if (eval_mode_i == eval_mode_j) {
125045a787f7SJeremy L Thompson             input_matrix_reuse[i].index     = j;
125145a787f7SJeremy L Thompson             input_matrix_reuse[i].is_input  = true;
125245a787f7SJeremy L Thompson             input_matrix_reuse[i].eval_mode = eval_mode_j;
12539ee499e5SJeremy L Thompson           }
12549ee499e5SJeremy L Thompson         }
12559ee499e5SJeremy L Thompson       }
12569ee499e5SJeremy L Thompson       CeedCallBackend(CeedBasisDestroy(&basis_j));
12579ee499e5SJeremy L Thompson     }
12589ee499e5SJeremy L Thompson     CeedCallBackend(CeedBasisDestroy(&basis_i));
12599ee499e5SJeremy L Thompson   }
12609ee499e5SJeremy L Thompson 
12619ee499e5SJeremy L Thompson   // -- Determine output mat reuse
126245a787f7SJeremy L Thompson   FieldReuse_Hip output_matrix_reuse[CEED_FIELD_MAX];
12639ee499e5SJeremy L Thompson 
12649ee499e5SJeremy L Thompson   for (CeedInt i = 0; i < num_output_fields; i++) {
126545a787f7SJeremy L Thompson     output_matrix_reuse[i].index = -1;
12669ee499e5SJeremy L Thompson   }
12679ee499e5SJeremy L Thompson   for (CeedInt i = 0; i < num_output_fields; i++) {
12689ee499e5SJeremy L Thompson     CeedEvalMode eval_mode_i;
12699ee499e5SJeremy L Thompson     CeedBasis    basis_i;
12709ee499e5SJeremy L Thompson 
12719ee499e5SJeremy L Thompson     CeedCallBackend(CeedQFunctionFieldGetEvalMode(qf_output_fields[i], &eval_mode_i));
12729ee499e5SJeremy L Thompson     CeedCallBackend(CeedOperatorFieldGetBasis(op_output_fields[i], &basis_i));
127345a787f7SJeremy L Thompson     for (CeedInt j = 0; (output_matrix_reuse[i].index == -1) && (j < num_input_fields); j++) {
12749ee499e5SJeremy L Thompson       CeedEvalMode eval_mode_j;
12759ee499e5SJeremy L Thompson       CeedBasis    basis_j;
12769ee499e5SJeremy L Thompson 
12779ee499e5SJeremy L Thompson       CeedCallBackend(CeedQFunctionFieldGetEvalMode(qf_input_fields[j], &eval_mode_j));
12789ee499e5SJeremy L Thompson       if (eval_mode_j == CEED_EVAL_WEIGHT) continue;
12799ee499e5SJeremy L Thompson       CeedCallBackend(CeedOperatorFieldGetBasis(op_input_fields[j], &basis_j));
12809ee499e5SJeremy L Thompson       if (basis_i == basis_j) {
12819ee499e5SJeremy L Thompson         if (is_tensor) {
128245a787f7SJeremy L Thompson           output_matrix_reuse[i].index     = j;
128345a787f7SJeremy L Thompson           output_matrix_reuse[i].is_input  = true;
128445a787f7SJeremy L Thompson           output_matrix_reuse[i].eval_mode = eval_mode_j;
12859ee499e5SJeremy L Thompson         } else {
12869ee499e5SJeremy L Thompson           // For non-tensor can only re-use with the same eval mode
12879ee499e5SJeremy L Thompson           if (eval_mode_i == eval_mode_j) {
128845a787f7SJeremy L Thompson             output_matrix_reuse[i].index     = j;
128945a787f7SJeremy L Thompson             output_matrix_reuse[i].is_input  = true;
129045a787f7SJeremy L Thompson             output_matrix_reuse[i].eval_mode = eval_mode_j;
12919ee499e5SJeremy L Thompson           }
12929ee499e5SJeremy L Thompson         }
12939ee499e5SJeremy L Thompson       }
12949ee499e5SJeremy L Thompson       CeedCallBackend(CeedBasisDestroy(&basis_j));
12959ee499e5SJeremy L Thompson     }
129645a787f7SJeremy L Thompson     for (CeedInt j = 0; (output_matrix_reuse[i].index == -1) && (j < i); j++) {
12979ee499e5SJeremy L Thompson       CeedEvalMode eval_mode_j;
12989ee499e5SJeremy L Thompson       CeedBasis    basis_j;
12999ee499e5SJeremy L Thompson 
13009ee499e5SJeremy L Thompson       CeedCallBackend(CeedQFunctionFieldGetEvalMode(qf_output_fields[j], &eval_mode_j));
13019ee499e5SJeremy L Thompson       if (eval_mode_j == CEED_EVAL_WEIGHT) continue;
13029ee499e5SJeremy L Thompson       CeedCallBackend(CeedOperatorFieldGetBasis(op_output_fields[j], &basis_j));
13039ee499e5SJeremy L Thompson       if (basis_i == basis_j) {
13049ee499e5SJeremy L Thompson         if (is_tensor) {
130545a787f7SJeremy L Thompson           output_matrix_reuse[i].index     = j;
130645a787f7SJeremy L Thompson           output_matrix_reuse[i].is_input  = false;
130745a787f7SJeremy L Thompson           output_matrix_reuse[i].eval_mode = eval_mode_j;
13089ee499e5SJeremy L Thompson         } else {
13099ee499e5SJeremy L Thompson           // For non-tensor can only re-use with the same eval mode
13109ee499e5SJeremy L Thompson           if (eval_mode_i == eval_mode_j) {
131145a787f7SJeremy L Thompson             output_matrix_reuse[i].index     = j;
131245a787f7SJeremy L Thompson             output_matrix_reuse[i].is_input  = false;
131345a787f7SJeremy L Thompson             output_matrix_reuse[i].eval_mode = eval_mode_j;
13149ee499e5SJeremy L Thompson           }
13159ee499e5SJeremy L Thompson         }
13169ee499e5SJeremy L Thompson       }
13179ee499e5SJeremy L Thompson       CeedCallBackend(CeedBasisDestroy(&basis_j));
13189ee499e5SJeremy L Thompson     }
13199ee499e5SJeremy L Thompson     CeedCallBackend(CeedBasisDestroy(&basis_i));
13209ee499e5SJeremy L Thompson   }
13219ee499e5SJeremy L Thompson 
13227d8d0e25Snbeams   // Initialize constants, and matrices B and G
13234b3e95d5SJeremy L Thompson   code << "\n  // Input field constants and basis data\n";
13249e201c85SYohann   for (CeedInt i = 0; i < num_input_fields; i++) {
13259ee499e5SJeremy L Thompson     CeedCallBackend(CeedOperatorBuildKernelFieldData_Hip_gen(code, data, i, op_input_fields[i], qf_input_fields[i], input_matrix_reuse[i], Q_1d, true,
13269ee499e5SJeremy L Thompson                                                              is_tensor, is_at_points, use_3d_slices));
13277d8d0e25Snbeams   }
13284b3e95d5SJeremy L Thompson   code << "\n  // Output field constants and basis data\n";
13299e201c85SYohann   for (CeedInt i = 0; i < num_output_fields; i++) {
13309ee499e5SJeremy L Thompson     CeedCallBackend(CeedOperatorBuildKernelFieldData_Hip_gen(code, data, i, op_output_fields[i], qf_output_fields[i], output_matrix_reuse[i], Q_1d,
13319ee499e5SJeremy L Thompson                                                              false, is_tensor, is_at_points, use_3d_slices));
13324b3e95d5SJeremy L Thompson   }
13337d8d0e25Snbeams 
13344b3e95d5SJeremy L Thompson   // Loop over all elements
13354b3e95d5SJeremy L Thompson   code << "\n  // Element loop\n";
13367d8d0e25Snbeams   code << "  __syncthreads();\n";
13379e201c85SYohann   code << "  for (CeedInt elem = blockIdx.x*blockDim.z + threadIdx.z; elem < num_elem; elem += gridDim.x*blockDim.z) {\n";
13384b3e95d5SJeremy L Thompson 
1339e93651e5SJeremy L Thompson   // -- Compute minimum buffer space needed
13403a2968d6SJeremy L Thompson   CeedInt max_rstr_buffer_size = 1;
1341e93651e5SJeremy L Thompson 
1342e93651e5SJeremy L Thompson   for (CeedInt i = 0; i < num_input_fields; i++) {
1343e93651e5SJeremy L Thompson     CeedInt             num_comp, elem_size;
1344e93651e5SJeremy L Thompson     CeedElemRestriction elem_rstr;
1345e93651e5SJeremy L Thompson 
1346e93651e5SJeremy L Thompson     CeedCallBackend(CeedOperatorFieldGetElemRestriction(op_input_fields[i], &elem_rstr));
1347e93651e5SJeremy L Thompson     CeedCallBackend(CeedElemRestrictionGetNumComponents(elem_rstr, &num_comp));
1348e93651e5SJeremy L Thompson     CeedCallBackend(CeedElemRestrictionGetElementSize(elem_rstr, &elem_size));
13499123fb08SJeremy L Thompson     max_rstr_buffer_size = CeedIntMax(max_rstr_buffer_size, num_comp * (is_tensor && (dim >= 3) ? elem_size : 1));
1350681d0ea7SJeremy L Thompson     CeedCallBackend(CeedElemRestrictionDestroy(&elem_rstr));
1351e93651e5SJeremy L Thompson   }
1352e93651e5SJeremy L Thompson   for (CeedInt i = 0; i < num_output_fields; i++) {
1353e93651e5SJeremy L Thompson     CeedInt             num_comp, elem_size;
1354e93651e5SJeremy L Thompson     CeedElemRestriction elem_rstr;
1355e93651e5SJeremy L Thompson 
1356e93651e5SJeremy L Thompson     CeedCallBackend(CeedOperatorFieldGetElemRestriction(op_output_fields[i], &elem_rstr));
1357e93651e5SJeremy L Thompson     CeedCallBackend(CeedElemRestrictionGetNumComponents(elem_rstr, &num_comp));
1358e93651e5SJeremy L Thompson     CeedCallBackend(CeedElemRestrictionGetElementSize(elem_rstr, &elem_size));
13599123fb08SJeremy L Thompson     max_rstr_buffer_size = CeedIntMax(max_rstr_buffer_size, num_comp * (is_tensor && (dim >= 3) ? elem_size : 1));
1360681d0ea7SJeremy L Thompson     CeedCallBackend(CeedElemRestrictionDestroy(&elem_rstr));
1361e93651e5SJeremy L Thompson   }
1362e93651e5SJeremy L Thompson   code << "    // Scratch restriction buffer space\n";
1363e93651e5SJeremy L Thompson   code << "    CeedScalar r_e_scratch[" << max_rstr_buffer_size << "];\n";
1364e93651e5SJeremy L Thompson 
1365e93651e5SJeremy L Thompson   // -- Determine best input field processing order
1366e93651e5SJeremy L Thompson   CeedInt field_rstr_in_buffer[CEED_FIELD_MAX], input_field_order[CEED_FIELD_MAX];
1367e93651e5SJeremy L Thompson 
1368e93651e5SJeremy L Thompson   for (CeedInt i = 0; i < num_input_fields; i++) {
1369e93651e5SJeremy L Thompson     field_rstr_in_buffer[i] = -1;
1370e93651e5SJeremy L Thompson     input_field_order[i]    = -1;
1371e93651e5SJeremy L Thompson   }
1372e93651e5SJeremy L Thompson   {
1373e93651e5SJeremy L Thompson     bool    is_ordered[CEED_FIELD_MAX];
1374e93651e5SJeremy L Thompson     CeedInt curr_index = 0;
1375e93651e5SJeremy L Thompson 
1376e93651e5SJeremy L Thompson     for (CeedInt i = 0; i < num_input_fields; i++) is_ordered[i] = false;
1377e93651e5SJeremy L Thompson     for (CeedInt i = 0; i < num_input_fields; i++) {
1378e93651e5SJeremy L Thompson       CeedVector          vec_i;
1379e93651e5SJeremy L Thompson       CeedElemRestriction rstr_i;
1380e93651e5SJeremy L Thompson 
1381e93651e5SJeremy L Thompson       if (is_ordered[i]) continue;
1382e93651e5SJeremy L Thompson       field_rstr_in_buffer[i]       = i;
1383e93651e5SJeremy L Thompson       is_ordered[i]                 = true;
1384e93651e5SJeremy L Thompson       input_field_order[curr_index] = i;
1385e93651e5SJeremy L Thompson       curr_index++;
1386034f99fdSJeremy L Thompson       CeedCallBackend(CeedOperatorFieldGetVector(op_input_fields[i], &vec_i));
1387e93651e5SJeremy L Thompson       if (vec_i == CEED_VECTOR_NONE) continue;  // CEED_EVAL_WEIGHT
1388e93651e5SJeremy L Thompson       CeedCallBackend(CeedOperatorFieldGetElemRestriction(op_input_fields[i], &rstr_i));
1389e93651e5SJeremy L Thompson       for (CeedInt j = i + 1; j < num_input_fields; j++) {
1390e93651e5SJeremy L Thompson         CeedVector          vec_j;
1391e93651e5SJeremy L Thompson         CeedElemRestriction rstr_j;
1392e93651e5SJeremy L Thompson 
1393e93651e5SJeremy L Thompson         CeedCallBackend(CeedOperatorFieldGetVector(op_input_fields[j], &vec_j));
1394e93651e5SJeremy L Thompson         CeedCallBackend(CeedOperatorFieldGetElemRestriction(op_input_fields[j], &rstr_j));
1395e93651e5SJeremy L Thompson         if (rstr_i == rstr_j && vec_i == vec_j) {
1396e93651e5SJeremy L Thompson           field_rstr_in_buffer[j]       = i;
1397e93651e5SJeremy L Thompson           is_ordered[j]                 = true;
1398e93651e5SJeremy L Thompson           input_field_order[curr_index] = j;
1399e93651e5SJeremy L Thompson           curr_index++;
1400e93651e5SJeremy L Thompson         }
14013a2968d6SJeremy L Thompson         CeedCallBackend(CeedVectorDestroy(&vec_j));
14023a2968d6SJeremy L Thompson         CeedCallBackend(CeedElemRestrictionDestroy(&rstr_j));
1403e93651e5SJeremy L Thompson       }
14043a2968d6SJeremy L Thompson       CeedCallBackend(CeedVectorDestroy(&vec_i));
14053a2968d6SJeremy L Thompson       CeedCallBackend(CeedElemRestrictionDestroy(&rstr_i));
1406e93651e5SJeremy L Thompson     }
1407e93651e5SJeremy L Thompson   }
1408e93651e5SJeremy L Thompson 
14094b3e95d5SJeremy L Thompson   // -- Input restriction and basis
14103a2968d6SJeremy L Thompson   code << "\n    // -- Input field restrictions and basis actions\n";
14119e201c85SYohann   for (CeedInt i = 0; i < num_input_fields; i++) {
141259fa3f92SJeremy L Thompson     const char   *field_name;
141359fa3f92SJeremy L Thompson     const CeedInt f = input_field_order[i];
1414e93651e5SJeremy L Thompson 
141559fa3f92SJeremy L Thompson     CeedCallBackend(CeedOperatorFieldGetName(op_input_fields[f], &field_name));
141659fa3f92SJeremy L Thompson     code << "    // ---- Input field " << f << ": " << field_name << "\n";
14177d8d0e25Snbeams 
14184b3e95d5SJeremy L Thompson     // ---- Restriction
1419e93651e5SJeremy L Thompson     CeedCallBackend(CeedOperatorBuildKernelRestriction_Hip_gen(code, data, f, dim, field_rstr_in_buffer, op_input_fields[f], qf_input_fields[f], Q_1d,
14209123fb08SJeremy L Thompson                                                                true, is_tensor, is_at_points, use_3d_slices));
1421b7453713SJeremy L Thompson 
14224b3e95d5SJeremy L Thompson     // ---- Basis action
14239123fb08SJeremy L Thompson     CeedCallBackend(CeedOperatorBuildKernelBasis_Hip_gen(code, data, f, dim, op_input_fields[f], qf_input_fields[f], Q_1d, true, is_tensor,
14249123fb08SJeremy L Thompson                                                          is_at_points, use_3d_slices));
14257d8d0e25Snbeams   }
14267d8d0e25Snbeams 
14274b3e95d5SJeremy L Thompson   // -- Q function
14283a2968d6SJeremy L Thompson   CeedCallBackend(CeedOperatorBuildKernelQFunction_Hip_gen(code, data, dim, max_num_points, num_input_fields, op_input_fields, qf_input_fields,
14299123fb08SJeremy L Thompson                                                            num_output_fields, op_output_fields, qf_output_fields, qfunction_name, Q_1d, is_tensor,
14309123fb08SJeremy L Thompson                                                            is_at_points, use_3d_slices));
14317d8d0e25Snbeams 
14324b3e95d5SJeremy L Thompson   // -- Output basis and restriction
14334b3e95d5SJeremy L Thompson   code << "\n    // -- Output field basis action and restrictions\n";
14349e201c85SYohann   for (CeedInt i = 0; i < num_output_fields; i++) {
143559fa3f92SJeremy L Thompson     const char *field_name;
143659fa3f92SJeremy L Thompson 
143759fa3f92SJeremy L Thompson     CeedCallBackend(CeedOperatorFieldGetName(op_output_fields[i], &field_name));
143859fa3f92SJeremy L Thompson     code << "    // ---- Output field " << i << ": " << field_name << "\n";
1439b7453713SJeremy L Thompson 
14404b3e95d5SJeremy L Thompson     // ---- Basis action
14419123fb08SJeremy L Thompson     CeedCallBackend(CeedOperatorBuildKernelBasis_Hip_gen(code, data, i, dim, op_output_fields[i], qf_output_fields[i], Q_1d, false, is_tensor,
14429123fb08SJeremy L Thompson                                                          is_at_points, use_3d_slices));
14437d8d0e25Snbeams 
14444b3e95d5SJeremy L Thompson     // ---- Restriction
14453a2968d6SJeremy L Thompson     CeedCallBackend(CeedOperatorBuildKernelRestriction_Hip_gen(code, data, i, dim, NULL, op_output_fields[i], qf_output_fields[i], Q_1d, false,
14469123fb08SJeremy L Thompson                                                                is_tensor, is_at_points, use_3d_slices));
14477d8d0e25Snbeams   }
14487d8d0e25Snbeams 
14494b3e95d5SJeremy L Thompson   // Close loop and function
14507d8d0e25Snbeams   code << "  }\n";
14517d8d0e25Snbeams   code << "}\n";
14527d8d0e25Snbeams   code << "// -----------------------------------------------------------------------------\n\n";
14537d8d0e25Snbeams 
1454539ec17dSJeremy L Thompson   CeedInt block_sizes[3] = {0, 0, 0};
14559e201c85SYohann   CeedInt num_elem;
1456b7453713SJeremy L Thompson 
14573a2968d6SJeremy L Thompson   // Compile
14582b730f8bSJeremy L Thompson   CeedCallBackend(CeedOperatorGetNumElements(op, &num_elem));
14599123fb08SJeremy L Thompson   CeedCallBackend(BlockGridCalculate_Hip_gen(is_tensor ? dim : 1, num_elem, data->max_P_1d, Q_1d, block_sizes));
146090c30374SJeremy L Thompson   if (is_at_points) block_sizes[2] = 1;
14618d12f40eSJeremy L Thompson   {
14628d12f40eSJeremy L Thompson     bool is_compile_good = false;
14638d12f40eSJeremy L Thompson 
14646b92dc4bSJeremy L Thompson     CeedCallBackend(CeedTryCompile_Hip(ceed, code.str().c_str(), &is_compile_good, &data->module, 2, "OP_T_1D", block_sizes[0], "BLOCK_SIZE",
14652b730f8bSJeremy L Thompson                                        block_sizes[0] * block_sizes[1] * block_sizes[2]));
14668d12f40eSJeremy L Thompson     if (is_compile_good) {
14678d12f40eSJeremy L Thompson       *is_good_build = true;
1468eb7e6cafSJeremy L Thompson       CeedCallBackend(CeedGetKernel_Hip(ceed, data->module, operator_name.c_str(), &data->op));
14698d12f40eSJeremy L Thompson     } else {
14708d12f40eSJeremy L Thompson       *is_good_build     = false;
14718d12f40eSJeremy L Thompson       data->use_fallback = true;
14728d12f40eSJeremy L Thompson     }
14738d12f40eSJeremy L Thompson   }
14742b730f8bSJeremy L Thompson   CeedCallBackend(CeedOperatorSetSetupDone(op));
14759bc66399SJeremy L Thompson   CeedCallBackend(CeedDestroy(&ceed));
1476c11e12f4SJeremy L Thompson   CeedCallBackend(CeedQFunctionDestroy(&qf));
1477e15f9bd0SJeremy L Thompson   return CEED_ERROR_SUCCESS;
14787d8d0e25Snbeams }
14792a86cc9dSSebastian Grimberg 
14807d8d0e25Snbeams //------------------------------------------------------------------------------
1481