Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Implement poisson function #209

Merged
merged 18 commits into from
Aug 8, 2021

Conversation

denisalevi
Copy link
Member

This PR implements the poisson function. Should close #174.

See brian-team/brian2#1111

@denisalevi
Copy link
Member Author

denisalevi commented Jun 2, 2021

There are two different ways that poisson random numbers are generated in CUDA now:

  1. Using a buffer system which is refilled from host code in regular intervals using the cuRAND host API. This is used for poisson(lambda) when lambda is a the same value across all neurons/synapses and doesn't change during a run.
  2. Using on the fly RNG from device code using the cuRAND device API. This is used for poisson(lambda) when lambda is a vectorized variable (different across neurons/synapses) or when it can change during a simulation. We do this because the host side API is only efficient when generating many random numbers for the same lambda.

To decide which RNG type to use, I regex match the <lambda> string in _poisson(<lambda>, _vectorization_idx) in the generated code. Stored as lambda_str = <lambda>, I first test if if is a literal and if not I look up the variable in the codeobjects variables dictionary and check if it is scalar. It looks something like this:

lamba_vectorized = True
try:
    # try to convert to float, will raise ValueError if not possible
    lamda_value = float(lamda_str)
    lamba_vectorized = False
except ValueError:
    # lamda is not a literal but a variable, check if lamda is scalar
    lamda_var = codeobj.variables[lamda_str]
    if lamda_var.scalar and lamda_var.constant:
        lamda_vectorized = False
        lamda_value = lamda_var.value

@mstimberg
Could you maybe review this check? For poisson(<lambda>) calls where <lambda> is not a literal, will it always be stored as a Variable in codeobj.variables? And if so: If I make check that it is scalar and constant, can I be sure that it won't change during a run and also that I always have access to its value before the standalone simulation is run?

If the check is fine, it will still miss a few cases where host side RNG could be used: E.g. if <lambda> is a non-scalar variable that happens to still have the same value for all neurons/synapses and doesn't change during a run. Such cases could probably be detected already on Python side (if a vectorized <lambda> variable is set with a fixed array or a scalar value) or on C++/CUDA side (if it is set using string syntax but happens to have same lambda for all neurons/synapses, similar to how we check if delays are scalar even though they were set with string syntax). Just leaving this here as a comment, not a priority for now.

@denisalevi denisalevi force-pushed the update-brian2-submodule branch 3 times, most recently from 15472ce to 06f1d94 Compare July 27, 2021 09:13
@denisalevi denisalevi force-pushed the implement-poisson-function branch 2 times, most recently from 5279419 to e2e3365 Compare July 30, 2021 11:17
@denisalevi
Copy link
Member Author

I added poisson versions for all the seed RNG tests. The test_random_number_generation.py has a lot of tests now and the same test for each of the random number functions. I started refactoring it such that the tests are dynamically generated (same test but different RNG functions). Something wasn't working, I left the code as comment at the bottom of the file, for another time to figure out...

Tests are passing. I opened #228 to make sure the logic for checking poisson's lambda variable are correct.

Merging.

@denisalevi denisalevi merged commit 32fd0bb into update-brian2-submodule Aug 8, 2021
@denisalevi denisalevi deleted the implement-poisson-function branch August 8, 2021 07:55
denisalevi added a commit that referenced this pull request Aug 8, 2021
Update brian2 submodule to last version using nosetest
Collection of following PRs: #218, #219, #223, #227, #202, #209
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

1 participant