简体   繁体   中英

Is there a way to convert a ChunkMut<T> from Vec::chunks_mut to a slice &mut [T]?

I'm filling a vector in parallel, but for this generalized question, I've only found hints and no answers.

The code below works, but I want to switch to Rng::fill instead of iterating over each chunk. It might not be possible to have multiple mutable slices inside a single Vec ; I'm not sure.

extern crate rayon;
extern crate rand;
extern crate rand_xoshiro;

use rand::{Rng, SeedableRng};
use rand_xoshiro::Xoshiro256StarStar;
use rayon::prelude::*;
use std::{iter, env};
use std::sync::{Arc, Mutex};

// i16 because I was filling up my RAM for large tests...
fn gen_rand_vec(data: &mut [i16]) {
    let num_threads = rayon::current_num_threads();
    let mut rng = rand::thread_rng();
    let mut prng = Xoshiro256StarStar::from_rng(&mut rng).unwrap();
    // lazy iterator of fast, unique RNGs
    // Arc and Mutex are just so it can be accessed from multiple threads
    let rng_it = Arc::new(Mutex::new(iter::repeat(()).map(|()| {
        let new_prng = prng.clone();
        prng.jump();
        new_prng
    })));
    // generates random numbers for each chunk in parallel
    // par_chunks_mut is parallel version of chunks_mut
    data.par_chunks_mut(data.len() / num_threads).for_each(|chunk| {
        // I used extra braces because it might be required to unlock Mutex. 
        // Not sure.
        let mut prng = { rng_it.lock().unwrap().next().unwrap() };
        for i in chunk.iter_mut() {
            *i = prng.gen_range(-1024, 1024);
        }
    });
}

It turns out that a ChunksMut iterator gives slices. I'm not sure how to glean that from the documentation. I figured it out by reading the source :

#[derive(Debug)]
#[stable(feature = "rust1", since = "1.0.0")]
pub struct ChunksMut<'a, T:'a> {
    v: &'a mut [T],
    chunk_size: usize
}

#[stable(feature = "rust1", since = "1.0.0")]
impl<'a, T> Iterator for ChunksMut<'a, T> {
    type Item = &'a mut [T];

    #[inline]
    fn next(&mut self) -> Option<&'a mut [T]> {
        if self.v.is_empty() {
            None
        } else {
            let sz = cmp::min(self.v.len(), self.chunk_size);
            let tmp = mem::replace(&mut self.v, &mut []);
            let (head, tail) = tmp.split_at_mut(sz);
            self.v = tail;
            Some(head)
        }
}

I guess it's just experience; to others it must be obvious that an iterator of type ChunksMut<T> from Vec<T> returns objects of type [T] . That makes sense now. It just wasn't very clear with the intermediate struct.

pub fn chunks_mut(&mut self, chunk_size: usize) -> ChunksMut<T>
// ...
impl<'a, T> Iterator for ChunksMut<'a, T>

Reading this, it looked like the iterator returned objects of type T , the same as Vec<T>.iter() , which wouldn't make sense.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM