formatting

This commit is contained in:
Niko Abeler 2022-11-20 17:09:39 +01:00
parent 19cea83bd8
commit 8b18c1a570
6 changed files with 44 additions and 31 deletions

3
.vscode/settings.json vendored Normal file
View File

@ -0,0 +1,3 @@
{
"editor.formatOnSave": true,
}

View File

@ -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.

View File

@ -1,4 +1,3 @@
/** /**
* Read Graph data from file * Read Graph data from file
* Format: * Format:

View File

@ -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 {
@ -84,4 +97,3 @@ fn edge_matrix_from_edge_list(number_of_nodes: usize, edge_list: Vec<(u32, u32)>
} }
matrix_ptr matrix_ptr
} }

View File

@ -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,7 +9,6 @@ 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;
@ -19,7 +18,7 @@ impl MyModel {
size, size,
opt_dist, opt_dist,
c: c, c: c,
dc: c / ((iterations + 1) as f32) dc: c / ((iterations + 1) as f32),
} }
} }
@ -28,7 +27,6 @@ impl MyModel {
} }
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();

View File

@ -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)]);
} }
} }