2013-02-24 12:57:16 -06:00
|
|
|
// xfail-pretty
|
|
|
|
|
2012-12-10 19:32:48 -06:00
|
|
|
// Copyright 2012 The Rust Project Developers. See the COPYRIGHT
|
|
|
|
// file at the top-level directory of this distribution and at
|
|
|
|
// http://rust-lang.org/COPYRIGHT.
|
|
|
|
//
|
|
|
|
// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
|
|
|
|
// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
|
|
|
|
// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
|
|
|
|
// option. This file may not be copied, modified, or distributed
|
|
|
|
// except according to those terms.
|
|
|
|
|
2013-01-31 19:12:29 -06:00
|
|
|
#[allow(deprecated_mode)];
|
2012-09-18 21:25:59 -05:00
|
|
|
|
2012-09-18 20:49:23 -05:00
|
|
|
/*!
|
2012-05-14 18:54:43 -05:00
|
|
|
|
2012-06-18 15:34:15 -05:00
|
|
|
An implementation of the Graph500 Breadth First Search problem in Rust.
|
2012-05-14 18:54:43 -05:00
|
|
|
|
|
|
|
*/
|
|
|
|
|
2012-09-11 19:46:20 -05:00
|
|
|
extern mod std;
|
2012-09-05 14:32:05 -05:00
|
|
|
use std::arc;
|
|
|
|
use std::time;
|
|
|
|
use std::deque::Deque;
|
|
|
|
use std::par;
|
2013-04-03 08:28:36 -05:00
|
|
|
use core::hashmap::{HashMap, HashSet};
|
2012-12-28 21:57:18 -06:00
|
|
|
use core::int::abs;
|
2013-03-14 13:22:14 -05:00
|
|
|
use core::rand::RngUtil;
|
2012-05-14 18:54:43 -05:00
|
|
|
|
|
|
|
type node_id = i64;
|
2012-06-29 18:26:56 -05:00
|
|
|
type graph = ~[~[node_id]];
|
|
|
|
type bfs_result = ~[node_id];
|
2012-05-14 18:54:43 -05:00
|
|
|
|
2012-06-29 18:26:56 -05:00
|
|
|
fn make_edges(scale: uint, edgefactor: uint) -> ~[(node_id, node_id)] {
|
2013-04-23 09:00:43 -05:00
|
|
|
let r = rand::XorShiftRng::new();
|
2012-05-14 18:54:43 -05:00
|
|
|
|
2013-04-23 09:00:43 -05:00
|
|
|
fn choose_edge<R: rand::Rng>(i: node_id, j: node_id, scale: uint, r: &R)
|
2012-05-14 18:54:43 -05:00
|
|
|
-> (node_id, node_id) {
|
|
|
|
|
|
|
|
let A = 0.57;
|
|
|
|
let B = 0.19;
|
|
|
|
let C = 0.19;
|
2013-02-01 01:13:36 -06:00
|
|
|
|
2012-05-14 18:54:43 -05:00
|
|
|
if scale == 0u {
|
|
|
|
(i, j)
|
|
|
|
}
|
|
|
|
else {
|
2012-05-22 17:29:49 -05:00
|
|
|
let i = i * 2i64;
|
|
|
|
let j = j * 2i64;
|
2012-05-14 18:54:43 -05:00
|
|
|
let scale = scale - 1u;
|
2013-02-01 01:13:36 -06:00
|
|
|
|
2013-04-24 07:29:19 -05:00
|
|
|
let x = r.gen::<float>();
|
2012-05-14 18:54:43 -05:00
|
|
|
|
|
|
|
if x < A {
|
|
|
|
choose_edge(i, j, scale, r)
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
let x = x - A;
|
|
|
|
if x < B {
|
2012-05-22 17:29:49 -05:00
|
|
|
choose_edge(i + 1i64, j, scale, r)
|
2012-05-14 18:54:43 -05:00
|
|
|
}
|
|
|
|
else {
|
|
|
|
let x = x - B;
|
|
|
|
if x < C {
|
2012-05-22 17:29:49 -05:00
|
|
|
choose_edge(i, j + 1i64, scale, r)
|
2012-05-14 18:54:43 -05:00
|
|
|
}
|
|
|
|
else {
|
2012-05-22 17:29:49 -05:00
|
|
|
choose_edge(i + 1i64, j + 1i64, scale, r)
|
2012-05-14 18:54:43 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-06-30 18:19:07 -05:00
|
|
|
do vec::from_fn((1u << scale) * edgefactor) |_i| {
|
2013-04-23 09:00:43 -05:00
|
|
|
choose_edge(0i64, 0i64, scale, &r)
|
2012-05-14 18:54:43 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-06-29 18:26:56 -05:00
|
|
|
fn make_graph(N: uint, edges: ~[(node_id, node_id)]) -> graph {
|
2013-03-23 20:22:00 -05:00
|
|
|
let mut graph = do vec::from_fn(N) |_i| {
|
2013-04-03 08:28:36 -05:00
|
|
|
HashSet::new()
|
2012-05-22 17:29:49 -05:00
|
|
|
};
|
2012-05-14 18:54:43 -05:00
|
|
|
|
2012-09-18 23:41:13 -05:00
|
|
|
do vec::each(edges) |e| {
|
2012-09-18 23:41:37 -05:00
|
|
|
match *e {
|
|
|
|
(i, j) => {
|
2013-03-23 20:22:00 -05:00
|
|
|
graph[i].insert(j);
|
|
|
|
graph[j].insert(i);
|
2012-09-18 23:41:37 -05:00
|
|
|
}
|
|
|
|
}
|
2012-05-14 18:54:43 -05:00
|
|
|
true
|
|
|
|
}
|
|
|
|
|
2013-03-23 20:22:00 -05:00
|
|
|
do vec::map_consume(graph) |mut v| {
|
|
|
|
let mut vec = ~[];
|
|
|
|
do v.consume |i| {
|
|
|
|
vec.push(i);
|
|
|
|
}
|
|
|
|
vec
|
2012-05-16 17:45:21 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-03-10 10:02:16 -05:00
|
|
|
fn gen_search_keys(graph: &[~[node_id]], n: uint) -> ~[node_id] {
|
2013-04-03 08:28:36 -05:00
|
|
|
let mut keys = HashSet::new();
|
2013-04-23 09:00:43 -05:00
|
|
|
let r = rand::rng();
|
2012-05-16 17:45:21 -05:00
|
|
|
|
2013-02-02 01:52:01 -06:00
|
|
|
while keys.len() < n {
|
2012-05-22 17:29:49 -05:00
|
|
|
let k = r.gen_uint_range(0u, graph.len());
|
2012-05-16 17:45:21 -05:00
|
|
|
|
2012-06-30 18:19:07 -05:00
|
|
|
if graph[k].len() > 0u && vec::any(graph[k], |i| {
|
2012-09-28 00:20:47 -05:00
|
|
|
*i != k as node_id
|
2012-06-26 15:55:56 -05:00
|
|
|
}) {
|
2013-03-23 20:22:00 -05:00
|
|
|
keys.insert(k as node_id);
|
2012-05-16 17:45:21 -05:00
|
|
|
}
|
2012-05-16 17:03:03 -05:00
|
|
|
}
|
2013-03-23 20:22:00 -05:00
|
|
|
let mut vec = ~[];
|
|
|
|
do keys.consume |i| {
|
|
|
|
vec.push(i);
|
|
|
|
}
|
|
|
|
return vec;
|
2012-05-14 18:54:43 -05:00
|
|
|
}
|
|
|
|
|
2012-07-04 16:53:12 -05:00
|
|
|
/**
|
|
|
|
* Returns a vector of all the parents in the BFS tree rooted at key.
|
|
|
|
*
|
|
|
|
* Nodes that are unreachable have a parent of -1.
|
|
|
|
*/
|
2012-05-15 17:21:35 -05:00
|
|
|
fn bfs(graph: graph, key: node_id) -> bfs_result {
|
2013-02-04 13:07:36 -06:00
|
|
|
let mut marks : ~[node_id]
|
2013-01-30 22:06:32 -06:00
|
|
|
= vec::from_elem(vec::len(graph), -1i64);
|
2012-05-14 18:54:43 -05:00
|
|
|
|
2013-02-16 16:55:55 -06:00
|
|
|
let mut q = Deque::new();
|
2012-05-14 18:54:43 -05:00
|
|
|
|
2013-02-16 16:55:55 -06:00
|
|
|
q.add_back(key);
|
2012-05-14 18:54:43 -05:00
|
|
|
marks[key] = key;
|
|
|
|
|
2013-02-16 16:55:55 -06:00
|
|
|
while !q.is_empty() {
|
|
|
|
let t = q.pop_front();
|
2012-05-14 18:54:43 -05:00
|
|
|
|
2012-06-30 18:19:07 -05:00
|
|
|
do graph[t].each() |k| {
|
2012-09-19 18:55:01 -05:00
|
|
|
if marks[*k] == -1i64 {
|
|
|
|
marks[*k] = t;
|
2013-02-16 16:55:55 -06:00
|
|
|
q.add_back(*k);
|
2012-05-14 18:54:43 -05:00
|
|
|
}
|
|
|
|
true
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
2013-02-15 04:44:18 -06:00
|
|
|
marks
|
2012-05-14 18:54:43 -05:00
|
|
|
}
|
|
|
|
|
2012-07-04 16:53:12 -05:00
|
|
|
/**
|
|
|
|
* Another version of the bfs function.
|
|
|
|
*
|
|
|
|
* This one uses the same algorithm as the parallel one, just without
|
|
|
|
* using the parallel vector operators.
|
|
|
|
*/
|
2012-05-16 17:52:00 -05:00
|
|
|
fn bfs2(graph: graph, key: node_id) -> bfs_result {
|
|
|
|
// This works by doing functional updates of a color vector.
|
|
|
|
|
|
|
|
enum color {
|
|
|
|
white,
|
|
|
|
// node_id marks which node turned this gray/black.
|
|
|
|
// the node id later becomes the parent.
|
|
|
|
gray(node_id),
|
|
|
|
black(node_id)
|
|
|
|
};
|
|
|
|
|
2012-06-30 18:19:07 -05:00
|
|
|
let mut colors = do vec::from_fn(graph.len()) |i| {
|
2012-05-16 17:52:00 -05:00
|
|
|
if i as node_id == key {
|
|
|
|
gray(key)
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
white
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2012-09-28 00:20:47 -05:00
|
|
|
fn is_gray(c: &color) -> bool {
|
|
|
|
match *c {
|
2012-08-03 21:59:04 -05:00
|
|
|
gray(_) => { true }
|
|
|
|
_ => { false }
|
2012-05-16 17:52:00 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-09-19 00:44:34 -05:00
|
|
|
let mut i = 0;
|
2012-05-16 17:52:00 -05:00
|
|
|
while vec::any(colors, is_gray) {
|
|
|
|
// Do the BFS.
|
2013-03-08 14:39:42 -06:00
|
|
|
info!("PBFS iteration %?", i);
|
2012-09-19 00:44:34 -05:00
|
|
|
i += 1;
|
2012-06-30 18:19:07 -05:00
|
|
|
colors = do colors.mapi() |i, c| {
|
2012-09-21 20:43:30 -05:00
|
|
|
let c : color = *c;
|
2012-08-06 14:34:08 -05:00
|
|
|
match c {
|
2012-08-03 21:59:04 -05:00
|
|
|
white => {
|
2012-05-16 17:52:00 -05:00
|
|
|
let i = i as node_id;
|
2012-08-28 17:54:45 -05:00
|
|
|
|
2012-12-14 17:57:59 -06:00
|
|
|
let neighbors = copy graph[i];
|
2012-08-28 17:54:45 -05:00
|
|
|
|
2012-05-16 17:52:00 -05:00
|
|
|
let mut color = white;
|
|
|
|
|
2012-06-30 18:19:07 -05:00
|
|
|
do neighbors.each() |k| {
|
2012-09-28 00:20:47 -05:00
|
|
|
if is_gray(&colors[*k]) {
|
2012-09-19 18:55:01 -05:00
|
|
|
color = gray(*k);
|
2012-05-16 17:52:00 -05:00
|
|
|
false
|
|
|
|
}
|
|
|
|
else { true }
|
|
|
|
};
|
|
|
|
|
|
|
|
color
|
|
|
|
}
|
2012-08-03 21:59:04 -05:00
|
|
|
gray(parent) => { black(parent) }
|
|
|
|
black(parent) => { black(parent) }
|
2012-05-16 17:52:00 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Convert the results.
|
2012-06-30 18:19:07 -05:00
|
|
|
do vec::map(colors) |c| {
|
2012-09-21 20:43:30 -05:00
|
|
|
match *c {
|
2012-08-03 21:59:04 -05:00
|
|
|
white => { -1i64 }
|
|
|
|
black(parent) => { parent }
|
2013-02-11 21:26:38 -06:00
|
|
|
_ => { fail!(~"Found remaining gray nodes in BFS") }
|
2012-05-16 17:52:00 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-07-04 16:53:12 -05:00
|
|
|
/// A parallel version of the bfs function.
|
2012-08-26 21:15:10 -05:00
|
|
|
fn pbfs(&&graph: arc::ARC<graph>, key: node_id) -> bfs_result {
|
2012-05-16 17:03:03 -05:00
|
|
|
// This works by doing functional updates of a color vector.
|
|
|
|
|
|
|
|
enum color {
|
|
|
|
white,
|
|
|
|
// node_id marks which node turned this gray/black.
|
|
|
|
// the node id later becomes the parent.
|
|
|
|
gray(node_id),
|
|
|
|
black(node_id)
|
|
|
|
};
|
|
|
|
|
2012-08-28 17:54:45 -05:00
|
|
|
let graph_vec = arc::get(&graph); // FIXME #3387 requires this temp
|
|
|
|
let mut colors = do vec::from_fn(graph_vec.len()) |i| {
|
2012-05-16 17:03:03 -05:00
|
|
|
if i as node_id == key {
|
|
|
|
gray(key)
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
white
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2012-05-21 18:26:28 -05:00
|
|
|
#[inline(always)]
|
2012-09-28 20:26:21 -05:00
|
|
|
fn is_gray(c: &color) -> bool {
|
|
|
|
match *c {
|
2012-08-03 21:59:04 -05:00
|
|
|
gray(_) => { true }
|
|
|
|
_ => { false }
|
2012-05-16 17:03:03 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-01-31 19:12:29 -06:00
|
|
|
fn is_gray_factory() -> ~fn(c: &color) -> bool {
|
|
|
|
let r: ~fn(c: &color) -> bool = is_gray;
|
|
|
|
r
|
|
|
|
}
|
|
|
|
|
2012-09-28 20:29:31 -05:00
|
|
|
let mut i = 0;
|
2013-01-31 19:12:29 -06:00
|
|
|
while par::any(colors, is_gray_factory) {
|
2012-05-16 17:03:03 -05:00
|
|
|
// Do the BFS.
|
2013-03-08 14:39:42 -06:00
|
|
|
info!("PBFS iteration %?", i);
|
2012-09-28 20:29:31 -05:00
|
|
|
i += 1;
|
2012-05-18 16:39:58 -05:00
|
|
|
let old_len = colors.len();
|
2012-05-25 17:29:27 -05:00
|
|
|
|
2013-02-15 04:44:18 -06:00
|
|
|
let color = arc::ARC(colors);
|
2012-05-30 17:18:45 -05:00
|
|
|
|
2012-08-28 17:54:45 -05:00
|
|
|
let color_vec = arc::get(&color); // FIXME #3387 requires this temp
|
2013-01-31 19:12:29 -06:00
|
|
|
colors = do par::mapi(*color_vec) {
|
2012-05-30 17:18:45 -05:00
|
|
|
let colors = arc::clone(&color);
|
|
|
|
let graph = arc::clone(&graph);
|
2013-03-01 17:55:31 -06:00
|
|
|
let result: ~fn(+x: uint, +y: &color) -> color = |i, c| {
|
2012-05-30 17:18:45 -05:00
|
|
|
let colors = arc::get(&colors);
|
|
|
|
let graph = arc::get(&graph);
|
2013-01-31 19:12:29 -06:00
|
|
|
match *c {
|
2012-08-03 21:59:04 -05:00
|
|
|
white => {
|
2012-05-30 17:18:45 -05:00
|
|
|
let i = i as node_id;
|
2012-08-28 17:54:45 -05:00
|
|
|
|
2012-12-14 17:57:59 -06:00
|
|
|
let neighbors = copy graph[i];
|
2012-08-28 17:54:45 -05:00
|
|
|
|
2012-05-30 17:18:45 -05:00
|
|
|
let mut color = white;
|
2012-08-28 17:54:45 -05:00
|
|
|
|
2012-06-30 18:19:07 -05:00
|
|
|
do neighbors.each() |k| {
|
2012-09-28 20:29:31 -05:00
|
|
|
if is_gray(&colors[*k]) {
|
2012-09-19 18:55:01 -05:00
|
|
|
color = gray(*k);
|
2012-05-30 17:18:45 -05:00
|
|
|
false
|
|
|
|
}
|
|
|
|
else { true }
|
2012-05-21 18:26:28 -05:00
|
|
|
};
|
2012-05-30 17:18:45 -05:00
|
|
|
color
|
|
|
|
}
|
2012-08-03 21:59:04 -05:00
|
|
|
gray(parent) => { black(parent) }
|
|
|
|
black(parent) => { black(parent) }
|
2012-05-30 17:18:45 -05:00
|
|
|
}
|
2013-03-01 17:55:31 -06:00
|
|
|
};
|
|
|
|
result
|
2012-05-18 16:39:58 -05:00
|
|
|
};
|
2013-03-28 20:39:09 -05:00
|
|
|
assert!((colors.len() == old_len));
|
2012-05-16 17:03:03 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
// Convert the results.
|
2013-01-31 19:12:29 -06:00
|
|
|
do par::map(colors) {
|
2013-03-01 17:55:31 -06:00
|
|
|
let result: ~fn(c: &color) -> i64 = |c| {
|
2013-01-31 19:12:29 -06:00
|
|
|
match *c {
|
|
|
|
white => { -1i64 }
|
|
|
|
black(parent) => { parent }
|
2013-02-11 21:26:38 -06:00
|
|
|
_ => { fail!(~"Found remaining gray nodes in BFS") }
|
2013-01-31 19:12:29 -06:00
|
|
|
}
|
2013-03-01 17:55:31 -06:00
|
|
|
};
|
|
|
|
result
|
2012-05-16 17:03:03 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-07-04 16:53:12 -05:00
|
|
|
/// Performs at least some of the validation in the Graph500 spec.
|
2013-02-01 01:13:36 -06:00
|
|
|
fn validate(edges: ~[(node_id, node_id)],
|
2012-05-15 17:21:35 -05:00
|
|
|
root: node_id, tree: bfs_result) -> bool {
|
|
|
|
// There are 5 things to test. Below is code for each of them.
|
|
|
|
|
|
|
|
// 1. The BFS tree is a tree and does not contain cycles.
|
|
|
|
//
|
|
|
|
// We do this by iterating over the tree, and tracing each of the
|
|
|
|
// parent chains back to the root. While we do this, we also
|
|
|
|
// compute the levels for each node.
|
|
|
|
|
2013-03-08 14:39:42 -06:00
|
|
|
info!(~"Verifying tree structure...");
|
2012-05-15 17:21:35 -05:00
|
|
|
|
|
|
|
let mut status = true;
|
2012-06-30 18:19:07 -05:00
|
|
|
let level = do tree.map() |parent| {
|
2012-09-21 20:43:30 -05:00
|
|
|
let mut parent = *parent;
|
2012-06-29 18:26:56 -05:00
|
|
|
let mut path = ~[];
|
2012-05-15 17:21:35 -05:00
|
|
|
|
2012-05-22 17:29:49 -05:00
|
|
|
if parent == -1i64 {
|
2012-05-15 17:21:35 -05:00
|
|
|
// This node was not in the tree.
|
|
|
|
-1
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
while parent != root {
|
2012-09-28 00:20:47 -05:00
|
|
|
if vec::contains(path, &parent) {
|
2012-05-15 17:21:35 -05:00
|
|
|
status = false;
|
|
|
|
}
|
|
|
|
|
2012-09-26 19:33:34 -05:00
|
|
|
path.push(parent);
|
2012-05-15 17:21:35 -05:00
|
|
|
parent = tree[parent];
|
|
|
|
}
|
|
|
|
|
|
|
|
// The length of the path back to the root is the current
|
|
|
|
// level.
|
|
|
|
path.len() as int
|
|
|
|
}
|
|
|
|
};
|
2013-02-01 01:13:36 -06:00
|
|
|
|
2012-08-01 19:30:05 -05:00
|
|
|
if !status { return status }
|
2012-05-15 17:21:35 -05:00
|
|
|
|
|
|
|
// 2. Each tree edge connects vertices whose BFS levels differ by
|
|
|
|
// exactly one.
|
|
|
|
|
2013-03-08 14:39:42 -06:00
|
|
|
info!(~"Verifying tree edges...");
|
2012-05-15 17:21:35 -05:00
|
|
|
|
2012-06-30 18:19:07 -05:00
|
|
|
let status = do tree.alli() |k, parent| {
|
2012-09-28 00:20:47 -05:00
|
|
|
if *parent != root && *parent != -1i64 {
|
|
|
|
level[*parent] == level[k] - 1
|
2012-05-15 17:21:35 -05:00
|
|
|
}
|
|
|
|
else {
|
|
|
|
true
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2012-08-01 19:30:05 -05:00
|
|
|
if !status { return status }
|
2012-05-15 17:21:35 -05:00
|
|
|
|
|
|
|
// 3. Every edge in the input list has vertices with levels that
|
|
|
|
// differ by at most one or that both are not in the BFS tree.
|
|
|
|
|
2013-03-08 14:39:42 -06:00
|
|
|
info!(~"Verifying graph edges...");
|
2012-05-15 17:21:35 -05:00
|
|
|
|
2012-06-30 18:19:07 -05:00
|
|
|
let status = do edges.all() |e| {
|
2012-09-28 19:04:39 -05:00
|
|
|
let (u, v) = *e;
|
2012-05-15 17:21:35 -05:00
|
|
|
|
|
|
|
abs(level[u] - level[v]) <= 1
|
|
|
|
};
|
|
|
|
|
2013-02-01 01:13:36 -06:00
|
|
|
if !status { return status }
|
2012-05-15 17:21:35 -05:00
|
|
|
|
|
|
|
// 4. The BFS tree spans an entire connected component's vertices.
|
|
|
|
|
|
|
|
// This is harder. We'll skip it for now...
|
|
|
|
|
|
|
|
// 5. A node and its parent are joined by an edge of the original
|
|
|
|
// graph.
|
|
|
|
|
2013-03-08 14:39:42 -06:00
|
|
|
info!(~"Verifying tree and graph edges...");
|
2012-05-15 17:21:35 -05:00
|
|
|
|
2013-01-31 19:12:29 -06:00
|
|
|
let status = do par::alli(tree) {
|
|
|
|
let edges = copy edges;
|
2013-03-01 17:55:31 -06:00
|
|
|
let result: ~fn(+x: uint, v: &i64) -> bool = |u, v| {
|
2013-01-31 19:12:29 -06:00
|
|
|
let u = u as node_id;
|
|
|
|
if *v == -1i64 || u == root {
|
|
|
|
true
|
|
|
|
} else {
|
|
|
|
edges.contains(&(u, *v)) || edges.contains(&(*v, u))
|
|
|
|
}
|
2013-03-01 17:55:31 -06:00
|
|
|
};
|
|
|
|
result
|
2012-05-15 17:21:35 -05:00
|
|
|
};
|
|
|
|
|
2013-02-01 01:13:36 -06:00
|
|
|
if !status { return status }
|
2012-05-15 17:21:35 -05:00
|
|
|
|
|
|
|
// If we get through here, all the tests passed!
|
|
|
|
true
|
|
|
|
}
|
|
|
|
|
2012-10-03 21:16:27 -05:00
|
|
|
fn main() {
|
|
|
|
let args = os::args();
|
2012-07-14 00:57:48 -05:00
|
|
|
let args = if os::getenv(~"RUST_BENCH").is_some() {
|
|
|
|
~[~"", ~"15", ~"48"]
|
2012-09-19 00:44:34 -05:00
|
|
|
} else if args.len() <= 1 {
|
2012-07-14 00:57:48 -05:00
|
|
|
~[~"", ~"10", ~"16"]
|
2012-05-24 00:53:50 -05:00
|
|
|
} else {
|
|
|
|
args
|
|
|
|
};
|
|
|
|
|
|
|
|
let scale = uint::from_str(args[1]).get();
|
|
|
|
let num_keys = uint::from_str(args[2]).get();
|
2012-05-16 17:45:21 -05:00
|
|
|
let do_validate = false;
|
2012-05-30 17:45:29 -05:00
|
|
|
let do_sequential = true;
|
2012-05-14 18:54:43 -05:00
|
|
|
|
|
|
|
let start = time::precise_time_s();
|
2012-09-19 00:44:34 -05:00
|
|
|
let edges = make_edges(scale, 16);
|
2012-05-14 18:54:43 -05:00
|
|
|
let stop = time::precise_time_s();
|
|
|
|
|
2012-08-22 19:24:52 -05:00
|
|
|
io::stdout().write_line(fmt!("Generated %? edges in %? seconds.",
|
|
|
|
vec::len(edges), stop - start));
|
2012-05-14 18:54:43 -05:00
|
|
|
|
|
|
|
let start = time::precise_time_s();
|
2012-12-06 17:56:52 -06:00
|
|
|
let graph = make_graph(1 << scale, copy edges);
|
2012-05-14 18:54:43 -05:00
|
|
|
let stop = time::precise_time_s();
|
|
|
|
|
2012-09-19 00:44:34 -05:00
|
|
|
let mut total_edges = 0;
|
2012-09-18 23:41:13 -05:00
|
|
|
vec::each(graph, |edges| { total_edges += edges.len(); true });
|
2012-05-14 18:54:43 -05:00
|
|
|
|
2012-08-22 19:24:52 -05:00
|
|
|
io::stdout().write_line(fmt!("Generated graph with %? edges in %? seconds.",
|
2012-09-19 00:44:34 -05:00
|
|
|
total_edges / 2,
|
2012-08-22 19:24:52 -05:00
|
|
|
stop - start));
|
2012-05-15 17:21:35 -05:00
|
|
|
|
2012-05-16 17:45:21 -05:00
|
|
|
let mut total_seq = 0.0;
|
|
|
|
let mut total_par = 0.0;
|
|
|
|
|
2012-08-29 16:45:25 -05:00
|
|
|
let graph_arc = arc::ARC(copy graph);
|
2012-05-30 17:32:19 -05:00
|
|
|
|
2012-06-30 18:19:07 -05:00
|
|
|
do gen_search_keys(graph, num_keys).map() |root| {
|
2012-07-14 00:57:48 -05:00
|
|
|
io::stdout().write_line(~"");
|
2012-08-22 19:24:52 -05:00
|
|
|
io::stdout().write_line(fmt!("Search key: %?", root));
|
2012-05-15 17:21:35 -05:00
|
|
|
|
2012-05-16 17:54:50 -05:00
|
|
|
if do_sequential {
|
2012-05-16 17:45:21 -05:00
|
|
|
let start = time::precise_time_s();
|
2012-12-14 17:57:59 -06:00
|
|
|
let bfs_tree = bfs(copy graph, *root);
|
2012-05-16 17:45:21 -05:00
|
|
|
let stop = time::precise_time_s();
|
2013-02-01 01:13:36 -06:00
|
|
|
|
2012-05-16 17:54:50 -05:00
|
|
|
//total_seq += stop - start;
|
2012-05-16 17:52:00 -05:00
|
|
|
|
2012-05-16 17:54:50 -05:00
|
|
|
io::stdout().write_line(
|
2012-08-22 19:24:52 -05:00
|
|
|
fmt!("Sequential BFS completed in %? seconds.",
|
|
|
|
stop - start));
|
2013-02-01 01:13:36 -06:00
|
|
|
|
2012-05-16 17:54:50 -05:00
|
|
|
if do_validate {
|
|
|
|
let start = time::precise_time_s();
|
2013-03-28 20:39:09 -05:00
|
|
|
assert!((validate(copy edges, *root, bfs_tree)));
|
2012-05-16 17:54:50 -05:00
|
|
|
let stop = time::precise_time_s();
|
2013-02-01 01:13:36 -06:00
|
|
|
|
2012-05-16 17:54:50 -05:00
|
|
|
io::stdout().write_line(
|
2012-08-22 19:24:52 -05:00
|
|
|
fmt!("Validation completed in %? seconds.",
|
|
|
|
stop - start));
|
2012-05-16 17:54:50 -05:00
|
|
|
}
|
2013-02-01 01:13:36 -06:00
|
|
|
|
2012-05-16 17:52:00 -05:00
|
|
|
let start = time::precise_time_s();
|
2012-12-14 17:57:59 -06:00
|
|
|
let bfs_tree = bfs2(copy graph, *root);
|
2012-05-16 17:52:00 -05:00
|
|
|
let stop = time::precise_time_s();
|
2013-02-01 01:13:36 -06:00
|
|
|
|
2012-05-16 17:54:50 -05:00
|
|
|
total_seq += stop - start;
|
2013-02-01 01:13:36 -06:00
|
|
|
|
2012-05-16 17:54:50 -05:00
|
|
|
io::stdout().write_line(
|
2012-08-22 19:24:52 -05:00
|
|
|
fmt!("Alternate Sequential BFS completed in %? seconds.",
|
|
|
|
stop - start));
|
2013-02-01 01:13:36 -06:00
|
|
|
|
2012-05-16 17:54:50 -05:00
|
|
|
if do_validate {
|
|
|
|
let start = time::precise_time_s();
|
2013-03-28 20:39:09 -05:00
|
|
|
assert!((validate(copy edges, *root, bfs_tree)));
|
2012-05-16 17:54:50 -05:00
|
|
|
let stop = time::precise_time_s();
|
2013-02-01 01:13:36 -06:00
|
|
|
|
2012-05-16 17:54:50 -05:00
|
|
|
io::stdout().write_line(
|
2012-08-22 19:24:52 -05:00
|
|
|
fmt!("Validation completed in %? seconds.",
|
|
|
|
stop - start));
|
2012-05-16 17:54:50 -05:00
|
|
|
}
|
2012-05-16 17:45:21 -05:00
|
|
|
}
|
2013-02-01 01:13:36 -06:00
|
|
|
|
2012-05-16 17:45:21 -05:00
|
|
|
let start = time::precise_time_s();
|
2012-09-21 20:43:30 -05:00
|
|
|
let bfs_tree = pbfs(graph_arc, *root);
|
2012-05-16 17:45:21 -05:00
|
|
|
let stop = time::precise_time_s();
|
|
|
|
|
|
|
|
total_par += stop - start;
|
|
|
|
|
2012-08-22 19:24:52 -05:00
|
|
|
io::stdout().write_line(fmt!("Parallel BFS completed in %? seconds.",
|
|
|
|
stop - start));
|
2012-05-16 17:45:21 -05:00
|
|
|
|
|
|
|
if do_validate {
|
|
|
|
let start = time::precise_time_s();
|
2013-03-28 20:39:09 -05:00
|
|
|
assert!((validate(copy edges, *root, bfs_tree)));
|
2012-05-16 17:45:21 -05:00
|
|
|
let stop = time::precise_time_s();
|
2013-02-01 01:13:36 -06:00
|
|
|
|
2012-08-22 19:24:52 -05:00
|
|
|
io::stdout().write_line(fmt!("Validation completed in %? seconds.",
|
|
|
|
stop - start));
|
2012-05-16 17:45:21 -05:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2012-07-14 00:57:48 -05:00
|
|
|
io::stdout().write_line(~"");
|
2012-05-16 17:45:21 -05:00
|
|
|
io::stdout().write_line(
|
2012-08-22 19:24:52 -05:00
|
|
|
fmt!("Total sequential: %? \t Total Parallel: %? \t Speedup: %?x",
|
|
|
|
total_seq, total_par, total_seq / total_par));
|
2012-05-15 17:21:35 -05:00
|
|
|
}
|