1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
#![feature(scoped)]
#![cfg_attr(test, feature(test))]
use std::sync::{Arc};
use std::thread;
use std::sync::atomic::{AtomicUsize, Ordering};
use std::mem;
pub fn pmap<A, B, F>(array:  &[A], out:&mut [B], max: usize, f: F)
where F: Send+Sync, F: Fn(A)->B,  A: Send+Sync+Copy,  B: Send+Sync
  {
    assert!(out.len() >= array.len());
    let idx = &AtomicUsize::new(0);
    let len = array.len();
    let array = Arc::new(array);
    let f = Arc::new(f);
    {
        let mut threads: Vec<thread::JoinGuard<bool>> = Vec::new();
        for _ in 0..max {
            let data: &mut [B] = unsafe{ mem::transmute_copy(&out) };
            let f = f.clone();
            let array = array.clone();
            let trd = thread::scoped(move|| {
                loop {
                    let i = idx.fetch_add(1, Ordering::Relaxed);
                    if i >= len {
                        return true;
                    }
                    let in_data = array[i];
                    let out_data = f(in_data);
                    data[i] = out_data;
                    //println!("inside {} {} {:?}", i, j, data.deref());
                }
            });
            threads.push(trd);
        }
    }
}

#[cfg(test)]
mod tests {
    extern crate test;
    use super::pmap;


    #[test]
    fn it_works() {
        let behind = [1usize, 2, 3, 4, 5];
        let mut thing = [0usize; 5];
        pmap(&behind, &mut thing, 2, inc);
        let out = [1usize, 2, 6,24,120];
        assert_eq!(thing, out);
    }
    fn inc (x: usize) -> usize {
      inc2(1, x)
    }
    fn inc2 (acc: usize, x: usize) -> usize {
        match x {
          1=> acc,
          _=> inc2(acc * x, x - 1)
      }
    }
}


#[cfg(test)]
mod bench {
    extern crate test;
    extern crate rand;
    use super::pmap;
    use self::test::Bencher;
    use self::rand::Rng;
    fn fill(array:  &mut [u8]) {
      let mut rng = rand::chacha::ChaChaRng::new_unseeded();
      let len = array.len();
      for i in 0..len {
        array[i] = rng.gen::<u8>();
      }
    }
    #[bench]
    fn one(bh: & mut Bencher) {
        let mut behind = [0u8; 8000];
        let mut thing = [0u8;8000];
        fill(&mut behind);
        bh.iter( || {
            pmap(&behind, &mut thing, 1, inc);
        });
        bh.bytes = behind.len() as u64;
    }
    #[bench]
    fn two(bh: & mut Bencher) {
        let mut behind = [0u8; 8000];
        let mut thing = [0u8;8000];
        fill(&mut behind);
        bh.iter( || {
            pmap(&behind, &mut thing, 2, inc);
        });
        bh.bytes = behind.len() as u64;
    }
    #[bench]
    fn three(bh: & mut Bencher) {
        let mut behind = [0u8; 8000];
        let mut thing = [0u8; 8000];
        fill(&mut behind);
        bh.iter( || {
            pmap(&behind, &mut thing, 3, inc);
        });
        bh.bytes = behind.len() as u64;
    }
    #[bench]
    fn four(bh: & mut Bencher) {
        let mut behind = [0u8; 8000];
        let mut thing = [0u8;8000];
        fill(&mut behind);
        bh.iter( || {
            pmap(&behind, &mut thing, 4, inc);
        });
        bh.bytes = behind.len() as u64;
    }
    #[bench]
    fn six(bh: & mut Bencher) {
        let mut behind = [0u8; 8000];
        let mut thing = [0u8;8000];
        fill(&mut behind);
        bh.iter( || {
            pmap(&behind, &mut thing, 6, inc);
        });
        bh.bytes = behind.len() as u64;
    }
    #[bench]
    fn eight(bh: & mut Bencher) {
        let mut behind = [0u8; 8000];
        let mut thing = [0u8;8000];
        fill(&mut behind);
        bh.iter( || {
            pmap(&behind, &mut thing, 8, inc);
        });
        bh.bytes = behind.len() as u64;
    }
    fn inc (x: u8) -> u8 {
      inc2(1, x)
    }
    fn inc2 (acc: u8, x: u8) -> u8 {
        match x {
          1=> acc,
          _=> inc2(acc * x, x - 1)
      }
    }
}