formatting
This commit is contained in:
parent
19cea83bd8
commit
8b18c1a570
|
@ -0,0 +1,3 @@
|
||||||
|
{
|
||||||
|
"editor.formatOnSave": true,
|
||||||
|
}
|
19
src/lib.rs
19
src/lib.rs
|
@ -1,17 +1,20 @@
|
||||||
mod runner;
|
|
||||||
mod utils;
|
|
||||||
mod spring_model;
|
|
||||||
mod graph;
|
mod graph;
|
||||||
|
mod runner;
|
||||||
|
mod spring_model;
|
||||||
|
mod utils;
|
||||||
|
|
||||||
use pyo3::prelude::*;
|
use pyo3::prelude::*;
|
||||||
|
|
||||||
/// Formats the sum of two numbers as string.
|
/// Formats the sum of two numbers as string.
|
||||||
#[pyfunction(number_of_nodes, edges, "*", iter=500, threads=0)]
|
#[pyfunction(number_of_nodes, edges, "*", iter = 500, threads = 0)]
|
||||||
fn layout_from_edge_list(number_of_nodes: usize, edges: Vec<(u32, u32)>, iter: usize, threads: usize) -> PyResult<Vec<(f32, f32)>> {
|
fn layout_from_edge_list(
|
||||||
|
number_of_nodes: usize,
|
||||||
|
edges: Vec<(u32, u32)>,
|
||||||
|
iter: usize,
|
||||||
|
threads: usize,
|
||||||
|
) -> PyResult<Vec<(f32, f32)>> {
|
||||||
let r = runner::Runner::new(iter, threads);
|
let r = runner::Runner::new(iter, threads);
|
||||||
Ok(
|
Ok(r.layout(number_of_nodes, edges))
|
||||||
r.layout(number_of_nodes, edges)
|
|
||||||
)
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/// A Python module implemented in Rust.
|
/// A Python module implemented in Rust.
|
||||||
|
|
|
@ -1,4 +1,3 @@
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Read Graph data from file
|
* Read Graph data from file
|
||||||
* Format:
|
* Format:
|
||||||
|
|
|
@ -1,9 +1,8 @@
|
||||||
|
use crate::graph::{new_edge_matrix, new_node_vector, EdgeMatrix};
|
||||||
use std::sync::{Arc, RwLock};
|
|
||||||
use std::thread;
|
|
||||||
use crate::graph::{EdgeMatrix, new_node_vector, new_edge_matrix};
|
|
||||||
use crate::spring_model;
|
use crate::spring_model;
|
||||||
use crate::utils;
|
use crate::utils;
|
||||||
|
use std::sync::{Arc, RwLock};
|
||||||
|
use std::thread;
|
||||||
|
|
||||||
pub struct Runner {
|
pub struct Runner {
|
||||||
iterations: usize,
|
iterations: usize,
|
||||||
|
@ -14,8 +13,14 @@ impl Runner {
|
||||||
pub fn new(iterations: usize, threads: usize) -> Self {
|
pub fn new(iterations: usize, threads: usize) -> Self {
|
||||||
if threads == 0 {
|
if threads == 0 {
|
||||||
match std::thread::available_parallelism() {
|
match std::thread::available_parallelism() {
|
||||||
Ok(threads) => Runner { iterations, threads: threads.get() },
|
Ok(threads) => Runner {
|
||||||
Err(_) => Runner { iterations, threads: 1 },
|
iterations,
|
||||||
|
threads: threads.get(),
|
||||||
|
},
|
||||||
|
Err(_) => Runner {
|
||||||
|
iterations,
|
||||||
|
threads: 1,
|
||||||
|
},
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
Runner {
|
Runner {
|
||||||
|
@ -25,14 +30,22 @@ impl Runner {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn layout(self: &Self, number_of_nodes: usize, edge_list: Vec<(u32, u32)>) -> Vec<(f32, f32)> {
|
pub fn layout(
|
||||||
|
self: &Self,
|
||||||
|
number_of_nodes: usize,
|
||||||
|
edge_list: Vec<(u32, u32)>,
|
||||||
|
) -> Vec<(f32, f32)> {
|
||||||
// let edges = connection_matrix(size);
|
// let edges = connection_matrix(size);
|
||||||
let edges = edge_matrix_from_edge_list(number_of_nodes, edge_list);
|
let edges = edge_matrix_from_edge_list(number_of_nodes, edge_list);
|
||||||
let mut nodes = new_node_vector(number_of_nodes);
|
let mut nodes = new_node_vector(number_of_nodes);
|
||||||
let mut nodes_next = new_node_vector(number_of_nodes);
|
let mut nodes_next = new_node_vector(number_of_nodes);
|
||||||
|
|
||||||
// let model = Arc::new(RwLock::new(spring_model::InitialModel::new(edges, number_of_nodes)));
|
// let model = Arc::new(RwLock::new(spring_model::InitialModel::new(edges, number_of_nodes)));
|
||||||
let model = Arc::new(RwLock::new(spring_model::MyModel::new(edges, number_of_nodes, self.iterations)));
|
let model = Arc::new(RwLock::new(spring_model::MyModel::new(
|
||||||
|
edges,
|
||||||
|
number_of_nodes,
|
||||||
|
self.iterations,
|
||||||
|
)));
|
||||||
|
|
||||||
let chunks = utils::gen_chunks(number_of_nodes, self.threads);
|
let chunks = utils::gen_chunks(number_of_nodes, self.threads);
|
||||||
for _epoch in 0..self.iterations {
|
for _epoch in 0..self.iterations {
|
||||||
|
@ -45,7 +58,7 @@ impl Runner {
|
||||||
let model = model.clone();
|
let model = model.clone();
|
||||||
let handle = thread::spawn(move || {
|
let handle = thread::spawn(move || {
|
||||||
for n in chunk {
|
for n in chunk {
|
||||||
let update = model.read().unwrap().step(&nodes,n);
|
let update = model.read().unwrap().step(&nodes, n);
|
||||||
let mut result = nodes_next[n].write().unwrap();
|
let mut result = nodes_next[n].write().unwrap();
|
||||||
result.x = update.x;
|
result.x = update.x;
|
||||||
result.y = update.y;
|
result.y = update.y;
|
||||||
|
@ -84,4 +97,3 @@ fn edge_matrix_from_edge_list(number_of_nodes: usize, edge_list: Vec<(u32, u32)>
|
||||||
}
|
}
|
||||||
matrix_ptr
|
matrix_ptr
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -1,4 +1,4 @@
|
||||||
use crate::graph::{EdgeMatrix, NodeVector, Node};
|
use crate::graph::{EdgeMatrix, Node, NodeVector};
|
||||||
|
|
||||||
pub struct MyModel {
|
pub struct MyModel {
|
||||||
edges: EdgeMatrix,
|
edges: EdgeMatrix,
|
||||||
|
@ -9,26 +9,24 @@ pub struct MyModel {
|
||||||
}
|
}
|
||||||
|
|
||||||
impl MyModel {
|
impl MyModel {
|
||||||
|
|
||||||
pub fn new(edges: EdgeMatrix, size: usize, iterations: usize) -> MyModel {
|
pub fn new(edges: EdgeMatrix, size: usize, iterations: usize) -> MyModel {
|
||||||
let opt_dist = 1.0;
|
let opt_dist = 1.0;
|
||||||
let c = 0.1;
|
let c = 0.1;
|
||||||
|
|
||||||
MyModel{
|
MyModel {
|
||||||
edges,
|
edges,
|
||||||
size,
|
size,
|
||||||
opt_dist,
|
opt_dist,
|
||||||
c: c,
|
c: c,
|
||||||
dc: c / ((iterations + 1) as f32)
|
dc: c / ((iterations + 1) as f32),
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn prepare(& mut self, _nodes: &NodeVector) {
|
pub fn prepare(&mut self, _nodes: &NodeVector) {
|
||||||
self.c -= self.dc;
|
self.c -= self.dc;
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn step(&self, nodes: &NodeVector, i_node: usize) -> Node {
|
pub fn step(&self, nodes: &NodeVector, i_node: usize) -> Node {
|
||||||
|
|
||||||
let node = nodes[i_node].read().unwrap();
|
let node = nodes[i_node].read().unwrap();
|
||||||
let edges = self.edges.read().unwrap();
|
let edges = self.edges.read().unwrap();
|
||||||
|
|
||||||
|
|
|
@ -15,7 +15,6 @@ pub fn gen_chunks(n: usize, chunks: usize) -> Vec<Range<usize>> {
|
||||||
borders
|
borders
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
#[cfg(test)]
|
#[cfg(test)]
|
||||||
mod test {
|
mod test {
|
||||||
use super::*;
|
use super::*;
|
||||||
|
@ -37,5 +36,4 @@ mod test {
|
||||||
let borders = gen_chunks(10, 1);
|
let borders = gen_chunks(10, 1);
|
||||||
assert_eq!(borders, vec![(0..10)]);
|
assert_eq!(borders, vec![(0..10)]);
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue