Fix MT random seed and SPLIT KISS random state restore - #2620
Draft
mads-bertelsen-agentic wants to merge 2 commits into
Draft
Fix MT random seed and SPLIT KISS random state restore#2620mads-bertelsen-agentic wants to merge 2 commits into
mads-bertelsen-agentic wants to merge 2 commits into
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.
mads-bertelsen-agentic
marked this pull request as draft
September 1, 2026 10:24
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.
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