first implementation

This commit is contained in:
Niko Abeler 2022-11-13 20:55:37 +01:00
parent f3c9abe5b5
commit a0dda954ad
1 changed files with 86 additions and 37 deletions

View File

@ -1,6 +1,8 @@
use std::thread; use std::thread;
use std::sync::{Arc, RwLock}; use std::sync::{Arc, RwLock};
use rand::Rng; use rand::Rng;
use std::fs::File;
use std::io::prelude::*;
struct Node { struct Node {
x: f32, x: f32,
@ -38,9 +40,13 @@ fn connection_matrix(size: usize) -> Arc<RwLock<Vec<Vec<Edge>>>> {
Arc::new(RwLock::new(matrix)) Arc::new(RwLock::new(matrix))
} }
fn main() { fn main() -> std::io::Result<()> {
let size = 24000; const C_REP: f32 = 0.1;
const C_SPRING: f32 = 0.1;
const ITER: usize = 200;
let size = 5000;
let threads = 8; let threads = 8;
let nodes = nodes_list(size); let nodes = nodes_list(size);
@ -48,44 +54,87 @@ fn main() {
let edges = connection_matrix(size); let edges = connection_matrix(size);
let mut handles = vec![]; for epoch in 0..ITER {
let mut handles = vec![];
let chunks = size / threads; 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 handle = thread::spawn(move || { let handle = thread::spawn(move || {
for j in 0..chunks { for j in 0..chunks {
let n = i * chunks + j; 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();
let mut node_x = node.x; let mut node_x = node.x;
let mut node_y = node.y; let mut node_y = node.y;
for o in 0..size { for o in 0..size {
let o_x: f32; if o == n {
let o_y: f32; continue;
{ }
let other = nodes[o].read().unwrap(); let o_x: f32;
o_x = other.x; let o_y: f32;
o_y = other.y; {
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().max(0.01);
let unit_x = d_x / dist;
let unit_y = d_y / dist;
let f_rep = C_REP/(dist).powi(2);
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;
let edge = edges[n][o].weight;
if edge > 0.0 {
let f_spring = C_SPRING * (dist / edge).log(2.0);
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;
}
} }
let edge = edges[n][o].weight; let mut result = nodes_next[n].write().unwrap();
node_x += (o_x - node.x) * edge; result.x = node_x;
node_y += (o_y - node.y) * edge; result.y = node_y;
} }
let mut result = nodes_next[n].write().unwrap(); });
result.x = node_x; handles.push(handle);
result.y = node_y; }
}
}); for handle in handles {
handles.push(handle); handle.join().unwrap();
}
for i in 0..size {
let mut node = nodes[i].write().unwrap();
let node_next = nodes_next[i].read().unwrap();
node.x = node_next.x;
node.y = node_next.y;
}
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())?;
}
} }
for handle in handles { Ok(())
handle.join().unwrap();
}
} }