Skip to content

Commit

Permalink
formatting
Browse files Browse the repository at this point in the history
  • Loading branch information
daemontus committed Jan 27, 2020
1 parent 201fa33 commit a6dfb44
Show file tree
Hide file tree
Showing 9 changed files with 196 additions and 148 deletions.
197 changes: 110 additions & 87 deletions src/main.rs

Large diffs are not rendered by default.

29 changes: 19 additions & 10 deletions src/scc/algo_components.rs
Original file line number Diff line number Diff line change
@@ -1,24 +1,30 @@
use super::StateSet;
use crate::scc::algo_par_reach::guarded_reach;
use crate::scc::ProgressTracker;
use biodivine_lib_param_bn::async_graph::{AsyncGraph, FwdIterator};
use biodivine_lib_param_bn::bdd_params::BddParams;
use biodivine_lib_std::param_graph::{EvolutionOperator, Graph, Params};
use biodivine_lib_std::IdState;
use std::option::Option::Some;
use crate::scc::ProgressTracker;
use rayon::prelude::*;
use std::option::Option::Some;
use std::sync::atomic::{AtomicBool, Ordering};

pub fn components<F>(graph: &AsyncGraph, progress: &ProgressTracker, cancelled: &AtomicBool, on_component: F)
where
pub fn components<F>(
graph: &AsyncGraph,
progress: &ProgressTracker,
cancelled: &AtomicBool,
on_component: F,
) where
F: Fn(StateSet) -> () + Send + Sync,
{
crossbeam::thread::scope(|scope| {
let num_states = graph.states().count();
let fwd = graph.fwd();
let bwd = graph.bwd();
let initial = StateSet::new_with_fun(num_states, |_| Some(graph.unit_params().clone()));
let sink_pairs: Vec<(IdState, BddParams)> = graph.states().collect::<Vec<_>>()
let sink_pairs: Vec<(IdState, BddParams)> = graph
.states()
.collect::<Vec<_>>()
.par_iter()
.filter_map(|s| {
let has_next = fwd
Expand Down Expand Up @@ -46,7 +52,8 @@ where
for (s, is_sink) in sink_pairs {
sinks.put(s, is_sink);
}
let can_reach_sink = guarded_reach(&bwd, &sinks, &initial, &AtomicBool::new(false), &progress);
let can_reach_sink =
guarded_reach(&bwd, &sinks, &initial, &AtomicBool::new(false), &progress);
// This is not correct - on_component is called with individual components - sinks are multiple different components.
//on_component(sinks); // notify about the sinks we have found
let initial = StateSet::new_with_fun(num_states, |i| {
Expand All @@ -65,7 +72,6 @@ where
queue.push(with_cardinality(initial));

while let Some((universe, universe_cardinality)) = queue.pop() {

if cancelled.load(Ordering::SeqCst) {
return ();
}
Expand All @@ -85,7 +91,8 @@ where
return ();
}

let component_with_pivots = guarded_reach(&bwd, &pivots, &forward, cancelled, &progress);
let component_with_pivots =
guarded_reach(&bwd, &pivots, &forward, cancelled, &progress);

if cancelled.load(Ordering::SeqCst) {
return ();
Expand All @@ -109,7 +116,8 @@ where
});
}

let basins_of_reachable_terminals = guarded_reach(&bwd, &forward, &universe, cancelled, &progress);
let basins_of_reachable_terminals =
guarded_reach(&bwd, &forward, &universe, cancelled, &progress);

if cancelled.load(Ordering::SeqCst) {
return ();
Expand All @@ -128,7 +136,8 @@ where
queue.push(with_cardinality(unreachable_terminals));
}
}
}).unwrap();
})
.unwrap();
}

// Augment a state set with the cardinality of the set
Expand Down
35 changes: 24 additions & 11 deletions src/scc/algo_par_reach.rs
Original file line number Diff line number Diff line change
@@ -1,15 +1,17 @@
use crate::scc::{StateSet, ProgressTracker};
use crate::scc::{ProgressTracker, StateSet};
use biodivine_lib_param_bn::bdd_params::BddParams;
use biodivine_lib_std::param_graph::{EvolutionOperator, InvertibleEvolutionOperator, Params};
use biodivine_lib_std::IdState;
use rayon::prelude::*;
use std::collections::HashSet;
use std::sync::atomic::{AtomicBool, Ordering};

