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 1049aac155SJeremy L Thompson #include <ceed.h> 11ec3da8bcSJed Brown #include <ceed/backend.h> 1249aac155SJeremy L Thompson #include <ceed/jit-source/cuda/cuda-types.h> 139e201c85SYohann #include <ceed/jit-tools.h> 143d576824SJeremy L Thompson #include <cuda_runtime.h> 152b730f8bSJeremy L Thompson 16241a4b83SYohann #include <iostream> 17241a4b83SYohann #include <sstream> 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 25ab213215SJeremy L Thompson //------------------------------------------------------------------------------ 26ab213215SJeremy L Thompson // Build singe operator kernel 27ab213215SJeremy L Thompson //------------------------------------------------------------------------------ 28241a4b83SYohann extern "C" int CeedCudaGenOperatorBuild(CeedOperator op) { 29241a4b83SYohann using std::ostringstream; 30241a4b83SYohann using std::string; 319e201c85SYohann bool is_setup_done; 322b730f8bSJeremy L Thompson CeedCallBackend(CeedOperatorIsSetupDone(op, &is_setup_done)); 339e201c85SYohann if (is_setup_done) return CEED_ERROR_SUCCESS; 34241a4b83SYohann Ceed ceed; 352b730f8bSJeremy L Thompson CeedCallBackend(CeedOperatorGetCeed(op, &ceed)); 36241a4b83SYohann CeedOperator_Cuda_gen *data; 372b730f8bSJeremy L Thompson CeedCallBackend(CeedOperatorGetData(op, &data)); 38241a4b83SYohann CeedQFunction qf; 39241a4b83SYohann CeedQFunction_Cuda_gen *qf_data; 402b730f8bSJeremy L Thompson CeedCallBackend(CeedOperatorGetQFunction(op, &qf)); 412b730f8bSJeremy L Thompson CeedCallBackend(CeedQFunctionGetData(qf, &qf_data)); 42e79b91d9SJeremy L Thompson CeedSize lsize; 432b730f8bSJeremy L Thompson CeedInt Q, P_1d = 0, Q_1d = 0, elem_size, num_input_fields, num_output_fields, num_comp, dim = 1; 442b730f8bSJeremy L Thompson CeedCallBackend(CeedOperatorGetNumQuadraturePoints(op, &Q)); 459e201c85SYohann Q_1d = Q; 469e201c85SYohann CeedOperatorField *op_input_fields, *op_output_fields; 472b730f8bSJeremy L Thompson CeedCallBackend(CeedOperatorGetFields(op, &num_input_fields, &op_input_fields, &num_output_fields, &op_output_fields)); 489e201c85SYohann CeedQFunctionField *qf_input_fields, *qf_output_fields; 492b730f8bSJeremy L Thompson CeedCallBackend(CeedQFunctionGetFields(qf, NULL, &qf_input_fields, NULL, &qf_output_fields)); 509e201c85SYohann CeedEvalMode eval_mode; 51241a4b83SYohann CeedBasis basis; 52241a4b83SYohann CeedBasis_Cuda_shared *basis_data; 53241a4b83SYohann CeedElemRestriction Erestrict; 54461525f5SNatalie Beams CeedElemRestriction_Cuda *restr_data; 55241a4b83SYohann 569e201c85SYohann // TODO: put in a function? 570b454692Sjeremylt // Check for restriction only identity operator 580b454692Sjeremylt bool is_identity_qf; 592b730f8bSJeremy L Thompson CeedCallBackend(CeedQFunctionIsIdentity(qf, &is_identity_qf)); 600b454692Sjeremylt if (is_identity_qf) { 619e201c85SYohann CeedEvalMode eval_mode_in, eval_mode_out; 622b730f8bSJeremy L Thompson CeedCallBackend(CeedQFunctionFieldGetEvalMode(qf_input_fields[0], &eval_mode_in)); 632b730f8bSJeremy L Thompson CeedCallBackend(CeedQFunctionFieldGetEvalMode(qf_output_fields[0], &eval_mode_out)); 649e201c85SYohann if (eval_mode_in == CEED_EVAL_NONE && eval_mode_out == CEED_EVAL_NONE) 650b454692Sjeremylt // LCOV_EXCL_START 662b730f8bSJeremy L Thompson return CeedError(ceed, CEED_ERROR_BACKEND, "Backend does not implement restriction only identity operators"); 670b454692Sjeremylt // LCOV_EXCL_STOP 680b454692Sjeremylt } 690b454692Sjeremylt 70241a4b83SYohann ostringstream code; 71241a4b83SYohann 729e201c85SYohann // TODO: put in a function? 73f1a13f77SYohann Dudouit // Add atomicAdd function for old NVidia architectures 74f1a13f77SYohann Dudouit struct cudaDeviceProp prop; 75f1a13f77SYohann Dudouit Ceed_Cuda *ceed_data; 762b730f8bSJeremy L Thompson CeedCallBackend(CeedGetData(ceed, &ceed_data)); 772b730f8bSJeremy L Thompson CeedCallBackend(cudaGetDeviceProperties(&prop, ceed_data->device_id)); 7880a9ef05SNatalie Beams if ((prop.major < 6) && (CEED_SCALAR_TYPE != CEED_SCALAR_FP32)) { 799e201c85SYohann char *atomic_add_path, *atomic_add_source; 802b730f8bSJeremy L Thompson CeedCallBackend(CeedGetJitAbsolutePath(ceed, "ceed/jit-source/cuda/cuda-atomic-add-fallback.h", &atomic_add_path)); 819e201c85SYohann CeedDebug256(ceed, 2, "----- Loading Atomic Add Source -----\n"); 822b730f8bSJeremy L Thompson CeedCallBackend(CeedLoadSourceToBuffer(ceed, atomic_add_path, &atomic_add_source)); 839e201c85SYohann code << atomic_add_source; 842b730f8bSJeremy L Thompson CeedCallBackend(CeedFree(&atomic_add_path)); 852b730f8bSJeremy L Thompson CeedCallBackend(CeedFree(&atomic_add_source)); 86f1a13f77SYohann Dudouit } 87f1a13f77SYohann Dudouit 889e201c85SYohann // Load basis source files 899e201c85SYohann // TODO: generalize to accept different device functions? 909e201c85SYohann { 919e201c85SYohann char *tensor_basis_kernel_path, *tensor_basis_kernel_source; 922b730f8bSJeremy L Thompson CeedCallBackend(CeedGetJitAbsolutePath(ceed, "ceed/jit-source/cuda/cuda-shared-basis-tensor-templates.h", &tensor_basis_kernel_path)); 939e201c85SYohann CeedDebug256(ceed, 2, "----- Loading Tensor Basis Kernel Source -----\n"); 942b730f8bSJeremy L Thompson CeedCallBackend(CeedLoadSourceToBuffer(ceed, tensor_basis_kernel_path, &tensor_basis_kernel_source)); 959e201c85SYohann code << tensor_basis_kernel_source; 962b730f8bSJeremy L Thompson CeedCallBackend(CeedFree(&tensor_basis_kernel_path)); 972b730f8bSJeremy L Thompson CeedCallBackend(CeedFree(&tensor_basis_kernel_source)); 989e201c85SYohann } 999e201c85SYohann { 1009e201c85SYohann char *cuda_gen_template_path, *cuda_gen_template_source; 1012b730f8bSJeremy L Thompson CeedCallBackend(CeedGetJitAbsolutePath(ceed, "ceed/jit-source/cuda/cuda-gen-templates.h", &cuda_gen_template_path)); 1029e201c85SYohann CeedDebug256(ceed, 2, "----- Loading Cuda-Gen Template Source -----\n"); 1032b730f8bSJeremy L Thompson CeedCallBackend(CeedLoadSourceToBuffer(ceed, cuda_gen_template_path, &cuda_gen_template_source)); 1049e201c85SYohann code << cuda_gen_template_source; 1052b730f8bSJeremy L Thompson CeedCallBackend(CeedFree(&cuda_gen_template_path)); 1062b730f8bSJeremy L Thompson CeedCallBackend(CeedFree(&cuda_gen_template_source)); 1079e201c85SYohann } 108241a4b83SYohann 1099e201c85SYohann // Get QFunction source and name 1109e201c85SYohann string q_function_source(qf_data->q_function_source); 1119e201c85SYohann string q_function_name(qf_data->q_function_name); 1129e201c85SYohann string operator_name; 113204bfdd7SJeremy L Thompson operator_name = "CeedKernelCudaGenOperator_" + q_function_name; 1144d537eeaSYohann 1159e201c85SYohann // Find dim, P_1d, Q_1d 1169e201c85SYohann data->max_P_1d = 0; 1179e201c85SYohann for (CeedInt i = 0; i < num_input_fields; i++) { 1182b730f8bSJeremy L Thompson CeedCallBackend(CeedOperatorFieldGetBasis(op_input_fields[i], &basis)); 1191da99368SJeremy L Thompson if (basis != CEED_BASIS_COLLOCATED) { 1202b730f8bSJeremy L Thompson CeedCallBackend(CeedBasisGetData(basis, &basis_data)); 1212b730f8bSJeremy L Thompson CeedCallBackend(CeedQFunctionFieldGetEvalMode(qf_input_fields[i], &eval_mode)); 1221da99368SJeremy L Thompson 1239e201c85SYohann // Collect dim, P_1d, and Q_1d 1242b730f8bSJeremy L Thompson CeedCallBackend(CeedBasisGetDimension(basis, &dim)); 1251da99368SJeremy L Thompson bool isTensor; 1262b730f8bSJeremy L Thompson CeedCallBackend(CeedBasisIsTensor(basis, &isTensor)); 1271da99368SJeremy L Thompson if (isTensor) { 1282b730f8bSJeremy L Thompson CeedCallBackend(CeedBasisGetNumQuadraturePoints1D(basis, &Q_1d)); 1292b730f8bSJeremy L Thompson CeedCallBackend(CeedBasisGetNumNodes1D(basis, &P_1d)); 1309e201c85SYohann if (P_1d > data->max_P_1d) data->max_P_1d = P_1d; 1311da99368SJeremy L Thompson } else { 132e9f4dca0SJeremy L Thompson // LCOV_EXCL_START 133e15f9bd0SJeremy L Thompson return CeedError(ceed, CEED_ERROR_BACKEND, "Backend does not implement operators with non-tensor basis"); 134e9f4dca0SJeremy L Thompson // LCOV_EXCL_STOP 1351da99368SJeremy L Thompson } 1361da99368SJeremy L Thompson } 1371da99368SJeremy L Thompson } 1389e201c85SYohann // Check output bases for Q_1d, dim as well 139dc64899eSYohann // The only input basis might be CEED_BASIS_COLLOCATED 1409e201c85SYohann for (CeedInt i = 0; i < num_output_fields; i++) { 1412b730f8bSJeremy L Thompson CeedCallBackend(CeedOperatorFieldGetBasis(op_output_fields[i], &basis)); 1420f54b25eSjeremylt 1431da99368SJeremy L Thompson if (basis != CEED_BASIS_COLLOCATED) { 1442b730f8bSJeremy L Thompson CeedCallBackend(CeedBasisGetData(basis, &basis_data)); 1452b730f8bSJeremy L Thompson CeedCallBackend(CeedQFunctionFieldGetEvalMode(qf_output_fields[i], &eval_mode)); 1460f54b25eSjeremylt 1479e201c85SYohann // Collect Q_1d 1482b730f8bSJeremy L Thompson CeedCallBackend(CeedBasisGetDimension(basis, &dim)); 1491da99368SJeremy L Thompson bool isTensor; 1502b730f8bSJeremy L Thompson CeedCallBackend(CeedBasisIsTensor(basis, &isTensor)); 1511da99368SJeremy L Thompson if (isTensor) { 1522b730f8bSJeremy L Thompson CeedCallBackend(CeedBasisGetNumQuadraturePoints1D(basis, &Q_1d)); 1531da99368SJeremy L Thompson } else { 154e9f4dca0SJeremy L Thompson // LCOV_EXCL_START 155e15f9bd0SJeremy L Thompson return CeedError(ceed, CEED_ERROR_BACKEND, "Backend does not implement operators with non-tensor basis"); 156e9f4dca0SJeremy L Thompson // LCOV_EXCL_STOP 1571da99368SJeremy L Thompson } 1581da99368SJeremy L Thompson } 1591da99368SJeremy L Thompson } 1601da99368SJeremy L Thompson data->dim = dim; 1619e201c85SYohann data->Q_1d = Q_1d; 1621da99368SJeremy L Thompson 1639e201c85SYohann // Only use 3D collocated gradient parallelization strategy when gradient is computed 1649e201c85SYohann // TODO: put in a function? 1659e201c85SYohann bool use_collograd_parallelization = false; 1669e201c85SYohann if (dim == 3) { 1679e201c85SYohann bool was_grad_found = false; 1689e201c85SYohann for (CeedInt i = 0; i < num_input_fields; i++) { 1692b730f8bSJeremy L Thompson CeedCallBackend(CeedQFunctionFieldGetEvalMode(qf_input_fields[i], &eval_mode)); 1709e201c85SYohann if (eval_mode == CEED_EVAL_GRAD) { 1712b730f8bSJeremy L Thompson CeedCallBackend(CeedOperatorFieldGetBasis(op_input_fields[i], &basis)); 1722b730f8bSJeremy L Thompson CeedCallBackend(CeedBasisGetData(basis, &basis_data)); 1739e201c85SYohann use_collograd_parallelization = !!basis_data->d_collo_grad_1d && (was_grad_found ? use_collograd_parallelization : true); 1749e201c85SYohann was_grad_found = true; 1759e201c85SYohann } 1769e201c85SYohann } 1779e201c85SYohann for (CeedInt i = 0; i < num_output_fields; i++) { 1782b730f8bSJeremy L Thompson CeedCallBackend(CeedQFunctionFieldGetEvalMode(qf_output_fields[i], &eval_mode)); 1799e201c85SYohann if (eval_mode == CEED_EVAL_GRAD) { 1802b730f8bSJeremy L Thompson CeedCallBackend(CeedOperatorFieldGetBasis(op_output_fields[i], &basis)); 1812b730f8bSJeremy L Thompson CeedCallBackend(CeedBasisGetData(basis, &basis_data)); 1829e201c85SYohann use_collograd_parallelization = !!basis_data->d_collo_grad_1d && (was_grad_found ? use_collograd_parallelization : true); 1839e201c85SYohann was_grad_found = true; 1849e201c85SYohann } 1859e201c85SYohann } 1861da99368SJeremy L Thompson } 1871da99368SJeremy L Thompson 1889e201c85SYohann // Define CEED_Q_VLA 1899e201c85SYohann code << "\n#undef CEED_Q_VLA\n"; 1909e201c85SYohann if (dim != 3 || use_collograd_parallelization) { 1919e201c85SYohann code << "#define CEED_Q_VLA 1\n\n"; 1929e201c85SYohann } else { 1939e201c85SYohann code << "#define CEED_Q_VLA " << Q_1d << "\n\n"; 1949e201c85SYohann } 1959e201c85SYohann 1969e201c85SYohann code << q_function_source; 197241a4b83SYohann 198241a4b83SYohann // Setup 199818e0025SJeremy L Thompson code << "\n// -----------------------------------------------------------------------------\n"; 2002b730f8bSJeremy L Thompson code << "\nextern \"C\" __global__ void " << operator_name 2012b730f8bSJeremy L Thompson << "(CeedInt num_elem, void* ctx, FieldsInt_Cuda indices, Fields_Cuda fields, Fields_Cuda B, Fields_Cuda G, CeedScalar* W) {\n"; 2029e201c85SYohann for (CeedInt i = 0; i < num_input_fields; i++) { 2032b730f8bSJeremy L Thompson CeedCallBackend(CeedQFunctionFieldGetEvalMode(qf_input_fields[i], &eval_mode)); 2049e201c85SYohann if (eval_mode != CEED_EVAL_WEIGHT) { // Skip CEED_EVAL_WEIGHT 2059e201c85SYohann code << " const CeedScalar* d_u_" << i << " = fields.inputs[" << i << "];\n"; 206241a4b83SYohann } 207241a4b83SYohann } 208241a4b83SYohann 2099e201c85SYohann for (CeedInt i = 0; i < num_output_fields; i++) { 2109e201c85SYohann code << " CeedScalar* d_v_" << i << " = fields.outputs[" << i << "];\n"; 211241a4b83SYohann } 2121da99368SJeremy L Thompson 2139e201c85SYohann code << " const CeedInt dim = " << dim << ";\n"; 2149e201c85SYohann code << " const CeedInt Q_1d = " << Q_1d << ";\n"; 2151da99368SJeremy L Thompson 216241a4b83SYohann code << " extern __shared__ CeedScalar slice[];\n"; 2179e201c85SYohann // TODO put in a function? InitSharedData_Cuda? 2189e201c85SYohann code << " SharedData_Cuda data;\n"; 2199e201c85SYohann code << " data.t_id_x = threadIdx.x;\n"; 2209e201c85SYohann code << " data.t_id_y = threadIdx.y;\n"; 2219e201c85SYohann code << " data.t_id_z = threadIdx.z;\n"; 2229e201c85SYohann code << " data.t_id = threadIdx.x + threadIdx.y*blockDim.x + threadIdx.z*blockDim.y*blockDim.x;\n"; 2239e201c85SYohann code << " data.slice = slice+data.t_id_z*T_1D" << (dim > 1 ? "*T_1D" : "") << ";\n"; 224920dcdc4Sjeremylt 225818e0025SJeremy L Thompson code << "\n // -- Input field constants and basis data --\n"; 2269e201c85SYohann // TODO: Put in a function? 227ac421f39SYohann // Initialize constants, and matrices B and G 2289e201c85SYohann for (CeedInt i = 0; i < num_input_fields; i++) { 229818e0025SJeremy L Thompson code << " // ---- Input field " << i << " ----\n"; 2309e201c85SYohann // Get elem_size, eval_mode, num_comp 2312b730f8bSJeremy L Thompson CeedCallBackend(CeedOperatorFieldGetElemRestriction(op_input_fields[i], &Erestrict)); 2322b730f8bSJeremy L Thompson CeedCallBackend(CeedElemRestrictionGetElementSize(Erestrict, &elem_size)); 2332b730f8bSJeremy L Thompson CeedCallBackend(CeedQFunctionFieldGetEvalMode(qf_input_fields[i], &eval_mode)); 2342b730f8bSJeremy L Thompson CeedCallBackend(CeedElemRestrictionGetNumComponents(Erestrict, &num_comp)); 235920dcdc4Sjeremylt 236920dcdc4Sjeremylt // Set field constants 2379e201c85SYohann if (eval_mode != CEED_EVAL_WEIGHT) { 2382b730f8bSJeremy L Thompson CeedCallBackend(CeedOperatorFieldGetBasis(op_input_fields[i], &basis)); 239920dcdc4Sjeremylt if (basis != CEED_BASIS_COLLOCATED) { 2402b730f8bSJeremy L Thompson CeedCallBackend(CeedBasisGetNumNodes1D(basis, &P_1d)); 2419e201c85SYohann code << " const CeedInt P_in_" << i << " = " << P_1d << ";\n"; 242920dcdc4Sjeremylt } else { 2439e201c85SYohann code << " const CeedInt P_in_" << i << " = " << Q_1d << ";\n"; 244920dcdc4Sjeremylt } 2459e201c85SYohann code << " const CeedInt num_comp_in_" << i << " = " << num_comp << ";\n"; 246920dcdc4Sjeremylt } 247920dcdc4Sjeremylt 248920dcdc4Sjeremylt // Load basis data 2499e201c85SYohann code << " // EvalMode: " << CeedEvalModes[eval_mode] << "\n"; 2509e201c85SYohann switch (eval_mode) { 251241a4b83SYohann case CEED_EVAL_NONE: 252241a4b83SYohann break; 253241a4b83SYohann case CEED_EVAL_INTERP: 2542b730f8bSJeremy L Thompson CeedCallBackend(CeedBasisGetData(basis, &basis_data)); 2559e201c85SYohann data->B.inputs[i] = basis_data->d_interp_1d; 2569e201c85SYohann code << " __shared__ CeedScalar s_B_in_" << i << "[" << P_1d * Q_1d << "];\n"; 2579e201c85SYohann code << " loadMatrix<P_in_" << i << ",Q_1d>(data, B.inputs[" << i << "], s_B_in_" << i << ");\n"; 258241a4b83SYohann break; 259241a4b83SYohann case CEED_EVAL_GRAD: 2602b730f8bSJeremy L Thompson CeedCallBackend(CeedBasisGetData(basis, &basis_data)); 2619e201c85SYohann data->B.inputs[i] = basis_data->d_interp_1d; 2629e201c85SYohann code << " __shared__ CeedScalar s_B_in_" << i << "[" << P_1d * Q_1d << "];\n"; 2639e201c85SYohann code << " loadMatrix<P_in_" << i << ",Q_1d>(data, B.inputs[" << i << "], s_B_in_" << i << ");\n"; 2649e201c85SYohann if (use_collograd_parallelization) { 2659e201c85SYohann data->G.inputs[i] = basis_data->d_collo_grad_1d; 2669e201c85SYohann code << " __shared__ CeedScalar s_G_in_" << i << "[" << Q_1d * Q_1d << "];\n"; 2679e201c85SYohann code << " loadMatrix<Q_1d,Q_1d>(data, G.inputs[" << i << "], s_G_in_" << i << ");\n"; 268ac421f39SYohann } else { 2699e201c85SYohann bool has_collo_grad = !!basis_data->d_collo_grad_1d; 2709e201c85SYohann data->G.inputs[i] = has_collo_grad ? basis_data->d_collo_grad_1d : basis_data->d_grad_1d; 2719e201c85SYohann code << " __shared__ CeedScalar s_G_in_" << i << "[" << Q_1d * (has_collo_grad ? Q_1d : P_1d) << "];\n"; 2722b730f8bSJeremy 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 2732b730f8bSJeremy L Thompson << ");\n"; 274ac421f39SYohann } 275241a4b83SYohann break; 276241a4b83SYohann case CEED_EVAL_WEIGHT: 277241a4b83SYohann break; // No action 278241a4b83SYohann case CEED_EVAL_DIV: 279241a4b83SYohann break; // TODO: Not implemented 280241a4b83SYohann case CEED_EVAL_CURL: 281241a4b83SYohann break; // TODO: Not implemented 282241a4b83SYohann } 283241a4b83SYohann } 284920dcdc4Sjeremylt 285818e0025SJeremy L Thompson code << "\n // -- Output field constants and basis data --\n"; 2869e201c85SYohann for (CeedInt i = 0; i < num_output_fields; i++) { 287818e0025SJeremy L Thompson code << " // ---- Output field " << i << " ----\n"; 2889e201c85SYohann // Get elem_size, eval_mode, num_comp 2892b730f8bSJeremy L Thompson CeedCallBackend(CeedOperatorFieldGetElemRestriction(op_output_fields[i], &Erestrict)); 2902b730f8bSJeremy L Thompson CeedCallBackend(CeedElemRestrictionGetElementSize(Erestrict, &elem_size)); 2912b730f8bSJeremy L Thompson CeedCallBackend(CeedQFunctionFieldGetEvalMode(qf_output_fields[i], &eval_mode)); 2922b730f8bSJeremy L Thompson CeedCallBackend(CeedElemRestrictionGetNumComponents(Erestrict, &num_comp)); 293920dcdc4Sjeremylt 294920dcdc4Sjeremylt // Set field constants 2952b730f8bSJeremy L Thompson CeedCallBackend(CeedOperatorFieldGetBasis(op_output_fields[i], &basis)); 296920dcdc4Sjeremylt if (basis != CEED_BASIS_COLLOCATED) { 2972b730f8bSJeremy L Thompson CeedCallBackend(CeedBasisGetNumNodes1D(basis, &P_1d)); 2989e201c85SYohann code << " const CeedInt P_out_" << i << " = " << P_1d << ";\n"; 299920dcdc4Sjeremylt } else { 3009e201c85SYohann code << " const CeedInt P_out_" << i << " = " << Q_1d << ";\n"; 301920dcdc4Sjeremylt } 3029e201c85SYohann code << " const CeedInt num_comp_out_" << i << " = " << num_comp << ";\n"; 303920dcdc4Sjeremylt 304920dcdc4Sjeremylt // Load basis data 3059e201c85SYohann code << " // EvalMode: " << CeedEvalModes[eval_mode] << "\n"; 3069e201c85SYohann switch (eval_mode) { 307920dcdc4Sjeremylt case CEED_EVAL_NONE: 308920dcdc4Sjeremylt break; // No action 309920dcdc4Sjeremylt case CEED_EVAL_INTERP: 3102b730f8bSJeremy L Thompson CeedCallBackend(CeedBasisGetData(basis, &basis_data)); 3119e201c85SYohann data->B.outputs[i] = basis_data->d_interp_1d; 3129e201c85SYohann code << " __shared__ CeedScalar s_B_out_" << i << "[" << P_1d * Q_1d << "];\n"; 3139e201c85SYohann code << " loadMatrix<P_out_" << i << ",Q_1d>(data, B.outputs[" << i << "], s_B_out_" << i << ");\n"; 314241a4b83SYohann break; 315241a4b83SYohann case CEED_EVAL_GRAD: 3162b730f8bSJeremy L Thompson CeedCallBackend(CeedBasisGetData(basis, &basis_data)); 3179e201c85SYohann data->B.outputs[i] = basis_data->d_interp_1d; 3189e201c85SYohann code << " __shared__ CeedScalar s_B_out_" << i << "[" << P_1d * Q_1d << "];\n"; 3199e201c85SYohann code << " loadMatrix<P_out_" << i << ",Q_1d>(data, B.outputs[" << i << "], s_B_out_" << i << ");\n"; 3209e201c85SYohann if (use_collograd_parallelization) { 3219e201c85SYohann data->G.outputs[i] = basis_data->d_collo_grad_1d; 3229e201c85SYohann code << " __shared__ CeedScalar s_G_out_" << i << "[" << Q_1d * Q_1d << "];\n"; 3239e201c85SYohann code << " loadMatrix<Q_1d,Q_1d>(data, G.outputs[" << i << "], s_G_out_" << i << ");\n"; 324ac421f39SYohann } else { 3259e201c85SYohann bool has_collo_grad = !!basis_data->d_collo_grad_1d; 3269e201c85SYohann data->G.outputs[i] = has_collo_grad ? basis_data->d_collo_grad_1d : basis_data->d_grad_1d; 3279e201c85SYohann code << " __shared__ CeedScalar s_G_out_" << i << "[" << Q_1d * (has_collo_grad ? Q_1d : P_1d) << "];\n"; 3282b730f8bSJeremy L Thompson code << " loadMatrix<" << (has_collo_grad ? "Q_1d" : ("P_out_" + std::to_string(i))) << ",Q_1d>(data, G.outputs[" << i << "], s_G_out_" 3292b730f8bSJeremy L Thompson << i << ");\n"; 330ac421f39SYohann } 331241a4b83SYohann break; 332e9f4dca0SJeremy L Thompson // LCOV_EXCL_START 333241a4b83SYohann case CEED_EVAL_WEIGHT: { 334241a4b83SYohann Ceed ceed; 3352b730f8bSJeremy L Thompson CeedCallBackend(CeedOperatorGetCeed(op, &ceed)); 3362b730f8bSJeremy L Thompson return CeedError(ceed, CEED_ERROR_BACKEND, "CEED_EVAL_WEIGHT cannot be an output evaluation mode"); 337241a4b83SYohann break; // Should not occur 338241a4b83SYohann } 339241a4b83SYohann case CEED_EVAL_DIV: 340241a4b83SYohann break; // TODO: Not implemented 341241a4b83SYohann case CEED_EVAL_CURL: 342241a4b83SYohann break; // TODO: Not implemented 343e9f4dca0SJeremy L Thompson // LCOV_EXCL_STOP 344241a4b83SYohann } 345241a4b83SYohann } 346818e0025SJeremy L Thompson code << "\n // -- Element loop --\n"; 347ac421f39SYohann code << " __syncthreads();\n"; 3489e201c85SYohann code << " for (CeedInt elem = blockIdx.x*blockDim.z + threadIdx.z; elem < num_elem; elem += gridDim.x*blockDim.z) {\n"; 349241a4b83SYohann // Input basis apply if needed 350ac421f39SYohann // Generate the correct eval mode code for each input 351818e0025SJeremy L Thompson code << " // -- Input field restrictions and basis actions --\n"; 3529e201c85SYohann for (CeedInt i = 0; i < num_input_fields; i++) { 353818e0025SJeremy L Thompson code << " // ---- Input field " << i << " ----\n"; 3549e201c85SYohann // Get elem_size, eval_mode, num_comp 3552b730f8bSJeremy L Thompson CeedCallBackend(CeedOperatorFieldGetElemRestriction(op_input_fields[i], &Erestrict)); 3562b730f8bSJeremy L Thompson CeedCallBackend(CeedElemRestrictionGetElementSize(Erestrict, &elem_size)); 3572b730f8bSJeremy L Thompson CeedCallBackend(CeedQFunctionFieldGetEvalMode(qf_input_fields[i], &eval_mode)); 3582b730f8bSJeremy L Thompson CeedCallBackend(CeedElemRestrictionGetNumComponents(Erestrict, &num_comp)); 359920dcdc4Sjeremylt 3609e201c85SYohann // TODO: put in a function? 361920dcdc4Sjeremylt // Restriction 3622b730f8bSJeremy L Thompson if (eval_mode != CEED_EVAL_WEIGHT && !((eval_mode == CEED_EVAL_NONE) && use_collograd_parallelization)) { 3639e201c85SYohann code << " CeedScalar r_u_" << i << "[num_comp_in_" << i << "*P_in_" << i << "];\n"; 3649a2291e3SJeremy L Thompson 3659e201c85SYohann bool is_strided; 3662b730f8bSJeremy L Thompson CeedCallBackend(CeedElemRestrictionIsStrided(Erestrict, &is_strided)); 3679e201c85SYohann if (!is_strided) { 3682b730f8bSJeremy L Thompson CeedCallBackend(CeedElemRestrictionGetLVectorSize(Erestrict, &lsize)); 3695c7b696cSjeremylt code << " const CeedInt lsize_in_" << i << " = " << lsize << ";\n"; 3709e201c85SYohann CeedInt comp_stride; 3712b730f8bSJeremy L Thompson CeedCallBackend(CeedElemRestrictionGetCompStride(Erestrict, &comp_stride)); 3729e201c85SYohann code << " // CompStride: " << comp_stride << "\n"; 3732b730f8bSJeremy L Thompson CeedCallBackend(CeedElemRestrictionGetData(Erestrict, &restr_data)); 3749e201c85SYohann data->indices.inputs[i] = restr_data->d_ind; 3752b730f8bSJeremy L Thompson code << " readDofsOffset" << dim << "d<num_comp_in_" << i << ", " << comp_stride << ", P_in_" << i << ">(data, lsize_in_" << i 3762b730f8bSJeremy L Thompson << ", elem, indices.inputs[" << i << "], d_u_" << i << ", r_u_" << i << ");\n"; 377ccedf6b0Sjeremylt } else { 378920dcdc4Sjeremylt bool backendstrides; 3792b730f8bSJeremy L Thompson CeedCallBackend(CeedElemRestrictionHasBackendStrides(Erestrict, &backendstrides)); 3809e201c85SYohann CeedInt num_elem; 3812b730f8bSJeremy L Thompson CeedCallBackend(CeedElemRestrictionGetNumElements(Erestrict, &num_elem)); 3829e201c85SYohann CeedInt strides[3] = {1, elem_size * num_elem, elem_size}; 383920dcdc4Sjeremylt if (!backendstrides) { 3842b730f8bSJeremy L Thompson CeedCallBackend(CeedElemRestrictionGetStrides(Erestrict, &strides)); 385ccedf6b0Sjeremylt } 386920dcdc4Sjeremylt code << " // Strides: {" << strides[0] << ", " << strides[1] << ", " << strides[2] << "}\n"; 3872b730f8bSJeremy L Thompson code << " readDofsStrided" << dim << "d<num_comp_in_" << i << ",P_in_" << i << "," << strides[0] << "," << strides[1] << "," << strides[2] 3882b730f8bSJeremy L Thompson << ">(data, elem, d_u_" << i << ", r_u_" << i << ");\n"; 389920dcdc4Sjeremylt } 390920dcdc4Sjeremylt } 391920dcdc4Sjeremylt 3929e201c85SYohann // TODO: put in a function? 393920dcdc4Sjeremylt // Basis action 3949e201c85SYohann code << " // EvalMode: " << CeedEvalModes[eval_mode] << "\n"; 3959e201c85SYohann switch (eval_mode) { 396920dcdc4Sjeremylt case CEED_EVAL_NONE: 3979e201c85SYohann if (!use_collograd_parallelization) { 3989e201c85SYohann code << " CeedScalar* r_t_" << i << " = r_u_" << i << ";\n"; 399920dcdc4Sjeremylt } 400920dcdc4Sjeremylt break; 401920dcdc4Sjeremylt case CEED_EVAL_INTERP: 4029e201c85SYohann code << " CeedScalar r_t_" << i << "[num_comp_in_" << i << "*Q_1d];\n"; 4032b730f8bSJeremy 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_" 4042b730f8bSJeremy L Thompson << i << ", r_t_" << i << ");\n"; 405241a4b83SYohann break; 406241a4b83SYohann case CEED_EVAL_GRAD: 4079e201c85SYohann if (use_collograd_parallelization) { 4089e201c85SYohann code << " CeedScalar r_t_" << i << "[num_comp_in_" << i << "*Q_1d];\n"; 4092b730f8bSJeremy L Thompson code << " Interp" << (dim > 1 ? "Tensor" : "") << dim << "d<num_comp_in_" << i << ",P_in_" << i << ",Q_1d>(data, r_u_" << i 4102b730f8bSJeremy L Thompson << ", s_B_in_" << i << ", r_t_" << i << ");\n"; 411ac421f39SYohann } else { 4129e201c85SYohann CeedInt P_1d; 4132b730f8bSJeremy L Thompson CeedCallBackend(CeedOperatorFieldGetBasis(op_input_fields[i], &basis)); 4142b730f8bSJeremy L Thompson CeedCallBackend(CeedBasisGetNumNodes1D(basis, &P_1d)); 4159e201c85SYohann code << " CeedScalar r_t_" << i << "[num_comp_in_" << i << "*dim*Q_1d];\n"; 4162b730f8bSJeremy L Thompson code << " Grad" << (dim > 1 ? "Tensor" : "") << (dim == 3 && Q_1d >= P_1d ? "Collocated" : "") << dim << "d<num_comp_in_" << i 4172b730f8bSJeremy L Thompson << ",P_in_" << i << ",Q_1d>(data, r_u_" << i << ", s_B_in_" << i << ", s_G_in_" << i << ", r_t_" << i << ");\n"; 418ac421f39SYohann } 419241a4b83SYohann break; 420241a4b83SYohann case CEED_EVAL_WEIGHT: 4219e201c85SYohann code << " CeedScalar r_t_" << i << "[Q_1d];\n"; 4222b730f8bSJeremy L Thompson CeedCallBackend(CeedOperatorFieldGetBasis(op_input_fields[i], &basis)); 4232b730f8bSJeremy L Thompson CeedCallBackend(CeedBasisGetData(basis, &basis_data)); 424437930d1SJeremy L Thompson data->W = basis_data->d_q_weight_1d; 4259e201c85SYohann code << " Weight" << (dim > 1 ? "Tensor" : "") << dim << "d<Q_1d>(data, W, r_t_" << i << ");\n"; 426241a4b83SYohann break; // No action 427241a4b83SYohann case CEED_EVAL_DIV: 428241a4b83SYohann break; // TODO: Not implemented 429241a4b83SYohann case CEED_EVAL_CURL: 430241a4b83SYohann break; // TODO: Not implemented 431241a4b83SYohann } 432241a4b83SYohann } 433ac421f39SYohann 434ea61e9acSJeremy L Thompson // TODO: put in a function + separate collograd logic 435241a4b83SYohann // Q function 436818e0025SJeremy L Thompson code << "\n // -- Output field setup --\n"; 4379e201c85SYohann for (CeedInt i = 0; i < num_output_fields; i++) { 438818e0025SJeremy L Thompson code << "\n // ---- Output field " << i << " ----\n"; 4392b730f8bSJeremy L Thompson CeedCallBackend(CeedQFunctionFieldGetEvalMode(qf_output_fields[i], &eval_mode)); 4402b730f8bSJeremy L Thompson if (eval_mode == CEED_EVAL_GRAD) { 4419e201c85SYohann if (use_collograd_parallelization) { 442ac421f39SYohann // Accumulator for gradient slices 4439e201c85SYohann code << " CeedScalar r_tt_" << i << "[num_comp_out_" << i << "*Q_1d];\n"; 4449e201c85SYohann code << " for (CeedInt i = 0; i < num_comp_out_" << i << "; i++) {\n"; 4459e201c85SYohann code << " for (CeedInt j = 0; j < Q_1d; ++j) {\n"; 4469e201c85SYohann code << " r_tt_" << i << "[j + i*Q_1d] = 0.0;\n"; 447ac421f39SYohann code << " }\n"; 448ac421f39SYohann code << " }\n"; 449ac421f39SYohann } else { 4509e201c85SYohann code << " CeedScalar r_tt_" << i << "[num_comp_out_" << i << "*dim*Q_1d];\n"; 451241a4b83SYohann } 452ac421f39SYohann } 4532b730f8bSJeremy L Thompson if (eval_mode == CEED_EVAL_NONE || eval_mode == CEED_EVAL_INTERP) { 4549e201c85SYohann code << " CeedScalar r_tt_" << i << "[num_comp_out_" << i << "*Q_1d];\n"; 455241a4b83SYohann } 456241a4b83SYohann } 457ac421f39SYohann // We treat quadrature points per slice in 3d to save registers 4589e201c85SYohann if (use_collograd_parallelization) { 4599e201c85SYohann code << "\n // Note: Using planes of 3D elements\n"; 460ac421f39SYohann code << "#pragma unroll\n"; 4619e201c85SYohann code << " for (CeedInt q = 0; q < Q_1d; q++) {\n"; 462818e0025SJeremy L Thompson code << " // -- Input fields --\n"; 4639e201c85SYohann for (CeedInt i = 0; i < num_input_fields; i++) { 464818e0025SJeremy L Thompson code << " // ---- Input field " << i << " ----\n"; 4659e201c85SYohann // Get elem_size, eval_mode, num_comp 4662b730f8bSJeremy L Thompson CeedCallBackend(CeedQFunctionFieldGetEvalMode(qf_input_fields[i], &eval_mode)); 467ac421f39SYohann // Basis action 4689e201c85SYohann code << " // EvalMode: " << CeedEvalModes[eval_mode] << "\n"; 4699e201c85SYohann switch (eval_mode) { 470ac421f39SYohann case CEED_EVAL_NONE: 4719e201c85SYohann code << " CeedScalar r_q_" << i << "[num_comp_in_" << i << "];\n"; 4729a2291e3SJeremy L Thompson 4739e201c85SYohann bool is_strided; 4742b730f8bSJeremy L Thompson CeedCallBackend(CeedOperatorFieldGetElemRestriction(op_input_fields[i], &Erestrict)); 4752b730f8bSJeremy L Thompson CeedCallBackend(CeedElemRestrictionIsStrided(Erestrict, &is_strided)); 4769e201c85SYohann if (!is_strided) { 4772b730f8bSJeremy L Thompson CeedCallBackend(CeedElemRestrictionGetLVectorSize(Erestrict, &lsize)); 4785c7b696cSjeremylt code << " const CeedInt lsize_in_" << i << " = " << lsize << ";\n"; 4799e201c85SYohann CeedInt comp_stride; 4802b730f8bSJeremy L Thompson CeedCallBackend(CeedElemRestrictionGetCompStride(Erestrict, &comp_stride)); 4819e201c85SYohann code << " // CompStride: " << comp_stride << "\n"; 4822b730f8bSJeremy L Thompson CeedCallBackend(CeedElemRestrictionGetData(Erestrict, &restr_data)); 4839e201c85SYohann data->indices.inputs[i] = restr_data->d_ind; 4842b730f8bSJeremy L Thompson code << " readSliceQuadsOffset" 4852b730f8bSJeremy L Thompson << "3d<num_comp_in_" << i << ", " << comp_stride << ", Q_1d>(data, lsize_in_" << i << ", elem, q, indices.inputs[" << i << "], d_u_" 4862b730f8bSJeremy L Thompson << i << ", r_q_" << i << ");\n"; 487920dcdc4Sjeremylt } else { 4882b730f8bSJeremy L Thompson CeedCallBackend(CeedElemRestrictionGetElementSize(Erestrict, &elem_size)); 489920dcdc4Sjeremylt bool backendstrides; 4902b730f8bSJeremy L Thompson CeedCallBackend(CeedElemRestrictionHasBackendStrides(Erestrict, &backendstrides)); 4919e201c85SYohann CeedInt num_elem; 4922b730f8bSJeremy L Thompson CeedCallBackend(CeedElemRestrictionGetNumElements(Erestrict, &num_elem)); 4939e201c85SYohann CeedInt strides[3] = {1, elem_size * num_elem, elem_size}; 494920dcdc4Sjeremylt if (!backendstrides) { 4952b730f8bSJeremy L Thompson CeedCallBackend(CeedElemRestrictionGetStrides(Erestrict, &strides)); 496920dcdc4Sjeremylt } 497920dcdc4Sjeremylt code << " // Strides: {" << strides[0] << ", " << strides[1] << ", " << strides[2] << "}\n"; 4982b730f8bSJeremy L Thompson code << " readSliceQuadsStrided" 4992b730f8bSJeremy L Thompson << "3d<num_comp_in_" << i 5002b730f8bSJeremy L Thompson << ",Q_1d" 5012b730f8bSJeremy L Thompson "," 5022b730f8bSJeremy L Thompson << strides[0] << "," << strides[1] << "," << strides[2] << ">(data, elem, q, d_u_" << i << ", r_q_" << i << ");\n"; 503920dcdc4Sjeremylt } 504ac421f39SYohann break; 505ac421f39SYohann case CEED_EVAL_INTERP: 5069e201c85SYohann code << " CeedScalar r_q_" << i << "[num_comp_in_" << i << "];\n"; 5079e201c85SYohann code << " for (CeedInt j = 0; j < num_comp_in_" << i << " ; ++j) {\n"; 5089e201c85SYohann code << " r_q_" << i << "[j] = r_t_" << i << "[q + j*Q_1d];\n"; 509ac421f39SYohann code << " }\n"; 510ac421f39SYohann break; 511ac421f39SYohann case CEED_EVAL_GRAD: 5129e201c85SYohann code << " CeedScalar r_q_" << i << "[num_comp_in_" << i << "*dim];\n"; 5139e201c85SYohann code << " gradCollo3d<num_comp_in_" << i << ",Q_1d>(data, q, r_t_" << i << ", s_G_in_" << i << ", r_q_" << i << ");\n"; 514ac421f39SYohann break; 515ac421f39SYohann case CEED_EVAL_WEIGHT: 5169e201c85SYohann code << " CeedScalar r_q_" << i << "[1];\n"; 5179e201c85SYohann code << " r_q_" << i << "[0] = r_t_" << i << "[q];\n"; 518ac421f39SYohann break; // No action 519ac421f39SYohann case CEED_EVAL_DIV: 520ac421f39SYohann break; // TODO: Not implemented 521ac421f39SYohann case CEED_EVAL_CURL: 522ac421f39SYohann break; // TODO: Not implemented 523ac421f39SYohann } 524ac421f39SYohann } 525818e0025SJeremy L Thompson code << "\n // -- Output fields --\n"; 5269e201c85SYohann for (CeedInt i = 0; i < num_output_fields; i++) { 527818e0025SJeremy L Thompson code << " // ---- Output field " << i << " ----\n"; 5282b730f8bSJeremy L Thompson CeedCallBackend(CeedQFunctionFieldGetEvalMode(qf_output_fields[i], &eval_mode)); 529ac421f39SYohann // Basis action 5309e201c85SYohann switch (eval_mode) { 531ac421f39SYohann case CEED_EVAL_NONE: 5329e201c85SYohann code << " CeedScalar r_qq_" << i << "[num_comp_out_" << i << "];\n"; 533ac421f39SYohann break; // No action 534ac421f39SYohann case CEED_EVAL_INTERP: 5359e201c85SYohann code << " CeedScalar r_qq_" << i << "[num_comp_out_" << i << "];\n"; 536ac421f39SYohann break; 537ac421f39SYohann case CEED_EVAL_GRAD: 5389e201c85SYohann code << " CeedScalar r_qq_" << i << "[num_comp_out_" << i << "*dim];\n"; 539ac421f39SYohann break; 540ac421f39SYohann case CEED_EVAL_WEIGHT: 541ac421f39SYohann break; // Should not occur 542ac421f39SYohann case CEED_EVAL_DIV: 543ac421f39SYohann break; // TODO: Not implemented 544ac421f39SYohann case CEED_EVAL_CURL: 545ac421f39SYohann break; // TODO: Not implemented 546ac421f39SYohann } 547ac421f39SYohann } 548ac421f39SYohann } else { 5499e201c85SYohann code << "\n // Note: Using full elements\n"; 550818e0025SJeremy L Thompson code << " // -- Input fields --\n"; 5519e201c85SYohann for (CeedInt i = 0; i < num_input_fields; i++) { 552818e0025SJeremy L Thompson code << " // ---- Input field " << i << " ----\n"; 5539e201c85SYohann code << " CeedScalar* r_q_" << i << " = r_t_" << i << ";\n"; 554ac421f39SYohann } 555818e0025SJeremy L Thompson code << " // -- Output fields --\n"; 5569e201c85SYohann for (CeedInt i = 0; i < num_output_fields; i++) { 557818e0025SJeremy L Thompson code << " // ---- Output field " << i << " ----\n"; 5589e201c85SYohann code << " CeedScalar* r_qq_" << i << " = r_tt_" << i << ";\n"; 559ac421f39SYohann } 560ac421f39SYohann } 561818e0025SJeremy L Thompson code << "\n // -- QFunction Inputs and outputs --\n"; 5629e201c85SYohann code << " CeedScalar* in[" << num_input_fields << "];\n"; 5639e201c85SYohann for (CeedInt i = 0; i < num_input_fields; i++) { 564818e0025SJeremy L Thompson code << " // ---- Input field " << i << " ----\n"; 5659e201c85SYohann code << " in[" << i << "] = r_q_" << i << ";\n"; 5664d537eeaSYohann } 5679e201c85SYohann code << " CeedScalar* out[" << num_output_fields << "];\n"; 5689e201c85SYohann for (CeedInt i = 0; i < num_output_fields; i++) { 569818e0025SJeremy L Thompson code << " // ---- Output field " << i << " ----\n"; 5709e201c85SYohann code << " out[" << i << "] = r_qq_" << i << ";\n"; 5714d537eeaSYohann } 572818e0025SJeremy L Thompson code << "\n // -- Apply QFunction --\n"; 5739e201c85SYohann code << " " << q_function_name << "(ctx, "; 5749e201c85SYohann if (dim != 3 || use_collograd_parallelization) { 575ac421f39SYohann code << "1"; 576ac421f39SYohann } else { 5779e201c85SYohann code << "Q_1d"; 578ac421f39SYohann } 579ac421f39SYohann code << ", in, out);\n"; 5809e201c85SYohann if (use_collograd_parallelization) { 581818e0025SJeremy L Thompson code << " // -- Output fields --\n"; 5829e201c85SYohann for (CeedInt i = 0; i < num_output_fields; i++) { 583818e0025SJeremy L Thompson code << " // ---- Output field " << i << " ----\n"; 5842b730f8bSJeremy L Thompson CeedCallBackend(CeedQFunctionFieldGetEvalMode(qf_output_fields[i], &eval_mode)); 585ac421f39SYohann // Basis action 5869e201c85SYohann code << " // EvalMode: " << CeedEvalModes[eval_mode] << "\n"; 5879e201c85SYohann switch (eval_mode) { 588ac421f39SYohann case CEED_EVAL_NONE: 5899e201c85SYohann code << " for (CeedInt j = 0; j < num_comp_out_" << i << " ; ++j) {\n"; 5909e201c85SYohann code << " r_tt_" << i << "[q + j*Q_1d] = r_qq_" << i << "[j];\n"; 591ac421f39SYohann code << " }\n"; 592ac421f39SYohann break; // No action 593ac421f39SYohann case CEED_EVAL_INTERP: 5949e201c85SYohann code << " for (CeedInt j = 0; j < num_comp_out_" << i << " ; ++j) {\n"; 5959e201c85SYohann code << " r_tt_" << i << "[q + j*Q_1d] = r_qq_" << i << "[j];\n"; 596ac421f39SYohann code << " }\n"; 597ac421f39SYohann break; 598ac421f39SYohann case CEED_EVAL_GRAD: 5999e201c85SYohann code << " gradColloTranspose3d<num_comp_out_" << i << ",Q_1d>(data, q, r_qq_" << i << ", s_G_out_" << i << ", r_tt_" << i << ");\n"; 600ac421f39SYohann break; 601ac421f39SYohann case CEED_EVAL_WEIGHT: 602ac421f39SYohann break; // Should not occur 603ac421f39SYohann case CEED_EVAL_DIV: 604ac421f39SYohann break; // TODO: Not implemented 605ac421f39SYohann case CEED_EVAL_CURL: 606ac421f39SYohann break; // TODO: Not implemented 607ac421f39SYohann } 608ac421f39SYohann } 609ac421f39SYohann code << " }\n"; 610ac421f39SYohann } 611241a4b83SYohann 612241a4b83SYohann // Output basis apply if needed 613ac421f39SYohann // Generate the correct eval mode code for each output 614818e0025SJeremy L Thompson code << "\n // -- Output field basis action and restrictions --\n"; 6159e201c85SYohann for (CeedInt i = 0; i < num_output_fields; i++) { 616818e0025SJeremy L Thompson code << " // ---- Output field " << i << " ----\n"; 6179e201c85SYohann // Get elem_size, eval_mode, num_comp 6182b730f8bSJeremy L Thompson CeedCallBackend(CeedOperatorFieldGetElemRestriction(op_output_fields[i], &Erestrict)); 6192b730f8bSJeremy L Thompson CeedCallBackend(CeedElemRestrictionGetElementSize(Erestrict, &elem_size)); 6202b730f8bSJeremy L Thompson CeedCallBackend(CeedQFunctionFieldGetEvalMode(qf_output_fields[i], &eval_mode)); 6212b730f8bSJeremy L Thompson CeedCallBackend(CeedElemRestrictionGetNumComponents(Erestrict, &num_comp)); 6229e201c85SYohann // TODO put in a function 623241a4b83SYohann // Basis action 6249e201c85SYohann code << " // EvalMode: " << CeedEvalModes[eval_mode] << "\n"; 6259e201c85SYohann switch (eval_mode) { 626241a4b83SYohann case CEED_EVAL_NONE: 6279e201c85SYohann code << " CeedScalar* r_v_" << i << " = r_tt_" << i << ";\n"; 628241a4b83SYohann break; // No action 629241a4b83SYohann case CEED_EVAL_INTERP: 6309e201c85SYohann code << " CeedScalar r_v_" << i << "[num_comp_out_" << i << "*P_out_" << i << "];\n"; 6312b730f8bSJeremy L Thompson code << " InterpTranspose" << (dim > 1 ? "Tensor" : "") << dim << "d<num_comp_out_" << i << ",P_out_" << i << ",Q_1d>(data, r_tt_" << i 6322b730f8bSJeremy L Thompson << ", s_B_out_" << i << ", r_v_" << i << ");\n"; 633241a4b83SYohann break; 634241a4b83SYohann case CEED_EVAL_GRAD: 6359e201c85SYohann code << " CeedScalar r_v_" << i << "[num_comp_out_" << i << "*P_out_" << i << "];\n"; 6369e201c85SYohann if (use_collograd_parallelization) { 6372b730f8bSJeremy L Thompson code << " InterpTranspose" << (dim > 1 ? "Tensor" : "") << dim << "d<num_comp_out_" << i << ",P_out_" << i << ",Q_1d>(data, r_tt_" << i 6382b730f8bSJeremy L Thompson << ", s_B_out_" << i << ", r_v_" << i << ");\n"; 639ac421f39SYohann } else { 6409e201c85SYohann CeedInt P_1d; 6412b730f8bSJeremy L Thompson CeedCallBackend(CeedOperatorFieldGetBasis(op_output_fields[i], &basis)); 6422b730f8bSJeremy L Thompson CeedCallBackend(CeedBasisGetNumNodes1D(basis, &P_1d)); 6432b730f8bSJeremy L Thompson code << " GradTranspose" << (dim > 1 ? "Tensor" : "") << (dim == 3 && Q_1d >= P_1d ? "Collocated" : "") << dim << "d<num_comp_out_" << i 6442b730f8bSJeremy L Thompson << ",P_out_" << i << ",Q_1d>(data, r_tt_" << i << ", s_B_out_" << i << ", s_G_out_" << i << ", r_v_" << i << ");\n"; 645ac421f39SYohann } 646241a4b83SYohann break; 647e9f4dca0SJeremy L Thompson // LCOV_EXCL_START 648241a4b83SYohann case CEED_EVAL_WEIGHT: { 649241a4b83SYohann Ceed ceed; 6502b730f8bSJeremy L Thompson CeedCallBackend(CeedOperatorGetCeed(op, &ceed)); 6512b730f8bSJeremy L Thompson return CeedError(ceed, CEED_ERROR_BACKEND, "CEED_EVAL_WEIGHT cannot be an output evaluation mode"); 652241a4b83SYohann break; // Should not occur 653241a4b83SYohann } 654241a4b83SYohann case CEED_EVAL_DIV: 655241a4b83SYohann break; // TODO: Not implemented 656241a4b83SYohann case CEED_EVAL_CURL: 657241a4b83SYohann break; // TODO: Not implemented 658e9f4dca0SJeremy L Thompson // LCOV_EXCL_STOP 659241a4b83SYohann } 6609e201c85SYohann // TODO put in a function 661920dcdc4Sjeremylt // Restriction 6629e201c85SYohann bool is_strided; 6632b730f8bSJeremy L Thompson CeedCallBackend(CeedElemRestrictionIsStrided(Erestrict, &is_strided)); 6649e201c85SYohann if (!is_strided) { 6652b730f8bSJeremy L Thompson CeedCallBackend(CeedElemRestrictionGetLVectorSize(Erestrict, &lsize)); 6665c7b696cSjeremylt code << " const CeedInt lsize_out_" << i << " = " << lsize << ";\n"; 6679e201c85SYohann CeedInt comp_stride; 6682b730f8bSJeremy L Thompson CeedCallBackend(CeedElemRestrictionGetCompStride(Erestrict, &comp_stride)); 6699e201c85SYohann code << " // CompStride: " << comp_stride << "\n"; 6702b730f8bSJeremy L Thompson CeedCallBackend(CeedElemRestrictionGetData(Erestrict, &restr_data)); 6719e201c85SYohann data->indices.outputs[i] = restr_data->d_ind; 6722b730f8bSJeremy L Thompson code << " writeDofsOffset" << dim << "d<num_comp_out_" << i << ", " << comp_stride << ", P_out_" << i << ">(data, lsize_out_" << i 6732b730f8bSJeremy L Thompson << ", elem, indices.outputs[" << i << "], r_v_" << i << ", d_v_" << i << ");\n"; 674920dcdc4Sjeremylt } else { 6759e201c85SYohann bool has_backend_strides; 6762b730f8bSJeremy L Thompson CeedCallBackend(CeedElemRestrictionHasBackendStrides(Erestrict, &has_backend_strides)); 6779e201c85SYohann CeedInt num_elem; 6782b730f8bSJeremy L Thompson CeedCallBackend(CeedElemRestrictionGetNumElements(Erestrict, &num_elem)); 6799e201c85SYohann CeedInt strides[3] = {1, elem_size * num_elem, elem_size}; 6809e201c85SYohann if (!has_backend_strides) { 6812b730f8bSJeremy L Thompson CeedCallBackend(CeedElemRestrictionGetStrides(Erestrict, &strides)); 682920dcdc4Sjeremylt } 683920dcdc4Sjeremylt code << " // Strides: {" << strides[0] << ", " << strides[1] << ", " << strides[2] << "}\n"; 6842b730f8bSJeremy L Thompson code << " writeDofsStrided" << dim << "d<num_comp_out_" << i << ",P_out_" << i << "," << strides[0] << "," << strides[1] << "," << strides[2] 6852b730f8bSJeremy L Thompson << ">(data, elem, r_v_" << i << ", d_v_" << i << ");\n"; 686920dcdc4Sjeremylt } 687241a4b83SYohann } 688241a4b83SYohann 689241a4b83SYohann code << " }\n"; 690818e0025SJeremy L Thompson code << "}\n"; 691818e0025SJeremy L Thompson code << "// -----------------------------------------------------------------------------\n\n"; 692241a4b83SYohann 693ab213215SJeremy L Thompson // View kernel for debugging 69446dc0734SJeremy L Thompson CeedDebug256(ceed, 2, "Generated Operator Kernels:\n"); 6953f21f6b1SJeremy L Thompson CeedDebug(ceed, code.str().c_str()); 696241a4b83SYohann 6972b730f8bSJeremy L Thompson CeedCallBackend(CeedCompileCuda(ceed, code.str().c_str(), &data->module, 1, "T_1D", CeedIntMax(Q_1d, data->max_P_1d))); 6982b730f8bSJeremy L Thompson CeedCallBackend(CeedGetKernelCuda(ceed, data->module, operator_name.c_str(), &data->op)); 699241a4b83SYohann 7002b730f8bSJeremy L Thompson CeedCallBackend(CeedOperatorSetSetupDone(op)); 701e15f9bd0SJeremy L Thompson return CEED_ERROR_SUCCESS; 702241a4b83SYohann } 703*2a86cc9dSSebastian Grimberg 704ab213215SJeremy L Thompson //------------------------------------------------------------------------------ 705