-
Notifications
You must be signed in to change notification settings - Fork 19
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
seq WASM compatibility #33
base: master
Are you sure you want to change the base?
Conversation
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thanks a lot for your pull request. I have left some comments on your code that I would like to address before we merge this. Unfortunately, I have little time this month, so I will probably not be able to respond quickly to changes / questions.
src/sim/seq.rs
Outdated
@@ -185,7 +174,7 @@ where | |||
/// Kill off phenotypes using stochastic universal sampling. | |||
fn kill_off(&mut self, count: usize) { | |||
let ratio = self.population.len() / count; | |||
let mut i = ::rand::thread_rng().gen_range::<usize>(0, self.population.len()); | |||
let mut i = XorShiftRng::new_unseeded().gen_range::<usize>(0, self.population.len()); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It looks like new_unseeded
is deprecated and you should use the FromEntropy
or SeedableRng
trait instead.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yes, I know - fromEntropy won't work with WASM though, I was trying to find something that is sufficiently random and available, but I think it will come back to the user to take care of seeding it properly. This is an architecture issue, I'd say - because PRNGs will repeat after a while which means passing in a RNG is going to different than what it is now (I think).
But in any case, passing in a seed will need an interface, the question is where 😄
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I would think that passing in the seed would go well in the SimBuilder
. You could have a method that sets the seed, and have a default seed as well. That way you should be able to use Seedable
, not break the interface, and remove this deprecated call 😃
@@ -120,15 +118,6 @@ where | |||
} | |||
|
|||
self.iter_limit.inc(); | |||
self.duration = match self.duration { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Rather than remove this code, I would refactor it to a free function (not exported) and have that function have a different implementation based on the target. The regular implementation would be this removed code, while the WASM implementation would simply return None
.
Could you also tell me how you compiled this project for WASM (I tried building it and got over 90 errors for undefined identifiers, such as c_long
)?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'll get something up soon that demonstrates that
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
about the Instant stuff, it could be as easy as an if/else Option each time, I just thought that there might be a better idea 😆 I guess we can improve later on as well though
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'll get something up soon that demonstrates that
Thanks 👍. That will allow me to help you better.
about the Instant stuff [...]
I don't really understand what you mean here, but we can take a look at it once I get your PR to compile
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I successfully build it like that:
cargo +nightly build --target wasm32-unknown-unknown
wasm-bindgen target/wasm32-unknown-unknown/release/wasm_tspsolver.wasm --nodejs
@@ -27,8 +27,10 @@ use super::*; | |||
use super::select::*; | |||
use super::iterlimit::*; | |||
use super::earlystopper::*; | |||
use std::time::Instant; | |||
use std::boxed::Box; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'm not sure why you need this but it might be needed somewhere. Just make sure to remove unused use
statements if this isn't needed 😄
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
True laziness on my side. will be gone soon 👍
@m-decoster oh no worries, I am unexpectedly busy too. I am happy with the pace 👍 |
Hi @m-decoster, I added something "new". In a word, it's now possible to collect various statistics from each step's population fitness. This should replace (and expand!) the ability to collect step timings but has the drawback of adding a type parameter that has to be specified for the default Take a look and let me know what you think! Since we talked last there has also been a fix for From my view, the remaining things are:
|
@m-decoster friendly ping! 😄 Happy to discuss the changes whenever you have time |
@celaus Sorry, I will look at it ASAP. |
@celaus Unfortunately I am still unable to build the library with the The issues originate in As far as I can tell the problem lies in the
So the problem basically lies in the Since you are able to get it building, could you post detailed instructions? UPDATE: I managed to get it to build but removing the references to |
I also took the liberty of cleaning up some code. Overall it looks quite good, if we can get a PoC working of Anyway, here's the commit: 411b068. I will create a PR on your repo as well so that you can integrate this change. |
Hi @m-decoster - thanks for looking at this :) I am not sure why this doesn't build for you, it does for me without any issue, rayon enabled and nothing changed that isn't in the PR. In fact, I was surprised that crossbeam and rayon simply compiled to be honest. I have a version of this running on Azure Functions (i.e. Node 😄 ). I'll experiment and try to break my build, I'll let you know what I get.
|
Hi! So these are the (minor) changes to the seq module (incl. the update to the WASM compatible 0.5
rand
crate).I think there are a few things that are worth doing:
Instant
is available, WASM bindgen doesn't support it yet though.thread_rng
number generator doesn't work with WASM bindgen yet, a good solution would probably be to be able to pass in a custom RNG (likeMath.random
orXorShiftRng
).This PR should mostly show the required changes to
seq
. I am happy to use a completely different approach too!Tracked also in #32