CPU multithreading is working!

lunkhound
Posts: 99
Joined: Thu Nov 21, 2013 8:57 pm

Re: CPU multithreading is working!

Post by lunkhound » Sat Mar 03, 2018 1:36 am

To show how the new parallel solver can improve on and integrate with the existing multithreading, consider the demo from the example browser found under "Benchmarks/1000 stack". In this scene we have a large island, a medium island and 3 small islands. This graphic shows a comparison of using the existing multithreading (which doesn't handle single large islands well) with a new approach which augments the existing method with a parallel solver (which does handle large islands well -- but not small ones).

In each case we are using 4 threads to solve all 5 islands:

demo1kstack-batching-threshold-comparison.png
demo1kstack-batching-threshold-comparison.png (472.62 KiB) Viewed 1113 times

rebirth
Posts: 24
Joined: Thu Jul 24, 2014 2:48 am

Re: CPU multithreading is working!

Post by rebirth » Wed Mar 07, 2018 5:26 pm

I've tried getting this working, but VS is throwing an assert:

Code: Select all

btAssert( gBtTaskScheduler != NULL );  // call btSetTaskScheduler() with a valid task scheduler first! - line 444 of btThreads.cpp
I chose PPL in cmake and I'm creating the world like so:

Code: Select all

broadPhase = new btDbvtBroadphase();
collisionConfig = new btDefaultCollisionConfiguration();
collisionDispatcher = new btCollisionDispatcherMt(collisionConfig, 40);
solver = new btConstraintSolverPoolMt(BT_MAX_THREAD_COUNT);
dynamicsWorld = new btDiscreteDynamicsWorldMt(collisionDispatcher, broadPhase, solver, collisionConfig);
Am I missing something?

lunkhound
Posts: 99
Joined: Thu Nov 21, 2013 8:57 pm

Re: CPU multithreading is working!

Post by lunkhound » Thu Mar 08, 2018 6:51 pm

rebirth wrote:
Wed Mar 07, 2018 5:26 pm
I've tried getting this working, but VS is throwing an assert:

Code: Select all

btAssert( gBtTaskScheduler != NULL );  // call btSetTaskScheduler() with a valid task scheduler first! - line 444 of btThreads.cpp
I chose PPL in cmake and I'm creating the world like so:

Code: Select all

broadPhase = new btDbvtBroadphase();
collisionConfig = new btDefaultCollisionConfiguration();
collisionDispatcher = new btCollisionDispatcherMt(collisionConfig, 40);
solver = new btConstraintSolverPoolMt(BT_MAX_THREAD_COUNT);
dynamicsWorld = new btDiscreteDynamicsWorldMt(collisionDispatcher, broadPhase, solver, collisionConfig);
Am I missing something?
It looks like you are not setting up a task scheduler. To use PPL you would add something like:

Code: Select all

btSetTaskScheduler( btGetPPLTaskScheduler() );
Note that the version of bullet multithreading I just recently posted about, is still only a Pull Request -- it is not (yet) present in the main Bullet repo.
However you can get it here:
https://github.com/lunkhound/bullet3

Note the world setup is changed slightly to allow for the new parallel solver:

Code: Select all

broadPhase = new btDbvtBroadphase();
collisionConfig = new btDefaultCollisionConfiguration();
collisionDispatcher = new btCollisionDispatcherMt(collisionConfig, 40);
solverPool = new btConstraintSolverPoolMt(BT_MAX_THREAD_COUNT);
parallelSolver = new btSequentialImpulseConstraintSolverMt();  // create parallel solver
dynamicsWorld = new btDiscreteDynamicsWorldMt(collisionDispatcher, broadPhase, solverPool, parallelSolver, collisionConfig);
btSetTaskScheduler( btCreateDefaultTaskScheduler() );  // use built-in task scheduler

rebirth
Posts: 24
Joined: Thu Jul 24, 2014 2:48 am

Re: CPU multithreading is working!

Post by rebirth » Thu Mar 08, 2018 7:16 pm

Thanks. I was using the wrong master, which doesn't have the btSequentialImpulseConstraintSolverMt source files. I'll build it now and let you know what I think after a couple of days of testing.

lunkhound
Posts: 99
Joined: Thu Nov 21, 2013 8:57 pm

Re: CPU multithreading is working!

Post by lunkhound » Thu Mar 08, 2018 7:58 pm

rebirth wrote:
Thu Mar 08, 2018 7:16 pm
Thanks. I was using the wrong master, which doesn't have the btSequentialImpulseConstraintSolverMt source files. I'll build it now and let you know what I think after a couple of days of testing.
Sorry, that was my bad -- I had the wrong default branch on my repo. I fixed that now.

If you should run into any trouble, just post it here and I'll try to help. Also, for best results I recommend using the built-in task scheduler rather than the others. I haven't been testing the others much.

lunkhound
Posts: 99
Joined: Thu Nov 21, 2013 8:57 pm

Re: CPU multithreading is working!

Post by lunkhound » Sun Apr 15, 2018 8:40 pm

Note that my pull-request for the multithreaded constraint solver has been accepted and merged (a couple of weeks ago). So this is now available in the main Bullet repo.
Thanks Erwin!

With this latest PR, the task scheduler is now included in the core libs, which should make it easier than ever to get multithreading working in a standalone project. Feel free to post in this thread if you run into any trouble getting it set up.

przemir
Posts: 2
Joined: Thu May 03, 2018 2:21 pm

Re: CPU multithreading is working!

Post by przemir » Thu May 03, 2018 2:32 pm

lunkhound wrote:
Sun Apr 15, 2018 8:40 pm
Note that my pull-request for the multithreaded constraint solver has been accepted and merged (a couple of weeks ago). So this is now available in the main Bullet repo.
Thanks for sharing your code.

1. My program using single-threaded btDiscreteDynamicsWorld crashed because of btSequentialImpulseConstraintSolver::getOrInitSolverBody method returned -1
It happened because of line:

Code: Select all

	if ( btRigidBody* rb = btRigidBody::upcast( &body ) )
I use CollisionObjects aside from RigidBodies. After I commented line above, single-threaded simulation started to work again.
Similar line is in btSequentialImpulseConstraintSolverMt::getOrInitSolverBodyThreadsafe though.

2. After configuring btDiscreteDynamicsWorldMt with specified btSequentialImpulseConstraintSolverMt program crashes as well.
For btDiscreteDynamicsWorldMt without btSequentialImpulseConstraintSolverMt it works.
I guess (like in the case above) it may have problems when there are pure CollisionObjects inside dynamic world aside from RigidBodies.
To reproduce problem with a small amount of code, I modified your sample (MultiThreadedDemo) a bit, i.e replaced fragment of method MultiThreadedDemo::createStack

from:

Code: Select all

				btRigidBody* body = localCreateRigidBody( mass, trans, boxShape );
				body->setFriction( 1.0f );
				body->setRollingFriction( gSliderRollingFriction );
to:

Code: Select all

				if(iX % 2 == 0) {
					btRigidBody* body = localCreateRigidBody( mass, trans, boxShape );
					body->setFriction( 1.0f );
					body->setRollingFriction( gSliderRollingFriction );
				}
				else {
					btCollisionObject* collisionObject = new btCollisionObject();
					collisionObject->setWorldTransform(trans);
					collisionObject->setCollisionShape(boxShape);
					m_dynamicsWorld->addCollisionObject(collisionObject);
				}
Now it causes crash.

User avatar
Erwin Coumans
Site Admin
Posts: 4113
Joined: Sun Jun 26, 2005 6:43 pm
Location: California, USA
Contact:

Re: CPU multithreading is working!

Post by Erwin Coumans » Thu May 03, 2018 2:56 pm

Thanks for the reply. Can you report the issue in the Bullet https://github.com/bulletphysics/bullet3/issues tracker, so we can track progress on fixes?
przemir wrote:
Thu May 03, 2018 2:32 pm

1. My program using single-threaded btDiscreteDynamicsWorld crashed because of btSequentialImpulseConstraintSolver::getOrInitSolverBody method returned -1
It happened because of line:

Code: Select all

	if ( btRigidBody* rb = btRigidBody::upcast( &body ) )
I use CollisionObjects aside from RigidBodies. After I commented line above, single-threaded simulation started to work again.
Similar line is in btSequentialImpulseConstraintSolverMt::getOrInitSolverBodyThreadsafe though.

2. After configuring btDiscreteDynamicsWorldMt with specified btSequentialImpulseConstraintSolverMt program crashes as well.
For btDiscreteDynamicsWorldMt without btSequentialImpulseConstraintSolverMt it works.
I guess (like in the case above) it may have problems when there are pure CollisionObjects inside dynamic world aside from RigidBodies.
To reproduce problem with a small amount of code, I modified your sample (MultiThreadedDemo) a bit, i.e replaced fragment of method MultiThreadedDemo::createStack

from:

Code: Select all

				btRigidBody* body = localCreateRigidBody( mass, trans, boxShape );
				body->setFriction( 1.0f );
				body->setRollingFriction( gSliderRollingFriction );
to:

Code: Select all

				if(iX % 2 == 0) {
					btRigidBody* body = localCreateRigidBody( mass, trans, boxShape );
					body->setFriction( 1.0f );
					body->setRollingFriction( gSliderRollingFriction );
				}
				else {
					btCollisionObject* collisionObject = new btCollisionObject();
					collisionObject->setWorldTransform(trans);
					collisionObject->setCollisionShape(boxShape);
					m_dynamicsWorld->addCollisionObject(collisionObject);
				}
Now it causes crash.

lunkhound
Posts: 99
Joined: Thu Nov 21, 2013 8:57 pm

Re: CPU multithreading is working!

Post by lunkhound » Fri May 04, 2018 9:14 pm

I'll investigate this crash shortly.

lunkhound
Posts: 99
Joined: Thu Nov 21, 2013 8:57 pm

Re: CPU multithreading is working!

Post by lunkhound » Fri May 04, 2018 10:59 pm

przemir wrote:
Thu May 03, 2018 2:32 pm
To reproduce problem with a small amount of code, I modified your sample (MultiThreadedDemo) a bit, i.e replaced fragment of method MultiThreadedDemo::createStack

from:

Code: Select all

				btRigidBody* body = localCreateRigidBody( mass, trans, boxShape );
				body->setFriction( 1.0f );
				body->setRollingFriction( gSliderRollingFriction );
to:

Code: Select all

				if(iX % 2 == 0) {
					btRigidBody* body = localCreateRigidBody( mass, trans, boxShape );
					body->setFriction( 1.0f );
					body->setRollingFriction( gSliderRollingFriction );
				}
				else {
					btCollisionObject* collisionObject = new btCollisionObject();
					collisionObject->setWorldTransform(trans);
					collisionObject->setCollisionShape(boxShape);
					m_dynamicsWorld->addCollisionObject(collisionObject);
				}
Now it causes crash.
I am having trouble reproducing this crash. I have latest code from bullet main repo. Using Cmake and building with multithreading enabled. I applied the suggested change to MultiThreadedDemo::createStack function. Compiled on windows 10 with MSVC 2015, 64-bit, RelWithDebInfo configuration.
When I run the MultiThreadedDemo it isn't crashing and half of the boxes are not dynamic as expected.
Am I missing a step?

przemir
Posts: 2
Joined: Thu May 03, 2018 2:21 pm

Re: CPU multithreading is working!

Post by przemir » Sat May 05, 2018 10:40 am

lunkhound wrote:
Fri May 04, 2018 10:59 pm
I am having trouble reproducing this crash. I have latest code from bullet main repo. Using Cmake and building with multithreading enabled. I applied the suggested change to MultiThreadedDemo::createStack function. Compiled on windows 10 with MSVC 2015, 64-bit, RelWithDebInfo configuration.
I compiled with configuration you mentioned and this work without problems.
I encountered problems for 32-bit, Debug, Windows10.

Here is log (with assert):

Code: Select all

Version = 4.5.0 - Build 22.20.16.4836
Vendor = Intel
Renderer = Intel(R) HD Graphics 630
starting thread 0
started TaskScheduler thread 0 with threadHandle 00000378
starting thread 1
started TaskScheduler thread 1 with threadHandle 00000384
starting thread 2
started TaskScheduler thread 2 with threadHandle 00000390
starting thread 3
started TaskScheduler thread 3 with threadHandle 0000039C
starting thread 4
started TaskScheduler thread 4 with threadHandle 000003A8
starting thread 5
started TaskScheduler thread 5 with threadHandle 000003B4
starting thread 6
started TaskScheduler thread 6 with threadHandle 000003C0
b3Printf: Selected demo: Multithreaded Demo
Assert D:\Libraries\Bullet\bullet3-2018_05_03-modified\src\BulletDynamics\ConstraintSolver\btBatchedConstraints.cpp:215 (conInfo.numConstraintRows <= 6)
Edit
Erwin Coumans wrote:
Thu May 03, 2018 2:56 pm
Thanks for the reply. Can you report the issue in the Bullet https://github.com/bulletphysics/bullet3/issues tracker, so we can track progress on fixes?
Issue added.
https://github.com/bulletphysics/bullet3/issues/1673

Post Reply