xref: /libCEED/backends/hip-gen/ceed-hip-gen-operator-build.cpp (revision 0ccda8ebe42db3fb90cdb724a58e4e5d2aedf1a1)
1d275d636SJeremy L Thompson // Copyright (c) 2017-2025, Lawrence Livermore National Security, LLC and other CEED contributors.
23d8e8822SJeremy L Thompson // All Rights Reserved. See the top-level LICENSE and NOTICE files for details.
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>
120183ed61SJeremy L Thompson #include <ceed/gen-tools.h>
139e201c85SYohann #include <ceed/jit-tools.h>
142b730f8bSJeremy L Thompson 
157d8d0e25Snbeams #include <iostream>
167d8d0e25Snbeams #include <sstream>
172b730f8bSJeremy L Thompson #include <string>
182b730f8bSJeremy L Thompson 
190d0321e0SJeremy L Thompson #include "../hip-ref/ceed-hip-ref.h"
207d8d0e25Snbeams #include "../hip-shared/ceed-hip-shared.h"
21b2165e7aSSebastian Grimberg #include "../hip/ceed-hip-common.h"
227d8d0e25Snbeams #include "../hip/ceed-hip-compile.h"
232b730f8bSJeremy L Thompson #include "ceed-hip-gen.h"
24b3e1519bSnbeams 
2545a787f7SJeremy L Thompson struct FieldReuse_Hip {
2645a787f7SJeremy L Thompson   CeedInt      index;
2745a787f7SJeremy L Thompson   bool         is_input;
2845a787f7SJeremy L Thompson   CeedEvalMode eval_mode;
2945a787f7SJeremy L Thompson };
3045a787f7SJeremy L Thompson 
31b3e1519bSnbeams //------------------------------------------------------------------------------
32b3e1519bSnbeams // Calculate the block size used for launching the operator kernel
33b3e1519bSnbeams //------------------------------------------------------------------------------
342b730f8bSJeremy 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) {
353a2968d6SJeremy L Thompson   const CeedInt thread_1d = CeedIntMax(Q_1d, P_1d);
36b3e1519bSnbeams   if (dim == 1) {
373a2968d6SJeremy L Thompson     CeedInt elems_per_block = 64 * thread_1d > 256 ? 256 / thread_1d : 64;
38b7453713SJeremy L Thompson 
399e201c85SYohann     elems_per_block = elems_per_block > 0 ? elems_per_block : 1;
403a2968d6SJeremy L Thompson     block_sizes[0]  = thread_1d;
41b3e1519bSnbeams     block_sizes[1]  = 1;
429e201c85SYohann     block_sizes[2]  = elems_per_block;
43b3e1519bSnbeams   } else if (dim == 2) {
443a2968d6SJeremy L Thompson     const CeedInt elems_per_block = thread_1d < 4 ? 16 : 2;
45b7453713SJeremy L Thompson 
463a2968d6SJeremy L Thompson     block_sizes[0] = thread_1d;
473a2968d6SJeremy L Thompson     block_sizes[1] = thread_1d;
489e201c85SYohann     block_sizes[2] = elems_per_block;
49b3e1519bSnbeams   } else if (dim == 3) {
503a2968d6SJeremy L Thompson     const CeedInt elems_per_block = thread_1d < 6 ? 4 : (thread_1d < 8 ? 2 : 1);
51b7453713SJeremy L Thompson 
523a2968d6SJeremy L Thompson     block_sizes[0] = thread_1d;
533a2968d6SJeremy L Thompson     block_sizes[1] = thread_1d;
549e201c85SYohann     block_sizes[2] = elems_per_block;
55b3e1519bSnbeams   }
56b3e1519bSnbeams   return CEED_ERROR_SUCCESS;
57b3e1519bSnbeams }
58b3e1519bSnbeams 
597d8d0e25Snbeams //------------------------------------------------------------------------------
604b3e95d5SJeremy L Thompson // Determine type of operator
614b3e95d5SJeremy L Thompson //------------------------------------------------------------------------------
624b3e95d5SJeremy L Thompson static int CeedOperatorBuildKernelData_Hip_gen(Ceed ceed, CeedInt num_input_fields, CeedOperatorField *op_input_fields,
634b3e95d5SJeremy L Thompson                                                CeedQFunctionField *qf_input_fields, CeedInt num_output_fields, CeedOperatorField *op_output_fields,
6474398b5aSJeremy L Thompson                                                CeedQFunctionField *qf_output_fields, CeedInt *max_P, CeedInt *max_P_1d, CeedInt *Q, CeedInt *Q_1d,
6574398b5aSJeremy L Thompson                                                CeedInt *max_dim, bool *is_all_tensor, bool *use_3d_slices) {
6674398b5aSJeremy L Thompson   // Check if all are tensor
6774398b5aSJeremy L Thompson   *is_all_tensor = true;
684b3e95d5SJeremy L Thompson   for (CeedInt i = 0; i < num_input_fields; i++) {
694b3e95d5SJeremy L Thompson     CeedBasis basis;
704b3e95d5SJeremy L Thompson 
714b3e95d5SJeremy L Thompson     CeedCallBackend(CeedOperatorFieldGetBasis(op_input_fields[i], &basis));
724b3e95d5SJeremy L Thompson     if (basis != CEED_BASIS_NONE) {
734b3e95d5SJeremy L Thompson       bool is_field_tensor;
744b3e95d5SJeremy L Thompson 
754b3e95d5SJeremy L Thompson       CeedCallBackend(CeedBasisIsTensor(basis, &is_field_tensor));
7674398b5aSJeremy L Thompson       *is_all_tensor = *is_all_tensor && is_field_tensor;
774b3e95d5SJeremy L Thompson     }
783a2968d6SJeremy L Thompson     CeedCallBackend(CeedBasisDestroy(&basis));
794b3e95d5SJeremy L Thompson   }
804b3e95d5SJeremy L Thompson   for (CeedInt i = 0; i < num_output_fields; i++) {
814b3e95d5SJeremy L Thompson     CeedBasis basis;
824b3e95d5SJeremy L Thompson 
834b3e95d5SJeremy L Thompson     CeedCallBackend(CeedOperatorFieldGetBasis(op_output_fields[i], &basis));
844b3e95d5SJeremy L Thompson     if (basis != CEED_BASIS_NONE) {
854b3e95d5SJeremy L Thompson       bool is_field_tensor;
864b3e95d5SJeremy L Thompson 
874b3e95d5SJeremy L Thompson       CeedCallBackend(CeedBasisIsTensor(basis, &is_field_tensor));
8874398b5aSJeremy L Thompson       *is_all_tensor = *is_all_tensor && is_field_tensor;
8974398b5aSJeremy L Thompson     }
9074398b5aSJeremy L Thompson     CeedCallBackend(CeedBasisDestroy(&basis));
9174398b5aSJeremy L Thompson   }
9274398b5aSJeremy L Thompson 
9374398b5aSJeremy L Thompson   // Find max_P, max_P_1d, Q, and Q_1d
9474398b5aSJeremy L Thompson   bool is_all_3d = true;
9574398b5aSJeremy L Thompson 
9674398b5aSJeremy L Thompson   *max_P    = 0;
9774398b5aSJeremy L Thompson   *max_P_1d = 0;
9874398b5aSJeremy L Thompson   *Q        = 0;
9974398b5aSJeremy L Thompson   *Q_1d     = 0;
10074398b5aSJeremy L Thompson   for (CeedInt i = 0; i < num_input_fields; i++) {
10174398b5aSJeremy L Thompson     CeedBasis basis;
10274398b5aSJeremy L Thompson 
10374398b5aSJeremy L Thompson     CeedCallBackend(CeedOperatorFieldGetBasis(op_input_fields[i], &basis));
10474398b5aSJeremy L Thompson     if (basis != CEED_BASIS_NONE) {
10574398b5aSJeremy L Thompson       bool    is_field_tensor;
10674398b5aSJeremy L Thompson       CeedInt field_dim = 0, field_P = 0, field_P_1d = 0, field_Q = 0, field_Q_1d = 0;
10774398b5aSJeremy L Thompson 
10874398b5aSJeremy L Thompson       // Check if 3D
1094b3e95d5SJeremy L Thompson       CeedCallBackend(CeedBasisGetDimension(basis, &field_dim));
11074398b5aSJeremy L Thompson       is_all_3d = is_all_3d && (field_dim == 3);
11174398b5aSJeremy L Thompson       *max_dim  = CeedIntMax(*max_dim, field_dim);
11274398b5aSJeremy L Thompson 
11374398b5aSJeremy L Thompson       // Collect P, P_1d, Q, and Q_1d
11474398b5aSJeremy L Thompson       CeedCallBackend(CeedBasisGetNumNodes(basis, &field_P));
11574398b5aSJeremy L Thompson       *max_P = CeedIntMax(*max_P, field_P);
11674398b5aSJeremy L Thompson       CeedCallBackend(CeedBasisIsTensor(basis, &is_field_tensor));
11774398b5aSJeremy L Thompson       if (is_field_tensor) {
11874398b5aSJeremy L Thompson         CeedCallBackend(CeedBasisGetNumNodes1D(basis, &field_P_1d));
11974398b5aSJeremy L Thompson         *max_P_1d = CeedIntMax(*max_P_1d, field_P_1d);
12074398b5aSJeremy L Thompson       }
12174398b5aSJeremy L Thompson       CeedCallBackend(CeedBasisGetNumQuadraturePoints(basis, &field_Q));
12274398b5aSJeremy L Thompson       CeedCheck(*Q == 0 || field_Q == *Q, ceed, CEED_ERROR_BACKEND, "Quadrature spaces must be compatible");
12374398b5aSJeremy L Thompson       *Q = field_Q;
12474398b5aSJeremy L Thompson       if (is_field_tensor) {
12574398b5aSJeremy L Thompson         CeedCallBackend(CeedBasisGetNumQuadraturePoints1D(basis, &field_Q_1d));
1264b3e95d5SJeremy L Thompson         CeedCheck(*Q_1d == 0 || field_Q_1d == *Q_1d, ceed, CEED_ERROR_BACKEND, "Quadrature spaces must be compatible");
1274b3e95d5SJeremy L Thompson         *Q_1d = field_Q_1d;
1284b3e95d5SJeremy L Thompson       }
12974398b5aSJeremy L Thompson     }
13074398b5aSJeremy L Thompson     CeedCallBackend(CeedBasisDestroy(&basis));
13174398b5aSJeremy L Thompson   }
13274398b5aSJeremy L Thompson   for (CeedInt i = 0; i < num_output_fields; i++) {
13374398b5aSJeremy L Thompson     CeedBasis basis;
13474398b5aSJeremy L Thompson 
13574398b5aSJeremy L Thompson     CeedCallBackend(CeedOperatorFieldGetBasis(op_output_fields[i], &basis));
13674398b5aSJeremy L Thompson     if (basis != CEED_BASIS_NONE) {
13774398b5aSJeremy L Thompson       bool    is_field_tensor;
13874398b5aSJeremy L Thompson       CeedInt field_dim = 0, field_P = 0, field_P_1d = 0, field_Q = 0, field_Q_1d = 0;
13974398b5aSJeremy L Thompson 
14074398b5aSJeremy L Thompson       // Check if 3D
14174398b5aSJeremy L Thompson       CeedCallBackend(CeedBasisGetDimension(basis, &field_dim));
14274398b5aSJeremy L Thompson       is_all_3d = is_all_3d && (field_dim == 3);
14374398b5aSJeremy L Thompson       *max_dim  = CeedIntMax(*max_dim, field_dim);
14474398b5aSJeremy L Thompson 
14574398b5aSJeremy L Thompson       // Collect P, P_1d, Q, and Q_1d
14674398b5aSJeremy L Thompson       CeedCallBackend(CeedBasisGetNumNodes(basis, &field_P));
14774398b5aSJeremy L Thompson       *max_P = CeedIntMax(*max_P, field_P);
14874398b5aSJeremy L Thompson       CeedCallBackend(CeedBasisIsTensor(basis, &is_field_tensor));
14974398b5aSJeremy L Thompson       if (is_field_tensor) {
15074398b5aSJeremy L Thompson         CeedCallBackend(CeedBasisGetNumNodes1D(basis, &field_P_1d));
15174398b5aSJeremy L Thompson         *max_P_1d = CeedIntMax(*max_P_1d, field_P_1d);
15274398b5aSJeremy L Thompson       }
15374398b5aSJeremy L Thompson       CeedCallBackend(CeedBasisGetNumQuadraturePoints(basis, &field_Q));
15474398b5aSJeremy L Thompson       CeedCheck(*Q == 0 || field_Q == *Q, ceed, CEED_ERROR_BACKEND, "Quadrature spaces must be compatible");
15574398b5aSJeremy L Thompson       *Q = field_Q;
15674398b5aSJeremy L Thompson       if (is_field_tensor) {
15774398b5aSJeremy L Thompson         CeedCallBackend(CeedBasisGetNumQuadraturePoints1D(basis, &field_Q_1d));
15874398b5aSJeremy L Thompson         CeedCheck(*Q_1d == 0 || field_Q_1d == *Q_1d, ceed, CEED_ERROR_BACKEND, "Quadrature spaces must be compatible");
15974398b5aSJeremy L Thompson         *Q_1d = field_Q_1d;
16074398b5aSJeremy L Thompson       }
16174398b5aSJeremy L Thompson     }
1623a2968d6SJeremy L Thompson     CeedCallBackend(CeedBasisDestroy(&basis));
1634b3e95d5SJeremy L Thompson   }
1644b3e95d5SJeremy L Thompson 
1654b3e95d5SJeremy L Thompson   // Only use 3D collocated gradient parallelization strategy when gradient is computed
1664b3e95d5SJeremy L Thompson   *use_3d_slices = false;
16774398b5aSJeremy L Thompson   if (is_all_3d && *is_all_tensor) {
1684b3e95d5SJeremy L Thompson     bool was_grad_found = false;
1694b3e95d5SJeremy L Thompson 
1704b3e95d5SJeremy L Thompson     for (CeedInt i = 0; i < num_input_fields; i++) {
1714b3e95d5SJeremy L Thompson       CeedEvalMode eval_mode;
1724b3e95d5SJeremy L Thompson 
1734b3e95d5SJeremy L Thompson       CeedCallBackend(CeedQFunctionFieldGetEvalMode(qf_input_fields[i], &eval_mode));
1744b3e95d5SJeremy L Thompson       if (eval_mode == CEED_EVAL_GRAD) {
1754b3e95d5SJeremy L Thompson         CeedBasis_Hip_shared *basis_data;
1764b3e95d5SJeremy L Thompson         CeedBasis             basis;
1774b3e95d5SJeremy L Thompson 
1784b3e95d5SJeremy L Thompson         CeedCallBackend(CeedOperatorFieldGetBasis(op_input_fields[i], &basis));
1794b3e95d5SJeremy L Thompson         CeedCallBackend(CeedBasisGetData(basis, &basis_data));
1804b3e95d5SJeremy L Thompson         *use_3d_slices = basis_data->d_collo_grad_1d && (was_grad_found ? *use_3d_slices : true);
1814b3e95d5SJeremy L Thompson         was_grad_found = true;
1823a2968d6SJeremy L Thompson         CeedCallBackend(CeedBasisDestroy(&basis));
1834b3e95d5SJeremy L Thompson       }
1844b3e95d5SJeremy L Thompson     }
1854b3e95d5SJeremy L Thompson     for (CeedInt i = 0; i < num_output_fields; i++) {
1864b3e95d5SJeremy L Thompson       CeedEvalMode eval_mode;
1874b3e95d5SJeremy L Thompson 
1884b3e95d5SJeremy L Thompson       CeedCallBackend(CeedQFunctionFieldGetEvalMode(qf_output_fields[i], &eval_mode));
1894b3e95d5SJeremy L Thompson       if (eval_mode == CEED_EVAL_GRAD) {
1904b3e95d5SJeremy L Thompson         CeedBasis_Hip_shared *basis_data;
1914b3e95d5SJeremy L Thompson         CeedBasis             basis;
1924b3e95d5SJeremy L Thompson 
1934b3e95d5SJeremy L Thompson         CeedCallBackend(CeedOperatorFieldGetBasis(op_output_fields[i], &basis));
1944b3e95d5SJeremy L Thompson         CeedCallBackend(CeedBasisGetData(basis, &basis_data));
1954b3e95d5SJeremy L Thompson         *use_3d_slices = basis_data->d_collo_grad_1d && (was_grad_found ? *use_3d_slices : true);
1964b3e95d5SJeremy L Thompson         was_grad_found = true;
1973a2968d6SJeremy L Thompson         CeedCallBackend(CeedBasisDestroy(&basis));
1984b3e95d5SJeremy L Thompson       }
1994b3e95d5SJeremy L Thompson     }
2004b3e95d5SJeremy L Thompson   }
2014b3e95d5SJeremy L Thompson   return CEED_ERROR_SUCCESS;
2024b3e95d5SJeremy L Thompson }
2034b3e95d5SJeremy L Thompson 
2044b3e95d5SJeremy L Thompson //------------------------------------------------------------------------------
2054b3e95d5SJeremy L Thompson // Setup fields
2064b3e95d5SJeremy L Thompson //------------------------------------------------------------------------------
2070183ed61SJeremy L Thompson static int CeedOperatorBuildKernelFieldData_Hip_gen(std::ostringstream &code, CeedOperator_Hip_gen *data, Tab &tab, CeedInt i,
2080183ed61SJeremy L Thompson                                                     CeedOperatorField op_field, CeedQFunctionField qf_field, FieldReuse_Hip field_reuse,
2090183ed61SJeremy L Thompson                                                     CeedInt max_dim, CeedInt Q, CeedInt Q_1d, bool is_input, bool is_all_tensor, bool is_at_points,
2100183ed61SJeremy L Thompson                                                     bool use_3d_slices) {
21174398b5aSJeremy L Thompson   bool      is_tensor = true;
21274398b5aSJeremy L Thompson   CeedBasis basis;
21374398b5aSJeremy L Thompson   CeedCallBackend(CeedOperatorFieldGetBasis(op_field, &basis));
21474398b5aSJeremy L Thompson   if (basis != CEED_BASIS_NONE) CeedCallBackend(CeedBasisIsTensor(basis, &is_tensor));
21574398b5aSJeremy L Thompson 
21659fa3f92SJeremy L Thompson   const char           *field_name;
2174b3e95d5SJeremy L Thompson   std::string           var_suffix = (is_input ? "_in_" : "_out_") + std::to_string(i);
2189123fb08SJeremy L Thompson   std::string           P_name = (is_tensor ? "P_1d" : "P") + var_suffix, Q_name = is_tensor ? "Q_1d" : "Q";
2194b3e95d5SJeremy L Thompson   std::string           option_name = (is_input ? "inputs" : "outputs");
2204b3e95d5SJeremy L Thompson   CeedEvalMode          eval_mode   = CEED_EVAL_NONE;
22174398b5aSJeremy L Thompson   CeedInt               elem_size = 0, num_comp = 0, dim = max_dim, P_1d = 0;
2224b3e95d5SJeremy L Thompson   CeedElemRestriction   elem_rstr;
2234b3e95d5SJeremy L Thompson   CeedBasis_Hip_shared *basis_data;
2244b3e95d5SJeremy L Thompson 
2259ee499e5SJeremy L Thompson   // Field reuse info
22645a787f7SJeremy L Thompson   bool use_previous_field = field_reuse.index != -1;
2279ee499e5SJeremy L Thompson 
22859fa3f92SJeremy L Thompson   CeedCallBackend(CeedOperatorFieldGetName(op_field, &field_name));
2290183ed61SJeremy L Thompson   code << tab << "// -- " << (is_input ? "Input" : "Output") << " field " << i << ": " << field_name << "\n";
2304b3e95d5SJeremy L Thompson 
2314b3e95d5SJeremy L Thompson   // Get field data
2324b3e95d5SJeremy L Thompson   CeedCallBackend(CeedOperatorFieldGetElemRestriction(op_field, &elem_rstr));
2334b3e95d5SJeremy L Thompson   if (elem_rstr != CEED_ELEMRESTRICTION_NONE) {
2344b3e95d5SJeremy L Thompson     CeedCallBackend(CeedElemRestrictionGetElementSize(elem_rstr, &elem_size));
2354b3e95d5SJeremy L Thompson     CeedCallBackend(CeedElemRestrictionGetNumComponents(elem_rstr, &num_comp));
2364b3e95d5SJeremy L Thompson   }
2373a2968d6SJeremy L Thompson   CeedCallBackend(CeedElemRestrictionDestroy(&elem_rstr));
2384b3e95d5SJeremy L Thompson   if (basis != CEED_BASIS_NONE) {
2394b3e95d5SJeremy L Thompson     CeedCallBackend(CeedBasisGetData(basis, &basis_data));
24074398b5aSJeremy L Thompson     CeedCallBackend(CeedBasisGetDimension(basis, &dim));
2419123fb08SJeremy L Thompson     if (is_tensor) CeedCallBackend(CeedBasisGetNumNodes1D(basis, &P_1d));
2429123fb08SJeremy L Thompson     else CeedCallBackend(CeedBasisGetNumNodes(basis, &P_1d));
2434b3e95d5SJeremy L Thompson   }
2444b3e95d5SJeremy L Thompson   CeedCallBackend(CeedQFunctionFieldGetEvalMode(qf_field, &eval_mode));
2454b3e95d5SJeremy L Thompson 
2464b3e95d5SJeremy L Thompson   // Set field constants
2470183ed61SJeremy L Thompson   code << tab << "const CeedInt dim" << var_suffix << " = " << dim << ";\n";
24874398b5aSJeremy L Thompson   if (is_tensor && !is_all_tensor) {
24974398b5aSJeremy L Thompson     CeedInt P = 0;
25074398b5aSJeremy L Thompson 
25174398b5aSJeremy L Thompson     CeedCallBackend(CeedBasisGetNumNodes(basis, &P));
2520183ed61SJeremy L Thompson     code << tab << "const CeedInt P" << var_suffix << " = " << (basis == CEED_BASIS_NONE ? Q : P) << ";\n";
25374398b5aSJeremy L Thompson   }
2540183ed61SJeremy L Thompson   code << tab << "const CeedInt " << P_name << " = " << (basis == CEED_BASIS_NONE ? Q_1d : P_1d) << ";\n";
255343e3094SJeremy L Thompson   if (eval_mode != CEED_EVAL_WEIGHT) {
2560183ed61SJeremy L Thompson     code << tab << "const CeedInt num_comp" << var_suffix << " = " << num_comp << ";\n";
2574b3e95d5SJeremy L Thompson   }
2584b3e95d5SJeremy L Thompson 
2594b3e95d5SJeremy L Thompson   // Load basis data
2600183ed61SJeremy L Thompson   code << tab << "// EvalMode: " << CeedEvalModes[eval_mode] << "\n";
2614b3e95d5SJeremy L Thompson   switch (eval_mode) {
2624b3e95d5SJeremy L Thompson     case CEED_EVAL_NONE:
2634b3e95d5SJeremy L Thompson       break;
2644b3e95d5SJeremy L Thompson     case CEED_EVAL_INTERP:
2653a2968d6SJeremy L Thompson       if (is_at_points) {
2663a2968d6SJeremy L Thompson         // AtPoints
2673a2968d6SJeremy L Thompson         if (!basis_data->d_chebyshev_interp_1d) {
2683a2968d6SJeremy L Thompson           CeedSize    interp_bytes;
2693a2968d6SJeremy L Thompson           CeedScalar *chebyshev_interp_1d;
2703a2968d6SJeremy L Thompson 
2713a2968d6SJeremy L Thompson           interp_bytes = P_1d * Q_1d * sizeof(CeedScalar);
2723a2968d6SJeremy L Thompson           CeedCallBackend(CeedCalloc(P_1d * Q_1d, &chebyshev_interp_1d));
2733a2968d6SJeremy L Thompson           CeedCallBackend(CeedBasisGetChebyshevInterp1D(basis, chebyshev_interp_1d));
2743a2968d6SJeremy L Thompson           CeedCallHip(CeedBasisReturnCeed(basis), hipMalloc((void **)&basis_data->d_chebyshev_interp_1d, interp_bytes));
2753a2968d6SJeremy L Thompson           CeedCallHip(CeedBasisReturnCeed(basis),
2763a2968d6SJeremy L Thompson                       hipMemcpy(basis_data->d_chebyshev_interp_1d, chebyshev_interp_1d, interp_bytes, hipMemcpyHostToDevice));
2773a2968d6SJeremy L Thompson           CeedCallBackend(CeedFree(&chebyshev_interp_1d));
2783a2968d6SJeremy L Thompson         }
2793a2968d6SJeremy L Thompson         if (is_input) data->B.inputs[i] = basis_data->d_chebyshev_interp_1d;
2803a2968d6SJeremy L Thompson         else data->B.outputs[i] = basis_data->d_chebyshev_interp_1d;
2813a2968d6SJeremy L Thompson       } else {
2823a2968d6SJeremy L Thompson         // Standard quadrature
2834b3e95d5SJeremy L Thompson         if (is_input) data->B.inputs[i] = basis_data->d_interp_1d;
2844b3e95d5SJeremy L Thompson         else data->B.outputs[i] = basis_data->d_interp_1d;
2853a2968d6SJeremy L Thompson       }
2869ee499e5SJeremy L Thompson       if (use_previous_field) {
28745a787f7SJeremy L Thompson         std::string reuse_var = "s_B" + ((field_reuse.is_input ? "_in_" : "_out_") + std::to_string(field_reuse.index));
2889ee499e5SJeremy L Thompson 
2890183ed61SJeremy L Thompson         code << tab << "CeedScalar *s_B" << var_suffix << " = " << reuse_var << ";\n";
2909ee499e5SJeremy L Thompson       } else {
291*0ccda8ebSJeremy L Thompson         bool is_collocated = false;
292*0ccda8ebSJeremy L Thompson 
293*0ccda8ebSJeremy L Thompson         CeedCallBackend(CeedBasisIsCollocated(basis, &is_collocated));
294*0ccda8ebSJeremy L Thompson         if (is_collocated && !is_at_points) {
295*0ccda8ebSJeremy L Thompson           code << tab << "CeedScalar *s_B" << var_suffix << " = NULL;\n";
296*0ccda8ebSJeremy L Thompson         } else {
2970183ed61SJeremy L Thompson           code << tab << "__shared__ CeedScalar s_B" << var_suffix << "[" << P_name << "*" << Q_name << "];\n";
2980183ed61SJeremy L Thompson           code << tab << "LoadMatrix<" << P_name << ", " << Q_name << ">(data, B." << option_name << "[" << i << "], s_B" << var_suffix << ");\n";
2999ee499e5SJeremy L Thompson         }
300*0ccda8ebSJeremy L Thompson       }
3014b3e95d5SJeremy L Thompson       break;
3024b3e95d5SJeremy L Thompson     case CEED_EVAL_GRAD:
3033a2968d6SJeremy L Thompson       if (is_at_points) {
3043a2968d6SJeremy L Thompson         // AtPoints
3053a2968d6SJeremy L Thompson         if (!basis_data->d_chebyshev_interp_1d) {
3063a2968d6SJeremy L Thompson           CeedSize    interp_bytes;
3073a2968d6SJeremy L Thompson           CeedScalar *chebyshev_interp_1d;
3083a2968d6SJeremy L Thompson 
3093a2968d6SJeremy L Thompson           interp_bytes = P_1d * Q_1d * sizeof(CeedScalar);
3103a2968d6SJeremy L Thompson           CeedCallBackend(CeedCalloc(P_1d * Q_1d, &chebyshev_interp_1d));
3113a2968d6SJeremy L Thompson           CeedCallBackend(CeedBasisGetChebyshevInterp1D(basis, chebyshev_interp_1d));
3123a2968d6SJeremy L Thompson           CeedCallHip(CeedBasisReturnCeed(basis), hipMalloc((void **)&basis_data->d_chebyshev_interp_1d, interp_bytes));
3133a2968d6SJeremy L Thompson           CeedCallHip(CeedBasisReturnCeed(basis),
3143a2968d6SJeremy L Thompson                       hipMemcpy(basis_data->d_chebyshev_interp_1d, chebyshev_interp_1d, interp_bytes, hipMemcpyHostToDevice));
3153a2968d6SJeremy L Thompson           CeedCallBackend(CeedFree(&chebyshev_interp_1d));
3163a2968d6SJeremy L Thompson         }
3173a2968d6SJeremy L Thompson         if (is_input) data->B.inputs[i] = basis_data->d_chebyshev_interp_1d;
3183a2968d6SJeremy L Thompson         else data->B.outputs[i] = basis_data->d_chebyshev_interp_1d;
3193a2968d6SJeremy L Thompson       } else {
3203a2968d6SJeremy L Thompson         // Standard quadrature
3214b3e95d5SJeremy L Thompson         if (is_input) data->B.inputs[i] = basis_data->d_interp_1d;
3224b3e95d5SJeremy L Thompson         else data->B.outputs[i] = basis_data->d_interp_1d;
3233a2968d6SJeremy L Thompson       }
3249123fb08SJeremy L Thompson       if (is_tensor) {
3259ee499e5SJeremy L Thompson         if (use_previous_field) {
32645a787f7SJeremy L Thompson           std::string reuse_var = "s_B" + ((field_reuse.is_input ? "_in_" : "_out_") + std::to_string(field_reuse.index));
3279ee499e5SJeremy L Thompson 
3280183ed61SJeremy L Thompson           code << tab << "CeedScalar *s_B" << var_suffix << " = " << reuse_var << ";\n";
3299ee499e5SJeremy L Thompson         } else {
330*0ccda8ebSJeremy L Thompson           bool is_collocated = false;
331*0ccda8ebSJeremy L Thompson 
332*0ccda8ebSJeremy L Thompson           CeedCallBackend(CeedBasisIsCollocated(basis, &is_collocated));
333*0ccda8ebSJeremy L Thompson           if (is_collocated && !is_at_points) {
334*0ccda8ebSJeremy L Thompson             code << tab << "CeedScalar *s_B" << var_suffix << " = NULL;\n";
335*0ccda8ebSJeremy L Thompson           } else {
3360183ed61SJeremy L Thompson             code << tab << "__shared__ CeedScalar s_B" << var_suffix << "[" << P_name << "*" << Q_name << "];\n";
3370183ed61SJeremy L Thompson             code << tab << "LoadMatrix<" << P_name << ", " << Q_name << ">(data, B." << option_name << "[" << i << "], s_B" << var_suffix << ");\n";
3389123fb08SJeremy L Thompson           }
3399ee499e5SJeremy L Thompson         }
340*0ccda8ebSJeremy L Thompson       }
3413a2968d6SJeremy L Thompson       if (is_at_points) break;  // No G mat for AtPoints
3424b3e95d5SJeremy L Thompson       if (use_3d_slices) {
3434b3e95d5SJeremy L Thompson         if (is_input) data->G.inputs[i] = basis_data->d_collo_grad_1d;
3444b3e95d5SJeremy L Thompson         else data->G.outputs[i] = basis_data->d_collo_grad_1d;
34545a787f7SJeremy L Thompson         if (use_previous_field && field_reuse.eval_mode == CEED_EVAL_GRAD) {
34645a787f7SJeremy L Thompson           std::string reuse_var = "s_G" + ((field_reuse.is_input ? "_in_" : "_out_") + std::to_string(field_reuse.index));
3479ee499e5SJeremy L Thompson 
3480183ed61SJeremy L Thompson           code << tab << "CeedScalar *s_G" << var_suffix << " = " << reuse_var << ";\n";
3499ee499e5SJeremy L Thompson         } else {
3500183ed61SJeremy L Thompson           code << tab << "__shared__ CeedScalar s_G" << var_suffix << "[" << Q_name << "*" << Q_name << "];\n";
3510183ed61SJeremy L Thompson           code << tab << "LoadMatrix<" << Q_name << ", " << Q_name << ">(data, G." << option_name << "[" << i << "], s_G" << var_suffix << ");\n";
3529ee499e5SJeremy L Thompson         }
3534b3e95d5SJeremy L Thompson       } else {
3544b3e95d5SJeremy L Thompson         bool has_collo_grad = basis_data->d_collo_grad_1d;
3554b3e95d5SJeremy L Thompson 
3564b3e95d5SJeremy L Thompson         if (is_input) data->G.inputs[i] = has_collo_grad ? basis_data->d_collo_grad_1d : basis_data->d_grad_1d;
3574b3e95d5SJeremy L Thompson         else data->G.outputs[i] = has_collo_grad ? basis_data->d_collo_grad_1d : basis_data->d_grad_1d;
3584b3e95d5SJeremy L Thompson         if (has_collo_grad) {
35945a787f7SJeremy L Thompson           if (use_previous_field && field_reuse.eval_mode == CEED_EVAL_GRAD) {
36045a787f7SJeremy L Thompson             std::string reuse_var = "s_G" + ((field_reuse.is_input ? "_in_" : "_out_") + std::to_string(field_reuse.index));
3619ee499e5SJeremy L Thompson 
3620183ed61SJeremy L Thompson             code << tab << "CeedScalar *s_G" << var_suffix << " = " << reuse_var << ";\n";
3639ee499e5SJeremy L Thompson           } else {
3640183ed61SJeremy L Thompson             code << tab << "__shared__ CeedScalar s_G" << var_suffix << "[" << Q_name << "*" << Q_name << "];\n";
3650183ed61SJeremy L Thompson             code << tab << "LoadMatrix<" << Q_name << ", " << Q_name << ">(data, G." << option_name << "[" << i << "], s_G" << var_suffix << ");\n";
3669ee499e5SJeremy L Thompson           }
3679ee499e5SJeremy L Thompson         } else {
36845a787f7SJeremy L Thompson           if (use_previous_field && field_reuse.eval_mode == CEED_EVAL_GRAD) {
36945a787f7SJeremy L Thompson             std::string reuse_var = "s_G" + ((field_reuse.is_input ? "_in_" : "_out_") + std::to_string(field_reuse.index));
3709ee499e5SJeremy L Thompson 
3710183ed61SJeremy L Thompson             code << tab << "CeedScalar *s_G" << var_suffix << " = " << reuse_var << ";\n";
3724b3e95d5SJeremy L Thompson           } else {
3730183ed61SJeremy L Thompson             code << tab << "__shared__ CeedScalar s_G" << var_suffix << "[" << P_name << "*" << Q_name << (is_tensor ? "" : "*dim")
37474398b5aSJeremy L Thompson                  << (is_tensor ? "" : var_suffix) << "];\n";
3750183ed61SJeremy L Thompson             code << tab << "LoadMatrix<" << P_name << ", " << Q_name << (is_tensor ? "" : "*dim") << (is_tensor ? "" : var_suffix) << ">(data, G."
37674398b5aSJeremy L Thompson                  << option_name << "[" << i << "], s_G" << var_suffix << ");\n";
3774b3e95d5SJeremy L Thompson           }
3784b3e95d5SJeremy L Thompson         }
3799ee499e5SJeremy L Thompson       }
3804b3e95d5SJeremy L Thompson       break;
3814b3e95d5SJeremy L Thompson     case CEED_EVAL_WEIGHT:
3824b3e95d5SJeremy L Thompson       break;  // No action
3834b3e95d5SJeremy L Thompson       // LCOV_EXCL_START
3844b3e95d5SJeremy L Thompson     case CEED_EVAL_DIV:
3854b3e95d5SJeremy L Thompson     case CEED_EVAL_CURL:
3864b3e95d5SJeremy L Thompson       break;  // TODO: Not implemented
3874b3e95d5SJeremy L Thompson               // LCOV_EXCL_STOP
3884b3e95d5SJeremy L Thompson   }
3893a2968d6SJeremy L Thompson   CeedCallBackend(CeedBasisDestroy(&basis));
3904b3e95d5SJeremy L Thompson   return CEED_ERROR_SUCCESS;
3914b3e95d5SJeremy L Thompson }
3924b3e95d5SJeremy L Thompson 
3934b3e95d5SJeremy L Thompson //------------------------------------------------------------------------------
3944b3e95d5SJeremy L Thompson // Restriction
3954b3e95d5SJeremy L Thompson //------------------------------------------------------------------------------
3960183ed61SJeremy L Thompson static int CeedOperatorBuildKernelRestriction_Hip_gen(std::ostringstream &code, CeedOperator_Hip_gen *data, Tab &tab, CeedInt i,
3970183ed61SJeremy L Thompson                                                       CeedInt field_input_buffer[], CeedOperatorField op_field, CeedQFunctionField qf_field,
3980183ed61SJeremy L Thompson                                                       CeedInt max_dim, CeedInt Q_1d, bool is_input, bool is_all_tensor, bool is_at_points,
3990183ed61SJeremy L Thompson                                                       bool use_3d_slices) {
4004b3e95d5SJeremy L Thompson   std::string              var_suffix = (is_input ? "_in_" : "_out_") + std::to_string(i);
40174398b5aSJeremy L Thompson   std::string              P_name     = (is_all_tensor ? "P_1d" : "P") + var_suffix;
4024b3e95d5SJeremy L Thompson   CeedEvalMode             eval_mode  = CEED_EVAL_NONE;
40374398b5aSJeremy L Thompson   CeedInt                  elem_size = 0, num_comp = 0;
4044b3e95d5SJeremy L Thompson   CeedSize                 l_size;
405f815fac9SJeremy L Thompson   CeedRestrictionType      rstr_type = CEED_RESTRICTION_STANDARD;
4064b3e95d5SJeremy L Thompson   CeedElemRestriction_Hip *rstr_data;
4074b3e95d5SJeremy L Thompson   CeedElemRestriction      elem_rstr;
4084b3e95d5SJeremy L Thompson 
4094b3e95d5SJeremy L Thompson   // Get field data
4104b3e95d5SJeremy L Thompson   CeedCallBackend(CeedOperatorFieldGetElemRestriction(op_field, &elem_rstr));
4114b3e95d5SJeremy L Thompson   if (elem_rstr != CEED_ELEMRESTRICTION_NONE) {
412f815fac9SJeremy L Thompson     CeedCallBackend(CeedElemRestrictionGetType(elem_rstr, &rstr_type));
4134b3e95d5SJeremy L Thompson     CeedCallBackend(CeedElemRestrictionGetElementSize(elem_rstr, &elem_size));
4144b3e95d5SJeremy L Thompson     CeedCallBackend(CeedElemRestrictionGetNumComponents(elem_rstr, &num_comp));
4154b3e95d5SJeremy L Thompson     CeedCallBackend(CeedElemRestrictionGetData(elem_rstr, &rstr_data));
4164b3e95d5SJeremy L Thompson   }
4174b3e95d5SJeremy L Thompson   CeedCallBackend(CeedQFunctionFieldGetEvalMode(qf_field, &eval_mode));
4184b3e95d5SJeremy L Thompson 
4194b3e95d5SJeremy L Thompson   // Restriction
4204b3e95d5SJeremy L Thompson   if (is_input) {
4214b3e95d5SJeremy L Thompson     // Input
422e93651e5SJeremy L Thompson     if (field_input_buffer[i] != i) {
423e93651e5SJeremy L Thompson       std::string buffer_name = "r_e_in_" + std::to_string(field_input_buffer[i]);
424e93651e5SJeremy L Thompson 
425e93651e5SJeremy L Thompson       // Restriction was already done for previous input
4260183ed61SJeremy L Thompson       code << tab << "CeedScalar *r_e" << var_suffix << " = " << buffer_name << ";\n";
4273a2968d6SJeremy L Thompson     } else if (eval_mode != CEED_EVAL_WEIGHT && !((eval_mode == CEED_EVAL_NONE) && use_3d_slices && is_at_points)) {
4283a2968d6SJeremy L Thompson       if (eval_mode == CEED_EVAL_NONE && rstr_type != CEED_RESTRICTION_POINTS) {
429e93651e5SJeremy L Thompson         // No basis action, so r_e_in_* in also r_q_in_* and needs to be allocated
4300183ed61SJeremy L Thompson         code << tab << "CeedScalar r_e" << var_suffix << "[num_comp" << var_suffix << "*" << P_name << "];\n";
4313a2968d6SJeremy L Thompson       } else if (rstr_type != CEED_RESTRICTION_POINTS) {
432e93651e5SJeremy L Thompson         // Otherwise we're using the scratch space
4330183ed61SJeremy L Thompson         code << tab << "CeedScalar *r_e" << var_suffix << " = r_e_scratch;\n";
434e93651e5SJeremy L Thompson       }
435f815fac9SJeremy L Thompson       switch (rstr_type) {
436f815fac9SJeremy L Thompson         case CEED_RESTRICTION_STANDARD: {
4374b3e95d5SJeremy L Thompson           CeedInt comp_stride;
4384b3e95d5SJeremy L Thompson 
4394b3e95d5SJeremy L Thompson           CeedCallBackend(CeedElemRestrictionGetLVectorSize(elem_rstr, &l_size));
4400183ed61SJeremy L Thompson           code << tab << "const CeedInt l_size" << var_suffix << " = " << l_size << ";\n";
4414b3e95d5SJeremy L Thompson           CeedCallBackend(CeedElemRestrictionGetCompStride(elem_rstr, &comp_stride));
4420183ed61SJeremy L Thompson           code << tab << "const CeedInt comp_stride" << var_suffix << " = " << comp_stride << ";\n";
4434b3e95d5SJeremy L Thompson           data->indices.inputs[i] = (CeedInt *)rstr_data->d_offsets;
4440183ed61SJeremy L Thompson           code << tab << "ReadLVecStandard" << (is_all_tensor ? max_dim : 1) << "d<num_comp" << var_suffix << ", comp_stride" << var_suffix << ", "
4450183ed61SJeremy L Thompson                << P_name << ">(data, l_size" << var_suffix << ", elem, indices.inputs[" << i << "], d" << var_suffix << ", r_e" << var_suffix
4460183ed61SJeremy L Thompson                << ");\n";
447f815fac9SJeremy L Thompson           break;
448f815fac9SJeremy L Thompson         }
449f815fac9SJeremy L Thompson         case CEED_RESTRICTION_STRIDED: {
4504b3e95d5SJeremy L Thompson           bool    has_backend_strides;
4514b3e95d5SJeremy L Thompson           CeedInt num_elem;
4524b3e95d5SJeremy L Thompson 
4534b3e95d5SJeremy L Thompson           CeedCallBackend(CeedElemRestrictionHasBackendStrides(elem_rstr, &has_backend_strides));
4544b3e95d5SJeremy L Thompson           CeedCallBackend(CeedElemRestrictionGetNumElements(elem_rstr, &num_elem));
4554b3e95d5SJeremy L Thompson           CeedInt strides[3] = {1, elem_size * num_elem, elem_size};
4564b3e95d5SJeremy L Thompson 
4574b3e95d5SJeremy L Thompson           if (!has_backend_strides) {
4584b3e95d5SJeremy L Thompson             CeedCallBackend(CeedElemRestrictionGetStrides(elem_rstr, strides));
4594b3e95d5SJeremy L Thompson           }
4600183ed61SJeremy L Thompson           code << tab << "const CeedInt strides" << var_suffix << "_0 = " << strides[0] << ", strides" << var_suffix << "_1 = " << strides[1]
4610183ed61SJeremy L Thompson                << ", strides" << var_suffix << "_2 = " << strides[2] << ";\n";
4620183ed61SJeremy L Thompson           code << tab << "ReadLVecStrided" << (is_all_tensor ? max_dim : 1) << "d<num_comp" << var_suffix << ", " << P_name << ", strides"
4630183ed61SJeremy L Thompson                << var_suffix << "_0, strides" << var_suffix << "_1, strides" << var_suffix << "_2>(data, elem, d" << var_suffix << ", r_e"
4640183ed61SJeremy L Thompson                << var_suffix << ");\n";
465f815fac9SJeremy L Thompson           break;
466f815fac9SJeremy L Thompson         }
4673a2968d6SJeremy L Thompson         case CEED_RESTRICTION_POINTS: {
4683a2968d6SJeremy L Thompson           CeedInt comp_stride;
4693a2968d6SJeremy L Thompson 
4703a2968d6SJeremy L Thompson           CeedCallBackend(CeedElemRestrictionGetCompStride(elem_rstr, &comp_stride));
4710183ed61SJeremy L Thompson           code << tab << "const CeedInt comp_stride" << var_suffix << " = " << comp_stride << ";\n";
4723a2968d6SJeremy L Thompson           data->indices.inputs[i] = (CeedInt *)rstr_data->d_offsets;
4733a2968d6SJeremy L Thompson           break;
4743a2968d6SJeremy L Thompson         }
475f815fac9SJeremy L Thompson         // LCOV_EXCL_START
476f815fac9SJeremy L Thompson         case CEED_RESTRICTION_ORIENTED:
477f815fac9SJeremy L Thompson         case CEED_RESTRICTION_CURL_ORIENTED:
478f815fac9SJeremy L Thompson           break;  // TODO: Not implemented
479f815fac9SJeremy L Thompson                   // LCOV_EXCL_STOP
4804b3e95d5SJeremy L Thompson       }
4814b3e95d5SJeremy L Thompson     }
4824b3e95d5SJeremy L Thompson   } else {
4834b3e95d5SJeremy L Thompson     // Output
484f815fac9SJeremy L Thompson     switch (rstr_type) {
485f815fac9SJeremy L Thompson       case CEED_RESTRICTION_STANDARD: {
4864b3e95d5SJeremy L Thompson         CeedInt comp_stride;
4874b3e95d5SJeremy L Thompson 
4884b3e95d5SJeremy L Thompson         CeedCallBackend(CeedElemRestrictionGetLVectorSize(elem_rstr, &l_size));
4890183ed61SJeremy L Thompson         code << tab << "const CeedInt l_size" << var_suffix << " = " << l_size << ";\n";
4904b3e95d5SJeremy L Thompson         CeedCallBackend(CeedElemRestrictionGetCompStride(elem_rstr, &comp_stride));
4910183ed61SJeremy L Thompson         code << tab << "const CeedInt comp_stride" << var_suffix << " = " << comp_stride << ";\n";
4924b3e95d5SJeremy L Thompson         data->indices.outputs[i] = (CeedInt *)rstr_data->d_offsets;
4930183ed61SJeremy L Thompson         code << tab << "WriteLVecStandard" << (is_all_tensor ? max_dim : 1) << "d<num_comp" << var_suffix << ", comp_stride" << var_suffix << ", "
4940183ed61SJeremy L Thompson              << P_name << ">(data, l_size" << var_suffix << ", elem, indices.outputs[" << i << "], r_e" << var_suffix << ", d" << var_suffix
4950183ed61SJeremy L Thompson              << ");\n";
496f815fac9SJeremy L Thompson         break;
497f815fac9SJeremy L Thompson       }
498f815fac9SJeremy L Thompson       case CEED_RESTRICTION_STRIDED: {
4994b3e95d5SJeremy L Thompson         bool    has_backend_strides;
5004b3e95d5SJeremy L Thompson         CeedInt num_elem;
5014b3e95d5SJeremy L Thompson 
5024b3e95d5SJeremy L Thompson         CeedCallBackend(CeedElemRestrictionHasBackendStrides(elem_rstr, &has_backend_strides));
5034b3e95d5SJeremy L Thompson         CeedCallBackend(CeedElemRestrictionGetNumElements(elem_rstr, &num_elem));
5044b3e95d5SJeremy L Thompson         CeedInt strides[3] = {1, elem_size * num_elem, elem_size};
5054b3e95d5SJeremy L Thompson 
5064b3e95d5SJeremy L Thompson         if (!has_backend_strides) {
5074b3e95d5SJeremy L Thompson           CeedCallBackend(CeedElemRestrictionGetStrides(elem_rstr, strides));
5084b3e95d5SJeremy L Thompson         }
5090183ed61SJeremy L Thompson         code << tab << "const CeedInt strides" << var_suffix << "_0 = " << strides[0] << ", strides" << var_suffix << "_1 = " << strides[1]
5100183ed61SJeremy L Thompson              << ", strides" << var_suffix << "_2 = " << strides[2] << ";\n";
5110183ed61SJeremy L Thompson         code << tab << "WriteLVecStrided" << (is_all_tensor ? max_dim : 1) << "d<num_comp" << var_suffix << ", " << P_name << ", strides"
5120183ed61SJeremy L Thompson              << var_suffix << "_0, strides" << var_suffix << "_1, strides" << var_suffix << "_2>(data, elem, r_e" << var_suffix << ", d" << var_suffix
5130183ed61SJeremy L Thompson              << ");\n";
514f815fac9SJeremy L Thompson         break;
515f815fac9SJeremy L Thompson       }
5163a2968d6SJeremy L Thompson       case CEED_RESTRICTION_POINTS:
5173a2968d6SJeremy L Thompson         data->indices.outputs[i] = (CeedInt *)rstr_data->d_offsets;
5183a2968d6SJeremy L Thompson         break;
519f815fac9SJeremy L Thompson       // LCOV_EXCL_START
520f815fac9SJeremy L Thompson       case CEED_RESTRICTION_ORIENTED:
521f815fac9SJeremy L Thompson       case CEED_RESTRICTION_CURL_ORIENTED:
522f815fac9SJeremy L Thompson         break;  // TODO: Not implemented
523f815fac9SJeremy L Thompson                 // LCOV_EXCL_STOP
5244b3e95d5SJeremy L Thompson     }
5254b3e95d5SJeremy L Thompson   }
5263a2968d6SJeremy L Thompson   CeedCallBackend(CeedElemRestrictionDestroy(&elem_rstr));
5274b3e95d5SJeremy L Thompson   return CEED_ERROR_SUCCESS;
5284b3e95d5SJeremy L Thompson }
5294b3e95d5SJeremy L Thompson 
5304b3e95d5SJeremy L Thompson //------------------------------------------------------------------------------
5314b3e95d5SJeremy L Thompson // Basis
5324b3e95d5SJeremy L Thompson //------------------------------------------------------------------------------
5330183ed61SJeremy L Thompson static int CeedOperatorBuildKernelBasis_Hip_gen(std::ostringstream &code, CeedOperator_Hip_gen *data, Tab &tab, CeedInt i, CeedOperatorField op_field,
53474398b5aSJeremy L Thompson                                                 CeedQFunctionField qf_field, CeedInt max_dim, CeedInt Q_1d, bool is_input, bool is_all_tensor,
5353a2968d6SJeremy L Thompson                                                 bool is_at_points, bool use_3d_slices) {
536*0ccda8ebSJeremy L Thompson   bool      is_tensor = true, is_collocated = true;
53774398b5aSJeremy L Thompson   CeedBasis basis;
53874398b5aSJeremy L Thompson   CeedCallBackend(CeedOperatorFieldGetBasis(op_field, &basis));
53974398b5aSJeremy L Thompson   CeedCallBackend(CeedBasisIsTensor(basis, &is_tensor));
540*0ccda8ebSJeremy L Thompson   CeedCallBackend(CeedBasisIsCollocated(basis, &is_collocated));
54174398b5aSJeremy L Thompson 
5424b3e95d5SJeremy L Thompson   std::string         var_suffix = (is_input ? "_in_" : "_out_") + std::to_string(i);
5439123fb08SJeremy L Thompson   std::string         P_name = (is_tensor ? "P_1d" : "P") + var_suffix, Q_name = is_tensor ? "Q_1d" : "Q";
5444b3e95d5SJeremy L Thompson   CeedEvalMode        eval_mode = CEED_EVAL_NONE;
54574398b5aSJeremy L Thompson   CeedInt             dim = max_dim, elem_size = 0, num_comp = 0, P_1d = 0;
5464b3e95d5SJeremy L Thompson   CeedElemRestriction elem_rstr;
5474b3e95d5SJeremy L Thompson 
5484b3e95d5SJeremy L Thompson   // Get field data
5494b3e95d5SJeremy L Thompson   CeedCallBackend(CeedOperatorFieldGetElemRestriction(op_field, &elem_rstr));
5504b3e95d5SJeremy L Thompson   if (elem_rstr != CEED_ELEMRESTRICTION_NONE) {
5514b3e95d5SJeremy L Thompson     CeedCallBackend(CeedElemRestrictionGetElementSize(elem_rstr, &elem_size));
5524b3e95d5SJeremy L Thompson     CeedCallBackend(CeedElemRestrictionGetNumComponents(elem_rstr, &num_comp));
5534b3e95d5SJeremy L Thompson   }
5543a2968d6SJeremy L Thompson   CeedCallBackend(CeedElemRestrictionDestroy(&elem_rstr));
5554b3e95d5SJeremy L Thompson   if (basis != CEED_BASIS_NONE) {
55674398b5aSJeremy L Thompson     CeedCallBackend(CeedBasisGetDimension(basis, &dim));
5579123fb08SJeremy L Thompson     if (is_tensor) CeedCallBackend(CeedBasisGetNumNodes1D(basis, &P_1d));
5589123fb08SJeremy L Thompson     else CeedCallBackend(CeedBasisGetNumNodes(basis, &P_1d));
5594b3e95d5SJeremy L Thompson   }
5604b3e95d5SJeremy L Thompson   CeedCallBackend(CeedQFunctionFieldGetEvalMode(qf_field, &eval_mode));
5614b3e95d5SJeremy L Thompson 
5624b3e95d5SJeremy L Thompson   // Basis
5630183ed61SJeremy L Thompson   code << tab << "// EvalMode: " << CeedEvalModes[eval_mode] << "\n";
5644b3e95d5SJeremy L Thompson   if (is_input) {
5654b3e95d5SJeremy L Thompson     switch (eval_mode) {
5664b3e95d5SJeremy L Thompson       case CEED_EVAL_NONE:
5673a2968d6SJeremy L Thompson         if (!use_3d_slices && !is_at_points) {
5680183ed61SJeremy L Thompson           code << tab << "CeedScalar *r_q" << var_suffix << " = r_e" << var_suffix << ";\n";
5694b3e95d5SJeremy L Thompson         }
5704b3e95d5SJeremy L Thompson         break;
5714b3e95d5SJeremy L Thompson       case CEED_EVAL_INTERP:
5723a2968d6SJeremy L Thompson         if (is_at_points) {
5739123fb08SJeremy L Thompson           std::string function_name = (dim == 1 ? "Interp" : "InterpTensor") + std::to_string(dim) + "d";
5749123fb08SJeremy L Thompson 
5750183ed61SJeremy L Thompson           code << tab << "CeedScalar r_c" << var_suffix << "[num_comp" << var_suffix << "*" << (dim >= 3 ? Q_name : "1") << "];\n";
5760183ed61SJeremy L Thompson           code << tab << function_name << "<num_comp" << var_suffix << ", " << P_name << ", " << Q_name << ", OP_T_1D>(data, r_e" << var_suffix
5776b92dc4bSJeremy L Thompson                << ", s_B" << var_suffix << ", r_c" << var_suffix << ");\n";
5783a2968d6SJeremy L Thompson         } else {
579*0ccda8ebSJeremy L Thompson           std::string function_name = is_tensor ? ((dim == 1 ? "Interp" : "InterpTensor") + std::string(is_collocated ? "CollocatedNodes" : "") +
580*0ccda8ebSJeremy L Thompson                                                    std::to_string(dim) + "d" + (is_all_tensor ? "" : "Flattened"))
58174398b5aSJeremy L Thompson                                                 : "InterpNonTensor";
58274398b5aSJeremy L Thompson           std::string op_t_1d_name  = (is_all_tensor || !is_tensor) ? "OP_T_1D" : (P_1d > Q_1d ? P_name : Q_name);
5839123fb08SJeremy L Thompson 
5840183ed61SJeremy L Thompson           code << tab << "CeedScalar r_q" << var_suffix << "[num_comp" << var_suffix << "*" << (is_all_tensor && (dim >= 3) ? Q_name : "1") << "];\n";
5850183ed61SJeremy L Thompson           code << tab << function_name << "<num_comp" << var_suffix << ", " << P_name << ", " << Q_name << ", " << op_t_1d_name << ">(data, r_e"
58674398b5aSJeremy L Thompson                << var_suffix << ", s_B" << var_suffix << ", r_q" << var_suffix << ");\n";
5873a2968d6SJeremy L Thompson         }
5884b3e95d5SJeremy L Thompson         break;
5894b3e95d5SJeremy L Thompson       case CEED_EVAL_GRAD:
5903a2968d6SJeremy L Thompson         if (is_at_points) {
5919123fb08SJeremy L Thompson           std::string function_name = (dim == 1 ? "Interp" : "InterpTensor") + std::to_string(dim) + "d";
5929123fb08SJeremy L Thompson 
5930183ed61SJeremy L Thompson           code << tab << "CeedScalar r_c" << var_suffix << "[num_comp" << var_suffix << "*" << (dim >= 3 ? Q_name : "1") << "];\n";
5940183ed61SJeremy L Thompson           code << tab << function_name << "<num_comp" << var_suffix << ", " << P_name << ", " << Q_name << ", OP_T_1D>(data, r_e" << var_suffix
5956b92dc4bSJeremy L Thompson                << ", s_B" << var_suffix << ", r_c" << var_suffix << ");\n";
5963a2968d6SJeremy L Thompson         } else if (use_3d_slices) {
597*0ccda8ebSJeremy L Thompson           std::string function_name =
598*0ccda8ebSJeremy L Thompson               (dim > 1 ? "InterpTensor" : "Interp") + std::string(is_collocated ? "CollocatedNodes" : "") + std::to_string(dim) + "d";
5999123fb08SJeremy L Thompson 
6000183ed61SJeremy L Thompson           code << tab << "CeedScalar r_q" << var_suffix << "[num_comp" << var_suffix << "*" << Q_name << "];\n";
6010183ed61SJeremy L Thompson           code << tab << function_name << "<num_comp" << var_suffix << ", " << P_name << ", " << Q_name << ", OP_T_1D>(data, r_e" << var_suffix
6026b92dc4bSJeremy L Thompson                << ", s_B" << var_suffix << ", r_q" << var_suffix << ");\n";
6039123fb08SJeremy L Thompson         } else if (is_tensor) {
604*0ccda8ebSJeremy L Thompson           bool        is_collocated_grad = dim == 3 && Q_1d >= P_1d;
605*0ccda8ebSJeremy L Thompson           std::string function_name =
606*0ccda8ebSJeremy L Thompson               (dim == 1 ? "Grad" : ("GradTensor" + std::string(is_collocated ? "CollocatedNodes" : (is_collocated_grad ? "Collocated" : "")))) +
607*0ccda8ebSJeremy L Thompson               std::to_string(dim) + "d" + (is_all_tensor ? "" : "Flattened");
60874398b5aSJeremy L Thompson           std::string op_t_1d_name = is_all_tensor ? "OP_T_1D" : (P_1d > Q_1d ? P_name : Q_name);
6099123fb08SJeremy L Thompson 
6100183ed61SJeremy L Thompson           code << tab << "CeedScalar r_q" << var_suffix << "[num_comp" << var_suffix << "*dim" << var_suffix << "*"
61174398b5aSJeremy L Thompson                << (is_all_tensor && dim >= 3 ? Q_name : "1") << "];\n";
6120183ed61SJeremy L Thompson           code << tab << function_name << "<num_comp" << var_suffix << ", " << P_name << ", " << Q_name << ", " << op_t_1d_name << ">(data, r_e"
61374398b5aSJeremy L Thompson                << var_suffix << ", s_B" << var_suffix << ", s_G" << var_suffix << ", r_q" << var_suffix << ");\n";
6144b3e95d5SJeremy L Thompson         } else {
6159123fb08SJeremy L Thompson           std::string function_name = "GradNonTensor";
6169123fb08SJeremy L Thompson 
6170183ed61SJeremy L Thompson           code << tab << "CeedScalar r_q" << var_suffix << "[num_comp" << var_suffix << "*dim" << var_suffix << "];\n";
6180183ed61SJeremy L Thompson           code << tab << function_name << "<num_comp" << var_suffix << ", dim" << var_suffix << ", " << P_name << ", " << Q_name
61974398b5aSJeremy L Thompson                << ", OP_T_1D>(data, r_e" << var_suffix << ", s_G" << var_suffix << ", r_q" << var_suffix << ");\n";
6204b3e95d5SJeremy L Thompson         }
6214b3e95d5SJeremy L Thompson         break;
6224b3e95d5SJeremy L Thompson       case CEED_EVAL_WEIGHT: {
6233a2968d6SJeremy L Thompson         if (is_at_points) {
6240183ed61SJeremy L Thompson           code << tab << "// Nothing to do AtPoints\n";
6253a2968d6SJeremy L Thompson         } else {
6264b3e95d5SJeremy L Thompson           CeedBasis_Hip_shared *basis_data;
62774398b5aSJeremy L Thompson           std::string           function_name = is_tensor
62874398b5aSJeremy L Thompson                                                     ? ((dim == 1 ? "Weight" : "WeightTensor") + std::to_string(dim) + "d" + (is_all_tensor ? "" : "Flattened"))
62974398b5aSJeremy L Thompson                                                     : "WeightNonTensor";
6304b3e95d5SJeremy L Thompson 
6310183ed61SJeremy L Thompson           code << tab << "CeedScalar r_q" << var_suffix << "[" << (is_all_tensor && (dim >= 3) ? Q_name : "1") << "];\n";
6324b3e95d5SJeremy L Thompson           CeedCallBackend(CeedBasisGetData(basis, &basis_data));
6334b3e95d5SJeremy L Thompson           data->W = basis_data->d_q_weight_1d;
6340183ed61SJeremy L Thompson           code << tab << function_name << "<" << P_name << ", " << Q_name << ">(data, W, r_q" << var_suffix << ");\n";
6353a2968d6SJeremy L Thompson         }
6364b3e95d5SJeremy L Thompson         break;
6374b3e95d5SJeremy L Thompson       }
6384b3e95d5SJeremy L Thompson       // LCOV_EXCL_START
6394b3e95d5SJeremy L Thompson       case CEED_EVAL_DIV:
6404b3e95d5SJeremy L Thompson       case CEED_EVAL_CURL:
6414b3e95d5SJeremy L Thompson         break;  // TODO: Not implemented
6424b3e95d5SJeremy L Thompson                 // LCOV_EXCL_STOP
6434b3e95d5SJeremy L Thompson     }
6444b3e95d5SJeremy L Thompson   } else {
6454b3e95d5SJeremy L Thompson     switch (eval_mode) {
6464b3e95d5SJeremy L Thompson       case CEED_EVAL_NONE:
6470183ed61SJeremy L Thompson         code << tab << "CeedScalar *r_e" << var_suffix << " = r_q" << var_suffix << ";\n";
6484b3e95d5SJeremy L Thompson         break;  // No action
6494b3e95d5SJeremy L Thompson       case CEED_EVAL_INTERP:
6500183ed61SJeremy L Thompson         code << tab << "CeedScalar *r_e" << var_suffix << " = r_e_scratch;\n";
6513a2968d6SJeremy L Thompson         if (is_at_points) {
6529123fb08SJeremy L Thompson           std::string function_name = (dim == 1 ? "InterpTranspose" : "InterpTransposeTensor") + std::to_string(dim) + "d";
6539123fb08SJeremy L Thompson 
6540183ed61SJeremy L Thompson           code << tab << function_name << "<num_comp" << var_suffix << ", " << P_name << ", " << Q_name << ", OP_T_1D>(data, r_c" << var_suffix
6556b92dc4bSJeremy L Thompson                << ", s_B" << var_suffix << ", r_e" << var_suffix << ");\n";
6563a2968d6SJeremy L Thompson         } else {
6579123fb08SJeremy L Thompson           std::string function_name =
658*0ccda8ebSJeremy L Thompson               is_tensor ? ((dim == 1 ? "InterpTranspose" : "InterpTransposeTensor") + std::string(is_collocated ? "CollocatedNodes" : "") +
659*0ccda8ebSJeremy L Thompson                            std::to_string(dim) + "d" + (is_all_tensor ? "" : "Flattened"))
66074398b5aSJeremy L Thompson                         : "InterpTransposeNonTensor";
66174398b5aSJeremy L Thompson           std::string op_t_1d_name = (is_all_tensor || !is_tensor) ? "OP_T_1D" : (P_1d > Q_1d ? P_name : Q_name);
6629123fb08SJeremy L Thompson 
6630183ed61SJeremy L Thompson           code << tab << function_name << "<num_comp" << var_suffix << ", " << P_name << ", " << Q_name << ", " << op_t_1d_name << ">(data, r_q"
66474398b5aSJeremy L Thompson                << var_suffix << ", s_B" << var_suffix << ", r_e" << var_suffix << ");\n";
6653a2968d6SJeremy L Thompson         }
6664b3e95d5SJeremy L Thompson         break;
6674b3e95d5SJeremy L Thompson       case CEED_EVAL_GRAD:
6680183ed61SJeremy L Thompson         code << tab << "CeedScalar *r_e" << var_suffix << " = r_e_scratch;\n";
6693a2968d6SJeremy L Thompson         if (is_at_points) {
6709123fb08SJeremy L Thompson           std::string function_name = (dim == 1 ? "InterpTranspose" : "InterpTransposeTensor") + std::to_string(dim) + "d";
6719123fb08SJeremy L Thompson 
6720183ed61SJeremy L Thompson           code << tab << function_name << "<num_comp" << var_suffix << ", " << P_name << ", " << Q_name << ", OP_T_1D>(data, r_c" << var_suffix
6736b92dc4bSJeremy L Thompson                << ", s_B" << var_suffix << ", r_e" << var_suffix << ");\n";
6743a2968d6SJeremy L Thompson         } else if (use_3d_slices) {
675*0ccda8ebSJeremy L Thompson           std::string function_name = (dim == 1 ? "InterpTranspose" : "InterpTransposeTensor") + std::string(is_collocated ? "CollocatedNodes" : "") +
676*0ccda8ebSJeremy L Thompson                                       std::to_string(dim) + "d";
6779123fb08SJeremy L Thompson 
6780183ed61SJeremy L Thompson           code << tab << function_name << "<num_comp" << var_suffix << ", " << P_name << ", " << Q_name << ", OP_T_1D>(data, r_q" << var_suffix
6796b92dc4bSJeremy L Thompson                << ", s_B" << var_suffix << ", r_e" << var_suffix << ");\n";
6809123fb08SJeremy L Thompson         } else if (is_tensor) {
681*0ccda8ebSJeremy L Thompson           bool        is_collocated_grad = dim == 3 && Q_1d >= P_1d;
682*0ccda8ebSJeremy L Thompson           std::string function_name =
683*0ccda8ebSJeremy L Thompson               (dim == 1 ? "GradTranspose"
684*0ccda8ebSJeremy L Thompson                         : ("GradTransposeTensor" + std::string(is_collocated ? "CollocatedNodes" : (is_collocated_grad ? "Collocated" : "")))) +
68574398b5aSJeremy L Thompson               std::to_string(dim) + "d" + (is_all_tensor ? "" : "Flattened");
68674398b5aSJeremy L Thompson           std::string op_t_1d_name = is_all_tensor ? "OP_T_1D" : (P_1d > Q_1d ? P_name : Q_name);
6879123fb08SJeremy L Thompson 
6880183ed61SJeremy L Thompson           code << tab << function_name << "<num_comp" << var_suffix << ", " << P_name << ", " << Q_name << ", " << op_t_1d_name << ">(data, r_q"
68974398b5aSJeremy L Thompson                << var_suffix << ", s_B" << var_suffix << ", s_G" << var_suffix << ", r_e" << var_suffix << ");\n";
6904b3e95d5SJeremy L Thompson         } else {
6919123fb08SJeremy L Thompson           std::string function_name = "GradTransposeNonTensor";
6929123fb08SJeremy L Thompson 
6930183ed61SJeremy L Thompson           code << tab << function_name << "<num_comp" << var_suffix << ", dim" << var_suffix << ", " << P_name << ", " << Q_name
69474398b5aSJeremy L Thompson                << ", OP_T_1D>(data, r_q" << var_suffix << ", s_G" << var_suffix << ", r_e" << var_suffix << ");\n";
6954b3e95d5SJeremy L Thompson         }
6964b3e95d5SJeremy L Thompson         break;
6974b3e95d5SJeremy L Thompson       // LCOV_EXCL_START
6984b3e95d5SJeremy L Thompson       case CEED_EVAL_WEIGHT:
6994b3e95d5SJeremy L Thompson         break;  // Should not occur
7004b3e95d5SJeremy L Thompson       case CEED_EVAL_DIV:
7014b3e95d5SJeremy L Thompson       case CEED_EVAL_CURL:
7024b3e95d5SJeremy L Thompson         break;  // TODO: Not implemented
7034b3e95d5SJeremy L Thompson                 // LCOV_EXCL_STOP
7044b3e95d5SJeremy L Thompson     }
7054b3e95d5SJeremy L Thompson   }
7063a2968d6SJeremy L Thompson   CeedCallBackend(CeedBasisDestroy(&basis));
7074b3e95d5SJeremy L Thompson   return CEED_ERROR_SUCCESS;
7084b3e95d5SJeremy L Thompson }
7094b3e95d5SJeremy L Thompson 
7104b3e95d5SJeremy L Thompson //------------------------------------------------------------------------------
7114b3e95d5SJeremy L Thompson // QFunction
7124b3e95d5SJeremy L Thompson //------------------------------------------------------------------------------
7130183ed61SJeremy L Thompson static int CeedOperatorBuildKernelQFunction_Hip_gen(std::ostringstream &code, CeedOperator_Hip_gen *data, Tab &tab, CeedInt max_dim,
7140183ed61SJeremy L Thompson                                                     CeedInt max_num_points, CeedInt num_input_fields, CeedOperatorField *op_input_fields,
7150183ed61SJeremy L Thompson                                                     CeedQFunctionField *qf_input_fields, CeedInt num_output_fields,
7160183ed61SJeremy L Thompson                                                     CeedOperatorField *op_output_fields, CeedQFunctionField *qf_output_fields,
7170183ed61SJeremy L Thompson                                                     std::string qfunction_name, CeedInt Q_1d, bool is_all_tensor, bool is_at_points,
7180183ed61SJeremy L Thompson                                                     bool use_3d_slices) {
71974398b5aSJeremy L Thompson   std::string         Q_name    = is_all_tensor ? "Q_1d" : "Q";
7204b3e95d5SJeremy L Thompson   CeedEvalMode        eval_mode = CEED_EVAL_NONE;
7214b3e95d5SJeremy L Thompson   CeedElemRestriction elem_rstr;
7224b3e95d5SJeremy L Thompson 
7238b97b69aSJeremy L Thompson   // Setup output arrays
7240183ed61SJeremy L Thompson   code << "\n";
7250183ed61SJeremy L Thompson   code << tab << "// -- Output field setup\n";
7264b3e95d5SJeremy L Thompson   for (CeedInt i = 0; i < num_output_fields; i++) {
72759fa3f92SJeremy L Thompson     const char *field_name;
7284b3e95d5SJeremy L Thompson     std::string var_suffix = "_out_" + std::to_string(i);
7294b3e95d5SJeremy L Thompson 
73059fa3f92SJeremy L Thompson     CeedCallBackend(CeedOperatorFieldGetName(op_output_fields[i], &field_name));
7310183ed61SJeremy L Thompson     code << tab << "// ---- Output field " << i << ": " << field_name << "\n";
7324b3e95d5SJeremy L Thompson     CeedCallBackend(CeedQFunctionFieldGetEvalMode(qf_output_fields[i], &eval_mode));
7333a2968d6SJeremy L Thompson     switch (eval_mode) {
7343a2968d6SJeremy L Thompson       case CEED_EVAL_NONE:
7353a2968d6SJeremy L Thompson         if (is_at_points) {
7360183ed61SJeremy L Thompson           code << tab << "CeedScalar r_q" << var_suffix << "[num_comp" << var_suffix << "];\n";
7373a2968d6SJeremy L Thompson         } else {
7380183ed61SJeremy L Thompson           code << tab << "CeedScalar r_q" << var_suffix << "[num_comp" << var_suffix << "*" << (is_all_tensor && (max_dim >= 3) ? Q_name : "1")
73974398b5aSJeremy L Thompson                << "];\n";
7404b3e95d5SJeremy L Thompson         }
7413a2968d6SJeremy L Thompson         break;
7423a2968d6SJeremy L Thompson       case CEED_EVAL_INTERP:
7433a2968d6SJeremy L Thompson         if (is_at_points) {
7443a2968d6SJeremy L Thompson           // Accumulator for point data
7450183ed61SJeremy L Thompson           code << tab << "CeedScalar r_c" << var_suffix << "[num_comp" << var_suffix << "*" << (max_dim >= 3 ? Q_name : "1") << "];\n";
7460183ed61SJeremy L Thompson           code << tab << "for (CeedInt i = 0; i < num_comp" << var_suffix << "*" << (max_dim >= 3 ? Q_name : "1") << "; i++) r_c" << var_suffix
747b8245c6cSJeremy L Thompson                << "[i] = 0.0;\n";
7483a2968d6SJeremy L Thompson         } else {
7490183ed61SJeremy L Thompson           code << tab << "CeedScalar r_q" << var_suffix << "[num_comp" << var_suffix << "*" << (is_all_tensor && (max_dim >= 3) ? Q_name : "1")
75074398b5aSJeremy L Thompson                << "];\n";
7513a2968d6SJeremy L Thompson         }
7523a2968d6SJeremy L Thompson         break;
7533a2968d6SJeremy L Thompson       case CEED_EVAL_GRAD:
7543a2968d6SJeremy L Thompson         if (is_at_points) {
7553a2968d6SJeremy L Thompson           // Accumulator for point data
7560183ed61SJeremy L Thompson           code << tab << "CeedScalar r_c" << var_suffix << "[num_comp" << var_suffix << "*" << (max_dim >= 3 ? Q_name : "1") << "];\n";
7570183ed61SJeremy L Thompson           code << tab << "for (CeedInt i = 0; i < num_comp" << var_suffix << "*" << (max_dim >= 3 ? Q_name : "1") << "; i++) r_c" << var_suffix
758b8245c6cSJeremy L Thompson                << "[i] = 0.0;\n";
7593a2968d6SJeremy L Thompson         } else if (use_3d_slices) {
7604b3e95d5SJeremy L Thompson           // Accumulator for gradient slices
7610183ed61SJeremy L Thompson           code << tab << "CeedScalar r_q" << var_suffix << "[num_comp" << var_suffix << "*" << Q_name << "];\n";
7620183ed61SJeremy L Thompson           code << tab << "for (CeedInt i = 0; i < num_comp" << var_suffix << "*" << Q_name << "; i++) r_q" << var_suffix << "[i] = 0.0;\n";
7634b3e95d5SJeremy L Thompson         } else {
7640183ed61SJeremy L Thompson           code << tab << "CeedScalar r_q" << var_suffix << "[num_comp" << var_suffix << "*dim" << var_suffix << "*"
76574398b5aSJeremy L Thompson                << (is_all_tensor && (max_dim >= 3) ? Q_name : "1") << "];\n";
7664b3e95d5SJeremy L Thompson         }
7673a2968d6SJeremy L Thompson         break;
7683a2968d6SJeremy L Thompson       case CEED_EVAL_WEIGHT:
7693a2968d6SJeremy L Thompson         break;
7703a2968d6SJeremy L Thompson         // LCOV_EXCL_START
7713a2968d6SJeremy L Thompson       case CEED_EVAL_DIV:
7723a2968d6SJeremy L Thompson       case CEED_EVAL_CURL:
7733a2968d6SJeremy L Thompson         break;  // TODO: Not implemented
7743a2968d6SJeremy L Thompson                 // LCOV_EXCL_STOP
7754b3e95d5SJeremy L Thompson     }
7764b3e95d5SJeremy L Thompson   }
7774b3e95d5SJeremy L Thompson 
7783a2968d6SJeremy L Thompson   if (is_at_points) {
7793a2968d6SJeremy L Thompson     // We need to handle batches of points
7800183ed61SJeremy L Thompson     code << "\n";
7810183ed61SJeremy L Thompson     code << tab << "// Note: Using batches of points\n";
7820183ed61SJeremy L Thompson     code << tab << "const CeedInt point_loop_bound = (blockDim.x*blockDim.y) * ceil((1.0*max_num_points) / (blockDim.x*blockDim.y));\n\n";
7830183ed61SJeremy L Thompson     code << tab << "#pragma unroll\n";
7840183ed61SJeremy L Thompson     code << tab << "for (CeedInt i = threadIdx.x + threadIdx.y*blockDim.x; i < point_loop_bound; i += blockDim.x*blockDim.y) {\n";
7850183ed61SJeremy L Thompson     tab.push();
7860183ed61SJeremy L Thompson     code << tab << "const CeedInt p = i % max_num_points;\n\n";
7873a2968d6SJeremy L Thompson 
7880183ed61SJeremy L Thompson     code << tab << "// -- Coordinates\n";
7890183ed61SJeremy L Thompson     code << tab << "CeedScalar r_x[max_dim];\n";
7900183ed61SJeremy L Thompson     code << tab << "ReadPoint<max_dim, coords_comp_stride, max_num_points>(data, elem, p, max_num_points, points.indices, points.coords, r_x);\n\n";
7913a2968d6SJeremy L Thompson 
7920183ed61SJeremy L Thompson     code << tab << "// -- Input fields\n";
7933a2968d6SJeremy L Thompson     for (CeedInt i = 0; i < num_input_fields; i++) {
79459fa3f92SJeremy L Thompson       const char *field_name;
7953a2968d6SJeremy L Thompson       std::string var_suffix = "_in_" + std::to_string(i);
796f725b54bSJeremy L Thompson       std::string P_name     = "P_1d" + var_suffix;
7973a2968d6SJeremy L Thompson 
79859fa3f92SJeremy L Thompson       CeedCallBackend(CeedOperatorFieldGetName(op_input_fields[i], &field_name));
7990183ed61SJeremy L Thompson       code << tab << "// ---- Input field " << i << ": " << field_name << "\n";
8003a2968d6SJeremy L Thompson       CeedCallBackend(CeedQFunctionFieldGetEvalMode(qf_input_fields[i], &eval_mode));
8013a2968d6SJeremy L Thompson       // Basis action
8020183ed61SJeremy L Thompson       code << tab << "// EvalMode: " << CeedEvalModes[eval_mode] << "\n";
8033a2968d6SJeremy L Thompson       switch (eval_mode) {
8043a2968d6SJeremy L Thompson         case CEED_EVAL_NONE:
8050183ed61SJeremy L Thompson           code << tab << "CeedScalar r_s" << var_suffix << "[num_comp" << var_suffix << "];\n";
8060183ed61SJeremy L Thompson           code << tab << "ReadPoint<num_comp" << var_suffix << ", comp_stride" << var_suffix
8073a2968d6SJeremy L Thompson                << ", max_num_points>(data, elem, p, max_num_points, indices.inputs[" << i << "], d" << var_suffix << ", r_s" << var_suffix << ");\n";
8083a2968d6SJeremy L Thompson           break;
8093a2968d6SJeremy L Thompson         case CEED_EVAL_INTERP:
8100183ed61SJeremy L Thompson           code << tab << "CeedScalar r_s" << var_suffix << "[num_comp" << var_suffix << "];\n";
8110183ed61SJeremy L Thompson           code << tab << "InterpAtPoints" << max_dim << "d<num_comp" << var_suffix << ", max_num_points, " << P_name << ", " << Q_name
81274398b5aSJeremy L Thompson                << ">(data, i, r_c" << var_suffix << ", r_x, r_s" << var_suffix << ");\n";
8133a2968d6SJeremy L Thompson           break;
8143a2968d6SJeremy L Thompson         case CEED_EVAL_GRAD:
8150183ed61SJeremy L Thompson           code << tab << "CeedScalar r_s" << var_suffix << "[num_comp" << var_suffix << "*dim" << var_suffix << "];\n";
8160183ed61SJeremy L Thompson           code << tab << "GradAtPoints" << max_dim << "d<num_comp" << var_suffix << ", max_num_points, " << P_name << ", " << Q_name
81774398b5aSJeremy L Thompson                << ">(data, i, r_c" << var_suffix << ", r_x, r_s" << var_suffix << ");\n";
8183a2968d6SJeremy L Thompson           break;
8193a2968d6SJeremy L Thompson         case CEED_EVAL_WEIGHT:
8200183ed61SJeremy L Thompson           code << tab << "CeedScalar r_s" << var_suffix << "[1];\n";
8210183ed61SJeremy L Thompson           code << tab << "r_s" << var_suffix << "[0] = 1.0;\n";
8223a2968d6SJeremy L Thompson           break;
8233a2968d6SJeremy L Thompson           // LCOV_EXCL_START
8243a2968d6SJeremy L Thompson         case CEED_EVAL_DIV:
8253a2968d6SJeremy L Thompson         case CEED_EVAL_CURL:
8263a2968d6SJeremy L Thompson           break;  // TODO: Not implemented
8273a2968d6SJeremy L Thompson                   // LCOV_EXCL_STOP
8283a2968d6SJeremy L Thompson       }
8293a2968d6SJeremy L Thompson     }
8300183ed61SJeremy L Thompson     code << "\n";
8310183ed61SJeremy L Thompson     code << tab << "// -- Output fields\n";
8323a2968d6SJeremy L Thompson     for (CeedInt i = 0; i < num_output_fields; i++) {
83359fa3f92SJeremy L Thompson       const char *field_name;
8343a2968d6SJeremy L Thompson       std::string var_suffix = "_out_" + std::to_string(i);
8353a2968d6SJeremy L Thompson 
83659fa3f92SJeremy L Thompson       CeedCallBackend(CeedOperatorFieldGetName(op_output_fields[i], &field_name));
8370183ed61SJeremy L Thompson       code << tab << "// ---- Output field " << i << ": " << field_name << "\n";
8383a2968d6SJeremy L Thompson       CeedCallBackend(CeedQFunctionFieldGetEvalMode(qf_output_fields[i], &eval_mode));
8393a2968d6SJeremy L Thompson       // Basis action
8403a2968d6SJeremy L Thompson       switch (eval_mode) {
8413a2968d6SJeremy L Thompson         case CEED_EVAL_NONE:
8420183ed61SJeremy L Thompson           code << tab << "CeedScalar r_s" << var_suffix << "[num_comp" << var_suffix << "];\n";
8433a2968d6SJeremy L Thompson           break;
8443a2968d6SJeremy L Thompson         case CEED_EVAL_INTERP:
8450183ed61SJeremy L Thompson           code << tab << "CeedScalar r_s" << var_suffix << "[num_comp" << var_suffix << "];\n";
8463a2968d6SJeremy L Thompson           break;
8473a2968d6SJeremy L Thompson         case CEED_EVAL_GRAD:
8480183ed61SJeremy L Thompson           code << tab << "CeedScalar r_s" << var_suffix << "[num_comp" << var_suffix << "*dim" << var_suffix << "];\n";
8493a2968d6SJeremy L Thompson           break;
8503a2968d6SJeremy L Thompson           // LCOV_EXCL_START
8513a2968d6SJeremy L Thompson         case CEED_EVAL_WEIGHT:
8523a2968d6SJeremy L Thompson           break;  // Should not occur
8533a2968d6SJeremy L Thompson         case CEED_EVAL_DIV:
8543a2968d6SJeremy L Thompson         case CEED_EVAL_CURL:
8553a2968d6SJeremy L Thompson           break;  // TODO: Not implemented
8563a2968d6SJeremy L Thompson                   // LCOV_EXCL_STOP
8573a2968d6SJeremy L Thompson       }
8583a2968d6SJeremy L Thompson     }
8593a2968d6SJeremy L Thompson 
8603a2968d6SJeremy L Thompson   } else if (use_3d_slices) {
8614b3e95d5SJeremy L Thompson     // We treat quadrature points per slice in 3d to save registers
8620183ed61SJeremy L Thompson     code << "\n";
8630183ed61SJeremy L Thompson     code << tab << "// Note: Using planes of 3D elements\n";
8640183ed61SJeremy L Thompson     code << tab << "#pragma unroll\n";
8650183ed61SJeremy L Thompson     code << tab << "for (CeedInt q = 0; q < " << Q_name << "; q++) {\n";
8660183ed61SJeremy L Thompson     tab.push();
8670183ed61SJeremy L Thompson     code << tab << "// -- Input fields\n";
8684b3e95d5SJeremy L Thompson     for (CeedInt i = 0; i < num_input_fields; i++) {
86959fa3f92SJeremy L Thompson       const char *field_name;
8704b3e95d5SJeremy L Thompson       std::string var_suffix = "_in_" + std::to_string(i);
8714b3e95d5SJeremy L Thompson 
87259fa3f92SJeremy L Thompson       CeedCallBackend(CeedOperatorFieldGetName(op_input_fields[i], &field_name));
8730183ed61SJeremy L Thompson       code << tab << "// ---- Input field " << i << ": " << field_name << "\n";
8744b3e95d5SJeremy L Thompson       CeedCallBackend(CeedQFunctionFieldGetEvalMode(qf_input_fields[i], &eval_mode));
8754b3e95d5SJeremy L Thompson       // Basis action
8760183ed61SJeremy L Thompson       code << tab << "// EvalMode: " << CeedEvalModes[eval_mode] << "\n";
8774b3e95d5SJeremy L Thompson       switch (eval_mode) {
8784b3e95d5SJeremy L Thompson         case CEED_EVAL_NONE:
8794b3e95d5SJeremy L Thompson           bool is_strided;
8804b3e95d5SJeremy L Thompson 
8810183ed61SJeremy L Thompson           code << tab << "CeedScalar r_s" << var_suffix << "[num_comp" << var_suffix << "];\n";
8824b3e95d5SJeremy L Thompson 
8834b3e95d5SJeremy L Thompson           CeedCallBackend(CeedOperatorFieldGetElemRestriction(op_input_fields[i], &elem_rstr));
8844b3e95d5SJeremy L Thompson           CeedCallBackend(CeedElemRestrictionIsStrided(elem_rstr, &is_strided));
8854b3e95d5SJeremy L Thompson           if (is_strided) {
8864b3e95d5SJeremy L Thompson             bool    has_backend_strides;
8874b3e95d5SJeremy L Thompson             CeedInt num_elem, elem_size;
8884b3e95d5SJeremy L Thompson 
8894b3e95d5SJeremy L Thompson             CeedCallBackend(CeedElemRestrictionGetElementSize(elem_rstr, &elem_size));
8904b3e95d5SJeremy L Thompson             CeedCallBackend(CeedElemRestrictionHasBackendStrides(elem_rstr, &has_backend_strides));
8914b3e95d5SJeremy L Thompson             CeedCallBackend(CeedElemRestrictionGetNumElements(elem_rstr, &num_elem));
8924b3e95d5SJeremy L Thompson             CeedInt strides[3] = {1, elem_size * num_elem, elem_size};
8934b3e95d5SJeremy L Thompson 
8944b3e95d5SJeremy L Thompson             if (!has_backend_strides) {
8954b3e95d5SJeremy L Thompson               CeedCallBackend(CeedElemRestrictionGetStrides(elem_rstr, strides));
8964b3e95d5SJeremy L Thompson             }
8970183ed61SJeremy L Thompson             code << tab << "const CeedInt strides" << var_suffix << "_0 = " << strides[0] << ", strides" << var_suffix << "_1 = " << strides[1]
8980183ed61SJeremy L Thompson                  << ", strides" << var_suffix << "_2 = " << strides[2] << ";\n";
8990183ed61SJeremy L Thompson             code << tab << "ReadEVecSliceStrided3d<num_comp" << var_suffix << ", " << Q_name << ", strides" << var_suffix << "_0, strides"
9000183ed61SJeremy L Thompson                  << var_suffix << "_1, strides" << var_suffix << "_2>(data, elem, q, d" << var_suffix << ", r_s" << var_suffix << ");\n";
9014b3e95d5SJeremy L Thompson           } else {
9024b3e95d5SJeremy L Thompson             CeedSize                 l_size = 0;
9034b3e95d5SJeremy L Thompson             CeedInt                  comp_stride;
9044b3e95d5SJeremy L Thompson             CeedElemRestriction_Hip *rstr_data;
9054b3e95d5SJeremy L Thompson 
9064b3e95d5SJeremy L Thompson             CeedCallBackend(CeedElemRestrictionGetLVectorSize(elem_rstr, &l_size));
9070183ed61SJeremy L Thompson             code << tab << "const CeedInt l_size" << var_suffix << " = " << l_size << ";\n";
9084b3e95d5SJeremy L Thompson             CeedCallBackend(CeedElemRestrictionGetCompStride(elem_rstr, &comp_stride));
9090183ed61SJeremy L Thompson             code << tab << "const CeedInt comp_stride" << var_suffix << " = " << comp_stride << ";\n";
9104b3e95d5SJeremy L Thompson             CeedCallBackend(CeedElemRestrictionGetData(elem_rstr, &rstr_data));
9114b3e95d5SJeremy L Thompson             data->indices.inputs[i] = (CeedInt *)rstr_data->d_offsets;
9120183ed61SJeremy L Thompson             code << tab << "ReadEVecSliceStandard3d<num_comp" << var_suffix << ", comp_stride" << var_suffix << ", " << Q_name << ">(data, l_size"
9130183ed61SJeremy L Thompson                  << var_suffix << ", elem, q, indices.inputs[" << i << "], d" << var_suffix << ", r_s" << var_suffix << ");\n";
9144b3e95d5SJeremy L Thompson           }
9159123fb08SJeremy L Thompson           CeedCallBackend(CeedElemRestrictionDestroy(&elem_rstr));
9164b3e95d5SJeremy L Thompson           break;
9174b3e95d5SJeremy L Thompson         case CEED_EVAL_INTERP:
9180183ed61SJeremy L Thompson           code << tab << "CeedScalar r_s" << var_suffix << "[num_comp" << var_suffix << "];\n";
9190183ed61SJeremy L Thompson           code << tab << "for (CeedInt j = 0; j < num_comp" << var_suffix << "; j++) {\n";
9200183ed61SJeremy L Thompson           tab.push();
9210183ed61SJeremy L Thompson           code << tab << "r_s" << var_suffix << "[j] = r_q" << var_suffix << "[q + j*" << Q_name << "];\n";
9220183ed61SJeremy L Thompson           tab.pop();
9230183ed61SJeremy L Thompson           code << tab << "}\n";
9244b3e95d5SJeremy L Thompson           break;
9254b3e95d5SJeremy L Thompson         case CEED_EVAL_GRAD:
9260183ed61SJeremy L Thompson           code << tab << "CeedScalar r_s" << var_suffix << "[num_comp" << var_suffix << "*dim" << var_suffix << "];\n";
9270183ed61SJeremy L Thompson           code << tab << "GradColloSlice3d<num_comp" << var_suffix << ", " << Q_name << ", OP_T_1D>(data, q, r_q" << var_suffix << ", s_G"
9286b92dc4bSJeremy L Thompson                << var_suffix << ", r_s" << var_suffix << ");\n";
9294b3e95d5SJeremy L Thompson           break;
9304b3e95d5SJeremy L Thompson         case CEED_EVAL_WEIGHT:
9310183ed61SJeremy L Thompson           code << tab << "CeedScalar r_s" << var_suffix << "[1];\n";
9320183ed61SJeremy L Thompson           code << tab << "r_s" << var_suffix << "[0] = r_q" << var_suffix << "[q];\n";
9333a2968d6SJeremy L Thompson           break;
9344b3e95d5SJeremy L Thompson           // LCOV_EXCL_START
9354b3e95d5SJeremy L Thompson         case CEED_EVAL_DIV:
9364b3e95d5SJeremy L Thompson         case CEED_EVAL_CURL:
9374b3e95d5SJeremy L Thompson           break;  // TODO: Not implemented
9384b3e95d5SJeremy L Thompson                   // LCOV_EXCL_STOP
9394b3e95d5SJeremy L Thompson       }
9404b3e95d5SJeremy L Thompson     }
9410183ed61SJeremy L Thompson     code << "\n";
9420183ed61SJeremy L Thompson     code << tab << "// -- Output fields\n";
9434b3e95d5SJeremy L Thompson     for (CeedInt i = 0; i < num_output_fields; i++) {
94459fa3f92SJeremy L Thompson       const char *field_name;
9454b3e95d5SJeremy L Thompson       std::string var_suffix = "_out_" + std::to_string(i);
9464b3e95d5SJeremy L Thompson 
94759fa3f92SJeremy L Thompson       CeedCallBackend(CeedOperatorFieldGetName(op_output_fields[i], &field_name));
9480183ed61SJeremy L Thompson       code << tab << "// ---- Output field " << i << ": " << field_name << "\n";
9494b3e95d5SJeremy L Thompson       CeedCallBackend(CeedQFunctionFieldGetEvalMode(qf_output_fields[i], &eval_mode));
9504b3e95d5SJeremy L Thompson       // Basis action
9514b3e95d5SJeremy L Thompson       switch (eval_mode) {
9524b3e95d5SJeremy L Thompson         case CEED_EVAL_NONE:
9530183ed61SJeremy L Thompson           code << tab << "CeedScalar r_s" << var_suffix << "[num_comp" << var_suffix << "];\n";
9543a2968d6SJeremy L Thompson           break;
9554b3e95d5SJeremy L Thompson         case CEED_EVAL_INTERP:
9560183ed61SJeremy L Thompson           code << tab << "CeedScalar r_s" << var_suffix << "[num_comp" << var_suffix << "];\n";
9574b3e95d5SJeremy L Thompson           break;
9584b3e95d5SJeremy L Thompson         case CEED_EVAL_GRAD:
9590183ed61SJeremy L Thompson           code << tab << "CeedScalar r_s" << var_suffix << "[num_comp" << var_suffix << "*dim" << var_suffix << "];\n";
9604b3e95d5SJeremy L Thompson           break;
9614b3e95d5SJeremy L Thompson           // LCOV_EXCL_START
9624b3e95d5SJeremy L Thompson         case CEED_EVAL_WEIGHT:
9634b3e95d5SJeremy L Thompson           break;  // Should not occur
9644b3e95d5SJeremy L Thompson         case CEED_EVAL_DIV:
9654b3e95d5SJeremy L Thompson         case CEED_EVAL_CURL:
9664b3e95d5SJeremy L Thompson           break;  // TODO: Not implemented
9674b3e95d5SJeremy L Thompson                   // LCOV_EXCL_STOP
9684b3e95d5SJeremy L Thompson       }
9694b3e95d5SJeremy L Thompson     }
9704b3e95d5SJeremy L Thompson   } else {
9710183ed61SJeremy L Thompson     code << "\n";
9720183ed61SJeremy L Thompson     code << tab << "// Note: Using full elements\n";
9730183ed61SJeremy L Thompson     code << tab << "{\n";
9740183ed61SJeremy L Thompson     tab.push();
9750183ed61SJeremy L Thompson     code << tab << "// -- Input fields\n";
9764b3e95d5SJeremy L Thompson     for (CeedInt i = 0; i < num_input_fields; i++) {
97759fa3f92SJeremy L Thompson       const char *field_name;
97859fa3f92SJeremy L Thompson 
97959fa3f92SJeremy L Thompson       CeedCallBackend(CeedOperatorFieldGetName(op_input_fields[i], &field_name));
9800183ed61SJeremy L Thompson       code << tab << "// ---- Input field " << i << ": " << field_name << "\n";
9810183ed61SJeremy L Thompson       code << tab << "CeedScalar *r_s_in_" << i << " = r_q_in_" << i << ";\n";
9824b3e95d5SJeremy L Thompson     }
9830183ed61SJeremy L Thompson     code << tab << "// -- Output fields\n";
9844b3e95d5SJeremy L Thompson     for (CeedInt i = 0; i < num_output_fields; i++) {
98559fa3f92SJeremy L Thompson       const char *field_name;
98659fa3f92SJeremy L Thompson 
98759fa3f92SJeremy L Thompson       CeedCallBackend(CeedOperatorFieldGetName(op_output_fields[i], &field_name));
9880183ed61SJeremy L Thompson       code << tab << "// ---- Output field " << i << ": " << field_name << "\n";
9890183ed61SJeremy L Thompson       code << tab << "CeedScalar *r_s_out_" << i << " = r_q_out_" << i << ";\n";
9904b3e95d5SJeremy L Thompson     }
9914b3e95d5SJeremy L Thompson   }
9924b3e95d5SJeremy L Thompson 
9934b3e95d5SJeremy L Thompson   // Input and output buffers
9940183ed61SJeremy L Thompson   code << "\n";
9950183ed61SJeremy L Thompson   code << tab << "// -- QFunction inputs and outputs\n";
9960183ed61SJeremy L Thompson   code << tab << "// ---- Inputs\n";
9970183ed61SJeremy L Thompson   code << tab << "CeedScalar *inputs[" << CeedIntMax(num_input_fields, 1) << "];\n";
9984b3e95d5SJeremy L Thompson   for (CeedInt i = 0; i < num_input_fields; i++) {
99959fa3f92SJeremy L Thompson     const char *field_name;
100059fa3f92SJeremy L Thompson 
100159fa3f92SJeremy L Thompson     CeedCallBackend(CeedOperatorFieldGetName(op_input_fields[i], &field_name));
10020183ed61SJeremy L Thompson     code << tab << "// ------ Input field " << i << ": " << field_name << "\n";
10030183ed61SJeremy L Thompson     code << tab << "inputs[" << i << "] = r_s_in_" << i << ";\n";
10044b3e95d5SJeremy L Thompson   }
10050183ed61SJeremy L Thompson   code << tab << "// ---- Outputs\n";
10060183ed61SJeremy L Thompson   code << tab << "CeedScalar *outputs[" << CeedIntMax(num_output_fields, 1) << "];\n";
10074b3e95d5SJeremy L Thompson   for (CeedInt i = 0; i < num_output_fields; i++) {
100859fa3f92SJeremy L Thompson     const char *field_name;
100959fa3f92SJeremy L Thompson 
101059fa3f92SJeremy L Thompson     CeedCallBackend(CeedOperatorFieldGetName(op_output_fields[i], &field_name));
10110183ed61SJeremy L Thompson     code << tab << "// ------ Output field " << i << ": " << field_name << "\n";
10120183ed61SJeremy L Thompson     code << tab << "outputs[" << i << "] = r_s_out_" << i << ";\n";
10134b3e95d5SJeremy L Thompson   }
10144b3e95d5SJeremy L Thompson 
10154b3e95d5SJeremy L Thompson   // Apply QFunction
10160183ed61SJeremy L Thompson   code << "\n";
10170183ed61SJeremy L Thompson   code << tab << "// -- Apply QFunction\n";
10180183ed61SJeremy L Thompson   code << tab << "" << qfunction_name << "(ctx, ";
101974398b5aSJeremy L Thompson   if (max_dim != 3 || is_at_points || use_3d_slices || !is_all_tensor) {
10204b3e95d5SJeremy L Thompson     code << "1";
10214b3e95d5SJeremy L Thompson   } else {
10229123fb08SJeremy L Thompson     code << Q_name;
10234b3e95d5SJeremy L Thompson   }
10244b3e95d5SJeremy L Thompson   code << ", inputs, outputs);\n";
10254b3e95d5SJeremy L Thompson 
10263a2968d6SJeremy L Thompson   if (is_at_points) {
10273a2968d6SJeremy L Thompson     // Map back to coefficients
10280183ed61SJeremy L Thompson     code << "\n";
10290183ed61SJeremy L Thompson     code << tab << "// -- Output fields\n";
10303a2968d6SJeremy L Thompson     for (CeedInt i = 0; i < num_output_fields; i++) {
103159fa3f92SJeremy L Thompson       const char *field_name;
10323a2968d6SJeremy L Thompson       std::string var_suffix = "_out_" + std::to_string(i);
10333a2968d6SJeremy L Thompson       std::string P_name     = "P_1d" + var_suffix;
10343a2968d6SJeremy L Thompson 
103559fa3f92SJeremy L Thompson       CeedCallBackend(CeedOperatorFieldGetName(op_output_fields[i], &field_name));
10360183ed61SJeremy L Thompson       code << tab << "// ---- Output field " << i << ": " << field_name << "\n";
10373a2968d6SJeremy L Thompson       CeedCallBackend(CeedQFunctionFieldGetEvalMode(qf_output_fields[i], &eval_mode));
10383a2968d6SJeremy L Thompson       // Basis action
10390183ed61SJeremy L Thompson       code << tab << "// EvalMode: " << CeedEvalModes[eval_mode] << "\n";
10403a2968d6SJeremy L Thompson       switch (eval_mode) {
10413a2968d6SJeremy L Thompson         case CEED_EVAL_NONE: {
10423a2968d6SJeremy L Thompson           CeedInt             comp_stride;
10433a2968d6SJeremy L Thompson           CeedElemRestriction elem_rstr;
10443a2968d6SJeremy L Thompson 
10453a2968d6SJeremy L Thompson           CeedCallBackend(CeedOperatorFieldGetElemRestriction(op_output_fields[i], &elem_rstr));
10463a2968d6SJeremy L Thompson           CeedCallBackend(CeedElemRestrictionGetCompStride(elem_rstr, &comp_stride));
10473a2968d6SJeremy L Thompson           CeedCallBackend(CeedElemRestrictionDestroy(&elem_rstr));
10480183ed61SJeremy L Thompson           code << tab << "const CeedInt comp_stride" << var_suffix << " = " << comp_stride << ";\n";
10490183ed61SJeremy L Thompson           code << tab << "WritePoint<num_comp" << var_suffix << ", comp_stride" << var_suffix
10503a2968d6SJeremy L Thompson                << ", max_num_points>(data, elem, i, points.num_per_elem[elem], indices.outputs[" << i << "]"
10513a2968d6SJeremy L Thompson                << ", r_s" << var_suffix << ", d" << var_suffix << ");\n";
10523a2968d6SJeremy L Thompson           break;
10533a2968d6SJeremy L Thompson         }
10543a2968d6SJeremy L Thompson         case CEED_EVAL_INTERP:
10550183ed61SJeremy L Thompson           code << tab << "if (i >= points.num_per_elem[elem]) {\n";
10560183ed61SJeremy L Thompson           tab.push();
10570183ed61SJeremy L Thompson           code << tab << "for (CeedInt j = 0; j < num_comp" << var_suffix << "; j++) r_s" << var_suffix << "[j] = 0.0;\n";
10580183ed61SJeremy L Thompson           tab.pop();
10590183ed61SJeremy L Thompson           code << tab << "}\n";
10600183ed61SJeremy L Thompson           code << tab << "InterpTransposeAtPoints" << max_dim << "d<num_comp" << var_suffix << ", max_num_points, " << P_name << ", " << Q_name
1061f725b54bSJeremy L Thompson                << ">(data, i, r_s" << var_suffix << ", r_x, r_c" << var_suffix << ");\n";
10623a2968d6SJeremy L Thompson           break;
10633a2968d6SJeremy L Thompson         case CEED_EVAL_GRAD:
10640183ed61SJeremy L Thompson           code << tab << "if (i >= points.num_per_elem[elem]) {\n";
10650183ed61SJeremy L Thompson           tab.push();
10660183ed61SJeremy L Thompson           code << tab << "for (CeedInt j = 0; j < num_comp" << var_suffix << "*dim" << var_suffix << "; j++) r_s" << var_suffix << "[j] = 0.0;\n";
10670183ed61SJeremy L Thompson           tab.pop();
10680183ed61SJeremy L Thompson           code << tab << "}\n";
10690183ed61SJeremy L Thompson           code << tab << "GradTransposeAtPoints" << max_dim << "d<num_comp" << var_suffix << ", max_num_points, " << P_name << ", " << Q_name
1070f725b54bSJeremy L Thompson                << ">(data, i, r_s" << var_suffix << ", r_x, r_c" << var_suffix << ");\n";
10713a2968d6SJeremy L Thompson           break;
10723a2968d6SJeremy L Thompson           // LCOV_EXCL_START
10733a2968d6SJeremy L Thompson         case CEED_EVAL_WEIGHT:
10743a2968d6SJeremy L Thompson           break;  // Should not occur
10753a2968d6SJeremy L Thompson         case CEED_EVAL_DIV:
10763a2968d6SJeremy L Thompson         case CEED_EVAL_CURL:
10773a2968d6SJeremy L Thompson           break;  // TODO: Not implemented
10783a2968d6SJeremy L Thompson                   // LCOV_EXCL_STOP
10793a2968d6SJeremy L Thompson       }
10803a2968d6SJeremy L Thompson     }
10813a2968d6SJeremy L Thompson   } else if (use_3d_slices) {
10824b3e95d5SJeremy L Thompson     // Copy or apply transpose grad, if needed
10830183ed61SJeremy L Thompson     code << "\n";
10840183ed61SJeremy L Thompson     code << tab << "// -- Output fields\n";
10854b3e95d5SJeremy L Thompson     for (CeedInt i = 0; i < num_output_fields; i++) {
108659fa3f92SJeremy L Thompson       const char *field_name;
10874b3e95d5SJeremy L Thompson       std::string var_suffix = "_out_" + std::to_string(i);
10884b3e95d5SJeremy L Thompson       std::string P_name     = "P_1d" + var_suffix;
10894b3e95d5SJeremy L Thompson 
109059fa3f92SJeremy L Thompson       CeedCallBackend(CeedOperatorFieldGetName(op_output_fields[i], &field_name));
10910183ed61SJeremy L Thompson       code << tab << "// ---- Output field " << i << ": " << field_name << "\n";
10924b3e95d5SJeremy L Thompson       CeedCallBackend(CeedQFunctionFieldGetEvalMode(qf_output_fields[i], &eval_mode));
10934b3e95d5SJeremy L Thompson       // Basis action
10940183ed61SJeremy L Thompson       code << tab << "// EvalMode: " << CeedEvalModes[eval_mode] << "\n";
10954b3e95d5SJeremy L Thompson       switch (eval_mode) {
10964b3e95d5SJeremy L Thompson         case CEED_EVAL_NONE:
10970183ed61SJeremy L Thompson           code << tab << "for (CeedInt j = 0; j < num_comp" << var_suffix << " ; j++) {\n";
10980183ed61SJeremy L Thompson           tab.push();
10990183ed61SJeremy L Thompson           code << tab << "r_q" << var_suffix << "[q + j*" << Q_name << "] = r_s" << var_suffix << "[j];\n";
11000183ed61SJeremy L Thompson           tab.pop();
11010183ed61SJeremy L Thompson           code << tab << "}\n";
11023a2968d6SJeremy L Thompson           break;
11034b3e95d5SJeremy L Thompson         case CEED_EVAL_INTERP:
11040183ed61SJeremy L Thompson           code << tab << "for (CeedInt j = 0; j < num_comp" << var_suffix << " ; j++) {\n";
11050183ed61SJeremy L Thompson           tab.push();
11060183ed61SJeremy L Thompson           code << tab << "r_q" << var_suffix << "[q + j*" << Q_name << "] = r_s" << var_suffix << "[j];\n";
11070183ed61SJeremy L Thompson           tab.pop();
11080183ed61SJeremy L Thompson           code << tab << "}\n";
11094b3e95d5SJeremy L Thompson           break;
11104b3e95d5SJeremy L Thompson         case CEED_EVAL_GRAD:
11110183ed61SJeremy L Thompson           code << tab << "GradColloSliceTranspose3d<num_comp" << var_suffix << ", " << Q_name << ", OP_T_1D>(data, q, r_s" << var_suffix << ", s_G"
1112f815fac9SJeremy L Thompson                << var_suffix << ", r_q" << var_suffix << ");\n";
11134b3e95d5SJeremy L Thompson           break;
11144b3e95d5SJeremy L Thompson           // LCOV_EXCL_START
11154b3e95d5SJeremy L Thompson         case CEED_EVAL_WEIGHT:
11164b3e95d5SJeremy L Thompson           break;  // Should not occur
11174b3e95d5SJeremy L Thompson         case CEED_EVAL_DIV:
11184b3e95d5SJeremy L Thompson         case CEED_EVAL_CURL:
11194b3e95d5SJeremy L Thompson           break;  // TODO: Not implemented
11204b3e95d5SJeremy L Thompson                   // LCOV_EXCL_STOP
11214b3e95d5SJeremy L Thompson       }
11224b3e95d5SJeremy L Thompson     }
11234b3e95d5SJeremy L Thompson   }
11240183ed61SJeremy L Thompson   tab.pop();
11250183ed61SJeremy L Thompson   code << tab << "}\n";
11264b3e95d5SJeremy L Thompson   return CEED_ERROR_SUCCESS;
11274b3e95d5SJeremy L Thompson }
11284b3e95d5SJeremy L Thompson 
11294b3e95d5SJeremy L Thompson //------------------------------------------------------------------------------
11309e201c85SYohann // Build single operator kernel
11317d8d0e25Snbeams //------------------------------------------------------------------------------
11328d12f40eSJeremy L Thompson extern "C" int CeedOperatorBuildKernel_Hip_gen(CeedOperator op, bool *is_good_build) {
113374398b5aSJeremy L Thompson   bool                   is_all_tensor = true, is_all_nontensor = true, is_at_points = false, use_3d_slices = false;
11347d8d0e25Snbeams   Ceed                   ceed;
1135efa41df3SJeremy L Thompson   CeedInt                Q = 0, Q_1d = 0, num_input_fields, num_output_fields, max_dim = 1, max_num_points = 0, coords_comp_stride = 0;
1136b7453713SJeremy L Thompson   CeedQFunctionField    *qf_input_fields, *qf_output_fields;
1137b7453713SJeremy L Thompson   CeedQFunction_Hip_gen *qf_data;
1138b7453713SJeremy L Thompson   CeedQFunction          qf;
1139b7453713SJeremy L Thompson   CeedOperatorField     *op_input_fields, *op_output_fields;
1140b7453713SJeremy L Thompson   CeedOperator_Hip_gen  *data;
11414b3e95d5SJeremy L Thompson   std::ostringstream     code;
11420183ed61SJeremy L Thompson   Tab                    tab;
11434b3e95d5SJeremy L Thompson 
11448d12f40eSJeremy L Thompson   CeedCallBackend(CeedOperatorGetData(op, &data));
11454b3e95d5SJeremy L Thompson   {
11464b3e95d5SJeremy L Thompson     bool is_setup_done;
1147b7453713SJeremy L Thompson 
1148b7453713SJeremy L Thompson     CeedCallBackend(CeedOperatorIsSetupDone(op, &is_setup_done));
11498d12f40eSJeremy L Thompson     if (is_setup_done) {
11508d12f40eSJeremy L Thompson       *is_good_build = !data->use_fallback;
11518d12f40eSJeremy L Thompson       return CEED_ERROR_SUCCESS;
11528d12f40eSJeremy L Thompson     }
11534b3e95d5SJeremy L Thompson   }
1154b7453713SJeremy L Thompson 
11558d12f40eSJeremy L Thompson   // Check field compatibility
11568d12f40eSJeremy L Thompson   CeedCallBackend(CeedOperatorGetFields(op, &num_input_fields, &op_input_fields, &num_output_fields, &op_output_fields));
11578d12f40eSJeremy L Thompson   {
115874398b5aSJeremy L Thompson     bool has_shared_bases = true;
11598d12f40eSJeremy L Thompson 
11608d12f40eSJeremy L Thompson     for (CeedInt i = 0; i < num_input_fields; i++) {
11618d12f40eSJeremy L Thompson       CeedBasis basis;
11628d12f40eSJeremy L Thompson 
11638d12f40eSJeremy L Thompson       CeedCallBackend(CeedOperatorFieldGetBasis(op_input_fields[i], &basis));
11648d12f40eSJeremy L Thompson       if (basis != CEED_BASIS_NONE) {
11658d12f40eSJeremy L Thompson         bool        is_tensor = true;
11668d12f40eSJeremy L Thompson         const char *resource;
11678d12f40eSJeremy L Thompson         char       *resource_root;
11688d12f40eSJeremy L Thompson         Ceed        basis_ceed;
11698d12f40eSJeremy L Thompson 
11708d12f40eSJeremy L Thompson         CeedCallBackend(CeedBasisIsTensor(basis, &is_tensor));
1171c9192acaSJeremy L Thompson         is_all_tensor    = is_all_tensor && is_tensor;
1172c9192acaSJeremy L Thompson         is_all_nontensor = is_all_nontensor && !is_tensor;
11738d12f40eSJeremy L Thompson         CeedCallBackend(CeedBasisGetCeed(basis, &basis_ceed));
11748d12f40eSJeremy L Thompson         CeedCallBackend(CeedGetResource(basis_ceed, &resource));
11758d12f40eSJeremy L Thompson         CeedCallBackend(CeedGetResourceRoot(basis_ceed, resource, ":", &resource_root));
1176c9192acaSJeremy L Thompson         has_shared_bases = has_shared_bases && !strcmp(resource_root, "/gpu/hip/shared");
11778d12f40eSJeremy L Thompson         CeedCallBackend(CeedFree(&resource_root));
11788d12f40eSJeremy L Thompson         CeedCallBackend(CeedDestroy(&basis_ceed));
11798d12f40eSJeremy L Thompson       }
11808d12f40eSJeremy L Thompson       CeedCallBackend(CeedBasisDestroy(&basis));
11818d12f40eSJeremy L Thompson     }
11828d12f40eSJeremy L Thompson 
11838d12f40eSJeremy L Thompson     for (CeedInt i = 0; i < num_output_fields; i++) {
11848d12f40eSJeremy L Thompson       CeedBasis basis;
11858d12f40eSJeremy L Thompson 
11868d12f40eSJeremy L Thompson       CeedCallBackend(CeedOperatorFieldGetBasis(op_output_fields[i], &basis));
11878d12f40eSJeremy L Thompson       if (basis != CEED_BASIS_NONE) {
11888d12f40eSJeremy L Thompson         bool        is_tensor = true;
11898d12f40eSJeremy L Thompson         const char *resource;
11908d12f40eSJeremy L Thompson         char       *resource_root;
11918d12f40eSJeremy L Thompson         Ceed        basis_ceed;
11928d12f40eSJeremy L Thompson 
11938d12f40eSJeremy L Thompson         CeedCallBackend(CeedBasisIsTensor(basis, &is_tensor));
1194c9192acaSJeremy L Thompson         is_all_tensor    = is_all_tensor && is_tensor;
1195c9192acaSJeremy L Thompson         is_all_nontensor = is_all_nontensor && !is_tensor;
11968d12f40eSJeremy L Thompson 
11978d12f40eSJeremy L Thompson         CeedCallBackend(CeedBasisGetCeed(basis, &basis_ceed));
11988d12f40eSJeremy L Thompson         CeedCallBackend(CeedGetResource(basis_ceed, &resource));
11998d12f40eSJeremy L Thompson         CeedCallBackend(CeedGetResourceRoot(basis_ceed, resource, ":", &resource_root));
1200c9192acaSJeremy L Thompson         has_shared_bases = has_shared_bases && !strcmp(resource_root, "/gpu/hip/shared");
12018d12f40eSJeremy L Thompson         CeedCallBackend(CeedFree(&resource_root));
12028d12f40eSJeremy L Thompson         CeedCallBackend(CeedDestroy(&basis_ceed));
12038d12f40eSJeremy L Thompson       }
12048d12f40eSJeremy L Thompson       CeedCallBackend(CeedBasisDestroy(&basis));
12058d12f40eSJeremy L Thompson     }
12068d12f40eSJeremy L Thompson     // -- Fallback to ref if not all bases are shared
120774398b5aSJeremy L Thompson     if (!has_shared_bases) {
12088d12f40eSJeremy L Thompson       *is_good_build = false;
12098d12f40eSJeremy L Thompson       return CEED_ERROR_SUCCESS;
12108d12f40eSJeremy L Thompson     }
12118d12f40eSJeremy L Thompson   }
1212b7453713SJeremy L Thompson   CeedCallBackend(CeedOperatorGetCeed(op, &ceed));
1213b7453713SJeremy L Thompson   CeedCallBackend(CeedOperatorGetQFunction(op, &qf));
1214b7453713SJeremy L Thompson   CeedCallBackend(CeedQFunctionGetData(qf, &qf_data));
1215b7453713SJeremy L Thompson   CeedCallBackend(CeedQFunctionGetFields(qf, NULL, &qf_input_fields, NULL, &qf_output_fields));
12167d8d0e25Snbeams 
12174b3e95d5SJeremy L Thompson   // Get operator data
12183a2968d6SJeremy L Thompson   CeedCallBackend(CeedOperatorIsAtPoints(op, &is_at_points));
121974398b5aSJeremy L Thompson   {
1220efa41df3SJeremy L Thompson     CeedInt max_P = 0, max_P_1d = 0;
122174398b5aSJeremy L Thompson 
12224b3e95d5SJeremy L Thompson     CeedCallBackend(CeedOperatorBuildKernelData_Hip_gen(ceed, num_input_fields, op_input_fields, qf_input_fields, num_output_fields, op_output_fields,
122374398b5aSJeremy L Thompson                                                         qf_output_fields, &max_P, &max_P_1d, &Q, &Q_1d, &max_dim, &is_all_tensor, &use_3d_slices));
122474398b5aSJeremy L Thompson     data->max_P_1d = is_all_tensor ? max_P_1d : max_P;
122574398b5aSJeremy L Thompson   }
122674398b5aSJeremy L Thompson   if (max_dim == 0) max_dim = 1;
122774398b5aSJeremy L Thompson   data->dim = max_dim;
12283a2968d6SJeremy L Thompson   if (is_at_points) {
12293a2968d6SJeremy L Thompson     CeedElemRestriction_Hip *rstr_data;
12303a2968d6SJeremy L Thompson     CeedElemRestriction      rstr_points = NULL;
12314b3e95d5SJeremy L Thompson 
12323a2968d6SJeremy L Thompson     CeedCallBackend(CeedOperatorAtPointsGetPoints(op, &rstr_points, NULL));
12333a2968d6SJeremy L Thompson     CeedCallBackend(CeedElemRestrictionGetMaxPointsInElement(rstr_points, &max_num_points));
12343a2968d6SJeremy L Thompson     CeedCallBackend(CeedElemRestrictionGetCompStride(rstr_points, &coords_comp_stride));
12353a2968d6SJeremy L Thompson     CeedCallBackend(CeedElemRestrictionGetData(rstr_points, &rstr_data));
12363a2968d6SJeremy L Thompson     data->points.indices = (CeedInt *)rstr_data->d_offsets;
12373a2968d6SJeremy L Thompson     CeedCallBackend(CeedElemRestrictionDestroy(&rstr_points));
12383a2968d6SJeremy L Thompson   }
12393a2968d6SJeremy L Thompson   if (is_at_points) use_3d_slices = false;
12403a2968d6SJeremy L Thompson   if (Q_1d == 0) {
12413a2968d6SJeremy L Thompson     if (is_at_points) Q_1d = max_num_points;
12423a2968d6SJeremy L Thompson     else CeedCallBackend(CeedOperatorGetNumQuadraturePoints(op, &Q_1d));
12434b3e95d5SJeremy L Thompson   }
124474398b5aSJeremy L Thompson   if (Q == 0) Q = Q_1d;
124574398b5aSJeremy L Thompson   data->Q    = Q;
12464b3e95d5SJeremy L Thompson   data->Q_1d = Q_1d;
12474b3e95d5SJeremy L Thompson 
12480b454692Sjeremylt   // Check for restriction only identity operator
12494b3e95d5SJeremy L Thompson   {
12504b3e95d5SJeremy L Thompson     bool is_identity_qf;
12514b3e95d5SJeremy L Thompson 
12522b730f8bSJeremy L Thompson     CeedCallBackend(CeedQFunctionIsIdentity(qf, &is_identity_qf));
12530b454692Sjeremylt     if (is_identity_qf) {
12549e201c85SYohann       CeedEvalMode eval_mode_in, eval_mode_out;
1255b7453713SJeremy L Thompson 
12562b730f8bSJeremy L Thompson       CeedCallBackend(CeedQFunctionFieldGetEvalMode(qf_input_fields[0], &eval_mode_in));
12572b730f8bSJeremy L Thompson       CeedCallBackend(CeedQFunctionFieldGetEvalMode(qf_output_fields[0], &eval_mode_out));
12586574a04fSJeremy L Thompson       CeedCheck(eval_mode_in != CEED_EVAL_NONE || eval_mode_out != CEED_EVAL_NONE, ceed, CEED_ERROR_BACKEND,
12596574a04fSJeremy L Thompson                 "Backend does not implement restriction only identity operators");
12600b454692Sjeremylt     }
12614b3e95d5SJeremy L Thompson   }
1262b2165e7aSSebastian Grimberg 
1263b2165e7aSSebastian Grimberg   // Load basis source files
1264eaf9ad10SZach Atkins   if (!is_all_nontensor) {
12650183ed61SJeremy L Thompson     code << tab << "// Tensor basis source\n";
12660183ed61SJeremy L Thompson     code << tab << "#include <ceed/jit-source/hip/hip-shared-basis-tensor-templates.h>\n\n";
126774398b5aSJeremy L Thompson   }
126874398b5aSJeremy L Thompson   if (!is_all_tensor) {
12690183ed61SJeremy L Thompson     code << tab << "// Non-tensor basis source\n";
12700183ed61SJeremy L Thompson     code << tab << "#include <ceed/jit-source/hip/hip-shared-basis-nontensor-templates.h>\n\n";
12719123fb08SJeremy L Thompson   }
12729123fb08SJeremy L Thompson   if (is_at_points) {
12730183ed61SJeremy L Thompson     code << tab << "// AtPoints basis source\n";
12740183ed61SJeremy L Thompson     code << tab << "#include <ceed/jit-source/hip/hip-shared-basis-tensor-at-points-templates.h>\n\n";
12759123fb08SJeremy L Thompson   }
127674398b5aSJeremy L Thompson   if (!is_all_tensor && !is_all_nontensor) {
12770183ed61SJeremy L Thompson     code << tab << "// Tensor basis source\n";
12780183ed61SJeremy L Thompson     code << tab << "#include <ceed/jit-source/hip/hip-shared-basis-tensor-flattened-templates.h>\n\n";
127974398b5aSJeremy L Thompson   }
12800183ed61SJeremy L Thompson   code << tab << "// CodeGen operator source\n";
12810183ed61SJeremy L Thompson   code << tab << "#include <ceed/jit-source/hip/hip-gen-templates.h>\n\n";
12827d8d0e25Snbeams 
12834b3e95d5SJeremy L Thompson   // Get QFunction name
12844b3e95d5SJeremy L Thompson   std::string qfunction_name(qf_data->qfunction_name);
12854b3e95d5SJeremy L Thompson   std::string operator_name;
12864b3e95d5SJeremy L Thompson 
128709095acaSJeremy L Thompson   operator_name = "CeedKernelHipGenOperator_" + qfunction_name;
12887d8d0e25Snbeams 
12899e201c85SYohann   // Define CEED_Q_VLA
12900183ed61SJeremy L Thompson   code << "\n" << tab << "#undef CEED_Q_VLA\n";
129174398b5aSJeremy L Thompson   if (max_dim != 3 || is_at_points || use_3d_slices || !is_all_tensor) {
12920183ed61SJeremy L Thompson     code << tab << "#define CEED_Q_VLA 1\n\n";
12939e201c85SYohann   } else {
12940183ed61SJeremy L Thompson     code << tab << "#define CEED_Q_VLA " << Q_1d << "\n\n";
12959e201c85SYohann   }
12969e201c85SYohann 
12974b3e95d5SJeremy L Thompson   // Add user QFunction source
12984b3e95d5SJeremy L Thompson   {
12999c25dd66SJeremy L Thompson     const char *source_path;
13004b3e95d5SJeremy L Thompson 
13019c25dd66SJeremy L Thompson     CeedCallBackend(CeedQFunctionGetSourcePath(qf, &source_path));
13029c25dd66SJeremy L Thompson     CeedCheck(source_path, ceed, CEED_ERROR_UNSUPPORTED, "/gpu/hip/gen backend requires QFunction source code file");
13039c25dd66SJeremy L Thompson 
13040183ed61SJeremy L Thompson     code << tab << "// User QFunction source\n";
13050183ed61SJeremy L Thompson     code << tab << "#include \"" << source_path << "\"\n\n";
13064b3e95d5SJeremy L Thompson   }
13077d8d0e25Snbeams 
13087d8d0e25Snbeams   // Setup
13090183ed61SJeremy L Thompson   code << "\n" << tab << "// -----------------------------------------------------------------------------\n";
13100183ed61SJeremy L Thompson   code << tab << "// Operator Kernel\n";
13110183ed61SJeremy L Thompson   code << tab << "// \n";
13120183ed61SJeremy L Thompson   code << tab << "// d_[in,out]_i:   CeedVector device array\n";
13130183ed61SJeremy L Thompson   code << tab << "// r_[in,out]_e_i: Element vector register\n";
13140183ed61SJeremy L Thompson   code << tab << "// r_[in,out]_q_i: Quadrature space vector register\n";
13150183ed61SJeremy L Thompson   code << tab << "// r_[in,out]_c_i: AtPoints Chebyshev coefficients register\n";
13160183ed61SJeremy L Thompson   code << tab << "// r_[in,out]_s_i: Quadrature space slice vector register\n";
13170183ed61SJeremy L Thompson   code << tab << "// \n";
13180183ed61SJeremy L Thompson   code << tab << "// s_B_[in,out]_i: Interpolation matrix, shared memory\n";
13190183ed61SJeremy L Thompson   code << tab << "// s_G_[in,out]_i: Gradient matrix, shared memory\n";
13200183ed61SJeremy L Thompson   code << tab << "// -----------------------------------------------------------------------------\n";
13210183ed61SJeremy L Thompson   code << tab << "extern \"C\" __launch_bounds__(BLOCK_SIZE)\n";
13222b730f8bSJeremy L Thompson   code << "__global__ void " << operator_name
13233a2968d6SJeremy 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";
13240183ed61SJeremy L Thompson   tab.push();
13254b3e95d5SJeremy L Thompson 
13264b3e95d5SJeremy L Thompson   // Scratch buffers
13279e201c85SYohann   for (CeedInt i = 0; i < num_input_fields; i++) {
13284b3e95d5SJeremy L Thompson     CeedEvalMode eval_mode;
13294b3e95d5SJeremy L Thompson 
13302b730f8bSJeremy L Thompson     CeedCallBackend(CeedQFunctionFieldGetEvalMode(qf_input_fields[i], &eval_mode));
13319e201c85SYohann     if (eval_mode != CEED_EVAL_WEIGHT) {  // Skip CEED_EVAL_WEIGHT
13320183ed61SJeremy L Thompson       code << tab << "const CeedScalar *__restrict__ d_in_" << i << " = fields.inputs[" << i << "];\n";
13337d8d0e25Snbeams     }
13347d8d0e25Snbeams   }
13359e201c85SYohann   for (CeedInt i = 0; i < num_output_fields; i++) {
13360183ed61SJeremy L Thompson     code << tab << "CeedScalar *__restrict__ d_out_" << i << " = fields.outputs[" << i << "];\n";
13377d8d0e25Snbeams   }
13387d8d0e25Snbeams 
13390183ed61SJeremy L Thompson   code << tab << "const CeedInt max_dim = " << max_dim << ";\n";
134074398b5aSJeremy L Thompson   if (!is_all_tensor) {
13410183ed61SJeremy L Thompson     code << tab << "const CeedInt Q = " << Q << ";\n";
134274398b5aSJeremy L Thompson   }
134374398b5aSJeremy L Thompson   if (!is_all_nontensor) {
13440183ed61SJeremy L Thompson     code << tab << "const CeedInt Q_1d = " << Q_1d << ";\n";
134574398b5aSJeremy L Thompson   }
13463a2968d6SJeremy L Thompson   if (is_at_points) {
13470183ed61SJeremy L Thompson     code << tab << "const CeedInt max_num_points = " << max_num_points << ";\n";
13480183ed61SJeremy L Thompson     code << tab << "const CeedInt coords_comp_stride = " << coords_comp_stride << ";\n";
13493a2968d6SJeremy L Thompson   }
13507d8d0e25Snbeams 
13514b3e95d5SJeremy L Thompson   // Shared data
13520183ed61SJeremy L Thompson   code << tab << "extern __shared__ CeedScalar slice[];\n";
13530183ed61SJeremy L Thompson   code << tab << "SharedData_Hip data;\n";
13540183ed61SJeremy L Thompson   code << tab << "data.t_id_x = threadIdx.x;\n";
13550183ed61SJeremy L Thompson   code << tab << "data.t_id_y = threadIdx.y;\n";
13560183ed61SJeremy L Thompson   code << tab << "data.t_id_z = threadIdx.z;\n";
13570183ed61SJeremy L Thompson   code << tab << "data.t_id   = threadIdx.x + threadIdx.y*blockDim.x + threadIdx.z*blockDim.y*blockDim.x;\n";
13580183ed61SJeremy L Thompson   code << tab << "data.slice  = slice + data.t_id_z*OP_T_1D" << ((!is_all_tensor || max_dim == 1) ? "" : "*OP_T_1D") << ";\n";
13597d8d0e25Snbeams 
13609ee499e5SJeremy L Thompson   // -- Determine input mat reuse
136145a787f7SJeremy L Thompson   FieldReuse_Hip input_matrix_reuse[CEED_FIELD_MAX];
13629ee499e5SJeremy L Thompson 
13639ee499e5SJeremy L Thompson   for (CeedInt i = 0; i < num_input_fields; i++) {
136445a787f7SJeremy L Thompson     input_matrix_reuse[i].index = -1;
13659ee499e5SJeremy L Thompson   }
13669ee499e5SJeremy L Thompson   for (CeedInt i = 0; i < num_input_fields; i++) {
136774398b5aSJeremy L Thompson     bool         is_tensor = true;
13689ee499e5SJeremy L Thompson     CeedEvalMode eval_mode_i;
13699ee499e5SJeremy L Thompson     CeedBasis    basis_i;
13709ee499e5SJeremy L Thompson 
13719ee499e5SJeremy L Thompson     CeedCallBackend(CeedQFunctionFieldGetEvalMode(qf_input_fields[i], &eval_mode_i));
13729ee499e5SJeremy L Thompson     if (eval_mode_i == CEED_EVAL_WEIGHT) continue;
13739ee499e5SJeremy L Thompson     CeedCallBackend(CeedOperatorFieldGetBasis(op_input_fields[i], &basis_i));
137474398b5aSJeremy L Thompson     CeedCallBackend(CeedBasisIsTensor(basis_i, &is_tensor));
137545a787f7SJeremy L Thompson     for (CeedInt j = 0; (input_matrix_reuse[i].index == -1) && (j < i); j++) {
13769ee499e5SJeremy L Thompson       CeedEvalMode eval_mode_j;
13779ee499e5SJeremy L Thompson       CeedBasis    basis_j;
13789ee499e5SJeremy L Thompson 
13799ee499e5SJeremy L Thompson       CeedCallBackend(CeedQFunctionFieldGetEvalMode(qf_input_fields[j], &eval_mode_j));
13809ee499e5SJeremy L Thompson       if (eval_mode_j == CEED_EVAL_WEIGHT) continue;
13819ee499e5SJeremy L Thompson       CeedCallBackend(CeedOperatorFieldGetBasis(op_input_fields[j], &basis_j));
13829ee499e5SJeremy L Thompson       if (basis_i == basis_j) {
13839ee499e5SJeremy L Thompson         if (is_tensor) {
138445a787f7SJeremy L Thompson           input_matrix_reuse[i].index     = j;
138545a787f7SJeremy L Thompson           input_matrix_reuse[i].is_input  = true;
138645a787f7SJeremy L Thompson           input_matrix_reuse[i].eval_mode = eval_mode_j;
13879ee499e5SJeremy L Thompson         } else {
13889ee499e5SJeremy L Thompson           // For non-tensor can only re-use with the same eval mode
13899ee499e5SJeremy L Thompson           if (eval_mode_i == eval_mode_j) {
139045a787f7SJeremy L Thompson             input_matrix_reuse[i].index     = j;
139145a787f7SJeremy L Thompson             input_matrix_reuse[i].is_input  = true;
139245a787f7SJeremy L Thompson             input_matrix_reuse[i].eval_mode = eval_mode_j;
13939ee499e5SJeremy L Thompson           }
13949ee499e5SJeremy L Thompson         }
13959ee499e5SJeremy L Thompson       }
13969ee499e5SJeremy L Thompson       CeedCallBackend(CeedBasisDestroy(&basis_j));
13979ee499e5SJeremy L Thompson     }
13989ee499e5SJeremy L Thompson     CeedCallBackend(CeedBasisDestroy(&basis_i));
13999ee499e5SJeremy L Thompson   }
14009ee499e5SJeremy L Thompson 
14019ee499e5SJeremy L Thompson   // -- Determine output mat reuse
140245a787f7SJeremy L Thompson   FieldReuse_Hip output_matrix_reuse[CEED_FIELD_MAX];
14039ee499e5SJeremy L Thompson 
14049ee499e5SJeremy L Thompson   for (CeedInt i = 0; i < num_output_fields; i++) {
140545a787f7SJeremy L Thompson     output_matrix_reuse[i].index = -1;
14069ee499e5SJeremy L Thompson   }
14079ee499e5SJeremy L Thompson   for (CeedInt i = 0; i < num_output_fields; i++) {
140874398b5aSJeremy L Thompson     bool         is_tensor = true;
14099ee499e5SJeremy L Thompson     CeedEvalMode eval_mode_i;
14109ee499e5SJeremy L Thompson     CeedBasis    basis_i;
14119ee499e5SJeremy L Thompson 
14129ee499e5SJeremy L Thompson     CeedCallBackend(CeedQFunctionFieldGetEvalMode(qf_output_fields[i], &eval_mode_i));
14139ee499e5SJeremy L Thompson     CeedCallBackend(CeedOperatorFieldGetBasis(op_output_fields[i], &basis_i));
141445a787f7SJeremy L Thompson     for (CeedInt j = 0; (output_matrix_reuse[i].index == -1) && (j < num_input_fields); j++) {
14159ee499e5SJeremy L Thompson       CeedEvalMode eval_mode_j;
14169ee499e5SJeremy L Thompson       CeedBasis    basis_j;
14179ee499e5SJeremy L Thompson 
14189ee499e5SJeremy L Thompson       CeedCallBackend(CeedQFunctionFieldGetEvalMode(qf_input_fields[j], &eval_mode_j));
14199ee499e5SJeremy L Thompson       if (eval_mode_j == CEED_EVAL_WEIGHT) continue;
14209ee499e5SJeremy L Thompson       CeedCallBackend(CeedOperatorFieldGetBasis(op_input_fields[j], &basis_j));
14219ee499e5SJeremy L Thompson       if (basis_i == basis_j) {
14229ee499e5SJeremy L Thompson         if (is_tensor) {
142345a787f7SJeremy L Thompson           output_matrix_reuse[i].index     = j;
142445a787f7SJeremy L Thompson           output_matrix_reuse[i].is_input  = true;
142545a787f7SJeremy L Thompson           output_matrix_reuse[i].eval_mode = eval_mode_j;
14269ee499e5SJeremy L Thompson         } else {
14279ee499e5SJeremy L Thompson           // For non-tensor can only re-use with the same eval mode
14289ee499e5SJeremy L Thompson           if (eval_mode_i == eval_mode_j) {
142945a787f7SJeremy L Thompson             output_matrix_reuse[i].index     = j;
143045a787f7SJeremy L Thompson             output_matrix_reuse[i].is_input  = true;
143145a787f7SJeremy L Thompson             output_matrix_reuse[i].eval_mode = eval_mode_j;
14329ee499e5SJeremy L Thompson           }
14339ee499e5SJeremy L Thompson         }
14349ee499e5SJeremy L Thompson       }
14359ee499e5SJeremy L Thompson       CeedCallBackend(CeedBasisDestroy(&basis_j));
14369ee499e5SJeremy L Thompson     }
143745a787f7SJeremy L Thompson     for (CeedInt j = 0; (output_matrix_reuse[i].index == -1) && (j < i); j++) {
14389ee499e5SJeremy L Thompson       CeedEvalMode eval_mode_j;
14399ee499e5SJeremy L Thompson       CeedBasis    basis_j;
14409ee499e5SJeremy L Thompson 
14419ee499e5SJeremy L Thompson       CeedCallBackend(CeedQFunctionFieldGetEvalMode(qf_output_fields[j], &eval_mode_j));
14429ee499e5SJeremy L Thompson       if (eval_mode_j == CEED_EVAL_WEIGHT) continue;
14439ee499e5SJeremy L Thompson       CeedCallBackend(CeedOperatorFieldGetBasis(op_output_fields[j], &basis_j));
144474398b5aSJeremy L Thompson       CeedCallBackend(CeedBasisIsTensor(basis_i, &is_tensor));
14459ee499e5SJeremy L Thompson       if (basis_i == basis_j) {
14469ee499e5SJeremy L Thompson         if (is_tensor) {
144745a787f7SJeremy L Thompson           output_matrix_reuse[i].index     = j;
144845a787f7SJeremy L Thompson           output_matrix_reuse[i].is_input  = false;
144945a787f7SJeremy L Thompson           output_matrix_reuse[i].eval_mode = eval_mode_j;
14509ee499e5SJeremy L Thompson         } else {
14519ee499e5SJeremy L Thompson           // For non-tensor can only re-use with the same eval mode
14529ee499e5SJeremy L Thompson           if (eval_mode_i == eval_mode_j) {
145345a787f7SJeremy L Thompson             output_matrix_reuse[i].index     = j;
145445a787f7SJeremy L Thompson             output_matrix_reuse[i].is_input  = false;
145545a787f7SJeremy L Thompson             output_matrix_reuse[i].eval_mode = eval_mode_j;
14569ee499e5SJeremy L Thompson           }
14579ee499e5SJeremy L Thompson         }
14589ee499e5SJeremy L Thompson       }
14599ee499e5SJeremy L Thompson       CeedCallBackend(CeedBasisDestroy(&basis_j));
14609ee499e5SJeremy L Thompson     }
14619ee499e5SJeremy L Thompson     CeedCallBackend(CeedBasisDestroy(&basis_i));
14629ee499e5SJeremy L Thompson   }
14639ee499e5SJeremy L Thompson 
14647d8d0e25Snbeams   // Initialize constants, and matrices B and G
14650183ed61SJeremy L Thompson   code << "\n" << tab << "// Input field constants and basis data\n";
14669e201c85SYohann   for (CeedInt i = 0; i < num_input_fields; i++) {
14670183ed61SJeremy L Thompson     CeedCallBackend(CeedOperatorBuildKernelFieldData_Hip_gen(code, data, tab, i, op_input_fields[i], qf_input_fields[i], input_matrix_reuse[i],
14680183ed61SJeremy L Thompson                                                              max_dim, Q, Q_1d, true, is_all_tensor, is_at_points, use_3d_slices));
14697d8d0e25Snbeams   }
14700183ed61SJeremy L Thompson   code << "\n" << tab << "// Output field constants and basis data\n";
14719e201c85SYohann   for (CeedInt i = 0; i < num_output_fields; i++) {
14720183ed61SJeremy L Thompson     CeedCallBackend(CeedOperatorBuildKernelFieldData_Hip_gen(code, data, tab, i, op_output_fields[i], qf_output_fields[i], output_matrix_reuse[i],
14730183ed61SJeremy L Thompson                                                              max_dim, Q, Q_1d, false, is_all_tensor, is_at_points, use_3d_slices));
14744b3e95d5SJeremy L Thompson   }
14757d8d0e25Snbeams 
14764b3e95d5SJeremy L Thompson   // Loop over all elements
14770183ed61SJeremy L Thompson   code << "\n" << tab << "// Element loop\n";
14780183ed61SJeremy L Thompson   code << tab << "__syncthreads();\n";
14790183ed61SJeremy L Thompson   code << tab << "for (CeedInt elem = blockIdx.x*blockDim.z + threadIdx.z; elem < num_elem; elem += gridDim.x*blockDim.z) {\n";
14800183ed61SJeremy L Thompson   tab.push();
14814b3e95d5SJeremy L Thompson 
1482e93651e5SJeremy L Thompson   // -- Compute minimum buffer space needed
14833a2968d6SJeremy L Thompson   CeedInt max_rstr_buffer_size = 1;
1484e93651e5SJeremy L Thompson 
1485e93651e5SJeremy L Thompson   for (CeedInt i = 0; i < num_input_fields; i++) {
14866de40545SJeremy L Thompson     CeedEvalMode eval_mode;
14876de40545SJeremy L Thompson 
14886de40545SJeremy L Thompson     CeedCallBackend(CeedQFunctionFieldGetEvalMode(qf_input_fields[i], &eval_mode));
14896de40545SJeremy L Thompson     if (eval_mode != CEED_EVAL_NONE && eval_mode != CEED_EVAL_WEIGHT) {
1490a61b1c91SJeremy L Thompson       CeedInt             num_comp;
1491e93651e5SJeremy L Thompson       CeedElemRestriction elem_rstr;
1492e93651e5SJeremy L Thompson 
1493e93651e5SJeremy L Thompson       CeedCallBackend(CeedOperatorFieldGetElemRestriction(op_input_fields[i], &elem_rstr));
1494e93651e5SJeremy L Thompson       CeedCallBackend(CeedElemRestrictionGetNumComponents(elem_rstr, &num_comp));
1495a61b1c91SJeremy L Thompson       max_rstr_buffer_size = CeedIntMax(max_rstr_buffer_size, num_comp * (is_all_tensor && (max_dim >= 3) ? Q_1d : 1));
1496681d0ea7SJeremy L Thompson       CeedCallBackend(CeedElemRestrictionDestroy(&elem_rstr));
1497e93651e5SJeremy L Thompson     }
14986de40545SJeremy L Thompson   }
1499e93651e5SJeremy L Thompson   for (CeedInt i = 0; i < num_output_fields; i++) {
15006de40545SJeremy L Thompson     CeedEvalMode eval_mode;
15016de40545SJeremy L Thompson 
15026de40545SJeremy L Thompson     CeedCallBackend(CeedQFunctionFieldGetEvalMode(qf_output_fields[i], &eval_mode));
15036de40545SJeremy L Thompson     if (eval_mode != CEED_EVAL_NONE) {
1504a61b1c91SJeremy L Thompson       CeedInt             num_comp;
1505e93651e5SJeremy L Thompson       CeedElemRestriction elem_rstr;
1506e93651e5SJeremy L Thompson 
1507e93651e5SJeremy L Thompson       CeedCallBackend(CeedOperatorFieldGetElemRestriction(op_output_fields[i], &elem_rstr));
1508e93651e5SJeremy L Thompson       CeedCallBackend(CeedElemRestrictionGetNumComponents(elem_rstr, &num_comp));
1509a61b1c91SJeremy L Thompson       max_rstr_buffer_size = CeedIntMax(max_rstr_buffer_size, num_comp * (is_all_tensor && (max_dim >= 3) ? Q_1d : 1));
1510681d0ea7SJeremy L Thompson       CeedCallBackend(CeedElemRestrictionDestroy(&elem_rstr));
1511e93651e5SJeremy L Thompson     }
15126de40545SJeremy L Thompson   }
15130183ed61SJeremy L Thompson   code << tab << "// Scratch restriction buffer space\n";
15140183ed61SJeremy L Thompson   code << tab << "CeedScalar r_e_scratch[" << max_rstr_buffer_size << "];\n";
1515e93651e5SJeremy L Thompson 
1516e93651e5SJeremy L Thompson   // -- Determine best input field processing order
1517e93651e5SJeremy L Thompson   CeedInt field_rstr_in_buffer[CEED_FIELD_MAX], input_field_order[CEED_FIELD_MAX];
1518e93651e5SJeremy L Thompson 
1519e93651e5SJeremy L Thompson   for (CeedInt i = 0; i < num_input_fields; i++) {
1520e93651e5SJeremy L Thompson     field_rstr_in_buffer[i] = -1;
1521e93651e5SJeremy L Thompson     input_field_order[i]    = -1;
1522e93651e5SJeremy L Thompson   }
1523e93651e5SJeremy L Thompson   {
1524e93651e5SJeremy L Thompson     bool    is_ordered[CEED_FIELD_MAX];
1525e93651e5SJeremy L Thompson     CeedInt curr_index = 0;
1526e93651e5SJeremy L Thompson 
1527e93651e5SJeremy L Thompson     for (CeedInt i = 0; i < num_input_fields; i++) is_ordered[i] = false;
1528e93651e5SJeremy L Thompson     for (CeedInt i = 0; i < num_input_fields; i++) {
1529e93651e5SJeremy L Thompson       CeedVector          vec_i;
1530e93651e5SJeremy L Thompson       CeedElemRestriction rstr_i;
1531e93651e5SJeremy L Thompson 
1532e93651e5SJeremy L Thompson       if (is_ordered[i]) continue;
1533e93651e5SJeremy L Thompson       field_rstr_in_buffer[i]       = i;
1534e93651e5SJeremy L Thompson       is_ordered[i]                 = true;
1535e93651e5SJeremy L Thompson       input_field_order[curr_index] = i;
1536e93651e5SJeremy L Thompson       curr_index++;
1537034f99fdSJeremy L Thompson       CeedCallBackend(CeedOperatorFieldGetVector(op_input_fields[i], &vec_i));
1538e93651e5SJeremy L Thompson       if (vec_i == CEED_VECTOR_NONE) continue;  // CEED_EVAL_WEIGHT
1539e93651e5SJeremy L Thompson       CeedCallBackend(CeedOperatorFieldGetElemRestriction(op_input_fields[i], &rstr_i));
1540e93651e5SJeremy L Thompson       for (CeedInt j = i + 1; j < num_input_fields; j++) {
1541e93651e5SJeremy L Thompson         CeedVector          vec_j;
1542e93651e5SJeremy L Thompson         CeedElemRestriction rstr_j;
1543e93651e5SJeremy L Thompson 
1544e93651e5SJeremy L Thompson         CeedCallBackend(CeedOperatorFieldGetVector(op_input_fields[j], &vec_j));
1545e93651e5SJeremy L Thompson         CeedCallBackend(CeedOperatorFieldGetElemRestriction(op_input_fields[j], &rstr_j));
1546e93651e5SJeremy L Thompson         if (rstr_i == rstr_j && vec_i == vec_j) {
1547e93651e5SJeremy L Thompson           field_rstr_in_buffer[j]       = i;
1548e93651e5SJeremy L Thompson           is_ordered[j]                 = true;
1549e93651e5SJeremy L Thompson           input_field_order[curr_index] = j;
1550e93651e5SJeremy L Thompson           curr_index++;
1551e93651e5SJeremy L Thompson         }
15523a2968d6SJeremy L Thompson         CeedCallBackend(CeedVectorDestroy(&vec_j));
15533a2968d6SJeremy L Thompson         CeedCallBackend(CeedElemRestrictionDestroy(&rstr_j));
1554e93651e5SJeremy L Thompson       }
15553a2968d6SJeremy L Thompson       CeedCallBackend(CeedVectorDestroy(&vec_i));
15563a2968d6SJeremy L Thompson       CeedCallBackend(CeedElemRestrictionDestroy(&rstr_i));
1557e93651e5SJeremy L Thompson     }
1558e93651e5SJeremy L Thompson   }
1559e93651e5SJeremy L Thompson 
15604b3e95d5SJeremy L Thompson   // -- Input restriction and basis
15610183ed61SJeremy L Thompson   code << "\n" << tab << "// -- Input field restrictions and basis actions\n";
15629e201c85SYohann   for (CeedInt i = 0; i < num_input_fields; i++) {
156359fa3f92SJeremy L Thompson     const char   *field_name;
156459fa3f92SJeremy L Thompson     const CeedInt f = input_field_order[i];
1565e93651e5SJeremy L Thompson 
156659fa3f92SJeremy L Thompson     CeedCallBackend(CeedOperatorFieldGetName(op_input_fields[f], &field_name));
15670183ed61SJeremy L Thompson     code << tab << "// ---- Input field " << f << ": " << field_name << "\n";
15687d8d0e25Snbeams 
15694b3e95d5SJeremy L Thompson     // ---- Restriction
15700183ed61SJeremy L Thompson     CeedCallBackend(CeedOperatorBuildKernelRestriction_Hip_gen(code, data, tab, f, field_rstr_in_buffer, op_input_fields[f], qf_input_fields[f],
15710183ed61SJeremy L Thompson                                                                max_dim, Q_1d, true, is_all_tensor, is_at_points, use_3d_slices));
1572b7453713SJeremy L Thompson 
15734b3e95d5SJeremy L Thompson     // ---- Basis action
15740183ed61SJeremy L Thompson     CeedCallBackend(CeedOperatorBuildKernelBasis_Hip_gen(code, data, tab, f, op_input_fields[f], qf_input_fields[f], max_dim, Q_1d, true,
15750183ed61SJeremy L Thompson                                                          is_all_tensor, is_at_points, use_3d_slices));
15767d8d0e25Snbeams   }
15777d8d0e25Snbeams 
15784b3e95d5SJeremy L Thompson   // -- Q function
15790183ed61SJeremy L Thompson   CeedCallBackend(CeedOperatorBuildKernelQFunction_Hip_gen(code, data, tab, max_dim, max_num_points, num_input_fields, op_input_fields,
15800183ed61SJeremy L Thompson                                                            qf_input_fields, num_output_fields, op_output_fields, qf_output_fields, qfunction_name,
15810183ed61SJeremy L Thompson                                                            Q_1d, is_all_tensor, is_at_points, use_3d_slices));
15827d8d0e25Snbeams 
15834b3e95d5SJeremy L Thompson   // -- Output basis and restriction
15840183ed61SJeremy L Thompson   code << "\n" << tab << "// -- Output field basis action and restrictions\n";
15859e201c85SYohann   for (CeedInt i = 0; i < num_output_fields; i++) {
158659fa3f92SJeremy L Thompson     const char *field_name;
158759fa3f92SJeremy L Thompson 
158859fa3f92SJeremy L Thompson     CeedCallBackend(CeedOperatorFieldGetName(op_output_fields[i], &field_name));
15890183ed61SJeremy L Thompson     code << tab << "// ---- Output field " << i << ": " << field_name << "\n";
1590b7453713SJeremy L Thompson 
15914b3e95d5SJeremy L Thompson     // ---- Basis action
15920183ed61SJeremy L Thompson     CeedCallBackend(CeedOperatorBuildKernelBasis_Hip_gen(code, data, tab, i, op_output_fields[i], qf_output_fields[i], max_dim, Q_1d, false,
15930183ed61SJeremy L Thompson                                                          is_all_tensor, is_at_points, use_3d_slices));
15947d8d0e25Snbeams 
15954b3e95d5SJeremy L Thompson     // ---- Restriction
15960183ed61SJeremy L Thompson     CeedCallBackend(CeedOperatorBuildKernelRestriction_Hip_gen(code, data, tab, i, NULL, op_output_fields[i], qf_output_fields[i], max_dim, Q_1d,
15970183ed61SJeremy L Thompson                                                                false, is_all_tensor, is_at_points, use_3d_slices));
15987d8d0e25Snbeams   }
15997d8d0e25Snbeams 
16004b3e95d5SJeremy L Thompson   // Close loop and function
16010183ed61SJeremy L Thompson   tab.pop();
16020183ed61SJeremy L Thompson   code << tab << "}\n";
16030183ed61SJeremy L Thompson   tab.pop();
16040183ed61SJeremy L Thompson   code << tab << "}\n";
16050183ed61SJeremy L Thompson   code << tab << "// -----------------------------------------------------------------------------\n\n";
16067d8d0e25Snbeams 
1607539ec17dSJeremy L Thompson   CeedInt block_sizes[3] = {0, 0, 0};
16089e201c85SYohann   CeedInt num_elem;
1609b7453713SJeremy L Thompson 
16103a2968d6SJeremy L Thompson   // Compile
16112b730f8bSJeremy L Thompson   CeedCallBackend(CeedOperatorGetNumElements(op, &num_elem));
161274398b5aSJeremy L Thompson   CeedCallBackend(BlockGridCalculate_Hip_gen(is_all_tensor ? max_dim : 1, num_elem, data->max_P_1d, is_all_tensor ? Q_1d : Q, block_sizes));
16138d12f40eSJeremy L Thompson   {
16148d12f40eSJeremy L Thompson     bool is_compile_good = false;
16158d12f40eSJeremy L Thompson 
1616a61b1c91SJeremy L Thompson     data->thread_1d = block_sizes[0];
16176b92dc4bSJeremy L Thompson     CeedCallBackend(CeedTryCompile_Hip(ceed, code.str().c_str(), &is_compile_good, &data->module, 2, "OP_T_1D", block_sizes[0], "BLOCK_SIZE",
16182b730f8bSJeremy L Thompson                                        block_sizes[0] * block_sizes[1] * block_sizes[2]));
16198d12f40eSJeremy L Thompson     if (is_compile_good) {
16208d12f40eSJeremy L Thompson       *is_good_build = true;
1621eb7e6cafSJeremy L Thompson       CeedCallBackend(CeedGetKernel_Hip(ceed, data->module, operator_name.c_str(), &data->op));
16228d12f40eSJeremy L Thompson     } else {
16238d12f40eSJeremy L Thompson       *is_good_build     = false;
16248d12f40eSJeremy L Thompson       data->use_fallback = true;
16258d12f40eSJeremy L Thompson     }
16268d12f40eSJeremy L Thompson   }
16272b730f8bSJeremy L Thompson   CeedCallBackend(CeedOperatorSetSetupDone(op));
16289bc66399SJeremy L Thompson   CeedCallBackend(CeedDestroy(&ceed));
1629c11e12f4SJeremy L Thompson   CeedCallBackend(CeedQFunctionDestroy(&qf));
1630e15f9bd0SJeremy L Thompson   return CEED_ERROR_SUCCESS;
16317d8d0e25Snbeams }
16322a86cc9dSSebastian Grimberg 
16337d8d0e25Snbeams //------------------------------------------------------------------------------
16340183ed61SJeremy L Thompson // Build AtPoints assembly operator kernel
16350183ed61SJeremy L Thompson //------------------------------------------------------------------------------
16360183ed61SJeremy L Thompson static int CeedOperatorBuildKernelAssemblyAtPoints_Hip_gen(CeedOperator op, bool is_full, bool *is_good_build) {
16370183ed61SJeremy L Thompson   bool                   is_all_tensor = true, is_at_points = false, use_3d_slices = false;
16380183ed61SJeremy L Thompson   Ceed                   ceed;
16390183ed61SJeremy L Thompson   CeedInt                Q, Q_1d, num_input_fields, num_output_fields, max_dim = 1, max_num_points = 0, coords_comp_stride = 0;
16400183ed61SJeremy L Thompson   CeedQFunctionField    *qf_input_fields, *qf_output_fields;
16410183ed61SJeremy L Thompson   CeedQFunction_Hip_gen *qf_data;
16420183ed61SJeremy L Thompson   CeedQFunction          qf;
16430183ed61SJeremy L Thompson   CeedOperatorField     *op_input_fields, *op_output_fields;
16440183ed61SJeremy L Thompson   CeedOperator_Hip_gen  *data;
16450183ed61SJeremy L Thompson   std::ostringstream     code;
16460183ed61SJeremy L Thompson   Tab                    tab;
16470183ed61SJeremy L Thompson 
16480183ed61SJeremy L Thompson   // Check compatibility
16490183ed61SJeremy L Thompson   CeedCallBackend(CeedOperatorGetCeed(op, &ceed));
16500183ed61SJeremy L Thompson   CeedCallBackend(CeedOperatorIsAtPoints(op, &is_at_points));
16510183ed61SJeremy L Thompson   CeedCheck(is_at_points, ceed, CEED_ERROR_BACKEND, "Only AtPoints operator assembly supported");
16520183ed61SJeremy L Thompson 
16530183ed61SJeremy L Thompson   // Retrieve operator data
16540183ed61SJeremy L Thompson   CeedCallBackend(CeedOperatorGetData(op, &data));
16550183ed61SJeremy L Thompson   Q       = data->Q;
16560183ed61SJeremy L Thompson   Q_1d    = data->Q_1d;
16570183ed61SJeremy L Thompson   max_dim = data->dim;
16580183ed61SJeremy L Thompson   {
16590183ed61SJeremy L Thompson     CeedElemRestriction rstr_points = NULL;
16600183ed61SJeremy L Thompson 
16610183ed61SJeremy L Thompson     CeedCallBackend(CeedOperatorAtPointsGetPoints(op, &rstr_points, NULL));
16620183ed61SJeremy L Thompson     CeedCallBackend(CeedElemRestrictionGetMaxPointsInElement(rstr_points, &max_num_points));
16630183ed61SJeremy L Thompson     CeedCallBackend(CeedElemRestrictionGetCompStride(rstr_points, &coords_comp_stride));
16640183ed61SJeremy L Thompson     CeedCallBackend(CeedElemRestrictionDestroy(&rstr_points));
16650183ed61SJeremy L Thompson   }
16660183ed61SJeremy L Thompson   CeedCallBackend(CeedOperatorGetQFunction(op, &qf));
16670183ed61SJeremy L Thompson   CeedCallBackend(CeedQFunctionGetData(qf, &qf_data));
16680183ed61SJeremy L Thompson   CeedCallBackend(CeedQFunctionGetFields(qf, NULL, &qf_input_fields, NULL, &qf_output_fields));
16690183ed61SJeremy L Thompson   CeedCallBackend(CeedOperatorGetFields(op, &num_input_fields, &op_input_fields, &num_output_fields, &op_output_fields));
16700183ed61SJeremy L Thompson 
16710183ed61SJeremy L Thompson   // Load basis source files
16720183ed61SJeremy L Thompson   code << tab << "// Tensor basis source\n";
16730183ed61SJeremy L Thompson   code << tab << "#include <ceed/jit-source/hip/hip-shared-basis-tensor-templates.h>\n\n";
16740183ed61SJeremy L Thompson   code << tab << "// AtPoints basis source\n";
16750183ed61SJeremy L Thompson   code << tab << "#include <ceed/jit-source/hip/hip-shared-basis-tensor-at-points-templates.h>\n\n";
16760183ed61SJeremy L Thompson   code << tab << "// CodeGen operator source\n";
16770183ed61SJeremy L Thompson   code << tab << "#include <ceed/jit-source/hip/hip-gen-templates.h>\n\n";
16780183ed61SJeremy L Thompson 
16790183ed61SJeremy L Thompson   // Get QFunction name
16800183ed61SJeremy L Thompson   std::string qfunction_name(qf_data->qfunction_name);
16810183ed61SJeremy L Thompson   std::string operator_name;
16820183ed61SJeremy L Thompson 
16830183ed61SJeremy L Thompson   if (is_full) {
16840183ed61SJeremy L Thompson     operator_name = "CeedKernelHipGenOperatorFullAssembly_" + qfunction_name;
16850183ed61SJeremy L Thompson   } else {
16860183ed61SJeremy L Thompson     operator_name = "CeedKernelHipGenOperatorDiagonalAssembly_" + qfunction_name;
16870183ed61SJeremy L Thompson   }
16880183ed61SJeremy L Thompson 
16890183ed61SJeremy L Thompson   // Define CEED_Q_VLA
16900183ed61SJeremy L Thompson   code << "\n" << tab << "#undef CEED_Q_VLA\n";
16910183ed61SJeremy L Thompson   code << tab << "#define CEED_Q_VLA 1\n\n";
16920183ed61SJeremy L Thompson 
16930183ed61SJeremy L Thompson   // Add user QFunction source
16940183ed61SJeremy L Thompson   {
16950183ed61SJeremy L Thompson     const char *source_path;
16960183ed61SJeremy L Thompson 
16970183ed61SJeremy L Thompson     CeedCallBackend(CeedQFunctionGetSourcePath(qf, &source_path));
16980183ed61SJeremy L Thompson     CeedCheck(source_path, ceed, CEED_ERROR_UNSUPPORTED, "/gpu/hip/gen backend requires QFunction source code file");
16990183ed61SJeremy L Thompson 
17000183ed61SJeremy L Thompson     code << tab << "// User QFunction source\n";
17010183ed61SJeremy L Thompson     code << tab << "#include \"" << source_path << "\"\n\n";
17020183ed61SJeremy L Thompson   }
17030183ed61SJeremy L Thompson 
17040183ed61SJeremy L Thompson   // Setup
17050183ed61SJeremy L Thompson   code << "\n" << tab << "// -----------------------------------------------------------------------------\n";
17060183ed61SJeremy L Thompson   code << tab << "// Operator Assembly Kernel\n";
17070183ed61SJeremy L Thompson   code << tab << "// \n";
17080183ed61SJeremy L Thompson   code << tab << "// d_[in,out]_i:   CeedVector device array\n";
17090183ed61SJeremy L Thompson   code << tab << "// r_[in,out]_e_i: Element vector register\n";
17100183ed61SJeremy L Thompson   code << tab << "// r_[in,out]_q_i: Quadrature space vector register\n";
17110183ed61SJeremy L Thompson   code << tab << "// r_[in,out]_c_i: AtPoints Chebyshev coefficients register\n";
17120183ed61SJeremy L Thompson   code << tab << "// r_[in,out]_s_i: Quadrature space slice vector register\n";
17130183ed61SJeremy L Thompson   code << tab << "// \n";
17140183ed61SJeremy L Thompson   code << tab << "// s_B_[in,out]_i: Interpolation matrix, shared memory\n";
17150183ed61SJeremy L Thompson   code << tab << "// s_G_[in,out]_i: Gradient matrix, shared memory\n";
17160183ed61SJeremy L Thompson   code << tab << "// -----------------------------------------------------------------------------\n";
17170183ed61SJeremy L Thompson   code << tab << "extern \"C\" __global__ void " << operator_name
17180183ed61SJeremy L Thompson        << "(CeedInt num_elem, void* ctx, FieldsInt_Hip indices, Fields_Hip fields, Fields_Hip B, Fields_Hip G, CeedScalar *W, Points_Hip "
17190183ed61SJeremy L Thompson           "points, CeedScalar *__restrict__ values_array) {\n";
17200183ed61SJeremy L Thompson   tab.push();
17210183ed61SJeremy L Thompson 
17220183ed61SJeremy L Thompson   // Scratch buffers
17230183ed61SJeremy L Thompson   for (CeedInt i = 0; i < num_input_fields; i++) {
17240183ed61SJeremy L Thompson     CeedEvalMode eval_mode;
17250183ed61SJeremy L Thompson 
17260183ed61SJeremy L Thompson     CeedCallBackend(CeedQFunctionFieldGetEvalMode(qf_input_fields[i], &eval_mode));
17270183ed61SJeremy L Thompson     if (eval_mode != CEED_EVAL_WEIGHT) {  // Skip CEED_EVAL_WEIGHT
17280183ed61SJeremy L Thompson       code << tab << "const CeedScalar *__restrict__ d_in_" << i << " = fields.inputs[" << i << "];\n";
17290183ed61SJeremy L Thompson     }
17300183ed61SJeremy L Thompson   }
17310183ed61SJeremy L Thompson   for (CeedInt i = 0; i < num_output_fields; i++) {
17320183ed61SJeremy L Thompson     code << tab << "CeedScalar *__restrict__ d_out_" << i << " = fields.outputs[" << i << "];\n";
17330183ed61SJeremy L Thompson   }
17340183ed61SJeremy L Thompson 
17350183ed61SJeremy L Thompson   code << tab << "const CeedInt max_dim = " << max_dim << ";\n";
17360183ed61SJeremy L Thompson   code << tab << "const CeedInt Q_1d = " << Q_1d << ";\n";
17370183ed61SJeremy L Thompson   code << tab << "const CeedInt max_num_points = " << max_num_points << ";\n";
17380183ed61SJeremy L Thompson   code << tab << "const CeedInt coords_comp_stride = " << coords_comp_stride << ";\n";
17390183ed61SJeremy L Thompson 
17400183ed61SJeremy L Thompson   // Shared data
17410183ed61SJeremy L Thompson   code << tab << "extern __shared__ CeedScalar slice[];\n";
17420183ed61SJeremy L Thompson   code << tab << "SharedData_Hip data;\n";
17430183ed61SJeremy L Thompson   code << tab << "data.t_id_x = threadIdx.x;\n";
17440183ed61SJeremy L Thompson   code << tab << "data.t_id_y = threadIdx.y;\n";
17450183ed61SJeremy L Thompson   code << tab << "data.t_id_z = threadIdx.z;\n";
17460183ed61SJeremy L Thompson   code << tab << "data.t_id   = threadIdx.x + threadIdx.y*blockDim.x + threadIdx.z*blockDim.y*blockDim.x;\n";
17470183ed61SJeremy L Thompson   code << tab << "data.slice  = slice + data.t_id_z*OP_T_1D" << ((!is_all_tensor || max_dim == 1) ? "" : "*OP_T_1D") << ";\n";
17480183ed61SJeremy L Thompson 
17490183ed61SJeremy L Thompson   // -- Determine input mat reuse
17500183ed61SJeremy L Thompson   FieldReuse_Hip input_matrix_reuse[CEED_FIELD_MAX];
17510183ed61SJeremy L Thompson 
17520183ed61SJeremy L Thompson   for (CeedInt i = 0; i < num_input_fields; i++) {
17530183ed61SJeremy L Thompson     input_matrix_reuse[i].index = -1;
17540183ed61SJeremy L Thompson   }
17550183ed61SJeremy L Thompson   for (CeedInt i = 0; i < num_input_fields; i++) {
17560183ed61SJeremy L Thompson     CeedEvalMode eval_mode_i;
17570183ed61SJeremy L Thompson     CeedBasis    basis_i;
17580183ed61SJeremy L Thompson 
17590183ed61SJeremy L Thompson     CeedCallBackend(CeedQFunctionFieldGetEvalMode(qf_input_fields[i], &eval_mode_i));
17600183ed61SJeremy L Thompson     if (eval_mode_i == CEED_EVAL_WEIGHT) continue;
17610183ed61SJeremy L Thompson     CeedCallBackend(CeedOperatorFieldGetBasis(op_input_fields[i], &basis_i));
17620183ed61SJeremy L Thompson     for (CeedInt j = 0; (input_matrix_reuse[i].index == -1) && (j < i); j++) {
17630183ed61SJeremy L Thompson       CeedEvalMode eval_mode_j;
17640183ed61SJeremy L Thompson       CeedBasis    basis_j;
17650183ed61SJeremy L Thompson 
17660183ed61SJeremy L Thompson       CeedCallBackend(CeedQFunctionFieldGetEvalMode(qf_input_fields[j], &eval_mode_j));
17670183ed61SJeremy L Thompson       if (eval_mode_j == CEED_EVAL_WEIGHT) continue;
17680183ed61SJeremy L Thompson       CeedCallBackend(CeedOperatorFieldGetBasis(op_input_fields[j], &basis_j));
17690183ed61SJeremy L Thompson       if (basis_i == basis_j) {
17700183ed61SJeremy L Thompson         input_matrix_reuse[i].index     = j;
17710183ed61SJeremy L Thompson         input_matrix_reuse[i].is_input  = true;
17720183ed61SJeremy L Thompson         input_matrix_reuse[i].eval_mode = eval_mode_j;
17730183ed61SJeremy L Thompson       }
17740183ed61SJeremy L Thompson       CeedCallBackend(CeedBasisDestroy(&basis_j));
17750183ed61SJeremy L Thompson     }
17760183ed61SJeremy L Thompson     CeedCallBackend(CeedBasisDestroy(&basis_i));
17770183ed61SJeremy L Thompson   }
17780183ed61SJeremy L Thompson 
17790183ed61SJeremy L Thompson   // -- Determine output mat reuse
17800183ed61SJeremy L Thompson   FieldReuse_Hip output_matrix_reuse[CEED_FIELD_MAX];
17810183ed61SJeremy L Thompson 
17820183ed61SJeremy L Thompson   for (CeedInt i = 0; i < num_output_fields; i++) {
17830183ed61SJeremy L Thompson     output_matrix_reuse[i].index = -1;
17840183ed61SJeremy L Thompson   }
17850183ed61SJeremy L Thompson   for (CeedInt i = 0; i < num_output_fields; i++) {
17860183ed61SJeremy L Thompson     CeedEvalMode eval_mode_i;
17870183ed61SJeremy L Thompson     CeedBasis    basis_i;
17880183ed61SJeremy L Thompson 
17890183ed61SJeremy L Thompson     CeedCallBackend(CeedQFunctionFieldGetEvalMode(qf_output_fields[i], &eval_mode_i));
17900183ed61SJeremy L Thompson     CeedCallBackend(CeedOperatorFieldGetBasis(op_output_fields[i], &basis_i));
17910183ed61SJeremy L Thompson     for (CeedInt j = 0; (output_matrix_reuse[i].index == -1) && (j < num_input_fields); j++) {
17920183ed61SJeremy L Thompson       CeedEvalMode eval_mode_j;
17930183ed61SJeremy L Thompson       CeedBasis    basis_j;
17940183ed61SJeremy L Thompson 
17950183ed61SJeremy L Thompson       CeedCallBackend(CeedQFunctionFieldGetEvalMode(qf_input_fields[j], &eval_mode_j));
17960183ed61SJeremy L Thompson       if (eval_mode_j == CEED_EVAL_WEIGHT) continue;
17970183ed61SJeremy L Thompson       CeedCallBackend(CeedOperatorFieldGetBasis(op_input_fields[j], &basis_j));
17980183ed61SJeremy L Thompson       if (basis_i == basis_j) {
17990183ed61SJeremy L Thompson         output_matrix_reuse[i].index     = j;
18000183ed61SJeremy L Thompson         output_matrix_reuse[i].is_input  = true;
18010183ed61SJeremy L Thompson         output_matrix_reuse[i].eval_mode = eval_mode_j;
18020183ed61SJeremy L Thompson       }
18030183ed61SJeremy L Thompson       CeedCallBackend(CeedBasisDestroy(&basis_j));
18040183ed61SJeremy L Thompson     }
18050183ed61SJeremy L Thompson     for (CeedInt j = 0; (output_matrix_reuse[i].index == -1) && (j < i); j++) {
18060183ed61SJeremy L Thompson       CeedEvalMode eval_mode_j;
18070183ed61SJeremy L Thompson       CeedBasis    basis_j;
18080183ed61SJeremy L Thompson 
18090183ed61SJeremy L Thompson       CeedCallBackend(CeedQFunctionFieldGetEvalMode(qf_output_fields[j], &eval_mode_j));
18100183ed61SJeremy L Thompson       if (eval_mode_j == CEED_EVAL_WEIGHT) continue;
18110183ed61SJeremy L Thompson       CeedCallBackend(CeedOperatorFieldGetBasis(op_output_fields[j], &basis_j));
18120183ed61SJeremy L Thompson       if (basis_i == basis_j) {
18130183ed61SJeremy L Thompson         output_matrix_reuse[i].index     = j;
18140183ed61SJeremy L Thompson         output_matrix_reuse[i].is_input  = false;
18150183ed61SJeremy L Thompson         output_matrix_reuse[i].eval_mode = eval_mode_j;
18160183ed61SJeremy L Thompson       }
18170183ed61SJeremy L Thompson       CeedCallBackend(CeedBasisDestroy(&basis_j));
18180183ed61SJeremy L Thompson     }
18190183ed61SJeremy L Thompson     CeedCallBackend(CeedBasisDestroy(&basis_i));
18200183ed61SJeremy L Thompson   }
18210183ed61SJeremy L Thompson 
18220183ed61SJeremy L Thompson   // Initialize constants, and matrices B and G
18230183ed61SJeremy L Thompson   code << "\n" << tab << "// Input field constants and basis data\n";
18240183ed61SJeremy L Thompson   for (CeedInt i = 0; i < num_input_fields; i++) {
18250183ed61SJeremy L Thompson     CeedCallBackend(CeedOperatorBuildKernelFieldData_Hip_gen(code, data, tab, i, op_input_fields[i], qf_input_fields[i], input_matrix_reuse[i],
18260183ed61SJeremy L Thompson                                                              max_dim, Q, Q_1d, true, is_all_tensor, is_at_points, use_3d_slices));
18270183ed61SJeremy L Thompson   }
18280183ed61SJeremy L Thompson   code << "\n" << tab << "// Output field constants and basis data\n";
18290183ed61SJeremy L Thompson   for (CeedInt i = 0; i < num_output_fields; i++) {
18300183ed61SJeremy L Thompson     CeedCallBackend(CeedOperatorBuildKernelFieldData_Hip_gen(code, data, tab, i, op_output_fields[i], qf_output_fields[i], output_matrix_reuse[i],
18310183ed61SJeremy L Thompson                                                              max_dim, Q, Q_1d, false, is_all_tensor, is_at_points, use_3d_slices));
18320183ed61SJeremy L Thompson   }
18330183ed61SJeremy L Thompson 
18340183ed61SJeremy L Thompson   // Loop over all elements
18350183ed61SJeremy L Thompson   code << "\n" << tab << "// Element loop\n";
18360183ed61SJeremy L Thompson   code << tab << "__syncthreads();\n";
18370183ed61SJeremy L Thompson   code << tab << "for (CeedInt elem = blockIdx.x*blockDim.z + threadIdx.z; elem < num_elem; elem += gridDim.x*blockDim.z) {\n";
18380183ed61SJeremy L Thompson   tab.push();
18390183ed61SJeremy L Thompson 
18400183ed61SJeremy L Thompson   // -- Compute minimum buffer space needed
18410183ed61SJeremy L Thompson   CeedInt max_rstr_buffer_size = 1;
18420183ed61SJeremy L Thompson 
18430183ed61SJeremy L Thompson   for (CeedInt i = 0; i < num_input_fields; i++) {
18440183ed61SJeremy L Thompson     CeedEvalMode eval_mode;
18450183ed61SJeremy L Thompson 
18460183ed61SJeremy L Thompson     CeedCallBackend(CeedQFunctionFieldGetEvalMode(qf_input_fields[i], &eval_mode));
18470183ed61SJeremy L Thompson     if (eval_mode != CEED_EVAL_NONE && eval_mode != CEED_EVAL_WEIGHT) {
18480183ed61SJeremy L Thompson       CeedInt             num_comp;
18490183ed61SJeremy L Thompson       CeedElemRestriction elem_rstr;
18500183ed61SJeremy L Thompson 
18510183ed61SJeremy L Thompson       CeedCallBackend(CeedOperatorFieldGetElemRestriction(op_input_fields[i], &elem_rstr));
18520183ed61SJeremy L Thompson       CeedCallBackend(CeedElemRestrictionGetNumComponents(elem_rstr, &num_comp));
18530183ed61SJeremy L Thompson       max_rstr_buffer_size = CeedIntMax(max_rstr_buffer_size, num_comp * (is_all_tensor && (max_dim >= 3) ? Q_1d : 1));
18540183ed61SJeremy L Thompson       CeedCallBackend(CeedElemRestrictionDestroy(&elem_rstr));
18550183ed61SJeremy L Thompson     }
18560183ed61SJeremy L Thompson   }
18570183ed61SJeremy L Thompson   for (CeedInt i = 0; i < num_output_fields; i++) {
18580183ed61SJeremy L Thompson     CeedEvalMode eval_mode;
18590183ed61SJeremy L Thompson 
18600183ed61SJeremy L Thompson     CeedCallBackend(CeedQFunctionFieldGetEvalMode(qf_output_fields[i], &eval_mode));
18610183ed61SJeremy L Thompson     if (eval_mode != CEED_EVAL_NONE) {
18620183ed61SJeremy L Thompson       CeedInt             num_comp;
18630183ed61SJeremy L Thompson       CeedElemRestriction elem_rstr;
18640183ed61SJeremy L Thompson 
18650183ed61SJeremy L Thompson       CeedCallBackend(CeedOperatorFieldGetElemRestriction(op_output_fields[i], &elem_rstr));
18660183ed61SJeremy L Thompson       CeedCallBackend(CeedElemRestrictionGetNumComponents(elem_rstr, &num_comp));
18670183ed61SJeremy L Thompson       max_rstr_buffer_size = CeedIntMax(max_rstr_buffer_size, num_comp * (is_all_tensor && (max_dim >= 3) ? Q_1d : 1));
18680183ed61SJeremy L Thompson       CeedCallBackend(CeedElemRestrictionDestroy(&elem_rstr));
18690183ed61SJeremy L Thompson     }
18700183ed61SJeremy L Thompson   }
18710183ed61SJeremy L Thompson   code << tab << "// Scratch restriction buffer space\n";
18720183ed61SJeremy L Thompson   code << tab << "CeedScalar r_e_scratch[" << max_rstr_buffer_size << "];\n";
18730183ed61SJeremy L Thompson 
18740183ed61SJeremy L Thompson   // -- Determine best input field processing order
18750183ed61SJeremy L Thompson   CeedInt field_rstr_in_buffer[CEED_FIELD_MAX], input_field_order[CEED_FIELD_MAX];
18760183ed61SJeremy L Thompson 
18770183ed61SJeremy L Thompson   for (CeedInt i = 0; i < num_input_fields; i++) {
18780183ed61SJeremy L Thompson     field_rstr_in_buffer[i] = -1;
18790183ed61SJeremy L Thompson     input_field_order[i]    = -1;
18800183ed61SJeremy L Thompson   }
18810183ed61SJeremy L Thompson   {
18820183ed61SJeremy L Thompson     bool    is_ordered[CEED_FIELD_MAX];
18830183ed61SJeremy L Thompson     CeedInt curr_index = 0;
18840183ed61SJeremy L Thompson 
18850183ed61SJeremy L Thompson     for (CeedInt i = 0; i < num_input_fields; i++) is_ordered[i] = false;
18860183ed61SJeremy L Thompson     for (CeedInt i = 0; i < num_input_fields; i++) {
18870183ed61SJeremy L Thompson       CeedVector          vec_i;
18880183ed61SJeremy L Thompson       CeedElemRestriction rstr_i;
18890183ed61SJeremy L Thompson 
18900183ed61SJeremy L Thompson       if (is_ordered[i]) continue;
18910183ed61SJeremy L Thompson       field_rstr_in_buffer[i]       = i;
18920183ed61SJeremy L Thompson       is_ordered[i]                 = true;
18930183ed61SJeremy L Thompson       input_field_order[curr_index] = i;
18940183ed61SJeremy L Thompson       curr_index++;
18950183ed61SJeremy L Thompson       CeedCallBackend(CeedOperatorFieldGetVector(op_input_fields[i], &vec_i));
18960183ed61SJeremy L Thompson       if (vec_i == CEED_VECTOR_NONE) continue;  // CEED_EVAL_WEIGHT
18970183ed61SJeremy L Thompson       CeedCallBackend(CeedOperatorFieldGetElemRestriction(op_input_fields[i], &rstr_i));
18980183ed61SJeremy L Thompson       for (CeedInt j = i + 1; j < num_input_fields; j++) {
18990183ed61SJeremy L Thompson         CeedVector          vec_j;
19000183ed61SJeremy L Thompson         CeedElemRestriction rstr_j;
19010183ed61SJeremy L Thompson 
19020183ed61SJeremy L Thompson         CeedCallBackend(CeedOperatorFieldGetVector(op_input_fields[j], &vec_j));
19030183ed61SJeremy L Thompson         CeedCallBackend(CeedOperatorFieldGetElemRestriction(op_input_fields[j], &rstr_j));
19040183ed61SJeremy L Thompson         if (rstr_i == rstr_j && vec_i == vec_j) {
19050183ed61SJeremy L Thompson           field_rstr_in_buffer[j]       = i;
19060183ed61SJeremy L Thompson           is_ordered[j]                 = true;
19070183ed61SJeremy L Thompson           input_field_order[curr_index] = j;
19080183ed61SJeremy L Thompson           curr_index++;
19090183ed61SJeremy L Thompson         }
19100183ed61SJeremy L Thompson         CeedCallBackend(CeedVectorDestroy(&vec_j));
19110183ed61SJeremy L Thompson         CeedCallBackend(CeedElemRestrictionDestroy(&rstr_j));
19120183ed61SJeremy L Thompson       }
19130183ed61SJeremy L Thompson       CeedCallBackend(CeedVectorDestroy(&vec_i));
19140183ed61SJeremy L Thompson       CeedCallBackend(CeedElemRestrictionDestroy(&rstr_i));
19150183ed61SJeremy L Thompson     }
19160183ed61SJeremy L Thompson   }
19170183ed61SJeremy L Thompson 
19180183ed61SJeremy L Thompson   // -- Input restriction and basis
19190183ed61SJeremy L Thompson   code << "\n" << tab << "// -- Input field restrictions and basis actions\n";
19200183ed61SJeremy L Thompson   CeedInt active_field_index = -1;
19210183ed61SJeremy L Thompson 
19220183ed61SJeremy L Thompson   for (CeedInt i = 0; i < num_input_fields; i++) {
19230183ed61SJeremy L Thompson     bool          is_active = false;
19240183ed61SJeremy L Thompson     const char   *field_name;
19250183ed61SJeremy L Thompson     const CeedInt f = input_field_order[i];
19260183ed61SJeremy L Thompson 
19270183ed61SJeremy L Thompson     {
19280183ed61SJeremy L Thompson       CeedVector vec;
19290183ed61SJeremy L Thompson 
19300183ed61SJeremy L Thompson       CeedCallBackend(CeedOperatorFieldGetVector(op_input_fields[f], &vec));
19310183ed61SJeremy L Thompson       is_active = vec == CEED_VECTOR_ACTIVE;
19320183ed61SJeremy L Thompson       CeedCallBackend(CeedVectorDestroy(&vec));
19330183ed61SJeremy L Thompson     }
19340183ed61SJeremy L Thompson 
19350183ed61SJeremy L Thompson     CeedCallBackend(CeedOperatorFieldGetName(op_input_fields[f], &field_name));
19360183ed61SJeremy L Thompson     code << tab << "// ---- Input field " << f << ": " << field_name << "\n";
19370183ed61SJeremy L Thompson 
19380183ed61SJeremy L Thompson     if (is_active) {
19390183ed61SJeremy L Thompson       std::string var_suffix = "_in_" + std::to_string(f);
19400183ed61SJeremy L Thompson 
19410183ed61SJeremy L Thompson       code << tab << "// Active field - no restriction or basis action here\n";
19420183ed61SJeremy L Thompson       if (active_field_index == -1) {
19430183ed61SJeremy L Thompson         active_field_index = f;
19440183ed61SJeremy L Thompson         code << tab << "CeedScalar r_e" << var_suffix << "[num_comp" << var_suffix << "*" << (max_dim >= 3 ? "P_1d" + var_suffix : "1")
19450183ed61SJeremy L Thompson              << "] = {0.0};\n";
19460183ed61SJeremy L Thompson       } else {
19470183ed61SJeremy L Thompson         code << tab << "CeedScalar *r_e" << var_suffix << " = r_e_in_" << active_field_index << ";\n";
19480183ed61SJeremy L Thompson       }
19490183ed61SJeremy L Thompson     } else {
19500183ed61SJeremy L Thompson       // ---- Restriction
19510183ed61SJeremy L Thompson       CeedCallBackend(CeedOperatorBuildKernelRestriction_Hip_gen(code, data, tab, f, field_rstr_in_buffer, op_input_fields[f], qf_input_fields[f],
19520183ed61SJeremy L Thompson                                                                  max_dim, Q_1d, true, is_all_tensor, is_at_points, use_3d_slices));
19530183ed61SJeremy L Thompson 
19540183ed61SJeremy L Thompson       // ---- Basis action
19550183ed61SJeremy L Thompson       CeedCallBackend(CeedOperatorBuildKernelBasis_Hip_gen(code, data, tab, f, op_input_fields[f], qf_input_fields[f], max_dim, Q_1d, true,
19560183ed61SJeremy L Thompson                                                            is_all_tensor, is_at_points, use_3d_slices));
19570183ed61SJeremy L Thompson     }
19580183ed61SJeremy L Thompson   }
19590183ed61SJeremy L Thompson 
19600183ed61SJeremy L Thompson   // -- Loop over active field
19610183ed61SJeremy L Thompson   std::string active_var_suffix = "_in_" + std::to_string(active_field_index);
19620183ed61SJeremy L Thompson 
19630183ed61SJeremy L Thompson   code << "\n" << tab << "// Loop over nodes in active field\n";
19640183ed61SJeremy L Thompson   code << tab << "for (CeedInt n = 0; n < num_comp" << active_var_suffix << "*P_1d" << active_var_suffix
19650183ed61SJeremy L Thompson        << (max_dim > 1 ? "*P_1d" + active_var_suffix : "") << (max_dim > 2 ? "*P_1d" + active_var_suffix : "") << "; n++) {\n";
19660183ed61SJeremy L Thompson   tab.push();
19670183ed61SJeremy L Thompson 
19680183ed61SJeremy L Thompson   // -- Set current active node and component to 1
19690183ed61SJeremy L Thompson   code << tab << "// Set current active node and component to 1.0\n";
19700183ed61SJeremy L Thompson   code << tab << "SetEVecStandard" << max_dim << "d_Single<num_comp" << active_var_suffix << ", P_1d" << active_var_suffix << ">(data, n, 1.0, r_e"
19710183ed61SJeremy L Thompson        << active_var_suffix << ");\n\n";
19720183ed61SJeremy L Thompson 
19730183ed61SJeremy L Thompson   for (CeedInt i = 0; i < num_input_fields; i++) {
19740183ed61SJeremy L Thompson     bool          is_active = false;
19750183ed61SJeremy L Thompson     const char   *field_name;
19760183ed61SJeremy L Thompson     const CeedInt f = input_field_order[i];
19770183ed61SJeremy L Thompson 
19780183ed61SJeremy L Thompson     {
19790183ed61SJeremy L Thompson       CeedVector vec;
19800183ed61SJeremy L Thompson 
19810183ed61SJeremy L Thompson       CeedCallBackend(CeedOperatorFieldGetVector(op_input_fields[f], &vec));
19820183ed61SJeremy L Thompson       is_active = vec == CEED_VECTOR_ACTIVE;
19830183ed61SJeremy L Thompson       CeedCallBackend(CeedVectorDestroy(&vec));
19840183ed61SJeremy L Thompson     }
19850183ed61SJeremy L Thompson     if (!is_active) continue;
19860183ed61SJeremy L Thompson 
19870183ed61SJeremy L Thompson     CeedCallBackend(CeedOperatorFieldGetName(op_input_fields[f], &field_name));
19880183ed61SJeremy L Thompson     code << tab << "// ---- Input field " << f << ": " << field_name << "\n";
19890183ed61SJeremy L Thompson 
19900183ed61SJeremy L Thompson     // ---- Basis action
19910183ed61SJeremy L Thompson     CeedCallBackend(CeedOperatorBuildKernelBasis_Hip_gen(code, data, tab, f, op_input_fields[f], qf_input_fields[f], max_dim, Q_1d, true,
19920183ed61SJeremy L Thompson                                                          is_all_tensor, is_at_points, use_3d_slices));
19930183ed61SJeremy L Thompson   }
19940183ed61SJeremy L Thompson 
19950183ed61SJeremy L Thompson   // -- Q function
19960183ed61SJeremy L Thompson   CeedCallBackend(CeedOperatorBuildKernelQFunction_Hip_gen(code, data, tab, max_dim, max_num_points, num_input_fields, op_input_fields,
19970183ed61SJeremy L Thompson                                                            qf_input_fields, num_output_fields, op_output_fields, qf_output_fields, qfunction_name,
19980183ed61SJeremy L Thompson                                                            Q_1d, is_all_tensor, is_at_points, use_3d_slices));
19990183ed61SJeremy L Thompson 
20000183ed61SJeremy L Thompson   // -- Output basis and restriction
20010183ed61SJeremy L Thompson   code << "\n" << tab << "// -- Output field basis action and restrictions\n";
20020183ed61SJeremy L Thompson   for (CeedInt i = 0; i < num_output_fields; i++) {
20030183ed61SJeremy L Thompson     bool        is_active = false;
20040183ed61SJeremy L Thompson     const char *field_name;
20050183ed61SJeremy L Thompson 
20060183ed61SJeremy L Thompson     {
20070183ed61SJeremy L Thompson       CeedVector vec;
20080183ed61SJeremy L Thompson 
20090183ed61SJeremy L Thompson       CeedCallBackend(CeedOperatorFieldGetVector(op_output_fields[i], &vec));
20100183ed61SJeremy L Thompson       is_active = vec == CEED_VECTOR_ACTIVE;
20110183ed61SJeremy L Thompson       CeedCallBackend(CeedVectorDestroy(&vec));
20120183ed61SJeremy L Thompson     }
20130183ed61SJeremy L Thompson     if (!is_active) continue;
20140183ed61SJeremy L Thompson 
20150183ed61SJeremy L Thompson     CeedCallBackend(CeedOperatorFieldGetName(op_output_fields[i], &field_name));
20160183ed61SJeremy L Thompson     code << tab << "// ---- Output field " << i << ": " << field_name << "\n";
20170183ed61SJeremy L Thompson 
20180183ed61SJeremy L Thompson     // ---- Basis action
20190183ed61SJeremy L Thompson     CeedCallBackend(CeedOperatorBuildKernelBasis_Hip_gen(code, data, tab, i, op_output_fields[i], qf_output_fields[i], max_dim, Q_1d, false,
20200183ed61SJeremy L Thompson                                                          is_all_tensor, is_at_points, use_3d_slices));
20210183ed61SJeremy L Thompson 
20220183ed61SJeremy L Thompson     // ---- Restriction
20230183ed61SJeremy L Thompson     if (is_full) {
2024692716b7SZach Atkins       std::string         var_suffix = "_out_" + std::to_string(i);
2025692716b7SZach Atkins       CeedInt             comp_stride;
2026692716b7SZach Atkins       CeedSize            l_size;
2027692716b7SZach Atkins       CeedElemRestriction elem_rstr;
2028692716b7SZach Atkins 
2029692716b7SZach Atkins       CeedCallBackend(CeedOperatorFieldGetElemRestriction(op_output_fields[i], &elem_rstr));
2030692716b7SZach Atkins       CeedCallBackend(CeedElemRestrictionGetLVectorSize(elem_rstr, &l_size));
2031692716b7SZach Atkins       code << tab << "const CeedInt l_size" << var_suffix << " = " << l_size << ";\n";
2032692716b7SZach Atkins       CeedCallBackend(CeedElemRestrictionGetCompStride(elem_rstr, &comp_stride));
2033692716b7SZach Atkins       code << tab << "const CeedInt comp_stride" << var_suffix << " = " << comp_stride << ";\n";
2034692716b7SZach Atkins       code << tab << "WriteLVecStandard" << max_dim << "d_Assembly<num_comp" << var_suffix << ", comp_stride" << var_suffix << ", P_1d" + var_suffix
2035692716b7SZach Atkins            << ">(data, l_size" << var_suffix << ", elem, n, r_e" << var_suffix << ", values_array);\n";
2036692716b7SZach Atkins       CeedCallBackend(CeedElemRestrictionDestroy(&elem_rstr));
20370183ed61SJeremy L Thompson     } else {
20380183ed61SJeremy L Thompson       std::string         var_suffix = "_out_" + std::to_string(i);
20390183ed61SJeremy L Thompson       CeedInt             comp_stride;
20400183ed61SJeremy L Thompson       CeedSize            l_size;
20410183ed61SJeremy L Thompson       CeedElemRestriction elem_rstr;
20420183ed61SJeremy L Thompson 
20430183ed61SJeremy L Thompson       CeedCallBackend(CeedOperatorFieldGetElemRestriction(op_output_fields[i], &elem_rstr));
20440183ed61SJeremy L Thompson       CeedCallBackend(CeedElemRestrictionGetLVectorSize(elem_rstr, &l_size));
20450183ed61SJeremy L Thompson       code << tab << "const CeedInt l_size" << var_suffix << " = " << l_size << ";\n";
20460183ed61SJeremy L Thompson       CeedCallBackend(CeedElemRestrictionGetCompStride(elem_rstr, &comp_stride));
20470183ed61SJeremy L Thompson       code << tab << "const CeedInt comp_stride" << var_suffix << " = " << comp_stride << ";\n";
20480183ed61SJeremy L Thompson       code << tab << "WriteLVecStandard" << max_dim << "d_Single<num_comp" << var_suffix << ", comp_stride" << var_suffix << ", P_1d" + var_suffix
20490183ed61SJeremy L Thompson            << ">(data, l_size" << var_suffix << ", elem, n, indices.outputs[" << i << "], r_e" << var_suffix << ", values_array);\n";
20500183ed61SJeremy L Thompson       CeedCallBackend(CeedElemRestrictionDestroy(&elem_rstr));
20510183ed61SJeremy L Thompson     }
20520183ed61SJeremy L Thompson   }
20530183ed61SJeremy L Thompson 
20540183ed61SJeremy L Thompson   // -- Reset current active node and component
20550183ed61SJeremy L Thompson   code << "\n" << tab << "// Reset current active node and component to 0.0\n";
20560183ed61SJeremy L Thompson   code << tab << "SetEVecStandard" << max_dim << "d_Single<num_comp" << active_var_suffix << ", P_1d" << active_var_suffix << ">(data, n, 0.0, r_e"
20570183ed61SJeremy L Thompson        << active_var_suffix << ");\n";
20580183ed61SJeremy L Thompson 
20590183ed61SJeremy L Thompson   // -- End of loop over active field
20600183ed61SJeremy L Thompson   tab.pop();
20610183ed61SJeremy L Thompson   code << tab << "}\n";
20620183ed61SJeremy L Thompson 
20630183ed61SJeremy L Thompson   // Close loop and function
20640183ed61SJeremy L Thompson   tab.pop();
20650183ed61SJeremy L Thompson   code << tab << "}\n";
20660183ed61SJeremy L Thompson   tab.pop();
20670183ed61SJeremy L Thompson   code << tab << "}\n";
20680183ed61SJeremy L Thompson   code << tab << "// -----------------------------------------------------------------------------\n\n";
20690183ed61SJeremy L Thompson 
20700183ed61SJeremy L Thompson   CeedInt block_sizes[3] = {0, 0, 0};
20710183ed61SJeremy L Thompson   CeedInt num_elem;
20720183ed61SJeremy L Thompson 
20730183ed61SJeremy L Thompson   // Compile
20740183ed61SJeremy L Thompson   CeedCallBackend(CeedOperatorGetNumElements(op, &num_elem));
20750183ed61SJeremy L Thompson   CeedCallBackend(BlockGridCalculate_Hip_gen(max_dim, num_elem, data->max_P_1d, Q_1d, block_sizes));
20760183ed61SJeremy L Thompson   {
20770183ed61SJeremy L Thompson     bool is_compile_good = false;
20780183ed61SJeremy L Thompson 
20790183ed61SJeremy L Thompson     data->thread_1d = block_sizes[0];
20800183ed61SJeremy L Thompson     CeedCallBackend(CeedTryCompile_Hip(ceed, code.str().c_str(), &is_compile_good,
20810183ed61SJeremy L Thompson                                        is_full ? &data->module_assemble_full : &data->module_assemble_diagonal, 2, "OP_T_1D", block_sizes[0],
20820183ed61SJeremy L Thompson                                        "BLOCK_SIZE", block_sizes[0] * block_sizes[1] * block_sizes[2]));
20830183ed61SJeremy L Thompson     if (is_compile_good) {
20840183ed61SJeremy L Thompson       *is_good_build = true;
20850183ed61SJeremy L Thompson       CeedCallBackend(CeedGetKernel_Hip(ceed, is_full ? data->module_assemble_full : data->module_assemble_diagonal, operator_name.c_str(),
20860183ed61SJeremy L Thompson                                         is_full ? &data->assemble_full : &data->assemble_diagonal));
20870183ed61SJeremy L Thompson     } else {
20880183ed61SJeremy L Thompson       *is_good_build              = false;
20890183ed61SJeremy L Thompson       data->use_assembly_fallback = true;
20900183ed61SJeremy L Thompson     }
20910183ed61SJeremy L Thompson   }
20920183ed61SJeremy L Thompson   CeedCallBackend(CeedDestroy(&ceed));
20930183ed61SJeremy L Thompson   CeedCallBackend(CeedQFunctionDestroy(&qf));
20940183ed61SJeremy L Thompson   return CEED_ERROR_SUCCESS;
20950183ed61SJeremy L Thompson }
20960183ed61SJeremy L Thompson 
20970183ed61SJeremy L Thompson extern "C" int CeedOperatorBuildKernelDiagonalAssemblyAtPoints_Hip_gen(CeedOperator op, bool *is_good_build) {
20980183ed61SJeremy L Thompson   return CeedOperatorBuildKernelAssemblyAtPoints_Hip_gen(op, false, is_good_build);
20990183ed61SJeremy L Thompson }
21000183ed61SJeremy L Thompson 
2101692716b7SZach Atkins extern "C" int CeedOperatorBuildKernelFullAssemblyAtPoints_Hip_gen(CeedOperator op, bool *is_good_build) {
2102692716b7SZach Atkins   return CeedOperatorBuildKernelAssemblyAtPoints_Hip_gen(op, true, is_good_build);
2103692716b7SZach Atkins }
21045daefc96SJeremy L Thompson //------------------------------------------------------------------------------
21055daefc96SJeremy L Thompson // Build QFunction assembly operator kernel
21065daefc96SJeremy L Thompson //------------------------------------------------------------------------------
21075daefc96SJeremy L Thompson extern "C" int CeedOperatorBuildKernelLinearAssembleQFunction_Hip_gen(CeedOperator op, bool *is_good_build) {
21085daefc96SJeremy L Thompson   bool                   is_all_tensor = true, is_all_nontensor = true, is_at_points = false, use_3d_slices = false;
21095daefc96SJeremy L Thompson   Ceed                   ceed;
21105daefc96SJeremy L Thompson   CeedInt                Q, Q_1d, num_input_fields, num_output_fields, max_dim = 1, max_num_points = 0;
21115daefc96SJeremy L Thompson   CeedQFunctionField    *qf_input_fields, *qf_output_fields;
21125daefc96SJeremy L Thompson   CeedQFunction_Hip_gen *qf_data;
21135daefc96SJeremy L Thompson   CeedQFunction          qf;
21145daefc96SJeremy L Thompson   CeedOperatorField     *op_input_fields, *op_output_fields;
21155daefc96SJeremy L Thompson   CeedOperator_Hip_gen  *data;
21165daefc96SJeremy L Thompson   std::ostringstream     code;
21175daefc96SJeremy L Thompson   Tab                    tab;
21185daefc96SJeremy L Thompson 
21195daefc96SJeremy L Thompson   // Check compatibility
21205daefc96SJeremy L Thompson   CeedCallBackend(CeedOperatorGetCeed(op, &ceed));
21215daefc96SJeremy L Thompson   CeedCallBackend(CeedOperatorIsAtPoints(op, &is_at_points));
21225daefc96SJeremy L Thompson   CeedCheck(!is_at_points, ceed, CEED_ERROR_BACKEND, "AtPoints QFunction assembly is not supported");
21235daefc96SJeremy L Thompson 
21245daefc96SJeremy L Thompson   // Check field compatibility
21255daefc96SJeremy L Thompson   CeedCallBackend(CeedOperatorGetFields(op, &num_input_fields, &op_input_fields, &num_output_fields, &op_output_fields));
21265daefc96SJeremy L Thompson   {
21275daefc96SJeremy L Thompson     bool has_shared_bases = true;
21285daefc96SJeremy L Thompson 
21295daefc96SJeremy L Thompson     for (CeedInt i = 0; i < num_input_fields; i++) {
21305daefc96SJeremy L Thompson       CeedBasis basis;
21315daefc96SJeremy L Thompson 
21325daefc96SJeremy L Thompson       CeedCallBackend(CeedOperatorFieldGetBasis(op_input_fields[i], &basis));
21335daefc96SJeremy L Thompson       if (basis != CEED_BASIS_NONE) {
21345daefc96SJeremy L Thompson         bool        is_tensor = true;
21355daefc96SJeremy L Thompson         const char *resource;
21365daefc96SJeremy L Thompson         char       *resource_root;
21375daefc96SJeremy L Thompson         Ceed        basis_ceed;
21385daefc96SJeremy L Thompson 
21395daefc96SJeremy L Thompson         CeedCallBackend(CeedBasisIsTensor(basis, &is_tensor));
21405daefc96SJeremy L Thompson         is_all_tensor    = is_all_tensor && is_tensor;
21415daefc96SJeremy L Thompson         is_all_nontensor = is_all_nontensor && !is_tensor;
21425daefc96SJeremy L Thompson         CeedCallBackend(CeedBasisGetCeed(basis, &basis_ceed));
21435daefc96SJeremy L Thompson         CeedCallBackend(CeedGetResource(basis_ceed, &resource));
21445daefc96SJeremy L Thompson         CeedCallBackend(CeedGetResourceRoot(basis_ceed, resource, ":", &resource_root));
21455daefc96SJeremy L Thompson         has_shared_bases = has_shared_bases && !strcmp(resource_root, "/gpu/hip/shared");
21465daefc96SJeremy L Thompson         CeedCallBackend(CeedFree(&resource_root));
21475daefc96SJeremy L Thompson         CeedCallBackend(CeedDestroy(&basis_ceed));
21485daefc96SJeremy L Thompson       }
21495daefc96SJeremy L Thompson       CeedCallBackend(CeedBasisDestroy(&basis));
21505daefc96SJeremy L Thompson     }
21515daefc96SJeremy L Thompson 
21525daefc96SJeremy L Thompson     for (CeedInt i = 0; i < num_output_fields; i++) {
21535daefc96SJeremy L Thompson       CeedBasis basis;
21545daefc96SJeremy L Thompson 
21555daefc96SJeremy L Thompson       CeedCallBackend(CeedOperatorFieldGetBasis(op_output_fields[i], &basis));
21565daefc96SJeremy L Thompson       if (basis != CEED_BASIS_NONE) {
21575daefc96SJeremy L Thompson         bool        is_tensor = true;
21585daefc96SJeremy L Thompson         const char *resource;
21595daefc96SJeremy L Thompson         char       *resource_root;
21605daefc96SJeremy L Thompson         Ceed        basis_ceed;
21615daefc96SJeremy L Thompson 
21625daefc96SJeremy L Thompson         CeedCallBackend(CeedBasisIsTensor(basis, &is_tensor));
21635daefc96SJeremy L Thompson         is_all_tensor    = is_all_tensor && is_tensor;
21645daefc96SJeremy L Thompson         is_all_nontensor = is_all_nontensor && !is_tensor;
21655daefc96SJeremy L Thompson 
21665daefc96SJeremy L Thompson         CeedCallBackend(CeedBasisGetCeed(basis, &basis_ceed));
21675daefc96SJeremy L Thompson         CeedCallBackend(CeedGetResource(basis_ceed, &resource));
21685daefc96SJeremy L Thompson         CeedCallBackend(CeedGetResourceRoot(basis_ceed, resource, ":", &resource_root));
21695daefc96SJeremy L Thompson         has_shared_bases = has_shared_bases && !strcmp(resource_root, "/gpu/hip/shared");
21705daefc96SJeremy L Thompson         CeedCallBackend(CeedFree(&resource_root));
21715daefc96SJeremy L Thompson         CeedCallBackend(CeedDestroy(&basis_ceed));
21725daefc96SJeremy L Thompson       }
21735daefc96SJeremy L Thompson       CeedCallBackend(CeedBasisDestroy(&basis));
21745daefc96SJeremy L Thompson     }
21755daefc96SJeremy L Thompson   }
21765daefc96SJeremy L Thompson 
21775daefc96SJeremy L Thompson   // Retrieve operator data
21785daefc96SJeremy L Thompson   CeedCallBackend(CeedOperatorGetData(op, &data));
21795daefc96SJeremy L Thompson   Q       = data->Q;
21805daefc96SJeremy L Thompson   Q_1d    = data->Q_1d;
21815daefc96SJeremy L Thompson   max_dim = data->dim;
21825daefc96SJeremy L Thompson   CeedCallBackend(CeedOperatorGetQFunction(op, &qf));
21835daefc96SJeremy L Thompson   CeedCallBackend(CeedQFunctionGetData(qf, &qf_data));
21845daefc96SJeremy L Thompson   CeedCallBackend(CeedQFunctionGetFields(qf, NULL, &qf_input_fields, NULL, &qf_output_fields));
21855daefc96SJeremy L Thompson 
21865daefc96SJeremy L Thompson   // Load basis source files
21875daefc96SJeremy L Thompson   if (!is_all_nontensor) {
21885daefc96SJeremy L Thompson     code << tab << "// Tensor basis source\n";
21895daefc96SJeremy L Thompson     code << tab << "#include <ceed/jit-source/hip/hip-shared-basis-tensor-templates.h>\n\n";
21905daefc96SJeremy L Thompson   }
21915daefc96SJeremy L Thompson   if (!is_all_tensor) {
21925daefc96SJeremy L Thompson     code << tab << "// Non-tensor basis source\n";
21935daefc96SJeremy L Thompson     code << tab << "#include <ceed/jit-source/hip/hip-shared-basis-nontensor-templates.h>\n\n";
21945daefc96SJeremy L Thompson   }
21955daefc96SJeremy L Thompson   if (!is_all_tensor && !is_all_nontensor) {
21965daefc96SJeremy L Thompson     code << "// Tensor basis source\n";
21975daefc96SJeremy L Thompson     code << "#include <ceed/jit-source/hip/hip-shared-basis-tensor-flattened-templates.h>\n\n";
21985daefc96SJeremy L Thompson   }
21995daefc96SJeremy L Thompson   code << "// CodeGen operator source\n";
22005daefc96SJeremy L Thompson   code << "#include <ceed/jit-source/hip/hip-gen-templates.h>\n\n";
22015daefc96SJeremy L Thompson 
22025daefc96SJeremy L Thompson   // Get QFunction name
22035daefc96SJeremy L Thompson   std::string qfunction_name(qf_data->qfunction_name);
22045daefc96SJeremy L Thompson   std::string operator_name;
22055daefc96SJeremy L Thompson 
22065daefc96SJeremy L Thompson   operator_name = "CeedKernelHipGenQFunctionAssembly_" + qfunction_name;
22075daefc96SJeremy L Thompson 
22085daefc96SJeremy L Thompson   // Define CEED_Q_VLA
22095daefc96SJeremy L Thompson   code << "\n" << tab << "#undef CEED_Q_VLA\n";
22105daefc96SJeremy L Thompson   if (max_dim != 3 || is_at_points || use_3d_slices || !is_all_tensor) {
22115daefc96SJeremy L Thompson     code << tab << "#define CEED_Q_VLA 1\n\n";
22125daefc96SJeremy L Thompson   } else {
22135daefc96SJeremy L Thompson     code << tab << "#define CEED_Q_VLA " << Q_1d << "\n\n";
22145daefc96SJeremy L Thompson   }
22155daefc96SJeremy L Thompson 
22165daefc96SJeremy L Thompson   // Add user QFunction source
22175daefc96SJeremy L Thompson   {
22185daefc96SJeremy L Thompson     const char *source_path;
22195daefc96SJeremy L Thompson 
22205daefc96SJeremy L Thompson     CeedCallBackend(CeedQFunctionGetSourcePath(qf, &source_path));
22215daefc96SJeremy L Thompson     CeedCheck(source_path, ceed, CEED_ERROR_UNSUPPORTED, "/gpu/hip/gen backend requires QFunction source code file");
22225daefc96SJeremy L Thompson 
22235daefc96SJeremy L Thompson     code << tab << "// User QFunction source\n";
22245daefc96SJeremy L Thompson     code << tab << "#include \"" << source_path << "\"\n\n";
22255daefc96SJeremy L Thompson   }
22265daefc96SJeremy L Thompson 
22275daefc96SJeremy L Thompson   // Setup
22285daefc96SJeremy L Thompson   code << "\n" << tab << "// -----------------------------------------------------------------------------\n";
22295daefc96SJeremy L Thompson   code << tab << "// Operator Assembly Kernel\n";
22305daefc96SJeremy L Thompson   code << tab << "// \n";
22315daefc96SJeremy L Thompson   code << tab << "// d_[in,out]_i:   CeedVector device array\n";
22325daefc96SJeremy L Thompson   code << tab << "// r_[in,out]_e_i: Element vector register\n";
22335daefc96SJeremy L Thompson   code << tab << "// r_[in,out]_q_i: Quadrature space vector register\n";
22345daefc96SJeremy L Thompson   code << tab << "// r_[in,out]_c_i: AtPoints Chebyshev coefficients register\n";
22355daefc96SJeremy L Thompson   code << tab << "// r_[in,out]_s_i: Quadrature space slice vector register\n";
22365daefc96SJeremy L Thompson   code << tab << "// \n";
22375daefc96SJeremy L Thompson   code << tab << "// s_B_[in,out]_i: Interpolation matrix, shared memory\n";
22385daefc96SJeremy L Thompson   code << tab << "// s_G_[in,out]_i: Gradient matrix, shared memory\n";
22395daefc96SJeremy L Thompson   code << tab << "// -----------------------------------------------------------------------------\n";
22405daefc96SJeremy L Thompson   code << tab << "extern \"C\" __global__ void " << operator_name
22415daefc96SJeremy L Thompson        << "(CeedInt num_elem, void* ctx, FieldsInt_Hip indices, Fields_Hip fields, Fields_Hip B, Fields_Hip G, CeedScalar *W, Points_Hip "
22425daefc96SJeremy L Thompson           "points, CeedScalar *__restrict__ values_array) {\n";
22435daefc96SJeremy L Thompson   tab.push();
22445daefc96SJeremy L Thompson 
22455daefc96SJeremy L Thompson   // Scratch buffers
22465daefc96SJeremy L Thompson   for (CeedInt i = 0; i < num_input_fields; i++) {
22475daefc96SJeremy L Thompson     CeedEvalMode eval_mode;
22485daefc96SJeremy L Thompson 
22495daefc96SJeremy L Thompson     CeedCallBackend(CeedQFunctionFieldGetEvalMode(qf_input_fields[i], &eval_mode));
22505daefc96SJeremy L Thompson     if (eval_mode != CEED_EVAL_WEIGHT) {  // Skip CEED_EVAL_WEIGHT
22515daefc96SJeremy L Thompson       code << tab << "const CeedScalar *__restrict__ d_in_" << i << " = fields.inputs[" << i << "];\n";
22525daefc96SJeremy L Thompson     }
22535daefc96SJeremy L Thompson   }
22545daefc96SJeremy L Thompson   for (CeedInt i = 0; i < num_output_fields; i++) {
22555daefc96SJeremy L Thompson     code << tab << "CeedScalar *__restrict__ d_out_" << i << " = fields.outputs[" << i << "];\n";
22565daefc96SJeremy L Thompson   }
22575daefc96SJeremy L Thompson 
22585daefc96SJeremy L Thompson   code << tab << "const CeedInt max_dim = " << max_dim << ";\n";
22595daefc96SJeremy L Thompson   if (!is_all_tensor) {
22605daefc96SJeremy L Thompson     code << tab << "const CeedInt Q = " << Q << ";\n";
22615daefc96SJeremy L Thompson   }
22625daefc96SJeremy L Thompson   if (!is_all_nontensor) {
22635daefc96SJeremy L Thompson     code << tab << "const CeedInt Q_1d = " << Q_1d << ";\n";
22645daefc96SJeremy L Thompson   }
22655daefc96SJeremy L Thompson 
22665daefc96SJeremy L Thompson   // Shared data
22675daefc96SJeremy L Thompson   code << tab << "extern __shared__ CeedScalar slice[];\n";
22685daefc96SJeremy L Thompson   code << tab << "SharedData_Hip data;\n";
22695daefc96SJeremy L Thompson   code << tab << "data.t_id_x = threadIdx.x;\n";
22705daefc96SJeremy L Thompson   code << tab << "data.t_id_y = threadIdx.y;\n";
22715daefc96SJeremy L Thompson   code << tab << "data.t_id_z = threadIdx.z;\n";
22725daefc96SJeremy L Thompson   code << tab << "data.t_id   = threadIdx.x + threadIdx.y*blockDim.x + threadIdx.z*blockDim.y*blockDim.x;\n";
22735daefc96SJeremy L Thompson   code << tab << "data.slice  = slice + data.t_id_z*OP_T_1D" << ((!is_all_tensor || max_dim == 1) ? "" : "*OP_T_1D") << ";\n";
22745daefc96SJeremy L Thompson 
22755daefc96SJeremy L Thompson   // -- Determine input mat reuse
22765daefc96SJeremy L Thompson   FieldReuse_Hip input_matrix_reuse[CEED_FIELD_MAX];
22775daefc96SJeremy L Thompson 
22785daefc96SJeremy L Thompson   for (CeedInt i = 0; i < num_input_fields; i++) {
22795daefc96SJeremy L Thompson     input_matrix_reuse[i].index = -1;
22805daefc96SJeremy L Thompson   }
22815daefc96SJeremy L Thompson   for (CeedInt i = 0; i < num_input_fields; i++) {
22825daefc96SJeremy L Thompson     bool         is_tensor = true;
22835daefc96SJeremy L Thompson     CeedEvalMode eval_mode_i;
22845daefc96SJeremy L Thompson     CeedBasis    basis_i;
22855daefc96SJeremy L Thompson 
22865daefc96SJeremy L Thompson     CeedCallBackend(CeedQFunctionFieldGetEvalMode(qf_input_fields[i], &eval_mode_i));
22875daefc96SJeremy L Thompson     if (eval_mode_i == CEED_EVAL_WEIGHT) continue;
22885daefc96SJeremy L Thompson     CeedCallBackend(CeedOperatorFieldGetBasis(op_input_fields[i], &basis_i));
22895daefc96SJeremy L Thompson     CeedCallBackend(CeedBasisIsTensor(basis_i, &is_tensor));
22905daefc96SJeremy L Thompson     for (CeedInt j = 0; (input_matrix_reuse[i].index == -1) && (j < i); j++) {
22915daefc96SJeremy L Thompson       CeedEvalMode eval_mode_j;
22925daefc96SJeremy L Thompson       CeedBasis    basis_j;
22935daefc96SJeremy L Thompson 
22945daefc96SJeremy L Thompson       CeedCallBackend(CeedQFunctionFieldGetEvalMode(qf_input_fields[j], &eval_mode_j));
22955daefc96SJeremy L Thompson       if (eval_mode_j == CEED_EVAL_WEIGHT) continue;
22965daefc96SJeremy L Thompson       CeedCallBackend(CeedOperatorFieldGetBasis(op_input_fields[j], &basis_j));
22975daefc96SJeremy L Thompson       if (basis_i == basis_j) {
22985daefc96SJeremy L Thompson         if (is_tensor) {
22995daefc96SJeremy L Thompson           input_matrix_reuse[i].index     = j;
23005daefc96SJeremy L Thompson           input_matrix_reuse[i].is_input  = true;
23015daefc96SJeremy L Thompson           input_matrix_reuse[i].eval_mode = eval_mode_j;
23025daefc96SJeremy L Thompson         } else {
23035daefc96SJeremy L Thompson           // For non-tensor can only re-use with the same eval mode
23045daefc96SJeremy L Thompson           if (eval_mode_i == eval_mode_j) {
23055daefc96SJeremy L Thompson             input_matrix_reuse[i].index     = j;
23065daefc96SJeremy L Thompson             input_matrix_reuse[i].is_input  = true;
23075daefc96SJeremy L Thompson             input_matrix_reuse[i].eval_mode = eval_mode_j;
23085daefc96SJeremy L Thompson           }
23095daefc96SJeremy L Thompson         }
23105daefc96SJeremy L Thompson       }
23115daefc96SJeremy L Thompson       CeedCallBackend(CeedBasisDestroy(&basis_j));
23125daefc96SJeremy L Thompson     }
23135daefc96SJeremy L Thompson     CeedCallBackend(CeedBasisDestroy(&basis_i));
23145daefc96SJeremy L Thompson   }
23155daefc96SJeremy L Thompson 
23165daefc96SJeremy L Thompson   // -- Determine output mat reuse
23175daefc96SJeremy L Thompson   FieldReuse_Hip output_matrix_reuse[CEED_FIELD_MAX];
23185daefc96SJeremy L Thompson 
23195daefc96SJeremy L Thompson   for (CeedInt i = 0; i < num_output_fields; i++) {
23205daefc96SJeremy L Thompson     output_matrix_reuse[i].index = -1;
23215daefc96SJeremy L Thompson   }
23225daefc96SJeremy L Thompson   for (CeedInt i = 0; i < num_output_fields; i++) {
23235daefc96SJeremy L Thompson     bool         is_tensor = true;
23245daefc96SJeremy L Thompson     CeedEvalMode eval_mode_i;
23255daefc96SJeremy L Thompson     CeedBasis    basis_i;
23265daefc96SJeremy L Thompson 
23275daefc96SJeremy L Thompson     CeedCallBackend(CeedQFunctionFieldGetEvalMode(qf_output_fields[i], &eval_mode_i));
23285daefc96SJeremy L Thompson     CeedCallBackend(CeedOperatorFieldGetBasis(op_output_fields[i], &basis_i));
23295daefc96SJeremy L Thompson     CeedCallBackend(CeedBasisIsTensor(basis_i, &is_tensor));
23305daefc96SJeremy L Thompson     for (CeedInt j = 0; (output_matrix_reuse[i].index == -1) && (j < num_input_fields); j++) {
23315daefc96SJeremy L Thompson       CeedEvalMode eval_mode_j;
23325daefc96SJeremy L Thompson       CeedBasis    basis_j;
23335daefc96SJeremy L Thompson 
23345daefc96SJeremy L Thompson       CeedCallBackend(CeedQFunctionFieldGetEvalMode(qf_input_fields[j], &eval_mode_j));
23355daefc96SJeremy L Thompson       if (eval_mode_j == CEED_EVAL_WEIGHT) continue;
23365daefc96SJeremy L Thompson       CeedCallBackend(CeedOperatorFieldGetBasis(op_input_fields[j], &basis_j));
23375daefc96SJeremy L Thompson       if (basis_i == basis_j) {
23385daefc96SJeremy L Thompson         if (is_tensor) {
23395daefc96SJeremy L Thompson           output_matrix_reuse[i].index     = j;
23405daefc96SJeremy L Thompson           output_matrix_reuse[i].is_input  = true;
23415daefc96SJeremy L Thompson           output_matrix_reuse[i].eval_mode = eval_mode_j;
23425daefc96SJeremy L Thompson         } else {
23435daefc96SJeremy L Thompson           // For non-tensor can only re-use with the same eval mode
23445daefc96SJeremy L Thompson           if (eval_mode_i == eval_mode_j) {
23455daefc96SJeremy L Thompson             output_matrix_reuse[i].index     = j;
23465daefc96SJeremy L Thompson             output_matrix_reuse[i].is_input  = true;
23475daefc96SJeremy L Thompson             output_matrix_reuse[i].eval_mode = eval_mode_j;
23485daefc96SJeremy L Thompson           }
23495daefc96SJeremy L Thompson         }
23505daefc96SJeremy L Thompson       }
23515daefc96SJeremy L Thompson       CeedCallBackend(CeedBasisDestroy(&basis_j));
23525daefc96SJeremy L Thompson     }
23535daefc96SJeremy L Thompson     for (CeedInt j = 0; (output_matrix_reuse[i].index == -1) && (j < i); j++) {
23545daefc96SJeremy L Thompson       CeedEvalMode eval_mode_j;
23555daefc96SJeremy L Thompson       CeedBasis    basis_j;
23565daefc96SJeremy L Thompson 
23575daefc96SJeremy L Thompson       CeedCallBackend(CeedQFunctionFieldGetEvalMode(qf_output_fields[j], &eval_mode_j));
23585daefc96SJeremy L Thompson       if (eval_mode_j == CEED_EVAL_WEIGHT) continue;
23595daefc96SJeremy L Thompson       CeedCallBackend(CeedOperatorFieldGetBasis(op_output_fields[j], &basis_j));
23605daefc96SJeremy L Thompson       if (basis_i == basis_j) {
23615daefc96SJeremy L Thompson         if (is_tensor) {
23625daefc96SJeremy L Thompson           output_matrix_reuse[i].index     = j;
23635daefc96SJeremy L Thompson           output_matrix_reuse[i].is_input  = false;
23645daefc96SJeremy L Thompson           output_matrix_reuse[i].eval_mode = eval_mode_j;
23655daefc96SJeremy L Thompson         } else {
23665daefc96SJeremy L Thompson           // For non-tensor can only re-use with the same eval mode
23675daefc96SJeremy L Thompson           if (eval_mode_i == eval_mode_j) {
23685daefc96SJeremy L Thompson             output_matrix_reuse[i].index     = j;
23695daefc96SJeremy L Thompson             output_matrix_reuse[i].is_input  = false;
23705daefc96SJeremy L Thompson             output_matrix_reuse[i].eval_mode = eval_mode_j;
23715daefc96SJeremy L Thompson           }
23725daefc96SJeremy L Thompson         }
23735daefc96SJeremy L Thompson       }
23745daefc96SJeremy L Thompson       CeedCallBackend(CeedBasisDestroy(&basis_j));
23755daefc96SJeremy L Thompson     }
23765daefc96SJeremy L Thompson     CeedCallBackend(CeedBasisDestroy(&basis_i));
23775daefc96SJeremy L Thompson   }
23785daefc96SJeremy L Thompson 
23795daefc96SJeremy L Thompson   // Initialize constants, and matrices B and G
23805daefc96SJeremy L Thompson   code << "\n" << tab << "// Input field constants and basis data\n";
23815daefc96SJeremy L Thompson   for (CeedInt i = 0; i < num_input_fields; i++) {
23825daefc96SJeremy L Thompson     CeedCallBackend(CeedOperatorBuildKernelFieldData_Hip_gen(code, data, tab, i, op_input_fields[i], qf_input_fields[i], input_matrix_reuse[i],
23835daefc96SJeremy L Thompson                                                              max_dim, Q, Q_1d, true, is_all_tensor, is_at_points, use_3d_slices));
23845daefc96SJeremy L Thompson   }
23855daefc96SJeremy L Thompson   code << "\n" << tab << "// Output field constants and basis data\n";
23865daefc96SJeremy L Thompson   for (CeedInt i = 0; i < num_output_fields; i++) {
23875daefc96SJeremy L Thompson     CeedCallBackend(CeedOperatorBuildKernelFieldData_Hip_gen(code, data, tab, i, op_output_fields[i], qf_output_fields[i], output_matrix_reuse[i],
23885daefc96SJeremy L Thompson                                                              max_dim, Q, Q_1d, false, is_all_tensor, is_at_points, use_3d_slices));
23895daefc96SJeremy L Thompson   }
23905daefc96SJeremy L Thompson 
23915daefc96SJeremy L Thompson   // Loop over all elements
23925daefc96SJeremy L Thompson   code << "\n" << tab << "// Element loop\n";
23935daefc96SJeremy L Thompson   code << tab << "__syncthreads();\n";
23945daefc96SJeremy L Thompson   code << tab << "for (CeedInt elem = blockIdx.x*blockDim.z + threadIdx.z; elem < num_elem; elem += gridDim.x*blockDim.z) {\n";
23955daefc96SJeremy L Thompson   tab.push();
23965daefc96SJeremy L Thompson 
23975daefc96SJeremy L Thompson   // -- Compute minimum buffer space needed
23985daefc96SJeremy L Thompson   CeedInt max_rstr_buffer_size = 1;
23995daefc96SJeremy L Thompson 
24005daefc96SJeremy L Thompson   for (CeedInt i = 0; i < num_input_fields; i++) {
24015daefc96SJeremy L Thompson     CeedEvalMode eval_mode;
24025daefc96SJeremy L Thompson 
24035daefc96SJeremy L Thompson     CeedCallBackend(CeedQFunctionFieldGetEvalMode(qf_input_fields[i], &eval_mode));
24045daefc96SJeremy L Thompson     if (eval_mode != CEED_EVAL_NONE && eval_mode != CEED_EVAL_WEIGHT) {
24055daefc96SJeremy L Thompson       CeedInt             num_comp;
24065daefc96SJeremy L Thompson       CeedElemRestriction elem_rstr;
24075daefc96SJeremy L Thompson 
24085daefc96SJeremy L Thompson       CeedCallBackend(CeedOperatorFieldGetElemRestriction(op_input_fields[i], &elem_rstr));
24095daefc96SJeremy L Thompson       CeedCallBackend(CeedElemRestrictionGetNumComponents(elem_rstr, &num_comp));
24105daefc96SJeremy L Thompson       max_rstr_buffer_size = CeedIntMax(max_rstr_buffer_size, num_comp * (is_all_tensor && (max_dim >= 3) ? Q_1d : 1));
24115daefc96SJeremy L Thompson       CeedCallBackend(CeedElemRestrictionDestroy(&elem_rstr));
24125daefc96SJeremy L Thompson     }
24135daefc96SJeremy L Thompson   }
24145daefc96SJeremy L Thompson   for (CeedInt i = 0; i < num_output_fields; i++) {
24155daefc96SJeremy L Thompson     CeedEvalMode eval_mode;
24165daefc96SJeremy L Thompson 
24175daefc96SJeremy L Thompson     CeedCallBackend(CeedQFunctionFieldGetEvalMode(qf_output_fields[i], &eval_mode));
24185daefc96SJeremy L Thompson     if (eval_mode != CEED_EVAL_NONE) {
24195daefc96SJeremy L Thompson       CeedInt             num_comp;
24205daefc96SJeremy L Thompson       CeedElemRestriction elem_rstr;
24215daefc96SJeremy L Thompson 
24225daefc96SJeremy L Thompson       CeedCallBackend(CeedOperatorFieldGetElemRestriction(op_output_fields[i], &elem_rstr));
24235daefc96SJeremy L Thompson       CeedCallBackend(CeedElemRestrictionGetNumComponents(elem_rstr, &num_comp));
24245daefc96SJeremy L Thompson       max_rstr_buffer_size = CeedIntMax(max_rstr_buffer_size, num_comp * (is_all_tensor && (max_dim >= 3) ? Q_1d : 1));
24255daefc96SJeremy L Thompson       CeedCallBackend(CeedElemRestrictionDestroy(&elem_rstr));
24265daefc96SJeremy L Thompson     }
24275daefc96SJeremy L Thompson   }
24285daefc96SJeremy L Thompson   code << tab << "// Scratch restriction buffer space\n";
24295daefc96SJeremy L Thompson   code << tab << "CeedScalar r_e_scratch[" << max_rstr_buffer_size << "];\n";
24305daefc96SJeremy L Thompson 
24315daefc96SJeremy L Thompson   // -- Determine best input field processing order
24325daefc96SJeremy L Thompson   CeedInt field_rstr_in_buffer[CEED_FIELD_MAX], input_field_order[CEED_FIELD_MAX];
24335daefc96SJeremy L Thompson 
24345daefc96SJeremy L Thompson   for (CeedInt i = 0; i < num_input_fields; i++) {
24355daefc96SJeremy L Thompson     field_rstr_in_buffer[i] = -1;
24365daefc96SJeremy L Thompson     input_field_order[i]    = -1;
24375daefc96SJeremy L Thompson   }
24385daefc96SJeremy L Thompson   {
24395daefc96SJeremy L Thompson     bool    is_ordered[CEED_FIELD_MAX];
24405daefc96SJeremy L Thompson     CeedInt curr_index = 0;
24415daefc96SJeremy L Thompson 
24425daefc96SJeremy L Thompson     for (CeedInt i = 0; i < num_input_fields; i++) is_ordered[i] = false;
24435daefc96SJeremy L Thompson     for (CeedInt i = 0; i < num_input_fields; i++) {
24445daefc96SJeremy L Thompson       CeedVector          vec_i;
24455daefc96SJeremy L Thompson       CeedElemRestriction rstr_i;
24465daefc96SJeremy L Thompson 
24475daefc96SJeremy L Thompson       if (is_ordered[i]) continue;
24485daefc96SJeremy L Thompson       field_rstr_in_buffer[i]       = i;
24495daefc96SJeremy L Thompson       is_ordered[i]                 = true;
24505daefc96SJeremy L Thompson       input_field_order[curr_index] = i;
24515daefc96SJeremy L Thompson       curr_index++;
24525daefc96SJeremy L Thompson       CeedCallBackend(CeedOperatorFieldGetVector(op_input_fields[i], &vec_i));
24535daefc96SJeremy L Thompson       if (vec_i == CEED_VECTOR_NONE) continue;  // CEED_EVAL_WEIGHT
24545daefc96SJeremy L Thompson       CeedCallBackend(CeedOperatorFieldGetElemRestriction(op_input_fields[i], &rstr_i));
24555daefc96SJeremy L Thompson       for (CeedInt j = i + 1; j < num_input_fields; j++) {
24565daefc96SJeremy L Thompson         CeedVector          vec_j;
24575daefc96SJeremy L Thompson         CeedElemRestriction rstr_j;
24585daefc96SJeremy L Thompson 
24595daefc96SJeremy L Thompson         CeedCallBackend(CeedOperatorFieldGetVector(op_input_fields[j], &vec_j));
24605daefc96SJeremy L Thompson         CeedCallBackend(CeedOperatorFieldGetElemRestriction(op_input_fields[j], &rstr_j));
24615daefc96SJeremy L Thompson         if (rstr_i == rstr_j && vec_i == vec_j) {
24625daefc96SJeremy L Thompson           field_rstr_in_buffer[j]       = i;
24635daefc96SJeremy L Thompson           is_ordered[j]                 = true;
24645daefc96SJeremy L Thompson           input_field_order[curr_index] = j;
24655daefc96SJeremy L Thompson           curr_index++;
24665daefc96SJeremy L Thompson         }
24675daefc96SJeremy L Thompson         CeedCallBackend(CeedVectorDestroy(&vec_j));
24685daefc96SJeremy L Thompson         CeedCallBackend(CeedElemRestrictionDestroy(&rstr_j));
24695daefc96SJeremy L Thompson       }
24705daefc96SJeremy L Thompson       CeedCallBackend(CeedVectorDestroy(&vec_i));
24715daefc96SJeremy L Thompson       CeedCallBackend(CeedElemRestrictionDestroy(&rstr_i));
24725daefc96SJeremy L Thompson     }
24735daefc96SJeremy L Thompson   }
24745daefc96SJeremy L Thompson 
24755daefc96SJeremy L Thompson   // -- Input restriction and basis
24765daefc96SJeremy L Thompson   code << "\n" << tab << "// -- Input field restrictions and basis actions\n";
24775daefc96SJeremy L Thompson   CeedInt num_active_in = 0, num_active_out = 0, qf_assembly_size_out = 0;
24785daefc96SJeremy L Thompson   CeedInt active_fields_in[CEED_FIELD_MAX], active_fields_out[CEED_FIELD_MAX];
24795daefc96SJeremy L Thompson 
24805daefc96SJeremy L Thompson   for (CeedInt i = 0; i < num_input_fields; i++) {
24815daefc96SJeremy L Thompson     bool          is_active = false;
24825daefc96SJeremy L Thompson     const char   *field_name;
24835daefc96SJeremy L Thompson     const CeedInt f = input_field_order[i];
24845daefc96SJeremy L Thompson 
24855daefc96SJeremy L Thompson     {
24865daefc96SJeremy L Thompson       CeedVector vec;
24875daefc96SJeremy L Thompson 
24885daefc96SJeremy L Thompson       CeedCallBackend(CeedOperatorFieldGetVector(op_input_fields[f], &vec));
24895daefc96SJeremy L Thompson       is_active = vec == CEED_VECTOR_ACTIVE;
24905daefc96SJeremy L Thompson       CeedCallBackend(CeedVectorDestroy(&vec));
24915daefc96SJeremy L Thompson     }
24925daefc96SJeremy L Thompson 
24935daefc96SJeremy L Thompson     CeedCallBackend(CeedOperatorFieldGetName(op_input_fields[f], &field_name));
24945daefc96SJeremy L Thompson     code << tab << "// ---- Input field " << f << ": " << field_name << "\n";
24955daefc96SJeremy L Thompson 
24965daefc96SJeremy L Thompson     if (is_active) {
24975daefc96SJeremy L Thompson       CeedEvalMode eval_mode;
24985daefc96SJeremy L Thompson       CeedInt      field_size;
24995daefc96SJeremy L Thompson 
25005daefc96SJeremy L Thompson       active_fields_in[num_active_in] = f;
25015daefc96SJeremy L Thompson       num_active_in++;
25025daefc96SJeremy L Thompson       CeedCallBackend(CeedQFunctionFieldGetSize(qf_input_fields[f], &field_size));
25035daefc96SJeremy L Thompson       CeedCallBackend(CeedQFunctionFieldGetEvalMode(qf_input_fields[f], &eval_mode));
25045daefc96SJeremy L Thompson       if (eval_mode == CEED_EVAL_GRAD) {
25055daefc96SJeremy L Thompson         code << tab << "CeedScalar r_q_in_" << f << "[num_comp_in_" << f << "*" << "dim_in_" << f << "*"
25065daefc96SJeremy L Thompson              << (is_all_tensor && (max_dim >= 3) ? "Q_1d" : "1") << "] = {0.};\n";
25075daefc96SJeremy L Thompson       } else {
25085daefc96SJeremy L Thompson         code << tab << "CeedScalar r_q_in_" << f << "[num_comp_in_" << f << "*" << (is_all_tensor && (max_dim >= 3) ? "Q_1d" : "1") << "] = {0.};\n";
25095daefc96SJeremy L Thompson       }
25105daefc96SJeremy L Thompson       code << tab << "const CeedInt field_size_in_" << f << " = " << field_size << ";\n";
25115daefc96SJeremy L Thompson     } else {
25125daefc96SJeremy L Thompson       // ---- Restriction
25135daefc96SJeremy L Thompson       CeedCallBackend(CeedOperatorBuildKernelRestriction_Hip_gen(code, data, tab, f, field_rstr_in_buffer, op_input_fields[f], qf_input_fields[f],
25145daefc96SJeremy L Thompson                                                                  max_dim, Q_1d, true, is_all_tensor, is_at_points, use_3d_slices));
25155daefc96SJeremy L Thompson 
25165daefc96SJeremy L Thompson       // ---- Basis action
25175daefc96SJeremy L Thompson       CeedCallBackend(CeedOperatorBuildKernelBasis_Hip_gen(code, data, tab, f, op_input_fields[f], qf_input_fields[f], max_dim, Q_1d, true,
25185daefc96SJeremy L Thompson                                                            is_all_tensor, is_at_points, use_3d_slices));
25195daefc96SJeremy L Thompson     }
25205daefc96SJeremy L Thompson   }
25215daefc96SJeremy L Thompson   code << tab << "const CeedInt field_sizes_in[" << num_active_in << "] = {";
25225daefc96SJeremy L Thompson   for (CeedInt i = 0; i < num_active_in; i++) {
25235daefc96SJeremy L Thompson     code << "field_size_in_" << active_fields_in[i] << (i < num_active_in - 1 ? ", " : "");
25245daefc96SJeremy L Thompson   }
25255daefc96SJeremy L Thompson   code << "};\n";
25265daefc96SJeremy L Thompson   code << tab << "CeedScalar * r_q_in[" << num_active_in << "] = {";
25275daefc96SJeremy L Thompson   for (CeedInt i = 0; i < num_active_in; i++) {
25285daefc96SJeremy L Thompson     code << "r_q_in_" << active_fields_in[i] << (i < num_active_in - 1 ? ", " : "");
25295daefc96SJeremy L Thompson   }
25305daefc96SJeremy L Thompson   code << "};\n";
25315daefc96SJeremy L Thompson 
25325daefc96SJeremy L Thompson   for (CeedInt i = 0; i < num_output_fields; i++) {
25335daefc96SJeremy L Thompson     bool is_active = false;
25345daefc96SJeremy L Thompson 
25355daefc96SJeremy L Thompson     {
25365daefc96SJeremy L Thompson       CeedVector vec;
25375daefc96SJeremy L Thompson 
25385daefc96SJeremy L Thompson       CeedCallBackend(CeedOperatorFieldGetVector(op_output_fields[i], &vec));
25395daefc96SJeremy L Thompson       is_active = vec == CEED_VECTOR_ACTIVE;
25405daefc96SJeremy L Thompson       CeedCallBackend(CeedVectorDestroy(&vec));
25415daefc96SJeremy L Thompson     }
25425daefc96SJeremy L Thompson     if (is_active) {
25435daefc96SJeremy L Thompson       const char *field_name;
25445daefc96SJeremy L Thompson       CeedInt     field_size;
25455daefc96SJeremy L Thompson 
25465daefc96SJeremy L Thompson       active_fields_out[num_active_out] = i;
25475daefc96SJeremy L Thompson       num_active_out++;
25485daefc96SJeremy L Thompson       CeedCallBackend(CeedQFunctionFieldGetSize(qf_output_fields[i], &field_size));
25495daefc96SJeremy L Thompson       qf_assembly_size_out += field_size;
25505daefc96SJeremy L Thompson       CeedCallBackend(CeedOperatorFieldGetName(op_output_fields[i], &field_name));
25515daefc96SJeremy L Thompson       code << tab << "// ---- Output field " << i << ": " << field_name << "\n";
25525daefc96SJeremy L Thompson       code << tab << "const CeedInt field_size_out_" << i << " = " << field_size << ";\n";
25535daefc96SJeremy L Thompson     }
25545daefc96SJeremy L Thompson   }
25555daefc96SJeremy L Thompson   code << tab << "const CeedInt field_sizes_out[" << num_active_out << "] = {";
25565daefc96SJeremy L Thompson   for (CeedInt i = 0; i < num_active_out; i++) {
25575daefc96SJeremy L Thompson     code << "field_size_out_" << active_fields_out[i] << (i < num_active_out - 1 ? ", " : "");
25585daefc96SJeremy L Thompson   }
25595daefc96SJeremy L Thompson   code << "};\n";
25605daefc96SJeremy L Thompson   code << tab << "const CeedInt total_size_out = " << qf_assembly_size_out << ";\n";
25615daefc96SJeremy L Thompson 
25625daefc96SJeremy L Thompson   // -- Loop over active field
25635daefc96SJeremy L Thompson   code << "\n" << tab << "CeedInt input_offset = 0;\n";
25645daefc96SJeremy L Thompson   code << tab << "// Loop over active QFunction input fields\n";
25655daefc96SJeremy L Thompson   code << tab << "const CeedInt num_active_in = " << num_active_in << ";\n";
25665daefc96SJeremy L Thompson   code << tab << "for (CeedInt a = 0; a < num_active_in; a++) {\n";
25675daefc96SJeremy L Thompson   tab.push();
25685daefc96SJeremy L Thompson 
25695daefc96SJeremy L Thompson   // -- Loop over size of active field
25705daefc96SJeremy L Thompson   code << "\n" << tab << "// Loop over current active input field size\n";
25715daefc96SJeremy L Thompson   code << tab << "const CeedInt field_size_in = field_sizes_in[a];\n";
25725daefc96SJeremy L Thompson   code << tab << "for (CeedInt s = 0; s < field_size_in; s++) {\n";
25735daefc96SJeremy L Thompson   tab.push();
25745daefc96SJeremy L Thompson 
25755daefc96SJeremy L Thompson   // -- Set current active point and component to 1
25765daefc96SJeremy L Thompson   code << tab << "// Set current active point and component to 1.0\n";
25775daefc96SJeremy L Thompson   if (is_all_tensor && (max_dim >= 3)) {
25785daefc96SJeremy L Thompson     code << tab << "for (CeedInt i = 0; i < Q_1d; i++) r_q_in[a][i + s * Q_1d] = 1.0;\n";
25795daefc96SJeremy L Thompson   } else {
25805daefc96SJeremy L Thompson     code << tab << "r_q_in[a][s] = 1.0;\n";
25815daefc96SJeremy L Thompson   }
25825daefc96SJeremy L Thompson 
25835daefc96SJeremy L Thompson   // -- Q function
25845daefc96SJeremy L Thompson   CeedCallBackend(CeedOperatorBuildKernelQFunction_Hip_gen(code, data, tab, max_dim, max_num_points, num_input_fields, op_input_fields,
25855daefc96SJeremy L Thompson                                                            qf_input_fields, num_output_fields, op_output_fields, qf_output_fields, qfunction_name,
25865daefc96SJeremy L Thompson                                                            Q_1d, is_all_tensor, is_at_points, use_3d_slices));
25875daefc96SJeremy L Thompson 
25885daefc96SJeremy L Thompson   // -- Output basis and restriction
25895daefc96SJeremy L Thompson   code << "\n" << tab << "// -- Output field basis action and restrictions\n";
25905daefc96SJeremy L Thompson   CeedScalar offset = 0;
25915daefc96SJeremy L Thompson 
25925daefc96SJeremy L Thompson   for (CeedInt i = 0; i < num_output_fields; i++) {
25935daefc96SJeremy L Thompson     bool        is_active = false;
25945daefc96SJeremy L Thompson     const char *field_name;
25955daefc96SJeremy L Thompson 
25965daefc96SJeremy L Thompson     {
25975daefc96SJeremy L Thompson       CeedVector vec;
25985daefc96SJeremy L Thompson 
25995daefc96SJeremy L Thompson       CeedCallBackend(CeedOperatorFieldGetVector(op_output_fields[i], &vec));
26005daefc96SJeremy L Thompson       is_active = vec == CEED_VECTOR_ACTIVE;
26015daefc96SJeremy L Thompson       CeedCallBackend(CeedVectorDestroy(&vec));
26025daefc96SJeremy L Thompson     }
26035daefc96SJeremy L Thompson     if (!is_active) continue;
26045daefc96SJeremy L Thompson 
26055daefc96SJeremy L Thompson     CeedCallBackend(CeedOperatorFieldGetName(op_output_fields[i], &field_name));
26065daefc96SJeremy L Thompson     code << tab << "// ---- Output field " << i << ": " << field_name << "\n";
26075daefc96SJeremy L Thompson 
26085daefc96SJeremy L Thompson     // ---- Restriction
26095daefc96SJeremy L Thompson     CeedInt field_size;
26105daefc96SJeremy L Thompson 
26115daefc96SJeremy L Thompson     code << tab << "WriteLVecStandard" << (is_all_tensor ? max_dim : 1) << "d_QFAssembly<total_size_out, field_size_out_" << i << ", "
26125daefc96SJeremy L Thompson          << (is_all_tensor ? "Q_1d" : "Q") << ">(data, num_elem, elem, input_offset + s, " << offset << ", r_q_out_" << i << ", values_array);\n";
26135daefc96SJeremy L Thompson     CeedCallBackend(CeedQFunctionFieldGetSize(qf_output_fields[i], &field_size));
26145daefc96SJeremy L Thompson     offset += field_size;
26155daefc96SJeremy L Thompson   }
26165daefc96SJeremy L Thompson 
26175daefc96SJeremy L Thompson   // -- Reset current active node and component
26185daefc96SJeremy L Thompson   code << "\n" << tab << "// Reset current active node and component to 0.0\n";
26195daefc96SJeremy L Thompson   if (is_all_tensor && (max_dim >= 3)) {
26205daefc96SJeremy L Thompson     code << tab << "for (CeedInt i = 0; i < Q_1d; i++) r_q_in[a][i + s * Q_1d] = 0.0;\n";
26215daefc96SJeremy L Thompson   } else {
26225daefc96SJeremy L Thompson     code << tab << "r_q_in[a][s] = 0.0;\n";
26235daefc96SJeremy L Thompson   }
26245daefc96SJeremy L Thompson 
26255daefc96SJeremy L Thompson   // -- End of loop over size of active field
26265daefc96SJeremy L Thompson   tab.pop();
26275daefc96SJeremy L Thompson   code << tab << "}\n";
26285daefc96SJeremy L Thompson   code << tab << "input_offset += field_size_in;\n";
26295daefc96SJeremy L Thompson 
26305daefc96SJeremy L Thompson   // -- End of loop over active field
26315daefc96SJeremy L Thompson   tab.pop();
26325daefc96SJeremy L Thompson   code << tab << "}\n";
26335daefc96SJeremy L Thompson 
26345daefc96SJeremy L Thompson   // Close loop and function
26355daefc96SJeremy L Thompson   tab.pop();
26365daefc96SJeremy L Thompson   code << tab << "}\n";
26375daefc96SJeremy L Thompson   tab.pop();
26385daefc96SJeremy L Thompson   code << tab << "}\n";
26395daefc96SJeremy L Thompson   code << tab << "// -----------------------------------------------------------------------------\n\n";
26405daefc96SJeremy L Thompson 
26415daefc96SJeremy L Thompson   CeedInt block_sizes[3] = {0, 0, 0};
26425daefc96SJeremy L Thompson   CeedInt num_elem;
26435daefc96SJeremy L Thompson 
26445daefc96SJeremy L Thompson   // Compile
26455daefc96SJeremy L Thompson   CeedCallBackend(CeedOperatorGetNumElements(op, &num_elem));
26465daefc96SJeremy L Thompson   CeedCallBackend(BlockGridCalculate_Hip_gen(max_dim, num_elem, data->max_P_1d, Q_1d, block_sizes));
26475daefc96SJeremy L Thompson   {
26485daefc96SJeremy L Thompson     bool is_compile_good = false;
26495daefc96SJeremy L Thompson 
26505daefc96SJeremy L Thompson     data->thread_1d = block_sizes[0];
26515daefc96SJeremy L Thompson     CeedCallBackend(CeedTryCompile_Hip(ceed, code.str().c_str(), &is_compile_good, &data->module_assemble_qfunction, 2, "OP_T_1D", block_sizes[0],
26525daefc96SJeremy L Thompson                                        "BLOCK_SIZE", block_sizes[0] * block_sizes[1] * block_sizes[2]));
26535daefc96SJeremy L Thompson     if (is_compile_good) {
26545daefc96SJeremy L Thompson       *is_good_build = true;
26555daefc96SJeremy L Thompson       CeedCallBackend(CeedGetKernel_Hip(ceed, data->module_assemble_qfunction, operator_name.c_str(), &data->assemble_qfunction));
26565daefc96SJeremy L Thompson     } else {
26575daefc96SJeremy L Thompson       *is_good_build              = false;
26585daefc96SJeremy L Thompson       data->use_assembly_fallback = true;
26595daefc96SJeremy L Thompson     }
26605daefc96SJeremy L Thompson   }
26615daefc96SJeremy L Thompson   CeedCallBackend(CeedDestroy(&ceed));
26625daefc96SJeremy L Thompson   CeedCallBackend(CeedQFunctionDestroy(&qf));
26635daefc96SJeremy L Thompson   return CEED_ERROR_SUCCESS;
26645daefc96SJeremy L Thompson }
2665692716b7SZach Atkins 
26660183ed61SJeremy L Thompson //------------------------------------------------------------------------------
2667