xref: /libCEED/backends/hip-shared/ceed-hip-shared-basis.c (revision db2becc9f302fe8eb3a32ace50ce3f3a5d42e6c4)
15aed82e4SJeremy L Thompson // Copyright (c) 2017-2024, Lawrence Livermore National Security, LLC and other CEED contributors.
23d8e8822SJeremy L Thompson // All Rights Reserved. See the top-level LICENSE and NOTICE files for details.
37d8d0e25Snbeams //
43d8e8822SJeremy L Thompson // SPDX-License-Identifier: BSD-2-Clause
57d8d0e25Snbeams //
63d8e8822SJeremy L Thompson // This file is part of CEED:  http://github.com/ceed
77d8d0e25Snbeams 
849aac155SJeremy L Thompson #include <ceed.h>
9ec3da8bcSJed Brown #include <ceed/backend.h>
10437930d1SJeremy L Thompson #include <ceed/jit-tools.h>
1149aac155SJeremy L Thompson #include <stdbool.h>
123d576824SJeremy L Thompson #include <stddef.h>
13c85e8640SSebastian Grimberg #include <hip/hip_runtime.h>
142b730f8bSJeremy L Thompson 
157fcac036SJeremy L Thompson #include "../hip/ceed-hip-common.h"
167d8d0e25Snbeams #include "../hip/ceed-hip-compile.h"
172b730f8bSJeremy L Thompson #include "ceed-hip-shared.h"
187d8d0e25Snbeams 
197d8d0e25Snbeams //------------------------------------------------------------------------------
209e31c45bSnbeams // Compute a block size based on required minimum threads
219e31c45bSnbeams //------------------------------------------------------------------------------
229e31c45bSnbeams static CeedInt ComputeBlockSizeFromRequirement(const CeedInt required) {
239e31c45bSnbeams   CeedInt maxSize     = 1024;  // Max total threads per block
249e31c45bSnbeams   CeedInt currentSize = 64;    // Start with one group
259e31c45bSnbeams 
269e31c45bSnbeams   while (currentSize < maxSize) {
272b730f8bSJeremy L Thompson     if (currentSize > required) break;
282b730f8bSJeremy L Thompson     else currentSize = currentSize * 2;
299e31c45bSnbeams   }
309e31c45bSnbeams   return currentSize;
319e31c45bSnbeams }
329e31c45bSnbeams 
339e31c45bSnbeams //------------------------------------------------------------------------------
349e31c45bSnbeams // Compute required thread block sizes for basis kernels given P, Q, dim, and
359e201c85SYohann // num_comp (num_comp not currently used, but may be again in other basis
369e201c85SYohann // parallelization options)
379e31c45bSnbeams //------------------------------------------------------------------------------
382b730f8bSJeremy L Thompson static int ComputeBasisThreadBlockSizes(const CeedInt dim, const CeedInt P_1d, const CeedInt Q_1d, const CeedInt num_comp, CeedInt *block_sizes) {
399e31c45bSnbeams   // Note that this will use the same block sizes for all dimensions when compiling,
409e31c45bSnbeams   // but as each basis object is defined for a particular dimension, we will never
419e31c45bSnbeams   // call any kernels except the ones for the dimension for which we have computed the
429e31c45bSnbeams   // block sizes.
43437930d1SJeremy L Thompson   const CeedInt thread_1d = CeedIntMax(P_1d, Q_1d);
44b7453713SJeremy L Thompson 
459e31c45bSnbeams   switch (dim) {
469e31c45bSnbeams     case 1: {
479e31c45bSnbeams       // Interp kernels:
48437930d1SJeremy L Thompson       block_sizes[0] = 256;
499e31c45bSnbeams 
509e31c45bSnbeams       // Grad kernels:
51437930d1SJeremy L Thompson       block_sizes[1] = 256;
529e31c45bSnbeams 
539e31c45bSnbeams       // Weight kernels:
54437930d1SJeremy L Thompson       block_sizes[2] = 256;
559e31c45bSnbeams     } break;
569e31c45bSnbeams     case 2: {
579e31c45bSnbeams       // Interp kernels:
589e201c85SYohann       CeedInt required = thread_1d * thread_1d;
59b7453713SJeremy L Thompson 
609e201c85SYohann       block_sizes[0] = CeedIntMax(256, ComputeBlockSizeFromRequirement(required));
619e31c45bSnbeams 
629e31c45bSnbeams       // Grad kernels: currently use same required minimum threads
639e201c85SYohann       block_sizes[1] = CeedIntMax(256, ComputeBlockSizeFromRequirement(required));
649e31c45bSnbeams 
659e31c45bSnbeams       // Weight kernels:
66437930d1SJeremy L Thompson       required       = CeedIntMax(64, Q_1d * Q_1d);
679e201c85SYohann       block_sizes[2] = CeedIntMax(256, ComputeBlockSizeFromRequirement(required));
689e31c45bSnbeams 
699e31c45bSnbeams     } break;
709e31c45bSnbeams     case 3: {
719e31c45bSnbeams       // Interp kernels:
729e201c85SYohann       CeedInt required = thread_1d * thread_1d;
73b7453713SJeremy L Thompson 
749e201c85SYohann       block_sizes[0] = CeedIntMax(256, ComputeBlockSizeFromRequirement(required));
759e31c45bSnbeams 
769e31c45bSnbeams       // Grad kernels: currently use same required minimum threads
779e201c85SYohann       block_sizes[1] = CeedIntMax(256, ComputeBlockSizeFromRequirement(required));
789e31c45bSnbeams 
799e31c45bSnbeams       // Weight kernels:
80437930d1SJeremy L Thompson       required       = Q_1d * Q_1d * Q_1d;
819e201c85SYohann       block_sizes[2] = CeedIntMax(256, ComputeBlockSizeFromRequirement(required));
829e31c45bSnbeams     }
839e31c45bSnbeams   }
84e15f9bd0SJeremy L Thompson   return CEED_ERROR_SUCCESS;
859e31c45bSnbeams }
869e31c45bSnbeams 
879e31c45bSnbeams //------------------------------------------------------------------------------
887d8d0e25Snbeams // Apply basis
897d8d0e25Snbeams //------------------------------------------------------------------------------
90*db2becc9SJeremy L Thompson static int CeedBasisApplyTensorCore_Hip_shared(CeedBasis basis, bool apply_add, const CeedInt num_elem, CeedTransposeMode t_mode,
91*db2becc9SJeremy L Thompson                                                CeedEvalMode eval_mode, CeedVector u, CeedVector v) {
927d8d0e25Snbeams   Ceed                  ceed;
936dbfb411Snbeams   Ceed_Hip             *ceed_Hip;
94437930d1SJeremy L Thompson   CeedInt               dim, num_comp;
95b7453713SJeremy L Thompson   const CeedScalar     *d_u;
96b7453713SJeremy L Thompson   CeedScalar           *d_v;
97b7453713SJeremy L Thompson   CeedBasis_Hip_shared *data;
98b7453713SJeremy L Thompson 
99b7453713SJeremy L Thompson   CeedCallBackend(CeedBasisGetCeed(basis, &ceed));
100b7453713SJeremy L Thompson   CeedCallBackend(CeedGetData(ceed, &ceed_Hip));
101b7453713SJeremy L Thompson   CeedCallBackend(CeedBasisGetData(basis, &data));
1022b730f8bSJeremy L Thompson   CeedCallBackend(CeedBasisGetDimension(basis, &dim));
1032b730f8bSJeremy L Thompson   CeedCallBackend(CeedBasisGetNumComponents(basis, &num_comp));
1047d8d0e25Snbeams 
1059ea2cfd9SJeremy L Thompson   // Get read/write access to u, v
1066574a04fSJeremy L Thompson   if (u != CEED_VECTOR_NONE) CeedCallBackend(CeedVectorGetArrayRead(u, CEED_MEM_DEVICE, &d_u));
1076574a04fSJeremy L Thompson   else CeedCheck(eval_mode == CEED_EVAL_WEIGHT, ceed, CEED_ERROR_BACKEND, "An input vector is required for this CeedEvalMode");
108*db2becc9SJeremy L Thompson   if (apply_add) CeedCallBackend(CeedVectorGetArray(v, CEED_MEM_DEVICE, &d_v));
109*db2becc9SJeremy L Thompson   else CeedCallBackend(CeedVectorGetArrayWrite(v, CEED_MEM_DEVICE, &d_v));
1107d8d0e25Snbeams 
1117d8d0e25Snbeams   // Apply basis operation
112437930d1SJeremy L Thompson   switch (eval_mode) {
1137d8d0e25Snbeams     case CEED_EVAL_INTERP: {
114437930d1SJeremy L Thompson       CeedInt P_1d, Q_1d;
115437930d1SJeremy L Thompson       CeedInt block_size = data->block_sizes[0];
116b7453713SJeremy L Thompson 
1172b730f8bSJeremy L Thompson       CeedCallBackend(CeedBasisGetNumNodes1D(basis, &P_1d));
1182b730f8bSJeremy L Thompson       CeedCallBackend(CeedBasisGetNumQuadraturePoints1D(basis, &Q_1d));
119437930d1SJeremy L Thompson       CeedInt thread_1d     = CeedIntMax(Q_1d, P_1d);
1202b730f8bSJeremy L Thompson       void   *interp_args[] = {(void *)&num_elem, &data->d_interp_1d, &d_u, &d_v};
121b7453713SJeremy L Thompson 
1227d8d0e25Snbeams       if (dim == 1) {
123437930d1SJeremy L Thompson         CeedInt elems_per_block = 64 * thread_1d > 256 ? 256 / thread_1d : 64;
124437930d1SJeremy L Thompson         elems_per_block         = elems_per_block > 0 ? elems_per_block : 1;
1252b730f8bSJeremy L Thompson         CeedInt grid            = num_elem / elems_per_block + ((num_elem / elems_per_block * elems_per_block < num_elem) ? 1 : 0);
126437930d1SJeremy L Thompson         CeedInt shared_mem      = elems_per_block * thread_1d * sizeof(CeedScalar);
127b2165e7aSSebastian Grimberg 
1289e201c85SYohann         if (t_mode == CEED_TRANSPOSE) {
129*db2becc9SJeremy L Thompson           CeedCallBackend(CeedRunKernelDimShared_Hip(ceed, apply_add ? data->InterpTransposeAdd : data->InterpTranspose, grid, thread_1d, 1,
130*db2becc9SJeremy L Thompson                                                      elems_per_block, shared_mem, interp_args));
1319e201c85SYohann         } else {
132eb7e6cafSJeremy L Thompson           CeedCallBackend(CeedRunKernelDimShared_Hip(ceed, data->Interp, grid, thread_1d, 1, elems_per_block, shared_mem, interp_args));
1339e201c85SYohann         }
1347d8d0e25Snbeams       } else if (dim == 2) {
1359e31c45bSnbeams         // Check if required threads is small enough to do multiple elems
1362b730f8bSJeremy L Thompson         const CeedInt elems_per_block = CeedIntMax(block_size / (thread_1d * thread_1d), 1);
1372b730f8bSJeremy L Thompson         CeedInt       grid            = num_elem / elems_per_block + ((num_elem / elems_per_block * elems_per_block < num_elem) ? 1 : 0);
1382b730f8bSJeremy L Thompson         CeedInt       shared_mem      = elems_per_block * thread_1d * thread_1d * sizeof(CeedScalar);
139b2165e7aSSebastian Grimberg 
1409e201c85SYohann         if (t_mode == CEED_TRANSPOSE) {
141*db2becc9SJeremy L Thompson           CeedCallBackend(CeedRunKernelDimShared_Hip(ceed, apply_add ? data->InterpTransposeAdd : data->InterpTranspose, grid, thread_1d, thread_1d,
142*db2becc9SJeremy L Thompson                                                      elems_per_block, shared_mem, interp_args));
1439e201c85SYohann         } else {
144eb7e6cafSJeremy L Thompson           CeedCallBackend(CeedRunKernelDimShared_Hip(ceed, data->Interp, grid, thread_1d, thread_1d, elems_per_block, shared_mem, interp_args));
1459e201c85SYohann         }
1467d8d0e25Snbeams       } else if (dim == 3) {
1472b730f8bSJeremy L Thompson         const CeedInt elems_per_block = CeedIntMax(block_size / (thread_1d * thread_1d), 1);
1482b730f8bSJeremy L Thompson         CeedInt       grid            = num_elem / elems_per_block + ((num_elem / elems_per_block * elems_per_block < num_elem) ? 1 : 0);
1492b730f8bSJeremy L Thompson         CeedInt       shared_mem      = elems_per_block * thread_1d * thread_1d * sizeof(CeedScalar);
150b2165e7aSSebastian Grimberg 
1519e201c85SYohann         if (t_mode == CEED_TRANSPOSE) {
152*db2becc9SJeremy L Thompson           CeedCallBackend(CeedRunKernelDimShared_Hip(ceed, apply_add ? data->InterpTransposeAdd : data->InterpTranspose, grid, thread_1d, thread_1d,
153*db2becc9SJeremy L Thompson                                                      elems_per_block, shared_mem, interp_args));
1549e201c85SYohann         } else {
155eb7e6cafSJeremy L Thompson           CeedCallBackend(CeedRunKernelDimShared_Hip(ceed, data->Interp, grid, thread_1d, thread_1d, elems_per_block, shared_mem, interp_args));
1569e201c85SYohann         }
1577d8d0e25Snbeams       }
1587d8d0e25Snbeams     } break;
1597d8d0e25Snbeams     case CEED_EVAL_GRAD: {
160437930d1SJeremy L Thompson       CeedInt P_1d, Q_1d;
161437930d1SJeremy L Thompson       CeedInt block_size = data->block_sizes[1];
162b7453713SJeremy L Thompson 
1632b730f8bSJeremy L Thompson       CeedCallBackend(CeedBasisGetNumNodes1D(basis, &P_1d));
1642b730f8bSJeremy L Thompson       CeedCallBackend(CeedBasisGetNumQuadraturePoints1D(basis, &Q_1d));
165437930d1SJeremy L Thompson       CeedInt     thread_1d = CeedIntMax(Q_1d, P_1d);
1669e201c85SYohann       CeedScalar *d_grad_1d = data->d_grad_1d;
167b7453713SJeremy L Thompson 
1689e201c85SYohann       if (data->d_collo_grad_1d) {
1699e201c85SYohann         d_grad_1d = data->d_collo_grad_1d;
1709e201c85SYohann       }
1712b730f8bSJeremy L Thompson       void *grad_args[] = {(void *)&num_elem, &data->d_interp_1d, &d_grad_1d, &d_u, &d_v};
1727d8d0e25Snbeams       if (dim == 1) {
173437930d1SJeremy L Thompson         CeedInt elems_per_block = 64 * thread_1d > 256 ? 256 / thread_1d : 64;
174437930d1SJeremy L Thompson         elems_per_block         = elems_per_block > 0 ? elems_per_block : 1;
1752b730f8bSJeremy L Thompson         CeedInt grid            = num_elem / elems_per_block + ((num_elem / elems_per_block * elems_per_block < num_elem) ? 1 : 0);
176437930d1SJeremy L Thompson         CeedInt shared_mem      = elems_per_block * thread_1d * sizeof(CeedScalar);
177b2165e7aSSebastian Grimberg 
1789e201c85SYohann         if (t_mode == CEED_TRANSPOSE) {
179*db2becc9SJeremy L Thompson           CeedCallBackend(CeedRunKernelDimShared_Hip(ceed, apply_add ? data->GradTransposeAdd : data->GradTranspose, grid, thread_1d, 1,
180*db2becc9SJeremy L Thompson                                                      elems_per_block, shared_mem, grad_args));
1819e201c85SYohann         } else {
182eb7e6cafSJeremy L Thompson           CeedCallBackend(CeedRunKernelDimShared_Hip(ceed, data->Grad, grid, thread_1d, 1, elems_per_block, shared_mem, grad_args));
1839e201c85SYohann         }
1847d8d0e25Snbeams       } else if (dim == 2) {
1859e31c45bSnbeams         // Check if required threads is small enough to do multiple elems
1862b730f8bSJeremy L Thompson         const CeedInt elems_per_block = CeedIntMax(block_size / (thread_1d * thread_1d), 1);
1872b730f8bSJeremy L Thompson         CeedInt       grid            = num_elem / elems_per_block + ((num_elem / elems_per_block * elems_per_block < num_elem) ? 1 : 0);
1882b730f8bSJeremy L Thompson         CeedInt       shared_mem      = elems_per_block * thread_1d * thread_1d * sizeof(CeedScalar);
189b2165e7aSSebastian Grimberg 
1909e201c85SYohann         if (t_mode == CEED_TRANSPOSE) {
191*db2becc9SJeremy L Thompson           CeedCallBackend(CeedRunKernelDimShared_Hip(ceed, apply_add ? data->GradTransposeAdd : data->GradTranspose, grid, thread_1d, thread_1d,
192*db2becc9SJeremy L Thompson                                                      elems_per_block, shared_mem, grad_args));
1939e201c85SYohann         } else {
194eb7e6cafSJeremy L Thompson           CeedCallBackend(CeedRunKernelDimShared_Hip(ceed, data->Grad, grid, thread_1d, thread_1d, elems_per_block, shared_mem, grad_args));
1959e201c85SYohann         }
1967d8d0e25Snbeams       } else if (dim == 3) {
1972b730f8bSJeremy L Thompson         const CeedInt elems_per_block = CeedIntMax(block_size / (thread_1d * thread_1d), 1);
1982b730f8bSJeremy L Thompson         CeedInt       grid            = num_elem / elems_per_block + ((num_elem / elems_per_block * elems_per_block < num_elem) ? 1 : 0);
1992b730f8bSJeremy L Thompson         CeedInt       shared_mem      = elems_per_block * thread_1d * thread_1d * sizeof(CeedScalar);
200b2165e7aSSebastian Grimberg 
2019e201c85SYohann         if (t_mode == CEED_TRANSPOSE) {
202*db2becc9SJeremy L Thompson           CeedCallBackend(CeedRunKernelDimShared_Hip(ceed, apply_add ? data->GradTransposeAdd : data->GradTranspose, grid, thread_1d, thread_1d,
203*db2becc9SJeremy L Thompson                                                      elems_per_block, shared_mem, grad_args));
2049e201c85SYohann         } else {
205eb7e6cafSJeremy L Thompson           CeedCallBackend(CeedRunKernelDimShared_Hip(ceed, data->Grad, grid, thread_1d, thread_1d, elems_per_block, shared_mem, grad_args));
2069e201c85SYohann         }
2077d8d0e25Snbeams       }
2087d8d0e25Snbeams     } break;
2097d8d0e25Snbeams     case CEED_EVAL_WEIGHT: {
210437930d1SJeremy L Thompson       CeedInt Q_1d;
211437930d1SJeremy L Thompson       CeedInt block_size = data->block_sizes[2];
212b7453713SJeremy L Thompson 
213097cc795SJames Wright       CeedCheck(data->d_q_weight_1d, ceed, CEED_ERROR_BACKEND, "%s not supported; q_weights_1d not set", CeedEvalModes[eval_mode]);
2142b730f8bSJeremy L Thompson       CeedCallBackend(CeedBasisGetNumQuadraturePoints1D(basis, &Q_1d));
215437930d1SJeremy L Thompson       void *weight_args[] = {(void *)&num_elem, (void *)&data->d_q_weight_1d, &d_v};
216b7453713SJeremy L Thompson 
2177d8d0e25Snbeams       if (dim == 1) {
218437930d1SJeremy L Thompson         const CeedInt opt_elems       = block_size / Q_1d;
219437930d1SJeremy L Thompson         const CeedInt elems_per_block = opt_elems > 0 ? opt_elems : 1;
2202b730f8bSJeremy L Thompson         const CeedInt grid_size       = num_elem / elems_per_block + ((num_elem / elems_per_block * elems_per_block < num_elem) ? 1 : 0);
221b2165e7aSSebastian Grimberg 
222eb7e6cafSJeremy L Thompson         CeedCallBackend(CeedRunKernelDim_Hip(ceed, data->Weight, grid_size, Q_1d, elems_per_block, 1, weight_args));
2237d8d0e25Snbeams       } else if (dim == 2) {
224437930d1SJeremy L Thompson         const CeedInt opt_elems       = block_size / (Q_1d * Q_1d);
225437930d1SJeremy L Thompson         const CeedInt elems_per_block = opt_elems > 0 ? opt_elems : 1;
2262b730f8bSJeremy L Thompson         const CeedInt grid_size       = num_elem / elems_per_block + ((num_elem / elems_per_block * elems_per_block < num_elem) ? 1 : 0);
227b2165e7aSSebastian Grimberg 
228eb7e6cafSJeremy L Thompson         CeedCallBackend(CeedRunKernelDim_Hip(ceed, data->Weight, grid_size, Q_1d, Q_1d, elems_per_block, weight_args));
2297d8d0e25Snbeams       } else if (dim == 3) {
2309e201c85SYohann         const CeedInt opt_elems       = block_size / (Q_1d * Q_1d);
2319e201c85SYohann         const CeedInt elems_per_block = opt_elems > 0 ? opt_elems : 1;
2322b730f8bSJeremy L Thompson         const CeedInt grid_size       = num_elem / elems_per_block + ((num_elem / elems_per_block * elems_per_block < num_elem) ? 1 : 0);
233b2165e7aSSebastian Grimberg 
234eb7e6cafSJeremy L Thompson         CeedCallBackend(CeedRunKernelDim_Hip(ceed, data->Weight, grid_size, Q_1d, Q_1d, elems_per_block, weight_args));
2357d8d0e25Snbeams       }
2367d8d0e25Snbeams     } break;
2379ea2cfd9SJeremy L Thompson     case CEED_EVAL_NONE: /* handled separately below */
2389ea2cfd9SJeremy L Thompson       break;
2397d8d0e25Snbeams     // LCOV_EXCL_START
2407d8d0e25Snbeams     case CEED_EVAL_DIV:
2417d8d0e25Snbeams     case CEED_EVAL_CURL:
242bcbe1c99SJeremy L Thompson       return CeedError(ceed, CEED_ERROR_BACKEND, "%s not supported", CeedEvalModes[eval_mode]);
2437d8d0e25Snbeams       // LCOV_EXCL_STOP
2447d8d0e25Snbeams   }
2457d8d0e25Snbeams 
2469ea2cfd9SJeremy L Thompson   // Restore vectors, cover CEED_EVAL_NONE
2472b730f8bSJeremy L Thompson   CeedCallBackend(CeedVectorRestoreArray(v, &d_v));
2489ea2cfd9SJeremy L Thompson   if (eval_mode == CEED_EVAL_NONE) CeedCallBackend(CeedVectorSetArray(v, CEED_MEM_DEVICE, CEED_COPY_VALUES, (CeedScalar *)d_u));
2499ea2cfd9SJeremy L Thompson   if (eval_mode != CEED_EVAL_WEIGHT) CeedCallBackend(CeedVectorRestoreArrayRead(u, &d_u));
250e15f9bd0SJeremy L Thompson   return CEED_ERROR_SUCCESS;
2517d8d0e25Snbeams }
2527d8d0e25Snbeams 
253*db2becc9SJeremy L Thompson int CeedBasisApplyTensor_Hip_shared(CeedBasis basis, const CeedInt num_elem, CeedTransposeMode t_mode, CeedEvalMode eval_mode, CeedVector u,
254*db2becc9SJeremy L Thompson                                     CeedVector v) {
255*db2becc9SJeremy L Thompson   CeedCallBackend(CeedBasisApplyTensorCore_Hip_shared(basis, false, num_elem, t_mode, eval_mode, u, v));
256*db2becc9SJeremy L Thompson   return CEED_ERROR_SUCCESS;
257*db2becc9SJeremy L Thompson }
258*db2becc9SJeremy L Thompson 
259*db2becc9SJeremy L Thompson int CeedBasisApplyAddTensor_Hip_shared(CeedBasis basis, const CeedInt num_elem, CeedTransposeMode t_mode, CeedEvalMode eval_mode, CeedVector u,
260*db2becc9SJeremy L Thompson                                        CeedVector v) {
261*db2becc9SJeremy L Thompson   CeedCallBackend(CeedBasisApplyTensorCore_Hip_shared(basis, true, num_elem, t_mode, eval_mode, u, v));
262*db2becc9SJeremy L Thompson   return CEED_ERROR_SUCCESS;
263*db2becc9SJeremy L Thompson }
264*db2becc9SJeremy L Thompson 
2657d8d0e25Snbeams //------------------------------------------------------------------------------
2661dda9c1aSJeremy L Thompson // Basis apply - tensor AtPoints
2671dda9c1aSJeremy L Thompson //------------------------------------------------------------------------------
268*db2becc9SJeremy L Thompson static int CeedBasisApplyAtPointsCore_Hip_shared(CeedBasis basis, bool apply_add, const CeedInt num_elem, const CeedInt *num_points,
269*db2becc9SJeremy L Thompson                                                  CeedTransposeMode t_mode, CeedEvalMode eval_mode, CeedVector x_ref, CeedVector u, CeedVector v) {
2701dda9c1aSJeremy L Thompson   Ceed                  ceed;
2711dda9c1aSJeremy L Thompson   CeedInt               Q_1d, dim, max_num_points = num_points[0];
2721dda9c1aSJeremy L Thompson   const CeedInt         is_transpose   = t_mode == CEED_TRANSPOSE;
2731dda9c1aSJeremy L Thompson   const int             max_block_size = 32;
2741dda9c1aSJeremy L Thompson   const CeedScalar     *d_x, *d_u;
2751dda9c1aSJeremy L Thompson   CeedScalar           *d_v;
2761dda9c1aSJeremy L Thompson   CeedBasis_Hip_shared *data;
2771dda9c1aSJeremy L Thompson 
2781dda9c1aSJeremy L Thompson   CeedCallBackend(CeedBasisGetCeed(basis, &ceed));
2791dda9c1aSJeremy L Thompson   CeedCallBackend(CeedBasisGetData(basis, &data));
2801dda9c1aSJeremy L Thompson   CeedCallBackend(CeedBasisGetNumQuadraturePoints1D(basis, &Q_1d));
2811dda9c1aSJeremy L Thompson   CeedCallBackend(CeedBasisGetDimension(basis, &dim));
2821dda9c1aSJeremy L Thompson 
2831dda9c1aSJeremy L Thompson   // Check uniform number of points per elem
2841dda9c1aSJeremy L Thompson   for (CeedInt i = 1; i < num_elem; i++) {
2851dda9c1aSJeremy L Thompson     CeedCheck(max_num_points == num_points[i], ceed, CEED_ERROR_BACKEND,
2861dda9c1aSJeremy L Thompson               "BasisApplyAtPoints only supported for the same number of points in each element");
2871dda9c1aSJeremy L Thompson   }
2881dda9c1aSJeremy L Thompson 
2891dda9c1aSJeremy L Thompson   // Weight handled separately
2901dda9c1aSJeremy L Thompson   if (eval_mode == CEED_EVAL_WEIGHT) {
2911dda9c1aSJeremy L Thompson     CeedCall(CeedVectorSetValue(v, 1.0));
2921dda9c1aSJeremy L Thompson     return CEED_ERROR_SUCCESS;
2931dda9c1aSJeremy L Thompson   }
2941dda9c1aSJeremy L Thompson 
2951dda9c1aSJeremy L Thompson   // Build kernels if needed
2961dda9c1aSJeremy L Thompson   if (data->num_points != max_num_points) {
2971dda9c1aSJeremy L Thompson     CeedInt P_1d;
2981dda9c1aSJeremy L Thompson 
2991dda9c1aSJeremy L Thompson     CeedCallBackend(CeedBasisGetNumNodes1D(basis, &P_1d));
3001dda9c1aSJeremy L Thompson     data->num_points = max_num_points;
3011dda9c1aSJeremy L Thompson 
3021dda9c1aSJeremy L Thompson     // -- Create interp matrix to Chebyshev coefficients
3031dda9c1aSJeremy L Thompson     if (!data->d_chebyshev_interp_1d) {
3041dda9c1aSJeremy L Thompson       CeedSize    interp_bytes;
3051dda9c1aSJeremy L Thompson       CeedScalar *chebyshev_interp_1d;
3061dda9c1aSJeremy L Thompson 
3071dda9c1aSJeremy L Thompson       interp_bytes = P_1d * Q_1d * sizeof(CeedScalar);
3081dda9c1aSJeremy L Thompson       CeedCallBackend(CeedCalloc(P_1d * Q_1d, &chebyshev_interp_1d));
3091dda9c1aSJeremy L Thompson       CeedCall(CeedBasisGetChebyshevInterp1D(basis, chebyshev_interp_1d));
3101dda9c1aSJeremy L Thompson       CeedCallHip(ceed, hipMalloc((void **)&data->d_chebyshev_interp_1d, interp_bytes));
3111dda9c1aSJeremy L Thompson       CeedCallHip(ceed, hipMemcpy(data->d_chebyshev_interp_1d, chebyshev_interp_1d, interp_bytes, hipMemcpyHostToDevice));
3121dda9c1aSJeremy L Thompson       CeedCallBackend(CeedFree(&chebyshev_interp_1d));
3131dda9c1aSJeremy L Thompson     }
3141dda9c1aSJeremy L Thompson 
3151dda9c1aSJeremy L Thompson     // -- Compile kernels
3161dda9c1aSJeremy L Thompson     char       *basis_kernel_source;
3171dda9c1aSJeremy L Thompson     const char *basis_kernel_path;
3181dda9c1aSJeremy L Thompson     CeedInt     num_comp;
3191dda9c1aSJeremy L Thompson 
3201dda9c1aSJeremy L Thompson     if (data->moduleAtPoints) CeedCallHip(ceed, hipModuleUnload(data->moduleAtPoints));
3211dda9c1aSJeremy L Thompson     CeedCallBackend(CeedBasisGetNumComponents(basis, &num_comp));
3221dda9c1aSJeremy L Thompson     CeedCallBackend(CeedGetJitAbsolutePath(ceed, "ceed/jit-source/hip/hip-ref-basis-tensor-at-points.h", &basis_kernel_path));
3231dda9c1aSJeremy L Thompson     CeedDebug256(ceed, CEED_DEBUG_COLOR_SUCCESS, "----- Loading Basis Kernel Source -----\n");
3241dda9c1aSJeremy L Thompson     CeedCallBackend(CeedLoadSourceToBuffer(ceed, basis_kernel_path, &basis_kernel_source));
3251dda9c1aSJeremy L Thompson     CeedDebug256(ceed, CEED_DEBUG_COLOR_SUCCESS, "----- Loading Basis Kernel Source Complete! -----\n");
3261dda9c1aSJeremy L Thompson     CeedCallBackend(CeedCompile_Hip(ceed, basis_kernel_source, &data->moduleAtPoints, 9, "BASIS_Q_1D", Q_1d, "BASIS_P_1D", P_1d, "BASIS_BUF_LEN",
327f7c9815fSJeremy L Thompson                                     Q_1d * CeedIntPow(Q_1d > P_1d ? Q_1d : P_1d, dim - 1), "BASIS_DIM", dim, "BASIS_NUM_COMP", num_comp,
3281dda9c1aSJeremy L Thompson                                     "BASIS_NUM_NODES", CeedIntPow(P_1d, dim), "BASIS_NUM_QPTS", CeedIntPow(Q_1d, dim), "BASIS_NUM_PTS",
329f7c9815fSJeremy L Thompson                                     max_num_points, "POINTS_BUFF_LEN", CeedIntPow(Q_1d, dim - 1)));
3301dda9c1aSJeremy L Thompson     CeedCallBackend(CeedGetKernel_Hip(ceed, data->moduleAtPoints, "InterpAtPoints", &data->InterpAtPoints));
3311dda9c1aSJeremy L Thompson     CeedCallBackend(CeedGetKernel_Hip(ceed, data->moduleAtPoints, "GradAtPoints", &data->GradAtPoints));
3321dda9c1aSJeremy L Thompson     CeedCallBackend(CeedFree(&basis_kernel_path));
3331dda9c1aSJeremy L Thompson     CeedCallBackend(CeedFree(&basis_kernel_source));
3341dda9c1aSJeremy L Thompson   }
3351dda9c1aSJeremy L Thompson 
3361dda9c1aSJeremy L Thompson   // Get read/write access to u, v
3371dda9c1aSJeremy L Thompson   CeedCallBackend(CeedVectorGetArrayRead(x_ref, CEED_MEM_DEVICE, &d_x));
3381dda9c1aSJeremy L Thompson   if (u != CEED_VECTOR_NONE) CeedCallBackend(CeedVectorGetArrayRead(u, CEED_MEM_DEVICE, &d_u));
3391dda9c1aSJeremy L Thompson   else CeedCheck(eval_mode == CEED_EVAL_WEIGHT, ceed, CEED_ERROR_BACKEND, "An input vector is required for this CeedEvalMode");
340*db2becc9SJeremy L Thompson   if (apply_add) CeedCallBackend(CeedVectorGetArray(v, CEED_MEM_DEVICE, &d_v));
341*db2becc9SJeremy L Thompson   else CeedCallBackend(CeedVectorGetArrayWrite(v, CEED_MEM_DEVICE, &d_v));
3421dda9c1aSJeremy L Thompson 
3431dda9c1aSJeremy L Thompson   // Clear v for transpose operation
344*db2becc9SJeremy L Thompson   if (is_transpose && !apply_add) {
3451dda9c1aSJeremy L Thompson     CeedSize length;
3461dda9c1aSJeremy L Thompson 
3471dda9c1aSJeremy L Thompson     CeedCallBackend(CeedVectorGetLength(v, &length));
3481dda9c1aSJeremy L Thompson     CeedCallHip(ceed, hipMemset(d_v, 0, length * sizeof(CeedScalar)));
3491dda9c1aSJeremy L Thompson   }
3501dda9c1aSJeremy L Thompson 
3511dda9c1aSJeremy L Thompson   // Basis action
3521dda9c1aSJeremy L Thompson   switch (eval_mode) {
3531dda9c1aSJeremy L Thompson     case CEED_EVAL_INTERP: {
3541dda9c1aSJeremy L Thompson       void         *interp_args[] = {(void *)&num_elem, (void *)&is_transpose, &data->d_chebyshev_interp_1d, &d_x, &d_u, &d_v};
3551dda9c1aSJeremy L Thompson       const CeedInt block_size    = CeedIntMin(CeedIntPow(Q_1d, dim), max_block_size);
3561dda9c1aSJeremy L Thompson 
3571dda9c1aSJeremy L Thompson       CeedCallBackend(CeedRunKernel_Hip(ceed, data->InterpAtPoints, num_elem, block_size, interp_args));
3581dda9c1aSJeremy L Thompson     } break;
3591dda9c1aSJeremy L Thompson     case CEED_EVAL_GRAD: {
3601dda9c1aSJeremy L Thompson       void         *grad_args[] = {(void *)&num_elem, (void *)&is_transpose, &data->d_chebyshev_interp_1d, &d_x, &d_u, &d_v};
3612d10e82cSJeremy L Thompson       const CeedInt block_size  = CeedIntMin(CeedIntPow(Q_1d, dim), max_block_size);
3621dda9c1aSJeremy L Thompson 
3631dda9c1aSJeremy L Thompson       CeedCallBackend(CeedRunKernel_Hip(ceed, data->GradAtPoints, num_elem, block_size, grad_args));
3641dda9c1aSJeremy L Thompson     } break;
3651dda9c1aSJeremy L Thompson     case CEED_EVAL_WEIGHT:
3661dda9c1aSJeremy L Thompson     case CEED_EVAL_NONE: /* handled separately below */
3671dda9c1aSJeremy L Thompson       break;
3681dda9c1aSJeremy L Thompson     // LCOV_EXCL_START
3691dda9c1aSJeremy L Thompson     case CEED_EVAL_DIV:
3701dda9c1aSJeremy L Thompson     case CEED_EVAL_CURL:
3711dda9c1aSJeremy L Thompson       return CeedError(ceed, CEED_ERROR_BACKEND, "%s not supported", CeedEvalModes[eval_mode]);
3721dda9c1aSJeremy L Thompson       // LCOV_EXCL_STOP
3731dda9c1aSJeremy L Thompson   }
3741dda9c1aSJeremy L Thompson 
3751dda9c1aSJeremy L Thompson   // Restore vectors, cover CEED_EVAL_NONE
3761dda9c1aSJeremy L Thompson   CeedCallBackend(CeedVectorRestoreArrayRead(x_ref, &d_x));
3771dda9c1aSJeremy L Thompson   CeedCallBackend(CeedVectorRestoreArray(v, &d_v));
3781dda9c1aSJeremy L Thompson   if (eval_mode == CEED_EVAL_NONE) CeedCallBackend(CeedVectorSetArray(v, CEED_MEM_DEVICE, CEED_COPY_VALUES, (CeedScalar *)d_u));
3791dda9c1aSJeremy L Thompson   if (eval_mode != CEED_EVAL_WEIGHT) CeedCallBackend(CeedVectorRestoreArrayRead(u, &d_u));
3801dda9c1aSJeremy L Thompson   return CEED_ERROR_SUCCESS;
3811dda9c1aSJeremy L Thompson }
3821dda9c1aSJeremy L Thompson 
383*db2becc9SJeremy L Thompson static int CeedBasisApplyAtPoints_Hip_shared(CeedBasis basis, const CeedInt num_elem, const CeedInt *num_points, CeedTransposeMode t_mode,
384*db2becc9SJeremy L Thompson                                              CeedEvalMode eval_mode, CeedVector x_ref, CeedVector u, CeedVector v) {
385*db2becc9SJeremy L Thompson   CeedCallBackend(CeedBasisApplyAtPointsCore_Hip_shared(basis, false, num_elem, num_points, t_mode, eval_mode, x_ref, u, v));
386*db2becc9SJeremy L Thompson   return CEED_ERROR_SUCCESS;
387*db2becc9SJeremy L Thompson }
388*db2becc9SJeremy L Thompson 
389*db2becc9SJeremy L Thompson static int CeedBasisApplyAddAtPoints_Hip_shared(CeedBasis basis, const CeedInt num_elem, const CeedInt *num_points, CeedTransposeMode t_mode,
390*db2becc9SJeremy L Thompson                                                 CeedEvalMode eval_mode, CeedVector x_ref, CeedVector u, CeedVector v) {
391*db2becc9SJeremy L Thompson   CeedCallBackend(CeedBasisApplyAtPointsCore_Hip_shared(basis, true, num_elem, num_points, t_mode, eval_mode, x_ref, u, v));
392*db2becc9SJeremy L Thompson   return CEED_ERROR_SUCCESS;
393*db2becc9SJeremy L Thompson }
394*db2becc9SJeremy L Thompson 
3951dda9c1aSJeremy L Thompson //------------------------------------------------------------------------------
3967d8d0e25Snbeams // Destroy basis
3977d8d0e25Snbeams //------------------------------------------------------------------------------
3987d8d0e25Snbeams static int CeedBasisDestroy_Hip_shared(CeedBasis basis) {
3997d8d0e25Snbeams   Ceed                  ceed;
4007d8d0e25Snbeams   CeedBasis_Hip_shared *data;
401b7453713SJeremy L Thompson 
402b7453713SJeremy L Thompson   CeedCallBackend(CeedBasisGetCeed(basis, &ceed));
4032b730f8bSJeremy L Thompson   CeedCallBackend(CeedBasisGetData(basis, &data));
4042b730f8bSJeremy L Thompson   CeedCallHip(ceed, hipModuleUnload(data->module));
4051dda9c1aSJeremy L Thompson   if (data->moduleAtPoints) CeedCallHip(ceed, hipModuleUnload(data->moduleAtPoints));
406097cc795SJames Wright   if (data->d_q_weight_1d) CeedCallHip(ceed, hipFree(data->d_q_weight_1d));
4072b730f8bSJeremy L Thompson   CeedCallHip(ceed, hipFree(data->d_interp_1d));
4082b730f8bSJeremy L Thompson   CeedCallHip(ceed, hipFree(data->d_grad_1d));
4092b730f8bSJeremy L Thompson   CeedCallHip(ceed, hipFree(data->d_collo_grad_1d));
4101dda9c1aSJeremy L Thompson   CeedCallHip(ceed, hipFree(data->d_chebyshev_interp_1d));
4112b730f8bSJeremy L Thompson   CeedCallBackend(CeedFree(&data));
412e15f9bd0SJeremy L Thompson   return CEED_ERROR_SUCCESS;
4137d8d0e25Snbeams }
4147d8d0e25Snbeams 
4157d8d0e25Snbeams //------------------------------------------------------------------------------
4167d8d0e25Snbeams // Create tensor basis
4177d8d0e25Snbeams //------------------------------------------------------------------------------
4182b730f8bSJeremy L Thompson int CeedBasisCreateTensorH1_Hip_shared(CeedInt dim, CeedInt P_1d, CeedInt Q_1d, const CeedScalar *interp_1d, const CeedScalar *grad_1d,
4196574a04fSJeremy L Thompson                                        const CeedScalar *q_ref_1d, const CeedScalar *q_weight_1d, CeedBasis basis) {
4207d8d0e25Snbeams   Ceed                  ceed;
42122070f95SJeremy L Thompson   char                 *basis_kernel_source;
42222070f95SJeremy L Thompson   const char           *basis_kernel_path;
423b7453713SJeremy L Thompson   CeedInt               num_comp;
424b7453713SJeremy L Thompson   const CeedInt         q_bytes      = Q_1d * sizeof(CeedScalar);
425397164e9SSebastian Grimberg   const CeedInt         interp_bytes = q_bytes * P_1d;
4267d8d0e25Snbeams   CeedBasis_Hip_shared *data;
427b7453713SJeremy L Thompson 
428b7453713SJeremy L Thompson   CeedCallBackend(CeedBasisGetCeed(basis, &ceed));
4292b730f8bSJeremy L Thompson   CeedCallBackend(CeedCalloc(1, &data));
4307d8d0e25Snbeams 
4317d8d0e25Snbeams   // Copy basis data to GPU
432097cc795SJames Wright   if (q_weight_1d) {
433b7453713SJeremy L Thompson     CeedCallHip(ceed, hipMalloc((void **)&data->d_q_weight_1d, q_bytes));
434b7453713SJeremy L Thompson     CeedCallHip(ceed, hipMemcpy(data->d_q_weight_1d, q_weight_1d, q_bytes, hipMemcpyHostToDevice));
435097cc795SJames Wright   }
436397164e9SSebastian Grimberg   CeedCallHip(ceed, hipMalloc((void **)&data->d_interp_1d, interp_bytes));
437397164e9SSebastian Grimberg   CeedCallHip(ceed, hipMemcpy(data->d_interp_1d, interp_1d, interp_bytes, hipMemcpyHostToDevice));
438397164e9SSebastian Grimberg   CeedCallHip(ceed, hipMalloc((void **)&data->d_grad_1d, interp_bytes));
439397164e9SSebastian Grimberg   CeedCallHip(ceed, hipMemcpy(data->d_grad_1d, grad_1d, interp_bytes, hipMemcpyHostToDevice));
4407d8d0e25Snbeams 
4417d8d0e25Snbeams   // Compute collocated gradient and copy to GPU
442437930d1SJeremy L Thompson   data->d_collo_grad_1d    = NULL;
4439e201c85SYohann   bool has_collocated_grad = dim == 3 && Q_1d >= P_1d;
444b7453713SJeremy L Thompson 
4459e201c85SYohann   if (has_collocated_grad) {
446437930d1SJeremy L Thompson     CeedScalar *collo_grad_1d;
447b7453713SJeremy L Thompson 
4482b730f8bSJeremy L Thompson     CeedCallBackend(CeedMalloc(Q_1d * Q_1d, &collo_grad_1d));
4492b730f8bSJeremy L Thompson     CeedCallBackend(CeedBasisGetCollocatedGrad(basis, collo_grad_1d));
450b7453713SJeremy L Thompson     CeedCallHip(ceed, hipMalloc((void **)&data->d_collo_grad_1d, q_bytes * Q_1d));
451b7453713SJeremy L Thompson     CeedCallHip(ceed, hipMemcpy(data->d_collo_grad_1d, collo_grad_1d, q_bytes * Q_1d, hipMemcpyHostToDevice));
4522b730f8bSJeremy L Thompson     CeedCallBackend(CeedFree(&collo_grad_1d));
4537d8d0e25Snbeams   }
4547d8d0e25Snbeams 
4559e31c45bSnbeams   // Set number of threads per block for basis kernels
4562b730f8bSJeremy L Thompson   CeedCallBackend(CeedBasisGetNumComponents(basis, &num_comp));
4572b730f8bSJeremy L Thompson   CeedCallBackend(ComputeBasisThreadBlockSizes(dim, P_1d, Q_1d, num_comp, data->block_sizes));
4589e31c45bSnbeams 
4599e31c45bSnbeams   // Compile basis kernels
4602b730f8bSJeremy L Thompson   CeedCallBackend(CeedGetJitAbsolutePath(ceed, "ceed/jit-source/hip/hip-shared-basis-tensor.h", &basis_kernel_path));
46123d4529eSJeremy L Thompson   CeedDebug256(ceed, CEED_DEBUG_COLOR_SUCCESS, "----- Loading Basis Kernel Source -----\n");
4622b730f8bSJeremy L Thompson   CeedCallBackend(CeedLoadSourceToBuffer(ceed, basis_kernel_path, &basis_kernel_source));
46323d4529eSJeremy L Thompson   CeedDebug256(ceed, CEED_DEBUG_COLOR_SUCCESS, "----- Loading Basis Kernel Source Complete! -----\n");
464eb7e6cafSJeremy L Thompson   CeedCallBackend(CeedCompile_Hip(ceed, basis_kernel_source, &data->module, 11, "BASIS_Q_1D", Q_1d, "BASIS_P_1D", P_1d, "T_1D",
465eb7e6cafSJeremy L Thompson                                   CeedIntMax(Q_1d, P_1d), "BASIS_DIM", dim, "BASIS_NUM_COMP", num_comp, "BASIS_NUM_NODES", CeedIntPow(P_1d, dim),
466eb7e6cafSJeremy L Thompson                                   "BASIS_NUM_QPTS", CeedIntPow(Q_1d, dim), "BASIS_INTERP_BLOCK_SIZE", data->block_sizes[0], "BASIS_GRAD_BLOCK_SIZE",
4672b730f8bSJeremy L Thompson                                   data->block_sizes[1], "BASIS_WEIGHT_BLOCK_SIZE", data->block_sizes[2], "BASIS_HAS_COLLOCATED_GRAD",
4682b730f8bSJeremy L Thompson                                   has_collocated_grad));
469eb7e6cafSJeremy L Thompson   CeedCallBackend(CeedGetKernel_Hip(ceed, data->module, "Interp", &data->Interp));
470eb7e6cafSJeremy L Thompson   CeedCallBackend(CeedGetKernel_Hip(ceed, data->module, "InterpTranspose", &data->InterpTranspose));
471*db2becc9SJeremy L Thompson   CeedCallBackend(CeedGetKernel_Hip(ceed, data->module, "InterpTransposeAdd", &data->InterpTransposeAdd));
472eb7e6cafSJeremy L Thompson   CeedCallBackend(CeedGetKernel_Hip(ceed, data->module, "Grad", &data->Grad));
473eb7e6cafSJeremy L Thompson   CeedCallBackend(CeedGetKernel_Hip(ceed, data->module, "GradTranspose", &data->GradTranspose));
474*db2becc9SJeremy L Thompson   CeedCallBackend(CeedGetKernel_Hip(ceed, data->module, "GradTransposeAdd", &data->GradTransposeAdd));
475eb7e6cafSJeremy L Thompson   CeedCallBackend(CeedGetKernel_Hip(ceed, data->module, "Weight", &data->Weight));
4762b730f8bSJeremy L Thompson   CeedCallBackend(CeedFree(&basis_kernel_path));
4772b730f8bSJeremy L Thompson   CeedCallBackend(CeedFree(&basis_kernel_source));
4787d8d0e25Snbeams 
4792b730f8bSJeremy L Thompson   CeedCallBackend(CeedBasisSetData(basis, data));
4807d8d0e25Snbeams 
4817d8d0e25Snbeams   // Register backend functions
4822b730f8bSJeremy L Thompson   CeedCallBackend(CeedSetBackendFunction(ceed, "Basis", basis, "Apply", CeedBasisApplyTensor_Hip_shared));
483*db2becc9SJeremy L Thompson   CeedCallBackend(CeedSetBackendFunction(ceed, "Basis", basis, "ApplyAdd", CeedBasisApplyAddTensor_Hip_shared));
4841dda9c1aSJeremy L Thompson   CeedCallBackend(CeedSetBackendFunction(ceed, "Basis", basis, "ApplyAtPoints", CeedBasisApplyAtPoints_Hip_shared));
485*db2becc9SJeremy L Thompson   CeedCallBackend(CeedSetBackendFunction(ceed, "Basis", basis, "ApplyAddAtPoints", CeedBasisApplyAddAtPoints_Hip_shared));
4862b730f8bSJeremy L Thompson   CeedCallBackend(CeedSetBackendFunction(ceed, "Basis", basis, "Destroy", CeedBasisDestroy_Hip_shared));
487e15f9bd0SJeremy L Thompson   return CEED_ERROR_SUCCESS;
4887d8d0e25Snbeams }
4892a86cc9dSSebastian Grimberg 
4907d8d0e25Snbeams //------------------------------------------------------------------------------
491