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. 30d0321e0SJeremy L Thompson // 43d8e8822SJeremy L Thompson // SPDX-License-Identifier: BSD-2-Clause 50d0321e0SJeremy L Thompson // 63d8e8822SJeremy L Thompson // This file is part of CEED: http://github.com/ceed 70d0321e0SJeremy L Thompson 849aac155SJeremy L Thompson #include <ceed.h> 90d0321e0SJeremy L Thompson #include <ceed/backend.h> 10437930d1SJeremy L Thompson #include <ceed/jit-tools.h> 110d0321e0SJeremy L Thompson #include <cuda.h> 120d0321e0SJeremy L Thompson #include <cuda_runtime.h> 13*111870feSJeremy L Thompson #include <string.h> 142b730f8bSJeremy L Thompson 1549aac155SJeremy L Thompson #include "../cuda/ceed-cuda-common.h" 160d0321e0SJeremy L Thompson #include "../cuda/ceed-cuda-compile.h" 172b730f8bSJeremy L Thompson #include "ceed-cuda-ref.h" 180d0321e0SJeremy L Thompson 190d0321e0SJeremy L Thompson //------------------------------------------------------------------------------ 200d0321e0SJeremy L Thompson // Basis apply - tensor 210d0321e0SJeremy L Thompson //------------------------------------------------------------------------------ 22db2becc9SJeremy L Thompson static int CeedBasisApplyCore_Cuda(CeedBasis basis, bool apply_add, const CeedInt num_elem, CeedTransposeMode t_mode, CeedEvalMode eval_mode, 23db2becc9SJeremy L Thompson CeedVector u, CeedVector v) { 240d0321e0SJeremy L Thompson Ceed ceed; 25ca735530SJeremy L Thompson CeedInt Q_1d, dim; 267bbbfca3SJeremy L Thompson const CeedInt is_transpose = t_mode == CEED_TRANSPOSE; 27437930d1SJeremy L Thompson const int max_block_size = 32; 280d0321e0SJeremy L Thompson const CeedScalar *d_u; 290d0321e0SJeremy L Thompson CeedScalar *d_v; 30ca735530SJeremy L Thompson CeedBasis_Cuda *data; 31ca735530SJeremy L Thompson 32ca735530SJeremy L Thompson CeedCallBackend(CeedBasisGetCeed(basis, &ceed)); 33ca735530SJeremy L Thompson CeedCallBackend(CeedBasisGetData(basis, &data)); 34ca735530SJeremy L Thompson 359ea2cfd9SJeremy L Thompson // Get read/write access to u, v 366574a04fSJeremy L Thompson if (u != CEED_VECTOR_NONE) CeedCallBackend(CeedVectorGetArrayRead(u, CEED_MEM_DEVICE, &d_u)); 376574a04fSJeremy L Thompson else CeedCheck(eval_mode == CEED_EVAL_WEIGHT, ceed, CEED_ERROR_BACKEND, "An input vector is required for this CeedEvalMode"); 38db2becc9SJeremy L Thompson if (apply_add) CeedCallBackend(CeedVectorGetArray(v, CEED_MEM_DEVICE, &d_v)); 39db2becc9SJeremy L Thompson else CeedCallBackend(CeedVectorGetArrayWrite(v, CEED_MEM_DEVICE, &d_v)); 400d0321e0SJeremy L Thompson 410d0321e0SJeremy L Thompson // Clear v for transpose operation 42db2becc9SJeremy L Thompson if (is_transpose && !apply_add) { 431f9221feSJeremy L Thompson CeedSize length; 44ca735530SJeremy L Thompson 452b730f8bSJeremy L Thompson CeedCallBackend(CeedVectorGetLength(v, &length)); 462b730f8bSJeremy L Thompson CeedCallCuda(ceed, cudaMemset(d_v, 0, length * sizeof(CeedScalar))); 470d0321e0SJeremy L Thompson } 482b730f8bSJeremy L Thompson CeedCallBackend(CeedBasisGetNumQuadraturePoints1D(basis, &Q_1d)); 492b730f8bSJeremy L Thompson CeedCallBackend(CeedBasisGetDimension(basis, &dim)); 500d0321e0SJeremy L Thompson 510d0321e0SJeremy L Thompson // Basis action 52437930d1SJeremy L Thompson switch (eval_mode) { 530d0321e0SJeremy L Thompson case CEED_EVAL_INTERP: { 547bbbfca3SJeremy L Thompson void *interp_args[] = {(void *)&num_elem, (void *)&is_transpose, &data->d_interp_1d, &d_u, &d_v}; 55b2165e7aSSebastian Grimberg const CeedInt block_size = CeedIntMin(CeedIntPow(Q_1d, dim), max_block_size); 560d0321e0SJeremy L Thompson 57eb7e6cafSJeremy L Thompson CeedCallBackend(CeedRunKernel_Cuda(ceed, data->Interp, num_elem, block_size, interp_args)); 580d0321e0SJeremy L Thompson } break; 590d0321e0SJeremy L Thompson case CEED_EVAL_GRAD: { 607bbbfca3SJeremy L Thompson void *grad_args[] = {(void *)&num_elem, (void *)&is_transpose, &data->d_interp_1d, &data->d_grad_1d, &d_u, &d_v}; 61b2165e7aSSebastian Grimberg const CeedInt block_size = max_block_size; 620d0321e0SJeremy L Thompson 63eb7e6cafSJeremy L Thompson CeedCallBackend(CeedRunKernel_Cuda(ceed, data->Grad, num_elem, block_size, grad_args)); 640d0321e0SJeremy L Thompson } break; 650d0321e0SJeremy L Thompson case CEED_EVAL_WEIGHT: { 66097cc795SJames Wright CeedCheck(data->d_q_weight_1d, ceed, CEED_ERROR_BACKEND, "%s not supported; q_weights_1d not set", CeedEvalModes[eval_mode]); 67437930d1SJeremy L Thompson void *weight_args[] = {(void *)&num_elem, (void *)&data->d_q_weight_1d, &d_v}; 68b2165e7aSSebastian Grimberg const int block_size_x = Q_1d; 69b2165e7aSSebastian Grimberg const int block_size_y = dim >= 2 ? Q_1d : 1; 70b2165e7aSSebastian Grimberg 71b2165e7aSSebastian Grimberg CeedCallBackend(CeedRunKernelDim_Cuda(ceed, data->Weight, num_elem, block_size_x, block_size_y, 1, weight_args)); 720d0321e0SJeremy L Thompson } break; 739ea2cfd9SJeremy L Thompson case CEED_EVAL_NONE: /* handled separately below */ 749ea2cfd9SJeremy L Thompson break; 750d0321e0SJeremy L Thompson // LCOV_EXCL_START 760d0321e0SJeremy L Thompson case CEED_EVAL_DIV: 770d0321e0SJeremy L Thompson case CEED_EVAL_CURL: 78bcbe1c99SJeremy L Thompson return CeedError(ceed, CEED_ERROR_BACKEND, "%s not supported", CeedEvalModes[eval_mode]); 790d0321e0SJeremy L Thompson // LCOV_EXCL_STOP 800d0321e0SJeremy L Thompson } 810d0321e0SJeremy L Thompson 829ea2cfd9SJeremy L Thompson // Restore vectors, cover CEED_EVAL_NONE 832b730f8bSJeremy L Thompson CeedCallBackend(CeedVectorRestoreArray(v, &d_v)); 849ea2cfd9SJeremy L Thompson if (eval_mode == CEED_EVAL_NONE) CeedCallBackend(CeedVectorSetArray(v, CEED_MEM_DEVICE, CEED_COPY_VALUES, (CeedScalar *)d_u)); 859ea2cfd9SJeremy L Thompson if (eval_mode != CEED_EVAL_WEIGHT) CeedCallBackend(CeedVectorRestoreArrayRead(u, &d_u)); 860d0321e0SJeremy L Thompson return CEED_ERROR_SUCCESS; 870d0321e0SJeremy L Thompson } 880d0321e0SJeremy L Thompson 89db2becc9SJeremy L Thompson static int CeedBasisApply_Cuda(CeedBasis basis, const CeedInt num_elem, CeedTransposeMode t_mode, CeedEvalMode eval_mode, CeedVector u, 90db2becc9SJeremy L Thompson CeedVector v) { 91db2becc9SJeremy L Thompson CeedCallBackend(CeedBasisApplyCore_Cuda(basis, false, num_elem, t_mode, eval_mode, u, v)); 92db2becc9SJeremy L Thompson return CEED_ERROR_SUCCESS; 93db2becc9SJeremy L Thompson } 94db2becc9SJeremy L Thompson 95db2becc9SJeremy L Thompson static int CeedBasisApplyAdd_Cuda(CeedBasis basis, const CeedInt num_elem, CeedTransposeMode t_mode, CeedEvalMode eval_mode, CeedVector u, 96db2becc9SJeremy L Thompson CeedVector v) { 97db2becc9SJeremy L Thompson CeedCallBackend(CeedBasisApplyCore_Cuda(basis, true, num_elem, t_mode, eval_mode, u, v)); 98db2becc9SJeremy L Thompson return CEED_ERROR_SUCCESS; 99db2becc9SJeremy L Thompson } 100db2becc9SJeremy L Thompson 1010d0321e0SJeremy L Thompson //------------------------------------------------------------------------------ 10234d14614SJeremy L Thompson // Basis apply - tensor AtPoints 10334d14614SJeremy L Thompson //------------------------------------------------------------------------------ 104db2becc9SJeremy L Thompson static int CeedBasisApplyAtPointsCore_Cuda(CeedBasis basis, bool apply_add, const CeedInt num_elem, const CeedInt *num_points, 105db2becc9SJeremy L Thompson CeedTransposeMode t_mode, CeedEvalMode eval_mode, CeedVector x_ref, CeedVector u, CeedVector v) { 10634d14614SJeremy L Thompson Ceed ceed; 10734d14614SJeremy L Thompson CeedInt Q_1d, dim, max_num_points = num_points[0]; 10834d14614SJeremy L Thompson const CeedInt is_transpose = t_mode == CEED_TRANSPOSE; 10934d14614SJeremy L Thompson const int max_block_size = 32; 11034d14614SJeremy L Thompson const CeedScalar *d_x, *d_u; 11134d14614SJeremy L Thompson CeedScalar *d_v; 11234d14614SJeremy L Thompson CeedBasis_Cuda *data; 11334d14614SJeremy L Thompson 11434d14614SJeremy L Thompson CeedCallBackend(CeedBasisGetCeed(basis, &ceed)); 11534d14614SJeremy L Thompson CeedCallBackend(CeedBasisGetData(basis, &data)); 11634d14614SJeremy L Thompson CeedCallBackend(CeedBasisGetNumQuadraturePoints1D(basis, &Q_1d)); 11734d14614SJeremy L Thompson CeedCallBackend(CeedBasisGetDimension(basis, &dim)); 11834d14614SJeremy L Thompson 11934d14614SJeremy L Thompson // Weight handled separately 12034d14614SJeremy L Thompson if (eval_mode == CEED_EVAL_WEIGHT) { 1215a5594ffSJeremy L Thompson CeedCallBackend(CeedVectorSetValue(v, 1.0)); 12234d14614SJeremy L Thompson return CEED_ERROR_SUCCESS; 12334d14614SJeremy L Thompson } 12434d14614SJeremy L Thompson 125*111870feSJeremy L Thompson // Check padded to uniform number of points per elem 126*111870feSJeremy L Thompson for (CeedInt i = 1; i < num_elem; i++) max_num_points = CeedIntMax(max_num_points, num_points[i]); 127*111870feSJeremy L Thompson { 128*111870feSJeremy L Thompson CeedInt num_comp, q_comp; 129*111870feSJeremy L Thompson CeedSize len, len_required; 130*111870feSJeremy L Thompson 131*111870feSJeremy L Thompson CeedCallBackend(CeedBasisGetNumComponents(basis, &num_comp)); 132*111870feSJeremy L Thompson CeedCallBackend(CeedBasisGetNumQuadratureComponents(basis, eval_mode, &q_comp)); 133*111870feSJeremy L Thompson CeedCallBackend(CeedVectorGetLength(is_transpose ? u : v, &len)); 134*111870feSJeremy L Thompson len_required = (CeedSize)num_comp * (CeedSize)q_comp * (CeedSize)num_elem * (CeedSize)max_num_points; 135*111870feSJeremy L Thompson CeedCheck(len >= len_required, ceed, CEED_ERROR_BACKEND, 136*111870feSJeremy L Thompson "Vector at points must be padded to the same number of points in each element for BasisApplyAtPoints on GPU backends." 137*111870feSJeremy L Thompson " Found %" CeedSize_FMT ", Required %" CeedSize_FMT, 138*111870feSJeremy L Thompson len, len_required); 139*111870feSJeremy L Thompson } 140*111870feSJeremy L Thompson 141*111870feSJeremy L Thompson // Move num_points array to device 142*111870feSJeremy L Thompson if (is_transpose) { 143*111870feSJeremy L Thompson const CeedInt num_bytes = num_elem * sizeof(CeedInt); 144*111870feSJeremy L Thompson 145*111870feSJeremy L Thompson if (num_elem != data->num_elem_at_points) { 146*111870feSJeremy L Thompson data->num_elem_at_points = num_elem; 147*111870feSJeremy L Thompson 148*111870feSJeremy L Thompson if (data->d_points_per_elem) CeedCallCuda(ceed, cudaFree(data->d_points_per_elem)); 149*111870feSJeremy L Thompson CeedCallCuda(ceed, cudaMalloc((void **)&data->d_points_per_elem, num_bytes)); 150*111870feSJeremy L Thompson CeedCallBackend(CeedFree(&data->h_points_per_elem)); 151*111870feSJeremy L Thompson CeedCallBackend(CeedCalloc(num_elem, &data->h_points_per_elem)); 152*111870feSJeremy L Thompson } 153*111870feSJeremy L Thompson if (!memcmp(data->h_points_per_elem, num_points, num_bytes)) { 154*111870feSJeremy L Thompson memcpy(data->h_points_per_elem, num_points, num_bytes); 155*111870feSJeremy L Thompson CeedCallCuda(ceed, cudaMemcpy(data->d_points_per_elem, num_points, num_bytes, cudaMemcpyHostToDevice)); 156*111870feSJeremy L Thompson } 157*111870feSJeremy L Thompson } 158*111870feSJeremy L Thompson 15934d14614SJeremy L Thompson // Build kernels if needed 16034d14614SJeremy L Thompson if (data->num_points != max_num_points) { 16134d14614SJeremy L Thompson CeedInt P_1d; 16234d14614SJeremy L Thompson 16334d14614SJeremy L Thompson CeedCallBackend(CeedBasisGetNumNodes1D(basis, &P_1d)); 16434d14614SJeremy L Thompson data->num_points = max_num_points; 16534d14614SJeremy L Thompson 16634d14614SJeremy L Thompson // -- Create interp matrix to Chebyshev coefficients 16734d14614SJeremy L Thompson if (!data->d_chebyshev_interp_1d) { 16834d14614SJeremy L Thompson CeedSize interp_bytes; 16934d14614SJeremy L Thompson CeedScalar *chebyshev_interp_1d; 17034d14614SJeremy L Thompson 17134d14614SJeremy L Thompson interp_bytes = P_1d * Q_1d * sizeof(CeedScalar); 17234d14614SJeremy L Thompson CeedCallBackend(CeedCalloc(P_1d * Q_1d, &chebyshev_interp_1d)); 1735a5594ffSJeremy L Thompson CeedCallBackend(CeedBasisGetChebyshevInterp1D(basis, chebyshev_interp_1d)); 17434d14614SJeremy L Thompson CeedCallCuda(ceed, cudaMalloc((void **)&data->d_chebyshev_interp_1d, interp_bytes)); 17534d14614SJeremy L Thompson CeedCallCuda(ceed, cudaMemcpy(data->d_chebyshev_interp_1d, chebyshev_interp_1d, interp_bytes, cudaMemcpyHostToDevice)); 17634d14614SJeremy L Thompson CeedCallBackend(CeedFree(&chebyshev_interp_1d)); 17734d14614SJeremy L Thompson } 17834d14614SJeremy L Thompson 17934d14614SJeremy L Thompson // -- Compile kernels 18034d14614SJeremy L Thompson char *basis_kernel_source; 18134d14614SJeremy L Thompson const char *basis_kernel_path; 18234d14614SJeremy L Thompson CeedInt num_comp; 18334d14614SJeremy L Thompson 18434d14614SJeremy L Thompson if (data->moduleAtPoints) CeedCallCuda(ceed, cuModuleUnload(data->moduleAtPoints)); 18534d14614SJeremy L Thompson CeedCallBackend(CeedBasisGetNumComponents(basis, &num_comp)); 18634d14614SJeremy L Thompson CeedCallBackend(CeedGetJitAbsolutePath(ceed, "ceed/jit-source/cuda/cuda-ref-basis-tensor-at-points.h", &basis_kernel_path)); 18734d14614SJeremy L Thompson CeedDebug256(ceed, CEED_DEBUG_COLOR_SUCCESS, "----- Loading Basis Kernel Source -----\n"); 18834d14614SJeremy L Thompson CeedCallBackend(CeedLoadSourceToBuffer(ceed, basis_kernel_path, &basis_kernel_source)); 18934d14614SJeremy L Thompson CeedDebug256(ceed, CEED_DEBUG_COLOR_SUCCESS, "----- Loading Basis Kernel Source Complete! -----\n"); 19034d14614SJeremy L Thompson CeedCallBackend(CeedCompile_Cuda(ceed, basis_kernel_source, &data->moduleAtPoints, 9, "BASIS_Q_1D", Q_1d, "BASIS_P_1D", P_1d, "BASIS_BUF_LEN", 191f7c9815fSJeremy L Thompson Q_1d * CeedIntPow(Q_1d > P_1d ? Q_1d : P_1d, dim - 1), "BASIS_DIM", dim, "BASIS_NUM_COMP", num_comp, 19234d14614SJeremy L Thompson "BASIS_NUM_NODES", CeedIntPow(P_1d, dim), "BASIS_NUM_QPTS", CeedIntPow(Q_1d, dim), "BASIS_NUM_PTS", 193f7c9815fSJeremy L Thompson max_num_points, "POINTS_BUFF_LEN", CeedIntPow(Q_1d, dim - 1))); 19434d14614SJeremy L Thompson CeedCallBackend(CeedGetKernel_Cuda(ceed, data->moduleAtPoints, "InterpAtPoints", &data->InterpAtPoints)); 19534d14614SJeremy L Thompson CeedCallBackend(CeedGetKernel_Cuda(ceed, data->moduleAtPoints, "GradAtPoints", &data->GradAtPoints)); 19634d14614SJeremy L Thompson CeedCallBackend(CeedFree(&basis_kernel_path)); 19734d14614SJeremy L Thompson CeedCallBackend(CeedFree(&basis_kernel_source)); 19834d14614SJeremy L Thompson } 19934d14614SJeremy L Thompson 20034d14614SJeremy L Thompson // Get read/write access to u, v 20134d14614SJeremy L Thompson CeedCallBackend(CeedVectorGetArrayRead(x_ref, CEED_MEM_DEVICE, &d_x)); 20234d14614SJeremy L Thompson if (u != CEED_VECTOR_NONE) CeedCallBackend(CeedVectorGetArrayRead(u, CEED_MEM_DEVICE, &d_u)); 20334d14614SJeremy L Thompson else CeedCheck(eval_mode == CEED_EVAL_WEIGHT, ceed, CEED_ERROR_BACKEND, "An input vector is required for this CeedEvalMode"); 204db2becc9SJeremy L Thompson if (apply_add) CeedCallBackend(CeedVectorGetArray(v, CEED_MEM_DEVICE, &d_v)); 205db2becc9SJeremy L Thompson else CeedCallBackend(CeedVectorGetArrayWrite(v, CEED_MEM_DEVICE, &d_v)); 20634d14614SJeremy L Thompson 20734d14614SJeremy L Thompson // Clear v for transpose operation 208db2becc9SJeremy L Thompson if (is_transpose && !apply_add) { 20934d14614SJeremy L Thompson CeedSize length; 21034d14614SJeremy L Thompson 21134d14614SJeremy L Thompson CeedCallBackend(CeedVectorGetLength(v, &length)); 21234d14614SJeremy L Thompson CeedCallCuda(ceed, cudaMemset(d_v, 0, length * sizeof(CeedScalar))); 21334d14614SJeremy L Thompson } 21434d14614SJeremy L Thompson 21534d14614SJeremy L Thompson // Basis action 21634d14614SJeremy L Thompson switch (eval_mode) { 21734d14614SJeremy L Thompson case CEED_EVAL_INTERP: { 218*111870feSJeremy L Thompson void *interp_args[] = {(void *)&num_elem, (void *)&is_transpose, &data->d_chebyshev_interp_1d, &data->d_points_per_elem, &d_x, &d_u, &d_v}; 21934d14614SJeremy L Thompson const CeedInt block_size = CeedIntMin(CeedIntPow(Q_1d, dim), max_block_size); 22034d14614SJeremy L Thompson 22134d14614SJeremy L Thompson CeedCallBackend(CeedRunKernel_Cuda(ceed, data->InterpAtPoints, num_elem, block_size, interp_args)); 22234d14614SJeremy L Thompson } break; 22334d14614SJeremy L Thompson case CEED_EVAL_GRAD: { 224*111870feSJeremy L Thompson void *grad_args[] = {(void *)&num_elem, (void *)&is_transpose, &data->d_chebyshev_interp_1d, &data->d_points_per_elem, &d_x, &d_u, &d_v}; 2252d10e82cSJeremy L Thompson const CeedInt block_size = CeedIntMin(CeedIntPow(Q_1d, dim), max_block_size); 22634d14614SJeremy L Thompson 22734d14614SJeremy L Thompson CeedCallBackend(CeedRunKernel_Cuda(ceed, data->GradAtPoints, num_elem, block_size, grad_args)); 22834d14614SJeremy L Thompson } break; 22934d14614SJeremy L Thompson case CEED_EVAL_WEIGHT: 23034d14614SJeremy L Thompson case CEED_EVAL_NONE: /* handled separately below */ 23134d14614SJeremy L Thompson break; 23234d14614SJeremy L Thompson // LCOV_EXCL_START 23334d14614SJeremy L Thompson case CEED_EVAL_DIV: 23434d14614SJeremy L Thompson case CEED_EVAL_CURL: 23534d14614SJeremy L Thompson return CeedError(ceed, CEED_ERROR_BACKEND, "%s not supported", CeedEvalModes[eval_mode]); 23634d14614SJeremy L Thompson // LCOV_EXCL_STOP 23734d14614SJeremy L Thompson } 23834d14614SJeremy L Thompson 23934d14614SJeremy L Thompson // Restore vectors, cover CEED_EVAL_NONE 24034d14614SJeremy L Thompson CeedCallBackend(CeedVectorRestoreArrayRead(x_ref, &d_x)); 24134d14614SJeremy L Thompson CeedCallBackend(CeedVectorRestoreArray(v, &d_v)); 24234d14614SJeremy L Thompson if (eval_mode == CEED_EVAL_NONE) CeedCallBackend(CeedVectorSetArray(v, CEED_MEM_DEVICE, CEED_COPY_VALUES, (CeedScalar *)d_u)); 24334d14614SJeremy L Thompson if (eval_mode != CEED_EVAL_WEIGHT) CeedCallBackend(CeedVectorRestoreArrayRead(u, &d_u)); 24434d14614SJeremy L Thompson return CEED_ERROR_SUCCESS; 24534d14614SJeremy L Thompson } 24634d14614SJeremy L Thompson 247db2becc9SJeremy L Thompson static int CeedBasisApplyAtPoints_Cuda(CeedBasis basis, const CeedInt num_elem, const CeedInt *num_points, CeedTransposeMode t_mode, 248db2becc9SJeremy L Thompson CeedEvalMode eval_mode, CeedVector x_ref, CeedVector u, CeedVector v) { 249db2becc9SJeremy L Thompson CeedCallBackend(CeedBasisApplyAtPointsCore_Cuda(basis, false, num_elem, num_points, t_mode, eval_mode, x_ref, u, v)); 250db2becc9SJeremy L Thompson return CEED_ERROR_SUCCESS; 251db2becc9SJeremy L Thompson } 252db2becc9SJeremy L Thompson 253db2becc9SJeremy L Thompson static int CeedBasisApplyAddAtPoints_Cuda(CeedBasis basis, const CeedInt num_elem, const CeedInt *num_points, CeedTransposeMode t_mode, 254db2becc9SJeremy L Thompson CeedEvalMode eval_mode, CeedVector x_ref, CeedVector u, CeedVector v) { 255db2becc9SJeremy L Thompson CeedCallBackend(CeedBasisApplyAtPointsCore_Cuda(basis, true, num_elem, num_points, t_mode, eval_mode, x_ref, u, v)); 256db2becc9SJeremy L Thompson return CEED_ERROR_SUCCESS; 257db2becc9SJeremy L Thompson } 258db2becc9SJeremy L Thompson 25934d14614SJeremy L Thompson //------------------------------------------------------------------------------ 2600d0321e0SJeremy L Thompson // Basis apply - non-tensor 2610d0321e0SJeremy L Thompson //------------------------------------------------------------------------------ 262db2becc9SJeremy L Thompson static int CeedBasisApplyNonTensorCore_Cuda(CeedBasis basis, bool apply_add, const CeedInt num_elem, CeedTransposeMode t_mode, CeedEvalMode eval_mode, 263db2becc9SJeremy L Thompson CeedVector u, CeedVector v) { 2640d0321e0SJeremy L Thompson Ceed ceed; 265437930d1SJeremy L Thompson CeedInt num_nodes, num_qpts; 2667bbbfca3SJeremy L Thompson const CeedInt is_transpose = t_mode == CEED_TRANSPOSE; 267d075f50bSSebastian Grimberg const int elems_per_block = 1; 268d075f50bSSebastian Grimberg const int grid = CeedDivUpInt(num_elem, elems_per_block); 2690d0321e0SJeremy L Thompson const CeedScalar *d_u; 2700d0321e0SJeremy L Thompson CeedScalar *d_v; 271ca735530SJeremy L Thompson CeedBasisNonTensor_Cuda *data; 272ca735530SJeremy L Thompson 273ca735530SJeremy L Thompson CeedCallBackend(CeedBasisGetCeed(basis, &ceed)); 274ca735530SJeremy L Thompson CeedCallBackend(CeedBasisGetData(basis, &data)); 275ca735530SJeremy L Thompson CeedCallBackend(CeedBasisGetNumQuadraturePoints(basis, &num_qpts)); 276ca735530SJeremy L Thompson CeedCallBackend(CeedBasisGetNumNodes(basis, &num_nodes)); 277ca735530SJeremy L Thompson 2789ea2cfd9SJeremy L Thompson // Get read/write access to u, v 2799ea2cfd9SJeremy L Thompson if (u != CEED_VECTOR_NONE) CeedCallBackend(CeedVectorGetArrayRead(u, CEED_MEM_DEVICE, &d_u)); 2809ea2cfd9SJeremy L Thompson else CeedCheck(eval_mode == CEED_EVAL_WEIGHT, ceed, CEED_ERROR_BACKEND, "An input vector is required for this CeedEvalMode"); 281db2becc9SJeremy L Thompson if (apply_add) CeedCallBackend(CeedVectorGetArray(v, CEED_MEM_DEVICE, &d_v)); 282db2becc9SJeremy L Thompson else CeedCallBackend(CeedVectorGetArrayWrite(v, CEED_MEM_DEVICE, &d_v)); 2830d0321e0SJeremy L Thompson 2840d0321e0SJeremy L Thompson // Clear v for transpose operation 285db2becc9SJeremy L Thompson if (is_transpose && !apply_add) { 2861f9221feSJeremy L Thompson CeedSize length; 287ca735530SJeremy L Thompson 2882b730f8bSJeremy L Thompson CeedCallBackend(CeedVectorGetLength(v, &length)); 2892b730f8bSJeremy L Thompson CeedCallCuda(ceed, cudaMemset(d_v, 0, length * sizeof(CeedScalar))); 2900d0321e0SJeremy L Thompson } 2910d0321e0SJeremy L Thompson 2920d0321e0SJeremy L Thompson // Apply basis operation 293437930d1SJeremy L Thompson switch (eval_mode) { 2940d0321e0SJeremy L Thompson case CEED_EVAL_INTERP: { 295d075f50bSSebastian Grimberg void *interp_args[] = {(void *)&num_elem, &data->d_interp, &d_u, &d_v}; 2967bbbfca3SJeremy L Thompson const int block_size_x = is_transpose ? num_nodes : num_qpts; 297b2165e7aSSebastian Grimberg 2987bbbfca3SJeremy L Thompson if (is_transpose) { 299d075f50bSSebastian Grimberg CeedCallBackend(CeedRunKernelDim_Cuda(ceed, data->InterpTranspose, grid, block_size_x, 1, elems_per_block, interp_args)); 300d075f50bSSebastian Grimberg } else { 301b2165e7aSSebastian Grimberg CeedCallBackend(CeedRunKernelDim_Cuda(ceed, data->Interp, grid, block_size_x, 1, elems_per_block, interp_args)); 302d075f50bSSebastian Grimberg } 3030d0321e0SJeremy L Thompson } break; 3040d0321e0SJeremy L Thompson case CEED_EVAL_GRAD: { 305d075f50bSSebastian Grimberg void *grad_args[] = {(void *)&num_elem, &data->d_grad, &d_u, &d_v}; 3067bbbfca3SJeremy L Thompson const int block_size_x = is_transpose ? num_nodes : num_qpts; 307b2165e7aSSebastian Grimberg 3087bbbfca3SJeremy L Thompson if (is_transpose) { 309d075f50bSSebastian Grimberg CeedCallBackend(CeedRunKernelDim_Cuda(ceed, data->DerivTranspose, grid, block_size_x, 1, elems_per_block, grad_args)); 310d075f50bSSebastian Grimberg } else { 311d075f50bSSebastian Grimberg CeedCallBackend(CeedRunKernelDim_Cuda(ceed, data->Deriv, grid, block_size_x, 1, elems_per_block, grad_args)); 312d075f50bSSebastian Grimberg } 313d075f50bSSebastian Grimberg } break; 314d075f50bSSebastian Grimberg case CEED_EVAL_DIV: { 315d075f50bSSebastian Grimberg void *div_args[] = {(void *)&num_elem, &data->d_div, &d_u, &d_v}; 3167bbbfca3SJeremy L Thompson const int block_size_x = is_transpose ? num_nodes : num_qpts; 317d075f50bSSebastian Grimberg 3187bbbfca3SJeremy L Thompson if (is_transpose) { 319d075f50bSSebastian Grimberg CeedCallBackend(CeedRunKernelDim_Cuda(ceed, data->DerivTranspose, grid, block_size_x, 1, elems_per_block, div_args)); 320d075f50bSSebastian Grimberg } else { 321d075f50bSSebastian Grimberg CeedCallBackend(CeedRunKernelDim_Cuda(ceed, data->Deriv, grid, block_size_x, 1, elems_per_block, div_args)); 322d075f50bSSebastian Grimberg } 323d075f50bSSebastian Grimberg } break; 324d075f50bSSebastian Grimberg case CEED_EVAL_CURL: { 325d075f50bSSebastian Grimberg void *curl_args[] = {(void *)&num_elem, &data->d_curl, &d_u, &d_v}; 3267bbbfca3SJeremy L Thompson const int block_size_x = is_transpose ? num_nodes : num_qpts; 327d075f50bSSebastian Grimberg 3287bbbfca3SJeremy L Thompson if (is_transpose) { 329d075f50bSSebastian Grimberg CeedCallBackend(CeedRunKernelDim_Cuda(ceed, data->DerivTranspose, grid, block_size_x, 1, elems_per_block, curl_args)); 330d075f50bSSebastian Grimberg } else { 331d075f50bSSebastian Grimberg CeedCallBackend(CeedRunKernelDim_Cuda(ceed, data->Deriv, grid, block_size_x, 1, elems_per_block, curl_args)); 332d075f50bSSebastian Grimberg } 3330d0321e0SJeremy L Thompson } break; 3340d0321e0SJeremy L Thompson case CEED_EVAL_WEIGHT: { 335097cc795SJames Wright CeedCheck(data->d_q_weight, ceed, CEED_ERROR_BACKEND, "%s not supported; q_weights not set", CeedEvalModes[eval_mode]); 336437930d1SJeremy L Thompson void *weight_args[] = {(void *)&num_elem, (void *)&data->d_q_weight, &d_v}; 337b2165e7aSSebastian Grimberg 338eb7e6cafSJeremy L Thompson CeedCallBackend(CeedRunKernelDim_Cuda(ceed, data->Weight, grid, num_qpts, 1, elems_per_block, weight_args)); 3390d0321e0SJeremy L Thompson } break; 3409ea2cfd9SJeremy L Thompson case CEED_EVAL_NONE: /* handled separately below */ 3419ea2cfd9SJeremy L Thompson break; 3420d0321e0SJeremy L Thompson } 3430d0321e0SJeremy L Thompson 3449ea2cfd9SJeremy L Thompson // Restore vectors, cover CEED_EVAL_NONE 3452b730f8bSJeremy L Thompson CeedCallBackend(CeedVectorRestoreArray(v, &d_v)); 3469ea2cfd9SJeremy L Thompson if (eval_mode == CEED_EVAL_NONE) CeedCallBackend(CeedVectorSetArray(v, CEED_MEM_DEVICE, CEED_COPY_VALUES, (CeedScalar *)d_u)); 3479ea2cfd9SJeremy L Thompson if (eval_mode != CEED_EVAL_WEIGHT) CeedCallBackend(CeedVectorRestoreArrayRead(u, &d_u)); 3480d0321e0SJeremy L Thompson return CEED_ERROR_SUCCESS; 3490d0321e0SJeremy L Thompson } 3500d0321e0SJeremy L Thompson 351db2becc9SJeremy L Thompson static int CeedBasisApplyNonTensor_Cuda(CeedBasis basis, const CeedInt num_elem, CeedTransposeMode t_mode, CeedEvalMode eval_mode, CeedVector u, 352db2becc9SJeremy L Thompson CeedVector v) { 353db2becc9SJeremy L Thompson CeedCallBackend(CeedBasisApplyNonTensorCore_Cuda(basis, false, num_elem, t_mode, eval_mode, u, v)); 354db2becc9SJeremy L Thompson return CEED_ERROR_SUCCESS; 355db2becc9SJeremy L Thompson } 356db2becc9SJeremy L Thompson 357db2becc9SJeremy L Thompson static int CeedBasisApplyAddNonTensor_Cuda(CeedBasis basis, const CeedInt num_elem, CeedTransposeMode t_mode, CeedEvalMode eval_mode, CeedVector u, 358db2becc9SJeremy L Thompson CeedVector v) { 359db2becc9SJeremy L Thompson CeedCallBackend(CeedBasisApplyNonTensorCore_Cuda(basis, true, num_elem, t_mode, eval_mode, u, v)); 360db2becc9SJeremy L Thompson return CEED_ERROR_SUCCESS; 361db2becc9SJeremy L Thompson } 362db2becc9SJeremy L Thompson 3630d0321e0SJeremy L Thompson //------------------------------------------------------------------------------ 3640d0321e0SJeremy L Thompson // Destroy tensor basis 3650d0321e0SJeremy L Thompson //------------------------------------------------------------------------------ 3660d0321e0SJeremy L Thompson static int CeedBasisDestroy_Cuda(CeedBasis basis) { 3670d0321e0SJeremy L Thompson Ceed ceed; 3680d0321e0SJeremy L Thompson CeedBasis_Cuda *data; 369ca735530SJeremy L Thompson 370ca735530SJeremy L Thompson CeedCallBackend(CeedBasisGetCeed(basis, &ceed)); 3712b730f8bSJeremy L Thompson CeedCallBackend(CeedBasisGetData(basis, &data)); 3722b730f8bSJeremy L Thompson CeedCallCuda(ceed, cuModuleUnload(data->module)); 37334d14614SJeremy L Thompson if (data->moduleAtPoints) CeedCallCuda(ceed, cuModuleUnload(data->moduleAtPoints)); 374097cc795SJames Wright if (data->d_q_weight_1d) CeedCallCuda(ceed, cudaFree(data->d_q_weight_1d)); 375*111870feSJeremy L Thompson CeedCallBackend(CeedFree(&data->h_points_per_elem)); 376*111870feSJeremy L Thompson if (data->d_points_per_elem) CeedCallCuda(ceed, cudaFree(data->d_points_per_elem)); 3772b730f8bSJeremy L Thompson CeedCallCuda(ceed, cudaFree(data->d_interp_1d)); 3782b730f8bSJeremy L Thompson CeedCallCuda(ceed, cudaFree(data->d_grad_1d)); 37934d14614SJeremy L Thompson CeedCallCuda(ceed, cudaFree(data->d_chebyshev_interp_1d)); 3802b730f8bSJeremy L Thompson CeedCallBackend(CeedFree(&data)); 3810d0321e0SJeremy L Thompson return CEED_ERROR_SUCCESS; 3820d0321e0SJeremy L Thompson } 3830d0321e0SJeremy L Thompson 3840d0321e0SJeremy L Thompson //------------------------------------------------------------------------------ 3850d0321e0SJeremy L Thompson // Destroy non-tensor basis 3860d0321e0SJeremy L Thompson //------------------------------------------------------------------------------ 3870d0321e0SJeremy L Thompson static int CeedBasisDestroyNonTensor_Cuda(CeedBasis basis) { 3880d0321e0SJeremy L Thompson Ceed ceed; 3890d0321e0SJeremy L Thompson CeedBasisNonTensor_Cuda *data; 390ca735530SJeremy L Thompson 391ca735530SJeremy L Thompson CeedCallBackend(CeedBasisGetCeed(basis, &ceed)); 3922b730f8bSJeremy L Thompson CeedCallBackend(CeedBasisGetData(basis, &data)); 3932b730f8bSJeremy L Thompson CeedCallCuda(ceed, cuModuleUnload(data->module)); 394097cc795SJames Wright if (data->d_q_weight) CeedCallCuda(ceed, cudaFree(data->d_q_weight)); 3952b730f8bSJeremy L Thompson CeedCallCuda(ceed, cudaFree(data->d_interp)); 3962b730f8bSJeremy L Thompson CeedCallCuda(ceed, cudaFree(data->d_grad)); 397d075f50bSSebastian Grimberg CeedCallCuda(ceed, cudaFree(data->d_div)); 398d075f50bSSebastian Grimberg CeedCallCuda(ceed, cudaFree(data->d_curl)); 3992b730f8bSJeremy L Thompson CeedCallBackend(CeedFree(&data)); 4000d0321e0SJeremy L Thompson return CEED_ERROR_SUCCESS; 4010d0321e0SJeremy L Thompson } 4020d0321e0SJeremy L Thompson 4030d0321e0SJeremy L Thompson //------------------------------------------------------------------------------ 4040d0321e0SJeremy L Thompson // Create tensor 4050d0321e0SJeremy L Thompson //------------------------------------------------------------------------------ 4062b730f8bSJeremy L Thompson int CeedBasisCreateTensorH1_Cuda(CeedInt dim, CeedInt P_1d, CeedInt Q_1d, const CeedScalar *interp_1d, const CeedScalar *grad_1d, 4072b730f8bSJeremy L Thompson const CeedScalar *q_ref_1d, const CeedScalar *q_weight_1d, CeedBasis basis) { 4080d0321e0SJeremy L Thompson Ceed ceed; 40922070f95SJeremy L Thompson char *basis_kernel_source; 41022070f95SJeremy L Thompson const char *basis_kernel_path; 411ca735530SJeremy L Thompson CeedInt num_comp; 412ca735530SJeremy L Thompson const CeedInt q_bytes = Q_1d * sizeof(CeedScalar); 413ca735530SJeremy L Thompson const CeedInt interp_bytes = q_bytes * P_1d; 4140d0321e0SJeremy L Thompson CeedBasis_Cuda *data; 415ca735530SJeremy L Thompson 416ca735530SJeremy L Thompson CeedCallBackend(CeedBasisGetCeed(basis, &ceed)); 4172b730f8bSJeremy L Thompson CeedCallBackend(CeedCalloc(1, &data)); 4180d0321e0SJeremy L Thompson 4190d0321e0SJeremy L Thompson // Copy data to GPU 420097cc795SJames Wright if (q_weight_1d) { 4212b730f8bSJeremy L Thompson CeedCallCuda(ceed, cudaMalloc((void **)&data->d_q_weight_1d, q_bytes)); 4222b730f8bSJeremy L Thompson CeedCallCuda(ceed, cudaMemcpy(data->d_q_weight_1d, q_weight_1d, q_bytes, cudaMemcpyHostToDevice)); 423097cc795SJames Wright } 4242b730f8bSJeremy L Thompson CeedCallCuda(ceed, cudaMalloc((void **)&data->d_interp_1d, interp_bytes)); 4252b730f8bSJeremy L Thompson CeedCallCuda(ceed, cudaMemcpy(data->d_interp_1d, interp_1d, interp_bytes, cudaMemcpyHostToDevice)); 4262b730f8bSJeremy L Thompson CeedCallCuda(ceed, cudaMalloc((void **)&data->d_grad_1d, interp_bytes)); 4272b730f8bSJeremy L Thompson CeedCallCuda(ceed, cudaMemcpy(data->d_grad_1d, grad_1d, interp_bytes, cudaMemcpyHostToDevice)); 4280d0321e0SJeremy L Thompson 429ecc88aebSJeremy L Thompson // Compile basis kernels 4302b730f8bSJeremy L Thompson CeedCallBackend(CeedBasisGetNumComponents(basis, &num_comp)); 4312b730f8bSJeremy L Thompson CeedCallBackend(CeedGetJitAbsolutePath(ceed, "ceed/jit-source/cuda/cuda-ref-basis-tensor.h", &basis_kernel_path)); 43223d4529eSJeremy L Thompson CeedDebug256(ceed, CEED_DEBUG_COLOR_SUCCESS, "----- Loading Basis Kernel Source -----\n"); 4332b730f8bSJeremy L Thompson CeedCallBackend(CeedLoadSourceToBuffer(ceed, basis_kernel_path, &basis_kernel_source)); 43423d4529eSJeremy L Thompson CeedDebug256(ceed, CEED_DEBUG_COLOR_SUCCESS, "----- Loading Basis Kernel Source Complete! -----\n"); 435eb7e6cafSJeremy L Thompson CeedCallBackend(CeedCompile_Cuda(ceed, basis_kernel_source, &data->module, 7, "BASIS_Q_1D", Q_1d, "BASIS_P_1D", P_1d, "BASIS_BUF_LEN", 436f7c9815fSJeremy L Thompson Q_1d * CeedIntPow(Q_1d > P_1d ? Q_1d : P_1d, dim - 1), "BASIS_DIM", dim, "BASIS_NUM_COMP", num_comp, 4372b730f8bSJeremy L Thompson "BASIS_NUM_NODES", CeedIntPow(P_1d, dim), "BASIS_NUM_QPTS", CeedIntPow(Q_1d, dim))); 438eb7e6cafSJeremy L Thompson CeedCallBackend(CeedGetKernel_Cuda(ceed, data->module, "Interp", &data->Interp)); 439eb7e6cafSJeremy L Thompson CeedCallBackend(CeedGetKernel_Cuda(ceed, data->module, "Grad", &data->Grad)); 440eb7e6cafSJeremy L Thompson CeedCallBackend(CeedGetKernel_Cuda(ceed, data->module, "Weight", &data->Weight)); 4412b730f8bSJeremy L Thompson CeedCallBackend(CeedFree(&basis_kernel_path)); 4422b730f8bSJeremy L Thompson CeedCallBackend(CeedFree(&basis_kernel_source)); 443437930d1SJeremy L Thompson 4442b730f8bSJeremy L Thompson CeedCallBackend(CeedBasisSetData(basis, data)); 4450d0321e0SJeremy L Thompson 446d075f50bSSebastian Grimberg // Register backend functions 4472b730f8bSJeremy L Thompson CeedCallBackend(CeedSetBackendFunction(ceed, "Basis", basis, "Apply", CeedBasisApply_Cuda)); 448db2becc9SJeremy L Thompson CeedCallBackend(CeedSetBackendFunction(ceed, "Basis", basis, "ApplyAdd", CeedBasisApplyAdd_Cuda)); 44934d14614SJeremy L Thompson CeedCallBackend(CeedSetBackendFunction(ceed, "Basis", basis, "ApplyAtPoints", CeedBasisApplyAtPoints_Cuda)); 450db2becc9SJeremy L Thompson CeedCallBackend(CeedSetBackendFunction(ceed, "Basis", basis, "ApplyAddAtPoints", CeedBasisApplyAddAtPoints_Cuda)); 4512b730f8bSJeremy L Thompson CeedCallBackend(CeedSetBackendFunction(ceed, "Basis", basis, "Destroy", CeedBasisDestroy_Cuda)); 4520d0321e0SJeremy L Thompson return CEED_ERROR_SUCCESS; 4530d0321e0SJeremy L Thompson } 4540d0321e0SJeremy L Thompson 4550d0321e0SJeremy L Thompson //------------------------------------------------------------------------------ 456d075f50bSSebastian Grimberg // Create non-tensor H^1 4570d0321e0SJeremy L Thompson //------------------------------------------------------------------------------ 4582b730f8bSJeremy L Thompson int CeedBasisCreateH1_Cuda(CeedElemTopology topo, CeedInt dim, CeedInt num_nodes, CeedInt num_qpts, const CeedScalar *interp, const CeedScalar *grad, 45951475c7cSJeremy L Thompson const CeedScalar *q_ref, const CeedScalar *q_weight, CeedBasis basis) { 4600d0321e0SJeremy L Thompson Ceed ceed; 46122070f95SJeremy L Thompson char *basis_kernel_source; 46222070f95SJeremy L Thompson const char *basis_kernel_path; 463d075f50bSSebastian Grimberg CeedInt num_comp, q_comp_interp, q_comp_grad; 464ca735530SJeremy L Thompson const CeedInt q_bytes = num_qpts * sizeof(CeedScalar); 4650d0321e0SJeremy L Thompson CeedBasisNonTensor_Cuda *data; 466ca735530SJeremy L Thompson 467ca735530SJeremy L Thompson CeedCallBackend(CeedBasisGetCeed(basis, &ceed)); 4682b730f8bSJeremy L Thompson CeedCallBackend(CeedCalloc(1, &data)); 4690d0321e0SJeremy L Thompson 4700d0321e0SJeremy L Thompson // Copy basis data to GPU 471d075f50bSSebastian Grimberg CeedCallBackend(CeedBasisGetNumQuadratureComponents(basis, CEED_EVAL_INTERP, &q_comp_interp)); 472d075f50bSSebastian Grimberg CeedCallBackend(CeedBasisGetNumQuadratureComponents(basis, CEED_EVAL_GRAD, &q_comp_grad)); 473097cc795SJames Wright if (q_weight) { 4742b730f8bSJeremy L Thompson CeedCallCuda(ceed, cudaMalloc((void **)&data->d_q_weight, q_bytes)); 4752b730f8bSJeremy L Thompson CeedCallCuda(ceed, cudaMemcpy(data->d_q_weight, q_weight, q_bytes, cudaMemcpyHostToDevice)); 476097cc795SJames Wright } 477d075f50bSSebastian Grimberg if (interp) { 478d075f50bSSebastian Grimberg const CeedInt interp_bytes = q_bytes * num_nodes * q_comp_interp; 479d075f50bSSebastian Grimberg 4802b730f8bSJeremy L Thompson CeedCallCuda(ceed, cudaMalloc((void **)&data->d_interp, interp_bytes)); 4812b730f8bSJeremy L Thompson CeedCallCuda(ceed, cudaMemcpy(data->d_interp, interp, interp_bytes, cudaMemcpyHostToDevice)); 482d075f50bSSebastian Grimberg } 483d075f50bSSebastian Grimberg if (grad) { 484d075f50bSSebastian Grimberg const CeedInt grad_bytes = q_bytes * num_nodes * q_comp_grad; 485d075f50bSSebastian Grimberg 4862b730f8bSJeremy L Thompson CeedCallCuda(ceed, cudaMalloc((void **)&data->d_grad, grad_bytes)); 4872b730f8bSJeremy L Thompson CeedCallCuda(ceed, cudaMemcpy(data->d_grad, grad, grad_bytes, cudaMemcpyHostToDevice)); 488d075f50bSSebastian Grimberg } 4890d0321e0SJeremy L Thompson 4900d0321e0SJeremy L Thompson // Compile basis kernels 4912b730f8bSJeremy L Thompson CeedCallBackend(CeedBasisGetNumComponents(basis, &num_comp)); 4922b730f8bSJeremy L Thompson CeedCallBackend(CeedGetJitAbsolutePath(ceed, "ceed/jit-source/cuda/cuda-ref-basis-nontensor.h", &basis_kernel_path)); 49323d4529eSJeremy L Thompson CeedDebug256(ceed, CEED_DEBUG_COLOR_SUCCESS, "----- Loading Basis Kernel Source -----\n"); 4942b730f8bSJeremy L Thompson CeedCallBackend(CeedLoadSourceToBuffer(ceed, basis_kernel_path, &basis_kernel_source)); 49523d4529eSJeremy L Thompson CeedDebug256(ceed, CEED_DEBUG_COLOR_SUCCESS, "----- Loading Basis Kernel Source Complete! -----\n"); 496d075f50bSSebastian Grimberg CeedCallBackend(CeedCompile_Cuda(ceed, basis_kernel_source, &data->module, 5, "BASIS_Q", num_qpts, "BASIS_P", num_nodes, "BASIS_Q_COMP_INTERP", 497d075f50bSSebastian Grimberg q_comp_interp, "BASIS_Q_COMP_DERIV", q_comp_grad, "BASIS_NUM_COMP", num_comp)); 498d075f50bSSebastian Grimberg CeedCallBackend(CeedGetKernel_Cuda(ceed, data->module, "Interp", &data->Interp)); 499d075f50bSSebastian Grimberg CeedCallBackend(CeedGetKernel_Cuda(ceed, data->module, "InterpTranspose", &data->InterpTranspose)); 500d075f50bSSebastian Grimberg CeedCallBackend(CeedGetKernel_Cuda(ceed, data->module, "Deriv", &data->Deriv)); 501d075f50bSSebastian Grimberg CeedCallBackend(CeedGetKernel_Cuda(ceed, data->module, "DerivTranspose", &data->DerivTranspose)); 502d075f50bSSebastian Grimberg CeedCallBackend(CeedGetKernel_Cuda(ceed, data->module, "Weight", &data->Weight)); 503d075f50bSSebastian Grimberg CeedCallBackend(CeedFree(&basis_kernel_path)); 504d075f50bSSebastian Grimberg CeedCallBackend(CeedFree(&basis_kernel_source)); 505d075f50bSSebastian Grimberg 506d075f50bSSebastian Grimberg CeedCallBackend(CeedBasisSetData(basis, data)); 507d075f50bSSebastian Grimberg 508d075f50bSSebastian Grimberg // Register backend functions 509d075f50bSSebastian Grimberg CeedCallBackend(CeedSetBackendFunction(ceed, "Basis", basis, "Apply", CeedBasisApplyNonTensor_Cuda)); 510db2becc9SJeremy L Thompson CeedCallBackend(CeedSetBackendFunction(ceed, "Basis", basis, "ApplyAdd", CeedBasisApplyAddNonTensor_Cuda)); 511d075f50bSSebastian Grimberg CeedCallBackend(CeedSetBackendFunction(ceed, "Basis", basis, "Destroy", CeedBasisDestroyNonTensor_Cuda)); 512d075f50bSSebastian Grimberg return CEED_ERROR_SUCCESS; 513d075f50bSSebastian Grimberg } 514d075f50bSSebastian Grimberg 515d075f50bSSebastian Grimberg //------------------------------------------------------------------------------ 516d075f50bSSebastian Grimberg // Create non-tensor H(div) 517d075f50bSSebastian Grimberg //------------------------------------------------------------------------------ 518d075f50bSSebastian Grimberg int CeedBasisCreateHdiv_Cuda(CeedElemTopology topo, CeedInt dim, CeedInt num_nodes, CeedInt num_qpts, const CeedScalar *interp, const CeedScalar *div, 519d075f50bSSebastian Grimberg const CeedScalar *q_ref, const CeedScalar *q_weight, CeedBasis basis) { 520d075f50bSSebastian Grimberg Ceed ceed; 52122070f95SJeremy L Thompson char *basis_kernel_source; 52222070f95SJeremy L Thompson const char *basis_kernel_path; 523d075f50bSSebastian Grimberg CeedInt num_comp, q_comp_interp, q_comp_div; 524d075f50bSSebastian Grimberg const CeedInt q_bytes = num_qpts * sizeof(CeedScalar); 525d075f50bSSebastian Grimberg CeedBasisNonTensor_Cuda *data; 526d075f50bSSebastian Grimberg 527d075f50bSSebastian Grimberg CeedCallBackend(CeedBasisGetCeed(basis, &ceed)); 528d075f50bSSebastian Grimberg CeedCallBackend(CeedCalloc(1, &data)); 529d075f50bSSebastian Grimberg 530d075f50bSSebastian Grimberg // Copy basis data to GPU 531d075f50bSSebastian Grimberg CeedCallBackend(CeedBasisGetNumQuadratureComponents(basis, CEED_EVAL_INTERP, &q_comp_interp)); 532d075f50bSSebastian Grimberg CeedCallBackend(CeedBasisGetNumQuadratureComponents(basis, CEED_EVAL_DIV, &q_comp_div)); 533097cc795SJames Wright if (q_weight) { 534d075f50bSSebastian Grimberg CeedCallCuda(ceed, cudaMalloc((void **)&data->d_q_weight, q_bytes)); 535d075f50bSSebastian Grimberg CeedCallCuda(ceed, cudaMemcpy(data->d_q_weight, q_weight, q_bytes, cudaMemcpyHostToDevice)); 536097cc795SJames Wright } 537d075f50bSSebastian Grimberg if (interp) { 538d075f50bSSebastian Grimberg const CeedInt interp_bytes = q_bytes * num_nodes * q_comp_interp; 539d075f50bSSebastian Grimberg 540d075f50bSSebastian Grimberg CeedCallCuda(ceed, cudaMalloc((void **)&data->d_interp, interp_bytes)); 541d075f50bSSebastian Grimberg CeedCallCuda(ceed, cudaMemcpy(data->d_interp, interp, interp_bytes, cudaMemcpyHostToDevice)); 542d075f50bSSebastian Grimberg } 543d075f50bSSebastian Grimberg if (div) { 544d075f50bSSebastian Grimberg const CeedInt div_bytes = q_bytes * num_nodes * q_comp_div; 545d075f50bSSebastian Grimberg 546d075f50bSSebastian Grimberg CeedCallCuda(ceed, cudaMalloc((void **)&data->d_div, div_bytes)); 547d075f50bSSebastian Grimberg CeedCallCuda(ceed, cudaMemcpy(data->d_div, div, div_bytes, cudaMemcpyHostToDevice)); 548d075f50bSSebastian Grimberg } 549d075f50bSSebastian Grimberg 550d075f50bSSebastian Grimberg // Compile basis kernels 551d075f50bSSebastian Grimberg CeedCallBackend(CeedBasisGetNumComponents(basis, &num_comp)); 552d075f50bSSebastian Grimberg CeedCallBackend(CeedGetJitAbsolutePath(ceed, "ceed/jit-source/cuda/cuda-ref-basis-nontensor.h", &basis_kernel_path)); 553d075f50bSSebastian Grimberg CeedDebug256(ceed, CEED_DEBUG_COLOR_SUCCESS, "----- Loading Basis Kernel Source -----\n"); 554d075f50bSSebastian Grimberg CeedCallBackend(CeedLoadSourceToBuffer(ceed, basis_kernel_path, &basis_kernel_source)); 555d075f50bSSebastian Grimberg CeedDebug256(ceed, CEED_DEBUG_COLOR_SUCCESS, "----- Loading Basis Kernel Source Complete! -----\n"); 556d075f50bSSebastian Grimberg CeedCallBackend(CeedCompile_Cuda(ceed, basis_kernel_source, &data->module, 5, "BASIS_Q", num_qpts, "BASIS_P", num_nodes, "BASIS_Q_COMP_INTERP", 557d075f50bSSebastian Grimberg q_comp_interp, "BASIS_Q_COMP_DERIV", q_comp_div, "BASIS_NUM_COMP", num_comp)); 558d075f50bSSebastian Grimberg CeedCallBackend(CeedGetKernel_Cuda(ceed, data->module, "Interp", &data->Interp)); 559d075f50bSSebastian Grimberg CeedCallBackend(CeedGetKernel_Cuda(ceed, data->module, "InterpTranspose", &data->InterpTranspose)); 560d075f50bSSebastian Grimberg CeedCallBackend(CeedGetKernel_Cuda(ceed, data->module, "Deriv", &data->Deriv)); 561d075f50bSSebastian Grimberg CeedCallBackend(CeedGetKernel_Cuda(ceed, data->module, "DerivTranspose", &data->DerivTranspose)); 562d075f50bSSebastian Grimberg CeedCallBackend(CeedGetKernel_Cuda(ceed, data->module, "Weight", &data->Weight)); 563d075f50bSSebastian Grimberg CeedCallBackend(CeedFree(&basis_kernel_path)); 564d075f50bSSebastian Grimberg CeedCallBackend(CeedFree(&basis_kernel_source)); 565d075f50bSSebastian Grimberg 566d075f50bSSebastian Grimberg CeedCallBackend(CeedBasisSetData(basis, data)); 567d075f50bSSebastian Grimberg 568d075f50bSSebastian Grimberg // Register backend functions 569d075f50bSSebastian Grimberg CeedCallBackend(CeedSetBackendFunction(ceed, "Basis", basis, "Apply", CeedBasisApplyNonTensor_Cuda)); 570db2becc9SJeremy L Thompson CeedCallBackend(CeedSetBackendFunction(ceed, "Basis", basis, "ApplyAdd", CeedBasisApplyAddNonTensor_Cuda)); 571d075f50bSSebastian Grimberg CeedCallBackend(CeedSetBackendFunction(ceed, "Basis", basis, "Destroy", CeedBasisDestroyNonTensor_Cuda)); 572d075f50bSSebastian Grimberg return CEED_ERROR_SUCCESS; 573d075f50bSSebastian Grimberg } 574d075f50bSSebastian Grimberg 575d075f50bSSebastian Grimberg //------------------------------------------------------------------------------ 576d075f50bSSebastian Grimberg // Create non-tensor H(curl) 577d075f50bSSebastian Grimberg //------------------------------------------------------------------------------ 578d075f50bSSebastian Grimberg int CeedBasisCreateHcurl_Cuda(CeedElemTopology topo, CeedInt dim, CeedInt num_nodes, CeedInt num_qpts, const CeedScalar *interp, 579d075f50bSSebastian Grimberg const CeedScalar *curl, const CeedScalar *q_ref, const CeedScalar *q_weight, CeedBasis basis) { 580d075f50bSSebastian Grimberg Ceed ceed; 58122070f95SJeremy L Thompson char *basis_kernel_source; 58222070f95SJeremy L Thompson const char *basis_kernel_path; 583d075f50bSSebastian Grimberg CeedInt num_comp, q_comp_interp, q_comp_curl; 584d075f50bSSebastian Grimberg const CeedInt q_bytes = num_qpts * sizeof(CeedScalar); 585d075f50bSSebastian Grimberg CeedBasisNonTensor_Cuda *data; 586d075f50bSSebastian Grimberg 587d075f50bSSebastian Grimberg CeedCallBackend(CeedBasisGetCeed(basis, &ceed)); 588d075f50bSSebastian Grimberg CeedCallBackend(CeedCalloc(1, &data)); 589d075f50bSSebastian Grimberg 590d075f50bSSebastian Grimberg // Copy basis data to GPU 591d075f50bSSebastian Grimberg CeedCallBackend(CeedBasisGetNumQuadratureComponents(basis, CEED_EVAL_INTERP, &q_comp_interp)); 592d075f50bSSebastian Grimberg CeedCallBackend(CeedBasisGetNumQuadratureComponents(basis, CEED_EVAL_CURL, &q_comp_curl)); 593097cc795SJames Wright if (q_weight) { 594d075f50bSSebastian Grimberg CeedCallCuda(ceed, cudaMalloc((void **)&data->d_q_weight, q_bytes)); 595d075f50bSSebastian Grimberg CeedCallCuda(ceed, cudaMemcpy(data->d_q_weight, q_weight, q_bytes, cudaMemcpyHostToDevice)); 596097cc795SJames Wright } 597d075f50bSSebastian Grimberg if (interp) { 598d075f50bSSebastian Grimberg const CeedInt interp_bytes = q_bytes * num_nodes * q_comp_interp; 599d075f50bSSebastian Grimberg 600d075f50bSSebastian Grimberg CeedCallCuda(ceed, cudaMalloc((void **)&data->d_interp, interp_bytes)); 601d075f50bSSebastian Grimberg CeedCallCuda(ceed, cudaMemcpy(data->d_interp, interp, interp_bytes, cudaMemcpyHostToDevice)); 602d075f50bSSebastian Grimberg } 603d075f50bSSebastian Grimberg if (curl) { 604d075f50bSSebastian Grimberg const CeedInt curl_bytes = q_bytes * num_nodes * q_comp_curl; 605d075f50bSSebastian Grimberg 606d075f50bSSebastian Grimberg CeedCallCuda(ceed, cudaMalloc((void **)&data->d_curl, curl_bytes)); 607d075f50bSSebastian Grimberg CeedCallCuda(ceed, cudaMemcpy(data->d_curl, curl, curl_bytes, cudaMemcpyHostToDevice)); 608d075f50bSSebastian Grimberg } 609d075f50bSSebastian Grimberg 610d075f50bSSebastian Grimberg // Compile basis kernels 611d075f50bSSebastian Grimberg CeedCallBackend(CeedBasisGetNumComponents(basis, &num_comp)); 612d075f50bSSebastian Grimberg CeedCallBackend(CeedGetJitAbsolutePath(ceed, "ceed/jit-source/cuda/cuda-ref-basis-nontensor.h", &basis_kernel_path)); 613d075f50bSSebastian Grimberg CeedDebug256(ceed, CEED_DEBUG_COLOR_SUCCESS, "----- Loading Basis Kernel Source -----\n"); 614d075f50bSSebastian Grimberg CeedCallBackend(CeedLoadSourceToBuffer(ceed, basis_kernel_path, &basis_kernel_source)); 615d075f50bSSebastian Grimberg CeedDebug256(ceed, CEED_DEBUG_COLOR_SUCCESS, "----- Loading Basis Kernel Source Complete! -----\n"); 616d075f50bSSebastian Grimberg CeedCallBackend(CeedCompile_Cuda(ceed, basis_kernel_source, &data->module, 5, "BASIS_Q", num_qpts, "BASIS_P", num_nodes, "BASIS_Q_COMP_INTERP", 617d075f50bSSebastian Grimberg q_comp_interp, "BASIS_Q_COMP_DERIV", q_comp_curl, "BASIS_NUM_COMP", num_comp)); 618d075f50bSSebastian Grimberg CeedCallBackend(CeedGetKernel_Cuda(ceed, data->module, "Interp", &data->Interp)); 619d075f50bSSebastian Grimberg CeedCallBackend(CeedGetKernel_Cuda(ceed, data->module, "InterpTranspose", &data->InterpTranspose)); 620d075f50bSSebastian Grimberg CeedCallBackend(CeedGetKernel_Cuda(ceed, data->module, "Deriv", &data->Deriv)); 621d075f50bSSebastian Grimberg CeedCallBackend(CeedGetKernel_Cuda(ceed, data->module, "DerivTranspose", &data->DerivTranspose)); 622d075f50bSSebastian Grimberg CeedCallBackend(CeedGetKernel_Cuda(ceed, data->module, "Weight", &data->Weight)); 6232b730f8bSJeremy L Thompson CeedCallBackend(CeedFree(&basis_kernel_path)); 6242b730f8bSJeremy L Thompson CeedCallBackend(CeedFree(&basis_kernel_source)); 6250d0321e0SJeremy L Thompson 6262b730f8bSJeremy L Thompson CeedCallBackend(CeedBasisSetData(basis, data)); 6270d0321e0SJeremy L Thompson 6280d0321e0SJeremy L Thompson // Register backend functions 6292b730f8bSJeremy L Thompson CeedCallBackend(CeedSetBackendFunction(ceed, "Basis", basis, "Apply", CeedBasisApplyNonTensor_Cuda)); 630db2becc9SJeremy L Thompson CeedCallBackend(CeedSetBackendFunction(ceed, "Basis", basis, "ApplyAdd", CeedBasisApplyAddNonTensor_Cuda)); 6312b730f8bSJeremy L Thompson CeedCallBackend(CeedSetBackendFunction(ceed, "Basis", basis, "Destroy", CeedBasisDestroyNonTensor_Cuda)); 6320d0321e0SJeremy L Thompson return CEED_ERROR_SUCCESS; 6330d0321e0SJeremy L Thompson } 6342a86cc9dSSebastian Grimberg 6350d0321e0SJeremy L Thompson //------------------------------------------------------------------------------ 636