xref: /libCEED/rust/libceed-sys/c-src/backends/hip-ref/ceed-hip-ref-basis.c (revision d075f50ba6d3b1e38c233860adb1de6c814f0afc)
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
38*d075f50bSSebastian 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     // Evaluate the divergence to/from the quadrature points
700d0321e0SJeremy L Thompson     case CEED_EVAL_DIV:
710d0321e0SJeremy L Thompson       return CeedError(ceed, CEED_ERROR_BACKEND, "CEED_EVAL_DIV not supported");
720d0321e0SJeremy L Thompson     // Evaluate the curl to/from the quadrature points
730d0321e0SJeremy L Thompson     case CEED_EVAL_CURL:
740d0321e0SJeremy L Thompson       return CeedError(ceed, CEED_ERROR_BACKEND, "CEED_EVAL_CURL not supported");
750d0321e0SJeremy L Thompson     // Take no action, BasisApply should not have been called
760d0321e0SJeremy L Thompson     case CEED_EVAL_NONE:
772b730f8bSJeremy L Thompson       return CeedError(ceed, CEED_ERROR_BACKEND, "CEED_EVAL_NONE does not make sense in this context");
780d0321e0SJeremy L Thompson       // LCOV_EXCL_STOP
790d0321e0SJeremy L Thompson   }
800d0321e0SJeremy L Thompson 
810d0321e0SJeremy L Thompson   // Restore vectors
82437930d1SJeremy L Thompson   if (eval_mode != CEED_EVAL_WEIGHT) {
832b730f8bSJeremy L Thompson     CeedCallBackend(CeedVectorRestoreArrayRead(u, &d_u));
840d0321e0SJeremy L Thompson   }
852b730f8bSJeremy L Thompson   CeedCallBackend(CeedVectorRestoreArray(v, &d_v));
860d0321e0SJeremy L Thompson   return CEED_ERROR_SUCCESS;
870d0321e0SJeremy L Thompson }
880d0321e0SJeremy L Thompson 
890d0321e0SJeremy L Thompson //------------------------------------------------------------------------------
900d0321e0SJeremy L Thompson // Basis apply - non-tensor
910d0321e0SJeremy L Thompson //------------------------------------------------------------------------------
922b730f8bSJeremy L Thompson int CeedBasisApplyNonTensor_Hip(CeedBasis basis, const CeedInt num_elem, CeedTransposeMode t_mode, CeedEvalMode eval_mode, CeedVector u,
932b730f8bSJeremy L Thompson                                 CeedVector v) {
940d0321e0SJeremy L Thompson   Ceed                    ceed;
95437930d1SJeremy L Thompson   CeedInt                 num_nodes, num_qpts;
96437930d1SJeremy L Thompson   const CeedInt           transpose       = t_mode == CEED_TRANSPOSE;
97*d075f50bSSebastian Grimberg   const int               elems_per_block = 1;
98*d075f50bSSebastian Grimberg   const int               grid            = CeedDivUpInt(num_elem, elems_per_block);
990d0321e0SJeremy L Thompson   const CeedScalar       *d_u;
1000d0321e0SJeremy L Thompson   CeedScalar             *d_v;
101b7453713SJeremy L Thompson   CeedBasisNonTensor_Hip *data;
102b7453713SJeremy L Thompson 
103b7453713SJeremy L Thompson   CeedCallBackend(CeedBasisGetCeed(basis, &ceed));
104b7453713SJeremy L Thompson   CeedCallBackend(CeedBasisGetData(basis, &data));
105b7453713SJeremy L Thompson   CeedCallBackend(CeedBasisGetNumQuadraturePoints(basis, &num_qpts));
106b7453713SJeremy L Thompson   CeedCallBackend(CeedBasisGetNumNodes(basis, &num_nodes));
107b7453713SJeremy L Thompson 
108b7453713SJeremy L Thompson   // Read vectors
109437930d1SJeremy L Thompson   if (eval_mode != CEED_EVAL_WEIGHT) {
1102b730f8bSJeremy L Thompson     CeedCallBackend(CeedVectorGetArrayRead(u, CEED_MEM_DEVICE, &d_u));
1110d0321e0SJeremy L Thompson   }
1122b730f8bSJeremy L Thompson   CeedCallBackend(CeedVectorGetArrayWrite(v, CEED_MEM_DEVICE, &d_v));
1130d0321e0SJeremy L Thompson 
1140d0321e0SJeremy L Thompson   // Clear v for transpose operation
115*d075f50bSSebastian Grimberg   if (transpose) {
1161f9221feSJeremy L Thompson     CeedSize length;
117b7453713SJeremy L Thompson 
1182b730f8bSJeremy L Thompson     CeedCallBackend(CeedVectorGetLength(v, &length));
1192b730f8bSJeremy L Thompson     CeedCallHip(ceed, hipMemset(d_v, 0, length * sizeof(CeedScalar)));
1200d0321e0SJeremy L Thompson   }
1210d0321e0SJeremy L Thompson 
1220d0321e0SJeremy L Thompson   // Apply basis operation
123437930d1SJeremy L Thompson   switch (eval_mode) {
1240d0321e0SJeremy L Thompson     case CEED_EVAL_INTERP: {
125*d075f50bSSebastian Grimberg       void     *interp_args[] = {(void *)&num_elem, &data->d_interp, &d_u, &d_v};
1262b730f8bSJeremy L Thompson       const int block_size_x  = transpose ? num_nodes : num_qpts;
127b2165e7aSSebastian Grimberg 
128*d075f50bSSebastian Grimberg       if (transpose) {
129*d075f50bSSebastian Grimberg         CeedCallBackend(CeedRunKernelDim_Hip(ceed, data->InterpTranspose, grid, block_size_x, 1, elems_per_block, interp_args));
130*d075f50bSSebastian Grimberg       } else {
131b2165e7aSSebastian Grimberg         CeedCallBackend(CeedRunKernelDim_Hip(ceed, data->Interp, grid, block_size_x, 1, elems_per_block, interp_args));
132*d075f50bSSebastian Grimberg       }
1330d0321e0SJeremy L Thompson     } break;
1340d0321e0SJeremy L Thompson     case CEED_EVAL_GRAD: {
135*d075f50bSSebastian Grimberg       void     *grad_args[]  = {(void *)&num_elem, &data->d_grad, &d_u, &d_v};
1362b730f8bSJeremy L Thompson       const int block_size_x = transpose ? num_nodes : num_qpts;
137b2165e7aSSebastian Grimberg 
138*d075f50bSSebastian Grimberg       if (transpose) {
139*d075f50bSSebastian Grimberg         CeedCallBackend(CeedRunKernelDim_Hip(ceed, data->DerivTranspose, grid, block_size_x, 1, elems_per_block, grad_args));
140*d075f50bSSebastian Grimberg       } else {
141*d075f50bSSebastian Grimberg         CeedCallBackend(CeedRunKernelDim_Hip(ceed, data->Deriv, grid, block_size_x, 1, elems_per_block, grad_args));
142*d075f50bSSebastian Grimberg       }
143*d075f50bSSebastian Grimberg     } break;
144*d075f50bSSebastian Grimberg     case CEED_EVAL_DIV: {
145*d075f50bSSebastian Grimberg       void     *div_args[]   = {(void *)&num_elem, &data->d_div, &d_u, &d_v};
146*d075f50bSSebastian Grimberg       const int block_size_x = transpose ? num_nodes : num_qpts;
147*d075f50bSSebastian Grimberg 
148*d075f50bSSebastian Grimberg       if (transpose) {
149*d075f50bSSebastian Grimberg         CeedCallBackend(CeedRunKernelDim_Hip(ceed, data->DerivTranspose, grid, block_size_x, 1, elems_per_block, div_args));
150*d075f50bSSebastian Grimberg       } else {
151*d075f50bSSebastian Grimberg         CeedCallBackend(CeedRunKernelDim_Hip(ceed, data->Deriv, grid, block_size_x, 1, elems_per_block, div_args));
152*d075f50bSSebastian Grimberg       }
153*d075f50bSSebastian Grimberg     } break;
154*d075f50bSSebastian Grimberg     case CEED_EVAL_CURL: {
155*d075f50bSSebastian Grimberg       void     *curl_args[]  = {(void *)&num_elem, &data->d_curl, &d_u, &d_v};
156*d075f50bSSebastian Grimberg       const int block_size_x = transpose ? num_nodes : num_qpts;
157*d075f50bSSebastian Grimberg 
158*d075f50bSSebastian Grimberg       if (transpose) {
159*d075f50bSSebastian Grimberg         CeedCallBackend(CeedRunKernelDim_Hip(ceed, data->DerivTranspose, grid, block_size_x, 1, elems_per_block, curl_args));
160*d075f50bSSebastian Grimberg       } else {
161*d075f50bSSebastian Grimberg         CeedCallBackend(CeedRunKernelDim_Hip(ceed, data->Deriv, grid, block_size_x, 1, elems_per_block, curl_args));
162*d075f50bSSebastian Grimberg       }
1630d0321e0SJeremy L Thompson     } break;
1640d0321e0SJeremy L Thompson     case CEED_EVAL_WEIGHT: {
165437930d1SJeremy L Thompson       void *weight_args[] = {(void *)&num_elem, (void *)&data->d_q_weight, &d_v};
166b2165e7aSSebastian Grimberg 
167b2165e7aSSebastian Grimberg       CeedCallBackend(CeedRunKernelDim_Hip(ceed, data->Weight, grid, num_qpts, 1, elems_per_block, weight_args));
1680d0321e0SJeremy L Thompson     } break;
1690d0321e0SJeremy L Thompson     // LCOV_EXCL_START
1700d0321e0SJeremy L Thompson     // Take no action, BasisApply should not have been called
1710d0321e0SJeremy L Thompson     case CEED_EVAL_NONE:
1722b730f8bSJeremy L Thompson       return CeedError(ceed, CEED_ERROR_BACKEND, "CEED_EVAL_NONE does not make sense in this context");
1730d0321e0SJeremy L Thompson       // LCOV_EXCL_STOP
1740d0321e0SJeremy L Thompson   }
1750d0321e0SJeremy L Thompson 
1760d0321e0SJeremy L Thompson   // Restore vectors
177437930d1SJeremy L Thompson   if (eval_mode != CEED_EVAL_WEIGHT) {
1782b730f8bSJeremy L Thompson     CeedCallBackend(CeedVectorRestoreArrayRead(u, &d_u));
1790d0321e0SJeremy L Thompson   }
1802b730f8bSJeremy L Thompson   CeedCallBackend(CeedVectorRestoreArray(v, &d_v));
1810d0321e0SJeremy L Thompson   return CEED_ERROR_SUCCESS;
1820d0321e0SJeremy L Thompson }
1830d0321e0SJeremy L Thompson 
1840d0321e0SJeremy L Thompson //------------------------------------------------------------------------------
1850d0321e0SJeremy L Thompson // Destroy tensor basis
1860d0321e0SJeremy L Thompson //------------------------------------------------------------------------------
1870d0321e0SJeremy L Thompson static int CeedBasisDestroy_Hip(CeedBasis basis) {
1880d0321e0SJeremy L Thompson   Ceed           ceed;
1890d0321e0SJeremy L Thompson   CeedBasis_Hip *data;
190b7453713SJeremy L Thompson 
191b7453713SJeremy L Thompson   CeedCallBackend(CeedBasisGetCeed(basis, &ceed));
1922b730f8bSJeremy L Thompson   CeedCallBackend(CeedBasisGetData(basis, &data));
1932b730f8bSJeremy L Thompson   CeedCallHip(ceed, hipModuleUnload(data->module));
1942b730f8bSJeremy L Thompson   CeedCallHip(ceed, hipFree(data->d_q_weight_1d));
1952b730f8bSJeremy L Thompson   CeedCallHip(ceed, hipFree(data->d_interp_1d));
1962b730f8bSJeremy L Thompson   CeedCallHip(ceed, hipFree(data->d_grad_1d));
1972b730f8bSJeremy L Thompson   CeedCallBackend(CeedFree(&data));
1980d0321e0SJeremy L Thompson   return CEED_ERROR_SUCCESS;
1990d0321e0SJeremy L Thompson }
2000d0321e0SJeremy L Thompson 
2010d0321e0SJeremy L Thompson //------------------------------------------------------------------------------
2020d0321e0SJeremy L Thompson // Destroy non-tensor basis
2030d0321e0SJeremy L Thompson //------------------------------------------------------------------------------
2040d0321e0SJeremy L Thompson static int CeedBasisDestroyNonTensor_Hip(CeedBasis basis) {
2050d0321e0SJeremy L Thompson   Ceed                    ceed;
2060d0321e0SJeremy L Thompson   CeedBasisNonTensor_Hip *data;
207b7453713SJeremy L Thompson 
208b7453713SJeremy L Thompson   CeedCallBackend(CeedBasisGetCeed(basis, &ceed));
2092b730f8bSJeremy L Thompson   CeedCallBackend(CeedBasisGetData(basis, &data));
2102b730f8bSJeremy L Thompson   CeedCallHip(ceed, hipModuleUnload(data->module));
2112b730f8bSJeremy L Thompson   CeedCallHip(ceed, hipFree(data->d_q_weight));
2122b730f8bSJeremy L Thompson   CeedCallHip(ceed, hipFree(data->d_interp));
2132b730f8bSJeremy L Thompson   CeedCallHip(ceed, hipFree(data->d_grad));
214*d075f50bSSebastian Grimberg   CeedCallHip(ceed, hipFree(data->d_div));
215*d075f50bSSebastian Grimberg   CeedCallHip(ceed, hipFree(data->d_curl));
2162b730f8bSJeremy L Thompson   CeedCallBackend(CeedFree(&data));
2170d0321e0SJeremy L Thompson   return CEED_ERROR_SUCCESS;
2180d0321e0SJeremy L Thompson }
2190d0321e0SJeremy L Thompson 
2200d0321e0SJeremy L Thompson //------------------------------------------------------------------------------
2210d0321e0SJeremy L Thompson // Create tensor
2220d0321e0SJeremy L Thompson //------------------------------------------------------------------------------
2232b730f8bSJeremy L Thompson int CeedBasisCreateTensorH1_Hip(CeedInt dim, CeedInt P_1d, CeedInt Q_1d, const CeedScalar *interp_1d, const CeedScalar *grad_1d,
2246574a04fSJeremy L Thompson                                 const CeedScalar *q_ref_1d, const CeedScalar *q_weight_1d, CeedBasis basis) {
2250d0321e0SJeremy L Thompson   Ceed           ceed;
226b7453713SJeremy L Thompson   char          *basis_kernel_path, *basis_kernel_source;
227b7453713SJeremy L Thompson   CeedInt        num_comp;
228b7453713SJeremy L Thompson   const CeedInt  q_bytes      = Q_1d * sizeof(CeedScalar);
229b7453713SJeremy L Thompson   const CeedInt  interp_bytes = q_bytes * P_1d;
2300d0321e0SJeremy L Thompson   CeedBasis_Hip *data;
231b7453713SJeremy L Thompson 
232b7453713SJeremy L Thompson   CeedCallBackend(CeedBasisGetCeed(basis, &ceed));
2332b730f8bSJeremy L Thompson   CeedCallBackend(CeedCalloc(1, &data));
2340d0321e0SJeremy L Thompson 
2350d0321e0SJeremy L Thompson   // Copy data to GPU
2362b730f8bSJeremy L Thompson   CeedCallHip(ceed, hipMalloc((void **)&data->d_q_weight_1d, q_bytes));
2372b730f8bSJeremy L Thompson   CeedCallHip(ceed, hipMemcpy(data->d_q_weight_1d, q_weight_1d, q_bytes, hipMemcpyHostToDevice));
2382b730f8bSJeremy L Thompson   CeedCallHip(ceed, hipMalloc((void **)&data->d_interp_1d, interp_bytes));
2392b730f8bSJeremy L Thompson   CeedCallHip(ceed, hipMemcpy(data->d_interp_1d, interp_1d, interp_bytes, hipMemcpyHostToDevice));
2402b730f8bSJeremy L Thompson   CeedCallHip(ceed, hipMalloc((void **)&data->d_grad_1d, interp_bytes));
2412b730f8bSJeremy L Thompson   CeedCallHip(ceed, hipMemcpy(data->d_grad_1d, grad_1d, interp_bytes, hipMemcpyHostToDevice));
2420d0321e0SJeremy L Thompson 
243ecc88aebSJeremy L Thompson   // Compile basis kernels
244b7453713SJeremy L Thompson   CeedCallBackend(CeedBasisGetNumComponents(basis, &num_comp));
2452b730f8bSJeremy L Thompson   CeedCallBackend(CeedGetJitAbsolutePath(ceed, "ceed/jit-source/hip/hip-ref-basis-tensor.h", &basis_kernel_path));
24623d4529eSJeremy L Thompson   CeedDebug256(ceed, CEED_DEBUG_COLOR_SUCCESS, "----- Loading Basis Kernel Source -----\n");
2472b730f8bSJeremy L Thompson   CeedCallBackend(CeedLoadSourceToBuffer(ceed, basis_kernel_path, &basis_kernel_source));
24823d4529eSJeremy L Thompson   CeedDebug256(ceed, CEED_DEBUG_COLOR_SUCCESS, "----- Loading Basis Kernel Source Complete! -----\n");
249eb7e6cafSJeremy 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",
250b7453713SJeremy L Thompson                                   num_comp * CeedIntPow(Q_1d > P_1d ? Q_1d : P_1d, dim), "BASIS_DIM", dim, "BASIS_NUM_COMP", num_comp,
251b7453713SJeremy L Thompson                                   "BASIS_NUM_NODES", CeedIntPow(P_1d, dim), "BASIS_NUM_QPTS", CeedIntPow(Q_1d, dim)));
252eb7e6cafSJeremy L Thompson   CeedCallBackend(CeedGetKernel_Hip(ceed, data->module, "Interp", &data->Interp));
253eb7e6cafSJeremy L Thompson   CeedCallBackend(CeedGetKernel_Hip(ceed, data->module, "Grad", &data->Grad));
254eb7e6cafSJeremy L Thompson   CeedCallBackend(CeedGetKernel_Hip(ceed, data->module, "Weight", &data->Weight));
2552b730f8bSJeremy L Thompson   CeedCallBackend(CeedFree(&basis_kernel_path));
2562b730f8bSJeremy L Thompson   CeedCallBackend(CeedFree(&basis_kernel_source));
257437930d1SJeremy L Thompson 
2582b730f8bSJeremy L Thompson   CeedCallBackend(CeedBasisSetData(basis, data));
2590d0321e0SJeremy L Thompson 
260*d075f50bSSebastian Grimberg   // Register backend functions
2612b730f8bSJeremy L Thompson   CeedCallBackend(CeedSetBackendFunction(ceed, "Basis", basis, "Apply", CeedBasisApply_Hip));
2622b730f8bSJeremy L Thompson   CeedCallBackend(CeedSetBackendFunction(ceed, "Basis", basis, "Destroy", CeedBasisDestroy_Hip));
2630d0321e0SJeremy L Thompson   return CEED_ERROR_SUCCESS;
2640d0321e0SJeremy L Thompson }
2650d0321e0SJeremy L Thompson 
2660d0321e0SJeremy L Thompson //------------------------------------------------------------------------------
267*d075f50bSSebastian Grimberg // Create non-tensor H^1
2680d0321e0SJeremy L Thompson //------------------------------------------------------------------------------
2692b730f8bSJeremy L Thompson int CeedBasisCreateH1_Hip(CeedElemTopology topo, CeedInt dim, CeedInt num_nodes, CeedInt num_qpts, const CeedScalar *interp, const CeedScalar *grad,
27051475c7cSJeremy L Thompson                           const CeedScalar *q_ref, const CeedScalar *q_weight, CeedBasis basis) {
2710d0321e0SJeremy L Thompson   Ceed                    ceed;
272b7453713SJeremy L Thompson   char                   *basis_kernel_path, *basis_kernel_source;
273*d075f50bSSebastian Grimberg   CeedInt                 num_comp, q_comp_interp, q_comp_grad;
274b7453713SJeremy L Thompson   const CeedInt           q_bytes = num_qpts * sizeof(CeedScalar);
2750d0321e0SJeremy L Thompson   CeedBasisNonTensor_Hip *data;
276b7453713SJeremy L Thompson 
277b7453713SJeremy L Thompson   CeedCallBackend(CeedBasisGetCeed(basis, &ceed));
2782b730f8bSJeremy L Thompson   CeedCallBackend(CeedCalloc(1, &data));
2790d0321e0SJeremy L Thompson 
2800d0321e0SJeremy L Thompson   // Copy basis data to GPU
281*d075f50bSSebastian Grimberg   CeedCallBackend(CeedBasisGetNumQuadratureComponents(basis, CEED_EVAL_INTERP, &q_comp_interp));
282*d075f50bSSebastian Grimberg   CeedCallBackend(CeedBasisGetNumQuadratureComponents(basis, CEED_EVAL_GRAD, &q_comp_grad));
2832b730f8bSJeremy L Thompson   CeedCallHip(ceed, hipMalloc((void **)&data->d_q_weight, q_bytes));
2842b730f8bSJeremy L Thompson   CeedCallHip(ceed, hipMemcpy(data->d_q_weight, q_weight, q_bytes, hipMemcpyHostToDevice));
285*d075f50bSSebastian Grimberg   if (interp) {
286*d075f50bSSebastian Grimberg     const CeedInt interp_bytes = q_bytes * num_nodes * q_comp_interp;
287*d075f50bSSebastian Grimberg 
2882b730f8bSJeremy L Thompson     CeedCallHip(ceed, hipMalloc((void **)&data->d_interp, interp_bytes));
2892b730f8bSJeremy L Thompson     CeedCallHip(ceed, hipMemcpy(data->d_interp, interp, interp_bytes, hipMemcpyHostToDevice));
290*d075f50bSSebastian Grimberg   }
291*d075f50bSSebastian Grimberg   if (grad) {
292*d075f50bSSebastian Grimberg     const CeedInt grad_bytes = q_bytes * num_nodes * q_comp_grad;
293*d075f50bSSebastian Grimberg 
2942b730f8bSJeremy L Thompson     CeedCallHip(ceed, hipMalloc((void **)&data->d_grad, grad_bytes));
2952b730f8bSJeremy L Thompson     CeedCallHip(ceed, hipMemcpy(data->d_grad, grad, grad_bytes, hipMemcpyHostToDevice));
296*d075f50bSSebastian Grimberg   }
2970d0321e0SJeremy L Thompson 
2980d0321e0SJeremy L Thompson   // Compile basis kernels
299b7453713SJeremy L Thompson   CeedCallBackend(CeedBasisGetNumComponents(basis, &num_comp));
3002b730f8bSJeremy L Thompson   CeedCallBackend(CeedGetJitAbsolutePath(ceed, "ceed/jit-source/hip/hip-ref-basis-nontensor.h", &basis_kernel_path));
30123d4529eSJeremy L Thompson   CeedDebug256(ceed, CEED_DEBUG_COLOR_SUCCESS, "----- Loading Basis Kernel Source -----\n");
3022b730f8bSJeremy L Thompson   CeedCallBackend(CeedLoadSourceToBuffer(ceed, basis_kernel_path, &basis_kernel_source));
30323d4529eSJeremy L Thompson   CeedDebug256(ceed, CEED_DEBUG_COLOR_SUCCESS, "----- Loading Basis Kernel Source Complete! -----\n");
304*d075f50bSSebastian Grimberg   CeedCallBackend(CeedCompile_Hip(ceed, basis_kernel_source, &data->module, 5, "BASIS_Q", num_qpts, "BASIS_P", num_nodes, "BASIS_Q_COMP_INTERP",
305*d075f50bSSebastian Grimberg                                   q_comp_interp, "BASIS_Q_COMP_DERIV", q_comp_grad, "BASIS_NUM_COMP", num_comp));
306eb7e6cafSJeremy L Thompson   CeedCallBackend(CeedGetKernel_Hip(ceed, data->module, "Interp", &data->Interp));
307*d075f50bSSebastian Grimberg   CeedCallBackend(CeedGetKernel_Hip(ceed, data->module, "InterpTranspose", &data->InterpTranspose));
308*d075f50bSSebastian Grimberg   CeedCallBackend(CeedGetKernel_Hip(ceed, data->module, "Deriv", &data->Deriv));
309*d075f50bSSebastian Grimberg   CeedCallBackend(CeedGetKernel_Hip(ceed, data->module, "DerivTranspose", &data->DerivTranspose));
310eb7e6cafSJeremy L Thompson   CeedCallBackend(CeedGetKernel_Hip(ceed, data->module, "Weight", &data->Weight));
3112b730f8bSJeremy L Thompson   CeedCallBackend(CeedFree(&basis_kernel_path));
3122b730f8bSJeremy L Thompson   CeedCallBackend(CeedFree(&basis_kernel_source));
313*d075f50bSSebastian Grimberg 
314*d075f50bSSebastian Grimberg   CeedCallBackend(CeedBasisSetData(basis, data));
315*d075f50bSSebastian Grimberg 
316*d075f50bSSebastian Grimberg   // Register backend functions
317*d075f50bSSebastian Grimberg   CeedCallBackend(CeedSetBackendFunction(ceed, "Basis", basis, "Apply", CeedBasisApplyNonTensor_Hip));
318*d075f50bSSebastian Grimberg   CeedCallBackend(CeedSetBackendFunction(ceed, "Basis", basis, "Destroy", CeedBasisDestroyNonTensor_Hip));
319*d075f50bSSebastian Grimberg   return CEED_ERROR_SUCCESS;
320*d075f50bSSebastian Grimberg }
321*d075f50bSSebastian Grimberg 
322*d075f50bSSebastian Grimberg //------------------------------------------------------------------------------
323*d075f50bSSebastian Grimberg // Create non-tensor H(div)
324*d075f50bSSebastian Grimberg //------------------------------------------------------------------------------
325*d075f50bSSebastian Grimberg int CeedBasisCreateHdiv_Hip(CeedElemTopology topo, CeedInt dim, CeedInt num_nodes, CeedInt num_qpts, const CeedScalar *interp, const CeedScalar *div,
326*d075f50bSSebastian Grimberg                             const CeedScalar *q_ref, const CeedScalar *q_weight, CeedBasis basis) {
327*d075f50bSSebastian Grimberg   Ceed                    ceed;
328*d075f50bSSebastian Grimberg   char                   *basis_kernel_path, *basis_kernel_source;
329*d075f50bSSebastian Grimberg   CeedInt                 num_comp, q_comp_interp, q_comp_div;
330*d075f50bSSebastian Grimberg   const CeedInt           q_bytes = num_qpts * sizeof(CeedScalar);
331*d075f50bSSebastian Grimberg   CeedBasisNonTensor_Hip *data;
332*d075f50bSSebastian Grimberg 
333*d075f50bSSebastian Grimberg   CeedCallBackend(CeedBasisGetCeed(basis, &ceed));
334*d075f50bSSebastian Grimberg   CeedCallBackend(CeedCalloc(1, &data));
335*d075f50bSSebastian Grimberg 
336*d075f50bSSebastian Grimberg   // Copy basis data to GPU
337*d075f50bSSebastian Grimberg   CeedCallBackend(CeedBasisGetNumQuadratureComponents(basis, CEED_EVAL_INTERP, &q_comp_interp));
338*d075f50bSSebastian Grimberg   CeedCallBackend(CeedBasisGetNumQuadratureComponents(basis, CEED_EVAL_DIV, &q_comp_div));
339*d075f50bSSebastian Grimberg   CeedCallHip(ceed, hipMalloc((void **)&data->d_q_weight, q_bytes));
340*d075f50bSSebastian Grimberg   CeedCallHip(ceed, hipMemcpy(data->d_q_weight, q_weight, q_bytes, hipMemcpyHostToDevice));
341*d075f50bSSebastian Grimberg   if (interp) {
342*d075f50bSSebastian Grimberg     const CeedInt interp_bytes = q_bytes * num_nodes * q_comp_interp;
343*d075f50bSSebastian Grimberg 
344*d075f50bSSebastian Grimberg     CeedCallHip(ceed, hipMalloc((void **)&data->d_interp, interp_bytes));
345*d075f50bSSebastian Grimberg     CeedCallHip(ceed, hipMemcpy(data->d_interp, interp, interp_bytes, hipMemcpyHostToDevice));
346*d075f50bSSebastian Grimberg   }
347*d075f50bSSebastian Grimberg   if (div) {
348*d075f50bSSebastian Grimberg     const CeedInt div_bytes = q_bytes * num_nodes * q_comp_div;
349*d075f50bSSebastian Grimberg 
350*d075f50bSSebastian Grimberg     CeedCallHip(ceed, hipMalloc((void **)&data->d_div, div_bytes));
351*d075f50bSSebastian Grimberg     CeedCallHip(ceed, hipMemcpy(data->d_div, div, div_bytes, hipMemcpyHostToDevice));
352*d075f50bSSebastian Grimberg   }
353*d075f50bSSebastian Grimberg 
354*d075f50bSSebastian Grimberg   // Compile basis kernels
355*d075f50bSSebastian Grimberg   CeedCallBackend(CeedBasisGetNumComponents(basis, &num_comp));
356*d075f50bSSebastian Grimberg   CeedCallBackend(CeedGetJitAbsolutePath(ceed, "ceed/jit-source/hip/hip-ref-basis-nontensor.h", &basis_kernel_path));
357*d075f50bSSebastian Grimberg   CeedDebug256(ceed, CEED_DEBUG_COLOR_SUCCESS, "----- Loading Basis Kernel Source -----\n");
358*d075f50bSSebastian Grimberg   CeedCallBackend(CeedLoadSourceToBuffer(ceed, basis_kernel_path, &basis_kernel_source));
359*d075f50bSSebastian Grimberg   CeedDebug256(ceed, CEED_DEBUG_COLOR_SUCCESS, "----- Loading Basis Kernel Source Complete! -----\n");
360*d075f50bSSebastian Grimberg   CeedCallBackend(CeedCompile_Hip(ceed, basis_kernel_source, &data->module, 5, "BASIS_Q", num_qpts, "BASIS_P", num_nodes, "BASIS_Q_COMP_INTERP",
361*d075f50bSSebastian Grimberg                                   q_comp_interp, "BASIS_Q_COMP_DERIV", q_comp_div, "BASIS_NUM_COMP", num_comp));
362*d075f50bSSebastian Grimberg   CeedCallBackend(CeedGetKernel_Hip(ceed, data->module, "Interp", &data->Interp));
363*d075f50bSSebastian Grimberg   CeedCallBackend(CeedGetKernel_Hip(ceed, data->module, "InterpTranspose", &data->InterpTranspose));
364*d075f50bSSebastian Grimberg   CeedCallBackend(CeedGetKernel_Hip(ceed, data->module, "Deriv", &data->Deriv));
365*d075f50bSSebastian Grimberg   CeedCallBackend(CeedGetKernel_Hip(ceed, data->module, "DerivTranspose", &data->DerivTranspose));
366*d075f50bSSebastian Grimberg   CeedCallBackend(CeedGetKernel_Hip(ceed, data->module, "Weight", &data->Weight));
367*d075f50bSSebastian Grimberg   CeedCallBackend(CeedFree(&basis_kernel_path));
368*d075f50bSSebastian Grimberg   CeedCallBackend(CeedFree(&basis_kernel_source));
369*d075f50bSSebastian Grimberg 
370*d075f50bSSebastian Grimberg   CeedCallBackend(CeedBasisSetData(basis, data));
371*d075f50bSSebastian Grimberg 
372*d075f50bSSebastian Grimberg   // Register backend functions
373*d075f50bSSebastian Grimberg   CeedCallBackend(CeedSetBackendFunction(ceed, "Basis", basis, "Apply", CeedBasisApplyNonTensor_Hip));
374*d075f50bSSebastian Grimberg   CeedCallBackend(CeedSetBackendFunction(ceed, "Basis", basis, "Destroy", CeedBasisDestroyNonTensor_Hip));
375*d075f50bSSebastian Grimberg   return CEED_ERROR_SUCCESS;
376*d075f50bSSebastian Grimberg }
377*d075f50bSSebastian Grimberg 
378*d075f50bSSebastian Grimberg //------------------------------------------------------------------------------
379*d075f50bSSebastian Grimberg // Create non-tensor H(curl)
380*d075f50bSSebastian Grimberg //------------------------------------------------------------------------------
381*d075f50bSSebastian Grimberg int CeedBasisCreateHcurl_Hip(CeedElemTopology topo, CeedInt dim, CeedInt num_nodes, CeedInt num_qpts, const CeedScalar *interp,
382*d075f50bSSebastian Grimberg                              const CeedScalar *curl, const CeedScalar *q_ref, const CeedScalar *q_weight, CeedBasis basis) {
383*d075f50bSSebastian Grimberg   Ceed                    ceed;
384*d075f50bSSebastian Grimberg   char                   *basis_kernel_path, *basis_kernel_source;
385*d075f50bSSebastian Grimberg   CeedInt                 num_comp, q_comp_interp, q_comp_curl;
386*d075f50bSSebastian Grimberg   const CeedInt           q_bytes = num_qpts * sizeof(CeedScalar);
387*d075f50bSSebastian Grimberg   CeedBasisNonTensor_Hip *data;
388*d075f50bSSebastian Grimberg 
389*d075f50bSSebastian Grimberg   CeedCallBackend(CeedBasisGetCeed(basis, &ceed));
390*d075f50bSSebastian Grimberg   CeedCallBackend(CeedCalloc(1, &data));
391*d075f50bSSebastian Grimberg 
392*d075f50bSSebastian Grimberg   // Copy basis data to GPU
393*d075f50bSSebastian Grimberg   CeedCallBackend(CeedBasisGetNumQuadratureComponents(basis, CEED_EVAL_INTERP, &q_comp_interp));
394*d075f50bSSebastian Grimberg   CeedCallBackend(CeedBasisGetNumQuadratureComponents(basis, CEED_EVAL_CURL, &q_comp_curl));
395*d075f50bSSebastian Grimberg   CeedCallHip(ceed, hipMalloc((void **)&data->d_q_weight, q_bytes));
396*d075f50bSSebastian Grimberg   CeedCallHip(ceed, hipMemcpy(data->d_q_weight, q_weight, q_bytes, hipMemcpyHostToDevice));
397*d075f50bSSebastian Grimberg   if (interp) {
398*d075f50bSSebastian Grimberg     const CeedInt interp_bytes = q_bytes * num_nodes * q_comp_interp;
399*d075f50bSSebastian Grimberg 
400*d075f50bSSebastian Grimberg     CeedCallHip(ceed, hipMalloc((void **)&data->d_interp, interp_bytes));
401*d075f50bSSebastian Grimberg     CeedCallHip(ceed, hipMemcpy(data->d_interp, interp, interp_bytes, hipMemcpyHostToDevice));
402*d075f50bSSebastian Grimberg   }
403*d075f50bSSebastian Grimberg   if (curl) {
404*d075f50bSSebastian Grimberg     const CeedInt curl_bytes = q_bytes * num_nodes * q_comp_curl;
405*d075f50bSSebastian Grimberg 
406*d075f50bSSebastian Grimberg     CeedCallHip(ceed, hipMalloc((void **)&data->d_curl, curl_bytes));
407*d075f50bSSebastian Grimberg     CeedCallHip(ceed, hipMemcpy(data->d_curl, curl, curl_bytes, hipMemcpyHostToDevice));
408*d075f50bSSebastian Grimberg   }
409*d075f50bSSebastian Grimberg 
410*d075f50bSSebastian Grimberg   // Compile basis kernels
411*d075f50bSSebastian Grimberg   CeedCallBackend(CeedBasisGetNumComponents(basis, &num_comp));
412*d075f50bSSebastian Grimberg   CeedCallBackend(CeedGetJitAbsolutePath(ceed, "ceed/jit-source/hip/hip-ref-basis-nontensor.h", &basis_kernel_path));
413*d075f50bSSebastian Grimberg   CeedDebug256(ceed, CEED_DEBUG_COLOR_SUCCESS, "----- Loading Basis Kernel Source -----\n");
414*d075f50bSSebastian Grimberg   CeedCallBackend(CeedLoadSourceToBuffer(ceed, basis_kernel_path, &basis_kernel_source));
415*d075f50bSSebastian Grimberg   CeedDebug256(ceed, CEED_DEBUG_COLOR_SUCCESS, "----- Loading Basis Kernel Source Complete! -----\n");
416*d075f50bSSebastian Grimberg   CeedCallBackend(CeedCompile_Hip(ceed, basis_kernel_source, &data->module, 5, "BASIS_Q", num_qpts, "BASIS_P", num_nodes, "BASIS_Q_COMP_INTERP",
417*d075f50bSSebastian Grimberg                                   q_comp_interp, "BASIS_Q_COMP_DERIV", q_comp_curl, "BASIS_NUM_COMP", num_comp));
418*d075f50bSSebastian Grimberg   CeedCallBackend(CeedGetKernel_Hip(ceed, data->module, "Interp", &data->Interp));
419*d075f50bSSebastian Grimberg   CeedCallBackend(CeedGetKernel_Hip(ceed, data->module, "InterpTranspose", &data->InterpTranspose));
420*d075f50bSSebastian Grimberg   CeedCallBackend(CeedGetKernel_Hip(ceed, data->module, "Deriv", &data->Deriv));
421*d075f50bSSebastian Grimberg   CeedCallBackend(CeedGetKernel_Hip(ceed, data->module, "DerivTranspose", &data->DerivTranspose));
422*d075f50bSSebastian Grimberg   CeedCallBackend(CeedGetKernel_Hip(ceed, data->module, "Weight", &data->Weight));
423*d075f50bSSebastian Grimberg   CeedCallBackend(CeedFree(&basis_kernel_path));
424*d075f50bSSebastian Grimberg   CeedCallBackend(CeedFree(&basis_kernel_source));
425*d075f50bSSebastian Grimberg 
4262b730f8bSJeremy L Thompson   CeedCallBackend(CeedBasisSetData(basis, data));
4270d0321e0SJeremy L Thompson 
4280d0321e0SJeremy L Thompson   // Register backend functions
4292b730f8bSJeremy L Thompson   CeedCallBackend(CeedSetBackendFunction(ceed, "Basis", basis, "Apply", CeedBasisApplyNonTensor_Hip));
4302b730f8bSJeremy L Thompson   CeedCallBackend(CeedSetBackendFunction(ceed, "Basis", basis, "Destroy", CeedBasisDestroyNonTensor_Hip));
4310d0321e0SJeremy L Thompson   return CEED_ERROR_SUCCESS;
4320d0321e0SJeremy L Thompson }
4332a86cc9dSSebastian Grimberg 
4340d0321e0SJeremy L Thompson //------------------------------------------------------------------------------
435