Python package #1
|
@ -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
|
||||
}
|
16
src/lib.rs
16
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<String> {
|
||||
Ok((a + b).to_string())
|
||||
fn layout_from_edge_list(number_of_nodes: usize, edges: Vec<(u32, u32)>) -> PyResult<Vec<(f32, f32)>> {
|
||||
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(())
|
||||
}
|
|
@ -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)
|
||||
}
|
Loading…
Reference in New Issue