Python package #1

Merged
h4kor merged 7 commits from python_package into main 2022-11-20 16:14:56 +00:00
6 changed files with 44 additions and 31 deletions
Showing only changes of commit 8b18c1a570 - Show all commits

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 runner;
mod spring_model;
mod utils;
use pyo3::prelude::*;
/// Formats the sum of two numbers as string.
#[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)>> {
#[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)>> {
let r = runner::Runner::new(iter, threads);
Ok(
r.layout(number_of_nodes, edges)
)
Ok(r.layout(number_of_nodes, edges))
}
/// A Python module implemented in Rust.
@ -19,4 +22,4 @@ fn layout_from_edge_list(number_of_nodes: usize, edges: Vec<(u32, u32)>, iter: u
fn graph_force(_py: Python, m: &PyModule) -> PyResult<()> {
m.add_function(wrap_pyfunction!(layout_from_edge_list, m)?)?;
Ok(())
}
}

View File

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

View File

@ -1,9 +1,8 @@
use std::sync::{Arc, RwLock};
use std::thread;
use crate::graph::{EdgeMatrix, new_node_vector, new_edge_matrix};
use crate::graph::{new_edge_matrix, new_node_vector, EdgeMatrix};
use crate::spring_model;
use crate::utils;
use std::sync::{Arc, RwLock};
use std::thread;
pub struct Runner {
iterations: usize,
@ -14,8 +13,14 @@ impl Runner {
pub fn new(iterations: usize, threads: usize) -> Self {
if threads == 0 {
match std::thread::available_parallelism() {
Ok(threads) => Runner { iterations, threads: threads.get() },
Err(_) => Runner { iterations, threads: 1 },
Ok(threads) => Runner {
iterations,
threads: threads.get(),
},
Err(_) => Runner {
iterations,
threads: 1,
},
}
} else {
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 = edge_matrix_from_edge_list(number_of_nodes, edge_list);
let mut nodes = 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::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);
for _epoch in 0..self.iterations {
@ -45,7 +58,7 @@ impl Runner {
let model = model.clone();
let handle = thread::spawn(move || {
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();
result.x = update.x;
result.y = update.y;
@ -57,7 +70,7 @@ impl Runner {
for handle in handles {
handle.join().unwrap();
}
// swap nodes and nodes_next
let tmp = nodes.clone();
nodes = nodes_next.clone();
@ -84,4 +97,3 @@ fn edge_matrix_from_edge_list(number_of_nodes: usize, edge_list: Vec<(u32, u32)>
}
matrix_ptr
}

View File

@ -1,4 +1,4 @@
use crate::graph::{EdgeMatrix, NodeVector, Node};
use crate::graph::{EdgeMatrix, Node, NodeVector};
pub struct MyModel {
edges: EdgeMatrix,
@ -9,26 +9,24 @@ pub struct MyModel {
}
impl MyModel {
pub fn new(edges: EdgeMatrix, size: usize, iterations: usize) -> MyModel {
let opt_dist = 1.0;
let c = 0.1;
MyModel{
MyModel {
edges,
size,
opt_dist,
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;
}
pub fn step(&self, nodes: &NodeVector, i_node: usize) -> Node {
let node = nodes[i_node].read().unwrap();
let edges = self.edges.read().unwrap();
@ -79,10 +77,10 @@ impl MyModel {
let sum_l = (sum_x * sum_x + sum_y * sum_y).sqrt().max(1e-6).recip() * self.c;
let sum_x = sum_x * sum_l;
let sum_y = sum_y * sum_l;
Node {
x: node_x + sum_x,
y: node_y + sum_y,
}
}
}
}

View File

@ -15,7 +15,6 @@ pub fn gen_chunks(n: usize, chunks: usize) -> Vec<Range<usize>> {
borders
}
#[cfg(test)]
mod test {
use super::*;
@ -37,5 +36,4 @@ mod test {
let borders = gen_chunks(10, 1);
assert_eq!(borders, vec![(0..10)]);
}
}