proper chunks

This commit is contained in:
Niko Abeler 2022-11-17 19:37:07 +01:00
parent 0bac20babe
commit 8759a58cb9
3 changed files with 45 additions and 3 deletions

View File

@ -4,6 +4,8 @@ use rand::Rng;
use std::fs::File; use std::fs::File;
use std::io::prelude::*; use std::io::prelude::*;
mod utils;
struct Node { struct Node {
x: f32, x: f32,
y: f32, y: f32,
@ -79,16 +81,16 @@ fn main() -> std::io::Result<()> {
let nodes = nodes_list(size); let nodes = nodes_list(size);
let nodes_next = nodes_list(size); let nodes_next = nodes_list(size);
let chunks = utils::chunk_borders(size, THREADS);
for epoch in 0..ITER { for epoch in 0..ITER {
let mut handles = vec![]; let mut handles = vec![];
let chunks = size / THREADS;
for i in 0..THREADS { for i in 0..THREADS {
let nodes = nodes.clone(); let nodes = nodes.clone();
let nodes_next = nodes_next.clone(); let nodes_next = nodes_next.clone();
let edges = edges.clone(); let edges = edges.clone();
let chunk = chunks[i].clone();
let handle = thread::spawn(move || { let handle = thread::spawn(move || {
for j in 0..chunks { for n in chunk {
let n = i * chunks + j;
let node = nodes[n].read().unwrap(); let node = nodes[n].read().unwrap();
let edges = edges.read().unwrap(); let edges = edges.read().unwrap();

0
src/spring_model.rs Normal file
View File

40
src/utils.rs Normal file
View File

@ -0,0 +1,40 @@
use std::ops::Range;
pub fn chunk_borders(n: usize, chunks: usize) -> Vec<Range<usize>> {
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)]);
}
}