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: 1 comment 2 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.
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…