xref: /libCEED/rust/libceed-sys/c-src/backends/hip-ref/ceed-hip-ref-basis.c (revision 9ea2cfd987bb00fe9579c83295ba7c97d225c170)
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;
22b7453713SJeremy L Thompson   CeedInt           Q_1d, dim;
23437930d1SJeremy L Thompson   const CeedInt     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 
32*9ea2cfd9SJeremy 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
38d075f50bSSebastian Grimberg   if (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: {
502b730f8bSJeremy L Thompson       void         *interp_args[] = {(void *)&num_elem, (void *)&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: {
562b730f8bSJeremy L Thompson       void         *grad_args[] = {(void *)&num_elem, (void *)&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: {
62437930d1SJeremy L Thompson       void     *weight_args[] = {(void *)&num_elem, (void *)&data->d_q_weight_1d, &d_v};
63b2165e7aSSebastian Grimberg       const int block_size_x  = Q_1d;
64b2165e7aSSebastian Grimberg       const int block_size_y  = dim >= 2 ? Q_1d : 1;
650d0321e0SJeremy L Thompson 
66b2165e7aSSebastian Grimberg       CeedCallBackend(CeedRunKernelDim_Hip(ceed, data->Weight, num_elem, block_size_x, block_size_y, 1, weight_args));
670d0321e0SJeremy L Thompson     } break;
68*9ea2cfd9SJeremy L Thompson     case CEED_EVAL_NONE: /* handled separately below */
69*9ea2cfd9SJeremy L Thompson       break;
700d0321e0SJeremy L Thompson     // LCOV_EXCL_START
710d0321e0SJeremy L Thompson     case CEED_EVAL_DIV:
720d0321e0SJeremy L Thompson     case CEED_EVAL_CURL:
73bcbe1c99SJeremy L Thompson       return CeedError(ceed, CEED_ERROR_BACKEND, "%s not supported", CeedEvalModes[eval_mode]);
740d0321e0SJeremy L Thompson       // LCOV_EXCL_STOP
750d0321e0SJeremy L Thompson   }
760d0321e0SJeremy L Thompson 
77*9ea2cfd9SJeremy L Thompson   // Restore vectors, cover CEED_EVAL_NONE
782b730f8bSJeremy L Thompson   CeedCallBackend(CeedVectorRestoreArray(v, &d_v));
79*9ea2cfd9SJeremy L Thompson   if (eval_mode == CEED_EVAL_NONE) CeedCallBackend(CeedVectorSetArray(v, CEED_MEM_DEVICE, CEED_COPY_VALUES, (CeedScalar *)d_u));
80*9ea2cfd9SJeremy L Thompson   if (eval_mode != CEED_EVAL_WEIGHT) CeedCallBackend(CeedVectorRestoreArrayRead(u, &d_u));
810d0321e0SJeremy L Thompson   return CEED_ERROR_SUCCESS;
820d0321e0SJeremy L Thompson }
830d0321e0SJeremy L Thompson 
840d0321e0SJeremy L Thompson //------------------------------------------------------------------------------
850d0321e0SJeremy L Thompson // Basis apply - non-tensor
860d0321e0SJeremy L Thompson //------------------------------------------------------------------------------
872b730f8bSJeremy L Thompson int CeedBasisApplyNonTensor_Hip(CeedBasis basis, const CeedInt num_elem, CeedTransposeMode t_mode, CeedEvalMode eval_mode, CeedVector u,
882b730f8bSJeremy L Thompson                                 CeedVector v) {
890d0321e0SJeremy L Thompson   Ceed                    ceed;
90437930d1SJeremy L Thompson   CeedInt                 num_nodes, num_qpts;
91437930d1SJeremy L Thompson   const CeedInt           transpose       = t_mode == CEED_TRANSPOSE;
92d075f50bSSebastian Grimberg   const int               elems_per_block = 1;
93d075f50bSSebastian Grimberg   const int               grid            = CeedDivUpInt(num_elem, elems_per_block);
940d0321e0SJeremy L Thompson   const CeedScalar       *d_u;
950d0321e0SJeremy L Thompson   CeedScalar             *d_v;
96b7453713SJeremy L Thompson   CeedBasisNonTensor_Hip *data;
97b7453713SJeremy L Thompson 
98b7453713SJeremy L Thompson   CeedCallBackend(CeedBasisGetCeed(basis, &ceed));
99b7453713SJeremy L Thompson   CeedCallBackend(CeedBasisGetData(basis, &data));
100b7453713SJeremy L Thompson   CeedCallBackend(CeedBasisGetNumQuadraturePoints(basis, &num_qpts));
101b7453713SJeremy L Thompson   CeedCallBackend(CeedBasisGetNumNodes(basis, &num_nodes));
102b7453713SJeremy L Thompson 
103*9ea2cfd9SJeremy L Thompson   // Get read/write access to u, v
104*9ea2cfd9SJeremy L Thompson   if (u != CEED_VECTOR_NONE) CeedCallBackend(CeedVectorGetArrayRead(u, CEED_MEM_DEVICE, &d_u));
105*9ea2cfd9SJeremy L Thompson   else CeedCheck(eval_mode == CEED_EVAL_WEIGHT, ceed, CEED_ERROR_BACKEND, "An input vector is required for this CeedEvalMode");
1062b730f8bSJeremy L Thompson   CeedCallBackend(CeedVectorGetArrayWrite(v, CEED_MEM_DEVICE, &d_v));
1070d0321e0SJeremy L Thompson 
1080d0321e0SJeremy L Thompson   // Clear v for transpose operation
109d075f50bSSebastian Grimberg   if (transpose) {
1101f9221feSJeremy L Thompson     CeedSize length;
111b7453713SJeremy L Thompson 
1122b730f8bSJeremy L Thompson     CeedCallBackend(CeedVectorGetLength(v, &length));
1132b730f8bSJeremy L Thompson     CeedCallHip(ceed, hipMemset(d_v, 0, length * sizeof(CeedScalar)));
1140d0321e0SJeremy L Thompson   }
1150d0321e0SJeremy L Thompson 
1160d0321e0SJeremy L Thompson   // Apply basis operation
117437930d1SJeremy L Thompson   switch (eval_mode) {
1180d0321e0SJeremy L Thompson     case CEED_EVAL_INTERP: {
119d075f50bSSebastian Grimberg       void     *interp_args[] = {(void *)&num_elem, &data->d_interp, &d_u, &d_v};
1202b730f8bSJeremy L Thompson       const int block_size_x  = transpose ? num_nodes : num_qpts;
121b2165e7aSSebastian Grimberg 
122d075f50bSSebastian Grimberg       if (transpose) {
123d075f50bSSebastian Grimberg         CeedCallBackend(CeedRunKernelDim_Hip(ceed, data->InterpTranspose, grid, block_size_x, 1, elems_per_block, interp_args));
124d075f50bSSebastian Grimberg       } else {
125b2165e7aSSebastian Grimberg         CeedCallBackend(CeedRunKernelDim_Hip(ceed, data->Interp, grid, block_size_x, 1, elems_per_block, interp_args));
126d075f50bSSebastian Grimberg       }
1270d0321e0SJeremy L Thompson     } break;
1280d0321e0SJeremy L Thompson     case CEED_EVAL_GRAD: {
129d075f50bSSebastian Grimberg       void     *grad_args[]  = {(void *)&num_elem, &data->d_grad, &d_u, &d_v};
1302b730f8bSJeremy L Thompson       const int block_size_x = transpose ? num_nodes : num_qpts;
131b2165e7aSSebastian Grimberg 
132d075f50bSSebastian Grimberg       if (transpose) {
133d075f50bSSebastian Grimberg         CeedCallBackend(CeedRunKernelDim_Hip(ceed, data->DerivTranspose, grid, block_size_x, 1, elems_per_block, grad_args));
134d075f50bSSebastian Grimberg       } else {
135d075f50bSSebastian Grimberg         CeedCallBackend(CeedRunKernelDim_Hip(ceed, data->Deriv, grid, block_size_x, 1, elems_per_block, grad_args));
136d075f50bSSebastian Grimberg       }
137d075f50bSSebastian Grimberg     } break;
138d075f50bSSebastian Grimberg     case CEED_EVAL_DIV: {
139d075f50bSSebastian Grimberg       void     *div_args[]   = {(void *)&num_elem, &data->d_div, &d_u, &d_v};
140d075f50bSSebastian Grimberg       const int block_size_x = transpose ? num_nodes : num_qpts;
141d075f50bSSebastian Grimberg 
142d075f50bSSebastian Grimberg       if (transpose) {
143d075f50bSSebastian Grimberg         CeedCallBackend(CeedRunKernelDim_Hip(ceed, data->DerivTranspose, grid, block_size_x, 1, elems_per_block, div_args));
144d075f50bSSebastian Grimberg       } else {
145d075f50bSSebastian Grimberg         CeedCallBackend(CeedRunKernelDim_Hip(ceed, data->Deriv, grid, block_size_x, 1, elems_per_block, div_args));
146d075f50bSSebastian Grimberg       }
147d075f50bSSebastian Grimberg     } break;
148d075f50bSSebastian Grimberg     case CEED_EVAL_CURL: {
149d075f50bSSebastian Grimberg       void     *curl_args[]  = {(void *)&num_elem, &data->d_curl, &d_u, &d_v};
150d075f50bSSebastian Grimberg       const int block_size_x = transpose ? num_nodes : num_qpts;
151d075f50bSSebastian Grimberg 
152d075f50bSSebastian Grimberg       if (transpose) {
153d075f50bSSebastian Grimberg         CeedCallBackend(CeedRunKernelDim_Hip(ceed, data->DerivTranspose, grid, block_size_x, 1, elems_per_block, curl_args));
154d075f50bSSebastian Grimberg       } else {
155d075f50bSSebastian Grimberg         CeedCallBackend(CeedRunKernelDim_Hip(ceed, data->Deriv, grid, block_size_x, 1, elems_per_block, curl_args));
156d075f50bSSebastian Grimberg       }
1570d0321e0SJeremy L Thompson     } break;
1580d0321e0SJeremy L Thompson     case CEED_EVAL_WEIGHT: {
159437930d1SJeremy L Thompson       void *weight_args[] = {(void *)&num_elem, (void *)&data->d_q_weight, &d_v};
160b2165e7aSSebastian Grimberg 
161b2165e7aSSebastian Grimberg       CeedCallBackend(CeedRunKernelDim_Hip(ceed, data->Weight, grid, num_qpts, 1, elems_per_block, weight_args));
1620d0321e0SJeremy L Thompson     } break;
163*9ea2cfd9SJeremy L Thompson     case CEED_EVAL_NONE: /* handled separately below */
164*9ea2cfd9SJeremy L Thompson       break;
1650d0321e0SJeremy L Thompson   }
1660d0321e0SJeremy L Thompson 
167*9ea2cfd9SJeremy L Thompson   // Restore vectors, cover CEED_EVAL_NONE
1682b730f8bSJeremy L Thompson   CeedCallBackend(CeedVectorRestoreArray(v, &d_v));
169*9ea2cfd9SJeremy L Thompson   if (eval_mode == CEED_EVAL_NONE) CeedCallBackend(CeedVectorSetArray(v, CEED_MEM_DEVICE, CEED_COPY_VALUES, (CeedScalar *)d_u));
170*9ea2cfd9SJeremy L Thompson   if (eval_mode != CEED_EVAL_WEIGHT) CeedCallBackend(CeedVectorRestoreArrayRead(u, &d_u));
1710d0321e0SJeremy L Thompson   return CEED_ERROR_SUCCESS;
1720d0321e0SJeremy L Thompson }
1730d0321e0SJeremy L Thompson 
1740d0321e0SJeremy L Thompson //------------------------------------------------------------------------------
1750d0321e0SJeremy L Thompson // Destroy tensor basis
1760d0321e0SJeremy L Thompson //------------------------------------------------------------------------------
1770d0321e0SJeremy L Thompson static int CeedBasisDestroy_Hip(CeedBasis basis) {
1780d0321e0SJeremy L Thompson   Ceed           ceed;
1790d0321e0SJeremy L Thompson   CeedBasis_Hip *data;
180b7453713SJeremy L Thompson 
181b7453713SJeremy L Thompson   CeedCallBackend(CeedBasisGetCeed(basis, &ceed));
1822b730f8bSJeremy L Thompson   CeedCallBackend(CeedBasisGetData(basis, &data));
1832b730f8bSJeremy L Thompson   CeedCallHip(ceed, hipModuleUnload(data->module));
1842b730f8bSJeremy L Thompson   CeedCallHip(ceed, hipFree(data->d_q_weight_1d));
1852b730f8bSJeremy L Thompson   CeedCallHip(ceed, hipFree(data->d_interp_1d));
1862b730f8bSJeremy L Thompson   CeedCallHip(ceed, hipFree(data->d_grad_1d));
1872b730f8bSJeremy L Thompson   CeedCallBackend(CeedFree(&data));
1880d0321e0SJeremy L Thompson   return CEED_ERROR_SUCCESS;
1890d0321e0SJeremy L Thompson }
1900d0321e0SJeremy L Thompson 
1910d0321e0SJeremy L Thompson //------------------------------------------------------------------------------
1920d0321e0SJeremy L Thompson // Destroy non-tensor basis
1930d0321e0SJeremy L Thompson //------------------------------------------------------------------------------
1940d0321e0SJeremy L Thompson static int CeedBasisDestroyNonTensor_Hip(CeedBasis basis) {
1950d0321e0SJeremy L Thompson   Ceed                    ceed;
1960d0321e0SJeremy L Thompson   CeedBasisNonTensor_Hip *data;
197b7453713SJeremy L Thompson 
198b7453713SJeremy L Thompson   CeedCallBackend(CeedBasisGetCeed(basis, &ceed));
1992b730f8bSJeremy L Thompson   CeedCallBackend(CeedBasisGetData(basis, &data));
2002b730f8bSJeremy L Thompson   CeedCallHip(ceed, hipModuleUnload(data->module));
2012b730f8bSJeremy L Thompson   CeedCallHip(ceed, hipFree(data->d_q_weight));
2022b730f8bSJeremy L Thompson   CeedCallHip(ceed, hipFree(data->d_interp));
2032b730f8bSJeremy L Thompson   CeedCallHip(ceed, hipFree(data->d_grad));
204d075f50bSSebastian Grimberg   CeedCallHip(ceed, hipFree(data->d_div));
205d075f50bSSebastian Grimberg   CeedCallHip(ceed, hipFree(data->d_curl));
2062b730f8bSJeremy L Thompson   CeedCallBackend(CeedFree(&data));
2070d0321e0SJeremy L Thompson   return CEED_ERROR_SUCCESS;
2080d0321e0SJeremy L Thompson }
2090d0321e0SJeremy L Thompson 
2100d0321e0SJeremy L Thompson //------------------------------------------------------------------------------
2110d0321e0SJeremy L Thompson // Create tensor
2120d0321e0SJeremy L Thompson //------------------------------------------------------------------------------
2132b730f8bSJeremy L Thompson int CeedBasisCreateTensorH1_Hip(CeedInt dim, CeedInt P_1d, CeedInt Q_1d, const CeedScalar *interp_1d, const CeedScalar *grad_1d,
2146574a04fSJeremy L Thompson                                 const CeedScalar *q_ref_1d, const CeedScalar *q_weight_1d, CeedBasis basis) {
2150d0321e0SJeremy L Thompson   Ceed           ceed;
216b7453713SJeremy L Thompson   char          *basis_kernel_path, *basis_kernel_source;
217b7453713SJeremy L Thompson   CeedInt        num_comp;
218b7453713SJeremy L Thompson   const CeedInt  q_bytes      = Q_1d * sizeof(CeedScalar);
219b7453713SJeremy L Thompson   const CeedInt  interp_bytes = q_bytes * P_1d;
2200d0321e0SJeremy L Thompson   CeedBasis_Hip *data;
221b7453713SJeremy L Thompson 
222b7453713SJeremy L Thompson   CeedCallBackend(CeedBasisGetCeed(basis, &ceed));
2232b730f8bSJeremy L Thompson   CeedCallBackend(CeedCalloc(1, &data));
2240d0321e0SJeremy L Thompson 
2250d0321e0SJeremy L Thompson   // Copy data to GPU
2262b730f8bSJeremy L Thompson   CeedCallHip(ceed, hipMalloc((void **)&data->d_q_weight_1d, q_bytes));
2272b730f8bSJeremy L Thompson   CeedCallHip(ceed, hipMemcpy(data->d_q_weight_1d, q_weight_1d, q_bytes, hipMemcpyHostToDevice));
2282b730f8bSJeremy L Thompson   CeedCallHip(ceed, hipMalloc((void **)&data->d_interp_1d, interp_bytes));
2292b730f8bSJeremy L Thompson   CeedCallHip(ceed, hipMemcpy(data->d_interp_1d, interp_1d, interp_bytes, hipMemcpyHostToDevice));
2302b730f8bSJeremy L Thompson   CeedCallHip(ceed, hipMalloc((void **)&data->d_grad_1d, interp_bytes));
2312b730f8bSJeremy L Thompson   CeedCallHip(ceed, hipMemcpy(data->d_grad_1d, grad_1d, interp_bytes, hipMemcpyHostToDevice));
2320d0321e0SJeremy L Thompson 
233ecc88aebSJeremy L Thompson   // Compile basis kernels
234b7453713SJeremy L Thompson   CeedCallBackend(CeedBasisGetNumComponents(basis, &num_comp));
2352b730f8bSJeremy L Thompson   CeedCallBackend(CeedGetJitAbsolutePath(ceed, "ceed/jit-source/hip/hip-ref-basis-tensor.h", &basis_kernel_path));
23623d4529eSJeremy L Thompson   CeedDebug256(ceed, CEED_DEBUG_COLOR_SUCCESS, "----- Loading Basis Kernel Source -----\n");
2372b730f8bSJeremy L Thompson   CeedCallBackend(CeedLoadSourceToBuffer(ceed, basis_kernel_path, &basis_kernel_source));
23823d4529eSJeremy L Thompson   CeedDebug256(ceed, CEED_DEBUG_COLOR_SUCCESS, "----- Loading Basis Kernel Source Complete! -----\n");
239eb7e6cafSJeremy 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",
240b7453713SJeremy L Thompson                                   num_comp * CeedIntPow(Q_1d > P_1d ? Q_1d : P_1d, dim), "BASIS_DIM", dim, "BASIS_NUM_COMP", num_comp,
241b7453713SJeremy L Thompson                                   "BASIS_NUM_NODES", CeedIntPow(P_1d, dim), "BASIS_NUM_QPTS", CeedIntPow(Q_1d, dim)));
242eb7e6cafSJeremy L Thompson   CeedCallBackend(CeedGetKernel_Hip(ceed, data->module, "Interp", &data->Interp));
243eb7e6cafSJeremy L Thompson   CeedCallBackend(CeedGetKernel_Hip(ceed, data->module, "Grad", &data->Grad));
244eb7e6cafSJeremy L Thompson   CeedCallBackend(CeedGetKernel_Hip(ceed, data->module, "Weight", &data->Weight));
2452b730f8bSJeremy L Thompson   CeedCallBackend(CeedFree(&basis_kernel_path));
2462b730f8bSJeremy L Thompson   CeedCallBackend(CeedFree(&basis_kernel_source));
247437930d1SJeremy L Thompson 
2482b730f8bSJeremy L Thompson   CeedCallBackend(CeedBasisSetData(basis, data));
2490d0321e0SJeremy L Thompson 
250d075f50bSSebastian Grimberg   // Register backend functions
2512b730f8bSJeremy L Thompson   CeedCallBackend(CeedSetBackendFunction(ceed, "Basis", basis, "Apply", CeedBasisApply_Hip));
2522b730f8bSJeremy L Thompson   CeedCallBackend(CeedSetBackendFunction(ceed, "Basis", basis, "Destroy", CeedBasisDestroy_Hip));
2530d0321e0SJeremy L Thompson   return CEED_ERROR_SUCCESS;
2540d0321e0SJeremy L Thompson }
2550d0321e0SJeremy L Thompson 
2560d0321e0SJeremy L Thompson //------------------------------------------------------------------------------
257d075f50bSSebastian Grimberg // Create non-tensor H^1
2580d0321e0SJeremy L Thompson //------------------------------------------------------------------------------
2592b730f8bSJeremy L Thompson int CeedBasisCreateH1_Hip(CeedElemTopology topo, CeedInt dim, CeedInt num_nodes, CeedInt num_qpts, const CeedScalar *interp, const CeedScalar *grad,
26051475c7cSJeremy L Thompson                           const CeedScalar *q_ref, const CeedScalar *q_weight, CeedBasis basis) {
2610d0321e0SJeremy L Thompson   Ceed                    ceed;
262b7453713SJeremy L Thompson   char                   *basis_kernel_path, *basis_kernel_source;
263d075f50bSSebastian Grimberg   CeedInt                 num_comp, q_comp_interp, q_comp_grad;
264b7453713SJeremy L Thompson   const CeedInt           q_bytes = num_qpts * sizeof(CeedScalar);
2650d0321e0SJeremy L Thompson   CeedBasisNonTensor_Hip *data;
266b7453713SJeremy L Thompson 
267b7453713SJeremy L Thompson   CeedCallBackend(CeedBasisGetCeed(basis, &ceed));
2682b730f8bSJeremy L Thompson   CeedCallBackend(CeedCalloc(1, &data));
2690d0321e0SJeremy L Thompson 
2700d0321e0SJeremy L Thompson   // Copy basis data to GPU
271d075f50bSSebastian Grimberg   CeedCallBackend(CeedBasisGetNumQuadratureComponents(basis, CEED_EVAL_INTERP, &q_comp_interp));
272d075f50bSSebastian Grimberg   CeedCallBackend(CeedBasisGetNumQuadratureComponents(basis, CEED_EVAL_GRAD, &q_comp_grad));
2732b730f8bSJeremy L Thompson   CeedCallHip(ceed, hipMalloc((void **)&data->d_q_weight, q_bytes));
2742b730f8bSJeremy L Thompson   CeedCallHip(ceed, hipMemcpy(data->d_q_weight, q_weight, q_bytes, hipMemcpyHostToDevice));
275d075f50bSSebastian Grimberg   if (interp) {
276d075f50bSSebastian Grimberg     const CeedInt interp_bytes = q_bytes * num_nodes * q_comp_interp;
277d075f50bSSebastian Grimberg 
2782b730f8bSJeremy L Thompson     CeedCallHip(ceed, hipMalloc((void **)&data->d_interp, interp_bytes));
2792b730f8bSJeremy L Thompson     CeedCallHip(ceed, hipMemcpy(data->d_interp, interp, interp_bytes, hipMemcpyHostToDevice));
280d075f50bSSebastian Grimberg   }
281d075f50bSSebastian Grimberg   if (grad) {
282d075f50bSSebastian Grimberg     const CeedInt grad_bytes = q_bytes * num_nodes * q_comp_grad;
283d075f50bSSebastian Grimberg 
2842b730f8bSJeremy L Thompson     CeedCallHip(ceed, hipMalloc((void **)&data->d_grad, grad_bytes));
2852b730f8bSJeremy L Thompson     CeedCallHip(ceed, hipMemcpy(data->d_grad, grad, grad_bytes, hipMemcpyHostToDevice));
286d075f50bSSebastian Grimberg   }
2870d0321e0SJeremy L Thompson 
2880d0321e0SJeremy L Thompson   // Compile basis kernels
289b7453713SJeremy L Thompson   CeedCallBackend(CeedBasisGetNumComponents(basis, &num_comp));
2902b730f8bSJeremy L Thompson   CeedCallBackend(CeedGetJitAbsolutePath(ceed, "ceed/jit-source/hip/hip-ref-basis-nontensor.h", &basis_kernel_path));
29123d4529eSJeremy L Thompson   CeedDebug256(ceed, CEED_DEBUG_COLOR_SUCCESS, "----- Loading Basis Kernel Source -----\n");
2922b730f8bSJeremy L Thompson   CeedCallBackend(CeedLoadSourceToBuffer(ceed, basis_kernel_path, &basis_kernel_source));
29323d4529eSJeremy L Thompson   CeedDebug256(ceed, CEED_DEBUG_COLOR_SUCCESS, "----- Loading Basis Kernel Source Complete! -----\n");
294d075f50bSSebastian Grimberg   CeedCallBackend(CeedCompile_Hip(ceed, basis_kernel_source, &data->module, 5, "BASIS_Q", num_qpts, "BASIS_P", num_nodes, "BASIS_Q_COMP_INTERP",
295d075f50bSSebastian Grimberg                                   q_comp_interp, "BASIS_Q_COMP_DERIV", q_comp_grad, "BASIS_NUM_COMP", num_comp));
296eb7e6cafSJeremy L Thompson   CeedCallBackend(CeedGetKernel_Hip(ceed, data->module, "Interp", &data->Interp));
297d075f50bSSebastian Grimberg   CeedCallBackend(CeedGetKernel_Hip(ceed, data->module, "InterpTranspose", &data->InterpTranspose));
298d075f50bSSebastian Grimberg   CeedCallBackend(CeedGetKernel_Hip(ceed, data->module, "Deriv", &data->Deriv));
299d075f50bSSebastian Grimberg   CeedCallBackend(CeedGetKernel_Hip(ceed, data->module, "DerivTranspose", &data->DerivTranspose));
300eb7e6cafSJeremy L Thompson   CeedCallBackend(CeedGetKernel_Hip(ceed, data->module, "Weight", &data->Weight));
3012b730f8bSJeremy L Thompson   CeedCallBackend(CeedFree(&basis_kernel_path));
3022b730f8bSJeremy L Thompson   CeedCallBackend(CeedFree(&basis_kernel_source));
303d075f50bSSebastian Grimberg 
304d075f50bSSebastian Grimberg   CeedCallBackend(CeedBasisSetData(basis, data));
305d075f50bSSebastian Grimberg 
306d075f50bSSebastian Grimberg   // Register backend functions
307d075f50bSSebastian Grimberg   CeedCallBackend(CeedSetBackendFunction(ceed, "Basis", basis, "Apply", CeedBasisApplyNonTensor_Hip));
308d075f50bSSebastian Grimberg   CeedCallBackend(CeedSetBackendFunction(ceed, "Basis", basis, "Destroy", CeedBasisDestroyNonTensor_Hip));
309d075f50bSSebastian Grimberg   return CEED_ERROR_SUCCESS;
310d075f50bSSebastian Grimberg }
311d075f50bSSebastian Grimberg 
312d075f50bSSebastian Grimberg //------------------------------------------------------------------------------
313d075f50bSSebastian Grimberg // Create non-tensor H(div)
314d075f50bSSebastian Grimberg //------------------------------------------------------------------------------
315d075f50bSSebastian Grimberg int CeedBasisCreateHdiv_Hip(CeedElemTopology topo, CeedInt dim, CeedInt num_nodes, CeedInt num_qpts, const CeedScalar *interp, const CeedScalar *div,
316d075f50bSSebastian Grimberg                             const CeedScalar *q_ref, const CeedScalar *q_weight, CeedBasis basis) {
317d075f50bSSebastian Grimberg   Ceed                    ceed;
318d075f50bSSebastian Grimberg   char                   *basis_kernel_path, *basis_kernel_source;
319d075f50bSSebastian Grimberg   CeedInt                 num_comp, q_comp_interp, q_comp_div;
320d075f50bSSebastian Grimberg   const CeedInt           q_bytes = num_qpts * sizeof(CeedScalar);
321d075f50bSSebastian Grimberg   CeedBasisNonTensor_Hip *data;
322d075f50bSSebastian Grimberg 
323d075f50bSSebastian Grimberg   CeedCallBackend(CeedBasisGetCeed(basis, &ceed));
324d075f50bSSebastian Grimberg   CeedCallBackend(CeedCalloc(1, &data));
325d075f50bSSebastian Grimberg 
326d075f50bSSebastian Grimberg   // Copy basis data to GPU
327d075f50bSSebastian Grimberg   CeedCallBackend(CeedBasisGetNumQuadratureComponents(basis, CEED_EVAL_INTERP, &q_comp_interp));
328d075f50bSSebastian Grimberg   CeedCallBackend(CeedBasisGetNumQuadratureComponents(basis, CEED_EVAL_DIV, &q_comp_div));
329d075f50bSSebastian Grimberg   CeedCallHip(ceed, hipMalloc((void **)&data->d_q_weight, q_bytes));
330d075f50bSSebastian Grimberg   CeedCallHip(ceed, hipMemcpy(data->d_q_weight, q_weight, q_bytes, hipMemcpyHostToDevice));
331d075f50bSSebastian Grimberg   if (interp) {
332d075f50bSSebastian Grimberg     const CeedInt interp_bytes = q_bytes * num_nodes * q_comp_interp;
333d075f50bSSebastian Grimberg 
334d075f50bSSebastian Grimberg     CeedCallHip(ceed, hipMalloc((void **)&data->d_interp, interp_bytes));
335d075f50bSSebastian Grimberg     CeedCallHip(ceed, hipMemcpy(data->d_interp, interp, interp_bytes, hipMemcpyHostToDevice));
336d075f50bSSebastian Grimberg   }
337d075f50bSSebastian Grimberg   if (div) {
338d075f50bSSebastian Grimberg     const CeedInt div_bytes = q_bytes * num_nodes * q_comp_div;
339d075f50bSSebastian Grimberg 
340d075f50bSSebastian Grimberg     CeedCallHip(ceed, hipMalloc((void **)&data->d_div, div_bytes));
341d075f50bSSebastian Grimberg     CeedCallHip(ceed, hipMemcpy(data->d_div, div, div_bytes, hipMemcpyHostToDevice));
342d075f50bSSebastian Grimberg   }
343d075f50bSSebastian Grimberg 
344d075f50bSSebastian Grimberg   // Compile basis kernels
345d075f50bSSebastian Grimberg   CeedCallBackend(CeedBasisGetNumComponents(basis, &num_comp));
346d075f50bSSebastian Grimberg   CeedCallBackend(CeedGetJitAbsolutePath(ceed, "ceed/jit-source/hip/hip-ref-basis-nontensor.h", &basis_kernel_path));
347d075f50bSSebastian Grimberg   CeedDebug256(ceed, CEED_DEBUG_COLOR_SUCCESS, "----- Loading Basis Kernel Source -----\n");
348d075f50bSSebastian Grimberg   CeedCallBackend(CeedLoadSourceToBuffer(ceed, basis_kernel_path, &basis_kernel_source));
349d075f50bSSebastian Grimberg   CeedDebug256(ceed, CEED_DEBUG_COLOR_SUCCESS, "----- Loading Basis Kernel Source Complete! -----\n");
350d075f50bSSebastian Grimberg   CeedCallBackend(CeedCompile_Hip(ceed, basis_kernel_source, &data->module, 5, "BASIS_Q", num_qpts, "BASIS_P", num_nodes, "BASIS_Q_COMP_INTERP",
351d075f50bSSebastian Grimberg                                   q_comp_interp, "BASIS_Q_COMP_DERIV", q_comp_div, "BASIS_NUM_COMP", num_comp));
352d075f50bSSebastian Grimberg   CeedCallBackend(CeedGetKernel_Hip(ceed, data->module, "Interp", &data->Interp));
353d075f50bSSebastian Grimberg   CeedCallBackend(CeedGetKernel_Hip(ceed, data->module, "InterpTranspose", &data->InterpTranspose));
354d075f50bSSebastian Grimberg   CeedCallBackend(CeedGetKernel_Hip(ceed, data->module, "Deriv", &data->Deriv));
355d075f50bSSebastian Grimberg   CeedCallBackend(CeedGetKernel_Hip(ceed, data->module, "DerivTranspose", &data->DerivTranspose));
356d075f50bSSebastian Grimberg   CeedCallBackend(CeedGetKernel_Hip(ceed, data->module, "Weight", &data->Weight));
357d075f50bSSebastian Grimberg   CeedCallBackend(CeedFree(&basis_kernel_path));
358d075f50bSSebastian Grimberg   CeedCallBackend(CeedFree(&basis_kernel_source));
359d075f50bSSebastian Grimberg 
360d075f50bSSebastian Grimberg   CeedCallBackend(CeedBasisSetData(basis, data));
361d075f50bSSebastian Grimberg 
362d075f50bSSebastian Grimberg   // Register backend functions
363d075f50bSSebastian Grimberg   CeedCallBackend(CeedSetBackendFunction(ceed, "Basis", basis, "Apply", CeedBasisApplyNonTensor_Hip));
364d075f50bSSebastian Grimberg   CeedCallBackend(CeedSetBackendFunction(ceed, "Basis", basis, "Destroy", CeedBasisDestroyNonTensor_Hip));
365d075f50bSSebastian Grimberg   return CEED_ERROR_SUCCESS;
366d075f50bSSebastian Grimberg }
367d075f50bSSebastian Grimberg 
368d075f50bSSebastian Grimberg //------------------------------------------------------------------------------
369d075f50bSSebastian Grimberg // Create non-tensor H(curl)
370d075f50bSSebastian Grimberg //------------------------------------------------------------------------------
371d075f50bSSebastian Grimberg int CeedBasisCreateHcurl_Hip(CeedElemTopology topo, CeedInt dim, CeedInt num_nodes, CeedInt num_qpts, const CeedScalar *interp,
372d075f50bSSebastian Grimberg                              const CeedScalar *curl, const CeedScalar *q_ref, const CeedScalar *q_weight, CeedBasis basis) {
373d075f50bSSebastian Grimberg   Ceed                    ceed;
374d075f50bSSebastian Grimberg   char                   *basis_kernel_path, *basis_kernel_source;
375d075f50bSSebastian Grimberg   CeedInt                 num_comp, q_comp_interp, q_comp_curl;
376d075f50bSSebastian Grimberg   const CeedInt           q_bytes = num_qpts * sizeof(CeedScalar);
377d075f50bSSebastian Grimberg   CeedBasisNonTensor_Hip *data;
378d075f50bSSebastian Grimberg 
379d075f50bSSebastian Grimberg   CeedCallBackend(CeedBasisGetCeed(basis, &ceed));
380d075f50bSSebastian Grimberg   CeedCallBackend(CeedCalloc(1, &data));
381d075f50bSSebastian Grimberg 
382d075f50bSSebastian Grimberg   // Copy basis data to GPU
383d075f50bSSebastian Grimberg   CeedCallBackend(CeedBasisGetNumQuadratureComponents(basis, CEED_EVAL_INTERP, &q_comp_interp));
384d075f50bSSebastian Grimberg   CeedCallBackend(CeedBasisGetNumQuadratureComponents(basis, CEED_EVAL_CURL, &q_comp_curl));
385d075f50bSSebastian Grimberg   CeedCallHip(ceed, hipMalloc((void **)&data->d_q_weight, q_bytes));
386d075f50bSSebastian Grimberg   CeedCallHip(ceed, hipMemcpy(data->d_q_weight, q_weight, q_bytes, hipMemcpyHostToDevice));
387d075f50bSSebastian Grimberg   if (interp) {
388d075f50bSSebastian Grimberg     const CeedInt interp_bytes = q_bytes * num_nodes * q_comp_interp;
389d075f50bSSebastian Grimberg 
390d075f50bSSebastian Grimberg     CeedCallHip(ceed, hipMalloc((void **)&data->d_interp, interp_bytes));
391d075f50bSSebastian Grimberg     CeedCallHip(ceed, hipMemcpy(data->d_interp, interp, interp_bytes, hipMemcpyHostToDevice));
392d075f50bSSebastian Grimberg   }
393d075f50bSSebastian Grimberg   if (curl) {
394d075f50bSSebastian Grimberg     const CeedInt curl_bytes = q_bytes * num_nodes * q_comp_curl;
395d075f50bSSebastian Grimberg 
396d075f50bSSebastian Grimberg     CeedCallHip(ceed, hipMalloc((void **)&data->d_curl, curl_bytes));
397d075f50bSSebastian Grimberg     CeedCallHip(ceed, hipMemcpy(data->d_curl, curl, curl_bytes, hipMemcpyHostToDevice));
398d075f50bSSebastian Grimberg   }
399d075f50bSSebastian Grimberg 
400d075f50bSSebastian Grimberg   // Compile basis kernels
401d075f50bSSebastian Grimberg   CeedCallBackend(CeedBasisGetNumComponents(basis, &num_comp));
402d075f50bSSebastian Grimberg   CeedCallBackend(CeedGetJitAbsolutePath(ceed, "ceed/jit-source/hip/hip-ref-basis-nontensor.h", &basis_kernel_path));
403d075f50bSSebastian Grimberg   CeedDebug256(ceed, CEED_DEBUG_COLOR_SUCCESS, "----- Loading Basis Kernel Source -----\n");
404d075f50bSSebastian Grimberg   CeedCallBackend(CeedLoadSourceToBuffer(ceed, basis_kernel_path, &basis_kernel_source));
405d075f50bSSebastian Grimberg   CeedDebug256(ceed, CEED_DEBUG_COLOR_SUCCESS, "----- Loading Basis Kernel Source Complete! -----\n");
406d075f50bSSebastian Grimberg   CeedCallBackend(CeedCompile_Hip(ceed, basis_kernel_source, &data->module, 5, "BASIS_Q", num_qpts, "BASIS_P", num_nodes, "BASIS_Q_COMP_INTERP",
407d075f50bSSebastian Grimberg                                   q_comp_interp, "BASIS_Q_COMP_DERIV", q_comp_curl, "BASIS_NUM_COMP", num_comp));
408d075f50bSSebastian Grimberg   CeedCallBackend(CeedGetKernel_Hip(ceed, data->module, "Interp", &data->Interp));
409d075f50bSSebastian Grimberg   CeedCallBackend(CeedGetKernel_Hip(ceed, data->module, "InterpTranspose", &data->InterpTranspose));
410d075f50bSSebastian Grimberg   CeedCallBackend(CeedGetKernel_Hip(ceed, data->module, "Deriv", &data->Deriv));
411d075f50bSSebastian Grimberg   CeedCallBackend(CeedGetKernel_Hip(ceed, data->module, "DerivTranspose", &data->DerivTranspose));
412d075f50bSSebastian Grimberg   CeedCallBackend(CeedGetKernel_Hip(ceed, data->module, "Weight", &data->Weight));
413d075f50bSSebastian Grimberg   CeedCallBackend(CeedFree(&basis_kernel_path));
414d075f50bSSebastian Grimberg   CeedCallBackend(CeedFree(&basis_kernel_source));
415d075f50bSSebastian Grimberg 
4162b730f8bSJeremy L Thompson   CeedCallBackend(CeedBasisSetData(basis, data));
4170d0321e0SJeremy L Thompson 
4180d0321e0SJeremy L Thompson   // Register backend functions
4192b730f8bSJeremy L Thompson   CeedCallBackend(CeedSetBackendFunction(ceed, "Basis", basis, "Apply", CeedBasisApplyNonTensor_Hip));
4202b730f8bSJeremy L Thompson   CeedCallBackend(CeedSetBackendFunction(ceed, "Basis", basis, "Destroy", CeedBasisDestroyNonTensor_Hip));
4210d0321e0SJeremy L Thompson   return CEED_ERROR_SUCCESS;
4220d0321e0SJeremy L Thompson }
4232a86cc9dSSebastian Grimberg 
4240d0321e0SJeremy L Thompson //------------------------------------------------------------------------------
425