Random Generator and AT elements #879
-
Dear all, Lines 24 to 25 in 8620504 I will separate the questions in three parts : PYAT, AT MATLAB, and Elements using random numbers. Please, let me know what I got wrong about how it works. PYAT Lines 799 to 800 in 8620504 Line 28 in 8620504 Line 43 in 8620504 Lines 787 to 793 in 8620504 AT MATLAB Lines 73 to 74 in 8620504 Lines 30 to 31 in 8620504 Elements using random numbers (QuantDiff) at/atmat/lattice/element_creation/atQuantDiff.m Lines 17 to 19 in 8620504 at/atintegrators/QuantDiffPass.c Line 59 in 8620504 (VariableThinMultipole) at/pyat/at/lattice/variable_elements.py Lines 49 to 50 in 8620504 at/atintegrators/VariableThinMPolePass.c Line 69 in 8620504 Lines 153 to 156 in 8620504 Line 46 in 8620504 Lines 42 to 44 in 8620504 Lines 27 to 28 in 8620504 Moreover, and assuming all I asked before is correct. The C implementation of the generator seems to be compatible with pyat through numpy https://numpy.org/doc/2.0/reference/random/bit_generators/pcg64.html , but, I couldn't find the same for Matlab. I guess it means that the generator can not belong to the element because it needs to work in both python and matlab, therefore, it needs to be kept at the C level. If one desires to use the element seed in tracking, a new stream should be initialized before starting tracking, which means checking the ring for random elements and create every stream. Alternatively, one could forget about the element seed and create an input for the user seed ( A third solution would be to remove any One other option would be getting PCG to work inside MATLAB, so, the element could have its own stream when defined, and the tracking would only the element own stream. At last, maybe a more complicated way, one could change to a generator that exist in both python and matlab, and again implement the stream when creating the element. |
Beta Was this translation helpful? Give feedback.
Replies: 7 comments 8 replies
-
I'll put things in another way: random generators used in python and C are distinct, and as far as I can see, there is no specific random generator in Matlab, only the Matlab standard ones. The motivation is to avoid having the C engine depending on any external library. The whole implementation is in atrandom.c, and the behaviour is identical when running under python or Matlab. the C generators can be accessed from python but not from Matlab. For element passmethods, the generators which matter are the C generators, and the will work for both Matlab and python. There are 2 streams:
The buffers for these streams are defined in at.c: Lines 73 to 75 in 8620504 There is a third stream using a static private buffer, with simpler function calls without the buffer argument. Its buffer is defined here: Line 46 in 8620504 It should be used only in regions without parallel processing. There is no provision to have a specific stream for each element: element random values should be taken from one of the 2 tracking streams. So you are right, the seed specified in the variable multipole is not used. But the present implementation in Now comes the question of explicitly seeding the generators. It is possible in python, but not in Matlab. For the moment, a python random generator is used to provide the seed of C generators at the beginning of each tracking. It would be easy to add an optional
That's more difficult in Matlab: the tracking streams must be declared in atpass.c to be accessed by element passmethods. Then they cannot be seen by other mex-functions allowing explicit seeding. Defining them in a shared library accessed by several Mex-function is a solution, but how to compile and link a shared library with the Mex command? Finally about the "python" generators defined in lattice.options.py: they are completely independent form the C ones, and I don't think they are used anywhere except seeding the C generators. |
Beta Was this translation helpful? Give feedback.
-
Hello, |
Beta Was this translation helpful? Give feedback.
-
Hi Nicola, This commit mentions an update of the pass function that does not longer takes a seed. The current version does not take |
Beta Was this translation helpful? Give feedback.
-
Thanks @oscarxblanco. I think we need a way to pass a seed to the c code from matlab, for when we run the same identical process many times. In the past it was possible to specify a different seed in the quantum diffusion element for each process, but now it is not possible. |
Beta Was this translation helpful? Give feedback.
-
The seed can be set in python through a dedicated function, we had a quick look this morning with Nicola but could no find the matlab equivalent. I don't whether it is easy to implement but it should be possible maybe it even exist... we have to wait for @lfarv. A short description of random generators in the documentation would be helpful as well. |
Beta Was this translation helpful? Give feedback.
-
Hi all. Here are some explanations, in addition to the previous ones: The random generator has been taken out of elements because of parallel processing. In the case of python multiprocessing and MPI, the generators would be copied and would generate the same sequence. That's why there are now 2 different streams: a common one and a thread-specific one, handled in the C "main" function, Resetting the seed can be done in python because we can have several entry points into the same So the only solution in Matlab would be to add another input argument to the The problem is tricky because of the multiple parallel processing options: OpenMP for both python and Matlab, python multiprocessing and MPI for python only, and Matlab parallel-processing toolbox. The additional argument on tracking startup should work for all except Matlab parallel toolbox, though the generated sequence will be different for different methods. |
Beta Was this translation helpful? Give feedback.
-
#905 allows giving a seed for random generators when starting a tracking in |
Beta Was this translation helpful? Give feedback.
I'll put things in another way: random generators used in python and C are distinct, and as far as I can see, there is no specific random generator in Matlab, only the Matlab standard ones. The motivation is to avoid having the C engine depending on any external library. The whole implementation is in atrandom.c, and the behaviour is identical when running under python or Matlab. the C generators can be accessed from python but not from Matlab. For element passmethods, the generators which matter are the C generators, and the will work for both Matlab and python. There are 2 streams:
Param->thread_rng
will generate different streams in different parallel threads, eith…