xref: /libCEED/backends/hip-ref/ceed-hip-ref.h (revision d4cc18453651bd0f94c1a2e078b2646a92dafdcc)
1*9ba83ac0SJeremy L Thompson // Copyright (c) 2017-2026, 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
7509d4af6SJeremy L Thompson #pragma once
80d0321e0SJeremy L Thompson 
949aac155SJeremy L Thompson #include <ceed.h>
100d0321e0SJeremy L Thompson #include <ceed/backend.h>
1149aac155SJeremy L Thompson #include <ceed/jit-source/hip/hip-types.h>
120d0321e0SJeremy L Thompson #include <hip/hip_runtime.h>
1305c335cbSnbeams #if (HIP_VERSION >= 50200000)
1405c335cbSnbeams #include <hipblas/hipblas.h>  // IWYU pragma: export
1505c335cbSnbeams #else
1605c335cbSnbeams #include <hipblas.h>  // IWYU pragma: export
1705c335cbSnbeams #endif
1805c335cbSnbeams 
190d0321e0SJeremy L Thompson typedef struct {
20a3b195efSJeremy L Thompson   int         has_unified_addressing;
210d0321e0SJeremy L Thompson   CeedScalar *h_array;
220d0321e0SJeremy L Thompson   CeedScalar *h_array_borrowed;
230d0321e0SJeremy L Thompson   CeedScalar *h_array_owned;
240d0321e0SJeremy L Thompson   CeedScalar *d_array;
250d0321e0SJeremy L Thompson   CeedScalar *d_array_borrowed;
260d0321e0SJeremy L Thompson   CeedScalar *d_array_owned;
270d0321e0SJeremy L Thompson } CeedVector_Hip;
280d0321e0SJeremy L Thompson 
290d0321e0SJeremy L Thompson typedef struct {
300d0321e0SJeremy L Thompson   hipModule_t     module;
31cf8cbdd6SSebastian Grimberg   hipFunction_t   ApplyNoTranspose, ApplyTranspose;
32cf8cbdd6SSebastian Grimberg   hipFunction_t   ApplyUnsignedNoTranspose, ApplyUnsignedTranspose;
33cf8cbdd6SSebastian Grimberg   hipFunction_t   ApplyUnorientedNoTranspose, ApplyUnorientedTranspose;
34437930d1SJeremy L Thompson   CeedInt         num_nodes;
35f5d1e504SJeremy L Thompson   const CeedInt  *h_offsets;
36f5d1e504SJeremy L Thompson   const CeedInt  *h_offsets_borrowed;
37f5d1e504SJeremy L Thompson   const CeedInt  *h_offsets_owned;
38081aa29dSJeremy L Thompson   const CeedInt  *d_offsets;
39f5d1e504SJeremy L Thompson   const CeedInt  *d_offsets_borrowed;
40f5d1e504SJeremy L Thompson   const CeedInt  *d_offsets_owned;
41081aa29dSJeremy L Thompson   const CeedInt  *d_t_offsets;
42081aa29dSJeremy L Thompson   const CeedInt  *d_t_indices;
43081aa29dSJeremy L Thompson   const CeedInt  *d_l_vec_indices;
44f5d1e504SJeremy L Thompson   const bool     *h_orients;
45f5d1e504SJeremy L Thompson   const bool     *h_orients_borrowed;
46f5d1e504SJeremy L Thompson   const bool     *h_orients_owned;
47f5d1e504SJeremy L Thompson   const bool     *d_orients;
48f5d1e504SJeremy L Thompson   const bool     *d_orients_borrowed;
49f5d1e504SJeremy L Thompson   const bool     *d_orients_owned;
50f5d1e504SJeremy L Thompson   const CeedInt8 *h_curl_orients;
51f5d1e504SJeremy L Thompson   const CeedInt8 *h_curl_orients_borrowed;
52f5d1e504SJeremy L Thompson   const CeedInt8 *h_curl_orients_owned;
53f5d1e504SJeremy L Thompson   const CeedInt8 *d_curl_orients;
54f5d1e504SJeremy L Thompson   const CeedInt8 *d_curl_orients_borrowed;
55f5d1e504SJeremy L Thompson   const CeedInt8 *d_curl_orients_owned;
56fe960054SJeremy L Thompson   const CeedInt  *h_offsets_at_points;
57fe960054SJeremy L Thompson   const CeedInt  *h_offsets_at_points_borrowed;
58fe960054SJeremy L Thompson   const CeedInt  *h_offsets_at_points_owned;
59fe960054SJeremy L Thompson   const CeedInt  *d_offsets_at_points;
60fe960054SJeremy L Thompson   const CeedInt  *d_offsets_at_points_borrowed;
61fe960054SJeremy L Thompson   const CeedInt  *d_offsets_at_points_owned;
620b63de31SJeremy L Thompson   const CeedInt  *h_points_per_elem;
630b63de31SJeremy L Thompson   const CeedInt  *h_points_per_elem_borrowed;
640b63de31SJeremy L Thompson   const CeedInt  *h_points_per_elem_owned;
650b63de31SJeremy L Thompson   const CeedInt  *d_points_per_elem;
660b63de31SJeremy L Thompson   const CeedInt  *d_points_per_elem_borrowed;
670b63de31SJeremy L Thompson   const CeedInt  *d_points_per_elem_owned;
680d0321e0SJeremy L Thompson } CeedElemRestriction_Hip;
690d0321e0SJeremy L Thompson 
70437930d1SJeremy L Thompson typedef struct {
71437930d1SJeremy L Thompson   hipModule_t   module;
72437930d1SJeremy L Thompson   hipFunction_t Interp;
73437930d1SJeremy L Thompson   hipFunction_t Grad;
74437930d1SJeremy L Thompson   hipFunction_t Weight;
751c21e869SJeremy L Thompson   hipModule_t   moduleAtPoints;
761c21e869SJeremy L Thompson   CeedInt       num_points;
771c21e869SJeremy L Thompson   hipFunction_t InterpAtPoints;
7881ae6159SJeremy L Thompson   hipFunction_t InterpTransposeAtPoints;
791c21e869SJeremy L Thompson   hipFunction_t GradAtPoints;
8081ae6159SJeremy L Thompson   hipFunction_t GradTransposeAtPoints;
81437930d1SJeremy L Thompson   CeedScalar   *d_interp_1d;
82437930d1SJeremy L Thompson   CeedScalar   *d_grad_1d;
83437930d1SJeremy L Thompson   CeedScalar   *d_q_weight_1d;
841c21e869SJeremy L Thompson   CeedScalar   *d_chebyshev_interp_1d;
85111870feSJeremy L Thompson   CeedInt       num_elem_at_points;
86111870feSJeremy L Thompson   CeedInt      *h_points_per_elem;
87111870feSJeremy L Thompson   CeedInt      *d_points_per_elem;
88437930d1SJeremy L Thompson } CeedBasis_Hip;
89437930d1SJeremy L Thompson 
90437930d1SJeremy L Thompson typedef struct {
91437930d1SJeremy L Thompson   hipModule_t   module;
92437930d1SJeremy L Thompson   hipFunction_t Interp;
93d075f50bSSebastian Grimberg   hipFunction_t InterpTranspose;
94d075f50bSSebastian Grimberg   hipFunction_t Deriv;
95d075f50bSSebastian Grimberg   hipFunction_t DerivTranspose;
96437930d1SJeremy L Thompson   hipFunction_t Weight;
97437930d1SJeremy L Thompson   CeedScalar   *d_interp;
98437930d1SJeremy L Thompson   CeedScalar   *d_grad;
99d075f50bSSebastian Grimberg   CeedScalar   *d_div;
100d075f50bSSebastian Grimberg   CeedScalar   *d_curl;
101437930d1SJeremy L Thompson   CeedScalar   *d_q_weight;
102437930d1SJeremy L Thompson } CeedBasisNonTensor_Hip;
103437930d1SJeremy L Thompson 
1040d0321e0SJeremy L Thompson typedef struct {
1050d0321e0SJeremy L Thompson   hipModule_t   module;
1067d023984SJeremy L Thompson   const char   *qfunction_name;
107437930d1SJeremy L Thompson   hipFunction_t QFunction;
1080d0321e0SJeremy L Thompson   Fields_Hip    fields;
1090d0321e0SJeremy L Thompson   void         *d_c;
1100d0321e0SJeremy L Thompson } CeedQFunction_Hip;
1110d0321e0SJeremy L Thompson 
1120d0321e0SJeremy L Thompson typedef struct {
1130d0321e0SJeremy L Thompson   void *h_data;
1140d0321e0SJeremy L Thompson   void *h_data_borrowed;
1150d0321e0SJeremy L Thompson   void *h_data_owned;
1160d0321e0SJeremy L Thompson   void *d_data;
1170d0321e0SJeremy L Thompson   void *d_data_borrowed;
1180d0321e0SJeremy L Thompson   void *d_data_owned;
1190d0321e0SJeremy L Thompson } CeedQFunctionContext_Hip;
1200d0321e0SJeremy L Thompson 
1210d0321e0SJeremy L Thompson typedef struct {
122cbfe683aSSebastian Grimberg   hipModule_t         module, module_point_block;
123004e4986SSebastian Grimberg   hipFunction_t       LinearDiagonal;
124004e4986SSebastian Grimberg   hipFunction_t       LinearPointBlock;
125b7453713SJeremy L Thompson   CeedElemRestriction diag_rstr, point_block_diag_rstr;
126b7453713SJeremy L Thompson   CeedVector          elem_diag, point_block_elem_diag;
127004e4986SSebastian Grimberg   CeedEvalMode       *d_eval_modes_in, *d_eval_modes_out;
128004e4986SSebastian Grimberg   CeedScalar         *d_identity, *d_interp_in, *d_grad_in, *d_div_in, *d_curl_in;
129004e4986SSebastian Grimberg   CeedScalar         *d_interp_out, *d_grad_out, *d_div_out, *d_curl_out;
1300d0321e0SJeremy L Thompson } CeedOperatorDiag_Hip;
1310d0321e0SJeremy L Thompson 
1320d0321e0SJeremy L Thompson typedef struct {
133a835093fSnbeams   hipModule_t   module;
134004e4986SSebastian Grimberg   hipFunction_t LinearAssemble;
135004e4986SSebastian Grimberg   CeedInt       block_size_x, block_size_y, elems_per_block;
136a835093fSnbeams   CeedScalar   *d_B_in, *d_B_out;
137a835093fSnbeams } CeedOperatorAssemble_Hip;
138a835093fSnbeams 
139a835093fSnbeams typedef struct {
1408bbba8cdSJeremy L Thompson   bool                     *skip_rstr_in, *skip_rstr_out, *apply_add_basis_out;
14168c01f39SZach Atkins   uint64_t                 *input_states, points_state;  // State tracking for passive inputs
142034f99fdSJeremy L Thompson   CeedVector               *e_vecs_in, *e_vecs_out;
143034f99fdSJeremy L Thompson   CeedVector               *q_vecs_in, *q_vecs_out;
144b7453713SJeremy L Thompson   CeedInt                   num_inputs, num_outputs;
145b7453713SJeremy L Thompson   CeedInt                   num_active_in, num_active_out;
146034f99fdSJeremy L Thompson   CeedInt                  *input_field_order, *output_field_order;
147034f99fdSJeremy L Thompson   CeedSize                  max_active_e_vec_len;
14867d9480aSJeremy L Thompson   CeedInt                   max_num_points;
149111870feSJeremy L Thompson   CeedInt                  *num_points;
15067d9480aSJeremy L Thompson   CeedVector               *qf_active_in, point_coords_elem;
1510d0321e0SJeremy L Thompson   CeedOperatorDiag_Hip     *diag;
152a835093fSnbeams   CeedOperatorAssemble_Hip *asmb;
1530d0321e0SJeremy L Thompson } CeedOperator_Hip;
1540d0321e0SJeremy L Thompson 
155eb7e6cafSJeremy L Thompson CEED_INTERN int CeedGetHipblasHandle_Hip(Ceed ceed, hipblasHandle_t *handle);
1560d0321e0SJeremy L Thompson 
1571f9221feSJeremy L Thompson CEED_INTERN int CeedVectorCreate_Hip(CeedSize n, CeedVector vec);
1580d0321e0SJeremy L Thompson 
159a267acd1SJeremy L Thompson CEED_INTERN int CeedElemRestrictionCreate_Hip(CeedMemType mem_type, CeedCopyMode copy_mode, const CeedInt *offsets, const bool *orients,
160a267acd1SJeremy L Thompson                                               const CeedInt8 *curl_orients, CeedElemRestriction rstr);
1610d0321e0SJeremy L Thompson 
1626574a04fSJeremy L Thompson CEED_INTERN int CeedBasisCreateTensorH1_Hip(CeedInt dim, CeedInt P_1d, CeedInt Q_1d, const CeedScalar *interp_1d, const CeedScalar *grad_1d,
1636574a04fSJeremy L Thompson                                             const CeedScalar *q_ref_1d, const CeedScalar *q_weight_1d, CeedBasis basis);
16451475c7cSJeremy L Thompson CEED_INTERN int CeedBasisCreateH1_Hip(CeedElemTopology topo, CeedInt dim, CeedInt num_nodes, CeedInt num_qpts, const CeedScalar *interp,
16551475c7cSJeremy L Thompson                                       const CeedScalar *grad, const CeedScalar *q_ref, const CeedScalar *q_weight, CeedBasis basis);
166d075f50bSSebastian Grimberg CEED_INTERN int CeedBasisCreateHdiv_Hip(CeedElemTopology topo, CeedInt dim, CeedInt num_nodes, CeedInt num_qpts, const CeedScalar *interp,
167d075f50bSSebastian Grimberg                                         const CeedScalar *div, const CeedScalar *q_ref, const CeedScalar *q_weight, CeedBasis basis);
168d075f50bSSebastian Grimberg CEED_INTERN int CeedBasisCreateHcurl_Hip(CeedElemTopology topo, CeedInt dim, CeedInt num_nodes, CeedInt num_qpts, const CeedScalar *interp,
169d075f50bSSebastian Grimberg                                          const CeedScalar *curl, const CeedScalar *q_ref, const CeedScalar *q_weight, CeedBasis basis);
1700d0321e0SJeremy L Thompson 
1710d0321e0SJeremy L Thompson CEED_INTERN int CeedQFunctionCreate_Hip(CeedQFunction qf);
1720d0321e0SJeremy L Thompson 
1730d0321e0SJeremy L Thompson CEED_INTERN int CeedQFunctionContextCreate_Hip(CeedQFunctionContext ctx);
1740d0321e0SJeremy L Thompson 
1750d0321e0SJeremy L Thompson CEED_INTERN int CeedOperatorCreate_Hip(CeedOperator op);
17667d9480aSJeremy L Thompson CEED_INTERN int CeedOperatorCreateAtPoints_Hip(CeedOperator op);
177