xref: /petsc/src/ts/utils/dmplexlandau/kokkos/landau.kokkos.cxx (revision 8a6f2e61db23672ba91ff51cce60bcb02422d81f)
1e0eea495SMark /*
2e0eea495SMark   Implements the Kokkos kernel
3e0eea495SMark */
4e0eea495SMark #include <petscconf.h>
511d22bbfSJunchao Zhang #include <petscvec_kokkos.hpp>
6e0eea495SMark #include <petsc/private/dmpleximpl.h>   /*I   "petscdmplex.h"   I*/
7e0eea495SMark #include <petsclandau.h>
8e0eea495SMark #include <petscts.h>
9a587d139SMark 
10e0eea495SMark #include <Kokkos_Core.hpp>
11e0eea495SMark #include <cstdio>
12e0eea495SMark typedef Kokkos::TeamPolicy<>::member_type team_member;
13e0eea495SMark #include "../land_tensors.h"
14a587d139SMark #include <petscaijdevice.h>
15e0eea495SMark 
16e0eea495SMark namespace landau_inner_red {  // namespace helps with name resolution in reduction identity
1752cdd6eaSMark   template< class ScalarType >
18e0eea495SMark   struct array_type {
1952cdd6eaSMark     ScalarType gg2[LANDAU_DIM];
2052cdd6eaSMark     ScalarType gg3[LANDAU_DIM][LANDAU_DIM];
21e0eea495SMark 
22e0eea495SMark     KOKKOS_INLINE_FUNCTION   // Default constructor - Initialize to 0's
23e0eea495SMark     array_type() {
24e0eea495SMark       for (int j = 0; j < LANDAU_DIM; j++) {
2552cdd6eaSMark         gg2[j] = 0;
26e0eea495SMark         for (int k = 0; k < LANDAU_DIM; k++) {
2752cdd6eaSMark           gg3[j][k] = 0;
28e0eea495SMark         }
29e0eea495SMark       }
30e0eea495SMark     }
31e0eea495SMark     KOKKOS_INLINE_FUNCTION   // Copy Constructor
32e0eea495SMark     array_type(const array_type & rhs) {
33e0eea495SMark       for (int j = 0; j < LANDAU_DIM; j++) {
3452cdd6eaSMark         gg2[j] = rhs.gg2[j];
35e0eea495SMark         for (int k = 0; k < LANDAU_DIM; k++) {
3652cdd6eaSMark           gg3[j][k] = rhs.gg3[j][k];
37e0eea495SMark         }
38e0eea495SMark       }
39e0eea495SMark     }
40e0eea495SMark     KOKKOS_INLINE_FUNCTION   // add operator
4170a7d78aSStefano Zampini     array_type& operator += (const array_type& src)
4270a7d78aSStefano Zampini     {
43e0eea495SMark       for (int j = 0; j < LANDAU_DIM; j++) {
4452cdd6eaSMark         gg2[j] += src.gg2[j];
45e0eea495SMark         for (int k = 0; k < LANDAU_DIM; k++) {
4652cdd6eaSMark           gg3[j][k] += src.gg3[j][k];
47e0eea495SMark         }
48e0eea495SMark       }
49e0eea495SMark       return *this;
50e0eea495SMark     }
51e0eea495SMark     KOKKOS_INLINE_FUNCTION   // volatile add operator
5270a7d78aSStefano Zampini     void operator += (const volatile array_type& src) volatile
5370a7d78aSStefano Zampini     {
54e0eea495SMark       for (int j = 0; j < LANDAU_DIM; j++) {
5552cdd6eaSMark         gg2[j] += src.gg2[j];
56e0eea495SMark         for (int k = 0; k < LANDAU_DIM; k++) {
5752cdd6eaSMark           gg3[j][k] += src.gg3[j][k];
58e0eea495SMark         }
59e0eea495SMark       }
60e0eea495SMark     }
61e0eea495SMark   };
6252cdd6eaSMark   typedef array_type<PetscReal> TensorValueType;  // used to simplify code below
63e0eea495SMark }
64e0eea495SMark 
65e0eea495SMark namespace Kokkos { //reduction identity must be defined in Kokkos namespace
66e0eea495SMark   template<>
6752cdd6eaSMark   struct reduction_identity< landau_inner_red::TensorValueType > {
6852cdd6eaSMark     KOKKOS_FORCEINLINE_FUNCTION static landau_inner_red::TensorValueType sum() {
6952cdd6eaSMark       return landau_inner_red::TensorValueType();
70e0eea495SMark     }
71e0eea495SMark   };
72e0eea495SMark }
73e0eea495SMark 
74e0eea495SMark extern "C"  {
75*8a6f2e61SMark Adams PetscErrorCode LandauKokkosCreateMatMaps(P4estVertexMaps maps[], pointInterpolationP4est (*pointMaps)[LANDAU_MAX_Q_FACE], PetscInt Nf[], PetscInt Nq, PetscInt grid)
76a587d139SMark {
77a587d139SMark   P4estVertexMaps   h_maps;  /* host container */
78*8a6f2e61SMark Adams   const Kokkos::View<pointInterpolationP4est*[LANDAU_MAX_Q_FACE], Kokkos::LayoutRight, Kokkos::HostSpace, Kokkos::MemoryTraits<Kokkos::Unmanaged> >    h_points ((pointInterpolationP4est*)pointMaps, maps[grid].num_reduced);
79*8a6f2e61SMark Adams   const Kokkos::View< LandauIdx*[LANDAU_MAX_SPECIES][LANDAU_MAX_NQ], Kokkos::LayoutRight, Kokkos::HostSpace, Kokkos::MemoryTraits<Kokkos::Unmanaged> > h_gidx ((LandauIdx*)maps[grid].gIdx, maps[grid].num_elements);
80*8a6f2e61SMark Adams   Kokkos::View<pointInterpolationP4est*[LANDAU_MAX_Q_FACE], Kokkos::LayoutRight>   *d_points = new Kokkos::View<pointInterpolationP4est*[LANDAU_MAX_Q_FACE], Kokkos::LayoutRight>("points", maps[grid].num_reduced);
81*8a6f2e61SMark Adams   Kokkos::View<LandauIdx*[LANDAU_MAX_SPECIES][LANDAU_MAX_NQ], Kokkos::LayoutRight> *d_gidx = new Kokkos::View<LandauIdx*[LANDAU_MAX_SPECIES][LANDAU_MAX_NQ], Kokkos::LayoutRight>("gIdx", maps[grid].num_elements);
82a587d139SMark 
83a587d139SMark   PetscFunctionBegin;
84a587d139SMark   Kokkos::deep_copy (*d_gidx, h_gidx);
85a587d139SMark   Kokkos::deep_copy (*d_points, h_points);
86*8a6f2e61SMark Adams   h_maps.num_elements = maps[grid].num_elements;
87*8a6f2e61SMark Adams   h_maps.num_face = maps[grid].num_face;
88*8a6f2e61SMark Adams   h_maps.num_reduced = maps[grid].num_reduced;
89*8a6f2e61SMark Adams   h_maps.deviceType = maps[grid].deviceType;
90*8a6f2e61SMark Adams   h_maps.numgrids = maps[grid].numgrids;
91*8a6f2e61SMark Adams   h_maps.Nf = Nf[grid];
92a587d139SMark   h_maps.Nq = Nq;
93a587d139SMark   h_maps.c_maps = (pointInterpolationP4est (*)[LANDAU_MAX_Q_FACE]) d_points->data();
94*8a6f2e61SMark Adams   maps[grid].vp1 = (void*)d_points;
95a587d139SMark   h_maps.gIdx = (LandauIdx (*)[LANDAU_MAX_SPECIES][LANDAU_MAX_NQ]) d_gidx->data();
96*8a6f2e61SMark Adams   maps[grid].vp2 = (void*)d_gidx;
97a587d139SMark   {
98a587d139SMark     Kokkos::View<P4estVertexMaps, Kokkos::HostSpace> h_maps_k(&h_maps);
9911d22bbfSJunchao Zhang     Kokkos::View<P4estVertexMaps>                    *d_maps_k = new Kokkos::View<P4estVertexMaps>(Kokkos::create_mirror(Kokkos::DefaultExecutionSpace::memory_space(),h_maps_k));
100a587d139SMark     Kokkos::deep_copy (*d_maps_k, h_maps_k);
101*8a6f2e61SMark Adams     maps[grid].d_self = d_maps_k->data();
102*8a6f2e61SMark Adams     maps[grid].vp3 = (void*)d_maps_k;
103a587d139SMark   }
104a587d139SMark   PetscFunctionReturn(0);
105a587d139SMark }
106*8a6f2e61SMark Adams PetscErrorCode LandauKokkosDestroyMatMaps(P4estVertexMaps maps[], PetscInt num_grids)
107a587d139SMark {
108362febeeSStefano Zampini   PetscFunctionBegin;
109*8a6f2e61SMark Adams   for (PetscInt grid=0;grid<num_grids;grid++) {
110*8a6f2e61SMark Adams     auto a = static_cast<Kokkos::View<pointInterpolationP4est*[LANDAU_MAX_Q_FACE], Kokkos::LayoutRight>*>(maps[grid].vp1);
111*8a6f2e61SMark Adams     auto b = static_cast<Kokkos::View<LandauIdx*[LANDAU_MAX_SPECIES][LANDAU_MAX_NQ], Kokkos::LayoutRight>*>(maps[grid].vp2);
112*8a6f2e61SMark Adams     auto c = static_cast<Kokkos::View<P4estVertexMaps*>*>(maps[grid].vp3);
113a587d139SMark     delete a;  delete b;  delete c;
114*8a6f2e61SMark Adams   }
115a587d139SMark   PetscFunctionReturn(0);
116a587d139SMark }
117a587d139SMark 
118*8a6f2e61SMark Adams PetscErrorCode LandauKokkosStaticDataSet(DM plex, const PetscInt Nq, const PetscInt num_grids, PetscInt a_numCells[], PetscInt a_species_offset[], PetscInt a_mat_offset[],
119*8a6f2e61SMark Adams                                          PetscReal a_nu_alpha[], PetscReal a_nu_beta[], PetscReal a_invMass[], PetscReal a_invJ[],
120*8a6f2e61SMark Adams                                          PetscReal a_x[], PetscReal a_y[], PetscReal a_z[], PetscReal a_w[], LandauStaticData *SData_d)
121e0eea495SMark {
122e0eea495SMark   PetscReal       *BB,*DD;
123e8d2b73aSMark Adams   PetscErrorCode  ierr;
124e8d2b73aSMark Adams   PetscTabulation *Tf;
125*8a6f2e61SMark Adams   PetscInt        dim;
126*8a6f2e61SMark Adams   PetscInt        Nb=Nq,ip_offset[LANDAU_MAX_GRIDS+1],ipf_offset[LANDAU_MAX_GRIDS+1],elem_offset[LANDAU_MAX_GRIDS+1],nip,IPf_sz,Nftot;
127e8d2b73aSMark Adams   PetscDS         prob;
128e0eea495SMark 
129e0eea495SMark   PetscFunctionBegin;
130e0eea495SMark   ierr = DMGetDimension(plex, &dim);CHKERRQ(ierr);
131e0eea495SMark   ierr = DMGetDS(plex, &prob);CHKERRQ(ierr);
13252cdd6eaSMark   if (LANDAU_DIM != dim) SETERRQ2(PETSC_COMM_WORLD, PETSC_ERR_PLIB, "dim %D != LANDAU_DIM %d",dim,LANDAU_DIM);
133e0eea495SMark   ierr = PetscDSGetTabulation(prob, &Tf);CHKERRQ(ierr);
134e8d2b73aSMark Adams   BB   = Tf[0]->T[0]; DD = Tf[0]->T[1];
135*8a6f2e61SMark Adams   ip_offset[0] = ipf_offset[0] = elem_offset[0] = 0;
136*8a6f2e61SMark Adams   nip = 0;
137*8a6f2e61SMark Adams   IPf_sz = 0;
138*8a6f2e61SMark Adams   for (PetscInt grid=0 ; grid<num_grids ; grid++) {
139*8a6f2e61SMark Adams     PetscInt nfloc = a_species_offset[grid+1] - a_species_offset[grid];
140*8a6f2e61SMark Adams     elem_offset[grid+1] = elem_offset[grid] + a_numCells[grid];
141*8a6f2e61SMark Adams     nip += a_numCells[grid]*Nq;
142*8a6f2e61SMark Adams     ip_offset[grid+1] = nip;
143*8a6f2e61SMark Adams     IPf_sz += Nq*nfloc*a_numCells[grid];
144*8a6f2e61SMark Adams     ipf_offset[grid+1] = IPf_sz;
145*8a6f2e61SMark Adams   }
146*8a6f2e61SMark Adams   Nftot = a_species_offset[num_grids];
147e8d2b73aSMark Adams   ierr = PetscKokkosInitializeCheck();CHKERRQ(ierr);
148e8d2b73aSMark Adams   {
149*8a6f2e61SMark Adams     const Kokkos::View<PetscReal*, Kokkos::LayoutLeft, Kokkos::HostSpace, Kokkos::MemoryTraits<Kokkos::Unmanaged> > h_alpha (a_nu_alpha, Nftot);
150*8a6f2e61SMark Adams     auto alpha = new Kokkos::View<PetscReal*, Kokkos::LayoutLeft> ("alpha", Nftot);
151e8d2b73aSMark Adams     SData_d->alpha = static_cast<void*>(alpha);
152*8a6f2e61SMark Adams     const Kokkos::View<PetscReal*, Kokkos::LayoutLeft, Kokkos::HostSpace, Kokkos::MemoryTraits<Kokkos::Unmanaged> > h_beta (a_nu_beta, Nftot);
153*8a6f2e61SMark Adams     auto beta = new Kokkos::View<PetscReal*, Kokkos::LayoutLeft> ("beta", Nftot);
154e8d2b73aSMark Adams     SData_d->beta = static_cast<void*>(beta);
155*8a6f2e61SMark Adams     const Kokkos::View<PetscReal*, Kokkos::LayoutLeft, Kokkos::HostSpace, Kokkos::MemoryTraits<Kokkos::Unmanaged> > h_invMass (a_invMass,Nftot);
156*8a6f2e61SMark Adams     auto invMass = new Kokkos::View<PetscReal*, Kokkos::LayoutLeft> ("invMass", Nftot);
157e8d2b73aSMark Adams     SData_d->invMass = static_cast<void*>(invMass);
158e8d2b73aSMark Adams     const Kokkos::View<PetscReal*, Kokkos::LayoutLeft, Kokkos::HostSpace, Kokkos::MemoryTraits<Kokkos::Unmanaged> > h_BB (BB,Nq*Nb);
159e8d2b73aSMark Adams     auto B = new Kokkos::View<PetscReal*, Kokkos::LayoutLeft> ("B", Nq*Nb);
160e8d2b73aSMark Adams     SData_d->B = static_cast<void*>(B);
161e8d2b73aSMark Adams     const Kokkos::View<PetscReal*, Kokkos::LayoutLeft, Kokkos::HostSpace, Kokkos::MemoryTraits<Kokkos::Unmanaged> > h_DD (DD,Nq*Nb*dim);
162e8d2b73aSMark Adams     auto D = new Kokkos::View<PetscReal*, Kokkos::LayoutLeft> ("D", Nq*Nb*dim);
163e8d2b73aSMark Adams     SData_d->D = static_cast<void*>(D);
164e8d2b73aSMark Adams     const Kokkos::View<PetscReal*, Kokkos::LayoutLeft, Kokkos::HostSpace, Kokkos::MemoryTraits<Kokkos::Unmanaged> > h_invJ (a_invJ, nip*dim*dim);
165e8d2b73aSMark Adams     auto invJ = new Kokkos::View<PetscReal*, Kokkos::LayoutLeft> ("invJ", nip*dim*dim);
166e8d2b73aSMark Adams     SData_d->invJ = static_cast<void*>(invJ);
167e8d2b73aSMark Adams     const Kokkos::View<PetscReal*, Kokkos::LayoutLeft, Kokkos::HostSpace, Kokkos::MemoryTraits<Kokkos::Unmanaged> > h_x (a_x, nip);
168e8d2b73aSMark Adams     auto x = new Kokkos::View<PetscReal*, Kokkos::LayoutLeft> ("x", nip);
169e8d2b73aSMark Adams     SData_d->x = static_cast<void*>(x);
170e8d2b73aSMark Adams     const Kokkos::View<PetscReal*, Kokkos::LayoutLeft, Kokkos::HostSpace, Kokkos::MemoryTraits<Kokkos::Unmanaged> > h_y (a_y, nip);
171e8d2b73aSMark Adams     auto y = new Kokkos::View<PetscReal*, Kokkos::LayoutLeft> ("y", nip);
172e8d2b73aSMark Adams     SData_d->y = static_cast<void*>(y);
173e8d2b73aSMark Adams     const Kokkos::View<PetscReal*, Kokkos::LayoutLeft, Kokkos::HostSpace, Kokkos::MemoryTraits<Kokkos::Unmanaged> > h_w (a_w, nip);
174e8d2b73aSMark Adams     auto w = new Kokkos::View<PetscReal*, Kokkos::LayoutLeft> ("w", nip);
175e8d2b73aSMark Adams     SData_d->w = static_cast<void*>(w);
176e8d2b73aSMark Adams 
177e8d2b73aSMark Adams     Kokkos::deep_copy (*alpha, h_alpha);
178e8d2b73aSMark Adams     Kokkos::deep_copy (*beta, h_beta);
179e8d2b73aSMark Adams     Kokkos::deep_copy (*invMass, h_invMass);
180e8d2b73aSMark Adams     Kokkos::deep_copy (*B, h_BB);
181e8d2b73aSMark Adams     Kokkos::deep_copy (*D, h_DD);
182e8d2b73aSMark Adams     Kokkos::deep_copy (*invJ, h_invJ);
183e8d2b73aSMark Adams     Kokkos::deep_copy (*x, h_x);
184e8d2b73aSMark Adams     Kokkos::deep_copy (*y, h_y);
185e8d2b73aSMark Adams     Kokkos::deep_copy (*w, h_w);
186e8d2b73aSMark Adams 
187*8a6f2e61SMark Adams     if (dim==3) {
188*8a6f2e61SMark Adams       const Kokkos::View<PetscReal*, Kokkos::LayoutLeft, Kokkos::HostSpace, Kokkos::MemoryTraits<Kokkos::Unmanaged> > h_z (a_z , dim==3 ? nip : 0);
189*8a6f2e61SMark Adams       auto z = new Kokkos::View<PetscReal*, Kokkos::LayoutLeft> ("z", nip);
190*8a6f2e61SMark Adams       SData_d->z = static_cast<void*>(z);
191*8a6f2e61SMark Adams       Kokkos::deep_copy (*z, h_z);
192*8a6f2e61SMark Adams     } else SData_d->z = NULL;
193*8a6f2e61SMark Adams 
194*8a6f2e61SMark Adams     //
195*8a6f2e61SMark Adams     const Kokkos::View<PetscInt*, Kokkos::LayoutLeft, Kokkos::HostSpace, Kokkos::MemoryTraits<Kokkos::Unmanaged> > h_NCells (a_numCells, num_grids);
196*8a6f2e61SMark Adams     auto nc = new Kokkos::View<PetscInt*, Kokkos::LayoutLeft> ("NCells",num_grids);
197*8a6f2e61SMark Adams     SData_d->NCells = static_cast<void*>(nc);
198*8a6f2e61SMark Adams     Kokkos::deep_copy (*nc, h_NCells);
199*8a6f2e61SMark Adams 
200*8a6f2e61SMark Adams     const Kokkos::View<PetscInt*, Kokkos::LayoutLeft, Kokkos::HostSpace, Kokkos::MemoryTraits<Kokkos::Unmanaged> > h_species_offset (a_species_offset, num_grids+1);
201*8a6f2e61SMark Adams     auto soff = new Kokkos::View<PetscInt*, Kokkos::LayoutLeft> ("species_offset",num_grids+1);
202*8a6f2e61SMark Adams     SData_d->species_offset = static_cast<void*>(soff);
203*8a6f2e61SMark Adams     Kokkos::deep_copy (*soff, h_species_offset);
204*8a6f2e61SMark Adams 
205*8a6f2e61SMark Adams     const Kokkos::View<PetscInt*, Kokkos::LayoutLeft, Kokkos::HostSpace, Kokkos::MemoryTraits<Kokkos::Unmanaged> > h_mat_offset (a_mat_offset, num_grids+1);
206*8a6f2e61SMark Adams     auto moff = new Kokkos::View<PetscInt*, Kokkos::LayoutLeft> ("mat_offset",num_grids+1);
207*8a6f2e61SMark Adams     SData_d->mat_offset = static_cast<void*>(moff);
208*8a6f2e61SMark Adams     Kokkos::deep_copy (*moff, h_mat_offset);
209*8a6f2e61SMark Adams 
210*8a6f2e61SMark Adams     const Kokkos::View<PetscInt*, Kokkos::LayoutLeft, Kokkos::HostSpace, Kokkos::MemoryTraits<Kokkos::Unmanaged> > h_ip_offset (ip_offset, num_grids+1);
211*8a6f2e61SMark Adams     auto ipoff = new Kokkos::View<PetscInt*, Kokkos::LayoutLeft> ("ip_offset",num_grids+1);
212*8a6f2e61SMark Adams     SData_d->ip_offset = static_cast<void*>(ipoff);
213*8a6f2e61SMark Adams     Kokkos::deep_copy (*ipoff,  h_ip_offset);
214*8a6f2e61SMark Adams 
215*8a6f2e61SMark Adams     const Kokkos::View<PetscInt*, Kokkos::LayoutLeft, Kokkos::HostSpace, Kokkos::MemoryTraits<Kokkos::Unmanaged> > h_elem_offset (elem_offset, num_grids+1);
216*8a6f2e61SMark Adams     auto eoff = new Kokkos::View<PetscInt*, Kokkos::LayoutLeft> ("elem_offset",num_grids+1);
217*8a6f2e61SMark Adams     SData_d->elem_offset = static_cast<void*>(eoff);
218*8a6f2e61SMark Adams     Kokkos::deep_copy (*eoff,  h_elem_offset);
219*8a6f2e61SMark Adams 
220*8a6f2e61SMark Adams     const Kokkos::View<PetscInt*, Kokkos::LayoutLeft, Kokkos::HostSpace, Kokkos::MemoryTraits<Kokkos::Unmanaged> > h_ipf_offset (ipf_offset, num_grids+1);
221*8a6f2e61SMark Adams     auto ipfoff = new Kokkos::View<PetscInt*, Kokkos::LayoutLeft> ("ipf_offset",num_grids+1);
222*8a6f2e61SMark Adams     SData_d->ipf_offset = static_cast<void*>(ipfoff);
223*8a6f2e61SMark Adams     Kokkos::deep_copy (*ipfoff,  h_ipf_offset);
224*8a6f2e61SMark Adams 
225*8a6f2e61SMark Adams #if defined(LANDAU_LAYOUT_LEFT) // preallocate dynamic data, no copy
226*8a6f2e61SMark Adams     auto ipfdf_data =  new Kokkos::View<PetscReal**, Kokkos::LayoutLeft > ("fdf", dim+1, IPf_sz);
227*8a6f2e61SMark Adams #else
228*8a6f2e61SMark Adams     auto ipfdf_data =  new Kokkos::View<PetscReal**, Kokkos::LayoutRight > ("fdf", dim+1, IPf_sz);
229*8a6f2e61SMark Adams #endif
230*8a6f2e61SMark Adams     SData_d->ipfdf_data = static_cast<void*>(ipfdf_data);
231*8a6f2e61SMark Adams 
232*8a6f2e61SMark Adams     auto Eq_m = new Kokkos::View<PetscReal*, Kokkos::LayoutLeft> ("Eq_m",Nftot); // allocate but do not set
233e8d2b73aSMark Adams     SData_d->Eq_m = static_cast<void*>(Eq_m);
234e8d2b73aSMark Adams   }
235*8a6f2e61SMark Adams   SData_d->maps = NULL; // not used
236e8d2b73aSMark Adams   PetscFunctionReturn(0);
237e8d2b73aSMark Adams }
238e8d2b73aSMark Adams 
239*8a6f2e61SMark Adams PetscErrorCode LandauKokkosStaticDataClear(LandauStaticData *SData_d)
240e8d2b73aSMark Adams {
241e8d2b73aSMark Adams   PetscFunctionBegin;
242e8d2b73aSMark Adams   if (SData_d->alpha) {
243e8d2b73aSMark Adams     auto alpha = static_cast<Kokkos::View<PetscReal*, Kokkos::LayoutLeft>*>(SData_d->alpha);
244e8d2b73aSMark Adams     delete alpha;
245*8a6f2e61SMark Adams     SData_d->alpha = NULL;
246e8d2b73aSMark Adams     auto beta = static_cast<Kokkos::View<PetscReal*, Kokkos::LayoutLeft>*>(SData_d->beta);
247e8d2b73aSMark Adams     delete beta;
248e8d2b73aSMark Adams     auto invMass = static_cast<Kokkos::View<PetscReal*, Kokkos::LayoutLeft>*>(SData_d->invMass);
249e8d2b73aSMark Adams     delete invMass;
250e8d2b73aSMark Adams     auto B = static_cast<Kokkos::View<PetscReal*, Kokkos::LayoutLeft>*>(SData_d->B);
251e8d2b73aSMark Adams     delete B;
252e8d2b73aSMark Adams     auto D = static_cast<Kokkos::View<PetscReal*, Kokkos::LayoutLeft>*>(SData_d->D);
253e8d2b73aSMark Adams     delete D;
254e8d2b73aSMark Adams     auto invJ = static_cast<Kokkos::View<PetscReal*, Kokkos::LayoutLeft>*>(SData_d->invJ);
255e8d2b73aSMark Adams     delete invJ;
256e8d2b73aSMark Adams     auto x = static_cast<Kokkos::View<PetscReal*, Kokkos::LayoutLeft>*>(SData_d->x);
257e8d2b73aSMark Adams     delete x;
258e8d2b73aSMark Adams     auto y = static_cast<Kokkos::View<PetscReal*, Kokkos::LayoutLeft>*>(SData_d->y);
259e8d2b73aSMark Adams     delete y;
260*8a6f2e61SMark Adams     if (SData_d->z) {
261e8d2b73aSMark Adams       auto z = static_cast<Kokkos::View<PetscReal*, Kokkos::LayoutLeft>*>(SData_d->z);
262e8d2b73aSMark Adams       delete z;
263*8a6f2e61SMark Adams     }
264*8a6f2e61SMark Adams #if defined(LANDAU_LAYOUT_LEFT) // preallocate dynamic data, no copy
265*8a6f2e61SMark Adams     auto z = static_cast<Kokkos::View<PetscReal**, Kokkos::LayoutLeft>*>(SData_d->ipfdf_data);
266*8a6f2e61SMark Adams #else
267*8a6f2e61SMark Adams     auto z = static_cast<Kokkos::View<PetscReal**, Kokkos::LayoutRight>*>(SData_d->ipfdf_data);
268*8a6f2e61SMark Adams #endif
269*8a6f2e61SMark Adams     delete z;
270e8d2b73aSMark Adams     auto w = static_cast<Kokkos::View<PetscReal*, Kokkos::LayoutLeft>*>(SData_d->w);
271e8d2b73aSMark Adams     delete w;
272e8d2b73aSMark Adams     auto Eq_m = static_cast<Kokkos::View<PetscReal*, Kokkos::LayoutLeft>*>(SData_d->Eq_m);
273e8d2b73aSMark Adams     delete Eq_m;
274*8a6f2e61SMark Adams     // offset
275*8a6f2e61SMark Adams     auto nc = static_cast<Kokkos::View<PetscInt*, Kokkos::LayoutLeft>*>(SData_d->NCells);
276*8a6f2e61SMark Adams     delete nc;
277*8a6f2e61SMark Adams     auto soff = static_cast<Kokkos::View<PetscInt*, Kokkos::LayoutLeft>*>(SData_d->species_offset);
278*8a6f2e61SMark Adams     delete soff;
279*8a6f2e61SMark Adams     auto moff = static_cast<Kokkos::View<PetscInt*, Kokkos::LayoutLeft>*>(SData_d->mat_offset);
280*8a6f2e61SMark Adams     delete moff;
281*8a6f2e61SMark Adams     auto ipoff = static_cast<Kokkos::View<PetscInt*, Kokkos::LayoutLeft>*>(SData_d->ip_offset);
282*8a6f2e61SMark Adams     delete ipoff;
283*8a6f2e61SMark Adams     auto eoff = static_cast<Kokkos::View<PetscInt*, Kokkos::LayoutLeft>*>(SData_d->elem_offset);
284*8a6f2e61SMark Adams     delete eoff;
285*8a6f2e61SMark Adams     auto ipfoff = static_cast<Kokkos::View<PetscInt*, Kokkos::LayoutLeft>*>(SData_d->ipf_offset);
286*8a6f2e61SMark Adams     delete ipfoff;
287af0767daSMark Adams   }
288e8d2b73aSMark Adams   PetscFunctionReturn(0);
289e8d2b73aSMark Adams }
290e8d2b73aSMark Adams 
291*8a6f2e61SMark Adams #define KOKKOS_SHARED_LEVEL 1
292*8a6f2e61SMark Adams KOKKOS_INLINE_FUNCTION
293*8a6f2e61SMark Adams PetscErrorCode landau_mat_assemble(PetscSplitCSRDataStructure d_mat, const team_member team,
294*8a6f2e61SMark Adams                                    Kokkos::View<PetscScalar**, Kokkos::LayoutRight, Kokkos::DefaultExecutionSpace::scratch_memory_space> s_fieldMats,
295*8a6f2e61SMark Adams                                    Kokkos::View<PetscInt**, Kokkos::LayoutRight, Kokkos::DefaultExecutionSpace::scratch_memory_space>  s_idx,
296*8a6f2e61SMark Adams                                    Kokkos::View<PetscReal**, Kokkos::LayoutRight, Kokkos::DefaultExecutionSpace::scratch_memory_space> s_scale,
297*8a6f2e61SMark Adams                                    const PetscInt Nb, const PetscInt Nq, const PetscInt nfaces, const PetscInt moffset, const PetscInt elem, const PetscInt fieldA, const P4estVertexMaps *d_maps)
298*8a6f2e61SMark Adams {
299*8a6f2e61SMark Adams   const LandauIdx *const Idxs = &d_maps->gIdx[elem][fieldA][0];
300*8a6f2e61SMark Adams   team.team_barrier();
301*8a6f2e61SMark Adams   Kokkos::parallel_for(Kokkos::TeamVectorRange(team,0,Nb), [=] (int f) {
302*8a6f2e61SMark Adams       PetscInt q, idx = Idxs[f];
303*8a6f2e61SMark Adams       if (idx >= 0) {
304*8a6f2e61SMark Adams         s_idx(f,0) = idx + moffset;
305*8a6f2e61SMark Adams         s_scale(f,0) = 1.;
306*8a6f2e61SMark Adams       } else {
307*8a6f2e61SMark Adams         idx = -idx - 1;
308*8a6f2e61SMark Adams         for (q = 0; q < nfaces; q++) {
309*8a6f2e61SMark Adams           s_idx(f,q) = d_maps->c_maps[idx][q].gid + moffset;
310*8a6f2e61SMark Adams           s_scale(f,q) = d_maps->c_maps[idx][q].scale;
311*8a6f2e61SMark Adams         }
312*8a6f2e61SMark Adams       }
313*8a6f2e61SMark Adams     });
314*8a6f2e61SMark Adams   team.team_barrier();
315*8a6f2e61SMark Adams   Kokkos::parallel_for(Kokkos::TeamThreadRange(team,0,Nb), [=] (int f) {
316*8a6f2e61SMark Adams       PetscInt nr,idx = Idxs[f];
317*8a6f2e61SMark Adams       if (idx >= 0) {
318*8a6f2e61SMark Adams         nr = 1;
319*8a6f2e61SMark Adams       } else {
320*8a6f2e61SMark Adams         nr = nfaces;
321*8a6f2e61SMark Adams       }
322*8a6f2e61SMark Adams       Kokkos::parallel_for(Kokkos::ThreadVectorRange(team,0,Nb), [=] (int g) {
323*8a6f2e61SMark Adams           PetscScalar     vals[LANDAU_MAX_Q_FACE*LANDAU_MAX_Q_FACE];
324*8a6f2e61SMark Adams           PetscInt        q,d,nc,idx = Idxs[g];
325*8a6f2e61SMark Adams           if (idx >= 0) {
326*8a6f2e61SMark Adams             nc = 1;
327*8a6f2e61SMark Adams           } else {
328*8a6f2e61SMark Adams             nc = nfaces;
329*8a6f2e61SMark Adams           }
330*8a6f2e61SMark Adams           for (q = 0; q < nr; q++) {
331*8a6f2e61SMark Adams             for (d = 0; d < nc; d++) {
332*8a6f2e61SMark Adams               vals[q*nc + d] = s_scale(f,q)*s_scale(g,d)*s_fieldMats(f,g);
333*8a6f2e61SMark Adams             }
334*8a6f2e61SMark Adams           }
335*8a6f2e61SMark Adams           MatSetValuesDevice(d_mat,nr,&s_idx(f,0),nc,&s_idx(g,0),vals,ADD_VALUES);
336*8a6f2e61SMark Adams         });
337*8a6f2e61SMark Adams     });
338*8a6f2e61SMark Adams   return 0;
339*8a6f2e61SMark Adams }
340*8a6f2e61SMark Adams 
341*8a6f2e61SMark Adams PetscErrorCode LandauKokkosJacobian(DM plex[], const PetscInt Nq, const PetscInt num_grids, const PetscInt a_numCells[], PetscReal a_Eq_m[], PetscScalar a_elem_closure[],
342*8a6f2e61SMark Adams                                     const PetscInt N, const PetscScalar a_xarray[], const LandauStaticData *SData_d, const PetscInt num_sub_blocks, const PetscReal shift,
343*8a6f2e61SMark Adams                                     const PetscLogEvent events[], const PetscInt a_mat_offset[], const PetscInt a_species_offset[], Mat subJ[], Mat JacP)
344e8d2b73aSMark Adams {
345e8d2b73aSMark Adams   using scr_mem_t = Kokkos::DefaultExecutionSpace::scratch_memory_space;
3461eec90e4SMark Adams   using fieldMats_scr_t = Kokkos::View<PetscScalar**, Kokkos::LayoutRight, scr_mem_t>;
3471eec90e4SMark Adams   using idx_scr_t = Kokkos::View<PetscInt**, Kokkos::LayoutRight, scr_mem_t>;
3481eec90e4SMark Adams   using scale_scr_t = Kokkos::View<PetscReal**, Kokkos::LayoutRight, scr_mem_t>;
349e8d2b73aSMark Adams   using g2_scr_t = Kokkos::View<PetscReal***, Kokkos::LayoutRight, scr_mem_t>;
350e8d2b73aSMark Adams   using g3_scr_t = Kokkos::View<PetscReal****, Kokkos::LayoutRight, scr_mem_t>;
351e8d2b73aSMark Adams   PetscErrorCode    ierr;
352*8a6f2e61SMark Adams   PetscInt          Nb=Nq,dim,num_cells_max,num_cells_tot,Nf_max,nip_global;
353*8a6f2e61SMark Adams   int               nfaces=0;
354e8d2b73aSMark Adams   LandauCtx         *ctx;
355e8d2b73aSMark Adams   PetscReal         *d_Eq_m=NULL;
356*8a6f2e61SMark Adams   PetscScalar       *d_vertex_f=NULL;
357*8a6f2e61SMark Adams   P4estVertexMaps   *maps[LANDAU_MAX_GRIDS]; // this gets captured
358*8a6f2e61SMark Adams   PetscSplitCSRDataStructure d_mat;
359*8a6f2e61SMark Adams   PetscContainer    container = NULL;
360bd08a171SMark Adams   const int         conc = Kokkos::DefaultExecutionSpace().concurrency(), openmp = !!(conc < 1000), team_size = (openmp==0) ? Nq : 1;
361e8d2b73aSMark Adams   auto              d_alpha_k = static_cast<Kokkos::View<PetscReal*, Kokkos::LayoutLeft>*>(SData_d->alpha); //static data
362e8d2b73aSMark Adams   const PetscReal   *d_alpha = d_alpha_k->data();
363*8a6f2e61SMark Adams   const PetscInt    Nftot = d_alpha_k->size(); // total number of species
364e8d2b73aSMark Adams   auto              d_beta_k = static_cast<Kokkos::View<PetscReal*, Kokkos::LayoutLeft>*>(SData_d->beta);
365e8d2b73aSMark Adams   const PetscReal   *d_beta = d_beta_k->data();
366e8d2b73aSMark Adams   auto              d_invMass_k = static_cast<Kokkos::View<PetscReal*, Kokkos::LayoutLeft>*>(SData_d->invMass);
367e8d2b73aSMark Adams   const PetscReal   *d_invMass = d_invMass_k->data();
368e8d2b73aSMark Adams   auto              d_B_k = static_cast<Kokkos::View<PetscReal*, Kokkos::LayoutLeft>*>(SData_d->B);
369e8d2b73aSMark Adams   const PetscReal   *d_BB = d_B_k->data();
370e8d2b73aSMark Adams   auto              d_D_k = static_cast<Kokkos::View<PetscReal*, Kokkos::LayoutLeft>*>(SData_d->D);
371e8d2b73aSMark Adams   const PetscReal   *d_DD = d_D_k->data();
372e8d2b73aSMark Adams   auto              d_invJ_k = *static_cast<Kokkos::View<PetscReal*, Kokkos::LayoutLeft>*>(SData_d->invJ);     // use Kokkos vector in kernels
373e8d2b73aSMark Adams   auto              d_x_k = static_cast<Kokkos::View<PetscReal*, Kokkos::LayoutLeft>*>(SData_d->x); //static data
374e8d2b73aSMark Adams   const PetscReal   *d_x = d_x_k->data();
375e8d2b73aSMark Adams   auto              d_y_k = static_cast<Kokkos::View<PetscReal*, Kokkos::LayoutLeft>*>(SData_d->y); //static data
376e8d2b73aSMark Adams   const PetscReal   *d_y = d_y_k->data();
377e8d2b73aSMark Adams   auto              d_z_k = static_cast<Kokkos::View<PetscReal*, Kokkos::LayoutLeft>*>(SData_d->z); //static data
378*8a6f2e61SMark Adams   const PetscReal   *d_z = (LANDAU_DIM==3) ? d_z_k->data() : NULL;
379*8a6f2e61SMark Adams   auto              d_w_k = *static_cast<Kokkos::View<PetscReal*, Kokkos::LayoutLeft>*>(SData_d->w); //static data
380*8a6f2e61SMark Adams   const PetscReal   *d_w = d_w_k.data();
381*8a6f2e61SMark Adams   auto              d_Eq_m_k = static_cast<Kokkos::View<PetscReal*, Kokkos::LayoutLeft>*>(SData_d->Eq_m); // static storage, dynamci data - E(t), copy later
382*8a6f2e61SMark Adams   // grid offset d_numCells
383*8a6f2e61SMark Adams   auto              d_numCells_k = static_cast<Kokkos::View<PetscInt*, Kokkos::LayoutLeft>*>(SData_d->NCells);
384*8a6f2e61SMark Adams   const PetscInt   *d_numCells = d_numCells_k->data();
385*8a6f2e61SMark Adams   auto              d_species_offset_k = static_cast<Kokkos::View<PetscInt*, Kokkos::LayoutLeft>*>(SData_d->species_offset);
386*8a6f2e61SMark Adams   const PetscInt   *d_species_offset = d_species_offset_k->data();
387*8a6f2e61SMark Adams   auto              d_mat_offset_k = static_cast<Kokkos::View<PetscInt*, Kokkos::LayoutLeft>*>(SData_d->mat_offset);
388*8a6f2e61SMark Adams   const PetscInt   *d_mat_offset = d_mat_offset_k->data();
389*8a6f2e61SMark Adams   auto              d_ip_offset_k = static_cast<Kokkos::View<PetscInt*, Kokkos::LayoutLeft>*>(SData_d->ip_offset);
390*8a6f2e61SMark Adams   const PetscInt   *d_ip_offset = d_ip_offset_k->data();
391*8a6f2e61SMark Adams   auto              d_ipf_offset_k = static_cast<Kokkos::View<PetscInt*, Kokkos::LayoutLeft>*>(SData_d->ipf_offset);
392*8a6f2e61SMark Adams   const PetscInt   *d_ipf_offset = d_ipf_offset_k->data();
393*8a6f2e61SMark Adams   auto              d_elem_offset_k = static_cast<Kokkos::View<PetscInt*, Kokkos::LayoutLeft>*>(SData_d->elem_offset);
394*8a6f2e61SMark Adams   const PetscInt   *d_elem_offset = d_elem_offset_k->data();
395*8a6f2e61SMark Adams #if defined(LANDAU_LAYOUT_LEFT) // preallocate dynamic data, no copy
396*8a6f2e61SMark Adams   Kokkos::View<PetscReal**, Kokkos::LayoutLeft > d_fdf_k = *static_cast<Kokkos::View<PetscReal**, Kokkos::LayoutLeft >*>(SData_d->ipfdf_data);
397*8a6f2e61SMark Adams #else
398*8a6f2e61SMark Adams   Kokkos::View<PetscReal**, Kokkos::LayoutRight > d_fdf_k = *static_cast<Kokkos::View<PetscReal**, Kokkos::LayoutRight >*>(SData_d->ipfdf_data);
399*8a6f2e61SMark Adams #endif
400e8d2b73aSMark Adams   PetscFunctionBegin;
401e8d2b73aSMark Adams   ierr = PetscLogEventBegin(events[3],0,0,0,0);CHKERRQ(ierr);
402*8a6f2e61SMark Adams   ierr = DMGetApplicationContext(plex[0], &ctx);CHKERRQ(ierr);
403e8d2b73aSMark Adams   if (!ctx) SETERRQ(PETSC_COMM_SELF, PETSC_ERR_PLIB, "no context");
404*8a6f2e61SMark Adams   ierr = DMGetDimension(plex[0], &dim);CHKERRQ(ierr);
405e8d2b73aSMark Adams   if (LANDAU_DIM != dim) SETERRQ2(PETSC_COMM_WORLD, PETSC_ERR_PLIB, "dim %D != LANDAU_DIM %d",dim,LANDAU_DIM);
406a587d139SMark   if (ctx->gpu_assembly) {
407a587d139SMark     ierr = PetscObjectQuery((PetscObject) JacP, "assembly_maps", (PetscObject *) &container);CHKERRQ(ierr);
408a587d139SMark     if (container) { // not here first call
409a587d139SMark       P4estVertexMaps   *h_maps=NULL;
410a587d139SMark       ierr = PetscContainerGetPointer(container, (void **) &h_maps);CHKERRQ(ierr);
411*8a6f2e61SMark Adams       for (PetscInt grid=0 ; grid<num_grids ; grid++) {
412*8a6f2e61SMark Adams         if (h_maps[grid].d_self) {
413*8a6f2e61SMark Adams           maps[grid] = h_maps[grid].d_self;
414*8a6f2e61SMark Adams           nfaces = h_maps[grid].num_face; // nface=0 for CPU assembly
415a587d139SMark         } else {
416a587d139SMark           SETERRQ(PETSC_COMM_SELF, PETSC_ERR_PLIB, "GPU assembly but no metadata in container");
417a587d139SMark         }
418*8a6f2e61SMark Adams       }
419a587d139SMark       // this does the setup the first time called
420a587d139SMark       ierr = MatKokkosGetDeviceMatWrite(JacP,&d_mat);CHKERRQ(ierr);
421a587d139SMark     } else { // kernel output - first call assembled on device
422*8a6f2e61SMark Adams       for (PetscInt grid=0 ; grid<num_grids ; grid++) maps[grid] = NULL;
423*8a6f2e61SMark Adams       nfaces = 0;
424a587d139SMark     }
425a587d139SMark   } else {
426*8a6f2e61SMark Adams     for (PetscInt grid=0 ; grid<num_grids ; grid++) maps[grid] = NULL;
427*8a6f2e61SMark Adams     nfaces = 0;
428a587d139SMark   }
429*8a6f2e61SMark Adams   nip_global = num_cells_tot = Nf_max = num_cells_max = 0;
430*8a6f2e61SMark Adams   for (PetscInt grid=0 ; grid<num_grids ; grid++) {
431*8a6f2e61SMark Adams     int Nfloc = a_species_offset[grid+1] - a_species_offset[grid];
432*8a6f2e61SMark Adams     if (Nfloc > Nf_max) Nf_max = Nfloc;
433*8a6f2e61SMark Adams     if (a_numCells[grid] > num_cells_max) num_cells_max = a_numCells[grid];
434*8a6f2e61SMark Adams     num_cells_tot += a_numCells[grid];
435*8a6f2e61SMark Adams     nip_global += Nq*a_numCells[grid];
436*8a6f2e61SMark Adams   }
437*8a6f2e61SMark Adams   const PetscInt totDim_max = Nf_max*Nq, elem_mat_size_max = totDim_max*totDim_max;
438*8a6f2e61SMark Adams   const PetscInt elem_mat_num_cells_max_grid = container ? 0 : num_cells_max;
439*8a6f2e61SMark Adams   Kokkos::View<PetscScalar***, Kokkos::LayoutRight> d_elem_mats("element matrices", num_grids, elem_mat_num_cells_max_grid, elem_mat_size_max); // first call have large set of global element matrices
440*8a6f2e61SMark Adams   const Kokkos::View<PetscReal*, Kokkos::LayoutLeft, Kokkos::HostSpace, Kokkos::MemoryTraits<Kokkos::Unmanaged> >  h_Eq_m_k (a_Eq_m, Nftot);
441*8a6f2e61SMark Adams   if (a_elem_closure || a_xarray) {
442e8d2b73aSMark Adams     Kokkos::deep_copy (*d_Eq_m_k, h_Eq_m_k);
443e8d2b73aSMark Adams     d_Eq_m = d_Eq_m_k->data();
444*8a6f2e61SMark Adams   } else d_Eq_m = NULL;
445*8a6f2e61SMark Adams   ierr = PetscKokkosInitializeCheck();CHKERRQ(ierr);
446*8a6f2e61SMark Adams   ierr = PetscLogEventEnd(events[3],0,0,0,0);CHKERRQ(ierr);
447*8a6f2e61SMark Adams   if (a_elem_closure || a_xarray) { // Jacobian, create f & df
448*8a6f2e61SMark Adams     Kokkos::View<PetscScalar*, Kokkos::LayoutLeft> *d_vertex_f_k = NULL;
4491eec90e4SMark Adams     ierr = PetscLogEventBegin(events[1],0,0,0,0);CHKERRQ(ierr);
450*8a6f2e61SMark Adams     if (a_elem_closure) {
451*8a6f2e61SMark Adams       PetscInt closure_sz = 0; // argh, don't have this on the host!!!
452*8a6f2e61SMark Adams       for (PetscInt grid=0 ; grid<num_grids ; grid++) {
453*8a6f2e61SMark Adams         PetscInt nfloc = a_species_offset[grid+1] - a_species_offset[grid];
454*8a6f2e61SMark Adams         closure_sz     += Nq*nfloc*a_numCells[grid];
455*8a6f2e61SMark Adams       }
456*8a6f2e61SMark Adams       d_vertex_f_k = new Kokkos::View<PetscScalar*, Kokkos::LayoutLeft> ("closure",closure_sz);
457*8a6f2e61SMark Adams       const Kokkos::View<PetscScalar*, Kokkos::LayoutLeft, Kokkos::HostSpace, Kokkos::MemoryTraits<Kokkos::Unmanaged> > h_closure_k (a_elem_closure, closure_sz); // Vertex data for each element
458*8a6f2e61SMark Adams       Kokkos::deep_copy (*d_vertex_f_k, h_closure_k);
459*8a6f2e61SMark Adams       d_vertex_f  = d_vertex_f_k->data();
460af0767daSMark Adams     } else {
461*8a6f2e61SMark Adams       d_vertex_f = (PetscScalar*)a_xarray;
462af0767daSMark Adams     }
463e8d2b73aSMark Adams     ierr = PetscLogEventEnd(events[1],0,0,0,0);CHKERRQ(ierr);
464930e68a5SMark Adams     ierr = PetscLogEventBegin(events[8],0,0,0,0);CHKERRQ(ierr);
465e6118d12SMark Adams     ierr = PetscLogGpuTimeBegin();CHKERRQ(ierr);
466*8a6f2e61SMark Adams     Kokkos::parallel_for("f, df", Kokkos::TeamPolicy<>(num_cells_tot, team_size, /* Kokkos::AUTO */ 16), KOKKOS_LAMBDA (const team_member team) {
467*8a6f2e61SMark Adams         // find my grid
468*8a6f2e61SMark Adams         PetscInt grid = 0, g_cell = team.league_rank();
469*8a6f2e61SMark Adams         while (g_cell >= d_elem_offset[grid+1]) grid++; // yuck search for grid
470*8a6f2e61SMark Adams         {
471*8a6f2e61SMark Adams           const PetscInt     moffset = d_mat_offset[grid], nip_loc = d_numCells[grid]*Nq, Nfloc = d_species_offset[grid+1] - d_species_offset[grid], elem = g_cell -  d_elem_offset[grid];
472*8a6f2e61SMark Adams           const PetscInt     IP_idx = d_ip_offset[grid], IPf_idx = d_ipf_offset[grid];
473af0767daSMark Adams           const PetscScalar  *coef;
474af0767daSMark Adams           PetscScalar        coef_buff[LANDAU_MAX_SPECIES*LANDAU_MAX_NQ];
475a587d139SMark           // un pack IPData
476*8a6f2e61SMark Adams           if (!maps[grid]) {
477*8a6f2e61SMark Adams             coef = &d_vertex_f[elem*Nb*Nfloc + IPf_idx]; // closure and IP indexing are the same
478af0767daSMark Adams           } else {
479af0767daSMark Adams             coef = coef_buff;
480*8a6f2e61SMark Adams             for (int f = 0; f < Nfloc; ++f) {
481*8a6f2e61SMark Adams               LandauIdx *const Idxs = &maps[grid]->gIdx[elem][f][0];
482af0767daSMark Adams               for (int b = 0; b < Nb; ++b) {
483af0767daSMark Adams                 PetscInt idx = Idxs[b];
484af0767daSMark Adams                 if (idx >= 0) {
485*8a6f2e61SMark Adams                   coef_buff[f*Nb+b] = d_vertex_f[idx+moffset]; // xarray data, not IP
486af0767daSMark Adams                 } else {
487af0767daSMark Adams                   idx = -idx - 1;
488af0767daSMark Adams                   coef_buff[f*Nb+b] = 0;
489*8a6f2e61SMark Adams                   for (int q = 0; q < maps[grid]->num_face; q++) {
490*8a6f2e61SMark Adams                     PetscInt    id = maps[grid]->c_maps[idx][q].gid;
491*8a6f2e61SMark Adams                     PetscScalar scale = maps[grid]->c_maps[idx][q].scale;
492*8a6f2e61SMark Adams                     coef_buff[f*Nb+b] += scale*d_vertex_f[id+moffset];
493af0767daSMark Adams                   }
494af0767daSMark Adams                 }
495af0767daSMark Adams               }
496af0767daSMark Adams             }
497af0767daSMark Adams           }
498a587d139SMark           Kokkos::parallel_for(Kokkos::TeamThreadRange(team,0,Nq), [=] (int myQi) {
499*8a6f2e61SMark Adams               const PetscInt          ipidx = IP_idx + myQi + elem * Nq, ipidx_g = myQi + elem * Nq;
500e8d2b73aSMark Adams               const PetscReal *const  invJj = &d_invJ_k(ipidx*dim*dim);
501a587d139SMark               const PetscReal         *Bq = &d_BB[myQi*Nb], *Dq = &d_DD[myQi*Nb*dim];
502*8a6f2e61SMark Adams               Kokkos::parallel_for(Kokkos::ThreadVectorRange(team,0,(int)Nfloc), [=] (int f) {
503a587d139SMark                   PetscInt   b, e, d;
504e8d2b73aSMark Adams                   PetscReal  refSpaceDer[LANDAU_DIM];
505*8a6f2e61SMark Adams                   const PetscInt idx = IPf_idx + f*nip_loc + ipidx_g;
506*8a6f2e61SMark Adams                   d_fdf_k(0,idx) = 0.0;
507a587d139SMark                   for (d = 0; d < LANDAU_DIM; ++d) refSpaceDer[d] = 0.0;
508a587d139SMark                   for (b = 0; b < Nb; ++b) {
509a587d139SMark                     const PetscInt    cidx = b;
510*8a6f2e61SMark Adams                     d_fdf_k(0,idx) += Bq[cidx]*PetscRealPart(coef[f*Nb+cidx]);
511addd1e01SJunchao Zhang                     for (d = 0; d < dim; ++d) refSpaceDer[d] += Dq[cidx*dim+d]*PetscRealPart(coef[f*Nb+cidx]);
512a587d139SMark                   }
513a587d139SMark                   for (d = 0; d < dim; ++d) {
514*8a6f2e61SMark Adams                     for (e = 0, d_fdf_k(d+1,idx) = 0.0; e < dim; ++e) {
515*8a6f2e61SMark Adams                       d_fdf_k(d+1,idx) += invJj[e*dim+d]*refSpaceDer[e];
516a587d139SMark                     }
517a587d139SMark                   }
518*8a6f2e61SMark Adams                 }); // f
519*8a6f2e61SMark Adams             }); // myQi
520*8a6f2e61SMark Adams         }
521*8a6f2e61SMark Adams       }); // global elems - fdf
522*8a6f2e61SMark Adams     // #if defined(PETSC_HAVE_CUDA) || defined(PETSC_HAVE_HIP)
523*8a6f2e61SMark Adams     //     ierr = PetscLogGpuFlops(nip_loc*(PetscLogDouble)(2*Nb*(1+dim)));CHKERRQ(ierr);
524*8a6f2e61SMark Adams     // #else
525*8a6f2e61SMark Adams     //     ierr = PetscLogFlops(nip_loc*(PetscLogDouble)(2*Nb*(1+dim)));CHKERRQ(ierr);
526*8a6f2e61SMark Adams     // #endif
527*8a6f2e61SMark Adams     Kokkos::fence();
528*8a6f2e61SMark Adams     ierr = PetscLogGpuTimeEnd();CHKERRQ(ierr); // is this a fence?
529930e68a5SMark Adams     ierr = PetscLogEventEnd(events[8],0,0,0,0);CHKERRQ(ierr);
530*8a6f2e61SMark Adams     if (d_vertex_f_k) delete d_vertex_f_k;
531e6118d12SMark Adams     // Jacobian
532a587d139SMark     ierr = PetscLogEventBegin(events[4],0,0,0,0);CHKERRQ(ierr);
533e6118d12SMark Adams     ierr = PetscLogGpuTimeBegin();CHKERRQ(ierr);
534*8a6f2e61SMark Adams     // #if defined(PETSC_HAVE_CUDA) || defined(PETSC_HAVE_HIP)
535*8a6f2e61SMark Adams     //           ierr = PetscLogGpuFlops(nip_loc*(PetscLogDouble)((nip_loc*(11*Nfloc+ 4*dim*dim) + 6*Nfloc*dim*dim*dim + 10*Nfloc*dim*dim + 4*Nfloc*dim + Nb*Nfloc*Nb*Nq*dim*dim*5)));CHKERRQ(ierr);
536*8a6f2e61SMark Adams     // #else
537*8a6f2e61SMark Adams     //           ierr = PetscLogFlops(nip_loc*(PetscLogDouble)((nip_loc*(11*Nfloc+ 4*dim*dim) + 6*Nfloc*dim*dim*dim + 10*Nfloc*dim*dim + 4*Nfloc*dim + Nb*Nfloc*Nb*Nq*dim*dim*5)));CHKERRQ(ierr);
538*8a6f2e61SMark Adams     // #endif
539*8a6f2e61SMark Adams     const int scr_bytes = 2*(g2_scr_t::shmem_size(dim,Nf_max,Nq) + g3_scr_t::shmem_size(dim,dim,Nf_max,Nq))+fieldMats_scr_t::shmem_size(Nb,Nb)+idx_scr_t::shmem_size(Nb,nfaces)+scale_scr_t::shmem_size(Nb,nfaces);
540*8a6f2e61SMark Adams     ierr = PetscInfo6(plex[0], "Jacobian shared memory size: %d bytes in level %d num_cells_tot=%D team size=%D #face=%D Nf_max=%D\n",scr_bytes,KOKKOS_SHARED_LEVEL,num_cells_tot,team_size,nfaces,Nf_max);CHKERRQ(ierr);
541*8a6f2e61SMark Adams     Kokkos::parallel_for("Jacobian", Kokkos::TeamPolicy<>(num_cells_tot, team_size, /* Kokkos::AUTO */ 16).set_scratch_size(KOKKOS_SHARED_LEVEL, Kokkos::PerTeam(scr_bytes)), KOKKOS_LAMBDA (const team_member team) {
542*8a6f2e61SMark Adams         // find my grid
543*8a6f2e61SMark Adams         PetscInt grid = 0, g_cell = team.league_rank();
544*8a6f2e61SMark Adams         while (g_cell >= d_elem_offset[grid+1]) grid++; // yuck search for grid
545*8a6f2e61SMark Adams         {
546*8a6f2e61SMark Adams           const PetscInt  moffset = d_mat_offset[grid], Nfloc = d_species_offset[grid+1]-d_species_offset[grid], elem = g_cell-d_elem_offset[grid], totDim = Nfloc*Nq;
547*8a6f2e61SMark Adams           const PetscInt  IP_idx = d_ip_offset[grid];
548*8a6f2e61SMark Adams           const PetscInt  f_off = d_species_offset[grid];
549*8a6f2e61SMark Adams           g2_scr_t        g2(team.team_scratch(KOKKOS_SHARED_LEVEL),dim,Nfloc,Nq);
550*8a6f2e61SMark Adams           g3_scr_t        g3(team.team_scratch(KOKKOS_SHARED_LEVEL),dim,dim,Nfloc,Nq);
551*8a6f2e61SMark Adams           g2_scr_t        gg2(team.team_scratch(KOKKOS_SHARED_LEVEL),dim,Nfloc,Nq);
552*8a6f2e61SMark Adams           g3_scr_t        gg3(team.team_scratch(KOKKOS_SHARED_LEVEL),dim,dim,Nfloc,Nq);
553e0eea495SMark           // get g2[] & g3[]
554e0eea495SMark           Kokkos::parallel_for(Kokkos::TeamThreadRange(team,0,Nq), [=] (int myQi) {
555e0eea495SMark               using Kokkos::parallel_reduce;
556*8a6f2e61SMark Adams               const PetscInt                    jpidx_g = myQi + elem * Nq, jpidx = IP_idx + jpidx_g;
557e8d2b73aSMark Adams               const PetscReal* const            invJj = &d_invJ_k(jpidx*dim*dim);
558e8d2b73aSMark Adams               const PetscReal                   vj[3] = {d_x[jpidx], d_y[jpidx], d_z ? d_z[jpidx] : 0}, wj = d_w[jpidx];
559*8a6f2e61SMark Adams               landau_inner_red::TensorValueType gg_temp; // reduce on part of gg2 and g33 for IP jpidx_g
560*8a6f2e61SMark Adams               Kokkos::parallel_reduce(Kokkos::ThreadVectorRange (team, (int)nip_global), [=] (const int& ipidx, landau_inner_red::TensorValueType & ggg) {
561e8d2b73aSMark Adams                   const PetscReal wi = d_w[ipidx], x = d_x[ipidx], y = d_y[ipidx];
56252cdd6eaSMark                   PetscReal       temp1[3] = {0, 0, 0}, temp2 = 0;
563*8a6f2e61SMark Adams                   PetscInt        fieldA,d2,d3,f_off_r,grid_r,ipidx_g,nip_loc_r,Nfloc_r,IPf_idx_r;
564e0eea495SMark #if LANDAU_DIM==2
565*8a6f2e61SMark Adams                   PetscReal Ud[2][2], Uk[2][2], mask = (PetscAbs(vj[0]-x) < 100*PETSC_SQRT_MACHINE_EPSILON && PetscAbs(vj[1]-y) < 100*PETSC_SQRT_MACHINE_EPSILON) ? 0. : 1.;
566*8a6f2e61SMark Adams                   LandauTensor2D(vj, x, y, Ud, Uk, mask);
56752cdd6eaSMark #else
568*8a6f2e61SMark Adams                   PetscReal U[3][3], z = d_z[ipidx], mask = (PetscAbs(vj[0]-x) < 100*PETSC_SQRT_MACHINE_EPSILON && PetscAbs(vj[1]-y) < 100*PETSC_SQRT_MACHINE_EPSILON && PetscAbs(vj[2]-z) < 100*PETSC_SQRT_MACHINE_EPSILON) ? 0. : 1.;
569*8a6f2e61SMark Adams                   LandauTensor3D(vj, x, y, z, U, mask);
57052cdd6eaSMark #endif
571*8a6f2e61SMark Adams                   grid_r = 0;
572*8a6f2e61SMark Adams                   while (ipidx >= d_ip_offset[grid_r+1]) grid_r++; // yuck search for grid
573*8a6f2e61SMark Adams                   f_off_r = d_species_offset[grid_r];
574*8a6f2e61SMark Adams                   ipidx_g = ipidx - d_ip_offset[grid_r];
575*8a6f2e61SMark Adams                   nip_loc_r = d_numCells[grid_r]*Nq;
576*8a6f2e61SMark Adams                   Nfloc_r = d_species_offset[grid_r+1] - d_species_offset[grid_r];
577*8a6f2e61SMark Adams                   IPf_idx_r = d_ipf_offset[grid_r];
578*8a6f2e61SMark Adams                   for (fieldA = 0; fieldA < Nfloc_r; ++fieldA) {
579*8a6f2e61SMark Adams                     const PetscInt idx = IPf_idx_r + fieldA*nip_loc_r + ipidx_g;
580*8a6f2e61SMark Adams                     temp1[0] += d_fdf_k(1,idx)*d_beta[fieldA+f_off_r]*d_invMass[fieldA+f_off_r];
581*8a6f2e61SMark Adams                     temp1[1] += d_fdf_k(2,idx)*d_beta[fieldA+f_off_r]*d_invMass[fieldA+f_off_r];
58252cdd6eaSMark #if LANDAU_DIM==3
583*8a6f2e61SMark Adams                     temp1[2] += d_fdf_k(3,idx)*d_beta[fieldA+f_off_r]*d_invMass[fieldA+f_off_r];
58452cdd6eaSMark #endif
585*8a6f2e61SMark Adams                     temp2    += d_fdf_k(0,idx)*d_beta[fieldA+f_off_r];
58652cdd6eaSMark                   }
58752cdd6eaSMark                   temp1[0] *= wi;
58852cdd6eaSMark                   temp1[1] *= wi;
58952cdd6eaSMark #if LANDAU_DIM==3
59052cdd6eaSMark                   temp1[2] *= wi;
59152cdd6eaSMark #endif
59252cdd6eaSMark                   temp2    *= wi;
59352cdd6eaSMark #if LANDAU_DIM==2
59452cdd6eaSMark                   for (d2 = 0; d2 < 2; d2++) {
595e0eea495SMark                     for (d3 = 0; d3 < 2; ++d3) {
596e0eea495SMark                       /* K = U * grad(f): g2=e: i,A */
59752cdd6eaSMark                       ggg.gg2[d2] += Uk[d2][d3]*temp1[d3];
598e0eea495SMark                       /* D = -U * (I \kron (fx)): g3=f: i,j,A */
59952cdd6eaSMark                       ggg.gg3[d2][d3] += Ud[d2][d3]*temp2;
600e0eea495SMark                     }
601e0eea495SMark                   }
602e0eea495SMark #else
603e0eea495SMark                   for (d2 = 0; d2 < 3; ++d2) {
604e0eea495SMark                     for (d3 = 0; d3 < 3; ++d3) {
605e0eea495SMark                       /* K = U * grad(f): g2 = e: i,A */
60652cdd6eaSMark                       ggg.gg2[d2] += U[d2][d3]*temp1[d3];
607e0eea495SMark                       /* D = -U * (I \kron (fx)): g3 = f: i,j,A */
60852cdd6eaSMark                       ggg.gg3[d2][d3] += U[d2][d3]*temp2;
609e0eea495SMark                     }
610e0eea495SMark                   }
611e0eea495SMark #endif
61252cdd6eaSMark                 }, Kokkos::Sum<landau_inner_red::TensorValueType>(gg_temp));
61352cdd6eaSMark               // add alpha and put in gg2/3
614*8a6f2e61SMark Adams               Kokkos::parallel_for(Kokkos::ThreadVectorRange (team, (int)Nfloc), [&] (const int& fieldA) {
61552cdd6eaSMark                   PetscInt d2,d3;
61652cdd6eaSMark                   for (d2 = 0; d2 < dim; d2++) {
617*8a6f2e61SMark Adams                     gg2(d2,fieldA,myQi) = gg_temp.gg2[d2]*d_alpha[fieldA+f_off];
61852cdd6eaSMark                     for (d3 = 0; d3 < dim; d3++) {
619*8a6f2e61SMark Adams                       gg3(d2,d3,fieldA,myQi) = -gg_temp.gg3[d2][d3]*d_alpha[fieldA+f_off]*d_invMass[fieldA+f_off];
62052cdd6eaSMark                     }
62152cdd6eaSMark                   }
622e0eea495SMark                 });
62352cdd6eaSMark               /* add electric field term once per IP */
624*8a6f2e61SMark Adams               Kokkos::parallel_for(Kokkos::ThreadVectorRange (team, (int)Nfloc), [&] (const int& fieldA) {
625*8a6f2e61SMark Adams                   gg2(dim-1,fieldA,myQi) += d_Eq_m[fieldA+f_off];
62652cdd6eaSMark                 });
627*8a6f2e61SMark Adams               Kokkos::parallel_for(Kokkos::ThreadVectorRange (team, (int)Nfloc), [=] (const int& fieldA) {
628e0eea495SMark                   int d,d2,d3,dp;
629e0eea495SMark                   /* Jacobian transform - g2, g3 - per thread (2D) */
630e0eea495SMark                   for (d = 0; d < dim; ++d) {
63152cdd6eaSMark                     g2(d,fieldA,myQi) = 0;
632e0eea495SMark                     for (d2 = 0; d2 < dim; ++d2) {
63352cdd6eaSMark                       g2(d,fieldA,myQi) += invJj[d*dim+d2]*gg2(d2,fieldA,myQi);
63452cdd6eaSMark                       g3(d,d2,fieldA,myQi) = 0;
635e0eea495SMark                       for (d3 = 0; d3 < dim; ++d3) {
636e0eea495SMark                         for (dp = 0; dp < dim; ++dp) {
63752cdd6eaSMark                           g3(d,d2,fieldA,myQi) += invJj[d*dim + d3]*gg3(d3,dp,fieldA,myQi)*invJj[d2*dim + dp];
638e0eea495SMark                         }
639e0eea495SMark                       }
64052cdd6eaSMark                       g3(d,d2,fieldA,myQi) *= wj;
641e0eea495SMark                     }
64252cdd6eaSMark                     g2(d,fieldA,myQi) *= wj;
643e0eea495SMark                   }
644e0eea495SMark                 });
64552cdd6eaSMark             }); // Nq
646e0eea495SMark           team.team_barrier();
6471eec90e4SMark Adams           { /* assemble */
648*8a6f2e61SMark Adams             fieldMats_scr_t s_fieldMats(team.team_scratch(KOKKOS_SHARED_LEVEL),Nb,Nb); // Only used for GPU assembly (ie, not first pass)
649*8a6f2e61SMark Adams             idx_scr_t       s_idx(team.team_scratch(KOKKOS_SHARED_LEVEL),Nb,nfaces);
650*8a6f2e61SMark Adams             scale_scr_t     s_scale(team.team_scratch(KOKKOS_SHARED_LEVEL),Nb,nfaces);
651*8a6f2e61SMark Adams             for (PetscInt fieldA = 0; fieldA < Nfloc; fieldA++) {
652930e68a5SMark Adams               /* assemble */
6531eec90e4SMark Adams               Kokkos::parallel_for(Kokkos::TeamThreadRange(team,0,Nb), [=] (int f) {
6541eec90e4SMark Adams                   Kokkos::parallel_for(Kokkos::ThreadVectorRange(team,0,Nb), [=] (int g) {
6551eec90e4SMark Adams                       PetscScalar t = 0;
6561eec90e4SMark Adams                       for (int qj = 0 ; qj < Nq ; qj++) { // look at others integration points
657e0eea495SMark                         const PetscReal *BJq = &d_BB[qj*Nb], *DIq = &d_DD[qj*Nb*dim];
6581eec90e4SMark Adams                         for (int d = 0; d < dim; ++d) {
6591eec90e4SMark Adams                           t += DIq[f*dim+d]*g2(d,fieldA,qj)*BJq[g];
6601eec90e4SMark Adams                           for (int d2 = 0; d2 < dim; ++d2) {
6611eec90e4SMark Adams                             t += DIq[f*dim + d]*g3(d,d2,fieldA,qj)*DIq[g*dim + d2];
662e0eea495SMark                           }
663e0eea495SMark                         }
664930e68a5SMark Adams                       }
665*8a6f2e61SMark Adams                       if (elem_mat_num_cells_max_grid) { // CPU assembly
6661eec90e4SMark Adams                         const PetscInt fOff = (fieldA*Nb + f)*totDim + fieldA*Nb + g;
667*8a6f2e61SMark Adams                         d_elem_mats(grid,elem,fOff) = t;
6681eec90e4SMark Adams                       } else s_fieldMats(f,g) = t;
6691eec90e4SMark Adams                     });
6701eec90e4SMark Adams                 });
671*8a6f2e61SMark Adams               if (!elem_mat_num_cells_max_grid) { // GPU assembly
672*8a6f2e61SMark Adams                 landau_mat_assemble (d_mat, team, s_fieldMats, s_idx, s_scale, Nb, Nq, nfaces, moffset, elem, fieldA, maps[grid]);
6731eec90e4SMark Adams               }
6741eec90e4SMark Adams             }
675a587d139SMark           }
676e0eea495SMark         }
677e0eea495SMark       });
678e6118d12SMark Adams     ierr = PetscLogGpuTimeEnd();CHKERRQ(ierr);
6791eec90e4SMark Adams     ierr = PetscLogEventEnd(events[4],0,0,0,0);CHKERRQ(ierr);
6801eec90e4SMark Adams   } else { // mass
6811eec90e4SMark Adams     ierr = PetscLogEventBegin(events[4],0,0,0,0);CHKERRQ(ierr);
682e6118d12SMark Adams     ierr = PetscLogGpuTimeBegin();CHKERRQ(ierr);
683*8a6f2e61SMark Adams     // #if defined(PETSC_HAVE_CUDA) || defined(PETSC_HAVE_HIP)
684*8a6f2e61SMark Adams     //           ierr = PetscLogGpuFlops(nip_loc*(PetscLogDouble)(Nb*Nfloc*Nb*Nq*4));CHKERRQ(ierr);
685*8a6f2e61SMark Adams     //           if (ctx->deviceType == LANDAU_CPU) PetscInfo(plex[grid], "Warning: Landau selected CPU but no support for Kokkos using CPU\n");
686*8a6f2e61SMark Adams     // #else
687*8a6f2e61SMark Adams     //           ierr = PetscLogFlops(nip_loc*(PetscLogDouble)(Nb*Nfloc*Nb*Nq*4));CHKERRQ(ierr);
688*8a6f2e61SMark Adams     // #endif
689*8a6f2e61SMark Adams     int scr_bytes = fieldMats_scr_t::shmem_size(Nq,Nq) + idx_scr_t::shmem_size(Nb,nfaces) + scale_scr_t::shmem_size(Nb,nfaces);
690*8a6f2e61SMark Adams     ierr = PetscInfo6(plex[0], "Mass shared memory size: %d bytes in level %d conc=%D team size=%D #face=%D Nb=%D\n",scr_bytes,KOKKOS_SHARED_LEVEL,conc,team_size,nfaces,Nb);CHKERRQ(ierr);
691*8a6f2e61SMark Adams     Kokkos::parallel_for("Mass", Kokkos::TeamPolicy<>(num_cells_tot, team_size, /* Kokkos::AUTO */ 16).set_scratch_size(KOKKOS_SHARED_LEVEL, Kokkos::PerTeam(scr_bytes)), KOKKOS_LAMBDA (const team_member team) {
6921eec90e4SMark Adams         fieldMats_scr_t s_fieldMats(team.team_scratch(KOKKOS_SHARED_LEVEL),Nb,Nb);
6931eec90e4SMark Adams         idx_scr_t       s_idx(team.team_scratch(KOKKOS_SHARED_LEVEL),Nb,nfaces);
6941eec90e4SMark Adams         scale_scr_t     s_scale(team.team_scratch(KOKKOS_SHARED_LEVEL),Nb,nfaces);
695*8a6f2e61SMark Adams         // find my grid
696*8a6f2e61SMark Adams         PetscInt grid = 0, g_cell = team.league_rank();
697*8a6f2e61SMark Adams         while (g_cell >= d_elem_offset[grid+1]) grid++; // yuck search for grid
698*8a6f2e61SMark Adams         {
699*8a6f2e61SMark Adams           const PetscInt moffset = d_mat_offset[grid], Nfloc = d_species_offset[grid+1]-d_species_offset[grid], totDim = Nfloc*Nq, elem = g_cell-d_elem_offset[grid];
700*8a6f2e61SMark Adams           const PetscInt IP_idx = d_ip_offset[grid];
701*8a6f2e61SMark Adams           for (int fieldA = 0; fieldA < Nfloc; fieldA++) {
7021eec90e4SMark Adams             /* assemble */
7031eec90e4SMark Adams             Kokkos::parallel_for(Kokkos::TeamThreadRange(team,0,Nb), [=] (int f) {
7041eec90e4SMark Adams                 Kokkos::parallel_for(Kokkos::ThreadVectorRange(team,0,Nb), [=] (int g) {
7051eec90e4SMark Adams                     PetscScalar    t = 0;
7061eec90e4SMark Adams                     for (int qj = 0 ; qj < Nq ; qj++) { // look at others integration points
7071eec90e4SMark Adams                       const PetscReal *BJq = &d_BB[qj*Nb];
708*8a6f2e61SMark Adams                       const PetscInt jpidx = IP_idx + qj + elem * Nq;
709*8a6f2e61SMark Adams                       if (dim==2) {
710*8a6f2e61SMark Adams                         t += BJq[f] * d_w_k(jpidx) * shift * BJq[g] * 2. * PETSC_PI;
711*8a6f2e61SMark Adams                       } else {
712*8a6f2e61SMark Adams                         t += BJq[f] * d_w_k(jpidx) * shift * BJq[g];
7131eec90e4SMark Adams                       }
714*8a6f2e61SMark Adams                     }
715*8a6f2e61SMark Adams                     if (elem_mat_num_cells_max_grid) {
7161eec90e4SMark Adams                       const PetscInt fOff = (fieldA*Nb + f)*totDim + fieldA*Nb + g;
717*8a6f2e61SMark Adams                       d_elem_mats(grid,elem,fOff) = t;
7181eec90e4SMark Adams                     } else s_fieldMats(f,g) = t;
719e0eea495SMark                   });
720e0eea495SMark               });
721*8a6f2e61SMark Adams             if (!elem_mat_num_cells_max_grid) { // device assembly
722*8a6f2e61SMark Adams               landau_mat_assemble (d_mat, team, s_fieldMats, s_idx, s_scale, Nb, Nq,nfaces, moffset, elem, fieldA, maps[grid]);
723*8a6f2e61SMark Adams             } // else not using GPU assembly
7241eec90e4SMark Adams           }
7251eec90e4SMark Adams         }
7261eec90e4SMark Adams       });
727e6118d12SMark Adams     ierr = PetscLogGpuTimeEnd();CHKERRQ(ierr);
7281eec90e4SMark Adams     ierr = PetscLogEventEnd(events[4],0,0,0,0);CHKERRQ(ierr);
7291eec90e4SMark Adams   }
730e8d2b73aSMark Adams   Kokkos::fence();
731*8a6f2e61SMark Adams   if (elem_mat_num_cells_max_grid) { // CPU assembly
732*8a6f2e61SMark Adams     Kokkos::View<PetscScalar***, Kokkos::LayoutRight>::HostMirror h_elem_mats = Kokkos::create_mirror_view(d_elem_mats);
733e0eea495SMark     Kokkos::deep_copy (h_elem_mats, d_elem_mats);
734*8a6f2e61SMark Adams     for (PetscInt grid=0 ; grid<num_grids ; grid++) {
735*8a6f2e61SMark Adams       const PetscInt  Nfloc = a_species_offset[grid+1]-a_species_offset[grid], totDim = Nfloc*Nq;
736*8a6f2e61SMark Adams       PetscSection    section, globalSection;
737*8a6f2e61SMark Adams       PetscInt        cStart,cEnd;
738*8a6f2e61SMark Adams       ierr = PetscLogEventBegin(events[5],0,0,0,0);CHKERRQ(ierr);
739*8a6f2e61SMark Adams       ierr = DMPlexGetHeightStratum(plex[grid],0,&cStart,&cEnd);CHKERRQ(ierr);
740*8a6f2e61SMark Adams       ierr = DMGetLocalSection(plex[grid], &section);CHKERRQ(ierr);
741*8a6f2e61SMark Adams       ierr = DMGetGlobalSection(plex[grid], &globalSection);CHKERRQ(ierr);
742e0eea495SMark       ierr = PetscLogEventEnd(events[5],0,0,0,0);CHKERRQ(ierr);
743e0eea495SMark       ierr = PetscLogEventBegin(events[6],0,0,0,0);CHKERRQ(ierr);
744*8a6f2e61SMark Adams       for (PetscInt ej = cStart ; ej < cEnd; ++ej) {
745*8a6f2e61SMark Adams         const PetscScalar *elMat = &h_elem_mats(grid,ej-cStart,0);
746*8a6f2e61SMark Adams         ierr = DMPlexMatSetClosure(plex[grid], section, globalSection, subJ[grid], ej, elMat, ADD_VALUES);CHKERRQ(ierr);
747*8a6f2e61SMark Adams         if (grid==0 && ej==-1) {
748e0eea495SMark           int d,f;
749*8a6f2e61SMark Adams           PetscPrintf(PETSC_COMM_SELF,"Kokkos Element matrix %d/%d\n",1,(int)a_numCells[grid]);
750e0eea495SMark           for (d = 0; d < totDim; ++d){
75152cdd6eaSMark             for (f = 0; f < totDim; ++f) PetscPrintf(PETSC_COMM_SELF," %12.5e",  PetscRealPart(elMat[d*totDim + f]));
75252cdd6eaSMark             PetscPrintf(PETSC_COMM_SELF,"\n");
753e0eea495SMark           }
754e8d2b73aSMark Adams           exit(14);
755e0eea495SMark         }
756e0eea495SMark       }
757*8a6f2e61SMark Adams       // transition to use of maps for a Kokkos VecGetClosure
7580dacec1dSMark Adams       if (ctx->gpu_assembly) {
759*8a6f2e61SMark Adams         if (!(a_elem_closure || a_xarray)) SETERRQ(PETSC_COMM_SELF, PETSC_ERR_PLIB, "transition in Mass");
760e0eea495SMark       }
761*8a6f2e61SMark Adams       if (!container) {   // move nest matrix to global JacP
762*8a6f2e61SMark Adams         PetscInt          moffset = a_mat_offset[grid], nloc, nzl, colbuf[1024], row;
763*8a6f2e61SMark Adams         const PetscInt    *cols;
764*8a6f2e61SMark Adams         const PetscScalar *vals;
765*8a6f2e61SMark Adams         Mat               B = subJ[grid];
766*8a6f2e61SMark Adams         ierr = MatAssemblyBegin(B, MAT_FINAL_ASSEMBLY);CHKERRQ(ierr);
767*8a6f2e61SMark Adams         ierr = MatAssemblyEnd(B, MAT_FINAL_ASSEMBLY);CHKERRQ(ierr);
768*8a6f2e61SMark Adams         ierr = MatGetSize(B, &nloc, NULL);CHKERRQ(ierr);
769*8a6f2e61SMark Adams         if (nloc != a_mat_offset[grid+1] - moffset) SETERRQ2(PetscObjectComm((PetscObject) B), PETSC_ERR_PLIB, "nloc %D != mat_offset[grid+1] - moffset = %D",nloc, a_mat_offset[grid+1] - moffset);
770*8a6f2e61SMark Adams         for (int i=0 ; i<nloc ; i++) {
771*8a6f2e61SMark Adams           ierr = MatGetRow(B,i,&nzl,&cols,&vals);CHKERRQ(ierr);
772*8a6f2e61SMark Adams           if (nzl>1024) SETERRQ1(PetscObjectComm((PetscObject) B), PETSC_ERR_PLIB, "Row too big: %D",nzl);
773*8a6f2e61SMark Adams           for (int j=0; j<nzl; j++) colbuf[j] = cols[j] + moffset;
774*8a6f2e61SMark Adams           row = i + moffset;
775*8a6f2e61SMark Adams           ierr = MatSetValues(JacP,1,&row,nzl,colbuf,vals,ADD_VALUES);CHKERRQ(ierr);
776*8a6f2e61SMark Adams           ierr = MatRestoreRow(B,i,&nzl,&cols,&vals);CHKERRQ(ierr);
777*8a6f2e61SMark Adams         }
778*8a6f2e61SMark Adams         ierr = MatDestroy(&subJ[grid]);CHKERRQ(ierr);
779*8a6f2e61SMark Adams       }
780*8a6f2e61SMark Adams       ierr = PetscLogEventEnd(events[6],0,0,0,0);CHKERRQ(ierr);
781*8a6f2e61SMark Adams     } // grids
782a587d139SMark   }
783e0eea495SMark   PetscFunctionReturn(0);
784e0eea495SMark }
785e0eea495SMark } // extern "C"
786