Skip to content

Add rayon compatability crate#27

Merged
NthTensor merged 1 commit intomainfrom
rayon-compat
Jul 8, 2025
Merged

Add rayon compatability crate#27
NthTensor merged 1 commit intomainfrom
rayon-compat

Conversation

@NthTensor
Copy link
Copy Markdown
Owner

Adds rayon-compat, the world's worst implementation of rayon-core.

Using this crate, it's possible to run a good amount of the rayon library on top of forte. Programs that use rayon will compile using this patch, but may crash unexpectedly and/or have UB. So don't, like, use this in production.

@NthTensor
Copy link
Copy Markdown
Owner Author

NthTensor commented Jun 28, 2025

Note, I was able to produce UB on this branch https://github.com/DGriffin91/obvhs/tree/insertion_removal_forte using cargo run --features parallel,timeit --release --example demoscene. I suspect it hit one of the unimplemented!() panics, and while the stack was being unwound, another task accessed a bad memory location and got the process terminated. The other possibility is that forte itself has an unsoundness issue somewhere (not impossible).

I probably need to port over rayon's panic safety before this can proceed. Would also be good to hook up miri and shuttle properly.

@NthTensor
Copy link
Copy Markdown
Owner Author

Note for later: Rayon uses in_place_scope, and it looked like I was going to have to add dedicated support for this. But forte actually runs all scope closures in place, so I just need to relax the Send bound on scope.

With that change, it should be possible to add proper implementations for both in_place_scope and scoped spawn. As far as I can tell it's only those three (join, in_place_scope and Scope::spawn) that rayon actually calls.

@NthTensor
Copy link
Copy Markdown
Owner Author

NthTensor commented Jun 30, 2025

The issue I was seeing before turned out not to be UB, but I went ahead and implemented some proper exception safety anyway for my own peace of mind. All of rayon's par iter api should now be supported and more or less safe to call. On most benchmarks I'm seeing similar performance, with forte tending to show up slightly behind rayon by a factor of about 1.1x to 1.5x.

@NthTensor
Copy link
Copy Markdown
Owner Author

Did some further optimization using the obvhs benchmarks and rewrote the heartbeat sender to distribute heartbeats more evenly amongst the live tenants. This also removes the use of timestamps for heartbeats, which would have posed a problem for web and no_std builds.

We are catching up to rayon on performance now.

obvhs physics example:
  rayon: 3.25s  bench | 1.62ms per iter
  forte: 3.48s  bench | 1.74ms per iter

obvhs demoscene example:
  rayon: 1.38s  generate bvh
  forte: 1.39s  generate bvh

@NthTensor NthTensor force-pushed the rayon-compat branch 2 times, most recently from ab1e90a to 1082a9f Compare July 1, 2025 01:00
@NthTensor
Copy link
Copy Markdown
Owner Author

CI appears to have a spurious failure. Meh, I'll fix it later.

@kdy1
Copy link
Copy Markdown

kdy1 commented Jul 3, 2025

I'm trying to use Forte with turbopack with vercel/next.js#81165, and the performance is to be on par with rayon-core. Nice work!

https://codspeed.io/vercel/next.js/branches/kdy1%2Fforte

@NthTensor
Copy link
Copy Markdown
Owner Author

Excellent! I'm still working on some optimizations, and I bet I can improve on that still. Just recently I have managed to beat rayon on the obvhs benchmarks (those changes should be pushed soon), but I'm still behind on rayon-demo, and vs chili in its fork join demo.

I think the next step should probably be to attempt to determine the cause of the time difference vs chili. Suspect it might be branch prediction.

Please let me know if you run into any issues.

@kdy1
Copy link
Copy Markdown

kdy1 commented Jul 4, 2025

Great to hear that!

Please let me know if there's anything I can help

@NthTensor NthTensor force-pushed the rayon-compat branch 7 times, most recently from 0d92a0d to 28241c9 Compare July 7, 2025 22:28
@NthTensor
Copy link
Copy Markdown
Owner Author

I'm now pretty happy with the performance of this as an initial jumping off point. Just need to clean up CI and then will be able to merge this.

@NthTensor NthTensor merged commit a32d90a into main Jul 8, 2025
4 checks passed
@NthTensor NthTensor deleted the rayon-compat branch July 8, 2025 23:00
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants