10#ifndef DEM_SOLVER_POSITION_HPP
11#define DEM_SOLVER_POSITION_HPP
13#include <Kokkos_Core.hpp>
25 return invM + rn.
x * rn.
x * invI.
x + rn.
y * rn.
y * invI.
y + rn.
z * rn.
z * invI.
z;
34 Kokkos::View<int*, CpMem> counts) {
37 "peclet::dem::count_position_jacobi", Kokkos::RangePolicy<CpExec>(space, 0, numContacts),
38 KOKKOS_LAMBDA(
int idx) {
40 Kokkos::atomic_add(&counts(c.
bodyA), 1);
42 Kokkos::atomic_add(&counts(c.
bodyB), 1);
59inline void solvePosition(Kokkos::View<const ContactC*, CpMem> contacts,
int numContacts,
61 Kokkos::View<float, CpMem> maxOverlap,
62 Kokkos::View<const int*, CpMem> onlyColor = {},
int colorFilter = 0,
63 bool massSplit =
false) {
64 const Kokkos::View<const float*, CpMem> invMass = bodies.
invMass;
65 const Kokkos::View<const float* [3], CpMem> posPred = pred.
posPred;
66 const Kokkos::View<const float* [4], CpMem> quatPred = pred.
quatPred;
67 const Kokkos::View<const float* [4], CpMem> quatStatic = bodies.
quat;
68 const Kokkos::View<float* [3], CpMem> deltaPos = delta.
deltaPos;
71 const bool filt = onlyColor.extent(0) > 0;
73 "peclet::dem::solve_position", Kokkos::RangePolicy<CpExec>(space, 0, numContacts),
74 KOKKOS_LAMBDA(
int idx) {
75 if (filt && onlyColor(idx) != colorFilter)
77 const ContactC c = contacts(idx);
78 const int idA = c.bodyA, idB = c.bodyB;
79 const float invMassA = invMass(idA);
80 const float invMassB = (idB >= 0) ? invMass(idB) : 0.0f;
82 const F3 pA =
ldF3(posPred, idA);
83 const F4 qA =
ldF4(quatPred, idA);
87 pB =
ldF3(posPred, idB);
88 qB =
ldF4(quatPred, idB);
94 F3 rB{c.rB.x, c.rB.y, c.rB.z};
95 F3 n{c.normal.x, c.normal.y, c.normal.z};
104 n =
F3{c.normal.x, c.normal.y, c.normal.z};
105 const F3 pAsurf =
add3(pA, rA);
106 C =
dot3(
sub3(pAsurf,
F3{c.rB.x, c.rB.y, c.rB.z}), n);
108 const F3 pAc =
add3(pA, rA);
109 const F3 pBc =
add3(pB, rB);
118 const float wA = invMassA;
119 const float wB = invMassB;
120 const float wTotal = wA + wB;
125 massSplit ?
static_cast<float>(constraintCounts(idA)) * wA +
126 ((idB >= 0) ?
static_cast<float>(constraintCounts(idB)) * wB : 0.0f)
129 const float dLambda = -C / wSolve;
132 Kokkos::atomic_add(&deltaPos(idA, 0), n.x * dLambda * invMassA);
133 Kokkos::atomic_add(&deltaPos(idA, 1), n.y * dLambda * invMassA);
134 Kokkos::atomic_add(&deltaPos(idA, 2), n.z * dLambda * invMassA);
136 Kokkos::atomic_add(&deltaPos(idB, 0), -n.x * dLambda * invMassB);
137 Kokkos::atomic_add(&deltaPos(idB, 1), -n.y * dLambda * invMassB);
138 Kokkos::atomic_add(&deltaPos(idB, 2), -n.z * dLambda * invMassB);
140 Kokkos::atomic_add(&constraintCounts(idB), 1);
143 Kokkos::atomic_add(&constraintCounts(idA), 1);
146 Kokkos::atomic_max(&maxOverlap(), -C);
167 const Kokkos::View<const unsigned char*, CpMem>& sleepMask,
169 for (
int k = units.
begin(u); k < units.
end(u); ++k)
170 if (!sleepMask(units.
contact(k)))
184 return {
"peclet::dem::pcolor_init_bodies",
"peclet::dem::pcolor_init_contacts",
185 "peclet::dem::pcolor_reset_winner",
"peclet::dem::pcolor_contend",
186 "peclet::dem::pcolor_commit",
"peclet::dem::pcolor_max",
187 "peclet::dem::pcolor_leftover"};
193 const int lead =
units.leader(idx);
195 const int ea =
ov.slotA(idx, c.
bodyA);
196 const int eb = (c.
bodyB >= 0) ?
ov.slotB(idx, c.
bodyB) : -1;
210inline int colorContacts(Kokkos::View<const ContactC*, CpMem> contacts,
int numContacts,
211 int numBodies, Kokkos::View<int*, CpMem> cColor,
213 Kokkos::View<const unsigned char*, CpMem> sleepMask = {},
214 PosUnits units = {}, SlotOverride ov = {},
int vertexSpan = 0) {
215 const UnitEdges edges{contacts, sleepMask, units, ov, sleepMask.extent(0) > 0};
216 return colorEdges(edges, numContacts, numBodies, vertexSpan, cColor, scratch, leftover);
230 Kokkos::View<int*, CpMem> cColor,
231 Kokkos::View<unsigned long long*, CpMem> keysOut,
232 const ColorScratch& scratch,
int& leftover,
bool forceFull,
233 Kokkos::View<const unsigned char*, CpMem> sleepMask = {},
234 PosUnits units = {}) {
235 const Kokkos::View<const unsigned long long*, CpMem> prevKeys = carried.
keys;
236 const Kokkos::View<const int*, CpMem> prevColor = carried.
color;
237 const int prevCount = carried.
count;
238 const Kokkos::View<long long*, CpMem> bodyWinner = scratch.
bodyWinner;
239 const Kokkos::View<std::uint64_t*, CpMem> bodyMask = scratch.
bodyColorMask;
242 if (numContacts <= 0 || numBodies <= 0)
244 const bool full0 = forceFull || prevCount <= 0;
245 const bool sleepOn = sleepMask.extent(0) > 0;
248 Kokkos::parallel_for(
249 "peclet::dem::pcolor_i_seed", Kokkos::RangePolicy<CpExec>(space, 0, numContacts),
250 KOKKOS_LAMBDA(
int idx) {
253 for (
int k2 = units.begin(idx); k2 < units.end(idx); ++k2) {
254 const int ci = units.contact(k2);
255 keysOut(ci) =
pairKey(contacts(ci));
257 const unsigned long long k = keysOut(units.leader(idx));
258 if (sleepOn &&
unitAsleep(units, sleepMask, idx)) {
264 int lo = 0, hi = prevCount;
266 const int mid = (lo + hi) >> 1;
267 if (prevKeys(mid) < k)
272 if (lo < prevCount && prevKeys(lo) == k) {
273 const int pc = prevColor(lo);
280 Kokkos::parallel_for(
281 "peclet::dem::pcolor_i_init_bodies", Kokkos::RangePolicy<CpExec>(space, 0, numBodies),
282 KOKKOS_LAMBDA(
int i) { bodyMask(i) = 0; });
292 Kokkos::parallel_for(
293 "peclet::dem::pcolor_i_mask", Kokkos::RangePolicy<CpExec>(space, 0, numContacts),
294 KOKKOS_LAMBDA(
int idx) {
295 const int col = cColor(idx);
298 const ContactC c = contacts(units.leader(idx));
299 const std::uint64_t bit = std::uint64_t(1) << col;
300 bool conflict = ((Kokkos::atomic_fetch_or(&bodyMask(c.bodyA), bit) >> col) & 1) != 0;
302 conflict |= ((Kokkos::atomic_fetch_or(&bodyMask(c.bodyB), bit) >> col) & 1) != 0;
309 int remaining = 1, prevRemaining = -1;
314 const int maxRounds = numContacts + 2;
315 for (
int round = 0; round < maxRounds && remaining > 0; ++round) {
316 Kokkos::parallel_for(
317 "peclet::dem::pcolor_i_reset_winner", Kokkos::RangePolicy<CpExec>(space, 0, numBodies),
318 KOKKOS_LAMBDA(
int i) { bodyWinner(i) = -1; });
319 Kokkos::parallel_for(
320 "peclet::dem::pcolor_i_contend", Kokkos::RangePolicy<CpExec>(space, 0, numContacts),
321 KOKKOS_LAMBDA(
int idx) {
322 if (cColor(idx) != -1)
324 const int lead = units.leader(idx);
326 const long long key =
colorKey(lead);
327 Kokkos::atomic_max(&bodyWinner(c.bodyA), key);
329 Kokkos::atomic_max(&bodyWinner(c.bodyB), key);
332 Kokkos::parallel_reduce(
333 "peclet::dem::pcolor_i_commit", Kokkos::RangePolicy<CpExec>(space, 0, numContacts),
334 KOKKOS_LAMBDA(
int idx,
int& acc) {
335 if (cColor(idx) != -1)
337 const int lead = units.leader(idx);
339 const int ea = c.bodyA;
340 const int eb = c.bodyB;
341 const long long key =
colorKey(lead);
342 if (bodyWinner(ea) != key || (eb >= 0 && bodyWinner(eb) != key)) {
346 std::uint64_t forbidden = bodyMask(ea);
348 forbidden |= bodyMask(eb);
352#if defined(PECLET_DEM_TEST_MUTANT) && PECLET_DEM_TEST_MUTANT == 3
361 const std::uint64_t bit = std::uint64_t(1) << col;
368 if (rem == prevRemaining)
374 Kokkos::parallel_reduce(
375 "peclet::dem::pcolor_i_max", Kokkos::RangePolicy<CpExec>(space, 0, numContacts),
376 KOKKOS_LAMBDA(
int idx,
int& mx) {
377 if (cColor(idx) > mx)
380 Kokkos::Max<int>(maxc));
381 Kokkos::parallel_reduce(
382 "peclet::dem::pcolor_i_leftover", Kokkos::RangePolicy<CpExec>(space, 0, numContacts),
383 KOKKOS_LAMBDA(
int idx,
int& acc) {
394 Kokkos::View<const int*, CpMem> unitColor,
395 Kokkos::View<int*, CpMem> contactColor) {
399 Kokkos::parallel_for(
400 "peclet::dem::pcolor_expand", Kokkos::RangePolicy<CpExec>(space, 0, numUnits),
401 KOKKOS_LAMBDA(
int u) {
402 const int col = unitColor(u);
403 for (
int k = units.
begin(u); k < units.
end(u); ++k)
404 contactColor(units.
contact(k)) = col;
410 Kokkos::View<const int*, CpMem> color,
411 Kokkos::View<unsigned long long*, CpMem> prevKeys,
412 Kokkos::View<int*, CpMem> prevColor, Kokkos::View<int*, CpMem> perm,
414 if (numContacts <= 0)
417 const int n = numContacts;
418 const auto rng = Kokkos::pair<int, int>(0, n);
419 auto kd = Kokkos::subview(prevKeys, rng);
420 Kokkos::deep_copy(space, kd, Kokkos::subview(keys, rng));
421 Kokkos::parallel_for(
422 "peclet::dem::pcolor_commit_iota", Kokkos::RangePolicy<CpExec>(space, 0, n),
423 KOKKOS_LAMBDA(
int i) { perm(i) = i; });
425 auto pd = Kokkos::subview(perm, rng);
426 Kokkos::Experimental::sort_by_key(space, kd, pd);
428 Kokkos::View<int*, CpMem> pc = prevColor;
429 Kokkos::View<const int*, CpMem> c = color;
430 Kokkos::parallel_for(
431 "peclet::dem::pcolor_commit_gather", Kokkos::RangePolicy<CpExec>(space, 0, n),
432 KOKKOS_LAMBDA(
int i) { pc(i) = c(perm(i)); });
467 for (
int k =
units.begin(u); k <
units.end(u); ++k)
474 if (
ov.a.extent(0) > 0) {
482 const float invMassA =
invMass(idA);
483 const float invMassB = (idB >= 0) ?
invMass(idB) : 0.0f;
507 const F3 pAsurf =
add3(pA, rA);
510 const F3 pAc =
add3(pA, rA);
511 const F3 pBc =
add3(pB, rB);
515 const float lam = accumulated ?
posLambdaAcc(idx) : 0.0f;
516 if (C >= 0.0f && lam <= 0.0f)
523 const float wTotal = invMassA + invMassB;
530#if defined(PECLET_DEM_TEST_MUTANT) && PECLET_DEM_TEST_MUTANT == 7
532 const float lamNew = Kokkos::fmax(lam, lam -
omega * C / wTotal);
534 const float lamNew = Kokkos::fmax(0.0f, lam -
omega * C / wTotal);
536 dLambda = lamNew - lam;
540 Kokkos::atomic_max(&
posResidual(), Kokkos::fabs(dLambda) * wTotal);
542 dLambda = -C / wTotal;
552 posPred(idA, 0) += n.
x * dLambda * invMassA;
553 posPred(idA, 1) += n.
y * dLambda * invMassA;
554 posPred(idA, 2) += n.
z * dLambda * invMassA;
556 posPred(idB, 0) += -n.
x * dLambda * invMassB;
557 posPred(idB, 1) += -n.
y * dLambda * invMassB;
558 posPred(idB, 2) += -n.
z * dLambda * invMassB;
572 Kokkos::View<const ContactC*, CpMem> contacts,
const PosUnits& units,
int numContacts,
573 const ColorSchedule& sched, Kokkos::View<const float*, CpMem> invMass,
574 Kokkos::View<
float* [3],
CpMem> posPred, Kokkos::View<
const float* [4],
CpMem> quatPred,
575 Kokkos::View<
const float* [4],
CpMem> quatStatic,
576 Kokkos::View<
const float* [3],
CpMem> invInertia, Kokkos::View<float, CpMem> maxOverlap,
577 Kokkos::View<float*, CpMem> posLambdaAcc = {},
const FusedLoopSpec* loop =
nullptr,
578 SlotOverride ov = {}, Kokkos::View<float, CpMem> posResidual = {},
float omega = 1.0f) {
579 const Kokkos::View<const int*, CpMem> cColor = sched.
color;
581 const Kokkos::View<const int*, CpMem> colorPerm = sched.
perm;
582 const std::vector<int>* colorOffs = sched.
offs;
586 maxOverlap, posLambdaAcc, units, ov, posResidual, omega};
587#ifdef KOKKOS_ENABLE_CUDA
591 if (fused && fused->maxBucket > 0 && colorOffs)
592 return demLaunchFusedSweepLoop(
593 space, f, colorPerm, *fused, numColors, *loop,
594 posResidual.extent(0) > 0 ? posResidual.data() : maxOverlap.data());
597 if (fused && fused->maxBucket > 0 && colorOffs &&
598 demLaunchFusedColorSweep(space, f, colorPerm, *fused, numColors))
605 for (
int color = 0; color < numColors; ++color) {
607 const int b = (*colorOffs)[color], e = (*colorOffs)[color + 1];
610 Kokkos::parallel_for(
611 "peclet::dem::solve_position_gs", Kokkos::RangePolicy<CpExec>(space, b, e),
612 KOKKOS_LAMBDA(
int i2) { f.solveOne(colorPerm(i2)); });
614 Kokkos::parallel_for(
615 "peclet::dem::solve_position_gs", Kokkos::RangePolicy<CpExec>(space, 0, numContacts),
616 KOKKOS_LAMBDA(
int idx) {
617 if (cColor(idx) == color)
dem — portable POD types + math + analytic SDFs shared by the Kokkos kernel ports.
float computeW(F3 r, F3 dir, float invM, F3 invI)
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.
bool solvePositionColoredGS(Kokkos::View< const ContactC *, CpMem > contacts, const PosUnits &units, int numContacts, const ColorSchedule &sched, Kokkos::View< const float *, CpMem > invMass, Kokkos::View< float *[3], CpMem > posPred, Kokkos::View< const float *[4], CpMem > quatPred, Kokkos::View< const float *[4], CpMem > quatStatic, Kokkos::View< const float *[3], CpMem > invInertia, Kokkos::View< float, CpMem > maxOverlap, Kokkos::View< float *, CpMem > posLambdaAcc={}, const FusedLoopSpec *loop=nullptr, SlotOverride ov={}, Kokkos::View< float, CpMem > posResidual={}, float omega=1.0f)
Colored Gauss–Seidel XPBD overlap solve: sweep the numColors colour classes in order,...
int colorContacts(Kokkos::View< const ContactC *, CpMem > contacts, int numContacts, int numBodies, Kokkos::View< int *, CpMem > cColor, const ColorScratch &scratch, int &leftover, Kokkos::View< const unsigned char *, CpMem > sleepMask={}, PosUnits units={}, SlotOverride ov={}, int vertexSpan=0)
Greedy graph-colour the contacts (raw bodyA/bodyB): no two contacts sharing a body get the same colou...
CpExec::memory_space CpMem
bool unitAsleep(const PosUnits &units, const Kokkos::View< const unsigned char *, CpMem > &sleepMask, int u)
A position unit is sleep-inactive iff all its contacts are sleep-masked (§4.3).
F3 ldF3(const V &v, int i)
F3 rotateVector(F4 q, F3 v)
int colorContactsIncremental(Kokkos::View< const ContactC *, CpMem > contacts, int numContacts, int numBodies, const CarriedColors &carried, Kokkos::View< int *, CpMem > cColor, Kokkos::View< unsigned long long *, CpMem > keysOut, const ColorScratch &scratch, int &leftover, bool forceFull, Kokkos::View< const unsigned char *, CpMem > sleepMask={}, PosUnits units={})
Incremental (warm-started) contact colouring for the single-GPU PGS position solve.
void commitContactColor(Kokkos::View< const unsigned long long *, CpMem > keys, Kokkos::View< const int *, CpMem > color, Kokkos::View< unsigned long long *, CpMem > prevKeys, Kokkos::View< int *, CpMem > prevColor, Kokkos::View< int *, CpMem > perm, int numContacts)
Commit this substep's per-contact (key, colour) sorted by key, for next substep's warm gather.
Kokkos::DefaultExecutionSpace CpExec
F4 ldF4(const V &v, int i)
long long colorKey(int idx)
splitmix32 finalizer: a well-mixed pseudo-random priority per edge index.
constexpr int kColorUncolourable
void solvePosition(Kokkos::View< const ContactC *, CpMem > contacts, int numContacts, const BodyState &bodies, const BodyPred &pred, const BodyDelta &delta, Kokkos::View< float, CpMem > maxOverlap, Kokkos::View< const int *, CpMem > onlyColor={}, int colorFilter=0, bool massSplit=false)
Accumulate XPBD position corrections for numContacts contacts.
constexpr int kColorPalette
Colouring palette (docs/contact_solve_framework.md §4.2 item 1): the greedy takes the lowest colour f...
void countPositionJacobi(Kokkos::View< const ContactC *, CpMem > contacts, int numContacts, Kokkos::View< int *, CpMem > counts)
Mass-split Jacobi count pass (docs/contact_solve_framework.md §3.1, D3): per body slot,...
bool normalOfA(const ContactC &c)
Which body's surface a contact's normal belongs to.
void expandUnitColors(const PosUnits &units, int numUnits, Kokkos::View< const int *, CpMem > unitColor, Kokkos::View< int *, CpMem > contactColor)
Copy each position unit's colour onto its contacts (contactColor(i) = unitColor(unit of i)).
dem — fused colour sweeps: one persistent kernel per sweep instead of one kernel launch per colour.
The solve's per-body accumulators: position/orientation and velocity corrections and the per-body con...
Kokkos::View< float *[3], CpMem > deltaPos
Kokkos::View< int *, CpMem > constraintCounts
The substep's predicted body state (Particles::bodyPred()).
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 *, CpMem > invMass
Kokkos::View< float *[4], CpMem > quat
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).
The colour schedule of one coloured Gauss–Seidel sweep: each item's colour and the colour count,...
Kokkos::View< const int *, CpMem > perm
const FusedSweepCtx * fused
const std::vector< int > * offs
Kokkos::View< const int *, CpMem > color
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
Device-side context for a fused colour sweep: the colour offsets (numColors+1, uploaded from buildCol...
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....
The position-unit edge source of colorEdges: a unit's edge is its leader contact's raw (bodyA,...
ColorEdge edge(int idx) const
Kokkos::View< const ContactC *, CpMem > contacts
Kokkos::View< const unsigned char *, CpMem > sleepMask
static constexpr ColorLabels labels()
int initialColor(int idx) const