peclet-dem 1.1.0
Performance-portable XPBD Discrete Element Method (Kokkos + ArborX)
Loading...
Searching...
No Matches
solve_copies.hpp
Go to the documentation of this file.
1
7#ifndef DEM_SOLVE_COPIES_HPP
8#define DEM_SOLVE_COPIES_HPP
9
10#include <cstddef>
11#include <cstdint>
12#include <Kokkos_Core.hpp>
13#include <Kokkos_Sort.hpp>
14#include <string>
15
16#include "contact_preprocessing.hpp" // ManifoldC, ContactC, PosUnits, SlotOverride
17#include "particles.hpp" // Particles, PhaseCopies
18
19namespace peclet::dem {
20
21// ================================ contact-solve copies ================================
22// docs/contact_solve_framework.md §4.4 (hub copies), §4.5 (solve views, relaxation) and §WO-4
23// item 2 (demStep's periodic images in the position phase). A body updated through more than one
24// slot in a phase is a fold GROUP: its slots (the base, hub copies, images) are solved with the
25// mass-split inverse masses k x invMass, and after every sweep the group is folded -- the base
26// takes sigma + (sum of the members' increments) / k and every member is re-seeded to it (plus its
27// periodic shift). sigma is the phase seed, re-marked after every rank sync.
28
32inline constexpr int kHubEdgeBudget = 32;
43inline constexpr float kSplitOmegaVelocity = 1.0f;
44
46template <class V>
47inline void growCopyView(V& v, std::size_t n, const char* label) {
48 if (v.extent(0) < n)
49 v = V(Kokkos::view_alloc(Kokkos::WithoutInitializing, std::string(label)), n + n / 2 + 64);
50}
51
54inline void velocityEdgeEnds(Kokkos::View<const ManifoldC*, CpMem> manifolds, int nm,
55 Kokkos::View<const int*, CpMem> realIdx,
56 Kokkos::View<const int*, CpMem> color, Kokkos::View<int*, CpMem> eA,
57 Kokkos::View<int*, CpMem> eB) {
58 Kokkos::parallel_for(
59 "peclet::dem::copies_vel_ends", Kokkos::RangePolicy<CpExec>(0, nm), KOKKOS_LAMBDA(int e) {
60 const ManifoldC m = manifolds(e);
61 const bool act = color(e) != -2;
62 eA(e) = act ? realIdx(m.bodyA) : -1;
63 eB(e) = (act && m.bodyB >= 0) ? realIdx(m.bodyB) : -1;
64 });
65}
66
69inline void positionEdgeEnds(Kokkos::View<const ContactC*, CpMem> contacts, const PosUnits& units,
70 int nu, Kokkos::View<const int*, CpMem> color,
71 Kokkos::View<int*, CpMem> eA, Kokkos::View<int*, CpMem> eB) {
72 Kokkos::parallel_for(
73 "peclet::dem::copies_pos_ends", Kokkos::RangePolicy<CpExec>(0, nu), KOKKOS_LAMBDA(int u) {
74 const ContactC c = contacts(units.leader(u));
75 const bool act = color(u) != -2;
76 eA(u) = act ? c.bodyA : -1;
77 eB(u) = (act && c.bodyB >= 0) ? c.bodyB : -1;
78 });
79}
80
82inline void vertexDegree(Kokkos::View<const int*, CpMem> eA, Kokkos::View<const int*, CpMem> eB,
83 int nE, int nV, Kokkos::View<int*, CpMem> deg) {
84 Kokkos::deep_copy(Kokkos::subview(deg, Kokkos::pair<int, int>(0, nV)), 0);
85 Kokkos::parallel_for(
86 "peclet::dem::copies_degree", Kokkos::RangePolicy<CpExec>(0, nE), KOKKOS_LAMBDA(int e) {
87 if (eA(e) >= 0)
88 Kokkos::atomic_add(&deg(eA(e)), 1);
89 if (eB(e) >= 0)
90 Kokkos::atomic_add(&deg(eB(e)), 1);
91 });
92 Kokkos::fence();
93}
94
101inline void buildHubCopies(Kokkos::View<const int*, CpMem> eA, Kokkos::View<const int*, CpMem> eB,
102 int nE, int nV, int slotBase, Kokkos::View<const int*, CpMem> deg,
103 PhaseCopies& H) {
104 CpExec space;
105 using Kokkos::view_alloc;
106 using Kokkos::WithoutInitializing;
107 H.nHubs = 0;
108 H.nCopies = 0;
109 H.slotBase = slotBase;
110 H.maxHubDegree = 0;
111 growCopyView(H.slotA, static_cast<std::size_t>(nE), "peclet::dem::copies_slotA");
112 growCopyView(H.slotB, static_cast<std::size_t>(nE), "peclet::dem::copies_slotB");
113 Kokkos::deep_copy(space, Kokkos::subview(H.slotA, Kokkos::pair<int, int>(0, nE)), -1);
114 Kokkos::deep_copy(space, Kokkos::subview(H.slotB, Kokkos::pair<int, int>(0, nE)), -1);
115 Kokkos::View<int*, CpMem> hubId(
116 view_alloc(space, "peclet::dem::copies_hubid", WithoutInitializing), nV);
117 int nHubs = 0;
118 Kokkos::parallel_scan(
119 "peclet::dem::copies_hub_scan", Kokkos::RangePolicy<CpExec>(space, 0, nV),
120 KOKKOS_LAMBDA(int v, int& run, const bool final) {
121#if defined(PECLET_DEM_TEST_MUTANT) && PECLET_DEM_TEST_MUTANT == 3
122 const bool hub = false; // G13 mutant 3: no hub copies
123#else
124 const bool hub = deg(v) > kHubEdgeBudget;
125#endif
126 if (final)
127 hubId(v) = hub ? run : -1;
128 if (hub)
129 ++run;
130 },
131 nHubs);
132 space.fence();
133 H.nHubs = nHubs;
134 if (nHubs == 0)
135 return;
136 growCopyView(H.hubVertex, static_cast<std::size_t>(nHubs), "peclet::dem::copies_hubv");
137 growCopyView(H.hubS, static_cast<std::size_t>(nHubs), "peclet::dem::copies_hubs");
138 auto hv = H.hubVertex;
139 auto hs = H.hubS;
140 Kokkos::parallel_for(
141 "peclet::dem::copies_hub_list", Kokkos::RangePolicy<CpExec>(space, 0, nV),
142 KOKKOS_LAMBDA(int v) {
143 const int h = hubId(v);
144 if (h < 0)
145 return;
146 hv(h) = v;
147 hs(h) = (deg(v) + kHubEdgeBudget - 1) / kHubEdgeBudget;
148 });
149 Kokkos::View<int*, CpMem> copyOff(
150 view_alloc(space, "peclet::dem::copies_off", WithoutInitializing), nHubs);
151 Kokkos::View<int*, CpMem> degOff(
152 view_alloc(space, "peclet::dem::copies_doff", WithoutInitializing), nHubs);
153 int nCopies = 0, nEntries = 0, maxDeg = 0;
154 Kokkos::parallel_scan(
155 "peclet::dem::copies_off_scan", Kokkos::RangePolicy<CpExec>(space, 0, nHubs),
156 KOKKOS_LAMBDA(int h, int& run, const bool final) {
157 if (final)
158 copyOff(h) = run;
159 run += hs(h) - 1;
160 },
161 nCopies);
162 Kokkos::parallel_scan(
163 "peclet::dem::copies_doff_scan", Kokkos::RangePolicy<CpExec>(space, 0, nHubs),
164 KOKKOS_LAMBDA(int h, int& run, const bool final) {
165 if (final)
166 degOff(h) = run;
167 run += deg(hv(h));
168 },
169 nEntries);
170 Kokkos::parallel_reduce(
171 "peclet::dem::copies_maxdeg", Kokkos::RangePolicy<CpExec>(space, 0, nHubs),
172 KOKKOS_LAMBDA(int h, int& m) {
173 if (deg(hv(h)) > m)
174 m = deg(hv(h));
175 },
176 Kokkos::Max<int>(maxDeg));
177 space.fence();
178 H.nCopies = nCopies;
179 H.maxHubDegree = maxDeg;
180 // (hub, edge, end) keys: the hub in the high bits, then the edge index, then the end (0 = A,
181 // 1 = B), so each hub's edges sort by edge index -- canonical (manifolds and units are sorted by
182 // pair key).
183 Kokkos::View<std::uint64_t*, CpMem> keys(
184 view_alloc(space, "peclet::dem::copies_keys", WithoutInitializing), nEntries);
185 Kokkos::View<int, CpMem> cursor("peclet::dem::copies_cursor");
186 Kokkos::parallel_for(
187 "peclet::dem::copies_entries", Kokkos::RangePolicy<CpExec>(space, 0, nE),
188 KOKKOS_LAMBDA(int e) {
189 const int ends[2] = {eA(e), eB(e)};
190 for (int end = 0; end < 2; ++end) {
191 const int v = ends[end];
192 if (v < 0 || hubId(v) < 0)
193 continue;
194 const int p = Kokkos::atomic_fetch_add(&cursor(), 1);
195 keys(p) = (static_cast<std::uint64_t>(hubId(v)) << 33) |
196 (static_cast<std::uint64_t>(static_cast<unsigned>(e)) << 1) |
197 static_cast<std::uint64_t>(end);
198 }
199 });
200 Kokkos::sort(space, keys);
201 growCopyView(H.copyBase, static_cast<std::size_t>(nCopies > 0 ? nCopies : 1),
202 "peclet::dem::copies_base");
203 auto sA = H.slotA;
204 auto sB = H.slotB;
205 auto cb = H.copyBase;
206 Kokkos::parallel_for(
207 "peclet::dem::copies_assign", Kokkos::RangePolicy<CpExec>(space, 0, nEntries),
208 KOKKOS_LAMBDA(int p) {
209 const std::uint64_t k = keys(p);
210 const int h = static_cast<int>(k >> 33);
211 const int e = static_cast<int>((k >> 1) & 0xFFFFFFFFull);
212 const int end = static_cast<int>(k & 1ull);
213 const int s = hs(h);
214 const int c = (p - degOff(h)) % s;
215 const int slot = (c == 0) ? hv(h) : slotBase + copyOff(h) + c - 1;
216 if (end == 0)
217 sA(e) = slot;
218 else
219 sB(e) = slot;
220 if (c > 0)
221 cb(copyOff(h) + c - 1) = hv(h);
222 });
223 space.fence();
224}
225
241inline void buildCopyGroups(Kokkos::View<const int*, CpMem> realIdx, int span, int numReal,
242 Kokkos::View<const int*, CpMem> deg,
243 Kokkos::View<const float* [3], CpMem> imageShift, PhaseCopies& H,
244 bool identityBodies = false) {
245 CpExec space;
246 using Kokkos::view_alloc;
247 using Kokkos::WithoutInitializing;
248 const int nC = H.nCopies, base = H.slotBase, nQ = span + nC;
249 H.nGroups = 0;
250 if (nQ <= 0)
251 return;
252 auto cb = H.copyBase;
253 Kokkos::View<int*, CpMem> cnt("peclet::dem::copies_cnt", span);
254 Kokkos::parallel_for(
255 "peclet::dem::copies_grp_cnt", Kokkos::RangePolicy<CpExec>(space, 0, nQ),
256 KOKKOS_LAMBDA(int qi) {
257 const int o = identityBodies ? ((qi < span) ? qi : cb(qi - span))
258 : ((qi < span) ? realIdx(qi) : realIdx(cb(qi - span)));
259 Kokkos::atomic_add(&cnt(o), 1);
260 });
261 Kokkos::View<std::uint64_t*, CpMem> keys(
262 view_alloc(space, "peclet::dem::copies_gkeys", WithoutInitializing), nQ);
263 int nMem = 0;
264 Kokkos::parallel_reduce(
265 "peclet::dem::copies_grp_keys", Kokkos::RangePolicy<CpExec>(space, 0, nQ),
266 KOKKOS_LAMBDA(int qi, int& acc) {
267 const int q = (qi < span) ? qi : base + (qi - span);
268 const int o = identityBodies ? ((qi < span) ? qi : cb(qi - span))
269 : ((qi < span) ? realIdx(qi) : realIdx(cb(qi - span)));
270 if (cnt(o) >= 2) {
271 keys(qi) = (static_cast<std::uint64_t>(static_cast<unsigned>(o)) << 32) |
272 static_cast<unsigned>(q);
273 acc += 1;
274 } else {
275 keys(qi) = ~std::uint64_t(0);
276 }
277 },
278 nMem);
279 space.fence();
280 if (nMem == 0)
281 return;
282 Kokkos::sort(space, keys);
283 growCopyView(H.groupStart, static_cast<std::size_t>(nMem) + 1, "peclet::dem::copies_gstart");
284 growCopyView(H.groupSlot, static_cast<std::size_t>(nMem), "peclet::dem::copies_gslot");
285 growCopyView(H.groupShift, static_cast<std::size_t>(nMem), "peclet::dem::copies_gshift");
286 growCopyView(H.groupActive, static_cast<std::size_t>(nMem), "peclet::dem::copies_gact");
287 auto gs = H.groupStart;
288 auto gq = H.groupSlot;
289 auto gsh = H.groupShift;
290 auto ga = H.groupActive;
291 const bool haveShift = imageShift.extent(0) > 0;
292 int nG = 0;
293 Kokkos::parallel_scan(
294 "peclet::dem::copies_grp_scan", Kokkos::RangePolicy<CpExec>(space, 0, nMem),
295 KOKKOS_LAMBDA(int p, int& run, const bool final) {
296 const bool isNew = (p == 0) || ((keys(p) >> 32) != (keys(p - 1) >> 32));
297 if (final) {
298 const int q = static_cast<int>(keys(p) & 0xFFFFFFFFull);
299 gq(p) = q;
300 const int b = (q < base) ? q : cb(q - base); // an image's (or its copy's) image slot
301 F3 sh{0.0f, 0.0f, 0.0f};
302 if (haveShift && b >= numReal && b < span)
303 sh = F3{imageShift(b, 0), imageShift(b, 1), imageShift(b, 2)};
304 gsh(p, 0) = sh.x;
305 gsh(p, 1) = sh.y;
306 gsh(p, 2) = sh.z;
307 ga(p) = (q >= base || deg(q) > 0) ? 1 : 0; // a copy always has an edge
308 if (isNew)
309 gs(run) = p;
310 }
311 if (isNew)
312 ++run;
313 },
314 nG);
315 Kokkos::deep_copy(space, Kokkos::subview(gs, nG), nMem);
316 growCopyView(H.groupK, static_cast<std::size_t>(nG), "peclet::dem::copies_gk");
317 growCopyView(H.seedX, static_cast<std::size_t>(nG), "peclet::dem::copies_seedx");
318 growCopyView(H.seedW, static_cast<std::size_t>(nG), "peclet::dem::copies_seedw");
319 auto gk = H.groupK;
320 Kokkos::parallel_for(
321 "peclet::dem::copies_grp_k", Kokkos::RangePolicy<CpExec>(space, 0, nG), KOKKOS_LAMBDA(int g) {
322 int k = 0;
323 for (int p = gs(g); p < gs(g + 1); ++p)
324 k += ga(p);
325 gk(g) = k > 0 ? k : 1;
326 });
327 space.fence();
328 H.nGroups = nG;
329}
330
334inline void buildSolveViews(Particles& P, const PhaseCopies& H, int nSlots,
335 Kokkos::View<const float*, CpMem> invMass,
336 Kokkos::View<const float* [3], CpMem> invInertia) {
337 CpExec space;
338 growCopyView(P.invMassSolve, static_cast<std::size_t>(nSlots), "peclet::dem::invMassSolve");
339 growCopyView(P.invInertiaSolve, static_cast<std::size_t>(nSlots), "peclet::dem::invInertiaSolve");
340 growCopyView(P.splitSlot, static_cast<std::size_t>(nSlots), "peclet::dem::splitSlot");
341 auto ims = P.invMassSolve;
342 auto iis = P.invInertiaSolve;
343 auto spl = P.splitSlot;
344 Kokkos::parallel_for(
345 "peclet::dem::copies_solve_views", Kokkos::RangePolicy<CpExec>(space, 0, nSlots),
346 KOKKOS_LAMBDA(int q) {
347 const bool own = q < static_cast<int>(invMass.extent(0));
348 ims(q) = own ? invMass(q) : 0.0f;
349 for (int c = 0; c < 3; ++c)
350 iis(q, c) = own ? invInertia(q, c) : 0.0f;
351 spl(q) = 0;
352 });
353 auto gs = H.groupStart;
354 auto gq = H.groupSlot;
355 auto gk = H.groupK;
356 Kokkos::parallel_for(
357 "peclet::dem::copies_solve_split", Kokkos::RangePolicy<CpExec>(space, 0, H.nGroups),
358 KOKKOS_LAMBDA(int g) {
359 const int b = gq(gs(g));
360 const int k = gk(g);
361 const float kf = static_cast<float>(k);
362 const float m = kf * invMass(b);
363 const float i0 = kf * invInertia(b, 0), i1 = kf * invInertia(b, 1),
364 i2 = kf * invInertia(b, 2);
365 for (int p = gs(g); p < gs(g + 1); ++p) {
366 const int q = gq(p);
367 ims(q) = m;
368 iis(q, 0) = i0;
369 iis(q, 1) = i1;
370 iis(q, 2) = i2;
371 spl(q) = k > 1 ? 1 : 0;
372 }
373 });
374 space.fence();
375}
376
379inline void seedCopySlotState(Particles& P, const PhaseCopies& H) {
380 if (H.nCopies <= 0)
381 return;
382 auto cb = H.copyBase;
383 const int base = H.slotBase;
384 auto quat = P.quat;
385 auto quatPred = P.quatPred;
386 auto invI = P.invInertia;
387 auto grounded = P.groundedLevel;
388 Kokkos::parallel_for(
389 "peclet::dem::copies_seed_state", Kokkos::RangePolicy<CpExec>(0, H.nCopies),
390 KOKKOS_LAMBDA(int c) {
391 const int q = base + c, b = cb(c);
392 for (int j = 0; j < 4; ++j) {
393 quat(q, j) = quat(b, j);
394 quatPred(q, j) = quatPred(b, j);
395 }
396 for (int j = 0; j < 3; ++j)
397 invI(q, j) = invI(b, j);
398 grounded(q) = grounded(b);
399 });
400 Kokkos::fence();
401}
402
408inline void markCopySeeds(const PhaseCopies& H, Kokkos::View<float* [3], CpMem> x,
409 Kokkos::View<float* [3], CpMem> w, Kokkos::View<float*, CpMem> orphan,
410 Kokkos::View<float*, CpMem> orphanPeak, bool shareFromBase = false) {
411 if (H.nGroups <= 0)
412 return;
413 auto gs = H.groupStart;
414 auto gq = H.groupSlot;
415 auto gsh = H.groupShift;
416 auto gk = H.groupK;
417 auto sx = H.seedX;
418 auto sw = H.seedW;
419 const bool haveW = w.extent(0) > 0;
420 const bool haveO = orphan.extent(0) > 0;
421 Kokkos::parallel_for(
422 "peclet::dem::copies_mark", Kokkos::RangePolicy<CpExec>(0, H.nGroups), KOKKOS_LAMBDA(int g) {
423 const int b = gq(gs(g));
424 for (int c = 0; c < 3; ++c) {
425 sx(g, c) = x(b, c);
426 if (haveW)
427 sw(g, c) = w(b, c);
428 }
429 float share = 0.0f, pk = 0.0f;
430 if (haveO) {
431 share = shareFromBase ? orphan(b) : orphan(b) / static_cast<float>(gk(g));
432 pk = orphanPeak(b);
433 }
434 for (int p = gs(g); p < gs(g + 1); ++p) {
435 const int q = gq(p);
436 if (q != b) {
437 for (int c = 0; c < 3; ++c) {
438 x(q, c) = sx(g, c) + gsh(p, c);
439 if (haveW)
440 w(q, c) = sw(g, c);
441 }
442 }
443 if (haveO) {
444 orphan(q) = share;
445 orphanPeak(q) = pk;
446 }
447 }
448 });
449}
450
459inline void foldCopies(CpExec space, const PhaseCopies& H, Kokkos::View<float* [3], CpMem> x,
460 Kokkos::View<float* [3], CpMem> w, Kokkos::View<float*, CpMem> orphan,
461 Kokkos::View<float*, CpMem> orphanPeak,
462 Kokkos::View<float, CpMem> consensus = {}) {
463 if (H.nGroups <= 0)
464 return;
465 auto gs = H.groupStart;
466 auto gq = H.groupSlot;
467 auto gsh = H.groupShift;
468 auto gk = H.groupK;
469 auto ga = H.groupActive;
470 auto sx = H.seedX;
471 auto sw = H.seedW;
472 const bool haveW = w.extent(0) > 0;
473 const bool haveO = orphan.extent(0) > 0;
474 const bool haveC = consensus.data() != nullptr;
475 Kokkos::parallel_for(
476 "peclet::dem::copies_fold", Kokkos::RangePolicy<CpExec>(space, 0, H.nGroups),
477 KOKKOS_LAMBDA(int g) {
478 const float kf = static_cast<float>(gk(g));
479 float tx[3] = {0.0f, 0.0f, 0.0f}, tw[3] = {0.0f, 0.0f, 0.0f};
480 float oSum = 0.0f, oPk = 0.0f;
481 for (int p = gs(g); p < gs(g + 1); ++p) {
482 if (!ga(p))
483 continue;
484 const int q = gq(p);
485 for (int c = 0; c < 3; ++c) {
486 tx[c] += x(q, c) - (sx(g, c) + gsh(p, c));
487 if (haveW)
488 tw[c] += w(q, c) - sw(g, c);
489 }
490 if (haveO) {
491 oSum += orphan(q);
492 oPk = Kokkos::fmax(oPk, orphanPeak(q));
493 }
494 }
495 float nx[3], nw[3];
496 for (int c = 0; c < 3; ++c) {
497#if defined(PECLET_DEM_TEST_MUTANT) && PECLET_DEM_TEST_MUTANT == 2
498 nx[c] = sx(g, c) + tx[c]; // G13 mutant 2: raw sum of split-mass increments (no 1/k)
499 nw[c] = haveW ? sw(g, c) + tw[c] : 0.0f;
500#else
501 nx[c] = sx(g, c) + tx[c] / kf;
502 nw[c] = haveW ? sw(g, c) + tw[c] / kf : 0.0f;
503#endif
504 }
505 const float share = oSum / kf;
506 if (haveC) {
507 float cmax = 0.0f;
508 for (int p = gs(g); p < gs(g + 1); ++p) {
509 if (!ga(p))
510 continue;
511 const int q = gq(p);
512 float s2 = 0.0f;
513 for (int c = 0; c < 3; ++c) {
514 const float e = tx[c] / kf - (x(q, c) - (sx(g, c) + gsh(p, c)));
515 s2 += e * e;
516 }
517 cmax = Kokkos::fmax(cmax, Kokkos::sqrt(s2));
518 }
519 if (cmax > 0.0f)
520 Kokkos::atomic_max(&consensus(), cmax);
521 }
522 for (int p = gs(g); p < gs(g + 1); ++p) {
523 const int q = gq(p);
524 for (int c = 0; c < 3; ++c) {
525 x(q, c) = nx[c] + gsh(p, c);
526 if (haveW)
527 w(q, c) = nw[c];
528 }
529 if (haveO) {
530 orphan(q) = share;
531 orphanPeak(q) = oPk;
532 }
533 }
534 });
535}
536
539inline void reseedCopies(CpExec space, const PhaseCopies& H, Kokkos::View<float* [3], CpMem> x,
540 Kokkos::View<float* [3], CpMem> w) {
541 if (H.nGroups <= 0)
542 return;
543 auto gs = H.groupStart;
544 auto gq = H.groupSlot;
545 auto gsh = H.groupShift;
546 Kokkos::parallel_for(
547 "peclet::dem::copies_reseed", Kokkos::RangePolicy<CpExec>(space, 0, H.nGroups),
548 KOKKOS_LAMBDA(int g) {
549 const int b = gq(gs(g));
550 for (int p = gs(g) + 1; p < gs(g + 1); ++p) {
551 const int q = gq(p);
552 for (int c = 0; c < 3; ++c) {
553 x(q, c) = x(b, c) + gsh(p, c);
554 w(q, c) = w(b, c);
555 }
556 }
557 });
558}
559
562inline void unfoldOrphan(const PhaseCopies& H, Kokkos::View<float*, CpMem> orphan) {
563 if (H.nGroups <= 0 || orphan.extent(0) == 0)
564 return;
565 auto gs = H.groupStart;
566 auto gq = H.groupSlot;
567 auto ga = H.groupActive;
568 Kokkos::parallel_for(
569 "peclet::dem::copies_unfold", Kokkos::RangePolicy<CpExec>(0, H.nGroups),
570 KOKKOS_LAMBDA(int g) {
571 float s = 0.0f;
572 for (int p = gs(g); p < gs(g + 1); ++p)
573 if (ga(p))
574 s += orphan(gq(p));
575 orphan(gq(gs(g))) = s;
576 });
577 Kokkos::fence();
578}
579
584inline void activityHitVelocity(Kokkos::View<const ManifoldC*, CpMem> manifolds, int nm,
585 Kokkos::View<const int*, CpMem> realIdx,
586 Kokkos::View<const int*, CpMem> color, SlotOverride ov,
587 Kokkos::View<unsigned char*, CpMem> hit, int nSlots) {
588 CpExec space;
589 Kokkos::deep_copy(space, Kokkos::subview(hit, Kokkos::pair<int, int>(0, nSlots)),
590 static_cast<unsigned char>(0));
591 Kokkos::parallel_for(
592 "peclet::dem::activity_vel", Kokkos::RangePolicy<CpExec>(space, 0, nm), KOKKOS_LAMBDA(int e) {
593 if (color(e) < 0)
594 return;
595 const ManifoldC m = manifolds(e);
596 Kokkos::atomic_store(&hit(ov.slotA(e, realIdx(m.bodyA))), static_cast<unsigned char>(1));
597 if (m.bodyB >= 0)
598 Kokkos::atomic_store(&hit(ov.slotB(e, realIdx(m.bodyB))), static_cast<unsigned char>(1));
599 });
600 space.fence();
601}
602
604inline void activityHitPosition(Kokkos::View<const ContactC*, CpMem> contacts,
605 const PosUnits& units, int nu,
606 Kokkos::View<const int*, CpMem> color, SlotOverride ov,
607 Kokkos::View<unsigned char*, CpMem> hit, int nSlots) {
608 CpExec space;
609 Kokkos::deep_copy(space, Kokkos::subview(hit, Kokkos::pair<int, int>(0, nSlots)),
610 static_cast<unsigned char>(0));
611 Kokkos::parallel_for(
612 "peclet::dem::activity_pos", Kokkos::RangePolicy<CpExec>(space, 0, nu), KOKKOS_LAMBDA(int u) {
613 if (color(u) < 0)
614 return;
615 const ContactC c = contacts(units.leader(u));
616 Kokkos::atomic_store(&hit(ov.slotA(u, c.bodyA)), static_cast<unsigned char>(1));
617 if (c.bodyB >= 0)
618 Kokkos::atomic_store(&hit(ov.slotB(u, c.bodyB)), static_cast<unsigned char>(1));
619 });
620 space.fence();
621}
622
626inline void finishActivity(Kokkos::View<const unsigned char*, CpMem> hit, int nSlots,
627 PhaseCopies& H, Kokkos::View<int*, CpMem> a) {
628 CpExec space;
629 const int nAll = nSlots + (H.nGroups > 0 ? H.nCopies : 0);
630 Kokkos::parallel_for(
631 "peclet::dem::activity_a", Kokkos::RangePolicy<CpExec>(space, 0, nAll),
632 KOKKOS_LAMBDA(int q) { a(q) = q < nSlots ? static_cast<int>(hit(q)) : 0; });
633 if (H.nGroups > 0) {
634 auto gs = H.groupStart;
635 auto gq = H.groupSlot;
636 auto ga = H.groupActive;
637 auto gk = H.groupK;
638 Kokkos::parallel_for(
639 "peclet::dem::activity_groups", Kokkos::RangePolicy<CpExec>(space, 0, H.nGroups),
640 KOKKOS_LAMBDA(int g) {
641 int n = 0;
642 for (int p = gs(g); p < gs(g + 1); ++p) {
643 const unsigned char h = hit(gq(p));
644 ga(p) = h;
645 n += h;
646 }
647 gk(g) = n > 0 ? n : 1;
648 a(gq(gs(g))) = n;
649 });
650 }
651 space.fence();
652}
653
658inline void buildSolveViewsRankK(Particles& P, const PhaseCopies& H, int nSlots,
659 Kokkos::View<const float*, CpMem> invMass,
660 Kokkos::View<const float* [3], CpMem> invInertia,
661 Kokkos::View<const int*, CpMem> kRank) {
662 CpExec space;
663 growCopyView(P.invMassSolve, static_cast<std::size_t>(nSlots), "peclet::dem::invMassSolve");
664 growCopyView(P.invInertiaSolve, static_cast<std::size_t>(nSlots), "peclet::dem::invInertiaSolve");
665 growCopyView(P.splitSlot, static_cast<std::size_t>(nSlots), "peclet::dem::splitSlot");
666 auto ims = P.invMassSolve;
667 auto iis = P.invInertiaSolve;
668 auto spl = P.splitSlot;
669 auto cb = H.copyBase;
670 const int base = H.slotBase;
671 const bool haveCopies = H.nCopies > 0;
672 Kokkos::parallel_for(
673 "peclet::dem::rankk_solve_views", Kokkos::RangePolicy<CpExec>(space, 0, nSlots),
674 KOKKOS_LAMBDA(int q) {
675 const int b = (haveCopies && q >= base) ? cb(q - base) : q;
676 const int k = kRank(b);
677 if (k > 1) {
678 const float kf = static_cast<float>(k);
679 ims(q) = kf * invMass(b);
680 for (int c = 0; c < 3; ++c)
681 iis(q, c) = kf * invInertia(b, c);
682 spl(q) = 1;
683 } else {
684 ims(q) = invMass(b);
685 for (int c = 0; c < 3; ++c)
686 iis(q, c) = invInertia(b, c);
687 spl(q) = 0;
688 }
689 });
690 space.fence();
691}
692
695inline void buildInvMassCoarse(Kokkos::View<const float*, CpMem> invMass,
696 Kokkos::View<const int*, CpMem> k, Kokkos::View<const int*, CpMem> a,
697 int n, Kokkos::View<float*, CpMem> out) {
698 Kokkos::parallel_for(
699 "peclet::dem::inv_mass_coarse", Kokkos::RangePolicy<CpExec>(0, n), KOKKOS_LAMBDA(int q) {
700 const int aq = a(q) > 1 ? a(q) : 1;
701 const float ratio = static_cast<float>(k(q)) / static_cast<float>(aq);
702 out(q) = invMass(q) * ratio;
703 });
704 Kokkos::fence();
705}
706
711inline void buildInvInertiaCoarse(Kokkos::View<const float* [3], CpMem> invInertia,
712 Kokkos::View<const int*, CpMem> k,
713 Kokkos::View<const int*, CpMem> a, int n,
714 Kokkos::View<float* [3], CpMem> out) {
715 Kokkos::parallel_for(
716 "peclet::dem::inv_inertia_coarse", Kokkos::RangePolicy<CpExec>(0, n), KOKKOS_LAMBDA(int q) {
717 const int aq = a(q) > 1 ? a(q) : 1;
718 const float ratio = static_cast<float>(k(q)) / static_cast<float>(aq);
719 for (int c = 0; c < 3; ++c)
720 out(q, c) = invInertia(q, c) * ratio;
721 });
722 Kokkos::fence();
723}
724
727inline int countInactiveAggregated(Kokkos::View<const int*, CpMem> parent, int off, int nV,
728 int nGroups1, Kokkos::View<const int*, CpMem> a) {
729 if (nV <= 0 || nGroups1 <= 0)
730 return 0;
731 Kokkos::View<int*, CpMem> cnt("peclet::dem::ml_inact_cnt", nGroups1);
732 Kokkos::parallel_for(
733 "peclet::dem::ml_inact_hist", Kokkos::RangePolicy<CpExec>(0, nV),
734 KOKKOS_LAMBDA(int v) { Kokkos::atomic_add(&cnt(parent(off + v)), 1); });
735 int n = 0;
736 Kokkos::parallel_reduce(
737 "peclet::dem::ml_inact_count", Kokkos::RangePolicy<CpExec>(0, nV),
738 KOKKOS_LAMBDA(int v, int& acc) {
739 if (a(v) == 0 && cnt(parent(off + v)) >= 2)
740 acc += 1;
741 },
742 n);
743 return n;
744}
745
747inline int countSplitBodies(const PhaseCopies& H) {
748 if (H.nGroups <= 0)
749 return 0;
750 auto gk = H.groupK;
751 int n = 0;
752 Kokkos::parallel_reduce(
753 "peclet::dem::copies_count_split", Kokkos::RangePolicy<CpExec>(0, H.nGroups),
754 KOKKOS_LAMBDA(int g, int& acc) { acc += gk(g) > 1 ? 1 : 0; }, n);
755 return n;
756}
757
762inline int countSplitAggregated(Kokkos::View<const int*, CpMem> parent, int off, int nV,
763 int nGroups1, Kokkos::View<const unsigned char*, CpMem> split) {
764 if (nV <= 0 || nGroups1 <= 0 || split.extent(0) < static_cast<std::size_t>(nV))
765 return 0;
766 Kokkos::View<int*, CpMem> cnt("peclet::dem::ml_agg_cnt", nGroups1);
767 Kokkos::parallel_for(
768 "peclet::dem::ml_agg_hist", Kokkos::RangePolicy<CpExec>(0, nV),
769 KOKKOS_LAMBDA(int v) { Kokkos::atomic_add(&cnt(parent(off + v)), 1); });
770 int n = 0;
771 Kokkos::parallel_reduce(
772 "peclet::dem::ml_agg_count", Kokkos::RangePolicy<CpExec>(0, nV),
773 KOKKOS_LAMBDA(int v, int& acc) {
774 if (split(v) != 0 && cnt(parent(off + v)) >= 2)
775 acc += 1;
776 },
777 n);
778 return n;
779}
780
783inline int countLightHubs(const PhaseCopies& H, Kokkos::View<const int*, CpMem> eA,
784 Kokkos::View<const int*, CpMem> eB, int nE, int nV,
785 Kokkos::View<const float*, CpMem> invMass) {
786 if (H.nHubs <= 0)
787 return 0;
788 const int nH = H.nHubs;
789 auto hv = H.hubVertex;
790 Kokkos::View<int*, CpMem> hubOf("peclet::dem::lh_hubof", nV);
791 Kokkos::deep_copy(hubOf, -1);
792 Kokkos::parallel_for(
793 "peclet::dem::lh_map", Kokkos::RangePolicy<CpExec>(0, nH),
794 KOKKOS_LAMBDA(int h) { hubOf(hv(h)) = h; });
795 Kokkos::View<double*, CpMem> msum("peclet::dem::lh_msum", nH);
796 Kokkos::View<int*, CpMem> mcnt("peclet::dem::lh_mcnt", nH);
797 Kokkos::parallel_for(
798 "peclet::dem::lh_accum", Kokkos::RangePolicy<CpExec>(0, nE), KOKKOS_LAMBDA(int e) {
799 const int a = eA(e), b = eB(e);
800 if (a < 0 || b < 0)
801 return;
802 const int ends[2][2] = {{a, b}, {b, a}};
803 for (int j = 0; j < 2; ++j) {
804 const int h = hubOf(ends[j][0]);
805 const float im = invMass(ends[j][1]);
806 if (h >= 0 && im > 0.0f) {
807 Kokkos::atomic_add(&msum(h), 1.0 / static_cast<double>(im));
808 Kokkos::atomic_add(&mcnt(h), 1);
809 }
810 }
811 });
812 int n = 0;
813 Kokkos::parallel_reduce(
814 "peclet::dem::lh_count", Kokkos::RangePolicy<CpExec>(0, nH),
815 KOKKOS_LAMBDA(int h, int& acc) {
816 const float im = invMass(hv(h));
817 if (mcnt(h) == 0 || im <= 0.0f)
818 return;
819 const double mHub = 1.0 / static_cast<double>(im);
820 if (mHub < 10.0 * msum(h) / static_cast<double>(mcnt(h)))
821 acc += 1;
822 },
823 n);
824 return n;
825}
826
827} // namespace peclet::dem
828
829#endif // DEM_SOLVE_COPIES_HPP
dem — portable (Kokkos) contact->manifold reduction, replacing the thrust-based reduce_contacts_to_ma...
int countLightHubs(const PhaseCopies &H, Kokkos::View< const int *, CpMem > eA, Kokkos::View< const int *, CpMem > eB, int nE, int nV, Kokkos::View< const float *, CpMem > invMass)
Light hubs (split_stats, R-P3): velocity hubs whose mass is below 10x the mean mass of their partners...
void vertexDegree(Kokkos::View< const int *, CpMem > eA, Kokkos::View< const int *, CpMem > eB, int nE, int nV, Kokkos::View< int *, CpMem > deg)
Per-vertex active degree over [0, nV) (§4.4 step 1). Wall edges have one endpoint.
void finishActivity(Kokkos::View< const unsigned char *, CpMem > hit, int nSlots, PhaseCopies &H, Kokkos::View< int *, CpMem > a)
a(q) from the pass (§13.3): hit(q) for a slot outside any group; at a hub base the number of its grou...
void activityHitVelocity(Kokkos::View< const ManifoldC *, CpMem > manifolds, int nm, Kokkos::View< const int *, CpMem > realIdx, Kokkos::View< const int *, CpMem > color, SlotOverride ov, Kokkos::View< unsigned char *, CpMem > hit, int nSlots)
Rank-level activity pass (docs/contact_solve_framework.md §13.3 C4; the distributed step only),...
void markCopySeeds(const PhaseCopies &H, Kokkos::View< float *[3], CpMem > x, Kokkos::View< float *[3], CpMem > w, Kokkos::View< float *, CpMem > orphan, Kokkos::View< float *, CpMem > orphanPeak, bool shareFromBase=false)
Mark the phase seed sigma of every group and re-seed its members from the base (§4....
void positionEdgeEnds(Kokkos::View< const ContactC *, CpMem > contacts, const PosUnits &units, int nu, Kokkos::View< const int *, CpMem > color, Kokkos::View< int *, CpMem > eA, Kokkos::View< int *, CpMem > eB)
Position-phase colouring edges: one per unit, the leader contact's raw (bodyA, bodyB); active = the u...
void velocityEdgeEnds(Kokkos::View< const ManifoldC *, CpMem > manifolds, int nm, Kokkos::View< const int *, CpMem > realIdx, Kokkos::View< const int *, CpMem > color, Kokkos::View< int *, CpMem > eA, Kokkos::View< int *, CpMem > eB)
Velocity-phase colouring edges: the vertices realIdx(bodyA / bodyB) of each manifold (-1: a wall end,...
constexpr int kHubEdgeBudget
Hub edge budget (§4.4): a colouring vertex with more active edges is split into ceil(d / kHubEdgeBudg...
void buildSolveViewsRankK(Particles &P, const PhaseCopies &H, int nSlots, Kokkos::View< const float *, CpMem > invMass, Kokkos::View< const float *[3], CpMem > invInertia, Kokkos::View< const int *, CpMem > kRank)
Solve views under rank-level M (§13.3, §4.5): invMassSolve / invInertiaSolve = k x the body's masses ...
CpExec::memory_space CpMem
void activityHitPosition(Kokkos::View< const ContactC *, CpMem > contacts, const PosUnits &units, int nu, Kokkos::View< const int *, CpMem > color, SlotOverride ov, Kokkos::View< unsigned char *, CpMem > hit, int nSlots)
The same over the position phase's units (raw slots of the leader contact, through the overrides).
void reseedCopies(CpExec space, const PhaseCopies &H, Kokkos::View< float *[3], CpMem > x, Kokkos::View< float *[3], CpMem > w)
Re-seed every member from its base, sigma unchanged (the multilevel pass, after the coarse cycle has ...
void buildHubCopies(Kokkos::View< const int *, CpMem > eA, Kokkos::View< const int *, CpMem > eB, int nE, int nV, int slotBase, Kokkos::View< const int *, CpMem > deg, PhaseCopies &H)
Build one phase's hub copies (§4.4 steps 2, 3 and 5): every vertex with d > kHubEdgeBudget active edg...
void buildInvInertiaCoarse(Kokkos::View< const float *[3], CpMem > invInertia, Kokkos::View< const int *, CpMem > k, Kokkos::View< const int *, CpMem > a, int n, Kokkos::View< float *[3], CpMem > out)
The multilevel coarse vertex's inverse inertia (docs/contact_physics_followups.md §2....
void buildSolveViews(Particles &P, const PhaseCopies &H, int nSlots, Kokkos::View< const float *, CpMem > invMass, Kokkos::View< const float *[3], CpMem > invInertia)
Solve views of a phase (§4.5): invMassSolve / invInertiaSolve = the phase's masses on every slot in [...
void seedCopySlotState(Particles &P, const PhaseCopies &H)
Seed the copy slots' read-only state from their hub vertex (§4.4 step 6): orientation,...
Kokkos::DefaultExecutionSpace CpExec
constexpr float kSplitOmegaVelocity
Split relaxation (§4.5, §13.1): the PGS normal step on an edge touching a mass-split slot is scaled b...
int countSplitBodies(const PhaseCopies &H)
Count the bodies whose group has k > 1 (split_stats).
int countSplitAggregated(Kokkos::View< const int *, CpMem > parent, int off, int nV, int nGroups1, Kokkos::View< const unsigned char *, CpMem > split)
split_stats.mlHubAggregated (§13.2's positive control): the vertices v in [0, nV) with split(v) !...
void growCopyView(V &v, std::size_t n, const char *label)
Grow-only (re)allocation of a copy-machinery view to at least n entries.
void buildCopyGroups(Kokkos::View< const int *, CpMem > realIdx, int span, int numReal, Kokkos::View< const int *, CpMem > deg, Kokkos::View< const float *[3], CpMem > imageShift, PhaseCopies &H, bool identityBodies=false)
Build the fold groups of a phase (§4.4 "local fold", §WO-4 item 2): every body with more than one slo...
void foldCopies(CpExec space, const PhaseCopies &H, Kokkos::View< float *[3], CpMem > x, Kokkos::View< float *[3], CpMem > w, Kokkos::View< float *, CpMem > orphan, Kokkos::View< float *, CpMem > orphanPeak, Kokkos::View< float, CpMem > consensus={})
The local fold (§4.4), once per iteration after the sweep: per group T = sum over the ACTIVE members ...
int countInactiveAggregated(Kokkos::View< const int *, CpMem > parent, int off, int nV, int nGroups1, Kokkos::View< const int *, CpMem > a)
Debug-build check (§13.2, §13.5 WO-5 item 4): the vertices with a = 0 that sit in a level-1 multileve...
void buildInvMassCoarse(Kokkos::View< const float *, CpMem > invMass, Kokkos::View< const int *, CpMem > k, Kokkos::View< const int *, CpMem > a, int n, Kokkos::View< float *, CpMem > out)
The multilevel coarse vertex's inverse mass (§13.2): invMass(q) (k(q) / max(1, a(q))),...
void unfoldOrphan(const PhaseCopies &H, Kokkos::View< float *, CpMem > orphan)
Orphan accounts back from share form (before a rank sync and at the end of the velocity phase): the b...
dem — portable (Kokkos) particle SoA container: the storage the dem flip pivots on.
Portable mirror of ParticleSystem.cuh ContactConstraint (the fields this reduction touches).
Portable mirror of ManifoldConstraint.
Kokkos::View< float *[3], CpMem > invInertiaSolve
Kokkos::View< unsigned char *, CpMem > splitSlot
Kokkos::View< float *, CpMem > invMassSolve
Kokkos::View< unsigned char *, CpMem > groundedLevel
One solve phase's body copies (docs/contact_solve_framework.md §4.4, §4.5, §WO-4 item 2): the hub cop...
Definition particles.hpp:44
Kokkos::View< float *[3], CpMem > seedX
Definition particles.hpp:53
Kokkos::View< int *, CpMem > hubS
Definition particles.hpp:49
Kokkos::View< float *[3], CpMem > seedW
Definition particles.hpp:53
Kokkos::View< int *, CpMem > slotB
Definition particles.hpp:47
Kokkos::View< float *[3], CpMem > groupShift
Definition particles.hpp:51
Kokkos::View< int *, CpMem > hubVertex
Definition particles.hpp:49
Kokkos::View< int *, CpMem > copyBase
Definition particles.hpp:48
Kokkos::View< int *, CpMem > slotA
Definition particles.hpp:47
Kokkos::View< int *, CpMem > groupK
Definition particles.hpp:50
Kokkos::View< int *, CpMem > groupStart
Definition particles.hpp:50
Kokkos::View< unsigned char *, CpMem > groupActive
Definition particles.hpp:52
Kokkos::View< int *, CpMem > groupSlot
Definition particles.hpp:50
The position units of a contact list as a CSR (§4.3): unit u owns the contacts list(start(u)) .
Per-edge slot overrides of a phase's hub copies (docs/contact_solve_framework.md §4....
int slotA(int e, int def) const
int slotB(int e, int def) const