Skip to content
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

Allow joining over FlaggedStorage in parallel #541

Open
torkleyy opened this issue Jan 4, 2019 · 16 comments
Open

Allow joining over FlaggedStorage in parallel #541

torkleyy opened this issue Jan 4, 2019 · 16 comments

Comments

@torkleyy
Copy link
Member

torkleyy commented Jan 4, 2019

Problem

We cannot join over a FlaggedStorage as usual because it does not fulfill the DistinctStorage guarantee (since it pushes events when accessed).

Potential solution

We can push the events prior or after the actual access and keep it out of any parallel logic. This seems to be the technically most reasonable solution, but might be very hard to get right, especially with the API we have.

@torkleyy torkleyy mentioned this issue Jan 4, 2019
34 tasks
@torkleyy
Copy link
Member Author

torkleyy commented Jan 4, 2019

cc @Moxinilian Would you be interested in taking a look?

@Moxinilian
Copy link
Member

I think the easiest solution would be to extend the parallel iteration process to call a storage-specific method prior to parallel iteration. This method would pass the joined mask bitset to the storages. We could push all the edit events at that time.

I think the best way to implement this is by extending the Join trait with a pre-process method (breaking change should be avoidable by defaulting it to a no-op). The FlaggedStorage would implement that method by doing the event push logic (and forwarding the call to the wrapped storage).

The Join trait would therefore get a new safe method fn prepare(&mut self, mask: T::Mask) to which the iteration bitset is passed. This method would be implemented for joinable tuples as a call to all prepare of the joinables with the mask. The default implementation should be a no-op. As this is statically determined, LLVM should be able to truncate those no-op calls, ensuring this is zero-cost for non-flagged storages.

The FlaggedStorage wrapper type would do the flagging of entities there using the mask (this could even make flagging more efficient considering it will be an optimizable bitset AND, maybe?). It would implement Join and ParJoin manually. In the immutable implementation of Join, the prepare method will only forward it to the wrapped storage, but in the mutable iteration, it will first register the masked entities as dirty, then forward it to the wrapped storage.

This prepare method would be called here for serial and here for parallel.

Notice that this has the drawback of being inefficient, as the final mutable iteration might have filtered out some of the entities. It seems like the current FlaggedStorage doesn't do it either, so at least for now I believe it is acceptable.

Do you think this would be a reasonable fix?

@Xaeroxe
Copy link
Member

Xaeroxe commented Jan 4, 2019

I realize this might come off as flippant but serious question has anyone ever seriously used a parallel join?

@Moxinilian
Copy link
Member

Yes, in the simulation I'm making for my finals.

torkleyy added a commit to torkleyy/specs that referenced this issue Jan 5, 2019
537: Update rand requirement from 0.5.5 to 0.6.1 r=torkleyy a=dependabot[bot]

