Skip to content

Commit ad292a8

Browse files
committed
Add xorshift to core::rand, which gave a 3x speedup for graph generation in the bfs code. Also, remove trailing white space.
1 parent 09a32ae commit ad292a8

File tree

3 files changed

+34
-4
lines changed

3 files changed

+34
-4
lines changed

src/libcore/rand.rs

+30
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
#[doc = "Random number generation"];
22

33
export rng, seed, seeded_rng, weighted, extensions;
4+
export xorshift, seeded_xorshift;
45

56
enum rctx {}
67

@@ -253,6 +254,35 @@ fn seeded_rng(seed: [u8]) -> rng {
253254
@rand_res(rustrt::rand_new_seeded(seed)) as rng
254255
}
255256

257+
type xorshift_state = {
258+
mut x: u32,
259+
mut y: u32,
260+
mut z: u32,
261+
mut w: u32
262+
};
263+
264+
impl of rng for xorshift_state {
265+
fn next() -> u32 {
266+
let x = self.x;
267+
let mut t = x ^ (x << 11);
268+
self.x = self.y;
269+
self.y = self.z;
270+
self.z = self.w;
271+
let w = self.w;
272+
self.w = w ^ (w >> 19) ^ (t ^ (t >> 8));
273+
self.w
274+
}
275+
}
276+
277+
fn xorshift() -> rng {
278+
// constants taken from http://en.wikipedia.org/wiki/Xorshift
279+
seeded_xorshift(123456789u32, 362436069u32, 521288629u32, 88675123u32)
280+
}
281+
282+
fn seeded_xorshift(x: u32, y: u32, z: u32, w: u32) -> rng {
283+
{mut x: x, mut y: y, mut z: z, mut w: w} as rng
284+
}
285+
256286
#[cfg(test)]
257287
mod tests {
258288

src/libstd/par.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -129,7 +129,7 @@ fn alli<A: copy send>(xs: [A], f: fn~(uint, A) -> bool) -> bool {
129129

130130
#[doc="Returns true if the function holds for any elements in the vector."]
131131
fn any<A: copy send>(xs: [A], f: fn~(A) -> bool) -> bool {
132-
vec::any(map_slices(xs) {||
132+
vec::any(map_slices(xs) {||
133133
fn~(_base : uint, slice: [const A]/&, copy f) -> bool {
134134
vec::any(slice, f)
135135
}

src/test/bench/graph500-bfs.rs

+3-3
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ type graph = [[node_id]];
2121
type bfs_result = [node_id];
2222

2323
fn make_edges(scale: uint, edgefactor: uint) -> [(node_id, node_id)] {
24-
let r = rand::rng();
24+
let r = rand::xorshift();
2525

2626
fn choose_edge(i: node_id, j: node_id, scale: uint, r: rand::rng)
2727
-> (node_id, node_id) {
@@ -247,12 +247,12 @@ fn pbfs(&&graph: arc::arc<graph>, key: node_id) -> bfs_result {
247247
white {
248248
let i = i as node_id;
249249

250-
let neighbors = (*graph)[i];
250+
let neighbors = graph[i];
251251

252252
let mut color = white;
253253

254254
neighbors.each() {|k|
255-
if is_gray((*colors)[k]) {
255+
if is_gray(colors[k]) {
256256
color = gray(k);
257257
false
258258
}

0 commit comments

Comments
 (0)