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