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, 364b3e95d5SJeremy L Thompson CeedQFunctionField *qf_output_fields, CeedInt *max_P_1d, CeedInt *Q_1d, CeedInt *dim, bool *is_tensor, 374b3e95d5SJeremy L Thompson bool *use_3d_slices) { 384b3e95d5SJeremy L Thompson // Find dim, P_1d, Q_1d 394b3e95d5SJeremy L Thompson *max_P_1d = 0; 404b3e95d5SJeremy L Thompson *Q_1d = 0; 414b3e95d5SJeremy L Thompson *dim = 0; 424b3e95d5SJeremy L Thompson *is_tensor = true; 43dc007f05SJeremy L Thompson 444b3e95d5SJeremy L Thompson for (CeedInt i = 0; i < num_input_fields; i++) { 454b3e95d5SJeremy L Thompson CeedBasis basis; 464b3e95d5SJeremy L Thompson 474b3e95d5SJeremy L Thompson CeedCallBackend(CeedOperatorFieldGetBasis(op_input_fields[i], &basis)); 484b3e95d5SJeremy L Thompson if (basis != CEED_BASIS_NONE) { 494b3e95d5SJeremy L Thompson bool is_field_tensor; 504b3e95d5SJeremy L Thompson CeedInt field_P_1d = 0, field_Q_1d = 0, field_dim = 0; 514b3e95d5SJeremy L Thompson 524b3e95d5SJeremy L Thompson // Collect dim, P_1d, and Q_1d 534b3e95d5SJeremy L Thompson CeedCallBackend(CeedBasisIsTensor(basis, &is_field_tensor)); 544b3e95d5SJeremy L Thompson *is_tensor = *is_tensor && is_field_tensor; 55dc007f05SJeremy L Thompson if (is_field_tensor) CeedCallBackend(CeedBasisGetNumNodes1D(basis, &field_P_1d)); 56dc007f05SJeremy L Thompson else CeedCallBackend(CeedBasisGetNumNodes(basis, &field_P_1d)); 574b3e95d5SJeremy L Thompson *max_P_1d = CeedIntMax(*max_P_1d, field_P_1d); 584b3e95d5SJeremy L Thompson CeedCallBackend(CeedBasisGetDimension(basis, &field_dim)); 594b3e95d5SJeremy L Thompson CeedCheck(*dim == 0 || field_dim == *dim, ceed, CEED_ERROR_BACKEND, "Quadrature spaces must be compatible"); 604b3e95d5SJeremy L Thompson *dim = field_dim; 61dc007f05SJeremy L Thompson if (is_field_tensor) CeedCallBackend(CeedBasisGetNumQuadraturePoints1D(basis, &field_Q_1d)); 62dc007f05SJeremy L Thompson else CeedCallBackend(CeedBasisGetNumQuadraturePoints(basis, &field_Q_1d)); 634b3e95d5SJeremy L Thompson CeedCheck(*Q_1d == 0 || field_Q_1d == *Q_1d, ceed, CEED_ERROR_BACKEND, "Quadrature spaces must be compatible"); 644b3e95d5SJeremy L Thompson *Q_1d = field_Q_1d; 654b3e95d5SJeremy L Thompson } 66681d0ea7SJeremy L Thompson CeedCallBackend(CeedBasisDestroy(&basis)); 674b3e95d5SJeremy L Thompson } 684b3e95d5SJeremy L Thompson for (CeedInt i = 0; i < num_output_fields; i++) { 694b3e95d5SJeremy L Thompson CeedBasis basis; 704b3e95d5SJeremy L Thompson 714b3e95d5SJeremy L Thompson CeedCallBackend(CeedOperatorFieldGetBasis(op_output_fields[i], &basis)); 724b3e95d5SJeremy L Thompson if (basis != CEED_BASIS_NONE) { 734b3e95d5SJeremy L Thompson bool is_field_tensor; 744b3e95d5SJeremy L Thompson CeedInt field_P_1d = 0, field_Q_1d = 0, field_dim = 0; 754b3e95d5SJeremy L Thompson 764b3e95d5SJeremy L Thompson // Collect dim, P_1d, and Q_1d 774b3e95d5SJeremy L Thompson CeedCallBackend(CeedBasisIsTensor(basis, &is_field_tensor)); 784b3e95d5SJeremy L Thompson *is_tensor = *is_tensor && is_field_tensor; 79dc007f05SJeremy L Thompson if (is_field_tensor) CeedCallBackend(CeedBasisGetNumNodes1D(basis, &field_P_1d)); 80dc007f05SJeremy L Thompson else CeedCallBackend(CeedBasisGetNumNodes(basis, &field_P_1d)); 814b3e95d5SJeremy L Thompson *max_P_1d = CeedIntMax(*max_P_1d, field_P_1d); 824b3e95d5SJeremy L Thompson CeedCallBackend(CeedBasisGetDimension(basis, &field_dim)); 834b3e95d5SJeremy L Thompson CeedCheck(*dim == 0 || field_dim == *dim, ceed, CEED_ERROR_BACKEND, "Quadrature spaces must be compatible"); 844b3e95d5SJeremy L Thompson *dim = field_dim; 85dc007f05SJeremy L Thompson if (is_field_tensor) CeedCallBackend(CeedBasisGetNumQuadraturePoints1D(basis, &field_Q_1d)); 86dc007f05SJeremy L Thompson else CeedCallBackend(CeedBasisGetNumQuadraturePoints(basis, &field_Q_1d)); 874b3e95d5SJeremy L Thompson CeedCheck(*Q_1d == 0 || field_Q_1d == *Q_1d, ceed, CEED_ERROR_BACKEND, "Quadrature spaces must be compatible"); 884b3e95d5SJeremy L Thompson *Q_1d = field_Q_1d; 894b3e95d5SJeremy L Thompson } 90681d0ea7SJeremy L Thompson CeedCallBackend(CeedBasisDestroy(&basis)); 914b3e95d5SJeremy L Thompson } 924b3e95d5SJeremy L Thompson 934b3e95d5SJeremy L Thompson // Only use 3D collocated gradient parallelization strategy when gradient is computed 944b3e95d5SJeremy L Thompson *use_3d_slices = false; 954b3e95d5SJeremy L Thompson if (*dim == 3) { 964b3e95d5SJeremy L Thompson bool was_grad_found = false; 974b3e95d5SJeremy L Thompson 984b3e95d5SJeremy L Thompson for (CeedInt i = 0; i < num_input_fields; i++) { 994b3e95d5SJeremy L Thompson CeedEvalMode eval_mode; 1004b3e95d5SJeremy L Thompson 1014b3e95d5SJeremy L Thompson CeedCallBackend(CeedQFunctionFieldGetEvalMode(qf_input_fields[i], &eval_mode)); 1024b3e95d5SJeremy L Thompson if (eval_mode == CEED_EVAL_GRAD) { 1034b3e95d5SJeremy L Thompson CeedBasis_Cuda_shared *basis_data; 1044b3e95d5SJeremy L Thompson CeedBasis basis; 1054b3e95d5SJeremy L Thompson 1064b3e95d5SJeremy L Thompson CeedCallBackend(CeedOperatorFieldGetBasis(op_input_fields[i], &basis)); 1074b3e95d5SJeremy L Thompson CeedCallBackend(CeedBasisGetData(basis, &basis_data)); 1084b3e95d5SJeremy L Thompson *use_3d_slices = basis_data->d_collo_grad_1d && (was_grad_found ? *use_3d_slices : true); 1094b3e95d5SJeremy L Thompson was_grad_found = true; 110681d0ea7SJeremy L Thompson CeedCallBackend(CeedBasisDestroy(&basis)); 1114b3e95d5SJeremy L Thompson } 1124b3e95d5SJeremy L Thompson } 1134b3e95d5SJeremy L Thompson for (CeedInt i = 0; i < num_output_fields; i++) { 1144b3e95d5SJeremy L Thompson CeedEvalMode eval_mode; 1154b3e95d5SJeremy L Thompson 1164b3e95d5SJeremy L Thompson CeedCallBackend(CeedQFunctionFieldGetEvalMode(qf_output_fields[i], &eval_mode)); 1174b3e95d5SJeremy L Thompson if (eval_mode == CEED_EVAL_GRAD) { 1184b3e95d5SJeremy L Thompson CeedBasis_Cuda_shared *basis_data; 1194b3e95d5SJeremy L Thompson CeedBasis basis; 1204b3e95d5SJeremy L Thompson 1214b3e95d5SJeremy L Thompson CeedCallBackend(CeedOperatorFieldGetBasis(op_output_fields[i], &basis)); 1224b3e95d5SJeremy L Thompson CeedCallBackend(CeedBasisGetData(basis, &basis_data)); 1234b3e95d5SJeremy L Thompson *use_3d_slices = basis_data->d_collo_grad_1d && (was_grad_found ? *use_3d_slices : true); 1244b3e95d5SJeremy L Thompson was_grad_found = true; 125681d0ea7SJeremy L Thompson CeedCallBackend(CeedBasisDestroy(&basis)); 1264b3e95d5SJeremy L Thompson } 1274b3e95d5SJeremy L Thompson } 1284b3e95d5SJeremy L Thompson } 1294b3e95d5SJeremy L Thompson return CEED_ERROR_SUCCESS; 1304b3e95d5SJeremy L Thompson } 1314b3e95d5SJeremy L Thompson 1324b3e95d5SJeremy L Thompson //------------------------------------------------------------------------------ 1334b3e95d5SJeremy L Thompson // Setup fields 1344b3e95d5SJeremy L Thompson //------------------------------------------------------------------------------ 1354b3e95d5SJeremy L Thompson static int CeedOperatorBuildKernelFieldData_Cuda_gen(std::ostringstream &code, CeedOperator_Cuda_gen *data, CeedInt i, CeedOperatorField op_field, 13645a787f7SJeremy L Thompson CeedQFunctionField qf_field, FieldReuse_Cuda field_reuse, CeedInt Q_1d, bool is_input, 13745a787f7SJeremy L Thompson bool is_tensor, bool is_at_points, bool use_3d_slices) { 13859fa3f92SJeremy L Thompson const char *field_name; 1394b3e95d5SJeremy L Thompson std::string var_suffix = (is_input ? "_in_" : "_out_") + std::to_string(i); 140dc007f05SJeremy L Thompson std::string P_name = (is_tensor ? "P_1d" : "P") + var_suffix, Q_name = is_tensor ? "Q_1d" : "Q"; 1414b3e95d5SJeremy L Thompson std::string option_name = (is_input ? "inputs" : "outputs"); 1424b3e95d5SJeremy L Thompson CeedEvalMode eval_mode = CEED_EVAL_NONE; 1434b3e95d5SJeremy L Thompson CeedInt elem_size = 0, num_comp = 0, P_1d = 0; 1444b3e95d5SJeremy L Thompson CeedElemRestriction elem_rstr; 1454b3e95d5SJeremy L Thompson CeedBasis_Cuda_shared *basis_data; 1464b3e95d5SJeremy L Thompson CeedBasis basis; 1474b3e95d5SJeremy L Thompson 1480a2a6492SJeremy L Thompson // Field reuse info 14945a787f7SJeremy L Thompson bool use_previous_field = field_reuse.index != -1; 1500a2a6492SJeremy L Thompson 15159fa3f92SJeremy L Thompson CeedCallBackend(CeedOperatorFieldGetName(op_field, &field_name)); 15259fa3f92SJeremy L Thompson code << " // -- " << (is_input ? "Input" : "Output") << " field " << i << ": " << field_name << "\n"; 1534b3e95d5SJeremy L Thompson 1544b3e95d5SJeremy L Thompson // Get field data 1554b3e95d5SJeremy L Thompson CeedCallBackend(CeedOperatorFieldGetElemRestriction(op_field, &elem_rstr)); 1564b3e95d5SJeremy L Thompson if (elem_rstr != CEED_ELEMRESTRICTION_NONE) { 1574b3e95d5SJeremy L Thompson CeedCallBackend(CeedElemRestrictionGetElementSize(elem_rstr, &elem_size)); 1584b3e95d5SJeremy L Thompson CeedCallBackend(CeedElemRestrictionGetNumComponents(elem_rstr, &num_comp)); 1594b3e95d5SJeremy L Thompson } 160681d0ea7SJeremy L Thompson CeedCallBackend(CeedElemRestrictionDestroy(&elem_rstr)); 1614b3e95d5SJeremy L Thompson CeedCallBackend(CeedOperatorFieldGetBasis(op_field, &basis)); 1624b3e95d5SJeremy L Thompson if (basis != CEED_BASIS_NONE) { 1634b3e95d5SJeremy L Thompson CeedCallBackend(CeedBasisGetData(basis, &basis_data)); 164dc007f05SJeremy L Thompson if (is_tensor) CeedCallBackend(CeedBasisGetNumNodes1D(basis, &P_1d)); 165dc007f05SJeremy L Thompson else CeedCallBackend(CeedBasisGetNumNodes(basis, &P_1d)); 1664b3e95d5SJeremy L Thompson } 1674b3e95d5SJeremy L Thompson CeedCallBackend(CeedQFunctionFieldGetEvalMode(qf_field, &eval_mode)); 1684b3e95d5SJeremy L Thompson 1694b3e95d5SJeremy L Thompson // Set field constants 1704b3e95d5SJeremy L Thompson code << " const CeedInt " << P_name << " = " << (basis == CEED_BASIS_NONE ? Q_1d : P_1d) << ";\n"; 171*343e3094SJeremy L Thompson if (eval_mode != CEED_EVAL_WEIGHT) { 1724b3e95d5SJeremy L Thompson code << " const CeedInt num_comp" << var_suffix << " = " << num_comp << ";\n"; 1734b3e95d5SJeremy L Thompson } 1744b3e95d5SJeremy L Thompson 1754b3e95d5SJeremy L Thompson // Load basis data 1764b3e95d5SJeremy L Thompson code << " // EvalMode: " << CeedEvalModes[eval_mode] << "\n"; 1774b3e95d5SJeremy L Thompson switch (eval_mode) { 1784b3e95d5SJeremy L Thompson case CEED_EVAL_NONE: 1794b3e95d5SJeremy L Thompson break; 1804b3e95d5SJeremy L Thompson case CEED_EVAL_INTERP: 1818b97b69aSJeremy L Thompson if (is_at_points) { 1828b97b69aSJeremy L Thompson // AtPoints 1838b97b69aSJeremy L Thompson if (!basis_data->d_chebyshev_interp_1d) { 1848b97b69aSJeremy L Thompson CeedSize interp_bytes; 1858b97b69aSJeremy L Thompson CeedScalar *chebyshev_interp_1d; 1868b97b69aSJeremy L Thompson 1878b97b69aSJeremy L Thompson interp_bytes = P_1d * Q_1d * sizeof(CeedScalar); 1888b97b69aSJeremy L Thompson CeedCallBackend(CeedCalloc(P_1d * Q_1d, &chebyshev_interp_1d)); 1898b97b69aSJeremy L Thompson CeedCallBackend(CeedBasisGetChebyshevInterp1D(basis, chebyshev_interp_1d)); 1908b97b69aSJeremy L Thompson CeedCallCuda(CeedBasisReturnCeed(basis), cudaMalloc((void **)&basis_data->d_chebyshev_interp_1d, interp_bytes)); 1918b97b69aSJeremy L Thompson CeedCallCuda(CeedBasisReturnCeed(basis), 1928b97b69aSJeremy L Thompson cudaMemcpy(basis_data->d_chebyshev_interp_1d, chebyshev_interp_1d, interp_bytes, cudaMemcpyHostToDevice)); 1938b97b69aSJeremy L Thompson CeedCallBackend(CeedFree(&chebyshev_interp_1d)); 1948b97b69aSJeremy L Thompson } 1958b97b69aSJeremy L Thompson if (is_input) data->B.inputs[i] = basis_data->d_chebyshev_interp_1d; 1968b97b69aSJeremy L Thompson else data->B.outputs[i] = basis_data->d_chebyshev_interp_1d; 1978b97b69aSJeremy L Thompson } else { 1988b97b69aSJeremy L Thompson // Standard quadrature 1994b3e95d5SJeremy L Thompson if (is_input) data->B.inputs[i] = basis_data->d_interp_1d; 2004b3e95d5SJeremy L Thompson else data->B.outputs[i] = basis_data->d_interp_1d; 2018b97b69aSJeremy L Thompson } 2020a2a6492SJeremy L Thompson if (use_previous_field) { 20345a787f7SJeremy L Thompson std::string reuse_var = "s_B" + ((field_reuse.is_input ? "_in_" : "_out_") + std::to_string(field_reuse.index)); 2040a2a6492SJeremy L Thompson 2050a2a6492SJeremy L Thompson code << " CeedScalar *s_B" << var_suffix << " = " << reuse_var << ";\n"; 2060a2a6492SJeremy L Thompson } else { 207dc007f05SJeremy L Thompson code << " __shared__ CeedScalar s_B" << var_suffix << "[" << P_name << "*" << Q_name << "];\n"; 208f815fac9SJeremy L Thompson code << " LoadMatrix<" << P_name << ", " << Q_name << ">(data, B." << option_name << "[" << i << "], s_B" << var_suffix << ");\n"; 2090a2a6492SJeremy L Thompson } 2104b3e95d5SJeremy L Thompson break; 2114b3e95d5SJeremy L Thompson case CEED_EVAL_GRAD: 2128b97b69aSJeremy L Thompson if (is_at_points) { 2138b97b69aSJeremy L Thompson // AtPoints 2148b97b69aSJeremy L Thompson if (!basis_data->d_chebyshev_interp_1d) { 2158b97b69aSJeremy L Thompson CeedSize interp_bytes; 2168b97b69aSJeremy L Thompson CeedScalar *chebyshev_interp_1d; 2178b97b69aSJeremy L Thompson 2188b97b69aSJeremy L Thompson interp_bytes = P_1d * Q_1d * sizeof(CeedScalar); 2198b97b69aSJeremy L Thompson CeedCallBackend(CeedCalloc(P_1d * Q_1d, &chebyshev_interp_1d)); 2208b97b69aSJeremy L Thompson CeedCallBackend(CeedBasisGetChebyshevInterp1D(basis, chebyshev_interp_1d)); 2218b97b69aSJeremy L Thompson CeedCallCuda(CeedBasisReturnCeed(basis), cudaMalloc((void **)&basis_data->d_chebyshev_interp_1d, interp_bytes)); 2228b97b69aSJeremy L Thompson CeedCallCuda(CeedBasisReturnCeed(basis), 2238b97b69aSJeremy L Thompson cudaMemcpy(basis_data->d_chebyshev_interp_1d, chebyshev_interp_1d, interp_bytes, cudaMemcpyHostToDevice)); 2248b97b69aSJeremy L Thompson CeedCallBackend(CeedFree(&chebyshev_interp_1d)); 2258b97b69aSJeremy L Thompson } 2268b97b69aSJeremy L Thompson if (is_input) data->B.inputs[i] = basis_data->d_chebyshev_interp_1d; 2278b97b69aSJeremy L Thompson else data->B.outputs[i] = basis_data->d_chebyshev_interp_1d; 2288b97b69aSJeremy L Thompson } else { 2298b97b69aSJeremy L Thompson // Standard quadrature 2304b3e95d5SJeremy L Thompson if (is_input) data->B.inputs[i] = basis_data->d_interp_1d; 2314b3e95d5SJeremy L Thompson else data->B.outputs[i] = basis_data->d_interp_1d; 2328b97b69aSJeremy L Thompson } 233dc007f05SJeremy L Thompson if (is_tensor) { 2340a2a6492SJeremy L Thompson if (use_previous_field) { 23545a787f7SJeremy L Thompson std::string reuse_var = "s_B" + ((field_reuse.is_input ? "_in_" : "_out_") + std::to_string(field_reuse.index)); 2360a2a6492SJeremy L Thompson 2370a2a6492SJeremy L Thompson code << " CeedScalar *s_B" << var_suffix << " = " << reuse_var << ";\n"; 2380a2a6492SJeremy L Thompson } else { 239dc007f05SJeremy L Thompson code << " __shared__ CeedScalar s_B" << var_suffix << "[" << P_name << "*" << Q_name << "];\n"; 240f815fac9SJeremy L Thompson code << " LoadMatrix<" << P_name << ", " << Q_name << ">(data, B." << option_name << "[" << i << "], s_B" << var_suffix << ");\n"; 241dc007f05SJeremy L Thompson } 2420a2a6492SJeremy L Thompson } 2438b97b69aSJeremy L Thompson if (is_at_points) break; // No G mat for AtPoints 2444b3e95d5SJeremy L Thompson if (use_3d_slices) { 2454b3e95d5SJeremy L Thompson if (is_input) data->G.inputs[i] = basis_data->d_collo_grad_1d; 2464b3e95d5SJeremy L Thompson else data->G.outputs[i] = basis_data->d_collo_grad_1d; 24745a787f7SJeremy L Thompson if (use_previous_field && field_reuse.eval_mode == CEED_EVAL_GRAD) { 24845a787f7SJeremy L Thompson std::string reuse_var = "s_G" + ((field_reuse.is_input ? "_in_" : "_out_") + std::to_string(field_reuse.index)); 2490a2a6492SJeremy L Thompson 2500a2a6492SJeremy L Thompson code << " CeedScalar *s_G" << var_suffix << " = " << reuse_var << ";\n"; 2510a2a6492SJeremy L Thompson } else { 252dc007f05SJeremy L Thompson code << " __shared__ CeedScalar s_G" << var_suffix << "[" << Q_name << "*" << Q_name << "];\n"; 253f815fac9SJeremy L Thompson code << " LoadMatrix<" << Q_name << ", " << Q_name << ">(data, G." << option_name << "[" << i << "], s_G" << var_suffix << ");\n"; 2540a2a6492SJeremy L Thompson } 2554b3e95d5SJeremy L Thompson } else { 2564b3e95d5SJeremy L Thompson bool has_collo_grad = basis_data->d_collo_grad_1d; 2574b3e95d5SJeremy L Thompson 2584b3e95d5SJeremy L Thompson if (is_input) data->G.inputs[i] = has_collo_grad ? basis_data->d_collo_grad_1d : basis_data->d_grad_1d; 2594b3e95d5SJeremy L Thompson else data->G.outputs[i] = has_collo_grad ? basis_data->d_collo_grad_1d : basis_data->d_grad_1d; 2604b3e95d5SJeremy L Thompson if (has_collo_grad) { 26145a787f7SJeremy L Thompson if (use_previous_field && field_reuse.eval_mode == CEED_EVAL_GRAD) { 26245a787f7SJeremy L Thompson std::string reuse_var = "s_G" + ((field_reuse.is_input ? "_in_" : "_out_") + std::to_string(field_reuse.index)); 2630a2a6492SJeremy L Thompson 2640a2a6492SJeremy L Thompson code << " CeedScalar *s_G" << var_suffix << " = " << reuse_var << ";\n"; 2650a2a6492SJeremy L Thompson } else { 266dc007f05SJeremy L Thompson code << " __shared__ CeedScalar s_G" << var_suffix << "[" << Q_name << "*" << Q_name << "];\n"; 267f815fac9SJeremy L Thompson code << " LoadMatrix<" << Q_name << ", " << Q_name << ">(data, G." << option_name << "[" << i << "], s_G" << var_suffix << ");\n"; 2680a2a6492SJeremy L Thompson } 2690a2a6492SJeremy L Thompson } else { 27045a787f7SJeremy L Thompson if (use_previous_field && field_reuse.eval_mode == CEED_EVAL_GRAD) { 27145a787f7SJeremy L Thompson std::string reuse_var = "s_G" + ((field_reuse.is_input ? "_in_" : "_out_") + std::to_string(field_reuse.index)); 2720a2a6492SJeremy L Thompson 2730a2a6492SJeremy L Thompson code << " CeedScalar *s_G" << var_suffix << " = " << reuse_var << ";\n"; 2744b3e95d5SJeremy L Thompson } else { 275dc007f05SJeremy L Thompson code << " __shared__ CeedScalar s_G" << var_suffix << "[" << P_name << "*" << Q_name << (is_tensor ? "" : "*dim") << "];\n"; 276dc007f05SJeremy L Thompson code << " LoadMatrix<" << P_name << ", " << Q_name << (is_tensor ? "" : "*dim") << ">(data, G." << option_name << "[" << i << "], s_G" 277dc007f05SJeremy L Thompson << var_suffix << ");\n"; 2784b3e95d5SJeremy L Thompson } 2794b3e95d5SJeremy L Thompson } 2800a2a6492SJeremy L Thompson } 2814b3e95d5SJeremy L Thompson break; 2824b3e95d5SJeremy L Thompson case CEED_EVAL_WEIGHT: 2834b3e95d5SJeremy L Thompson break; // No action 2844b3e95d5SJeremy L Thompson // LCOV_EXCL_START 2854b3e95d5SJeremy L Thompson case CEED_EVAL_DIV: 2864b3e95d5SJeremy L Thompson case CEED_EVAL_CURL: 2874b3e95d5SJeremy L Thompson break; // TODO: Not implemented 2884b3e95d5SJeremy L Thompson // LCOV_EXCL_STOP 2894b3e95d5SJeremy L Thompson } 2908b97b69aSJeremy L Thompson CeedCallBackend(CeedBasisDestroy(&basis)); 2914b3e95d5SJeremy L Thompson return CEED_ERROR_SUCCESS; 2924b3e95d5SJeremy L Thompson } 2934b3e95d5SJeremy L Thompson 2944b3e95d5SJeremy L Thompson //------------------------------------------------------------------------------ 2954b3e95d5SJeremy L Thompson // Restriction 2964b3e95d5SJeremy L Thompson //------------------------------------------------------------------------------ 2974b3e95d5SJeremy L Thompson static int CeedOperatorBuildKernelRestriction_Cuda_gen(std::ostringstream &code, CeedOperator_Cuda_gen *data, CeedInt i, CeedInt dim, 298e93651e5SJeremy L Thompson CeedInt field_input_buffer[], CeedOperatorField op_field, CeedQFunctionField qf_field, 299dc007f05SJeremy L Thompson CeedInt Q_1d, bool is_input, bool is_tensor, bool is_at_points, bool use_3d_slices) { 3004b3e95d5SJeremy L Thompson std::string var_suffix = (is_input ? "_in_" : "_out_") + std::to_string(i); 301dc007f05SJeremy L Thompson std::string P_name = (is_tensor ? "P_1d" : "P") + var_suffix; 3024b3e95d5SJeremy L Thompson CeedEvalMode eval_mode = CEED_EVAL_NONE; 3034b3e95d5SJeremy L Thompson CeedInt elem_size = 0, num_comp = 0, P_1d = 0; 3044b3e95d5SJeremy L Thompson CeedSize l_size; 305f815fac9SJeremy L Thompson CeedRestrictionType rstr_type = CEED_RESTRICTION_STANDARD; 3064b3e95d5SJeremy L Thompson CeedElemRestriction_Cuda *rstr_data; 3074b3e95d5SJeremy L Thompson CeedElemRestriction elem_rstr; 3084b3e95d5SJeremy L Thompson CeedBasis basis; 3094b3e95d5SJeremy L Thompson 3104b3e95d5SJeremy L Thompson // Get field data 3114b3e95d5SJeremy L Thompson CeedCallBackend(CeedOperatorFieldGetElemRestriction(op_field, &elem_rstr)); 3124b3e95d5SJeremy L Thompson if (elem_rstr != CEED_ELEMRESTRICTION_NONE) { 313f815fac9SJeremy L Thompson CeedCallBackend(CeedElemRestrictionGetType(elem_rstr, &rstr_type)); 3144b3e95d5SJeremy L Thompson CeedCallBackend(CeedElemRestrictionGetElementSize(elem_rstr, &elem_size)); 3154b3e95d5SJeremy L Thompson CeedCallBackend(CeedElemRestrictionGetNumComponents(elem_rstr, &num_comp)); 3164b3e95d5SJeremy L Thompson CeedCallBackend(CeedElemRestrictionGetData(elem_rstr, &rstr_data)); 3174b3e95d5SJeremy L Thompson } 3184b3e95d5SJeremy L Thompson CeedCallBackend(CeedOperatorFieldGetBasis(op_field, &basis)); 3194b3e95d5SJeremy L Thompson if (basis != CEED_BASIS_NONE) { 320dc007f05SJeremy L Thompson if (is_tensor) CeedCallBackend(CeedBasisGetNumNodes1D(basis, &P_1d)); 321dc007f05SJeremy L Thompson else CeedCallBackend(CeedBasisGetNumNodes(basis, &P_1d)); 3224b3e95d5SJeremy L Thompson } 323681d0ea7SJeremy L Thompson CeedCallBackend(CeedBasisDestroy(&basis)); 3244b3e95d5SJeremy L Thompson CeedCallBackend(CeedQFunctionFieldGetEvalMode(qf_field, &eval_mode)); 3254b3e95d5SJeremy L Thompson 3264b3e95d5SJeremy L Thompson // Restriction 3274b3e95d5SJeremy L Thompson if (is_input) { 3284b3e95d5SJeremy L Thompson // Input 329e93651e5SJeremy L Thompson if (field_input_buffer[i] != i) { 330e93651e5SJeremy L Thompson std::string buffer_name = "r_e_in_" + std::to_string(field_input_buffer[i]); 331e93651e5SJeremy L Thompson 332e93651e5SJeremy L Thompson // Restriction was already done for previous input 333e93651e5SJeremy L Thompson code << " CeedScalar *r_e" << var_suffix << " = " << buffer_name << ";\n"; 3348b97b69aSJeremy L Thompson } else if (eval_mode != CEED_EVAL_WEIGHT && !((eval_mode == CEED_EVAL_NONE) && use_3d_slices && is_at_points)) { 3358b97b69aSJeremy L Thompson if (eval_mode == CEED_EVAL_NONE && rstr_type != CEED_RESTRICTION_POINTS) { 336e93651e5SJeremy L Thompson // No basis action, so r_e_in_* in also r_q_in_* and needs to be allocated 3374b3e95d5SJeremy L Thompson code << " CeedScalar r_e" << var_suffix << "[num_comp" << var_suffix << "*" << P_name << "];\n"; 3388b97b69aSJeremy L Thompson } else if (rstr_type != CEED_RESTRICTION_POINTS) { 339e93651e5SJeremy L Thompson // Otherwise we're using the scratch space 340e93651e5SJeremy L Thompson code << " CeedScalar *r_e" << var_suffix << " = r_e_scratch;\n"; 341e93651e5SJeremy L Thompson } 342f815fac9SJeremy L Thompson switch (rstr_type) { 343f815fac9SJeremy L Thompson case CEED_RESTRICTION_STANDARD: { 3444b3e95d5SJeremy L Thompson CeedInt comp_stride; 3454b3e95d5SJeremy L Thompson 3464b3e95d5SJeremy L Thompson CeedCallBackend(CeedElemRestrictionGetLVectorSize(elem_rstr, &l_size)); 3474b3e95d5SJeremy L Thompson code << " const CeedInt l_size" << var_suffix << " = " << l_size << ";\n"; 3484b3e95d5SJeremy L Thompson CeedCallBackend(CeedElemRestrictionGetCompStride(elem_rstr, &comp_stride)); 3494b3e95d5SJeremy L Thompson code << " // CompStride: " << comp_stride << "\n"; 3504b3e95d5SJeremy L Thompson data->indices.inputs[i] = (CeedInt *)rstr_data->d_offsets; 351dc007f05SJeremy L Thompson code << " ReadLVecStandard" << (is_tensor ? dim : 1) << "d<num_comp" << var_suffix << ", " << comp_stride << ", " << P_name 352dc007f05SJeremy L Thompson << ">(data, l_size" << var_suffix << ", elem, indices.inputs[" << i << "], d" << var_suffix << ", r_e" << var_suffix << ");\n"; 353f815fac9SJeremy L Thompson break; 354f815fac9SJeremy L Thompson } 355f815fac9SJeremy L Thompson case CEED_RESTRICTION_STRIDED: { 3564b3e95d5SJeremy L Thompson bool has_backend_strides; 3574b3e95d5SJeremy L Thompson CeedInt num_elem; 3584b3e95d5SJeremy L Thompson 3594b3e95d5SJeremy L Thompson CeedCallBackend(CeedElemRestrictionHasBackendStrides(elem_rstr, &has_backend_strides)); 3604b3e95d5SJeremy L Thompson CeedCallBackend(CeedElemRestrictionGetNumElements(elem_rstr, &num_elem)); 3614b3e95d5SJeremy L Thompson CeedInt strides[3] = {1, elem_size * num_elem, elem_size}; 3624b3e95d5SJeremy L Thompson 3634b3e95d5SJeremy L Thompson if (!has_backend_strides) { 3644b3e95d5SJeremy L Thompson CeedCallBackend(CeedElemRestrictionGetStrides(elem_rstr, strides)); 3654b3e95d5SJeremy L Thompson } 3664b3e95d5SJeremy L Thompson code << " // Strides: {" << strides[0] << ", " << strides[1] << ", " << strides[2] << "}\n"; 367dc007f05SJeremy L Thompson code << " ReadLVecStrided" << (is_tensor ? dim : 1) << "d<num_comp" << var_suffix << ", " << P_name << ", " << strides[0] << ", " 368dc007f05SJeremy L Thompson << strides[1] << ", " << strides[2] << ">(data, elem, d" << var_suffix << ", r_e" << var_suffix << ");\n"; 369f815fac9SJeremy L Thompson break; 370f815fac9SJeremy L Thompson } 3718b97b69aSJeremy L Thompson case CEED_RESTRICTION_POINTS: { 3728b97b69aSJeremy L Thompson CeedInt comp_stride; 3738b97b69aSJeremy L Thompson 3748b97b69aSJeremy L Thompson CeedCallBackend(CeedElemRestrictionGetCompStride(elem_rstr, &comp_stride)); 3758b97b69aSJeremy L Thompson code << " const CeedInt comp_stride" << var_suffix << " = " << comp_stride << ";\n"; 3768b97b69aSJeremy L Thompson data->indices.inputs[i] = (CeedInt *)rstr_data->d_offsets; 3778b97b69aSJeremy L Thompson break; 3788b97b69aSJeremy L Thompson } 379f815fac9SJeremy L Thompson // LCOV_EXCL_START 380f815fac9SJeremy L Thompson case CEED_RESTRICTION_ORIENTED: 381f815fac9SJeremy L Thompson case CEED_RESTRICTION_CURL_ORIENTED: 382f815fac9SJeremy L Thompson break; // TODO: Not implemented 383f815fac9SJeremy L Thompson // LCOV_EXCL_STOP 3844b3e95d5SJeremy L Thompson } 3854b3e95d5SJeremy L Thompson } 3864b3e95d5SJeremy L Thompson } else { 3874b3e95d5SJeremy L Thompson // Output 388f815fac9SJeremy L Thompson switch (rstr_type) { 389f815fac9SJeremy L Thompson case CEED_RESTRICTION_STANDARD: { 3904b3e95d5SJeremy L Thompson CeedInt comp_stride; 3914b3e95d5SJeremy L Thompson 3924b3e95d5SJeremy L Thompson CeedCallBackend(CeedElemRestrictionGetLVectorSize(elem_rstr, &l_size)); 3934b3e95d5SJeremy L Thompson code << " const CeedInt l_size" << var_suffix << " = " << l_size << ";\n"; 3944b3e95d5SJeremy L Thompson CeedCallBackend(CeedElemRestrictionGetCompStride(elem_rstr, &comp_stride)); 3954b3e95d5SJeremy L Thompson code << " // CompStride: " << comp_stride << "\n"; 3964b3e95d5SJeremy L Thompson data->indices.outputs[i] = (CeedInt *)rstr_data->d_offsets; 397dc007f05SJeremy L Thompson code << " WriteLVecStandard" << (is_tensor ? dim : 1) << "d<num_comp" << var_suffix << ", " << comp_stride << ", " << P_name 398dc007f05SJeremy L Thompson << ">(data, l_size" << var_suffix << ", elem, indices.outputs[" << i << "], r_e" << var_suffix << ", d" << var_suffix << ");\n"; 399f815fac9SJeremy L Thompson break; 400f815fac9SJeremy L Thompson } 401f815fac9SJeremy L Thompson case CEED_RESTRICTION_STRIDED: { 4024b3e95d5SJeremy L Thompson bool has_backend_strides; 4034b3e95d5SJeremy L Thompson CeedInt num_elem; 4044b3e95d5SJeremy L Thompson 4054b3e95d5SJeremy L Thompson CeedCallBackend(CeedElemRestrictionHasBackendStrides(elem_rstr, &has_backend_strides)); 4064b3e95d5SJeremy L Thompson CeedCallBackend(CeedElemRestrictionGetNumElements(elem_rstr, &num_elem)); 4074b3e95d5SJeremy L Thompson CeedInt strides[3] = {1, elem_size * num_elem, elem_size}; 4084b3e95d5SJeremy L Thompson 4094b3e95d5SJeremy L Thompson if (!has_backend_strides) { 4104b3e95d5SJeremy L Thompson CeedCallBackend(CeedElemRestrictionGetStrides(elem_rstr, strides)); 4114b3e95d5SJeremy L Thompson } 4124b3e95d5SJeremy L Thompson code << " // Strides: {" << strides[0] << ", " << strides[1] << ", " << strides[2] << "}\n"; 413dc007f05SJeremy L Thompson code << " WriteLVecStrided" << (is_tensor ? dim : 1) << "d<num_comp" << var_suffix << ", " << P_name << ", " << strides[0] << ", " 414dc007f05SJeremy L Thompson << strides[1] << ", " << strides[2] << ">(data, elem, r_e" << var_suffix << ", d" << var_suffix << ");\n"; 415f815fac9SJeremy L Thompson break; 416f815fac9SJeremy L Thompson } 4178b97b69aSJeremy L Thompson case CEED_RESTRICTION_POINTS: 4188b97b69aSJeremy L Thompson data->indices.outputs[i] = (CeedInt *)rstr_data->d_offsets; 4198b97b69aSJeremy L Thompson break; 420f815fac9SJeremy L Thompson // LCOV_EXCL_START 421f815fac9SJeremy L Thompson case CEED_RESTRICTION_ORIENTED: 422f815fac9SJeremy L Thompson case CEED_RESTRICTION_CURL_ORIENTED: 423f815fac9SJeremy L Thompson break; // TODO: Not implemented 424f815fac9SJeremy L Thompson // LCOV_EXCL_STOP 4254b3e95d5SJeremy L Thompson } 4264b3e95d5SJeremy L Thompson } 427681d0ea7SJeremy L Thompson CeedCallBackend(CeedElemRestrictionDestroy(&elem_rstr)); 4284b3e95d5SJeremy L Thompson return CEED_ERROR_SUCCESS; 4294b3e95d5SJeremy L Thompson } 4304b3e95d5SJeremy L Thompson 4314b3e95d5SJeremy L Thompson //------------------------------------------------------------------------------ 4324b3e95d5SJeremy L Thompson // Basis 4334b3e95d5SJeremy L Thompson //------------------------------------------------------------------------------ 4344b3e95d5SJeremy L Thompson static int CeedOperatorBuildKernelBasis_Cuda_gen(std::ostringstream &code, CeedOperator_Cuda_gen *data, CeedInt i, CeedInt dim, 435dc007f05SJeremy L Thompson CeedOperatorField op_field, CeedQFunctionField qf_field, CeedInt Q_1d, bool is_input, bool is_tensor, 4368b97b69aSJeremy L Thompson bool is_at_points, bool use_3d_slices) { 4374b3e95d5SJeremy L Thompson std::string var_suffix = (is_input ? "_in_" : "_out_") + std::to_string(i); 438dc007f05SJeremy L Thompson std::string P_name = (is_tensor ? "P_1d" : "P") + var_suffix, Q_name = is_tensor ? "Q_1d" : "Q"; 4394b3e95d5SJeremy L Thompson CeedEvalMode eval_mode = CEED_EVAL_NONE; 4404b3e95d5SJeremy L Thompson CeedInt elem_size = 0, num_comp = 0, P_1d = 0; 4414b3e95d5SJeremy L Thompson CeedElemRestriction elem_rstr; 4424b3e95d5SJeremy L Thompson CeedBasis basis; 4434b3e95d5SJeremy L Thompson 4444b3e95d5SJeremy L Thompson // Get field data 4454b3e95d5SJeremy L Thompson CeedCallBackend(CeedOperatorFieldGetElemRestriction(op_field, &elem_rstr)); 4464b3e95d5SJeremy L Thompson if (elem_rstr != CEED_ELEMRESTRICTION_NONE) { 4474b3e95d5SJeremy L Thompson CeedCallBackend(CeedElemRestrictionGetElementSize(elem_rstr, &elem_size)); 4484b3e95d5SJeremy L Thompson CeedCallBackend(CeedElemRestrictionGetNumComponents(elem_rstr, &num_comp)); 4494b3e95d5SJeremy L Thompson } 450681d0ea7SJeremy L Thompson CeedCallBackend(CeedElemRestrictionDestroy(&elem_rstr)); 4514b3e95d5SJeremy L Thompson CeedCallBackend(CeedOperatorFieldGetBasis(op_field, &basis)); 4524b3e95d5SJeremy L Thompson if (basis != CEED_BASIS_NONE) { 453dc007f05SJeremy L Thompson if (is_tensor) CeedCallBackend(CeedBasisGetNumNodes1D(basis, &P_1d)); 454dc007f05SJeremy L Thompson else CeedCallBackend(CeedBasisGetNumNodes(basis, &P_1d)); 4554b3e95d5SJeremy L Thompson } 4564b3e95d5SJeremy L Thompson CeedCallBackend(CeedQFunctionFieldGetEvalMode(qf_field, &eval_mode)); 4574b3e95d5SJeremy L Thompson 4584b3e95d5SJeremy L Thompson // Basis 4594b3e95d5SJeremy L Thompson code << " // EvalMode: " << CeedEvalModes[eval_mode] << "\n"; 4604b3e95d5SJeremy L Thompson if (is_input) { 4614b3e95d5SJeremy L Thompson switch (eval_mode) { 4624b3e95d5SJeremy L Thompson case CEED_EVAL_NONE: 4638b97b69aSJeremy L Thompson if (!use_3d_slices && !is_at_points) { 4644b3e95d5SJeremy L Thompson code << " CeedScalar *r_q" << var_suffix << " = r_e" << var_suffix << ";\n"; 4654b3e95d5SJeremy L Thompson } 4664b3e95d5SJeremy L Thompson break; 4674b3e95d5SJeremy L Thompson case CEED_EVAL_INTERP: 4688b97b69aSJeremy L Thompson if (is_at_points) { 469dc007f05SJeremy L Thompson std::string function_name = (dim == 1 ? "Interp" : "InterpTensor") + std::to_string(dim) + "d"; 470dc007f05SJeremy L Thompson 471dc007f05SJeremy L Thompson code << " CeedScalar r_c" << var_suffix << "[num_comp" << var_suffix << "*" << (dim >= 3 ? Q_name : "1") << "];\n"; 47299421279SJeremy L Thompson code << " " << function_name << "<num_comp" << var_suffix << ", " << P_name << ", " << Q_name << ", OP_T_1D>(data, r_e" << var_suffix 47399421279SJeremy L Thompson << ", s_B" << var_suffix << ", r_c" << var_suffix << ");\n"; 4748b97b69aSJeremy L Thompson } else { 475dc007f05SJeremy L Thompson std::string function_name = is_tensor ? ((dim == 1 ? "Interp" : "InterpTensor") + std::to_string(dim) + "d") : "InterpNonTensor"; 476dc007f05SJeremy L Thompson 477dc007f05SJeremy L Thompson code << " CeedScalar r_q" << var_suffix << "[num_comp" << var_suffix << "*" << (is_tensor && (dim >= 3) ? Q_name : "1") << "];\n"; 47899421279SJeremy L Thompson code << " " << function_name << "<num_comp" << var_suffix << ", " << P_name << ", " << Q_name << ", OP_T_1D>(data, r_e" << var_suffix 47999421279SJeremy L Thompson << ", s_B" << var_suffix << ", r_q" << var_suffix << ");\n"; 4808b97b69aSJeremy L Thompson } 4814b3e95d5SJeremy L Thompson break; 4824b3e95d5SJeremy L Thompson case CEED_EVAL_GRAD: 4838b97b69aSJeremy L Thompson if (is_at_points) { 484dc007f05SJeremy L Thompson std::string function_name = (dim == 1 ? "Interp" : "InterpTensor") + std::to_string(dim) + "d"; 485dc007f05SJeremy L Thompson 486dc007f05SJeremy L Thompson code << " CeedScalar r_c" << var_suffix << "[num_comp" << var_suffix << "*" << (dim >= 3 ? Q_name : "1") << "];\n"; 48799421279SJeremy L Thompson code << " " << function_name << "<num_comp" << var_suffix << ", " << P_name << ", " << Q_name << ", OP_T_1D>(data, r_e" << var_suffix 48899421279SJeremy L Thompson << ", s_B" << var_suffix << ", r_c" << var_suffix << ");\n"; 4898b97b69aSJeremy L Thompson } else if (use_3d_slices) { 490dc007f05SJeremy L Thompson std::string function_name = (dim > 1 ? "InterpTensor" : "Interp") + std::to_string(dim) + "d"; 491dc007f05SJeremy L Thompson 4924b3e95d5SJeremy L Thompson code << " CeedScalar r_q" << var_suffix << "[num_comp" << var_suffix << "*" << Q_name << "];\n"; 49399421279SJeremy L Thompson code << " " << function_name << "<num_comp" << var_suffix << ", " << P_name << ", " << Q_name << ", OP_T_1D>(data, r_e" << var_suffix 49499421279SJeremy L Thompson << ", s_B" << var_suffix << ", r_q" << var_suffix << ");\n"; 495dc007f05SJeremy L Thompson } else if (is_tensor) { 496dc007f05SJeremy L Thompson bool is_collocated = dim == 3 && Q_1d >= P_1d; 497dc007f05SJeremy L Thompson std::string function_name = (dim == 1 ? "Grad" : (is_collocated ? "GradTensorCollocated" : "GradTensor")) + std::to_string(dim) + "d"; 498dc007f05SJeremy L Thompson 499dc007f05SJeremy L Thompson code << " CeedScalar r_q" << var_suffix << "[num_comp" << var_suffix << "*dim*" << (dim >= 3 ? Q_name : "1") << "];\n"; 50099421279SJeremy L Thompson code << " " << function_name << "<num_comp" << var_suffix << ", " << P_name << ", " << Q_name << ", OP_T_1D>(data, r_e" << var_suffix 50199421279SJeremy L Thompson << ", s_B" << var_suffix << ", s_G" << var_suffix << ", r_q" << var_suffix << ");\n"; 5024b3e95d5SJeremy L Thompson } else { 503dc007f05SJeremy L Thompson std::string function_name = "GradNonTensor"; 504dc007f05SJeremy L Thompson 505dc007f05SJeremy L Thompson code << " CeedScalar r_q" << var_suffix << "[num_comp" << var_suffix << "*dim];\n"; 50699421279SJeremy L Thompson code << " " << function_name << "<num_comp" << var_suffix << ", dim, " << P_name << ", " << Q_name << ", OP_T_1D>(data, r_e" 50799421279SJeremy L Thompson << var_suffix << ", s_G" << var_suffix << ", r_q" << var_suffix << ");\n"; 5084b3e95d5SJeremy L Thompson } 5094b3e95d5SJeremy L Thompson break; 5104b3e95d5SJeremy L Thompson case CEED_EVAL_WEIGHT: { 5118b97b69aSJeremy L Thompson if (is_at_points) { 5128b97b69aSJeremy L Thompson code << " // Nothing to do AtPoints\n"; 5138b97b69aSJeremy L Thompson } else { 5144b3e95d5SJeremy L Thompson CeedBasis_Cuda_shared *basis_data; 515dc007f05SJeremy L Thompson std::string function_name = is_tensor ? ((dim == 1 ? "Weight" : "WeightTensor") + std::to_string(dim) + "d") : "WeightNonTensor"; 5164b3e95d5SJeremy L Thompson 517dc007f05SJeremy L Thompson code << " CeedScalar r_q" << var_suffix << "[" << (is_tensor && (dim >= 3) ? Q_name : "1") << "];\n"; 5184b3e95d5SJeremy L Thompson CeedCallBackend(CeedBasisGetData(basis, &basis_data)); 5194b3e95d5SJeremy L Thompson data->W = basis_data->d_q_weight_1d; 520*343e3094SJeremy L Thompson code << " " << function_name << "<" << P_name << ", " << Q_name << ">(data, W, r_q" << var_suffix << ");\n"; 5218b97b69aSJeremy L Thompson } 5224b3e95d5SJeremy L Thompson break; 5234b3e95d5SJeremy L Thompson } 5244b3e95d5SJeremy L Thompson // LCOV_EXCL_START 5254b3e95d5SJeremy L Thompson case CEED_EVAL_DIV: 5264b3e95d5SJeremy L Thompson case CEED_EVAL_CURL: 5274b3e95d5SJeremy L Thompson break; // TODO: Not implemented 5284b3e95d5SJeremy L Thompson // LCOV_EXCL_STOP 5294b3e95d5SJeremy L Thompson } 5304b3e95d5SJeremy L Thompson } else { 5314b3e95d5SJeremy L Thompson switch (eval_mode) { 5324b3e95d5SJeremy L Thompson case CEED_EVAL_NONE: 5334b3e95d5SJeremy L Thompson code << " CeedScalar *r_e" << var_suffix << " = r_q" << var_suffix << ";\n"; 5344b3e95d5SJeremy L Thompson break; // No action 5354b3e95d5SJeremy L Thompson case CEED_EVAL_INTERP: 536e93651e5SJeremy L Thompson code << " CeedScalar *r_e" << var_suffix << " = r_e_scratch;\n"; 5378b97b69aSJeremy L Thompson if (is_at_points) { 538dc007f05SJeremy L Thompson std::string function_name = (dim == 1 ? "InterpTranspose" : "InterpTransposeTensor") + std::to_string(dim) + "d"; 539dc007f05SJeremy L Thompson 54099421279SJeremy L Thompson code << " " << function_name << "<num_comp" << var_suffix << ", " << P_name << ", " << Q_name << ", OP_T_1D>(data, r_c" << var_suffix 54199421279SJeremy L Thompson << ", s_B" << var_suffix << ", r_e" << var_suffix << ");\n"; 5428b97b69aSJeremy L Thompson } else { 543dc007f05SJeremy L Thompson std::string function_name = 544dc007f05SJeremy L Thompson is_tensor ? ((dim == 1 ? "InterpTranspose" : "InterpTransposeTensor") + std::to_string(dim) + "d") : "InterpTransposeNonTensor"; 545dc007f05SJeremy L Thompson 54699421279SJeremy L Thompson code << " " << function_name << "<num_comp" << var_suffix << ", " << P_name << ", " << Q_name << ", OP_T_1D>(data, r_q" << var_suffix 54799421279SJeremy L Thompson << ", s_B" << var_suffix << ", r_e" << var_suffix << ");\n"; 5488b97b69aSJeremy L Thompson } 5494b3e95d5SJeremy L Thompson break; 5504b3e95d5SJeremy L Thompson case CEED_EVAL_GRAD: 551e93651e5SJeremy L Thompson code << " CeedScalar *r_e" << var_suffix << " = r_e_scratch;\n"; 5528b97b69aSJeremy L Thompson if (is_at_points) { 553dc007f05SJeremy L Thompson std::string function_name = (dim == 1 ? "InterpTranspose" : "InterpTransposeTensor") + std::to_string(dim) + "d"; 554dc007f05SJeremy L Thompson 55599421279SJeremy L Thompson code << " " << function_name << "<num_comp" << var_suffix << ", " << P_name << ", " << Q_name << ", OP_T_1D>(data, r_c" << var_suffix 55699421279SJeremy L Thompson << ", s_B" << var_suffix << ", r_e" << var_suffix << ");\n"; 5578b97b69aSJeremy L Thompson } else if (use_3d_slices) { 558dc007f05SJeremy L Thompson std::string function_name = (dim == 1 ? "InterpTranspose" : "InterpTransposeTensor") + std::to_string(dim) + "d"; 559dc007f05SJeremy L Thompson 56099421279SJeremy L Thompson code << " " << function_name << "<num_comp" << var_suffix << ", " << P_name << ", " << Q_name << ", OP_T_1D>(data, r_q" << var_suffix 56199421279SJeremy L Thompson << ", s_B" << var_suffix << ", r_e" << var_suffix << ");\n"; 562dc007f05SJeremy L Thompson } else if (is_tensor) { 563dc007f05SJeremy L Thompson bool is_collocated = dim == 3 && Q_1d >= P_1d; 564dc007f05SJeremy L Thompson std::string function_name = 565dc007f05SJeremy L Thompson (dim == 1 ? "GradTranspose" : (is_collocated ? "GradTransposeTensorCollocated" : "GradTransposeTensor")) + std::to_string(dim) + "d"; 566dc007f05SJeremy L Thompson 56799421279SJeremy L Thompson code << " " << function_name << "<num_comp" << var_suffix << ", " << P_name << ", " << Q_name << ", OP_T_1D>(data, r_q" << var_suffix 56899421279SJeremy L Thompson << ", s_B" << var_suffix << ", s_G" << var_suffix << ", r_e" << var_suffix << ");\n"; 5694b3e95d5SJeremy L Thompson } else { 570dc007f05SJeremy L Thompson std::string function_name = "GradTransposeNonTensor"; 571dc007f05SJeremy L Thompson 57299421279SJeremy L Thompson code << " " << function_name << "<num_comp" << var_suffix << ", dim, " << P_name << ", " << Q_name << ", OP_T_1D>(data, r_q" 57399421279SJeremy L Thompson << var_suffix << ", s_G" << var_suffix << ", r_e" << var_suffix << ");\n"; 5744b3e95d5SJeremy L Thompson } 5754b3e95d5SJeremy L Thompson break; 5764b3e95d5SJeremy L Thompson // LCOV_EXCL_START 5774b3e95d5SJeremy L Thompson case CEED_EVAL_WEIGHT: 5784b3e95d5SJeremy L Thompson break; // Should not occur 5794b3e95d5SJeremy L Thompson case CEED_EVAL_DIV: 5804b3e95d5SJeremy L Thompson case CEED_EVAL_CURL: 5814b3e95d5SJeremy L Thompson break; // TODO: Not implemented 5824b3e95d5SJeremy L Thompson // LCOV_EXCL_STOP 5834b3e95d5SJeremy L Thompson } 5844b3e95d5SJeremy L Thompson } 585681d0ea7SJeremy L Thompson CeedCallBackend(CeedBasisDestroy(&basis)); 5864b3e95d5SJeremy L Thompson return CEED_ERROR_SUCCESS; 5874b3e95d5SJeremy L Thompson } 5884b3e95d5SJeremy L Thompson 5894b3e95d5SJeremy L Thompson //------------------------------------------------------------------------------ 5904b3e95d5SJeremy L Thompson // QFunction 5914b3e95d5SJeremy L Thompson //------------------------------------------------------------------------------ 5928b97b69aSJeremy L Thompson static int CeedOperatorBuildKernelQFunction_Cuda_gen(std::ostringstream &code, CeedOperator_Cuda_gen *data, CeedInt dim, CeedInt max_num_points, 5938b97b69aSJeremy L Thompson CeedInt num_input_fields, CeedOperatorField *op_input_fields, 5948b97b69aSJeremy L Thompson CeedQFunctionField *qf_input_fields, CeedInt num_output_fields, 5958b97b69aSJeremy L Thompson CeedOperatorField *op_output_fields, CeedQFunctionField *qf_output_fields, 596dc007f05SJeremy L Thompson std::string qfunction_name, CeedInt Q_1d, bool is_tensor, bool is_at_points, 597dc007f05SJeremy L Thompson bool use_3d_slices) { 598dc007f05SJeremy L Thompson std::string Q_name = is_tensor ? "Q_1d" : "Q"; 5994b3e95d5SJeremy L Thompson CeedEvalMode eval_mode = CEED_EVAL_NONE; 6004b3e95d5SJeremy L Thompson CeedElemRestriction elem_rstr; 6014b3e95d5SJeremy L Thompson 6028b97b69aSJeremy L Thompson // Setup output arrays 6034b3e95d5SJeremy L Thompson code << "\n // -- Output field setup\n"; 6044b3e95d5SJeremy L Thompson for (CeedInt i = 0; i < num_output_fields; i++) { 60559fa3f92SJeremy L Thompson const char *field_name; 6064b3e95d5SJeremy L Thompson std::string var_suffix = "_out_" + std::to_string(i); 6074b3e95d5SJeremy L Thompson 60859fa3f92SJeremy L Thompson CeedCallBackend(CeedOperatorFieldGetName(op_output_fields[i], &field_name)); 60959fa3f92SJeremy L Thompson code << " // ---- Output field " << i << ": " << field_name << "\n"; 6104b3e95d5SJeremy L Thompson CeedCallBackend(CeedQFunctionFieldGetEvalMode(qf_output_fields[i], &eval_mode)); 6118b97b69aSJeremy L Thompson switch (eval_mode) { 6128b97b69aSJeremy L Thompson case CEED_EVAL_NONE: 6138b97b69aSJeremy L Thompson if (is_at_points) { 6148b97b69aSJeremy L Thompson code << " CeedScalar r_q" << var_suffix << "[num_comp" << var_suffix << "];\n"; 6158b97b69aSJeremy L Thompson } else { 616dc007f05SJeremy L Thompson code << " CeedScalar r_q" << var_suffix << "[num_comp" << var_suffix << "*" << (is_tensor && (dim >= 3) ? Q_name : "1") << "];\n"; 6174b3e95d5SJeremy L Thompson } 6188b97b69aSJeremy L Thompson break; 6198b97b69aSJeremy L Thompson case CEED_EVAL_INTERP: 6208b97b69aSJeremy L Thompson if (is_at_points) { 6218b97b69aSJeremy L Thompson // Accumulator for point data 622dc007f05SJeremy L Thompson code << " CeedScalar r_c" << var_suffix << "[num_comp" << var_suffix << "*" << (dim >= 3 ? Q_name : "1") << "];\n"; 623dc007f05SJeremy L Thompson code << " for (CeedInt i = 0; i < num_comp" << var_suffix << "*" << (dim >= 3 ? Q_name : "1") << "; i++) {\n"; 6248b97b69aSJeremy L Thompson code << " r_c" << var_suffix << "[i] = 0.0;\n"; 6258b97b69aSJeremy L Thompson code << " }\n"; 6268b97b69aSJeremy L Thompson } else { 627dc007f05SJeremy L Thompson code << " CeedScalar r_q" << var_suffix << "[num_comp" << var_suffix << "*" << (is_tensor && (dim >= 3) ? Q_name : "1") << "];\n"; 6288b97b69aSJeremy L Thompson } 6298b97b69aSJeremy L Thompson break; 6308b97b69aSJeremy L Thompson case CEED_EVAL_GRAD: 6318b97b69aSJeremy L Thompson if (is_at_points) { 6328b97b69aSJeremy L Thompson // Accumulator for point data 633dc007f05SJeremy L Thompson code << " CeedScalar r_c" << var_suffix << "[num_comp" << var_suffix << "*" << (dim >= 3 ? Q_name : "1") << "*dim];\n"; 634dc007f05SJeremy L Thompson code << " for (CeedInt i = 0; i < num_comp" << var_suffix << "*" << (dim >= 3 ? Q_name : "1") << "; i++) {\n"; 6358b97b69aSJeremy L Thompson code << " r_c" << var_suffix << "[i] = 0.0;\n"; 6368b97b69aSJeremy L Thompson code << " }\n"; 6378b97b69aSJeremy L Thompson } else if (use_3d_slices) { 6384b3e95d5SJeremy L Thompson // Accumulator for gradient slices 6394b3e95d5SJeremy L Thompson code << " CeedScalar r_q" << var_suffix << "[num_comp" << var_suffix << "*" << Q_name << "];\n"; 6404b3e95d5SJeremy L Thompson code << " for (CeedInt i = 0; i < num_comp" << var_suffix << "*" << Q_name << "; i++) {\n"; 6414b3e95d5SJeremy L Thompson code << " r_q" << var_suffix << "[i] = 0.0;\n"; 6424b3e95d5SJeremy L Thompson code << " }\n"; 6434b3e95d5SJeremy L Thompson } else { 644dc007f05SJeremy L Thompson code << " CeedScalar r_q" << var_suffix << "[num_comp" << var_suffix << "*dim*" << (is_tensor && (dim >= 3) ? Q_name : "1") << "];\n"; 6454b3e95d5SJeremy L Thompson } 6468b97b69aSJeremy L Thompson break; 6478b97b69aSJeremy L Thompson case CEED_EVAL_WEIGHT: 6488b97b69aSJeremy L Thompson break; 6498b97b69aSJeremy L Thompson // LCOV_EXCL_START 6508b97b69aSJeremy L Thompson case CEED_EVAL_DIV: 6518b97b69aSJeremy L Thompson case CEED_EVAL_CURL: 6528b97b69aSJeremy L Thompson break; // TODO: Not implemented 6538b97b69aSJeremy L Thompson // LCOV_EXCL_STOP 6544b3e95d5SJeremy L Thompson } 6554b3e95d5SJeremy L Thompson } 6564b3e95d5SJeremy L Thompson 6578b97b69aSJeremy L Thompson if (is_at_points) { 6588b97b69aSJeremy L Thompson // We need to handle batches of points 6598b97b69aSJeremy L Thompson code << "\n // Note: Using batches of points\n"; 6608b97b69aSJeremy L Thompson code << " const CeedInt point_loop_bound = (blockDim.x * blockDim.y) * ceil(1.0 * max_num_points / (blockDim.x * blockDim.y));\n\n"; 6618b97b69aSJeremy L Thompson code << " #pragma unroll\n"; 6628b97b69aSJeremy L Thompson code << " for (CeedInt i = threadIdx.x + threadIdx.y * blockDim.x; i < point_loop_bound; i += blockDim.x * blockDim.y) {\n"; 6638b97b69aSJeremy L Thompson code << " const CeedInt p = i % max_num_points;\n\n"; 6648b97b69aSJeremy L Thompson 6658b97b69aSJeremy L Thompson code << " // -- Coordinates\n"; 6668b97b69aSJeremy L Thompson code << " CeedScalar r_x[dim];\n"; 6678b97b69aSJeremy L Thompson code << " ReadPoint<dim, coords_comp_stride, max_num_points>(data, elem, p, max_num_points, points.indices, points.coords, r_x);\n\n"; 6688b97b69aSJeremy L Thompson 6698b97b69aSJeremy L Thompson code << " // -- Input fields\n"; 6708b97b69aSJeremy L Thompson for (CeedInt i = 0; i < num_input_fields; i++) { 67159fa3f92SJeremy L Thompson const char *field_name; 6728b97b69aSJeremy L Thompson std::string var_suffix = "_in_" + std::to_string(i); 673f725b54bSJeremy L Thompson std::string P_name = "P_1d" + var_suffix; 6748b97b69aSJeremy L Thompson 67559fa3f92SJeremy L Thompson CeedCallBackend(CeedOperatorFieldGetName(op_input_fields[i], &field_name)); 67659fa3f92SJeremy L Thompson code << " // ---- Input field " << i << ": " << field_name << "\n"; 6778b97b69aSJeremy L Thompson CeedCallBackend(CeedQFunctionFieldGetEvalMode(qf_input_fields[i], &eval_mode)); 6788b97b69aSJeremy L Thompson // Basis action 6798b97b69aSJeremy L Thompson code << " // EvalMode: " << CeedEvalModes[eval_mode] << "\n"; 6808b97b69aSJeremy L Thompson switch (eval_mode) { 6818b97b69aSJeremy L Thompson case CEED_EVAL_NONE: 6828b97b69aSJeremy L Thompson code << " CeedScalar r_s" << var_suffix << "[num_comp" << var_suffix << "];\n"; 6838b97b69aSJeremy L Thompson code << " ReadPoint<num_comp" << var_suffix << ", comp_stride" << var_suffix 6848b97b69aSJeremy L Thompson << ", max_num_points>(data, elem, p, max_num_points, indices.inputs[" << i << "], d" << var_suffix << ", r_s" << var_suffix << ");\n"; 6858b97b69aSJeremy L Thompson break; 6868b97b69aSJeremy L Thompson case CEED_EVAL_INTERP: 6878b97b69aSJeremy L Thompson code << " CeedScalar r_s" << var_suffix << "[num_comp" << var_suffix << "];\n"; 688f725b54bSJeremy L Thompson code << " InterpAtPoints" << dim << "d<num_comp" << var_suffix << ", max_num_points, " << P_name << ", " << Q_name << ">(data, i, r_c" 689f725b54bSJeremy L Thompson << var_suffix << ", r_x, r_s" << var_suffix << ");\n"; 6908b97b69aSJeremy L Thompson break; 6918b97b69aSJeremy L Thompson case CEED_EVAL_GRAD: 6928b97b69aSJeremy L Thompson code << " CeedScalar r_s" << var_suffix << "[num_comp" << var_suffix << "*dim];\n"; 693f725b54bSJeremy L Thompson code << " GradAtPoints" << dim << "d<num_comp" << var_suffix << ", max_num_points, " << P_name << ", " << Q_name << ">(data, i, r_c" 694f725b54bSJeremy L Thompson << var_suffix << ", r_x, r_s" << var_suffix << ");\n"; 6958b97b69aSJeremy L Thompson break; 6968b97b69aSJeremy L Thompson case CEED_EVAL_WEIGHT: 6978b97b69aSJeremy L Thompson code << " CeedScalar r_s" << var_suffix << "[1];\n"; 6988b97b69aSJeremy L Thompson code << " r_s" << var_suffix << "[0] = 1.0;\n"; 6998b97b69aSJeremy L Thompson break; 7008b97b69aSJeremy L Thompson // LCOV_EXCL_START 7018b97b69aSJeremy L Thompson case CEED_EVAL_DIV: 7028b97b69aSJeremy L Thompson case CEED_EVAL_CURL: 7038b97b69aSJeremy L Thompson break; // TODO: Not implemented 7048b97b69aSJeremy L Thompson // LCOV_EXCL_STOP 7058b97b69aSJeremy L Thompson } 7068b97b69aSJeremy L Thompson } 7078b97b69aSJeremy L Thompson code << "\n // -- Output fields\n"; 7088b97b69aSJeremy L Thompson for (CeedInt i = 0; i < num_output_fields; i++) { 70959fa3f92SJeremy L Thompson const char *field_name; 7108b97b69aSJeremy L Thompson std::string var_suffix = "_out_" + std::to_string(i); 7118b97b69aSJeremy L Thompson 71259fa3f92SJeremy L Thompson CeedCallBackend(CeedOperatorFieldGetName(op_output_fields[i], &field_name)); 71359fa3f92SJeremy L Thompson code << " // ---- Output field " << i << ": " << field_name << "\n"; 7148b97b69aSJeremy L Thompson CeedCallBackend(CeedQFunctionFieldGetEvalMode(qf_output_fields[i], &eval_mode)); 7158b97b69aSJeremy L Thompson // Basis action 7168b97b69aSJeremy L Thompson switch (eval_mode) { 7178b97b69aSJeremy L Thompson case CEED_EVAL_NONE: 7188b97b69aSJeremy L Thompson code << " CeedScalar r_s" << var_suffix << "[num_comp" << var_suffix << "];\n"; 7198b97b69aSJeremy L Thompson break; 7208b97b69aSJeremy L Thompson case CEED_EVAL_INTERP: 7218b97b69aSJeremy L Thompson code << " CeedScalar r_s" << var_suffix << "[num_comp" << var_suffix << "];\n"; 7228b97b69aSJeremy L Thompson break; 7238b97b69aSJeremy L Thompson case CEED_EVAL_GRAD: 7248b97b69aSJeremy L Thompson code << " CeedScalar r_s" << var_suffix << "[num_comp" << var_suffix << "*dim];\n"; 7258b97b69aSJeremy L Thompson break; 7268b97b69aSJeremy L Thompson // LCOV_EXCL_START 7278b97b69aSJeremy L Thompson case CEED_EVAL_WEIGHT: 7288b97b69aSJeremy L Thompson break; // Should not occur 7298b97b69aSJeremy L Thompson case CEED_EVAL_DIV: 7308b97b69aSJeremy L Thompson case CEED_EVAL_CURL: 7318b97b69aSJeremy L Thompson break; // TODO: Not implemented 7328b97b69aSJeremy L Thompson // LCOV_EXCL_STOP 7338b97b69aSJeremy L Thompson } 7348b97b69aSJeremy L Thompson } 7358b97b69aSJeremy L Thompson 7368b97b69aSJeremy L Thompson } else if (use_3d_slices) { 7374b3e95d5SJeremy L Thompson // We treat quadrature points per slice in 3d to save registers 7384b3e95d5SJeremy L Thompson code << "\n // Note: Using planes of 3D elements\n"; 7394b3e95d5SJeremy L Thompson code << " #pragma unroll\n"; 7404b3e95d5SJeremy L Thompson code << " for (CeedInt q = 0; q < " << Q_name << "; q++) {\n"; 7414b3e95d5SJeremy L Thompson code << " // -- Input fields\n"; 7424b3e95d5SJeremy L Thompson for (CeedInt i = 0; i < num_input_fields; i++) { 74359fa3f92SJeremy L Thompson const char *field_name; 7444b3e95d5SJeremy L Thompson std::string var_suffix = "_in_" + std::to_string(i); 7454b3e95d5SJeremy L Thompson 74659fa3f92SJeremy L Thompson CeedCallBackend(CeedOperatorFieldGetName(op_input_fields[i], &field_name)); 74759fa3f92SJeremy L Thompson code << " // ---- Input field " << i << ": " << field_name << "\n"; 7484b3e95d5SJeremy L Thompson CeedCallBackend(CeedQFunctionFieldGetEvalMode(qf_input_fields[i], &eval_mode)); 7494b3e95d5SJeremy L Thompson // Basis action 7504b3e95d5SJeremy L Thompson code << " // EvalMode: " << CeedEvalModes[eval_mode] << "\n"; 7514b3e95d5SJeremy L Thompson switch (eval_mode) { 7524b3e95d5SJeremy L Thompson case CEED_EVAL_NONE: 7534b3e95d5SJeremy L Thompson bool is_strided; 7544b3e95d5SJeremy L Thompson 7554b3e95d5SJeremy L Thompson code << " CeedScalar r_s" << var_suffix << "[num_comp" << var_suffix << "];\n"; 7564b3e95d5SJeremy L Thompson 7574b3e95d5SJeremy L Thompson CeedCallBackend(CeedOperatorFieldGetElemRestriction(op_input_fields[i], &elem_rstr)); 7584b3e95d5SJeremy L Thompson CeedCallBackend(CeedElemRestrictionIsStrided(elem_rstr, &is_strided)); 7594b3e95d5SJeremy L Thompson if (is_strided) { 7604b3e95d5SJeremy L Thompson bool has_backend_strides; 7614b3e95d5SJeremy L Thompson CeedInt num_elem, elem_size; 7624b3e95d5SJeremy L Thompson 7634b3e95d5SJeremy L Thompson CeedCallBackend(CeedElemRestrictionGetElementSize(elem_rstr, &elem_size)); 7644b3e95d5SJeremy L Thompson CeedCallBackend(CeedElemRestrictionHasBackendStrides(elem_rstr, &has_backend_strides)); 7654b3e95d5SJeremy L Thompson CeedCallBackend(CeedElemRestrictionGetNumElements(elem_rstr, &num_elem)); 7664b3e95d5SJeremy L Thompson CeedInt strides[3] = {1, elem_size * num_elem, elem_size}; 7674b3e95d5SJeremy L Thompson 7684b3e95d5SJeremy L Thompson if (!has_backend_strides) { 7694b3e95d5SJeremy L Thompson CeedCallBackend(CeedElemRestrictionGetStrides(elem_rstr, strides)); 7704b3e95d5SJeremy L Thompson } 7714b3e95d5SJeremy L Thompson code << " // Strides: {" << strides[0] << ", " << strides[1] << ", " << strides[2] << "}\n"; 772f815fac9SJeremy L Thompson code << " ReadEVecSliceStrided3d<num_comp" << var_suffix << ", " << Q_name << ", " << strides[0] << ", " << strides[1] << ", " 7734b3e95d5SJeremy L Thompson << strides[2] << ">(data, elem, q, d" << var_suffix << ", r_s" << var_suffix << ");\n"; 7744b3e95d5SJeremy L Thompson } else { 7754b3e95d5SJeremy L Thompson CeedSize l_size = 0; 7764b3e95d5SJeremy L Thompson CeedInt comp_stride; 7774b3e95d5SJeremy L Thompson CeedElemRestriction_Cuda *rstr_data; 7784b3e95d5SJeremy L Thompson 7794b3e95d5SJeremy L Thompson CeedCallBackend(CeedElemRestrictionGetLVectorSize(elem_rstr, &l_size)); 7804b3e95d5SJeremy L Thompson code << " const CeedInt l_size" << var_suffix << " = " << l_size << ";\n"; 7814b3e95d5SJeremy L Thompson CeedCallBackend(CeedElemRestrictionGetCompStride(elem_rstr, &comp_stride)); 7824b3e95d5SJeremy L Thompson code << " // CompStride: " << comp_stride << "\n"; 7834b3e95d5SJeremy L Thompson CeedCallBackend(CeedElemRestrictionGetData(elem_rstr, &rstr_data)); 7844b3e95d5SJeremy L Thompson data->indices.inputs[i] = (CeedInt *)rstr_data->d_offsets; 785f815fac9SJeremy L Thompson code << " ReadEVecSliceStandard3d<num_comp" << var_suffix << ", " << comp_stride << ", " << Q_name << ">(data, l_size" << var_suffix 7864b3e95d5SJeremy L Thompson << ", elem, q, indices.inputs[" << i << "], d" << var_suffix << ", r_s" << var_suffix << ");\n"; 7874b3e95d5SJeremy L Thompson } 788681d0ea7SJeremy L Thompson CeedCallBackend(CeedElemRestrictionDestroy(&elem_rstr)); 7894b3e95d5SJeremy L Thompson break; 7904b3e95d5SJeremy L Thompson case CEED_EVAL_INTERP: 7914b3e95d5SJeremy L Thompson code << " CeedScalar r_s" << var_suffix << "[num_comp" << var_suffix << "];\n"; 7924b3e95d5SJeremy L Thompson code << " for (CeedInt j = 0; j < num_comp" << var_suffix << "; j++) {\n"; 7934b3e95d5SJeremy L Thompson code << " r_s" << var_suffix << "[j] = r_q" << var_suffix << "[q + j*" << Q_name << "];\n"; 7944b3e95d5SJeremy L Thompson code << " }\n"; 7954b3e95d5SJeremy L Thompson break; 7964b3e95d5SJeremy L Thompson case CEED_EVAL_GRAD: 7974b3e95d5SJeremy L Thompson code << " CeedScalar r_s" << var_suffix << "[num_comp" << var_suffix << "*dim];\n"; 79899421279SJeremy L Thompson code << " GradColloSlice3d<num_comp" << var_suffix << ", " << Q_name << ", OP_T_1D>(data, q, r_q" << var_suffix << ", s_G" 79999421279SJeremy L Thompson << var_suffix << ", r_s" << var_suffix << ");\n"; 8004b3e95d5SJeremy L Thompson break; 8014b3e95d5SJeremy L Thompson case CEED_EVAL_WEIGHT: 8024b3e95d5SJeremy L Thompson code << " CeedScalar r_s" << var_suffix << "[1];\n"; 8034b3e95d5SJeremy L Thompson code << " r_s" << var_suffix << "[0] = r_q" << var_suffix << "[q];\n"; 8048b97b69aSJeremy L Thompson break; 8054b3e95d5SJeremy L Thompson // LCOV_EXCL_START 8064b3e95d5SJeremy L Thompson case CEED_EVAL_DIV: 8074b3e95d5SJeremy L Thompson case CEED_EVAL_CURL: 8084b3e95d5SJeremy L Thompson break; // TODO: Not implemented 8094b3e95d5SJeremy L Thompson // LCOV_EXCL_STOP 8104b3e95d5SJeremy L Thompson } 8114b3e95d5SJeremy L Thompson } 8124b3e95d5SJeremy L Thompson code << "\n // -- Output fields\n"; 8134b3e95d5SJeremy L Thompson for (CeedInt i = 0; i < num_output_fields; i++) { 81459fa3f92SJeremy L Thompson const char *field_name; 8154b3e95d5SJeremy L Thompson std::string var_suffix = "_out_" + std::to_string(i); 8164b3e95d5SJeremy L Thompson 81759fa3f92SJeremy L Thompson CeedCallBackend(CeedOperatorFieldGetName(op_output_fields[i], &field_name)); 81859fa3f92SJeremy L Thompson code << " // ---- Output field " << i << ": " << field_name << "\n"; 8194b3e95d5SJeremy L Thompson CeedCallBackend(CeedQFunctionFieldGetEvalMode(qf_output_fields[i], &eval_mode)); 8204b3e95d5SJeremy L Thompson // Basis action 8214b3e95d5SJeremy L Thompson switch (eval_mode) { 8224b3e95d5SJeremy L Thompson case CEED_EVAL_NONE: 8234b3e95d5SJeremy L Thompson code << " CeedScalar r_s" << var_suffix << "[num_comp" << var_suffix << "];\n"; 8248b97b69aSJeremy L Thompson break; 8254b3e95d5SJeremy L Thompson case CEED_EVAL_INTERP: 8264b3e95d5SJeremy L Thompson code << " CeedScalar r_s" << var_suffix << "[num_comp" << var_suffix << "];\n"; 8274b3e95d5SJeremy L Thompson break; 8284b3e95d5SJeremy L Thompson case CEED_EVAL_GRAD: 8294b3e95d5SJeremy L Thompson code << " CeedScalar r_s" << var_suffix << "[num_comp" << var_suffix << "*dim];\n"; 8304b3e95d5SJeremy L Thompson break; 8314b3e95d5SJeremy L Thompson // LCOV_EXCL_START 8324b3e95d5SJeremy L Thompson case CEED_EVAL_WEIGHT: 8334b3e95d5SJeremy L Thompson break; // Should not occur 8344b3e95d5SJeremy L Thompson case CEED_EVAL_DIV: 8354b3e95d5SJeremy L Thompson case CEED_EVAL_CURL: 8364b3e95d5SJeremy L Thompson break; // TODO: Not implemented 8374b3e95d5SJeremy L Thompson // LCOV_EXCL_STOP 8384b3e95d5SJeremy L Thompson } 8394b3e95d5SJeremy L Thompson } 8404b3e95d5SJeremy L Thompson } else { 8414b3e95d5SJeremy L Thompson code << "\n // Note: Using full elements\n"; 8424b3e95d5SJeremy L Thompson code << " {\n"; 8434b3e95d5SJeremy L Thompson code << " // -- Input fields\n"; 8444b3e95d5SJeremy L Thompson for (CeedInt i = 0; i < num_input_fields; i++) { 84559fa3f92SJeremy L Thompson const char *field_name; 84659fa3f92SJeremy L Thompson 84759fa3f92SJeremy L Thompson CeedCallBackend(CeedOperatorFieldGetName(op_input_fields[i], &field_name)); 84859fa3f92SJeremy L Thompson code << " // ---- Input field " << i << ": " << field_name << "\n"; 8494b3e95d5SJeremy L Thompson code << " CeedScalar *r_s_in_" << i << " = r_q_in_" << i << ";\n"; 8504b3e95d5SJeremy L Thompson } 8514b3e95d5SJeremy L Thompson code << " // -- Output fields\n"; 8524b3e95d5SJeremy L Thompson for (CeedInt i = 0; i < num_output_fields; i++) { 85359fa3f92SJeremy L Thompson const char *field_name; 85459fa3f92SJeremy L Thompson 85559fa3f92SJeremy L Thompson CeedCallBackend(CeedOperatorFieldGetName(op_output_fields[i], &field_name)); 85659fa3f92SJeremy L Thompson code << " // ---- Output field " << i << ": " << field_name << "\n"; 8574b3e95d5SJeremy L Thompson code << " CeedScalar *r_s_out_" << i << " = r_q_out_" << i << ";\n"; 8584b3e95d5SJeremy L Thompson } 8594b3e95d5SJeremy L Thompson } 8604b3e95d5SJeremy L Thompson 8614b3e95d5SJeremy L Thompson // Input and output buffers 8624b3e95d5SJeremy L Thompson code << "\n // -- QFunction inputs and outputs\n"; 8634b3e95d5SJeremy L Thompson code << " // ---- Inputs\n"; 8644b3e95d5SJeremy L Thompson code << " CeedScalar *inputs[" << CeedIntMax(num_input_fields, 1) << "];\n"; 8654b3e95d5SJeremy L Thompson for (CeedInt i = 0; i < num_input_fields; i++) { 86659fa3f92SJeremy L Thompson const char *field_name; 86759fa3f92SJeremy L Thompson 86859fa3f92SJeremy L Thompson CeedCallBackend(CeedOperatorFieldGetName(op_input_fields[i], &field_name)); 86959fa3f92SJeremy L Thompson code << " // ------ Input field " << i << ": " << field_name << "\n"; 8704b3e95d5SJeremy L Thompson code << " inputs[" << i << "] = r_s_in_" << i << ";\n"; 8714b3e95d5SJeremy L Thompson } 8724b3e95d5SJeremy L Thompson code << " // ---- Outputs\n"; 8734b3e95d5SJeremy L Thompson code << " CeedScalar *outputs[" << CeedIntMax(num_output_fields, 1) << "];\n"; 8744b3e95d5SJeremy L Thompson for (CeedInt i = 0; i < num_output_fields; i++) { 87559fa3f92SJeremy L Thompson const char *field_name; 87659fa3f92SJeremy L Thompson 87759fa3f92SJeremy L Thompson CeedCallBackend(CeedOperatorFieldGetName(op_output_fields[i], &field_name)); 87859fa3f92SJeremy L Thompson code << " // ------ Output field " << i << ": " << field_name << "\n"; 8794b3e95d5SJeremy L Thompson code << " outputs[" << i << "] = r_s_out_" << i << ";\n"; 8804b3e95d5SJeremy L Thompson } 8814b3e95d5SJeremy L Thompson 8824b3e95d5SJeremy L Thompson // Apply QFunction 8834b3e95d5SJeremy L Thompson code << "\n // -- Apply QFunction\n"; 8844b3e95d5SJeremy L Thompson code << " " << qfunction_name << "(ctx, "; 885dc007f05SJeremy L Thompson if (dim != 3 || is_at_points || use_3d_slices || !is_tensor) { 8864b3e95d5SJeremy L Thompson code << "1"; 8874b3e95d5SJeremy L Thompson } else { 888dc007f05SJeremy L Thompson code << Q_name; 8894b3e95d5SJeremy L Thompson } 8904b3e95d5SJeremy L Thompson code << ", inputs, outputs);\n"; 8914b3e95d5SJeremy L Thompson 8928b97b69aSJeremy L Thompson if (is_at_points) { 8938b97b69aSJeremy L Thompson // Map back to coefficients 8948b97b69aSJeremy L Thompson code << "\n // -- Output fields\n"; 8958b97b69aSJeremy L Thompson for (CeedInt i = 0; i < num_output_fields; i++) { 89659fa3f92SJeremy L Thompson const char *field_name; 8978b97b69aSJeremy L Thompson std::string var_suffix = "_out_" + std::to_string(i); 8988b97b69aSJeremy L Thompson std::string P_name = "P_1d" + var_suffix; 8998b97b69aSJeremy L Thompson 90059fa3f92SJeremy L Thompson CeedCallBackend(CeedOperatorFieldGetName(op_output_fields[i], &field_name)); 90159fa3f92SJeremy L Thompson code << " // ---- Output field " << i << ": " << field_name << "\n"; 9028b97b69aSJeremy L Thompson CeedCallBackend(CeedQFunctionFieldGetEvalMode(qf_output_fields[i], &eval_mode)); 9038b97b69aSJeremy L Thompson // Basis action 9048b97b69aSJeremy L Thompson code << " // EvalMode: " << CeedEvalModes[eval_mode] << "\n"; 9058b97b69aSJeremy L Thompson switch (eval_mode) { 9068b97b69aSJeremy L Thompson case CEED_EVAL_NONE: { 9078b97b69aSJeremy L Thompson CeedInt comp_stride; 9088b97b69aSJeremy L Thompson CeedElemRestriction elem_rstr; 9098b97b69aSJeremy L Thompson 9108b97b69aSJeremy L Thompson CeedCallBackend(CeedOperatorFieldGetElemRestriction(op_output_fields[i], &elem_rstr)); 9118b97b69aSJeremy L Thompson CeedCallBackend(CeedElemRestrictionGetCompStride(elem_rstr, &comp_stride)); 9128b97b69aSJeremy L Thompson CeedCallBackend(CeedElemRestrictionDestroy(&elem_rstr)); 9138b97b69aSJeremy L Thompson code << " const CeedInt comp_stride" << var_suffix << " = " << comp_stride << ";\n"; 9148b97b69aSJeremy L Thompson code << " WritePoint<num_comp" << var_suffix << ", comp_stride" << var_suffix 9158b97b69aSJeremy L Thompson << ", max_num_points>(data, elem, i, points.num_per_elem[elem], indices.outputs[" << i << "]" 9168b97b69aSJeremy L Thompson << ", r_s" << var_suffix << ", d" << var_suffix << ");\n"; 9178b97b69aSJeremy L Thompson break; 9188b97b69aSJeremy L Thompson } 9198b97b69aSJeremy L Thompson case CEED_EVAL_INTERP: 9208b97b69aSJeremy L Thompson code << " if (i >= points.num_per_elem[elem]) {\n"; 9218b97b69aSJeremy L Thompson code << " for (CeedInt j = 0; j < num_comp" << var_suffix << "; j++) r_s" << var_suffix << "[j] = 0.0;\n"; 9228b97b69aSJeremy L Thompson code << " }\n"; 923f725b54bSJeremy L Thompson code << " InterpTransposeAtPoints" << dim << "d<num_comp" << var_suffix << ", max_num_points, " << P_name << ", " << Q_name 924f725b54bSJeremy L Thompson << ">(data, i, r_s" << var_suffix << ", r_x, r_c" << var_suffix << ");\n"; 9258b97b69aSJeremy L Thompson break; 9268b97b69aSJeremy L Thompson case CEED_EVAL_GRAD: 9278b97b69aSJeremy L Thompson code << " if (i >= points.num_per_elem[elem]) {\n"; 9288b97b69aSJeremy L Thompson code << " for (CeedInt j = 0; j < num_comp" << var_suffix << "*dim; j++) r_s" << var_suffix << "[j] = 0.0;\n"; 9298b97b69aSJeremy L Thompson code << " }\n"; 930f725b54bSJeremy L Thompson code << " GradTransposeAtPoints" << dim << "d<num_comp" << var_suffix << ", max_num_points, " << P_name << ", " << Q_name 931f725b54bSJeremy L Thompson << ">(data, i, r_s" << var_suffix << ", r_x, r_c" << var_suffix << ");\n"; 9328b97b69aSJeremy L Thompson break; 9338b97b69aSJeremy L Thompson // LCOV_EXCL_START 9348b97b69aSJeremy L Thompson case CEED_EVAL_WEIGHT: 9358b97b69aSJeremy L Thompson break; // Should not occur 9368b97b69aSJeremy L Thompson case CEED_EVAL_DIV: 9378b97b69aSJeremy L Thompson case CEED_EVAL_CURL: 9388b97b69aSJeremy L Thompson break; // TODO: Not implemented 9398b97b69aSJeremy L Thompson // LCOV_EXCL_STOP 9408b97b69aSJeremy L Thompson } 9418b97b69aSJeremy L Thompson } 9428b97b69aSJeremy L Thompson } else if (use_3d_slices) { 9434b3e95d5SJeremy L Thompson // Copy or apply transpose grad, if needed 9448b97b69aSJeremy L Thompson code << "\n // -- Output fields\n"; 9454b3e95d5SJeremy L Thompson for (CeedInt i = 0; i < num_output_fields; i++) { 94659fa3f92SJeremy L Thompson const char *field_name; 9474b3e95d5SJeremy L Thompson std::string var_suffix = "_out_" + std::to_string(i); 9484b3e95d5SJeremy L Thompson std::string P_name = "P_1d" + var_suffix; 9494b3e95d5SJeremy L Thompson 95059fa3f92SJeremy L Thompson CeedCallBackend(CeedOperatorFieldGetName(op_output_fields[i], &field_name)); 95159fa3f92SJeremy L Thompson code << " // ---- Output field " << i << ": " << field_name << "\n"; 9524b3e95d5SJeremy L Thompson CeedCallBackend(CeedQFunctionFieldGetEvalMode(qf_output_fields[i], &eval_mode)); 9534b3e95d5SJeremy L Thompson // Basis action 9544b3e95d5SJeremy L Thompson code << " // EvalMode: " << CeedEvalModes[eval_mode] << "\n"; 9554b3e95d5SJeremy L Thompson switch (eval_mode) { 9564b3e95d5SJeremy L Thompson case CEED_EVAL_NONE: 9574b3e95d5SJeremy L Thompson code << " for (CeedInt j = 0; j < num_comp" << var_suffix << " ; j++) {\n"; 9584b3e95d5SJeremy L Thompson code << " r_q" << var_suffix << "[q + j*" << Q_name << "] = r_s" << var_suffix << "[j];\n"; 9594b3e95d5SJeremy L Thompson code << " }\n"; 9608b97b69aSJeremy L Thompson break; 9614b3e95d5SJeremy L Thompson case CEED_EVAL_INTERP: 9624b3e95d5SJeremy L Thompson code << " for (CeedInt j = 0; j < num_comp" << var_suffix << " ; j++) {\n"; 9634b3e95d5SJeremy L Thompson code << " r_q" << var_suffix << "[q + j*" << Q_name << "] = r_s" << var_suffix << "[j];\n"; 9644b3e95d5SJeremy L Thompson code << " }\n"; 9654b3e95d5SJeremy L Thompson break; 9664b3e95d5SJeremy L Thompson case CEED_EVAL_GRAD: 96799421279SJeremy L Thompson code << " GradColloSliceTranspose3d<num_comp" << var_suffix << ", " << Q_name << ", OP_T_1D>(data, q, r_s" << var_suffix << ", s_G" 968f815fac9SJeremy L Thompson << var_suffix << ", r_q" << var_suffix << ");\n"; 9694b3e95d5SJeremy L Thompson break; 9704b3e95d5SJeremy L Thompson // LCOV_EXCL_START 9714b3e95d5SJeremy L Thompson case CEED_EVAL_WEIGHT: 9724b3e95d5SJeremy L Thompson break; // Should not occur 9734b3e95d5SJeremy L Thompson case CEED_EVAL_DIV: 9744b3e95d5SJeremy L Thompson case CEED_EVAL_CURL: 9754b3e95d5SJeremy L Thompson break; // TODO: Not implemented 9764b3e95d5SJeremy L Thompson // LCOV_EXCL_STOP 9774b3e95d5SJeremy L Thompson } 9784b3e95d5SJeremy L Thompson } 9794b3e95d5SJeremy L Thompson } 9804b3e95d5SJeremy L Thompson code << " }\n"; 9814b3e95d5SJeremy L Thompson return CEED_ERROR_SUCCESS; 9824b3e95d5SJeremy L Thompson } 9834b3e95d5SJeremy L Thompson 9844b3e95d5SJeremy L Thompson //------------------------------------------------------------------------------ 985b2165e7aSSebastian Grimberg // Build single operator kernel 986ab213215SJeremy L Thompson //------------------------------------------------------------------------------ 987ddae5012SJeremy L Thompson extern "C" int CeedOperatorBuildKernel_Cuda_gen(CeedOperator op, bool *is_good_build) { 9888b97b69aSJeremy L Thompson bool is_tensor = true, is_at_points = false, use_3d_slices = false; 989241a4b83SYohann Ceed ceed; 9908b97b69aSJeremy L Thompson CeedInt Q_1d, num_input_fields, num_output_fields, dim = 1, max_num_points = 0, coords_comp_stride = 0; 991ca735530SJeremy L Thompson CeedQFunctionField *qf_input_fields, *qf_output_fields; 992ca735530SJeremy L Thompson CeedQFunction_Cuda_gen *qf_data; 993ca735530SJeremy L Thompson CeedQFunction qf; 994ca735530SJeremy L Thompson CeedOperatorField *op_input_fields, *op_output_fields; 995ca735530SJeremy L Thompson CeedOperator_Cuda_gen *data; 9964b3e95d5SJeremy L Thompson std::ostringstream code; 9974b3e95d5SJeremy L Thompson 998ddae5012SJeremy L Thompson CeedCallBackend(CeedOperatorGetData(op, &data)); 9994b3e95d5SJeremy L Thompson { 10004b3e95d5SJeremy L Thompson bool is_setup_done; 1001ca735530SJeremy L Thompson 1002ca735530SJeremy L Thompson CeedCallBackend(CeedOperatorIsSetupDone(op, &is_setup_done)); 1003ddae5012SJeremy L Thompson if (is_setup_done) { 1004ddae5012SJeremy L Thompson *is_good_build = !data->use_fallback; 1005ddae5012SJeremy L Thompson return CEED_ERROR_SUCCESS; 1006ddae5012SJeremy L Thompson } 1007ddae5012SJeremy L Thompson } 1008ddae5012SJeremy L Thompson 1009ddae5012SJeremy L Thompson // Check field compatibility 10108d12f40eSJeremy L Thompson CeedCallBackend(CeedOperatorGetFields(op, &num_input_fields, &op_input_fields, &num_output_fields, &op_output_fields)); 1011ddae5012SJeremy L Thompson { 1012ddae5012SJeremy L Thompson bool has_shared_bases = true, is_all_tensor = true, is_all_nontensor = true; 1013ddae5012SJeremy L Thompson 1014ddae5012SJeremy L Thompson for (CeedInt i = 0; i < num_input_fields; i++) { 1015ddae5012SJeremy L Thompson CeedBasis basis; 1016ddae5012SJeremy L Thompson 1017ddae5012SJeremy L Thompson CeedCallBackend(CeedOperatorFieldGetBasis(op_input_fields[i], &basis)); 1018ddae5012SJeremy L Thompson if (basis != CEED_BASIS_NONE) { 1019ddae5012SJeremy L Thompson bool is_tensor = true; 1020ddae5012SJeremy L Thompson const char *resource; 1021ddae5012SJeremy L Thompson char *resource_root; 1022ddae5012SJeremy L Thompson Ceed basis_ceed; 1023ddae5012SJeremy L Thompson 1024ddae5012SJeremy L Thompson CeedCallBackend(CeedBasisIsTensor(basis, &is_tensor)); 1025c9192acaSJeremy L Thompson is_all_tensor = is_all_tensor && is_tensor; 102645a787f7SJeremy L Thompson is_all_nontensor = is_all_nontensor && !is_tensor; 1027ddae5012SJeremy L Thompson CeedCallBackend(CeedBasisGetCeed(basis, &basis_ceed)); 1028ddae5012SJeremy L Thompson CeedCallBackend(CeedGetResource(basis_ceed, &resource)); 1029ddae5012SJeremy L Thompson CeedCallBackend(CeedGetResourceRoot(basis_ceed, resource, ":", &resource_root)); 1030c9192acaSJeremy L Thompson has_shared_bases = has_shared_bases && !strcmp(resource_root, "/gpu/cuda/shared"); 1031ddae5012SJeremy L Thompson CeedCallBackend(CeedFree(&resource_root)); 1032ddae5012SJeremy L Thompson CeedCallBackend(CeedDestroy(&basis_ceed)); 1033ddae5012SJeremy L Thompson } 1034ddae5012SJeremy L Thompson CeedCallBackend(CeedBasisDestroy(&basis)); 10354b3e95d5SJeremy L Thompson } 1036ca735530SJeremy L Thompson 1037ddae5012SJeremy L Thompson for (CeedInt i = 0; i < num_output_fields; i++) { 1038ddae5012SJeremy L Thompson CeedBasis basis; 1039ddae5012SJeremy L Thompson 1040ddae5012SJeremy L Thompson CeedCallBackend(CeedOperatorFieldGetBasis(op_output_fields[i], &basis)); 1041ddae5012SJeremy L Thompson if (basis != CEED_BASIS_NONE) { 1042ddae5012SJeremy L Thompson bool is_tensor = true; 1043ddae5012SJeremy L Thompson const char *resource; 1044ddae5012SJeremy L Thompson char *resource_root; 1045ddae5012SJeremy L Thompson Ceed basis_ceed; 1046ddae5012SJeremy L Thompson 1047ddae5012SJeremy L Thompson CeedCallBackend(CeedBasisIsTensor(basis, &is_tensor)); 1048c9192acaSJeremy L Thompson is_all_tensor = is_all_tensor && is_tensor; 1049c9192acaSJeremy L Thompson is_all_nontensor = is_all_nontensor && !is_tensor; 1050ddae5012SJeremy L Thompson 1051ddae5012SJeremy L Thompson CeedCallBackend(CeedBasisGetCeed(basis, &basis_ceed)); 1052ddae5012SJeremy L Thompson CeedCallBackend(CeedGetResource(basis_ceed, &resource)); 1053ddae5012SJeremy L Thompson CeedCallBackend(CeedGetResourceRoot(basis_ceed, resource, ":", &resource_root)); 1054c9192acaSJeremy L Thompson has_shared_bases = has_shared_bases && !strcmp(resource_root, "/gpu/cuda/shared"); 1055ddae5012SJeremy L Thompson CeedCallBackend(CeedFree(&resource_root)); 1056ddae5012SJeremy L Thompson CeedCallBackend(CeedDestroy(&basis_ceed)); 1057ddae5012SJeremy L Thompson } 1058ddae5012SJeremy L Thompson CeedCallBackend(CeedBasisDestroy(&basis)); 1059ddae5012SJeremy L Thompson } 1060ddae5012SJeremy L Thompson // -- Fallback to ref if not all bases are shared 1061ddae5012SJeremy L Thompson if (!has_shared_bases || (!is_all_tensor && !is_all_nontensor)) { 1062ddae5012SJeremy L Thompson *is_good_build = false; 1063ddae5012SJeremy L Thompson return CEED_ERROR_SUCCESS; 1064ddae5012SJeremy L Thompson } 1065ddae5012SJeremy L Thompson } 1066ca735530SJeremy L Thompson CeedCallBackend(CeedOperatorGetCeed(op, &ceed)); 1067ca735530SJeremy L Thompson CeedCallBackend(CeedOperatorGetQFunction(op, &qf)); 1068ca735530SJeremy L Thompson CeedCallBackend(CeedQFunctionGetData(qf, &qf_data)); 1069ca735530SJeremy L Thompson CeedCallBackend(CeedQFunctionGetFields(qf, NULL, &qf_input_fields, NULL, &qf_output_fields)); 1070241a4b83SYohann 10714b3e95d5SJeremy L Thompson // Get operator data 10728b97b69aSJeremy L Thompson CeedCallBackend(CeedOperatorIsAtPoints(op, &is_at_points)); 10734b3e95d5SJeremy L Thompson CeedCallBackend(CeedOperatorBuildKernelData_Cuda_gen(ceed, num_input_fields, op_input_fields, qf_input_fields, num_output_fields, op_output_fields, 10744b3e95d5SJeremy L Thompson qf_output_fields, &data->max_P_1d, &Q_1d, &dim, &is_tensor, &use_3d_slices)); 10754b3e95d5SJeremy L Thompson if (dim == 0) dim = 1; 10764b3e95d5SJeremy L Thompson data->dim = dim; 10778b97b69aSJeremy L Thompson if (is_at_points) { 10788b97b69aSJeremy L Thompson CeedElemRestriction_Cuda *rstr_data; 10798b97b69aSJeremy L Thompson CeedElemRestriction rstr_points = NULL; 10804b3e95d5SJeremy L Thompson 10818b97b69aSJeremy L Thompson CeedCallBackend(CeedOperatorAtPointsGetPoints(op, &rstr_points, NULL)); 10828b97b69aSJeremy L Thompson CeedCallBackend(CeedElemRestrictionGetMaxPointsInElement(rstr_points, &max_num_points)); 10838b97b69aSJeremy L Thompson CeedCallBackend(CeedElemRestrictionGetCompStride(rstr_points, &coords_comp_stride)); 10848b97b69aSJeremy L Thompson CeedCallBackend(CeedElemRestrictionGetData(rstr_points, &rstr_data)); 10858b97b69aSJeremy L Thompson data->points.indices = (CeedInt *)rstr_data->d_offsets; 10868b97b69aSJeremy L Thompson CeedCallBackend(CeedElemRestrictionDestroy(&rstr_points)); 10878b97b69aSJeremy L Thompson } 10888b97b69aSJeremy L Thompson if (is_at_points) use_3d_slices = false; 10898b97b69aSJeremy L Thompson if (Q_1d == 0) { 10908b97b69aSJeremy L Thompson if (is_at_points) Q_1d = max_num_points; 10918b97b69aSJeremy L Thompson else CeedCallBackend(CeedOperatorGetNumQuadraturePoints(op, &Q_1d)); 10924b3e95d5SJeremy L Thompson } 10934b3e95d5SJeremy L Thompson data->Q_1d = Q_1d; 10944b3e95d5SJeremy L Thompson 10950b454692Sjeremylt // Check for restriction only identity operator 10964b3e95d5SJeremy L Thompson { 10974b3e95d5SJeremy L Thompson bool is_identity_qf; 10984b3e95d5SJeremy L Thompson 10992b730f8bSJeremy L Thompson CeedCallBackend(CeedQFunctionIsIdentity(qf, &is_identity_qf)); 11000b454692Sjeremylt if (is_identity_qf) { 11019e201c85SYohann CeedEvalMode eval_mode_in, eval_mode_out; 1102ca735530SJeremy L Thompson 11032b730f8bSJeremy L Thompson CeedCallBackend(CeedQFunctionFieldGetEvalMode(qf_input_fields[0], &eval_mode_in)); 11042b730f8bSJeremy L Thompson CeedCallBackend(CeedQFunctionFieldGetEvalMode(qf_output_fields[0], &eval_mode_out)); 11056574a04fSJeremy L Thompson CeedCheck(eval_mode_in != CEED_EVAL_NONE || eval_mode_out != CEED_EVAL_NONE, ceed, CEED_ERROR_BACKEND, 11066574a04fSJeremy L Thompson "Backend does not implement restriction only identity operators"); 11070b454692Sjeremylt } 11084b3e95d5SJeremy L Thompson } 11090b454692Sjeremylt 1110f1a13f77SYohann Dudouit // Add atomicAdd function for old NVidia architectures 11114b3e95d5SJeremy L Thompson { 11124b3e95d5SJeremy L Thompson Ceed_Cuda *ceed_data; 11134b3e95d5SJeremy L Thompson struct cudaDeviceProp prop; 11144b3e95d5SJeremy L Thompson 11152b730f8bSJeremy L Thompson CeedCallBackend(CeedGetData(ceed, &ceed_data)); 11162b730f8bSJeremy L Thompson CeedCallBackend(cudaGetDeviceProperties(&prop, ceed_data->device_id)); 111780a9ef05SNatalie Beams if ((prop.major < 6) && (CEED_SCALAR_TYPE != CEED_SCALAR_FP32)) { 11189c25dd66SJeremy L Thompson code << "// AtomicAdd fallback source\n"; 11199c25dd66SJeremy L Thompson code << "#include <ceed/jit-source/cuda/cuda-atomic-add-fallback.h>\n\n"; 1120f1a13f77SYohann Dudouit } 11214b3e95d5SJeremy L Thompson } 1122f1a13f77SYohann Dudouit 11239e201c85SYohann // Load basis source files 1124dc007f05SJeremy L Thompson if (is_tensor) { 11259c25dd66SJeremy L Thompson code << "// Tensor basis source\n"; 11269c25dd66SJeremy L Thompson code << "#include <ceed/jit-source/cuda/cuda-shared-basis-tensor-templates.h>\n\n"; 1127dc007f05SJeremy L Thompson } else { 1128dc007f05SJeremy L Thompson code << "// Non-tensor basis source\n"; 1129dc007f05SJeremy L Thompson code << "#include <ceed/jit-source/cuda/cuda-shared-basis-nontensor-templates.h>\n\n"; 1130dc007f05SJeremy L Thompson } 1131dc007f05SJeremy L Thompson if (is_at_points) { 11328b97b69aSJeremy L Thompson code << "// AtPoints basis source\n"; 11338b97b69aSJeremy L Thompson code << "#include <ceed/jit-source/cuda/cuda-shared-basis-tensor-at-points-templates.h>\n\n"; 1134dc007f05SJeremy L Thompson } 11359c25dd66SJeremy L Thompson code << "// CodeGen operator source\n"; 11369c25dd66SJeremy L Thompson code << "#include <ceed/jit-source/cuda/cuda-gen-templates.h>\n\n"; 1137241a4b83SYohann 11384b3e95d5SJeremy L Thompson // Get QFunction name 11394b3e95d5SJeremy L Thompson std::string qfunction_name(qf_data->qfunction_name); 11404b3e95d5SJeremy L Thompson std::string operator_name; 11414b3e95d5SJeremy L Thompson 114209095acaSJeremy L Thompson operator_name = "CeedKernelCudaGenOperator_" + qfunction_name; 11434d537eeaSYohann 11449e201c85SYohann // Define CEED_Q_VLA 11459e201c85SYohann code << "\n#undef CEED_Q_VLA\n"; 1146dc007f05SJeremy L Thompson if (dim != 3 || is_at_points || use_3d_slices || !is_tensor) { 11479e201c85SYohann code << "#define CEED_Q_VLA 1\n\n"; 11489e201c85SYohann } else { 11499e201c85SYohann code << "#define CEED_Q_VLA " << Q_1d << "\n\n"; 11509e201c85SYohann } 11519e201c85SYohann 11524b3e95d5SJeremy L Thompson // Add user QFunction source 11534b3e95d5SJeremy L Thompson { 11549c25dd66SJeremy L Thompson const char *source_path; 11554b3e95d5SJeremy L Thompson 11569c25dd66SJeremy L Thompson CeedCallBackend(CeedQFunctionGetSourcePath(qf, &source_path)); 11579c25dd66SJeremy L Thompson CeedCheck(source_path, ceed, CEED_ERROR_UNSUPPORTED, "/gpu/cuda/gen backend requires QFunction source code file"); 11589c25dd66SJeremy L Thompson 11599c25dd66SJeremy L Thompson code << "// User QFunction source\n"; 11609c25dd66SJeremy L Thompson code << "#include \"" << source_path << "\"\n\n"; 11614b3e95d5SJeremy L Thompson } 1162241a4b83SYohann 1163241a4b83SYohann // Setup 1164818e0025SJeremy L Thompson code << "\n// -----------------------------------------------------------------------------\n"; 11654b3e95d5SJeremy L Thompson code << "// Operator Kernel\n"; 11664b3e95d5SJeremy L Thompson code << "// \n"; 11674b3e95d5SJeremy L Thompson code << "// d_[in,out]_i: CeedVector device array\n"; 11684b3e95d5SJeremy L Thompson code << "// r_[in,out]_e_i: Element vector register\n"; 11694b3e95d5SJeremy L Thompson code << "// r_[in,out]_q_i: Quadrature space vector register\n"; 11709123fb08SJeremy L Thompson code << "// r_[in,out]_c_i: AtPoints Chebyshev coefficients register\n"; 11714b3e95d5SJeremy L Thompson code << "// r_[in,out]_s_i: Quadrature space slice vector register\n"; 11724b3e95d5SJeremy L Thompson code << "// \n"; 11734b3e95d5SJeremy L Thompson code << "// s_B_[in,out]_i: Interpolation matrix, shared memory\n"; 11744b3e95d5SJeremy L Thompson code << "// s_G_[in,out]_i: Gradient matrix, shared memory\n"; 11754b3e95d5SJeremy L Thompson code << "// -----------------------------------------------------------------------------\n"; 11764b3e95d5SJeremy L Thompson code << "extern \"C\" __global__ void " << operator_name 11778b97b69aSJeremy L Thompson << "(CeedInt num_elem, void* ctx, FieldsInt_Cuda indices, Fields_Cuda fields, Fields_Cuda B, Fields_Cuda G, CeedScalar *W, Points_Cuda " 11788b97b69aSJeremy L Thompson "points) {\n"; 11794b3e95d5SJeremy L Thompson 11804b3e95d5SJeremy L Thompson // Scratch buffers 11819e201c85SYohann for (CeedInt i = 0; i < num_input_fields; i++) { 11824b3e95d5SJeremy L Thompson CeedEvalMode eval_mode; 11834b3e95d5SJeremy L Thompson 11842b730f8bSJeremy L Thompson CeedCallBackend(CeedQFunctionFieldGetEvalMode(qf_input_fields[i], &eval_mode)); 11859e201c85SYohann if (eval_mode != CEED_EVAL_WEIGHT) { // Skip CEED_EVAL_WEIGHT 1186826538b3SJeremy L Thompson code << " const CeedScalar *__restrict__ d_in_" << i << " = fields.inputs[" << i << "];\n"; 1187241a4b83SYohann } 1188241a4b83SYohann } 11899e201c85SYohann for (CeedInt i = 0; i < num_output_fields; i++) { 1190826538b3SJeremy L Thompson code << " CeedScalar *__restrict__ d_out_" << i << " = fields.outputs[" << i << "];\n"; 1191241a4b83SYohann } 11921da99368SJeremy L Thompson 11939e201c85SYohann code << " const CeedInt dim = " << dim << ";\n"; 1194dc007f05SJeremy L Thompson code << " const CeedInt " << (is_tensor ? "Q_1d" : "Q") << " = " << Q_1d << ";\n"; 11958b97b69aSJeremy L Thompson if (is_at_points) { 11968b97b69aSJeremy L Thompson code << " const CeedInt max_num_points = " << max_num_points << ";\n"; 11978b97b69aSJeremy L Thompson code << " const CeedInt coords_comp_stride = " << coords_comp_stride << ";\n"; 11988b97b69aSJeremy L Thompson } 11991da99368SJeremy L Thompson 12004b3e95d5SJeremy L Thompson // Shared data 1201241a4b83SYohann code << " extern __shared__ CeedScalar slice[];\n"; 12029e201c85SYohann code << " SharedData_Cuda data;\n"; 12039e201c85SYohann code << " data.t_id_x = threadIdx.x;\n"; 12049e201c85SYohann code << " data.t_id_y = threadIdx.y;\n"; 12059e201c85SYohann code << " data.t_id_z = threadIdx.z;\n"; 12069e201c85SYohann code << " data.t_id = threadIdx.x + threadIdx.y*blockDim.x + threadIdx.z*blockDim.y*blockDim.x;\n"; 120799421279SJeremy L Thompson code << " data.slice = slice + data.t_id_z*OP_T_1D" << ((!is_tensor || dim == 1) ? "" : "*OP_T_1D") << ";\n"; 1208920dcdc4Sjeremylt 12090a2a6492SJeremy L Thompson // -- Determine input mat reuse 121045a787f7SJeremy L Thompson FieldReuse_Cuda input_matrix_reuse[CEED_FIELD_MAX]; 12110a2a6492SJeremy L Thompson 12120a2a6492SJeremy L Thompson for (CeedInt i = 0; i < num_input_fields; i++) { 121345a787f7SJeremy L Thompson input_matrix_reuse[i].index = -1; 12140a2a6492SJeremy L Thompson } 12150a2a6492SJeremy L Thompson for (CeedInt i = 0; i < num_input_fields; i++) { 12160a2a6492SJeremy L Thompson CeedEvalMode eval_mode_i; 12170a2a6492SJeremy L Thompson CeedBasis basis_i; 12180a2a6492SJeremy L Thompson 12190a2a6492SJeremy L Thompson CeedCallBackend(CeedQFunctionFieldGetEvalMode(qf_input_fields[i], &eval_mode_i)); 12200a2a6492SJeremy L Thompson if (eval_mode_i == CEED_EVAL_WEIGHT) continue; 12210a2a6492SJeremy L Thompson CeedCallBackend(CeedOperatorFieldGetBasis(op_input_fields[i], &basis_i)); 122245a787f7SJeremy L Thompson for (CeedInt j = 0; (input_matrix_reuse[i].index == -1) && (j < i); j++) { 12230a2a6492SJeremy L Thompson CeedEvalMode eval_mode_j; 12240a2a6492SJeremy L Thompson CeedBasis basis_j; 12250a2a6492SJeremy L Thompson 12260a2a6492SJeremy L Thompson CeedCallBackend(CeedQFunctionFieldGetEvalMode(qf_input_fields[j], &eval_mode_j)); 12270a2a6492SJeremy L Thompson if (eval_mode_j == CEED_EVAL_WEIGHT) continue; 12280a2a6492SJeremy L Thompson CeedCallBackend(CeedOperatorFieldGetBasis(op_input_fields[j], &basis_j)); 12290a2a6492SJeremy L Thompson if (basis_i == basis_j) { 12300a2a6492SJeremy L Thompson if (is_tensor) { 123145a787f7SJeremy L Thompson input_matrix_reuse[i].index = j; 123245a787f7SJeremy L Thompson input_matrix_reuse[i].is_input = true; 123345a787f7SJeremy L Thompson input_matrix_reuse[i].eval_mode = eval_mode_j; 12340a2a6492SJeremy L Thompson } else { 12350a2a6492SJeremy L Thompson // For non-tensor can only re-use with the same eval mode 12360a2a6492SJeremy L Thompson if (eval_mode_i == eval_mode_j) { 123745a787f7SJeremy L Thompson input_matrix_reuse[i].index = j; 123845a787f7SJeremy L Thompson input_matrix_reuse[i].is_input = true; 123945a787f7SJeremy L Thompson input_matrix_reuse[i].eval_mode = eval_mode_j; 12400a2a6492SJeremy L Thompson } 12410a2a6492SJeremy L Thompson } 12420a2a6492SJeremy L Thompson } 12430a2a6492SJeremy L Thompson CeedCallBackend(CeedBasisDestroy(&basis_j)); 12440a2a6492SJeremy L Thompson } 12450a2a6492SJeremy L Thompson CeedCallBackend(CeedBasisDestroy(&basis_i)); 12460a2a6492SJeremy L Thompson } 12470a2a6492SJeremy L Thompson 12480a2a6492SJeremy L Thompson // -- Determine output mat reuse 124945a787f7SJeremy L Thompson FieldReuse_Cuda output_matrix_reuse[CEED_FIELD_MAX]; 12500a2a6492SJeremy L Thompson 12510a2a6492SJeremy L Thompson for (CeedInt i = 0; i < num_output_fields; i++) { 125245a787f7SJeremy L Thompson output_matrix_reuse[i].index = -1; 12530a2a6492SJeremy L Thompson } 12540a2a6492SJeremy L Thompson for (CeedInt i = 0; i < num_output_fields; i++) { 12550a2a6492SJeremy L Thompson CeedEvalMode eval_mode_i; 12560a2a6492SJeremy L Thompson CeedBasis basis_i; 12570a2a6492SJeremy L Thompson 12580a2a6492SJeremy L Thompson CeedCallBackend(CeedQFunctionFieldGetEvalMode(qf_output_fields[i], &eval_mode_i)); 12590a2a6492SJeremy L Thompson CeedCallBackend(CeedOperatorFieldGetBasis(op_output_fields[i], &basis_i)); 126045a787f7SJeremy L Thompson for (CeedInt j = 0; (output_matrix_reuse[i].index == -1) && (j < num_input_fields); j++) { 12610a2a6492SJeremy L Thompson CeedEvalMode eval_mode_j; 12620a2a6492SJeremy L Thompson CeedBasis basis_j; 12630a2a6492SJeremy L Thompson 12640a2a6492SJeremy L Thompson CeedCallBackend(CeedQFunctionFieldGetEvalMode(qf_input_fields[j], &eval_mode_j)); 12650a2a6492SJeremy L Thompson if (eval_mode_j == CEED_EVAL_WEIGHT) continue; 12660a2a6492SJeremy L Thompson CeedCallBackend(CeedOperatorFieldGetBasis(op_input_fields[j], &basis_j)); 12670a2a6492SJeremy L Thompson if (basis_i == basis_j) { 12680a2a6492SJeremy L Thompson if (is_tensor) { 126945a787f7SJeremy L Thompson output_matrix_reuse[i].index = j; 127045a787f7SJeremy L Thompson output_matrix_reuse[i].is_input = true; 127145a787f7SJeremy L Thompson output_matrix_reuse[i].eval_mode = eval_mode_j; 12720a2a6492SJeremy L Thompson } else { 12730a2a6492SJeremy L Thompson // For non-tensor can only re-use with the same eval mode 12740a2a6492SJeremy L Thompson if (eval_mode_i == eval_mode_j) { 127545a787f7SJeremy L Thompson output_matrix_reuse[i].index = j; 127645a787f7SJeremy L Thompson output_matrix_reuse[i].is_input = true; 127745a787f7SJeremy L Thompson output_matrix_reuse[i].eval_mode = eval_mode_j; 12780a2a6492SJeremy L Thompson } 12790a2a6492SJeremy L Thompson } 12800a2a6492SJeremy L Thompson } 12810a2a6492SJeremy L Thompson CeedCallBackend(CeedBasisDestroy(&basis_j)); 12820a2a6492SJeremy L Thompson } 128345a787f7SJeremy L Thompson for (CeedInt j = 0; (output_matrix_reuse[i].index == -1) && (j < i); j++) { 12840a2a6492SJeremy L Thompson CeedEvalMode eval_mode_j; 12850a2a6492SJeremy L Thompson CeedBasis basis_j; 12860a2a6492SJeremy L Thompson 12870a2a6492SJeremy L Thompson CeedCallBackend(CeedQFunctionFieldGetEvalMode(qf_output_fields[j], &eval_mode_j)); 12880a2a6492SJeremy L Thompson if (eval_mode_j == CEED_EVAL_WEIGHT) continue; 12890a2a6492SJeremy L Thompson CeedCallBackend(CeedOperatorFieldGetBasis(op_output_fields[j], &basis_j)); 12900a2a6492SJeremy L Thompson if (basis_i == basis_j) { 12910a2a6492SJeremy L Thompson if (is_tensor) { 129245a787f7SJeremy L Thompson output_matrix_reuse[i].index = j; 129345a787f7SJeremy L Thompson output_matrix_reuse[i].is_input = false; 129445a787f7SJeremy L Thompson output_matrix_reuse[i].eval_mode = eval_mode_j; 12950a2a6492SJeremy L Thompson } else { 12960a2a6492SJeremy L Thompson // For non-tensor can only re-use with the same eval mode 12970a2a6492SJeremy L Thompson if (eval_mode_i == eval_mode_j) { 129845a787f7SJeremy L Thompson output_matrix_reuse[i].index = j; 129945a787f7SJeremy L Thompson output_matrix_reuse[i].is_input = false; 130045a787f7SJeremy L Thompson output_matrix_reuse[i].eval_mode = eval_mode_j; 13010a2a6492SJeremy L Thompson } 13020a2a6492SJeremy L Thompson } 13030a2a6492SJeremy L Thompson } 13040a2a6492SJeremy L Thompson CeedCallBackend(CeedBasisDestroy(&basis_j)); 13050a2a6492SJeremy L Thompson } 13060a2a6492SJeremy L Thompson CeedCallBackend(CeedBasisDestroy(&basis_i)); 13070a2a6492SJeremy L Thompson } 13080a2a6492SJeremy L Thompson 1309ac421f39SYohann // Initialize constants, and matrices B and G 13104b3e95d5SJeremy L Thompson code << "\n // Input field constants and basis data\n"; 13119e201c85SYohann for (CeedInt i = 0; i < num_input_fields; i++) { 13120a2a6492SJeremy L Thompson CeedCallBackend(CeedOperatorBuildKernelFieldData_Cuda_gen(code, data, i, op_input_fields[i], qf_input_fields[i], input_matrix_reuse[i], Q_1d, 13130a2a6492SJeremy L Thompson true, is_tensor, is_at_points, use_3d_slices)); 1314920dcdc4Sjeremylt } 13154b3e95d5SJeremy L Thompson code << "\n // Output field constants and basis data\n"; 13169e201c85SYohann for (CeedInt i = 0; i < num_output_fields; i++) { 13170a2a6492SJeremy L Thompson CeedCallBackend(CeedOperatorBuildKernelFieldData_Cuda_gen(code, data, i, op_output_fields[i], qf_output_fields[i], output_matrix_reuse[i], Q_1d, 13180a2a6492SJeremy L Thompson false, is_tensor, is_at_points, use_3d_slices)); 13194b3e95d5SJeremy L Thompson } 1320920dcdc4Sjeremylt 13214b3e95d5SJeremy L Thompson // Loop over all elements 13224b3e95d5SJeremy L Thompson code << "\n // Element loop\n"; 1323ac421f39SYohann code << " __syncthreads();\n"; 13249e201c85SYohann code << " for (CeedInt elem = blockIdx.x*blockDim.z + threadIdx.z; elem < num_elem; elem += gridDim.x*blockDim.z) {\n"; 13254b3e95d5SJeremy L Thompson 1326e93651e5SJeremy L Thompson // -- Compute minimum buffer space needed 13278b97b69aSJeremy L Thompson CeedInt max_rstr_buffer_size = 1; 1328e93651e5SJeremy L Thompson 13299e201c85SYohann for (CeedInt i = 0; i < num_input_fields; i++) { 1330e93651e5SJeremy L Thompson CeedInt num_comp, elem_size; 1331e93651e5SJeremy L Thompson CeedElemRestriction elem_rstr; 1332e93651e5SJeremy L Thompson 1333e93651e5SJeremy L Thompson CeedCallBackend(CeedOperatorFieldGetElemRestriction(op_input_fields[i], &elem_rstr)); 1334e93651e5SJeremy L Thompson CeedCallBackend(CeedElemRestrictionGetNumComponents(elem_rstr, &num_comp)); 1335e93651e5SJeremy L Thompson CeedCallBackend(CeedElemRestrictionGetElementSize(elem_rstr, &elem_size)); 1336dc007f05SJeremy L Thompson max_rstr_buffer_size = CeedIntMax(max_rstr_buffer_size, num_comp * (is_tensor && (dim >= 3) ? elem_size : 1)); 1337681d0ea7SJeremy L Thompson CeedCallBackend(CeedElemRestrictionDestroy(&elem_rstr)); 1338e93651e5SJeremy L Thompson } 1339e93651e5SJeremy L Thompson for (CeedInt i = 0; i < num_output_fields; i++) { 1340e93651e5SJeremy L Thompson CeedInt num_comp, elem_size; 1341e93651e5SJeremy L Thompson CeedElemRestriction elem_rstr; 1342e93651e5SJeremy L Thompson 1343e93651e5SJeremy L Thompson CeedCallBackend(CeedOperatorFieldGetElemRestriction(op_output_fields[i], &elem_rstr)); 1344e93651e5SJeremy L Thompson CeedCallBackend(CeedElemRestrictionGetNumComponents(elem_rstr, &num_comp)); 1345e93651e5SJeremy L Thompson CeedCallBackend(CeedElemRestrictionGetElementSize(elem_rstr, &elem_size)); 1346dc007f05SJeremy L Thompson max_rstr_buffer_size = CeedIntMax(max_rstr_buffer_size, num_comp * (is_tensor && (dim >= 3) ? elem_size : 1)); 1347681d0ea7SJeremy L Thompson CeedCallBackend(CeedElemRestrictionDestroy(&elem_rstr)); 1348e93651e5SJeremy L Thompson } 1349e93651e5SJeremy L Thompson code << " // Scratch restriction buffer space\n"; 1350e93651e5SJeremy L Thompson code << " CeedScalar r_e_scratch[" << max_rstr_buffer_size << "];\n"; 1351e93651e5SJeremy L Thompson 1352e93651e5SJeremy L Thompson // -- Determine best input field processing order 1353e93651e5SJeremy L Thompson CeedInt field_rstr_in_buffer[CEED_FIELD_MAX], input_field_order[CEED_FIELD_MAX]; 1354e93651e5SJeremy L Thompson 1355e93651e5SJeremy L Thompson for (CeedInt i = 0; i < num_input_fields; i++) { 1356e93651e5SJeremy L Thompson field_rstr_in_buffer[i] = -1; 1357e93651e5SJeremy L Thompson input_field_order[i] = -1; 1358e93651e5SJeremy L Thompson } 1359e93651e5SJeremy L Thompson { 1360e93651e5SJeremy L Thompson bool is_ordered[CEED_FIELD_MAX]; 1361e93651e5SJeremy L Thompson CeedInt curr_index = 0; 1362e93651e5SJeremy L Thompson 1363e93651e5SJeremy L Thompson for (CeedInt i = 0; i < num_input_fields; i++) is_ordered[i] = false; 1364e93651e5SJeremy L Thompson for (CeedInt i = 0; i < num_input_fields; i++) { 1365e93651e5SJeremy L Thompson CeedVector vec_i; 1366e93651e5SJeremy L Thompson CeedElemRestriction rstr_i; 1367e93651e5SJeremy L Thompson 1368e93651e5SJeremy L Thompson if (is_ordered[i]) continue; 1369e93651e5SJeremy L Thompson field_rstr_in_buffer[i] = i; 1370e93651e5SJeremy L Thompson is_ordered[i] = true; 1371e93651e5SJeremy L Thompson input_field_order[curr_index] = i; 1372e93651e5SJeremy L Thompson curr_index++; 1373034f99fdSJeremy L Thompson CeedCallBackend(CeedOperatorFieldGetVector(op_input_fields[i], &vec_i)); 1374e93651e5SJeremy L Thompson if (vec_i == CEED_VECTOR_NONE) continue; // CEED_EVAL_WEIGHT 1375e93651e5SJeremy L Thompson CeedCallBackend(CeedOperatorFieldGetElemRestriction(op_input_fields[i], &rstr_i)); 1376e93651e5SJeremy L Thompson for (CeedInt j = i + 1; j < num_input_fields; j++) { 1377e93651e5SJeremy L Thompson CeedVector vec_j; 1378e93651e5SJeremy L Thompson CeedElemRestriction rstr_j; 1379e93651e5SJeremy L Thompson 1380e93651e5SJeremy L Thompson CeedCallBackend(CeedOperatorFieldGetVector(op_input_fields[j], &vec_j)); 1381e93651e5SJeremy L Thompson CeedCallBackend(CeedOperatorFieldGetElemRestriction(op_input_fields[j], &rstr_j)); 1382e93651e5SJeremy L Thompson if (rstr_i == rstr_j && vec_i == vec_j) { 1383e93651e5SJeremy L Thompson field_rstr_in_buffer[j] = i; 1384e93651e5SJeremy L Thompson is_ordered[j] = true; 1385e93651e5SJeremy L Thompson input_field_order[curr_index] = j; 1386e93651e5SJeremy L Thompson curr_index++; 1387e93651e5SJeremy L Thompson } 1388681d0ea7SJeremy L Thompson CeedCallBackend(CeedVectorDestroy(&vec_j)); 1389681d0ea7SJeremy L Thompson CeedCallBackend(CeedElemRestrictionDestroy(&rstr_j)); 1390e93651e5SJeremy L Thompson } 1391681d0ea7SJeremy L Thompson CeedCallBackend(CeedVectorDestroy(&vec_i)); 1392681d0ea7SJeremy L Thompson CeedCallBackend(CeedElemRestrictionDestroy(&rstr_i)); 1393e93651e5SJeremy L Thompson } 1394e93651e5SJeremy L Thompson } 1395e93651e5SJeremy L Thompson 1396e93651e5SJeremy L Thompson // -- Input restriction and basis 1397e93651e5SJeremy L Thompson code << "\n // -- Input field restrictions and basis actions\n"; 1398e93651e5SJeremy L Thompson for (CeedInt i = 0; i < num_input_fields; i++) { 139959fa3f92SJeremy L Thompson const char *field_name; 140059fa3f92SJeremy L Thompson const CeedInt f = input_field_order[i]; 1401e93651e5SJeremy L Thompson 140259fa3f92SJeremy L Thompson CeedCallBackend(CeedOperatorFieldGetName(op_input_fields[f], &field_name)); 140359fa3f92SJeremy L Thompson code << " // ---- Input field " << f << ": " << field_name << "\n"; 1404920dcdc4Sjeremylt 14054b3e95d5SJeremy L Thompson // ---- Restriction 1406e93651e5SJeremy L Thompson CeedCallBackend(CeedOperatorBuildKernelRestriction_Cuda_gen(code, data, f, dim, field_rstr_in_buffer, op_input_fields[f], qf_input_fields[f], 1407dc007f05SJeremy L Thompson Q_1d, true, is_tensor, is_at_points, use_3d_slices)); 14089a2291e3SJeremy L Thompson 14094b3e95d5SJeremy L Thompson // ---- Basis action 1410dc007f05SJeremy L Thompson CeedCallBackend(CeedOperatorBuildKernelBasis_Cuda_gen(code, data, f, dim, op_input_fields[f], qf_input_fields[f], Q_1d, true, is_tensor, 1411dc007f05SJeremy L Thompson is_at_points, use_3d_slices)); 1412920dcdc4Sjeremylt } 1413920dcdc4Sjeremylt 14144b3e95d5SJeremy L Thompson // -- Q function 14158b97b69aSJeremy L Thompson CeedCallBackend(CeedOperatorBuildKernelQFunction_Cuda_gen(code, data, dim, max_num_points, num_input_fields, op_input_fields, qf_input_fields, 1416dc007f05SJeremy L Thompson num_output_fields, op_output_fields, qf_output_fields, qfunction_name, Q_1d, is_tensor, 1417dc007f05SJeremy L Thompson is_at_points, use_3d_slices)); 1418ca735530SJeremy L Thompson 14194b3e95d5SJeremy L Thompson // -- Output basis and restriction 14204b3e95d5SJeremy L Thompson code << "\n // -- Output field basis action and restrictions\n"; 14219e201c85SYohann for (CeedInt i = 0; i < num_output_fields; i++) { 142259fa3f92SJeremy L Thompson const char *field_name; 142359fa3f92SJeremy L Thompson 142459fa3f92SJeremy L Thompson CeedCallBackend(CeedOperatorFieldGetName(op_output_fields[i], &field_name)); 142559fa3f92SJeremy L Thompson code << " // ---- Output field " << i << ": " << field_name << "\n"; 1426ca735530SJeremy L Thompson 14274b3e95d5SJeremy L Thompson // ---- Basis action 1428dc007f05SJeremy L Thompson CeedCallBackend(CeedOperatorBuildKernelBasis_Cuda_gen(code, data, i, dim, op_output_fields[i], qf_output_fields[i], Q_1d, false, is_tensor, 1429dc007f05SJeremy L Thompson is_at_points, use_3d_slices)); 14309a2291e3SJeremy L Thompson 14314b3e95d5SJeremy L Thompson // ---- Restriction 14328b97b69aSJeremy L Thompson CeedCallBackend(CeedOperatorBuildKernelRestriction_Cuda_gen(code, data, i, dim, NULL, op_output_fields[i], qf_output_fields[i], Q_1d, false, 1433dc007f05SJeremy L Thompson is_tensor, is_at_points, use_3d_slices)); 1434ac421f39SYohann } 1435241a4b83SYohann 14364b3e95d5SJeremy L Thompson // Close loop and function 1437241a4b83SYohann code << " }\n"; 1438818e0025SJeremy L Thompson code << "}\n"; 1439818e0025SJeremy L Thompson code << "// -----------------------------------------------------------------------------\n\n"; 1440241a4b83SYohann 14413a2968d6SJeremy L Thompson // Compile 1442ddae5012SJeremy L Thompson { 1443ddae5012SJeremy L Thompson bool is_compile_good = false; 1444ddae5012SJeremy L Thompson 144599421279SJeremy L Thompson CeedCallBackend(CeedTryCompile_Cuda(ceed, code.str().c_str(), &is_compile_good, &data->module, 1, "OP_T_1D", CeedIntMax(Q_1d, data->max_P_1d))); 1446ddae5012SJeremy L Thompson if (is_compile_good) { 1447ddae5012SJeremy L Thompson *is_good_build = true; 1448eb7e6cafSJeremy L Thompson CeedCallBackend(CeedGetKernel_Cuda(ceed, data->module, operator_name.c_str(), &data->op)); 1449ddae5012SJeremy L Thompson } else { 1450ddae5012SJeremy L Thompson *is_good_build = false; 1451ddae5012SJeremy L Thompson data->use_fallback = true; 1452ddae5012SJeremy L Thompson } 1453ddae5012SJeremy L Thompson } 14542b730f8bSJeremy L Thompson CeedCallBackend(CeedOperatorSetSetupDone(op)); 14559bc66399SJeremy L Thompson CeedCallBackend(CeedDestroy(&ceed)); 1456c11e12f4SJeremy L Thompson CeedCallBackend(CeedQFunctionDestroy(&qf)); 1457e15f9bd0SJeremy L Thompson return CEED_ERROR_SUCCESS; 1458241a4b83SYohann } 14592a86cc9dSSebastian Grimberg 1460ab213215SJeremy L Thompson //------------------------------------------------------------------------------ 1461