Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
So this PR aim to add multithreading to nphysics.
This is still a draft, and a lot of things still need to be done.
I prefer to get some opinion before going further, in order to be sure we agree on the design choice.
So here is the actual choices made for this PR :
1. In
Bodyset
,body
andground
are now locked by aRwLock
(fromparking_lot
) :When body or body_mut are called, It's returning a
RwLockReadGuard
andRwLockWriteGuard
.This require
rigid_bidy()
andmultibody()
methods fromworld
to be changed so I made them returning aMappedReadLockGuard
(orMappedWriteLockGuard
for the "mut" methods), in order to havedowncast()
working.when
add_body()
is called it was returning a&mut
to a body trait object. With this PR, it should return theRwLock
. I changed it and made it return theBodyHandle
instead, because I thought it may be better than directly manipulate theRwLock
. I am not really sure about this!One of the problem with
RwLock
, is that if you want to manipulate a body from your app, and let's say having a write lock, and then you callstep()
. If you forgot to unlock the body, it's going to block the app, and finally, you are going to have a lot of responsability when using nphysics!!A solution would be to make the
RwLock
internal to the body, and each time you call a method, it's internally acquiring the appropriate lock ( read or write ) and then releasing it when finished. The problem is that it would probably kill performance.Another radically different alternative is to change
N: RealField
toN: Atomic<RealField>
with the atomic crate for example. Then It would be totally non-blocking, and theRwLock
problem would not exist. We could useRelaxed
ordering for maximum performance and rely onfence
for memory barrier.One possible issue would be for performance on weekly ordered architecture. I don't have experience on theses ones so I am not sure, but it seems that atomic operations are more expansive. How is it in reality? ( like on ARM for example)
2. A step is divided in two methods. The master, and the slaves.
The master function ( named
step_multithread_master()
here ) distribute and manage work made by the "slaves" (step_multithread_slave()
).Basically, the user clone a ref to the
World
struct between thread (withArc
for example), the main thread call the master function and the others call the slave one. In order to work with safe Rust, it need to entirely rely on interior mutability in order to safely shareWorld
between trade (With RwLock and/or Atomic like said upper).Master and slaves communicate through
MultithreadStep
struct, and each step has his own. The struct is still very basic and can be improved (withBarrier
instead of constantly loading atomic values in a loop for example)nphysics itself doesn't manage threads.
3. Example in Testbed3d
set_thread_number()
. If it's 0, then the originalstep()
method is called.Things That need to be done :
1. Use exclusively interior mutability in
World
with RwLock/AtomicBodyset
has been rewritten. Because of that, unsafe is temporary used in testbed3d to make multithreading work.2. Complete master and slave method
update_kinematics()
andupdate_dynamics()
have been done.3. Reorganize
step
in smaller step ?4. Rebase on top of CCD ?
5. Make ncollide multithreaded
6. Other suggestions ?