xref: /libCEED/backends/cuda-ref/ceed-cuda-ref.h (revision 68c01f397ddc8bbbc42c9edecbbe7eea80732430)
1d275d636SJeremy L Thompson // Copyright (c) 2017-2025, 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/cuda/cuda-types.h>
1249aac155SJeremy L Thompson #include <cublas_v2.h>
130d0321e0SJeremy L Thompson #include <cuda.h>
142b730f8bSJeremy L Thompson 
150d0321e0SJeremy L Thompson typedef struct {
160d0321e0SJeremy L Thompson   CeedScalar *h_array;
170d0321e0SJeremy L Thompson   CeedScalar *h_array_borrowed;
180d0321e0SJeremy L Thompson   CeedScalar *h_array_owned;
190d0321e0SJeremy L Thompson   CeedScalar *d_array;
200d0321e0SJeremy L Thompson   CeedScalar *d_array_borrowed;
210d0321e0SJeremy L Thompson   CeedScalar *d_array_owned;
220d0321e0SJeremy L Thompson } CeedVector_Cuda;
230d0321e0SJeremy L Thompson 
240d0321e0SJeremy L Thompson typedef struct {
250d0321e0SJeremy L Thompson   CUmodule        module;
26cf8cbdd6SSebastian Grimberg   CUfunction      ApplyNoTranspose, ApplyTranspose;
27cf8cbdd6SSebastian Grimberg   CUfunction      ApplyUnsignedNoTranspose, ApplyUnsignedTranspose;
28cf8cbdd6SSebastian Grimberg   CUfunction      ApplyUnorientedNoTranspose, ApplyUnorientedTranspose;
29437930d1SJeremy L Thompson   CeedInt         num_nodes;
30f5d1e504SJeremy L Thompson   const CeedInt  *h_offsets;
31f5d1e504SJeremy L Thompson   const CeedInt  *h_offsets_borrowed;
32f5d1e504SJeremy L Thompson   const CeedInt  *h_offsets_owned;
33081aa29dSJeremy L Thompson   const CeedInt  *d_offsets;
34f5d1e504SJeremy L Thompson   const CeedInt  *d_offsets_borrowed;
35f5d1e504SJeremy L Thompson   const CeedInt  *d_offsets_owned;
36081aa29dSJeremy L Thompson   const CeedInt  *d_t_offsets;
37081aa29dSJeremy L Thompson   const CeedInt  *d_t_indices;
38081aa29dSJeremy L Thompson   const CeedInt  *d_l_vec_indices;
39f5d1e504SJeremy L Thompson   const bool     *h_orients;
40f5d1e504SJeremy L Thompson   const bool     *h_orients_borrowed;
41f5d1e504SJeremy L Thompson   const bool     *h_orients_owned;
42f5d1e504SJeremy L Thompson   const bool     *d_orients;
43f5d1e504SJeremy L Thompson   const bool     *d_orients_borrowed;
44f5d1e504SJeremy L Thompson   const bool     *d_orients_owned;
45f5d1e504SJeremy L Thompson   const CeedInt8 *h_curl_orients;
46f5d1e504SJeremy L Thompson   const CeedInt8 *h_curl_orients_borrowed;
47f5d1e504SJeremy L Thompson   const CeedInt8 *h_curl_orients_owned;
48f5d1e504SJeremy L Thompson   const CeedInt8 *d_curl_orients;
49f5d1e504SJeremy L Thompson   const CeedInt8 *d_curl_orients_borrowed;
50f5d1e504SJeremy L Thompson   const CeedInt8 *d_curl_orients_owned;
51b20a4af9SJeremy L Thompson   const CeedInt  *h_offsets_at_points;
52b20a4af9SJeremy L Thompson   const CeedInt  *h_offsets_at_points_borrowed;
53b20a4af9SJeremy L Thompson   const CeedInt  *h_offsets_at_points_owned;
54b20a4af9SJeremy L Thompson   const CeedInt  *d_offsets_at_points;
55b20a4af9SJeremy L Thompson   const CeedInt  *d_offsets_at_points_borrowed;
56b20a4af9SJeremy L Thompson   const CeedInt  *d_offsets_at_points_owned;
570b63de31SJeremy L Thompson   const CeedInt  *h_points_per_elem;
580b63de31SJeremy L Thompson   const CeedInt  *h_points_per_elem_borrowed;
590b63de31SJeremy L Thompson   const CeedInt  *h_points_per_elem_owned;
600b63de31SJeremy L Thompson   const CeedInt  *d_points_per_elem;
610b63de31SJeremy L Thompson   const CeedInt  *d_points_per_elem_borrowed;
620b63de31SJeremy L Thompson   const CeedInt  *d_points_per_elem_owned;
630d0321e0SJeremy L Thompson } CeedElemRestriction_Cuda;
640d0321e0SJeremy L Thompson 
65437930d1SJeremy L Thompson typedef struct {
66437930d1SJeremy L Thompson   CUmodule    module;
67437930d1SJeremy L Thompson   CUfunction  Interp;
68437930d1SJeremy L Thompson   CUfunction  Grad;
69437930d1SJeremy L Thompson   CUfunction  Weight;
7034d14614SJeremy L Thompson   CUmodule    moduleAtPoints;
7134d14614SJeremy L Thompson   CeedInt     num_points;
7234d14614SJeremy L Thompson   CUfunction  InterpAtPoints;
7381ae6159SJeremy L Thompson   CUfunction  InterpTransposeAtPoints;
7434d14614SJeremy L Thompson   CUfunction  GradAtPoints;
7581ae6159SJeremy L Thompson   CUfunction  GradTransposeAtPoints;
76437930d1SJeremy L Thompson   CeedScalar *d_interp_1d;
77437930d1SJeremy L Thompson   CeedScalar *d_grad_1d;
78437930d1SJeremy L Thompson   CeedScalar *d_q_weight_1d;
7934d14614SJeremy L Thompson   CeedScalar *d_chebyshev_interp_1d;
80111870feSJeremy L Thompson   CeedInt     num_elem_at_points;
81111870feSJeremy L Thompson   CeedInt    *h_points_per_elem;
82111870feSJeremy L Thompson   CeedInt    *d_points_per_elem;
83437930d1SJeremy L Thompson } CeedBasis_Cuda;
84437930d1SJeremy L Thompson 
85437930d1SJeremy L Thompson typedef struct {
86437930d1SJeremy L Thompson   CUmodule    module;
87437930d1SJeremy L Thompson   CUfunction  Interp;
88d075f50bSSebastian Grimberg   CUfunction  InterpTranspose;
89d075f50bSSebastian Grimberg   CUfunction  Deriv;
90d075f50bSSebastian Grimberg   CUfunction  DerivTranspose;
91437930d1SJeremy L Thompson   CUfunction  Weight;
92437930d1SJeremy L Thompson   CeedScalar *d_interp;
93437930d1SJeremy L Thompson   CeedScalar *d_grad;
94d075f50bSSebastian Grimberg   CeedScalar *d_div;
95d075f50bSSebastian Grimberg   CeedScalar *d_curl;
96437930d1SJeremy L Thompson   CeedScalar *d_q_weight;
97437930d1SJeremy L Thompson } CeedBasisNonTensor_Cuda;
98437930d1SJeremy L Thompson 
990d0321e0SJeremy L Thompson typedef struct {
1000d0321e0SJeremy L Thompson   CUmodule    module;
1017d023984SJeremy L Thompson   const char *qfunction_name;
102437930d1SJeremy L Thompson   CUfunction  QFunction;
1030d0321e0SJeremy L Thompson   Fields_Cuda fields;
1040d0321e0SJeremy L Thompson   void       *d_c;
1050d0321e0SJeremy L Thompson } CeedQFunction_Cuda;
1060d0321e0SJeremy L Thompson 
1070d0321e0SJeremy L Thompson typedef struct {
1080d0321e0SJeremy L Thompson   void *h_data;
1090d0321e0SJeremy L Thompson   void *h_data_borrowed;
1100d0321e0SJeremy L Thompson   void *h_data_owned;
1110d0321e0SJeremy L Thompson   void *d_data;
1120d0321e0SJeremy L Thompson   void *d_data_borrowed;
1130d0321e0SJeremy L Thompson   void *d_data_owned;
1140d0321e0SJeremy L Thompson } CeedQFunctionContext_Cuda;
1150d0321e0SJeremy L Thompson 
1160d0321e0SJeremy L Thompson typedef struct {
117cbfe683aSSebastian Grimberg   CUmodule            module, module_point_block;
118004e4986SSebastian Grimberg   CUfunction          LinearDiagonal;
119004e4986SSebastian Grimberg   CUfunction          LinearPointBlock;
120506b1a0cSSebastian Grimberg   CeedElemRestriction diag_rstr, point_block_diag_rstr;
121ca735530SJeremy L Thompson   CeedVector          elem_diag, point_block_elem_diag;
122004e4986SSebastian Grimberg   CeedEvalMode       *d_eval_modes_in, *d_eval_modes_out;
123004e4986SSebastian Grimberg   CeedScalar         *d_identity, *d_interp_in, *d_grad_in, *d_div_in, *d_curl_in;
124004e4986SSebastian Grimberg   CeedScalar         *d_interp_out, *d_grad_out, *d_div_out, *d_curl_out;
1250d0321e0SJeremy L Thompson } CeedOperatorDiag_Cuda;
1260d0321e0SJeremy L Thompson 
1270d0321e0SJeremy L Thompson typedef struct {
128cc132f9aSnbeams   CUmodule    module;
129004e4986SSebastian Grimberg   CUfunction  LinearAssemble;
130004e4986SSebastian Grimberg   CeedInt     block_size_x, block_size_y, elems_per_block;
131cc132f9aSnbeams   CeedScalar *d_B_in, *d_B_out;
132cc132f9aSnbeams } CeedOperatorAssemble_Cuda;
133cc132f9aSnbeams 
134cc132f9aSnbeams typedef struct {
1358bbba8cdSJeremy L Thompson   bool                      *skip_rstr_in, *skip_rstr_out, *apply_add_basis_out;
136*68c01f39SZach Atkins   uint64_t                  *input_states, points_state;  // State tracking for passive inputs
137034f99fdSJeremy L Thompson   CeedVector                *e_vecs_in, *e_vecs_out;
138034f99fdSJeremy L Thompson   CeedVector                *q_vecs_in, *q_vecs_out;
139ca735530SJeremy L Thompson   CeedInt                    num_inputs, num_outputs;
140ca735530SJeremy L Thompson   CeedInt                    num_active_in, num_active_out;
141034f99fdSJeremy L Thompson   CeedInt                   *input_field_order, *output_field_order;
142034f99fdSJeremy L Thompson   CeedSize                   max_active_e_vec_len;
143756ca9e9SJeremy L Thompson   CeedInt                    max_num_points;
144111870feSJeremy L Thompson   CeedInt                   *num_points;
145756ca9e9SJeremy L Thompson   CeedVector                *qf_active_in, point_coords_elem;
1460d0321e0SJeremy L Thompson   CeedOperatorDiag_Cuda     *diag;
147cc132f9aSnbeams   CeedOperatorAssemble_Cuda *asmb;
1480d0321e0SJeremy L Thompson } CeedOperator_Cuda;
1490d0321e0SJeremy L Thompson 
150eb7e6cafSJeremy L Thompson CEED_INTERN int CeedGetCublasHandle_Cuda(Ceed ceed, cublasHandle_t *handle);
1510d0321e0SJeremy L Thompson 
1521f9221feSJeremy L Thompson CEED_INTERN int CeedVectorCreate_Cuda(CeedSize n, CeedVector vec);
1530d0321e0SJeremy L Thompson 
15458111819SJeremy L Thompson CEED_INTERN int CeedElemRestrictionCreate_Cuda(CeedMemType mem_type, CeedCopyMode copy_mode, const CeedInt *offsets, const bool *orients,
1550c73c039SSebastian Grimberg                                                const CeedInt8 *curl_orients, CeedElemRestriction r);
1560d0321e0SJeremy L Thompson 
1572b730f8bSJeremy L Thompson CEED_INTERN int CeedBasisCreateTensorH1_Cuda(CeedInt dim, CeedInt P_1d, CeedInt Q_1d, const CeedScalar *interp_1d, const CeedScalar *grad_1d,
1586574a04fSJeremy L Thompson                                              const CeedScalar *q_ref_1d, const CeedScalar *q_weight_1d, CeedBasis basis);
15951475c7cSJeremy L Thompson CEED_INTERN int CeedBasisCreateH1_Cuda(CeedElemTopology topo, CeedInt dim, CeedInt num_nodes, CeedInt num_qpts, const CeedScalar *interp,
16051475c7cSJeremy L Thompson                                        const CeedScalar *grad, const CeedScalar *q_ref, const CeedScalar *q_weight, CeedBasis basis);
161d075f50bSSebastian Grimberg CEED_INTERN int CeedBasisCreateHdiv_Cuda(CeedElemTopology topo, CeedInt dim, CeedInt num_nodes, CeedInt num_qpts, const CeedScalar *interp,
162d075f50bSSebastian Grimberg                                          const CeedScalar *div, const CeedScalar *q_ref, const CeedScalar *q_weight, CeedBasis basis);
163d075f50bSSebastian Grimberg CEED_INTERN int CeedBasisCreateHcurl_Cuda(CeedElemTopology topo, CeedInt dim, CeedInt num_nodes, CeedInt num_qpts, const CeedScalar *interp,
164d075f50bSSebastian Grimberg                                           const CeedScalar *curl, const CeedScalar *q_ref, const CeedScalar *q_weight, CeedBasis basis);
1650d0321e0SJeremy L Thompson 
1660d0321e0SJeremy L Thompson CEED_INTERN int CeedQFunctionCreate_Cuda(CeedQFunction qf);
1670d0321e0SJeremy L Thompson 
1680d0321e0SJeremy L Thompson CEED_INTERN int CeedQFunctionContextCreate_Cuda(CeedQFunctionContext ctx);
1690d0321e0SJeremy L Thompson 
1700d0321e0SJeremy L Thompson CEED_INTERN int CeedOperatorCreate_Cuda(CeedOperator op);
171756ca9e9SJeremy L Thompson CEED_INTERN int CeedOperatorCreateAtPoints_Cuda(CeedOperator op);
172