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