Is it possible to access the iteration/step number inside a Stan program?

Not from the Stan language. We intentionally designed the log density to be stateless.

I do understand your motivation. The only option is what Ben is suggesting.
RStan has its own mechanism for adding C++ directly to the build process after declaring the function you’re going to use in the function block. It won’t be guaranteed to work everywhere. And the sharing will be per-chain, not cross-chain. So you won’t be able to get the chain ID, which is another thing people ask for and which we intentionally made stateless so that all chains would have the same behavior.

1 Like