5 Replies Latest reply on Dec 19, 2008 3:18 AM by rick.weber

    Random number generation in kernel?

    jtelf2

      Hi there, my Stream application requires the use of a random number generator inside the kernel code. Since the standard c libraries aren't available and there isn't a random function in the kernel intrinsics, what's the best way to achieve this? Have I missed a library somewhere or will I need to implement a simple custom generator inside the kernel code?

      Many thanks.

       

       

        • Random number generation in kernel?
          genaganna

          MersenneTwister sample is one good sample for Random number generation

          Sample is aviable at legacy/appsMersenneTwister and CPP/apps/MersenneTwister

            • Random number generation in kernel?
              twinclouds

              Another good reference is the ones defined in the "Numerical Recipes," which can also be accessed online: http://www.physics.louisville.edu/help/nr/bookfpdf.html ->http://www.physics.louisville.edu/help/nr/bookfpdf/f7-1.pdf.  It is given in Fortran but should be easy to translated to C.

              BTW, do you generating the random numbers once for all of the streams or one per stream?  I might need to do the latter but don't know if it will work.

               

                • Random number generation in kernel?
                  rick.weber

                   

                  Originally posted by: twinclouds Another good reference is the ones defined in the "Numerical Recipes," which can also be accessed online: http://www.physics.louisville.edu/help/nr/bookfpdf.html ->http://www.physics.louisville.edu/help/nr/bookfpdf/f7-1.pdf.  It is given in Fortran but should be easy to translated to C.

                   

                  BTW, do you generating the random numbers once for all of the streams or one per stream?  I might need to do the latter but don't know if it will work.

                   

                   

                   

                  You can do the former by passing a single seed for use in whatever pseudo random number generator you use. You can do the latter by passing a stream the size of your output stream that contains many different seeds for each coordinate in the output stream. In either case, you will want to copy the resulting number generated to either a number in the first case or a stream in the second for reseeding the generator when you call the kernel again (since I don't think you can really maintain the generators' state on a GPU).

                    • Random number generation in kernel?
                      Brane214

                      Interesting question.

                      I noted that while OP was asking about random number generation, all answers were pertaining to _pseudo_random numbers.

                      Which exposes interesting problem - out of all hardware registers etc on GPU, is there some address span that would yield non-deterministic result- some noise on some internal bus on non-decoded address etc that could be used at least as the seed ?

                      I know that everything on the chip has some influence to everything else and that is for this reason practically impossible to get true "white noise" source , even when purposefully done, but is there a way to catch some of that noise for seed, even if it might be heavilly "coloured" ?

                       

                       

                       

                        • Random number generation in kernel?
                          rick.weber

                           

                          Originally posted by: Brane214 Interesting question.

                           

                          I noted that while OP was asking about random number generation, all answers were pertaining to _pseudo_random numbers.

                           

                          Which exposes interesting problem - out of all hardware registers etc on GPU, is there some address span that would yield non-deterministic result- some noise on some internal bus on non-decoded address etc that could be used at least as the seed ?

                           

                          I know that everything on the chip has some influence to everything else and that is for this reason practically impossible to get true "white noise" source , even when purposefully done, but is there a way to catch some of that noise for seed, even if it might be heavilly "coloured" ?

                           

                           

                           

                           

                           

                           

                           

                          Short answer: no.

                          Brook+ has no mechanisms for interfacing with hardware level registers supposing that an A/D register exists or something that exhibits noise.

                          The CAL documentation lists a set of instructions and what can be a parameter to those instructions. There is no way to interface with any components that would exhibit enough randomness to be suitable for what you're suggesting. You could try reading an uninitialized register, but even that will give some numbers more frequently than others (in fact, from experimentation, writing an uninitialized register to a stream returns 0 if I recall).

                          Even then, skimming through the R600 architecture document, I didn't see anything that could be used in this way. There might be a timer on the GPU that I missed, but I don't know how you would use it.