Multithreading

steviegiovanni
Posts: 3
Joined: Thu Aug 11, 2011 8:27 am

Multithreading

Post by steviegiovanni »

Does anybody know if bullet 2.78 supports multithreading or not? If it supports multithreading, how do I turn it on? I have a 12 cores PC and trying to simulate thousand of boxes stacked together.
xexuxjy
Posts: 225
Joined: Wed Jan 07, 2009 11:43 am
Location: London

Re: Multithreading

Post by xexuxjy »

Yes it does,

Have a look at the MultiThreadedDemo in the Demos directory to see how to setup a parallel constraint solver etc.
User avatar
Erwin Coumans
Site Admin
Posts: 4221
Joined: Sun Jun 26, 2005 6:43 pm
Location: California, USA

Re: Multithreading

Post by Erwin Coumans »

There is indeed multi-threading, although only the collision detection will provide some speedup.

See the MultiThreadedDemo, in particular replace the btCollisionDispatcher with SpuGatheringCollisionDispatcher:

Code: Select all

	btDefaultCollisionConstructionInfo cci;
	cci.m_defaultMaxPersistentManifoldPoolSize = 32768;
	m_collisionConfiguration = new btDefaultCollisionConfiguration(cci);
	
	int maxNumOutstandingTasks = 4;

#ifdef USE_WIN32_THREADING

m_threadSupportCollision = new Win32ThreadSupport(Win32ThreadSupport::Win32ThreadConstructionInfo(
								"collision",
								processCollisionTask,
								createCollisionLocalStoreMemory,
								maxNumOutstandingTasks));
#elif defined (USE_PTHREADS)
    PosixThreadSupport::ThreadConstructionInfo constructionInfo("collision",
								processCollisionTask,
								createCollisionLocalStoreMemory,
								maxNumOutstandingTasks);
    m_threadSupportCollision = new PosixThreadSupport(constructionInfo);
#else

	SequentialThreadSupport::SequentialThreadConstructionInfo colCI("collision",processCollisionTask,createCollisionLocalStoreMemory);
	SequentialThreadSupport* m_threadSupportCollision = new SequentialThreadSupport(colCI);
#endif

	m_dispatcher = new	SpuGatheringCollisionDispatcher(m_threadSupportCollision,maxNumOutstandingTasks,m_collisionConfiguration);
This parallel SpuGatheringCollisionDispatcher was originally written for PlayStation 3 SPUs, but it also works multi-threaded under Windows, Linux and Max OSX (pthreads).

There is a starting point for a parallel constraint solver, but it is currently slower than the regular (sequential) constraint solver because the parallel solver lacks SIMD and other optimizations.
This will be improved for Bullet 3.x.
Thanks,
Erwin
steviegiovanni
Posts: 3
Joined: Thu Aug 11, 2011 8:27 am

Re: Multithreading

Post by steviegiovanni »

Thanks Erwin,

I tried your snippet but my program crashed during runtime. the terminal printed
thread with taskid 0 with handle 00000288 exiting
Thread TERMINATED
thread with taskid 2 with handle 000002A0 exiting
Thread TERMINATED
thread with taskid 1 with handle 00000294 exiting
Thread TERMINATED
thread with taskid 3 with handle 000002AC exiting
Thread TERMINATED

Any idea what happen?