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