Skip to content

Move Cython random number generation to C++ mechanism #1664

@mstimberg

Description

@mstimberg

In C++ standalone mode, we use the random generator mechanism from the C++ standard library, wrapped around in our own functions to get rand, randn, and poisson implementations. See #1559 for the PR that introduced this for C++ standalone mode. Note that the reason for using our own functions for rand and so on is that this way, we are generating exactly the same random numbers as before. In Cython, we are instead calling out to the numpy library (which under the hood does the same thing that we are doing in C++ standalone). It always asks for 20000 numbers at a time, to avoid going through numpy for every single number. It would be good to avoid this and use the same approach as C++ standalone mode. The C++ standalone code is distributed over a few files:

  • the objects.h template (note that it's in the same file as objects.cpp contains the definition of the RandomGenerator class, which is wrapping the standard library function and adds the rand, randn, and seed functionality
  • The generate_rand_code function in the brian2/devices/cpp_standalone/codeobject.py generates the actual rand/randn function definition – this is a bit more complicated than necessary for Cython, because in C++ mode we can run things in multiple threads and in that case we have more one RandomGenerator object per thread.
  • The cpp_generator.py file adds an implementation for the poisson function (which calls out to rand).

In principle, we should be able to reuse all this from the Cython code. The main obstacle is that we don't have the objects.h file, so we should move the RandomGenerator definition out of it and into an independent file that we can then use both from Cython and from C++ standalone mode.

Metadata

Metadata

Assignees

No one assigned

    Type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions