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

version 0.3.0 #39

Merged
merged 13 commits into from
Jan 21, 2025
36 changes: 18 additions & 18 deletions Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "diffsl"
version = "0.2.6"
version = "0.3.0"
edition = "2021"
description = "A compiler for a domain-specific language for ordinary differential equations (ODE)."
license-file = "LICENSE.txt"
Expand All @@ -22,13 +22,13 @@ test_compile = []
rayon = ["dep:rayon"]

[dependencies]
ndarray = { version = ">=0.15.0", features = ["approx-0_5"] }
anyhow = "1.0"
approx = ">=0.5"
pest = ">=2.1.3"
pest_derive = ">=2.1.0"
itertools = ">=0.10.3"
uid = "0.1.7"
ndarray = { version = "=0.16.1" }
anyhow = "1.0.95"
approx = "0.5"
pest = "2.7.15"
pest_derive = "2.7.15"
itertools = "0.14.0"
uid = "0.1.8"
inkwell-150 = { package = "inkwell", version = ">=0.5.0", features = ["llvm15-0"], optional = true }
inkwell-160 = { package = "inkwell", version = ">=0.5.0", features = ["llvm16-0"], optional = true }
inkwell-170 = { package = "inkwell", version = ">=0.5.0", features = ["llvm17-0"], optional = true }
Expand All @@ -37,23 +37,23 @@ llvm-sys-150 = { package = "llvm-sys", version = "150.0.3", optional = true }
llvm-sys-160 = { package = "llvm-sys", version = "160.1.0", optional = true }
llvm-sys-170 = { package = "llvm-sys", version = "170.0.1", optional = true }
llvm-sys-180 = { package = "llvm-sys", version = "180.0.0", optional = true }
inkwell_internals = { version = "0.9.0", optional = true }
cranelift = "0.110.1"
cranelift-module = "0.110.1"
cranelift-jit = "0.110.1"
cranelift-native = "0.110.1"
target-lexicon = "0.12.16"
inkwell_internals = { version = "0.10.0", optional = true }
cranelift = "0.115.1"
cranelift-module = "0.115.1"
cranelift-jit = "0.115.1"
cranelift-native = "0.115.1"
target-lexicon = "0.13.1"
aliasable = "0.1.3"
rayon = { version="1.10.0", optional = true }
lazy_static = "1.5.0"

[build-dependencies]
bindgen = { version = "0.69.4", optional = true }
cmake = { version = "0.1.50", optional = true }
bindgen = { version = "0.71.1", optional = true }
cmake = { version = "0.1.52", optional = true }

[dev-dependencies]
divan = "0.1.14"
env_logger = "0.11.5"
divan = "0.1.17"
env_logger = "0.11.6"

