Write a complete, production-quality parallel Lennard-Jones force kernel from
scratch, plugged into the repo's real LJKernelView seam, and defend it with
measurements.
Everything in the nine lessons feeds into this: the serial rewrite that makes threading possible, the formulation choice that makes it safe, the scheduling that makes it scale, the discipline that makes the speedup number honest, and the judgement to leave the O(1) physics out of the hot loop.
You are not re-deriving the LJ potential. You are answering: given this kernel, how do you make it fast, correct, and reproducible on 8 cores — and how do you prove all three?
| Lesson | What it contributes |
|---|---|
| 1 | the harness, and the discipline of measuring before changing |
| 2 | flat tables, r² arithmetic, hoisted reciprocals |
| 3 | scatter vs gather, and pricing the choice |
| 4 | partitioning without dropping work, fixed-order reduction |
| 5 | why your write pattern is the whole safety argument |
| 6 | scheduling under heterogeneous cores |
| 7 | choosing a baseline you can defend |
| 8 | recognising the work that must not be parallelized |
Implement project/src/student_kernel.cpp:
double studentCompute(const LJKernelView& view, StudentBackend backend, int numThreads);- Four backends —
Triangular(best serial),Gather(serial),Parallel(your best parallel effort),ParallelDeterministic. - Correctness. Forces must match
LJPotential::calculateForcesReferenceto better than 1e-9 relative, for every backend. - Energy. Total potential energy, each pair counted once, matching the reference to 1e-9 relative.
- Thread independence.
Parallelmust give the same forces (to 1e-10) for 1, 2, 3, 5, 8 and 16 threads. If it does not, you have a race. - Determinism.
ParallelDeterministicmust be bitwise identical across repeated runs at 8 threads.Parallelneed not be — but must still be correct. - No races. Verified by requirements 4 and 5, and by running under TSan.
- Edge cases. n = 0, n = 1, a non-periodic axis (box length 0), and an atom whose type has no parameters, must all behave — no crash, no NaN.
- Physics invariant. Σ F⃗ = 0 across the periodic system for every backend.
- Performance.
Parallelat 8 threads must be at least 2× faster than your ownTriangularat N = 4096. (The repo's shipped kernel manages 4.03×; 2× is the passing bar.) - Tail correction. Implement
studentTailEnergy. It must not change any force — asserted bitwise.
cmake -B build -S . -DMOLSIM_BUILD_LEARN=ON -DCMAKE_BUILD_TYPE=Release
cmake --build build --parallel
cd build && ctest -L learn -R Capstone --output-on-failure
./build/project/capstone_report 4096 # your own measurementsSuggested, not prescribed:
project/src/
student_kernel.h the interface (given -- do not change the signatures)
student_kernel.cpp your implementation
A shape that works:
- one
computeAtomRange(view, i0, i1)doing the gather body, shared by every parallel backend, so all of them run identical arithmetic and any difference is a parallelism bug rather than an arithmetic one Parallel= OpenMP over that,schedulechosen by measurementParallelDeterministic= same, with a hand-rolled fixed-order reduction
If you want the fastest kernel rather than the most portable one, revisit
lesson 5: per-thread private buffers over the triangular loop beat gather by
~1.9× on this CPU. That is a legitimate answer for Parallel, and it is worth
being able to say why it would not be the right answer on a GPU.
ctest cannot grade judgement. Fill in project/REPORT.md:
- Your measured table: every backend, ms/call at N=4096, speedup against the baseline you chose, and a sentence justifying that baseline.
- A strong-scaling study, 1–8 threads, with efficiency. Explain the shape of the curve. If it has a knee, say what causes it.
- The serial fraction implied by Amdahl at 4 threads. Is Amdahl your limit? If not, what is?
- Which formulation you chose for
Paralleland why — including what it costs. - One thing you measured that contradicted what you expected.
Item 5 is not filler. It happened three times while this course was being
written: the std::map lookup was worth 10% rather than the predicted 2.5×,
std::pow was already optimised by the compiler, and SoA turned out to be
slower on CPU.
- Add a
Vectorizedbackend and see whether you can beat the compiler's auto-vectorization. - Wire the pressure virial through the kernel and validate against
P_tail. - Make the marshalling in
LJPotential::buildViewincremental — positions change every step, but types and tables do not. - Profile with Instruments (macOS) and find out what fraction of cycles is stalled on memory rather than arithmetic.