15aed82e4SJeremy L Thompson // Copyright (c) 2017-2024, Lawrence Livermore National Security, LLC and other CEED contributors. 23d8e8822SJeremy L Thompson // All Rights Reserved. See the top-level LICENSE and NOTICE files for details. 37d8d0e25Snbeams // 43d8e8822SJeremy L Thompson // SPDX-License-Identifier: BSD-2-Clause 57d8d0e25Snbeams // 63d8e8822SJeremy L Thompson // This file is part of CEED: http://github.com/ceed 73d576824SJeremy L Thompson 87d8d0e25Snbeams #define CEED_DEBUG_COLOR 12 97d8d0e25Snbeams 1049aac155SJeremy L Thompson #include <ceed.h> 11ec3da8bcSJed Brown #include <ceed/backend.h> 129e201c85SYohann #include <ceed/jit-tools.h> 132b730f8bSJeremy L Thompson 147d8d0e25Snbeams #include <iostream> 157d8d0e25Snbeams #include <sstream> 162b730f8bSJeremy L Thompson #include <string> 172b730f8bSJeremy L Thompson 180d0321e0SJeremy L Thompson #include "../hip-ref/ceed-hip-ref.h" 197d8d0e25Snbeams #include "../hip-shared/ceed-hip-shared.h" 20b2165e7aSSebastian Grimberg #include "../hip/ceed-hip-common.h" 217d8d0e25Snbeams #include "../hip/ceed-hip-compile.h" 222b730f8bSJeremy L Thompson #include "ceed-hip-gen.h" 23b3e1519bSnbeams 2445a787f7SJeremy L Thompson struct FieldReuse_Hip { 2545a787f7SJeremy L Thompson CeedInt index; 2645a787f7SJeremy L Thompson bool is_input; 2745a787f7SJeremy L Thompson CeedEvalMode eval_mode; 2845a787f7SJeremy L Thompson }; 2945a787f7SJeremy L Thompson 30b3e1519bSnbeams //------------------------------------------------------------------------------ 31b3e1519bSnbeams // Calculate the block size used for launching the operator kernel 32b3e1519bSnbeams //------------------------------------------------------------------------------ 332b730f8bSJeremy L Thompson extern "C" int BlockGridCalculate_Hip_gen(const CeedInt dim, const CeedInt num_elem, const CeedInt P_1d, const CeedInt Q_1d, CeedInt *block_sizes) { 343a2968d6SJeremy L Thompson const CeedInt thread_1d = CeedIntMax(Q_1d, P_1d); 35b3e1519bSnbeams if (dim == 1) { 363a2968d6SJeremy L Thompson CeedInt elems_per_block = 64 * thread_1d > 256 ? 256 / thread_1d : 64; 37b7453713SJeremy L Thompson 389e201c85SYohann elems_per_block = elems_per_block > 0 ? elems_per_block : 1; 393a2968d6SJeremy L Thompson block_sizes[0] = thread_1d; 40b3e1519bSnbeams block_sizes[1] = 1; 419e201c85SYohann block_sizes[2] = elems_per_block; 42b3e1519bSnbeams } else if (dim == 2) { 433a2968d6SJeremy L Thompson const CeedInt elems_per_block = thread_1d < 4 ? 16 : 2; 44b7453713SJeremy L Thompson 453a2968d6SJeremy L Thompson block_sizes[0] = thread_1d; 463a2968d6SJeremy L Thompson block_sizes[1] = thread_1d; 479e201c85SYohann block_sizes[2] = elems_per_block; 48b3e1519bSnbeams } else if (dim == 3) { 493a2968d6SJeremy L Thompson const CeedInt elems_per_block = thread_1d < 6 ? 4 : (thread_1d < 8 ? 2 : 1); 50b7453713SJeremy L Thompson 513a2968d6SJeremy L Thompson block_sizes[0] = thread_1d; 523a2968d6SJeremy L Thompson block_sizes[1] = thread_1d; 539e201c85SYohann block_sizes[2] = elems_per_block; 54b3e1519bSnbeams } 55b3e1519bSnbeams return CEED_ERROR_SUCCESS; 56b3e1519bSnbeams } 57b3e1519bSnbeams 587d8d0e25Snbeams //------------------------------------------------------------------------------ 594b3e95d5SJeremy L Thompson // Determine type of operator 604b3e95d5SJeremy L Thompson //------------------------------------------------------------------------------ 614b3e95d5SJeremy L Thompson static int CeedOperatorBuildKernelData_Hip_gen(Ceed ceed, CeedInt num_input_fields, CeedOperatorField *op_input_fields, 624b3e95d5SJeremy L Thompson CeedQFunctionField *qf_input_fields, CeedInt num_output_fields, CeedOperatorField *op_output_fields, 634b3e95d5SJeremy L Thompson CeedQFunctionField *qf_output_fields, CeedInt *max_P_1d, CeedInt *Q_1d, CeedInt *dim, bool *is_tensor, 644b3e95d5SJeremy L Thompson bool *use_3d_slices) { 654b3e95d5SJeremy L Thompson // Find dim, P_1d, Q_1d 664b3e95d5SJeremy L Thompson *max_P_1d = 0; 674b3e95d5SJeremy L Thompson *Q_1d = 0; 684b3e95d5SJeremy L Thompson *dim = 0; 694b3e95d5SJeremy L Thompson *is_tensor = true; 709123fb08SJeremy L Thompson 714b3e95d5SJeremy L Thompson for (CeedInt i = 0; i < num_input_fields; i++) { 724b3e95d5SJeremy L Thompson CeedBasis basis; 734b3e95d5SJeremy L Thompson 744b3e95d5SJeremy L Thompson CeedCallBackend(CeedOperatorFieldGetBasis(op_input_fields[i], &basis)); 754b3e95d5SJeremy L Thompson if (basis != CEED_BASIS_NONE) { 764b3e95d5SJeremy L Thompson bool is_field_tensor; 774b3e95d5SJeremy L Thompson CeedInt field_P_1d = 0, field_Q_1d = 0, field_dim = 0; 784b3e95d5SJeremy L Thompson 794b3e95d5SJeremy L Thompson // Collect dim, P_1d, and Q_1d 804b3e95d5SJeremy L Thompson CeedCallBackend(CeedBasisIsTensor(basis, &is_field_tensor)); 814b3e95d5SJeremy L Thompson *is_tensor = *is_tensor && is_field_tensor; 829123fb08SJeremy L Thompson if (is_field_tensor) CeedCallBackend(CeedBasisGetNumNodes1D(basis, &field_P_1d)); 839123fb08SJeremy L Thompson else CeedCallBackend(CeedBasisGetNumNodes(basis, &field_P_1d)); 844b3e95d5SJeremy L Thompson *max_P_1d = CeedIntMax(*max_P_1d, field_P_1d); 854b3e95d5SJeremy L Thompson CeedCallBackend(CeedBasisGetDimension(basis, &field_dim)); 864b3e95d5SJeremy L Thompson CeedCheck(*dim == 0 || field_dim == *dim, ceed, CEED_ERROR_BACKEND, "Quadrature spaces must be compatible"); 874b3e95d5SJeremy L Thompson *dim = field_dim; 889123fb08SJeremy L Thompson if (is_field_tensor) CeedCallBackend(CeedBasisGetNumQuadraturePoints1D(basis, &field_Q_1d)); 899123fb08SJeremy L Thompson else CeedCallBackend(CeedBasisGetNumQuadraturePoints(basis, &field_Q_1d)); 904b3e95d5SJeremy L Thompson CeedCheck(*Q_1d == 0 || field_Q_1d == *Q_1d, ceed, CEED_ERROR_BACKEND, "Quadrature spaces must be compatible"); 914b3e95d5SJeremy L Thompson *Q_1d = field_Q_1d; 924b3e95d5SJeremy L Thompson } 933a2968d6SJeremy L Thompson CeedCallBackend(CeedBasisDestroy(&basis)); 944b3e95d5SJeremy L Thompson } 954b3e95d5SJeremy L Thompson for (CeedInt i = 0; i < num_output_fields; i++) { 964b3e95d5SJeremy L Thompson CeedBasis basis; 974b3e95d5SJeremy L Thompson 984b3e95d5SJeremy L Thompson CeedCallBackend(CeedOperatorFieldGetBasis(op_output_fields[i], &basis)); 994b3e95d5SJeremy L Thompson if (basis != CEED_BASIS_NONE) { 1004b3e95d5SJeremy L Thompson bool is_field_tensor; 1014b3e95d5SJeremy L Thompson CeedInt field_P_1d = 0, field_Q_1d = 0, field_dim = 0; 1024b3e95d5SJeremy L Thompson 1034b3e95d5SJeremy L Thompson // Collect dim, P_1d, and Q_1d 1044b3e95d5SJeremy L Thompson CeedCallBackend(CeedBasisIsTensor(basis, &is_field_tensor)); 1054b3e95d5SJeremy L Thompson *is_tensor = *is_tensor && is_field_tensor; 1069123fb08SJeremy L Thompson if (is_field_tensor) CeedCallBackend(CeedBasisGetNumNodes1D(basis, &field_P_1d)); 1079123fb08SJeremy L Thompson else CeedCallBackend(CeedBasisGetNumNodes(basis, &field_P_1d)); 1084b3e95d5SJeremy L Thompson *max_P_1d = CeedIntMax(*max_P_1d, field_P_1d); 1094b3e95d5SJeremy L Thompson CeedCallBackend(CeedBasisGetDimension(basis, &field_dim)); 1104b3e95d5SJeremy L Thompson CeedCheck(*dim == 0 || field_dim == *dim, ceed, CEED_ERROR_BACKEND, "Quadrature spaces must be compatible"); 1114b3e95d5SJeremy L Thompson *dim = field_dim; 1129123fb08SJeremy L Thompson if (is_field_tensor) CeedCallBackend(CeedBasisGetNumQuadraturePoints1D(basis, &field_Q_1d)); 1139123fb08SJeremy L Thompson else CeedCallBackend(CeedBasisGetNumQuadraturePoints(basis, &field_Q_1d)); 1144b3e95d5SJeremy L Thompson CeedCheck(*Q_1d == 0 || field_Q_1d == *Q_1d, ceed, CEED_ERROR_BACKEND, "Quadrature spaces must be compatible"); 1154b3e95d5SJeremy L Thompson *Q_1d = field_Q_1d; 1164b3e95d5SJeremy L Thompson } 1173a2968d6SJeremy L Thompson CeedCallBackend(CeedBasisDestroy(&basis)); 1184b3e95d5SJeremy L Thompson } 1194b3e95d5SJeremy L Thompson 1204b3e95d5SJeremy L Thompson // Only use 3D collocated gradient parallelization strategy when gradient is computed 1214b3e95d5SJeremy L Thompson *use_3d_slices = false; 1224b3e95d5SJeremy L Thompson if (*dim == 3) { 1234b3e95d5SJeremy L Thompson bool was_grad_found = false; 1244b3e95d5SJeremy L Thompson 1254b3e95d5SJeremy L Thompson for (CeedInt i = 0; i < num_input_fields; i++) { 1264b3e95d5SJeremy L Thompson CeedEvalMode eval_mode; 1274b3e95d5SJeremy L Thompson 1284b3e95d5SJeremy L Thompson CeedCallBackend(CeedQFunctionFieldGetEvalMode(qf_input_fields[i], &eval_mode)); 1294b3e95d5SJeremy L Thompson if (eval_mode == CEED_EVAL_GRAD) { 1304b3e95d5SJeremy L Thompson CeedBasis_Hip_shared *basis_data; 1314b3e95d5SJeremy L Thompson CeedBasis basis; 1324b3e95d5SJeremy L Thompson 1334b3e95d5SJeremy L Thompson CeedCallBackend(CeedOperatorFieldGetBasis(op_input_fields[i], &basis)); 1344b3e95d5SJeremy L Thompson CeedCallBackend(CeedBasisGetData(basis, &basis_data)); 1354b3e95d5SJeremy L Thompson *use_3d_slices = basis_data->d_collo_grad_1d && (was_grad_found ? *use_3d_slices : true); 1364b3e95d5SJeremy L Thompson was_grad_found = true; 1373a2968d6SJeremy L Thompson CeedCallBackend(CeedBasisDestroy(&basis)); 1384b3e95d5SJeremy L Thompson } 1394b3e95d5SJeremy L Thompson } 1404b3e95d5SJeremy L Thompson for (CeedInt i = 0; i < num_output_fields; i++) { 1414b3e95d5SJeremy L Thompson CeedEvalMode eval_mode; 1424b3e95d5SJeremy L Thompson 1434b3e95d5SJeremy L Thompson CeedCallBackend(CeedQFunctionFieldGetEvalMode(qf_output_fields[i], &eval_mode)); 1444b3e95d5SJeremy L Thompson if (eval_mode == CEED_EVAL_GRAD) { 1454b3e95d5SJeremy L Thompson CeedBasis_Hip_shared *basis_data; 1464b3e95d5SJeremy L Thompson CeedBasis basis; 1474b3e95d5SJeremy L Thompson 1484b3e95d5SJeremy L Thompson CeedCallBackend(CeedOperatorFieldGetBasis(op_output_fields[i], &basis)); 1494b3e95d5SJeremy L Thompson CeedCallBackend(CeedBasisGetData(basis, &basis_data)); 1504b3e95d5SJeremy L Thompson *use_3d_slices = basis_data->d_collo_grad_1d && (was_grad_found ? *use_3d_slices : true); 1514b3e95d5SJeremy L Thompson was_grad_found = true; 1523a2968d6SJeremy L Thompson CeedCallBackend(CeedBasisDestroy(&basis)); 1534b3e95d5SJeremy L Thompson } 1544b3e95d5SJeremy L Thompson } 1554b3e95d5SJeremy L Thompson } 1564b3e95d5SJeremy L Thompson return CEED_ERROR_SUCCESS; 1574b3e95d5SJeremy L Thompson } 1584b3e95d5SJeremy L Thompson 1594b3e95d5SJeremy L Thompson //------------------------------------------------------------------------------ 1604b3e95d5SJeremy L Thompson // Setup fields 1614b3e95d5SJeremy L Thompson //------------------------------------------------------------------------------ 1624b3e95d5SJeremy L Thompson static int CeedOperatorBuildKernelFieldData_Hip_gen(std::ostringstream &code, CeedOperator_Hip_gen *data, CeedInt i, CeedOperatorField op_field, 16345a787f7SJeremy L Thompson CeedQFunctionField qf_field, FieldReuse_Hip field_reuse, CeedInt Q_1d, bool is_input, 16445a787f7SJeremy L Thompson bool is_tensor, bool is_at_points, bool use_3d_slices) { 165*59fa3f92SJeremy L Thompson const char *field_name; 1664b3e95d5SJeremy L Thompson std::string var_suffix = (is_input ? "_in_" : "_out_") + std::to_string(i); 1679123fb08SJeremy L Thompson std::string P_name = (is_tensor ? "P_1d" : "P") + var_suffix, Q_name = is_tensor ? "Q_1d" : "Q"; 1684b3e95d5SJeremy L Thompson std::string option_name = (is_input ? "inputs" : "outputs"); 1694b3e95d5SJeremy L Thompson CeedEvalMode eval_mode = CEED_EVAL_NONE; 1704b3e95d5SJeremy L Thompson CeedInt elem_size = 0, num_comp = 0, P_1d = 0; 1714b3e95d5SJeremy L Thompson CeedElemRestriction elem_rstr; 1724b3e95d5SJeremy L Thompson CeedBasis_Hip_shared *basis_data; 1734b3e95d5SJeremy L Thompson CeedBasis basis; 1744b3e95d5SJeremy L Thompson 1759ee499e5SJeremy L Thompson // Field reuse info 17645a787f7SJeremy L Thompson bool use_previous_field = field_reuse.index != -1; 1779ee499e5SJeremy L Thompson 178*59fa3f92SJeremy L Thompson CeedCallBackend(CeedOperatorFieldGetName(op_field, &field_name)); 179*59fa3f92SJeremy L Thompson code << " // -- " << (is_input ? "Input" : "Output") << " field " << i << ": " << field_name << "\n"; 1804b3e95d5SJeremy L Thompson 1814b3e95d5SJeremy L Thompson // Get field data 1824b3e95d5SJeremy L Thompson CeedCallBackend(CeedOperatorFieldGetElemRestriction(op_field, &elem_rstr)); 1834b3e95d5SJeremy L Thompson if (elem_rstr != CEED_ELEMRESTRICTION_NONE) { 1844b3e95d5SJeremy L Thompson CeedCallBackend(CeedElemRestrictionGetElementSize(elem_rstr, &elem_size)); 1854b3e95d5SJeremy L Thompson CeedCallBackend(CeedElemRestrictionGetNumComponents(elem_rstr, &num_comp)); 1864b3e95d5SJeremy L Thompson } 1873a2968d6SJeremy L Thompson CeedCallBackend(CeedElemRestrictionDestroy(&elem_rstr)); 1884b3e95d5SJeremy L Thompson CeedCallBackend(CeedOperatorFieldGetBasis(op_field, &basis)); 1894b3e95d5SJeremy L Thompson if (basis != CEED_BASIS_NONE) { 1904b3e95d5SJeremy L Thompson CeedCallBackend(CeedBasisGetData(basis, &basis_data)); 1919123fb08SJeremy L Thompson if (is_tensor) CeedCallBackend(CeedBasisGetNumNodes1D(basis, &P_1d)); 1929123fb08SJeremy L Thompson else CeedCallBackend(CeedBasisGetNumNodes(basis, &P_1d)); 1934b3e95d5SJeremy L Thompson } 1944b3e95d5SJeremy L Thompson CeedCallBackend(CeedQFunctionFieldGetEvalMode(qf_field, &eval_mode)); 1954b3e95d5SJeremy L Thompson 1964b3e95d5SJeremy L Thompson // Set field constants 1974b3e95d5SJeremy L Thompson if (eval_mode != CEED_EVAL_WEIGHT) { 1984b3e95d5SJeremy L Thompson code << " const CeedInt " << P_name << " = " << (basis == CEED_BASIS_NONE ? Q_1d : P_1d) << ";\n"; 1994b3e95d5SJeremy L Thompson code << " const CeedInt num_comp" << var_suffix << " = " << num_comp << ";\n"; 2004b3e95d5SJeremy L Thompson } 2014b3e95d5SJeremy L Thompson 2024b3e95d5SJeremy L Thompson // Load basis data 2034b3e95d5SJeremy L Thompson code << " // EvalMode: " << CeedEvalModes[eval_mode] << "\n"; 2044b3e95d5SJeremy L Thompson switch (eval_mode) { 2054b3e95d5SJeremy L Thompson case CEED_EVAL_NONE: 2064b3e95d5SJeremy L Thompson break; 2074b3e95d5SJeremy L Thompson case CEED_EVAL_INTERP: 2083a2968d6SJeremy L Thompson if (is_at_points) { 2093a2968d6SJeremy L Thompson // AtPoints 2103a2968d6SJeremy L Thompson if (!basis_data->d_chebyshev_interp_1d) { 2113a2968d6SJeremy L Thompson CeedSize interp_bytes; 2123a2968d6SJeremy L Thompson CeedScalar *chebyshev_interp_1d; 2133a2968d6SJeremy L Thompson 2143a2968d6SJeremy L Thompson interp_bytes = P_1d * Q_1d * sizeof(CeedScalar); 2153a2968d6SJeremy L Thompson CeedCallBackend(CeedCalloc(P_1d * Q_1d, &chebyshev_interp_1d)); 2163a2968d6SJeremy L Thompson CeedCallBackend(CeedBasisGetChebyshevInterp1D(basis, chebyshev_interp_1d)); 2173a2968d6SJeremy L Thompson CeedCallHip(CeedBasisReturnCeed(basis), hipMalloc((void **)&basis_data->d_chebyshev_interp_1d, interp_bytes)); 2183a2968d6SJeremy L Thompson CeedCallHip(CeedBasisReturnCeed(basis), 2193a2968d6SJeremy L Thompson hipMemcpy(basis_data->d_chebyshev_interp_1d, chebyshev_interp_1d, interp_bytes, hipMemcpyHostToDevice)); 2203a2968d6SJeremy L Thompson CeedCallBackend(CeedFree(&chebyshev_interp_1d)); 2213a2968d6SJeremy L Thompson } 2223a2968d6SJeremy L Thompson if (is_input) data->B.inputs[i] = basis_data->d_chebyshev_interp_1d; 2233a2968d6SJeremy L Thompson else data->B.outputs[i] = basis_data->d_chebyshev_interp_1d; 2243a2968d6SJeremy L Thompson } else { 2253a2968d6SJeremy L Thompson // Standard quadrature 2264b3e95d5SJeremy L Thompson if (is_input) data->B.inputs[i] = basis_data->d_interp_1d; 2274b3e95d5SJeremy L Thompson else data->B.outputs[i] = basis_data->d_interp_1d; 2283a2968d6SJeremy L Thompson } 2299ee499e5SJeremy L Thompson if (use_previous_field) { 23045a787f7SJeremy L Thompson std::string reuse_var = "s_B" + ((field_reuse.is_input ? "_in_" : "_out_") + std::to_string(field_reuse.index)); 2319ee499e5SJeremy L Thompson 2329ee499e5SJeremy L Thompson code << " CeedScalar *s_B" << var_suffix << " = " << reuse_var << ";\n"; 2339ee499e5SJeremy L Thompson } else { 2349123fb08SJeremy L Thompson code << " __shared__ CeedScalar s_B" << var_suffix << "[" << P_name << "*" << Q_name << "];\n"; 235f815fac9SJeremy L Thompson code << " LoadMatrix<" << P_name << ", " << Q_name << ">(data, B." << option_name << "[" << i << "], s_B" << var_suffix << ");\n"; 2369ee499e5SJeremy L Thompson } 2374b3e95d5SJeremy L Thompson break; 2384b3e95d5SJeremy L Thompson case CEED_EVAL_GRAD: 2393a2968d6SJeremy L Thompson if (is_at_points) { 2403a2968d6SJeremy L Thompson // AtPoints 2413a2968d6SJeremy L Thompson if (!basis_data->d_chebyshev_interp_1d) { 2423a2968d6SJeremy L Thompson CeedSize interp_bytes; 2433a2968d6SJeremy L Thompson CeedScalar *chebyshev_interp_1d; 2443a2968d6SJeremy L Thompson 2453a2968d6SJeremy L Thompson interp_bytes = P_1d * Q_1d * sizeof(CeedScalar); 2463a2968d6SJeremy L Thompson CeedCallBackend(CeedCalloc(P_1d * Q_1d, &chebyshev_interp_1d)); 2473a2968d6SJeremy L Thompson CeedCallBackend(CeedBasisGetChebyshevInterp1D(basis, chebyshev_interp_1d)); 2483a2968d6SJeremy L Thompson CeedCallHip(CeedBasisReturnCeed(basis), hipMalloc((void **)&basis_data->d_chebyshev_interp_1d, interp_bytes)); 2493a2968d6SJeremy L Thompson CeedCallHip(CeedBasisReturnCeed(basis), 2503a2968d6SJeremy L Thompson hipMemcpy(basis_data->d_chebyshev_interp_1d, chebyshev_interp_1d, interp_bytes, hipMemcpyHostToDevice)); 2513a2968d6SJeremy L Thompson CeedCallBackend(CeedFree(&chebyshev_interp_1d)); 2523a2968d6SJeremy L Thompson } 2533a2968d6SJeremy L Thompson if (is_input) data->B.inputs[i] = basis_data->d_chebyshev_interp_1d; 2543a2968d6SJeremy L Thompson else data->B.outputs[i] = basis_data->d_chebyshev_interp_1d; 2553a2968d6SJeremy L Thompson } else { 2563a2968d6SJeremy L Thompson // Standard quadrature 2574b3e95d5SJeremy L Thompson if (is_input) data->B.inputs[i] = basis_data->d_interp_1d; 2584b3e95d5SJeremy L Thompson else data->B.outputs[i] = basis_data->d_interp_1d; 2593a2968d6SJeremy L Thompson } 2609123fb08SJeremy L Thompson if (is_tensor) { 2619ee499e5SJeremy L Thompson if (use_previous_field) { 26245a787f7SJeremy L Thompson std::string reuse_var = "s_B" + ((field_reuse.is_input ? "_in_" : "_out_") + std::to_string(field_reuse.index)); 2639ee499e5SJeremy L Thompson 2649ee499e5SJeremy L Thompson code << " CeedScalar *s_B" << var_suffix << " = " << reuse_var << ";\n"; 2659ee499e5SJeremy L Thompson } else { 2669123fb08SJeremy L Thompson code << " __shared__ CeedScalar s_B" << var_suffix << "[" << P_name << "*" << Q_name << "];\n"; 267f815fac9SJeremy L Thompson code << " LoadMatrix<" << P_name << ", " << Q_name << ">(data, B." << option_name << "[" << i << "], s_B" << var_suffix << ");\n"; 2689123fb08SJeremy L Thompson } 2699ee499e5SJeremy L Thompson } 2703a2968d6SJeremy L Thompson if (is_at_points) break; // No G mat for AtPoints 2714b3e95d5SJeremy L Thompson if (use_3d_slices) { 2724b3e95d5SJeremy L Thompson if (is_input) data->G.inputs[i] = basis_data->d_collo_grad_1d; 2734b3e95d5SJeremy L Thompson else data->G.outputs[i] = basis_data->d_collo_grad_1d; 27445a787f7SJeremy L Thompson if (use_previous_field && field_reuse.eval_mode == CEED_EVAL_GRAD) { 27545a787f7SJeremy L Thompson std::string reuse_var = "s_G" + ((field_reuse.is_input ? "_in_" : "_out_") + std::to_string(field_reuse.index)); 2769ee499e5SJeremy L Thompson 2779ee499e5SJeremy L Thompson code << " CeedScalar *s_G" << var_suffix << " = " << reuse_var << ";\n"; 2789ee499e5SJeremy L Thompson } else { 2799123fb08SJeremy L Thompson code << " __shared__ CeedScalar s_G" << var_suffix << "[" << Q_name << "*" << Q_name << "];\n"; 280f815fac9SJeremy L Thompson code << " LoadMatrix<" << Q_name << ", " << Q_name << ">(data, G." << option_name << "[" << i << "], s_G" << var_suffix << ");\n"; 2819ee499e5SJeremy L Thompson } 2824b3e95d5SJeremy L Thompson } else { 2834b3e95d5SJeremy L Thompson bool has_collo_grad = basis_data->d_collo_grad_1d; 2844b3e95d5SJeremy L Thompson 2854b3e95d5SJeremy L Thompson if (is_input) data->G.inputs[i] = has_collo_grad ? basis_data->d_collo_grad_1d : basis_data->d_grad_1d; 2864b3e95d5SJeremy L Thompson else data->G.outputs[i] = has_collo_grad ? basis_data->d_collo_grad_1d : basis_data->d_grad_1d; 2874b3e95d5SJeremy L Thompson if (has_collo_grad) { 28845a787f7SJeremy L Thompson if (use_previous_field && field_reuse.eval_mode == CEED_EVAL_GRAD) { 28945a787f7SJeremy L Thompson std::string reuse_var = "s_G" + ((field_reuse.is_input ? "_in_" : "_out_") + std::to_string(field_reuse.index)); 2909ee499e5SJeremy L Thompson 2919ee499e5SJeremy L Thompson code << " CeedScalar *s_G" << var_suffix << " = " << reuse_var << ";\n"; 2929ee499e5SJeremy L Thompson } else { 2939123fb08SJeremy L Thompson code << " __shared__ CeedScalar s_G" << var_suffix << "[" << Q_name << "*" << Q_name << "];\n"; 294f815fac9SJeremy L Thompson code << " LoadMatrix<" << Q_name << ", " << Q_name << ">(data, G." << option_name << "[" << i << "], s_G" << var_suffix << ");\n"; 2959ee499e5SJeremy L Thompson } 2969ee499e5SJeremy L Thompson } else { 29745a787f7SJeremy L Thompson if (use_previous_field && field_reuse.eval_mode == CEED_EVAL_GRAD) { 29845a787f7SJeremy L Thompson std::string reuse_var = "s_G" + ((field_reuse.is_input ? "_in_" : "_out_") + std::to_string(field_reuse.index)); 2999ee499e5SJeremy L Thompson 3009ee499e5SJeremy L Thompson code << " CeedScalar *s_G" << var_suffix << " = " << reuse_var << ";\n"; 3014b3e95d5SJeremy L Thompson } else { 3029123fb08SJeremy L Thompson code << " __shared__ CeedScalar s_G" << var_suffix << "[" << P_name << "*" << Q_name << (is_tensor ? "" : "*dim") << "];\n"; 3039123fb08SJeremy L Thompson code << " LoadMatrix<" << P_name << ", " << Q_name << (is_tensor ? "" : "*dim") << ">(data, G." << option_name << "[" << i << "], s_G" 3049123fb08SJeremy L Thompson << var_suffix << ");\n"; 3054b3e95d5SJeremy L Thompson } 3064b3e95d5SJeremy L Thompson } 3079ee499e5SJeremy L Thompson } 3084b3e95d5SJeremy L Thompson break; 3094b3e95d5SJeremy L Thompson case CEED_EVAL_WEIGHT: 3104b3e95d5SJeremy L Thompson break; // No action 3114b3e95d5SJeremy L Thompson // LCOV_EXCL_START 3124b3e95d5SJeremy L Thompson case CEED_EVAL_DIV: 3134b3e95d5SJeremy L Thompson case CEED_EVAL_CURL: 3144b3e95d5SJeremy L Thompson break; // TODO: Not implemented 3154b3e95d5SJeremy L Thompson // LCOV_EXCL_STOP 3164b3e95d5SJeremy L Thompson } 3173a2968d6SJeremy L Thompson CeedCallBackend(CeedBasisDestroy(&basis)); 3184b3e95d5SJeremy L Thompson return CEED_ERROR_SUCCESS; 3194b3e95d5SJeremy L Thompson } 3204b3e95d5SJeremy L Thompson 3214b3e95d5SJeremy L Thompson //------------------------------------------------------------------------------ 3224b3e95d5SJeremy L Thompson // Restriction 3234b3e95d5SJeremy L Thompson //------------------------------------------------------------------------------ 3244b3e95d5SJeremy L Thompson static int CeedOperatorBuildKernelRestriction_Hip_gen(std::ostringstream &code, CeedOperator_Hip_gen *data, CeedInt i, CeedInt dim, 325e93651e5SJeremy L Thompson CeedInt field_input_buffer[], CeedOperatorField op_field, CeedQFunctionField qf_field, 3269123fb08SJeremy L Thompson CeedInt Q_1d, bool is_input, bool is_tensor, bool is_at_points, bool use_3d_slices) { 3274b3e95d5SJeremy L Thompson std::string var_suffix = (is_input ? "_in_" : "_out_") + std::to_string(i); 3289123fb08SJeremy L Thompson std::string P_name = (is_tensor ? "P_1d" : "P") + var_suffix; 3294b3e95d5SJeremy L Thompson CeedEvalMode eval_mode = CEED_EVAL_NONE; 3304b3e95d5SJeremy L Thompson CeedInt elem_size = 0, num_comp = 0, P_1d = 0; 3314b3e95d5SJeremy L Thompson CeedSize l_size; 332f815fac9SJeremy L Thompson CeedRestrictionType rstr_type = CEED_RESTRICTION_STANDARD; 3334b3e95d5SJeremy L Thompson CeedElemRestriction_Hip *rstr_data; 3344b3e95d5SJeremy L Thompson CeedElemRestriction elem_rstr; 3354b3e95d5SJeremy L Thompson CeedBasis basis; 3364b3e95d5SJeremy L Thompson 3374b3e95d5SJeremy L Thompson // Get field data 3384b3e95d5SJeremy L Thompson CeedCallBackend(CeedOperatorFieldGetElemRestriction(op_field, &elem_rstr)); 3394b3e95d5SJeremy L Thompson if (elem_rstr != CEED_ELEMRESTRICTION_NONE) { 340f815fac9SJeremy L Thompson CeedCallBackend(CeedElemRestrictionGetType(elem_rstr, &rstr_type)); 3414b3e95d5SJeremy L Thompson CeedCallBackend(CeedElemRestrictionGetElementSize(elem_rstr, &elem_size)); 3424b3e95d5SJeremy L Thompson CeedCallBackend(CeedElemRestrictionGetNumComponents(elem_rstr, &num_comp)); 3434b3e95d5SJeremy L Thompson CeedCallBackend(CeedElemRestrictionGetData(elem_rstr, &rstr_data)); 3444b3e95d5SJeremy L Thompson } 3454b3e95d5SJeremy L Thompson CeedCallBackend(CeedOperatorFieldGetBasis(op_field, &basis)); 3464b3e95d5SJeremy L Thompson if (basis != CEED_BASIS_NONE) { 3479123fb08SJeremy L Thompson if (is_tensor) CeedCallBackend(CeedBasisGetNumNodes1D(basis, &P_1d)); 3489123fb08SJeremy L Thompson else CeedCallBackend(CeedBasisGetNumNodes(basis, &P_1d)); 3494b3e95d5SJeremy L Thompson } 3503a2968d6SJeremy L Thompson CeedCallBackend(CeedBasisDestroy(&basis)); 3514b3e95d5SJeremy L Thompson CeedCallBackend(CeedQFunctionFieldGetEvalMode(qf_field, &eval_mode)); 3524b3e95d5SJeremy L Thompson 3534b3e95d5SJeremy L Thompson // Restriction 3544b3e95d5SJeremy L Thompson if (is_input) { 3554b3e95d5SJeremy L Thompson // Input 356e93651e5SJeremy L Thompson if (field_input_buffer[i] != i) { 357e93651e5SJeremy L Thompson std::string buffer_name = "r_e_in_" + std::to_string(field_input_buffer[i]); 358e93651e5SJeremy L Thompson 359e93651e5SJeremy L Thompson // Restriction was already done for previous input 360e93651e5SJeremy L Thompson code << " CeedScalar *r_e" << var_suffix << " = " << buffer_name << ";\n"; 3613a2968d6SJeremy L Thompson } else if (eval_mode != CEED_EVAL_WEIGHT && !((eval_mode == CEED_EVAL_NONE) && use_3d_slices && is_at_points)) { 3623a2968d6SJeremy L Thompson if (eval_mode == CEED_EVAL_NONE && rstr_type != CEED_RESTRICTION_POINTS) { 363e93651e5SJeremy L Thompson // No basis action, so r_e_in_* in also r_q_in_* and needs to be allocated 3644b3e95d5SJeremy L Thompson code << " CeedScalar r_e" << var_suffix << "[num_comp" << var_suffix << "*" << P_name << "];\n"; 3653a2968d6SJeremy L Thompson } else if (rstr_type != CEED_RESTRICTION_POINTS) { 366e93651e5SJeremy L Thompson // Otherwise we're using the scratch space 367e93651e5SJeremy L Thompson code << " CeedScalar *r_e" << var_suffix << " = r_e_scratch;\n"; 368e93651e5SJeremy L Thompson } 369f815fac9SJeremy L Thompson switch (rstr_type) { 370f815fac9SJeremy L Thompson case CEED_RESTRICTION_STANDARD: { 3714b3e95d5SJeremy L Thompson CeedInt comp_stride; 3724b3e95d5SJeremy L Thompson 3734b3e95d5SJeremy L Thompson CeedCallBackend(CeedElemRestrictionGetLVectorSize(elem_rstr, &l_size)); 3744b3e95d5SJeremy L Thompson code << " const CeedInt l_size" << var_suffix << " = " << l_size << ";\n"; 3754b3e95d5SJeremy L Thompson CeedCallBackend(CeedElemRestrictionGetCompStride(elem_rstr, &comp_stride)); 3764b3e95d5SJeremy L Thompson code << " // CompStride: " << comp_stride << "\n"; 3774b3e95d5SJeremy L Thompson data->indices.inputs[i] = (CeedInt *)rstr_data->d_offsets; 3789123fb08SJeremy L Thompson code << " ReadLVecStandard" << (is_tensor ? dim : 1) << "d<num_comp" << var_suffix << ", " << comp_stride << ", " << P_name 3799123fb08SJeremy L Thompson << ">(data, l_size" << var_suffix << ", elem, indices.inputs[" << i << "], d" << var_suffix << ", r_e" << var_suffix << ");\n"; 380f815fac9SJeremy L Thompson break; 381f815fac9SJeremy L Thompson } 382f815fac9SJeremy L Thompson case CEED_RESTRICTION_STRIDED: { 3834b3e95d5SJeremy L Thompson bool has_backend_strides; 3844b3e95d5SJeremy L Thompson CeedInt num_elem; 3854b3e95d5SJeremy L Thompson 3864b3e95d5SJeremy L Thompson CeedCallBackend(CeedElemRestrictionHasBackendStrides(elem_rstr, &has_backend_strides)); 3874b3e95d5SJeremy L Thompson CeedCallBackend(CeedElemRestrictionGetNumElements(elem_rstr, &num_elem)); 3884b3e95d5SJeremy L Thompson CeedInt strides[3] = {1, elem_size * num_elem, elem_size}; 3894b3e95d5SJeremy L Thompson 3904b3e95d5SJeremy L Thompson if (!has_backend_strides) { 3914b3e95d5SJeremy L Thompson CeedCallBackend(CeedElemRestrictionGetStrides(elem_rstr, strides)); 3924b3e95d5SJeremy L Thompson } 3934b3e95d5SJeremy L Thompson code << " // Strides: {" << strides[0] << ", " << strides[1] << ", " << strides[2] << "}\n"; 3949123fb08SJeremy L Thompson code << " ReadLVecStrided" << (is_tensor ? dim : 1) << "d<num_comp" << var_suffix << ", " << P_name << ", " << strides[0] << ", " 3959123fb08SJeremy L Thompson << strides[1] << ", " << strides[2] << ">(data, elem, d" << var_suffix << ", r_e" << var_suffix << ");\n"; 396f815fac9SJeremy L Thompson break; 397f815fac9SJeremy L Thompson } 3983a2968d6SJeremy L Thompson case CEED_RESTRICTION_POINTS: { 3993a2968d6SJeremy L Thompson CeedInt comp_stride; 4003a2968d6SJeremy L Thompson 4013a2968d6SJeremy L Thompson CeedCallBackend(CeedElemRestrictionGetCompStride(elem_rstr, &comp_stride)); 4023a2968d6SJeremy L Thompson code << " const CeedInt comp_stride" << var_suffix << " = " << comp_stride << ";\n"; 4033a2968d6SJeremy L Thompson data->indices.inputs[i] = (CeedInt *)rstr_data->d_offsets; 4043a2968d6SJeremy L Thompson break; 4053a2968d6SJeremy L Thompson } 406f815fac9SJeremy L Thompson // LCOV_EXCL_START 407f815fac9SJeremy L Thompson case CEED_RESTRICTION_ORIENTED: 408f815fac9SJeremy L Thompson case CEED_RESTRICTION_CURL_ORIENTED: 409f815fac9SJeremy L Thompson break; // TODO: Not implemented 410f815fac9SJeremy L Thompson // LCOV_EXCL_STOP 4114b3e95d5SJeremy L Thompson } 4124b3e95d5SJeremy L Thompson } 4134b3e95d5SJeremy L Thompson } else { 4144b3e95d5SJeremy L Thompson // Output 415f815fac9SJeremy L Thompson switch (rstr_type) { 416f815fac9SJeremy L Thompson case CEED_RESTRICTION_STANDARD: { 4174b3e95d5SJeremy L Thompson CeedInt comp_stride; 4184b3e95d5SJeremy L Thompson 4194b3e95d5SJeremy L Thompson CeedCallBackend(CeedElemRestrictionGetLVectorSize(elem_rstr, &l_size)); 4204b3e95d5SJeremy L Thompson code << " const CeedInt l_size" << var_suffix << " = " << l_size << ";\n"; 4214b3e95d5SJeremy L Thompson CeedCallBackend(CeedElemRestrictionGetCompStride(elem_rstr, &comp_stride)); 4224b3e95d5SJeremy L Thompson code << " // CompStride: " << comp_stride << "\n"; 4234b3e95d5SJeremy L Thompson data->indices.outputs[i] = (CeedInt *)rstr_data->d_offsets; 4249123fb08SJeremy L Thompson code << " WriteLVecStandard" << (is_tensor ? dim : 1) << "d<num_comp" << var_suffix << ", " << comp_stride << ", " << P_name 4259123fb08SJeremy L Thompson << ">(data, l_size" << var_suffix << ", elem, indices.outputs[" << i << "], r_e" << var_suffix << ", d" << var_suffix << ");\n"; 426f815fac9SJeremy L Thompson break; 427f815fac9SJeremy L Thompson } 428f815fac9SJeremy L Thompson case CEED_RESTRICTION_STRIDED: { 4294b3e95d5SJeremy L Thompson bool has_backend_strides; 4304b3e95d5SJeremy L Thompson CeedInt num_elem; 4314b3e95d5SJeremy L Thompson 4324b3e95d5SJeremy L Thompson CeedCallBackend(CeedElemRestrictionHasBackendStrides(elem_rstr, &has_backend_strides)); 4334b3e95d5SJeremy L Thompson CeedCallBackend(CeedElemRestrictionGetNumElements(elem_rstr, &num_elem)); 4344b3e95d5SJeremy L Thompson CeedInt strides[3] = {1, elem_size * num_elem, elem_size}; 4354b3e95d5SJeremy L Thompson 4364b3e95d5SJeremy L Thompson if (!has_backend_strides) { 4374b3e95d5SJeremy L Thompson CeedCallBackend(CeedElemRestrictionGetStrides(elem_rstr, strides)); 4384b3e95d5SJeremy L Thompson } 4394b3e95d5SJeremy L Thompson code << " // Strides: {" << strides[0] << ", " << strides[1] << ", " << strides[2] << "}\n"; 4409123fb08SJeremy L Thompson code << " WriteLVecStrided" << (is_tensor ? dim : 1) << "d<num_comp" << var_suffix << ", " << P_name << ", " << strides[0] << ", " 4419123fb08SJeremy L Thompson << strides[1] << ", " << strides[2] << ">(data, elem, r_e" << var_suffix << ", d" << var_suffix << ");\n"; 442f815fac9SJeremy L Thompson break; 443f815fac9SJeremy L Thompson } 4443a2968d6SJeremy L Thompson case CEED_RESTRICTION_POINTS: 4453a2968d6SJeremy L Thompson data->indices.outputs[i] = (CeedInt *)rstr_data->d_offsets; 4463a2968d6SJeremy L Thompson break; 447f815fac9SJeremy L Thompson // LCOV_EXCL_START 448f815fac9SJeremy L Thompson case CEED_RESTRICTION_ORIENTED: 449f815fac9SJeremy L Thompson case CEED_RESTRICTION_CURL_ORIENTED: 450f815fac9SJeremy L Thompson break; // TODO: Not implemented 451f815fac9SJeremy L Thompson // LCOV_EXCL_STOP 4524b3e95d5SJeremy L Thompson } 4534b3e95d5SJeremy L Thompson } 4543a2968d6SJeremy L Thompson CeedCallBackend(CeedElemRestrictionDestroy(&elem_rstr)); 4554b3e95d5SJeremy L Thompson return CEED_ERROR_SUCCESS; 4564b3e95d5SJeremy L Thompson } 4574b3e95d5SJeremy L Thompson 4584b3e95d5SJeremy L Thompson //------------------------------------------------------------------------------ 4594b3e95d5SJeremy L Thompson // Basis 4604b3e95d5SJeremy L Thompson //------------------------------------------------------------------------------ 4614b3e95d5SJeremy L Thompson static int CeedOperatorBuildKernelBasis_Hip_gen(std::ostringstream &code, CeedOperator_Hip_gen *data, CeedInt i, CeedInt dim, 4629123fb08SJeremy L Thompson CeedOperatorField op_field, CeedQFunctionField qf_field, CeedInt Q_1d, bool is_input, bool is_tensor, 4633a2968d6SJeremy L Thompson bool is_at_points, bool use_3d_slices) { 4644b3e95d5SJeremy L Thompson std::string var_suffix = (is_input ? "_in_" : "_out_") + std::to_string(i); 4659123fb08SJeremy L Thompson std::string P_name = (is_tensor ? "P_1d" : "P") + var_suffix, Q_name = is_tensor ? "Q_1d" : "Q"; 4664b3e95d5SJeremy L Thompson CeedEvalMode eval_mode = CEED_EVAL_NONE; 4674b3e95d5SJeremy L Thompson CeedInt elem_size = 0, num_comp = 0, P_1d = 0; 4684b3e95d5SJeremy L Thompson CeedElemRestriction elem_rstr; 4694b3e95d5SJeremy L Thompson CeedBasis basis; 4704b3e95d5SJeremy L Thompson 4714b3e95d5SJeremy L Thompson // Get field data 4724b3e95d5SJeremy L Thompson CeedCallBackend(CeedOperatorFieldGetElemRestriction(op_field, &elem_rstr)); 4734b3e95d5SJeremy L Thompson if (elem_rstr != CEED_ELEMRESTRICTION_NONE) { 4744b3e95d5SJeremy L Thompson CeedCallBackend(CeedElemRestrictionGetElementSize(elem_rstr, &elem_size)); 4754b3e95d5SJeremy L Thompson CeedCallBackend(CeedElemRestrictionGetNumComponents(elem_rstr, &num_comp)); 4764b3e95d5SJeremy L Thompson } 4773a2968d6SJeremy L Thompson CeedCallBackend(CeedElemRestrictionDestroy(&elem_rstr)); 4784b3e95d5SJeremy L Thompson CeedCallBackend(CeedOperatorFieldGetBasis(op_field, &basis)); 4794b3e95d5SJeremy L Thompson if (basis != CEED_BASIS_NONE) { 4809123fb08SJeremy L Thompson if (is_tensor) CeedCallBackend(CeedBasisGetNumNodes1D(basis, &P_1d)); 4819123fb08SJeremy L Thompson else CeedCallBackend(CeedBasisGetNumNodes(basis, &P_1d)); 4824b3e95d5SJeremy L Thompson } 4834b3e95d5SJeremy L Thompson CeedCallBackend(CeedQFunctionFieldGetEvalMode(qf_field, &eval_mode)); 4844b3e95d5SJeremy L Thompson 4854b3e95d5SJeremy L Thompson // Basis 4864b3e95d5SJeremy L Thompson code << " // EvalMode: " << CeedEvalModes[eval_mode] << "\n"; 4874b3e95d5SJeremy L Thompson if (is_input) { 4884b3e95d5SJeremy L Thompson switch (eval_mode) { 4894b3e95d5SJeremy L Thompson case CEED_EVAL_NONE: 4903a2968d6SJeremy L Thompson if (!use_3d_slices && !is_at_points) { 4914b3e95d5SJeremy L Thompson code << " CeedScalar *r_q" << var_suffix << " = r_e" << var_suffix << ";\n"; 4924b3e95d5SJeremy L Thompson } 4934b3e95d5SJeremy L Thompson break; 4944b3e95d5SJeremy L Thompson case CEED_EVAL_INTERP: 4953a2968d6SJeremy L Thompson if (is_at_points) { 4969123fb08SJeremy L Thompson std::string function_name = (dim == 1 ? "Interp" : "InterpTensor") + std::to_string(dim) + "d"; 4979123fb08SJeremy L Thompson 4989123fb08SJeremy L Thompson code << " CeedScalar r_c" << var_suffix << "[num_comp" << var_suffix << "*" << (dim >= 3 ? Q_name : "1") << "];\n"; 4999123fb08SJeremy L Thompson code << " " << function_name << "<num_comp" << var_suffix << ", " << P_name << ", " << Q_name << ">(data, r_e" << var_suffix << ", s_B" 5009123fb08SJeremy L Thompson << var_suffix << ", r_c" << var_suffix << ");\n"; 5013a2968d6SJeremy L Thompson } else { 5029123fb08SJeremy L Thompson std::string function_name = is_tensor ? ((dim == 1 ? "Interp" : "InterpTensor") + std::to_string(dim) + "d") : "InterpNonTensor"; 5039123fb08SJeremy L Thompson 5049123fb08SJeremy L Thompson code << " CeedScalar r_q" << var_suffix << "[num_comp" << var_suffix << "*" << (is_tensor && (dim >= 3) ? Q_name : "1") << "];\n"; 5059123fb08SJeremy L Thompson code << " " << function_name << "<num_comp" << var_suffix << ", " << P_name << ", " << Q_name << ">(data, r_e" << var_suffix << ", s_B" 5069123fb08SJeremy L Thompson << var_suffix << ", r_q" << var_suffix << ");\n"; 5073a2968d6SJeremy L Thompson } 5084b3e95d5SJeremy L Thompson break; 5094b3e95d5SJeremy L Thompson case CEED_EVAL_GRAD: 5103a2968d6SJeremy L Thompson if (is_at_points) { 5119123fb08SJeremy L Thompson std::string function_name = (dim == 1 ? "Interp" : "InterpTensor") + std::to_string(dim) + "d"; 5129123fb08SJeremy L Thompson 5139123fb08SJeremy L Thompson code << " CeedScalar r_c" << var_suffix << "[num_comp" << var_suffix << "*" << (dim >= 3 ? Q_name : "1") << "];\n"; 5149123fb08SJeremy L Thompson code << " " << function_name << "<num_comp" << var_suffix << ", " << P_name << ", " << Q_name << ">(data, r_e" << var_suffix << ", s_B" 5159123fb08SJeremy L Thompson << var_suffix << ", r_c" << var_suffix << ");\n"; 5163a2968d6SJeremy L Thompson } else if (use_3d_slices) { 5179123fb08SJeremy L Thompson std::string function_name = (dim > 1 ? "InterpTensor" : "Interp") + std::to_string(dim) + "d"; 5189123fb08SJeremy L Thompson 5194b3e95d5SJeremy L Thompson code << " CeedScalar r_q" << var_suffix << "[num_comp" << var_suffix << "*" << Q_name << "];\n"; 5209123fb08SJeremy L Thompson code << " " << function_name << "<num_comp" << var_suffix << ", " << P_name << ", " << Q_name << ">(data, r_e" << var_suffix << ", s_B" 5219123fb08SJeremy L Thompson << var_suffix << ", r_q" << var_suffix << ");\n"; 5229123fb08SJeremy L Thompson } else if (is_tensor) { 5239123fb08SJeremy L Thompson bool is_collocated = dim == 3 && Q_1d >= P_1d; 5249123fb08SJeremy L Thompson std::string function_name = (dim == 1 ? "Grad" : (is_collocated ? "GradTensorCollocated" : "GradTensor")) + std::to_string(dim) + "d"; 5259123fb08SJeremy L Thompson 5269123fb08SJeremy L Thompson code << " CeedScalar r_q" << var_suffix << "[num_comp" << var_suffix << "*dim*" << (dim >= 3 ? Q_name : "1") << "];\n"; 5279123fb08SJeremy L Thompson code << " " << function_name << "<num_comp" << var_suffix << ", " << P_name << ", " << Q_name << ">(data, r_e" << var_suffix << ", s_B" 5289123fb08SJeremy L Thompson << var_suffix << ", s_G" << var_suffix << ", r_q" << var_suffix << ");\n"; 5294b3e95d5SJeremy L Thompson } else { 5309123fb08SJeremy L Thompson std::string function_name = "GradNonTensor"; 5319123fb08SJeremy L Thompson 5329123fb08SJeremy L Thompson code << " CeedScalar r_q" << var_suffix << "[num_comp" << var_suffix << "*dim];\n"; 5339123fb08SJeremy L Thompson code << " " << function_name << "<num_comp" << var_suffix << ", dim, " << P_name << ", " << Q_name << ">(data, r_e" << var_suffix 5349123fb08SJeremy L Thompson << ", s_G" << var_suffix << ", r_q" << var_suffix << ");\n"; 5354b3e95d5SJeremy L Thompson } 5364b3e95d5SJeremy L Thompson break; 5374b3e95d5SJeremy L Thompson case CEED_EVAL_WEIGHT: { 5383a2968d6SJeremy L Thompson if (is_at_points) { 5393a2968d6SJeremy L Thompson code << " // Nothing to do AtPoints\n"; 5403a2968d6SJeremy L Thompson } else { 5414b3e95d5SJeremy L Thompson CeedBasis_Hip_shared *basis_data; 5429123fb08SJeremy L Thompson std::string function_name = is_tensor ? ((dim == 1 ? "Weight" : "WeightTensor") + std::to_string(dim) + "d") : "WeightNonTensor"; 5434b3e95d5SJeremy L Thompson 5449123fb08SJeremy L Thompson code << " CeedScalar r_q" << var_suffix << "[" << (is_tensor && (dim >= 3) ? Q_name : "1") << "];\n"; 5454b3e95d5SJeremy L Thompson CeedCallBackend(CeedBasisGetData(basis, &basis_data)); 5464b3e95d5SJeremy L Thompson data->W = basis_data->d_q_weight_1d; 5479123fb08SJeremy L Thompson code << " " << function_name << "<" << Q_name << ">(data, W, r_q" << var_suffix << ");\n"; 5483a2968d6SJeremy L Thompson } 5494b3e95d5SJeremy L Thompson break; 5504b3e95d5SJeremy L Thompson } 5514b3e95d5SJeremy L Thompson // LCOV_EXCL_START 5524b3e95d5SJeremy L Thompson case CEED_EVAL_DIV: 5534b3e95d5SJeremy L Thompson case CEED_EVAL_CURL: 5544b3e95d5SJeremy L Thompson break; // TODO: Not implemented 5554b3e95d5SJeremy L Thompson // LCOV_EXCL_STOP 5564b3e95d5SJeremy L Thompson } 5574b3e95d5SJeremy L Thompson } else { 5584b3e95d5SJeremy L Thompson switch (eval_mode) { 5594b3e95d5SJeremy L Thompson case CEED_EVAL_NONE: 5604b3e95d5SJeremy L Thompson code << " CeedScalar *r_e" << var_suffix << " = r_q" << var_suffix << ";\n"; 5614b3e95d5SJeremy L Thompson break; // No action 5624b3e95d5SJeremy L Thompson case CEED_EVAL_INTERP: 563e93651e5SJeremy L Thompson code << " CeedScalar *r_e" << var_suffix << " = r_e_scratch;\n"; 5643a2968d6SJeremy L Thompson if (is_at_points) { 5659123fb08SJeremy L Thompson std::string function_name = (dim == 1 ? "InterpTranspose" : "InterpTransposeTensor") + std::to_string(dim) + "d"; 5669123fb08SJeremy L Thompson 5679123fb08SJeremy L Thompson code << " " << function_name << "<num_comp" << var_suffix << ", " << P_name << ", " << Q_name << ">(data, r_c" << var_suffix << ", s_B" 5689123fb08SJeremy L Thompson << var_suffix << ", r_e" << var_suffix << ");\n"; 5693a2968d6SJeremy L Thompson } else { 5709123fb08SJeremy L Thompson std::string function_name = 5719123fb08SJeremy L Thompson is_tensor ? ((dim == 1 ? "InterpTranspose" : "InterpTransposeTensor") + std::to_string(dim) + "d") : "InterpTransposeNonTensor"; 5729123fb08SJeremy L Thompson 5739123fb08SJeremy L Thompson code << " " << function_name << "<num_comp" << var_suffix << ", " << P_name << ", " << Q_name << ">(data, r_q" << var_suffix << ", s_B" 5749123fb08SJeremy L Thompson << var_suffix << ", r_e" << var_suffix << ");\n"; 5753a2968d6SJeremy L Thompson } 5764b3e95d5SJeremy L Thompson break; 5774b3e95d5SJeremy L Thompson case CEED_EVAL_GRAD: 578e93651e5SJeremy L Thompson code << " CeedScalar *r_e" << var_suffix << " = r_e_scratch;\n"; 5793a2968d6SJeremy L Thompson if (is_at_points) { 5809123fb08SJeremy L Thompson std::string function_name = (dim == 1 ? "InterpTranspose" : "InterpTransposeTensor") + std::to_string(dim) + "d"; 5819123fb08SJeremy L Thompson 5829123fb08SJeremy L Thompson code << " " << function_name << "<num_comp" << var_suffix << ", " << P_name << ", " << Q_name << ">(data, r_c" << var_suffix << ", s_B" 5839123fb08SJeremy L Thompson << var_suffix << ", r_e" << var_suffix << ");\n"; 5843a2968d6SJeremy L Thompson } else if (use_3d_slices) { 5859123fb08SJeremy L Thompson std::string function_name = (dim == 1 ? "InterpTranspose" : "InterpTransposeTensor") + std::to_string(dim) + "d"; 5869123fb08SJeremy L Thompson 5879123fb08SJeremy L Thompson code << " " << function_name << "<num_comp" << var_suffix << ", " << P_name << ", " << Q_name << ">(data, r_q" << var_suffix << ", s_B" 5889123fb08SJeremy L Thompson << var_suffix << ", r_e" << var_suffix << ");\n"; 5899123fb08SJeremy L Thompson } else if (is_tensor) { 5909123fb08SJeremy L Thompson bool is_collocated = dim == 3 && Q_1d >= P_1d; 5919123fb08SJeremy L Thompson std::string function_name = 5929123fb08SJeremy L Thompson (dim == 1 ? "GradTranspose" : (is_collocated ? "GradTransposeTensorCollocated" : "GradTransposeTensor")) + std::to_string(dim) + "d"; 5939123fb08SJeremy L Thompson 5949123fb08SJeremy L Thompson code << " " << function_name << "<num_comp" << var_suffix << ", " << P_name << ", " << Q_name << ">(data, r_q" << var_suffix << ", s_B" 5959123fb08SJeremy L Thompson << var_suffix << ", s_G" << var_suffix << ", r_e" << var_suffix << ");\n"; 5964b3e95d5SJeremy L Thompson } else { 5979123fb08SJeremy L Thompson std::string function_name = "GradTransposeNonTensor"; 5989123fb08SJeremy L Thompson 5999123fb08SJeremy L Thompson code << " " << function_name << "<num_comp" << var_suffix << ", dim, " << P_name << ", " << Q_name << ">(data, r_q" << var_suffix 6009123fb08SJeremy L Thompson << ", s_G" << var_suffix << ", r_e" << var_suffix << ");\n"; 6014b3e95d5SJeremy L Thompson } 6024b3e95d5SJeremy L Thompson break; 6034b3e95d5SJeremy L Thompson // LCOV_EXCL_START 6044b3e95d5SJeremy L Thompson case CEED_EVAL_WEIGHT: 6054b3e95d5SJeremy L Thompson break; // Should not occur 6064b3e95d5SJeremy L Thompson case CEED_EVAL_DIV: 6074b3e95d5SJeremy L Thompson case CEED_EVAL_CURL: 6084b3e95d5SJeremy L Thompson break; // TODO: Not implemented 6094b3e95d5SJeremy L Thompson // LCOV_EXCL_STOP 6104b3e95d5SJeremy L Thompson } 6114b3e95d5SJeremy L Thompson } 6123a2968d6SJeremy L Thompson CeedCallBackend(CeedBasisDestroy(&basis)); 6134b3e95d5SJeremy L Thompson return CEED_ERROR_SUCCESS; 6144b3e95d5SJeremy L Thompson } 6154b3e95d5SJeremy L Thompson 6164b3e95d5SJeremy L Thompson //------------------------------------------------------------------------------ 6174b3e95d5SJeremy L Thompson // QFunction 6184b3e95d5SJeremy L Thompson //------------------------------------------------------------------------------ 6193a2968d6SJeremy L Thompson static int CeedOperatorBuildKernelQFunction_Hip_gen(std::ostringstream &code, CeedOperator_Hip_gen *data, CeedInt dim, CeedInt max_num_points, 6203a2968d6SJeremy L Thompson CeedInt num_input_fields, CeedOperatorField *op_input_fields, CeedQFunctionField *qf_input_fields, 6214b3e95d5SJeremy L Thompson CeedInt num_output_fields, CeedOperatorField *op_output_fields, 6229123fb08SJeremy L Thompson CeedQFunctionField *qf_output_fields, std::string qfunction_name, CeedInt Q_1d, bool is_tensor, 6239123fb08SJeremy L Thompson bool is_at_points, bool use_3d_slices) { 6249123fb08SJeremy L Thompson std::string Q_name = is_tensor ? "Q_1d" : "Q"; 6254b3e95d5SJeremy L Thompson CeedEvalMode eval_mode = CEED_EVAL_NONE; 6264b3e95d5SJeremy L Thompson CeedElemRestriction elem_rstr; 6274b3e95d5SJeremy L Thompson 6288b97b69aSJeremy L Thompson // Setup output arrays 6294b3e95d5SJeremy L Thompson code << "\n // -- Output field setup\n"; 6304b3e95d5SJeremy L Thompson for (CeedInt i = 0; i < num_output_fields; i++) { 631*59fa3f92SJeremy L Thompson const char *field_name; 6324b3e95d5SJeremy L Thompson std::string var_suffix = "_out_" + std::to_string(i); 6334b3e95d5SJeremy L Thompson 634*59fa3f92SJeremy L Thompson CeedCallBackend(CeedOperatorFieldGetName(op_output_fields[i], &field_name)); 635*59fa3f92SJeremy L Thompson code << " // ---- Output field " << i << ": " << field_name << "\n"; 6364b3e95d5SJeremy L Thompson CeedCallBackend(CeedQFunctionFieldGetEvalMode(qf_output_fields[i], &eval_mode)); 6373a2968d6SJeremy L Thompson switch (eval_mode) { 6383a2968d6SJeremy L Thompson case CEED_EVAL_NONE: 6393a2968d6SJeremy L Thompson if (is_at_points) { 6403a2968d6SJeremy L Thompson code << " CeedScalar r_q" << var_suffix << "[num_comp" << var_suffix << "];\n"; 6413a2968d6SJeremy L Thompson } else { 6429123fb08SJeremy L Thompson code << " CeedScalar r_q" << var_suffix << "[num_comp" << var_suffix << "*" << (is_tensor && (dim >= 3) ? Q_name : "1") << "];\n"; 6434b3e95d5SJeremy L Thompson } 6443a2968d6SJeremy L Thompson break; 6453a2968d6SJeremy L Thompson case CEED_EVAL_INTERP: 6463a2968d6SJeremy L Thompson if (is_at_points) { 6473a2968d6SJeremy L Thompson // Accumulator for point data 6489123fb08SJeremy L Thompson code << " CeedScalar r_c" << var_suffix << "[num_comp" << var_suffix << "*" << (dim >= 3 ? Q_name : "1") << "];\n"; 6499123fb08SJeremy L Thompson code << " for (CeedInt i = 0; i < num_comp" << var_suffix << "*" << (dim >= 3 ? Q_name : "1") << "; i++) {\n"; 6503a2968d6SJeremy L Thompson code << " r_c" << var_suffix << "[i] = 0.0;\n"; 6513a2968d6SJeremy L Thompson code << " }\n"; 6523a2968d6SJeremy L Thompson } else { 6539123fb08SJeremy L Thompson code << " CeedScalar r_q" << var_suffix << "[num_comp" << var_suffix << "*" << (is_tensor && (dim >= 3) ? Q_name : "1") << "];\n"; 6543a2968d6SJeremy L Thompson } 6553a2968d6SJeremy L Thompson break; 6563a2968d6SJeremy L Thompson case CEED_EVAL_GRAD: 6573a2968d6SJeremy L Thompson if (is_at_points) { 6583a2968d6SJeremy L Thompson // Accumulator for point data 6599123fb08SJeremy L Thompson code << " CeedScalar r_c" << var_suffix << "[num_comp" << var_suffix << "*" << (dim >= 3 ? Q_name : "1") << "*dim];\n"; 6609123fb08SJeremy L Thompson code << " for (CeedInt i = 0; i < num_comp" << var_suffix << "*" << (dim >= 3 ? Q_name : "1") << "; i++) {\n"; 6613a2968d6SJeremy L Thompson code << " r_c" << var_suffix << "[i] = 0.0;\n"; 6623a2968d6SJeremy L Thompson code << " }\n"; 6633a2968d6SJeremy L Thompson } else if (use_3d_slices) { 6644b3e95d5SJeremy L Thompson // Accumulator for gradient slices 6654b3e95d5SJeremy L Thompson code << " CeedScalar r_q" << var_suffix << "[num_comp" << var_suffix << "*" << Q_name << "];\n"; 6664b3e95d5SJeremy L Thompson code << " for (CeedInt i = 0; i < num_comp" << var_suffix << "*" << Q_name << "; i++) {\n"; 6674b3e95d5SJeremy L Thompson code << " r_q" << var_suffix << "[i] = 0.0;\n"; 6684b3e95d5SJeremy L Thompson code << " }\n"; 6694b3e95d5SJeremy L Thompson } else { 6709123fb08SJeremy L Thompson code << " CeedScalar r_q" << var_suffix << "[num_comp" << var_suffix << "*dim*" << (is_tensor && (dim >= 3) ? Q_name : "1") << "];\n"; 6714b3e95d5SJeremy L Thompson } 6723a2968d6SJeremy L Thompson break; 6733a2968d6SJeremy L Thompson case CEED_EVAL_WEIGHT: 6743a2968d6SJeremy L Thompson break; 6753a2968d6SJeremy L Thompson // LCOV_EXCL_START 6763a2968d6SJeremy L Thompson case CEED_EVAL_DIV: 6773a2968d6SJeremy L Thompson case CEED_EVAL_CURL: 6783a2968d6SJeremy L Thompson break; // TODO: Not implemented 6793a2968d6SJeremy L Thompson // LCOV_EXCL_STOP 6804b3e95d5SJeremy L Thompson } 6814b3e95d5SJeremy L Thompson } 6824b3e95d5SJeremy L Thompson 6833a2968d6SJeremy L Thompson if (is_at_points) { 6843a2968d6SJeremy L Thompson // We need to handle batches of points 6853a2968d6SJeremy L Thompson code << "\n // Note: Using batches of points\n"; 6863a2968d6SJeremy L Thompson code << " const CeedInt point_loop_bound = (blockDim.x * blockDim.y) * ceil(1.0 * max_num_points / (blockDim.x * blockDim.y));\n\n"; 6873a2968d6SJeremy L Thompson code << " #pragma unroll\n"; 6883a2968d6SJeremy L Thompson code << " for (CeedInt i = threadIdx.x + threadIdx.y * blockDim.x; i < point_loop_bound; i += blockDim.x * blockDim.y) {\n"; 6893a2968d6SJeremy L Thompson code << " const CeedInt p = i % max_num_points;\n\n"; 6903a2968d6SJeremy L Thompson 6913a2968d6SJeremy L Thompson code << " // -- Coordinates\n"; 6923a2968d6SJeremy L Thompson code << " CeedScalar r_x[dim];\n"; 6933a2968d6SJeremy L Thompson code << " ReadPoint<dim, coords_comp_stride, max_num_points>(data, elem, p, max_num_points, points.indices, points.coords, r_x);\n\n"; 6943a2968d6SJeremy L Thompson 6953a2968d6SJeremy L Thompson code << " // -- Input fields\n"; 6963a2968d6SJeremy L Thompson for (CeedInt i = 0; i < num_input_fields; i++) { 697*59fa3f92SJeremy L Thompson const char *field_name; 6983a2968d6SJeremy L Thompson std::string var_suffix = "_in_" + std::to_string(i); 6993a2968d6SJeremy L Thompson 700*59fa3f92SJeremy L Thompson CeedCallBackend(CeedOperatorFieldGetName(op_input_fields[i], &field_name)); 701*59fa3f92SJeremy L Thompson code << " // ---- Input field " << i << ": " << field_name << "\n"; 7023a2968d6SJeremy L Thompson CeedCallBackend(CeedQFunctionFieldGetEvalMode(qf_input_fields[i], &eval_mode)); 7033a2968d6SJeremy L Thompson // Basis action 7043a2968d6SJeremy L Thompson code << " // EvalMode: " << CeedEvalModes[eval_mode] << "\n"; 7053a2968d6SJeremy L Thompson switch (eval_mode) { 7063a2968d6SJeremy L Thompson case CEED_EVAL_NONE: 7073a2968d6SJeremy L Thompson code << " CeedScalar r_s" << var_suffix << "[num_comp" << var_suffix << "];\n"; 7083a2968d6SJeremy L Thompson code << " ReadPoint<num_comp" << var_suffix << ", comp_stride" << var_suffix 7093a2968d6SJeremy L Thompson << ", max_num_points>(data, elem, p, max_num_points, indices.inputs[" << i << "], d" << var_suffix << ", r_s" << var_suffix << ");\n"; 7103a2968d6SJeremy L Thompson break; 7113a2968d6SJeremy L Thompson case CEED_EVAL_INTERP: 7123a2968d6SJeremy L Thompson code << " CeedScalar r_s" << var_suffix << "[num_comp" << var_suffix << "];\n"; 7133a2968d6SJeremy L Thompson code << " InterpAtPoints" << dim << "d<num_comp" << var_suffix << ", max_num_points, " << Q_name << ">(data, i, r_c" << var_suffix 7143a2968d6SJeremy L Thompson << ", r_x, r_s" << var_suffix << ");\n"; 7153a2968d6SJeremy L Thompson break; 7163a2968d6SJeremy L Thompson case CEED_EVAL_GRAD: 7173a2968d6SJeremy L Thompson code << " CeedScalar r_s" << var_suffix << "[num_comp" << var_suffix << "*dim];\n"; 7183a2968d6SJeremy L Thompson code << " GradAtPoints" << dim << "d<num_comp" << var_suffix << ", max_num_points, " << Q_name << ">(data, i, r_c" << var_suffix 7193a2968d6SJeremy L Thompson << ", r_x, r_s" << var_suffix << ");\n"; 7203a2968d6SJeremy L Thompson break; 7213a2968d6SJeremy L Thompson case CEED_EVAL_WEIGHT: 7223a2968d6SJeremy L Thompson code << " CeedScalar r_s" << var_suffix << "[1];\n"; 7233a2968d6SJeremy L Thompson code << " r_s" << var_suffix << "[0] = 1.0;\n"; 7243a2968d6SJeremy L Thompson break; 7253a2968d6SJeremy L Thompson // LCOV_EXCL_START 7263a2968d6SJeremy L Thompson case CEED_EVAL_DIV: 7273a2968d6SJeremy L Thompson case CEED_EVAL_CURL: 7283a2968d6SJeremy L Thompson break; // TODO: Not implemented 7293a2968d6SJeremy L Thompson // LCOV_EXCL_STOP 7303a2968d6SJeremy L Thompson } 7313a2968d6SJeremy L Thompson } 7323a2968d6SJeremy L Thompson code << "\n // -- Output fields\n"; 7333a2968d6SJeremy L Thompson for (CeedInt i = 0; i < num_output_fields; i++) { 734*59fa3f92SJeremy L Thompson const char *field_name; 7353a2968d6SJeremy L Thompson std::string var_suffix = "_out_" + std::to_string(i); 7363a2968d6SJeremy L Thompson 737*59fa3f92SJeremy L Thompson CeedCallBackend(CeedOperatorFieldGetName(op_output_fields[i], &field_name)); 738*59fa3f92SJeremy L Thompson code << " // ---- Output field " << i << ": " << field_name << "\n"; 7393a2968d6SJeremy L Thompson CeedCallBackend(CeedQFunctionFieldGetEvalMode(qf_output_fields[i], &eval_mode)); 7403a2968d6SJeremy L Thompson // Basis action 7413a2968d6SJeremy L Thompson switch (eval_mode) { 7423a2968d6SJeremy L Thompson case CEED_EVAL_NONE: 7433a2968d6SJeremy L Thompson code << " CeedScalar r_s" << var_suffix << "[num_comp" << var_suffix << "];\n"; 7443a2968d6SJeremy L Thompson break; 7453a2968d6SJeremy L Thompson case CEED_EVAL_INTERP: 7463a2968d6SJeremy L Thompson code << " CeedScalar r_s" << var_suffix << "[num_comp" << var_suffix << "];\n"; 7473a2968d6SJeremy L Thompson break; 7483a2968d6SJeremy L Thompson case CEED_EVAL_GRAD: 7493a2968d6SJeremy L Thompson code << " CeedScalar r_s" << var_suffix << "[num_comp" << var_suffix << "*dim];\n"; 7503a2968d6SJeremy L Thompson break; 7513a2968d6SJeremy L Thompson // LCOV_EXCL_START 7523a2968d6SJeremy L Thompson case CEED_EVAL_WEIGHT: 7533a2968d6SJeremy L Thompson break; // Should not occur 7543a2968d6SJeremy L Thompson case CEED_EVAL_DIV: 7553a2968d6SJeremy L Thompson case CEED_EVAL_CURL: 7563a2968d6SJeremy L Thompson break; // TODO: Not implemented 7573a2968d6SJeremy L Thompson // LCOV_EXCL_STOP 7583a2968d6SJeremy L Thompson } 7593a2968d6SJeremy L Thompson } 7603a2968d6SJeremy L Thompson 7613a2968d6SJeremy L Thompson } else if (use_3d_slices) { 7624b3e95d5SJeremy L Thompson // We treat quadrature points per slice in 3d to save registers 7634b3e95d5SJeremy L Thompson code << "\n // Note: Using planes of 3D elements\n"; 7644b3e95d5SJeremy L Thompson code << " #pragma unroll\n"; 7654b3e95d5SJeremy L Thompson code << " for (CeedInt q = 0; q < " << Q_name << "; q++) {\n"; 7664b3e95d5SJeremy L Thompson code << " // -- Input fields\n"; 7674b3e95d5SJeremy L Thompson for (CeedInt i = 0; i < num_input_fields; i++) { 768*59fa3f92SJeremy L Thompson const char *field_name; 7694b3e95d5SJeremy L Thompson std::string var_suffix = "_in_" + std::to_string(i); 7704b3e95d5SJeremy L Thompson 771*59fa3f92SJeremy L Thompson CeedCallBackend(CeedOperatorFieldGetName(op_input_fields[i], &field_name)); 772*59fa3f92SJeremy L Thompson code << " // ---- Input field " << i << ": " << field_name << "\n"; 7734b3e95d5SJeremy L Thompson CeedCallBackend(CeedQFunctionFieldGetEvalMode(qf_input_fields[i], &eval_mode)); 7744b3e95d5SJeremy L Thompson // Basis action 7754b3e95d5SJeremy L Thompson code << " // EvalMode: " << CeedEvalModes[eval_mode] << "\n"; 7764b3e95d5SJeremy L Thompson switch (eval_mode) { 7774b3e95d5SJeremy L Thompson case CEED_EVAL_NONE: 7784b3e95d5SJeremy L Thompson bool is_strided; 7794b3e95d5SJeremy L Thompson 7804b3e95d5SJeremy L Thompson code << " CeedScalar r_s" << var_suffix << "[num_comp" << var_suffix << "];\n"; 7814b3e95d5SJeremy L Thompson 7824b3e95d5SJeremy L Thompson CeedCallBackend(CeedOperatorFieldGetElemRestriction(op_input_fields[i], &elem_rstr)); 7834b3e95d5SJeremy L Thompson CeedCallBackend(CeedElemRestrictionIsStrided(elem_rstr, &is_strided)); 7844b3e95d5SJeremy L Thompson if (is_strided) { 7854b3e95d5SJeremy L Thompson bool has_backend_strides; 7864b3e95d5SJeremy L Thompson CeedInt num_elem, elem_size; 7874b3e95d5SJeremy L Thompson 7884b3e95d5SJeremy L Thompson CeedCallBackend(CeedElemRestrictionGetElementSize(elem_rstr, &elem_size)); 7894b3e95d5SJeremy L Thompson CeedCallBackend(CeedElemRestrictionHasBackendStrides(elem_rstr, &has_backend_strides)); 7904b3e95d5SJeremy L Thompson CeedCallBackend(CeedElemRestrictionGetNumElements(elem_rstr, &num_elem)); 7914b3e95d5SJeremy L Thompson CeedInt strides[3] = {1, elem_size * num_elem, elem_size}; 7924b3e95d5SJeremy L Thompson 7934b3e95d5SJeremy L Thompson if (!has_backend_strides) { 7944b3e95d5SJeremy L Thompson CeedCallBackend(CeedElemRestrictionGetStrides(elem_rstr, strides)); 7954b3e95d5SJeremy L Thompson } 7964b3e95d5SJeremy L Thompson code << " // Strides: {" << strides[0] << ", " << strides[1] << ", " << strides[2] << "}\n"; 797f815fac9SJeremy L Thompson code << " ReadEVecSliceStrided3d<num_comp" << var_suffix << ", " << Q_name << ", " << strides[0] << ", " << strides[1] << ", " 7984b3e95d5SJeremy L Thompson << strides[2] << ">(data, elem, q, d" << var_suffix << ", r_s" << var_suffix << ");\n"; 7994b3e95d5SJeremy L Thompson } else { 8004b3e95d5SJeremy L Thompson CeedSize l_size = 0; 8014b3e95d5SJeremy L Thompson CeedInt comp_stride; 8024b3e95d5SJeremy L Thompson CeedElemRestriction_Hip *rstr_data; 8034b3e95d5SJeremy L Thompson 8044b3e95d5SJeremy L Thompson CeedCallBackend(CeedElemRestrictionGetLVectorSize(elem_rstr, &l_size)); 8054b3e95d5SJeremy L Thompson code << " const CeedInt l_size" << var_suffix << " = " << l_size << ";\n"; 8064b3e95d5SJeremy L Thompson CeedCallBackend(CeedElemRestrictionGetCompStride(elem_rstr, &comp_stride)); 8074b3e95d5SJeremy L Thompson code << " // CompStride: " << comp_stride << "\n"; 8084b3e95d5SJeremy L Thompson CeedCallBackend(CeedElemRestrictionGetData(elem_rstr, &rstr_data)); 8094b3e95d5SJeremy L Thompson data->indices.inputs[i] = (CeedInt *)rstr_data->d_offsets; 810f815fac9SJeremy L Thompson code << " ReadEVecSliceStandard3d<num_comp" << var_suffix << ", " << comp_stride << ", " << Q_name << ">(data, l_size" << var_suffix 8114b3e95d5SJeremy L Thompson << ", elem, q, indices.inputs[" << i << "], d" << var_suffix << ", r_s" << var_suffix << ");\n"; 8124b3e95d5SJeremy L Thompson } 8139123fb08SJeremy L Thompson CeedCallBackend(CeedElemRestrictionDestroy(&elem_rstr)); 8144b3e95d5SJeremy L Thompson break; 8154b3e95d5SJeremy L Thompson case CEED_EVAL_INTERP: 8164b3e95d5SJeremy L Thompson code << " CeedScalar r_s" << var_suffix << "[num_comp" << var_suffix << "];\n"; 8174b3e95d5SJeremy L Thompson code << " for (CeedInt j = 0; j < num_comp" << var_suffix << "; j++) {\n"; 8184b3e95d5SJeremy L Thompson code << " r_s" << var_suffix << "[j] = r_q" << var_suffix << "[q + j*" << Q_name << "];\n"; 8194b3e95d5SJeremy L Thompson code << " }\n"; 8204b3e95d5SJeremy L Thompson break; 8214b3e95d5SJeremy L Thompson case CEED_EVAL_GRAD: 8224b3e95d5SJeremy L Thompson code << " CeedScalar r_s" << var_suffix << "[num_comp" << var_suffix << "*dim];\n"; 823f815fac9SJeremy L Thompson code << " GradColloSlice3d<num_comp" << var_suffix << ", " << Q_name << ">(data, q, r_q" << var_suffix << ", s_G" << var_suffix 824f815fac9SJeremy L Thompson << ", r_s" << var_suffix << ");\n"; 8254b3e95d5SJeremy L Thompson break; 8264b3e95d5SJeremy L Thompson case CEED_EVAL_WEIGHT: 8274b3e95d5SJeremy L Thompson code << " CeedScalar r_s" << var_suffix << "[1];\n"; 8284b3e95d5SJeremy L Thompson code << " r_s" << var_suffix << "[0] = r_q" << var_suffix << "[q];\n"; 8293a2968d6SJeremy L Thompson break; 8304b3e95d5SJeremy L Thompson // LCOV_EXCL_START 8314b3e95d5SJeremy L Thompson case CEED_EVAL_DIV: 8324b3e95d5SJeremy L Thompson case CEED_EVAL_CURL: 8334b3e95d5SJeremy L Thompson break; // TODO: Not implemented 8344b3e95d5SJeremy L Thompson // LCOV_EXCL_STOP 8354b3e95d5SJeremy L Thompson } 8364b3e95d5SJeremy L Thompson } 8374b3e95d5SJeremy L Thompson code << "\n // -- Output fields\n"; 8384b3e95d5SJeremy L Thompson for (CeedInt i = 0; i < num_output_fields; i++) { 839*59fa3f92SJeremy L Thompson const char *field_name; 8404b3e95d5SJeremy L Thompson std::string var_suffix = "_out_" + std::to_string(i); 8414b3e95d5SJeremy L Thompson 842*59fa3f92SJeremy L Thompson CeedCallBackend(CeedOperatorFieldGetName(op_output_fields[i], &field_name)); 843*59fa3f92SJeremy L Thompson code << " // ---- Output field " << i << ": " << field_name << "\n"; 8444b3e95d5SJeremy L Thompson CeedCallBackend(CeedQFunctionFieldGetEvalMode(qf_output_fields[i], &eval_mode)); 8454b3e95d5SJeremy L Thompson // Basis action 8464b3e95d5SJeremy L Thompson switch (eval_mode) { 8474b3e95d5SJeremy L Thompson case CEED_EVAL_NONE: 8484b3e95d5SJeremy L Thompson code << " CeedScalar r_s" << var_suffix << "[num_comp" << var_suffix << "];\n"; 8493a2968d6SJeremy L Thompson break; 8504b3e95d5SJeremy L Thompson case CEED_EVAL_INTERP: 8514b3e95d5SJeremy L Thompson code << " CeedScalar r_s" << var_suffix << "[num_comp" << var_suffix << "];\n"; 8524b3e95d5SJeremy L Thompson break; 8534b3e95d5SJeremy L Thompson case CEED_EVAL_GRAD: 8544b3e95d5SJeremy L Thompson code << " CeedScalar r_s" << var_suffix << "[num_comp" << var_suffix << "*dim];\n"; 8554b3e95d5SJeremy L Thompson break; 8564b3e95d5SJeremy L Thompson // LCOV_EXCL_START 8574b3e95d5SJeremy L Thompson case CEED_EVAL_WEIGHT: 8584b3e95d5SJeremy L Thompson break; // Should not occur 8594b3e95d5SJeremy L Thompson case CEED_EVAL_DIV: 8604b3e95d5SJeremy L Thompson case CEED_EVAL_CURL: 8614b3e95d5SJeremy L Thompson break; // TODO: Not implemented 8624b3e95d5SJeremy L Thompson // LCOV_EXCL_STOP 8634b3e95d5SJeremy L Thompson } 8644b3e95d5SJeremy L Thompson } 8654b3e95d5SJeremy L Thompson } else { 8664b3e95d5SJeremy L Thompson code << "\n // Note: Using full elements\n"; 8674b3e95d5SJeremy L Thompson code << " {\n"; 8684b3e95d5SJeremy L Thompson code << " // -- Input fields\n"; 8694b3e95d5SJeremy L Thompson for (CeedInt i = 0; i < num_input_fields; i++) { 870*59fa3f92SJeremy L Thompson const char *field_name; 871*59fa3f92SJeremy L Thompson 872*59fa3f92SJeremy L Thompson CeedCallBackend(CeedOperatorFieldGetName(op_input_fields[i], &field_name)); 873*59fa3f92SJeremy L Thompson code << " // ---- Input field " << i << ": " << field_name << "\n"; 8744b3e95d5SJeremy L Thompson code << " CeedScalar *r_s_in_" << i << " = r_q_in_" << i << ";\n"; 8754b3e95d5SJeremy L Thompson } 8764b3e95d5SJeremy L Thompson code << " // -- Output fields\n"; 8774b3e95d5SJeremy L Thompson for (CeedInt i = 0; i < num_output_fields; i++) { 878*59fa3f92SJeremy L Thompson const char *field_name; 879*59fa3f92SJeremy L Thompson 880*59fa3f92SJeremy L Thompson CeedCallBackend(CeedOperatorFieldGetName(op_output_fields[i], &field_name)); 881*59fa3f92SJeremy L Thompson code << " // ---- Output field " << i << ": " << field_name << "\n"; 8824b3e95d5SJeremy L Thompson code << " CeedScalar *r_s_out_" << i << " = r_q_out_" << i << ";\n"; 8834b3e95d5SJeremy L Thompson } 8844b3e95d5SJeremy L Thompson } 8854b3e95d5SJeremy L Thompson 8864b3e95d5SJeremy L Thompson // Input and output buffers 8874b3e95d5SJeremy L Thompson code << "\n // -- QFunction inputs and outputs\n"; 8884b3e95d5SJeremy L Thompson code << " // ---- Inputs\n"; 8894b3e95d5SJeremy L Thompson code << " CeedScalar *inputs[" << CeedIntMax(num_input_fields, 1) << "];\n"; 8904b3e95d5SJeremy L Thompson for (CeedInt i = 0; i < num_input_fields; i++) { 891*59fa3f92SJeremy L Thompson const char *field_name; 892*59fa3f92SJeremy L Thompson 893*59fa3f92SJeremy L Thompson CeedCallBackend(CeedOperatorFieldGetName(op_input_fields[i], &field_name)); 894*59fa3f92SJeremy L Thompson code << " // ------ Input field " << i << ": " << field_name << "\n"; 8954b3e95d5SJeremy L Thompson code << " inputs[" << i << "] = r_s_in_" << i << ";\n"; 8964b3e95d5SJeremy L Thompson } 8974b3e95d5SJeremy L Thompson code << " // ---- Outputs\n"; 8984b3e95d5SJeremy L Thompson code << " CeedScalar *outputs[" << CeedIntMax(num_output_fields, 1) << "];\n"; 8994b3e95d5SJeremy L Thompson for (CeedInt i = 0; i < num_output_fields; i++) { 900*59fa3f92SJeremy L Thompson const char *field_name; 901*59fa3f92SJeremy L Thompson 902*59fa3f92SJeremy L Thompson CeedCallBackend(CeedOperatorFieldGetName(op_output_fields[i], &field_name)); 903*59fa3f92SJeremy L Thompson code << " // ------ Output field " << i << ": " << field_name << "\n"; 9044b3e95d5SJeremy L Thompson code << " outputs[" << i << "] = r_s_out_" << i << ";\n"; 9054b3e95d5SJeremy L Thompson } 9064b3e95d5SJeremy L Thompson 9074b3e95d5SJeremy L Thompson // Apply QFunction 9084b3e95d5SJeremy L Thompson code << "\n // -- Apply QFunction\n"; 9094b3e95d5SJeremy L Thompson code << " " << qfunction_name << "(ctx, "; 9109123fb08SJeremy L Thompson if (dim != 3 || is_at_points || use_3d_slices || !is_tensor) { 9114b3e95d5SJeremy L Thompson code << "1"; 9124b3e95d5SJeremy L Thompson } else { 9139123fb08SJeremy L Thompson code << Q_name; 9144b3e95d5SJeremy L Thompson } 9154b3e95d5SJeremy L Thompson code << ", inputs, outputs);\n"; 9164b3e95d5SJeremy L Thompson 9173a2968d6SJeremy L Thompson if (is_at_points) { 9183a2968d6SJeremy L Thompson // Map back to coefficients 9193a2968d6SJeremy L Thompson code << "\n // -- Output fields\n"; 9203a2968d6SJeremy L Thompson for (CeedInt i = 0; i < num_output_fields; i++) { 921*59fa3f92SJeremy L Thompson const char *field_name; 9223a2968d6SJeremy L Thompson std::string var_suffix = "_out_" + std::to_string(i); 9233a2968d6SJeremy L Thompson std::string P_name = "P_1d" + var_suffix; 9243a2968d6SJeremy L Thompson 925*59fa3f92SJeremy L Thompson CeedCallBackend(CeedOperatorFieldGetName(op_output_fields[i], &field_name)); 926*59fa3f92SJeremy L Thompson code << " // ---- Output field " << i << ": " << field_name << "\n"; 9273a2968d6SJeremy L Thompson CeedCallBackend(CeedQFunctionFieldGetEvalMode(qf_output_fields[i], &eval_mode)); 9283a2968d6SJeremy L Thompson // Basis action 9293a2968d6SJeremy L Thompson code << " // EvalMode: " << CeedEvalModes[eval_mode] << "\n"; 9303a2968d6SJeremy L Thompson switch (eval_mode) { 9313a2968d6SJeremy L Thompson case CEED_EVAL_NONE: { 9323a2968d6SJeremy L Thompson CeedInt comp_stride; 9333a2968d6SJeremy L Thompson CeedElemRestriction elem_rstr; 9343a2968d6SJeremy L Thompson 9353a2968d6SJeremy L Thompson CeedCallBackend(CeedOperatorFieldGetElemRestriction(op_output_fields[i], &elem_rstr)); 9363a2968d6SJeremy L Thompson CeedCallBackend(CeedElemRestrictionGetCompStride(elem_rstr, &comp_stride)); 9373a2968d6SJeremy L Thompson CeedCallBackend(CeedElemRestrictionDestroy(&elem_rstr)); 9383a2968d6SJeremy L Thompson code << " const CeedInt comp_stride" << var_suffix << " = " << comp_stride << ";\n"; 9393a2968d6SJeremy L Thompson code << " WritePoint<num_comp" << var_suffix << ", comp_stride" << var_suffix 9403a2968d6SJeremy L Thompson << ", max_num_points>(data, elem, i, points.num_per_elem[elem], indices.outputs[" << i << "]" 9413a2968d6SJeremy L Thompson << ", r_s" << var_suffix << ", d" << var_suffix << ");\n"; 9423a2968d6SJeremy L Thompson break; 9433a2968d6SJeremy L Thompson } 9443a2968d6SJeremy L Thompson case CEED_EVAL_INTERP: 9453a2968d6SJeremy L Thompson code << " if (i >= points.num_per_elem[elem]) {\n"; 9463a2968d6SJeremy L Thompson code << " for (CeedInt j = 0; j < num_comp" << var_suffix << "; j++) r_s" << var_suffix << "[j] = 0.0;\n"; 9473a2968d6SJeremy L Thompson code << " }\n"; 9483a2968d6SJeremy L Thompson code << " InterpTransposeAtPoints" << dim << "d<num_comp" << var_suffix << ", max_num_points, " << Q_name << ">(data, i, r_s" 9493a2968d6SJeremy L Thompson << var_suffix << ", r_x, r_c" << var_suffix << ");\n"; 9503a2968d6SJeremy L Thompson break; 9513a2968d6SJeremy L Thompson case CEED_EVAL_GRAD: 9523a2968d6SJeremy L Thompson code << " if (i >= points.num_per_elem[elem]) {\n"; 9533a2968d6SJeremy L Thompson code << " for (CeedInt j = 0; j < num_comp" << var_suffix << "*dim; j++) r_s" << var_suffix << "[j] = 0.0;\n"; 9543a2968d6SJeremy L Thompson code << " }\n"; 9553a2968d6SJeremy L Thompson code << " GradTransposeAtPoints" << dim << "d<num_comp" << var_suffix << ", max_num_points, " << Q_name << ">(data, i, r_s" 9563a2968d6SJeremy L Thompson << var_suffix << ", r_x, r_c" << var_suffix << ");\n"; 9573a2968d6SJeremy L Thompson break; 9583a2968d6SJeremy L Thompson // LCOV_EXCL_START 9593a2968d6SJeremy L Thompson case CEED_EVAL_WEIGHT: 9603a2968d6SJeremy L Thompson break; // Should not occur 9613a2968d6SJeremy L Thompson case CEED_EVAL_DIV: 9623a2968d6SJeremy L Thompson case CEED_EVAL_CURL: 9633a2968d6SJeremy L Thompson break; // TODO: Not implemented 9643a2968d6SJeremy L Thompson // LCOV_EXCL_STOP 9653a2968d6SJeremy L Thompson } 9663a2968d6SJeremy L Thompson } 9673a2968d6SJeremy L Thompson } else if (use_3d_slices) { 9684b3e95d5SJeremy L Thompson // Copy or apply transpose grad, if needed 9693a2968d6SJeremy L Thompson code << "\n // -- Output fields\n"; 9704b3e95d5SJeremy L Thompson for (CeedInt i = 0; i < num_output_fields; i++) { 971*59fa3f92SJeremy L Thompson const char *field_name; 9724b3e95d5SJeremy L Thompson std::string var_suffix = "_out_" + std::to_string(i); 9734b3e95d5SJeremy L Thompson std::string P_name = "P_1d" + var_suffix; 9744b3e95d5SJeremy L Thompson 975*59fa3f92SJeremy L Thompson CeedCallBackend(CeedOperatorFieldGetName(op_output_fields[i], &field_name)); 976*59fa3f92SJeremy L Thompson code << " // ---- Output field " << i << ": " << field_name << "\n"; 9774b3e95d5SJeremy L Thompson CeedCallBackend(CeedQFunctionFieldGetEvalMode(qf_output_fields[i], &eval_mode)); 9784b3e95d5SJeremy L Thompson // Basis action 9794b3e95d5SJeremy L Thompson code << " // EvalMode: " << CeedEvalModes[eval_mode] << "\n"; 9804b3e95d5SJeremy L Thompson switch (eval_mode) { 9814b3e95d5SJeremy L Thompson case CEED_EVAL_NONE: 9824b3e95d5SJeremy L Thompson code << " for (CeedInt j = 0; j < num_comp" << var_suffix << " ; j++) {\n"; 9834b3e95d5SJeremy L Thompson code << " r_q" << var_suffix << "[q + j*" << Q_name << "] = r_s" << var_suffix << "[j];\n"; 9844b3e95d5SJeremy L Thompson code << " }\n"; 9853a2968d6SJeremy L Thompson break; 9864b3e95d5SJeremy L Thompson case CEED_EVAL_INTERP: 9874b3e95d5SJeremy L Thompson code << " for (CeedInt j = 0; j < num_comp" << var_suffix << " ; j++) {\n"; 9884b3e95d5SJeremy L Thompson code << " r_q" << var_suffix << "[q + j*" << Q_name << "] = r_s" << var_suffix << "[j];\n"; 9894b3e95d5SJeremy L Thompson code << " }\n"; 9904b3e95d5SJeremy L Thompson break; 9914b3e95d5SJeremy L Thompson case CEED_EVAL_GRAD: 992f815fac9SJeremy L Thompson code << " GradColloSliceTranspose3d<num_comp" << var_suffix << ", " << Q_name << ">(data, q, r_s" << var_suffix << ", s_G" 993f815fac9SJeremy L Thompson << var_suffix << ", r_q" << var_suffix << ");\n"; 9944b3e95d5SJeremy L Thompson break; 9954b3e95d5SJeremy L Thompson // LCOV_EXCL_START 9964b3e95d5SJeremy L Thompson case CEED_EVAL_WEIGHT: 9974b3e95d5SJeremy L Thompson break; // Should not occur 9984b3e95d5SJeremy L Thompson case CEED_EVAL_DIV: 9994b3e95d5SJeremy L Thompson case CEED_EVAL_CURL: 10004b3e95d5SJeremy L Thompson break; // TODO: Not implemented 10014b3e95d5SJeremy L Thompson // LCOV_EXCL_STOP 10024b3e95d5SJeremy L Thompson } 10034b3e95d5SJeremy L Thompson } 10044b3e95d5SJeremy L Thompson } 10054b3e95d5SJeremy L Thompson code << " }\n"; 10064b3e95d5SJeremy L Thompson return CEED_ERROR_SUCCESS; 10074b3e95d5SJeremy L Thompson } 10084b3e95d5SJeremy L Thompson 10094b3e95d5SJeremy L Thompson //------------------------------------------------------------------------------ 10109e201c85SYohann // Build single operator kernel 10117d8d0e25Snbeams //------------------------------------------------------------------------------ 10128d12f40eSJeremy L Thompson extern "C" int CeedOperatorBuildKernel_Hip_gen(CeedOperator op, bool *is_good_build) { 10133a2968d6SJeremy L Thompson bool is_tensor = true, is_at_points = false, use_3d_slices = false; 10147d8d0e25Snbeams Ceed ceed; 10153a2968d6SJeremy L Thompson CeedInt Q_1d, num_input_fields, num_output_fields, dim = 1, max_num_points = 0, coords_comp_stride = 0; 1016b7453713SJeremy L Thompson CeedQFunctionField *qf_input_fields, *qf_output_fields; 1017b7453713SJeremy L Thompson CeedQFunction_Hip_gen *qf_data; 1018b7453713SJeremy L Thompson CeedQFunction qf; 1019b7453713SJeremy L Thompson CeedOperatorField *op_input_fields, *op_output_fields; 1020b7453713SJeremy L Thompson CeedOperator_Hip_gen *data; 10214b3e95d5SJeremy L Thompson std::ostringstream code; 10224b3e95d5SJeremy L Thompson 10238d12f40eSJeremy L Thompson CeedCallBackend(CeedOperatorGetData(op, &data)); 10244b3e95d5SJeremy L Thompson { 10254b3e95d5SJeremy L Thompson bool is_setup_done; 1026b7453713SJeremy L Thompson 1027b7453713SJeremy L Thompson CeedCallBackend(CeedOperatorIsSetupDone(op, &is_setup_done)); 10288d12f40eSJeremy L Thompson if (is_setup_done) { 10298d12f40eSJeremy L Thompson *is_good_build = !data->use_fallback; 10308d12f40eSJeremy L Thompson return CEED_ERROR_SUCCESS; 10318d12f40eSJeremy L Thompson } 10324b3e95d5SJeremy L Thompson } 1033b7453713SJeremy L Thompson 10348d12f40eSJeremy L Thompson // Check field compatibility 10358d12f40eSJeremy L Thompson CeedCallBackend(CeedOperatorGetFields(op, &num_input_fields, &op_input_fields, &num_output_fields, &op_output_fields)); 10368d12f40eSJeremy L Thompson { 10378d12f40eSJeremy L Thompson bool has_shared_bases = true, is_all_tensor = true, is_all_nontensor = true; 10388d12f40eSJeremy L Thompson 10398d12f40eSJeremy L Thompson for (CeedInt i = 0; i < num_input_fields; i++) { 10408d12f40eSJeremy L Thompson CeedBasis basis; 10418d12f40eSJeremy L Thompson 10428d12f40eSJeremy L Thompson CeedCallBackend(CeedOperatorFieldGetBasis(op_input_fields[i], &basis)); 10438d12f40eSJeremy L Thompson if (basis != CEED_BASIS_NONE) { 10448d12f40eSJeremy L Thompson bool is_tensor = true; 10458d12f40eSJeremy L Thompson const char *resource; 10468d12f40eSJeremy L Thompson char *resource_root; 10478d12f40eSJeremy L Thompson Ceed basis_ceed; 10488d12f40eSJeremy L Thompson 10498d12f40eSJeremy L Thompson CeedCallBackend(CeedBasisIsTensor(basis, &is_tensor)); 1050c9192acaSJeremy L Thompson is_all_tensor = is_all_tensor && is_tensor; 1051c9192acaSJeremy L Thompson is_all_nontensor = is_all_nontensor && !is_tensor; 10528d12f40eSJeremy L Thompson CeedCallBackend(CeedBasisGetCeed(basis, &basis_ceed)); 10538d12f40eSJeremy L Thompson CeedCallBackend(CeedGetResource(basis_ceed, &resource)); 10548d12f40eSJeremy L Thompson CeedCallBackend(CeedGetResourceRoot(basis_ceed, resource, ":", &resource_root)); 1055c9192acaSJeremy L Thompson has_shared_bases = has_shared_bases && !strcmp(resource_root, "/gpu/hip/shared"); 10568d12f40eSJeremy L Thompson CeedCallBackend(CeedFree(&resource_root)); 10578d12f40eSJeremy L Thompson CeedCallBackend(CeedDestroy(&basis_ceed)); 10588d12f40eSJeremy L Thompson } 10598d12f40eSJeremy L Thompson CeedCallBackend(CeedBasisDestroy(&basis)); 10608d12f40eSJeremy L Thompson } 10618d12f40eSJeremy L Thompson 10628d12f40eSJeremy L Thompson for (CeedInt i = 0; i < num_output_fields; i++) { 10638d12f40eSJeremy L Thompson CeedBasis basis; 10648d12f40eSJeremy L Thompson 10658d12f40eSJeremy L Thompson CeedCallBackend(CeedOperatorFieldGetBasis(op_output_fields[i], &basis)); 10668d12f40eSJeremy L Thompson if (basis != CEED_BASIS_NONE) { 10678d12f40eSJeremy L Thompson bool is_tensor = true; 10688d12f40eSJeremy L Thompson const char *resource; 10698d12f40eSJeremy L Thompson char *resource_root; 10708d12f40eSJeremy L Thompson Ceed basis_ceed; 10718d12f40eSJeremy L Thompson 10728d12f40eSJeremy L Thompson CeedCallBackend(CeedBasisIsTensor(basis, &is_tensor)); 1073c9192acaSJeremy L Thompson is_all_tensor = is_all_tensor && is_tensor; 1074c9192acaSJeremy L Thompson is_all_nontensor = is_all_nontensor && !is_tensor; 10758d12f40eSJeremy L Thompson 10768d12f40eSJeremy L Thompson CeedCallBackend(CeedBasisGetCeed(basis, &basis_ceed)); 10778d12f40eSJeremy L Thompson CeedCallBackend(CeedGetResource(basis_ceed, &resource)); 10788d12f40eSJeremy L Thompson CeedCallBackend(CeedGetResourceRoot(basis_ceed, resource, ":", &resource_root)); 1079c9192acaSJeremy L Thompson has_shared_bases = has_shared_bases && !strcmp(resource_root, "/gpu/hip/shared"); 10808d12f40eSJeremy L Thompson CeedCallBackend(CeedFree(&resource_root)); 10818d12f40eSJeremy L Thompson CeedCallBackend(CeedDestroy(&basis_ceed)); 10828d12f40eSJeremy L Thompson } 10838d12f40eSJeremy L Thompson CeedCallBackend(CeedBasisDestroy(&basis)); 10848d12f40eSJeremy L Thompson } 10858d12f40eSJeremy L Thompson // -- Fallback to ref if not all bases are shared 10868d12f40eSJeremy L Thompson if (!has_shared_bases || (!is_all_tensor && !is_all_nontensor)) { 10878d12f40eSJeremy L Thompson *is_good_build = false; 10888d12f40eSJeremy L Thompson return CEED_ERROR_SUCCESS; 10898d12f40eSJeremy L Thompson } 10908d12f40eSJeremy L Thompson } 1091b7453713SJeremy L Thompson CeedCallBackend(CeedOperatorGetCeed(op, &ceed)); 1092b7453713SJeremy L Thompson CeedCallBackend(CeedOperatorGetQFunction(op, &qf)); 1093b7453713SJeremy L Thompson CeedCallBackend(CeedQFunctionGetData(qf, &qf_data)); 1094b7453713SJeremy L Thompson CeedCallBackend(CeedQFunctionGetFields(qf, NULL, &qf_input_fields, NULL, &qf_output_fields)); 10957d8d0e25Snbeams 10964b3e95d5SJeremy L Thompson // Get operator data 10973a2968d6SJeremy L Thompson CeedCallBackend(CeedOperatorIsAtPoints(op, &is_at_points)); 10984b3e95d5SJeremy L Thompson CeedCallBackend(CeedOperatorBuildKernelData_Hip_gen(ceed, num_input_fields, op_input_fields, qf_input_fields, num_output_fields, op_output_fields, 10994b3e95d5SJeremy L Thompson qf_output_fields, &data->max_P_1d, &Q_1d, &dim, &is_tensor, &use_3d_slices)); 11004b3e95d5SJeremy L Thompson if (dim == 0) dim = 1; 11014b3e95d5SJeremy L Thompson data->dim = dim; 11023a2968d6SJeremy L Thompson if (is_at_points) { 11033a2968d6SJeremy L Thompson CeedElemRestriction_Hip *rstr_data; 11043a2968d6SJeremy L Thompson CeedElemRestriction rstr_points = NULL; 11054b3e95d5SJeremy L Thompson 11063a2968d6SJeremy L Thompson CeedCallBackend(CeedOperatorAtPointsGetPoints(op, &rstr_points, NULL)); 11073a2968d6SJeremy L Thompson CeedCallBackend(CeedElemRestrictionGetMaxPointsInElement(rstr_points, &max_num_points)); 11083a2968d6SJeremy L Thompson CeedCallBackend(CeedElemRestrictionGetCompStride(rstr_points, &coords_comp_stride)); 11093a2968d6SJeremy L Thompson CeedCallBackend(CeedElemRestrictionGetData(rstr_points, &rstr_data)); 11103a2968d6SJeremy L Thompson data->points.indices = (CeedInt *)rstr_data->d_offsets; 11113a2968d6SJeremy L Thompson CeedCallBackend(CeedElemRestrictionDestroy(&rstr_points)); 11123a2968d6SJeremy L Thompson } 11133a2968d6SJeremy L Thompson if (is_at_points) use_3d_slices = false; 11143a2968d6SJeremy L Thompson if (Q_1d == 0) { 11153a2968d6SJeremy L Thompson if (is_at_points) Q_1d = max_num_points; 11163a2968d6SJeremy L Thompson else CeedCallBackend(CeedOperatorGetNumQuadraturePoints(op, &Q_1d)); 11174b3e95d5SJeremy L Thompson } 11184b3e95d5SJeremy L Thompson data->Q_1d = Q_1d; 11194b3e95d5SJeremy L Thompson 11200b454692Sjeremylt // Check for restriction only identity operator 11214b3e95d5SJeremy L Thompson { 11224b3e95d5SJeremy L Thompson bool is_identity_qf; 11234b3e95d5SJeremy L Thompson 11242b730f8bSJeremy L Thompson CeedCallBackend(CeedQFunctionIsIdentity(qf, &is_identity_qf)); 11250b454692Sjeremylt if (is_identity_qf) { 11269e201c85SYohann CeedEvalMode eval_mode_in, eval_mode_out; 1127b7453713SJeremy L Thompson 11282b730f8bSJeremy L Thompson CeedCallBackend(CeedQFunctionFieldGetEvalMode(qf_input_fields[0], &eval_mode_in)); 11292b730f8bSJeremy L Thompson CeedCallBackend(CeedQFunctionFieldGetEvalMode(qf_output_fields[0], &eval_mode_out)); 11306574a04fSJeremy L Thompson CeedCheck(eval_mode_in != CEED_EVAL_NONE || eval_mode_out != CEED_EVAL_NONE, ceed, CEED_ERROR_BACKEND, 11316574a04fSJeremy L Thompson "Backend does not implement restriction only identity operators"); 11320b454692Sjeremylt } 11334b3e95d5SJeremy L Thompson } 1134b2165e7aSSebastian Grimberg 1135b2165e7aSSebastian Grimberg // Load basis source files 11369123fb08SJeremy L Thompson if (is_tensor) { 11379c25dd66SJeremy L Thompson code << "// Tensor basis source\n"; 11389c25dd66SJeremy L Thompson code << "#include <ceed/jit-source/hip/hip-shared-basis-tensor-templates.h>\n\n"; 11399123fb08SJeremy L Thompson } else { 11409123fb08SJeremy L Thompson code << "// Non-tensor basis source\n"; 11419123fb08SJeremy L Thompson code << "#include <ceed/jit-source/hip/hip-shared-basis-nontensor-templates.h>\n\n"; 11429123fb08SJeremy L Thompson } 11439123fb08SJeremy L Thompson if (is_at_points) { 11443a2968d6SJeremy L Thompson code << "// AtPoints basis source\n"; 11453a2968d6SJeremy L Thompson code << "#include <ceed/jit-source/hip/hip-shared-basis-tensor-at-points-templates.h>\n\n"; 11469123fb08SJeremy L Thompson } 11479c25dd66SJeremy L Thompson code << "// CodeGen operator source\n"; 11489c25dd66SJeremy L Thompson code << "#include <ceed/jit-source/hip/hip-gen-templates.h>\n\n"; 11497d8d0e25Snbeams 11504b3e95d5SJeremy L Thompson // Get QFunction name 11514b3e95d5SJeremy L Thompson std::string qfunction_name(qf_data->qfunction_name); 11524b3e95d5SJeremy L Thompson std::string operator_name; 11534b3e95d5SJeremy L Thompson 115409095acaSJeremy L Thompson operator_name = "CeedKernelHipGenOperator_" + qfunction_name; 11557d8d0e25Snbeams 11569e201c85SYohann // Define CEED_Q_VLA 11579e201c85SYohann code << "\n#undef CEED_Q_VLA\n"; 11589123fb08SJeremy L Thompson if (dim != 3 || is_at_points || use_3d_slices || !is_tensor) { 11599e201c85SYohann code << "#define CEED_Q_VLA 1\n\n"; 11609e201c85SYohann } else { 11619e201c85SYohann code << "#define CEED_Q_VLA " << Q_1d << "\n\n"; 11629e201c85SYohann } 11639e201c85SYohann 11644b3e95d5SJeremy L Thompson // Add user QFunction source 11654b3e95d5SJeremy L Thompson { 11669c25dd66SJeremy L Thompson const char *source_path; 11674b3e95d5SJeremy L Thompson 11689c25dd66SJeremy L Thompson CeedCallBackend(CeedQFunctionGetSourcePath(qf, &source_path)); 11699c25dd66SJeremy L Thompson CeedCheck(source_path, ceed, CEED_ERROR_UNSUPPORTED, "/gpu/hip/gen backend requires QFunction source code file"); 11709c25dd66SJeremy L Thompson 11719c25dd66SJeremy L Thompson code << "// User QFunction source\n"; 11729c25dd66SJeremy L Thompson code << "#include \"" << source_path << "\"\n\n"; 11734b3e95d5SJeremy L Thompson } 11747d8d0e25Snbeams 11757d8d0e25Snbeams // Setup 11767d8d0e25Snbeams code << "\n// -----------------------------------------------------------------------------\n"; 11774b3e95d5SJeremy L Thompson code << "// Operator Kernel\n"; 11784b3e95d5SJeremy L Thompson code << "// \n"; 11794b3e95d5SJeremy L Thompson code << "// d_[in,out]_i: CeedVector device array\n"; 11804b3e95d5SJeremy L Thompson code << "// r_[in,out]_e_i: Element vector register\n"; 11814b3e95d5SJeremy L Thompson code << "// r_[in,out]_q_i: Quadrature space vector register\n"; 11829123fb08SJeremy L Thompson code << "// r_[in,out]_c_i: AtPoints Chebyshev coefficients register\n"; 11834b3e95d5SJeremy L Thompson code << "// r_[in,out]_s_i: Quadrature space slice vector register\n"; 11844b3e95d5SJeremy L Thompson code << "// \n"; 11854b3e95d5SJeremy L Thompson code << "// s_B_[in,out]_i: Interpolation matrix, shared memory\n"; 11864b3e95d5SJeremy L Thompson code << "// s_G_[in,out]_i: Gradient matrix, shared memory\n"; 11874b3e95d5SJeremy L Thompson code << "// -----------------------------------------------------------------------------\n"; 1188b3e1519bSnbeams code << "\nextern \"C\" __launch_bounds__(BLOCK_SIZE)\n"; 11892b730f8bSJeremy L Thompson code << "__global__ void " << operator_name 11903a2968d6SJeremy 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"; 11914b3e95d5SJeremy L Thompson 11924b3e95d5SJeremy L Thompson // Scratch buffers 11939e201c85SYohann for (CeedInt i = 0; i < num_input_fields; i++) { 11944b3e95d5SJeremy L Thompson CeedEvalMode eval_mode; 11954b3e95d5SJeremy L Thompson 11962b730f8bSJeremy L Thompson CeedCallBackend(CeedQFunctionFieldGetEvalMode(qf_input_fields[i], &eval_mode)); 11979e201c85SYohann if (eval_mode != CEED_EVAL_WEIGHT) { // Skip CEED_EVAL_WEIGHT 11984b3e95d5SJeremy L Thompson code << " const CeedScalar *d_in_" << i << " = fields.inputs[" << i << "];\n"; 11997d8d0e25Snbeams } 12007d8d0e25Snbeams } 12019e201c85SYohann for (CeedInt i = 0; i < num_output_fields; i++) { 12024b3e95d5SJeremy L Thompson code << " CeedScalar *d_out_" << i << " = fields.outputs[" << i << "];\n"; 12037d8d0e25Snbeams } 12047d8d0e25Snbeams 12059e201c85SYohann code << " const CeedInt dim = " << dim << ";\n"; 12069123fb08SJeremy L Thompson code << " const CeedInt " << (is_tensor ? "Q_1d" : "Q") << " = " << Q_1d << ";\n"; 12073a2968d6SJeremy L Thompson if (is_at_points) { 12083a2968d6SJeremy L Thompson code << " const CeedInt max_num_points = " << max_num_points << ";\n"; 12093a2968d6SJeremy L Thompson code << " const CeedInt coords_comp_stride = " << coords_comp_stride << ";\n"; 12103a2968d6SJeremy L Thompson } 12117d8d0e25Snbeams 12124b3e95d5SJeremy L Thompson // Shared data 12134b3e95d5SJeremy L Thompson code << " extern __shared__ CeedScalar slice[];\n"; 12149e201c85SYohann code << " SharedData_Hip data;\n"; 12159e201c85SYohann code << " data.t_id_x = threadIdx.x;\n"; 12169e201c85SYohann code << " data.t_id_y = threadIdx.y;\n"; 12179e201c85SYohann code << " data.t_id_z = threadIdx.z;\n"; 12189e201c85SYohann code << " data.t_id = threadIdx.x + threadIdx.y*blockDim.x + threadIdx.z*blockDim.y*blockDim.x;\n"; 12199123fb08SJeremy L Thompson code << " data.slice = slice + data.t_id_z*T_1D" << ((!is_tensor || dim == 1) ? "" : "*T_1D") << ";\n"; 12207d8d0e25Snbeams 12219ee499e5SJeremy L Thompson // -- Determine input mat reuse 122245a787f7SJeremy L Thompson FieldReuse_Hip input_matrix_reuse[CEED_FIELD_MAX]; 12239ee499e5SJeremy L Thompson 12249ee499e5SJeremy L Thompson for (CeedInt i = 0; i < num_input_fields; i++) { 122545a787f7SJeremy L Thompson input_matrix_reuse[i].index = -1; 12269ee499e5SJeremy L Thompson } 12279ee499e5SJeremy L Thompson for (CeedInt i = 0; i < num_input_fields; i++) { 12289ee499e5SJeremy L Thompson CeedEvalMode eval_mode_i; 12299ee499e5SJeremy L Thompson CeedBasis basis_i; 12309ee499e5SJeremy L Thompson 12319ee499e5SJeremy L Thompson CeedCallBackend(CeedQFunctionFieldGetEvalMode(qf_input_fields[i], &eval_mode_i)); 12329ee499e5SJeremy L Thompson if (eval_mode_i == CEED_EVAL_WEIGHT) continue; 12339ee499e5SJeremy L Thompson CeedCallBackend(CeedOperatorFieldGetBasis(op_input_fields[i], &basis_i)); 123445a787f7SJeremy L Thompson for (CeedInt j = 0; (input_matrix_reuse[i].index == -1) && (j < i); j++) { 12359ee499e5SJeremy L Thompson CeedEvalMode eval_mode_j; 12369ee499e5SJeremy L Thompson CeedBasis basis_j; 12379ee499e5SJeremy L Thompson 12389ee499e5SJeremy L Thompson CeedCallBackend(CeedQFunctionFieldGetEvalMode(qf_input_fields[j], &eval_mode_j)); 12399ee499e5SJeremy L Thompson if (eval_mode_j == CEED_EVAL_WEIGHT) continue; 12409ee499e5SJeremy L Thompson CeedCallBackend(CeedOperatorFieldGetBasis(op_input_fields[j], &basis_j)); 12419ee499e5SJeremy L Thompson if (basis_i == basis_j) { 12429ee499e5SJeremy L Thompson if (is_tensor) { 124345a787f7SJeremy L Thompson input_matrix_reuse[i].index = j; 124445a787f7SJeremy L Thompson input_matrix_reuse[i].is_input = true; 124545a787f7SJeremy L Thompson input_matrix_reuse[i].eval_mode = eval_mode_j; 12469ee499e5SJeremy L Thompson } else { 12479ee499e5SJeremy L Thompson // For non-tensor can only re-use with the same eval mode 12489ee499e5SJeremy L Thompson if (eval_mode_i == eval_mode_j) { 124945a787f7SJeremy L Thompson input_matrix_reuse[i].index = j; 125045a787f7SJeremy L Thompson input_matrix_reuse[i].is_input = true; 125145a787f7SJeremy L Thompson input_matrix_reuse[i].eval_mode = eval_mode_j; 12529ee499e5SJeremy L Thompson } 12539ee499e5SJeremy L Thompson } 12549ee499e5SJeremy L Thompson } 12559ee499e5SJeremy L Thompson CeedCallBackend(CeedBasisDestroy(&basis_j)); 12569ee499e5SJeremy L Thompson } 12579ee499e5SJeremy L Thompson CeedCallBackend(CeedBasisDestroy(&basis_i)); 12589ee499e5SJeremy L Thompson } 12599ee499e5SJeremy L Thompson 12609ee499e5SJeremy L Thompson // -- Determine output mat reuse 126145a787f7SJeremy L Thompson FieldReuse_Hip output_matrix_reuse[CEED_FIELD_MAX]; 12629ee499e5SJeremy L Thompson 12639ee499e5SJeremy L Thompson for (CeedInt i = 0; i < num_output_fields; i++) { 126445a787f7SJeremy L Thompson output_matrix_reuse[i].index = -1; 12659ee499e5SJeremy L Thompson } 12669ee499e5SJeremy L Thompson for (CeedInt i = 0; i < num_output_fields; i++) { 12679ee499e5SJeremy L Thompson CeedEvalMode eval_mode_i; 12689ee499e5SJeremy L Thompson CeedBasis basis_i; 12699ee499e5SJeremy L Thompson 12709ee499e5SJeremy L Thompson CeedCallBackend(CeedQFunctionFieldGetEvalMode(qf_output_fields[i], &eval_mode_i)); 12719ee499e5SJeremy L Thompson CeedCallBackend(CeedOperatorFieldGetBasis(op_output_fields[i], &basis_i)); 127245a787f7SJeremy L Thompson for (CeedInt j = 0; (output_matrix_reuse[i].index == -1) && (j < num_input_fields); j++) { 12739ee499e5SJeremy L Thompson CeedEvalMode eval_mode_j; 12749ee499e5SJeremy L Thompson CeedBasis basis_j; 12759ee499e5SJeremy L Thompson 12769ee499e5SJeremy L Thompson CeedCallBackend(CeedQFunctionFieldGetEvalMode(qf_input_fields[j], &eval_mode_j)); 12779ee499e5SJeremy L Thompson if (eval_mode_j == CEED_EVAL_WEIGHT) continue; 12789ee499e5SJeremy L Thompson CeedCallBackend(CeedOperatorFieldGetBasis(op_input_fields[j], &basis_j)); 12799ee499e5SJeremy L Thompson if (basis_i == basis_j) { 12809ee499e5SJeremy L Thompson if (is_tensor) { 128145a787f7SJeremy L Thompson output_matrix_reuse[i].index = j; 128245a787f7SJeremy L Thompson output_matrix_reuse[i].is_input = true; 128345a787f7SJeremy L Thompson output_matrix_reuse[i].eval_mode = eval_mode_j; 12849ee499e5SJeremy L Thompson } else { 12859ee499e5SJeremy L Thompson // For non-tensor can only re-use with the same eval mode 12869ee499e5SJeremy L Thompson if (eval_mode_i == eval_mode_j) { 128745a787f7SJeremy L Thompson output_matrix_reuse[i].index = j; 128845a787f7SJeremy L Thompson output_matrix_reuse[i].is_input = true; 128945a787f7SJeremy L Thompson output_matrix_reuse[i].eval_mode = eval_mode_j; 12909ee499e5SJeremy L Thompson } 12919ee499e5SJeremy L Thompson } 12929ee499e5SJeremy L Thompson } 12939ee499e5SJeremy L Thompson CeedCallBackend(CeedBasisDestroy(&basis_j)); 12949ee499e5SJeremy L Thompson } 129545a787f7SJeremy L Thompson for (CeedInt j = 0; (output_matrix_reuse[i].index == -1) && (j < i); j++) { 12969ee499e5SJeremy L Thompson CeedEvalMode eval_mode_j; 12979ee499e5SJeremy L Thompson CeedBasis basis_j; 12989ee499e5SJeremy L Thompson 12999ee499e5SJeremy L Thompson CeedCallBackend(CeedQFunctionFieldGetEvalMode(qf_output_fields[j], &eval_mode_j)); 13009ee499e5SJeremy L Thompson if (eval_mode_j == CEED_EVAL_WEIGHT) continue; 13019ee499e5SJeremy L Thompson CeedCallBackend(CeedOperatorFieldGetBasis(op_output_fields[j], &basis_j)); 13029ee499e5SJeremy L Thompson if (basis_i == basis_j) { 13039ee499e5SJeremy L Thompson if (is_tensor) { 130445a787f7SJeremy L Thompson output_matrix_reuse[i].index = j; 130545a787f7SJeremy L Thompson output_matrix_reuse[i].is_input = false; 130645a787f7SJeremy L Thompson output_matrix_reuse[i].eval_mode = eval_mode_j; 13079ee499e5SJeremy L Thompson } else { 13089ee499e5SJeremy L Thompson // For non-tensor can only re-use with the same eval mode 13099ee499e5SJeremy L Thompson if (eval_mode_i == eval_mode_j) { 131045a787f7SJeremy L Thompson output_matrix_reuse[i].index = j; 131145a787f7SJeremy L Thompson output_matrix_reuse[i].is_input = false; 131245a787f7SJeremy L Thompson output_matrix_reuse[i].eval_mode = eval_mode_j; 13139ee499e5SJeremy L Thompson } 13149ee499e5SJeremy L Thompson } 13159ee499e5SJeremy L Thompson } 13169ee499e5SJeremy L Thompson CeedCallBackend(CeedBasisDestroy(&basis_j)); 13179ee499e5SJeremy L Thompson } 13189ee499e5SJeremy L Thompson CeedCallBackend(CeedBasisDestroy(&basis_i)); 13199ee499e5SJeremy L Thompson } 13209ee499e5SJeremy L Thompson 13217d8d0e25Snbeams // Initialize constants, and matrices B and G 13224b3e95d5SJeremy L Thompson code << "\n // Input field constants and basis data\n"; 13239e201c85SYohann for (CeedInt i = 0; i < num_input_fields; i++) { 13249ee499e5SJeremy L Thompson CeedCallBackend(CeedOperatorBuildKernelFieldData_Hip_gen(code, data, i, op_input_fields[i], qf_input_fields[i], input_matrix_reuse[i], Q_1d, true, 13259ee499e5SJeremy L Thompson is_tensor, is_at_points, use_3d_slices)); 13267d8d0e25Snbeams } 13274b3e95d5SJeremy L Thompson code << "\n // Output field constants and basis data\n"; 13289e201c85SYohann for (CeedInt i = 0; i < num_output_fields; i++) { 13299ee499e5SJeremy L Thompson CeedCallBackend(CeedOperatorBuildKernelFieldData_Hip_gen(code, data, i, op_output_fields[i], qf_output_fields[i], output_matrix_reuse[i], Q_1d, 13309ee499e5SJeremy L Thompson false, is_tensor, is_at_points, use_3d_slices)); 13314b3e95d5SJeremy L Thompson } 13327d8d0e25Snbeams 13334b3e95d5SJeremy L Thompson // Loop over all elements 13344b3e95d5SJeremy L Thompson code << "\n // Element loop\n"; 13357d8d0e25Snbeams code << " __syncthreads();\n"; 13369e201c85SYohann code << " for (CeedInt elem = blockIdx.x*blockDim.z + threadIdx.z; elem < num_elem; elem += gridDim.x*blockDim.z) {\n"; 13374b3e95d5SJeremy L Thompson 1338e93651e5SJeremy L Thompson // -- Compute minimum buffer space needed 13393a2968d6SJeremy L Thompson CeedInt max_rstr_buffer_size = 1; 1340e93651e5SJeremy L Thompson 1341e93651e5SJeremy L Thompson for (CeedInt i = 0; i < num_input_fields; i++) { 1342e93651e5SJeremy L Thompson CeedInt num_comp, elem_size; 1343e93651e5SJeremy L Thompson CeedElemRestriction elem_rstr; 1344e93651e5SJeremy L Thompson 1345e93651e5SJeremy L Thompson CeedCallBackend(CeedOperatorFieldGetElemRestriction(op_input_fields[i], &elem_rstr)); 1346e93651e5SJeremy L Thompson CeedCallBackend(CeedElemRestrictionGetNumComponents(elem_rstr, &num_comp)); 1347e93651e5SJeremy L Thompson CeedCallBackend(CeedElemRestrictionGetElementSize(elem_rstr, &elem_size)); 13489123fb08SJeremy L Thompson max_rstr_buffer_size = CeedIntMax(max_rstr_buffer_size, num_comp * (is_tensor && (dim >= 3) ? elem_size : 1)); 1349681d0ea7SJeremy L Thompson CeedCallBackend(CeedElemRestrictionDestroy(&elem_rstr)); 1350e93651e5SJeremy L Thompson } 1351e93651e5SJeremy L Thompson for (CeedInt i = 0; i < num_output_fields; i++) { 1352e93651e5SJeremy L Thompson CeedInt num_comp, elem_size; 1353e93651e5SJeremy L Thompson CeedElemRestriction elem_rstr; 1354e93651e5SJeremy L Thompson 1355e93651e5SJeremy L Thompson CeedCallBackend(CeedOperatorFieldGetElemRestriction(op_output_fields[i], &elem_rstr)); 1356e93651e5SJeremy L Thompson CeedCallBackend(CeedElemRestrictionGetNumComponents(elem_rstr, &num_comp)); 1357e93651e5SJeremy L Thompson CeedCallBackend(CeedElemRestrictionGetElementSize(elem_rstr, &elem_size)); 13589123fb08SJeremy L Thompson max_rstr_buffer_size = CeedIntMax(max_rstr_buffer_size, num_comp * (is_tensor && (dim >= 3) ? elem_size : 1)); 1359681d0ea7SJeremy L Thompson CeedCallBackend(CeedElemRestrictionDestroy(&elem_rstr)); 1360e93651e5SJeremy L Thompson } 1361e93651e5SJeremy L Thompson code << " // Scratch restriction buffer space\n"; 1362e93651e5SJeremy L Thompson code << " CeedScalar r_e_scratch[" << max_rstr_buffer_size << "];\n"; 1363e93651e5SJeremy L Thompson 1364e93651e5SJeremy L Thompson // -- Determine best input field processing order 1365e93651e5SJeremy L Thompson CeedInt field_rstr_in_buffer[CEED_FIELD_MAX], input_field_order[CEED_FIELD_MAX]; 1366e93651e5SJeremy L Thompson 1367e93651e5SJeremy L Thompson for (CeedInt i = 0; i < num_input_fields; i++) { 1368e93651e5SJeremy L Thompson field_rstr_in_buffer[i] = -1; 1369e93651e5SJeremy L Thompson input_field_order[i] = -1; 1370e93651e5SJeremy L Thompson } 1371e93651e5SJeremy L Thompson { 1372e93651e5SJeremy L Thompson bool is_ordered[CEED_FIELD_MAX]; 1373e93651e5SJeremy L Thompson CeedInt curr_index = 0; 1374e93651e5SJeremy L Thompson 1375e93651e5SJeremy L Thompson for (CeedInt i = 0; i < num_input_fields; i++) is_ordered[i] = false; 1376e93651e5SJeremy L Thompson for (CeedInt i = 0; i < num_input_fields; i++) { 1377e93651e5SJeremy L Thompson CeedVector vec_i; 1378e93651e5SJeremy L Thompson CeedElemRestriction rstr_i; 1379e93651e5SJeremy L Thompson 1380e93651e5SJeremy L Thompson if (is_ordered[i]) continue; 1381e93651e5SJeremy L Thompson field_rstr_in_buffer[i] = i; 1382e93651e5SJeremy L Thompson is_ordered[i] = true; 1383e93651e5SJeremy L Thompson input_field_order[curr_index] = i; 1384e93651e5SJeremy L Thompson curr_index++; 1385034f99fdSJeremy L Thompson CeedCallBackend(CeedOperatorFieldGetVector(op_input_fields[i], &vec_i)); 1386e93651e5SJeremy L Thompson if (vec_i == CEED_VECTOR_NONE) continue; // CEED_EVAL_WEIGHT 1387e93651e5SJeremy L Thompson CeedCallBackend(CeedOperatorFieldGetElemRestriction(op_input_fields[i], &rstr_i)); 1388e93651e5SJeremy L Thompson for (CeedInt j = i + 1; j < num_input_fields; j++) { 1389e93651e5SJeremy L Thompson CeedVector vec_j; 1390e93651e5SJeremy L Thompson CeedElemRestriction rstr_j; 1391e93651e5SJeremy L Thompson 1392e93651e5SJeremy L Thompson CeedCallBackend(CeedOperatorFieldGetVector(op_input_fields[j], &vec_j)); 1393e93651e5SJeremy L Thompson CeedCallBackend(CeedOperatorFieldGetElemRestriction(op_input_fields[j], &rstr_j)); 1394e93651e5SJeremy L Thompson if (rstr_i == rstr_j && vec_i == vec_j) { 1395e93651e5SJeremy L Thompson field_rstr_in_buffer[j] = i; 1396e93651e5SJeremy L Thompson is_ordered[j] = true; 1397e93651e5SJeremy L Thompson input_field_order[curr_index] = j; 1398e93651e5SJeremy L Thompson curr_index++; 1399e93651e5SJeremy L Thompson } 14003a2968d6SJeremy L Thompson CeedCallBackend(CeedVectorDestroy(&vec_j)); 14013a2968d6SJeremy L Thompson CeedCallBackend(CeedElemRestrictionDestroy(&rstr_j)); 1402e93651e5SJeremy L Thompson } 14033a2968d6SJeremy L Thompson CeedCallBackend(CeedVectorDestroy(&vec_i)); 14043a2968d6SJeremy L Thompson CeedCallBackend(CeedElemRestrictionDestroy(&rstr_i)); 1405e93651e5SJeremy L Thompson } 1406e93651e5SJeremy L Thompson } 1407e93651e5SJeremy L Thompson 14084b3e95d5SJeremy L Thompson // -- Input restriction and basis 14093a2968d6SJeremy L Thompson code << "\n // -- Input field restrictions and basis actions\n"; 14109e201c85SYohann for (CeedInt i = 0; i < num_input_fields; i++) { 1411*59fa3f92SJeremy L Thompson const char *field_name; 1412*59fa3f92SJeremy L Thompson const CeedInt f = input_field_order[i]; 1413e93651e5SJeremy L Thompson 1414*59fa3f92SJeremy L Thompson CeedCallBackend(CeedOperatorFieldGetName(op_input_fields[f], &field_name)); 1415*59fa3f92SJeremy L Thompson code << " // ---- Input field " << f << ": " << field_name << "\n"; 14167d8d0e25Snbeams 14174b3e95d5SJeremy L Thompson // ---- Restriction 1418e93651e5SJeremy L Thompson CeedCallBackend(CeedOperatorBuildKernelRestriction_Hip_gen(code, data, f, dim, field_rstr_in_buffer, op_input_fields[f], qf_input_fields[f], Q_1d, 14199123fb08SJeremy L Thompson true, is_tensor, is_at_points, use_3d_slices)); 1420b7453713SJeremy L Thompson 14214b3e95d5SJeremy L Thompson // ---- Basis action 14229123fb08SJeremy L Thompson CeedCallBackend(CeedOperatorBuildKernelBasis_Hip_gen(code, data, f, dim, op_input_fields[f], qf_input_fields[f], Q_1d, true, is_tensor, 14239123fb08SJeremy L Thompson is_at_points, use_3d_slices)); 14247d8d0e25Snbeams } 14257d8d0e25Snbeams 14264b3e95d5SJeremy L Thompson // -- Q function 14273a2968d6SJeremy L Thompson CeedCallBackend(CeedOperatorBuildKernelQFunction_Hip_gen(code, data, dim, max_num_points, num_input_fields, op_input_fields, qf_input_fields, 14289123fb08SJeremy L Thompson num_output_fields, op_output_fields, qf_output_fields, qfunction_name, Q_1d, is_tensor, 14299123fb08SJeremy L Thompson is_at_points, use_3d_slices)); 14307d8d0e25Snbeams 14314b3e95d5SJeremy L Thompson // -- Output basis and restriction 14324b3e95d5SJeremy L Thompson code << "\n // -- Output field basis action and restrictions\n"; 14339e201c85SYohann for (CeedInt i = 0; i < num_output_fields; i++) { 1434*59fa3f92SJeremy L Thompson const char *field_name; 1435*59fa3f92SJeremy L Thompson 1436*59fa3f92SJeremy L Thompson CeedCallBackend(CeedOperatorFieldGetName(op_output_fields[i], &field_name)); 1437*59fa3f92SJeremy L Thompson code << " // ---- Output field " << i << ": " << field_name << "\n"; 1438b7453713SJeremy L Thompson 14394b3e95d5SJeremy L Thompson // ---- Basis action 14409123fb08SJeremy L Thompson CeedCallBackend(CeedOperatorBuildKernelBasis_Hip_gen(code, data, i, dim, op_output_fields[i], qf_output_fields[i], Q_1d, false, is_tensor, 14419123fb08SJeremy L Thompson is_at_points, use_3d_slices)); 14427d8d0e25Snbeams 14434b3e95d5SJeremy L Thompson // ---- Restriction 14443a2968d6SJeremy L Thompson CeedCallBackend(CeedOperatorBuildKernelRestriction_Hip_gen(code, data, i, dim, NULL, op_output_fields[i], qf_output_fields[i], Q_1d, false, 14459123fb08SJeremy L Thompson is_tensor, is_at_points, use_3d_slices)); 14467d8d0e25Snbeams } 14477d8d0e25Snbeams 14484b3e95d5SJeremy L Thompson // Close loop and function 14497d8d0e25Snbeams code << " }\n"; 14507d8d0e25Snbeams code << "}\n"; 14517d8d0e25Snbeams code << "// -----------------------------------------------------------------------------\n\n"; 14527d8d0e25Snbeams 1453539ec17dSJeremy L Thompson CeedInt block_sizes[3] = {0, 0, 0}; 14549e201c85SYohann CeedInt num_elem; 1455b7453713SJeremy L Thompson 14563a2968d6SJeremy L Thompson // Compile 14572b730f8bSJeremy L Thompson CeedCallBackend(CeedOperatorGetNumElements(op, &num_elem)); 14589123fb08SJeremy L Thompson CeedCallBackend(BlockGridCalculate_Hip_gen(is_tensor ? dim : 1, num_elem, data->max_P_1d, Q_1d, block_sizes)); 14598d12f40eSJeremy L Thompson { 14608d12f40eSJeremy L Thompson bool is_compile_good = false; 14618d12f40eSJeremy L Thompson 14628d12f40eSJeremy L Thompson CeedCallBackend(CeedTryCompile_Hip(ceed, code.str().c_str(), &is_compile_good, &data->module, 2, "T_1D", block_sizes[0], "BLOCK_SIZE", 14632b730f8bSJeremy L Thompson block_sizes[0] * block_sizes[1] * block_sizes[2])); 14648d12f40eSJeremy L Thompson if (is_compile_good) { 14658d12f40eSJeremy L Thompson *is_good_build = true; 1466eb7e6cafSJeremy L Thompson CeedCallBackend(CeedGetKernel_Hip(ceed, data->module, operator_name.c_str(), &data->op)); 14678d12f40eSJeremy L Thompson } else { 14688d12f40eSJeremy L Thompson *is_good_build = false; 14698d12f40eSJeremy L Thompson data->use_fallback = true; 14708d12f40eSJeremy L Thompson } 14718d12f40eSJeremy L Thompson } 14722b730f8bSJeremy L Thompson CeedCallBackend(CeedOperatorSetSetupDone(op)); 14739bc66399SJeremy L Thompson CeedCallBackend(CeedDestroy(&ceed)); 1474c11e12f4SJeremy L Thompson CeedCallBackend(CeedQFunctionDestroy(&qf)); 1475e15f9bd0SJeremy L Thompson return CEED_ERROR_SUCCESS; 14767d8d0e25Snbeams } 14772a86cc9dSSebastian Grimberg 14787d8d0e25Snbeams //------------------------------------------------------------------------------ 1479