Fix MT random seed and SPLIT KISS random state restore - #2620
Conversation
Preserve all seven KISS RNG words when generated CPU and regular GPU SPLIT loops restore the particle state. The previous scalar backup only carried randstate[0] and reset the remaining KISS state words on every repetition. Correct the MT initialization guard in the common runtime so mt_srandom(mcseed) runs for RNG_ALG=1, matching the selector emitted by the code generator and ensuring command-line seed input is applied to MT. Validated with the Unittest_SPLIT instrument for KISS and MT at SPLITS=1, 10, and 100, including deterministic same-seed and differing-seed checks.
Emit RANDSTATE_LEN alongside randstate_t for the selected MT or KISS algorithm before generating the particle and SPLIT code. Generate normal SPLIT backup and restore loops from RANDSTATE_LEN instead of assuming the seven-word KISS state. Guard the runtime header definitions so the generated preamble remains authoritative. Leave the FUNNEL implementation unchanged for a separate follow-up.
|
Checking the failed tests I see several of them are templateSANS_mantid which does use SPLIT (without a number). I am looking into if this instrument actually produce significantly different results before and after this patch. |
|
It seems that the templateSANS_mantid test failing is just a fluke, it converges to the target at higher ncounts and using random seed, but the tests have a fixed seed, so it will always fail here. The random number generation in SPLIT is changed with the PR, so it shouldn't be identical to the old results. |
|
I will
|
|
@mads-bertelsen I can't seem to start the mcstas-conda-testsuite workflow from the @mads-bertelsen-agentic branch, can you trigger that to run manually just once? |
|
@mads-bertelsen I am running a 4-core run 1e7 on my local mac for now. |
|
Local test OK except a curl retrieval of an mcpl.gz for ESS_butterfly_MCPL_test.
(And this a fix for that is under way at #2630) Deemed OK for hockey - merging. |

Free-form text area
Please describe what your PR is adding in terms of features or bugfixes:
While looking around in the codebase I think I stumbled over a couple of problems with the random number generation that I here propose a fix for. This was discovered while using AI to analyze the current systems. Let me know if I misunderstood something and any of these fixes are actually necessary!
1: Mismatch in defines with MT causing seed to be ignored
mccode/src/cogen.c.in:2422-2424
MT initialization is performed only by:
mccode-r.c:4259-4270
mccode_main.c
The above RNG_ALG sets the MT seed only when KISS is used instead of MT. Simple fix of RNG_ALG == 1 instead.
2: When the KISS random number generator is used (default), SPLIT needs to copy and restore the random state of each ray to continue the random number generation instead of redoing the same RNG. The KISS random number generate has 7 values on the particle struct, but only the first is copied / restored, so the quality of random numbers in SPLIT is reduced, easy fix to loop over the included values.
Questions in draft stage:
Declaration of use of AI-tools
Development OS / boundary conditions
Please describe what OS you developed and tested your additions on, and if any special dependencies are required:
PR Checklist for contributing to McStas/McXtrace
For a coherent and useful contribution to McStas/McXtrace, please fill in relevant parts of the checklist:
My work touches the code-generator in mccode/src
My work touches / adds to the runtime lib code (.c,.h etc in multiple locations
My PR is meant to fix a specific, existing issue
My contribution contains something else