1 /* 2 Implements the Kokkos kernel 3 */ 4 #include <petscconf.h> 5 #include <petscvec_kokkos.hpp> 6 #include <petsc/private/dmpleximpl.h> /*I "petscdmplex.h" I*/ 7 #include <petsclandau.h> 8 #include <petscts.h> 9 10 #include <Kokkos_Core.hpp> 11 #include <cstdio> 12 typedef Kokkos::TeamPolicy<>::member_type team_member; 13 #include "../land_tensors.h" 14 #include <petscaijdevice.h> 15 16 namespace landau_inner_red { // namespace helps with name resolution in reduction identity 17 template< class ScalarType > 18 struct array_type { 19 ScalarType gg2[LANDAU_DIM]; 20 ScalarType gg3[LANDAU_DIM][LANDAU_DIM]; 21 22 KOKKOS_INLINE_FUNCTION // Default constructor - Initialize to 0's 23 array_type() { 24 for (int j = 0; j < LANDAU_DIM; j++) { 25 gg2[j] = 0; 26 for (int k = 0; k < LANDAU_DIM; k++) { 27 gg3[j][k] = 0; 28 } 29 } 30 } 31 KOKKOS_INLINE_FUNCTION // Copy Constructor 32 array_type(const array_type & rhs) { 33 for (int j = 0; j < LANDAU_DIM; j++) { 34 gg2[j] = rhs.gg2[j]; 35 for (int k = 0; k < LANDAU_DIM; k++) { 36 gg3[j][k] = rhs.gg3[j][k]; 37 } 38 } 39 } 40 KOKKOS_INLINE_FUNCTION // add operator 41 array_type& operator += (const array_type& src) 42 { 43 for (int j = 0; j < LANDAU_DIM; j++) { 44 gg2[j] += src.gg2[j]; 45 for (int k = 0; k < LANDAU_DIM; k++) { 46 gg3[j][k] += src.gg3[j][k]; 47 } 48 } 49 return *this; 50 } 51 KOKKOS_INLINE_FUNCTION // volatile add operator 52 void operator += (const volatile array_type& src) volatile 53 { 54 for (int j = 0; j < LANDAU_DIM; j++) { 55 gg2[j] += src.gg2[j]; 56 for (int k = 0; k < LANDAU_DIM; k++) { 57 gg3[j][k] += src.gg3[j][k]; 58 } 59 } 60 } 61 }; 62 typedef array_type<PetscReal> TensorValueType; // used to simplify code below 63 } 64 65 namespace Kokkos { //reduction identity must be defined in Kokkos namespace 66 template<> 67 struct reduction_identity< landau_inner_red::TensorValueType > { 68 KOKKOS_FORCEINLINE_FUNCTION static landau_inner_red::TensorValueType sum() { 69 return landau_inner_red::TensorValueType(); 70 } 71 }; 72 } 73 74 extern "C" { 75 PetscErrorCode LandauKokkosCreateMatMaps(P4estVertexMaps maps[], pointInterpolationP4est (*pointMaps)[LANDAU_MAX_Q_FACE], PetscInt Nf[], PetscInt Nq, PetscInt grid) 76 { 77 P4estVertexMaps h_maps; /* host container */ 78 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 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 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 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); 82 83 PetscFunctionBegin; 84 Kokkos::deep_copy (*d_gidx, h_gidx); 85 Kokkos::deep_copy (*d_points, h_points); 86 h_maps.num_elements = maps[grid].num_elements; 87 h_maps.num_face = maps[grid].num_face; 88 h_maps.num_reduced = maps[grid].num_reduced; 89 h_maps.deviceType = maps[grid].deviceType; 90 h_maps.numgrids = maps[grid].numgrids; 91 h_maps.Nf = Nf[grid]; 92 h_maps.Nq = Nq; 93 h_maps.c_maps = (pointInterpolationP4est (*)[LANDAU_MAX_Q_FACE]) d_points->data(); 94 maps[grid].vp1 = (void*)d_points; 95 h_maps.gIdx = (LandauIdx (*)[LANDAU_MAX_SPECIES][LANDAU_MAX_NQ]) d_gidx->data(); 96 maps[grid].vp2 = (void*)d_gidx; 97 { 98 Kokkos::View<P4estVertexMaps, Kokkos::HostSpace> h_maps_k(&h_maps); 99 Kokkos::View<P4estVertexMaps> *d_maps_k = new Kokkos::View<P4estVertexMaps>(Kokkos::create_mirror(Kokkos::DefaultExecutionSpace::memory_space(),h_maps_k)); 100 Kokkos::deep_copy (*d_maps_k, h_maps_k); 101 maps[grid].d_self = d_maps_k->data(); 102 maps[grid].vp3 = (void*)d_maps_k; 103 } 104 PetscFunctionReturn(0); 105 } 106 PetscErrorCode LandauKokkosDestroyMatMaps(P4estVertexMaps maps[], PetscInt num_grids) 107 { 108 PetscFunctionBegin; 109 for (PetscInt grid=0;grid<num_grids;grid++) { 110 auto a = static_cast<Kokkos::View<pointInterpolationP4est*[LANDAU_MAX_Q_FACE], Kokkos::LayoutRight>*>(maps[grid].vp1); 111 auto b = static_cast<Kokkos::View<LandauIdx*[LANDAU_MAX_SPECIES][LANDAU_MAX_NQ], Kokkos::LayoutRight>*>(maps[grid].vp2); 112 auto c = static_cast<Kokkos::View<P4estVertexMaps*>*>(maps[grid].vp3); 113 delete a; delete b; delete c; 114 } 115 PetscFunctionReturn(0); 116 } 117 118 PetscErrorCode LandauKokkosStaticDataSet(DM plex, const PetscInt Nq, const PetscInt batch_sz, const PetscInt num_grids, 119 PetscInt a_numCells[], PetscInt a_species_offset[], PetscInt a_mat_offset[], 120 PetscReal a_nu_alpha[], PetscReal a_nu_beta[], PetscReal a_invMass[], PetscReal a_invJ[], 121 PetscReal a_x[], PetscReal a_y[], PetscReal a_z[], PetscReal a_w[], LandauStaticData *SData_d) 122 { 123 PetscReal *BB,*DD; 124 PetscErrorCode ierr; 125 PetscTabulation *Tf; 126 PetscInt dim; 127 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; 128 PetscDS prob; 129 130 PetscFunctionBegin; 131 ierr = DMGetDimension(plex, &dim);CHKERRQ(ierr); 132 ierr = DMGetDS(plex, &prob);CHKERRQ(ierr); 133 if (LANDAU_DIM != dim) SETERRQ2(PETSC_COMM_WORLD, PETSC_ERR_PLIB, "dim %D != LANDAU_DIM %d",dim,LANDAU_DIM); 134 ierr = PetscDSGetTabulation(prob, &Tf);CHKERRQ(ierr); 135 BB = Tf[0]->T[0]; DD = Tf[0]->T[1]; 136 ip_offset[0] = ipf_offset[0] = elem_offset[0] = 0; 137 nip = 0; 138 IPf_sz = 0; 139 for (PetscInt grid=0 ; grid<num_grids ; grid++) { 140 PetscInt nfloc = a_species_offset[grid+1] - a_species_offset[grid]; 141 elem_offset[grid+1] = elem_offset[grid] + a_numCells[grid]; 142 nip += a_numCells[grid]*Nq; 143 ip_offset[grid+1] = nip; 144 IPf_sz += Nq*nfloc*a_numCells[grid]; 145 ipf_offset[grid+1] = IPf_sz; 146 } 147 Nftot = a_species_offset[num_grids]; 148 ierr = PetscKokkosInitializeCheck();CHKERRQ(ierr); 149 { 150 const Kokkos::View<PetscReal*, Kokkos::LayoutLeft, Kokkos::HostSpace, Kokkos::MemoryTraits<Kokkos::Unmanaged> > h_alpha (a_nu_alpha, Nftot); 151 auto alpha = new Kokkos::View<PetscReal*, Kokkos::LayoutLeft> ("alpha", Nftot); 152 SData_d->alpha = static_cast<void*>(alpha); 153 const Kokkos::View<PetscReal*, Kokkos::LayoutLeft, Kokkos::HostSpace, Kokkos::MemoryTraits<Kokkos::Unmanaged> > h_beta (a_nu_beta, Nftot); 154 auto beta = new Kokkos::View<PetscReal*, Kokkos::LayoutLeft> ("beta", Nftot); 155 SData_d->beta = static_cast<void*>(beta); 156 const Kokkos::View<PetscReal*, Kokkos::LayoutLeft, Kokkos::HostSpace, Kokkos::MemoryTraits<Kokkos::Unmanaged> > h_invMass (a_invMass,Nftot); 157 auto invMass = new Kokkos::View<PetscReal*, Kokkos::LayoutLeft> ("invMass", Nftot); 158 SData_d->invMass = static_cast<void*>(invMass); 159 const Kokkos::View<PetscReal*, Kokkos::LayoutLeft, Kokkos::HostSpace, Kokkos::MemoryTraits<Kokkos::Unmanaged> > h_BB (BB,Nq*Nb); 160 auto B = new Kokkos::View<PetscReal*, Kokkos::LayoutLeft> ("B", Nq*Nb); 161 SData_d->B = static_cast<void*>(B); 162 const Kokkos::View<PetscReal*, Kokkos::LayoutLeft, Kokkos::HostSpace, Kokkos::MemoryTraits<Kokkos::Unmanaged> > h_DD (DD,Nq*Nb*dim); 163 auto D = new Kokkos::View<PetscReal*, Kokkos::LayoutLeft> ("D", Nq*Nb*dim); 164 SData_d->D = static_cast<void*>(D); 165 const Kokkos::View<PetscReal*, Kokkos::LayoutLeft, Kokkos::HostSpace, Kokkos::MemoryTraits<Kokkos::Unmanaged> > h_invJ (a_invJ, nip*dim*dim); 166 auto invJ = new Kokkos::View<PetscReal*, Kokkos::LayoutLeft> ("invJ", nip*dim*dim); 167 SData_d->invJ = static_cast<void*>(invJ); 168 const Kokkos::View<PetscReal*, Kokkos::LayoutLeft, Kokkos::HostSpace, Kokkos::MemoryTraits<Kokkos::Unmanaged> > h_x (a_x, nip); 169 auto x = new Kokkos::View<PetscReal*, Kokkos::LayoutLeft> ("x", nip); 170 SData_d->x = static_cast<void*>(x); 171 const Kokkos::View<PetscReal*, Kokkos::LayoutLeft, Kokkos::HostSpace, Kokkos::MemoryTraits<Kokkos::Unmanaged> > h_y (a_y, nip); 172 auto y = new Kokkos::View<PetscReal*, Kokkos::LayoutLeft> ("y", nip); 173 SData_d->y = static_cast<void*>(y); 174 const Kokkos::View<PetscReal*, Kokkos::LayoutLeft, Kokkos::HostSpace, Kokkos::MemoryTraits<Kokkos::Unmanaged> > h_w (a_w, nip); 175 auto w = new Kokkos::View<PetscReal*, Kokkos::LayoutLeft> ("w", nip); 176 SData_d->w = static_cast<void*>(w); 177 178 Kokkos::deep_copy (*alpha, h_alpha); 179 Kokkos::deep_copy (*beta, h_beta); 180 Kokkos::deep_copy (*invMass, h_invMass); 181 Kokkos::deep_copy (*B, h_BB); 182 Kokkos::deep_copy (*D, h_DD); 183 Kokkos::deep_copy (*invJ, h_invJ); 184 Kokkos::deep_copy (*x, h_x); 185 Kokkos::deep_copy (*y, h_y); 186 Kokkos::deep_copy (*w, h_w); 187 188 if (dim==3) { 189 const Kokkos::View<PetscReal*, Kokkos::LayoutLeft, Kokkos::HostSpace, Kokkos::MemoryTraits<Kokkos::Unmanaged> > h_z (a_z , dim==3 ? nip : 0); 190 auto z = new Kokkos::View<PetscReal*, Kokkos::LayoutLeft> ("z", nip); 191 SData_d->z = static_cast<void*>(z); 192 Kokkos::deep_copy (*z, h_z); 193 } else SData_d->z = NULL; 194 195 // 196 const Kokkos::View<PetscInt*, Kokkos::LayoutLeft, Kokkos::HostSpace, Kokkos::MemoryTraits<Kokkos::Unmanaged> > h_NCells (a_numCells, num_grids); 197 auto nc = new Kokkos::View<PetscInt*, Kokkos::LayoutLeft> ("NCells",num_grids); 198 SData_d->NCells = static_cast<void*>(nc); 199 Kokkos::deep_copy (*nc, h_NCells); 200 201 const Kokkos::View<PetscInt*, Kokkos::LayoutLeft, Kokkos::HostSpace, Kokkos::MemoryTraits<Kokkos::Unmanaged> > h_species_offset (a_species_offset, num_grids+1); 202 auto soff = new Kokkos::View<PetscInt*, Kokkos::LayoutLeft> ("species_offset",num_grids+1); 203 SData_d->species_offset = static_cast<void*>(soff); 204 Kokkos::deep_copy (*soff, h_species_offset); 205 206 const Kokkos::View<PetscInt*, Kokkos::LayoutLeft, Kokkos::HostSpace, Kokkos::MemoryTraits<Kokkos::Unmanaged> > h_mat_offset (a_mat_offset, num_grids+1); 207 auto moff = new Kokkos::View<PetscInt*, Kokkos::LayoutLeft> ("mat_offset",num_grids+1); 208 SData_d->mat_offset = static_cast<void*>(moff); 209 Kokkos::deep_copy (*moff, h_mat_offset); 210 211 const Kokkos::View<PetscInt*, Kokkos::LayoutLeft, Kokkos::HostSpace, Kokkos::MemoryTraits<Kokkos::Unmanaged> > h_ip_offset (ip_offset, num_grids+1); 212 auto ipoff = new Kokkos::View<PetscInt*, Kokkos::LayoutLeft> ("ip_offset",num_grids+1); 213 SData_d->ip_offset = static_cast<void*>(ipoff); 214 Kokkos::deep_copy (*ipoff, h_ip_offset); 215 216 const Kokkos::View<PetscInt*, Kokkos::LayoutLeft, Kokkos::HostSpace, Kokkos::MemoryTraits<Kokkos::Unmanaged> > h_elem_offset (elem_offset, num_grids+1); 217 auto eoff = new Kokkos::View<PetscInt*, Kokkos::LayoutLeft> ("elem_offset",num_grids+1); 218 SData_d->elem_offset = static_cast<void*>(eoff); 219 Kokkos::deep_copy (*eoff, h_elem_offset); 220 221 const Kokkos::View<PetscInt*, Kokkos::LayoutLeft, Kokkos::HostSpace, Kokkos::MemoryTraits<Kokkos::Unmanaged> > h_ipf_offset (ipf_offset, num_grids+1); 222 auto ipfoff = new Kokkos::View<PetscInt*, Kokkos::LayoutLeft> ("ipf_offset",num_grids+1); 223 SData_d->ipf_offset = static_cast<void*>(ipfoff); 224 Kokkos::deep_copy (*ipfoff, h_ipf_offset); 225 #if defined(LANDAU_LAYOUT_LEFT) // preallocate dynamic data, no copy 226 auto ipfdf_data = new Kokkos::View<PetscReal***, Kokkos::LayoutLeft > ("fdf", batch_sz, dim+1, IPf_sz); 227 #else 228 auto ipfdf_data = new Kokkos::View<PetscReal***, Kokkos::LayoutRight > ("fdf",batch_sz, dim+1, IPf_sz); 229 #endif 230 SData_d->ipfdf_data = static_cast<void*>(ipfdf_data); 231 auto Eq_m = new Kokkos::View<PetscReal*, Kokkos::LayoutLeft> ("Eq_m",Nftot); // allocate but do not set, same for whole batch 232 SData_d->Eq_m = static_cast<void*>(Eq_m); 233 } 234 SData_d->maps = NULL; // not used 235 PetscFunctionReturn(0); 236 } 237 238 PetscErrorCode LandauKokkosStaticDataClear(LandauStaticData *SData_d) 239 { 240 PetscFunctionBegin; 241 if (SData_d->alpha) { 242 auto alpha = static_cast<Kokkos::View<PetscReal*, Kokkos::LayoutLeft>*>(SData_d->alpha); 243 delete alpha; 244 SData_d->alpha = NULL; 245 auto beta = static_cast<Kokkos::View<PetscReal*, Kokkos::LayoutLeft>*>(SData_d->beta); 246 delete beta; 247 auto invMass = static_cast<Kokkos::View<PetscReal*, Kokkos::LayoutLeft>*>(SData_d->invMass); 248 delete invMass; 249 auto B = static_cast<Kokkos::View<PetscReal*, Kokkos::LayoutLeft>*>(SData_d->B); 250 delete B; 251 auto D = static_cast<Kokkos::View<PetscReal*, Kokkos::LayoutLeft>*>(SData_d->D); 252 delete D; 253 auto invJ = static_cast<Kokkos::View<PetscReal*, Kokkos::LayoutLeft>*>(SData_d->invJ); 254 delete invJ; 255 auto x = static_cast<Kokkos::View<PetscReal*, Kokkos::LayoutLeft>*>(SData_d->x); 256 delete x; 257 auto y = static_cast<Kokkos::View<PetscReal*, Kokkos::LayoutLeft>*>(SData_d->y); 258 delete y; 259 if (SData_d->z) { 260 auto z = static_cast<Kokkos::View<PetscReal*, Kokkos::LayoutLeft>*>(SData_d->z); 261 delete z; 262 } 263 #if defined(LANDAU_LAYOUT_LEFT) // preallocate dynamic data, no copy 264 auto z = static_cast<Kokkos::View<PetscReal***, Kokkos::LayoutLeft>*>(SData_d->ipfdf_data); 265 #else 266 auto z = static_cast<Kokkos::View<PetscReal***, Kokkos::LayoutRight>*>(SData_d->ipfdf_data); 267 #endif 268 delete z; 269 auto w = static_cast<Kokkos::View<PetscReal*, Kokkos::LayoutLeft>*>(SData_d->w); 270 delete w; 271 auto Eq_m = static_cast<Kokkos::View<PetscReal*, Kokkos::LayoutLeft>*>(SData_d->Eq_m); 272 delete Eq_m; 273 // offset 274 auto nc = static_cast<Kokkos::View<PetscInt*, Kokkos::LayoutLeft>*>(SData_d->NCells); 275 delete nc; 276 auto soff = static_cast<Kokkos::View<PetscInt*, Kokkos::LayoutLeft>*>(SData_d->species_offset); 277 delete soff; 278 auto moff = static_cast<Kokkos::View<PetscInt*, Kokkos::LayoutLeft>*>(SData_d->mat_offset); 279 delete moff; 280 auto ipoff = static_cast<Kokkos::View<PetscInt*, Kokkos::LayoutLeft>*>(SData_d->ip_offset); 281 delete ipoff; 282 auto eoff = static_cast<Kokkos::View<PetscInt*, Kokkos::LayoutLeft>*>(SData_d->elem_offset); 283 delete eoff; 284 auto ipfoff = static_cast<Kokkos::View<PetscInt*, Kokkos::LayoutLeft>*>(SData_d->ipf_offset); 285 delete ipfoff; 286 } 287 PetscFunctionReturn(0); 288 } 289 290 #define KOKKOS_SHARED_LEVEL 1 291 KOKKOS_INLINE_FUNCTION 292 PetscErrorCode landau_mat_assemble(PetscSplitCSRDataStructure d_mat, const team_member team, 293 Kokkos::View<PetscScalar**, Kokkos::LayoutRight, Kokkos::DefaultExecutionSpace::scratch_memory_space> s_fieldMats, 294 Kokkos::View<PetscInt**, Kokkos::LayoutRight, Kokkos::DefaultExecutionSpace::scratch_memory_space> s_idx, 295 Kokkos::View<PetscReal**, Kokkos::LayoutRight, Kokkos::DefaultExecutionSpace::scratch_memory_space> s_scale, 296 const PetscInt Nb, const PetscInt Nq, const PetscInt nfaces, const PetscInt moffset, const PetscInt elem, const PetscInt fieldA, const P4estVertexMaps *d_maps) 297 { 298 const LandauIdx *const Idxs = &d_maps->gIdx[elem][fieldA][0]; 299 team.team_barrier(); 300 Kokkos::parallel_for(Kokkos::TeamVectorRange(team,0,Nb), [=] (int f) { 301 PetscInt q, idx = Idxs[f]; 302 if (idx >= 0) { 303 s_idx(f,0) = idx + moffset; 304 s_scale(f,0) = 1.; 305 } else { 306 idx = -idx - 1; 307 for (q = 0; q < nfaces; q++) { 308 s_idx(f,q) = d_maps->c_maps[idx][q].gid + moffset; 309 s_scale(f,q) = d_maps->c_maps[idx][q].scale; 310 } 311 } 312 }); 313 team.team_barrier(); 314 Kokkos::parallel_for(Kokkos::TeamThreadRange(team,0,Nb), [=] (int f) { 315 PetscInt nr,idx = Idxs[f]; 316 if (idx >= 0) { 317 nr = 1; 318 } else { 319 nr = nfaces; 320 } 321 Kokkos::parallel_for(Kokkos::ThreadVectorRange(team,0,Nb), [=] (int g) { 322 PetscScalar vals[LANDAU_MAX_Q_FACE*LANDAU_MAX_Q_FACE]; 323 PetscInt q,d,nc,idx = Idxs[g]; 324 if (idx >= 0) { 325 nc = 1; 326 } else { 327 nc = nfaces; 328 } 329 for (q = 0; q < nr; q++) { 330 for (d = 0; d < nc; d++) { 331 vals[q*nc + d] = s_scale(f,q)*s_scale(g,d)*s_fieldMats(f,g); 332 } 333 } 334 MatSetValuesDevice(d_mat,nr,&s_idx(f,0),nc,&s_idx(g,0),vals,ADD_VALUES); 335 }); 336 }); 337 return 0; 338 } 339 340 PetscErrorCode LandauKokkosJacobian(DM plex[], const PetscInt Nq, const PetscInt batch_sz, const PetscInt num_grids, const PetscInt a_numCells[], PetscReal a_Eq_m[], PetscScalar a_elem_closure[], 341 const PetscScalar a_xarray[], const LandauStaticData *SData_d, const PetscInt num_sub_blocks, const PetscReal shift, 342 const PetscLogEvent events[], const PetscInt a_mat_offset[], const PetscInt a_species_offset[], Mat subJ[], Mat JacP) 343 { 344 using scr_mem_t = Kokkos::DefaultExecutionSpace::scratch_memory_space; 345 using fieldMats_scr_t = Kokkos::View<PetscScalar**, Kokkos::LayoutRight, scr_mem_t>; 346 using real2_scr_t = Kokkos::View<PetscScalar**, Kokkos::LayoutRight, scr_mem_t>; 347 using idx_scr_t = Kokkos::View<PetscInt**, Kokkos::LayoutRight, scr_mem_t>; 348 using scale_scr_t = Kokkos::View<PetscReal**, Kokkos::LayoutRight, scr_mem_t>; 349 using g2_scr_t = Kokkos::View<PetscReal***, Kokkos::LayoutRight, scr_mem_t>; 350 using g3_scr_t = Kokkos::View<PetscReal****, Kokkos::LayoutRight, scr_mem_t>; 351 PetscErrorCode ierr; 352 PetscInt Nb=Nq,dim,num_cells_max,Nf_max,num_cells_batch; 353 int nfaces=0; 354 LandauCtx *ctx; 355 PetscReal *d_Eq_m=NULL; 356 PetscScalar *d_vertex_f=NULL; 357 P4estVertexMaps *maps[LANDAU_MAX_GRIDS]; // this gets captured 358 PetscSplitCSRDataStructure d_mat; 359 PetscContainer container; 360 const int conc = Kokkos::DefaultExecutionSpace().concurrency(), openmp = !!(conc < 1000), team_size = (openmp==0) ? Nq : 1; 361 auto d_alpha_k = static_cast<Kokkos::View<PetscReal*, Kokkos::LayoutLeft>*>(SData_d->alpha); //static data 362 const PetscReal *d_alpha = d_alpha_k->data(); 363 const PetscInt Nftot = d_alpha_k->size(); // total number of species 364 auto d_beta_k = static_cast<Kokkos::View<PetscReal*, Kokkos::LayoutLeft>*>(SData_d->beta); 365 const PetscReal *d_beta = d_beta_k->data(); 366 auto d_invMass_k = static_cast<Kokkos::View<PetscReal*, Kokkos::LayoutLeft>*>(SData_d->invMass); 367 const PetscReal *d_invMass = d_invMass_k->data(); 368 auto d_B_k = static_cast<Kokkos::View<PetscReal*, Kokkos::LayoutLeft>*>(SData_d->B); 369 const PetscReal *d_BB = d_B_k->data(); 370 auto d_D_k = static_cast<Kokkos::View<PetscReal*, Kokkos::LayoutLeft>*>(SData_d->D); 371 const PetscReal *d_DD = d_D_k->data(); 372 auto d_invJ_k = *static_cast<Kokkos::View<PetscReal*, Kokkos::LayoutLeft>*>(SData_d->invJ); // use Kokkos vector in kernels 373 auto d_x_k = static_cast<Kokkos::View<PetscReal*, Kokkos::LayoutLeft>*>(SData_d->x); //static data 374 const PetscReal *d_x = d_x_k->data(); 375 auto d_y_k = static_cast<Kokkos::View<PetscReal*, Kokkos::LayoutLeft>*>(SData_d->y); //static data 376 const PetscReal *d_y = d_y_k->data(); 377 auto d_z_k = static_cast<Kokkos::View<PetscReal*, Kokkos::LayoutLeft>*>(SData_d->z); //static data 378 const PetscReal *d_z = (LANDAU_DIM==3) ? d_z_k->data() : NULL; 379 auto d_w_k = *static_cast<Kokkos::View<PetscReal*, Kokkos::LayoutLeft>*>(SData_d->w); //static data 380 const PetscReal *d_w = d_w_k.data(); 381 // grid offsets - single vertex grid data 382 auto d_numCells_k = static_cast<Kokkos::View<PetscInt*, Kokkos::LayoutLeft>*>(SData_d->NCells); 383 const PetscInt *d_numCells = d_numCells_k->data(); 384 auto d_species_offset_k = static_cast<Kokkos::View<PetscInt*, Kokkos::LayoutLeft>*>(SData_d->species_offset); 385 const PetscInt *d_species_offset = d_species_offset_k->data(); 386 auto d_mat_offset_k = static_cast<Kokkos::View<PetscInt*, Kokkos::LayoutLeft>*>(SData_d->mat_offset); 387 const PetscInt *d_mat_offset = d_mat_offset_k->data(); 388 auto d_ip_offset_k = static_cast<Kokkos::View<PetscInt*, Kokkos::LayoutLeft>*>(SData_d->ip_offset); 389 const PetscInt *d_ip_offset = d_ip_offset_k->data(); 390 auto d_ipf_offset_k = static_cast<Kokkos::View<PetscInt*, Kokkos::LayoutLeft>*>(SData_d->ipf_offset); 391 const PetscInt *d_ipf_offset = d_ipf_offset_k->data(); 392 auto d_elem_offset_k = static_cast<Kokkos::View<PetscInt*, Kokkos::LayoutLeft>*>(SData_d->elem_offset); 393 const PetscInt *d_elem_offset = d_elem_offset_k->data(); 394 #if defined(LANDAU_LAYOUT_LEFT) // preallocate dynamic data, including batched vertices 395 Kokkos::View<PetscReal***, Kokkos::LayoutLeft > d_fdf_k = *static_cast<Kokkos::View<PetscReal***, Kokkos::LayoutLeft >*>(SData_d->ipfdf_data); 396 #else 397 Kokkos::View<PetscReal***, Kokkos::LayoutRight > d_fdf_k = *static_cast<Kokkos::View<PetscReal***, Kokkos::LayoutRight >*>(SData_d->ipfdf_data); 398 #endif 399 auto d_Eq_m_k = static_cast<Kokkos::View<PetscReal*, Kokkos::LayoutLeft>*>(SData_d->Eq_m); // static storage, dynamci data - E(t), copy later, single vertex 400 401 PetscFunctionBegin; 402 ierr = PetscLogEventBegin(events[3],0,0,0,0);CHKERRQ(ierr); 403 ierr = DMGetApplicationContext(plex[0], &ctx);CHKERRQ(ierr); 404 if (!ctx) SETERRQ(PETSC_COMM_SELF, PETSC_ERR_PLIB, "no context"); 405 ierr = DMGetDimension(plex[0], &dim);CHKERRQ(ierr); 406 if (LANDAU_DIM != dim) SETERRQ2(PETSC_COMM_WORLD, PETSC_ERR_PLIB, "dim %D != LANDAU_DIM %d",dim,LANDAU_DIM); 407 if (ctx->gpu_assembly) { 408 ierr = PetscObjectQuery((PetscObject) JacP, "assembly_maps", (PetscObject *) &container);CHKERRQ(ierr); 409 if (container) { // not here first call 410 P4estVertexMaps *h_maps=NULL; 411 ierr = PetscContainerGetPointer(container, (void **) &h_maps);CHKERRQ(ierr); 412 for (PetscInt grid=0 ; grid<num_grids ; grid++) { 413 if (h_maps[grid].d_self) { 414 maps[grid] = h_maps[grid].d_self; 415 nfaces = h_maps[grid].num_face; // nface=0 for CPU assembly 416 } else { 417 SETERRQ(PETSC_COMM_SELF, PETSC_ERR_PLIB, "GPU assembly but no metadata in container"); 418 } 419 } 420 // this does the setup the first time called 421 ierr = MatKokkosGetDeviceMatWrite(JacP,&d_mat);CHKERRQ(ierr); 422 } else { // kernel output - first call assembled on device 423 for (PetscInt grid=0 ; grid<num_grids ; grid++) maps[grid] = NULL; 424 nfaces = 0; 425 d_mat = NULL; 426 } 427 } else { 428 for (PetscInt grid=0 ; grid<num_grids ; grid++) maps[grid] = NULL; 429 nfaces = 0; 430 d_mat = NULL; 431 container = NULL; 432 } 433 num_cells_batch = Nf_max = num_cells_max = 0; 434 for (PetscInt grid=0 ; grid<num_grids ; grid++) { 435 int Nfloc = a_species_offset[grid+1] - a_species_offset[grid]; 436 if (Nfloc > Nf_max) Nf_max = Nfloc; 437 if (a_numCells[grid] > num_cells_max) num_cells_max = a_numCells[grid]; 438 num_cells_batch += a_numCells[grid]; // we don't have a host element offset here (but in ctx) 439 } 440 const PetscInt totDim_max = Nf_max*Nq, elem_mat_size_max = totDim_max*totDim_max; 441 const PetscInt elem_mat_num_cells_max_grid = container ? 0 : num_cells_max; 442 Kokkos::View<PetscScalar****, Kokkos::LayoutRight> d_elem_mats("element matrices", batch_sz, num_grids, elem_mat_num_cells_max_grid, elem_mat_size_max); // first call have large set of global (Jac) element matrices 443 const Kokkos::View<PetscReal*, Kokkos::LayoutLeft, Kokkos::HostSpace, Kokkos::MemoryTraits<Kokkos::Unmanaged> > h_Eq_m_k (a_Eq_m, Nftot); 444 if (a_elem_closure || a_xarray) { 445 Kokkos::deep_copy (*d_Eq_m_k, h_Eq_m_k); 446 d_Eq_m = d_Eq_m_k->data(); 447 } else d_Eq_m = NULL; 448 ierr = PetscKokkosInitializeCheck();CHKERRQ(ierr); 449 ierr = PetscLogEventEnd(events[3],0,0,0,0);CHKERRQ(ierr); 450 if (a_elem_closure || a_xarray) { // Jacobian, create f & df 451 Kokkos::View<PetscScalar*, Kokkos::LayoutLeft> *d_vertex_f_k = NULL; 452 ierr = PetscLogEventBegin(events[1],0,0,0,0);CHKERRQ(ierr); 453 if (a_elem_closure) { 454 PetscInt closure_sz = 0; // argh, don't have this on the host!!! 455 for (PetscInt grid=0 ; grid<num_grids ; grid++) { 456 PetscInt nfloc = a_species_offset[grid+1] - a_species_offset[grid]; 457 closure_sz += Nq*nfloc*a_numCells[grid]; 458 } 459 closure_sz *= batch_sz; 460 d_vertex_f_k = new Kokkos::View<PetscScalar*, Kokkos::LayoutLeft> ("closure",closure_sz); 461 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 462 Kokkos::deep_copy (*d_vertex_f_k, h_closure_k); 463 d_vertex_f = d_vertex_f_k->data(); 464 } else { 465 d_vertex_f = (PetscScalar*)a_xarray; 466 } 467 ierr = PetscLogEventEnd(events[1],0,0,0,0);CHKERRQ(ierr); 468 ierr = PetscLogEventBegin(events[8],0,0,0,0);CHKERRQ(ierr); 469 ierr = PetscLogGpuTimeBegin();CHKERRQ(ierr); 470 471 const int scr_bytes_fdf = real2_scr_t::shmem_size(Nf_max,Nb); 472 ierr = PetscInfo6(plex[0], "Jacobian shared memory size: %d bytes in level %d num cells total=%D team size=%D #face=%D Nf_max=%D\n",scr_bytes_fdf,KOKKOS_SHARED_LEVEL,num_cells_batch*batch_sz,team_size,nfaces,Nf_max);CHKERRQ(ierr); 473 Kokkos::parallel_for("f, df", Kokkos::TeamPolicy<>(num_cells_batch*batch_sz, team_size, /* Kokkos::AUTO */ 16).set_scratch_size(KOKKOS_SHARED_LEVEL, Kokkos::PerTeam(scr_bytes_fdf)), KOKKOS_LAMBDA (const team_member team) { 474 const PetscInt b_Nelem = d_elem_offset[num_grids], b_elem_idx = team.league_rank()%b_Nelem, b_id = team.league_rank()/b_Nelem, IPf_sz_glb = d_ipf_offset[num_grids]; 475 // find my grid 476 PetscInt grid = 0; 477 while (b_elem_idx >= d_elem_offset[grid+1]) grid++; 478 { 479 const PetscInt loc_nip = d_numCells[grid]*Nq, loc_Nf = d_species_offset[grid+1] - d_species_offset[grid], loc_elem = b_elem_idx - d_elem_offset[grid]; 480 const PetscInt moffset = LAND_MOFFSET(b_id,grid,batch_sz,num_grids,d_mat_offset); 481 { 482 real2_scr_t s_coef_k(team.team_scratch(KOKKOS_SHARED_LEVEL),Nf_max,Nb); 483 PetscScalar *coef; 484 const PetscReal *invJe = &d_invJ_k((d_ip_offset[grid] + loc_elem*Nq)*dim*dim); 485 // un pack IPData 486 if (!maps[grid]) { 487 coef = &d_vertex_f[b_id*IPf_sz_glb + d_ipf_offset[grid] + loc_elem*Nb*loc_Nf]; // closure and IP indexing are the same 488 } else { 489 coef = s_coef_k.data(); 490 Kokkos::parallel_for(Kokkos::TeamThreadRange(team,0,(int)loc_Nf), [=] (int f) { 491 //for (int f = 0; f < loc_Nf; ++f) { 492 Kokkos::parallel_for(Kokkos::ThreadVectorRange(team,0,(int)Nb), [=] (int b) { 493 //for (int b = 0; b < Nb; ++b) { 494 LandauIdx idx = maps[grid]->gIdx[loc_elem][f][b]; 495 if (idx >= 0) { 496 coef[f*Nb+b] = d_vertex_f[idx+moffset]; // xarray data, not IP, need raw array to deal with CPU assemble case (not used) 497 } else { 498 idx = -idx - 1; 499 coef[f*Nb+b] = 0; 500 for (int q = 0; q < maps[grid]->num_face; q++) { 501 PetscInt id = maps[grid]->c_maps[idx][q].gid; 502 PetscScalar scale = maps[grid]->c_maps[idx][q].scale; 503 coef[f*Nb+b] += scale*d_vertex_f[id+moffset]; 504 } 505 } 506 }); 507 }); 508 } 509 team.team_barrier(); 510 Kokkos::parallel_for(Kokkos::TeamThreadRange(team,0,Nq), [=] (int myQi) { 511 const PetscReal *const invJ = &invJe[myQi*dim*dim]; // b_elem_idx: batch element index 512 const PetscReal *Bq = &d_BB[myQi*Nb], *Dq = &d_DD[myQi*Nb*dim]; 513 Kokkos::parallel_for(Kokkos::ThreadVectorRange(team,0,(int)loc_Nf), [=] (int f) { 514 PetscInt b, e, d; 515 PetscReal refSpaceDer[LANDAU_DIM]; 516 const PetscInt idx = d_ipf_offset[grid] + f*loc_nip + loc_elem*Nq + myQi; 517 d_fdf_k(b_id,0,idx) = 0.0; 518 for (d = 0; d < LANDAU_DIM; ++d) refSpaceDer[d] = 0.0; 519 for (b = 0; b < Nb; ++b) { 520 d_fdf_k(b_id,0,idx) += Bq[b]*PetscRealPart(coef[f*Nb+b]); 521 for (d = 0; d < dim; ++d) refSpaceDer[d] += Dq[b*dim+d]*PetscRealPart(coef[f*Nb+b]); 522 } 523 for (d = 0; d < dim; ++d) { 524 for (e = 0, d_fdf_k(b_id,d+1,idx) = 0.0; e < dim; ++e) { 525 d_fdf_k(b_id,d+1,idx) += invJ[e*dim+d]*refSpaceDer[e]; 526 } 527 } 528 }); // f 529 }); // myQi 530 } 531 team.team_barrier(); 532 } // 'grid' scope 533 }); // global elems - fdf 534 Kokkos::fence(); 535 ierr = PetscLogGpuTimeEnd();CHKERRQ(ierr); // is this a fence? 536 ierr = PetscLogEventEnd(events[8],0,0,0,0);CHKERRQ(ierr); 537 // Jacobian 538 ierr = PetscLogEventBegin(events[4],0,0,0,0);CHKERRQ(ierr); 539 ierr = PetscLogGpuTimeBegin();CHKERRQ(ierr); 540 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); 541 Kokkos::parallel_for("Jacobian", Kokkos::TeamPolicy<>(num_cells_batch*batch_sz, team_size, /* Kokkos::AUTO */ 16).set_scratch_size(KOKKOS_SHARED_LEVEL, Kokkos::PerTeam(scr_bytes)), KOKKOS_LAMBDA (const team_member team) { 542 const PetscInt b_Nelem = d_elem_offset[num_grids], b_elem_idx = team.league_rank()%b_Nelem, b_id = team.league_rank()/b_Nelem; 543 // find my grid 544 PetscInt grid = 0; 545 while (b_elem_idx >= d_elem_offset[grid+1]) grid++; 546 { 547 const PetscInt loc_Nf = d_species_offset[grid+1]-d_species_offset[grid], loc_elem = b_elem_idx - d_elem_offset[grid]; 548 const PetscInt moffset = LAND_MOFFSET(b_id,grid,batch_sz,num_grids,d_mat_offset); 549 const PetscInt f_off = d_species_offset[grid], totDim = loc_Nf*Nq; 550 g2_scr_t g2(team.team_scratch(KOKKOS_SHARED_LEVEL),dim,loc_Nf,Nq); 551 g3_scr_t g3(team.team_scratch(KOKKOS_SHARED_LEVEL),dim,dim,loc_Nf,Nq); 552 g2_scr_t gg2(team.team_scratch(KOKKOS_SHARED_LEVEL),dim,loc_Nf,Nq); 553 g3_scr_t gg3(team.team_scratch(KOKKOS_SHARED_LEVEL),dim,dim,loc_Nf,Nq); 554 // get g2[] & g3[] 555 Kokkos::parallel_for(Kokkos::TeamThreadRange(team,0,Nq), [=] (int myQi) { 556 using Kokkos::parallel_reduce; 557 const PetscInt jpidx_glb = d_ip_offset[grid] + loc_elem * Nq + myQi; 558 const PetscReal *invJ = &d_invJ_k(jpidx_glb*dim*dim); 559 const PetscReal vj[3] = {d_x[jpidx_glb], d_y[jpidx_glb], d_z ? d_z[jpidx_glb] : 0}, wj = d_w[jpidx_glb]; 560 landau_inner_red::TensorValueType gg_temp; // reduce on part of gg2 and g33 for IP jpidx_g 561 Kokkos::parallel_reduce(Kokkos::ThreadVectorRange (team, (int)d_ip_offset[num_grids]), [=] (const int& ipidx, landau_inner_red::TensorValueType & ggg) { 562 const PetscReal wi = d_w[ipidx], x = d_x[ipidx], y = d_y[ipidx]; 563 PetscReal temp1[3] = {0, 0, 0}, temp2 = 0; 564 PetscInt fieldA,d2,d3,f_off_r,grid_r,ipidx_g,nip_loc_r,loc_Nf_r; 565 #if LANDAU_DIM==2 566 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.; 567 LandauTensor2D(vj, x, y, Ud, Uk, mask); 568 #else 569 PetscReal U[3][3], z = d_z[jpidx_glb], 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.; 570 LandauTensor3D(vj, x, y, z, U, mask); 571 #endif 572 grid_r = 0; 573 while (ipidx >= d_ip_offset[grid_r+1]) grid_r++; // yuck search for grid 574 f_off_r = d_species_offset[grid_r]; 575 ipidx_g = ipidx - d_ip_offset[grid_r]; 576 nip_loc_r = d_numCells[grid_r]*Nq; 577 loc_Nf_r = d_species_offset[grid_r+1] - d_species_offset[grid_r]; 578 for (fieldA = 0; fieldA < loc_Nf_r; ++fieldA) { 579 const PetscInt idx = d_ipf_offset[grid_r] + fieldA*nip_loc_r + ipidx_g; 580 temp1[0] += d_fdf_k(b_id,1,idx)*d_beta[fieldA+f_off_r]*d_invMass[fieldA+f_off_r]; 581 temp1[1] += d_fdf_k(b_id,2,idx)*d_beta[fieldA+f_off_r]*d_invMass[fieldA+f_off_r]; 582 #if LANDAU_DIM==3 583 temp1[2] += d_fdf_k(b_id,3,idx)*d_beta[fieldA+f_off_r]*d_invMass[fieldA+f_off_r]; 584 #endif 585 temp2 += d_fdf_k(b_id,0,idx)*d_beta[fieldA+f_off_r]; 586 } 587 temp1[0] *= wi; 588 temp1[1] *= wi; 589 #if LANDAU_DIM==3 590 temp1[2] *= wi; 591 #endif 592 temp2 *= wi; 593 #if LANDAU_DIM==2 594 for (d2 = 0; d2 < 2; d2++) { 595 for (d3 = 0; d3 < 2; ++d3) { 596 /* K = U * grad(f): g2=e: i,A */ 597 ggg.gg2[d2] += Uk[d2][d3]*temp1[d3]; 598 /* D = -U * (I \kron (fx)): g3=f: i,j,A */ 599 ggg.gg3[d2][d3] += Ud[d2][d3]*temp2; 600 } 601 } 602 #else 603 for (d2 = 0; d2 < 3; ++d2) { 604 for (d3 = 0; d3 < 3; ++d3) { 605 /* K = U * grad(f): g2 = e: i,A */ 606 ggg.gg2[d2] += U[d2][d3]*temp1[d3]; 607 /* D = -U * (I \kron (fx)): g3 = f: i,j,A */ 608 ggg.gg3[d2][d3] += U[d2][d3]*temp2; 609 } 610 } 611 #endif 612 }, Kokkos::Sum<landau_inner_red::TensorValueType>(gg_temp)); 613 // add alpha and put in gg2/3 614 Kokkos::parallel_for(Kokkos::ThreadVectorRange (team, (int)loc_Nf), [&] (const int& fieldA) { 615 PetscInt d2,d3; 616 for (d2 = 0; d2 < dim; d2++) { 617 gg2(d2,fieldA,myQi) = gg_temp.gg2[d2]*d_alpha[fieldA+f_off]; 618 for (d3 = 0; d3 < dim; d3++) { 619 gg3(d2,d3,fieldA,myQi) = -gg_temp.gg3[d2][d3]*d_alpha[fieldA+f_off]*d_invMass[fieldA+f_off]; 620 } 621 } 622 }); 623 /* add electric field term once per IP */ 624 Kokkos::parallel_for(Kokkos::ThreadVectorRange (team, (int)loc_Nf), [&] (const int& fieldA) { 625 gg2(dim-1,fieldA,myQi) += d_Eq_m[fieldA+f_off]; 626 }); 627 Kokkos::parallel_for(Kokkos::ThreadVectorRange (team, (int)loc_Nf), [=] (const int& fieldA) { 628 int d,d2,d3,dp; 629 /* Jacobian transform - g2, g3 - per thread (2D) */ 630 for (d = 0; d < dim; ++d) { 631 g2(d,fieldA,myQi) = 0; 632 for (d2 = 0; d2 < dim; ++d2) { 633 g2(d,fieldA,myQi) += invJ[d*dim+d2]*gg2(d2,fieldA,myQi); 634 g3(d,d2,fieldA,myQi) = 0; 635 for (d3 = 0; d3 < dim; ++d3) { 636 for (dp = 0; dp < dim; ++dp) { 637 g3(d,d2,fieldA,myQi) += invJ[d*dim + d3]*gg3(d3,dp,fieldA,myQi)*invJ[d2*dim + dp]; 638 } 639 } 640 g3(d,d2,fieldA,myQi) *= wj; 641 } 642 g2(d,fieldA,myQi) *= wj; 643 } 644 }); 645 }); // Nq 646 team.team_barrier(); 647 { /* assemble */ 648 fieldMats_scr_t s_fieldMats(team.team_scratch(KOKKOS_SHARED_LEVEL),Nb,Nb); // Only used for GPU assembly (ie, not first pass) 649 idx_scr_t s_idx(team.team_scratch(KOKKOS_SHARED_LEVEL),Nb,nfaces); 650 scale_scr_t s_scale(team.team_scratch(KOKKOS_SHARED_LEVEL),Nb,nfaces); 651 for (PetscInt fieldA = 0; fieldA < loc_Nf; fieldA++) { 652 /* assemble */ 653 Kokkos::parallel_for(Kokkos::TeamThreadRange(team,0,Nb), [=] (int f) { 654 Kokkos::parallel_for(Kokkos::ThreadVectorRange(team,0,Nb), [=] (int g) { 655 PetscScalar t = 0; 656 for (int qj = 0 ; qj < Nq ; qj++) { // look at others integration points 657 const PetscReal *BJq = &d_BB[qj*Nb], *DIq = &d_DD[qj*Nb*dim]; 658 for (int d = 0; d < dim; ++d) { 659 t += DIq[f*dim+d]*g2(d,fieldA,qj)*BJq[g]; 660 for (int d2 = 0; d2 < dim; ++d2) { 661 t += DIq[f*dim + d]*g3(d,d2,fieldA,qj)*DIq[g*dim + d2]; 662 } 663 } 664 } 665 if (elem_mat_num_cells_max_grid) { // CPU assembly 666 const PetscInt fOff = (fieldA*Nb + f)*totDim + fieldA*Nb + g; 667 d_elem_mats(b_id,grid,loc_elem,fOff) = t; 668 } else { 669 s_fieldMats(f,g) = t; 670 } 671 }); 672 }); 673 if (!elem_mat_num_cells_max_grid) { // GPU assembly 674 landau_mat_assemble (d_mat, team, s_fieldMats, s_idx, s_scale, Nb, Nq, nfaces, moffset, loc_elem, fieldA, maps[grid]); 675 } 676 } 677 } 678 } // scope with 'grid' 679 }); 680 ierr = PetscLogGpuTimeEnd();CHKERRQ(ierr); 681 ierr = PetscLogEventEnd(events[4],0,0,0,0);CHKERRQ(ierr); 682 if (d_vertex_f_k) delete d_vertex_f_k; 683 } else { // mass 684 ierr = PetscLogEventBegin(events[4],0,0,0,0);CHKERRQ(ierr); 685 ierr = PetscLogGpuTimeBegin();CHKERRQ(ierr); 686 int scr_bytes = fieldMats_scr_t::shmem_size(Nq,Nq) + idx_scr_t::shmem_size(Nb,nfaces) + scale_scr_t::shmem_size(Nb,nfaces); 687 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); 688 Kokkos::parallel_for("Mass", Kokkos::TeamPolicy<>(num_cells_batch*batch_sz, team_size, /* Kokkos::AUTO */ 16).set_scratch_size(KOKKOS_SHARED_LEVEL, Kokkos::PerTeam(scr_bytes)), KOKKOS_LAMBDA (const team_member team) { 689 const PetscInt b_Nelem = d_elem_offset[num_grids], b_elem_idx = team.league_rank()%b_Nelem, b_id = team.league_rank()/b_Nelem; 690 fieldMats_scr_t s_fieldMats(team.team_scratch(KOKKOS_SHARED_LEVEL),Nb,Nb); 691 idx_scr_t s_idx(team.team_scratch(KOKKOS_SHARED_LEVEL),Nb,nfaces); 692 scale_scr_t s_scale(team.team_scratch(KOKKOS_SHARED_LEVEL),Nb,nfaces); 693 // find my grid 694 PetscInt grid = 0; 695 while (b_elem_idx >= d_elem_offset[grid+1]) grid++; 696 { 697 const PetscInt loc_Nf = d_species_offset[grid+1]-d_species_offset[grid], loc_elem = b_elem_idx - d_elem_offset[grid], totDim = loc_Nf*Nq, jpidx_0 = d_ip_offset[grid] + loc_elem * Nq; 698 const PetscInt moffset = LAND_MOFFSET(b_id,grid,batch_sz,num_grids,d_mat_offset); 699 for (int fieldA = 0; fieldA < loc_Nf; fieldA++) { 700 /* assemble */ 701 Kokkos::parallel_for(Kokkos::TeamThreadRange(team,0,Nb), [=] (int f) { 702 Kokkos::parallel_for(Kokkos::ThreadVectorRange(team,0,Nb), [=] (int g) { 703 PetscScalar t = 0; 704 for (int qj = 0 ; qj < Nq ; qj++) { // look at others integration points 705 const PetscReal *BJq = &d_BB[qj*Nb]; 706 const PetscInt jpidx_glb = jpidx_0 + qj; 707 if (dim==2) { 708 t += BJq[f] * d_w_k(jpidx_glb) * shift * BJq[g] * 2. * PETSC_PI; 709 } else { 710 t += BJq[f] * d_w_k(jpidx_glb) * shift * BJq[g]; 711 } 712 } 713 if (elem_mat_num_cells_max_grid) { 714 const PetscInt fOff = (fieldA*Nb + f)*totDim + fieldA*Nb + g; 715 d_elem_mats(b_id,grid,loc_elem,fOff) = t; 716 } else s_fieldMats(f,g) = t; 717 }); 718 }); 719 if (!elem_mat_num_cells_max_grid) { // device assembly 720 landau_mat_assemble (d_mat, team, s_fieldMats, s_idx, s_scale, Nb, Nq, nfaces, moffset, loc_elem, fieldA, maps[grid]); 721 } // else not using GPU assembly 722 } 723 } 724 }); 725 ierr = PetscLogGpuTimeEnd();CHKERRQ(ierr); 726 ierr = PetscLogEventEnd(events[4],0,0,0,0);CHKERRQ(ierr); 727 } 728 Kokkos::fence(); 729 if (elem_mat_num_cells_max_grid) { // CPU assembly 730 Kokkos::View<PetscScalar****, Kokkos::LayoutRight>::HostMirror h_elem_mats = Kokkos::create_mirror_view(d_elem_mats); 731 Kokkos::deep_copy (h_elem_mats, d_elem_mats); 732 if (container) SETERRQ(PETSC_COMM_SELF, PETSC_ERR_PLIB, "?????"); 733 for (PetscInt b_id = 0 ; b_id < batch_sz ; b_id++) { // OpenMP (once) 734 for (PetscInt grid=0 ; grid<num_grids ; grid++) { 735 PetscSection section, globalSection; 736 PetscInt cStart,cEnd; 737 Mat B = subJ[ LAND_PACK_IDX(b_id,grid) ]; 738 PetscInt moffset = LAND_MOFFSET(b_id,grid,batch_sz,num_grids,a_mat_offset), nloc, nzl, colbuf[1024], row; 739 const PetscInt *cols; 740 const PetscScalar *vals; 741 ierr = PetscLogEventBegin(events[5],0,0,0,0);CHKERRQ(ierr); 742 ierr = DMPlexGetHeightStratum(plex[grid],0,&cStart,&cEnd);CHKERRQ(ierr); 743 ierr = DMGetLocalSection(plex[grid], §ion);CHKERRQ(ierr); 744 ierr = DMGetGlobalSection(plex[grid], &globalSection);CHKERRQ(ierr); 745 ierr = PetscLogEventEnd(events[5],0,0,0,0);CHKERRQ(ierr); 746 ierr = PetscLogEventBegin(events[6],0,0,0,0);CHKERRQ(ierr); 747 for (PetscInt ej = cStart ; ej < cEnd; ++ej) { 748 const PetscScalar *elMat = &h_elem_mats(b_id,grid,ej-cStart,0); 749 ierr = DMPlexMatSetClosure(plex[grid], section, globalSection, B, ej, elMat, ADD_VALUES);CHKERRQ(ierr); 750 if (grid==0 && ej==-1) { 751 const PetscInt loc_Nf = a_species_offset[grid+1]-a_species_offset[grid], totDim = loc_Nf*Nq; 752 int d,f; 753 PetscPrintf(PETSC_COMM_SELF,"Kokkos Element matrix %d/%d\n",1,(int)a_numCells[grid]); 754 for (d = 0; d < totDim; ++d){ 755 for (f = 0; f < totDim; ++f) PetscPrintf(PETSC_COMM_SELF," %12.5e", PetscRealPart(elMat[d*totDim + f])); 756 PetscPrintf(PETSC_COMM_SELF,"\n"); 757 } 758 exit(14); 759 } 760 } 761 // move nest matrix to global JacP 762 ierr = MatAssemblyBegin(B, MAT_FINAL_ASSEMBLY);CHKERRQ(ierr); 763 ierr = MatAssemblyEnd(B, MAT_FINAL_ASSEMBLY);CHKERRQ(ierr); 764 ierr = MatGetSize(B, &nloc, NULL);CHKERRQ(ierr); 765 for (int i=0 ; i<nloc ; i++) { 766 ierr = MatGetRow(B,i,&nzl,&cols,&vals);CHKERRQ(ierr); 767 if (nzl>1024) SETERRQ1(PetscObjectComm((PetscObject) B), PETSC_ERR_PLIB, "Row too big: %D",nzl); 768 for (int j=0; j<nzl; j++) colbuf[j] = cols[j] + moffset; 769 row = i + moffset; 770 ierr = MatSetValues(JacP,1,&row,nzl,colbuf,vals,ADD_VALUES);CHKERRQ(ierr); 771 ierr = MatRestoreRow(B,i,&nzl,&cols,&vals);CHKERRQ(ierr); 772 } 773 ierr = MatDestroy(&subJ[ LAND_PACK_IDX(b_id,grid) ]);CHKERRQ(ierr); 774 ierr = PetscLogEventEnd(events[6],0,0,0,0);CHKERRQ(ierr); 775 } // grids 776 } 777 } 778 PetscFunctionReturn(0); 779 } 780 } // extern "C" 781