Skip to main content

Compiler

Struct Compiler 

Source
pub struct Compiler { /* private fields */ }

Implementations§

Source§

impl Compiler

The central hub of the Rust interface. It compiles a list of variables and expressions into a callable object (of type Application).

§Workflow

  1. Create terminals (variables and constants) and compose expressions using Expr methods:
    • Constructors: var, from, unary, binary, …
    • Standard algebraic operations: add, mul, …
    • Standard operators +, -, *, /, %, &, |, ^, !.
    • Unary functions such as sin, exp, and other standard mathematical functions.
    • Binary functions such as pow, min, …
    • IfElse operation ifelse(cond, true_val, false_val).
    • Heavide function: heaviside(x), which returns 1 if x >= 0; otherwise 0.
    • Comparison methods eq, ne, lt, le, gt, and ge.
    • Looping constructs sum and prod.
  2. Create a new Compiler object (say, comp) using one of its constructors: new() or with_compile_type(ty: CompilerType).
  3. Fine-tune the optimization passes using opt_level, simd, fastmath, and cse methods (optional).
  4. Define user-defined functions by called comp.def_unary and comp.def_binary (optional).
  5. Compile by calling comp.compile or comp.compile_params. The result is of type Application (say, app).
  6. Execute the compiled code using one of the app’s call functions:
    • call(&[f64]): scalar call.
    • call_params(&[f64], &[f64]): scalar call with parameters.
    • call_simd(&[__m256d]): simd call.
    • call_simd_params(&[__m256d], &[f64]): simd call with parameters.
  7. Optionally, generate a standalone fast function to execute.

§Examples

use anyhow::Result;
use symjit::{Compiler, Expr};

pub fn main() -> Result<()> {
    let x = Expr::var("x");
    let y = Expr::var("y");
    let u = &x + &y;
    let v = &x * &y;

    let mut config = Config::default();
    config.set_opt_level(2);
    let mut comp = Compiler::with_config(config);
    let mut app = comp.compile(&[x, y], &[u, v])?;
    let res = app.call(&[3.0, 5.0]);
    println!("{:?}", &res);

    Ok(())
}
Source

pub fn new() -> Compiler

Creates a new Compiler object with default settings.

Source

pub fn with_config(config: Config) -> Compiler

Source

pub fn compile(&mut self, states: &[Expr], obs: &[Expr]) -> Result<Application>

Compiles a model.

states is a list of variables, created by Expr::var. obs is a list of expressions.

Source

pub fn compile_params( &mut self, states: &[Expr], obs: &[Expr], params: &[Expr], ) -> Result<Application>

Compiles a model with parameters.

states is a list of variables, created by Expr::var. obs is a list of expressions. params is a list of parameters, created by Expr::var.

Note: for scalar functions, the difference between states and params is mostly by convenion. However, they are different in SIMD cases, as params are always f64.

Source§

impl Compiler

Source

pub fn translate( &mut self, json: String, num_params: usize, ) -> Result<Application>

Compiles a Symbolica model.

json is the JSON-encoded output of Symbolica export_instructions.

Example:

let params = vec![parse!("x"), parse!("y")];
let eval = parse!("x + y^2")
    .evaluator(&FunctionMap::new(), &params, OptimizationSettings::default())?

let json = serde_json::to_string(&eval.export_instructions())?;
let mut comp = Compiler::new();
let mut app = comp.translate(&json)?;
assert!(app.evaluate_single(&[2.0, 3.0]) == 11.0);

Auto Trait Implementations§

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T> IntoEither for T

Source§

fn into_either(self, into_left: bool) -> Either<Self, Self>

Converts self into a Left variant of Either<Self, Self> if into_left is true. Converts self into a Right variant of Either<Self, Self> otherwise. Read more
Source§

fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
where F: FnOnce(&Self) -> bool,

Converts self into a Left variant of Either<Self, Self> if into_left(&self) returns true. Converts self into a Right variant of Either<Self, Self> otherwise. Read more
Source§

impl<T> Pointable for T

Source§

const ALIGN: usize

The alignment of pointer.
Source§

type Init = T

The type for initializers.
Source§

unsafe fn init(init: <T as Pointable>::Init) -> usize

Initializes a with the given initializer. Read more
Source§

unsafe fn deref<'a>(ptr: usize) -> &'a T

Dereferences the given pointer. Read more
Source§

unsafe fn deref_mut<'a>(ptr: usize) -> &'a mut T

Mutably dereferences the given pointer. Read more
Source§

unsafe fn drop(ptr: usize)

Drops the object pointed to by the given pointer. Read more
Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.