Updates the requirements on [rand](https://github.com/rust-random/rand) to permit the latest version.
<details>
<summary>Changelog</summary>

*Sourced from [rand's changelog](https://github.com/rust-random/rand/blob/master/CHANGELOG.md).*

> ## [0.6.1] - 2018-11-22
> - Support sampling `Duration` also for `no_std` (only since Rust 1.25) ([amethyst#649](https://github-redirect.dependabot.com/rust-random/rand/issues/649))
> - Disable default features of `libc` ([amethyst#647](https://github-redirect.dependabot.com/rust-random/rand/issues/647))
> 
> ## [0.6.0] - 2018-11-14
> 
> ### Project organisation
> - Rand has moved from [rust-lang-nursery](https://github.com/rust-lang-nursery/rand)
>   to [rust-random](https://github.com/rust-random/rand)! ([amethyst#578](https://github-redirect.dependabot.com/rust-random/rand/issues/578))
> - Created [The Rust Random Book](https://rust-random.github.io/book/)
>   ([source](https://github.com/rust-random/book))
> - Update copyright and licence notices ([amethyst#591](https://github-redirect.dependabot.com/rust-random/rand/issues/591), [amethyst#611](https://github-redirect.dependabot.com/rust-random/rand/issues/611))
> - Migrate policy documentation from the wiki ([amethyst#544](https://github-redirect.dependabot.com/rust-random/rand/issues/544))
> 
> ### Platforms
> - Add fork protection on Unix ([amethyst#466](https://github-redirect.dependabot.com/rust-random/rand/issues/466))
> - Added support for wasm-bindgen. ([amethyst#541](https://github-redirect.dependabot.com/rust-random/rand/issues/541), [amethyst#559](https://github-redirect.dependabot.com/rust-random/rand/issues/559), [amethyst#562](https://github-redirect.dependabot.com/rust-random/rand/issues/562), [amethyst#600](https://github-redirect.dependabot.com/rust-random/rand/issues/600))
> - Enable `OsRng` for powerpc64, sparc and sparc64 ([amethyst#609](https://github-redirect.dependabot.com/rust-random/rand/issues/609))
> - Use `syscall` from `libc` on Linux instead of redefining it ([amethyst#629](https://github-redirect.dependabot.com/rust-random/rand/issues/629))
> 
> ### RNGs
> - Switch `SmallRng` to use PCG ([amethyst#623](https://github-redirect.dependabot.com/rust-random/rand/issues/623))
> - Implement `Pcg32` and `Pcg64Mcg` generators ([amethyst#632](https://github-redirect.dependabot.com/rust-random/rand/issues/632))
> - Move ISAAC RNGs to a dedicated crate ([amethyst#551](https://github-redirect.dependabot.com/rust-random/rand/issues/551))
> - Move Xorshift RNG to its own crate ([amethyst#557](https://github-redirect.dependabot.com/rust-random/rand/issues/557))
> - Move ChaCha and HC128 RNGs to dedicated crates ([amethyst#607](https://github-redirect.dependabot.com/rust-random/rand/issues/607), [amethyst#636](https://github-redirect.dependabot.com/rust-random/rand/issues/636))
> - Remove usage of `Rc` from `ThreadRng` ([amethyst#615](https://github-redirect.dependabot.com/rust-random/rand/issues/615))
> 
> ### Sampling and distributions
> - Implement `Rng.gen_ratio()` and `Bernoulli::new_ratio()` ([amethyst#491](https://github-redirect.dependabot.com/rust-random/rand/issues/491))
> - Make `Uniform` strictly respect `f32` / `f64` high/low bounds ([amethyst#477](https://github-redirect.dependabot.com/rust-random/rand/issues/477))
> - Allow `gen_range` and `Uniform` to work on non-`Copy` types ([amethyst#506](https://github-redirect.dependabot.com/rust-random/rand/issues/506))
> - `Uniform` supports inclusive ranges: `Uniform::from(a..=b)`. This is
>   automatically enabled for Rust >= 1.27. ([amethyst#566](https://github-redirect.dependabot.com/rust-random/rand/issues/566))
> - Implement `TrustedLen` and `FusedIterator` for `DistIter` ([amethyst#620](https://github-redirect.dependabot.com/rust-random/rand/issues/620))
> 
> #### New distributions
> - Add the `Dirichlet` distribution ([amethyst#485](https://github-redirect.dependabot.com/rust-random/rand/issues/485))
> - Added sampling from the unit sphere and circle. ([amethyst#567](https://github-redirect.dependabot.com/rust-random/rand/issues/567))
> - Implement the triangular distribution ([amethyst#575](https://github-redirect.dependabot.com/rust-random/rand/issues/575))
> - Implement the Weibull distribution ([amethyst#576](https://github-redirect.dependabot.com/rust-random/rand/issues/576))
> - Implement the Beta distribution ([amethyst#574](https://github-redirect.dependabot.com/rust-random/rand/issues/574))
> 
> #### Optimisations
> 
> - Optimise `Bernoulli::new` ([amethyst#500](https://github-redirect.dependabot.com/rust-random/rand/issues/500))
> - Optimise `char` sampling ([amethyst#519](https://github-redirect.dependabot.com/rust-random/rand/issues/519))
> - Optimise sampling of `std::time::Duration` ([amethyst#583](https://github-redirect.dependabot.com/rust-random/rand/issues/583))
> 
> ### Sequences
></table> ... (truncated)
</details>
<details>
<summary>Commits</summary>

- See full diff in [compare view](https://github.com/rust-random/rand/commits/0.6.1)
</details>
<br />

Dependabot will resolve any conflicts with this PR as long as you don't alter it yourself. You can also trigger a rebase manually by commenting `@dependabot rebase`.

[//]: # (dependabot-automerge-start)
[//]: # (dependabot-automerge-end)

---

**Note:** This repo was added to Dependabot recently, so you'll receive a maximum of 5 PRs for your first few update runs. Once an update run creates fewer than 5 PRs we'll remove that limit.

You can always request more updates by clicking `Bump now` in your [Dependabot dashboard](https://app.dependabot.com).

<details>
<summary>Dependabot commands and options</summary>
<br />

You can trigger Dependabot actions by commenting on this PR:
- `@dependabot rebase` will rebase this PR
- `@dependabot recreate` will recreate this PR, overwriting any edits that have been made to it
- `@dependabot merge` will merge this PR after your CI passes on it
- `@dependabot cancel merge` will cancel a previously requested merge
- `@dependabot reopen` will reopen this PR if it is closed
- `@dependabot ignore this [patch|minor|major] version` will close this PR and stop Dependabot creating any more for this minor/major version (unless you reopen the PR or upgrade to it yourself)
- `@dependabot ignore this dependency` will close this PR and stop Dependabot creating any more for this dependency (unless you reopen the PR or upgrade to it yourself)
- `@dependabot use these labels` will set the current labels as the default for future PRs for this repo and language
- `@dependabot use these reviewers` will set the current reviewers as the default for future PRs for this repo and language
- `@dependabot use these assignees` will set the current assignees as the default for future PRs for this repo and language
- `@dependabot use this milestone` will set the current milestone as the default for future PRs for this repo and language
- `@dependabot badge me` will comment on this PR with code to add a "Dependabot enabled" badge to your readme

Additionally, you can set the following in your Dependabot [dashboard](https://app.dependabot.com):
- Update frequency (including time of day and day of week)
- Automerge options (never/patch/minor, and dev/runtime dependencies)
- Pull request limits (per update run and/or open at any time)
- Out-of-range updates (receive only lockfile updates, if desired)
- Security updates (receive only security updates, if desired)

Finally, you can contact us by mentioning @dependabot.

</details>



Co-authored-by: dependabot[bot] <[email protected]>
Co-authored-by: Thomas Schaller <[email protected]>
@michaelmelanson
Copy link

Hi @Xaeroxe, I randomly ran across this issue just now, and yes I have been using parallel joins fairly extensively. Here's a couple examples from a side project of mine:

https://github.com/michaelmelanson/spiking-neural-net/blob/master/src/simulation/models/izhikevich.rs#L44
https://github.com/michaelmelanson/spiking-neural-net/blob/master/src/simulation/systems/synaptic_transmission.rs#L22
https://github.com/michaelmelanson/spiking-neural-net/blob/master/src/simulation/learning/stdp.rs#L33

@Xaeroxe
Copy link
Member

Xaeroxe commented Jan 10, 2019

That's great to hear, thank you!

@michaelmelanson
Copy link

No problem at all. I think it's a bit of a weird use case for Specs, but it's worked worked surprisingly well for me on this project.

@VictorKoenders
Copy link

VictorKoenders commented Jan 30, 2019

I ran into this issue while trying to parallelize some systems with amethyst's Transform component which happens to have a FlaggedStorage storage.

It'd be nice to support this, but definitely not required

@stale
Copy link

stale bot commented Mar 31, 2019

This issue has been automatically marked as stale because it has not had recent activity. It will be closed in 7 days if no further activity occurs. Feel free to comment if this is still relevant.

@stale stale bot added the stale No activity since 60, issue is about to be closed label Mar 31, 2019
@Moxinilian
Copy link
Member

Nope nope nope

@stale stale bot removed the stale No activity since 60, issue is about to be closed label Mar 31, 2019
@stale
Copy link

stale bot commented May 30, 2019

This issue has been automatically marked as stale because it has not had recent activity. It will be closed in 7 days if no further activity occurs. Feel free to comment if this is still relevant.

@stale stale bot added the stale No activity since 60, issue is about to be closed label May 30, 2019
@OvermindDL1
Copy link

Another nope. ^.^

@stale stale bot removed the stale No activity since 60, issue is about to be closed label May 30, 2019
jaynus added a commit to jaynus/specs that referenced this issue Jun 28, 2019
…s making FlaggedStorage par_join compatible. Closes amethyst#541
@stale
Copy link

stale bot commented Jul 29, 2019

This issue has been automatically marked as stale because it has not had recent activity. It will be closed in 7 days if no further activity occurs. Feel free to comment if this is still relevant.

@stale stale bot added the stale No activity since 60, issue is about to be closed label Jul 29, 2019
@OvermindDL1
Copy link

This should probably use a non-stale label or whatever it was called?

@stale stale bot removed the stale No activity since 60, issue is about to be closed label Jul 29, 2019
@stale
Copy link

stale bot commented Sep 27, 2019

This issue has been automatically marked as stale because it has not had recent activity. It will be closed in 7 days if no further activity occurs. Feel free to comment if this is still relevant.

@stale stale bot added the stale No activity since 60, issue is about to be closed label Sep 27, 2019
@caelunshun
Copy link

Still relevant.

@stale stale bot removed the stale No activity since 60, issue is about to be closed label Sep 28, 2019
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

7 participants