This commit is contained in:
Niko Abeler 2022-11-13 20:10:54 +01:00
commit f3c9abe5b5
4 changed files with 176 additions and 0 deletions

1
.gitignore vendored Normal file
View File

@ -0,0 +1 @@
/target

75
Cargo.lock generated Normal file
View File

@ -0,0 +1,75 @@
# This file is automatically @generated by Cargo.
# It is not intended for manual editing.
version = 3
[[package]]
name = "cfg-if"
version = "1.0.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "baf1de4339761588bc0619e3cbc0120ee582ebb74b53b4efbf79117bd2da40fd"
[[package]]
name = "getrandom"
version = "0.2.8"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "c05aeb6a22b8f62540c194aac980f2115af067bfe15a0734d7277a768d396b31"
dependencies = [
"cfg-if",
"libc",
"wasi",
]
[[package]]
name = "graph"
version = "0.1.0"
dependencies = [
"rand",
]
[[package]]
name = "libc"
version = "0.2.137"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "fc7fcc620a3bff7cdd7a365be3376c97191aeaccc2a603e600951e452615bf89"
[[package]]
name = "ppv-lite86"
version = "0.2.17"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "5b40af805b3121feab8a3c29f04d8ad262fa8e0561883e7653e024ae4479e6de"
[[package]]
name = "rand"
version = "0.8.5"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "34af8d1a0e25924bc5b7c43c079c942339d8f0a8b57c39049bef581b46327404"
dependencies = [
"libc",
"rand_chacha",
"rand_core",
]
[[package]]
name = "rand_chacha"
version = "0.3.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "e6c10a63a0fa32252be49d21e7709d4d4baf8d231c2dbce1eaa8141b9b127d88"
dependencies = [
"ppv-lite86",
"rand_core",
]
[[package]]
name = "rand_core"
version = "0.6.4"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "ec0be4795e2f6a28069bec0b5ff3e2ac9bafc99e6a9a7dc3547996c5c816922c"
dependencies = [
"getrandom",
]
[[package]]
name = "wasi"
version = "0.11.0+wasi-snapshot-preview1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "9c8d87e72b64a3b4db28d11ce29237c246188f4f51057d65a7eab63b7987e423"

9
Cargo.toml Normal file
View File

@ -0,0 +1,9 @@
[package]
name = "graph"
version = "0.1.0"
edition = "2021"
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
[dependencies]
rand = "0.8.5"

91
src/main.rs Normal file
View File

@ -0,0 +1,91 @@
use std::thread;
use std::sync::{Arc, RwLock};
use rand::Rng;
struct Node {
x: f32,
y: f32,
}
struct Edge {
weight: f32,
}
fn nodes_list(size: usize) -> Arc<Vec<RwLock<Node>>> {
let mut nodes = Vec::new();
for _ in 0..size {
let node = RwLock::new(Node {
x: rand::thread_rng().gen_range(0.0..100.0),
y: rand::thread_rng().gen_range(0.0..100.0),
});
nodes.push(node);
}
Arc::new(nodes)
}
fn connection_matrix(size: usize) -> Arc<RwLock<Vec<Vec<Edge>>>> {
let mut rng = rand::thread_rng();
let mut matrix = Vec::with_capacity(size);
for _ in 0..size {
let mut row = Vec::with_capacity(size);
for _ in 0..size {
let p: f32 = rng.gen();
row.push(Edge { weight: if p < 0.1 {1.0} else {0.0} });
}
matrix.push(row);
}
Arc::new(RwLock::new(matrix))
}
fn main() {
let size = 24000;
let threads = 8;
let nodes = nodes_list(size);
let nodes_next = nodes_list(size);
let edges = connection_matrix(size);
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 handle = thread::spawn(move || {
for j in 0..chunks {
let n = i * chunks + j;
let node = nodes[n].read().unwrap();
let edges = edges.read().unwrap();
let mut node_x = node.x;
let mut node_y = node.y;
for o in 0..size {
let o_x: f32;
let o_y: f32;
{
let other = nodes[o].read().unwrap();
o_x = other.x;
o_y = other.y;
}
let edge = edges[n][o].weight;
node_x += (o_x - node.x) * edge;
node_y += (o_y - node.y) * edge;
}
let mut result = nodes_next[n].write().unwrap();
result.x = node_x;
result.y = node_y;
}
});
handles.push(handle);
}
for handle in handles {
handle.join().unwrap();
}
}