15aed82e4SJeremy L Thompson // Copyright (c) 2017-2024, Lawrence Livermore National Security, LLC and other CEED contributors. 23d8e8822SJeremy L Thompson // All Rights Reserved. See the top-level LICENSE and NOTICE files for details. 3241a4b83SYohann // 43d8e8822SJeremy L Thompson // SPDX-License-Identifier: BSD-2-Clause 5241a4b83SYohann // 63d8e8822SJeremy L Thompson // This file is part of CEED: http://github.com/ceed 73d576824SJeremy L Thompson 8b8e71988SJeremy L Thompson #define CEED_DEBUG_COLOR 12 97df94212SJeremy L Thompson 1049aac155SJeremy L Thompson #include <ceed.h> 11ec3da8bcSJed Brown #include <ceed/backend.h> 129e201c85SYohann #include <ceed/jit-tools.h> 133d576824SJeremy L Thompson #include <cuda_runtime.h> 142b730f8bSJeremy L Thompson 15241a4b83SYohann #include <iostream> 16241a4b83SYohann #include <sstream> 17b2165e7aSSebastian Grimberg #include <string> 182b730f8bSJeremy L Thompson 190d0321e0SJeremy L Thompson #include "../cuda-ref/ceed-cuda-ref.h" 20241a4b83SYohann #include "../cuda-shared/ceed-cuda-shared.h" 2149aac155SJeremy L Thompson #include "../cuda/ceed-cuda-common.h" 222b730f8bSJeremy L Thompson #include "../cuda/ceed-cuda-compile.h" 232b730f8bSJeremy L Thompson #include "ceed-cuda-gen.h" 24241a4b83SYohann 2545a787f7SJeremy L Thompson struct FieldReuse_Cuda { 2645a787f7SJeremy L Thompson CeedInt index; 2745a787f7SJeremy L Thompson bool is_input; 2845a787f7SJeremy L Thompson CeedEvalMode eval_mode; 2945a787f7SJeremy L Thompson }; 3045a787f7SJeremy L Thompson 31ab213215SJeremy L Thompson //------------------------------------------------------------------------------ 324b3e95d5SJeremy L Thompson // Determine type of operator 334b3e95d5SJeremy L Thompson //------------------------------------------------------------------------------ 344b3e95d5SJeremy L Thompson static int CeedOperatorBuildKernelData_Cuda_gen(Ceed ceed, CeedInt num_input_fields, CeedOperatorField *op_input_fields, 354b3e95d5SJeremy L Thompson CeedQFunctionField *qf_input_fields, CeedInt num_output_fields, CeedOperatorField *op_output_fields, 36412e5683SJeremy L Thompson CeedQFunctionField *qf_output_fields, CeedInt *max_P, CeedInt *max_P_1d, CeedInt *Q, CeedInt *Q_1d, 37412e5683SJeremy L Thompson CeedInt *max_dim, bool *is_all_tensor, bool *use_3d_slices) { 38412e5683SJeremy L Thompson // Check if all are tensor 39412e5683SJeremy L Thompson *is_all_tensor = true; 404b3e95d5SJeremy L Thompson for (CeedInt i = 0; i < num_input_fields; i++) { 414b3e95d5SJeremy L Thompson CeedBasis basis; 424b3e95d5SJeremy L Thompson 434b3e95d5SJeremy L Thompson CeedCallBackend(CeedOperatorFieldGetBasis(op_input_fields[i], &basis)); 444b3e95d5SJeremy L Thompson if (basis != CEED_BASIS_NONE) { 454b3e95d5SJeremy L Thompson bool is_field_tensor; 464b3e95d5SJeremy L Thompson 474b3e95d5SJeremy L Thompson CeedCallBackend(CeedBasisIsTensor(basis, &is_field_tensor)); 48412e5683SJeremy L Thompson *is_all_tensor = *is_all_tensor && is_field_tensor; 494b3e95d5SJeremy L Thompson } 50681d0ea7SJeremy L Thompson CeedCallBackend(CeedBasisDestroy(&basis)); 514b3e95d5SJeremy L Thompson } 524b3e95d5SJeremy L Thompson for (CeedInt i = 0; i < num_output_fields; i++) { 534b3e95d5SJeremy L Thompson CeedBasis basis; 544b3e95d5SJeremy L Thompson 554b3e95d5SJeremy L Thompson CeedCallBackend(CeedOperatorFieldGetBasis(op_output_fields[i], &basis)); 564b3e95d5SJeremy L Thompson if (basis != CEED_BASIS_NONE) { 574b3e95d5SJeremy L Thompson bool is_field_tensor; 584b3e95d5SJeremy L Thompson 594b3e95d5SJeremy L Thompson CeedCallBackend(CeedBasisIsTensor(basis, &is_field_tensor)); 60412e5683SJeremy L Thompson *is_all_tensor = *is_all_tensor && is_field_tensor; 61412e5683SJeremy L Thompson } 62412e5683SJeremy L Thompson CeedCallBackend(CeedBasisDestroy(&basis)); 63412e5683SJeremy L Thompson } 64412e5683SJeremy L Thompson 65412e5683SJeremy L Thompson // Find max_P, max_P_1d, Q, and Q_1d 66412e5683SJeremy L Thompson bool is_all_3d = true; 67412e5683SJeremy L Thompson 68412e5683SJeremy L Thompson *max_P = 0; 69412e5683SJeremy L Thompson *max_P_1d = 0; 70412e5683SJeremy L Thompson *Q = 0; 71412e5683SJeremy L Thompson *Q_1d = 0; 72412e5683SJeremy L Thompson for (CeedInt i = 0; i < num_input_fields; i++) { 73412e5683SJeremy L Thompson CeedBasis basis; 74412e5683SJeremy L Thompson 75412e5683SJeremy L Thompson CeedCallBackend(CeedOperatorFieldGetBasis(op_input_fields[i], &basis)); 76412e5683SJeremy L Thompson if (basis != CEED_BASIS_NONE) { 77412e5683SJeremy L Thompson bool is_field_tensor; 78412e5683SJeremy L Thompson CeedInt field_dim = 0, field_P = 0, field_P_1d = 0, field_Q = 0, field_Q_1d = 0; 79412e5683SJeremy L Thompson 80412e5683SJeremy L Thompson // Check if 3D 814b3e95d5SJeremy L Thompson CeedCallBackend(CeedBasisGetDimension(basis, &field_dim)); 82412e5683SJeremy L Thompson is_all_3d = is_all_3d && (field_dim == 3); 83412e5683SJeremy L Thompson *max_dim = CeedIntMax(*max_dim, field_dim); 84412e5683SJeremy L Thompson 85412e5683SJeremy L Thompson // Collect P, P_1d, Q, and Q_1d 86412e5683SJeremy L Thompson CeedCallBackend(CeedBasisGetNumNodes(basis, &field_P)); 87412e5683SJeremy L Thompson *max_P = CeedIntMax(*max_P, field_P); 88412e5683SJeremy L Thompson CeedCallBackend(CeedBasisIsTensor(basis, &is_field_tensor)); 89412e5683SJeremy L Thompson if (is_field_tensor) { 90412e5683SJeremy L Thompson CeedCallBackend(CeedBasisGetNumNodes1D(basis, &field_P_1d)); 91412e5683SJeremy L Thompson *max_P_1d = CeedIntMax(*max_P_1d, field_P_1d); 92412e5683SJeremy L Thompson } 93412e5683SJeremy L Thompson CeedCallBackend(CeedBasisGetNumQuadraturePoints(basis, &field_Q)); 94412e5683SJeremy L Thompson CeedCheck(*Q == 0 || field_Q == *Q, ceed, CEED_ERROR_BACKEND, "Quadrature spaces must be compatible"); 95412e5683SJeremy L Thompson *Q = field_Q; 96412e5683SJeremy L Thompson if (is_field_tensor) { 97412e5683SJeremy L Thompson CeedCallBackend(CeedBasisGetNumQuadraturePoints1D(basis, &field_Q_1d)); 984b3e95d5SJeremy L Thompson CeedCheck(*Q_1d == 0 || field_Q_1d == *Q_1d, ceed, CEED_ERROR_BACKEND, "Quadrature spaces must be compatible"); 994b3e95d5SJeremy L Thompson *Q_1d = field_Q_1d; 1004b3e95d5SJeremy L Thompson } 101412e5683SJeremy L Thompson } 102412e5683SJeremy L Thompson CeedCallBackend(CeedBasisDestroy(&basis)); 103412e5683SJeremy L Thompson } 104412e5683SJeremy L Thompson for (CeedInt i = 0; i < num_output_fields; i++) { 105412e5683SJeremy L Thompson CeedBasis basis; 106412e5683SJeremy L Thompson 107412e5683SJeremy L Thompson CeedCallBackend(CeedOperatorFieldGetBasis(op_output_fields[i], &basis)); 108412e5683SJeremy L Thompson if (basis != CEED_BASIS_NONE) { 109412e5683SJeremy L Thompson bool is_field_tensor; 110412e5683SJeremy L Thompson CeedInt field_dim = 0, field_P = 0, field_P_1d = 0, field_Q = 0, field_Q_1d = 0; 111412e5683SJeremy L Thompson 112412e5683SJeremy L Thompson // Check if 3D 113412e5683SJeremy L Thompson CeedCallBackend(CeedBasisGetDimension(basis, &field_dim)); 114412e5683SJeremy L Thompson is_all_3d = is_all_3d && (field_dim == 3); 115412e5683SJeremy L Thompson *max_dim = CeedIntMax(*max_dim, field_dim); 116412e5683SJeremy L Thompson 117412e5683SJeremy L Thompson // Collect P, P_1d, Q, and Q_1d 118412e5683SJeremy L Thompson CeedCallBackend(CeedBasisGetNumNodes(basis, &field_P)); 119412e5683SJeremy L Thompson *max_P = CeedIntMax(*max_P, field_P); 120412e5683SJeremy L Thompson CeedCallBackend(CeedBasisIsTensor(basis, &is_field_tensor)); 121412e5683SJeremy L Thompson if (is_field_tensor) { 122412e5683SJeremy L Thompson CeedCallBackend(CeedBasisGetNumNodes1D(basis, &field_P_1d)); 123412e5683SJeremy L Thompson *max_P_1d = CeedIntMax(*max_P_1d, field_P_1d); 124412e5683SJeremy L Thompson } 125412e5683SJeremy L Thompson CeedCallBackend(CeedBasisGetNumQuadraturePoints(basis, &field_Q)); 126412e5683SJeremy L Thompson CeedCheck(*Q == 0 || field_Q == *Q, ceed, CEED_ERROR_BACKEND, "Quadrature spaces must be compatible"); 127412e5683SJeremy L Thompson *Q = field_Q; 128412e5683SJeremy L Thompson if (is_field_tensor) { 129412e5683SJeremy L Thompson CeedCallBackend(CeedBasisGetNumQuadraturePoints1D(basis, &field_Q_1d)); 130412e5683SJeremy L Thompson CeedCheck(*Q_1d == 0 || field_Q_1d == *Q_1d, ceed, CEED_ERROR_BACKEND, "Quadrature spaces must be compatible"); 131412e5683SJeremy L Thompson *Q_1d = field_Q_1d; 132412e5683SJeremy L Thompson } 133412e5683SJeremy L Thompson } 134681d0ea7SJeremy L Thompson CeedCallBackend(CeedBasisDestroy(&basis)); 1354b3e95d5SJeremy L Thompson } 1364b3e95d5SJeremy L Thompson 1374b3e95d5SJeremy L Thompson // Only use 3D collocated gradient parallelization strategy when gradient is computed 1384b3e95d5SJeremy L Thompson *use_3d_slices = false; 139412e5683SJeremy L Thompson if (is_all_3d && *is_all_tensor) { 1404b3e95d5SJeremy L Thompson bool was_grad_found = false; 1414b3e95d5SJeremy L Thompson 1424b3e95d5SJeremy L Thompson for (CeedInt i = 0; i < num_input_fields; i++) { 1434b3e95d5SJeremy L Thompson CeedEvalMode eval_mode; 1444b3e95d5SJeremy L Thompson 1454b3e95d5SJeremy L Thompson CeedCallBackend(CeedQFunctionFieldGetEvalMode(qf_input_fields[i], &eval_mode)); 1464b3e95d5SJeremy L Thompson if (eval_mode == CEED_EVAL_GRAD) { 1474b3e95d5SJeremy L Thompson CeedBasis_Cuda_shared *basis_data; 1484b3e95d5SJeremy L Thompson CeedBasis basis; 1494b3e95d5SJeremy L Thompson 1504b3e95d5SJeremy L Thompson CeedCallBackend(CeedOperatorFieldGetBasis(op_input_fields[i], &basis)); 1514b3e95d5SJeremy L Thompson CeedCallBackend(CeedBasisGetData(basis, &basis_data)); 1524b3e95d5SJeremy L Thompson *use_3d_slices = basis_data->d_collo_grad_1d && (was_grad_found ? *use_3d_slices : true); 1534b3e95d5SJeremy L Thompson was_grad_found = true; 154681d0ea7SJeremy L Thompson CeedCallBackend(CeedBasisDestroy(&basis)); 1554b3e95d5SJeremy L Thompson } 1564b3e95d5SJeremy L Thompson } 1574b3e95d5SJeremy L Thompson for (CeedInt i = 0; i < num_output_fields; i++) { 1584b3e95d5SJeremy L Thompson CeedEvalMode eval_mode; 1594b3e95d5SJeremy L Thompson 1604b3e95d5SJeremy L Thompson CeedCallBackend(CeedQFunctionFieldGetEvalMode(qf_output_fields[i], &eval_mode)); 1614b3e95d5SJeremy L Thompson if (eval_mode == CEED_EVAL_GRAD) { 1624b3e95d5SJeremy L Thompson CeedBasis_Cuda_shared *basis_data; 1634b3e95d5SJeremy L Thompson CeedBasis basis; 1644b3e95d5SJeremy L Thompson 1654b3e95d5SJeremy L Thompson CeedCallBackend(CeedOperatorFieldGetBasis(op_output_fields[i], &basis)); 1664b3e95d5SJeremy L Thompson CeedCallBackend(CeedBasisGetData(basis, &basis_data)); 1674b3e95d5SJeremy L Thompson *use_3d_slices = basis_data->d_collo_grad_1d && (was_grad_found ? *use_3d_slices : true); 1684b3e95d5SJeremy L Thompson was_grad_found = true; 169681d0ea7SJeremy L Thompson CeedCallBackend(CeedBasisDestroy(&basis)); 1704b3e95d5SJeremy L Thompson } 1714b3e95d5SJeremy L Thompson } 1724b3e95d5SJeremy L Thompson } 1734b3e95d5SJeremy L Thompson return CEED_ERROR_SUCCESS; 1744b3e95d5SJeremy L Thompson } 1754b3e95d5SJeremy L Thompson 1764b3e95d5SJeremy L Thompson //------------------------------------------------------------------------------ 1774b3e95d5SJeremy L Thompson // Setup fields 1784b3e95d5SJeremy L Thompson //------------------------------------------------------------------------------ 1794b3e95d5SJeremy L Thompson static int CeedOperatorBuildKernelFieldData_Cuda_gen(std::ostringstream &code, CeedOperator_Cuda_gen *data, CeedInt i, CeedOperatorField op_field, 1808014c5e7SJeremy L Thompson CeedQFunctionField qf_field, FieldReuse_Cuda field_reuse, CeedInt max_dim, CeedInt Q, 1818014c5e7SJeremy L Thompson CeedInt Q_1d, bool is_input, bool is_all_tensor, bool is_at_points, bool use_3d_slices) { 182412e5683SJeremy L Thompson bool is_tensor = true; 183412e5683SJeremy L Thompson CeedBasis basis; 184412e5683SJeremy L Thompson CeedCallBackend(CeedOperatorFieldGetBasis(op_field, &basis)); 185412e5683SJeremy L Thompson if (basis != CEED_BASIS_NONE) CeedCallBackend(CeedBasisIsTensor(basis, &is_tensor)); 186412e5683SJeremy L Thompson 18759fa3f92SJeremy L Thompson const char *field_name; 1884b3e95d5SJeremy L Thompson std::string var_suffix = (is_input ? "_in_" : "_out_") + std::to_string(i); 189dc007f05SJeremy L Thompson std::string P_name = (is_tensor ? "P_1d" : "P") + var_suffix, Q_name = is_tensor ? "Q_1d" : "Q"; 1904b3e95d5SJeremy L Thompson std::string option_name = (is_input ? "inputs" : "outputs"); 1914b3e95d5SJeremy L Thompson CeedEvalMode eval_mode = CEED_EVAL_NONE; 1928014c5e7SJeremy L Thompson CeedInt elem_size = 0, num_comp = 0, dim = max_dim, P_1d = 0; 1934b3e95d5SJeremy L Thompson CeedElemRestriction elem_rstr; 1944b3e95d5SJeremy L Thompson CeedBasis_Cuda_shared *basis_data; 1954b3e95d5SJeremy L Thompson 1960a2a6492SJeremy L Thompson // Field reuse info 19745a787f7SJeremy L Thompson bool use_previous_field = field_reuse.index != -1; 1980a2a6492SJeremy L Thompson 19959fa3f92SJeremy L Thompson CeedCallBackend(CeedOperatorFieldGetName(op_field, &field_name)); 20059fa3f92SJeremy L Thompson code << " // -- " << (is_input ? "Input" : "Output") << " field " << i << ": " << field_name << "\n"; 2014b3e95d5SJeremy L Thompson 2024b3e95d5SJeremy L Thompson // Get field data 2034b3e95d5SJeremy L Thompson CeedCallBackend(CeedOperatorFieldGetElemRestriction(op_field, &elem_rstr)); 2044b3e95d5SJeremy L Thompson if (elem_rstr != CEED_ELEMRESTRICTION_NONE) { 2054b3e95d5SJeremy L Thompson CeedCallBackend(CeedElemRestrictionGetElementSize(elem_rstr, &elem_size)); 2064b3e95d5SJeremy L Thompson CeedCallBackend(CeedElemRestrictionGetNumComponents(elem_rstr, &num_comp)); 2074b3e95d5SJeremy L Thompson } 208681d0ea7SJeremy L Thompson CeedCallBackend(CeedElemRestrictionDestroy(&elem_rstr)); 2094b3e95d5SJeremy L Thompson if (basis != CEED_BASIS_NONE) { 2104b3e95d5SJeremy L Thompson CeedCallBackend(CeedBasisGetData(basis, &basis_data)); 211412e5683SJeremy L Thompson CeedCallBackend(CeedBasisGetDimension(basis, &dim)); 212dc007f05SJeremy L Thompson if (is_tensor) CeedCallBackend(CeedBasisGetNumNodes1D(basis, &P_1d)); 213dc007f05SJeremy L Thompson else CeedCallBackend(CeedBasisGetNumNodes(basis, &P_1d)); 2144b3e95d5SJeremy L Thompson } 2154b3e95d5SJeremy L Thompson CeedCallBackend(CeedQFunctionFieldGetEvalMode(qf_field, &eval_mode)); 2164b3e95d5SJeremy L Thompson 2174b3e95d5SJeremy L Thompson // Set field constants 218412e5683SJeremy L Thompson code << " const CeedInt dim" << var_suffix << " = " << dim << ";\n"; 219412e5683SJeremy L Thompson if (is_tensor && !is_all_tensor) { 220412e5683SJeremy L Thompson CeedInt P = 0; 221412e5683SJeremy L Thompson 222412e5683SJeremy L Thompson CeedCallBackend(CeedBasisGetNumNodes(basis, &P)); 223c433aabcSJeremy L Thompson code << " const CeedInt P" << var_suffix << " = " << (basis == CEED_BASIS_NONE ? Q : P) << ";\n"; 224412e5683SJeremy L Thompson } 2254b3e95d5SJeremy L Thompson code << " const CeedInt " << P_name << " = " << (basis == CEED_BASIS_NONE ? Q_1d : P_1d) << ";\n"; 226343e3094SJeremy L Thompson if (eval_mode != CEED_EVAL_WEIGHT) { 2274b3e95d5SJeremy L Thompson code << " const CeedInt num_comp" << var_suffix << " = " << num_comp << ";\n"; 2284b3e95d5SJeremy L Thompson } 2294b3e95d5SJeremy L Thompson 2304b3e95d5SJeremy L Thompson // Load basis data 2314b3e95d5SJeremy L Thompson code << " // EvalMode: " << CeedEvalModes[eval_mode] << "\n"; 2324b3e95d5SJeremy L Thompson switch (eval_mode) { 2334b3e95d5SJeremy L Thompson case CEED_EVAL_NONE: 2344b3e95d5SJeremy L Thompson break; 2354b3e95d5SJeremy L Thompson case CEED_EVAL_INTERP: 2368b97b69aSJeremy L Thompson if (is_at_points) { 2378b97b69aSJeremy L Thompson // AtPoints 2388b97b69aSJeremy L Thompson if (!basis_data->d_chebyshev_interp_1d) { 2398b97b69aSJeremy L Thompson CeedSize interp_bytes; 2408b97b69aSJeremy L Thompson CeedScalar *chebyshev_interp_1d; 2418b97b69aSJeremy L Thompson 2428b97b69aSJeremy L Thompson interp_bytes = P_1d * Q_1d * sizeof(CeedScalar); 2438b97b69aSJeremy L Thompson CeedCallBackend(CeedCalloc(P_1d * Q_1d, &chebyshev_interp_1d)); 2448b97b69aSJeremy L Thompson CeedCallBackend(CeedBasisGetChebyshevInterp1D(basis, chebyshev_interp_1d)); 2458b97b69aSJeremy L Thompson CeedCallCuda(CeedBasisReturnCeed(basis), cudaMalloc((void **)&basis_data->d_chebyshev_interp_1d, interp_bytes)); 2468b97b69aSJeremy L Thompson CeedCallCuda(CeedBasisReturnCeed(basis), 2478b97b69aSJeremy L Thompson cudaMemcpy(basis_data->d_chebyshev_interp_1d, chebyshev_interp_1d, interp_bytes, cudaMemcpyHostToDevice)); 2488b97b69aSJeremy L Thompson CeedCallBackend(CeedFree(&chebyshev_interp_1d)); 2498b97b69aSJeremy L Thompson } 2508b97b69aSJeremy L Thompson if (is_input) data->B.inputs[i] = basis_data->d_chebyshev_interp_1d; 2518b97b69aSJeremy L Thompson else data->B.outputs[i] = basis_data->d_chebyshev_interp_1d; 2528b97b69aSJeremy L Thompson } else { 2538b97b69aSJeremy L Thompson // Standard quadrature 2544b3e95d5SJeremy L Thompson if (is_input) data->B.inputs[i] = basis_data->d_interp_1d; 2554b3e95d5SJeremy L Thompson else data->B.outputs[i] = basis_data->d_interp_1d; 2568b97b69aSJeremy L Thompson } 2570a2a6492SJeremy L Thompson if (use_previous_field) { 25845a787f7SJeremy L Thompson std::string reuse_var = "s_B" + ((field_reuse.is_input ? "_in_" : "_out_") + std::to_string(field_reuse.index)); 2590a2a6492SJeremy L Thompson 2600a2a6492SJeremy L Thompson code << " CeedScalar *s_B" << var_suffix << " = " << reuse_var << ";\n"; 2610a2a6492SJeremy L Thompson } else { 262dc007f05SJeremy L Thompson code << " __shared__ CeedScalar s_B" << var_suffix << "[" << P_name << "*" << Q_name << "];\n"; 263f815fac9SJeremy L Thompson code << " LoadMatrix<" << P_name << ", " << Q_name << ">(data, B." << option_name << "[" << i << "], s_B" << var_suffix << ");\n"; 2640a2a6492SJeremy L Thompson } 2654b3e95d5SJeremy L Thompson break; 2664b3e95d5SJeremy L Thompson case CEED_EVAL_GRAD: 2678b97b69aSJeremy L Thompson if (is_at_points) { 2688b97b69aSJeremy L Thompson // AtPoints 2698b97b69aSJeremy L Thompson if (!basis_data->d_chebyshev_interp_1d) { 2708b97b69aSJeremy L Thompson CeedSize interp_bytes; 2718b97b69aSJeremy L Thompson CeedScalar *chebyshev_interp_1d; 2728b97b69aSJeremy L Thompson 2738b97b69aSJeremy L Thompson interp_bytes = P_1d * Q_1d * sizeof(CeedScalar); 2748b97b69aSJeremy L Thompson CeedCallBackend(CeedCalloc(P_1d * Q_1d, &chebyshev_interp_1d)); 2758b97b69aSJeremy L Thompson CeedCallBackend(CeedBasisGetChebyshevInterp1D(basis, chebyshev_interp_1d)); 2768b97b69aSJeremy L Thompson CeedCallCuda(CeedBasisReturnCeed(basis), cudaMalloc((void **)&basis_data->d_chebyshev_interp_1d, interp_bytes)); 2778b97b69aSJeremy L Thompson CeedCallCuda(CeedBasisReturnCeed(basis), 2788b97b69aSJeremy L Thompson cudaMemcpy(basis_data->d_chebyshev_interp_1d, chebyshev_interp_1d, interp_bytes, cudaMemcpyHostToDevice)); 2798b97b69aSJeremy L Thompson CeedCallBackend(CeedFree(&chebyshev_interp_1d)); 2808b97b69aSJeremy L Thompson } 2818b97b69aSJeremy L Thompson if (is_input) data->B.inputs[i] = basis_data->d_chebyshev_interp_1d; 2828b97b69aSJeremy L Thompson else data->B.outputs[i] = basis_data->d_chebyshev_interp_1d; 2838b97b69aSJeremy L Thompson } else { 2848b97b69aSJeremy L Thompson // Standard quadrature 2854b3e95d5SJeremy L Thompson if (is_input) data->B.inputs[i] = basis_data->d_interp_1d; 2864b3e95d5SJeremy L Thompson else data->B.outputs[i] = basis_data->d_interp_1d; 2878b97b69aSJeremy L Thompson } 288dc007f05SJeremy L Thompson if (is_tensor) { 2890a2a6492SJeremy L Thompson if (use_previous_field) { 29045a787f7SJeremy L Thompson std::string reuse_var = "s_B" + ((field_reuse.is_input ? "_in_" : "_out_") + std::to_string(field_reuse.index)); 2910a2a6492SJeremy L Thompson 2920a2a6492SJeremy L Thompson code << " CeedScalar *s_B" << var_suffix << " = " << reuse_var << ";\n"; 2930a2a6492SJeremy L Thompson } else { 294dc007f05SJeremy L Thompson code << " __shared__ CeedScalar s_B" << var_suffix << "[" << P_name << "*" << Q_name << "];\n"; 295f815fac9SJeremy L Thompson code << " LoadMatrix<" << P_name << ", " << Q_name << ">(data, B." << option_name << "[" << i << "], s_B" << var_suffix << ");\n"; 296dc007f05SJeremy L Thompson } 2970a2a6492SJeremy L Thompson } 2988b97b69aSJeremy L Thompson if (is_at_points) break; // No G mat for AtPoints 2994b3e95d5SJeremy L Thompson if (use_3d_slices) { 3004b3e95d5SJeremy L Thompson if (is_input) data->G.inputs[i] = basis_data->d_collo_grad_1d; 3014b3e95d5SJeremy L Thompson else data->G.outputs[i] = basis_data->d_collo_grad_1d; 30245a787f7SJeremy L Thompson if (use_previous_field && field_reuse.eval_mode == CEED_EVAL_GRAD) { 30345a787f7SJeremy L Thompson std::string reuse_var = "s_G" + ((field_reuse.is_input ? "_in_" : "_out_") + std::to_string(field_reuse.index)); 3040a2a6492SJeremy L Thompson 3050a2a6492SJeremy L Thompson code << " CeedScalar *s_G" << var_suffix << " = " << reuse_var << ";\n"; 3060a2a6492SJeremy L Thompson } else { 307dc007f05SJeremy L Thompson code << " __shared__ CeedScalar s_G" << var_suffix << "[" << Q_name << "*" << Q_name << "];\n"; 308f815fac9SJeremy L Thompson code << " LoadMatrix<" << Q_name << ", " << Q_name << ">(data, G." << option_name << "[" << i << "], s_G" << var_suffix << ");\n"; 3090a2a6492SJeremy L Thompson } 3104b3e95d5SJeremy L Thompson } else { 3114b3e95d5SJeremy L Thompson bool has_collo_grad = basis_data->d_collo_grad_1d; 3124b3e95d5SJeremy L Thompson 3134b3e95d5SJeremy L Thompson if (is_input) data->G.inputs[i] = has_collo_grad ? basis_data->d_collo_grad_1d : basis_data->d_grad_1d; 3144b3e95d5SJeremy L Thompson else data->G.outputs[i] = has_collo_grad ? basis_data->d_collo_grad_1d : basis_data->d_grad_1d; 3154b3e95d5SJeremy L Thompson if (has_collo_grad) { 31645a787f7SJeremy L Thompson if (use_previous_field && field_reuse.eval_mode == CEED_EVAL_GRAD) { 31745a787f7SJeremy L Thompson std::string reuse_var = "s_G" + ((field_reuse.is_input ? "_in_" : "_out_") + std::to_string(field_reuse.index)); 3180a2a6492SJeremy L Thompson 3190a2a6492SJeremy L Thompson code << " CeedScalar *s_G" << var_suffix << " = " << reuse_var << ";\n"; 3200a2a6492SJeremy L Thompson } else { 321dc007f05SJeremy L Thompson code << " __shared__ CeedScalar s_G" << var_suffix << "[" << Q_name << "*" << Q_name << "];\n"; 322f815fac9SJeremy L Thompson code << " LoadMatrix<" << Q_name << ", " << Q_name << ">(data, G." << option_name << "[" << i << "], s_G" << var_suffix << ");\n"; 3230a2a6492SJeremy L Thompson } 3240a2a6492SJeremy L Thompson } else { 32545a787f7SJeremy L Thompson if (use_previous_field && field_reuse.eval_mode == CEED_EVAL_GRAD) { 32645a787f7SJeremy L Thompson std::string reuse_var = "s_G" + ((field_reuse.is_input ? "_in_" : "_out_") + std::to_string(field_reuse.index)); 3270a2a6492SJeremy L Thompson 3280a2a6492SJeremy L Thompson code << " CeedScalar *s_G" << var_suffix << " = " << reuse_var << ";\n"; 3294b3e95d5SJeremy L Thompson } else { 330412e5683SJeremy L Thompson code << " __shared__ CeedScalar s_G" << var_suffix << "[" << P_name << "*" << Q_name << (is_tensor ? "" : "*dim") 331412e5683SJeremy L Thompson << (is_tensor ? "" : var_suffix) << "];\n"; 332412e5683SJeremy L Thompson code << " LoadMatrix<" << P_name << ", " << Q_name << (is_tensor ? "" : "*dim") << (is_tensor ? "" : var_suffix) << ">(data, G." 333412e5683SJeremy L Thompson << option_name << "[" << i << "], s_G" << var_suffix << ");\n"; 3344b3e95d5SJeremy L Thompson } 3354b3e95d5SJeremy L Thompson } 3360a2a6492SJeremy L Thompson } 3374b3e95d5SJeremy L Thompson break; 3384b3e95d5SJeremy L Thompson case CEED_EVAL_WEIGHT: 3394b3e95d5SJeremy L Thompson break; // No action 3404b3e95d5SJeremy L Thompson // LCOV_EXCL_START 3414b3e95d5SJeremy L Thompson case CEED_EVAL_DIV: 3424b3e95d5SJeremy L Thompson case CEED_EVAL_CURL: 3434b3e95d5SJeremy L Thompson break; // TODO: Not implemented 3444b3e95d5SJeremy L Thompson // LCOV_EXCL_STOP 3454b3e95d5SJeremy L Thompson } 3468b97b69aSJeremy L Thompson CeedCallBackend(CeedBasisDestroy(&basis)); 3474b3e95d5SJeremy L Thompson return CEED_ERROR_SUCCESS; 3484b3e95d5SJeremy L Thompson } 3494b3e95d5SJeremy L Thompson 3504b3e95d5SJeremy L Thompson //------------------------------------------------------------------------------ 3514b3e95d5SJeremy L Thompson // Restriction 3524b3e95d5SJeremy L Thompson //------------------------------------------------------------------------------ 3538014c5e7SJeremy L Thompson static int CeedOperatorBuildKernelRestriction_Cuda_gen(std::ostringstream &code, CeedOperator_Cuda_gen *data, CeedInt i, CeedInt field_input_buffer[], 3548014c5e7SJeremy L Thompson CeedOperatorField op_field, CeedQFunctionField qf_field, CeedInt max_dim, CeedInt Q_1d, 3558014c5e7SJeremy L Thompson bool is_input, bool is_all_tensor, bool is_at_points, bool use_3d_slices) { 3564b3e95d5SJeremy L Thompson std::string var_suffix = (is_input ? "_in_" : "_out_") + std::to_string(i); 357412e5683SJeremy L Thompson std::string P_name = (is_all_tensor ? "P_1d" : "P") + var_suffix; 3584b3e95d5SJeremy L Thompson CeedEvalMode eval_mode = CEED_EVAL_NONE; 359412e5683SJeremy L Thompson CeedInt elem_size = 0, num_comp = 0; 3604b3e95d5SJeremy L Thompson CeedSize l_size; 361f815fac9SJeremy L Thompson CeedRestrictionType rstr_type = CEED_RESTRICTION_STANDARD; 3624b3e95d5SJeremy L Thompson CeedElemRestriction_Cuda *rstr_data; 3634b3e95d5SJeremy L Thompson CeedElemRestriction elem_rstr; 3644b3e95d5SJeremy L Thompson 3654b3e95d5SJeremy L Thompson // Get field data 3664b3e95d5SJeremy L Thompson CeedCallBackend(CeedOperatorFieldGetElemRestriction(op_field, &elem_rstr)); 3674b3e95d5SJeremy L Thompson if (elem_rstr != CEED_ELEMRESTRICTION_NONE) { 368f815fac9SJeremy L Thompson CeedCallBackend(CeedElemRestrictionGetType(elem_rstr, &rstr_type)); 3694b3e95d5SJeremy L Thompson CeedCallBackend(CeedElemRestrictionGetElementSize(elem_rstr, &elem_size)); 3704b3e95d5SJeremy L Thompson CeedCallBackend(CeedElemRestrictionGetNumComponents(elem_rstr, &num_comp)); 3714b3e95d5SJeremy L Thompson CeedCallBackend(CeedElemRestrictionGetData(elem_rstr, &rstr_data)); 3724b3e95d5SJeremy L Thompson } 3734b3e95d5SJeremy L Thompson CeedCallBackend(CeedQFunctionFieldGetEvalMode(qf_field, &eval_mode)); 3744b3e95d5SJeremy L Thompson 3754b3e95d5SJeremy L Thompson // Restriction 3764b3e95d5SJeremy L Thompson if (is_input) { 3774b3e95d5SJeremy L Thompson // Input 378e93651e5SJeremy L Thompson if (field_input_buffer[i] != i) { 379e93651e5SJeremy L Thompson std::string buffer_name = "r_e_in_" + std::to_string(field_input_buffer[i]); 380e93651e5SJeremy L Thompson 381e93651e5SJeremy L Thompson // Restriction was already done for previous input 382e93651e5SJeremy L Thompson code << " CeedScalar *r_e" << var_suffix << " = " << buffer_name << ";\n"; 3838b97b69aSJeremy L Thompson } else if (eval_mode != CEED_EVAL_WEIGHT && !((eval_mode == CEED_EVAL_NONE) && use_3d_slices && is_at_points)) { 3848b97b69aSJeremy L Thompson if (eval_mode == CEED_EVAL_NONE && rstr_type != CEED_RESTRICTION_POINTS) { 385e93651e5SJeremy L Thompson // No basis action, so r_e_in_* in also r_q_in_* and needs to be allocated 3864b3e95d5SJeremy L Thompson code << " CeedScalar r_e" << var_suffix << "[num_comp" << var_suffix << "*" << P_name << "];\n"; 3878b97b69aSJeremy L Thompson } else if (rstr_type != CEED_RESTRICTION_POINTS) { 388e93651e5SJeremy L Thompson // Otherwise we're using the scratch space 389e93651e5SJeremy L Thompson code << " CeedScalar *r_e" << var_suffix << " = r_e_scratch;\n"; 390e93651e5SJeremy L Thompson } 391f815fac9SJeremy L Thompson switch (rstr_type) { 392f815fac9SJeremy L Thompson case CEED_RESTRICTION_STANDARD: { 3934b3e95d5SJeremy L Thompson CeedInt comp_stride; 3944b3e95d5SJeremy L Thompson 3954b3e95d5SJeremy L Thompson CeedCallBackend(CeedElemRestrictionGetLVectorSize(elem_rstr, &l_size)); 3964b3e95d5SJeremy L Thompson code << " const CeedInt l_size" << var_suffix << " = " << l_size << ";\n"; 3974b3e95d5SJeremy L Thompson CeedCallBackend(CeedElemRestrictionGetCompStride(elem_rstr, &comp_stride)); 3984b3e95d5SJeremy L Thompson code << " // CompStride: " << comp_stride << "\n"; 3994b3e95d5SJeremy L Thompson data->indices.inputs[i] = (CeedInt *)rstr_data->d_offsets; 400412e5683SJeremy L Thompson code << " ReadLVecStandard" << (is_all_tensor ? max_dim : 1) << "d<num_comp" << var_suffix << ", " << comp_stride << ", " << P_name 401dc007f05SJeremy L Thompson << ">(data, l_size" << var_suffix << ", elem, indices.inputs[" << i << "], d" << var_suffix << ", r_e" << var_suffix << ");\n"; 402f815fac9SJeremy L Thompson break; 403f815fac9SJeremy L Thompson } 404f815fac9SJeremy L Thompson case CEED_RESTRICTION_STRIDED: { 4054b3e95d5SJeremy L Thompson bool has_backend_strides; 4064b3e95d5SJeremy L Thompson CeedInt num_elem; 4074b3e95d5SJeremy L Thompson 4084b3e95d5SJeremy L Thompson CeedCallBackend(CeedElemRestrictionHasBackendStrides(elem_rstr, &has_backend_strides)); 4094b3e95d5SJeremy L Thompson CeedCallBackend(CeedElemRestrictionGetNumElements(elem_rstr, &num_elem)); 4104b3e95d5SJeremy L Thompson CeedInt strides[3] = {1, elem_size * num_elem, elem_size}; 4114b3e95d5SJeremy L Thompson 4124b3e95d5SJeremy L Thompson if (!has_backend_strides) { 4134b3e95d5SJeremy L Thompson CeedCallBackend(CeedElemRestrictionGetStrides(elem_rstr, strides)); 4144b3e95d5SJeremy L Thompson } 4154b3e95d5SJeremy L Thompson code << " // Strides: {" << strides[0] << ", " << strides[1] << ", " << strides[2] << "}\n"; 416412e5683SJeremy L Thompson code << " ReadLVecStrided" << (is_all_tensor ? max_dim : 1) << "d<num_comp" << var_suffix << ", " << P_name << ", " << strides[0] << ", " 417dc007f05SJeremy L Thompson << strides[1] << ", " << strides[2] << ">(data, elem, d" << var_suffix << ", r_e" << var_suffix << ");\n"; 418f815fac9SJeremy L Thompson break; 419f815fac9SJeremy L Thompson } 4208b97b69aSJeremy L Thompson case CEED_RESTRICTION_POINTS: { 4218b97b69aSJeremy L Thompson CeedInt comp_stride; 4228b97b69aSJeremy L Thompson 4238b97b69aSJeremy L Thompson CeedCallBackend(CeedElemRestrictionGetCompStride(elem_rstr, &comp_stride)); 4248b97b69aSJeremy L Thompson code << " const CeedInt comp_stride" << var_suffix << " = " << comp_stride << ";\n"; 4258b97b69aSJeremy L Thompson data->indices.inputs[i] = (CeedInt *)rstr_data->d_offsets; 4268b97b69aSJeremy L Thompson break; 4278b97b69aSJeremy L Thompson } 428f815fac9SJeremy L Thompson // LCOV_EXCL_START 429f815fac9SJeremy L Thompson case CEED_RESTRICTION_ORIENTED: 430f815fac9SJeremy L Thompson case CEED_RESTRICTION_CURL_ORIENTED: 431f815fac9SJeremy L Thompson break; // TODO: Not implemented 432f815fac9SJeremy L Thompson // LCOV_EXCL_STOP 4334b3e95d5SJeremy L Thompson } 4344b3e95d5SJeremy L Thompson } 4354b3e95d5SJeremy L Thompson } else { 4364b3e95d5SJeremy L Thompson // Output 437f815fac9SJeremy L Thompson switch (rstr_type) { 438f815fac9SJeremy L Thompson case CEED_RESTRICTION_STANDARD: { 4394b3e95d5SJeremy L Thompson CeedInt comp_stride; 4404b3e95d5SJeremy L Thompson 4414b3e95d5SJeremy L Thompson CeedCallBackend(CeedElemRestrictionGetLVectorSize(elem_rstr, &l_size)); 4424b3e95d5SJeremy L Thompson code << " const CeedInt l_size" << var_suffix << " = " << l_size << ";\n"; 4434b3e95d5SJeremy L Thompson CeedCallBackend(CeedElemRestrictionGetCompStride(elem_rstr, &comp_stride)); 4444b3e95d5SJeremy L Thompson code << " // CompStride: " << comp_stride << "\n"; 4454b3e95d5SJeremy L Thompson data->indices.outputs[i] = (CeedInt *)rstr_data->d_offsets; 446412e5683SJeremy L Thompson code << " WriteLVecStandard" << (is_all_tensor ? max_dim : 1) << "d<num_comp" << var_suffix << ", " << comp_stride << ", " << P_name 447dc007f05SJeremy L Thompson << ">(data, l_size" << var_suffix << ", elem, indices.outputs[" << i << "], r_e" << var_suffix << ", d" << var_suffix << ");\n"; 448f815fac9SJeremy L Thompson break; 449f815fac9SJeremy L Thompson } 450f815fac9SJeremy L Thompson case CEED_RESTRICTION_STRIDED: { 4514b3e95d5SJeremy L Thompson bool has_backend_strides; 4524b3e95d5SJeremy L Thompson CeedInt num_elem; 4534b3e95d5SJeremy L Thompson 4544b3e95d5SJeremy L Thompson CeedCallBackend(CeedElemRestrictionHasBackendStrides(elem_rstr, &has_backend_strides)); 4554b3e95d5SJeremy L Thompson CeedCallBackend(CeedElemRestrictionGetNumElements(elem_rstr, &num_elem)); 4564b3e95d5SJeremy L Thompson CeedInt strides[3] = {1, elem_size * num_elem, elem_size}; 4574b3e95d5SJeremy L Thompson 4584b3e95d5SJeremy L Thompson if (!has_backend_strides) { 4594b3e95d5SJeremy L Thompson CeedCallBackend(CeedElemRestrictionGetStrides(elem_rstr, strides)); 4604b3e95d5SJeremy L Thompson } 4614b3e95d5SJeremy L Thompson code << " // Strides: {" << strides[0] << ", " << strides[1] << ", " << strides[2] << "}\n"; 462412e5683SJeremy L Thompson code << " WriteLVecStrided" << (is_all_tensor ? max_dim : 1) << "d<num_comp" << var_suffix << ", " << P_name << ", " << strides[0] << ", " 463dc007f05SJeremy L Thompson << strides[1] << ", " << strides[2] << ">(data, elem, r_e" << var_suffix << ", d" << var_suffix << ");\n"; 464f815fac9SJeremy L Thompson break; 465f815fac9SJeremy L Thompson } 4668b97b69aSJeremy L Thompson case CEED_RESTRICTION_POINTS: 4678b97b69aSJeremy L Thompson data->indices.outputs[i] = (CeedInt *)rstr_data->d_offsets; 4688b97b69aSJeremy L Thompson break; 469f815fac9SJeremy L Thompson // LCOV_EXCL_START 470f815fac9SJeremy L Thompson case CEED_RESTRICTION_ORIENTED: 471f815fac9SJeremy L Thompson case CEED_RESTRICTION_CURL_ORIENTED: 472f815fac9SJeremy L Thompson break; // TODO: Not implemented 473f815fac9SJeremy L Thompson // LCOV_EXCL_STOP 4744b3e95d5SJeremy L Thompson } 4754b3e95d5SJeremy L Thompson } 476681d0ea7SJeremy L Thompson CeedCallBackend(CeedElemRestrictionDestroy(&elem_rstr)); 4774b3e95d5SJeremy L Thompson return CEED_ERROR_SUCCESS; 4784b3e95d5SJeremy L Thompson } 4794b3e95d5SJeremy L Thompson 4804b3e95d5SJeremy L Thompson //------------------------------------------------------------------------------ 4814b3e95d5SJeremy L Thompson // Basis 4824b3e95d5SJeremy L Thompson //------------------------------------------------------------------------------ 4838014c5e7SJeremy L Thompson static int CeedOperatorBuildKernelBasis_Cuda_gen(std::ostringstream &code, CeedOperator_Cuda_gen *data, CeedInt i, CeedOperatorField op_field, 4848014c5e7SJeremy L Thompson CeedQFunctionField qf_field, CeedInt max_dim, CeedInt Q_1d, bool is_input, bool is_all_tensor, 4858014c5e7SJeremy L Thompson bool is_at_points, bool use_3d_slices) { 486412e5683SJeremy L Thompson bool is_tensor = true; 487412e5683SJeremy L Thompson CeedBasis basis; 488412e5683SJeremy L Thompson CeedCallBackend(CeedOperatorFieldGetBasis(op_field, &basis)); 489412e5683SJeremy L Thompson CeedCallBackend(CeedBasisIsTensor(basis, &is_tensor)); 490412e5683SJeremy L Thompson 4914b3e95d5SJeremy L Thompson std::string var_suffix = (is_input ? "_in_" : "_out_") + std::to_string(i); 492dc007f05SJeremy L Thompson std::string P_name = (is_tensor ? "P_1d" : "P") + var_suffix, Q_name = is_tensor ? "Q_1d" : "Q"; 4934b3e95d5SJeremy L Thompson CeedEvalMode eval_mode = CEED_EVAL_NONE; 494412e5683SJeremy L Thompson CeedInt dim = max_dim, elem_size = 0, num_comp = 0, P_1d = 0; 4954b3e95d5SJeremy L Thompson CeedElemRestriction elem_rstr; 4964b3e95d5SJeremy L Thompson 4974b3e95d5SJeremy L Thompson // Get field data 4984b3e95d5SJeremy L Thompson CeedCallBackend(CeedOperatorFieldGetElemRestriction(op_field, &elem_rstr)); 4994b3e95d5SJeremy L Thompson if (elem_rstr != CEED_ELEMRESTRICTION_NONE) { 5004b3e95d5SJeremy L Thompson CeedCallBackend(CeedElemRestrictionGetElementSize(elem_rstr, &elem_size)); 5014b3e95d5SJeremy L Thompson CeedCallBackend(CeedElemRestrictionGetNumComponents(elem_rstr, &num_comp)); 5024b3e95d5SJeremy L Thompson } 503681d0ea7SJeremy L Thompson CeedCallBackend(CeedElemRestrictionDestroy(&elem_rstr)); 5044b3e95d5SJeremy L Thompson if (basis != CEED_BASIS_NONE) { 505412e5683SJeremy L Thompson CeedCallBackend(CeedBasisGetDimension(basis, &dim)); 506dc007f05SJeremy L Thompson if (is_tensor) CeedCallBackend(CeedBasisGetNumNodes1D(basis, &P_1d)); 507dc007f05SJeremy L Thompson else CeedCallBackend(CeedBasisGetNumNodes(basis, &P_1d)); 5084b3e95d5SJeremy L Thompson } 5094b3e95d5SJeremy L Thompson CeedCallBackend(CeedQFunctionFieldGetEvalMode(qf_field, &eval_mode)); 5104b3e95d5SJeremy L Thompson 5114b3e95d5SJeremy L Thompson // Basis 5124b3e95d5SJeremy L Thompson code << " // EvalMode: " << CeedEvalModes[eval_mode] << "\n"; 5134b3e95d5SJeremy L Thompson if (is_input) { 5144b3e95d5SJeremy L Thompson switch (eval_mode) { 5154b3e95d5SJeremy L Thompson case CEED_EVAL_NONE: 5168b97b69aSJeremy L Thompson if (!use_3d_slices && !is_at_points) { 5174b3e95d5SJeremy L Thompson code << " CeedScalar *r_q" << var_suffix << " = r_e" << var_suffix << ";\n"; 5184b3e95d5SJeremy L Thompson } 5194b3e95d5SJeremy L Thompson break; 5204b3e95d5SJeremy L Thompson case CEED_EVAL_INTERP: 5218b97b69aSJeremy L Thompson if (is_at_points) { 522dc007f05SJeremy L Thompson std::string function_name = (dim == 1 ? "Interp" : "InterpTensor") + std::to_string(dim) + "d"; 523dc007f05SJeremy L Thompson 524dc007f05SJeremy L Thompson code << " CeedScalar r_c" << var_suffix << "[num_comp" << var_suffix << "*" << (dim >= 3 ? Q_name : "1") << "];\n"; 52599421279SJeremy L Thompson code << " " << function_name << "<num_comp" << var_suffix << ", " << P_name << ", " << Q_name << ", OP_T_1D>(data, r_e" << var_suffix 52699421279SJeremy L Thompson << ", s_B" << var_suffix << ", r_c" << var_suffix << ");\n"; 5278b97b69aSJeremy L Thompson } else { 528412e5683SJeremy L Thompson std::string function_name = is_tensor 529412e5683SJeremy L Thompson ? ((dim == 1 ? "Interp" : "InterpTensor") + std::to_string(dim) + "d" + (is_all_tensor ? "" : "Flattened")) 530412e5683SJeremy L Thompson : "InterpNonTensor"; 531c433aabcSJeremy L Thompson std::string op_t_1d_name = (is_all_tensor || !is_tensor) ? "OP_T_1D" : (P_1d > Q_1d ? P_name : Q_name); 532dc007f05SJeremy L Thompson 533259057edSJeremy L Thompson code << " CeedScalar r_q" << var_suffix << "[num_comp" << var_suffix << "*" << (is_all_tensor && (dim >= 3) ? Q_name : "1") << "];\n"; 534c433aabcSJeremy L Thompson code << " " << function_name << "<num_comp" << var_suffix << ", " << P_name << ", " << Q_name << ", " << op_t_1d_name << ">(data, r_e" 535c433aabcSJeremy L Thompson << var_suffix << ", s_B" << var_suffix << ", r_q" << var_suffix << ");\n"; 5368b97b69aSJeremy L Thompson } 5374b3e95d5SJeremy L Thompson break; 5384b3e95d5SJeremy L Thompson case CEED_EVAL_GRAD: 5398b97b69aSJeremy L Thompson if (is_at_points) { 540dc007f05SJeremy L Thompson std::string function_name = (dim == 1 ? "Interp" : "InterpTensor") + std::to_string(dim) + "d"; 541dc007f05SJeremy L Thompson 542dc007f05SJeremy L Thompson code << " CeedScalar r_c" << var_suffix << "[num_comp" << var_suffix << "*" << (dim >= 3 ? Q_name : "1") << "];\n"; 54399421279SJeremy L Thompson code << " " << function_name << "<num_comp" << var_suffix << ", " << P_name << ", " << Q_name << ", OP_T_1D>(data, r_e" << var_suffix 54499421279SJeremy L Thompson << ", s_B" << var_suffix << ", r_c" << var_suffix << ");\n"; 5458b97b69aSJeremy L Thompson } else if (use_3d_slices) { 546dc007f05SJeremy L Thompson std::string function_name = (dim > 1 ? "InterpTensor" : "Interp") + std::to_string(dim) + "d"; 547dc007f05SJeremy L Thompson 5484b3e95d5SJeremy L Thompson code << " CeedScalar r_q" << var_suffix << "[num_comp" << var_suffix << "*" << Q_name << "];\n"; 54999421279SJeremy L Thompson code << " " << function_name << "<num_comp" << var_suffix << ", " << P_name << ", " << Q_name << ", OP_T_1D>(data, r_e" << var_suffix 55099421279SJeremy L Thompson << ", s_B" << var_suffix << ", r_q" << var_suffix << ");\n"; 551dc007f05SJeremy L Thompson } else if (is_tensor) { 552dc007f05SJeremy L Thompson bool is_collocated = dim == 3 && Q_1d >= P_1d; 553412e5683SJeremy L Thompson std::string function_name = (dim == 1 ? "Grad" : (is_collocated ? "GradTensorCollocated" : "GradTensor")) + std::to_string(dim) + "d" + 554412e5683SJeremy L Thompson (is_all_tensor ? "" : "Flattened"); 555c433aabcSJeremy L Thompson std::string op_t_1d_name = is_all_tensor ? "OP_T_1D" : (P_1d > Q_1d ? P_name : Q_name); 556dc007f05SJeremy L Thompson 557259057edSJeremy L Thompson code << " CeedScalar r_q" << var_suffix << "[num_comp" << var_suffix << "*dim" << var_suffix << "*" 558259057edSJeremy L Thompson << (is_all_tensor && dim >= 3 ? Q_name : "1") << "];\n"; 559c433aabcSJeremy L Thompson code << " " << function_name << "<num_comp" << var_suffix << ", " << P_name << ", " << Q_name << ", " << op_t_1d_name << ">(data, r_e" 560c433aabcSJeremy L Thompson << var_suffix << ", s_B" << var_suffix << ", s_G" << var_suffix << ", r_q" << var_suffix << ");\n"; 5614b3e95d5SJeremy L Thompson } else { 562dc007f05SJeremy L Thompson std::string function_name = "GradNonTensor"; 563dc007f05SJeremy L Thompson 564412e5683SJeremy L Thompson code << " CeedScalar r_q" << var_suffix << "[num_comp" << var_suffix << "*dim" << var_suffix << "];\n"; 565c433aabcSJeremy L Thompson code << " " << function_name << "<num_comp" << var_suffix << ", dim" << var_suffix << ", " << P_name << ", " << Q_name 566c433aabcSJeremy L Thompson << ", OP_T_1D>(data, r_e" << var_suffix << ", s_G" << var_suffix << ", r_q" << var_suffix << ");\n"; 5674b3e95d5SJeremy L Thompson } 5684b3e95d5SJeremy L Thompson break; 5694b3e95d5SJeremy L Thompson case CEED_EVAL_WEIGHT: { 5708b97b69aSJeremy L Thompson if (is_at_points) { 5718b97b69aSJeremy L Thompson code << " // Nothing to do AtPoints\n"; 5728b97b69aSJeremy L Thompson } else { 5734b3e95d5SJeremy L Thompson CeedBasis_Cuda_shared *basis_data; 574412e5683SJeremy L Thompson std::string function_name = is_tensor 575412e5683SJeremy L Thompson ? ((dim == 1 ? "Weight" : "WeightTensor") + std::to_string(dim) + "d" + (is_all_tensor ? "" : "Flattened")) 576412e5683SJeremy L Thompson : "WeightNonTensor"; 5774b3e95d5SJeremy L Thompson 578259057edSJeremy L Thompson code << " CeedScalar r_q" << var_suffix << "[" << (is_all_tensor && (dim >= 3) ? Q_name : "1") << "];\n"; 5794b3e95d5SJeremy L Thompson CeedCallBackend(CeedBasisGetData(basis, &basis_data)); 5804b3e95d5SJeremy L Thompson data->W = basis_data->d_q_weight_1d; 581343e3094SJeremy L Thompson code << " " << function_name << "<" << P_name << ", " << Q_name << ">(data, W, r_q" << var_suffix << ");\n"; 5828b97b69aSJeremy L Thompson } 5834b3e95d5SJeremy L Thompson break; 5844b3e95d5SJeremy L Thompson } 5854b3e95d5SJeremy L Thompson // LCOV_EXCL_START 5864b3e95d5SJeremy L Thompson case CEED_EVAL_DIV: 5874b3e95d5SJeremy L Thompson case CEED_EVAL_CURL: 5884b3e95d5SJeremy L Thompson break; // TODO: Not implemented 5894b3e95d5SJeremy L Thompson // LCOV_EXCL_STOP 5904b3e95d5SJeremy L Thompson } 5914b3e95d5SJeremy L Thompson } else { 5924b3e95d5SJeremy L Thompson switch (eval_mode) { 5934b3e95d5SJeremy L Thompson case CEED_EVAL_NONE: 5944b3e95d5SJeremy L Thompson code << " CeedScalar *r_e" << var_suffix << " = r_q" << var_suffix << ";\n"; 5954b3e95d5SJeremy L Thompson break; // No action 5964b3e95d5SJeremy L Thompson case CEED_EVAL_INTERP: 597e93651e5SJeremy L Thompson code << " CeedScalar *r_e" << var_suffix << " = r_e_scratch;\n"; 5988b97b69aSJeremy L Thompson if (is_at_points) { 599dc007f05SJeremy L Thompson std::string function_name = (dim == 1 ? "InterpTranspose" : "InterpTransposeTensor") + std::to_string(dim) + "d"; 600dc007f05SJeremy L Thompson 60199421279SJeremy L Thompson code << " " << function_name << "<num_comp" << var_suffix << ", " << P_name << ", " << Q_name << ", OP_T_1D>(data, r_c" << var_suffix 60299421279SJeremy L Thompson << ", s_B" << var_suffix << ", r_e" << var_suffix << ");\n"; 6038b97b69aSJeremy L Thompson } else { 604dc007f05SJeremy L Thompson std::string function_name = 605412e5683SJeremy L Thompson is_tensor ? ((dim == 1 ? "InterpTranspose" : "InterpTransposeTensor") + std::to_string(dim) + "d" + (is_all_tensor ? "" : "Flattened")) 606412e5683SJeremy L Thompson : "InterpTransposeNonTensor"; 607c433aabcSJeremy L Thompson std::string op_t_1d_name = (is_all_tensor || !is_tensor) ? "OP_T_1D" : (P_1d > Q_1d ? P_name : Q_name); 608dc007f05SJeremy L Thompson 609c433aabcSJeremy L Thompson code << " " << function_name << "<num_comp" << var_suffix << ", " << P_name << ", " << Q_name << ", " << op_t_1d_name << ">(data, r_q" 610c433aabcSJeremy L Thompson << var_suffix << ", s_B" << var_suffix << ", r_e" << var_suffix << ");\n"; 6118b97b69aSJeremy L Thompson } 6124b3e95d5SJeremy L Thompson break; 6134b3e95d5SJeremy L Thompson case CEED_EVAL_GRAD: 614e93651e5SJeremy L Thompson code << " CeedScalar *r_e" << var_suffix << " = r_e_scratch;\n"; 6158b97b69aSJeremy L Thompson if (is_at_points) { 616dc007f05SJeremy L Thompson std::string function_name = (dim == 1 ? "InterpTranspose" : "InterpTransposeTensor") + std::to_string(dim) + "d"; 617dc007f05SJeremy L Thompson 61899421279SJeremy L Thompson code << " " << function_name << "<num_comp" << var_suffix << ", " << P_name << ", " << Q_name << ", OP_T_1D>(data, r_c" << var_suffix 61999421279SJeremy L Thompson << ", s_B" << var_suffix << ", r_e" << var_suffix << ");\n"; 6208b97b69aSJeremy L Thompson } else if (use_3d_slices) { 621dc007f05SJeremy L Thompson std::string function_name = (dim == 1 ? "InterpTranspose" : "InterpTransposeTensor") + std::to_string(dim) + "d"; 622dc007f05SJeremy L Thompson 62399421279SJeremy L Thompson code << " " << function_name << "<num_comp" << var_suffix << ", " << P_name << ", " << Q_name << ", OP_T_1D>(data, r_q" << var_suffix 62499421279SJeremy L Thompson << ", s_B" << var_suffix << ", r_e" << var_suffix << ");\n"; 625dc007f05SJeremy L Thompson } else if (is_tensor) { 626dc007f05SJeremy L Thompson bool is_collocated = dim == 3 && Q_1d >= P_1d; 627412e5683SJeremy L Thompson std::string function_name = (dim == 1 ? "GradTranspose" : (is_collocated ? "GradTransposeTensorCollocated" : "GradTransposeTensor")) + 628412e5683SJeremy L Thompson std::to_string(dim) + "d" + (is_all_tensor ? "" : "Flattened"); 629c433aabcSJeremy L Thompson std::string op_t_1d_name = is_all_tensor ? "OP_T_1D" : (P_1d > Q_1d ? P_name : Q_name); 630dc007f05SJeremy L Thompson 631c433aabcSJeremy L Thompson code << " " << function_name << "<num_comp" << var_suffix << ", " << P_name << ", " << Q_name << ", " << op_t_1d_name << ">(data, r_q" 632c433aabcSJeremy L Thompson << var_suffix << ", s_B" << var_suffix << ", s_G" << var_suffix << ", r_e" << var_suffix << ");\n"; 6334b3e95d5SJeremy L Thompson } else { 634dc007f05SJeremy L Thompson std::string function_name = "GradTransposeNonTensor"; 635dc007f05SJeremy L Thompson 636c433aabcSJeremy L Thompson code << " " << function_name << "<num_comp" << var_suffix << ", dim" << var_suffix << ", " << P_name << ", " << Q_name 637c433aabcSJeremy L Thompson << ", OP_T_1D>(data, r_q" << var_suffix << ", s_G" << var_suffix << ", r_e" << var_suffix << ");\n"; 6384b3e95d5SJeremy L Thompson } 6394b3e95d5SJeremy L Thompson break; 6404b3e95d5SJeremy L Thompson // LCOV_EXCL_START 6414b3e95d5SJeremy L Thompson case CEED_EVAL_WEIGHT: 6424b3e95d5SJeremy L Thompson break; // Should not occur 6434b3e95d5SJeremy L Thompson case CEED_EVAL_DIV: 6444b3e95d5SJeremy L Thompson case CEED_EVAL_CURL: 6454b3e95d5SJeremy L Thompson break; // TODO: Not implemented 6464b3e95d5SJeremy L Thompson // LCOV_EXCL_STOP 6474b3e95d5SJeremy L Thompson } 6484b3e95d5SJeremy L Thompson } 649681d0ea7SJeremy L Thompson CeedCallBackend(CeedBasisDestroy(&basis)); 6504b3e95d5SJeremy L Thompson return CEED_ERROR_SUCCESS; 6514b3e95d5SJeremy L Thompson } 6524b3e95d5SJeremy L Thompson 6534b3e95d5SJeremy L Thompson //------------------------------------------------------------------------------ 6544b3e95d5SJeremy L Thompson // QFunction 6554b3e95d5SJeremy L Thompson //------------------------------------------------------------------------------ 656412e5683SJeremy L Thompson static int CeedOperatorBuildKernelQFunction_Cuda_gen(std::ostringstream &code, CeedOperator_Cuda_gen *data, CeedInt max_dim, CeedInt max_num_points, 6578b97b69aSJeremy L Thompson CeedInt num_input_fields, CeedOperatorField *op_input_fields, 6588b97b69aSJeremy L Thompson CeedQFunctionField *qf_input_fields, CeedInt num_output_fields, 6598b97b69aSJeremy L Thompson CeedOperatorField *op_output_fields, CeedQFunctionField *qf_output_fields, 660412e5683SJeremy L Thompson std::string qfunction_name, CeedInt Q_1d, bool is_all_tensor, bool is_at_points, 661dc007f05SJeremy L Thompson bool use_3d_slices) { 662412e5683SJeremy L Thompson std::string Q_name = is_all_tensor ? "Q_1d" : "Q"; 6634b3e95d5SJeremy L Thompson CeedEvalMode eval_mode = CEED_EVAL_NONE; 6644b3e95d5SJeremy L Thompson CeedElemRestriction elem_rstr; 6654b3e95d5SJeremy L Thompson 6668b97b69aSJeremy L Thompson // Setup output arrays 6674b3e95d5SJeremy L Thompson code << "\n // -- Output field setup\n"; 6684b3e95d5SJeremy L Thompson for (CeedInt i = 0; i < num_output_fields; i++) { 66959fa3f92SJeremy L Thompson const char *field_name; 6704b3e95d5SJeremy L Thompson std::string var_suffix = "_out_" + std::to_string(i); 6714b3e95d5SJeremy L Thompson 67259fa3f92SJeremy L Thompson CeedCallBackend(CeedOperatorFieldGetName(op_output_fields[i], &field_name)); 67359fa3f92SJeremy L Thompson code << " // ---- Output field " << i << ": " << field_name << "\n"; 6744b3e95d5SJeremy L Thompson CeedCallBackend(CeedQFunctionFieldGetEvalMode(qf_output_fields[i], &eval_mode)); 6758b97b69aSJeremy L Thompson switch (eval_mode) { 6768b97b69aSJeremy L Thompson case CEED_EVAL_NONE: 6778b97b69aSJeremy L Thompson if (is_at_points) { 6788b97b69aSJeremy L Thompson code << " CeedScalar r_q" << var_suffix << "[num_comp" << var_suffix << "];\n"; 6798b97b69aSJeremy L Thompson } else { 680412e5683SJeremy L Thompson code << " CeedScalar r_q" << var_suffix << "[num_comp" << var_suffix << "*" << (is_all_tensor && (max_dim >= 3) ? Q_name : "1") 681412e5683SJeremy L Thompson << "];\n"; 6824b3e95d5SJeremy L Thompson } 6838b97b69aSJeremy L Thompson break; 6848b97b69aSJeremy L Thompson case CEED_EVAL_INTERP: 6858b97b69aSJeremy L Thompson if (is_at_points) { 6868b97b69aSJeremy L Thompson // Accumulator for point data 687412e5683SJeremy L Thompson code << " CeedScalar r_c" << var_suffix << "[num_comp" << var_suffix << "*" << (max_dim >= 3 ? Q_name : "1") << "];\n"; 688412e5683SJeremy L Thompson code << " for (CeedInt i = 0; i < num_comp" << var_suffix << "*" << (max_dim >= 3 ? Q_name : "1") << "; i++) {\n"; 6898b97b69aSJeremy L Thompson code << " r_c" << var_suffix << "[i] = 0.0;\n"; 6908b97b69aSJeremy L Thompson code << " }\n"; 6918b97b69aSJeremy L Thompson } else { 692412e5683SJeremy L Thompson code << " CeedScalar r_q" << var_suffix << "[num_comp" << var_suffix << "*" << (is_all_tensor && (max_dim >= 3) ? Q_name : "1") 693412e5683SJeremy L Thompson << "];\n"; 6948b97b69aSJeremy L Thompson } 6958b97b69aSJeremy L Thompson break; 6968b97b69aSJeremy L Thompson case CEED_EVAL_GRAD: 6978b97b69aSJeremy L Thompson if (is_at_points) { 6988b97b69aSJeremy L Thompson // Accumulator for point data 699a61b1c91SJeremy L Thompson code << " CeedScalar r_c" << var_suffix << "[num_comp" << var_suffix << "*" << (max_dim >= 3 ? Q_name : "1") << "];\n"; 700412e5683SJeremy L Thompson code << " for (CeedInt i = 0; i < num_comp" << var_suffix << "*" << (max_dim >= 3 ? Q_name : "1") << "; i++) {\n"; 7018b97b69aSJeremy L Thompson code << " r_c" << var_suffix << "[i] = 0.0;\n"; 7028b97b69aSJeremy L Thompson code << " }\n"; 7038b97b69aSJeremy L Thompson } else if (use_3d_slices) { 7044b3e95d5SJeremy L Thompson // Accumulator for gradient slices 7054b3e95d5SJeremy L Thompson code << " CeedScalar r_q" << var_suffix << "[num_comp" << var_suffix << "*" << Q_name << "];\n"; 7064b3e95d5SJeremy L Thompson code << " for (CeedInt i = 0; i < num_comp" << var_suffix << "*" << Q_name << "; i++) {\n"; 7074b3e95d5SJeremy L Thompson code << " r_q" << var_suffix << "[i] = 0.0;\n"; 7084b3e95d5SJeremy L Thompson code << " }\n"; 7094b3e95d5SJeremy L Thompson } else { 710412e5683SJeremy L Thompson code << " CeedScalar r_q" << var_suffix << "[num_comp" << var_suffix << "*dim" << var_suffix << "*" 711412e5683SJeremy L Thompson << (is_all_tensor && (max_dim >= 3) ? Q_name : "1") << "];\n"; 7124b3e95d5SJeremy L Thompson } 7138b97b69aSJeremy L Thompson break; 7148b97b69aSJeremy L Thompson case CEED_EVAL_WEIGHT: 7158b97b69aSJeremy L Thompson break; 7168b97b69aSJeremy L Thompson // LCOV_EXCL_START 7178b97b69aSJeremy L Thompson case CEED_EVAL_DIV: 7188b97b69aSJeremy L Thompson case CEED_EVAL_CURL: 7198b97b69aSJeremy L Thompson break; // TODO: Not implemented 7208b97b69aSJeremy L Thompson // LCOV_EXCL_STOP 7214b3e95d5SJeremy L Thompson } 7224b3e95d5SJeremy L Thompson } 7234b3e95d5SJeremy L Thompson 7248b97b69aSJeremy L Thompson if (is_at_points) { 7258b97b69aSJeremy L Thompson // We need to handle batches of points 7268b97b69aSJeremy L Thompson code << "\n // Note: Using batches of points\n"; 727a61b1c91SJeremy L Thompson code << " const CeedInt point_loop_bound = (blockDim.x*blockDim.y) * ceil((1.0*max_num_points) / (blockDim.x*blockDim.y));\n\n"; 7288b97b69aSJeremy L Thompson code << " #pragma unroll\n"; 7298b97b69aSJeremy L Thompson code << " for (CeedInt i = threadIdx.x + threadIdx.y*blockDim.x; i < point_loop_bound; i += blockDim.x*blockDim.y) {\n"; 7308b97b69aSJeremy L Thompson code << " const CeedInt p = i % max_num_points;\n\n"; 7318b97b69aSJeremy L Thompson 7328b97b69aSJeremy L Thompson code << " // -- Coordinates\n"; 733412e5683SJeremy L Thompson code << " CeedScalar r_x[max_dim];\n"; 734412e5683SJeremy L Thompson code << " ReadPoint<max_dim, coords_comp_stride, max_num_points>(data, elem, p, max_num_points, points.indices, points.coords, r_x);\n\n"; 7358b97b69aSJeremy L Thompson 7368b97b69aSJeremy L Thompson code << " // -- Input fields\n"; 7378b97b69aSJeremy L Thompson for (CeedInt i = 0; i < num_input_fields; i++) { 73859fa3f92SJeremy L Thompson const char *field_name; 7398b97b69aSJeremy L Thompson std::string var_suffix = "_in_" + std::to_string(i); 740f725b54bSJeremy L Thompson std::string P_name = "P_1d" + var_suffix; 7418b97b69aSJeremy L Thompson 74259fa3f92SJeremy L Thompson CeedCallBackend(CeedOperatorFieldGetName(op_input_fields[i], &field_name)); 74359fa3f92SJeremy L Thompson code << " // ---- Input field " << i << ": " << field_name << "\n"; 7448b97b69aSJeremy L Thompson CeedCallBackend(CeedQFunctionFieldGetEvalMode(qf_input_fields[i], &eval_mode)); 7458b97b69aSJeremy L Thompson // Basis action 7468b97b69aSJeremy L Thompson code << " // EvalMode: " << CeedEvalModes[eval_mode] << "\n"; 7478b97b69aSJeremy L Thompson switch (eval_mode) { 7488b97b69aSJeremy L Thompson case CEED_EVAL_NONE: 7498b97b69aSJeremy L Thompson code << " CeedScalar r_s" << var_suffix << "[num_comp" << var_suffix << "];\n"; 7508b97b69aSJeremy L Thompson code << " ReadPoint<num_comp" << var_suffix << ", comp_stride" << var_suffix 7518b97b69aSJeremy L Thompson << ", max_num_points>(data, elem, p, max_num_points, indices.inputs[" << i << "], d" << var_suffix << ", r_s" << var_suffix << ");\n"; 7528b97b69aSJeremy L Thompson break; 7538b97b69aSJeremy L Thompson case CEED_EVAL_INTERP: 7548b97b69aSJeremy L Thompson code << " CeedScalar r_s" << var_suffix << "[num_comp" << var_suffix << "];\n"; 755412e5683SJeremy L Thompson code << " InterpAtPoints" << max_dim << "d<num_comp" << var_suffix << ", max_num_points, " << P_name << ", " << Q_name 756412e5683SJeremy L Thompson << ">(data, i, r_c" << var_suffix << ", r_x, r_s" << var_suffix << ");\n"; 7578b97b69aSJeremy L Thompson break; 7588b97b69aSJeremy L Thompson case CEED_EVAL_GRAD: 759412e5683SJeremy L Thompson code << " CeedScalar r_s" << var_suffix << "[num_comp" << var_suffix << "*dim" << var_suffix << "];\n"; 760412e5683SJeremy L Thompson code << " GradAtPoints" << max_dim << "d<num_comp" << var_suffix << ", max_num_points, " << P_name << ", " << Q_name 761412e5683SJeremy L Thompson << ">(data, i, r_c" << var_suffix << ", r_x, r_s" << var_suffix << ");\n"; 7628b97b69aSJeremy L Thompson break; 7638b97b69aSJeremy L Thompson case CEED_EVAL_WEIGHT: 7648b97b69aSJeremy L Thompson code << " CeedScalar r_s" << var_suffix << "[1];\n"; 7658b97b69aSJeremy L Thompson code << " r_s" << var_suffix << "[0] = 1.0;\n"; 7668b97b69aSJeremy L Thompson break; 7678b97b69aSJeremy L Thompson // LCOV_EXCL_START 7688b97b69aSJeremy L Thompson case CEED_EVAL_DIV: 7698b97b69aSJeremy L Thompson case CEED_EVAL_CURL: 7708b97b69aSJeremy L Thompson break; // TODO: Not implemented 7718b97b69aSJeremy L Thompson // LCOV_EXCL_STOP 7728b97b69aSJeremy L Thompson } 7738b97b69aSJeremy L Thompson } 7748b97b69aSJeremy L Thompson code << "\n // -- Output fields\n"; 7758b97b69aSJeremy L Thompson for (CeedInt i = 0; i < num_output_fields; i++) { 77659fa3f92SJeremy L Thompson const char *field_name; 7778b97b69aSJeremy L Thompson std::string var_suffix = "_out_" + std::to_string(i); 7788b97b69aSJeremy L Thompson 77959fa3f92SJeremy L Thompson CeedCallBackend(CeedOperatorFieldGetName(op_output_fields[i], &field_name)); 78059fa3f92SJeremy L Thompson code << " // ---- Output field " << i << ": " << field_name << "\n"; 7818b97b69aSJeremy L Thompson CeedCallBackend(CeedQFunctionFieldGetEvalMode(qf_output_fields[i], &eval_mode)); 7828b97b69aSJeremy L Thompson // Basis action 7838b97b69aSJeremy L Thompson switch (eval_mode) { 7848b97b69aSJeremy L Thompson case CEED_EVAL_NONE: 7858b97b69aSJeremy L Thompson code << " CeedScalar r_s" << var_suffix << "[num_comp" << var_suffix << "];\n"; 7868b97b69aSJeremy L Thompson break; 7878b97b69aSJeremy L Thompson case CEED_EVAL_INTERP: 7888b97b69aSJeremy L Thompson code << " CeedScalar r_s" << var_suffix << "[num_comp" << var_suffix << "];\n"; 7898b97b69aSJeremy L Thompson break; 7908b97b69aSJeremy L Thompson case CEED_EVAL_GRAD: 791412e5683SJeremy L Thompson code << " CeedScalar r_s" << var_suffix << "[num_comp" << var_suffix << "*dim" << var_suffix << "];\n"; 7928b97b69aSJeremy L Thompson break; 7938b97b69aSJeremy L Thompson // LCOV_EXCL_START 7948b97b69aSJeremy L Thompson case CEED_EVAL_WEIGHT: 7958b97b69aSJeremy L Thompson break; // Should not occur 7968b97b69aSJeremy L Thompson case CEED_EVAL_DIV: 7978b97b69aSJeremy L Thompson case CEED_EVAL_CURL: 7988b97b69aSJeremy L Thompson break; // TODO: Not implemented 7998b97b69aSJeremy L Thompson // LCOV_EXCL_STOP 8008b97b69aSJeremy L Thompson } 8018b97b69aSJeremy L Thompson } 8028b97b69aSJeremy L Thompson 8038b97b69aSJeremy L Thompson } else if (use_3d_slices) { 8044b3e95d5SJeremy L Thompson // We treat quadrature points per slice in 3d to save registers 8054b3e95d5SJeremy L Thompson code << "\n // Note: Using planes of 3D elements\n"; 8064b3e95d5SJeremy L Thompson code << " #pragma unroll\n"; 8074b3e95d5SJeremy L Thompson code << " for (CeedInt q = 0; q < " << Q_name << "; q++) {\n"; 8084b3e95d5SJeremy L Thompson code << " // -- Input fields\n"; 8094b3e95d5SJeremy L Thompson for (CeedInt i = 0; i < num_input_fields; i++) { 81059fa3f92SJeremy L Thompson const char *field_name; 8114b3e95d5SJeremy L Thompson std::string var_suffix = "_in_" + std::to_string(i); 8124b3e95d5SJeremy L Thompson 81359fa3f92SJeremy L Thompson CeedCallBackend(CeedOperatorFieldGetName(op_input_fields[i], &field_name)); 81459fa3f92SJeremy L Thompson code << " // ---- Input field " << i << ": " << field_name << "\n"; 8154b3e95d5SJeremy L Thompson CeedCallBackend(CeedQFunctionFieldGetEvalMode(qf_input_fields[i], &eval_mode)); 8164b3e95d5SJeremy L Thompson // Basis action 8174b3e95d5SJeremy L Thompson code << " // EvalMode: " << CeedEvalModes[eval_mode] << "\n"; 8184b3e95d5SJeremy L Thompson switch (eval_mode) { 8194b3e95d5SJeremy L Thompson case CEED_EVAL_NONE: 8204b3e95d5SJeremy L Thompson bool is_strided; 8214b3e95d5SJeremy L Thompson 8224b3e95d5SJeremy L Thompson code << " CeedScalar r_s" << var_suffix << "[num_comp" << var_suffix << "];\n"; 8234b3e95d5SJeremy L Thompson 8244b3e95d5SJeremy L Thompson CeedCallBackend(CeedOperatorFieldGetElemRestriction(op_input_fields[i], &elem_rstr)); 8254b3e95d5SJeremy L Thompson CeedCallBackend(CeedElemRestrictionIsStrided(elem_rstr, &is_strided)); 8264b3e95d5SJeremy L Thompson if (is_strided) { 8274b3e95d5SJeremy L Thompson bool has_backend_strides; 8284b3e95d5SJeremy L Thompson CeedInt num_elem, elem_size; 8294b3e95d5SJeremy L Thompson 8304b3e95d5SJeremy L Thompson CeedCallBackend(CeedElemRestrictionGetElementSize(elem_rstr, &elem_size)); 8314b3e95d5SJeremy L Thompson CeedCallBackend(CeedElemRestrictionHasBackendStrides(elem_rstr, &has_backend_strides)); 8324b3e95d5SJeremy L Thompson CeedCallBackend(CeedElemRestrictionGetNumElements(elem_rstr, &num_elem)); 8334b3e95d5SJeremy L Thompson CeedInt strides[3] = {1, elem_size * num_elem, elem_size}; 8344b3e95d5SJeremy L Thompson 8354b3e95d5SJeremy L Thompson if (!has_backend_strides) { 8364b3e95d5SJeremy L Thompson CeedCallBackend(CeedElemRestrictionGetStrides(elem_rstr, strides)); 8374b3e95d5SJeremy L Thompson } 8384b3e95d5SJeremy L Thompson code << " // Strides: {" << strides[0] << ", " << strides[1] << ", " << strides[2] << "}\n"; 839f815fac9SJeremy L Thompson code << " ReadEVecSliceStrided3d<num_comp" << var_suffix << ", " << Q_name << ", " << strides[0] << ", " << strides[1] << ", " 8404b3e95d5SJeremy L Thompson << strides[2] << ">(data, elem, q, d" << var_suffix << ", r_s" << var_suffix << ");\n"; 8414b3e95d5SJeremy L Thompson } else { 8424b3e95d5SJeremy L Thompson CeedSize l_size = 0; 8434b3e95d5SJeremy L Thompson CeedInt comp_stride; 8444b3e95d5SJeremy L Thompson CeedElemRestriction_Cuda *rstr_data; 8454b3e95d5SJeremy L Thompson 8464b3e95d5SJeremy L Thompson CeedCallBackend(CeedElemRestrictionGetLVectorSize(elem_rstr, &l_size)); 8474b3e95d5SJeremy L Thompson code << " const CeedInt l_size" << var_suffix << " = " << l_size << ";\n"; 8484b3e95d5SJeremy L Thompson CeedCallBackend(CeedElemRestrictionGetCompStride(elem_rstr, &comp_stride)); 8494b3e95d5SJeremy L Thompson code << " // CompStride: " << comp_stride << "\n"; 8504b3e95d5SJeremy L Thompson CeedCallBackend(CeedElemRestrictionGetData(elem_rstr, &rstr_data)); 8514b3e95d5SJeremy L Thompson data->indices.inputs[i] = (CeedInt *)rstr_data->d_offsets; 852f815fac9SJeremy L Thompson code << " ReadEVecSliceStandard3d<num_comp" << var_suffix << ", " << comp_stride << ", " << Q_name << ">(data, l_size" << var_suffix 8534b3e95d5SJeremy L Thompson << ", elem, q, indices.inputs[" << i << "], d" << var_suffix << ", r_s" << var_suffix << ");\n"; 8544b3e95d5SJeremy L Thompson } 855681d0ea7SJeremy L Thompson CeedCallBackend(CeedElemRestrictionDestroy(&elem_rstr)); 8564b3e95d5SJeremy L Thompson break; 8574b3e95d5SJeremy L Thompson case CEED_EVAL_INTERP: 8584b3e95d5SJeremy L Thompson code << " CeedScalar r_s" << var_suffix << "[num_comp" << var_suffix << "];\n"; 8594b3e95d5SJeremy L Thompson code << " for (CeedInt j = 0; j < num_comp" << var_suffix << "; j++) {\n"; 8604b3e95d5SJeremy L Thompson code << " r_s" << var_suffix << "[j] = r_q" << var_suffix << "[q + j*" << Q_name << "];\n"; 8614b3e95d5SJeremy L Thompson code << " }\n"; 8624b3e95d5SJeremy L Thompson break; 8634b3e95d5SJeremy L Thompson case CEED_EVAL_GRAD: 864412e5683SJeremy L Thompson code << " CeedScalar r_s" << var_suffix << "[num_comp" << var_suffix << "*dim" << var_suffix << "];\n"; 86599421279SJeremy L Thompson code << " GradColloSlice3d<num_comp" << var_suffix << ", " << Q_name << ", OP_T_1D>(data, q, r_q" << var_suffix << ", s_G" 86699421279SJeremy L Thompson << var_suffix << ", r_s" << var_suffix << ");\n"; 8674b3e95d5SJeremy L Thompson break; 8684b3e95d5SJeremy L Thompson case CEED_EVAL_WEIGHT: 8694b3e95d5SJeremy L Thompson code << " CeedScalar r_s" << var_suffix << "[1];\n"; 8704b3e95d5SJeremy L Thompson code << " r_s" << var_suffix << "[0] = r_q" << var_suffix << "[q];\n"; 8718b97b69aSJeremy L Thompson break; 8724b3e95d5SJeremy L Thompson // LCOV_EXCL_START 8734b3e95d5SJeremy L Thompson case CEED_EVAL_DIV: 8744b3e95d5SJeremy L Thompson case CEED_EVAL_CURL: 8754b3e95d5SJeremy L Thompson break; // TODO: Not implemented 8764b3e95d5SJeremy L Thompson // LCOV_EXCL_STOP 8774b3e95d5SJeremy L Thompson } 8784b3e95d5SJeremy L Thompson } 8794b3e95d5SJeremy L Thompson code << "\n // -- Output fields\n"; 8804b3e95d5SJeremy L Thompson for (CeedInt i = 0; i < num_output_fields; i++) { 88159fa3f92SJeremy L Thompson const char *field_name; 8824b3e95d5SJeremy L Thompson std::string var_suffix = "_out_" + std::to_string(i); 8834b3e95d5SJeremy L Thompson 88459fa3f92SJeremy L Thompson CeedCallBackend(CeedOperatorFieldGetName(op_output_fields[i], &field_name)); 88559fa3f92SJeremy L Thompson code << " // ---- Output field " << i << ": " << field_name << "\n"; 8864b3e95d5SJeremy L Thompson CeedCallBackend(CeedQFunctionFieldGetEvalMode(qf_output_fields[i], &eval_mode)); 8874b3e95d5SJeremy L Thompson // Basis action 8884b3e95d5SJeremy L Thompson switch (eval_mode) { 8894b3e95d5SJeremy L Thompson case CEED_EVAL_NONE: 8904b3e95d5SJeremy L Thompson code << " CeedScalar r_s" << var_suffix << "[num_comp" << var_suffix << "];\n"; 8918b97b69aSJeremy L Thompson break; 8924b3e95d5SJeremy L Thompson case CEED_EVAL_INTERP: 8934b3e95d5SJeremy L Thompson code << " CeedScalar r_s" << var_suffix << "[num_comp" << var_suffix << "];\n"; 8944b3e95d5SJeremy L Thompson break; 8954b3e95d5SJeremy L Thompson case CEED_EVAL_GRAD: 896412e5683SJeremy L Thompson code << " CeedScalar r_s" << var_suffix << "[num_comp" << var_suffix << "*dim" << var_suffix << "];\n"; 8974b3e95d5SJeremy L Thompson break; 8984b3e95d5SJeremy L Thompson // LCOV_EXCL_START 8994b3e95d5SJeremy L Thompson case CEED_EVAL_WEIGHT: 9004b3e95d5SJeremy L Thompson break; // Should not occur 9014b3e95d5SJeremy L Thompson case CEED_EVAL_DIV: 9024b3e95d5SJeremy L Thompson case CEED_EVAL_CURL: 9034b3e95d5SJeremy L Thompson break; // TODO: Not implemented 9044b3e95d5SJeremy L Thompson // LCOV_EXCL_STOP 9054b3e95d5SJeremy L Thompson } 9064b3e95d5SJeremy L Thompson } 9074b3e95d5SJeremy L Thompson } else { 9084b3e95d5SJeremy L Thompson code << "\n // Note: Using full elements\n"; 9094b3e95d5SJeremy L Thompson code << " {\n"; 9104b3e95d5SJeremy L Thompson code << " // -- Input fields\n"; 9114b3e95d5SJeremy L Thompson for (CeedInt i = 0; i < num_input_fields; i++) { 91259fa3f92SJeremy L Thompson const char *field_name; 91359fa3f92SJeremy L Thompson 91459fa3f92SJeremy L Thompson CeedCallBackend(CeedOperatorFieldGetName(op_input_fields[i], &field_name)); 91559fa3f92SJeremy L Thompson code << " // ---- Input field " << i << ": " << field_name << "\n"; 9164b3e95d5SJeremy L Thompson code << " CeedScalar *r_s_in_" << i << " = r_q_in_" << i << ";\n"; 9174b3e95d5SJeremy L Thompson } 9184b3e95d5SJeremy L Thompson code << " // -- Output fields\n"; 9194b3e95d5SJeremy L Thompson for (CeedInt i = 0; i < num_output_fields; i++) { 92059fa3f92SJeremy L Thompson const char *field_name; 92159fa3f92SJeremy L Thompson 92259fa3f92SJeremy L Thompson CeedCallBackend(CeedOperatorFieldGetName(op_output_fields[i], &field_name)); 92359fa3f92SJeremy L Thompson code << " // ---- Output field " << i << ": " << field_name << "\n"; 9244b3e95d5SJeremy L Thompson code << " CeedScalar *r_s_out_" << i << " = r_q_out_" << i << ";\n"; 9254b3e95d5SJeremy L Thompson } 9264b3e95d5SJeremy L Thompson } 9274b3e95d5SJeremy L Thompson 9284b3e95d5SJeremy L Thompson // Input and output buffers 9294b3e95d5SJeremy L Thompson code << "\n // -- QFunction inputs and outputs\n"; 9304b3e95d5SJeremy L Thompson code << " // ---- Inputs\n"; 9314b3e95d5SJeremy L Thompson code << " CeedScalar *inputs[" << CeedIntMax(num_input_fields, 1) << "];\n"; 9324b3e95d5SJeremy L Thompson for (CeedInt i = 0; i < num_input_fields; i++) { 93359fa3f92SJeremy L Thompson const char *field_name; 93459fa3f92SJeremy L Thompson 93559fa3f92SJeremy L Thompson CeedCallBackend(CeedOperatorFieldGetName(op_input_fields[i], &field_name)); 93659fa3f92SJeremy L Thompson code << " // ------ Input field " << i << ": " << field_name << "\n"; 9374b3e95d5SJeremy L Thompson code << " inputs[" << i << "] = r_s_in_" << i << ";\n"; 9384b3e95d5SJeremy L Thompson } 9394b3e95d5SJeremy L Thompson code << " // ---- Outputs\n"; 9404b3e95d5SJeremy L Thompson code << " CeedScalar *outputs[" << CeedIntMax(num_output_fields, 1) << "];\n"; 9414b3e95d5SJeremy L Thompson for (CeedInt i = 0; i < num_output_fields; i++) { 94259fa3f92SJeremy L Thompson const char *field_name; 94359fa3f92SJeremy L Thompson 94459fa3f92SJeremy L Thompson CeedCallBackend(CeedOperatorFieldGetName(op_output_fields[i], &field_name)); 94559fa3f92SJeremy L Thompson code << " // ------ Output field " << i << ": " << field_name << "\n"; 9464b3e95d5SJeremy L Thompson code << " outputs[" << i << "] = r_s_out_" << i << ";\n"; 9474b3e95d5SJeremy L Thompson } 9484b3e95d5SJeremy L Thompson 9494b3e95d5SJeremy L Thompson // Apply QFunction 9504b3e95d5SJeremy L Thompson code << "\n // -- Apply QFunction\n"; 9514b3e95d5SJeremy L Thompson code << " " << qfunction_name << "(ctx, "; 952412e5683SJeremy L Thompson if (max_dim != 3 || is_at_points || use_3d_slices || !is_all_tensor) { 9534b3e95d5SJeremy L Thompson code << "1"; 9544b3e95d5SJeremy L Thompson } else { 955dc007f05SJeremy L Thompson code << Q_name; 9564b3e95d5SJeremy L Thompson } 9574b3e95d5SJeremy L Thompson code << ", inputs, outputs);\n"; 9584b3e95d5SJeremy L Thompson 9598b97b69aSJeremy L Thompson if (is_at_points) { 9608b97b69aSJeremy L Thompson // Map back to coefficients 9618b97b69aSJeremy L Thompson code << "\n // -- Output fields\n"; 9628b97b69aSJeremy L Thompson for (CeedInt i = 0; i < num_output_fields; i++) { 96359fa3f92SJeremy L Thompson const char *field_name; 9648b97b69aSJeremy L Thompson std::string var_suffix = "_out_" + std::to_string(i); 9658b97b69aSJeremy L Thompson std::string P_name = "P_1d" + var_suffix; 9668b97b69aSJeremy L Thompson 96759fa3f92SJeremy L Thompson CeedCallBackend(CeedOperatorFieldGetName(op_output_fields[i], &field_name)); 96859fa3f92SJeremy L Thompson code << " // ---- Output field " << i << ": " << field_name << "\n"; 9698b97b69aSJeremy L Thompson CeedCallBackend(CeedQFunctionFieldGetEvalMode(qf_output_fields[i], &eval_mode)); 9708b97b69aSJeremy L Thompson // Basis action 9718b97b69aSJeremy L Thompson code << " // EvalMode: " << CeedEvalModes[eval_mode] << "\n"; 9728b97b69aSJeremy L Thompson switch (eval_mode) { 9738b97b69aSJeremy L Thompson case CEED_EVAL_NONE: { 9748b97b69aSJeremy L Thompson CeedInt comp_stride; 9758b97b69aSJeremy L Thompson CeedElemRestriction elem_rstr; 9768b97b69aSJeremy L Thompson 9778b97b69aSJeremy L Thompson CeedCallBackend(CeedOperatorFieldGetElemRestriction(op_output_fields[i], &elem_rstr)); 9788b97b69aSJeremy L Thompson CeedCallBackend(CeedElemRestrictionGetCompStride(elem_rstr, &comp_stride)); 9798b97b69aSJeremy L Thompson CeedCallBackend(CeedElemRestrictionDestroy(&elem_rstr)); 9808b97b69aSJeremy L Thompson code << " const CeedInt comp_stride" << var_suffix << " = " << comp_stride << ";\n"; 9818b97b69aSJeremy L Thompson code << " WritePoint<num_comp" << var_suffix << ", comp_stride" << var_suffix 9828b97b69aSJeremy L Thompson << ", max_num_points>(data, elem, i, points.num_per_elem[elem], indices.outputs[" << i << "]" 9838b97b69aSJeremy L Thompson << ", r_s" << var_suffix << ", d" << var_suffix << ");\n"; 9848b97b69aSJeremy L Thompson break; 9858b97b69aSJeremy L Thompson } 9868b97b69aSJeremy L Thompson case CEED_EVAL_INTERP: 9878b97b69aSJeremy L Thompson code << " if (i >= points.num_per_elem[elem]) {\n"; 9888b97b69aSJeremy L Thompson code << " for (CeedInt j = 0; j < num_comp" << var_suffix << "; j++) r_s" << var_suffix << "[j] = 0.0;\n"; 9898b97b69aSJeremy L Thompson code << " }\n"; 990412e5683SJeremy L Thompson code << " InterpTransposeAtPoints" << max_dim << "d<num_comp" << var_suffix << ", max_num_points, " << P_name << ", " << Q_name 991f725b54bSJeremy L Thompson << ">(data, i, r_s" << var_suffix << ", r_x, r_c" << var_suffix << ");\n"; 9928b97b69aSJeremy L Thompson break; 9938b97b69aSJeremy L Thompson case CEED_EVAL_GRAD: 9948b97b69aSJeremy L Thompson code << " if (i >= points.num_per_elem[elem]) {\n"; 995412e5683SJeremy L Thompson code << " for (CeedInt j = 0; j < num_comp" << var_suffix << "*dim" << var_suffix << "; j++) r_s" << var_suffix << "[j] = 0.0;\n"; 9968b97b69aSJeremy L Thompson code << " }\n"; 997412e5683SJeremy L Thompson code << " GradTransposeAtPoints" << max_dim << "d<num_comp" << var_suffix << ", max_num_points, " << P_name << ", " << Q_name 998f725b54bSJeremy L Thompson << ">(data, i, r_s" << var_suffix << ", r_x, r_c" << var_suffix << ");\n"; 9998b97b69aSJeremy L Thompson break; 10008b97b69aSJeremy L Thompson // LCOV_EXCL_START 10018b97b69aSJeremy L Thompson case CEED_EVAL_WEIGHT: 10028b97b69aSJeremy L Thompson break; // Should not occur 10038b97b69aSJeremy L Thompson case CEED_EVAL_DIV: 10048b97b69aSJeremy L Thompson case CEED_EVAL_CURL: 10058b97b69aSJeremy L Thompson break; // TODO: Not implemented 10068b97b69aSJeremy L Thompson // LCOV_EXCL_STOP 10078b97b69aSJeremy L Thompson } 10088b97b69aSJeremy L Thompson } 10098b97b69aSJeremy L Thompson } else if (use_3d_slices) { 10104b3e95d5SJeremy L Thompson // Copy or apply transpose grad, if needed 10118b97b69aSJeremy L Thompson code << "\n // -- Output fields\n"; 10124b3e95d5SJeremy L Thompson for (CeedInt i = 0; i < num_output_fields; i++) { 101359fa3f92SJeremy L Thompson const char *field_name; 10144b3e95d5SJeremy L Thompson std::string var_suffix = "_out_" + std::to_string(i); 10154b3e95d5SJeremy L Thompson std::string P_name = "P_1d" + var_suffix; 10164b3e95d5SJeremy L Thompson 101759fa3f92SJeremy L Thompson CeedCallBackend(CeedOperatorFieldGetName(op_output_fields[i], &field_name)); 101859fa3f92SJeremy L Thompson code << " // ---- Output field " << i << ": " << field_name << "\n"; 10194b3e95d5SJeremy L Thompson CeedCallBackend(CeedQFunctionFieldGetEvalMode(qf_output_fields[i], &eval_mode)); 10204b3e95d5SJeremy L Thompson // Basis action 10214b3e95d5SJeremy L Thompson code << " // EvalMode: " << CeedEvalModes[eval_mode] << "\n"; 10224b3e95d5SJeremy L Thompson switch (eval_mode) { 10234b3e95d5SJeremy L Thompson case CEED_EVAL_NONE: 10244b3e95d5SJeremy L Thompson code << " for (CeedInt j = 0; j < num_comp" << var_suffix << " ; j++) {\n"; 10254b3e95d5SJeremy L Thompson code << " r_q" << var_suffix << "[q + j*" << Q_name << "] = r_s" << var_suffix << "[j];\n"; 10264b3e95d5SJeremy L Thompson code << " }\n"; 10278b97b69aSJeremy L Thompson break; 10284b3e95d5SJeremy L Thompson case CEED_EVAL_INTERP: 10294b3e95d5SJeremy L Thompson code << " for (CeedInt j = 0; j < num_comp" << var_suffix << " ; j++) {\n"; 10304b3e95d5SJeremy L Thompson code << " r_q" << var_suffix << "[q + j*" << Q_name << "] = r_s" << var_suffix << "[j];\n"; 10314b3e95d5SJeremy L Thompson code << " }\n"; 10324b3e95d5SJeremy L Thompson break; 10334b3e95d5SJeremy L Thompson case CEED_EVAL_GRAD: 103499421279SJeremy L Thompson code << " GradColloSliceTranspose3d<num_comp" << var_suffix << ", " << Q_name << ", OP_T_1D>(data, q, r_s" << var_suffix << ", s_G" 1035f815fac9SJeremy L Thompson << var_suffix << ", r_q" << var_suffix << ");\n"; 10364b3e95d5SJeremy L Thompson break; 10374b3e95d5SJeremy L Thompson // LCOV_EXCL_START 10384b3e95d5SJeremy L Thompson case CEED_EVAL_WEIGHT: 10394b3e95d5SJeremy L Thompson break; // Should not occur 10404b3e95d5SJeremy L Thompson case CEED_EVAL_DIV: 10414b3e95d5SJeremy L Thompson case CEED_EVAL_CURL: 10424b3e95d5SJeremy L Thompson break; // TODO: Not implemented 10434b3e95d5SJeremy L Thompson // LCOV_EXCL_STOP 10444b3e95d5SJeremy L Thompson } 10454b3e95d5SJeremy L Thompson } 10464b3e95d5SJeremy L Thompson } 10474b3e95d5SJeremy L Thompson code << " }\n"; 10484b3e95d5SJeremy L Thompson return CEED_ERROR_SUCCESS; 10494b3e95d5SJeremy L Thompson } 10504b3e95d5SJeremy L Thompson 10514b3e95d5SJeremy L Thompson //------------------------------------------------------------------------------ 1052b2165e7aSSebastian Grimberg // Build single operator kernel 1053ab213215SJeremy L Thompson //------------------------------------------------------------------------------ 1054ddae5012SJeremy L Thompson extern "C" int CeedOperatorBuildKernel_Cuda_gen(CeedOperator op, bool *is_good_build) { 1055412e5683SJeremy L Thompson bool is_all_tensor = true, is_all_nontensor = true, is_at_points = false, use_3d_slices = false; 1056241a4b83SYohann Ceed ceed; 1057efa41df3SJeremy L Thompson CeedInt Q = 0, Q_1d = 0, num_input_fields, num_output_fields, max_dim = 1, max_num_points = 0, coords_comp_stride = 0; 1058ca735530SJeremy L Thompson CeedQFunctionField *qf_input_fields, *qf_output_fields; 1059ca735530SJeremy L Thompson CeedQFunction_Cuda_gen *qf_data; 1060ca735530SJeremy L Thompson CeedQFunction qf; 1061ca735530SJeremy L Thompson CeedOperatorField *op_input_fields, *op_output_fields; 1062ca735530SJeremy L Thompson CeedOperator_Cuda_gen *data; 10634b3e95d5SJeremy L Thompson std::ostringstream code; 10644b3e95d5SJeremy L Thompson 1065ddae5012SJeremy L Thompson CeedCallBackend(CeedOperatorGetData(op, &data)); 10664b3e95d5SJeremy L Thompson { 10674b3e95d5SJeremy L Thompson bool is_setup_done; 1068ca735530SJeremy L Thompson 1069ca735530SJeremy L Thompson CeedCallBackend(CeedOperatorIsSetupDone(op, &is_setup_done)); 1070ddae5012SJeremy L Thompson if (is_setup_done) { 1071ddae5012SJeremy L Thompson *is_good_build = !data->use_fallback; 1072ddae5012SJeremy L Thompson return CEED_ERROR_SUCCESS; 1073ddae5012SJeremy L Thompson } 1074ddae5012SJeremy L Thompson } 1075ddae5012SJeremy L Thompson 1076ddae5012SJeremy L Thompson // Check field compatibility 10778d12f40eSJeremy L Thompson CeedCallBackend(CeedOperatorGetFields(op, &num_input_fields, &op_input_fields, &num_output_fields, &op_output_fields)); 1078ddae5012SJeremy L Thompson { 1079412e5683SJeremy L Thompson bool has_shared_bases = true; 1080ddae5012SJeremy L Thompson 1081ddae5012SJeremy L Thompson for (CeedInt i = 0; i < num_input_fields; i++) { 1082ddae5012SJeremy L Thompson CeedBasis basis; 1083ddae5012SJeremy L Thompson 1084ddae5012SJeremy L Thompson CeedCallBackend(CeedOperatorFieldGetBasis(op_input_fields[i], &basis)); 1085ddae5012SJeremy L Thompson if (basis != CEED_BASIS_NONE) { 1086ddae5012SJeremy L Thompson bool is_tensor = true; 1087ddae5012SJeremy L Thompson const char *resource; 1088ddae5012SJeremy L Thompson char *resource_root; 1089ddae5012SJeremy L Thompson Ceed basis_ceed; 1090ddae5012SJeremy L Thompson 1091ddae5012SJeremy L Thompson CeedCallBackend(CeedBasisIsTensor(basis, &is_tensor)); 1092c9192acaSJeremy L Thompson is_all_tensor = is_all_tensor && is_tensor; 109345a787f7SJeremy L Thompson is_all_nontensor = is_all_nontensor && !is_tensor; 1094ddae5012SJeremy L Thompson CeedCallBackend(CeedBasisGetCeed(basis, &basis_ceed)); 1095ddae5012SJeremy L Thompson CeedCallBackend(CeedGetResource(basis_ceed, &resource)); 1096ddae5012SJeremy L Thompson CeedCallBackend(CeedGetResourceRoot(basis_ceed, resource, ":", &resource_root)); 1097c9192acaSJeremy L Thompson has_shared_bases = has_shared_bases && !strcmp(resource_root, "/gpu/cuda/shared"); 1098ddae5012SJeremy L Thompson CeedCallBackend(CeedFree(&resource_root)); 1099ddae5012SJeremy L Thompson CeedCallBackend(CeedDestroy(&basis_ceed)); 1100ddae5012SJeremy L Thompson } 1101ddae5012SJeremy L Thompson CeedCallBackend(CeedBasisDestroy(&basis)); 11024b3e95d5SJeremy L Thompson } 1103ca735530SJeremy L Thompson 1104ddae5012SJeremy L Thompson for (CeedInt i = 0; i < num_output_fields; i++) { 1105ddae5012SJeremy L Thompson CeedBasis basis; 1106ddae5012SJeremy L Thompson 1107ddae5012SJeremy L Thompson CeedCallBackend(CeedOperatorFieldGetBasis(op_output_fields[i], &basis)); 1108ddae5012SJeremy L Thompson if (basis != CEED_BASIS_NONE) { 1109ddae5012SJeremy L Thompson bool is_tensor = true; 1110ddae5012SJeremy L Thompson const char *resource; 1111ddae5012SJeremy L Thompson char *resource_root; 1112ddae5012SJeremy L Thompson Ceed basis_ceed; 1113ddae5012SJeremy L Thompson 1114ddae5012SJeremy L Thompson CeedCallBackend(CeedBasisIsTensor(basis, &is_tensor)); 1115c9192acaSJeremy L Thompson is_all_tensor = is_all_tensor && is_tensor; 1116c9192acaSJeremy L Thompson is_all_nontensor = is_all_nontensor && !is_tensor; 1117ddae5012SJeremy L Thompson 1118ddae5012SJeremy L Thompson CeedCallBackend(CeedBasisGetCeed(basis, &basis_ceed)); 1119ddae5012SJeremy L Thompson CeedCallBackend(CeedGetResource(basis_ceed, &resource)); 1120ddae5012SJeremy L Thompson CeedCallBackend(CeedGetResourceRoot(basis_ceed, resource, ":", &resource_root)); 1121c9192acaSJeremy L Thompson has_shared_bases = has_shared_bases && !strcmp(resource_root, "/gpu/cuda/shared"); 1122ddae5012SJeremy L Thompson CeedCallBackend(CeedFree(&resource_root)); 1123ddae5012SJeremy L Thompson CeedCallBackend(CeedDestroy(&basis_ceed)); 1124ddae5012SJeremy L Thompson } 1125ddae5012SJeremy L Thompson CeedCallBackend(CeedBasisDestroy(&basis)); 1126ddae5012SJeremy L Thompson } 1127ddae5012SJeremy L Thompson // -- Fallback to ref if not all bases are shared 1128412e5683SJeremy L Thompson if (!has_shared_bases) { 1129ddae5012SJeremy L Thompson *is_good_build = false; 1130ddae5012SJeremy L Thompson return CEED_ERROR_SUCCESS; 1131ddae5012SJeremy L Thompson } 1132ddae5012SJeremy L Thompson } 1133ca735530SJeremy L Thompson CeedCallBackend(CeedOperatorGetCeed(op, &ceed)); 1134ca735530SJeremy L Thompson CeedCallBackend(CeedOperatorGetQFunction(op, &qf)); 1135ca735530SJeremy L Thompson CeedCallBackend(CeedQFunctionGetData(qf, &qf_data)); 1136ca735530SJeremy L Thompson CeedCallBackend(CeedQFunctionGetFields(qf, NULL, &qf_input_fields, NULL, &qf_output_fields)); 1137241a4b83SYohann 11384b3e95d5SJeremy L Thompson // Get operator data 11398b97b69aSJeremy L Thompson CeedCallBackend(CeedOperatorIsAtPoints(op, &is_at_points)); 1140412e5683SJeremy L Thompson { 1141efa41df3SJeremy L Thompson CeedInt max_P = 0, max_P_1d = 0; 1142412e5683SJeremy L Thompson 1143412e5683SJeremy L Thompson CeedCallBackend(CeedOperatorBuildKernelData_Cuda_gen(ceed, num_input_fields, op_input_fields, qf_input_fields, num_output_fields, 1144412e5683SJeremy L Thompson op_output_fields, qf_output_fields, &max_P, &max_P_1d, &Q, &Q_1d, &max_dim, &is_all_tensor, 1145412e5683SJeremy L Thompson &use_3d_slices)); 1146412e5683SJeremy L Thompson data->max_P_1d = is_all_tensor ? max_P_1d : max_P; 1147412e5683SJeremy L Thompson } 1148412e5683SJeremy L Thompson if (max_dim == 0) max_dim = 1; 1149412e5683SJeremy L Thompson data->dim = max_dim; 11508b97b69aSJeremy L Thompson if (is_at_points) { 11518b97b69aSJeremy L Thompson CeedElemRestriction_Cuda *rstr_data; 11528b97b69aSJeremy L Thompson CeedElemRestriction rstr_points = NULL; 11534b3e95d5SJeremy L Thompson 11548b97b69aSJeremy L Thompson CeedCallBackend(CeedOperatorAtPointsGetPoints(op, &rstr_points, NULL)); 11558b97b69aSJeremy L Thompson CeedCallBackend(CeedElemRestrictionGetMaxPointsInElement(rstr_points, &max_num_points)); 11568b97b69aSJeremy L Thompson CeedCallBackend(CeedElemRestrictionGetCompStride(rstr_points, &coords_comp_stride)); 11578b97b69aSJeremy L Thompson CeedCallBackend(CeedElemRestrictionGetData(rstr_points, &rstr_data)); 11588b97b69aSJeremy L Thompson data->points.indices = (CeedInt *)rstr_data->d_offsets; 11598b97b69aSJeremy L Thompson CeedCallBackend(CeedElemRestrictionDestroy(&rstr_points)); 11608b97b69aSJeremy L Thompson } 11618b97b69aSJeremy L Thompson if (is_at_points) use_3d_slices = false; 11628b97b69aSJeremy L Thompson if (Q_1d == 0) { 11638b97b69aSJeremy L Thompson if (is_at_points) Q_1d = max_num_points; 11648b97b69aSJeremy L Thompson else CeedCallBackend(CeedOperatorGetNumQuadraturePoints(op, &Q_1d)); 11654b3e95d5SJeremy L Thompson } 1166c433aabcSJeremy L Thompson if (Q == 0) Q = Q_1d; 1167c433aabcSJeremy L Thompson data->Q = Q; 11684b3e95d5SJeremy L Thompson data->Q_1d = Q_1d; 11694b3e95d5SJeremy L Thompson 11700b454692Sjeremylt // Check for restriction only identity operator 11714b3e95d5SJeremy L Thompson { 11724b3e95d5SJeremy L Thompson bool is_identity_qf; 11734b3e95d5SJeremy L Thompson 11742b730f8bSJeremy L Thompson CeedCallBackend(CeedQFunctionIsIdentity(qf, &is_identity_qf)); 11750b454692Sjeremylt if (is_identity_qf) { 11769e201c85SYohann CeedEvalMode eval_mode_in, eval_mode_out; 1177ca735530SJeremy L Thompson 11782b730f8bSJeremy L Thompson CeedCallBackend(CeedQFunctionFieldGetEvalMode(qf_input_fields[0], &eval_mode_in)); 11792b730f8bSJeremy L Thompson CeedCallBackend(CeedQFunctionFieldGetEvalMode(qf_output_fields[0], &eval_mode_out)); 11806574a04fSJeremy L Thompson CeedCheck(eval_mode_in != CEED_EVAL_NONE || eval_mode_out != CEED_EVAL_NONE, ceed, CEED_ERROR_BACKEND, 11816574a04fSJeremy L Thompson "Backend does not implement restriction only identity operators"); 11820b454692Sjeremylt } 11834b3e95d5SJeremy L Thompson } 11840b454692Sjeremylt 1185f1a13f77SYohann Dudouit // Add atomicAdd function for old NVidia architectures 11864b3e95d5SJeremy L Thompson { 11874b3e95d5SJeremy L Thompson Ceed_Cuda *ceed_data; 11884b3e95d5SJeremy L Thompson struct cudaDeviceProp prop; 11894b3e95d5SJeremy L Thompson 11902b730f8bSJeremy L Thompson CeedCallBackend(CeedGetData(ceed, &ceed_data)); 11912b730f8bSJeremy L Thompson CeedCallBackend(cudaGetDeviceProperties(&prop, ceed_data->device_id)); 119280a9ef05SNatalie Beams if ((prop.major < 6) && (CEED_SCALAR_TYPE != CEED_SCALAR_FP32)) { 11939c25dd66SJeremy L Thompson code << "// AtomicAdd fallback source\n"; 11949c25dd66SJeremy L Thompson code << "#include <ceed/jit-source/cuda/cuda-atomic-add-fallback.h>\n\n"; 1195f1a13f77SYohann Dudouit } 11964b3e95d5SJeremy L Thompson } 1197f1a13f77SYohann Dudouit 11989e201c85SYohann // Load basis source files 1199412e5683SJeremy L Thompson if (!is_all_nontensor) { 12009c25dd66SJeremy L Thompson code << "// Tensor basis source\n"; 12019c25dd66SJeremy L Thompson code << "#include <ceed/jit-source/cuda/cuda-shared-basis-tensor-templates.h>\n\n"; 1202412e5683SJeremy L Thompson } 1203412e5683SJeremy L Thompson if (!is_all_tensor) { 1204dc007f05SJeremy L Thompson code << "// Non-tensor basis source\n"; 1205dc007f05SJeremy L Thompson code << "#include <ceed/jit-source/cuda/cuda-shared-basis-nontensor-templates.h>\n\n"; 1206dc007f05SJeremy L Thompson } 1207c8e372f0SJeremy L Thompson if (!is_all_tensor && !is_all_nontensor) { 1208c8e372f0SJeremy L Thompson code << "// Tensor basis source\n"; 1209c8e372f0SJeremy L Thompson code << "#include <ceed/jit-source/cuda/cuda-shared-basis-tensor-flattened-templates.h>\n\n"; 1210c8e372f0SJeremy L Thompson } 1211dc007f05SJeremy L Thompson if (is_at_points) { 12128b97b69aSJeremy L Thompson code << "// AtPoints basis source\n"; 12138b97b69aSJeremy L Thompson code << "#include <ceed/jit-source/cuda/cuda-shared-basis-tensor-at-points-templates.h>\n\n"; 1214dc007f05SJeremy L Thompson } 12159c25dd66SJeremy L Thompson code << "// CodeGen operator source\n"; 12169c25dd66SJeremy L Thompson code << "#include <ceed/jit-source/cuda/cuda-gen-templates.h>\n\n"; 1217241a4b83SYohann 12184b3e95d5SJeremy L Thompson // Get QFunction name 12194b3e95d5SJeremy L Thompson std::string qfunction_name(qf_data->qfunction_name); 12204b3e95d5SJeremy L Thompson std::string operator_name; 12214b3e95d5SJeremy L Thompson 122209095acaSJeremy L Thompson operator_name = "CeedKernelCudaGenOperator_" + qfunction_name; 12234d537eeaSYohann 12249e201c85SYohann // Define CEED_Q_VLA 12259e201c85SYohann code << "\n#undef CEED_Q_VLA\n"; 1226412e5683SJeremy L Thompson if (max_dim != 3 || is_at_points || use_3d_slices || !is_all_tensor) { 12279e201c85SYohann code << "#define CEED_Q_VLA 1\n\n"; 12289e201c85SYohann } else { 12299e201c85SYohann code << "#define CEED_Q_VLA " << Q_1d << "\n\n"; 12309e201c85SYohann } 12319e201c85SYohann 12324b3e95d5SJeremy L Thompson // Add user QFunction source 12334b3e95d5SJeremy L Thompson { 12349c25dd66SJeremy L Thompson const char *source_path; 12354b3e95d5SJeremy L Thompson 12369c25dd66SJeremy L Thompson CeedCallBackend(CeedQFunctionGetSourcePath(qf, &source_path)); 12379c25dd66SJeremy L Thompson CeedCheck(source_path, ceed, CEED_ERROR_UNSUPPORTED, "/gpu/cuda/gen backend requires QFunction source code file"); 12389c25dd66SJeremy L Thompson 12399c25dd66SJeremy L Thompson code << "// User QFunction source\n"; 12409c25dd66SJeremy L Thompson code << "#include \"" << source_path << "\"\n\n"; 12414b3e95d5SJeremy L Thompson } 1242241a4b83SYohann 1243241a4b83SYohann // Setup 1244818e0025SJeremy L Thompson code << "\n// -----------------------------------------------------------------------------\n"; 12454b3e95d5SJeremy L Thompson code << "// Operator Kernel\n"; 12464b3e95d5SJeremy L Thompson code << "// \n"; 12474b3e95d5SJeremy L Thompson code << "// d_[in,out]_i: CeedVector device array\n"; 12484b3e95d5SJeremy L Thompson code << "// r_[in,out]_e_i: Element vector register\n"; 12494b3e95d5SJeremy L Thompson code << "// r_[in,out]_q_i: Quadrature space vector register\n"; 12509123fb08SJeremy L Thompson code << "// r_[in,out]_c_i: AtPoints Chebyshev coefficients register\n"; 12514b3e95d5SJeremy L Thompson code << "// r_[in,out]_s_i: Quadrature space slice vector register\n"; 12524b3e95d5SJeremy L Thompson code << "// \n"; 12534b3e95d5SJeremy L Thompson code << "// s_B_[in,out]_i: Interpolation matrix, shared memory\n"; 12544b3e95d5SJeremy L Thompson code << "// s_G_[in,out]_i: Gradient matrix, shared memory\n"; 12554b3e95d5SJeremy L Thompson code << "// -----------------------------------------------------------------------------\n"; 12564b3e95d5SJeremy L Thompson code << "extern \"C\" __global__ void " << operator_name 12578b97b69aSJeremy L Thompson << "(CeedInt num_elem, void* ctx, FieldsInt_Cuda indices, Fields_Cuda fields, Fields_Cuda B, Fields_Cuda G, CeedScalar *W, Points_Cuda " 12588b97b69aSJeremy L Thompson "points) {\n"; 12594b3e95d5SJeremy L Thompson 12604b3e95d5SJeremy L Thompson // Scratch buffers 12619e201c85SYohann for (CeedInt i = 0; i < num_input_fields; i++) { 12624b3e95d5SJeremy L Thompson CeedEvalMode eval_mode; 12634b3e95d5SJeremy L Thompson 12642b730f8bSJeremy L Thompson CeedCallBackend(CeedQFunctionFieldGetEvalMode(qf_input_fields[i], &eval_mode)); 12659e201c85SYohann if (eval_mode != CEED_EVAL_WEIGHT) { // Skip CEED_EVAL_WEIGHT 1266826538b3SJeremy L Thompson code << " const CeedScalar *__restrict__ d_in_" << i << " = fields.inputs[" << i << "];\n"; 1267241a4b83SYohann } 1268241a4b83SYohann } 12699e201c85SYohann for (CeedInt i = 0; i < num_output_fields; i++) { 1270826538b3SJeremy L Thompson code << " CeedScalar *__restrict__ d_out_" << i << " = fields.outputs[" << i << "];\n"; 1271241a4b83SYohann } 12721da99368SJeremy L Thompson 1273412e5683SJeremy L Thompson code << " const CeedInt max_dim = " << max_dim << ";\n"; 1274412e5683SJeremy L Thompson if (!is_all_tensor) { 1275412e5683SJeremy L Thompson code << " const CeedInt Q = " << Q << ";\n"; 1276412e5683SJeremy L Thompson } 1277412e5683SJeremy L Thompson if (!is_all_nontensor) { 1278412e5683SJeremy L Thompson code << " const CeedInt Q_1d = " << Q_1d << ";\n"; 1279412e5683SJeremy L Thompson } 12808b97b69aSJeremy L Thompson if (is_at_points) { 12818b97b69aSJeremy L Thompson code << " const CeedInt max_num_points = " << max_num_points << ";\n"; 12828b97b69aSJeremy L Thompson code << " const CeedInt coords_comp_stride = " << coords_comp_stride << ";\n"; 12838b97b69aSJeremy L Thompson } 12841da99368SJeremy L Thompson 12854b3e95d5SJeremy L Thompson // Shared data 1286241a4b83SYohann code << " extern __shared__ CeedScalar slice[];\n"; 12879e201c85SYohann code << " SharedData_Cuda data;\n"; 12889e201c85SYohann code << " data.t_id_x = threadIdx.x;\n"; 12899e201c85SYohann code << " data.t_id_y = threadIdx.y;\n"; 12909e201c85SYohann code << " data.t_id_z = threadIdx.z;\n"; 12919e201c85SYohann code << " data.t_id = threadIdx.x + threadIdx.y*blockDim.x + threadIdx.z*blockDim.y*blockDim.x;\n"; 1292412e5683SJeremy L Thompson code << " data.slice = slice + data.t_id_z*OP_T_1D" << ((!is_all_tensor || max_dim == 1) ? "" : "*OP_T_1D") << ";\n"; 1293920dcdc4Sjeremylt 12940a2a6492SJeremy L Thompson // -- Determine input mat reuse 129545a787f7SJeremy L Thompson FieldReuse_Cuda input_matrix_reuse[CEED_FIELD_MAX]; 12960a2a6492SJeremy L Thompson 12970a2a6492SJeremy L Thompson for (CeedInt i = 0; i < num_input_fields; i++) { 129845a787f7SJeremy L Thompson input_matrix_reuse[i].index = -1; 12990a2a6492SJeremy L Thompson } 13000a2a6492SJeremy L Thompson for (CeedInt i = 0; i < num_input_fields; i++) { 1301412e5683SJeremy L Thompson bool is_tensor = true; 13020a2a6492SJeremy L Thompson CeedEvalMode eval_mode_i; 13030a2a6492SJeremy L Thompson CeedBasis basis_i; 13040a2a6492SJeremy L Thompson 13050a2a6492SJeremy L Thompson CeedCallBackend(CeedQFunctionFieldGetEvalMode(qf_input_fields[i], &eval_mode_i)); 13060a2a6492SJeremy L Thompson if (eval_mode_i == CEED_EVAL_WEIGHT) continue; 13070a2a6492SJeremy L Thompson CeedCallBackend(CeedOperatorFieldGetBasis(op_input_fields[i], &basis_i)); 1308412e5683SJeremy L Thompson CeedCallBackend(CeedBasisIsTensor(basis_i, &is_tensor)); 130945a787f7SJeremy L Thompson for (CeedInt j = 0; (input_matrix_reuse[i].index == -1) && (j < i); j++) { 13100a2a6492SJeremy L Thompson CeedEvalMode eval_mode_j; 13110a2a6492SJeremy L Thompson CeedBasis basis_j; 13120a2a6492SJeremy L Thompson 13130a2a6492SJeremy L Thompson CeedCallBackend(CeedQFunctionFieldGetEvalMode(qf_input_fields[j], &eval_mode_j)); 13140a2a6492SJeremy L Thompson if (eval_mode_j == CEED_EVAL_WEIGHT) continue; 13150a2a6492SJeremy L Thompson CeedCallBackend(CeedOperatorFieldGetBasis(op_input_fields[j], &basis_j)); 13160a2a6492SJeremy L Thompson if (basis_i == basis_j) { 13170a2a6492SJeremy L Thompson if (is_tensor) { 131845a787f7SJeremy L Thompson input_matrix_reuse[i].index = j; 131945a787f7SJeremy L Thompson input_matrix_reuse[i].is_input = true; 132045a787f7SJeremy L Thompson input_matrix_reuse[i].eval_mode = eval_mode_j; 13210a2a6492SJeremy L Thompson } else { 13220a2a6492SJeremy L Thompson // For non-tensor can only re-use with the same eval mode 13230a2a6492SJeremy L Thompson if (eval_mode_i == eval_mode_j) { 132445a787f7SJeremy L Thompson input_matrix_reuse[i].index = j; 132545a787f7SJeremy L Thompson input_matrix_reuse[i].is_input = true; 132645a787f7SJeremy L Thompson input_matrix_reuse[i].eval_mode = eval_mode_j; 13270a2a6492SJeremy L Thompson } 13280a2a6492SJeremy L Thompson } 13290a2a6492SJeremy L Thompson } 13300a2a6492SJeremy L Thompson CeedCallBackend(CeedBasisDestroy(&basis_j)); 13310a2a6492SJeremy L Thompson } 13320a2a6492SJeremy L Thompson CeedCallBackend(CeedBasisDestroy(&basis_i)); 13330a2a6492SJeremy L Thompson } 13340a2a6492SJeremy L Thompson 13350a2a6492SJeremy L Thompson // -- Determine output mat reuse 133645a787f7SJeremy L Thompson FieldReuse_Cuda output_matrix_reuse[CEED_FIELD_MAX]; 13370a2a6492SJeremy L Thompson 13380a2a6492SJeremy L Thompson for (CeedInt i = 0; i < num_output_fields; i++) { 133945a787f7SJeremy L Thompson output_matrix_reuse[i].index = -1; 13400a2a6492SJeremy L Thompson } 13410a2a6492SJeremy L Thompson for (CeedInt i = 0; i < num_output_fields; i++) { 1342412e5683SJeremy L Thompson bool is_tensor = true; 13430a2a6492SJeremy L Thompson CeedEvalMode eval_mode_i; 13440a2a6492SJeremy L Thompson CeedBasis basis_i; 13450a2a6492SJeremy L Thompson 13460a2a6492SJeremy L Thompson CeedCallBackend(CeedQFunctionFieldGetEvalMode(qf_output_fields[i], &eval_mode_i)); 13470a2a6492SJeremy L Thompson CeedCallBackend(CeedOperatorFieldGetBasis(op_output_fields[i], &basis_i)); 1348412e5683SJeremy L Thompson CeedCallBackend(CeedBasisIsTensor(basis_i, &is_tensor)); 134945a787f7SJeremy L Thompson for (CeedInt j = 0; (output_matrix_reuse[i].index == -1) && (j < num_input_fields); j++) { 13500a2a6492SJeremy L Thompson CeedEvalMode eval_mode_j; 13510a2a6492SJeremy L Thompson CeedBasis basis_j; 13520a2a6492SJeremy L Thompson 13530a2a6492SJeremy L Thompson CeedCallBackend(CeedQFunctionFieldGetEvalMode(qf_input_fields[j], &eval_mode_j)); 13540a2a6492SJeremy L Thompson if (eval_mode_j == CEED_EVAL_WEIGHT) continue; 13550a2a6492SJeremy L Thompson CeedCallBackend(CeedOperatorFieldGetBasis(op_input_fields[j], &basis_j)); 13560a2a6492SJeremy L Thompson if (basis_i == basis_j) { 13570a2a6492SJeremy L Thompson if (is_tensor) { 135845a787f7SJeremy L Thompson output_matrix_reuse[i].index = j; 135945a787f7SJeremy L Thompson output_matrix_reuse[i].is_input = true; 136045a787f7SJeremy L Thompson output_matrix_reuse[i].eval_mode = eval_mode_j; 13610a2a6492SJeremy L Thompson } else { 13620a2a6492SJeremy L Thompson // For non-tensor can only re-use with the same eval mode 13630a2a6492SJeremy L Thompson if (eval_mode_i == eval_mode_j) { 136445a787f7SJeremy L Thompson output_matrix_reuse[i].index = j; 136545a787f7SJeremy L Thompson output_matrix_reuse[i].is_input = true; 136645a787f7SJeremy L Thompson output_matrix_reuse[i].eval_mode = eval_mode_j; 13670a2a6492SJeremy L Thompson } 13680a2a6492SJeremy L Thompson } 13690a2a6492SJeremy L Thompson } 13700a2a6492SJeremy L Thompson CeedCallBackend(CeedBasisDestroy(&basis_j)); 13710a2a6492SJeremy L Thompson } 137245a787f7SJeremy L Thompson for (CeedInt j = 0; (output_matrix_reuse[i].index == -1) && (j < i); j++) { 13730a2a6492SJeremy L Thompson CeedEvalMode eval_mode_j; 13740a2a6492SJeremy L Thompson CeedBasis basis_j; 13750a2a6492SJeremy L Thompson 13760a2a6492SJeremy L Thompson CeedCallBackend(CeedQFunctionFieldGetEvalMode(qf_output_fields[j], &eval_mode_j)); 13770a2a6492SJeremy L Thompson if (eval_mode_j == CEED_EVAL_WEIGHT) continue; 13780a2a6492SJeremy L Thompson CeedCallBackend(CeedOperatorFieldGetBasis(op_output_fields[j], &basis_j)); 13790a2a6492SJeremy L Thompson if (basis_i == basis_j) { 13800a2a6492SJeremy L Thompson if (is_tensor) { 138145a787f7SJeremy L Thompson output_matrix_reuse[i].index = j; 138245a787f7SJeremy L Thompson output_matrix_reuse[i].is_input = false; 138345a787f7SJeremy L Thompson output_matrix_reuse[i].eval_mode = eval_mode_j; 13840a2a6492SJeremy L Thompson } else { 13850a2a6492SJeremy L Thompson // For non-tensor can only re-use with the same eval mode 13860a2a6492SJeremy L Thompson if (eval_mode_i == eval_mode_j) { 138745a787f7SJeremy L Thompson output_matrix_reuse[i].index = j; 138845a787f7SJeremy L Thompson output_matrix_reuse[i].is_input = false; 138945a787f7SJeremy L Thompson output_matrix_reuse[i].eval_mode = eval_mode_j; 13900a2a6492SJeremy L Thompson } 13910a2a6492SJeremy L Thompson } 13920a2a6492SJeremy L Thompson } 13930a2a6492SJeremy L Thompson CeedCallBackend(CeedBasisDestroy(&basis_j)); 13940a2a6492SJeremy L Thompson } 13950a2a6492SJeremy L Thompson CeedCallBackend(CeedBasisDestroy(&basis_i)); 13960a2a6492SJeremy L Thompson } 13970a2a6492SJeremy L Thompson 1398ac421f39SYohann // Initialize constants, and matrices B and G 13994b3e95d5SJeremy L Thompson code << "\n // Input field constants and basis data\n"; 14009e201c85SYohann for (CeedInt i = 0; i < num_input_fields; i++) { 14018014c5e7SJeremy L Thompson CeedCallBackend(CeedOperatorBuildKernelFieldData_Cuda_gen(code, data, i, op_input_fields[i], qf_input_fields[i], input_matrix_reuse[i], max_dim, 14028014c5e7SJeremy L Thompson Q, Q_1d, true, is_all_tensor, is_at_points, use_3d_slices)); 1403920dcdc4Sjeremylt } 14044b3e95d5SJeremy L Thompson code << "\n // Output field constants and basis data\n"; 14059e201c85SYohann for (CeedInt i = 0; i < num_output_fields; i++) { 14068014c5e7SJeremy L Thompson CeedCallBackend(CeedOperatorBuildKernelFieldData_Cuda_gen(code, data, i, op_output_fields[i], qf_output_fields[i], output_matrix_reuse[i], 14078014c5e7SJeremy L Thompson max_dim, Q, Q_1d, false, is_all_tensor, is_at_points, use_3d_slices)); 14084b3e95d5SJeremy L Thompson } 1409920dcdc4Sjeremylt 14104b3e95d5SJeremy L Thompson // Loop over all elements 14114b3e95d5SJeremy L Thompson code << "\n // Element loop\n"; 1412ac421f39SYohann code << " __syncthreads();\n"; 14139e201c85SYohann code << " for (CeedInt elem = blockIdx.x*blockDim.z + threadIdx.z; elem < num_elem; elem += gridDim.x*blockDim.z) {\n"; 14144b3e95d5SJeremy L Thompson 1415e93651e5SJeremy L Thompson // -- Compute minimum buffer space needed 14168b97b69aSJeremy L Thompson CeedInt max_rstr_buffer_size = 1; 1417e93651e5SJeremy L Thompson 14189e201c85SYohann for (CeedInt i = 0; i < num_input_fields; i++) { 1419*6de40545SJeremy L Thompson CeedEvalMode eval_mode; 1420*6de40545SJeremy L Thompson 1421*6de40545SJeremy L Thompson CeedCallBackend(CeedQFunctionFieldGetEvalMode(qf_input_fields[i], &eval_mode)); 1422*6de40545SJeremy L Thompson if (eval_mode != CEED_EVAL_NONE && eval_mode != CEED_EVAL_WEIGHT) { 1423a61b1c91SJeremy L Thompson CeedInt num_comp; 1424e93651e5SJeremy L Thompson CeedElemRestriction elem_rstr; 1425e93651e5SJeremy L Thompson 1426e93651e5SJeremy L Thompson CeedCallBackend(CeedOperatorFieldGetElemRestriction(op_input_fields[i], &elem_rstr)); 1427e93651e5SJeremy L Thompson CeedCallBackend(CeedElemRestrictionGetNumComponents(elem_rstr, &num_comp)); 1428a61b1c91SJeremy L Thompson max_rstr_buffer_size = CeedIntMax(max_rstr_buffer_size, num_comp * (is_all_tensor && (max_dim >= 3) ? Q_1d : 1)); 1429681d0ea7SJeremy L Thompson CeedCallBackend(CeedElemRestrictionDestroy(&elem_rstr)); 1430e93651e5SJeremy L Thompson } 1431*6de40545SJeremy L Thompson } 1432e93651e5SJeremy L Thompson for (CeedInt i = 0; i < num_output_fields; i++) { 1433*6de40545SJeremy L Thompson CeedEvalMode eval_mode; 1434*6de40545SJeremy L Thompson 1435*6de40545SJeremy L Thompson CeedCallBackend(CeedQFunctionFieldGetEvalMode(qf_output_fields[i], &eval_mode)); 1436*6de40545SJeremy L Thompson if (eval_mode != CEED_EVAL_NONE) { 1437a61b1c91SJeremy L Thompson CeedInt num_comp; 1438e93651e5SJeremy L Thompson CeedElemRestriction elem_rstr; 1439e93651e5SJeremy L Thompson 1440e93651e5SJeremy L Thompson CeedCallBackend(CeedOperatorFieldGetElemRestriction(op_output_fields[i], &elem_rstr)); 1441e93651e5SJeremy L Thompson CeedCallBackend(CeedElemRestrictionGetNumComponents(elem_rstr, &num_comp)); 1442a61b1c91SJeremy L Thompson max_rstr_buffer_size = CeedIntMax(max_rstr_buffer_size, num_comp * (is_all_tensor && (max_dim >= 3) ? Q_1d : 1)); 1443681d0ea7SJeremy L Thompson CeedCallBackend(CeedElemRestrictionDestroy(&elem_rstr)); 1444e93651e5SJeremy L Thompson } 1445*6de40545SJeremy L Thompson } 1446e93651e5SJeremy L Thompson code << " // Scratch restriction buffer space\n"; 1447e93651e5SJeremy L Thompson code << " CeedScalar r_e_scratch[" << max_rstr_buffer_size << "];\n"; 1448e93651e5SJeremy L Thompson 1449e93651e5SJeremy L Thompson // -- Determine best input field processing order 1450e93651e5SJeremy L Thompson CeedInt field_rstr_in_buffer[CEED_FIELD_MAX], input_field_order[CEED_FIELD_MAX]; 1451e93651e5SJeremy L Thompson 1452e93651e5SJeremy L Thompson for (CeedInt i = 0; i < num_input_fields; i++) { 1453e93651e5SJeremy L Thompson field_rstr_in_buffer[i] = -1; 1454e93651e5SJeremy L Thompson input_field_order[i] = -1; 1455e93651e5SJeremy L Thompson } 1456e93651e5SJeremy L Thompson { 1457e93651e5SJeremy L Thompson bool is_ordered[CEED_FIELD_MAX]; 1458e93651e5SJeremy L Thompson CeedInt curr_index = 0; 1459e93651e5SJeremy L Thompson 1460e93651e5SJeremy L Thompson for (CeedInt i = 0; i < num_input_fields; i++) is_ordered[i] = false; 1461e93651e5SJeremy L Thompson for (CeedInt i = 0; i < num_input_fields; i++) { 1462e93651e5SJeremy L Thompson CeedVector vec_i; 1463e93651e5SJeremy L Thompson CeedElemRestriction rstr_i; 1464e93651e5SJeremy L Thompson 1465e93651e5SJeremy L Thompson if (is_ordered[i]) continue; 1466e93651e5SJeremy L Thompson field_rstr_in_buffer[i] = i; 1467e93651e5SJeremy L Thompson is_ordered[i] = true; 1468e93651e5SJeremy L Thompson input_field_order[curr_index] = i; 1469e93651e5SJeremy L Thompson curr_index++; 1470034f99fdSJeremy L Thompson CeedCallBackend(CeedOperatorFieldGetVector(op_input_fields[i], &vec_i)); 1471e93651e5SJeremy L Thompson if (vec_i == CEED_VECTOR_NONE) continue; // CEED_EVAL_WEIGHT 1472e93651e5SJeremy L Thompson CeedCallBackend(CeedOperatorFieldGetElemRestriction(op_input_fields[i], &rstr_i)); 1473e93651e5SJeremy L Thompson for (CeedInt j = i + 1; j < num_input_fields; j++) { 1474e93651e5SJeremy L Thompson CeedVector vec_j; 1475e93651e5SJeremy L Thompson CeedElemRestriction rstr_j; 1476e93651e5SJeremy L Thompson 1477e93651e5SJeremy L Thompson CeedCallBackend(CeedOperatorFieldGetVector(op_input_fields[j], &vec_j)); 1478e93651e5SJeremy L Thompson CeedCallBackend(CeedOperatorFieldGetElemRestriction(op_input_fields[j], &rstr_j)); 1479e93651e5SJeremy L Thompson if (rstr_i == rstr_j && vec_i == vec_j) { 1480e93651e5SJeremy L Thompson field_rstr_in_buffer[j] = i; 1481e93651e5SJeremy L Thompson is_ordered[j] = true; 1482e93651e5SJeremy L Thompson input_field_order[curr_index] = j; 1483e93651e5SJeremy L Thompson curr_index++; 1484e93651e5SJeremy L Thompson } 1485681d0ea7SJeremy L Thompson CeedCallBackend(CeedVectorDestroy(&vec_j)); 1486681d0ea7SJeremy L Thompson CeedCallBackend(CeedElemRestrictionDestroy(&rstr_j)); 1487e93651e5SJeremy L Thompson } 1488681d0ea7SJeremy L Thompson CeedCallBackend(CeedVectorDestroy(&vec_i)); 1489681d0ea7SJeremy L Thompson CeedCallBackend(CeedElemRestrictionDestroy(&rstr_i)); 1490e93651e5SJeremy L Thompson } 1491e93651e5SJeremy L Thompson } 1492e93651e5SJeremy L Thompson 1493e93651e5SJeremy L Thompson // -- Input restriction and basis 1494e93651e5SJeremy L Thompson code << "\n // -- Input field restrictions and basis actions\n"; 1495e93651e5SJeremy L Thompson for (CeedInt i = 0; i < num_input_fields; i++) { 149659fa3f92SJeremy L Thompson const char *field_name; 149759fa3f92SJeremy L Thompson const CeedInt f = input_field_order[i]; 1498e93651e5SJeremy L Thompson 149959fa3f92SJeremy L Thompson CeedCallBackend(CeedOperatorFieldGetName(op_input_fields[f], &field_name)); 150059fa3f92SJeremy L Thompson code << " // ---- Input field " << f << ": " << field_name << "\n"; 1501920dcdc4Sjeremylt 15024b3e95d5SJeremy L Thompson // ---- Restriction 15038014c5e7SJeremy L Thompson CeedCallBackend(CeedOperatorBuildKernelRestriction_Cuda_gen(code, data, f, field_rstr_in_buffer, op_input_fields[f], qf_input_fields[f], max_dim, 1504412e5683SJeremy L Thompson Q_1d, true, is_all_tensor, is_at_points, use_3d_slices)); 15059a2291e3SJeremy L Thompson 15064b3e95d5SJeremy L Thompson // ---- Basis action 15078014c5e7SJeremy L Thompson CeedCallBackend(CeedOperatorBuildKernelBasis_Cuda_gen(code, data, f, op_input_fields[f], qf_input_fields[f], max_dim, Q_1d, true, is_all_tensor, 1508dc007f05SJeremy L Thompson is_at_points, use_3d_slices)); 1509920dcdc4Sjeremylt } 1510920dcdc4Sjeremylt 15114b3e95d5SJeremy L Thompson // -- Q function 1512412e5683SJeremy L Thompson CeedCallBackend(CeedOperatorBuildKernelQFunction_Cuda_gen(code, data, max_dim, max_num_points, num_input_fields, op_input_fields, qf_input_fields, 1513412e5683SJeremy L Thompson num_output_fields, op_output_fields, qf_output_fields, qfunction_name, Q_1d, 1514412e5683SJeremy L Thompson is_all_tensor, is_at_points, use_3d_slices)); 1515ca735530SJeremy L Thompson 15164b3e95d5SJeremy L Thompson // -- Output basis and restriction 15174b3e95d5SJeremy L Thompson code << "\n // -- Output field basis action and restrictions\n"; 15189e201c85SYohann for (CeedInt i = 0; i < num_output_fields; i++) { 151959fa3f92SJeremy L Thompson const char *field_name; 152059fa3f92SJeremy L Thompson 152159fa3f92SJeremy L Thompson CeedCallBackend(CeedOperatorFieldGetName(op_output_fields[i], &field_name)); 152259fa3f92SJeremy L Thompson code << " // ---- Output field " << i << ": " << field_name << "\n"; 1523ca735530SJeremy L Thompson 15244b3e95d5SJeremy L Thompson // ---- Basis action 15258014c5e7SJeremy L Thompson CeedCallBackend(CeedOperatorBuildKernelBasis_Cuda_gen(code, data, i, op_output_fields[i], qf_output_fields[i], max_dim, Q_1d, false, 1526412e5683SJeremy L Thompson is_all_tensor, is_at_points, use_3d_slices)); 15279a2291e3SJeremy L Thompson 15284b3e95d5SJeremy L Thompson // ---- Restriction 15298014c5e7SJeremy L Thompson CeedCallBackend(CeedOperatorBuildKernelRestriction_Cuda_gen(code, data, i, NULL, op_output_fields[i], qf_output_fields[i], max_dim, Q_1d, false, 1530412e5683SJeremy L Thompson is_all_tensor, is_at_points, use_3d_slices)); 1531ac421f39SYohann } 1532241a4b83SYohann 15334b3e95d5SJeremy L Thompson // Close loop and function 1534241a4b83SYohann code << " }\n"; 1535818e0025SJeremy L Thompson code << "}\n"; 1536818e0025SJeremy L Thompson code << "// -----------------------------------------------------------------------------\n\n"; 1537241a4b83SYohann 15383a2968d6SJeremy L Thompson // Compile 1539ddae5012SJeremy L Thompson { 1540ddae5012SJeremy L Thompson bool is_compile_good = false; 1541412e5683SJeremy L Thompson const CeedInt T_1d = CeedIntMax(is_all_tensor ? Q_1d : Q, data->max_P_1d); 1542ddae5012SJeremy L Thompson 1543a61b1c91SJeremy L Thompson data->thread_1d = T_1d; 1544412e5683SJeremy L Thompson CeedCallBackend(CeedTryCompile_Cuda(ceed, code.str().c_str(), &is_compile_good, &data->module, 1, "OP_T_1D", T_1d)); 1545ddae5012SJeremy L Thompson if (is_compile_good) { 1546ddae5012SJeremy L Thompson *is_good_build = true; 1547eb7e6cafSJeremy L Thompson CeedCallBackend(CeedGetKernel_Cuda(ceed, data->module, operator_name.c_str(), &data->op)); 1548ddae5012SJeremy L Thompson } else { 1549ddae5012SJeremy L Thompson *is_good_build = false; 1550ddae5012SJeremy L Thompson data->use_fallback = true; 1551ddae5012SJeremy L Thompson } 1552ddae5012SJeremy L Thompson } 15532b730f8bSJeremy L Thompson CeedCallBackend(CeedOperatorSetSetupDone(op)); 15549bc66399SJeremy L Thompson CeedCallBackend(CeedDestroy(&ceed)); 1555c11e12f4SJeremy L Thompson CeedCallBackend(CeedQFunctionDestroy(&qf)); 1556e15f9bd0SJeremy L Thompson return CEED_ERROR_SUCCESS; 1557241a4b83SYohann } 15582a86cc9dSSebastian Grimberg 1559ab213215SJeremy L Thompson //------------------------------------------------------------------------------ 1560