16#ifndef DEM_CONTACT_PREPROCESSING_HPP
17#define DEM_CONTACT_PREPROCESSING_HPP
21#include <Kokkos_Core.hpp>
22#include <Kokkos_Sort.hpp>
29using CpExec = Kokkos::DefaultExecutionSpace;
30using CpMem = CpExec::memory_space;
38 Kokkos::View<float* [3], CpMem>
pos;
39 Kokkos::View<float* [3], CpMem>
vel;
40 Kokkos::View<float* [4], CpMem>
quat;
41 Kokkos::View<float* [3], CpMem>
angVel;
44 Kokkos::View<float*, CpMem>
scale;
69 Kokkos::View<const unsigned long long*, CpMem>
keys;
70 Kokkos::View<const int*, CpMem>
color;
77 Kokkos::View<unsigned char*, CpMem>
asleep;
95 Kokkos::View<unsigned long long*, CpMem>
keys;
134 return -1 - wallIndex;
163 const unsigned hi = a < b ? a : b, lo = a < b ? b : a;
164 return (
static_cast<unsigned long long>(hi) << 32) | lo;
170template <
class KeysView>
171KOKKOS_INLINE_FUNCTION
int lowerBoundKey(
const KeysView& keys,
int count,
unsigned long long key) {
172 int lo = 0, hi = count;
174 const int mid = (lo + hi) >> 1;
189 Kokkos::View<const int*, CpMem> realIdx) {
190 const unsigned a =
static_cast<unsigned>(realIdx(m.
bodyA));
191 const unsigned b = (m.
bodyB >= 0) ?
static_cast<unsigned>(realIdx(m.
bodyB)) : 0xFFFFFFFFu;
199 int numManifolds, Kokkos::View<const int*, CpMem> realIdx,
200 Kokkos::View<const int*, CpMem> keyIdx,
201 Kokkos::View<const unsigned long long*, CpMem> prevKeys,
202 int prevCount, Kokkos::View<unsigned long long*, CpMem> outKeys,
203 Kokkos::View<unsigned char*, CpMem> outFlags) {
206 Kokkos::parallel_for(
207 "peclet::dem::mark_persistent", Kokkos::RangePolicy<CpExec>(space, 0, numManifolds),
208 KOKKOS_LAMBDA(
int idx) {
211 outKeys(idx) = ~0ull;
215 const unsigned long long k =
pairKeyOf(m, keyIdx);
218 outFlags(idx) = (lo < prevCount && prevKeys(lo) == k) ? 1 : 0;
228inline void gatherWarmLambda(Kokkos::View<const ManifoldC*, CpMem> manifolds,
int numManifolds,
229 Kokkos::View<const int*, CpMem> realIdx,
230 Kokkos::View<const int*, CpMem> keyIdx,
const PairLedger& prev,
231 Kokkos::View<const float*, CpMem> prevPosImpulse,
int prevCount,
232 const PairLedger& out, Kokkos::View<float*, CpMem> outPosImpulse,
233 Kokkos::View<unsigned char*, CpMem> outMatched = {},
234 bool dedupTwins =
true) {
235 const Kokkos::View<const unsigned long long*, CpMem> prevKeys = prev.
keys;
236 const Kokkos::View<const float*, CpMem> prevLambda = prev.
lambda;
237 const Kokkos::View<const float* [3], CpMem> prevLambdaT = prev.
lambdaT;
238 const Kokkos::View<const float*, CpMem> prevRestBank = prev.
restBank,
240 const Kokkos::View<unsigned long long*, CpMem> outKeys = out.
keys;
241 const Kokkos::View<float*, CpMem> outWarm = out.
lambda;
242 const Kokkos::View<float* [3], CpMem> outWarmT = out.
lambdaT;
243 const Kokkos::View<float*, CpMem> outRestBank = out.
restBank, outRestVPeak = out.
restVPeak;
245 Kokkos::parallel_for(
246 "peclet::dem::gather_warm", Kokkos::RangePolicy<CpExec>(space, 0, numManifolds),
247 KOKKOS_LAMBDA(
int idx) {
248 const ManifoldC m = manifolds(idx);
250 if (dedupTwins && m.num_points > 0 && m.bodyB >= 0 && realIdx(m.bodyA) > realIdx(m.bodyB))
252 if (m.num_points <= 0 || dup) {
253 outKeys(idx) = ~0ull;
255 outWarmT(idx, 0) = outWarmT(idx, 1) = outWarmT(idx, 2) = 0.0f;
256 outPosImpulse(idx) = 0.0f;
257 outRestBank(idx) = 0.0f;
258 outRestVPeak(idx) = 0.0f;
261 const unsigned long long k =
pairKeyOf(m, keyIdx);
264 const bool hit = (lo < prevCount && prevKeys(lo) == k);
265 if (hit && outMatched.extent(0) > 0)
267 outWarm(idx) = hit ? prevLambda(lo) : 0.0f;
268 outWarmT(idx, 0) = hit ? prevLambdaT(lo, 0) : 0.0f;
269 outWarmT(idx, 1) = hit ? prevLambdaT(lo, 1) : 0.0f;
270 outWarmT(idx, 2) = hit ? prevLambdaT(lo, 2) : 0.0f;
271 outPosImpulse(idx) = hit ? prevPosImpulse(lo) : 0.0f;
272 outRestBank(idx) = hit ? prevRestBank(lo) : 0.0f;
273 outRestVPeak(idx) = hit ? prevRestVPeak(lo) : 0.0f;
281 Kokkos::View<int*, CpMem> perm,
285 Kokkos::View<const int*, CpMem> color = {},
286 Kokkos::View<int*, CpMem> prevColor = {}) {
287 const Kokkos::View<const unsigned long long*, CpMem> keys = cur.
keys;
288 const Kokkos::View<const float*, CpMem> lambda = cur.
lambda;
289 const Kokkos::View<const float* [3], CpMem> lambdaT = cur.
lambdaT;
290 const Kokkos::View<const float*, CpMem> restBank = cur.
restBank, restVPeak = cur.
restVPeak;
291 const Kokkos::View<unsigned long long*, CpMem> prevKeys = prev.
keys;
292 const Kokkos::View<float*, CpMem> prevLambda = prev.
lambda;
293 const Kokkos::View<float* [3], CpMem> prevLambdaT = prev.
lambdaT;
294 const Kokkos::View<float*, CpMem> prevRestBank = prev.
restBank, prevRestVPeak = prev.
restVPeak;
295 if (numManifolds <= 0)
298 const int n = numManifolds;
299 const auto rng = Kokkos::pair<int, int>(0, n);
300 auto kd = Kokkos::subview(prevKeys, rng);
301 Kokkos::deep_copy(space, kd, Kokkos::subview(keys, rng));
302 Kokkos::parallel_for(
303 "peclet::dem::commit_iota", Kokkos::RangePolicy<CpExec>(space, 0, n),
304 KOKKOS_LAMBDA(
int i) { perm(i) = i; });
306 auto pd = Kokkos::subview(perm, rng);
307 Kokkos::Experimental::sort_by_key(space, kd, pd);
309 Kokkos::View<float*, CpMem> pl = prevLambda;
310 Kokkos::View<float* [3], CpMem> plt = prevLambdaT;
311 Kokkos::View<float*, CpMem> prb = prevRestBank;
312 Kokkos::View<float*, CpMem> prv = prevRestVPeak;
313 const bool carryColor = color.extent(0) > 0 && prevColor.extent(0) > 0;
314 Kokkos::View<const int*, CpMem> col = color;
315 Kokkos::View<int*, CpMem> pcol = prevColor;
316 Kokkos::parallel_for(
317 "peclet::dem::commit_gather", Kokkos::RangePolicy<CpExec>(space, 0, n), KOKKOS_LAMBDA(
int i) {
318 const int j = perm(i);
320 plt(i, 0) = lambdaT(j, 0);
321 plt(i, 1) = lambdaT(j, 1);
322 plt(i, 2) = lambdaT(j, 2);
323 prb(i) = restBank(j);
324 prv(i) = restVPeak(j);
333 Kokkos::View<unsigned long long*, CpMem> prevKeys,
int numManifolds) {
334 if (numManifolds <= 0)
337 auto src = Kokkos::subview(keys, Kokkos::pair<int, int>(0, numManifolds));
338 auto dst = Kokkos::subview(prevKeys, Kokkos::pair<int, int>(0, numManifolds));
339 Kokkos::deep_copy(space, dst, src);
340 Kokkos::sort(space, dst);
350 Kokkos::View<const int*, CpMem> realIdx,
351 Kokkos::View<
const float* [3],
CpMem> posPred,
F3 gHat,
352 Kokkos::View<unsigned char*, CpMem> grounded,
int numReal,
353 int sweeps,
int decay) {
355 Kokkos::parallel_for(
356 "peclet::dem::grounded_decay", Kokkos::RangePolicy<CpExec>(space, 0, numReal),
357 KOKKOS_LAMBDA(
int i) {
358 const int g =
static_cast<int>(grounded(i)) - decay;
359 grounded(i) =
static_cast<unsigned char>(g > 0 ? g : 0);
361 for (
int s = 0; s < sweeps; ++s) {
362 Kokkos::parallel_for(
363 "peclet::dem::grounded_sweep", Kokkos::RangePolicy<CpExec>(space, 0, numManifolds),
364 KOKKOS_LAMBDA(
int idx) {
368 const int realA = realIdx(m.
bodyA);
370 Kokkos::atomic_max(&grounded(realA),
static_cast<unsigned char>(255));
373 const int realB = realIdx(m.
bodyB);
375 const float up = -(dx.
x * gHat.
x + dx.
y * gHat.
y + dx.
z * gHat.
z);
376 const float thr = 0.3f * Kokkos::sqrt(
dot3(dx, dx));
378 const int lvl =
static_cast<int>(grounded(realB)) - 1;
380 Kokkos::atomic_max(&grounded(realA),
static_cast<unsigned char>(lvl));
381 }
else if (up < -thr) {
382 const int lvl =
static_cast<int>(grounded(realA)) - 1;
384 Kokkos::atomic_max(&grounded(realB),
static_cast<unsigned char>(lvl));
399 Kokkos::View<const int*, CpMem> realIdx,
400 Kokkos::View<
const float* [3],
CpMem> posPred,
F3 gHat,
401 Kokkos::View<int*, CpMem> heights,
int numReal) {
403 Kokkos::parallel_for(
404 "peclet::dem::height_init", Kokkos::RangePolicy<CpExec>(space, 0, numReal),
405 KOKKOS_LAMBDA(
int i) { heights(i) =
kLevelInf; });
406 const int maxSweeps = 1024;
407 for (
int s = 0; s < maxSweeps; ++s) {
409 Kokkos::parallel_reduce(
410 "peclet::dem::height_sweep", Kokkos::RangePolicy<CpExec>(space, 0, numManifolds),
411 KOKKOS_LAMBDA(
int idx,
int& acc) {
415 const int realA = realIdx(m.
bodyA);
417 if (Kokkos::atomic_fetch_min(&heights(realA), 0) > 0)
421 const int realB = realIdx(m.
bodyB);
423 const float up = -(dx.
x * gHat.
x + dx.
y * gHat.
y + dx.
z * gHat.
z);
424 const float thr = 0.3f * Kokkos::sqrt(
dot3(dx, dx));
426 const int cand = heights(realB);
427 if (cand <
kLevelInf && Kokkos::atomic_fetch_min(&heights(realA), cand + 1) > cand + 1)
429 }
else if (up < -thr) {
430 const int cand = heights(realA);
431 if (cand <
kLevelInf && Kokkos::atomic_fetch_min(&heights(realB), cand + 1) > cand + 1)
450 Kokkos::View<int*, CpMem> perm,
451 Kokkos::View<int*, CpMem> cursor,
452 std::vector<int>& offs) {
453 offs.assign(
static_cast<std::size_t
>(numColors) + 1, 0);
454 if (n <= 0 || numColors <= 0)
457 auto cnt = Kokkos::subview(cursor, Kokkos::pair<int, int>(0, numColors));
458 Kokkos::deep_copy(space, cnt, 0);
459 Kokkos::parallel_for(
460 "peclet::dem::bucket_hist", Kokkos::RangePolicy<CpExec>(space, 0, n), KOKKOS_LAMBDA(
int i) {
461 const int c = colorOf(i);
463 Kokkos::atomic_add(&cursor(c), 1);
465 auto hCnt = Kokkos::create_mirror_view(cnt);
466 Kokkos::deep_copy(space, hCnt, cnt);
468 for (
int c = 0; c < numColors; ++c)
469 offs[
static_cast<std::size_t
>(c) + 1] = offs[
static_cast<std::size_t
>(c)] + hCnt(c);
470 for (
int c = 0; c < numColors; ++c)
471 hCnt(c) = offs[
static_cast<std::size_t
>(c)];
472 Kokkos::deep_copy(space, cnt, hCnt);
473 Kokkos::parallel_for(
474 "peclet::dem::bucket_scatter", Kokkos::RangePolicy<CpExec>(space, 0, n),
475 KOKKOS_LAMBDA(
int i) {
476 const int c = colorOf(i);
478 perm(Kokkos::atomic_fetch_add(&cursor(c), 1)) = i;
485KOKKOS_INLINE_FUNCTION
long long colorKey(
int idx) {
486 unsigned int z =
static_cast<unsigned>(idx) + 0x9e3779b9u;
487 z = (z ^ (z >> 16)) * 0x21f0aaadu;
488 z = (z ^ (z >> 15)) * 0x735a2d97u;
491 return (
static_cast<long long>(z & 0x7fffffffu) << 32) |
static_cast<unsigned>(idx);
498 return (
static_cast<std::uint64_t
>(
static_cast<unsigned>(idA)) << 32) | 0xFFFFFFFFu;
499 const unsigned u = (idA < idB) ? idA : idB;
500 const unsigned v = (idA < idB) ? idB : idA;
501 return (
static_cast<std::uint64_t
>(u) << 32) | v;
523 Kokkos::View<const int*, CpMem>
a,
b;
524 Kokkos::View<const unsigned char*, CpMem>
split;
526 KOKKOS_INLINE_FUNCTION
int slotA(
int e,
int def)
const {
527 if (
a.extent(0) > 0) {
534 KOKKOS_INLINE_FUNCTION
int slotB(
int e,
int def)
const {
535 if (
b.extent(0) > 0) {
543 KOKKOS_INLINE_FUNCTION
bool relax(
int sA,
int sB)
const {
544 return split.extent(0) > 0 && (
split(sA) != 0 || (sB >= 0 &&
split(sB) != 0));
578template <
class EdgeAccessor>
579int colorEdges(
const EdgeAccessor& edges,
int numEdges,
int numVertices,
int vertexSpan,
580 Kokkos::View<int*, CpMem> color,
const ColorScratch& scratch,
int& leftover) {
581 const Kokkos::View<long long*, CpMem> bodyWinner = scratch.
bodyWinner;
582 const Kokkos::View<std::uint64_t*, CpMem> bodyMask = scratch.
bodyColorMask;
583 constexpr ColorLabels labels = EdgeAccessor::labels();
586 if (numEdges <= 0 || numVertices <= 0)
588 const int nV = vertexSpan > 0 ? vertexSpan : numVertices;
589 Kokkos::parallel_for(
590 labels.
initBodies, Kokkos::RangePolicy<CpExec>(space, 0, nV),
591 KOKKOS_LAMBDA(
int i) { bodyMask(i) = 0; });
592 Kokkos::parallel_for(
593 labels.
initEdges, Kokkos::RangePolicy<CpExec>(space, 0, numEdges),
594 KOKKOS_LAMBDA(
int idx) { color(idx) = edges.initialColor(idx); });
596 int remaining = 1, prevRemaining = -1;
601 const int maxRounds = numEdges + 2;
602 for (
int round = 0; round < maxRounds && remaining > 0; ++round) {
603 Kokkos::parallel_for(
604 labels.
resetWinner, Kokkos::RangePolicy<CpExec>(space, 0, nV),
605 KOKKOS_LAMBDA(
int i) { bodyWinner(i) = -1; });
606 Kokkos::parallel_for(
607 labels.
contend, Kokkos::RangePolicy<CpExec>(space, 0, numEdges), KOKKOS_LAMBDA(
int idx) {
608 if (color(idx) != -1)
611 Kokkos::atomic_max(&bodyWinner(e.
a), e.
key);
613 Kokkos::atomic_max(&bodyWinner(e.
b), e.
key);
616 Kokkos::parallel_reduce(
617 labels.
commit, Kokkos::RangePolicy<CpExec>(space, 0, numEdges),
618 KOKKOS_LAMBDA(
int idx,
int& acc) {
619 if (color(idx) != -1)
623 if (bodyWinner(e.
a) != e.
key || (e.
b >= 0 && bodyWinner(e.
b) != e.
key)) {
627 std::uint64_t forbidden = bodyMask(e.
a);
629 forbidden |= bodyMask(e.
b);
633#if defined(PECLET_DEM_TEST_MUTANT) && PECLET_DEM_TEST_MUTANT == 3
642 const std::uint64_t bit = std::uint64_t(1) << c;
643 bodyMask(e.
a) |= bit;
645 bodyMask(e.
b) |= bit;
651 if (rem == prevRemaining)
658 Kokkos::parallel_reduce(
659 labels.
max, Kokkos::RangePolicy<CpExec>(space, 0, numEdges),
660 KOKKOS_LAMBDA(
int idx,
int& mx) {
664 Kokkos::Max<int>(maxc));
665 Kokkos::parallel_reduce(
666 labels.
leftover, Kokkos::RangePolicy<CpExec>(space, 0, numEdges),
667 KOKKOS_LAMBDA(
int idx,
int& acc) {
678KOKKOS_INLINE_FUNCTION
void decodeKey(std::uint64_t key,
int& bodyA,
int& bodyB) {
679 const unsigned v =
static_cast<unsigned>(key & 0xFFFFFFFFu);
680 bodyA =
static_cast<int>((key >> 32) & 0x7FFFFFFFu);
681 bodyB = (v == 0xFFFFFFFFu) ? -1 :
static_cast<int>(v);
694 const bool flip = (idB >= 0 && idB < idA);
698 F4 n_aligned = flip ? n_vec :
F4{-n_vec.
x, -n_vec.
y, -n_vec.
z, 0.0f};
702 const F4 rA_mid{c.
rA.
x - shift.
x, c.
rA.
y - shift.
y, c.
rA.
z - shift.
z, 0.0f};
703 const F4 rB_mid{c.
rB.
x + shift.
x, c.
rB.
y + shift.
y, c.
rB.
z + shift.
z, 0.0f};
705 const F4 r_can = flip ? rB_mid : rA_mid;
706 const F4 r_other = flip ? rA_mid : rB_mid;
708 const F4 tau_can =
cross3(r_can, n_aligned);
709 const F4 tau_other =
cross3(r_other,
F4{-n_aligned.
x, -n_aligned.
y, -n_aligned.
z, 0.0f});
715 m.
rB_sum =
F4{r_other.
x, r_other.
y, r_other.
z, 0.0f};
734 Kokkos::View<int*, CpMem> before(
735 Kokkos::view_alloc(space,
"peclet::dem::cp::partBefore", Kokkos::WithoutInitializing), n);
736 Kokkos::View<ContactC*, CpMem> ct = contacts;
738 Kokkos::parallel_scan(
739 "peclet::dem::cp::partScan", Kokkos::RangePolicy<CpExec>(space, 0, n),
740 KOKKOS_LAMBDA(
int i,
int& run,
const bool final) {
741 const int f = owns(ct(i)) ? 1 : 0;
747 Kokkos::View<ContactC*, CpMem> part(
748 Kokkos::view_alloc(space,
"peclet::dem::cp::part", Kokkos::WithoutInitializing), n);
749 const int no = ncOwned;
750 Kokkos::parallel_for(
751 "peclet::dem::cp::partScatter", Kokkos::RangePolicy<CpExec>(space, 0, n),
752 KOKKOS_LAMBDA(
int i) {
753 const bool f = owns(ct(i));
754 part(f ? before(i) : no + (i - before(i))) = ct(i);
756 Kokkos::deep_copy(space,
757 Kokkos::subview(contacts, std::pair<std::size_t, std::size_t>(
758 0,
static_cast<std::size_t
>(n))),
773 Kokkos::View<ManifoldC*, CpMem> outManifolds,
774 Kokkos::View<int, CpMem> outCount,
775 Kokkos::View<int*, CpMem> contactSlot = {},
776 int numOwnedContacts = -1,
int* numOwnedManifolds =
nullptr) {
779 Kokkos::deep_copy(space, outCount, 0);
780 if (numOwnedManifolds)
781 *numOwnedManifolds = 0;
786 Kokkos::View<std::uint64_t*, CpMem> keys(
787 Kokkos::view_alloc(space,
"peclet::dem::cp::keys", Kokkos::WithoutInitializing), n);
788 Kokkos::View<int*, CpMem> perm(
789 Kokkos::view_alloc(space,
"peclet::dem::cp::perm", Kokkos::WithoutInitializing), n);
790 const int nOwnedC = numOwnedContacts;
791 Kokkos::parallel_for(
792 "peclet::dem::cp::key", Kokkos::RangePolicy<CpExec>(space, 0, n), KOKKOS_LAMBDA(
int i) {
793 keys(i) =
pairKey(contacts(i)) |
799 Kokkos::Experimental::sort_by_key(space, keys, perm);
802 Kokkos::View<int*, CpMem> segId(
803 Kokkos::view_alloc(space,
"peclet::dem::cp::segId", Kokkos::WithoutInitializing), n);
805 Kokkos::parallel_scan(
806 "peclet::dem::cp::segscan", Kokkos::RangePolicy<CpExec>(space, 0, n),
807 KOKKOS_LAMBDA(
int p,
int& run,
const bool final) {
808 const bool isNew = (p == 0) || (keys(p) != keys(p - 1));
815 if (numOwnedManifolds) {
816 Kokkos::View<std::uint64_t*, CpMem> kk = keys;
818 Kokkos::parallel_reduce(
819 "peclet::dem::cp::ownedSegs", Kokkos::RangePolicy<CpExec>(space, 0, n),
820 KOKKOS_LAMBDA(
int p,
int& cnt) {
821 const bool leader = (p == 0) || (kk(p) != kk(p - 1));
826 *numOwnedManifolds = nOwnedM;
830 Kokkos::View<std::uint64_t*, CpMem> k = keys;
831 Kokkos::View<int*, CpMem> sid = segId;
832 Kokkos::View<ManifoldC*, CpMem> out = outManifolds;
833 Kokkos::parallel_for(
834 "peclet::dem::cp::init", Kokkos::RangePolicy<CpExec>(space, 0, n), KOKKOS_LAMBDA(
int p) {
835 const bool leader = (p == 0) || (k(p) != k(p - 1));
844 Kokkos::View<const ContactC*, CpMem> ct = contacts;
845 Kokkos::View<int*, CpMem> pm = perm;
846 Kokkos::parallel_for(
847 "peclet::dem::cp::accum", Kokkos::RangePolicy<CpExec>(space, 0, n), KOKKOS_LAMBDA(
int p) {
849 const int s = sid(p);
850 Kokkos::atomic_add(&out(s).num_points, m.num_points);
851 Kokkos::atomic_add(&out(s).normal_sum.x, m.normal_sum.x);
852 Kokkos::atomic_add(&out(s).normal_sum.y, m.normal_sum.y);
853 Kokkos::atomic_add(&out(s).normal_sum.z, m.normal_sum.z);
854 Kokkos::atomic_add(&out(s).torque_armA_sum.x, m.torque_armA_sum.x);
855 Kokkos::atomic_add(&out(s).torque_armA_sum.y, m.torque_armA_sum.y);
856 Kokkos::atomic_add(&out(s).torque_armA_sum.z, m.torque_armA_sum.z);
857 Kokkos::atomic_add(&out(s).torque_armB_sum.x, m.torque_armB_sum.x);
858 Kokkos::atomic_add(&out(s).torque_armB_sum.y, m.torque_armB_sum.y);
859 Kokkos::atomic_add(&out(s).torque_armB_sum.z, m.torque_armB_sum.z);
860 Kokkos::atomic_add(&out(s).rA_sum.x, m.rA_sum.x);
861 Kokkos::atomic_add(&out(s).rA_sum.y, m.rA_sum.y);
862 Kokkos::atomic_add(&out(s).rA_sum.z, m.rA_sum.z);
863 Kokkos::atomic_add(&out(s).rB_sum.x, m.rB_sum.x);
864 Kokkos::atomic_add(&out(s).rB_sum.y, m.rB_sum.y);
865 Kokkos::atomic_add(&out(s).rB_sum.z, m.rB_sum.z);
866 Kokkos::atomic_add(&out(s).wallVel_sum.x, m.wallVel_sum.x);
867 Kokkos::atomic_add(&out(s).wallVel_sum.y, m.wallVel_sum.y);
868 Kokkos::atomic_add(&out(s).wallVel_sum.z, m.wallVel_sum.z);
869 Kokkos::atomic_add(&out(s).restitution_sum, m.restitution_sum);
870 Kokkos::atomic_add(&out(s).friction_sum, m.friction_sum);
873 if (contactSlot.extent(0) >= (
size_t)n) {
874 Kokkos::View<int*, CpMem> cs = contactSlot;
875 Kokkos::parallel_for(
876 "peclet::dem::cp::slotmap", Kokkos::RangePolicy<CpExec>(space, 0, n),
877 KOKKOS_LAMBDA(
int p) { cs(pm(p)) = sid(p); });
881 Kokkos::deep_copy(space, outCount, numSeg);
891 return (
static_cast<std::uint64_t
>(
static_cast<unsigned>(c.
bodyA)) << 32) |
892 static_cast<unsigned>(c.
bodyB);
902 Kokkos::View<const int*, CpMem>
start;
903 Kokkos::View<const int*, CpMem>
list;
904 KOKKOS_INLINE_FUNCTION
bool on()
const {
return start.extent(0) > 0; }
905 KOKKOS_INLINE_FUNCTION
int begin(
int u)
const {
return on() ?
start(u) : u; }
906 KOKKOS_INLINE_FUNCTION
int end(
int u)
const {
return on() ?
start(u + 1) : u + 1; }
907 KOKKOS_INLINE_FUNCTION
int contact(
int k)
const {
return on() ?
list(k) : k; }
921 Kokkos::View<int*, CpMem> unitStart,
922 Kokkos::View<int*, CpMem> unitContacts,
int numManifolds = -1) {
925 Kokkos::deep_copy(space, Kokkos::subview(unitStart, 0), 0);
929 auto writeIdentity = [&]() {
930 Kokkos::View<int*, CpMem> us = unitStart;
931 Kokkos::View<int*, CpMem> uc = unitContacts;
932 Kokkos::parallel_for(
933 "peclet::dem::cp::uident", Kokkos::RangePolicy<CpExec>(space, 0, n), KOKKOS_LAMBDA(
int p) {
937 Kokkos::deep_copy(space, Kokkos::subview(unitStart, n), n);
941 if (numManifolds == n)
942 return writeIdentity();
943 using Kokkos::view_alloc;
944 using Kokkos::WithoutInitializing;
945 Kokkos::View<std::uint64_t*, CpMem> keys(
946 view_alloc(space,
"peclet::dem::cp::ukeys", WithoutInitializing), n);
947 Kokkos::View<int*, CpMem> perm(view_alloc(space,
"peclet::dem::cp::uperm", WithoutInitializing),
949 Kokkos::parallel_for(
950 "peclet::dem::cp::ukey", Kokkos::RangePolicy<CpExec>(space, 0, n), KOKKOS_LAMBDA(
int i) {
951 keys(i) =
unitKey(contacts(i));
954 Kokkos::Experimental::sort_by_key(space, keys, perm);
955 Kokkos::View<int*, CpMem> segId(view_alloc(space,
"peclet::dem::cp::useg", WithoutInitializing),
958 Kokkos::parallel_scan(
959 "peclet::dem::cp::usegscan", Kokkos::RangePolicy<CpExec>(space, 0, n),
960 KOKKOS_LAMBDA(
int p,
int& run,
const bool final) {
961 if (p == 0 || keys(p) != keys(p - 1))
971 return writeIdentity();
973 Kokkos::View<int*, CpMem> segLead(
974 view_alloc(space,
"peclet::dem::cp::ulead", WithoutInitializing), numSeg);
975 Kokkos::deep_copy(space, segLead, n);
976 Kokkos::parallel_for(
977 "peclet::dem::cp::uleadmin", Kokkos::RangePolicy<CpExec>(space, 0, n),
978 KOKKOS_LAMBDA(
int p) { Kokkos::atomic_min(&segLead(segId(p)), perm(p)); });
982 Kokkos::parallel_for(
983 "peclet::dem::cp::ukey2", Kokkos::RangePolicy<CpExec>(space, 0, n), KOKKOS_LAMBDA(
int p) {
984 const int i = perm(p);
985 keys(p) = (
static_cast<std::uint64_t
>(
static_cast<unsigned>(segLead(segId(p)))) << 32) |
986 static_cast<unsigned>(i);
988 Kokkos::Experimental::sort_by_key(space, keys, perm);
989 Kokkos::View<int*, CpMem> us = unitStart;
990 Kokkos::View<int*, CpMem> uc = unitContacts;
992 Kokkos::parallel_scan(
993 "peclet::dem::cp::ustart", Kokkos::RangePolicy<CpExec>(space, 0, n),
994 KOKKOS_LAMBDA(
int p,
int& run,
const bool final) {
995 const bool isNew = (p == 0) || ((keys(p) >> 32) != (keys(p - 1) >> 32));
1005 Kokkos::deep_copy(space, Kokkos::subview(unitStart, nu), n);
1015 Kokkos::View<const int*, CpMem> contactSlot,
1016 Kokkos::View<const ManifoldC*, CpMem> manifolds,
1017 Kokkos::View<const float*, CpMem> lambdaAcc) {
1019 Kokkos::parallel_for(
1020 "peclet::dem::cp::pgs_friction_bound", Kokkos::RangePolicy<CpExec>(space, 0, numContacts),
1021 KOKKOS_LAMBDA(
int i) {
1022 const int s = contactSlot(i);
1025 contacts(i).friction_lambda_n = lambdaAcc(s) /
static_cast<float>(np);
1036inline void commitPosImpulse(Kokkos::View<const float*, CpMem> posLambdaContact,
int numContacts,
1037 Kokkos::View<const int*, CpMem> contactSlot,
1038 Kokkos::View<const ManifoldC*, CpMem> manifolds,
int numManifolds,
1039 Kokkos::View<const unsigned long long*, CpMem> keys,
1040 Kokkos::View<const unsigned long long*, CpMem> prevKeysSorted,
1041 int prevCount,
float dt, Kokkos::View<float*, CpMem> scratchManifold,
1042 Kokkos::View<float*, CpMem> prevPosImpulse) {
1044 auto sm = Kokkos::subview(scratchManifold, Kokkos::pair<int, int>(0, numManifolds));
1045 Kokkos::deep_copy(space, sm, 0.0f);
1046 Kokkos::parallel_for(
1047 "peclet::dem::pos_load_reduce", Kokkos::RangePolicy<CpExec>(space, 0, numContacts),
1048 KOKKOS_LAMBDA(
int i) {
1049 const float l = posLambdaContact(i);
1051 Kokkos::atomic_add(&scratchManifold(contactSlot(i)), l / dt);
1053 Kokkos::parallel_for(
1054 "peclet::dem::pos_load_scatter", Kokkos::RangePolicy<CpExec>(space, 0, numManifolds),
1055 KOKKOS_LAMBDA(
int idx) {
1056 const unsigned long long k = keys(idx);
1059 int lo = 0, hi = prevCount;
1061 const int m = (lo + hi) >> 1;
1062 if (prevKeysSorted(m) < k)
1067 if (lo < prevCount && prevKeysSorted(lo) == k)
1068 prevPosImpulse(lo) = scratchManifold(idx);
dem — portable POD types + math + analytic SDFs shared by the Kokkos kernel ports.
std::uint64_t pairKey(const ContactC &c)
Canonical pair key: (min<<32)|max, or (idA<<32)|0xFFFFFFFF for a boundary (idB<0) contact.
int colorEdges(const EdgeAccessor &edges, int numEdges, int numVertices, int vertexSpan, Kokkos::View< int *, CpMem > color, const ColorScratch &scratch, int &leftover)
Greedy graph colouring of an edge set: no two edges sharing a colouring vertex get the same colour.
void commitPosImpulse(Kokkos::View< const float *, CpMem > posLambdaContact, int numContacts, Kokkos::View< const int *, CpMem > contactSlot, Kokkos::View< const ManifoldC *, CpMem > manifolds, int numManifolds, Kokkos::View< const unsigned long long *, CpMem > keys, Kokkos::View< const unsigned long long *, CpMem > prevKeysSorted, int prevCount, float dt, Kokkos::View< float *, CpMem > scratchManifold, Kokkos::View< float *, CpMem > prevPosImpulse)
After the position solve: convert the per-contact positional lambdas into an impulse- equivalent per ...
ManifoldC transformContact(const ContactC &c)
Per-contact transform to a single-point manifold, aligned to the canonical pair.
std::uint64_t unitKey(const ContactC &c)
Position-unit key (docs/contact_solve_framework.md §4.3, §12 S6): the canonical pair key for a body-b...
constexpr int wallContactId(int wallIndex)
Wall identity of a boundary contact (docs/contact_solve_framework.md §12 S6): a contact with a wall c...
CpExec::memory_space CpMem
void decodeKey(std::uint64_t key, int &bodyA, int &bodyB)
Decode the canonical (bodyA, bodyB) from a pair key (bodyB = -1 for boundary).
void buildColorBuckets(Kokkos::View< const int *, CpMem > colorOf, int n, int numColors, Kokkos::View< int *, CpMem > perm, Kokkos::View< int *, CpMem > cursor, std::vector< int > &offs)
Dense colour buckets (numColors <= 64): perm[offs[c] .
void frictionBoundFromLambda(Kokkos::View< ContactC *, CpMem > contacts, int numContacts, Kokkos::View< const int *, CpMem > contactSlot, Kokkos::View< const ManifoldC *, CpMem > manifolds, Kokkos::View< const float *, CpMem > lambdaAcc)
PGS friction bound: overwrite each contact's friction_lambda_n with its manifold's converged PGS push...
constexpr std::uint64_t kNonOwnedPairBit
Distributed step: bit 63 of a manifold-reduction key marks a contact this rank sees but does not own ...
F3 ldF3(const V &v, int i)
constexpr int wallIndexOf(int bodyB)
int reduceContactsToManifolds(Kokkos::View< const ContactC *, CpMem > contacts, int n, Kokkos::View< ManifoldC *, CpMem > outManifolds, Kokkos::View< int, CpMem > outCount, Kokkos::View< int *, CpMem > contactSlot={}, int numOwnedContacts=-1, int *numOwnedManifolds=nullptr)
Reduce n contacts to manifolds (one per unique canonical pair).
unsigned long long pairKeyFromGids(unsigned a, unsigned b)
The one 64-bit pair identity every gid-keyed ledger uses (the XPBD persistent-contact and warm-start ...
Kokkos::DefaultExecutionSpace CpExec
void commitPairKeysLambda(const PairLedger &cur, const PairLedger &prev, Kokkos::View< int *, CpMem > perm, int numManifolds, Kokkos::View< const int *, CpMem > color={}, Kokkos::View< int *, CpMem > prevColor={})
Save this substep's keys + converged impulses (normal AND tangential) and key-sort them for next subs...
int buildPositionUnits(Kokkos::View< const ContactC *, CpMem > contacts, int n, Kokkos::View< int *, CpMem > unitStart, Kokkos::View< int *, CpMem > unitContacts, int numManifolds=-1)
Build the position units of contacts [0, n) (docs/contact_solve_framework.md §4.3,...
int partitionContacts(Kokkos::View< ContactC *, CpMem > contacts, int n, const Owns &owns)
Stable partition of contacts [0, n) by owns(contact): the owned ones first, [0, ncOwned),...
void markPersistentManifolds(Kokkos::View< const ManifoldC *, CpMem > manifolds, int numManifolds, Kokkos::View< const int *, CpMem > realIdx, Kokkos::View< const int *, CpMem > keyIdx, Kokkos::View< const unsigned long long *, CpMem > prevKeys, int prevCount, Kokkos::View< unsigned long long *, CpMem > outKeys, Kokkos::View< unsigned char *, CpMem > outFlags)
keyIdx maps a body slot to the identity the pair key is built from: the REAL index map on the single-...
long long colorKey(int idx)
splitmix32 finalizer: a well-mixed pseudo-random priority per edge index.
void updateGroundedLevels(Kokkos::View< const ManifoldC *, CpMem > manifolds, int numManifolds, Kokkos::View< const int *, CpMem > realIdx, Kokkos::View< const float *[3], CpMem > posPred, F3 gHat, Kokkos::View< unsigned char *, CpMem > grounded, int numReal, int sweeps, int decay)
Guendelman support levels, warm-started: decay every body's level by decay, re-seed 255 at wall/plane...
constexpr int kLevelInf
Height-from-floor BFS levels for the level-ordered ("multilevel") stabilization pass: 0 at a wall/pla...
void gatherWarmLambda(Kokkos::View< const ManifoldC *, CpMem > manifolds, int numManifolds, Kokkos::View< const int *, CpMem > realIdx, Kokkos::View< const int *, CpMem > keyIdx, const PairLedger &prev, Kokkos::View< const float *, CpMem > prevPosImpulse, int prevCount, const PairLedger &out, Kokkos::View< float *, CpMem > outPosImpulse, Kokkos::View< unsigned char *, CpMem > outMatched={}, bool dedupTwins=true)
Warm-start gather for the PGS velocity solve: per manifold, write its pair key and look up the previo...
void commitPairKeys(Kokkos::View< const unsigned long long *, CpMem > keys, Kokkos::View< unsigned long long *, CpMem > prevKeys, int numManifolds)
Copy this substep's keys into prevKeys and sort them for next substep's binary search.
constexpr int kColorUncolourable
void computeHeightLevels(Kokkos::View< const ManifoldC *, CpMem > manifolds, int numManifolds, Kokkos::View< const int *, CpMem > realIdx, Kokkos::View< const float *[3], CpMem > posPred, F3 gHat, Kokkos::View< int *, CpMem > heights, int numReal)
constexpr int kColorPalette
Colouring palette (docs/contact_solve_framework.md §4.2 item 1): the greedy takes the lowest colour f...
int lowerBoundKey(const KeysView &keys, int count, unsigned long long key)
lower_bound over a ledger's sorted keys: the first index whose key is >= key (count when none).
bool normalOfA(const ContactC &c)
Which body's surface a contact's normal belongs to.
unsigned long long pairKeyOf(const ManifoldC &m, Kokkos::View< const int *, CpMem > realIdx)
Persistent-contact detection for the gravity-gated restitution rule.
The solve's per-body accumulators: position/orientation and velocity corrections and the per-body con...
Kokkos::View< float *[4], CpMem > deltaQuat
Kokkos::View< float *[3], CpMem > deltaVel
Kokkos::View< float *[3], CpMem > deltaPos
Kokkos::View< float *[3], CpMem > deltaAngVel
Kokkos::View< int *, CpMem > constraintCounts
The substep's predicted body state (Particles::bodyPred()).
Kokkos::View< float *[3], CpMem > velPred
Kokkos::View< float *[3], CpMem > angVelPred
Kokkos::View< float *[3], CpMem > posPred
Kokkos::View< float *[4], CpMem > quatPred
A body's state, one row per particle slot (Particles::bodyState()).
Kokkos::View< float *[3], CpMem > pos
Kokkos::View< float *[3], CpMem > invInertia
Kokkos::View< float *, CpMem > scale
Kokkos::View< float *, CpMem > invMass
Kokkos::View< int *, CpMem > shapeId
Kokkos::View< float *[3], CpMem > angVel
Kokkos::View< float *[4], CpMem > quat
Kokkos::View< float *[3], CpMem > vel
The previous substep's colour ledger for an incremental colouring: the key-sorted pair keys,...
Kokkos::View< const unsigned long long *, CpMem > keys
Kokkos::View< const int *, CpMem > color
One colouring edge as the arbitration sees it: the colouring vertices of its A / B ends (b = -1 for a...
Kernel labels of one colorEdges instantiation (each caller keeps its own profiling names).
Per-vertex scratch of the Jones-Plassmann colourings: the round's winning edge key and the committed-...
Kokkos::View< long long *, CpMem > bodyWinner
Kokkos::View< std::uint64_t *, CpMem > bodyColorMask
Portable mirror of ManifoldConstraint.
The pair-keyed warm-start ledger: pair key, converged normal and tangential impulses,...
Kokkos::View< float *[3], CpMem > lambdaT
Kokkos::View< float *, CpMem > restVPeak
Kokkos::View< float *, CpMem > restBank
Kokkos::View< unsigned long long *, CpMem > keys
Kokkos::View< float *, CpMem > lambda
The position units of a contact list as a CSR (§4.3): unit u owns the contacts list(start(u)) .
Kokkos::View< const int *, CpMem > start
Kokkos::View< const int *, CpMem > list
Island-sleeping state per real body: asleep flag, low-motion counter, moving-wall flag,...
Kokkos::View< unsigned char *, CpMem > asleep
Kokkos::View< int *, CpMem > sleepPrevCount
Kokkos::View< int *, CpMem > sleepCurCount
Kokkos::View< unsigned char *, CpMem > sleepMovingWall
Kokkos::View< unsigned char *, CpMem > sleepCounter
Per-edge slot overrides of a phase's hub copies (docs/contact_solve_framework.md §4....
Kokkos::View< const unsigned char *, CpMem > split
int slotA(int e, int def) const
bool relax(int sA, int sB) const
True iff the edge touches a mass-split slot (its step is scaled by omega before the clamp).
int slotB(int e, int def) const
Kokkos::View< const int *, CpMem > a
Kokkos::View< const int *, CpMem > b