aviator777 wrote:Without going into deep details and through topic what are the minimum steps required to set-up basic physics multi-threading ?
Is it just replacing btDiscreteDynamicsWorld with btDiscreteDynamicsWorldMt ?
No it isn't that simple. Bullet 2.x is single-threaded by default and does not have it's own threading system (task manager/job manager).
However, you can use Cmake to configure the Bullet 2.x core libraries (LinearMath, BulletCollision, BulletDynamics) to have certain operations be threadsafe.
In addition, you can also use Cmake to configure the bullet examples (some of them) to run multi-threaded using certain supported threading APIs (OpenMP, Intel's TBB (Threaded Building Blocks), and Microsoft's PPL (Parallel Patterns Library)).
If you want to integrate Bullet with multi-threading into your own application, you'll want to build the multi-threaded demo (as detailed below) so you have a working example to look at. Then you'll need to decide which threading task manager you want to use -- OpenMP, TBB, or even a custom one if your application already has a threading system (as long as it can support a "parallel-for" similar to what OpenMP and TBB have).
Then have a look at the source file https://github.com/bulletphysics/bullet ... MTBase.cpp
This is where all of the multithreading is done. Look at every instance in that file where "parallelFor" is called. You'll want to copy much of the code from that file into your own application (minus the example framework stuff).
This gives you the most flexibility in how Bullet multi-threading works in your application. If the multi-threading was built into the core Bullet libraries, then it would be tied to a specific threading API which could conflict with how the application uses threads. I wish it was simpler.
Here is how you use Cmake to configure the various options:
For OpenMP there are no external dependencies, so enabling it is just turning on a flag in Cmake. OpenMP works with Windows/Visual Studio and also Linux/GCC. When I tested it with Linux/Clang there was a problem with a missing "omp.h" system header, so it doesn't work with Clang at the moment.
Microsoft's Parallel Patterns Library (PPL) is also supported and has no dependencies, but it's Visual Studio only.
For Intel's TBB you'll need to install it separately, and then point Cmake to the include and lib directories.
Download a precompiled distribution of TBB from https://www.threadingbuildingblocks.org/download
(choose your OS), or you can grab it from https://github.com/wjakob/tbb
and build it from source. The latest version of TBB should work (just tested it on Windows), I've also tested it in the past with 4.3 and 4.4 versions with no issues. The version on Github is unofficial but adds a Cmake build system which could be nice for those who wish to build from source.
To configure Bullet for the multithreaded demo, use the Cmake build system to enable the flags
and at least one of the following flags:
- BULLET2_MULTITHREADED_OPEN_MP_DEMO (Windows/Visual Studio and linux/GCC; not Clang)
- BULLET2_MULTITHREADED_PPL_DEMO (Windows/Visual Studio 2010 or later only)
- BULLET2_MULTITHREADED_TBB_DEMO (requires external dependency -- see below)
Once TBB is installed somewhere, go back to the Bullet Cmake configuration and set paths for:
- BULLET2_TBB_INCLUDE_DIR (point this to the top "include" directory in the TBB install)
- BULLET2_TBB_LIB_DIR (point this to the directory with the correct "tbb.lib" file for your OS and compiler)