相关疑难解决方法(0)

如何在 Rust 中为不同线程克隆随机数生成器?

我正在研究基于 C++ 代码库(PBRT,如果有人熟悉的话)的光线追踪器的 Rust 实现。C++ 版本定义的类之一是一系列采样器,以减少渲染图像中的噪声。在渲染过程中,每当需要随机数时,该采样器就会被克隆到每个渲染线程中。这就是我在 Rust 中选择的做法,我承认这有点复杂:

#[derive(Clone)]
pub struct PixelSampler {
    samples_1d: Vec<Vec<f64>>,
    samples_2d: Vec<Vec<Point2<f64>>>,
    current_1d_dimension: i32,
    current_2d_dimension: i32,
    rng: rngs::ThreadRng
}
pub enum Samplers {
    StratifiedSampler {x_samples: i64, y_samples: i64, jitter_samples: bool, pixel: PixelSampler },
    ZeroTwoSequenceSampler { pixel: PixelSampler }
}

impl Clone for Samplers {
    fn clone(&self) -> Self {
        match self {
            Samplers::StratifiedSampler { x_samples, y_samples, jitter_samples, pixel } => { 
                Samplers::StratifiedSampler {x_samples: *x_samples,
                                             y_samples: *y_samples,
                                             jitter_samples: *jitter_samples,
                                             pixel: pixel.clone() }
             }
            Samplers::ZeroTwoSequenceSampler { pixel …
Run Code Online (Sandbox Code Playgroud)

multithreading clone rust

6
推荐指数
1
解决办法
2313
查看次数

标签 统计

clone ×1

multithreading ×1

rust ×1