[[bench]]
name = "evaluation"
Expand Down
79 changes: 73 additions & 6 deletions src/execution/compiler.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,8 @@ use crate::{

use super::{
interface::{
BarrierInitFunc, CalcOutGradientFunc, RhsGradientFunc, SetInputsGradientFunc,
U0GradientFunc,
BarrierInitFunc, CalcOutGradientFunc, CalcOutReverseGradientFunc, RhsGradientFunc,
SetInputsGradientFunc, U0GradientFunc,
},
module::CodegenModule,
};
Expand Down Expand Up @@ -59,7 +59,7 @@ struct JitGradFunctions {
struct JitGradRFunctions {
set_u0_rgrad: U0GradientFunc,
rhs_rgrad: RhsGradientFunc,
calc_out_rgrad: CalcOutGradientFunc,
calc_out_rgrad: CalcOutReverseGradientFunc,
set_inputs_rgrad: SetInputsGradientFunc,
}

Expand Down Expand Up @@ -226,7 +226,7 @@ impl<M: CodegenModule> Compiler<M> {
)
},
calc_out_rgrad: unsafe {
std::mem::transmute::<*const u8, CalcOutGradientFunc>(
std::mem::transmute::<*const u8, CalcOutReverseGradientFunc>(
module.jit(calc_out_rgrad.unwrap())?,
)
},
Expand Down Expand Up @@ -583,8 +583,8 @@ impl<M: CodegenModule> Compiler<M> {
.calc_out_rgrad)(
t,
yy.as_ptr(),
dyy.as_ptr(),
data.as_ptr() as *mut f64,
dyy.as_ptr() as *mut f64,
data.as_ptr(),
ddata.as_ptr() as *mut f64,
i,
dim,
Expand Down Expand Up @@ -1271,6 +1271,73 @@ mod tests {
big_state_tridiag: "b_ij { (0..100, 0..100): 3.0, (0..99, 1..100): 2.0, (1..100, 0..99): 1.0, (0, 99): 1.0, (99, 0): 2.0 } r_i { b_ij * u_j }" expect "r" vec![6.; 100]; vec![6.; 100]; vec![600.],
}

#[cfg(feature = "llvm")]
#[test]
fn test_bad_big_state_expr() {
let full_text = "
in = [p]
p { 1 }
u_i {
(0:50): x = p,
(50:100): y = p,
}
r_i { x_i }
F_i { u_i }";
let model = parse_ds_string(full_text).unwrap();
let discrete_model = DiscreteModel::build("test_bad_big_state_expr", &model).unwrap();
let compiler = Compiler::<crate::LlvmModule>::from_discrete_model(
&discrete_model,
CompilerMode::MultiThreaded(None),
)
.unwrap();
let mut data = compiler.get_new_data();
let inputs = vec![1.];
compiler.set_inputs(inputs.as_slice(), data.as_mut_slice());
let mut u0 = vec![0.; 100];
compiler.set_u0(u0.as_mut_slice(), data.as_mut_slice());
let mut res = vec![0.; 100];
compiler.rhs(0., u0.as_slice(), data.as_mut_slice(), res.as_mut_slice());
compiler.calc_out(0., u0.as_slice(), data.as_mut_slice());
let mut ddata = compiler.get_new_data();
let dr = compiler
.get_tensor_data_mut("r", ddata.as_mut_slice())
.unwrap();
let mut dinputs = vec![0.; 1];
dr.fill(1.);
let mut dres = vec![0.; 100];
let mut du0 = vec![0.; 100];
compiler.calc_out_rgrad(
0.,
u0.as_slice(),
du0.as_mut_slice(),
data.as_mut_slice(),
ddata.as_mut_slice(),
);
assert_relative_eq!(du0[0..50], vec![1.; 50].as_slice());
compiler.rhs_rgrad(
0.,
u0.as_slice(),
du0.as_mut_slice(),
data.as_mut_slice(),
ddata.as_mut_slice(),
res.as_slice(),
dres.as_mut_slice(),
);
compiler.set_u0_rgrad(
u0.as_mut_slice(),
du0.as_mut_slice(),
data.as_mut_slice(),
ddata.as_mut_slice(),
);
compiler.set_inputs_rgrad(
inputs.as_slice(),
dinputs.as_mut_slice(),
data.as_mut_slice(),
ddata.as_mut_slice(),
);
assert_relative_eq!(dinputs.as_slice(), vec![50.].as_slice());
}

#[test]
fn test_repeated_grad_cranelift() {
test_repeated_grad_common::<CraneliftModule>();
Expand Down
9 changes: 9 additions & 0 deletions src/execution/interface.rs
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,15 @@ pub type CalcOutGradientFunc = unsafe extern "C" fn(
threadId: UIntType,
threadDim: UIntType,
);
pub type CalcOutReverseGradientFunc = unsafe extern "C" fn(
time: RealType,
u: *const RealType,
du: *mut RealType,
data: *const RealType,
ddata: *mut RealType,
threadId: UIntType,
threadDim: UIntType,
);
pub type GetDimsFunc = unsafe extern "C" fn(
states: *mut UIntType,
inputs: *mut UIntType,
Expand Down
Loading
Loading