Skip to content

Commit 083cb16

Browse files
feat: make sure compiler can be shared between threads (#59)
* feat: make sure compiler is Send * wrap cranelift module in mutex so still Sync * module should be sync now as well * remove not-used code * fix dead_code * fix clippy warnings
1 parent d1c9f24 commit 083cb16

File tree

15 files changed

+507
-428
lines changed

15 files changed

+507
-428
lines changed

src/ast/mod.rs

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -315,25 +315,25 @@ pub enum AstKind<'a> {
315315
}
316316

317317
impl<'a> AstKind<'a> {
318-
pub fn as_tensor(&self) -> Option<&Tensor> {
318+
pub fn as_tensor(&self) -> Option<&Tensor<'_>> {
319319
match self {
320320
AstKind::Tensor(m) => Some(m),
321321
_ => None,
322322
}
323323
}
324-
pub fn as_assignment(&self) -> Option<&Assignment> {
324+
pub fn as_assignment(&self) -> Option<&Assignment<'_>> {
325325
match self {
326326
AstKind::Assignment(m) => Some(m),
327327
_ => None,
328328
}
329329
}
330-
pub fn as_model(&self) -> Option<&Model> {
330+
pub fn as_model(&self) -> Option<&Model<'_>> {
331331
match self {
332332
AstKind::Model(m) => Some(m),
333333
_ => None,
334334
}
335335
}
336-
pub fn as_ds_model(&self) -> Option<&DsModel> {
336+
pub fn as_ds_model(&self) -> Option<&DsModel<'_>> {
337337
match self {
338338
AstKind::DsModel(m) => Some(m),
339339
_ => None,
@@ -345,7 +345,7 @@ impl<'a> AstKind<'a> {
345345
_ => None,
346346
}
347347
}
348-
pub fn as_domain(&self) -> Option<&Domain> {
348+
pub fn as_domain(&self) -> Option<&Domain<'_>> {
349349
match self {
350350
AstKind::Domain(m) => Some(m),
351351
_ => None,
@@ -357,31 +357,31 @@ impl<'a> AstKind<'a> {
357357
_ => None,
358358
}
359359
}
360-
pub fn as_call_arg(&self) -> Option<&CallArg> {
360+
pub fn as_call_arg(&self) -> Option<&CallArg<'_>> {
361361
match self {
362362
AstKind::CallArg(m) => Some(m),
363363
_ => None,
364364
}
365365
}
366-
pub fn as_named_gradient(&self) -> Option<&NamedGradient> {
366+
pub fn as_named_gradient(&self) -> Option<&NamedGradient<'_>> {
367367
match self {
368368
AstKind::NamedGradient(m) => Some(m),
369369
_ => None,
370370
}
371371
}
372-
pub fn as_name(&self) -> Option<&Name> {
372+
pub fn as_name(&self) -> Option<&Name<'_>> {
373373
match self {
374374
AstKind::Name(n) => Some(n),
375375
_ => None,
376376
}
377377
}
378-
pub fn as_array(&self) -> Option<&Tensor> {
378+
pub fn as_array(&self) -> Option<&Tensor<'_>> {
379379
match self {
380380
AstKind::Tensor(a) => Some(a),
381381
_ => None,
382382
}
383383
}
384-
pub fn as_vector(&self) -> Option<&Vector> {
384+
pub fn as_vector(&self) -> Option<&Vector<'_>> {
385385
match self {
386386
AstKind::Vector(a) => Some(a),
387387
_ => None,
@@ -401,13 +401,13 @@ impl<'a> AstKind<'a> {
401401
_ => None,
402402
}
403403
}
404-
pub fn as_indice(&self) -> Option<&Indice> {
404+
pub fn as_indice(&self) -> Option<&Indice<'_>> {
405405
match self {
406406
AstKind::Indice(a) => Some(a),
407407
_ => None,
408408
}
409409
}
410-
pub fn as_tensor_elmt(&self) -> Option<&TensorElmt> {
410+
pub fn as_tensor_elmt(&self) -> Option<&TensorElmt<'_>> {
411411
match self {
412412
AstKind::TensorElmt(a) => Some(a),
413413
_ => None,

src/discretise/discrete_model.rs

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -15,10 +15,10 @@ use crate::ast::StringSpan;
1515
use crate::continuous::ModelInfo;
1616
use crate::continuous::Variable;
1717

18+
use super::ArcLayout;
1819
use super::Env;
1920
use super::Index;
2021
use super::Layout;
21-
use super::RcLayout;
2222
use super::Tensor;
2323
use super::TensorBlock;
2424
use super::ValidationError;
@@ -191,8 +191,8 @@ impl<'s> DiscreteModel<'s> {
191191
name,
192192
start.clone(),
193193
array.indices().to_vec(),
194-
RcLayout::new(elmt_layout),
195-
RcLayout::new(expr_layout),
194+
ArcLayout::new(elmt_layout),
195+
ArcLayout::new(expr_layout),
196196
*expr,
197197
));
198198

@@ -219,7 +219,7 @@ impl<'s> DiscreteModel<'s> {
219219
let tensor = Tensor::new(
220220
array.name(),
221221
elmts,
222-
RcLayout::new(layout),
222+
ArcLayout::new(layout),
223223
array.indices().to_vec(),
224224
);
225225
//check that the number of indices matches the rank
@@ -702,26 +702,26 @@ impl<'s> DiscreteModel<'s> {
702702
}
703703
}
704704

705-
pub fn inputs(&self) -> &[Tensor] {
705+
pub fn inputs(&self) -> &[Tensor<'_>] {
706706
self.inputs.as_ref()
707707
}
708708

709-
pub fn constant_defns(&self) -> &[Tensor] {
709+
pub fn constant_defns(&self) -> &[Tensor<'_>] {
710710
self.constant_defns.as_ref()
711711
}
712712

713-
pub fn input_dep_defns(&self) -> &[Tensor] {
713+
pub fn input_dep_defns(&self) -> &[Tensor<'_>] {
714714
self.input_dep_defns.as_ref()
715715
}
716716

717-
pub fn time_dep_defns(&self) -> &[Tensor] {
717+
pub fn time_dep_defns(&self) -> &[Tensor<'_>] {
718718
self.time_dep_defns.as_ref()
719719
}
720-
pub fn state_dep_defns(&self) -> &[Tensor] {
720+
pub fn state_dep_defns(&self) -> &[Tensor<'_>] {
721721
self.state_dep_defns.as_ref()
722722
}
723723

724-
pub fn dstate_dep_defns(&self) -> &[Tensor] {
724+
pub fn dstate_dep_defns(&self) -> &[Tensor<'_>] {
725725
self.dstate_dep_defns.as_ref()
726726
}
727727

src/discretise/env.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,12 +5,12 @@ use ndarray::s;
55
use crate::ast::{self, Ast, AstKind, StringSpan};
66

77
use super::{
8-
can_broadcast_to, layout::RcLayout, Layout, LayoutKind, Shape, Tensor, TensorBlock,
8+
can_broadcast_to, layout::ArcLayout, Layout, LayoutKind, Shape, Tensor, TensorBlock,
99
ValidationError, ValidationErrors,
1010
};
1111

1212
pub struct EnvVar {
13-
layout: RcLayout,
13+
layout: ArcLayout,
1414
is_time_dependent: bool,
1515
is_state_dependent: bool,
1616
is_dstatedt_dependent: bool,
@@ -57,7 +57,7 @@ impl Env {
5757
vars.insert(
5858
"t".to_string(),
5959
EnvVar {
60-
layout: RcLayout::new(Layout::new_scalar()),
60+
layout: ArcLayout::new(Layout::new_scalar()),
6161
is_time_dependent: true,
6262
is_state_dependent: false,
6363
is_dstatedt_dependent: false,

src/discretise/layout.rs

Lines changed: 19 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,13 @@
11
use anyhow::{anyhow, Result};
22
use itertools::Itertools;
33
use ndarray::s;
4-
use std::{convert::AsRef, fmt, hash::Hash, hash::Hasher, ops::Deref, rc::Rc};
4+
use std::{
5+
convert::AsRef,
6+
fmt,
7+
hash::{Hash, Hasher},
8+
ops::Deref,
9+
sync::Arc,
10+
};
511

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

@@ -709,38 +715,38 @@ impl Layout {
709715
}
710716
}
711717

712-
// RcLayout is a wrapper for Rc<Layout> that implements Hash and PartialEq based on ptr equality
718+
// ArcLayout is a wrapper for Arc<Layout> that implements Hash and PartialEq based on ptr equality
713719
#[derive(Debug)]
714-
pub struct RcLayout(Rc<Layout>);
715-
impl Hash for RcLayout {
720+
pub struct ArcLayout(Arc<Layout>);
721+
impl Hash for ArcLayout {
716722
fn hash<H: Hasher>(&self, state: &mut H) {
717-
Rc::as_ptr(&self.0).hash(state);
723+
Arc::as_ptr(&self.0).hash(state);
718724
}
719725
}
720-
impl PartialEq for RcLayout {
726+
impl PartialEq for ArcLayout {
721727
fn eq(&self, other: &Self) -> bool {
722-
Rc::ptr_eq(&self.0, &other.0)
728+
Arc::ptr_eq(&self.0, &other.0)
723729
}
724730
}
725-
impl Clone for RcLayout {
731+
impl Clone for ArcLayout {
726732
fn clone(&self) -> Self {
727733
Self(self.0.clone())
728734
}
729735
}
730-
impl Eq for RcLayout {}
731-
impl Deref for RcLayout {
736+
impl Eq for ArcLayout {}
737+
impl Deref for ArcLayout {
732738
type Target = Layout;
733739
fn deref(&self) -> &Self::Target {
734740
&self.0
735741
}
736742
}
737-
impl AsRef<Layout> for RcLayout {
743+
impl AsRef<Layout> for ArcLayout {
738744
fn as_ref(&self) -> &Layout {
739745
&self.0
740746
}
741747
}
742-
impl RcLayout {
748+
impl ArcLayout {
743749
pub fn new(layout: Layout) -> Self {
744-
Self(Rc::new(layout))
750+
Self(Arc::new(layout))
745751
}
746752
}

src/discretise/mod.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ pub mod env;
88
pub use env::Env;
99

1010
pub mod layout;
11-
pub use layout::{Layout, LayoutKind, RcLayout};
11+
pub use layout::{ArcLayout, Layout, LayoutKind};
1212

1313
pub mod shape;
1414
pub use shape::{broadcast_shapes, can_broadcast_to, Shape};

src/discretise/tensor.rs

Lines changed: 14 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ use ndarray::Array1;
44

55
use crate::ast::Ast;
66

7-
use super::{Layout, RcLayout, Shape};
7+
use super::{ArcLayout, Layout, Shape};
88

99
pub type Index = Array1<i64>;
1010

@@ -14,8 +14,8 @@ pub struct TensorBlock<'s> {
1414
name: Option<String>,
1515
start: Index,
1616
indices: Vec<char>,
17-
layout: RcLayout,
18-
expr_layout: RcLayout,
17+
layout: ArcLayout,
18+
expr_layout: ArcLayout,
1919
expr: Ast<'s>,
2020
tangent_expr: Ast<'s>,
2121
}
@@ -25,8 +25,8 @@ impl<'s> TensorBlock<'s> {
2525
name: Option<String>,
2626
start: Index,
2727
indices: Vec<char>,
28-
layout: RcLayout,
29-
expr_layout: RcLayout,
28+
layout: ArcLayout,
29+
expr_layout: ArcLayout,
3030
expr: Ast<'s>,
3131
) -> Self {
3232
Self {
@@ -40,7 +40,7 @@ impl<'s> TensorBlock<'s> {
4040
}
4141
}
4242
pub fn new_dense_vector(name: Option<String>, start: i64, shape: usize, expr: Ast<'s>) -> Self {
43-
let layout = RcLayout::new(Layout::dense(Shape::from_vec(vec![shape])));
43+
let layout = ArcLayout::new(Layout::dense(Shape::from_vec(vec![shape])));
4444
Self {
4545
name,
4646
start: Index::from_vec(vec![start]),
@@ -84,11 +84,11 @@ impl<'s> TensorBlock<'s> {
8484
self.layout.is_diagonal()
8585
}
8686

87-
pub fn layout(&self) -> &RcLayout {
87+
pub fn layout(&self) -> &ArcLayout {
8888
&self.layout
8989
}
9090

91-
pub fn expr_layout(&self) -> &RcLayout {
91+
pub fn expr_layout(&self) -> &ArcLayout {
9292
&self.expr_layout
9393
}
9494

@@ -130,7 +130,7 @@ impl fmt::Display for TensorBlock<'_> {
130130
pub struct Tensor<'s> {
131131
name: &'s str,
132132
elmts: Vec<TensorBlock<'s>>,
133-
layout: RcLayout,
133+
layout: ArcLayout,
134134
indices: Vec<char>,
135135
}
136136

@@ -140,7 +140,7 @@ impl<'s> Tensor<'s> {
140140
name,
141141
elmts: Vec::new(),
142142
indices: Vec::new(),
143-
layout: RcLayout::new(Layout::dense(Shape::zeros(0))),
143+
layout: ArcLayout::new(Layout::dense(Shape::zeros(0))),
144144
}
145145
}
146146

@@ -159,7 +159,7 @@ impl<'s> Tensor<'s> {
159159
pub fn new(
160160
name: &'s str,
161161
elmts: Vec<TensorBlock<'s>>,
162-
layout: RcLayout,
162+
layout: ArcLayout,
163163
indices: Vec<char>,
164164
) -> Self {
165165
Self {
@@ -172,10 +172,10 @@ impl<'s> Tensor<'s> {
172172

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

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

202-
pub fn layout_ptr(&self) -> &RcLayout {
202+
pub fn layout_ptr(&self) -> &ArcLayout {
203203
&self.layout
204204
}
205205

0 commit comments

Comments
 (0)