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) 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->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->gIdx, maps->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->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->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->num_elements; 87 h_maps.num_face = maps->num_face; 88 h_maps.num_reduced = maps->num_reduced; 89 h_maps.deviceType = maps->deviceType; 90 h_maps.Nf = Nf; 91 h_maps.Nq = Nq; 92 h_maps.c_maps = (pointInterpolationP4est (*)[LANDAU_MAX_Q_FACE]) d_points->data(); 93 maps->vp1 = (void*)d_points; 94 h_maps.gIdx = (LandauIdx (*)[LANDAU_MAX_SPECIES][LANDAU_MAX_NQ]) d_gidx->data(); 95 maps->vp2 = (void*)d_gidx; 96 { 97 Kokkos::View<P4estVertexMaps, Kokkos::HostSpace> h_maps_k(&h_maps); 98 Kokkos::View<P4estVertexMaps> *d_maps_k = new Kokkos::View<P4estVertexMaps>(Kokkos::create_mirror(Kokkos::DefaultExecutionSpace::memory_space(),h_maps_k)); 99 Kokkos::deep_copy (*d_maps_k, h_maps_k); 100 maps->data = d_maps_k->data(); 101 maps->vp3 = (void*)d_maps_k; 102 } 103 PetscFunctionReturn(0); 104 } 105 PetscErrorCode LandauKokkosDestroyMatMaps(P4estVertexMaps *maps) 106 { 107 Kokkos::View<pointInterpolationP4est*[LANDAU_MAX_Q_FACE], Kokkos::LayoutRight> *a = (Kokkos::View<pointInterpolationP4est*[LANDAU_MAX_Q_FACE], Kokkos::LayoutRight>*)maps->vp1; 108 Kokkos::View<LandauIdx*[LANDAU_MAX_SPECIES][LANDAU_MAX_NQ], Kokkos::LayoutRight>*b = (Kokkos::View<LandauIdx*[LANDAU_MAX_SPECIES][LANDAU_MAX_NQ], Kokkos::LayoutRight>*)maps->vp2; 109 Kokkos::View<P4estVertexMaps*> *c = (Kokkos::View<P4estVertexMaps*>*)maps->vp3; 110 delete a; delete b; delete c; 111 PetscFunctionReturn(0); 112 } 113 114 PetscErrorCode LandauKokkosStaticDataSet(DM plex, const PetscInt Nq, PetscReal nu_alpha[], PetscReal nu_beta[], PetscReal a_invMass[], PetscReal a_invJ[], PetscReal a_mass_w[], 115 PetscReal a_x[], PetscReal a_y[], PetscReal a_z[], PetscReal a_w[], LandauGeomData *SData_d) 116 { 117 PetscReal *BB,*DD; 118 PetscErrorCode ierr; 119 PetscTabulation *Tf; 120 LandauCtx *ctx; 121 PetscInt *Nbf,dim,Nf,Nb,nip,cStart,cEnd; 122 PetscDS prob; 123 124 PetscFunctionBegin; 125 ierr = DMGetApplicationContext(plex, &ctx);CHKERRQ(ierr); 126 if (!ctx) SETERRQ(PETSC_COMM_SELF, PETSC_ERR_PLIB, "no context"); 127 ierr = DMGetDimension(plex, &dim);CHKERRQ(ierr); 128 ierr = DMPlexGetHeightStratum(plex,0,&cStart,&cEnd);CHKERRQ(ierr); 129 nip = (cEnd - cStart)*Nq; 130 ierr = DMGetDS(plex, &prob);CHKERRQ(ierr); 131 ierr = PetscDSGetNumFields(prob, &Nf);CHKERRQ(ierr); 132 ierr = PetscDSGetDimensions(prob, &Nbf);CHKERRQ(ierr); Nb = Nbf[0]; 133 if (Nq != Nb) SETERRQ2(PETSC_COMM_SELF, PETSC_ERR_PLIB, "Nq != Nb. %D %D",Nq,Nb); 134 if (LANDAU_DIM != dim) SETERRQ2(PETSC_COMM_WORLD, PETSC_ERR_PLIB, "dim %D != LANDAU_DIM %d",dim,LANDAU_DIM); 135 ierr = PetscDSGetTabulation(prob, &Tf);CHKERRQ(ierr); 136 BB = Tf[0]->T[0]; DD = Tf[0]->T[1]; 137 ierr = PetscKokkosInitializeCheck();CHKERRQ(ierr); 138 { 139 const Kokkos::View<PetscReal*, Kokkos::LayoutLeft, Kokkos::HostSpace, Kokkos::MemoryTraits<Kokkos::Unmanaged> > h_alpha (nu_alpha, Nf); 140 auto alpha = new Kokkos::View<PetscReal*, Kokkos::LayoutLeft> ("alpha", Nf); 141 SData_d->alpha = static_cast<void*>(alpha); 142 const Kokkos::View<PetscReal*, Kokkos::LayoutLeft, Kokkos::HostSpace, Kokkos::MemoryTraits<Kokkos::Unmanaged> > h_beta (nu_beta, Nf); 143 auto beta = new Kokkos::View<PetscReal*, Kokkos::LayoutLeft> ("beta", Nf); 144 SData_d->beta = static_cast<void*>(beta); 145 const Kokkos::View<PetscReal*, Kokkos::LayoutLeft, Kokkos::HostSpace, Kokkos::MemoryTraits<Kokkos::Unmanaged> > h_invMass (a_invMass,Nf); 146 auto invMass = new Kokkos::View<PetscReal*, Kokkos::LayoutLeft> ("invMass", Nf); 147 SData_d->invMass = static_cast<void*>(invMass); 148 const Kokkos::View<PetscReal*, Kokkos::LayoutLeft, Kokkos::HostSpace, Kokkos::MemoryTraits<Kokkos::Unmanaged> > h_BB (BB,Nq*Nb); 149 auto B = new Kokkos::View<PetscReal*, Kokkos::LayoutLeft> ("B", Nq*Nb); 150 SData_d->B = static_cast<void*>(B); 151 const Kokkos::View<PetscReal*, Kokkos::LayoutLeft, Kokkos::HostSpace, Kokkos::MemoryTraits<Kokkos::Unmanaged> > h_DD (DD,Nq*Nb*dim); 152 auto D = new Kokkos::View<PetscReal*, Kokkos::LayoutLeft> ("D", Nq*Nb*dim); 153 SData_d->D = static_cast<void*>(D); 154 const Kokkos::View<PetscReal*, Kokkos::LayoutLeft, Kokkos::HostSpace, Kokkos::MemoryTraits<Kokkos::Unmanaged> > h_mass_w (a_mass_w, nip); 155 auto mass_w = new Kokkos::View<PetscReal*, Kokkos::LayoutLeft> ("mass_w", nip); 156 SData_d->mass_w = static_cast<void*>(mass_w); 157 const Kokkos::View<PetscReal*, Kokkos::LayoutLeft, Kokkos::HostSpace, Kokkos::MemoryTraits<Kokkos::Unmanaged> > h_invJ (a_invJ, nip*dim*dim); 158 auto invJ = new Kokkos::View<PetscReal*, Kokkos::LayoutLeft> ("invJ", nip*dim*dim); 159 SData_d->invJ = static_cast<void*>(invJ); 160 const Kokkos::View<PetscReal*, Kokkos::LayoutLeft, Kokkos::HostSpace, Kokkos::MemoryTraits<Kokkos::Unmanaged> > h_x (a_x, nip); 161 auto x = new Kokkos::View<PetscReal*, Kokkos::LayoutLeft> ("x", nip); 162 SData_d->x = static_cast<void*>(x); 163 const Kokkos::View<PetscReal*, Kokkos::LayoutLeft, Kokkos::HostSpace, Kokkos::MemoryTraits<Kokkos::Unmanaged> > h_y (a_y, nip); 164 auto y = new Kokkos::View<PetscReal*, Kokkos::LayoutLeft> ("y", nip); 165 SData_d->y = static_cast<void*>(y); 166 const Kokkos::View<PetscReal*, Kokkos::LayoutLeft, Kokkos::HostSpace, Kokkos::MemoryTraits<Kokkos::Unmanaged> > h_w (a_w, nip); 167 auto w = new Kokkos::View<PetscReal*, Kokkos::LayoutLeft> ("w", nip); 168 SData_d->w = static_cast<void*>(w); 169 const Kokkos::View<PetscReal*, Kokkos::LayoutLeft, Kokkos::HostSpace, Kokkos::MemoryTraits<Kokkos::Unmanaged> > h_z (a_z , dim==3 ? nip : 0); 170 auto z = new Kokkos::View<PetscReal*, Kokkos::LayoutLeft> ("z", dim==3 ? nip : 0); 171 SData_d->z = static_cast<void*>(z); 172 173 Kokkos::deep_copy (*mass_w, h_mass_w); 174 Kokkos::deep_copy (*alpha, h_alpha); 175 Kokkos::deep_copy (*beta, h_beta); 176 Kokkos::deep_copy (*invMass, h_invMass); 177 Kokkos::deep_copy (*B, h_BB); 178 Kokkos::deep_copy (*D, h_DD); 179 Kokkos::deep_copy (*invJ, h_invJ); 180 Kokkos::deep_copy (*x, h_x); 181 Kokkos::deep_copy (*y, h_y); 182 Kokkos::deep_copy (*z, h_z); 183 Kokkos::deep_copy (*w, h_w); 184 185 auto Eq_m = new Kokkos::View<PetscReal*, Kokkos::LayoutLeft> ("Eq_m",Nf); 186 SData_d->Eq_m = static_cast<void*>(Eq_m); 187 auto IPf = new Kokkos::View<PetscScalar*, Kokkos::LayoutLeft> ("IPf",nip*Nf); // Nq==Nb 188 SData_d->IPf = static_cast<void*>(IPf); 189 } 190 PetscFunctionReturn(0); 191 } 192 193 PetscErrorCode LandauKokkosStaticDataClear(LandauGeomData *SData_d) 194 { 195 PetscFunctionBegin; 196 if (SData_d->alpha) { 197 auto alpha = static_cast<Kokkos::View<PetscReal*, Kokkos::LayoutLeft>*>(SData_d->alpha); 198 delete alpha; 199 auto beta = static_cast<Kokkos::View<PetscReal*, Kokkos::LayoutLeft>*>(SData_d->beta); 200 delete beta; 201 auto invMass = static_cast<Kokkos::View<PetscReal*, Kokkos::LayoutLeft>*>(SData_d->invMass); 202 delete invMass; 203 auto B = static_cast<Kokkos::View<PetscReal*, Kokkos::LayoutLeft>*>(SData_d->B); 204 delete B; 205 auto D = static_cast<Kokkos::View<PetscReal*, Kokkos::LayoutLeft>*>(SData_d->D); 206 delete D; 207 auto mass_w = static_cast<Kokkos::View<PetscReal*, Kokkos::LayoutLeft>*>(SData_d->mass_w); 208 delete mass_w; 209 auto invJ = static_cast<Kokkos::View<PetscReal*, Kokkos::LayoutLeft>*>(SData_d->invJ); 210 delete invJ; 211 auto x = static_cast<Kokkos::View<PetscReal*, Kokkos::LayoutLeft>*>(SData_d->x); 212 delete x; 213 auto y = static_cast<Kokkos::View<PetscReal*, Kokkos::LayoutLeft>*>(SData_d->y); 214 delete y; 215 auto z = static_cast<Kokkos::View<PetscReal*, Kokkos::LayoutLeft>*>(SData_d->z); 216 delete z; 217 auto w = static_cast<Kokkos::View<PetscReal*, Kokkos::LayoutLeft>*>(SData_d->w); 218 delete w; 219 auto Eq_m = static_cast<Kokkos::View<PetscReal*, Kokkos::LayoutLeft>*>(SData_d->Eq_m); 220 delete Eq_m; 221 if (SData_d->IPf) { 222 auto IPf = static_cast<Kokkos::View<PetscScalar*, Kokkos::LayoutLeft>*>(SData_d->IPf); 223 delete IPf; 224 } 225 } 226 PetscFunctionReturn(0); 227 } 228 229 PetscErrorCode LandauKokkosJacobian(DM plex, const PetscInt Nq, PetscReal a_Eq_m[], PetscScalar a_IPf[], const PetscInt N, const PetscScalar a_xarray[], LandauGeomData *SData_d, 230 const PetscInt num_sub_blocks, PetscReal shift, const PetscLogEvent events[], Mat JacP) 231 { 232 using scr_mem_t = Kokkos::DefaultExecutionSpace::scratch_memory_space; 233 using g2_scr_t = Kokkos::View<PetscReal***, Kokkos::LayoutRight, scr_mem_t>; 234 using g3_scr_t = Kokkos::View<PetscReal****, Kokkos::LayoutRight, scr_mem_t>; 235 PetscErrorCode ierr; 236 PetscInt *Nbf,Nb,cStart,cEnd,Nf,dim,numCells,totDim,global_elem_mat_sz,nip; 237 PetscDS prob; 238 LandauCtx *ctx; 239 PetscReal *d_Eq_m=NULL; 240 PetscScalar *d_IPf=NULL; 241 P4estVertexMaps *d_maps=NULL; 242 PetscSplitCSRDataStructure d_mat=NULL; 243 const int conc = Kokkos::DefaultExecutionSpace().concurrency(), openmp = !!(conc < 1000), team_size = (openmp==0) ? Nq : 1; 244 int scr_bytes; 245 auto d_alpha_k = static_cast<Kokkos::View<PetscReal*, Kokkos::LayoutLeft>*>(SData_d->alpha); //static data 246 const PetscReal *d_alpha = d_alpha_k->data(); 247 auto d_beta_k = static_cast<Kokkos::View<PetscReal*, Kokkos::LayoutLeft>*>(SData_d->beta); 248 const PetscReal *d_beta = d_beta_k->data(); 249 auto d_invMass_k = static_cast<Kokkos::View<PetscReal*, Kokkos::LayoutLeft>*>(SData_d->invMass); 250 const PetscReal *d_invMass = d_invMass_k->data(); 251 auto d_B_k = static_cast<Kokkos::View<PetscReal*, Kokkos::LayoutLeft>*>(SData_d->B); 252 const PetscReal *d_BB = d_B_k->data(); 253 auto d_D_k = static_cast<Kokkos::View<PetscReal*, Kokkos::LayoutLeft>*>(SData_d->D); 254 const PetscReal *d_DD = d_D_k->data(); 255 auto d_mass_w_k = *static_cast<Kokkos::View<PetscReal*, Kokkos::LayoutLeft>*>(SData_d->mass_w); 256 auto d_invJ_k = *static_cast<Kokkos::View<PetscReal*, Kokkos::LayoutLeft>*>(SData_d->invJ); // use Kokkos vector in kernels 257 auto d_x_k = static_cast<Kokkos::View<PetscReal*, Kokkos::LayoutLeft>*>(SData_d->x); //static data 258 const PetscReal *d_x = d_x_k->data(); 259 auto d_y_k = static_cast<Kokkos::View<PetscReal*, Kokkos::LayoutLeft>*>(SData_d->y); //static data 260 const PetscReal *d_y = d_y_k->data(); 261 auto d_z_k = static_cast<Kokkos::View<PetscReal*, Kokkos::LayoutLeft>*>(SData_d->z); //static data 262 const PetscReal *d_z = d_z_k->data(); 263 auto d_w_k = static_cast<Kokkos::View<PetscReal*, Kokkos::LayoutLeft>*>(SData_d->w); //static data 264 const PetscReal *d_w = d_w_k->data(); 265 // dynamic data pointers 266 auto d_Eq_m_k = static_cast<Kokkos::View<PetscReal*, Kokkos::LayoutLeft>*>(SData_d->Eq_m); //static data 267 auto d_IPf_k = static_cast<Kokkos::View<PetscScalar*, Kokkos::LayoutLeft>*>(SData_d->IPf); //static data 268 269 PetscFunctionBegin; 270 ierr = PetscLogEventBegin(events[3],0,0,0,0);CHKERRQ(ierr); 271 ierr = DMGetApplicationContext(plex, &ctx);CHKERRQ(ierr); 272 if (!ctx) SETERRQ(PETSC_COMM_SELF, PETSC_ERR_PLIB, "no context"); 273 ierr = DMGetDimension(plex, &dim);CHKERRQ(ierr); 274 ierr = DMPlexGetHeightStratum(plex,0,&cStart,&cEnd);CHKERRQ(ierr); 275 numCells = cEnd - cStart; 276 nip = numCells*Nq; 277 ierr = DMGetDS(plex, &prob);CHKERRQ(ierr); 278 ierr = PetscDSGetNumFields(prob, &Nf);CHKERRQ(ierr); 279 ierr = PetscDSGetDimensions(prob, &Nbf);CHKERRQ(ierr); Nb = Nbf[0]; 280 if (Nq != Nb) SETERRQ2(PETSC_COMM_SELF, PETSC_ERR_PLIB, "Nq != Nb. %D %D",Nq,Nb); 281 if (LANDAU_DIM != dim) SETERRQ2(PETSC_COMM_WORLD, PETSC_ERR_PLIB, "dim %D != LANDAU_DIM %d",dim,LANDAU_DIM); 282 scr_bytes = 2*(g2_scr_t::shmem_size(dim,Nf,Nq) + g3_scr_t::shmem_size(dim,dim,Nf,Nq)); 283 ierr = PetscDSGetTotalDimension(prob, &totDim);CHKERRQ(ierr); 284 if (ctx->gpu_assembly) { 285 PetscContainer container; 286 ierr = PetscObjectQuery((PetscObject) JacP, "assembly_maps", (PetscObject *) &container);CHKERRQ(ierr); 287 if (container) { // not here first call 288 #if defined(PETSC_HAVE_KOKKOS_KERNELS) 289 P4estVertexMaps *h_maps=NULL; 290 ierr = PetscContainerGetPointer(container, (void **) &h_maps);CHKERRQ(ierr); 291 if (h_maps->data) { 292 d_maps = h_maps->data; 293 } else { 294 SETERRQ(PETSC_COMM_SELF, PETSC_ERR_PLIB, "GPU assembly but no metadata in container"); 295 } 296 // this does the setup the first time called 297 ierr = MatKokkosGetDeviceMatWrite(JacP,&d_mat);CHKERRQ(ierr); 298 global_elem_mat_sz = 0; 299 #else 300 SETERRQ(PETSC_COMM_SELF, PETSC_ERR_PLIB, "GPU assembly w/o kokkos kernels -- should not be here"); 301 #endif 302 } else { // kernel output - first call assembled on device 303 global_elem_mat_sz = numCells; 304 } 305 } else { 306 global_elem_mat_sz = numCells; // no device assembly 307 } 308 ierr = PetscKokkosInitializeCheck();CHKERRQ(ierr); 309 ierr = PetscLogEventEnd(events[3],0,0,0,0);CHKERRQ(ierr); 310 { 311 #if defined(LANDAU_LAYOUT_LEFT) 312 Kokkos::View<PetscReal***, Kokkos::LayoutLeft > d_fdf_k("df", dim+1, Nf, (a_IPf || a_xarray) ? nip : 0); 313 #else 314 Kokkos::View<PetscReal***, Kokkos::LayoutRight > d_fdf_k("df", dim+1, Nf, (a_IPf || a_xarray) ? nip : 0); 315 #endif 316 const Kokkos::View<PetscScalar*, Kokkos::LayoutLeft, Kokkos::HostSpace, Kokkos::MemoryTraits<Kokkos::Unmanaged> >h_IPf_k (a_IPf, (a_IPf || a_xarray) ? nip*Nf : 0); 317 const Kokkos::View<PetscReal*, Kokkos::LayoutLeft, Kokkos::HostSpace, Kokkos::MemoryTraits<Kokkos::Unmanaged> > h_Eq_m_k (a_Eq_m, (a_IPf || a_xarray) ? Nf : 0); 318 Kokkos::View<PetscScalar**, Kokkos::LayoutRight> d_elem_mats("element matrices", global_elem_mat_sz, totDim*totDim); 319 // copy dynamic data to device 320 if (a_IPf || a_xarray) { // form f and df 321 static int cc=0; 322 ierr = PetscLogEventBegin(events[1],0,0,0,0);CHKERRQ(ierr); 323 Kokkos::deep_copy (*d_Eq_m_k, h_Eq_m_k); 324 d_Eq_m = d_Eq_m_k->data(); 325 if (a_IPf) { 326 Kokkos::deep_copy (*d_IPf_k, h_IPf_k); 327 d_IPf = d_IPf_k->data(); 328 } else { 329 d_IPf = (PetscScalar*)a_xarray; 330 } 331 ierr = PetscLogEventEnd(events[1],0,0,0,0);CHKERRQ(ierr); 332 #define KOKKOS_SHARED_LEVEL 1 333 if (cc++ == 0) { 334 ierr = PetscInfo4(plex, "shared memory size: %d bytes in level %d conc=%D team size=%D\n",scr_bytes,KOKKOS_SHARED_LEVEL,conc,team_size);CHKERRQ(ierr); 335 } 336 // get f and df 337 ierr = PetscLogEventBegin(events[8],0,0,0,0);CHKERRQ(ierr); 338 ierr = PetscLogGpuTimeBegin();CHKERRQ(ierr); 339 Kokkos::parallel_for("f, df", Kokkos::TeamPolicy<>(numCells, team_size, /* Kokkos::AUTO */ 16), KOKKOS_LAMBDA (const team_member team) { 340 const PetscInt elem = team.league_rank(); 341 const PetscScalar *coef; 342 PetscScalar coef_buff[LANDAU_MAX_SPECIES*LANDAU_MAX_NQ]; 343 // un pack IPData 344 if (!d_maps) { 345 coef = &d_IPf[elem*Nb*Nf]; 346 } else { 347 coef = coef_buff; 348 for (int f = 0; f < Nf; ++f) { 349 LandauIdx *const Idxs = &d_maps->gIdx[elem][f][0]; 350 for (int b = 0; b < Nb; ++b) { 351 PetscInt idx = Idxs[b]; 352 if (idx >= 0) { 353 coef_buff[f*Nb+b] = d_IPf[idx]; 354 } else { 355 idx = -idx - 1; 356 coef_buff[f*Nb+b] = 0; 357 for (int q = 0; q < d_maps->num_face; q++) { 358 PetscInt id = d_maps->c_maps[idx][q].gid; 359 PetscScalar scale = d_maps->c_maps[idx][q].scale; 360 coef_buff[f*Nb+b] += scale*d_IPf[id]; 361 } 362 } 363 } 364 } 365 } 366 Kokkos::parallel_for(Kokkos::TeamThreadRange(team,0,Nq), [=] (int myQi) { 367 const PetscInt ipidx = myQi + elem * Nq; 368 const PetscReal *const invJj = &d_invJ_k(ipidx*dim*dim); 369 const PetscReal *Bq = &d_BB[myQi*Nb], *Dq = &d_DD[myQi*Nb*dim]; 370 Kokkos::parallel_for(Kokkos::ThreadVectorRange(team,0,(int)Nf), [=] (int f) { 371 PetscInt b, e, d; 372 PetscReal refSpaceDer[LANDAU_DIM]; 373 d_fdf_k(0,f,ipidx) = 0.0; 374 for (d = 0; d < LANDAU_DIM; ++d) refSpaceDer[d] = 0.0; 375 for (b = 0; b < Nb; ++b) { 376 const PetscInt cidx = b; 377 d_fdf_k(0,f,ipidx) += Bq[cidx]*PetscRealPart(coef[f*Nb+cidx]); 378 for (d = 0; d < dim; ++d) refSpaceDer[d] += Dq[cidx*dim+d]*PetscRealPart(coef[f*Nb+cidx]); 379 } 380 for (d = 0; d < dim; ++d) { 381 for (e = 0, d_fdf_k(d+1,f,ipidx) = 0.0; e < dim; ++e) { 382 d_fdf_k(d+1,f,ipidx) += invJj[e*dim+d]*refSpaceDer[e]; 383 } 384 } 385 }); // Nf 386 }); // Nq 387 }); // elems 388 Kokkos::fence(); 389 #if defined(PETSC_HAVE_CUDA) || defined(PETSC_HAVE_HIP) 390 ierr = PetscLogGpuFlops(nip*(PetscLogDouble)(2*Nb*(1+dim)));CHKERRQ(ierr); 391 #else 392 ierr = PetscLogFlops(nip*(PetscLogDouble)(2*Nb*(1+dim)));CHKERRQ(ierr); 393 #endif 394 ierr = PetscLogGpuTimeEnd();CHKERRQ(ierr); 395 ierr = PetscLogEventEnd(events[8],0,0,0,0);CHKERRQ(ierr); 396 } 397 ierr = PetscLogEventBegin(events[4],0,0,0,0);CHKERRQ(ierr); 398 ierr = PetscLogGpuTimeBegin();CHKERRQ(ierr); 399 #if defined(PETSC_HAVE_CUDA) || defined(PETSC_HAVE_HIP) 400 ierr = PetscLogGpuFlops(nip*(PetscLogDouble)(!(a_IPf || a_xarray) ? (nip*(11*Nf+ 4*dim*dim) + 6*Nf*dim*dim*dim + 10*Nf*dim*dim + 4*Nf*dim + Nb*Nf*Nb*Nq*dim*dim*5) : Nb*Nf*Nb*Nq*4));CHKERRQ(ierr); 401 if (ctx->deviceType == LANDAU_CPU) PetscInfo(plex, "Warning: Landau selected CPU but no support for Kokkos using CPU\n"); 402 #else 403 ierr = PetscLogFlops(nip*(PetscLogDouble)(!(a_IPf || a_xarray) ? (nip*(11*Nf+ 4*dim*dim) + 6*Nf*dim*dim*dim + 10*Nf*dim*dim + 4*Nf*dim + Nb*Nf*Nb*Nq*dim*dim*5) : Nb*Nf*Nb*Nq*4));CHKERRQ(ierr); 404 #endif 405 Kokkos::parallel_for("Landau elements", Kokkos::TeamPolicy<>(numCells, team_size, /*Kokkos::AUTO*/ 16).set_scratch_size(KOKKOS_SHARED_LEVEL, Kokkos::PerTeam(scr_bytes)), KOKKOS_LAMBDA (const team_member team) { 406 const PetscInt elem = team.league_rank(); 407 g2_scr_t g2(team.team_scratch(KOKKOS_SHARED_LEVEL),dim,Nf,Nq); // we don't use these for mass matrix 408 g3_scr_t g3(team.team_scratch(KOKKOS_SHARED_LEVEL),dim,dim,Nf,Nq); 409 if (d_IPf) { 410 g2_scr_t gg2(team.team_scratch(KOKKOS_SHARED_LEVEL),dim,Nf,Nq); 411 g3_scr_t gg3(team.team_scratch(KOKKOS_SHARED_LEVEL),dim,dim,Nf,Nq); 412 // get g2[] & g3[] 413 Kokkos::parallel_for(Kokkos::TeamThreadRange(team,0,Nq), [=] (int myQi) { 414 using Kokkos::parallel_reduce; 415 const PetscInt jpidx = myQi + elem * Nq; 416 const PetscReal* const invJj = &d_invJ_k(jpidx*dim*dim); 417 const PetscReal vj[3] = {d_x[jpidx], d_y[jpidx], d_z ? d_z[jpidx] : 0}, wj = d_w[jpidx]; 418 landau_inner_red::TensorValueType gg_temp; // reduce on part of gg2 and g33 for IP jpidx 419 Kokkos::parallel_reduce(Kokkos::ThreadVectorRange (team, (int)nip), [=] (const int& ipidx, landau_inner_red::TensorValueType & ggg) { 420 const PetscReal wi = d_w[ipidx], x = d_x[ipidx], y = d_y[ipidx]; 421 PetscReal temp1[3] = {0, 0, 0}, temp2 = 0; 422 PetscInt fieldA,d2,d3; 423 #if LANDAU_DIM==2 424 PetscReal Ud[2][2], Uk[2][2]; 425 LandauTensor2D(vj, x, y, Ud, Uk, (ipidx==jpidx) ? 0. : 1.); 426 #else 427 PetscReal U[3][3], z = d_z[ipidx]; 428 LandauTensor3D(vj, x, y, z, U, (ipidx==jpidx) ? 0. : 1.); 429 #endif 430 for (fieldA = 0; fieldA < Nf; ++fieldA) { 431 temp1[0] += d_fdf_k(1,fieldA,ipidx)*d_beta[fieldA]*d_invMass[fieldA]; 432 temp1[1] += d_fdf_k(2,fieldA,ipidx)*d_beta[fieldA]*d_invMass[fieldA]; 433 #if LANDAU_DIM==3 434 temp1[2] += d_fdf_k(3,fieldA,ipidx)*d_beta[fieldA]*d_invMass[fieldA]; 435 #endif 436 temp2 += d_fdf_k(0,fieldA,ipidx)*d_beta[fieldA]; 437 } 438 temp1[0] *= wi; 439 temp1[1] *= wi; 440 #if LANDAU_DIM==3 441 temp1[2] *= wi; 442 #endif 443 temp2 *= wi; 444 #if LANDAU_DIM==2 445 for (d2 = 0; d2 < 2; d2++) { 446 for (d3 = 0; d3 < 2; ++d3) { 447 /* K = U * grad(f): g2=e: i,A */ 448 ggg.gg2[d2] += Uk[d2][d3]*temp1[d3]; 449 /* D = -U * (I \kron (fx)): g3=f: i,j,A */ 450 ggg.gg3[d2][d3] += Ud[d2][d3]*temp2; 451 } 452 } 453 #else 454 for (d2 = 0; d2 < 3; ++d2) { 455 for (d3 = 0; d3 < 3; ++d3) { 456 /* K = U * grad(f): g2 = e: i,A */ 457 ggg.gg2[d2] += U[d2][d3]*temp1[d3]; 458 /* D = -U * (I \kron (fx)): g3 = f: i,j,A */ 459 ggg.gg3[d2][d3] += U[d2][d3]*temp2; 460 } 461 } 462 #endif 463 }, Kokkos::Sum<landau_inner_red::TensorValueType>(gg_temp)); 464 // add alpha and put in gg2/3 465 Kokkos::parallel_for(Kokkos::ThreadVectorRange (team, (int)Nf), [&] (const int& fieldA) { 466 PetscInt d2,d3; 467 for (d2 = 0; d2 < dim; d2++) { 468 gg2(d2,fieldA,myQi) = gg_temp.gg2[d2]*d_alpha[fieldA]; 469 for (d3 = 0; d3 < dim; d3++) { 470 gg3(d2,d3,fieldA,myQi) = -gg_temp.gg3[d2][d3]*d_alpha[fieldA]*d_invMass[fieldA]; 471 } 472 } 473 }); 474 /* add electric field term once per IP */ 475 Kokkos::parallel_for(Kokkos::ThreadVectorRange (team, (int)Nf), [&] (const int& fieldA) { 476 gg2(dim-1,fieldA,myQi) += d_Eq_m[fieldA]; 477 }); 478 Kokkos::parallel_for(Kokkos::ThreadVectorRange (team, (int)Nf), [=] (const int& fieldA) { 479 int d,d2,d3,dp; 480 /* Jacobian transform - g2, g3 - per thread (2D) */ 481 for (d = 0; d < dim; ++d) { 482 g2(d,fieldA,myQi) = 0; 483 for (d2 = 0; d2 < dim; ++d2) { 484 g2(d,fieldA,myQi) += invJj[d*dim+d2]*gg2(d2,fieldA,myQi); 485 g3(d,d2,fieldA,myQi) = 0; 486 for (d3 = 0; d3 < dim; ++d3) { 487 for (dp = 0; dp < dim; ++dp) { 488 g3(d,d2,fieldA,myQi) += invJj[d*dim + d3]*gg3(d3,dp,fieldA,myQi)*invJj[d2*dim + dp]; 489 } 490 } 491 g3(d,d2,fieldA,myQi) *= wj; 492 } 493 g2(d,fieldA,myQi) *= wj; 494 } 495 }); 496 }); // Nq 497 team.team_barrier(); 498 } // Jacobian 499 /* assemble */ 500 Kokkos::parallel_for(Kokkos::TeamThreadRange(team,0,Nb), [=] (int blk_i) { 501 Kokkos::parallel_for(Kokkos::ThreadVectorRange(team,0,(int)Nf), [=] (int fieldA) { 502 int blk_j,qj,d,d2; 503 const PetscInt i = fieldA*Nb + blk_i; /* Element matrix row */ 504 for (blk_j = 0; blk_j < Nb; ++blk_j) { 505 const PetscInt j = fieldA*Nb + blk_j; /* Element matrix column */ 506 const PetscInt fOff = i*totDim + j; 507 PetscScalar t = global_elem_mat_sz ? d_elem_mats(elem,fOff) : 0; 508 for (qj = 0 ; qj < Nq ; qj++) { // look at others integration points 509 const PetscReal *BJq = &d_BB[qj*Nb], *DIq = &d_DD[qj*Nb*dim]; 510 if (d_IPf) { 511 for (d = 0; d < dim; ++d) { 512 t += DIq[blk_i*dim+d]*g2(d,fieldA,qj)*BJq[blk_j]; 513 for (d2 = 0; d2 < dim; ++d2) { 514 t += DIq[blk_i*dim + d]*g3(d,d2,fieldA,qj)*DIq[blk_j*dim + d2]; 515 } 516 } 517 } else { 518 const PetscInt jpidx = qj + elem * Nq; 519 t += BJq[blk_i] * d_mass_w_k(jpidx) * shift * BJq[blk_j]; 520 } 521 } 522 if (global_elem_mat_sz) d_elem_mats(elem,fOff) = t; // can set this because local element matrix[fOff] 523 else { 524 PetscScalar vals[LANDAU_MAX_Q_FACE*LANDAU_MAX_Q_FACE],row_scale[LANDAU_MAX_Q_FACE],col_scale[LANDAU_MAX_Q_FACE]; 525 PetscInt q,idx,nr,nc,rows0[LANDAU_MAX_Q_FACE],cols0[LANDAU_MAX_Q_FACE],rows[LANDAU_MAX_Q_FACE],cols[LANDAU_MAX_Q_FACE]; 526 const LandauIdx *const Idxs = &d_maps->gIdx[elem][fieldA][0]; 527 idx = Idxs[blk_i]; 528 if (idx >= 0) { 529 nr = 1; 530 rows0[0] = idx; 531 row_scale[0] = 1.; 532 } else { 533 idx = -idx - 1; 534 nr = d_maps->num_face; 535 for (q = 0; q < d_maps->num_face; q++) { 536 rows0[q] = d_maps->c_maps[idx][q].gid; 537 row_scale[q] = d_maps->c_maps[idx][q].scale; 538 } 539 } 540 idx = Idxs[blk_j]; 541 if (idx >= 0) { 542 nc = 1; 543 cols0[0] = idx; 544 col_scale[0] = 1.; 545 } else { 546 idx = -idx - 1; 547 nc = d_maps->num_face; 548 for (q = 0; q < d_maps->num_face; q++) { 549 cols0[q] = d_maps->c_maps[idx][q].gid; 550 col_scale[q] = d_maps->c_maps[idx][q].scale; 551 } 552 } 553 for (q = 0; q < nr; q++) rows[q] = rows0[q]; 554 for (q = 0; q < nc; q++) cols[q] = cols0[q]; 555 for (q = 0; q < nr; q++) { 556 for (d = 0; d < nc; d++) { 557 vals[q*nc + d] = row_scale[q]*col_scale[d]*t; 558 } 559 } 560 MatSetValuesDevice(d_mat,nr,rows,nc,cols,vals,ADD_VALUES); 561 } 562 } 563 }); 564 }); 565 }); 566 ierr = PetscLogGpuTimeEnd();CHKERRQ(ierr); 567 Kokkos::fence(); 568 ierr = PetscLogEventEnd(events[4],0,0,0,0);CHKERRQ(ierr); 569 if (global_elem_mat_sz) { 570 PetscSection section, globalSection; 571 Kokkos::View<PetscScalar**, Kokkos::LayoutRight>::HostMirror h_elem_mats = Kokkos::create_mirror_view(d_elem_mats); 572 ierr = PetscLogEventBegin(events[5],0,0,0,0);CHKERRQ(ierr); 573 ierr = DMGetLocalSection(plex, §ion);CHKERRQ(ierr); 574 ierr = DMGetGlobalSection(plex, &globalSection);CHKERRQ(ierr); 575 Kokkos::deep_copy (h_elem_mats, d_elem_mats); 576 ierr = PetscLogEventEnd(events[5],0,0,0,0);CHKERRQ(ierr); 577 ierr = PetscLogEventBegin(events[6],0,0,0,0);CHKERRQ(ierr); 578 PetscInt ej; 579 for (ej = cStart ; ej < cEnd; ++ej) { 580 const PetscScalar *elMat = &h_elem_mats(ej-cStart,0); 581 ierr = DMPlexMatSetClosure(plex, section, globalSection, JacP, ej, elMat, ADD_VALUES);CHKERRQ(ierr); 582 if (ej==-1) { 583 int d,f; 584 PetscPrintf(PETSC_COMM_SELF,"Kokkos Element matrix %d/%d\n",1,(int)numCells); 585 for (d = 0; d < totDim; ++d) { 586 for (f = 0; f < totDim; ++f) PetscPrintf(PETSC_COMM_SELF," %12.5e", PetscRealPart(elMat[d*totDim + f])); 587 PetscPrintf(PETSC_COMM_SELF,"\n"); 588 } 589 exit(14); 590 } 591 } 592 ierr = PetscLogEventEnd(events[6],0,0,0,0);CHKERRQ(ierr); 593 // transition to use of maps for VecGetClosure 594 if (ctx->gpu_assembly) { 595 auto IPf = static_cast<Kokkos::View<PetscScalar*, Kokkos::LayoutLeft>*>(SData_d->IPf); 596 delete IPf; 597 SData_d->IPf = NULL; 598 if (!(a_IPf || a_xarray)) SETERRQ(PETSC_COMM_SELF, PETSC_ERR_PLIB, "transition without Jacobian"); 599 } 600 } 601 } 602 PetscFunctionReturn(0); 603 } 604 } // extern "C" 605