Created
September 3, 2013 13:46
-
-
Save logicchains/6424159 to your computer and use it in GitHub Desktop.
Rust: undefined scheduler behaviour
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
use std::{os, vec,comm}; | |
static NUM_TA: uint = 1000; | |
static NUM_CORES: uint = 4; | |
static NUM_TB: uint = 50_000; | |
#[deriving(Clone)] | |
struct TypeB { a: uint, b: uint, c: uint, d: uint} | |
#[deriving(Clone)] | |
struct TypeA { TypeBs: ~[TypeB] } | |
fn main() { | |
let prng:u32 = match os::args() { | |
[_, seed, .._] => FromStr::from_str(seed).unwrap_or_default(18), | |
_ => 18 | |
}; | |
printf!("The random seed is: %?\n", prng); | |
let (port, chan): (Port<~[TypeA]>, Chan<~[TypeA]>) = stream(); | |
let chan = comm::SharedChan::new(chan); | |
for val in range(1,NUM_CORES+1){ | |
let child_chan = chan.clone(); | |
let newprng = prng.clone() * ( (val*val) as u32 ); | |
do spawn { | |
let mut thisprng = newprng.clone(); | |
printf!("Starting thread number %? with seed: %? \n", val, thisprng); | |
child_chan.send( make_TypeAs(NUM_TA/NUM_CORES, &mut thisprng) ); | |
} | |
} | |
let mut ls: ~[TypeA] = ~[]; | |
for _ in range(0,NUM_CORES){ | |
ls = vec::append(ls,port.recv() ); | |
} | |
} | |
fn make_TypeAs(n: uint, gen:&mut u32) -> ~[TypeA] { | |
let TA: ~[TypeA] = do vec::from_fn(n) |_| { | |
let TB = make_TypeBs(gen); | |
TypeA { TypeBs: TB } | |
}; | |
TA | |
} | |
fn make_TypeBs(prng: &mut u32) -> ~[TypeB] { | |
let TB: ~[TypeB] = do vec::from_fn(NUM_TB) |_| { | |
let a = rand_uint(prng)*rand_uint(prng)/rand_uint(prng); | |
let b = rand_uint(prng)*rand_uint(prng)/rand_uint(prng); | |
let c = rand_uint(prng)*rand_uint(prng)/rand_uint(prng); | |
let d = rand_uint(prng)*rand_uint(prng)/rand_uint(prng); | |
TypeB { a: a, b: b, c: c, d: d } | |
}; | |
TB | |
} | |
fn rand_uint(prng: &mut u32) -> uint { | |
*prng += *prng; | |
*prng ^= 1; | |
if (*prng as i32) < 0 { | |
*prng ^= 0x88888eef; | |
} | |
*prng as uint | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment