pijul nest
guest [sign in]

add sharded version of parallel writes; move writes and writes_discard to lib.rs

[?]
2paXZqkGuqYVrdei2n8ZVCtynbH1mwewHrmi87LA9cto
Jan 20, 2022, 1:32 AM
J7KIVYF5AYPZVBJN4F5C6JL7JKT7P6H4KB5QEUOSNTLHGSD47PEAC

Dependencies

Change contents

  • edit in src/lib.rs at line 7
    [2.220]
    [2.220]
    }
    /// Sequentially write `value` into `slice` with the given `indices`
    pub fn writes<T: Copy>(slice: &mut [T], value: T, indices: &[usize]) -> bool {
    for i in indices {
    if *i >= slice.len() {
    return false;
    }
    }
    for i in indices {
    slice[*i] = value;
    }
    true
    }
    /// Sequentially write `value` into `slice` with the given `indices`, discarding
    /// the writes for any out of bound indexes
    pub fn writes_discard<T: Copy>(vs: &mut [T], val: T, indices: &[usize]) {
    for i in indices {
    if *i < vs.len() {
    vs[*i] = val;
    }
    }
  • edit in src/lib.rs at line 54
    [2.865]
    [2.865]
    }
    fn partition(indices: &[usize]) -> [&[usize]; 4] {
    let size = indices.len();
    let shard = size / 4;
    let one = &indices[0..shard];
    let two = &indices[shard..shard * 2];
    let three = &indices[shard * 2..shard * 3];
    let four = &indices[shard * 3..];
    [one, two, three, four]
    }
    /// Write `value` into `slice` with the given `indices` in parallel, discarding
    /// the writes for any out of bound indexes
    pub fn writes_par_shard_discard<T: Copy + Send + Sync>(
    slice: &mut [T],
    value: T,
    indices: &[usize],
    ) {
    let size = slice.len();
    let tref = TmpRef(slice.as_mut_ptr());
    if size <= 4 {
    for v in slice.iter_mut() {
    *v = value;
    }
    } else {
    let shards = partition(indices);
    shards.par_iter().for_each(move |shard| {
    let tmp = tref.clone();
    let slice = unsafe { std::slice::from_raw_parts_mut(tmp.0, size) };
    writes_discard(slice, value, shard);
    });
    }
  • edit in src/lib.rs at line 97
    [2.1204]
    [2.1204]
    let tmp = tref.clone();
  • edit in src/lib.rs at line 99
    [2.1227][2.1227:1263]()
    let tmp = tref.clone();
  • edit in benches/main.rs at line 4
    [2.1535][2.1535:1848]()
    /// Sequentially write `value` into `slice` with the given `indices`
    pub fn writes<T: Copy>(slice: &mut [T], value: T, indices: &[usize]) -> bool {
    for i in indices {
    if *i >= slice.len() {
    return false;
    }
    }
    for i in indices {
    slice[*i] = value;
    }
    true
    }
  • edit in benches/main.rs at line 5
    [2.1849][2.1849:2143]()
    /// Sequentially write `value` into `slice` with the given `indices`, discarding
    /// the writes for any out of bound indexes
    pub fn writes_discard<T: Copy>(vs: &mut [T], val: T, indices: &[usize]) {
    for i in indices {
    if *i < vs.len() {
    vs[*i] = val;
    }
    }
    }
  • edit in benches/main.rs at line 38
    [2.3244]
    [2.3244]
    });
    c.bench_function(&format!("writes_par_shard_discard {}", size), |b| {
    b.iter(|| writes_par_shard_discard(&mut vs, black_box(val), &indices))
  • edit in benches/main.rs at line 77
    [2.4309]
    [2.4309]
    c.bench_function(&format!("writes_par_shard_discard vals {}", size), |b| {
    b.iter(|| {
    for val in vals {
    writes_par_shard_discard(&mut vs, black_box(*val), &indices);
    }
    })
    });