Skip to content

Commit

Permalink
Replace std::time with instant to work on wasm
Browse files Browse the repository at this point in the history
  • Loading branch information
stefan-k committed Jan 8, 2021
1 parent 5a1a6df commit 2aaef4d
Show file tree
Hide file tree
Showing 5 changed files with 18 additions and 10 deletions.
3 changes: 3 additions & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ anyhow = "1.0"
approx = "0.4.0"
bincode = "1.1.4"
ctrlc = { version = "3.1.2", optional = true }
instant = {version = "0.1", features = ["now"] }
gnuplot = { version = "0.0.37", optional = true}
paste = "1.0.0"
nalgebra = { version = "0.22.0", optional = true, features = ["serde-serialize"] }
Expand Down Expand Up @@ -49,6 +50,8 @@ default = []
nalgebral = ["nalgebra"]
ndarrayl = ["ndarray", "ndarray-linalg", "ndarray-rand"]
visualizer = ["gnuplot"]
wasm-bindgen = ["instant/wasm-bindgen"]
stdweb = ["instant/stdweb"]

[badges]
travis-ci = { repository = "argmin-rs/argmin", branch = "master" }
Expand Down
5 changes: 3 additions & 2 deletions src/core/executor.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ use crate::core::{
ArgminCheckpoint, ArgminIterData, ArgminKV, ArgminOp, ArgminResult, Error, IterState, Observe,
Observer, ObserverMode, OpWrapper, Solver, TerminationReason,
};
use instant;
use serde::de::DeserializeOwned;
use serde::{Deserialize, Serialize};
use std::path::Path;
Expand Down Expand Up @@ -103,7 +104,7 @@ where

/// Run the executor
pub fn run(mut self) -> Result<ArgminResult<O>, Error> {
let total_time = std::time::Instant::now();
let total_time = instant::Instant::now();

let running = Arc::new(AtomicBool::new(true));

Expand Down Expand Up @@ -161,7 +162,7 @@ where
}

// Start time measurement
let start = std::time::Instant::now();
let start = instant::Instant::now();

let data = self.solver.next_iter(&mut self.op, &self.state)?;

Expand Down
9 changes: 5 additions & 4 deletions src/core/iterstate.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
// copied, modified, or distributed except according to those terms.

use crate::core::{ArgminOp, OpWrapper, TerminationReason};
use instant;
use num::traits::float::Float;
use paste::item;
use serde::{Deserialize, Serialize};
Expand Down Expand Up @@ -62,7 +63,7 @@ pub struct IterState<O: ArgminOp> {
/// Number of modify evaluations so far
pub modify_func_count: u64,
/// Time required so far
pub time: std::time::Duration,
pub time: instant::Duration,
/// Reason of termination
pub termination_reason: TerminationReason,
}
Expand Down Expand Up @@ -136,7 +137,7 @@ impl<O: ArgminOp> IterState<O> {
hessian_func_count: 0,
jacobian_func_count: 0,
modify_func_count: 0,
time: std::time::Duration::new(0, 0),
time: instant::Duration::new(0, 0),
termination_reason: TerminationReason::NotTerminated,
}
}
Expand Down Expand Up @@ -212,7 +213,7 @@ impl<O: ArgminOp> IterState<O> {
TerminationReason,
"Set termination_reason"
);
setter!(time, std::time::Duration, "Set time required so far");
setter!(time, instant::Duration, "Set time required so far");
getter!(param, O::Param, "Returns current parameter vector");
getter!(prev_param, O::Param, "Returns previous parameter vector");
getter!(best_param, O::Param, "Returns best parameter vector");
Expand Down Expand Up @@ -269,7 +270,7 @@ impl<O: ArgminOp> IterState<O> {
TerminationReason,
"Get termination_reason"
);
getter!(time, std::time::Duration, "Get time required so far");
getter!(time, instant::Duration, "Get time required so far");
getter_option!(grad, O::Param, "Returns gradient");
getter_option!(prev_grad, O::Param, "Returns previous gradient");
getter_option!(hessian, O::Hessian, "Returns current Hessian");
Expand Down
5 changes: 3 additions & 2 deletions src/core/observers/visualizer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
extern crate gnuplot;
use crate::prelude::*;
use instant;
use std::sync::Mutex;

/// Visualize iterations of a solver for cost functions of type
Expand All @@ -34,7 +35,7 @@ pub struct Visualizer3d {
/// Optional visualized surface of cost function
surface: Option<Surface>,
/// Optional delay between iterations
delay: Option<std::time::Duration>,
delay: Option<instant::Duration>,
}

impl Visualizer3d {
Expand All @@ -54,7 +55,7 @@ impl Visualizer3d {
}

/// Set delay
pub fn delay(mut self, duration: std::time::Duration) -> Self {
pub fn delay(mut self, duration: instant::Duration) -> Self {
self.delay = Some(duration);
self
}
Expand Down
6 changes: 4 additions & 2 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -188,6 +188,7 @@
//! use argmin::solver::gradientdescent::SteepestDescent;
//! use argmin::solver::linesearch::MoreThuenteLineSearch;
//! # use argmin_testfunctions::{rosenbrock_2d, rosenbrock_2d_derivative};
//! # use instant;
//! #
//! # struct Rosenbrock {
//! # a: f64,
Expand Down Expand Up @@ -234,7 +235,7 @@
//! .run()?;
//! #
//! # // Wait a second (lets the logger flush everything first)
//! # std::thread::sleep(std::time::Duration::from_secs(1));
//! # std::thread::sleep(instant::Duration::from_secs(1));
//!
//! // print result
//! println!("{}", res);
Expand Down Expand Up @@ -352,6 +353,7 @@
//! # use argmin::solver::landweber::*;
//! # use argmin_testfunctions::{rosenbrock_2d, rosenbrock_2d_derivative};
//! # use argmin::core::Error;
//! # use instant;
//! #
//! # #[derive(Default)]
//! # struct Rosenbrock {}
Expand Down Expand Up @@ -388,7 +390,7 @@
//! .run()?;
//! #
//! # // Wait a second (lets the logger flush everything before printing to screen again)
//! # std::thread::sleep(std::time::Duration::from_secs(1));
//! # std::thread::sleep(instant::Duration::from_secs(1));
//! # println!("{}", res);
//! # Ok(())
//! # }
Expand Down

0 comments on commit 2aaef4d

Please sign in to comment.