xref: /libCEED/rust/libceed-sys/c-src/backends/hip-ref/ceed-hip-ref-basis.c (revision b2165e7a2e371018feedcb47974a027ed47e0487)
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.
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 <hip/hip_runtime.h>
122b730f8bSJeremy L Thompson 
1349aac155SJeremy L Thompson #include "../hip/ceed-hip-common.h"
140d0321e0SJeremy L Thompson #include "../hip/ceed-hip-compile.h"
152b730f8bSJeremy L Thompson #include "ceed-hip-ref.h"
160d0321e0SJeremy L Thompson 
170d0321e0SJeremy L Thompson //------------------------------------------------------------------------------
180d0321e0SJeremy L Thompson // Basis apply - tensor
190d0321e0SJeremy L Thompson //------------------------------------------------------------------------------
202b730f8bSJeremy L Thompson int CeedBasisApply_Hip(CeedBasis basis, const CeedInt num_elem, CeedTransposeMode t_mode, CeedEvalMode eval_mode, CeedVector u, CeedVector v) {
210d0321e0SJeremy L Thompson   Ceed ceed;
222b730f8bSJeremy L Thompson   CeedCallBackend(CeedBasisGetCeed(basis, &ceed));
230d0321e0SJeremy L Thompson   Ceed_Hip *ceed_Hip;
242b730f8bSJeremy L Thompson   CeedCallBackend(CeedGetData(ceed, &ceed_Hip));
250d0321e0SJeremy L Thompson   CeedBasis_Hip *data;
262b730f8bSJeremy L Thompson   CeedCallBackend(CeedBasisGetData(basis, &data));
27437930d1SJeremy L Thompson   const CeedInt transpose      = t_mode == CEED_TRANSPOSE;
28437930d1SJeremy L Thompson   const int     max_block_size = 64;
290d0321e0SJeremy L Thompson 
300d0321e0SJeremy L Thompson   // Read vectors
310d0321e0SJeremy L Thompson   const CeedScalar *d_u;
320d0321e0SJeremy L Thompson   CeedScalar       *d_v;
336574a04fSJeremy L Thompson   if (u != CEED_VECTOR_NONE) CeedCallBackend(CeedVectorGetArrayRead(u, CEED_MEM_DEVICE, &d_u));
346574a04fSJeremy L Thompson   else CeedCheck(eval_mode == CEED_EVAL_WEIGHT, ceed, CEED_ERROR_BACKEND, "An input vector is required for this CeedEvalMode");
352b730f8bSJeremy L Thompson   CeedCallBackend(CeedVectorGetArrayWrite(v, CEED_MEM_DEVICE, &d_v));
360d0321e0SJeremy L Thompson 
370d0321e0SJeremy L Thompson   // Clear v for transpose operation
38437930d1SJeremy L Thompson   if (t_mode == CEED_TRANSPOSE) {
391f9221feSJeremy L Thompson     CeedSize length;
402b730f8bSJeremy L Thompson     CeedCallBackend(CeedVectorGetLength(v, &length));
412b730f8bSJeremy L Thompson     CeedCallHip(ceed, hipMemset(d_v, 0, length * sizeof(CeedScalar)));
420d0321e0SJeremy L Thompson   }
43*b2165e7aSSebastian Grimberg   CeedInt Q_1d, dim;
44*b2165e7aSSebastian Grimberg   CeedCallBackend(CeedBasisGetNumQuadraturePoints1D(basis, &Q_1d));
45*b2165e7aSSebastian Grimberg   CeedCallBackend(CeedBasisGetDimension(basis, &dim));
460d0321e0SJeremy L Thompson 
470d0321e0SJeremy L Thompson   // Basis action
48437930d1SJeremy L Thompson   switch (eval_mode) {
490d0321e0SJeremy L Thompson     case CEED_EVAL_INTERP: {
502b730f8bSJeremy L Thompson       void         *interp_args[] = {(void *)&num_elem, (void *)&transpose, &data->d_interp_1d, &d_u, &d_v};
51*b2165e7aSSebastian Grimberg       const CeedInt block_size    = CeedIntMin(CeedIntPow(Q_1d, dim), max_block_size);
520d0321e0SJeremy L Thompson 
53eb7e6cafSJeremy L Thompson       CeedCallBackend(CeedRunKernel_Hip(ceed, data->Interp, num_elem, block_size, interp_args));
540d0321e0SJeremy L Thompson     } break;
550d0321e0SJeremy L Thompson     case CEED_EVAL_GRAD: {
562b730f8bSJeremy L Thompson       void         *grad_args[] = {(void *)&num_elem, (void *)&transpose, &data->d_interp_1d, &data->d_grad_1d, &d_u, &d_v};
57*b2165e7aSSebastian Grimberg       const CeedInt block_size  = max_block_size;
580d0321e0SJeremy L Thompson 
59eb7e6cafSJeremy L Thompson       CeedCallBackend(CeedRunKernel_Hip(ceed, data->Grad, num_elem, block_size, grad_args));
600d0321e0SJeremy L Thompson     } break;
610d0321e0SJeremy L Thompson     case CEED_EVAL_WEIGHT: {
62437930d1SJeremy L Thompson       void     *weight_args[] = {(void *)&num_elem, (void *)&data->d_q_weight_1d, &d_v};
63*b2165e7aSSebastian Grimberg       const int block_size_x  = Q_1d;
64*b2165e7aSSebastian Grimberg       const int block_size_y  = dim >= 2 ? Q_1d : 1;
650d0321e0SJeremy L Thompson 
66*b2165e7aSSebastian Grimberg       CeedCallBackend(CeedRunKernelDim_Hip(ceed, data->Weight, num_elem, block_size_x, block_size_y, 1, weight_args));
670d0321e0SJeremy L Thompson     } break;
680d0321e0SJeremy L Thompson     // LCOV_EXCL_START
690d0321e0SJeremy L Thompson     // Evaluate the divergence to/from the quadrature points
700d0321e0SJeremy L Thompson     case CEED_EVAL_DIV:
710d0321e0SJeremy L Thompson       return CeedError(ceed, CEED_ERROR_BACKEND, "CEED_EVAL_DIV not supported");
720d0321e0SJeremy L Thompson     // Evaluate the curl to/from the quadrature points
730d0321e0SJeremy L Thompson     case CEED_EVAL_CURL:
740d0321e0SJeremy L Thompson       return CeedError(ceed, CEED_ERROR_BACKEND, "CEED_EVAL_CURL not supported");
750d0321e0SJeremy L Thompson     // Take no action, BasisApply should not have been called
760d0321e0SJeremy L Thompson     case CEED_EVAL_NONE:
772b730f8bSJeremy L Thompson       return CeedError(ceed, CEED_ERROR_BACKEND, "CEED_EVAL_NONE does not make sense in this context");
780d0321e0SJeremy L Thompson       // LCOV_EXCL_STOP
790d0321e0SJeremy L Thompson   }
800d0321e0SJeremy L Thompson 
810d0321e0SJeremy L Thompson   // Restore vectors
82437930d1SJeremy L Thompson   if (eval_mode != CEED_EVAL_WEIGHT) {
832b730f8bSJeremy L Thompson     CeedCallBackend(CeedVectorRestoreArrayRead(u, &d_u));
840d0321e0SJeremy L Thompson   }
852b730f8bSJeremy L Thompson   CeedCallBackend(CeedVectorRestoreArray(v, &d_v));
860d0321e0SJeremy L Thompson   return CEED_ERROR_SUCCESS;
870d0321e0SJeremy L Thompson }
880d0321e0SJeremy L Thompson 
890d0321e0SJeremy L Thompson //------------------------------------------------------------------------------
900d0321e0SJeremy L Thompson // Basis apply - non-tensor
910d0321e0SJeremy L Thompson //------------------------------------------------------------------------------
922b730f8bSJeremy L Thompson int CeedBasisApplyNonTensor_Hip(CeedBasis basis, const CeedInt num_elem, CeedTransposeMode t_mode, CeedEvalMode eval_mode, CeedVector u,
932b730f8bSJeremy L Thompson                                 CeedVector v) {
940d0321e0SJeremy L Thompson   Ceed ceed;
952b730f8bSJeremy L Thompson   CeedCallBackend(CeedBasisGetCeed(basis, &ceed));
960d0321e0SJeremy L Thompson   Ceed_Hip *ceed_Hip;
972b730f8bSJeremy L Thompson   CeedCallBackend(CeedGetData(ceed, &ceed_Hip));
980d0321e0SJeremy L Thompson   CeedBasisNonTensor_Hip *data;
992b730f8bSJeremy L Thompson   CeedCallBackend(CeedBasisGetData(basis, &data));
100437930d1SJeremy L Thompson   CeedInt num_nodes, num_qpts;
1012b730f8bSJeremy L Thompson   CeedCallBackend(CeedBasisGetNumQuadraturePoints(basis, &num_qpts));
1022b730f8bSJeremy L Thompson   CeedCallBackend(CeedBasisGetNumNodes(basis, &num_nodes));
103437930d1SJeremy L Thompson   const CeedInt transpose       = t_mode == CEED_TRANSPOSE;
104*b2165e7aSSebastian Grimberg   int           elems_per_block = 1;
105*b2165e7aSSebastian Grimberg   int           grid            = num_elem / elems_per_block + ((num_elem / elems_per_block * elems_per_block < num_elem) ? 1 : 0);
1060d0321e0SJeremy L Thompson 
1070d0321e0SJeremy L Thompson   // Read vectors
1080d0321e0SJeremy L Thompson   const CeedScalar *d_u;
1090d0321e0SJeremy L Thompson   CeedScalar       *d_v;
110437930d1SJeremy L Thompson   if (eval_mode != CEED_EVAL_WEIGHT) {
1112b730f8bSJeremy L Thompson     CeedCallBackend(CeedVectorGetArrayRead(u, CEED_MEM_DEVICE, &d_u));
1120d0321e0SJeremy L Thompson   }
1132b730f8bSJeremy L Thompson   CeedCallBackend(CeedVectorGetArrayWrite(v, CEED_MEM_DEVICE, &d_v));
1140d0321e0SJeremy L Thompson 
1150d0321e0SJeremy L Thompson   // Clear v for transpose operation
116437930d1SJeremy L Thompson   if (t_mode == CEED_TRANSPOSE) {
1171f9221feSJeremy L Thompson     CeedSize length;
1182b730f8bSJeremy L Thompson     CeedCallBackend(CeedVectorGetLength(v, &length));
1192b730f8bSJeremy L Thompson     CeedCallHip(ceed, hipMemset(d_v, 0, length * sizeof(CeedScalar)));
1200d0321e0SJeremy L Thompson   }
1210d0321e0SJeremy L Thompson 
1220d0321e0SJeremy L Thompson   // Apply basis operation
123437930d1SJeremy L Thompson   switch (eval_mode) {
1240d0321e0SJeremy L Thompson     case CEED_EVAL_INTERP: {
1252b730f8bSJeremy L Thompson       void     *interp_args[] = {(void *)&num_elem, (void *)&transpose, &data->d_interp, &d_u, &d_v};
1262b730f8bSJeremy L Thompson       const int block_size_x  = transpose ? num_nodes : num_qpts;
127*b2165e7aSSebastian Grimberg 
128*b2165e7aSSebastian Grimberg       CeedCallBackend(CeedRunKernelDim_Hip(ceed, data->Interp, grid, block_size_x, 1, elems_per_block, interp_args));
1290d0321e0SJeremy L Thompson     } break;
1300d0321e0SJeremy L Thompson     case CEED_EVAL_GRAD: {
1312b730f8bSJeremy L Thompson       void     *grad_args[]  = {(void *)&num_elem, (void *)&transpose, &data->d_grad, &d_u, &d_v};
1322b730f8bSJeremy L Thompson       const int block_size_x = transpose ? num_nodes : num_qpts;
133*b2165e7aSSebastian Grimberg 
134*b2165e7aSSebastian Grimberg       CeedCallBackend(CeedRunKernelDim_Hip(ceed, data->Grad, grid, block_size_x, 1, elems_per_block, grad_args));
1350d0321e0SJeremy L Thompson     } break;
1360d0321e0SJeremy L Thompson     case CEED_EVAL_WEIGHT: {
137437930d1SJeremy L Thompson       void *weight_args[] = {(void *)&num_elem, (void *)&data->d_q_weight, &d_v};
138*b2165e7aSSebastian Grimberg 
139*b2165e7aSSebastian Grimberg       CeedCallBackend(CeedRunKernelDim_Hip(ceed, data->Weight, grid, num_qpts, 1, elems_per_block, weight_args));
1400d0321e0SJeremy L Thompson     } break;
1410d0321e0SJeremy L Thompson     // LCOV_EXCL_START
1420d0321e0SJeremy L Thompson     // Evaluate the divergence to/from the quadrature points
1430d0321e0SJeremy L Thompson     case CEED_EVAL_DIV:
1440d0321e0SJeremy L Thompson       return CeedError(ceed, CEED_ERROR_BACKEND, "CEED_EVAL_DIV not supported");
1450d0321e0SJeremy L Thompson     // Evaluate the curl to/from the quadrature points
1460d0321e0SJeremy L Thompson     case CEED_EVAL_CURL:
1470d0321e0SJeremy L Thompson       return CeedError(ceed, CEED_ERROR_BACKEND, "CEED_EVAL_CURL not supported");
1480d0321e0SJeremy L Thompson     // Take no action, BasisApply should not have been called
1490d0321e0SJeremy L Thompson     case CEED_EVAL_NONE:
1502b730f8bSJeremy L Thompson       return CeedError(ceed, CEED_ERROR_BACKEND, "CEED_EVAL_NONE does not make sense in this context");
1510d0321e0SJeremy L Thompson       // LCOV_EXCL_STOP
1520d0321e0SJeremy L Thompson   }
1530d0321e0SJeremy L Thompson 
1540d0321e0SJeremy L Thompson   // Restore vectors
155437930d1SJeremy L Thompson   if (eval_mode != CEED_EVAL_WEIGHT) {
1562b730f8bSJeremy L Thompson     CeedCallBackend(CeedVectorRestoreArrayRead(u, &d_u));
1570d0321e0SJeremy L Thompson   }
1582b730f8bSJeremy L Thompson   CeedCallBackend(CeedVectorRestoreArray(v, &d_v));
1590d0321e0SJeremy L Thompson   return CEED_ERROR_SUCCESS;
1600d0321e0SJeremy L Thompson }
1610d0321e0SJeremy L Thompson 
1620d0321e0SJeremy L Thompson //------------------------------------------------------------------------------
1630d0321e0SJeremy L Thompson // Destroy tensor basis
1640d0321e0SJeremy L Thompson //------------------------------------------------------------------------------
1650d0321e0SJeremy L Thompson static int CeedBasisDestroy_Hip(CeedBasis basis) {
1660d0321e0SJeremy L Thompson   Ceed ceed;
1672b730f8bSJeremy L Thompson   CeedCallBackend(CeedBasisGetCeed(basis, &ceed));
1680d0321e0SJeremy L Thompson 
1690d0321e0SJeremy L Thompson   CeedBasis_Hip *data;
1702b730f8bSJeremy L Thompson   CeedCallBackend(CeedBasisGetData(basis, &data));
1710d0321e0SJeremy L Thompson 
1722b730f8bSJeremy L Thompson   CeedCallHip(ceed, hipModuleUnload(data->module));
1730d0321e0SJeremy L Thompson 
1742b730f8bSJeremy L Thompson   CeedCallHip(ceed, hipFree(data->d_q_weight_1d));
1752b730f8bSJeremy L Thompson   CeedCallHip(ceed, hipFree(data->d_interp_1d));
1762b730f8bSJeremy L Thompson   CeedCallHip(ceed, hipFree(data->d_grad_1d));
1772b730f8bSJeremy L Thompson   CeedCallBackend(CeedFree(&data));
178437930d1SJeremy L Thompson 
1790d0321e0SJeremy L Thompson   return CEED_ERROR_SUCCESS;
1800d0321e0SJeremy L Thompson }
1810d0321e0SJeremy L Thompson 
1820d0321e0SJeremy L Thompson //------------------------------------------------------------------------------
1830d0321e0SJeremy L Thompson // Destroy non-tensor basis
1840d0321e0SJeremy L Thompson //------------------------------------------------------------------------------
1850d0321e0SJeremy L Thompson static int CeedBasisDestroyNonTensor_Hip(CeedBasis basis) {
1860d0321e0SJeremy L Thompson   Ceed ceed;
1872b730f8bSJeremy L Thompson   CeedCallBackend(CeedBasisGetCeed(basis, &ceed));
1880d0321e0SJeremy L Thompson 
1890d0321e0SJeremy L Thompson   CeedBasisNonTensor_Hip *data;
1902b730f8bSJeremy L Thompson   CeedCallBackend(CeedBasisGetData(basis, &data));
1910d0321e0SJeremy L Thompson 
1922b730f8bSJeremy L Thompson   CeedCallHip(ceed, hipModuleUnload(data->module));
1930d0321e0SJeremy L Thompson 
1942b730f8bSJeremy L Thompson   CeedCallHip(ceed, hipFree(data->d_q_weight));
1952b730f8bSJeremy L Thompson   CeedCallHip(ceed, hipFree(data->d_interp));
1962b730f8bSJeremy L Thompson   CeedCallHip(ceed, hipFree(data->d_grad));
1972b730f8bSJeremy L Thompson   CeedCallBackend(CeedFree(&data));
198437930d1SJeremy L Thompson 
1990d0321e0SJeremy L Thompson   return CEED_ERROR_SUCCESS;
2000d0321e0SJeremy L Thompson }
2010d0321e0SJeremy L Thompson 
2020d0321e0SJeremy L Thompson //------------------------------------------------------------------------------
2030d0321e0SJeremy L Thompson // Create tensor
2040d0321e0SJeremy L Thompson //------------------------------------------------------------------------------
2052b730f8bSJeremy L Thompson int CeedBasisCreateTensorH1_Hip(CeedInt dim, CeedInt P_1d, CeedInt Q_1d, const CeedScalar *interp_1d, const CeedScalar *grad_1d,
2066574a04fSJeremy L Thompson                                 const CeedScalar *q_ref_1d, const CeedScalar *q_weight_1d, CeedBasis basis) {
2070d0321e0SJeremy L Thompson   Ceed ceed;
2082b730f8bSJeremy L Thompson   CeedCallBackend(CeedBasisGetCeed(basis, &ceed));
2090d0321e0SJeremy L Thompson   CeedBasis_Hip *data;
2102b730f8bSJeremy L Thompson   CeedCallBackend(CeedCalloc(1, &data));
2110d0321e0SJeremy L Thompson 
2120d0321e0SJeremy L Thompson   // Copy data to GPU
213437930d1SJeremy L Thompson   const CeedInt q_bytes = Q_1d * sizeof(CeedScalar);
2142b730f8bSJeremy L Thompson   CeedCallHip(ceed, hipMalloc((void **)&data->d_q_weight_1d, q_bytes));
2152b730f8bSJeremy L Thompson   CeedCallHip(ceed, hipMemcpy(data->d_q_weight_1d, q_weight_1d, q_bytes, hipMemcpyHostToDevice));
2160d0321e0SJeremy L Thompson 
217437930d1SJeremy L Thompson   const CeedInt interp_bytes = q_bytes * P_1d;
2182b730f8bSJeremy L Thompson   CeedCallHip(ceed, hipMalloc((void **)&data->d_interp_1d, interp_bytes));
2192b730f8bSJeremy L Thompson   CeedCallHip(ceed, hipMemcpy(data->d_interp_1d, interp_1d, interp_bytes, hipMemcpyHostToDevice));
2200d0321e0SJeremy L Thompson 
2212b730f8bSJeremy L Thompson   CeedCallHip(ceed, hipMalloc((void **)&data->d_grad_1d, interp_bytes));
2222b730f8bSJeremy L Thompson   CeedCallHip(ceed, hipMemcpy(data->d_grad_1d, grad_1d, interp_bytes, hipMemcpyHostToDevice));
2230d0321e0SJeremy L Thompson 
224ecc88aebSJeremy L Thompson   // Compile basis kernels
2250d0321e0SJeremy L Thompson   CeedInt ncomp;
2262b730f8bSJeremy L Thompson   CeedCallBackend(CeedBasisGetNumComponents(basis, &ncomp));
227437930d1SJeremy L Thompson   char *basis_kernel_path, *basis_kernel_source;
2282b730f8bSJeremy L Thompson   CeedCallBackend(CeedGetJitAbsolutePath(ceed, "ceed/jit-source/hip/hip-ref-basis-tensor.h", &basis_kernel_path));
22923d4529eSJeremy L Thompson   CeedDebug256(ceed, CEED_DEBUG_COLOR_SUCCESS, "----- Loading Basis Kernel Source -----\n");
2302b730f8bSJeremy L Thompson   CeedCallBackend(CeedLoadSourceToBuffer(ceed, basis_kernel_path, &basis_kernel_source));
23123d4529eSJeremy L Thompson   CeedDebug256(ceed, CEED_DEBUG_COLOR_SUCCESS, "----- Loading Basis Kernel Source Complete! -----\n");
232eb7e6cafSJeremy L Thompson   CeedCallBackend(CeedCompile_Hip(ceed, basis_kernel_source, &data->module, 7, "BASIS_Q_1D", Q_1d, "BASIS_P_1D", P_1d, "BASIS_BUF_LEN",
2332b730f8bSJeremy L Thompson                                   ncomp * CeedIntPow(Q_1d > P_1d ? Q_1d : P_1d, dim), "BASIS_DIM", dim, "BASIS_NUM_COMP", ncomp, "BASIS_NUM_NODES",
2342b730f8bSJeremy L Thompson                                   CeedIntPow(P_1d, dim), "BASIS_NUM_QPTS", CeedIntPow(Q_1d, dim)));
235eb7e6cafSJeremy L Thompson   CeedCallBackend(CeedGetKernel_Hip(ceed, data->module, "Interp", &data->Interp));
236eb7e6cafSJeremy L Thompson   CeedCallBackend(CeedGetKernel_Hip(ceed, data->module, "Grad", &data->Grad));
237eb7e6cafSJeremy L Thompson   CeedCallBackend(CeedGetKernel_Hip(ceed, data->module, "Weight", &data->Weight));
2382b730f8bSJeremy L Thompson   CeedCallBackend(CeedFree(&basis_kernel_path));
2392b730f8bSJeremy L Thompson   CeedCallBackend(CeedFree(&basis_kernel_source));
240437930d1SJeremy L Thompson 
2412b730f8bSJeremy L Thompson   CeedCallBackend(CeedBasisSetData(basis, data));
2420d0321e0SJeremy L Thompson 
2432b730f8bSJeremy L Thompson   CeedCallBackend(CeedSetBackendFunction(ceed, "Basis", basis, "Apply", CeedBasisApply_Hip));
2442b730f8bSJeremy L Thompson   CeedCallBackend(CeedSetBackendFunction(ceed, "Basis", basis, "Destroy", CeedBasisDestroy_Hip));
2450d0321e0SJeremy L Thompson   return CEED_ERROR_SUCCESS;
2460d0321e0SJeremy L Thompson }
2470d0321e0SJeremy L Thompson 
2480d0321e0SJeremy L Thompson //------------------------------------------------------------------------------
2490d0321e0SJeremy L Thompson // Create non-tensor
2500d0321e0SJeremy L Thompson //------------------------------------------------------------------------------
2512b730f8bSJeremy L Thompson int CeedBasisCreateH1_Hip(CeedElemTopology topo, CeedInt dim, CeedInt num_nodes, CeedInt num_qpts, const CeedScalar *interp, const CeedScalar *grad,
25251475c7cSJeremy L Thompson                           const CeedScalar *q_ref, const CeedScalar *q_weight, CeedBasis basis) {
2530d0321e0SJeremy L Thompson   Ceed ceed;
2542b730f8bSJeremy L Thompson   CeedCallBackend(CeedBasisGetCeed(basis, &ceed));
2550d0321e0SJeremy L Thompson   CeedBasisNonTensor_Hip *data;
2562b730f8bSJeremy L Thompson   CeedCallBackend(CeedCalloc(1, &data));
2570d0321e0SJeremy L Thompson 
2580d0321e0SJeremy L Thompson   // Copy basis data to GPU
259437930d1SJeremy L Thompson   const CeedInt q_bytes = num_qpts * sizeof(CeedScalar);
2602b730f8bSJeremy L Thompson   CeedCallHip(ceed, hipMalloc((void **)&data->d_q_weight, q_bytes));
2612b730f8bSJeremy L Thompson   CeedCallHip(ceed, hipMemcpy(data->d_q_weight, q_weight, q_bytes, hipMemcpyHostToDevice));
2620d0321e0SJeremy L Thompson 
263437930d1SJeremy L Thompson   const CeedInt interp_bytes = q_bytes * num_nodes;
2642b730f8bSJeremy L Thompson   CeedCallHip(ceed, hipMalloc((void **)&data->d_interp, interp_bytes));
2652b730f8bSJeremy L Thompson   CeedCallHip(ceed, hipMemcpy(data->d_interp, interp, interp_bytes, hipMemcpyHostToDevice));
2660d0321e0SJeremy L Thompson 
267437930d1SJeremy L Thompson   const CeedInt grad_bytes = q_bytes * num_nodes * dim;
2682b730f8bSJeremy L Thompson   CeedCallHip(ceed, hipMalloc((void **)&data->d_grad, grad_bytes));
2692b730f8bSJeremy L Thompson   CeedCallHip(ceed, hipMemcpy(data->d_grad, grad, grad_bytes, hipMemcpyHostToDevice));
2700d0321e0SJeremy L Thompson 
2710d0321e0SJeremy L Thompson   // Compile basis kernels
2720d0321e0SJeremy L Thompson   CeedInt ncomp;
2732b730f8bSJeremy L Thompson   CeedCallBackend(CeedBasisGetNumComponents(basis, &ncomp));
274437930d1SJeremy L Thompson   char *basis_kernel_path, *basis_kernel_source;
2752b730f8bSJeremy L Thompson   CeedCallBackend(CeedGetJitAbsolutePath(ceed, "ceed/jit-source/hip/hip-ref-basis-nontensor.h", &basis_kernel_path));
27623d4529eSJeremy L Thompson   CeedDebug256(ceed, CEED_DEBUG_COLOR_SUCCESS, "----- Loading Basis Kernel Source -----\n");
2772b730f8bSJeremy L Thompson   CeedCallBackend(CeedLoadSourceToBuffer(ceed, basis_kernel_path, &basis_kernel_source));
27823d4529eSJeremy L Thompson   CeedDebug256(ceed, CEED_DEBUG_COLOR_SUCCESS, "----- Loading Basis Kernel Source Complete! -----\n");
279eb7e6cafSJeremy L Thompson   CeedCallBackend(CeedCompile_Hip(ceed, basis_kernel_source, &data->module, 4, "BASIS_Q", num_qpts, "BASIS_P", num_nodes, "BASIS_DIM", dim,
2802b730f8bSJeremy L Thompson                                   "BASIS_NUM_COMP", ncomp));
281eb7e6cafSJeremy L Thompson   CeedCallBackend(CeedGetKernel_Hip(ceed, data->module, "Interp", &data->Interp));
282eb7e6cafSJeremy L Thompson   CeedCallBackend(CeedGetKernel_Hip(ceed, data->module, "Grad", &data->Grad));
283eb7e6cafSJeremy L Thompson   CeedCallBackend(CeedGetKernel_Hip(ceed, data->module, "Weight", &data->Weight));
2842b730f8bSJeremy L Thompson   CeedCallBackend(CeedFree(&basis_kernel_path));
2852b730f8bSJeremy L Thompson   CeedCallBackend(CeedFree(&basis_kernel_source));
2862b730f8bSJeremy L Thompson   CeedCallBackend(CeedBasisSetData(basis, data));
2870d0321e0SJeremy L Thompson 
2880d0321e0SJeremy L Thompson   // Register backend functions
2892b730f8bSJeremy L Thompson   CeedCallBackend(CeedSetBackendFunction(ceed, "Basis", basis, "Apply", CeedBasisApplyNonTensor_Hip));
2902b730f8bSJeremy L Thompson   CeedCallBackend(CeedSetBackendFunction(ceed, "Basis", basis, "Destroy", CeedBasisDestroyNonTensor_Hip));
2910d0321e0SJeremy L Thompson   return CEED_ERROR_SUCCESS;
2920d0321e0SJeremy L Thompson }
2932a86cc9dSSebastian Grimberg 
2940d0321e0SJeremy L Thompson //------------------------------------------------------------------------------
295