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
impl RngGenerator
Sourcepub fn new(engine: RngEngine, seed: u64, ctx: &Arc<Context>) -> RandResult<Self>
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.
Sourcepub fn set_offset(&mut self, offset: u64)
pub fn set_offset(&mut self, offset: u64)
Sets the stream offset (for counter-based generators).
Sourcepub fn generate_uniform_f32(
&mut self,
output: &mut DeviceBuffer<f32>,
) -> RandResult<()>
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.
Sourcepub fn generate_uniform_f64(
&mut self,
output: &mut DeviceBuffer<f64>,
) -> RandResult<()>
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.
Sourcepub fn generate_uniform_f32_optimized(
&mut self,
output: &mut DeviceBuffer<f32>,
) -> RandResult<()>
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.
Sourcepub fn generate_normal_f32_optimized(
&mut self,
output: &mut DeviceBuffer<f32>,
mean: f32,
stddev: f32,
) -> RandResult<()>
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.
Sourcepub fn generate_normal_f32(
&mut self,
output: &mut DeviceBuffer<f32>,
mean: f32,
stddev: f32,
) -> RandResult<()>
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.
Sourcepub fn generate_normal_f64(
&mut self,
output: &mut DeviceBuffer<f64>,
mean: f64,
stddev: f64,
) -> RandResult<()>
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.
Sourcepub fn generate_log_normal_f32(
&mut self,
output: &mut DeviceBuffer<f32>,
mean: f32,
stddev: f32,
) -> RandResult<()>
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.
Sourcepub fn generate_log_normal_f64(
&mut self,
output: &mut DeviceBuffer<f64>,
mean: f64,
stddev: f64,
) -> RandResult<()>
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.
Sourcepub fn generate_poisson_f32(
&mut self,
output: &mut DeviceBuffer<f32>,
lambda: f64,
) -> RandResult<()>
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.
Sourcepub fn generate_u32(&mut self, output: &mut DeviceBuffer<u32>) -> RandResult<()>
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.