prepare_codeobj_code_for_rng function
(Shortest import: from brian2cuda.device import prepare_codeobj_code_for_rng)
- brian2cuda.device.prepare_codeobj_code_for_rng(codeobj)[source]
Prepare a CodeObject for random number generation (RNG).
- There are two different ways that random numbers are generated in CUDA:
Using a buffer system which is refilled from host code in regular intervals using the cuRAND host API. This is used for
rand(),randn()andpoisson(lambda)whenlambdais a scalar. The buffer system is implemented in therand.cutemplate.Using on-the-fly RNG from device code using the cuRAND device API. This is used for
binomialandpoisson(lambda)whenlambdais a vectorized variable (different across neurons/synapses). This needs initilization of cuRAND random states, which is also happening in therand.cutemplate.
This function counts the number of
rand(),randn()andpoisson(<lambda>)appearances incodeobj.code.cu_fileand stores this number in thecodeobj.rng_callsdictionary (with keys"rand","randn"and"poisson_<idx>",one <idx> perpoisson()call). If the codeobject uses the curand device API for RNG (for binomial of poisson with variable lambda), this function setscodeobj.needs_curand_states = True.For RNG functions that use the buffer system, this function replaces the function arguments in the generated code such that a pointer to the random number buffer and the correct index are passed as function arguments.
For RNG functions that use on-the-fly RNG, the functions are not replaced since no pointer or index has to be passed.
For the
poissonRNG, the RNG type depends on thelambdavalue. For scalarlambda, we use the buffer system which is most efficient and most robust in the RNG. For vectorizedlambdavalues, the host API is inefficient and instead the simple device API is used, which is the most efficient but least robust. For the two RNG systems to work, we overload the CUDA implementation of_poissonwith_poisson(double _lambda, ...)and_poisson(unsigned int* _poisson_buffer, ...). When the buffer system is used, we replace the_poisson(<lambda>, ...)calls with_poisson(<int_pointer>, ...)calls.For
poissonwithlambda <= 0, the returned random numbers are always0. This function makes sure that thelambdais replaced with a double literal for our overloaded_poissonfunction to work correctly.- Parameters:
codeobj: CodeObjects :
Codeobject with generated CUDA code in
codeobj.code.cu_file.