graph-force/src/main.rs

117 lines
3.6 KiB
Rust
Raw Normal View History

2022-11-18 18:32:13 +00:00
mod utils;
mod spring_model;
2022-11-18 18:47:20 +00:00
mod my_model;
2022-11-18 18:32:13 +00:00
mod graph;
2022-11-13 19:10:54 +00:00
use rand::Rng;
2022-11-13 19:55:37 +00:00
use std::fs::File;
use std::io::prelude::*;
2022-11-17 19:28:32 +00:00
use std::sync::{Arc, RwLock};
use std::thread;
2022-11-18 18:32:13 +00:00
use graph::{EdgeMatrix, NodeVector, Node, Edge};
2022-11-13 19:10:54 +00:00
2022-11-18 18:32:13 +00:00
fn nodes_list(size: usize) -> NodeVector {
2022-11-13 19:10:54 +00:00
let mut nodes = Vec::new();
for _ in 0..size {
let node = RwLock::new(Node {
2022-11-18 18:32:13 +00:00
x: rand::thread_rng().gen_range(-0.5..0.5),
y: rand::thread_rng().gen_range(-0.5..0.5),
2022-11-13 19:10:54 +00:00
});
nodes.push(node);
}
Arc::new(nodes)
}
2022-11-17 18:13:23 +00:00
fn connection_matrix(size: usize) -> EdgeMatrix {
2022-11-13 19:10:54 +00:00
let mut matrix = Vec::with_capacity(size);
for _ in 0..size {
let mut row = Vec::with_capacity(size);
for _ in 0..size {
2022-11-17 19:28:32 +00:00
row.push(Edge { weight: 0.0 });
2022-11-13 19:10:54 +00:00
}
matrix.push(row);
}
Arc::new(RwLock::new(matrix))
}
2022-11-17 18:13:23 +00:00
/**
* 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)
}
fn main() -> std::io::Result<()> {
2022-11-18 19:57:46 +00:00
const ITER: usize = 50;
2022-11-17 18:13:23 +00:00
const THREADS: usize = 8;
2022-11-17 19:28:32 +00:00
2022-11-17 18:13:23 +00:00
// let edges = connection_matrix(size);
2022-11-18 19:57:46 +00:00
let (size, edges): (usize, EdgeMatrix) = read_graph("../graph.bin");
2022-11-17 18:13:23 +00:00
println!("Size: {}", size);
2022-11-18 19:57:46 +00:00
let mut nodes = nodes_list(size);
let mut nodes_next = nodes_list(size);
2022-11-13 19:10:54 +00:00
2022-11-18 18:47:20 +00:00
// let model = Arc::new(RwLock::new(spring_model::InitialModel::new(edges, size)));
2022-11-18 19:57:46 +00:00
let model = Arc::new(RwLock::new(my_model::MyModel::new(edges, size, ITER)));
2022-11-17 19:28:32 +00:00
let chunks = utils::gen_chunks(size, THREADS);
2022-11-13 19:55:37 +00:00
for epoch in 0..ITER {
2022-11-18 18:32:13 +00:00
model.write().unwrap().prepare(&nodes);
2022-11-13 19:55:37 +00:00
let mut handles = vec![];
2022-11-17 18:13:23 +00:00
for i in 0..THREADS {
2022-11-13 19:55:37 +00:00
let nodes = nodes.clone();
let nodes_next = nodes_next.clone();
2022-11-17 18:37:07 +00:00
let chunk = chunks[i].clone();
2022-11-18 18:32:13 +00:00
let model = model.clone();
2022-11-13 19:55:37 +00:00
let handle = thread::spawn(move || {
2022-11-17 18:37:07 +00:00
for n in chunk {
2022-11-18 18:32:13 +00:00
let update = model.read().unwrap().step(&nodes,n);
2022-11-13 19:55:37 +00:00
let mut result = nodes_next[n].write().unwrap();
2022-11-18 18:32:13 +00:00
result.x = update.x;
result.y = update.y;
2022-11-13 19:10:54 +00:00
}
2022-11-13 19:55:37 +00:00
});
handles.push(handle);
}
2022-11-17 19:28:32 +00:00
2022-11-13 19:55:37 +00:00
for handle in handles {
handle.join().unwrap();
}
2022-11-18 19:57:46 +00:00
// swap nodes and nodes_next
let tmp = nodes.clone();
nodes = nodes_next.clone();
nodes_next = tmp.clone();
2022-11-13 19:55:37 +00:00
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())?;
}
2022-11-13 19:10:54 +00:00
}
2022-11-13 19:55:37 +00:00
Ok(())
2022-11-13 19:10:54 +00:00
}