Skip to main content

RngGenerator

Struct RngGenerator 

Source
pub struct RngGenerator { /* private fields */ }
Expand description

High-level GPU random number generator.

Wraps one of the available RngEngine implementations and manages CUDA resources (context, stream, modules) for kernel compilation and launch.

§Example

let mut rng = RngGenerator::new(RngEngine::Philox, 42, &ctx)?;
let mut buf = DeviceBuffer::<f32>::alloc(1024)?;
rng.generate_uniform_f32(&mut buf)?;

Implementations§

Source§

impl RngGenerator

Source

pub fn new(engine: RngEngine, seed: u64, ctx: &Arc<Context>) -> RandResult<Self>

Creates a new RNG generator with the specified engine and seed.

§Errors

Returns RandError::Cuda if CUDA stream creation fails.

Source

pub fn set_seed(&mut self, seed: u64)

Sets the RNG seed.

Source

pub fn set_offset(&mut self, offset: u64)

Sets the stream offset (for counter-based generators).

Source

pub fn skip(&mut self, n: u64)

Advances the offset by n elements.

Source

pub fn generate_uniform_f32( &mut self, output: &mut DeviceBuffer<f32>, ) -> RandResult<()>

Generates uniformly distributed f32 values in [0, 1).

§Errors

Returns RandError on PTX generation, compilation, or launch failure.

Source

pub fn generate_uniform_f64( &mut self, output: &mut DeviceBuffer<f64>, ) -> RandResult<()>

Generates uniformly distributed f64 values in [0, 1).

§Errors

Returns RandError on PTX generation, compilation, or launch failure.

Source

pub fn generate_uniform_f32_optimized( &mut self, output: &mut DeviceBuffer<f32>, ) -> RandResult<()>

Generates uniform f32 values using the optimized 4-per-thread Philox engine.

For large outputs (>= 1024 elements), this uses the optimized Philox engine where each thread generates 4 values. For smaller counts or non-Philox engines, falls back to the standard engine.

§Errors

Returns RandError on PTX generation, compilation, or launch failure.

Source

pub fn generate_normal_f32_optimized( &mut self, output: &mut DeviceBuffer<f32>, mean: f32, stddev: f32, ) -> RandResult<()>

Generates normal f32 values using the optimized 4-per-thread Philox engine.

For large outputs (>= 1024 elements), each thread generates 4 normal values using two Box-Muller transforms on the full Philox output. Falls back to the standard engine for small counts or non-Philox engines.

§Errors

Returns RandError on PTX generation, compilation, or launch failure.

Source

pub fn generate_normal_f32( &mut self, output: &mut DeviceBuffer<f32>, mean: f32, stddev: f32, ) -> RandResult<()>

Generates normally distributed f32 values.

§Errors

Returns RandError on PTX generation, compilation, or launch failure.

Source

pub fn generate_normal_f64( &mut self, output: &mut DeviceBuffer<f64>, mean: f64, stddev: f64, ) -> RandResult<()>

Generates normally distributed f64 values.

§Errors

Returns RandError on PTX generation, compilation, or launch failure.

Source

pub fn generate_log_normal_f32( &mut self, output: &mut DeviceBuffer<f32>, mean: f32, stddev: f32, ) -> RandResult<()>

Generates log-normally distributed f32 values.

A log-normal variate is exp(Normal(mean, stddev)).

§Errors

Returns RandError on PTX generation, compilation, or launch failure.

Source

pub fn generate_log_normal_f64( &mut self, output: &mut DeviceBuffer<f64>, mean: f64, stddev: f64, ) -> RandResult<()>

Generates log-normally distributed f64 values.

§Errors

Returns RandError on PTX generation, compilation, or launch failure.

Source

pub fn generate_poisson_f32( &mut self, output: &mut DeviceBuffer<f32>, lambda: f64, ) -> RandResult<()>

Generates Poisson-distributed f32 values.

Uses a normal approximation: Normal(lambda, sqrt(lambda)) followed by in-place rounding to nearest integer and clamping to >= 0.

§Errors

Returns RandError on PTX generation, compilation, or launch failure.

Source

pub fn generate_u32(&mut self, output: &mut DeviceBuffer<u32>) -> RandResult<()>

Generates raw u32 random values.

Only supported for the Philox engine. Other engines return RandError::UnsupportedDistribution.

§Errors

Returns RandError on unsupported engine, PTX generation, or launch failure.

Auto Trait Implementations§

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T> Instrument for T

Source§

fn instrument(self, span: Span) -> Instrumented<Self>

Instruments this type with the provided Span, returning an Instrumented wrapper. Read more
Source§

fn in_current_span(self) -> Instrumented<Self>

Instruments this type with the current Span, returning an Instrumented wrapper. Read more
Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.
Source§

impl<T> WithSubscriber for T

Source§

fn with_subscriber<S>(self, subscriber: S) -> WithDispatch<Self>
where S: Into<Dispatch>,

Attaches the provided Subscriber to this type, returning a WithDispatch wrapper. Read more
Source§

fn with_current_subscriber(self) -> WithDispatch<Self>

Attaches the current default Subscriber to this type, returning a WithDispatch wrapper. Read more