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