Skip to content

Random Number Generators Infrastructure + Reintegrate Philox RNG

Frederik Hennig requested to merge fhennig/random-numbers into v2.0-dev

Set up infrastructure for random number generation in the frontend, IR and backend passes, and integrate the Philox random number generator into the pystencils 2.0 backend.

Types

  • Add PsNamedArrayType, a subclass of PsArrayType that models nd-array types with a name, i.e. C++ structs/classes implementing the [] operator. This finally allows handling of arrays as first-class objects, which is not possible through C-arrays.

RNGs in the frontend

The old symbolic RNG interface is replaced by a cleaner and more concise design: An RNG object acts as a factory for symbolic calls to an RNG engine, and the generated random numbers are encapsulated in array-symbols (using PsNamedArrayType) which can be indexed through [].

Example:

rng = ps.random.Philox("my_rng", "float32")
ctr = ps.TypedSymbol("c", "uint32")
rx, rasm = rng.get_random_vector(ctr)

f = ps.fields("f: float32[2D]")

asms = [
    rasm,  # rx: Vec< float32, 4 > = my_rng(ctr)
    ps.Assignment(f(), rx[0] + rx[1] + rx[2] + rx[3])
]
  • Remove module pystencils.rng, introduce module pystencils.sympyextensions.random
  • Introduce base class RngBase for symbolic RNGs
  • Introduce class Philox for the 32x4 Philox counter-based RNG
  • Add user manual chapter on using RNGs

RNGs in the Backend

  • Add PsIrFunction as a common base class for IR functions that need to be lowered to implementations
  • Add PsRngEngineFunction and RngSpec in backend.functions to model counter-based RNG engines with external state
  • Extend FreezeExpressions to map front-end RNG invocations to IR RNG engine functions
  • Implement type checking for RNG engine functions
  • Extend GenericCPU and GenericGPU classes to lower Philox RNG engine to runtime implementations
  • Implement short-vector structs for random number vectors returned by Philox in the runtime headers
  • Wrap philox RNG implementation into functions returning generated numbers as a short vector (instead of returning through output-references)

Future Work

  • Implement vectorization of Philox RNG
  • Reintroduce AESNI RNG for x86 architectures
Edited by Frederik Hennig

Merge request reports

Loading