From 3a9bf6d6ee4ef302d5aeec2e5482c700f89d9200 Mon Sep 17 00:00:00 2001 From: Niko Abeler Date: Sun, 20 Nov 2022 14:49:43 +0100 Subject: [PATCH 1/7] init with maturin --- .gitignore | 2 + Cargo.lock | 254 +++++++++++++++++++++++++++++++++++++- Cargo.toml | 6 +- pyproject.toml | 14 +++ src/{main.rs => _main.rs} | 0 src/lib.rs | 14 +++ 6 files changed, 288 insertions(+), 2 deletions(-) create mode 100644 pyproject.toml rename src/{main.rs => _main.rs} (100%) create mode 100644 src/lib.rs diff --git a/.gitignore b/.gitignore index 87270ee..8f41e77 100644 --- a/.gitignore +++ b/.gitignore @@ -1,2 +1,4 @@ /target result/ + +venv/ \ No newline at end of file diff --git a/Cargo.lock b/Cargo.lock index c4c3df0..1577a36 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -2,6 +2,18 @@ # It is not intended for manual editing. version = 3 +[[package]] +name = "autocfg" +version = "1.1.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "d468802bab17cbc0cc575e9b053f41e72aa36bfa6b7f55e3529ffa43161b97fa" + +[[package]] +name = "bitflags" +version = "1.3.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "bef38d45163c2f1dde094a7dfd33ccf595c92905c8f8f4fdc18d06fb1037718a" + [[package]] name = "cfg-if" version = "1.0.0" @@ -20,24 +32,157 @@ dependencies = [ ] [[package]] -name = "graph" +name = "graph_force" version = "0.1.0" dependencies = [ + "pyo3", "rand", ] +[[package]] +name = "indoc" +version = "1.0.7" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "adab1eaa3408fb7f0c777a73e7465fd5656136fc93b670eb6df3c88c2c1344e3" + [[package]] name = "libc" version = "0.2.137" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "fc7fcc620a3bff7cdd7a365be3376c97191aeaccc2a603e600951e452615bf89" +[[package]] +name = "lock_api" +version = "0.4.9" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "435011366fe56583b16cf956f9df0095b405b82d76425bc8981c0e22e60ec4df" +dependencies = [ + "autocfg", + "scopeguard", +] + +[[package]] +name = "memoffset" +version = "0.6.5" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "5aa361d4faea93603064a027415f07bd8e1d5c88c9fbf68bf56a285428fd79ce" +dependencies = [ + "autocfg", +] + +[[package]] +name = "once_cell" +version = "1.16.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "86f0b0d4bf799edbc74508c1e8bf170ff5f41238e5f8225603ca7caaae2b7860" + +[[package]] +name = "parking_lot" +version = "0.12.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "3742b2c103b9f06bc9fff0a37ff4912935851bee6d36f3c02bcc755bcfec228f" +dependencies = [ + "lock_api", + "parking_lot_core", +] + +[[package]] +name = "parking_lot_core" +version = "0.9.4" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "4dc9e0dc2adc1c69d09143aff38d3d30c5c3f0df0dad82e6d25547af174ebec0" +dependencies = [ + "cfg-if", + "libc", + "redox_syscall", + "smallvec", + "windows-sys", +] + [[package]] name = "ppv-lite86" version = "0.2.17" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "5b40af805b3121feab8a3c29f04d8ad262fa8e0561883e7653e024ae4479e6de" +[[package]] +name = "proc-macro2" +version = "1.0.47" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "5ea3d908b0e36316caf9e9e2c4625cdde190a7e6f440d794667ed17a1855e725" +dependencies = [ + "unicode-ident", +] + +[[package]] +name = "pyo3" +version = "0.17.3" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "268be0c73583c183f2b14052337465768c07726936a260f480f0857cb95ba543" +dependencies = [ + "cfg-if", + "indoc", + "libc", + "memoffset", + "parking_lot", + "pyo3-build-config", + "pyo3-ffi", + "pyo3-macros", + "unindent", +] + +[[package]] +name = "pyo3-build-config" +version = "0.17.3" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "28fcd1e73f06ec85bf3280c48c67e731d8290ad3d730f8be9dc07946923005c8" +dependencies = [ + "once_cell", + "target-lexicon", +] + +[[package]] +name = "pyo3-ffi" +version = "0.17.3" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "0f6cb136e222e49115b3c51c32792886defbfb0adead26a688142b346a0b9ffc" +dependencies = [ + "libc", + "pyo3-build-config", +] + +[[package]] +name = "pyo3-macros" +version = "0.17.3" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "94144a1266e236b1c932682136dc35a9dee8d3589728f68130c7c3861ef96b28" +dependencies = [ + "proc-macro2", + "pyo3-macros-backend", + "quote", + "syn", +] + +[[package]] +name = "pyo3-macros-backend" +version = "0.17.3" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "c8df9be978a2d2f0cdebabb03206ed73b11314701a5bfe71b0d753b81997777f" +dependencies = [ + "proc-macro2", + "quote", + "syn", +] + +[[package]] +name = "quote" +version = "1.0.21" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "bbe448f377a7d6961e30f5955f9b8d106c3f5e449d493ee1b125c1d43c2b5179" +dependencies = [ + "proc-macro2", +] + [[package]] name = "rand" version = "0.8.5" @@ -68,8 +213,115 @@ dependencies = [ "getrandom", ] +[[package]] +name = "redox_syscall" +version = "0.2.16" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "fb5a58c1855b4b6819d59012155603f0b22ad30cad752600aadfcb695265519a" +dependencies = [ + "bitflags", +] + +[[package]] +name = "scopeguard" +version = "1.1.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "d29ab0c6d3fc0ee92fe66e2d99f700eab17a8d57d1c1d3b748380fb20baa78cd" + +[[package]] +name = "smallvec" +version = "1.10.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "a507befe795404456341dfab10cef66ead4c041f62b8b11bbb92bffe5d0953e0" + +[[package]] +name = "syn" +version = "1.0.103" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "a864042229133ada95abf3b54fdc62ef5ccabe9515b64717bcb9a1919e59445d" +dependencies = [ + "proc-macro2", + "quote", + "unicode-ident", +] + +[[package]] +name = "target-lexicon" +version = "0.12.5" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "9410d0f6853b1d94f0e519fb95df60f29d2c1eff2d921ffdf01a4c8a3b54f12d" + +[[package]] +name = "unicode-ident" +version = "1.0.5" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "6ceab39d59e4c9499d4e5a8ee0e2735b891bb7308ac83dfb4e80cad195c9f6f3" + +[[package]] +name = "unindent" +version = "0.1.10" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "58ee9362deb4a96cef4d437d1ad49cffc9b9e92d202b6995674e928ce684f112" + [[package]] name = "wasi" version = "0.11.0+wasi-snapshot-preview1" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "9c8d87e72b64a3b4db28d11ce29237c246188f4f51057d65a7eab63b7987e423" + +[[package]] +name = "windows-sys" +version = "0.42.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "5a3e1820f08b8513f676f7ab6c1f99ff312fb97b553d30ff4dd86f9f15728aa7" +dependencies = [ + "windows_aarch64_gnullvm", + "windows_aarch64_msvc", + "windows_i686_gnu", + "windows_i686_msvc", + "windows_x86_64_gnu", + "windows_x86_64_gnullvm", + "windows_x86_64_msvc", +] + +[[package]] +name = "windows_aarch64_gnullvm" +version = "0.42.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "41d2aa71f6f0cbe00ae5167d90ef3cfe66527d6f613ca78ac8024c3ccab9a19e" + +[[package]] +name = "windows_aarch64_msvc" +version = "0.42.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "dd0f252f5a35cac83d6311b2e795981f5ee6e67eb1f9a7f64eb4500fbc4dcdb4" + +[[package]] +name = "windows_i686_gnu" +version = "0.42.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "fbeae19f6716841636c28d695375df17562ca208b2b7d0dc47635a50ae6c5de7" + +[[package]] +name = "windows_i686_msvc" +version = "0.42.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "84c12f65daa39dd2babe6e442988fc329d6243fdce47d7d2d155b8d874862246" + +[[package]] +name = "windows_x86_64_gnu" +version = "0.42.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "bf7b1b21b5362cbc318f686150e5bcea75ecedc74dd157d874d754a2ca44b0ed" + +[[package]] +name = "windows_x86_64_gnullvm" +version = "0.42.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "09d525d2ba30eeb3297665bd434a54297e4170c7f1a44cad4ef58095b4cd2028" + +[[package]] +name = "windows_x86_64_msvc" +version = "0.42.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "f40009d85759725a34da6d89a94e63d7bdc50a862acf0dbc7c8e488f1edcb6f5" diff --git a/Cargo.toml b/Cargo.toml index 2578eb0..29ba271 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -1,9 +1,13 @@ [package] -name = "graph" +name = "graph_force" version = "0.1.0" edition = "2021" # See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html +[lib] +name = "graph_force" +crate-type = ["cdylib"] [dependencies] rand = "0.8.5" +pyo3 = { version = "0.17.3", features = ["extension-module"] } diff --git a/pyproject.toml b/pyproject.toml new file mode 100644 index 0000000..7e4a9a6 --- /dev/null +++ b/pyproject.toml @@ -0,0 +1,14 @@ +[build-system] +requires = ["maturin>=0.14,<0.15"] +build-backend = "maturin" + +[project] +name = "graph_force" +requires-python = ">=3.7" +classifiers = [ + "Programming Language :: Rust", + "Programming Language :: Python :: Implementation :: CPython", + "Programming Language :: Python :: Implementation :: PyPy", +] + + diff --git a/src/main.rs b/src/_main.rs similarity index 100% rename from src/main.rs rename to src/_main.rs diff --git a/src/lib.rs b/src/lib.rs new file mode 100644 index 0000000..622fb79 --- /dev/null +++ b/src/lib.rs @@ -0,0 +1,14 @@ +use pyo3::prelude::*; + +/// Formats the sum of two numbers as string. +#[pyfunction] +fn sum_as_string(a: usize, b: usize) -> PyResult { + Ok((a + b).to_string()) +} + +/// A Python module implemented in Rust. +#[pymodule] +fn test(_py: Python, m: &PyModule) -> PyResult<()> { + m.add_function(wrap_pyfunction!(sum_as_string, m)?)?; + Ok(()) +} \ No newline at end of file -- 2.40.1 From d897436ba14701df546425cc5b589e0678952dbb Mon Sep 17 00:00:00 2001 From: Niko Abeler Date: Sun, 20 Nov 2022 15:18:06 +0100 Subject: [PATCH 2/7] first successful execution --- src/{_main.rs => layout.rs} | 66 +++++++++++++------------------------ src/lib.rs | 16 ++++++--- src/reader.rs | 27 +++++++++++++++ 3 files changed, 61 insertions(+), 48 deletions(-) rename src/{_main.rs => layout.rs} (52%) create mode 100644 src/reader.rs diff --git a/src/_main.rs b/src/layout.rs similarity index 52% rename from src/_main.rs rename to src/layout.rs index 1e74e23..252f684 100644 --- a/src/_main.rs +++ b/src/layout.rs @@ -1,14 +1,10 @@ -mod utils; -mod spring_model; -mod my_model; -mod graph; use rand::Rng; -use std::fs::File; -use std::io::prelude::*; use std::sync::{Arc, RwLock}; use std::thread; -use graph::{EdgeMatrix, NodeVector, Node, Edge}; +use crate::graph::{EdgeMatrix, NodeVector, Node, Edge}; +use crate::my_model; +use crate::utils; fn nodes_list(size: usize) -> NodeVector { @@ -35,47 +31,31 @@ fn connection_matrix(size: usize) -> EdgeMatrix { Arc::new(RwLock::new(matrix)) } -/** - * Read Graph data from file - * Format: - * - Little endian - * - 4 bytes: number of nodes(int) - * - 12 bytes: nodeA(int), nodeB(int), weight(float) - */ -fn read_graph(file_name: &str) -> (usize, EdgeMatrix) { - let mut file = File::open(file_name).expect("file not found"); - let mut size_buffer = [0; 4]; - file.read_exact(&mut size_buffer).expect("buffer overflow"); - let size = u32::from_le_bytes(size_buffer) as usize; - let matrix_ptr = connection_matrix(size); +fn edge_matrix_from_edge_list(number_of_nodes: usize, edge_list: Vec<(u32, u32)>) -> EdgeMatrix { + let matrix_ptr = connection_matrix(number_of_nodes as usize); { let mut matrix = matrix_ptr.write().unwrap(); - let mut buffer = [0; 12]; - while file.read_exact(&mut buffer).is_ok() { - let node_a = u32::from_le_bytes(buffer[0..4].try_into().unwrap()) as usize; - let node_b = u32::from_le_bytes(buffer[4..8].try_into().unwrap()) as usize; - let weight = f32::from_le_bytes(buffer[8..12].try_into().unwrap()); - matrix[node_a][node_b].weight = weight; - matrix[node_b][node_a].weight = weight; + for (node_a, node_b) in edge_list { + matrix[node_a as usize][node_b as usize].weight = 1.0; + matrix[node_b as usize][node_a as usize].weight = 1.0; } } - (size, matrix_ptr) + matrix_ptr } -fn main() -> std::io::Result<()> { +pub fn layout(number_of_nodes: usize, edge_list: Vec<(u32, u32)>) -> Vec<(f32, f32)> { const ITER: usize = 5000; const THREADS: usize = 8; // let edges = connection_matrix(size); - let (size, edges): (usize, EdgeMatrix) = read_graph("../debug_graph.bin"); - println!("Size: {}", size); - let mut nodes = nodes_list(size); - let mut nodes_next = nodes_list(size); + let edges = edge_matrix_from_edge_list(number_of_nodes, edge_list); + let mut nodes = nodes_list(number_of_nodes); + let mut nodes_next = nodes_list(number_of_nodes); - // let model = Arc::new(RwLock::new(spring_model::InitialModel::new(edges, size))); - let model = Arc::new(RwLock::new(my_model::MyModel::new(edges, size, ITER))); + // let model = Arc::new(RwLock::new(spring_model::InitialModel::new(edges, number_of_nodes))); + let model = Arc::new(RwLock::new(my_model::MyModel::new(edges, number_of_nodes, ITER))); - let chunks = utils::gen_chunks(size, THREADS); + let chunks = utils::gen_chunks(number_of_nodes, THREADS); for epoch in 0..ITER { model.write().unwrap().prepare(&nodes); let mut handles = vec![]; @@ -103,14 +83,12 @@ fn main() -> std::io::Result<()> { let tmp = nodes.clone(); nodes = nodes_next.clone(); nodes_next = tmp.clone(); - - let mut file = File::create(format!("result/{:04}.txt", epoch))?; - for i in 0..size { - let node = nodes[i].read().unwrap(); - // println!("{} {}", node.x, node.y); - file.write_all(format!("{} {}\n", node.x, node.y).as_bytes())?; - } } - Ok(()) + let mut result = vec![]; + for node in nodes.iter() { + let node = node.read().unwrap(); + result.push((node.x, node.y)); + } + result } diff --git a/src/lib.rs b/src/lib.rs index 622fb79..839cd98 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -1,14 +1,22 @@ +mod layout; +mod utils; +mod spring_model; +mod my_model; +mod graph; + use pyo3::prelude::*; /// Formats the sum of two numbers as string. #[pyfunction] -fn sum_as_string(a: usize, b: usize) -> PyResult { - Ok((a + b).to_string()) +fn layout_from_edge_list(number_of_nodes: usize, edges: Vec<(u32, u32)>) -> PyResult> { + Ok( + layout::layout(number_of_nodes, edges) + ) } /// A Python module implemented in Rust. #[pymodule] -fn test(_py: Python, m: &PyModule) -> PyResult<()> { - m.add_function(wrap_pyfunction!(sum_as_string, m)?)?; +fn graph_force(_py: Python, m: &PyModule) -> PyResult<()> { + m.add_function(wrap_pyfunction!(layout_from_edge_list, m)?)?; Ok(()) } \ No newline at end of file diff --git a/src/reader.rs b/src/reader.rs new file mode 100644 index 0000000..0144b8a --- /dev/null +++ b/src/reader.rs @@ -0,0 +1,27 @@ + +/** + * Read Graph data from file + * Format: + * - Little endian + * - 4 bytes: number of nodes(int) + * - 12 bytes: nodeA(int), nodeB(int), weight(float) + */ +fn read_graph(file_name: &str) -> (usize, EdgeMatrix) { + let mut file = File::open(file_name).expect("file not found"); + let mut size_buffer = [0; 4]; + file.read_exact(&mut size_buffer).expect("buffer overflow"); + let size = u32::from_le_bytes(size_buffer) as usize; + let matrix_ptr = connection_matrix(size); + { + let mut matrix = matrix_ptr.write().unwrap(); + let mut buffer = [0; 12]; + while file.read_exact(&mut buffer).is_ok() { + let node_a = u32::from_le_bytes(buffer[0..4].try_into().unwrap()) as usize; + let node_b = u32::from_le_bytes(buffer[4..8].try_into().unwrap()) as usize; + let weight = f32::from_le_bytes(buffer[8..12].try_into().unwrap()); + matrix[node_a][node_b].weight = weight; + matrix[node_b][node_a].weight = weight; + } + } + (size, matrix_ptr) +} -- 2.40.1 From d8b172c7fb5732e69305a107323451bca7b31c09 Mon Sep 17 00:00:00 2001 From: Niko Abeler Date: Sun, 20 Nov 2022 16:57:31 +0100 Subject: [PATCH 3/7] clean up and moving iter and threads to params --- src/graph.rs | 24 +++++++++++ src/layout.rs | 94 ------------------------------------------- src/lib.rs | 10 ++--- src/my_model.rs | 97 --------------------------------------------- src/runner.rs | 80 +++++++++++++++++++++++++++++++++++++ src/spring_model.rs | 66 +++++++++++++++++------------- src/utils.rs | 1 + 7 files changed, 149 insertions(+), 223 deletions(-) delete mode 100644 src/layout.rs delete mode 100644 src/my_model.rs create mode 100644 src/runner.rs diff --git a/src/graph.rs b/src/graph.rs index 662a005..4649e5c 100644 --- a/src/graph.rs +++ b/src/graph.rs @@ -1,3 +1,4 @@ +use rand::Rng; use std::sync::{Arc, RwLock}; pub struct Node { pub x: f32, @@ -10,3 +11,26 @@ pub struct Edge { pub type EdgeMatrix = Arc>>>; pub type NodeVector = Arc>>; + +pub fn new_edge_matrix(size: usize) -> EdgeMatrix { + let mut matrix = Vec::with_capacity(size); + for _ in 0..size { + let mut row = Vec::with_capacity(size); + for _ in 0..size { + row.push(Edge { weight: 0.0 }); + } + matrix.push(row); + } + Arc::new(RwLock::new(matrix)) +} + +pub fn new_node_vector(size: usize) -> NodeVector { + let mut nodes = Vec::with_capacity(size); + for _ in 0..size { + nodes.push(RwLock::new(Node { + x: rand::thread_rng().gen_range(-0.5..0.5), + y: rand::thread_rng().gen_range(-0.5..0.5), + })); + } + Arc::new(nodes) +} diff --git a/src/layout.rs b/src/layout.rs deleted file mode 100644 index 252f684..0000000 --- a/src/layout.rs +++ /dev/null @@ -1,94 +0,0 @@ - -use rand::Rng; -use std::sync::{Arc, RwLock}; -use std::thread; -use crate::graph::{EdgeMatrix, NodeVector, Node, Edge}; -use crate::my_model; -use crate::utils; - - -fn nodes_list(size: usize) -> NodeVector { - let mut nodes = Vec::new(); - for _ in 0..size { - let node = RwLock::new(Node { - x: rand::thread_rng().gen_range(-0.5..0.5), - y: rand::thread_rng().gen_range(-0.5..0.5), - }); - nodes.push(node); - } - Arc::new(nodes) -} - -fn connection_matrix(size: usize) -> EdgeMatrix { - let mut matrix = Vec::with_capacity(size); - for _ in 0..size { - let mut row = Vec::with_capacity(size); - for _ in 0..size { - row.push(Edge { weight: 0.0 }); - } - matrix.push(row); - } - Arc::new(RwLock::new(matrix)) -} - -fn edge_matrix_from_edge_list(number_of_nodes: usize, edge_list: Vec<(u32, u32)>) -> EdgeMatrix { - let matrix_ptr = connection_matrix(number_of_nodes as usize); - { - let mut matrix = matrix_ptr.write().unwrap(); - for (node_a, node_b) in edge_list { - matrix[node_a as usize][node_b as usize].weight = 1.0; - matrix[node_b as usize][node_a as usize].weight = 1.0; - } - } - matrix_ptr -} - -pub fn layout(number_of_nodes: usize, edge_list: Vec<(u32, u32)>) -> Vec<(f32, f32)> { - const ITER: usize = 5000; - const THREADS: usize = 8; - - // let edges = connection_matrix(size); - let edges = edge_matrix_from_edge_list(number_of_nodes, edge_list); - let mut nodes = nodes_list(number_of_nodes); - let mut nodes_next = nodes_list(number_of_nodes); - - // let model = Arc::new(RwLock::new(spring_model::InitialModel::new(edges, number_of_nodes))); - let model = Arc::new(RwLock::new(my_model::MyModel::new(edges, number_of_nodes, ITER))); - - let chunks = utils::gen_chunks(number_of_nodes, THREADS); - for epoch in 0..ITER { - model.write().unwrap().prepare(&nodes); - let mut handles = vec![]; - for i in 0..THREADS { - let nodes = nodes.clone(); - let nodes_next = nodes_next.clone(); - let chunk = chunks[i].clone(); - let model = model.clone(); - let handle = thread::spawn(move || { - for n in chunk { - let update = model.read().unwrap().step(&nodes,n); - let mut result = nodes_next[n].write().unwrap(); - result.x = update.x; - result.y = update.y; - } - }); - handles.push(handle); - } - - for handle in handles { - handle.join().unwrap(); - } - - // swap nodes and nodes_next - let tmp = nodes.clone(); - nodes = nodes_next.clone(); - nodes_next = tmp.clone(); - } - - let mut result = vec![]; - for node in nodes.iter() { - let node = node.read().unwrap(); - result.push((node.x, node.y)); - } - result -} diff --git a/src/lib.rs b/src/lib.rs index 839cd98..5dda95c 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -1,16 +1,16 @@ -mod layout; +mod runner; mod utils; mod spring_model; -mod my_model; mod graph; use pyo3::prelude::*; /// Formats the sum of two numbers as string. -#[pyfunction] -fn layout_from_edge_list(number_of_nodes: usize, edges: Vec<(u32, u32)>) -> PyResult> { +#[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> { + let r = runner::Runner::new(iter, threads); Ok( - layout::layout(number_of_nodes, edges) + r.layout(number_of_nodes, edges) ) } diff --git a/src/my_model.rs b/src/my_model.rs deleted file mode 100644 index 23bfc3b..0000000 --- a/src/my_model.rs +++ /dev/null @@ -1,97 +0,0 @@ -use crate::graph::{EdgeMatrix, NodeVector, Node}; - -pub struct MyModel { - edges: EdgeMatrix, - ranks: Vec, - size: usize, - opt_dist: f32, - c: f32, - dc: f32, -} - -impl MyModel { - - pub fn new(edges: EdgeMatrix, size: usize, iterations: usize) -> MyModel { - let opt_dist = 1.0; - let c = 0.1; - let mut ranks = vec![0.0; size]; - { - let edges = edges.read().unwrap(); - for i in 0..size { - ranks[i] = edges[i].iter().map(|e| e.weight).sum(); - } - } - - MyModel{ - edges, - ranks, - size, - opt_dist, - c: c, - dc: c / ((iterations + 1) as f32) - } - } - - 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(); - - let node_x = node.x; - let node_y = node.y; - - let mut sum_x = 0.0; - let mut sum_y = 0.0; - - for o in 0..self.size { - if o == i_node { - continue; - } - let o_x: f32; - let o_y: f32; - { - let other = nodes[o].read().unwrap(); - o_x = other.x; - o_y = other.y; - } - - let d_x = o_x - node_x; - let d_y = o_y - node_y; - let dist = (d_x * d_x + d_y * d_y).sqrt(); - let unit_x = d_x / dist; - let unit_y = d_y / dist; - - let edge = edges[i_node][o].weight; - - if edge == 0.0 { - let f_rep = dist.powi(2).recip().min(self.opt_dist); - let f_rep_x = f_rep * unit_x; - let f_rep_y = f_rep * unit_y; - - sum_x -= f_rep_x; - sum_y -= f_rep_y; - } else { - let f_spring = 0.5 * (dist - self.opt_dist); - let f_spring_x = f_spring * unit_x; - let f_spring_y = f_spring * unit_y; - sum_x += f_spring_x; - sum_y += f_spring_y; - } - } - - // limit the movement - // TODO: find a good upper bound - 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, - } - } -} diff --git a/src/runner.rs b/src/runner.rs new file mode 100644 index 0000000..a5ca36b --- /dev/null +++ b/src/runner.rs @@ -0,0 +1,80 @@ + +use std::sync::{Arc, RwLock}; +use std::thread; +use crate::graph::{EdgeMatrix, new_node_vector, new_edge_matrix}; +use crate::spring_model; +use crate::utils; + +pub struct Runner { + iterations: usize, + threads: usize, +} + +impl Runner { + pub fn new(iterations: usize, threads: usize) -> Self { + Self { + iterations, + threads, + } + } + + 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 chunks = utils::gen_chunks(number_of_nodes, self.threads); + for _epoch in 0..self.iterations { + model.write().unwrap().prepare(&nodes); + let mut handles = vec![]; + for i in 0..self.threads { + let nodes = nodes.clone(); + let nodes_next = nodes_next.clone(); + let chunk = chunks[i].clone(); + let model = model.clone(); + let handle = thread::spawn(move || { + for n in chunk { + let update = model.read().unwrap().step(&nodes,n); + let mut result = nodes_next[n].write().unwrap(); + result.x = update.x; + result.y = update.y; + } + }); + handles.push(handle); + } + + for handle in handles { + handle.join().unwrap(); + } + + // swap nodes and nodes_next + let tmp = nodes.clone(); + nodes = nodes_next.clone(); + nodes_next = tmp.clone(); + } + + let mut result = vec![]; + for node in nodes.iter() { + let node = node.read().unwrap(); + result.push((node.x, node.y)); + } + result + } +} + +fn edge_matrix_from_edge_list(number_of_nodes: usize, edge_list: Vec<(u32, u32)>) -> EdgeMatrix { + let matrix_ptr = new_edge_matrix(number_of_nodes as usize); + { + let mut matrix = matrix_ptr.write().unwrap(); + for (node_a, node_b) in edge_list { + matrix[node_a as usize][node_b as usize].weight = 1.0; + matrix[node_b as usize][node_a as usize].weight = 1.0; + } + } + matrix_ptr +} + diff --git a/src/spring_model.rs b/src/spring_model.rs index 1ec293f..bdbda5c 100644 --- a/src/spring_model.rs +++ b/src/spring_model.rs @@ -1,29 +1,30 @@ use crate::graph::{EdgeMatrix, NodeVector, Node}; - -const C_REP: f32 = 0.1; -const C_SPRING: f32 = 0.1; - -pub struct InitialModel { +pub struct MyModel { edges: EdgeMatrix, size: usize, opt_dist: f32, - t: f32, - + c: f32, + dc: f32, } -impl InitialModel { +impl MyModel { - pub fn new(edges: EdgeMatrix, size: usize) -> InitialModel { - let opt_dist = 1.0 / (size as f32).sqrt(); - InitialModel{ edges, size, opt_dist, t: 0.1 } + pub fn new(edges: EdgeMatrix, size: usize, iterations: usize) -> MyModel { + let opt_dist = 1.0; + let c = 0.1; + + MyModel{ + edges, + size, + opt_dist, + c: c, + dc: c / ((iterations + 1) as f32) + } } - pub fn prepare(& mut self, nodes: &NodeVector) { - self.t = 0.1 * f32::max( - nodes.iter().map(|n| n.read().unwrap().x.abs()).reduce(|a, b| a.max(b)).unwrap(), - nodes.iter().map(|n| n.read().unwrap().y.abs()).reduce(|a, b| a.max(b)).unwrap() - ); + pub fn prepare(& mut self, _nodes: &NodeVector) { + self.c -= self.dc; } pub fn step(&self, nodes: &NodeVector, i_node: usize) -> Node { @@ -31,8 +32,12 @@ impl InitialModel { let node = nodes[i_node].read().unwrap(); let edges = self.edges.read().unwrap(); - let mut node_x = node.x; - let mut node_y = node.y; + let node_x = node.x; + let node_y = node.y; + + let mut sum_x = 0.0; + let mut sum_y = 0.0; + for o in 0..self.size { if o == i_node { continue; @@ -47,30 +52,37 @@ impl InitialModel { let d_x = o_x - node_x; let d_y = o_y - node_y; - let dist = (d_x * d_x + d_y * d_y).sqrt().max(0.01); + let dist = (d_x * d_x + d_y * d_y).sqrt(); let unit_x = d_x / dist; let unit_y = d_y / dist; let edge = edges[i_node][o].weight; if edge == 0.0 { - let f_rep = (C_REP / (dist).powi(2)).min(self.t); + let f_rep = dist.powi(2).recip().min(self.opt_dist); let f_rep_x = f_rep * unit_x; let f_rep_y = f_rep * unit_y; - node_x -= f_rep_x; - node_y -= f_rep_y; + sum_x -= f_rep_x; + sum_y -= f_rep_y; } else { - let f_spring = (C_SPRING * (dist / self.opt_dist).log(2.0)).min(self.t); + let f_spring = 0.5 * (dist - self.opt_dist); let f_spring_x = f_spring * unit_x; let f_spring_y = f_spring * unit_y; - node_x += f_spring_x; - node_y += f_spring_y; + sum_x += f_spring_x; + sum_y += f_spring_y; } } + + // limit the movement + // TODO: find a good upper bound + 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, - y: node_y, + x: node_x + sum_x, + y: node_y + sum_y, } } } diff --git a/src/utils.rs b/src/utils.rs index 2e2be32..87d8cc9 100644 --- a/src/utils.rs +++ b/src/utils.rs @@ -16,6 +16,7 @@ pub fn gen_chunks(n: usize, chunks: usize) -> Vec> { } +#[cfg(test)] mod test { use super::*; -- 2.40.1 From 19cea83bd8520f6065e7e3af056de967af0edc9e Mon Sep 17 00:00:00 2001 From: Niko Abeler Date: Sun, 20 Nov 2022 17:05:11 +0100 Subject: [PATCH 4/7] use thread estimation if threads is set to 0 --- src/runner.rs | 13 ++++++++++--- 1 file changed, 10 insertions(+), 3 deletions(-) diff --git a/src/runner.rs b/src/runner.rs index a5ca36b..cabff5b 100644 --- a/src/runner.rs +++ b/src/runner.rs @@ -12,9 +12,16 @@ pub struct Runner { impl Runner { pub fn new(iterations: usize, threads: usize) -> Self { - Self { - iterations, - threads, + if threads == 0 { + match std::thread::available_parallelism() { + Ok(threads) => Runner { iterations, threads: threads.get() }, + Err(_) => Runner { iterations, threads: 1 }, + } + } else { + Runner { + iterations, + threads, + } } } -- 2.40.1 From 8b18c1a5708fa56e8b34e5b91f0747d205b617a4 Mon Sep 17 00:00:00 2001 From: Niko Abeler Date: Sun, 20 Nov 2022 17:09:39 +0100 Subject: [PATCH 5/7] formatting --- .vscode/settings.json | 3 +++ src/lib.rs | 21 ++++++++++++--------- src/reader.rs | 1 - src/runner.rs | 34 +++++++++++++++++++++++----------- src/spring_model.rs | 14 ++++++-------- src/utils.rs | 2 -- 6 files changed, 44 insertions(+), 31 deletions(-) create mode 100644 .vscode/settings.json diff --git a/.vscode/settings.json b/.vscode/settings.json new file mode 100644 index 0000000..ebfde9a --- /dev/null +++ b/.vscode/settings.json @@ -0,0 +1,3 @@ +{ + "editor.formatOnSave": true, +} \ No newline at end of file diff --git a/src/lib.rs b/src/lib.rs index 5dda95c..e6622dd 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -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> { +#[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> { 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(()) -} \ No newline at end of file +} diff --git a/src/reader.rs b/src/reader.rs index 0144b8a..e5d2f4d 100644 --- a/src/reader.rs +++ b/src/reader.rs @@ -1,4 +1,3 @@ - /** * Read Graph data from file * Format: diff --git a/src/runner.rs b/src/runner.rs index cabff5b..3bd4062 100644 --- a/src/runner.rs +++ b/src/runner.rs @@ -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 } - diff --git a/src/spring_model.rs b/src/spring_model.rs index bdbda5c..94168e1 100644 --- a/src/spring_model.rs +++ b/src/spring_model.rs @@ -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, - } + } } } diff --git a/src/utils.rs b/src/utils.rs index 87d8cc9..7be8f55 100644 --- a/src/utils.rs +++ b/src/utils.rs @@ -15,7 +15,6 @@ pub fn gen_chunks(n: usize, chunks: usize) -> Vec> { 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)]); } - } -- 2.40.1 From 50a15a158cd770f030723ac293559c330b2f3112 Mon Sep 17 00:00:00 2001 From: Niko Abeler Date: Sun, 20 Nov 2022 17:11:41 +0100 Subject: [PATCH 6/7] added README --- README.md | 4 ++++ 1 file changed, 4 insertions(+) create mode 100644 README.md diff --git a/README.md b/README.md new file mode 100644 index 0000000..1277f60 --- /dev/null +++ b/README.md @@ -0,0 +1,4 @@ +# Force Graph + +A python/rust library for embedding graphs in 2D space, using force-directed layouts. + -- 2.40.1 From b6e4744210fab8a42f2df5d61479d0a96fb44a09 Mon Sep 17 00:00:00 2001 From: Niko Abeler Date: Sun, 20 Nov 2022 17:14:34 +0100 Subject: [PATCH 7/7] requirements + minimal readme --- README.md | 23 ++++++++++++++++++++++- requirements.txt | 1 + 2 files changed, 23 insertions(+), 1 deletion(-) create mode 100644 requirements.txt diff --git a/README.md b/README.md index 1277f60..0fab51f 100644 --- a/README.md +++ b/README.md @@ -1,4 +1,25 @@ -# Force Graph +# Graph Force A python/rust library for embedding graphs in 2D space, using force-directed layouts. +## Development + +### Setup +``` +python3 -m venv venv +source venv/bin/activate +pip install -r requirements.txt +``` + +### Build +``` +maturin develop +``` + +## Usage +```python +import graph_force + +edges = [(0, 1), (1, 2), (2, 3), (3, 0)] +pos = graph_force.layout_from_edge_list(4, edges) +``` \ No newline at end of file diff --git a/requirements.txt b/requirements.txt new file mode 100644 index 0000000..6fdfcae --- /dev/null +++ b/requirements.txt @@ -0,0 +1 @@ +maturin==0.14.1 -- 2.40.1