RNGSeed input reproducible runs

67 views
Skip to first unread message

G Buist

unread,
Nov 29, 2022, 7:36:28 AM11/29/22
to mcx-users
Dear Dr. Fang,

I have some questions about the RNGSeed parameter, is it correct that for values greater than 2^31 are all RNGSeed will generate the same results? (see figures below). And how does the size of RNGSeed affect the randomness of the simulations?

According to documentation on Monte Carlo eXtreme: GPU-based Monte Carlo Simulations: Doc/mcx help it has to be a large positive integer to get reproducible results. But it does not give within which range it has to be.
In this group on the RNGSeed I found:  "it is a 32-bit integer (if set to 0 or a negative value, mcx will use system clock as the seed), you are recommended to set it to a big positive number (so that every bit is randomized).  explicitly setting to a big positive integer has the benefit of creating somewhat reproducible results, but setting to 0 or negative value can not do the same."

Is it correct to interpret this as to get reproducible results RNGSeed has to be a positive integer between 1 and 2^(32-1) (recommended to set it to a big positive number so that every bit is randomized)?

To check this I did some simulations with 100 photons with mua=mus=0, n=1 and g=1 where I saved the seeds (' -w spxv --saveseed 1 -F jnii -E 2147483651 --bc ______000001 ')
With as resulting photon seeds for the different RNGSeed

Figure_photonseeds_RNGSeed_less_2_31.pngFigure_photonseeds_RNGSeed_greater_2_31.png

Kind Regards,
Gijs Buist

Qianqian Fang

unread,
Nov 29, 2022, 12:06:14 PM11/29/22
to mcx-...@googlegroups.com
see my comments below


On 11/29/22 07:36, 'G Buist' via mcx-users wrote:
Dear Dr. Fang,

I have some questions about the RNGSeed parameter, is it correct that for values greater than 2^31 are all RNGSeed will generate the same results? (see figures below). And how does the size of RNGSeed affect the randomness of the simulations?

According to documentation on Monte Carlo eXtreme: GPU-based Monte Carlo Simulations: Doc/mcx help it has to be a large positive integer to get reproducible results. But it does not give within which range it has to be.
In this group on the RNGSeed I found:  "it is a 32-bit integer (if set to 0 or a negative value, mcx will use system clock as the seed), you are recommended to set it to a big positive number (so that every bit is randomized).  explicitly setting to a big positive integer has the benefit of creating somewhat reproducible results, but setting to 0 or negative value can not do the same."

Is it correct to interpret this as to get reproducible results RNGSeed has to be a positive integer between 1 and 2^(32-1) (recommended to set it to a big positive number so that every bit is randomized)?


yes this is still correct.



To check this I did some simulations with 100 photons with mua=mus=0, n=1 and g=1 where I saved the seeds (' -w spxv --saveseed 1 -F jnii -E 2147483651 --bc ______000001 ')
With as resulting photon seeds for the different RNGSeed


please note that exact reproducibility is not guaranteed for a multi-threaded environment. however, in your test case, if you only run 100 photons, it is likely every thread only need to run a single photon, therefore, the RNG seed will be the initial seed generated on the CPU (host), which is based on libc's RNG function and is supposed to be exactly reproducible.

what if you change -E 2147483651 to something a little bit smaller? say remove the leading number 2 and make it 8 digit?



Figure_photonseeds_RNGSeed_less_2_31.pngFigure_photonseeds_RNGSeed_greater_2_31.png

Kind Regards,
Gijs Buist

--
You received this message because you are subscribed to the Google Groups "mcx-users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to mcx-users+...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/mcx-users/7ba1be84-6339-4c34-81eb-b6281dce1664n%40googlegroups.com.


Buist, G. (Gijs)

unread,
Nov 30, 2022, 5:03:00 AM11/30/22
to mcx-...@googlegroups.com

Dear Qianqian,

 

Thanks for the quick reaction. I want to be sure that I understood what you said, is it correct that:

 

RNGSeed is used to generate a seed for each photon that will be launched. These photon seeds together with the simulation parameters ( volume, optical properties, flags) will determine the photons interaction. Reruns use saved photon seeds to reproduce the photon interactions of each photon seed exactly. However it is not guaranteed that the same RNGSeed will reproduce the same photon seeds if the number of photons is greater than the number of threads. This is because the timing (sequencing) of when which threads are done and will need a new photon seed is impossible to control and keep constant.

 

Also I have a second question. From the sentence that said that RNGSeed is a 32-bit integer I had understood that RNGSeed has to be between 1 and 2^32. But from simulations I did I noticed that simulations with RNGSeed > 2^31 gave the exact same number of detected photons while different RNGSeeds where used (51 different RNGSeed with RNGSeed >2^31 gave same number of detected photons). I therefore did runs while saving the photon seeds as described in my first message. I did 4 runs with different RNGSeed with RNGSeed <2^31 and 4 with different RNGSeed and RNGSeed >2^31. The results can be seen in the attached figures, which show that for the RNGSeed <2^31 we get different photon seeds for every RNGSeed while for RNGSeed >2^31 we get the exact same photon seeds. Now my question is: what is the range of RNGSeed to get as reproducible results as possible while different RNGSeed result in calculating different realisations?

 

