xref: /libCEED/rust/libceed-sys/c-src/backends/hip-ref/ceed-hip-ref-basis.c (revision bcbe1c99be4be6e5341a66695d3b47170158a1bf)
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 
32b7453713SJeremy L Thompson   // Read vectors
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;
680d0321e0SJeremy L Thompson     // LCOV_EXCL_START
690d0321e0SJeremy L Thompson     case CEED_EVAL_DIV:
700d0321e0SJeremy L Thompson     case CEED_EVAL_CURL:
71*bcbe1c99SJeremy L Thompson       return CeedError(ceed, CEED_ERROR_BACKEND, "%s not supported", CeedEvalModes[eval_mode]);
720d0321e0SJeremy L Thompson     case CEED_EVAL_NONE:
732b730f8bSJeremy L Thompson       return CeedError(ceed, CEED_ERROR_BACKEND, "CEED_EVAL_NONE does not make sense in this context");
740d0321e0SJeremy L Thompson       // LCOV_EXCL_STOP
750d0321e0SJeremy L Thompson   }
760d0321e0SJeremy L Thompson 
770d0321e0SJeremy L Thompson   // Restore vectors
78437930d1SJeremy L Thompson   if (eval_mode != CEED_EVAL_WEIGHT) {
792b730f8bSJeremy L Thompson     CeedCallBackend(CeedVectorRestoreArrayRead(u, &d_u));
800d0321e0SJeremy L Thompson   }
812b730f8bSJeremy L Thompson   CeedCallBackend(CeedVectorRestoreArray(v, &d_v));
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;
92437930d1SJeremy L Thompson   const CeedInt           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 
104b7453713SJeremy L Thompson   // Read vectors
105437930d1SJeremy L Thompson   if (eval_mode != CEED_EVAL_WEIGHT) {
1062b730f8bSJeremy L Thompson     CeedCallBackend(CeedVectorGetArrayRead(u, CEED_MEM_DEVICE, &d_u));
1070d0321e0SJeremy L Thompson   }
1082b730f8bSJeremy L Thompson   CeedCallBackend(CeedVectorGetArrayWrite(v, CEED_MEM_DEVICE, &d_v));
1090d0321e0SJeremy L Thompson 
1100d0321e0SJeremy L Thompson   // Clear v for transpose operation
111d075f50bSSebastian Grimberg   if (transpose) {
1121f9221feSJeremy L Thompson     CeedSize length;
113b7453713SJeremy L Thompson 
1142b730f8bSJeremy L Thompson     CeedCallBackend(CeedVectorGetLength(v, &length));
1152b730f8bSJeremy L Thompson     CeedCallHip(ceed, hipMemset(d_v, 0, length * sizeof(CeedScalar)));
1160d0321e0SJeremy L Thompson   }
1170d0321e0SJeremy L Thompson 
1180d0321e0SJeremy L Thompson   // Apply basis operation
119437930d1SJeremy L Thompson   switch (eval_mode) {
1200d0321e0SJeremy L Thompson     case CEED_EVAL_INTERP: {
121d075f50bSSebastian Grimberg       void     *interp_args[] = {(void *)&num_elem, &data->d_interp, &d_u, &d_v};
1222b730f8bSJeremy L Thompson       const int block_size_x  = transpose ? num_nodes : num_qpts;
123b2165e7aSSebastian Grimberg 
124d075f50bSSebastian Grimberg       if (transpose) {
125d075f50bSSebastian Grimberg         CeedCallBackend(CeedRunKernelDim_Hip(ceed, data->InterpTranspose, grid, block_size_x, 1, elems_per_block, interp_args));
126d075f50bSSebastian Grimberg       } else {
127b2165e7aSSebastian Grimberg         CeedCallBackend(CeedRunKernelDim_Hip(ceed, data->Interp, grid, block_size_x, 1, elems_per_block, interp_args));
128d075f50bSSebastian Grimberg       }
1290d0321e0SJeremy L Thompson     } break;
1300d0321e0SJeremy L Thompson     case CEED_EVAL_GRAD: {
131d075f50bSSebastian Grimberg       void     *grad_args[]  = {(void *)&num_elem, &data->d_grad, &d_u, &d_v};
1322b730f8bSJeremy L Thompson       const int block_size_x = transpose ? num_nodes : num_qpts;
133b2165e7aSSebastian Grimberg 
134d075f50bSSebastian Grimberg       if (transpose) {
135d075f50bSSebastian Grimberg         CeedCallBackend(CeedRunKernelDim_Hip(ceed, data->DerivTranspose, grid, block_size_x, 1, elems_per_block, grad_args));
136d075f50bSSebastian Grimberg       } else {
137d075f50bSSebastian Grimberg         CeedCallBackend(CeedRunKernelDim_Hip(ceed, data->Deriv, grid, block_size_x, 1, elems_per_block, grad_args));
138d075f50bSSebastian Grimberg       }
139d075f50bSSebastian Grimberg     } break;
140d075f50bSSebastian Grimberg     case CEED_EVAL_DIV: {
141d075f50bSSebastian Grimberg       void     *div_args[]   = {(void *)&num_elem, &data->d_div, &d_u, &d_v};
142d075f50bSSebastian Grimberg       const int block_size_x = transpose ? num_nodes : num_qpts;
143d075f50bSSebastian Grimberg 
144d075f50bSSebastian Grimberg       if (transpose) {
145d075f50bSSebastian Grimberg         CeedCallBackend(CeedRunKernelDim_Hip(ceed, data->DerivTranspose, grid, block_size_x, 1, elems_per_block, div_args));
146d075f50bSSebastian Grimberg       } else {
147d075f50bSSebastian Grimberg         CeedCallBackend(CeedRunKernelDim_Hip(ceed, data->Deriv, grid, block_size_x, 1, elems_per_block, div_args));
148d075f50bSSebastian Grimberg       }
149d075f50bSSebastian Grimberg     } break;
150d075f50bSSebastian Grimberg     case CEED_EVAL_CURL: {
151d075f50bSSebastian Grimberg       void     *curl_args[]  = {(void *)&num_elem, &data->d_curl, &d_u, &d_v};
152d075f50bSSebastian Grimberg       const int block_size_x = transpose ? num_nodes : num_qpts;
153d075f50bSSebastian Grimberg 
154d075f50bSSebastian Grimberg       if (transpose) {
155d075f50bSSebastian Grimberg         CeedCallBackend(CeedRunKernelDim_Hip(ceed, data->DerivTranspose, grid, block_size_x, 1, elems_per_block, curl_args));
156d075f50bSSebastian Grimberg       } else {
157d075f50bSSebastian Grimberg         CeedCallBackend(CeedRunKernelDim_Hip(ceed, data->Deriv, grid, block_size_x, 1, elems_per_block, curl_args));
158d075f50bSSebastian Grimberg       }
1590d0321e0SJeremy L Thompson     } break;
1600d0321e0SJeremy L Thompson     case CEED_EVAL_WEIGHT: {
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;
1650d0321e0SJeremy L Thompson     // LCOV_EXCL_START
1660d0321e0SJeremy L Thompson     case CEED_EVAL_NONE:
1672b730f8bSJeremy L Thompson       return CeedError(ceed, CEED_ERROR_BACKEND, "CEED_EVAL_NONE does not make sense in this context");
1680d0321e0SJeremy L Thompson       // LCOV_EXCL_STOP
1690d0321e0SJeremy L Thompson   }
1700d0321e0SJeremy L Thompson 
1710d0321e0SJeremy L Thompson   // Restore vectors
172437930d1SJeremy L Thompson   if (eval_mode != CEED_EVAL_WEIGHT) {
1732b730f8bSJeremy L Thompson     CeedCallBackend(CeedVectorRestoreArrayRead(u, &d_u));
1740d0321e0SJeremy L Thompson   }
1752b730f8bSJeremy L Thompson   CeedCallBackend(CeedVectorRestoreArray(v, &d_v));
1760d0321e0SJeremy L Thompson   return CEED_ERROR_SUCCESS;
1770d0321e0SJeremy L Thompson }
1780d0321e0SJeremy L Thompson 
1790d0321e0SJeremy L Thompson //------------------------------------------------------------------------------
1800d0321e0SJeremy L Thompson // Destroy tensor basis
1810d0321e0SJeremy L Thompson //------------------------------------------------------------------------------
1820d0321e0SJeremy L Thompson static int CeedBasisDestroy_Hip(CeedBasis basis) {
1830d0321e0SJeremy L Thompson   Ceed           ceed;
1840d0321e0SJeremy L Thompson   CeedBasis_Hip *data;
185b7453713SJeremy L Thompson 
186b7453713SJeremy L Thompson   CeedCallBackend(CeedBasisGetCeed(basis, &ceed));
1872b730f8bSJeremy L Thompson   CeedCallBackend(CeedBasisGetData(basis, &data));
1882b730f8bSJeremy L Thompson   CeedCallHip(ceed, hipModuleUnload(data->module));
1892b730f8bSJeremy L Thompson   CeedCallHip(ceed, hipFree(data->d_q_weight_1d));
1902b730f8bSJeremy L Thompson   CeedCallHip(ceed, hipFree(data->d_interp_1d));
1912b730f8bSJeremy L Thompson   CeedCallHip(ceed, hipFree(data->d_grad_1d));
1922b730f8bSJeremy L Thompson   CeedCallBackend(CeedFree(&data));
1930d0321e0SJeremy L Thompson   return CEED_ERROR_SUCCESS;
1940d0321e0SJeremy L Thompson }
1950d0321e0SJeremy L Thompson 
1960d0321e0SJeremy L Thompson //------------------------------------------------------------------------------
1970d0321e0SJeremy L Thompson // Destroy non-tensor basis
1980d0321e0SJeremy L Thompson //------------------------------------------------------------------------------
1990d0321e0SJeremy L Thompson static int CeedBasisDestroyNonTensor_Hip(CeedBasis basis) {
2000d0321e0SJeremy L Thompson   Ceed                    ceed;
2010d0321e0SJeremy L Thompson   CeedBasisNonTensor_Hip *data;
202b7453713SJeremy L Thompson 
203b7453713SJeremy L Thompson   CeedCallBackend(CeedBasisGetCeed(basis, &ceed));
2042b730f8bSJeremy L Thompson   CeedCallBackend(CeedBasisGetData(basis, &data));
2052b730f8bSJeremy L Thompson   CeedCallHip(ceed, hipModuleUnload(data->module));
2062b730f8bSJeremy L Thompson   CeedCallHip(ceed, hipFree(data->d_q_weight));
2072b730f8bSJeremy L Thompson   CeedCallHip(ceed, hipFree(data->d_interp));
2082b730f8bSJeremy L Thompson   CeedCallHip(ceed, hipFree(data->d_grad));
209d075f50bSSebastian Grimberg   CeedCallHip(ceed, hipFree(data->d_div));
210d075f50bSSebastian Grimberg   CeedCallHip(ceed, hipFree(data->d_curl));
2112b730f8bSJeremy L Thompson   CeedCallBackend(CeedFree(&data));
2120d0321e0SJeremy L Thompson   return CEED_ERROR_SUCCESS;
2130d0321e0SJeremy L Thompson }
2140d0321e0SJeremy L Thompson 
2150d0321e0SJeremy L Thompson //------------------------------------------------------------------------------
2160d0321e0SJeremy L Thompson // Create tensor
2170d0321e0SJeremy L Thompson //------------------------------------------------------------------------------
2182b730f8bSJeremy L Thompson int CeedBasisCreateTensorH1_Hip(CeedInt dim, CeedInt P_1d, CeedInt Q_1d, const CeedScalar *interp_1d, const CeedScalar *grad_1d,
2196574a04fSJeremy L Thompson                                 const CeedScalar *q_ref_1d, const CeedScalar *q_weight_1d, CeedBasis basis) {
2200d0321e0SJeremy L Thompson   Ceed           ceed;
221b7453713SJeremy L Thompson   char          *basis_kernel_path, *basis_kernel_source;
222b7453713SJeremy L Thompson   CeedInt        num_comp;
223b7453713SJeremy L Thompson   const CeedInt  q_bytes      = Q_1d * sizeof(CeedScalar);
224b7453713SJeremy L Thompson   const CeedInt  interp_bytes = q_bytes * P_1d;
2250d0321e0SJeremy L Thompson   CeedBasis_Hip *data;
226b7453713SJeremy L Thompson 
227b7453713SJeremy L Thompson   CeedCallBackend(CeedBasisGetCeed(basis, &ceed));
2282b730f8bSJeremy L Thompson   CeedCallBackend(CeedCalloc(1, &data));
2290d0321e0SJeremy L Thompson 
2300d0321e0SJeremy L Thompson   // Copy data to GPU
2312b730f8bSJeremy L Thompson   CeedCallHip(ceed, hipMalloc((void **)&data->d_q_weight_1d, q_bytes));
2322b730f8bSJeremy L Thompson   CeedCallHip(ceed, hipMemcpy(data->d_q_weight_1d, q_weight_1d, q_bytes, hipMemcpyHostToDevice));
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;
267b7453713SJeremy L Thompson   char                   *basis_kernel_path, *basis_kernel_source;
268d075f50bSSebastian Grimberg   CeedInt                 num_comp, q_comp_interp, q_comp_grad;
269b7453713SJeremy L Thompson   const CeedInt           q_bytes = num_qpts * sizeof(CeedScalar);
2700d0321e0SJeremy L Thompson   CeedBasisNonTensor_Hip *data;
271b7453713SJeremy L Thompson 
272b7453713SJeremy L Thompson   CeedCallBackend(CeedBasisGetCeed(basis, &ceed));
2732b730f8bSJeremy L Thompson   CeedCallBackend(CeedCalloc(1, &data));
2740d0321e0SJeremy L Thompson 
2750d0321e0SJeremy L Thompson   // Copy basis data to GPU
276d075f50bSSebastian Grimberg   CeedCallBackend(CeedBasisGetNumQuadratureComponents(basis, CEED_EVAL_INTERP, &q_comp_interp));
277d075f50bSSebastian Grimberg   CeedCallBackend(CeedBasisGetNumQuadratureComponents(basis, CEED_EVAL_GRAD, &q_comp_grad));
2782b730f8bSJeremy L Thompson   CeedCallHip(ceed, hipMalloc((void **)&data->d_q_weight, q_bytes));
2792b730f8bSJeremy L Thompson   CeedCallHip(ceed, hipMemcpy(data->d_q_weight, q_weight, q_bytes, hipMemcpyHostToDevice));
280d075f50bSSebastian Grimberg   if (interp) {
281d075f50bSSebastian Grimberg     const CeedInt interp_bytes = q_bytes * num_nodes * q_comp_interp;
282d075f50bSSebastian Grimberg 
2832b730f8bSJeremy L Thompson     CeedCallHip(ceed, hipMalloc((void **)&data->d_interp, interp_bytes));
2842b730f8bSJeremy L Thompson     CeedCallHip(ceed, hipMemcpy(data->d_interp, interp, interp_bytes, hipMemcpyHostToDevice));
285d075f50bSSebastian Grimberg   }
286d075f50bSSebastian Grimberg   if (grad) {
287d075f50bSSebastian Grimberg     const CeedInt grad_bytes = q_bytes * num_nodes * q_comp_grad;
288d075f50bSSebastian Grimberg 
2892b730f8bSJeremy L Thompson     CeedCallHip(ceed, hipMalloc((void **)&data->d_grad, grad_bytes));
2902b730f8bSJeremy L Thompson     CeedCallHip(ceed, hipMemcpy(data->d_grad, grad, grad_bytes, hipMemcpyHostToDevice));
291d075f50bSSebastian Grimberg   }
2920d0321e0SJeremy L Thompson 
2930d0321e0SJeremy L Thompson   // Compile basis kernels
294b7453713SJeremy L Thompson   CeedCallBackend(CeedBasisGetNumComponents(basis, &num_comp));
2952b730f8bSJeremy L Thompson   CeedCallBackend(CeedGetJitAbsolutePath(ceed, "ceed/jit-source/hip/hip-ref-basis-nontensor.h", &basis_kernel_path));
29623d4529eSJeremy L Thompson   CeedDebug256(ceed, CEED_DEBUG_COLOR_SUCCESS, "----- Loading Basis Kernel Source -----\n");
2972b730f8bSJeremy L Thompson   CeedCallBackend(CeedLoadSourceToBuffer(ceed, basis_kernel_path, &basis_kernel_source));
29823d4529eSJeremy L Thompson   CeedDebug256(ceed, CEED_DEBUG_COLOR_SUCCESS, "----- Loading Basis Kernel Source Complete! -----\n");
299d075f50bSSebastian Grimberg   CeedCallBackend(CeedCompile_Hip(ceed, basis_kernel_source, &data->module, 5, "BASIS_Q", num_qpts, "BASIS_P", num_nodes, "BASIS_Q_COMP_INTERP",
300d075f50bSSebastian Grimberg                                   q_comp_interp, "BASIS_Q_COMP_DERIV", q_comp_grad, "BASIS_NUM_COMP", num_comp));
301eb7e6cafSJeremy L Thompson   CeedCallBackend(CeedGetKernel_Hip(ceed, data->module, "Interp", &data->Interp));
302d075f50bSSebastian Grimberg   CeedCallBackend(CeedGetKernel_Hip(ceed, data->module, "InterpTranspose", &data->InterpTranspose));
303d075f50bSSebastian Grimberg   CeedCallBackend(CeedGetKernel_Hip(ceed, data->module, "Deriv", &data->Deriv));
304d075f50bSSebastian Grimberg   CeedCallBackend(CeedGetKernel_Hip(ceed, data->module, "DerivTranspose", &data->DerivTranspose));
305eb7e6cafSJeremy L Thompson   CeedCallBackend(CeedGetKernel_Hip(ceed, data->module, "Weight", &data->Weight));
3062b730f8bSJeremy L Thompson   CeedCallBackend(CeedFree(&basis_kernel_path));
3072b730f8bSJeremy L Thompson   CeedCallBackend(CeedFree(&basis_kernel_source));
308d075f50bSSebastian Grimberg 
309d075f50bSSebastian Grimberg   CeedCallBackend(CeedBasisSetData(basis, data));
310d075f50bSSebastian Grimberg 
311d075f50bSSebastian Grimberg   // Register backend functions
312d075f50bSSebastian Grimberg   CeedCallBackend(CeedSetBackendFunction(ceed, "Basis", basis, "Apply", CeedBasisApplyNonTensor_Hip));
313d075f50bSSebastian Grimberg   CeedCallBackend(CeedSetBackendFunction(ceed, "Basis", basis, "Destroy", CeedBasisDestroyNonTensor_Hip));
314d075f50bSSebastian Grimberg   return CEED_ERROR_SUCCESS;
315d075f50bSSebastian Grimberg }
316d075f50bSSebastian Grimberg 
317d075f50bSSebastian Grimberg //------------------------------------------------------------------------------
318d075f50bSSebastian Grimberg // Create non-tensor H(div)
319d075f50bSSebastian Grimberg //------------------------------------------------------------------------------
320d075f50bSSebastian Grimberg int CeedBasisCreateHdiv_Hip(CeedElemTopology topo, CeedInt dim, CeedInt num_nodes, CeedInt num_qpts, const CeedScalar *interp, const CeedScalar *div,
321d075f50bSSebastian Grimberg                             const CeedScalar *q_ref, const CeedScalar *q_weight, CeedBasis basis) {
322d075f50bSSebastian Grimberg   Ceed                    ceed;
323d075f50bSSebastian Grimberg   char                   *basis_kernel_path, *basis_kernel_source;
324d075f50bSSebastian Grimberg   CeedInt                 num_comp, q_comp_interp, q_comp_div;
325d075f50bSSebastian Grimberg   const CeedInt           q_bytes = num_qpts * sizeof(CeedScalar);
326d075f50bSSebastian Grimberg   CeedBasisNonTensor_Hip *data;
327d075f50bSSebastian Grimberg 
328d075f50bSSebastian Grimberg   CeedCallBackend(CeedBasisGetCeed(basis, &ceed));
329d075f50bSSebastian Grimberg   CeedCallBackend(CeedCalloc(1, &data));
330d075f50bSSebastian Grimberg 
331d075f50bSSebastian Grimberg   // Copy basis data to GPU
332d075f50bSSebastian Grimberg   CeedCallBackend(CeedBasisGetNumQuadratureComponents(basis, CEED_EVAL_INTERP, &q_comp_interp));
333d075f50bSSebastian Grimberg   CeedCallBackend(CeedBasisGetNumQuadratureComponents(basis, CEED_EVAL_DIV, &q_comp_div));
334d075f50bSSebastian Grimberg   CeedCallHip(ceed, hipMalloc((void **)&data->d_q_weight, q_bytes));
335d075f50bSSebastian Grimberg   CeedCallHip(ceed, hipMemcpy(data->d_q_weight, q_weight, q_bytes, hipMemcpyHostToDevice));
336d075f50bSSebastian Grimberg   if (interp) {
337d075f50bSSebastian Grimberg     const CeedInt interp_bytes = q_bytes * num_nodes * q_comp_interp;
338d075f50bSSebastian Grimberg 
339d075f50bSSebastian Grimberg     CeedCallHip(ceed, hipMalloc((void **)&data->d_interp, interp_bytes));
340d075f50bSSebastian Grimberg     CeedCallHip(ceed, hipMemcpy(data->d_interp, interp, interp_bytes, hipMemcpyHostToDevice));
341d075f50bSSebastian Grimberg   }
342d075f50bSSebastian Grimberg   if (div) {
343d075f50bSSebastian Grimberg     const CeedInt div_bytes = q_bytes * num_nodes * q_comp_div;
344d075f50bSSebastian Grimberg 
345d075f50bSSebastian Grimberg     CeedCallHip(ceed, hipMalloc((void **)&data->d_div, div_bytes));
346d075f50bSSebastian Grimberg     CeedCallHip(ceed, hipMemcpy(data->d_div, div, div_bytes, hipMemcpyHostToDevice));
347d075f50bSSebastian Grimberg   }
348d075f50bSSebastian Grimberg 
349d075f50bSSebastian Grimberg   // Compile basis kernels
350d075f50bSSebastian Grimberg   CeedCallBackend(CeedBasisGetNumComponents(basis, &num_comp));
351d075f50bSSebastian Grimberg   CeedCallBackend(CeedGetJitAbsolutePath(ceed, "ceed/jit-source/hip/hip-ref-basis-nontensor.h", &basis_kernel_path));
352d075f50bSSebastian Grimberg   CeedDebug256(ceed, CEED_DEBUG_COLOR_SUCCESS, "----- Loading Basis Kernel Source -----\n");
353d075f50bSSebastian Grimberg   CeedCallBackend(CeedLoadSourceToBuffer(ceed, basis_kernel_path, &basis_kernel_source));
354d075f50bSSebastian Grimberg   CeedDebug256(ceed, CEED_DEBUG_COLOR_SUCCESS, "----- Loading Basis Kernel Source Complete! -----\n");
355d075f50bSSebastian Grimberg   CeedCallBackend(CeedCompile_Hip(ceed, basis_kernel_source, &data->module, 5, "BASIS_Q", num_qpts, "BASIS_P", num_nodes, "BASIS_Q_COMP_INTERP",
356d075f50bSSebastian Grimberg                                   q_comp_interp, "BASIS_Q_COMP_DERIV", q_comp_div, "BASIS_NUM_COMP", num_comp));
357d075f50bSSebastian Grimberg   CeedCallBackend(CeedGetKernel_Hip(ceed, data->module, "Interp", &data->Interp));
358d075f50bSSebastian Grimberg   CeedCallBackend(CeedGetKernel_Hip(ceed, data->module, "InterpTranspose", &data->InterpTranspose));
359d075f50bSSebastian Grimberg   CeedCallBackend(CeedGetKernel_Hip(ceed, data->module, "Deriv", &data->Deriv));
360d075f50bSSebastian Grimberg   CeedCallBackend(CeedGetKernel_Hip(ceed, data->module, "DerivTranspose", &data->DerivTranspose));
361d075f50bSSebastian Grimberg   CeedCallBackend(CeedGetKernel_Hip(ceed, data->module, "Weight", &data->Weight));
362d075f50bSSebastian Grimberg   CeedCallBackend(CeedFree(&basis_kernel_path));
363d075f50bSSebastian Grimberg   CeedCallBackend(CeedFree(&basis_kernel_source));
364d075f50bSSebastian Grimberg 
365d075f50bSSebastian Grimberg   CeedCallBackend(CeedBasisSetData(basis, data));
366d075f50bSSebastian Grimberg 
367d075f50bSSebastian Grimberg   // Register backend functions
368d075f50bSSebastian Grimberg   CeedCallBackend(CeedSetBackendFunction(ceed, "Basis", basis, "Apply", CeedBasisApplyNonTensor_Hip));
369d075f50bSSebastian Grimberg   CeedCallBackend(CeedSetBackendFunction(ceed, "Basis", basis, "Destroy", CeedBasisDestroyNonTensor_Hip));
370d075f50bSSebastian Grimberg   return CEED_ERROR_SUCCESS;
371d075f50bSSebastian Grimberg }
372d075f50bSSebastian Grimberg 
373d075f50bSSebastian Grimberg //------------------------------------------------------------------------------
374d075f50bSSebastian Grimberg // Create non-tensor H(curl)
375d075f50bSSebastian Grimberg //------------------------------------------------------------------------------
376d075f50bSSebastian Grimberg int CeedBasisCreateHcurl_Hip(CeedElemTopology topo, CeedInt dim, CeedInt num_nodes, CeedInt num_qpts, const CeedScalar *interp,
377d075f50bSSebastian Grimberg                              const CeedScalar *curl, const CeedScalar *q_ref, const CeedScalar *q_weight, CeedBasis basis) {
378d075f50bSSebastian Grimberg   Ceed                    ceed;
379d075f50bSSebastian Grimberg   char                   *basis_kernel_path, *basis_kernel_source;
380d075f50bSSebastian Grimberg   CeedInt                 num_comp, q_comp_interp, q_comp_curl;
381d075f50bSSebastian Grimberg   const CeedInt           q_bytes = num_qpts * sizeof(CeedScalar);
382d075f50bSSebastian Grimberg   CeedBasisNonTensor_Hip *data;
383d075f50bSSebastian Grimberg 
384d075f50bSSebastian Grimberg   CeedCallBackend(CeedBasisGetCeed(basis, &ceed));
385d075f50bSSebastian Grimberg   CeedCallBackend(CeedCalloc(1, &data));
386d075f50bSSebastian Grimberg 
387d075f50bSSebastian Grimberg   // Copy basis data to GPU
388d075f50bSSebastian Grimberg   CeedCallBackend(CeedBasisGetNumQuadratureComponents(basis, CEED_EVAL_INTERP, &q_comp_interp));
389d075f50bSSebastian Grimberg   CeedCallBackend(CeedBasisGetNumQuadratureComponents(basis, CEED_EVAL_CURL, &q_comp_curl));
390d075f50bSSebastian Grimberg   CeedCallHip(ceed, hipMalloc((void **)&data->d_q_weight, q_bytes));
391d075f50bSSebastian Grimberg   CeedCallHip(ceed, hipMemcpy(data->d_q_weight, q_weight, q_bytes, hipMemcpyHostToDevice));
392d075f50bSSebastian Grimberg   if (interp) {
393d075f50bSSebastian Grimberg     const CeedInt interp_bytes = q_bytes * num_nodes * q_comp_interp;
394d075f50bSSebastian Grimberg 
395d075f50bSSebastian Grimberg     CeedCallHip(ceed, hipMalloc((void **)&data->d_interp, interp_bytes));
396d075f50bSSebastian Grimberg     CeedCallHip(ceed, hipMemcpy(data->d_interp, interp, interp_bytes, hipMemcpyHostToDevice));
397d075f50bSSebastian Grimberg   }
398d075f50bSSebastian Grimberg   if (curl) {
399d075f50bSSebastian Grimberg     const CeedInt curl_bytes = q_bytes * num_nodes * q_comp_curl;
400d075f50bSSebastian Grimberg 
401d075f50bSSebastian Grimberg     CeedCallHip(ceed, hipMalloc((void **)&data->d_curl, curl_bytes));
402d075f50bSSebastian Grimberg     CeedCallHip(ceed, hipMemcpy(data->d_curl, curl, curl_bytes, hipMemcpyHostToDevice));
403d075f50bSSebastian Grimberg   }
404d075f50bSSebastian Grimberg 
405d075f50bSSebastian Grimberg   // Compile basis kernels
406d075f50bSSebastian Grimberg   CeedCallBackend(CeedBasisGetNumComponents(basis, &num_comp));
407d075f50bSSebastian Grimberg   CeedCallBackend(CeedGetJitAbsolutePath(ceed, "ceed/jit-source/hip/hip-ref-basis-nontensor.h", &basis_kernel_path));
408d075f50bSSebastian Grimberg   CeedDebug256(ceed, CEED_DEBUG_COLOR_SUCCESS, "----- Loading Basis Kernel Source -----\n");
409d075f50bSSebastian Grimberg   CeedCallBackend(CeedLoadSourceToBuffer(ceed, basis_kernel_path, &basis_kernel_source));
410d075f50bSSebastian Grimberg   CeedDebug256(ceed, CEED_DEBUG_COLOR_SUCCESS, "----- Loading Basis Kernel Source Complete! -----\n");
411d075f50bSSebastian Grimberg   CeedCallBackend(CeedCompile_Hip(ceed, basis_kernel_source, &data->module, 5, "BASIS_Q", num_qpts, "BASIS_P", num_nodes, "BASIS_Q_COMP_INTERP",
412d075f50bSSebastian Grimberg                                   q_comp_interp, "BASIS_Q_COMP_DERIV", q_comp_curl, "BASIS_NUM_COMP", num_comp));
413d075f50bSSebastian Grimberg   CeedCallBackend(CeedGetKernel_Hip(ceed, data->module, "Interp", &data->Interp));
414d075f50bSSebastian Grimberg   CeedCallBackend(CeedGetKernel_Hip(ceed, data->module, "InterpTranspose", &data->InterpTranspose));
415d075f50bSSebastian Grimberg   CeedCallBackend(CeedGetKernel_Hip(ceed, data->module, "Deriv", &data->Deriv));
416d075f50bSSebastian Grimberg   CeedCallBackend(CeedGetKernel_Hip(ceed, data->module, "DerivTranspose", &data->DerivTranspose));
417d075f50bSSebastian Grimberg   CeedCallBackend(CeedGetKernel_Hip(ceed, data->module, "Weight", &data->Weight));
418d075f50bSSebastian Grimberg   CeedCallBackend(CeedFree(&basis_kernel_path));
419d075f50bSSebastian Grimberg   CeedCallBackend(CeedFree(&basis_kernel_source));
420d075f50bSSebastian Grimberg 
4212b730f8bSJeremy L Thompson   CeedCallBackend(CeedBasisSetData(basis, data));
4220d0321e0SJeremy L Thompson 
4230d0321e0SJeremy L Thompson   // Register backend functions
4242b730f8bSJeremy L Thompson   CeedCallBackend(CeedSetBackendFunction(ceed, "Basis", basis, "Apply", CeedBasisApplyNonTensor_Hip));
4252b730f8bSJeremy L Thompson   CeedCallBackend(CeedSetBackendFunction(ceed, "Basis", basis, "Destroy", CeedBasisDestroyNonTensor_Hip));
4260d0321e0SJeremy L Thompson   return CEED_ERROR_SUCCESS;
4270d0321e0SJeremy L Thompson }
4282a86cc9dSSebastian Grimberg 
4290d0321e0SJeremy L Thompson //------------------------------------------------------------------------------
430