Skip to content
New issue

Have a question about this project? # for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “#”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? # to your account

Implement our own PRNG for rayon-core #571

Merged
merged 2 commits into from
Jun 5, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 0 additions & 6 deletions .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -15,12 +15,6 @@ matrix:
- rust: 1.13.0
os: linux
#if: everything!
before_script:
# rand 0.4.2 requires rust 1.15, and rand-0.3.22 requires rand-0.4 :/
# manually lowering the dependency in rayon-core:
- sed -i -e 's/^rand = .*$/rand = "=0.3.20"/' rayon-core/Cargo.toml
- cargo generate-lockfile
- git checkout rayon-core/Cargo.toml

- rust: stable
os: linux
Expand Down
2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ default-features = false
[dev-dependencies]
docopt = "0.8"
lazy_static = "1"
rand = ">= 0.3, < 0.5"
rand = "0.4"
serde = "1"
serde_derive = "1"

Expand Down
2 changes: 1 addition & 1 deletion rayon-core/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@ keywords = ["parallel", "thread", "concurrency", "join", "performance"]
categories = ["concurrency"]

[dependencies]
rand = ">= 0.3, < 0.5"
num_cpus = "1.2"
libc = "0.2.16"
lazy_static = "1"
Expand All @@ -25,3 +24,4 @@ lazy_static = "1"
version = "0.2.0"

[dev-dependencies]
rand = "0.4"
2 changes: 2 additions & 0 deletions rayon-core/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,8 @@ extern crate crossbeam_deque;
extern crate lazy_static;
extern crate libc;
extern crate num_cpus;

#[cfg(test)]
extern crate rand;

#[macro_use]
Expand Down
64 changes: 48 additions & 16 deletions rayon-core/src/registry.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,14 +7,15 @@ use job::Job;
use internal::task::Task;
use latch::{LatchProbe, Latch, CountLatch, LockLatch, SpinLatch, TickleLatch};
use log::Event::*;
use rand::{self, Rng};
use sleep::Sleep;
use std::any::Any;
use std::cell::{Cell, UnsafeCell};
use std::cell::Cell;
use std::collections::hash_map::DefaultHasher;
use std::hash::Hasher;
use std::sync::{Arc, Mutex, Once, ONCE_INIT};
use std::sync::atomic::{AtomicUsize, ATOMIC_USIZE_INIT, Ordering};
use std::thread;
use std::mem;
use std::u32;
use std::usize;
use unwind;
use util::leak;
Expand Down Expand Up @@ -460,7 +461,7 @@ pub struct WorkerThread {
breadth_first: bool,

/// A weak random number generator.
rng: UnsafeCell<rand::XorShiftRng>,
rng: XorShift64Star,

registry: Arc<Registry>,
}
Expand Down Expand Up @@ -602,17 +603,8 @@ impl WorkerThread {
if num_threads <= 1 {
return None;
}
assert!(num_threads < (u32::MAX as usize),
"we do not support more than u32::MAX worker threads");

let start = {
// OK to use this UnsafeCell because (a) this data is
// confined to current thread, as WorkerThread is not Send
// nor Sync and (b) rand crate will not call back into
// this method.
let rng = &mut *self.rng.get();
rng.next_u32() % num_threads as u32
} as usize;

let start = self.rng.next_usize(num_threads);
(start .. num_threads)
.chain(0 .. start)
.filter(|&i| i != self.index)
Expand Down Expand Up @@ -646,7 +638,7 @@ unsafe fn main_loop(worker: Deque<JobRef>,
worker: worker,
breadth_first: breadth_first,
index: index,
rng: UnsafeCell::new(rand::weak_rng()),
rng: XorShift64Star::new(),
registry: registry.clone(),
};
WorkerThread::set_current(&worker_thread);
Expand Down Expand Up @@ -716,3 +708,43 @@ pub fn in_worker<OP, R>(op: OP) -> R
}
}
}

/// [xorshift*] is a fast pseudorandom number generator which will
/// even tolerate weak seeding, as long as it's not zero.
///
/// [xorshift*]: https://en.wikipedia.org/wiki/Xorshift#xorshift*
struct XorShift64Star {
state: Cell<u64>,
}

impl XorShift64Star {
fn new() -> Self {
// Any non-zero seed will do -- this uses the hash of a global counter.
let mut seed = 0;
while seed == 0 {
let mut hasher = DefaultHasher::new();
static COUNTER: AtomicUsize = ATOMIC_USIZE_INIT;
hasher.write_usize(COUNTER.fetch_add(1, Ordering::Relaxed));
seed = hasher.finish();
}

XorShift64Star {
state: Cell::new(seed),
}
}

fn next(&self) -> u64 {
let mut x = self.state.get();
debug_assert_ne!(x, 0);
x ^= x >> 12;
x ^= x << 25;
x ^= x >> 27;
self.state.set(x);
x.wrapping_mul(0x2545_f491_4f6c_dd1d)
}

/// Return a value from `0..n`.
fn next_usize(&self, n: usize) -> usize {
(self.next() % n as u64) as usize
}
}
2 changes: 1 addition & 1 deletion rayon-demo/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ fixedbitset = "0.1.5"
glium = "0.20"
lazy_static = "1"
odds = "0.3"
rand = ">= 0.3, < 0.5"
rand = "0.4"
regex = "0.2"
serde = "1"
serde_derive = "1"
Expand Down