Compare commits

...

3 commits

Author SHA1 Message Date
Joe
2496741ec7 multithreaded update 2025-10-17 17:25:47 -07:00
Joe
fdc783b87c multithread the creation of the index 2025-10-17 17:15:44 -07:00
Joe
dec3ea0e60 removed atomics, still fast 2025-10-17 16:58:34 -07:00

View file

@ -1,11 +1,9 @@
use std::{
sync::{
atomic::{AtomicUsize, Ordering::Relaxed},
Arc, RwLock,
},
sync::{Arc, RwLock},
thread,
};
#[derive(Debug, Default, Clone, Copy, PartialEq, PartialOrd)]
pub struct Point2d {
pub x: f64,
pub y: f64,
@ -13,8 +11,8 @@ pub struct Point2d {
#[derive(Debug, Default, Clone, Copy)]
pub struct PVal {
pub point: usize,
pub val: f64,
point_index: usize,
val: f64,
}
impl PartialEq for PVal {
@ -41,21 +39,25 @@ impl Ord for PVal {
impl PVal {
pub fn new(point: usize, val: f64) -> Self {
Self { point, val }
Self {
point_index: point,
val,
}
}
pub fn point_index(&self) -> usize {
self.point
self.point_index
}
// pub fn val(&self) -> f64 {
// f64::from_bits(self.val)
// }
pub fn val(&self) -> f64 {
self.val
}
}
#[derive(Debug, Default, Clone)]
pub struct Spindex2d {
pub xpoints: Arc<Vec<AtomicUsize>>,
pub ypoints: Arc<Vec<AtomicUsize>>,
pub xpoints: Arc<RwLock<Vec<usize>>>,
pub ypoints: Arc<RwLock<Vec<usize>>>,
pub xs: Arc<RwLock<Vec<PVal>>>,
pub ys: Arc<RwLock<Vec<PVal>>>,
}
@ -63,28 +65,32 @@ pub struct Spindex2d {
impl Spindex2d {
pub fn new(points: &[Point2d]) -> Self {
let mut xpoints = Vec::with_capacity(points.len());
let mut ypoints = Vec::with_capacity(points.len());
let mut xs = Vec::with_capacity(points.len());
let mut ys = Vec::with_capacity(points.len());
for (i, point) in points.iter().enumerate() {
let x = PVal {
point: i,
val: point.x,
};
xs.push(x);
let y = PVal {
point: i,
val: point.y,
};
ys.push(y);
xpoints.push(AtomicUsize::new(i));
ypoints.push(AtomicUsize::new(i));
}
let mut ypoints = Vec::with_capacity(points.len());
let mut ys = Vec::with_capacity(points.len());
thread::scope(|s| {
s.spawn(|| {
for (i, point) in points.iter().enumerate() {
let x = PVal::new(i, point.x);
xs.push(x);
xpoints.push(i);
}
});
s.spawn(|| {
for (i, point) in points.iter().enumerate() {
let y = PVal::new(i, point.y);
ys.push(y);
ypoints.push(i);
}
});
});
let index = Self {
xpoints: xpoints.into(),
ypoints: ypoints.into(),
xpoints: Arc::new(RwLock::new(xpoints)),
ypoints: Arc::new(RwLock::new(ypoints)),
xs: Arc::new(RwLock::new(xs)),
ys: Arc::new(RwLock::new(ys)),
};
@ -98,21 +104,30 @@ impl Spindex2d {
/// when constructing this index. May panic if the updated points argument is longer than the
/// original points.
pub fn update(&self, points: &[Point2d]) {
{
let mut xs = self.xs.write().unwrap();
let mut ys = self.ys.write().unwrap();
for (i, point) in points.iter().enumerate() {
let xi = self.xpoints[i].load(Relaxed);
let yi = self.ypoints[i].load(Relaxed);
let x = point.x;
let x = PVal { point: i, val: x };
xs[xi] = x;
thread::scope(|s| {
s.spawn(|| {
let mut xs = self.xs.write().unwrap();
let xpoints = self.xpoints.read().unwrap();
for (i, point) in points.iter().enumerate() {
let xi = xpoints[i];
let x = PVal::new(i, point.x);
xs[xi] = x;
}
});
s.spawn(|| {
let mut ys = self.ys.write().unwrap();
let ypoints = self.ypoints.read().unwrap();
for (i, point) in points.iter().enumerate() {
let yi = ypoints[i];
let y = PVal::new(i, point.y);
ys[yi] = y;
}
});
});
let y = point.y;
let y = PVal { point: i, val: y };
ys[yi] = y;
}
}
self.sort();
}
@ -122,9 +137,10 @@ impl Spindex2d {
let xhandle = thread::spawn(move || {
let mut xs = xs.write().unwrap();
xs.sort_unstable();
let mut points = points.write().unwrap();
for (i, x) in xs.iter().enumerate() {
let p = x.point;
points[p].store(i, Relaxed);
let p = x.point_index;
points[p] = i;
}
});
@ -133,9 +149,10 @@ impl Spindex2d {
let yhandle = thread::spawn(move || {
let mut ys = ys.write().unwrap();
ys.sort_unstable();
let mut points = points.write().unwrap();
for (i, y) in ys.iter().enumerate() {
let p = y.point;
points[p].store(i, Relaxed);
let p = y.point_index;
points[p] = i;
}
});