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> 129e201c85SYohann #include <ceed/jit-tools.h> 132b730f8bSJeremy L Thompson 147d8d0e25Snbeams #include <iostream> 157d8d0e25Snbeams #include <sstream> 162b730f8bSJeremy L Thompson #include <string> 172b730f8bSJeremy L Thompson 180d0321e0SJeremy L Thompson #include "../hip-ref/ceed-hip-ref.h" 197d8d0e25Snbeams #include "../hip-shared/ceed-hip-shared.h" 20b2165e7aSSebastian Grimberg #include "../hip/ceed-hip-common.h" 217d8d0e25Snbeams #include "../hip/ceed-hip-compile.h" 222b730f8bSJeremy L Thompson #include "ceed-hip-gen.h" 23b3e1519bSnbeams 2445a787f7SJeremy L Thompson struct FieldReuse_Hip { 2545a787f7SJeremy L Thompson CeedInt index; 2645a787f7SJeremy L Thompson bool is_input; 2745a787f7SJeremy L Thompson CeedEvalMode eval_mode; 2845a787f7SJeremy L Thompson }; 2945a787f7SJeremy L Thompson 30b3e1519bSnbeams //------------------------------------------------------------------------------ 31b3e1519bSnbeams // Calculate the block size used for launching the operator kernel 32b3e1519bSnbeams //------------------------------------------------------------------------------ 332b730f8bSJeremy L Thompson extern "C" int BlockGridCalculate_Hip_gen(const CeedInt dim, const CeedInt num_elem, const CeedInt P_1d, const CeedInt Q_1d, CeedInt *block_sizes) { 343a2968d6SJeremy L Thompson const CeedInt thread_1d = CeedIntMax(Q_1d, P_1d); 35b3e1519bSnbeams if (dim == 1) { 363a2968d6SJeremy L Thompson CeedInt elems_per_block = 64 * thread_1d > 256 ? 256 / thread_1d : 64; 37b7453713SJeremy L Thompson 389e201c85SYohann elems_per_block = elems_per_block > 0 ? elems_per_block : 1; 393a2968d6SJeremy L Thompson block_sizes[0] = thread_1d; 40b3e1519bSnbeams block_sizes[1] = 1; 419e201c85SYohann block_sizes[2] = elems_per_block; 42b3e1519bSnbeams } else if (dim == 2) { 433a2968d6SJeremy L Thompson const CeedInt elems_per_block = thread_1d < 4 ? 16 : 2; 44b7453713SJeremy L Thompson 453a2968d6SJeremy L Thompson block_sizes[0] = thread_1d; 463a2968d6SJeremy L Thompson block_sizes[1] = thread_1d; 479e201c85SYohann block_sizes[2] = elems_per_block; 48b3e1519bSnbeams } else if (dim == 3) { 493a2968d6SJeremy L Thompson const CeedInt elems_per_block = thread_1d < 6 ? 4 : (thread_1d < 8 ? 2 : 1); 50b7453713SJeremy L Thompson 513a2968d6SJeremy L Thompson block_sizes[0] = thread_1d; 523a2968d6SJeremy L Thompson block_sizes[1] = thread_1d; 539e201c85SYohann block_sizes[2] = elems_per_block; 54b3e1519bSnbeams } 55b3e1519bSnbeams return CEED_ERROR_SUCCESS; 56b3e1519bSnbeams } 57b3e1519bSnbeams 587d8d0e25Snbeams //------------------------------------------------------------------------------ 594b3e95d5SJeremy L Thompson // Determine type of operator 604b3e95d5SJeremy L Thompson //------------------------------------------------------------------------------ 614b3e95d5SJeremy L Thompson static int CeedOperatorBuildKernelData_Hip_gen(Ceed ceed, CeedInt num_input_fields, CeedOperatorField *op_input_fields, 624b3e95d5SJeremy L Thompson CeedQFunctionField *qf_input_fields, CeedInt num_output_fields, CeedOperatorField *op_output_fields, 6374398b5aSJeremy L Thompson CeedQFunctionField *qf_output_fields, CeedInt *max_P, CeedInt *max_P_1d, CeedInt *Q, CeedInt *Q_1d, 6474398b5aSJeremy L Thompson CeedInt *max_dim, bool *is_all_tensor, bool *use_3d_slices) { 6574398b5aSJeremy L Thompson // Check if all are tensor 6674398b5aSJeremy L Thompson *is_all_tensor = true; 674b3e95d5SJeremy L Thompson for (CeedInt i = 0; i < num_input_fields; i++) { 684b3e95d5SJeremy L Thompson CeedBasis basis; 694b3e95d5SJeremy L Thompson 704b3e95d5SJeremy L Thompson CeedCallBackend(CeedOperatorFieldGetBasis(op_input_fields[i], &basis)); 714b3e95d5SJeremy L Thompson if (basis != CEED_BASIS_NONE) { 724b3e95d5SJeremy L Thompson bool is_field_tensor; 734b3e95d5SJeremy L Thompson 744b3e95d5SJeremy L Thompson CeedCallBackend(CeedBasisIsTensor(basis, &is_field_tensor)); 7574398b5aSJeremy L Thompson *is_all_tensor = *is_all_tensor && is_field_tensor; 764b3e95d5SJeremy L Thompson } 773a2968d6SJeremy L Thompson CeedCallBackend(CeedBasisDestroy(&basis)); 784b3e95d5SJeremy L Thompson } 794b3e95d5SJeremy L Thompson for (CeedInt i = 0; i < num_output_fields; i++) { 804b3e95d5SJeremy L Thompson CeedBasis basis; 814b3e95d5SJeremy L Thompson 824b3e95d5SJeremy L Thompson CeedCallBackend(CeedOperatorFieldGetBasis(op_output_fields[i], &basis)); 834b3e95d5SJeremy L Thompson if (basis != CEED_BASIS_NONE) { 844b3e95d5SJeremy L Thompson bool is_field_tensor; 854b3e95d5SJeremy L Thompson 864b3e95d5SJeremy L Thompson CeedCallBackend(CeedBasisIsTensor(basis, &is_field_tensor)); 8774398b5aSJeremy L Thompson *is_all_tensor = *is_all_tensor && is_field_tensor; 8874398b5aSJeremy L Thompson } 8974398b5aSJeremy L Thompson CeedCallBackend(CeedBasisDestroy(&basis)); 9074398b5aSJeremy L Thompson } 9174398b5aSJeremy L Thompson 9274398b5aSJeremy L Thompson // Find max_P, max_P_1d, Q, and Q_1d 9374398b5aSJeremy L Thompson bool is_all_3d = true; 9474398b5aSJeremy L Thompson 9574398b5aSJeremy L Thompson *max_P = 0; 9674398b5aSJeremy L Thompson *max_P_1d = 0; 9774398b5aSJeremy L Thompson *Q = 0; 9874398b5aSJeremy L Thompson *Q_1d = 0; 9974398b5aSJeremy L Thompson for (CeedInt i = 0; i < num_input_fields; i++) { 10074398b5aSJeremy L Thompson CeedBasis basis; 10174398b5aSJeremy L Thompson 10274398b5aSJeremy L Thompson CeedCallBackend(CeedOperatorFieldGetBasis(op_input_fields[i], &basis)); 10374398b5aSJeremy L Thompson if (basis != CEED_BASIS_NONE) { 10474398b5aSJeremy L Thompson bool is_field_tensor; 10574398b5aSJeremy L Thompson CeedInt field_dim = 0, field_P = 0, field_P_1d = 0, field_Q = 0, field_Q_1d = 0; 10674398b5aSJeremy L Thompson 10774398b5aSJeremy L Thompson // Check if 3D 1084b3e95d5SJeremy L Thompson CeedCallBackend(CeedBasisGetDimension(basis, &field_dim)); 10974398b5aSJeremy L Thompson is_all_3d = is_all_3d && (field_dim == 3); 11074398b5aSJeremy L Thompson *max_dim = CeedIntMax(*max_dim, field_dim); 11174398b5aSJeremy L Thompson 11274398b5aSJeremy L Thompson // Collect P, P_1d, Q, and Q_1d 11374398b5aSJeremy L Thompson CeedCallBackend(CeedBasisGetNumNodes(basis, &field_P)); 11474398b5aSJeremy L Thompson *max_P = CeedIntMax(*max_P, field_P); 11574398b5aSJeremy L Thompson CeedCallBackend(CeedBasisIsTensor(basis, &is_field_tensor)); 11674398b5aSJeremy L Thompson if (is_field_tensor) { 11774398b5aSJeremy L Thompson CeedCallBackend(CeedBasisGetNumNodes1D(basis, &field_P_1d)); 11874398b5aSJeremy L Thompson *max_P_1d = CeedIntMax(*max_P_1d, field_P_1d); 11974398b5aSJeremy L Thompson } 12074398b5aSJeremy L Thompson CeedCallBackend(CeedBasisGetNumQuadraturePoints(basis, &field_Q)); 12174398b5aSJeremy L Thompson CeedCheck(*Q == 0 || field_Q == *Q, ceed, CEED_ERROR_BACKEND, "Quadrature spaces must be compatible"); 12274398b5aSJeremy L Thompson *Q = field_Q; 12374398b5aSJeremy L Thompson if (is_field_tensor) { 12474398b5aSJeremy L Thompson CeedCallBackend(CeedBasisGetNumQuadraturePoints1D(basis, &field_Q_1d)); 1254b3e95d5SJeremy L Thompson CeedCheck(*Q_1d == 0 || field_Q_1d == *Q_1d, ceed, CEED_ERROR_BACKEND, "Quadrature spaces must be compatible"); 1264b3e95d5SJeremy L Thompson *Q_1d = field_Q_1d; 1274b3e95d5SJeremy L Thompson } 12874398b5aSJeremy L Thompson } 12974398b5aSJeremy L Thompson CeedCallBackend(CeedBasisDestroy(&basis)); 13074398b5aSJeremy L Thompson } 13174398b5aSJeremy L Thompson for (CeedInt i = 0; i < num_output_fields; i++) { 13274398b5aSJeremy L Thompson CeedBasis basis; 13374398b5aSJeremy L Thompson 13474398b5aSJeremy L Thompson CeedCallBackend(CeedOperatorFieldGetBasis(op_output_fields[i], &basis)); 13574398b5aSJeremy L Thompson if (basis != CEED_BASIS_NONE) { 13674398b5aSJeremy L Thompson bool is_field_tensor; 13774398b5aSJeremy L Thompson CeedInt field_dim = 0, field_P = 0, field_P_1d = 0, field_Q = 0, field_Q_1d = 0; 13874398b5aSJeremy L Thompson 13974398b5aSJeremy L Thompson // Check if 3D 14074398b5aSJeremy L Thompson CeedCallBackend(CeedBasisGetDimension(basis, &field_dim)); 14174398b5aSJeremy L Thompson is_all_3d = is_all_3d && (field_dim == 3); 14274398b5aSJeremy L Thompson *max_dim = CeedIntMax(*max_dim, field_dim); 14374398b5aSJeremy L Thompson 14474398b5aSJeremy L Thompson // Collect P, P_1d, Q, and Q_1d 14574398b5aSJeremy L Thompson CeedCallBackend(CeedBasisGetNumNodes(basis, &field_P)); 14674398b5aSJeremy L Thompson *max_P = CeedIntMax(*max_P, field_P); 14774398b5aSJeremy L Thompson CeedCallBackend(CeedBasisIsTensor(basis, &is_field_tensor)); 14874398b5aSJeremy L Thompson if (is_field_tensor) { 14974398b5aSJeremy L Thompson CeedCallBackend(CeedBasisGetNumNodes1D(basis, &field_P_1d)); 15074398b5aSJeremy L Thompson *max_P_1d = CeedIntMax(*max_P_1d, field_P_1d); 15174398b5aSJeremy L Thompson } 15274398b5aSJeremy L Thompson CeedCallBackend(CeedBasisGetNumQuadraturePoints(basis, &field_Q)); 15374398b5aSJeremy L Thompson CeedCheck(*Q == 0 || field_Q == *Q, ceed, CEED_ERROR_BACKEND, "Quadrature spaces must be compatible"); 15474398b5aSJeremy L Thompson *Q = field_Q; 15574398b5aSJeremy L Thompson if (is_field_tensor) { 15674398b5aSJeremy L Thompson CeedCallBackend(CeedBasisGetNumQuadraturePoints1D(basis, &field_Q_1d)); 15774398b5aSJeremy L Thompson CeedCheck(*Q_1d == 0 || field_Q_1d == *Q_1d, ceed, CEED_ERROR_BACKEND, "Quadrature spaces must be compatible"); 15874398b5aSJeremy L Thompson *Q_1d = field_Q_1d; 15974398b5aSJeremy L Thompson } 16074398b5aSJeremy L Thompson } 1613a2968d6SJeremy L Thompson CeedCallBackend(CeedBasisDestroy(&basis)); 1624b3e95d5SJeremy L Thompson } 1634b3e95d5SJeremy L Thompson 1644b3e95d5SJeremy L Thompson // Only use 3D collocated gradient parallelization strategy when gradient is computed 1654b3e95d5SJeremy L Thompson *use_3d_slices = false; 16674398b5aSJeremy L Thompson if (is_all_3d && *is_all_tensor) { 1674b3e95d5SJeremy L Thompson bool was_grad_found = false; 1684b3e95d5SJeremy L Thompson 1694b3e95d5SJeremy L Thompson for (CeedInt i = 0; i < num_input_fields; i++) { 1704b3e95d5SJeremy L Thompson CeedEvalMode eval_mode; 1714b3e95d5SJeremy L Thompson 1724b3e95d5SJeremy L Thompson CeedCallBackend(CeedQFunctionFieldGetEvalMode(qf_input_fields[i], &eval_mode)); 1734b3e95d5SJeremy L Thompson if (eval_mode == CEED_EVAL_GRAD) { 1744b3e95d5SJeremy L Thompson CeedBasis_Hip_shared *basis_data; 1754b3e95d5SJeremy L Thompson CeedBasis basis; 1764b3e95d5SJeremy L Thompson 1774b3e95d5SJeremy L Thompson CeedCallBackend(CeedOperatorFieldGetBasis(op_input_fields[i], &basis)); 1784b3e95d5SJeremy L Thompson CeedCallBackend(CeedBasisGetData(basis, &basis_data)); 1794b3e95d5SJeremy L Thompson *use_3d_slices = basis_data->d_collo_grad_1d && (was_grad_found ? *use_3d_slices : true); 1804b3e95d5SJeremy L Thompson was_grad_found = true; 1813a2968d6SJeremy L Thompson CeedCallBackend(CeedBasisDestroy(&basis)); 1824b3e95d5SJeremy L Thompson } 1834b3e95d5SJeremy L Thompson } 1844b3e95d5SJeremy L Thompson for (CeedInt i = 0; i < num_output_fields; i++) { 1854b3e95d5SJeremy L Thompson CeedEvalMode eval_mode; 1864b3e95d5SJeremy L Thompson 1874b3e95d5SJeremy L Thompson CeedCallBackend(CeedQFunctionFieldGetEvalMode(qf_output_fields[i], &eval_mode)); 1884b3e95d5SJeremy L Thompson if (eval_mode == CEED_EVAL_GRAD) { 1894b3e95d5SJeremy L Thompson CeedBasis_Hip_shared *basis_data; 1904b3e95d5SJeremy L Thompson CeedBasis basis; 1914b3e95d5SJeremy L Thompson 1924b3e95d5SJeremy L Thompson CeedCallBackend(CeedOperatorFieldGetBasis(op_output_fields[i], &basis)); 1934b3e95d5SJeremy L Thompson CeedCallBackend(CeedBasisGetData(basis, &basis_data)); 1944b3e95d5SJeremy L Thompson *use_3d_slices = basis_data->d_collo_grad_1d && (was_grad_found ? *use_3d_slices : true); 1954b3e95d5SJeremy L Thompson was_grad_found = true; 1963a2968d6SJeremy L Thompson CeedCallBackend(CeedBasisDestroy(&basis)); 1974b3e95d5SJeremy L Thompson } 1984b3e95d5SJeremy L Thompson } 1994b3e95d5SJeremy L Thompson } 2004b3e95d5SJeremy L Thompson return CEED_ERROR_SUCCESS; 2014b3e95d5SJeremy L Thompson } 2024b3e95d5SJeremy L Thompson 2034b3e95d5SJeremy L Thompson //------------------------------------------------------------------------------ 2044b3e95d5SJeremy L Thompson // Setup fields 2054b3e95d5SJeremy L Thompson //------------------------------------------------------------------------------ 2064b3e95d5SJeremy L Thompson static int CeedOperatorBuildKernelFieldData_Hip_gen(std::ostringstream &code, CeedOperator_Hip_gen *data, CeedInt i, CeedOperatorField op_field, 20774398b5aSJeremy L Thompson CeedQFunctionField qf_field, FieldReuse_Hip field_reuse, CeedInt max_dim, CeedInt Q, CeedInt Q_1d, 20874398b5aSJeremy L Thompson bool is_input, bool is_all_tensor, bool is_at_points, bool use_3d_slices) { 20974398b5aSJeremy L Thompson bool is_tensor = true; 21074398b5aSJeremy L Thompson CeedBasis basis; 21174398b5aSJeremy L Thompson CeedCallBackend(CeedOperatorFieldGetBasis(op_field, &basis)); 21274398b5aSJeremy L Thompson if (basis != CEED_BASIS_NONE) CeedCallBackend(CeedBasisIsTensor(basis, &is_tensor)); 21374398b5aSJeremy L Thompson 21459fa3f92SJeremy L Thompson const char *field_name; 2154b3e95d5SJeremy L Thompson std::string var_suffix = (is_input ? "_in_" : "_out_") + std::to_string(i); 2169123fb08SJeremy L Thompson std::string P_name = (is_tensor ? "P_1d" : "P") + var_suffix, Q_name = is_tensor ? "Q_1d" : "Q"; 2174b3e95d5SJeremy L Thompson std::string option_name = (is_input ? "inputs" : "outputs"); 2184b3e95d5SJeremy L Thompson CeedEvalMode eval_mode = CEED_EVAL_NONE; 21974398b5aSJeremy L Thompson CeedInt elem_size = 0, num_comp = 0, dim = max_dim, P_1d = 0; 2204b3e95d5SJeremy L Thompson CeedElemRestriction elem_rstr; 2214b3e95d5SJeremy L Thompson CeedBasis_Hip_shared *basis_data; 2224b3e95d5SJeremy L Thompson 2239ee499e5SJeremy L Thompson // Field reuse info 22445a787f7SJeremy L Thompson bool use_previous_field = field_reuse.index != -1; 2259ee499e5SJeremy L Thompson 22659fa3f92SJeremy L Thompson CeedCallBackend(CeedOperatorFieldGetName(op_field, &field_name)); 22759fa3f92SJeremy L Thompson code << " // -- " << (is_input ? "Input" : "Output") << " field " << i << ": " << field_name << "\n"; 2284b3e95d5SJeremy L Thompson 2294b3e95d5SJeremy L Thompson // Get field data 2304b3e95d5SJeremy L Thompson CeedCallBackend(CeedOperatorFieldGetElemRestriction(op_field, &elem_rstr)); 2314b3e95d5SJeremy L Thompson if (elem_rstr != CEED_ELEMRESTRICTION_NONE) { 2324b3e95d5SJeremy L Thompson CeedCallBackend(CeedElemRestrictionGetElementSize(elem_rstr, &elem_size)); 2334b3e95d5SJeremy L Thompson CeedCallBackend(CeedElemRestrictionGetNumComponents(elem_rstr, &num_comp)); 2344b3e95d5SJeremy L Thompson } 2353a2968d6SJeremy L Thompson CeedCallBackend(CeedElemRestrictionDestroy(&elem_rstr)); 2364b3e95d5SJeremy L Thompson if (basis != CEED_BASIS_NONE) { 2374b3e95d5SJeremy L Thompson CeedCallBackend(CeedBasisGetData(basis, &basis_data)); 23874398b5aSJeremy L Thompson CeedCallBackend(CeedBasisGetDimension(basis, &dim)); 2399123fb08SJeremy L Thompson if (is_tensor) CeedCallBackend(CeedBasisGetNumNodes1D(basis, &P_1d)); 2409123fb08SJeremy L Thompson else CeedCallBackend(CeedBasisGetNumNodes(basis, &P_1d)); 2414b3e95d5SJeremy L Thompson } 2424b3e95d5SJeremy L Thompson CeedCallBackend(CeedQFunctionFieldGetEvalMode(qf_field, &eval_mode)); 2434b3e95d5SJeremy L Thompson 2444b3e95d5SJeremy L Thompson // Set field constants 24574398b5aSJeremy L Thompson code << " const CeedInt dim" << var_suffix << " = " << dim << ";\n"; 24674398b5aSJeremy L Thompson if (is_tensor && !is_all_tensor) { 24774398b5aSJeremy L Thompson CeedInt P = 0; 24874398b5aSJeremy L Thompson 24974398b5aSJeremy L Thompson CeedCallBackend(CeedBasisGetNumNodes(basis, &P)); 25074398b5aSJeremy L Thompson code << " const CeedInt P" << var_suffix << " = " << (basis == CEED_BASIS_NONE ? Q : P) << ";\n"; 25174398b5aSJeremy L Thompson } 2524b3e95d5SJeremy L Thompson code << " const CeedInt " << P_name << " = " << (basis == CEED_BASIS_NONE ? Q_1d : P_1d) << ";\n"; 253343e3094SJeremy L Thompson if (eval_mode != CEED_EVAL_WEIGHT) { 2544b3e95d5SJeremy L Thompson code << " const CeedInt num_comp" << var_suffix << " = " << num_comp << ";\n"; 2554b3e95d5SJeremy L Thompson } 2564b3e95d5SJeremy L Thompson 2574b3e95d5SJeremy L Thompson // Load basis data 2584b3e95d5SJeremy L Thompson code << " // EvalMode: " << CeedEvalModes[eval_mode] << "\n"; 2594b3e95d5SJeremy L Thompson switch (eval_mode) { 2604b3e95d5SJeremy L Thompson case CEED_EVAL_NONE: 2614b3e95d5SJeremy L Thompson break; 2624b3e95d5SJeremy L Thompson case CEED_EVAL_INTERP: 2633a2968d6SJeremy L Thompson if (is_at_points) { 2643a2968d6SJeremy L Thompson // AtPoints 2653a2968d6SJeremy L Thompson if (!basis_data->d_chebyshev_interp_1d) { 2663a2968d6SJeremy L Thompson CeedSize interp_bytes; 2673a2968d6SJeremy L Thompson CeedScalar *chebyshev_interp_1d; 2683a2968d6SJeremy L Thompson 2693a2968d6SJeremy L Thompson interp_bytes = P_1d * Q_1d * sizeof(CeedScalar); 2703a2968d6SJeremy L Thompson CeedCallBackend(CeedCalloc(P_1d * Q_1d, &chebyshev_interp_1d)); 2713a2968d6SJeremy L Thompson CeedCallBackend(CeedBasisGetChebyshevInterp1D(basis, chebyshev_interp_1d)); 2723a2968d6SJeremy L Thompson CeedCallHip(CeedBasisReturnCeed(basis), hipMalloc((void **)&basis_data->d_chebyshev_interp_1d, interp_bytes)); 2733a2968d6SJeremy L Thompson CeedCallHip(CeedBasisReturnCeed(basis), 2743a2968d6SJeremy L Thompson hipMemcpy(basis_data->d_chebyshev_interp_1d, chebyshev_interp_1d, interp_bytes, hipMemcpyHostToDevice)); 2753a2968d6SJeremy L Thompson CeedCallBackend(CeedFree(&chebyshev_interp_1d)); 2763a2968d6SJeremy L Thompson } 2773a2968d6SJeremy L Thompson if (is_input) data->B.inputs[i] = basis_data->d_chebyshev_interp_1d; 2783a2968d6SJeremy L Thompson else data->B.outputs[i] = basis_data->d_chebyshev_interp_1d; 2793a2968d6SJeremy L Thompson } else { 2803a2968d6SJeremy L Thompson // Standard quadrature 2814b3e95d5SJeremy L Thompson if (is_input) data->B.inputs[i] = basis_data->d_interp_1d; 2824b3e95d5SJeremy L Thompson else data->B.outputs[i] = basis_data->d_interp_1d; 2833a2968d6SJeremy L Thompson } 2849ee499e5SJeremy L Thompson if (use_previous_field) { 28545a787f7SJeremy L Thompson std::string reuse_var = "s_B" + ((field_reuse.is_input ? "_in_" : "_out_") + std::to_string(field_reuse.index)); 2869ee499e5SJeremy L Thompson 2879ee499e5SJeremy L Thompson code << " CeedScalar *s_B" << var_suffix << " = " << reuse_var << ";\n"; 2889ee499e5SJeremy L Thompson } else { 2899123fb08SJeremy L Thompson code << " __shared__ CeedScalar s_B" << var_suffix << "[" << P_name << "*" << Q_name << "];\n"; 290f815fac9SJeremy L Thompson code << " LoadMatrix<" << P_name << ", " << Q_name << ">(data, B." << option_name << "[" << i << "], s_B" << var_suffix << ");\n"; 2919ee499e5SJeremy L Thompson } 2924b3e95d5SJeremy L Thompson break; 2934b3e95d5SJeremy L Thompson case CEED_EVAL_GRAD: 2943a2968d6SJeremy L Thompson if (is_at_points) { 2953a2968d6SJeremy L Thompson // AtPoints 2963a2968d6SJeremy L Thompson if (!basis_data->d_chebyshev_interp_1d) { 2973a2968d6SJeremy L Thompson CeedSize interp_bytes; 2983a2968d6SJeremy L Thompson CeedScalar *chebyshev_interp_1d; 2993a2968d6SJeremy L Thompson 3003a2968d6SJeremy L Thompson interp_bytes = P_1d * Q_1d * sizeof(CeedScalar); 3013a2968d6SJeremy L Thompson CeedCallBackend(CeedCalloc(P_1d * Q_1d, &chebyshev_interp_1d)); 3023a2968d6SJeremy L Thompson CeedCallBackend(CeedBasisGetChebyshevInterp1D(basis, chebyshev_interp_1d)); 3033a2968d6SJeremy L Thompson CeedCallHip(CeedBasisReturnCeed(basis), hipMalloc((void **)&basis_data->d_chebyshev_interp_1d, interp_bytes)); 3043a2968d6SJeremy L Thompson CeedCallHip(CeedBasisReturnCeed(basis), 3053a2968d6SJeremy L Thompson hipMemcpy(basis_data->d_chebyshev_interp_1d, chebyshev_interp_1d, interp_bytes, hipMemcpyHostToDevice)); 3063a2968d6SJeremy L Thompson CeedCallBackend(CeedFree(&chebyshev_interp_1d)); 3073a2968d6SJeremy L Thompson } 3083a2968d6SJeremy L Thompson if (is_input) data->B.inputs[i] = basis_data->d_chebyshev_interp_1d; 3093a2968d6SJeremy L Thompson else data->B.outputs[i] = basis_data->d_chebyshev_interp_1d; 3103a2968d6SJeremy L Thompson } else { 3113a2968d6SJeremy L Thompson // Standard quadrature 3124b3e95d5SJeremy L Thompson if (is_input) data->B.inputs[i] = basis_data->d_interp_1d; 3134b3e95d5SJeremy L Thompson else data->B.outputs[i] = basis_data->d_interp_1d; 3143a2968d6SJeremy L Thompson } 3159123fb08SJeremy L Thompson if (is_tensor) { 3169ee499e5SJeremy L Thompson if (use_previous_field) { 31745a787f7SJeremy L Thompson std::string reuse_var = "s_B" + ((field_reuse.is_input ? "_in_" : "_out_") + std::to_string(field_reuse.index)); 3189ee499e5SJeremy L Thompson 3199ee499e5SJeremy L Thompson code << " CeedScalar *s_B" << var_suffix << " = " << reuse_var << ";\n"; 3209ee499e5SJeremy L Thompson } else { 3219123fb08SJeremy L Thompson code << " __shared__ CeedScalar s_B" << var_suffix << "[" << P_name << "*" << Q_name << "];\n"; 322f815fac9SJeremy L Thompson code << " LoadMatrix<" << P_name << ", " << Q_name << ">(data, B." << option_name << "[" << i << "], s_B" << var_suffix << ");\n"; 3239123fb08SJeremy L Thompson } 3249ee499e5SJeremy L Thompson } 3253a2968d6SJeremy L Thompson if (is_at_points) break; // No G mat for AtPoints 3264b3e95d5SJeremy L Thompson if (use_3d_slices) { 3274b3e95d5SJeremy L Thompson if (is_input) data->G.inputs[i] = basis_data->d_collo_grad_1d; 3284b3e95d5SJeremy L Thompson else data->G.outputs[i] = basis_data->d_collo_grad_1d; 32945a787f7SJeremy L Thompson if (use_previous_field && field_reuse.eval_mode == CEED_EVAL_GRAD) { 33045a787f7SJeremy L Thompson std::string reuse_var = "s_G" + ((field_reuse.is_input ? "_in_" : "_out_") + std::to_string(field_reuse.index)); 3319ee499e5SJeremy L Thompson 3329ee499e5SJeremy L Thompson code << " CeedScalar *s_G" << var_suffix << " = " << reuse_var << ";\n"; 3339ee499e5SJeremy L Thompson } else { 3349123fb08SJeremy L Thompson code << " __shared__ CeedScalar s_G" << var_suffix << "[" << Q_name << "*" << Q_name << "];\n"; 335f815fac9SJeremy L Thompson code << " LoadMatrix<" << Q_name << ", " << Q_name << ">(data, G." << option_name << "[" << i << "], s_G" << var_suffix << ");\n"; 3369ee499e5SJeremy L Thompson } 3374b3e95d5SJeremy L Thompson } else { 3384b3e95d5SJeremy L Thompson bool has_collo_grad = basis_data->d_collo_grad_1d; 3394b3e95d5SJeremy L Thompson 3404b3e95d5SJeremy L Thompson if (is_input) data->G.inputs[i] = has_collo_grad ? basis_data->d_collo_grad_1d : basis_data->d_grad_1d; 3414b3e95d5SJeremy L Thompson else data->G.outputs[i] = has_collo_grad ? basis_data->d_collo_grad_1d : basis_data->d_grad_1d; 3424b3e95d5SJeremy L Thompson if (has_collo_grad) { 34345a787f7SJeremy L Thompson if (use_previous_field && field_reuse.eval_mode == CEED_EVAL_GRAD) { 34445a787f7SJeremy L Thompson std::string reuse_var = "s_G" + ((field_reuse.is_input ? "_in_" : "_out_") + std::to_string(field_reuse.index)); 3459ee499e5SJeremy L Thompson 3469ee499e5SJeremy L Thompson code << " CeedScalar *s_G" << var_suffix << " = " << reuse_var << ";\n"; 3479ee499e5SJeremy L Thompson } else { 3489123fb08SJeremy L Thompson code << " __shared__ CeedScalar s_G" << var_suffix << "[" << Q_name << "*" << Q_name << "];\n"; 349f815fac9SJeremy L Thompson code << " LoadMatrix<" << Q_name << ", " << Q_name << ">(data, G." << option_name << "[" << i << "], s_G" << var_suffix << ");\n"; 3509ee499e5SJeremy L Thompson } 3519ee499e5SJeremy L Thompson } else { 35245a787f7SJeremy L Thompson if (use_previous_field && field_reuse.eval_mode == CEED_EVAL_GRAD) { 35345a787f7SJeremy L Thompson std::string reuse_var = "s_G" + ((field_reuse.is_input ? "_in_" : "_out_") + std::to_string(field_reuse.index)); 3549ee499e5SJeremy L Thompson 3559ee499e5SJeremy L Thompson code << " CeedScalar *s_G" << var_suffix << " = " << reuse_var << ";\n"; 3564b3e95d5SJeremy L Thompson } else { 35774398b5aSJeremy L Thompson code << " __shared__ CeedScalar s_G" << var_suffix << "[" << P_name << "*" << Q_name << (is_tensor ? "" : "*dim") 35874398b5aSJeremy L Thompson << (is_tensor ? "" : var_suffix) << "];\n"; 35974398b5aSJeremy L Thompson code << " LoadMatrix<" << P_name << ", " << Q_name << (is_tensor ? "" : "*dim") << (is_tensor ? "" : var_suffix) << ">(data, G." 36074398b5aSJeremy L Thompson << option_name << "[" << i << "], s_G" << var_suffix << ");\n"; 3614b3e95d5SJeremy L Thompson } 3624b3e95d5SJeremy L Thompson } 3639ee499e5SJeremy L Thompson } 3644b3e95d5SJeremy L Thompson break; 3654b3e95d5SJeremy L Thompson case CEED_EVAL_WEIGHT: 3664b3e95d5SJeremy L Thompson break; // No action 3674b3e95d5SJeremy L Thompson // LCOV_EXCL_START 3684b3e95d5SJeremy L Thompson case CEED_EVAL_DIV: 3694b3e95d5SJeremy L Thompson case CEED_EVAL_CURL: 3704b3e95d5SJeremy L Thompson break; // TODO: Not implemented 3714b3e95d5SJeremy L Thompson // LCOV_EXCL_STOP 3724b3e95d5SJeremy L Thompson } 3733a2968d6SJeremy L Thompson CeedCallBackend(CeedBasisDestroy(&basis)); 3744b3e95d5SJeremy L Thompson return CEED_ERROR_SUCCESS; 3754b3e95d5SJeremy L Thompson } 3764b3e95d5SJeremy L Thompson 3774b3e95d5SJeremy L Thompson //------------------------------------------------------------------------------ 3784b3e95d5SJeremy L Thompson // Restriction 3794b3e95d5SJeremy L Thompson //------------------------------------------------------------------------------ 38074398b5aSJeremy L Thompson static int CeedOperatorBuildKernelRestriction_Hip_gen(std::ostringstream &code, CeedOperator_Hip_gen *data, CeedInt i, CeedInt field_input_buffer[], 38174398b5aSJeremy L Thompson CeedOperatorField op_field, CeedQFunctionField qf_field, CeedInt max_dim, CeedInt Q_1d, 38274398b5aSJeremy L Thompson bool is_input, bool is_all_tensor, bool is_at_points, bool use_3d_slices) { 3834b3e95d5SJeremy L Thompson std::string var_suffix = (is_input ? "_in_" : "_out_") + std::to_string(i); 38474398b5aSJeremy L Thompson std::string P_name = (is_all_tensor ? "P_1d" : "P") + var_suffix; 3854b3e95d5SJeremy L Thompson CeedEvalMode eval_mode = CEED_EVAL_NONE; 38674398b5aSJeremy L Thompson CeedInt elem_size = 0, num_comp = 0; 3874b3e95d5SJeremy L Thompson CeedSize l_size; 388f815fac9SJeremy L Thompson CeedRestrictionType rstr_type = CEED_RESTRICTION_STANDARD; 3894b3e95d5SJeremy L Thompson CeedElemRestriction_Hip *rstr_data; 3904b3e95d5SJeremy L Thompson CeedElemRestriction elem_rstr; 3914b3e95d5SJeremy L Thompson 3924b3e95d5SJeremy L Thompson // Get field data 3934b3e95d5SJeremy L Thompson CeedCallBackend(CeedOperatorFieldGetElemRestriction(op_field, &elem_rstr)); 3944b3e95d5SJeremy L Thompson if (elem_rstr != CEED_ELEMRESTRICTION_NONE) { 395f815fac9SJeremy L Thompson CeedCallBackend(CeedElemRestrictionGetType(elem_rstr, &rstr_type)); 3964b3e95d5SJeremy L Thompson CeedCallBackend(CeedElemRestrictionGetElementSize(elem_rstr, &elem_size)); 3974b3e95d5SJeremy L Thompson CeedCallBackend(CeedElemRestrictionGetNumComponents(elem_rstr, &num_comp)); 3984b3e95d5SJeremy L Thompson CeedCallBackend(CeedElemRestrictionGetData(elem_rstr, &rstr_data)); 3994b3e95d5SJeremy L Thompson } 4004b3e95d5SJeremy L Thompson CeedCallBackend(CeedQFunctionFieldGetEvalMode(qf_field, &eval_mode)); 4014b3e95d5SJeremy L Thompson 4024b3e95d5SJeremy L Thompson // Restriction 4034b3e95d5SJeremy L Thompson if (is_input) { 4044b3e95d5SJeremy L Thompson // Input 405e93651e5SJeremy L Thompson if (field_input_buffer[i] != i) { 406e93651e5SJeremy L Thompson std::string buffer_name = "r_e_in_" + std::to_string(field_input_buffer[i]); 407e93651e5SJeremy L Thompson 408e93651e5SJeremy L Thompson // Restriction was already done for previous input 409e93651e5SJeremy L Thompson code << " CeedScalar *r_e" << var_suffix << " = " << buffer_name << ";\n"; 4103a2968d6SJeremy L Thompson } else if (eval_mode != CEED_EVAL_WEIGHT && !((eval_mode == CEED_EVAL_NONE) && use_3d_slices && is_at_points)) { 4113a2968d6SJeremy L Thompson if (eval_mode == CEED_EVAL_NONE && rstr_type != CEED_RESTRICTION_POINTS) { 412e93651e5SJeremy L Thompson // No basis action, so r_e_in_* in also r_q_in_* and needs to be allocated 4134b3e95d5SJeremy L Thompson code << " CeedScalar r_e" << var_suffix << "[num_comp" << var_suffix << "*" << P_name << "];\n"; 4143a2968d6SJeremy L Thompson } else if (rstr_type != CEED_RESTRICTION_POINTS) { 415e93651e5SJeremy L Thompson // Otherwise we're using the scratch space 416e93651e5SJeremy L Thompson code << " CeedScalar *r_e" << var_suffix << " = r_e_scratch;\n"; 417e93651e5SJeremy L Thompson } 418f815fac9SJeremy L Thompson switch (rstr_type) { 419f815fac9SJeremy L Thompson case CEED_RESTRICTION_STANDARD: { 4204b3e95d5SJeremy L Thompson CeedInt comp_stride; 4214b3e95d5SJeremy L Thompson 4224b3e95d5SJeremy L Thompson CeedCallBackend(CeedElemRestrictionGetLVectorSize(elem_rstr, &l_size)); 4234b3e95d5SJeremy L Thompson code << " const CeedInt l_size" << var_suffix << " = " << l_size << ";\n"; 4244b3e95d5SJeremy L Thompson CeedCallBackend(CeedElemRestrictionGetCompStride(elem_rstr, &comp_stride)); 4254b3e95d5SJeremy L Thompson code << " // CompStride: " << comp_stride << "\n"; 4264b3e95d5SJeremy L Thompson data->indices.inputs[i] = (CeedInt *)rstr_data->d_offsets; 42774398b5aSJeremy L Thompson code << " ReadLVecStandard" << (is_all_tensor ? max_dim : 1) << "d<num_comp" << var_suffix << ", " << comp_stride << ", " << P_name 4289123fb08SJeremy L Thompson << ">(data, l_size" << var_suffix << ", elem, indices.inputs[" << i << "], d" << var_suffix << ", r_e" << var_suffix << ");\n"; 429f815fac9SJeremy L Thompson break; 430f815fac9SJeremy L Thompson } 431f815fac9SJeremy L Thompson case CEED_RESTRICTION_STRIDED: { 4324b3e95d5SJeremy L Thompson bool has_backend_strides; 4334b3e95d5SJeremy L Thompson CeedInt num_elem; 4344b3e95d5SJeremy L Thompson 4354b3e95d5SJeremy L Thompson CeedCallBackend(CeedElemRestrictionHasBackendStrides(elem_rstr, &has_backend_strides)); 4364b3e95d5SJeremy L Thompson CeedCallBackend(CeedElemRestrictionGetNumElements(elem_rstr, &num_elem)); 4374b3e95d5SJeremy L Thompson CeedInt strides[3] = {1, elem_size * num_elem, elem_size}; 4384b3e95d5SJeremy L Thompson 4394b3e95d5SJeremy L Thompson if (!has_backend_strides) { 4404b3e95d5SJeremy L Thompson CeedCallBackend(CeedElemRestrictionGetStrides(elem_rstr, strides)); 4414b3e95d5SJeremy L Thompson } 4424b3e95d5SJeremy L Thompson code << " // Strides: {" << strides[0] << ", " << strides[1] << ", " << strides[2] << "}\n"; 44374398b5aSJeremy L Thompson code << " ReadLVecStrided" << (is_all_tensor ? max_dim : 1) << "d<num_comp" << var_suffix << ", " << P_name << ", " << strides[0] << ", " 4449123fb08SJeremy L Thompson << strides[1] << ", " << strides[2] << ">(data, elem, d" << var_suffix << ", r_e" << var_suffix << ");\n"; 445f815fac9SJeremy L Thompson break; 446f815fac9SJeremy L Thompson } 4473a2968d6SJeremy L Thompson case CEED_RESTRICTION_POINTS: { 4483a2968d6SJeremy L Thompson CeedInt comp_stride; 4493a2968d6SJeremy L Thompson 4503a2968d6SJeremy L Thompson CeedCallBackend(CeedElemRestrictionGetCompStride(elem_rstr, &comp_stride)); 4513a2968d6SJeremy L Thompson code << " const CeedInt comp_stride" << var_suffix << " = " << comp_stride << ";\n"; 4523a2968d6SJeremy L Thompson data->indices.inputs[i] = (CeedInt *)rstr_data->d_offsets; 4533a2968d6SJeremy L Thompson break; 4543a2968d6SJeremy L Thompson } 455f815fac9SJeremy L Thompson // LCOV_EXCL_START 456f815fac9SJeremy L Thompson case CEED_RESTRICTION_ORIENTED: 457f815fac9SJeremy L Thompson case CEED_RESTRICTION_CURL_ORIENTED: 458f815fac9SJeremy L Thompson break; // TODO: Not implemented 459f815fac9SJeremy L Thompson // LCOV_EXCL_STOP 4604b3e95d5SJeremy L Thompson } 4614b3e95d5SJeremy L Thompson } 4624b3e95d5SJeremy L Thompson } else { 4634b3e95d5SJeremy L Thompson // Output 464f815fac9SJeremy L Thompson switch (rstr_type) { 465f815fac9SJeremy L Thompson case CEED_RESTRICTION_STANDARD: { 4664b3e95d5SJeremy L Thompson CeedInt comp_stride; 4674b3e95d5SJeremy L Thompson 4684b3e95d5SJeremy L Thompson CeedCallBackend(CeedElemRestrictionGetLVectorSize(elem_rstr, &l_size)); 4694b3e95d5SJeremy L Thompson code << " const CeedInt l_size" << var_suffix << " = " << l_size << ";\n"; 4704b3e95d5SJeremy L Thompson CeedCallBackend(CeedElemRestrictionGetCompStride(elem_rstr, &comp_stride)); 4714b3e95d5SJeremy L Thompson code << " // CompStride: " << comp_stride << "\n"; 4724b3e95d5SJeremy L Thompson data->indices.outputs[i] = (CeedInt *)rstr_data->d_offsets; 47374398b5aSJeremy L Thompson code << " WriteLVecStandard" << (is_all_tensor ? max_dim : 1) << "d<num_comp" << var_suffix << ", " << comp_stride << ", " << P_name 4749123fb08SJeremy L Thompson << ">(data, l_size" << var_suffix << ", elem, indices.outputs[" << i << "], r_e" << var_suffix << ", d" << var_suffix << ");\n"; 475f815fac9SJeremy L Thompson break; 476f815fac9SJeremy L Thompson } 477f815fac9SJeremy L Thompson case CEED_RESTRICTION_STRIDED: { 4784b3e95d5SJeremy L Thompson bool has_backend_strides; 4794b3e95d5SJeremy L Thompson CeedInt num_elem; 4804b3e95d5SJeremy L Thompson 4814b3e95d5SJeremy L Thompson CeedCallBackend(CeedElemRestrictionHasBackendStrides(elem_rstr, &has_backend_strides)); 4824b3e95d5SJeremy L Thompson CeedCallBackend(CeedElemRestrictionGetNumElements(elem_rstr, &num_elem)); 4834b3e95d5SJeremy L Thompson CeedInt strides[3] = {1, elem_size * num_elem, elem_size}; 4844b3e95d5SJeremy L Thompson 4854b3e95d5SJeremy L Thompson if (!has_backend_strides) { 4864b3e95d5SJeremy L Thompson CeedCallBackend(CeedElemRestrictionGetStrides(elem_rstr, strides)); 4874b3e95d5SJeremy L Thompson } 4884b3e95d5SJeremy L Thompson code << " // Strides: {" << strides[0] << ", " << strides[1] << ", " << strides[2] << "}\n"; 48974398b5aSJeremy L Thompson code << " WriteLVecStrided" << (is_all_tensor ? max_dim : 1) << "d<num_comp" << var_suffix << ", " << P_name << ", " << strides[0] << ", " 4909123fb08SJeremy L Thompson << strides[1] << ", " << strides[2] << ">(data, elem, r_e" << var_suffix << ", d" << var_suffix << ");\n"; 491f815fac9SJeremy L Thompson break; 492f815fac9SJeremy L Thompson } 4933a2968d6SJeremy L Thompson case CEED_RESTRICTION_POINTS: 4943a2968d6SJeremy L Thompson data->indices.outputs[i] = (CeedInt *)rstr_data->d_offsets; 4953a2968d6SJeremy L Thompson break; 496f815fac9SJeremy L Thompson // LCOV_EXCL_START 497f815fac9SJeremy L Thompson case CEED_RESTRICTION_ORIENTED: 498f815fac9SJeremy L Thompson case CEED_RESTRICTION_CURL_ORIENTED: 499f815fac9SJeremy L Thompson break; // TODO: Not implemented 500f815fac9SJeremy L Thompson // LCOV_EXCL_STOP 5014b3e95d5SJeremy L Thompson } 5024b3e95d5SJeremy L Thompson } 5033a2968d6SJeremy L Thompson CeedCallBackend(CeedElemRestrictionDestroy(&elem_rstr)); 5044b3e95d5SJeremy L Thompson return CEED_ERROR_SUCCESS; 5054b3e95d5SJeremy L Thompson } 5064b3e95d5SJeremy L Thompson 5074b3e95d5SJeremy L Thompson //------------------------------------------------------------------------------ 5084b3e95d5SJeremy L Thompson // Basis 5094b3e95d5SJeremy L Thompson //------------------------------------------------------------------------------ 51074398b5aSJeremy L Thompson static int CeedOperatorBuildKernelBasis_Hip_gen(std::ostringstream &code, CeedOperator_Hip_gen *data, CeedInt i, CeedOperatorField op_field, 51174398b5aSJeremy L Thompson CeedQFunctionField qf_field, CeedInt max_dim, CeedInt Q_1d, bool is_input, bool is_all_tensor, 5123a2968d6SJeremy L Thompson bool is_at_points, bool use_3d_slices) { 51374398b5aSJeremy L Thompson bool is_tensor = true; 51474398b5aSJeremy L Thompson CeedBasis basis; 51574398b5aSJeremy L Thompson CeedCallBackend(CeedOperatorFieldGetBasis(op_field, &basis)); 51674398b5aSJeremy L Thompson CeedCallBackend(CeedBasisIsTensor(basis, &is_tensor)); 51774398b5aSJeremy L Thompson 5184b3e95d5SJeremy L Thompson std::string var_suffix = (is_input ? "_in_" : "_out_") + std::to_string(i); 5199123fb08SJeremy L Thompson std::string P_name = (is_tensor ? "P_1d" : "P") + var_suffix, Q_name = is_tensor ? "Q_1d" : "Q"; 5204b3e95d5SJeremy L Thompson CeedEvalMode eval_mode = CEED_EVAL_NONE; 52174398b5aSJeremy L Thompson CeedInt dim = max_dim, elem_size = 0, num_comp = 0, P_1d = 0; 5224b3e95d5SJeremy L Thompson CeedElemRestriction elem_rstr; 5234b3e95d5SJeremy L Thompson 5244b3e95d5SJeremy L Thompson // Get field data 5254b3e95d5SJeremy L Thompson CeedCallBackend(CeedOperatorFieldGetElemRestriction(op_field, &elem_rstr)); 5264b3e95d5SJeremy L Thompson if (elem_rstr != CEED_ELEMRESTRICTION_NONE) { 5274b3e95d5SJeremy L Thompson CeedCallBackend(CeedElemRestrictionGetElementSize(elem_rstr, &elem_size)); 5284b3e95d5SJeremy L Thompson CeedCallBackend(CeedElemRestrictionGetNumComponents(elem_rstr, &num_comp)); 5294b3e95d5SJeremy L Thompson } 5303a2968d6SJeremy L Thompson CeedCallBackend(CeedElemRestrictionDestroy(&elem_rstr)); 5314b3e95d5SJeremy L Thompson if (basis != CEED_BASIS_NONE) { 53274398b5aSJeremy L Thompson CeedCallBackend(CeedBasisGetDimension(basis, &dim)); 5339123fb08SJeremy L Thompson if (is_tensor) CeedCallBackend(CeedBasisGetNumNodes1D(basis, &P_1d)); 5349123fb08SJeremy L Thompson else CeedCallBackend(CeedBasisGetNumNodes(basis, &P_1d)); 5354b3e95d5SJeremy L Thompson } 5364b3e95d5SJeremy L Thompson CeedCallBackend(CeedQFunctionFieldGetEvalMode(qf_field, &eval_mode)); 5374b3e95d5SJeremy L Thompson 5384b3e95d5SJeremy L Thompson // Basis 5394b3e95d5SJeremy L Thompson code << " // EvalMode: " << CeedEvalModes[eval_mode] << "\n"; 5404b3e95d5SJeremy L Thompson if (is_input) { 5414b3e95d5SJeremy L Thompson switch (eval_mode) { 5424b3e95d5SJeremy L Thompson case CEED_EVAL_NONE: 5433a2968d6SJeremy L Thompson if (!use_3d_slices && !is_at_points) { 5444b3e95d5SJeremy L Thompson code << " CeedScalar *r_q" << var_suffix << " = r_e" << var_suffix << ";\n"; 5454b3e95d5SJeremy L Thompson } 5464b3e95d5SJeremy L Thompson break; 5474b3e95d5SJeremy L Thompson case CEED_EVAL_INTERP: 5483a2968d6SJeremy L Thompson if (is_at_points) { 5499123fb08SJeremy L Thompson std::string function_name = (dim == 1 ? "Interp" : "InterpTensor") + std::to_string(dim) + "d"; 5509123fb08SJeremy L Thompson 5519123fb08SJeremy L Thompson code << " CeedScalar r_c" << var_suffix << "[num_comp" << var_suffix << "*" << (dim >= 3 ? Q_name : "1") << "];\n"; 5526b92dc4bSJeremy L Thompson code << " " << function_name << "<num_comp" << var_suffix << ", " << P_name << ", " << Q_name << ", OP_T_1D>(data, r_e" << var_suffix 5536b92dc4bSJeremy L Thompson << ", s_B" << var_suffix << ", r_c" << var_suffix << ");\n"; 5543a2968d6SJeremy L Thompson } else { 55574398b5aSJeremy L Thompson std::string function_name = is_tensor 55674398b5aSJeremy L Thompson ? ((dim == 1 ? "Interp" : "InterpTensor") + std::to_string(dim) + "d" + (is_all_tensor ? "" : "Flattened")) 55774398b5aSJeremy L Thompson : "InterpNonTensor"; 55874398b5aSJeremy L Thompson std::string op_t_1d_name = (is_all_tensor || !is_tensor) ? "OP_T_1D" : (P_1d > Q_1d ? P_name : Q_name); 5599123fb08SJeremy L Thompson 56074398b5aSJeremy L Thompson code << " CeedScalar r_q" << var_suffix << "[num_comp" << var_suffix << "*" << (is_all_tensor && (dim >= 3) ? Q_name : "1") << "];\n"; 56174398b5aSJeremy L Thompson code << " " << function_name << "<num_comp" << var_suffix << ", " << P_name << ", " << Q_name << ", " << op_t_1d_name << ">(data, r_e" 56274398b5aSJeremy L Thompson << var_suffix << ", s_B" << var_suffix << ", r_q" << var_suffix << ");\n"; 5633a2968d6SJeremy L Thompson } 5644b3e95d5SJeremy L Thompson break; 5654b3e95d5SJeremy L Thompson case CEED_EVAL_GRAD: 5663a2968d6SJeremy L Thompson if (is_at_points) { 5679123fb08SJeremy L Thompson std::string function_name = (dim == 1 ? "Interp" : "InterpTensor") + std::to_string(dim) + "d"; 5689123fb08SJeremy L Thompson 5699123fb08SJeremy L Thompson code << " CeedScalar r_c" << var_suffix << "[num_comp" << var_suffix << "*" << (dim >= 3 ? Q_name : "1") << "];\n"; 5706b92dc4bSJeremy L Thompson code << " " << function_name << "<num_comp" << var_suffix << ", " << P_name << ", " << Q_name << ", OP_T_1D>(data, r_e" << var_suffix 5716b92dc4bSJeremy L Thompson << ", s_B" << var_suffix << ", r_c" << var_suffix << ");\n"; 5723a2968d6SJeremy L Thompson } else if (use_3d_slices) { 5739123fb08SJeremy L Thompson std::string function_name = (dim > 1 ? "InterpTensor" : "Interp") + std::to_string(dim) + "d"; 5749123fb08SJeremy L Thompson 5754b3e95d5SJeremy L Thompson code << " CeedScalar r_q" << var_suffix << "[num_comp" << var_suffix << "*" << Q_name << "];\n"; 5766b92dc4bSJeremy L Thompson code << " " << 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_q" << var_suffix << ");\n"; 5789123fb08SJeremy L Thompson } else if (is_tensor) { 5799123fb08SJeremy L Thompson bool is_collocated = dim == 3 && Q_1d >= P_1d; 58074398b5aSJeremy L Thompson std::string function_name = (dim == 1 ? "Grad" : (is_collocated ? "GradTensorCollocated" : "GradTensor")) + std::to_string(dim) + "d" + 58174398b5aSJeremy L Thompson (is_all_tensor ? "" : "Flattened"); 58274398b5aSJeremy L Thompson std::string op_t_1d_name = is_all_tensor ? "OP_T_1D" : (P_1d > Q_1d ? P_name : Q_name); 5839123fb08SJeremy L Thompson 58474398b5aSJeremy L Thompson code << " CeedScalar r_q" << var_suffix << "[num_comp" << var_suffix << "*dim" << var_suffix << "*" 58574398b5aSJeremy L Thompson << (is_all_tensor && dim >= 3 ? Q_name : "1") << "];\n"; 58674398b5aSJeremy L Thompson code << " " << function_name << "<num_comp" << var_suffix << ", " << P_name << ", " << Q_name << ", " << op_t_1d_name << ">(data, r_e" 58774398b5aSJeremy L Thompson << var_suffix << ", s_B" << var_suffix << ", s_G" << var_suffix << ", r_q" << var_suffix << ");\n"; 5884b3e95d5SJeremy L Thompson } else { 5899123fb08SJeremy L Thompson std::string function_name = "GradNonTensor"; 5909123fb08SJeremy L Thompson 59174398b5aSJeremy L Thompson code << " CeedScalar r_q" << var_suffix << "[num_comp" << var_suffix << "*dim" << var_suffix << "];\n"; 59274398b5aSJeremy L Thompson code << " " << function_name << "<num_comp" << var_suffix << ", dim" << var_suffix << ", " << P_name << ", " << Q_name 59374398b5aSJeremy L Thompson << ", OP_T_1D>(data, r_e" << var_suffix << ", s_G" << var_suffix << ", r_q" << var_suffix << ");\n"; 5944b3e95d5SJeremy L Thompson } 5954b3e95d5SJeremy L Thompson break; 5964b3e95d5SJeremy L Thompson case CEED_EVAL_WEIGHT: { 5973a2968d6SJeremy L Thompson if (is_at_points) { 5983a2968d6SJeremy L Thompson code << " // Nothing to do AtPoints\n"; 5993a2968d6SJeremy L Thompson } else { 6004b3e95d5SJeremy L Thompson CeedBasis_Hip_shared *basis_data; 60174398b5aSJeremy L Thompson std::string function_name = is_tensor 60274398b5aSJeremy L Thompson ? ((dim == 1 ? "Weight" : "WeightTensor") + std::to_string(dim) + "d" + (is_all_tensor ? "" : "Flattened")) 60374398b5aSJeremy L Thompson : "WeightNonTensor"; 6044b3e95d5SJeremy L Thompson 60574398b5aSJeremy L Thompson code << " CeedScalar r_q" << var_suffix << "[" << (is_all_tensor && (dim >= 3) ? Q_name : "1") << "];\n"; 6064b3e95d5SJeremy L Thompson CeedCallBackend(CeedBasisGetData(basis, &basis_data)); 6074b3e95d5SJeremy L Thompson data->W = basis_data->d_q_weight_1d; 608343e3094SJeremy L Thompson code << " " << function_name << "<" << P_name << ", " << Q_name << ">(data, W, r_q" << var_suffix << ");\n"; 6093a2968d6SJeremy L Thompson } 6104b3e95d5SJeremy L Thompson break; 6114b3e95d5SJeremy L Thompson } 6124b3e95d5SJeremy L Thompson // LCOV_EXCL_START 6134b3e95d5SJeremy L Thompson case CEED_EVAL_DIV: 6144b3e95d5SJeremy L Thompson case CEED_EVAL_CURL: 6154b3e95d5SJeremy L Thompson break; // TODO: Not implemented 6164b3e95d5SJeremy L Thompson // LCOV_EXCL_STOP 6174b3e95d5SJeremy L Thompson } 6184b3e95d5SJeremy L Thompson } else { 6194b3e95d5SJeremy L Thompson switch (eval_mode) { 6204b3e95d5SJeremy L Thompson case CEED_EVAL_NONE: 6214b3e95d5SJeremy L Thompson code << " CeedScalar *r_e" << var_suffix << " = r_q" << var_suffix << ";\n"; 6224b3e95d5SJeremy L Thompson break; // No action 6234b3e95d5SJeremy L Thompson case CEED_EVAL_INTERP: 624e93651e5SJeremy L Thompson code << " CeedScalar *r_e" << var_suffix << " = r_e_scratch;\n"; 6253a2968d6SJeremy L Thompson if (is_at_points) { 6269123fb08SJeremy L Thompson std::string function_name = (dim == 1 ? "InterpTranspose" : "InterpTransposeTensor") + std::to_string(dim) + "d"; 6279123fb08SJeremy L Thompson 6286b92dc4bSJeremy L Thompson code << " " << function_name << "<num_comp" << var_suffix << ", " << P_name << ", " << Q_name << ", OP_T_1D>(data, r_c" << var_suffix 6296b92dc4bSJeremy L Thompson << ", s_B" << var_suffix << ", r_e" << var_suffix << ");\n"; 6303a2968d6SJeremy L Thompson } else { 6319123fb08SJeremy L Thompson std::string function_name = 63274398b5aSJeremy L Thompson is_tensor ? ((dim == 1 ? "InterpTranspose" : "InterpTransposeTensor") + std::to_string(dim) + "d" + (is_all_tensor ? "" : "Flattened")) 63374398b5aSJeremy L Thompson : "InterpTransposeNonTensor"; 63474398b5aSJeremy L Thompson std::string op_t_1d_name = (is_all_tensor || !is_tensor) ? "OP_T_1D" : (P_1d > Q_1d ? P_name : Q_name); 6359123fb08SJeremy L Thompson 63674398b5aSJeremy L Thompson code << " " << function_name << "<num_comp" << var_suffix << ", " << P_name << ", " << Q_name << ", " << op_t_1d_name << ">(data, r_q" 63774398b5aSJeremy L Thompson << var_suffix << ", s_B" << var_suffix << ", r_e" << var_suffix << ");\n"; 6383a2968d6SJeremy L Thompson } 6394b3e95d5SJeremy L Thompson break; 6404b3e95d5SJeremy L Thompson case CEED_EVAL_GRAD: 641e93651e5SJeremy L Thompson code << " CeedScalar *r_e" << var_suffix << " = r_e_scratch;\n"; 6423a2968d6SJeremy L Thompson if (is_at_points) { 6439123fb08SJeremy L Thompson std::string function_name = (dim == 1 ? "InterpTranspose" : "InterpTransposeTensor") + std::to_string(dim) + "d"; 6449123fb08SJeremy L Thompson 6456b92dc4bSJeremy L Thompson code << " " << function_name << "<num_comp" << var_suffix << ", " << P_name << ", " << Q_name << ", OP_T_1D>(data, r_c" << var_suffix 6466b92dc4bSJeremy L Thompson << ", s_B" << var_suffix << ", r_e" << var_suffix << ");\n"; 6473a2968d6SJeremy L Thompson } else if (use_3d_slices) { 6489123fb08SJeremy L Thompson std::string function_name = (dim == 1 ? "InterpTranspose" : "InterpTransposeTensor") + std::to_string(dim) + "d"; 6499123fb08SJeremy L Thompson 6506b92dc4bSJeremy L Thompson code << " " << function_name << "<num_comp" << var_suffix << ", " << P_name << ", " << Q_name << ", OP_T_1D>(data, r_q" << var_suffix 6516b92dc4bSJeremy L Thompson << ", s_B" << var_suffix << ", r_e" << var_suffix << ");\n"; 6529123fb08SJeremy L Thompson } else if (is_tensor) { 6539123fb08SJeremy L Thompson bool is_collocated = dim == 3 && Q_1d >= P_1d; 65474398b5aSJeremy L Thompson std::string function_name = (dim == 1 ? "GradTranspose" : (is_collocated ? "GradTransposeTensorCollocated" : "GradTransposeTensor")) + 65574398b5aSJeremy L Thompson std::to_string(dim) + "d" + (is_all_tensor ? "" : "Flattened"); 65674398b5aSJeremy L Thompson std::string op_t_1d_name = is_all_tensor ? "OP_T_1D" : (P_1d > Q_1d ? P_name : Q_name); 6579123fb08SJeremy L Thompson 65874398b5aSJeremy L Thompson code << " " << function_name << "<num_comp" << var_suffix << ", " << P_name << ", " << Q_name << ", " << op_t_1d_name << ">(data, r_q" 65974398b5aSJeremy L Thompson << var_suffix << ", s_B" << var_suffix << ", s_G" << var_suffix << ", r_e" << var_suffix << ");\n"; 6604b3e95d5SJeremy L Thompson } else { 6619123fb08SJeremy L Thompson std::string function_name = "GradTransposeNonTensor"; 6629123fb08SJeremy L Thompson 66374398b5aSJeremy L Thompson code << " " << function_name << "<num_comp" << var_suffix << ", dim" << var_suffix << ", " << P_name << ", " << Q_name 66474398b5aSJeremy L Thompson << ", OP_T_1D>(data, r_q" << var_suffix << ", s_G" << var_suffix << ", r_e" << var_suffix << ");\n"; 6654b3e95d5SJeremy L Thompson } 6664b3e95d5SJeremy L Thompson break; 6674b3e95d5SJeremy L Thompson // LCOV_EXCL_START 6684b3e95d5SJeremy L Thompson case CEED_EVAL_WEIGHT: 6694b3e95d5SJeremy L Thompson break; // Should not occur 6704b3e95d5SJeremy L Thompson case CEED_EVAL_DIV: 6714b3e95d5SJeremy L Thompson case CEED_EVAL_CURL: 6724b3e95d5SJeremy L Thompson break; // TODO: Not implemented 6734b3e95d5SJeremy L Thompson // LCOV_EXCL_STOP 6744b3e95d5SJeremy L Thompson } 6754b3e95d5SJeremy L Thompson } 6763a2968d6SJeremy L Thompson CeedCallBackend(CeedBasisDestroy(&basis)); 6774b3e95d5SJeremy L Thompson return CEED_ERROR_SUCCESS; 6784b3e95d5SJeremy L Thompson } 6794b3e95d5SJeremy L Thompson 6804b3e95d5SJeremy L Thompson //------------------------------------------------------------------------------ 6814b3e95d5SJeremy L Thompson // QFunction 6824b3e95d5SJeremy L Thompson //------------------------------------------------------------------------------ 68374398b5aSJeremy L Thompson static int CeedOperatorBuildKernelQFunction_Hip_gen(std::ostringstream &code, CeedOperator_Hip_gen *data, CeedInt max_dim, CeedInt max_num_points, 6843a2968d6SJeremy L Thompson CeedInt num_input_fields, CeedOperatorField *op_input_fields, CeedQFunctionField *qf_input_fields, 6854b3e95d5SJeremy L Thompson CeedInt num_output_fields, CeedOperatorField *op_output_fields, 68674398b5aSJeremy L Thompson CeedQFunctionField *qf_output_fields, std::string qfunction_name, CeedInt Q_1d, 68774398b5aSJeremy L Thompson bool is_all_tensor, bool is_at_points, bool use_3d_slices) { 68874398b5aSJeremy L Thompson std::string Q_name = is_all_tensor ? "Q_1d" : "Q"; 6894b3e95d5SJeremy L Thompson CeedEvalMode eval_mode = CEED_EVAL_NONE; 6904b3e95d5SJeremy L Thompson CeedElemRestriction elem_rstr; 6914b3e95d5SJeremy L Thompson 6928b97b69aSJeremy L Thompson // Setup output arrays 6934b3e95d5SJeremy L Thompson code << "\n // -- Output field setup\n"; 6944b3e95d5SJeremy L Thompson for (CeedInt i = 0; i < num_output_fields; i++) { 69559fa3f92SJeremy L Thompson const char *field_name; 6964b3e95d5SJeremy L Thompson std::string var_suffix = "_out_" + std::to_string(i); 6974b3e95d5SJeremy L Thompson 69859fa3f92SJeremy L Thompson CeedCallBackend(CeedOperatorFieldGetName(op_output_fields[i], &field_name)); 69959fa3f92SJeremy L Thompson code << " // ---- Output field " << i << ": " << field_name << "\n"; 7004b3e95d5SJeremy L Thompson CeedCallBackend(CeedQFunctionFieldGetEvalMode(qf_output_fields[i], &eval_mode)); 7013a2968d6SJeremy L Thompson switch (eval_mode) { 7023a2968d6SJeremy L Thompson case CEED_EVAL_NONE: 7033a2968d6SJeremy L Thompson if (is_at_points) { 7043a2968d6SJeremy L Thompson code << " CeedScalar r_q" << var_suffix << "[num_comp" << var_suffix << "];\n"; 7053a2968d6SJeremy L Thompson } else { 70674398b5aSJeremy L Thompson code << " CeedScalar r_q" << var_suffix << "[num_comp" << var_suffix << "*" << (is_all_tensor && (max_dim >= 3) ? Q_name : "1") 70774398b5aSJeremy L Thompson << "];\n"; 7084b3e95d5SJeremy L Thompson } 7093a2968d6SJeremy L Thompson break; 7103a2968d6SJeremy L Thompson case CEED_EVAL_INTERP: 7113a2968d6SJeremy L Thompson if (is_at_points) { 7123a2968d6SJeremy L Thompson // Accumulator for point data 71374398b5aSJeremy L Thompson code << " CeedScalar r_c" << var_suffix << "[num_comp" << var_suffix << "*" << (max_dim >= 3 ? Q_name : "1") << "];\n"; 714b8245c6cSJeremy L Thompson code << " for (CeedInt i = 0; i < num_comp" << var_suffix << "*" << (max_dim >= 3 ? Q_name : "1") << "; i++) r_c" << var_suffix 715b8245c6cSJeremy L Thompson << "[i] = 0.0;\n"; 7163a2968d6SJeremy L Thompson } else { 71774398b5aSJeremy L Thompson code << " CeedScalar r_q" << var_suffix << "[num_comp" << var_suffix << "*" << (is_all_tensor && (max_dim >= 3) ? Q_name : "1") 71874398b5aSJeremy L Thompson << "];\n"; 7193a2968d6SJeremy L Thompson } 7203a2968d6SJeremy L Thompson break; 7213a2968d6SJeremy L Thompson case CEED_EVAL_GRAD: 7223a2968d6SJeremy L Thompson if (is_at_points) { 7233a2968d6SJeremy L Thompson // Accumulator for point data 724a61b1c91SJeremy L Thompson code << " CeedScalar r_c" << var_suffix << "[num_comp" << var_suffix << "*" << (max_dim >= 3 ? Q_name : "1") << "];\n"; 725b8245c6cSJeremy L Thompson code << " for (CeedInt i = 0; i < num_comp" << var_suffix << "*" << (max_dim >= 3 ? Q_name : "1") << "; i++) r_c" << var_suffix 726b8245c6cSJeremy L Thompson << "[i] = 0.0;\n"; 7273a2968d6SJeremy L Thompson } else if (use_3d_slices) { 7284b3e95d5SJeremy L Thompson // Accumulator for gradient slices 7294b3e95d5SJeremy L Thompson code << " CeedScalar r_q" << var_suffix << "[num_comp" << var_suffix << "*" << Q_name << "];\n"; 730b8245c6cSJeremy L Thompson code << " for (CeedInt i = 0; i < num_comp" << var_suffix << "*" << Q_name << "; i++) r_q" << var_suffix << "[i] = 0.0;\n"; 7314b3e95d5SJeremy L Thompson } else { 73274398b5aSJeremy L Thompson code << " CeedScalar r_q" << var_suffix << "[num_comp" << var_suffix << "*dim" << var_suffix << "*" 73374398b5aSJeremy L Thompson << (is_all_tensor && (max_dim >= 3) ? Q_name : "1") << "];\n"; 7344b3e95d5SJeremy L Thompson } 7353a2968d6SJeremy L Thompson break; 7363a2968d6SJeremy L Thompson case CEED_EVAL_WEIGHT: 7373a2968d6SJeremy L Thompson break; 7383a2968d6SJeremy L Thompson // LCOV_EXCL_START 7393a2968d6SJeremy L Thompson case CEED_EVAL_DIV: 7403a2968d6SJeremy L Thompson case CEED_EVAL_CURL: 7413a2968d6SJeremy L Thompson break; // TODO: Not implemented 7423a2968d6SJeremy L Thompson // LCOV_EXCL_STOP 7434b3e95d5SJeremy L Thompson } 7444b3e95d5SJeremy L Thompson } 7454b3e95d5SJeremy L Thompson 7463a2968d6SJeremy L Thompson if (is_at_points) { 7473a2968d6SJeremy L Thompson // We need to handle batches of points 7483a2968d6SJeremy L Thompson code << "\n // Note: Using batches of points\n"; 749a61b1c91SJeremy L Thompson code << " const CeedInt point_loop_bound = (blockDim.x*blockDim.y) * ceil((1.0*max_num_points) / (blockDim.x*blockDim.y));\n\n"; 7503a2968d6SJeremy L Thompson code << " #pragma unroll\n"; 7513a2968d6SJeremy L Thompson code << " for (CeedInt i = threadIdx.x + threadIdx.y*blockDim.x; i < point_loop_bound; i += blockDim.x*blockDim.y) {\n"; 7523a2968d6SJeremy L Thompson code << " const CeedInt p = i % max_num_points;\n\n"; 7533a2968d6SJeremy L Thompson 7543a2968d6SJeremy L Thompson code << " // -- Coordinates\n"; 75574398b5aSJeremy L Thompson code << " CeedScalar r_x[max_dim];\n"; 75674398b5aSJeremy L Thompson code << " ReadPoint<max_dim, coords_comp_stride, max_num_points>(data, elem, p, max_num_points, points.indices, points.coords, r_x);\n\n"; 7573a2968d6SJeremy L Thompson 7583a2968d6SJeremy L Thompson code << " // -- Input fields\n"; 7593a2968d6SJeremy L Thompson for (CeedInt i = 0; i < num_input_fields; i++) { 76059fa3f92SJeremy L Thompson const char *field_name; 7613a2968d6SJeremy L Thompson std::string var_suffix = "_in_" + std::to_string(i); 762f725b54bSJeremy L Thompson std::string P_name = "P_1d" + var_suffix; 7633a2968d6SJeremy L Thompson 76459fa3f92SJeremy L Thompson CeedCallBackend(CeedOperatorFieldGetName(op_input_fields[i], &field_name)); 76559fa3f92SJeremy L Thompson code << " // ---- Input field " << i << ": " << field_name << "\n"; 7663a2968d6SJeremy L Thompson CeedCallBackend(CeedQFunctionFieldGetEvalMode(qf_input_fields[i], &eval_mode)); 7673a2968d6SJeremy L Thompson // Basis action 7683a2968d6SJeremy L Thompson code << " // EvalMode: " << CeedEvalModes[eval_mode] << "\n"; 7693a2968d6SJeremy L Thompson switch (eval_mode) { 7703a2968d6SJeremy L Thompson case CEED_EVAL_NONE: 7713a2968d6SJeremy L Thompson code << " CeedScalar r_s" << var_suffix << "[num_comp" << var_suffix << "];\n"; 7723a2968d6SJeremy L Thompson code << " ReadPoint<num_comp" << var_suffix << ", comp_stride" << var_suffix 7733a2968d6SJeremy L Thompson << ", max_num_points>(data, elem, p, max_num_points, indices.inputs[" << i << "], d" << var_suffix << ", r_s" << var_suffix << ");\n"; 7743a2968d6SJeremy L Thompson break; 7753a2968d6SJeremy L Thompson case CEED_EVAL_INTERP: 7763a2968d6SJeremy L Thompson code << " CeedScalar r_s" << var_suffix << "[num_comp" << var_suffix << "];\n"; 77774398b5aSJeremy L Thompson code << " InterpAtPoints" << max_dim << "d<num_comp" << var_suffix << ", max_num_points, " << P_name << ", " << Q_name 77874398b5aSJeremy L Thompson << ">(data, i, r_c" << var_suffix << ", r_x, r_s" << var_suffix << ");\n"; 7793a2968d6SJeremy L Thompson break; 7803a2968d6SJeremy L Thompson case CEED_EVAL_GRAD: 78174398b5aSJeremy L Thompson code << " CeedScalar r_s" << var_suffix << "[num_comp" << var_suffix << "*dim" << var_suffix << "];\n"; 78274398b5aSJeremy L Thompson code << " GradAtPoints" << max_dim << "d<num_comp" << var_suffix << ", max_num_points, " << P_name << ", " << Q_name 78374398b5aSJeremy L Thompson << ">(data, i, r_c" << var_suffix << ", r_x, r_s" << var_suffix << ");\n"; 7843a2968d6SJeremy L Thompson break; 7853a2968d6SJeremy L Thompson case CEED_EVAL_WEIGHT: 7863a2968d6SJeremy L Thompson code << " CeedScalar r_s" << var_suffix << "[1];\n"; 7873a2968d6SJeremy L Thompson code << " r_s" << var_suffix << "[0] = 1.0;\n"; 7883a2968d6SJeremy L Thompson break; 7893a2968d6SJeremy L Thompson // LCOV_EXCL_START 7903a2968d6SJeremy L Thompson case CEED_EVAL_DIV: 7913a2968d6SJeremy L Thompson case CEED_EVAL_CURL: 7923a2968d6SJeremy L Thompson break; // TODO: Not implemented 7933a2968d6SJeremy L Thompson // LCOV_EXCL_STOP 7943a2968d6SJeremy L Thompson } 7953a2968d6SJeremy L Thompson } 7963a2968d6SJeremy L Thompson code << "\n // -- Output fields\n"; 7973a2968d6SJeremy L Thompson for (CeedInt i = 0; i < num_output_fields; i++) { 79859fa3f92SJeremy L Thompson const char *field_name; 7993a2968d6SJeremy L Thompson std::string var_suffix = "_out_" + std::to_string(i); 8003a2968d6SJeremy L Thompson 80159fa3f92SJeremy L Thompson CeedCallBackend(CeedOperatorFieldGetName(op_output_fields[i], &field_name)); 80259fa3f92SJeremy L Thompson code << " // ---- Output field " << i << ": " << field_name << "\n"; 8033a2968d6SJeremy L Thompson CeedCallBackend(CeedQFunctionFieldGetEvalMode(qf_output_fields[i], &eval_mode)); 8043a2968d6SJeremy L Thompson // Basis action 8053a2968d6SJeremy L Thompson switch (eval_mode) { 8063a2968d6SJeremy L Thompson case CEED_EVAL_NONE: 8073a2968d6SJeremy L Thompson code << " CeedScalar r_s" << var_suffix << "[num_comp" << var_suffix << "];\n"; 8083a2968d6SJeremy L Thompson break; 8093a2968d6SJeremy L Thompson case CEED_EVAL_INTERP: 8103a2968d6SJeremy L Thompson code << " CeedScalar r_s" << var_suffix << "[num_comp" << var_suffix << "];\n"; 8113a2968d6SJeremy L Thompson break; 8123a2968d6SJeremy L Thompson case CEED_EVAL_GRAD: 81374398b5aSJeremy L Thompson code << " CeedScalar r_s" << var_suffix << "[num_comp" << var_suffix << "*dim" << var_suffix << "];\n"; 8143a2968d6SJeremy L Thompson break; 8153a2968d6SJeremy L Thompson // LCOV_EXCL_START 8163a2968d6SJeremy L Thompson case CEED_EVAL_WEIGHT: 8173a2968d6SJeremy L Thompson break; // Should not occur 8183a2968d6SJeremy L Thompson case CEED_EVAL_DIV: 8193a2968d6SJeremy L Thompson case CEED_EVAL_CURL: 8203a2968d6SJeremy L Thompson break; // TODO: Not implemented 8213a2968d6SJeremy L Thompson // LCOV_EXCL_STOP 8223a2968d6SJeremy L Thompson } 8233a2968d6SJeremy L Thompson } 8243a2968d6SJeremy L Thompson 8253a2968d6SJeremy L Thompson } else if (use_3d_slices) { 8264b3e95d5SJeremy L Thompson // We treat quadrature points per slice in 3d to save registers 8274b3e95d5SJeremy L Thompson code << "\n // Note: Using planes of 3D elements\n"; 8284b3e95d5SJeremy L Thompson code << " #pragma unroll\n"; 8294b3e95d5SJeremy L Thompson code << " for (CeedInt q = 0; q < " << Q_name << "; q++) {\n"; 8304b3e95d5SJeremy L Thompson code << " // -- Input fields\n"; 8314b3e95d5SJeremy L Thompson for (CeedInt i = 0; i < num_input_fields; i++) { 83259fa3f92SJeremy L Thompson const char *field_name; 8334b3e95d5SJeremy L Thompson std::string var_suffix = "_in_" + std::to_string(i); 8344b3e95d5SJeremy L Thompson 83559fa3f92SJeremy L Thompson CeedCallBackend(CeedOperatorFieldGetName(op_input_fields[i], &field_name)); 83659fa3f92SJeremy L Thompson code << " // ---- Input field " << i << ": " << field_name << "\n"; 8374b3e95d5SJeremy L Thompson CeedCallBackend(CeedQFunctionFieldGetEvalMode(qf_input_fields[i], &eval_mode)); 8384b3e95d5SJeremy L Thompson // Basis action 8394b3e95d5SJeremy L Thompson code << " // EvalMode: " << CeedEvalModes[eval_mode] << "\n"; 8404b3e95d5SJeremy L Thompson switch (eval_mode) { 8414b3e95d5SJeremy L Thompson case CEED_EVAL_NONE: 8424b3e95d5SJeremy L Thompson bool is_strided; 8434b3e95d5SJeremy L Thompson 8444b3e95d5SJeremy L Thompson code << " CeedScalar r_s" << var_suffix << "[num_comp" << var_suffix << "];\n"; 8454b3e95d5SJeremy L Thompson 8464b3e95d5SJeremy L Thompson CeedCallBackend(CeedOperatorFieldGetElemRestriction(op_input_fields[i], &elem_rstr)); 8474b3e95d5SJeremy L Thompson CeedCallBackend(CeedElemRestrictionIsStrided(elem_rstr, &is_strided)); 8484b3e95d5SJeremy L Thompson if (is_strided) { 8494b3e95d5SJeremy L Thompson bool has_backend_strides; 8504b3e95d5SJeremy L Thompson CeedInt num_elem, elem_size; 8514b3e95d5SJeremy L Thompson 8524b3e95d5SJeremy L Thompson CeedCallBackend(CeedElemRestrictionGetElementSize(elem_rstr, &elem_size)); 8534b3e95d5SJeremy L Thompson CeedCallBackend(CeedElemRestrictionHasBackendStrides(elem_rstr, &has_backend_strides)); 8544b3e95d5SJeremy L Thompson CeedCallBackend(CeedElemRestrictionGetNumElements(elem_rstr, &num_elem)); 8554b3e95d5SJeremy L Thompson CeedInt strides[3] = {1, elem_size * num_elem, elem_size}; 8564b3e95d5SJeremy L Thompson 8574b3e95d5SJeremy L Thompson if (!has_backend_strides) { 8584b3e95d5SJeremy L Thompson CeedCallBackend(CeedElemRestrictionGetStrides(elem_rstr, strides)); 8594b3e95d5SJeremy L Thompson } 8604b3e95d5SJeremy L Thompson code << " // Strides: {" << strides[0] << ", " << strides[1] << ", " << strides[2] << "}\n"; 861f815fac9SJeremy L Thompson code << " ReadEVecSliceStrided3d<num_comp" << var_suffix << ", " << Q_name << ", " << strides[0] << ", " << strides[1] << ", " 8624b3e95d5SJeremy L Thompson << strides[2] << ">(data, elem, q, d" << var_suffix << ", r_s" << var_suffix << ");\n"; 8634b3e95d5SJeremy L Thompson } else { 8644b3e95d5SJeremy L Thompson CeedSize l_size = 0; 8654b3e95d5SJeremy L Thompson CeedInt comp_stride; 8664b3e95d5SJeremy L Thompson CeedElemRestriction_Hip *rstr_data; 8674b3e95d5SJeremy L Thompson 8684b3e95d5SJeremy L Thompson CeedCallBackend(CeedElemRestrictionGetLVectorSize(elem_rstr, &l_size)); 8694b3e95d5SJeremy L Thompson code << " const CeedInt l_size" << var_suffix << " = " << l_size << ";\n"; 8704b3e95d5SJeremy L Thompson CeedCallBackend(CeedElemRestrictionGetCompStride(elem_rstr, &comp_stride)); 8714b3e95d5SJeremy L Thompson code << " // CompStride: " << comp_stride << "\n"; 8724b3e95d5SJeremy L Thompson CeedCallBackend(CeedElemRestrictionGetData(elem_rstr, &rstr_data)); 8734b3e95d5SJeremy L Thompson data->indices.inputs[i] = (CeedInt *)rstr_data->d_offsets; 874f815fac9SJeremy L Thompson code << " ReadEVecSliceStandard3d<num_comp" << var_suffix << ", " << comp_stride << ", " << Q_name << ">(data, l_size" << var_suffix 8754b3e95d5SJeremy L Thompson << ", elem, q, indices.inputs[" << i << "], d" << var_suffix << ", r_s" << var_suffix << ");\n"; 8764b3e95d5SJeremy L Thompson } 8779123fb08SJeremy L Thompson CeedCallBackend(CeedElemRestrictionDestroy(&elem_rstr)); 8784b3e95d5SJeremy L Thompson break; 8794b3e95d5SJeremy L Thompson case CEED_EVAL_INTERP: 8804b3e95d5SJeremy L Thompson code << " CeedScalar r_s" << var_suffix << "[num_comp" << var_suffix << "];\n"; 8814b3e95d5SJeremy L Thompson code << " for (CeedInt j = 0; j < num_comp" << var_suffix << "; j++) {\n"; 8824b3e95d5SJeremy L Thompson code << " r_s" << var_suffix << "[j] = r_q" << var_suffix << "[q + j*" << Q_name << "];\n"; 8834b3e95d5SJeremy L Thompson code << " }\n"; 8844b3e95d5SJeremy L Thompson break; 8854b3e95d5SJeremy L Thompson case CEED_EVAL_GRAD: 88674398b5aSJeremy L Thompson code << " CeedScalar r_s" << var_suffix << "[num_comp" << var_suffix << "*dim" << var_suffix << "];\n"; 8876b92dc4bSJeremy L Thompson code << " GradColloSlice3d<num_comp" << var_suffix << ", " << Q_name << ", OP_T_1D>(data, q, r_q" << var_suffix << ", s_G" 8886b92dc4bSJeremy L Thompson << var_suffix << ", r_s" << var_suffix << ");\n"; 8894b3e95d5SJeremy L Thompson break; 8904b3e95d5SJeremy L Thompson case CEED_EVAL_WEIGHT: 8914b3e95d5SJeremy L Thompson code << " CeedScalar r_s" << var_suffix << "[1];\n"; 8924b3e95d5SJeremy L Thompson code << " r_s" << var_suffix << "[0] = r_q" << var_suffix << "[q];\n"; 8933a2968d6SJeremy L Thompson break; 8944b3e95d5SJeremy L Thompson // LCOV_EXCL_START 8954b3e95d5SJeremy L Thompson case CEED_EVAL_DIV: 8964b3e95d5SJeremy L Thompson case CEED_EVAL_CURL: 8974b3e95d5SJeremy L Thompson break; // TODO: Not implemented 8984b3e95d5SJeremy L Thompson // LCOV_EXCL_STOP 8994b3e95d5SJeremy L Thompson } 9004b3e95d5SJeremy L Thompson } 9014b3e95d5SJeremy L Thompson code << "\n // -- Output fields\n"; 9024b3e95d5SJeremy L Thompson for (CeedInt i = 0; i < num_output_fields; i++) { 90359fa3f92SJeremy L Thompson const char *field_name; 9044b3e95d5SJeremy L Thompson std::string var_suffix = "_out_" + std::to_string(i); 9054b3e95d5SJeremy L Thompson 90659fa3f92SJeremy L Thompson CeedCallBackend(CeedOperatorFieldGetName(op_output_fields[i], &field_name)); 90759fa3f92SJeremy L Thompson code << " // ---- Output field " << i << ": " << field_name << "\n"; 9084b3e95d5SJeremy L Thompson CeedCallBackend(CeedQFunctionFieldGetEvalMode(qf_output_fields[i], &eval_mode)); 9094b3e95d5SJeremy L Thompson // Basis action 9104b3e95d5SJeremy L Thompson switch (eval_mode) { 9114b3e95d5SJeremy L Thompson case CEED_EVAL_NONE: 9124b3e95d5SJeremy L Thompson code << " CeedScalar r_s" << var_suffix << "[num_comp" << var_suffix << "];\n"; 9133a2968d6SJeremy L Thompson break; 9144b3e95d5SJeremy L Thompson case CEED_EVAL_INTERP: 9154b3e95d5SJeremy L Thompson code << " CeedScalar r_s" << var_suffix << "[num_comp" << var_suffix << "];\n"; 9164b3e95d5SJeremy L Thompson break; 9174b3e95d5SJeremy L Thompson case CEED_EVAL_GRAD: 91874398b5aSJeremy L Thompson code << " CeedScalar r_s" << var_suffix << "[num_comp" << var_suffix << "*dim" << var_suffix << "];\n"; 9194b3e95d5SJeremy L Thompson break; 9204b3e95d5SJeremy L Thompson // LCOV_EXCL_START 9214b3e95d5SJeremy L Thompson case CEED_EVAL_WEIGHT: 9224b3e95d5SJeremy L Thompson break; // Should not occur 9234b3e95d5SJeremy L Thompson case CEED_EVAL_DIV: 9244b3e95d5SJeremy L Thompson case CEED_EVAL_CURL: 9254b3e95d5SJeremy L Thompson break; // TODO: Not implemented 9264b3e95d5SJeremy L Thompson // LCOV_EXCL_STOP 9274b3e95d5SJeremy L Thompson } 9284b3e95d5SJeremy L Thompson } 9294b3e95d5SJeremy L Thompson } else { 9304b3e95d5SJeremy L Thompson code << "\n // Note: Using full elements\n"; 9314b3e95d5SJeremy L Thompson code << " {\n"; 9324b3e95d5SJeremy L Thompson code << " // -- Input fields\n"; 9334b3e95d5SJeremy L Thompson for (CeedInt i = 0; i < num_input_fields; i++) { 93459fa3f92SJeremy L Thompson const char *field_name; 93559fa3f92SJeremy L Thompson 93659fa3f92SJeremy L Thompson CeedCallBackend(CeedOperatorFieldGetName(op_input_fields[i], &field_name)); 93759fa3f92SJeremy L Thompson code << " // ---- Input field " << i << ": " << field_name << "\n"; 9384b3e95d5SJeremy L Thompson code << " CeedScalar *r_s_in_" << i << " = r_q_in_" << i << ";\n"; 9394b3e95d5SJeremy L Thompson } 9404b3e95d5SJeremy L Thompson code << " // -- Output fields\n"; 9414b3e95d5SJeremy L Thompson for (CeedInt i = 0; i < num_output_fields; i++) { 94259fa3f92SJeremy L Thompson const char *field_name; 94359fa3f92SJeremy L Thompson 94459fa3f92SJeremy L Thompson CeedCallBackend(CeedOperatorFieldGetName(op_output_fields[i], &field_name)); 94559fa3f92SJeremy L Thompson code << " // ---- Output field " << i << ": " << field_name << "\n"; 9464b3e95d5SJeremy L Thompson code << " CeedScalar *r_s_out_" << i << " = r_q_out_" << i << ";\n"; 9474b3e95d5SJeremy L Thompson } 9484b3e95d5SJeremy L Thompson } 9494b3e95d5SJeremy L Thompson 9504b3e95d5SJeremy L Thompson // Input and output buffers 9514b3e95d5SJeremy L Thompson code << "\n // -- QFunction inputs and outputs\n"; 9524b3e95d5SJeremy L Thompson code << " // ---- Inputs\n"; 9534b3e95d5SJeremy L Thompson code << " CeedScalar *inputs[" << CeedIntMax(num_input_fields, 1) << "];\n"; 9544b3e95d5SJeremy L Thompson for (CeedInt i = 0; i < num_input_fields; i++) { 95559fa3f92SJeremy L Thompson const char *field_name; 95659fa3f92SJeremy L Thompson 95759fa3f92SJeremy L Thompson CeedCallBackend(CeedOperatorFieldGetName(op_input_fields[i], &field_name)); 95859fa3f92SJeremy L Thompson code << " // ------ Input field " << i << ": " << field_name << "\n"; 9594b3e95d5SJeremy L Thompson code << " inputs[" << i << "] = r_s_in_" << i << ";\n"; 9604b3e95d5SJeremy L Thompson } 9614b3e95d5SJeremy L Thompson code << " // ---- Outputs\n"; 9624b3e95d5SJeremy L Thompson code << " CeedScalar *outputs[" << CeedIntMax(num_output_fields, 1) << "];\n"; 9634b3e95d5SJeremy L Thompson for (CeedInt i = 0; i < num_output_fields; i++) { 96459fa3f92SJeremy L Thompson const char *field_name; 96559fa3f92SJeremy L Thompson 96659fa3f92SJeremy L Thompson CeedCallBackend(CeedOperatorFieldGetName(op_output_fields[i], &field_name)); 96759fa3f92SJeremy L Thompson code << " // ------ Output field " << i << ": " << field_name << "\n"; 9684b3e95d5SJeremy L Thompson code << " outputs[" << i << "] = r_s_out_" << i << ";\n"; 9694b3e95d5SJeremy L Thompson } 9704b3e95d5SJeremy L Thompson 9714b3e95d5SJeremy L Thompson // Apply QFunction 9724b3e95d5SJeremy L Thompson code << "\n // -- Apply QFunction\n"; 9734b3e95d5SJeremy L Thompson code << " " << qfunction_name << "(ctx, "; 97474398b5aSJeremy L Thompson if (max_dim != 3 || is_at_points || use_3d_slices || !is_all_tensor) { 9754b3e95d5SJeremy L Thompson code << "1"; 9764b3e95d5SJeremy L Thompson } else { 9779123fb08SJeremy L Thompson code << Q_name; 9784b3e95d5SJeremy L Thompson } 9794b3e95d5SJeremy L Thompson code << ", inputs, outputs);\n"; 9804b3e95d5SJeremy L Thompson 9813a2968d6SJeremy L Thompson if (is_at_points) { 9823a2968d6SJeremy L Thompson // Map back to coefficients 9833a2968d6SJeremy L Thompson code << "\n // -- Output fields\n"; 9843a2968d6SJeremy L Thompson for (CeedInt i = 0; i < num_output_fields; i++) { 98559fa3f92SJeremy L Thompson const char *field_name; 9863a2968d6SJeremy L Thompson std::string var_suffix = "_out_" + std::to_string(i); 9873a2968d6SJeremy L Thompson std::string P_name = "P_1d" + var_suffix; 9883a2968d6SJeremy L Thompson 98959fa3f92SJeremy L Thompson CeedCallBackend(CeedOperatorFieldGetName(op_output_fields[i], &field_name)); 99059fa3f92SJeremy L Thompson code << " // ---- Output field " << i << ": " << field_name << "\n"; 9913a2968d6SJeremy L Thompson CeedCallBackend(CeedQFunctionFieldGetEvalMode(qf_output_fields[i], &eval_mode)); 9923a2968d6SJeremy L Thompson // Basis action 9933a2968d6SJeremy L Thompson code << " // EvalMode: " << CeedEvalModes[eval_mode] << "\n"; 9943a2968d6SJeremy L Thompson switch (eval_mode) { 9953a2968d6SJeremy L Thompson case CEED_EVAL_NONE: { 9963a2968d6SJeremy L Thompson CeedInt comp_stride; 9973a2968d6SJeremy L Thompson CeedElemRestriction elem_rstr; 9983a2968d6SJeremy L Thompson 9993a2968d6SJeremy L Thompson CeedCallBackend(CeedOperatorFieldGetElemRestriction(op_output_fields[i], &elem_rstr)); 10003a2968d6SJeremy L Thompson CeedCallBackend(CeedElemRestrictionGetCompStride(elem_rstr, &comp_stride)); 10013a2968d6SJeremy L Thompson CeedCallBackend(CeedElemRestrictionDestroy(&elem_rstr)); 10023a2968d6SJeremy L Thompson code << " const CeedInt comp_stride" << var_suffix << " = " << comp_stride << ";\n"; 10033a2968d6SJeremy L Thompson code << " WritePoint<num_comp" << var_suffix << ", comp_stride" << var_suffix 10043a2968d6SJeremy L Thompson << ", max_num_points>(data, elem, i, points.num_per_elem[elem], indices.outputs[" << i << "]" 10053a2968d6SJeremy L Thompson << ", r_s" << var_suffix << ", d" << var_suffix << ");\n"; 10063a2968d6SJeremy L Thompson break; 10073a2968d6SJeremy L Thompson } 10083a2968d6SJeremy L Thompson case CEED_EVAL_INTERP: 10093a2968d6SJeremy L Thompson code << " if (i >= points.num_per_elem[elem]) {\n"; 10103a2968d6SJeremy L Thompson code << " for (CeedInt j = 0; j < num_comp" << var_suffix << "; j++) r_s" << var_suffix << "[j] = 0.0;\n"; 10113a2968d6SJeremy L Thompson code << " }\n"; 101274398b5aSJeremy L Thompson code << " InterpTransposeAtPoints" << max_dim << "d<num_comp" << var_suffix << ", max_num_points, " << P_name << ", " << Q_name 1013f725b54bSJeremy L Thompson << ">(data, i, r_s" << var_suffix << ", r_x, r_c" << var_suffix << ");\n"; 10143a2968d6SJeremy L Thompson break; 10153a2968d6SJeremy L Thompson case CEED_EVAL_GRAD: 10163a2968d6SJeremy L Thompson code << " if (i >= points.num_per_elem[elem]) {\n"; 101774398b5aSJeremy L Thompson code << " for (CeedInt j = 0; j < num_comp" << var_suffix << "*dim" << var_suffix << "; j++) r_s" << var_suffix << "[j] = 0.0;\n"; 10183a2968d6SJeremy L Thompson code << " }\n"; 101974398b5aSJeremy L Thompson code << " GradTransposeAtPoints" << max_dim << "d<num_comp" << var_suffix << ", max_num_points, " << P_name << ", " << Q_name 1020f725b54bSJeremy L Thompson << ">(data, i, r_s" << var_suffix << ", r_x, r_c" << var_suffix << ");\n"; 10213a2968d6SJeremy L Thompson break; 10223a2968d6SJeremy L Thompson // LCOV_EXCL_START 10233a2968d6SJeremy L Thompson case CEED_EVAL_WEIGHT: 10243a2968d6SJeremy L Thompson break; // Should not occur 10253a2968d6SJeremy L Thompson case CEED_EVAL_DIV: 10263a2968d6SJeremy L Thompson case CEED_EVAL_CURL: 10273a2968d6SJeremy L Thompson break; // TODO: Not implemented 10283a2968d6SJeremy L Thompson // LCOV_EXCL_STOP 10293a2968d6SJeremy L Thompson } 10303a2968d6SJeremy L Thompson } 10313a2968d6SJeremy L Thompson } else if (use_3d_slices) { 10324b3e95d5SJeremy L Thompson // Copy or apply transpose grad, if needed 10333a2968d6SJeremy L Thompson code << "\n // -- Output fields\n"; 10344b3e95d5SJeremy L Thompson for (CeedInt i = 0; i < num_output_fields; i++) { 103559fa3f92SJeremy L Thompson const char *field_name; 10364b3e95d5SJeremy L Thompson std::string var_suffix = "_out_" + std::to_string(i); 10374b3e95d5SJeremy L Thompson std::string P_name = "P_1d" + var_suffix; 10384b3e95d5SJeremy L Thompson 103959fa3f92SJeremy L Thompson CeedCallBackend(CeedOperatorFieldGetName(op_output_fields[i], &field_name)); 104059fa3f92SJeremy L Thompson code << " // ---- Output field " << i << ": " << field_name << "\n"; 10414b3e95d5SJeremy L Thompson CeedCallBackend(CeedQFunctionFieldGetEvalMode(qf_output_fields[i], &eval_mode)); 10424b3e95d5SJeremy L Thompson // Basis action 10434b3e95d5SJeremy L Thompson code << " // EvalMode: " << CeedEvalModes[eval_mode] << "\n"; 10444b3e95d5SJeremy L Thompson switch (eval_mode) { 10454b3e95d5SJeremy L Thompson case CEED_EVAL_NONE: 10464b3e95d5SJeremy L Thompson code << " for (CeedInt j = 0; j < num_comp" << var_suffix << " ; j++) {\n"; 10474b3e95d5SJeremy L Thompson code << " r_q" << var_suffix << "[q + j*" << Q_name << "] = r_s" << var_suffix << "[j];\n"; 10484b3e95d5SJeremy L Thompson code << " }\n"; 10493a2968d6SJeremy L Thompson break; 10504b3e95d5SJeremy L Thompson case CEED_EVAL_INTERP: 10514b3e95d5SJeremy L Thompson code << " for (CeedInt j = 0; j < num_comp" << var_suffix << " ; j++) {\n"; 10524b3e95d5SJeremy L Thompson code << " r_q" << var_suffix << "[q + j*" << Q_name << "] = r_s" << var_suffix << "[j];\n"; 10534b3e95d5SJeremy L Thompson code << " }\n"; 10544b3e95d5SJeremy L Thompson break; 10554b3e95d5SJeremy L Thompson case CEED_EVAL_GRAD: 10566b92dc4bSJeremy L Thompson code << " GradColloSliceTranspose3d<num_comp" << var_suffix << ", " << Q_name << ", OP_T_1D>(data, q, r_s" << var_suffix << ", s_G" 1057f815fac9SJeremy L Thompson << var_suffix << ", r_q" << var_suffix << ");\n"; 10584b3e95d5SJeremy L Thompson break; 10594b3e95d5SJeremy L Thompson // LCOV_EXCL_START 10604b3e95d5SJeremy L Thompson case CEED_EVAL_WEIGHT: 10614b3e95d5SJeremy L Thompson break; // Should not occur 10624b3e95d5SJeremy L Thompson case CEED_EVAL_DIV: 10634b3e95d5SJeremy L Thompson case CEED_EVAL_CURL: 10644b3e95d5SJeremy L Thompson break; // TODO: Not implemented 10654b3e95d5SJeremy L Thompson // LCOV_EXCL_STOP 10664b3e95d5SJeremy L Thompson } 10674b3e95d5SJeremy L Thompson } 10684b3e95d5SJeremy L Thompson } 10694b3e95d5SJeremy L Thompson code << " }\n"; 10704b3e95d5SJeremy L Thompson return CEED_ERROR_SUCCESS; 10714b3e95d5SJeremy L Thompson } 10724b3e95d5SJeremy L Thompson 10734b3e95d5SJeremy L Thompson //------------------------------------------------------------------------------ 10749e201c85SYohann // Build single operator kernel 10757d8d0e25Snbeams //------------------------------------------------------------------------------ 10768d12f40eSJeremy L Thompson extern "C" int CeedOperatorBuildKernel_Hip_gen(CeedOperator op, bool *is_good_build) { 107774398b5aSJeremy L Thompson bool is_all_tensor = true, is_all_nontensor = true, is_at_points = false, use_3d_slices = false; 10787d8d0e25Snbeams Ceed ceed; 1079efa41df3SJeremy 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; 1080b7453713SJeremy L Thompson CeedQFunctionField *qf_input_fields, *qf_output_fields; 1081b7453713SJeremy L Thompson CeedQFunction_Hip_gen *qf_data; 1082b7453713SJeremy L Thompson CeedQFunction qf; 1083b7453713SJeremy L Thompson CeedOperatorField *op_input_fields, *op_output_fields; 1084b7453713SJeremy L Thompson CeedOperator_Hip_gen *data; 10854b3e95d5SJeremy L Thompson std::ostringstream code; 10864b3e95d5SJeremy L Thompson 10878d12f40eSJeremy L Thompson CeedCallBackend(CeedOperatorGetData(op, &data)); 10884b3e95d5SJeremy L Thompson { 10894b3e95d5SJeremy L Thompson bool is_setup_done; 1090b7453713SJeremy L Thompson 1091b7453713SJeremy L Thompson CeedCallBackend(CeedOperatorIsSetupDone(op, &is_setup_done)); 10928d12f40eSJeremy L Thompson if (is_setup_done) { 10938d12f40eSJeremy L Thompson *is_good_build = !data->use_fallback; 10948d12f40eSJeremy L Thompson return CEED_ERROR_SUCCESS; 10958d12f40eSJeremy L Thompson } 10964b3e95d5SJeremy L Thompson } 1097b7453713SJeremy L Thompson 10988d12f40eSJeremy L Thompson // Check field compatibility 10998d12f40eSJeremy L Thompson CeedCallBackend(CeedOperatorGetFields(op, &num_input_fields, &op_input_fields, &num_output_fields, &op_output_fields)); 11008d12f40eSJeremy L Thompson { 110174398b5aSJeremy L Thompson bool has_shared_bases = true; 11028d12f40eSJeremy L Thompson 11038d12f40eSJeremy L Thompson for (CeedInt i = 0; i < num_input_fields; i++) { 11048d12f40eSJeremy L Thompson CeedBasis basis; 11058d12f40eSJeremy L Thompson 11068d12f40eSJeremy L Thompson CeedCallBackend(CeedOperatorFieldGetBasis(op_input_fields[i], &basis)); 11078d12f40eSJeremy L Thompson if (basis != CEED_BASIS_NONE) { 11088d12f40eSJeremy L Thompson bool is_tensor = true; 11098d12f40eSJeremy L Thompson const char *resource; 11108d12f40eSJeremy L Thompson char *resource_root; 11118d12f40eSJeremy L Thompson Ceed basis_ceed; 11128d12f40eSJeremy L Thompson 11138d12f40eSJeremy L Thompson CeedCallBackend(CeedBasisIsTensor(basis, &is_tensor)); 1114c9192acaSJeremy L Thompson is_all_tensor = is_all_tensor && is_tensor; 1115c9192acaSJeremy L Thompson is_all_nontensor = is_all_nontensor && !is_tensor; 11168d12f40eSJeremy L Thompson CeedCallBackend(CeedBasisGetCeed(basis, &basis_ceed)); 11178d12f40eSJeremy L Thompson CeedCallBackend(CeedGetResource(basis_ceed, &resource)); 11188d12f40eSJeremy L Thompson CeedCallBackend(CeedGetResourceRoot(basis_ceed, resource, ":", &resource_root)); 1119c9192acaSJeremy L Thompson has_shared_bases = has_shared_bases && !strcmp(resource_root, "/gpu/hip/shared"); 11208d12f40eSJeremy L Thompson CeedCallBackend(CeedFree(&resource_root)); 11218d12f40eSJeremy L Thompson CeedCallBackend(CeedDestroy(&basis_ceed)); 11228d12f40eSJeremy L Thompson } 11238d12f40eSJeremy L Thompson CeedCallBackend(CeedBasisDestroy(&basis)); 11248d12f40eSJeremy L Thompson } 11258d12f40eSJeremy L Thompson 11268d12f40eSJeremy L Thompson for (CeedInt i = 0; i < num_output_fields; i++) { 11278d12f40eSJeremy L Thompson CeedBasis basis; 11288d12f40eSJeremy L Thompson 11298d12f40eSJeremy L Thompson CeedCallBackend(CeedOperatorFieldGetBasis(op_output_fields[i], &basis)); 11308d12f40eSJeremy L Thompson if (basis != CEED_BASIS_NONE) { 11318d12f40eSJeremy L Thompson bool is_tensor = true; 11328d12f40eSJeremy L Thompson const char *resource; 11338d12f40eSJeremy L Thompson char *resource_root; 11348d12f40eSJeremy L Thompson Ceed basis_ceed; 11358d12f40eSJeremy L Thompson 11368d12f40eSJeremy L Thompson CeedCallBackend(CeedBasisIsTensor(basis, &is_tensor)); 1137c9192acaSJeremy L Thompson is_all_tensor = is_all_tensor && is_tensor; 1138c9192acaSJeremy L Thompson is_all_nontensor = is_all_nontensor && !is_tensor; 11398d12f40eSJeremy L Thompson 11408d12f40eSJeremy L Thompson CeedCallBackend(CeedBasisGetCeed(basis, &basis_ceed)); 11418d12f40eSJeremy L Thompson CeedCallBackend(CeedGetResource(basis_ceed, &resource)); 11428d12f40eSJeremy L Thompson CeedCallBackend(CeedGetResourceRoot(basis_ceed, resource, ":", &resource_root)); 1143c9192acaSJeremy L Thompson has_shared_bases = has_shared_bases && !strcmp(resource_root, "/gpu/hip/shared"); 11448d12f40eSJeremy L Thompson CeedCallBackend(CeedFree(&resource_root)); 11458d12f40eSJeremy L Thompson CeedCallBackend(CeedDestroy(&basis_ceed)); 11468d12f40eSJeremy L Thompson } 11478d12f40eSJeremy L Thompson CeedCallBackend(CeedBasisDestroy(&basis)); 11488d12f40eSJeremy L Thompson } 11498d12f40eSJeremy L Thompson // -- Fallback to ref if not all bases are shared 115074398b5aSJeremy L Thompson if (!has_shared_bases) { 11518d12f40eSJeremy L Thompson *is_good_build = false; 11528d12f40eSJeremy L Thompson return CEED_ERROR_SUCCESS; 11538d12f40eSJeremy L Thompson } 11548d12f40eSJeremy L Thompson } 1155b7453713SJeremy L Thompson CeedCallBackend(CeedOperatorGetCeed(op, &ceed)); 1156b7453713SJeremy L Thompson CeedCallBackend(CeedOperatorGetQFunction(op, &qf)); 1157b7453713SJeremy L Thompson CeedCallBackend(CeedQFunctionGetData(qf, &qf_data)); 1158b7453713SJeremy L Thompson CeedCallBackend(CeedQFunctionGetFields(qf, NULL, &qf_input_fields, NULL, &qf_output_fields)); 11597d8d0e25Snbeams 11604b3e95d5SJeremy L Thompson // Get operator data 11613a2968d6SJeremy L Thompson CeedCallBackend(CeedOperatorIsAtPoints(op, &is_at_points)); 116274398b5aSJeremy L Thompson { 1163efa41df3SJeremy L Thompson CeedInt max_P = 0, max_P_1d = 0; 116474398b5aSJeremy L Thompson 11654b3e95d5SJeremy L Thompson CeedCallBackend(CeedOperatorBuildKernelData_Hip_gen(ceed, num_input_fields, op_input_fields, qf_input_fields, num_output_fields, op_output_fields, 116674398b5aSJeremy L Thompson qf_output_fields, &max_P, &max_P_1d, &Q, &Q_1d, &max_dim, &is_all_tensor, &use_3d_slices)); 116774398b5aSJeremy L Thompson data->max_P_1d = is_all_tensor ? max_P_1d : max_P; 116874398b5aSJeremy L Thompson } 116974398b5aSJeremy L Thompson if (max_dim == 0) max_dim = 1; 117074398b5aSJeremy L Thompson data->dim = max_dim; 11713a2968d6SJeremy L Thompson if (is_at_points) { 11723a2968d6SJeremy L Thompson CeedElemRestriction_Hip *rstr_data; 11733a2968d6SJeremy L Thompson CeedElemRestriction rstr_points = NULL; 11744b3e95d5SJeremy L Thompson 11753a2968d6SJeremy L Thompson CeedCallBackend(CeedOperatorAtPointsGetPoints(op, &rstr_points, NULL)); 11763a2968d6SJeremy L Thompson CeedCallBackend(CeedElemRestrictionGetMaxPointsInElement(rstr_points, &max_num_points)); 11773a2968d6SJeremy L Thompson CeedCallBackend(CeedElemRestrictionGetCompStride(rstr_points, &coords_comp_stride)); 11783a2968d6SJeremy L Thompson CeedCallBackend(CeedElemRestrictionGetData(rstr_points, &rstr_data)); 11793a2968d6SJeremy L Thompson data->points.indices = (CeedInt *)rstr_data->d_offsets; 11803a2968d6SJeremy L Thompson CeedCallBackend(CeedElemRestrictionDestroy(&rstr_points)); 11813a2968d6SJeremy L Thompson } 11823a2968d6SJeremy L Thompson if (is_at_points) use_3d_slices = false; 11833a2968d6SJeremy L Thompson if (Q_1d == 0) { 11843a2968d6SJeremy L Thompson if (is_at_points) Q_1d = max_num_points; 11853a2968d6SJeremy L Thompson else CeedCallBackend(CeedOperatorGetNumQuadraturePoints(op, &Q_1d)); 11864b3e95d5SJeremy L Thompson } 118774398b5aSJeremy L Thompson if (Q == 0) Q = Q_1d; 118874398b5aSJeremy L Thompson data->Q = Q; 11894b3e95d5SJeremy L Thompson data->Q_1d = Q_1d; 11904b3e95d5SJeremy L Thompson 11910b454692Sjeremylt // Check for restriction only identity operator 11924b3e95d5SJeremy L Thompson { 11934b3e95d5SJeremy L Thompson bool is_identity_qf; 11944b3e95d5SJeremy L Thompson 11952b730f8bSJeremy L Thompson CeedCallBackend(CeedQFunctionIsIdentity(qf, &is_identity_qf)); 11960b454692Sjeremylt if (is_identity_qf) { 11979e201c85SYohann CeedEvalMode eval_mode_in, eval_mode_out; 1198b7453713SJeremy L Thompson 11992b730f8bSJeremy L Thompson CeedCallBackend(CeedQFunctionFieldGetEvalMode(qf_input_fields[0], &eval_mode_in)); 12002b730f8bSJeremy L Thompson CeedCallBackend(CeedQFunctionFieldGetEvalMode(qf_output_fields[0], &eval_mode_out)); 12016574a04fSJeremy L Thompson CeedCheck(eval_mode_in != CEED_EVAL_NONE || eval_mode_out != CEED_EVAL_NONE, ceed, CEED_ERROR_BACKEND, 12026574a04fSJeremy L Thompson "Backend does not implement restriction only identity operators"); 12030b454692Sjeremylt } 12044b3e95d5SJeremy L Thompson } 1205b2165e7aSSebastian Grimberg 1206b2165e7aSSebastian Grimberg // Load basis source files 1207*eaf9ad10SZach Atkins if (!is_all_nontensor) { 12089c25dd66SJeremy L Thompson code << "// Tensor basis source\n"; 12099c25dd66SJeremy L Thompson code << "#include <ceed/jit-source/hip/hip-shared-basis-tensor-templates.h>\n\n"; 121074398b5aSJeremy L Thompson } 121174398b5aSJeremy L Thompson if (!is_all_tensor) { 12129123fb08SJeremy L Thompson code << "// Non-tensor basis source\n"; 12139123fb08SJeremy L Thompson code << "#include <ceed/jit-source/hip/hip-shared-basis-nontensor-templates.h>\n\n"; 12149123fb08SJeremy L Thompson } 12159123fb08SJeremy L Thompson if (is_at_points) { 12163a2968d6SJeremy L Thompson code << "// AtPoints basis source\n"; 12173a2968d6SJeremy L Thompson code << "#include <ceed/jit-source/hip/hip-shared-basis-tensor-at-points-templates.h>\n\n"; 12189123fb08SJeremy L Thompson } 121974398b5aSJeremy L Thompson if (!is_all_tensor && !is_all_nontensor) { 122074398b5aSJeremy L Thompson code << "// Tensor basis source\n"; 122174398b5aSJeremy L Thompson code << "#include <ceed/jit-source/hip/hip-shared-basis-tensor-flattened-templates.h>\n\n"; 122274398b5aSJeremy L Thompson } 12239c25dd66SJeremy L Thompson code << "// CodeGen operator source\n"; 12249c25dd66SJeremy L Thompson code << "#include <ceed/jit-source/hip/hip-gen-templates.h>\n\n"; 12257d8d0e25Snbeams 12264b3e95d5SJeremy L Thompson // Get QFunction name 12274b3e95d5SJeremy L Thompson std::string qfunction_name(qf_data->qfunction_name); 12284b3e95d5SJeremy L Thompson std::string operator_name; 12294b3e95d5SJeremy L Thompson 123009095acaSJeremy L Thompson operator_name = "CeedKernelHipGenOperator_" + qfunction_name; 12317d8d0e25Snbeams 12329e201c85SYohann // Define CEED_Q_VLA 12339e201c85SYohann code << "\n#undef CEED_Q_VLA\n"; 123474398b5aSJeremy L Thompson if (max_dim != 3 || is_at_points || use_3d_slices || !is_all_tensor) { 12359e201c85SYohann code << "#define CEED_Q_VLA 1\n\n"; 12369e201c85SYohann } else { 12379e201c85SYohann code << "#define CEED_Q_VLA " << Q_1d << "\n\n"; 12389e201c85SYohann } 12399e201c85SYohann 12404b3e95d5SJeremy L Thompson // Add user QFunction source 12414b3e95d5SJeremy L Thompson { 12429c25dd66SJeremy L Thompson const char *source_path; 12434b3e95d5SJeremy L Thompson 12449c25dd66SJeremy L Thompson CeedCallBackend(CeedQFunctionGetSourcePath(qf, &source_path)); 12459c25dd66SJeremy L Thompson CeedCheck(source_path, ceed, CEED_ERROR_UNSUPPORTED, "/gpu/hip/gen backend requires QFunction source code file"); 12469c25dd66SJeremy L Thompson 12479c25dd66SJeremy L Thompson code << "// User QFunction source\n"; 12489c25dd66SJeremy L Thompson code << "#include \"" << source_path << "\"\n\n"; 12494b3e95d5SJeremy L Thompson } 12507d8d0e25Snbeams 12517d8d0e25Snbeams // Setup 12527d8d0e25Snbeams code << "\n// -----------------------------------------------------------------------------\n"; 12534b3e95d5SJeremy L Thompson code << "// Operator Kernel\n"; 12544b3e95d5SJeremy L Thompson code << "// \n"; 12554b3e95d5SJeremy L Thompson code << "// d_[in,out]_i: CeedVector device array\n"; 12564b3e95d5SJeremy L Thompson code << "// r_[in,out]_e_i: Element vector register\n"; 12574b3e95d5SJeremy L Thompson code << "// r_[in,out]_q_i: Quadrature space vector register\n"; 12589123fb08SJeremy L Thompson code << "// r_[in,out]_c_i: AtPoints Chebyshev coefficients register\n"; 12594b3e95d5SJeremy L Thompson code << "// r_[in,out]_s_i: Quadrature space slice vector register\n"; 12604b3e95d5SJeremy L Thompson code << "// \n"; 12614b3e95d5SJeremy L Thompson code << "// s_B_[in,out]_i: Interpolation matrix, shared memory\n"; 12624b3e95d5SJeremy L Thompson code << "// s_G_[in,out]_i: Gradient matrix, shared memory\n"; 12634b3e95d5SJeremy L Thompson code << "// -----------------------------------------------------------------------------\n"; 1264b3e1519bSnbeams code << "\nextern \"C\" __launch_bounds__(BLOCK_SIZE)\n"; 12652b730f8bSJeremy L Thompson code << "__global__ void " << operator_name 12663a2968d6SJeremy 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"; 12674b3e95d5SJeremy L Thompson 12684b3e95d5SJeremy L Thompson // Scratch buffers 12699e201c85SYohann for (CeedInt i = 0; i < num_input_fields; i++) { 12704b3e95d5SJeremy L Thompson CeedEvalMode eval_mode; 12714b3e95d5SJeremy L Thompson 12722b730f8bSJeremy L Thompson CeedCallBackend(CeedQFunctionFieldGetEvalMode(qf_input_fields[i], &eval_mode)); 12739e201c85SYohann if (eval_mode != CEED_EVAL_WEIGHT) { // Skip CEED_EVAL_WEIGHT 1274826538b3SJeremy L Thompson code << " const CeedScalar *__restrict__ d_in_" << i << " = fields.inputs[" << i << "];\n"; 12757d8d0e25Snbeams } 12767d8d0e25Snbeams } 12779e201c85SYohann for (CeedInt i = 0; i < num_output_fields; i++) { 1278826538b3SJeremy L Thompson code << " CeedScalar *__restrict__ d_out_" << i << " = fields.outputs[" << i << "];\n"; 12797d8d0e25Snbeams } 12807d8d0e25Snbeams 128174398b5aSJeremy L Thompson code << " const CeedInt max_dim = " << max_dim << ";\n"; 128274398b5aSJeremy L Thompson if (!is_all_tensor) { 128374398b5aSJeremy L Thompson code << " const CeedInt Q = " << Q << ";\n"; 128474398b5aSJeremy L Thompson } 128574398b5aSJeremy L Thompson if (!is_all_nontensor) { 128674398b5aSJeremy L Thompson code << " const CeedInt Q_1d = " << Q_1d << ";\n"; 128774398b5aSJeremy L Thompson } 12883a2968d6SJeremy L Thompson if (is_at_points) { 12893a2968d6SJeremy L Thompson code << " const CeedInt max_num_points = " << max_num_points << ";\n"; 12903a2968d6SJeremy L Thompson code << " const CeedInt coords_comp_stride = " << coords_comp_stride << ";\n"; 12913a2968d6SJeremy L Thompson } 12927d8d0e25Snbeams 12934b3e95d5SJeremy L Thompson // Shared data 12944b3e95d5SJeremy L Thompson code << " extern __shared__ CeedScalar slice[];\n"; 12959e201c85SYohann code << " SharedData_Hip data;\n"; 12969e201c85SYohann code << " data.t_id_x = threadIdx.x;\n"; 12979e201c85SYohann code << " data.t_id_y = threadIdx.y;\n"; 12989e201c85SYohann code << " data.t_id_z = threadIdx.z;\n"; 12999e201c85SYohann code << " data.t_id = threadIdx.x + threadIdx.y*blockDim.x + threadIdx.z*blockDim.y*blockDim.x;\n"; 130074398b5aSJeremy L Thompson code << " data.slice = slice + data.t_id_z*OP_T_1D" << ((!is_all_tensor || max_dim == 1) ? "" : "*OP_T_1D") << ";\n"; 13017d8d0e25Snbeams 13029ee499e5SJeremy L Thompson // -- Determine input mat reuse 130345a787f7SJeremy L Thompson FieldReuse_Hip input_matrix_reuse[CEED_FIELD_MAX]; 13049ee499e5SJeremy L Thompson 13059ee499e5SJeremy L Thompson for (CeedInt i = 0; i < num_input_fields; i++) { 130645a787f7SJeremy L Thompson input_matrix_reuse[i].index = -1; 13079ee499e5SJeremy L Thompson } 13089ee499e5SJeremy L Thompson for (CeedInt i = 0; i < num_input_fields; i++) { 130974398b5aSJeremy L Thompson bool is_tensor = true; 13109ee499e5SJeremy L Thompson CeedEvalMode eval_mode_i; 13119ee499e5SJeremy L Thompson CeedBasis basis_i; 13129ee499e5SJeremy L Thompson 13139ee499e5SJeremy L Thompson CeedCallBackend(CeedQFunctionFieldGetEvalMode(qf_input_fields[i], &eval_mode_i)); 13149ee499e5SJeremy L Thompson if (eval_mode_i == CEED_EVAL_WEIGHT) continue; 13159ee499e5SJeremy L Thompson CeedCallBackend(CeedOperatorFieldGetBasis(op_input_fields[i], &basis_i)); 131674398b5aSJeremy L Thompson CeedCallBackend(CeedBasisIsTensor(basis_i, &is_tensor)); 131745a787f7SJeremy L Thompson for (CeedInt j = 0; (input_matrix_reuse[i].index == -1) && (j < i); j++) { 13189ee499e5SJeremy L Thompson CeedEvalMode eval_mode_j; 13199ee499e5SJeremy L Thompson CeedBasis basis_j; 13209ee499e5SJeremy L Thompson 13219ee499e5SJeremy L Thompson CeedCallBackend(CeedQFunctionFieldGetEvalMode(qf_input_fields[j], &eval_mode_j)); 13229ee499e5SJeremy L Thompson if (eval_mode_j == CEED_EVAL_WEIGHT) continue; 13239ee499e5SJeremy L Thompson CeedCallBackend(CeedOperatorFieldGetBasis(op_input_fields[j], &basis_j)); 13249ee499e5SJeremy L Thompson if (basis_i == basis_j) { 13259ee499e5SJeremy L Thompson if (is_tensor) { 132645a787f7SJeremy L Thompson input_matrix_reuse[i].index = j; 132745a787f7SJeremy L Thompson input_matrix_reuse[i].is_input = true; 132845a787f7SJeremy L Thompson input_matrix_reuse[i].eval_mode = eval_mode_j; 13299ee499e5SJeremy L Thompson } else { 13309ee499e5SJeremy L Thompson // For non-tensor can only re-use with the same eval mode 13319ee499e5SJeremy L Thompson if (eval_mode_i == eval_mode_j) { 133245a787f7SJeremy L Thompson input_matrix_reuse[i].index = j; 133345a787f7SJeremy L Thompson input_matrix_reuse[i].is_input = true; 133445a787f7SJeremy L Thompson input_matrix_reuse[i].eval_mode = eval_mode_j; 13359ee499e5SJeremy L Thompson } 13369ee499e5SJeremy L Thompson } 13379ee499e5SJeremy L Thompson } 13389ee499e5SJeremy L Thompson CeedCallBackend(CeedBasisDestroy(&basis_j)); 13399ee499e5SJeremy L Thompson } 13409ee499e5SJeremy L Thompson CeedCallBackend(CeedBasisDestroy(&basis_i)); 13419ee499e5SJeremy L Thompson } 13429ee499e5SJeremy L Thompson 13439ee499e5SJeremy L Thompson // -- Determine output mat reuse 134445a787f7SJeremy L Thompson FieldReuse_Hip output_matrix_reuse[CEED_FIELD_MAX]; 13459ee499e5SJeremy L Thompson 13469ee499e5SJeremy L Thompson for (CeedInt i = 0; i < num_output_fields; i++) { 134745a787f7SJeremy L Thompson output_matrix_reuse[i].index = -1; 13489ee499e5SJeremy L Thompson } 13499ee499e5SJeremy L Thompson for (CeedInt i = 0; i < num_output_fields; i++) { 135074398b5aSJeremy L Thompson bool is_tensor = true; 13519ee499e5SJeremy L Thompson CeedEvalMode eval_mode_i; 13529ee499e5SJeremy L Thompson CeedBasis basis_i; 13539ee499e5SJeremy L Thompson 13549ee499e5SJeremy L Thompson CeedCallBackend(CeedQFunctionFieldGetEvalMode(qf_output_fields[i], &eval_mode_i)); 13559ee499e5SJeremy L Thompson CeedCallBackend(CeedOperatorFieldGetBasis(op_output_fields[i], &basis_i)); 135645a787f7SJeremy L Thompson for (CeedInt j = 0; (output_matrix_reuse[i].index == -1) && (j < num_input_fields); j++) { 13579ee499e5SJeremy L Thompson CeedEvalMode eval_mode_j; 13589ee499e5SJeremy L Thompson CeedBasis basis_j; 13599ee499e5SJeremy L Thompson 13609ee499e5SJeremy L Thompson CeedCallBackend(CeedQFunctionFieldGetEvalMode(qf_input_fields[j], &eval_mode_j)); 13619ee499e5SJeremy L Thompson if (eval_mode_j == CEED_EVAL_WEIGHT) continue; 13629ee499e5SJeremy L Thompson CeedCallBackend(CeedOperatorFieldGetBasis(op_input_fields[j], &basis_j)); 13639ee499e5SJeremy L Thompson if (basis_i == basis_j) { 13649ee499e5SJeremy L Thompson if (is_tensor) { 136545a787f7SJeremy L Thompson output_matrix_reuse[i].index = j; 136645a787f7SJeremy L Thompson output_matrix_reuse[i].is_input = true; 136745a787f7SJeremy L Thompson output_matrix_reuse[i].eval_mode = eval_mode_j; 13689ee499e5SJeremy L Thompson } else { 13699ee499e5SJeremy L Thompson // For non-tensor can only re-use with the same eval mode 13709ee499e5SJeremy L Thompson if (eval_mode_i == eval_mode_j) { 137145a787f7SJeremy L Thompson output_matrix_reuse[i].index = j; 137245a787f7SJeremy L Thompson output_matrix_reuse[i].is_input = true; 137345a787f7SJeremy L Thompson output_matrix_reuse[i].eval_mode = eval_mode_j; 13749ee499e5SJeremy L Thompson } 13759ee499e5SJeremy L Thompson } 13769ee499e5SJeremy L Thompson } 13779ee499e5SJeremy L Thompson CeedCallBackend(CeedBasisDestroy(&basis_j)); 13789ee499e5SJeremy L Thompson } 137945a787f7SJeremy L Thompson for (CeedInt j = 0; (output_matrix_reuse[i].index == -1) && (j < i); j++) { 13809ee499e5SJeremy L Thompson CeedEvalMode eval_mode_j; 13819ee499e5SJeremy L Thompson CeedBasis basis_j; 13829ee499e5SJeremy L Thompson 13839ee499e5SJeremy L Thompson CeedCallBackend(CeedQFunctionFieldGetEvalMode(qf_output_fields[j], &eval_mode_j)); 13849ee499e5SJeremy L Thompson if (eval_mode_j == CEED_EVAL_WEIGHT) continue; 13859ee499e5SJeremy L Thompson CeedCallBackend(CeedOperatorFieldGetBasis(op_output_fields[j], &basis_j)); 138674398b5aSJeremy L Thompson CeedCallBackend(CeedBasisIsTensor(basis_i, &is_tensor)); 13879ee499e5SJeremy L Thompson if (basis_i == basis_j) { 13889ee499e5SJeremy L Thompson if (is_tensor) { 138945a787f7SJeremy L Thompson output_matrix_reuse[i].index = j; 139045a787f7SJeremy L Thompson output_matrix_reuse[i].is_input = false; 139145a787f7SJeremy L Thompson output_matrix_reuse[i].eval_mode = eval_mode_j; 13929ee499e5SJeremy L Thompson } else { 13939ee499e5SJeremy L Thompson // For non-tensor can only re-use with the same eval mode 13949ee499e5SJeremy L Thompson if (eval_mode_i == eval_mode_j) { 139545a787f7SJeremy L Thompson output_matrix_reuse[i].index = j; 139645a787f7SJeremy L Thompson output_matrix_reuse[i].is_input = false; 139745a787f7SJeremy L Thompson output_matrix_reuse[i].eval_mode = eval_mode_j; 13989ee499e5SJeremy L Thompson } 13999ee499e5SJeremy L Thompson } 14009ee499e5SJeremy L Thompson } 14019ee499e5SJeremy L Thompson CeedCallBackend(CeedBasisDestroy(&basis_j)); 14029ee499e5SJeremy L Thompson } 14039ee499e5SJeremy L Thompson CeedCallBackend(CeedBasisDestroy(&basis_i)); 14049ee499e5SJeremy L Thompson } 14059ee499e5SJeremy L Thompson 14067d8d0e25Snbeams // Initialize constants, and matrices B and G 14074b3e95d5SJeremy L Thompson code << "\n // Input field constants and basis data\n"; 14089e201c85SYohann for (CeedInt i = 0; i < num_input_fields; i++) { 140974398b5aSJeremy L Thompson CeedCallBackend(CeedOperatorBuildKernelFieldData_Hip_gen(code, data, i, op_input_fields[i], qf_input_fields[i], input_matrix_reuse[i], max_dim, Q, 141074398b5aSJeremy L Thompson Q_1d, true, is_all_tensor, is_at_points, use_3d_slices)); 14117d8d0e25Snbeams } 14124b3e95d5SJeremy L Thompson code << "\n // Output field constants and basis data\n"; 14139e201c85SYohann for (CeedInt i = 0; i < num_output_fields; i++) { 141474398b5aSJeremy L Thompson CeedCallBackend(CeedOperatorBuildKernelFieldData_Hip_gen(code, data, i, op_output_fields[i], qf_output_fields[i], output_matrix_reuse[i], max_dim, 141574398b5aSJeremy L Thompson Q, Q_1d, false, is_all_tensor, is_at_points, use_3d_slices)); 14164b3e95d5SJeremy L Thompson } 14177d8d0e25Snbeams 14184b3e95d5SJeremy L Thompson // Loop over all elements 14194b3e95d5SJeremy L Thompson code << "\n // Element loop\n"; 14207d8d0e25Snbeams code << " __syncthreads();\n"; 14219e201c85SYohann code << " for (CeedInt elem = blockIdx.x*blockDim.z + threadIdx.z; elem < num_elem; elem += gridDim.x*blockDim.z) {\n"; 14224b3e95d5SJeremy L Thompson 1423e93651e5SJeremy L Thompson // -- Compute minimum buffer space needed 14243a2968d6SJeremy L Thompson CeedInt max_rstr_buffer_size = 1; 1425e93651e5SJeremy L Thompson 1426e93651e5SJeremy L Thompson for (CeedInt i = 0; i < num_input_fields; i++) { 14276de40545SJeremy L Thompson CeedEvalMode eval_mode; 14286de40545SJeremy L Thompson 14296de40545SJeremy L Thompson CeedCallBackend(CeedQFunctionFieldGetEvalMode(qf_input_fields[i], &eval_mode)); 14306de40545SJeremy L Thompson if (eval_mode != CEED_EVAL_NONE && eval_mode != CEED_EVAL_WEIGHT) { 1431a61b1c91SJeremy L Thompson CeedInt num_comp; 1432e93651e5SJeremy L Thompson CeedElemRestriction elem_rstr; 1433e93651e5SJeremy L Thompson 1434e93651e5SJeremy L Thompson CeedCallBackend(CeedOperatorFieldGetElemRestriction(op_input_fields[i], &elem_rstr)); 1435e93651e5SJeremy L Thompson CeedCallBackend(CeedElemRestrictionGetNumComponents(elem_rstr, &num_comp)); 1436a61b1c91SJeremy L Thompson max_rstr_buffer_size = CeedIntMax(max_rstr_buffer_size, num_comp * (is_all_tensor && (max_dim >= 3) ? Q_1d : 1)); 1437681d0ea7SJeremy L Thompson CeedCallBackend(CeedElemRestrictionDestroy(&elem_rstr)); 1438e93651e5SJeremy L Thompson } 14396de40545SJeremy L Thompson } 1440e93651e5SJeremy L Thompson for (CeedInt i = 0; i < num_output_fields; i++) { 14416de40545SJeremy L Thompson CeedEvalMode eval_mode; 14426de40545SJeremy L Thompson 14436de40545SJeremy L Thompson CeedCallBackend(CeedQFunctionFieldGetEvalMode(qf_output_fields[i], &eval_mode)); 14446de40545SJeremy L Thompson if (eval_mode != CEED_EVAL_NONE) { 1445a61b1c91SJeremy L Thompson CeedInt num_comp; 1446e93651e5SJeremy L Thompson CeedElemRestriction elem_rstr; 1447e93651e5SJeremy L Thompson 1448e93651e5SJeremy L Thompson CeedCallBackend(CeedOperatorFieldGetElemRestriction(op_output_fields[i], &elem_rstr)); 1449e93651e5SJeremy L Thompson CeedCallBackend(CeedElemRestrictionGetNumComponents(elem_rstr, &num_comp)); 1450a61b1c91SJeremy L Thompson max_rstr_buffer_size = CeedIntMax(max_rstr_buffer_size, num_comp * (is_all_tensor && (max_dim >= 3) ? Q_1d : 1)); 1451681d0ea7SJeremy L Thompson CeedCallBackend(CeedElemRestrictionDestroy(&elem_rstr)); 1452e93651e5SJeremy L Thompson } 14536de40545SJeremy L Thompson } 1454e93651e5SJeremy L Thompson code << " // Scratch restriction buffer space\n"; 1455e93651e5SJeremy L Thompson code << " CeedScalar r_e_scratch[" << max_rstr_buffer_size << "];\n"; 1456e93651e5SJeremy L Thompson 1457e93651e5SJeremy L Thompson // -- Determine best input field processing order 1458e93651e5SJeremy L Thompson CeedInt field_rstr_in_buffer[CEED_FIELD_MAX], input_field_order[CEED_FIELD_MAX]; 1459e93651e5SJeremy L Thompson 1460e93651e5SJeremy L Thompson for (CeedInt i = 0; i < num_input_fields; i++) { 1461e93651e5SJeremy L Thompson field_rstr_in_buffer[i] = -1; 1462e93651e5SJeremy L Thompson input_field_order[i] = -1; 1463e93651e5SJeremy L Thompson } 1464e93651e5SJeremy L Thompson { 1465e93651e5SJeremy L Thompson bool is_ordered[CEED_FIELD_MAX]; 1466e93651e5SJeremy L Thompson CeedInt curr_index = 0; 1467e93651e5SJeremy L Thompson 1468e93651e5SJeremy L Thompson for (CeedInt i = 0; i < num_input_fields; i++) is_ordered[i] = false; 1469e93651e5SJeremy L Thompson for (CeedInt i = 0; i < num_input_fields; i++) { 1470e93651e5SJeremy L Thompson CeedVector vec_i; 1471e93651e5SJeremy L Thompson CeedElemRestriction rstr_i; 1472e93651e5SJeremy L Thompson 1473e93651e5SJeremy L Thompson if (is_ordered[i]) continue; 1474e93651e5SJeremy L Thompson field_rstr_in_buffer[i] = i; 1475e93651e5SJeremy L Thompson is_ordered[i] = true; 1476e93651e5SJeremy L Thompson input_field_order[curr_index] = i; 1477e93651e5SJeremy L Thompson curr_index++; 1478034f99fdSJeremy L Thompson CeedCallBackend(CeedOperatorFieldGetVector(op_input_fields[i], &vec_i)); 1479e93651e5SJeremy L Thompson if (vec_i == CEED_VECTOR_NONE) continue; // CEED_EVAL_WEIGHT 1480e93651e5SJeremy L Thompson CeedCallBackend(CeedOperatorFieldGetElemRestriction(op_input_fields[i], &rstr_i)); 1481e93651e5SJeremy L Thompson for (CeedInt j = i + 1; j < num_input_fields; j++) { 1482e93651e5SJeremy L Thompson CeedVector vec_j; 1483e93651e5SJeremy L Thompson CeedElemRestriction rstr_j; 1484e93651e5SJeremy L Thompson 1485e93651e5SJeremy L Thompson CeedCallBackend(CeedOperatorFieldGetVector(op_input_fields[j], &vec_j)); 1486e93651e5SJeremy L Thompson CeedCallBackend(CeedOperatorFieldGetElemRestriction(op_input_fields[j], &rstr_j)); 1487e93651e5SJeremy L Thompson if (rstr_i == rstr_j && vec_i == vec_j) { 1488e93651e5SJeremy L Thompson field_rstr_in_buffer[j] = i; 1489e93651e5SJeremy L Thompson is_ordered[j] = true; 1490e93651e5SJeremy L Thompson input_field_order[curr_index] = j; 1491e93651e5SJeremy L Thompson curr_index++; 1492e93651e5SJeremy L Thompson } 14933a2968d6SJeremy L Thompson CeedCallBackend(CeedVectorDestroy(&vec_j)); 14943a2968d6SJeremy L Thompson CeedCallBackend(CeedElemRestrictionDestroy(&rstr_j)); 1495e93651e5SJeremy L Thompson } 14963a2968d6SJeremy L Thompson CeedCallBackend(CeedVectorDestroy(&vec_i)); 14973a2968d6SJeremy L Thompson CeedCallBackend(CeedElemRestrictionDestroy(&rstr_i)); 1498e93651e5SJeremy L Thompson } 1499e93651e5SJeremy L Thompson } 1500e93651e5SJeremy L Thompson 15014b3e95d5SJeremy L Thompson // -- Input restriction and basis 15023a2968d6SJeremy L Thompson code << "\n // -- Input field restrictions and basis actions\n"; 15039e201c85SYohann for (CeedInt i = 0; i < num_input_fields; i++) { 150459fa3f92SJeremy L Thompson const char *field_name; 150559fa3f92SJeremy L Thompson const CeedInt f = input_field_order[i]; 1506e93651e5SJeremy L Thompson 150759fa3f92SJeremy L Thompson CeedCallBackend(CeedOperatorFieldGetName(op_input_fields[f], &field_name)); 150859fa3f92SJeremy L Thompson code << " // ---- Input field " << f << ": " << field_name << "\n"; 15097d8d0e25Snbeams 15104b3e95d5SJeremy L Thompson // ---- Restriction 151174398b5aSJeremy L Thompson CeedCallBackend(CeedOperatorBuildKernelRestriction_Hip_gen(code, data, f, field_rstr_in_buffer, op_input_fields[f], qf_input_fields[f], max_dim, 151274398b5aSJeremy L Thompson Q_1d, true, is_all_tensor, is_at_points, use_3d_slices)); 1513b7453713SJeremy L Thompson 15144b3e95d5SJeremy L Thompson // ---- Basis action 151574398b5aSJeremy L Thompson CeedCallBackend(CeedOperatorBuildKernelBasis_Hip_gen(code, data, f, op_input_fields[f], qf_input_fields[f], max_dim, Q_1d, true, is_all_tensor, 15169123fb08SJeremy L Thompson is_at_points, use_3d_slices)); 15177d8d0e25Snbeams } 15187d8d0e25Snbeams 15194b3e95d5SJeremy L Thompson // -- Q function 152074398b5aSJeremy L Thompson CeedCallBackend(CeedOperatorBuildKernelQFunction_Hip_gen(code, data, max_dim, max_num_points, num_input_fields, op_input_fields, qf_input_fields, 152174398b5aSJeremy L Thompson num_output_fields, op_output_fields, qf_output_fields, qfunction_name, Q_1d, is_all_tensor, 15229123fb08SJeremy L Thompson is_at_points, use_3d_slices)); 15237d8d0e25Snbeams 15244b3e95d5SJeremy L Thompson // -- Output basis and restriction 15254b3e95d5SJeremy L Thompson code << "\n // -- Output field basis action and restrictions\n"; 15269e201c85SYohann for (CeedInt i = 0; i < num_output_fields; i++) { 152759fa3f92SJeremy L Thompson const char *field_name; 152859fa3f92SJeremy L Thompson 152959fa3f92SJeremy L Thompson CeedCallBackend(CeedOperatorFieldGetName(op_output_fields[i], &field_name)); 153059fa3f92SJeremy L Thompson code << " // ---- Output field " << i << ": " << field_name << "\n"; 1531b7453713SJeremy L Thompson 15324b3e95d5SJeremy L Thompson // ---- Basis action 153374398b5aSJeremy L Thompson CeedCallBackend(CeedOperatorBuildKernelBasis_Hip_gen(code, data, i, op_output_fields[i], qf_output_fields[i], max_dim, Q_1d, false, is_all_tensor, 15349123fb08SJeremy L Thompson is_at_points, use_3d_slices)); 15357d8d0e25Snbeams 15364b3e95d5SJeremy L Thompson // ---- Restriction 153774398b5aSJeremy L Thompson CeedCallBackend(CeedOperatorBuildKernelRestriction_Hip_gen(code, data, i, NULL, op_output_fields[i], qf_output_fields[i], max_dim, Q_1d, false, 153874398b5aSJeremy L Thompson is_all_tensor, is_at_points, use_3d_slices)); 15397d8d0e25Snbeams } 15407d8d0e25Snbeams 15414b3e95d5SJeremy L Thompson // Close loop and function 15427d8d0e25Snbeams code << " }\n"; 15437d8d0e25Snbeams code << "}\n"; 15447d8d0e25Snbeams code << "// -----------------------------------------------------------------------------\n\n"; 15457d8d0e25Snbeams 1546539ec17dSJeremy L Thompson CeedInt block_sizes[3] = {0, 0, 0}; 15479e201c85SYohann CeedInt num_elem; 1548b7453713SJeremy L Thompson 15493a2968d6SJeremy L Thompson // Compile 15502b730f8bSJeremy L Thompson CeedCallBackend(CeedOperatorGetNumElements(op, &num_elem)); 155174398b5aSJeremy 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)); 155290c30374SJeremy L Thompson if (is_at_points) block_sizes[2] = 1; 15538d12f40eSJeremy L Thompson { 15548d12f40eSJeremy L Thompson bool is_compile_good = false; 15558d12f40eSJeremy L Thompson 1556a61b1c91SJeremy L Thompson data->thread_1d = block_sizes[0]; 15576b92dc4bSJeremy L Thompson CeedCallBackend(CeedTryCompile_Hip(ceed, code.str().c_str(), &is_compile_good, &data->module, 2, "OP_T_1D", block_sizes[0], "BLOCK_SIZE", 15582b730f8bSJeremy L Thompson block_sizes[0] * block_sizes[1] * block_sizes[2])); 15598d12f40eSJeremy L Thompson if (is_compile_good) { 15608d12f40eSJeremy L Thompson *is_good_build = true; 1561eb7e6cafSJeremy L Thompson CeedCallBackend(CeedGetKernel_Hip(ceed, data->module, operator_name.c_str(), &data->op)); 15628d12f40eSJeremy L Thompson } else { 15638d12f40eSJeremy L Thompson *is_good_build = false; 15648d12f40eSJeremy L Thompson data->use_fallback = true; 15658d12f40eSJeremy L Thompson } 15668d12f40eSJeremy L Thompson } 15672b730f8bSJeremy L Thompson CeedCallBackend(CeedOperatorSetSetupDone(op)); 15689bc66399SJeremy L Thompson CeedCallBackend(CeedDestroy(&ceed)); 1569c11e12f4SJeremy L Thompson CeedCallBackend(CeedQFunctionDestroy(&qf)); 1570e15f9bd0SJeremy L Thompson return CEED_ERROR_SUCCESS; 15717d8d0e25Snbeams } 15722a86cc9dSSebastian Grimberg 15737d8d0e25Snbeams //------------------------------------------------------------------------------ 1574