Repository navigation
Port per-ray RNG seeding and SPLIT randstate fix from McCode #2808 (fixes #392) - #394
Merged
Merged
Conversation
…ixes mccode-dev#392) Match McCode's classic code generator (mccode-dev/McCode#2808): - seed each ray with _hash(_hash(seed) + cloop*gpu_innerloop + pidx) instead of _hash((pidx+1)*(seed+1)), and drop the per-batch seed bump, in raytrace_all and raytrace_all_funnel - make _uid unique across GPU batches and MPI ranks - size the particle randstate by RANDSTATE_LEN, defined in the RNG_ALG block as cogen.c.in does - SPLIT backs up and restores all RANDSTATE_LEN words of the RNG state, not only word 0 Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Contributor
Author
|
@g5t parallel to McCode PR mccode-dev/McCode#2808 |
g5t
approved these changes
Oct 18, 2026
g5t
left a comment
Collaborator
There was a problem hiding this comment.
A spot check looks like this is identical to your changes in McStas 🤞
Contributor
Author
|
@g5t cool, thanks. I will now proceed to merge on the McStas side also. I have adapted my CI to take mccode-antlr from main, should give us a clearer picture of the state of everything going forward. |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Fixes #392. This ports mccode-dev/McCode#2808 (fix for mccode-dev/McCode#2807) to mccode-antlr's code generator, so the generated C matches McCode's classic
cogen.c.in.Warning
Results change for every KISS-RNG run and for every instrument with SPLIT. They will not be bit-identical to earlier mccode-antlr output. The new output matches McCode's classic generator with #2808.
Changes
c_raytrace.py, in bothraytrace_alland the funnel loop):srandom(_hash((pidx+1)*(seed+1)))becomessrandom(_hash(_hash(seed) + cloop*gpu_innerloop + pidx)), and the per-batchseed = seed + gpu_innerloopis removed. The old key was a product, so different seeds and different MPI ranks shared rays, and the key wrapped at 2^32 on MSVC._uidis nowcloop*gpu_innerloop + pidx, plusmpi_node_rank * loops*gpu_innerloopunder MPI. This makes it unique across GPU batches. On CPU the values are unchanged.randstate[RANDSTATE_LEN]replaces the hard-codedrandstate[7]in the particle struct (c_header.py). Unlike what Port per-ray RNG seeding and _uid fix from McCode cogen (mccode-dev/McCode#2807) #392 assumed, mccode-antlr'sRNG_ALGblock did not defineRANDSTATE_LEN; the runtime header that defines it comes after the struct. So this PR adds#define RANDSTATE_LEN 1/7to that block, ascogen.c.indoes. Older runtimes (v3.8.7) define the same value, which is a legal identical redefinition.randstate[0], so KISS children restarted words 1-6 from the same state and got correlated streams. AllRANDSTATE_LENwords are now backed up and restored, with the same loop and variable names ascogen.c.in.sort_absorb_last()(FUNNEL + SPLIT) lives in the shared runtimemccode-r.c, so mccode-antlr picks it up with the McCode release.Tested
pytestwithMCCODEANTLR_MCCODE_POOCH__TAG=v3.8.7and withlatest(v3.9.2), C compile tests included: 1267 passed, 10 skipped (MPI, OpenACC, mcpl, build123d, matplotlib). No test needed changing.Test_RNG_rand01: generated with this branch, compiled with the McCode #2808 runtime (786934b; itsmccode-r.c/mccode-r.h.inchanges spliced into the embedded runtime), and run with-s 1000 Ncount=1000. The md5 of the non-#lines ofrngout.datis6c203d69205b0656c1391555e6068c93, the same as McCode's classic generator.Unittest_SPLITwithSPLITS=10,-n 1e5: compiles and runs,PSD_I=1,PSD_N=1e6, as the instrument's example expects.Not tested
🤖 Generated with Claude Code