Skip to content

Add TinyMT64 as opt-in RNG_ALG=3 (draft; needs McCode runtime from rng-gpu-mt-design) - #393

Draft
willend wants to merge 1 commit into
mccode-dev:mainfrom
willend:tinymt64-rng-alg-3
Draft

willend wants to merge 1 commit into
mccode-dev:mainfrom
willend:tinymt64-rng-alg-3

Conversation

@willend

@willend willend commented Oct 18, 2026

Copy link
Copy Markdown
Contributor

Draft: depends on McCode's TinyMT64 runtime, which is not merged in McCode yet. It lives on branch rng-gpu-mt-design (commit 69b18b803, "Add TinyMT64 as GPU-capable RNG_ALG=3"); the design and results are in RNG_GPU_DESIGN.md.

What

This adds TinyMT64 (Saito & Matsumoto, BSD-3) as an opt-in random number generator, RNG_ALG=3. It mirrors the header block in McCode's cogen.c.in on that branch: four lines in c_header.py, namely #define _RNG_ALG_TINYMT 3 plus an #elif branch with randstate_t uint64_t and RANDSTATE_LEN 2.

The generator functions (tinymt64_srandom, tinymt64_random, ...) live in McCode's shared runtime mccode-r.c / mccode-r.h.in. mccode-antlr embeds that runtime from the libc registry, so it needs no copy of its own. Building with -DRNG_ALG=3 needs a McCode runtime that includes TinyMT64.

The default stays KISS. Results don't change unless RNG_ALG=3 is chosen.

Relation to #392

#392 (porting McCode #2808's per-ray seeding) adds RANDSTATE_LEN for MT/KISS and changes the particle struct to randstate[RANDSTATE_LEN]. This PR works without #392, because the current hard-coded randstate[7] has room for TinyMT's 2 words. The two PRs touch neighbouring lines of the same #if block, so whichever lands second will hit a trivial merge conflict.

Checks

  • pytest passes.
  • I generated C for McStas Test_RNG_rand01 with this branch, using the runtime from rng-gpu-mt-design (swapped in for the registry's runtime by a scratch script), then built it with -DRNG_ALG=3 and ran -s 1000 Ncount=1000:
    • With mccode-antlr's current per-ray seed _hash((pidx+1)*(seed+1)), the output differs from classic McCode, as it should: that branch uses #2808's seed.
    • After replacing the seed line in the generated C with #2808's _hash(_hash(seed) + cloop*gpu_innerloop + pidx) (what Port per-ray RNG seeding and _uid fix from McCode cogen (mccode-dev/McCode#2807) #392 ports), the md5 of the non-# lines of rngout.dat is 753159792665dde568a61d3afd8e42ae. That is identical to classic McCode on rng-gpu-mt-design.
  • Default build (KISS): the C generated by this branch differs from upstream main's only in the four header lines above, and the KISS rngout.dat md5 is identical for both.

🤖 Generated with Claude Code

Mirror McCode's rng-gpu-mt-design header: _RNG_ALG_TINYMT 3 with a
two-word uint64_t state. The generators live in McCode's runtime
(mccode-r.c/.h), so this needs a runtime that provides them. KISS stays
the default.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@willend

willend commented Oct 18, 2026

Copy link
Copy Markdown
Contributor Author

Similar to what I have on https://github.com/mccode-dev/McCode/tree/rng-gpu-mt-design.

@g5t keeping this in draft, no reason to add until we've agreed IRL that this makes sense. (See slack chat on same topic.)

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant