xref: /libCEED/rust/libceed-sys/c-src/backends/magma/ceed-magma-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.
37f5b9731SStan Tomov //
43d8e8822SJeremy L Thompson // SPDX-License-Identifier: BSD-2-Clause
57f5b9731SStan Tomov //
63d8e8822SJeremy L Thompson // This file is part of CEED:  http://github.com/ceed
77f5b9731SStan Tomov 
849aac155SJeremy L Thompson #include <ceed.h>
9ec3da8bcSJed Brown #include <ceed/backend.h>
10f6af633fSnbeams #include <ceed/jit-tools.h>
11f6af633fSnbeams #include <string.h>
122b730f8bSJeremy L Thompson 
13e5f091ebSnbeams #ifdef CEED_MAGMA_USE_HIP
14f6af633fSnbeams #include "../hip/ceed-hip-common.h"
15f6af633fSnbeams #include "../hip/ceed-hip-compile.h"
16f6af633fSnbeams #else
17f6af633fSnbeams #include "../cuda/ceed-cuda-common.h"
18f6af633fSnbeams #include "../cuda/ceed-cuda-compile.h"
19f6af633fSnbeams #endif
2000fb7a04SSebastian Grimberg #include "ceed-magma-common.h"
2100fb7a04SSebastian Grimberg #include "ceed-magma.h"
227f5b9731SStan Tomov 
23940a72f1SSebastian Grimberg #include "ceed-magma-gemm-nontensor.h"
24940a72f1SSebastian Grimberg #include "ceed-magma-gemm-selector.h"
25940a72f1SSebastian Grimberg 
26940a72f1SSebastian Grimberg //------------------------------------------------------------------------------
27940a72f1SSebastian Grimberg // Basis apply - tensor
28940a72f1SSebastian Grimberg //------------------------------------------------------------------------------
29*db2becc9SJeremy L Thompson static int CeedBasisApplyCore_Magma(CeedBasis basis, bool apply_add, CeedInt num_elem, CeedTransposeMode t_mode, CeedEvalMode e_mode, CeedVector u,
30*db2becc9SJeremy L Thompson                                     CeedVector v) {
317f5b9731SStan Tomov   Ceed              ceed;
32e0582403Sabdelfattah83   Ceed_Magma       *data;
33940a72f1SSebastian Grimberg   CeedInt           dim, num_comp, num_nodes, P_1d, Q_1d, P, Q;
34940a72f1SSebastian Grimberg   const CeedScalar *d_u;
35940a72f1SSebastian Grimberg   CeedScalar       *d_v;
3638293ee6SJeremy L Thompson   CeedBasis_Magma  *impl;
3738293ee6SJeremy L Thompson 
3838293ee6SJeremy L Thompson   CeedCallBackend(CeedBasisGetCeed(basis, &ceed));
39940a72f1SSebastian Grimberg   CeedCallBackend(CeedGetData(ceed, &data));
40940a72f1SSebastian Grimberg   CeedCallBackend(CeedBasisGetData(basis, &impl));
4138293ee6SJeremy L Thompson   CeedCallBackend(CeedBasisGetDimension(basis, &dim));
4238293ee6SJeremy L Thompson   CeedCallBackend(CeedBasisGetNumComponents(basis, &num_comp));
43940a72f1SSebastian Grimberg   CeedCallBackend(CeedBasisGetNumNodes(basis, &num_nodes));
4438293ee6SJeremy L Thompson   CeedCallBackend(CeedBasisGetNumNodes1D(basis, &P_1d));
4538293ee6SJeremy L Thompson   CeedCallBackend(CeedBasisGetNumQuadraturePoints1D(basis, &Q_1d));
46940a72f1SSebastian Grimberg   P = P_1d;
47940a72f1SSebastian Grimberg   Q = Q_1d;
4838293ee6SJeremy L Thompson   if (t_mode == CEED_TRANSPOSE) {
4938293ee6SJeremy L Thompson     P = Q_1d;
5038293ee6SJeremy L Thompson     Q = P_1d;
517f5b9731SStan Tomov   }
527f5b9731SStan Tomov 
53940a72f1SSebastian Grimberg   // Read vectors
54940a72f1SSebastian Grimberg   if (u != CEED_VECTOR_NONE) CeedCallBackend(CeedVectorGetArrayRead(u, CEED_MEM_DEVICE, &d_u));
55940a72f1SSebastian Grimberg   else CeedCheck(e_mode == CEED_EVAL_WEIGHT, ceed, CEED_ERROR_BACKEND, "An input vector is required for this CeedEvalMode");
56*db2becc9SJeremy L Thompson   if (apply_add) CeedCallBackend(CeedVectorGetArray(v, CEED_MEM_DEVICE, &d_v));
57*db2becc9SJeremy L Thompson   else CeedCallBackend(CeedVectorGetArrayWrite(v, CEED_MEM_DEVICE, &d_v));
58940a72f1SSebastian Grimberg 
59940a72f1SSebastian Grimberg   // Apply basis operation
60940a72f1SSebastian Grimberg   switch (e_mode) {
61940a72f1SSebastian Grimberg     case CEED_EVAL_INTERP: {
627f5b9731SStan Tomov       // Define element sizes for dofs/quad
6338293ee6SJeremy L Thompson       CeedInt elem_qpts_size = CeedIntPow(Q_1d, dim);
6438293ee6SJeremy L Thompson       CeedInt elem_dofs_size = CeedIntPow(P_1d, dim);
657f5b9731SStan Tomov 
667f5b9731SStan Tomov       // E-vector ordering -------------- Q-vector ordering
67868539c2SNatalie Beams       //  component                        component
68868539c2SNatalie Beams       //    elem                             elem
697f5b9731SStan Tomov       //       node                            node
707f5b9731SStan Tomov 
717f5b9731SStan Tomov       // ---  Define strides for NOTRANSPOSE mode: ---
72940a72f1SSebastian Grimberg       // Input (d_u) is E-vector, output (d_v) is Q-vector
737f5b9731SStan Tomov 
747f5b9731SStan Tomov       // Element strides
7538293ee6SJeremy L Thompson       CeedInt u_elem_stride = elem_dofs_size;
7638293ee6SJeremy L Thompson       CeedInt v_elem_stride = elem_qpts_size;
777f5b9731SStan Tomov       // Component strides
7838293ee6SJeremy L Thompson       CeedInt u_comp_stride = num_elem * elem_dofs_size;
7938293ee6SJeremy L Thompson       CeedInt v_comp_stride = num_elem * elem_qpts_size;
8038293ee6SJeremy L Thompson       if (t_mode == CEED_TRANSPOSE) {
81940a72f1SSebastian Grimberg         // Input (d_u) is Q-vector, output (d_v) is E-vector
827f5b9731SStan Tomov         // Element strides
8338293ee6SJeremy L Thompson         v_elem_stride = elem_dofs_size;
8438293ee6SJeremy L Thompson         u_elem_stride = elem_qpts_size;
857f5b9731SStan Tomov         // Component strides
8638293ee6SJeremy L Thompson         v_comp_stride = num_elem * elem_dofs_size;
8738293ee6SJeremy L Thompson         u_comp_stride = num_elem * elem_qpts_size;
887f5b9731SStan Tomov       }
8938293ee6SJeremy L Thompson       CeedInt num_threads = 1;
9038293ee6SJeremy L Thompson       CeedInt num_t_col   = 1;
9138293ee6SJeremy L Thompson       CeedInt shared_mem  = 0;
9238293ee6SJeremy L Thompson       CeedInt max_P_Q     = CeedIntMax(P, Q);
93f6af633fSnbeams 
94f6af633fSnbeams       switch (dim) {
95f6af633fSnbeams         case 1:
9638293ee6SJeremy L Thompson           num_threads = max_P_Q;
9738293ee6SJeremy L Thompson           num_t_col   = MAGMA_BASIS_NTCOL(num_threads, MAGMA_MAXTHREADS_1D);
9838293ee6SJeremy L Thompson           shared_mem += sizeof(CeedScalar) * num_t_col * (num_comp * (1 * P + 1 * Q));
9938293ee6SJeremy L Thompson           shared_mem += sizeof(CeedScalar) * (P * Q);
100f6af633fSnbeams           break;
101f6af633fSnbeams         case 2:
10238293ee6SJeremy L Thompson           num_threads = max_P_Q;
10338293ee6SJeremy L Thompson           num_t_col   = MAGMA_BASIS_NTCOL(num_threads, MAGMA_MAXTHREADS_2D);
10438293ee6SJeremy L Thompson           shared_mem += P * Q * sizeof(CeedScalar);  // for sT
105940a72f1SSebastian Grimberg           // for reforming rU we need P x P, and for the intermediate output we need P x Q
106940a72f1SSebastian Grimberg           shared_mem += num_t_col * (P * max_P_Q * sizeof(CeedScalar));
107f6af633fSnbeams           break;
108f6af633fSnbeams         case 3:
10938293ee6SJeremy L Thompson           num_threads = max_P_Q * max_P_Q;
11038293ee6SJeremy L Thompson           num_t_col   = MAGMA_BASIS_NTCOL(num_threads, MAGMA_MAXTHREADS_3D);
11138293ee6SJeremy L Thompson           shared_mem += sizeof(CeedScalar) * (P * Q);  // for sT
112940a72f1SSebastian Grimberg           // rU needs P^2 x P, the intermediate output needs max(P^2 x Q, P x Q^2)
113940a72f1SSebastian Grimberg           shared_mem += sizeof(CeedScalar) * num_t_col * (CeedIntMax(P * P * max_P_Q, P * Q * Q));
114940a72f1SSebastian Grimberg           break;
115f6af633fSnbeams       }
116940a72f1SSebastian Grimberg       CeedInt grid   = CeedDivUpInt(num_elem, num_t_col);
117940a72f1SSebastian Grimberg       void   *args[] = {&impl->d_interp_1d, &d_u, &u_elem_stride, &u_comp_stride, &d_v, &v_elem_stride, &v_comp_stride, &num_elem};
118f6af633fSnbeams 
11938293ee6SJeremy L Thompson       if (t_mode == CEED_TRANSPOSE) {
120*db2becc9SJeremy L Thompson         CeedCallBackend(CeedRunKernelDimSharedMagma(ceed, apply_add ? impl->InterpTransposeAdd : impl->InterpTranspose, grid, num_threads, num_t_col,
121*db2becc9SJeremy L Thompson                                                     1, shared_mem, args));
122f6af633fSnbeams       } else {
123940a72f1SSebastian Grimberg         CeedCallBackend(CeedRunKernelDimSharedMagma(ceed, impl->Interp, grid, num_threads, num_t_col, 1, shared_mem, args));
124f6af633fSnbeams       }
1252b730f8bSJeremy L Thompson     } break;
1263513a710Sjeremylt     case CEED_EVAL_GRAD: {
1277f5b9731SStan Tomov       // Define element sizes for dofs/quad
12838293ee6SJeremy L Thompson       CeedInt elem_qpts_size = CeedIntPow(Q_1d, dim);
12938293ee6SJeremy L Thompson       CeedInt elem_dofs_size = CeedIntPow(P_1d, dim);
1307f5b9731SStan Tomov 
131940a72f1SSebastian Grimberg       // In CEED_NOTRANSPOSE mode:
132940a72f1SSebastian Grimberg       // d_u is (P^dim x nc), column-major layout (nc = num_comp)
133940a72f1SSebastian Grimberg       // d_v is (Q^dim x nc x dim), column-major layout (nc = num_comp)
134940a72f1SSebastian Grimberg       // In CEED_TRANSPOSE mode, the sizes of d_u and d_v are switched.
135940a72f1SSebastian Grimberg 
1367f5b9731SStan Tomov       // E-vector ordering -------------- Q-vector ordering
1377f5b9731SStan Tomov       //                                  dim
138868539c2SNatalie Beams       //  component                        component
139868539c2SNatalie Beams       //    elem                              elem
1407f5b9731SStan Tomov       //       node                            node
1417f5b9731SStan Tomov 
1427f5b9731SStan Tomov       // ---  Define strides for NOTRANSPOSE mode: ---
143940a72f1SSebastian Grimberg       // Input (d_u) is E-vector, output (d_v) is Q-vector
1447f5b9731SStan Tomov 
1457f5b9731SStan Tomov       // Element strides
14638293ee6SJeremy L Thompson       CeedInt u_elem_stride = elem_dofs_size;
14738293ee6SJeremy L Thompson       CeedInt v_elem_stride = elem_qpts_size;
1487f5b9731SStan Tomov       // Component strides
14938293ee6SJeremy L Thompson       CeedInt u_comp_stride = num_elem * elem_dofs_size;
15038293ee6SJeremy L Thompson       CeedInt v_comp_stride = num_elem * elem_qpts_size;
1517f5b9731SStan Tomov       // Dimension strides
15238293ee6SJeremy L Thompson       CeedInt u_dim_stride = 0;
15338293ee6SJeremy L Thompson       CeedInt v_dim_stride = num_elem * elem_qpts_size * num_comp;
15438293ee6SJeremy L Thompson       if (t_mode == CEED_TRANSPOSE) {
155940a72f1SSebastian Grimberg         // Input (d_u) is Q-vector, output (d_v) is E-vector
1567f5b9731SStan Tomov         // Element strides
15738293ee6SJeremy L Thompson         v_elem_stride = elem_dofs_size;
15838293ee6SJeremy L Thompson         u_elem_stride = elem_qpts_size;
1597f5b9731SStan Tomov         // Component strides
16038293ee6SJeremy L Thompson         v_comp_stride = num_elem * elem_dofs_size;
16138293ee6SJeremy L Thompson         u_comp_stride = num_elem * elem_qpts_size;
1627f5b9731SStan Tomov         // Dimension strides
16338293ee6SJeremy L Thompson         v_dim_stride = 0;
16438293ee6SJeremy L Thompson         u_dim_stride = num_elem * elem_qpts_size * num_comp;
1657f5b9731SStan Tomov       }
16638293ee6SJeremy L Thompson       CeedInt num_threads = 1;
16738293ee6SJeremy L Thompson       CeedInt num_t_col   = 1;
16838293ee6SJeremy L Thompson       CeedInt shared_mem  = 0;
16938293ee6SJeremy L Thompson       CeedInt max_P_Q     = CeedIntMax(P, Q);
170f6af633fSnbeams 
171f6af633fSnbeams       switch (dim) {
172f6af633fSnbeams         case 1:
17338293ee6SJeremy L Thompson           num_threads = max_P_Q;
17438293ee6SJeremy L Thompson           num_t_col   = MAGMA_BASIS_NTCOL(num_threads, MAGMA_MAXTHREADS_1D);
17538293ee6SJeremy L Thompson           shared_mem += sizeof(CeedScalar) * num_t_col * (num_comp * (1 * P + 1 * Q));
17638293ee6SJeremy L Thompson           shared_mem += sizeof(CeedScalar) * (P * Q);
177f6af633fSnbeams           break;
178f6af633fSnbeams         case 2:
17938293ee6SJeremy L Thompson           num_threads = max_P_Q;
18038293ee6SJeremy L Thompson           num_t_col   = MAGMA_BASIS_NTCOL(num_threads, MAGMA_MAXTHREADS_2D);
18138293ee6SJeremy L Thompson           shared_mem += sizeof(CeedScalar) * 2 * P * Q;  // for sTinterp and sTgrad
182940a72f1SSebastian Grimberg           // for reforming rU we need P x P, and for the intermediate output we need P x Q
183940a72f1SSebastian Grimberg           shared_mem += sizeof(CeedScalar) * num_t_col * (P * max_P_Q);
184f6af633fSnbeams           break;
185f6af633fSnbeams         case 3:
18638293ee6SJeremy L Thompson           num_threads = max_P_Q * max_P_Q;
18738293ee6SJeremy L Thompson           num_t_col   = MAGMA_BASIS_NTCOL(num_threads, MAGMA_MAXTHREADS_3D);
18838293ee6SJeremy L Thompson           shared_mem += sizeof(CeedScalar) * 2 * P * Q;  // for sTinterp and sTgrad
189940a72f1SSebastian Grimberg           // rU needs P^2 x P, the intermediate outputs need (P^2 x Q + P x Q^2)
190940a72f1SSebastian Grimberg           shared_mem += sizeof(CeedScalar) * num_t_col * CeedIntMax(P * P * P, (P * P * Q) + (P * Q * Q));
191940a72f1SSebastian Grimberg           break;
192f6af633fSnbeams       }
193940a72f1SSebastian Grimberg       CeedInt grid   = CeedDivUpInt(num_elem, num_t_col);
194940a72f1SSebastian Grimberg       void   *args[] = {&impl->d_interp_1d, &impl->d_grad_1d, &d_u,          &u_elem_stride, &u_comp_stride, &u_dim_stride, &d_v,
19538293ee6SJeremy L Thompson                         &v_elem_stride,     &v_comp_stride,   &v_dim_stride, &num_elem};
196f6af633fSnbeams 
19738293ee6SJeremy L Thompson       if (t_mode == CEED_TRANSPOSE) {
198*db2becc9SJeremy L Thompson         CeedCallBackend(CeedRunKernelDimSharedMagma(ceed, apply_add ? impl->GradTransposeAdd : impl->GradTranspose, grid, num_threads, num_t_col, 1,
199*db2becc9SJeremy L Thompson                                                     shared_mem, args));
200f6af633fSnbeams       } else {
201940a72f1SSebastian Grimberg         CeedCallBackend(CeedRunKernelDimSharedMagma(ceed, impl->Grad, grid, num_threads, num_t_col, 1, shared_mem, args));
202f6af633fSnbeams       }
2032b730f8bSJeremy L Thompson     } break;
2043513a710Sjeremylt     case CEED_EVAL_WEIGHT: {
205940a72f1SSebastian Grimberg       CeedCheck(t_mode != CEED_TRANSPOSE, ceed, CEED_ERROR_BACKEND, "CEED_EVAL_WEIGHT incompatible with CEED_TRANSPOSE");
206097cc795SJames Wright       CeedCheck(impl->d_q_weight_1d, ceed, CEED_ERROR_BACKEND, "%s not supported; q_weight_1d not set", CeedEvalModes[e_mode]);
20738293ee6SJeremy L Thompson       CeedInt elem_dofs_size = CeedIntPow(Q, dim);
20838293ee6SJeremy L Thompson       CeedInt num_threads    = 1;
20938293ee6SJeremy L Thompson       CeedInt num_t_col      = 1;
21038293ee6SJeremy L Thompson       CeedInt shared_mem     = 0;
211f6af633fSnbeams 
212f6af633fSnbeams       switch (dim) {
213f6af633fSnbeams         case 1:
21438293ee6SJeremy L Thompson           num_threads = Q;
21538293ee6SJeremy L Thompson           num_t_col   = MAGMA_BASIS_NTCOL(num_threads, MAGMA_MAXTHREADS_1D);
21638293ee6SJeremy L Thompson           shared_mem += sizeof(CeedScalar) * Q;              // for d_q_weight_1d
21738293ee6SJeremy L Thompson           shared_mem += sizeof(CeedScalar) * num_t_col * Q;  // for output
218f6af633fSnbeams           break;
219f6af633fSnbeams         case 2:
22038293ee6SJeremy L Thompson           num_threads = Q;
22138293ee6SJeremy L Thompson           num_t_col   = MAGMA_BASIS_NTCOL(num_threads, MAGMA_MAXTHREADS_2D);
22238293ee6SJeremy L Thompson           shared_mem += sizeof(CeedScalar) * Q;  // for d_q_weight_1d
223f6af633fSnbeams           break;
224f6af633fSnbeams         case 3:
22538293ee6SJeremy L Thompson           num_threads = Q * Q;
22638293ee6SJeremy L Thompson           num_t_col   = MAGMA_BASIS_NTCOL(num_threads, MAGMA_MAXTHREADS_3D);
22738293ee6SJeremy L Thompson           shared_mem += sizeof(CeedScalar) * Q;  // for d_q_weight_1d
228940a72f1SSebastian Grimberg           break;
229f6af633fSnbeams       }
230940a72f1SSebastian Grimberg       CeedInt grid   = CeedDivUpInt(num_elem, num_t_col);
231940a72f1SSebastian Grimberg       void   *args[] = {&impl->d_q_weight_1d, &d_v, &elem_dofs_size, &num_elem};
232f6af633fSnbeams 
233940a72f1SSebastian Grimberg       CeedCallBackend(CeedRunKernelDimSharedMagma(ceed, impl->Weight, grid, num_threads, num_t_col, 1, shared_mem, args));
2342b730f8bSJeremy L Thompson     } break;
2353513a710Sjeremylt     // LCOV_EXCL_START
2363513a710Sjeremylt     case CEED_EVAL_DIV:
2373513a710Sjeremylt     case CEED_EVAL_CURL:
238bcbe1c99SJeremy L Thompson       return CeedError(ceed, CEED_ERROR_BACKEND, "%s not supported", CeedEvalModes[e_mode]);
2393513a710Sjeremylt     case CEED_EVAL_NONE:
2402b730f8bSJeremy L Thompson       return CeedError(ceed, CEED_ERROR_BACKEND, "CEED_EVAL_NONE does not make sense in this context");
2413513a710Sjeremylt       // LCOV_EXCL_STOP
2423513a710Sjeremylt   }
2437f5b9731SStan Tomov 
244940a72f1SSebastian Grimberg   // Must sync to ensure completeness
245e0582403Sabdelfattah83   ceed_magma_queue_sync(data->queue);
246e0582403Sabdelfattah83 
247940a72f1SSebastian Grimberg   // Restore vectors
24838293ee6SJeremy L Thompson   if (e_mode != CEED_EVAL_WEIGHT) {
249940a72f1SSebastian Grimberg     CeedCallBackend(CeedVectorRestoreArrayRead(u, &d_u));
2507f5b9731SStan Tomov   }
251940a72f1SSebastian Grimberg   CeedCallBackend(CeedVectorRestoreArray(v, &d_v));
252e15f9bd0SJeremy L Thompson   return CEED_ERROR_SUCCESS;
2537f5b9731SStan Tomov }
2547f5b9731SStan Tomov 
255*db2becc9SJeremy L Thompson static int CeedBasisApply_Magma(CeedBasis basis, CeedInt num_elem, CeedTransposeMode t_mode, CeedEvalMode e_mode, CeedVector u, CeedVector v) {
256*db2becc9SJeremy L Thompson   CeedCallBackend(CeedBasisApplyCore_Magma(basis, false, num_elem, t_mode, e_mode, u, v));
257*db2becc9SJeremy L Thompson   return CEED_ERROR_SUCCESS;
258*db2becc9SJeremy L Thompson }
259*db2becc9SJeremy L Thompson 
260*db2becc9SJeremy L Thompson static int CeedBasisApplyAdd_Magma(CeedBasis basis, CeedInt num_elem, CeedTransposeMode t_mode, CeedEvalMode e_mode, CeedVector u, CeedVector v) {
261*db2becc9SJeremy L Thompson   CeedCallBackend(CeedBasisApplyCore_Magma(basis, true, num_elem, t_mode, e_mode, u, v));
262*db2becc9SJeremy L Thompson   return CEED_ERROR_SUCCESS;
263*db2becc9SJeremy L Thompson }
264*db2becc9SJeremy L Thompson 
265940a72f1SSebastian Grimberg //------------------------------------------------------------------------------
26614950a8eSJeremy L Thompson // Basis apply - tensor AtPoints
26714950a8eSJeremy L Thompson //------------------------------------------------------------------------------
26814950a8eSJeremy L Thompson int CeedBasisApplyAtPoints_Magma(CeedBasis basis, const CeedInt num_elem, const CeedInt *num_points, CeedTransposeMode t_mode, CeedEvalMode eval_mode,
26914950a8eSJeremy L Thompson                                  CeedVector x_ref, CeedVector u, CeedVector v) {
27014950a8eSJeremy L Thompson   return CeedError(CeedBasisReturnCeed(basis), CEED_ERROR_BACKEND, "Backend does not implement CeedBasisApplyAtPoints");
27114950a8eSJeremy L Thompson }
27214950a8eSJeremy L Thompson 
27314950a8eSJeremy L Thompson //------------------------------------------------------------------------------
274940a72f1SSebastian Grimberg // Basis apply - non-tensor
275940a72f1SSebastian Grimberg //------------------------------------------------------------------------------
276*db2becc9SJeremy L Thompson static int CeedBasisApplyNonTensorCore_Magma(CeedBasis basis, bool apply_add, CeedInt num_elem, CeedTransposeMode t_mode, CeedEvalMode e_mode,
277*db2becc9SJeremy L Thompson                                              CeedVector u, CeedVector v) {
278868539c2SNatalie Beams   Ceed                      ceed;
279e0582403Sabdelfattah83   Ceed_Magma               *data;
2807251047cSSebastian Grimberg   CeedInt                   num_comp, num_nodes, num_qpts, P, Q, N;
2817251047cSSebastian Grimberg   const CeedScalar         *d_u;
282940a72f1SSebastian Grimberg   CeedScalar               *d_v;
28338293ee6SJeremy L Thompson   CeedBasisNonTensor_Magma *impl;
28438293ee6SJeremy L Thompson 
28538293ee6SJeremy L Thompson   CeedCallBackend(CeedBasisGetCeed(basis, &ceed));
28638293ee6SJeremy L Thompson   CeedCallBackend(CeedGetData(ceed, &data));
287940a72f1SSebastian Grimberg   CeedCallBackend(CeedBasisGetData(basis, &impl));
28838293ee6SJeremy L Thompson   CeedCallBackend(CeedBasisGetNumComponents(basis, &num_comp));
289940a72f1SSebastian Grimberg   CeedCallBackend(CeedBasisGetNumNodes(basis, &num_nodes));
29038293ee6SJeremy L Thompson   CeedCallBackend(CeedBasisGetNumQuadraturePoints(basis, &num_qpts));
291940a72f1SSebastian Grimberg   P = num_nodes;
292940a72f1SSebastian Grimberg   Q = num_qpts;
293940a72f1SSebastian Grimberg   N = num_elem * num_comp;
29438293ee6SJeremy L Thompson 
295940a72f1SSebastian Grimberg   // Read vectors
296940a72f1SSebastian Grimberg   if (u != CEED_VECTOR_NONE) CeedCallBackend(CeedVectorGetArrayRead(u, CEED_MEM_DEVICE, &d_u));
29738293ee6SJeremy L Thompson   else CeedCheck(e_mode == CEED_EVAL_WEIGHT, ceed, CEED_ERROR_BACKEND, "An input vector is required for this CeedEvalMode");
298*db2becc9SJeremy L Thompson   if (apply_add) CeedCallBackend(CeedVectorGetArray(v, CEED_MEM_DEVICE, &d_v));
299*db2becc9SJeremy L Thompson   else CeedCallBackend(CeedVectorGetArrayWrite(v, CEED_MEM_DEVICE, &d_v));
300868539c2SNatalie Beams 
3017251047cSSebastian Grimberg   // Compile kernels for N as needed
3027251047cSSebastian Grimberg   CeedInt iN = 0;
3037251047cSSebastian Grimberg   if (P <= MAGMA_NONTENSOR_CUSTOM_KERNEL_MAX_P && Q <= MAGMA_NONTENSOR_CUSTOM_KERNEL_MAX_Q && (e_mode != CEED_EVAL_WEIGHT || !impl->Weight)) {
304940a72f1SSebastian Grimberg     CeedInt n_array[MAGMA_NONTENSOR_KERNEL_INSTANCES] = {MAGMA_NONTENSOR_KERNEL_N_VALUES};
3057251047cSSebastian Grimberg     CeedInt diff                                      = abs(n_array[iN] - N), idiff;
30638293ee6SJeremy L Thompson 
307023b8a51Sabdelfattah83     for (CeedInt in = iN + 1; in < MAGMA_NONTENSOR_KERNEL_INSTANCES; in++) {
308940a72f1SSebastian Grimberg       idiff = abs(n_array[in] - N);
309023b8a51Sabdelfattah83       if (idiff < diff) {
310023b8a51Sabdelfattah83         iN   = in;
311023b8a51Sabdelfattah83         diff = idiff;
312868539c2SNatalie Beams       }
31380a9ef05SNatalie Beams     }
31480a9ef05SNatalie Beams 
315940a72f1SSebastian Grimberg     if (!impl->NB_interp[iN]) {
3169d15e85bSSebastian Grimberg       CeedFESpace fe_space;
3179d15e85bSSebastian Grimberg       CeedInt     q_comp_interp, q_comp_deriv;
318940a72f1SSebastian Grimberg       Ceed        ceed_delegate;
31922070f95SJeremy L Thompson       char       *basis_kernel_source;
32022070f95SJeremy L Thompson       const char *basis_kernel_path, *weight_kernel_path;
321509d4af6SJeremy L Thompson       char      **file_paths     = NULL;
322509d4af6SJeremy L Thompson       CeedInt     num_file_paths = 0;
323940a72f1SSebastian Grimberg       magma_int_t arch           = magma_getdevice_arch();
32480a9ef05SNatalie Beams 
325940a72f1SSebastian Grimberg       // Tuning parameters for NB
3269d15e85bSSebastian Grimberg       CeedCallBackend(CeedBasisGetFESpace(basis, &fe_space));
3279d15e85bSSebastian Grimberg       CeedCallBackend(CeedBasisGetNumQuadratureComponents(basis, CEED_EVAL_INTERP, &q_comp_interp));
3289d15e85bSSebastian Grimberg       switch (fe_space) {
3299d15e85bSSebastian Grimberg         case CEED_FE_SPACE_H1:
3309d15e85bSSebastian Grimberg           CeedCallBackend(CeedBasisGetNumQuadratureComponents(basis, CEED_EVAL_GRAD, &q_comp_deriv));
3319d15e85bSSebastian Grimberg           break;
3329d15e85bSSebastian Grimberg         case CEED_FE_SPACE_HDIV:
3339d15e85bSSebastian Grimberg           CeedCallBackend(CeedBasisGetNumQuadratureComponents(basis, CEED_EVAL_DIV, &q_comp_deriv));
3349d15e85bSSebastian Grimberg           break;
3359d15e85bSSebastian Grimberg         case CEED_FE_SPACE_HCURL:
3369d15e85bSSebastian Grimberg           CeedCallBackend(CeedBasisGetNumQuadratureComponents(basis, CEED_EVAL_CURL, &q_comp_deriv));
3379d15e85bSSebastian Grimberg           break;
3389d15e85bSSebastian Grimberg       }
3399d15e85bSSebastian Grimberg       impl->NB_interp[iN]   = nontensor_rtc_get_nb(arch, 'n', q_comp_interp, P, Q, n_array[iN]);
3409d15e85bSSebastian Grimberg       impl->NB_interp_t[iN] = nontensor_rtc_get_nb(arch, 't', q_comp_interp, P, Q, n_array[iN]);
3419d15e85bSSebastian Grimberg       impl->NB_deriv[iN]    = nontensor_rtc_get_nb(arch, 'n', q_comp_deriv, P, Q, n_array[iN]);
3429d15e85bSSebastian Grimberg       impl->NB_deriv_t[iN]  = nontensor_rtc_get_nb(arch, 't', q_comp_deriv, P, Q, n_array[iN]);
343023b8a51Sabdelfattah83 
344940a72f1SSebastian Grimberg       // The RTC compilation code expects a Ceed with the common Ceed_Cuda or Ceed_Hip data
345940a72f1SSebastian Grimberg       CeedCallBackend(CeedGetDelegate(ceed, &ceed_delegate));
346023b8a51Sabdelfattah83 
347940a72f1SSebastian Grimberg       // Compile kernels
3489d15e85bSSebastian Grimberg       CeedCallBackend(CeedGetJitAbsolutePath(ceed, "ceed/jit-source/magma/magma-basis-interp-deriv-nontensor.h", &basis_kernel_path));
349940a72f1SSebastian Grimberg       CeedDebug256(ceed, CEED_DEBUG_COLOR_SUCCESS, "----- Loading Basis Kernel Source -----\n");
350509d4af6SJeremy L Thompson       CeedCallBackend(CeedLoadSourceAndInitializeBuffer(ceed, basis_kernel_path, &num_file_paths, &file_paths, &basis_kernel_source));
3517251047cSSebastian Grimberg       if (!impl->Weight) {
3527251047cSSebastian Grimberg         CeedCallBackend(CeedGetJitAbsolutePath(ceed, "ceed/jit-source/magma/magma-basis-weight-nontensor.h", &weight_kernel_path));
353509d4af6SJeremy L Thompson         CeedCallBackend(CeedLoadSourceToInitializedBuffer(ceed, weight_kernel_path, &num_file_paths, &file_paths, &basis_kernel_source));
3547251047cSSebastian Grimberg       }
355940a72f1SSebastian Grimberg       CeedDebug256(ceed, CEED_DEBUG_COLOR_SUCCESS, "----- Loading Basis Kernel Source Complete! -----\n");
3567251047cSSebastian Grimberg       CeedCallBackend(CeedCompileMagma(ceed_delegate, basis_kernel_source, &impl->module[iN], 8, "BASIS_Q_COMP_INTERP", q_comp_interp,
3579d15e85bSSebastian Grimberg                                        "BASIS_Q_COMP_DERIV", q_comp_deriv, "BASIS_P", P, "BASIS_Q", Q, "BASIS_NB_INTERP_N", impl->NB_interp[iN],
3589d15e85bSSebastian Grimberg                                        "BASIS_NB_INTERP_T", impl->NB_interp_t[iN], "BASIS_NB_DERIV_N", impl->NB_deriv[iN], "BASIS_NB_DERIV_T",
3599d15e85bSSebastian Grimberg                                        impl->NB_deriv_t[iN]));
3607251047cSSebastian Grimberg       CeedCallBackend(CeedGetKernelMagma(ceed, impl->module[iN], "magma_interp_nontensor_n", &impl->Interp[iN]));
3617251047cSSebastian Grimberg       CeedCallBackend(CeedGetKernelMagma(ceed, impl->module[iN], "magma_interp_nontensor_t", &impl->InterpTranspose[iN]));
362*db2becc9SJeremy L Thompson       CeedCallBackend(CeedGetKernelMagma(ceed, impl->module[iN], "magma_interp_nontensor_ta", &impl->InterpTransposeAdd[iN]));
3637251047cSSebastian Grimberg       CeedCallBackend(CeedGetKernelMagma(ceed, impl->module[iN], "magma_deriv_nontensor_n", &impl->Deriv[iN]));
3647251047cSSebastian Grimberg       CeedCallBackend(CeedGetKernelMagma(ceed, impl->module[iN], "magma_deriv_nontensor_t", &impl->DerivTranspose[iN]));
365*db2becc9SJeremy L Thompson       CeedCallBackend(CeedGetKernelMagma(ceed, impl->module[iN], "magma_deriv_nontensor_ta", &impl->DerivTransposeAdd[iN]));
3667251047cSSebastian Grimberg       if (!impl->Weight) {
3677251047cSSebastian Grimberg         CeedCallBackend(CeedGetKernelMagma(ceed, impl->module[iN], "magma_weight_nontensor", &impl->Weight));
3687251047cSSebastian Grimberg         CeedCallBackend(CeedFree(&weight_kernel_path));
3697251047cSSebastian Grimberg       }
3709d15e85bSSebastian Grimberg       CeedCallBackend(CeedFree(&basis_kernel_path));
371940a72f1SSebastian Grimberg       CeedCallBackend(CeedFree(&basis_kernel_source));
372509d4af6SJeremy L Thompson       for (CeedInt i = 0; i < num_file_paths; i++) CeedCall(CeedFree(&file_paths[i]));
373509d4af6SJeremy L Thompson       CeedCall(CeedFree(&file_paths));
374940a72f1SSebastian Grimberg     }
3757251047cSSebastian Grimberg   }
3767251047cSSebastian Grimberg 
3777251047cSSebastian Grimberg   // Apply basis operation
3787251047cSSebastian Grimberg   if (e_mode != CEED_EVAL_WEIGHT) {
3797251047cSSebastian Grimberg     const CeedScalar *d_b = NULL;
3807251047cSSebastian Grimberg     CeedInt           q_comp, NB, M, K;
3817251047cSSebastian Grimberg     CeedMagmaFunction Kernel;
3827251047cSSebastian Grimberg 
3837251047cSSebastian Grimberg     switch (e_mode) {
3847251047cSSebastian Grimberg       case CEED_EVAL_INTERP:
3857251047cSSebastian Grimberg         d_b = impl->d_interp;
3867251047cSSebastian Grimberg         break;
3877251047cSSebastian Grimberg       case CEED_EVAL_GRAD:
3887251047cSSebastian Grimberg         d_b = impl->d_grad;
3897251047cSSebastian Grimberg         break;
3907251047cSSebastian Grimberg       case CEED_EVAL_DIV:
3917251047cSSebastian Grimberg         d_b = impl->d_div;
3927251047cSSebastian Grimberg         break;
3937251047cSSebastian Grimberg       case CEED_EVAL_CURL:
3947251047cSSebastian Grimberg         d_b = impl->d_curl;
3957251047cSSebastian Grimberg         break;
3967251047cSSebastian Grimberg       // LCOV_EXCL_START
3977251047cSSebastian Grimberg       case CEED_EVAL_WEIGHT:
3987251047cSSebastian Grimberg       case CEED_EVAL_NONE:
399bcbe1c99SJeremy L Thompson         return CeedError(ceed, CEED_ERROR_BACKEND, "%s does not make sense in this context", CeedEvalModes[e_mode]);
4007251047cSSebastian Grimberg         // LCOV_EXCL_STOP
4017251047cSSebastian Grimberg     }
4027251047cSSebastian Grimberg     CeedCallBackend(CeedBasisGetNumQuadratureComponents(basis, e_mode, &q_comp));
4037251047cSSebastian Grimberg     M = (t_mode == CEED_TRANSPOSE) ? P : Q, K = (t_mode == CEED_TRANSPOSE) ? Q : P;
4047251047cSSebastian Grimberg 
4057251047cSSebastian Grimberg     if (P <= MAGMA_NONTENSOR_CUSTOM_KERNEL_MAX_P && Q <= MAGMA_NONTENSOR_CUSTOM_KERNEL_MAX_Q) {
4069d15e85bSSebastian Grimberg       if (e_mode == CEED_EVAL_INTERP) {
4079d15e85bSSebastian Grimberg         if (t_mode == CEED_TRANSPOSE) {
408*db2becc9SJeremy L Thompson           Kernel = apply_add ? impl->InterpTransposeAdd[iN] : impl->InterpTranspose[iN];
4099d15e85bSSebastian Grimberg           NB     = impl->NB_interp_t[iN];
4109d15e85bSSebastian Grimberg         } else {
4119d15e85bSSebastian Grimberg           Kernel = impl->Interp[iN];
4129d15e85bSSebastian Grimberg           NB     = impl->NB_interp[iN];
4139d15e85bSSebastian Grimberg         }
4149d15e85bSSebastian Grimberg       } else {
4159d15e85bSSebastian Grimberg         if (t_mode == CEED_TRANSPOSE) {
416*db2becc9SJeremy L Thompson           Kernel = apply_add ? impl->DerivTransposeAdd[iN] : impl->DerivTranspose[iN];
4179d15e85bSSebastian Grimberg           NB     = impl->NB_deriv_t[iN];
4189d15e85bSSebastian Grimberg         } else {
4199d15e85bSSebastian Grimberg           Kernel = impl->Deriv[iN];
4209d15e85bSSebastian Grimberg           NB     = impl->NB_deriv[iN];
4219d15e85bSSebastian Grimberg         }
4229d15e85bSSebastian Grimberg       }
423940a72f1SSebastian Grimberg       CeedInt num_t_col    = MAGMA_BASIS_NTCOL(M, MAGMA_MAXTHREADS_1D);
4249d15e85bSSebastian Grimberg       CeedInt grid         = CeedDivUpInt(N, num_t_col * NB);
425833aa127SSebastian Grimberg       CeedInt shared_mem_A = P * Q * sizeof(CeedScalar);
426940a72f1SSebastian Grimberg       CeedInt shared_mem_B = num_t_col * K * NB * sizeof(CeedScalar);
427833aa127SSebastian Grimberg       CeedInt shared_mem   = (t_mode != CEED_TRANSPOSE && q_comp > 1) ? (shared_mem_A + shared_mem_B) : CeedIntMax(shared_mem_A, shared_mem_B);
4289d15e85bSSebastian Grimberg       void   *args[]       = {&N, &d_b, &d_u, &d_v};
429940a72f1SSebastian Grimberg 
4309d15e85bSSebastian Grimberg       CeedCallBackend(CeedRunKernelDimSharedMagma(ceed, Kernel, grid, M, num_t_col, 1, shared_mem, args));
4319d15e85bSSebastian Grimberg     } else {
4329d15e85bSSebastian Grimberg       for (CeedInt d = 0; d < q_comp; d++) {
43338293ee6SJeremy L Thompson         if (t_mode == CEED_TRANSPOSE) {
434*db2becc9SJeremy L Thompson           const CeedScalar beta = (apply_add || (d > 0)) ? 1.0 : 0.0;
4359d15e85bSSebastian Grimberg           magma_gemm_nontensor(MagmaNoTrans, MagmaNoTrans, P, N, Q, 1.0, d_b + d * P * Q, P, d_u + d * N * Q, Q, beta, d_v, P, data->queue);
436940a72f1SSebastian Grimberg         } else {
4379d15e85bSSebastian Grimberg           magma_gemm_nontensor(MagmaTrans, MagmaNoTrans, Q, N, P, 1.0, d_b + d * P * Q, P, d_u, P, 0.0, d_v + d * N * Q, Q, data->queue);
438940a72f1SSebastian Grimberg         }
439940a72f1SSebastian Grimberg       }
440940a72f1SSebastian Grimberg     }
441940a72f1SSebastian Grimberg   } else {
442940a72f1SSebastian Grimberg     CeedCheck(t_mode != CEED_TRANSPOSE, ceed, CEED_ERROR_BACKEND, "CEED_EVAL_WEIGHT incompatible with CEED_TRANSPOSE");
443097cc795SJames Wright     CeedCheck(impl->d_q_weight, ceed, CEED_ERROR_BACKEND, "%s not supported; q_weight not set", CeedEvalModes[e_mode]);
444940a72f1SSebastian Grimberg     CeedInt num_t_col  = MAGMA_BASIS_NTCOL(Q, MAGMA_MAXTHREADS_1D);
445940a72f1SSebastian Grimberg     CeedInt grid       = CeedDivUpInt(num_elem, num_t_col);
446940a72f1SSebastian Grimberg     CeedInt shared_mem = Q * sizeof(CeedScalar) + num_t_col * Q * sizeof(CeedScalar);
4479d15e85bSSebastian Grimberg     void   *args[]     = {&num_elem, &impl->d_q_weight, &d_v};
448868539c2SNatalie Beams 
449940a72f1SSebastian Grimberg     CeedCallBackend(CeedRunKernelDimSharedMagma(ceed, impl->Weight, grid, Q, num_t_col, 1, shared_mem, args));
450940a72f1SSebastian Grimberg   }
451940a72f1SSebastian Grimberg 
452940a72f1SSebastian Grimberg   // Must sync to ensure completeness
453e0582403Sabdelfattah83   ceed_magma_queue_sync(data->queue);
454e0582403Sabdelfattah83 
455940a72f1SSebastian Grimberg   // Restore vectors
45638293ee6SJeremy L Thompson   if (e_mode != CEED_EVAL_WEIGHT) {
457940a72f1SSebastian Grimberg     CeedCallBackend(CeedVectorRestoreArrayRead(u, &d_u));
458868539c2SNatalie Beams   }
459940a72f1SSebastian Grimberg   CeedCallBackend(CeedVectorRestoreArray(v, &d_v));
460e15f9bd0SJeremy L Thompson   return CEED_ERROR_SUCCESS;
461868539c2SNatalie Beams }
462868539c2SNatalie Beams 
463*db2becc9SJeremy L Thompson static int CeedBasisApplyNonTensor_Magma(CeedBasis basis, CeedInt num_elem, CeedTransposeMode t_mode, CeedEvalMode e_mode, CeedVector u,
464*db2becc9SJeremy L Thompson                                          CeedVector v) {
465*db2becc9SJeremy L Thompson   CeedCallBackend(CeedBasisApplyNonTensorCore_Magma(basis, false, num_elem, t_mode, e_mode, u, v));
466*db2becc9SJeremy L Thompson   return CEED_ERROR_SUCCESS;
467*db2becc9SJeremy L Thompson }
468*db2becc9SJeremy L Thompson 
469*db2becc9SJeremy L Thompson static int CeedBasisApplyAddNonTensor_Magma(CeedBasis basis, CeedInt num_elem, CeedTransposeMode t_mode, CeedEvalMode e_mode, CeedVector u,
470*db2becc9SJeremy L Thompson                                             CeedVector v) {
471*db2becc9SJeremy L Thompson   CeedCallBackend(CeedBasisApplyNonTensorCore_Magma(basis, true, num_elem, t_mode, e_mode, u, v));
472*db2becc9SJeremy L Thompson   return CEED_ERROR_SUCCESS;
473*db2becc9SJeremy L Thompson }
474*db2becc9SJeremy L Thompson 
475940a72f1SSebastian Grimberg //------------------------------------------------------------------------------
476940a72f1SSebastian Grimberg // Destroy tensor basis
477940a72f1SSebastian Grimberg //------------------------------------------------------------------------------
478940a72f1SSebastian Grimberg static int CeedBasisDestroy_Magma(CeedBasis basis) {
479f6af633fSnbeams   Ceed             ceed;
48038293ee6SJeremy L Thompson   CeedBasis_Magma *impl;
48138293ee6SJeremy L Thompson 
4822b730f8bSJeremy L Thompson   CeedCallBackend(CeedBasisGetCeed(basis, &ceed));
483940a72f1SSebastian Grimberg   CeedCallBackend(CeedBasisGetData(basis, &impl));
484e5f091ebSnbeams #ifdef CEED_MAGMA_USE_HIP
4852b730f8bSJeremy L Thompson   CeedCallHip(ceed, hipModuleUnload(impl->module));
486f6af633fSnbeams #else
4872b730f8bSJeremy L Thompson   CeedCallCuda(ceed, cuModuleUnload(impl->module));
488f6af633fSnbeams #endif
489940a72f1SSebastian Grimberg   CeedCallBackend(magma_free(impl->d_interp_1d));
490940a72f1SSebastian Grimberg   CeedCallBackend(magma_free(impl->d_grad_1d));
491097cc795SJames Wright   if (impl->d_q_weight_1d) CeedCallBackend(magma_free(impl->d_q_weight_1d));
4922b730f8bSJeremy L Thompson   CeedCallBackend(CeedFree(&impl));
493e15f9bd0SJeremy L Thompson   return CEED_ERROR_SUCCESS;
4947f5b9731SStan Tomov }
4957f5b9731SStan Tomov 
496940a72f1SSebastian Grimberg //------------------------------------------------------------------------------
497940a72f1SSebastian Grimberg // Destroy non-tensor basis
498940a72f1SSebastian Grimberg //------------------------------------------------------------------------------
499940a72f1SSebastian Grimberg static int CeedBasisDestroyNonTensor_Magma(CeedBasis basis) {
500023b8a51Sabdelfattah83   Ceed                      ceed;
50138293ee6SJeremy L Thompson   CeedBasisNonTensor_Magma *impl;
50238293ee6SJeremy L Thompson 
503940a72f1SSebastian Grimberg   CeedCallBackend(CeedBasisGetCeed(basis, &ceed));
50438293ee6SJeremy L Thompson   CeedCallBackend(CeedBasisGetData(basis, &impl));
505940a72f1SSebastian Grimberg   for (CeedInt in = 0; in < MAGMA_NONTENSOR_KERNEL_INSTANCES; in++) {
5067251047cSSebastian Grimberg     if (impl->module[in]) {
507940a72f1SSebastian Grimberg #ifdef CEED_MAGMA_USE_HIP
5087251047cSSebastian Grimberg       CeedCallHip(ceed, hipModuleUnload(impl->module[in]));
509940a72f1SSebastian Grimberg #else
5107251047cSSebastian Grimberg       CeedCallCuda(ceed, cuModuleUnload(impl->module[in]));
511940a72f1SSebastian Grimberg #endif
512940a72f1SSebastian Grimberg     }
513940a72f1SSebastian Grimberg   }
51438293ee6SJeremy L Thompson   CeedCallBackend(magma_free(impl->d_interp));
51538293ee6SJeremy L Thompson   CeedCallBackend(magma_free(impl->d_grad));
5169d15e85bSSebastian Grimberg   CeedCallBackend(magma_free(impl->d_div));
5179d15e85bSSebastian Grimberg   CeedCallBackend(magma_free(impl->d_curl));
518097cc795SJames Wright   if (impl->d_q_weight) CeedCallBackend(magma_free(impl->d_q_weight));
5192b730f8bSJeremy L Thompson   CeedCallBackend(CeedFree(&impl));
520e15f9bd0SJeremy L Thompson   return CEED_ERROR_SUCCESS;
521868539c2SNatalie Beams }
522868539c2SNatalie Beams 
523940a72f1SSebastian Grimberg //------------------------------------------------------------------------------
524940a72f1SSebastian Grimberg // Create tensor
525940a72f1SSebastian Grimberg //------------------------------------------------------------------------------
526940a72f1SSebastian Grimberg int CeedBasisCreateTensorH1_Magma(CeedInt dim, CeedInt P_1d, CeedInt Q_1d, const CeedScalar *interp_1d, const CeedScalar *grad_1d,
52738293ee6SJeremy L Thompson                                   const CeedScalar *q_ref_1d, const CeedScalar *q_weight_1d, CeedBasis basis) {
52838293ee6SJeremy L Thompson   Ceed             ceed, ceed_delegate;
52938293ee6SJeremy L Thompson   Ceed_Magma      *data;
53022070f95SJeremy L Thompson   char            *basis_kernel_source;
53122070f95SJeremy L Thompson   const char      *interp_kernel_path, *grad_kernel_path, *weight_kernel_path;
532509d4af6SJeremy L Thompson   char           **file_paths     = NULL;
533509d4af6SJeremy L Thompson   CeedInt          num_file_paths = 0;
534940a72f1SSebastian Grimberg   CeedInt          num_comp;
5357f5b9731SStan Tomov   CeedBasis_Magma *impl;
53638293ee6SJeremy L Thompson 
5372b730f8bSJeremy L Thompson   CeedCallBackend(CeedBasisGetCeed(basis, &ceed));
5382b730f8bSJeremy L Thompson   CeedCallBackend(CeedGetData(ceed, &data));
539940a72f1SSebastian Grimberg   CeedCallBackend(CeedCalloc(1, &impl));
540e0582403Sabdelfattah83 
541940a72f1SSebastian Grimberg   // Copy basis data to GPU
542097cc795SJames Wright   if (q_weight_1d) {
543940a72f1SSebastian Grimberg     CeedCallBackend(magma_malloc((void **)&impl->d_q_weight_1d, Q_1d * sizeof(q_weight_1d[0])));
544940a72f1SSebastian Grimberg     magma_setvector(Q_1d, sizeof(q_weight_1d[0]), q_weight_1d, 1, impl->d_q_weight_1d, 1, data->queue);
545097cc795SJames Wright   }
54638293ee6SJeremy L Thompson   CeedCallBackend(magma_malloc((void **)&impl->d_interp_1d, Q_1d * P_1d * sizeof(interp_1d[0])));
54738293ee6SJeremy L Thompson   magma_setvector(Q_1d * P_1d, sizeof(interp_1d[0]), interp_1d, 1, impl->d_interp_1d, 1, data->queue);
54838293ee6SJeremy L Thompson   CeedCallBackend(magma_malloc((void **)&impl->d_grad_1d, Q_1d * P_1d * sizeof(grad_1d[0])));
54938293ee6SJeremy L Thompson   magma_setvector(Q_1d * P_1d, sizeof(grad_1d[0]), grad_1d, 1, impl->d_grad_1d, 1, data->queue);
5507f5b9731SStan Tomov 
551940a72f1SSebastian Grimberg   // The RTC compilation code expects a Ceed with the common Ceed_Cuda or Ceed_Hip data
552940a72f1SSebastian Grimberg   CeedCallBackend(CeedGetDelegate(ceed, &ceed_delegate));
553940a72f1SSebastian Grimberg 
554940a72f1SSebastian Grimberg   // Compile kernels
555940a72f1SSebastian Grimberg   CeedCallBackend(CeedBasisGetNumComponents(basis, &num_comp));
556940a72f1SSebastian Grimberg   {
557940a72f1SSebastian Grimberg     char   *interp_kernel_name_base = "ceed/jit-source/magma/magma-basis-interp";
558940a72f1SSebastian Grimberg     CeedInt interp_kernel_name_len  = strlen(interp_kernel_name_base) + 6;
559940a72f1SSebastian Grimberg     char    interp_kernel_name[interp_kernel_name_len];
560940a72f1SSebastian Grimberg 
561940a72f1SSebastian Grimberg     snprintf(interp_kernel_name, interp_kernel_name_len, "%s-%" CeedInt_FMT "d.h", interp_kernel_name_base, dim);
562940a72f1SSebastian Grimberg     CeedCallBackend(CeedGetJitAbsolutePath(ceed, interp_kernel_name, &interp_kernel_path));
563940a72f1SSebastian Grimberg   }
564940a72f1SSebastian Grimberg   CeedDebug256(ceed, CEED_DEBUG_COLOR_SUCCESS, "----- Loading Basis Kernel Source -----\n");
565509d4af6SJeremy L Thompson   CeedCallBackend(CeedLoadSourceAndInitializeBuffer(ceed, interp_kernel_path, &num_file_paths, &file_paths, &basis_kernel_source));
566940a72f1SSebastian Grimberg   {
567940a72f1SSebastian Grimberg     char   *grad_kernel_name_base = "ceed/jit-source/magma/magma-basis-grad";
568940a72f1SSebastian Grimberg     CeedInt grad_kernel_name_len  = strlen(grad_kernel_name_base) + 6;
569940a72f1SSebastian Grimberg     char    grad_kernel_name[grad_kernel_name_len];
570940a72f1SSebastian Grimberg 
571940a72f1SSebastian Grimberg     snprintf(grad_kernel_name, grad_kernel_name_len, "%s-%" CeedInt_FMT "d.h", grad_kernel_name_base, dim);
572940a72f1SSebastian Grimberg     CeedCallBackend(CeedGetJitAbsolutePath(ceed, grad_kernel_name, &grad_kernel_path));
573940a72f1SSebastian Grimberg   }
574509d4af6SJeremy L Thompson   CeedCallBackend(CeedLoadSourceToInitializedBuffer(ceed, grad_kernel_path, &num_file_paths, &file_paths, &basis_kernel_source));
575940a72f1SSebastian Grimberg   {
576940a72f1SSebastian Grimberg     char   *weight_kernel_name_base = "ceed/jit-source/magma/magma-basis-weight";
577940a72f1SSebastian Grimberg     CeedInt weight_kernel_name_len  = strlen(weight_kernel_name_base) + 6;
578940a72f1SSebastian Grimberg     char    weight_kernel_name[weight_kernel_name_len];
579940a72f1SSebastian Grimberg 
580940a72f1SSebastian Grimberg     snprintf(weight_kernel_name, weight_kernel_name_len, "%s-%" CeedInt_FMT "d.h", weight_kernel_name_base, dim);
581940a72f1SSebastian Grimberg     CeedCallBackend(CeedGetJitAbsolutePath(ceed, weight_kernel_name, &weight_kernel_path));
582940a72f1SSebastian Grimberg   }
583509d4af6SJeremy L Thompson   CeedCallBackend(CeedLoadSourceToInitializedBuffer(ceed, weight_kernel_path, &num_file_paths, &file_paths, &basis_kernel_source));
584940a72f1SSebastian Grimberg   CeedDebug256(ceed, CEED_DEBUG_COLOR_SUCCESS, "----- Loading Basis Kernel Source Complete! -----\n");
585940a72f1SSebastian Grimberg   CeedCallBackend(CeedCompileMagma(ceed_delegate, basis_kernel_source, &impl->module, 5, "BASIS_DIM", dim, "BASIS_NUM_COMP", num_comp, "BASIS_P",
586940a72f1SSebastian Grimberg                                    P_1d, "BASIS_Q", Q_1d, "BASIS_MAX_P_Q", CeedIntMax(P_1d, Q_1d)));
587940a72f1SSebastian Grimberg   switch (dim) {
588940a72f1SSebastian Grimberg     case 1:
589940a72f1SSebastian Grimberg       CeedCallBackend(CeedGetKernelMagma(ceed, impl->module, "magma_interpn_1d_kernel", &impl->Interp));
590940a72f1SSebastian Grimberg       CeedCallBackend(CeedGetKernelMagma(ceed, impl->module, "magma_interpt_1d_kernel", &impl->InterpTranspose));
591*db2becc9SJeremy L Thompson       CeedCallBackend(CeedGetKernelMagma(ceed, impl->module, "magma_interpta_1d_kernel", &impl->InterpTransposeAdd));
592940a72f1SSebastian Grimberg       CeedCallBackend(CeedGetKernelMagma(ceed, impl->module, "magma_gradn_1d_kernel", &impl->Grad));
593940a72f1SSebastian Grimberg       CeedCallBackend(CeedGetKernelMagma(ceed, impl->module, "magma_gradt_1d_kernel", &impl->GradTranspose));
594*db2becc9SJeremy L Thompson       CeedCallBackend(CeedGetKernelMagma(ceed, impl->module, "magma_gradta_1d_kernel", &impl->GradTransposeAdd));
595940a72f1SSebastian Grimberg       CeedCallBackend(CeedGetKernelMagma(ceed, impl->module, "magma_weight_1d_kernel", &impl->Weight));
596940a72f1SSebastian Grimberg       break;
597940a72f1SSebastian Grimberg     case 2:
598940a72f1SSebastian Grimberg       CeedCallBackend(CeedGetKernelMagma(ceed, impl->module, "magma_interpn_2d_kernel", &impl->Interp));
599940a72f1SSebastian Grimberg       CeedCallBackend(CeedGetKernelMagma(ceed, impl->module, "magma_interpt_2d_kernel", &impl->InterpTranspose));
600*db2becc9SJeremy L Thompson       CeedCallBackend(CeedGetKernelMagma(ceed, impl->module, "magma_interpta_2d_kernel", &impl->InterpTransposeAdd));
601940a72f1SSebastian Grimberg       CeedCallBackend(CeedGetKernelMagma(ceed, impl->module, "magma_gradn_2d_kernel", &impl->Grad));
602940a72f1SSebastian Grimberg       CeedCallBackend(CeedGetKernelMagma(ceed, impl->module, "magma_gradt_2d_kernel", &impl->GradTranspose));
603*db2becc9SJeremy L Thompson       CeedCallBackend(CeedGetKernelMagma(ceed, impl->module, "magma_gradta_2d_kernel", &impl->GradTransposeAdd));
604940a72f1SSebastian Grimberg       CeedCallBackend(CeedGetKernelMagma(ceed, impl->module, "magma_weight_2d_kernel", &impl->Weight));
605940a72f1SSebastian Grimberg       break;
606940a72f1SSebastian Grimberg     case 3:
607940a72f1SSebastian Grimberg       CeedCallBackend(CeedGetKernelMagma(ceed, impl->module, "magma_interpn_3d_kernel", &impl->Interp));
608940a72f1SSebastian Grimberg       CeedCallBackend(CeedGetKernelMagma(ceed, impl->module, "magma_interpt_3d_kernel", &impl->InterpTranspose));
609*db2becc9SJeremy L Thompson       CeedCallBackend(CeedGetKernelMagma(ceed, impl->module, "magma_interpta_3d_kernel", &impl->InterpTransposeAdd));
610940a72f1SSebastian Grimberg       CeedCallBackend(CeedGetKernelMagma(ceed, impl->module, "magma_gradn_3d_kernel", &impl->Grad));
611940a72f1SSebastian Grimberg       CeedCallBackend(CeedGetKernelMagma(ceed, impl->module, "magma_gradt_3d_kernel", &impl->GradTranspose));
612*db2becc9SJeremy L Thompson       CeedCallBackend(CeedGetKernelMagma(ceed, impl->module, "magma_gradta_3d_kernel", &impl->GradTransposeAdd));
613940a72f1SSebastian Grimberg       CeedCallBackend(CeedGetKernelMagma(ceed, impl->module, "magma_weight_3d_kernel", &impl->Weight));
614940a72f1SSebastian Grimberg       break;
615940a72f1SSebastian Grimberg   }
616940a72f1SSebastian Grimberg   CeedCallBackend(CeedFree(&interp_kernel_path));
617940a72f1SSebastian Grimberg   CeedCallBackend(CeedFree(&grad_kernel_path));
618940a72f1SSebastian Grimberg   CeedCallBackend(CeedFree(&weight_kernel_path));
619940a72f1SSebastian Grimberg   CeedCallBackend(CeedFree(&basis_kernel_source));
620509d4af6SJeremy L Thompson   for (CeedInt i = 0; i < num_file_paths; i++) CeedCall(CeedFree(&file_paths[i]));
621509d4af6SJeremy L Thompson   CeedCall(CeedFree(&file_paths));
6227f5b9731SStan Tomov 
6232b730f8bSJeremy L Thompson   CeedCallBackend(CeedBasisSetData(basis, impl));
624940a72f1SSebastian Grimberg 
625940a72f1SSebastian Grimberg   CeedCallBackend(CeedSetBackendFunction(ceed, "Basis", basis, "Apply", CeedBasisApply_Magma));
626*db2becc9SJeremy L Thompson   CeedCallBackend(CeedSetBackendFunction(ceed, "Basis", basis, "ApplyAdd", CeedBasisApplyAdd_Magma));
62714950a8eSJeremy L Thompson   CeedCallBackend(CeedSetBackendFunction(ceed, "Basis", basis, "ApplyAtPoints", CeedBasisApplyAtPoints_Magma));
628940a72f1SSebastian Grimberg   CeedCallBackend(CeedSetBackendFunction(ceed, "Basis", basis, "Destroy", CeedBasisDestroy_Magma));
629e15f9bd0SJeremy L Thompson   return CEED_ERROR_SUCCESS;
6307f5b9731SStan Tomov }
6317f5b9731SStan Tomov 
632940a72f1SSebastian Grimberg //------------------------------------------------------------------------------
633940a72f1SSebastian Grimberg // Create non-tensor H^1
634940a72f1SSebastian Grimberg //------------------------------------------------------------------------------
635940a72f1SSebastian Grimberg int CeedBasisCreateH1_Magma(CeedElemTopology topo, CeedInt dim, CeedInt num_nodes, CeedInt num_qpts, const CeedScalar *interp, const CeedScalar *grad,
63638293ee6SJeremy L Thompson                             const CeedScalar *q_ref, const CeedScalar *q_weight, CeedBasis basis) {
6377251047cSSebastian Grimberg   Ceed                      ceed;
638e0582403Sabdelfattah83   Ceed_Magma               *data;
63938293ee6SJeremy L Thompson   CeedBasisNonTensor_Magma *impl;
64038293ee6SJeremy L Thompson 
64138293ee6SJeremy L Thompson   CeedCallBackend(CeedBasisGetCeed(basis, &ceed));
6422b730f8bSJeremy L Thompson   CeedCallBackend(CeedGetData(ceed, &data));
6432b730f8bSJeremy L Thompson   CeedCallBackend(CeedCalloc(1, &impl));
644023b8a51Sabdelfattah83 
645940a72f1SSebastian Grimberg   // Copy basis data to GPU
646097cc795SJames Wright   if (q_weight) {
64738293ee6SJeremy L Thompson     CeedCallBackend(magma_malloc((void **)&impl->d_q_weight, num_qpts * sizeof(q_weight[0])));
64838293ee6SJeremy L Thompson     magma_setvector(num_qpts, sizeof(q_weight[0]), q_weight, 1, impl->d_q_weight, 1, data->queue);
649097cc795SJames Wright   }
6509d15e85bSSebastian Grimberg   if (interp) {
6519d15e85bSSebastian Grimberg     CeedInt q_comp_interp;
6529d15e85bSSebastian Grimberg 
6539d15e85bSSebastian Grimberg     CeedCallBackend(CeedBasisGetNumQuadratureComponents(basis, CEED_EVAL_INTERP, &q_comp_interp));
6549d15e85bSSebastian Grimberg     CeedCallBackend(magma_malloc((void **)&impl->d_interp, num_qpts * num_nodes * q_comp_interp * sizeof(interp[0])));
6559d15e85bSSebastian Grimberg     magma_setvector(num_qpts * num_nodes * q_comp_interp, sizeof(interp[0]), interp, 1, impl->d_interp, 1, data->queue);
6569d15e85bSSebastian Grimberg   }
6579d15e85bSSebastian Grimberg   if (grad) {
6589d15e85bSSebastian Grimberg     CeedInt q_comp_grad;
6599d15e85bSSebastian Grimberg 
6609d15e85bSSebastian Grimberg     CeedCallBackend(CeedBasisGetNumQuadratureComponents(basis, CEED_EVAL_GRAD, &q_comp_grad));
6619d15e85bSSebastian Grimberg     CeedCallBackend(magma_malloc((void **)&impl->d_grad, num_qpts * num_nodes * q_comp_grad * sizeof(grad[0])));
6629d15e85bSSebastian Grimberg     magma_setvector(num_qpts * num_nodes * q_comp_grad, sizeof(grad[0]), grad, 1, impl->d_grad, 1, data->queue);
6639d15e85bSSebastian Grimberg   }
6649d15e85bSSebastian Grimberg 
6657251047cSSebastian Grimberg   // Compile the weight kernel if it won't be compiled later on
6667251047cSSebastian Grimberg   if (num_nodes > MAGMA_NONTENSOR_CUSTOM_KERNEL_MAX_P || num_qpts > MAGMA_NONTENSOR_CUSTOM_KERNEL_MAX_Q) {
6677251047cSSebastian Grimberg     Ceed        ceed_delegate;
66822070f95SJeremy L Thompson     char       *basis_kernel_source;
66922070f95SJeremy L Thompson     const char *weight_kernel_path;
6707251047cSSebastian Grimberg 
6719d15e85bSSebastian Grimberg     // The RTC compilation code expects a Ceed with the common Ceed_Cuda or Ceed_Hip data
6729d15e85bSSebastian Grimberg     CeedCallBackend(CeedGetDelegate(ceed, &ceed_delegate));
6739d15e85bSSebastian Grimberg 
6749d15e85bSSebastian Grimberg     // Compile weight kernel (the remainder of kernel compilation happens at first call to CeedBasisApply)
6759d15e85bSSebastian Grimberg     CeedCallBackend(CeedGetJitAbsolutePath(ceed, "ceed/jit-source/magma/magma-basis-weight-nontensor.h", &weight_kernel_path));
6769d15e85bSSebastian Grimberg     CeedDebug256(ceed, CEED_DEBUG_COLOR_SUCCESS, "----- Loading Basis Kernel Source -----\n");
6779d15e85bSSebastian Grimberg     CeedCallBackend(CeedLoadSourceToBuffer(ceed, weight_kernel_path, &basis_kernel_source));
6789d15e85bSSebastian Grimberg     CeedDebug256(ceed, CEED_DEBUG_COLOR_SUCCESS, "----- Loading Basis Kernel Source Complete! -----\n");
6797251047cSSebastian Grimberg     CeedCallBackend(CeedCompileMagma(ceed_delegate, basis_kernel_source, &impl->module[0], 1, "BASIS_Q", num_qpts));
6807251047cSSebastian Grimberg     CeedCallBackend(CeedGetKernelMagma(ceed, impl->module[0], "magma_weight_nontensor", &impl->Weight));
6819d15e85bSSebastian Grimberg     CeedCallBackend(CeedFree(&weight_kernel_path));
6829d15e85bSSebastian Grimberg     CeedCallBackend(CeedFree(&basis_kernel_source));
6837251047cSSebastian Grimberg   }
6849d15e85bSSebastian Grimberg 
6859d15e85bSSebastian Grimberg   CeedCallBackend(CeedBasisSetData(basis, impl));
6869d15e85bSSebastian Grimberg 
6879d15e85bSSebastian Grimberg   // Register backend functions
6889d15e85bSSebastian Grimberg   CeedCallBackend(CeedSetBackendFunction(ceed, "Basis", basis, "Apply", CeedBasisApplyNonTensor_Magma));
689*db2becc9SJeremy L Thompson   CeedCallBackend(CeedSetBackendFunction(ceed, "Basis", basis, "ApplyAdd", CeedBasisApplyAddNonTensor_Magma));
6909d15e85bSSebastian Grimberg   CeedCallBackend(CeedSetBackendFunction(ceed, "Basis", basis, "Destroy", CeedBasisDestroyNonTensor_Magma));
6919d15e85bSSebastian Grimberg   return CEED_ERROR_SUCCESS;
6929d15e85bSSebastian Grimberg }
6939d15e85bSSebastian Grimberg 
6949d15e85bSSebastian Grimberg //------------------------------------------------------------------------------
6959d15e85bSSebastian Grimberg // Create non-tensor H(div)
6969d15e85bSSebastian Grimberg //------------------------------------------------------------------------------
6979d15e85bSSebastian Grimberg int CeedBasisCreateHdiv_Magma(CeedElemTopology topo, CeedInt dim, CeedInt num_nodes, CeedInt num_qpts, const CeedScalar *interp,
6989d15e85bSSebastian Grimberg                               const CeedScalar *div, const CeedScalar *q_ref, const CeedScalar *q_weight, CeedBasis basis) {
6997251047cSSebastian Grimberg   Ceed                      ceed;
7009d15e85bSSebastian Grimberg   Ceed_Magma               *data;
7019d15e85bSSebastian Grimberg   CeedBasisNonTensor_Magma *impl;
7029d15e85bSSebastian Grimberg 
7039d15e85bSSebastian Grimberg   CeedCallBackend(CeedBasisGetCeed(basis, &ceed));
7049d15e85bSSebastian Grimberg   CeedCallBackend(CeedGetData(ceed, &data));
7059d15e85bSSebastian Grimberg   CeedCallBackend(CeedCalloc(1, &impl));
7069d15e85bSSebastian Grimberg 
7079d15e85bSSebastian Grimberg   // Copy basis data to GPU
708097cc795SJames Wright   if (q_weight) {
7099d15e85bSSebastian Grimberg     CeedCallBackend(magma_malloc((void **)&impl->d_q_weight, num_qpts * sizeof(q_weight[0])));
7109d15e85bSSebastian Grimberg     magma_setvector(num_qpts, sizeof(q_weight[0]), q_weight, 1, impl->d_q_weight, 1, data->queue);
711097cc795SJames Wright   }
7129d15e85bSSebastian Grimberg   if (interp) {
7139d15e85bSSebastian Grimberg     CeedInt q_comp_interp;
7149d15e85bSSebastian Grimberg 
7159d15e85bSSebastian Grimberg     CeedCallBackend(CeedBasisGetNumQuadratureComponents(basis, CEED_EVAL_INTERP, &q_comp_interp));
7169d15e85bSSebastian Grimberg     CeedCallBackend(magma_malloc((void **)&impl->d_interp, num_qpts * num_nodes * q_comp_interp * sizeof(interp[0])));
7179d15e85bSSebastian Grimberg     magma_setvector(num_qpts * num_nodes * q_comp_interp, sizeof(interp[0]), interp, 1, impl->d_interp, 1, data->queue);
7189d15e85bSSebastian Grimberg   }
7199d15e85bSSebastian Grimberg   if (div) {
7209d15e85bSSebastian Grimberg     CeedInt q_comp_div;
7219d15e85bSSebastian Grimberg 
7229d15e85bSSebastian Grimberg     CeedCallBackend(CeedBasisGetNumQuadratureComponents(basis, CEED_EVAL_DIV, &q_comp_div));
7239d15e85bSSebastian Grimberg     CeedCallBackend(magma_malloc((void **)&impl->d_div, num_qpts * num_nodes * q_comp_div * sizeof(div[0])));
7249d15e85bSSebastian Grimberg     magma_setvector(num_qpts * num_nodes * q_comp_div, sizeof(div[0]), div, 1, impl->d_div, 1, data->queue);
7259d15e85bSSebastian Grimberg   }
7269d15e85bSSebastian Grimberg 
7277251047cSSebastian Grimberg   // Compile the weight kernel if it won't be compiled later on
7287251047cSSebastian Grimberg   if (num_nodes > MAGMA_NONTENSOR_CUSTOM_KERNEL_MAX_P || num_qpts > MAGMA_NONTENSOR_CUSTOM_KERNEL_MAX_Q) {
7297251047cSSebastian Grimberg     Ceed        ceed_delegate;
73022070f95SJeremy L Thompson     char       *basis_kernel_source;
73122070f95SJeremy L Thompson     const char *weight_kernel_path;
7327251047cSSebastian Grimberg 
7339d15e85bSSebastian Grimberg     // The RTC compilation code expects a Ceed with the common Ceed_Cuda or Ceed_Hip data
7349d15e85bSSebastian Grimberg     CeedCallBackend(CeedGetDelegate(ceed, &ceed_delegate));
7359d15e85bSSebastian Grimberg 
7369d15e85bSSebastian Grimberg     // Compile weight kernel (the remainder of kernel compilation happens at first call to CeedBasisApply)
7379d15e85bSSebastian Grimberg     CeedCallBackend(CeedGetJitAbsolutePath(ceed, "ceed/jit-source/magma/magma-basis-weight-nontensor.h", &weight_kernel_path));
7389d15e85bSSebastian Grimberg     CeedDebug256(ceed, CEED_DEBUG_COLOR_SUCCESS, "----- Loading Basis Kernel Source -----\n");
7399d15e85bSSebastian Grimberg     CeedCallBackend(CeedLoadSourceToBuffer(ceed, weight_kernel_path, &basis_kernel_source));
7409d15e85bSSebastian Grimberg     CeedDebug256(ceed, CEED_DEBUG_COLOR_SUCCESS, "----- Loading Basis Kernel Source Complete! -----\n");
7417251047cSSebastian Grimberg     CeedCallBackend(CeedCompileMagma(ceed_delegate, basis_kernel_source, &impl->module[0], 1, "BASIS_Q", num_qpts));
7427251047cSSebastian Grimberg     CeedCallBackend(CeedGetKernelMagma(ceed, impl->module[0], "magma_weight_nontensor", &impl->Weight));
7439d15e85bSSebastian Grimberg     CeedCallBackend(CeedFree(&weight_kernel_path));
7449d15e85bSSebastian Grimberg     CeedCallBackend(CeedFree(&basis_kernel_source));
7457251047cSSebastian Grimberg   }
7469d15e85bSSebastian Grimberg 
7479d15e85bSSebastian Grimberg   CeedCallBackend(CeedBasisSetData(basis, impl));
7489d15e85bSSebastian Grimberg 
7499d15e85bSSebastian Grimberg   // Register backend functions
7509d15e85bSSebastian Grimberg   CeedCallBackend(CeedSetBackendFunction(ceed, "Basis", basis, "Apply", CeedBasisApplyNonTensor_Magma));
751*db2becc9SJeremy L Thompson   CeedCallBackend(CeedSetBackendFunction(ceed, "Basis", basis, "ApplyAdd", CeedBasisApplyAddNonTensor_Magma));
7529d15e85bSSebastian Grimberg   CeedCallBackend(CeedSetBackendFunction(ceed, "Basis", basis, "Destroy", CeedBasisDestroyNonTensor_Magma));
7539d15e85bSSebastian Grimberg   return CEED_ERROR_SUCCESS;
7549d15e85bSSebastian Grimberg }
7559d15e85bSSebastian Grimberg 
7569d15e85bSSebastian Grimberg //------------------------------------------------------------------------------
7579d15e85bSSebastian Grimberg // Create non-tensor H(curl)
7589d15e85bSSebastian Grimberg //------------------------------------------------------------------------------
7599d15e85bSSebastian Grimberg int CeedBasisCreateHcurl_Magma(CeedElemTopology topo, CeedInt dim, CeedInt num_nodes, CeedInt num_qpts, const CeedScalar *interp,
7609d15e85bSSebastian Grimberg                                const CeedScalar *curl, const CeedScalar *q_ref, const CeedScalar *q_weight, CeedBasis basis) {
7617251047cSSebastian Grimberg   Ceed                      ceed;
7629d15e85bSSebastian Grimberg   Ceed_Magma               *data;
7639d15e85bSSebastian Grimberg   CeedBasisNonTensor_Magma *impl;
7649d15e85bSSebastian Grimberg 
7659d15e85bSSebastian Grimberg   CeedCallBackend(CeedBasisGetCeed(basis, &ceed));
7669d15e85bSSebastian Grimberg   CeedCallBackend(CeedGetData(ceed, &data));
7679d15e85bSSebastian Grimberg   CeedCallBackend(CeedCalloc(1, &impl));
7689d15e85bSSebastian Grimberg 
7699d15e85bSSebastian Grimberg   // Copy basis data to GPU
770097cc795SJames Wright   if (q_weight) {
7719d15e85bSSebastian Grimberg     CeedCallBackend(magma_malloc((void **)&impl->d_q_weight, num_qpts * sizeof(q_weight[0])));
7729d15e85bSSebastian Grimberg     magma_setvector(num_qpts, sizeof(q_weight[0]), q_weight, 1, impl->d_q_weight, 1, data->queue);
773097cc795SJames Wright   }
7749d15e85bSSebastian Grimberg   if (interp) {
7759d15e85bSSebastian Grimberg     CeedInt q_comp_interp;
7769d15e85bSSebastian Grimberg 
7779d15e85bSSebastian Grimberg     CeedCallBackend(CeedBasisGetNumQuadratureComponents(basis, CEED_EVAL_INTERP, &q_comp_interp));
7789d15e85bSSebastian Grimberg     CeedCallBackend(magma_malloc((void **)&impl->d_interp, num_qpts * num_nodes * q_comp_interp * sizeof(interp[0])));
7799d15e85bSSebastian Grimberg     magma_setvector(num_qpts * num_nodes * q_comp_interp, sizeof(interp[0]), interp, 1, impl->d_interp, 1, data->queue);
7809d15e85bSSebastian Grimberg   }
7819d15e85bSSebastian Grimberg   if (curl) {
7829d15e85bSSebastian Grimberg     CeedInt q_comp_curl;
7839d15e85bSSebastian Grimberg 
7849d15e85bSSebastian Grimberg     CeedCallBackend(CeedBasisGetNumQuadratureComponents(basis, CEED_EVAL_CURL, &q_comp_curl));
7859d15e85bSSebastian Grimberg     CeedCallBackend(magma_malloc((void **)&impl->d_curl, num_qpts * num_nodes * q_comp_curl * sizeof(curl[0])));
7869d15e85bSSebastian Grimberg     magma_setvector(num_qpts * num_nodes * q_comp_curl, sizeof(curl[0]), curl, 1, impl->d_curl, 1, data->queue);
7879d15e85bSSebastian Grimberg   }
788940a72f1SSebastian Grimberg 
7897251047cSSebastian Grimberg   // Compile the weight kernel if it won't be compiled later on
7907251047cSSebastian Grimberg   if (num_nodes > MAGMA_NONTENSOR_CUSTOM_KERNEL_MAX_P || num_qpts > MAGMA_NONTENSOR_CUSTOM_KERNEL_MAX_Q) {
7917251047cSSebastian Grimberg     Ceed        ceed_delegate;
79222070f95SJeremy L Thompson     char       *basis_kernel_source;
79322070f95SJeremy L Thompson     const char *weight_kernel_path;
7947251047cSSebastian Grimberg 
795940a72f1SSebastian Grimberg     // The RTC compilation code expects a Ceed with the common Ceed_Cuda or Ceed_Hip data
796940a72f1SSebastian Grimberg     CeedCallBackend(CeedGetDelegate(ceed, &ceed_delegate));
797940a72f1SSebastian Grimberg 
798940a72f1SSebastian Grimberg     // Compile weight kernel (the remainder of kernel compilation happens at first call to CeedBasisApply)
799940a72f1SSebastian Grimberg     CeedCallBackend(CeedGetJitAbsolutePath(ceed, "ceed/jit-source/magma/magma-basis-weight-nontensor.h", &weight_kernel_path));
800940a72f1SSebastian Grimberg     CeedDebug256(ceed, CEED_DEBUG_COLOR_SUCCESS, "----- Loading Basis Kernel Source -----\n");
801940a72f1SSebastian Grimberg     CeedCallBackend(CeedLoadSourceToBuffer(ceed, weight_kernel_path, &basis_kernel_source));
802940a72f1SSebastian Grimberg     CeedDebug256(ceed, CEED_DEBUG_COLOR_SUCCESS, "----- Loading Basis Kernel Source Complete! -----\n");
8037251047cSSebastian Grimberg     CeedCallBackend(CeedCompileMagma(ceed_delegate, basis_kernel_source, &impl->module[0], 1, "BASIS_Q", num_qpts));
8047251047cSSebastian Grimberg     CeedCallBackend(CeedGetKernelMagma(ceed, impl->module[0], "magma_weight_nontensor", &impl->Weight));
805940a72f1SSebastian Grimberg     CeedCallBackend(CeedFree(&weight_kernel_path));
806940a72f1SSebastian Grimberg     CeedCallBackend(CeedFree(&basis_kernel_source));
8077251047cSSebastian Grimberg   }
808868539c2SNatalie Beams 
809023b8a51Sabdelfattah83   CeedCallBackend(CeedBasisSetData(basis, impl));
810940a72f1SSebastian Grimberg 
811940a72f1SSebastian Grimberg   // Register backend functions
812940a72f1SSebastian Grimberg   CeedCallBackend(CeedSetBackendFunction(ceed, "Basis", basis, "Apply", CeedBasisApplyNonTensor_Magma));
813*db2becc9SJeremy L Thompson   CeedCallBackend(CeedSetBackendFunction(ceed, "Basis", basis, "ApplyAdd", CeedBasisApplyAddNonTensor_Magma));
814940a72f1SSebastian Grimberg   CeedCallBackend(CeedSetBackendFunction(ceed, "Basis", basis, "Destroy", CeedBasisDestroyNonTensor_Magma));
815e15f9bd0SJeremy L Thompson   return CEED_ERROR_SUCCESS;
8167f5b9731SStan Tomov }
817940a72f1SSebastian Grimberg 
818940a72f1SSebastian Grimberg //------------------------------------------------------------------------------
819