-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
13 changed files
with
236 additions
and
78 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,80 @@ | ||
#[macro_use] | ||
extern crate aegir; | ||
extern crate rand; | ||
|
||
use aegir::{ | ||
ids::{X, C}, | ||
ops, | ||
Context, | ||
Differentiable, | ||
Function, | ||
Identifier, | ||
Read, | ||
Write, | ||
Node, | ||
}; | ||
use std::time; | ||
|
||
pub struct Ctx { | ||
pub x: Vec<f64>, | ||
cache: Option<f64> | ||
} | ||
|
||
impl Ctx { | ||
pub fn new(x: Vec<f64>) -> Ctx { | ||
Ctx { x, cache: None, } | ||
} | ||
} | ||
|
||
impl AsMut<Self> for Ctx { | ||
fn as_mut(&mut self) -> &mut Self { self } | ||
} | ||
|
||
impl Context for Ctx {} | ||
|
||
impl Read<X> for Ctx { | ||
type Buffer = Vec<f64>; | ||
|
||
fn read(&self, _: X) -> Option<Vec<f64>> { Some(self.x.clone()) } | ||
} | ||
|
||
impl Read<C> for Ctx { | ||
type Buffer = f64; | ||
|
||
fn read(&self, _: C) -> Option<f64> { self.cache.clone() } | ||
} | ||
|
||
impl Write<C> for Ctx { | ||
fn write(&mut self, _: C, value: f64) { self.cache.replace(value); } | ||
} | ||
|
||
macro_rules! time_op { | ||
($node:ident) => {{ | ||
let repeated = $node | ||
.add($node.clone()) | ||
.add($node.clone()) | ||
.add($node.clone()) | ||
.add($node.clone()) | ||
.add($node.clone()) | ||
.add($node.clone()) | ||
.add($node.clone()); | ||
|
||
let mut ctx = Ctx::new(vec![1.0; 100_000_000]); | ||
|
||
let start = time::Instant::now(); | ||
|
||
repeated.evaluate(&mut ctx).ok(); | ||
|
||
time::Instant::now().duration_since(start) | ||
}} | ||
} | ||
|
||
fn main() { | ||
let x = X.into_var(); | ||
|
||
let basic = ops::Sum(ops::Negate(ops::Double(x))); | ||
let cached = basic.clone().cached(C); | ||
|
||
println!("Basic:\t{:?}", time_op!(basic)); | ||
println!("Cached:\t{:?}", time_op!(cached)); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,51 @@ | ||
use crate::{ | ||
Node, | ||
Write, | ||
Contains, | ||
Function, | ||
Identifier, | ||
AegirResult, | ||
}; | ||
|
||
#[derive(Copy, Clone, Debug, PartialEq)] | ||
pub struct Cached<N, T> { | ||
pub node: N, | ||
pub ident: T, | ||
} | ||
|
||
impl<N: Node, T: Identifier> Node for Cached<N, T> {} | ||
|
||
impl<N, T, I> Contains<I> for Cached<N, T> | ||
where | ||
N: Contains<I>, | ||
T: Identifier, | ||
I: Identifier + PartialEq<T>, | ||
{ | ||
fn contains(&self, ident: I) -> bool { | ||
ident == self.ident || self.node.contains(ident) | ||
} | ||
} | ||
|
||
impl<N, T, C> Function<C> for Cached<N, T> | ||
where | ||
N: Function<C, Value = C::Buffer>, | ||
T: Identifier, | ||
C: Write<T>, | ||
{ | ||
type Error = N::Error; | ||
type Value = N::Value; | ||
|
||
fn evaluate<CR: AsMut<C>>(&self, mut ctx: CR) -> AegirResult<Self, C> { | ||
let cached = ctx.as_mut().read(self.ident); | ||
|
||
if let Some(value) = cached { | ||
return Ok(value); | ||
} | ||
|
||
let value = self.node.evaluate(ctx.as_mut())?; | ||
|
||
ctx.as_mut().write(self.ident, value.clone()); | ||
|
||
Ok(value) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.