xref: /libCEED/rust/libceed-sys/c-src/backends/hip-ref/ceed-hip-ref-basis.c (revision 097cc79570fc1ccd17774d2bf9cd5e51d3925370)
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 <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;
22b7453713SJeremy L Thompson   CeedInt           Q_1d, dim;
237bbbfca3SJeremy L Thompson   const CeedInt     is_transpose   = t_mode == CEED_TRANSPOSE;
24437930d1SJeremy L Thompson   const int         max_block_size = 64;
250d0321e0SJeremy L Thompson   const CeedScalar *d_u;
260d0321e0SJeremy L Thompson   CeedScalar       *d_v;
27b7453713SJeremy L Thompson   CeedBasis_Hip    *data;
28b7453713SJeremy L Thompson 
29b7453713SJeremy L Thompson   CeedCallBackend(CeedBasisGetCeed(basis, &ceed));
30b7453713SJeremy L Thompson   CeedCallBackend(CeedBasisGetData(basis, &data));
31b7453713SJeremy L Thompson 
329ea2cfd9SJeremy L Thompson   // Get read/write access to u, 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
387bbbfca3SJeremy L Thompson   if (is_transpose) {
391f9221feSJeremy L Thompson     CeedSize length;
40b7453713SJeremy L Thompson 
412b730f8bSJeremy L Thompson     CeedCallBackend(CeedVectorGetLength(v, &length));
422b730f8bSJeremy L Thompson     CeedCallHip(ceed, hipMemset(d_v, 0, length * sizeof(CeedScalar)));
430d0321e0SJeremy L Thompson   }
44b2165e7aSSebastian Grimberg   CeedCallBackend(CeedBasisGetNumQuadraturePoints1D(basis, &Q_1d));
45b2165e7aSSebastian 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: {
507bbbfca3SJeremy L Thompson       void         *interp_args[] = {(void *)&num_elem, (void *)&is_transpose, &data->d_interp_1d, &d_u, &d_v};
51b2165e7aSSebastian 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: {
567bbbfca3SJeremy L Thompson       void         *grad_args[] = {(void *)&num_elem, (void *)&is_transpose, &data->d_interp_1d, &data->d_grad_1d, &d_u, &d_v};
57b2165e7aSSebastian 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: {
62*097cc795SJames Wright       CeedCheck(data->d_q_weight_1d, ceed, CEED_ERROR_BACKEND, "%s not supported; q_weights_1d not set", CeedEvalModes[eval_mode]);
63437930d1SJeremy L Thompson       void     *weight_args[] = {(void *)&num_elem, (void *)&data->d_q_weight_1d, &d_v};
64b2165e7aSSebastian Grimberg       const int block_size_x  = Q_1d;
65b2165e7aSSebastian Grimberg       const int block_size_y  = dim >= 2 ? Q_1d : 1;
660d0321e0SJeremy L Thompson 
67b2165e7aSSebastian Grimberg       CeedCallBackend(CeedRunKernelDim_Hip(ceed, data->Weight, num_elem, block_size_x, block_size_y, 1, weight_args));
680d0321e0SJeremy L Thompson     } break;
699ea2cfd9SJeremy L Thompson     case CEED_EVAL_NONE: /* handled separately below */
709ea2cfd9SJeremy L Thompson       break;
710d0321e0SJeremy L Thompson     // LCOV_EXCL_START
720d0321e0SJeremy L Thompson     case CEED_EVAL_DIV:
730d0321e0SJeremy L Thompson     case CEED_EVAL_CURL:
74bcbe1c99SJeremy L Thompson       return CeedError(ceed, CEED_ERROR_BACKEND, "%s not supported", CeedEvalModes[eval_mode]);
750d0321e0SJeremy L Thompson       // LCOV_EXCL_STOP
760d0321e0SJeremy L Thompson   }
770d0321e0SJeremy L Thompson 
789ea2cfd9SJeremy L Thompson   // Restore vectors, cover CEED_EVAL_NONE
792b730f8bSJeremy L Thompson   CeedCallBackend(CeedVectorRestoreArray(v, &d_v));
809ea2cfd9SJeremy L Thompson   if (eval_mode == CEED_EVAL_NONE) CeedCallBackend(CeedVectorSetArray(v, CEED_MEM_DEVICE, CEED_COPY_VALUES, (CeedScalar *)d_u));
819ea2cfd9SJeremy L Thompson   if (eval_mode != CEED_EVAL_WEIGHT) CeedCallBackend(CeedVectorRestoreArrayRead(u, &d_u));
820d0321e0SJeremy L Thompson   return CEED_ERROR_SUCCESS;
830d0321e0SJeremy L Thompson }
840d0321e0SJeremy L Thompson 
850d0321e0SJeremy L Thompson //------------------------------------------------------------------------------
860d0321e0SJeremy L Thompson // Basis apply - non-tensor
870d0321e0SJeremy L Thompson //------------------------------------------------------------------------------
882b730f8bSJeremy L Thompson int CeedBasisApplyNonTensor_Hip(CeedBasis basis, const CeedInt num_elem, CeedTransposeMode t_mode, CeedEvalMode eval_mode, CeedVector u,
892b730f8bSJeremy L Thompson                                 CeedVector v) {
900d0321e0SJeremy L Thompson   Ceed                    ceed;
91437930d1SJeremy L Thompson   CeedInt                 num_nodes, num_qpts;
927bbbfca3SJeremy L Thompson   const CeedInt           is_transpose    = t_mode == CEED_TRANSPOSE;
93d075f50bSSebastian Grimberg   const int               elems_per_block = 1;
94d075f50bSSebastian Grimberg   const int               grid            = CeedDivUpInt(num_elem, elems_per_block);
950d0321e0SJeremy L Thompson   const CeedScalar       *d_u;
960d0321e0SJeremy L Thompson   CeedScalar             *d_v;
97b7453713SJeremy L Thompson   CeedBasisNonTensor_Hip *data;
98b7453713SJeremy L Thompson 
99b7453713SJeremy L Thompson   CeedCallBackend(CeedBasisGetCeed(basis, &ceed));
100b7453713SJeremy L Thompson   CeedCallBackend(CeedBasisGetData(basis, &data));
101b7453713SJeremy L Thompson   CeedCallBackend(CeedBasisGetNumQuadraturePoints(basis, &num_qpts));
102b7453713SJeremy L Thompson   CeedCallBackend(CeedBasisGetNumNodes(basis, &num_nodes));
103b7453713SJeremy L Thompson 
1049ea2cfd9SJeremy L Thompson   // Get read/write access to u, v
1059ea2cfd9SJeremy L Thompson   if (u != CEED_VECTOR_NONE) CeedCallBackend(CeedVectorGetArrayRead(u, CEED_MEM_DEVICE, &d_u));
1069ea2cfd9SJeremy L Thompson   else CeedCheck(eval_mode == CEED_EVAL_WEIGHT, ceed, CEED_ERROR_BACKEND, "An input vector is required for this CeedEvalMode");
1072b730f8bSJeremy L Thompson   CeedCallBackend(CeedVectorGetArrayWrite(v, CEED_MEM_DEVICE, &d_v));
1080d0321e0SJeremy L Thompson 
1090d0321e0SJeremy L Thompson   // Clear v for transpose operation
1107bbbfca3SJeremy L Thompson   if (is_transpose) {
1111f9221feSJeremy L Thompson     CeedSize length;
112b7453713SJeremy L Thompson 
1132b730f8bSJeremy L Thompson     CeedCallBackend(CeedVectorGetLength(v, &length));
1142b730f8bSJeremy L Thompson     CeedCallHip(ceed, hipMemset(d_v, 0, length * sizeof(CeedScalar)));
1150d0321e0SJeremy L Thompson   }
1160d0321e0SJeremy L Thompson 
1170d0321e0SJeremy L Thompson   // Apply basis operation
118437930d1SJeremy L Thompson   switch (eval_mode) {
1190d0321e0SJeremy L Thompson     case CEED_EVAL_INTERP: {
120d075f50bSSebastian Grimberg       void     *interp_args[] = {(void *)&num_elem, &data->d_interp, &d_u, &d_v};
1217bbbfca3SJeremy L Thompson       const int block_size_x  = is_transpose ? num_nodes : num_qpts;
122b2165e7aSSebastian Grimberg 
1237bbbfca3SJeremy L Thompson       if (is_transpose) {
124d075f50bSSebastian Grimberg         CeedCallBackend(CeedRunKernelDim_Hip(ceed, data->InterpTranspose, grid, block_size_x, 1, elems_per_block, interp_args));
125d075f50bSSebastian Grimberg       } else {
126b2165e7aSSebastian Grimberg         CeedCallBackend(CeedRunKernelDim_Hip(ceed, data->Interp, grid, block_size_x, 1, elems_per_block, interp_args));
127d075f50bSSebastian Grimberg       }
1280d0321e0SJeremy L Thompson     } break;
1290d0321e0SJeremy L Thompson     case CEED_EVAL_GRAD: {
130d075f50bSSebastian Grimberg       void     *grad_args[]  = {(void *)&num_elem, &data->d_grad, &d_u, &d_v};
1317bbbfca3SJeremy L Thompson       const int block_size_x = is_transpose ? num_nodes : num_qpts;
132b2165e7aSSebastian Grimberg 
1337bbbfca3SJeremy L Thompson       if (is_transpose) {
134d075f50bSSebastian Grimberg         CeedCallBackend(CeedRunKernelDim_Hip(ceed, data->DerivTranspose, grid, block_size_x, 1, elems_per_block, grad_args));
135d075f50bSSebastian Grimberg       } else {
136d075f50bSSebastian Grimberg         CeedCallBackend(CeedRunKernelDim_Hip(ceed, data->Deriv, grid, block_size_x, 1, elems_per_block, grad_args));
137d075f50bSSebastian Grimberg       }
138d075f50bSSebastian Grimberg     } break;
139d075f50bSSebastian Grimberg     case CEED_EVAL_DIV: {
140d075f50bSSebastian Grimberg       void     *div_args[]   = {(void *)&num_elem, &data->d_div, &d_u, &d_v};
1417bbbfca3SJeremy L Thompson       const int block_size_x = is_transpose ? num_nodes : num_qpts;
142d075f50bSSebastian Grimberg 
1437bbbfca3SJeremy L Thompson       if (is_transpose) {
144d075f50bSSebastian Grimberg         CeedCallBackend(CeedRunKernelDim_Hip(ceed, data->DerivTranspose, grid, block_size_x, 1, elems_per_block, div_args));
145d075f50bSSebastian Grimberg       } else {
146d075f50bSSebastian Grimberg         CeedCallBackend(CeedRunKernelDim_Hip(ceed, data->Deriv, grid, block_size_x, 1, elems_per_block, div_args));
147d075f50bSSebastian Grimberg       }
148d075f50bSSebastian Grimberg     } break;
149d075f50bSSebastian Grimberg     case CEED_EVAL_CURL: {
150d075f50bSSebastian Grimberg       void     *curl_args[]  = {(void *)&num_elem, &data->d_curl, &d_u, &d_v};
1517bbbfca3SJeremy L Thompson       const int block_size_x = is_transpose ? num_nodes : num_qpts;
152d075f50bSSebastian Grimberg 
1537bbbfca3SJeremy L Thompson       if (is_transpose) {
154d075f50bSSebastian Grimberg         CeedCallBackend(CeedRunKernelDim_Hip(ceed, data->DerivTranspose, grid, block_size_x, 1, elems_per_block, curl_args));
155d075f50bSSebastian Grimberg       } else {
156d075f50bSSebastian Grimberg         CeedCallBackend(CeedRunKernelDim_Hip(ceed, data->Deriv, grid, block_size_x, 1, elems_per_block, curl_args));
157d075f50bSSebastian Grimberg       }
1580d0321e0SJeremy L Thompson     } break;
1590d0321e0SJeremy L Thompson     case CEED_EVAL_WEIGHT: {
160*097cc795SJames Wright       CeedCheck(data->d_q_weight, ceed, CEED_ERROR_BACKEND, "%s not supported; q_weights not set", CeedEvalModes[eval_mode]);
161437930d1SJeremy L Thompson       void *weight_args[] = {(void *)&num_elem, (void *)&data->d_q_weight, &d_v};
162b2165e7aSSebastian Grimberg 
163b2165e7aSSebastian Grimberg       CeedCallBackend(CeedRunKernelDim_Hip(ceed, data->Weight, grid, num_qpts, 1, elems_per_block, weight_args));
1640d0321e0SJeremy L Thompson     } break;
1659ea2cfd9SJeremy L Thompson     case CEED_EVAL_NONE: /* handled separately below */
1669ea2cfd9SJeremy L Thompson       break;
1670d0321e0SJeremy L Thompson   }
1680d0321e0SJeremy L Thompson 
1699ea2cfd9SJeremy L Thompson   // Restore vectors, cover CEED_EVAL_NONE
1702b730f8bSJeremy L Thompson   CeedCallBackend(CeedVectorRestoreArray(v, &d_v));
1719ea2cfd9SJeremy L Thompson   if (eval_mode == CEED_EVAL_NONE) CeedCallBackend(CeedVectorSetArray(v, CEED_MEM_DEVICE, CEED_COPY_VALUES, (CeedScalar *)d_u));
1729ea2cfd9SJeremy L Thompson   if (eval_mode != CEED_EVAL_WEIGHT) CeedCallBackend(CeedVectorRestoreArrayRead(u, &d_u));
1730d0321e0SJeremy L Thompson   return CEED_ERROR_SUCCESS;
1740d0321e0SJeremy L Thompson }
1750d0321e0SJeremy L Thompson 
1760d0321e0SJeremy L Thompson //------------------------------------------------------------------------------
1770d0321e0SJeremy L Thompson // Destroy tensor basis
1780d0321e0SJeremy L Thompson //------------------------------------------------------------------------------
1790d0321e0SJeremy L Thompson static int CeedBasisDestroy_Hip(CeedBasis basis) {
1800d0321e0SJeremy L Thompson   Ceed           ceed;
1810d0321e0SJeremy L Thompson   CeedBasis_Hip *data;
182b7453713SJeremy L Thompson 
183b7453713SJeremy L Thompson   CeedCallBackend(CeedBasisGetCeed(basis, &ceed));
1842b730f8bSJeremy L Thompson   CeedCallBackend(CeedBasisGetData(basis, &data));
1852b730f8bSJeremy L Thompson   CeedCallHip(ceed, hipModuleUnload(data->module));
186*097cc795SJames Wright   if (data->d_q_weight_1d) CeedCallHip(ceed, hipFree(data->d_q_weight_1d));
1872b730f8bSJeremy L Thompson   CeedCallHip(ceed, hipFree(data->d_interp_1d));
1882b730f8bSJeremy L Thompson   CeedCallHip(ceed, hipFree(data->d_grad_1d));
1892b730f8bSJeremy L Thompson   CeedCallBackend(CeedFree(&data));
1900d0321e0SJeremy L Thompson   return CEED_ERROR_SUCCESS;
1910d0321e0SJeremy L Thompson }
1920d0321e0SJeremy L Thompson 
1930d0321e0SJeremy L Thompson //------------------------------------------------------------------------------
1940d0321e0SJeremy L Thompson // Destroy non-tensor basis
1950d0321e0SJeremy L Thompson //------------------------------------------------------------------------------
1960d0321e0SJeremy L Thompson static int CeedBasisDestroyNonTensor_Hip(CeedBasis basis) {
1970d0321e0SJeremy L Thompson   Ceed                    ceed;
1980d0321e0SJeremy L Thompson   CeedBasisNonTensor_Hip *data;
199b7453713SJeremy L Thompson 
200b7453713SJeremy L Thompson   CeedCallBackend(CeedBasisGetCeed(basis, &ceed));
2012b730f8bSJeremy L Thompson   CeedCallBackend(CeedBasisGetData(basis, &data));
2022b730f8bSJeremy L Thompson   CeedCallHip(ceed, hipModuleUnload(data->module));
203*097cc795SJames Wright   if (data->d_q_weight) CeedCallHip(ceed, hipFree(data->d_q_weight));
2042b730f8bSJeremy L Thompson   CeedCallHip(ceed, hipFree(data->d_interp));
2052b730f8bSJeremy L Thompson   CeedCallHip(ceed, hipFree(data->d_grad));
206d075f50bSSebastian Grimberg   CeedCallHip(ceed, hipFree(data->d_div));
207d075f50bSSebastian Grimberg   CeedCallHip(ceed, hipFree(data->d_curl));
2082b730f8bSJeremy L Thompson   CeedCallBackend(CeedFree(&data));
2090d0321e0SJeremy L Thompson   return CEED_ERROR_SUCCESS;
2100d0321e0SJeremy L Thompson }
2110d0321e0SJeremy L Thompson 
2120d0321e0SJeremy L Thompson //------------------------------------------------------------------------------
2130d0321e0SJeremy L Thompson // Create tensor
2140d0321e0SJeremy L Thompson //------------------------------------------------------------------------------
2152b730f8bSJeremy L Thompson int CeedBasisCreateTensorH1_Hip(CeedInt dim, CeedInt P_1d, CeedInt Q_1d, const CeedScalar *interp_1d, const CeedScalar *grad_1d,
2166574a04fSJeremy L Thompson                                 const CeedScalar *q_ref_1d, const CeedScalar *q_weight_1d, CeedBasis basis) {
2170d0321e0SJeremy L Thompson   Ceed           ceed;
21822070f95SJeremy L Thompson   char          *basis_kernel_source;
21922070f95SJeremy L Thompson   const char    *basis_kernel_path;
220b7453713SJeremy L Thompson   CeedInt        num_comp;
221b7453713SJeremy L Thompson   const CeedInt  q_bytes      = Q_1d * sizeof(CeedScalar);
222b7453713SJeremy L Thompson   const CeedInt  interp_bytes = q_bytes * P_1d;
2230d0321e0SJeremy L Thompson   CeedBasis_Hip *data;
224b7453713SJeremy L Thompson 
225b7453713SJeremy L Thompson   CeedCallBackend(CeedBasisGetCeed(basis, &ceed));
2262b730f8bSJeremy L Thompson   CeedCallBackend(CeedCalloc(1, &data));
2270d0321e0SJeremy L Thompson 
2280d0321e0SJeremy L Thompson   // Copy data to GPU
229*097cc795SJames Wright   if (q_weight_1d) {
2302b730f8bSJeremy L Thompson     CeedCallHip(ceed, hipMalloc((void **)&data->d_q_weight_1d, q_bytes));
2312b730f8bSJeremy L Thompson     CeedCallHip(ceed, hipMemcpy(data->d_q_weight_1d, q_weight_1d, q_bytes, hipMemcpyHostToDevice));
232*097cc795SJames Wright   }
2332b730f8bSJeremy L Thompson   CeedCallHip(ceed, hipMalloc((void **)&data->d_interp_1d, interp_bytes));
2342b730f8bSJeremy L Thompson   CeedCallHip(ceed, hipMemcpy(data->d_interp_1d, interp_1d, interp_bytes, hipMemcpyHostToDevice));
2352b730f8bSJeremy L Thompson   CeedCallHip(ceed, hipMalloc((void **)&data->d_grad_1d, interp_bytes));
2362b730f8bSJeremy L Thompson   CeedCallHip(ceed, hipMemcpy(data->d_grad_1d, grad_1d, interp_bytes, hipMemcpyHostToDevice));
2370d0321e0SJeremy L Thompson 
238ecc88aebSJeremy L Thompson   // Compile basis kernels
239b7453713SJeremy L Thompson   CeedCallBackend(CeedBasisGetNumComponents(basis, &num_comp));
2402b730f8bSJeremy L Thompson   CeedCallBackend(CeedGetJitAbsolutePath(ceed, "ceed/jit-source/hip/hip-ref-basis-tensor.h", &basis_kernel_path));
24123d4529eSJeremy L Thompson   CeedDebug256(ceed, CEED_DEBUG_COLOR_SUCCESS, "----- Loading Basis Kernel Source -----\n");
2422b730f8bSJeremy L Thompson   CeedCallBackend(CeedLoadSourceToBuffer(ceed, basis_kernel_path, &basis_kernel_source));
24323d4529eSJeremy L Thompson   CeedDebug256(ceed, CEED_DEBUG_COLOR_SUCCESS, "----- Loading Basis Kernel Source Complete! -----\n");
244eb7e6cafSJeremy 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",
245b7453713SJeremy L Thompson                                   num_comp * CeedIntPow(Q_1d > P_1d ? Q_1d : P_1d, dim), "BASIS_DIM", dim, "BASIS_NUM_COMP", num_comp,
246b7453713SJeremy L Thompson                                   "BASIS_NUM_NODES", CeedIntPow(P_1d, dim), "BASIS_NUM_QPTS", CeedIntPow(Q_1d, dim)));
247eb7e6cafSJeremy L Thompson   CeedCallBackend(CeedGetKernel_Hip(ceed, data->module, "Interp", &data->Interp));
248eb7e6cafSJeremy L Thompson   CeedCallBackend(CeedGetKernel_Hip(ceed, data->module, "Grad", &data->Grad));
249eb7e6cafSJeremy L Thompson   CeedCallBackend(CeedGetKernel_Hip(ceed, data->module, "Weight", &data->Weight));
2502b730f8bSJeremy L Thompson   CeedCallBackend(CeedFree(&basis_kernel_path));
2512b730f8bSJeremy L Thompson   CeedCallBackend(CeedFree(&basis_kernel_source));
252437930d1SJeremy L Thompson 
2532b730f8bSJeremy L Thompson   CeedCallBackend(CeedBasisSetData(basis, data));
2540d0321e0SJeremy L Thompson 
255d075f50bSSebastian Grimberg   // Register backend functions
2562b730f8bSJeremy L Thompson   CeedCallBackend(CeedSetBackendFunction(ceed, "Basis", basis, "Apply", CeedBasisApply_Hip));
2572b730f8bSJeremy L Thompson   CeedCallBackend(CeedSetBackendFunction(ceed, "Basis", basis, "Destroy", CeedBasisDestroy_Hip));
2580d0321e0SJeremy L Thompson   return CEED_ERROR_SUCCESS;
2590d0321e0SJeremy L Thompson }
2600d0321e0SJeremy L Thompson 
2610d0321e0SJeremy L Thompson //------------------------------------------------------------------------------
262d075f50bSSebastian Grimberg // Create non-tensor H^1
2630d0321e0SJeremy L Thompson //------------------------------------------------------------------------------
2642b730f8bSJeremy L Thompson int CeedBasisCreateH1_Hip(CeedElemTopology topo, CeedInt dim, CeedInt num_nodes, CeedInt num_qpts, const CeedScalar *interp, const CeedScalar *grad,
26551475c7cSJeremy L Thompson                           const CeedScalar *q_ref, const CeedScalar *q_weight, CeedBasis basis) {
2660d0321e0SJeremy L Thompson   Ceed                    ceed;
26722070f95SJeremy L Thompson   char                   *basis_kernel_source;
26822070f95SJeremy L Thompson   const char             *basis_kernel_path;
269d075f50bSSebastian Grimberg   CeedInt                 num_comp, q_comp_interp, q_comp_grad;
270b7453713SJeremy L Thompson   const CeedInt           q_bytes = num_qpts * sizeof(CeedScalar);
2710d0321e0SJeremy L Thompson   CeedBasisNonTensor_Hip *data;
272b7453713SJeremy L Thompson 
273b7453713SJeremy L Thompson   CeedCallBackend(CeedBasisGetCeed(basis, &ceed));
2742b730f8bSJeremy L Thompson   CeedCallBackend(CeedCalloc(1, &data));
2750d0321e0SJeremy L Thompson 
2760d0321e0SJeremy L Thompson   // Copy basis data to GPU
277d075f50bSSebastian Grimberg   CeedCallBackend(CeedBasisGetNumQuadratureComponents(basis, CEED_EVAL_INTERP, &q_comp_interp));
278d075f50bSSebastian Grimberg   CeedCallBackend(CeedBasisGetNumQuadratureComponents(basis, CEED_EVAL_GRAD, &q_comp_grad));
279*097cc795SJames Wright   if (q_weight) {
2802b730f8bSJeremy L Thompson     CeedCallHip(ceed, hipMalloc((void **)&data->d_q_weight, q_bytes));
2812b730f8bSJeremy L Thompson     CeedCallHip(ceed, hipMemcpy(data->d_q_weight, q_weight, q_bytes, hipMemcpyHostToDevice));
282*097cc795SJames Wright   }
283d075f50bSSebastian Grimberg   if (interp) {
284d075f50bSSebastian Grimberg     const CeedInt interp_bytes = q_bytes * num_nodes * q_comp_interp;
285d075f50bSSebastian Grimberg 
2862b730f8bSJeremy L Thompson     CeedCallHip(ceed, hipMalloc((void **)&data->d_interp, interp_bytes));
2872b730f8bSJeremy L Thompson     CeedCallHip(ceed, hipMemcpy(data->d_interp, interp, interp_bytes, hipMemcpyHostToDevice));
288d075f50bSSebastian Grimberg   }
289d075f50bSSebastian Grimberg   if (grad) {
290d075f50bSSebastian Grimberg     const CeedInt grad_bytes = q_bytes * num_nodes * q_comp_grad;
291d075f50bSSebastian Grimberg 
2922b730f8bSJeremy L Thompson     CeedCallHip(ceed, hipMalloc((void **)&data->d_grad, grad_bytes));
2932b730f8bSJeremy L Thompson     CeedCallHip(ceed, hipMemcpy(data->d_grad, grad, grad_bytes, hipMemcpyHostToDevice));
294d075f50bSSebastian Grimberg   }
2950d0321e0SJeremy L Thompson 
2960d0321e0SJeremy L Thompson   // Compile basis kernels
297b7453713SJeremy L Thompson   CeedCallBackend(CeedBasisGetNumComponents(basis, &num_comp));
2982b730f8bSJeremy L Thompson   CeedCallBackend(CeedGetJitAbsolutePath(ceed, "ceed/jit-source/hip/hip-ref-basis-nontensor.h", &basis_kernel_path));
29923d4529eSJeremy L Thompson   CeedDebug256(ceed, CEED_DEBUG_COLOR_SUCCESS, "----- Loading Basis Kernel Source -----\n");
3002b730f8bSJeremy L Thompson   CeedCallBackend(CeedLoadSourceToBuffer(ceed, basis_kernel_path, &basis_kernel_source));
30123d4529eSJeremy L Thompson   CeedDebug256(ceed, CEED_DEBUG_COLOR_SUCCESS, "----- Loading Basis Kernel Source Complete! -----\n");
302d075f50bSSebastian Grimberg   CeedCallBackend(CeedCompile_Hip(ceed, basis_kernel_source, &data->module, 5, "BASIS_Q", num_qpts, "BASIS_P", num_nodes, "BASIS_Q_COMP_INTERP",
303d075f50bSSebastian Grimberg                                   q_comp_interp, "BASIS_Q_COMP_DERIV", q_comp_grad, "BASIS_NUM_COMP", num_comp));
304eb7e6cafSJeremy L Thompson   CeedCallBackend(CeedGetKernel_Hip(ceed, data->module, "Interp", &data->Interp));
305d075f50bSSebastian Grimberg   CeedCallBackend(CeedGetKernel_Hip(ceed, data->module, "InterpTranspose", &data->InterpTranspose));
306d075f50bSSebastian Grimberg   CeedCallBackend(CeedGetKernel_Hip(ceed, data->module, "Deriv", &data->Deriv));
307d075f50bSSebastian Grimberg   CeedCallBackend(CeedGetKernel_Hip(ceed, data->module, "DerivTranspose", &data->DerivTranspose));
308eb7e6cafSJeremy L Thompson   CeedCallBackend(CeedGetKernel_Hip(ceed, data->module, "Weight", &data->Weight));
3092b730f8bSJeremy L Thompson   CeedCallBackend(CeedFree(&basis_kernel_path));
3102b730f8bSJeremy L Thompson   CeedCallBackend(CeedFree(&basis_kernel_source));
311d075f50bSSebastian Grimberg 
312d075f50bSSebastian Grimberg   CeedCallBackend(CeedBasisSetData(basis, data));
313d075f50bSSebastian Grimberg 
314d075f50bSSebastian Grimberg   // Register backend functions
315d075f50bSSebastian Grimberg   CeedCallBackend(CeedSetBackendFunction(ceed, "Basis", basis, "Apply", CeedBasisApplyNonTensor_Hip));
316d075f50bSSebastian Grimberg   CeedCallBackend(CeedSetBackendFunction(ceed, "Basis", basis, "Destroy", CeedBasisDestroyNonTensor_Hip));
317d075f50bSSebastian Grimberg   return CEED_ERROR_SUCCESS;
318d075f50bSSebastian Grimberg }
319d075f50bSSebastian Grimberg 
320d075f50bSSebastian Grimberg //------------------------------------------------------------------------------
321d075f50bSSebastian Grimberg // Create non-tensor H(div)
322d075f50bSSebastian Grimberg //------------------------------------------------------------------------------
323d075f50bSSebastian Grimberg int CeedBasisCreateHdiv_Hip(CeedElemTopology topo, CeedInt dim, CeedInt num_nodes, CeedInt num_qpts, const CeedScalar *interp, const CeedScalar *div,
324d075f50bSSebastian Grimberg                             const CeedScalar *q_ref, const CeedScalar *q_weight, CeedBasis basis) {
325d075f50bSSebastian Grimberg   Ceed                    ceed;
32622070f95SJeremy L Thompson   char                   *basis_kernel_source;
32722070f95SJeremy L Thompson   const char             *basis_kernel_path;
328d075f50bSSebastian Grimberg   CeedInt                 num_comp, q_comp_interp, q_comp_div;
329d075f50bSSebastian Grimberg   const CeedInt           q_bytes = num_qpts * sizeof(CeedScalar);
330d075f50bSSebastian Grimberg   CeedBasisNonTensor_Hip *data;
331d075f50bSSebastian Grimberg 
332d075f50bSSebastian Grimberg   CeedCallBackend(CeedBasisGetCeed(basis, &ceed));
333d075f50bSSebastian Grimberg   CeedCallBackend(CeedCalloc(1, &data));
334d075f50bSSebastian Grimberg 
335d075f50bSSebastian Grimberg   // Copy basis data to GPU
336d075f50bSSebastian Grimberg   CeedCallBackend(CeedBasisGetNumQuadratureComponents(basis, CEED_EVAL_INTERP, &q_comp_interp));
337d075f50bSSebastian Grimberg   CeedCallBackend(CeedBasisGetNumQuadratureComponents(basis, CEED_EVAL_DIV, &q_comp_div));
338*097cc795SJames Wright   if (q_weight) {
339d075f50bSSebastian Grimberg     CeedCallHip(ceed, hipMalloc((void **)&data->d_q_weight, q_bytes));
340d075f50bSSebastian Grimberg     CeedCallHip(ceed, hipMemcpy(data->d_q_weight, q_weight, q_bytes, hipMemcpyHostToDevice));
341*097cc795SJames Wright   }
342d075f50bSSebastian Grimberg   if (interp) {
343d075f50bSSebastian Grimberg     const CeedInt interp_bytes = q_bytes * num_nodes * q_comp_interp;
344d075f50bSSebastian Grimberg 
345d075f50bSSebastian Grimberg     CeedCallHip(ceed, hipMalloc((void **)&data->d_interp, interp_bytes));
346d075f50bSSebastian Grimberg     CeedCallHip(ceed, hipMemcpy(data->d_interp, interp, interp_bytes, hipMemcpyHostToDevice));
347d075f50bSSebastian Grimberg   }
348d075f50bSSebastian Grimberg   if (div) {
349d075f50bSSebastian Grimberg     const CeedInt div_bytes = q_bytes * num_nodes * q_comp_div;
350d075f50bSSebastian Grimberg 
351d075f50bSSebastian Grimberg     CeedCallHip(ceed, hipMalloc((void **)&data->d_div, div_bytes));
352d075f50bSSebastian Grimberg     CeedCallHip(ceed, hipMemcpy(data->d_div, div, div_bytes, hipMemcpyHostToDevice));
353d075f50bSSebastian Grimberg   }
354d075f50bSSebastian Grimberg 
355d075f50bSSebastian Grimberg   // Compile basis kernels
356d075f50bSSebastian Grimberg   CeedCallBackend(CeedBasisGetNumComponents(basis, &num_comp));
357d075f50bSSebastian Grimberg   CeedCallBackend(CeedGetJitAbsolutePath(ceed, "ceed/jit-source/hip/hip-ref-basis-nontensor.h", &basis_kernel_path));
358d075f50bSSebastian Grimberg   CeedDebug256(ceed, CEED_DEBUG_COLOR_SUCCESS, "----- Loading Basis Kernel Source -----\n");
359d075f50bSSebastian Grimberg   CeedCallBackend(CeedLoadSourceToBuffer(ceed, basis_kernel_path, &basis_kernel_source));
360d075f50bSSebastian Grimberg   CeedDebug256(ceed, CEED_DEBUG_COLOR_SUCCESS, "----- Loading Basis Kernel Source Complete! -----\n");
361d075f50bSSebastian Grimberg   CeedCallBackend(CeedCompile_Hip(ceed, basis_kernel_source, &data->module, 5, "BASIS_Q", num_qpts, "BASIS_P", num_nodes, "BASIS_Q_COMP_INTERP",
362d075f50bSSebastian Grimberg                                   q_comp_interp, "BASIS_Q_COMP_DERIV", q_comp_div, "BASIS_NUM_COMP", num_comp));
363d075f50bSSebastian Grimberg   CeedCallBackend(CeedGetKernel_Hip(ceed, data->module, "Interp", &data->Interp));
364d075f50bSSebastian Grimberg   CeedCallBackend(CeedGetKernel_Hip(ceed, data->module, "InterpTranspose", &data->InterpTranspose));
365d075f50bSSebastian Grimberg   CeedCallBackend(CeedGetKernel_Hip(ceed, data->module, "Deriv", &data->Deriv));
366d075f50bSSebastian Grimberg   CeedCallBackend(CeedGetKernel_Hip(ceed, data->module, "DerivTranspose", &data->DerivTranspose));
367d075f50bSSebastian Grimberg   CeedCallBackend(CeedGetKernel_Hip(ceed, data->module, "Weight", &data->Weight));
368d075f50bSSebastian Grimberg   CeedCallBackend(CeedFree(&basis_kernel_path));
369d075f50bSSebastian Grimberg   CeedCallBackend(CeedFree(&basis_kernel_source));
370d075f50bSSebastian Grimberg 
371d075f50bSSebastian Grimberg   CeedCallBackend(CeedBasisSetData(basis, data));
372d075f50bSSebastian Grimberg 
373d075f50bSSebastian Grimberg   // Register backend functions
374d075f50bSSebastian Grimberg   CeedCallBackend(CeedSetBackendFunction(ceed, "Basis", basis, "Apply", CeedBasisApplyNonTensor_Hip));
375d075f50bSSebastian Grimberg   CeedCallBackend(CeedSetBackendFunction(ceed, "Basis", basis, "Destroy", CeedBasisDestroyNonTensor_Hip));
376d075f50bSSebastian Grimberg   return CEED_ERROR_SUCCESS;
377d075f50bSSebastian Grimberg }
378d075f50bSSebastian Grimberg 
379d075f50bSSebastian Grimberg //------------------------------------------------------------------------------
380d075f50bSSebastian Grimberg // Create non-tensor H(curl)
381d075f50bSSebastian Grimberg //------------------------------------------------------------------------------
382d075f50bSSebastian Grimberg int CeedBasisCreateHcurl_Hip(CeedElemTopology topo, CeedInt dim, CeedInt num_nodes, CeedInt num_qpts, const CeedScalar *interp,
383d075f50bSSebastian Grimberg                              const CeedScalar *curl, const CeedScalar *q_ref, const CeedScalar *q_weight, CeedBasis basis) {
384d075f50bSSebastian Grimberg   Ceed                    ceed;
38522070f95SJeremy L Thompson   char                   *basis_kernel_source;
38622070f95SJeremy L Thompson   const char             *basis_kernel_path;
387d075f50bSSebastian Grimberg   CeedInt                 num_comp, q_comp_interp, q_comp_curl;
388d075f50bSSebastian Grimberg   const CeedInt           q_bytes = num_qpts * sizeof(CeedScalar);
389d075f50bSSebastian Grimberg   CeedBasisNonTensor_Hip *data;
390d075f50bSSebastian Grimberg 
391d075f50bSSebastian Grimberg   CeedCallBackend(CeedBasisGetCeed(basis, &ceed));
392d075f50bSSebastian Grimberg   CeedCallBackend(CeedCalloc(1, &data));
393d075f50bSSebastian Grimberg 
394d075f50bSSebastian Grimberg   // Copy basis data to GPU
395d075f50bSSebastian Grimberg   CeedCallBackend(CeedBasisGetNumQuadratureComponents(basis, CEED_EVAL_INTERP, &q_comp_interp));
396d075f50bSSebastian Grimberg   CeedCallBackend(CeedBasisGetNumQuadratureComponents(basis, CEED_EVAL_CURL, &q_comp_curl));
397*097cc795SJames Wright   if (q_weight) {
398d075f50bSSebastian Grimberg     CeedCallHip(ceed, hipMalloc((void **)&data->d_q_weight, q_bytes));
399d075f50bSSebastian Grimberg     CeedCallHip(ceed, hipMemcpy(data->d_q_weight, q_weight, q_bytes, hipMemcpyHostToDevice));
400*097cc795SJames Wright   }
401d075f50bSSebastian Grimberg   if (interp) {
402d075f50bSSebastian Grimberg     const CeedInt interp_bytes = q_bytes * num_nodes * q_comp_interp;
403d075f50bSSebastian Grimberg 
404d075f50bSSebastian Grimberg     CeedCallHip(ceed, hipMalloc((void **)&data->d_interp, interp_bytes));
405d075f50bSSebastian Grimberg     CeedCallHip(ceed, hipMemcpy(data->d_interp, interp, interp_bytes, hipMemcpyHostToDevice));
406d075f50bSSebastian Grimberg   }
407d075f50bSSebastian Grimberg   if (curl) {
408d075f50bSSebastian Grimberg     const CeedInt curl_bytes = q_bytes * num_nodes * q_comp_curl;
409d075f50bSSebastian Grimberg 
410d075f50bSSebastian Grimberg     CeedCallHip(ceed, hipMalloc((void **)&data->d_curl, curl_bytes));
411d075f50bSSebastian Grimberg     CeedCallHip(ceed, hipMemcpy(data->d_curl, curl, curl_bytes, hipMemcpyHostToDevice));
412d075f50bSSebastian Grimberg   }
413d075f50bSSebastian Grimberg 
414d075f50bSSebastian Grimberg   // Compile basis kernels
415d075f50bSSebastian Grimberg   CeedCallBackend(CeedBasisGetNumComponents(basis, &num_comp));
416d075f50bSSebastian Grimberg   CeedCallBackend(CeedGetJitAbsolutePath(ceed, "ceed/jit-source/hip/hip-ref-basis-nontensor.h", &basis_kernel_path));
417d075f50bSSebastian Grimberg   CeedDebug256(ceed, CEED_DEBUG_COLOR_SUCCESS, "----- Loading Basis Kernel Source -----\n");
418d075f50bSSebastian Grimberg   CeedCallBackend(CeedLoadSourceToBuffer(ceed, basis_kernel_path, &basis_kernel_source));
419d075f50bSSebastian Grimberg   CeedDebug256(ceed, CEED_DEBUG_COLOR_SUCCESS, "----- Loading Basis Kernel Source Complete! -----\n");
420d075f50bSSebastian Grimberg   CeedCallBackend(CeedCompile_Hip(ceed, basis_kernel_source, &data->module, 5, "BASIS_Q", num_qpts, "BASIS_P", num_nodes, "BASIS_Q_COMP_INTERP",
421d075f50bSSebastian Grimberg                                   q_comp_interp, "BASIS_Q_COMP_DERIV", q_comp_curl, "BASIS_NUM_COMP", num_comp));
422d075f50bSSebastian Grimberg   CeedCallBackend(CeedGetKernel_Hip(ceed, data->module, "Interp", &data->Interp));
423d075f50bSSebastian Grimberg   CeedCallBackend(CeedGetKernel_Hip(ceed, data->module, "InterpTranspose", &data->InterpTranspose));
424d075f50bSSebastian Grimberg   CeedCallBackend(CeedGetKernel_Hip(ceed, data->module, "Deriv", &data->Deriv));
425d075f50bSSebastian Grimberg   CeedCallBackend(CeedGetKernel_Hip(ceed, data->module, "DerivTranspose", &data->DerivTranspose));
426d075f50bSSebastian Grimberg   CeedCallBackend(CeedGetKernel_Hip(ceed, data->module, "Weight", &data->Weight));
427d075f50bSSebastian Grimberg   CeedCallBackend(CeedFree(&basis_kernel_path));
428d075f50bSSebastian Grimberg   CeedCallBackend(CeedFree(&basis_kernel_source));
429d075f50bSSebastian Grimberg 
4302b730f8bSJeremy L Thompson   CeedCallBackend(CeedBasisSetData(basis, data));
4310d0321e0SJeremy L Thompson 
4320d0321e0SJeremy L Thompson   // Register backend functions
4332b730f8bSJeremy L Thompson   CeedCallBackend(CeedSetBackendFunction(ceed, "Basis", basis, "Apply", CeedBasisApplyNonTensor_Hip));
4342b730f8bSJeremy L Thompson   CeedCallBackend(CeedSetBackendFunction(ceed, "Basis", basis, "Destroy", CeedBasisDestroyNonTensor_Hip));
4350d0321e0SJeremy L Thompson   return CEED_ERROR_SUCCESS;
4360d0321e0SJeremy L Thompson }
4372a86cc9dSSebastian Grimberg 
4380d0321e0SJeremy L Thompson //------------------------------------------------------------------------------
439