Finally I noticed that others have used 128-bit integers RNGSeed. How does this affect the simulations?

 

Thanks in advance.

Regards,

Gijs Buist

 

From: 'Qianqian Fang' via mcx-users <mcx-...@googlegroups.com>
Sent: dinsdag 29 november 2022 18:06
To: mcx-...@googlegroups.com
Subject: Re: [mcx-users] RNGSeed input reproducible runs

 

see my comments below

 

 

On 11/29/22 07:36, 'G Buist' via mcx-users wrote:

Dear Dr. Fang,

 

I have some questions about the RNGSeed parameter, is it correct that for values greater than 2^31 are all RNGSeed will generate the same results? (see figures below). And how does the size of RNGSeed affect the randomness of the simulations?

 

According to documentation on Monte Carlo eXtreme: GPU-based Monte Carlo Simulations: Doc/mcx help it has to be a large positive integer to get reproducible results. But it does not give within which range it has to be.

In this group on the RNGSeed I found:  "it is a 32-bit integer (if set to 0 or a negative value, mcx will use system clock as the seed), you are recommended to set it to a big positive number (so that every bit is randomized).  explicitly setting to a big positive integer has the benefit of creating somewhat reproducible results, but setting to 0 or negative value can not do the same."

 

Is it correct to interpret this as to get reproducible results RNGSeed has to be a positive integer between 1 and 2^(32-1) (recommended to set it to a big positive number so that every bit is randomized)?

 

yes this is still correct.

 

 

To check this I did some simulations with 100 photons with mua=mus=0, n=1 and g=1 where I saved the seeds (' -w spxv --saveseed 1 -F jnii -E 2147483651 --bc ______000001 ')

With as resulting photon seeds for the different RNGSeed

 

please note that exact reproducibility is not guaranteed for a multi-threaded environment. however, in your test case, if you only run 100 photons, it is likely every thread only need to run a single photon, therefore, the RNG seed will be the initial seed generated on the CPU (host), which is based on libc's RNG function and is supposed to be exactly reproducible.

what if you change -E 2147483651 to something a little bit smaller? say remove the leading number 2 and make it 8 digit?

 

 

 

Kind Regards,

Gijs Buist

 

--
You received this message because you are subscribed to the Google Groups "mcx-users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to mcx-users+...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/mcx-users/7ba1be84-6339-4c34-81eb-b6281dce1664n%40googlegroups.com.

 

--
You received this message because you are subscribed to a topic in the Google Groups "mcx-users" group.
To unsubscribe from this topic, visit https://groups.google.com/d/topic/mcx-users/E9VgPb4q62M/unsubscribe.
To unsubscribe from this group and all its topics, send an email to mcx-users+...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/mcx-users/cc9fe1b3-2e25-7768-b17a-1b4640aef134%40neu.edu.

Figure_photonseeds_RNGSeed_less_2_31.png
Figure_photonseeds_RNGSeed_greater_2_31.png

Qianqian Fang

unread,
Dec 2, 2022, 11:18:17 AM12/2/22
to mcx-...@googlegroups.com
sorry, now I see what you are asking. let me make another clarification.

the random numbers are handled as described below:

first, user gives a master seed, an integer 0-2^31, and given as part of the input file or by -E/cfg.seed input. if user seed is not given, mcx uses 1648335518 as the default seed.
if this integer is 0 or negative, MCX uses the current system clock (i.e. the elapsed seconds since the start of the machine) as the master seed.

then, MCX calls libc's rand() function (defined in <stdlib.h>) on the CPU, which internally use linear congruential generator (LCG) RNG, to produce a random seed array (128bit or 16 bytes per thread), with one thread per seed. such seed data is copied to the GPU.

on the GPU, every thread loads the 128 bit RNG seed at the beginning of the thread execution to initialize a GPU-based xorshift128+ RNG. The xorshift128+ RNG then updates the RNG state vector (128 bit) for each RNG consumption until the requested photon number per thread is completed; it is called in various places - such as scattering and reflection.

my previous comment on "exact reproducibility is not guaranteed" is for the output fluence - because accumulation of floating point numbers from multi-threads may produce different outputs due to the execution order (i.e. limited precision floating-point summation is not commutable). However, that comment is not true for RNG seeds. The host/CPU side random number seeds are expected to reproducible; as a result, the RNG seed used for each photon inside each thread are also supposed to be exactly reproducible.

when you use replay to save RNG seeds (128 bit per detected photon), it is possible that the output RNG data buffer may not be reproducible. However, I believe if you sort the RNG by rows, they should likely be reproducible - the difference may be caused by the order of executions of each thread - some thread runs slightly earlier than another, changing the position of the RNG in the output 1D seed buffer.


for your 2nd question, you can print cfg->seed value in mcx_utils.h and see what happened if you type in a number larger than 2^31-1 , if you do that, you will find out that overflow will happen and the number is capped at 2^31-1 if the user input is out of range.

hope this helps.


Qianqian
Reply all
Reply to author
Forward
0 new messages