xref: /libCEED/backends/cuda-ref/ceed-cuda-ref.h (revision 3eb59678ecb8a0fe884fb9297a6048221d053835)
1 // Copyright (c) 2017-2025, Lawrence Livermore National Security, LLC and other CEED contributors.
2 // All Rights Reserved. See the top-level LICENSE and NOTICE files for details.
3 //
4 // SPDX-License-Identifier: BSD-2-Clause
5 //
6 // This file is part of CEED:  http://github.com/ceed
7 #pragma once
8 
9 #include <ceed.h>
10 #include <ceed/backend.h>
11 #include <ceed/jit-source/cuda/cuda-types.h>
12 #include <cublas_v2.h>
13 #include <cuda.h>
14 
15 typedef struct {
16   CeedScalar *h_array;
17   CeedScalar *h_array_borrowed;
18   CeedScalar *h_array_owned;
19   CeedScalar *d_array;
20   CeedScalar *d_array_borrowed;
21   CeedScalar *d_array_owned;
22 } CeedVector_Cuda;
23 
24 typedef struct {
25   CUmodule        module;
26   CUfunction      ApplyNoTranspose, ApplyTranspose;
27   CUfunction      ApplyUnsignedNoTranspose, ApplyUnsignedTranspose;
28   CUfunction      ApplyUnorientedNoTranspose, ApplyUnorientedTranspose;
29   CeedInt         num_nodes;
30   const CeedInt  *h_offsets;
31   const CeedInt  *h_offsets_borrowed;
32   const CeedInt  *h_offsets_owned;
33   const CeedInt  *d_offsets;
34   const CeedInt  *d_offsets_borrowed;
35   const CeedInt  *d_offsets_owned;
36   const CeedInt  *d_t_offsets;
37   const CeedInt  *d_t_indices;
38   const CeedInt  *d_l_vec_indices;
39   const bool     *h_orients;
40   const bool     *h_orients_borrowed;
41   const bool     *h_orients_owned;
42   const bool     *d_orients;
43   const bool     *d_orients_borrowed;
44   const bool     *d_orients_owned;
45   const CeedInt8 *h_curl_orients;
46   const CeedInt8 *h_curl_orients_borrowed;
47   const CeedInt8 *h_curl_orients_owned;
48   const CeedInt8 *d_curl_orients;
49   const CeedInt8 *d_curl_orients_borrowed;
50   const CeedInt8 *d_curl_orients_owned;
51   const CeedInt  *h_offsets_at_points;
52   const CeedInt  *h_offsets_at_points_borrowed;
53   const CeedInt  *h_offsets_at_points_owned;
54   const CeedInt  *d_offsets_at_points;
55   const CeedInt  *d_offsets_at_points_borrowed;
56   const CeedInt  *d_offsets_at_points_owned;
57   const CeedInt  *h_points_per_elem;
58   const CeedInt  *h_points_per_elem_borrowed;
59   const CeedInt  *h_points_per_elem_owned;
60   const CeedInt  *d_points_per_elem;
61   const CeedInt  *d_points_per_elem_borrowed;
62   const CeedInt  *d_points_per_elem_owned;
63 } CeedElemRestriction_Cuda;
64 
65 typedef struct {
66   CUmodule    module;
67   CUfunction  Interp;
68   CUfunction  Grad;
69   CUfunction  Weight;
70   CUmodule    moduleAtPoints;
71   CeedInt     num_points;
72   CUfunction  InterpAtPoints;
73   CUfunction  InterpTransposeAtPoints;
74   CUfunction  GradAtPoints;
75   CUfunction  GradTransposeAtPoints;
76   CeedScalar *d_interp_1d;
77   CeedScalar *d_grad_1d;
78   CeedScalar *d_q_weight_1d;
79   CeedScalar *d_chebyshev_interp_1d;
80   CeedInt     num_elem_at_points;
81   CeedInt    *h_points_per_elem;
82   CeedInt    *d_points_per_elem;
83 } CeedBasis_Cuda;
84 
85 typedef struct {
86   CUmodule    module;
87   CUfunction  Interp;
88   CUfunction  InterpTranspose;
89   CUfunction  Deriv;
90   CUfunction  DerivTranspose;
91   CUfunction  Weight;
92   CeedScalar *d_interp;
93   CeedScalar *d_grad;
94   CeedScalar *d_div;
95   CeedScalar *d_curl;
96   CeedScalar *d_q_weight;
97 } CeedBasisNonTensor_Cuda;
98 
99 typedef struct {
100   CUmodule    module;
101   const char *qfunction_name;
102   CUfunction  QFunction;
103   Fields_Cuda fields;
104   void       *d_c;
105 } CeedQFunction_Cuda;
106 
107 typedef struct {
108   void *h_data;
109   void *h_data_borrowed;
110   void *h_data_owned;
111   void *d_data;
112   void *d_data_borrowed;
113   void *d_data_owned;
114 } CeedQFunctionContext_Cuda;
115 
116 typedef struct {
117   CUmodule            module, module_point_block;
118   CUfunction          LinearDiagonal;
119   CUfunction          LinearPointBlock;
120   CeedElemRestriction diag_rstr, point_block_diag_rstr;
121   CeedVector          elem_diag, point_block_elem_diag;
122   CeedEvalMode       *d_eval_modes_in, *d_eval_modes_out;
123   CeedScalar         *d_identity, *d_interp_in, *d_grad_in, *d_div_in, *d_curl_in;
124   CeedScalar         *d_interp_out, *d_grad_out, *d_div_out, *d_curl_out;
125 } CeedOperatorDiag_Cuda;
126 
127 typedef struct {
128   CUmodule    module;
129   CUfunction  LinearAssemble;
130   CeedInt     block_size_x, block_size_y, elems_per_block;
131   CeedScalar *d_B_in, *d_B_out;
132 } CeedOperatorAssemble_Cuda;
133 
134 typedef struct {
135   bool                      *skip_rstr_in, *skip_rstr_out, *apply_add_basis_out;
136   uint64_t                  *input_states;  // State tracking for passive inputs
137   CeedVector                *e_vecs_in, *e_vecs_out;
138   CeedVector                *q_vecs_in, *q_vecs_out;
139   CeedInt                    num_inputs, num_outputs;
140   CeedInt                    num_active_in, num_active_out;
141   CeedInt                   *input_field_order, *output_field_order;
142   CeedSize                   max_active_e_vec_len;
143   CeedInt                    max_num_points;
144   CeedInt                   *num_points;
145   CeedVector                *qf_active_in, point_coords_elem;
146   CeedOperatorDiag_Cuda     *diag;
147   CeedOperatorAssemble_Cuda *asmb;
148 } CeedOperator_Cuda;
149 
150 CEED_INTERN int CeedGetCublasHandle_Cuda(Ceed ceed, cublasHandle_t *handle);
151 
152 CEED_INTERN int CeedVectorCreate_Cuda(CeedSize n, CeedVector vec);
153 
154 CEED_INTERN int CeedElemRestrictionCreate_Cuda(CeedMemType mem_type, CeedCopyMode copy_mode, const CeedInt *offsets, const bool *orients,
155                                                const CeedInt8 *curl_orients, CeedElemRestriction r);
156 
157 CEED_INTERN int CeedBasisCreateTensorH1_Cuda(CeedInt dim, CeedInt P_1d, CeedInt Q_1d, const CeedScalar *interp_1d, const CeedScalar *grad_1d,
158                                              const CeedScalar *q_ref_1d, const CeedScalar *q_weight_1d, CeedBasis basis);
159 CEED_INTERN int CeedBasisCreateH1_Cuda(CeedElemTopology topo, CeedInt dim, CeedInt num_nodes, CeedInt num_qpts, const CeedScalar *interp,
160                                        const CeedScalar *grad, const CeedScalar *q_ref, const CeedScalar *q_weight, CeedBasis basis);
161 CEED_INTERN int CeedBasisCreateHdiv_Cuda(CeedElemTopology topo, CeedInt dim, CeedInt num_nodes, CeedInt num_qpts, const CeedScalar *interp,
162                                          const CeedScalar *div, const CeedScalar *q_ref, const CeedScalar *q_weight, CeedBasis basis);
163 CEED_INTERN int CeedBasisCreateHcurl_Cuda(CeedElemTopology topo, CeedInt dim, CeedInt num_nodes, CeedInt num_qpts, const CeedScalar *interp,
164                                           const CeedScalar *curl, const CeedScalar *q_ref, const CeedScalar *q_weight, CeedBasis basis);
165 
166 CEED_INTERN int CeedQFunctionCreate_Cuda(CeedQFunction qf);
167 
168 CEED_INTERN int CeedQFunctionContextCreate_Cuda(CeedQFunctionContext ctx);
169 
170 CEED_INTERN int CeedOperatorCreate_Cuda(CeedOperator op);
171 CEED_INTERN int CeedOperatorCreateAtPoints_Cuda(CeedOperator op);
172