Skip to content

Commit

Permalink
Add new expression evaluator to Python API
Browse files Browse the repository at this point in the history
- Fix ASM output for repeated register occurrences
  • Loading branch information
benruijl committed Aug 16, 2024
1 parent 938d22e commit bb49f9e
Show file tree
Hide file tree
Showing 5 changed files with 627 additions and 172 deletions.
68 changes: 11 additions & 57 deletions examples/nested_evaluation.rs
Original file line number Diff line number Diff line change
@@ -1,9 +1,7 @@
use std::time::Instant;

use symbolica::{
atom::{Atom, AtomView},
domains::{float::Complex, rational::Rational},
evaluate::{CompileOptions, ExpressionEvaluator, FunctionMap, InlineASM, OptimizationSettings},
atom::Atom,
domains::rational::Rational,
evaluate::{CompileOptions, FunctionMap, InlineASM, OptimizationSettings},
state::State,
};

Expand Down Expand Up @@ -73,64 +71,20 @@ fn main() {
OptimizationSettings::default(),
)
.unwrap();

let mut e_f64 = evaluator.map_coeff(&|x| x.into());
let r = e_f64.evaluate(&[5.]);
let r = e_f64.evaluate_single(&[5.]);
println!("{}", r);

let mut tree =
AtomView::to_eval_tree_multiple(&[e1.as_view(), e2.as_view()], &fn_map, &params).unwrap();

// optimize the tree using an occurrence-order Horner scheme
println!("Op original {:?}", tree.count_operations());
tree.horner_scheme();
println!("Op horner {:?}", tree.count_operations());
tree.common_subexpression_elimination();
println!("op cse {:?}", tree.count_operations());

//tree.common_pair_elimination();
//println!("op cpe {:?}", tree.count_operations());

let mut evaluator: ExpressionEvaluator<f64> = tree.linearize(None).map_coeff(&|x| x.into());

let ce = evaluator
.export_cpp(
"nested_evaluation.cpp",
"evaltest",
true,
InlineASM::default(),
)
let mut compiled = e_f64
.export_cpp("nested_evaluate.cpp", "nested", true, InlineASM::Intel)
.unwrap()
.compile("libneval.so", CompileOptions::default())
.compile("nested", CompileOptions::default())
.unwrap()
.load()
.unwrap();

let params = vec![5.];
let mut out = vec![0., 0.];
ce.evaluate(&params, &mut out);
println!("Eval from C++: {}, {}", out[0], out[1]);

{
let params = vec![Complex::new(5., 0.)];
let mut out = vec![Complex::new_zero(), Complex::new_zero()];
ce.evaluate(&params, &mut out);
println!("Eval from C++: {}, {}", out[0], out[1]);
}

// benchmark
let t = Instant::now();
for _ in 0..1000000 {
let _ = ce.evaluate(&params, &mut out);
}
println!("C++ time {:#?}", t.elapsed());

evaluator.evaluate_multiple(&params, &mut out);
println!("Eval: {}, {}", out[0], out[1]);

let params = vec![5.];
let t = Instant::now();
for _ in 0..1000000 {
evaluator.evaluate_multiple(&params, &mut out);
}
println!("Eager time {:#?}", t.elapsed());
let mut out = vec![0.];
compiled.evaluate(&[5.], &mut out);
println!("{}", out[0]);
}
Loading

0 comments on commit bb49f9e

Please sign in to comment.