fn all_possible_successors<F>(fwd: &F, set: &HashSet<IdState>) -> HashSet<IdState> where
F: EvolutionOperator<State = IdState, Params = BddParams> + Send + Sync
fn all_possible_successors<F>(fwd: &F, set: &HashSet<IdState>) -> HashSet<IdState>
where
F: EvolutionOperator<State = IdState, Params = BddParams> + Send + Sync,
{
return set.par_iter()
return set
.par_iter()
.flat_map(|s| fwd.step(*s).map(|(t, _)| t).collect::<Vec<_>>())
.collect();
}
Expand All @@ -18,7 +20,10 @@ trait FoldUnion {
fn fold_union(self) -> Option<BddParams>;
}

impl<I> FoldUnion for I where I: Iterator<Item=Option<BddParams>> {
impl<I> FoldUnion for I
where
I: Iterator<Item = Option<BddParams>>,
{
fn fold_union(self) -> Option<BddParams> {
return self.fold(None, |a, b| match (a, b) {
(Some(a), Some(b)) => Some(a.union(&b)),
Expand All @@ -29,9 +34,12 @@ impl<I> FoldUnion for I where I: Iterator<Item=Option<BddParams>> {
}
}

pub fn next_step<F, B>(fwd: &F, initial: &StateSet) -> StateSet where
F: InvertibleEvolutionOperator<State = IdState, Params = BddParams, InvertedOperator = B> + Send + Sync,
B: EvolutionOperator<State = IdState, Params = BddParams> + Send + Sync
pub fn next_step<F, B>(fwd: &F, initial: &StateSet) -> StateSet
where
F: InvertibleEvolutionOperator<State = IdState, Params = BddParams, InvertedOperator = B>
+ Send
+ Sync,
B: EvolutionOperator<State = IdState, Params = BddParams> + Send + Sync,
{
let bwd = fwd.invert();
let items: Vec<(IdState, &BddParams)> = initial.iter().collect();
Expand Down Expand Up @@ -68,7 +76,13 @@ pub fn next_step<F, B>(fwd: &F, initial: &StateSet) -> StateSet where
return result;
}

pub fn guarded_reach<F, B>(fwd: &F, initial: &StateSet, guard: &StateSet, cancelled: &AtomicBool, progress: &ProgressTracker) -> StateSet
pub fn guarded_reach<F, B>(
fwd: &F,
initial: &StateSet,
guard: &StateSet,
cancelled: &AtomicBool,
progress: &ProgressTracker,
) -> StateSet
where
F: InvertibleEvolutionOperator<State = IdState, Params = BddParams, InvertedOperator = B>
+ Send
Expand All @@ -85,9 +99,8 @@ where
}

while !changed.is_empty() {

if cancelled.load(Ordering::SeqCst) {
return result_set; // result is incorrect, but we are cancelled so we don't care...
return result_set; // result is incorrect, but we are cancelled so we don't care...
}

progress.update_last_wave(changed.len());
Expand Down
1 change: 0 additions & 1 deletion src/scc/impl_class.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@ impl Class {
vec.sort();
return Class(vec);
}

}

impl Display for Class {
Expand Down
17 changes: 7 additions & 10 deletions src/scc/impl_classifier.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,9 @@ use crate::scc::algo_components::find_pivots_basic;
use biodivine_lib_param_bn::async_graph::AsyncGraph;
use biodivine_lib_param_bn::bdd_params::BddParams;
use biodivine_lib_std::param_graph::{EvolutionOperator, Graph, Params};
use std::collections::HashMap;
use rayon::prelude::*;
use biodivine_lib_std::IdState;
use rayon::prelude::*;
use std::collections::HashMap;
use std::sync::Mutex;

impl Classifier {
Expand Down Expand Up @@ -36,8 +36,7 @@ impl Classifier {
let not_sink_params = without_sinks.fold_union();
if let Some(not_sink_params) = not_sink_params {
let pivots = find_pivots_basic(&without_sinks);
let mut oscillator =
Oscillator::new_with_pivots(pivots.clone(), graph.empty_params());
let mut oscillator = Oscillator::new_with_pivots(pivots.clone(), graph.empty_params());

let mut disorder = graph.empty_params();
let mut params_to_match = not_sink_params.clone();
Expand Down Expand Up @@ -94,7 +93,7 @@ impl Classifier {
original_classes.reverse(); // we need classes from largest to smallest

for class in original_classes {
let class_params= &(*classes)[&class];
let class_params = &(*classes)[&class];
let should_move_up = class_params.intersect(&params);
if !should_move_up.is_empty() {
let extended_class = class.clone_extended(behaviour);
Expand Down Expand Up @@ -130,13 +129,12 @@ impl Classifier {
let fwd = graph.fwd();
let mut result = component.clone();
let data: Vec<(IdState, BddParams)> = component.into_iter().collect();
let processed: Vec<(IdState, BddParams, BddParams)> = data.par_iter()
let processed: Vec<(IdState, BddParams, BddParams)> = data
.par_iter()
.filter_map(|(s, p): &(IdState, BddParams)| {
let has_successor = fwd
.step(*s)
.fold(graph.empty_params(), |a, (_, b)| {
a.union(&b)
});
.fold(graph.empty_params(), |a, (_, b)| a.union(&b));
let is_sink = p.minus(&has_successor);
if is_sink.is_empty() {
None
Expand Down Expand Up @@ -269,7 +267,6 @@ impl Classifier {
return (self.graph.empty_params(), self.graph.empty_params())
}
}*/

}

/// Oscillator partitions the
Expand Down
12 changes: 7 additions & 5 deletions src/scc/impl_progress_tracker.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ use biodivine_lib_param_bn::async_graph::AsyncGraph;
use std::sync::Mutex;

impl ProgressTracker {

pub fn new(graph: &AsyncGraph) -> ProgressTracker {
let unit_cardinality = graph.unit_params().cardinality();
let num_states = graph.num_states() as f64;
Expand All @@ -12,7 +11,7 @@ impl ProgressTracker {
total: graph_size,
remaining: Mutex::new(graph_size),
last_wave: Mutex::new(0),
}
};
}

pub fn update_last_wave(&self, value: usize) {
Expand All @@ -37,7 +36,10 @@ impl ProgressTracker {
}

pub fn get_percent_string(&self) -> String {
return format!("{:.2}% (Reachability remaining: {})", 100.0 - (self.get_progress() * 100.0), { *self.last_wave.lock().unwrap() });
return format!(
"{:.2}% (Reachability remaining: {})",
100.0 - (self.get_progress() * 100.0),
{ *self.last_wave.lock().unwrap() }
);
}

}
}
33 changes: 19 additions & 14 deletions src/scc/impl_state_set.rs
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
use super::{StateSet, StateSetIterator};
use crate::scc::StateSetIntoIterator;
use biodivine_lib_param_bn::bdd_params::BddParams;
use biodivine_lib_std::param_graph::Params;
use biodivine_lib_std::IdState;
use crate::scc::StateSetIntoIterator;
use rayon::prelude::*;

impl StateSet {
Expand Down Expand Up @@ -116,7 +116,7 @@ impl StateSet {
} else {
Some(result)
}
},
}
_ => None,
});
}
Expand All @@ -139,7 +139,7 @@ impl StateSet {
} else {
Some(result)
}
},
}
(Some(a), _) => Some(a.clone()),
_ => None,
});
Expand All @@ -165,7 +165,10 @@ impl StateSet {
}

pub fn into_iter(self) -> StateSetIntoIterator {
return StateSetIntoIterator { set: self.0.into_iter(), next: 0 };
return StateSetIntoIterator {
set: self.0.into_iter(),
next: 0,
};
}

pub fn minus_in_place(&mut self, other: &Self) {
Expand All @@ -176,22 +179,24 @@ impl StateSet {
let new = current.minus(other);
*current = new;
}
} // else its none, dont minus anything
} // else its none, dont minus anything
}
}

