Skip to main content

replay_map_raw_serial

Function replay_map_raw_serial 

Source
pub fn replay_map_raw_serial<D, S, F>(
    dest: &mut RawStridedMut<'_, D>,
    src: &RawStridedRef<'_, S>,
    f: F,
) -> Result<()>
where D: Copy, S: Copy, F: FnMut(&mut D, S),
Expand description

replay_map_raw on the calling thread, for callbacks and elements that are not Send + Sync: f(&mut dest[i], src[i]) for every element, with f an FnMut.

The traversal, the errors and the visit order are those of replay_map_raw under Exec::serial(): both walk the same route through the same leaves. f is called exactly once per logical element, sequentially on the calling thread, column-major over the given axes with axis 0 fastest. The one exception is a destination proven injective whose layout the shared cache-block model tiles: the walk then goes tile by tile, the tile grid and each tile column-major. A destination that is not proven injective is always walked column-major, so an accumulating f sees a repeated slot once per logical index, in that order.

A successful call allocates nothing at any rank; allocations made by f are its own. The wide route uses a rank-bounded amount of the caller’s stack. An error may allocate its payload.

With D = MaybeUninit<T> the destination is initialized only if f writes its slot on every call: a successful return guarantees that f ran exactly once for every reachable destination element, not that it wrote.

§Errors

As replay_copy_raw, before f first runs.

§Examples

A generic copy whose element type need not be Send or Sync, counting its calls through a captured Rc:

use std::cell::Cell;
use std::rc::Rc;
use strided_basic::{replay_map_raw_serial, RawStridedMut, RawStridedRef, Result};

fn copy_generic<T: Copy>(dst: &mut RawStridedMut<'_, T>, src: &RawStridedRef<'_, T>) -> Result<()> {
    replay_map_raw_serial(dst, src, |d, s| *d = s)
}

let stored = [0.0f64, 1.0, 2.0, 3.0, 4.0, 5.0]; // column-major 2 x 3
let src = RawStridedRef::new(&stored, &[2, 3], &[1, 2], 0).unwrap();
let mut out = [0.0f64; 6];
let mut dst = RawStridedMut::new(&mut out, &[2, 3], &[3, 1], 0).unwrap();
copy_generic(&mut dst, &src).unwrap();

let calls = Rc::new(Cell::new(0));
let counter = Rc::clone(&calls);
replay_map_raw_serial(&mut dst, &src, |d, s| {
    counter.set(counter.get() + 1);
    *d += s;
})
.unwrap();
drop(dst);
assert_eq!(calls.get(), 6);
assert_eq!(out, [0.0, 4.0, 8.0, 2.0, 6.0, 10.0]);

An uninitialized destination, filled by a closure that writes every slot:

use core::mem::MaybeUninit;
use strided_basic::{replay_map_raw_serial, RawStridedMut, RawStridedRef};

let src = [1.0f64, 2.0, 3.0];
let mut storage = [MaybeUninit::<f64>::uninit(); 3];
let mut dst = RawStridedMut::new(&mut storage, &[3], &[1], 0).unwrap();
let source = RawStridedRef::new(&src, &[3], &[-1], 2).unwrap();
replay_map_raw_serial(&mut dst, &source, |d: &mut MaybeUninit<f64>, s| {
    d.write(s);
})
.unwrap();
drop(dst);
// SAFETY: the closure writes its slot on every call, and the successful
// replay called it once for every reachable destination slot, which are
// all three slots of `storage`.
let values: Vec<f64> = storage.iter().map(|v| unsafe { v.assume_init() }).collect();
assert_eq!(values, [3.0, 2.0, 1.0]);