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. 37f5b9731SStan Tomov // 43d8e8822SJeremy L Thompson // SPDX-License-Identifier: BSD-2-Clause 57f5b9731SStan Tomov // 63d8e8822SJeremy L Thompson // This file is part of CEED: http://github.com/ceed 77f5b9731SStan Tomov 849aac155SJeremy L Thompson #include <ceed.h> 9ec3da8bcSJed Brown #include <ceed/backend.h> 10f6af633fSnbeams #include <ceed/jit-tools.h> 11f6af633fSnbeams #include <string.h> 122b730f8bSJeremy L Thompson 13e5f091ebSnbeams #ifdef CEED_MAGMA_USE_HIP 14f6af633fSnbeams #include "../hip/ceed-hip-common.h" 15f6af633fSnbeams #include "../hip/ceed-hip-compile.h" 16f6af633fSnbeams #else 17f6af633fSnbeams #include "../cuda/ceed-cuda-common.h" 18f6af633fSnbeams #include "../cuda/ceed-cuda-compile.h" 19f6af633fSnbeams #endif 2000fb7a04SSebastian Grimberg #include "ceed-magma-common.h" 2100fb7a04SSebastian Grimberg #include "ceed-magma.h" 227f5b9731SStan Tomov 23*940a72f1SSebastian Grimberg #include "ceed-magma-gemm-nontensor.h" 24*940a72f1SSebastian Grimberg #include "ceed-magma-gemm-selector.h" 25*940a72f1SSebastian Grimberg 26*940a72f1SSebastian Grimberg //------------------------------------------------------------------------------ 27*940a72f1SSebastian Grimberg // Basis apply - tensor 28*940a72f1SSebastian Grimberg //------------------------------------------------------------------------------ 29*940a72f1SSebastian Grimberg static int CeedBasisApply_Magma(CeedBasis basis, CeedInt num_elem, CeedTransposeMode t_mode, CeedEvalMode e_mode, CeedVector u, CeedVector v) { 307f5b9731SStan Tomov Ceed ceed; 31e0582403Sabdelfattah83 Ceed_Magma *data; 32*940a72f1SSebastian Grimberg CeedInt dim, num_comp, num_nodes, P_1d, Q_1d, P, Q; 33*940a72f1SSebastian Grimberg const CeedScalar *d_u; 34*940a72f1SSebastian Grimberg CeedScalar *d_v; 3538293ee6SJeremy L Thompson CeedBasis_Magma *impl; 3638293ee6SJeremy L Thompson 3738293ee6SJeremy L Thompson CeedCallBackend(CeedBasisGetCeed(basis, &ceed)); 38*940a72f1SSebastian Grimberg CeedCallBackend(CeedGetData(ceed, &data)); 39*940a72f1SSebastian Grimberg CeedCallBackend(CeedBasisGetData(basis, &impl)); 4038293ee6SJeremy L Thompson CeedCallBackend(CeedBasisGetDimension(basis, &dim)); 4138293ee6SJeremy L Thompson CeedCallBackend(CeedBasisGetNumComponents(basis, &num_comp)); 42*940a72f1SSebastian Grimberg CeedCallBackend(CeedBasisGetNumNodes(basis, &num_nodes)); 4338293ee6SJeremy L Thompson CeedCallBackend(CeedBasisGetNumNodes1D(basis, &P_1d)); 4438293ee6SJeremy L Thompson CeedCallBackend(CeedBasisGetNumQuadraturePoints1D(basis, &Q_1d)); 45*940a72f1SSebastian Grimberg P = P_1d; 46*940a72f1SSebastian Grimberg Q = Q_1d; 4738293ee6SJeremy L Thompson if (t_mode == CEED_TRANSPOSE) { 4838293ee6SJeremy L Thompson P = Q_1d; 4938293ee6SJeremy L Thompson Q = P_1d; 507f5b9731SStan Tomov } 517f5b9731SStan Tomov 52*940a72f1SSebastian Grimberg // Read vectors 53*940a72f1SSebastian Grimberg if (u != CEED_VECTOR_NONE) CeedCallBackend(CeedVectorGetArrayRead(u, CEED_MEM_DEVICE, &d_u)); 54*940a72f1SSebastian Grimberg else CeedCheck(e_mode == CEED_EVAL_WEIGHT, ceed, CEED_ERROR_BACKEND, "An input vector is required for this CeedEvalMode"); 55*940a72f1SSebastian Grimberg CeedCallBackend(CeedVectorGetArrayWrite(v, CEED_MEM_DEVICE, &d_v)); 56*940a72f1SSebastian Grimberg 57*940a72f1SSebastian Grimberg // Clear v for transpose operation 58*940a72f1SSebastian Grimberg if (t_mode == CEED_TRANSPOSE) { 59*940a72f1SSebastian Grimberg CeedSize length; 60*940a72f1SSebastian Grimberg 61*940a72f1SSebastian Grimberg CeedCallBackend(CeedVectorGetLength(v, &length)); 62*940a72f1SSebastian Grimberg if (CEED_SCALAR_TYPE == CEED_SCALAR_FP32) { 63*940a72f1SSebastian Grimberg magmablas_slaset(MagmaFull, length, 1, 0.0, 0.0, (float *)d_v, length, data->queue); 64*940a72f1SSebastian Grimberg } else { 65*940a72f1SSebastian Grimberg magmablas_dlaset(MagmaFull, length, 1, 0.0, 0.0, (double *)d_v, length, data->queue); 66*940a72f1SSebastian Grimberg } 67*940a72f1SSebastian Grimberg ceed_magma_queue_sync(data->queue); 68*940a72f1SSebastian Grimberg } 69*940a72f1SSebastian Grimberg 70*940a72f1SSebastian Grimberg // Apply basis operation 71*940a72f1SSebastian Grimberg switch (e_mode) { 72*940a72f1SSebastian Grimberg case CEED_EVAL_INTERP: { 737f5b9731SStan Tomov // Define element sizes for dofs/quad 7438293ee6SJeremy L Thompson CeedInt elem_qpts_size = CeedIntPow(Q_1d, dim); 7538293ee6SJeremy L Thompson CeedInt elem_dofs_size = CeedIntPow(P_1d, dim); 767f5b9731SStan Tomov 777f5b9731SStan Tomov // E-vector ordering -------------- Q-vector ordering 78868539c2SNatalie Beams // component component 79868539c2SNatalie Beams // elem elem 807f5b9731SStan Tomov // node node 817f5b9731SStan Tomov 827f5b9731SStan Tomov // --- Define strides for NOTRANSPOSE mode: --- 83*940a72f1SSebastian Grimberg // Input (d_u) is E-vector, output (d_v) is Q-vector 847f5b9731SStan Tomov 857f5b9731SStan Tomov // Element strides 8638293ee6SJeremy L Thompson CeedInt u_elem_stride = elem_dofs_size; 8738293ee6SJeremy L Thompson CeedInt v_elem_stride = elem_qpts_size; 887f5b9731SStan Tomov // Component strides 8938293ee6SJeremy L Thompson CeedInt u_comp_stride = num_elem * elem_dofs_size; 9038293ee6SJeremy L Thompson CeedInt v_comp_stride = num_elem * elem_qpts_size; 9138293ee6SJeremy L Thompson if (t_mode == CEED_TRANSPOSE) { 92*940a72f1SSebastian Grimberg // Input (d_u) is Q-vector, output (d_v) is E-vector 937f5b9731SStan Tomov // Element strides 9438293ee6SJeremy L Thompson v_elem_stride = elem_dofs_size; 9538293ee6SJeremy L Thompson u_elem_stride = elem_qpts_size; 967f5b9731SStan Tomov // Component strides 9738293ee6SJeremy L Thompson v_comp_stride = num_elem * elem_dofs_size; 9838293ee6SJeremy L Thompson u_comp_stride = num_elem * elem_qpts_size; 997f5b9731SStan Tomov } 10038293ee6SJeremy L Thompson CeedInt num_threads = 1; 10138293ee6SJeremy L Thompson CeedInt num_t_col = 1; 10238293ee6SJeremy L Thompson CeedInt shared_mem = 0; 10338293ee6SJeremy L Thompson CeedInt max_P_Q = CeedIntMax(P, Q); 104f6af633fSnbeams 105f6af633fSnbeams switch (dim) { 106f6af633fSnbeams case 1: 10738293ee6SJeremy L Thompson num_threads = max_P_Q; 10838293ee6SJeremy L Thompson num_t_col = MAGMA_BASIS_NTCOL(num_threads, MAGMA_MAXTHREADS_1D); 10938293ee6SJeremy L Thompson shared_mem += sizeof(CeedScalar) * num_t_col * (num_comp * (1 * P + 1 * Q)); 11038293ee6SJeremy L Thompson shared_mem += sizeof(CeedScalar) * (P * Q); 111f6af633fSnbeams break; 112f6af633fSnbeams case 2: 11338293ee6SJeremy L Thompson num_threads = max_P_Q; 11438293ee6SJeremy L Thompson num_t_col = MAGMA_BASIS_NTCOL(num_threads, MAGMA_MAXTHREADS_2D); 11538293ee6SJeremy L Thompson shared_mem += P * Q * sizeof(CeedScalar); // for sT 116*940a72f1SSebastian Grimberg // for reforming rU we need P x P, and for the intermediate output we need P x Q 117*940a72f1SSebastian Grimberg shared_mem += num_t_col * (P * max_P_Q * sizeof(CeedScalar)); 118f6af633fSnbeams break; 119f6af633fSnbeams case 3: 12038293ee6SJeremy L Thompson num_threads = max_P_Q * max_P_Q; 12138293ee6SJeremy L Thompson num_t_col = MAGMA_BASIS_NTCOL(num_threads, MAGMA_MAXTHREADS_3D); 12238293ee6SJeremy L Thompson shared_mem += sizeof(CeedScalar) * (P * Q); // for sT 123*940a72f1SSebastian Grimberg // rU needs P^2 x P, the intermediate output needs max(P^2 x Q, P x Q^2) 124*940a72f1SSebastian Grimberg shared_mem += sizeof(CeedScalar) * num_t_col * (CeedIntMax(P * P * max_P_Q, P * Q * Q)); 125*940a72f1SSebastian Grimberg break; 126f6af633fSnbeams } 127*940a72f1SSebastian Grimberg CeedInt grid = CeedDivUpInt(num_elem, num_t_col); 128*940a72f1SSebastian Grimberg void *args[] = {&impl->d_interp_1d, &d_u, &u_elem_stride, &u_comp_stride, &d_v, &v_elem_stride, &v_comp_stride, &num_elem}; 129f6af633fSnbeams 13038293ee6SJeremy L Thompson if (t_mode == CEED_TRANSPOSE) { 131*940a72f1SSebastian Grimberg CeedCallBackend(CeedRunKernelDimSharedMagma(ceed, impl->InterpTranspose, grid, num_threads, num_t_col, 1, shared_mem, args)); 132f6af633fSnbeams } else { 133*940a72f1SSebastian Grimberg CeedCallBackend(CeedRunKernelDimSharedMagma(ceed, impl->Interp, grid, num_threads, num_t_col, 1, shared_mem, args)); 134f6af633fSnbeams } 1352b730f8bSJeremy L Thompson } break; 1363513a710Sjeremylt case CEED_EVAL_GRAD: { 1377f5b9731SStan Tomov // Define element sizes for dofs/quad 13838293ee6SJeremy L Thompson CeedInt elem_qpts_size = CeedIntPow(Q_1d, dim); 13938293ee6SJeremy L Thompson CeedInt elem_dofs_size = CeedIntPow(P_1d, dim); 1407f5b9731SStan Tomov 141*940a72f1SSebastian Grimberg // In CEED_NOTRANSPOSE mode: 142*940a72f1SSebastian Grimberg // d_u is (P^dim x nc), column-major layout (nc = num_comp) 143*940a72f1SSebastian Grimberg // d_v is (Q^dim x nc x dim), column-major layout (nc = num_comp) 144*940a72f1SSebastian Grimberg // In CEED_TRANSPOSE mode, the sizes of d_u and d_v are switched. 145*940a72f1SSebastian Grimberg 1467f5b9731SStan Tomov // E-vector ordering -------------- Q-vector ordering 1477f5b9731SStan Tomov // dim 148868539c2SNatalie Beams // component component 149868539c2SNatalie Beams // elem elem 1507f5b9731SStan Tomov // node node 1517f5b9731SStan Tomov 1527f5b9731SStan Tomov // --- Define strides for NOTRANSPOSE mode: --- 153*940a72f1SSebastian Grimberg // Input (d_u) is E-vector, output (d_v) is Q-vector 1547f5b9731SStan Tomov 1557f5b9731SStan Tomov // Element strides 15638293ee6SJeremy L Thompson CeedInt u_elem_stride = elem_dofs_size; 15738293ee6SJeremy L Thompson CeedInt v_elem_stride = elem_qpts_size; 1587f5b9731SStan Tomov // Component strides 15938293ee6SJeremy L Thompson CeedInt u_comp_stride = num_elem * elem_dofs_size; 16038293ee6SJeremy L Thompson CeedInt v_comp_stride = num_elem * elem_qpts_size; 1617f5b9731SStan Tomov // Dimension strides 16238293ee6SJeremy L Thompson CeedInt u_dim_stride = 0; 16338293ee6SJeremy L Thompson CeedInt v_dim_stride = num_elem * elem_qpts_size * num_comp; 16438293ee6SJeremy L Thompson if (t_mode == CEED_TRANSPOSE) { 165*940a72f1SSebastian Grimberg // Input (d_u) is Q-vector, output (d_v) is E-vector 1667f5b9731SStan Tomov // Element strides 16738293ee6SJeremy L Thompson v_elem_stride = elem_dofs_size; 16838293ee6SJeremy L Thompson u_elem_stride = elem_qpts_size; 1697f5b9731SStan Tomov // Component strides 17038293ee6SJeremy L Thompson v_comp_stride = num_elem * elem_dofs_size; 17138293ee6SJeremy L Thompson u_comp_stride = num_elem * elem_qpts_size; 1727f5b9731SStan Tomov // Dimension strides 17338293ee6SJeremy L Thompson v_dim_stride = 0; 17438293ee6SJeremy L Thompson u_dim_stride = num_elem * elem_qpts_size * num_comp; 1757f5b9731SStan Tomov } 17638293ee6SJeremy L Thompson CeedInt num_threads = 1; 17738293ee6SJeremy L Thompson CeedInt num_t_col = 1; 17838293ee6SJeremy L Thompson CeedInt shared_mem = 0; 17938293ee6SJeremy L Thompson CeedInt max_P_Q = CeedIntMax(P, Q); 180f6af633fSnbeams 181f6af633fSnbeams switch (dim) { 182f6af633fSnbeams case 1: 18338293ee6SJeremy L Thompson num_threads = max_P_Q; 18438293ee6SJeremy L Thompson num_t_col = MAGMA_BASIS_NTCOL(num_threads, MAGMA_MAXTHREADS_1D); 18538293ee6SJeremy L Thompson shared_mem += sizeof(CeedScalar) * num_t_col * (num_comp * (1 * P + 1 * Q)); 18638293ee6SJeremy L Thompson shared_mem += sizeof(CeedScalar) * (P * Q); 187f6af633fSnbeams break; 188f6af633fSnbeams case 2: 18938293ee6SJeremy L Thompson num_threads = max_P_Q; 19038293ee6SJeremy L Thompson num_t_col = MAGMA_BASIS_NTCOL(num_threads, MAGMA_MAXTHREADS_2D); 19138293ee6SJeremy L Thompson shared_mem += sizeof(CeedScalar) * 2 * P * Q; // for sTinterp and sTgrad 192*940a72f1SSebastian Grimberg // for reforming rU we need P x P, and for the intermediate output we need P x Q 193*940a72f1SSebastian Grimberg shared_mem += sizeof(CeedScalar) * num_t_col * (P * max_P_Q); 194f6af633fSnbeams break; 195f6af633fSnbeams case 3: 19638293ee6SJeremy L Thompson num_threads = max_P_Q * max_P_Q; 19738293ee6SJeremy L Thompson num_t_col = MAGMA_BASIS_NTCOL(num_threads, MAGMA_MAXTHREADS_3D); 19838293ee6SJeremy L Thompson shared_mem += sizeof(CeedScalar) * 2 * P * Q; // for sTinterp and sTgrad 199*940a72f1SSebastian Grimberg // rU needs P^2 x P, the intermediate outputs need (P^2 x Q + P x Q^2) 200*940a72f1SSebastian Grimberg shared_mem += sizeof(CeedScalar) * num_t_col * CeedIntMax(P * P * P, (P * P * Q) + (P * Q * Q)); 201*940a72f1SSebastian Grimberg break; 202f6af633fSnbeams } 203*940a72f1SSebastian Grimberg CeedInt grid = CeedDivUpInt(num_elem, num_t_col); 204*940a72f1SSebastian Grimberg void *args[] = {&impl->d_interp_1d, &impl->d_grad_1d, &d_u, &u_elem_stride, &u_comp_stride, &u_dim_stride, &d_v, 20538293ee6SJeremy L Thompson &v_elem_stride, &v_comp_stride, &v_dim_stride, &num_elem}; 206f6af633fSnbeams 20738293ee6SJeremy L Thompson if (t_mode == CEED_TRANSPOSE) { 208*940a72f1SSebastian Grimberg CeedCallBackend(CeedRunKernelDimSharedMagma(ceed, impl->GradTranspose, grid, num_threads, num_t_col, 1, shared_mem, args)); 209f6af633fSnbeams } else { 210*940a72f1SSebastian Grimberg CeedCallBackend(CeedRunKernelDimSharedMagma(ceed, impl->Grad, grid, num_threads, num_t_col, 1, shared_mem, args)); 211f6af633fSnbeams } 2122b730f8bSJeremy L Thompson } break; 2133513a710Sjeremylt case CEED_EVAL_WEIGHT: { 214*940a72f1SSebastian Grimberg CeedCheck(t_mode != CEED_TRANSPOSE, ceed, CEED_ERROR_BACKEND, "CEED_EVAL_WEIGHT incompatible with CEED_TRANSPOSE"); 21538293ee6SJeremy L Thompson CeedInt elem_dofs_size = CeedIntPow(Q, dim); 21638293ee6SJeremy L Thompson CeedInt num_threads = 1; 21738293ee6SJeremy L Thompson CeedInt num_t_col = 1; 21838293ee6SJeremy L Thompson CeedInt shared_mem = 0; 219f6af633fSnbeams 220f6af633fSnbeams switch (dim) { 221f6af633fSnbeams case 1: 22238293ee6SJeremy L Thompson num_threads = Q; 22338293ee6SJeremy L Thompson num_t_col = MAGMA_BASIS_NTCOL(num_threads, MAGMA_MAXTHREADS_1D); 22438293ee6SJeremy L Thompson shared_mem += sizeof(CeedScalar) * Q; // for d_q_weight_1d 22538293ee6SJeremy L Thompson shared_mem += sizeof(CeedScalar) * num_t_col * Q; // for output 226f6af633fSnbeams break; 227f6af633fSnbeams case 2: 22838293ee6SJeremy L Thompson num_threads = Q; 22938293ee6SJeremy L Thompson num_t_col = MAGMA_BASIS_NTCOL(num_threads, MAGMA_MAXTHREADS_2D); 23038293ee6SJeremy L Thompson shared_mem += sizeof(CeedScalar) * Q; // for d_q_weight_1d 231f6af633fSnbeams break; 232f6af633fSnbeams case 3: 23338293ee6SJeremy L Thompson num_threads = Q * Q; 23438293ee6SJeremy L Thompson num_t_col = MAGMA_BASIS_NTCOL(num_threads, MAGMA_MAXTHREADS_3D); 23538293ee6SJeremy L Thompson shared_mem += sizeof(CeedScalar) * Q; // for d_q_weight_1d 236*940a72f1SSebastian Grimberg break; 237f6af633fSnbeams } 238*940a72f1SSebastian Grimberg CeedInt grid = CeedDivUpInt(num_elem, num_t_col); 239*940a72f1SSebastian Grimberg void *args[] = {&impl->d_q_weight_1d, &d_v, &elem_dofs_size, &num_elem}; 240f6af633fSnbeams 241*940a72f1SSebastian Grimberg CeedCallBackend(CeedRunKernelDimSharedMagma(ceed, impl->Weight, grid, num_threads, num_t_col, 1, shared_mem, args)); 2422b730f8bSJeremy L Thompson } break; 2433513a710Sjeremylt // LCOV_EXCL_START 2443513a710Sjeremylt case CEED_EVAL_DIV: 245e15f9bd0SJeremy L Thompson return CeedError(ceed, CEED_ERROR_BACKEND, "CEED_EVAL_DIV not supported"); 2463513a710Sjeremylt case CEED_EVAL_CURL: 247e15f9bd0SJeremy L Thompson return CeedError(ceed, CEED_ERROR_BACKEND, "CEED_EVAL_CURL not supported"); 2483513a710Sjeremylt case CEED_EVAL_NONE: 2492b730f8bSJeremy L Thompson return CeedError(ceed, CEED_ERROR_BACKEND, "CEED_EVAL_NONE does not make sense in this context"); 2503513a710Sjeremylt // LCOV_EXCL_STOP 2513513a710Sjeremylt } 2527f5b9731SStan Tomov 253*940a72f1SSebastian Grimberg // Must sync to ensure completeness 254e0582403Sabdelfattah83 ceed_magma_queue_sync(data->queue); 255e0582403Sabdelfattah83 256*940a72f1SSebastian Grimberg // Restore vectors 25738293ee6SJeremy L Thompson if (e_mode != CEED_EVAL_WEIGHT) { 258*940a72f1SSebastian Grimberg CeedCallBackend(CeedVectorRestoreArrayRead(u, &d_u)); 2597f5b9731SStan Tomov } 260*940a72f1SSebastian Grimberg CeedCallBackend(CeedVectorRestoreArray(v, &d_v)); 261e15f9bd0SJeremy L Thompson return CEED_ERROR_SUCCESS; 2627f5b9731SStan Tomov } 2637f5b9731SStan Tomov 264*940a72f1SSebastian Grimberg //------------------------------------------------------------------------------ 265*940a72f1SSebastian Grimberg // Basis apply - non-tensor 266*940a72f1SSebastian Grimberg //------------------------------------------------------------------------------ 267*940a72f1SSebastian Grimberg static int CeedBasisApplyNonTensor_Magma(CeedBasis basis, CeedInt num_elem, CeedTransposeMode t_mode, CeedEvalMode e_mode, CeedVector u, 268*940a72f1SSebastian Grimberg CeedVector v) { 269868539c2SNatalie Beams Ceed ceed; 270e0582403Sabdelfattah83 Ceed_Magma *data; 271*940a72f1SSebastian Grimberg CeedInt dim, num_comp, num_nodes, num_qpts, P, Q, N; 272*940a72f1SSebastian Grimberg const CeedScalar *d_u; 273*940a72f1SSebastian Grimberg CeedScalar *d_v; 27438293ee6SJeremy L Thompson CeedBasisNonTensor_Magma *impl; 27538293ee6SJeremy L Thompson 27638293ee6SJeremy L Thompson CeedCallBackend(CeedBasisGetCeed(basis, &ceed)); 27738293ee6SJeremy L Thompson CeedCallBackend(CeedGetData(ceed, &data)); 278*940a72f1SSebastian Grimberg CeedCallBackend(CeedBasisGetData(basis, &impl)); 27938293ee6SJeremy L Thompson CeedCallBackend(CeedBasisGetDimension(basis, &dim)); 28038293ee6SJeremy L Thompson CeedCallBackend(CeedBasisGetNumComponents(basis, &num_comp)); 281*940a72f1SSebastian Grimberg CeedCallBackend(CeedBasisGetNumNodes(basis, &num_nodes)); 28238293ee6SJeremy L Thompson CeedCallBackend(CeedBasisGetNumQuadraturePoints(basis, &num_qpts)); 283*940a72f1SSebastian Grimberg P = num_nodes; 284*940a72f1SSebastian Grimberg Q = num_qpts; 285*940a72f1SSebastian Grimberg N = num_elem * num_comp; 28638293ee6SJeremy L Thompson 287*940a72f1SSebastian Grimberg // Read vectors 288*940a72f1SSebastian Grimberg if (u != CEED_VECTOR_NONE) CeedCallBackend(CeedVectorGetArrayRead(u, CEED_MEM_DEVICE, &d_u)); 28938293ee6SJeremy L Thompson else CeedCheck(e_mode == CEED_EVAL_WEIGHT, ceed, CEED_ERROR_BACKEND, "An input vector is required for this CeedEvalMode"); 290*940a72f1SSebastian Grimberg CeedCallBackend(CeedVectorGetArrayWrite(v, CEED_MEM_DEVICE, &d_v)); 291868539c2SNatalie Beams 292*940a72f1SSebastian Grimberg // Clear v for transpose operation 29338293ee6SJeremy L Thompson if (t_mode == CEED_TRANSPOSE) { 2941f9221feSJeremy L Thompson CeedSize length; 29538293ee6SJeremy L Thompson 296*940a72f1SSebastian Grimberg CeedCallBackend(CeedVectorGetLength(v, &length)); 29780a9ef05SNatalie Beams if (CEED_SCALAR_TYPE == CEED_SCALAR_FP32) { 298*940a72f1SSebastian Grimberg magmablas_slaset(MagmaFull, length, 1, 0.0, 0.0, (float *)d_v, length, data->queue); 29980a9ef05SNatalie Beams } else { 300*940a72f1SSebastian Grimberg magmablas_dlaset(MagmaFull, length, 1, 0.0, 0.0, (double *)d_v, length, data->queue); 30180a9ef05SNatalie Beams } 302e0582403Sabdelfattah83 ceed_magma_queue_sync(data->queue); 303868539c2SNatalie Beams } 30480a9ef05SNatalie Beams 305*940a72f1SSebastian Grimberg // Apply basis operation 306*940a72f1SSebastian Grimberg if (e_mode != CEED_EVAL_WEIGHT) { 307*940a72f1SSebastian Grimberg if (P < MAGMA_NONTENSOR_CUSTOM_KERNEL_MAX_P && Q < MAGMA_NONTENSOR_CUSTOM_KERNEL_MAX_Q) { 308*940a72f1SSebastian Grimberg CeedInt n_array[MAGMA_NONTENSOR_KERNEL_INSTANCES] = {MAGMA_NONTENSOR_KERNEL_N_VALUES}; 309*940a72f1SSebastian Grimberg CeedInt iN = 0, diff = abs(n_array[iN] - N), idiff; 310*940a72f1SSebastian Grimberg CeedInt M = (t_mode == CEED_TRANSPOSE) ? P : Q, K = (t_mode == CEED_TRANSPOSE) ? Q : P; 31138293ee6SJeremy L Thompson 312023b8a51Sabdelfattah83 for (CeedInt in = iN + 1; in < MAGMA_NONTENSOR_KERNEL_INSTANCES; in++) { 313*940a72f1SSebastian Grimberg idiff = abs(n_array[in] - N); 314023b8a51Sabdelfattah83 if (idiff < diff) { 315023b8a51Sabdelfattah83 iN = in; 316023b8a51Sabdelfattah83 diff = idiff; 317868539c2SNatalie Beams } 31880a9ef05SNatalie Beams } 31980a9ef05SNatalie Beams 320*940a72f1SSebastian Grimberg // Compile kernels for N as needed 321*940a72f1SSebastian Grimberg if (!impl->NB_interp[iN]) { 322*940a72f1SSebastian Grimberg Ceed ceed_delegate; 323*940a72f1SSebastian Grimberg char *interp_kernel_path, *grad_kernel_path, *basis_kernel_source; 324*940a72f1SSebastian Grimberg magma_int_t arch = magma_getdevice_arch(); 32580a9ef05SNatalie Beams 326*940a72f1SSebastian Grimberg // Tuning parameters for NB 327*940a72f1SSebastian Grimberg impl->NB_interp[iN] = nontensor_rtc_get_nb(arch, 'n', 1, P, Q, n_array[iN]); 328*940a72f1SSebastian Grimberg impl->NB_interp_t[iN] = nontensor_rtc_get_nb(arch, 't', 1, P, Q, n_array[iN]); 329*940a72f1SSebastian Grimberg impl->NB_grad[iN] = nontensor_rtc_get_nb(arch, 'n', dim, P, Q, n_array[iN]); 330*940a72f1SSebastian Grimberg impl->NB_grad_t[iN] = nontensor_rtc_get_nb(arch, 't', dim, P, Q, n_array[iN]); 331023b8a51Sabdelfattah83 332*940a72f1SSebastian Grimberg // The RTC compilation code expects a Ceed with the common Ceed_Cuda or Ceed_Hip data 333*940a72f1SSebastian Grimberg CeedCallBackend(CeedGetDelegate(ceed, &ceed_delegate)); 334023b8a51Sabdelfattah83 335*940a72f1SSebastian Grimberg // Compile kernels 336*940a72f1SSebastian Grimberg CeedCallBackend(CeedGetJitAbsolutePath(ceed, "ceed/jit-source/magma/magma-basis-interp-nontensor.h", &interp_kernel_path)); 337*940a72f1SSebastian Grimberg CeedDebug256(ceed, CEED_DEBUG_COLOR_SUCCESS, "----- Loading Basis Kernel Source -----\n"); 338*940a72f1SSebastian Grimberg CeedCallBackend(CeedLoadSourceToBuffer(ceed, interp_kernel_path, &basis_kernel_source)); 339*940a72f1SSebastian Grimberg CeedCallBackend(CeedGetJitAbsolutePath(ceed, "ceed/jit-source/magma/magma-basis-grad-nontensor.h", &grad_kernel_path)); 340*940a72f1SSebastian Grimberg CeedCallBackend(CeedLoadSourceToInitializedBuffer(ceed, grad_kernel_path, &basis_kernel_source)); 341*940a72f1SSebastian Grimberg CeedDebug256(ceed, CEED_DEBUG_COLOR_SUCCESS, "----- Loading Basis Kernel Source Complete! -----\n"); 342*940a72f1SSebastian Grimberg CeedCallBackend(CeedCompileMagma(ceed_delegate, basis_kernel_source, &impl->module_interp[iN], 7, "BASIS_DIM", dim, "BASIS_P", P, "BASIS_Q", 343*940a72f1SSebastian Grimberg Q, "BASIS_NB_INTERP_N", impl->NB_interp[iN], "BASIS_NB_INTERP_T", impl->NB_interp_t[iN], "BASIS_NB_GRAD_N", 344*940a72f1SSebastian Grimberg impl->NB_grad[iN], "BASIS_NB_GRAD_T", impl->NB_grad_t[iN])); 345*940a72f1SSebastian Grimberg CeedCallBackend(CeedGetKernelMagma(ceed, impl->module_interp[iN], "magma_interp_nontensor_n", &impl->Interp[iN])); 346*940a72f1SSebastian Grimberg CeedCallBackend(CeedGetKernelMagma(ceed, impl->module_interp[iN], "magma_interp_nontensor_t", &impl->InterpTranspose[iN])); 347*940a72f1SSebastian Grimberg CeedCallBackend(CeedGetKernelMagma(ceed, impl->module_interp[iN], "magma_grad_nontensor_n", &impl->Grad[iN])); 348*940a72f1SSebastian Grimberg CeedCallBackend(CeedGetKernelMagma(ceed, impl->module_interp[iN], "magma_grad_nontensor_t", &impl->GradTranspose[iN])); 349*940a72f1SSebastian Grimberg CeedCallBackend(CeedFree(&interp_kernel_path)); 350*940a72f1SSebastian Grimberg CeedCallBackend(CeedFree(&grad_kernel_path)); 351*940a72f1SSebastian Grimberg CeedCallBackend(CeedFree(&basis_kernel_source)); 352*940a72f1SSebastian Grimberg } 353*940a72f1SSebastian Grimberg 354*940a72f1SSebastian Grimberg // Apply basis operation 355*940a72f1SSebastian Grimberg CeedInt num_t_col = MAGMA_BASIS_NTCOL(M, MAGMA_MAXTHREADS_1D); 356*940a72f1SSebastian Grimberg if (e_mode == CEED_EVAL_INTERP) { 357*940a72f1SSebastian Grimberg CeedInt NB = (t_mode == CEED_TRANSPOSE) ? impl->NB_interp_t[iN] : impl->NB_interp[iN]; 358*940a72f1SSebastian Grimberg CeedInt grid = CeedDivUpInt(N, NB * num_t_col); 359*940a72f1SSebastian Grimberg CeedInt shared_mem_A = (t_mode == CEED_TRANSPOSE) ? 0 : K * M * sizeof(CeedScalar); 360*940a72f1SSebastian Grimberg CeedInt shared_mem_B = num_t_col * K * NB * sizeof(CeedScalar); 361*940a72f1SSebastian Grimberg CeedInt shared_mem = (t_mode == CEED_TRANSPOSE) ? (shared_mem_A + shared_mem_B) : CeedIntMax(shared_mem_A, shared_mem_B); 362*940a72f1SSebastian Grimberg void *args[] = {&N, &impl->d_interp, &P, &d_u, &K, &d_v, &M}; 363*940a72f1SSebastian Grimberg 36438293ee6SJeremy L Thompson if (t_mode == CEED_TRANSPOSE) { 365*940a72f1SSebastian Grimberg CeedCallBackend(CeedRunKernelDimSharedMagma(ceed, impl->InterpTranspose[iN], grid, M, num_t_col, 1, shared_mem, args)); 36638293ee6SJeremy L Thompson } else { 367*940a72f1SSebastian Grimberg CeedCallBackend(CeedRunKernelDimSharedMagma(ceed, impl->Interp[iN], grid, M, num_t_col, 1, shared_mem, args)); 36838293ee6SJeremy L Thompson } 369*940a72f1SSebastian Grimberg } else if (e_mode == CEED_EVAL_GRAD) { 370*940a72f1SSebastian Grimberg CeedInt NB = (t_mode == CEED_TRANSPOSE) ? impl->NB_grad_t[iN] : impl->NB_grad[iN]; 371*940a72f1SSebastian Grimberg CeedInt grid = CeedDivUpInt(N, NB * num_t_col); 372*940a72f1SSebastian Grimberg CeedInt shared_mem = num_t_col * K * NB * sizeof(CeedScalar) + ((t_mode == CEED_TRANSPOSE) ? 0 : K * M * sizeof(CeedScalar)); 373*940a72f1SSebastian Grimberg void *args[] = {&N, &impl->d_grad, &P, &d_u, &K, &d_v, &M}; 37480a9ef05SNatalie Beams 37538293ee6SJeremy L Thompson if (t_mode == CEED_TRANSPOSE) { 376*940a72f1SSebastian Grimberg CeedCallBackend(CeedRunKernelDimSharedMagma(ceed, impl->GradTranspose[iN], grid, M, num_t_col, 1, shared_mem, args)); 377*940a72f1SSebastian Grimberg } else { 378*940a72f1SSebastian Grimberg CeedCallBackend(CeedRunKernelDimSharedMagma(ceed, impl->Grad[iN], grid, M, num_t_col, 1, shared_mem, args)); 37980a9ef05SNatalie Beams } 38080a9ef05SNatalie Beams } else { 381868539c2SNatalie Beams // LCOV_EXCL_START 382*940a72f1SSebastian Grimberg return CeedError(ceed, CEED_ERROR_BACKEND, "CEED_EVAL_DIV, CEED_EVAL_CURL not supported"); 383868539c2SNatalie Beams // LCOV_EXCL_STOP 384868539c2SNatalie Beams } 385*940a72f1SSebastian Grimberg } else { 386*940a72f1SSebastian Grimberg if (e_mode == CEED_EVAL_INTERP) { 387*940a72f1SSebastian Grimberg if (t_mode == CEED_TRANSPOSE) { 388*940a72f1SSebastian Grimberg magma_gemm_nontensor(MagmaNoTrans, MagmaNoTrans, P, N, Q, 1.0, impl->d_interp, P, d_u, Q, 0.0, d_v, P, data->queue); 389*940a72f1SSebastian Grimberg } else { 390*940a72f1SSebastian Grimberg magma_gemm_nontensor(MagmaTrans, MagmaNoTrans, Q, N, P, 1.0, impl->d_interp, P, d_u, P, 0.0, d_v, Q, data->queue); 391*940a72f1SSebastian Grimberg } 392*940a72f1SSebastian Grimberg } else if (e_mode == CEED_EVAL_GRAD) { 393*940a72f1SSebastian Grimberg if (t_mode == CEED_TRANSPOSE) { 394*940a72f1SSebastian Grimberg for (int d = 0; d < dim; d++) { 395*940a72f1SSebastian Grimberg const CeedScalar beta = (d > 0) ? 1.0 : 0.0; 396*940a72f1SSebastian Grimberg magma_gemm_nontensor(MagmaNoTrans, MagmaNoTrans, P, N, Q, 1.0, impl->d_grad + d * P * Q, P, d_u + d * N * Q, Q, beta, d_v, P, 397*940a72f1SSebastian Grimberg data->queue); 398*940a72f1SSebastian Grimberg } 399*940a72f1SSebastian Grimberg } else { 400*940a72f1SSebastian Grimberg for (int d = 0; d < dim; d++) { 401*940a72f1SSebastian Grimberg magma_gemm_nontensor(MagmaTrans, MagmaNoTrans, Q, N, P, 1.0, impl->d_grad + d * P * Q, P, d_u, P, 0.0, d_v + d * N * Q, Q, data->queue); 402*940a72f1SSebastian Grimberg } 403*940a72f1SSebastian Grimberg } 404*940a72f1SSebastian Grimberg } else { 405*940a72f1SSebastian Grimberg // LCOV_EXCL_START 406*940a72f1SSebastian Grimberg return CeedError(ceed, CEED_ERROR_BACKEND, "CEED_EVAL_DIV, CEED_EVAL_CURL not supported"); 407*940a72f1SSebastian Grimberg // LCOV_EXCL_STOP 408*940a72f1SSebastian Grimberg } 409*940a72f1SSebastian Grimberg } 410*940a72f1SSebastian Grimberg } else { 411*940a72f1SSebastian Grimberg CeedCheck(t_mode != CEED_TRANSPOSE, ceed, CEED_ERROR_BACKEND, "CEED_EVAL_WEIGHT incompatible with CEED_TRANSPOSE"); 412*940a72f1SSebastian Grimberg CeedInt num_t_col = MAGMA_BASIS_NTCOL(Q, MAGMA_MAXTHREADS_1D); 413*940a72f1SSebastian Grimberg CeedInt grid = CeedDivUpInt(num_elem, num_t_col); 414*940a72f1SSebastian Grimberg CeedInt shared_mem = Q * sizeof(CeedScalar) + num_t_col * Q * sizeof(CeedScalar); 415*940a72f1SSebastian Grimberg void *args[] = {&num_elem, &impl->d_q_weight, &d_v, &Q}; 416868539c2SNatalie Beams 417*940a72f1SSebastian Grimberg CeedCallBackend(CeedRunKernelDimSharedMagma(ceed, impl->Weight, grid, Q, num_t_col, 1, shared_mem, args)); 418*940a72f1SSebastian Grimberg } 419*940a72f1SSebastian Grimberg 420*940a72f1SSebastian Grimberg // Must sync to ensure completeness 421e0582403Sabdelfattah83 ceed_magma_queue_sync(data->queue); 422e0582403Sabdelfattah83 423*940a72f1SSebastian Grimberg // Restore vectors 42438293ee6SJeremy L Thompson if (e_mode != CEED_EVAL_WEIGHT) { 425*940a72f1SSebastian Grimberg CeedCallBackend(CeedVectorRestoreArrayRead(u, &d_u)); 426868539c2SNatalie Beams } 427*940a72f1SSebastian Grimberg CeedCallBackend(CeedVectorRestoreArray(v, &d_v)); 428e15f9bd0SJeremy L Thompson return CEED_ERROR_SUCCESS; 429868539c2SNatalie Beams } 430868539c2SNatalie Beams 431*940a72f1SSebastian Grimberg //------------------------------------------------------------------------------ 432*940a72f1SSebastian Grimberg // Destroy tensor basis 433*940a72f1SSebastian Grimberg //------------------------------------------------------------------------------ 434*940a72f1SSebastian Grimberg static int CeedBasisDestroy_Magma(CeedBasis basis) { 435f6af633fSnbeams Ceed ceed; 43638293ee6SJeremy L Thompson CeedBasis_Magma *impl; 43738293ee6SJeremy L Thompson 4382b730f8bSJeremy L Thompson CeedCallBackend(CeedBasisGetCeed(basis, &ceed)); 439*940a72f1SSebastian Grimberg CeedCallBackend(CeedBasisGetData(basis, &impl)); 440e5f091ebSnbeams #ifdef CEED_MAGMA_USE_HIP 4412b730f8bSJeremy L Thompson CeedCallHip(ceed, hipModuleUnload(impl->module)); 442f6af633fSnbeams #else 4432b730f8bSJeremy L Thompson CeedCallCuda(ceed, cuModuleUnload(impl->module)); 444f6af633fSnbeams #endif 445*940a72f1SSebastian Grimberg CeedCallBackend(magma_free(impl->d_interp_1d)); 446*940a72f1SSebastian Grimberg CeedCallBackend(magma_free(impl->d_grad_1d)); 447*940a72f1SSebastian Grimberg CeedCallBackend(magma_free(impl->d_q_weight_1d)); 4482b730f8bSJeremy L Thompson CeedCallBackend(CeedFree(&impl)); 449e15f9bd0SJeremy L Thompson return CEED_ERROR_SUCCESS; 4507f5b9731SStan Tomov } 4517f5b9731SStan Tomov 452*940a72f1SSebastian Grimberg //------------------------------------------------------------------------------ 453*940a72f1SSebastian Grimberg // Destroy non-tensor basis 454*940a72f1SSebastian Grimberg //------------------------------------------------------------------------------ 455*940a72f1SSebastian Grimberg static int CeedBasisDestroyNonTensor_Magma(CeedBasis basis) { 456023b8a51Sabdelfattah83 Ceed ceed; 45738293ee6SJeremy L Thompson CeedBasisNonTensor_Magma *impl; 45838293ee6SJeremy L Thompson 459*940a72f1SSebastian Grimberg CeedCallBackend(CeedBasisGetCeed(basis, &ceed)); 46038293ee6SJeremy L Thompson CeedCallBackend(CeedBasisGetData(basis, &impl)); 461*940a72f1SSebastian Grimberg #ifdef CEED_MAGMA_USE_HIP 462*940a72f1SSebastian Grimberg CeedCallHip(ceed, hipModuleUnload(impl->module_weight)); 463*940a72f1SSebastian Grimberg #else 464*940a72f1SSebastian Grimberg CeedCallCuda(ceed, cuModuleUnload(impl->module_weight)); 465*940a72f1SSebastian Grimberg #endif 466*940a72f1SSebastian Grimberg for (CeedInt in = 0; in < MAGMA_NONTENSOR_KERNEL_INSTANCES; in++) { 467*940a72f1SSebastian Grimberg if (impl->module_interp[in]) { 468*940a72f1SSebastian Grimberg #ifdef CEED_MAGMA_USE_HIP 469*940a72f1SSebastian Grimberg CeedCallHip(ceed, hipModuleUnload(impl->module_interp[in])); 470*940a72f1SSebastian Grimberg #else 471*940a72f1SSebastian Grimberg CeedCallCuda(ceed, cuModuleUnload(impl->module_interp[in])); 472*940a72f1SSebastian Grimberg #endif 473*940a72f1SSebastian Grimberg } 474*940a72f1SSebastian Grimberg } 47538293ee6SJeremy L Thompson CeedCallBackend(magma_free(impl->d_interp)); 47638293ee6SJeremy L Thompson CeedCallBackend(magma_free(impl->d_grad)); 47738293ee6SJeremy L Thompson CeedCallBackend(magma_free(impl->d_q_weight)); 4782b730f8bSJeremy L Thompson CeedCallBackend(CeedFree(&impl)); 479e15f9bd0SJeremy L Thompson return CEED_ERROR_SUCCESS; 480868539c2SNatalie Beams } 481868539c2SNatalie Beams 482*940a72f1SSebastian Grimberg //------------------------------------------------------------------------------ 483*940a72f1SSebastian Grimberg // Create tensor 484*940a72f1SSebastian Grimberg //------------------------------------------------------------------------------ 485*940a72f1SSebastian Grimberg int CeedBasisCreateTensorH1_Magma(CeedInt dim, CeedInt P_1d, CeedInt Q_1d, const CeedScalar *interp_1d, const CeedScalar *grad_1d, 48638293ee6SJeremy L Thompson const CeedScalar *q_ref_1d, const CeedScalar *q_weight_1d, CeedBasis basis) { 48738293ee6SJeremy L Thompson Ceed ceed, ceed_delegate; 48838293ee6SJeremy L Thompson Ceed_Magma *data; 489*940a72f1SSebastian Grimberg char *interp_kernel_path, *grad_kernel_path, *weight_kernel_path, *basis_kernel_source; 490*940a72f1SSebastian Grimberg CeedInt num_comp; 4917f5b9731SStan Tomov CeedBasis_Magma *impl; 49238293ee6SJeremy L Thompson 4932b730f8bSJeremy L Thompson CeedCallBackend(CeedBasisGetCeed(basis, &ceed)); 4942b730f8bSJeremy L Thompson CeedCallBackend(CeedGetData(ceed, &data)); 495*940a72f1SSebastian Grimberg CeedCallBackend(CeedCalloc(1, &impl)); 496e0582403Sabdelfattah83 497*940a72f1SSebastian Grimberg // Copy basis data to GPU 498*940a72f1SSebastian Grimberg CeedCallBackend(magma_malloc((void **)&impl->d_q_weight_1d, Q_1d * sizeof(q_weight_1d[0]))); 499*940a72f1SSebastian Grimberg magma_setvector(Q_1d, sizeof(q_weight_1d[0]), q_weight_1d, 1, impl->d_q_weight_1d, 1, data->queue); 50038293ee6SJeremy L Thompson CeedCallBackend(magma_malloc((void **)&impl->d_interp_1d, Q_1d * P_1d * sizeof(interp_1d[0]))); 50138293ee6SJeremy L Thompson magma_setvector(Q_1d * P_1d, sizeof(interp_1d[0]), interp_1d, 1, impl->d_interp_1d, 1, data->queue); 50238293ee6SJeremy L Thompson CeedCallBackend(magma_malloc((void **)&impl->d_grad_1d, Q_1d * P_1d * sizeof(grad_1d[0]))); 50338293ee6SJeremy L Thompson magma_setvector(Q_1d * P_1d, sizeof(grad_1d[0]), grad_1d, 1, impl->d_grad_1d, 1, data->queue); 5047f5b9731SStan Tomov 505*940a72f1SSebastian Grimberg // The RTC compilation code expects a Ceed with the common Ceed_Cuda or Ceed_Hip data 506*940a72f1SSebastian Grimberg CeedCallBackend(CeedGetDelegate(ceed, &ceed_delegate)); 507*940a72f1SSebastian Grimberg 508*940a72f1SSebastian Grimberg // Compile kernels 509*940a72f1SSebastian Grimberg CeedCallBackend(CeedBasisGetNumComponents(basis, &num_comp)); 510*940a72f1SSebastian Grimberg { 511*940a72f1SSebastian Grimberg char *interp_kernel_name_base = "ceed/jit-source/magma/magma-basis-interp"; 512*940a72f1SSebastian Grimberg CeedInt interp_kernel_name_len = strlen(interp_kernel_name_base) + 6; 513*940a72f1SSebastian Grimberg char interp_kernel_name[interp_kernel_name_len]; 514*940a72f1SSebastian Grimberg 515*940a72f1SSebastian Grimberg snprintf(interp_kernel_name, interp_kernel_name_len, "%s-%" CeedInt_FMT "d.h", interp_kernel_name_base, dim); 516*940a72f1SSebastian Grimberg CeedCallBackend(CeedGetJitAbsolutePath(ceed, interp_kernel_name, &interp_kernel_path)); 517*940a72f1SSebastian Grimberg } 518*940a72f1SSebastian Grimberg CeedDebug256(ceed, CEED_DEBUG_COLOR_SUCCESS, "----- Loading Basis Kernel Source -----\n"); 519*940a72f1SSebastian Grimberg CeedCallBackend(CeedLoadSourceToBuffer(ceed, interp_kernel_path, &basis_kernel_source)); 520*940a72f1SSebastian Grimberg { 521*940a72f1SSebastian Grimberg char *grad_kernel_name_base = "ceed/jit-source/magma/magma-basis-grad"; 522*940a72f1SSebastian Grimberg CeedInt grad_kernel_name_len = strlen(grad_kernel_name_base) + 6; 523*940a72f1SSebastian Grimberg char grad_kernel_name[grad_kernel_name_len]; 524*940a72f1SSebastian Grimberg 525*940a72f1SSebastian Grimberg snprintf(grad_kernel_name, grad_kernel_name_len, "%s-%" CeedInt_FMT "d.h", grad_kernel_name_base, dim); 526*940a72f1SSebastian Grimberg CeedCallBackend(CeedGetJitAbsolutePath(ceed, grad_kernel_name, &grad_kernel_path)); 527*940a72f1SSebastian Grimberg } 528*940a72f1SSebastian Grimberg CeedCallBackend(CeedLoadSourceToInitializedBuffer(ceed, grad_kernel_path, &basis_kernel_source)); 529*940a72f1SSebastian Grimberg { 530*940a72f1SSebastian Grimberg char *weight_kernel_name_base = "ceed/jit-source/magma/magma-basis-weight"; 531*940a72f1SSebastian Grimberg CeedInt weight_kernel_name_len = strlen(weight_kernel_name_base) + 6; 532*940a72f1SSebastian Grimberg char weight_kernel_name[weight_kernel_name_len]; 533*940a72f1SSebastian Grimberg 534*940a72f1SSebastian Grimberg snprintf(weight_kernel_name, weight_kernel_name_len, "%s-%" CeedInt_FMT "d.h", weight_kernel_name_base, dim); 535*940a72f1SSebastian Grimberg CeedCallBackend(CeedGetJitAbsolutePath(ceed, weight_kernel_name, &weight_kernel_path)); 536*940a72f1SSebastian Grimberg } 537*940a72f1SSebastian Grimberg CeedCallBackend(CeedLoadSourceToInitializedBuffer(ceed, weight_kernel_path, &basis_kernel_source)); 538*940a72f1SSebastian Grimberg CeedDebug256(ceed, CEED_DEBUG_COLOR_SUCCESS, "----- Loading Basis Kernel Source Complete! -----\n"); 539*940a72f1SSebastian Grimberg CeedCallBackend(CeedCompileMagma(ceed_delegate, basis_kernel_source, &impl->module, 5, "BASIS_DIM", dim, "BASIS_NUM_COMP", num_comp, "BASIS_P", 540*940a72f1SSebastian Grimberg P_1d, "BASIS_Q", Q_1d, "BASIS_MAX_P_Q", CeedIntMax(P_1d, Q_1d))); 541*940a72f1SSebastian Grimberg switch (dim) { 542*940a72f1SSebastian Grimberg case 1: 543*940a72f1SSebastian Grimberg CeedCallBackend(CeedGetKernelMagma(ceed, impl->module, "magma_interpn_1d_kernel", &impl->Interp)); 544*940a72f1SSebastian Grimberg CeedCallBackend(CeedGetKernelMagma(ceed, impl->module, "magma_interpt_1d_kernel", &impl->InterpTranspose)); 545*940a72f1SSebastian Grimberg CeedCallBackend(CeedGetKernelMagma(ceed, impl->module, "magma_gradn_1d_kernel", &impl->Grad)); 546*940a72f1SSebastian Grimberg CeedCallBackend(CeedGetKernelMagma(ceed, impl->module, "magma_gradt_1d_kernel", &impl->GradTranspose)); 547*940a72f1SSebastian Grimberg CeedCallBackend(CeedGetKernelMagma(ceed, impl->module, "magma_weight_1d_kernel", &impl->Weight)); 548*940a72f1SSebastian Grimberg break; 549*940a72f1SSebastian Grimberg case 2: 550*940a72f1SSebastian Grimberg CeedCallBackend(CeedGetKernelMagma(ceed, impl->module, "magma_interpn_2d_kernel", &impl->Interp)); 551*940a72f1SSebastian Grimberg CeedCallBackend(CeedGetKernelMagma(ceed, impl->module, "magma_interpt_2d_kernel", &impl->InterpTranspose)); 552*940a72f1SSebastian Grimberg CeedCallBackend(CeedGetKernelMagma(ceed, impl->module, "magma_gradn_2d_kernel", &impl->Grad)); 553*940a72f1SSebastian Grimberg CeedCallBackend(CeedGetKernelMagma(ceed, impl->module, "magma_gradt_2d_kernel", &impl->GradTranspose)); 554*940a72f1SSebastian Grimberg CeedCallBackend(CeedGetKernelMagma(ceed, impl->module, "magma_weight_2d_kernel", &impl->Weight)); 555*940a72f1SSebastian Grimberg break; 556*940a72f1SSebastian Grimberg case 3: 557*940a72f1SSebastian Grimberg CeedCallBackend(CeedGetKernelMagma(ceed, impl->module, "magma_interpn_3d_kernel", &impl->Interp)); 558*940a72f1SSebastian Grimberg CeedCallBackend(CeedGetKernelMagma(ceed, impl->module, "magma_interpt_3d_kernel", &impl->InterpTranspose)); 559*940a72f1SSebastian Grimberg CeedCallBackend(CeedGetKernelMagma(ceed, impl->module, "magma_gradn_3d_kernel", &impl->Grad)); 560*940a72f1SSebastian Grimberg CeedCallBackend(CeedGetKernelMagma(ceed, impl->module, "magma_gradt_3d_kernel", &impl->GradTranspose)); 561*940a72f1SSebastian Grimberg CeedCallBackend(CeedGetKernelMagma(ceed, impl->module, "magma_weight_3d_kernel", &impl->Weight)); 562*940a72f1SSebastian Grimberg break; 563*940a72f1SSebastian Grimberg } 564*940a72f1SSebastian Grimberg CeedCallBackend(CeedFree(&interp_kernel_path)); 565*940a72f1SSebastian Grimberg CeedCallBackend(CeedFree(&grad_kernel_path)); 566*940a72f1SSebastian Grimberg CeedCallBackend(CeedFree(&weight_kernel_path)); 567*940a72f1SSebastian Grimberg CeedCallBackend(CeedFree(&basis_kernel_source)); 5687f5b9731SStan Tomov 5692b730f8bSJeremy L Thompson CeedCallBackend(CeedBasisSetData(basis, impl)); 570*940a72f1SSebastian Grimberg 571*940a72f1SSebastian Grimberg CeedCallBackend(CeedSetBackendFunction(ceed, "Basis", basis, "Apply", CeedBasisApply_Magma)); 572*940a72f1SSebastian Grimberg CeedCallBackend(CeedSetBackendFunction(ceed, "Basis", basis, "Destroy", CeedBasisDestroy_Magma)); 573e15f9bd0SJeremy L Thompson return CEED_ERROR_SUCCESS; 5747f5b9731SStan Tomov } 5757f5b9731SStan Tomov 576*940a72f1SSebastian Grimberg //------------------------------------------------------------------------------ 577*940a72f1SSebastian Grimberg // Create non-tensor H^1 578*940a72f1SSebastian Grimberg //------------------------------------------------------------------------------ 579*940a72f1SSebastian Grimberg int CeedBasisCreateH1_Magma(CeedElemTopology topo, CeedInt dim, CeedInt num_nodes, CeedInt num_qpts, const CeedScalar *interp, const CeedScalar *grad, 58038293ee6SJeremy L Thompson const CeedScalar *q_ref, const CeedScalar *q_weight, CeedBasis basis) { 58138293ee6SJeremy L Thompson Ceed ceed, ceed_delegate; 582e0582403Sabdelfattah83 Ceed_Magma *data; 583*940a72f1SSebastian Grimberg char *weight_kernel_path, *basis_kernel_source; 58438293ee6SJeremy L Thompson CeedBasisNonTensor_Magma *impl; 58538293ee6SJeremy L Thompson 58638293ee6SJeremy L Thompson CeedCallBackend(CeedBasisGetCeed(basis, &ceed)); 5872b730f8bSJeremy L Thompson CeedCallBackend(CeedGetData(ceed, &data)); 5882b730f8bSJeremy L Thompson CeedCallBackend(CeedCalloc(1, &impl)); 589023b8a51Sabdelfattah83 590*940a72f1SSebastian Grimberg // Copy basis data to GPU 59138293ee6SJeremy L Thompson CeedCallBackend(magma_malloc((void **)&impl->d_q_weight, num_qpts * sizeof(q_weight[0]))); 59238293ee6SJeremy L Thompson magma_setvector(num_qpts, sizeof(q_weight[0]), q_weight, 1, impl->d_q_weight, 1, data->queue); 593*940a72f1SSebastian Grimberg CeedCallBackend(magma_malloc((void **)&impl->d_interp, num_qpts * num_nodes * sizeof(interp[0]))); 594*940a72f1SSebastian Grimberg magma_setvector(num_qpts * num_nodes, sizeof(interp[0]), interp, 1, impl->d_interp, 1, data->queue); 595*940a72f1SSebastian Grimberg CeedCallBackend(magma_malloc((void **)&impl->d_grad, num_qpts * num_nodes * dim * sizeof(grad[0]))); 596*940a72f1SSebastian Grimberg magma_setvector(num_qpts * num_nodes * dim, sizeof(grad[0]), grad, 1, impl->d_grad, 1, data->queue); 597*940a72f1SSebastian Grimberg 598*940a72f1SSebastian Grimberg // The RTC compilation code expects a Ceed with the common Ceed_Cuda or Ceed_Hip data 599*940a72f1SSebastian Grimberg CeedCallBackend(CeedGetDelegate(ceed, &ceed_delegate)); 600*940a72f1SSebastian Grimberg 601*940a72f1SSebastian Grimberg // Compile weight kernel (the remainder of kernel compilation happens at first call to CeedBasisApply) 602*940a72f1SSebastian Grimberg CeedCallBackend(CeedGetJitAbsolutePath(ceed, "ceed/jit-source/magma/magma-basis-weight-nontensor.h", &weight_kernel_path)); 603*940a72f1SSebastian Grimberg CeedDebug256(ceed, CEED_DEBUG_COLOR_SUCCESS, "----- Loading Basis Kernel Source -----\n"); 604*940a72f1SSebastian Grimberg CeedCallBackend(CeedLoadSourceToBuffer(ceed, weight_kernel_path, &basis_kernel_source)); 605*940a72f1SSebastian Grimberg CeedDebug256(ceed, CEED_DEBUG_COLOR_SUCCESS, "----- Loading Basis Kernel Source Complete! -----\n"); 606*940a72f1SSebastian Grimberg CeedCallBackend(CeedCompileMagma(ceed_delegate, basis_kernel_source, &impl->module_weight, 1, "BASIS_Q", num_qpts)); 607*940a72f1SSebastian Grimberg CeedCallBackend(CeedGetKernelMagma(ceed, impl->module_weight, "magma_weight_nontensor", &impl->Weight)); 608*940a72f1SSebastian Grimberg CeedCallBackend(CeedFree(&weight_kernel_path)); 609*940a72f1SSebastian Grimberg CeedCallBackend(CeedFree(&basis_kernel_source)); 610868539c2SNatalie Beams 611023b8a51Sabdelfattah83 CeedCallBackend(CeedBasisSetData(basis, impl)); 612*940a72f1SSebastian Grimberg 613*940a72f1SSebastian Grimberg // Register backend functions 614*940a72f1SSebastian Grimberg CeedCallBackend(CeedSetBackendFunction(ceed, "Basis", basis, "Apply", CeedBasisApplyNonTensor_Magma)); 615*940a72f1SSebastian Grimberg CeedCallBackend(CeedSetBackendFunction(ceed, "Basis", basis, "Destroy", CeedBasisDestroyNonTensor_Magma)); 616e15f9bd0SJeremy L Thompson return CEED_ERROR_SUCCESS; 6177f5b9731SStan Tomov } 618*940a72f1SSebastian Grimberg 619*940a72f1SSebastian Grimberg //------------------------------------------------------------------------------ 620