xref: /libCEED/backends/cuda-shared/ceed-cuda-shared-basis.c (revision 3f63d318f8f9109f8dfbea1e53a3f2691ea0025e)
1c532df63SYohann // Copyright (c) 2017-2018, Lawrence Livermore National Security, LLC.
2c532df63SYohann // Produced at the Lawrence Livermore National Laboratory. LLNL-CODE-734707.
3c532df63SYohann // All Rights reserved. See files LICENSE and NOTICE for details.
4c532df63SYohann //
5c532df63SYohann // This file is part of CEED, a collection of benchmarks, miniapps, software
6c532df63SYohann // libraries and APIs for efficient high-order finite element and spectral
7c532df63SYohann // element discretizations for exascale applications. For more information and
8c532df63SYohann // source code availability see http://github.com/ceed.
9c532df63SYohann //
10c532df63SYohann // The CEED research is supported by the Exascale Computing Project 17-SC-20-SC,
11c532df63SYohann // a collaborative effort of two U.S. Department of Energy organizations (Office
12c532df63SYohann // of Science and the National Nuclear Security Administration) responsible for
13c532df63SYohann // the planning and preparation of a capable exascale ecosystem, including
14c532df63SYohann // software, applications, hardware, advanced system engineering and early
15c532df63SYohann // testbed platforms, in support of the nation's exascale computing imperative.
16c532df63SYohann 
17c532df63SYohann #include <ceed-backend.h>
18c532df63SYohann #include <ceed.h>
19c532df63SYohann #include "ceed-cuda-shared.h"
20c532df63SYohann #include "../cuda/ceed-cuda.h"
21c532df63SYohann 
22c532df63SYohann //*********************
23c532df63SYohann // shared mem kernels
24c532df63SYohann static const char *kernelsShared = QUOTE(
25c532df63SYohann 
26c532df63SYohann inline __device__ void add(CeedScalar *r_V, const CeedScalar *r_U) {
27c532df63SYohann   for (int i = 0; i < Q1D; i++)
28c532df63SYohann     r_V[i] += r_U[i];
29c532df63SYohann }
30c532df63SYohann 
31c532df63SYohann //////////
32c532df63SYohann //  1D  //
33c532df63SYohann //////////
34c532df63SYohann 
35c532df63SYohann inline __device__ void readDofs1d(const int elem, const int tidx,
36d94769d2SYohann Dudouit                                   const int tidy, const int tidz,const int comp,
37c532df63SYohann                                   const int nelem, const CeedScalar *d_U, CeedScalar *slice) {
38c532df63SYohann   for (int i = 0; i < P1D; i++)
39d94769d2SYohann Dudouit     slice[i+tidz*Q1D] = d_U[i + comp*P1D + elem*BASIS_NCOMP*P1D];
40c532df63SYohann   for (int i = P1D; i < Q1D; i++)
41d94769d2SYohann Dudouit     slice[i+tidz*Q1D] = 0.0;
42c532df63SYohann }
43c532df63SYohann 
44c532df63SYohann inline __device__ void writeDofs1d(const int elem, const int tidx,
45c532df63SYohann                                    const int tidy, const int comp,
46c532df63SYohann                                    const int nelem, const CeedScalar &r_V, CeedScalar *d_V) {
47c532df63SYohann   if (tidx<P1D) {
48c532df63SYohann     d_V[tidx + comp*P1D + elem*BASIS_NCOMP*P1D] = r_V;
49c532df63SYohann   }
50c532df63SYohann }
51c532df63SYohann 
52c532df63SYohann inline __device__ void readQuads1d(const int elem, const int tidx,
53d94769d2SYohann Dudouit                                    const int tidy, const int tidz, const int comp,
54c532df63SYohann                                    const int dim, const int nelem, const CeedScalar *d_U, CeedScalar *slice) {
55c532df63SYohann   for (int i = 0; i < Q1D; i++)
56d94769d2SYohann Dudouit     slice[i+tidz*Q1D] = d_U[i + elem*Q1D + comp*Q1D*nelem + dim*BASIS_NCOMP*nelem*Q1D];
57c532df63SYohann }
58c532df63SYohann 
59c532df63SYohann inline __device__ void writeQuads1d(const int elem, const int tidx,
60c532df63SYohann                                     const int tidy, const int comp,
61c532df63SYohann                                     const int dim, const int nelem, const CeedScalar &r_V, CeedScalar *d_V) {
62c532df63SYohann   d_V[tidx + elem*Q1D + comp*Q1D*nelem + dim*BASIS_NCOMP*nelem*Q1D] = r_V;
63c532df63SYohann }
64c532df63SYohann 
65c532df63SYohann inline __device__ void ContractX1d(CeedScalar *slice, const int tidx,
66d94769d2SYohann Dudouit                                    const int tidy, const int tidz,
67c532df63SYohann                                    const CeedScalar &U, const CeedScalar *B, CeedScalar &V) {
68c532df63SYohann   V = 0.0;
69c532df63SYohann   for (int i = 0; i < P1D; ++i) {
70d94769d2SYohann Dudouit     V += B[i + tidx*P1D] * slice[i+tidz*Q1D];//contract x direction
71c532df63SYohann   }
72c532df63SYohann }
73c532df63SYohann 
74c532df63SYohann inline __device__ void ContractTransposeX1d(CeedScalar *slice, const int tidx,
75d94769d2SYohann Dudouit     const int tidy, const int tidz,
76c532df63SYohann     const CeedScalar &U, const CeedScalar *B, CeedScalar &V) {
77c532df63SYohann   V = 0.0;
78c532df63SYohann   for (int i = 0; i < Q1D; ++i) {
79d94769d2SYohann Dudouit     V += B[tidx + i*P1D] * slice[i+tidz*Q1D];//contract x direction
80c532df63SYohann   }
81c532df63SYohann }
82c532df63SYohann 
83c532df63SYohann inline __device__ void interp1d(const CeedInt nelem, const int transpose,
84c532df63SYohann                                 const CeedScalar *c_B, const CeedScalar *__restrict__ d_U,
85c532df63SYohann                                 CeedScalar *__restrict__ d_V,
86c532df63SYohann                                 CeedScalar *slice) {
87c532df63SYohann   CeedScalar r_V;
88c532df63SYohann   CeedScalar r_t;
89c532df63SYohann 
90c532df63SYohann   const int tidx = threadIdx.x;
91c532df63SYohann   const int tidy = threadIdx.y;
92d94769d2SYohann Dudouit   const int tidz = threadIdx.z;
93c532df63SYohann 
94c532df63SYohann 
95c532df63SYohann   for (CeedInt elem = blockIdx.x*blockDim.z + threadIdx.z; elem < nelem;
96c532df63SYohann        elem += gridDim.x*blockDim.z) {
97c532df63SYohann     for(int comp=0; comp<BASIS_NCOMP; comp++) {
98c532df63SYohann       if(!transpose) {
99d94769d2SYohann Dudouit         readDofs1d(elem, tidx, tidy, tidz, comp, nelem, d_U, slice);
100d94769d2SYohann Dudouit         ContractX1d(slice, tidx, tidy, tidz, r_t, c_B, r_V);
101c532df63SYohann         writeQuads1d(elem, tidx, tidy, comp, 0, nelem, r_V, d_V);
102c532df63SYohann       } else {
103d94769d2SYohann Dudouit         readQuads1d(elem, tidx, tidy, tidz, comp, 0, nelem, d_U, slice);
104d94769d2SYohann Dudouit         ContractTransposeX1d(slice, tidx, tidy, tidz, r_t, c_B, r_V);
105c532df63SYohann         writeDofs1d(elem, tidx, tidy, comp, nelem, r_V, d_V);
106c532df63SYohann       }
107c532df63SYohann     }
108c532df63SYohann   }
109c532df63SYohann }
110c532df63SYohann 
111c532df63SYohann inline __device__ void grad1d(const CeedInt nelem, const int transpose,
112c532df63SYohann                               const CeedScalar *c_B, const CeedScalar *c_G,
113c532df63SYohann                               const CeedScalar *__restrict__ d_U, CeedScalar *__restrict__ d_V,
114c532df63SYohann                               CeedScalar *slice) {
115c532df63SYohann   CeedScalar r_U;
116c532df63SYohann   CeedScalar r_V;
117c532df63SYohann 
118c532df63SYohann   const int tidx = threadIdx.x;
119d94769d2SYohann Dudouit   const int tidy = threadIdx.y;
120d94769d2SYohann Dudouit   const int tidz = threadIdx.z;
121c532df63SYohann   int dim;
122c532df63SYohann 
123c532df63SYohann   for (CeedInt elem = blockIdx.x*blockDim.z + threadIdx.z; elem < nelem;
124c532df63SYohann        elem += gridDim.x*blockDim.z) {
125c532df63SYohann     for(int comp=0; comp<BASIS_NCOMP; comp++) {
126c532df63SYohann       if(!transpose) {
127d94769d2SYohann Dudouit         readDofs1d(elem, tidx, tidy, tidz, comp, nelem, d_U, slice);
128d94769d2SYohann Dudouit         ContractX1d(slice, tidx, tidy, tidz, r_U, c_G, r_V);
129c532df63SYohann         dim = 0;
130c532df63SYohann         writeQuads1d(elem, tidx, tidy, comp, dim, nelem, r_V, d_V);
131c532df63SYohann       } else {
132c532df63SYohann         dim = 0;
133d94769d2SYohann Dudouit         readQuads1d(elem, tidx, tidy, tidz, comp, dim, nelem, d_U, slice);
134d94769d2SYohann Dudouit         ContractTransposeX1d(slice, tidx, tidy, tidz, r_U, c_G, r_V);
135c532df63SYohann         writeDofs1d(elem, tidx, tidy, comp, nelem, r_V, d_V);
136c532df63SYohann       }
137c532df63SYohann     }
138c532df63SYohann   }
139c532df63SYohann }
140c532df63SYohann //////////
141c532df63SYohann //  2D  //
142c532df63SYohann //////////
143c532df63SYohann 
144c532df63SYohann inline __device__ void readDofs2d(const int elem, const int tidx,
145c532df63SYohann                                   const int tidy, const int comp,
146c532df63SYohann                                   const int nelem, const CeedScalar *d_U, CeedScalar &U) {
147c532df63SYohann   U = (tidx<P1D
148c532df63SYohann        && tidy<P1D) ? d_U[tidx + tidy*P1D + comp*P1D*P1D + elem*BASIS_NCOMP*P1D*P1D ] :
149c532df63SYohann       0.0;
150c532df63SYohann }
151c532df63SYohann 
152c532df63SYohann inline __device__ void writeDofs2d(const int elem, const int tidx,
153c532df63SYohann                                    const int tidy, const int comp,
154c532df63SYohann                                    const int nelem, const CeedScalar &r_V, CeedScalar *d_V) {
155c532df63SYohann   if (tidx<P1D && tidy<P1D) {
156c532df63SYohann     d_V[tidx + tidy*P1D + comp*P1D*P1D + elem*BASIS_NCOMP*P1D*P1D ] = r_V;
157c532df63SYohann   }
158c532df63SYohann }
159c532df63SYohann 
160c532df63SYohann inline __device__ void readQuads2d(const int elem, const int tidx,
161c532df63SYohann                                    const int tidy, const int comp,
162c532df63SYohann                                    const int dim, const int nelem, const CeedScalar *d_U, CeedScalar &U ) {
163c532df63SYohann   U = d_U[tidx + tidy*Q1D + elem*Q1D*Q1D + comp*Q1D*Q1D*nelem +
164c532df63SYohann                dim*BASIS_NCOMP*nelem*Q1D*Q1D];
165c532df63SYohann }
166c532df63SYohann 
167c532df63SYohann inline __device__ void writeQuads2d(const int elem, const int tidx,
168c532df63SYohann                                     const int tidy, const int comp,
169c532df63SYohann                                     const int dim, const int nelem, const CeedScalar &r_V, CeedScalar *d_V) {
170c532df63SYohann   d_V[tidx + tidy*Q1D + elem*Q1D*Q1D + comp*Q1D*Q1D*nelem +
171c532df63SYohann            dim*BASIS_NCOMP*nelem*Q1D*Q1D ] = r_V;
172c532df63SYohann }
173c532df63SYohann 
174c532df63SYohann inline __device__ void ContractX2d(CeedScalar *slice, const int tidx,
1754247ecf3SYohann Dudouit                                    const int tidy, const int tidz,
176c532df63SYohann                                    const CeedScalar &U, const CeedScalar *B, CeedScalar &V) {
1774247ecf3SYohann Dudouit   slice[tidx+tidy*Q1D+tidz*Q1D*Q1D] = U;
178c532df63SYohann   __syncthreads();
179c532df63SYohann   V = 0.0;
180c532df63SYohann   for (int i = 0; i < P1D; ++i) {
1814247ecf3SYohann Dudouit     V += B[i + tidx*P1D] * slice[i + tidy*Q1D + tidz*Q1D*Q1D];//contract x direction
182c532df63SYohann   }
183c532df63SYohann   __syncthreads();
184c532df63SYohann }
185c532df63SYohann 
186c532df63SYohann inline __device__ void ContractY2d(CeedScalar *slice, const int tidx,
1874247ecf3SYohann Dudouit                                    const int tidy, const int tidz,
188c532df63SYohann                                    const CeedScalar &U, const CeedScalar *B, CeedScalar &V) {
1894247ecf3SYohann Dudouit   slice[tidx+tidy*Q1D+tidz*Q1D*Q1D] = U;
190c532df63SYohann   __syncthreads();
191c532df63SYohann   V = 0.0;
192c532df63SYohann   for (int i = 0; i < P1D; ++i) {
1934247ecf3SYohann Dudouit     V += B[i + tidy*P1D] * slice[tidx + i*Q1D + tidz*Q1D*Q1D];//contract y direction
194c532df63SYohann   }
195c532df63SYohann   __syncthreads();
196c532df63SYohann }
197c532df63SYohann 
198c532df63SYohann inline __device__ void ContractTransposeY2d(CeedScalar *slice, const int tidx,
1994247ecf3SYohann Dudouit     const int tidy, const int tidz,
200c532df63SYohann     const CeedScalar &U, const CeedScalar *B, CeedScalar &V) {
2014247ecf3SYohann Dudouit   slice[tidx+tidy*Q1D+tidz*Q1D*Q1D] = U;
202c532df63SYohann   __syncthreads();
203c532df63SYohann   V = 0.0;
204c532df63SYohann   if (tidy<P1D) {
205c532df63SYohann     for (int i = 0; i < Q1D; ++i) {
2064247ecf3SYohann Dudouit       V += B[tidy + i*P1D] * slice[tidx + i*Q1D + tidz*Q1D*Q1D];//contract y direction
207c532df63SYohann     }
208c532df63SYohann   }
209c532df63SYohann   __syncthreads();
210c532df63SYohann }
211c532df63SYohann 
212c532df63SYohann inline __device__ void ContractTransposeX2d(CeedScalar *slice, const int tidx,
2134247ecf3SYohann Dudouit     const int tidy, const int tidz,
214c532df63SYohann     const CeedScalar &U, const CeedScalar *B, CeedScalar &V) {
2154247ecf3SYohann Dudouit   slice[tidx+tidy*Q1D+tidz*Q1D*Q1D] = U;
216c532df63SYohann   __syncthreads();
217c532df63SYohann   V = 0.0;
218c532df63SYohann   if (tidx<P1D) {
219c532df63SYohann     for (int i = 0; i < Q1D; ++i) {
2204247ecf3SYohann Dudouit       V += B[tidx + i*P1D] * slice[i + tidy*Q1D + tidz*Q1D*Q1D];//contract x direction
221c532df63SYohann     }
222c532df63SYohann   }
223c532df63SYohann   __syncthreads();
224c532df63SYohann }
225c532df63SYohann 
226c532df63SYohann inline __device__ void interp2d(const CeedInt nelem, const int transpose,
227c532df63SYohann                                 const CeedScalar *c_B, const CeedScalar *__restrict__ d_U,
228c532df63SYohann                                 CeedScalar *__restrict__ d_V,
229c532df63SYohann                                 CeedScalar *slice) {
230c532df63SYohann   CeedScalar r_V;
231c532df63SYohann   CeedScalar r_t;
232c532df63SYohann 
233c532df63SYohann   const int tidx = threadIdx.x;
234c532df63SYohann   const int tidy = threadIdx.y;
2354247ecf3SYohann Dudouit   const int tidz = threadIdx.z;
2364247ecf3SYohann Dudouit   const int blockElem = tidz/BASIS_NCOMP;
2374247ecf3SYohann Dudouit   const int elemsPerBlock = blockDim.z/BASIS_NCOMP;
2384247ecf3SYohann Dudouit   const int comp = tidz%BASIS_NCOMP;
239c532df63SYohann 
2404247ecf3SYohann Dudouit   for (CeedInt elem = blockIdx.x*elemsPerBlock + blockElem; elem < nelem;
2414247ecf3SYohann Dudouit        elem += gridDim.x*elemsPerBlock) {
2424247ecf3SYohann Dudouit     // for(int comp=0; comp<BASIS_NCOMP; comp++) {
2434247ecf3SYohann Dudouit       const int comp = tidz%BASIS_NCOMP;
244c532df63SYohann       r_V = 0.0;
245c532df63SYohann       r_t = 0.0;
246c532df63SYohann       if(!transpose) {
247c532df63SYohann         readDofs2d(elem, tidx, tidy, comp, nelem, d_U, r_V);
2484247ecf3SYohann Dudouit         ContractX2d(slice, tidx, tidy, tidz, r_V, c_B, r_t);
2494247ecf3SYohann Dudouit         ContractY2d(slice, tidx, tidy, tidz, r_t, c_B, r_V);
250c532df63SYohann         writeQuads2d(elem, tidx, tidy, comp, 0, nelem, r_V, d_V);
251c532df63SYohann       } else {
252c532df63SYohann         readQuads2d(elem, tidx, tidy, comp, 0, nelem, d_U, r_V);
2534247ecf3SYohann Dudouit         ContractTransposeY2d(slice, tidx, tidy, tidz, r_V, c_B, r_t);
2544247ecf3SYohann Dudouit         ContractTransposeX2d(slice, tidx, tidy, tidz, r_t, c_B, r_V);
255c532df63SYohann         writeDofs2d(elem, tidx, tidy, comp, nelem, r_V, d_V);
256c532df63SYohann       }
2574247ecf3SYohann Dudouit     // }
258c532df63SYohann   }
259c532df63SYohann }
260c532df63SYohann 
261c532df63SYohann inline __device__ void grad2d(const CeedInt nelem, const int transpose,
262c532df63SYohann                               const CeedScalar *c_B, const CeedScalar *c_G,
263c532df63SYohann                               const CeedScalar *__restrict__ d_U, CeedScalar *__restrict__ d_V,
264c532df63SYohann                               CeedScalar *slice) {
265c532df63SYohann   CeedScalar r_U;
266c532df63SYohann   CeedScalar r_V;
267c532df63SYohann   CeedScalar r_t;
268c532df63SYohann 
269c532df63SYohann   const int tidx = threadIdx.x;
270c532df63SYohann   const int tidy = threadIdx.y;
2714247ecf3SYohann Dudouit   const int tidz = threadIdx.z;
2724247ecf3SYohann Dudouit   const int blockElem = tidz/BASIS_NCOMP;
2734247ecf3SYohann Dudouit   const int elemsPerBlock = blockDim.z/BASIS_NCOMP;
2744247ecf3SYohann Dudouit   const int comp = tidz%BASIS_NCOMP;
275c532df63SYohann   int dim;
276c532df63SYohann 
2774247ecf3SYohann Dudouit   for (CeedInt elem = blockIdx.x*elemsPerBlock + blockElem; elem < nelem;
2784247ecf3SYohann Dudouit        elem += gridDim.x*elemsPerBlock) {
2794247ecf3SYohann Dudouit     // for(int comp=0; comp<BASIS_NCOMP; comp++) {
280c532df63SYohann       if(!transpose) {
281c532df63SYohann         readDofs2d(elem, tidx, tidy, comp, nelem, d_U, r_U);
2824247ecf3SYohann Dudouit         ContractX2d(slice, tidx, tidy, tidz, r_U, c_G, r_t);
2834247ecf3SYohann Dudouit         ContractY2d(slice, tidx, tidy, tidz, r_t, c_B, r_V);
284c532df63SYohann         dim = 0;
285c532df63SYohann         writeQuads2d(elem, tidx, tidy, comp, dim, nelem, r_V, d_V);
2864247ecf3SYohann Dudouit         ContractX2d(slice, tidx, tidy, tidz, r_U, c_B, r_t);
2874247ecf3SYohann Dudouit         ContractY2d(slice, tidx, tidy, tidz, r_t, c_G, r_V);
288c532df63SYohann         dim = 1;
289c532df63SYohann         writeQuads2d(elem, tidx, tidy, comp, dim, nelem, r_V, d_V);
290c532df63SYohann       } else {
291c532df63SYohann         dim = 0;
292c532df63SYohann         readQuads2d(elem, tidx, tidy, comp, dim, nelem, d_U, r_U);
2934247ecf3SYohann Dudouit         ContractTransposeY2d(slice, tidx, tidy, tidz, r_U, c_B, r_t);
2944247ecf3SYohann Dudouit         ContractTransposeX2d(slice, tidx, tidy, tidz, r_t, c_G, r_V);
295c532df63SYohann         dim = 1;
296c532df63SYohann         readQuads2d(elem, tidx, tidy, comp, dim, nelem, d_U, r_U);
2974247ecf3SYohann Dudouit         ContractTransposeY2d(slice, tidx, tidy, tidz, r_U, c_G, r_t);
2984247ecf3SYohann Dudouit         ContractTransposeX2d(slice, tidx, tidy, tidz, r_t, c_B, r_U);
299c532df63SYohann         r_V+=r_U;
300c532df63SYohann         writeDofs2d(elem, tidx, tidy, comp, nelem, r_V, d_V);
301c532df63SYohann       }
3024247ecf3SYohann Dudouit     // }
303c532df63SYohann   }
304c532df63SYohann }
305c532df63SYohann //////////
306c532df63SYohann //  3D  //
307c532df63SYohann //////////
308c532df63SYohann 
309c532df63SYohann inline __device__ void readDofs3d(const int elem, const int tidx,
310c532df63SYohann                                   const int tidy, const int comp,
311c532df63SYohann                                   const int nelem, const CeedScalar *d_U, CeedScalar *r_U) {
312c532df63SYohann   for (int i = 0; i < P1D; i++)
313c532df63SYohann     r_U[i] = (tidx<P1D
314c532df63SYohann               && tidy<P1D) ? d_U[tidx + tidy*P1D + i*P1D*P1D + comp*P1D*P1D*P1D +
315c532df63SYohann                                       elem*BASIS_NCOMP*P1D*P1D*P1D ] : 0.0;
316c532df63SYohann   for (int i = P1D; i < Q1D; i++)
317c532df63SYohann     r_U[i] = 0.0;
318c532df63SYohann }
319c532df63SYohann 
320c532df63SYohann inline __device__ void readQuads3d(const int elem, const int tidx,
321c532df63SYohann                                    const int tidy, const int comp,
322c532df63SYohann                                    const int dim, const int nelem, const CeedScalar *d_U, CeedScalar *r_U) {
323c532df63SYohann   for (int i = 0; i < Q1D; i++)
324c532df63SYohann     r_U[i] = d_U[tidx + tidy*Q1D + i*Q1D*Q1D + elem*Q1D*Q1D*Q1D +
325c532df63SYohann                  comp*Q1D*Q1D*Q1D*nelem + dim*BASIS_NCOMP*nelem*Q1D*Q1D*Q1D];
326c532df63SYohann }
327c532df63SYohann 
328c532df63SYohann inline __device__ void writeDofs3d(const int elem, const int tidx,
329c532df63SYohann                                    const int tidy, const int comp,
330c532df63SYohann                                    const int nelem, const CeedScalar *r_V, CeedScalar *d_V) {
331c532df63SYohann   if (tidx<P1D && tidy<P1D) {
332c532df63SYohann     for (int i = 0; i < P1D; i++)
333c532df63SYohann       d_V[tidx + tidy*P1D + i*P1D*P1D + comp*P1D*P1D*P1D +
334c532df63SYohann           elem*BASIS_NCOMP*P1D*P1D*P1D ] = r_V[i];
335c532df63SYohann   }
336c532df63SYohann }
337c532df63SYohann 
338c532df63SYohann inline __device__ void writeQuads3d(const int elem, const int tidx,
339c532df63SYohann                                     const int tidy, const int comp,
340c532df63SYohann                                     const int dim, const int nelem, const CeedScalar *r_V, CeedScalar *d_V) {
341c532df63SYohann   for (int i = 0; i < Q1D; i++)
342c532df63SYohann     d_V[tidx + tidy*Q1D + i*Q1D*Q1D + elem*Q1D*Q1D*Q1D + comp*Q1D*Q1D*Q1D*nelem +
343c532df63SYohann         dim*BASIS_NCOMP*nelem*Q1D*Q1D*Q1D ] = r_V[i];
344c532df63SYohann }
345c532df63SYohann 
346c532df63SYohann inline __device__ void ContractX3d(CeedScalar *slice, const int tidx,
347698ebc35SYohann Dudouit                                    const int tidy, const int tidz,
348c532df63SYohann                                    const CeedScalar *U, const CeedScalar *B, CeedScalar *V) {
349c532df63SYohann   for (int k = 0; k < P1D; ++k) {
350698ebc35SYohann Dudouit     slice[tidx+tidy*Q1D+tidz*Q1D*Q1D] = U[k];
351c532df63SYohann     __syncthreads();
352c532df63SYohann     V[k] = 0.0;
353c532df63SYohann     for (int i = 0; i < P1D; ++i) {
354698ebc35SYohann Dudouit       V[k] += B[i + tidx*P1D] * slice[i + tidy*Q1D + tidz*Q1D*Q1D];//contract x direction
355c532df63SYohann     }
356c532df63SYohann     __syncthreads();
357c532df63SYohann   }
358c532df63SYohann }
359c532df63SYohann 
360c532df63SYohann inline __device__ void ContractY3d(CeedScalar *slice, const int tidx,
361698ebc35SYohann Dudouit                                    const int tidy, const int tidz,
362c532df63SYohann                                    const CeedScalar *U, const CeedScalar *B, CeedScalar *V) {
363c532df63SYohann   for (int k = 0; k < P1D; ++k) {
364698ebc35SYohann Dudouit     slice[tidx+tidy*Q1D+tidz*Q1D*Q1D] = U[k];
365c532df63SYohann     __syncthreads();
366c532df63SYohann     V[k] = 0.0;
367c532df63SYohann     for (int i = 0; i < P1D; ++i) {
368698ebc35SYohann Dudouit       V[k] += B[i + tidy*P1D] * slice[tidx + i*Q1D + tidz*Q1D*Q1D];//contract y direction
369c532df63SYohann     }
370c532df63SYohann     __syncthreads();
371c532df63SYohann   }
372c532df63SYohann }
373c532df63SYohann 
374c532df63SYohann inline __device__ void ContractZ3d(CeedScalar *slice, const int tidx,
375698ebc35SYohann Dudouit                                    const int tidy, const int tidz,
376c532df63SYohann                                    const CeedScalar *U, const CeedScalar *B, CeedScalar *V) {
377c532df63SYohann   for (int k = 0; k < Q1D; ++k) {
378c532df63SYohann     V[k] = 0.0;
379c532df63SYohann     for (int i = 0; i < P1D; ++i) {
380c532df63SYohann       V[k] += B[i + k*P1D] * U[i];//contract z direction
381c532df63SYohann     }
382c532df63SYohann   }
383c532df63SYohann }
384c532df63SYohann 
385c532df63SYohann inline __device__ void ContractTransposeZ3d(CeedScalar *slice, const int tidx,
386698ebc35SYohann Dudouit     const int tidy, const int tidz,
387c532df63SYohann     const CeedScalar *U, const CeedScalar *B, CeedScalar *V) {
388c532df63SYohann   for (int k = 0; k < Q1D; ++k) {
389c532df63SYohann     V[k] = 0.0;
390c532df63SYohann     if (k<P1D) {
391c532df63SYohann       for (int i = 0; i < Q1D; ++i) {
392c532df63SYohann         V[k] += B[k + i*P1D] * U[i];//contract z direction
393c532df63SYohann       }
394c532df63SYohann     }
395c532df63SYohann   }
396c532df63SYohann }
397c532df63SYohann 
398c532df63SYohann inline __device__ void ContractTransposeY3d(CeedScalar *slice, const int tidx,
399698ebc35SYohann Dudouit     const int tidy, const int tidz,
400c532df63SYohann     const CeedScalar *U, const CeedScalar *B, CeedScalar *V) {
401c532df63SYohann   for (int k = 0; k < P1D; ++k) {
402698ebc35SYohann Dudouit     slice[tidx+tidy*Q1D+tidz*Q1D*Q1D] = U[k];
403c532df63SYohann     __syncthreads();
404c532df63SYohann     V[k] = 0.0;
405c532df63SYohann     if (tidy<P1D) {
406c532df63SYohann       for (int i = 0; i < Q1D; ++i) {
407698ebc35SYohann Dudouit         V[k] += B[tidy + i*P1D] * slice[tidx + i*Q1D + tidz*Q1D*Q1D];//contract y direction
408c532df63SYohann       }
409c532df63SYohann     }
410c532df63SYohann     __syncthreads();
411c532df63SYohann   }
412c532df63SYohann }
413c532df63SYohann 
414c532df63SYohann inline __device__ void ContractTransposeX3d(CeedScalar *slice, const int tidx,
415698ebc35SYohann Dudouit     const int tidy, const int tidz,
416c532df63SYohann     const CeedScalar *U, const CeedScalar *B, CeedScalar *V) {
417c532df63SYohann   for (int k = 0; k < P1D; ++k) {
418698ebc35SYohann Dudouit     slice[tidx+tidy*Q1D+tidz*Q1D*Q1D] = U[k];
419c532df63SYohann     __syncthreads();
420c532df63SYohann     V[k] = 0.0;
421c532df63SYohann     if (tidx<P1D) {
422c532df63SYohann       for (int i = 0; i < Q1D; ++i) {
423698ebc35SYohann Dudouit         V[k] += B[tidx + i*P1D] * slice[i + tidy*Q1D + tidz*Q1D*Q1D];//contract x direction
424c532df63SYohann       }
425c532df63SYohann     }
426c532df63SYohann     __syncthreads();
427c532df63SYohann   }
428c532df63SYohann }
429c532df63SYohann 
430c532df63SYohann inline __device__ void interp3d(const CeedInt nelem, const int transpose,
431c532df63SYohann                                 const CeedScalar *c_B, const CeedScalar *__restrict__ d_U,
432c532df63SYohann                                 CeedScalar *__restrict__ d_V,
433c532df63SYohann                                 CeedScalar *slice) {
434c532df63SYohann   CeedScalar r_V[Q1D];
435c532df63SYohann   CeedScalar r_t[Q1D];
436c532df63SYohann 
437c532df63SYohann   const int tidx = threadIdx.x;
438c532df63SYohann   const int tidy = threadIdx.y;
439698ebc35SYohann Dudouit   const int tidz = threadIdx.z;
440698ebc35SYohann Dudouit   const int blockElem = tidz/BASIS_NCOMP;
441698ebc35SYohann Dudouit   const int elemsPerBlock = blockDim.z/BASIS_NCOMP;
442698ebc35SYohann Dudouit   const int comp = tidz%BASIS_NCOMP;
443c532df63SYohann 
444698ebc35SYohann Dudouit   for (CeedInt elem = blockIdx.x*elemsPerBlock + blockElem; elem < nelem;
445698ebc35SYohann Dudouit        elem += gridDim.x*elemsPerBlock) {
446698ebc35SYohann Dudouit     // for(int comp=0; comp<BASIS_NCOMP; comp++) {
447c532df63SYohann       for (int i = 0; i < Q1D; ++i) {
448c532df63SYohann         r_V[i] = 0.0;
449c532df63SYohann         r_t[i] = 0.0;
450c532df63SYohann       }
451c532df63SYohann       if(!transpose) {
452c532df63SYohann         readDofs3d(elem, tidx, tidy, comp, nelem, d_U, r_V);
453698ebc35SYohann Dudouit         ContractX3d(slice, tidx, tidy, tidz, r_V, c_B, r_t);
454698ebc35SYohann Dudouit         ContractY3d(slice, tidx, tidy, tidz, r_t, c_B, r_V);
455698ebc35SYohann Dudouit         ContractZ3d(slice, tidx, tidy, tidz, r_V, c_B, r_t);
456c532df63SYohann         writeQuads3d(elem, tidx, tidy, comp, 0, nelem, r_t, d_V);
457c532df63SYohann       } else {
458c532df63SYohann         readQuads3d(elem, tidx, tidy, comp, 0, nelem, d_U, r_V);
459698ebc35SYohann Dudouit         ContractTransposeZ3d(slice, tidx, tidy, tidz, r_V, c_B, r_t);
460698ebc35SYohann Dudouit         ContractTransposeY3d(slice, tidx, tidy, tidz, r_t, c_B, r_V);
461698ebc35SYohann Dudouit         ContractTransposeX3d(slice, tidx, tidy, tidz, r_V, c_B, r_t);
462c532df63SYohann         writeDofs3d(elem, tidx, tidy, comp, nelem, r_t, d_V);
463c532df63SYohann       }
464698ebc35SYohann Dudouit     // }
465c532df63SYohann   }
466c532df63SYohann }
467c532df63SYohann 
468c532df63SYohann inline __device__ void grad3d(const CeedInt nelem, const int transpose,
469c532df63SYohann                               const CeedScalar *c_B, const CeedScalar *c_G,
470c532df63SYohann                               const CeedScalar *__restrict__ d_U, CeedScalar *__restrict__ d_V,
471c532df63SYohann                               CeedScalar *slice) {
472c532df63SYohann   //use P1D for one of these
473c532df63SYohann   CeedScalar r_U[Q1D];
474c532df63SYohann   CeedScalar r_V[Q1D];
475c532df63SYohann   CeedScalar r_t[Q1D];
476c532df63SYohann 
477c532df63SYohann   const int tidx = threadIdx.x;
478c532df63SYohann   const int tidy = threadIdx.y;
479698ebc35SYohann Dudouit   const int tidz = threadIdx.z;
480698ebc35SYohann Dudouit   const int blockElem = tidz/BASIS_NCOMP;
481698ebc35SYohann Dudouit   const int elemsPerBlock = blockDim.z/BASIS_NCOMP;
482698ebc35SYohann Dudouit   const int comp = tidz%BASIS_NCOMP;
483c532df63SYohann   int dim;
484c532df63SYohann 
485698ebc35SYohann Dudouit   for (CeedInt elem = blockIdx.x*elemsPerBlock + blockElem; elem < nelem;
486698ebc35SYohann Dudouit        elem += gridDim.x*elemsPerBlock) {
487698ebc35SYohann Dudouit     // for(int comp=0; comp<BASIS_NCOMP; comp++) {
488c532df63SYohann       if(!transpose) {
489c532df63SYohann         readDofs3d(elem, tidx, tidy, comp, nelem, d_U, r_U);
490698ebc35SYohann Dudouit         ContractX3d(slice, tidx, tidy, tidz, r_U, c_G, r_V);
491698ebc35SYohann Dudouit         ContractY3d(slice, tidx, tidy, tidz, r_V, c_B, r_t);
492698ebc35SYohann Dudouit         ContractZ3d(slice, tidx, tidy, tidz, r_t, c_B, r_V);
493c532df63SYohann         dim = 0;
494c532df63SYohann         writeQuads3d(elem, tidx, tidy, comp, dim, nelem, r_V, d_V);
495698ebc35SYohann Dudouit         ContractX3d(slice, tidx, tidy, tidz, r_U, c_B, r_V);
496698ebc35SYohann Dudouit         ContractY3d(slice, tidx, tidy, tidz, r_V, c_G, r_t);
497698ebc35SYohann Dudouit         ContractZ3d(slice, tidx, tidy, tidz, r_t, c_B, r_V);
498c532df63SYohann         dim = 1;
499c532df63SYohann         writeQuads3d(elem, tidx, tidy, comp, dim, nelem, r_V, d_V);
500698ebc35SYohann Dudouit         ContractX3d(slice, tidx, tidy, tidz, r_U, c_B, r_V);
501698ebc35SYohann Dudouit         ContractY3d(slice, tidx, tidy, tidz, r_V, c_B, r_t);
502698ebc35SYohann Dudouit         ContractZ3d(slice, tidx, tidy, tidz, r_t, c_G, r_V);
503c532df63SYohann         dim = 2;
504c532df63SYohann         writeQuads3d(elem, tidx, tidy, comp, dim, nelem, r_V, d_V);
505c532df63SYohann       } else {
506c532df63SYohann         dim = 0;
507c532df63SYohann         readQuads3d(elem, tidx, tidy, comp, dim, nelem, d_U, r_U);
508698ebc35SYohann Dudouit         ContractTransposeZ3d(slice, tidx, tidy, tidz, r_U, c_B, r_t);
509698ebc35SYohann Dudouit         ContractTransposeY3d(slice, tidx, tidy, tidz, r_t, c_B, r_U);
510698ebc35SYohann Dudouit         ContractTransposeX3d(slice, tidx, tidy, tidz, r_U, c_G, r_V);
511c532df63SYohann         dim = 1;
512c532df63SYohann         readQuads3d(elem, tidx, tidy, comp, dim, nelem, d_U, r_U);
513698ebc35SYohann Dudouit         ContractTransposeZ3d(slice, tidx, tidy, tidz, r_U, c_B, r_t);
514698ebc35SYohann Dudouit         ContractTransposeY3d(slice, tidx, tidy, tidz, r_t, c_G, r_U);
515698ebc35SYohann Dudouit         ContractTransposeX3d(slice, tidx, tidy, tidz, r_U, c_B, r_t);
516c532df63SYohann         add(r_V, r_t);
517c532df63SYohann         dim = 2;
518c532df63SYohann         readQuads3d(elem, tidx, tidy, comp, dim, nelem, d_U, r_U);
519698ebc35SYohann Dudouit         ContractTransposeZ3d(slice, tidx, tidy, tidz, r_U, c_G, r_t);
520698ebc35SYohann Dudouit         ContractTransposeY3d(slice, tidx, tidy, tidz, r_t, c_B, r_U);
521698ebc35SYohann Dudouit         ContractTransposeX3d(slice, tidx, tidy, tidz, r_U, c_B, r_t);
522c532df63SYohann         add(r_V, r_t);
523c532df63SYohann         writeDofs3d(elem, tidx, tidy, comp, nelem, r_V, d_V);
524c532df63SYohann       }
525698ebc35SYohann Dudouit     // }
526c532df63SYohann   }
527c532df63SYohann }
528c532df63SYohann 
529c532df63SYohann /////////////
530c532df63SYohann // Kernels //
531c532df63SYohann /////////////
532c532df63SYohann extern "C" __global__ void interp(const CeedInt nelem, const int transpose,
533c532df63SYohann                                   const CeedScalar *c_B, const CeedScalar *__restrict__ d_U,
534c532df63SYohann                                   CeedScalar *__restrict__ d_V) {
535074be161SYohann Dudouit   // __shared__ double slice[Q1D*Q1D];//Fix me if ElemPerBlock>1
536074be161SYohann Dudouit   extern __shared__ double slice[];
537c532df63SYohann   if (BASIS_DIM==1) {
538c532df63SYohann     interp1d(nelem, transpose, c_B, d_U, d_V, slice);
539c532df63SYohann   } else if (BASIS_DIM==2) {
540c532df63SYohann     interp2d(nelem, transpose, c_B, d_U, d_V, slice);
541c532df63SYohann   } else if (BASIS_DIM==3) {
542c532df63SYohann     interp3d(nelem, transpose, c_B, d_U, d_V, slice);
543c532df63SYohann   }
544c532df63SYohann }
545c532df63SYohann 
546c532df63SYohann extern "C" __global__ void grad(const CeedInt nelem, const int transpose,
547c532df63SYohann                                 const CeedScalar *c_B, const CeedScalar *c_G,
548c532df63SYohann                                 const CeedScalar *__restrict__ d_U, CeedScalar *__restrict__ d_V) {
549074be161SYohann Dudouit   // __shared__ double slice[Q1D*Q1D];//Fix me if ElemPerBlock>1
550074be161SYohann Dudouit   extern __shared__ double slice[];
551c532df63SYohann   if (BASIS_DIM==1) {
552c532df63SYohann     grad1d(nelem, transpose, c_B, c_G, d_U, d_V, slice);
553c532df63SYohann   } else if (BASIS_DIM==2) {
554c532df63SYohann     grad2d(nelem, transpose, c_B, c_G, d_U, d_V, slice);
555c532df63SYohann   } else if (BASIS_DIM==3) {
556c532df63SYohann     grad3d(nelem, transpose, c_B, c_G, d_U, d_V, slice);
557c532df63SYohann   }
558c532df63SYohann }
559c532df63SYohann 
560c532df63SYohann /////////////
561c532df63SYohann // Weights //
562c532df63SYohann /////////////
563c532df63SYohann __device__ void weight1d(const CeedInt nelem, const CeedScalar *qweight1d,
564c532df63SYohann                          CeedScalar *w) {
565074be161SYohann Dudouit   const int tid = threadIdx.x;
566074be161SYohann Dudouit   const CeedScalar weight = qweight1d[tid];
567074be161SYohann Dudouit   for (CeedInt elem = blockIdx.x*blockDim.y + threadIdx.y; elem < nelem;
568074be161SYohann Dudouit        elem += gridDim.x*blockDim.y) {
569074be161SYohann Dudouit     const int ind = elem*Q1D + tid;
570074be161SYohann Dudouit     w[ind] = weight;
571c532df63SYohann   }
572c532df63SYohann }
573c532df63SYohann 
574c532df63SYohann __device__ void weight2d(const CeedInt nelem, const CeedScalar *qweight1d,
575c532df63SYohann                          CeedScalar *w) {
576074be161SYohann Dudouit   const int i = threadIdx.x;
577074be161SYohann Dudouit   const int j = threadIdx.y;
578074be161SYohann Dudouit   const CeedScalar weight = qweight1d[i]*qweight1d[j];
579074be161SYohann Dudouit   for (CeedInt elem = blockIdx.x*blockDim.z + threadIdx.z; elem < nelem;
580074be161SYohann Dudouit        elem += gridDim.x*blockDim.z) {
581074be161SYohann Dudouit     const int ind = elem*Q1D*Q1D + i + j*Q1D;
582074be161SYohann Dudouit     w[ind] = weight;
583c532df63SYohann   }
584c532df63SYohann }
585c532df63SYohann 
586c532df63SYohann __device__ void weight3d(const CeedInt nelem, const CeedScalar *qweight1d,
587c532df63SYohann                          CeedScalar *w) {
588074be161SYohann Dudouit   const int i = threadIdx.x;
589074be161SYohann Dudouit   const int j = threadIdx.y;
590074be161SYohann Dudouit   const int k = threadIdx.z;
591074be161SYohann Dudouit   const CeedScalar weight = qweight1d[i]*qweight1d[j]*qweight1d[k];
592074be161SYohann Dudouit   for (int e = blockIdx.x; e < nelem; e += gridDim.x) {
593074be161SYohann Dudouit     const int ind = e*Q1D*Q1D*Q1D + i + j*Q1D + k*Q1D*Q1D;
594074be161SYohann Dudouit     w[ind] = weight;
595c532df63SYohann   }
596c532df63SYohann }
597c532df63SYohann 
598c532df63SYohann extern "C" __global__ void weight(const CeedInt nelem,
599c532df63SYohann                                   const CeedScalar *__restrict__ qweight1d, CeedScalar *__restrict__ v) {
600c532df63SYohann   if (BASIS_DIM==1) {
601c532df63SYohann     weight1d(nelem, qweight1d, v);
602c532df63SYohann   } else if (BASIS_DIM==2) {
603c532df63SYohann     weight2d(nelem, qweight1d, v);
604c532df63SYohann   } else if (BASIS_DIM==3) {
605c532df63SYohann     weight3d(nelem, qweight1d, v);
606c532df63SYohann   }
607c532df63SYohann }
608c532df63SYohann 
609c532df63SYohann                                    );
610c532df63SYohann 
611c532df63SYohann int CeedCudaInitInterp(CeedScalar *d_B, CeedInt P1d, CeedInt Q1d,
612c532df63SYohann                        CeedScalar **c_B);
613c532df63SYohann int CeedCudaInitInterpGrad(CeedScalar *d_B, CeedScalar *d_G, CeedInt P1d,
614c532df63SYohann                            CeedInt Q1d, CeedScalar **c_B_ptr, CeedScalar **c_G_ptr);
615c532df63SYohann 
616c532df63SYohann int CeedBasisApplyTensor_Cuda_shared(CeedBasis basis, const CeedInt nelem,
617c532df63SYohann                                      CeedTransposeMode tmode,
618c532df63SYohann                                      CeedEvalMode emode, CeedVector u, CeedVector v) {
619c532df63SYohann   int ierr;
620c532df63SYohann   Ceed ceed;
621c532df63SYohann   ierr = CeedBasisGetCeed(basis, &ceed); CeedChk(ierr);
622c532df63SYohann   Ceed_Cuda_shared *ceed_Cuda;
623c532df63SYohann   CeedGetData(ceed, (void *) &ceed_Cuda); CeedChk(ierr);
624c532df63SYohann   CeedBasis_Cuda_shared *data;
625c532df63SYohann   CeedBasisGetData(basis, (void *)&data); CeedChk(ierr);
626c532df63SYohann   const CeedInt transpose = tmode == CEED_TRANSPOSE;
6274247ecf3SYohann Dudouit   CeedInt dim, ncomp;
628074be161SYohann Dudouit   ierr = CeedBasisGetDimension(basis, &dim); CeedChk(ierr);
6294247ecf3SYohann Dudouit   ierr = CeedBasisGetNumComponents(basis, &ncomp); CeedChk(ierr);
630c532df63SYohann 
631c532df63SYohann   const CeedScalar *d_u;
632c532df63SYohann   CeedScalar *d_v;
633c532df63SYohann   if(emode!=CEED_EVAL_WEIGHT) {
634c532df63SYohann     ierr = CeedVectorGetArrayRead(u, CEED_MEM_DEVICE, &d_u); CeedChk(ierr);
635c532df63SYohann   }
636c532df63SYohann   ierr = CeedVectorGetArray(v, CEED_MEM_DEVICE, &d_v); CeedChk(ierr);
637c532df63SYohann 
638c532df63SYohann   if (tmode == CEED_TRANSPOSE) {
639c532df63SYohann     CeedInt length;
640c532df63SYohann     ierr = CeedVectorGetLength(v, &length); CeedChk(ierr);
641c532df63SYohann     ierr = cudaMemset(d_v, 0, length * sizeof(CeedScalar)); CeedChk(ierr);
642c532df63SYohann   }
643c532df63SYohann   if (emode == CEED_EVAL_INTERP) {
644c532df63SYohann     //TODO: check performance difference between c_B and d_B
645c532df63SYohann     CeedInt P1d, Q1d;
646c532df63SYohann     ierr = CeedBasisGetNumNodes1D(basis, &P1d); CeedChk(ierr);
647c532df63SYohann     ierr = CeedBasisGetNumQuadraturePoints1D(basis, &Q1d); CeedChk(ierr);
648074be161SYohann Dudouit     // if (ceed_Cuda->Q1d != Q1d || ceed_Cuda->P1d != P1d)
649074be161SYohann Dudouit     // {
650074be161SYohann Dudouit     //   ceed_Cuda->Q1d = Q1d;
651074be161SYohann Dudouit     //   ceed_Cuda->P1d = P1d;
652074be161SYohann Dudouit     //   ceed_Cuda->grad = false;
653c532df63SYohann       ierr = CeedCudaInitInterp(data->d_interp1d, P1d, Q1d, &data->c_B);
654c532df63SYohann       CeedChk(ierr);
655074be161SYohann Dudouit     // }
656c532df63SYohann     void *interpargs[] = {(void *) &nelem, (void *) &transpose, &data->c_B, &d_u, &d_v};
657074be161SYohann Dudouit     // void *interpargs[] = {(void *) &nelem, (void *) &transpose, &data->d_interp1d, &d_u, &d_v};
658074be161SYohann Dudouit     if (dim==1)
659074be161SYohann Dudouit     {
660d94769d2SYohann Dudouit       CeedInt elemsPerBlock = 32;
6614247ecf3SYohann Dudouit       CeedInt grid = nelem/elemsPerBlock + ( (nelem/elemsPerBlock*elemsPerBlock<nelem)? 1 : 0 );
662d94769d2SYohann Dudouit       CeedInt sharedMem = elemsPerBlock*Q1d*sizeof(CeedScalar);
663074be161SYohann Dudouit       ierr = run_kernel_dim_shared(ceed, data->interp, grid, Q1d, 1, elemsPerBlock, sharedMem,
664c532df63SYohann                             interpargs);
665c532df63SYohann       CeedChk(ierr);
666074be161SYohann Dudouit     } else if (dim==2) {
6674247ecf3SYohann Dudouit       const CeedInt optElems[7] = {0,32,8,6,4,2,8};
6684247ecf3SYohann Dudouit       CeedInt elemsPerBlock = Q1d < 7 ? optElems[Q1d]/ncomp : 1;
6694247ecf3SYohann Dudouit       CeedInt grid = nelem/elemsPerBlock + ( (nelem/elemsPerBlock*elemsPerBlock<nelem)? 1 : 0 );
6704247ecf3SYohann Dudouit       CeedInt sharedMem = ncomp*elemsPerBlock*Q1d*Q1d*sizeof(CeedScalar);
6714247ecf3SYohann Dudouit       ierr = run_kernel_dim_shared(ceed, data->interp, grid, Q1d, Q1d, ncomp*elemsPerBlock, sharedMem,
672074be161SYohann Dudouit                             interpargs);
673074be161SYohann Dudouit       CeedChk(ierr);
674074be161SYohann Dudouit     } else if (dim==3) {
675*3f63d318SYohann Dudouit       CeedInt elemsPerBlock = 1;
6764247ecf3SYohann Dudouit       CeedInt grid = nelem/elemsPerBlock + ( (nelem/elemsPerBlock*elemsPerBlock<nelem)? 1 : 0 );
677698ebc35SYohann Dudouit       CeedInt sharedMem = ncomp*elemsPerBlock*Q1d*Q1d*sizeof(CeedScalar);
678698ebc35SYohann Dudouit       ierr = run_kernel_dim_shared(ceed, data->interp, grid, Q1d, Q1d, ncomp*elemsPerBlock, sharedMem,
679074be161SYohann Dudouit                             interpargs);
680074be161SYohann Dudouit       CeedChk(ierr);
681074be161SYohann Dudouit     }
682c532df63SYohann   } else if (emode == CEED_EVAL_GRAD) {
683c532df63SYohann     CeedInt P1d, Q1d;
684c532df63SYohann     ierr = CeedBasisGetNumNodes1D(basis, &P1d); CeedChk(ierr);
685c532df63SYohann     ierr = CeedBasisGetNumQuadraturePoints1D(basis, &Q1d); CeedChk(ierr);
686074be161SYohann Dudouit     // if (ceed_Cuda->Q1d != Q1d || ceed_Cuda->P1d != P1d || !data->grad)
687074be161SYohann Dudouit     // {
688074be161SYohann Dudouit     //   ceed_Cuda->Q1d = Q1d;
689074be161SYohann Dudouit     //   ceed_Cuda->P1d = P1d;
690074be161SYohann Dudouit     //   ceed_Cuda->grad = true;
691c532df63SYohann       ierr = CeedCudaInitInterpGrad(data->d_interp1d, data->d_grad1d, P1d,
692c532df63SYohann                                     Q1d, &data->c_B, &data->c_G);
693c532df63SYohann       CeedChk(ierr);
694074be161SYohann Dudouit     // }
695c532df63SYohann     void *gradargs[] = {(void *) &nelem, (void *) &transpose, &data->c_B, &data->c_G, &d_u, &d_v};
696074be161SYohann Dudouit     // void *gradargs[] = {(void *) &nelem, (void *) &transpose, &data->d_interp1d, &data->d_grad1d, &d_u, &d_v};
697074be161SYohann Dudouit     if (dim==1)
698074be161SYohann Dudouit     {
699d94769d2SYohann Dudouit       CeedInt elemsPerBlock = 32;
7004247ecf3SYohann Dudouit       CeedInt grid = nelem/elemsPerBlock + ( (nelem/elemsPerBlock*elemsPerBlock<nelem)? 1 : 0 );
701d94769d2SYohann Dudouit       CeedInt sharedMem = elemsPerBlock*Q1d*sizeof(CeedScalar);
702074be161SYohann Dudouit       ierr = run_kernel_dim_shared(ceed, data->grad, grid, Q1d, 1, elemsPerBlock, sharedMem,
703c532df63SYohann                           gradargs);
704c532df63SYohann       CeedChk(ierr);
705074be161SYohann Dudouit     } else if (dim==2) {
7064247ecf3SYohann Dudouit       const CeedInt optElems[7] = {0,32,8,6,4,2,8};
7074247ecf3SYohann Dudouit       CeedInt elemsPerBlock = Q1d < 7 ? optElems[Q1d]/ncomp : 1;
7084247ecf3SYohann Dudouit       CeedInt grid = nelem/elemsPerBlock + ( (nelem/elemsPerBlock*elemsPerBlock<nelem)? 1 : 0 );
7094247ecf3SYohann Dudouit       CeedInt sharedMem = ncomp*elemsPerBlock*Q1d*Q1d*sizeof(CeedScalar);
7104247ecf3SYohann Dudouit       ierr = run_kernel_dim_shared(ceed, data->grad, grid, Q1d, Q1d, ncomp*elemsPerBlock, sharedMem,
711074be161SYohann Dudouit                           gradargs);
712074be161SYohann Dudouit       CeedChk(ierr);
713074be161SYohann Dudouit     } else if (dim==3) {
714*3f63d318SYohann Dudouit       CeedInt elemsPerBlock = 1;
7154247ecf3SYohann Dudouit       CeedInt grid = nelem/elemsPerBlock + ( (nelem/elemsPerBlock*elemsPerBlock<nelem)? 1 : 0 );
716698ebc35SYohann Dudouit       CeedInt sharedMem = ncomp*elemsPerBlock*Q1d*Q1d*sizeof(CeedScalar);
717698ebc35SYohann Dudouit       ierr = run_kernel_dim_shared(ceed, data->grad, grid, Q1d, Q1d, ncomp*elemsPerBlock, sharedMem,
718074be161SYohann Dudouit                           gradargs);
719074be161SYohann Dudouit       CeedChk(ierr);
720074be161SYohann Dudouit     }
721c532df63SYohann   } else if (emode == CEED_EVAL_WEIGHT) {
722074be161SYohann Dudouit     CeedInt Q1d;
723074be161SYohann Dudouit     ierr = CeedBasisGetNumQuadraturePoints1D(basis, &Q1d); CeedChk(ierr);
724c532df63SYohann     void *weightargs[] = {(void *) &nelem, (void *) &data->d_qweight1d, &d_v};
725074be161SYohann Dudouit     if(dim==1){
726074be161SYohann Dudouit       const CeedInt elemsPerBlock = 32/Q1d;
727074be161SYohann Dudouit       const CeedInt gridsize = nelem/elemsPerBlock + ( (nelem/elemsPerBlock*elemsPerBlock<nelem)? 1 : 0 );
728074be161SYohann Dudouit       ierr = run_kernel_dim(ceed, data->weight, gridsize, Q1d, elemsPerBlock, 1, weightargs);
729074be161SYohann Dudouit     } else if(dim==2) {
730717ff8a3SYohann Dudouit       const CeedInt optElems = 32/(Q1d*Q1d);
731717ff8a3SYohann Dudouit       const CeedInt elemsPerBlock = optElems>0?optElems:1;
732074be161SYohann Dudouit       const CeedInt gridsize = nelem/elemsPerBlock + ( (nelem/elemsPerBlock*elemsPerBlock<nelem)? 1 : 0 );
733074be161SYohann Dudouit       ierr = run_kernel_dim(ceed, data->weight, gridsize, Q1d, Q1d, elemsPerBlock, weightargs);
734074be161SYohann Dudouit     } else if(dim==3) {
735074be161SYohann Dudouit       const CeedInt gridsize = nelem;
736074be161SYohann Dudouit       ierr = run_kernel_dim(ceed, data->weight, gridsize, Q1d, Q1d, Q1d, weightargs);
737074be161SYohann Dudouit     }
738c532df63SYohann   }
739c532df63SYohann 
740c532df63SYohann   if(emode!=CEED_EVAL_WEIGHT) {
741c532df63SYohann     ierr = CeedVectorRestoreArrayRead(u, &d_u); CeedChk(ierr);
742c532df63SYohann   }
743c532df63SYohann   ierr = CeedVectorRestoreArray(v, &d_v); CeedChk(ierr);
744c532df63SYohann 
745c532df63SYohann   return 0;
746c532df63SYohann }
747c532df63SYohann 
748c532df63SYohann static int CeedBasisDestroy_Cuda_shared(CeedBasis basis) {
749c532df63SYohann   int ierr;
750c532df63SYohann   Ceed ceed;
751c532df63SYohann   ierr = CeedBasisGetCeed(basis, &ceed); CeedChk(ierr);
752c532df63SYohann 
753c532df63SYohann   CeedBasis_Cuda_shared *data;
754c532df63SYohann   ierr = CeedBasisGetData(basis, (void *) &data); CeedChk(ierr);
755c532df63SYohann 
756c532df63SYohann   CeedChk_Cu(ceed, cuModuleUnload(data->module));
757c532df63SYohann 
758c532df63SYohann   ierr = cudaFree(data->d_qweight1d); CeedChk_Cu(ceed, ierr);
759c532df63SYohann   ierr = cudaFree(data->d_interp1d); CeedChk_Cu(ceed, ierr);
760c532df63SYohann   ierr = cudaFree(data->d_grad1d); CeedChk_Cu(ceed, ierr);
761c532df63SYohann 
762c532df63SYohann   ierr = CeedFree(&data); CeedChk(ierr);
763c532df63SYohann 
764c532df63SYohann   return 0;
765c532df63SYohann }
766c532df63SYohann 
767c532df63SYohann int CeedBasisCreateTensorH1_Cuda_shared(CeedInt dim, CeedInt P1d, CeedInt Q1d,
768c532df63SYohann                                         const CeedScalar *interp1d,
769c532df63SYohann                                         const CeedScalar *grad1d,
770c532df63SYohann                                         const CeedScalar *qref1d,
771c532df63SYohann                                         const CeedScalar *qweight1d,
772c532df63SYohann                                         CeedBasis basis) {
773c532df63SYohann   int ierr;
774c532df63SYohann   Ceed ceed;
775c532df63SYohann   ierr = CeedBasisGetCeed(basis, &ceed); CeedChk(ierr);
776c532df63SYohann   CeedBasis_Cuda_shared *data;
777c532df63SYohann   ierr = CeedCalloc(1, &data); CeedChk(ierr);
778c532df63SYohann 
779c532df63SYohann   const CeedInt qBytes = Q1d * sizeof(CeedScalar);
780c532df63SYohann   ierr = cudaMalloc((void **)&data->d_qweight1d, qBytes); CeedChk_Cu(ceed, ierr);
781c532df63SYohann   ierr = cudaMemcpy(data->d_qweight1d, qweight1d, qBytes,
782c532df63SYohann                     cudaMemcpyHostToDevice); CeedChk_Cu(ceed, ierr);
783c532df63SYohann 
784c532df63SYohann   const CeedInt iBytes = qBytes * P1d;
785c532df63SYohann   ierr = cudaMalloc((void **)&data->d_interp1d, iBytes); CeedChk_Cu(ceed, ierr);
786c532df63SYohann   ierr = cudaMemcpy(data->d_interp1d, interp1d, iBytes,
787c532df63SYohann                     cudaMemcpyHostToDevice); CeedChk_Cu(ceed, ierr);
788c532df63SYohann 
789c532df63SYohann   ierr = cudaMalloc((void **)&data->d_grad1d, iBytes); CeedChk_Cu(ceed, ierr);
790c532df63SYohann   ierr = cudaMemcpy(data->d_grad1d, grad1d, iBytes,
791c532df63SYohann                     cudaMemcpyHostToDevice); CeedChk_Cu(ceed, ierr);
792c532df63SYohann 
793c532df63SYohann   CeedInt ncomp;
794c532df63SYohann   ierr = CeedBasisGetNumComponents(basis, &ncomp); CeedChk(ierr);
795c532df63SYohann   ierr = compile(ceed, kernelsShared, &data->module, 7,
796c532df63SYohann                  "Q1D", Q1d,
797c532df63SYohann                  "P1D", P1d,
798c532df63SYohann                  "BASIS_BUF_LEN", ncomp * CeedIntPow(Q1d > P1d ?
799c532df63SYohann                      Q1d : P1d, dim),
800c532df63SYohann                  "BASIS_DIM", dim,
801c532df63SYohann                  "BASIS_NCOMP", ncomp,
802c532df63SYohann                  "BASIS_ELEMSIZE", CeedIntPow(P1d, dim),
803c532df63SYohann                  "BASIS_NQPT", CeedIntPow(Q1d, dim)
804c532df63SYohann                 ); CeedChk(ierr);
805c532df63SYohann   ierr = get_kernel(ceed, data->module, "interp", &data->interp);
806c532df63SYohann   CeedChk(ierr);
807c532df63SYohann   ierr = get_kernel(ceed, data->module, "grad", &data->grad);
808c532df63SYohann   CeedChk(ierr);
809c532df63SYohann   ierr = get_kernel(ceed, data->module, "weight", &data->weight);
810c532df63SYohann   CeedChk(ierr);
811c532df63SYohann 
812c532df63SYohann   ierr = CeedBasisSetData(basis, (void *)&data);
813c532df63SYohann   CeedChk(ierr);
814c532df63SYohann   ierr = CeedSetBackendFunction(ceed, "Basis", basis, "Apply",
815c532df63SYohann                                 CeedBasisApplyTensor_Cuda_shared);
816c532df63SYohann   CeedChk(ierr);
817c532df63SYohann   ierr = CeedSetBackendFunction(ceed, "Basis", basis, "Destroy",
818c532df63SYohann                                 CeedBasisDestroy_Cuda_shared);
819c532df63SYohann   CeedChk(ierr);
820c532df63SYohann   return 0;
821c532df63SYohann }
822c532df63SYohann 
823c532df63SYohann int CeedBasisCreateH1_Cuda_shared(CeedElemTopology topo, CeedInt dim,
824c532df63SYohann                                   CeedInt ndof, CeedInt nqpts,
825c532df63SYohann                                   const CeedScalar *interp,
826c532df63SYohann                                   const CeedScalar *grad,
827c532df63SYohann                                   const CeedScalar *qref,
828c532df63SYohann                                   const CeedScalar *qweight,
829c532df63SYohann                                   CeedBasis basis) {
830c532df63SYohann   int ierr;
831c532df63SYohann   Ceed ceed;
832c532df63SYohann   ierr = CeedBasisGetCeed(basis, &ceed); CeedChk(ierr);
833c532df63SYohann   return CeedError(ceed, 1, "Backend does not implement generic H1 basis");
834c532df63SYohann }
835