Skip to content

feat: make sure compiler can be shared between threads #59

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

Merged
merged 6 commits into from
Jul 7, 2025
Merged
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
24 changes: 12 additions & 12 deletions src/ast/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -315,25 +315,25 @@ pub enum AstKind<'a> {
}

impl<'a> AstKind<'a> {
pub fn as_tensor(&self) -> Option<&Tensor> {
pub fn as_tensor(&self) -> Option<&Tensor<'_>> {
match self {
AstKind::Tensor(m) => Some(m),
_ => None,
}
}
pub fn as_assignment(&self) -> Option<&Assignment> {
pub fn as_assignment(&self) -> Option<&Assignment<'_>> {
match self {
AstKind::Assignment(m) => Some(m),
_ => None,
}
}
pub fn as_model(&self) -> Option<&Model> {
pub fn as_model(&self) -> Option<&Model<'_>> {
match self {
AstKind::Model(m) => Some(m),
_ => None,
}
}
pub fn as_ds_model(&self) -> Option<&DsModel> {
pub fn as_ds_model(&self) -> Option<&DsModel<'_>> {
match self {
AstKind::DsModel(m) => Some(m),
_ => None,
Expand All @@ -345,7 +345,7 @@ impl<'a> AstKind<'a> {
_ => None,
}
}
pub fn as_domain(&self) -> Option<&Domain> {
pub fn as_domain(&self) -> Option<&Domain<'_>> {
match self {
AstKind::Domain(m) => Some(m),
_ => None,
Expand All @@ -357,31 +357,31 @@ impl<'a> AstKind<'a> {
_ => None,
}
}
pub fn as_call_arg(&self) -> Option<&CallArg> {
pub fn as_call_arg(&self) -> Option<&CallArg<'_>> {
match self {
AstKind::CallArg(m) => Some(m),
_ => None,
}
}
pub fn as_named_gradient(&self) -> Option<&NamedGradient> {
pub fn as_named_gradient(&self) -> Option<&NamedGradient<'_>> {
match self {
AstKind::NamedGradient(m) => Some(m),
_ => None,
}
}
pub fn as_name(&self) -> Option<&Name> {
pub fn as_name(&self) -> Option<&Name<'_>> {
match self {
AstKind::Name(n) => Some(n),
_ => None,
}
}
pub fn as_array(&self) -> Option<&Tensor> {
pub fn as_array(&self) -> Option<&Tensor<'_>> {
match self {
AstKind::Tensor(a) => Some(a),
_ => None,
}
}
pub fn as_vector(&self) -> Option<&Vector> {
pub fn as_vector(&self) -> Option<&Vector<'_>> {
match self {
AstKind::Vector(a) => Some(a),
_ => None,
Expand All @@ -401,13 +401,13 @@ impl<'a> AstKind<'a> {
_ => None,
}
}
pub fn as_indice(&self) -> Option<&Indice> {
pub fn as_indice(&self) -> Option<&Indice<'_>> {
match self {
AstKind::Indice(a) => Some(a),
_ => None,
}
}
pub fn as_tensor_elmt(&self) -> Option<&TensorElmt> {
pub fn as_tensor_elmt(&self) -> Option<&TensorElmt<'_>> {
match self {
AstKind::TensorElmt(a) => Some(a),
_ => None,
Expand Down
20 changes: 10 additions & 10 deletions src/discretise/discrete_model.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,10 +15,10 @@ use crate::ast::StringSpan;
use crate::continuous::ModelInfo;
use crate::continuous::Variable;

use super::ArcLayout;
use super::Env;
use super::Index;
use super::Layout;
use super::RcLayout;
use super::Tensor;
use super::TensorBlock;
use super::ValidationError;
Expand Down Expand Up @@ -191,8 +191,8 @@ impl<'s> DiscreteModel<'s> {
name,
start.clone(),
array.indices().to_vec(),
RcLayout::new(elmt_layout),
RcLayout::new(expr_layout),
ArcLayout::new(elmt_layout),
ArcLayout::new(expr_layout),
*expr,
));

Expand All @@ -219,7 +219,7 @@ impl<'s> DiscreteModel<'s> {
let tensor = Tensor::new(
array.name(),
elmts,
RcLayout::new(layout),
ArcLayout::new(layout),
array.indices().to_vec(),
);
//check that the number of indices matches the rank
Expand Down Expand Up @@ -702,26 +702,26 @@ impl<'s> DiscreteModel<'s> {
}
}

pub fn inputs(&self) -> &[Tensor] {
pub fn inputs(&self) -> &[Tensor<'_>] {
self.inputs.as_ref()
}

pub fn constant_defns(&self) -> &[Tensor] {
pub fn constant_defns(&self) -> &[Tensor<'_>] {
self.constant_defns.as_ref()
}

pub fn input_dep_defns(&self) -> &[Tensor] {
pub fn input_dep_defns(&self) -> &[Tensor<'_>] {
self.input_dep_defns.as_ref()
}

pub fn time_dep_defns(&self) -> &[Tensor] {
pub fn time_dep_defns(&self) -> &[Tensor<'_>] {
self.time_dep_defns.as_ref()
}
pub fn state_dep_defns(&self) -> &[Tensor] {
pub fn state_dep_defns(&self) -> &[Tensor<'_>] {
self.state_dep_defns.as_ref()
}

pub fn dstate_dep_defns(&self) -> &[Tensor] {
pub fn dstate_dep_defns(&self) -> &[Tensor<'_>] {
self.dstate_dep_defns.as_ref()
}

Expand Down
6 changes: 3 additions & 3 deletions src/discretise/env.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,12 @@ use ndarray::s;
use crate::ast::{self, Ast, AstKind, StringSpan};

use super::{
can_broadcast_to, layout::RcLayout, Layout, LayoutKind, Shape, Tensor, TensorBlock,
can_broadcast_to, layout::ArcLayout, Layout, LayoutKind, Shape, Tensor, TensorBlock,
ValidationError, ValidationErrors,
};

pub struct EnvVar {
layout: RcLayout,
layout: ArcLayout,
is_time_dependent: bool,
is_state_dependent: bool,
is_dstatedt_dependent: bool,
Expand Down Expand Up @@ -57,7 +57,7 @@ impl Env {
vars.insert(
"t".to_string(),
EnvVar {
layout: RcLayout::new(Layout::new_scalar()),
layout: ArcLayout::new(Layout::new_scalar()),
is_time_dependent: true,
is_state_dependent: false,
is_dstatedt_dependent: false,
Expand Down
32 changes: 19 additions & 13 deletions src/discretise/layout.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,13 @@
use anyhow::{anyhow, Result};
use itertools::Itertools;
use ndarray::s;
use std::{convert::AsRef, fmt, hash::Hash, hash::Hasher, ops::Deref, rc::Rc};
use std::{
convert::AsRef,
fmt,
hash::{Hash, Hasher},
ops::Deref,
sync::Arc,
};

use super::{broadcast_shapes, shape::Shape, tensor::Index, TensorBlock};

Expand Down Expand Up @@ -709,38 +715,38 @@ impl Layout {
}
}

// RcLayout is a wrapper for Rc<Layout> that implements Hash and PartialEq based on ptr equality
// ArcLayout is a wrapper for Arc<Layout> that implements Hash and PartialEq based on ptr equality
#[derive(Debug)]
pub struct RcLayout(Rc<Layout>);
impl Hash for RcLayout {
pub struct ArcLayout(Arc<Layout>);
impl Hash for ArcLayout {
fn hash<H: Hasher>(&self, state: &mut H) {
Rc::as_ptr(&self.0).hash(state);
Arc::as_ptr(&self.0).hash(state);
}
}
impl PartialEq for RcLayout {
impl PartialEq for ArcLayout {
fn eq(&self, other: &Self) -> bool {
Rc::ptr_eq(&self.0, &other.0)
Arc::ptr_eq(&self.0, &other.0)
}
}
impl Clone for RcLayout {
impl Clone for ArcLayout {
fn clone(&self) -> Self {
Self(self.0.clone())
}
}
impl Eq for RcLayout {}
impl Deref for RcLayout {
impl Eq for ArcLayout {}
impl Deref for ArcLayout {
type Target = Layout;
fn deref(&self) -> &Self::Target {
&self.0
}
}
impl AsRef<Layout> for RcLayout {
impl AsRef<Layout> for ArcLayout {
fn as_ref(&self) -> &Layout {
&self.0
}
}
impl RcLayout {
impl ArcLayout {
pub fn new(layout: Layout) -> Self {
Self(Rc::new(layout))
Self(Arc::new(layout))
}
}
2 changes: 1 addition & 1 deletion src/discretise/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ pub mod env;
pub use env::Env;

pub mod layout;
pub use layout::{Layout, LayoutKind, RcLayout};
pub use layout::{ArcLayout, Layout, LayoutKind};

pub mod shape;
pub use shape::{broadcast_shapes, can_broadcast_to, Shape};
Expand Down
28 changes: 14 additions & 14 deletions src/discretise/tensor.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ use ndarray::Array1;

use crate::ast::Ast;

use super::{Layout, RcLayout, Shape};
use super::{ArcLayout, Layout, Shape};

pub type Index = Array1<i64>;

Expand All @@ -14,8 +14,8 @@ pub struct TensorBlock<'s> {
name: Option<String>,
start: Index,
indices: Vec<char>,
layout: RcLayout,
expr_layout: RcLayout,
layout: ArcLayout,
expr_layout: ArcLayout,
expr: Ast<'s>,
tangent_expr: Ast<'s>,
}
Expand All @@ -25,8 +25,8 @@ impl<'s> TensorBlock<'s> {
name: Option<String>,
start: Index,
indices: Vec<char>,
layout: RcLayout,
expr_layout: RcLayout,
layout: ArcLayout,
expr_layout: ArcLayout,
expr: Ast<'s>,
) -> Self {
Self {
Expand All @@ -40,7 +40,7 @@ impl<'s> TensorBlock<'s> {
}
}
pub fn new_dense_vector(name: Option<String>, start: i64, shape: usize, expr: Ast<'s>) -> Self {
let layout = RcLayout::new(Layout::dense(Shape::from_vec(vec![shape])));
let layout = ArcLayout::new(Layout::dense(Shape::from_vec(vec![shape])));
Self {
name,
start: Index::from_vec(vec![start]),
Expand Down Expand Up @@ -84,11 +84,11 @@ impl<'s> TensorBlock<'s> {
self.layout.is_diagonal()
}

pub fn layout(&self) -> &RcLayout {
pub fn layout(&self) -> &ArcLayout {
&self.layout
}

pub fn expr_layout(&self) -> &RcLayout {
pub fn expr_layout(&self) -> &ArcLayout {
&self.expr_layout
}

Expand Down Expand Up @@ -130,7 +130,7 @@ impl fmt::Display for TensorBlock<'_> {
pub struct Tensor<'s> {
name: &'s str,
elmts: Vec<TensorBlock<'s>>,
layout: RcLayout,
layout: ArcLayout,
indices: Vec<char>,
}

Expand All @@ -140,7 +140,7 @@ impl<'s> Tensor<'s> {
name,
elmts: Vec::new(),
indices: Vec::new(),
layout: RcLayout::new(Layout::dense(Shape::zeros(0))),
layout: ArcLayout::new(Layout::dense(Shape::zeros(0))),
}
}

Expand All @@ -159,7 +159,7 @@ impl<'s> Tensor<'s> {
pub fn new(
name: &'s str,
elmts: Vec<TensorBlock<'s>>,
layout: RcLayout,
layout: ArcLayout,
indices: Vec<char>,
) -> Self {
Self {
Expand All @@ -172,10 +172,10 @@ impl<'s> Tensor<'s> {

pub fn new_no_layout(name: &'s str, elmts: Vec<TensorBlock<'s>>, indices: Vec<char>) -> Self {
if elmts.is_empty() {
Tensor::new("out", vec![], RcLayout::new(Layout::new_empty(0)), vec![])
Tensor::new("out", vec![], ArcLayout::new(Layout::new_empty(0)), vec![])
} else {
let layout = Layout::concatenate(elmts.as_slice(), indices.len()).unwrap();
Tensor::new(name, elmts, RcLayout::new(layout), indices)
Tensor::new(name, elmts, ArcLayout::new(layout), indices)
}
}

Expand All @@ -199,7 +199,7 @@ impl<'s> Tensor<'s> {
self.indices.as_ref()
}

pub fn layout_ptr(&self) -> &RcLayout {
pub fn layout_ptr(&self) -> &ArcLayout {
&self.layout
}

Expand Down
Loading
Loading