Skip to content

Commit

Permalink
feat: multithreaded execution (#34)
Browse files Browse the repository at this point in the history
* cranelift threads seem to work

* threading works for llvm cranelift, working on interleaving diagonals

* think I've fixed the interleaving diags

* threads working for multi-threaded tests

* max threads is nstates / 10

* allow user to override thread_dim

* cargo fmt

* clippy

* make sure we don't use more threads than states

* place a barrier before multithreading call

* update comment

* cranelift use i32
  • Loading branch information
martinjrobins authored Jan 3, 2025
1 parent 954fec3 commit 0181574
Show file tree
Hide file tree
Showing 9 changed files with 1,370 additions and 255 deletions.
3 changes: 3 additions & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -11,13 +11,15 @@ repository = "https://github.com/martinjrobins/diffsl"
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html

[features]
default = ["rayon"]
llvm15-0 = ["inkwell-150", "llvm-sys-150", "llvm", "enzyme"]
llvm16-0 = ["inkwell-160", "llvm-sys-160", "llvm", "enzyme"]
llvm17-0 = ["inkwell-170", "llvm-sys-170", "llvm", "enzyme"]
llvm18-0 = ["inkwell-180", "llvm-sys-180", "llvm", "enzyme"]
enzyme = ["bindgen", "cmake"]
llvm = []
test_compile = []
rayon = ["dep:rayon"]

[dependencies]
ndarray = { version = ">=0.15.0", features = ["approx-0_5"] }
Expand All @@ -42,6 +44,7 @@ cranelift-jit = "0.110.1"
cranelift-native = "0.110.1"
target-lexicon = "0.12.16"
aliasable = "0.1.3"
rayon = { version="1.10.0", optional = true }

[build-dependencies]
bindgen = { version = "0.69.4", optional = true }
Expand Down
2 changes: 1 addition & 1 deletion benches/evaluation.rs
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ fn setup<M: CodegenModule>(n: usize, f_text: &str, name: &str) -> Compiler<M> {
);
let model = parse_ds_string(&full_text).unwrap();
let discrete_model = DiscreteModel::build(name, &model).unwrap();
Compiler::from_discrete_model(&discrete_model).unwrap()
Compiler::from_discrete_model(&discrete_model, Default::default()).unwrap()
}

#[cfg(feature = "llvm")]
Expand Down
60 changes: 60 additions & 0 deletions src/discretise/discrete_model.rs
Original file line number Diff line number Diff line change
Expand Up @@ -732,6 +732,7 @@ mod tests {
use crate::{
continuous::ModelInfo,
discretise::DiscreteModel,
execution::Translation,
parser::{parse_ds_string, parse_ms_string},
};

Expand Down Expand Up @@ -1227,4 +1228,63 @@ mod tests {
assert_eq!(model.out().elmts().len(), 1);
assert_eq!(model.out().elmts()[0].expr().to_string(), "u_i");
}

#[test]
fn test_sparse_layout() {
let text = "
u_i {
y = 1,
}
r_ij {
(0..3, 0..3): 1,
(1..3, 0..2): 3,
}
b_ij {
(0, 0): 1,
(1, 0): 3,
(1, 1): 1,
(2, 1): 3,
(2, 2): 1,
}
F_i {
y,
}
";
let model = parse_ds_string(text).unwrap();
let model = DiscreteModel::build("$name", &model).unwrap();
let r = model
.time_indep_defns()
.iter()
.find(|t| t.name() == "r")
.unwrap();
let b = model
.time_indep_defns()
.iter()
.find(|t| t.name() == "b")
.unwrap();
for tensor in [r, b] {
let layout = tensor.layout();
assert_eq!(layout.shape()[0], 3);
assert_eq!(layout.shape()[1], 3);
assert_eq!(
layout.indices().map(|i| i.to_string()).collect::<Vec<_>>(),
vec!["[0, 0]", "[1, 0]", "[1, 1]", "[2, 1]", "[2, 2]"]
);
assert_eq!(layout.to_data_layout(), vec![0, 0, 1, 0, 1, 1, 2, 1, 2, 2]);
}
let translation = Translation::new(
r.elmts()[0].expr_layout(),
r.elmts()[0].layout(),
r.elmts()[0].start(),
r.layout_ptr(),
);
assert_eq!(translation.to_data_layout(), vec![0, 2, 4]);
let translation = Translation::new(
r.elmts()[1].expr_layout(),
r.elmts()[1].layout(),
r.elmts()[1].start(),
r.layout_ptr(),
);
assert_eq!(translation.to_data_layout(), vec![1, 3]);
}
}
Loading

0 comments on commit 0181574

Please sign in to comment.