forked from matter-labs/bellman
-
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.
make generator and prover work with new futures
- Loading branch information
Showing
9 changed files
with
197 additions
and
17 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
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 |
---|---|---|
|
@@ -35,7 +35,7 @@ use crate::domain::{ | |
Scalar | ||
}; | ||
|
||
use crate::worker::{ | ||
use crate::worker_new::{ | ||
Worker | ||
}; | ||
|
||
|
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
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,148 @@ | ||
//! This is a dummy interface to substitute multicore worker | ||
//! in environments like WASM | ||
extern crate futures_new; | ||
|
||
use std::marker::PhantomData; | ||
|
||
use std::future::{Future}; | ||
use std::task::{Context, Poll}; | ||
use std::pin::{Pin}; | ||
|
||
use self::futures_new::channel::oneshot::{channel, Sender, Receiver}; | ||
use self::futures_new::executor::block_on; | ||
|
||
#[derive(Clone)] | ||
pub struct Worker { | ||
cpus: usize, | ||
} | ||
|
||
impl Worker { | ||
// We don't expose this outside the library so that | ||
// all `Worker` instances have the same number of | ||
// CPUs configured. | ||
pub(crate) fn new_with_cpus(_cpus: usize) -> Worker { | ||
Worker { | ||
cpus: 1, | ||
} | ||
} | ||
|
||
pub fn new() -> Worker { | ||
Self::new_with_cpus(1) | ||
} | ||
|
||
pub fn log_num_cpus(&self) -> u32 { | ||
0u32 | ||
} | ||
|
||
pub fn compute<F, T, E>( | ||
&self, f: F | ||
) -> WorkerFuture<T, E> | ||
where F: FnOnce() -> Result<T, E> + Send + 'static, | ||
T: Send + 'static, | ||
E: Send + 'static | ||
{ | ||
let result = f(); | ||
|
||
let (sender, receiver) = channel(); | ||
let _ = sender.send(result); | ||
|
||
let worker_future = WorkerFuture { | ||
receiver | ||
}; | ||
|
||
worker_future | ||
} | ||
|
||
pub fn scope<'a, F, R>( | ||
&self, | ||
elements: usize, | ||
f: F | ||
) -> R | ||
where F: FnOnce(&Scope<'a>, usize) -> R | ||
{ | ||
let chunk_size = if elements == 0 { 1 } else { elements }; | ||
|
||
let scope = Scope{ | ||
_marker: PhantomData | ||
}; | ||
|
||
f(&scope, chunk_size) | ||
} | ||
} | ||
#[derive(Clone)] | ||
pub struct Scope<'a> { | ||
_marker: PhantomData<& 'a usize> | ||
} | ||
|
||
impl<'a> Scope<'a> { | ||
pub fn spawn<F, R>( | ||
&self, | ||
f: F | ||
) -> R | ||
where F: FnOnce(&Scope<'a>) -> R | ||
{ | ||
f(&self) | ||
} | ||
} | ||
|
||
pub struct WorkerFuture<T, E> { | ||
receiver: Receiver<Result<T, E>> | ||
} | ||
|
||
impl<T: Send + 'static, E: Send + 'static> Future for WorkerFuture<T, E> { | ||
type Output = Result<T, E>; | ||
|
||
fn poll(self: Pin<&mut Self>, cx: &mut Context) -> Poll<Self::Output> | ||
{ | ||
let rec = unsafe { self.map_unchecked_mut(|s| &mut s.receiver) }; | ||
match rec.poll(cx) { | ||
Poll::Ready(v) => { | ||
if let Ok(v) = v { | ||
return Poll::Ready(v) | ||
} else { | ||
panic!("Worker future can not have canceled sender"); | ||
} | ||
}, | ||
Poll::Pending => { | ||
return Poll::Pending; | ||
} | ||
} | ||
} | ||
} | ||
|
||
impl<T: Send + 'static, E: Send + 'static> WorkerFuture<T, E> { | ||
pub fn wait(self) -> <Self as Future>::Output { | ||
block_on(self) | ||
} | ||
} | ||
|
||
|
||
#[test] | ||
fn test_trivial_singlecore_spawning() { | ||
use self::futures_new::executor::block_on; | ||
|
||
fn long_fn() -> Result<usize, ()> { | ||
let mut i: usize = 1; | ||
println!("Start calculating long task"); | ||
for _ in 0..1000000 { | ||
i = i.wrapping_mul(42); | ||
} | ||
|
||
println!("Done calculating long task"); | ||
|
||
Ok(i) | ||
} | ||
|
||
let worker = Worker::new(); | ||
println!("Spawning"); | ||
let fut = worker.compute(|| long_fn()); | ||
println!("Done spawning"); | ||
|
||
println!("Will sleep now"); | ||
|
||
std::thread::sleep(std::time::Duration::from_millis(10000)); | ||
|
||
println!("Done sleeping"); | ||
|
||
let _ = block_on(fut); | ||
} |