Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Implement work-stealing queue approach to speed up parallel reduction #86

Closed
wants to merge 2 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
331 changes: 265 additions & 66 deletions Cargo.lock

Large diffs are not rendered by default.

4 changes: 3 additions & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "hvm-core"
version = "0.2.21"
version = "0.2.22"
edition = "2021"
description = "HVM-Core is a massively parallel Interaction Combinator evaluator."
license = "MIT"
Expand All @@ -27,6 +27,8 @@ arrayvec = "0.7.4"
clap = { version = "4.5.1", features = ["derive"] }
nohash-hasher = { version = "0.2.0" }
stacker = "0.1.15"
fastrand = "2.0.1"
st3 = "0.4.1"

##--COMPILER-CUTOFF--##

Expand Down
1 change: 1 addition & 0 deletions cspell.json
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
"dref",
"dups",
"effectful",
"fastrand",
"fmts",
"fuzzer",
"hasher",
Expand Down
5 changes: 4 additions & 1 deletion src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -513,7 +513,10 @@ fn compile_executable(target: &str, host: Arc<Mutex<host::Host>>) -> Result<(),
process::exit(1);
}

fs::copy(".hvm/target/release/hvmc", target)?;
#[cfg(not(target_os = "windows"))]
fs::copy("./.hvm/target/release/hvmc", target)?;
#[cfg(target_os = "windows")]
fs::copy("./.hvm/target/release/hvmc.exe", target + ".exe")?;

Ok(())
}
3 changes: 1 addition & 2 deletions src/run.rs
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,6 @@ use std::{
mem::size_of,
ops::{Add, AddAssign, Deref, DerefMut},
sync::{Arc, Barrier},
thread,
};

#[cfg(feature = "_fuzz")]
Expand All @@ -48,7 +47,7 @@ use crate::fuzz::spin_loop;
#[cfg(not(feature = "_fuzz"))]
fn spin_loop() {} // this could use `std::hint::spin_loop`, but in practice it hurts performance

use atomic::{AtomicU64, AtomicUsize, Ordering::Relaxed};
use atomic::{AtomicU64, Ordering::Relaxed};

use Tag::*;

Expand Down
39 changes: 25 additions & 14 deletions src/run/linker.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
use st3::lifo::Worker;

use super::*;

/// Stores extra data needed about the nodes when in lazy mode. (In strict mode,
Expand All @@ -9,6 +11,8 @@ pub(super) struct Header {
pub(super) targ: Port,
}

pub type Redex = (Port, Port);

/// Manages linking ports and wires within the net.
///
/// When threads interfere, this uses the atomic linking algorithm described in
Expand All @@ -24,13 +28,18 @@ pub struct Linker<'h, M: Mode> {
_mode: PhantomData<M>,
}

pub const WORKER_QUEUE_CAPACITY: usize = 1 << 22;

deref!({<'h, M: Mode>} Linker<'h, M> => self.allocator: Allocator<'h>);

impl<'h, M: Mode> Linker<'h, M> {
pub fn new(heap: &'h Heap) -> Self {
Linker::new_with_worker(heap, Worker::new(WORKER_QUEUE_CAPACITY))
}
pub(super) fn new_with_worker(heap: &'h Heap, worker: Worker<Redex>) -> Self {
Linker {
allocator: Allocator::new(heap),
redexes: RedexQueue::default(),
redexes: RedexQueue { fast: Vec::new(), slow: worker },
rwts: Default::default(),
headers: Default::default(),
_mode: PhantomData,
Expand Down Expand Up @@ -96,7 +105,7 @@ impl<'h, M: Mode> Linker<'h, M> {
if redex_would_shrink(&a, &b) {
self.redexes.fast.push((a, b));
} else {
self.redexes.slow.push((a, b));
self.redexes.slow.push((a, b)).expect("exceeded redex queue capacity");
}
} else {
self.set_header(a.clone(), b.clone());
Expand Down Expand Up @@ -350,10 +359,10 @@ impl<'h, M: Mode> Linker<'h, M> {
}
}

#[derive(Debug, Default)]
#[derive(Debug)]
pub struct RedexQueue {
pub(super) fast: Vec<(Port, Port)>,
pub(super) slow: Vec<(Port, Port)>,
pub(super) fast: Vec<Redex>,
pub(super) slow: Worker<Redex>,
}

impl RedexQueue {
Expand All @@ -363,29 +372,31 @@ impl RedexQueue {
self.fast.pop().or_else(|| self.slow.pop())
}
#[inline(always)]
pub fn len(&self) -> usize {
self.fast.len() + self.slow.len()
}
#[inline(always)]
pub fn is_empty(&self) -> bool {
self.fast.is_empty() && self.slow.is_empty()
}
#[inline(always)]
pub fn drain(&mut self) -> impl Iterator<Item = (Port, Port)> + '_ {
self.fast.drain(..).chain(self.slow.drain(..))
self.fast.drain(..).chain(self.slow.drain(|x| x).unwrap())
}
#[inline(always)]
pub fn iter(&self) -> impl Iterator<Item = &(Port, Port)> {
self.fast.iter().chain(self.slow.iter())
// TODO
// todo!();
[].into_iter()
// self.fast.iter().chain(self.slow.)
}
#[inline(always)]
pub fn iter_mut(&mut self) -> impl Iterator<Item = &mut (Port, Port)> {
self.fast.iter_mut().chain(self.slow.iter_mut())
todo!();
[].into_iter()
// self.fast.iter_mut().chain(self.slow.iter_mut())
}
#[inline(always)]
pub fn clear(&mut self) {
self.fast.clear();
self.slow.clear();
todo!();
// self.fast.clear();
// self.slow.clear();
}
}

Expand Down
5 changes: 5 additions & 0 deletions src/run/net.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
use std::mem::MaybeUninit;

use st3::lifo::Worker;

use super::*;

/// An interaction combinator net.
Expand All @@ -24,6 +26,9 @@ impl<'h, M: Mode> Net<'h, M> {
pub(super) fn new_with_root(heap: &'h Heap, root: Wire) -> Self {
Net { linker: Linker::new(heap), tid: 0, tids: 1, trgs: Box::new_uninit_slice(1 << 16), root }
}
pub(super) fn new_with_root_worker(heap: &'h Heap, root: Wire, worker: Worker<Redex>) -> Self {
Net { linker: Linker::new_with_worker(heap, worker), tid: 0, tids: 1, trgs: Box::new_uninit_slice(1 << 16), root }
}

/// Boots a net from a Def.
pub fn boot(&mut self, def: &Def) {
Expand Down
Loading
Loading