13d8e8822SJeremy L Thompson // Copyright (c) 2017-2022, 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 10ec3da8bcSJed Brown #include <ceed/backend.h> 112b730f8bSJeremy L Thompson #include <ceed/ceed.h> 129e201c85SYohann #include <ceed/jit-tools.h> 133d576824SJeremy L Thompson #include <cuda_runtime.h> 142b730f8bSJeremy L Thompson 15241a4b83SYohann #include <iostream> 16241a4b83SYohann #include <sstream> 172b730f8bSJeremy L Thompson 180d0321e0SJeremy L Thompson #include "../cuda-ref/ceed-cuda-ref.h" 19241a4b83SYohann #include "../cuda-shared/ceed-cuda-shared.h" 202b730f8bSJeremy L Thompson #include "../cuda/ceed-cuda-compile.h" 212b730f8bSJeremy L Thompson #include "ceed-cuda-gen.h" 22241a4b83SYohann 23ab213215SJeremy L Thompson //------------------------------------------------------------------------------ 24ab213215SJeremy L Thompson // Build singe operator kernel 25ab213215SJeremy L Thompson //------------------------------------------------------------------------------ 26241a4b83SYohann extern "C" int CeedCudaGenOperatorBuild(CeedOperator op) { 27241a4b83SYohann using std::ostringstream; 28241a4b83SYohann using std::string; 299e201c85SYohann bool is_setup_done; 302b730f8bSJeremy L Thompson CeedCallBackend(CeedOperatorIsSetupDone(op, &is_setup_done)); 319e201c85SYohann if (is_setup_done) return CEED_ERROR_SUCCESS; 32241a4b83SYohann Ceed ceed; 332b730f8bSJeremy L Thompson CeedCallBackend(CeedOperatorGetCeed(op, &ceed)); 34241a4b83SYohann CeedOperator_Cuda_gen *data; 352b730f8bSJeremy L Thompson CeedCallBackend(CeedOperatorGetData(op, &data)); 36241a4b83SYohann CeedQFunction qf; 37241a4b83SYohann CeedQFunction_Cuda_gen *qf_data; 382b730f8bSJeremy L Thompson CeedCallBackend(CeedOperatorGetQFunction(op, &qf)); 392b730f8bSJeremy L Thompson CeedCallBackend(CeedQFunctionGetData(qf, &qf_data)); 40e79b91d9SJeremy L Thompson CeedSize lsize; 412b730f8bSJeremy L Thompson CeedInt Q, P_1d = 0, Q_1d = 0, elem_size, num_input_fields, num_output_fields, num_comp, dim = 1; 422b730f8bSJeremy L Thompson CeedCallBackend(CeedOperatorGetNumQuadraturePoints(op, &Q)); 439e201c85SYohann Q_1d = Q; 449e201c85SYohann CeedOperatorField *op_input_fields, *op_output_fields; 452b730f8bSJeremy L Thompson CeedCallBackend(CeedOperatorGetFields(op, &num_input_fields, &op_input_fields, &num_output_fields, &op_output_fields)); 469e201c85SYohann CeedQFunctionField *qf_input_fields, *qf_output_fields; 472b730f8bSJeremy L Thompson CeedCallBackend(CeedQFunctionGetFields(qf, NULL, &qf_input_fields, NULL, &qf_output_fields)); 489e201c85SYohann CeedEvalMode eval_mode; 49241a4b83SYohann CeedBasis basis; 50241a4b83SYohann CeedBasis_Cuda_shared *basis_data; 51241a4b83SYohann CeedElemRestriction Erestrict; 52461525f5SNatalie Beams CeedElemRestriction_Cuda *restr_data; 53241a4b83SYohann 549e201c85SYohann // TODO: put in a function? 550b454692Sjeremylt // Check for restriction only identity operator 560b454692Sjeremylt bool is_identity_qf; 572b730f8bSJeremy L Thompson CeedCallBackend(CeedQFunctionIsIdentity(qf, &is_identity_qf)); 580b454692Sjeremylt if (is_identity_qf) { 599e201c85SYohann CeedEvalMode eval_mode_in, eval_mode_out; 602b730f8bSJeremy L Thompson CeedCallBackend(CeedQFunctionFieldGetEvalMode(qf_input_fields[0], &eval_mode_in)); 612b730f8bSJeremy L Thompson CeedCallBackend(CeedQFunctionFieldGetEvalMode(qf_output_fields[0], &eval_mode_out)); 629e201c85SYohann if (eval_mode_in == CEED_EVAL_NONE && eval_mode_out == CEED_EVAL_NONE) 630b454692Sjeremylt // LCOV_EXCL_START 642b730f8bSJeremy L Thompson return CeedError(ceed, CEED_ERROR_BACKEND, "Backend does not implement restriction only identity operators"); 650b454692Sjeremylt // LCOV_EXCL_STOP 660b454692Sjeremylt } 670b454692Sjeremylt 68241a4b83SYohann ostringstream code; 69241a4b83SYohann 709e201c85SYohann // TODO: put in a function? 71f1a13f77SYohann Dudouit // Add atomicAdd function for old NVidia architectures 72f1a13f77SYohann Dudouit struct cudaDeviceProp prop; 73f1a13f77SYohann Dudouit Ceed_Cuda *ceed_data; 742b730f8bSJeremy L Thompson CeedCallBackend(CeedGetData(ceed, &ceed_data)); 752b730f8bSJeremy L Thompson CeedCallBackend(cudaGetDeviceProperties(&prop, ceed_data->device_id)); 7680a9ef05SNatalie Beams if ((prop.major < 6) && (CEED_SCALAR_TYPE != CEED_SCALAR_FP32)) { 779e201c85SYohann char *atomic_add_path, *atomic_add_source; 782b730f8bSJeremy L Thompson CeedCallBackend(CeedGetJitAbsolutePath(ceed, "ceed/jit-source/cuda/cuda-atomic-add-fallback.h", &atomic_add_path)); 799e201c85SYohann CeedDebug256(ceed, 2, "----- Loading Atomic Add Source -----\n"); 802b730f8bSJeremy L Thompson CeedCallBackend(CeedLoadSourceToBuffer(ceed, atomic_add_path, &atomic_add_source)); 819e201c85SYohann code << atomic_add_source; 822b730f8bSJeremy L Thompson CeedCallBackend(CeedFree(&atomic_add_path)); 832b730f8bSJeremy L Thompson CeedCallBackend(CeedFree(&atomic_add_source)); 84f1a13f77SYohann Dudouit } 85f1a13f77SYohann Dudouit 869e201c85SYohann // Load basis source files 879e201c85SYohann // TODO: generalize to accept different device functions? 889e201c85SYohann { 899e201c85SYohann char *tensor_basis_kernel_path, *tensor_basis_kernel_source; 902b730f8bSJeremy L Thompson CeedCallBackend(CeedGetJitAbsolutePath(ceed, "ceed/jit-source/cuda/cuda-shared-basis-tensor-templates.h", &tensor_basis_kernel_path)); 919e201c85SYohann CeedDebug256(ceed, 2, "----- Loading Tensor Basis Kernel Source -----\n"); 922b730f8bSJeremy L Thompson CeedCallBackend(CeedLoadSourceToBuffer(ceed, tensor_basis_kernel_path, &tensor_basis_kernel_source)); 939e201c85SYohann code << tensor_basis_kernel_source; 942b730f8bSJeremy L Thompson CeedCallBackend(CeedFree(&tensor_basis_kernel_path)); 952b730f8bSJeremy L Thompson CeedCallBackend(CeedFree(&tensor_basis_kernel_source)); 969e201c85SYohann } 979e201c85SYohann { 989e201c85SYohann char *cuda_gen_template_path, *cuda_gen_template_source; 992b730f8bSJeremy L Thompson CeedCallBackend(CeedGetJitAbsolutePath(ceed, "ceed/jit-source/cuda/cuda-gen-templates.h", &cuda_gen_template_path)); 1009e201c85SYohann CeedDebug256(ceed, 2, "----- Loading Cuda-Gen Template Source -----\n"); 1012b730f8bSJeremy L Thompson CeedCallBackend(CeedLoadSourceToBuffer(ceed, cuda_gen_template_path, &cuda_gen_template_source)); 1029e201c85SYohann code << cuda_gen_template_source; 1032b730f8bSJeremy L Thompson CeedCallBackend(CeedFree(&cuda_gen_template_path)); 1042b730f8bSJeremy L Thompson CeedCallBackend(CeedFree(&cuda_gen_template_source)); 1059e201c85SYohann } 106241a4b83SYohann 1079e201c85SYohann // Get QFunction source and name 1089e201c85SYohann string q_function_source(qf_data->q_function_source); 1099e201c85SYohann string q_function_name(qf_data->q_function_name); 1109e201c85SYohann string operator_name; 111204bfdd7SJeremy L Thompson operator_name = "CeedKernelCudaGenOperator_" + q_function_name; 1124d537eeaSYohann 1139e201c85SYohann // Find dim, P_1d, Q_1d 1149e201c85SYohann data->max_P_1d = 0; 1159e201c85SYohann for (CeedInt i = 0; i < num_input_fields; i++) { 1162b730f8bSJeremy L Thompson CeedCallBackend(CeedOperatorFieldGetBasis(op_input_fields[i], &basis)); 1171da99368SJeremy L Thompson if (basis != CEED_BASIS_COLLOCATED) { 1182b730f8bSJeremy L Thompson CeedCallBackend(CeedBasisGetData(basis, &basis_data)); 1192b730f8bSJeremy L Thompson CeedCallBackend(CeedQFunctionFieldGetEvalMode(qf_input_fields[i], &eval_mode)); 1201da99368SJeremy L Thompson 1219e201c85SYohann // Collect dim, P_1d, and Q_1d 1222b730f8bSJeremy L Thompson CeedCallBackend(CeedBasisGetDimension(basis, &dim)); 1231da99368SJeremy L Thompson bool isTensor; 1242b730f8bSJeremy L Thompson CeedCallBackend(CeedBasisIsTensor(basis, &isTensor)); 1251da99368SJeremy L Thompson if (isTensor) { 1262b730f8bSJeremy L Thompson CeedCallBackend(CeedBasisGetNumQuadraturePoints1D(basis, &Q_1d)); 1272b730f8bSJeremy L Thompson CeedCallBackend(CeedBasisGetNumNodes1D(basis, &P_1d)); 1289e201c85SYohann if (P_1d > data->max_P_1d) data->max_P_1d = P_1d; 1291da99368SJeremy L Thompson } else { 130e9f4dca0SJeremy L Thompson // LCOV_EXCL_START 131e15f9bd0SJeremy L Thompson return CeedError(ceed, CEED_ERROR_BACKEND, "Backend does not implement operators with non-tensor basis"); 132e9f4dca0SJeremy L Thompson // LCOV_EXCL_STOP 1331da99368SJeremy L Thompson } 1341da99368SJeremy L Thompson } 1351da99368SJeremy L Thompson } 1369e201c85SYohann // Check output bases for Q_1d, dim as well 137dc64899eSYohann // The only input basis might be CEED_BASIS_COLLOCATED 1389e201c85SYohann for (CeedInt i = 0; i < num_output_fields; i++) { 1392b730f8bSJeremy L Thompson CeedCallBackend(CeedOperatorFieldGetBasis(op_output_fields[i], &basis)); 1400f54b25eSjeremylt 1411da99368SJeremy L Thompson if (basis != CEED_BASIS_COLLOCATED) { 1422b730f8bSJeremy L Thompson CeedCallBackend(CeedBasisGetData(basis, &basis_data)); 1432b730f8bSJeremy L Thompson CeedCallBackend(CeedQFunctionFieldGetEvalMode(qf_output_fields[i], &eval_mode)); 1440f54b25eSjeremylt 1459e201c85SYohann // Collect Q_1d 1462b730f8bSJeremy L Thompson CeedCallBackend(CeedBasisGetDimension(basis, &dim)); 1471da99368SJeremy L Thompson bool isTensor; 1482b730f8bSJeremy L Thompson CeedCallBackend(CeedBasisIsTensor(basis, &isTensor)); 1491da99368SJeremy L Thompson if (isTensor) { 1502b730f8bSJeremy L Thompson CeedCallBackend(CeedBasisGetNumQuadraturePoints1D(basis, &Q_1d)); 1511da99368SJeremy L Thompson } else { 152e9f4dca0SJeremy L Thompson // LCOV_EXCL_START 153e15f9bd0SJeremy L Thompson return CeedError(ceed, CEED_ERROR_BACKEND, "Backend does not implement operators with non-tensor basis"); 154e9f4dca0SJeremy L Thompson // LCOV_EXCL_STOP 1551da99368SJeremy L Thompson } 1561da99368SJeremy L Thompson } 1571da99368SJeremy L Thompson } 1581da99368SJeremy L Thompson data->dim = dim; 1599e201c85SYohann data->Q_1d = Q_1d; 1601da99368SJeremy L Thompson 1619e201c85SYohann // Only use 3D collocated gradient parallelization strategy when gradient is computed 1629e201c85SYohann // TODO: put in a function? 1639e201c85SYohann bool use_collograd_parallelization = false; 1649e201c85SYohann if (dim == 3) { 1659e201c85SYohann bool was_grad_found = false; 1669e201c85SYohann for (CeedInt i = 0; i < num_input_fields; i++) { 1672b730f8bSJeremy L Thompson CeedCallBackend(CeedQFunctionFieldGetEvalMode(qf_input_fields[i], &eval_mode)); 1689e201c85SYohann if (eval_mode == CEED_EVAL_GRAD) { 1692b730f8bSJeremy L Thompson CeedCallBackend(CeedOperatorFieldGetBasis(op_input_fields[i], &basis)); 1702b730f8bSJeremy L Thompson CeedCallBackend(CeedBasisGetData(basis, &basis_data)); 1719e201c85SYohann use_collograd_parallelization = !!basis_data->d_collo_grad_1d && (was_grad_found ? use_collograd_parallelization : true); 1729e201c85SYohann was_grad_found = true; 1739e201c85SYohann } 1749e201c85SYohann } 1759e201c85SYohann for (CeedInt i = 0; i < num_output_fields; i++) { 1762b730f8bSJeremy L Thompson CeedCallBackend(CeedQFunctionFieldGetEvalMode(qf_output_fields[i], &eval_mode)); 1779e201c85SYohann if (eval_mode == CEED_EVAL_GRAD) { 1782b730f8bSJeremy L Thompson CeedCallBackend(CeedOperatorFieldGetBasis(op_output_fields[i], &basis)); 1792b730f8bSJeremy L Thompson CeedCallBackend(CeedBasisGetData(basis, &basis_data)); 1809e201c85SYohann use_collograd_parallelization = !!basis_data->d_collo_grad_1d && (was_grad_found ? use_collograd_parallelization : true); 1819e201c85SYohann was_grad_found = true; 1829e201c85SYohann } 1839e201c85SYohann } 1841da99368SJeremy L Thompson } 1851da99368SJeremy L Thompson 1869e201c85SYohann // Define CEED_Q_VLA 1879e201c85SYohann code << "\n#undef CEED_Q_VLA\n"; 1889e201c85SYohann if (dim != 3 || use_collograd_parallelization) { 1899e201c85SYohann code << "#define CEED_Q_VLA 1\n\n"; 1909e201c85SYohann } else { 1919e201c85SYohann code << "#define CEED_Q_VLA " << Q_1d << "\n\n"; 1929e201c85SYohann } 1939e201c85SYohann 1949e201c85SYohann code << q_function_source; 195241a4b83SYohann 196241a4b83SYohann // Setup 197818e0025SJeremy L Thompson code << "\n// -----------------------------------------------------------------------------\n"; 1982b730f8bSJeremy L Thompson code << "\nextern \"C\" __global__ void " << operator_name 1992b730f8bSJeremy L Thompson << "(CeedInt num_elem, void* ctx, FieldsInt_Cuda indices, Fields_Cuda fields, Fields_Cuda B, Fields_Cuda G, CeedScalar* W) {\n"; 2009e201c85SYohann for (CeedInt i = 0; i < num_input_fields; i++) { 2012b730f8bSJeremy L Thompson CeedCallBackend(CeedQFunctionFieldGetEvalMode(qf_input_fields[i], &eval_mode)); 2029e201c85SYohann if (eval_mode != CEED_EVAL_WEIGHT) { // Skip CEED_EVAL_WEIGHT 2039e201c85SYohann code << " const CeedScalar* d_u_" << i << " = fields.inputs[" << i << "];\n"; 204241a4b83SYohann } 205241a4b83SYohann } 206241a4b83SYohann 2079e201c85SYohann for (CeedInt i = 0; i < num_output_fields; i++) { 2089e201c85SYohann code << " CeedScalar* d_v_" << i << " = fields.outputs[" << i << "];\n"; 209241a4b83SYohann } 2101da99368SJeremy L Thompson 2119e201c85SYohann code << " const CeedInt dim = " << dim << ";\n"; 2129e201c85SYohann code << " const CeedInt Q_1d = " << Q_1d << ";\n"; 2131da99368SJeremy L Thompson 214241a4b83SYohann code << " extern __shared__ CeedScalar slice[];\n"; 2159e201c85SYohann // TODO put in a function? InitSharedData_Cuda? 2169e201c85SYohann code << " SharedData_Cuda data;\n"; 2179e201c85SYohann code << " data.t_id_x = threadIdx.x;\n"; 2189e201c85SYohann code << " data.t_id_y = threadIdx.y;\n"; 2199e201c85SYohann code << " data.t_id_z = threadIdx.z;\n"; 2209e201c85SYohann code << " data.t_id = threadIdx.x + threadIdx.y*blockDim.x + threadIdx.z*blockDim.y*blockDim.x;\n"; 2219e201c85SYohann code << " data.slice = slice+data.t_id_z*T_1D" << (dim > 1 ? "*T_1D" : "") << ";\n"; 222920dcdc4Sjeremylt 223818e0025SJeremy L Thompson code << "\n // -- Input field constants and basis data --\n"; 2249e201c85SYohann // TODO: Put in a function? 225ac421f39SYohann // Initialize constants, and matrices B and G 2269e201c85SYohann for (CeedInt i = 0; i < num_input_fields; i++) { 227818e0025SJeremy L Thompson code << " // ---- Input field " << i << " ----\n"; 2289e201c85SYohann // Get elem_size, eval_mode, num_comp 2292b730f8bSJeremy L Thompson CeedCallBackend(CeedOperatorFieldGetElemRestriction(op_input_fields[i], &Erestrict)); 2302b730f8bSJeremy L Thompson CeedCallBackend(CeedElemRestrictionGetElementSize(Erestrict, &elem_size)); 2312b730f8bSJeremy L Thompson CeedCallBackend(CeedQFunctionFieldGetEvalMode(qf_input_fields[i], &eval_mode)); 2322b730f8bSJeremy L Thompson CeedCallBackend(CeedElemRestrictionGetNumComponents(Erestrict, &num_comp)); 233920dcdc4Sjeremylt 234920dcdc4Sjeremylt // Set field constants 2359e201c85SYohann if (eval_mode != CEED_EVAL_WEIGHT) { 2362b730f8bSJeremy L Thompson CeedCallBackend(CeedOperatorFieldGetBasis(op_input_fields[i], &basis)); 237920dcdc4Sjeremylt if (basis != CEED_BASIS_COLLOCATED) { 2382b730f8bSJeremy L Thompson CeedCallBackend(CeedBasisGetNumNodes1D(basis, &P_1d)); 2399e201c85SYohann code << " const CeedInt P_in_" << i << " = " << P_1d << ";\n"; 240920dcdc4Sjeremylt } else { 2419e201c85SYohann code << " const CeedInt P_in_" << i << " = " << Q_1d << ";\n"; 242920dcdc4Sjeremylt } 2439e201c85SYohann code << " const CeedInt num_comp_in_" << i << " = " << num_comp << ";\n"; 244920dcdc4Sjeremylt } 245920dcdc4Sjeremylt 246920dcdc4Sjeremylt // Load basis data 2479e201c85SYohann code << " // EvalMode: " << CeedEvalModes[eval_mode] << "\n"; 2489e201c85SYohann switch (eval_mode) { 249241a4b83SYohann case CEED_EVAL_NONE: 250241a4b83SYohann break; 251241a4b83SYohann case CEED_EVAL_INTERP: 2522b730f8bSJeremy L Thompson CeedCallBackend(CeedBasisGetData(basis, &basis_data)); 2539e201c85SYohann data->B.inputs[i] = basis_data->d_interp_1d; 2549e201c85SYohann code << " __shared__ CeedScalar s_B_in_" << i << "[" << P_1d * Q_1d << "];\n"; 2559e201c85SYohann code << " loadMatrix<P_in_" << i << ",Q_1d>(data, B.inputs[" << i << "], s_B_in_" << i << ");\n"; 256241a4b83SYohann break; 257241a4b83SYohann case CEED_EVAL_GRAD: 2582b730f8bSJeremy L Thompson CeedCallBackend(CeedBasisGetData(basis, &basis_data)); 2599e201c85SYohann data->B.inputs[i] = basis_data->d_interp_1d; 2609e201c85SYohann code << " __shared__ CeedScalar s_B_in_" << i << "[" << P_1d * Q_1d << "];\n"; 2619e201c85SYohann code << " loadMatrix<P_in_" << i << ",Q_1d>(data, B.inputs[" << i << "], s_B_in_" << i << ");\n"; 2629e201c85SYohann if (use_collograd_parallelization) { 2639e201c85SYohann data->G.inputs[i] = basis_data->d_collo_grad_1d; 2649e201c85SYohann code << " __shared__ CeedScalar s_G_in_" << i << "[" << Q_1d * Q_1d << "];\n"; 2659e201c85SYohann code << " loadMatrix<Q_1d,Q_1d>(data, G.inputs[" << i << "], s_G_in_" << i << ");\n"; 266ac421f39SYohann } else { 2679e201c85SYohann bool has_collo_grad = !!basis_data->d_collo_grad_1d; 2689e201c85SYohann data->G.inputs[i] = has_collo_grad ? basis_data->d_collo_grad_1d : basis_data->d_grad_1d; 2699e201c85SYohann code << " __shared__ CeedScalar s_G_in_" << i << "[" << Q_1d * (has_collo_grad ? Q_1d : P_1d) << "];\n"; 2702b730f8bSJeremy L Thompson code << " loadMatrix<" << (has_collo_grad ? "Q_1d" : ("P_in_" + std::to_string(i))) << ",Q_1d>(data, G.inputs[" << i << "], s_G_in_" << i 2712b730f8bSJeremy L Thompson << ");\n"; 272ac421f39SYohann } 273241a4b83SYohann break; 274241a4b83SYohann case CEED_EVAL_WEIGHT: 275241a4b83SYohann break; // No action 276241a4b83SYohann case CEED_EVAL_DIV: 277241a4b83SYohann break; // TODO: Not implemented 278241a4b83SYohann case CEED_EVAL_CURL: 279241a4b83SYohann break; // TODO: Not implemented 280241a4b83SYohann } 281241a4b83SYohann } 282920dcdc4Sjeremylt 283818e0025SJeremy L Thompson code << "\n // -- Output field constants and basis data --\n"; 2849e201c85SYohann for (CeedInt i = 0; i < num_output_fields; i++) { 285818e0025SJeremy L Thompson code << " // ---- Output field " << i << " ----\n"; 2869e201c85SYohann // Get elem_size, eval_mode, num_comp 2872b730f8bSJeremy L Thompson CeedCallBackend(CeedOperatorFieldGetElemRestriction(op_output_fields[i], &Erestrict)); 2882b730f8bSJeremy L Thompson CeedCallBackend(CeedElemRestrictionGetElementSize(Erestrict, &elem_size)); 2892b730f8bSJeremy L Thompson CeedCallBackend(CeedQFunctionFieldGetEvalMode(qf_output_fields[i], &eval_mode)); 2902b730f8bSJeremy L Thompson CeedCallBackend(CeedElemRestrictionGetNumComponents(Erestrict, &num_comp)); 291920dcdc4Sjeremylt 292920dcdc4Sjeremylt // Set field constants 2932b730f8bSJeremy L Thompson CeedCallBackend(CeedOperatorFieldGetBasis(op_output_fields[i], &basis)); 294920dcdc4Sjeremylt if (basis != CEED_BASIS_COLLOCATED) { 2952b730f8bSJeremy L Thompson CeedCallBackend(CeedBasisGetNumNodes1D(basis, &P_1d)); 2969e201c85SYohann code << " const CeedInt P_out_" << i << " = " << P_1d << ";\n"; 297920dcdc4Sjeremylt } else { 2989e201c85SYohann code << " const CeedInt P_out_" << i << " = " << Q_1d << ";\n"; 299920dcdc4Sjeremylt } 3009e201c85SYohann code << " const CeedInt num_comp_out_" << i << " = " << num_comp << ";\n"; 301920dcdc4Sjeremylt 302920dcdc4Sjeremylt // Load basis data 3039e201c85SYohann code << " // EvalMode: " << CeedEvalModes[eval_mode] << "\n"; 3049e201c85SYohann switch (eval_mode) { 305920dcdc4Sjeremylt case CEED_EVAL_NONE: 306920dcdc4Sjeremylt break; // No action 307920dcdc4Sjeremylt case CEED_EVAL_INTERP: 3082b730f8bSJeremy L Thompson CeedCallBackend(CeedBasisGetData(basis, &basis_data)); 3099e201c85SYohann data->B.outputs[i] = basis_data->d_interp_1d; 3109e201c85SYohann code << " __shared__ CeedScalar s_B_out_" << i << "[" << P_1d * Q_1d << "];\n"; 3119e201c85SYohann code << " loadMatrix<P_out_" << i << ",Q_1d>(data, B.outputs[" << i << "], s_B_out_" << i << ");\n"; 312241a4b83SYohann break; 313241a4b83SYohann case CEED_EVAL_GRAD: 3142b730f8bSJeremy L Thompson CeedCallBackend(CeedBasisGetData(basis, &basis_data)); 3159e201c85SYohann data->B.outputs[i] = basis_data->d_interp_1d; 3169e201c85SYohann code << " __shared__ CeedScalar s_B_out_" << i << "[" << P_1d * Q_1d << "];\n"; 3179e201c85SYohann code << " loadMatrix<P_out_" << i << ",Q_1d>(data, B.outputs[" << i << "], s_B_out_" << i << ");\n"; 3189e201c85SYohann if (use_collograd_parallelization) { 3199e201c85SYohann data->G.outputs[i] = basis_data->d_collo_grad_1d; 3209e201c85SYohann code << " __shared__ CeedScalar s_G_out_" << i << "[" << Q_1d * Q_1d << "];\n"; 3219e201c85SYohann code << " loadMatrix<Q_1d,Q_1d>(data, G.outputs[" << i << "], s_G_out_" << i << ");\n"; 322ac421f39SYohann } else { 3239e201c85SYohann bool has_collo_grad = !!basis_data->d_collo_grad_1d; 3249e201c85SYohann data->G.outputs[i] = has_collo_grad ? basis_data->d_collo_grad_1d : basis_data->d_grad_1d; 3259e201c85SYohann code << " __shared__ CeedScalar s_G_out_" << i << "[" << Q_1d * (has_collo_grad ? Q_1d : P_1d) << "];\n"; 3262b730f8bSJeremy L Thompson code << " loadMatrix<" << (has_collo_grad ? "Q_1d" : ("P_out_" + std::to_string(i))) << ",Q_1d>(data, G.outputs[" << i << "], s_G_out_" 3272b730f8bSJeremy L Thompson << i << ");\n"; 328ac421f39SYohann } 329241a4b83SYohann break; 330e9f4dca0SJeremy L Thompson // LCOV_EXCL_START 331241a4b83SYohann case CEED_EVAL_WEIGHT: { 332241a4b83SYohann Ceed ceed; 3332b730f8bSJeremy L Thompson CeedCallBackend(CeedOperatorGetCeed(op, &ceed)); 3342b730f8bSJeremy L Thompson return CeedError(ceed, CEED_ERROR_BACKEND, "CEED_EVAL_WEIGHT cannot be an output evaluation mode"); 335241a4b83SYohann break; // Should not occur 336241a4b83SYohann } 337241a4b83SYohann case CEED_EVAL_DIV: 338241a4b83SYohann break; // TODO: Not implemented 339241a4b83SYohann case CEED_EVAL_CURL: 340241a4b83SYohann break; // TODO: Not implemented 341e9f4dca0SJeremy L Thompson // LCOV_EXCL_STOP 342241a4b83SYohann } 343241a4b83SYohann } 344818e0025SJeremy L Thompson code << "\n // -- Element loop --\n"; 345ac421f39SYohann code << " __syncthreads();\n"; 3469e201c85SYohann code << " for (CeedInt elem = blockIdx.x*blockDim.z + threadIdx.z; elem < num_elem; elem += gridDim.x*blockDim.z) {\n"; 347241a4b83SYohann // Input basis apply if needed 348ac421f39SYohann // Generate the correct eval mode code for each input 349818e0025SJeremy L Thompson code << " // -- Input field restrictions and basis actions --\n"; 3509e201c85SYohann for (CeedInt i = 0; i < num_input_fields; i++) { 351818e0025SJeremy L Thompson code << " // ---- Input field " << i << " ----\n"; 3529e201c85SYohann // Get elem_size, eval_mode, num_comp 3532b730f8bSJeremy L Thompson CeedCallBackend(CeedOperatorFieldGetElemRestriction(op_input_fields[i], &Erestrict)); 3542b730f8bSJeremy L Thompson CeedCallBackend(CeedElemRestrictionGetElementSize(Erestrict, &elem_size)); 3552b730f8bSJeremy L Thompson CeedCallBackend(CeedQFunctionFieldGetEvalMode(qf_input_fields[i], &eval_mode)); 3562b730f8bSJeremy L Thompson CeedCallBackend(CeedElemRestrictionGetNumComponents(Erestrict, &num_comp)); 357920dcdc4Sjeremylt 3589e201c85SYohann // TODO: put in a function? 359920dcdc4Sjeremylt // Restriction 3602b730f8bSJeremy L Thompson if (eval_mode != CEED_EVAL_WEIGHT && !((eval_mode == CEED_EVAL_NONE) && use_collograd_parallelization)) { 3619e201c85SYohann code << " CeedScalar r_u_" << i << "[num_comp_in_" << i << "*P_in_" << i << "];\n"; 3629a2291e3SJeremy L Thompson 3639e201c85SYohann bool is_strided; 3642b730f8bSJeremy L Thompson CeedCallBackend(CeedElemRestrictionIsStrided(Erestrict, &is_strided)); 3659e201c85SYohann if (!is_strided) { 3662b730f8bSJeremy L Thompson CeedCallBackend(CeedElemRestrictionGetLVectorSize(Erestrict, &lsize)); 3675c7b696cSjeremylt code << " const CeedInt lsize_in_" << i << " = " << lsize << ";\n"; 3689e201c85SYohann CeedInt comp_stride; 3692b730f8bSJeremy L Thompson CeedCallBackend(CeedElemRestrictionGetCompStride(Erestrict, &comp_stride)); 3709e201c85SYohann code << " // CompStride: " << comp_stride << "\n"; 3712b730f8bSJeremy L Thompson CeedCallBackend(CeedElemRestrictionGetData(Erestrict, &restr_data)); 3729e201c85SYohann data->indices.inputs[i] = restr_data->d_ind; 3732b730f8bSJeremy L Thompson code << " readDofsOffset" << dim << "d<num_comp_in_" << i << ", " << comp_stride << ", P_in_" << i << ">(data, lsize_in_" << i 3742b730f8bSJeremy L Thompson << ", elem, indices.inputs[" << i << "], d_u_" << i << ", r_u_" << i << ");\n"; 375ccedf6b0Sjeremylt } else { 376920dcdc4Sjeremylt bool backendstrides; 3772b730f8bSJeremy L Thompson CeedCallBackend(CeedElemRestrictionHasBackendStrides(Erestrict, &backendstrides)); 3789e201c85SYohann CeedInt num_elem; 3792b730f8bSJeremy L Thompson CeedCallBackend(CeedElemRestrictionGetNumElements(Erestrict, &num_elem)); 3809e201c85SYohann CeedInt strides[3] = {1, elem_size * num_elem, elem_size}; 381920dcdc4Sjeremylt if (!backendstrides) { 3822b730f8bSJeremy L Thompson CeedCallBackend(CeedElemRestrictionGetStrides(Erestrict, &strides)); 383ccedf6b0Sjeremylt } 384920dcdc4Sjeremylt code << " // Strides: {" << strides[0] << ", " << strides[1] << ", " << strides[2] << "}\n"; 3852b730f8bSJeremy L Thompson code << " readDofsStrided" << dim << "d<num_comp_in_" << i << ",P_in_" << i << "," << strides[0] << "," << strides[1] << "," << strides[2] 3862b730f8bSJeremy L Thompson << ">(data, elem, d_u_" << i << ", r_u_" << i << ");\n"; 387920dcdc4Sjeremylt } 388920dcdc4Sjeremylt } 389920dcdc4Sjeremylt 3909e201c85SYohann // TODO: put in a function? 391920dcdc4Sjeremylt // Basis action 3929e201c85SYohann code << " // EvalMode: " << CeedEvalModes[eval_mode] << "\n"; 3939e201c85SYohann switch (eval_mode) { 394920dcdc4Sjeremylt case CEED_EVAL_NONE: 3959e201c85SYohann if (!use_collograd_parallelization) { 3969e201c85SYohann code << " CeedScalar* r_t_" << i << " = r_u_" << i << ";\n"; 397920dcdc4Sjeremylt } 398920dcdc4Sjeremylt break; 399920dcdc4Sjeremylt case CEED_EVAL_INTERP: 4009e201c85SYohann code << " CeedScalar r_t_" << i << "[num_comp_in_" << i << "*Q_1d];\n"; 4012b730f8bSJeremy L Thompson code << " Interp" << (dim > 1 ? "Tensor" : "") << dim << "d<num_comp_in_" << i << ",P_in_" << i << ",Q_1d>(data, r_u_" << i << ", s_B_in_" 4022b730f8bSJeremy L Thompson << i << ", r_t_" << i << ");\n"; 403241a4b83SYohann break; 404241a4b83SYohann case CEED_EVAL_GRAD: 4059e201c85SYohann if (use_collograd_parallelization) { 4069e201c85SYohann code << " CeedScalar r_t_" << i << "[num_comp_in_" << i << "*Q_1d];\n"; 4072b730f8bSJeremy L Thompson code << " Interp" << (dim > 1 ? "Tensor" : "") << dim << "d<num_comp_in_" << i << ",P_in_" << i << ",Q_1d>(data, r_u_" << i 4082b730f8bSJeremy L Thompson << ", s_B_in_" << i << ", r_t_" << i << ");\n"; 409ac421f39SYohann } else { 4109e201c85SYohann CeedInt P_1d; 4112b730f8bSJeremy L Thompson CeedCallBackend(CeedOperatorFieldGetBasis(op_input_fields[i], &basis)); 4122b730f8bSJeremy L Thompson CeedCallBackend(CeedBasisGetNumNodes1D(basis, &P_1d)); 4139e201c85SYohann code << " CeedScalar r_t_" << i << "[num_comp_in_" << i << "*dim*Q_1d];\n"; 4142b730f8bSJeremy L Thompson code << " Grad" << (dim > 1 ? "Tensor" : "") << (dim == 3 && Q_1d >= P_1d ? "Collocated" : "") << dim << "d<num_comp_in_" << i 4152b730f8bSJeremy L Thompson << ",P_in_" << i << ",Q_1d>(data, r_u_" << i << ", s_B_in_" << i << ", s_G_in_" << i << ", r_t_" << i << ");\n"; 416ac421f39SYohann } 417241a4b83SYohann break; 418241a4b83SYohann case CEED_EVAL_WEIGHT: 4199e201c85SYohann code << " CeedScalar r_t_" << i << "[Q_1d];\n"; 4202b730f8bSJeremy L Thompson CeedCallBackend(CeedOperatorFieldGetBasis(op_input_fields[i], &basis)); 4212b730f8bSJeremy L Thompson CeedCallBackend(CeedBasisGetData(basis, &basis_data)); 422437930d1SJeremy L Thompson data->W = basis_data->d_q_weight_1d; 4239e201c85SYohann code << " Weight" << (dim > 1 ? "Tensor" : "") << dim << "d<Q_1d>(data, W, r_t_" << i << ");\n"; 424241a4b83SYohann break; // No action 425241a4b83SYohann case CEED_EVAL_DIV: 426241a4b83SYohann break; // TODO: Not implemented 427241a4b83SYohann case CEED_EVAL_CURL: 428241a4b83SYohann break; // TODO: Not implemented 429241a4b83SYohann } 430241a4b83SYohann } 431ac421f39SYohann 432*ea61e9acSJeremy L Thompson // TODO: put in a function + separate collograd logic 433241a4b83SYohann // Q function 434818e0025SJeremy L Thompson code << "\n // -- Output field setup --\n"; 4359e201c85SYohann for (CeedInt i = 0; i < num_output_fields; i++) { 436818e0025SJeremy L Thompson code << "\n // ---- Output field " << i << " ----\n"; 4372b730f8bSJeremy L Thompson CeedCallBackend(CeedQFunctionFieldGetEvalMode(qf_output_fields[i], &eval_mode)); 4382b730f8bSJeremy L Thompson if (eval_mode == CEED_EVAL_GRAD) { 4399e201c85SYohann if (use_collograd_parallelization) { 440ac421f39SYohann // Accumulator for gradient slices 4419e201c85SYohann code << " CeedScalar r_tt_" << i << "[num_comp_out_" << i << "*Q_1d];\n"; 4429e201c85SYohann code << " for (CeedInt i = 0; i < num_comp_out_" << i << "; i++) {\n"; 4439e201c85SYohann code << " for (CeedInt j = 0; j < Q_1d; ++j) {\n"; 4449e201c85SYohann code << " r_tt_" << i << "[j + i*Q_1d] = 0.0;\n"; 445ac421f39SYohann code << " }\n"; 446ac421f39SYohann code << " }\n"; 447ac421f39SYohann } else { 4489e201c85SYohann code << " CeedScalar r_tt_" << i << "[num_comp_out_" << i << "*dim*Q_1d];\n"; 449241a4b83SYohann } 450ac421f39SYohann } 4512b730f8bSJeremy L Thompson if (eval_mode == CEED_EVAL_NONE || eval_mode == CEED_EVAL_INTERP) { 4529e201c85SYohann code << " CeedScalar r_tt_" << i << "[num_comp_out_" << i << "*Q_1d];\n"; 453241a4b83SYohann } 454241a4b83SYohann } 455ac421f39SYohann // We treat quadrature points per slice in 3d to save registers 4569e201c85SYohann if (use_collograd_parallelization) { 4579e201c85SYohann code << "\n // Note: Using planes of 3D elements\n"; 458ac421f39SYohann code << "#pragma unroll\n"; 4599e201c85SYohann code << " for (CeedInt q = 0; q < Q_1d; q++) {\n"; 460818e0025SJeremy L Thompson code << " // -- Input fields --\n"; 4619e201c85SYohann for (CeedInt i = 0; i < num_input_fields; i++) { 462818e0025SJeremy L Thompson code << " // ---- Input field " << i << " ----\n"; 4639e201c85SYohann // Get elem_size, eval_mode, num_comp 4642b730f8bSJeremy L Thompson CeedCallBackend(CeedQFunctionFieldGetEvalMode(qf_input_fields[i], &eval_mode)); 465ac421f39SYohann // Basis action 4669e201c85SYohann code << " // EvalMode: " << CeedEvalModes[eval_mode] << "\n"; 4679e201c85SYohann switch (eval_mode) { 468ac421f39SYohann case CEED_EVAL_NONE: 4699e201c85SYohann code << " CeedScalar r_q_" << i << "[num_comp_in_" << i << "];\n"; 4709a2291e3SJeremy L Thompson 4719e201c85SYohann bool is_strided; 4722b730f8bSJeremy L Thompson CeedCallBackend(CeedOperatorFieldGetElemRestriction(op_input_fields[i], &Erestrict)); 4732b730f8bSJeremy L Thompson CeedCallBackend(CeedElemRestrictionIsStrided(Erestrict, &is_strided)); 4749e201c85SYohann if (!is_strided) { 4752b730f8bSJeremy L Thompson CeedCallBackend(CeedElemRestrictionGetLVectorSize(Erestrict, &lsize)); 4765c7b696cSjeremylt code << " const CeedInt lsize_in_" << i << " = " << lsize << ";\n"; 4779e201c85SYohann CeedInt comp_stride; 4782b730f8bSJeremy L Thompson CeedCallBackend(CeedElemRestrictionGetCompStride(Erestrict, &comp_stride)); 4799e201c85SYohann code << " // CompStride: " << comp_stride << "\n"; 4802b730f8bSJeremy L Thompson CeedCallBackend(CeedElemRestrictionGetData(Erestrict, &restr_data)); 4819e201c85SYohann data->indices.inputs[i] = restr_data->d_ind; 4822b730f8bSJeremy L Thompson code << " readSliceQuadsOffset" 4832b730f8bSJeremy L Thompson << "3d<num_comp_in_" << i << ", " << comp_stride << ", Q_1d>(data, lsize_in_" << i << ", elem, q, indices.inputs[" << i << "], d_u_" 4842b730f8bSJeremy L Thompson << i << ", r_q_" << i << ");\n"; 485920dcdc4Sjeremylt } else { 4862b730f8bSJeremy L Thompson CeedCallBackend(CeedElemRestrictionGetElementSize(Erestrict, &elem_size)); 487920dcdc4Sjeremylt bool backendstrides; 4882b730f8bSJeremy L Thompson CeedCallBackend(CeedElemRestrictionHasBackendStrides(Erestrict, &backendstrides)); 4899e201c85SYohann CeedInt num_elem; 4902b730f8bSJeremy L Thompson CeedCallBackend(CeedElemRestrictionGetNumElements(Erestrict, &num_elem)); 4919e201c85SYohann CeedInt strides[3] = {1, elem_size * num_elem, elem_size}; 492920dcdc4Sjeremylt if (!backendstrides) { 4932b730f8bSJeremy L Thompson CeedCallBackend(CeedElemRestrictionGetStrides(Erestrict, &strides)); 494920dcdc4Sjeremylt } 495920dcdc4Sjeremylt code << " // Strides: {" << strides[0] << ", " << strides[1] << ", " << strides[2] << "}\n"; 4962b730f8bSJeremy L Thompson code << " readSliceQuadsStrided" 4972b730f8bSJeremy L Thompson << "3d<num_comp_in_" << i 4982b730f8bSJeremy L Thompson << ",Q_1d" 4992b730f8bSJeremy L Thompson "," 5002b730f8bSJeremy L Thompson << strides[0] << "," << strides[1] << "," << strides[2] << ">(data, elem, q, d_u_" << i << ", r_q_" << i << ");\n"; 501920dcdc4Sjeremylt } 502ac421f39SYohann break; 503ac421f39SYohann case CEED_EVAL_INTERP: 5049e201c85SYohann code << " CeedScalar r_q_" << i << "[num_comp_in_" << i << "];\n"; 5059e201c85SYohann code << " for (CeedInt j = 0; j < num_comp_in_" << i << " ; ++j) {\n"; 5069e201c85SYohann code << " r_q_" << i << "[j] = r_t_" << i << "[q + j*Q_1d];\n"; 507ac421f39SYohann code << " }\n"; 508ac421f39SYohann break; 509ac421f39SYohann case CEED_EVAL_GRAD: 5109e201c85SYohann code << " CeedScalar r_q_" << i << "[num_comp_in_" << i << "*dim];\n"; 5119e201c85SYohann code << " gradCollo3d<num_comp_in_" << i << ",Q_1d>(data, q, r_t_" << i << ", s_G_in_" << i << ", r_q_" << i << ");\n"; 512ac421f39SYohann break; 513ac421f39SYohann case CEED_EVAL_WEIGHT: 5149e201c85SYohann code << " CeedScalar r_q_" << i << "[1];\n"; 5159e201c85SYohann code << " r_q_" << i << "[0] = r_t_" << i << "[q];\n"; 516ac421f39SYohann break; // No action 517ac421f39SYohann case CEED_EVAL_DIV: 518ac421f39SYohann break; // TODO: Not implemented 519ac421f39SYohann case CEED_EVAL_CURL: 520ac421f39SYohann break; // TODO: Not implemented 521ac421f39SYohann } 522ac421f39SYohann } 523818e0025SJeremy L Thompson code << "\n // -- Output fields --\n"; 5249e201c85SYohann for (CeedInt i = 0; i < num_output_fields; i++) { 525818e0025SJeremy L Thompson code << " // ---- Output field " << i << " ----\n"; 5262b730f8bSJeremy L Thompson CeedCallBackend(CeedQFunctionFieldGetEvalMode(qf_output_fields[i], &eval_mode)); 527ac421f39SYohann // Basis action 5289e201c85SYohann switch (eval_mode) { 529ac421f39SYohann case CEED_EVAL_NONE: 5309e201c85SYohann code << " CeedScalar r_qq_" << i << "[num_comp_out_" << i << "];\n"; 531ac421f39SYohann break; // No action 532ac421f39SYohann case CEED_EVAL_INTERP: 5339e201c85SYohann code << " CeedScalar r_qq_" << i << "[num_comp_out_" << i << "];\n"; 534ac421f39SYohann break; 535ac421f39SYohann case CEED_EVAL_GRAD: 5369e201c85SYohann code << " CeedScalar r_qq_" << i << "[num_comp_out_" << i << "*dim];\n"; 537ac421f39SYohann break; 538ac421f39SYohann case CEED_EVAL_WEIGHT: 539ac421f39SYohann break; // Should not occur 540ac421f39SYohann case CEED_EVAL_DIV: 541ac421f39SYohann break; // TODO: Not implemented 542ac421f39SYohann case CEED_EVAL_CURL: 543ac421f39SYohann break; // TODO: Not implemented 544ac421f39SYohann } 545ac421f39SYohann } 546ac421f39SYohann } else { 5479e201c85SYohann code << "\n // Note: Using full elements\n"; 548818e0025SJeremy L Thompson code << " // -- Input fields --\n"; 5499e201c85SYohann for (CeedInt i = 0; i < num_input_fields; i++) { 550818e0025SJeremy L Thompson code << " // ---- Input field " << i << " ----\n"; 5519e201c85SYohann code << " CeedScalar* r_q_" << i << " = r_t_" << i << ";\n"; 552ac421f39SYohann } 553818e0025SJeremy L Thompson code << " // -- Output fields --\n"; 5549e201c85SYohann for (CeedInt i = 0; i < num_output_fields; i++) { 555818e0025SJeremy L Thompson code << " // ---- Output field " << i << " ----\n"; 5569e201c85SYohann code << " CeedScalar* r_qq_" << i << " = r_tt_" << i << ";\n"; 557ac421f39SYohann } 558ac421f39SYohann } 559818e0025SJeremy L Thompson code << "\n // -- QFunction Inputs and outputs --\n"; 5609e201c85SYohann code << " CeedScalar* in[" << num_input_fields << "];\n"; 5619e201c85SYohann for (CeedInt i = 0; i < num_input_fields; i++) { 562818e0025SJeremy L Thompson code << " // ---- Input field " << i << " ----\n"; 5639e201c85SYohann code << " in[" << i << "] = r_q_" << i << ";\n"; 5644d537eeaSYohann } 5659e201c85SYohann code << " CeedScalar* out[" << num_output_fields << "];\n"; 5669e201c85SYohann for (CeedInt i = 0; i < num_output_fields; i++) { 567818e0025SJeremy L Thompson code << " // ---- Output field " << i << " ----\n"; 5689e201c85SYohann code << " out[" << i << "] = r_qq_" << i << ";\n"; 5694d537eeaSYohann } 570818e0025SJeremy L Thompson code << "\n // -- Apply QFunction --\n"; 5719e201c85SYohann code << " " << q_function_name << "(ctx, "; 5729e201c85SYohann if (dim != 3 || use_collograd_parallelization) { 573ac421f39SYohann code << "1"; 574ac421f39SYohann } else { 5759e201c85SYohann code << "Q_1d"; 576ac421f39SYohann } 577ac421f39SYohann code << ", in, out);\n"; 5789e201c85SYohann if (use_collograd_parallelization) { 579818e0025SJeremy L Thompson code << " // -- Output fields --\n"; 5809e201c85SYohann for (CeedInt i = 0; i < num_output_fields; i++) { 581818e0025SJeremy L Thompson code << " // ---- Output field " << i << " ----\n"; 5822b730f8bSJeremy L Thompson CeedCallBackend(CeedQFunctionFieldGetEvalMode(qf_output_fields[i], &eval_mode)); 583ac421f39SYohann // Basis action 5849e201c85SYohann code << " // EvalMode: " << CeedEvalModes[eval_mode] << "\n"; 5859e201c85SYohann switch (eval_mode) { 586ac421f39SYohann case CEED_EVAL_NONE: 5879e201c85SYohann code << " for (CeedInt j = 0; j < num_comp_out_" << i << " ; ++j) {\n"; 5889e201c85SYohann code << " r_tt_" << i << "[q + j*Q_1d] = r_qq_" << i << "[j];\n"; 589ac421f39SYohann code << " }\n"; 590ac421f39SYohann break; // No action 591ac421f39SYohann case CEED_EVAL_INTERP: 5929e201c85SYohann code << " for (CeedInt j = 0; j < num_comp_out_" << i << " ; ++j) {\n"; 5939e201c85SYohann code << " r_tt_" << i << "[q + j*Q_1d] = r_qq_" << i << "[j];\n"; 594ac421f39SYohann code << " }\n"; 595ac421f39SYohann break; 596ac421f39SYohann case CEED_EVAL_GRAD: 5979e201c85SYohann code << " gradColloTranspose3d<num_comp_out_" << i << ",Q_1d>(data, q, r_qq_" << i << ", s_G_out_" << i << ", r_tt_" << i << ");\n"; 598ac421f39SYohann break; 599ac421f39SYohann case CEED_EVAL_WEIGHT: 600ac421f39SYohann break; // Should not occur 601ac421f39SYohann case CEED_EVAL_DIV: 602ac421f39SYohann break; // TODO: Not implemented 603ac421f39SYohann case CEED_EVAL_CURL: 604ac421f39SYohann break; // TODO: Not implemented 605ac421f39SYohann } 606ac421f39SYohann } 607ac421f39SYohann code << " }\n"; 608ac421f39SYohann } 609241a4b83SYohann 610241a4b83SYohann // Output basis apply if needed 611ac421f39SYohann // Generate the correct eval mode code for each output 612818e0025SJeremy L Thompson code << "\n // -- Output field basis action and restrictions --\n"; 6139e201c85SYohann for (CeedInt i = 0; i < num_output_fields; i++) { 614818e0025SJeremy L Thompson code << " // ---- Output field " << i << " ----\n"; 6159e201c85SYohann // Get elem_size, eval_mode, num_comp 6162b730f8bSJeremy L Thompson CeedCallBackend(CeedOperatorFieldGetElemRestriction(op_output_fields[i], &Erestrict)); 6172b730f8bSJeremy L Thompson CeedCallBackend(CeedElemRestrictionGetElementSize(Erestrict, &elem_size)); 6182b730f8bSJeremy L Thompson CeedCallBackend(CeedQFunctionFieldGetEvalMode(qf_output_fields[i], &eval_mode)); 6192b730f8bSJeremy L Thompson CeedCallBackend(CeedElemRestrictionGetNumComponents(Erestrict, &num_comp)); 6209e201c85SYohann // TODO put in a function 621241a4b83SYohann // Basis action 6229e201c85SYohann code << " // EvalMode: " << CeedEvalModes[eval_mode] << "\n"; 6239e201c85SYohann switch (eval_mode) { 624241a4b83SYohann case CEED_EVAL_NONE: 6259e201c85SYohann code << " CeedScalar* r_v_" << i << " = r_tt_" << i << ";\n"; 626241a4b83SYohann break; // No action 627241a4b83SYohann case CEED_EVAL_INTERP: 6289e201c85SYohann code << " CeedScalar r_v_" << i << "[num_comp_out_" << i << "*P_out_" << i << "];\n"; 6292b730f8bSJeremy L Thompson code << " InterpTranspose" << (dim > 1 ? "Tensor" : "") << dim << "d<num_comp_out_" << i << ",P_out_" << i << ",Q_1d>(data, r_tt_" << i 6302b730f8bSJeremy L Thompson << ", s_B_out_" << i << ", r_v_" << i << ");\n"; 631241a4b83SYohann break; 632241a4b83SYohann case CEED_EVAL_GRAD: 6339e201c85SYohann code << " CeedScalar r_v_" << i << "[num_comp_out_" << i << "*P_out_" << i << "];\n"; 6349e201c85SYohann if (use_collograd_parallelization) { 6352b730f8bSJeremy L Thompson code << " InterpTranspose" << (dim > 1 ? "Tensor" : "") << dim << "d<num_comp_out_" << i << ",P_out_" << i << ",Q_1d>(data, r_tt_" << i 6362b730f8bSJeremy L Thompson << ", s_B_out_" << i << ", r_v_" << i << ");\n"; 637ac421f39SYohann } else { 6389e201c85SYohann CeedInt P_1d; 6392b730f8bSJeremy L Thompson CeedCallBackend(CeedOperatorFieldGetBasis(op_output_fields[i], &basis)); 6402b730f8bSJeremy L Thompson CeedCallBackend(CeedBasisGetNumNodes1D(basis, &P_1d)); 6412b730f8bSJeremy L Thompson code << " GradTranspose" << (dim > 1 ? "Tensor" : "") << (dim == 3 && Q_1d >= P_1d ? "Collocated" : "") << dim << "d<num_comp_out_" << i 6422b730f8bSJeremy L Thompson << ",P_out_" << i << ",Q_1d>(data, r_tt_" << i << ", s_B_out_" << i << ", s_G_out_" << i << ", r_v_" << i << ");\n"; 643ac421f39SYohann } 644241a4b83SYohann break; 645e9f4dca0SJeremy L Thompson // LCOV_EXCL_START 646241a4b83SYohann case CEED_EVAL_WEIGHT: { 647241a4b83SYohann Ceed ceed; 6482b730f8bSJeremy L Thompson CeedCallBackend(CeedOperatorGetCeed(op, &ceed)); 6492b730f8bSJeremy L Thompson return CeedError(ceed, CEED_ERROR_BACKEND, "CEED_EVAL_WEIGHT cannot be an output evaluation mode"); 650241a4b83SYohann break; // Should not occur 651241a4b83SYohann } 652241a4b83SYohann case CEED_EVAL_DIV: 653241a4b83SYohann break; // TODO: Not implemented 654241a4b83SYohann case CEED_EVAL_CURL: 655241a4b83SYohann break; // TODO: Not implemented 656e9f4dca0SJeremy L Thompson // LCOV_EXCL_STOP 657241a4b83SYohann } 6589e201c85SYohann // TODO put in a function 659920dcdc4Sjeremylt // Restriction 6609e201c85SYohann bool is_strided; 6612b730f8bSJeremy L Thompson CeedCallBackend(CeedElemRestrictionIsStrided(Erestrict, &is_strided)); 6629e201c85SYohann if (!is_strided) { 6632b730f8bSJeremy L Thompson CeedCallBackend(CeedElemRestrictionGetLVectorSize(Erestrict, &lsize)); 6645c7b696cSjeremylt code << " const CeedInt lsize_out_" << i << " = " << lsize << ";\n"; 6659e201c85SYohann CeedInt comp_stride; 6662b730f8bSJeremy L Thompson CeedCallBackend(CeedElemRestrictionGetCompStride(Erestrict, &comp_stride)); 6679e201c85SYohann code << " // CompStride: " << comp_stride << "\n"; 6682b730f8bSJeremy L Thompson CeedCallBackend(CeedElemRestrictionGetData(Erestrict, &restr_data)); 6699e201c85SYohann data->indices.outputs[i] = restr_data->d_ind; 6702b730f8bSJeremy L Thompson code << " writeDofsOffset" << dim << "d<num_comp_out_" << i << ", " << comp_stride << ", P_out_" << i << ">(data, lsize_out_" << i 6712b730f8bSJeremy L Thompson << ", elem, indices.outputs[" << i << "], r_v_" << i << ", d_v_" << i << ");\n"; 672920dcdc4Sjeremylt } else { 6739e201c85SYohann bool has_backend_strides; 6742b730f8bSJeremy L Thompson CeedCallBackend(CeedElemRestrictionHasBackendStrides(Erestrict, &has_backend_strides)); 6759e201c85SYohann CeedInt num_elem; 6762b730f8bSJeremy L Thompson CeedCallBackend(CeedElemRestrictionGetNumElements(Erestrict, &num_elem)); 6779e201c85SYohann CeedInt strides[3] = {1, elem_size * num_elem, elem_size}; 6789e201c85SYohann if (!has_backend_strides) { 6792b730f8bSJeremy L Thompson CeedCallBackend(CeedElemRestrictionGetStrides(Erestrict, &strides)); 680920dcdc4Sjeremylt } 681920dcdc4Sjeremylt code << " // Strides: {" << strides[0] << ", " << strides[1] << ", " << strides[2] << "}\n"; 6822b730f8bSJeremy L Thompson code << " writeDofsStrided" << dim << "d<num_comp_out_" << i << ",P_out_" << i << "," << strides[0] << "," << strides[1] << "," << strides[2] 6832b730f8bSJeremy L Thompson << ">(data, elem, r_v_" << i << ", d_v_" << i << ");\n"; 684920dcdc4Sjeremylt } 685241a4b83SYohann } 686241a4b83SYohann 687241a4b83SYohann code << " }\n"; 688818e0025SJeremy L Thompson code << "}\n"; 689818e0025SJeremy L Thompson code << "// -----------------------------------------------------------------------------\n\n"; 690241a4b83SYohann 691ab213215SJeremy L Thompson // View kernel for debugging 69246dc0734SJeremy L Thompson CeedDebug256(ceed, 2, "Generated Operator Kernels:\n"); 6933f21f6b1SJeremy L Thompson CeedDebug(ceed, code.str().c_str()); 694241a4b83SYohann 6952b730f8bSJeremy L Thompson CeedCallBackend(CeedCompileCuda(ceed, code.str().c_str(), &data->module, 1, "T_1D", CeedIntMax(Q_1d, data->max_P_1d))); 6962b730f8bSJeremy L Thompson CeedCallBackend(CeedGetKernelCuda(ceed, data->module, operator_name.c_str(), &data->op)); 697241a4b83SYohann 6982b730f8bSJeremy L Thompson CeedCallBackend(CeedOperatorSetSetupDone(op)); 699e15f9bd0SJeremy L Thompson return CEED_ERROR_SUCCESS; 700241a4b83SYohann } 701ab213215SJeremy L Thompson //------------------------------------------------------------------------------ 702