Task 1 of 5
Newton, in one line: the gravitational pull between two bodies is
G · m₁ · m₂ / r². Divide out the mass being pulled and you get its
acceleration — a = G · M / r² — which only depends on the
other body. In this course G = 1 (astrophysicists rescale units to
do exactly this, so you're in good company).
Here 64 bodies drift around one star. Each thread owns one body — its position is
posX[this.thread.x], posY[this.thread.x] — and answers a single
question: how hard does the star pull on me? No loops yet; that's next.
starMass / r².dx, dy offsets to the star (already wired up)r² = dx·dx + dy·dystarMass / r² — inverse-square, with G = 1The law wants r², and dx*dx + dy*dy is
r². Taking Math.sqrt just to square it again is the most
popular way to waste GPU cycles.
return starMass / (dx * dx + dy * dy);
nbody sample assigns body i to thread i exactly like
this, and its HIP port runs the identical mapping on ROCm.
This page is an interactive exercise — the editor, the GPU runner and your saved progress need JavaScript. The text above is the full brief.