/// Intersect all values in this state set with the given params (in parallel).
pub fn par_restrict_to_params(&mut self, params: &BddParams) {
self.0.par_iter_mut().for_each(|value: &mut Option<BddParams>| {
if value.is_some() {
let new_params = params.intersect(value.as_ref().unwrap());
if new_params.is_empty() {
*value = None;
} else {
*value = Some(new_params);
self.0
.par_iter_mut()
.for_each(|value: &mut Option<BddParams>| {
if value.is_some() {
let new_params = params.intersect(value.as_ref().unwrap());
if new_params.is_empty() {
*value = None;
} else {
*value = Some(new_params);
}
}
}
});
});
}

pub fn fold_union(&self) -> Option<BddParams> {
Expand Down
6 changes: 3 additions & 3 deletions src/scc/impl_state_set_iterator.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
use super::StateSetIterator;
use crate::scc::StateSetIntoIterator;
use biodivine_lib_param_bn::bdd_params::BddParams;
use biodivine_lib_std::IdState;
use crate::scc::StateSetIntoIterator;

impl<'a> Iterator for StateSetIterator<'a> {
type Item = (IdState, &'a BddParams);
Expand Down Expand Up @@ -32,6 +32,6 @@ impl Iterator for StateSetIntoIterator {
Some((IdState::from(self.next - 1), params))
} else {
None
}
};
}
}
}
Loading

0 comments on commit a6dfb44

Please sign in to comment.