diff --git a/src/main.rs b/src/main.rs index c3ce22c..ef446cc 100644 --- a/src/main.rs +++ b/src/main.rs @@ -4,6 +4,8 @@ use rand::Rng; use std::fs::File; use std::io::prelude::*; +mod utils; + struct Node { x: f32, y: f32, @@ -79,16 +81,16 @@ fn main() -> std::io::Result<()> { let nodes = nodes_list(size); let nodes_next = nodes_list(size); + let chunks = utils::chunk_borders(size, THREADS); for epoch in 0..ITER { let mut handles = vec![]; - let chunks = size / THREADS; for i in 0..THREADS { let nodes = nodes.clone(); let nodes_next = nodes_next.clone(); let edges = edges.clone(); + let chunk = chunks[i].clone(); let handle = thread::spawn(move || { - for j in 0..chunks { - let n = i * chunks + j; + for n in chunk { let node = nodes[n].read().unwrap(); let edges = edges.read().unwrap(); diff --git a/src/spring_model.rs b/src/spring_model.rs new file mode 100644 index 0000000..e69de29 diff --git a/src/utils.rs b/src/utils.rs new file mode 100644 index 0000000..3712acd --- /dev/null +++ b/src/utils.rs @@ -0,0 +1,40 @@ +use std::ops::Range; + +pub fn chunk_borders(n: usize, chunks: usize) -> Vec> { + let mut borders = vec![]; + let chunk_size = n / chunks; + let mut start = 0; + for _ in 0..chunks { + let end = start + chunk_size; + borders.push(start..end); + start = end; + } + // Change the last chunk to include the remainder + let size = borders.len(); + borders[size - 1].end = n; + borders +} + + +mod test { + use super::*; + + #[test] + fn test_chunk_borders() { + let borders = chunk_borders(10, 3); + assert_eq!(borders, vec![(0..3), (3..6), (6..10)]); + } + + #[test] + fn test_chunk_borders2() { + let borders = chunk_borders(10, 2); + assert_eq!(borders, vec![(0..5), (5..10)]); + } + + #[test] + fn test_chunk_borders3() { + let borders = chunk_borders(10, 1); + assert_eq!(borders, vec![(0..10)]); + } + +}